Skip to content
This repository has been archived by the owner on Dec 4, 2017. It is now read-only.

Commit

Permalink
Adapt to the revised data channel API that landed in FF22.
Browse files Browse the repository at this point in the history
  • Loading branch information
Standard8 committed Apr 9, 2013
1 parent d18cae5 commit 5bfff9d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 61 deletions.
36 changes: 18 additions & 18 deletions static/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,15 @@ var webrtcMedia = {
.join("m=");
}
pc.setLocalDescription(answer, function() {
var randomPort = function() {
return Math.round(Math.random() * 60535) + 5000;
};
var localPort = randomPort();
var remotePort = randomPort();
while (remotePort == localPort) // Avoid being extremely unlucky...
remotePort = randomPort();
$.ajax({
type: 'POST',
url: '/answer',
contentType: 'application/json',
data: JSON.stringify({
to: aData.from,
request: JSON.stringify(answer),
callerPort: remotePort,
calleePort: localPort
request: JSON.stringify(answer)
})
});
pc.connectDataConnection(localPort, remotePort);
}, function(err) {alert("failed to setLocalDescription, " + err);});
}, function(err) {alert("failed to createAnswer, " + err);});
}, true);
Expand All @@ -73,7 +63,8 @@ var webrtcMedia = {
return pc;
},

endCall: function webrtcMedia_stopCall(aPc, aDc, aWin, aAudioOnly) {
endCall: function webrtcMedia_stopCall(aPc, aIncomingChannel, aOutgoingChannel,
aWin, aAudioOnly) {
var mediaElements = ["remoteAudio"];
if (!aAudioOnly)
mediaElements = mediaElements.concat("remoteVideo", "localVideo");
Expand All @@ -93,10 +84,12 @@ var webrtcMedia = {
element.mozSrcObject = null;
});

if (aIncomingChannel)
aIncomingChannel.close();
if (aOutgoingChannel)
aOutgoingChannel.close();
if (aPc)
aPc.close();
if (aDc)
aDc.close();
},

