pc.createanswer无法执行

时间:2014-10-16 12:10:07

标签: websocket webrtc sockjs ice rtcp

            // Set getUserMedia constraints
            var constraints = {video: true, audio: true};

            // From this point on, execution proceeds based on asynchronous events...
            // getUserMedia() handlers...
            var handleUserMedia= function (stream) {
                localStream = stream;
                attachMediaStream(localVideo, stream);
                console.log('Adding local stream.');
                sendMessage('got user media');
            }


            var handleUserMediaError= function (error){
                console.log('navigator.getUserMedia error: ', error);
            }


            // Server-mediated message exchanging...
            // 1. Server-->Client...
            // Handle 'created' message coming back from server:
            // this peer is the initiator
            var created =function  (){

                console.log("--->>> Message From Server --->>> Created");
                console.log('Created room ' );
                isInitiator = true;

                // Call getUserMedia() -- handleUserMedia()
                navigator.getUserMedia(constraints, handleUserMedia, handleUserMediaError);
                console.log('Getting user media with constraints', constraints);
                checkAndStart();
            }

            // Handle 'full' message coming back from server:
            // this peer arrived too late :-(
            var full= function  (){
                console.log("--->>> Message From Server --->>> Full");
                console.log('Room  is full');
            }

            // Handle 'join' message coming back from server:
            // another peer is joining the channel
            var join= function  (){
                console.log("--->>> Message From Server --->>> Join");
                console.log('Another peer made a request to join room ' );
                console.log('This peer is the initiator of room !');
                isChannelReady = true;
            }

            // Handle 'joined' message coming back from server:
            // this is the second peer joining the channel
            var joined= function  (){
                console.log("--->>> Message From Server --->>> Joined");
                console.log('This peer has joined room ');
                isChannelReady = true;
                // Call getUserMedia() --handleUserMedia()
                navigator.getUserMedia(constraints, handleUserMedia, handleUserMediaError);
                console.log('Getting user media with constraints', constraints);
            }

            // Server-sent log message...
            var log= function  (){
                console.log("--->>> Message From Server --->>> LOG");
                console.log.apply(console, array);
            }

            // Receive message from the other peer via the signaling server
            socket.onmessage = function(event){ 

                var payload = event.data.split( "\n" );
                var destinationurl = payload[1];
                var message = payload[7];
                /*message = message.replace(/(?:\\[rn])+/g, "");*/
                /*if(event.data.search("destination:/testurl")==-1)
                    {*/

                console.log('Received message:', message) ;

                if(message.search("created")!= -1) {
                    created();
                }
                else if(message.search("joined")!=-1) {
                    joined();
                }
                else if(message.search("join")!=-1) {

                    join();
                }

                else if (message.search("full")!=-1) {
                    full();
                }
                else if (message.search("got user media")!=-1) {
                    console.log("--->>> Message From Server --->>> Got User Media and Now Going to Call checkAndStart()");
                    checkAndStart();
                }
                else if (message.search("bye")!=-1 && isStarted) {
                    console.log("--->>> Message From Server --->>> BYE");
                    handleRemoteHangup();
                }
                else {
                    //message = message.replace(/(?:\\[rn])+/g, "");
                    //message= message.replace(/"/g, "'");
                    /*var obj = eval("(" + message + ')');*/
                    var message = message.substring(0, message.length-1)
                    message = JSON.parse(message);


                    if (message.type == 'offer') {
                        console.log("--->>> Message From Server --->>> OFFER, and will call CheckAndStart()");
                        if (!isInitiator && !isStarted) {
                            checkAndStart();
                        }
                        pc.setRemoteDescription(new RTCSessionDescription(message));
                        doAnswer();
                    }
                    else if (message.type == 'answer' && isStarted) {
                        console.log("--->>> Message From Server --->>> ANSWER, SETRemoteDescription");
                        pc.setRemoteDescription(new RTCSessionDescription(message));
                    }
                    else if (message.type == 'candidate' && isStarted) {
                        console.log("--->>> Message From Server --->>> CANDIDATE");
                        var candidate = new RTCIceCandidate({sdpMLineIndex:message.label,
                            candidate:message.candidate});
                        pc.addIceCandidate(candidate);
                    }
                }

            }
        /*}*/

            // 2. Client-->Server
            // Send message to the other peer via the signaling server
            var sendMessage=function (message){
                console.log('Sending message: ', message);
                /*socket.send(message);
                */


                //subscribe
                //send 
                //message create or join


                /*message = message.replace(/(?:\\[rn])+/g, "");*/

                stomp.send("/createjoin",{},message);
            }

            // Channel negotiation trigger function
            var checkAndStart=function () {
                if (!isStarted && typeof localStream != 'undefined' && isChannelReady) {
                    createPeerConnection();
                    console.log("cehckAndStart Called, PeerConnection Created");
                    isStarted = true;
                    if (isInitiator) {
                        doCall();
                        console.log("cehckAndStart Called, Call initiated");
                    }
                }
                else {
                    console.log("cehckAndStart Called, But call not initiated");
                }
            }

            // PeerConnection management...
            var createPeerConnection= function () {
                try {
                    pc = new webkitRTCPeerConnection(pc_config, pc_constraints);
                    pc.addStream(localStream);
                    pc.onicecandidate = handleIceCandidate;
                    console.log('Created RTCPeerConnnection with:\n' + ' config: \'' + JSON.stringify(pc_config) + '\';\n' +' constraints: \'' + JSON.stringify(pc_constraints) + '\'.');
                } 
                catch (e) {
                    console.log('Failed to create PeerConnection, exception: ' + e.message);
                    alert('Cannot create RTCPeerConnection object.');
                    return;
                }

                pc.onaddstream = handleRemoteStreamAdded;
                pc.onremovestream = handleRemoteStreamRemoved;

                console.log("isInitiator: " + isInitiator);

                if (isInitiator) {
                    try {
                        // Create a reliable data channel
                        sendChannel = pc.createDataChannel("sendDataChannel",
                                {reliable: true});
                        console.log('Created send data channel');
                    } catch (e) {
                        alert('Failed to create data channel. ');
                        console.log('createDataChannel() failed with exception: ' + e.message);
                    }
                    sendChannel.onopen = handleSendChannelStateChange;
                    sendChannel.onmessage = handleMessage;
                    sendChannel.onclose = handleSendChannelStateChange;
                } 

                else { // Joiner
                    console.log("gotReceiveChannel\n\n\n\n\n");
                    pc.ondatachannel = gotReceiveChannel;
                }
            }

            // Data channel management
            var sendData= function  () {
                var data = sendTextarea.value;
                if(isInitiator) 

                    sendChannel.send(data);
                else receiveChannel.send(data);
                console.log('Sent data: ' + data);
            }

            // Handlers...
            var gotReceiveChannel = function (event) {
                console.log('Receive Channel Callback');
                receiveChannel = event.channel;
                receiveChannel.onmessage = handleMessage;
                receiveChannel.onopen = handleReceiveChannelStateChange;
                receiveChannel.onclose = handleReceiveChannelStateChange;
            }

            var handleMessage=function (event) {
                console.log('Received message: ' + event.data);
                receiveTextarea.value += event.data + '\n';
            }

            var handleSendChannelStateChange=function () {
                var readyState = sendChannel.readyState;
                console.log('Send channel state is: ' + readyState);

                // If channel ready, enable user's input
                if (readyState == "open") {
                    dataChannelSend.disabled = false;
                    dataChannelSend.focus();
                    dataChannelSend.placeholder = "";
                    sendButton.disabled = false;
                } 
                else {
                    dataChannelSend.disabled = true;
                    sendButton.disabled = true;
                }
            }

            var handleReceiveChannelStateChange=function () {
                var readyState = receiveChannel.readyState;
                console.log('Receive channel state is: ' + readyState);

                // If channel ready, enable user's input
                if (readyState == "open") {
                    dataChannelSend.disabled = false;
                    dataChannelSend.focus();
                    dataChannelSend.placeholder = "";
                    sendButton.disabled = false;
                } else {
                    dataChannelSend.disabled = true;
                    sendButton.disabled = true;
                }
            }

            // ICE candidates management
            var handleIceCandidate= function (event) {
                console.log('handleIceCandidate event: ', event);
                if (event.candidate) {
                    sendMessage(JSON.stringify({
                        type: 'candidate',
                        label: event.candidate.sdpMLineIndex,
                        id: event.candidate.sdpMid,
                        candidate: event.candidate.candidate}));
                } else {
                    console.log('End of candidates.');
                }
            }

            // Create Offer
            var doCall=function() {
                console.log('Creating Offer...');
                try {
                    pc.createOffer(setLocalAndSendMessage, onSignalingError,
                            sdpConstraints);
                } catch (e) {
                    // TODO: handle exception
                    console.log("error"+e);
                }
            }

            // Signaling error handler
            var onSignalingError= function (error) {
                console.log('Failed to create signaling message : ' + error.name);
            }

            // Create Answer
            var doAnswer= function() {
                console.log('Sending answer to peer.');

                    pc.createAnswer(setLocalAndSendMessage, onSignalingError,
                            sdpConstraints);

            }

            // Success handler for both createOffer()
            // and createAnswer()
            var setLocalAndSendMessage= function (sessionDescription) {
                console.log("sessionDescription**"+sessionDescription);
                pc.setLocalDescription(sessionDescription);
                sendMessage(JSON.stringify(sessionDescription));
            }

            // Remote stream handlers...
            var handleRemoteStreamAdded= function(event) {
                console.log('Remote stream added.');
                attachMediaStream(remoteVideo, event.stream);
                console.log('Remote stream attached!!.');
                remoteStream = event.stream;
            }

            var handleRemoteStreamRemoved= function (event) {
                console.log('Remote stream removed. Event: ', event);
            }

            // Clean-up functions...
            var hangup =function () {
                console.log('Hanging up.');
                stop();
                sendMessage('bye');
            }

            var handleRemoteHangup = function () {
                console.log('Session terminated.');
                stop();
                isInitiator = false;
            }
            var stop= function() {
                isStarted = false;
                if (sendChannel) sendChannel.close();
                if (receiveChannel) receiveChannel.close();
                if (pc) pc.close();
                pc = null;
                sendButton.disabled=true;
            }

            var attachMediaStream= function( video, lclStream)
            {
                if (window.URL) {
                    video.src = URL.createObjectURL(lclStream);
                } else {
                    video.src = lclStream;
                }
            }

   here is my message of type "offer"

{"sdp":"v=0\r\no=- 4542862114432068298 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE audio video data\r\na=msid-semantic: WMS vuXjLI4c8JwkOw4znUzYhl1Udp1q9zv63ZTn\r\nm=audio 1 RTP/SAVPF 111 103 104 0 8 106 105 13 126\r\nc=IN IP4 0.0.0.0\r\na=rtcp:1 IN IP4 0.0.0.0\r\na=ice-ufrag:Iom5FHfWBDWNB4O9\r\na=ice-pwd:ejEjHzkf0VDNR37sAomQVGjL\r\na=ice-options:google-ice\r\na=fingerprint:sha-256 0B:C1:56:A7:55:29:C7:5B:6D:BD:55:99:8E:40:B2:61:1C:78:F8:A4:8F:9B:95:D5:32:9C:BC:D0:A2:4B:EC:04\r\na=setup:actpass\r\na=mid:audio\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=sendrecv\r\na=rtcp-mux\r\na=rtpmap:111 opus/48000/2\r\na=fmtp:111 minptime=10\r\na=rtpmap:103 ISAC/16000\r\na=rtpmap:104 ISAC/32000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:106 CN/32000\r\na=rtpmap:105 CN/16000\r\na=rtpmap:13 CN/8000\r\na=rtpmap:126 telephone-event/8000\r\na=maxptime:60\r\na=ssrc:1321218595 cname:LMU99LT8oOlpgre0\r\na=ssrc:1321218595 msid:vuXjLI4c8JwkOw4znUzYhl1Udp1q9zv63ZTn ae0d1ec6-d144-43a7-998a-fe5de8137b21\r\na=ssrc:1321218595 mslabel:vuXjLI4c8JwkOw4znUzYhl1Udp1q9zv63ZTn\r\na=ssrc:1321218595 label:ae0d1ec6-d144-43a7-998a-fe5de8137b21\r\nm=video 1 RTP/SAVPF 100 116 117 96\r\nc=IN IP4 0.0.0.0\r\na=rtcp:1 IN IP4 0.0.0.0\r\na=ice-ufrag:Iom5FHfWBDWNB4O9\r\na=ice-pwd:ejEjHzkf0VDNR37sAomQVGjL\r\na=ice-options:google-ice\r\na=fingerprint:sha-256 0B:C1:56:A7:55:29:C7:5B:6D:BD:55:99:8E:40:B2:61:1C:78:F8:A4:8F:9B:95:D5:32:9C:BC:D0:A2:4B:EC:04\r\na=setup:actpass\r\na=mid:video\r\na=extmap:2 urn:ietf:params:rtp-hdrext:toffset\r\na=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=sendrecv\r\na=rtcp-mux\r\na=rtpmap:100 VP8/90000\r\na=rtcp-fb:100 ccm fir\r\na=rtcp-fb:100 nack\r\na=rtcp-fb:100 nack pli\r\na=rtcp-fb:100 goog-remb\r\na=rtpmap:116 red/90000\r\na=rtpmap:117 ulpfec/90000\r\na=rtpmap:96 rtx/90000\r\na=fmtp:96 apt=100\r\na=ssrc-group:FID 2564084270 1462373314\r\na=ssrc:2564084270 cname:LMU99LT8oOlpgre0\r\na=ssrc:2564084270 msid:vuXjLI4c8JwkOw4znUzYhl1Udp1q9zv63ZTn 843026ff-14a0-4f3d-9306-13634fc9de15\r\na=ssrc:2564084270 mslabel:vuXjLI4c8JwkOw4znUzYhl1Udp1q9zv63ZTn\r\na=ssrc:2564084270 label:843026ff-14a0-4f3d-9306-13634fc9de15\r\na=ssrc:1462373314 cname:LMU99LT8oOlpgre0\r\na=ssrc:1462373314 msid:vuXjLI4c8JwkOw4znUzYhl1Udp1q9zv63ZTn 843026ff-14a0-4f3d-9306-13634fc9de15\r\na=ssrc:1462373314 mslabel:vuXjLI4c8JwkOw4znUzYhl1Udp1q9zv63ZTn\r\na=ssrc:1462373314 label:843026ff-14a0-4f3d-9306-13634fc9de15\r\nm=application 1 DTLS/SCTP 5000\r\nc=IN IP4 0.0.0.0\r\na=ice-ufrag:Iom5FHfWBDWNB4O9\r\na=ice-pwd:ejEjHzkf0VDNR37sAomQVGjL\r\na=ice-options:google-ice\r\na=fingerprint:sha-256 0B:C1:56:A7:55:29:C7:5B:6D:BD:55:99:8E:40:B2:61:1C:78:F8:A4:8F:9B:95:D5:32:9C:BC:D0:A2:4B:EC:04\r\na=setup:actpass\r\na=mid:data\r\na=sctpmap:5000 webrtc-datachannel 1024\r\n","type":"offer"}


My pc is like: iceConnectionState: "new"
iceGatheringState: "new"
localDescription: null
onaddstream: function (event) {
ondatachannel: function (event) {
onicecandidate: function (event) {
oniceconnectionstatechange: null
onnegotiationneeded: null
onremovestream: function (event) {
onsignalingstatechange: null
remoteDescription: RTCSessionDescription
signalingState: "have-remote-offer"
__proto__: RTCPeerConnection

与这些事件关联的所有方法都正确附加。已创建商品并交换消息,但在连接远程流后,所有内容都会停止。没有交换回答类型的消息。此外,createanswer不会执行任何回调。既不成功也不错误。所有错误都正确形成。

Please suggest why its not executing.

0 个答案:

没有答案