dongqiu5184 2015-08-28 14:55
浏览 1223
已采纳

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

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条回答 默认 最新

  • douwen0647 2015-08-31 01:51
    关注

    I have not worked on php, but as far as WebRTC is concerned:

    • It does not make any difference whether one uses Web Sockets or Ajax for relaying data between peers.
    • 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.
    • 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. You need to pass them to the appropriate PeerConnection object on both sides. 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. 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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。