Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Audio Focus Handling | |
| 2 | |
| 3 A MediaSession collects all audio-producing objects in one tab. It is usually | |
| 4 unpleasant when multiple MediaSessions play sound at the same time. Audio focus | |
| 5 handling manages the MediaSessions and mixes them in proper ways. This is part | |
| 6 of the default media session on desktop project. | |
| 7 | |
| 8 [TOC] | |
| 9 | |
| 10 ## Processing model | |
| 11 | |
| 12 ### Audio focus types | |
| 13 | |
| 14 There are "persistent" and "transient" audio focus types. | |
| 15 | |
| 16 * Persistent audios are used for long media playback, and they should not mix | |
| 17 with each other. When they start to play, they should pause all other | |
| 18 playbacks. | |
| 19 * Transient audios are used for short media playback such as a ping for incoming | |
| 20 message. When they start to play, they should play on top of other playbacks | |
| 21 and the other playbacks should duck (have reduced volume). | |
| 22 | |
| 23 ### `MediaSession` | |
| 24 | |
| 25 Audio-producing objects should join `MediaSession` when they want to produce | |
| 26 sound. `MediaSession` has the following states: | |
|
mlamouri (slow - plz ping)
2016/09/30 09:35:22
nit: you have two spaces after the dot and before
Zhiqiang Zhang (Slow)
2016/09/30 10:04:43
Done.
| |
| 27 | |
| 28 * ACTIVE: the `MediaSession` has audio focus and its audio-producing objects can | |
| 29 play. | |
| 30 * SUSPENDED: the MediaSession does not have audio focus. All audio-producing | |
| 31 objects are paused and can be resumed when the session gains audio focus. | |
| 32 * INACTIVE: the MediaSession does not have audio focus, and there is no | |
| 33 audio-producing objects in this `MediaSession`. | |
| 34 | |
| 35 Besides, `MediaSession` has a `DUCKING` flag, which means its managed | |
| 36 audio-producing objects has lowered volume. The flag is orthogonal with | |
| 37 `MediaSession` state. | |
| 38 | |
| 39 ### `AudioFocusManager` | |
| 40 | |
| 41 `AudioFocusManager` is a global instance which manages the state of | |
| 42 `MediaSession`s. | |
|
mlamouri (slow - plz ping)
2016/09/30 09:35:22
Specificy that it's for platforms that do not have
Zhiqiang Zhang (Slow)
2016/09/30 10:04:43
Done.
| |
| 43 | |
| 44 When an audio-producing object wants to play audio, it should join `MediaSession ` | |
| 45 and tell which kind of audio focus type it requires. `MediaSession` will then | |
| 46 request audio focus from `AudioFocusManager`, and will allow the object to play | |
| 47 sound if successful. `AudioFocusManager` will notify other `MediaSession`s if | |
| 48 their states are changed. | |
| 49 | |
| 50 When an audio-producing object stops playing audio, it should be removed from | |
| 51 its `MediaSession`, and `MediaSession` should abandon its audio focus if its | |
| 52 audio-producing objects is empty. `AudioFocusManager` will notify other | |
| 53 `MediaSession`s of state change if necessary. | |
| 54 | |
| 55 ## The algorithm for handling audio focus | |
| 56 | |
| 57 `AudioFocusManager` uses a stack implementation. It keeps track of all | |
| 58 ACTIVE/SUSPENDED `MediaSession`s. When a `MediaSession` requests audio focus, it | |
| 59 will be put at the top of the stack, and will be removed from the stack when it | |
| 60 abandons audio focus. | |
| 61 | |
| 62 The algorithm is as follows: | |
| 63 | |
| 64 * When a `MediaSession` requests audio focus: | |
| 65 | |
| 66 * Remove it from the audio focus stack if it's already there, and place it at | |
| 67 the top of audio focus stack, grant focus to the session and let it play. | |
| 68 * If the session is persistent, suspend all the other sessions on the stack. | |
| 69 * If the session is transient, we should duck any active persistent audio | |
| 70 focus entry if present: | |
| 71 | |
| 72 * If the next top entry is transient, do nothing. | |
| 73 * If the next top entry is persistent, let the next top entry start ducking. | |
| 74 | |
| 75 * When a `MediaSession` abandons audio focus: | |
| 76 | |
| 77 * If the session is not on the top, just remove it from the stack. | |
| 78 * If the session is on the top, remove it from the stack. | |
| 79 | |
| 80 * If the stack becomes empty, do nothing. | |
| 81 * If the next top session is transient, do nothing. | |
| 82 * If the next top session is persistent, stop ducking it. | |
| 83 | |
| 84 ### Handling Pepper | |
| 85 | |
| 86 Pepper is different from media elements since it has a different model. Pepper | |
| 87 cannot be paused, but its volume can be changed. When considering Pepper, the | |
| 88 above algorithm must be modified. | |
| 89 | |
| 90 When Pepper joins `MediaSession`, it should request persistent focus type. When | |
| 91 AudioFocusManager wants to suspend a `MediaSession`, it must check whether the | |
| 92 session has Pepper instance, and if yes, it should duck the session instead. | |
| 93 | |
| 94 Also, whenever a session abandons focus, and the next top session is INACTIVE, | |
| 95 `AudioFocusManager` should find the next session having Pepper and unduck it. | |
| OLD | NEW |