Skip to content

Instantly share code, notes, and snippets.

@aBuder
Created March 18, 2015 14:10
Show Gist options
  • Save aBuder/bb9f3f5305170ff52948 to your computer and use it in GitHub Desktop.
Save aBuder/bb9f3f5305170ff52948 to your computer and use it in GitHub Desktop.
Play-Wamp-Webrtc
/*
* Copyright (c) 2015 Alexander Buder . All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/
var wamp_session = null;
var peer_connection= null;
var configuration = {
iceServers: [
{url: "stun:23.21.150.121"},
{url: "stun:stun.l.google.com:19302"},
{url: "turn:numb.viagenie.ca", credential: "webrtcdemo", username: "louis%40mozilla.com"}
]
}
document.getElementById("startButton").onclick = init(true);
window.onload = function() {
var wsuri = "ws://localhost:9000/wamp";
ab.connect(wsuri, onConnection, onError);
function onConnection(session){
wamp_session = session;
session.subscribe("http://example.com/webrtc", onMessage);
}
// handler if wamp message received
function onMessage (topic, event) {
console.log("receive message on: " + topic + " " + event);
if (!peer_connection){
init(false);
}
if(event.sessionId != wamp_session._session_id){
debugger;
if (event.sdp)
peer_connection.setRemoteDescription(new RTCSessionDescription(event.sdp));
else if(event.candidate){
peer_connection.addIceCandidate(new RTCIceCandidate(event.candidate));
}
}
}
// handler if wamp error received
function onError(code, reason) {
console.log(reason);
}
function onUserMediaError(error){
console.log(error);
}
};
function init(isCaller){
// Erstellung einer PeerConnection
peer_connection = new webkitRTCPeerConnection(configuration);
// send any ice candidates to the other peer
peer_connection.onicecandidate = function (evt) {
wamp_session.publish("http://example.com/webrtc", {sessionId: wamp_session._session_id , candidate: evt.candidate});
};
// once remote stream arrives, show it in the remote video element
peer_connection.onaddstream = function (evt) {
debugger;
document.getElementById("remoteVideo").src = URL.createObjectURL(evt.stream);
};
// get the local stream, show it in the local video element and send it
navigator.webkitGetUserMedia({ "audio": true, "video": true },
function onUserMediaSuccess(stream){
document.getElementById("localVideo").src = URL.createObjectURL(stream);
peer_connection.addStream(stream);
if (isCaller)
peer_connection.createOffer(gotDescription);
else
peer_connection.createAnswer(peer_connection.remoteDescription, gotDescription);
function gotDescription(desc) {
peer_connection.setLocalDescription(desc);
wamp_session.publish("http://example.com/webrtc", {sessionId: wamp_session._session_id , sdp: desc});
}
}
,
function onUserMediaError(error){
console.log(error);
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment