webrtc - 如何在 peerjs 库中使用 addTransceiver?
问题描述
有没有关于如何在 peerjs 中使用 addTransceiver 的示例代码?实际上,我正在寻找一种在房间中的对等点之间创建双向连接的方法,并且如果该特定对等点选择共享音频或共享视频按钮,则可以将对等点的音频或视频共享给房间中的所有对等点。换句话说,我怎样才能通过在 peerjs 库中使用 addTransceiver 从连接中添加或删除轨道?有很多关于如何在 peerConnection 对象上使用 addTransceiver 的示例,但没有一个与 peerjs 库相关!使用 addTransceiver 时,我是否需要在 peerjs 库中考虑类似onnegotiationneeded
或类似的东西?createOffer
客户端代码:
let peers = {}; //list of all peers in this room
let myId = null; // global variable for handling peer ID
peer.on('open', id => {
myId = id; // make this client's ID global
socket.emit('join-room', ROOM_ID, id, clientName); //send your roomId, id and name to server .... then it will be sent to other clients
});
peer.on('connection', function(conn) {
let userId = conn.peer;
Object.assign(peers[userId], {connObj:conn}); //save this connection details to peers list (global object)
});
socket.on('new-user', (userId, clientName) => { // when new user joines the room
peers[userId] = {name: clientName};
let conn = createConnection(userId);
peers[userId] = {name:clientName, connObj:conn}; //save this connection details to peers list (global object)
});
function createConnection(userId) {
let conn = peer.connect(userId); //connection to peer with specified id
return conn; // return connection object
}
// this event will be fired when a peer start to share his video
socket.on('check-ontrack-event', userId => {
console.log('cheking audio shre: ')
let conn = peers[userId]["connObj"];
let pc = conn.peerConnection;
pc.ontrack = ({transceiver}) => {
console.log('new onTrack event fired :)');
console.log(transceiver.receiver.track);
}
});
let localAudioStream = null;
document.getElementById('audioShare').addEventListener('click', () => {
let mic_icon = document.querySelector('.fa-microphone');
if( (localAudioStream !== null) && (localAudioStream.getAudioTracks()[0] !== undefined) && (localAudioStream.getAudioTracks().length > 0) ) {
console.log('there is audio track');
const tracks = localAudioStream.getTracks();
tracks.forEach(function(track) {
track.stop();
});
localAudioStream.removeTrack(localAudioStream.getAudioTracks()[0]);
mic_icon.classList.remove('blue_color');
} else if( (localAudioStream == null) || (localAudioStream !== null && localAudioStream.getAudioTracks().length == 0) ) { //+++ calling Block
console.log('there is not any audio track');
navigator.mediaDevices.getUserMedia({
video: false,
audio: true
}).then(audioStream => {
localAudioStream = audioStream;
for(let peerId in peers) {
if(peerId == myId) continue;
let conn = peers[peerId]["connObj"];
let pc = conn.peerConnection;
pc.addTransceiver(localAudioStream.getTracks()[0], {direction: "sendrecv"});
socket.emit('audio-shared', myId);
}
mic_icon.classList.add('blue_color');
}).catch(err => {
console.log('error occured during audio get ' + err);
});
} else {
console.log('nothing to say!');
}
});
服务器端代码:
const fs = require('fs');
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const { v4: uuidV4 } = require('uuid');
const { ExpressPeerServer } = require('peer');
const peerServer = ExpressPeerServer(server, {
debug: true
});
.
.
.
io.on('connection', socket => {
console.log('user connected'); //testing
socket.on('join-room', (roomId, userId, clientName) => {
socket.join(roomId);
socket.on('audio-shared', userId => {
socket.to(roomId).emit('check-ontrack-event', userId);
console.log('user with id ' + userId + ' shared his audio');
});
});
});
首先,我通过使用peer.connect('remote-peer-id')
方法在每个对等点之间创建了一个连接,并connection
为房间中的每个对等点保存了对等点对象中的对象。之后,我尝试peerConnection
向该connection
对象添加音频流。但不幸的是它不起作用。
任何提示将不胜感激。
解决方案
推荐阅读
- visual-c++ - VS C++ 可以通过命令行卸载吗?
- tensorflow - 将 tfrecord 文件批量加载到 keras 模型中
- php - 哪种表结构在性能方面更有效
- react-native - 使用 ExtraData 更改 FlatList 中的唯一项
- python - 如何选择具有多个条件的行?
- scala - 将 JSON 从 URL 转换为数据帧(Pyspark 和 Scala)
- javascript - 如何将 api 中的数据和标签插入数据集中以显示图表
- .net - 使用接近设备发送和接收对 APDU 命令的响应
- vue.js - Vue cli 在服务期间删除 outputDir 文件夹
- python - 在 Python 中设置变量的值时出现运行时错误