Skip to content

Commit cbf7c1f

Browse files
committed
add mute/unmute microphone
1 parent c635734 commit cbf7c1f

File tree

7 files changed

+107
-2
lines changed

7 files changed

+107
-2
lines changed

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RootEncoder/Package.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// swift-tools-version: 5.10
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "RootEncoder",
8+
platforms: [.iOS(.v14)],
9+
products: [
10+
// Products define the executables and libraries a package produces, making them visible to other packages.
11+
.library(
12+
name: "RootEncoder",
13+
targets: ["RootEncoder"]
14+
),
15+
],
16+
targets: [
17+
// Targets are the basic building blocks of a package, defining a module or a test suite.
18+
// Targets can depend on other targets in this package and products from dependencies.
19+
.target(
20+
name: "RootEncoder"
21+
),
22+
.testTarget(
23+
name: "RootEncoderTests",
24+
dependencies: ["RootEncoder"]
25+
),
26+
]
27+
)

RootEncoder/Sources/RootEncoder/encoder/input/audio/MicrophoneManager.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class MicrophoneManager {
1515
private let audioEngine = AVAudioEngine()
1616
private var inputNode: AVAudioInputNode?
1717
private var inputFormat: AVAudioFormat?
18+
private var muted = false
1819

1920
private var callback: GetMicrophoneData?
2021

@@ -30,7 +31,7 @@ public class MicrophoneManager {
3031
}
3132
inputNode?.installTap(onBus: 0, bufferSize: 2048, format: inputFormat) { buffer, time in
3233
self.thread.async {
33-
self.callback?.getPcmData(buffer: buffer, time: time)
34+
self.callback?.getPcmData(buffer: buffer.mute(enabled: !self.muted), time: time)
3435
}
3536
}
3637
audioEngine.prepare()
@@ -52,4 +53,16 @@ public class MicrophoneManager {
5253
audioEngine.stop()
5354
audioEngine.inputNode.removeTap(onBus: 0)
5455
}
56+
57+
public func isMuted() -> Bool {
58+
return muted
59+
}
60+
61+
public func mute() {
62+
muted = true
63+
}
64+
65+
public func unmute() {
66+
muted = false
67+
}
5568
}

RootEncoder/Sources/RootEncoder/encoder/utils/EncoderUtils.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,40 @@ public extension AVAudioPCMBuffer {
4747
}
4848
return audioByteArray
4949
}
50+
51+
func mute(enabled: Bool) -> AVAudioPCMBuffer {
52+
if enabled {
53+
return self
54+
}
55+
let numSamples = Int(frameLength)
56+
if format.isInterleaved {
57+
let channelCount = Int(format.channelCount)
58+
switch format.commonFormat {
59+
case .pcmFormatInt16:
60+
int16ChannelData?[0].update(repeating: 0, count: numSamples * channelCount)
61+
case .pcmFormatInt32:
62+
int32ChannelData?[0].update(repeating: 0, count: numSamples * channelCount)
63+
case .pcmFormatFloat32:
64+
floatChannelData?[0].update(repeating: 0, count: numSamples * channelCount)
65+
default:
66+
break
67+
}
68+
} else {
69+
for i in 0..<Int(format.channelCount) {
70+
switch format.commonFormat {
71+
case .pcmFormatInt16:
72+
int16ChannelData?[i].update(repeating: 0, count: numSamples)
73+
case .pcmFormatInt32:
74+
int32ChannelData?[i].update(repeating: 0, count: numSamples)
75+
case .pcmFormatFloat32:
76+
floatChannelData?[i].update(repeating: 0, count: numSamples)
77+
default:
78+
break
79+
}
80+
}
81+
}
82+
return self
83+
}
5084
}
5185

5286
extension AVAudioTime {

RootEncoder/Sources/RootEncoder/library/base/CameraBase.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ public class CameraBase: GetMicrophoneData, GetCameraData, GetAacData, GetH264Da
141141
public func switchCamera() {
142142
cameraManager.switchCamera()
143143
}
144+
145+
public func isMuted() -> Bool {
146+
return microphone.isMuted()
147+
}
148+
149+
public func mute() {
150+
microphone.mute()
151+
}
152+
153+
public func unmute() {
154+
microphone.unmute()
155+
}
144156

145157
public func startPreview(resolution: CameraHelper.Resolution, facing: CameraHelper.Facing = .BACK, rotation: Int) {
146158
if (!isOnPreview()) {

RootEncoder/Sources/RootEncoder/library/base/DisplayBase.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ public class DisplayBase: GetMicrophoneData, GetCameraData, GetAacData, GetH264D
116116
streaming
117117
}
118118

119+
public func isMuted() -> Bool {
120+
return microphone.isMuted()
121+
}
122+
123+
public func mute() {
124+
microphone.mute()
125+
}
126+
127+
public func unmute() {
128+
microphone.unmute()
129+
}
130+
119131
public func setVideoCodec(codec: VideoCodec) {
120132
setVideoCodecImp(codec: codec)
121133
recordController.setVideoCodec(codec: codec)

app/RtmpSwiftUIView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct RtmpSwiftUIView: View, ConnectChecker {
7474
}
7575

7676

77-
@State private var endpoint = "rtmp://192.168.0.160:1935/live/pedro"
77+
@State private var endpoint = "rtmp://192.168.0.176:1935/live/pedro"
7878
@State private var bStreamText = "Start stream"
7979
@State private var bRecordText = "Start record"
8080
@State private var isShowingToast = false

0 commit comments

Comments
 (0)