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