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