forked from mdale/html5videowall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
videochat.js
155 lines (150 loc) · 5.2 KB
/
videochat.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
(function( global, $){
var connected = false,
local,
remote,
localStream,
remoteStream,
pc,
peers = 0,
ready = false;
getUserMedia(function() {
ready = true;
});
function stop() {
if (localStream) {
localStream.stop()
localStream = null;
app.$ui.videos.set('local', '');
}
hangup();
}
function getUserMedia(callback) {
local = $('#local')[0];
remote = $('#remote')[0];
if (localStream) {
callback && callback();
return;
}
var get = 'getUserMedia';
if (!navigator.getUserMedia && navigator.webkitGetUserMedia) {
get = 'webkitGetUserMedia';
};
navigator[get]({audio:true, video:true},
function(stream) {
console.log("User has granted access to local media.");
localStream = stream;
var url = webkitURL.createObjectURL(stream);
local.src = url;
local.play();
callback && callback();
},
function(error) {
console.log("Failed to get access to local media. Error code was " + error.code);
//alert("Failed to get access to local media. Error code was " + error.code + ".");
}
);
console.log("Requested access to local media with new syntax.");
}
var remoteId;
function send(message) {
connection.sendMessage({
to: remoteId,
rtc: message
})
}
window.call = function(userId) {
$('#local').show();
if ( remoteId ) {
return;
}
remoteId = userId;
var offer;
if (!pc) {
createPeerConnection(userId);
}
if (localStream) {
pc.addStream(localStream, null);
}
offer = pc.createOffer({ has_video: true, has_audio: true });
pc.setLocalDescription(webkitPeerConnection00.SDP_OFFER, offer);
send({ type: "offer", data: offer.toSdp() });
pc.startIce();
}
window.hangup = function() {
if (remoteStream) {
remoteStream = null;
}
if (pc) {
pc.close();
pc = null;
}
send({ type: "hangup" });
remoteId = null;
$('#remote').hide();
$('#local').hide();
}
function createPeerConnection () {
pc = new webkitPeerConnection00(
"STUN stun.l.google.com:19302",
function(candidate, moreToFollow) {
if (moreToFollow) {
var msg = JSON.stringify({ label: candidate.label, sdp: candidate.toSdp() });
send({ type: "icecandidate", data: msg });
} else {
candidate && console.log(candidate, candidate.toSdp());
//console.log("Received end of candidates (more_to_follow == false)");
}
}
);
pc.onaddstream = function(event) {
console.log("Remote stream added.", event);
remoteStream = event.stream;
var url = webkitURL.createObjectURL(event.stream);
remote.src = url;
remote.play();
$('#remote').show();
};
pc.onremovestream = function(event) {
console.log("Remote stream removed.");
remote.src = '';
}
}
connection.onMessage(function(msg) {
var offer,
answer,
candidate, c,
message;
if (msg.data.rtc && msg.data.to == localStorage.id) {
message = msg.data.rtc;
if (message.type == 'hangup') {
hangup();
} else if (message.type == 'offer' && !remoteId) {
remoteId = msg.user;
console.log('offer', message);
if (!pc) {
createPeerConnection();
}
// create the PeerConnection
if (localStream)
pc.addStream(localStream, null);
// feed the received offer into the PeerConnection and
// start candidate generation
offer = new SessionDescription(message.data);
pc.setRemoteDescription(webkitPeerConnection00.SDP_OFFER, offer);
answer = pc.createAnswer(message.data, { has_video: true, has_audio: true });
pc.setLocalDescription(webkitPeerConnection00.SDP_ANSWER, answer);
send({ type: "answer", data: answer.toSdp() });
pc.startIce();
} else if (message.type == "answer") {
console.log('answer', message);
answer = new SessionDescription(message.data);
pc.setRemoteDescription(webkitPeerConnection00.SDP_ANSWER, answer);
} else if (message.type == "icecandidate") {
candidate = JSON.parse(message.data);
//console.log("ice", candiate);
candidate = new IceCandidate(candidate.label, candidate.sdp);
pc.processIceMessage(candidate);
}
}
});
})( window, window.jQuery )