esp32-a2dp-xq 0.1.0
ESP32 A2DP source with SBC-XQ
Loading...
Searching...
No Matches
a2dp_xq.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
28#pragma once
29
30#include <stdbool.h>
31#include <stddef.h>
32#include <stdint.h>
33
34#include "esp_err.h"
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#define A2DP_XQ_VERSION_MAJOR 0
41#define A2DP_XQ_VERSION_MINOR 1
42#define A2DP_XQ_VERSION_PATCH 0
43#define A2DP_XQ_VERSION_STRING "0.1.0"
44
46#define A2DP_XQ_SAMPLE_RATE 44100u
47
55
56typedef struct {
57 char name[64];
58 char addr[18];
59 int8_t rssi;
60 bool paired;
63
64typedef struct {
66 char device_name[64];
67 char device_addr[18];
68 char codec[16];
69 char mode[24];
70 uint8_t bitpool;
71 uint16_t bitrate_kbps;
72 uint32_t sample_rate;
74
88
95
101
104typedef void (*a2dp_xq_event_cb_t)(const a2dp_xq_event_t *event, void *user);
105typedef void (*a2dp_xq_remote_cb_t)(a2dp_xq_remote_t cmd, int value, void *user);
106
119
120#define A2DP_XQ_CONFIG_DEFAULT() \
121 { \
122 .device_name = "ESP32", .allow_sbc_xq = true, .auto_reconnect = true, .last_device = NULL, \
123 .state_dir = NULL, .event_cb = NULL, .event_user = NULL, .remote_cb = NULL, .remote_user = NULL, \
124 .worker_core = 0, \
125 }
126
129
130// ------------------------------------------------------------------- lifecycle ----
134esp_err_t a2dp_xq_init(const a2dp_xq_config_t *cfg);
136void a2dp_xq_deinit(void);
138
139// ----------------------------------------------------------------------- audio ----
141esp_err_t a2dp_xq_start(void);
143void a2dp_xq_stop(void);
146void a2dp_xq_pause(bool paused);
147
151int32_t a2dp_xq_write_s16(const int16_t *pcm, uint32_t frames, uint32_t timeout_ms);
154int32_t a2dp_xq_write_q31(const int32_t *pcm, uint32_t frames, uint32_t timeout_ms);
156void a2dp_xq_flush(void);
159
160// ---------------------------------------------------------------------- volume ----
166float a2dp_xq_volume_to_db(int v127);
167uint8_t a2dp_xq_volume_from_db(float db);
168
169// ------------------------------------------------------------- devices and link ----
172void a2dp_xq_describe(char *out, size_t out_size);
174void a2dp_xq_scan(bool start);
177uint32_t a2dp_xq_devices(a2dp_xq_device_t *out, uint32_t max);
178esp_err_t a2dp_xq_connect(const char *addr);
181void a2dp_xq_forget(const char *addr);
184esp_err_t a2dp_xq_set_xq_allowed(const char *addr, bool allowed);
185
186typedef struct {
187 uint32_t underruns;
189 uint32_t fifo_frames;
194 uint16_t sink_delay_ms;
197
198#ifdef __cplusplus
199}
200#endif
uint32_t a2dp_xq_buffered_frames(void)
Frames queued and not yet sent.
void a2dp_xq_scan(bool start)
Inquiry for about 10 s; A2DP_XQ_EVENT_DEVICE_FOUND per device (audio devices only).
bool a2dp_xq_is_initialized(void)
esp_err_t a2dp_xq_set_xq_allowed(const char *addr, bool allowed)
Allow or forbid SBC-XQ for one device (kept in state_dir); takes effect on the next connection.
void a2dp_xq_stop(void)
Close the audio path (queued audio is dropped) and suspend the stream.
void a2dp_xq_forget(const char *addr)
Remove the bond and the stored results of a device.
void(* a2dp_xq_remote_cb_t)(a2dp_xq_remote_t cmd, int value, void *user)
Definition a2dp_xq.h:105
bool a2dp_xq_supported(void)
True when this build can negotiate SBC-XQ (ESP-IDF >= 6.0 with the internal SBC encoder).
void a2dp_xq_deinit(void)
Disconnect and release everything (controller disabled, memory returned).
esp_err_t a2dp_xq_start(void)
Open the audio path: the stream to the headphones starts as soon as they are connected.
void a2dp_xq_get_stats(a2dp_xq_stats_t *out)
void a2dp_xq_disconnect(void)
esp_err_t a2dp_xq_init(const a2dp_xq_config_t *cfg)
Start the controller (BR/EDR only, the BLE memory is released), Bluedroid, A2DP source and AVRCP.
esp_err_t a2dp_xq_connect(const char *addr)
void a2dp_xq_pause(bool paused)
Pause keeps the queued audio; after 5 s of pause the stream is suspended so the headphones can sleep ...
float a2dp_xq_volume_to_db(int v127)
AVRCP absolute volume (0..127) <-> dB: 127 = 0 dB, 1..127 span 60 dB in equal steps,...
void a2dp_xq_set_volume_db(float db)
Output volume in dB (<= 0).
a2dp_xq_remote_t
Definition a2dp_xq.h:75
@ A2DP_XQ_REMOTE_LINK_DOWN
audio is started but no headphones are connected and no reconnect is pending (switched off,...
Definition a2dp_xq.h:85
@ A2DP_XQ_REMOTE_NEXT
Definition a2dp_xq.h:80
@ A2DP_XQ_REMOTE_VOL_DOWN
Definition a2dp_xq.h:83
@ A2DP_XQ_REMOTE_PLAY
Definition a2dp_xq.h:77
@ A2DP_XQ_REMOTE_VOL_UP
Definition a2dp_xq.h:82
@ A2DP_XQ_REMOTE_STOP
Definition a2dp_xq.h:79
@ A2DP_XQ_REMOTE_PLAY_PAUSE
Definition a2dp_xq.h:76
@ A2DP_XQ_REMOTE_PREV
Definition a2dp_xq.h:81
@ A2DP_XQ_REMOTE_VOLUME_ABS
value = 0..127 set on the headphones (their own volume changed)
Definition a2dp_xq.h:84
@ A2DP_XQ_REMOTE_PAUSE
Definition a2dp_xq.h:78
a2dp_xq_state_t
Definition a2dp_xq.h:48
@ A2DP_XQ_STATE_SCANNING
discovery running
Definition a2dp_xq.h:51
@ A2DP_XQ_STATE_OFF
not initialized
Definition a2dp_xq.h:49
@ A2DP_XQ_STATE_CONNECTED
Definition a2dp_xq.h:53
@ A2DP_XQ_STATE_CONNECTING
Definition a2dp_xq.h:52
@ A2DP_XQ_STATE_IDLE
no headphones connected
Definition a2dp_xq.h:50
uint8_t a2dp_xq_volume_from_db(float db)
void a2dp_xq_flush(void)
Drop what is queued (seek, skip).
void a2dp_xq_get_status(a2dp_xq_status_t *out)
int32_t a2dp_xq_write_q31(const int32_t *pcm, uint32_t frames, uint32_t timeout_ms)
Same from left-justified 32-bit samples (24-bit audio in the top bits): reduced to 16 bits with TPDF ...
uint32_t a2dp_xq_devices(a2dp_xq_device_t *out, uint32_t max)
Bonded devices and the ones found by the last scan: connected first, then paired, then by signal stre...
void a2dp_xq_describe(char *out, size_t out_size)
"SBC-XQ 452 kbps", "SBC 328 kbps", "" when not connected.
void(* a2dp_xq_event_cb_t)(const a2dp_xq_event_t *event, void *user)
Both callbacks run in the library's worker task, outside its locks: they may call back into the libra...
Definition a2dp_xq.h:104
int32_t a2dp_xq_write_s16(const int16_t *pcm, uint32_t frames, uint32_t timeout_ms)
Queue interleaved stereo frames at A2DP_XQ_SAMPLE_RATE.
a2dp_xq_event_type_t
Definition a2dp_xq.h:89
@ A2DP_XQ_EVENT_CODEC
the stream configuration is known: a2dp_xq_get_status()
Definition a2dp_xq.h:93
@ A2DP_XQ_EVENT_PAIRING_FAILED
event.device.addr
Definition a2dp_xq.h:92
@ A2DP_XQ_EVENT_DEVICE_FOUND
discovery found or named a device: event.device
Definition a2dp_xq.h:91
@ A2DP_XQ_EVENT_STATE
state changed: event.state
Definition a2dp_xq.h:90
Definition a2dp_xq.h:107
const char * last_device
"aa:bb:..." to reconnect to, NULL = most recently used
Definition a2dp_xq.h:111
void * remote_user
Definition a2dp_xq.h:116
int8_t worker_core
core of the worker task (-1 = any)
Definition a2dp_xq.h:117
void * event_user
Definition a2dp_xq.h:114
const char * device_name
our name as the headphones see it
Definition a2dp_xq.h:108
const char * state_dir
per-device memory is kept here (SD card), NULL = RAM only
Definition a2dp_xq.h:112
bool allow_sbc_xq
try Dual Channel bitpool 38 first (ESP-IDF >= 6.0)
Definition a2dp_xq.h:109
bool auto_reconnect
connect to the last device after init and after link loss
Definition a2dp_xq.h:110
a2dp_xq_remote_cb_t remote_cb
AVRCP passthrough and volume from the headphones.
Definition a2dp_xq.h:115
a2dp_xq_event_cb_t event_cb
Definition a2dp_xq.h:113
Definition a2dp_xq.h:56
int8_t rssi
dBm, 0 if unknown
Definition a2dp_xq.h:59
bool paired
Definition a2dp_xq.h:60
bool connected
Definition a2dp_xq.h:61
Definition a2dp_xq.h:96
a2dp_xq_event_type_t type
Definition a2dp_xq.h:97
a2dp_xq_device_t device
Definition a2dp_xq.h:99
a2dp_xq_state_t state
Definition a2dp_xq.h:98
Definition a2dp_xq.h:186
uint16_t sink_delay_ms
delay reported by the headphones (A2DP delay reporting), 0 if none
Definition a2dp_xq.h:194
uint32_t fifo_limit_frames
FIFO size used (~300 ms)
Definition a2dp_xq.h:190
uint32_t underrun_frames
silence frames inserted by those underruns
Definition a2dp_xq.h:188
bool abs_volume
headphones support AVRCP absolute volume
Definition a2dp_xq.h:192
uint8_t remote_volume
last absolute volume reported by the headphones, 0..127
Definition a2dp_xq.h:193
uint32_t underruns
times the encoder found the FIFO empty while audio was expected
Definition a2dp_xq.h:187
uint32_t fifo_frames
frames queued now
Definition a2dp_xq.h:189
bool streaming
A2DP media stream started.
Definition a2dp_xq.h:191
Definition a2dp_xq.h:64
a2dp_xq_state_t state
Definition a2dp_xq.h:65
uint32_t sample_rate
Definition a2dp_xq.h:72
uint8_t bitpool
Definition a2dp_xq.h:70
uint16_t bitrate_kbps
Definition a2dp_xq.h:71