15738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/******************************************************************************
25738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
35738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Copyright (C) 2009-2012 Broadcom Corporation
45738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
55738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Licensed under the Apache License, Version 2.0 (the "License");
65738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  you may not use this file except in compliance with the License.
75738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  You may obtain a copy of the License at:
85738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
95738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  http://www.apache.org/licenses/LICENSE-2.0
105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  See the License for the specific language governing permissions and
155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  limitations under the License.
165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project ******************************************************************************/
185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*****************************************************************************
205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Filename:      audio_a2dp_hw.h
225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
235738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Description:
245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *****************************************************************************/
265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
275738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#ifndef AUDIO_A2DP_HW_H
285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#define AUDIO_A2DP_HW_H
295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
304ebaa86a3ab1ea390d238e1f6dee62cd837c60edPavlin Radoslavov#include <stdint.h>
314ebaa86a3ab1ea390d238e1f6dee62cd837c60edPavlin Radoslavov
32304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov#include <hardware/bt_av.h>
33304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov
345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*****************************************************************************
35ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *  Constants & Macros
36ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *****************************************************************************/
375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#define A2DP_AUDIO_HARDWARE_INTERFACE "audio.a2dp"
395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#define A2DP_CTRL_PATH "/data/misc/bluedroid/.a2dp_ctrl"
405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#define A2DP_DATA_PATH "/data/misc/bluedroid/.a2dp_data"
415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
423d02215979170e36d237457a1fd7c574ca81e99cAndy Hung// AUDIO_STREAM_OUTPUT_BUFFER_SZ controls the size of the audio socket buffer.
433d02215979170e36d237457a1fd7c574ca81e99cAndy Hung// If one assumes the write buffer is always full during normal BT playback,
443d02215979170e36d237457a1fd7c574ca81e99cAndy Hung// then increasing this value increases our playback latency.
453d02215979170e36d237457a1fd7c574ca81e99cAndy Hung//
463d02215979170e36d237457a1fd7c574ca81e99cAndy Hung// FIXME: The BT HAL should consume data at a constant rate.
479ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// AudioFlinger assumes that the HAL draws data at a constant rate, which is
489ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// true for most audio devices; however, the BT engine reads data at a variable
499ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// rate (over the short term), which confuses both AudioFlinger as well as
509ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// applications which deliver data at a (generally) fixed rate.
513d02215979170e36d237457a1fd7c574ca81e99cAndy Hung//
529ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// 20 * 512 is not sufficient to smooth the variability for some BT devices,
533d02215979170e36d237457a1fd7c574ca81e99cAndy Hung// resulting in mixer sleep and throttling. We increase this to 28 * 512 to help
543d02215979170e36d237457a1fd7c574ca81e99cAndy Hung// reduce the effect of variable data consumption.
55911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#define AUDIO_STREAM_OUTPUT_BUFFER_SZ (28 * 512)
56b32704f424e0ab4cd33ad670173997f8e3a0a31cPavlin Radoslavov#define AUDIO_STREAM_CONTROL_OUTPUT_BUFFER_SZ 256
573d02215979170e36d237457a1fd7c574ca81e99cAndy Hung
583d02215979170e36d237457a1fd7c574ca81e99cAndy Hung// AUDIO_STREAM_OUTPUT_BUFFER_PERIODS controls how the socket buffer is divided
599ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// for AudioFlinger data delivery. The AudioFlinger mixer delivers data in
609ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// chunks of AUDIO_STREAM_OUTPUT_BUFFER_SZ / AUDIO_STREAM_OUTPUT_BUFFER_PERIODS.
619ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// If the number of periods is 2, the socket buffer represents "double
629ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// buffering" of the AudioFlinger mixer buffer.
633d02215979170e36d237457a1fd7c574ca81e99cAndy Hung//
649ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// In general, AUDIO_STREAM_OUTPUT_BUFFER_PERIODS * 16 * 4 should be a divisor
659ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// of AUDIO_STREAM_OUTPUT_BUFFER_SZ.
663d02215979170e36d237457a1fd7c574ca81e99cAndy Hung//
673d02215979170e36d237457a1fd7c574ca81e99cAndy Hung// These values should be chosen such that
683d02215979170e36d237457a1fd7c574ca81e99cAndy Hung//
693d02215979170e36d237457a1fd7c574ca81e99cAndy Hung// AUDIO_STREAM_BUFFER_SIZE * 1000 / (AUDIO_STREAM_OUTPUT_BUFFER_PERIODS
703d02215979170e36d237457a1fd7c574ca81e99cAndy Hung//         * AUDIO_STREAM_DEFAULT_RATE * 4) > 20 (ms)
713d02215979170e36d237457a1fd7c574ca81e99cAndy Hung//
729ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// to avoid introducing the FastMixer in AudioFlinger. Using the FastMixer
739ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson// results in unnecessary latency and CPU overhead for Bluetooth.
745e9f56940e1865d16892639e4c9144a65547626ePavlin Radoslavov#define AUDIO_STREAM_OUTPUT_BUFFER_PERIODS 2
753d02215979170e36d237457a1fd7c574ca81e99cAndy Hung
76911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#define AUDIO_SKT_DISCONNECTED (-1)
775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projecttypedef enum {
79911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_CMD_NONE,
80911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_CMD_CHECK_READY,
81911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_CMD_START,
82911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_CMD_STOP,
83911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_CMD_SUSPEND,
84911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_GET_INPUT_AUDIO_CONFIG,
85911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_GET_OUTPUT_AUDIO_CONFIG,
865ce0116fbf5b530d5b93f11312096eade1021c1ePavlin Radoslavov  A2DP_CTRL_SET_OUTPUT_AUDIO_CONFIG,
87911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_CMD_OFFLOAD_START,
885738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project} tA2DP_CTRL_CMD;
895738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projecttypedef enum {
91911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_ACK_SUCCESS,
92911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_ACK_FAILURE,
93911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_ACK_INCALL_FAILURE, /* Failure when in Call*/
94911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  A2DP_CTRL_ACK_UNSUPPORTED
955738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project} tA2DP_CTRL_ACK;
965738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
974ebaa86a3ab1ea390d238e1f6dee62cd837c60edPavlin Radoslavovtypedef uint32_t tA2DP_SAMPLE_RATE;
984ebaa86a3ab1ea390d238e1f6dee62cd837c60edPavlin Radoslavovtypedef uint8_t tA2DP_CHANNEL_COUNT;
994ebaa86a3ab1ea390d238e1f6dee62cd837c60edPavlin Radoslavovtypedef uint8_t tA2DP_BITS_PER_SAMPLE;
1005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1015738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*****************************************************************************
102ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *  Type definitions for callback functions
103ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *****************************************************************************/
1045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1055738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*****************************************************************************
106ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *  Type definitions and return values
107ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *****************************************************************************/
1085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1095738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*****************************************************************************
110ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *  Extern variables and functions
111ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *****************************************************************************/
1125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*****************************************************************************
114ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *  Functions
115ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *****************************************************************************/
1165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
117304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// Computes the Audio A2DP HAL output buffer size.
118304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// |codec_sample_rate| is the sample rate of the output stream.
119304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// |codec_bits_per_sample| is the number of bits per sample of the output
120304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// stream.
121304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// |codec_channel_mode| is the channel mode of the output stream.
122304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov//
123304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// The buffer size is computed by using the following formula:
124304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov//
125304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// AUDIO_STREAM_OUTPUT_BUFFER_SIZE =
126304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov//    (TIME_PERIOD_MS * AUDIO_STREAM_OUTPUT_BUFFER_PERIODS *
127304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov//     SAMPLE_RATE_HZ * NUMBER_OF_CHANNELS * (BITS_PER_SAMPLE / 8)) / 1000
128304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov//
129304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// AUDIO_STREAM_OUTPUT_BUFFER_PERIODS controls how the socket buffer is
130304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// divided for AudioFlinger data delivery. The AudioFlinger mixer delivers
131304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// data in chunks of
132304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// (AUDIO_STREAM_OUTPUT_BUFFER_SIZE / AUDIO_STREAM_OUTPUT_BUFFER_PERIODS) .
133304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// If the number of periods is 2, the socket buffer represents "double
134304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// buffering" of the AudioFlinger mixer buffer.
135304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov//
136304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// Furthermore, the AudioFlinger expects the buffer size to be a multiple
137304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// of 16 frames.
138304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov//
139304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// NOTE: Currently, the computation uses the conservative 20ms time period.
140304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov//
141304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// Returns the computed buffer size. If any of the input parameters is
142304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov// invalid, the return value is the default |AUDIO_STREAM_OUTPUT_BUFFER_SZ|.
143304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavovextern size_t audio_a2dp_hw_stream_compute_buffer_size(
144304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov    btav_a2dp_codec_sample_rate_t codec_sample_rate,
145304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov    btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample,
146304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov    btav_a2dp_codec_channel_mode_t codec_channel_mode);
147304ceeb5202060a9c625a55369cbf28d72f5e2d9Pavlin Radoslavov
148febb1515f9051c14da4d1ef69c006db77ab901a0Pavlin Radoslavov// Returns a string representation of |event|.
149febb1515f9051c14da4d1ef69c006db77ab901a0Pavlin Radoslavovextern const char* audio_a2dp_hw_dump_ctrl_event(tA2DP_CTRL_CMD event);
1505738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
151febb1515f9051c14da4d1ef69c006db77ab901a0Pavlin Radoslavov#endif /* A2DP_AUDIO_HW_H */
152