• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2012 Broadcom Corporation
4  *
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:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  **
21  **  Name:          btif_media_task.c
22  **
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
26  **
27  ******************************************************************************/
28 
29 #include <string.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <pthread.h>
36 #include <stdint.h>
37 #include <sys/time.h>
38 #include <errno.h>
39 
40 #include "bt_target.h"
41 #include "gki.h"
42 #include "bta_api.h"
43 #include "btu.h"
44 #include "bta_sys.h"
45 #include "bta_sys_int.h"
46 
47 #include "bta_av_api.h"
48 #include "a2d_api.h"
49 #include "a2d_sbc.h"
50 #include "a2d_int.h"
51 #include "bta_av_sbc.h"
52 #include "bta_av_ci.h"
53 #include "l2c_api.h"
54 
55 #include "btif_av_co.h"
56 #include "btif_media.h"
57 
58 #if (BTA_AV_INCLUDED == TRUE)
59 #include "sbc_encoder.h"
60 #endif
61 
62 #define LOG_TAG "BTIF-MEDIA"
63 
64 #include <hardware/bluetooth.h>
65 #include "audio_a2dp_hw.h"
66 #include "btif_av.h"
67 #include "btif_sm.h"
68 #include "btif_util.h"
69 #if (BTA_AV_SINK_INCLUDED == TRUE)
70 #include "oi_codec_sbc.h"
71 #include "oi_status.h"
72 #endif
73 #include "stdio.h"
74 #include <dlfcn.h>
75 
76 //#define DEBUG_MEDIA_AV_FLOW TRUE
77 
78 #if (BTA_AV_SINK_INCLUDED == TRUE)
79 OI_CODEC_SBC_DECODER_CONTEXT context;
80 OI_UINT32 contextData[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)];
81 OI_INT16 pcmData[15*SBC_MAX_SAMPLES_PER_FRAME*SBC_MAX_CHANNELS];
82 #endif
83 
84 /*****************************************************************************
85  **  Constants
86  *****************************************************************************/
87 
88 #ifndef AUDIO_CHANNEL_OUT_MONO
89 #define AUDIO_CHANNEL_OUT_MONO 0x01
90 #endif
91 
92 #ifndef AUDIO_CHANNEL_OUT_STEREO
93 #define AUDIO_CHANNEL_OUT_STEREO 0x03
94 #endif
95 
96 /* BTIF media task gki event definition */
97 #define BTIF_MEDIA_TASK_CMD TASK_MBOX_0_EVT_MASK
98 #define BTIF_MEDIA_TASK_DATA TASK_MBOX_1_EVT_MASK
99 
100 #define BTIF_MEDIA_TASK_KILL EVENT_MASK(GKI_SHUTDOWN_EVT)
101 
102 #define BTIF_MEDIA_AA_TASK_TIMER_ID TIMER_0
103 #define BTIF_MEDIA_AV_TASK_TIMER_ID TIMER_1
104 #define BTIF_MEDIA_AVK_TASK_TIMER_ID TIMER_2
105 
106 #define BTIF_MEDIA_AA_TASK_TIMER TIMER_0_EVT_MASK
107 #define BTIF_MEDIA_AV_TASK_TIMER TIMER_1_EVT_MASK
108 #define BTIF_MEDIA_AVK_TASK_TIMER TIMER_2_EVT_MASK
109 
110 
111 #define BTIF_MEDIA_TASK_CMD_MBOX        TASK_MBOX_0     /* cmd mailbox  */
112 #define BTIF_MEDIA_TASK_DATA_MBOX       TASK_MBOX_1     /* data mailbox  */
113 
114 
115 /* BTIF media cmd event definition : BTIF_MEDIA_TASK_CMD */
116 enum
117 {
118     BTIF_MEDIA_START_AA_TX = 1,
119     BTIF_MEDIA_STOP_AA_TX,
120     BTIF_MEDIA_AA_RX_RDY,
121     BTIF_MEDIA_UIPC_RX_RDY,
122     BTIF_MEDIA_SBC_ENC_INIT,
123     BTIF_MEDIA_SBC_ENC_UPDATE,
124     BTIF_MEDIA_SBC_DEC_INIT,
125     BTIF_MEDIA_VIDEO_DEC_INIT,
126     BTIF_MEDIA_FLUSH_AA_TX,
127     BTIF_MEDIA_FLUSH_AA_RX,
128     BTIF_MEDIA_AUDIO_FEEDING_INIT,
129     BTIF_MEDIA_AUDIO_RECEIVING_INIT,
130     BTIF_MEDIA_AUDIO_SINK_CFG_UPDATE,
131     BTIF_MEDIA_AUDIO_SINK_START_DECODING,
132     BTIF_MEDIA_AUDIO_SINK_STOP_DECODING,
133     BTIF_MEDIA_AUDIO_SINK_CLEAR_TRACK
134 };
135 
136 enum {
137     MEDIA_TASK_STATE_OFF = 0,
138     MEDIA_TASK_STATE_ON = 1,
139     MEDIA_TASK_STATE_SHUTTING_DOWN = 2
140 };
141 
142 /* Macro to multiply the media task tick */
143 #ifndef BTIF_MEDIA_NUM_TICK
144 #define BTIF_MEDIA_NUM_TICK      1
145 #endif
146 
147 /* Media task tick in milliseconds, must be set to multiple of
148    (1000/TICKS_PER_SEC) (10) */
149 
150 #define BTIF_MEDIA_TIME_TICK                     (20 * BTIF_MEDIA_NUM_TICK)
151 #define A2DP_DATA_READ_POLL_MS    (BTIF_MEDIA_TIME_TICK / 2)
152 #define BTIF_SINK_MEDIA_TIME_TICK                (20 * BTIF_MEDIA_NUM_TICK)
153 
154 
155 /* buffer pool */
156 #define BTIF_MEDIA_AA_POOL_ID GKI_POOL_ID_3
157 #define BTIF_MEDIA_AA_BUF_SIZE GKI_BUF3_SIZE
158 
159 /* offset */
160 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
161 #define BTIF_MEDIA_AA_SBC_OFFSET (AVDT_MEDIA_OFFSET + BTA_AV_SBC_HDR_SIZE + 1)
162 #else
163 #define BTIF_MEDIA_AA_SBC_OFFSET (AVDT_MEDIA_OFFSET + BTA_AV_SBC_HDR_SIZE)
164 #endif
165 
166 /* Define the bitrate step when trying to match bitpool value */
167 #ifndef BTIF_MEDIA_BITRATE_STEP
168 #define BTIF_MEDIA_BITRATE_STEP 5
169 #endif
170 
171 /* Middle quality quality setting @ 44.1 khz */
172 #define DEFAULT_SBC_BITRATE 328
173 
174 #ifndef BTIF_A2DP_NON_EDR_MAX_RATE
175 #define BTIF_A2DP_NON_EDR_MAX_RATE 229
176 #endif
177 
178 #ifndef A2DP_MEDIA_TASK_STACK_SIZE
179 #define A2DP_MEDIA_TASK_STACK_SIZE       0x2000         /* In bytes */
180 #endif
181 
182 #define A2DP_MEDIA_TASK_TASK_STR        ((INT8 *) "A2DP-MEDIA")
183 static UINT32 a2dp_media_task_stack[(A2DP_MEDIA_TASK_STACK_SIZE + 3) / 4];
184 
185 #define BT_MEDIA_TASK A2DP_MEDIA_TASK
186 
187 #define USEC_PER_SEC 1000000L
188 #define TPUT_STATS_INTERVAL_US (3000*1000)
189 
190 /*
191  * CONGESTION COMPENSATION CTRL ::
192  *
193  * Thus setting controls how many buffers we will hold in media task
194  * during temp link congestion. Together with the stack buffer queues
195  * it controls much temporary a2dp link congestion we can
196  * compensate for. It however also depends on the default run level of sinks
197  * jitterbuffers. Depending on type of sink this would vary.
198  * Ideally the (SRC) max tx buffer capacity should equal the sinks
199  * jitterbuffer runlevel including any intermediate buffers on the way
200  * towards the sinks codec.
201  */
202 
203 /* fixme -- define this in pcm time instead of buffer count */
204 
205 /* The typical runlevel of the tx queue size is ~1 buffer
206    but due to link flow control or thread preemption in lower
207    layers we might need to temporarily buffer up data */
208 
209 /* 18 frames is equivalent to 6.89*18*2.9 ~= 360 ms @ 44.1 khz, 20 ms mediatick */
210 #define MAX_OUTPUT_A2DP_FRAME_QUEUE_SZ 18
211 
212 #ifndef MAX_PCM_FRAME_NUM_PER_TICK
213 #define MAX_PCM_FRAME_NUM_PER_TICK     14
214 #endif
215 
216 //#define BTIF_MEDIA_VERBOSE_ENABLED
217 /* In case of A2DP SINK, we will delay start by 5 AVDTP Packets*/
218 #define MAX_A2DP_DELAYED_START_FRAME_COUNT 5
219 #define PACKET_PLAYED_PER_TICK_48 8
220 #define PACKET_PLAYED_PER_TICK_44 7
221 #define PACKET_PLAYED_PER_TICK_32 5
222 #define PACKET_PLAYED_PER_TICK_16 3
223 
224 
225 #ifdef BTIF_MEDIA_VERBOSE_ENABLED
226 #define VERBOSE(fmt, ...) \
227       LogMsg( TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
228               TRACE_TYPE_ERROR, fmt, ## __VA_ARGS__)
229 #else
230 #define VERBOSE(fmt, ...)
231 #endif
232 
233 /*****************************************************************************
234  **  Data types
235  *****************************************************************************/
236 typedef struct
237 {
238     UINT16 num_frames_to_be_processed;
239     UINT16 len;
240     UINT16 offset;
241     UINT16 layer_specific;
242 } tBT_SBC_HDR;
243 
244 typedef struct
245 {
246     UINT32 aa_frame_counter;
247     INT32  aa_feed_counter;
248     INT32  aa_feed_residue;
249     UINT32 counter;
250     UINT32 bytes_per_tick;  /* pcm bytes read each media task tick */
251 } tBTIF_AV_MEDIA_FEEDINGS_PCM_STATE;
252 
253 
254 typedef union
255 {
256     tBTIF_AV_MEDIA_FEEDINGS_PCM_STATE pcm;
257 } tBTIF_AV_MEDIA_FEEDINGS_STATE;
258 
259 typedef struct
260 {
261 #if (BTA_AV_INCLUDED == TRUE)
262     BUFFER_Q TxAaQ;
263     BUFFER_Q RxSbcQ;
264     BOOLEAN is_tx_timer;
265     BOOLEAN is_rx_timer;
266     UINT16 TxAaMtuSize;
267     UINT32 timestamp;
268     UINT8 TxTranscoding;
269     tBTIF_AV_FEEDING_MODE feeding_mode;
270     tBTIF_AV_MEDIA_FEEDINGS media_feeding;
271     tBTIF_AV_MEDIA_FEEDINGS_STATE media_feeding_state;
272     SBC_ENC_PARAMS encoder;
273     UINT8 busy_level;
274     void* av_sm_hdl;
275     UINT8 a2dp_cmd_pending; /* we can have max one command pending */
276     BOOLEAN tx_flush; /* discards any outgoing data when true */
277     BOOLEAN rx_flush; /* discards any incoming data when true */
278     UINT8 peer_sep;
279     BOOLEAN data_channel_open;
280     UINT8   frames_to_process;
281 
282     UINT32  sample_rate;
283     UINT8   channel_count;
284 #endif
285 
286 } tBTIF_MEDIA_CB;
287 
288 typedef struct {
289     long long rx;
290     long long rx_tot;
291     long long tx;
292     long long tx_tot;
293     long long ts_prev_us;
294 } t_stat;
295 
296 /*****************************************************************************
297  **  Local data
298  *****************************************************************************/
299 
300 static tBTIF_MEDIA_CB btif_media_cb;
301 static int media_task_running = MEDIA_TASK_STATE_OFF;
302 static UINT64 last_frame_us = 0;
303 
304 
305 /*****************************************************************************
306  **  Local functions
307  *****************************************************************************/
308 
309 static void btif_a2dp_data_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event);
310 static void btif_a2dp_ctrl_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event);
311 static void btif_a2dp_encoder_update(void);
312 const char* dump_media_event(UINT16 event);
313 #if (BTA_AV_SINK_INCLUDED == TRUE)
314 extern OI_STATUS OI_CODEC_SBC_DecodeFrame(OI_CODEC_SBC_DECODER_CONTEXT *context,
315                                           const OI_BYTE **frameData,
316                                           unsigned long *frameBytes,
317                                           OI_INT16 *pcmData,
318                                           unsigned long *pcmBytes);
319 extern OI_STATUS OI_CODEC_SBC_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
320                                            unsigned long *decoderData,
321                                            unsigned long decoderDataBytes,
322                                            OI_UINT8 maxChannels,
323                                            OI_UINT8 pcmStride,
324                                            OI_BOOL enhanced);
325 #endif
326 static void btif_media_flush_q(BUFFER_Q *p_q);
327 static void btif_media_task_aa_handle_stop_decoding(void );
328 static void btif_media_task_aa_rx_flush(void);
329 static BOOLEAN btif_media_task_stop_decoding_req(void);
330 
331 /*****************************************************************************
332  **  Externs
333  *****************************************************************************/
334 
335 static void btif_media_task_handle_cmd(BT_HDR *p_msg);
336 static void btif_media_task_handle_media(BT_HDR*p_msg);
337 /* Handle incoming media packets A2DP SINK streaming*/
338 #if (BTA_AV_SINK_INCLUDED == TRUE)
339 static void btif_media_task_handle_inc_media(tBT_SBC_HDR*p_msg);
340 #endif
341 
342 #if (BTA_AV_INCLUDED == TRUE)
343 static void btif_media_send_aa_frame(void);
344 static void btif_media_task_feeding_state_reset(void);
345 static void btif_media_task_aa_start_tx(void);
346 static void btif_media_task_aa_stop_tx(void);
347 static void btif_media_task_enc_init(BT_HDR *p_msg);
348 static void btif_media_task_enc_update(BT_HDR *p_msg);
349 static void btif_media_task_audio_feeding_init(BT_HDR *p_msg);
350 static void btif_media_task_aa_tx_flush(BT_HDR *p_msg);
351 static void btif_media_aa_prep_2_send(UINT8 nb_frame);
352 #if (BTA_AV_SINK_INCLUDED == TRUE)
353 static void btif_media_task_aa_handle_decoder_reset(BT_HDR *p_msg);
354 static void btif_media_task_aa_handle_clear_track(void);
355 #endif
356 static void btif_media_task_aa_handle_start_decoding(void );
357 #endif
358 BOOLEAN btif_media_task_start_decoding_req(void);
359 BOOLEAN btif_media_task_clear_track(void);
360 /*****************************************************************************
361  **  Misc helper functions
362  *****************************************************************************/
363 
time_now_us()364 static UINT64 time_now_us()
365 {
366     struct timespec ts_now;
367     clock_gettime(CLOCK_BOOTTIME, &ts_now);
368     return ((UINT64)ts_now.tv_sec * USEC_PER_SEC) + ((UINT64)ts_now.tv_nsec / 1000);
369 }
370 
log_tstamps_us(char * comment)371 static void log_tstamps_us(char *comment)
372 {
373     static UINT64 prev_us = 0;
374     const UINT64 now_us = time_now_us();
375     APPL_TRACE_DEBUG("[%s] ts %08llu, diff : %08llu, queue sz %d", comment, now_us, now_us - prev_us,
376                 btif_media_cb.TxAaQ.count);
377     prev_us = now_us;
378 }
379 
dump_media_event(UINT16 event)380 const char* dump_media_event(UINT16 event)
381 {
382     switch(event)
383     {
384         CASE_RETURN_STR(BTIF_MEDIA_START_AA_TX)
385         CASE_RETURN_STR(BTIF_MEDIA_STOP_AA_TX)
386         CASE_RETURN_STR(BTIF_MEDIA_AA_RX_RDY)
387         CASE_RETURN_STR(BTIF_MEDIA_UIPC_RX_RDY)
388         CASE_RETURN_STR(BTIF_MEDIA_SBC_ENC_INIT)
389         CASE_RETURN_STR(BTIF_MEDIA_SBC_ENC_UPDATE)
390         CASE_RETURN_STR(BTIF_MEDIA_SBC_DEC_INIT)
391         CASE_RETURN_STR(BTIF_MEDIA_VIDEO_DEC_INIT)
392         CASE_RETURN_STR(BTIF_MEDIA_FLUSH_AA_TX)
393         CASE_RETURN_STR(BTIF_MEDIA_FLUSH_AA_RX)
394         CASE_RETURN_STR(BTIF_MEDIA_AUDIO_FEEDING_INIT)
395         CASE_RETURN_STR(BTIF_MEDIA_AUDIO_RECEIVING_INIT)
396         CASE_RETURN_STR(BTIF_MEDIA_AUDIO_SINK_CFG_UPDATE)
397         CASE_RETURN_STR(BTIF_MEDIA_AUDIO_SINK_START_DECODING)
398         CASE_RETURN_STR(BTIF_MEDIA_AUDIO_SINK_STOP_DECODING)
399         CASE_RETURN_STR(BTIF_MEDIA_AUDIO_SINK_CLEAR_TRACK)
400 
401         default:
402             return "UNKNOWN MEDIA EVENT";
403     }
404 }
405 
406 /*****************************************************************************
407  **  A2DP CTRL PATH
408  *****************************************************************************/
409 
dump_a2dp_ctrl_event(UINT8 event)410 static const char* dump_a2dp_ctrl_event(UINT8 event)
411 {
412     switch(event)
413     {
414         CASE_RETURN_STR(A2DP_CTRL_CMD_NONE)
415         CASE_RETURN_STR(A2DP_CTRL_CMD_CHECK_READY)
416         CASE_RETURN_STR(A2DP_CTRL_CMD_START)
417         CASE_RETURN_STR(A2DP_CTRL_CMD_STOP)
418         CASE_RETURN_STR(A2DP_CTRL_CMD_SUSPEND)
419         default:
420             return "UNKNOWN MSG ID";
421     }
422 }
423 
btif_audiopath_detached(void)424 static void btif_audiopath_detached(void)
425 {
426     APPL_TRACE_EVENT("## AUDIO PATH DETACHED ##");
427 
428     /*  send stop request only if we are actively streaming and haven't received
429         a stop request. Potentially audioflinger detached abnormally */
430     if (btif_media_cb.is_tx_timer)
431     {
432         /* post stop event and wait for audio path to stop */
433         btif_dispatch_sm_event(BTIF_AV_STOP_STREAM_REQ_EVT, NULL, 0);
434     }
435 }
436 
a2dp_cmd_acknowledge(int status)437 static void a2dp_cmd_acknowledge(int status)
438 {
439     UINT8 ack = status;
440 
441     APPL_TRACE_EVENT("## a2dp ack : %s, status %d ##",
442           dump_a2dp_ctrl_event(btif_media_cb.a2dp_cmd_pending), status);
443 
444     /* sanity check */
445     if (btif_media_cb.a2dp_cmd_pending == A2DP_CTRL_CMD_NONE)
446     {
447         APPL_TRACE_ERROR("warning : no command pending, ignore ack");
448         return;
449     }
450 
451     /* clear pending */
452     btif_media_cb.a2dp_cmd_pending = A2DP_CTRL_CMD_NONE;
453 
454     /* acknowledge start request */
455     UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, &ack, 1);
456 }
457 
458 
btif_recv_ctrl_data(void)459 static void btif_recv_ctrl_data(void)
460 {
461     UINT8 cmd = 0;
462     int n;
463     n = UIPC_Read(UIPC_CH_ID_AV_CTRL, NULL, &cmd, 1);
464 
465     /* detach on ctrl channel means audioflinger process was terminated */
466     if (n == 0)
467     {
468         APPL_TRACE_EVENT("CTRL CH DETACHED");
469         UIPC_Close(UIPC_CH_ID_AV_CTRL);
470         /* we can operate only on datachannel, if af client wants to
471            do send additional commands the ctrl channel would be reestablished */
472         //btif_audiopath_detached();
473         return;
474     }
475 
476     APPL_TRACE_DEBUG("a2dp-ctrl-cmd : %s", dump_a2dp_ctrl_event(cmd));
477 
478     btif_media_cb.a2dp_cmd_pending = cmd;
479 
480     switch(cmd)
481     {
482         case A2DP_CTRL_CMD_CHECK_READY:
483 
484             if (media_task_running == MEDIA_TASK_STATE_SHUTTING_DOWN)
485             {
486                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
487                 return;
488             }
489 
490             /* check whether av is ready to setup a2dp datapath */
491             if ((btif_av_stream_ready() == TRUE) || (btif_av_stream_started_ready() == TRUE))
492             {
493                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
494             }
495             else
496             {
497                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
498             }
499             break;
500 
501         case A2DP_CTRL_CMD_START:
502 
503             if (btif_av_stream_ready() == TRUE)
504             {
505                 /* setup audio data channel listener */
506                 UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
507 
508                 /* post start event and wait for audio path to open */
509                 btif_dispatch_sm_event(BTIF_AV_START_STREAM_REQ_EVT, NULL, 0);
510 
511 #if (BTA_AV_SINK_INCLUDED == TRUE)
512                 if (btif_media_cb.peer_sep == AVDT_TSEP_SRC)
513                     a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
514 #endif
515             }
516             else if (btif_av_stream_started_ready())
517             {
518                 /* already started, setup audio data channel listener
519                    and ack back immediately */
520                 UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
521 
522                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
523             }
524             else
525             {
526                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
527                 break;
528             }
529             break;
530 
531         case A2DP_CTRL_CMD_STOP:
532             if (btif_media_cb.peer_sep == AVDT_TSEP_SNK && btif_media_cb.is_tx_timer == FALSE)
533             {
534                 /* we are already stopped, just ack back */
535                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
536                 break;
537             }
538 
539             btif_dispatch_sm_event(BTIF_AV_STOP_STREAM_REQ_EVT, NULL, 0);
540             a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
541             break;
542 
543         case A2DP_CTRL_CMD_SUSPEND:
544             /* local suspend */
545             if (btif_av_stream_started_ready())
546             {
547                 btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
548             }
549             else
550             {
551                 /* if we are not in started state, just ack back ok and let
552                    audioflinger close the channel. This can happen if we are
553                    remotely suspended */
554                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
555             }
556             break;
557 
558         case A2DP_CTRL_GET_AUDIO_CONFIG:
559         {
560             uint32_t sample_rate = btif_media_cb.sample_rate;
561             uint8_t channel_count = btif_media_cb.channel_count;
562 
563             a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
564             UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, (UINT8 *)&sample_rate, 4);
565             UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, &channel_count, 1);
566             break;
567         }
568 
569         default:
570             APPL_TRACE_ERROR("UNSUPPORTED CMD (%d)", cmd);
571             a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
572             break;
573     }
574     APPL_TRACE_DEBUG("a2dp-ctrl-cmd : %s DONE", dump_a2dp_ctrl_event(cmd));
575 }
576 
btif_a2dp_ctrl_cb(tUIPC_CH_ID ch_id,tUIPC_EVENT event)577 static void btif_a2dp_ctrl_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event)
578 {
579     UNUSED(ch_id);
580 
581     APPL_TRACE_DEBUG("A2DP-CTRL-CHANNEL EVENT %s", dump_uipc_event(event));
582 
583     switch(event)
584     {
585         case UIPC_OPEN_EVT:
586             /* fetch av statemachine handle */
587             btif_media_cb.av_sm_hdl = btif_av_get_sm_handle();
588             break;
589 
590         case UIPC_CLOSE_EVT:
591             /* restart ctrl server unless we are shutting down */
592             if (media_task_running == MEDIA_TASK_STATE_ON)
593                 UIPC_Open(UIPC_CH_ID_AV_CTRL , btif_a2dp_ctrl_cb);
594             break;
595 
596         case UIPC_RX_DATA_READY_EVT:
597             btif_recv_ctrl_data();
598             break;
599 
600         default :
601             APPL_TRACE_ERROR("### A2DP-CTRL-CHANNEL EVENT %d NOT HANDLED ###", event);
602             break;
603     }
604 }
605 
btif_a2dp_data_cb(tUIPC_CH_ID ch_id,tUIPC_EVENT event)606 static void btif_a2dp_data_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event)
607 {
608     UNUSED(ch_id);
609 
610     APPL_TRACE_DEBUG("BTIF MEDIA (A2DP-DATA) EVENT %s", dump_uipc_event(event));
611 
612     switch(event)
613     {
614         case UIPC_OPEN_EVT:
615 
616             /*  read directly from media task from here on (keep callback for
617                 connection events */
618             UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_REG_REMOVE_ACTIVE_READSET, NULL);
619             UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_SET_READ_POLL_TMO,
620                        (void *)A2DP_DATA_READ_POLL_MS);
621 
622             if (btif_media_cb.peer_sep == AVDT_TSEP_SNK) {
623                 /* Start the media task to encode SBC */
624                 btif_media_task_start_aa_req();
625 
626                 /* make sure we update any changed sbc encoder params */
627                 btif_a2dp_encoder_update();
628             }
629             btif_media_cb.data_channel_open = TRUE;
630 
631             /* ack back when media task is fully started */
632             break;
633 
634         case UIPC_CLOSE_EVT:
635             a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
636             btif_audiopath_detached();
637             btif_media_cb.data_channel_open = FALSE;
638             break;
639 
640         default :
641             APPL_TRACE_ERROR("### A2DP-DATA EVENT %d NOT HANDLED ###", event);
642             break;
643     }
644 }
645 
646 
647 /*****************************************************************************
648  **  BTIF ADAPTATION
649  *****************************************************************************/
650 
btif_media_task_get_sbc_rate(void)651 static UINT16 btif_media_task_get_sbc_rate(void)
652 {
653     UINT16 rate = DEFAULT_SBC_BITRATE;
654 
655     /* restrict bitrate if a2dp link is non-edr */
656     if (!btif_av_is_peer_edr())
657     {
658         rate = BTIF_A2DP_NON_EDR_MAX_RATE;
659         APPL_TRACE_DEBUG("non-edr a2dp sink detected, restrict rate to %d", rate);
660     }
661 
662     return rate;
663 }
664 
btif_a2dp_encoder_init(void)665 static void btif_a2dp_encoder_init(void)
666 {
667     UINT16 minmtu;
668     tBTIF_MEDIA_INIT_AUDIO msg;
669     tA2D_SBC_CIE sbc_config;
670 
671     /* lookup table for converting channel mode */
672     UINT16 codec_mode_tbl[5] = { SBC_JOINT_STEREO, SBC_STEREO, SBC_DUAL, 0, SBC_MONO };
673 
674     /* lookup table for converting number of blocks */
675     UINT16 codec_block_tbl[5] = { 16, 12, 8, 0, 4 };
676 
677     /* lookup table to convert freq */
678     UINT16 freq_block_tbl[5] = { SBC_sf48000, SBC_sf44100, SBC_sf32000, 0, SBC_sf16000 };
679 
680     APPL_TRACE_DEBUG("btif_a2dp_encoder_init");
681 
682     /* Retrieve the current SBC configuration (default if currently not used) */
683     bta_av_co_audio_get_sbc_config(&sbc_config, &minmtu);
684     msg.NumOfSubBands = (sbc_config.num_subbands == A2D_SBC_IE_SUBBAND_4) ? 4 : 8;
685     msg.NumOfBlocks = codec_block_tbl[sbc_config.block_len >> 5];
686     msg.AllocationMethod = (sbc_config.alloc_mthd == A2D_SBC_IE_ALLOC_MD_L) ? SBC_LOUDNESS : SBC_SNR;
687     msg.ChannelMode = codec_mode_tbl[sbc_config.ch_mode >> 1];
688     msg.SamplingFreq = freq_block_tbl[sbc_config.samp_freq >> 5];
689     msg.MtuSize = minmtu;
690 
691     APPL_TRACE_EVENT("msg.ChannelMode %x", msg.ChannelMode);
692 
693     /* Init the media task to encode SBC properly */
694     btif_media_task_enc_init_req(&msg);
695 }
696 
btif_a2dp_encoder_update(void)697 static void btif_a2dp_encoder_update(void)
698 {
699     UINT16 minmtu;
700     tA2D_SBC_CIE sbc_config;
701     tBTIF_MEDIA_UPDATE_AUDIO msg;
702     UINT8 pref_min;
703     UINT8 pref_max;
704 
705     APPL_TRACE_DEBUG("btif_a2dp_encoder_update");
706 
707     /* Retrieve the current SBC configuration (default if currently not used) */
708     bta_av_co_audio_get_sbc_config(&sbc_config, &minmtu);
709 
710     APPL_TRACE_DEBUG("btif_a2dp_encoder_update: Common min_bitpool:%d(0x%x) max_bitpool:%d(0x%x)",
711             sbc_config.min_bitpool, sbc_config.min_bitpool,
712             sbc_config.max_bitpool, sbc_config.max_bitpool);
713 
714     if (sbc_config.min_bitpool > sbc_config.max_bitpool)
715     {
716         APPL_TRACE_ERROR("btif_a2dp_encoder_update: ERROR btif_a2dp_encoder_update min_bitpool > max_bitpool");
717     }
718 
719     /* check if remote sink has a preferred bitpool range */
720     if (bta_av_co_get_remote_bitpool_pref(&pref_min, &pref_max) == TRUE)
721     {
722         /* adjust our preferred bitpool with the remote preference if within
723            our capable range */
724 
725         if (pref_min < sbc_config.min_bitpool)
726             pref_min = sbc_config.min_bitpool;
727 
728         if (pref_max > sbc_config.max_bitpool)
729             pref_max = sbc_config.max_bitpool;
730 
731         msg.MinBitPool = pref_min;
732         msg.MaxBitPool = pref_max;
733 
734         if ((pref_min != sbc_config.min_bitpool) || (pref_max != sbc_config.max_bitpool))
735         {
736             APPL_TRACE_EVENT("## adjusted our bitpool range to peer pref [%d:%d] ##",
737                 pref_min, pref_max);
738         }
739     }
740     else
741     {
742         msg.MinBitPool = sbc_config.min_bitpool;
743         msg.MaxBitPool = sbc_config.max_bitpool;
744     }
745 
746     msg.MinMtuSize = minmtu;
747 
748     /* Update the media task to encode SBC properly */
749     btif_media_task_enc_update_req(&msg);
750 }
751 
752 
753 /*****************************************************************************
754 **
755 ** Function        btif_a2dp_start_media_task
756 **
757 ** Description
758 **
759 ** Returns
760 **
761 *******************************************************************************/
762 
btif_a2dp_start_media_task(void)763 int btif_a2dp_start_media_task(void)
764 {
765     int retval;
766 
767     if (media_task_running != MEDIA_TASK_STATE_OFF)
768     {
769         APPL_TRACE_ERROR("warning : media task already running");
770         return GKI_FAILURE;
771     }
772 
773     APPL_TRACE_EVENT("## A2DP START MEDIA TASK ##");
774 
775     /* start a2dp media task */
776     retval = GKI_create_task((TASKPTR)btif_media_task, A2DP_MEDIA_TASK,
777                 A2DP_MEDIA_TASK_TASK_STR,
778                 (UINT16 *) ((UINT8 *)a2dp_media_task_stack + A2DP_MEDIA_TASK_STACK_SIZE),
779                 sizeof(a2dp_media_task_stack));
780 
781     if (retval != GKI_SUCCESS)
782         return retval;
783 
784     /* wait for task to come up to sure we are able to send messages to it */
785     while (media_task_running == MEDIA_TASK_STATE_OFF)
786         usleep(10);
787 
788     APPL_TRACE_EVENT("## A2DP MEDIA TASK STARTED ##");
789 
790     return retval;
791 }
792 
793 /*****************************************************************************
794 **
795 ** Function        btif_a2dp_stop_media_task
796 **
797 ** Description
798 **
799 ** Returns
800 **
801 *******************************************************************************/
802 
btif_a2dp_stop_media_task(void)803 void btif_a2dp_stop_media_task(void)
804 {
805     APPL_TRACE_EVENT("## A2DP STOP MEDIA TASK ##");
806     GKI_destroy_task(BT_MEDIA_TASK);
807 }
808 
809 /*****************************************************************************
810 **
811 ** Function        btif_a2dp_on_init
812 **
813 ** Description
814 **
815 ** Returns
816 **
817 *******************************************************************************/
818 
btif_a2dp_on_init(void)819 void btif_a2dp_on_init(void)
820 {
821     //tput_mon(1, 0, 1);
822 }
823 
824 
825 /*****************************************************************************
826 **
827 ** Function        btif_a2dp_setup_codec
828 **
829 ** Description
830 **
831 ** Returns
832 **
833 *******************************************************************************/
834 
btif_a2dp_setup_codec(void)835 void btif_a2dp_setup_codec(void)
836 {
837     tBTIF_AV_MEDIA_FEEDINGS media_feeding;
838     tBTIF_STATUS status;
839 
840     APPL_TRACE_EVENT("## A2DP SETUP CODEC ##");
841 
842     GKI_disable();
843 
844     /* for now hardcode 44.1 khz 16 bit stereo PCM format */
845     media_feeding.cfg.pcm.sampling_freq = 44100;
846     media_feeding.cfg.pcm.bit_per_sample = 16;
847     media_feeding.cfg.pcm.num_channel = 2;
848     media_feeding.format = BTIF_AV_CODEC_PCM;
849 
850     if (bta_av_co_audio_set_codec(&media_feeding, &status))
851     {
852         tBTIF_MEDIA_INIT_AUDIO_FEEDING mfeed;
853 
854         /* Init the encoding task */
855         btif_a2dp_encoder_init();
856 
857         /* Build the media task configuration */
858         mfeed.feeding = media_feeding;
859         mfeed.feeding_mode = BTIF_AV_FEEDING_ASYNCHRONOUS;
860         /* Send message to Media task to configure transcoding */
861         btif_media_task_audio_feeding_init_req(&mfeed);
862     }
863 
864     GKI_enable();
865 }
866 
867 
868 /*****************************************************************************
869 **
870 ** Function        btif_a2dp_on_idle
871 **
872 ** Description
873 **
874 ** Returns
875 **
876 *******************************************************************************/
877 
btif_a2dp_on_idle(void)878 void btif_a2dp_on_idle(void)
879 {
880     APPL_TRACE_EVENT("## ON A2DP IDLE ##");
881     if (btif_media_cb.peer_sep == AVDT_TSEP_SNK)
882     {
883         /* Make sure media task is stopped */
884         btif_media_task_stop_aa_req();
885     }
886 
887     bta_av_co_init();
888 #if (BTA_AV_SINK_INCLUDED == TRUE)
889     if (btif_media_cb.peer_sep == AVDT_TSEP_SRC)
890     {
891         btif_media_cb.rx_flush = TRUE;
892         btif_media_task_aa_rx_flush_req();
893         btif_media_task_stop_decoding_req();
894         btif_media_task_clear_track();
895         APPL_TRACE_DEBUG("Stopped BT track");
896     }
897 #endif
898 }
899 
900 /*****************************************************************************
901 **
902 ** Function        btif_a2dp_on_open
903 **
904 ** Description
905 **
906 ** Returns
907 **
908 *******************************************************************************/
909 
btif_a2dp_on_open(void)910 void btif_a2dp_on_open(void)
911 {
912     APPL_TRACE_EVENT("## ON A2DP OPEN ##");
913 
914     /* always use callback to notify socket events */
915     UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
916 }
917 
918 /*******************************************************************************
919  **
920  ** Function         btif_media_task_clear_track
921  **
922  ** Description
923  **
924  ** Returns          TRUE is success
925  **
926  *******************************************************************************/
btif_media_task_clear_track(void)927 BOOLEAN btif_media_task_clear_track(void)
928 {
929     BT_HDR *p_buf;
930 
931     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
932     {
933         return FALSE;
934     }
935 
936     p_buf->event = BTIF_MEDIA_AUDIO_SINK_CLEAR_TRACK;
937 
938     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
939     return TRUE;
940 }
941 /*******************************************************************************
942  **
943  ** Function         btif_media_task_stop_decoding_req
944  **
945  ** Description
946  **
947  ** Returns          TRUE is success
948  **
949  *******************************************************************************/
btif_media_task_stop_decoding_req(void)950 BOOLEAN btif_media_task_stop_decoding_req(void)
951 {
952     BT_HDR *p_buf;
953 
954     if (!btif_media_cb.is_rx_timer)
955         return TRUE;   /*  if timer is not running no need to send message */
956 
957     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
958     {
959         return FALSE;
960     }
961 
962     p_buf->event = BTIF_MEDIA_AUDIO_SINK_STOP_DECODING;
963 
964     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
965     return TRUE;
966 }
967 
968 /*******************************************************************************
969  **
970  ** Function         btif_media_task_start_decoding_req
971  **
972  ** Description
973  **
974  ** Returns          TRUE is success
975  **
976  *******************************************************************************/
btif_media_task_start_decoding_req(void)977 BOOLEAN btif_media_task_start_decoding_req(void)
978 {
979     BT_HDR *p_buf;
980 
981     if(btif_media_cb.is_rx_timer)
982         return FALSE;   /*  if timer is already running no need to send message */
983 
984     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
985     {
986         return FALSE;
987     }
988 
989     p_buf->event = BTIF_MEDIA_AUDIO_SINK_START_DECODING;
990 
991     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
992     return TRUE;
993 }
994 
995 /*****************************************************************************
996 **
997 ** Function        btif_reset_decoder
998 **
999 ** Description
1000 **
1001 ** Returns
1002 **
1003 *******************************************************************************/
1004 
btif_reset_decoder(UINT8 * p_av)1005 void btif_reset_decoder(UINT8 *p_av)
1006 {
1007     APPL_TRACE_EVENT("btif_reset_decoder");
1008     APPL_TRACE_DEBUG("btif_reset_decoder p_codec_info[%x:%x:%x:%x:%x:%x]",
1009             p_av[1], p_av[2], p_av[3],
1010             p_av[4], p_av[5], p_av[6]);
1011 
1012     tBTIF_MEDIA_SINK_CFG_UPDATE *p_buf;
1013     if (NULL == (p_buf = GKI_getbuf(sizeof(tBTIF_MEDIA_SINK_CFG_UPDATE))))
1014     {
1015         APPL_TRACE_EVENT("btif_reset_decoder No Buffer ");
1016         return;
1017     }
1018 
1019     memcpy(p_buf->codec_info,p_av, AVDT_CODEC_SIZE);
1020     p_buf->hdr.event = BTIF_MEDIA_AUDIO_SINK_CFG_UPDATE;
1021 
1022     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
1023 }
1024 
1025 /*****************************************************************************
1026 **
1027 ** Function        btif_a2dp_on_started
1028 **
1029 ** Description
1030 **
1031 ** Returns
1032 **
1033 *******************************************************************************/
1034 
btif_a2dp_on_started(tBTA_AV_START * p_av,BOOLEAN pending_start)1035 BOOLEAN btif_a2dp_on_started(tBTA_AV_START *p_av, BOOLEAN pending_start)
1036 {
1037     tBTIF_STATUS status;
1038     BOOLEAN ack = FALSE;
1039 
1040     APPL_TRACE_EVENT("## ON A2DP STARTED ##");
1041 
1042     if (p_av == NULL)
1043     {
1044         /* ack back a local start request */
1045         a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
1046         return TRUE;
1047     }
1048 
1049     if (p_av->status == BTA_AV_SUCCESS)
1050     {
1051         if (p_av->suspending == FALSE)
1052         {
1053             if (p_av->initiator)
1054             {
1055                 if (pending_start) {
1056                     a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
1057                     ack = TRUE;
1058                 }
1059             }
1060             else
1061             {
1062                 /* we were remotely started,  make sure codec
1063                    is setup before datapath is started */
1064                 btif_a2dp_setup_codec();
1065             }
1066 
1067             /* media task is autostarted upon a2dp audiopath connection */
1068         }
1069     }
1070     else if (pending_start)
1071     {
1072         a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
1073         ack = TRUE;
1074     }
1075     return ack;
1076 }
1077 
1078 
1079 /*****************************************************************************
1080 **
1081 ** Function        btif_a2dp_ack_fail
1082 **
1083 ** Description
1084 **
1085 ** Returns
1086 **
1087 *******************************************************************************/
1088 
btif_a2dp_ack_fail(void)1089 void btif_a2dp_ack_fail(void)
1090 {
1091     tBTIF_STATUS status;
1092 
1093     APPL_TRACE_EVENT("## A2DP_CTRL_ACK_FAILURE ##");
1094     a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
1095 }
1096 
1097 /*****************************************************************************
1098 **
1099 ** Function        btif_a2dp_on_stopped
1100 **
1101 ** Description
1102 **
1103 ** Returns
1104 **
1105 *******************************************************************************/
1106 
btif_a2dp_on_stopped(tBTA_AV_SUSPEND * p_av)1107 void btif_a2dp_on_stopped(tBTA_AV_SUSPEND *p_av)
1108 {
1109     APPL_TRACE_EVENT("## ON A2DP STOPPED ##");
1110     if (btif_media_cb.peer_sep == AVDT_TSEP_SRC) /*  Handling for A2DP SINK cases*/
1111     {
1112         btif_media_cb.rx_flush = TRUE;
1113         btif_media_task_aa_rx_flush_req();
1114         btif_media_task_stop_decoding_req();
1115         UIPC_Close(UIPC_CH_ID_AV_AUDIO);
1116         btif_media_cb.data_channel_open = FALSE;
1117         return;
1118     }
1119     /* allow using this api for other than suspend */
1120     if (p_av != NULL)
1121     {
1122         if (p_av->status != BTA_AV_SUCCESS)
1123         {
1124             APPL_TRACE_EVENT("AV STOP FAILED (%d)", p_av->status);
1125 
1126             if (p_av->initiator)
1127                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
1128             return;
1129         }
1130     }
1131 
1132     /* ensure tx frames are immediately suspended */
1133     btif_media_cb.tx_flush = 1;
1134 
1135     /* request to stop media task  */
1136     btif_media_task_aa_tx_flush_req();
1137     btif_media_task_stop_aa_req();
1138 
1139     /* once stream is fully stopped we will ack back */
1140 }
1141 
1142 
1143 /*****************************************************************************
1144 **
1145 ** Function        btif_a2dp_on_suspended
1146 **
1147 ** Description
1148 **
1149 ** Returns
1150 **
1151 *******************************************************************************/
1152 
btif_a2dp_on_suspended(tBTA_AV_SUSPEND * p_av)1153 void btif_a2dp_on_suspended(tBTA_AV_SUSPEND *p_av)
1154 {
1155     APPL_TRACE_EVENT("## ON A2DP SUSPENDED ##");
1156     if (btif_media_cb.peer_sep == AVDT_TSEP_SRC)
1157     {
1158         btif_media_cb.rx_flush = TRUE;
1159         btif_media_task_aa_rx_flush_req();
1160         btif_media_task_stop_decoding_req();
1161         return;
1162     }
1163 
1164     /* check for status failures */
1165     if (p_av->status != BTA_AV_SUCCESS)
1166     {
1167         if (p_av->initiator == TRUE)
1168             a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
1169     }
1170 
1171     /* once stream is fully stopped we will ack back */
1172 
1173     /* ensure tx frames are immediately flushed */
1174     btif_media_cb.tx_flush = 1;
1175 
1176     /* stop timer tick */
1177     btif_media_task_stop_aa_req();
1178 }
1179 
1180 /* when true media task discards any rx frames */
btif_a2dp_set_rx_flush(BOOLEAN enable)1181 void btif_a2dp_set_rx_flush(BOOLEAN enable)
1182 {
1183     APPL_TRACE_EVENT("## DROP RX %d ##", enable);
1184     btif_media_cb.rx_flush = enable;
1185 }
1186 
1187 /* when true media task discards any tx frames */
btif_a2dp_set_tx_flush(BOOLEAN enable)1188 void btif_a2dp_set_tx_flush(BOOLEAN enable)
1189 {
1190     APPL_TRACE_EVENT("## DROP TX %d ##", enable);
1191     btif_media_cb.tx_flush = enable;
1192 }
1193 
1194 #if (BTA_AV_SINK_INCLUDED == TRUE)
1195 /*******************************************************************************
1196  **
1197  ** Function         btif_media_task_avk_handle_timer
1198  **
1199  ** Description
1200  **
1201  ** Returns          void
1202  **
1203  *******************************************************************************/
btif_media_task_avk_handle_timer(void)1204 static void btif_media_task_avk_handle_timer ( void )
1205 {
1206     UINT8 count;
1207     tBT_SBC_HDR *p_msg;
1208     int num_sbc_frames;
1209     int num_frames_to_process;
1210 
1211     count = btif_media_cb.RxSbcQ.count;
1212     if (0 == count)
1213     {
1214         APPL_TRACE_DEBUG("  QUE  EMPTY ");
1215     }
1216     else
1217     {
1218         if (btif_media_cb.rx_flush == TRUE)
1219         {
1220             btif_media_flush_q(&(btif_media_cb.RxSbcQ));
1221             return;
1222         }
1223 
1224         num_frames_to_process = btif_media_cb.frames_to_process;
1225         APPL_TRACE_DEBUG(" Process Frames + ");
1226 
1227         do
1228         {
1229             p_msg = (tBT_SBC_HDR *)GKI_getfirst(&(btif_media_cb.RxSbcQ));
1230             if (p_msg == NULL)
1231                 return;
1232             num_sbc_frames  = p_msg->num_frames_to_be_processed; /* num of frames in Que Packets */
1233             APPL_TRACE_DEBUG(" Frames left in topmost packet %d", num_sbc_frames);
1234             APPL_TRACE_DEBUG(" Remaining frames to process in tick %d", num_frames_to_process);
1235             APPL_TRACE_DEBUG(" Num of Packets in Que %d", btif_media_cb.RxSbcQ.count);
1236 
1237             if ( num_sbc_frames > num_frames_to_process) /*  Que Packet has more frames*/
1238             {
1239                  p_msg->num_frames_to_be_processed= num_frames_to_process;
1240                  btif_media_task_handle_inc_media(p_msg);
1241                  p_msg->num_frames_to_be_processed = num_sbc_frames - num_frames_to_process;
1242                  num_frames_to_process = 0;
1243                  break;
1244             }
1245             else                                        /*  Que packet has less frames */
1246             {
1247                 btif_media_task_handle_inc_media(p_msg);
1248                 p_msg = (tBT_SBC_HDR *)GKI_dequeue(&(btif_media_cb.RxSbcQ));
1249                 if( p_msg == NULL )
1250                 {
1251                      APPL_TRACE_ERROR("Insufficient data in que ");
1252                      break;
1253                 }
1254                 num_frames_to_process = num_frames_to_process - p_msg->num_frames_to_be_processed;
1255                 GKI_freebuf(p_msg);
1256             }
1257         }while(num_frames_to_process > 0);
1258 
1259         APPL_TRACE_DEBUG(" Process Frames - ");
1260     }
1261 }
1262 #endif
1263 
1264 /*******************************************************************************
1265  **
1266  ** Function         btif_media_task_aa_handle_timer
1267  **
1268  ** Description
1269  **
1270  ** Returns          void
1271  **
1272  *******************************************************************************/
1273 
btif_media_task_aa_handle_timer(void)1274 static void btif_media_task_aa_handle_timer(void)
1275 {
1276 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
1277     static UINT16 Debug = 0;
1278     APPL_TRACE_DEBUG("btif_media_task_aa_handle_timer: %d", Debug++);
1279 #endif
1280 
1281     log_tstamps_us("media task tx timer");
1282 
1283 #if (BTA_AV_INCLUDED == TRUE)
1284     if(btif_media_cb.is_tx_timer == TRUE)
1285     {
1286         btif_media_send_aa_frame();
1287     }
1288     else
1289     {
1290         APPL_TRACE_ERROR("ERROR Media task Scheduled after Suspend");
1291     }
1292 #endif
1293 }
1294 
1295 #if (BTA_AV_INCLUDED == TRUE)
1296 /*******************************************************************************
1297  **
1298  ** Function         btif_media_task_aa_handle_timer
1299  **
1300  ** Description
1301  **
1302  ** Returns          void
1303  **
1304  *******************************************************************************/
btif_media_task_aa_handle_uipc_rx_rdy(void)1305 static void btif_media_task_aa_handle_uipc_rx_rdy(void)
1306 {
1307 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
1308     static UINT16 Debug = 0;
1309     APPL_TRACE_DEBUG("btif_media_task_aa_handle_uipc_rx_rdy: %d", Debug++);
1310 #endif
1311 
1312     /* process all the UIPC data */
1313     btif_media_aa_prep_2_send(0xFF);
1314 
1315     /* send it */
1316     VERBOSE("btif_media_task_aa_handle_uipc_rx_rdy calls bta_av_ci_src_data_ready");
1317     bta_av_ci_src_data_ready(BTA_AV_CHNL_AUDIO);
1318 }
1319 #endif
1320 
1321 /*******************************************************************************
1322  **
1323  ** Function         btif_media_task_init
1324  **
1325  ** Description
1326  **
1327  ** Returns          void
1328  **
1329  *******************************************************************************/
1330 
btif_media_task_init(void)1331 void btif_media_task_init(void)
1332 {
1333     memset(&(btif_media_cb), 0, sizeof(btif_media_cb));
1334 
1335     UIPC_Init(NULL);
1336 
1337 #if (BTA_AV_INCLUDED == TRUE)
1338     UIPC_Open(UIPC_CH_ID_AV_CTRL , btif_a2dp_ctrl_cb);
1339 #endif
1340 }
1341 /*******************************************************************************
1342  **
1343  ** Function         btif_media_task
1344  **
1345  ** Description      Task for SBC encoder.  This task receives an
1346  **                  event when the waveIn interface has a pcm data buffer
1347  **                  ready.  On receiving the event, handle all ready pcm
1348  **                  data buffers.  If stream is started, run the SBC encoder
1349  **                  on each chunk of pcm samples and build an output packet
1350  **                  consisting of one or more encoded SBC frames.
1351  **
1352  ** Returns          void
1353  **
1354  *******************************************************************************/
btif_media_task(void * p)1355 int btif_media_task(void *p)
1356 {
1357     UINT16 event;
1358     BT_HDR *p_msg;
1359     UNUSED(p);
1360 
1361     VERBOSE("================ MEDIA TASK STARTING ================");
1362 
1363     btif_media_task_init();
1364 
1365     media_task_running = MEDIA_TASK_STATE_ON;
1366 
1367     raise_priority_a2dp(TASK_HIGH_MEDIA);
1368 
1369     while (1)
1370     {
1371         event = GKI_wait(0xffff, 0);
1372 
1373         VERBOSE("================= MEDIA TASK EVENT %d ===============", event);
1374 
1375         if (event & BTIF_MEDIA_TASK_CMD)
1376         {
1377             /* Process all messages in the queue */
1378             while ((p_msg = (BT_HDR *) GKI_read_mbox(BTIF_MEDIA_TASK_CMD_MBOX)) != NULL)
1379             {
1380                 btif_media_task_handle_cmd(p_msg);
1381             }
1382         }
1383 
1384         if (event & BTIF_MEDIA_TASK_DATA)
1385         {
1386             VERBOSE("================= Received Media Packets %d ===============", event);
1387             /* Process all messages in the queue */
1388             while ((p_msg = (BT_HDR *) GKI_read_mbox(BTIF_MEDIA_TASK_DATA_MBOX)) != NULL)
1389             {
1390                 btif_media_task_handle_media(p_msg);
1391             }
1392         }
1393 
1394         if (event & BTIF_MEDIA_AA_TASK_TIMER)
1395         {
1396             /* advance audio timer expiration */
1397             btif_media_task_aa_handle_timer();
1398         }
1399 
1400         if (event & BTIF_MEDIA_AVK_TASK_TIMER)
1401         {
1402 #if (BTA_AV_SINK_INCLUDED == TRUE)
1403             /* advance audio timer expiration for a2dp sink */
1404             btif_media_task_avk_handle_timer();
1405 #endif
1406         }
1407 
1408 
1409 
1410         VERBOSE("=============== MEDIA TASK EVENT %d DONE ============", event);
1411 
1412         /* When we get this event we exit the task  - should only happen on GKI_shutdown  */
1413         if (event & BTIF_MEDIA_TASK_KILL)
1414         {
1415             /* make sure no channels are restarted while shutting down */
1416             media_task_running = MEDIA_TASK_STATE_SHUTTING_DOWN;
1417 
1418             /* this calls blocks until uipc is fully closed */
1419             UIPC_Close(UIPC_CH_ID_ALL);
1420             break;
1421         }
1422     }
1423 
1424     /* Clear media task flag */
1425     media_task_running = MEDIA_TASK_STATE_OFF;
1426 
1427     APPL_TRACE_DEBUG("MEDIA TASK EXITING");
1428 
1429     return 0;
1430 }
1431 
1432 
1433 /*******************************************************************************
1434  **
1435  ** Function         btif_media_task_send_cmd_evt
1436  **
1437  ** Description
1438  **
1439  ** Returns          TRUE is success
1440  **
1441  *******************************************************************************/
btif_media_task_send_cmd_evt(UINT16 Evt)1442 BOOLEAN btif_media_task_send_cmd_evt(UINT16 Evt)
1443 {
1444     BT_HDR *p_buf;
1445     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
1446     {
1447         return FALSE;
1448     }
1449 
1450     p_buf->event = Evt;
1451 
1452     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
1453     return TRUE;
1454 }
1455 
1456 /*******************************************************************************
1457  **
1458  ** Function         btif_media_flush_q
1459  **
1460  ** Description
1461  **
1462  ** Returns          void
1463  **
1464  *******************************************************************************/
btif_media_flush_q(BUFFER_Q * p_q)1465 static void btif_media_flush_q(BUFFER_Q *p_q)
1466 {
1467     while (!GKI_queue_is_empty(p_q))
1468     {
1469         GKI_freebuf(GKI_dequeue(p_q));
1470     }
1471 }
1472 
1473 
1474 /*******************************************************************************
1475  **
1476  ** Function         btif_media_task_handle_cmd
1477  **
1478  ** Description
1479  **
1480  ** Returns          void
1481  **
1482  *******************************************************************************/
btif_media_task_handle_cmd(BT_HDR * p_msg)1483 static void btif_media_task_handle_cmd(BT_HDR *p_msg)
1484 {
1485     VERBOSE("btif_media_task_handle_cmd : %d %s", p_msg->event,
1486              dump_media_event(p_msg->event));
1487 
1488     switch (p_msg->event)
1489     {
1490 #if (BTA_AV_INCLUDED == TRUE)
1491     case BTIF_MEDIA_START_AA_TX:
1492         btif_media_task_aa_start_tx();
1493         break;
1494     case BTIF_MEDIA_STOP_AA_TX:
1495         btif_media_task_aa_stop_tx();
1496         break;
1497     case BTIF_MEDIA_SBC_ENC_INIT:
1498         btif_media_task_enc_init(p_msg);
1499         break;
1500     case BTIF_MEDIA_SBC_ENC_UPDATE:
1501         btif_media_task_enc_update(p_msg);
1502         break;
1503     case BTIF_MEDIA_AUDIO_FEEDING_INIT:
1504         btif_media_task_audio_feeding_init(p_msg);
1505         break;
1506     case BTIF_MEDIA_FLUSH_AA_TX:
1507         btif_media_task_aa_tx_flush(p_msg);
1508         break;
1509     case BTIF_MEDIA_UIPC_RX_RDY:
1510         btif_media_task_aa_handle_uipc_rx_rdy();
1511         break;
1512     case BTIF_MEDIA_AUDIO_SINK_CFG_UPDATE:
1513 #if (BTA_AV_SINK_INCLUDED == TRUE)
1514         btif_media_task_aa_handle_decoder_reset(p_msg);
1515 #endif
1516         break;
1517     case BTIF_MEDIA_AUDIO_SINK_START_DECODING:
1518         btif_media_task_aa_handle_start_decoding();
1519         break;
1520     case BTIF_MEDIA_AUDIO_SINK_CLEAR_TRACK:
1521 #if (BTA_AV_SINK_INCLUDED == TRUE)
1522         btif_media_task_aa_handle_clear_track();
1523 #endif
1524         break;
1525     case BTIF_MEDIA_AUDIO_SINK_STOP_DECODING:
1526         btif_media_task_aa_handle_stop_decoding();
1527         break;
1528      case BTIF_MEDIA_FLUSH_AA_RX:
1529         btif_media_task_aa_rx_flush();
1530         break;
1531 #endif
1532     default:
1533         APPL_TRACE_ERROR("ERROR in btif_media_task_handle_cmd unknown event %d", p_msg->event);
1534     }
1535     GKI_freebuf(p_msg);
1536     VERBOSE("btif_media_task_handle_cmd : %s DONE", dump_media_event(p_msg->event));
1537 }
1538 
1539 #if (BTA_AV_SINK_INCLUDED == TRUE)
1540 /*******************************************************************************
1541  **
1542  ** Function         btif_media_task_handle_inc_media
1543  **
1544  ** Description
1545  **
1546  ** Returns          void
1547  **
1548  *******************************************************************************/
btif_media_task_handle_inc_media(tBT_SBC_HDR * p_msg)1549 static void btif_media_task_handle_inc_media(tBT_SBC_HDR*p_msg)
1550 {
1551     UINT8 *sbc_start_frame = ((UINT8*)(p_msg + 1) + p_msg->offset + 1);
1552     int count;
1553     UINT32 pcmBytes, availPcmBytes;
1554     OI_INT16 *pcmDataPointer = pcmData; /*Will be overwritten on next packet receipt*/
1555     OI_STATUS status;
1556     int num_sbc_frames = p_msg->num_frames_to_be_processed;
1557     UINT32 sbc_frame_len = p_msg->len - 1;
1558     availPcmBytes = 2*sizeof(pcmData);
1559 
1560     if ((btif_media_cb.peer_sep == AVDT_TSEP_SNK) || (btif_media_cb.rx_flush))
1561     {
1562         APPL_TRACE_DEBUG(" State Changed happened in this tick ");
1563         return;
1564     }
1565 
1566     // ignore data if no one is listening
1567     if (!btif_media_cb.data_channel_open)
1568         return;
1569 
1570     APPL_TRACE_DEBUG("Number of sbc frames %d, frame_len %d", num_sbc_frames, sbc_frame_len);
1571 
1572     for(count = 0; count < num_sbc_frames && sbc_frame_len != 0; count ++)
1573     {
1574         pcmBytes = availPcmBytes;
1575         status = OI_CODEC_SBC_DecodeFrame(&context, (const OI_BYTE**)&sbc_start_frame,
1576                                                         (OI_UINT32 *)&sbc_frame_len,
1577                                                         (OI_INT16 *)pcmDataPointer,
1578                                                         (OI_UINT32 *)&pcmBytes);
1579         if (!OI_SUCCESS(status)) {
1580             APPL_TRACE_ERROR("Decoding failure: %d\n", status);
1581             break;
1582         }
1583         availPcmBytes -= pcmBytes;
1584         pcmDataPointer += pcmBytes/2;
1585         p_msg->offset += (p_msg->len - 1) - sbc_frame_len;
1586         p_msg->len = sbc_frame_len + 1;
1587     }
1588 
1589     UIPC_Send(UIPC_CH_ID_AV_AUDIO, 0, (UINT8 *)pcmData, (2*sizeof(pcmData) - availPcmBytes));
1590 }
1591 #endif
1592 
1593 /*******************************************************************************
1594  **
1595  ** Function         btif_media_task_handle_media
1596  **
1597  ** Description
1598  **
1599  ** Returns          void
1600  **
1601  *******************************************************************************/
btif_media_task_handle_media(BT_HDR * p_msg)1602 static void btif_media_task_handle_media(BT_HDR*p_msg)
1603 {
1604     APPL_TRACE_DEBUG(" btif_media_task_handle_media ");
1605     GKI_freebuf(p_msg);
1606 }
1607 #if (BTA_AV_INCLUDED == TRUE)
1608 /*******************************************************************************
1609  **
1610  ** Function         btif_media_task_enc_init_req
1611  **
1612  ** Description
1613  **
1614  ** Returns          TRUE is success
1615  **
1616  *******************************************************************************/
btif_media_task_enc_init_req(tBTIF_MEDIA_INIT_AUDIO * p_msg)1617 BOOLEAN btif_media_task_enc_init_req(tBTIF_MEDIA_INIT_AUDIO *p_msg)
1618 {
1619     tBTIF_MEDIA_INIT_AUDIO *p_buf;
1620     if (NULL == (p_buf = GKI_getbuf(sizeof(tBTIF_MEDIA_INIT_AUDIO))))
1621     {
1622         return FALSE;
1623     }
1624 
1625     memcpy(p_buf, p_msg, sizeof(tBTIF_MEDIA_INIT_AUDIO));
1626     p_buf->hdr.event = BTIF_MEDIA_SBC_ENC_INIT;
1627 
1628     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
1629     return TRUE;
1630 }
1631 
1632 /*******************************************************************************
1633  **
1634  ** Function         btif_media_task_enc_update_req
1635  **
1636  ** Description
1637  **
1638  ** Returns          TRUE is success
1639  **
1640  *******************************************************************************/
btif_media_task_enc_update_req(tBTIF_MEDIA_UPDATE_AUDIO * p_msg)1641 BOOLEAN btif_media_task_enc_update_req(tBTIF_MEDIA_UPDATE_AUDIO *p_msg)
1642 {
1643     tBTIF_MEDIA_UPDATE_AUDIO *p_buf;
1644     if (NULL == (p_buf = GKI_getbuf(sizeof(tBTIF_MEDIA_UPDATE_AUDIO))))
1645     {
1646         return FALSE;
1647     }
1648 
1649     memcpy(p_buf, p_msg, sizeof(tBTIF_MEDIA_UPDATE_AUDIO));
1650     p_buf->hdr.event = BTIF_MEDIA_SBC_ENC_UPDATE;
1651 
1652     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
1653     return TRUE;
1654 }
1655 
1656 /*******************************************************************************
1657  **
1658  ** Function         btif_media_task_audio_feeding_init_req
1659  **
1660  ** Description
1661  **
1662  ** Returns          TRUE is success
1663  **
1664  *******************************************************************************/
btif_media_task_audio_feeding_init_req(tBTIF_MEDIA_INIT_AUDIO_FEEDING * p_msg)1665 BOOLEAN btif_media_task_audio_feeding_init_req(tBTIF_MEDIA_INIT_AUDIO_FEEDING *p_msg)
1666 {
1667     tBTIF_MEDIA_INIT_AUDIO_FEEDING *p_buf;
1668     if (NULL == (p_buf = GKI_getbuf(sizeof(tBTIF_MEDIA_INIT_AUDIO_FEEDING))))
1669     {
1670         return FALSE;
1671     }
1672 
1673     memcpy(p_buf, p_msg, sizeof(tBTIF_MEDIA_INIT_AUDIO_FEEDING));
1674     p_buf->hdr.event = BTIF_MEDIA_AUDIO_FEEDING_INIT;
1675 
1676     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
1677     return TRUE;
1678 }
1679 
1680 /*******************************************************************************
1681  **
1682  ** Function         btif_media_task_start_aa_req
1683  **
1684  ** Description
1685  **
1686  ** Returns          TRUE is success
1687  **
1688  *******************************************************************************/
btif_media_task_start_aa_req(void)1689 BOOLEAN btif_media_task_start_aa_req(void)
1690 {
1691     BT_HDR *p_buf;
1692     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
1693     {
1694         APPL_TRACE_EVENT("GKI failed");
1695         return FALSE;
1696     }
1697 
1698     p_buf->event = BTIF_MEDIA_START_AA_TX;
1699 
1700     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
1701     return TRUE;
1702 }
1703 
1704 /*******************************************************************************
1705  **
1706  ** Function         btif_media_task_stop_aa_req
1707  **
1708  ** Description
1709  **
1710  ** Returns          TRUE is success
1711  **
1712  *******************************************************************************/
btif_media_task_stop_aa_req(void)1713 BOOLEAN btif_media_task_stop_aa_req(void)
1714 {
1715     BT_HDR *p_buf;
1716     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
1717     {
1718         return FALSE;
1719     }
1720 
1721     p_buf->event = BTIF_MEDIA_STOP_AA_TX;
1722 
1723     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
1724     return TRUE;
1725 }
1726 /*******************************************************************************
1727  **
1728  ** Function         btif_media_task_aa_rx_flush_req
1729  **
1730  ** Description
1731  **
1732  ** Returns          TRUE is success
1733  **
1734  *******************************************************************************/
btif_media_task_aa_rx_flush_req(void)1735 BOOLEAN btif_media_task_aa_rx_flush_req(void)
1736 {
1737     BT_HDR *p_buf;
1738 
1739     if (GKI_queue_is_empty(&(btif_media_cb.RxSbcQ))== TRUE) /*  Que is already empty */
1740         return TRUE;
1741 
1742     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
1743     {
1744         return FALSE;
1745     }
1746 
1747     p_buf->event = BTIF_MEDIA_FLUSH_AA_RX;
1748 
1749     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
1750     return TRUE;
1751 }
1752 
1753 /*******************************************************************************
1754  **
1755  ** Function         btif_media_task_aa_tx_flush_req
1756  **
1757  ** Description
1758  **
1759  ** Returns          TRUE is success
1760  **
1761  *******************************************************************************/
btif_media_task_aa_tx_flush_req(void)1762 BOOLEAN btif_media_task_aa_tx_flush_req(void)
1763 {
1764     BT_HDR *p_buf;
1765     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
1766     {
1767         return FALSE;
1768     }
1769 
1770     p_buf->event = BTIF_MEDIA_FLUSH_AA_TX;
1771 
1772     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
1773     return TRUE;
1774 }
1775 /*******************************************************************************
1776  **
1777  ** Function         btif_media_task_aa_rx_flush
1778  **
1779  ** Description
1780  **
1781  ** Returns          void
1782  **
1783  *******************************************************************************/
btif_media_task_aa_rx_flush(void)1784 static void btif_media_task_aa_rx_flush(void)
1785 {
1786     /* Flush all enqueued GKI SBC  buffers (encoded) */
1787     APPL_TRACE_DEBUG("btif_media_task_aa_rx_flush");
1788 
1789     btif_media_flush_q(&(btif_media_cb.RxSbcQ));
1790 }
1791 
1792 
1793 /*******************************************************************************
1794  **
1795  ** Function         btif_media_task_aa_tx_flush
1796  **
1797  ** Description
1798  **
1799  ** Returns          void
1800  **
1801  *******************************************************************************/
btif_media_task_aa_tx_flush(BT_HDR * p_msg)1802 static void btif_media_task_aa_tx_flush(BT_HDR *p_msg)
1803 {
1804     UNUSED(p_msg);
1805 
1806     /* Flush all enqueued GKI music buffers (encoded) */
1807     APPL_TRACE_DEBUG("btif_media_task_aa_tx_flush");
1808 
1809     btif_media_cb.media_feeding_state.pcm.counter = 0;
1810     btif_media_cb.media_feeding_state.pcm.aa_feed_residue = 0;
1811 
1812     btif_media_flush_q(&(btif_media_cb.TxAaQ));
1813 
1814     UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_REQ_RX_FLUSH, NULL);
1815 }
1816 
1817 /*******************************************************************************
1818  **
1819  ** Function       btif_media_task_enc_init
1820  **
1821  ** Description    Initialize encoding task
1822  **
1823  ** Returns        void
1824  **
1825  *******************************************************************************/
btif_media_task_enc_init(BT_HDR * p_msg)1826 static void btif_media_task_enc_init(BT_HDR *p_msg)
1827 {
1828     tBTIF_MEDIA_INIT_AUDIO *pInitAudio = (tBTIF_MEDIA_INIT_AUDIO *) p_msg;
1829 
1830     APPL_TRACE_DEBUG("btif_media_task_enc_init");
1831 
1832     btif_media_cb.timestamp = 0;
1833 
1834     /* SBC encoder config (enforced even if not used) */
1835     btif_media_cb.encoder.s16ChannelMode = pInitAudio->ChannelMode;
1836     btif_media_cb.encoder.s16NumOfSubBands = pInitAudio->NumOfSubBands;
1837     btif_media_cb.encoder.s16NumOfBlocks = pInitAudio->NumOfBlocks;
1838     btif_media_cb.encoder.s16AllocationMethod = pInitAudio->AllocationMethod;
1839     btif_media_cb.encoder.s16SamplingFreq = pInitAudio->SamplingFreq;
1840 
1841     btif_media_cb.encoder.u16BitRate = btif_media_task_get_sbc_rate();
1842 
1843     /* Default transcoding is PCM to SBC, modified by feeding configuration */
1844     btif_media_cb.TxTranscoding = BTIF_MEDIA_TRSCD_PCM_2_SBC;
1845     btif_media_cb.TxAaMtuSize = ((BTIF_MEDIA_AA_BUF_SIZE-BTIF_MEDIA_AA_SBC_OFFSET-sizeof(BT_HDR))
1846             < pInitAudio->MtuSize) ? (BTIF_MEDIA_AA_BUF_SIZE - BTIF_MEDIA_AA_SBC_OFFSET
1847             - sizeof(BT_HDR)) : pInitAudio->MtuSize;
1848 
1849     APPL_TRACE_EVENT("btif_media_task_enc_init busy %d, mtu %d, peer mtu %d",
1850                      btif_media_cb.busy_level, btif_media_cb.TxAaMtuSize, pInitAudio->MtuSize);
1851     APPL_TRACE_EVENT("      ch mode %d, subnd %d, nb blk %d, alloc %d, rate %d, freq %d",
1852             btif_media_cb.encoder.s16ChannelMode, btif_media_cb.encoder.s16NumOfSubBands,
1853             btif_media_cb.encoder.s16NumOfBlocks,
1854             btif_media_cb.encoder.s16AllocationMethod, btif_media_cb.encoder.u16BitRate,
1855             btif_media_cb.encoder.s16SamplingFreq);
1856 
1857     /* Reset entirely the SBC encoder */
1858     SBC_Encoder_Init(&(btif_media_cb.encoder));
1859     APPL_TRACE_DEBUG("btif_media_task_enc_init bit pool %d", btif_media_cb.encoder.s16BitPool);
1860 }
1861 
1862 /*******************************************************************************
1863  **
1864  ** Function       btif_media_task_enc_update
1865  **
1866  ** Description    Update encoding task
1867  **
1868  ** Returns        void
1869  **
1870  *******************************************************************************/
1871 
btif_media_task_enc_update(BT_HDR * p_msg)1872 static void btif_media_task_enc_update(BT_HDR *p_msg)
1873 {
1874     tBTIF_MEDIA_UPDATE_AUDIO * pUpdateAudio = (tBTIF_MEDIA_UPDATE_AUDIO *) p_msg;
1875     SBC_ENC_PARAMS *pstrEncParams = &btif_media_cb.encoder;
1876     UINT16 s16SamplingFreq;
1877     SINT16 s16BitPool = 0;
1878     SINT16 s16BitRate;
1879     SINT16 s16FrameLen;
1880     UINT8 protect = 0;
1881 
1882     APPL_TRACE_DEBUG("btif_media_task_enc_update : minmtu %d, maxbp %d minbp %d",
1883             pUpdateAudio->MinMtuSize, pUpdateAudio->MaxBitPool, pUpdateAudio->MinBitPool);
1884 
1885     /* Only update the bitrate and MTU size while timer is running to make sure it has been initialized */
1886     //if (btif_media_cb.is_tx_timer)
1887     {
1888         btif_media_cb.TxAaMtuSize = ((BTIF_MEDIA_AA_BUF_SIZE -
1889                                       BTIF_MEDIA_AA_SBC_OFFSET - sizeof(BT_HDR))
1890                 < pUpdateAudio->MinMtuSize) ? (BTIF_MEDIA_AA_BUF_SIZE - BTIF_MEDIA_AA_SBC_OFFSET
1891                 - sizeof(BT_HDR)) : pUpdateAudio->MinMtuSize;
1892 
1893         /* Set the initial target bit rate */
1894         pstrEncParams->u16BitRate = btif_media_task_get_sbc_rate();
1895 
1896         if (pstrEncParams->s16SamplingFreq == SBC_sf16000)
1897             s16SamplingFreq = 16000;
1898         else if (pstrEncParams->s16SamplingFreq == SBC_sf32000)
1899             s16SamplingFreq = 32000;
1900         else if (pstrEncParams->s16SamplingFreq == SBC_sf44100)
1901             s16SamplingFreq = 44100;
1902         else
1903             s16SamplingFreq = 48000;
1904 
1905         do
1906         {
1907             if (pstrEncParams->s16NumOfBlocks == 0 || pstrEncParams->s16NumOfSubBands == 0
1908                 || pstrEncParams->s16NumOfChannels == 0)
1909             {
1910                 APPL_TRACE_ERROR("btif_media_task_enc_update() - Avoiding division by zero...");
1911                 APPL_TRACE_ERROR("btif_media_task_enc_update() - block=%d, subBands=%d, channels=%d",
1912                     pstrEncParams->s16NumOfBlocks, pstrEncParams->s16NumOfSubBands,
1913                     pstrEncParams->s16NumOfChannels);
1914                 break;
1915             }
1916 
1917             if ((pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) ||
1918                 (pstrEncParams->s16ChannelMode == SBC_STEREO) )
1919             {
1920                 s16BitPool = (SINT16)( (pstrEncParams->u16BitRate *
1921                     pstrEncParams->s16NumOfSubBands * 1000 / s16SamplingFreq)
1922                     -( (32 + (4 * pstrEncParams->s16NumOfSubBands *
1923                     pstrEncParams->s16NumOfChannels)
1924                     + ( (pstrEncParams->s16ChannelMode - 2) *
1925                     pstrEncParams->s16NumOfSubBands )   )
1926                     / pstrEncParams->s16NumOfBlocks) );
1927 
1928                 s16FrameLen = 4 + (4*pstrEncParams->s16NumOfSubBands*
1929                     pstrEncParams->s16NumOfChannels)/8
1930                     + ( ((pstrEncParams->s16ChannelMode - 2) *
1931                     pstrEncParams->s16NumOfSubBands)
1932                     + (pstrEncParams->s16NumOfBlocks * s16BitPool) ) / 8;
1933 
1934                 s16BitRate = (8 * s16FrameLen * s16SamplingFreq)
1935                     / (pstrEncParams->s16NumOfSubBands *
1936                     pstrEncParams->s16NumOfBlocks * 1000);
1937 
1938                 if (s16BitRate > pstrEncParams->u16BitRate)
1939                     s16BitPool--;
1940 
1941                 if(pstrEncParams->s16NumOfSubBands == 8)
1942                     s16BitPool = (s16BitPool > 255) ? 255 : s16BitPool;
1943                 else
1944                     s16BitPool = (s16BitPool > 128) ? 128 : s16BitPool;
1945             }
1946             else
1947             {
1948                 s16BitPool = (SINT16)( ((pstrEncParams->s16NumOfSubBands *
1949                     pstrEncParams->u16BitRate * 1000)
1950                     / (s16SamplingFreq * pstrEncParams->s16NumOfChannels))
1951                     -( ( (32 / pstrEncParams->s16NumOfChannels) +
1952                     (4 * pstrEncParams->s16NumOfSubBands) )
1953                     /   pstrEncParams->s16NumOfBlocks ) );
1954 
1955                 pstrEncParams->s16BitPool = (s16BitPool >
1956                     (16 * pstrEncParams->s16NumOfSubBands))
1957                     ? (16*pstrEncParams->s16NumOfSubBands) : s16BitPool;
1958             }
1959 
1960             if (s16BitPool < 0)
1961             {
1962                 s16BitPool = 0;
1963             }
1964 
1965             APPL_TRACE_EVENT("bitpool candidate : %d (%d kbps)",
1966                          s16BitPool, pstrEncParams->u16BitRate);
1967 
1968             if (s16BitPool > pUpdateAudio->MaxBitPool)
1969             {
1970                 APPL_TRACE_DEBUG("btif_media_task_enc_update computed bitpool too large (%d)",
1971                                     s16BitPool);
1972                 /* Decrease bitrate */
1973                 btif_media_cb.encoder.u16BitRate -= BTIF_MEDIA_BITRATE_STEP;
1974                 /* Record that we have decreased the bitrate */
1975                 protect |= 1;
1976             }
1977             else if (s16BitPool < pUpdateAudio->MinBitPool)
1978             {
1979                 APPL_TRACE_WARNING("btif_media_task_enc_update computed bitpool too small (%d)", s16BitPool);
1980 
1981                 /* Increase bitrate */
1982                 UINT16 previous_u16BitRate = btif_media_cb.encoder.u16BitRate;
1983                 btif_media_cb.encoder.u16BitRate += BTIF_MEDIA_BITRATE_STEP;
1984                 /* Record that we have increased the bitrate */
1985                 protect |= 2;
1986                 /* Check over-flow */
1987                 if (btif_media_cb.encoder.u16BitRate < previous_u16BitRate)
1988                     protect |= 3;
1989             }
1990             else
1991             {
1992                 break;
1993             }
1994             /* In case we have already increased and decreased the bitrate, just stop */
1995             if (protect == 3)
1996             {
1997                 APPL_TRACE_ERROR("btif_media_task_enc_update could not find bitpool in range");
1998                 break;
1999             }
2000         } while (1);
2001 
2002         /* Finally update the bitpool in the encoder structure */
2003         pstrEncParams->s16BitPool = s16BitPool;
2004 
2005         APPL_TRACE_DEBUG("btif_media_task_enc_update final bit rate %d, final bit pool %d",
2006                 btif_media_cb.encoder.u16BitRate, btif_media_cb.encoder.s16BitPool);
2007 
2008         /* make sure we reinitialize encoder with new settings */
2009         SBC_Encoder_Init(&(btif_media_cb.encoder));
2010     }
2011 }
2012 
2013 /*******************************************************************************
2014  **
2015  ** Function         btif_media_task_pcm2sbc_init
2016  **
2017  ** Description      Init encoding task for PCM to SBC according to feeding
2018  **
2019  ** Returns          void
2020  **
2021  *******************************************************************************/
btif_media_task_pcm2sbc_init(tBTIF_MEDIA_INIT_AUDIO_FEEDING * p_feeding)2022 static void btif_media_task_pcm2sbc_init(tBTIF_MEDIA_INIT_AUDIO_FEEDING * p_feeding)
2023 {
2024     BOOLEAN reconfig_needed = FALSE;
2025 
2026     APPL_TRACE_DEBUG("PCM feeding:");
2027     APPL_TRACE_DEBUG("sampling_freq:%d", p_feeding->feeding.cfg.pcm.sampling_freq);
2028     APPL_TRACE_DEBUG("num_channel:%d", p_feeding->feeding.cfg.pcm.num_channel);
2029     APPL_TRACE_DEBUG("bit_per_sample:%d", p_feeding->feeding.cfg.pcm.bit_per_sample);
2030 
2031     /* Check the PCM feeding sampling_freq */
2032     switch (p_feeding->feeding.cfg.pcm.sampling_freq)
2033     {
2034         case  8000:
2035         case 12000:
2036         case 16000:
2037         case 24000:
2038         case 32000:
2039         case 48000:
2040             /* For these sampling_freq the AV connection must be 48000 */
2041             if (btif_media_cb.encoder.s16SamplingFreq != SBC_sf48000)
2042             {
2043                 /* Reconfiguration needed at 48000 */
2044                 APPL_TRACE_DEBUG("SBC Reconfiguration needed at 48000");
2045                 btif_media_cb.encoder.s16SamplingFreq = SBC_sf48000;
2046                 reconfig_needed = TRUE;
2047             }
2048             break;
2049 
2050         case 11025:
2051         case 22050:
2052         case 44100:
2053             /* For these sampling_freq the AV connection must be 44100 */
2054             if (btif_media_cb.encoder.s16SamplingFreq != SBC_sf44100)
2055             {
2056                 /* Reconfiguration needed at 44100 */
2057                 APPL_TRACE_DEBUG("SBC Reconfiguration needed at 44100");
2058                 btif_media_cb.encoder.s16SamplingFreq = SBC_sf44100;
2059                 reconfig_needed = TRUE;
2060             }
2061             break;
2062         default:
2063             APPL_TRACE_DEBUG("Feeding PCM sampling_freq unsupported");
2064             break;
2065     }
2066 
2067     /* Some AV Headsets do not support Mono => always ask for Stereo */
2068     if (btif_media_cb.encoder.s16ChannelMode == SBC_MONO)
2069     {
2070         APPL_TRACE_DEBUG("SBC Reconfiguration needed in Stereo");
2071         btif_media_cb.encoder.s16ChannelMode = SBC_JOINT_STEREO;
2072         reconfig_needed = TRUE;
2073     }
2074 
2075     if (reconfig_needed != FALSE)
2076     {
2077         APPL_TRACE_DEBUG("btif_media_task_pcm2sbc_init :: mtu %d", btif_media_cb.TxAaMtuSize);
2078         APPL_TRACE_DEBUG("ch mode %d, nbsubd %d, nb %d, alloc %d, rate %d, freq %d",
2079                 btif_media_cb.encoder.s16ChannelMode,
2080                 btif_media_cb.encoder.s16NumOfSubBands, btif_media_cb.encoder.s16NumOfBlocks,
2081                 btif_media_cb.encoder.s16AllocationMethod, btif_media_cb.encoder.u16BitRate,
2082                 btif_media_cb.encoder.s16SamplingFreq);
2083 
2084         SBC_Encoder_Init(&(btif_media_cb.encoder));
2085     }
2086     else
2087     {
2088         APPL_TRACE_DEBUG("btif_media_task_pcm2sbc_init no SBC reconfig needed");
2089     }
2090 }
2091 
2092 
2093 /*******************************************************************************
2094  **
2095  ** Function         btif_media_task_audio_feeding_init
2096  **
2097  ** Description      Initialize the audio path according to the feeding format
2098  **
2099  ** Returns          void
2100  **
2101  *******************************************************************************/
btif_media_task_audio_feeding_init(BT_HDR * p_msg)2102 static void btif_media_task_audio_feeding_init(BT_HDR *p_msg)
2103 {
2104     tBTIF_MEDIA_INIT_AUDIO_FEEDING *p_feeding = (tBTIF_MEDIA_INIT_AUDIO_FEEDING *) p_msg;
2105 
2106     APPL_TRACE_DEBUG("btif_media_task_audio_feeding_init format:%d", p_feeding->feeding.format);
2107 
2108     /* Save Media Feeding information */
2109     btif_media_cb.feeding_mode = p_feeding->feeding_mode;
2110     btif_media_cb.media_feeding = p_feeding->feeding;
2111 
2112     /* Handle different feeding formats */
2113     switch (p_feeding->feeding.format)
2114     {
2115         case BTIF_AV_CODEC_PCM:
2116             btif_media_cb.TxTranscoding = BTIF_MEDIA_TRSCD_PCM_2_SBC;
2117             btif_media_task_pcm2sbc_init(p_feeding);
2118             break;
2119 
2120         default :
2121             APPL_TRACE_ERROR("unknown feeding format %d", p_feeding->feeding.format);
2122             break;
2123     }
2124 }
2125 
btif_a2dp_get_track_frequency(UINT8 frequency)2126 int btif_a2dp_get_track_frequency(UINT8 frequency) {
2127     int freq = 48000;
2128     switch (frequency) {
2129         case A2D_SBC_IE_SAMP_FREQ_16:
2130             freq = 16000;
2131             break;
2132         case A2D_SBC_IE_SAMP_FREQ_32:
2133             freq = 32000;
2134             break;
2135         case A2D_SBC_IE_SAMP_FREQ_44:
2136             freq = 44100;
2137             break;
2138         case A2D_SBC_IE_SAMP_FREQ_48:
2139             freq = 48000;
2140             break;
2141     }
2142     return freq;
2143 }
2144 
btif_a2dp_get_track_channel_count(UINT8 channeltype)2145 int btif_a2dp_get_track_channel_count(UINT8 channeltype) {
2146     int count = 1;
2147     switch (channeltype) {
2148         case A2D_SBC_IE_CH_MD_MONO:
2149             count = 1;
2150             break;
2151         case A2D_SBC_IE_CH_MD_DUAL:
2152         case A2D_SBC_IE_CH_MD_STEREO:
2153         case A2D_SBC_IE_CH_MD_JOINT:
2154             count = 2;
2155             break;
2156     }
2157     return count;
2158 }
2159 
btif_a2dp_set_peer_sep(UINT8 sep)2160 void btif_a2dp_set_peer_sep(UINT8 sep) {
2161     btif_media_cb.peer_sep = sep;
2162 }
2163 
2164 /*******************************************************************************
2165  **
2166  ** Function         btif_media_task_aa_handle_stop_decoding
2167  **
2168  ** Description
2169  **
2170  ** Returns          void
2171  **
2172  *******************************************************************************/
btif_media_task_aa_handle_stop_decoding(void)2173 static void btif_media_task_aa_handle_stop_decoding(void )
2174 {
2175     btif_media_cb.is_rx_timer = FALSE;
2176     GKI_stop_timer(BTIF_MEDIA_AVK_TASK_TIMER_ID);
2177 }
2178 
2179 /*******************************************************************************
2180  **
2181  ** Function         btif_media_task_aa_handle_start_decoding
2182  **
2183  ** Description
2184  **
2185  ** Returns          void
2186  **
2187  *******************************************************************************/
btif_media_task_aa_handle_start_decoding(void)2188 static void btif_media_task_aa_handle_start_decoding(void )
2189 {
2190     if(btif_media_cb.is_rx_timer == TRUE)
2191         return;
2192     btif_media_cb.is_rx_timer = TRUE;
2193     GKI_start_timer(BTIF_MEDIA_AVK_TASK_TIMER_ID, GKI_MS_TO_TICKS(BTIF_SINK_MEDIA_TIME_TICK), TRUE);
2194 }
2195 
2196 #if (BTA_AV_SINK_INCLUDED == TRUE)
2197 
btif_media_task_aa_handle_clear_track(void)2198 static void btif_media_task_aa_handle_clear_track (void)
2199 {
2200     APPL_TRACE_DEBUG("btif_media_task_aa_handle_clear_track");
2201 }
2202 
2203 /*******************************************************************************
2204  **
2205  ** Function         btif_media_task_aa_handle_decoder_reset
2206  **
2207  ** Description
2208  **
2209  ** Returns          void
2210  **
2211  *******************************************************************************/
btif_media_task_aa_handle_decoder_reset(BT_HDR * p_msg)2212 static void btif_media_task_aa_handle_decoder_reset(BT_HDR *p_msg)
2213 {
2214     tBTIF_MEDIA_SINK_CFG_UPDATE *p_buf = (tBTIF_MEDIA_SINK_CFG_UPDATE*) p_msg;
2215     tA2D_STATUS a2d_status;
2216     tA2D_SBC_CIE sbc_cie;
2217     OI_STATUS       status;
2218     UINT32          freq_multiple = 48*20; /* frequency multiple for 20ms of data , initialize with 48K*/
2219     UINT32          num_blocks = 16;
2220     UINT32          num_subbands = 8;
2221 
2222     APPL_TRACE_DEBUG("btif_media_task_aa_handle_decoder_reset p_codec_info[%x:%x:%x:%x:%x:%x]",
2223             p_buf->codec_info[1], p_buf->codec_info[2], p_buf->codec_info[3],
2224             p_buf->codec_info[4], p_buf->codec_info[5], p_buf->codec_info[6]);
2225 
2226     a2d_status = A2D_ParsSbcInfo(&sbc_cie, p_buf->codec_info, FALSE);
2227     if (a2d_status != A2D_SUCCESS)
2228     {
2229         APPL_TRACE_ERROR("ERROR dump_codec_info A2D_ParsSbcInfo fail:%d", a2d_status);
2230         return;
2231     }
2232 
2233     btif_media_cb.sample_rate = btif_a2dp_get_track_frequency(sbc_cie.samp_freq);
2234     btif_media_cb.channel_count = btif_a2dp_get_track_channel_count(sbc_cie.ch_mode);
2235 
2236     btif_media_cb.rx_flush = FALSE;
2237     APPL_TRACE_DEBUG("Reset to sink role");
2238     status = OI_CODEC_SBC_DecoderReset(&context, contextData, sizeof(contextData), 2, 2, FALSE);
2239     if (!OI_SUCCESS(status)) {
2240         APPL_TRACE_ERROR("OI_CODEC_SBC_DecoderReset failed with error code %d\n", status);
2241     }
2242 
2243     UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
2244 
2245     switch(sbc_cie.samp_freq)
2246     {
2247         case A2D_SBC_IE_SAMP_FREQ_16:
2248             APPL_TRACE_DEBUG("\tsamp_freq:%d (16000)", sbc_cie.samp_freq);
2249             freq_multiple = 16*20;
2250             break;
2251         case A2D_SBC_IE_SAMP_FREQ_32:
2252             APPL_TRACE_DEBUG("\tsamp_freq:%d (32000)", sbc_cie.samp_freq);
2253             freq_multiple = 32*20;
2254             break;
2255         case A2D_SBC_IE_SAMP_FREQ_44:
2256             APPL_TRACE_DEBUG("\tsamp_freq:%d (44100)", sbc_cie.samp_freq);
2257             freq_multiple = 441*2;
2258             break;
2259         case A2D_SBC_IE_SAMP_FREQ_48:
2260             APPL_TRACE_DEBUG("\tsamp_freq:%d (48000)", sbc_cie.samp_freq);
2261             freq_multiple = 48*20;
2262             break;
2263         default:
2264             APPL_TRACE_DEBUG(" Unknown Frequency ");
2265             break;
2266     }
2267 
2268     switch(sbc_cie.ch_mode)
2269     {
2270         case A2D_SBC_IE_CH_MD_MONO:
2271             APPL_TRACE_DEBUG("\tch_mode:%d (Mono)", sbc_cie.ch_mode);
2272             break;
2273         case A2D_SBC_IE_CH_MD_DUAL:
2274             APPL_TRACE_DEBUG("\tch_mode:%d (DUAL)", sbc_cie.ch_mode);
2275             break;
2276         case A2D_SBC_IE_CH_MD_STEREO:
2277             APPL_TRACE_DEBUG("\tch_mode:%d (STEREO)", sbc_cie.ch_mode);
2278             break;
2279         case A2D_SBC_IE_CH_MD_JOINT:
2280             APPL_TRACE_DEBUG("\tch_mode:%d (JOINT)", sbc_cie.ch_mode);
2281             break;
2282         default:
2283             APPL_TRACE_DEBUG(" Unknown Mode ");
2284             break;
2285     }
2286 
2287     switch(sbc_cie.block_len)
2288     {
2289         case A2D_SBC_IE_BLOCKS_4:
2290             APPL_TRACE_DEBUG("\tblock_len:%d (4)", sbc_cie.block_len);
2291             num_blocks = 4;
2292             break;
2293         case A2D_SBC_IE_BLOCKS_8:
2294             APPL_TRACE_DEBUG("\tblock_len:%d (8)", sbc_cie.block_len);
2295             num_blocks = 8;
2296             break;
2297         case A2D_SBC_IE_BLOCKS_12:
2298             APPL_TRACE_DEBUG("\tblock_len:%d (12)", sbc_cie.block_len);
2299             num_blocks = 12;
2300             break;
2301         case A2D_SBC_IE_BLOCKS_16:
2302             APPL_TRACE_DEBUG("\tblock_len:%d (16)", sbc_cie.block_len);
2303             num_blocks = 16;
2304             break;
2305         default:
2306             APPL_TRACE_DEBUG(" Unknown BlockLen ");
2307             break;
2308     }
2309 
2310     switch(sbc_cie.num_subbands)
2311     {
2312         case A2D_SBC_IE_SUBBAND_4:
2313             APPL_TRACE_DEBUG("\tnum_subbands:%d (4)", sbc_cie.num_subbands);
2314             num_subbands = 4;
2315             break;
2316         case A2D_SBC_IE_SUBBAND_8:
2317             APPL_TRACE_DEBUG("\tnum_subbands:%d (8)", sbc_cie.num_subbands);
2318             num_subbands = 8;
2319             break;
2320         default:
2321             APPL_TRACE_DEBUG(" Unknown SubBands ");
2322             break;
2323     }
2324 
2325     switch(sbc_cie.alloc_mthd)
2326     {
2327         case A2D_SBC_IE_ALLOC_MD_S:
2328             APPL_TRACE_DEBUG("\talloc_mthd:%d (SNR)", sbc_cie.alloc_mthd);
2329             break;
2330         case A2D_SBC_IE_ALLOC_MD_L:
2331             APPL_TRACE_DEBUG("\talloc_mthd:%d (Loudness)", sbc_cie.alloc_mthd);
2332             break;
2333         default:
2334             APPL_TRACE_DEBUG(" Unknown Allocation Method");
2335             break;
2336     }
2337 
2338     APPL_TRACE_DEBUG("\tBit pool Min:%d Max:%d", sbc_cie.min_bitpool, sbc_cie.max_bitpool);
2339 
2340     btif_media_cb.frames_to_process = ((freq_multiple)/(num_blocks*num_subbands)) + 1;
2341     APPL_TRACE_DEBUG(" Frames to be processed in 20 ms %d",btif_media_cb.frames_to_process);
2342 }
2343 #endif
2344 
2345 /*******************************************************************************
2346  **
2347  ** Function         btif_media_task_feeding_state_reset
2348  **
2349  ** Description      Reset the media feeding state
2350  **
2351  ** Returns          void
2352  **
2353  *******************************************************************************/
btif_media_task_feeding_state_reset(void)2354 static void btif_media_task_feeding_state_reset(void)
2355 {
2356     /* By default, just clear the entire state */
2357     memset(&btif_media_cb.media_feeding_state, 0, sizeof(btif_media_cb.media_feeding_state));
2358 
2359     if (btif_media_cb.TxTranscoding == BTIF_MEDIA_TRSCD_PCM_2_SBC)
2360     {
2361         btif_media_cb.media_feeding_state.pcm.bytes_per_tick =
2362                 (btif_media_cb.media_feeding.cfg.pcm.sampling_freq *
2363                  btif_media_cb.media_feeding.cfg.pcm.bit_per_sample / 8 *
2364                  btif_media_cb.media_feeding.cfg.pcm.num_channel *
2365                  BTIF_MEDIA_TIME_TICK)/1000;
2366 
2367         APPL_TRACE_WARNING("pcm bytes per tick %d",
2368                             (int)btif_media_cb.media_feeding_state.pcm.bytes_per_tick);
2369     }
2370 }
2371 /*******************************************************************************
2372  **
2373  ** Function         btif_media_task_aa_start_tx
2374  **
2375  ** Description      Start media task encoding
2376  **
2377  ** Returns          void
2378  **
2379  *******************************************************************************/
btif_media_task_aa_start_tx(void)2380 static void btif_media_task_aa_start_tx(void)
2381 {
2382     APPL_TRACE_DEBUG("btif_media_task_aa_start_tx is timer %d, feeding mode %d",
2383              btif_media_cb.is_tx_timer, btif_media_cb.feeding_mode);
2384 
2385     /* Use a timer to poll the UIPC, get rid of the UIPC call back */
2386     // UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_REG_CBACK, NULL);
2387 
2388     btif_media_cb.is_tx_timer = TRUE;
2389     last_frame_us = 0;
2390 
2391     /* Reset the media feeding state */
2392     btif_media_task_feeding_state_reset();
2393 
2394     APPL_TRACE_EVENT("starting timer %d ticks (%d)",
2395                   GKI_MS_TO_TICKS(BTIF_MEDIA_TIME_TICK), TICKS_PER_SEC);
2396 
2397     GKI_start_timer(BTIF_MEDIA_AA_TASK_TIMER_ID, GKI_MS_TO_TICKS(BTIF_MEDIA_TIME_TICK), TRUE);
2398 }
2399 
2400 /*******************************************************************************
2401  **
2402  ** Function         btif_media_task_aa_stop_tx
2403  **
2404  ** Description      Stop media task encoding
2405  **
2406  ** Returns          void
2407  **
2408  *******************************************************************************/
btif_media_task_aa_stop_tx(void)2409 static void btif_media_task_aa_stop_tx(void)
2410 {
2411     APPL_TRACE_DEBUG("btif_media_task_aa_stop_tx is timer: %d", btif_media_cb.is_tx_timer);
2412 
2413     /* Stop the timer first */
2414     GKI_stop_timer(BTIF_MEDIA_AA_TASK_TIMER_ID);
2415     btif_media_cb.is_tx_timer = FALSE;
2416 
2417     UIPC_Close(UIPC_CH_ID_AV_AUDIO);
2418 
2419     /* audio engine stopped, reset tx suspended flag */
2420     btif_media_cb.tx_flush = 0;
2421     last_frame_us = 0;
2422 
2423     /* Reset the media feeding state */
2424     btif_media_task_feeding_state_reset();
2425 }
2426 
2427 /*******************************************************************************
2428  **
2429  ** Function         btif_get_num_aa_frame
2430  **
2431  ** Description
2432  **
2433  ** Returns          The number of media frames in this time slice
2434  **
2435  *******************************************************************************/
btif_get_num_aa_frame(void)2436 static UINT8 btif_get_num_aa_frame(void)
2437 {
2438     UINT8 result=0;
2439 
2440     switch (btif_media_cb.TxTranscoding)
2441     {
2442         case BTIF_MEDIA_TRSCD_PCM_2_SBC:
2443         {
2444             UINT32 pcm_bytes_per_frame = btif_media_cb.encoder.s16NumOfSubBands *
2445                              btif_media_cb.encoder.s16NumOfBlocks *
2446                              btif_media_cb.media_feeding.cfg.pcm.num_channel *
2447                              btif_media_cb.media_feeding.cfg.pcm.bit_per_sample / 8;
2448 
2449             UINT32 us_this_tick = BTIF_MEDIA_TIME_TICK * 1000;
2450             UINT64 now_us = time_now_us();
2451             if (last_frame_us != 0)
2452                 us_this_tick = (now_us - last_frame_us);
2453             last_frame_us = now_us;
2454 
2455             btif_media_cb.media_feeding_state.pcm.counter +=
2456                                 btif_media_cb.media_feeding_state.pcm.bytes_per_tick *
2457                                 us_this_tick / (BTIF_MEDIA_TIME_TICK * 1000);
2458 
2459             /* calculate nbr of frames pending for this media tick */
2460             result = btif_media_cb.media_feeding_state.pcm.counter/pcm_bytes_per_frame;
2461             if (result > MAX_PCM_FRAME_NUM_PER_TICK)
2462             {
2463                 APPL_TRACE_WARNING("%s() - Limiting frames to be sent from %d to %d"
2464                     , __FUNCTION__, result, MAX_PCM_FRAME_NUM_PER_TICK);
2465                 result = MAX_PCM_FRAME_NUM_PER_TICK;
2466             }
2467             btif_media_cb.media_feeding_state.pcm.counter -= result*pcm_bytes_per_frame;
2468 
2469             VERBOSE("WRITE %d FRAMES", result);
2470         }
2471         break;
2472 
2473         default:
2474             APPL_TRACE_ERROR("ERROR btif_get_num_aa_frame Unsupported transcoding format 0x%x",
2475                     btif_media_cb.TxTranscoding);
2476             result = 0;
2477             break;
2478     }
2479 
2480 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
2481     APPL_TRACE_DEBUG("btif_get_num_aa_frame returns %d", result);
2482 #endif
2483 
2484     return (UINT8)result;
2485 }
2486 
2487 /*******************************************************************************
2488  **
2489  ** Function         btif_media_sink_enque_buf
2490  **
2491  ** Description      This function is called by the av_co to fill A2DP Sink Queue
2492  **
2493  **
2494  ** Returns          size of the queue
2495  *******************************************************************************/
btif_media_sink_enque_buf(BT_HDR * p_pkt)2496 UINT8 btif_media_sink_enque_buf(BT_HDR *p_pkt)
2497 {
2498     tBT_SBC_HDR *p_msg;
2499 
2500     if(btif_media_cb.rx_flush == TRUE) /* Flush enabled, do not enque*/
2501         return btif_media_cb.RxSbcQ.count;
2502     if(btif_media_cb.RxSbcQ.count == MAX_OUTPUT_A2DP_FRAME_QUEUE_SZ)
2503     {
2504         GKI_freebuf(GKI_dequeue(&(btif_media_cb.RxSbcQ)));
2505     }
2506 
2507     BTIF_TRACE_VERBOSE("btif_media_sink_enque_buf + ");
2508     /* allocate and Queue this buffer */
2509     if ((p_msg = (tBT_SBC_HDR *) GKI_getbuf(sizeof(tBT_SBC_HDR) +
2510                         p_pkt->offset+ p_pkt->len)) != NULL)
2511     {
2512         memcpy(p_msg, p_pkt, (sizeof(BT_HDR) + p_pkt->offset + p_pkt->len));
2513         p_msg->num_frames_to_be_processed = (*((UINT8*)(p_msg + 1) + p_msg->offset)) & 0x0f;
2514         BTIF_TRACE_VERBOSE("btif_media_sink_enque_buf + ", p_msg->num_frames_to_be_processed);
2515         GKI_enqueue(&(btif_media_cb.RxSbcQ), p_msg);
2516         if(btif_media_cb.RxSbcQ.count == MAX_A2DP_DELAYED_START_FRAME_COUNT)
2517         {
2518             BTIF_TRACE_DEBUG(" Initiate Decoding ");
2519             btif_media_task_start_decoding_req();
2520         }
2521     }
2522     else
2523     {
2524         /* let caller deal with a failed allocation */
2525         BTIF_TRACE_VERBOSE("btif_media_sink_enque_buf No Buffer left - ");
2526     }
2527     return btif_media_cb.RxSbcQ.count;
2528 }
2529 
2530 /*******************************************************************************
2531  **
2532  ** Function         btif_media_aa_readbuf
2533  **
2534  ** Description      This function is called by the av_co to get the next buffer to send
2535  **
2536  **
2537  ** Returns          void
2538  *******************************************************************************/
btif_media_aa_readbuf(void)2539 BT_HDR *btif_media_aa_readbuf(void)
2540 {
2541     return GKI_dequeue(&(btif_media_cb.TxAaQ));
2542 }
2543 
2544 /*******************************************************************************
2545  **
2546  ** Function         btif_media_aa_read_feeding
2547  **
2548  ** Description
2549  **
2550  ** Returns          void
2551  **
2552  *******************************************************************************/
2553 
btif_media_aa_read_feeding(tUIPC_CH_ID channel_id)2554 BOOLEAN btif_media_aa_read_feeding(tUIPC_CH_ID channel_id)
2555 {
2556     UINT16 event;
2557     UINT16 blocm_x_subband = btif_media_cb.encoder.s16NumOfSubBands * \
2558                              btif_media_cb.encoder.s16NumOfBlocks;
2559     UINT32 read_size;
2560     UINT16 sbc_sampling = 48000;
2561     UINT32 src_samples;
2562     UINT16 bytes_needed = blocm_x_subband * btif_media_cb.encoder.s16NumOfChannels * \
2563                           btif_media_cb.media_feeding.cfg.pcm.bit_per_sample / 8;
2564     static UINT16 up_sampled_buffer[SBC_MAX_NUM_FRAME * SBC_MAX_NUM_OF_BLOCKS
2565             * SBC_MAX_NUM_OF_CHANNELS * SBC_MAX_NUM_OF_SUBBANDS * 2];
2566     static UINT16 read_buffer[SBC_MAX_NUM_FRAME * SBC_MAX_NUM_OF_BLOCKS
2567             * SBC_MAX_NUM_OF_CHANNELS * SBC_MAX_NUM_OF_SUBBANDS];
2568     UINT32 src_size_used;
2569     UINT32 dst_size_used;
2570     BOOLEAN fract_needed;
2571     INT32   fract_max;
2572     INT32   fract_threshold;
2573     UINT32  nb_byte_read;
2574 
2575     /* Get the SBC sampling rate */
2576     switch (btif_media_cb.encoder.s16SamplingFreq)
2577     {
2578     case SBC_sf48000:
2579         sbc_sampling = 48000;
2580         break;
2581     case SBC_sf44100:
2582         sbc_sampling = 44100;
2583         break;
2584     case SBC_sf32000:
2585         sbc_sampling = 32000;
2586         break;
2587     case SBC_sf16000:
2588         sbc_sampling = 16000;
2589         break;
2590     }
2591 
2592     if (sbc_sampling == btif_media_cb.media_feeding.cfg.pcm.sampling_freq) {
2593         read_size = bytes_needed - btif_media_cb.media_feeding_state.pcm.aa_feed_residue;
2594         nb_byte_read = UIPC_Read(channel_id, &event,
2595                   ((UINT8 *)btif_media_cb.encoder.as16PcmBuffer) +
2596                   btif_media_cb.media_feeding_state.pcm.aa_feed_residue,
2597                   read_size);
2598         if (nb_byte_read == read_size) {
2599             btif_media_cb.media_feeding_state.pcm.aa_feed_residue = 0;
2600             return TRUE;
2601         } else {
2602             APPL_TRACE_WARNING("### UNDERFLOW :: ONLY READ %d BYTES OUT OF %d ###",
2603                 nb_byte_read, read_size);
2604             btif_media_cb.media_feeding_state.pcm.aa_feed_residue += nb_byte_read;
2605             return FALSE;
2606         }
2607     }
2608 
2609     /* Some Feeding PCM frequencies require to split the number of sample */
2610     /* to read. */
2611     /* E.g 128/6=21.3333 => read 22 and 21 and 21 => max = 2; threshold = 0*/
2612     fract_needed = FALSE;   /* Default */
2613     switch (btif_media_cb.media_feeding.cfg.pcm.sampling_freq)
2614     {
2615     case 32000:
2616     case 8000:
2617         fract_needed = TRUE;
2618         fract_max = 2;          /* 0, 1 and 2 */
2619         fract_threshold = 0;    /* Add one for the first */
2620         break;
2621     case 16000:
2622         fract_needed = TRUE;
2623         fract_max = 2;          /* 0, 1 and 2 */
2624         fract_threshold = 1;    /* Add one for the first two frames*/
2625         break;
2626     }
2627 
2628     /* Compute number of sample to read from source */
2629     src_samples = blocm_x_subband;
2630     src_samples *= btif_media_cb.media_feeding.cfg.pcm.sampling_freq;
2631     src_samples /= sbc_sampling;
2632 
2633     /* The previous division may have a remainder not null */
2634     if (fract_needed)
2635     {
2636         if (btif_media_cb.media_feeding_state.pcm.aa_feed_counter <= fract_threshold)
2637         {
2638             src_samples++; /* for every read before threshold add one sample */
2639         }
2640 
2641         /* do nothing if counter >= threshold */
2642         btif_media_cb.media_feeding_state.pcm.aa_feed_counter++; /* one more read */
2643         if (btif_media_cb.media_feeding_state.pcm.aa_feed_counter > fract_max)
2644         {
2645             btif_media_cb.media_feeding_state.pcm.aa_feed_counter = 0;
2646         }
2647     }
2648 
2649     /* Compute number of bytes to read from source */
2650     read_size = src_samples;
2651     read_size *= btif_media_cb.media_feeding.cfg.pcm.num_channel;
2652     read_size *= (btif_media_cb.media_feeding.cfg.pcm.bit_per_sample / 8);
2653 
2654     /* Read Data from UIPC channel */
2655     nb_byte_read = UIPC_Read(channel_id, &event, (UINT8 *)read_buffer, read_size);
2656 
2657     //tput_mon(TRUE, nb_byte_read, FALSE);
2658 
2659     if (nb_byte_read < read_size)
2660     {
2661         APPL_TRACE_WARNING("### UNDERRUN :: ONLY READ %d BYTES OUT OF %d ###",
2662                 nb_byte_read, read_size);
2663 
2664         if (nb_byte_read == 0)
2665             return FALSE;
2666 
2667         if(btif_media_cb.feeding_mode == BTIF_AV_FEEDING_ASYNCHRONOUS)
2668         {
2669             /* Fill the unfilled part of the read buffer with silence (0) */
2670             memset(((UINT8 *)read_buffer) + nb_byte_read, 0, read_size - nb_byte_read);
2671             nb_byte_read = read_size;
2672         }
2673     }
2674 
2675     /* Initialize PCM up-sampling engine */
2676     bta_av_sbc_init_up_sample(btif_media_cb.media_feeding.cfg.pcm.sampling_freq,
2677             sbc_sampling, btif_media_cb.media_feeding.cfg.pcm.bit_per_sample,
2678             btif_media_cb.media_feeding.cfg.pcm.num_channel);
2679 
2680     /* re-sample read buffer */
2681     /* The output PCM buffer will be stereo, 16 bit per sample */
2682     dst_size_used = bta_av_sbc_up_sample((UINT8 *)read_buffer,
2683             (UINT8 *)up_sampled_buffer + btif_media_cb.media_feeding_state.pcm.aa_feed_residue,
2684             nb_byte_read,
2685             sizeof(up_sampled_buffer) - btif_media_cb.media_feeding_state.pcm.aa_feed_residue,
2686             &src_size_used);
2687 
2688 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
2689     APPL_TRACE_DEBUG("btif_media_aa_read_feeding readsz:%d src_size_used:%d dst_size_used:%d",
2690             read_size, src_size_used, dst_size_used);
2691 #endif
2692 
2693     /* update the residue */
2694     btif_media_cb.media_feeding_state.pcm.aa_feed_residue += dst_size_used;
2695 
2696     /* only copy the pcm sample when we have up-sampled enough PCM */
2697     if(btif_media_cb.media_feeding_state.pcm.aa_feed_residue >= bytes_needed)
2698     {
2699         /* Copy the output pcm samples in SBC encoding buffer */
2700         memcpy((UINT8 *)btif_media_cb.encoder.as16PcmBuffer,
2701                 (UINT8 *)up_sampled_buffer,
2702                 bytes_needed);
2703         /* update the residue */
2704         btif_media_cb.media_feeding_state.pcm.aa_feed_residue -= bytes_needed;
2705 
2706         if (btif_media_cb.media_feeding_state.pcm.aa_feed_residue != 0)
2707         {
2708             memcpy((UINT8 *)up_sampled_buffer,
2709                    (UINT8 *)up_sampled_buffer + bytes_needed,
2710                    btif_media_cb.media_feeding_state.pcm.aa_feed_residue);
2711         }
2712         return TRUE;
2713     }
2714 
2715 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
2716     APPL_TRACE_DEBUG("btif_media_aa_read_feeding residue:%d, dst_size_used %d, bytes_needed %d",
2717             btif_media_cb.media_feeding_state.pcm.aa_feed_residue, dst_size_used, bytes_needed);
2718 #endif
2719 
2720     return FALSE;
2721 }
2722 
2723 /*******************************************************************************
2724  **
2725  ** Function         btif_media_aa_prep_sbc_2_send
2726  **
2727  ** Description
2728  **
2729  ** Returns          void
2730  **
2731  *******************************************************************************/
btif_media_aa_prep_sbc_2_send(UINT8 nb_frame)2732 static void btif_media_aa_prep_sbc_2_send(UINT8 nb_frame)
2733 {
2734     BT_HDR * p_buf;
2735     UINT16 blocm_x_subband = btif_media_cb.encoder.s16NumOfSubBands *
2736                              btif_media_cb.encoder.s16NumOfBlocks;
2737 
2738 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
2739     APPL_TRACE_DEBUG("btif_media_aa_prep_sbc_2_send nb_frame %d, TxAaQ %d",
2740                        nb_frame, btif_media_cb.TxAaQ.count);
2741 #endif
2742     while (nb_frame)
2743     {
2744         if (NULL == (p_buf = GKI_getpoolbuf(BTIF_MEDIA_AA_POOL_ID)))
2745         {
2746             APPL_TRACE_ERROR ("ERROR btif_media_aa_prep_sbc_2_send no buffer TxCnt %d ",
2747                                 btif_media_cb.TxAaQ.count);
2748             return;
2749         }
2750 
2751         /* Init buffer */
2752         p_buf->offset = BTIF_MEDIA_AA_SBC_OFFSET;
2753         p_buf->len = 0;
2754         p_buf->layer_specific = 0;
2755 
2756         do
2757         {
2758             /* Write @ of allocated buffer in encoder.pu8Packet */
2759             btif_media_cb.encoder.pu8Packet = (UINT8 *) (p_buf + 1) + p_buf->offset + p_buf->len;
2760             /* Fill allocated buffer with 0 */
2761             memset(btif_media_cb.encoder.as16PcmBuffer, 0, blocm_x_subband
2762                     * btif_media_cb.encoder.s16NumOfChannels);
2763 
2764             /* Read PCM data and upsample them if needed */
2765             if (btif_media_aa_read_feeding(UIPC_CH_ID_AV_AUDIO))
2766             {
2767                 /* SBC encode and descramble frame */
2768                 SBC_Encoder(&(btif_media_cb.encoder));
2769                 A2D_SbcChkFrInit(btif_media_cb.encoder.pu8Packet);
2770                 A2D_SbcDescramble(btif_media_cb.encoder.pu8Packet, btif_media_cb.encoder.u16PacketLength);
2771                 /* Update SBC frame length */
2772                 p_buf->len += btif_media_cb.encoder.u16PacketLength;
2773                 nb_frame--;
2774                 p_buf->layer_specific++;
2775             }
2776             else
2777             {
2778                 APPL_TRACE_WARNING("btif_media_aa_prep_sbc_2_send underflow %d, %d",
2779                     nb_frame, btif_media_cb.media_feeding_state.pcm.aa_feed_residue);
2780                 btif_media_cb.media_feeding_state.pcm.counter += nb_frame *
2781                      btif_media_cb.encoder.s16NumOfSubBands *
2782                      btif_media_cb.encoder.s16NumOfBlocks *
2783                      btif_media_cb.media_feeding.cfg.pcm.num_channel *
2784                      btif_media_cb.media_feeding.cfg.pcm.bit_per_sample / 8;
2785                 /* no more pcm to read */
2786                 nb_frame = 0;
2787 
2788                 /* break read loop if timer was stopped (media task stopped) */
2789                 if ( btif_media_cb.is_tx_timer == FALSE )
2790                 {
2791                     GKI_freebuf(p_buf);
2792                     return;
2793                 }
2794             }
2795 
2796         } while (((p_buf->len + btif_media_cb.encoder.u16PacketLength) < btif_media_cb.TxAaMtuSize)
2797                 && (p_buf->layer_specific < 0x0F) && nb_frame);
2798 
2799         if(p_buf->len)
2800         {
2801             /* timestamp of the media packet header represent the TS of the first SBC frame
2802                i.e the timestamp before including this frame */
2803             *((UINT32 *) (p_buf + 1)) = btif_media_cb.timestamp;
2804 
2805             btif_media_cb.timestamp += p_buf->layer_specific * blocm_x_subband;
2806 
2807             VERBOSE("TX QUEUE NOW %d", btif_media_cb.TxAaQ.count);
2808 
2809             if (btif_media_cb.tx_flush)
2810             {
2811                 APPL_TRACE_DEBUG("### tx suspended, discarded frame ###");
2812 
2813                 if (btif_media_cb.TxAaQ.count > 0)
2814                     btif_media_flush_q(&(btif_media_cb.TxAaQ));
2815 
2816                 GKI_freebuf(p_buf);
2817                 return;
2818             }
2819 
2820             /* Enqueue the encoded SBC frame in AA Tx Queue */
2821             GKI_enqueue(&(btif_media_cb.TxAaQ), p_buf);
2822         }
2823         else
2824         {
2825             GKI_freebuf(p_buf);
2826         }
2827     }
2828 }
2829 
2830 
2831 /*******************************************************************************
2832  **
2833  ** Function         btif_media_aa_prep_2_send
2834  **
2835  ** Description
2836  **
2837  ** Returns          void
2838  **
2839  *******************************************************************************/
2840 
btif_media_aa_prep_2_send(UINT8 nb_frame)2841 static void btif_media_aa_prep_2_send(UINT8 nb_frame)
2842 {
2843     VERBOSE("%s() - frames=%d (queue=%d)", __FUNCTION__, nb_frame, btif_media_cb.TxAaQ.count);
2844 
2845     while (btif_media_cb.TxAaQ.count >= (MAX_OUTPUT_A2DP_FRAME_QUEUE_SZ-nb_frame))
2846     {
2847         APPL_TRACE_WARNING("%s() - TX queue buffer count %d",
2848             __FUNCTION__, btif_media_cb.TxAaQ.count);
2849         GKI_freebuf(GKI_dequeue(&(btif_media_cb.TxAaQ)));
2850     }
2851 
2852     if (btif_media_cb.TxAaQ.count) --nb_frame;
2853 
2854     switch (btif_media_cb.TxTranscoding)
2855     {
2856     case BTIF_MEDIA_TRSCD_PCM_2_SBC:
2857         btif_media_aa_prep_sbc_2_send(nb_frame);
2858         break;
2859 
2860 
2861     default:
2862         APPL_TRACE_ERROR("ERROR btif_media_aa_prep_2_send unsupported transcoding format 0x%x",btif_media_cb.TxTranscoding);
2863         break;
2864     }
2865 }
2866 
2867 /*******************************************************************************
2868  **
2869  ** Function         btif_media_send_aa_frame
2870  **
2871  ** Description
2872  **
2873  ** Returns          void
2874  **
2875  *******************************************************************************/
btif_media_send_aa_frame(void)2876 static void btif_media_send_aa_frame(void)
2877 {
2878     UINT8 nb_frame_2_send;
2879 
2880     /* get the number of frame to send */
2881     nb_frame_2_send = btif_get_num_aa_frame();
2882 
2883     if (nb_frame_2_send != 0)
2884     {
2885         /* format and Q buffer to send */
2886         btif_media_aa_prep_2_send(nb_frame_2_send);
2887     }
2888 
2889     /* send it */
2890     VERBOSE("btif_media_send_aa_frame : send %d frames", nb_frame_2_send);
2891     bta_av_ci_src_data_ready(BTA_AV_CHNL_AUDIO);
2892 }
2893 
2894 #endif /* BTA_AV_INCLUDED == TRUE */
2895 
2896 /*******************************************************************************
2897  **
2898  ** Function         dump_codec_info
2899  **
2900  ** Description      Decode and display codec_info (for debug)
2901  **
2902  ** Returns          void
2903  **
2904  *******************************************************************************/
dump_codec_info(unsigned char * p_codec)2905 void dump_codec_info(unsigned char *p_codec)
2906 {
2907     tA2D_STATUS a2d_status;
2908     tA2D_SBC_CIE sbc_cie;
2909 
2910     a2d_status = A2D_ParsSbcInfo(&sbc_cie, p_codec, FALSE);
2911     if (a2d_status != A2D_SUCCESS)
2912     {
2913         APPL_TRACE_ERROR("ERROR dump_codec_info A2D_ParsSbcInfo fail:%d", a2d_status);
2914         return;
2915     }
2916 
2917     APPL_TRACE_DEBUG("dump_codec_info");
2918 
2919     if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_16)
2920     {    APPL_TRACE_DEBUG("\tsamp_freq:%d (16000)", sbc_cie.samp_freq);}
2921     else  if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_32)
2922     {    APPL_TRACE_DEBUG("\tsamp_freq:%d (32000)", sbc_cie.samp_freq);}
2923     else  if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_44)
2924     {    APPL_TRACE_DEBUG("\tsamp_freq:%d (44.100)", sbc_cie.samp_freq);}
2925     else  if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_48)
2926     {    APPL_TRACE_DEBUG("\tsamp_freq:%d (48000)", sbc_cie.samp_freq);}
2927     else
2928     {    APPL_TRACE_DEBUG("\tBAD samp_freq:%d", sbc_cie.samp_freq);}
2929 
2930     if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_MONO)
2931     {    APPL_TRACE_DEBUG("\tch_mode:%d (Mono)", sbc_cie.ch_mode);}
2932     else  if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_DUAL)
2933     {    APPL_TRACE_DEBUG("\tch_mode:%d (Dual)", sbc_cie.ch_mode);}
2934     else  if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_STEREO)
2935     {    APPL_TRACE_DEBUG("\tch_mode:%d (Stereo)", sbc_cie.ch_mode);}
2936     else  if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_JOINT)
2937     {    APPL_TRACE_DEBUG("\tch_mode:%d (Joint)", sbc_cie.ch_mode);}
2938     else
2939     {    APPL_TRACE_DEBUG("\tBAD ch_mode:%d", sbc_cie.ch_mode);}
2940 
2941     if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_4)
2942     {    APPL_TRACE_DEBUG("\tblock_len:%d (4)", sbc_cie.block_len);}
2943     else  if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_8)
2944     {    APPL_TRACE_DEBUG("\tblock_len:%d (8)", sbc_cie.block_len);}
2945     else  if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_12)
2946     {    APPL_TRACE_DEBUG("\tblock_len:%d (12)", sbc_cie.block_len);}
2947     else  if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_16)
2948     {    APPL_TRACE_DEBUG("\tblock_len:%d (16)", sbc_cie.block_len);}
2949     else
2950     {    APPL_TRACE_DEBUG("\tBAD block_len:%d", sbc_cie.block_len);}
2951 
2952     if (sbc_cie.num_subbands == A2D_SBC_IE_SUBBAND_4)
2953     {    APPL_TRACE_DEBUG("\tnum_subbands:%d (4)", sbc_cie.num_subbands);}
2954     else  if (sbc_cie.num_subbands == A2D_SBC_IE_SUBBAND_8)
2955     {    APPL_TRACE_DEBUG("\tnum_subbands:%d (8)", sbc_cie.num_subbands);}
2956     else
2957     {    APPL_TRACE_DEBUG("\tBAD num_subbands:%d", sbc_cie.num_subbands);}
2958 
2959     if (sbc_cie.alloc_mthd == A2D_SBC_IE_ALLOC_MD_S)
2960     {    APPL_TRACE_DEBUG("\talloc_mthd:%d (SNR)", sbc_cie.alloc_mthd);}
2961     else  if (sbc_cie.alloc_mthd == A2D_SBC_IE_ALLOC_MD_L)
2962     {    APPL_TRACE_DEBUG("\talloc_mthd:%d (Loundess)", sbc_cie.alloc_mthd);}
2963     else
2964     {    APPL_TRACE_DEBUG("\tBAD alloc_mthd:%d", sbc_cie.alloc_mthd);}
2965 
2966     APPL_TRACE_DEBUG("\tBit pool Min:%d Max:%d", sbc_cie.min_bitpool, sbc_cie.max_bitpool);
2967 
2968 }
2969 
2970