tree: c97a879abea076ddc58232bdf69c736a558dabf2 [path history] [tgz]
  1. BUILD.gn
  2. README.md
  3. activity_dispatcher.cc
  4. activity_dispatcher.h
  5. activity_dispatcher_unittest.cc
  6. audio_admin.cc
  7. audio_admin.h
  8. audio_admin_unittest.cc
  9. audio_capturer.cc
  10. audio_capturer.h
  11. audio_capturer_unittest.cc
  12. audio_clock.cc
  13. audio_clock.h
  14. audio_clock_coefficients.h
  15. audio_clock_unittest.cc
  16. audio_core_impl.cc
  17. audio_core_impl.h
  18. audio_device.cc
  19. audio_device.h
  20. audio_device_manager.cc
  21. audio_device_manager.h
  22. audio_device_settings.cc
  23. audio_device_settings.h
  24. audio_device_settings_unittest.cc
  25. audio_device_unittest.cc
  26. audio_driver.h
  27. audio_driver_clock_unittest.cc
  28. audio_driver_unittest.cc
  29. audio_driver_v1.cc
  30. audio_driver_v2.cc
  31. audio_impl.h
  32. audio_input.cc
  33. audio_input.h
  34. audio_input_unittest.cc
  35. audio_object.h
  36. audio_output.cc
  37. audio_output.h
  38. audio_output_unittest.cc
  39. audio_policy.h
  40. audio_renderer.cc
  41. audio_renderer.h
  42. audio_renderer_unittest.cc
  43. audio_tuner_impl.cc
  44. audio_tuner_impl.h
  45. audio_tuner_impl_unittest.cc
  46. base_capturer.cc
  47. base_capturer.h
  48. base_renderer.cc
  49. base_renderer.h
  50. cached_readable_stream_buffer.h
  51. capture_packet_queue.cc
  52. capture_packet_queue.h
  53. capture_packet_queue_unittest.cc
  54. context.cc
  55. context.h
  56. device_config.cc
  57. device_config.h
  58. device_config_unittest.cc
  59. device_registry.h
  60. driver_output.cc
  61. driver_output.h
  62. driver_output_unittest.cc
  63. effects_controller_impl.cc
  64. effects_controller_impl.h
  65. effects_stage.cc
  66. effects_stage.h
  67. effects_stage_unittest.cc
  68. link_matrix.cc
  69. link_matrix.h
  70. link_matrix_unittest.cc
  71. loudness_transform.cc
  72. loudness_transform.h
  73. loudness_transform_unittest.cc
  74. main.cc
  75. meta/
  76. mix_stage.cc
  77. mix_stage.h
  78. mix_stage_clock_unittest.cc
  79. mix_stage_unittest.cc
  80. mixer/
  81. output_pipeline.cc
  82. output_pipeline.h
  83. output_pipeline_unittest.cc
  84. packet.cc
  85. packet.h
  86. packet_queue.cc
  87. packet_queue.h
  88. packet_queue_unittest.cc
  89. packet_unittest.cc
  90. pending_flush_token.cc
  91. pending_flush_token.h
  92. pin_executable_memory.cc
  93. pin_executable_memory.h
  94. pin_executable_memory_unittest.cc
  95. pipeline_config.cc
  96. pipeline_config.h
  97. pipeline_config_unittest.cc
  98. plug_detector.cc
  99. plug_detector.h
  100. plug_detector_unittest.cc
  101. policy_loader.cc
  102. policy_loader.h
  103. policy_loader_fuzzer.cc
  104. policy_loader_unittest.cc
  105. policy_loader_unittest_data.h
  106. process_config.cc
  107. process_config.h
  108. process_config_loader.cc
  109. process_config_loader.h
  110. process_config_loader_unittest.cc
  111. process_config_unittest.cc
  112. profile_provider.cc
  113. profile_provider.h
  114. profile_provider_unittest.cc
  115. reporter.cc
  116. reporter.h
  117. reporter_unittest.cc
  118. ring_buffer.cc
  119. ring_buffer.h
  120. ring_buffer_unittest.cc
  121. route_graph.cc
  122. route_graph.h
  123. route_graph_unittest.cc
  124. schema/
  125. stream.h
  126. stream_usage.cc
  127. stream_usage.h
  128. stream_usage_unittest.cc
  129. stream_volume_manager.cc
  130. stream_volume_manager.h
  131. stream_volume_manager_unittest.cc
  132. tap_stage.cc
  133. tap_stage.h
  134. tap_stage_unittest.cc
  135. test/
  136. testing/
  137. thermal_agent.cc
  138. thermal_agent.h
  139. thermal_agent_unittest.cc
  140. thermal_config.h
  141. threading_model.cc
  142. threading_model.h
  143. threading_model_unittest.cc
  144. throttle_output.h
  145. throttle_output_unittest.cc
  146. ultrasound_capturer.cc
  147. ultrasound_capturer.h
  148. ultrasound_factory.cc
  149. ultrasound_factory.h
  150. ultrasound_renderer.cc
  151. ultrasound_renderer.h
  152. usage_gain_reporter_impl.cc
  153. usage_gain_reporter_impl.h
  154. usage_gain_reporter_impl_unittest.cc
  155. usage_reporter_impl.cc
  156. usage_reporter_impl.h
  157. usage_reporter_impl_unittest.cc
  158. usage_settings.cc
  159. usage_settings.h
  160. usage_settings_unittest.cc
  161. utils.cc
  162. utils.h
  163. utils_unittest.cc
  164. versioned_timeline_function.h
  165. versioned_timeline_function_unittest.cc
  166. volume_control.cc
  167. volume_control.h
  168. volume_control_unittest.cc
  169. volume_curve.cc
  170. volume_curve.h
  171. volume_curve_unittest.cc
  172. wakeup_event.cc
  173. wakeup_event.h
  174. wakeup_event_unittest.cc
src/media/audio/audio_core/README.md

Audio Core

This is the core of the audio system. It implements the core FIDL APIs fuchsia.media.AudioCapturer and fuchsia.media.AudioRenderer. At a high level, audio core is organized as follows:

+---------------------+        +---------------------+
|   AudioRenderers    |        |    AudioCapturers   |
| r1   r2   ...   rN  |        | c1   c2   ...   cN  |
+---\---|----------|--+        +--Λ---ΛΛ-------------+
     \  |          |      +-------+  / |
      \ |          |   loopback     /  |
+------VV----------V--+   |    +---/---|-------------+
| o1   o2   ...   oN--+---+    | i1   i2   ...   iN  |
|    AudioOutputs     |        |     AudioInputs     |
+---------------------+        +---------------------+

The relevant types are:

  • AudioRenderers represent channels from applications that want to play audio.
  • AudioCapturers represent channels to applications that want to record audio.
  • AudioOutputs represent hardware outputs (speakers).
  • AudioInputs represent hardware inputs (microphones).

To control output routing, we use an enum called an AudioRenderUsage, which has values like BACKGROUND, MEDIA, COMMUNICATION, etc. We maintain a many-to-one mapping from AudioRenderUsage to AudioOutput, then map AudioRenderers to AudioOutputs based on this type. For example, if two AudioRenderers r1 and r2 are created with AudioRenderUsage MEDIA, they are both routed to the AudioOutput assigned to MEDIA (o2 in the above graph).

Input routing works similarly, using a type called AudioCaptureUsage. Additionally, special “loopback” inputs are routed from AudioOutputs.

Output Pipelines

At each AudioOutput, an OutputPipeline controls the mixing of input streams into a single output stream. This mixing happens in a graph structure that combines MixStage nodes (which mix multiple input streams into a single output) and EffectsStage nodes (which apply a sequence of transformations to an input stream). MixStage nodes also perform basic transformations: source format conversion, rechannelization, sample rate conversion, gain scaling, and accumulation. Gain scaling can be configured at both a per-stream level (per-AudioRenderer) and a per-AudioRenderUsage level (usually corresponding to volume controls on the device).

For example, the following graph illustrates an OutputPipeline created for four renderers, three of which (r1, r2, r3) are mixed first because they share the same AudioRenderUsage.

     r1  r2  r3
     |   |   |
  +--V---V---V---+
  |  m1  m2  m3  |
  |   MixStage   |
  +------|-------+
         |
  +------V-------+
  | EffectsStage |
  | 1. high pass |
  | 2. compress  |
  +---------\----+   r4
             \       |
           +--V------V---+
           |   m1    m2  |
           |   MixStage  |
           +------|------+
                  V
                device

If loopback capability is enabled for the given device, then a specific pipeline stage can be designated as the loopback stage. For example, if certain hardware-specific effects should not be included in the loopback stream, the loopback stream can be injected at an early stage before the final output is sent to the device.

Input Pipelines

There is no complex processing in AudioCapturers other than a simple mixer.

Streams, Frames, Clocks, and Timestamps

We represent audio as PCM-encoded streams. Each stream is a sequence of frames with N samples per frame, where the audio has N channels. Within a stream, individual frames are identified by a frame number, which is a simple index into the stream. Each pipeline stage has zero or more source streams and destinations streams; in a pipeline graph, nodes are transformations and edges are streams.

