Skip to content

Commit 8dd330f

Browse files
srikieonlineSrikanth Peddibhotlaavelad
authored
feat: Handle audio focus events (#8427)
In some platforms custom audio focus events are emitted. This needs to be handled to avoid the stall detection. --------- Co-authored-by: Srikanth Peddibhotla <[email protected]> Co-authored-by: Álvaro Velad Galván <[email protected]>
1 parent c867839 commit 8dd330f

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

lib/media/gap_jumping_controller.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ shaka.media.StallDetector = class {
394394
/** @override */
395395
release() {
396396
// Drop external references to make things easier on the GC.
397+
if (this.implementation_) {
398+
this.implementation_.release();
399+
}
397400
this.implementation_ = null;
398401
this.onStall_ = null;
399402
}
@@ -465,6 +468,11 @@ shaka.media.StallDetector.Implementation = class {
465468
* @return {number}
466469
*/
467470
getWallSeconds() {}
471+
472+
/**
473+
* Releases object
474+
*/
475+
release() {}
468476
};
469477

470478

@@ -482,6 +490,34 @@ shaka.media.StallDetector.MediaElementImplementation = class {
482490
constructor(mediaElement) {
483491
/** @private {!HTMLMediaElement} */
484492
this.mediaElement_ = mediaElement;
493+
/**
494+
* For listeners scoped to the lifetime of the instance.
495+
* @private {shaka.util.EventManager}
496+
*/
497+
this.audioEventManager_ = new shaka.util.EventManager();
498+
/**
499+
* Audio focus state
500+
* @private {boolean}
501+
*/
502+
this.audioFocusPaused_ = false;
503+
// Audio Focus temporary leads to playback pause.
504+
this.audioEventManager_.listen(this.mediaElement_,
505+
'audiofocuspaused', () => {
506+
shaka.log.info(`Audio focus paused`);
507+
this.audioFocusPaused_ = true;
508+
});
509+
// Audio Focus is granted. App can play now.
510+
this.audioEventManager_.listen(this.mediaElement_,
511+
'audiofocusgranted', () => {
512+
shaka.log.info(`Audio focus granted`);
513+
this.audioFocusPaused_ = false;
514+
});
515+
// Audio focus is lost, app shouldn't play anymore.
516+
this.audioEventManager_.listen(this.mediaElement_,
517+
'audiofocuslost', () => {
518+
shaka.log.info(`Audio focus lost`);
519+
this.audioFocusPaused_ = true;
520+
});
485521
}
486522

487523
/** @override */
@@ -494,7 +530,10 @@ shaka.media.StallDetector.MediaElementImplementation = class {
494530
if (this.mediaElement_.playbackRate == 0) {
495531
return false;
496532
}
497-
533+
// If playback paused due to no audio focus, we ignore the stall.
534+
if (this.audioFocusPaused_) {
535+
return false;
536+
}
498537
// If we have don't have enough content, we are not stalled, we are
499538
// buffering.
500539
if (this.mediaElement_.buffered.length == 0) {
@@ -542,5 +581,13 @@ shaka.media.StallDetector.MediaElementImplementation = class {
542581

543582
return false;
544583
}
584+
585+
/** @override */
586+
release() {
587+
if (this.audioEventManager_) {
588+
this.audioEventManager_.release();
589+
}
590+
this.audioEventManager_ = null;
591+
}
545592
};
546593

project-words.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# events / html
22
abrstatuschanged
33
adblocker
4+
audiofocuspaused
5+
audiofocusgranted
6+
audiofocuslost
47
audiolang
58
audiotrackchange
69
audiotrackchanged

test/media/stall_detector_unit.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ describe('StallDetector', () => {
2929

3030
/** @override */
3131
getWallSeconds() { return this.wallSeconds; }
32+
33+
/** @override */
34+
release() {}
3235
}
3336

3437
/** @type {!TestImplementation} */

0 commit comments

Comments
 (0)