我想流媒体我的应用程序到twitch,youtube或这样的流媒体服务,而没有任何其他的应用程序,如暴徒粉碎。
根据苹果公司的说法,通过使用广播扩展,我可以流式传输我的应用程序屏幕。广播扩展将视频数据作为CMSampleBuffer的一种类型。然后我应该把数据发送到rtmp服务器,如youtube,twitch等。
我认为如果我可以获得视频数据,我就可以在不使用应用程序中的广播扩展的情况下流式传输其他内容。因此,我尝试发送RPScreenRecorder数据到rtmp服务器,但我不工作。
这是我写的代码。我使用HaishinKit开源框架来实现rtmp通信。(https://github.com/shogo4405/HaishinKit.swift/tree/master/Examples/iOS/Screencast)
let rpScreenRecorder : RPScreenRecorder = RPScreenRecorder.shared()
private var broadcaster: RTMPBroadcaster = RTMPBroadcaster()
rpScreenRecorder.startCapture(handler: { (cmSampleBuffer, rpSampleBufferType, error) in
if (error != nil) {
print("Error is occured \(error.debugDescription)")
} else {
if let description: CMVideoFormatDescription = CMSampleBufferGetFormatDescription(cmSampleBuffer) {
let dimensions: CMVideoDimensions = CMVideoFormatDescriptionGetDimensions(description)
self.broadcaster.stream.videoSettings = [
"width": dimensions.width,
"height": dimensions.height ,
"profileLevel": kVTProfileLevel_H264_Baseline_AutoLevel
]
}
self.broadcaster.appendSampleBuffer(cmSampleBuffer, withType: .video)
}
}) { (error) in
if ( error != nil) {
print ( "Error occured \(error.debugDescription)")
} else {
print ("Success")
}
}
}如果您有任何解决方案,请回答我:)
发布于 2020-02-03 19:00:21
我已经尝试了类似的设置,可以实现你想要的,只需要稍微调整一下:
我在您的示例中看不到这一点,但请确保广播公司的端点设置正确。例如:
let endpointURL: String = "rtmps://live-api-s.facebook.com:443/rtmp/"
let streamName: String = "..."
self.broadcaster.streamName = streamName
self.broadcaster.connect(endpointURL, arguments: nil)然后,在startCapture的处理程序块中,您需要按缓冲区类型进行过滤,以便将正确的数据发送到流。在这种情况下,您只发送视频,这样我们就可以忽略音频。(您也可以找到一些使用HaishinKit发送音频的示例。)例如:
RPScreenRecorder.shared().startCapture(handler: { (sampleBuffer, type, error) in
if type == .video, broadcaster.connected {
if let description: CMVideoFormatDescription = CMSampleBufferGetFormatDescription(sampleBuffer) {
let dimensions: CMVideoDimensions = CMVideoFormatDescriptionGetDimensions(description)
broadcaster.stream.videoSettings = [
.width: dimensions.width,
.height: dimensions.height ,
.profileLevel: kVTProfileLevel_H264_Baseline_AutoLevel
]
}
broadcaster.appendSampleBuffer(sampleBuffer, withType: .video)
}
}) { (error) in }此外,还要确保在流式传输过程中更新屏幕。我注意到,如果你用RPScreenRecorder记录一个静态窗口,那么它只会在有新的视频数据需要发送时才更新处理程序。为了测试,我添加了一个简单的UISlider,当您移动提要时,它会更新提要。
我已经用Facebook Live对它进行了测试,我认为它应该也能与其他RTMP服务一起工作。
https://stackoverflow.com/questions/52068782
复制相似问题