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  *  Filename:      audio_a2dp_hw.h
22  *
23  *  Description:
24  *
25  *****************************************************************************/
26 
27 #ifndef AUDIO_A2DP_HW_H
28 #define AUDIO_A2DP_HW_H
29 
30 #include <stdint.h>
31 
32 #include <hardware/bt_av.h>
33 
34 /*****************************************************************************
35  *  Constants & Macros
36  *****************************************************************************/
37 
38 #define A2DP_AUDIO_HARDWARE_INTERFACE "audio.a2dp"
39 #define A2DP_CTRL_PATH "/data/misc/bluedroid/.a2dp_ctrl"
40 #define A2DP_DATA_PATH "/data/misc/bluedroid/.a2dp_data"
41 
42 // AUDIO_STREAM_OUTPUT_BUFFER_SZ controls the size of the audio socket buffer.
43 // If one assumes the write buffer is always full during normal BT playback,
44 // then increasing this value increases our playback latency.
45 //
46 // FIXME: The BT HAL should consume data at a constant rate.
47 // AudioFlinger assumes that the HAL draws data at a constant rate, which is
48 // true for most audio devices; however, the BT engine reads data at a variable
49 // rate (over the short term), which confuses both AudioFlinger as well as
50 // applications which deliver data at a (generally) fixed rate.
51 //
52 // 20 * 512 is not sufficient to smooth the variability for some BT devices,
53 // resulting in mixer sleep and throttling. We increase this to 28 * 512 to help
54 // reduce the effect of variable data consumption.
55 #define AUDIO_STREAM_OUTPUT_BUFFER_SZ (28 * 512)
56 #define AUDIO_STREAM_CONTROL_OUTPUT_BUFFER_SZ 256
57 
58 // AUDIO_STREAM_OUTPUT_BUFFER_PERIODS controls how the socket buffer is divided
59 // for AudioFlinger data delivery. The AudioFlinger mixer delivers data in
60 // chunks of AUDIO_STREAM_OUTPUT_BUFFER_SZ / AUDIO_STREAM_OUTPUT_BUFFER_PERIODS.
61 // If the number of periods is 2, the socket buffer represents "double
62 // buffering" of the AudioFlinger mixer buffer.
63 //
64 // In general, AUDIO_STREAM_OUTPUT_BUFFER_PERIODS * 16 * 4 should be a divisor
65 // of AUDIO_STREAM_OUTPUT_BUFFER_SZ.
66 //
67 // These values should be chosen such that
68 //
69 // AUDIO_STREAM_BUFFER_SIZE * 1000 / (AUDIO_STREAM_OUTPUT_BUFFER_PERIODS
70 //         * AUDIO_STREAM_DEFAULT_RATE * 4) > 20 (ms)
71 //
72 // to avoid introducing the FastMixer in AudioFlinger. Using the FastMixer
73 // results in unnecessary latency and CPU overhead for Bluetooth.
74 #define AUDIO_STREAM_OUTPUT_BUFFER_PERIODS 2
75 
76 #define AUDIO_SKT_DISCONNECTED (-1)
77 
78 typedef enum {
79   A2DP_CTRL_CMD_NONE,
80   A2DP_CTRL_CMD_CHECK_READY,
81   A2DP_CTRL_CMD_START,
82   A2DP_CTRL_CMD_STOP,
83   A2DP_CTRL_CMD_SUSPEND,
84   A2DP_CTRL_GET_INPUT_AUDIO_CONFIG,
85   A2DP_CTRL_GET_OUTPUT_AUDIO_CONFIG,
86   A2DP_CTRL_SET_OUTPUT_AUDIO_CONFIG,
87   A2DP_CTRL_CMD_OFFLOAD_START,
88 } tA2DP_CTRL_CMD;
89 
90 typedef enum {
91   A2DP_CTRL_ACK_SUCCESS,
92   A2DP_CTRL_ACK_FAILURE,
93   A2DP_CTRL_ACK_INCALL_FAILURE, /* Failure when in Call*/
94   A2DP_CTRL_ACK_UNSUPPORTED
95 } tA2DP_CTRL_ACK;
96 
97 typedef uint32_t tA2DP_SAMPLE_RATE;
98 typedef uint8_t tA2DP_CHANNEL_COUNT;
99 typedef uint8_t tA2DP_BITS_PER_SAMPLE;
100 
101 /*****************************************************************************
102  *  Type definitions for callback functions
103  *****************************************************************************/
104 
105 /*****************************************************************************
106  *  Type definitions and return values
107  *****************************************************************************/
108 
109 /*****************************************************************************
110  *  Extern variables and functions
111  *****************************************************************************/
112 
113 /*****************************************************************************
114  *  Functions
115  *****************************************************************************/
116 
117 // Computes the Audio A2DP HAL output buffer size.
118 // |codec_sample_rate| is the sample rate of the output stream.
119 // |codec_bits_per_sample| is the number of bits per sample of the output
120 // stream.
121 // |codec_channel_mode| is the channel mode of the output stream.
122 //
123 // The buffer size is computed by using the following formula:
124 //
125 // AUDIO_STREAM_OUTPUT_BUFFER_SIZE =
126 //    (TIME_PERIOD_MS * AUDIO_STREAM_OUTPUT_BUFFER_PERIODS *
127 //     SAMPLE_RATE_HZ * NUMBER_OF_CHANNELS * (BITS_PER_SAMPLE / 8)) / 1000
128 //
129 // AUDIO_STREAM_OUTPUT_BUFFER_PERIODS controls how the socket buffer is
130 // divided for AudioFlinger data delivery. The AudioFlinger mixer delivers
131 // data in chunks of
132 // (AUDIO_STREAM_OUTPUT_BUFFER_SIZE / AUDIO_STREAM_OUTPUT_BUFFER_PERIODS) .
133 // If the number of periods is 2, the socket buffer represents "double
134 // buffering" of the AudioFlinger mixer buffer.
135 //
136 // Furthermore, the AudioFlinger expects the buffer size to be a multiple
137 // of 16 frames.
138 //
139 // NOTE: Currently, the computation uses the conservative 20ms time period.
140 //
141 // Returns the computed buffer size. If any of the input parameters is
142 // invalid, the return value is the default |AUDIO_STREAM_OUTPUT_BUFFER_SZ|.
143 extern size_t audio_a2dp_hw_stream_compute_buffer_size(
144     btav_a2dp_codec_sample_rate_t codec_sample_rate,
145     btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample,
146     btav_a2dp_codec_channel_mode_t codec_channel_mode);
147 
148 // Returns a string representation of |event|.
149 extern const char* audio_a2dp_hw_dump_ctrl_event(tA2DP_CTRL_CMD event);
150 
151 #endif /* A2DP_AUDIO_HW_H */
152