-
Notifications
You must be signed in to change notification settings - Fork 3
/
media-handler.js
128 lines (101 loc) · 2.99 KB
/
media-handler.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
var EventEmitter = require('eventemitter3');
var inherits = require('inherits');
var extend = require('cog/extend');
var capture = require('rtc-capture');
var utils = require('./utils');
var taskqueue = require('rtc-taskqueue');
var ObservIce = require('observ-ice');
var SIP = require('sip.js');
function MediaHandler(config, session, opts) {
if (! (this instanceof MediaHandler)) {
return new MediaHandler(config, session, opts);
}
EventEmitter.call(this);
// save the config to pass onto capture, etc
this.config = config;
// create a peer connection
this.pc = utils.createPeer(config, session, opts);
this.queue = taskqueue(this.pc, this._buildTQOpts(config, session, opts));
this.ice = ObservIce(this.pc);
}
inherits(MediaHandler, EventEmitter);
module.exports = MediaHandler;
var prot = MediaHandler.prototype;
prot.close = function() {
console.warn('TODO: close');
};
prot.getDescription = function(succeed, fail, hint) {
var pc = this.pc;
var q = this.queue;
var ice = this.ice;
// generate some default constraints if none supplied
var constraints = (hint || {}).constraints || { audio: true, video: true };
function connect(stream) {
var methodName = utils.hasOffer(pc, 'remote') ? 'createAnswer' : 'createOffer';
var stop;
q.once('sdp.local', function(desc) {
if (ice.gathered()) {
return ready(pc.localDescription.sdp);
}
else {
stop = ice.gathered(function(value) {
if (value) {
stop();
ready(pc.localDescription.sdp);
}
});
}
});
pc.addStream(stream);
q[methodName].call(q);
}
function ready(sdp) {
// sdp = SIP.Hacks.Chrome.needsExplicitlyInactiveSDP(sdp);
// sdp = SIP.Hacks.AllBrowsers.unmaskDtls(sdp);
succeed(sdp);
}
console.log('getDescription called: ', arguments);
if (hint && hint.stream) {
return connect(hint.stream);
}
capture(constraints, this.config, function(err, stream) {
if (err) {
return fail(err);
}
return connect(stream);
});
};
prot.setDescription = function(sdp, pass, fail) {
var raw = {
type: utils.hasOffer(this.pc, 'local') ? 'answer' : 'offer',
sdp: sdp
};
this.pc.setRemoteDescription(utils.createSessionDesc(this.config, raw), pass, fail);
};
// TODO: implement these additional functions
prot.isMuted = function() {
return {
audio: false,
video: false
};
};
prot.mute = function(opts) {};
prot.unmute = function(opts) {};
prot.hold = function() {};
prot.unhold = function() {};
prot.getLocalStreams = function() {
return this.pc.getLocalStreams();
};
prot.getRemoteStreams = function() {
return this.pc.getRemoteStreams();
};
prot._buildTQOpts = function(config, session, opts) {
var constraints = (session && session.RTCConstraints) || {};
var cfg = extend({}, config, constraints.mandatory);
(constraints.optional || []).forEach(function(item) {
Object.keys(item).forEach(function(key) {
cfg[key] = item[key];
});
});
return cfg;
};