用于调用的WebRTC函数调用流程[Android] [英] WebRTC flow of function calls for making calls [Android]

查看:117
本文介绍了用于调用的WebRTC函数调用流程[Android]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在参考并且也正在浏览AppRTCDemo的源代码,这是WebRTC的演示应用程序.

I am referring and also going through source code of AppRTCDemo which is a demo application for WebRTC.

我正在尝试的是:

  1. 构建我自己的WebRTC应用程序,该应用程序将在Android设备上进行AV调用.
  2. 替换现有的https://apprtc.appspot.com/服务器和相关功能.
  1. Build my own WebRTC application which will do AV calls on a Android Device.
  2. Replace existing https://apprtc.appspot.com/ server and related functionality.

为归档以上几点,我想了解WebRTC函数调用的基本流程以及进行/接收调用的步骤(我需要调用的功能并在那里流动).

For archiving above points, I want to understand basic flow of WebRTC function calls and steps to make/receive calls (functions that i need to calls and there flow).

我遍历了源代码并且了解了几件事, 但是由于代码很难理解,并且没有任何文档.

I have gone through the source code and understood few things, but as code is pretty complicated to understand, and without any documentation.

如果有人提供任何示例或文档来说明进行/接收AV呼叫的步骤(我们如何获取/设置SDP,如何呈现本地/远程视频等),这将是非常有帮助的.

It will be great help if some one provides any examples or documents explaining the steps for making/receiving AV calls (how we get/set SDP, how to render local/remote video etc.).

我看过这些帖子,对您有很大帮助:

I have seen these posts and are very helpful:

  1. WebRTC Java服务器故障
  2. https://www.webrtc-experiment.com/docs/WebRTC -PeerConnection.html
  1. WebRTC java server trouble
  2. https://www.webrtc-experiment.com/docs/WebRTC-PeerConnection.html

我能够构建和运行AppRTCDemo App.

I am able to build and run AppRTCDemo App.

任何对此的帮助将是巨大的帮助!

Any help on this will be great help!

推荐答案

没有时间轴,它是异步的,但我将尝试解释,但有两个主要流程,即SDP的要约和回答流程以及icecandidate流程.

There is no timeline, it's asynchronous but i will try to explain but there is two main flow, the flow of offer and answer with SDP and the flow of icecandidate.

流程1:SDP

第1步-提供同伴:

在报价端,创建一个RTCPeerconnection(以stun,trun服务器作为参数).

On the offer side, create a RTCPeerconnection (with stun, trun servers as parameters).

    var STUN = {
        url:'stun:stun.l.google.com:19302'
    };

    var TURN = {
        url: 'turn:homeo@turn.bistri.com:80',
        credential: 'homeo'
    };

    var iceServers = {
        iceServers: [STUN, TURN]
    };

    var peer = new RTCPeerConnection(iceServers);

第2步-提供同伴:

根据您的约束调用getUserMedia.在成功回调中,使用addStream方法将流添加到RTCPeerconnection.然后,您可以通过在Peerconnection对象上调用createOffer来创建商品.

Call getUserMedia with your constraints. In the success callback, add the stream to the RTCPeerconnection using the addStream method. Then you can create the offer with calling createOffer on the Peerconnection Object.

    navigator.webkitGetUserMedia(
    {
      audio: false,
      video: {
        mandatory: {
          maxWidth: screen.width,
          maxHeight: screen.height,
          minFrameRate: 1,
          maxFrameRate: 25
        }
      }
    },
    gotStream, function(e){console.log("getUserMedia error: ", e);});

    function gotStream(stream){
      //If you want too see your own camera
      vid.src = webkitURL.createObjectURL(stream);

      peer.addStream(stream);

      peer.createOffer(onSdpSuccess, onSdpError);
    }

第3步-提供同伴:

在createOffer的回调方法中,将参数(sdp提供)设置为RTCPeerConnection(将开始收集ICE候选者)的localDescription.然后使用信令服务器将要约发送给其他对等方. (我不会描述信令服务器,它只是将数据从另一个传递到另一个).

