9c0cac0e by David LaPalomento

Invert conditional to remove a level of indentation

The segment response callback was getting pretty deeply nested, so invert the readyState check and return early. Also added some comments.
1 parent 225eb805
...@@ -292,11 +292,10 @@ var ...@@ -292,11 +292,10 @@ var
292 // always start playback with the default rendition 292 // always start playback with the default rendition
293 if (!player.hls.media) { 293 if (!player.hls.media) {
294 player.hls.media = player.hls.master.playlists[0]; 294 player.hls.media = player.hls.master.playlists[0];
295 if (parser.manifest.totalDuration) { 295
296 // update the duration 296 // update the duration
297 if (parser.manifest.totalDuration) {
297 player.duration(parser.manifest.totalDuration); 298 player.duration(parser.manifest.totalDuration);
298 // Notify the flash layer
299 //player.el().querySelector('.vjs-tech').vjs_setProperty('duration',parser.manifest.totalDuration);
300 } else { 299 } else {
301 player.duration(totalDuration(parser.manifest)); 300 player.duration(totalDuration(parser.manifest));
302 } 301 }
...@@ -311,8 +310,9 @@ var ...@@ -311,8 +310,9 @@ var
311 downloadPlaylist(resolveUrl(srcUrl, playlist.uri)); 310 downloadPlaylist(resolveUrl(srcUrl, playlist.uri));
312 } else { 311 } else {
313 player.hls.media = playlist; 312 player.hls.media = playlist;
314 if (player.hls.media.totalDuration) { 313
315 // update the duration 314 // update the duration
315 if (player.hls.media.totalDuration) {
316 player.duration(player.hls.media.totalDuration); 316 player.duration(player.hls.media.totalDuration);
317 } else { 317 } else {
318 player.duration(totalDuration(player.hls.media)); 318 player.duration(totalDuration(player.hls.media));
...@@ -370,14 +370,8 @@ var ...@@ -370,14 +370,8 @@ var
370 segmentXhr.onreadystatechange = function() { 370 segmentXhr.onreadystatechange = function() {
371 var playlist; 371 var playlist;
372 372
373 if (this.readyState === 4) { 373 // wait until the request completes
374 if (this.status >= 400) { 374 if (this.readyState !== 4) {
375 player.hls.error = {
376 status: segmentXhr.status,
377 message: 'HLS segment request error at URL: ' + segmentUri,
378 code: (segmentXhr.status >= 500) ? 4 : 2
379 };
380 player.trigger('error');
381 return; 375 return;
382 } 376 }
383 377
...@@ -389,6 +383,17 @@ var ...@@ -389,6 +383,17 @@ var
389 return; 383 return;
390 } 384 }
391 385
386 // trigger an error if the request was not successful
387 if (this.status >= 400) {
388 player.hls.error = {
389 status: segmentXhr.status,
390 message: 'HLS segment request error at URL: ' + segmentUri,
391 code: (segmentXhr.status >= 500) ? 4 : 2
392 };
393 player.trigger('error');
394 return;
395 }
396
392 // calculate the download bandwidth 397 // calculate the download bandwidth
393 player.hls.segmentXhrTime = (+new Date()) - startTime; 398 player.hls.segmentXhrTime = (+new Date()) - startTime;
394 player.hls.bandwidth = (this.response.byteLength / player.hls.segmentXhrTime) * 8 * 1000; 399 player.hls.bandwidth = (this.response.byteLength / player.hls.segmentXhrTime) * 8 * 1000;
...@@ -415,7 +420,6 @@ var ...@@ -415,7 +420,6 @@ var
415 player.hls.mediaIndex++; 420 player.hls.mediaIndex++;
416 421
417 if (player.hls.mediaIndex === player.hls.media.segments.length) { 422 if (player.hls.mediaIndex === player.hls.media.segments.length) {
418 //TODO - Fix the endofstream //
419 mediaSource.endOfStream(); 423 mediaSource.endOfStream();
420 return; 424 return;
421 } 425 }
...@@ -428,7 +432,6 @@ var ...@@ -428,7 +432,6 @@ var
428 } else { 432 } else {
429 player.hls.media = playlist; 433 player.hls.media = playlist;
430 } 434 }
431 }
432 }; 435 };
433 startTime = +new Date(); 436 startTime = +new Date();
434 segmentXhr.send(null); 437 segmentXhr.send(null);
......