-
Notifications
You must be signed in to change notification settings - Fork 14
/
rtcrtpsender.js
129 lines (117 loc) · 3.67 KB
/
rtcrtpsender.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
/*
* Copyright (c) 2017 rtcpeerconnection-shim authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/
'use strict';
var util = require('./util');
/* a wrapper around Edge's RTCRtpSender that does a lazy construct of
* of the native sender when all the required parameters (track and
* transport) are available.
*/
module.exports = function(window) {
var RTCRtpSender_ = window.RTCRtpSender;
var RTCRtpSender = function(trackOrKind, transport) {
this._sender = null;
this._track = null;
this._transport = null;
if (typeof trackOrKind === 'string') {
this.kind = trackOrKind;
this._transport = transport || null;
} else if (!transport) {
this._track = trackOrKind;
this.kind = trackOrKind.kind;
} else {
this._sender = new RTCRtpSender_(trackOrKind, transport);
this.kind = trackOrKind.kind;
}
this._isStopped = false;
};
// ORTC defines the DTMF sender a bit different.
// https://github.com/w3c/ortc/issues/714
Object.defineProperty(RTCRtpSender.prototype, 'dtmf', {
get: function() {
if (this._dtmf === undefined) {
if (this.kind === 'audio') {
if (!this._sender) {
throw (util.makeError('InvalidStateError',
'Can not access dtmf in this state'));
} else {
this._dtmf = new window.RTCDtmfSender(this._sender);
}
} else if (this.kind === 'video') {
this._dtmf = null;
}
}
return this._dtmf;
}
});
RTCRtpSender.getCapabilities = RTCRtpSender_.getCapabilities;
Object.defineProperty(RTCRtpSender.prototype, 'track', {
get: function() {
return this._sender ? this._sender.track : this._track;
}
});
Object.defineProperty(RTCRtpSender.prototype, 'transport', {
get: function() {
return this._sender ? this._sender.transport : this._transport;
}
});
RTCRtpSender.prototype.setTransport = function(transport) {
if (!this._sender && this._track) {
this._sender = new RTCRtpSender_(this._track, transport);
} else if (this._sender) {
this._sender.setTransport(transport);
} else {
this._transport = transport;
}
};
RTCRtpSender.prototype.replaceTrack = function(track) {
if (track && this.kind !== track.kind) {
return Promise.reject(new TypeError());
}
if (this._sender) {
this._sender.replaceTrack(track);
} else if (track && this._transport) {
this._sender = new RTCRtpSender_(track, this._transport);
} else {
this._track = track;
}
return Promise.resolve();
};
RTCRtpSender.prototype.setTrack = function(track) { // deprecated.
if (track && this.kind !== track.kind) {
return Promise.reject(new TypeError());
}
if (this._sender) {
this._sender.setTrack(track);
} else if (track && this._transport) {
this._sender = new RTCRtpSender_(track, this._transport);
} else {
this._track = track;
}
return Promise.resolve();
};
RTCRtpSender.prototype.send = function(parameters) {
if (this._sender) {
return this._sender.send(parameters);
}
return Promise.reject(util.makeError('InvalidStateError',
'Can not call send in this state'));
};
RTCRtpSender.prototype.stop = function() {
if (this._sender) {
this._sender.stop();
}
};
RTCRtpSender.prototype.getStats = function() {
if (this._sender) {
return this._sender.getStats();
}
return Promise.reject(util.makeError('InvalidStateError',
'Can not call send in this state'));
};
return RTCRtpSender;
};