-
Notifications
You must be signed in to change notification settings - Fork 13
/
PhoneRTCMediaHandler.js
283 lines (243 loc) · 8.38 KB
/
PhoneRTCMediaHandler.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
var PhoneRTCMediaHandler = function(session, options) {
var events = [
];
options = options || {};
this.logger = session.ua.getLogger('sip.invitecontext.mediahandler', session.id);
this.session = session;
this.localMedia = null;
this.ready = true;
this.mediaStreamManager = SIP.WebRTC.MediaStreamManager.cast(options.mediaStreamManager);
this.audioMuted = false;
this.videoMuted = false;
// old init() from here on
var idx, length, server,
servers = [],
stunServers = options.stunServers || null,
turnServers = options.turnServers || null,
config = this.session.ua.configuration;
this.RTCConstraints = options.RTCConstraints || {};
if (!stunServers) {
stunServers = config.stunServers;
}
if(!turnServers) {
turnServers = config.turnServers;
}
/* Change 'url' to 'urls' whenever this issue is solved:
* https://code.google.com/p/webrtc/issues/detail?id=2096
*/
servers.push({'url': stunServers});
length = turnServers.length;
for (idx = 0; idx < length; idx++) {
server = turnServers[idx];
servers.push({
'url': server.urls,
'username': server.username,
'credential': server.password
});
}
this.initEvents(events);
this.phonertc = {};
};
PhoneRTCMediaHandler.prototype = Object.create(SIP.MediaHandler.prototype, {
// Functions the session can use
isReady: {writable: true, value: function isReady () {
return this.ready;
}},
close: {writable: true, value: function close () {
this.logger.log('closing PeerConnection');
// have to check signalingState since this.close() gets called multiple times
// TODO figure out why that happens
if(this.peerConnection && this.peerConnection.signalingState !== 'closed') {
this.peerConnection.close();
if(this.localMedia) {
this.mediaStreamManager.release(this.localMedia);
}
}
}},
/**
* @param {Function} onSuccess
* @param {Function} onFailure
* @param {SIP.WebRTC.MediaStream | (getUserMedia constraints)} [mediaHint]
* the MediaStream (or the constraints describing it) to be used for the session
*/
getDescription: {writable: true, value: function getDescription (onSuccess, onFailure, mediaHint) {
onFailure = onFailure;
mediaHint = mediaHint;
if (!this.phonertc.role) {
this.phonertcCall('caller');
}
var pcDelay = 2000;
setTimeout(function () {
var sdp = this.phonertc.localSdp;
if (this.phonertc.role !== 'caller') {
sdp = sdp.replace('a=setup:actpass', 'a=setup:passive');
}
sdp = sdp.replace(/a=crypto.*\r\n/g, '');
onSuccess(sdp);
}.bind(this), pcDelay);
}},
phonertcSendMessageCallback: {writable: true, value: function phonertcSendMessageCallback (data) {
this.logger.log("XXX phonertcSendMessageCallback: " + JSON.stringify(data, null, 2));
if (['offer', 'answer'].indexOf(data.type) > -1) {
this.phonertc.localSdp = data.sdp;
}
else if (data.type === 'candidate') {
this.phonertc.localSdp += data.candidate;
}
}},
phonertcCall: {writable: true, value: function phonertcCall (role) {
this.logger.log("XXX phonertcCall: " + role);
this.phonertc.role = role;
cordova.plugins.phonertc.call({
isInitator: role === 'caller', // Caller or callee?
turn: {
host: 'turn:turn.example.com:3478',
username: 'user',
password: 'pass'
},
sendMessageCallback: this.phonertcSendMessageCallback.bind(this),
answerCallback: function () {
window.alert('Callee answered!');
},
disconnectCallback: function () {
window.alert('Call disconnected!');
}
});
}},
/**
* Message reception.
* @param {String} type
* @param {String} sdp
* @param {Function} onSuccess
* @param {Function} onFailure
*/
setDescription: {writable: true, value: function setDescription (sdp, onSuccess, onFailure) {
function setRemoteDescription (type, sdp) {
this.logger.log("XXX setRemoteDescription: " + type + "\n" + sdp);
cordova.plugins.phonertc.receiveMessage({type: type, sdp: sdp});
onSuccess();
}
if (!this.phonertc.role) {
this.phonertcCall('callee');
var pcDelay = 2000;
setTimeout(setRemoteDescription.bind(this, 'offer', sdp), pcDelay);
}
else if (this.phonertc.role = 'caller') {
setRemoteDescription.call(this, 'answer', sdp);
}
else {
this.logger.error('XXX setDescription called, but this.phonertc.role = ' + this.phonertc.role);
onFailure();
}
}},
// Functions the session can use, but only because it's convenient for the application
isMuted: {writable: true, value: function isMuted () {
return {
audio: this.audioMuted,
video: this.videoMuted
};
}},
mute: {writable: true, value: function mute (options) {
options = options;
}},
unmute: {writable: true, value: function unmute (options) {
options = options;
}},
hold: {writable: true, value: function hold () {
this.toggleMuteAudio(true);
this.toggleMuteVideo(true);
}},
unhold: {writable: true, value: function unhold () {
if (!this.audioMuted) {
this.toggleMuteAudio(false);
}
if (!this.videoMuted) {
this.toggleMuteVideo(false);
}
}},
// Functions the application can use, but not the session
getLocalStreams: {writable: true, value: function getLocalStreams () {
var pc = this.peerConnection;
if (pc && pc.signalingState === 'closed') {
this.logger.warn('peerConnection is closed, getLocalStreams returning []');
return [];
}
return (pc.getLocalStreams && pc.getLocalStreams()) ||
pc.localStreams || [];
}},
getRemoteStreams: {writable: true, value: function getRemoteStreams () {
var pc = this.peerConnection;
if (pc && pc.signalingState === 'closed') {
this.logger.warn('peerConnection is closed, getRemoteStreams returning []');
return [];
}
return(pc.getRemoteStreams && pc.getRemoteStreams()) ||
pc.remoteStreams || [];
}},
// Internal functions
hasOffer: {writable: true, value: function hasOffer (where) {
var offerState = 'have-' + where + '-offer';
return this.peerConnection.signalingState === offerState;
// TODO consider signalingStates with 'pranswer'?
}},
createOfferOrAnswer: {writable: true, value: function createOfferOrAnswer (onSuccess, onFailure, constraints) {
var self = this;
function readySuccess () {
var sdp = self.peerConnection.localDescription.sdp;
sdp = SIP.Hacks.Chrome.needsExplicitlyInactiveSDP(sdp);
self.ready = true;
onSuccess(sdp);
}
function onSetLocalDescriptionSuccess() {
if (self.peerConnection.iceGatheringState === 'complete' && self.peerConnection.iceConnectionState === 'connected') {
readySuccess();
} else {
self.onIceCompleted = function() {
self.logger.log('ICE Gathering Completed');
self.onIceCompleted = undefined;
readySuccess();
};
}
}
function methodFailed (methodName, e) {
self.logger.error('peerConnection.' + methodName + ' failed');
self.logger.error(e);
self.ready = true;
onFailure(e);
}
self.ready = false;
var methodName = self.hasOffer('remote') ? 'createAnswer' : 'createOffer';
self.peerConnection[methodName](
function(sessionDescription){
self.peerConnection.setLocalDescription(
sessionDescription,
onSetLocalDescriptionSuccess,
methodFailed.bind(null, 'setLocalDescription')
);
},
methodFailed.bind(null, methodName),
constraints
);
}},
addStream: {writable: true, value: function addStream (stream, onSuccess, onFailure, constraints) {
try {
this.peerConnection.addStream(stream, constraints);
} catch(e) {
this.logger.error('error adding stream');
this.logger.error(e);
onFailure(e);
return;
}
onSuccess();
}},
toggleMuteHelper: {writable: true, value: function toggleMuteHelper (trackGetter, mute) {
trackGetter = trackGetter;
mute = mute;
}},
toggleMuteAudio: {writable: true, value: function toggleMuteAudio (mute) {
this.toggleMuteHelper('getAudioTracks', mute);
}},
toggleMuteVideo: {writable: true, value: function toggleMuteVideo (mute) {
this.toggleMuteHelper('getVideoTracks', mute);
}}
});