如何在广播上传扩展(iOS)中实现上传?

时间:2016-09-22 14:54:26

标签: ios upload webrtc broadcast replaykit

有人知道是否有能力将帧缓冲区从广播上传扩展上传到主机应用程序,或者我应该将它们直接加载到后端?我的目标是从重放工具包拦截帧缓冲区,将它们发送到我的应用程序并使用Webrtc通过我的应用程序广播视频。 将不胜感激任何帮助。提前谢谢。

3 个答案:

答案 0 :(得分:6)

广播开始时仅加载广播上传扩展和广播UI扩展。据我所知,没有以编程方式启动主机应用程序并在后台将任何数据流式传输到它。

但您可以在广播上传扩展中实现整个逻辑。您的CMSampleBuffer实施内容包含视频Go Live with ReplayKit。所有后处理和上传逻辑都取决于实现。因此,您可以解压缩并处理帧,然后以任何合适的方式上传到您的服务器。如果您需要任何配置或授权详细信息,只需在广播UI扩展中或甚至在主机应用程序中设置它们,然后将它们存储在共享存储中。

在互联网上或Apple文档中没有太多关于它的信息。但你仍然可以:

答案 1 :(得分:6)

我尝试使用Replay Kit和webRTC组合完全相同的事情。 webRTC是iOS上的基本问题,如果转到后台,webRTC无法处理视频流。所以..你可以在你的视频聊天应用程序处于前台时将你的应用程序的屏幕视频流传输到webRTC,但是为了流式传输其他应用程序,当你的应用程序变为后台时,你可能无法处理视频流而只能处理webRTC上的语音。

您最好从上传扩展程序将其上传到服务器,我已经浪费了太多时间将上传扩展程序连接到主机应用程序..绝对无法控制上传扩展程序。

答案 2 :(得分:3)

我为您准备了一些代码,我已经在我的项目中实现了它,并在google-groups上进行了讨论: https://groups.google.com/d/msg/discuss-webrtc/jAHCnB12khE/zJEu1vyUAgAJ

我将在此处为下一代传输代码:

首先,我在广播扩展中创建了其他类来管理WebRTC相关代码并将其命名为PeerManager。

  

使用本地流设置视频轨道,请注意,您应该这样做   在生成本地报价之前。

private func setupVideoStreaming() {        
        localStream = webRTCPeer.peerConnectionFactory.mediaStream(withStreamId: "\(personID)_screen_sharing")
        videoSource = webRTCPeer.peerConnectionFactory.videoSource()
        videoCapturer = RTCVideoCapturer(delegate: videoSource)
        videoSource.adaptOutputFormat(toWidth: 441, height: 736, fps: 15)
        let videoTrack = webRTCPeer.peerConnectionFactory.videoTrack(with: videoSource, trackId: "screen_share_track_id")
        videoTrack.isEnabled = true
        localStream.addVideoTrack(videoTrack)
        for localStream in webRTCPeer.localPeerConnection.peerConnection.localStreams {
            webRTCPeer.localPeerConnection.peerConnection.remove(localStream)

        }
        webRTCPeer.localPeerConnection.peerConnection.add(localStream)
    }
  

我从提供CMSampleBuffer的系统中收到了回调,我将其转换为RTCVideoFrame并发送到videoSource(模拟   VideoCapturer)

override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) {
        switch sampleBufferType {
            case RPSampleBufferType.video:
                // Handle video sample buffer
                guard peerManager != nil, let imageBuffer: CVImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
                    break
                }
                let pixelFormat = CVPixelBufferGetPixelFormatType(imageBuffer) // kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
                let timeStampNs: Int64 = Int64(CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sampleBuffer)) * 1000000000)                
                let rtcPixlBuffer = RTCCVPixelBuffer(pixelBuffer: imageBuffer)
                let rtcVideoFrame = RTCVideoFrame(buffer: rtcPixlBuffer, rotation: ._0, timeStampNs: timeStampNs)
                peerManager.push(videoFrame: rtcVideoFrame)
            case RPSampleBufferType.audioApp:
                break
            case RPSampleBufferType.audioMic:
                break
        }
    }
  

peerManager的代码,它是来自push功能的实现   上面的代码。没什么奇怪的,我们使用以下方法模拟Capturer的行为   委托。

 func push(videoFrame: RTCVideoFrame) {
        guard isConnected, videoCapturer != nil, isProcessed else {
            return
        }
        videoSource.capturer(videoCapturer, didCapture: videoFrame)
    }

现在,您可以生成本地报价,发送报价并随时随地传输数据了。尝试检查您的本地报价,如果您做的一切正确,您应该在报价中看到 a = sendonly

P.S。根据 VladimirTechMan 的建议,您还可以在AppRTCMobile演示应用程序中查看广播扩展的示例代码。我为您找到了链接,这是Objective-C示例https://webrtc.googlesource.com/src/+/358f2e076051d28b012529d3ae6a080838d27209 您应该对ARDBroadcastSampleHandler.m / .h和ARDExternalSampleCapturer.m / .h文件感兴趣。 永远不要忘记,您可以根据https://webrtc.org/native-code/ios/

的说明自行构建它
相关问题