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 @@ ...@@ -398,12 +398,10 @@
398 this.manifest.totalDuration = entry.duration; 398 this.manifest.totalDuration = entry.duration;
399 }, 399 },
400 'endlist': function() { 400 'endlist': function() {
401 if(this.manifest.totalDuration === 0) 401 var i, calculatedDuration = 0;
402 { 402 if (this.manifest.totalDuration === 0) {
403 var calculatedDuration = 0, 403 for (i = 0; i < this.manifest.segments.length; i++) {
404 i; 404 if (this.manifest.segments[i].duration) {
405 for( i = 0; i < this.manifest.segments.length; i++) {
406 if(this.manifest.segments[i].duration) {
407 calculatedDuration += this.manifest.segments[i].duration; 405 calculatedDuration += this.manifest.segments[i].duration;
408 } else if (this.manifest.targetDuration > 0) { 406 } else if (this.manifest.targetDuration > 0) {
409 calculatedDuration += this.manifest.targetDuration; 407 calculatedDuration += this.manifest.targetDuration;
......
...@@ -84,7 +84,6 @@ var ...@@ -84,7 +84,6 @@ var
84 srcUrl, 84 srcUrl,
85 85
86 segmentXhr, 86 segmentXhr,
87 onDurationUpdate,
88 downloadPlaylist, 87 downloadPlaylist,
89 fillBuffer; 88 fillBuffer;
90 89
...@@ -155,10 +154,6 @@ var ...@@ -155,10 +154,6 @@ var
155 return bestVariant || sortedPlaylists[0]; 154 return bestVariant || sortedPlaylists[0];
156 }; 155 };
157 156
158 onDurationUpdate = function(value) {
159 player.duration(value);
160 };
161
162 /** 157 /**
163 * Download an M3U8 and update the current manifest object. If the provided 158 * Download an M3U8 and update the current manifest object. If the provided
164 * URL is a master playlist, the default variant will be downloaded and 159 * URL is a master playlist, the default variant will be downloaded and
...@@ -179,7 +174,6 @@ var ...@@ -179,7 +174,6 @@ var
179 if (xhr.readyState === 4) { 174 if (xhr.readyState === 4) {
180 // readystate DONE 175 // readystate DONE
181 parser = new videojs.m3u8.Parser(); 176 parser = new videojs.m3u8.Parser();
182 parser.on('durationUpdate', onDurationUpdate);
183 parser.push(xhr.responseText); 177 parser.push(xhr.responseText);
184 178
185 // master playlists 179 // master playlists
...@@ -213,6 +207,10 @@ var ...@@ -213,6 +207,10 @@ var
213 // always start playback with the default rendition 207 // always start playback with the default rendition
214 if (!player.hls.media) { 208 if (!player.hls.media) {
215 player.hls.media = player.hls.master.playlists[0]; 209 player.hls.media = player.hls.master.playlists[0];
210 if (parser.manifest.totalDuration) {
211 // update the duration
212 player.duration(parser.manifest.totalDuration);
213 }
216 player.trigger('loadedmanifest'); 214 player.trigger('loadedmanifest');
217 player.trigger('loadedmetadata'); 215 player.trigger('loadedmetadata');
218 return; 216 return;
...@@ -224,7 +222,12 @@ var ...@@ -224,7 +222,12 @@ var
224 downloadPlaylist(resolveUrl(srcUrl, playlist.uri)); 222 downloadPlaylist(resolveUrl(srcUrl, playlist.uri));
225 } else { 223 } else {
226 player.hls.media = playlist; 224 player.hls.media = playlist;
225 if (parser.manifest.totalDuration) {
226 // update the duration
227 player.duration(parser.manifest.totalDuration);
228 }
227 } 229 }
230
228 player.trigger('loadedmanifest'); 231 player.trigger('loadedmanifest');
229 } 232 }
230 }; 233 };
......
1 { 1 {
2 "allowCache": true, 2 "allowCache": true,
3 "totalDuration": 0,
3 "playlists": [{ 4 "playlists": [{
4 "attributes": { 5 "attributes": {
5 "PROGRAM-ID": 1, 6 "PROGRAM-ID": 1,
......
...@@ -108,6 +108,22 @@ test('loads the specified manifest URL on init', function() { ...@@ -108,6 +108,22 @@ test('loads the specified manifest URL on init', function() {
108 strictEqual(player.hls.readyState(), 1, 'the readyState is HAVE_METADATA'); 108 strictEqual(player.hls.readyState(), 1, 'the readyState is HAVE_METADATA');
109 }); 109 });
110 110
111 test('sets the duration if one is available on the playlist', function() {
112 var calls = 0;
113 player.duration = function(value) {
114 if (value === undefined) {
115 return 0;
116 }
117 calls++;
118 };
119 player.hls('manifest/media.m3u8');
120 videojs.mediaSources[player.currentSrc()].trigger({
121 type: 'sourceopen'
122 });
123
124 strictEqual(1, calls, 'duration is set');
125 });
126
111 test('starts downloading a segment on loadedmetadata', function() { 127 test('starts downloading a segment on loadedmetadata', function() {
112 player.hls('manifest/media.m3u8'); 128 player.hls('manifest/media.m3u8');
113 player.buffered = function() { 129 player.buffered = function() {
......