03083aa4 by David LaPalomento

Avoid creating new events and formatting fixup

Move the duration update functionality so it happens inline instead of in a custom event. Add a test case. Clean up formatting inconsistencies.
1 parent 4ef1ede5
......@@ -398,12 +398,10 @@
this.manifest.totalDuration = entry.duration;
},
'endlist': function() {
if(this.manifest.totalDuration === 0)
{
var calculatedDuration = 0,
i;
for( i = 0; i < this.manifest.segments.length; i++) {
if(this.manifest.segments[i].duration) {
var i, calculatedDuration = 0;
if (this.manifest.totalDuration === 0) {
for (i = 0; i < this.manifest.segments.length; i++) {
if (this.manifest.segments[i].duration) {
calculatedDuration += this.manifest.segments[i].duration;
} else if (this.manifest.targetDuration > 0) {
calculatedDuration += this.manifest.targetDuration;
......
......@@ -84,7 +84,6 @@ var
srcUrl,
segmentXhr,
onDurationUpdate,
downloadPlaylist,
fillBuffer;
......@@ -155,10 +154,6 @@ var
return bestVariant || sortedPlaylists[0];
};
onDurationUpdate = function(value) {
player.duration(value);
};
/**
* Download an M3U8 and update the current manifest object. If the provided
* URL is a master playlist, the default variant will be downloaded and
......@@ -179,7 +174,6 @@ var
if (xhr.readyState === 4) {
// readystate DONE
parser = new videojs.m3u8.Parser();
parser.on('durationUpdate', onDurationUpdate);
parser.push(xhr.responseText);
// master playlists
......@@ -213,6 +207,10 @@ var
// always start playback with the default rendition
if (!player.hls.media) {
player.hls.media = player.hls.master.playlists[0];
if (parser.manifest.totalDuration) {
// update the duration
player.duration(parser.manifest.totalDuration);
}
player.trigger('loadedmanifest');
player.trigger('loadedmetadata');
return;
......@@ -224,7 +222,12 @@ var
downloadPlaylist(resolveUrl(srcUrl, playlist.uri));
} else {
player.hls.media = playlist;
if (parser.manifest.totalDuration) {
// update the duration
player.duration(parser.manifest.totalDuration);
}
}
player.trigger('loadedmanifest');
}
};
......
{
"allowCache": true,
"totalDuration": 0,
"playlists": [{
"attributes": {
"PROGRAM-ID": 1,
......
......@@ -108,6 +108,22 @@ test('loads the specified manifest URL on init', function() {
strictEqual(player.hls.readyState(), 1, 'the readyState is HAVE_METADATA');
});
test('sets the duration if one is available on the playlist', function() {
var calls = 0;
player.duration = function(value) {
if (value === undefined) {
return 0;
}
calls++;
};
player.hls('manifest/media.m3u8');
videojs.mediaSources[player.currentSrc()].trigger({
type: 'sourceopen'
});
strictEqual(1, calls, 'duration is set');
});
test('starts downloading a segment on loadedmetadata', function() {
player.hls('manifest/media.m3u8');
player.buffered = function() {
......