blob: a96a509af78b71219a44a53beee326f857c5e3e6 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
vivek mehtaa51fd402016-02-04 19:49:33 -08002 * Copyright (C) 2013-2016 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_primary"
Haynes Mathew George03c40102016-01-29 17:57:48 -080018#define ATRACE_TAG ATRACE_TAG_AUDIO
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080019/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070020/*#define VERY_VERY_VERBOSE_LOGGING*/
21#ifdef VERY_VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080026
27#include <errno.h>
28#include <pthread.h>
29#include <stdint.h>
30#include <sys/time.h>
31#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080032#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070033#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070034#include <sys/resource.h>
35#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080036
37#include <cutils/log.h>
Haynes Mathew George03c40102016-01-29 17:57:48 -080038#include <cutils/trace.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080039#include <cutils/str_parms.h>
40#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070041#include <cutils/atomic.h>
42#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080043
Eric Laurentb23d5282013-05-14 15:27:20 -070044#include <hardware/audio_effect.h>
Andy Hung31aca912014-03-20 17:14:59 -070045#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -080050#include "audio_extn.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070051#include "platform_api.h"
52#include <platform.h>
Vineeta Srivastava4b89e372014-06-19 14:21:42 -070053#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include "sound/compress_params.h"
56
Eric Laurent397db572016-05-11 11:31:47 -070057/* COMPRESS_OFFLOAD_FRAGMENT_SIZE must be more than 8KB and a multiple of 32KB if more than 32KB.
58 * COMPRESS_OFFLOAD_FRAGMENT_SIZE * COMPRESS_OFFLOAD_NUM_FRAGMENTS must be less than 8MB. */
Marco Nelissen32093f52015-04-08 15:14:02 -070059#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024)
Marco Nelissen94c33a02015-05-12 09:11:34 -070060// 2 buffers causes problems with high bitrate files
61#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 3
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070062/* ToDo: Check and update a proper value in msec */
63#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
64#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
65
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -070066#define PROXY_OPEN_RETRY_COUNT 100
67#define PROXY_OPEN_WAIT_TIME 20
68
vivek mehtadae44712015-07-27 14:13:18 -070069#define MIN_CHANNEL_COUNT 1
70#define DEFAULT_CHANNEL_COUNT 2
71
Jean-Michel Trivic0750692015-10-12 12:12:32 -070072#ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT
73#define MAX_CHANNEL_COUNT 1
74#else
vivek mehtadae44712015-07-27 14:13:18 -070075#define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT))
76#define XSTR(x) STR(x)
77#define STR(x) #x
Jean-Michel Trivic0750692015-10-12 12:12:32 -070078#endif
vivek mehtadae44712015-07-27 14:13:18 -070079
Haynes Mathew George03c40102016-01-29 17:57:48 -080080#define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
81
Glenn Kasten4f993392014-05-14 07:30:48 -070082static unsigned int configured_low_latency_capture_period_size =
83 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
84
Andy Hung31aca912014-03-20 17:14:59 -070085/* This constant enables extended precision handling.
86 * TODO The flag is off until more testing is done.
87 */
88static const bool k_enable_extended_precision = false;
89
Eric Laurentb23d5282013-05-14 15:27:20 -070090struct pcm_config pcm_config_deep_buffer = {
vivek mehtadae44712015-07-27 14:13:18 -070091 .channels = DEFAULT_CHANNEL_COUNT,
Eric Laurentb23d5282013-05-14 15:27:20 -070092 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
93 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
94 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
95 .format = PCM_FORMAT_S16_LE,
96 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
97 .stop_threshold = INT_MAX,
98 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
99};
100
101struct pcm_config pcm_config_low_latency = {
vivek mehtadae44712015-07-27 14:13:18 -0700102 .channels = DEFAULT_CHANNEL_COUNT,
Eric Laurentb23d5282013-05-14 15:27:20 -0700103 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
104 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
105 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
106 .format = PCM_FORMAT_S16_LE,
107 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
108 .stop_threshold = INT_MAX,
109 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
110};
111
Haynes Mathew George03c40102016-01-29 17:57:48 -0800112static int af_period_multiplier = 4;
113struct pcm_config pcm_config_rt = {
114 .channels = DEFAULT_CHANNEL_COUNT,
115 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
116 .period_size = ULL_PERIOD_SIZE, //1 ms
117 .period_count = 512, //=> buffer size is 512ms
118 .format = PCM_FORMAT_S16_LE,
119 .start_threshold = ULL_PERIOD_SIZE*8, //8ms
120 .stop_threshold = INT_MAX,
121 .silence_threshold = 0,
122 .silence_size = 0,
123 .avail_min = ULL_PERIOD_SIZE, //1 ms
124};
125
Eric Laurentb23d5282013-05-14 15:27:20 -0700126struct pcm_config pcm_config_hdmi_multi = {
127 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
128 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
129 .period_size = HDMI_MULTI_PERIOD_SIZE,
130 .period_count = HDMI_MULTI_PERIOD_COUNT,
131 .format = PCM_FORMAT_S16_LE,
132 .start_threshold = 0,
133 .stop_threshold = INT_MAX,
134 .avail_min = 0,
135};
136
137struct pcm_config pcm_config_audio_capture = {
vivek mehtadae44712015-07-27 14:13:18 -0700138 .channels = DEFAULT_CHANNEL_COUNT,
Eric Laurentb23d5282013-05-14 15:27:20 -0700139 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
140 .format = PCM_FORMAT_S16_LE,
Eric Laurente2d2d1d2015-07-06 17:54:15 -0700141 .stop_threshold = INT_MAX,
142 .avail_min = 0,
Eric Laurentb23d5282013-05-14 15:27:20 -0700143};
144
Haynes Mathew George03c40102016-01-29 17:57:48 -0800145struct pcm_config pcm_config_audio_capture_rt = {
146 .channels = DEFAULT_CHANNEL_COUNT,
147 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
148 .period_size = ULL_PERIOD_SIZE,
149 .period_count = 512,
150 .format = PCM_FORMAT_S16_LE,
151 .start_threshold = 0,
152 .stop_threshold = INT_MAX,
153 .silence_threshold = 0,
154 .silence_size = 0,
155 .avail_min = ULL_PERIOD_SIZE, //1 ms
156};
157
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700158#define AFE_PROXY_CHANNEL_COUNT 2
159#define AFE_PROXY_SAMPLING_RATE 48000
160
161#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
162#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
163
164struct pcm_config pcm_config_afe_proxy_playback = {
165 .channels = AFE_PROXY_CHANNEL_COUNT,
166 .rate = AFE_PROXY_SAMPLING_RATE,
167 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
168 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
169 .format = PCM_FORMAT_S16_LE,
170 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
171 .stop_threshold = INT_MAX,
172 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
173};
174
175#define AFE_PROXY_RECORD_PERIOD_SIZE 768
176#define AFE_PROXY_RECORD_PERIOD_COUNT 4
177
178struct pcm_config pcm_config_afe_proxy_record = {
179 .channels = AFE_PROXY_CHANNEL_COUNT,
180 .rate = AFE_PROXY_SAMPLING_RATE,
181 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
182 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
183 .format = PCM_FORMAT_S16_LE,
184 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
185 .stop_threshold = INT_MAX,
186 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
187};
188
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700189const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700190 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
191 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
192 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700193 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Ravi Kumar Alamandaf78a4d92015-04-24 15:18:23 -0700194 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
Ravi Kumar Alamanda2bc7b022015-06-25 20:08:01 -0700195 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700196
Eric Laurentb23d5282013-05-14 15:27:20 -0700197 [USECASE_AUDIO_RECORD] = "audio-record",
198 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700199
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800200 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
201 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700202
Eric Laurentb23d5282013-05-14 15:27:20 -0700203 [USECASE_VOICE_CALL] = "voice-call",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700204 [USECASE_VOICE2_CALL] = "voice2-call",
205 [USECASE_VOLTE_CALL] = "volte-call",
206 [USECASE_QCHAT_CALL] = "qchat-call",
207 [USECASE_VOWLAN_CALL] = "vowlan-call",
vivek mehtaa51fd402016-02-04 19:49:33 -0800208 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
209 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700210
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700211 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
212 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
213
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700214 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
215 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700216};
217
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800218
219#define STRING_TO_ENUM(string) { #string, string }
220
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800221struct string_to_enum {
222 const char *name;
223 uint32_t value;
224};
225
226static const struct string_to_enum out_channels_name_to_enum_table[] = {
227 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
228 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
229 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
230};
231
Haynes Mathew George5191a852013-09-11 14:19:36 -0700232static int set_voice_volume_l(struct audio_device *adev, float volume);
vivek mehta1a9b7c02015-06-25 11:49:38 -0700233static struct audio_device *adev = NULL;
234static pthread_mutex_t adev_init_lock;
235static unsigned int audio_device_ref_count;
236
Haynes Mathew George03c40102016-01-29 17:57:48 -0800237static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
238 int flags __unused)
239{
240 int dir = 0;
241 switch (uc_id) {
242 case USECASE_AUDIO_RECORD_LOW_LATENCY:
243 dir = 1;
244 case USECASE_AUDIO_PLAYBACK_ULL:
245 break;
246 default:
247 return false;
248 }
249
250 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
251 PCM_PLAYBACK : PCM_CAPTURE);
252 if (adev->adm_is_noirq_avail)
253 return adev->adm_is_noirq_avail(adev->adm_data,
254 adev->snd_card, dev_id, dir);
255 return false;
256}
257
258static void register_out_stream(struct stream_out *out)
259{
260 struct audio_device *adev = out->dev;
261 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
262 return;
263
264 if (!adev->adm_register_output_stream)
265 return;
266
267 adev->adm_register_output_stream(adev->adm_data,
268 out->handle,
269 out->flags);
270
271 if (!adev->adm_set_config)
272 return;
273
274 if (out->realtime) {
275 adev->adm_set_config(adev->adm_data,
276 out->handle,
277 out->pcm, &out->config);
278 }
279}
280
281static void register_in_stream(struct stream_in *in)
282{
283 struct audio_device *adev = in->dev;
284 if (!adev->adm_register_input_stream)
285 return;
286
287 adev->adm_register_input_stream(adev->adm_data,
288 in->capture_handle,
289 in->flags);
290
291 if (!adev->adm_set_config)
292 return;
293
294 if (in->realtime) {
295 adev->adm_set_config(adev->adm_data,
296 in->capture_handle,
297 in->pcm,
298 &in->config);
299 }
300}
301
302static void request_out_focus(struct stream_out *out, long ns)
303{
304 struct audio_device *adev = out->dev;
305
306 if (out->routing_change) {
307 out->routing_change = false;
308 if (adev->adm_on_routing_change)
309 adev->adm_on_routing_change(adev->adm_data, out->handle);
310 }
311
312 if (adev->adm_request_focus_v2) {
313 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
314 } else if (adev->adm_request_focus) {
315 adev->adm_request_focus(adev->adm_data, out->handle);
316 }
317}
318
319static void request_in_focus(struct stream_in *in, long ns)
320{
321 struct audio_device *adev = in->dev;
322
323 if (in->routing_change) {
324 in->routing_change = false;
325 if (adev->adm_on_routing_change)
326 adev->adm_on_routing_change(adev->adm_data, in->capture_handle);
327 }
328
329 if (adev->adm_request_focus_v2) {
330 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
331 } else if (adev->adm_request_focus) {
332 adev->adm_request_focus(adev->adm_data, in->capture_handle);
333 }
334}
335
336static void release_out_focus(struct stream_out *out, long ns __unused)
337{
338 struct audio_device *adev = out->dev;
339
340 if (adev->adm_abandon_focus)
341 adev->adm_abandon_focus(adev->adm_data, out->handle);
342}
343
344static void release_in_focus(struct stream_in *in, long ns __unused)
345{
346 struct audio_device *adev = in->dev;
347 if (adev->adm_abandon_focus)
348 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
349}
350
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -0700351static int parse_snd_card_status(struct str_parms * parms, int * card,
352 card_status_t * status)
353{
354 char value[32]={0};
355 char state[32]={0};
356
357 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
358
359 if (ret < 0)
360 return -1;
361
362 // sscanf should be okay as value is of max length 32.
363 // same as sizeof state.
364 if (sscanf(value, "%d,%s", card, state) < 2)
365 return -1;
366
367 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
368 CARD_STATUS_OFFLINE;
369 return 0;
370}
371
vivek mehta1a9b7c02015-06-25 11:49:38 -0700372__attribute__ ((visibility ("default")))
373bool audio_hw_send_gain_dep_calibration(int level) {
374 bool ret_val = false;
375 ALOGV("%s: enter ... ", __func__);
376
377 pthread_mutex_lock(&adev_init_lock);
378
379 if (adev != NULL && adev->platform != NULL) {
380 pthread_mutex_lock(&adev->lock);
381 ret_val = platform_send_gain_dep_cal(adev->platform, level);
382 pthread_mutex_unlock(&adev->lock);
383 } else {
384 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
385 }
386
387 pthread_mutex_unlock(&adev_init_lock);
388
389 ALOGV("%s: exit with ret_val %d ", __func__, ret_val);
390 return ret_val;
391}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700392
vivek mehtaa8d7c922016-05-25 14:40:44 -0700393__attribute__ ((visibility ("default")))
394int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
395 int table_size) {
396 int ret_val = 0;
397 ALOGV("%s: enter ... ", __func__);
398
399 pthread_mutex_lock(&adev_init_lock);
400 if (adev == NULL) {
401 ALOGW("%s: adev is NULL .... ", __func__);
402 goto done;
403 }
404
405 pthread_mutex_lock(&adev->lock);
406 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
407 pthread_mutex_unlock(&adev->lock);
408done:
409 pthread_mutex_unlock(&adev_init_lock);
410 ALOGV("%s: exit ... ", __func__);
411 return ret_val;
412}
413
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700414static bool is_supported_format(audio_format_t format)
415{
Eric Laurent8251ac82014-07-23 11:00:25 -0700416 switch (format) {
417 case AUDIO_FORMAT_MP3:
418 case AUDIO_FORMAT_AAC_LC:
419 case AUDIO_FORMAT_AAC_HE_V1:
420 case AUDIO_FORMAT_AAC_HE_V2:
421 return true;
422 default:
423 break;
424 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700425 return false;
426}
427
Haynes Mathew George03c40102016-01-29 17:57:48 -0800428static inline bool is_mmap_usecase(audio_usecase_t uc_id)
429{
430 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
431 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
432}
433
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700434static int get_snd_codec_id(audio_format_t format)
435{
436 int id = 0;
437
Eric Laurent8251ac82014-07-23 11:00:25 -0700438 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700439 case AUDIO_FORMAT_MP3:
440 id = SND_AUDIOCODEC_MP3;
441 break;
442 case AUDIO_FORMAT_AAC:
443 id = SND_AUDIOCODEC_AAC;
444 break;
445 default:
446 ALOGE("%s: Unsupported audio format", __func__);
447 }
448
449 return id;
450}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800451
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800452int enable_audio_route(struct audio_device *adev,
453 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800454{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700455 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800456 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800457
458 if (usecase == NULL)
459 return -EINVAL;
460
461 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
462
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800463 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700464 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800465 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700466 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800467
468 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500469 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Eric Laurent2e140aa2016-06-30 17:14:46 -0700470 ALOGD("%s: usecase(%d) apply and update mixer path: %s", __func__, usecase->id, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700471 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800472
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800473 ALOGV("%s: exit", __func__);
474 return 0;
475}
476
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800477int disable_audio_route(struct audio_device *adev,
478 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800479{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700480 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800481 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800482
483 if (usecase == NULL)
484 return -EINVAL;
485
486 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700487 if (usecase->type == PCM_CAPTURE)
488 snd_device = usecase->in_snd_device;
489 else
490 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800491 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500492 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Eric Laurent2e140aa2016-06-30 17:14:46 -0700493 ALOGD("%s: usecase(%d) reset and update mixer path: %s", __func__, usecase->id, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700494 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800495
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800496 ALOGV("%s: exit", __func__);
497 return 0;
498}
499
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800500int enable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700501 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800502{
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700503 int i, num_devices = 0;
504 snd_device_t new_snd_devices[2];
vivek mehtade4849c2016-03-03 17:23:38 -0800505 int ret_val = -EINVAL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800506 if (snd_device < SND_DEVICE_MIN ||
507 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800508 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
vivek mehtade4849c2016-03-03 17:23:38 -0800509 goto on_error;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800510 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700511
Ravi Kumar Alamanda5a95ff62015-08-31 17:42:44 -0700512 platform_send_audio_calibration(adev->platform, snd_device);
513
vivek mehtade4849c2016-03-03 17:23:38 -0800514 if (adev->snd_dev_ref_cnt[snd_device] >= 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700515 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700516 __func__, snd_device, platform_get_snd_device_name(snd_device));
vivek mehtade4849c2016-03-03 17:23:38 -0800517 goto on_success;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700518 }
519
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700520 /* due to the possibility of calibration overwrite between listen
521 and audio, notify sound trigger hal before audio calibration is sent */
522 audio_extn_sound_trigger_update_device_status(snd_device,
523 ST_EVENT_SND_DEVICE_BUSY);
524
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700525 if (audio_extn_spkr_prot_is_enabled())
526 audio_extn_spkr_prot_calib_cancel(adev);
527
zhaoyang yin4211fad2015-06-04 21:13:25 +0800528 audio_extn_dsm_feedback_enable(adev, snd_device, true);
529
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700530 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
531 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
532 audio_extn_spkr_prot_is_enabled()) {
533 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
vivek mehtade4849c2016-03-03 17:23:38 -0800534 goto on_error;
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700535 }
536 if (audio_extn_spkr_prot_start_processing(snd_device)) {
537 ALOGE("%s: spkr_start_processing failed", __func__);
vivek mehtade4849c2016-03-03 17:23:38 -0800538 goto on_error;
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700539 }
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700540 } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
541 for (i = 0; i < num_devices; i++) {
542 enable_snd_device(adev, new_snd_devices[i]);
543 }
vivek mehtab6506412015-08-07 16:55:17 -0700544 platform_set_speaker_gain_in_combo(adev, snd_device, true);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700545 } else {
vivek mehtade4849c2016-03-03 17:23:38 -0800546 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
547 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
548 ALOGE(" %s: Invalid sound device returned", __func__);
549 goto on_error;
550 }
Ed Tam70b5c142016-03-21 19:14:29 -0700551
Eric Laurent2e140aa2016-06-30 17:14:46 -0700552 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
vivek mehtade4849c2016-03-03 17:23:38 -0800553 audio_route_apply_and_update_path(adev->audio_route, device_name);
554 }
555on_success:
556 adev->snd_dev_ref_cnt[snd_device]++;
557 ret_val = 0;
558on_error:
559 return ret_val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800560}
561
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800562int disable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700563 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800564{
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700565 int i, num_devices = 0;
566 snd_device_t new_snd_devices[2];
567
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800568 if (snd_device < SND_DEVICE_MIN ||
569 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800570 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800571 return -EINVAL;
572 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700573 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
574 ALOGE("%s: device ref cnt is already 0", __func__);
575 return -EINVAL;
576 }
577 adev->snd_dev_ref_cnt[snd_device]--;
578 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
zhaoyang yin4211fad2015-06-04 21:13:25 +0800579 audio_extn_dsm_feedback_enable(adev, snd_device, false);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700580 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
581 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
582 audio_extn_spkr_prot_is_enabled()) {
583 audio_extn_spkr_prot_stop_processing(snd_device);
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700584 } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
585 for (i = 0; i < num_devices; i++) {
586 disable_snd_device(adev, new_snd_devices[i]);
587 }
vivek mehtab6506412015-08-07 16:55:17 -0700588 platform_set_speaker_gain_in_combo(adev, snd_device, false);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700589 } else {
vivek mehtade4849c2016-03-03 17:23:38 -0800590 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
591 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
592 ALOGE(" %s: Invalid sound device returned", __func__);
593 return -EINVAL;
594 }
595
Eric Laurent2e140aa2016-06-30 17:14:46 -0700596 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
vivek mehtade4849c2016-03-03 17:23:38 -0800597 audio_route_reset_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700598 }
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700599 audio_extn_sound_trigger_update_device_status(snd_device,
600 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700601 }
vivek mehtab6506412015-08-07 16:55:17 -0700602
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800603 return 0;
604}
605
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700606static void check_and_route_playback_usecases(struct audio_device *adev,
607 struct audio_usecase *uc_info,
608 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700609{
610 struct listnode *node;
611 struct audio_usecase *usecase;
612 bool switch_device[AUDIO_USECASE_MAX];
613 int i, num_uc_to_switch = 0;
614
615 /*
616 * This function is to make sure that all the usecases that are active on
617 * the hardware codec backend are always routed to any one device that is
618 * handled by the hardware codec.
619 * For example, if low-latency and deep-buffer usecases are currently active
620 * on speaker and out_set_parameters(headset) is received on low-latency
621 * output, then we have to make sure deep-buffer is also switched to headset,
622 * because of the limitation that both the devices cannot be enabled
623 * at the same time as they share the same backend.
624 */
625 /* Disable all the usecases on the shared backend other than the
626 specified usecase */
627 for (i = 0; i < AUDIO_USECASE_MAX; i++)
628 switch_device[i] = false;
629
630 list_for_each(node, &adev->usecase_list) {
631 usecase = node_to_item(node, struct audio_usecase, list);
632 if (usecase->type != PCM_CAPTURE &&
633 usecase != uc_info &&
634 usecase->out_snd_device != snd_device &&
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700635 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND &&
636 platform_check_backends_match(snd_device, usecase->out_snd_device)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700637 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
638 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700639 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700640 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700641 switch_device[usecase->id] = true;
642 num_uc_to_switch++;
643 }
644 }
645
646 if (num_uc_to_switch) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700647 list_for_each(node, &adev->usecase_list) {
648 usecase = node_to_item(node, struct audio_usecase, list);
649 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700650 disable_snd_device(adev, usecase->out_snd_device);
sangwon.jeon866d5ff2013-10-17 21:42:50 +0900651 }
652 }
653
654 list_for_each(node, &adev->usecase_list) {
655 usecase = node_to_item(node, struct audio_usecase, list);
656 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700657 enable_snd_device(adev, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700658 }
659 }
660
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700661 /* Re-route all the usecases on the shared backend other than the
662 specified usecase to new snd devices */
663 list_for_each(node, &adev->usecase_list) {
664 usecase = node_to_item(node, struct audio_usecase, list);
665 /* Update the out_snd_device only before enabling the audio route */
666 if (switch_device[usecase->id] ) {
667 usecase->out_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700668 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700669 }
670 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700671 }
672}
673
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700674static void check_and_route_capture_usecases(struct audio_device *adev,
675 struct audio_usecase *uc_info,
676 snd_device_t snd_device)
677{
678 struct listnode *node;
679 struct audio_usecase *usecase;
680 bool switch_device[AUDIO_USECASE_MAX];
681 int i, num_uc_to_switch = 0;
682
vivek mehta4ed66e62016-04-15 23:33:34 -0700683 platform_check_and_set_capture_backend_cfg(adev, uc_info, snd_device);
684
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700685 /*
686 * This function is to make sure that all the active capture usecases
687 * are always routed to the same input sound device.
688 * For example, if audio-record and voice-call usecases are currently
689 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
690 * is received for voice call then we have to make sure that audio-record
691 * usecase is also switched to earpiece i.e. voice-dmic-ef,
692 * because of the limitation that two devices cannot be enabled
693 * at the same time if they share the same backend.
694 */
695 for (i = 0; i < AUDIO_USECASE_MAX; i++)
696 switch_device[i] = false;
697
698 list_for_each(node, &adev->usecase_list) {
699 usecase = node_to_item(node, struct audio_usecase, list);
700 if (usecase->type != PCM_PLAYBACK &&
701 usecase != uc_info &&
Anish Kumarff864712015-06-03 13:35:11 -0700702 usecase->in_snd_device != snd_device &&
703 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700704 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
705 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700706 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700707 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700708 switch_device[usecase->id] = true;
709 num_uc_to_switch++;
710 }
711 }
712
713 if (num_uc_to_switch) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700714 list_for_each(node, &adev->usecase_list) {
715 usecase = node_to_item(node, struct audio_usecase, list);
716 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700717 disable_snd_device(adev, usecase->in_snd_device);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700718 }
719 }
720
721 list_for_each(node, &adev->usecase_list) {
722 usecase = node_to_item(node, struct audio_usecase, list);
723 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700724 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700725 }
726 }
727
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700728 /* Re-route all the usecases on the shared backend other than the
729 specified usecase to new snd devices */
730 list_for_each(node, &adev->usecase_list) {
731 usecase = node_to_item(node, struct audio_usecase, list);
732 /* Update the in_snd_device only before enabling the audio route */
733 if (switch_device[usecase->id] ) {
734 usecase->in_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700735 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700736 }
737 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700738 }
739}
740
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800741/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700742static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800743{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700744 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700745 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800746
747 switch (channels) {
748 /*
749 * Do not handle stereo output in Multi-channel cases
750 * Stereo case is handled in normal playback path
751 */
752 case 6:
753 ALOGV("%s: HDMI supports 5.1", __func__);
754 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
755 break;
756 case 8:
757 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
758 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
759 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
760 break;
761 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700762 ALOGE("HDMI does not support multi channel playback");
763 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800764 break;
765 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700766 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800767}
768
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700769static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
770{
771 struct audio_usecase *usecase;
772 struct listnode *node;
773
774 list_for_each(node, &adev->usecase_list) {
775 usecase = node_to_item(node, struct audio_usecase, list);
776 if (usecase->type == VOICE_CALL) {
777 ALOGV("%s: usecase id %d", __func__, usecase->id);
778 return usecase->id;
779 }
780 }
781 return USECASE_INVALID;
782}
783
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800784struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
785 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700786{
787 struct audio_usecase *usecase;
788 struct listnode *node;
789
790 list_for_each(node, &adev->usecase_list) {
791 usecase = node_to_item(node, struct audio_usecase, list);
792 if (usecase->id == uc_id)
793 return usecase;
794 }
795 return NULL;
796}
797
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800798int select_devices(struct audio_device *adev,
799 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800800{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800801 snd_device_t out_snd_device = SND_DEVICE_NONE;
802 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700803 struct audio_usecase *usecase = NULL;
804 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800805 struct audio_usecase *hfp_usecase = NULL;
806 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800807 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700808 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800809
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700810 usecase = get_usecase_from_list(adev, uc_id);
811 if (usecase == NULL) {
812 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
813 return -EINVAL;
814 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800815
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800816 if ((usecase->type == VOICE_CALL) ||
817 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700818 out_snd_device = platform_get_output_snd_device(adev->platform,
819 usecase->stream.out->devices);
820 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700821 usecase->devices = usecase->stream.out->devices;
822 } else {
823 /*
824 * If the voice call is active, use the sound devices of voice call usecase
825 * so that it would not result any device switch. All the usecases will
826 * be switched to new device when select_devices() is called for voice call
827 * usecase. This is to avoid switching devices for voice call when
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700828 * check_and_route_playback_usecases() is called below.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700829 */
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700830 if (voice_is_in_call(adev)) {
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700831 vc_usecase = get_usecase_from_list(adev,
832 get_voice_usecase_id_from_list(adev));
833 if ((vc_usecase != NULL) &&
834 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
835 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700836 in_snd_device = vc_usecase->in_snd_device;
837 out_snd_device = vc_usecase->out_snd_device;
838 }
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800839 } else if (audio_extn_hfp_is_active(adev)) {
840 hfp_ucid = audio_extn_hfp_get_usecase();
841 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
842 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
843 in_snd_device = hfp_usecase->in_snd_device;
844 out_snd_device = hfp_usecase->out_snd_device;
845 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700846 }
847 if (usecase->type == PCM_PLAYBACK) {
848 usecase->devices = usecase->stream.out->devices;
849 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700850 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700851 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700852 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700853 if (usecase->stream.out == adev->primary_output &&
854 adev->active_input &&
Eric Laurent50a38ed2015-10-14 18:48:06 -0700855 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
856 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800857 out_snd_device != usecase->out_snd_device) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700858 select_devices(adev, adev->active_input->usecase);
859 }
860 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700861 } else if (usecase->type == PCM_CAPTURE) {
862 usecase->devices = usecase->stream.in->device;
863 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700864 if (in_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700865 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Eric Laurent50a38ed2015-10-14 18:48:06 -0700866 if (adev->active_input &&
867 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
868 adev->mode == AUDIO_MODE_IN_COMMUNICATION)) {
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800869 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda04643592015-09-24 19:17:26 -0700870 if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
871 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
872 } else if (adev->primary_output) {
873 out_device = adev->primary_output->devices;
874 }
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700875 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700876 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700877 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700878 }
879 }
880
881 if (out_snd_device == usecase->out_snd_device &&
882 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800883 return 0;
884 }
885
Eric Laurent2bafff12016-03-17 12:17:23 -0700886 if (out_snd_device != SND_DEVICE_NONE &&
887 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
888 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
889 __func__,
890 use_case_table[uc_id],
891 adev->last_logged_snd_device[uc_id][0],
892 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
893 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
894 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
895 -1,
896 out_snd_device,
897 platform_get_snd_device_name(out_snd_device),
898 platform_get_snd_device_acdb_id(out_snd_device));
899 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
900 }
901 if (in_snd_device != SND_DEVICE_NONE &&
902 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
903 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
904 __func__,
905 use_case_table[uc_id],
906 adev->last_logged_snd_device[uc_id][1],
907 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
908 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
909 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
910 -1,
911 in_snd_device,
912 platform_get_snd_device_name(in_snd_device),
913 platform_get_snd_device_acdb_id(in_snd_device));
914 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
915 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800916
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800917 /*
918 * Limitation: While in call, to do a device switch we need to disable
919 * and enable both RX and TX devices though one of them is same as current
920 * device.
921 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700922 if ((usecase->type == VOICE_CALL) &&
923 (usecase->in_snd_device != SND_DEVICE_NONE) &&
924 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700925 status = platform_switch_voice_call_device_pre(adev->platform);
vivek mehta765eb642015-08-07 19:46:06 -0700926 /* Disable sidetone only if voice call already exists */
927 if (voice_is_call_state_active(adev))
928 voice_set_sidetone(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800929 }
930
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700931 /* Disable current sound devices */
932 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700933 disable_audio_route(adev, usecase);
934 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800935 }
936
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700937 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700938 disable_audio_route(adev, usecase);
939 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800940 }
941
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700942 /* Applicable only on the targets that has external modem.
943 * New device information should be sent to modem before enabling
944 * the devices to reduce in-call device switch time.
945 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700946 if ((usecase->type == VOICE_CALL) &&
947 (usecase->in_snd_device != SND_DEVICE_NONE) &&
948 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700949 status = platform_switch_voice_call_enable_device_config(adev->platform,
950 out_snd_device,
951 in_snd_device);
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700952 }
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700953
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700954 /* Enable new sound devices */
955 if (out_snd_device != SND_DEVICE_NONE) {
956 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700957 check_and_route_playback_usecases(adev, usecase, out_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700958 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800959 }
960
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700961 if (in_snd_device != SND_DEVICE_NONE) {
962 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700963 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700964 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700965
Eric Laurentb23d5282013-05-14 15:27:20 -0700966 if (usecase->type == VOICE_CALL)
967 status = platform_switch_voice_call_device_post(adev->platform,
968 out_snd_device,
969 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800970
sangwoo170731f2013-06-08 15:36:36 +0900971 usecase->in_snd_device = in_snd_device;
972 usecase->out_snd_device = out_snd_device;
973
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700974 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900975
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700976 /* Applicable only on the targets that has external modem.
977 * Enable device command should be sent to modem only after
978 * enabling voice call mixer controls
979 */
vivek mehta765eb642015-08-07 19:46:06 -0700980 if (usecase->type == VOICE_CALL) {
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700981 status = platform_switch_voice_call_usecase_route_post(adev->platform,
982 out_snd_device,
983 in_snd_device);
vivek mehta765eb642015-08-07 19:46:06 -0700984 /* Enable sidetone only if voice call already exists */
985 if (voice_is_call_state_active(adev))
986 voice_set_sidetone(adev, out_snd_device, true);
987 }
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700988
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800989 return status;
990}
991
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800992static int stop_input_stream(struct stream_in *in)
993{
994 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800995 struct audio_usecase *uc_info;
996 struct audio_device *adev = in->dev;
997
Eric Laurentc8400632013-02-14 19:04:54 -0800998 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800999
Eric Laurent994a6932013-07-17 11:51:42 -07001000 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001001 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001002 uc_info = get_usecase_from_list(adev, in->usecase);
1003 if (uc_info == NULL) {
1004 ALOGE("%s: Could not find the usecase (%d) in the list",
1005 __func__, in->usecase);
1006 return -EINVAL;
1007 }
1008
Eric Laurent150dbfe2013-02-27 14:31:02 -08001009 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001010 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001011
1012 /* 2. Disable the tx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001013 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001014
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001015 list_remove(&uc_info->list);
1016 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001017
Eric Laurent994a6932013-07-17 11:51:42 -07001018 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001019 return ret;
1020}
1021
1022int start_input_stream(struct stream_in *in)
1023{
1024 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08001025 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026 struct audio_usecase *uc_info;
1027 struct audio_device *adev = in->dev;
1028
Eric Laurent994a6932013-07-17 11:51:42 -07001029 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07001030
1031 if (in->card_status == CARD_STATUS_OFFLINE ||
1032 adev->card_status == CARD_STATUS_OFFLINE) {
1033 ALOGW("in->card_status or adev->card_status offline, try again");
1034 ret = -EAGAIN;
1035 goto error_config;
1036 }
1037
Eric Laurentb23d5282013-05-14 15:27:20 -07001038 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001039 if (in->pcm_device_id < 0) {
1040 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
1041 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08001042 ret = -EINVAL;
1043 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001044 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001045
1046 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001047 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1048 uc_info->id = in->usecase;
1049 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001050 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001051 uc_info->devices = in->device;
1052 uc_info->in_snd_device = SND_DEVICE_NONE;
1053 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001054
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001055 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001056
1057 audio_extn_perf_lock_acquire();
1058
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001059 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001060
Eric Laurentc8400632013-02-14 19:04:54 -08001061 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001062 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001063
Andy Hung6ebe5962016-01-15 17:46:57 -08001064 unsigned int flags = PCM_IN | PCM_MONOTONIC;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001065 unsigned int pcm_open_retry_count = 0;
1066
1067 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1068 flags |= PCM_MMAP | PCM_NOIRQ;
1069 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George03c40102016-01-29 17:57:48 -08001070 } else if (in->realtime) {
1071 flags |= PCM_MMAP | PCM_NOIRQ;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001072 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001073
1074 while (1) {
1075 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
1076 flags, &in->config);
1077 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
1078 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
1079 if (in->pcm != NULL) {
1080 pcm_close(in->pcm);
1081 in->pcm = NULL;
1082 }
1083 if (pcm_open_retry_count-- == 0) {
1084 ret = -EIO;
1085 goto error_open;
1086 }
1087 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1088 continue;
1089 }
1090 break;
1091 }
1092
Ravi Kumar Alamanda50919a72015-10-02 09:37:33 -07001093 ALOGV("%s: pcm_prepare", __func__);
1094 ret = pcm_prepare(in->pcm);
1095 if (ret < 0) {
1096 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
1097 pcm_close(in->pcm);
1098 in->pcm = NULL;
1099 goto error_open;
1100 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08001101 if (in->realtime) {
1102 ret = pcm_start(in->pcm);
Haynes Mathew Georgefbe87312016-08-01 19:29:27 -07001103 if (ret < 0) {
1104 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
1105 pcm_close(in->pcm);
1106 in->pcm = NULL;
1107 goto error_open;
1108 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08001109 }
Haynes Mathew Georgefbe87312016-08-01 19:29:27 -07001110 register_in_stream(in);
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001111 audio_extn_perf_lock_release();
Eric Laurent994a6932013-07-17 11:51:42 -07001112 ALOGV("%s: exit", __func__);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07001113
Eric Laurentc8400632013-02-14 19:04:54 -08001114 return ret;
1115
1116error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001117 stop_input_stream(in);
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001118 audio_extn_perf_lock_release();
Eric Laurentc8400632013-02-14 19:04:54 -08001119
1120error_config:
1121 adev->active_input = NULL;
Eric Laurent2bafff12016-03-17 12:17:23 -07001122 ALOGW("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -08001123
1124 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001125}
1126
Eric Laurenta1478072015-09-21 17:21:52 -07001127void lock_input_stream(struct stream_in *in)
1128{
1129 pthread_mutex_lock(&in->pre_lock);
1130 pthread_mutex_lock(&in->lock);
1131 pthread_mutex_unlock(&in->pre_lock);
1132}
1133
1134void lock_output_stream(struct stream_out *out)
1135{
1136 pthread_mutex_lock(&out->pre_lock);
1137 pthread_mutex_lock(&out->lock);
1138 pthread_mutex_unlock(&out->pre_lock);
1139}
1140
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001141/* must be called with out->lock locked */
1142static int send_offload_cmd_l(struct stream_out* out, int command)
1143{
1144 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
1145
1146 ALOGVV("%s %d", __func__, command);
1147
1148 cmd->cmd = command;
1149 list_add_tail(&out->offload_cmd_list, &cmd->node);
1150 pthread_cond_signal(&out->offload_cond);
1151 return 0;
1152}
1153
1154/* must be called iwth out->lock locked */
1155static void stop_compressed_output_l(struct stream_out *out)
1156{
1157 out->offload_state = OFFLOAD_STATE_IDLE;
1158 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001159 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001160 if (out->compr != NULL) {
1161 compress_stop(out->compr);
1162 while (out->offload_thread_blocked) {
1163 pthread_cond_wait(&out->cond, &out->lock);
1164 }
1165 }
1166}
1167
1168static void *offload_thread_loop(void *context)
1169{
1170 struct stream_out *out = (struct stream_out *) context;
1171 struct listnode *item;
1172
1173 out->offload_state = OFFLOAD_STATE_IDLE;
1174 out->playback_started = 0;
1175
1176 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1177 set_sched_policy(0, SP_FOREGROUND);
1178 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1179
1180 ALOGV("%s", __func__);
Eric Laurenta1478072015-09-21 17:21:52 -07001181 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001182 for (;;) {
1183 struct offload_cmd *cmd = NULL;
1184 stream_callback_event_t event;
1185 bool send_callback = false;
1186
1187 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1188 __func__, list_empty(&out->offload_cmd_list),
1189 out->offload_state);
1190 if (list_empty(&out->offload_cmd_list)) {
1191 ALOGV("%s SLEEPING", __func__);
1192 pthread_cond_wait(&out->offload_cond, &out->lock);
1193 ALOGV("%s RUNNING", __func__);
1194 continue;
1195 }
1196
1197 item = list_head(&out->offload_cmd_list);
1198 cmd = node_to_item(item, struct offload_cmd, node);
1199 list_remove(item);
1200
1201 ALOGVV("%s STATE %d CMD %d out->compr %p",
1202 __func__, out->offload_state, cmd->cmd, out->compr);
1203
1204 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1205 free(cmd);
1206 break;
1207 }
1208
1209 if (out->compr == NULL) {
1210 ALOGE("%s: Compress handle is NULL", __func__);
Andy Hung68f55fd2016-04-21 11:51:11 -07001211 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001212 pthread_cond_signal(&out->cond);
1213 continue;
1214 }
1215 out->offload_thread_blocked = true;
1216 pthread_mutex_unlock(&out->lock);
1217 send_callback = false;
1218 switch(cmd->cmd) {
1219 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
1220 compress_wait(out->compr, -1);
1221 send_callback = true;
1222 event = STREAM_CBK_EVENT_WRITE_READY;
1223 break;
1224 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001225 compress_next_track(out->compr);
1226 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001227 send_callback = true;
1228 event = STREAM_CBK_EVENT_DRAIN_READY;
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -08001229 /* Resend the metadata for next iteration */
1230 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001231 break;
1232 case OFFLOAD_CMD_DRAIN:
1233 compress_drain(out->compr);
1234 send_callback = true;
1235 event = STREAM_CBK_EVENT_DRAIN_READY;
1236 break;
Haynes Mathew George3ddd3bd2016-07-07 20:01:53 -07001237 case OFFLOAD_CMD_ERROR:
1238 send_callback = true;
1239 event = STREAM_CBK_EVENT_ERROR;
1240 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001241 default:
1242 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1243 break;
1244 }
Eric Laurenta1478072015-09-21 17:21:52 -07001245 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001246 out->offload_thread_blocked = false;
1247 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001248 if (send_callback) {
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -08001249 ALOGVV("%s: sending offload_callback event %d", __func__, event);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001250 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001251 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001252 free(cmd);
1253 }
1254
1255 pthread_cond_signal(&out->cond);
1256 while (!list_empty(&out->offload_cmd_list)) {
1257 item = list_head(&out->offload_cmd_list);
1258 list_remove(item);
1259 free(node_to_item(item, struct offload_cmd, node));
1260 }
1261 pthread_mutex_unlock(&out->lock);
1262
1263 return NULL;
1264}
1265
1266static int create_offload_callback_thread(struct stream_out *out)
1267{
1268 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1269 list_init(&out->offload_cmd_list);
1270 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1271 offload_thread_loop, out);
1272 return 0;
1273}
1274
1275static int destroy_offload_callback_thread(struct stream_out *out)
1276{
Eric Laurenta1478072015-09-21 17:21:52 -07001277 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001278 stop_compressed_output_l(out);
1279 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1280
1281 pthread_mutex_unlock(&out->lock);
1282 pthread_join(out->offload_thread, (void **) NULL);
1283 pthread_cond_destroy(&out->offload_cond);
1284
1285 return 0;
1286}
1287
Eric Laurent07eeafd2013-10-06 12:52:49 -07001288static bool allow_hdmi_channel_config(struct audio_device *adev)
1289{
1290 struct listnode *node;
1291 struct audio_usecase *usecase;
1292 bool ret = true;
1293
1294 list_for_each(node, &adev->usecase_list) {
1295 usecase = node_to_item(node, struct audio_usecase, list);
1296 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1297 /*
1298 * If voice call is already existing, do not proceed further to avoid
1299 * disabling/enabling both RX and TX devices, CSD calls, etc.
1300 * Once the voice call done, the HDMI channels can be configured to
1301 * max channels of remaining use cases.
1302 */
1303 if (usecase->id == USECASE_VOICE_CALL) {
Joe Onorato188b6222016-03-01 11:02:27 -08001304 ALOGV("%s: voice call is active, no change in HDMI channels",
Eric Laurent07eeafd2013-10-06 12:52:49 -07001305 __func__);
1306 ret = false;
1307 break;
1308 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
Joe Onorato188b6222016-03-01 11:02:27 -08001309 ALOGV("%s: multi channel playback is active, "
Eric Laurent07eeafd2013-10-06 12:52:49 -07001310 "no change in HDMI channels", __func__);
1311 ret = false;
1312 break;
1313 }
1314 }
1315 }
1316 return ret;
1317}
1318
1319static int check_and_set_hdmi_channels(struct audio_device *adev,
1320 unsigned int channels)
1321{
1322 struct listnode *node;
1323 struct audio_usecase *usecase;
1324
1325 /* Check if change in HDMI channel config is allowed */
1326 if (!allow_hdmi_channel_config(adev))
1327 return 0;
1328
1329 if (channels == adev->cur_hdmi_channels) {
Joe Onorato188b6222016-03-01 11:02:27 -08001330 ALOGV("%s: Requested channels are same as current", __func__);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001331 return 0;
1332 }
1333
1334 platform_set_hdmi_channels(adev->platform, channels);
1335 adev->cur_hdmi_channels = channels;
1336
1337 /*
1338 * Deroute all the playback streams routed to HDMI so that
1339 * the back end is deactivated. Note that backend will not
1340 * be deactivated if any one stream is connected to it.
1341 */
1342 list_for_each(node, &adev->usecase_list) {
1343 usecase = node_to_item(node, struct audio_usecase, list);
1344 if (usecase->type == PCM_PLAYBACK &&
1345 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001346 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001347 }
1348 }
1349
1350 /*
1351 * Enable all the streams disabled above. Now the HDMI backend
1352 * will be activated with new channel configuration
1353 */
1354 list_for_each(node, &adev->usecase_list) {
1355 usecase = node_to_item(node, struct audio_usecase, list);
1356 if (usecase->type == PCM_PLAYBACK &&
1357 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001358 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001359 }
1360 }
1361
1362 return 0;
1363}
1364
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001365static int stop_output_stream(struct stream_out *out)
1366{
1367 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001368 struct audio_usecase *uc_info;
1369 struct audio_device *adev = out->dev;
1370
Eric Laurent994a6932013-07-17 11:51:42 -07001371 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001372 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001373 uc_info = get_usecase_from_list(adev, out->usecase);
1374 if (uc_info == NULL) {
1375 ALOGE("%s: Could not find the usecase (%d) in the list",
1376 __func__, out->usecase);
1377 return -EINVAL;
1378 }
1379
Haynes Mathew George41f86652014-06-17 14:22:15 -07001380 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1381 if (adev->visualizer_stop_output != NULL)
1382 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1383 if (adev->offload_effects_stop_output != NULL)
1384 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1385 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001386
Eric Laurent150dbfe2013-02-27 14:31:02 -08001387 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001388 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001389
1390 /* 2. Disable the rx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001391 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001392
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001393 list_remove(&uc_info->list);
1394 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001395
Eric Laurent0499d4f2014-08-25 22:39:29 -05001396 audio_extn_extspk_update(adev->extspk);
1397
Eric Laurent07eeafd2013-10-06 12:52:49 -07001398 /* Must be called after removing the usecase from list */
1399 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1400 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1401
Eric Laurent994a6932013-07-17 11:51:42 -07001402 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001403 return ret;
1404}
1405
1406int start_output_stream(struct stream_out *out)
1407{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001408 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409 struct audio_usecase *uc_info;
1410 struct audio_device *adev = out->dev;
1411
Eric Laurent994a6932013-07-17 11:51:42 -07001412 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001413 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07001414
1415 if (out->card_status == CARD_STATUS_OFFLINE ||
1416 adev->card_status == CARD_STATUS_OFFLINE) {
1417 ALOGW("out->card_status or adev->card_status offline, try again");
1418 ret = -EAGAIN;
1419 goto error_config;
1420 }
1421
Eric Laurentb23d5282013-05-14 15:27:20 -07001422 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001423 if (out->pcm_device_id < 0) {
1424 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1425 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001426 ret = -EINVAL;
1427 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001428 }
1429
1430 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1431 uc_info->id = out->usecase;
1432 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001433 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001434 uc_info->devices = out->devices;
1435 uc_info->in_snd_device = SND_DEVICE_NONE;
1436 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001437
Eric Laurent07eeafd2013-10-06 12:52:49 -07001438 /* This must be called before adding this usecase to the list */
1439 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1440 check_and_set_hdmi_channels(adev, out->config.channels);
1441
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001442 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001443
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001444 audio_extn_perf_lock_acquire();
1445
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001446 select_devices(adev, out->usecase);
1447
Eric Laurent0499d4f2014-08-25 22:39:29 -05001448 audio_extn_extspk_update(adev->extspk);
1449
Andy Hung31aca912014-03-20 17:14:59 -07001450 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001451 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001452 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001453 unsigned int flags = PCM_OUT;
1454 unsigned int pcm_open_retry_count = 0;
Haynes Mathew George03c40102016-01-29 17:57:48 -08001455
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001456 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1457 flags |= PCM_MMAP | PCM_NOIRQ;
1458 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George03c40102016-01-29 17:57:48 -08001459 } else if (out->realtime) {
1460 flags |= PCM_MMAP | PCM_NOIRQ;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001461 } else
1462 flags |= PCM_MONOTONIC;
1463
1464 while (1) {
1465 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1466 flags, &out->config);
1467 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
1468 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1469 if (out->pcm != NULL) {
1470 pcm_close(out->pcm);
1471 out->pcm = NULL;
1472 }
1473 if (pcm_open_retry_count-- == 0) {
1474 ret = -EIO;
1475 goto error_open;
1476 }
1477 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1478 continue;
1479 }
1480 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001481 }
Ravi Kumar Alamanda50919a72015-10-02 09:37:33 -07001482 ALOGV("%s: pcm_prepare", __func__);
1483 if (pcm_is_ready(out->pcm)) {
1484 ret = pcm_prepare(out->pcm);
1485 if (ret < 0) {
1486 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
1487 pcm_close(out->pcm);
1488 out->pcm = NULL;
1489 goto error_open;
1490 }
1491 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001492 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001493 out->pcm = NULL;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001494 out->compr = compress_open(adev->snd_card, out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001495 COMPRESS_IN, &out->compr_config);
1496 if (out->compr && !is_compress_ready(out->compr)) {
1497 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1498 compress_close(out->compr);
1499 out->compr = NULL;
1500 ret = -EIO;
1501 goto error_open;
1502 }
1503 if (out->offload_callback)
1504 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001505
1506 if (adev->visualizer_start_output != NULL)
Haynes Mathew George41f86652014-06-17 14:22:15 -07001507 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1508 if (adev->offload_effects_start_output != NULL)
1509 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001510 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08001511 ret = 0;
Haynes Mathew George03c40102016-01-29 17:57:48 -08001512 if (out->realtime) {
1513 ret = pcm_start(out->pcm);
Haynes Mathew Georgefbe87312016-08-01 19:29:27 -07001514 if (ret < 0) {
1515 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
1516 pcm_close(out->pcm);
1517 out->pcm = NULL;
1518 goto error_open;
1519 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08001520 }
Haynes Mathew Georgefbe87312016-08-01 19:29:27 -07001521 register_out_stream(out);
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001522 audio_extn_perf_lock_release();
Eric Laurent994a6932013-07-17 11:51:42 -07001523 ALOGV("%s: exit", __func__);
Haynes Mathew George03c40102016-01-29 17:57:48 -08001524 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001525error_open:
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001526 audio_extn_perf_lock_release();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001527 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001528error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001529 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001530}
1531
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001532static int check_input_parameters(uint32_t sample_rate,
1533 audio_format_t format,
1534 int channel_count)
1535{
vivek mehta4ed66e62016-04-15 23:33:34 -07001536 if ((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT)) {
vivek mehtadae44712015-07-27 14:13:18 -07001537 ALOGE("%s: unsupported AUDIO FORMAT (%d) ", __func__, format);
1538 return -EINVAL;
1539 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001540
vivek mehtadae44712015-07-27 14:13:18 -07001541 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > MAX_CHANNEL_COUNT)) {
Jean-Michel Trivic0750692015-10-12 12:12:32 -07001542 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
vivek mehtadae44712015-07-27 14:13:18 -07001543 channel_count, MIN_CHANNEL_COUNT, MAX_CHANNEL_COUNT);
1544 return -EINVAL;
1545 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001546
1547 switch (sample_rate) {
1548 case 8000:
1549 case 11025:
1550 case 12000:
1551 case 16000:
1552 case 22050:
1553 case 24000:
1554 case 32000:
1555 case 44100:
1556 case 48000:
1557 break;
1558 default:
vivek mehtadae44712015-07-27 14:13:18 -07001559 ALOGE("%s: unsupported (%d) samplerate passed ", __func__, sample_rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001560 return -EINVAL;
1561 }
1562
1563 return 0;
1564}
1565
1566static size_t get_input_buffer_size(uint32_t sample_rate,
1567 audio_format_t format,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001568 int channel_count,
1569 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001570{
1571 size_t size = 0;
1572
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001573 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1574 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001575
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001576 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001577 if (is_low_latency)
Glenn Kasten4f993392014-05-14 07:30:48 -07001578 size = configured_low_latency_capture_period_size;
vivek mehta4ed66e62016-04-15 23:33:34 -07001579
1580 size *= channel_count * audio_bytes_per_sample(format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001581
Glenn Kasten4f993392014-05-14 07:30:48 -07001582 /* make sure the size is multiple of 32 bytes
1583 * At 48 kHz mono 16-bit PCM:
1584 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1585 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1586 */
1587 size += 0x1f;
1588 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001589
1590 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001591}
1592
1593static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1594{
1595 struct stream_out *out = (struct stream_out *)stream;
1596
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001597 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001598}
1599
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001600static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001601{
1602 return -ENOSYS;
1603}
1604
1605static size_t out_get_buffer_size(const struct audio_stream *stream)
1606{
1607 struct stream_out *out = (struct stream_out *)stream;
1608
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001609 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1610 return out->compr_config.fragment_size;
1611 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08001612 return out->config.period_size * out->af_period_multiplier *
Eric Laurentfdf296a2014-07-03 16:41:51 -07001613 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001614}
1615
1616static uint32_t out_get_channels(const struct audio_stream *stream)
1617{
1618 struct stream_out *out = (struct stream_out *)stream;
1619
1620 return out->channel_mask;
1621}
1622
1623static audio_format_t out_get_format(const struct audio_stream *stream)
1624{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001625 struct stream_out *out = (struct stream_out *)stream;
1626
1627 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001628}
1629
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001630static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001631{
1632 return -ENOSYS;
1633}
1634
1635static int out_standby(struct audio_stream *stream)
1636{
1637 struct stream_out *out = (struct stream_out *)stream;
1638 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001639
Eric Laurent994a6932013-07-17 11:51:42 -07001640 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001641 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001642
Eric Laurenta1478072015-09-21 17:21:52 -07001643 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001644 if (!out->standby) {
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07001645 if (adev->adm_deregister_stream)
1646 adev->adm_deregister_stream(adev->adm_data, out->handle);
1647
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001648 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001649 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001650 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1651 if (out->pcm) {
1652 pcm_close(out->pcm);
1653 out->pcm = NULL;
1654 }
1655 } else {
1656 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001657 out->gapless_mdata.encoder_delay = 0;
1658 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001659 if (out->compr != NULL) {
1660 compress_close(out->compr);
1661 out->compr = NULL;
1662 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001663 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001664 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001665 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001666 }
1667 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001668 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001669 return 0;
1670}
1671
Haynes Mathew George3ddd3bd2016-07-07 20:01:53 -07001672static int out_on_error(struct audio_stream *stream)
1673{
1674 struct stream_out *out = (struct stream_out *)stream;
1675 struct audio_device *adev = out->dev;
1676 bool do_standby = false;
1677
1678 lock_output_stream(out);
1679 if (!out->standby) {
1680 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1681 stop_compressed_output_l(out);
1682 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
1683 } else
1684 do_standby = true;
1685 }
1686 pthread_mutex_unlock(&out->lock);
1687
1688 if (do_standby)
1689 return out_standby(&out->stream.common);
1690
1691 return 0;
1692}
1693
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001694static int out_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001695{
1696 return 0;
1697}
1698
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001699static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1700{
1701 int ret = 0;
1702 char value[32];
1703 struct compr_gapless_mdata tmp_mdata;
1704
1705 if (!out || !parms) {
1706 return -EINVAL;
1707 }
1708
1709 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1710 if (ret >= 0) {
1711 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1712 } else {
1713 return -EINVAL;
1714 }
1715
1716 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1717 if (ret >= 0) {
1718 tmp_mdata.encoder_padding = atoi(value);
1719 } else {
1720 return -EINVAL;
1721 }
1722
1723 out->gapless_mdata = tmp_mdata;
1724 out->send_new_metadata = 1;
1725 ALOGV("%s new encoder delay %u and padding %u", __func__,
1726 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1727
1728 return 0;
1729}
1730
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001731static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1732{
1733 return out == adev->primary_output || out == adev->voice_tx_output;
1734}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001735
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001736static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1737{
1738 struct stream_out *out = (struct stream_out *)stream;
1739 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001740 struct audio_usecase *usecase;
1741 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001742 struct str_parms *parms;
1743 char value[32];
1744 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001745 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001746 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001747
Eric Laurent2e140aa2016-06-30 17:14:46 -07001748 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001749 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001750 parms = str_parms_create_str(kvpairs);
1751 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1752 if (ret >= 0) {
1753 val = atoi(value);
Eric Laurenta1478072015-09-21 17:21:52 -07001754 lock_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001755 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001756
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001757 /*
1758 * When HDMI cable is unplugged the music playback is paused and
1759 * the policy manager sends routing=0. But the audioflinger
1760 * continues to write data until standby time (3sec).
1761 * As the HDMI core is turned off, the write gets blocked.
1762 * Avoid this by routing audio to speaker until standby.
1763 */
1764 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1765 val == AUDIO_DEVICE_NONE) {
1766 val = AUDIO_DEVICE_OUT_SPEAKER;
1767 }
1768
1769 /*
1770 * select_devices() call below switches all the usecases on the same
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -07001771 * backend to the new device. Refer to check_and_route_playback_usecases() in
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001772 * the select_devices(). But how do we undo this?
1773 *
1774 * For example, music playback is active on headset (deep-buffer usecase)
1775 * and if we go to ringtones and select a ringtone, low-latency usecase
1776 * will be started on headset+speaker. As we can't enable headset+speaker
1777 * and headset devices at the same time, select_devices() switches the music
1778 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1779 * So when the ringtone playback is completed, how do we undo the same?
1780 *
1781 * We are relying on the out_set_parameters() call on deep-buffer output,
1782 * once the ringtone playback is ended.
1783 * NOTE: We should not check if the current devices are same as new devices.
1784 * Because select_devices() must be called to switch back the music
1785 * playback to headset.
1786 */
Haynes Mathew George03c40102016-01-29 17:57:48 -08001787 audio_devices_t new_dev = val;
1788 if (new_dev != AUDIO_DEVICE_NONE) {
1789 bool same_dev = out->devices == new_dev;
1790 out->devices = new_dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001791
Eric Laurenta7657192014-10-09 21:09:33 -07001792 if (output_drives_call(adev, out)) {
1793 if (!voice_is_in_call(adev)) {
1794 if (adev->mode == AUDIO_MODE_IN_CALL) {
1795 adev->current_call_output = out;
1796 ret = voice_start_call(adev);
1797 }
1798 } else {
1799 adev->current_call_output = out;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001800 voice_update_devices_for_all_voice_usecases(adev);
Eric Laurenta7657192014-10-09 21:09:33 -07001801 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001802 }
vivek mehta0d3637a2016-07-24 09:32:02 -07001803
1804 if (!out->standby) {
1805 if (!same_dev) {
1806 ALOGV("update routing change");
1807 out->routing_change = true;
1808 }
1809 select_devices(adev, out->usecase);
1810 }
1811
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001812 }
1813
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001814 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001815 pthread_mutex_unlock(&out->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05001816
1817 /*handles device and call state changes*/
1818 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001819 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001820
1821 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1822 parse_compress_metadata(out, parms);
1823 }
1824
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001825 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001826 ALOGV("%s: exit: code(%d)", __func__, status);
1827 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001828}
1829
1830static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1831{
1832 struct stream_out *out = (struct stream_out *)stream;
1833 struct str_parms *query = str_parms_create_str(keys);
1834 char *str;
1835 char value[256];
1836 struct str_parms *reply = str_parms_create();
1837 size_t i, j;
1838 int ret;
1839 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001840 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001841 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1842 if (ret >= 0) {
1843 value[0] = '\0';
1844 i = 0;
1845 while (out->supported_channel_masks[i] != 0) {
1846 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1847 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1848 if (!first) {
1849 strcat(value, "|");
1850 }
1851 strcat(value, out_channels_name_to_enum_table[j].name);
1852 first = false;
1853 break;
1854 }
1855 }
1856 i++;
1857 }
1858 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1859 str = str_parms_to_str(reply);
1860 } else {
1861 str = strdup(keys);
1862 }
1863 str_parms_destroy(query);
1864 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001865 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001866 return str;
1867}
1868
1869static uint32_t out_get_latency(const struct audio_stream_out *stream)
1870{
Haynes Mathew George03c40102016-01-29 17:57:48 -08001871 uint32_t hw_delay, period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001872 struct stream_out *out = (struct stream_out *)stream;
1873
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001874 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1875 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
Haynes Mathew George03c40102016-01-29 17:57:48 -08001876 else if (out->realtime) {
1877 // since the buffer won't be filled up faster than realtime,
1878 // return a smaller number
1879 period_ms = (out->af_period_multiplier * out->config.period_size *
1880 1000) / (out->config.rate);
1881 hw_delay = platform_render_latency(out->usecase)/1000;
1882 return period_ms + hw_delay;
1883 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001884
1885 return (out->config.period_count * out->config.period_size * 1000) /
1886 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001887}
1888
1889static int out_set_volume(struct audio_stream_out *stream, float left,
1890 float right)
1891{
Eric Laurenta9024de2013-04-04 09:19:12 -07001892 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001893 int volume[2];
1894
Eric Laurenta9024de2013-04-04 09:19:12 -07001895 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1896 /* only take left channel into account: the API is for stereo anyway */
1897 out->muted = (left == 0.0f);
1898 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001899 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1900 const char *mixer_ctl_name = "Compress Playback Volume";
1901 struct audio_device *adev = out->dev;
1902 struct mixer_ctl *ctl;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001903 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1904 if (!ctl) {
Haynes Mathew George20bcfa82014-06-25 13:37:03 -07001905 /* try with the control based on device id */
1906 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1907 PCM_PLAYBACK);
1908 char ctl_name[128] = {0};
1909 snprintf(ctl_name, sizeof(ctl_name),
1910 "Compress Playback %d Volume", pcm_device_id);
1911 ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name);
1912 if (!ctl) {
1913 ALOGE("%s: Could not get volume ctl mixer cmd", __func__);
1914 return -EINVAL;
1915 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001916 }
1917 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1918 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1919 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1920 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001921 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001922
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001923 return -ENOSYS;
1924}
1925
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07001926// note: this call is safe only if the stream_cb is
1927// removed first in close_output_stream (as is done now).
1928static void out_snd_mon_cb(void * stream, struct str_parms * parms)
1929{
1930 if (!stream || !parms)
1931 return;
1932
1933 struct stream_out *out = (struct stream_out *)stream;
1934 struct audio_device *adev = out->dev;
1935
1936 card_status_t status;
1937 int card;
1938 if (parse_snd_card_status(parms, &card, &status) < 0)
1939 return;
1940
1941 pthread_mutex_lock(&adev->lock);
1942 bool valid_cb = (card == adev->snd_card);
1943 pthread_mutex_unlock(&adev->lock);
1944
1945 if (!valid_cb)
1946 return;
1947
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07001948 lock_output_stream(out);
1949 if (out->card_status != status)
1950 out->card_status = status;
1951 pthread_mutex_unlock(&out->lock);
1952
Haynes Mathew George3ddd3bd2016-07-07 20:01:53 -07001953 ALOGW("out_snd_mon_cb for card %d usecase %s, status %s", card,
1954 use_case_table[out->usecase],
1955 status == CARD_STATUS_OFFLINE ? "offline" : "online");
1956
1957 if (status == CARD_STATUS_OFFLINE)
1958 out_on_error(stream);
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07001959
1960 return;
1961}
1962
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001963#ifdef NO_AUDIO_OUT
1964static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
1965 const void *buffer, size_t bytes)
1966{
1967 struct stream_out *out = (struct stream_out *)stream;
1968
1969 /* No Output device supported other than BT for playback.
1970 * Sleep for the amount of buffer duration
1971 */
Eric Laurenta1478072015-09-21 17:21:52 -07001972 lock_output_stream(out);
Uday Kishore Pasupuletib14d76b2015-11-13 20:06:37 -08001973 usleep(bytes * 1000000 / audio_stream_out_frame_size(&out->stream.common) /
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001974 out_get_sample_rate(&out->stream.common));
1975 pthread_mutex_unlock(&out->lock);
1976 return bytes;
1977}
1978#endif
1979
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001980static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1981 size_t bytes)
1982{
1983 struct stream_out *out = (struct stream_out *)stream;
1984 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001985 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001986
Eric Laurenta1478072015-09-21 17:21:52 -07001987 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001988 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001989 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001990 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001991 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001992 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001993 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001994 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001995 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001996 goto exit;
1997 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001998 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001999
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002000 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002001 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
2002 if (out->send_new_metadata) {
2003 ALOGVV("send new gapless metadata");
2004 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
2005 out->send_new_metadata = 0;
2006 }
Eric Laurentb49b3f62016-02-29 17:59:49 -08002007 unsigned int avail;
2008 struct timespec tstamp;
2009 ret = compress_get_hpointer(out->compr, &avail, &tstamp);
2010 /* Do not limit write size if the available frames count is unknown */
2011 if (ret != 0) {
2012 avail = bytes;
2013 }
2014 if (avail == 0) {
2015 ret = 0;
2016 } else {
2017 if (avail > bytes) {
2018 avail = bytes;
2019 }
2020 ret = compress_write(out->compr, buffer, avail);
2021 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %zd",
2022 __func__, avail, ret);
2023 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002024
Eric Laurent6e895242013-09-05 16:10:57 -07002025 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002026 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
2027 }
Eric Laurentb49b3f62016-02-29 17:59:49 -08002028 if (ret > 0 && !out->playback_started) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002029 compress_start(out->compr);
2030 out->playback_started = 1;
2031 out->offload_state = OFFLOAD_STATE_PLAYING;
2032 }
2033 pthread_mutex_unlock(&out->lock);
2034 return ret;
2035 } else {
2036 if (out->pcm) {
2037 if (out->muted)
2038 memset((void *)buffer, 0, bytes);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002039
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002040 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002041
Haynes Mathew George03c40102016-01-29 17:57:48 -08002042 long ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
2043 out->config.rate;
2044 request_out_focus(out, ns);
2045
2046 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
2047 if (use_mmap)
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002048 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002049 else
2050 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002051
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002052 if (ret == 0)
2053 out->written += bytes / (out->config.channels * sizeof(short));
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002054
Haynes Mathew George03c40102016-01-29 17:57:48 -08002055 release_out_focus(out, ns);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002056 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002057 }
2058
2059exit:
2060 pthread_mutex_unlock(&out->lock);
2061
2062 if (ret != 0) {
Haynes Mathew George3ddd3bd2016-07-07 20:01:53 -07002063 out_on_error(&out->stream.common);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002064 if (out->pcm)
vivek mehta1a9b7c02015-06-25 11:49:38 -07002065 ALOGE("%s: error %zu - %s", __func__, ret, pcm_get_error(out->pcm));
Haynes Mathew George3ddd3bd2016-07-07 20:01:53 -07002066 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD)
2067 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
2068 out_get_sample_rate(&out->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002069 }
2070 return bytes;
2071}
2072
2073static int out_get_render_position(const struct audio_stream_out *stream,
2074 uint32_t *dsp_frames)
2075{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002076 struct stream_out *out = (struct stream_out *)stream;
2077 *dsp_frames = 0;
2078 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
Eric Laurenta1478072015-09-21 17:21:52 -07002079 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002080 if (out->compr != NULL) {
Andy Hung7171da22016-03-08 16:58:42 -08002081 unsigned long frames = 0;
2082 // TODO: check return value
2083 compress_get_tstamp(out->compr, &frames, &out->sample_rate);
2084 *dsp_frames = (uint32_t)frames;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002085 ALOGVV("%s rendered frames %d sample_rate %d",
2086 __func__, *dsp_frames, out->sample_rate);
2087 }
2088 pthread_mutex_unlock(&out->lock);
2089 return 0;
2090 } else
2091 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002092}
2093
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002094static int out_add_audio_effect(const struct audio_stream *stream __unused,
2095 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002096{
2097 return 0;
2098}
2099
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002100static int out_remove_audio_effect(const struct audio_stream *stream __unused,
2101 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002102{
2103 return 0;
2104}
2105
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002106static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
2107 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002108{
2109 return -EINVAL;
2110}
2111
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002112static int out_get_presentation_position(const struct audio_stream_out *stream,
2113 uint64_t *frames, struct timespec *timestamp)
2114{
2115 struct stream_out *out = (struct stream_out *)stream;
Andy Hung7171da22016-03-08 16:58:42 -08002116 int ret = -EINVAL;
Eric Laurent949a0892013-09-20 09:20:13 -07002117 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002118
Eric Laurenta1478072015-09-21 17:21:52 -07002119 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002120
Eric Laurent949a0892013-09-20 09:20:13 -07002121 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2122 if (out->compr != NULL) {
Andy Hung7171da22016-03-08 16:58:42 -08002123 // TODO: check return value
Eric Laurent949a0892013-09-20 09:20:13 -07002124 compress_get_tstamp(out->compr, &dsp_frames,
2125 &out->sample_rate);
2126 ALOGVV("%s rendered frames %ld sample_rate %d",
2127 __func__, dsp_frames, out->sample_rate);
2128 *frames = dsp_frames;
2129 ret = 0;
2130 /* this is the best we can do */
2131 clock_gettime(CLOCK_MONOTONIC, timestamp);
2132 }
2133 } else {
2134 if (out->pcm) {
vivek mehta1a9b7c02015-06-25 11:49:38 -07002135 unsigned int avail;
Eric Laurent949a0892013-09-20 09:20:13 -07002136 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
2137 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07002138 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07002139 // This adjustment accounts for buffering after app processor.
2140 // It is based on estimated DSP latency per use case, rather than exact.
2141 signed_frames -=
2142 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
2143
Eric Laurent949a0892013-09-20 09:20:13 -07002144 // It would be unusual for this value to be negative, but check just in case ...
2145 if (signed_frames >= 0) {
2146 *frames = signed_frames;
2147 ret = 0;
2148 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002149 }
2150 }
2151 }
2152
2153 pthread_mutex_unlock(&out->lock);
2154
2155 return ret;
2156}
2157
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002158static int out_set_callback(struct audio_stream_out *stream,
2159 stream_callback_t callback, void *cookie)
2160{
2161 struct stream_out *out = (struct stream_out *)stream;
2162
2163 ALOGV("%s", __func__);
Eric Laurenta1478072015-09-21 17:21:52 -07002164 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002165 out->offload_callback = callback;
2166 out->offload_cookie = cookie;
2167 pthread_mutex_unlock(&out->lock);
2168 return 0;
2169}
2170
2171static int out_pause(struct audio_stream_out* stream)
2172{
2173 struct stream_out *out = (struct stream_out *)stream;
2174 int status = -ENOSYS;
2175 ALOGV("%s", __func__);
2176 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurenta1478072015-09-21 17:21:52 -07002177 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002178 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
2179 status = compress_pause(out->compr);
2180 out->offload_state = OFFLOAD_STATE_PAUSED;
2181 }
2182 pthread_mutex_unlock(&out->lock);
2183 }
2184 return status;
2185}
2186
2187static int out_resume(struct audio_stream_out* stream)
2188{
2189 struct stream_out *out = (struct stream_out *)stream;
2190 int status = -ENOSYS;
2191 ALOGV("%s", __func__);
2192 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2193 status = 0;
Eric Laurenta1478072015-09-21 17:21:52 -07002194 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002195 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
2196 status = compress_resume(out->compr);
2197 out->offload_state = OFFLOAD_STATE_PLAYING;
2198 }
2199 pthread_mutex_unlock(&out->lock);
2200 }
2201 return status;
2202}
2203
2204static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2205{
2206 struct stream_out *out = (struct stream_out *)stream;
2207 int status = -ENOSYS;
2208 ALOGV("%s", __func__);
2209 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurenta1478072015-09-21 17:21:52 -07002210 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002211 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2212 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2213 else
2214 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2215 pthread_mutex_unlock(&out->lock);
2216 }
2217 return status;
2218}
2219
2220static int out_flush(struct audio_stream_out* stream)
2221{
2222 struct stream_out *out = (struct stream_out *)stream;
2223 ALOGV("%s", __func__);
2224 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurenta1478072015-09-21 17:21:52 -07002225 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002226 stop_compressed_output_l(out);
2227 pthread_mutex_unlock(&out->lock);
2228 return 0;
2229 }
2230 return -ENOSYS;
2231}
2232
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002233/** audio_stream_in implementation **/
2234static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2235{
2236 struct stream_in *in = (struct stream_in *)stream;
2237
2238 return in->config.rate;
2239}
2240
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002241static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002242{
2243 return -ENOSYS;
2244}
2245
2246static size_t in_get_buffer_size(const struct audio_stream *stream)
2247{
2248 struct stream_in *in = (struct stream_in *)stream;
2249
Haynes Mathew George03c40102016-01-29 17:57:48 -08002250 return in->config.period_size * in->af_period_multiplier *
2251 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002252}
2253
2254static uint32_t in_get_channels(const struct audio_stream *stream)
2255{
2256 struct stream_in *in = (struct stream_in *)stream;
2257
2258 return in->channel_mask;
2259}
2260
vivek mehta4ed66e62016-04-15 23:33:34 -07002261static audio_format_t in_get_format(const struct audio_stream *stream)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002262{
vivek mehta4ed66e62016-04-15 23:33:34 -07002263 struct stream_in *in = (struct stream_in *)stream;
2264 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002265}
2266
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002267static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002268{
2269 return -ENOSYS;
2270}
2271
2272static int in_standby(struct audio_stream *stream)
2273{
2274 struct stream_in *in = (struct stream_in *)stream;
2275 struct audio_device *adev = in->dev;
2276 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07002277 ALOGV("%s: enter", __func__);
Eric Laurenta1478072015-09-21 17:21:52 -07002278
2279 lock_input_stream(in);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002280
2281 if (!in->standby && in->is_st_session) {
Joe Onorato188b6222016-03-01 11:02:27 -08002282 ALOGV("%s: sound trigger pcm stop lab", __func__);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002283 audio_extn_sound_trigger_stop_lab(in);
2284 in->standby = true;
2285 }
2286
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002287 if (!in->standby) {
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002288 if (adev->adm_deregister_stream)
2289 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
2290
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002291 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002292 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002293 if (in->pcm) {
2294 pcm_close(in->pcm);
2295 in->pcm = NULL;
2296 }
Eric Laurentcefbbac2014-09-04 13:54:10 -05002297 adev->enable_voicerx = false;
2298 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE );
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002299 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002300 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002301 }
2302 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002303 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002304 return status;
2305}
2306
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002307static int in_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002308{
2309 return 0;
2310}
2311
2312static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2313{
2314 struct stream_in *in = (struct stream_in *)stream;
2315 struct audio_device *adev = in->dev;
2316 struct str_parms *parms;
2317 char *str;
2318 char value[32];
2319 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07002320 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002321
Eric Laurent994a6932013-07-17 11:51:42 -07002322 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002323 parms = str_parms_create_str(kvpairs);
2324
2325 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2326
Eric Laurenta1478072015-09-21 17:21:52 -07002327 lock_input_stream(in);
2328
Eric Laurent150dbfe2013-02-27 14:31:02 -08002329 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002330 if (ret >= 0) {
2331 val = atoi(value);
2332 /* no audio source uses val == 0 */
2333 if ((in->source != val) && (val != 0)) {
2334 in->source = val;
2335 }
2336 }
2337
2338 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07002339
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002340 if (ret >= 0) {
2341 val = atoi(value);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002342 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002343 in->device = val;
2344 /* If recording is in progress, change the tx device to new device */
Haynes Mathew George03c40102016-01-29 17:57:48 -08002345 if (!in->standby) {
2346 ALOGV("update input routing change");
2347 in->routing_change = true;
2348 select_devices(adev, in->usecase);
2349 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002350 }
2351 }
2352
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002353 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002354 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002355
2356 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07002357 ALOGV("%s: exit: status(%d)", __func__, status);
2358 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002359}
2360
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002361static char* in_get_parameters(const struct audio_stream *stream __unused,
2362 const char *keys __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002363{
2364 return strdup("");
2365}
2366
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002367static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002368{
2369 return 0;
2370}
2371
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07002372static void in_snd_mon_cb(void * stream, struct str_parms * parms)
2373{
2374 if (!stream || !parms)
2375 return;
2376
2377 struct stream_in *in = (struct stream_in *)stream;
2378 struct audio_device *adev = in->dev;
2379
2380 card_status_t status;
2381 int card;
2382 if (parse_snd_card_status(parms, &card, &status) < 0)
2383 return;
2384
2385 pthread_mutex_lock(&adev->lock);
2386 bool valid_cb = (card == adev->snd_card);
2387 pthread_mutex_unlock(&adev->lock);
2388
2389 if (!valid_cb)
2390 return;
2391
2392 lock_input_stream(in);
2393 if (in->card_status != status)
2394 in->card_status = status;
2395 pthread_mutex_unlock(&in->lock);
2396
2397 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
2398 use_case_table[in->usecase],
2399 status == CARD_STATUS_OFFLINE ? "offline" : "online");
2400
2401 // a better solution would be to report error back to AF and let
2402 // it put the stream to standby
2403 if (status == CARD_STATUS_OFFLINE)
2404 in_standby(&in->stream.common);
2405
2406 return;
2407}
2408
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002409static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2410 size_t bytes)
2411{
2412 struct stream_in *in = (struct stream_in *)stream;
2413 struct audio_device *adev = in->dev;
2414 int i, ret = -1;
vivek mehta4ed66e62016-04-15 23:33:34 -07002415 int *int_buf_stream = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002416
Eric Laurenta1478072015-09-21 17:21:52 -07002417 lock_input_stream(in);
2418
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002419 if (in->is_st_session) {
2420 ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
2421 /* Read from sound trigger HAL */
2422 audio_extn_sound_trigger_read(in, buffer, bytes);
2423 pthread_mutex_unlock(&in->lock);
2424 return bytes;
2425 }
2426
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002427 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002428 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002429 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002430 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002431 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002432 goto exit;
2433 }
2434 in->standby = 0;
2435 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002436
Haynes Mathew George03c40102016-01-29 17:57:48 -08002437 //what's the duration requested by the client?
2438 long ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
2439 in->config.rate;
2440 request_in_focus(in, ns);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002441
Haynes Mathew George03c40102016-01-29 17:57:48 -08002442 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002443 if (in->pcm) {
Haynes Mathew George03c40102016-01-29 17:57:48 -08002444 if (use_mmap) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002445 ret = pcm_mmap_read(in->pcm, buffer, bytes);
vivek mehta4ed66e62016-04-15 23:33:34 -07002446 } else {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002447 ret = pcm_read(in->pcm, buffer, bytes);
Eric Laurentf8b50aa2016-05-06 11:03:53 -07002448 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08002449 if (ret < 0) {
2450 ALOGE("Failed to read w/err %s", strerror(errno));
2451 ret = -errno;
2452 }
Eric Laurentf8b50aa2016-05-06 11:03:53 -07002453 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
2454 if (bytes % 4 == 0) {
2455 /* data from DSP comes in 24_8 format, convert it to 8_24 */
2456 int_buf_stream = buffer;
2457 for (size_t itt=0; itt < bytes/4 ; itt++) {
2458 int_buf_stream[itt] >>= 8;
vivek mehta4ed66e62016-04-15 23:33:34 -07002459 }
Eric Laurentf8b50aa2016-05-06 11:03:53 -07002460 } else {
2461 ALOGE("%s: !!! something wrong !!! ... data not 32 bit aligned ", __func__);
2462 ret = -EINVAL;
2463 goto exit;
vivek mehta4ed66e62016-04-15 23:33:34 -07002464 }
2465 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002466 }
2467
Haynes Mathew George03c40102016-01-29 17:57:48 -08002468 release_in_focus(in, ns);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002469
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002470 /*
2471 * Instead of writing zeroes here, we could trust the hardware
2472 * to always provide zeroes when muted.
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07002473 * No need to acquire adev->lock to read mic_muted here as we don't change its state.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002474 */
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07002475 if (ret == 0 && adev->mic_muted && in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002476 memset(buffer, 0, bytes);
2477
2478exit:
2479 pthread_mutex_unlock(&in->lock);
2480
2481 if (ret != 0) {
2482 in_standby(&in->stream.common);
2483 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Eric Laurentfdf296a2014-07-03 16:41:51 -07002484 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002485 in_get_sample_rate(&in->stream.common));
Andy Hungc8589872016-03-10 16:37:48 -08002486 memset(buffer, 0, bytes); // clear return data
2487 }
2488 if (bytes > 0) {
Andy Hung6ebe5962016-01-15 17:46:57 -08002489 in->frames_read += bytes / audio_stream_in_frame_size(stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002490 }
2491 return bytes;
2492}
2493
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002494static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002495{
2496 return 0;
2497}
2498
Andy Hung6ebe5962016-01-15 17:46:57 -08002499static int in_get_capture_position(const struct audio_stream_in *stream,
2500 int64_t *frames, int64_t *time)
2501{
2502 if (stream == NULL || frames == NULL || time == NULL) {
2503 return -EINVAL;
2504 }
2505 struct stream_in *in = (struct stream_in *)stream;
2506 int ret = -ENOSYS;
2507
2508 lock_input_stream(in);
2509 if (in->pcm) {
2510 struct timespec timestamp;
2511 unsigned int avail;
2512 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
2513 *frames = in->frames_read + avail;
2514 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec;
2515 ret = 0;
2516 }
2517 }
2518 pthread_mutex_unlock(&in->lock);
2519 return ret;
2520}
2521
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002522static int add_remove_audio_effect(const struct audio_stream *stream,
2523 effect_handle_t effect,
2524 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002525{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002526 struct stream_in *in = (struct stream_in *)stream;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002527 struct audio_device *adev = in->dev;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002528 int status = 0;
2529 effect_descriptor_t desc;
2530
2531 status = (*effect)->get_descriptor(effect, &desc);
2532 if (status != 0)
2533 return status;
2534
Eric Laurenta1478072015-09-21 17:21:52 -07002535 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002536 pthread_mutex_lock(&in->dev->lock);
Eric Laurent50a38ed2015-10-14 18:48:06 -07002537 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
vivek mehta733c1df2016-04-04 15:09:24 -07002538 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
Eric Laurent50a38ed2015-10-14 18:48:06 -07002539 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002540 in->enable_aec != enable &&
2541 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2542 in->enable_aec = enable;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002543 if (!enable)
2544 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
vivek mehta733c1df2016-04-04 15:09:24 -07002545 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
2546 adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
2547 adev->enable_voicerx = enable;
2548 struct audio_usecase *usecase;
2549 struct listnode *node;
2550 list_for_each(node, &adev->usecase_list) {
2551 usecase = node_to_item(node, struct audio_usecase, list);
2552 if (usecase->type == PCM_PLAYBACK) {
2553 select_devices(adev, usecase->id);
2554 break;
2555 }
Eric Laurentcefbbac2014-09-04 13:54:10 -05002556 }
2557 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002558 if (!in->standby)
2559 select_devices(in->dev, in->usecase);
2560 }
Ravi Kumar Alamanda3ad4e1b2014-06-03 00:08:15 -07002561 if (in->enable_ns != enable &&
2562 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2563 in->enable_ns = enable;
2564 if (!in->standby)
2565 select_devices(in->dev, in->usecase);
2566 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002567 pthread_mutex_unlock(&in->dev->lock);
2568 pthread_mutex_unlock(&in->lock);
2569
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002570 return 0;
2571}
2572
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002573static int in_add_audio_effect(const struct audio_stream *stream,
2574 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002575{
Eric Laurent994a6932013-07-17 11:51:42 -07002576 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002577 return add_remove_audio_effect(stream, effect, true);
2578}
2579
2580static int in_remove_audio_effect(const struct audio_stream *stream,
2581 effect_handle_t effect)
2582{
Eric Laurent994a6932013-07-17 11:51:42 -07002583 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002584 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002585}
2586
2587static int adev_open_output_stream(struct audio_hw_device *dev,
2588 audio_io_handle_t handle,
2589 audio_devices_t devices,
2590 audio_output_flags_t flags,
2591 struct audio_config *config,
Eric Laurent91975a62014-07-27 17:15:50 -07002592 struct audio_stream_out **stream_out,
2593 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002594{
2595 struct audio_device *adev = (struct audio_device *)dev;
2596 struct stream_out *out;
2597 int i, ret;
2598
Eric Laurent994a6932013-07-17 11:51:42 -07002599 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002600 __func__, config->sample_rate, config->channel_mask, devices, flags);
2601 *stream_out = NULL;
2602 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2603
2604 if (devices == AUDIO_DEVICE_NONE)
2605 devices = AUDIO_DEVICE_OUT_SPEAKER;
2606
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002607 out->flags = flags;
2608 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002609 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002610 out->format = config->format;
2611 out->sample_rate = config->sample_rate;
2612 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2613 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002614 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002615
2616 /* Init use case and pcm_config */
2617 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07002618 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002619 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002620 pthread_mutex_lock(&adev->lock);
2621 ret = read_hdmi_channel_masks(out);
2622 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002623 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002624 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002625
2626 if (config->sample_rate == 0)
2627 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2628 if (config->channel_mask == 0)
2629 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
Eric Laurentad1cec22015-12-16 10:12:27 -08002630 if (config->format == AUDIO_FORMAT_DEFAULT)
2631 config->format = AUDIO_FORMAT_PCM_16_BIT;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002632
2633 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002634 out->sample_rate = config->sample_rate;
Eric Laurentad1cec22015-12-16 10:12:27 -08002635 out->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002636 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2637 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002638 out->config.rate = config->sample_rate;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002639 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002640 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002641 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew George35807692016-07-07 20:04:54 -07002642 pthread_mutex_lock(&adev->lock);
2643 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
2644 pthread_mutex_unlock(&adev->lock);
2645
2646 // reject offload during card offline to allow
2647 // fallback to s/w paths
2648 if (offline) {
2649 ret = -ENODEV;
2650 goto error_open;
2651 }
2652
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002653 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2654 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2655 ALOGE("%s: Unsupported Offload information", __func__);
2656 ret = -EINVAL;
2657 goto error_open;
2658 }
2659 if (!is_supported_format(config->offload_info.format)) {
2660 ALOGE("%s: Unsupported audio format", __func__);
2661 ret = -EINVAL;
2662 goto error_open;
2663 }
2664
2665 out->compr_config.codec = (struct snd_codec *)
2666 calloc(1, sizeof(struct snd_codec));
2667
2668 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2669 if (config->offload_info.channel_mask)
2670 out->channel_mask = config->offload_info.channel_mask;
2671 else if (config->channel_mask)
2672 out->channel_mask = config->channel_mask;
2673 out->format = config->offload_info.format;
2674 out->sample_rate = config->offload_info.sample_rate;
2675
2676 out->stream.set_callback = out_set_callback;
2677 out->stream.pause = out_pause;
2678 out->stream.resume = out_resume;
2679 out->stream.drain = out_drain;
2680 out->stream.flush = out_flush;
2681
2682 out->compr_config.codec->id =
2683 get_snd_codec_id(config->offload_info.format);
2684 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2685 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
Eric Laurent1ccf3972014-10-23 14:42:59 -07002686 out->compr_config.codec->sample_rate = config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002687 out->compr_config.codec->bit_rate =
2688 config->offload_info.bit_rate;
2689 out->compr_config.codec->ch_in =
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002690 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002691 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2692
2693 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2694 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002695
2696 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002697 create_offload_callback_thread(out);
2698 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2699 __func__, config->offload_info.version,
2700 config->offload_info.bit_rate);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002701 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2702 if (config->sample_rate == 0)
2703 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2704 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2705 config->sample_rate != 8000) {
2706 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2707 ret = -EINVAL;
2708 goto error_open;
2709 }
2710 out->sample_rate = config->sample_rate;
2711 out->config.rate = config->sample_rate;
2712 if (config->format == AUDIO_FORMAT_DEFAULT)
2713 config->format = AUDIO_FORMAT_PCM_16_BIT;
2714 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2715 config->format = AUDIO_FORMAT_PCM_16_BIT;
2716 ret = -EINVAL;
2717 goto error_open;
2718 }
2719 out->format = config->format;
2720 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2721 out->config = pcm_config_afe_proxy_playback;
2722 adev->voice_tx_output = out;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002723 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07002724 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2725 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2726 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamandaf78a4d92015-04-24 15:18:23 -07002727 } else if (out->flags & AUDIO_OUTPUT_FLAG_TTS) {
2728 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
2729 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda2bc7b022015-06-25 20:08:01 -07002730 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
2731 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George03c40102016-01-29 17:57:48 -08002732 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL, out->flags);
2733 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
2734 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Andy Hung6fcba9c2014-03-18 11:53:32 -07002735 } else {
2736 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2737 out->config = pcm_config_low_latency;
2738 }
2739 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2740 if (k_enable_extended_precision
2741 && pcm_params_format_test(adev->use_case_table[out->usecase],
2742 pcm_format_from_audio_format(config->format))) {
2743 out->config.format = pcm_format_from_audio_format(config->format);
2744 /* out->format already set to config->format */
2745 } else {
2746 /* deny the externally proposed config format
2747 * and use the one specified in audio_hw layer configuration.
2748 * Note: out->format is returned by out->stream.common.get_format()
2749 * and is used to set config->format in the code several lines below.
2750 */
2751 out->format = audio_format_from_pcm_format(out->config.format);
2752 }
2753 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002754 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002755 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07002756 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
2757 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002758
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002759 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002760 if (adev->primary_output == NULL)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002761 adev->primary_output = out;
2762 else {
2763 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002764 ret = -EEXIST;
2765 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002766 }
2767 }
2768
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002769 /* Check if this usecase is already existing */
2770 pthread_mutex_lock(&adev->lock);
2771 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2772 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002773 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002774 ret = -EEXIST;
2775 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002776 }
2777 pthread_mutex_unlock(&adev->lock);
2778
2779 out->stream.common.get_sample_rate = out_get_sample_rate;
2780 out->stream.common.set_sample_rate = out_set_sample_rate;
2781 out->stream.common.get_buffer_size = out_get_buffer_size;
2782 out->stream.common.get_channels = out_get_channels;
2783 out->stream.common.get_format = out_get_format;
2784 out->stream.common.set_format = out_set_format;
2785 out->stream.common.standby = out_standby;
2786 out->stream.common.dump = out_dump;
2787 out->stream.common.set_parameters = out_set_parameters;
2788 out->stream.common.get_parameters = out_get_parameters;
2789 out->stream.common.add_audio_effect = out_add_audio_effect;
2790 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2791 out->stream.get_latency = out_get_latency;
2792 out->stream.set_volume = out_set_volume;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002793#ifdef NO_AUDIO_OUT
2794 out->stream.write = out_write_for_no_output;
2795#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002796 out->stream.write = out_write;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002797#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002798 out->stream.get_render_position = out_get_render_position;
2799 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002800 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002801
Haynes Mathew George03c40102016-01-29 17:57:48 -08002802 out->af_period_multiplier = out->realtime ? af_period_multiplier : 1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002803 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002804 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002805 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002806
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002807 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Eric Laurenta1478072015-09-21 17:21:52 -07002808 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002809 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2810
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002811 config->format = out->stream.common.get_format(&out->stream.common);
2812 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2813 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2814
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07002815
2816 /*
2817 By locking output stream before registering, we allow the callback
2818 to update stream's state only after stream's initial state is set to
2819 adev state.
2820 */
2821 lock_output_stream(out);
2822 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
2823 pthread_mutex_lock(&adev->lock);
2824 out->card_status = adev->card_status;
2825 pthread_mutex_unlock(&adev->lock);
2826 pthread_mutex_unlock(&out->lock);
2827
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002828 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002829 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002830 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002831
2832error_open:
2833 free(out);
2834 *stream_out = NULL;
Eric Laurent2bafff12016-03-17 12:17:23 -07002835 ALOGW("%s: exit: ret %d", __func__, ret);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002836 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002837}
2838
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002839static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002840 struct audio_stream_out *stream)
2841{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002842 struct stream_out *out = (struct stream_out *)stream;
2843 struct audio_device *adev = out->dev;
2844
Eric Laurent994a6932013-07-17 11:51:42 -07002845 ALOGV("%s: enter", __func__);
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07002846
2847 // must deregister from sndmonitor first to prevent races
2848 // between the callback and close_stream
2849 audio_extn_snd_mon_unregister_listener(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002850 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002851 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2852 destroy_offload_callback_thread(out);
2853
2854 if (out->compr_config.codec != NULL)
2855 free(out->compr_config.codec);
2856 }
Ravi Kumar Alamandaa4fc9022014-10-08 18:57:46 -07002857
2858 if (adev->voice_tx_output == out)
2859 adev->voice_tx_output = NULL;
2860
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002861 pthread_cond_destroy(&out->cond);
2862 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002863 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002864 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002865}
2866
2867static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2868{
2869 struct audio_device *adev = (struct audio_device *)dev;
2870 struct str_parms *parms;
2871 char *str;
2872 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002873 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002874 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002875 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002876
Joe Onorato188b6222016-03-01 11:02:27 -08002877 ALOGV("%s: enter: %s", __func__, kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002878
2879 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002880
2881 parms = str_parms_create_str(kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002882 status = voice_set_parameters(adev, parms);
2883 if (status != 0) {
2884 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002885 }
2886
2887 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2888 if (ret >= 0) {
Ravi Kumar Alamandae258e682015-06-25 13:32:42 -07002889 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002890 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2891 adev->bluetooth_nrec = true;
2892 else
2893 adev->bluetooth_nrec = false;
2894 }
2895
2896 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2897 if (ret >= 0) {
2898 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2899 adev->screen_off = false;
2900 else
2901 adev->screen_off = true;
2902 }
2903
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002904 ret = str_parms_get_int(parms, "rotation", &val);
2905 if (ret >= 0) {
2906 bool reverse_speakers = false;
2907 switch(val) {
2908 // FIXME: note that the code below assumes that the speakers are in the correct placement
2909 // relative to the user when the device is rotated 90deg from its default rotation. This
2910 // assumption is device-specific, not platform-specific like this code.
2911 case 270:
2912 reverse_speakers = true;
2913 break;
2914 case 0:
2915 case 90:
2916 case 180:
2917 break;
2918 default:
2919 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002920 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002921 }
Eric Laurent03f09432014-03-25 18:09:11 -07002922 if (status == 0) {
Ravi Kumar Alamanda1f60cf82015-04-23 19:45:17 -07002923 platform_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002924 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002925 }
2926
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002927 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2928 if (ret >= 0) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002929 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002930 }
2931
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08002932 audio_extn_hfp_set_parameters(adev, parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002933done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002934 str_parms_destroy(parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002935 pthread_mutex_unlock(&adev->lock);
Eric Laurent03f09432014-03-25 18:09:11 -07002936 ALOGV("%s: exit with code(%d)", __func__, status);
2937 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002938}
2939
2940static char* adev_get_parameters(const struct audio_hw_device *dev,
2941 const char *keys)
2942{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002943 struct audio_device *adev = (struct audio_device *)dev;
2944 struct str_parms *reply = str_parms_create();
2945 struct str_parms *query = str_parms_create_str(keys);
2946 char *str;
2947
2948 pthread_mutex_lock(&adev->lock);
2949
2950 voice_get_parameters(adev, query, reply);
2951 str = str_parms_to_str(reply);
2952 str_parms_destroy(query);
2953 str_parms_destroy(reply);
2954
2955 pthread_mutex_unlock(&adev->lock);
2956 ALOGV("%s: exit: returns - %s", __func__, str);
2957 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002958}
2959
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002960static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002961{
2962 return 0;
2963}
2964
Haynes Mathew George5191a852013-09-11 14:19:36 -07002965static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2966{
2967 int ret;
2968 struct audio_device *adev = (struct audio_device *)dev;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002969
Eric Laurent4cc4ce12014-09-10 13:21:01 -05002970 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
2971
Haynes Mathew George5191a852013-09-11 14:19:36 -07002972 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002973 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002974 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002975
Haynes Mathew George5191a852013-09-11 14:19:36 -07002976 return ret;
2977}
2978
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002979static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002980{
2981 return -ENOSYS;
2982}
2983
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002984static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2985 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002986{
2987 return -ENOSYS;
2988}
2989
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002990static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002991{
2992 return -ENOSYS;
2993}
2994
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002995static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002996{
2997 return -ENOSYS;
2998}
2999
3000static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
3001{
3002 struct audio_device *adev = (struct audio_device *)dev;
3003
3004 pthread_mutex_lock(&adev->lock);
3005 if (adev->mode != mode) {
Eric Laurent2bafff12016-03-17 12:17:23 -07003006 ALOGD("%s: mode %d", __func__, (int)mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003007 adev->mode = mode;
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -07003008 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) &&
3009 voice_is_in_call(adev)) {
3010 voice_stop_call(adev);
3011 adev->current_call_output = NULL;
3012 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003013 }
3014 pthread_mutex_unlock(&adev->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05003015
3016 audio_extn_extspk_set_mode(adev->extspk, mode);
3017
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003018 return 0;
3019}
3020
3021static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
3022{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003023 int ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003024 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003025
Eric Laurent2bafff12016-03-17 12:17:23 -07003026 ALOGD("%s: state %d", __func__, (int)state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003027 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003028 ret = voice_set_mic_mute(adev, state);
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07003029 adev->mic_muted = state;
Eric Laurenta609e8e2014-06-18 02:15:17 +00003030 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003031
3032 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003033}
3034
3035static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
3036{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003037 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003038 return 0;
3039}
3040
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07003041static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003042 const struct audio_config *config)
3043{
Eric Laurent0de8d1f2014-07-01 20:34:45 -07003044 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003045
Glenn Kasten68e79ce2014-07-15 10:56:59 -07003046 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
3047 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003048}
3049
3050static int adev_open_input_stream(struct audio_hw_device *dev,
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07003051 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003052 audio_devices_t devices,
3053 struct audio_config *config,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07003054 struct audio_stream_in **stream_in,
Eric Laurent91975a62014-07-27 17:15:50 -07003055 audio_input_flags_t flags,
3056 const char *address __unused,
Eric Laurentcefbbac2014-09-04 13:54:10 -05003057 audio_source_t source )
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003058{
3059 struct audio_device *adev = (struct audio_device *)dev;
3060 struct stream_in *in;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003061 int ret = 0, buffer_size, frame_size;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07003062 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003063 bool is_low_latency = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003064
Eric Laurent994a6932013-07-17 11:51:42 -07003065 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003066 *stream_in = NULL;
3067 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
3068 return -EINVAL;
3069
3070 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
3071
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003072 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Eric Laurenta1478072015-09-21 17:21:52 -07003073 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003074
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003075 in->stream.common.get_sample_rate = in_get_sample_rate;
3076 in->stream.common.set_sample_rate = in_set_sample_rate;
3077 in->stream.common.get_buffer_size = in_get_buffer_size;
3078 in->stream.common.get_channels = in_get_channels;
3079 in->stream.common.get_format = in_get_format;
3080 in->stream.common.set_format = in_set_format;
3081 in->stream.common.standby = in_standby;
3082 in->stream.common.dump = in_dump;
3083 in->stream.common.set_parameters = in_set_parameters;
3084 in->stream.common.get_parameters = in_get_parameters;
3085 in->stream.common.add_audio_effect = in_add_audio_effect;
3086 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3087 in->stream.set_gain = in_set_gain;
3088 in->stream.read = in_read;
3089 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hung6ebe5962016-01-15 17:46:57 -08003090 in->stream.get_capture_position = in_get_capture_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003091
3092 in->device = devices;
Eric Laurentcefbbac2014-09-04 13:54:10 -05003093 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003094 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003095 in->standby = 1;
3096 in->channel_mask = config->channel_mask;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07003097 in->capture_handle = handle;
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07003098 in->flags = flags;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003099
vivek mehta57ff9b52016-04-28 14:13:08 -07003100 // restrict 24 bit capture for unprocessed source only
3101 // for other sources if 24 bit requested reject 24 and set 16 bit capture only
3102 if (config->format == AUDIO_FORMAT_DEFAULT) {
vivek mehta4ed66e62016-04-15 23:33:34 -07003103 config->format = AUDIO_FORMAT_PCM_16_BIT;
vivek mehta57ff9b52016-04-28 14:13:08 -07003104 } else if (config->format == AUDIO_FORMAT_PCM_FLOAT ||
3105 config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
3106 config->format == AUDIO_FORMAT_PCM_8_24_BIT) {
3107 bool ret_error = false;
3108 /* 24 bit is restricted to UNPROCESSED source only,also format supported
3109 from HAL is 8_24
3110 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
3111 8_24 return error indicating supported format is 8_24
3112 *> In case of any other source requesting 24 bit or float return error
3113 indicating format supported is 16 bit only.
vivek mehta4ed66e62016-04-15 23:33:34 -07003114
vivek mehta57ff9b52016-04-28 14:13:08 -07003115 on error flinger will retry with supported format passed
3116 */
3117 if (source != AUDIO_SOURCE_UNPROCESSED) {
3118 config->format = AUDIO_FORMAT_PCM_16_BIT;
3119 ret_error = true;
3120 } else if (config->format != AUDIO_FORMAT_PCM_8_24_BIT) {
3121 config->format = AUDIO_FORMAT_PCM_8_24_BIT;
3122 ret_error = true;
3123 }
3124
3125 if (ret_error) {
3126 ret = -EINVAL;
3127 goto err_open;
3128 }
vivek mehta4ed66e62016-04-15 23:33:34 -07003129 }
3130
vivek mehta57ff9b52016-04-28 14:13:08 -07003131 in->format = config->format;
3132
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003133 /* Update config params with the requested sample rate and channels */
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003134 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
3135 if (config->sample_rate == 0)
3136 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3137 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
3138 config->sample_rate != 8000) {
3139 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3140 ret = -EINVAL;
3141 goto err_open;
3142 }
vivek mehta4ed66e62016-04-15 23:33:34 -07003143
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003144 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
3145 config->format = AUDIO_FORMAT_PCM_16_BIT;
3146 ret = -EINVAL;
3147 goto err_open;
3148 }
3149
3150 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
3151 in->config = pcm_config_afe_proxy_record;
3152 } else {
3153 in->usecase = USECASE_AUDIO_RECORD;
3154 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
3155 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
3156 is_low_latency = true;
Glenn Kasten4f993392014-05-14 07:30:48 -07003157#if LOW_LATENCY_CAPTURE_USE_CASE
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003158 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Glenn Kasten4f993392014-05-14 07:30:48 -07003159#endif
Haynes Mathew George03c40102016-01-29 17:57:48 -08003160 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003161 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003162
Haynes Mathew George03c40102016-01-29 17:57:48 -08003163 in->config = in->realtime ? pcm_config_audio_capture_rt :
3164 pcm_config_audio_capture;
3165
vivek mehta4ed66e62016-04-15 23:33:34 -07003166 if (config->format == AUDIO_FORMAT_PCM_8_24_BIT)
3167 in->config.format = PCM_FORMAT_S24_LE;
3168
Haynes Mathew George03c40102016-01-29 17:57:48 -08003169 if (!in->realtime) {
3170 frame_size = audio_stream_in_frame_size(&in->stream);
3171 buffer_size = get_input_buffer_size(config->sample_rate,
3172 config->format,
3173 channel_count,
Eric Laurentf8b50aa2016-05-06 11:03:53 -07003174 is_low_latency);
Haynes Mathew George03c40102016-01-29 17:57:48 -08003175 in->config.period_size = buffer_size / frame_size;
3176 } // period size is left untouched for rt mode playback
Glenn Kasten68e79ce2014-07-15 10:56:59 -07003177 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08003178
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003179 in->config.channels = channel_count;
Haynes Mathew George03c40102016-01-29 17:57:48 -08003180 if (in->realtime) {
3181 in->af_period_multiplier = af_period_multiplier;
3182 } else {
3183 in->config.rate = config->sample_rate;
3184 in->af_period_multiplier = 1;
3185 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003186
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07003187 /* This stream could be for sound trigger lab,
3188 get sound trigger pcm if present */
3189 audio_extn_sound_trigger_check_and_get_session(in);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003190
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07003191 lock_input_stream(in);
3192 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
3193 pthread_mutex_lock(&adev->lock);
3194 in->card_status = adev->card_status;
3195 pthread_mutex_unlock(&adev->lock);
3196 pthread_mutex_unlock(&in->lock);
3197
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003198 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07003199 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003200 return 0;
3201
3202err_open:
3203 free(in);
3204 *stream_in = NULL;
3205 return ret;
3206}
3207
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07003208static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003209 struct audio_stream_in *stream)
3210{
Eric Laurent994a6932013-07-17 11:51:42 -07003211 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003212
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07003213 // must deregister from sndmonitor first to prevent races
3214 // between the callback and close_stream
3215 audio_extn_snd_mon_unregister_listener(stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003216 in_standby(&stream->common);
3217 free(stream);
3218
3219 return;
3220}
3221
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07003222static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003223{
3224 return 0;
3225}
3226
Andy Hung31aca912014-03-20 17:14:59 -07003227/* verifies input and output devices and their capabilities.
3228 *
3229 * This verification is required when enabling extended bit-depth or
3230 * sampling rates, as not all qcom products support it.
3231 *
3232 * Suitable for calling only on initialization such as adev_open().
3233 * It fills the audio_device use_case_table[] array.
3234 *
3235 * Has a side-effect that it needs to configure audio routing / devices
3236 * in order to power up the devices and read the device parameters.
3237 * It does not acquire any hw device lock. Should restore the devices
3238 * back to "normal state" upon completion.
3239 */
3240static int adev_verify_devices(struct audio_device *adev)
3241{
3242 /* enumeration is a bit difficult because one really wants to pull
3243 * the use_case, device id, etc from the hidden pcm_device_table[].
3244 * In this case there are the following use cases and device ids.
3245 *
3246 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
3247 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
3248 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
3249 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
3250 * [USECASE_AUDIO_RECORD] = {0, 0},
3251 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
3252 * [USECASE_VOICE_CALL] = {2, 2},
3253 *
3254 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
3255 * USECASE_VOICE_CALL omitted, but possible for either input or output.
3256 */
3257
3258 /* should be the usecases enabled in adev_open_input_stream() */
3259 static const int test_in_usecases[] = {
3260 USECASE_AUDIO_RECORD,
3261 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
3262 };
3263 /* should be the usecases enabled in adev_open_output_stream()*/
3264 static const int test_out_usecases[] = {
3265 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
3266 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
3267 };
3268 static const usecase_type_t usecase_type_by_dir[] = {
3269 PCM_PLAYBACK,
3270 PCM_CAPTURE,
3271 };
3272 static const unsigned flags_by_dir[] = {
3273 PCM_OUT,
3274 PCM_IN,
3275 };
3276
3277 size_t i;
3278 unsigned dir;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003279 const unsigned card_id = adev->snd_card;
Andy Hung31aca912014-03-20 17:14:59 -07003280 char info[512]; /* for possible debug info */
3281
3282 for (dir = 0; dir < 2; ++dir) {
3283 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
3284 const unsigned flags_dir = flags_by_dir[dir];
3285 const size_t testsize =
3286 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
3287 const int *testcases =
3288 dir ? test_in_usecases : test_out_usecases;
3289 const audio_devices_t audio_device =
3290 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
3291
3292 for (i = 0; i < testsize; ++i) {
3293 const audio_usecase_t audio_usecase = testcases[i];
3294 int device_id;
3295 snd_device_t snd_device;
3296 struct pcm_params **pparams;
3297 struct stream_out out;
3298 struct stream_in in;
3299 struct audio_usecase uc_info;
3300 int retval;
3301
3302 pparams = &adev->use_case_table[audio_usecase];
3303 pcm_params_free(*pparams); /* can accept null input */
3304 *pparams = NULL;
3305
3306 /* find the device ID for the use case (signed, for error) */
3307 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
3308 if (device_id < 0)
3309 continue;
3310
3311 /* prepare structures for device probing */
3312 memset(&uc_info, 0, sizeof(uc_info));
3313 uc_info.id = audio_usecase;
3314 uc_info.type = usecase_type;
3315 if (dir) {
3316 adev->active_input = &in;
3317 memset(&in, 0, sizeof(in));
3318 in.device = audio_device;
3319 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
3320 uc_info.stream.in = &in;
3321 } else {
3322 adev->active_input = NULL;
3323 }
3324 memset(&out, 0, sizeof(out));
3325 out.devices = audio_device; /* only field needed in select_devices */
3326 uc_info.stream.out = &out;
3327 uc_info.devices = audio_device;
3328 uc_info.in_snd_device = SND_DEVICE_NONE;
3329 uc_info.out_snd_device = SND_DEVICE_NONE;
3330 list_add_tail(&adev->usecase_list, &uc_info.list);
3331
3332 /* select device - similar to start_(in/out)put_stream() */
3333 retval = select_devices(adev, audio_usecase);
3334 if (retval >= 0) {
3335 *pparams = pcm_params_get(card_id, device_id, flags_dir);
3336#if LOG_NDEBUG == 0
3337 if (*pparams) {
3338 ALOGV("%s: (%s) card %d device %d", __func__,
3339 dir ? "input" : "output", card_id, device_id);
3340 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
Andy Hung31aca912014-03-20 17:14:59 -07003341 } else {
3342 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
3343 }
3344#endif
3345 }
3346
3347 /* deselect device - similar to stop_(in/out)put_stream() */
3348 /* 1. Get and set stream specific mixer controls */
Glenn Kastene7137302014-04-28 15:12:18 -07003349 retval = disable_audio_route(adev, &uc_info);
Andy Hung31aca912014-03-20 17:14:59 -07003350 /* 2. Disable the rx device */
3351 retval = disable_snd_device(adev,
Glenn Kastene7137302014-04-28 15:12:18 -07003352 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
Andy Hung31aca912014-03-20 17:14:59 -07003353 list_remove(&uc_info.list);
3354 }
3355 }
3356 adev->active_input = NULL; /* restore adev state */
3357 return 0;
3358}
3359
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003360static int adev_close(hw_device_t *device)
3361{
Andy Hung31aca912014-03-20 17:14:59 -07003362 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003363 struct audio_device *adev = (struct audio_device *)device;
vivek mehta1a9b7c02015-06-25 11:49:38 -07003364
3365 if (!adev)
3366 return 0;
3367
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07003368 audio_extn_snd_mon_unregister_listener(adev);
vivek mehta1a9b7c02015-06-25 11:49:38 -07003369 pthread_mutex_lock(&adev_init_lock);
3370
3371 if ((--audio_device_ref_count) == 0) {
3372 audio_route_free(adev->audio_route);
3373 free(adev->snd_dev_ref_cnt);
3374 platform_deinit(adev->platform);
3375 audio_extn_extspk_deinit(adev->extspk);
3376 audio_extn_sound_trigger_deinit(adev);
3377 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
3378 pcm_params_free(adev->use_case_table[i]);
3379 }
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07003380 if (adev->adm_deinit)
3381 adev->adm_deinit(adev->adm_data);
vivek mehta1a9b7c02015-06-25 11:49:38 -07003382 free(device);
Andy Hung31aca912014-03-20 17:14:59 -07003383 }
vivek mehta1a9b7c02015-06-25 11:49:38 -07003384
3385 pthread_mutex_unlock(&adev_init_lock);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07003386
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003387 return 0;
3388}
3389
Glenn Kasten4f993392014-05-14 07:30:48 -07003390/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
3391 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
3392 * just that it _might_ work.
3393 */
3394static int period_size_is_plausible_for_low_latency(int period_size)
3395{
3396 switch (period_size) {
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07003397 case 48:
3398 case 96:
3399 case 144:
Glenn Kasten4f993392014-05-14 07:30:48 -07003400 case 160:
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07003401 case 192:
Glenn Kasten4f993392014-05-14 07:30:48 -07003402 case 240:
3403 case 320:
3404 case 480:
3405 return 1;
3406 default:
3407 return 0;
3408 }
3409}
3410
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07003411static void adev_snd_mon_cb(void * stream __unused, struct str_parms * parms)
3412{
3413 int card;
3414 card_status_t status;
3415
3416 if (!parms)
3417 return;
3418
3419 if (parse_snd_card_status(parms, &card, &status) < 0)
3420 return;
3421
3422 pthread_mutex_lock(&adev->lock);
3423 bool valid_cb = (card == adev->snd_card);
3424 if (valid_cb) {
3425 if (adev->card_status != status) {
3426 adev->card_status = status;
3427 platform_snd_card_update(adev->platform, status);
3428 }
3429 }
3430 pthread_mutex_unlock(&adev->lock);
3431 return;
3432}
3433
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003434static int adev_open(const hw_module_t *module, const char *name,
3435 hw_device_t **device)
3436{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003437 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003438
Eric Laurent2bafff12016-03-17 12:17:23 -07003439 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003440 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
vivek mehta1a9b7c02015-06-25 11:49:38 -07003441 pthread_mutex_lock(&adev_init_lock);
3442 if (audio_device_ref_count != 0) {
3443 *device = &adev->device.common;
3444 audio_device_ref_count++;
3445 ALOGV("%s: returning existing instance of adev", __func__);
3446 ALOGV("%s: exit", __func__);
3447 pthread_mutex_unlock(&adev_init_lock);
3448 return 0;
3449 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003450 adev = calloc(1, sizeof(struct audio_device));
3451
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003452 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3453
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003454 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3455 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3456 adev->device.common.module = (struct hw_module_t *)module;
3457 adev->device.common.close = adev_close;
3458
3459 adev->device.init_check = adev_init_check;
3460 adev->device.set_voice_volume = adev_set_voice_volume;
3461 adev->device.set_master_volume = adev_set_master_volume;
3462 adev->device.get_master_volume = adev_get_master_volume;
3463 adev->device.set_master_mute = adev_set_master_mute;
3464 adev->device.get_master_mute = adev_get_master_mute;
3465 adev->device.set_mode = adev_set_mode;
3466 adev->device.set_mic_mute = adev_set_mic_mute;
3467 adev->device.get_mic_mute = adev_get_mic_mute;
3468 adev->device.set_parameters = adev_set_parameters;
3469 adev->device.get_parameters = adev_get_parameters;
3470 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3471 adev->device.open_output_stream = adev_open_output_stream;
3472 adev->device.close_output_stream = adev_close_output_stream;
3473 adev->device.open_input_stream = adev_open_input_stream;
3474 adev->device.close_input_stream = adev_close_input_stream;
3475 adev->device.dump = adev_dump;
3476
3477 /* Set the default route before the PCM stream is opened */
3478 pthread_mutex_lock(&adev->lock);
3479 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003480 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003481 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003482 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003483 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003484 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07003485 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003486 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003487 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003488 pthread_mutex_unlock(&adev->lock);
3489
3490 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003491 adev->platform = platform_init(adev);
3492 if (!adev->platform) {
3493 free(adev->snd_dev_ref_cnt);
3494 free(adev);
3495 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3496 *device = NULL;
vivek mehta1a9b7c02015-06-25 11:49:38 -07003497 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003498 return -EINVAL;
3499 }
Eric Laurent0499d4f2014-08-25 22:39:29 -05003500 adev->extspk = audio_extn_extspk_init(adev);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07003501 audio_extn_sound_trigger_init(adev);
Eric Laurent0499d4f2014-08-25 22:39:29 -05003502
Glenn Kasten6d53bfd2016-04-04 16:58:03 -07003503 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3504 if (adev->visualizer_lib == NULL) {
3505 ALOGW("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3506 } else {
3507 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3508 adev->visualizer_start_output =
3509 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
3510 "visualizer_hal_start_output");
3511 adev->visualizer_stop_output =
3512 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
3513 "visualizer_hal_stop_output");
Eric Laurentc4aef752013-09-12 17:45:53 -07003514 }
3515
Glenn Kasten6d53bfd2016-04-04 16:58:03 -07003516 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3517 if (adev->offload_effects_lib == NULL) {
3518 ALOGW("%s: DLOPEN failed for %s", __func__,
3519 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3520 } else {
3521 ALOGV("%s: DLOPEN successful for %s", __func__,
3522 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3523 adev->offload_effects_start_output =
3524 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3525 "offload_effects_bundle_hal_start_output");
3526 adev->offload_effects_stop_output =
3527 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3528 "offload_effects_bundle_hal_stop_output");
Haynes Mathew George41f86652014-06-17 14:22:15 -07003529 }
3530
Glenn Kasten6d53bfd2016-04-04 16:58:03 -07003531 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
3532 if (adev->adm_lib == NULL) {
3533 ALOGW("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
3534 } else {
3535 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
3536 adev->adm_init = (adm_init_t)
3537 dlsym(adev->adm_lib, "adm_init");
3538 adev->adm_deinit = (adm_deinit_t)
3539 dlsym(adev->adm_lib, "adm_deinit");
3540 adev->adm_register_input_stream = (adm_register_input_stream_t)
3541 dlsym(adev->adm_lib, "adm_register_input_stream");
3542 adev->adm_register_output_stream = (adm_register_output_stream_t)
3543 dlsym(adev->adm_lib, "adm_register_output_stream");
3544 adev->adm_deregister_stream = (adm_deregister_stream_t)
3545 dlsym(adev->adm_lib, "adm_deregister_stream");
3546 adev->adm_request_focus = (adm_request_focus_t)
3547 dlsym(adev->adm_lib, "adm_request_focus");
3548 adev->adm_abandon_focus = (adm_abandon_focus_t)
3549 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George03c40102016-01-29 17:57:48 -08003550 adev->adm_set_config = (adm_set_config_t)
3551 dlsym(adev->adm_lib, "adm_set_config");
3552 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
3553 dlsym(adev->adm_lib, "adm_request_focus_v2");
3554 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
3555 dlsym(adev->adm_lib, "adm_is_noirq_avail");
3556 adev->adm_on_routing_change = (adm_on_routing_change_t)
3557 dlsym(adev->adm_lib, "adm_on_routing_change");
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07003558 }
3559
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07003560 adev->bt_wb_speech_enabled = false;
Eric Laurentcefbbac2014-09-04 13:54:10 -05003561 adev->enable_voicerx = false;
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07003562
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003563 *device = &adev->device.common;
vivek mehta1a9b7c02015-06-25 11:49:38 -07003564
Andy Hung31aca912014-03-20 17:14:59 -07003565 if (k_enable_extended_precision)
3566 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003567
Glenn Kasten4f993392014-05-14 07:30:48 -07003568 char value[PROPERTY_VALUE_MAX];
3569 int trial;
3570 if (property_get("audio_hal.period_size", value, NULL) > 0) {
3571 trial = atoi(value);
3572 if (period_size_is_plausible_for_low_latency(trial)) {
3573 pcm_config_low_latency.period_size = trial;
3574 pcm_config_low_latency.start_threshold = trial / 4;
3575 pcm_config_low_latency.avail_min = trial / 4;
3576 configured_low_latency_capture_period_size = trial;
3577 }
3578 }
3579 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
3580 trial = atoi(value);
3581 if (period_size_is_plausible_for_low_latency(trial)) {
3582 configured_low_latency_capture_period_size = trial;
3583 }
3584 }
3585
vivek mehta1a9b7c02015-06-25 11:49:38 -07003586 audio_device_ref_count++;
Haynes Mathew George03c40102016-01-29 17:57:48 -08003587
3588 if (property_get("audio_hal.period_multiplier", value, NULL) > 0) {
3589 af_period_multiplier = atoi(value);
3590 if (af_period_multiplier < 0) {
3591 af_period_multiplier = 2;
3592 } else if (af_period_multiplier > 4) {
3593 af_period_multiplier = 4;
3594 }
3595 ALOGV("new period_multiplier = %d", af_period_multiplier);
3596 }
3597
vivek mehta1a9b7c02015-06-25 11:49:38 -07003598 pthread_mutex_unlock(&adev_init_lock);
3599
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07003600 if (adev->adm_init)
3601 adev->adm_data = adev->adm_init();
3602
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07003603 audio_extn_perf_lock_init();
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07003604 audio_extn_snd_mon_init();
3605 pthread_mutex_lock(&adev->lock);
3606 audio_extn_snd_mon_register_listener(NULL, adev_snd_mon_cb);
3607 adev->card_status = CARD_STATUS_ONLINE;
3608 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07003609
Eric Laurent2bafff12016-03-17 12:17:23 -07003610 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003611 return 0;
3612}
3613
3614static struct hw_module_methods_t hal_module_methods = {
3615 .open = adev_open,
3616};
3617
3618struct audio_module HAL_MODULE_INFO_SYM = {
3619 .common = {
3620 .tag = HARDWARE_MODULE_TAG,
3621 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3622 .hal_api_version = HARDWARE_HAL_API_VERSION,
3623 .id = AUDIO_HARDWARE_MODULE_ID,
3624 .name = "QCOM Audio HAL",
3625 .author = "Code Aurora Forum",
3626 .methods = &hal_module_methods,
3627 },
3628};