twilio / twilio-video-ios

Programmable Video SDK by Twilio
http://twilio.com/video
Other
64 stars 22 forks source link

Audio problems with own AVPlayer when muting TwilioVideo mic #242

Closed js closed 1 year ago

js commented 1 year ago

Description

I'm working on something akin to watching a video together where users each play a (non-twilio) video stream using AVPlayer while also being in a Twilio Video room and seeing each other. A kind of watch together experience.

If I mute the mic by unpublishing the local audio track (as per your recommendations), the sound of the AVPlayer changes in volume, first time you toggle the mic it seems the main video is all muted but is in fact very low volume, after another one or two toggles of the microphone it suddenly becomes very loud.

Before the watch together session starts the AVAudioSession is set and activated with setCategory(.playAndRecord, mode: .videoChat.

It almost seems like the Twilio SDK is somehow messing with the audio session when the local mic track is published/unpublished?

This only happens on device and not in the simulator, I'm also pretty sure it didn't happen on iOS 15 on device. But it's running iOS 16 now.

Interestingly, toggling the isEnabled on the microphone track doesn't not seem to have this effect and causes no surprises in volume levels on any of videos playing

Code

How the mic is toggled:

var participant: TwilioVideo.LocalParticipant?
    private(set) var audioTrack: TwilioVideo.LocalAudioTrack?

    var isMicrophoneOn: Bool {
        get {
            audioTrack?.isEnabled ?? false
        }
        set {
            if newValue {
                guard audioTrack == nil, let audioTrack = LocalAudioTrack(options: nil, enabled: true, name: TrackName.microphone) else { return }

                self.audioTrack = audioTrack
                participant?.publishAudioTrack(audioTrack)
            } else {
                guard let audioTrack = audioTrack else { return }

                participant?.unpublishAudioTrack(audioTrack)
                self.audioTrack = nil
            }

            delegate?.localVideoParticipantDidUpdate(self)
        }
    }

Expected Behavior

No perceived changes in audio quality from either participants or local video

Actual Behavior

Video is perceived as muted by users

Reproduces How Often

Always

Video iOS SDK

5.2.1 via SPM (as a dependency of a local package that's a dependency of the main app bundle)

Xcode

Version 14.0 (14A309)

iOS Version

iOS 16.0.2

iOS Device

iPhone 12 mini / iOS 16.0.2

piyushtank commented 1 year ago

@js Thanks for reaching out, and apologies for the delayed response.

In order to use mic and speaker, VideoSDK's audio device configures AVAudioSession. It is iOS behavior that it surpasses the audio volume of AVPlayer and give priority to the active call.

You can try doing audioDevice.enabled = false when you disable the track and enable it after enabling the track. Internally audioDevice.enabled property stops and start AudioUnit which might be helpful here. I haven't tried this myself but I think this might fix the problem you are facing.