简体   繁体   English

有关webrtc的icecandidate和sdp的几个问题

[英]a few questions about icecandidate and sdp of webrtc

Suppose there has 3 users A , B , C . 假设有3个用户A,B,C。 And i am use Ajax rather than websockets to send and get messages. 我使用Ajax而不是websockets发送和获取消息。

Question blow: 问题打击:

1, In this case I just need that (B and C) can see A ,and don't need that A can see (B and C). 1,在这种情况下,我只需要(B和C)可以看到A,而不需要A可以看到(B和C)。 I have added A's sdp(offer) to B . 我已经将A的sdp(offer)添加到B中。 So is it necessary for A to add B's sdp(answer)? 那么,A有必要添加B的sdp(answer)吗?

2, I have found there has so many(about 17) icecandidate generated when A createOffer(). 2,我发现A createOffer()生成了很多(大约17个)icecandidate。 and I don't know how to handle these icecandidate . 而且我不知道如何处理这些候选人。 should B (and C) add these all icecandidate one by one for 17 times ? B(和C)是否应一一添加17种所有的冰候选物?

3, As I said 3 users. 3,正如我所说的3位用户。 This is a one-to-many application. 这是一对多的应用程序。 How to implement it? 如何执行呢? my main question is how to deal the sdp and icecandidate of each one.Does A need to add others' sdp and icecandidate . 我的主要问题是如何处理每个人的sdp和icecandidate。是否需要添加其他人的sdp和icecandidate。

here is the icecandidates blow: 这是候选人的打击:

{"candidate":"candidate:1754064501 1 udp 2122255103 2001::5ef5:79fd:8c8:c74e:f166:22f 52522 typ host generation 0","sdpMid":"audio","sdpMLineIndex":0}

{"candidate":"candidate:1026183099 1 udp 2122194687 192.168.2.100 52523 typ host generation 0","sdpMid":"audio","sdpMLineIndex":0}

{"candidate":"candidate:1754064501 2 udp 2122255102 2001::5ef5:79fd:8c8:c74e:f166:22f 52524 typ host generation 0","sdpMid":"audio","sdpMLineIndex":0}

{"candidate":"candidate:1026183099 2 udp 2122194686 192.168.2.100 52525 typ host generation 0","sdpMid":"audio","sdpMLineIndex":0}

{"candidate":"candidate:1754064501 1 udp 2122255103 2001::5ef5:79fd:8c8:c74e:f166:22f 52526 typ host generation 0","sdpMid":"video","sdpMLineIndex":1}

{"candidate":"candidate:1026183099 1 udp 2122194687 192.168.2.100 52527 typ host generation 0","sdpMid":"video","sdpMLineIndex":1}

{"candidate":"candidate:1754064501 2 udp 2122255102 2001::5ef5:79fd:8c8:c74e:f166:22f 52528 typ host generation 0","sdpMid":"video","sdpMLineIndex":1}

{"candidate":"candidate:1026183099 2 udp 2122194686 192.168.2.100 52529 typ host generation 0","sdpMid":"video","sdpMLineIndex":1}

{"candidate":"candidate:638524037 1 tcp 1518275327 2001::5ef5:79fd:8c8:c74e:f166:22f 0 typ host tcptype active generation 0","sdpMid":"audio","sdpMLineIndex":0}

{"candidate":"candidate:1940501323 1 tcp 1518214911 192.168.2.100 0 typ host tcptype active generation 0","sdpMid":"audio","sdpMLineIndex":0}

{"candidate":"candidate:638524037 2 tcp 1518275326 2001::5ef5:79fd:8c8:c74e:f166:22f 0 typ host tcptype active generation 0","sdpMid":"audio","sdpMLineIndex":0}

{"candidate":"candidate:1940501323 2 tcp 1518214910 192.168.2.100 0 typ host tcptype active generation 0","sdpMid":"audio","sdpMLineIndex":0}

