@@ -201,112 +201,136 @@ class Html5 extends Tech {
201201 }
202202
203203 /**
204- * Attempt to force override of native audio/video tracks.
204+ * Attempt to force override of tracks for the given type
205205 *
206+ * @param {String } type - Track type to override, possible values include 'Audio',
207+ * 'Video', and 'Text'.
206208 * @param {Boolean } override - If set to true native audio/video will be overridden,
207209 * otherwise native audio/video will potentially be used.
210+ * @private
208211 */
209- overrideNativeTracks ( override ) {
212+ overrideNative_ ( type , override ) {
210213 // If there is no behavioral change don't add/remove listeners
211- if ( override !== ( this . featuresNativeAudioTracks && this . featuresNativeVideoTracks ) ) {
214+ if ( override !== this [ `featuresNative ${ type } Tracks` ] ) {
212215 return ;
213216 }
214217
215- if ( this . audioTracksListeners_ ) {
216- Object . keys ( this . audioTracksListeners_ ) . forEach ( ( eventName ) => {
217- const elTracks = this . el ( ) . audioTracks ;
218-
219- elTracks . removeEventListener ( eventName , this . audioTracksListeners_ [ eventName ] ) ;
220- } ) ;
221- }
218+ const lowerCaseType = type . toLowerCase ( ) ;
222219
223- if ( this . videoTracksListeners_ ) {
224- Object . keys ( this . videoTracksListeners_ ) . forEach ( ( eventName ) => {
225- const elTracks = this . el ( ) . videoTracks ;
220+ if ( this [ ` ${ lowerCaseType } TracksListeners_` ] ) {
221+ Object . keys ( this [ ` ${ lowerCaseType } TracksListeners_` ] ) . forEach ( ( eventName ) => {
222+ const elTracks = this . el ( ) [ ` ${ lowerCaseType } Tracks` ] ;
226223
227- elTracks . removeEventListener ( eventName , this . videoTracksListeners_ [ eventName ] ) ;
224+ elTracks . removeEventListener ( eventName , this [ ` ${ lowerCaseType } TracksListeners_` ] [ eventName ] ) ;
228225 } ) ;
229226 }
230227
231- this . featuresNativeVideoTracks = ! override ;
232- this . featuresNativeAudioTracks = ! override ;
228+ this [ `featuresNative ${ type } Tracks` ] = ! override ;
229+ this [ ` ${ lowerCaseType } TracksListeners_` ] = null ;
233230
234- this . audioTracksListeners_ = null ;
235- this . videoTracksListeners_ = null ;
231+ this . proxyNativeTracksForType_ ( lowerCaseType ) ;
232+ }
236233
237- this . proxyNativeTracks_ ( ) ;
234+ /**
235+ * Attempt to force override of native audio tracks.
236+ *
237+ * @param {Boolean } override - If set to true native audio will be overridden,
238+ * otherwise native audio will potentially be used.
239+ */
240+ overrideNativeAudioTracks ( override ) {
241+ this . overrideNative_ ( 'Audio' , override ) ;
238242 }
239243
240244 /**
241- * Proxy all native track list events to our track lists if the browser we are playing
242- * in supports that type of track list.
245+ * Attempt to force override of native video tracks.
243246 *
247+ * @param {Boolean } override - If set to true native video will be overridden,
248+ * otherwise native video will potentially be used.
249+ */
250+ overrideNativeVideoTracks ( override ) {
251+ this . overrideNative_ ( 'Video' , override ) ;
252+ }
253+
254+ /**
255+ * Proxy native track list events for the given type to our track
256+ * lists if the browser we are playing in supports that type of track list.
257+ *
258+ * @param {string } name - Track type; values include 'audio', 'video', and 'text'
244259 * @private
245260 */
246- proxyNativeTracks_ ( ) {
247- TRACK_TYPES . names . forEach ( ( name ) => {
248- const props = TRACK_TYPES [ name ] ;
249- const elTracks = this . el ( ) [ props . getterName ] ;
250- const techTracks = this [ props . getterName ] ( ) ;
251-
252- if ( ! this [ `featuresNative${ props . capitalName } Tracks` ] ||
253- ! elTracks ||
254- ! elTracks . addEventListener ) {
255- return ;
261+ proxyNativeTracksForType_ ( name ) {
262+ const props = TRACK_TYPES [ name ] ;
263+ const elTracks = this . el ( ) [ props . getterName ] ;
264+ const techTracks = this [ props . getterName ] ( ) ;
265+
266+ if ( ! this [ `featuresNative${ props . capitalName } Tracks` ] ||
267+ ! elTracks ||
268+ ! elTracks . addEventListener ) {
269+ return ;
270+ }
271+ const listeners = {
272+ change ( e ) {
273+ techTracks . trigger ( {
274+ type : 'change' ,
275+ target : techTracks ,
276+ currentTarget : techTracks ,
277+ srcElement : techTracks
278+ } ) ;
279+ } ,
280+ addtrack ( e ) {
281+ techTracks . addTrack ( e . track ) ;
282+ } ,
283+ removetrack ( e ) {
284+ techTracks . removeTrack ( e . track ) ;
256285 }
257- const listeners = {
258- change ( e ) {
259- techTracks . trigger ( {
260- type : 'change' ,
261- target : techTracks ,
262- currentTarget : techTracks ,
263- srcElement : techTracks
264- } ) ;
265- } ,
266- addtrack ( e ) {
267- techTracks . addTrack ( e . track ) ;
268- } ,
269- removetrack ( e ) {
270- techTracks . removeTrack ( e . track ) ;
271- }
272- } ;
273- const removeOldTracks = function ( ) {
274- const removeTracks = [ ] ;
275-
276- for ( let i = 0 ; i < techTracks . length ; i ++ ) {
277- let found = false ;
286+ } ;
287+ const removeOldTracks = function ( ) {
288+ const removeTracks = [ ] ;
278289
279- for ( let j = 0 ; j < elTracks . length ; j ++ ) {
280- if ( elTracks [ j ] === techTracks [ i ] ) {
281- found = true ;
282- break ;
283- }
284- }
290+ for ( let i = 0 ; i < techTracks . length ; i ++ ) {
291+ let found = false ;
285292
286- if ( ! found ) {
287- removeTracks . push ( techTracks [ i ] ) ;
293+ for ( let j = 0 ; j < elTracks . length ; j ++ ) {
294+ if ( elTracks [ j ] === techTracks [ i ] ) {
295+ found = true ;
296+ break ;
288297 }
289298 }
290299
291- while ( removeTracks . length ) {
292- techTracks . removeTrack ( removeTracks . shift ( ) ) ;
300+ if ( ! found ) {
301+ removeTracks . push ( techTracks [ i ] ) ;
293302 }
294- } ;
303+ }
295304
296- this [ props . getterName + 'Listeners_' ] = listeners ;
305+ while ( removeTracks . length ) {
306+ techTracks . removeTrack ( removeTracks . shift ( ) ) ;
307+ }
308+ } ;
297309
298- Object . keys ( listeners ) . forEach ( ( eventName ) => {
299- const listener = listeners [ eventName ] ;
310+ this [ props . getterName + 'Listeners_' ] = listeners ;
300311
301- elTracks . addEventListener ( eventName , listener ) ;
302- this . on ( 'dispose' , ( e ) => elTracks . removeEventListener ( eventName , listener ) ) ;
303- } ) ;
312+ Object . keys ( listeners ) . forEach ( ( eventName ) => {
313+ const listener = listeners [ eventName ] ;
304314
305- // Remove (native) tracks that are not used anymore
306- this . on ( 'loadstart' , removeOldTracks ) ;
307- this . on ( 'dispose' , ( e ) => this . off ( 'loadstart' , removeOldTracks ) ) ;
315+ elTracks . addEventListener ( eventName , listener ) ;
316+ this . on ( 'dispose' , ( e ) => elTracks . removeEventListener ( eventName , listener ) ) ;
308317 } ) ;
309318
319+ // Remove (native) tracks that are not used anymore
320+ this . on ( 'loadstart' , removeOldTracks ) ;
321+ this . on ( 'dispose' , ( e ) => this . off ( 'loadstart' , removeOldTracks ) ) ;
322+ }
323+
324+ /**
325+ * Proxy all native track list events to our track lists if the browser we are playing
326+ * in supports that type of track list.
327+ *
328+ * @private
329+ */
330+ proxyNativeTracks_ ( ) {
331+ TRACK_TYPES . names . forEach ( ( name ) => {
332+ this . proxyNativeTracksForType_ ( name ) ;
333+ } ) ;
310334 }
311335
312336 /**
0 commit comments