关于icecandidate和webrtc的sdp的一些问题

时间:2015-08-28 14:55:19

标签: javascript php webrtc

假设有3个用户A,B,C。我使用Ajax而不是websockets来发送和获取消息。

问题打击:

1,在这种情况下,我只需要(B和C)可以看到A,并且不需要A可以看到(B和C)。我已经向B添加了A的sdp(offer)。那么A是否有必要添加B&#s; sdp(答案)?

2,我发现在创建一个createOffer()时会产生很多(大约17个)icecandidate。而且我不知道如何处理这些冰锥。应该B(和C)将这些冰糖一个一个一个地添加17次?

3,正如我所说的3位用户。这是一对多的应用程序。怎么实现呢?我的主要问题是如何处理每个人的sdp和icecandidate。是否需要添加其他人' sdp和icecandidate。

这是冰封的打击:

{"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

这是我的应用程序的代码:你会发现我使用了一个临时按钮,它可以调用" 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 }?>

1 个答案:

答案 0 :(得分:0)

我没有参与过php,但就WebRTC而言:

  • 使用Web套接字或Ajax在对等体之间中继数据没有任何区别。
  • 即使B只是消费者而A是提供者,B仍然需要提供sdp-answer,事实上,无论谁打电话和谁回答,提供者需要在制作sdp之前添加他的MediaStream提供/应答。
  • ICE候选人就像物理地址,他们帮助同伴找到彼此,A不是唯一创造它们的同伴,B(和C)也创造它们。您需要将它们传递到两侧的相应PeerConnection对象。没有他们交换,就没有办法打电话。
  • 虽然它是一对多的,但对于每个新的消费者(B,C,......),提供者(A)需要创建一个单独的PeerConnection,你也可以查看Muaz Khan的{ {3}}。如果您想保护提供商的带宽,您需要查看MCU,one-to-many boradcasting是来自Kurento的一对多广播示例。