How to disable audio in webrtc mobile app(ios) without changing in framework

3.2k Views Asked by At

I am working with webrtc mobile(ios). I can't disable audio in webrtc(ios). I have got no flag to disable audio.By changing in framwork/library it can done easily. My purpose is that I have to disable audio without changing in framework/library. Can anyone help me?.

2

There are 2 best solutions below

0
On BEST ANSWER

Update your question with code snippet, how you are creating mediaStrem or tracks(audio/video).

Generally with default Native WebRTC Framework,

RTCMediaStream localStream = [_factory mediaStreamWithStreamId:kARDMediaStreamId];
if(audioRequired) {
  RTCAudioTrack *aTrack = [_lmStream createLocalAudioTrack];
  [localStream addAudioTrack:aTrack];
}
RTCVideoTrack *vTrack = [_lmStream createLocalVideoTrack];
[localStream addVideoTrack:vTrack];
[_peerConnection addStream:localStream];

If you want to mute the Audio during the call, use below function.

- (void)enableAudio:(NSString *)id isAudioEnabled:(BOOL) isAudioEnabled {
  NSLog(@"Auido enabled: %d streams count:%d ", id, isAudioEnabled, _peerConnection.localStreams.count);
  if(_peerConnection.localStreams.count > 0) {
    RTCMediaStream *lStream = _peerConnection.localStreams[0];
    if(lStream.audioTracks.count > 0) { // Usually we will have only one track. If you have more than one, need to traverse all.
      // isAudioEnabled == 1 -> Unmute
      // isAudioEnabled == 0 -> Mute
      [lStream.audioTracks[0] setIsEnabled:isAudioEnabled];
    }
  }
}
0
On

in my case I didnt use streams and directly add audio track to peerconnection.

private func createMediaSenders() {
    let streamId = "stream"
    
    // Audio
    let audioTrack = self.createAudioTrack()
    self.pc.add(audioTrack, streamIds: [streamId])
    
    // Video
  /*  let videoTrack = self.createVideoTrack()
    self.localVideoTrack = videoTrack
    self.peerConnection.add(videoTrack, streamIds: [streamId])
    self.remoteVideoTrack = self.peerConnection.transceivers.first { $0.mediaType == .video }?.receiver.track as? RTCVideoTrack
    
    // Data
    if let dataChannel = createDataChannel() {
        dataChannel.delegate = self
        self.localDataChannel = dataChannel
    }*/
}

private func createAudioTrack() -> RTCAudioTrack {
    let audioConstrains = RTCMediaConstraints(mandatoryConstraints: nil, optionalConstraints: nil)
    let audioSource = sessionFactory.audioSource(with: audioConstrains)
    let audioTrack = sessionFactory.audioTrack(with: audioSource, trackId: "audio0")
    return audioTrack
}

to mute and unmute microphone I use this function

public func muteMicrophone(_ mute:Bool){
    
        for sender in pc.senders{

            if (sender.track?.kind == "audio") {
                sender.track?.isEnabled = mute
            }
        }
    }