shogo4405 / HaishinKit.swift

Camera and Microphone streaming library via RTMP and SRT for iOS, macOS, tvOS and visionOS.
https://docs.haishinkit.com/swift/latest
BSD 3-Clause "New" or "Revised" License
2.78k stars 618 forks source link

Attemped to start an invalid broadcast session! #1241

Closed minhvblc closed 1 year ago

minhvblc commented 1 year ago

Describe the bug

I want to create an app which can screen mirror my phone screen. When start recording, my phone show an error with this message: "Live broadcast to smarttv stopped due to: Attemped to start an invalid broadcast session". I think the ReplayKit.framework seems to be very memory hungry when the device is processing and stop my session. This is my code in SampleHandler file : `var httpStream : HTTPStream! var httpService: HLSService!

override func broadcastStarted(withSetupInfo setupInfo: [String : NSObject]?) {
    // User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.
   httpStream = HTTPStream()
    httpService = HLSService(domain: "", type: "_http._tcp", name: "Cast", port: 8080)
    httpService.addHTTPStream(httpStream)
    httpService.startRunning()
    httpStream.publish("hello")
    httpStream.expectedMedias.insert(.video)
}

override func broadcastPaused() {
    // User has requested to pause the broadcast. Samples will stop being delivered.
}

override func broadcastResumed() {
    // User has requested to resume the broadcast. Samples delivery will resume.
}

override func broadcastFinished() {
    // User has requested to finish the broadcast.
}

override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) {
    switch sampleBufferType {
    case RPSampleBufferType.video:
        self.httpStream.appendSampleBuffer(sampleBuffer, withType: .video)
        break
    case RPSampleBufferType.audioApp:
        // Handle audio sample buffer for app audio
        if let description = CMSampleBufferGetFormatDescription(sampleBuffer) {
            let dimensions = CMVideoFormatDescriptionGetDimensions(description)
            httpStream.videoSettings = VideoCodecSettings.init(videoSize: VideoSize(width: dimensions.width / 2,
                                                                                    height: dimensions.height / 2),
                                                               profileLevel: String(kVTProfileLevel_H264_Baseline_AutoLevel),
                                                               maxKeyFrameIntervalDuration: 2)
        }
        httpStream.appendSampleBuffer(sampleBuffer)
        break
    case RPSampleBufferType.audioMic:
        // Handle audio sample buffer for mic audio
        httpStream.appendSampleBuffer(sampleBuffer, withType: .audio)
        break
    @unknown default:
        // Handle other sample buffer types
        fatalError("Unknown type of sample buffer")
    }
}`

To Reproduce

  1. Start broadcast
  2. Run and see the error

Expected behavior

The braadcast session run normally and not be stop

Version

Latest

Smartphone info.

No response

Additional context

No response

Screenshots

No response

Relevant log output

No response

shogo4405 commented 1 year ago

Version 1.5.3 crashes because it does not support audioApp. Additionally, it is not possible to use audioApp and audioMic together.