gpt4 book ai didi

javascript - Kurento 端点顺序以录制截屏视频

转载 作者:搜寻专家 更新时间:2023-10-31 23:16:54 25 4
gpt4 key购买 nike

我正在尝试使用 kurento 媒体服务器录制网络摄像头,这是我在后端使用的功能:

var startScreen = (sessionId, ws, sdpOffer, callback) => {
console.log("Start screen")
getKurentoClient((error, kurentoClient) => {
if (error) {
return callback(error)
}

kurentoClient.create('MediaPipeline', (error, pipeline) => {
if (error) {
return callback(error)
}


var recordParams = {
stopOnEndOfStream: true,
mediaProfile: 'WEBM_VIDEO_ONLY',
uri: 'file:///PATH/TO/VIDEO/screen.webm'
}
pipeline.create('RecorderEndpoint', recordParams, (error, recorder) => {
if (error) return callback(error)

screenRecorder = recorder
recorder.record(() => console.log("recording"))
pipeline.create('WebRtcEndpoint', (error, webRtcEndpoint) => {
if (error) {
pipeline.release()
return callback(error)
}

screenRtcEndpoint = webRtcEndpoint

if (candidatesQueue[sessionId]) {
while(candidatesQueue[sessionId].length) {
var candidate = candidatesQueue[sessionId].shift()
webRtcEndpoint.addIceCandidate(candidate)
}
}
webRtcEndpoint.on('OnIceCandidate', (event) => {
var candidate = kurento.register.complexTypes.IceCandidate(event.candidate)
ws.send(JSON.stringify({
id: 'iceCandidateScreen',
candidate: candidate
}))
})

webRtcEndpoint.processOffer(sdpOffer, (error, sdpAnswer) => {
if (error) {
pipeline.release()
return callback(error)
}

sessions[sessionId] = {
'pipeline': pipeline,
'webRtcEndpoint': webRtcEndpoint
}

webRtcEndpoint.connect(webRtcEndpoint, error => {
if (error) {
return callback(error)
}
console.log("Connect webrtcEndpoint")
webRtcEndpoint.connect(screenRecorder, error => {
if (error) {
return callback(error)
}
console.log("connect to the screen recorder")
})

callback(null, sdpAnswer)
})

})

webRtcEndpoint.gatherCandidates((error) => {
if (error) {
return callback(error)
}
})
})
})
})
})
}

管道看起来像这样:

mediaPipeline -> recorderEndpoint and recorder.record -> WebRtcEndpoint connect webrtcendpoint -> connect recorder endpoint

在前端我这样做:

mediaConstrains = {
audio: false,
video: {
mandatory: {
maxWidth: 640,
maxHeight: 480,
maxFrameRate: 15,
minFrameRate: 1
}
}
}

var getMediaConstrains = () => mediaConstrains

var setMediaConstrains = (config) => {
mediaConstrains = config
}

var startScreen = () => {
var options = {
mediaConstrains: mediaConstrains,
onicecandidate: onIceCandidate,
configuration: { iceServers: [
{'url': 'turn:numb.viagenie.ca:3478',
'username': 'email@email.com',
'credential': 'passwordrandom'}
] }
}

webRtcPeerScreen = kurentoUtils.WebRtcPeer.WebRtcPeerSendonly(options, function (error) {
if (error) {
return onError(error)
}

this.generateOffer(onOfferScreen)
})
}

这仅适用于无法正常工作的截屏视频,我使用几乎完全相同的代码来录制完全正常工作的网络摄像头,这是停止功能:

var stop = () => {
if (webRtcPeerWebcam && webRtcPeerScreen) {
webRtcPeerWebcam.dispose()
webRtcPeerWebcam = null
webRtcPeerScreen.dispose()
webRtcPeerScreen = null

var message = {
id: 'stop'
}
sendMessage(message)
}
}

我在前端配置管道,但是,我将此消息发送到后端,然后此 wss 消息调用此函数:

var stop = (sessionId) => {
if (sessions[sessionId]) {
var pipeline = sessions[sessionId].pipeline
pipeline.release()
delete sessions[sessionId]
delete candidatesQueue[sessionId]
}
}

我认为这可能是为什么这不能用于录制截屏视频的问题,或者我可能未正确连接管道

无论如何谢谢!

PD:我在 KMS 日志中发现了这个:

KurentoMediaElementImpl MediaElementImpl.cpp:434 mediaFlowOutStateChange() <kmswebrtcendpoint373> Media N OT Flowing OUT in pad default with type video 

最佳答案

第一个也是最重要的问题是,在 SDP 协商之前,您正在处理候选人并收集这些候选人。这行不通,所以我认为您的 webrtc 根本无法工作,无论您如何连接端点。

其次,您在 WebRtcEndpoint 之前创建记录器,然后调用 record。录像机没有任何连接,也没有媒体流入。我建议您在将 WebRtcEndpoint 连接到记录器并在媒体流动后调用 record 方法。为此,您可以像这样向事件 MediaStateChanged 添加一个监听器

webRtcEndpoint.on('MediaStateChanged', function (event) {
if ((event.oldState !== event.newState) && (event.newState === 'CONNECTED')) {
// Start recording
recorderEndpoint.record();
}
});

创建后立即将 WebRtcEndpoint 连接到记录器。

此外,作为旁注,这一行没有意义,因为端点是 sendonly

webRtcEndpoint.connect(webRtcEndpoint, error => {

总结一下,这就是我的建议。不要忘记填补您可能发现的任何空白,因为如果不填补来自 OnIceCandidate 等的回调,它将无法工作。

var startScreen = (sessionId, ws, sdpOffer, callback) => {
console.log("Start screen")
getKurentoClient((error, kurentoClient) => {
if (error) {
return callback(error)
}

kurentoClient.create('MediaPipeline', (error, pipeline) => {
pipeline.create('RecorderEndpoint', recordParams, (error, recorder) => {
pipeline.create('WebRtcEndpoint', (error, webRtcEndpoint) => {
webRtcEndpoint.connect(screenRecorder, error => {
webRtcEndpoint.processOffer(sdpOffer, (error, sdpAnswer) => {
// The SDP negotiation must be completed before processing candidates
callback(null, sdpAnswer)
if (candidatesQueue[sessionId]) {
while (candidatesQueue[sessionId].length) {
var candidate = candidatesQueue[sessionId].shift()
webRtcEndpoint.addIceCandidate(candidate)
}
}

webRtcEndpoint.on('MediaStateChanged', function(event) {
// This will start recording right after media starts flowing
if ((event.oldState !== event.newState) && (event.newState === 'CONNECTED')) {
// Start recording
recorderEndpoint.record();
}
})

webRtcEndpoint.on('OnIceCandidate', (event) => { /* your code here */ })
// Candidates must be gathered after the negotiation is completed, and the
// event handler is bound
webRtcEndpoint.gatherCandidates((error) => { /* your code here */ })
})
})
})
})
})
})
}

关于javascript - Kurento 端点顺序以录制截屏视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36423773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com