首页 > 解决方案 > 如何快速将麦克风和 InApp Audio CMSampleBuffer 发送到 webRTC?

问题描述

我正在使用屏幕广播应用程序。我想在 WebRTC 服务器上发送我的屏幕录像。

override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) {
        //if source!.isSocketConnected {
            switch sampleBufferType {
            case RPSampleBufferType.video:
                // Handle video sample buffer
                source?.processVideoSampleBuffer(sampleBuffer)
                break
            case RPSampleBufferType.audioApp:
                // Handle audio sample buffer for app audio
                source?.processInAppAudioSampleBuffer(sampleBuffer)

                break
            case RPSampleBufferType.audioMic:
                // Handle audio sample buffer for mic audio
                source?.processAudioSampleBuffer(sampleBuffer)
                break
            @unknown default:
                break
            }
    }


// VideoBuffer Sending Method
func startCaptureLocalVideo(sampleBuffer: CMSampleBuffer) {

        let _pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
        if let pixelBuffer = _pixelBuffer {
            let rtcPixelBuffer = RTCCVPixelBuffer(pixelBuffer: pixelBuffer)
            let timeStampNs = CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sampleBuffer)) * 1000000000
            let rtcVideoFrame = RTCVideoFrame(buffer: rtcPixelBuffer, rotation: RTCVideoRotation._90, timeStampNs: Int64(timeStampNs))

            localVideoSource!.capturer(videoCapturer!, didCapture: rtcVideoFrame)
        }

    }

我成功地在 WebRTC 上发送 VIDEO Sample Buffer,但我陷入了 AUDIO 部分。

我没有找到如何将音频缓冲区发送到 WebRTC 的任何方法。

非常感谢您的回答。

标签: swiftwebrtcreplaykit

解决方案


我找到了解决方案,只需访问此链接并遵循指南: https ://github.com/pixiv/webrtc/blob/branch-heads/pixiv-m78/README.pixiv.md

WebRTC 团队不再支持原生框架,因此我们需要修改 WebRTC 源代码并重新构建它以在另一个应用程序中使用。

幸运的是,我找到了从 WebRTC 项目派生源代码的人,他更新了将 CMSampleBuffer 从广播扩展传递到 RTCPeerConnection 的函数。


推荐阅读