-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
203 lines (170 loc) · 3.32 KB
/
index.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
const { EventTarget, defineEventAttribute } = require('event-target-shim');
const uuidv4 = require('uuid').v4;
class FakeMediaStreamTrack extends EventTarget
{
constructor(
{
kind,
id,
label,
isolated,
muted,
readyState,
constraints,
data
} = {}
)
{
super();
if (!kind)
{
throw new TypeError('missing kind');
}
// Id.
// @type {string}
this._id = id || uuidv4();
// Kind ('audio' or 'video').
// @type {string}
this._kind = kind;
// Label.
// @type {string}
this._label = label || '';
// Isolated.
// @type {boolean}
this._isolated = isolated || false;
// Enabled flag.
// @type {boolean}
this._enabled = true;
// Muted flag.
// @type {boolean}
this._muted = muted || false;
// Ready state ('live' or 'ended').
// @type {string}
this._readyState = readyState || 'live';
// MediaTrackConstraints.
// @type {MediaTrackConstraints}
this._constraints = constraints || {};
// Custom data.
// @type {any}
this._data = data || {};
}
get id()
{
return this._id;
}
get kind()
{
return this._kind;
}
get label()
{
return this._label;
}
get isolated()
{
return this._isolated;
}
get enabled()
{
return this._enabled;
}
set enabled(enabled)
{
const changed = this._enabled !== enabled;
this._enabled = enabled;
if (changed)
{
this.dispatchEvent({ type: '@enabledchange' });
}
}
get muted()
{
return this._muted;
}
get readyState()
{
return this._readyState;
}
get data()
{
return this._data;
}
set data(data)
{
throw new TypeError('cannot replace data object');
}
clone({ id, data } = {})
{
return new FakeMediaStreamTrack(
{
id : id || uuidv4(),
kind : this._kind,
label : this._label,
isolated : this._isolated,
enabled : this._enabled,
muted : this._muted,
readyState : this._readyState,
constraints : this._constraints,
data : data !== undefined ? data : this._data
});
}
stop()
{
if (this._readyState === 'ended')
{
return;
}
this._readyState = 'ended';
this.dispatchEvent({ type: '@stop' });
}
getConstraints()
{
return this._constraints;
}
applyConstraints(constraints)
{
if (this._readyState === 'ended')
{
return;
}
this._constraints = constraints;
}
remoteStop()
{
if (this._readyState === 'ended')
{
return;
}
this._readyState = 'ended';
this.dispatchEvent({ type: '@stop' });
this.dispatchEvent({ type: 'ended' });
}
remoteMute()
{
if (this._muted)
{
return;
}
this._muted = true;
this.dispatchEvent({ type: 'mute' });
}
remoteUnmute()
{
if (!this._muted)
{
return;
}
this._muted = false;
this.dispatchEvent({ type: 'unmute' });
}
}
// Define EventTarget properties.
defineEventAttribute(FakeMediaStreamTrack.prototype, 'ended');
defineEventAttribute(FakeMediaStreamTrack.prototype, 'mute');
defineEventAttribute(FakeMediaStreamTrack.prototype, 'unmute');
defineEventAttribute(FakeMediaStreamTrack.prototype, '@enabledchange');
defineEventAttribute(FakeMediaStreamTrack.prototype, '@stop');
// NOTE: These are not implemented/dispatched.
defineEventAttribute(FakeMediaStreamTrack.prototype, 'isolationchange');
defineEventAttribute(FakeMediaStreamTrack.prototype, 'overconstrained');
exports.FakeMediaStreamTrack = FakeMediaStreamTrack;