906310ec by David LaPalomento

Don't throw an exception if there are no segments in a media playlist

A poorly operating live stream might publish media playlist before any segments are available. If that happens, keep checking back for updates instead of throwing an exception and dying.
1 parent 07c7021e
...@@ -204,13 +204,8 @@ var ...@@ -204,13 +204,8 @@ var
204 totalDuration = function(playlist) { 204 totalDuration = function(playlist) {
205 var 205 var
206 duration = 0, 206 duration = 0,
207 i, 207 segment,
208 segment; 208 i = (playlist.segments || []).length;
209
210 if (!playlist.segments) {
211 return 0;
212 }
213 i = playlist.segments.length;
214 209
215 // if present, use the duration specified in the playlist 210 // if present, use the duration specified in the playlist
216 if (playlist.totalDuration) { 211 if (playlist.totalDuration) {
...@@ -585,7 +580,7 @@ var ...@@ -585,7 +580,7 @@ var
585 var 580 var
586 buffered = player.buffered(), 581 buffered = player.buffered(),
587 bufferedTime = 0, 582 bufferedTime = 0,
588 segment = player.hls.media.segments[player.hls.mediaIndex], 583 segment,
589 segmentUri, 584 segmentUri,
590 startTime; 585 startTime;
591 586
...@@ -594,7 +589,13 @@ var ...@@ -594,7 +589,13 @@ var
594 return; 589 return;
595 } 590 }
596 591
592 // if no segments are available, do nothing
593 if (!player.hls.media.segments) {
594 return;
595 }
596
597 // if the video has finished downloading, stop trying to buffer 597 // if the video has finished downloading, stop trying to buffer
598 segment = player.hls.media.segments[player.hls.mediaIndex];
598 if (!segment) { 599 if (!segment) {
599 return; 600 return;
600 } 601 }
......
...@@ -1109,4 +1109,28 @@ test('only reloads the active media playlist', function() { ...@@ -1109,4 +1109,28 @@ test('only reloads the active media playlist', function() {
1109 'refreshed the active playlist'); 1109 'refreshed the active playlist');
1110 }); 1110 });
1111 1111
1112 test('does not break if the playlist has no segments', function() {
1113 window.XMLHttpRequest = function () {
1114 this.open = function () {};
1115 this.send = function () {
1116 this.readyState = 4;
1117 this.status = 200;
1118 this.responseText = '#EXTM3U\n' +
1119 '#EXT-X-PLAYLIST-TYPE:VOD\n' +
1120 '#EXT-X-TARGETDURATION:10\n';
1121 this.onreadystatechange();
1122 };
1123 };
1124 player.hls('manifest/master.m3u8');
1125 try {
1126 videojs.mediaSources[player.currentSrc()].trigger({
1127 type: 'sourceopen'
1128 });
1129 } catch(e) {
1130 ok(false, 'an error was thrown');
1131 throw e;
1132 }
1133 ok(true, 'no error was thrown');
1134 });
1135
1112 })(window, window.videojs); 1136 })(window, window.videojs);
......