Skip to content

Instantly share code, notes, and snippets.

@ggarber
Created August 27, 2013 22:53
Show Gist options
  • Save ggarber/6360123 to your computer and use it in GitHub Desktop.
Save ggarber/6360123 to your computer and use it in GitHub Desktop.

var signalingChannel = new SignalingChannel(); var conn;

// call start() to initiate function start() { var sock = new RTCSocket([{ url: "stun:stun.example.org" }]); conn = new RTCConnection(sock);

// send my ICE details to the other peer
signalingChannel.send(JSON.stringify({ "iceDescription": conn.getLocalIceDescription() }));

// apply any local ICE candidate and send it to the remote
conn.oncandidate = function (evt) {
    conn.setLocalCandidate(evt.candidate);
    signalingChannel.send(JSON.stringify({ "candidate": evt.candidate }));
}

// once remote stream arrives, show it in the remote video element
conn.onaddstream = function (evt) {
    remoteView.src = URL.createObjectURL(evt.stream);
};

// get a local stream, show it in a self-view and add it to be sent
navigator.getUserMedia({ "audio": true, "video": true }, function (stream) {
    selfView.src = URL.createObjectURL(stream);
    conn.addStream(stream);
    // send tracks description to the peer
    conn.tracks().forEach(function(track) {
        signalingChannel.send(JSON.stringify({ "track": track.getDescription() }));
    });
}, logError);

}

signalingChannel.onmessage = function (evt) { if (!conn) start();

var message = JSON.parse(evt.data);
if (message.iceDescription) {
    conn.connect(message.iceDescription);
}
if (message.candidate) {
    conn.setRemoteCandidate(message.candidate);
}
if (message.track) {
    conn.receiveTrack(message.track);
}

};

function logError(error) { log(error.name + ": " + error.message); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment