Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
[EX-18] Add sound detection (#47)
Browse files Browse the repository at this point in the history
* Fix rtc url

* Add volumeThreshold to start function

* Add SoundDetection class

* Add sound detection to example app

* Fix typo in podspec

* Add SoundDetectionError, VolumeChangedListener, SoundDetectedListener and bug fix
  • Loading branch information
incubo4u authored Sep 19, 2023
1 parent 8259938 commit 37754f0
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 20 deletions.
8 changes: 4 additions & 4 deletions MembraneVideoroomDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
2F1E4DF72A6EABE300C857F9 /* membrane-webrtc-ios */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "membrane-webrtc-ios"; sourceTree = "<group>"; };
3B1642DA27A134C4008BBBA7 /* RoomController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoomController.swift; sourceTree = "<group>"; };
3B16A1BF2796C42B0095941F /* RoomView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RoomView.swift; sourceTree = "<group>"; };
3B16A1C02796C42B0095941F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -100,6 +99,7 @@
44ACD3F0293FCFA9001197EC /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Release.xcconfig; sourceTree = "<group>"; };
44C0FE97291595C700E61E9A /* MembraneRTCTest.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MembraneRTCTest.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
44C0FE99291595C700E61E9A /* PeerConnectionManagerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeerConnectionManagerTest.swift; sourceTree = "<group>"; };
C4887FE62A72BB7300056BCF /* membrane-webrtc-ios */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = "membrane-webrtc-ios"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -139,7 +139,7 @@
2F1E4DF62A6EABE300C857F9 /* Packages */ = {
isa = PBXGroup;
children = (
2F1E4DF72A6EABE300C857F9 /* membrane-webrtc-ios */,
C4887FE62A72BB7300056BCF /* membrane-webrtc-ios */,
);
name = Packages;
sourceTree = "<group>";
Expand Down Expand Up @@ -511,7 +511,7 @@
buildSettings = {
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = YGRG6VYGY2;
DEVELOPMENT_TEAM = J5FM626PE2;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = ScreenBroadcastExt/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = ScreenBroadcastExt;
Expand Down Expand Up @@ -696,7 +696,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"MembraneVideoroomDemo/Preview Content\"";
DEVELOPMENT_TEAM = YGRG6VYGY2;
DEVELOPMENT_TEAM = J5FM626PE2;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = MembraneVideoroomDemo/Info.plist;
Expand Down
2 changes: 1 addition & 1 deletion MembraneVideoroomDemo/Controllers/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class AppController: ObservableObject {
self.displayName = displayName
let engineUrl = Constants.getRtcEngineUrl().trimmingCharacters(in: CharacterSet(charactersIn: "/"))

let transportUrl = "\(engineUrl)/socket"
let transportUrl = "\(engineUrl)"

let transport = PhoenixTransport(
url: transportUrl, topic: "room:\(room)", params: [:], channelParams: ["isSimulcastOn": true])
Expand Down
43 changes: 39 additions & 4 deletions MembraneVideoroomDemo/Controllers/RoomController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ParticipantVideo: Identifiable, ObservableObject {
class RoomController: ObservableObject {
weak var room: MembraneRTC?

var soundDetection: SoundDetection?
var localVideoTrack: LocalVideoTrack?
var localAudioTrack: LocalAudioTrack?
var localScreencastTrack: LocalScreenBroadcastTrack?
Expand All @@ -43,7 +44,8 @@ class RoomController: ObservableObject {
@Published var isMicEnabled: Bool
@Published var isCameraEnabled: Bool
@Published var isScreensharingEnabled: Bool

@Published var isSoundDetected: Bool
@Published var soundVolumedB: Int
@Published var primaryVideo: ParticipantVideo?

@Published var participants: [String: Participant]
Expand All @@ -58,17 +60,38 @@ class RoomController: ObservableObject {
@Published var screencastSimulcastConfig: SimulcastConfig = SimulcastConfig(
enabled: false, activeEncodings: [])

func soundDetectionListener(_ detection: Bool) {
DispatchQueue.main.async {
self.isSoundDetected = detection
}
}
func volumeChangedListener(_ soundVolume: Int) {
DispatchQueue.main.async {
self.soundVolumedB = soundVolume
}
}

init(_ room: MembraneRTC, _ displayName: String) {
self.room = room
self.displayName = displayName
participants = [:]
participantVideos = []
do {
self.soundDetection = try SoundDetection()
} catch let error {
print("Error initializing SoundDetection: \(error)")
}

isMicEnabled = true
isCameraEnabled = true
isScreensharingEnabled = false
isSoundDetected = false
soundVolumedB = 0

self.displayName = displayName
participants = [:]
participantVideos = []

room.add(delegate: self)
self.soundDetection?.setOnVolumeChangedListener(listener: volumeChangedListener)
self.soundDetection?.setOnSoundDetectedListener(listener: soundDetectionListener)

self.room?.connect(metadata: .init(["displayName": displayName]))
}
Expand Down Expand Up @@ -113,6 +136,18 @@ class RoomController: ObservableObject {
}
}

func toggleSoundDetection() {
if !self.isMicEnabled {
do {
try self.soundDetection?.start()
} catch let error {
print("Error starting sound detection: \(error)")
}
} else {
self.soundDetection?.stop()
}
}

func toggleLocalTrack(_ type: LocalTrackType) {
guard let room = room,
let localParticipantId = localParticipantId,
Expand Down
15 changes: 13 additions & 2 deletions MembraneVideoroomDemo/Views/RoomView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ struct RoomView: View {
}
}

@ViewBuilder
func soundDetectionInfo() -> some View {
Text(room.isSoundDetected ? "you are muted" : "").font(.system(size: 12))
Text("volume dB: \(room.soundVolumedB)").font(.system(size: 12))
}

@ViewBuilder
func participantsVideoViews(_ participantVideos: [ParticipantVideo], size: CGFloat) -> some View {
ScrollView(.vertical) {
Expand Down Expand Up @@ -118,6 +124,7 @@ struct RoomView: View {

Button(action: {
self.room.toggleLocalTrack(type)
self.room.toggleSoundDetection()
}) {
Image(systemName: enabled ? enabledLabel : disabledLabel)
.font(.system(size: 32, weight: .medium))
Expand Down Expand Up @@ -170,7 +177,11 @@ struct RoomView: View {
.font(.system(size: 32, weight: .bold))
.foregroundColor(Color.red.darker())
}

Button(action: {}) {
Image(systemName: !room.isMicEnabled ? "mic.fill" : "mic.slash.fill")
.font(.system(size: 32, weight: .bold))
.foregroundColor(room.isSoundDetected ? Color.blue.lighter() : Color.gray.darker())
}
cameraSwitchButton()

if #available(iOS 12, *) {
Expand Down Expand Up @@ -220,7 +231,7 @@ struct RoomView: View {
.foregroundColor(.white)

simulcastControls()

soundDetectionInfo()
if let errorMessage = room.errorMessage {
Text(errorMessage).foregroundColor(.red)
} else {
Expand Down
9 changes: 0 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@
"version": "3.1.2"
}
},
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections.git",
"state": {
"branch": null,
"revision": "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version": "1.0.4"
}
},
{
"package": "SwiftDocCPlugin",
"repositoryURL": "https://github.com/apple/swift-docc-plugin",
Expand Down
Loading

0 comments on commit 37754f0

Please sign in to comment.