This repository has been archived by the owner on Dec 4, 2017. It is now read-only.
forked from mixedpuppy/socialapi-demo
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmobile.js
302 lines (248 loc) · 7.81 KB
/
mobile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
var gUsername;
var gContacts = {};
var gChat;
var gFake = false;
var gIncomingChannel;
var gOutgoingChannel;
function startGuest() {
$("#signin").hide();
$("#guest").html('<form onsubmit="javascript:guestLogin(event);">' +
' <input type="text" id="user"/>' +
' <input type="submit" value="Sign in"/>' +
'</form>'
);
$("#user").focus();
}
function guestLogin(event) {
var user = $("#user").attr("value");
remoteLogin({assertion: user, fake: true});
gFake = true;
event.preventDefault();
}
function signedIn(aEmail) {
gUsername = aEmail;
$("#useridbox").text("Welcome " + aEmail + "!");
$("#useridbox").show();
$("#nouserid").hide();
$("#signin").hide();
$("#guest").hide();
$("#signout").show();
gContacts[aEmail] = $("<li>"); // Avoid displaying the user in the contact list.
setupEventSource();
}
function signedOut() {
gUsername = "";
$("#useridbox").text("");
$("#useridbox").hide();
$("#nouserid").show();
$("#signout").hide();
window.location.reload();
}
function onSignout() {
if (!gFake)
signout();
else {
gFake = false;
onPersonaLogout();
}
}
function onPersonaLogin(assertion) {
$("#signin").hide();
$("#guest").hide();
remoteLogin({assertion: assertion});
}
function onPersonaLogout() {
remoteLogout();
}
function onPersonaReady() {
if (gUsername || remoteLoginPending)
return;
$("#signin").show();
$("#guest").show();
}
function onLoad() {
watchPersonaLogins(onPersonaLogin, onPersonaLogout, onPersonaReady);
}
function onContactClick(aEvent) {
callPerson(aEvent.target.getAttribute("user"));
}
function resizeVideo() {
var localVideo = document.getElementById("localVideo");
var video = document.getElementById("remoteVideo");
video.setAttribute("style", "position: fixed; top: 0; left: 0; z-index: 1; background: black;");
var height = window.innerHeight;
var width = window.innerWidth;
video.setAttribute("width", width);
video.setAttribute("height", height);
localVideo.setAttribute("width", "108");
localVideo.setAttribute("height", "81");
localVideo.setAttribute("style", "position: fixed; z-index: 2;");
localVideo.style.top = (height - 81) + "px";
localVideo.style.left = (width - 108) + "px";
}
function setupVideo() {
if (gChat.audioOnly) {
$("#video").hide();
$("#calling").show();
document.getElementById("calleeName").textContent = gChat.who;
document.getElementById("closecall").setAttribute("class", "audioOnly");
return;
}
$("#video").show();
document.getElementById("closecall").setAttribute("class", "videoCall");
var video = document.getElementById("remoteVideo");
video.onclick = function() {
document.getElementById("video").mozRequestFullScreen();
};
resizeVideo();
window.addEventListener("resize", resizeVideo);
}
function callPerson(aPerson) {
if (gChat)
return;
gChat = {who: aPerson,
audioOnly: !document.getElementById("shareCamera").checked};
setupVideo();
$("#call").show();
$("#header").hide();
$("#contacts").hide();
gChat.pc = webrtcMedia.startCall(aPerson, window, gChat.audioOnly, onConnection, setupChat);
}
var filename = "default.txt";
function setupEventSource() {
var source = new EventSource("events?source=mobile");
source.onerror = function(e) {
window.location.reload();
};
source.addEventListener("ping", function(e) {}, false);
source.addEventListener("userjoined", function(e) {
if (e.data in gContacts) {
return;
}
var button = $('<button class="callButton" user="'+ e.data + '">Call</button>');
var c = $("<li>" + e.data + "</li>");
c.append(button);
$("#contactslist").append(c);
button.click(onContactClick);
gContacts[e.data] = c;
}, false);
source.addEventListener("userleft", function(e) {
if (!gContacts[e.data]) {
return;
}
gContacts[e.data].remove();
delete gContacts[e.data];
}, false);
source.addEventListener("offer", function(e) {
if (gChat)
return;
var data = JSON.parse(e.data);
var audioOnly = JSON.parse(data.request).sdp.indexOf("m=video") == -1;
gChat = {who: data.from, audioOnly: audioOnly};
$("#callAnswer").show();
document.getElementById("callerName").textContent = data.from;
document.getElementById("reject").onclick = function() {closeCall();};
document.getElementById("accept").onclick = function() {
$("#callAnswer").hide();
$("#call").show();
$("#header").hide();
$("#contacts").hide();
if (!document.getElementById("shareCamera").checked)
gChat.audioOnly = audioOnly = true;
setupVideo();
gChat.pc = webrtcMedia.handleOffer(data, window, audioOnly, onConnection, setupChat);
};
}, false);
source.addEventListener("answer", function(e) {
var data = JSON.parse(e.data);
var answer = JSON.parse(data.request);
if (!gChat.audioOnly && answer.sdp.indexOf("m=video") == -1) {
// If the other party answered with audio only, switch the
// display back to showing the chat.
gChat.audioOnly = true;
var audio = document.getElementById("localAudio");
var video = document.getElementById("localVideo");
// Using the audio+video stream as audio only has the
// unfortunate effect of keeping the webcam active...
audio.mozSrcObject = video.mozSrcObject;
video.mozSrcObject = null;
window.removeEventListener("resize", resizeVideo);
setupVideo();
}
var pc = gChat.pc;
pc.setRemoteDescription(JSON.parse(data.request), function() {
// Nothing to do for the audio/video. The interesting things for
// them will happen in onaddstream.
}, function(err) {alert("failed to setRemoteDescription with answer, " + err);});
}, false);
source.addEventListener("stopcall", function(e) {
var data = JSON.parse(e.data);
if (!gChat)
// XXX alert or log?
return;
if (gChat.who === data.from)
endCall();
}, false);
window.addEventListener("beforeunload", function() {
source.onerror = null;
source.close();
}, true);
}
function onConnection(aWin, aPc, aPerson, aOriginator) {
}
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")
window.open(details.url);
else
throw new Error("JSON, but not an url");
} catch(e) {
insertChatMessage("Them", evt.data);
}
};
document.getElementById("chatForm").onsubmit = function() {
var localChat = document.getElementById("localChat");
var message = localChat.value;
aOutgoingChannel.send(message);
localChat.value = "";
insertChatMessage("Me", message);
return false;
};
document.getElementById("localChat").removeAttribute("disabled");
}
function insertChatMessage(aFrom, aMessage) {
var box = document.getElementById("chat");
box.innerHTML += "<span class=\"" + aFrom + "\">" + aFrom + "</span>: " + aMessage + "<br/>";
box.scrollTop = box.scrollTopMax;
}
function cleanChat() {
var box = document.getElementById("chat");
box.innerHTML = '';
document.getElementById("localChat").setAttribute("disabled", "disabled");
}
function closeCall() {
if (!gChat)
return;
stopCall(gChat.who);
endCall();
}
function endCall() {
$("#callAnswer").hide();
$("#calling").hide();
document.getElementById("accept").onclick = null;
document.getElementById("reject").onclick = null;
webrtcMedia.endCall(gChat.pc, gIncomingChannel, gOutgoingChannel, window,
gChat.audioOnly);
if (!gChat.audioOnly)
window.removeEventListener("resize", resizeVideo);
gChat = null;
$("#call").hide();
$("#header").show();
$("#contacts").show();
cleanChat();
}