Each stream has a reference clock which controls how the frame timeline advances relative to real time. An important question is how we translate between frame numbers and timestamps. At any given point in time, two kinds of frame numbers are important:

  • The presentation frame identifies the frame that is currently being played at a speaker or captured at a microphone. If it were possible to hear an individual frame, this would be the frame heard from the speaker at the current time.

  • In output pipelines, the safe write frame identifies the frame that the pipeline is about to push into the next stage of the pipeline (for example, at the driver level, this is the next frame to be pushed into the hardware). This is the streams' last chance to produce this frame; all frames before this point have been pushed to the next pipeline stage. Once the frame passes this point, there is a delay, known as the presentation delay, before the frame is played at the speaker. To illustrate:

                       |<--- delay -->|<-- writable --> ...
    frame timeline: +++++++++++++++++++++++++++++++++++
                       ^              ^
                   presentation   safe write
                     frame          frame
    

    In the above diagram, each + is a frame; frame numbers increase from left to right; the arrows (^) are pointers at a specific time; and the arrows advance from left to right as time increases. Viewed another way, frames move on a conveyor belt from right to left: a frame is first writable, then moves to the delay phase after it passes the safe write pointer, then is finally presented.

  • In input pipelines, the safe read frame serves a similar purpose: it identifies the frame that the stream has just obtained from the prior stage of the pipeline. As above, there may be a delay between when the frame is captured at the microphone and when it becomes safe to read. To illustrate:

                    ... <-- readable -->|<--- delay -->|
    frame timeline:     +++++++++++++++++++++++++++++++++++
                                        ^              ^
                                    safe read     presentation
                                      frame          frame
    

    The above diagram uses the same format as the prior diagram: frame numbers increase from left to right and the arrows advance left to right as time increases; or, viewed another way, frames move on a conveyor belt from right to left as time advances. Frames start “in the air”, then are presented (captured) at the microphone, then enter the delay phase, and finally become readable. Note that frames are presented (captured) before they are safe to read, while in output pipelines, frames must be written before they can be presented (played at a speaker).

We anchor frames to real time using presentation frames. For each stream, we define a translation function ReferencePtsToFrame which translates between presentation timestamps (PTS) and frame numbers:

ReferencePtsToFrame(pts) = presentation frame number at pts

PTS is relative to the stream's reference clock. Different streams may have different clocks, and clocks can drift, but between any two clocks there always exists a linear translation.

Within Audio Core, pipeline stages communicate using frame numbers. When an output stream uses a different frame timeline than the stage's input streams, we can convert between the two frame timelines by hopping from input stream frame number, to input stream PTS (using an inverted ReferencePtsToFrame), to output stream PTS (using a clock-to-clock transformation), to output stream frame number (using ReferencePtsToFrame). When safe read/write frames are important, such as in drivers, we translate to and from presentation frames by adding the presentation delay, as illustrated in the timelines above.

Outside of Audio Core, AudioRenderer clients may define both a “reference clock” and a “media timeline”. Clients play audio by sending a stream of audio packets, where the “StreamPacket.pts” field gives the packet‘s PTS relative to the AudioRenderer’s media timeline, and where the media timeline can be translated to the reference clock timline via the rate passed to SetPtsUnits and the offsets passed to Play.

Debugging Tips

Inspecting final output from the system mixer

In development builds of the Fuchsia OS, the WavWriter can be used to examine the audio streams emitted by the system mixer (of course, this functionality is removed from official production builds where this might pose a media security or overall resource consumption risk).

To enable the WavWriter support in the system audio mixer, change the bool (kWavWriterEnabled) found toward the top of driver_output.cc to ‘true’. The Fuchsia system mixer produces a final mix output stream (potentially multi-channel) for every audio output device found. Thus, enabling the WavWriter will cause an audio file to be created for each output device.

These files are created on the target (Fuchsia) device at location /tmp/r/sys/<pkg>/wav_writer_N.wav, where N is a unique integer for each output and <pkg> is the name of the audio_core package (such as fuchsia.com:audio_core:0#meta:audio_core.cmx). One can copy these files back to the host with: fx scp <ip of fuchsia device>:/tmp/.../wav_writer_*.wav ~/Desktop/ At this time, once audio playback begins on any device, the system audio mixer produces audio for ALL audio output devices (even if no client is playing audio to that device). The wave files for these devices will, naturally, contain silence.

Additionally, at this time Fuchsia continues mixing (once it has started) to output devices indefinitely, even after all clients have closed. This will change in the future. Until then, however, the most effective way to use this tracing feature is to killall audio_core on the target, once playback is complete. (The audio_core process restarts automatically when needed, so this is benign.) The mixer calls UpdateHeader to update the ‘length’ parameter in both RIFF Chunk and WAV header after every successive file write, so the files should be complete and usable even if you kill audio_core during audio playback (which means that Close is never called).