如何使用Webrtc分享本地视频文件流

问题描述 投票:0回答:1

我尝试在 webrtc 中共享本地视频文件的流。在tomcat localhost中运行成功,但在网站中运行失败。 就当我共享 navigator.mediaDevices 的流时,它可以在任何地方工作,但共享视频文件的流只能在本地主机中工作。

var video = document.getElementById('myVideo');
var stream;
var rTCPeerConnection;

 /*
  stream = await navigator.mediaDevices.getUserMedia({
            audio: true,
            video: true
            });
 */
  
 video.onloadeddata = function(){
    if ($.browser.mozilla){
            stream = this.mozCaptureStream();
                }else{  
            stream = this.captureStream();
                }
    }
    
     video.src= "./myfile.webm";

function share_stream_to_peerConnection(){
    stream.getTracks().forEach(track =>  rTCPeerConnection.addTrack(track, stream)  );
}

javascript webrtc video-streaming peer-connection
1个回答
0
投票

这是我通过 webRTC 共享文件的一个简单示例,在这里您可以看到 webRTC 数据流是如何工作的。我使用 websocket 来处理 2 个用户之间的消息。也许会有帮助。

https://bit.cloud/intershare/galacfetch/hooks/web-rtc-local-share/~code/src/web-rtc-local-share.ts

// and here  an example of the  websocket in the backend side.
  let connectedClients = [];

  io.on('connection', function (socket) {
    // connected user
    connectedClients.push(socket.id);
    socket.on('disconnect', () => {
      const index = connectedClients.indexOf(socket.id);
      if (index > -1) {
        connectedClients.splice(index, 1);
      }
    });

    // Manejar oferta SDP
    socket.on('offer', (offer) => {
      const randomClients = getRandomClients(10, socket.id, connectedClients);
      if (randomClients.includes(socket.id)) {
        randomClients.forEach((clientId) => {
          socket.to(clientId).emit('offer', offer, socket.id);
        });
      }
    });

    // Manejar respuesta SDP
    socket.on('answer', (answer) => {
      const randomClients = getRandomClients(10, socket.id);
      if (randomClients.includes(socket.id)) {
        randomClients.forEach((clientId) => {
          socket.to(clientId).emit('answer', answer);
        });
      }
    });

    // Manejar ICE candidates
    socket.on('ice-candidate', (iceCandidate) => {
      const randomClients = getRandomClients(10, socket.id);
      if (randomClients.includes(socket.id)) {
        randomClients.forEach((clientId) => {
          socket.to(clientId).emit('ice-candidate', iceCandidate);
        });
      }
    });
  });

© www.soinside.com 2019 - 2024. All rights reserved.