|
@@ -95,7 +95,7 @@ export default class JitsiTrack extends EventEmitter {
|
95
|
95
|
this.type = trackMediaType;
|
96
|
96
|
this.track = track;
|
97
|
97
|
this.videoType = videoType;
|
98
|
|
- this.handlers = {};
|
|
98
|
+ this.handlers = new Map();
|
99
|
99
|
|
100
|
100
|
/**
|
101
|
101
|
* Indicates whether this JitsiTrack has been disposed. If true, this
|
|
@@ -105,11 +105,30 @@ export default class JitsiTrack extends EventEmitter {
|
105
|
105
|
* @type {boolean}
|
106
|
106
|
*/
|
107
|
107
|
this.disposed = false;
|
108
|
|
- this._setHandler('inactive', streamInactiveHandler);
|
|
108
|
+
|
|
109
|
+ /**
|
|
110
|
+ * The inactive handler which will be triggered when the underlying
|
|
111
|
+ * media stream ends.
|
|
112
|
+ * @type {Function}
|
|
113
|
+ */
|
|
114
|
+ this._streamInactiveHandler = streamInactiveHandler;
|
|
115
|
+ this._bindInactiveHandler(streamInactiveHandler);
|
109
|
116
|
}
|
110
|
117
|
|
111
|
118
|
/* eslint-enable max-params */
|
112
|
119
|
|
|
120
|
+ /**
|
|
121
|
+ * Binds the inactive handler.
|
|
122
|
+ * @param {Function} streamInactiveHandler
|
|
123
|
+ * @private
|
|
124
|
+ */
|
|
125
|
+ _bindInactiveHandler(streamInactiveHandler) {
|
|
126
|
+ if (RTCBrowserType.isFirefox()) {
|
|
127
|
+ implementOnEndedHandling(this);
|
|
128
|
+ }
|
|
129
|
+ addMediaStreamInactiveHandler(this.stream, streamInactiveHandler);
|
|
130
|
+ }
|
|
131
|
+
|
113
|
132
|
/**
|
114
|
133
|
* Sets handler to the WebRTC MediaStream or MediaStreamTrack object
|
115
|
134
|
* depending on the passed type.
|
|
@@ -117,20 +136,45 @@ export default class JitsiTrack extends EventEmitter {
|
117
|
136
|
* @param {Function} handler the handler.
|
118
|
137
|
*/
|
119
|
138
|
_setHandler(type, handler) {
|
120
|
|
- this.handlers[type] = handler;
|
|
139
|
+ if (!trackHandler2Prop.hasOwnProperty(type)) {
|
|
140
|
+ logger.error(`Invalid handler type ${type}`);
|
|
141
|
+
|
|
142
|
+ return;
|
|
143
|
+ }
|
|
144
|
+ if (handler) {
|
|
145
|
+ this.handlers.set(type, handler);
|
|
146
|
+ } else {
|
|
147
|
+ this.handlers.delete(type);
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ if (this.stream) {
|
|
151
|
+ // FIXME why only video tacks ?
|
|
152
|
+ for (const track of this.stream.getVideoTracks()) {
|
|
153
|
+ track[trackHandler2Prop[type]] = handler;
|
|
154
|
+ }
|
|
155
|
+ }
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+ /**
|
|
159
|
+ * Unregisters all event handlers bound to the underlying media stream/track
|
|
160
|
+ * @private
|
|
161
|
+ */
|
|
162
|
+ _unregisterHandlers() {
|
121
|
163
|
if (!this.stream) {
|
|
164
|
+ logger.warn(
|
|
165
|
+ `${this}: unable to unregister handlers - no stream object`);
|
|
166
|
+
|
122
|
167
|
return;
|
123
|
168
|
}
|
124
|
169
|
|
125
|
|
- if (type === 'inactive') {
|
126
|
|
- if (RTCBrowserType.isFirefox()) {
|
127
|
|
- implementOnEndedHandling(this);
|
|
170
|
+ for (const type of this.handlers.keys()) {
|
|
171
|
+ // FIXME why only video tracks ?
|
|
172
|
+ for (const videoTrack of this.stream.getVideoTracks()) {
|
|
173
|
+ videoTrack[trackHandler2Prop[type]] = undefined;
|
128
|
174
|
}
|
129
|
|
- addMediaStreamInactiveHandler(this.stream, handler);
|
130
|
|
- } else if (trackHandler2Prop.hasOwnProperty(type)) {
|
131
|
|
- this.stream.getVideoTracks().forEach(track => {
|
132
|
|
- track[trackHandler2Prop[type]] = handler;
|
133
|
|
- }, this);
|
|
175
|
+ }
|
|
176
|
+ if (this._streamInactiveHandler) {
|
|
177
|
+ addMediaStreamInactiveHandler(this.stream, undefined);
|
134
|
178
|
}
|
135
|
179
|
}
|
136
|
180
|
|
|
@@ -140,11 +184,19 @@ export default class JitsiTrack extends EventEmitter {
|
140
|
184
|
* @param {MediaStream} stream the new stream.
|
141
|
185
|
*/
|
142
|
186
|
_setStream(stream) {
|
|
187
|
+ if (this.stream === stream) {
|
|
188
|
+ logger.warn(`Attempt to set the same stream twice on ${this}`);
|
|
189
|
+
|
|
190
|
+ return;
|
|
191
|
+ }
|
|
192
|
+
|
143
|
193
|
this.stream = stream;
|
144
|
|
- Object.keys(this.handlers).forEach(type => {
|
145
|
|
- typeof this.handlers[type] === 'function'
|
146
|
|
- && this._setHandler(type, this.handlers[type]);
|
147
|
|
- }, this);
|
|
194
|
+ for (const type of this.handlers.keys()) {
|
|
195
|
+ this._setHandler(type, this.handlers.get(type));
|
|
196
|
+ }
|
|
197
|
+ if (this._streamInactiveHandler && this.stream) {
|
|
198
|
+ this._bindInactiveHandler(this._streamInactiveHandler);
|
|
199
|
+ }
|
148
|
200
|
}
|
149
|
201
|
|
150
|
202
|
/**
|