Simplify getMediaIndexForTime
We are no longer storing start information in the segments which allows us to greatly simplify this function
Showing
1 changed file
with
15 additions
and
47 deletions
... | @@ -422,7 +422,6 @@ | ... | @@ -422,7 +422,6 @@ |
422 | i, | 422 | i, |
423 | segment, | 423 | segment, |
424 | originalTime = time, | 424 | originalTime = time, |
425 | targetDuration = this.media_.targetDuration || 10, | ||
426 | numSegments = this.media_.segments.length, | 425 | numSegments = this.media_.segments.length, |
427 | lastSegment = numSegments - 1, | 426 | lastSegment = numSegments - 1, |
428 | startIndex, | 427 | startIndex, |
... | @@ -442,70 +441,36 @@ | ... | @@ -442,70 +441,36 @@ |
442 | 441 | ||
443 | // find segments with known timing information that bound the | 442 | // find segments with known timing information that bound the |
444 | // target time | 443 | // target time |
445 | |||
446 | // Walk backward until we find the first segment with timeline | ||
447 | // information that is earlier than `time` | ||
448 | for (i = lastSegment; i >= 0; i--) { | ||
449 | segment = this.media_.segments[i]; | ||
450 | if (segment.end !== undefined && segment.end <= time) { | ||
451 | startIndex = i + 1; | ||
452 | knownStart = segment.end; | ||
453 | if (startIndex >= numSegments) { | ||
454 | // The last segment claims to end *before* the time we are | ||
455 | // searching for so just return it | ||
456 | return numSegments; | ||
457 | } | ||
458 | break; | ||
459 | } | ||
460 | if (segment.start !== undefined && segment.start <= time) { | ||
461 | if (segment.end !== undefined && segment.end > time) { | ||
462 | // we've found the target segment exactly | ||
463 | return i; | ||
464 | } | ||
465 | startIndex = i; | ||
466 | knownStart = segment.start; | ||
467 | break; | ||
468 | } | ||
469 | } | ||
470 | |||
471 | // Walk forward until we find the first segment with timeline | ||
472 | // information that is greater than `time` | ||
473 | for (i = 0; i < numSegments; i++) { | 444 | for (i = 0; i < numSegments; i++) { |
474 | segment = this.media_.segments[i]; | 445 | segment = this.media_.segments[i]; |
475 | if (segment.start !== undefined && segment.start > time) { | 446 | if (segment.end) { |
476 | endIndex = i - 1; | 447 | if (segment.end > time) { |
477 | knownEnd = segment.start; | ||
478 | if (endIndex < 0) { | ||
479 | // The first segment claims to start *after* the time we are | ||
480 | // searching for so the target segment must no longer be | ||
481 | // available | ||
482 | return -1; | ||
483 | } | ||
484 | break; | ||
485 | } | ||
486 | if (segment.end !== undefined && segment.end > time) { | ||
487 | endIndex = i; | ||
488 | knownEnd = segment.end; | 448 | knownEnd = segment.end; |
449 | endIndex = i; | ||
489 | break; | 450 | break; |
451 | } else { | ||
452 | knownStart = segment.end; | ||
453 | startIndex = i; | ||
454 | } | ||
490 | } | 455 | } |
491 | } | 456 | } |
492 | 457 | ||
493 | // use the bounds we just found and playlist information to | 458 | // use the bounds we just found and playlist information to |
494 | // estimate the segment that contains the time we are looking for | 459 | // estimate the segment that contains the time we are looking for |
495 | |||
496 | if (startIndex !== undefined) { | 460 | if (startIndex !== undefined) { |
497 | // We have a known-start point that is before our desired time so | 461 | // We have a known-start point that is before our desired time so |
498 | // walk from that point forwards | 462 | // walk from that point forwards |
499 | time = time - knownStart; | 463 | time = time - knownStart; |
500 | for (i = startIndex; i < (endIndex || numSegments); i++) { | 464 | for (i = startIndex; i < (endIndex || numSegments); i++) { |
501 | segment = this.media_.segments[i]; | 465 | segment = this.media_.segments[i]; |
502 | time -= segment.duration || targetDuration; | 466 | time -= segment.duration; |
467 | |||
503 | if (time < 0) { | 468 | if (time < 0) { |
504 | return i; | 469 | return i; |
505 | } | 470 | } |
506 | } | 471 | } |
507 | 472 | ||
508 | if (i === endIndex) { | 473 | if (i >= endIndex) { |
509 | // We haven't found a segment but we did hit a known end point | 474 | // We haven't found a segment but we did hit a known end point |
510 | // so fallback to interpolating between the segment index | 475 | // so fallback to interpolating between the segment index |
511 | // based on the known span of the timeline we are dealing with | 476 | // based on the known span of the timeline we are dealing with |
... | @@ -523,7 +488,8 @@ | ... | @@ -523,7 +488,8 @@ |
523 | time = knownEnd - time; | 488 | time = knownEnd - time; |
524 | for (i = endIndex; i >= 0; i--) { | 489 | for (i = endIndex; i >= 0; i--) { |
525 | segment = this.media_.segments[i]; | 490 | segment = this.media_.segments[i]; |
526 | time -= segment.duration || targetDuration; | 491 | time -= segment.duration; |
492 | |||
527 | if (time < 0) { | 493 | if (time < 0) { |
528 | return i; | 494 | return i; |
529 | } | 495 | } |
... | @@ -535,12 +501,14 @@ | ... | @@ -535,12 +501,14 @@ |
535 | // subtracting durations until we find a segment that contains | 501 | // subtracting durations until we find a segment that contains |
536 | // time and return it | 502 | // time and return it |
537 | time = time - this.expired_; | 503 | time = time - this.expired_; |
504 | |||
538 | if (time < 0) { | 505 | if (time < 0) { |
539 | return -1; | 506 | return -1; |
540 | } | 507 | } |
508 | |||
541 | for (i = 0; i < numSegments; i++) { | 509 | for (i = 0; i < numSegments; i++) { |
542 | segment = this.media_.segments[i]; | 510 | segment = this.media_.segments[i]; |
543 | time -= segment.duration || targetDuration; | 511 | time -= segment.duration; |
544 | if (time < 0) { | 512 | if (time < 0) { |
545 | return i; | 513 | return i; |
546 | } | 514 | } | ... | ... |
-
Please register or sign in to post a comment