-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmediaHandlerLegacy-0.6.4.js
180 lines (166 loc) · 5.11 KB
/
mediaHandlerLegacy-0.6.4.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
/**
* @fileoverview PhoneRTCMediaHandlerLegacy
*/
/**
* Implements the PhoneRTC media handler constructor for SIP.js <= 0.6.4.
*/
var PhoneRTCMediaHandlerLegacy = function(session, options) {
// Create a logger.
window.console.log('Loading the Legacy PhoneRTC 2.0 Media Handler.');
// Finish initialization.
this.phonertc = {
/*
* Possible states are:
* - disconnected
* - connected
* - holding
* - muted
*/
'state': 'disconnected'
};
}
PhoneRTCMediaHandlerLegacy.prototype = Object.create(SIP.MediaHandler.prototype, {
/**
* render() is called by sip.js so it must be defined.
*/
render: {writable: true, value: function render() { }},
isReady: {writable: true, value: function isReady() { return true; }},
close: {writable: true, value: function close() {
var state = this.phonertc.state;
if(state !== 'disconnected') {
this.phonertc.session.close();
this.phonertc.session = null;
// Update our state.
this.phonertc.state = 'disconnected';
}
}},
getDescription: {writable: true, value: function getDescription(onSuccess, onFailure, mediaHint) {
var phonertc = this.phonertc;
var isInitiator = !phonertc.role || phonertc.role === 'caller';
if(isInitiator && phonertc.state === 'disconnected') {
this.startSession(null, onSuccess, onFailure);
} else {
if(phonertc.state === 'holding') {
onSuccess(phonertc.sdp.replace(/a=sendrecv\r\n/g, 'a=sendonly\r\n'));
} else {
onSuccess(phonertc.sdp);
if(phonertc.state === 'disconnected') {
phonertc.state = 'connected';
}
}
}
}},
setDescription: {writable: true, value: function setDescription(sdp, onSuccess, onFailure) {
var phonertc = this.phonertc;
var isNewCall = !phonertc.role;
if(isNewCall) {
this.startSession(sdp, onSuccess, onFailure);
}
var session = phonertc.session;
if((phonertc.role === 'caller' && phonertc.state === 'disconnected')) {
session.receive({'type': 'answer', 'sdp': sdp});
onSuccess();
if(phonertc.state === 'disconnected') {
phonertc.state = 'connected';
}
} else {
onSuccess();
}
}},
isMuted: {writable: true, value: function isMuted() {
return {
audio: this.phonertc.state === 'muted' ||
this.phonertc.state === 'holding',
video: true
};
}},
mute: {writable: true, value: function mute(options) {
var phonertc = this.phonertc;
var state = phonertc.state;
if(state === 'connected') {
phonertc.session.mute();
phonertc.state = 'muted';
}
}},
unmute: {writable: true, value: function unmute(options) {
var phonertc = this.phonertc;
var state = phonertc.state;
if(state === 'muted') {
phonertc.session.unmute();
phonertc.state = 'connected';
}
}},
hold: {writable: true, value: function hold () {
var phonertc = this.phonertc;
var state = phonertc.state;
if(state === 'connected') {
phonertc.session.mute();
phonertc.state = 'holding';
}
}},
unhold: {writable: true, value: function unhold () {
var phonertc = this.phonertc;
var state = phonertc.state;
if(state === 'holding') {
phonertc.session.unmute();
phonertc.state = 'connected';
}
}},
// Local Methods.
startSession: {writable: true, value: function startSession(sdp, onSuccess, onFailure) {
var phonertc = this.phonertc;
phonertc.role = sdp === null ? 'caller' : 'callee';
// Unfortunately, there is no message to let us know
// that PhoneRTC has finished gathering ice candidates.
// We use a watchdog to make sure all the ICE candidates
// are allocated before returning the SDP.
var watchdog = null;
phonertc.session = new cordova.plugins.phonertc.Session({
isInitiator: phonertc.role === 'caller'
});
var candidates = '';
phonertc.session.on('sendMessage', function (data) {
if(data.type === 'offer' || data.type === 'answer') {
phonertc.sdp = data.sdp;
if(data.type === 'answer') {
if(onSuccess) { onSuccess(); }
}
} else if(data.type === 'candidate') {
// If we receive another candidate we stop
// the watchdog and restart it again later.
if(watchdog !== null) {
clearTimeout(watchdog);
}
// Append the candidate to the SDP.
var candidate = "a=" + data.candidate + "\r\n";
if(data.id === 'audio') {
candidates += candidate;
}
// Start the watchdog.
watchdog = setTimeout(function() {
if(onSuccess) {
phonertc.sdp += candidates;
onSuccess(phonertc.sdp);
}
}, 500);
}
});
// If we received a session description pass it on to the
// PhoneRTC plugin.
if(phonertc.role === 'callee') {
phonertc.session.receive({'type': 'offer', 'sdp': sdp});
}
// Start the media.
phonertc.session.initialize();
}}
});
/**
* MediaHandler
* @class PeerConnection helper Class.
* @param {SIP.Session} session
* @param {Object} [options]
*/
module.exports = function(SIP) {
// Return the PhoneRTC media handler implementation.
return PhoneRTCMediaHandlerLegacy;
};