xref: /btstack/example/a2dp_source_demo.c (revision adaba9f34040c696f6e8e6f1096f218bf6ceb78e)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "btstack.h"
45 
46 #include "hxcmod.h"
47 #include "mods/mod.h"
48 
49 #define NUM_CHANNELS                2
50 #define A2DP_SAMPLE_RATE            44100
51 #define BYTES_PER_AUDIO_SAMPLE      (2*NUM_CHANNELS)
52 #define AUDIO_TIMEOUT_MS            10
53 #define TABLE_SIZE_441HZ            100
54 
55 typedef enum {
56     STREAM_SINE,
57     STREAM_MOD
58 } stream_data_source_t;
59 
60 typedef struct {
61     uint16_t a2dp_cid;
62     uint8_t  local_seid;
63 
64     uint32_t time_audio_data_sent; // ms
65     uint32_t acc_num_missed_samples;
66     uint32_t samples_ready;
67     btstack_timer_source_t audio_timer;
68     uint8_t  streaming;
69     int      max_media_payload_size;
70 
71     uint8_t  sbc_storage[1030];
72     uint16_t sbc_storage_count;
73     uint8_t  sbc_ready_to_send;
74 } a2dp_media_sending_context_t;
75 
76 static  uint8_t media_sbc_codec_capabilities[] = {
77     (AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO,
78     0xFF,//(AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS,
79     2, 53
80 };
81 
82 static const int16_t sine_int16[] = {
83      0,    2057,    4107,    6140,    8149,   10126,   12062,   13952,   15786,   17557,
84  19260,   20886,   22431,   23886,   25247,   26509,   27666,   28714,   29648,   30466,
85  31163,   31738,   32187,   32509,   32702,   32767,   32702,   32509,   32187,   31738,
86  31163,   30466,   29648,   28714,   27666,   26509,   25247,   23886,   22431,   20886,
87  19260,   17557,   15786,   13952,   12062,   10126,    8149,    6140,    4107,    2057,
88      0,   -2057,   -4107,   -6140,   -8149,  -10126,  -12062,  -13952,  -15786,  -17557,
89 -19260,  -20886,  -22431,  -23886,  -25247,  -26509,  -27666,  -28714,  -29648,  -30466,
90 -31163,  -31738,  -32187,  -32509,  -32702,  -32767,  -32702,  -32509,  -32187,  -31738,
91 -31163,  -30466,  -29648,  -28714,  -27666,  -26509,  -25247,  -23886,  -22431,  -20886,
92 -19260,  -17557,  -15786,  -13952,  -12062,  -10126,   -8149,   -6140,   -4107,   -2057,
93 };
94 
95 static const char * device_name = "A2DP Source BTstack";
96 
97 #ifdef HAVE_BTSTACK_STDIN
98 // mac 2011:    static const char * device_addr_string = "04:0C:CE:E4:85:D3";
99 // pts:         static const char * device_addr_string = "00:1B:DC:08:0A:A5";
100 // mac 2013:    static const char * device_addr_string = "84:38:35:65:d1:15";
101 // phone 2013:  static const char * device_addr_string = "D8:BB:2C:DF:F0:F2";
102 // minijambox:
103 static const char * device_addr_string = "00:21:3C:AC:F7:38";
104 // head phones: static const char * device_addr_string = "00:18:09:28:50:18";
105 // bt dongle:   static const char * device_addr_string = "00:15:83:5F:9D:46";
106 #endif
107 
108 static bd_addr_t device_addr;
109 static uint8_t sdp_a2dp_source_service_buffer[150];
110 static uint8_t media_sbc_codec_configuration[4];
111 static a2dp_media_sending_context_t media_tracker;
112 
113 static uint16_t avrcp_cid;
114 
115 static stream_data_source_t data_source;
116 
117 static int sine_phase;
118 
119 static int hxcmod_initialized;
120 static modcontext mod_context;
121 static tracker_buffer_state trkbuf;
122 
123 
124 /* AVRCP Target context START */
125 static const uint8_t subunit_info[] = {
126     0,0,0,0,
127     1,1,1,1,
128     2,2,2,2,
129     3,3,3,3,
130     4,4,4,4,
131     5,5,5,5,
132     6,6,6,6,
133     7,7,7,7
134 };
135 
136 static uint32_t company_id = 0x112233;
137 static uint8_t companies_num = 1;
138 static uint8_t companies[] = {
139     0x00, 0x19, 0x58 //BT SIG registered CompanyID
140 };
141 
142 static uint8_t events_num = 13;
143 static uint8_t events[] = {
144     AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED,
145     AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED,
146     AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END,
147     AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START,
148     AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED,
149     AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED,
150     AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED,
151     AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED,
152     AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED,
153     AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED,
154     AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED,
155     AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED,
156     AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED
157 };
158 
159 typedef struct {
160     char title[40];
161     char artist[40];
162     char album[40];
163     char genre[40];
164     uint32_t song_length_ms;
165     int total_tracks;
166     int track_nr;
167 } avrcp_now_playing_info_t;
168 
169 typedef struct {
170     avrcp_play_status_t status;
171     uint32_t song_length_ms;   // 0xFFFFFFFF if not supported
172     uint32_t song_position_ms; // 0xFFFFFFFF if not supported
173 } avrcp_play_status_info_t;
174 
175 avrcp_now_playing_info_t now_playing_info;
176 avrcp_play_status_info_t play_info;
177 
178 /* AVRCP Target context END */
179 
180 static void a2dp_demo_send_media_packet(void){
181     int num_bytes_in_frame = btstack_sbc_encoder_sbc_buffer_length();
182     int bytes_in_storage = media_tracker.sbc_storage_count;
183     uint8_t num_frames = bytes_in_storage / num_bytes_in_frame;
184     a2dp_source_stream_send_media_payload(media_tracker.local_seid, media_tracker.sbc_storage, bytes_in_storage, num_frames, 0);
185     media_tracker.sbc_storage_count = 0;
186     media_tracker.sbc_ready_to_send = 0;
187 }
188 
189 static void produce_sine_audio(int16_t * pcm_buffer, int num_samples_to_write){
190     int count;
191     for (count = 0; count < num_samples_to_write ; count++){
192         pcm_buffer[count * 2]     = sine_int16[sine_phase];
193         pcm_buffer[count * 2 + 1] = sine_int16[sine_phase];
194         sine_phase++;
195         if (sine_phase >= TABLE_SIZE_441HZ){
196             sine_phase -= TABLE_SIZE_441HZ;
197         }
198     }
199 }
200 
201 static void produce_mod_audio(int16_t * pcm_buffer, int num_samples_to_write){
202     hxcmod_fillbuffer(&mod_context, (unsigned short *) &pcm_buffer[0], num_samples_to_write, &trkbuf);
203 }
204 
205 static void produce_audio(int16_t * pcm_buffer, int num_samples){
206     switch (data_source){
207         case STREAM_SINE:
208             produce_sine_audio(pcm_buffer, num_samples);
209             break;
210         case STREAM_MOD:
211             produce_mod_audio(pcm_buffer, num_samples);
212             break;
213     }
214 }
215 
216 static int a2dp_demo_fill_sbc_audio_buffer(a2dp_media_sending_context_t * context){
217     // perform sbc encodin
218     int total_num_bytes_read = 0;
219     unsigned int num_audio_samples_per_sbc_buffer = btstack_sbc_encoder_num_audio_frames();
220     while (context->samples_ready >= num_audio_samples_per_sbc_buffer
221         && (context->max_media_payload_size - context->sbc_storage_count) >= btstack_sbc_encoder_sbc_buffer_length()){
222 
223         int16_t pcm_frame[256*NUM_CHANNELS];
224 
225         produce_audio(pcm_frame, num_audio_samples_per_sbc_buffer);
226         btstack_sbc_encoder_process_data(pcm_frame);
227 
228         uint16_t sbc_frame_size = btstack_sbc_encoder_sbc_buffer_length();
229         uint8_t * sbc_frame = btstack_sbc_encoder_sbc_buffer();
230 
231         total_num_bytes_read += num_audio_samples_per_sbc_buffer;
232         memcpy(&context->sbc_storage[context->sbc_storage_count], sbc_frame, sbc_frame_size);
233         context->sbc_storage_count += sbc_frame_size;
234         context->samples_ready -= num_audio_samples_per_sbc_buffer;
235     }
236     return total_num_bytes_read;
237 }
238 
239 static void a2dp_demo_audio_timeout_handler(btstack_timer_source_t * timer){
240     a2dp_media_sending_context_t * context = (a2dp_media_sending_context_t *) btstack_run_loop_get_timer_context(timer);
241     btstack_run_loop_set_timer(&context->audio_timer, AUDIO_TIMEOUT_MS);
242     btstack_run_loop_add_timer(&context->audio_timer);
243     uint32_t now = btstack_run_loop_get_time_ms();
244 
245     uint32_t update_period_ms = AUDIO_TIMEOUT_MS;
246     if (context->time_audio_data_sent > 0){
247         update_period_ms = now - context->time_audio_data_sent;
248     }
249 
250     uint32_t num_samples = (update_period_ms * A2DP_SAMPLE_RATE) / 1000;
251     context->acc_num_missed_samples += (update_period_ms * A2DP_SAMPLE_RATE) % 1000;
252 
253     while (context->acc_num_missed_samples >= 1000){
254         num_samples++;
255         context->acc_num_missed_samples -= 1000;
256     }
257     context->time_audio_data_sent = now;
258     context->samples_ready += num_samples;
259 
260     if (context->sbc_ready_to_send) return;
261 
262     a2dp_demo_fill_sbc_audio_buffer(context);
263 
264     if ((context->sbc_storage_count + btstack_sbc_encoder_sbc_buffer_length()) > context->max_media_payload_size){
265         // schedule sending
266         context->sbc_ready_to_send = 1;
267         a2dp_source_stream_endpoint_request_can_send_now(context->local_seid);
268     }
269 }
270 
271 static void a2dp_demo_timer_start(a2dp_media_sending_context_t * context){
272     context->max_media_payload_size = a2dp_max_media_payload_size(context->local_seid);
273     context->sbc_storage_count = 0;
274     context->sbc_ready_to_send = 0;
275     context->streaming = 1;
276     btstack_run_loop_remove_timer(&context->audio_timer);
277     btstack_run_loop_set_timer_handler(&context->audio_timer, a2dp_demo_audio_timeout_handler);
278     btstack_run_loop_set_timer_context(&context->audio_timer, context);
279     btstack_run_loop_set_timer(&context->audio_timer, AUDIO_TIMEOUT_MS);
280     btstack_run_loop_add_timer(&context->audio_timer);
281 }
282 
283 static void a2dp_demo_timer_stop(a2dp_media_sending_context_t * context){
284     context->time_audio_data_sent = 0;
285     context->acc_num_missed_samples = 0;
286     context->samples_ready = 0;
287     context->streaming = 1;
288     context->sbc_storage_count = 0;
289     context->sbc_ready_to_send = 0;
290     btstack_run_loop_remove_timer(&context->audio_timer);
291 }
292 
293 static void a2dp_demo_timer_pause(a2dp_media_sending_context_t * context){
294     btstack_run_loop_remove_timer(&context->audio_timer);
295 }
296 
297 
298 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
299     UNUSED(channel);
300     UNUSED(size);
301     uint8_t status;
302     uint8_t local_seid;
303 
304     switch (packet_type) {
305 
306         case HCI_EVENT_PACKET:
307             switch (hci_event_packet_get_type(packet)) {
308                 case HCI_EVENT_A2DP_META:
309                     switch (packet[2]){
310                         case A2DP_SUBEVENT_STREAM_ESTABLISHED:
311                             status = a2dp_subevent_stream_established_get_status(packet);
312                             if (status){
313                                 printf("Stream establishment failed: status 0x%02x.\n", status);
314                                 break;
315                             }
316                             local_seid = a2dp_subevent_stream_established_get_local_seid(packet);
317                             if (local_seid != media_tracker.local_seid){
318                                 printf("Stream establishment failed: wrong local seid %d, expected %d.\n", local_seid, media_tracker.local_seid);
319                                 break;
320                             }
321 
322                             media_tracker.a2dp_cid = a2dp_subevent_stream_established_get_a2dp_cid(packet);
323                             printf("Stream established: a2dp cid 0x%02x, local seid %d, remote seid %d.\n",
324                                 media_tracker.a2dp_cid, media_tracker.local_seid, a2dp_subevent_stream_established_get_remote_seid(packet));
325                             break;
326 
327                         case A2DP_SUBEVENT_STREAM_STARTED:
328                             play_info.status = AVRCP_PLAY_STATUS_PLAYING;
329                             a2dp_demo_timer_start(&media_tracker);
330                             printf("Stream started.\n");
331                             break;
332 
333                         case A2DP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW:
334                             a2dp_demo_send_media_packet();
335                             break;
336 
337                         case A2DP_SUBEVENT_STREAM_SUSPENDED:
338                             play_info.status = AVRCP_PLAY_STATUS_PAUSED;
339                             printf("Stream paused.\n");
340                             a2dp_demo_timer_pause(&media_tracker);
341                             break;
342 
343                         case A2DP_SUBEVENT_STREAM_RELEASED:
344                             play_info.status = AVRCP_PLAY_STATUS_STOPPED;
345                             printf("Stream released.\n");
346                             a2dp_demo_timer_stop(&media_tracker);
347                             break;
348                         default:
349                             printf("AVDTP Source demo: event 0x%02x is not implemented\n", packet[2]);
350                             break;
351                     }
352                     break;
353                 default:
354                     break;
355             }
356             break;
357         default:
358             // other packet type
359             break;
360     }
361 }
362 
363 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
364     UNUSED(channel);
365     UNUSED(size);
366     bd_addr_t event_addr;
367     uint16_t local_cid;
368     uint8_t  status = 0xFF;
369 
370     if (packet_type != HCI_EVENT_PACKET) return;
371     if (hci_event_packet_get_type(packet) != HCI_EVENT_AVRCP_META) return;
372     switch (packet[2]){
373         case AVRCP_SUBEVENT_CONNECTION_ESTABLISHED: {
374             local_cid = avrcp_subevent_connection_established_get_avrcp_cid(packet);
375             if (avrcp_cid != 0 && avrcp_cid != local_cid) {
376                 printf("AVRCP Connection failed, expected 0x%02X l2cap cid, received 0x%02X\n", avrcp_cid, local_cid);
377                 return;
378             }
379 
380             status = avrcp_subevent_connection_established_get_status(packet);
381             if (status != ERROR_CODE_SUCCESS){
382                 printf("AVRCP Connection failed: status 0x%02x\n", status);
383                 avrcp_cid = 0;
384                 return;
385             }
386             avrcp_cid = local_cid;
387             play_info.song_length_ms = 0xFFFFFFFF;
388             play_info.song_position_ms = 0xFFFFFFFF;
389             play_info.status = AVRCP_PLAY_STATUS_ERROR;
390 
391             avrcp_subevent_connection_established_get_bd_addr(packet, event_addr);
392             printf("Channel successfully opened: %s, avrcp_cid 0x%02x\n", bd_addr_to_str(event_addr), local_cid);
393             return;
394         }
395 
396         case AVRCP_SUBEVENT_UNIT_INFO_QUERY:
397             avrcp_target_unit_info(avrcp_cid, AVRCP_SUBUNIT_TYPE_AUDIO, company_id);
398             break;
399         case AVRCP_SUBEVENT_SUBUNIT_INFO_QUERY:
400             avrcp_target_subunit_info(avrcp_cid, AVRCP_SUBUNIT_TYPE_UNIT, avrcp_subevent_subunit_info_query_get_offset(packet), (uint8_t *)subunit_info);
401             break;
402         case AVRCP_SUBEVENT_EVENT_IDS_QUERY:
403             avrcp_target_supported_events(avrcp_cid, events_num, events, sizeof(events));
404             break;
405         case AVRCP_SUBEVENT_COMPANY_IDS_QUERY:
406             avrcp_target_supported_companies(avrcp_cid, companies_num, companies, sizeof(companies));
407             break;
408         case AVRCP_SUBEVENT_PLAY_STATUS_QUERY:
409             avrcp_target_play_status(avrcp_cid, play_info.song_length_ms, play_info.song_position_ms, play_info.status);
410             break;
411         case AVRCP_SUBEVENT_NOW_PLAYING_INFO_QUERY:
412             avrcp_target_set_now_playing_title(avrcp_cid, now_playing_info.title);
413             avrcp_target_set_now_playing_artist(avrcp_cid, now_playing_info.artist);
414             avrcp_target_set_now_playing_album(avrcp_cid, now_playing_info.album);
415             avrcp_target_set_now_playing_genre(avrcp_cid, now_playing_info.genre);
416             avrcp_target_set_now_playing_track_nr(avrcp_cid, now_playing_info.track_nr);
417             avrcp_target_set_now_playing_total_tracks(avrcp_cid, now_playing_info.total_tracks);
418             avrcp_target_set_now_playing_song_length_ms(avrcp_cid, now_playing_info.song_length_ms);
419 
420             avrcp_target_now_playing_info(avrcp_cid);
421             break;
422         case AVRCP_SUBEVENT_CONNECTION_RELEASED:
423             printf("Channel released: avrcp_cid 0x%02x\n", avrcp_subevent_connection_released_get_avrcp_cid(packet));
424             avrcp_cid = 0;
425             return;
426         default:
427             printf("A2DP Source/AVRCP Target event not parsed %02x\n", packet[2]);
428             break;
429     }
430 }
431 
432 #ifdef HAVE_BTSTACK_STDIN
433 static void show_usage(void){
434     bd_addr_t      iut_address;
435     gap_local_bd_addr(iut_address);
436     printf("\n--- Bluetooth A2DP Source/AVRCP Target Demo %s ---\n", bd_addr_to_str(iut_address));
437     printf("b      - AVDTP Source create  connection to addr %s\n", device_addr_string);
438     printf("B      - AVDTP Source disconnect\n");
439     printf("c      - AVRCP Target create connection to addr %s\n", device_addr_string);
440     printf("C      - AVRCP Target disconnect\n");
441 
442     printf("x      - start streaming sine\n");
443     if (hxcmod_initialized){
444         printf("z      - start streaming '%s'\n", mod_name);
445     }
446     printf("p      - pause streaming\n");
447 
448     printf("\n--- Bluetooth  AVRCP Target Commands %s ---\n", bd_addr_to_str(iut_address));
449     printf("Ctrl-c - exit\n");
450     printf("---\n");
451 }
452 
453 static void stdin_process(char cmd){
454     uint8_t status = ERROR_CODE_SUCCESS;
455     switch (cmd){
456         case 'b':
457             printf(" - Create AVDTP Source connection to addr %s.\n", bd_addr_to_str(device_addr));
458             status = a2dp_source_establish_stream(device_addr, media_tracker.local_seid, &media_tracker.a2dp_cid);
459             break;
460         case 'B':
461             printf(" - AVDTP Source Disconnect\n");
462             status = a2dp_source_disconnect(media_tracker.a2dp_cid);
463             break;
464         case 'c':
465             printf(" - Create AVRCP Target connection to addr %s.\n", bd_addr_to_str(device_addr));
466             status = avrcp_target_connect(device_addr, &avrcp_cid);
467             break;
468         case 'C':
469             printf(" - AVRCP Target disconnect\n");
470             status = avrcp_target_disconnect(avrcp_cid);
471             break;
472 
473         case '\n':
474         case '\r':
475             break;
476 
477         case 'x':
478             printf("Playing sine.\n");
479             data_source = STREAM_SINE;
480             status = a2dp_source_start_stream(media_tracker.a2dp_cid, media_tracker.local_seid);
481             break;
482         case 'z':
483             printf("Playing mod.\n");
484             data_source = STREAM_MOD;
485             status = a2dp_source_start_stream(media_tracker.a2dp_cid, media_tracker.local_seid);
486             break;
487         case 'p':
488             printf("Pause stream.\n");
489             status = a2dp_source_pause_stream(media_tracker.a2dp_cid, media_tracker.local_seid);
490             break;
491 
492         default:
493             show_usage();
494             return;
495     }
496     if (status != ERROR_CODE_SUCCESS){
497         printf("Could not perform command, status 0x%2x\n", status);
498     }
499 }
500 #endif
501 
502 
503 int btstack_main(int argc, const char * argv[]);
504 int btstack_main(int argc, const char * argv[]){
505     UNUSED(argc);
506     (void)argv;
507 
508     l2cap_init();
509     // Initialize AVDTP Source
510     a2dp_source_init();
511     a2dp_source_register_packet_handler(&packet_handler);
512 
513     avdtp_stream_endpoint_t * local_stream_endpoint = a2dp_source_create_stream_endpoint(AVDTP_AUDIO, AVDTP_CODEC_SBC, media_sbc_codec_capabilities, sizeof(media_sbc_codec_capabilities), media_sbc_codec_configuration, sizeof(media_sbc_codec_configuration));
514     if (!local_stream_endpoint){
515         printf("A2DP source demo: not enough memory to create local stream endpoint\n");
516         return 1;
517     }
518     media_tracker.local_seid = avdtp_local_seid(local_stream_endpoint);
519 
520     // Initialize AVRCP Target
521     avrcp_target_init();
522     avrcp_target_register_packet_handler(&avrcp_target_packet_handler);
523 
524     // Initialize SDP
525     sdp_init();
526     memset(sdp_a2dp_source_service_buffer, 0, sizeof(sdp_a2dp_source_service_buffer));
527     a2dp_source_create_sdp_record(sdp_a2dp_source_service_buffer, 0x10002, 1, NULL, NULL);
528     sdp_register_service(sdp_a2dp_source_service_buffer);
529 
530     gap_set_local_name(device_name);
531     gap_discoverable_control(1);
532     gap_set_class_of_device(0x200408);
533 
534     hxcmod_initialized = hxcmod_init(&mod_context);
535     if (hxcmod_initialized){
536         hxcmod_setcfg(&mod_context, A2DP_SAMPLE_RATE, 16, 1, 1, 1);
537         hxcmod_load(&mod_context, (void *) &mod_data, mod_len);
538         printf("loaded mod '%s', size %u\n", mod_name, mod_len);
539     }
540 
541     // For PTS test
542     memcpy(now_playing_info.title,  "Title  1", 8);
543     memcpy(now_playing_info.artist, "Artist 1", 8);
544     memcpy(now_playing_info.album,  "Album  1", 8);
545     memcpy(now_playing_info.genre,  "Genre  1", 8);
546     now_playing_info.track_nr = 1;
547     now_playing_info.total_tracks = 10;
548     now_playing_info.song_length_ms = 3655;
549 
550 #ifdef HAVE_BTSTACK_STDIN
551     // parse human readable Bluetooth address
552     sscanf_bd_addr(device_addr_string, device_addr);
553     btstack_stdin_setup(stdin_process);
554 #endif
555     // turn on!
556     hci_power_control(HCI_POWER_ON);
557     return 0;
558 }
559