Skip to content

Commit

Permalink
Store and restore text tracks from JSON.
Browse files Browse the repository at this point in the history
When switching techs, we want to clear out the old text tracks and then
restore them using the currently appropriate tracks.
  • Loading branch information
gkatsev committed Jul 21, 2015
1 parent 0268fc6 commit f198faa
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import globalOptions from './global-options.js';
import safeParseTuple from 'safe-json-parse/tuple';
import assign from 'object.assign';
import mergeOptions from './utils/merge-options.js';
import textTrackConverter from './tracks/text-track-list-converter.js';

// Include required child components (importing also registers them)
import MediaLoader from './tech/loader.js';
Expand Down Expand Up @@ -522,6 +523,8 @@ class Player extends Component {
let techComponent = Component.getComponent(techName);
this.tech = new techComponent(techOptions);

textTrackConverter.jsonToTextTracks(this.textTracksJson_ || [], this.tech);

this.on(this.tech, 'ready', this.handleTechReady);
this.on(this.tech, 'usenativecontrols', this.handleTechUseNativeControls);

Expand Down Expand Up @@ -580,6 +583,7 @@ class Player extends Component {
unloadTech() {
// Save the current text tracks so that we can reuse the same text tracks with the next tech
this.textTracks_ = this.textTracks();
this.textTracksJson_ = textTrackConverter.textTracksToJson(this);

this.isReady_ = false;

Expand Down
8 changes: 8 additions & 0 deletions src/js/tech/tech.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ class Tech extends Component {
* @method dispose
*/
dispose() {
// clear out text tracks because we can't reuse them between techs
let tt = this.textTracks();
let i = tt.length;
while(i--) {
this.removeRemoteTextTrack(tt[i]);
}


// Turn off any manual progress or timeupdate tracking
if (this.manualProgress) { this.manualProgressOff(); }

Expand Down
47 changes: 47 additions & 0 deletions src/js/tracks/text-track-list-converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
let ttToJson = function(track) {
return {
kind: track.kind,
label: track.label,
language: track.language,
id: track.id,
inBandMetadataTrackDispatchType: track.inBandMetadataTrackDispatchType,
mode: track.mode,
cues: track.cues && Array.prototype.map.call(track.cues, function(cue) {
return {
startTime: cue.startTime,
endTime: cue.endTime,
text: cue.text,
id: cue.id
};
}),
src: track.src
};
};

let textTracksToJson = function(tech) {
let trackEls = tech.el().querySelectorAll('track');

let trackObjs = Array.prototype.map.call(trackEls, (t) => t.track);
let tracks = Array.prototype.map.call(trackEls, function(trackEl) {
let json = ttToJson(trackEl.track);
json.src = trackEl.src;
return json;
});

return tracks.concat(Array.prototype.filter.call(tech.textTracks(), function(track) {
return trackObjs.indexOf(track) === -1;
}).map(ttToJson));
};

let jsonToTextTracks = function(json, tech) {
json.forEach(function(track) {
let addedTrack = tech.addRemoteTextTrack(track).track;
if (!track.src) {
track.cues.forEach((cue) => addedTrack.addCue(cue));
}
});

return tech.textTracks();
};

export default {textTracksToJson, jsonToTextTracks};
1 change: 1 addition & 0 deletions src/js/tracks/text-track.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ let TextTrack = function(options={}) {
});

if (options.src) {
tt.src = options.src;
loadTrack(options.src, tt);
} else {
tt.loaded_ = true;
Expand Down

0 comments on commit f198faa

Please sign in to comment.