Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

Add text tracks for closed captions listed in playlist #1096

Merged
merged 3 commits into from
Aug 19, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/master-playlist-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export class MasterPlaylistController extends videojs.EventTarget {

this.audioGroups_ = {};
this.subtitleGroups_ = { groups: {}, tracks: {} };
this.closedCaptionGroups_ = { groups: {}, tracks: {} };

this.mediaSource = new videojs.MediaSource({ mode });
this.audioinfo_ = null;
Expand Down Expand Up @@ -387,6 +388,7 @@ export class MasterPlaylistController extends videojs.EventTarget {
this.setupSubtitles();

this.triggerPresenceUsage_(this.master(), media);
this.fillClosedCaptionTracks_();

try {
this.setupSourceBuffers_();
Expand Down Expand Up @@ -642,6 +644,10 @@ export class MasterPlaylistController extends videojs.EventTarget {
}, ABORT_EARLY_BLACKLIST_SECONDS);
});

this.mainSegmentLoader_.on('reseteverything', () => {
this.tech_.trigger('hls-reset');
});

this.audioSegmentLoader_.on('ended', () => {
this.onEndOfStream();
});
Expand Down Expand Up @@ -806,6 +812,49 @@ export class MasterPlaylistController extends videojs.EventTarget {
// Do not enable a default subtitle track. Wait for user interaction instead.
}

/**
* fill our internal list of Captions Tracks with data from
* the master playlist or use a default
*
* @private
*/
fillClosedCaptionTracks_() {
let master = this.master();
let mediaGroups = master.mediaGroups || {};

for (let mediaGroup in mediaGroups['CLOSED-CAPTIONS']) {
if (!this.closedCaptionGroups_.groups[mediaGroup]) {
this.closedCaptionGroups_.groups[mediaGroup] = [];
}

for (let label in mediaGroups['CLOSED-CAPTIONS'][mediaGroup]) {
let properties = mediaGroups['CLOSED-CAPTIONS'][mediaGroup][label];

// We only support CEA608 captions for now, so ignore anything that
// doesn't use a CCx INSTREAM-ID
if (!properties.instreamId.match(/CC\d/)) {
continue;
}

this.closedCaptionGroups_.groups[mediaGroup].push(
videojs.mergeOptions({ id: label }, properties));

if (typeof this.closedCaptionGroups_.tracks[label] === 'undefined') {
let track = this.tech_.addRemoteTextTrack({
id: properties.instreamId,
kind: 'captions',
enabled: false,
language: properties.language,
label
}, false).track;

this.closedCaptionGroups_.tracks[label] = track;
}
}
}

}

/**
* Call load on our SegmentLoaders
*/
Expand Down
1 change: 1 addition & 0 deletions src/segment-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ export default class SegmentLoader extends videojs.EventTarget {
this.ended_ = false;
this.resetLoader();
this.remove(0, this.duration_());
this.trigger('reseteverything');
}

/**
Expand Down
50 changes: 50 additions & 0 deletions test/master-playlist-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,56 @@ QUnit.test('trigger events when an fMP4 stream is detected', function(assert) {
Hls.Playlist.isFmp4 = isFmp4Copy;
});

QUnit.test('adds only CEA608 closed-caption tracks when a master playlist is loaded', function(assert) {
this.requests.length = 0;
this.player = createPlayer();
this.player.src({
src: 'manifest/master-captions.m3u8',
type: 'application/vnd.apple.mpegurl'
});

// wait for async player.src to complete
this.clock.tick(1);

const masterPlaylistController = this.player.tech_.hls.masterPlaylistController_;

assert.equal(this.player.textTracks().length, 1, 'one text track to start');
assert.equal(this.player.textTracks()[0].label,
'segment-metadata',
'only segment-metadata text track');

// master, contains media groups for captions
this.standardXHRResponse(this.requests.shift());

// we wait for loadedmetadata before setting caption tracks, so we need to wait for a
// media playlist
assert.equal(this.player.textTracks().length, 1, 'only one text track after master');

// media
this.standardXHRResponse(this.requests.shift());

const master = masterPlaylistController.masterPlaylistLoader_.master;
const caps = master.mediaGroups['CLOSED-CAPTIONS'].CCs;
const capsArr = Object.keys(caps).map(key => Object.assign({name: key}, caps[key]));
const addedCaps = masterPlaylistController.closedCaptionGroups_.groups.CCs
.map(cap => Object.assign({name: cap.id}, cap));

assert.equal(capsArr.length, 4, '4 closed-caption tracks defined in playlist');
assert.equal(addedCaps.length, 2, '2 CEA608 tracks added internally');
assert.equal(addedCaps[0].instreamId, 'CC1', 'first 608 track is CC1');
assert.equal(addedCaps[1].instreamId, 'CC3', 'second 608 track is CC3');

const textTracks = this.player.textTracks();

assert.equal(textTracks.length, 3, '2 text tracks were added');
assert.equal(textTracks[1].mode, 'disabled', 'track starts disabled');
assert.equal(textTracks[2].mode, 'disabled', 'track starts disabled');
assert.equal(textTracks[1].id, addedCaps[0].instreamId, 'text track 1\'s id is CC\'s instreamId');
assert.equal(textTracks[2].id, addedCaps[1].instreamId, 'text track 2\'s id is CC\'s instreamId');
assert.equal(textTracks[1].label, addedCaps[0].name, 'text track 1\'s label is CC\'s name');
assert.equal(textTracks[2].label, addedCaps[1].name, 'text track 2\'s label is CC\'s name');
});

QUnit.test('adds subtitle tracks when a media playlist is loaded', function(assert) {
let hlsWebvttEvents = 0;

Expand Down
16 changes: 16 additions & 0 deletions utils/manifest/master-captions.m3u8
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# A simple master playlist with multiple variant streams
#EXTM3U

#EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS,GROUP-ID="CCs",LANGUAGE="en",NAME="English608",DEFAULT=YES,AUTOSELECT=YES,INSTREAM-ID="CC1"
#EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS,GROUP-ID="CCs",LANGUAGE="en",NAME="English708",DEFAULT=YES,AUTOSELECT=YES,INSTREAM-ID="SERVICE1"
#EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS,GROUP-ID="CCs",LANGUAGE="fr",NAME="Français608",DEFAULT=NO,AUTOSELECT=NO,INSTREAM-ID="CC3"
#EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS,GROUP-ID="CCs",LANGUAGE="fr",NAME="Français708",DEFAULT=NO,AUTOSELECT=NO,INSTREAM-ID="SERVICE3"

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=240000,RESOLUTION=396x224,CLOSED-CAPTIONS="CCs"
media.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=40000
media1.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=440000,RESOLUTION=396x224,CLOSED-CAPTIONS="CCs"
media2.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1928000,RESOLUTION=960x540,CLOSED-CAPTIONS="CCs"
media3.m3u8