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
296 // update the duration
295 if (parser.manifest.totalDuration) { 297 if (parser.manifest.totalDuration) {
296 // update the duration
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;
313
314 // update the duration
314 if (player.hls.media.totalDuration) { 315 if (player.hls.media.totalDuration) {
315 // update the duration
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,64 +370,67 @@ var ...@@ -370,64 +370,67 @@ 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 = { 375 return;
376 status: segmentXhr.status, 376 }
377 message: 'HLS segment request error at URL: ' + segmentUri, 377
378 code: (segmentXhr.status >= 500) ? 4 : 2 378 // the segment request is no longer outstanding
379 }; 379 segmentXhr = null;
380 player.trigger('error');
381 return;
382 }
383
384 // the segment request is no longer outstanding
385 segmentXhr = null;
386
387 // stop processing if the request was aborted
388 if (!this.response) {
389 return;
390 }
391 380
392 // calculate the download bandwidth 381 // stop processing if the request was aborted
393 player.hls.segmentXhrTime = (+new Date()) - startTime; 382 if (!this.response) {
394 player.hls.bandwidth = (this.response.byteLength / player.hls.segmentXhrTime) * 8 * 1000; 383 return;
384 }
395 385
396 // transmux the segment data from MP2T to FLV 386 // trigger an error if the request was not successful
397 segmentParser.parseSegmentBinaryData(new Uint8Array(this.response)); 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 }
398 396
399 // handle intra-segment seeking, if requested // 397 // calculate the download bandwidth
400 if (offset !== undefined && typeof offset === "number") { 398 player.hls.segmentXhrTime = (+new Date()) - startTime;
401 player.el().querySelector('.vjs-tech').vjs_setProperty('lastSeekedTime', player.hls.getPtsByTime(segmentParser,offset)/1000); 399 player.hls.bandwidth = (this.response.byteLength / player.hls.segmentXhrTime) * 8 * 1000;
402 for (tagIndex = 0; tagIndex < segmentParser.getTags().length; tagIndex++) { 400
403 if (segmentParser.getTags()[tagIndex].pts > offset) { 401 // transmux the segment data from MP2T to FLV
404 break; 402 segmentParser.parseSegmentBinaryData(new Uint8Array(this.response));
405 } 403
406 // we're seeking past this tag, so ignore it 404 // handle intra-segment seeking, if requested //
407 segmentParser.getNextTag(); 405 if (offset !== undefined && typeof offset === "number") {
406 player.el().querySelector('.vjs-tech').vjs_setProperty('lastSeekedTime', player.hls.getPtsByTime(segmentParser,offset)/1000);
407 for (tagIndex = 0; tagIndex < segmentParser.getTags().length; tagIndex++) {
408 if (segmentParser.getTags()[tagIndex].pts > offset) {
409 break;
408 } 410 }
411 // we're seeking past this tag, so ignore it
412 segmentParser.getNextTag();
409 } 413 }
414 }
410 415
411 while (segmentParser.tagsAvailable()) { 416 while (segmentParser.tagsAvailable()) {
412 player.hls.sourceBuffer.appendBuffer(segmentParser.getNextTag().bytes, player); 417 player.hls.sourceBuffer.appendBuffer(segmentParser.getNextTag().bytes, player);
413 } 418 }
414 419
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 // 423 mediaSource.endOfStream();
419 mediaSource.endOfStream(); 424 return;
420 return; 425 }
421 }
422 426
423 // figure out what stream the next segment should be downloaded from 427 // figure out what stream the next segment should be downloaded from
424 // with the updated bandwidth information 428 // with the updated bandwidth information
425 playlist = player.hls.selectPlaylist(); 429 playlist = player.hls.selectPlaylist();
426 if (!playlist.segments) { 430 if (!playlist.segments) {
427 downloadPlaylist(resolveUrl(srcUrl, playlist.uri)); 431 downloadPlaylist(resolveUrl(srcUrl, playlist.uri));
428 } else { 432 } else {
429 player.hls.media = playlist; 433 player.hls.media = playlist;
430 }
431 } 434 }
432 }; 435 };
433 startTime = +new Date(); 436 startTime = +new Date();
......