In the callback method of the createOffer, set the parameter (the sdp offer) as the localDescription of the RTCPeerConnection (who will start gathering the ICE candidate). Then send the offer to the other peer using the signaling server. (I will not describe signaling server, it's just passing data to one from another).

    function onSdpSuccess(sdp) {
        console.log(sdp);
        peer.setLocalDescription(sdp);
        //I use socket.io for my signaling server
        socket.emit('offer',sdp);
    }

第5步-回答同伴:

答案对方,每次收到要约时,先与TURN,STUN服务器创建RTCPeerconnection,然后再创建getUserMedia,然后在回调中,将流添加到RTCPeerConnection.对于SDP报价,将setRemoteDescription与sdpOffer一起使用.然后触发createAnswer. 在createAnswer的成功回调中,将setLocalDescription与参数一起使用,然后使用信令服务器将答案sdp发送到商品对等方.

The answer peer, each time it receives an offer, create a RTCPeerconnection with TURN, STUN server, then getUserMedia, then in the callback, add the stream to the RTCPeerConnection. With the SDP offer use setRemoteDescription with the sdpOffer. Then Trigger the createAnswer. In the success callback of the createAnswer, use setLocalDescription with the parameter and then send the answer sdp to the offer peer using signaling server.

    //Receive by a socket.io socket
    //The callbacks are useless unless for tracking
    socket.on('offer', function (sdp) {
        peer.setRemoteDescription(new RTCSessionDescription(sdp), onSdpSuccess, onSdpError);

        peer.createAnswer(function (sdp) {
            peer.setLocalDescription(sdp);
            socket.emit('answer',sdp);
        }, onSdpError);

    }); 

第7步:要约同行

在RTCPeerConnection上接收sdp答案setRemoteDescription.

Receive the sdp answer, setRemoteDescription on the RTCPeerConnection.

    socket.on('answer', function (sdp) {
      peer.setRemoteDescription(new RTCSessionDescription(sdp), function(){console.log("Remote Description Success")}, function(){console.log("Remote Description Error")});
    }); 

流程2:ICECandidate

Flow 2 : ICECandidate

双方:

每次RTCPeerConnection启用onicecandidate时,都将其通过信号发送服务器发送到另一个对等方. 收到来自信令服务器的icecandidate时,只需使用addIceCandidate(New RTCIceCandidate(obj))将其添加到RTCPeerConnection中即可.

Each time the RTCPeerConnection fire onicecandidate, send the candidate to the other peer through signalingserver. When a icecandidate is received, coming from signaling server, just add it to the RTCPeerConnection using the addIceCandidate(New RTCIceCandidate(obj))

    peer.onicecandidate = function (event) {
      console.log("New Candidate");
      console.log(event.candidate);

      socket.emit('candidate',event.candidate);
    };

    socket.on('candidate', function (candidate) {
      console.log("New Remote Candidate");
      console.log(candidate);

      peer.addIceCandidate(new RTCIceCandidate({
          sdpMLineIndex: candidate.sdpMLineIndex,
          candidate: candidate.candidate
      }));
    }); 

最后:

如果以上两个流程运行良好,请在每个RTCPeerConnection上使用onaddstream事件.当ICE候选人相互配对并找到最佳的点对点方式时,他们将添加与SDP协商并通过对等连接的流.因此,在这种情况下,您只需要将流添加到视频标签中就可以了.

If two flow above works well use the onaddstream event on each RTCPeerConnection. When ICE Candidates will pair each other and find the best way for peer-to-peer, they will add the stream negociated with the SDP and that is going through the peer to peer connection. So in this event, you juste need to add your stream then to a video tag for example and it's good.

    peer.onaddstream = function (event) {
      vid.src = webkitURL.createObjectURL(event.stream);
      console.log("New Stream");
      console.log(event.stream);
    };

我将编辑一些我认为有助于理解我在说什么的代码的tomorrow.如有疑问,请去解决.

I will edit tommorow with some code i think to help understand what i'm saying. If have question go for it.

这是我的信令服务器:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(3000);

app.get('/', function (req, res) {
  res.send('The cake is a lie');
});

io.on('connection', function (socket) {

  console.log('NEW CONNECTION');

  socket.on('offer', function (data) {
    console.log(data);
    socket.broadcast.emit("offer",data);
  });

  socket.on('answer', function (data) {
    console.log(data);
    socket.broadcast.emit("answer",data);
  });

  socket.on('candidate', function (data) {
    console.log(data);
    socket.broadcast.emit("candidate",data);
  });

});

这篇关于用于调用的WebRTC函数调用流程[Android]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