dongqiu5184
dongqiu5184
2015-08-28 14:55

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

  • webrtc
  • php
  • javascript

Suppose there has 3 users A , B , C . And i am use Ajax rather than websockets to send and get messages.

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). I have added A's sdp(offer) to B . So is it necessary for A to add B's sdp(answer)?

2, I have found there has so many(about 17) icecandidate generated when A createOffer(). and I don't know how to handle these icecandidate . should B (and C) add these all icecandidate one by one for 17 times ?

3, As I said 3 users. 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 .

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

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条回答

为你推荐