shogo4405 / HaishinKit.swift

Camera and Microphone streaming library via RTMP and SRT for iOS, macOS, tvOS and visionOS.
BSD 3-Clause "New" or "Revised" License
2.78k stars 617 forks source link

After switching to camera app and recording streaming hangs and doesn't work anymore #1184

Closed mkrn closed 1 year ago

mkrn commented 1 year ago

Describe the bug

If you went live, then switched to another app that captures video or audio, then switched back the image is frozen and you cannot resume streaming

To Reproduce

  1. Open SwiftUI example app
  2. Start streaming
  3. Switch to Camera app
  4. Start recording video, for 5 seconds
  5. Return to SwiftUI Example app
  6. The video preview will be frozen, and streaming will have 0 FPS (if started)

Expected behavior

I expect the streaming video/audio to resume

Version

main branch, 1.4.5

Smartphone info.

iPhone 13 Pro

Additional context

I attempted to handle AV Interruptions to restart AV Session before starting streaming again:

 nc.publisher(for: AVAudioSession.interruptionNotification, object: nil)
            .sink { notification in
                logger.info(notification)

                guard let userInfo = notification.userInfo,
                    let interruptionTypeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? NSNumber else { return }

                let interruptionType = AVAudioSession.InterruptionType(rawValue: interruptionTypeValue.uintValue)

                if interruptionType == .ended {
                    guard let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt else { return }
                    let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
                    if options.contains(.shouldResume) {
                        self.startAVSession()
                    }

                }
            }
            .store(in: &subscriptions)

I also attempted to use didBecomeActiveNotification to start AVSession before restarting

nc.publisher(for: UIApplication.didBecomeActiveNotification, object: nil)
            .sink { notification in
                guard self.status == .paused else { return }
                self.status = .connecting
                self.signalStrength = .na
                self.startAVSession()
                self.startPublish()
            }.store(in: &subscriptions)

Screenshots

No response

Relevant log output

No response

shogo4405 commented 1 year ago

I had never used camera.app during the stream. It's an awesome test. I fixed it 252ab16. Thank you.

mkrn commented 1 year ago

Waiting for this to merge to main, already converted code to use new videoSettings format Looking good! Thanks!

shogo4405 commented 1 year ago

Merged to main branch https://github.com/shogo4405/HaishinKit.swift/commit/e44bf9187efedc8832a7cb6d5a2de72ad1b905fc.