Remove unused tracks from track list when changing source #3002

Closed
wants to merge 4 commits into from

3 participants

@nickygerritsen

This should fix #3000 . When using native text tracks it removes unused ones on loadstart.

@gkatsev suggested using inbandmetadatatrack but that seems to be "" for my HLS subtitle tracks so I guess we can not use it.

@gkatsev gkatsev and 1 other commented on an outdated diff Feb 3, 2016
src/js/tech/html5.js
@@ -282,6 +293,33 @@ class Html5 extends Tech {
this.textTracks().removeTrack_(e.track);
}
+ removeOldTextTracks() {
@gkatsev
Video.js member
gkatsev added a note Feb 3, 2016

Make sure you consistently call this removeOldTextTracks_.

Hmmm I kinda copied this from the handle... functions. Why are they not postfixed with _ but this one should be? As far as I know they actually should all be underscored?

@gkatsev
Video.js member
gkatsev added a note May 3, 2016

Oh I see, I must've missed that, I guess carry on...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gkatsev gkatsev and 1 other commented on an outdated diff Feb 3, 2016
src/js/tech/html5.js
@@ -74,6 +74,7 @@ class Html5 extends Tech {
this.handleTextTrackChange_ = Fn.bind(this, this.handleTextTrackChange);
this.handleTextTrackAdd_ = Fn.bind(this, this.handleTextTrackAdd);
this.handleTextTrackRemove_ = Fn.bind(this, this.handleTextTrackRemove);
+ this.removeOldTextTracks_ = Fn.bind(this, this.removeOldTextTracks);
@gkatsev
Video.js member
gkatsev added a note Feb 3, 2016

missing the _.

Why is this Fn.bind dance here anyway btw? I'm wondering if it is needed for removeOldTextTracks_?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gkatsev gkatsev commented on the diff Feb 3, 2016
src/js/tech/html5.js
+ let techTrack = this.textTracks()[i];
+
+ let found = false;
+ for (let j = 0; j < this.el().textTracks.length; j++) {
+ if (this.el().textTracks[j] === techTrack) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ removeTracks.push(techTrack);
+ }
+ }
+
+ for (let i = 0; i < removeTracks.length; i++) {
@gkatsev
Video.js member
gkatsev added a note Feb 3, 2016
removeTracks.forEach((track) => this.textTracks().removeTrack_(track))

But really doesn't have to change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gkatsev gkatsev and 1 other commented on an outdated diff Feb 3, 2016
src/js/tech/html5.js
@@ -282,6 +293,33 @@ class Html5 extends Tech {
this.textTracks().removeTrack_(e.track);
}
+ removeOldTextTracks() {
+ // This will loop over the texttracks and check if they are still used
+ // If not, they will be removed from the emulated list
+ let removeTracks = [];
+
+ for (let i = 0; i < this.textTracks().length; i++) {
+ let techTrack = this.textTracks()[i];
@gkatsev
Video.js member
gkatsev added a note Feb 3, 2016

we probably should cache this.textTracks() in a variable outside the forloops so we don't have to make the look up each time.

You mean like

const textTracks = this.textTracks();

?

@gkatsev
Video.js member
gkatsev added a note May 3, 2016

yep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gkatsev gkatsev and 1 other commented on an outdated diff Feb 3, 2016
src/js/tech/html5.js
@@ -282,6 +293,33 @@ class Html5 extends Tech {
this.textTracks().removeTrack_(e.track);
}
+ removeOldTextTracks() {
+ // This will loop over the texttracks and check if they are still used
+ // If not, they will be removed from the emulated list
+ let removeTracks = [];
+
+ for (let i = 0; i < this.textTracks().length; i++) {
+ let techTrack = this.textTracks()[i];
+
+ let found = false;
+ for (let j = 0; j < this.el().textTracks.length; j++) {
+ if (this.el().textTracks[j] === techTrack) {
@gkatsev
Video.js member
gkatsev added a note Feb 3, 2016

we should also probably cache a reference to the native text track and possibly also do a nullcheck around it as well. Since it is conceivably possible to have an html5 browser that doesn't have text track support.

And with this you mean caching this.el().textTracks in some variable / constant and check if this.el().textTracks is null?

@gkatsev
Video.js member
gkatsev added a note May 3, 2016

yep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gkatsev gkatsev and 1 other commented on an outdated diff Feb 3, 2016
src/js/tech/html5.js
@@ -282,6 +293,33 @@ class Html5 extends Tech {
this.textTracks().removeTrack_(e.track);
}
+ removeOldTextTracks() {
+ // This will loop over the texttracks and check if they are still used
+ // If not, they will be removed from the emulated list
+ let removeTracks = [];
+
+ for (let i = 0; i < this.textTracks().length; i++) {
@gkatsev
Video.js member
gkatsev added a note Feb 3, 2016
let tt = this.textTracks();
let trackIndexOf = Array.prototype.indexOf.call.bind(this.el().textTracks);
Array.prototype.filter.call(tt,
                            (ourTrack) => trackIndexOf(ourTracks) === -1)
               .forEach((trackToRemove) => tt.removeTrack_(trackToRemove));

Why? Just felt like it. No change necessary.

I kinda do like the other short version, but this one isn't really readable anymore 😋

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gkatsev
Video.js member
gkatsev commented Apr 29, 2016

This needs to be rebased plus there's a few minor comments that need to be addressed.

@nickygerritsen

Totally, completely forgot about this PR. I'll add some replies above so we can tackle this one.

nickygerritsen added some commits Jan 14, 2016
@nickygerritsen nickygerritsen Remove unused tracks from track list when changing source. fixes #3000
fc91497
@nickygerritsen nickygerritsen Minor changes to structure for removing text tracks 283d462
@nickygerritsen nickygerritsen Little nicer code-flow
1a7e0a5
@nickygerritsen

I assume we do not have to remove video/audio tracks, as these are never native?

@gkatsev
Video.js member
gkatsev commented May 4, 2016

I think we might need to since we do listen to the addtrack on the native audio and video tracks as well. Thoughts, @brandonocasey?

@BrandonOCasey

I think Safari has support for native audio and video tracks so I think we will want to.

@nickygerritsen

OK then I'll try to mimic your way of doing this for all three track kinds.

@nickygerritsen nickygerritsen Also remove audio/video tracks
9401542
@nickygerritsen

OK I think this does the trick. I removed all the Fn.bind goodness, as Events.on already does this. this also gets rid of a "strange" dance with the underscored and non-underscored version of the function.

@gkatsev
Video.js member
gkatsev commented May 11, 2016

LGTM

@gkatsev gkatsev added the confirmed label May 11, 2016
@gkatsev gkatsev closed this in 9f37a64 Jun 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment