2016-09-26 22:44:32 +02:00
/*
* Copyright ( C ) 2016 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions
* are met :
*
* 1. Redistributions of source code must retain the above copyright
* notice , this list of conditions and the following disclaimer .
* 2. Redistributions in binary form must reproduce the above copyright
* notice , this list of conditions and the following disclaimer in the
* documentation and / or other materials provided with the distribution .
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission .
* 4. Any redistribution , use , or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain .
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ` ` AS IS ' ' AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT
* LIMITED TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT ,
* INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING ,
* BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS
* OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY ,
* OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE .
*
* Please inquire about commercial licensing options at
* contact @ bluekitchen - gmbh . com
*
*/
# include <stdint.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
2017-03-06 22:22:17 +01:00
# include <math.h>
2016-09-26 22:44:32 +02:00
2017-07-12 16:35:01 +02:00
# include "btstack.h"
2016-10-19 15:07:28 +02:00
2017-07-19 16:04:58 +02:00
# define NUM_CHANNELS 2
# define A2DP_SAMPLE_RATE 44100
# define BYTES_PER_AUDIO_SAMPLE (2*NUM_CHANNELS)
# define AUDIO_TIMEOUT_MS 10
# define TABLE_SIZE_441HZ 100
# define AVDTP_MEDIA_PAYLOAD_HEADER_SIZE 12
static const int16_t sine_int16 [ ] = {
0 , 2057 , 4107 , 6140 , 8149 , 10126 , 12062 , 13952 , 15786 , 17557 ,
19260 , 20886 , 22431 , 23886 , 25247 , 26509 , 27666 , 28714 , 29648 , 30466 ,
31163 , 31738 , 32187 , 32509 , 32702 , 32767 , 32702 , 32509 , 32187 , 31738 ,
31163 , 30466 , 29648 , 28714 , 27666 , 26509 , 25247 , 23886 , 22431 , 20886 ,
19260 , 17557 , 15786 , 13952 , 12062 , 10126 , 8149 , 6140 , 4107 , 2057 ,
0 , - 2057 , - 4107 , - 6140 , - 8149 , - 10126 , - 12062 , - 13952 , - 15786 , - 17557 ,
- 19260 , - 20886 , - 22431 , - 23886 , - 25247 , - 26509 , - 27666 , - 28714 , - 29648 , - 30466 ,
- 31163 , - 31738 , - 32187 , - 32509 , - 32702 , - 32767 , - 32702 , - 32509 , - 32187 , - 31738 ,
- 31163 , - 30466 , - 29648 , - 28714 , - 27666 , - 26509 , - 25247 , - 23886 , - 22431 , - 20886 ,
- 19260 , - 17557 , - 15786 , - 13952 , - 12062 , - 10126 , - 8149 , - 6140 , - 4107 , - 2057 ,
} ;
2017-01-04 15:13:07 +01:00
typedef struct {
// bitmaps
uint8_t sampling_frequency_bitmap ;
uint8_t channel_mode_bitmap ;
uint8_t block_length_bitmap ;
uint8_t subbands_bitmap ;
uint8_t allocation_method_bitmap ;
uint8_t min_bitpool_value ;
uint8_t max_bitpool_value ;
} adtvp_media_codec_information_sbc_t ;
2017-01-16 14:40:25 +01:00
typedef struct {
2017-01-16 15:16:45 +01:00
int reconfigure ;
2017-01-16 14:40:25 +01:00
int num_channels ;
int sampling_frequency ;
int channel_mode ;
int block_length ;
int subbands ;
int allocation_method ;
int min_bitpool_value ;
int max_bitpool_value ;
int frames_per_buffer ;
} avdtp_media_codec_configuration_sbc_t ;
2017-07-12 16:35:01 +02:00
# ifdef HAVE_BTSTACK_STDIN
// mac 2011: static const char * device_addr_string = "04:0C:CE:E4:85:D3";
// pts: static const char * device_addr_string = "00:1B:DC:08:0A:A5";
// mac 2013: static const char * device_addr_string = "84:38:35:65:d1:15";
// phone 2013: static const char * device_addr_string = "D8:BB:2C:DF:F0:F2";
// minijambox:
static const char * device_addr_string = " 00:21:3C:AC:F7:38 " ;
// head phones: static const char * device_addr_string = "00:18:09:28:50:18";
// bt dongle: static const char * device_addr_string = "00:15:83:5F:9D:46";
# endif
2017-07-19 16:04:58 +02:00
typedef struct {
uint16_t avdtp_cid ;
uint8_t local_seid ;
uint8_t remote_seid ;
uint32_t time_audio_data_sent ; // ms
uint32_t acc_num_missed_samples ;
uint32_t samples_ready ;
btstack_timer_source_t audio_timer ;
uint8_t streaming ;
int max_media_payload_size ;
uint8_t sbc_storage [ 1030 ] ;
uint16_t sbc_storage_count ;
uint8_t sbc_ready_to_send ;
} a2dp_media_sending_context_t ;
a2dp_media_sending_context_t media_tracker ;
2017-07-12 16:35:01 +02:00
static bd_addr_t device_addr ;
2017-07-19 16:04:58 +02:00
static int sine_phase ;
2017-07-12 16:35:01 +02:00
2017-03-06 22:22:17 +01:00
static uint8_t sdp_avdtp_source_service_buffer [ 150 ] ;
2017-01-16 14:40:25 +01:00
2017-07-19 16:04:58 +02:00
static avdtp_stream_endpoint_context_t sc ;
2017-01-16 14:40:25 +01:00
static uint16_t remote_configuration_bitmap ;
static avdtp_capabilities_t remote_configuration ;
2017-07-12 16:35:01 +02:00
static avdtp_context_t a2dp_source_context ;
2017-01-04 17:51:40 +01:00
2016-09-26 22:44:32 +02:00
static btstack_packet_callback_registration_t hci_event_callback_registration ;
2017-07-19 16:04:58 +02:00
static void a2dp_source_setup_media_header ( uint8_t * media_packet , int size , int * offset , uint8_t marker , uint16_t sequence_number ) {
if ( size < AVDTP_MEDIA_PAYLOAD_HEADER_SIZE ) {
log_error ( " small outgoing buffer " ) ;
return ;
}
uint8_t rtp_version = 2 ;
uint8_t padding = 0 ;
uint8_t extension = 0 ;
uint8_t csrc_count = 0 ;
uint8_t payload_type = 1 ; //0x60;
// uint16_t sequence_number = stream_endpoint->sequence_number;
uint32_t timestamp = btstack_run_loop_get_time_ms ( ) ;
uint32_t ssrc = 0x11223344 ;
// rtp header (min size 12B)
int pos = 0 ;
// int mtu = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
media_packet [ pos + + ] = ( rtp_version < < 6 ) | ( padding < < 5 ) | ( extension < < 4 ) | csrc_count ;
media_packet [ pos + + ] = ( marker < < 1 ) | payload_type ;
big_endian_store_16 ( media_packet , pos , sequence_number ) ;
pos + = 2 ;
big_endian_store_32 ( media_packet , pos , timestamp ) ;
pos + = 4 ;
big_endian_store_32 ( media_packet , pos , ssrc ) ; // only used for multicast
pos + = 4 ;
* offset = pos ;
}
static void a2dp_source_copy_media_payload ( uint8_t * media_packet , int size , int * offset , uint8_t * storage , int num_bytes_to_copy , uint8_t num_frames ) {
if ( size < num_bytes_to_copy + 1 ) {
log_error ( " small outgoing buffer: buffer size %u, but need %u " , size , num_bytes_to_copy + 1 ) ;
return ;
}
int pos = * offset ;
media_packet [ pos + + ] = num_frames ; // (fragmentation << 7) | (starting_packet << 6) | (last_packet << 5) | num_frames;
memcpy ( media_packet + pos , storage , num_bytes_to_copy ) ;
pos + = num_bytes_to_copy ;
* offset = pos ;
}
static int avdtp_source_stream_send_media_payload ( uint8_t local_seid , uint8_t * storage , int num_bytes_to_copy , uint8_t num_frames , uint8_t marker ) {
avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid ( local_seid , & a2dp_source_context ) ;
if ( ! stream_endpoint ) {
log_error ( " no stream_endpoint found for seid %d " , local_seid ) ;
return 0 ;
}
if ( stream_endpoint - > l2cap_media_cid = = 0 ) {
log_error ( " no media cid found for seid %d " , local_seid ) ;
return 0 ;
}
int size = l2cap_get_remote_mtu_for_local_cid ( stream_endpoint - > l2cap_media_cid ) ;
int offset = 0 ;
// HACK / PTS requests ERTM although we did not offer it in L2CAP Information Request. Withouth ERTM support, default MTU of 48 will be used, but that's to
// small for a 44.1kHz/16/8/Stereo/bitpool 53 sbc frame of 119 bytes (+ media info)
size = 0x290 ;
l2cap_reserve_packet_buffer ( ) ;
uint8_t * media_packet = l2cap_get_outgoing_buffer ( ) ;
//int size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
a2dp_source_setup_media_header ( media_packet , size , & offset , marker , stream_endpoint - > sequence_number ) ;
a2dp_source_copy_media_payload ( media_packet , size , & offset , storage , num_bytes_to_copy , num_frames ) ;
stream_endpoint - > sequence_number + + ;
l2cap_send_prepared ( stream_endpoint - > l2cap_media_cid , offset ) ;
return size ;
2017-01-04 15:13:07 +01:00
}
2017-07-19 16:04:58 +02:00
static void a2dp_demo_send_media_packet ( void ) {
int num_bytes_in_frame = btstack_sbc_encoder_sbc_buffer_length ( ) ;
int bytes_in_storage = media_tracker . sbc_storage_count ;
uint8_t num_frames = bytes_in_storage / num_bytes_in_frame ;
avdtp_source_stream_send_media_payload ( media_tracker . local_seid , media_tracker . sbc_storage , bytes_in_storage , num_frames , 0 ) ;
media_tracker . sbc_storage_count = 0 ;
media_tracker . sbc_ready_to_send = 0 ;
}
static void produce_sine_audio ( int16_t * pcm_buffer , int num_samples_to_write ) {
int count ;
for ( count = 0 ; count < num_samples_to_write ; count + + ) {
pcm_buffer [ count * 2 ] = sine_int16 [ sine_phase ] ;
pcm_buffer [ count * 2 + 1 ] = sine_int16 [ sine_phase ] ;
sine_phase + + ;
if ( sine_phase > = TABLE_SIZE_441HZ ) {
sine_phase - = TABLE_SIZE_441HZ ;
}
}
}
static int fill_sbc_audio_buffer ( a2dp_media_sending_context_t * context ) {
// perform sbc encodin
int total_num_bytes_read = 0 ;
int num_audio_samples_per_sbc_buffer = btstack_sbc_encoder_num_audio_frames ( ) ;
// printf("num_audio_samples_per_sbc_buffer %d, btstack_sbc_encoder_sbc_buffer_length() %d\n", num_audio_samples_per_sbc_buffer, btstack_sbc_encoder_sbc_buffer_length());
while ( context - > samples_ready > = num_audio_samples_per_sbc_buffer
& & ( context - > max_media_payload_size - context - > sbc_storage_count ) > = btstack_sbc_encoder_sbc_buffer_length ( ) ) {
uint8_t pcm_frame [ 256 * BYTES_PER_AUDIO_SAMPLE ] ;
produce_sine_audio ( ( int16_t * ) pcm_frame , num_audio_samples_per_sbc_buffer ) ;
btstack_sbc_encoder_process_data ( ( int16_t * ) pcm_frame ) ;
uint16_t sbc_frame_size = btstack_sbc_encoder_sbc_buffer_length ( ) ;
uint8_t * sbc_frame = btstack_sbc_encoder_sbc_buffer ( ) ;
total_num_bytes_read + = num_audio_samples_per_sbc_buffer ;
memcpy ( & context - > sbc_storage [ context - > sbc_storage_count ] , sbc_frame , sbc_frame_size ) ;
context - > sbc_storage_count + = sbc_frame_size ;
context - > samples_ready - = num_audio_samples_per_sbc_buffer ;
}
return total_num_bytes_read ;
}
static void avdtp_audio_timeout_handler ( btstack_timer_source_t * timer ) {
a2dp_media_sending_context_t * context = ( a2dp_media_sending_context_t * ) btstack_run_loop_get_timer_context ( timer ) ;
btstack_run_loop_set_timer ( & context - > audio_timer , AUDIO_TIMEOUT_MS ) ;
btstack_run_loop_add_timer ( & context - > audio_timer ) ;
uint32_t now = btstack_run_loop_get_time_ms ( ) ;
uint32_t update_period_ms = AUDIO_TIMEOUT_MS ;
if ( context - > time_audio_data_sent > 0 ) {
update_period_ms = now - context - > time_audio_data_sent ;
}
uint32_t num_samples = ( update_period_ms * A2DP_SAMPLE_RATE ) / 1000 ;
context - > acc_num_missed_samples + = ( update_period_ms * A2DP_SAMPLE_RATE ) % 1000 ;
while ( context - > acc_num_missed_samples > = 1000 ) {
num_samples + + ;
context - > acc_num_missed_samples - = 1000 ;
}
context - > time_audio_data_sent = now ;
context - > samples_ready + = num_samples ;
if ( context - > sbc_ready_to_send ) return ;
fill_sbc_audio_buffer ( context ) ;
if ( ( context - > sbc_storage_count + btstack_sbc_encoder_sbc_buffer_length ( ) ) > context - > max_media_payload_size ) {
// schedule sending
context - > sbc_ready_to_send = 1 ;
// a2dp_source_stream_endpoint_request_can_send_now(context->local_seid);
avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid ( context - > local_seid , & a2dp_source_context ) ;
if ( ! stream_endpoint ) {
printf ( " no stream_endpoint for seid %d \n " , context - > local_seid ) ;
return ;
}
stream_endpoint - > send_stream = 1 ;
if ( stream_endpoint - > connection ) {
avdtp_request_can_send_now_initiator ( stream_endpoint - > connection , stream_endpoint - > l2cap_media_cid ) ;
}
}
}
// static int avdtp_max_media_payload_size(uint8_t local_seid){
// avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context);
// if (!stream_endpoint) {
// log_error("no stream_endpoint found for seid %d", local_seid);
// return 0;
// }
// if (stream_endpoint->l2cap_media_cid == 0){
// log_error("no media cid found for seid %d", local_seid);
// return 0;
// }
// return l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid) - AVDTP_MEDIA_PAYLOAD_HEADER_SIZE;
// }
static void a2dp_demo_timer_start ( a2dp_media_sending_context_t * context ) {
context - > max_media_payload_size = 0x290 ; // avdtp_max_media_payload_size(context->local_seid);
context - > sbc_storage_count = 0 ;
context - > sbc_ready_to_send = 0 ;
context - > streaming = 1 ;
btstack_run_loop_remove_timer ( & context - > audio_timer ) ;
btstack_run_loop_set_timer_handler ( & context - > audio_timer , avdtp_audio_timeout_handler ) ;
btstack_run_loop_set_timer_context ( & context - > audio_timer , context ) ;
btstack_run_loop_set_timer ( & context - > audio_timer , AUDIO_TIMEOUT_MS ) ;
btstack_run_loop_add_timer ( & context - > audio_timer ) ;
}
static void a2dp_demo_timer_stop ( a2dp_media_sending_context_t * context ) {
context - > time_audio_data_sent = 0 ;
context - > acc_num_missed_samples = 0 ;
context - > samples_ready = 0 ;
context - > streaming = 1 ;
context - > sbc_storage_count = 0 ;
context - > sbc_ready_to_send = 0 ;
btstack_run_loop_remove_timer ( & context - > audio_timer ) ;
}
static void a2dp_demo_timer_pause ( a2dp_media_sending_context_t * context ) {
btstack_run_loop_remove_timer ( & context - > audio_timer ) ;
}
static void initialize_streaming_context ( const uint8_t * configuration ) {
sc . channel_mode = configuration [ 0 ] & 0x0F ;
sc . sampling_frequency = configuration [ 0 ] > > 4 ;
sc . block_length = configuration [ 1 ] > > 4 ;
sc . subbands = ( configuration [ 1 ] & 0x0F ) > > 1 ;
sc . allocation_method = ( configuration [ 1 ] & 0x03 ) > > 1 ;
sc . max_bitpool_value = configuration [ 3 ] ;
if ( sc . channel_mode & AVDTP_SBC_JOINT_STEREO ) {
sc . channel_mode = 3 ;
} else if ( sc . channel_mode & AVDTP_SBC_STEREO ) {
sc . channel_mode = 2 ;
} else if ( sc . channel_mode & AVDTP_SBC_DUAL_CHANNEL ) {
sc . channel_mode = 1 ;
} else if ( sc . channel_mode & AVDTP_SBC_MONO ) {
sc . channel_mode = 0 ;
}
if ( sc . sampling_frequency & AVDTP_SBC_16000 ) {
sc . sampling_frequency = 16000 ;
}
if ( sc . sampling_frequency & AVDTP_SBC_32000 ) {
sc . sampling_frequency = 32000 ;
}
if ( sc . sampling_frequency & AVDTP_SBC_44100 ) {
sc . sampling_frequency = 44100 ;
}
if ( sc . sampling_frequency & AVDTP_SBC_48000 ) {
sc . sampling_frequency = 48000 ;
}
if ( sc . subbands & AVDTP_SBC_SUBBANDS_4 ) {
sc . subbands = 4 ;
}
if ( sc . subbands & AVDTP_SBC_SUBBANDS_8 ) {
sc . subbands = 8 ;
}
if ( sc . block_length & AVDTP_SBC_BLOCK_LENGTH_4 ) {
sc . block_length = 4 ;
}
if ( sc . block_length & AVDTP_SBC_BLOCK_LENGTH_8 ) {
sc . block_length = 8 ;
}
if ( sc . block_length & AVDTP_SBC_BLOCK_LENGTH_12 ) {
sc . block_length = 12 ;
}
if ( sc . block_length & AVDTP_SBC_BLOCK_LENGTH_16 ) {
sc . block_length = 16 ;
}
}
static void initialize_sbc_encoder ( void ) {
printf ( " streaming_context: block length %d, subbands %d, allocation_method %d, sampling_frequency %d, max_bitpool_value %d, channel_mode %d \n " ,
sc . block_length , sc . subbands ,
sc . allocation_method , sc . sampling_frequency ,
sc . max_bitpool_value , sc . channel_mode ) ;
btstack_sbc_encoder_init ( & sc . sbc_encoder_state , SBC_MODE_STANDARD ,
sc . block_length , sc . subbands ,
sc . allocation_method , sc . sampling_frequency ,
sc . max_bitpool_value , sc . channel_mode ) ;
2017-01-16 14:40:25 +01:00
}
2016-09-26 22:44:32 +02:00
static void packet_handler ( uint8_t packet_type , uint16_t channel , uint8_t * packet , uint16_t size ) {
2017-07-18 15:34:01 +02:00
if ( packet_type ! = HCI_EVENT_PACKET ) return ;
if ( hci_event_packet_get_type ( packet ) ! = HCI_EVENT_AVDTP_META ) return ;
2017-01-17 17:16:07 +01:00
UNUSED ( channel ) ;
UNUSED ( size ) ;
2017-03-06 22:22:17 +01:00
uint8_t signal_identifier ;
uint8_t status ;
2017-07-12 16:35:01 +02:00
avdtp_sep_t sep ;
2017-07-19 16:04:58 +02:00
uint16_t avdtp_cid ;
2017-03-06 22:22:17 +01:00
2017-07-18 15:34:01 +02:00
switch ( packet [ 2 ] ) {
case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED :
avdtp_cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid ( packet ) ;
status = avdtp_subevent_signaling_connection_established_get_status ( packet ) ;
if ( status ! = 0 ) {
printf ( " AVDTP source signaling connection failed: status %d \n " , status ) ;
break ;
}
2017-07-19 16:04:58 +02:00
media_tracker . avdtp_cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid ( packet ) ;
2017-07-18 15:34:01 +02:00
printf ( " AVDTP source signaling connection established: avdtp_cid 0x%02x \n " , avdtp_cid ) ;
break ;
case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED :
status = avdtp_subevent_streaming_connection_established_get_status ( packet ) ;
if ( status ! = 0 ) {
printf ( " Streaming connection failed: status %d \n " , status ) ;
break ;
}
avdtp_cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid ( packet ) ;
printf ( " Streaming connection established, avdtp_cid 0x%02x \n " , avdtp_cid ) ;
break ;
case AVDTP_SUBEVENT_SIGNALING_SEP_FOUND :
sep . seid = avdtp_subevent_signaling_sep_found_get_remote_seid ( packet ) ; ;
sep . in_use = avdtp_subevent_signaling_sep_found_get_in_use ( packet ) ;
sep . media_type = avdtp_subevent_signaling_sep_found_get_media_type ( packet ) ;
sep . type = avdtp_subevent_signaling_sep_found_get_sep_type ( packet ) ;
printf ( " Found sep: seid %u, in_use %d, media type %d, sep type %d (1-SNK) \n " , sep . seid , sep . in_use , sep . media_type , sep . type ) ;
break ;
2017-07-19 16:04:58 +02:00
case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY : {
// TODO check cid
if ( ! sc . local_stream_endpoint ) return ;
uint8_t sampling_frequency = avdtp_choose_sbc_sampling_frequency ( sc . local_stream_endpoint , avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap ( packet ) ) ;
uint8_t channel_mode = avdtp_choose_sbc_channel_mode ( sc . local_stream_endpoint , avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap ( packet ) ) ;
uint8_t block_length = avdtp_choose_sbc_block_length ( sc . local_stream_endpoint , avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap ( packet ) ) ;
uint8_t subbands = avdtp_choose_sbc_subbands ( sc . local_stream_endpoint , avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap ( packet ) ) ;
uint8_t allocation_method = avdtp_choose_sbc_allocation_method ( sc . local_stream_endpoint , avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap ( packet ) ) ;
uint8_t max_bitpool_value = avdtp_choose_sbc_max_bitpool_value ( sc . local_stream_endpoint , avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value ( packet ) ) ;
uint8_t min_bitpool_value = avdtp_choose_sbc_min_bitpool_value ( sc . local_stream_endpoint , avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value ( packet ) ) ;
sc . local_stream_endpoint - > remote_configuration . media_codec . media_codec_information [ 0 ] = ( sampling_frequency < < 4 ) | channel_mode ;
sc . local_stream_endpoint - > remote_configuration . media_codec . media_codec_information [ 1 ] = ( block_length < < 4 ) | ( subbands < < 2 ) | allocation_method ;
sc . local_stream_endpoint - > remote_configuration . media_codec . media_codec_information [ 2 ] = min_bitpool_value ;
sc . local_stream_endpoint - > remote_configuration . media_codec . media_codec_information [ 3 ] = max_bitpool_value ;
sc . local_stream_endpoint - > remote_configuration_bitmap = store_bit16 ( sc . local_stream_endpoint - > remote_configuration_bitmap , AVDTP_MEDIA_CODEC , 1 ) ;
sc . local_stream_endpoint - > remote_configuration . media_codec . media_type = AVDTP_AUDIO ;
sc . local_stream_endpoint - > remote_configuration . media_codec . media_codec_type = AVDTP_CODEC_SBC ;
2017-07-18 15:34:01 +02:00
break ;
2017-07-19 16:04:58 +02:00
}
2017-07-18 15:34:01 +02:00
case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION : {
2017-07-19 16:04:58 +02:00
// TODO check cid
printf ( " AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION \n " ) ;
sc . sampling_frequency = avdtp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency ( packet ) ;
sc . block_length = avdtp_subevent_signaling_media_codec_sbc_configuration_get_block_length ( packet ) ;
sc . subbands = avdtp_subevent_signaling_media_codec_sbc_configuration_get_subbands ( packet ) ;
sc . allocation_method = avdtp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method ( packet ) - 1 ;
sc . max_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value ( packet ) ;
// TODO: deal with reconfigure: avdtp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet);
2017-07-18 15:34:01 +02:00
break ;
}
2017-07-19 16:04:58 +02:00
2017-07-18 15:34:01 +02:00
case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY :
printf ( " Received non SBC codec: not parsed \n " ) ;
break ;
2017-07-19 16:04:58 +02:00
case AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW :
a2dp_demo_send_media_packet ( ) ;
break ;
2017-07-18 15:34:01 +02:00
case AVDTP_SUBEVENT_SIGNALING_ACCEPT :
2017-07-19 16:04:58 +02:00
// TODO check cid
2017-07-18 15:34:01 +02:00
signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier ( packet ) ;
printf ( " Accepted %s \n " , avdtp_si2str ( signal_identifier ) ) ;
2017-03-06 22:22:17 +01:00
2017-07-18 15:34:01 +02:00
switch ( signal_identifier ) {
2017-07-19 16:04:58 +02:00
case AVDTP_SI_OPEN :
initialize_sbc_encoder ( ) ;
2017-07-18 15:34:01 +02:00
break ;
2017-07-19 16:04:58 +02:00
case AVDTP_SI_SET_CONFIGURATION :
break ;
case AVDTP_SI_START :
printf ( " Stream started. \n " ) ;
a2dp_demo_timer_start ( & media_tracker ) ;
2017-07-18 15:34:01 +02:00
break ;
case AVDTP_SI_SUSPEND :
2017-07-19 16:04:58 +02:00
printf ( " Stream paused. \n " ) ;
a2dp_demo_timer_pause ( & media_tracker ) ;
2017-07-18 15:34:01 +02:00
break ;
case AVDTP_SI_ABORT :
2017-07-19 16:04:58 +02:00
case AVDTP_SI_CLOSE :
printf ( " Stream released. \n " ) ;
a2dp_demo_timer_stop ( & media_tracker ) ;
2017-07-18 15:34:01 +02:00
break ;
2016-09-26 22:44:32 +02:00
default :
break ;
}
break ;
2017-07-19 16:04:58 +02:00
2017-07-18 15:34:01 +02:00
case AVDTP_SUBEVENT_SIGNALING_REJECT :
signal_identifier = avdtp_subevent_signaling_reject_get_signal_identifier ( packet ) ;
printf ( " Rejected %s \n " , avdtp_si2str ( signal_identifier ) ) ;
2016-09-26 22:44:32 +02:00
break ;
2017-07-18 15:34:01 +02:00
case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT :
signal_identifier = avdtp_subevent_signaling_general_reject_get_signal_identifier ( packet ) ;
printf ( " Rejected %s \n " , avdtp_si2str ( signal_identifier ) ) ;
break ;
2017-07-19 16:04:58 +02:00
case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED :
a2dp_demo_timer_stop ( & media_tracker ) ;
printf ( " Streaming connection released. \n " ) ;
break ;
case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED :
a2dp_demo_timer_stop ( & media_tracker ) ;
printf ( " Signaling connection released. \n " ) ;
break ;
2017-07-18 15:34:01 +02:00
default :
2017-07-19 16:04:58 +02:00
printf ( " AVDTP Source PTS test: event not parsed 0x%02x \n " , packet [ 2 ] ) ;
2017-07-18 15:34:01 +02:00
break ;
2016-09-26 22:44:32 +02:00
}
}
2017-01-26 00:20:42 +01:00
static const uint8_t media_sbc_codec_capabilities [ ] = {
2017-01-18 11:57:29 +01:00
0xFF , //(AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO,
0xFF , //(AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS,
2017-03-20 17:03:46 +01:00
2 , 53
2017-01-04 17:51:40 +01:00
} ;
2017-01-04 15:13:07 +01:00
2017-01-26 00:20:42 +01:00
static const uint8_t media_sbc_codec_configuration [ ] = {
( AVDTP_SBC_44100 < < 4 ) | AVDTP_SBC_STEREO ,
( AVDTP_SBC_BLOCK_LENGTH_16 < < 4 ) | ( AVDTP_SBC_SUBBANDS_8 < < 2 ) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS ,
2017-03-20 17:03:46 +01:00
2 , 53
2017-01-26 00:20:42 +01:00
} ;
static const uint8_t media_sbc_codec_reconfiguration [ ] = {
( AVDTP_SBC_44100 < < 4 ) | AVDTP_SBC_STEREO ,
( AVDTP_SBC_BLOCK_LENGTH_16 < < 4 ) | ( AVDTP_SBC_SUBBANDS_8 < < 2 ) | AVDTP_SBC_ALLOCATION_METHOD_SNR ,
2017-03-20 17:03:46 +01:00
2 , 53
2017-01-26 00:20:42 +01:00
} ;
2017-07-19 16:04:58 +02:00
2017-07-12 16:35:01 +02:00
# ifdef HAVE_BTSTACK_STDIN
static void show_usage ( void ) {
bd_addr_t iut_address ;
gap_local_bd_addr ( iut_address ) ;
printf ( " \n --- Bluetooth AVDTP SOURCE Test Console %s --- \n " , bd_addr_to_str ( iut_address ) ) ;
printf ( " c - create connection to addr %s \n " , device_addr_string ) ;
printf ( " C - disconnect \n " ) ;
printf ( " d - discover stream endpoints \n " ) ;
printf ( " g - get capabilities \n " ) ;
printf ( " a - get all capabilities \n " ) ;
printf ( " s - set configuration \n " ) ;
printf ( " f - get configuration \n " ) ;
2017-07-19 16:04:58 +02:00
printf ( " R - reconfigure stream with %d \n " , media_tracker . remote_seid ) ;
printf ( " o - open stream with seid %d \n " , media_tracker . remote_seid ) ;
printf ( " m - start stream with %d \n " , media_tracker . remote_seid ) ;
printf ( " A - abort stream with %d \n " , media_tracker . remote_seid ) ;
printf ( " S - stop stream with %d \n " , media_tracker . remote_seid ) ;
printf ( " P - suspend stream with %d \n " , media_tracker . remote_seid ) ;
2017-07-18 15:34:01 +02:00
printf ( " X - stop streaming sine \n " ) ;
2017-07-12 16:35:01 +02:00
printf ( " Ctrl-c - exit \n " ) ;
printf ( " --- \n " ) ;
}
2017-07-19 16:04:58 +02:00
2017-05-26 21:52:29 +02:00
static void stdin_process ( char cmd ) {
2017-01-17 10:28:43 +01:00
switch ( cmd ) {
2016-09-26 22:44:32 +02:00
case ' c ' :
2017-07-18 15:34:01 +02:00
printf ( " Establish AVDTP Source connection to %s \n " , device_addr_string ) ;
2017-07-19 16:04:58 +02:00
avdtp_source_connect ( device_addr , & media_tracker . avdtp_cid ) ;
2016-09-26 22:44:32 +02:00
break ;
2017-01-03 15:50:35 +01:00
case ' C ' :
2017-07-18 15:34:01 +02:00
printf ( " Disconnect AVDTP Source \n " ) ;
2017-07-19 16:04:58 +02:00
avdtp_source_disconnect ( media_tracker . avdtp_cid ) ;
2016-09-26 22:44:32 +02:00
break ;
2017-01-03 15:50:35 +01:00
case ' d ' :
2017-07-18 15:34:01 +02:00
printf ( " Discover stream endpoints of %s \n " , device_addr_string ) ;
2017-07-19 16:04:58 +02:00
avdtp_source_discover_stream_endpoints ( media_tracker . avdtp_cid ) ;
2017-01-04 15:13:07 +01:00
break ;
case ' g ' :
2017-07-19 16:04:58 +02:00
printf ( " Get capabilities of stream endpoint with seid %d \n " , media_tracker . remote_seid ) ;
avdtp_source_get_capabilities ( media_tracker . avdtp_cid , media_tracker . remote_seid ) ;
2017-01-04 15:13:07 +01:00
break ;
case ' a ' :
2017-07-19 16:04:58 +02:00
printf ( " Get all capabilities of stream endpoint with seid %d \n " , media_tracker . remote_seid ) ;
avdtp_source_get_all_capabilities ( media_tracker . avdtp_cid , media_tracker . remote_seid ) ;
2017-01-04 15:13:07 +01:00
break ;
2017-01-16 14:40:25 +01:00
case ' f ' :
2017-07-19 16:04:58 +02:00
printf ( " Get configuration of stream endpoint with seid %d \n " , media_tracker . remote_seid ) ;
avdtp_source_get_configuration ( media_tracker . avdtp_cid , media_tracker . remote_seid ) ;
2017-01-16 14:40:25 +01:00
break ;
2017-01-04 15:13:07 +01:00
case ' s ' :
2017-07-19 16:04:58 +02:00
printf ( " Set configuration of stream endpoint with seid %d \n " , media_tracker . remote_seid ) ;
2017-01-16 14:40:25 +01:00
remote_configuration_bitmap = store_bit16 ( remote_configuration_bitmap , AVDTP_MEDIA_CODEC , 1 ) ;
remote_configuration . media_codec . media_type = AVDTP_AUDIO ;
remote_configuration . media_codec . media_codec_type = AVDTP_CODEC_SBC ;
2017-01-26 00:20:42 +01:00
remote_configuration . media_codec . media_codec_information_len = sizeof ( media_sbc_codec_configuration ) ;
2017-07-12 16:35:01 +02:00
remote_configuration . media_codec . media_codec_information = ( uint8_t * ) media_sbc_codec_configuration ;
2017-07-19 16:04:58 +02:00
initialize_streaming_context ( media_sbc_codec_configuration ) ;
avdtp_source_set_configuration ( media_tracker . avdtp_cid , media_tracker . local_seid , media_tracker . remote_seid , remote_configuration_bitmap , remote_configuration ) ;
2017-01-10 17:01:13 +01:00
break ;
case ' R ' :
2017-07-19 16:04:58 +02:00
printf ( " Reconfigure stream endpoint with seid %d \n " , media_tracker . remote_seid ) ;
2017-01-26 00:20:42 +01:00
remote_configuration_bitmap = store_bit16 ( remote_configuration_bitmap , AVDTP_MEDIA_CODEC , 1 ) ;
remote_configuration . media_codec . media_type = AVDTP_AUDIO ;
remote_configuration . media_codec . media_codec_type = AVDTP_CODEC_SBC ;
remote_configuration . media_codec . media_codec_information_len = sizeof ( media_sbc_codec_reconfiguration ) ;
2017-07-12 16:35:01 +02:00
remote_configuration . media_codec . media_codec_information = ( uint8_t * ) media_sbc_codec_reconfiguration ;
2017-07-19 16:04:58 +02:00
initialize_streaming_context ( media_sbc_codec_reconfiguration ) ;
avdtp_source_reconfigure ( media_tracker . avdtp_cid , media_tracker . local_seid , media_tracker . remote_seid , remote_configuration_bitmap , remote_configuration ) ;
2017-01-10 17:01:13 +01:00
break ;
case ' o ' :
2017-07-19 16:04:58 +02:00
printf ( " Establish stream between local %d and remote %d seid \n " , media_tracker . local_seid , media_tracker . remote_seid ) ;
initialize_sbc_encoder ( ) ;
avdtp_source_open_stream ( media_tracker . avdtp_cid , media_tracker . local_seid , media_tracker . remote_seid ) ;
2017-01-03 15:50:35 +01:00
break ;
2017-01-10 17:01:13 +01:00
case ' m ' :
2017-07-19 16:04:58 +02:00
printf ( " Start stream between local %d and remote %d seid, \n " , media_tracker . local_seid , media_tracker . remote_seid ) ;
avdtp_source_start_stream ( media_tracker . avdtp_cid , media_tracker . local_seid ) ;
2017-01-10 17:01:13 +01:00
break ;
case ' A ' :
2017-07-19 16:04:58 +02:00
printf ( " Abort stream between local %d and remote %d seid \n " , media_tracker . local_seid , media_tracker . remote_seid ) ;
avdtp_source_abort_stream ( media_tracker . avdtp_cid , media_tracker . local_seid ) ;
2017-01-26 00:20:42 +01:00
break ;
2017-01-10 17:01:13 +01:00
case ' S ' :
2017-07-19 16:04:58 +02:00
printf ( " Release stream between local %d and remote %d seid \n " , media_tracker . local_seid , media_tracker . remote_seid ) ;
avdtp_source_stop_stream ( media_tracker . avdtp_cid , media_tracker . local_seid ) ;
2017-01-26 00:20:42 +01:00
break ;
2017-01-10 17:01:13 +01:00
case ' P ' :
2017-07-19 16:04:58 +02:00
printf ( " Susspend stream between local %d and remote %d seid \n " , media_tracker . local_seid , media_tracker . remote_seid ) ;
avdtp_source_suspend ( media_tracker . avdtp_cid , media_tracker . local_seid ) ;
2017-03-06 22:22:17 +01:00
break ;
case ' X ' :
2017-07-19 16:04:58 +02:00
printf ( " Stop streaming \n " ) ;
avdtp_source_stop_stream ( media_tracker . avdtp_cid , media_tracker . local_seid ) ;
2017-03-06 22:22:17 +01:00
break ;
2016-09-26 22:44:32 +02:00
case ' \n ' :
case ' \r ' :
break ;
default :
show_usage ( ) ;
break ;
}
}
2017-07-12 16:35:01 +02:00
# endif
2016-09-26 22:44:32 +02:00
2017-07-19 16:04:58 +02:00
2016-09-26 22:44:32 +02:00
int btstack_main ( int argc , const char * argv [ ] ) ;
int btstack_main ( int argc , const char * argv [ ] ) {
2017-01-17 17:16:07 +01:00
UNUSED ( argc ) ;
( void ) argv ;
2016-09-26 22:44:32 +02:00
/* Register for HCI events */
hci_event_callback_registration . callback = & packet_handler ;
hci_add_event_handler ( & hci_event_callback_registration ) ;
l2cap_init ( ) ;
// Initialize AVDTP Sink
2017-07-12 16:35:01 +02:00
avdtp_source_init ( & a2dp_source_context ) ;
2017-03-06 22:22:17 +01:00
avdtp_source_register_packet_handler ( & packet_handler ) ;
2017-07-19 16:04:58 +02:00
media_tracker . local_seid = a2dp_source_create_stream_endpoint ( AVDTP_AUDIO , AVDTP_CODEC_SBC , ( uint8_t * ) media_sbc_codec_capabilities , sizeof ( media_sbc_codec_capabilities ) , ( uint8_t * ) media_sbc_codec_configuration , sizeof ( media_sbc_codec_configuration ) ) ;
media_tracker . remote_seid = 1 ;
2016-09-28 12:07:40 +02:00
2016-09-26 22:44:32 +02:00
// Initialize SDP
sdp_init ( ) ;
2017-03-06 22:22:17 +01:00
memset ( sdp_avdtp_source_service_buffer , 0 , sizeof ( sdp_avdtp_source_service_buffer ) ) ;
2017-04-04 11:55:09 +02:00
a2dp_source_create_sdp_record ( sdp_avdtp_source_service_buffer , 0x10002 , 1 , NULL , NULL ) ;
2017-03-06 22:22:17 +01:00
sdp_register_service ( sdp_avdtp_source_service_buffer ) ;
2016-10-19 15:07:28 +02:00
2017-07-13 16:04:55 +02:00
gap_set_local_name ( " BTstack AVDTP Source PTS Test " ) ;
2016-10-19 15:07:28 +02:00
gap_discoverable_control ( 1 ) ;
gap_set_class_of_device ( 0x200408 ) ;
2016-09-26 22:44:32 +02:00
2017-07-12 16:35:01 +02:00
# ifdef HAVE_BTSTACK_STDIN
// parse human readable Bluetooth address
sscanf_bd_addr ( device_addr_string , device_addr ) ;
2016-09-26 22:44:32 +02:00
btstack_stdin_setup ( stdin_process ) ;
2017-07-12 16:35:01 +02:00
# endif
// turn on!
hci_power_control ( HCI_POWER_ON ) ;
2016-09-26 22:44:32 +02:00
return 0 ;
}