_createBasicPc: function webrtcMedia_createBasicPc(aWin, aPerson, aOriginator, aAudioOnly,
Expand All @@ -106,6 +99,7 @@ var webrtcMedia = {
if (!this.hasInternetAccess)
params = {iceServers: []};
var pc = new aWin.mozRTCPeerConnection(params);

pc.onaddstream = function(obj) {
var type = obj.type;
if (type == "video") {
Expand All @@ -120,17 +114,23 @@ var webrtcMedia = {
alert("sender onaddstream of unknown type, obj = " + obj.toSource());
}
};
pc.ondatachannel = function(aChannel) {
if (aDataConnectionCallback)
aDataConnectionCallback(aWin, aChannel, aPerson);
};

pc.onconnection = function() {
if (aConnectionCallback) {
aConnectionCallback(aWin, pc, aPerson, aOriginator);
}
};

pc.onclosedconnection = function(obj) {
};

var chan = pc.createDataChannel("SocialAPI");

pc.ondatachannel = function(aEvent) {
if (aDataConnectionCallback)
aDataConnectionCallback(aWin, chan, aEvent.channel, aPerson);
};

return pc;
},

Expand Down
23 changes: 12 additions & 11 deletions static/mobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var gUsername;
var gContacts = {};
var gChat;
var gFake = false;
var gIncomingChannel;
var gOutgoingChannel;

function startGuest() {
$("#signin").hide();
Expand Down Expand Up @@ -210,9 +212,6 @@ function setupEventSource() {
pc.setRemoteDescription(JSON.parse(data.request), function() {
// Nothing to do for the audio/video. The interesting things for
// them will happen in onaddstream.
// We need to establish the data connection though.
if (data.callerPort && data.calleePort)
pc.connectDataConnection(data.callerPort, data.calleePort);
}, function(err) {alert("failed to setRemoteDescription with answer, " + err);});
}, false);

Expand All @@ -233,13 +232,14 @@ function setupEventSource() {
}

function onConnection(aWin, aPc, aPerson, aOriginator) {
if (aOriginator)
setupChat(aWin, aPc.createDataChannel("SocialAPI", {}));
}

function setupChat(aWin, aChannel) {
aChannel.binaryType = "blob";
aChannel.onmessage = function(evt) {
function setupChat(aWin, aIncomingChannel, aOutgoingChannel) {
gIncomingChannel = aIncomingChannel;
gOutgoingChannel = aOutgoingChannel;

aOutgoingChannel.binaryType = "blob";
aIncomingChannel.onmessage = function(evt) {
try {
var details = JSON.parse(evt.data);
if (details.type == "url")
Expand All @@ -249,12 +249,12 @@ function setupChat(aWin, aChannel) {
} catch(e) {
insertChatMessage("Them", evt.data);
}
}
};

document.getElementById("chatForm").onsubmit = function() {
var localChat = document.getElementById("localChat");
var message = localChat.value;
aChannel.send(message);
aOutgoingChannel.send(message);
localChat.value = "";
insertChatMessage("Me", message);
return false;
Expand Down Expand Up @@ -288,7 +288,8 @@ function endCall() {
document.getElementById("accept").onclick = null;
document.getElementById("reject").onclick = null;

webrtcMedia.endCall(gChat.pc, null, window, gChat.audioOnly);
webrtcMedia.endCall(gChat.pc, gIncomingChannel, gOutgoingChannel, window,
gChat.audioOnly);
if (!gChat.audioOnly)
window.removeEventListener("resize", resizeVideo);
gChat = null;
Expand Down
60 changes: 28 additions & 32 deletions static/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,6 @@ function signedOut() {
}

function onConnection(aWin, aPc, aPerson, aOriginator) {
// Do we need to set up a data connection itself?
if (aOriginator)
setupFileSharing(aWin, aPc.createDataChannel("SocialAPI", {}), aPerson);

// Now set up the chat interface
aWin.document.getElementById("chatForm").onsubmit = function() {
var localChat = aWin.document.getElementById("localChat");
var message = localChat.value;
gChats[aPerson].dc.send(message);
localChat.value = "";
// XXX: Sometimes insertChatMessage throws an exception, don't know why yet.
try {
insertChatMessage(aWin, "Me", message);
} catch(e) {}
return false;
};
aWin.document.getElementById("localChat").removeAttribute("disabled");
}

function insertChatMessage(win, from, message) {
Expand Down Expand Up @@ -179,29 +162,44 @@ function gotChat(win, evt) {
}
}

function setupFileSharing(win, dc, target) {
function setupFileSharing(aWin, aIncomingChannel, aOutgoingChannel, aTarget) {
/* Setup data channel */
dc.binaryType = "blob";
dc.onmessage = function(evt) {
gotChat(win, evt);
aOutgoingChannel.binaryType = "blob";
aIncomingChannel.onmessage = function(evt) {
gotChat(aWin, evt);
};
gChats[target].dc = dc;
gChats[aTarget].incomingChannel = aIncomingChannel;
gChats[aTarget].outgoingChannel = aOutgoingChannel;

/* Setup drag and drop for file transfer */
var box = win.document.getElementById("content");
var box = aWin.document.getElementById("content");
box.addEventListener("dragover", ignoreDrag, false);
box.addEventListener("dragleave", ignoreDrag, false);
box.addEventListener("drop", handleDrop, false);

// Now set up the chat interface
aWin.document.getElementById("chatForm").onsubmit = function() {
var localChat = aWin.document.getElementById("localChat");
var message = localChat.value;
aOutgoingChannel.send(message);
localChat.value = "";
// XXX: Sometimes insertChatMessage throws an exception, don't know why yet.
try {
insertChatMessage(aWin, "Me", message);
} catch(e) {}
return false;
};
aWin.document.getElementById("localChat").removeAttribute("disabled");

function ignoreDrag(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = "copy";

if (e.type == "dragover") {
win.document.getElementById("fileDrop").style.display = "block";
aWin.document.getElementById("fileDrop").style.display = "block";
} else {
win.document.getElementById("fileDrop").style.display = "none";
aWin.document.getElementById("fileDrop").style.display = "none";
}
}

Expand All @@ -210,15 +208,15 @@ function setupFileSharing(win, dc, target) {
var files = e.target.files || e.dataTransfer.files;
if (files.length) {
for (var i = 0, f; f = files[i]; i++) {
dc.send(JSON.stringify({type: "file", filename: f.name}));
dc.send(f);
aOutgoingChannel.send(JSON.stringify({type: "file", filename: f.name}));
aOutgoingChannel.send(f);
}
} else {
var url = e.dataTransfer.getData("URL");
if (!url.trim()) {
url = e.dataTransfer.mozGetDataAt("text/x-moz-text-internal", 0);
}
dc.send(JSON.stringify({type: "url", url: url}));
aOutgoingChannel.send(JSON.stringify({type: "url", url: url}));
}
}
}
Expand Down Expand Up @@ -380,9 +378,6 @@ function setupEventSource() {
pc.setRemoteDescription(answer, function() {
// Nothing to do for the audio/video. The interesting things for
// them will happen in onaddstream.
// We need to establish the data connection though.
if (data.callerPort && data.calleePort)
pc.connectDataConnection(data.callerPort, data.calleePort);
}, function(err) {alert("failed to setRemoteDescription with answer, " + err);});
}, false);

Expand All @@ -398,7 +393,8 @@ function setupEventSource() {
// The chat to close doesn't exist any more...
return;
}
webrtcMedia.endCall(chat.pc, chat.dc, chat.win, chat.audioOnly);
webrtcMedia.endCall(chat.pc, chat.incomingChannel, chat.outgoingChannel,
chat.win, chat.audioOnly);
delete gChats[data.from];
chat.win.close();
});
Expand Down

0 comments on commit 5bfff9d

Please sign in to comment.