1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 /******************************************************************************
21 ** Name: btif_media_task.c
23 ** Description: This is the multimedia module for the BTIF system. It
24 ** contains task implementations AV, HS and HF profiles
25 ** audio & video processing
27 ******************************************************************************/
29 #define LOG_TAG "bt_btif_media"
36 #include "bt_target.h"
37 #include "fixed_queue.h"
42 #include "bta_sys_int.h"
44 #include "bta_av_api.h"
48 #include "bta_av_sbc.h"
49 #include "bta_av_ci.h"
52 #include "btif_av_co.h"
53 #include "btif_media.h"
62 #include "btif_util.h"
63 #if (BTA_AV_SINK_INCLUDED == TRUE)
64 #include "oi_codec_sbc.h"
65 #include "oi_status.h"
69 #include "btif_media.h"
70 #include "allocator.h"
72 #include "esp_a2dp_api.h"
74 #if (BTA_AV_SINK_INCLUDED == TRUE)
75 OI_CODEC_SBC_DECODER_CONTEXT context;
76 OI_UINT32 contextData[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)];
77 OI_INT16 pcmData[15 * SBC_MAX_SAMPLES_PER_FRAME * SBC_MAX_CHANNELS];
81 /*****************************************************************************
83 *****************************************************************************/
85 #ifndef AUDIO_CHANNEL_OUT_MONO
86 #define AUDIO_CHANNEL_OUT_MONO 0x01
89 #ifndef AUDIO_CHANNEL_OUT_STEREO
90 #define AUDIO_CHANNEL_OUT_STEREO 0x03
93 /* BTIF media cmd event definition : BTIF_MEDIA_TASK_CMD */
95 BTIF_MEDIA_START_AA_TX = 1,
96 BTIF_MEDIA_STOP_AA_TX,
98 BTIF_MEDIA_UIPC_RX_RDY,
99 BTIF_MEDIA_SBC_ENC_INIT,
100 BTIF_MEDIA_SBC_ENC_UPDATE,
101 BTIF_MEDIA_SBC_DEC_INIT,
102 BTIF_MEDIA_VIDEO_DEC_INIT,
103 BTIF_MEDIA_FLUSH_AA_TX,
104 BTIF_MEDIA_FLUSH_AA_RX,
105 BTIF_MEDIA_AUDIO_FEEDING_INIT,
106 BTIF_MEDIA_AUDIO_RECEIVING_INIT,
107 BTIF_MEDIA_AUDIO_SINK_CFG_UPDATE,
108 BTIF_MEDIA_AUDIO_SINK_CLEAR_TRACK
112 MEDIA_TASK_STATE_OFF = 0,
113 MEDIA_TASK_STATE_ON = 1,
114 MEDIA_TASK_STATE_SHUTTING_DOWN = 2
118 SIG_MEDIA_TASK_INIT = 0xf0,
119 SIG_MEDIA_TASK_CLEAN_UP = 0xf1,
120 SIG_MEDIA_TASK_AVK_ALARM_TO = 0xf2,
121 SIG_MEDIA_TASK_AA_ALARM_TO = 0xf3,
122 SIG_MEDIA_TASK_CMD_READY = 0xf4
125 /* Macro to multiply the media task tick */
126 #ifndef BTIF_MEDIA_NUM_TICK
127 #define BTIF_MEDIA_NUM_TICK 1
130 /* Media task tick in milliseconds, must be set to multiple of
131 (1000/TICKS_PER_SEC) (10) */
133 #define BTIF_MEDIA_TIME_TICK (20 * BTIF_MEDIA_NUM_TICK)
134 #define A2DP_DATA_READ_POLL_MS (BTIF_MEDIA_TIME_TICK / 2)
135 #define BTIF_SINK_MEDIA_TIME_TICK (20 * BTIF_MEDIA_NUM_TICK)
139 #define BTIF_MEDIA_AA_POOL_ID GKI_POOL_ID_3
140 #define BTIF_MEDIA_AA_BUF_SIZE GKI_BUF3_SIZE
143 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
144 #define BTIF_MEDIA_AA_SBC_OFFSET (AVDT_MEDIA_OFFSET + BTA_AV_SBC_HDR_SIZE + 1)
146 #define BTIF_MEDIA_AA_SBC_OFFSET (AVDT_MEDIA_OFFSET + BTA_AV_SBC_HDR_SIZE)
149 /* Define the bitrate step when trying to match bitpool value */
150 #ifndef BTIF_MEDIA_BITRATE_STEP
151 #define BTIF_MEDIA_BITRATE_STEP 5
154 /* Middle quality quality setting @ 44.1 khz */
155 #define DEFAULT_SBC_BITRATE 328
157 #ifndef BTIF_A2DP_NON_EDR_MAX_RATE
158 #define BTIF_A2DP_NON_EDR_MAX_RATE 229
161 #define USEC_PER_SEC 1000000L
162 #define TPUT_STATS_INTERVAL_US (3000*1000)
165 * CONGESTION COMPENSATION CTRL ::
167 * Thus setting controls how many buffers we will hold in media task
168 * during temp link congestion. Together with the stack buffer queues
169 * it controls much temporary a2dp link congestion we can
170 * compensate for. It however also depends on the default run level of sinks
171 * jitterbuffers. Depending on type of sink this would vary.
172 * Ideally the (SRC) max tx buffer capacity should equal the sinks
173 * jitterbuffer runlevel including any intermediate buffers on the way
174 * towards the sinks codec.
177 /* fixme -- define this in pcm time instead of buffer count */
179 /* The typical runlevel of the tx queue size is ~1 buffer
180 but due to link flow control or thread preemption in lower
181 layers we might need to temporarily buffer up data */
183 /* 18 frames is equivalent to 6.89*18*2.9 ~= 360 ms @ 44.1 khz, 20 ms mediatick */
184 #define MAX_OUTPUT_A2DP_FRAME_QUEUE_SZ 18
186 #ifndef MAX_PCM_FRAME_NUM_PER_TICK
187 #define MAX_PCM_FRAME_NUM_PER_TICK 14
190 /* In case of A2DP SINK, we will delay start by 5 AVDTP Packets*/
191 #define MAX_A2DP_DELAYED_START_FRAME_COUNT 5
192 #define PACKET_PLAYED_PER_TICK_48 8
193 #define PACKET_PLAYED_PER_TICK_44 7
194 #define PACKET_PLAYED_PER_TICK_32 5
195 #define PACKET_PLAYED_PER_TICK_16 3
198 UINT16 num_frames_to_be_processed;
201 UINT16 layer_specific;
205 UINT32 aa_frame_counter;
206 INT32 aa_feed_counter;
207 INT32 aa_feed_residue;
209 UINT32 bytes_per_tick; /* pcm bytes read each media task tick */
210 } tBTIF_AV_MEDIA_FEEDINGS_PCM_STATE;
213 tBTIF_AV_MEDIA_FEEDINGS_PCM_STATE pcm;
214 } tBTIF_AV_MEDIA_FEEDINGS_STATE;
217 #if (BTA_AV_INCLUDED == TRUE)
223 UINT8 a2dp_cmd_pending; /* we can have max one command pending */
224 BOOLEAN rx_flush; /* discards any incoming data when true */
226 BOOLEAN data_channel_open;
227 UINT8 frames_to_process;
231 osi_alarm_t *decode_alarm;
241 long long ts_prev_us;
244 #if (BTA_AV_SINK_INCLUDED == TRUE)
245 extern OI_STATUS OI_CODEC_SBC_DecodeFrame(OI_CODEC_SBC_DECODER_CONTEXT *context,
246 const OI_BYTE **frameData,
247 unsigned long *frameBytes,
249 unsigned long *pcmBytes);
250 extern OI_STATUS OI_CODEC_SBC_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
251 unsigned long *decoderData,
252 unsigned long decoderDataBytes,
253 OI_UINT8 maxChannels,
257 static void btif_media_flush_q(BUFFER_Q *p_q);
258 static void btif_media_task_aa_handle_stop_decoding(void );
259 static void btif_media_task_aa_rx_flush(void);
261 static const char *dump_media_event(UINT16 event);
262 static void btif_media_thread_handle_cmd(fixed_queue_t *queue);
264 /* Handle incoming media packets A2DP SINK streaming*/
265 #if (BTA_AV_SINK_INCLUDED == TRUE)
266 static void btif_media_task_handle_inc_media(tBT_SBC_HDR *p_msg);
269 #if (BTA_AV_INCLUDED == TRUE)
270 #if (BTA_AV_SINK_INCLUDED == TRUE)
271 static void btif_media_task_aa_handle_decoder_reset(BT_HDR *p_msg);
272 static void btif_media_task_aa_handle_clear_track(void);
274 static void btif_media_task_aa_handle_start_decoding(void);
276 BOOLEAN btif_media_task_clear_track(void);
277 static void btif_media_task_handler(void *arg);
279 static void btif_media_task_avk_handle_timer(UNUSED_ATTR void *context);
280 static void btif_media_thread_init(UNUSED_ATTR void *context);
281 static void btif_media_thread_cleanup(UNUSED_ATTR void *context);
282 extern BOOLEAN btif_hf_is_call_idle();
284 static tBTIF_MEDIA_CB btif_media_cb;
285 static int media_task_running = MEDIA_TASK_STATE_OFF;
287 static fixed_queue_t *btif_media_cmd_msg_queue = NULL;
288 static xTaskHandle xBtifMediaTaskHandle = NULL;
289 static QueueHandle_t xBtifMediaQueue = NULL;
291 static esp_a2d_data_cb_t bt_av_sink_data_callback = NULL;
293 esp_err_t esp_a2d_register_data_callback(esp_a2d_data_cb_t cb)
295 // TODO: need protection against race
296 bt_av_sink_data_callback = cb;
299 // TODO: need protection against race
300 #define BTIF_A2D_DATA_CB_TO_APP(data, len) do { \
301 if (bt_av_sink_data_callback) { \
302 bt_av_sink_data_callback(data, len); \
306 /*****************************************************************************
307 ** temporary hacked functions. TODO: port these functions or remove them?
308 *****************************************************************************/
309 BOOLEAN btif_hf_is_call_idle(void)
314 /*****************************************************************************
315 ** Misc helper functions
316 *****************************************************************************/
318 UNUSED_ATTR static const char *dump_media_event(UINT16 event)
321 CASE_RETURN_STR(BTIF_MEDIA_START_AA_TX)
322 CASE_RETURN_STR(BTIF_MEDIA_STOP_AA_TX)
323 CASE_RETURN_STR(BTIF_MEDIA_AA_RX_RDY)
324 CASE_RETURN_STR(BTIF_MEDIA_UIPC_RX_RDY)
325 CASE_RETURN_STR(BTIF_MEDIA_SBC_ENC_INIT)
326 CASE_RETURN_STR(BTIF_MEDIA_SBC_ENC_UPDATE)
327 CASE_RETURN_STR(BTIF_MEDIA_SBC_DEC_INIT)
328 CASE_RETURN_STR(BTIF_MEDIA_VIDEO_DEC_INIT)
329 CASE_RETURN_STR(BTIF_MEDIA_FLUSH_AA_TX)
330 CASE_RETURN_STR(BTIF_MEDIA_FLUSH_AA_RX)
331 CASE_RETURN_STR(BTIF_MEDIA_AUDIO_FEEDING_INIT)
332 CASE_RETURN_STR(BTIF_MEDIA_AUDIO_RECEIVING_INIT)
333 CASE_RETURN_STR(BTIF_MEDIA_AUDIO_SINK_CFG_UPDATE)
334 CASE_RETURN_STR(BTIF_MEDIA_AUDIO_SINK_CLEAR_TRACK)
337 return "UNKNOWN MEDIA EVENT";
341 /*****************************************************************************
343 *****************************************************************************/
345 // TODO: consider the necessity to add an API based on this function
346 static void btif_audiopath_detached(void)
348 APPL_TRACE_EVENT("## AUDIO PATH DETACHED ##");
350 /* send stop request only if we are actively streaming and haven't received
351 a stop request. Potentially audioflinger detached abnormally */
352 if (btif_media_cb.is_tx_timer) {
353 /* post stop event and wait for audio path to stop */
354 btif_dispatch_sm_event(BTIF_AV_STOP_STREAM_REQ_EVT, NULL, 0);
359 /*****************************************************************************
361 *****************************************************************************/
363 static void btif_media_task_post(uint32_t sig)
365 BtTaskEvt_t *evt = (BtTaskEvt_t *)osi_malloc(sizeof(BtTaskEvt_t));
373 if (xQueueSend(xBtifMediaQueue, &evt, 10 / portTICK_RATE_MS) != pdTRUE) {
374 APPL_TRACE_ERROR("xBtifMediaQueue failed\n");
378 static void btif_media_task_handler(void *arg)
382 if (pdTRUE == xQueueReceive(xBtifMediaQueue, &e, (portTickType)portMAX_DELAY)) {
383 // LOG_ERROR("med evt %d\n", e->sig);
385 case SIG_MEDIA_TASK_AVK_ALARM_TO:
386 btif_media_task_avk_handle_timer(NULL);
388 case SIG_MEDIA_TASK_CMD_READY:
389 fixed_queue_process(btif_media_cmd_msg_queue);
391 case SIG_MEDIA_TASK_INIT:
392 btif_media_thread_init(NULL);
394 case SIG_MEDIA_TASK_CLEAN_UP:
395 btif_media_thread_cleanup(NULL);
398 APPL_TRACE_ERROR("media task unhandled evt: 0x%x\n", e->sig);
405 bool btif_a2dp_start_media_task(void)
407 if (media_task_running != MEDIA_TASK_STATE_OFF) {
408 APPL_TRACE_ERROR("warning : media task already running");
412 APPL_TRACE_EVENT("## A2DP START MEDIA THREAD ##");
414 btif_media_cmd_msg_queue = fixed_queue_new(SIZE_MAX);
415 if (btif_media_cmd_msg_queue == NULL) {
419 xBtifMediaQueue = xQueueCreate(60, sizeof(void *));
420 if (xBtifMediaQueue == 0) {
423 xTaskCreate(btif_media_task_handler, "BtifMediaT\n", 2048, NULL, configMAX_PRIORITIES - 1, &xBtifMediaTaskHandle);
424 if (xBtifMediaTaskHandle == NULL) {
427 fixed_queue_register_dequeue(btif_media_cmd_msg_queue, btif_media_thread_handle_cmd);
428 btif_media_task_post(SIG_MEDIA_TASK_INIT);
430 APPL_TRACE_EVENT("## A2DP MEDIA THREAD STARTED ##\n");
435 APPL_TRACE_ERROR("%s unable to start up media thread\n", __func__);
437 if (xBtifMediaTaskHandle != NULL) {
438 vTaskDelete(xBtifMediaTaskHandle);
439 xBtifMediaTaskHandle = NULL;
442 if (xBtifMediaQueue != 0) {
443 vQueueDelete(xBtifMediaQueue);
446 fixed_queue_free(btif_media_cmd_msg_queue, NULL);
447 btif_media_cmd_msg_queue = NULL;
451 void btif_a2dp_stop_media_task(void)
453 APPL_TRACE_EVENT("## A2DP STOP MEDIA THREAD ##\n");
456 btif_media_task_post(SIG_MEDIA_TASK_CLEAN_UP);
457 // TODO: wait until CLEAN up is done, then do task delete
458 vTaskDelete(xBtifMediaTaskHandle);
459 xBtifMediaTaskHandle = NULL;
460 vQueueDelete(xBtifMediaQueue);
461 xBtifMediaQueue = NULL;
463 fixed_queue_free(btif_media_cmd_msg_queue, NULL);
464 btif_media_cmd_msg_queue = NULL;
467 /*****************************************************************************
469 ** Function btif_a2dp_on_init
475 *******************************************************************************/
477 void btif_a2dp_on_init(void)
483 /*****************************************************************************
485 ** Function btif_a2dp_setup_codec
491 *******************************************************************************/
493 void btif_a2dp_setup_codec(void)
495 tBTIF_AV_MEDIA_FEEDINGS media_feeding;
498 APPL_TRACE_EVENT("## A2DP SETUP CODEC ##\n");
502 /* for now hardcode 44.1 khz 16 bit stereo PCM format */
503 media_feeding.cfg.pcm.sampling_freq = 44100;
504 media_feeding.cfg.pcm.bit_per_sample = 16;
505 media_feeding.cfg.pcm.num_channel = 2;
506 media_feeding.format = BTIF_AV_CODEC_PCM;
508 bta_av_co_audio_set_codec(&media_feeding, &status);
514 /*****************************************************************************
516 ** Function btif_a2dp_on_idle
522 *******************************************************************************/
524 void btif_a2dp_on_idle(void)
526 APPL_TRACE_EVENT("## ON A2DP IDLE ##\n");
529 #if (BTA_AV_SINK_INCLUDED == TRUE)
530 if (btif_media_cb.peer_sep == AVDT_TSEP_SRC) {
531 btif_media_cb.rx_flush = TRUE;
532 btif_media_task_aa_rx_flush_req();
533 btif_media_task_aa_handle_stop_decoding();
534 btif_media_task_clear_track();
535 APPL_TRACE_DEBUG("Stopped BT track");
540 /*******************************************************************************
542 ** Function btif_media_task_clear_track
546 ** Returns TRUE is success
548 *******************************************************************************/
549 BOOLEAN btif_media_task_clear_track(void)
553 if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR)))) {
557 p_buf->event = BTIF_MEDIA_AUDIO_SINK_CLEAR_TRACK;
559 fixed_queue_enqueue(btif_media_cmd_msg_queue, p_buf);
560 btif_media_task_post(SIG_MEDIA_TASK_CMD_READY);
564 /*****************************************************************************
566 ** Function btif_reset_decoder
572 *******************************************************************************/
574 void btif_reset_decoder(UINT8 *p_av)
576 APPL_TRACE_EVENT("btif_reset_decoder");
577 APPL_TRACE_DEBUG("btif_reset_decoder p_codec_info[%x:%x:%x:%x:%x:%x]\n",
578 p_av[1], p_av[2], p_av[3],
579 p_av[4], p_av[5], p_av[6]);
581 tBTIF_MEDIA_SINK_CFG_UPDATE *p_buf;
582 if (NULL == (p_buf = GKI_getbuf(sizeof(tBTIF_MEDIA_SINK_CFG_UPDATE)))) {
583 APPL_TRACE_ERROR("btif_reset_decoder No Buffer ");
587 memcpy(p_buf->codec_info, p_av, AVDT_CODEC_SIZE);
588 p_buf->hdr.event = BTIF_MEDIA_AUDIO_SINK_CFG_UPDATE;
590 fixed_queue_enqueue(btif_media_cmd_msg_queue, p_buf);
591 btif_media_task_post(SIG_MEDIA_TASK_CMD_READY);
594 /*****************************************************************************
596 ** Function btif_a2dp_on_stopped
602 *******************************************************************************/
604 void btif_a2dp_on_stopped(tBTA_AV_SUSPEND *p_av)
606 APPL_TRACE_EVENT("## ON A2DP STOPPED ##\n");
607 if (btif_media_cb.peer_sep == AVDT_TSEP_SRC) { /* Handling for A2DP SINK cases*/
608 btif_media_cb.rx_flush = TRUE;
609 btif_media_task_aa_rx_flush_req();
610 btif_media_task_aa_handle_stop_decoding();
611 btif_media_cb.data_channel_open = FALSE;
617 /*****************************************************************************
619 ** Function btif_a2dp_on_suspended
625 *******************************************************************************/
627 void btif_a2dp_on_suspended(tBTA_AV_SUSPEND *p_av)
629 APPL_TRACE_EVENT("## ON A2DP SUSPENDED ##\n");
630 if (btif_media_cb.peer_sep == AVDT_TSEP_SRC) {
631 btif_media_cb.rx_flush = TRUE;
632 btif_media_task_aa_rx_flush_req();
633 btif_media_task_aa_handle_stop_decoding();
638 /* when true media task discards any rx frames */
639 void btif_a2dp_set_rx_flush(BOOLEAN enable)
641 APPL_TRACE_EVENT("## DROP RX %d ##\n", enable);
642 btif_media_cb.rx_flush = enable;
645 #if (BTA_AV_SINK_INCLUDED == TRUE)
646 static void btif_media_task_avk_handle_timer(UNUSED_ATTR void *context)
651 int num_frames_to_process;
653 count = btif_media_cb.RxSbcQ._count;
655 APPL_TRACE_DEBUG(" QUE EMPTY ");
657 if (btif_media_cb.rx_flush == TRUE) {
658 btif_media_flush_q(&(btif_media_cb.RxSbcQ));
662 num_frames_to_process = btif_media_cb.frames_to_process;
663 APPL_TRACE_DEBUG(" Process Frames + ");
665 p_msg = (tBT_SBC_HDR *)GKI_getfirst(&(btif_media_cb.RxSbcQ));
669 num_sbc_frames = p_msg->num_frames_to_be_processed; /* num of frames in Que Packets */
670 APPL_TRACE_DEBUG(" Frames left in topmost packet %d\n", num_sbc_frames);
671 APPL_TRACE_DEBUG(" Remaining frames to process in tick %d\n", num_frames_to_process);
672 APPL_TRACE_DEBUG(" Num of Packets in Que %d\n", btif_media_cb.RxSbcQ._count);
674 if ( num_sbc_frames > num_frames_to_process) { /* Que Packet has more frames*/
675 p_msg->num_frames_to_be_processed = num_frames_to_process;
676 btif_media_task_handle_inc_media(p_msg);
677 p_msg->num_frames_to_be_processed = num_sbc_frames - num_frames_to_process;
678 num_frames_to_process = 0;
680 } else { /* Que packet has less frames */
681 btif_media_task_handle_inc_media(p_msg);
682 p_msg = (tBT_SBC_HDR *)GKI_dequeue(&(btif_media_cb.RxSbcQ));
683 if ( p_msg == NULL ) {
684 APPL_TRACE_ERROR("Insufficient data in que ");
687 num_frames_to_process = num_frames_to_process - p_msg->num_frames_to_be_processed;
690 } while (num_frames_to_process > 0);
692 APPL_TRACE_DEBUG(" Process Frames - ");
696 static void btif_media_task_avk_handle_timer(UNUSED_ATTR void *context) {}
699 static void btif_media_thread_init(UNUSED_ATTR void *context)
701 memset(&btif_media_cb, 0, sizeof(btif_media_cb));
702 LOG_ERROR("med thread init\n");
703 #if (UIPC_INCLUDED == TRUE)
706 #if (BTA_AV_INCLUDED == TRUE)
707 UIPC_Open(UIPC_CH_ID_AV_CTRL , btif_a2dp_ctrl_cb);
708 #endif ( BTA_AV_INCLUDED == TRUE)
709 #endif /* UIPC_INCLUDED == TRUE */
710 btif_media_cb.av_sm_hdl = btif_av_get_sm_handle();
711 raise_priority_a2dp(TASK_HIGH_MEDIA);
712 media_task_running = MEDIA_TASK_STATE_ON;
715 static void btif_media_thread_cleanup(UNUSED_ATTR void *context)
717 /* make sure no channels are restarted while shutting down */
718 media_task_running = MEDIA_TASK_STATE_SHUTTING_DOWN;
720 btif_media_cb.data_channel_open = FALSE;
721 /* Clear media task flag */
722 media_task_running = MEDIA_TASK_STATE_OFF;
725 /*******************************************************************************
727 ** Function btif_media_flush_q
733 *******************************************************************************/
734 static void btif_media_flush_q(BUFFER_Q *p_q)
736 while (!GKI_queue_is_empty(p_q)) {
737 GKI_freebuf(GKI_dequeue(p_q));
741 static void btif_media_thread_handle_cmd(fixed_queue_t *queue)
744 while (!fixed_queue_is_empty(queue)) {
745 p_msg = (BT_HDR *)fixed_queue_dequeue(queue);
746 LOG_VERBOSE("btif_media_thread_handle_cmd : %d %s\n", p_msg->event,
747 dump_media_event(p_msg->event));
749 switch (p_msg->event) {
750 #if (BTA_AV_INCLUDED == TRUE)
751 case BTIF_MEDIA_AUDIO_SINK_CFG_UPDATE:
752 #if (BTA_AV_SINK_INCLUDED == TRUE)
753 btif_media_task_aa_handle_decoder_reset(p_msg);
756 case BTIF_MEDIA_AUDIO_SINK_CLEAR_TRACK:
757 #if (BTA_AV_SINK_INCLUDED == TRUE)
758 btif_media_task_aa_handle_clear_track();
761 case BTIF_MEDIA_FLUSH_AA_RX:
762 btif_media_task_aa_rx_flush();
766 APPL_TRACE_ERROR("ERROR in %s unknown event %d\n", __func__, p_msg->event);
769 LOG_VERBOSE("%s: %s DONE\n", __func__, dump_media_event(p_msg->event));
774 #if (BTA_AV_SINK_INCLUDED == TRUE)
775 /*******************************************************************************
777 ** Function btif_media_task_handle_inc_media
783 *******************************************************************************/
784 static void btif_media_task_handle_inc_media(tBT_SBC_HDR *p_msg)
786 UINT8 *sbc_start_frame = ((UINT8 *)(p_msg + 1) + p_msg->offset + 1);
788 UINT32 pcmBytes, availPcmBytes;
789 OI_INT16 *pcmDataPointer = pcmData; /*Will be overwritten on next packet receipt*/
791 int num_sbc_frames = p_msg->num_frames_to_be_processed;
792 UINT32 sbc_frame_len = p_msg->len - 1;
793 availPcmBytes = 2 * sizeof(pcmData);
795 if ((btif_media_cb.peer_sep == AVDT_TSEP_SNK) || (btif_media_cb.rx_flush)) {
796 APPL_TRACE_DEBUG(" State Changed happened in this tick ");
800 // ignore data if no one is listening
801 if (!btif_media_cb.data_channel_open) {
805 APPL_TRACE_DEBUG("Number of sbc frames %d, frame_len %d\n", num_sbc_frames, sbc_frame_len);
806 // LOG_ERROR("Number of sbc frames %d, frame_len %d\n", num_sbc_frames, sbc_frame_len);
807 for (count = 0; count < num_sbc_frames && sbc_frame_len != 0; count ++) {
808 pcmBytes = availPcmBytes;
809 status = OI_CODEC_SBC_DecodeFrame(&context, (const OI_BYTE **)&sbc_start_frame,
810 (OI_UINT32 *)&sbc_frame_len,
811 (OI_INT16 *)pcmDataPointer,
812 (OI_UINT32 *)&pcmBytes);
813 if (!OI_SUCCESS(status)) {
814 APPL_TRACE_ERROR("Decoding failure: %d\n", status);
817 availPcmBytes -= pcmBytes;
818 pcmDataPointer += pcmBytes / 2;
819 p_msg->offset += (p_msg->len - 1) - sbc_frame_len;
820 p_msg->len = sbc_frame_len + 1;
822 // LOG_ERROR("pre-send: %d\n", availPcmBytes);
824 // UIPC_Send(UIPC_CH_ID_AV_AUDIO, 0, (UINT8 *)pcmData, (2 * sizeof(pcmData) - availPcmBytes));
825 BTIF_A2D_DATA_CB_TO_APP((uint8_t *)pcmData, (2 * sizeof(pcmData) - availPcmBytes));
830 #if (BTA_AV_INCLUDED == TRUE)
832 /*******************************************************************************
834 ** Function btif_media_task_aa_rx_flush_req
838 ** Returns TRUE is success
840 *******************************************************************************/
841 BOOLEAN btif_media_task_aa_rx_flush_req(void)
845 if (GKI_queue_is_empty(&(btif_media_cb.RxSbcQ)) == TRUE) { /* Que is already empty */
849 if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR)))) {
853 p_buf->event = BTIF_MEDIA_FLUSH_AA_RX;
855 fixed_queue_enqueue(btif_media_cmd_msg_queue, p_buf);
856 btif_media_task_post(SIG_MEDIA_TASK_CMD_READY);
860 /*******************************************************************************
862 ** Function btif_media_task_aa_rx_flush
868 *******************************************************************************/
869 static void btif_media_task_aa_rx_flush(void)
871 /* Flush all enqueued GKI SBC buffers (encoded) */
872 APPL_TRACE_DEBUG("btif_media_task_aa_rx_flush");
874 btif_media_flush_q(&(btif_media_cb.RxSbcQ));
877 int btif_a2dp_get_track_frequency(UINT8 frequency)
881 case A2D_SBC_IE_SAMP_FREQ_16:
884 case A2D_SBC_IE_SAMP_FREQ_32:
887 case A2D_SBC_IE_SAMP_FREQ_44:
890 case A2D_SBC_IE_SAMP_FREQ_48:
897 int btif_a2dp_get_track_channel_count(UINT8 channeltype)
900 switch (channeltype) {
901 case A2D_SBC_IE_CH_MD_MONO:
904 case A2D_SBC_IE_CH_MD_DUAL:
905 case A2D_SBC_IE_CH_MD_STEREO:
906 case A2D_SBC_IE_CH_MD_JOINT:
913 void btif_a2dp_set_peer_sep(UINT8 sep)
915 btif_media_cb.peer_sep = sep;
918 static void btif_decode_alarm_cb(UNUSED_ATTR void *context)
920 btif_media_task_post(SIG_MEDIA_TASK_AVK_ALARM_TO);
923 static void btif_media_task_aa_handle_stop_decoding(void)
925 osi_alarm_free(btif_media_cb.decode_alarm);
926 btif_media_cb.decode_alarm = NULL;
929 static void btif_media_task_aa_handle_start_decoding(void)
931 if (btif_media_cb.decode_alarm) {
935 btif_media_cb.decode_alarm = osi_alarm_new("dec_timer\n", btif_decode_alarm_cb, NULL, BTIF_SINK_MEDIA_TIME_TICK, true);
936 if (!btif_media_cb.decode_alarm) {
937 APPL_TRACE_ERROR("%s unable to allocate decode alarm.\n", __func__);
940 osi_alarm_set(btif_media_cb.decode_alarm, BTIF_SINK_MEDIA_TIME_TICK);
943 #if (BTA_AV_SINK_INCLUDED == TRUE)
945 static void btif_media_task_aa_handle_clear_track (void)
947 APPL_TRACE_DEBUG("btif_media_task_aa_handle_clear_track");
950 /*******************************************************************************
952 ** Function btif_media_task_aa_handle_decoder_reset
958 *******************************************************************************/
959 static void btif_media_task_aa_handle_decoder_reset(BT_HDR *p_msg)
961 tBTIF_MEDIA_SINK_CFG_UPDATE *p_buf = (tBTIF_MEDIA_SINK_CFG_UPDATE *) p_msg;
962 tA2D_STATUS a2d_status;
963 tA2D_SBC_CIE sbc_cie;
965 UINT32 freq_multiple = 48 * 20; /* frequency multiple for 20ms of data , initialize with 48K*/
966 UINT32 num_blocks = 16;
967 UINT32 num_subbands = 8;
969 APPL_TRACE_EVENT("btif_media_task_aa_handle_decoder_reset p_codec_info[%x:%x:%x:%x:%x:%x]\n",
970 p_buf->codec_info[1], p_buf->codec_info[2], p_buf->codec_info[3],
971 p_buf->codec_info[4], p_buf->codec_info[5], p_buf->codec_info[6]);
973 a2d_status = A2D_ParsSbcInfo(&sbc_cie, p_buf->codec_info, FALSE);
974 if (a2d_status != A2D_SUCCESS) {
975 APPL_TRACE_ERROR("ERROR dump_codec_info A2D_ParsSbcInfo fail:%d\n", a2d_status);
979 btif_media_cb.sample_rate = btif_a2dp_get_track_frequency(sbc_cie.samp_freq);
980 btif_media_cb.channel_count = btif_a2dp_get_track_channel_count(sbc_cie.ch_mode);
982 btif_media_cb.rx_flush = FALSE;
983 APPL_TRACE_EVENT("Reset to sink role");
984 status = OI_CODEC_SBC_DecoderReset(&context, contextData, sizeof(contextData), 2, 2, FALSE);
985 if (!OI_SUCCESS(status)) {
986 APPL_TRACE_ERROR("OI_CODEC_SBC_DecoderReset failed with error code %d\n", status);
989 btif_media_cb.data_channel_open = TRUE;
991 switch (sbc_cie.samp_freq) {
992 case A2D_SBC_IE_SAMP_FREQ_16:
993 APPL_TRACE_DEBUG("\tsamp_freq:%d (16000)\n", sbc_cie.samp_freq);
994 freq_multiple = 16 * 20;
996 case A2D_SBC_IE_SAMP_FREQ_32:
997 APPL_TRACE_DEBUG("\tsamp_freq:%d (32000)\n", sbc_cie.samp_freq);
998 freq_multiple = 32 * 20;
1000 case A2D_SBC_IE_SAMP_FREQ_44:
1001 APPL_TRACE_DEBUG("\tsamp_freq:%d (44100)\n", sbc_cie.samp_freq);
1002 freq_multiple = 441 * 2;
1004 case A2D_SBC_IE_SAMP_FREQ_48:
1005 APPL_TRACE_DEBUG("\tsamp_freq:%d (48000)\n", sbc_cie.samp_freq);
1006 freq_multiple = 48 * 20;
1009 APPL_TRACE_DEBUG(" Unknown Frequency ");
1013 switch (sbc_cie.ch_mode) {
1014 case A2D_SBC_IE_CH_MD_MONO:
1015 APPL_TRACE_DEBUG("\tch_mode:%d (Mono)\n", sbc_cie.ch_mode);
1017 case A2D_SBC_IE_CH_MD_DUAL:
1018 APPL_TRACE_DEBUG("\tch_mode:%d (DUAL)\n", sbc_cie.ch_mode);
1020 case A2D_SBC_IE_CH_MD_STEREO:
1021 APPL_TRACE_DEBUG("\tch_mode:%d (STEREO)\n", sbc_cie.ch_mode);
1023 case A2D_SBC_IE_CH_MD_JOINT:
1024 APPL_TRACE_DEBUG("\tch_mode:%d (JOINT)\n", sbc_cie.ch_mode);
1027 APPL_TRACE_DEBUG(" Unknown Mode ");
1031 switch (sbc_cie.block_len) {
1032 case A2D_SBC_IE_BLOCKS_4:
1033 APPL_TRACE_DEBUG("\tblock_len:%d (4)\n", sbc_cie.block_len);
1036 case A2D_SBC_IE_BLOCKS_8:
1037 APPL_TRACE_DEBUG("\tblock_len:%d (8)\n", sbc_cie.block_len);
1040 case A2D_SBC_IE_BLOCKS_12:
1041 APPL_TRACE_DEBUG("\tblock_len:%d (12)\n", sbc_cie.block_len);
1044 case A2D_SBC_IE_BLOCKS_16:
1045 APPL_TRACE_DEBUG("\tblock_len:%d (16)\n", sbc_cie.block_len);
1049 APPL_TRACE_DEBUG(" Unknown BlockLen ");
1053 switch (sbc_cie.num_subbands) {
1054 case A2D_SBC_IE_SUBBAND_4:
1055 APPL_TRACE_DEBUG("\tnum_subbands:%d (4)\n", sbc_cie.num_subbands);
1058 case A2D_SBC_IE_SUBBAND_8:
1059 APPL_TRACE_DEBUG("\tnum_subbands:%d (8)\n", sbc_cie.num_subbands);
1063 APPL_TRACE_DEBUG(" Unknown SubBands ");
1067 switch (sbc_cie.alloc_mthd) {
1068 case A2D_SBC_IE_ALLOC_MD_S:
1069 APPL_TRACE_DEBUG("\talloc_mthd:%d (SNR)\n", sbc_cie.alloc_mthd);
1071 case A2D_SBC_IE_ALLOC_MD_L:
1072 APPL_TRACE_DEBUG("\talloc_mthd:%d (Loudness)\n", sbc_cie.alloc_mthd);
1075 APPL_TRACE_DEBUG(" Unknown Allocation Method");
1079 APPL_TRACE_EVENT("\tBit pool Min:%d Max:%d\n", sbc_cie.min_bitpool, sbc_cie.max_bitpool);
1081 btif_media_cb.frames_to_process = ((freq_multiple) / (num_blocks * num_subbands)) + 1;
1082 APPL_TRACE_EVENT(" Frames to be processed in 20 ms %d\n", btif_media_cb.frames_to_process);
1086 /*******************************************************************************
1088 ** Function btif_media_sink_enque_buf
1090 ** Description This function is called by the av_co to fill A2DP Sink Queue
1093 ** Returns size of the queue
1094 *******************************************************************************/
1095 UINT8 btif_media_sink_enque_buf(BT_HDR *p_pkt)
1099 if (btif_media_cb.rx_flush == TRUE) { /* Flush enabled, do not enque*/
1100 return GKI_queue_length(&btif_media_cb.RxSbcQ);
1102 if (GKI_queue_length(&btif_media_cb.RxSbcQ) == MAX_OUTPUT_A2DP_FRAME_QUEUE_SZ) {
1103 GKI_freebuf(GKI_dequeue(&(btif_media_cb.RxSbcQ)));
1106 BTIF_TRACE_VERBOSE("btif_media_sink_enque_buf + ");
1108 /* allocate and Queue this buffer */
1109 if ((p_msg = (tBT_SBC_HDR *) GKI_getbuf(sizeof(tBT_SBC_HDR) +
1110 p_pkt->offset + p_pkt->len)) != NULL) {
1111 memcpy(p_msg, p_pkt, (sizeof(BT_HDR) + p_pkt->offset + p_pkt->len));
1112 p_msg->num_frames_to_be_processed = (*((UINT8 *)(p_msg + 1) + p_msg->offset)) & 0x0f;
1113 BTIF_TRACE_VERBOSE("btif_media_sink_enque_buf %d + \n", p_msg->num_frames_to_be_processed);
1114 // LOG_ERROR("sink enq %d\n", p_msg->num_frames_to_be_processed);
1115 GKI_enqueue(&(btif_media_cb.RxSbcQ), p_msg);
1116 if (GKI_queue_length(&btif_media_cb.RxSbcQ) == MAX_A2DP_DELAYED_START_FRAME_COUNT) {
1117 BTIF_TRACE_DEBUG(" Initiate Decoding ");
1118 btif_media_task_aa_handle_start_decoding();
1121 /* let caller deal with a failed allocation */
1122 BTIF_TRACE_VERBOSE("btif_media_sink_enque_buf No Buffer left - ");
1124 return GKI_queue_length(&btif_media_cb.RxSbcQ);
1128 /*******************************************************************************
1130 ** Function btif_media_aa_readbuf
1132 ** Description This function is called by the av_co to get the next buffer to send
1136 *******************************************************************************/
1137 BT_HDR *btif_media_aa_readbuf(void)
1142 #endif /* BTA_AV_INCLUDED == TRUE */
1143 /*******************************************************************************
1145 ** Function dump_codec_info
1147 ** Description Decode and display codec_info (for debug)
1151 *******************************************************************************/
1152 void dump_codec_info(unsigned char *p_codec)
1154 tA2D_STATUS a2d_status;
1155 tA2D_SBC_CIE sbc_cie;
1157 a2d_status = A2D_ParsSbcInfo(&sbc_cie, p_codec, FALSE);
1158 if (a2d_status != A2D_SUCCESS) {
1159 APPL_TRACE_ERROR("ERROR dump_codec_info A2D_ParsSbcInfo fail:%d\n", a2d_status);
1163 APPL_TRACE_DEBUG("dump_codec_info");
1165 if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_16) {
1166 APPL_TRACE_DEBUG("\tsamp_freq:%d (16000)\n", sbc_cie.samp_freq);
1167 } else if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_32) {
1168 APPL_TRACE_DEBUG("\tsamp_freq:%d (32000)\n", sbc_cie.samp_freq);
1169 } else if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_44) {
1170 APPL_TRACE_DEBUG("\tsamp_freq:%d (44.100)\n", sbc_cie.samp_freq);
1171 } else if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_48) {
1172 APPL_TRACE_DEBUG("\tsamp_freq:%d (48000)\n", sbc_cie.samp_freq);
1174 APPL_TRACE_DEBUG("\tBAD samp_freq:%d\n", sbc_cie.samp_freq);
1177 if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_MONO) {
1178 APPL_TRACE_DEBUG("\tch_mode:%d (Mono)\n", sbc_cie.ch_mode);
1179 } else if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_DUAL) {
1180 APPL_TRACE_DEBUG("\tch_mode:%d (Dual)\n", sbc_cie.ch_mode);
1181 } else if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_STEREO) {
1182 APPL_TRACE_DEBUG("\tch_mode:%d (Stereo)\n", sbc_cie.ch_mode);
1183 } else if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_JOINT) {
1184 APPL_TRACE_DEBUG("\tch_mode:%d (Joint)\n", sbc_cie.ch_mode);
1186 APPL_TRACE_DEBUG("\tBAD ch_mode:%d\n", sbc_cie.ch_mode);
1189 if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_4) {
1190 APPL_TRACE_DEBUG("\tblock_len:%d (4)\n", sbc_cie.block_len);
1191 } else if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_8) {
1192 APPL_TRACE_DEBUG("\tblock_len:%d (8)\n", sbc_cie.block_len);
1193 } else if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_12) {
1194 APPL_TRACE_DEBUG("\tblock_len:%d (12)\n", sbc_cie.block_len);
1195 } else if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_16) {
1196 APPL_TRACE_DEBUG("\tblock_len:%d (16)\n", sbc_cie.block_len);
1198 APPL_TRACE_DEBUG("\tBAD block_len:%d\n", sbc_cie.block_len);
1201 if (sbc_cie.num_subbands == A2D_SBC_IE_SUBBAND_4) {
1202 APPL_TRACE_DEBUG("\tnum_subbands:%d (4)\n", sbc_cie.num_subbands);
1203 } else if (sbc_cie.num_subbands == A2D_SBC_IE_SUBBAND_8) {
1204 APPL_TRACE_DEBUG("\tnum_subbands:%d (8)\n", sbc_cie.num_subbands);
1206 APPL_TRACE_DEBUG("\tBAD num_subbands:%d\n", sbc_cie.num_subbands);
1209 if (sbc_cie.alloc_mthd == A2D_SBC_IE_ALLOC_MD_S) {
1210 APPL_TRACE_DEBUG("\talloc_mthd:%d (SNR)\n", sbc_cie.alloc_mthd);
1211 } else if (sbc_cie.alloc_mthd == A2D_SBC_IE_ALLOC_MD_L) {
1212 APPL_TRACE_DEBUG("\talloc_mthd:%d (Loundess)\n", sbc_cie.alloc_mthd);
1214 APPL_TRACE_DEBUG("\tBAD alloc_mthd:%d\n", sbc_cie.alloc_mthd);
1217 APPL_TRACE_DEBUG("\tBit pool Min:%d Max:%d\n", sbc_cie.min_bitpool, sbc_cie.max_bitpool);