@@ -11,6 +11,8 @@ import { initSegmentId } from './bin-utils';
1111import { mediaSegmentRequest , REQUEST_ERRORS } from './media-segment-request' ;
1212import { TIME_FUDGE_FACTOR , timeUntilRebuffer as timeUntilRebuffer_ } from './ranges' ;
1313import { minRebufferMaxBandwidthSelector } from './playlist-selectors' ;
14+ import { addCaptionData , createCaptionsTrackIfNotExists } from './util/text-tracks' ;
15+ import { CaptionParser } from 'mux.js/lib/mp4' ;
1416import logger from './util/logger' ;
1517
1618// in ms
@@ -166,6 +168,7 @@ export default class SegmentLoader extends videojs.EventTarget {
166168 this . segmentMetadataTrack_ = settings . segmentMetadataTrack ;
167169 this . goalBufferLength_ = settings . goalBufferLength ;
168170 this . sourceType_ = settings . sourceType ;
171+ this . inbandTextTracks_ = settings . inbandTextTracks ;
169172 this . state_ = 'INIT' ;
170173
171174 // private instance variables
@@ -180,6 +183,8 @@ export default class SegmentLoader extends videojs.EventTarget {
180183 // Fragmented mp4 playback
181184 this . activeInitSegmentId_ = null ;
182185 this . initSegments_ = { } ;
186+ // Fmp4 CaptionParser
187+ this . captionParser_ = new CaptionParser ( ) ;
183188
184189 this . decrypter_ = settings . decrypter ;
185190
@@ -240,6 +245,7 @@ export default class SegmentLoader extends videojs.EventTarget {
240245 this . sourceUpdater_ . dispose ( ) ;
241246 }
242247 this . resetStats_ ( ) ;
248+ this . captionParser_ . reset ( ) ;
243249 }
244250
245251 /**
@@ -340,7 +346,9 @@ export default class SegmentLoader extends videojs.EventTarget {
340346 this . initSegments_ [ id ] = storedMap = {
341347 resolvedUri : map . resolvedUri ,
342348 byterange : map . byterange ,
343- bytes : map . bytes
349+ bytes : map . bytes ,
350+ timescales : map . timescales ,
351+ videoTrackIds : map . videoTrackIds
344352 } ;
345353 }
346354
@@ -544,6 +552,8 @@ export default class SegmentLoader extends videojs.EventTarget {
544552 this . ended_ = false ;
545553 this . resetLoader ( ) ;
546554 this . remove ( 0 , this . duration_ ( ) ) ;
555+ // clears fmp4 captions
556+ this . captionParser_ . clearAllCaptions ( ) ;
547557 this . trigger ( 'reseteverything' ) ;
548558 }
549559
@@ -578,6 +588,12 @@ export default class SegmentLoader extends videojs.EventTarget {
578588 this . sourceUpdater_ . remove ( start , end ) ;
579589 }
580590 removeCuesFromTrack ( start , end , this . segmentMetadataTrack_ ) ;
591+
592+ if ( this . inbandTextTracks_ ) {
593+ for ( let id in this . inbandTextTracks_ ) {
594+ removeCuesFromTrack ( start , end , this . inbandTextTracks_ [ id ] ) ;
595+ }
596+ }
581597 }
582598
583599 /**
@@ -666,11 +682,13 @@ export default class SegmentLoader extends videojs.EventTarget {
666682 // (we are crossing a discontinuity somehow)
667683 // - The "timestampOffset" for the start of this segment is less than
668684 // the currently set timestampOffset
685+ // Also, clear captions if we are crossing a discontinuity boundary
669686 if ( segmentInfo . timeline !== this . currentTimeline_ ||
670687 ( ( segmentInfo . startOfSegment !== null ) &&
671688 segmentInfo . startOfSegment < this . sourceUpdater_ . timestampOffset ( ) ) ) {
672689 this . syncController_ . reset ( ) ;
673690 segmentInfo . timestampOffset = segmentInfo . startOfSegment ;
691+ this . captionParser_ . clearAllCaptions ( ) ;
674692 }
675693
676694 this . loadSegment_ ( segmentInfo ) ;
@@ -952,6 +970,7 @@ export default class SegmentLoader extends videojs.EventTarget {
952970 segmentInfo . abortRequests = mediaSegmentRequest ( this . hls_ . xhr ,
953971 this . xhrOptions_ ,
954972 this . decrypter_ ,
973+ this . captionParser_ ,
955974 this . createSimplifiedSegmentObj_ ( segmentInfo ) ,
956975 // progress callback
957976 this . handleProgress_ . bind ( this ) ,
@@ -1112,6 +1131,24 @@ export default class SegmentLoader extends videojs.EventTarget {
11121131 }
11131132
11141133 segmentInfo . endOfAllRequests = simpleSegment . endOfAllRequests ;
1134+
1135+ // This has fmp4 captions, add them to text tracks
1136+ if ( simpleSegment . fmp4Captions ) {
1137+ createCaptionsTrackIfNotExists (
1138+ this . inbandTextTracks_ ,
1139+ this . hls_ . tech_ ,
1140+ simpleSegment . captionStreams ) ;
1141+ addCaptionData ( {
1142+ inbandTextTracks : this . inbandTextTracks_ ,
1143+ captionArray : simpleSegment . fmp4Captions ,
1144+ // fmp4s will not have a timestamp offset
1145+ timestampOffset : 0
1146+ } ) ;
1147+ // Reset stored captions since we added parsed
1148+ // captions to a text track at this point
1149+ this . captionParser_ . clearParsedCaptions ( ) ;
1150+ }
1151+
11151152 this . handleSegment_ ( ) ;
11161153 }
11171154
0 commit comments