@@ -14,6 +14,7 @@ import {
14
14
alignMediaPlaylistByPDT ,
15
15
} from '../utils/discontinuities' ;
16
16
import { mediaAttributesIdentical } from '../utils/media-option-attributes' ;
17
+ import { useAlternateAudio } from '../utils/rendition-helper' ;
17
18
import type { FragmentTracker } from './fragment-tracker' ;
18
19
import type Hls from '../hls' ;
19
20
import type { Fragment , MediaFragment , Part } from '../loader/fragment' ;
@@ -519,7 +520,7 @@ class AudioStreamController
519
520
const cachedTrackLoadedData = this . cachedTrackLoadedData ;
520
521
if ( cachedTrackLoadedData ) {
521
522
this . cachedTrackLoadedData = null ;
522
- this . hls . trigger ( Events . AUDIO_TRACK_LOADED , cachedTrackLoadedData ) ;
523
+ this . onAudioTrackLoaded ( Events . AUDIO_TRACK_LOADED , cachedTrackLoadedData ) ;
523
524
}
524
525
}
525
526
@@ -528,25 +529,28 @@ class AudioStreamController
528
529
data : TrackLoadedData ,
529
530
) {
530
531
const { levels } = this ;
531
- const { details : newDetails , id : trackId } = data ;
532
+ const { details : newDetails , id : trackId , groupId, track } = data ;
533
+ if ( ! levels ) {
534
+ this . warn (
535
+ `Audio tracks reset while loading track ${ trackId } "${ track . name } " of "${ groupId } "` ,
536
+ ) ;
537
+ return ;
538
+ }
532
539
const mainDetails = this . mainDetails ;
533
540
if (
534
541
! mainDetails ||
535
- mainDetails . expired ||
536
- newDetails . endCC > mainDetails . endCC
542
+ newDetails . endCC > mainDetails . endCC ||
543
+ mainDetails . expired
537
544
) {
538
545
this . cachedTrackLoadedData = data ;
539
546
if ( this . state !== State . STOPPED ) {
540
547
this . state = State . WAITING_TRACK ;
541
548
}
542
549
return ;
543
550
}
544
- if ( ! levels ) {
545
- this . warn ( `Audio tracks were reset while loading level ${ trackId } ` ) ;
546
- return ;
547
- }
551
+ this . cachedTrackLoadedData = null ;
548
552
this . log (
549
- `Audio track ${ trackId } loaded [${ newDetails . startSN } ,${
553
+ `Audio track ${ trackId } " ${ track . name } " of " ${ groupId } " loaded [${ newDetails . startSN } ,${
550
554
newDetails . endSN
551
555
} ]${
552
556
newDetails . lastPartSn
@@ -555,18 +559,18 @@ class AudioStreamController
555
559
} ,duration:${ newDetails . totalduration } `,
556
560
) ;
557
561
558
- const track = levels [ trackId ] ;
562
+ const trackLevel = levels [ trackId ] ;
559
563
let sliding = 0 ;
560
- if ( newDetails . live || track . details ?. live ) {
564
+ if ( newDetails . live || trackLevel . details ?. live ) {
561
565
this . checkLiveUpdate ( newDetails ) ;
562
566
if ( newDetails . deltaUpdateFailed ) {
563
567
return ;
564
568
}
565
569
566
- if ( track . details ) {
570
+ if ( trackLevel . details ) {
567
571
sliding = this . alignPlaylists (
568
572
newDetails ,
569
- track . details ,
573
+ trackLevel . details ,
570
574
this . levelLastLoaded ?. details ,
571
575
) ;
572
576
}
@@ -580,8 +584,8 @@ class AudioStreamController
580
584
sliding = newDetails . fragmentStart ;
581
585
}
582
586
}
583
- track . details = newDetails ;
584
- this . levelLastLoaded = track ;
587
+ trackLevel . details = newDetails ;
588
+ this . levelLastLoaded = trackLevel ;
585
589
586
590
// compute start position if we are aligned with the main playlist
587
591
if ( ! this . startFragRequested ) {
@@ -1026,9 +1030,14 @@ class AudioStreamController
1026
1030
bufferedTrack . name !== switchingTrack . name ||
1027
1031
bufferedTrack . lang !== switchingTrack . lang )
1028
1032
) {
1029
- this . log ( 'Switching audio track : flushing all audio' ) ;
1030
- super . flushMainBuffer ( 0 , Number . POSITIVE_INFINITY , 'audio' ) ;
1031
- this . bufferedTrack = null ;
1033
+ if ( useAlternateAudio ( switchingTrack . url , this . hls ) ) {
1034
+ this . log ( 'Switching audio track : flushing all audio' ) ;
1035
+ super . flushMainBuffer ( 0 , Number . POSITIVE_INFINITY , 'audio' ) ;
1036
+ this . bufferedTrack = null ;
1037
+ } else {
1038
+ // Main is being buffered. Set bufferedTrack so that it is flushed when switching back to alt-audio
1039
+ this . bufferedTrack = switchingTrack ;
1040
+ }
1032
1041
}
1033
1042
}
1034
1043
0 commit comments