Skip to content

WebRTC Experiment — Real-time working WebRTC STUN/TURN specific demos. 1) JavaScript Only (PubNub specific) WebRTC Experiment --- 2) ASP.NET MVC specific WebRTC Experiment --- etc.

Notifications You must be signed in to change notification settings

kjiun/WebRTC-Experiment

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

WebRTC Experiment! WebRTC Experiment!

--

Real-time WebRTC Experiment that exposes the power of yours and mine favorite technology: WebRTC!

Preview / Demo

Screenshot

WebRTC Screenshot 2

##Credits

##Browsers

It works fine on Google Chrome Stable 23 (and upper stable releases!)

JavaScript code!

var CreateRTCPeerConnection = function (options) {

    window.RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection || window.RTCPeerConnection;
    window.RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.RTCSessionDescription;
    window.RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.RTCIceCandidate;

    window.URL = window.webkitURL || window.URL;
    navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.getUserMedia;

	// TURN:     { "iceServers": [{ "url": "turn:webrtc%40live.com@numb.viagenie.ca", "credential": "muazkh" }] }
    var config = { "iceServers": [{ "url": "stun:stun.l.google.com:19302"}] };

    this.peerConnection = new PeerConnection(options.config || config);

    this.peerConnection.onicecandidate = onicecandidate;
    this.peerConnection.onaddstream = onaddstream;
    this.peerConnection.addStream(options.stream);

    function onicecandidate(event) {

        if (!event.candidate || !CreateRTCPeerConnection.peerConnection) return;

        if (options.onicecandidate) options.onicecandidate(event.candidate);
    }

    function onaddstream(event, recheck) {
        if (event && options.remoteVideo) options.remoteVideo.src = URL.createObjectURL(event.stream);

        if (!event && recheck && options.onaddstream) {
            if (!(options.remoteVideo.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA || options.remoteVideo.paused || options.remoteVideo.currentTime <= 0)) {
                options.onaddstream();
            } else setTimeout(onaddstream, 500);
        }
    }

    function createOffer() {
        if (!options.createOffer) return;

        CreateRTCPeerConnection.peerConnection.createOffer(function (sessionDescription) {

            CreateRTCPeerConnection.peerConnection.setLocalDescription(sessionDescription);
            options.createOffer(sessionDescription);

        }, null, { audio: true, video: true });
    }

    createOffer();

    function createAnswer() {
        if (!options.createAnswer) return;

        CreateRTCPeerConnection.peerConnection.setRemoteDescription(new SessionDescription(options.offer));
        CreateRTCPeerConnection.peerConnection.createAnswer(function (sessionDescription) {

            CreateRTCPeerConnection.peerConnection.setLocalDescription(sessionDescription);
            options.createAnswer(sessionDescription);

        }, null, { audio: true, video: true });
    }

    createAnswer();

    return this;
};

CreateRTCPeerConnection.prototype.peerConnection = null;

CreateRTCPeerConnection.prototype.onanswer = function (sdp) {
    this.peerConnection.setRemoteDescription(new SessionDescription(sdp));
};

CreateRTCPeerConnection.prototype.addICE = function (candidate) {

    this.peerConnection.addIceCandidate(new IceCandidate({
        sdpMLineIndex: candidate.sdpMLineIndex,
        candidate: candidate.candidate
    }));

};


// Here is how to use above function!

var connection = CreateRTCPeerConnection({
    createOffer: function (sdp) {
        console.log('created offer');
    },
    onicecandidate: function (candidate) {
        console.log('ICE candidate is ready for other peer!');
    },
    onaddstream: function () {
        console.log('Got remote stream successfully!');
    }
});

// Pass ICE sent by other peer to process it!

connection.addICE({
    sdpMLineIndex: 1,
    candidate: candidate
});

// Pass SDP sent by other peer to finalize the handshake!

connection.onanswer(sdp);

##Spec references

Don't forget PubNub for Signalling!!

License

Copyright (c) 2012 Muaz Khan - Licensed under the MIT license.

About

WebRTC Experiment — Real-time working WebRTC STUN/TURN specific demos. 1) JavaScript Only (PubNub specific) WebRTC Experiment --- 2) ASP.NET MVC specific WebRTC Experiment --- etc.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published