Home | History | Annotate | Download | only in src
      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 #include "bt_utils.h"
     70 
     71 /*****************************************************************************
     72  **  Constants
     73  *****************************************************************************/
     74 
     75 //#define DEBUG_MEDIA_AV_FLOW TRUE
     76 
     77 /* BTIF media task gki event definition */
     78 #define BTIF_MEDIA_TASK_CMD TASK_MBOX_0_EVT_MASK
     79 #define BTIF_MEDIA_TASK_DATA TASK_MBOX_1_EVT_MASK
     80 
     81 #define BTIF_MEDIA_TASK_KILL EVENT_MASK(GKI_SHUTDOWN_EVT)
     82 
     83 #define BTIF_MEDIA_AA_TASK_TIMER_ID TIMER_0
     84 #define BTIF_MEDIA_AV_TASK_TIMER_ID TIMER_1
     85 #define BTIF_MEDIA_AA_TASK_TIMER TIMER_0_EVT_MASK
     86 #define BTIF_MEDIA_AV_TASK_TIMER TIMER_1_EVT_MASK
     87 
     88 #define BTIF_MEDIA_TASK_CMD_MBOX        TASK_MBOX_0     /* cmd mailbox  */
     89 #define BTIF_MEDIA_TASK_DATA_MBOX       TASK_MBOX_1     /* data mailbox  */
     90 
     91 /* BTIF media cmd event definition : BTIF_MEDIA_TASK_CMD */
     92 enum
     93 {
     94     BTIF_MEDIA_START_AA_TX = 1,
     95     BTIF_MEDIA_STOP_AA_TX,
     96     BTIF_MEDIA_AA_RX_RDY,
     97     BTIF_MEDIA_UIPC_RX_RDY,
     98     BTIF_MEDIA_SBC_ENC_INIT,
     99     BTIF_MEDIA_SBC_ENC_UPDATE,
    100     BTIF_MEDIA_SBC_DEC_INIT,
    101     BTIF_MEDIA_VIDEO_DEC_INIT,
    102     BTIF_MEDIA_FLUSH_AA_TX,
    103     BTIF_MEDIA_FLUSH_AA_RX,
    104     BTIF_MEDIA_AUDIO_FEEDING_INIT,
    105     BTIF_MEDIA_AUDIO_RECEIVING_INIT
    106 };
    107 
    108 enum {
    109     MEDIA_TASK_STATE_OFF = 0,
    110     MEDIA_TASK_STATE_ON = 1,
    111     MEDIA_TASK_STATE_SHUTTING_DOWN = 2
    112 };
    113 
    114 /* Macro to multiply the media task tick */
    115 #ifndef BTIF_MEDIA_NUM_TICK
    116 #define BTIF_MEDIA_NUM_TICK      1
    117 #endif
    118 
    119 /* Media task tick in milliseconds */
    120 #define BTIF_MEDIA_TIME_TICK                     (20 * BTIF_MEDIA_NUM_TICK)
    121 
    122 /* Number of frames per media task tick.
    123    Configure value rounded up to closest integer and
    124    adjust any deltas in btif_get_num_aa_frame */
    125 
    126 /* 7.5 frames/tick @ 20 ms tick (every 2nd frame send one less) */
    127 #define BTIF_MEDIA_FR_PER_TICKS_48               (8 * BTIF_MEDIA_NUM_TICK)
    128 
    129 /* 6.89 frames/tick  @ 20 ms tick (7 out of 64 frames send one less */
    130 #define BTIF_MEDIA_FR_PER_TICKS_44_1             (7 * BTIF_MEDIA_NUM_TICK)
    131 
    132 /* 5.0 frames/tick  @ 20 ms tick */
    133 #define BTIF_MEDIA_FR_PER_TICKS_32               (5 * BTIF_MEDIA_NUM_TICK)
    134 
    135 /* 2.5 frames/tick  @ 20 ms tick (every 2nd frame send one less) */
    136 #define BTIF_MEDIA_FR_PER_TICKS_16               (3 * BTIF_MEDIA_NUM_TICK)
    137 
    138 
    139 /* buffer pool */
    140 #define BTIF_MEDIA_AA_POOL_ID GKI_POOL_ID_3
    141 #define BTIF_MEDIA_AA_BUF_SIZE GKI_BUF3_SIZE
    142 
    143 /* offset */
    144 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
    145 #define BTIF_MEDIA_AA_SBC_OFFSET (AVDT_MEDIA_OFFSET + BTA_AV_SBC_HDR_SIZE + 1)
    146 #else
    147 #define BTIF_MEDIA_AA_SBC_OFFSET (AVDT_MEDIA_OFFSET + BTA_AV_SBC_HDR_SIZE)
    148 #endif
    149 
    150 /* Define the bitrate step when trying to match bitpool value */
    151 #ifndef BTIF_MEDIA_BITRATE_STEP
    152 #define BTIF_MEDIA_BITRATE_STEP 5
    153 #endif
    154 
    155 /* Middle quality quality setting @ 44.1 khz */
    156 #define DEFAULT_SBC_BITRATE 229
    157 
    158 #ifndef A2DP_MEDIA_TASK_STACK_SIZE
    159 #define A2DP_MEDIA_TASK_STACK_SIZE       0x2000         /* In bytes */
    160 #endif
    161 
    162 #define A2DP_MEDIA_TASK_TASK_STR        ((INT8 *) "A2DP-MEDIA")
    163 static UINT32 a2dp_media_task_stack[(A2DP_MEDIA_TASK_STACK_SIZE + 3) / 4];
    164 
    165 #define BT_MEDIA_TASK A2DP_MEDIA_TASK
    166 
    167 #define USEC_PER_SEC 1000000L
    168 #define TPUT_STATS_INTERVAL_US (3000*1000)
    169 
    170 /*
    171  * CONGESTION COMPENSATION CTRL ::
    172  *
    173  * Thus setting controls how many buffers we will hold in media task
    174  * during temp link congestion. Together with the stack buffer queues
    175  * it controls much temporary a2dp link congestion we can
    176  * compensate for. It however also depends on the default run level of sinks
    177  * jitterbuffers. Depending on type of sink this would vary.
    178  * Ideally the (SRC) max tx buffer capacity should equal the sinks
    179  * jitterbuffer runlevel including any intermediate buffers on the way
    180  * towards the sinks codec.
    181  */
    182 
    183 /* fixme -- define this in pcm time instead of buffer count */
    184 /* fixme -- tune optimal value. For now set a large buffer capacity */
    185 #define MAX_OUTPUT_BUFFER_QUEUE_SZ 24
    186 
    187 //#define BTIF_MEDIA_VERBOSE_ENABLED
    188 
    189 #ifdef BTIF_MEDIA_VERBOSE_ENABLED
    190 #define VERBOSE(fmt, ...) \
    191       LogMsg( TRACE_CTRL_GENERAL | TRACE_LAYER_NONE | TRACE_ORG_APPL | \
    192               TRACE_TYPE_ERROR, fmt, ## __VA_ARGS__)
    193 #else
    194 #define VERBOSE(fmt, ...)
    195 #endif
    196 
    197 /*****************************************************************************
    198  **  Data types
    199  *****************************************************************************/
    200 
    201 typedef struct
    202 {
    203     UINT32 aa_frame_counter;
    204     INT32  aa_feed_counter;
    205     INT32  aa_feed_residue;
    206 } tBTIF_AV_MEDIA_FEEDINGS_PCM_STATE;
    207 
    208 
    209 typedef union
    210 {
    211     tBTIF_AV_MEDIA_FEEDINGS_PCM_STATE pcm;
    212 } tBTIF_AV_MEDIA_FEEDINGS_STATE;
    213 
    214 typedef struct
    215 {
    216 #if (BTA_AV_INCLUDED == TRUE)
    217     BUFFER_Q TxAaQ;
    218     BOOLEAN is_tx_timer;
    219     UINT16 TxAaMtuSize;
    220     UINT32 timestamp;
    221     UINT8 TxTranscoding;
    222     tBTIF_AV_FEEDING_MODE feeding_mode;
    223     tBTIF_AV_MEDIA_FEEDINGS media_feeding;
    224     tBTIF_AV_MEDIA_FEEDINGS_STATE media_feeding_state;
    225     SBC_ENC_PARAMS encoder;
    226     UINT8 busy_level;
    227     void* av_sm_hdl;
    228     UINT8 a2dp_cmd_pending; /* we can have max one command pending */
    229     BOOLEAN tx_flush; /* discards any outgoing data when true */
    230     BOOLEAN scaling_disabled;
    231 #endif
    232 
    233 } tBTIF_MEDIA_CB;
    234 
    235 typedef struct {
    236     long long rx;
    237     long long rx_tot;
    238     long long tx;
    239     long long tx_tot;
    240     long long ts_prev_us;
    241 } t_stat;
    242 
    243 /*****************************************************************************
    244  **  Local data
    245  *****************************************************************************/
    246 
    247 static tBTIF_MEDIA_CB btif_media_cb;
    248 static int media_task_running = MEDIA_TASK_STATE_OFF;
    249 
    250 
    251 /*****************************************************************************
    252  **  Local functions
    253  *****************************************************************************/
    254 
    255 static void btif_a2dp_data_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event);
    256 static void btif_a2dp_ctrl_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event);
    257 static void btif_a2dp_encoder_update(void);
    258 const char* dump_media_event(UINT16 event);
    259 
    260 /*****************************************************************************
    261  **  Externs
    262  *****************************************************************************/
    263 
    264 static void btif_media_task_handle_cmd(BT_HDR *p_msg);
    265 static void btif_media_task_handle_media(BT_HDR *p_msg);
    266 
    267 #if (BTA_AV_INCLUDED == TRUE)
    268 static void btif_media_send_aa_frame(void);
    269 static void btif_media_task_feeding_state_reset(void);
    270 static void btif_media_task_aa_start_tx(void);
    271 static void btif_media_task_aa_stop_tx(void);
    272 static void btif_media_task_enc_init(BT_HDR *p_msg);
    273 static void btif_media_task_enc_update(BT_HDR *p_msg);
    274 static void btif_media_task_audio_feeding_init(BT_HDR *p_msg);
    275 static void btif_media_task_aa_tx_flush(BT_HDR *p_msg);
    276 static void btif_media_aa_prep_2_send(UINT8 nb_frame);
    277 #endif
    278 
    279 
    280 /*****************************************************************************
    281  **  Misc helper functions
    282  *****************************************************************************/
    283 
    284 static void tput_mon(int is_rx, int len, int reset)
    285 {
    286     /* only monitor one connection at a time for now */
    287     static t_stat cur_stat;
    288     struct timespec now;
    289     unsigned long long prev_us;
    290     unsigned long long now_us;
    291 
    292     if (reset == TRUE)
    293     {
    294         memset(&cur_stat, 0, sizeof(t_stat));
    295         return;
    296     }
    297 
    298     if (is_rx)
    299     {
    300         cur_stat.rx+=len;
    301         cur_stat.rx_tot+=len;
    302     }
    303     else
    304     {
    305         cur_stat.tx+=len;
    306         cur_stat.tx_tot+=len;
    307     }
    308     clock_gettime(CLOCK_MONOTONIC, &now);
    309 
    310     now_us = now.tv_sec*USEC_PER_SEC + now.tv_nsec/1000;
    311 
    312     if ((now_us - cur_stat.ts_prev_us) < TPUT_STATS_INTERVAL_US)
    313         return;
    314 
    315     APPL_TRACE_WARNING4("tput rx:%d, tx:%d (bytes/s)  (tot : rx %d, tx %d bytes)",
    316           (cur_stat.rx*1000000)/((now_us - cur_stat.ts_prev_us)),
    317           (cur_stat.tx*1000000)/((now_us - cur_stat.ts_prev_us)),
    318            cur_stat.rx_tot, cur_stat.tx_tot);
    319 
    320     /* stats dumped. now reset stats for next interval */
    321     cur_stat.rx = 0;
    322     cur_stat.tx = 0;
    323     cur_stat.ts_prev_us = now_us;
    324 }
    325 
    326 
    327 static void log_tstamps_us(char *comment)
    328 {
    329     #define USEC_PER_SEC 1000000L
    330     static struct timespec prev = {0, 0};
    331     struct timespec now, diff;
    332     unsigned int diff_us = 0;
    333     unsigned int now_us = 0;
    334 
    335     clock_gettime(CLOCK_MONOTONIC, &now);
    336     now_us = now.tv_sec*USEC_PER_SEC + now.tv_nsec/1000;
    337     diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + (now.tv_nsec - prev.tv_nsec)/1000;
    338 
    339     APPL_TRACE_DEBUG4("[%s] ts %08d, diff : %08d, queue sz %d", comment, now_us, diff_us,
    340                 btif_media_cb.TxAaQ.count);
    341 
    342     prev = now;
    343 }
    344 
    345 const char* dump_media_event(UINT16 event)
    346 {
    347     switch(event)
    348     {
    349         CASE_RETURN_STR(BTIF_MEDIA_START_AA_TX)
    350         CASE_RETURN_STR(BTIF_MEDIA_STOP_AA_TX)
    351         CASE_RETURN_STR(BTIF_MEDIA_AA_RX_RDY)
    352         CASE_RETURN_STR(BTIF_MEDIA_UIPC_RX_RDY)
    353         CASE_RETURN_STR(BTIF_MEDIA_SBC_ENC_INIT)
    354         CASE_RETURN_STR(BTIF_MEDIA_SBC_ENC_UPDATE)
    355         CASE_RETURN_STR(BTIF_MEDIA_SBC_DEC_INIT)
    356         CASE_RETURN_STR(BTIF_MEDIA_VIDEO_DEC_INIT)
    357         CASE_RETURN_STR(BTIF_MEDIA_FLUSH_AA_TX)
    358         CASE_RETURN_STR(BTIF_MEDIA_FLUSH_AA_RX)
    359         CASE_RETURN_STR(BTIF_MEDIA_AUDIO_FEEDING_INIT)
    360         CASE_RETURN_STR(BTIF_MEDIA_AUDIO_RECEIVING_INIT)
    361 
    362         default:
    363             return "UNKNOWN MEDIA EVENT";
    364     }
    365 }
    366 
    367 /*****************************************************************************
    368  **  A2DP CTRL PATH
    369  *****************************************************************************/
    370 
    371 static const char* dump_a2dp_ctrl_event(UINT8 event)
    372 {
    373     switch(event)
    374     {
    375         CASE_RETURN_STR(A2DP_CTRL_CMD_NONE)
    376         CASE_RETURN_STR(A2DP_CTRL_CMD_CHECK_READY)
    377         CASE_RETURN_STR(A2DP_CTRL_CMD_START)
    378         CASE_RETURN_STR(A2DP_CTRL_CMD_STOP)
    379         CASE_RETURN_STR(A2DP_CTRL_CMD_SUSPEND)
    380         default:
    381             return "UNKNOWN MSG ID";
    382     }
    383 }
    384 
    385 static void btif_audiopath_detached(void)
    386 {
    387     APPL_TRACE_EVENT0("## AUDIO PATH DETACHED ##");
    388 
    389     /*  send stop request only if we are actively streaming and haven't received
    390         a stop request. Potentially audioflinger detached abnormally */
    391     if (btif_media_cb.is_tx_timer)
    392     {
    393         /* post stop event and wait for audio path to stop */
    394         btif_dispatch_sm_event(BTIF_AV_STOP_STREAM_REQ_EVT, NULL, 0);
    395     }
    396 }
    397 
    398 static void a2dp_cmd_acknowledge(int status)
    399 {
    400     UINT8 ack = status;
    401 
    402     APPL_TRACE_EVENT2("## a2dp ack : %s, status %d ##",
    403           dump_a2dp_ctrl_event(btif_media_cb.a2dp_cmd_pending), status);
    404 
    405     /* sanity check */
    406     if (btif_media_cb.a2dp_cmd_pending == A2DP_CTRL_CMD_NONE)
    407     {
    408         APPL_TRACE_ERROR0("warning : no command pending, ignore ack");
    409         return;
    410     }
    411 
    412     /* clear pending */
    413     btif_media_cb.a2dp_cmd_pending = A2DP_CTRL_CMD_NONE;
    414 
    415     /* acknowledge start request */
    416     UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, &ack, 1);
    417 }
    418 
    419 
    420 static void btif_recv_ctrl_data(void)
    421 {
    422     UINT8 cmd = 0;
    423     int n;
    424 
    425     n = UIPC_Read(UIPC_CH_ID_AV_CTRL, NULL, &cmd, 1);
    426 
    427     /* detach on ctrl channel means audioflinger process was terminated */
    428     if (n == 0)
    429     {
    430         APPL_TRACE_EVENT0("CTRL CH DETACHED");
    431         UIPC_Close(UIPC_CH_ID_AV_CTRL);
    432         /* we can operate only on datachannel, if af client wants to
    433            do send additional commands the ctrl channel would be reestablished */
    434         //btif_audiopath_detached();
    435         return;
    436     }
    437 
    438     APPL_TRACE_DEBUG1("a2dp-ctrl-cmd : %s", dump_a2dp_ctrl_event(cmd));
    439 
    440     btif_media_cb.a2dp_cmd_pending = cmd;
    441 
    442     switch(cmd)
    443     {
    444         case A2DP_CTRL_CMD_CHECK_READY:
    445 
    446             if (media_task_running == MEDIA_TASK_STATE_SHUTTING_DOWN)
    447             {
    448                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
    449                 return;
    450             }
    451 
    452             /* check whether av is ready to setup a2dp datapath */
    453             if ((btif_av_stream_ready() == TRUE) || (btif_av_stream_started_ready() == TRUE))
    454             {
    455                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
    456             }
    457             else
    458             {
    459                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
    460             }
    461             break;
    462 
    463         case A2DP_CTRL_CMD_START:
    464 
    465             if (btif_av_stream_ready() == TRUE)
    466             {
    467                 /* setup audio data channel listener */
    468                 UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
    469 
    470                 /* post start event and wait for audio path to open */
    471                 btif_dispatch_sm_event(BTIF_AV_START_STREAM_REQ_EVT, NULL, 0);
    472             }
    473             else if (btif_av_stream_started_ready())
    474             {
    475                 /* already started, setup audio data channel listener
    476                    and ack back immediately */
    477                 UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
    478 
    479                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
    480             }
    481             else
    482             {
    483                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
    484                 break;
    485             }
    486             break;
    487 
    488         case A2DP_CTRL_CMD_STOP:
    489 
    490             if (btif_media_cb.is_tx_timer == FALSE)
    491             {
    492                 /* we are already stopped, just ack back */
    493                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
    494                 break;
    495             }
    496 
    497             btif_dispatch_sm_event(BTIF_AV_STOP_STREAM_REQ_EVT, NULL, 0);
    498             break;
    499 
    500         case A2DP_CTRL_CMD_SUSPEND:
    501             /* local suspend */
    502             if (btif_av_stream_started_ready())
    503             {
    504                 btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
    505             }
    506             else
    507             {
    508                 /* if we are not in started state, just ack back ok and let
    509                    audioflinger close the channel. This can happen if we are
    510                    remotely suspended */
    511                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
    512             }
    513             break;
    514 
    515         default:
    516             APPL_TRACE_ERROR1("UNSUPPORTED CMD (%d)", cmd);
    517             a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
    518             break;
    519     }
    520     APPL_TRACE_DEBUG1("a2dp-ctrl-cmd : %s DONE", dump_a2dp_ctrl_event(cmd));
    521 }
    522 
    523 static void btif_a2dp_ctrl_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event)
    524 {
    525     APPL_TRACE_DEBUG1("A2DP-CTRL-CHANNEL EVENT %s", dump_uipc_event(event));
    526 
    527     switch(event)
    528     {
    529         case UIPC_OPEN_EVT:
    530             /* fetch av statemachine handle */
    531             btif_media_cb.av_sm_hdl = btif_av_get_sm_handle();
    532             break;
    533 
    534         case UIPC_CLOSE_EVT:
    535             /* restart ctrl server unless we are shutting down */
    536             if (media_task_running == MEDIA_TASK_STATE_ON)
    537                 UIPC_Open(UIPC_CH_ID_AV_CTRL , btif_a2dp_ctrl_cb);
    538             break;
    539 
    540         case UIPC_RX_DATA_READY_EVT:
    541             btif_recv_ctrl_data();
    542             break;
    543 
    544         default :
    545             APPL_TRACE_ERROR1("### A2DP-CTRL-CHANNEL EVENT %d NOT HANDLED ###", event);
    546             break;
    547     }
    548 }
    549 
    550 static void btif_a2dp_data_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event)
    551 {
    552     APPL_TRACE_DEBUG1("BTIF MEDIA (A2DP-DATA) EVENT %s", dump_uipc_event(event));
    553 
    554     switch(event)
    555     {
    556         case UIPC_OPEN_EVT:
    557 
    558             /*  read directly from media task from here on (keep callback for
    559                 connection events */
    560             UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_REG_REMOVE_ACTIVE_READSET, NULL);
    561 
    562             /* Start the media task to encode SBC */
    563             btif_media_task_start_aa_req();
    564 
    565             /* make sure we update any changed sbc encoder params */
    566             btif_a2dp_encoder_update();
    567 
    568             /* ack back when media task is fully started */
    569             break;
    570 
    571         case UIPC_CLOSE_EVT:
    572             a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
    573             btif_audiopath_detached();
    574             break;
    575 
    576         default :
    577             APPL_TRACE_ERROR1("### A2DP-DATA EVENT %d NOT HANDLED ###", event);
    578             break;
    579     }
    580 }
    581 
    582 
    583 /*****************************************************************************
    584  **  BTIF ADAPTATION
    585  *****************************************************************************/
    586 
    587 static void btif_a2dp_encoder_init(void)
    588 {
    589     UINT16 minmtu;
    590     tBTIF_MEDIA_INIT_AUDIO msg;
    591     tA2D_SBC_CIE sbc_config;
    592 
    593     /* lookup table for converting channel mode */
    594     UINT16 codec_mode_tbl[5] = { SBC_JOINT_STEREO, SBC_STEREO, SBC_DUAL, 0, SBC_MONO };
    595 
    596     /* lookup table for converting number of blocks */
    597     UINT16 codec_block_tbl[5] = { 16, 12, 8, 0, 4 };
    598 
    599     /* lookup table to convert freq */
    600     UINT16 freq_block_tbl[5] = { SBC_sf48000, SBC_sf44100, SBC_sf32000, 0, SBC_sf16000 };
    601 
    602     APPL_TRACE_DEBUG0("btif_a2dp_encoder_init");
    603 
    604     /* Retrieve the current SBC configuration (default if currently not used) */
    605     bta_av_co_audio_get_sbc_config(&sbc_config, &minmtu);
    606     msg.NumOfSubBands = (sbc_config.num_subbands == A2D_SBC_IE_SUBBAND_4) ? 4 : 8;
    607     msg.NumOfBlocks = codec_block_tbl[sbc_config.block_len >> 5];
    608     msg.AllocationMethod = (sbc_config.alloc_mthd == A2D_SBC_IE_ALLOC_MD_L) ? SBC_LOUDNESS : SBC_SNR;
    609     msg.ChannelMode = codec_mode_tbl[sbc_config.ch_mode >> 1];
    610     msg.SamplingFreq = freq_block_tbl[sbc_config.samp_freq >> 5];
    611     msg.MtuSize = minmtu;
    612 
    613     APPL_TRACE_EVENT1("msg.ChannelMode %x", msg.ChannelMode);
    614 
    615     /* Init the media task to encode SBC properly */
    616     btif_media_task_enc_init_req(&msg);
    617 }
    618 
    619 static void btif_a2dp_encoder_update(void)
    620 {
    621     UINT16 minmtu;
    622     tA2D_SBC_CIE sbc_config;
    623     tBTIF_MEDIA_UPDATE_AUDIO msg;
    624     UINT8 pref_min;
    625     UINT8 pref_max;
    626 
    627     APPL_TRACE_DEBUG0("btif_a2dp_encoder_update");
    628 
    629     /* Retrieve the current SBC configuration (default if currently not used) */
    630     bta_av_co_audio_get_sbc_config(&sbc_config, &minmtu);
    631 
    632     APPL_TRACE_DEBUG4("btif_a2dp_encoder_update: Common min_bitpool:%d(0x%x) max_bitpool:%d(0x%x)",
    633             sbc_config.min_bitpool, sbc_config.min_bitpool,
    634             sbc_config.max_bitpool, sbc_config.max_bitpool);
    635 
    636     if (sbc_config.min_bitpool > sbc_config.max_bitpool)
    637     {
    638         APPL_TRACE_ERROR0("btif_a2dp_encoder_update: ERROR btif_a2dp_encoder_update min_bitpool > max_bitpool");
    639     }
    640 
    641     /* check if remote sink has a preferred bitpool range */
    642     if (bta_av_co_get_remote_bitpool_pref(&pref_min, &pref_max) == TRUE)
    643     {
    644         /* adjust our preferred bitpool with the remote preference if within
    645            our capable range */
    646 
    647         if (pref_min < sbc_config.min_bitpool)
    648             pref_min = sbc_config.min_bitpool;
    649 
    650         if (pref_max > sbc_config.max_bitpool)
    651             pref_max = sbc_config.max_bitpool;
    652 
    653         msg.MinBitPool = pref_min;
    654         msg.MaxBitPool = pref_max;
    655 
    656         if ((pref_min != sbc_config.min_bitpool) || (pref_max != sbc_config.max_bitpool))
    657         {
    658             APPL_TRACE_EVENT2("## adjusted our bitpool range to peer pref [%d:%d] ##",
    659                 pref_min, pref_max);
    660         }
    661     }
    662     else
    663     {
    664         msg.MinBitPool = sbc_config.min_bitpool;
    665         msg.MaxBitPool = sbc_config.max_bitpool;
    666     }
    667 
    668     msg.MinMtuSize = minmtu;
    669 
    670     /* Update the media task to encode SBC properly */
    671     btif_media_task_enc_update_req(&msg);
    672 }
    673 
    674 
    675 /*****************************************************************************
    676 **
    677 ** Function        btif_a2dp_start_media_task
    678 **
    679 ** Description
    680 **
    681 ** Returns
    682 **
    683 *******************************************************************************/
    684 
    685 int btif_a2dp_start_media_task(void)
    686 {
    687     int retval;
    688 
    689     if (media_task_running != MEDIA_TASK_STATE_OFF)
    690     {
    691         APPL_TRACE_ERROR0("warning : media task already running");
    692         return GKI_FAILURE;
    693     }
    694 
    695     APPL_TRACE_EVENT0("## A2DP START MEDIA TASK ##");
    696 
    697     /* start a2dp media task */
    698     retval = GKI_create_task((TASKPTR)btif_media_task, A2DP_MEDIA_TASK,
    699                 A2DP_MEDIA_TASK_TASK_STR,
    700                 (UINT16 *) ((UINT8 *)a2dp_media_task_stack + A2DP_MEDIA_TASK_STACK_SIZE),
    701                 sizeof(a2dp_media_task_stack));
    702 
    703     if (retval != GKI_SUCCESS)
    704         return retval;
    705 
    706     /* wait for task to come up to sure we are able to send messages to it */
    707     while (media_task_running == MEDIA_TASK_STATE_OFF)
    708         usleep(10);
    709 
    710     APPL_TRACE_EVENT0("## A2DP MEDIA TASK STARTED ##");
    711 
    712     return retval;
    713 }
    714 
    715 /*****************************************************************************
    716 **
    717 ** Function        btif_a2dp_stop_media_task
    718 **
    719 ** Description
    720 **
    721 ** Returns
    722 **
    723 *******************************************************************************/
    724 
    725 void btif_a2dp_stop_media_task(void)
    726 {
    727     APPL_TRACE_EVENT0("## A2DP STOP MEDIA TASK ##");
    728     GKI_destroy_task(BT_MEDIA_TASK);
    729 }
    730 
    731 /*****************************************************************************
    732 **
    733 ** Function        btif_a2dp_on_init
    734 **
    735 ** Description
    736 **
    737 ** Returns
    738 **
    739 *******************************************************************************/
    740 
    741 void btif_a2dp_on_init(void)
    742 {
    743     //tput_mon(1, 0, 1);
    744 }
    745 
    746 
    747 /*****************************************************************************
    748 **
    749 ** Function        btif_a2dp_setup_codec
    750 **
    751 ** Description
    752 **
    753 ** Returns
    754 **
    755 *******************************************************************************/
    756 
    757 void btif_a2dp_setup_codec(void)
    758 {
    759     tBTIF_AV_MEDIA_FEEDINGS media_feeding;
    760     tBTIF_STATUS status;
    761 
    762     APPL_TRACE_EVENT0("## A2DP SETUP CODEC ##");
    763 
    764     GKI_disable();
    765 
    766     /* for now hardcode 44.1 khz 16 bit stereo */
    767     media_feeding.cfg.pcm.sampling_freq = 44100;
    768     media_feeding.cfg.pcm.bit_per_sample = 16;
    769     media_feeding.cfg.pcm.num_channel = 2;
    770     media_feeding.format = BTIF_AV_CODEC_PCM;
    771 
    772     if (bta_av_co_audio_set_codec(&media_feeding, &status))
    773     {
    774         tBTIF_MEDIA_INIT_AUDIO_FEEDING mfeed;
    775 
    776         /* Init the encoding task */
    777         btif_a2dp_encoder_init();
    778 
    779         /* Build the media task configuration */
    780         mfeed.feeding = media_feeding;
    781         mfeed.feeding_mode = BTIF_AV_FEEDING_ASYNCHRONOUS;
    782         /* Send message to Media task to configure transcoding */
    783         btif_media_task_audio_feeding_init_req(&mfeed);
    784     }
    785 
    786     GKI_enable();
    787 }
    788 
    789 
    790 /*****************************************************************************
    791 **
    792 ** Function        btif_a2dp_on_idle
    793 **
    794 ** Description
    795 **
    796 ** Returns
    797 **
    798 *******************************************************************************/
    799 
    800 void btif_a2dp_on_idle(void)
    801 {
    802     APPL_TRACE_EVENT0("## ON A2DP IDLE ##");
    803 
    804     /* Make sure media task is stopped */
    805     btif_media_task_stop_aa_req();
    806 
    807     bta_av_co_init();
    808 }
    809 
    810 /*****************************************************************************
    811 **
    812 ** Function        btif_a2dp_on_open
    813 **
    814 ** Description
    815 **
    816 ** Returns
    817 **
    818 *******************************************************************************/
    819 
    820 void btif_a2dp_on_open(void)
    821 {
    822     APPL_TRACE_EVENT0("## ON A2DP OPEN ##");
    823 
    824     /* always use callback to notify socket events */
    825     UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
    826 }
    827 
    828 /*****************************************************************************
    829 **
    830 ** Function        btif_a2dp_on_started
    831 **
    832 ** Description
    833 **
    834 ** Returns
    835 **
    836 *******************************************************************************/
    837 
    838 void btif_a2dp_on_started(tBTA_AV_START *p_av)
    839 {
    840     tBTIF_STATUS status;
    841 
    842     APPL_TRACE_EVENT0("## ON A2DP STARTED ##");
    843 
    844     if (p_av == NULL)
    845     {
    846         /* ack back a local start request */
    847         a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
    848         return;
    849     }
    850 
    851     if (p_av->status == BTA_AV_SUCCESS)
    852     {
    853         if (p_av->suspending == FALSE)
    854         {
    855             if (p_av->initiator)
    856             {
    857                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
    858             }
    859             else
    860             {
    861                 /* we were remotely started,  make sure codec
    862                    is setup before datapath is started */
    863                 btif_a2dp_setup_codec();
    864             }
    865 
    866             /* media task is autostarted upon a2dp audiopath connection */
    867         }
    868     }
    869     else
    870     {
    871         a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
    872     }
    873 }
    874 
    875 
    876 /*****************************************************************************
    877 **
    878 ** Function        btif_a2dp_ack_fail
    879 **
    880 ** Description
    881 **
    882 ** Returns
    883 **
    884 *******************************************************************************/
    885 
    886 void btif_a2dp_ack_fail(void)
    887 {
    888     tBTIF_STATUS status;
    889 
    890     APPL_TRACE_EVENT0("## A2DP_CTRL_ACK_FAILURE ##");
    891     a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
    892 }
    893 
    894 /*****************************************************************************
    895 **
    896 ** Function        btif_a2dp_on_stopped
    897 **
    898 ** Description
    899 **
    900 ** Returns
    901 **
    902 *******************************************************************************/
    903 
    904 void btif_a2dp_on_stopped(tBTA_AV_SUSPEND *p_av)
    905 {
    906     APPL_TRACE_EVENT0("## ON A2DP STOPPED ##");
    907 
    908     /* allow using this api for other than suspend */
    909     if (p_av != NULL)
    910     {
    911         if (p_av->status != BTA_AV_SUCCESS)
    912         {
    913             APPL_TRACE_EVENT1("AV STOP FAILED (%d)", p_av->status);
    914 
    915             if (p_av->initiator)
    916                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
    917             return;
    918         }
    919     }
    920 
    921     /* ensure tx frames are immediately suspended */
    922     btif_media_cb.tx_flush = 1;
    923 
    924     /* request to stop media task  */
    925     btif_media_task_aa_tx_flush_req();
    926     btif_media_task_stop_aa_req();
    927 
    928     /* once stream is fully stopped we will ack back */
    929 }
    930 
    931 
    932 /*****************************************************************************
    933 **
    934 ** Function        btif_a2dp_on_suspended
    935 **
    936 ** Description
    937 **
    938 ** Returns
    939 **
    940 *******************************************************************************/
    941 
    942 void btif_a2dp_on_suspended(tBTA_AV_SUSPEND *p_av)
    943 {
    944     APPL_TRACE_EVENT0("## ON A2DP SUSPENDED ##");
    945 
    946     /* check for status failures */
    947     if (p_av->status != BTA_AV_SUCCESS)
    948     {
    949         if (p_av->initiator == TRUE)
    950             a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
    951     }
    952 
    953     /* once stream is fully stopped we will ack back */
    954 
    955     /* ensure tx frames are immediately flushed */
    956     btif_media_cb.tx_flush = 1;
    957 
    958     /* stop timer tick */
    959     btif_media_task_stop_aa_req();
    960 }
    961 
    962 /* when true media task discards any tx frames */
    963 void btif_a2dp_set_tx_flush(BOOLEAN enable)
    964 {
    965     APPL_TRACE_EVENT1("## DROP TX %d ##", enable);
    966     btif_media_cb.tx_flush = enable;
    967 }
    968 
    969 /*****************************************************************************
    970 **
    971 ** Function        btif_calc_pcmtime
    972 **
    973 ** Description     Calculates the pcmtime equivalent of a datapacket
    974 **
    975 ** Returns         microseconds
    976 **
    977 *******************************************************************************/
    978 
    979 static int btif_calc_pcmtime(UINT32 bytes_processed)
    980 {
    981     int pcm_time_us = 0;
    982     tBTIF_AV_MEDIA_FEED_CFG *p_cfg;
    983 
    984     p_cfg = &btif_media_cb.media_feeding.cfg;
    985 
    986     /* calculate corresponding pcm time based on data processed */
    987     switch(btif_media_cb.media_feeding.format)
    988     {
    989         case BTIF_AV_CODEC_PCM:
    990             pcm_time_us = (bytes_processed*1000000)/
    991                           (p_cfg->pcm.num_channel*p_cfg->pcm.sampling_freq*p_cfg->pcm.bit_per_sample/8);
    992             break;
    993 
    994         default :
    995             APPL_TRACE_ERROR1("mediafeeding format invalid : %d", btif_media_cb.media_feeding.format);
    996             break;
    997     }
    998 
    999     return pcm_time_us;
   1000 }
   1001 
   1002 
   1003 /*******************************************************************************
   1004  **
   1005  ** Function         btif_media_task_aa_handle_timer
   1006  **
   1007  ** Description
   1008  **
   1009  ** Returns          void
   1010  **
   1011  *******************************************************************************/
   1012 
   1013 static void btif_media_task_aa_handle_timer(void)
   1014 {
   1015 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
   1016     static UINT16 Debug = 0;
   1017     APPL_TRACE_DEBUG1("btif_media_task_aa_handle_timer: %d", Debug++);
   1018 #endif
   1019 
   1020     log_tstamps_us("media task tx timer");
   1021 
   1022 #if (BTA_AV_INCLUDED == TRUE)
   1023     btif_media_send_aa_frame();
   1024 #endif
   1025 }
   1026 
   1027 #if (BTA_AV_INCLUDED == TRUE)
   1028 /*******************************************************************************
   1029  **
   1030  ** Function         btif_media_task_aa_handle_timer
   1031  **
   1032  ** Description
   1033  **
   1034  ** Returns          void
   1035  **
   1036  *******************************************************************************/
   1037 static void btif_media_task_aa_handle_uipc_rx_rdy(void)
   1038 {
   1039 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
   1040     static UINT16 Debug = 0;
   1041     APPL_TRACE_DEBUG1("btif_media_task_aa_handle_uipc_rx_rdy: %d", Debug++);
   1042 #endif
   1043 
   1044     /* process all the UIPC data */
   1045     btif_media_aa_prep_2_send(0xFF);
   1046 
   1047     /* send it */
   1048     VERBOSE("btif_media_task_aa_handle_uipc_rx_rdy calls bta_av_ci_src_data_ready");
   1049     bta_av_ci_src_data_ready(BTA_AV_CHNL_AUDIO);
   1050 }
   1051 #endif
   1052 
   1053 /*******************************************************************************
   1054  **
   1055  ** Function         btif_media_task_init
   1056  **
   1057  ** Description
   1058  **
   1059  ** Returns          void
   1060  **
   1061  *******************************************************************************/
   1062 
   1063 void btif_media_task_init(void)
   1064 {
   1065     memset(&(btif_media_cb), 0, sizeof(btif_media_cb));
   1066 
   1067     UIPC_Init(NULL);
   1068 
   1069 #if (BTA_AV_INCLUDED == TRUE)
   1070     UIPC_Open(UIPC_CH_ID_AV_CTRL , btif_a2dp_ctrl_cb);
   1071 #endif
   1072 
   1073 
   1074 }
   1075 /*******************************************************************************
   1076  **
   1077  ** Function         btif_media_task
   1078  **
   1079  ** Description      Task for SBC encoder.  This task receives an
   1080  **                  event when the waveIn interface has a pcm data buffer
   1081  **                  ready.  On receiving the event, handle all ready pcm
   1082  **                  data buffers.  If stream is started, run the SBC encoder
   1083  **                  on each chunk of pcm samples and build an output packet
   1084  **                  consisting of one or more encoded SBC frames.
   1085  **
   1086  ** Returns          void
   1087  **
   1088  *******************************************************************************/
   1089 int btif_media_task(void *p)
   1090 {
   1091     UINT16 event;
   1092     BT_HDR *p_msg;
   1093 
   1094     VERBOSE("================ MEDIA TASK STARTING ================");
   1095 
   1096     btif_media_task_init();
   1097 
   1098     media_task_running = MEDIA_TASK_STATE_ON;
   1099 
   1100     raise_priority_a2dp(TASK_HIGH_MEDIA);
   1101 
   1102     while (1)
   1103     {
   1104         event = GKI_wait(0xffff, 0);
   1105 
   1106         VERBOSE("================= MEDIA TASK EVENT %d ===============", event);
   1107 
   1108         if (event & BTIF_MEDIA_TASK_CMD)
   1109         {
   1110             /* Process all messages in the queue */
   1111             while ((p_msg = (BT_HDR *) GKI_read_mbox(BTIF_MEDIA_TASK_CMD_MBOX)) != NULL)
   1112             {
   1113                 btif_media_task_handle_cmd(p_msg);
   1114             }
   1115         }
   1116 
   1117         if (event & BTIF_MEDIA_TASK_DATA)
   1118         {
   1119             /* Process all messages in the queue */
   1120             while ((p_msg = (BT_HDR *) GKI_read_mbox(BTIF_MEDIA_TASK_DATA_MBOX)) != NULL)
   1121             {
   1122                 btif_media_task_handle_media(p_msg);
   1123             }
   1124         }
   1125 
   1126         if (event & BTIF_MEDIA_AA_TASK_TIMER)
   1127         {
   1128             /* advance audio timer expiration */
   1129             btif_media_task_aa_handle_timer();
   1130         }
   1131 
   1132 
   1133         VERBOSE("=============== MEDIA TASK EVENT %d DONE ============", event);
   1134 
   1135         /* When we get this event we exit the task  - should only happen on GKI_shutdown  */
   1136         if (event & BTIF_MEDIA_TASK_KILL)
   1137         {
   1138             /* make sure no channels are restarted while shutting down */
   1139             media_task_running = MEDIA_TASK_STATE_SHUTTING_DOWN;
   1140 
   1141             /* this calls blocks until uipc is fully closed */
   1142             UIPC_Close(UIPC_CH_ID_ALL);
   1143             break;
   1144         }
   1145     }
   1146 
   1147     /* Clear media task flag */
   1148     media_task_running = MEDIA_TASK_STATE_OFF;
   1149 
   1150     APPL_TRACE_DEBUG0("MEDIA TASK EXITING");
   1151 
   1152     return 0;
   1153 }
   1154 
   1155 
   1156 /*******************************************************************************
   1157  **
   1158  ** Function         btif_media_task_send_cmd_evt
   1159  **
   1160  ** Description
   1161  **
   1162  ** Returns          TRUE is success
   1163  **
   1164  *******************************************************************************/
   1165 BOOLEAN btif_media_task_send_cmd_evt(UINT16 Evt)
   1166 {
   1167     BT_HDR *p_buf;
   1168     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
   1169     {
   1170         return FALSE;
   1171     }
   1172 
   1173     p_buf->event = Evt;
   1174 
   1175     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
   1176     return TRUE;
   1177 }
   1178 
   1179 /*******************************************************************************
   1180  **
   1181  ** Function         btif_media_flush_q
   1182  **
   1183  ** Description
   1184  **
   1185  ** Returns          void
   1186  **
   1187  *******************************************************************************/
   1188 static void btif_media_flush_q(BUFFER_Q *p_q)
   1189 {
   1190     while (GKI_IS_QUEUE_EMPTY(p_q) == FALSE)
   1191     {
   1192         GKI_freebuf(GKI_dequeue(p_q));
   1193     }
   1194 }
   1195 
   1196 
   1197 /*******************************************************************************
   1198  **
   1199  ** Function         btif_media_task_handle_cmd
   1200  **
   1201  ** Description
   1202  **
   1203  ** Returns          void
   1204  **
   1205  *******************************************************************************/
   1206 static void btif_media_task_handle_cmd(BT_HDR *p_msg)
   1207 {
   1208     VERBOSE("btif_media_task_handle_cmd : %d %s", p_msg->event,
   1209              dump_media_event(p_msg->event));
   1210 
   1211     switch (p_msg->event)
   1212     {
   1213 #if (BTA_AV_INCLUDED == TRUE)
   1214     case BTIF_MEDIA_START_AA_TX:
   1215         btif_media_task_aa_start_tx();
   1216         break;
   1217     case BTIF_MEDIA_STOP_AA_TX:
   1218         btif_media_task_aa_stop_tx();
   1219         break;
   1220     case BTIF_MEDIA_SBC_ENC_INIT:
   1221         btif_media_task_enc_init(p_msg);
   1222         break;
   1223     case BTIF_MEDIA_SBC_ENC_UPDATE:
   1224         btif_media_task_enc_update(p_msg);
   1225         break;
   1226     case BTIF_MEDIA_AUDIO_FEEDING_INIT:
   1227         btif_media_task_audio_feeding_init(p_msg);
   1228         break;
   1229     case BTIF_MEDIA_FLUSH_AA_TX:
   1230         btif_media_task_aa_tx_flush(p_msg);
   1231         break;
   1232     case BTIF_MEDIA_UIPC_RX_RDY:
   1233         btif_media_task_aa_handle_uipc_rx_rdy();
   1234         break;
   1235 #endif
   1236     default:
   1237         APPL_TRACE_ERROR1("ERROR in btif_media_task_handle_cmd unknown event %d", p_msg->event);
   1238     }
   1239     GKI_freebuf(p_msg);
   1240     VERBOSE("btif_media_task_handle_cmd : %s DONE", dump_media_event(p_msg->event));
   1241 }
   1242 
   1243 /*******************************************************************************
   1244  **
   1245  ** Function         btif_media_task_handle_media
   1246  **
   1247  ** Description
   1248  **
   1249  ** Returns          void
   1250  **
   1251  *******************************************************************************/
   1252 static void btif_media_task_handle_media(BT_HDR *p_msg)
   1253 {
   1254     APPL_TRACE_ERROR0("ERROR btif_media_task_handle_media: not in use");
   1255 
   1256     GKI_freebuf(p_msg);
   1257 }
   1258 
   1259 
   1260 
   1261 
   1262 #if (BTA_AV_INCLUDED == TRUE)
   1263 /*******************************************************************************
   1264  **
   1265  ** Function         btif_media_task_enc_init_req
   1266  **
   1267  ** Description
   1268  **
   1269  ** Returns          TRUE is success
   1270  **
   1271  *******************************************************************************/
   1272 BOOLEAN btif_media_task_enc_init_req(tBTIF_MEDIA_INIT_AUDIO *p_msg)
   1273 {
   1274     tBTIF_MEDIA_INIT_AUDIO *p_buf;
   1275     if (NULL == (p_buf = GKI_getbuf(sizeof(tBTIF_MEDIA_INIT_AUDIO))))
   1276     {
   1277         return FALSE;
   1278     }
   1279 
   1280     memcpy(p_buf, p_msg, sizeof(tBTIF_MEDIA_INIT_AUDIO));
   1281     p_buf->hdr.event = BTIF_MEDIA_SBC_ENC_INIT;
   1282 
   1283     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
   1284     return TRUE;
   1285 }
   1286 
   1287 /*******************************************************************************
   1288  **
   1289  ** Function         btif_media_task_enc_update_req
   1290  **
   1291  ** Description
   1292  **
   1293  ** Returns          TRUE is success
   1294  **
   1295  *******************************************************************************/
   1296 BOOLEAN btif_media_task_enc_update_req(tBTIF_MEDIA_UPDATE_AUDIO *p_msg)
   1297 {
   1298     tBTIF_MEDIA_UPDATE_AUDIO *p_buf;
   1299     if (NULL == (p_buf = GKI_getbuf(sizeof(tBTIF_MEDIA_UPDATE_AUDIO))))
   1300     {
   1301         return FALSE;
   1302     }
   1303 
   1304     memcpy(p_buf, p_msg, sizeof(tBTIF_MEDIA_UPDATE_AUDIO));
   1305     p_buf->hdr.event = BTIF_MEDIA_SBC_ENC_UPDATE;
   1306 
   1307     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
   1308     return TRUE;
   1309 }
   1310 
   1311 /*******************************************************************************
   1312  **
   1313  ** Function         btif_media_task_audio_feeding_init_req
   1314  **
   1315  ** Description
   1316  **
   1317  ** Returns          TRUE is success
   1318  **
   1319  *******************************************************************************/
   1320 BOOLEAN btif_media_task_audio_feeding_init_req(tBTIF_MEDIA_INIT_AUDIO_FEEDING *p_msg)
   1321 {
   1322     tBTIF_MEDIA_INIT_AUDIO_FEEDING *p_buf;
   1323     if (NULL == (p_buf = GKI_getbuf(sizeof(tBTIF_MEDIA_INIT_AUDIO_FEEDING))))
   1324     {
   1325         return FALSE;
   1326     }
   1327 
   1328     memcpy(p_buf, p_msg, sizeof(tBTIF_MEDIA_INIT_AUDIO_FEEDING));
   1329     p_buf->hdr.event = BTIF_MEDIA_AUDIO_FEEDING_INIT;
   1330 
   1331     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
   1332     return TRUE;
   1333 }
   1334 
   1335 /*******************************************************************************
   1336  **
   1337  ** Function         btif_media_task_start_aa_req
   1338  **
   1339  ** Description
   1340  **
   1341  ** Returns          TRUE is success
   1342  **
   1343  *******************************************************************************/
   1344 BOOLEAN btif_media_task_start_aa_req(void)
   1345 {
   1346     BT_HDR *p_buf;
   1347     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
   1348     {
   1349         APPL_TRACE_EVENT0("GKI failed");
   1350         return FALSE;
   1351     }
   1352 
   1353     p_buf->event = BTIF_MEDIA_START_AA_TX;
   1354 
   1355     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
   1356     return TRUE;
   1357 }
   1358 
   1359 /*******************************************************************************
   1360  **
   1361  ** Function         btif_media_task_stop_aa_req
   1362  **
   1363  ** Description
   1364  **
   1365  ** Returns          TRUE is success
   1366  **
   1367  *******************************************************************************/
   1368 BOOLEAN btif_media_task_stop_aa_req(void)
   1369 {
   1370     BT_HDR *p_buf;
   1371     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
   1372     {
   1373         return FALSE;
   1374     }
   1375 
   1376     p_buf->event = BTIF_MEDIA_STOP_AA_TX;
   1377 
   1378     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
   1379     return TRUE;
   1380 }
   1381 
   1382 /*******************************************************************************
   1383  **
   1384  ** Function         btif_media_task_aa_tx_flush_req
   1385  **
   1386  ** Description
   1387  **
   1388  ** Returns          TRUE is success
   1389  **
   1390  *******************************************************************************/
   1391 BOOLEAN btif_media_task_aa_tx_flush_req(void)
   1392 {
   1393     BT_HDR *p_buf;
   1394     if (NULL == (p_buf = GKI_getbuf(sizeof(BT_HDR))))
   1395     {
   1396         return FALSE;
   1397     }
   1398 
   1399     p_buf->event = BTIF_MEDIA_FLUSH_AA_TX;
   1400 
   1401     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_buf);
   1402     return TRUE;
   1403 }
   1404 
   1405 /*******************************************************************************
   1406  **
   1407  ** Function         btif_media_task_aa_tx_flush
   1408  **
   1409  ** Description
   1410  **
   1411  ** Returns          void
   1412  **
   1413  *******************************************************************************/
   1414 static void btif_media_task_aa_tx_flush(BT_HDR *p_msg)
   1415 {
   1416     /* Flush all enqueued GKI music buffers (encoded) */
   1417     APPL_TRACE_DEBUG0("btif_media_task_aa_tx_flush");
   1418 
   1419     btif_media_flush_q(&(btif_media_cb.TxAaQ));
   1420 
   1421     UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_REQ_RX_FLUSH, NULL);
   1422 }
   1423 
   1424 /*******************************************************************************
   1425  **
   1426  ** Function       btif_media_task_enc_init
   1427  **
   1428  ** Description    Initialize encoding task
   1429  **
   1430  ** Returns        void
   1431  **
   1432  *******************************************************************************/
   1433 static void btif_media_task_enc_init(BT_HDR *p_msg)
   1434 {
   1435     tBTIF_MEDIA_INIT_AUDIO *pInitAudio = (tBTIF_MEDIA_INIT_AUDIO *) p_msg;
   1436 
   1437     APPL_TRACE_DEBUG0("btif_media_task_enc_init");
   1438 
   1439     btif_media_cb.timestamp = 0;
   1440 
   1441     /* SBC encoder config (enforced even if not used) */
   1442     btif_media_cb.encoder.s16ChannelMode = pInitAudio->ChannelMode;
   1443     btif_media_cb.encoder.s16NumOfSubBands = pInitAudio->NumOfSubBands;
   1444     btif_media_cb.encoder.s16NumOfBlocks = pInitAudio->NumOfBlocks;
   1445     btif_media_cb.encoder.s16AllocationMethod = pInitAudio->AllocationMethod;
   1446     btif_media_cb.encoder.s16SamplingFreq = pInitAudio->SamplingFreq;
   1447 
   1448     btif_media_cb.encoder.u16BitRate = DEFAULT_SBC_BITRATE;
   1449     /* Default transcoding is PCM to SBC, modified by feeding configuration */
   1450     btif_media_cb.TxTranscoding = BTIF_MEDIA_TRSCD_PCM_2_SBC;
   1451     btif_media_cb.TxAaMtuSize = ((BTIF_MEDIA_AA_BUF_SIZE-BTIF_MEDIA_AA_SBC_OFFSET-sizeof(BT_HDR))
   1452             < pInitAudio->MtuSize) ? (BTIF_MEDIA_AA_BUF_SIZE - BTIF_MEDIA_AA_SBC_OFFSET
   1453             - sizeof(BT_HDR)) : pInitAudio->MtuSize;
   1454 
   1455     APPL_TRACE_EVENT3("btif_media_task_enc_init busy %d, mtu %d, peer mtu %d",
   1456                      btif_media_cb.busy_level, btif_media_cb.TxAaMtuSize, pInitAudio->MtuSize);
   1457     APPL_TRACE_EVENT6("      ch mode %d, subnd %d, nb blk %d, alloc %d, rate %d, freq %d",
   1458             btif_media_cb.encoder.s16ChannelMode, btif_media_cb.encoder.s16NumOfSubBands,
   1459             btif_media_cb.encoder.s16NumOfBlocks,
   1460             btif_media_cb.encoder.s16AllocationMethod, btif_media_cb.encoder.u16BitRate,
   1461             btif_media_cb.encoder.s16SamplingFreq);
   1462 
   1463     /* Reset entirely the SBC encoder */
   1464     SBC_Encoder_Init(&(btif_media_cb.encoder));
   1465     APPL_TRACE_DEBUG1("btif_media_task_enc_init bit pool %d", btif_media_cb.encoder.s16BitPool);
   1466 }
   1467 
   1468 /*******************************************************************************
   1469  **
   1470  ** Function       btif_media_task_enc_update
   1471  **
   1472  ** Description    Update encoding task
   1473  **
   1474  ** Returns        void
   1475  **
   1476  *******************************************************************************/
   1477 
   1478 static void btif_media_task_enc_update(BT_HDR *p_msg)
   1479 {
   1480     tBTIF_MEDIA_UPDATE_AUDIO * pUpdateAudio = (tBTIF_MEDIA_UPDATE_AUDIO *) p_msg;
   1481     SBC_ENC_PARAMS *pstrEncParams = &btif_media_cb.encoder;
   1482     UINT16 s16SamplingFreq;
   1483     SINT16 s16BitPool;
   1484     SINT16 s16BitRate;
   1485     SINT16 s16FrameLen;
   1486     UINT8 protect = 0;
   1487 
   1488     APPL_TRACE_DEBUG3("btif_media_task_enc_update : minmtu %d, maxbp %d minbp %d",
   1489             pUpdateAudio->MinMtuSize, pUpdateAudio->MaxBitPool, pUpdateAudio->MinBitPool);
   1490 
   1491     /* Only update the bitrate and MTU size while timer is running to make sure it has been initialized */
   1492     //if (btif_media_cb.is_tx_timer)
   1493     {
   1494         btif_media_cb.TxAaMtuSize = ((BTIF_MEDIA_AA_BUF_SIZE -
   1495                                       BTIF_MEDIA_AA_SBC_OFFSET - sizeof(BT_HDR))
   1496                 < pUpdateAudio->MinMtuSize) ? (BTIF_MEDIA_AA_BUF_SIZE - BTIF_MEDIA_AA_SBC_OFFSET
   1497                 - sizeof(BT_HDR)) : pUpdateAudio->MinMtuSize;
   1498 
   1499         /* Set the initial target bit rate */
   1500         pstrEncParams->u16BitRate = DEFAULT_SBC_BITRATE;
   1501 
   1502         if (pstrEncParams->s16SamplingFreq == SBC_sf16000)
   1503             s16SamplingFreq = 16000;
   1504         else if (pstrEncParams->s16SamplingFreq == SBC_sf32000)
   1505             s16SamplingFreq = 32000;
   1506         else if (pstrEncParams->s16SamplingFreq == SBC_sf44100)
   1507             s16SamplingFreq = 44100;
   1508         else
   1509             s16SamplingFreq = 48000;
   1510 
   1511         do
   1512         {
   1513             if ((pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) ||
   1514                 (pstrEncParams->s16ChannelMode == SBC_STEREO) )
   1515             {
   1516                 s16BitPool = (SINT16)( (pstrEncParams->u16BitRate *
   1517                     pstrEncParams->s16NumOfSubBands * 1000 / s16SamplingFreq)
   1518                     -( (32 + (4 * pstrEncParams->s16NumOfSubBands *
   1519                     pstrEncParams->s16NumOfChannels)
   1520                     + ( (pstrEncParams->s16ChannelMode - 2) *
   1521                     pstrEncParams->s16NumOfSubBands )   )
   1522                     / pstrEncParams->s16NumOfBlocks) );
   1523 
   1524                 s16FrameLen = 4 + (4*pstrEncParams->s16NumOfSubBands*
   1525                     pstrEncParams->s16NumOfChannels)/8
   1526                     + ( ((pstrEncParams->s16ChannelMode - 2) *
   1527                     pstrEncParams->s16NumOfSubBands)
   1528                     + (pstrEncParams->s16NumOfBlocks * s16BitPool) ) / 8;
   1529 
   1530                 s16BitRate = (8 * s16FrameLen * s16SamplingFreq)
   1531                     / (pstrEncParams->s16NumOfSubBands *
   1532                     pstrEncParams->s16NumOfBlocks * 1000);
   1533 
   1534                 if (s16BitRate > pstrEncParams->u16BitRate)
   1535                     s16BitPool--;
   1536 
   1537                 if(pstrEncParams->s16NumOfSubBands == 8)
   1538                     s16BitPool = (s16BitPool > 255) ? 255 : s16BitPool;
   1539                 else
   1540                     s16BitPool = (s16BitPool > 128) ? 128 : s16BitPool;
   1541             }
   1542             else
   1543             {
   1544                 s16BitPool = (SINT16)( ((pstrEncParams->s16NumOfSubBands *
   1545                     pstrEncParams->u16BitRate * 1000)
   1546                     / (s16SamplingFreq * pstrEncParams->s16NumOfChannels))
   1547                     -( ( (32 / pstrEncParams->s16NumOfChannels) +
   1548                     (4 * pstrEncParams->s16NumOfSubBands) )
   1549                     /   pstrEncParams->s16NumOfBlocks ) );
   1550 
   1551                 pstrEncParams->s16BitPool = (s16BitPool >
   1552                     (16 * pstrEncParams->s16NumOfSubBands))
   1553                     ? (16*pstrEncParams->s16NumOfSubBands) : s16BitPool;
   1554             }
   1555 
   1556             if (s16BitPool < 0)
   1557             {
   1558                 s16BitPool = 0;
   1559             }
   1560 
   1561             APPL_TRACE_EVENT2("bitpool candidate : %d (%d kbps)",
   1562                          s16BitPool, pstrEncParams->u16BitRate);
   1563 
   1564             if (s16BitPool > pUpdateAudio->MaxBitPool)
   1565             {
   1566                 APPL_TRACE_DEBUG1("btif_media_task_enc_update computed bitpool too large (%d)",
   1567                                     s16BitPool);
   1568                 /* Decrease bitrate */
   1569                 btif_media_cb.encoder.u16BitRate -= BTIF_MEDIA_BITRATE_STEP;
   1570                 /* Record that we have decreased the bitrate */
   1571                 protect |= 1;
   1572             }
   1573             else if (s16BitPool < pUpdateAudio->MinBitPool)
   1574             {
   1575                 APPL_TRACE_WARNING1("btif_media_task_enc_update computed bitpool too small (%d)", s16BitPool);
   1576                 /* Increase bitrate */
   1577                 btif_media_cb.encoder.u16BitRate += BTIF_MEDIA_BITRATE_STEP;
   1578                 /* Record that we have increased the bitrate */
   1579                 protect |= 2;
   1580             }
   1581             else
   1582             {
   1583                 break;
   1584             }
   1585             /* In case we have already increased and decreased the bitrate, just stop */
   1586             if (protect == 3)
   1587             {
   1588                 APPL_TRACE_ERROR0("btif_media_task_enc_update could not find bitpool in range");
   1589                 break;
   1590             }
   1591         } while (1);
   1592 
   1593         /* Finally update the bitpool in the encoder structure */
   1594         pstrEncParams->s16BitPool = s16BitPool;
   1595 
   1596         APPL_TRACE_DEBUG2("btif_media_task_enc_update final bit rate %d, final bit pool %d",
   1597                 btif_media_cb.encoder.u16BitRate, btif_media_cb.encoder.s16BitPool);
   1598 
   1599         /* make sure we reinitialize encoder with new settings */
   1600         SBC_Encoder_Init(&(btif_media_cb.encoder));
   1601     }
   1602 }
   1603 
   1604 /*******************************************************************************
   1605  **
   1606  ** Function         btif_media_task_pcm2sbc_init
   1607  **
   1608  ** Description      Init encoding task for PCM to SBC according to feeding
   1609  **
   1610  ** Returns          void
   1611  **
   1612  *******************************************************************************/
   1613 static void btif_media_task_pcm2sbc_init(tBTIF_MEDIA_INIT_AUDIO_FEEDING * p_feeding)
   1614 {
   1615     BOOLEAN reconfig_needed = FALSE;
   1616 
   1617     APPL_TRACE_DEBUG0("PCM feeding:");
   1618     APPL_TRACE_DEBUG1("sampling_freq:%d", p_feeding->feeding.cfg.pcm.sampling_freq);
   1619     APPL_TRACE_DEBUG1("num_channel:%d", p_feeding->feeding.cfg.pcm.num_channel);
   1620     APPL_TRACE_DEBUG1("bit_per_sample:%d", p_feeding->feeding.cfg.pcm.bit_per_sample);
   1621 
   1622     /* Check the PCM feeding sampling_freq */
   1623     switch (p_feeding->feeding.cfg.pcm.sampling_freq)
   1624     {
   1625         case  8000:
   1626         case 12000:
   1627         case 16000:
   1628         case 24000:
   1629         case 32000:
   1630         case 48000:
   1631             /* For these sampling_freq the AV connection must be 48000 */
   1632             if (btif_media_cb.encoder.s16SamplingFreq != SBC_sf48000)
   1633             {
   1634                 /* Reconfiguration needed at 48000 */
   1635                 APPL_TRACE_DEBUG0("SBC Reconfiguration needed at 48000");
   1636                 btif_media_cb.encoder.s16SamplingFreq = SBC_sf48000;
   1637                 reconfig_needed = TRUE;
   1638             }
   1639             break;
   1640 
   1641         case 11025:
   1642         case 22050:
   1643         case 44100:
   1644             /* For these sampling_freq the AV connection must be 44100 */
   1645             if (btif_media_cb.encoder.s16SamplingFreq != SBC_sf44100)
   1646             {
   1647                 /* Reconfiguration needed at 44100 */
   1648                 APPL_TRACE_DEBUG0("SBC Reconfiguration needed at 44100");
   1649                 btif_media_cb.encoder.s16SamplingFreq = SBC_sf44100;
   1650                 reconfig_needed = TRUE;
   1651             }
   1652             break;
   1653         default:
   1654             APPL_TRACE_DEBUG0("Feeding PCM sampling_freq unsupported");
   1655             break;
   1656     }
   1657 
   1658     /* Some AV Headsets do not support Mono => always ask for Stereo */
   1659     if (btif_media_cb.encoder.s16ChannelMode == SBC_MONO)
   1660     {
   1661         APPL_TRACE_DEBUG0("SBC Reconfiguration needed in Stereo");
   1662         btif_media_cb.encoder.s16ChannelMode = SBC_JOINT_STEREO;
   1663         reconfig_needed = TRUE;
   1664     }
   1665 
   1666     if (reconfig_needed != FALSE)
   1667     {
   1668         APPL_TRACE_DEBUG1("btif_media_task_pcm2sbc_init :: mtu %d", btif_media_cb.TxAaMtuSize);
   1669         APPL_TRACE_DEBUG6("ch mode %d, nbsubd %d, nb %d, alloc %d, rate %d, freq %d",
   1670                 btif_media_cb.encoder.s16ChannelMode,
   1671                 btif_media_cb.encoder.s16NumOfSubBands, btif_media_cb.encoder.s16NumOfBlocks,
   1672                 btif_media_cb.encoder.s16AllocationMethod, btif_media_cb.encoder.u16BitRate,
   1673                 btif_media_cb.encoder.s16SamplingFreq);
   1674 
   1675         SBC_Encoder_Init(&(btif_media_cb.encoder));
   1676     }
   1677     else
   1678     {
   1679         APPL_TRACE_DEBUG0("btif_media_task_pcm2sbc_init no SBC reconfig needed");
   1680     }
   1681 }
   1682 
   1683 
   1684 /*******************************************************************************
   1685  **
   1686  ** Function         btif_media_task_audio_feeding_init
   1687  **
   1688  ** Description      Initialize the audio path according to the feeding format
   1689  **
   1690  ** Returns          void
   1691  **
   1692  *******************************************************************************/
   1693 static void btif_media_task_audio_feeding_init(BT_HDR *p_msg)
   1694 {
   1695     tBTIF_MEDIA_INIT_AUDIO_FEEDING *p_feeding = (tBTIF_MEDIA_INIT_AUDIO_FEEDING *) p_msg;
   1696 
   1697     APPL_TRACE_DEBUG1("btif_media_task_audio_feeding_init format:%d", p_feeding->feeding.format);
   1698 
   1699     /* Save Media Feeding information */
   1700     btif_media_cb.feeding_mode = p_feeding->feeding_mode;
   1701     btif_media_cb.media_feeding = p_feeding->feeding;
   1702 
   1703     /* Handle different feeding formats */
   1704     switch (p_feeding->feeding.format)
   1705     {
   1706         case BTIF_AV_CODEC_PCM:
   1707             btif_media_cb.TxTranscoding = BTIF_MEDIA_TRSCD_PCM_2_SBC;
   1708             btif_media_task_pcm2sbc_init(p_feeding);
   1709             break;
   1710 
   1711         default :
   1712             APPL_TRACE_ERROR1("unknown feeding format %d", p_feeding->feeding.format);
   1713             break;
   1714     }
   1715 }
   1716 
   1717 /*******************************************************************************
   1718  **
   1719  ** Function         btif_media_task_uipc_cback
   1720  **
   1721  ** Description      UIPC call back function for synchronous mode only
   1722  **
   1723  ** Returns          void
   1724  **
   1725  *******************************************************************************/
   1726 static void btif_media_task_uipc_cback(BT_HDR *p_msg)
   1727 {
   1728     /* Sanity check */
   1729     if (NULL == p_msg)
   1730     {
   1731         return;
   1732     }
   1733 
   1734     /* Just handle RX_EVT */
   1735     if (p_msg->event != UIPC_RX_DATA_EVT)
   1736     {
   1737         return;
   1738     }
   1739 
   1740     p_msg->event = BTIF_MEDIA_UIPC_RX_RDY;
   1741 
   1742     GKI_send_msg(BT_MEDIA_TASK, BTIF_MEDIA_TASK_CMD_MBOX, p_msg);
   1743 }
   1744 
   1745 /*******************************************************************************
   1746  **
   1747  ** Function         btif_media_task_feeding_state_reset
   1748  **
   1749  ** Description      Reset the media feeding state
   1750  **
   1751  ** Returns          void
   1752  **
   1753  *******************************************************************************/
   1754 static void btif_media_task_feeding_state_reset(void)
   1755 {
   1756     /* By default, just clear the entire state */
   1757     memset(&btif_media_cb.media_feeding_state, 0, sizeof(btif_media_cb.media_feeding_state));
   1758 }
   1759 /*******************************************************************************
   1760  **
   1761  ** Function         btif_media_task_aa_start_tx
   1762  **
   1763  ** Description      Start media task encoding
   1764  **
   1765  ** Returns          void
   1766  **
   1767  *******************************************************************************/
   1768 static void btif_media_task_aa_start_tx(void)
   1769 {
   1770     APPL_TRACE_DEBUG2("btif_media_task_aa_start_tx is timer %d, feeding mode %d",
   1771              btif_media_cb.is_tx_timer, btif_media_cb.feeding_mode);
   1772 
   1773     /* Use a timer to poll the UIPC, get rid of the UIPC call back */
   1774     // UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_REG_CBACK, NULL);
   1775 
   1776     btif_media_cb.is_tx_timer = TRUE;
   1777 
   1778     /* Reset the media feeding state */
   1779     btif_media_task_feeding_state_reset();
   1780 
   1781     APPL_TRACE_EVENT2("starting timer %d ticks (%d)",
   1782                   GKI_MS_TO_TICKS(BTIF_MEDIA_TIME_TICK), TICKS_PER_SEC);
   1783 
   1784     GKI_start_timer(BTIF_MEDIA_AA_TASK_TIMER_ID, GKI_MS_TO_TICKS(BTIF_MEDIA_TIME_TICK), TRUE);
   1785 }
   1786 
   1787 /*******************************************************************************
   1788  **
   1789  ** Function         btif_media_task_aa_stop_tx
   1790  **
   1791  ** Description      Stop media task encoding
   1792  **
   1793  ** Returns          void
   1794  **
   1795  *******************************************************************************/
   1796 static void btif_media_task_aa_stop_tx(void)
   1797 {
   1798     APPL_TRACE_DEBUG1("btif_media_task_aa_stop_tx is timer: %d", btif_media_cb.is_tx_timer);
   1799 
   1800     /* Stop the timer first */
   1801     GKI_stop_timer(BTIF_MEDIA_AA_TASK_TIMER_ID);
   1802     btif_media_cb.is_tx_timer = FALSE;
   1803 
   1804     UIPC_Close(UIPC_CH_ID_AV_AUDIO);
   1805 
   1806     /* audio engine stopped, reset tx suspended flag */
   1807     btif_media_cb.tx_flush = 0;
   1808 
   1809     /* Reset the media feeding state */
   1810     btif_media_task_feeding_state_reset();
   1811 }
   1812 
   1813 /*******************************************************************************
   1814  **
   1815  ** Function         btif_get_num_aa_frame
   1816  **
   1817  ** Description
   1818  **
   1819  ** Returns          The number of media frames in this time slice
   1820  **
   1821  *******************************************************************************/
   1822 static UINT8 btif_get_num_aa_frame(void)
   1823 {
   1824     UINT8 result=0;
   1825 
   1826     switch (btif_media_cb.TxTranscoding)
   1827     {
   1828         case BTIF_MEDIA_TRSCD_PCM_2_SBC:
   1829             switch (btif_media_cb.encoder.s16SamplingFreq)
   1830             {
   1831             case SBC_sf16000:
   1832                 if (!btif_media_cb.scaling_disabled &&
   1833                     (btif_media_cb.media_feeding_state.pcm.aa_frame_counter++ % 2) == 0)
   1834                 {
   1835                     result = BTIF_MEDIA_FR_PER_TICKS_16-1;
   1836                 }
   1837                 else
   1838                 {
   1839                     result = BTIF_MEDIA_FR_PER_TICKS_16;
   1840                 }
   1841                 break;
   1842 
   1843             case SBC_sf32000:
   1844                 result = BTIF_MEDIA_FR_PER_TICKS_32;
   1845                 break;
   1846 
   1847             case SBC_sf48000:
   1848                 if (!btif_media_cb.scaling_disabled &&
   1849                     (btif_media_cb.media_feeding_state.pcm.aa_frame_counter++ % 2) == 0)
   1850                 {
   1851                     result = BTIF_MEDIA_FR_PER_TICKS_48-1;
   1852                 }
   1853                 else
   1854                 {
   1855                     result = BTIF_MEDIA_FR_PER_TICKS_48;
   1856                 }
   1857                 break;
   1858 
   1859             case SBC_sf44100:
   1860                 if (!btif_media_cb.scaling_disabled &&
   1861                     (btif_media_cb.media_feeding_state.pcm.aa_frame_counter++ % 64) < 7)
   1862                 {
   1863                     result = BTIF_MEDIA_FR_PER_TICKS_44_1-1;
   1864                 }
   1865                 else
   1866                 {
   1867                     result = BTIF_MEDIA_FR_PER_TICKS_44_1;
   1868                 }
   1869                 break;
   1870             }
   1871 
   1872             VERBOSE("WRITE %d FRAMES", result);
   1873             break;
   1874 
   1875         default:
   1876             APPL_TRACE_ERROR1("ERROR btif_get_num_aa_frame Unsupported transcoding format 0x%x",
   1877                     btif_media_cb.TxTranscoding);
   1878             result = 0;
   1879             break;
   1880     }
   1881 
   1882 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
   1883     APPL_TRACE_DEBUG1("btif_get_num_aa_frame returns %d", result);
   1884 #endif
   1885 
   1886     return result;
   1887 }
   1888 
   1889 /*******************************************************************************
   1890  **
   1891  ** Function         btif_media_aa_readbuf
   1892  **
   1893  ** Description      This function is called by the av_co to get the next buffer to send
   1894  **
   1895  **
   1896  ** Returns          void
   1897  *******************************************************************************/
   1898 BT_HDR *btif_media_aa_readbuf(void)
   1899 {
   1900     return GKI_dequeue(&(btif_media_cb.TxAaQ));
   1901 }
   1902 
   1903 /*******************************************************************************
   1904  **
   1905  ** Function         btif_media_aa_read_feeding
   1906  **
   1907  ** Description
   1908  **
   1909  ** Returns          void
   1910  **
   1911  *******************************************************************************/
   1912 
   1913 BOOLEAN btif_media_aa_read_feeding(tUIPC_CH_ID channel_id)
   1914 {
   1915     UINT16 event;
   1916     UINT16 blocm_x_subband = btif_media_cb.encoder.s16NumOfSubBands * \
   1917                              btif_media_cb.encoder.s16NumOfBlocks;
   1918     UINT32 read_size;
   1919     UINT16 sbc_sampling = 48000;
   1920     UINT32 src_samples;
   1921     UINT16 bytes_needed = blocm_x_subband * btif_media_cb.encoder.s16NumOfChannels * \
   1922                           sizeof(SINT16);
   1923     static UINT16 up_sampled_buffer[SBC_MAX_NUM_FRAME * SBC_MAX_NUM_OF_BLOCKS
   1924             * SBC_MAX_NUM_OF_CHANNELS * SBC_MAX_NUM_OF_SUBBANDS * 2];
   1925     static UINT16 read_buffer[SBC_MAX_NUM_FRAME * SBC_MAX_NUM_OF_BLOCKS
   1926             * SBC_MAX_NUM_OF_CHANNELS * SBC_MAX_NUM_OF_SUBBANDS];
   1927     UINT32 src_size_used;
   1928     UINT32 dst_size_used;
   1929     BOOLEAN fract_needed;
   1930     INT32   fract_max;
   1931     INT32   fract_threshold;
   1932     UINT32  nb_byte_read;
   1933 
   1934     /* Get the SBC sampling rate */
   1935     switch (btif_media_cb.encoder.s16SamplingFreq)
   1936     {
   1937     case SBC_sf48000:
   1938         sbc_sampling = 48000;
   1939         break;
   1940     case SBC_sf44100:
   1941         sbc_sampling = 44100;
   1942         break;
   1943     case SBC_sf32000:
   1944         sbc_sampling = 32000;
   1945         break;
   1946     case SBC_sf16000:
   1947         sbc_sampling = 16000;
   1948         break;
   1949     }
   1950 
   1951     /* Some Feeding PCM frequencies require to split the number of sample */
   1952     /* to read. */
   1953     /* E.g 128/6=21.3333 => read 22 and 21 and 21 => max = 2; threshold = 0*/
   1954     fract_needed = FALSE;   /* Default */
   1955     switch (btif_media_cb.media_feeding.cfg.pcm.sampling_freq)
   1956     {
   1957     case 32000:
   1958     case 8000:
   1959         fract_needed = TRUE;
   1960         fract_max = 2;          /* 0, 1 and 2 */
   1961         fract_threshold = 0;    /* Add one for the first */
   1962         break;
   1963     case 16000:
   1964         fract_needed = TRUE;
   1965         fract_max = 2;          /* 0, 1 and 2 */
   1966         fract_threshold = 1;    /* Add one for the first two frames*/
   1967         break;
   1968     }
   1969 
   1970     /* Compute number of sample to read from source */
   1971     src_samples = blocm_x_subband;
   1972     src_samples *= btif_media_cb.media_feeding.cfg.pcm.sampling_freq;
   1973     src_samples /= sbc_sampling;
   1974 
   1975     /* The previous division may have a remainder not null */
   1976     if (fract_needed)
   1977     {
   1978         if (btif_media_cb.media_feeding_state.pcm.aa_feed_counter <= fract_threshold)
   1979         {
   1980             src_samples++; /* for every read before threshold add one sample */
   1981         }
   1982 
   1983         /* do nothing if counter >= threshold */
   1984         btif_media_cb.media_feeding_state.pcm.aa_feed_counter++; /* one more read */
   1985         if (btif_media_cb.media_feeding_state.pcm.aa_feed_counter > fract_max)
   1986         {
   1987             btif_media_cb.media_feeding_state.pcm.aa_feed_counter = 0;
   1988         }
   1989     }
   1990 
   1991     /* Compute number of bytes to read from source */
   1992     read_size = src_samples;
   1993     read_size *= btif_media_cb.media_feeding.cfg.pcm.num_channel;
   1994     read_size *= (btif_media_cb.media_feeding.cfg.pcm.bit_per_sample / 8);
   1995 
   1996     /* Read Data from UIPC channel */
   1997     nb_byte_read = UIPC_Read(channel_id, &event, (UINT8 *)read_buffer, read_size);
   1998 
   1999     //tput_mon(TRUE, nb_byte_read, FALSE);
   2000 
   2001     if (nb_byte_read < read_size)
   2002     {
   2003         APPL_TRACE_WARNING2("### UNDERRUN :: ONLY READ %d BYTES OUT OF %d ###",
   2004                 nb_byte_read, read_size);
   2005 
   2006         if (nb_byte_read == 0)
   2007             return FALSE;
   2008 
   2009         if(btif_media_cb.feeding_mode == BTIF_AV_FEEDING_ASYNCHRONOUS)
   2010         {
   2011             /* Fill the unfilled part of the read buffer with silence (0) */
   2012             memset(((UINT8 *)read_buffer) + nb_byte_read, 0, read_size - nb_byte_read);
   2013             nb_byte_read = read_size;
   2014         }
   2015     }
   2016 
   2017     /* Initialize PCM up-sampling engine */
   2018     bta_av_sbc_init_up_sample(btif_media_cb.media_feeding.cfg.pcm.sampling_freq,
   2019             sbc_sampling, btif_media_cb.media_feeding.cfg.pcm.bit_per_sample,
   2020             btif_media_cb.media_feeding.cfg.pcm.num_channel);
   2021 
   2022     /* re-sample read buffer */
   2023     /* The output PCM buffer will be stereo, 16 bit per sample */
   2024     dst_size_used = bta_av_sbc_up_sample((UINT8 *)read_buffer,
   2025             (UINT8 *)up_sampled_buffer + btif_media_cb.media_feeding_state.pcm.aa_feed_residue,
   2026             nb_byte_read,
   2027             sizeof(up_sampled_buffer) - btif_media_cb.media_feeding_state.pcm.aa_feed_residue,
   2028             &src_size_used);
   2029 
   2030 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
   2031     APPL_TRACE_DEBUG3("btif_media_aa_read_feeding readsz:%d src_size_used:%d dst_size_used:%d",
   2032             read_size, src_size_used, dst_size_used);
   2033 #endif
   2034 
   2035     /* update the residue */
   2036     btif_media_cb.media_feeding_state.pcm.aa_feed_residue += dst_size_used;
   2037 
   2038     /* only copy the pcm sample when we have up-sampled enough PCM */
   2039     if(btif_media_cb.media_feeding_state.pcm.aa_feed_residue >= bytes_needed)
   2040     {
   2041         /* Copy the output pcm samples in SBC encoding buffer */
   2042         memcpy((UINT8 *)btif_media_cb.encoder.as16PcmBuffer,
   2043                 (UINT8 *)up_sampled_buffer,
   2044                 bytes_needed);
   2045         /* update the residue */
   2046         btif_media_cb.media_feeding_state.pcm.aa_feed_residue -= bytes_needed;
   2047 
   2048         if (btif_media_cb.media_feeding_state.pcm.aa_feed_residue != 0)
   2049         {
   2050             memcpy((UINT8 *)up_sampled_buffer,
   2051                    (UINT8 *)up_sampled_buffer + bytes_needed,
   2052                    btif_media_cb.media_feeding_state.pcm.aa_feed_residue);
   2053         }
   2054         return TRUE;
   2055     }
   2056 
   2057 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
   2058     APPL_TRACE_DEBUG3("btif_media_aa_read_feeding residue:%d, dst_size_used %d, bytes_needed %d",
   2059             btif_media_cb.media_feeding_state.pcm.aa_feed_residue, dst_size_used, bytes_needed);
   2060 #endif
   2061 
   2062     return FALSE;
   2063 }
   2064 
   2065 /*******************************************************************************
   2066  **
   2067  ** Function         btif_media_aa_prep_sbc_2_send
   2068  **
   2069  ** Description
   2070  **
   2071  ** Returns          void
   2072  **
   2073  *******************************************************************************/
   2074 static void btif_media_aa_prep_sbc_2_send(UINT8 nb_frame)
   2075 {
   2076     BT_HDR * p_buf;
   2077     UINT16 blocm_x_subband = btif_media_cb.encoder.s16NumOfSubBands *
   2078                              btif_media_cb.encoder.s16NumOfBlocks;
   2079 
   2080 #if (defined(DEBUG_MEDIA_AV_FLOW) && (DEBUG_MEDIA_AV_FLOW == TRUE))
   2081     APPL_TRACE_DEBUG2("btif_media_aa_prep_sbc_2_send nb_frame %d, TxAaQ %d",
   2082                        nb_frame, btif_media_cb.TxAaQ.count);
   2083 #endif
   2084     while (nb_frame)
   2085     {
   2086         if (NULL == (p_buf = GKI_getpoolbuf(BTIF_MEDIA_AA_POOL_ID)))
   2087         {
   2088             APPL_TRACE_ERROR1 ("ERROR btif_media_aa_prep_sbc_2_send no buffer TxCnt %d ",
   2089                                 btif_media_cb.TxAaQ.count);
   2090             return;
   2091         }
   2092 
   2093         /* Init buffer */
   2094         p_buf->offset = BTIF_MEDIA_AA_SBC_OFFSET;
   2095         p_buf->len = 0;
   2096         p_buf->layer_specific = 0;
   2097 
   2098         do
   2099         {
   2100             /* Write @ of allocated buffer in encoder.pu8Packet */
   2101             btif_media_cb.encoder.pu8Packet = (UINT8 *) (p_buf + 1) + p_buf->offset + p_buf->len;
   2102             /* Fill allocated buffer with 0 */
   2103             memset(btif_media_cb.encoder.as16PcmBuffer, 0, blocm_x_subband
   2104                     * btif_media_cb.encoder.s16NumOfChannels);
   2105 
   2106             /* Read PCM data and upsample them if needed */
   2107             if (btif_media_aa_read_feeding(UIPC_CH_ID_AV_AUDIO))
   2108             {
   2109                 /* SBC encode and descramble frame */
   2110                 SBC_Encoder(&(btif_media_cb.encoder));
   2111                 A2D_SbcChkFrInit(btif_media_cb.encoder.pu8Packet);
   2112                 A2D_SbcDescramble(btif_media_cb.encoder.pu8Packet, btif_media_cb.encoder.u16PacketLength);
   2113                 /* Update SBC frame length */
   2114                 p_buf->len += btif_media_cb.encoder.u16PacketLength;
   2115                 nb_frame--;
   2116                 p_buf->layer_specific++;
   2117             }
   2118             else
   2119             {
   2120                 /* no more pcm to read */
   2121                 nb_frame = 0;
   2122 
   2123                 /* break read loop if timer was stopped (media task stopped) */
   2124                 if ( btif_media_cb.is_tx_timer == FALSE )
   2125                     return;
   2126             }
   2127 
   2128         } while (((p_buf->len + btif_media_cb.encoder.u16PacketLength) < btif_media_cb.TxAaMtuSize)
   2129                 && (p_buf->layer_specific < 0x0F) && nb_frame);
   2130 
   2131         if(p_buf->len)
   2132         {
   2133             /* timestamp of the media packet header represent the TS of the first SBC frame
   2134                i.e the timestamp before including this frame */
   2135             *((UINT32 *) (p_buf + 1)) = btif_media_cb.timestamp;
   2136 
   2137             btif_media_cb.timestamp += p_buf->layer_specific * blocm_x_subband;
   2138 
   2139             VERBOSE("TX QUEUE NOW %d", btif_media_cb.TxAaQ.count);
   2140 
   2141             if (btif_media_cb.tx_flush)
   2142             {
   2143                 APPL_TRACE_DEBUG0("### tx suspended, discarded frame ###");
   2144 
   2145                 if (btif_media_cb.TxAaQ.count > 0)
   2146                     btif_media_flush_q(&(btif_media_cb.TxAaQ));
   2147 
   2148                 GKI_freebuf(p_buf);
   2149                 return;
   2150             }
   2151 
   2152             /* Enqueue the encoded SBC frame in AA Tx Queue */
   2153             GKI_enqueue(&(btif_media_cb.TxAaQ), p_buf);
   2154         }
   2155         else
   2156         {
   2157             GKI_freebuf(p_buf);
   2158         }
   2159     }
   2160 }
   2161 
   2162 
   2163 /*******************************************************************************
   2164  **
   2165  ** Function         btif_media_aa_prep_2_send
   2166  **
   2167  ** Description
   2168  **
   2169  ** Returns          void
   2170  **
   2171  *******************************************************************************/
   2172 
   2173 static void btif_media_aa_prep_2_send(UINT8 nb_frame)
   2174 {
   2175     VERBOSE("btif_media_aa_prep_2_send : %d frames (queue %d)", nb_frame,
   2176                        btif_media_cb.TxAaQ.count);
   2177 
   2178     /* Remove all the buffers not sent until there are only 4 in the queue */
   2179     while (btif_media_cb.TxAaQ.count >= MAX_OUTPUT_BUFFER_QUEUE_SZ)
   2180     {
   2181         APPL_TRACE_WARNING1("btif_media_aa_prep_2_send congestion buf count %d",btif_media_cb.TxAaQ.count);
   2182         GKI_freebuf(GKI_dequeue(&(btif_media_cb.TxAaQ)));
   2183     }
   2184 
   2185     switch (btif_media_cb.TxTranscoding)
   2186     {
   2187     case BTIF_MEDIA_TRSCD_PCM_2_SBC:
   2188         btif_media_aa_prep_sbc_2_send(nb_frame);
   2189         break;
   2190 
   2191 
   2192     default:
   2193         APPL_TRACE_ERROR1("ERROR btif_media_aa_prep_2_send unsupported transcoding format 0x%x",btif_media_cb.TxTranscoding);
   2194         break;
   2195     }
   2196 }
   2197 
   2198 /*******************************************************************************
   2199  **
   2200  ** Function         btif_media_send_aa_frame
   2201  **
   2202  ** Description
   2203  **
   2204  ** Returns          void
   2205  **
   2206  *******************************************************************************/
   2207 static void btif_media_send_aa_frame(void)
   2208 {
   2209     UINT8 nb_frame_2_send;
   2210 
   2211     /* get the number of frame to send */
   2212     nb_frame_2_send = btif_get_num_aa_frame();
   2213 
   2214     /* format and Q buffer to send */
   2215     btif_media_aa_prep_2_send(nb_frame_2_send);
   2216 
   2217     /* send it */
   2218     VERBOSE("btif_media_send_aa_frame : send %d frames", nb_frame_2_send);
   2219     bta_av_ci_src_data_ready(BTA_AV_CHNL_AUDIO);
   2220 }
   2221 
   2222 /*******************************************************************************
   2223  **
   2224  ** Function         btif_media_check_iop_exceptions
   2225  **
   2226  ** Description    Perform any device specific iop changes
   2227  **
   2228  ** Returns          void
   2229  **
   2230  *******************************************************************************/
   2231 
   2232 void btif_media_check_iop_exceptions(UINT8 *peer_bda)
   2233 {
   2234     /* disable rate scaling for pcm carkit */
   2235     if ((peer_bda[0] == 0x00) &&
   2236         (peer_bda[1] == 0x0E) &&
   2237         (peer_bda[2] == 0x9F))
   2238     {
   2239         BTIF_TRACE_WARNING0("detected pcm carkit, disable rate scaling");
   2240         btif_media_cb.scaling_disabled = TRUE;
   2241     }
   2242     else
   2243     {
   2244         btif_media_cb.scaling_disabled = FALSE;
   2245     }
   2246 }
   2247 
   2248 
   2249 #endif /* BTA_AV_INCLUDED == TRUE */
   2250 
   2251 /*******************************************************************************
   2252  **
   2253  ** Function         dump_codec_info
   2254  **
   2255  ** Description      Decode and display codec_info (for debug)
   2256  **
   2257  ** Returns          void
   2258  **
   2259  *******************************************************************************/
   2260 void dump_codec_info(unsigned char *p_codec)
   2261 {
   2262     tA2D_STATUS a2d_status;
   2263     tA2D_SBC_CIE sbc_cie;
   2264 
   2265     a2d_status = A2D_ParsSbcInfo(&sbc_cie, p_codec, FALSE);
   2266     if (a2d_status != A2D_SUCCESS)
   2267     {
   2268         APPL_TRACE_ERROR1("ERROR dump_codec_info A2D_ParsSbcInfo fail:%d", a2d_status);
   2269         return;
   2270     }
   2271 
   2272     APPL_TRACE_DEBUG0("dump_codec_info");
   2273 
   2274     if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_16)
   2275     {    APPL_TRACE_DEBUG1("\tsamp_freq:%d (16000)", sbc_cie.samp_freq);}
   2276     else  if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_32)
   2277     {    APPL_TRACE_DEBUG1("\tsamp_freq:%d (32000)", sbc_cie.samp_freq);}
   2278     else  if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_44)
   2279     {    APPL_TRACE_DEBUG1("\tsamp_freq:%d (44.100)", sbc_cie.samp_freq);}
   2280     else  if (sbc_cie.samp_freq == A2D_SBC_IE_SAMP_FREQ_48)
   2281     {    APPL_TRACE_DEBUG1("\tsamp_freq:%d (48000)", sbc_cie.samp_freq);}
   2282     else
   2283     {    APPL_TRACE_DEBUG1("\tBAD samp_freq:%d", sbc_cie.samp_freq);}
   2284 
   2285     if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_MONO)
   2286     {    APPL_TRACE_DEBUG1("\tch_mode:%d (Mono)", sbc_cie.ch_mode);}
   2287     else  if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_DUAL)
   2288     {    APPL_TRACE_DEBUG1("\tch_mode:%d (Dual)", sbc_cie.ch_mode);}
   2289     else  if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_STEREO)
   2290     {    APPL_TRACE_DEBUG1("\tch_mode:%d (Stereo)", sbc_cie.ch_mode);}
   2291     else  if (sbc_cie.ch_mode == A2D_SBC_IE_CH_MD_JOINT)
   2292     {    APPL_TRACE_DEBUG1("\tch_mode:%d (Joint)", sbc_cie.ch_mode);}
   2293     else
   2294     {    APPL_TRACE_DEBUG1("\tBAD ch_mode:%d", sbc_cie.ch_mode);}
   2295 
   2296     if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_4)
   2297     {    APPL_TRACE_DEBUG1("\tblock_len:%d (4)", sbc_cie.block_len);}
   2298     else  if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_8)
   2299     {    APPL_TRACE_DEBUG1("\tblock_len:%d (8)", sbc_cie.block_len);}
   2300     else  if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_12)
   2301     {    APPL_TRACE_DEBUG1("\tblock_len:%d (12)", sbc_cie.block_len);}
   2302     else  if (sbc_cie.block_len == A2D_SBC_IE_BLOCKS_16)
   2303     {    APPL_TRACE_DEBUG1("\tblock_len:%d (16)", sbc_cie.block_len);}
   2304     else
   2305     {    APPL_TRACE_DEBUG1("\tBAD block_len:%d", sbc_cie.block_len);}
   2306 
   2307     if (sbc_cie.num_subbands == A2D_SBC_IE_SUBBAND_4)
   2308     {    APPL_TRACE_DEBUG1("\tnum_subbands:%d (4)", sbc_cie.num_subbands);}
   2309     else  if (sbc_cie.num_subbands == A2D_SBC_IE_SUBBAND_8)
   2310     {    APPL_TRACE_DEBUG1("\tnum_subbands:%d (8)", sbc_cie.num_subbands);}
   2311     else
   2312     {    APPL_TRACE_DEBUG1("\tBAD num_subbands:%d", sbc_cie.num_subbands);}
   2313 
   2314     if (sbc_cie.alloc_mthd == A2D_SBC_IE_ALLOC_MD_S)
   2315     {    APPL_TRACE_DEBUG1("\talloc_mthd:%d (SNR)", sbc_cie.alloc_mthd);}
   2316     else  if (sbc_cie.alloc_mthd == A2D_SBC_IE_ALLOC_MD_L)
   2317     {    APPL_TRACE_DEBUG1("\talloc_mthd:%d (Loundess)", sbc_cie.alloc_mthd);}
   2318     else
   2319     {    APPL_TRACE_DEBUG1("\tBAD alloc_mthd:%d", sbc_cie.alloc_mthd);}
   2320 
   2321     APPL_TRACE_DEBUG2("\tBit pool Min:%d Max:%d", sbc_cie.min_bitpool, sbc_cie.max_bitpool);
   2322 
   2323 }
   2324 
   2325