gpt4 book ai didi

javascript - 远程视频流根本不显示

转载 作者:行者123 更新时间:2023-11-29 16:00:47 25 4
gpt4 key购买 nike

我一直在尝试使用我代码中对等连接功能下的 .ontrack 来显示远程视频流。到目前为止,.ontrack 仅在调用方触发,而在被调用方甚至在调用函数时也不会触发。

检查 .ontrack 是否触发的日志会声明“Got Remote Stream”,但仅在调用方这可能是这里的问题,但我不确定为什么对方不进入持有的 IF 语句.ontrack 当它没有语句正在检查的 event.stream[0] 时。

我在下面添加了来自调用方和被调用方的控制台日志。图像中没有显示的是,一段时间后候选人将显示为空,但两个用户仍然保持连接。

ma​​in.js

'use strict';

var isInitiator;
var configuration = {
iceServers: [
{
urls: 'stun:stun.l.google.com:19302'
}
]
};
var pc = new RTCPeerConnection(configuration);

// Define action buttons.
const callButton = document.getElementById('callButton');
const hangupButton = document.getElementById('hangupButton');

/////////////////////////////////////////////

window.room = prompt('Enter room name:');

var socket = io.connect();

if (room !== '') {
console.log('Message from client: Asking to join room ' + room);
socket.emit('create or join', room);
}

socket.on('created', function(room) {
console.log('Created room ' + room);
isInitiator = true;
startVideo();
});

socket.on('full', function(room) {
console.log('Message from client: Room ' + room + ' is full :^(');
});

socket.on('joined', function(room) {
console.log('joined: ' + room);
startVideo();
callButton.disabled = true;
});

socket.on('log', function(array) {
console.log.apply(console, array);
});

////////////////////////////////////////////////

async function sendMessage(message) {
console.log('Client sending message: ', message);
await socket.emit('message', message);
}

// This client receives a message
socket.on('message', async function(message) {
try {
if (message.type === 'offer') {
await pc.setRemoteDescription(new RTCSessionDescription(message));
await pc
.setLocalDescription(await pc.createAnswer())
.then(function() {
sendMessage(pc.localDescription);
})
.catch(function(err) {
console.log(err.name + ': ' + err.message);
});
createPeerConnection();
} else if (message.type === 'answer') {
await pc.setRemoteDescription(new RTCSessionDescription(message));
} else if (message.type === 'candidate') {
await pc.addIceCandidate(candidate);
}
} catch (err) {
console.error(err);
}
});

////////////////////////////////////////////////////

const localVideo = document.querySelector('#localVideo');
const remoteVideo = document.querySelector('#remoteVideo');

// Set up initial action buttons status: disable call and hangup.
callButton.disabled = true;
hangupButton.disabled = true;

// Add click event handlers for buttons.
callButton.addEventListener('click', callStart);
hangupButton.addEventListener('click', hangupCall);

function startVideo() {
navigator.mediaDevices
.getUserMedia({
audio: true,
video: true
})
.then(function(stream) {
localVideo.srcObject = stream;
stream.getTracks().forEach(track => pc.addTrack(track, stream));
})
.catch(function(err) {
console.log('getUserMedia() error: ' + err.name);
});
callButton.disabled = false;
}

async function callStart() {
createPeerConnection();
callButton.disabled = true;
hangupButton.disabled = false;
if (isInitiator) {
console.log('Sending offer to peer');
await pc
.setLocalDescription(await pc.createOffer())
.then(function() {
sendMessage(pc.localDescription);
})
.catch(function(err) {
console.log(err.name + ': ' + err.message);
});
}
}

/////////////////////////////////////////////////////////

function createPeerConnection() {
try {
pc.ontrack = event => {
if (remoteVideo.srcObject !== event.streams[0]) {
remoteVideo.srcObject = event.streams[0];
console.log('Got remote stream');
}
};
pc.onicecandidate = ({ candidate }) => sendMessage({ candidate });
console.log('Created RTCPeerConnnection');
} catch (e) {
console.log('Failed to create PeerConnection, exception: ' + e.message);
alert('Cannot create RTCPeerConnection object.');
return;
}
}

function hangupCall() {
pc.close();
pc = null;
callButton.disabled = false;
hangupButton.disabled = true;
console.log('Call Ended');
}

index.js

'use strict';

var express = require('express');
var app = (module.exports.app = express());
var path = require('path');

var server = require('http').createServer(app);
var io = require('socket.io')(server);
const PORT_NO = process.env.APP_PORT || 3000;
server.listen(PORT_NO);

app.get('/', function(request, response) {
response.sendFile(path.resolve('./index.html'));
});

app.use(express.static('.'));
io.on('connection', socket => {
function log() {
const array = ['Message from server:'];
for (let i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}

socket.on('message', message => {
log('Got message:', message);
socket.broadcast.emit('message', message);
});

socket.on('create or join', room => {
var clientsInRoom = io.sockets.adapter.rooms[room];
var numClients = clientsInRoom
? Object.keys(clientsInRoom.sockets).length
: 0;

// max two clients
if (numClients === 2) {
socket.emit('full', room);
return;
}

log('Room ' + room + ' now has ' + (numClients + 1) + ' client(s)');

if (numClients === 0) {
socket.join(room);
log('Client ID ' + socket.id + ' created room ' + room);
socket.emit('created', room, socket.id);
} else {
log('Client ID ' + socket.id + ' joined room ' + room);
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room, socket.id);
io.sockets.in(room).emit('ready');
}
});
});

Callee side (in Safari)

Caller side (in Firefox)

最佳答案

让加入者成为发起者。

我猜 'created' 发生在 'joined' 之前? IE。一方在另一方加入之前创建了房间?

因为您的 startVideo() 不仅仅启动本地视频——它实际上开始连接协商——我怀疑您在第二方准备好之前就开始协商了,一场比赛。而是尝试:

socket.on('created', function(room) {
console.log('Created room ' + room);
startVideo();
});

socket.on('joined', function(room) {
console.log('joined: ' + room);
isInitiator = true; // <-- begin negotiating once 2nd party arrives.
startVideo();
});

关于javascript - 远程视频流根本不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53582983/

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