Arduino A2DP
Loading...
Searching...
No Matches
Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
A2DPEncoderAAC Class Reference

AAC-LC encode support for the managed A2DP encoder framework: wraps any audio_tools::AudioEncoder that produces AAC-LC - e.g. audio_tools::AACEncoderFDK (codec-fdk-aac) or AACEncoderVO (codec-vo-aacenc) - and advertises the AAC SEP capability. Construct and configure (e.g. bitrate) your encoder of choice, include its codec header yourself, and register an A2DPEncoderAAC wrapping it via BluetoothA2DPSource::add_encoder(). More...

#include <A2DPEncoderAAC.h>

Inheritance diagram for A2DPEncoderAAC:
A2DPEncoder

Public Member Functions

 A2DPEncoderAAC (audio_tools::AudioEncoder &encoder, uint32_t bitrate=256000)
 
bool begin (const esp_a2d_mcc_t &mcc) override
 
void build_capability (esp_a2d_mcc_t &mcc) override
 Fills in the SEP capability advertised for this codec at registration.
 
esp_a2d_mct_t codec_type () override
 A2DP codec type this encoder handles (e.g. ESP_A2D_MCT_SBC)
 
virtual void end ()
 Closes the wrapped audio_tools encoder.
 
audio_tools::AudioInfo get_audio_info ()
 Negotiated sample rate/channels (the PCM is always provided as stereo)
 
virtual int max_frames_per_packet ()
 Max number of encoded frames that can be combined in one media packet.
 
const char * mime () override
 MIME type of the codec this encoder produces (e.g. "audio/sbc")
 
uint32_t negotiated_bitrate ()
 Bitrate (in bps) negotiated with the sink (0 if unknown)
 
virtual uint16_t payload_header_size ()
 
int sample_rate ()
 Sample rate of the PCM provided by the data callbacks.
 
uint32_t samples_per_frame () override
 
void set_bitrate_callback (void(*callback)(uint32_t bitrate))
 
void set_frame_sink (A2DPFrameSink *sink)
 Defines the target of the encoded frames.
 
void set_sample_rate (int rate)
 
virtual size_t write (const uint8_t *pcm, size_t len)
 

Protected Types

enum  Mode { Undefined , ADTS , Raw }
 

Protected Member Functions

void configure (const esp_a2d_mcc_t &mcc) override
 provides the negotiated bitrate to the bitrate callback
 
void parse_audio_info (const esp_a2d_mcc_t &mcc) override
 
void write_encoded (const uint8_t *data, size_t len) override
 removes the ADTS headers and forwards the raw access units
 

Static Protected Member Functions

static bool is_adts_sync (const uint8_t *data)
 

Protected Attributes

uint32_t bitrate
 
void(* bitrate_callback )(uint32_t bitrate) = nullptr
 
uint32_t bitrate_negotiated = 0
 
std::vector< uint8_t > buffer
 
int channels_cfg = 2
 
FrameOutput frame_output
 
int input_sample_rate = 44100
 
Mode mode = Undefined
 
audio_tools::AudioEncoder * p_encoder
 
A2DPFrameSink * p_sink = nullptr
 
int sample_rate_cfg = 44100
 

Static Protected Attributes

static const size_t kAdtsMinHeaderSize = 7
 

Detailed Description

AAC-LC encode support for the managed A2DP encoder framework: wraps any audio_tools::AudioEncoder that produces AAC-LC - e.g. audio_tools::AACEncoderFDK (codec-fdk-aac) or AACEncoderVO (codec-vo-aacenc) - and advertises the AAC SEP capability. Construct and configure (e.g. bitrate) your encoder of choice, include its codec header yourself, and register an A2DPEncoderAAC wrapping it via BluetoothA2DPSource::add_encoder().

The stack expects raw AAC access units (it adds the LATM framing itself), so if the wrapped encoder produces ADTS, the ADTS headers are removed. Output that does not start with an ADTS sync word is treated as one raw access unit per write.

The sink may negotiate a lower bitrate than the advertised one: register a callback with set_bitrate_callback() to apply the negotiated bitrate to your encoder (e.g. with setBitrate()); it is called before the encoder is started.

