1 /* 2 * Copyright (C) {copyright_year} 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 BLUEKITCHEN 24 * GMBH 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 #define BTSTACK_FILE__ "le_audio_demo_util_sink.c" 39 40 #include "le_audio_demo_util_sink.h" 41 42 #include "btstack_bool.h" 43 #include "btstack_config.h" 44 #include <btstack_debug.h> 45 #include <printf.h> 46 47 #include "hci.h" 48 #include "btstack_audio.h" 49 #include "btstack_lc3_google.h" 50 #include "btstack_lc3plus_fraunhofer.h" 51 52 #include "hxcmod.h" 53 #include "mods/mod.h" 54 55 #ifdef HAVE_POSIX_FILE_IO 56 #include "wav_util.h" 57 #include "btstack_ring_buffer.h" 58 59 #endif 60 61 //#define DEBUG_PLC 62 #ifdef DEBUG_PLC 63 #define printf_plc(...) printf(__VA_ARGS__) 64 #else 65 #define printf_plc(...) (void)(0); 66 #endif 67 68 #define MAX_CHANNELS 2 69 #define MAX_SAMPLES_PER_FRAME 480 70 #define MAX_LC3_FRAME_BYTES 155 71 72 // playback 73 #define MAX_NUM_LC3_FRAMES 15 74 #define MAX_BYTES_PER_SAMPLE 4 75 #define PLAYBACK_BUFFER_SIZE (MAX_NUM_LC3_FRAMES * MAX_SAMPLES_PER_FRAME * MAX_BYTES_PER_SAMPLE) 76 #define PLAYBACK_START_MS (MAX_NUM_LC3_FRAMES * 20 / 3) 77 78 #define ANSI_COLOR_RED "\x1b[31m" 79 #define ANSI_COLOR_GREEN "\x1b[32m" 80 #define ANSI_COLOR_YELLOW "\x1b[33m" 81 #define ANSI_COLOR_BLUE "\x1b[34m" 82 #define ANSI_COLOR_MAGENTA "\x1b[35m" 83 #define ANSI_COLOR_CYAN "\x1b[36m" 84 #define ANSI_COLOR_RESET "\x1b[0m" 85 86 // SINK 87 88 static const char * le_audio_demo_sink_filename_wav; 89 90 static btstack_lc3_frame_duration_t le_audio_demo_sink_frame_duration; 91 static hci_iso_type_t le_audio_demo_sink_type; 92 93 static uint32_t le_audio_demo_sink_sampling_frequency_hz; 94 static uint16_t le_audio_demo_sink_num_samples_per_frame; 95 static uint8_t le_audio_demo_sink_num_streams; 96 static uint8_t le_audio_demo_sink_num_channels_per_stream; 97 static uint8_t le_audio_demo_sink_num_channels; 98 static uint16_t le_audio_demo_sink_octets_per_frame; 99 static uint16_t le_audio_demo_sink_iso_interval_1250us; 100 static uint8_t le_audio_demo_sink_flush_timeout; 101 static uint8_t le_audio_demo_sink_pre_transmission_offset; 102 103 // playback 104 static uint16_t playback_start_threshold_bytes; 105 static bool playback_active; 106 static uint8_t playback_buffer_storage[PLAYBACK_BUFFER_SIZE]; 107 static btstack_ring_buffer_t playback_buffer; 108 109 // PLC 110 static bool stream_last_packet_received[MAX_CHANNELS]; 111 static uint16_t stream_last_packet_sequence[MAX_CHANNELS]; 112 static uint16_t group_last_packet_sequence; 113 static bool group_last_packet_received; 114 static uint16_t plc_timeout_initial_ms; 115 static uint16_t plc_timeout_subsequent_ms; 116 117 static uint32_t le_audio_demo_sink_lc3_frames; 118 static uint32_t samples_received; 119 static uint32_t samples_played; 120 static uint32_t samples_dropped; 121 122 static btstack_timer_source_t next_packet_timer; 123 124 // lc3 decoder 125 static bool le_audio_demo_lc3plus_decoder_requested = false; 126 static const btstack_lc3_decoder_t * lc3_decoder; 127 static int16_t pcm[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME]; 128 static bool have_pcm[MAX_CHANNELS]; 129 130 static btstack_lc3_decoder_google_t google_decoder_contexts[MAX_CHANNELS]; 131 #ifdef HAVE_LC3PLUS 132 static btstack_lc3plus_fraunhofer_decoder_t fraunhofer_decoder_contexts[MAX_CHANNELS]; 133 #endif 134 static void * decoder_contexts[MAX_CHANNELS]; 135 136 static void le_audio_connection_sink_playback(int16_t * buffer, uint16_t num_samples){ 137 // called from lower-layer but guaranteed to be on main thread 138 log_info("Playback: need %u, have %u", num_samples, btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2)); 139 140 samples_played += num_samples; 141 142 uint32_t bytes_needed = num_samples * le_audio_demo_sink_num_channels * 2; 143 if (playback_active == false){ 144 if (btstack_ring_buffer_bytes_available(&playback_buffer) >= playback_start_threshold_bytes) { 145 log_info("Playback started"); 146 playback_active = true; 147 } 148 } else { 149 if (bytes_needed > btstack_ring_buffer_bytes_available(&playback_buffer)) { 150 log_info("Playback underrun"); 151 printf("Playback Underrun\n"); 152 // empty buffer 153 uint32_t bytes_read; 154 btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read); 155 playback_active = false; 156 } 157 } 158 159 if (playback_active){ 160 uint32_t bytes_read; 161 btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read); 162 btstack_assert(bytes_read == bytes_needed); 163 } else { 164 memset(buffer, 0, bytes_needed); 165 } 166 } 167 168 static void setup_lc3_decoder(void){ 169 uint8_t channel; 170 for (channel = 0 ; channel < le_audio_demo_sink_num_channels ; channel++){ 171 // pick decoder 172 void * decoder_context = NULL; 173 #ifdef HAVE_LC3PLUS 174 if (use_lc3plus_decoder){ 175 decoder_context = &fraunhofer_decoder_contexts[channel]; 176 lc3_decoder = btstack_lc3plus_fraunhofer_decoder_init_instance(decoder_context); 177 } 178 else 179 #endif 180 { 181 decoder_context = &google_decoder_contexts[channel]; 182 lc3_decoder = btstack_lc3_decoder_google_init_instance(decoder_context); 183 } 184 decoder_contexts[channel] = decoder_context; 185 lc3_decoder->configure(decoder_context, le_audio_demo_sink_sampling_frequency_hz, le_audio_demo_sink_frame_duration, le_audio_demo_sink_octets_per_frame); 186 } 187 btstack_assert(le_audio_demo_sink_num_samples_per_frame <= MAX_SAMPLES_PER_FRAME); 188 } 189 190 static void store_samples_in_ringbuffer(void){ 191 // check if we have all channels 192 uint8_t channel; 193 for (channel = 0; channel < le_audio_demo_sink_num_channels; channel++){ 194 if (have_pcm[channel] == false) return; 195 } 196 #ifdef HAVE_POSIX_FILE_IO 197 // write wav samples 198 wav_writer_write_int16(le_audio_demo_sink_num_channels * le_audio_demo_sink_num_samples_per_frame, pcm); 199 #endif 200 // store samples in playback buffer 201 uint32_t bytes_to_store = le_audio_demo_sink_num_channels * le_audio_demo_sink_num_samples_per_frame * 2; 202 samples_received += le_audio_demo_sink_num_samples_per_frame; 203 if (btstack_ring_buffer_bytes_free(&playback_buffer) >= bytes_to_store) { 204 btstack_ring_buffer_write(&playback_buffer, (uint8_t *) pcm, bytes_to_store); 205 } else { 206 printf("Samples dropped\n"); 207 samples_dropped += le_audio_demo_sink_num_samples_per_frame; 208 } 209 memset(have_pcm, 0, sizeof(have_pcm)); 210 } 211 212 static void plc_do(uint8_t stream_index) { 213 // inject packet 214 uint8_t tmp_BEC_detect; 215 uint8_t BFI = 1; 216 uint8_t i; 217 for (i = 0; i < le_audio_demo_sink_num_channels_per_stream; i++){ 218 uint8_t effective_channel = stream_index + i; 219 (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], NULL, BFI, 220 &pcm[effective_channel], le_audio_demo_sink_num_channels, 221 &tmp_BEC_detect); 222 } 223 // and store in ringbuffer when PCM for all channels is available 224 store_samples_in_ringbuffer(); 225 } 226 227 // 228 // Perform PLC for packets missing in previous intervals 229 // 230 // assumptions: 231 // - packet sequence number is monotonic increasing 232 // - if packet with seq nr x is received, all packets with smaller seq number are either received or missed 233 static void plc_check(uint16_t packet_sequence_number) { 234 while (group_last_packet_sequence != packet_sequence_number){ 235 uint8_t i; 236 for (i=0;i<le_audio_demo_sink_num_streams;i++){ 237 // deal with first packet missing. inject silent samples, pcm buffer is memset to zero at start 238 if (stream_last_packet_received[i] == false){ 239 printf_plc("- ISO #%u, very first packet missing\n", i); 240 have_pcm[i] = true; 241 store_samples_in_ringbuffer(); 242 243 stream_last_packet_received[i] = true; 244 stream_last_packet_sequence[i] = group_last_packet_sequence; 245 continue; 246 } 247 248 // missing packet if big sequence counter is higher than bis sequence counter 249 if (btstack_time16_delta(group_last_packet_sequence, stream_last_packet_sequence[i]) > 0) { 250 printf_plc("- ISO #%u, PLC for %u\n", i, group_last_packet_sequence); 251 plc_do(i); 252 btstack_assert((stream_last_packet_sequence[i] + 1) == group_last_packet_sequence); 253 stream_last_packet_sequence[i] = group_last_packet_sequence; 254 } 255 } 256 group_last_packet_sequence++; 257 } 258 } 259 260 static void plc_timeout(btstack_timer_source_t * timer) { 261 // Restart timer. This will loose sync with ISO interval, but if we stop caring if we loose that many packets 262 btstack_run_loop_set_timer(timer, plc_timeout_subsequent_ms); 263 btstack_run_loop_set_timer_handler(timer, plc_timeout); 264 btstack_run_loop_add_timer(timer); 265 266 switch (le_audio_demo_sink_type){ 267 case HCI_ISO_TYPE_CIS: 268 // assume no packet received in iso interval => FT packets missed 269 plc_check(group_last_packet_sequence + le_audio_demo_sink_flush_timeout); 270 break; 271 case HCI_ISO_TYPE_BIS: 272 // assume PTO not used => 1 packet missed 273 plc_check(group_last_packet_sequence + 1); 274 break; 275 default: 276 btstack_unreachable(); 277 break; 278 } 279 } 280 281 void le_audio_demo_util_sink_init(const char * filename_wav){ 282 le_audio_demo_sink_filename_wav = filename_wav; 283 } 284 285 void le_audio_demo_util_sink_enable_lc3plus(bool enable){ 286 le_audio_demo_lc3plus_decoder_requested = enable; 287 } 288 289 void le_audio_demo_util_sink_configure_general(uint8_t num_streams, uint8_t num_channels_per_stream, 290 uint32_t sampling_frequency_hz, 291 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 292 uint32_t iso_interval_1250us) { 293 le_audio_demo_sink_sampling_frequency_hz = sampling_frequency_hz; 294 le_audio_demo_sink_frame_duration = frame_duration; 295 le_audio_demo_sink_octets_per_frame = octets_per_frame; 296 le_audio_demo_sink_iso_interval_1250us = iso_interval_1250us; 297 le_audio_demo_sink_num_streams = num_streams; 298 le_audio_demo_sink_num_channels_per_stream = num_channels_per_stream; 299 300 le_audio_demo_sink_num_channels = le_audio_demo_sink_num_streams * le_audio_demo_sink_num_channels_per_stream; 301 btstack_assert((le_audio_demo_sink_num_channels == 1) || (le_audio_demo_sink_num_channels == 2)); 302 303 le_audio_demo_sink_lc3_frames = 0; 304 305 le_audio_demo_sink_num_samples_per_frame = btstack_lc3_samples_per_frame(le_audio_demo_sink_sampling_frequency_hz, le_audio_demo_sink_frame_duration); 306 307 // switch to lc3plus if requested and possible 308 bool use_lc3plus_decoder = le_audio_demo_lc3plus_decoder_requested && (frame_duration == BTSTACK_LC3_FRAME_DURATION_10000US); 309 310 // init decoder 311 setup_lc3_decoder(); 312 313 printf("Configure: %u streams, %u channels per stream, sampling rate %u, samples per frame %u, lc3plus %u\n", 314 num_streams, num_channels_per_stream, sampling_frequency_hz, le_audio_demo_sink_num_samples_per_frame, use_lc3plus_decoder); 315 316 #ifdef HAVE_POSIX_FILE_IO 317 // create wav file 318 printf("WAV file: %s\n", le_audio_demo_sink_filename_wav); 319 wav_writer_open(le_audio_demo_sink_filename_wav, le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz); 320 #endif 321 322 // init playback buffer 323 btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE); 324 325 // calc start threshold in bytes for PLAYBACK_START_MS 326 playback_start_threshold_bytes = (sampling_frequency_hz / 1000 * PLAYBACK_START_MS) * le_audio_demo_sink_num_channels * 2; 327 328 // start playback 329 const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 330 if (sink != NULL){ 331 sink->init(le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz, le_audio_connection_sink_playback); 332 sink->start_stream(); 333 } 334 } 335 336 void le_audio_demo_util_sink_configure_unicast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz, 337 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 338 uint32_t iso_interval_1250us, uint8_t flush_timeout){ 339 le_audio_demo_sink_type = HCI_ISO_TYPE_CIS; 340 le_audio_demo_sink_flush_timeout = flush_timeout; 341 342 // set playback start: FT * ISO Interval + 10 ms 343 uint16_t playback_start_ms = flush_timeout * (iso_interval_1250us * 5 / 4) + 10; 344 uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms; 345 playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2; 346 printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes); 347 348 // set subsequent plc timeout: FT * ISO Interval 349 plc_timeout_subsequent_ms = flush_timeout * iso_interval_1250us * 5 / 4; 350 351 // set initial plc timeout:FT * ISO Interval + 4 ms 352 plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4; 353 354 printf("PLC: initial timeout %u ms\n", plc_timeout_initial_ms); 355 printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms); 356 357 le_audio_demo_util_sink_configure_general(num_streams, num_channels_per_stream, sampling_frequency_hz, 358 frame_duration, octets_per_frame, iso_interval_1250us); 359 } 360 361 void le_audio_demo_util_sink_configure_broadcast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz, 362 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 363 uint32_t iso_interval_1250us, uint8_t pre_transmission_offset) { 364 le_audio_demo_sink_type = HCI_ISO_TYPE_BIS; 365 le_audio_demo_sink_pre_transmission_offset = pre_transmission_offset; 366 367 // set playback start: ISO Interval + 10 ms 368 uint16_t playback_start_ms = (iso_interval_1250us * 5 / 4) + 10; 369 uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms; 370 playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2; 371 printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes); 372 373 // set subsequent plc timeout: ISO Interval 374 plc_timeout_subsequent_ms = iso_interval_1250us * 5 / 4; 375 376 // set initial plc timeout: ISO Interval + 4 ms 377 plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4; 378 379 printf("PLC: initial timeout %u ms\n", plc_timeout_initial_ms); 380 printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms); 381 382 le_audio_demo_util_sink_configure_unicast(num_streams, num_channels_per_stream, sampling_frequency_hz, frame_duration, 383 octets_per_frame, iso_interval_1250us, pre_transmission_offset); 384 } 385 386 void le_audio_demo_util_sink_receive(uint8_t stream_index, uint8_t *packet, uint16_t size) { 387 uint16_t header = little_endian_read_16(packet, 0); 388 hci_con_handle_t con_handle = header & 0x0fff; 389 uint8_t pb_flag = (header >> 12) & 3; 390 uint8_t ts_flag = (header >> 14) & 1; 391 uint16_t iso_load_len = little_endian_read_16(packet, 2); 392 393 uint16_t offset = 4; 394 uint32_t time_stamp = 0; 395 if (ts_flag){ 396 uint32_t time_stamp = little_endian_read_32(packet, offset); 397 offset += 4; 398 } 399 400 uint32_t receive_time_ms = btstack_run_loop_get_time_ms(); 401 402 uint16_t packet_sequence_number = little_endian_read_16(packet, offset); 403 offset += 2; 404 405 uint16_t header_2 = little_endian_read_16(packet, offset); 406 uint16_t iso_sdu_length = header_2 & 0x3fff; 407 uint8_t packet_status_flag = (uint8_t) (header_2 >> 14); 408 offset += 2; 409 410 if (iso_sdu_length == 0) return; 411 412 if (iso_sdu_length != le_audio_demo_sink_num_channels_per_stream * le_audio_demo_sink_octets_per_frame) { 413 printf("ISO Length %u != %u * %u\n", iso_sdu_length, le_audio_demo_sink_num_channels_per_stream, le_audio_demo_sink_octets_per_frame); 414 return; 415 } 416 417 // start with first packet on first stream 418 if (group_last_packet_received == false){ 419 if (stream_index != 0){ 420 printf("Ignore first packet for second stream\n"); 421 return; 422 } 423 group_last_packet_received = true; 424 group_last_packet_sequence = packet_sequence_number; 425 } 426 427 if (stream_last_packet_received[stream_index]) { 428 printf_plc("ISO #%u, receive %u\n", stream_index, packet_sequence_number); 429 430 int16_t packet_sequence_delta = btstack_time16_delta(packet_sequence_number, 431 stream_last_packet_sequence[stream_index]); 432 if (packet_sequence_delta < 1) { 433 // drop delayed packet that had already been generated by PLC 434 printf_plc("- dropping delayed packet. Current sequence number %u, last received or generated by PLC: %u\n", 435 packet_sequence_number, stream_last_packet_sequence[stream_index]); 436 return; 437 } 438 // simple check 439 if (packet_sequence_number != stream_last_packet_sequence[stream_index] + 1) { 440 printf_plc("- ISO #%u, missing %u\n", stream_index, stream_last_packet_sequence[stream_index] + 1); 441 } 442 } else { 443 printf_plc("ISO %u, first packet seq number %u\n", stream_index, packet_sequence_number); 444 stream_last_packet_received[stream_index] = true; 445 } 446 447 plc_check(packet_sequence_number); 448 449 uint8_t i; 450 for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++){ 451 // decode codec frame 452 uint8_t tmp_BEC_detect; 453 uint8_t BFI = 0; 454 uint8_t effective_channel = stream_index + i; 455 (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], &packet[offset], BFI, 456 &pcm[effective_channel], le_audio_demo_sink_num_channels, 457 &tmp_BEC_detect); 458 offset += le_audio_demo_sink_octets_per_frame; 459 have_pcm[stream_index + i] = true; 460 } 461 462 store_samples_in_ringbuffer(); 463 464 log_info("Samples in playback buffer %5u", btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2)); 465 466 le_audio_demo_sink_lc3_frames++; 467 468 // PLC 469 btstack_run_loop_remove_timer(&next_packet_timer); 470 btstack_run_loop_set_timer(&next_packet_timer, plc_timeout_initial_ms); 471 btstack_run_loop_set_timer_handler(&next_packet_timer, plc_timeout); 472 btstack_run_loop_add_timer(&next_packet_timer); 473 474 if (samples_received >= le_audio_demo_sink_sampling_frequency_hz){ 475 printf("LC3 Frames: %4u - samples received %5u, played %5u, dropped %5u\n", le_audio_demo_sink_lc3_frames, samples_received, samples_played, samples_dropped); 476 samples_received = 0; 477 samples_dropped = 0; 478 samples_played = 0; 479 } 480 481 stream_last_packet_sequence[stream_index] = packet_sequence_number; 482 } 483 484 /** 485 * @brief Close sink: close wav file, stop playback 486 */ 487 void le_audio_demo_util_sink_close(void){ 488 #ifdef HAVE_POSIX_FILE_IO 489 printf("Close WAV file\n"); 490 wav_writer_close(); 491 #endif 492 // stop playback 493 const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 494 if (sink != NULL){ 495 sink->stop_stream(); 496 } 497 // stop timer 498 btstack_run_loop_remove_timer(&next_packet_timer); 499 }