1 /******************************************************************************
2  *
3  *  Copyright 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   A2DP_CTRL_GET_PRESENTATION_POSITION,
89 } tA2DP_CTRL_CMD;
90 
91 typedef enum {
92   A2DP_CTRL_ACK_SUCCESS,
93   A2DP_CTRL_ACK_FAILURE,
94   A2DP_CTRL_ACK_INCALL_FAILURE, /* Failure when in Call*/
95   A2DP_CTRL_ACK_UNSUPPORTED,
96   A2DP_CTRL_ACK_PENDING,
97   A2DP_CTRL_ACK_DISCONNECT_IN_PROGRESS,
98 } tA2DP_CTRL_ACK;
99 
100 typedef uint32_t tA2DP_SAMPLE_RATE;
101 typedef uint8_t tA2DP_CHANNEL_COUNT;
102 typedef uint8_t tA2DP_BITS_PER_SAMPLE;
103 
104 /*****************************************************************************
105  *  Type definitions for callback functions
106  *****************************************************************************/
107 
108 /*****************************************************************************
109  *  Type definitions and return values
110  *****************************************************************************/
111 
112 /*****************************************************************************
113  *  Extern variables and functions
114  *****************************************************************************/
115 
116 /*****************************************************************************
117  *  Functions
118  *****************************************************************************/
119 
120 // Computes the Audio A2DP HAL output buffer size.
121 // |codec_sample_rate| is the sample rate of the output stream.
122 // |codec_bits_per_sample| is the number of bits per sample of the output
123 // stream.
124 // |codec_channel_mode| is the channel mode of the output stream.
125 //
126 // The buffer size is computed by using the following formula:
127 //
128 // AUDIO_STREAM_OUTPUT_BUFFER_SIZE =
129 //    (TIME_PERIOD_MS * AUDIO_STREAM_OUTPUT_BUFFER_PERIODS *
130 //     SAMPLE_RATE_HZ * NUMBER_OF_CHANNELS * (BITS_PER_SAMPLE / 8)) / 1000
131 //
132 // AUDIO_STREAM_OUTPUT_BUFFER_PERIODS controls how the socket buffer is
133 // divided for AudioFlinger data delivery. The AudioFlinger mixer delivers
134 // data in chunks of
135 // (AUDIO_STREAM_OUTPUT_BUFFER_SIZE / AUDIO_STREAM_OUTPUT_BUFFER_PERIODS) .
136 // If the number of periods is 2, the socket buffer represents "double
137 // buffering" of the AudioFlinger mixer buffer.
138 //
139 // Furthermore, the AudioFlinger expects the buffer size to be a multiple
140 // of 16 frames.
141 //
142 // NOTE: Currently, the computation uses the conservative 20ms time period.
143 //
144 // Returns the computed buffer size. If any of the input parameters is
145 // invalid, the return value is the default |AUDIO_STREAM_OUTPUT_BUFFER_SZ|.
146 size_t audio_a2dp_hw_stream_compute_buffer_size(
147     btav_a2dp_codec_sample_rate_t codec_sample_rate,
148     btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample,
149     btav_a2dp_codec_channel_mode_t codec_channel_mode);
150 
151 // Returns whether the delay reporting property is set.
152 bool delay_reporting_enabled();
153 
154 // Returns a string representation of |event|.
155 const char* audio_a2dp_hw_dump_ctrl_event(tA2DP_CTRL_CMD event);
156 
157 #endif /* A2DP_AUDIO_HW_H */
158