{"candidate":"candidate:638524037 1 tcp 1518275327 2001::5ef5:79fd:8c8:c74e:f166:22f 0 typ host tcptype active generation 0","sdpMid":"video","sdpMLineIndex":1}

{"candidate":"candidate:1940501323 1 tcp 1518214911 192.168.2.100 0 typ host tcptype active generation 0","sdpMid":"video","sdpMLineIndex":1}

{"candidate":"candidate:638524037 2 tcp 1518275326 2001::5ef5:79fd:8c8:c74e:f166:22f 0 typ host tcptype active generation 0","sdpMid":"video","sdpMLineIndex":1}

{"candidate":"candidate:1940501323 2 tcp 1518214910 192.168.2.100 0 typ host tcptype active generation 0","sdpMid":"video","sdpMLineIndex":1}

null

and here is my application's code: you will find that i use a temp button which call "Get Answer" to let A to get sdp and icecandidate (has Commented out) of B or C 这是我的应用程序的代码:您会发现我使用一个临时按钮,调用“ Get Answer”,让A获取B或C的sdp和icecandidate(已注释掉)

var getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);


    //use Google's stun server
    var iceServer = {
        "iceServers": [{
            "url": "stun:stun.l.google.com:19302"
        }]
    };


    var PeerConnection = (window.PeerConnection ||
                        window.webkitPeerConnection00 || 
                        window.webkitRTCPeerConnection || 
                        window.mozRTCPeerConnection);



    var pc = new PeerConnection(iceServer);

    var iceTimes = 1;



    function getAnswer(cid)
    {
        teacherLoop(cid);
        /*for(i=1;i<4;i++)
        {
           $.ajax({
                    url:'/get_webrtc_info.php',
                    type:'POST',
                    async:false,
                    data:{role:'get_answer_ice',times:i,"cid": cid},
                    success:function(msg)
                    {
                        var msg = JSON.parse(msg);
                        pc.addIceCandidate(new RTCIceCandidate(msg));                       

                    }
                }); 
        }*/

    }




    //start live
    function startLive(cid)
    {
        pc.onicecandidate = function(event){
            $.ajax({
                url:"/webrtc.php",
                type:'POST',
                async:false,
                data:{
                        "event": "__ice_candidate",
                        "times": iceTimes,
                        "cid": cid,
                        "data": {
                            "candidate": JSON.stringify(event.candidate)
                        }
                     },

                });
            iceTimes++;

        };

        $(".course-item-play-block").slideDown();

        getUserMedia.call(navigator, {
        "audio": true,
        "video": true
        }, function(stream){

            var myselfVideoElement = document.getElementById('video');
            myselfVideoElement.src = URL.createObjectURL(stream);

            pc.addStream(stream);


            pc.createOffer(function(desc){
                pc.setLocalDescription(desc);
                $.ajax({
                    url:'/webrtc.php',
                    type:'POST',
                    async:false,
                    data:{ 
                            "event": "__offer",
                            "cid": cid,
                            "data": {
                                "sdp": JSON.stringify(desc)
                            }
                         },
                    success:function()
                        {
                          //var timer = setInterval(teacherLoop,4000);
                        }
                    });

            });
        }, function(error){
            //
        });



    }


    function joinLive(cid)
    { 
        $(".course-item-play-block").slideDown();
        pc.onaddstream = function(event)
        {
            var remote_video = document.getElementById('video');
            remote_video.src = URL.createObjectURL(event.stream);
        }
        pc.onicecandidate = function(event){
            $.ajax({
                url:"/webrtc.php",
                type:'POST',
                async:false,
                data:{
                        "event": "__ice_candidate_answer",
                        "times": answerIceTimes,
                        "cid": cid,
                        "data": {
                            "candidate": JSON.stringify(event.candidate)
                        }
                     },

                });
            answerIceTimes++;

        };
        studentLoop(cid)
        getIce(cid)

    }




    function getIce(cid)
    {
        for(i=1;i<17;i++)
        {
           $.ajax({
                    url:'/get_webrtc_info.php',
                    type:'POST',
                    async:false,
                    data:{role:'get_ice',times:i,"cid": cid},
                    success:function(msg)
                    {
                        var msg = JSON.parse(msg);
                        pc.addIceCandidate(new RTCIceCandidate(msg));                       

                    }
                }); 
        }

    }


    function teacherLoop(cid)
    {
        $.ajax({
                    url:'/get_webrtc_info.php',
                    type:'POST',
                    data:{role:'teacher',"cid": cid},
                    success:function(msg)
                    {
                        var msg = JSON.parse(msg);
                        pc.setRemoteDescription(new RTCSessionDescription(msg));
                    }
                });

    }

    function studentLoop(cid)
    {
        $.ajax({
                    url:'/get_webrtc_info.php',
                    type:'POST',
                    async:false,
                    data:{role:'student',"cid": cid},
                    success:function(msg)
                    {

                        var msg = JSON.parse(msg);

                        pc.setRemoteDescription(new RTCSessionDescription(msg));

                            pc.createAnswer(function(answer) {
                              pc.setLocalDescription(new RTCSessionDescription(answer), function() {                            

                                $.ajax({
                                    url:'/webrtc.php',
                                    type:'POST',
                                    async:false,
                                    data:{ 
                                            "event": "__answer",
                                            "cid": cid,
                                            "data": {
                                                "sdp": JSON.stringify(answer)
                                            }
                                         },
                                    success:function()
                                            {
                                              //var timer = setInterval(student,4000);
                                              //studentLoop()
                                            }
                                    });

                              });
                            });



                });

    }
<div class="block course-item-play-block">
        <h3>Living:</h3>
        <video id="video" autoplay></video>
    </div>

<?php if ($isTeacher) {?>
        <div class="btn edit-course-btn common-btn common-btn-6x18 common-btn-blue" onclick="startLive(<?=$Data['cid']?>)">Start Live</div>

        <div class="btn edit-course-btn common-btn common-btn-6x18 common-btn-blue" onclick="getAnswer(<?=$Data['cid']?>)">Get Answer</div>

        <?php }else{?>
            <div class="btn edit-course-btn common-btn common-btn-6x18 common-btn-blue" onclick="joinLive(<?=$Data['cid']?>)">Join Live</div>
        <?php }?>

I have not worked on php, but as far as WebRTC is concerned: 我没有在php上工作,但是就WebRTC而言:

  • It does not make any difference whether one uses Web Sockets or Ajax for relaying data between peers. 使用Web套接字还是Ajax在同级之间中继数据没有什么区别。
  • Even if B is just a consumer and A is provider, B still needs to provide sdp-answer , in fact, it doesn't matter who calls and who answers, the provider needs to add his MediaStream before making sdp offer/answer. 即使B只是消费者,而A是提供者,B仍然需要提供sdp-answer ,实际上,无论呼叫者和应答者都是什么,提供者都需要在提供sdp提供/答案之前添加其MediaStream。
  • ICE candidates are like physical addresses, they help the peers find each others, A is not the only peer who creates them, B (and C) too creates them. ICE候选人就像物理地址一样,它们可以帮助对等方找到彼此,A不是唯一创建它们的对等方,B(和C)也可以创建它们。 You need to pass them to the appropriate PeerConnection object on both sides. 您需要将它们传递到两侧的相应PeerConnection对象。 without them exchanged, there is no way to start a call. 没有它们的交换,就无法发起呼叫。
  • Though it is one-to-many, for each new consumer( B, C, ...), the provider(A) needs to create a seperate PeerConnection , You can also check out Muaz Khan's one-to-many boradcasting . 尽管是一对多的,但对于每个新消费者(B,C,...),提供者(A)需要创建一个单独的PeerConnection ,您也可以查看Muaz Khan的一对多boradcasting If you want to protect the provider's bandwidth, you need to look into MCU, here is an one-to-many broadcast example from Kurento. 如果要保护提供商的带宽,则需要研究MCU, 是Kurento的一对多广播示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM