fuchsia_audio_codec/
lib.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/*!
6
7Provides an asynchronous wrapper around the StreamProcessor API to encode/decode audio packets.
8
9The main object to use is the StreamProcessor with either the `create_encoder` or `create_decoder` methods which are passed the input audio parameters and the
10encoder settings to provide to the StreamProcessor for the encoder case. The `input_domain`
11can be found as `fidl_fuchsia_media::DomainFormat` and it must be one of the Audio formats. The
12`encoder_settings` is found as `fidl_fuchsia_media::EncoderSettings`.
13
14After creating the encoder/decoder, audio input data can be provided either by using the StreamProcessor as an
15`AsyncWrite` provider.  The resulting audio is provided via the output stream which can
16be acquired via the `StreamProcessor::take_output_stream` call.
17
18# Example
19
20```rust
21
22let pcm_format = PcmFormat {
23    pcm_mode: AudioPcmMode::Linear,
24    bits_per_sample: 16,
25    frames_per_second: 44100,
26    channel_map: vec![AudioChannelId::Cf],
27};
28
29// Some sample SBC encoder settings.
30let sbc_encoder_settings = EncoderSettings::Sbc(SbcEncoderSettings {
31    sub_bands,
32    block_count,
33    allocation: SbcAllocation::AllocLoudness,
34    channel_mode: SbcChannelMode::Mono,
35    bit_pool: 59,
36});
37
38
39// Read a WAV file into memory.
40let mut raw_audio = Vec::new();
41File::open("sample.wav")?.read_to_end(&mut raw_audio)?;
42
43let encoder = StreamProcessor::create_encoder(pcm_format, sbc_encoder_settings);
44
45// 44 bytes offset skips the RIFF header in the wav file.
46// Delivering 16 audio frames at once that are 2 bytes each
47for audio_frames in raw_audio[44..].chunks(2 * 16) {
48    encoder.write(&audio_frames).await?;
49}
50
51encoder.close()?;
52
53let mut encoded_stream = encoder.take_encoded_stream();
54
55let mut output_file = File::create("sample.sbc")?;
56
57while let Some(data) = encoded_stream.try_next().await? {
58    output_file.write(&data)?;
59}
60
61output_file.close()?;
62
63```
64
65The output stream will begin producing data as soon as the StreamProcessor starts providing it -
66the writing of the uncompressed data and the reading of the output stream can happen on separate
67tasks.
68
69*/
70
71/// Interface to CodecFactory
72pub mod stream_processor;
73pub use stream_processor::{StreamProcessor, StreamProcessorOutputStream};
74
75mod buffer_collection_constraints;
76pub mod sysmem_allocator;