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

Commit

Permalink
Metadata cues may have length zero (#113)
Browse files Browse the repository at this point in the history
* Metadata cues may have length zero
It's not safe to assume that there is at least one element in metadata cues when addTextTrackData() is invoked. Double check the array is there and non-empty before tweaking start and end times. Add tests for that module.

* Fix another bad array length check
Empty arrays are truthy.
  • Loading branch information
dmlap authored Nov 3, 2016
1 parent af0ef10 commit c2b95c5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/add-text-track-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ const addTextTrackData = function(sourceHandler, captionArray, metadataArray) {
}, this);
}, sourceHandler);

/** Updating the metadeta cues so that
* the endTime of each cue is the startTime of the next cue
* the endTime of last cue is the duration of the video
*/
if (sourceHandler.metadataTrack_ && sourceHandler.metadataTrack_.cues) {
// Updating the metadeta cues so that
// the endTime of each cue is the startTime of the next cue
// the endTime of last cue is the duration of the video
if (sourceHandler.metadataTrack_ &&
sourceHandler.metadataTrack_.cues &&
sourceHandler.metadataTrack_.cues.length) {
let cues = sourceHandler.metadataTrack_.cues;
let cuesArray = [];

Expand Down
2 changes: 1 addition & 1 deletion src/html-media-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export default class HtmlMediaSource extends videojs.EventTarget {
let sourcebuffer = this.sourceBuffers[i];
let cues = sourcebuffer.metadataTrack_ && sourcebuffer.metadataTrack_.cues;

if (cues) {
if (cues && cues.length) {
cues[cues.length - 1].endTime = duration;
}
}
Expand Down
51 changes: 51 additions & 0 deletions test/add-text-track-data.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Qunit from 'qunit';
import { addTextTrackData } from '../src/add-text-track-data';

const { equal, module, test } = Qunit;

class MockTextTrack {
constructor() {
this.cues = [];
}
addCue(cue) {
this.cues.push(cue);
}
}

module('Text Track Data', {
beforeEach() {
this.sourceHandler = {
inbandTextTrack_: new MockTextTrack(),
metadataTrack_: new MockTextTrack(),
mediaSource_: {
duration: NaN
},
timestampOffset: 0
};
}
});

test('does nothing if no cues are specified', function() {
addTextTrackData(this.sourceHandler, [], []);
equal(this.sourceHandler.inbandTextTrack_.cues.length, 0, 'added no 608 cues');
equal(this.sourceHandler.metadataTrack_.cues.length, 0, 'added no metadata cues');
});

test('creates cues for 608 captions', function() {
addTextTrackData(this.sourceHandler, [{
startTime: 0,
endTime: 1,
text: 'caption text'
}], []);
equal(this.sourceHandler.inbandTextTrack_.cues.length, 1, 'added one 608 cues');
equal(this.sourceHandler.metadataTrack_.cues.length, 0, 'added no metadata cues');
});

test('creates cues for timed metadata', function() {
addTextTrackData(this.sourceHandler, [], [{
cueTime: 1,
frames: [{}]
}]);
equal(this.sourceHandler.inbandTextTrack_.cues.length, 0, 'added no 608 cues');
equal(this.sourceHandler.metadataTrack_.cues.length, 1, 'added one metadata cues');
});

0 comments on commit c2b95c5

Please sign in to comment.