1 /******************************************************************************
2  *
3  *  Copyright 2016 The Android Open Source Project
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_hearing_aid_hw.h
22  *
23  *  Description:
24  *
25  *****************************************************************************/
26 
27 #ifndef AUDIO_HEARING_AID_HW_H
28 #define AUDIO_HEARING_AID_HW_H
29 
30 #include <stdint.h>
31 
32 #include <hardware/bt_av.h>
33 
34 /*****************************************************************************
35  *  Constants & Macros
36  *****************************************************************************/
37 
38 #define HEARING_AID_AUDIO_HARDWARE_INTERFACE "audio.hearing_aid"
39 #define HEARING_AID_CTRL_PATH "/data/misc/bluedroid/.hearing_aid_ctrl"
40 #define HEARING_AID_DATA_PATH "/data/misc/bluedroid/.hearing_aid_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   HEARING_AID_CTRL_CMD_NONE,
80   HEARING_AID_CTRL_CMD_CHECK_READY,
81   HEARING_AID_CTRL_CMD_START,
82   HEARING_AID_CTRL_CMD_STOP,
83   HEARING_AID_CTRL_CMD_SUSPEND,
84   HEARING_AID_CTRL_GET_INPUT_AUDIO_CONFIG,
85   HEARING_AID_CTRL_GET_OUTPUT_AUDIO_CONFIG,
86   HEARING_AID_CTRL_SET_OUTPUT_AUDIO_CONFIG,
87   HEARING_AID_CTRL_CMD_OFFLOAD_START,
88 } tHEARING_AID_CTRL_CMD;
89 
90 typedef enum {
91   HEARING_AID_CTRL_ACK_SUCCESS,
92   HEARING_AID_CTRL_ACK_FAILURE,
93   HEARING_AID_CTRL_ACK_INCALL_FAILURE, /* Failure when in Call*/
94   HEARING_AID_CTRL_ACK_UNSUPPORTED
95 } tHEARING_AID_CTRL_ACK;
96 
97 typedef uint32_t tHA_SAMPLE_RATE;
98 typedef uint8_t tHA_CHANNEL_COUNT;
99 
100 /*****************************************************************************
101  *  Type definitions for callback functions
102  *****************************************************************************/
103 
104 /*****************************************************************************
105  *  Type definitions and return values
106  *****************************************************************************/
107 
108 /*****************************************************************************
109  *  Extern variables and functions
110  *****************************************************************************/
111 
112 /*****************************************************************************
113  *  Functions
114  *****************************************************************************/
115 
116 // Computes the Audio Hearing Aid HAL output buffer size.
117 // |codec_sample_rate| is the sample rate of the output stream.
118 // |codec_bits_per_sample| is the number of bits per sample of the output
119 // stream.
120 // |codec_channel_mode| is the channel mode of the output stream.
121 //
122 // The buffer size is computed by using the following formula:
123 //
124 // AUDIO_STREAM_OUTPUT_BUFFER_SIZE =
125 //    (TIME_PERIOD_MS * AUDIO_STREAM_OUTPUT_BUFFER_PERIODS *
126 //     SAMPLE_RATE_HZ * NUMBER_OF_CHANNELS * (BITS_PER_SAMPLE / 8)) / 1000
127 //
128 // AUDIO_STREAM_OUTPUT_BUFFER_PERIODS controls how the socket buffer is
129 // divided for AudioFlinger data delivery. The AudioFlinger mixer delivers
130 // data in chunks of
131 // (AUDIO_STREAM_OUTPUT_BUFFER_SIZE / AUDIO_STREAM_OUTPUT_BUFFER_PERIODS) .
132 // If the number of periods is 2, the socket buffer represents "double
133 // buffering" of the AudioFlinger mixer buffer.
134 //
135 // Furthermore, the AudioFlinger expects the buffer size to be a multiple
136 // of 16 frames.
137 //
138 // NOTE: Currently, the computation uses the conservative 20ms time period.
139 //
140 // Returns the computed buffer size. If any of the input parameters is
141 // invalid, the return value is the default |AUDIO_STREAM_OUTPUT_BUFFER_SZ|.
142 extern size_t audio_ha_hw_stream_compute_buffer_size(
143     btav_a2dp_codec_sample_rate_t codec_sample_rate,
144     btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample,
145     btav_a2dp_codec_channel_mode_t codec_channel_mode);
146 
147 #endif /* AUDIO_HEARING_AID_HW_H */
148