NOTE: registering a non-SBC stream endpoint is only supported by the underlying Bluedroid stack on ESP-IDF >= 6.1 (with CONFIG_BT_A2DP_CODEC_AAC_ENABLED=y).

Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ A2DPEncoderAAC()

A2DPEncoderAAC::A2DPEncoderAAC ( audio_tools::AudioEncoder &  encoder,
uint32_t  bitrate = 256000 
)
inline

bitrate: the (max) bitrate that is advertised to the sink: this should match the bitrate configured in the wrapped encoder

Member Function Documentation

◆ begin()

bool A2DPEncoderAAC::begin ( const esp_a2d_mcc_t &  mcc)
inlineoverridevirtual

Opens the wrapped audio_tools encoder for the negotiated SEP configuration: calls parse_audio_info(mcc) and configure(mcc) (implemented per codec), then wires up the frame output and starts the encoder.

Reimplemented from A2DPEncoder.

◆ build_capability()

void A2DPEncoderAAC::build_capability ( esp_a2d_mcc_t &  mcc)
inlineoverridevirtual

Fills in the SEP capability advertised for this codec at registration.

Implements A2DPEncoder.

◆ codec_type()

esp_a2d_mct_t A2DPEncoderAAC::codec_type ( )
inlineoverridevirtual

A2DP codec type this encoder handles (e.g. ESP_A2D_MCT_SBC)

Implements A2DPEncoder.

◆ configure()

void A2DPEncoderAAC::configure ( const esp_a2d_mcc_t &  mcc)
inlineoverrideprotectedvirtual

provides the negotiated bitrate to the bitrate callback

Reimplemented from A2DPEncoder.

◆ max_frames_per_packet()

virtual int A2DPEncoder::max_frames_per_packet ( )
inlinevirtualinherited

Max number of encoded frames that can be combined in one media packet.

Reimplemented in A2DPEncoderSBC.

◆ mime()

const char * A2DPEncoderAAC::mime ( )
inlineoverridevirtual

MIME type of the codec this encoder produces (e.g. "audio/sbc")

Implements A2DPEncoder.

◆ parse_audio_info()

void A2DPEncoderAAC::parse_audio_info ( const esp_a2d_mcc_t &  mcc)
inlineoverrideprotectedvirtual

determines sample_rate/channels from the negotiated AAC configuration (a single bit is set per field once negotiation completes)

Implements A2DPEncoder.

◆ payload_header_size()

virtual uint16_t A2DPEncoder::payload_header_size ( )
inlinevirtualinherited

Bytes of the audio MTU that are needed by the media payload header which is added by the stack (e.g. SBC: 1 byte, AAC: LATM header)

Reimplemented in A2DPEncoderSBC.

◆ samples_per_frame()

uint32_t A2DPEncoderAAC::samples_per_frame ( )
inlineoverridevirtual

Number of samples (per channel) covered by one encoded frame: used for the media packet timestamps

Implements A2DPEncoder.

◆ set_bitrate_callback()

void A2DPEncoderAAC::set_bitrate_callback ( void(*)(uint32_t bitrate)  callback)
inline

Defines a callback which is called with the negotiated bitrate (in bps) before the wrapped encoder is started, so that you can apply it to your encoder

◆ set_sample_rate()

void A2DPEncoder::set_sample_rate ( int  rate)
inlineinherited

Defines the sample rate of the PCM provided by the data callbacks (default 44100). Call before BluetoothA2DPSource::start().

◆ write()

virtual size_t A2DPEncoder::write ( const uint8_t *  pcm,
size_t  len 
)
inlinevirtualinherited

Feeds 16 bit stereo PCM to the wrapped encoder (downmixed to mono if this was negotiated); the encoded frames are forwarded to the frame sink

◆ write_encoded()

void A2DPEncoderAAC::write_encoded ( const uint8_t *  data,
size_t  len 
)
inlineoverrideprotectedvirtual

removes the ADTS headers and forwards the raw access units

Reimplemented from A2DPEncoder.


The documentation for this class was generated from the following file: