A2DP Sink - Receive Audio Stream and Control Playback
Source Code: a2dp_sink_demo.c
This A2DP Sink example demonstrates how to use the A2DP Sink service to receive an audio data stream from a remote A2DP Source device. In addition, the AVRCP Controller is used to get information on currently played media, such are title, artist and album, as well as to control the playback, i.e. to play, stop, repeat, etc. If HAVE_BTSTACK_STDIN is set, press SPACE on the console to show the available AVDTP and AVRCP commands.
To test with a remote device, e.g. a mobile phone, pair from the remote device with the demo, then start playing music on the remote device. Alternatively, set the device_addr_string to the Bluetooth address of your remote device in the code, and call connect from the UI.
For more info on BTstack audio, see our blog post A2DP Sink and Source on STM32 F4 Discovery Board.
Main Application Setup
The Listing here shows how to set up AD2P Sink and AVRCP services. Besides calling init() method for each service, you'll also need to register several packet handlers:
- hci_packet_handler - handles pairing and device discovery
- a2dp_sink_packet_handler - handles events on stream connection status (established, released), the media codec configuration, and, the status of the stream itself (opened, paused, stopped).
- handle_l2cap_media_data_packet - used to receive streaming data. If STORE_TO_WAV_FILE directive (check btstack_config.h) is used, the SBC decoder will be used to decode the SBC data into PCM frames. The resulting PCM frames are then processed in the SBC Decoder callback.
- avrcp_packet_handler - receives AVRCP connect/disconnect event.
- avrcp_controller_packet_handler - receives answers for sent AVRCP commands.
- avrcp_target_packet_handler - receives AVRCP commands, and registered notifications.
- stdin_process - used to trigger AVRCP commands to the A2DP Source device, such are get now playing info, start, stop, volume control. Requires HAVE_BTSTACK_STDIN.
To announce A2DP Sink and AVRCP services, you need to create corresponding SDP records and register them with the SDP service.
Note, currently only the SBC codec is supported. If you want to store the audio data in a file, you'll need to define STORE_TO_WAV_FILE. If STORE_TO_WAV_FILE directive is defined, the SBC decoder needs to get initialized when a2dp_sink_packet_handler receives event A2DP_SUBEVENT_STREAM_STARTED. The initialization of the SBC decoder requires a callback that handles PCM data:
- handle_pcm_data - handles PCM audio frames. Here, they are stored in a wav file if STORE_TO_WAV_FILE is defined, and/or played using the audio library.
static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void a2dp_sink_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t event_size);
static void handle_l2cap_media_data_packet(uint8_t seid, uint8_t *packet, uint16_t size);
static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
#ifdef HAVE_BTSTACK_STDIN
static void stdin_process(char cmd);
#endif
static int setup_demo(void){
// init protocols
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/l2cap.h#L477">l2cap_init</a>();
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L72">sdp_init</a>();
#ifdef ENABLE_BLE
// Initialize LE Security Manager. Needed for cross-transport key derivation
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L68">sm_init</a>();
#endif
#ifdef ENABLE_AVRCP_COVER_ART
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/goep_client.h#L125">goep_client_init</a>();
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_cover_art_client.h#L127">avrcp_cover_art_client_init</a>();
#endif
// Init profiles
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/a2dp_sink.h#L70">a2dp_sink_init</a>();
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp.h#L990">avrcp_init</a>();
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_controller.h#L83">avrcp_controller_init</a>();
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_target.h#L80">avrcp_target_init</a>();
// Configure A2DP Sink
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/a2dp_sink.h#L106">a2dp_sink_register_packet_handler</a>(&a2dp_sink_packet_handler);
a2dp_sink_register_media_handler(&handle_l2cap_media_data_packet);
a2dp_sink_demo_stream_endpoint_t * stream_endpoint = &a2dp_sink_demo_stream_endpoint;
avdtp_stream_endpoint_t * local_stream_endpoint = <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/a2dp_sink.h#L83">a2dp_sink_create_stream_endpoint</a>(AVDTP_AUDIO,
AVDTP_CODEC_SBC, media_sbc_codec_capabilities, sizeof(media_sbc_codec_capabilities),
stream_endpoint->media_sbc_codec_configuration, sizeof(stream_endpoint->media_sbc_codec_configuration));
btstack_assert(local_stream_endpoint != NULL);
// - Store stream enpoint's SEP ID, as it is used by A2DP API to identify the stream endpoint
stream_endpoint->a2dp_local_seid = avdtp_local_seid(local_stream_endpoint);
// Configure AVRCP Controller + Target
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp.h#L996">avrcp_register_packet_handler</a>(&avrcp_packet_handler);
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_controller.h#L89">avrcp_controller_register_packet_handler</a>(&avrcp_controller_packet_handler);
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_target.h#L86">avrcp_target_register_packet_handler</a>(&avrcp_target_packet_handler);
// Configure SDP
// - Create and register A2DP Sink service record
memset(sdp_avdtp_sink_service_buffer, 0, sizeof(sdp_avdtp_sink_service_buffer));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/a2dp_sink.h#L65">a2dp_sink_create_sdp_record</a>(sdp_avdtp_sink_service_buffer, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(),
AVDTP_SINK_FEATURE_MASK_HEADPHONE, NULL, NULL);
btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( sdp_avdtp_sink_service_buffer) <= sizeof(sdp_avdtp_sink_service_buffer));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(sdp_avdtp_sink_service_buffer);
// - Create AVRCP Controller service record and register it with SDP. We send Category 1 commands to the media player, e.g. play/pause
memset(sdp_avrcp_controller_service_buffer, 0, sizeof(sdp_avrcp_controller_service_buffer));
uint16_t controller_supported_features = 1 << AVRCP_CONTROLLER_SUPPORTED_FEATURE_CATEGORY_PLAYER_OR_RECORDER;
#ifdef AVRCP_BROWSING_ENABLED
controller_supported_features |= 1 << AVRCP_CONTROLLER_SUPPORTED_FEATURE_BROWSING;
#endif
#ifdef ENABLE_AVRCP_COVER_ART
controller_supported_features |= 1 << AVRCP_CONTROLLER_SUPPORTED_FEATURE_COVER_ART_GET_LINKED_THUMBNAIL;
#endif
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_controller.h#L78">avrcp_controller_create_sdp_record</a>(sdp_avrcp_controller_service_buffer, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(),
controller_supported_features, NULL, NULL);
btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( sdp_avrcp_controller_service_buffer) <= sizeof(sdp_avrcp_controller_service_buffer));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(sdp_avrcp_controller_service_buffer);
// - Create and register A2DP Sink service record
// - We receive Category 2 commands from the media player, e.g. volume up/down
memset(sdp_avrcp_target_service_buffer, 0, sizeof(sdp_avrcp_target_service_buffer));
uint16_t target_supported_features = 1 << AVRCP_TARGET_SUPPORTED_FEATURE_CATEGORY_MONITOR_OR_AMPLIFIER;
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_target.h#L75">avrcp_target_create_sdp_record</a>(sdp_avrcp_target_service_buffer,
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), target_supported_features, NULL, NULL);
btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( sdp_avrcp_target_service_buffer) <= sizeof(sdp_avrcp_target_service_buffer));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(sdp_avrcp_target_service_buffer);
// - Create and register Device ID (PnP) service record
memset(device_id_sdp_service_buffer, 0, sizeof(device_id_sdp_service_buffer));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/device_id_server.h#L65">device_id_create_sdp_record</a>(device_id_sdp_service_buffer,
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), DEVICE_ID_VENDOR_ID_SOURCE_BLUETOOTH, BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, 1, 1);
btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( device_id_sdp_service_buffer) <= sizeof(device_id_sdp_service_buffer));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(device_id_sdp_service_buffer);
// Configure GAP - discovery / connection
// - Set local name with a template Bluetooth address, that will be automatically
// replaced with an actual address once it is available, i.e. when BTstack boots
// up and starts talking to a Bluetooth module.
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L419">gap_set_local_name</a>("A2DP Sink Demo 00:00:00:00:00:00");
// - Allow to show up in Bluetooth inquiry
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L1256">gap_discoverable_control</a>(1);
// - Set Class of Device - Service Class: Audio + Rendering, Major Device Class: Audio, Minor: Headphone
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L432">gap_set_class_of_device</a>(0x240400);
// - Allow for role switch in general and sniff mode
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L439">gap_set_default_link_policy_settings</a>( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE );
// - Allow for role switch on outgoing connections
// - This allows A2DP Source, e.g. smartphone, to become master when we re-connect to it.
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L445">gap_set_allow_role_switch</a>(true);
// Register for HCI events
hci_event_callback_registration.callback = &hci_packet_handler;
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1580">hci_add_event_handler</a>(&hci_event_callback_registration);
// Inform about audio playback / test options
#ifdef HAVE_POSIX_FILE_IO
if (!<a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_audio.h#L157">btstack_audio_sink_get_instance</a>()){
printf("No audio playback.\n");
} else {
printf("Audio playback supported.\n");
}
#ifdef STORE_TO_WAV_FILE
printf("Audio will be stored to \'%s\' file.\n", wav_filename);
#endif
#endif
return 0;
}
Handle Media Data Packet
Here the audio data, are received through the handle_l2cap_media_data_packet callback. Currently, only the SBC media codec is supported. Hence, the media data consists of the media packet header and the SBC packet. The SBC frame will be stored in a ring buffer for later processing (instead of decoding it to PCM right away which would require a much larger buffer). If the audio stream wasn't started already and there are enough SBC frames in the ring buffer, start playback.