blob: 7814d956550f7cf65a7433a2c73d2fdb2ada7e16 [file] [log] [blame]
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -07001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * Not a contribution.
4 *
5 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#ifndef QCOM_AUDIO_HW_H
21#define QCOM_AUDIO_HW_H
22
23#include <cutils/list.h>
24#include <hardware/audio.h>
25#include <tinyalsa/asoundlib.h>
26#include <tinycompress/tinycompress.h>
Dhananjay Kumar09cbbf02013-11-25 15:49:57 +053027#include "sound/compress_params.h"
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -070028#include <audio_route/audio_route.h>
29
30#define VISUALIZER_LIBRARY_PATH "/system/lib/soundfx/libqcomvisualizer.so"
31
32/* Flags used to initialize acdb_settings variable that goes to ACDB library */
33#define DMIC_FLAG 0x00000002
34#define QMIC_FLAG 0x00000004
35#define TTY_MODE_OFF 0x00000010
36#define TTY_MODE_FULL 0x00000020
37#define TTY_MODE_VCO 0x00000040
38#define TTY_MODE_HCO 0x00000080
39#define TTY_MODE_CLEAR 0xFFFFFF0F
40
41#define ACDB_DEV_TYPE_OUT 1
42#define ACDB_DEV_TYPE_IN 2
43
44#define MAX_SUPPORTED_CHANNEL_MASKS 2
45#define DEFAULT_HDMI_OUT_CHANNELS 2
46
47typedef int snd_device_t;
Dhananjay Kumar09cbbf02013-11-25 15:49:57 +053048#include <platform.h>
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -070049
50/* These are the supported use cases by the hardware.
51 * Each usecase is mapped to a specific PCM device.
52 * Refer to pcm_device_table[].
53 */
54typedef enum {
55 USECASE_INVALID = -1,
56 /* Playback usecases */
57 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
58 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
59 USECASE_AUDIO_PLAYBACK_MULTI_CH,
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -070060 USECASE_AUDIO_PLAYBACK_FM,
Dhananjay Kumar09cbbf02013-11-25 15:49:57 +053061 USECASE_AUDIO_PLAYBACK_OFFLOAD,
62 USECASE_AUDIO_PLAYBACK_OFFLOAD1,
63 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
64 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -070065 /* Capture usecases */
66 USECASE_AUDIO_RECORD,
67 USECASE_AUDIO_RECORD_COMPRESS,
68 USECASE_AUDIO_RECORD_LOW_LATENCY,
69 USECASE_AUDIO_RECORD_FM_VIRTUAL,
70
71 /* Voice usecase */
72 USECASE_VOICE_CALL,
73
74 /* Voice extension usecases */
75 USECASE_VOICE2_CALL,
76 USECASE_VOLTE_CALL,
77 USECASE_QCHAT_CALL,
78 USECASE_COMPRESS_VOIP_CALL,
79
80 USECASE_INCALL_REC_UPLINK,
81 USECASE_INCALL_REC_DOWNLINK,
82 USECASE_INCALL_REC_UPLINK_AND_DOWNLINK,
83
84 USECASE_INCALL_MUSIC_UPLINK,
85 USECASE_INCALL_MUSIC_UPLINK2,
86
87 USECASE_AUDIO_SPKR_CALIB_RX,
88 USECASE_AUDIO_SPKR_CALIB_TX,
89 AUDIO_USECASE_MAX
90} audio_usecase_t;
91
Dhananjay Kumar09cbbf02013-11-25 15:49:57 +053092typedef enum {
93 DEEP_BUFFER_PLAYBACK_STREAM = 0,
94 LOW_LATENCY_PLAYBACK_STREAM,
95 MCH_PCM_PLAYBACK_STREAM,
96 OFFLOAD_PLAYBACK_STREAM,
97 LOW_LATENCY_RECORD_STREAM,
98 RECORD_STREAM,
99 VOICE_CALL_STREAM
100} audio_usecase_stream_type_t;
101
102#define STRING_TO_ENUM(string) { #string, string }
103struct string_to_enum {
104 const char *name;
105 uint32_t value;
106};
107
108static const struct string_to_enum out_channels_name_to_enum_table[] = {
109 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
110 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
111 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
112};
113
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -0700114#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
115
116/*
117 * tinyAlsa library interprets period size as number of frames
118 * one frame = channel_count * sizeof (pcm sample)
119 * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
120 * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
121 * We should take care of returning proper size when AudioFlinger queries for
122 * the buffer size of an input/output stream
123 */
124
125enum {
126 OFFLOAD_CMD_EXIT, /* exit compress offload thread loop*/
127 OFFLOAD_CMD_DRAIN, /* send a full drain request to DSP */
128 OFFLOAD_CMD_PARTIAL_DRAIN, /* send a partial drain request to DSP */
129 OFFLOAD_CMD_WAIT_FOR_BUFFER, /* wait for buffer released by DSP */
130};
131
132enum {
133 OFFLOAD_STATE_IDLE,
134 OFFLOAD_STATE_PLAYING,
135 OFFLOAD_STATE_PAUSED,
136};
137
138struct offload_cmd {
139 struct listnode node;
140 int cmd;
141 int data[];
142};
143
Dhananjay Kumar09cbbf02013-11-25 15:49:57 +0530144struct alsa_handle {
145
146 struct listnode list;
147//Parameters of the stream
148 struct pcm *pcm;
149 struct pcm_config config;
150
151 struct compress *compr;
152 struct compr_config compr_config;
153
154 struct stream_out *out;
155
156 audio_usecase_t usecase;
157 int device_id;
158 unsigned int sample_rate;
159 audio_channel_mask_t channel_mask;
160 audio_format_t input_format;
161 audio_format_t output_format;
162 audio_devices_t devices;
163
164 route_format_t route_format;
165 int decoder_type;
166
167 bool cmd_pending ;
168};
169
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -0700170struct stream_out {
171 struct audio_stream_out stream;
172 pthread_mutex_t lock; /* see note below on mutex acquisition order */
173 pthread_cond_t cond;
Dhananjay Kumar09cbbf02013-11-25 15:49:57 +0530174 /* TODO remove this */
175 /*
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -0700176 struct pcm_config config;
177 struct compr_config compr_config;
178 struct pcm *pcm;
179 struct compress *compr;
Dhananjay Kumar09cbbf02013-11-25 15:49:57 +0530180 */
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -0700181 int standby;
182 int pcm_device_id;
183 unsigned int sample_rate;
184 audio_channel_mask_t channel_mask;
185 audio_format_t format;
186 audio_devices_t devices;
187 audio_output_flags_t flags;
188 audio_usecase_t usecase;
189 /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
190 audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
191 bool muted;
192 uint64_t written; /* total frames written, not cleared when entering standby */
193 audio_io_handle_t handle;
194
195 int non_blocking;
196 int playback_started;
197 int offload_state;
198 pthread_cond_t offload_cond;
199 pthread_t offload_thread;
200 struct listnode offload_cmd_list;
201 bool offload_thread_blocked;
202
203 stream_callback_t offload_callback;
204 void *offload_cookie;
205 struct compr_gapless_mdata gapless_mdata;
206 int send_new_metadata;
207
208 struct audio_device *dev;
Dhananjay Kumar09cbbf02013-11-25 15:49:57 +0530209
210 /*devices configuration */
211 int left_volume;
212 int right_volume;
213 audio_usecase_stream_type_t uc_strm_type;
214 int hdmi_format;
215 int spdif_format;
216 int* device_formats; //TODO:Needs to come from AudioRutingManager
217 struct audio_config *config;
218
219 /* list of the session handles */
220 struct listnode session_list;
221
222 /* /MS11 instance */
223 int use_ms11_decoder;
224 void *ms11_decoder;
225 struct compr_config compr_config;
226
227 int channels;
228
229 /* Buffering utility */
230 struct audio_bitstream_sm *bitstrm;
231
232 int buffer_size;
233 int decoder_type;
234 bool dec_conf_set;
235 uint32_t min_bytes_req_to_dec;
236 bool is_m11_file_mode;
237 void *dec_conf_buf;
238 int32_t dec_conf_bufLength;
239 bool first_bitstrm_buf;
240
241 bool open_dec_route;
242 int dec_format_devices;
243 bool open_dec_mch_route;
244 int dec_mch_format_devices;
245 bool open_passt_route;
246 int passt_format_devices;
247 bool sw_open_trans_route;
248 int sw_trans_format_devices;
249 bool hw_open_trans_route;
250 int hw_trans_format_devices;
251 bool channel_status_set;
252 unsigned char channel_status[24];
253 int route_audio_to_a2dp;
254 int is_ms11_file_playback_mode;
255 char * write_temp_buf;
256 struct output_metadata output_meta_data;
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -0700257};
258
259struct stream_in {
260 struct audio_stream_in stream;
261 pthread_mutex_t lock; /* see note below on mutex acquisition order */
262 struct pcm_config config;
263 struct pcm *pcm;
264 int standby;
265 int source;
266 int pcm_device_id;
267 int device;
268 audio_channel_mask_t channel_mask;
269 audio_usecase_t usecase;
270 bool enable_aec;
271 bool enable_ns;
272 audio_format_t format;
273
274 struct audio_device *dev;
275};
276
277typedef enum {
278 PCM_PLAYBACK,
279 PCM_CAPTURE,
280 VOICE_CALL,
281 VOIP_CALL
282} usecase_type_t;
283
284union stream_ptr {
285 struct stream_in *in;
286 struct stream_out *out;
287};
288
289struct audio_usecase {
290 struct listnode list;
291 audio_usecase_t id;
292 usecase_type_t type;
293 audio_devices_t devices;
294 snd_device_t out_snd_device;
295 snd_device_t in_snd_device;
296 union stream_ptr stream;
Dhananjay Kumar09cbbf02013-11-25 15:49:57 +0530297 struct alsa_handle *handle;
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -0700298};
299
300struct audio_device {
301 struct audio_hw_device device;
302 pthread_mutex_t lock; /* see note below on mutex acquisition order */
303 struct mixer *mixer;
304 audio_mode_t mode;
305 audio_devices_t out_device;
306 struct stream_in *active_input;
307 struct stream_out *primary_output;
308 bool bluetooth_nrec;
309 bool screen_off;
310 int *snd_dev_ref_cnt;
311 struct listnode usecase_list;
312 struct audio_route *audio_route;
313 int acdb_settings;
314 bool speaker_lr_swap;
315 unsigned int cur_hdmi_channels;
316
317 void *platform;
318
319 void *visualizer_lib;
320 int (*visualizer_start_output)(audio_io_handle_t);
321 int (*visualizer_stop_output)(audio_io_handle_t);
322};
323
Dhananjay Kumar01e921a2013-11-26 23:33:22 +0530324static const char * const use_case_table[AUDIO_USECASE_MAX] = {
325 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
326 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
327 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
328 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Dhananjay Kumar09cbbf02013-11-25 15:49:57 +0530329 [USECASE_AUDIO_PLAYBACK_OFFLOAD1] = "compress-offload-playback1",
330 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
331 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
Dhananjay Kumar01e921a2013-11-26 23:33:22 +0530332 [USECASE_AUDIO_RECORD] = "audio-record",
333 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
334 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
335 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
336 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
337 [USECASE_VOICE_CALL] = "voice-call",
338
339 [USECASE_VOICE2_CALL] = "voice2-call",
340 [USECASE_VOLTE_CALL] = "volte-call",
341 [USECASE_QCHAT_CALL] = "qchat-call",
342 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
343 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
344 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
345 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
346 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
347 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
348 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
349 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
350};
351
Dhananjay Kumar09cbbf02013-11-25 15:49:57 +0530352
Dhananjay Kumar01e921a2013-11-26 23:33:22 +0530353int adev_open_output_stream(struct audio_hw_device *dev,
354 audio_io_handle_t handle,
355 audio_devices_t devices,
356 audio_output_flags_t flags,
357 struct audio_config *config,
358 struct audio_stream_out **stream_out);
359
360void adev_close_output_stream(struct audio_hw_device *dev,
361 struct audio_stream_out *stream);
362
Dhananjay Kumardaf6ebb2013-10-07 11:38:46 -0700363int select_devices(struct audio_device *adev,
364 audio_usecase_t uc_id);
365int disable_audio_route(struct audio_device *adev,
366 struct audio_usecase *usecase,
367 bool update_mixer);
368int disable_snd_device(struct audio_device *adev,
369 snd_device_t snd_device,
370 bool update_mixer);
371int enable_snd_device(struct audio_device *adev,
372 snd_device_t snd_device,
373 bool update_mixer);
374int enable_audio_route(struct audio_device *adev,
375 struct audio_usecase *usecase,
376 bool update_mixer);
377struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
378 audio_usecase_t uc_id);
379/*
380 * NOTE: when multiple mutexes have to be acquired, always take the
381 * stream_in or stream_out mutex first, followed by the audio_device mutex.
382 */
383
384#endif // QCOM_AUDIO_HW_H