blob: 35b726e0398ce58b739235ded7b1119592d52cc6 [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;
vivek mehtafb4d7bd2016-04-29 03:16:47 -0700236//cache last MBDRC cal step level
237static int last_known_cal_step = -1 ;
vivek mehta1a9b7c02015-06-25 11:49:38 -0700238
Haynes Mathew George03c40102016-01-29 17:57:48 -0800239static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
240 int flags __unused)
241{
242 int dir = 0;
243 switch (uc_id) {
244 case USECASE_AUDIO_RECORD_LOW_LATENCY:
245 dir = 1;
246 case USECASE_AUDIO_PLAYBACK_ULL:
247 break;
248 default:
249 return false;
250 }
251
252 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
253 PCM_PLAYBACK : PCM_CAPTURE);
254 if (adev->adm_is_noirq_avail)
255 return adev->adm_is_noirq_avail(adev->adm_data,
256 adev->snd_card, dev_id, dir);
257 return false;
258}
259
260static void register_out_stream(struct stream_out *out)
261{
262 struct audio_device *adev = out->dev;
263 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
264 return;
265
266 if (!adev->adm_register_output_stream)
267 return;
268
269 adev->adm_register_output_stream(adev->adm_data,
270 out->handle,
271 out->flags);
272
273 if (!adev->adm_set_config)
274 return;
275
276 if (out->realtime) {
277 adev->adm_set_config(adev->adm_data,
278 out->handle,
279 out->pcm, &out->config);
280 }
281}
282
283static void register_in_stream(struct stream_in *in)
284{
285 struct audio_device *adev = in->dev;
286 if (!adev->adm_register_input_stream)
287 return;
288
289 adev->adm_register_input_stream(adev->adm_data,
290 in->capture_handle,
291 in->flags);
292
293 if (!adev->adm_set_config)
294 return;
295
296 if (in->realtime) {
297 adev->adm_set_config(adev->adm_data,
298 in->capture_handle,
299 in->pcm,
300 &in->config);
301 }
302}
303
304static void request_out_focus(struct stream_out *out, long ns)
305{
306 struct audio_device *adev = out->dev;
307
308 if (out->routing_change) {
309 out->routing_change = false;
310 if (adev->adm_on_routing_change)
311 adev->adm_on_routing_change(adev->adm_data, out->handle);
312 }
313
314 if (adev->adm_request_focus_v2) {
315 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
316 } else if (adev->adm_request_focus) {
317 adev->adm_request_focus(adev->adm_data, out->handle);
318 }
319}
320
321static void request_in_focus(struct stream_in *in, long ns)
322{
323 struct audio_device *adev = in->dev;
324
325 if (in->routing_change) {
326 in->routing_change = false;
327 if (adev->adm_on_routing_change)
328 adev->adm_on_routing_change(adev->adm_data, in->capture_handle);
329 }
330
331 if (adev->adm_request_focus_v2) {
332 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
333 } else if (adev->adm_request_focus) {
334 adev->adm_request_focus(adev->adm_data, in->capture_handle);
335 }
336}
337
338static void release_out_focus(struct stream_out *out, long ns __unused)
339{
340 struct audio_device *adev = out->dev;
341
342 if (adev->adm_abandon_focus)
343 adev->adm_abandon_focus(adev->adm_data, out->handle);
344}
345
346static void release_in_focus(struct stream_in *in, long ns __unused)
347{
348 struct audio_device *adev = in->dev;
349 if (adev->adm_abandon_focus)
350 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
351}
352
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -0700353static int parse_snd_card_status(struct str_parms * parms, int * card,
354 card_status_t * status)
355{
356 char value[32]={0};
357 char state[32]={0};
358
359 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
360
361 if (ret < 0)
362 return -1;
363
364 // sscanf should be okay as value is of max length 32.
365 // same as sizeof state.
366 if (sscanf(value, "%d,%s", card, state) < 2)
367 return -1;
368
369 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
370 CARD_STATUS_OFFLINE;
371 return 0;
372}
373
vivek mehta1a9b7c02015-06-25 11:49:38 -0700374__attribute__ ((visibility ("default")))
375bool audio_hw_send_gain_dep_calibration(int level) {
376 bool ret_val = false;
377 ALOGV("%s: enter ... ", __func__);
378
379 pthread_mutex_lock(&adev_init_lock);
380
381 if (adev != NULL && adev->platform != NULL) {
382 pthread_mutex_lock(&adev->lock);
383 ret_val = platform_send_gain_dep_cal(adev->platform, level);
384 pthread_mutex_unlock(&adev->lock);
vivek mehtafb4d7bd2016-04-29 03:16:47 -0700385
386 // if cal set fails, cache level info
387 // if cal set succeds, reset known last cal set
388 if (!ret_val)
389 last_known_cal_step = level;
390 else if (last_known_cal_step != -1)
391 last_known_cal_step = -1;
vivek mehta1a9b7c02015-06-25 11:49:38 -0700392 } else {
393 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
394 }
395
396 pthread_mutex_unlock(&adev_init_lock);
397
398 ALOGV("%s: exit with ret_val %d ", __func__, ret_val);
399 return ret_val;
400}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700401
vivek mehtaa8d7c922016-05-25 14:40:44 -0700402__attribute__ ((visibility ("default")))
403int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
404 int table_size) {
405 int ret_val = 0;
406 ALOGV("%s: enter ... ", __func__);
407
408 pthread_mutex_lock(&adev_init_lock);
409 if (adev == NULL) {
410 ALOGW("%s: adev is NULL .... ", __func__);
411 goto done;
412 }
413
414 pthread_mutex_lock(&adev->lock);
415 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
416 pthread_mutex_unlock(&adev->lock);
417done:
418 pthread_mutex_unlock(&adev_init_lock);
419 ALOGV("%s: exit ... ", __func__);
420 return ret_val;
421}
422
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700423static bool is_supported_format(audio_format_t format)
424{
Eric Laurent8251ac82014-07-23 11:00:25 -0700425 switch (format) {
426 case AUDIO_FORMAT_MP3:
427 case AUDIO_FORMAT_AAC_LC:
428 case AUDIO_FORMAT_AAC_HE_V1:
429 case AUDIO_FORMAT_AAC_HE_V2:
430 return true;
431 default:
432 break;
433 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700434 return false;
435}
436
Haynes Mathew George03c40102016-01-29 17:57:48 -0800437static inline bool is_mmap_usecase(audio_usecase_t uc_id)
438{
439 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
440 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
441}
442
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700443static int get_snd_codec_id(audio_format_t format)
444{
445 int id = 0;
446
Eric Laurent8251ac82014-07-23 11:00:25 -0700447 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700448 case AUDIO_FORMAT_MP3:
449 id = SND_AUDIOCODEC_MP3;
450 break;
451 case AUDIO_FORMAT_AAC:
452 id = SND_AUDIOCODEC_AAC;
453 break;
454 default:
455 ALOGE("%s: Unsupported audio format", __func__);
456 }
457
458 return id;
459}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800460
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800461int enable_audio_route(struct audio_device *adev,
462 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800463{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700464 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800465 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800466
467 if (usecase == NULL)
468 return -EINVAL;
469
470 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
471
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800472 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700473 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800474 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700475 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800476
477 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500478 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Eric Laurent2e140aa2016-06-30 17:14:46 -0700479 ALOGD("%s: usecase(%d) apply and update mixer path: %s", __func__, usecase->id, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700480 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800481
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800482 ALOGV("%s: exit", __func__);
483 return 0;
484}
485
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800486int disable_audio_route(struct audio_device *adev,
487 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800488{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700489 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800490 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800491
492 if (usecase == NULL)
493 return -EINVAL;
494
495 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700496 if (usecase->type == PCM_CAPTURE)
497 snd_device = usecase->in_snd_device;
498 else
499 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800500 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500501 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Eric Laurent2e140aa2016-06-30 17:14:46 -0700502 ALOGD("%s: usecase(%d) reset and update mixer path: %s", __func__, usecase->id, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700503 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800504
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800505 ALOGV("%s: exit", __func__);
506 return 0;
507}
508
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800509int enable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700510 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800511{
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700512 int i, num_devices = 0;
513 snd_device_t new_snd_devices[2];
vivek mehtade4849c2016-03-03 17:23:38 -0800514 int ret_val = -EINVAL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800515 if (snd_device < SND_DEVICE_MIN ||
516 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800517 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
vivek mehtade4849c2016-03-03 17:23:38 -0800518 goto on_error;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800519 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700520
Ravi Kumar Alamanda5a95ff62015-08-31 17:42:44 -0700521 platform_send_audio_calibration(adev->platform, snd_device);
522
vivek mehtade4849c2016-03-03 17:23:38 -0800523 if (adev->snd_dev_ref_cnt[snd_device] >= 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700524 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700525 __func__, snd_device, platform_get_snd_device_name(snd_device));
vivek mehtade4849c2016-03-03 17:23:38 -0800526 goto on_success;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700527 }
528
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700529 /* due to the possibility of calibration overwrite between listen
530 and audio, notify sound trigger hal before audio calibration is sent */
531 audio_extn_sound_trigger_update_device_status(snd_device,
532 ST_EVENT_SND_DEVICE_BUSY);
533
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700534 if (audio_extn_spkr_prot_is_enabled())
535 audio_extn_spkr_prot_calib_cancel(adev);
536
zhaoyang yin4211fad2015-06-04 21:13:25 +0800537 audio_extn_dsm_feedback_enable(adev, snd_device, true);
538
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700539 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
540 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
541 audio_extn_spkr_prot_is_enabled()) {
542 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
vivek mehtade4849c2016-03-03 17:23:38 -0800543 goto on_error;
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700544 }
545 if (audio_extn_spkr_prot_start_processing(snd_device)) {
546 ALOGE("%s: spkr_start_processing failed", __func__);
vivek mehtade4849c2016-03-03 17:23:38 -0800547 goto on_error;
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700548 }
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700549 } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
550 for (i = 0; i < num_devices; i++) {
551 enable_snd_device(adev, new_snd_devices[i]);
552 }
vivek mehtab6506412015-08-07 16:55:17 -0700553 platform_set_speaker_gain_in_combo(adev, snd_device, true);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700554 } else {
vivek mehtade4849c2016-03-03 17:23:38 -0800555 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
556 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
557 ALOGE(" %s: Invalid sound device returned", __func__);
558 goto on_error;
559 }
Ed Tam70b5c142016-03-21 19:14:29 -0700560
Eric Laurent2e140aa2016-06-30 17:14:46 -0700561 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
vivek mehtade4849c2016-03-03 17:23:38 -0800562 audio_route_apply_and_update_path(adev->audio_route, device_name);
563 }
564on_success:
565 adev->snd_dev_ref_cnt[snd_device]++;
566 ret_val = 0;
567on_error:
568 return ret_val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800569}
570
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800571int disable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700572 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800573{
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700574 int i, num_devices = 0;
575 snd_device_t new_snd_devices[2];
576
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800577 if (snd_device < SND_DEVICE_MIN ||
578 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800579 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800580 return -EINVAL;
581 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700582 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
583 ALOGE("%s: device ref cnt is already 0", __func__);
584 return -EINVAL;
585 }
586 adev->snd_dev_ref_cnt[snd_device]--;
587 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
zhaoyang yin4211fad2015-06-04 21:13:25 +0800588 audio_extn_dsm_feedback_enable(adev, snd_device, false);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700589 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
590 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
591 audio_extn_spkr_prot_is_enabled()) {
592 audio_extn_spkr_prot_stop_processing(snd_device);
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700593 } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
594 for (i = 0; i < num_devices; i++) {
595 disable_snd_device(adev, new_snd_devices[i]);
596 }
vivek mehtab6506412015-08-07 16:55:17 -0700597 platform_set_speaker_gain_in_combo(adev, snd_device, false);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700598 } else {
vivek mehtade4849c2016-03-03 17:23:38 -0800599 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
600 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
601 ALOGE(" %s: Invalid sound device returned", __func__);
602 return -EINVAL;
603 }
604
Eric Laurent2e140aa2016-06-30 17:14:46 -0700605 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
vivek mehtade4849c2016-03-03 17:23:38 -0800606 audio_route_reset_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700607 }
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700608 audio_extn_sound_trigger_update_device_status(snd_device,
609 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700610 }
vivek mehtab6506412015-08-07 16:55:17 -0700611
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800612 return 0;
613}
614
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700615static void check_and_route_playback_usecases(struct audio_device *adev,
616 struct audio_usecase *uc_info,
617 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700618{
619 struct listnode *node;
620 struct audio_usecase *usecase;
621 bool switch_device[AUDIO_USECASE_MAX];
622 int i, num_uc_to_switch = 0;
623
624 /*
625 * This function is to make sure that all the usecases that are active on
626 * the hardware codec backend are always routed to any one device that is
627 * handled by the hardware codec.
628 * For example, if low-latency and deep-buffer usecases are currently active
629 * on speaker and out_set_parameters(headset) is received on low-latency
630 * output, then we have to make sure deep-buffer is also switched to headset,
631 * because of the limitation that both the devices cannot be enabled
632 * at the same time as they share the same backend.
633 */
634 /* Disable all the usecases on the shared backend other than the
635 specified usecase */
636 for (i = 0; i < AUDIO_USECASE_MAX; i++)
637 switch_device[i] = false;
638
639 list_for_each(node, &adev->usecase_list) {
640 usecase = node_to_item(node, struct audio_usecase, list);
641 if (usecase->type != PCM_CAPTURE &&
642 usecase != uc_info &&
643 usecase->out_snd_device != snd_device &&
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700644 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND &&
645 platform_check_backends_match(snd_device, usecase->out_snd_device)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700646 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
647 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700648 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700649 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700650 switch_device[usecase->id] = true;
651 num_uc_to_switch++;
652 }
653 }
654
655 if (num_uc_to_switch) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700656 list_for_each(node, &adev->usecase_list) {
657 usecase = node_to_item(node, struct audio_usecase, list);
658 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700659 disable_snd_device(adev, usecase->out_snd_device);
sangwon.jeon866d5ff2013-10-17 21:42:50 +0900660 }
661 }
662
663 list_for_each(node, &adev->usecase_list) {
664 usecase = node_to_item(node, struct audio_usecase, list);
665 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700666 enable_snd_device(adev, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700667 }
668 }
669
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700670 /* Re-route all the usecases on the shared backend other than the
671 specified usecase to new snd devices */
672 list_for_each(node, &adev->usecase_list) {
673 usecase = node_to_item(node, struct audio_usecase, list);
674 /* Update the out_snd_device only before enabling the audio route */
675 if (switch_device[usecase->id] ) {
676 usecase->out_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700677 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700678 }
679 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700680 }
681}
682
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700683static void check_and_route_capture_usecases(struct audio_device *adev,
684 struct audio_usecase *uc_info,
685 snd_device_t snd_device)
686{
687 struct listnode *node;
688 struct audio_usecase *usecase;
689 bool switch_device[AUDIO_USECASE_MAX];
690 int i, num_uc_to_switch = 0;
691
vivek mehta4ed66e62016-04-15 23:33:34 -0700692 platform_check_and_set_capture_backend_cfg(adev, uc_info, snd_device);
693
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700694 /*
695 * This function is to make sure that all the active capture usecases
696 * are always routed to the same input sound device.
697 * For example, if audio-record and voice-call usecases are currently
698 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
699 * is received for voice call then we have to make sure that audio-record
700 * usecase is also switched to earpiece i.e. voice-dmic-ef,
701 * because of the limitation that two devices cannot be enabled
702 * at the same time if they share the same backend.
703 */
704 for (i = 0; i < AUDIO_USECASE_MAX; i++)
705 switch_device[i] = false;
706
707 list_for_each(node, &adev->usecase_list) {
708 usecase = node_to_item(node, struct audio_usecase, list);
709 if (usecase->type != PCM_PLAYBACK &&
710 usecase != uc_info &&
Anish Kumarff864712015-06-03 13:35:11 -0700711 usecase->in_snd_device != snd_device &&
712 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700713 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
714 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700715 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700716 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700717 switch_device[usecase->id] = true;
718 num_uc_to_switch++;
719 }
720 }
721
722 if (num_uc_to_switch) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700723 list_for_each(node, &adev->usecase_list) {
724 usecase = node_to_item(node, struct audio_usecase, list);
725 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700726 disable_snd_device(adev, usecase->in_snd_device);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700727 }
728 }
729
730 list_for_each(node, &adev->usecase_list) {
731 usecase = node_to_item(node, struct audio_usecase, list);
732 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700733 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700734 }
735 }
736
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700737 /* Re-route all the usecases on the shared backend other than the
738 specified usecase to new snd devices */
739 list_for_each(node, &adev->usecase_list) {
740 usecase = node_to_item(node, struct audio_usecase, list);
741 /* Update the in_snd_device only before enabling the audio route */
742 if (switch_device[usecase->id] ) {
743 usecase->in_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700744 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700745 }
746 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700747 }
748}
749
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800750/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700751static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800752{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700753 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700754 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800755
756 switch (channels) {
757 /*
758 * Do not handle stereo output in Multi-channel cases
759 * Stereo case is handled in normal playback path
760 */
761 case 6:
762 ALOGV("%s: HDMI supports 5.1", __func__);
763 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
764 break;
765 case 8:
766 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
767 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
768 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
769 break;
770 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700771 ALOGE("HDMI does not support multi channel playback");
772 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800773 break;
774 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700775 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800776}
777
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700778static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
779{
780 struct audio_usecase *usecase;
781 struct listnode *node;
782
783 list_for_each(node, &adev->usecase_list) {
784 usecase = node_to_item(node, struct audio_usecase, list);
785 if (usecase->type == VOICE_CALL) {
786 ALOGV("%s: usecase id %d", __func__, usecase->id);
787 return usecase->id;
788 }
789 }
790 return USECASE_INVALID;
791}
792
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800793struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
794 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700795{
796 struct audio_usecase *usecase;
797 struct listnode *node;
798
799 list_for_each(node, &adev->usecase_list) {
800 usecase = node_to_item(node, struct audio_usecase, list);
801 if (usecase->id == uc_id)
802 return usecase;
803 }
804 return NULL;
805}
806
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800807int select_devices(struct audio_device *adev,
808 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800809{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800810 snd_device_t out_snd_device = SND_DEVICE_NONE;
811 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700812 struct audio_usecase *usecase = NULL;
813 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800814 struct audio_usecase *hfp_usecase = NULL;
815 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800816 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700817 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800818
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700819 usecase = get_usecase_from_list(adev, uc_id);
820 if (usecase == NULL) {
821 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
822 return -EINVAL;
823 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800824
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800825 if ((usecase->type == VOICE_CALL) ||
826 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700827 out_snd_device = platform_get_output_snd_device(adev->platform,
828 usecase->stream.out->devices);
829 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700830 usecase->devices = usecase->stream.out->devices;
831 } else {
832 /*
833 * If the voice call is active, use the sound devices of voice call usecase
834 * so that it would not result any device switch. All the usecases will
835 * be switched to new device when select_devices() is called for voice call
836 * usecase. This is to avoid switching devices for voice call when
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700837 * check_and_route_playback_usecases() is called below.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700838 */
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700839 if (voice_is_in_call(adev)) {
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700840 vc_usecase = get_usecase_from_list(adev,
841 get_voice_usecase_id_from_list(adev));
842 if ((vc_usecase != NULL) &&
843 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
844 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700845 in_snd_device = vc_usecase->in_snd_device;
846 out_snd_device = vc_usecase->out_snd_device;
847 }
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800848 } else if (audio_extn_hfp_is_active(adev)) {
849 hfp_ucid = audio_extn_hfp_get_usecase();
850 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
851 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
852 in_snd_device = hfp_usecase->in_snd_device;
853 out_snd_device = hfp_usecase->out_snd_device;
854 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700855 }
856 if (usecase->type == PCM_PLAYBACK) {
857 usecase->devices = usecase->stream.out->devices;
858 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700859 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700860 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700861 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700862 if (usecase->stream.out == adev->primary_output &&
863 adev->active_input &&
Eric Laurent50a38ed2015-10-14 18:48:06 -0700864 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
865 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800866 out_snd_device != usecase->out_snd_device) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700867 select_devices(adev, adev->active_input->usecase);
868 }
869 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700870 } else if (usecase->type == PCM_CAPTURE) {
871 usecase->devices = usecase->stream.in->device;
872 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700873 if (in_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700874 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Eric Laurent50a38ed2015-10-14 18:48:06 -0700875 if (adev->active_input &&
876 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
877 adev->mode == AUDIO_MODE_IN_COMMUNICATION)) {
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800878 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda04643592015-09-24 19:17:26 -0700879 if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
880 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
881 } else if (adev->primary_output) {
882 out_device = adev->primary_output->devices;
883 }
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700884 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700885 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700886 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700887 }
888 }
889
890 if (out_snd_device == usecase->out_snd_device &&
891 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800892 return 0;
893 }
894
Eric Laurent2bafff12016-03-17 12:17:23 -0700895 if (out_snd_device != SND_DEVICE_NONE &&
896 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
897 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
898 __func__,
899 use_case_table[uc_id],
900 adev->last_logged_snd_device[uc_id][0],
901 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
902 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
903 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
904 -1,
905 out_snd_device,
906 platform_get_snd_device_name(out_snd_device),
907 platform_get_snd_device_acdb_id(out_snd_device));
908 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
909 }
910 if (in_snd_device != SND_DEVICE_NONE &&
911 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
912 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
913 __func__,
914 use_case_table[uc_id],
915 adev->last_logged_snd_device[uc_id][1],
916 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
917 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
918 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
919 -1,
920 in_snd_device,
921 platform_get_snd_device_name(in_snd_device),
922 platform_get_snd_device_acdb_id(in_snd_device));
923 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
924 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800925
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800926 /*
927 * Limitation: While in call, to do a device switch we need to disable
928 * and enable both RX and TX devices though one of them is same as current
929 * device.
930 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700931 if ((usecase->type == VOICE_CALL) &&
932 (usecase->in_snd_device != SND_DEVICE_NONE) &&
933 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700934 status = platform_switch_voice_call_device_pre(adev->platform);
vivek mehta765eb642015-08-07 19:46:06 -0700935 /* Disable sidetone only if voice call already exists */
936 if (voice_is_call_state_active(adev))
937 voice_set_sidetone(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800938 }
939
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700940 /* Disable current sound devices */
941 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700942 disable_audio_route(adev, usecase);
943 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800944 }
945
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700946 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700947 disable_audio_route(adev, usecase);
948 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800949 }
950
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700951 /* Applicable only on the targets that has external modem.
952 * New device information should be sent to modem before enabling
953 * the devices to reduce in-call device switch time.
954 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700955 if ((usecase->type == VOICE_CALL) &&
956 (usecase->in_snd_device != SND_DEVICE_NONE) &&
957 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700958 status = platform_switch_voice_call_enable_device_config(adev->platform,
959 out_snd_device,
960 in_snd_device);
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700961 }
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700962
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700963 /* Enable new sound devices */
964 if (out_snd_device != SND_DEVICE_NONE) {
965 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700966 check_and_route_playback_usecases(adev, usecase, out_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700967 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800968 }
969
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700970 if (in_snd_device != SND_DEVICE_NONE) {
971 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700972 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700973 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700974
Eric Laurentb23d5282013-05-14 15:27:20 -0700975 if (usecase->type == VOICE_CALL)
976 status = platform_switch_voice_call_device_post(adev->platform,
977 out_snd_device,
978 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800979
sangwoo170731f2013-06-08 15:36:36 +0900980 usecase->in_snd_device = in_snd_device;
981 usecase->out_snd_device = out_snd_device;
982
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700983 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900984
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700985 /* Applicable only on the targets that has external modem.
986 * Enable device command should be sent to modem only after
987 * enabling voice call mixer controls
988 */
vivek mehta765eb642015-08-07 19:46:06 -0700989 if (usecase->type == VOICE_CALL) {
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700990 status = platform_switch_voice_call_usecase_route_post(adev->platform,
991 out_snd_device,
992 in_snd_device);
vivek mehta765eb642015-08-07 19:46:06 -0700993 /* Enable sidetone only if voice call already exists */
994 if (voice_is_call_state_active(adev))
995 voice_set_sidetone(adev, out_snd_device, true);
996 }
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700997
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800998 return status;
999}
1000
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001001static int stop_input_stream(struct stream_in *in)
1002{
1003 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001004 struct audio_usecase *uc_info;
1005 struct audio_device *adev = in->dev;
1006
Eric Laurentc8400632013-02-14 19:04:54 -08001007 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001008
Eric Laurent994a6932013-07-17 11:51:42 -07001009 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001010 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001011 uc_info = get_usecase_from_list(adev, in->usecase);
1012 if (uc_info == NULL) {
1013 ALOGE("%s: Could not find the usecase (%d) in the list",
1014 __func__, in->usecase);
1015 return -EINVAL;
1016 }
1017
Eric Laurent150dbfe2013-02-27 14:31:02 -08001018 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001019 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001020
1021 /* 2. Disable the tx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001022 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001023
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001024 list_remove(&uc_info->list);
1025 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026
Eric Laurent994a6932013-07-17 11:51:42 -07001027 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001028 return ret;
1029}
1030
1031int start_input_stream(struct stream_in *in)
1032{
1033 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08001034 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001035 struct audio_usecase *uc_info;
1036 struct audio_device *adev = in->dev;
1037
Eric Laurent994a6932013-07-17 11:51:42 -07001038 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07001039
1040 if (in->card_status == CARD_STATUS_OFFLINE ||
1041 adev->card_status == CARD_STATUS_OFFLINE) {
1042 ALOGW("in->card_status or adev->card_status offline, try again");
1043 ret = -EAGAIN;
1044 goto error_config;
1045 }
1046
Eric Laurentb23d5282013-05-14 15:27:20 -07001047 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001048 if (in->pcm_device_id < 0) {
1049 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
1050 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08001051 ret = -EINVAL;
1052 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001053 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001054
1055 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001056 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1057 uc_info->id = in->usecase;
1058 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001059 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001060 uc_info->devices = in->device;
1061 uc_info->in_snd_device = SND_DEVICE_NONE;
1062 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001063
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001064 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001065
1066 audio_extn_perf_lock_acquire();
1067
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001068 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001069
Eric Laurentc8400632013-02-14 19:04:54 -08001070 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001071 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001072
Andy Hung6ebe5962016-01-15 17:46:57 -08001073 unsigned int flags = PCM_IN | PCM_MONOTONIC;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001074 unsigned int pcm_open_retry_count = 0;
1075
1076 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1077 flags |= PCM_MMAP | PCM_NOIRQ;
1078 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George03c40102016-01-29 17:57:48 -08001079 } else if (in->realtime) {
1080 flags |= PCM_MMAP | PCM_NOIRQ;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001081 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001082
1083 while (1) {
1084 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
1085 flags, &in->config);
1086 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
1087 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
1088 if (in->pcm != NULL) {
1089 pcm_close(in->pcm);
1090 in->pcm = NULL;
1091 }
1092 if (pcm_open_retry_count-- == 0) {
1093 ret = -EIO;
1094 goto error_open;
1095 }
1096 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1097 continue;
1098 }
1099 break;
1100 }
1101
Ravi Kumar Alamanda50919a72015-10-02 09:37:33 -07001102 ALOGV("%s: pcm_prepare", __func__);
1103 ret = pcm_prepare(in->pcm);
1104 if (ret < 0) {
1105 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
1106 pcm_close(in->pcm);
1107 in->pcm = NULL;
1108 goto error_open;
1109 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08001110 if (in->realtime) {
1111 ret = pcm_start(in->pcm);
Haynes Mathew Georgefbe87312016-08-01 19:29:27 -07001112 if (ret < 0) {
1113 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
1114 pcm_close(in->pcm);
1115 in->pcm = NULL;
1116 goto error_open;
1117 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08001118 }
Haynes Mathew Georgefbe87312016-08-01 19:29:27 -07001119 register_in_stream(in);
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001120 audio_extn_perf_lock_release();
Eric Laurent994a6932013-07-17 11:51:42 -07001121 ALOGV("%s: exit", __func__);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07001122
Eric Laurentc8400632013-02-14 19:04:54 -08001123 return ret;
1124
1125error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001126 stop_input_stream(in);
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001127 audio_extn_perf_lock_release();
Eric Laurentc8400632013-02-14 19:04:54 -08001128
1129error_config:
1130 adev->active_input = NULL;
Eric Laurent2bafff12016-03-17 12:17:23 -07001131 ALOGW("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -08001132
1133 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001134}
1135
Eric Laurenta1478072015-09-21 17:21:52 -07001136void lock_input_stream(struct stream_in *in)
1137{
1138 pthread_mutex_lock(&in->pre_lock);
1139 pthread_mutex_lock(&in->lock);
1140 pthread_mutex_unlock(&in->pre_lock);
1141}
1142
1143void lock_output_stream(struct stream_out *out)
1144{
1145 pthread_mutex_lock(&out->pre_lock);
1146 pthread_mutex_lock(&out->lock);
1147 pthread_mutex_unlock(&out->pre_lock);
1148}
1149
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001150/* must be called with out->lock locked */
1151static int send_offload_cmd_l(struct stream_out* out, int command)
1152{
1153 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
1154
1155 ALOGVV("%s %d", __func__, command);
1156
1157 cmd->cmd = command;
1158 list_add_tail(&out->offload_cmd_list, &cmd->node);
1159 pthread_cond_signal(&out->offload_cond);
1160 return 0;
1161}
1162
1163/* must be called iwth out->lock locked */
1164static void stop_compressed_output_l(struct stream_out *out)
1165{
1166 out->offload_state = OFFLOAD_STATE_IDLE;
1167 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001168 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001169 if (out->compr != NULL) {
1170 compress_stop(out->compr);
1171 while (out->offload_thread_blocked) {
1172 pthread_cond_wait(&out->cond, &out->lock);
1173 }
1174 }
1175}
1176
1177static void *offload_thread_loop(void *context)
1178{
1179 struct stream_out *out = (struct stream_out *) context;
1180 struct listnode *item;
1181
1182 out->offload_state = OFFLOAD_STATE_IDLE;
1183 out->playback_started = 0;
1184
1185 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1186 set_sched_policy(0, SP_FOREGROUND);
1187 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1188
1189 ALOGV("%s", __func__);
Eric Laurenta1478072015-09-21 17:21:52 -07001190 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001191 for (;;) {
1192 struct offload_cmd *cmd = NULL;
1193 stream_callback_event_t event;
1194 bool send_callback = false;
1195
1196 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1197 __func__, list_empty(&out->offload_cmd_list),
1198 out->offload_state);
1199 if (list_empty(&out->offload_cmd_list)) {
1200 ALOGV("%s SLEEPING", __func__);
1201 pthread_cond_wait(&out->offload_cond, &out->lock);
1202 ALOGV("%s RUNNING", __func__);
1203 continue;
1204 }
1205
1206 item = list_head(&out->offload_cmd_list);
1207 cmd = node_to_item(item, struct offload_cmd, node);
1208 list_remove(item);
1209
1210 ALOGVV("%s STATE %d CMD %d out->compr %p",
1211 __func__, out->offload_state, cmd->cmd, out->compr);
1212
1213 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1214 free(cmd);
1215 break;
1216 }
1217
1218 if (out->compr == NULL) {
1219 ALOGE("%s: Compress handle is NULL", __func__);
Andy Hung68f55fd2016-04-21 11:51:11 -07001220 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001221 pthread_cond_signal(&out->cond);
1222 continue;
1223 }
1224 out->offload_thread_blocked = true;
1225 pthread_mutex_unlock(&out->lock);
1226 send_callback = false;
1227 switch(cmd->cmd) {
1228 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
1229 compress_wait(out->compr, -1);
1230 send_callback = true;
1231 event = STREAM_CBK_EVENT_WRITE_READY;
1232 break;
1233 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001234 compress_next_track(out->compr);
1235 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001236 send_callback = true;
1237 event = STREAM_CBK_EVENT_DRAIN_READY;
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -08001238 /* Resend the metadata for next iteration */
1239 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001240 break;
1241 case OFFLOAD_CMD_DRAIN:
1242 compress_drain(out->compr);
1243 send_callback = true;
1244 event = STREAM_CBK_EVENT_DRAIN_READY;
1245 break;
Haynes Mathew George3ddd3bd2016-07-07 20:01:53 -07001246 case OFFLOAD_CMD_ERROR:
1247 send_callback = true;
1248 event = STREAM_CBK_EVENT_ERROR;
1249 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001250 default:
1251 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1252 break;
1253 }
Eric Laurenta1478072015-09-21 17:21:52 -07001254 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001255 out->offload_thread_blocked = false;
1256 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001257 if (send_callback) {
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -08001258 ALOGVV("%s: sending offload_callback event %d", __func__, event);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001259 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001260 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001261 free(cmd);
1262 }
1263
1264 pthread_cond_signal(&out->cond);
1265 while (!list_empty(&out->offload_cmd_list)) {
1266 item = list_head(&out->offload_cmd_list);
1267 list_remove(item);
1268 free(node_to_item(item, struct offload_cmd, node));
1269 }
1270 pthread_mutex_unlock(&out->lock);
1271
1272 return NULL;
1273}
1274
1275static int create_offload_callback_thread(struct stream_out *out)
1276{
1277 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1278 list_init(&out->offload_cmd_list);
1279 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1280 offload_thread_loop, out);
1281 return 0;
1282}
1283
1284static int destroy_offload_callback_thread(struct stream_out *out)
1285{
Eric Laurenta1478072015-09-21 17:21:52 -07001286 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001287 stop_compressed_output_l(out);
1288 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1289
1290 pthread_mutex_unlock(&out->lock);
1291 pthread_join(out->offload_thread, (void **) NULL);
1292 pthread_cond_destroy(&out->offload_cond);
1293
1294 return 0;
1295}
1296
Eric Laurent07eeafd2013-10-06 12:52:49 -07001297static bool allow_hdmi_channel_config(struct audio_device *adev)
1298{
1299 struct listnode *node;
1300 struct audio_usecase *usecase;
1301 bool ret = true;
1302
1303 list_for_each(node, &adev->usecase_list) {
1304 usecase = node_to_item(node, struct audio_usecase, list);
1305 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1306 /*
1307 * If voice call is already existing, do not proceed further to avoid
1308 * disabling/enabling both RX and TX devices, CSD calls, etc.
1309 * Once the voice call done, the HDMI channels can be configured to
1310 * max channels of remaining use cases.
1311 */
1312 if (usecase->id == USECASE_VOICE_CALL) {
Joe Onorato188b6222016-03-01 11:02:27 -08001313 ALOGV("%s: voice call is active, no change in HDMI channels",
Eric Laurent07eeafd2013-10-06 12:52:49 -07001314 __func__);
1315 ret = false;
1316 break;
1317 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
Joe Onorato188b6222016-03-01 11:02:27 -08001318 ALOGV("%s: multi channel playback is active, "
Eric Laurent07eeafd2013-10-06 12:52:49 -07001319 "no change in HDMI channels", __func__);
1320 ret = false;
1321 break;
1322 }
1323 }
1324 }
1325 return ret;
1326}
1327
1328static int check_and_set_hdmi_channels(struct audio_device *adev,
1329 unsigned int channels)
1330{
1331 struct listnode *node;
1332 struct audio_usecase *usecase;
1333
1334 /* Check if change in HDMI channel config is allowed */
1335 if (!allow_hdmi_channel_config(adev))
1336 return 0;
1337
1338 if (channels == adev->cur_hdmi_channels) {
Joe Onorato188b6222016-03-01 11:02:27 -08001339 ALOGV("%s: Requested channels are same as current", __func__);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001340 return 0;
1341 }
1342
1343 platform_set_hdmi_channels(adev->platform, channels);
1344 adev->cur_hdmi_channels = channels;
1345
1346 /*
1347 * Deroute all the playback streams routed to HDMI so that
1348 * the back end is deactivated. Note that backend will not
1349 * be deactivated if any one stream is connected to it.
1350 */
1351 list_for_each(node, &adev->usecase_list) {
1352 usecase = node_to_item(node, struct audio_usecase, list);
1353 if (usecase->type == PCM_PLAYBACK &&
1354 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001355 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001356 }
1357 }
1358
1359 /*
1360 * Enable all the streams disabled above. Now the HDMI backend
1361 * will be activated with new channel configuration
1362 */
1363 list_for_each(node, &adev->usecase_list) {
1364 usecase = node_to_item(node, struct audio_usecase, list);
1365 if (usecase->type == PCM_PLAYBACK &&
1366 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001367 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001368 }
1369 }
1370
1371 return 0;
1372}
1373
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001374static int stop_output_stream(struct stream_out *out)
1375{
1376 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001377 struct audio_usecase *uc_info;
1378 struct audio_device *adev = out->dev;
1379
Eric Laurent994a6932013-07-17 11:51:42 -07001380 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001381 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001382 uc_info = get_usecase_from_list(adev, out->usecase);
1383 if (uc_info == NULL) {
1384 ALOGE("%s: Could not find the usecase (%d) in the list",
1385 __func__, out->usecase);
1386 return -EINVAL;
1387 }
1388
Haynes Mathew George41f86652014-06-17 14:22:15 -07001389 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1390 if (adev->visualizer_stop_output != NULL)
1391 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1392 if (adev->offload_effects_stop_output != NULL)
1393 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1394 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001395
Eric Laurent150dbfe2013-02-27 14:31:02 -08001396 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001397 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001398
1399 /* 2. Disable the rx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001400 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001401
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001402 list_remove(&uc_info->list);
1403 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404
Eric Laurent0499d4f2014-08-25 22:39:29 -05001405 audio_extn_extspk_update(adev->extspk);
1406
Eric Laurent07eeafd2013-10-06 12:52:49 -07001407 /* Must be called after removing the usecase from list */
1408 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1409 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1410
Eric Laurent994a6932013-07-17 11:51:42 -07001411 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001412 return ret;
1413}
1414
1415int start_output_stream(struct stream_out *out)
1416{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001417 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001418 struct audio_usecase *uc_info;
1419 struct audio_device *adev = out->dev;
1420
Eric Laurent994a6932013-07-17 11:51:42 -07001421 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001422 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07001423
1424 if (out->card_status == CARD_STATUS_OFFLINE ||
1425 adev->card_status == CARD_STATUS_OFFLINE) {
1426 ALOGW("out->card_status or adev->card_status offline, try again");
1427 ret = -EAGAIN;
1428 goto error_config;
1429 }
1430
Eric Laurentb23d5282013-05-14 15:27:20 -07001431 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001432 if (out->pcm_device_id < 0) {
1433 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1434 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001435 ret = -EINVAL;
1436 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001437 }
1438
1439 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1440 uc_info->id = out->usecase;
1441 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001442 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001443 uc_info->devices = out->devices;
1444 uc_info->in_snd_device = SND_DEVICE_NONE;
1445 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001446
Eric Laurent07eeafd2013-10-06 12:52:49 -07001447 /* This must be called before adding this usecase to the list */
1448 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1449 check_and_set_hdmi_channels(adev, out->config.channels);
1450
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001451 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001452
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001453 audio_extn_perf_lock_acquire();
1454
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001455 select_devices(adev, out->usecase);
1456
Eric Laurent0499d4f2014-08-25 22:39:29 -05001457 audio_extn_extspk_update(adev->extspk);
1458
Andy Hung31aca912014-03-20 17:14:59 -07001459 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001460 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001461 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001462 unsigned int flags = PCM_OUT;
1463 unsigned int pcm_open_retry_count = 0;
Haynes Mathew George03c40102016-01-29 17:57:48 -08001464
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001465 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1466 flags |= PCM_MMAP | PCM_NOIRQ;
1467 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George03c40102016-01-29 17:57:48 -08001468 } else if (out->realtime) {
1469 flags |= PCM_MMAP | PCM_NOIRQ;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001470 } else
1471 flags |= PCM_MONOTONIC;
1472
1473 while (1) {
1474 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1475 flags, &out->config);
1476 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
1477 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1478 if (out->pcm != NULL) {
1479 pcm_close(out->pcm);
1480 out->pcm = NULL;
1481 }
1482 if (pcm_open_retry_count-- == 0) {
1483 ret = -EIO;
1484 goto error_open;
1485 }
1486 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1487 continue;
1488 }
1489 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001490 }
Ravi Kumar Alamanda50919a72015-10-02 09:37:33 -07001491 ALOGV("%s: pcm_prepare", __func__);
1492 if (pcm_is_ready(out->pcm)) {
1493 ret = pcm_prepare(out->pcm);
1494 if (ret < 0) {
1495 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
1496 pcm_close(out->pcm);
1497 out->pcm = NULL;
1498 goto error_open;
1499 }
1500 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001501 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001502 out->pcm = NULL;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001503 out->compr = compress_open(adev->snd_card, out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001504 COMPRESS_IN, &out->compr_config);
1505 if (out->compr && !is_compress_ready(out->compr)) {
1506 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1507 compress_close(out->compr);
1508 out->compr = NULL;
1509 ret = -EIO;
1510 goto error_open;
1511 }
1512 if (out->offload_callback)
1513 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001514
1515 if (adev->visualizer_start_output != NULL)
Haynes Mathew George41f86652014-06-17 14:22:15 -07001516 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1517 if (adev->offload_effects_start_output != NULL)
1518 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001519 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08001520 ret = 0;
Haynes Mathew George03c40102016-01-29 17:57:48 -08001521 if (out->realtime) {
1522 ret = pcm_start(out->pcm);
Haynes Mathew Georgefbe87312016-08-01 19:29:27 -07001523 if (ret < 0) {
1524 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
1525 pcm_close(out->pcm);
1526 out->pcm = NULL;
1527 goto error_open;
1528 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08001529 }
Haynes Mathew Georgefbe87312016-08-01 19:29:27 -07001530 register_out_stream(out);
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001531 audio_extn_perf_lock_release();
Eric Laurent994a6932013-07-17 11:51:42 -07001532 ALOGV("%s: exit", __func__);
Haynes Mathew George03c40102016-01-29 17:57:48 -08001533 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001534error_open:
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07001535 audio_extn_perf_lock_release();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001536 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001537error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001538 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001539}
1540
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001541static int check_input_parameters(uint32_t sample_rate,
1542 audio_format_t format,
1543 int channel_count)
1544{
vivek mehta4ed66e62016-04-15 23:33:34 -07001545 if ((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT)) {
vivek mehtadae44712015-07-27 14:13:18 -07001546 ALOGE("%s: unsupported AUDIO FORMAT (%d) ", __func__, format);
1547 return -EINVAL;
1548 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001549
vivek mehtadae44712015-07-27 14:13:18 -07001550 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > MAX_CHANNEL_COUNT)) {
Jean-Michel Trivic0750692015-10-12 12:12:32 -07001551 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
vivek mehtadae44712015-07-27 14:13:18 -07001552 channel_count, MIN_CHANNEL_COUNT, MAX_CHANNEL_COUNT);
1553 return -EINVAL;
1554 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001555
1556 switch (sample_rate) {
1557 case 8000:
1558 case 11025:
1559 case 12000:
1560 case 16000:
1561 case 22050:
1562 case 24000:
1563 case 32000:
1564 case 44100:
1565 case 48000:
1566 break;
1567 default:
vivek mehtadae44712015-07-27 14:13:18 -07001568 ALOGE("%s: unsupported (%d) samplerate passed ", __func__, sample_rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001569 return -EINVAL;
1570 }
1571
1572 return 0;
1573}
1574
1575static size_t get_input_buffer_size(uint32_t sample_rate,
1576 audio_format_t format,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001577 int channel_count,
1578 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001579{
1580 size_t size = 0;
1581
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001582 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1583 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001584
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001585 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001586 if (is_low_latency)
Glenn Kasten4f993392014-05-14 07:30:48 -07001587 size = configured_low_latency_capture_period_size;
vivek mehta4ed66e62016-04-15 23:33:34 -07001588
1589 size *= channel_count * audio_bytes_per_sample(format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001590
Glenn Kasten4f993392014-05-14 07:30:48 -07001591 /* make sure the size is multiple of 32 bytes
1592 * At 48 kHz mono 16-bit PCM:
1593 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1594 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1595 */
1596 size += 0x1f;
1597 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001598
1599 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001600}
1601
1602static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1603{
1604 struct stream_out *out = (struct stream_out *)stream;
1605
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001606 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001607}
1608
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001609static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001610{
1611 return -ENOSYS;
1612}
1613
1614static size_t out_get_buffer_size(const struct audio_stream *stream)
1615{
1616 struct stream_out *out = (struct stream_out *)stream;
1617
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001618 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1619 return out->compr_config.fragment_size;
1620 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08001621 return out->config.period_size * out->af_period_multiplier *
Eric Laurentfdf296a2014-07-03 16:41:51 -07001622 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001623}
1624
1625static uint32_t out_get_channels(const struct audio_stream *stream)
1626{
1627 struct stream_out *out = (struct stream_out *)stream;
1628
1629 return out->channel_mask;
1630}
1631
1632static audio_format_t out_get_format(const struct audio_stream *stream)
1633{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001634 struct stream_out *out = (struct stream_out *)stream;
1635
1636 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001637}
1638
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001639static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001640{
1641 return -ENOSYS;
1642}
1643
1644static int out_standby(struct audio_stream *stream)
1645{
1646 struct stream_out *out = (struct stream_out *)stream;
1647 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001648
Eric Laurent994a6932013-07-17 11:51:42 -07001649 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001650 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001651
Eric Laurenta1478072015-09-21 17:21:52 -07001652 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001653 if (!out->standby) {
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07001654 if (adev->adm_deregister_stream)
1655 adev->adm_deregister_stream(adev->adm_data, out->handle);
1656
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001657 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001658 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001659 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1660 if (out->pcm) {
1661 pcm_close(out->pcm);
1662 out->pcm = NULL;
1663 }
1664 } else {
1665 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001666 out->gapless_mdata.encoder_delay = 0;
1667 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001668 if (out->compr != NULL) {
1669 compress_close(out->compr);
1670 out->compr = NULL;
1671 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001672 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001673 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001674 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001675 }
1676 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001677 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001678 return 0;
1679}
1680
Haynes Mathew George3ddd3bd2016-07-07 20:01:53 -07001681static int out_on_error(struct audio_stream *stream)
1682{
1683 struct stream_out *out = (struct stream_out *)stream;
1684 struct audio_device *adev = out->dev;
1685 bool do_standby = false;
1686
1687 lock_output_stream(out);
1688 if (!out->standby) {
1689 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1690 stop_compressed_output_l(out);
1691 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
1692 } else
1693 do_standby = true;
1694 }
1695 pthread_mutex_unlock(&out->lock);
1696
1697 if (do_standby)
1698 return out_standby(&out->stream.common);
1699
1700 return 0;
1701}
1702
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001703static int out_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001704{
1705 return 0;
1706}
1707
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001708static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1709{
1710 int ret = 0;
1711 char value[32];
1712 struct compr_gapless_mdata tmp_mdata;
1713
1714 if (!out || !parms) {
1715 return -EINVAL;
1716 }
1717
1718 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1719 if (ret >= 0) {
1720 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1721 } else {
1722 return -EINVAL;
1723 }
1724
1725 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1726 if (ret >= 0) {
1727 tmp_mdata.encoder_padding = atoi(value);
1728 } else {
1729 return -EINVAL;
1730 }
1731
1732 out->gapless_mdata = tmp_mdata;
1733 out->send_new_metadata = 1;
1734 ALOGV("%s new encoder delay %u and padding %u", __func__,
1735 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1736
1737 return 0;
1738}
1739
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001740static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1741{
1742 return out == adev->primary_output || out == adev->voice_tx_output;
1743}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001744
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001745static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1746{
1747 struct stream_out *out = (struct stream_out *)stream;
1748 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001749 struct audio_usecase *usecase;
1750 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001751 struct str_parms *parms;
1752 char value[32];
1753 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001754 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001755 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001756
Eric Laurent2e140aa2016-06-30 17:14:46 -07001757 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001758 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001759 parms = str_parms_create_str(kvpairs);
1760 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1761 if (ret >= 0) {
1762 val = atoi(value);
Eric Laurenta1478072015-09-21 17:21:52 -07001763 lock_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001764 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001765
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001766 /*
1767 * When HDMI cable is unplugged the music playback is paused and
1768 * the policy manager sends routing=0. But the audioflinger
1769 * continues to write data until standby time (3sec).
1770 * As the HDMI core is turned off, the write gets blocked.
1771 * Avoid this by routing audio to speaker until standby.
1772 */
1773 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1774 val == AUDIO_DEVICE_NONE) {
1775 val = AUDIO_DEVICE_OUT_SPEAKER;
1776 }
1777
1778 /*
1779 * select_devices() call below switches all the usecases on the same
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -07001780 * backend to the new device. Refer to check_and_route_playback_usecases() in
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001781 * the select_devices(). But how do we undo this?
1782 *
1783 * For example, music playback is active on headset (deep-buffer usecase)
1784 * and if we go to ringtones and select a ringtone, low-latency usecase
1785 * will be started on headset+speaker. As we can't enable headset+speaker
1786 * and headset devices at the same time, select_devices() switches the music
1787 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1788 * So when the ringtone playback is completed, how do we undo the same?
1789 *
1790 * We are relying on the out_set_parameters() call on deep-buffer output,
1791 * once the ringtone playback is ended.
1792 * NOTE: We should not check if the current devices are same as new devices.
1793 * Because select_devices() must be called to switch back the music
1794 * playback to headset.
1795 */
Haynes Mathew George03c40102016-01-29 17:57:48 -08001796 audio_devices_t new_dev = val;
1797 if (new_dev != AUDIO_DEVICE_NONE) {
1798 bool same_dev = out->devices == new_dev;
1799 out->devices = new_dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001800
Eric Laurenta7657192014-10-09 21:09:33 -07001801 if (output_drives_call(adev, out)) {
1802 if (!voice_is_in_call(adev)) {
1803 if (adev->mode == AUDIO_MODE_IN_CALL) {
1804 adev->current_call_output = out;
1805 ret = voice_start_call(adev);
1806 }
1807 } else {
1808 adev->current_call_output = out;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001809 voice_update_devices_for_all_voice_usecases(adev);
Eric Laurenta7657192014-10-09 21:09:33 -07001810 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001811 }
vivek mehta0d3637a2016-07-24 09:32:02 -07001812
1813 if (!out->standby) {
1814 if (!same_dev) {
1815 ALOGV("update routing change");
1816 out->routing_change = true;
1817 }
1818 select_devices(adev, out->usecase);
1819 }
1820
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001821 }
1822
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001823 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001824 pthread_mutex_unlock(&out->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05001825
1826 /*handles device and call state changes*/
1827 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001828 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001829
1830 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1831 parse_compress_metadata(out, parms);
1832 }
1833
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001834 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001835 ALOGV("%s: exit: code(%d)", __func__, status);
1836 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001837}
1838
1839static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1840{
1841 struct stream_out *out = (struct stream_out *)stream;
1842 struct str_parms *query = str_parms_create_str(keys);
1843 char *str;
1844 char value[256];
1845 struct str_parms *reply = str_parms_create();
1846 size_t i, j;
1847 int ret;
1848 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001849 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001850 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1851 if (ret >= 0) {
1852 value[0] = '\0';
1853 i = 0;
1854 while (out->supported_channel_masks[i] != 0) {
1855 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1856 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1857 if (!first) {
1858 strcat(value, "|");
1859 }
1860 strcat(value, out_channels_name_to_enum_table[j].name);
1861 first = false;
1862 break;
1863 }
1864 }
1865 i++;
1866 }
1867 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1868 str = str_parms_to_str(reply);
1869 } else {
1870 str = strdup(keys);
1871 }
1872 str_parms_destroy(query);
1873 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001874 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001875 return str;
1876}
1877
1878static uint32_t out_get_latency(const struct audio_stream_out *stream)
1879{
Haynes Mathew George03c40102016-01-29 17:57:48 -08001880 uint32_t hw_delay, period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001881 struct stream_out *out = (struct stream_out *)stream;
1882
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001883 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1884 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
Haynes Mathew George03c40102016-01-29 17:57:48 -08001885 else if (out->realtime) {
1886 // since the buffer won't be filled up faster than realtime,
1887 // return a smaller number
1888 period_ms = (out->af_period_multiplier * out->config.period_size *
1889 1000) / (out->config.rate);
1890 hw_delay = platform_render_latency(out->usecase)/1000;
1891 return period_ms + hw_delay;
1892 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001893
1894 return (out->config.period_count * out->config.period_size * 1000) /
1895 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001896}
1897
1898static int out_set_volume(struct audio_stream_out *stream, float left,
1899 float right)
1900{
Eric Laurenta9024de2013-04-04 09:19:12 -07001901 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001902 int volume[2];
1903
Eric Laurenta9024de2013-04-04 09:19:12 -07001904 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1905 /* only take left channel into account: the API is for stereo anyway */
1906 out->muted = (left == 0.0f);
1907 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001908 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1909 const char *mixer_ctl_name = "Compress Playback Volume";
1910 struct audio_device *adev = out->dev;
1911 struct mixer_ctl *ctl;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001912 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1913 if (!ctl) {
Haynes Mathew George20bcfa82014-06-25 13:37:03 -07001914 /* try with the control based on device id */
1915 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1916 PCM_PLAYBACK);
1917 char ctl_name[128] = {0};
1918 snprintf(ctl_name, sizeof(ctl_name),
1919 "Compress Playback %d Volume", pcm_device_id);
1920 ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name);
1921 if (!ctl) {
1922 ALOGE("%s: Could not get volume ctl mixer cmd", __func__);
1923 return -EINVAL;
1924 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001925 }
1926 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1927 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1928 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1929 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001930 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001931
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001932 return -ENOSYS;
1933}
1934
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07001935// note: this call is safe only if the stream_cb is
1936// removed first in close_output_stream (as is done now).
1937static void out_snd_mon_cb(void * stream, struct str_parms * parms)
1938{
1939 if (!stream || !parms)
1940 return;
1941
1942 struct stream_out *out = (struct stream_out *)stream;
1943 struct audio_device *adev = out->dev;
1944
1945 card_status_t status;
1946 int card;
1947 if (parse_snd_card_status(parms, &card, &status) < 0)
1948 return;
1949
1950 pthread_mutex_lock(&adev->lock);
1951 bool valid_cb = (card == adev->snd_card);
1952 pthread_mutex_unlock(&adev->lock);
1953
1954 if (!valid_cb)
1955 return;
1956
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07001957 lock_output_stream(out);
1958 if (out->card_status != status)
1959 out->card_status = status;
1960 pthread_mutex_unlock(&out->lock);
1961
Haynes Mathew George3ddd3bd2016-07-07 20:01:53 -07001962 ALOGW("out_snd_mon_cb for card %d usecase %s, status %s", card,
1963 use_case_table[out->usecase],
1964 status == CARD_STATUS_OFFLINE ? "offline" : "online");
1965
1966 if (status == CARD_STATUS_OFFLINE)
1967 out_on_error(stream);
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07001968
1969 return;
1970}
1971
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001972#ifdef NO_AUDIO_OUT
1973static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
1974 const void *buffer, size_t bytes)
1975{
1976 struct stream_out *out = (struct stream_out *)stream;
1977
1978 /* No Output device supported other than BT for playback.
1979 * Sleep for the amount of buffer duration
1980 */
Eric Laurenta1478072015-09-21 17:21:52 -07001981 lock_output_stream(out);
Uday Kishore Pasupuletib14d76b2015-11-13 20:06:37 -08001982 usleep(bytes * 1000000 / audio_stream_out_frame_size(&out->stream.common) /
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001983 out_get_sample_rate(&out->stream.common));
1984 pthread_mutex_unlock(&out->lock);
1985 return bytes;
1986}
1987#endif
1988
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001989static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1990 size_t bytes)
1991{
1992 struct stream_out *out = (struct stream_out *)stream;
1993 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001994 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001995
Eric Laurenta1478072015-09-21 17:21:52 -07001996 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001997 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001998 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001999 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002001 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002002 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002003 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002004 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002005 goto exit;
2006 }
vivek mehtafb4d7bd2016-04-29 03:16:47 -07002007
2008 if (last_known_cal_step != -1) {
2009 ALOGD("%s: retry previous failed cal level set", __func__);
2010 audio_hw_send_gain_dep_calibration(last_known_cal_step);
2011 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002012 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002013
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002014 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002015 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
2016 if (out->send_new_metadata) {
2017 ALOGVV("send new gapless metadata");
2018 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
2019 out->send_new_metadata = 0;
2020 }
Eric Laurentb49b3f62016-02-29 17:59:49 -08002021 unsigned int avail;
2022 struct timespec tstamp;
2023 ret = compress_get_hpointer(out->compr, &avail, &tstamp);
2024 /* Do not limit write size if the available frames count is unknown */
2025 if (ret != 0) {
2026 avail = bytes;
2027 }
2028 if (avail == 0) {
2029 ret = 0;
2030 } else {
2031 if (avail > bytes) {
2032 avail = bytes;
2033 }
2034 ret = compress_write(out->compr, buffer, avail);
2035 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %zd",
2036 __func__, avail, ret);
2037 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002038
Eric Laurent6e895242013-09-05 16:10:57 -07002039 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002040 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
2041 }
Eric Laurentb49b3f62016-02-29 17:59:49 -08002042 if (ret > 0 && !out->playback_started) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002043 compress_start(out->compr);
2044 out->playback_started = 1;
2045 out->offload_state = OFFLOAD_STATE_PLAYING;
2046 }
2047 pthread_mutex_unlock(&out->lock);
2048 return ret;
2049 } else {
2050 if (out->pcm) {
2051 if (out->muted)
2052 memset((void *)buffer, 0, bytes);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002053
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002054 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002055
Haynes Mathew George03c40102016-01-29 17:57:48 -08002056 long ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
2057 out->config.rate;
2058 request_out_focus(out, ns);
2059
2060 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
2061 if (use_mmap)
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002062 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002063 else
2064 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002065
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002066 if (ret == 0)
2067 out->written += bytes / (out->config.channels * sizeof(short));
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002068
Haynes Mathew George03c40102016-01-29 17:57:48 -08002069 release_out_focus(out, ns);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002070 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002071 }
2072
2073exit:
2074 pthread_mutex_unlock(&out->lock);
2075
2076 if (ret != 0) {
Haynes Mathew George3ddd3bd2016-07-07 20:01:53 -07002077 out_on_error(&out->stream.common);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002078 if (out->pcm)
vivek mehta1a9b7c02015-06-25 11:49:38 -07002079 ALOGE("%s: error %zu - %s", __func__, ret, pcm_get_error(out->pcm));
Haynes Mathew George3ddd3bd2016-07-07 20:01:53 -07002080 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD)
2081 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
2082 out_get_sample_rate(&out->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002083 }
2084 return bytes;
2085}
2086
2087static int out_get_render_position(const struct audio_stream_out *stream,
2088 uint32_t *dsp_frames)
2089{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002090 struct stream_out *out = (struct stream_out *)stream;
2091 *dsp_frames = 0;
2092 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
Eric Laurenta1478072015-09-21 17:21:52 -07002093 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002094 if (out->compr != NULL) {
Andy Hung7171da22016-03-08 16:58:42 -08002095 unsigned long frames = 0;
2096 // TODO: check return value
2097 compress_get_tstamp(out->compr, &frames, &out->sample_rate);
2098 *dsp_frames = (uint32_t)frames;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002099 ALOGVV("%s rendered frames %d sample_rate %d",
2100 __func__, *dsp_frames, out->sample_rate);
2101 }
2102 pthread_mutex_unlock(&out->lock);
2103 return 0;
2104 } else
2105 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002106}
2107
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002108static int out_add_audio_effect(const struct audio_stream *stream __unused,
2109 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002110{
2111 return 0;
2112}
2113
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002114static int out_remove_audio_effect(const struct audio_stream *stream __unused,
2115 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002116{
2117 return 0;
2118}
2119
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002120static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
2121 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002122{
2123 return -EINVAL;
2124}
2125
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002126static int out_get_presentation_position(const struct audio_stream_out *stream,
2127 uint64_t *frames, struct timespec *timestamp)
2128{
2129 struct stream_out *out = (struct stream_out *)stream;
Andy Hung7171da22016-03-08 16:58:42 -08002130 int ret = -EINVAL;
Eric Laurent949a0892013-09-20 09:20:13 -07002131 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002132
Eric Laurenta1478072015-09-21 17:21:52 -07002133 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002134
Eric Laurent949a0892013-09-20 09:20:13 -07002135 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2136 if (out->compr != NULL) {
Andy Hung7171da22016-03-08 16:58:42 -08002137 // TODO: check return value
Eric Laurent949a0892013-09-20 09:20:13 -07002138 compress_get_tstamp(out->compr, &dsp_frames,
2139 &out->sample_rate);
2140 ALOGVV("%s rendered frames %ld sample_rate %d",
2141 __func__, dsp_frames, out->sample_rate);
2142 *frames = dsp_frames;
2143 ret = 0;
2144 /* this is the best we can do */
2145 clock_gettime(CLOCK_MONOTONIC, timestamp);
2146 }
2147 } else {
2148 if (out->pcm) {
vivek mehta1a9b7c02015-06-25 11:49:38 -07002149 unsigned int avail;
Eric Laurent949a0892013-09-20 09:20:13 -07002150 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
2151 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07002152 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07002153 // This adjustment accounts for buffering after app processor.
2154 // It is based on estimated DSP latency per use case, rather than exact.
2155 signed_frames -=
2156 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
2157
Eric Laurent949a0892013-09-20 09:20:13 -07002158 // It would be unusual for this value to be negative, but check just in case ...
2159 if (signed_frames >= 0) {
2160 *frames = signed_frames;
2161 ret = 0;
2162 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002163 }
2164 }
2165 }
2166
2167 pthread_mutex_unlock(&out->lock);
2168
2169 return ret;
2170}
2171
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002172static int out_set_callback(struct audio_stream_out *stream,
2173 stream_callback_t callback, void *cookie)
2174{
2175 struct stream_out *out = (struct stream_out *)stream;
2176
2177 ALOGV("%s", __func__);
Eric Laurenta1478072015-09-21 17:21:52 -07002178 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002179 out->offload_callback = callback;
2180 out->offload_cookie = cookie;
2181 pthread_mutex_unlock(&out->lock);
2182 return 0;
2183}
2184
2185static int out_pause(struct audio_stream_out* stream)
2186{
2187 struct stream_out *out = (struct stream_out *)stream;
2188 int status = -ENOSYS;
2189 ALOGV("%s", __func__);
2190 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurenta1478072015-09-21 17:21:52 -07002191 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002192 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
2193 status = compress_pause(out->compr);
2194 out->offload_state = OFFLOAD_STATE_PAUSED;
2195 }
2196 pthread_mutex_unlock(&out->lock);
2197 }
2198 return status;
2199}
2200
2201static int out_resume(struct audio_stream_out* stream)
2202{
2203 struct stream_out *out = (struct stream_out *)stream;
2204 int status = -ENOSYS;
2205 ALOGV("%s", __func__);
2206 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2207 status = 0;
Eric Laurenta1478072015-09-21 17:21:52 -07002208 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002209 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
2210 status = compress_resume(out->compr);
2211 out->offload_state = OFFLOAD_STATE_PLAYING;
2212 }
2213 pthread_mutex_unlock(&out->lock);
2214 }
2215 return status;
2216}
2217
2218static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2219{
2220 struct stream_out *out = (struct stream_out *)stream;
2221 int status = -ENOSYS;
2222 ALOGV("%s", __func__);
2223 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurenta1478072015-09-21 17:21:52 -07002224 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002225 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2226 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2227 else
2228 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2229 pthread_mutex_unlock(&out->lock);
2230 }
2231 return status;
2232}
2233
2234static int out_flush(struct audio_stream_out* stream)
2235{
2236 struct stream_out *out = (struct stream_out *)stream;
2237 ALOGV("%s", __func__);
2238 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurenta1478072015-09-21 17:21:52 -07002239 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002240 stop_compressed_output_l(out);
2241 pthread_mutex_unlock(&out->lock);
2242 return 0;
2243 }
2244 return -ENOSYS;
2245}
2246
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002247/** audio_stream_in implementation **/
2248static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2249{
2250 struct stream_in *in = (struct stream_in *)stream;
2251
2252 return in->config.rate;
2253}
2254
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002255static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002256{
2257 return -ENOSYS;
2258}
2259
2260static size_t in_get_buffer_size(const struct audio_stream *stream)
2261{
2262 struct stream_in *in = (struct stream_in *)stream;
2263
Haynes Mathew George03c40102016-01-29 17:57:48 -08002264 return in->config.period_size * in->af_period_multiplier *
2265 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002266}
2267
2268static uint32_t in_get_channels(const struct audio_stream *stream)
2269{
2270 struct stream_in *in = (struct stream_in *)stream;
2271
2272 return in->channel_mask;
2273}
2274
vivek mehta4ed66e62016-04-15 23:33:34 -07002275static audio_format_t in_get_format(const struct audio_stream *stream)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002276{
vivek mehta4ed66e62016-04-15 23:33:34 -07002277 struct stream_in *in = (struct stream_in *)stream;
2278 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002279}
2280
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002281static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002282{
2283 return -ENOSYS;
2284}
2285
2286static int in_standby(struct audio_stream *stream)
2287{
2288 struct stream_in *in = (struct stream_in *)stream;
2289 struct audio_device *adev = in->dev;
2290 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07002291 ALOGV("%s: enter", __func__);
Eric Laurenta1478072015-09-21 17:21:52 -07002292
2293 lock_input_stream(in);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002294
2295 if (!in->standby && in->is_st_session) {
Joe Onorato188b6222016-03-01 11:02:27 -08002296 ALOGV("%s: sound trigger pcm stop lab", __func__);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002297 audio_extn_sound_trigger_stop_lab(in);
2298 in->standby = true;
2299 }
2300
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002301 if (!in->standby) {
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002302 if (adev->adm_deregister_stream)
2303 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
2304
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002305 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002306 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002307 if (in->pcm) {
2308 pcm_close(in->pcm);
2309 in->pcm = NULL;
2310 }
Eric Laurentcefbbac2014-09-04 13:54:10 -05002311 adev->enable_voicerx = false;
2312 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE );
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002313 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002314 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002315 }
2316 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002317 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002318 return status;
2319}
2320
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002321static int in_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002322{
2323 return 0;
2324}
2325
2326static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2327{
2328 struct stream_in *in = (struct stream_in *)stream;
2329 struct audio_device *adev = in->dev;
2330 struct str_parms *parms;
2331 char *str;
2332 char value[32];
2333 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07002334 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002335
Eric Laurent994a6932013-07-17 11:51:42 -07002336 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002337 parms = str_parms_create_str(kvpairs);
2338
2339 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2340
Eric Laurenta1478072015-09-21 17:21:52 -07002341 lock_input_stream(in);
2342
Eric Laurent150dbfe2013-02-27 14:31:02 -08002343 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002344 if (ret >= 0) {
2345 val = atoi(value);
2346 /* no audio source uses val == 0 */
2347 if ((in->source != val) && (val != 0)) {
2348 in->source = val;
2349 }
2350 }
2351
2352 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07002353
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002354 if (ret >= 0) {
2355 val = atoi(value);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002356 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002357 in->device = val;
2358 /* If recording is in progress, change the tx device to new device */
Haynes Mathew George03c40102016-01-29 17:57:48 -08002359 if (!in->standby) {
2360 ALOGV("update input routing change");
2361 in->routing_change = true;
2362 select_devices(adev, in->usecase);
2363 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002364 }
2365 }
2366
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002367 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002368 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002369
2370 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07002371 ALOGV("%s: exit: status(%d)", __func__, status);
2372 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002373}
2374
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002375static char* in_get_parameters(const struct audio_stream *stream __unused,
2376 const char *keys __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002377{
2378 return strdup("");
2379}
2380
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002381static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002382{
2383 return 0;
2384}
2385
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07002386static void in_snd_mon_cb(void * stream, struct str_parms * parms)
2387{
2388 if (!stream || !parms)
2389 return;
2390
2391 struct stream_in *in = (struct stream_in *)stream;
2392 struct audio_device *adev = in->dev;
2393
2394 card_status_t status;
2395 int card;
2396 if (parse_snd_card_status(parms, &card, &status) < 0)
2397 return;
2398
2399 pthread_mutex_lock(&adev->lock);
2400 bool valid_cb = (card == adev->snd_card);
2401 pthread_mutex_unlock(&adev->lock);
2402
2403 if (!valid_cb)
2404 return;
2405
2406 lock_input_stream(in);
2407 if (in->card_status != status)
2408 in->card_status = status;
2409 pthread_mutex_unlock(&in->lock);
2410
2411 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
2412 use_case_table[in->usecase],
2413 status == CARD_STATUS_OFFLINE ? "offline" : "online");
2414
2415 // a better solution would be to report error back to AF and let
2416 // it put the stream to standby
2417 if (status == CARD_STATUS_OFFLINE)
2418 in_standby(&in->stream.common);
2419
2420 return;
2421}
2422
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002423static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2424 size_t bytes)
2425{
2426 struct stream_in *in = (struct stream_in *)stream;
2427 struct audio_device *adev = in->dev;
2428 int i, ret = -1;
vivek mehta4ed66e62016-04-15 23:33:34 -07002429 int *int_buf_stream = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002430
Eric Laurenta1478072015-09-21 17:21:52 -07002431 lock_input_stream(in);
2432
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002433 if (in->is_st_session) {
2434 ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
2435 /* Read from sound trigger HAL */
2436 audio_extn_sound_trigger_read(in, buffer, bytes);
2437 pthread_mutex_unlock(&in->lock);
2438 return bytes;
2439 }
2440
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002441 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002442 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002443 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002444 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002445 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002446 goto exit;
2447 }
2448 in->standby = 0;
2449 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002450
Haynes Mathew George03c40102016-01-29 17:57:48 -08002451 //what's the duration requested by the client?
2452 long ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
2453 in->config.rate;
2454 request_in_focus(in, ns);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002455
Haynes Mathew George03c40102016-01-29 17:57:48 -08002456 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002457 if (in->pcm) {
Haynes Mathew George03c40102016-01-29 17:57:48 -08002458 if (use_mmap) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002459 ret = pcm_mmap_read(in->pcm, buffer, bytes);
vivek mehta4ed66e62016-04-15 23:33:34 -07002460 } else {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002461 ret = pcm_read(in->pcm, buffer, bytes);
Eric Laurentf8b50aa2016-05-06 11:03:53 -07002462 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08002463 if (ret < 0) {
2464 ALOGE("Failed to read w/err %s", strerror(errno));
2465 ret = -errno;
2466 }
Eric Laurentf8b50aa2016-05-06 11:03:53 -07002467 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
2468 if (bytes % 4 == 0) {
2469 /* data from DSP comes in 24_8 format, convert it to 8_24 */
2470 int_buf_stream = buffer;
2471 for (size_t itt=0; itt < bytes/4 ; itt++) {
2472 int_buf_stream[itt] >>= 8;
vivek mehta4ed66e62016-04-15 23:33:34 -07002473 }
Eric Laurentf8b50aa2016-05-06 11:03:53 -07002474 } else {
2475 ALOGE("%s: !!! something wrong !!! ... data not 32 bit aligned ", __func__);
2476 ret = -EINVAL;
2477 goto exit;
vivek mehta4ed66e62016-04-15 23:33:34 -07002478 }
2479 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002480 }
2481
Haynes Mathew George03c40102016-01-29 17:57:48 -08002482 release_in_focus(in, ns);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07002483
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002484 /*
2485 * Instead of writing zeroes here, we could trust the hardware
2486 * to always provide zeroes when muted.
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07002487 * 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 -08002488 */
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07002489 if (ret == 0 && adev->mic_muted && in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002490 memset(buffer, 0, bytes);
2491
2492exit:
2493 pthread_mutex_unlock(&in->lock);
2494
2495 if (ret != 0) {
2496 in_standby(&in->stream.common);
2497 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Eric Laurentfdf296a2014-07-03 16:41:51 -07002498 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002499 in_get_sample_rate(&in->stream.common));
Andy Hungc8589872016-03-10 16:37:48 -08002500 memset(buffer, 0, bytes); // clear return data
2501 }
2502 if (bytes > 0) {
Andy Hung6ebe5962016-01-15 17:46:57 -08002503 in->frames_read += bytes / audio_stream_in_frame_size(stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002504 }
2505 return bytes;
2506}
2507
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002508static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002509{
2510 return 0;
2511}
2512
Andy Hung6ebe5962016-01-15 17:46:57 -08002513static int in_get_capture_position(const struct audio_stream_in *stream,
2514 int64_t *frames, int64_t *time)
2515{
2516 if (stream == NULL || frames == NULL || time == NULL) {
2517 return -EINVAL;
2518 }
2519 struct stream_in *in = (struct stream_in *)stream;
2520 int ret = -ENOSYS;
2521
2522 lock_input_stream(in);
2523 if (in->pcm) {
2524 struct timespec timestamp;
2525 unsigned int avail;
2526 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
2527 *frames = in->frames_read + avail;
2528 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec;
2529 ret = 0;
2530 }
2531 }
2532 pthread_mutex_unlock(&in->lock);
2533 return ret;
2534}
2535
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002536static int add_remove_audio_effect(const struct audio_stream *stream,
2537 effect_handle_t effect,
2538 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002539{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002540 struct stream_in *in = (struct stream_in *)stream;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002541 struct audio_device *adev = in->dev;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002542 int status = 0;
2543 effect_descriptor_t desc;
2544
2545 status = (*effect)->get_descriptor(effect, &desc);
2546 if (status != 0)
2547 return status;
2548
Eric Laurenta1478072015-09-21 17:21:52 -07002549 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002550 pthread_mutex_lock(&in->dev->lock);
Eric Laurent50a38ed2015-10-14 18:48:06 -07002551 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
vivek mehta733c1df2016-04-04 15:09:24 -07002552 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
Eric Laurent50a38ed2015-10-14 18:48:06 -07002553 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002554 in->enable_aec != enable &&
2555 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2556 in->enable_aec = enable;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002557 if (!enable)
2558 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
vivek mehta733c1df2016-04-04 15:09:24 -07002559 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
2560 adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
2561 adev->enable_voicerx = enable;
2562 struct audio_usecase *usecase;
2563 struct listnode *node;
2564 list_for_each(node, &adev->usecase_list) {
2565 usecase = node_to_item(node, struct audio_usecase, list);
2566 if (usecase->type == PCM_PLAYBACK) {
2567 select_devices(adev, usecase->id);
2568 break;
2569 }
Eric Laurentcefbbac2014-09-04 13:54:10 -05002570 }
2571 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002572 if (!in->standby)
2573 select_devices(in->dev, in->usecase);
2574 }
Ravi Kumar Alamanda3ad4e1b2014-06-03 00:08:15 -07002575 if (in->enable_ns != enable &&
2576 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2577 in->enable_ns = enable;
2578 if (!in->standby)
2579 select_devices(in->dev, in->usecase);
2580 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002581 pthread_mutex_unlock(&in->dev->lock);
2582 pthread_mutex_unlock(&in->lock);
2583
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002584 return 0;
2585}
2586
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002587static int in_add_audio_effect(const struct audio_stream *stream,
2588 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002589{
Eric Laurent994a6932013-07-17 11:51:42 -07002590 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002591 return add_remove_audio_effect(stream, effect, true);
2592}
2593
2594static int in_remove_audio_effect(const struct audio_stream *stream,
2595 effect_handle_t effect)
2596{
Eric Laurent994a6932013-07-17 11:51:42 -07002597 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002598 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002599}
2600
2601static int adev_open_output_stream(struct audio_hw_device *dev,
2602 audio_io_handle_t handle,
2603 audio_devices_t devices,
2604 audio_output_flags_t flags,
2605 struct audio_config *config,
Eric Laurent91975a62014-07-27 17:15:50 -07002606 struct audio_stream_out **stream_out,
2607 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002608{
2609 struct audio_device *adev = (struct audio_device *)dev;
2610 struct stream_out *out;
2611 int i, ret;
2612
Eric Laurent994a6932013-07-17 11:51:42 -07002613 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002614 __func__, config->sample_rate, config->channel_mask, devices, flags);
2615 *stream_out = NULL;
2616 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2617
2618 if (devices == AUDIO_DEVICE_NONE)
2619 devices = AUDIO_DEVICE_OUT_SPEAKER;
2620
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002621 out->flags = flags;
2622 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002623 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002624 out->format = config->format;
2625 out->sample_rate = config->sample_rate;
2626 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2627 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002628 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002629
2630 /* Init use case and pcm_config */
2631 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07002632 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002633 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002634 pthread_mutex_lock(&adev->lock);
2635 ret = read_hdmi_channel_masks(out);
2636 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002637 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002638 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002639
2640 if (config->sample_rate == 0)
2641 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2642 if (config->channel_mask == 0)
2643 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
Eric Laurentad1cec22015-12-16 10:12:27 -08002644 if (config->format == AUDIO_FORMAT_DEFAULT)
2645 config->format = AUDIO_FORMAT_PCM_16_BIT;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002646
2647 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002648 out->sample_rate = config->sample_rate;
Eric Laurentad1cec22015-12-16 10:12:27 -08002649 out->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002650 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2651 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002652 out->config.rate = config->sample_rate;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002653 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002654 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002655 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew George35807692016-07-07 20:04:54 -07002656 pthread_mutex_lock(&adev->lock);
2657 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
2658 pthread_mutex_unlock(&adev->lock);
2659
2660 // reject offload during card offline to allow
2661 // fallback to s/w paths
2662 if (offline) {
2663 ret = -ENODEV;
2664 goto error_open;
2665 }
2666
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002667 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2668 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2669 ALOGE("%s: Unsupported Offload information", __func__);
2670 ret = -EINVAL;
2671 goto error_open;
2672 }
2673 if (!is_supported_format(config->offload_info.format)) {
2674 ALOGE("%s: Unsupported audio format", __func__);
2675 ret = -EINVAL;
2676 goto error_open;
2677 }
2678
2679 out->compr_config.codec = (struct snd_codec *)
2680 calloc(1, sizeof(struct snd_codec));
2681
2682 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2683 if (config->offload_info.channel_mask)
2684 out->channel_mask = config->offload_info.channel_mask;
2685 else if (config->channel_mask)
2686 out->channel_mask = config->channel_mask;
2687 out->format = config->offload_info.format;
2688 out->sample_rate = config->offload_info.sample_rate;
2689
2690 out->stream.set_callback = out_set_callback;
2691 out->stream.pause = out_pause;
2692 out->stream.resume = out_resume;
2693 out->stream.drain = out_drain;
2694 out->stream.flush = out_flush;
2695
2696 out->compr_config.codec->id =
2697 get_snd_codec_id(config->offload_info.format);
2698 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2699 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
Eric Laurent1ccf3972014-10-23 14:42:59 -07002700 out->compr_config.codec->sample_rate = config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002701 out->compr_config.codec->bit_rate =
2702 config->offload_info.bit_rate;
2703 out->compr_config.codec->ch_in =
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002704 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002705 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2706
2707 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2708 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002709
2710 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002711 create_offload_callback_thread(out);
2712 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2713 __func__, config->offload_info.version,
2714 config->offload_info.bit_rate);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002715 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2716 if (config->sample_rate == 0)
2717 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2718 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2719 config->sample_rate != 8000) {
2720 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2721 ret = -EINVAL;
2722 goto error_open;
2723 }
2724 out->sample_rate = config->sample_rate;
2725 out->config.rate = config->sample_rate;
2726 if (config->format == AUDIO_FORMAT_DEFAULT)
2727 config->format = AUDIO_FORMAT_PCM_16_BIT;
2728 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2729 config->format = AUDIO_FORMAT_PCM_16_BIT;
2730 ret = -EINVAL;
2731 goto error_open;
2732 }
2733 out->format = config->format;
2734 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2735 out->config = pcm_config_afe_proxy_playback;
2736 adev->voice_tx_output = out;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002737 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07002738 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2739 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2740 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamandaf78a4d92015-04-24 15:18:23 -07002741 } else if (out->flags & AUDIO_OUTPUT_FLAG_TTS) {
2742 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
2743 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda2bc7b022015-06-25 20:08:01 -07002744 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
2745 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George03c40102016-01-29 17:57:48 -08002746 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL, out->flags);
2747 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
2748 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Andy Hung6fcba9c2014-03-18 11:53:32 -07002749 } else {
2750 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2751 out->config = pcm_config_low_latency;
2752 }
2753 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2754 if (k_enable_extended_precision
2755 && pcm_params_format_test(adev->use_case_table[out->usecase],
2756 pcm_format_from_audio_format(config->format))) {
2757 out->config.format = pcm_format_from_audio_format(config->format);
2758 /* out->format already set to config->format */
2759 } else {
2760 /* deny the externally proposed config format
2761 * and use the one specified in audio_hw layer configuration.
2762 * Note: out->format is returned by out->stream.common.get_format()
2763 * and is used to set config->format in the code several lines below.
2764 */
2765 out->format = audio_format_from_pcm_format(out->config.format);
2766 }
2767 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002768 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002769 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07002770 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
2771 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002772
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002773 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002774 if (adev->primary_output == NULL)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002775 adev->primary_output = out;
2776 else {
2777 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002778 ret = -EEXIST;
2779 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002780 }
2781 }
2782
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002783 /* Check if this usecase is already existing */
2784 pthread_mutex_lock(&adev->lock);
2785 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2786 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002787 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002788 ret = -EEXIST;
2789 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002790 }
2791 pthread_mutex_unlock(&adev->lock);
2792
2793 out->stream.common.get_sample_rate = out_get_sample_rate;
2794 out->stream.common.set_sample_rate = out_set_sample_rate;
2795 out->stream.common.get_buffer_size = out_get_buffer_size;
2796 out->stream.common.get_channels = out_get_channels;
2797 out->stream.common.get_format = out_get_format;
2798 out->stream.common.set_format = out_set_format;
2799 out->stream.common.standby = out_standby;
2800 out->stream.common.dump = out_dump;
2801 out->stream.common.set_parameters = out_set_parameters;
2802 out->stream.common.get_parameters = out_get_parameters;
2803 out->stream.common.add_audio_effect = out_add_audio_effect;
2804 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2805 out->stream.get_latency = out_get_latency;
2806 out->stream.set_volume = out_set_volume;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002807#ifdef NO_AUDIO_OUT
2808 out->stream.write = out_write_for_no_output;
2809#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002810 out->stream.write = out_write;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002811#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002812 out->stream.get_render_position = out_get_render_position;
2813 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002814 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002815
Haynes Mathew George03c40102016-01-29 17:57:48 -08002816 out->af_period_multiplier = out->realtime ? af_period_multiplier : 1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002817 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002818 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002819 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002820
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002821 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Eric Laurenta1478072015-09-21 17:21:52 -07002822 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002823 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2824
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002825 config->format = out->stream.common.get_format(&out->stream.common);
2826 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2827 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2828
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07002829
2830 /*
2831 By locking output stream before registering, we allow the callback
2832 to update stream's state only after stream's initial state is set to
2833 adev state.
2834 */
2835 lock_output_stream(out);
2836 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
2837 pthread_mutex_lock(&adev->lock);
2838 out->card_status = adev->card_status;
2839 pthread_mutex_unlock(&adev->lock);
2840 pthread_mutex_unlock(&out->lock);
2841
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002842 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002843 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002844 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002845
2846error_open:
2847 free(out);
2848 *stream_out = NULL;
Eric Laurent2bafff12016-03-17 12:17:23 -07002849 ALOGW("%s: exit: ret %d", __func__, ret);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002850 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002851}
2852
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002853static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002854 struct audio_stream_out *stream)
2855{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002856 struct stream_out *out = (struct stream_out *)stream;
2857 struct audio_device *adev = out->dev;
2858
Eric Laurent994a6932013-07-17 11:51:42 -07002859 ALOGV("%s: enter", __func__);
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07002860
2861 // must deregister from sndmonitor first to prevent races
2862 // between the callback and close_stream
2863 audio_extn_snd_mon_unregister_listener(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002864 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002865 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2866 destroy_offload_callback_thread(out);
2867
2868 if (out->compr_config.codec != NULL)
2869 free(out->compr_config.codec);
2870 }
Ravi Kumar Alamandaa4fc9022014-10-08 18:57:46 -07002871
2872 if (adev->voice_tx_output == out)
2873 adev->voice_tx_output = NULL;
2874
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002875 pthread_cond_destroy(&out->cond);
2876 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002877 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002878 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002879}
2880
2881static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2882{
2883 struct audio_device *adev = (struct audio_device *)dev;
2884 struct str_parms *parms;
2885 char *str;
2886 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002887 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002888 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002889 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002890
Joe Onorato188b6222016-03-01 11:02:27 -08002891 ALOGV("%s: enter: %s", __func__, kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002892
2893 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002894
2895 parms = str_parms_create_str(kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002896 status = voice_set_parameters(adev, parms);
2897 if (status != 0) {
2898 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002899 }
2900
2901 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2902 if (ret >= 0) {
Ravi Kumar Alamandae258e682015-06-25 13:32:42 -07002903 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002904 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2905 adev->bluetooth_nrec = true;
2906 else
2907 adev->bluetooth_nrec = false;
2908 }
2909
2910 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2911 if (ret >= 0) {
2912 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2913 adev->screen_off = false;
2914 else
2915 adev->screen_off = true;
2916 }
2917
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002918 ret = str_parms_get_int(parms, "rotation", &val);
2919 if (ret >= 0) {
2920 bool reverse_speakers = false;
2921 switch(val) {
2922 // FIXME: note that the code below assumes that the speakers are in the correct placement
2923 // relative to the user when the device is rotated 90deg from its default rotation. This
2924 // assumption is device-specific, not platform-specific like this code.
2925 case 270:
2926 reverse_speakers = true;
2927 break;
2928 case 0:
2929 case 90:
2930 case 180:
2931 break;
2932 default:
2933 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002934 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002935 }
Eric Laurent03f09432014-03-25 18:09:11 -07002936 if (status == 0) {
Ravi Kumar Alamanda1f60cf82015-04-23 19:45:17 -07002937 platform_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002938 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002939 }
2940
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002941 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2942 if (ret >= 0) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002943 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002944 }
2945
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08002946 audio_extn_hfp_set_parameters(adev, parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002947done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002948 str_parms_destroy(parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002949 pthread_mutex_unlock(&adev->lock);
Eric Laurent03f09432014-03-25 18:09:11 -07002950 ALOGV("%s: exit with code(%d)", __func__, status);
2951 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002952}
2953
2954static char* adev_get_parameters(const struct audio_hw_device *dev,
2955 const char *keys)
2956{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002957 struct audio_device *adev = (struct audio_device *)dev;
2958 struct str_parms *reply = str_parms_create();
2959 struct str_parms *query = str_parms_create_str(keys);
2960 char *str;
2961
2962 pthread_mutex_lock(&adev->lock);
2963
2964 voice_get_parameters(adev, query, reply);
2965 str = str_parms_to_str(reply);
2966 str_parms_destroy(query);
2967 str_parms_destroy(reply);
2968
2969 pthread_mutex_unlock(&adev->lock);
2970 ALOGV("%s: exit: returns - %s", __func__, str);
2971 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002972}
2973
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002974static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002975{
2976 return 0;
2977}
2978
Haynes Mathew George5191a852013-09-11 14:19:36 -07002979static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2980{
2981 int ret;
2982 struct audio_device *adev = (struct audio_device *)dev;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002983
Eric Laurent4cc4ce12014-09-10 13:21:01 -05002984 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
2985
Haynes Mathew George5191a852013-09-11 14:19:36 -07002986 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002987 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002988 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002989
Haynes Mathew George5191a852013-09-11 14:19:36 -07002990 return ret;
2991}
2992
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002993static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002994{
2995 return -ENOSYS;
2996}
2997
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002998static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2999 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003000{
3001 return -ENOSYS;
3002}
3003
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07003004static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003005{
3006 return -ENOSYS;
3007}
3008
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07003009static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003010{
3011 return -ENOSYS;
3012}
3013
3014static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
3015{
3016 struct audio_device *adev = (struct audio_device *)dev;
3017
3018 pthread_mutex_lock(&adev->lock);
3019 if (adev->mode != mode) {
Eric Laurent2bafff12016-03-17 12:17:23 -07003020 ALOGD("%s: mode %d", __func__, (int)mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003021 adev->mode = mode;
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -07003022 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) &&
3023 voice_is_in_call(adev)) {
3024 voice_stop_call(adev);
3025 adev->current_call_output = NULL;
3026 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003027 }
3028 pthread_mutex_unlock(&adev->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05003029
3030 audio_extn_extspk_set_mode(adev->extspk, mode);
3031
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003032 return 0;
3033}
3034
3035static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
3036{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003037 int ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003038 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003039
Eric Laurent2bafff12016-03-17 12:17:23 -07003040 ALOGD("%s: state %d", __func__, (int)state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003041 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003042 ret = voice_set_mic_mute(adev, state);
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07003043 adev->mic_muted = state;
Eric Laurenta609e8e2014-06-18 02:15:17 +00003044 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003045
3046 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003047}
3048
3049static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
3050{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003051 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003052 return 0;
3053}
3054
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07003055static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003056 const struct audio_config *config)
3057{
Eric Laurent0de8d1f2014-07-01 20:34:45 -07003058 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003059
Glenn Kasten68e79ce2014-07-15 10:56:59 -07003060 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
3061 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003062}
3063
3064static int adev_open_input_stream(struct audio_hw_device *dev,
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07003065 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003066 audio_devices_t devices,
3067 struct audio_config *config,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07003068 struct audio_stream_in **stream_in,
Eric Laurent91975a62014-07-27 17:15:50 -07003069 audio_input_flags_t flags,
3070 const char *address __unused,
Eric Laurentcefbbac2014-09-04 13:54:10 -05003071 audio_source_t source )
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003072{
3073 struct audio_device *adev = (struct audio_device *)dev;
3074 struct stream_in *in;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003075 int ret = 0, buffer_size, frame_size;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07003076 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003077 bool is_low_latency = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003078
Eric Laurent994a6932013-07-17 11:51:42 -07003079 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003080 *stream_in = NULL;
3081 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
3082 return -EINVAL;
3083
3084 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
3085
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003086 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Eric Laurenta1478072015-09-21 17:21:52 -07003087 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003088
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003089 in->stream.common.get_sample_rate = in_get_sample_rate;
3090 in->stream.common.set_sample_rate = in_set_sample_rate;
3091 in->stream.common.get_buffer_size = in_get_buffer_size;
3092 in->stream.common.get_channels = in_get_channels;
3093 in->stream.common.get_format = in_get_format;
3094 in->stream.common.set_format = in_set_format;
3095 in->stream.common.standby = in_standby;
3096 in->stream.common.dump = in_dump;
3097 in->stream.common.set_parameters = in_set_parameters;
3098 in->stream.common.get_parameters = in_get_parameters;
3099 in->stream.common.add_audio_effect = in_add_audio_effect;
3100 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3101 in->stream.set_gain = in_set_gain;
3102 in->stream.read = in_read;
3103 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Andy Hung6ebe5962016-01-15 17:46:57 -08003104 in->stream.get_capture_position = in_get_capture_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003105
3106 in->device = devices;
Eric Laurentcefbbac2014-09-04 13:54:10 -05003107 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003108 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003109 in->standby = 1;
3110 in->channel_mask = config->channel_mask;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07003111 in->capture_handle = handle;
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07003112 in->flags = flags;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003113
vivek mehta57ff9b52016-04-28 14:13:08 -07003114 // restrict 24 bit capture for unprocessed source only
3115 // for other sources if 24 bit requested reject 24 and set 16 bit capture only
3116 if (config->format == AUDIO_FORMAT_DEFAULT) {
vivek mehta4ed66e62016-04-15 23:33:34 -07003117 config->format = AUDIO_FORMAT_PCM_16_BIT;
vivek mehta57ff9b52016-04-28 14:13:08 -07003118 } else if (config->format == AUDIO_FORMAT_PCM_FLOAT ||
3119 config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
3120 config->format == AUDIO_FORMAT_PCM_8_24_BIT) {
3121 bool ret_error = false;
3122 /* 24 bit is restricted to UNPROCESSED source only,also format supported
3123 from HAL is 8_24
3124 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
3125 8_24 return error indicating supported format is 8_24
3126 *> In case of any other source requesting 24 bit or float return error
3127 indicating format supported is 16 bit only.
vivek mehta4ed66e62016-04-15 23:33:34 -07003128
vivek mehta57ff9b52016-04-28 14:13:08 -07003129 on error flinger will retry with supported format passed
3130 */
3131 if (source != AUDIO_SOURCE_UNPROCESSED) {
3132 config->format = AUDIO_FORMAT_PCM_16_BIT;
3133 ret_error = true;
3134 } else if (config->format != AUDIO_FORMAT_PCM_8_24_BIT) {
3135 config->format = AUDIO_FORMAT_PCM_8_24_BIT;
3136 ret_error = true;
3137 }
3138
3139 if (ret_error) {
3140 ret = -EINVAL;
3141 goto err_open;
3142 }
vivek mehta4ed66e62016-04-15 23:33:34 -07003143 }
3144
vivek mehta57ff9b52016-04-28 14:13:08 -07003145 in->format = config->format;
3146
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003147 /* Update config params with the requested sample rate and channels */
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003148 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
3149 if (config->sample_rate == 0)
3150 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3151 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
3152 config->sample_rate != 8000) {
3153 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3154 ret = -EINVAL;
3155 goto err_open;
3156 }
vivek mehta4ed66e62016-04-15 23:33:34 -07003157
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003158 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
3159 config->format = AUDIO_FORMAT_PCM_16_BIT;
3160 ret = -EINVAL;
3161 goto err_open;
3162 }
3163
3164 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
3165 in->config = pcm_config_afe_proxy_record;
3166 } else {
3167 in->usecase = USECASE_AUDIO_RECORD;
3168 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
3169 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
3170 is_low_latency = true;
Glenn Kasten4f993392014-05-14 07:30:48 -07003171#if LOW_LATENCY_CAPTURE_USE_CASE
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003172 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Glenn Kasten4f993392014-05-14 07:30:48 -07003173#endif
Haynes Mathew George03c40102016-01-29 17:57:48 -08003174 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003175 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07003176
Haynes Mathew George03c40102016-01-29 17:57:48 -08003177 in->config = in->realtime ? pcm_config_audio_capture_rt :
3178 pcm_config_audio_capture;
3179
vivek mehta4ed66e62016-04-15 23:33:34 -07003180 if (config->format == AUDIO_FORMAT_PCM_8_24_BIT)
3181 in->config.format = PCM_FORMAT_S24_LE;
3182
Haynes Mathew George03c40102016-01-29 17:57:48 -08003183 if (!in->realtime) {
3184 frame_size = audio_stream_in_frame_size(&in->stream);
3185 buffer_size = get_input_buffer_size(config->sample_rate,
3186 config->format,
3187 channel_count,
Eric Laurentf8b50aa2016-05-06 11:03:53 -07003188 is_low_latency);
Haynes Mathew George03c40102016-01-29 17:57:48 -08003189 in->config.period_size = buffer_size / frame_size;
3190 } // period size is left untouched for rt mode playback
Glenn Kasten68e79ce2014-07-15 10:56:59 -07003191 }
Haynes Mathew George03c40102016-01-29 17:57:48 -08003192
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003193 in->config.channels = channel_count;
Haynes Mathew George03c40102016-01-29 17:57:48 -08003194 if (in->realtime) {
3195 in->af_period_multiplier = af_period_multiplier;
3196 } else {
3197 in->config.rate = config->sample_rate;
3198 in->af_period_multiplier = 1;
3199 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003200
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07003201 /* This stream could be for sound trigger lab,
3202 get sound trigger pcm if present */
3203 audio_extn_sound_trigger_check_and_get_session(in);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003204
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07003205 lock_input_stream(in);
3206 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
3207 pthread_mutex_lock(&adev->lock);
3208 in->card_status = adev->card_status;
3209 pthread_mutex_unlock(&adev->lock);
3210 pthread_mutex_unlock(&in->lock);
3211
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003212 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07003213 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003214 return 0;
3215
3216err_open:
3217 free(in);
3218 *stream_in = NULL;
3219 return ret;
3220}
3221
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07003222static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003223 struct audio_stream_in *stream)
3224{
Eric Laurent994a6932013-07-17 11:51:42 -07003225 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003226
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07003227 // must deregister from sndmonitor first to prevent races
3228 // between the callback and close_stream
3229 audio_extn_snd_mon_unregister_listener(stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003230 in_standby(&stream->common);
3231 free(stream);
3232
3233 return;
3234}
3235
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07003236static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003237{
3238 return 0;
3239}
3240
Andy Hung31aca912014-03-20 17:14:59 -07003241/* verifies input and output devices and their capabilities.
3242 *
3243 * This verification is required when enabling extended bit-depth or
3244 * sampling rates, as not all qcom products support it.
3245 *
3246 * Suitable for calling only on initialization such as adev_open().
3247 * It fills the audio_device use_case_table[] array.
3248 *
3249 * Has a side-effect that it needs to configure audio routing / devices
3250 * in order to power up the devices and read the device parameters.
3251 * It does not acquire any hw device lock. Should restore the devices
3252 * back to "normal state" upon completion.
3253 */
3254static int adev_verify_devices(struct audio_device *adev)
3255{
3256 /* enumeration is a bit difficult because one really wants to pull
3257 * the use_case, device id, etc from the hidden pcm_device_table[].
3258 * In this case there are the following use cases and device ids.
3259 *
3260 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
3261 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
3262 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
3263 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
3264 * [USECASE_AUDIO_RECORD] = {0, 0},
3265 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
3266 * [USECASE_VOICE_CALL] = {2, 2},
3267 *
3268 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
3269 * USECASE_VOICE_CALL omitted, but possible for either input or output.
3270 */
3271
3272 /* should be the usecases enabled in adev_open_input_stream() */
3273 static const int test_in_usecases[] = {
3274 USECASE_AUDIO_RECORD,
3275 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
3276 };
3277 /* should be the usecases enabled in adev_open_output_stream()*/
3278 static const int test_out_usecases[] = {
3279 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
3280 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
3281 };
3282 static const usecase_type_t usecase_type_by_dir[] = {
3283 PCM_PLAYBACK,
3284 PCM_CAPTURE,
3285 };
3286 static const unsigned flags_by_dir[] = {
3287 PCM_OUT,
3288 PCM_IN,
3289 };
3290
3291 size_t i;
3292 unsigned dir;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003293 const unsigned card_id = adev->snd_card;
Andy Hung31aca912014-03-20 17:14:59 -07003294 char info[512]; /* for possible debug info */
3295
3296 for (dir = 0; dir < 2; ++dir) {
3297 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
3298 const unsigned flags_dir = flags_by_dir[dir];
3299 const size_t testsize =
3300 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
3301 const int *testcases =
3302 dir ? test_in_usecases : test_out_usecases;
3303 const audio_devices_t audio_device =
3304 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
3305
3306 for (i = 0; i < testsize; ++i) {
3307 const audio_usecase_t audio_usecase = testcases[i];
3308 int device_id;
3309 snd_device_t snd_device;
3310 struct pcm_params **pparams;
3311 struct stream_out out;
3312 struct stream_in in;
3313 struct audio_usecase uc_info;
3314 int retval;
3315
3316 pparams = &adev->use_case_table[audio_usecase];
3317 pcm_params_free(*pparams); /* can accept null input */
3318 *pparams = NULL;
3319
3320 /* find the device ID for the use case (signed, for error) */
3321 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
3322 if (device_id < 0)
3323 continue;
3324
3325 /* prepare structures for device probing */
3326 memset(&uc_info, 0, sizeof(uc_info));
3327 uc_info.id = audio_usecase;
3328 uc_info.type = usecase_type;
3329 if (dir) {
3330 adev->active_input = &in;
3331 memset(&in, 0, sizeof(in));
3332 in.device = audio_device;
3333 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
3334 uc_info.stream.in = &in;
3335 } else {
3336 adev->active_input = NULL;
3337 }
3338 memset(&out, 0, sizeof(out));
3339 out.devices = audio_device; /* only field needed in select_devices */
3340 uc_info.stream.out = &out;
3341 uc_info.devices = audio_device;
3342 uc_info.in_snd_device = SND_DEVICE_NONE;
3343 uc_info.out_snd_device = SND_DEVICE_NONE;
3344 list_add_tail(&adev->usecase_list, &uc_info.list);
3345
3346 /* select device - similar to start_(in/out)put_stream() */
3347 retval = select_devices(adev, audio_usecase);
3348 if (retval >= 0) {
3349 *pparams = pcm_params_get(card_id, device_id, flags_dir);
3350#if LOG_NDEBUG == 0
3351 if (*pparams) {
3352 ALOGV("%s: (%s) card %d device %d", __func__,
3353 dir ? "input" : "output", card_id, device_id);
3354 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
Andy Hung31aca912014-03-20 17:14:59 -07003355 } else {
3356 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
3357 }
3358#endif
3359 }
3360
3361 /* deselect device - similar to stop_(in/out)put_stream() */
3362 /* 1. Get and set stream specific mixer controls */
Glenn Kastene7137302014-04-28 15:12:18 -07003363 retval = disable_audio_route(adev, &uc_info);
Andy Hung31aca912014-03-20 17:14:59 -07003364 /* 2. Disable the rx device */
3365 retval = disable_snd_device(adev,
Glenn Kastene7137302014-04-28 15:12:18 -07003366 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
Andy Hung31aca912014-03-20 17:14:59 -07003367 list_remove(&uc_info.list);
3368 }
3369 }
3370 adev->active_input = NULL; /* restore adev state */
3371 return 0;
3372}
3373
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003374static int adev_close(hw_device_t *device)
3375{
Andy Hung31aca912014-03-20 17:14:59 -07003376 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003377 struct audio_device *adev = (struct audio_device *)device;
vivek mehta1a9b7c02015-06-25 11:49:38 -07003378
3379 if (!adev)
3380 return 0;
3381
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07003382 audio_extn_snd_mon_unregister_listener(adev);
vivek mehta1a9b7c02015-06-25 11:49:38 -07003383 pthread_mutex_lock(&adev_init_lock);
3384
3385 if ((--audio_device_ref_count) == 0) {
3386 audio_route_free(adev->audio_route);
3387 free(adev->snd_dev_ref_cnt);
3388 platform_deinit(adev->platform);
3389 audio_extn_extspk_deinit(adev->extspk);
3390 audio_extn_sound_trigger_deinit(adev);
3391 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
3392 pcm_params_free(adev->use_case_table[i]);
3393 }
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07003394 if (adev->adm_deinit)
3395 adev->adm_deinit(adev->adm_data);
vivek mehta1a9b7c02015-06-25 11:49:38 -07003396 free(device);
Andy Hung31aca912014-03-20 17:14:59 -07003397 }
vivek mehta1a9b7c02015-06-25 11:49:38 -07003398
3399 pthread_mutex_unlock(&adev_init_lock);
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07003400
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003401 return 0;
3402}
3403
Glenn Kasten4f993392014-05-14 07:30:48 -07003404/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
3405 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
3406 * just that it _might_ work.
3407 */
3408static int period_size_is_plausible_for_low_latency(int period_size)
3409{
3410 switch (period_size) {
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07003411 case 48:
3412 case 96:
3413 case 144:
Glenn Kasten4f993392014-05-14 07:30:48 -07003414 case 160:
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07003415 case 192:
Glenn Kasten4f993392014-05-14 07:30:48 -07003416 case 240:
3417 case 320:
3418 case 480:
3419 return 1;
3420 default:
3421 return 0;
3422 }
3423}
3424
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07003425static void adev_snd_mon_cb(void * stream __unused, struct str_parms * parms)
3426{
3427 int card;
3428 card_status_t status;
3429
3430 if (!parms)
3431 return;
3432
3433 if (parse_snd_card_status(parms, &card, &status) < 0)
3434 return;
3435
3436 pthread_mutex_lock(&adev->lock);
3437 bool valid_cb = (card == adev->snd_card);
3438 if (valid_cb) {
3439 if (adev->card_status != status) {
3440 adev->card_status = status;
3441 platform_snd_card_update(adev->platform, status);
3442 }
3443 }
3444 pthread_mutex_unlock(&adev->lock);
3445 return;
3446}
3447
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003448static int adev_open(const hw_module_t *module, const char *name,
3449 hw_device_t **device)
3450{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003451 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003452
Eric Laurent2bafff12016-03-17 12:17:23 -07003453 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003454 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
vivek mehta1a9b7c02015-06-25 11:49:38 -07003455 pthread_mutex_lock(&adev_init_lock);
3456 if (audio_device_ref_count != 0) {
3457 *device = &adev->device.common;
3458 audio_device_ref_count++;
3459 ALOGV("%s: returning existing instance of adev", __func__);
3460 ALOGV("%s: exit", __func__);
3461 pthread_mutex_unlock(&adev_init_lock);
3462 return 0;
3463 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003464 adev = calloc(1, sizeof(struct audio_device));
3465
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003466 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3467
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003468 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3469 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3470 adev->device.common.module = (struct hw_module_t *)module;
3471 adev->device.common.close = adev_close;
3472
3473 adev->device.init_check = adev_init_check;
3474 adev->device.set_voice_volume = adev_set_voice_volume;
3475 adev->device.set_master_volume = adev_set_master_volume;
3476 adev->device.get_master_volume = adev_get_master_volume;
3477 adev->device.set_master_mute = adev_set_master_mute;
3478 adev->device.get_master_mute = adev_get_master_mute;
3479 adev->device.set_mode = adev_set_mode;
3480 adev->device.set_mic_mute = adev_set_mic_mute;
3481 adev->device.get_mic_mute = adev_get_mic_mute;
3482 adev->device.set_parameters = adev_set_parameters;
3483 adev->device.get_parameters = adev_get_parameters;
3484 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3485 adev->device.open_output_stream = adev_open_output_stream;
3486 adev->device.close_output_stream = adev_close_output_stream;
3487 adev->device.open_input_stream = adev_open_input_stream;
3488 adev->device.close_input_stream = adev_close_input_stream;
3489 adev->device.dump = adev_dump;
3490
3491 /* Set the default route before the PCM stream is opened */
3492 pthread_mutex_lock(&adev->lock);
3493 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003494 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003495 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003496 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003497 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003498 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07003499 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07003500 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003501 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003502 pthread_mutex_unlock(&adev->lock);
3503
3504 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003505 adev->platform = platform_init(adev);
3506 if (!adev->platform) {
3507 free(adev->snd_dev_ref_cnt);
3508 free(adev);
3509 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3510 *device = NULL;
vivek mehta1a9b7c02015-06-25 11:49:38 -07003511 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003512 return -EINVAL;
3513 }
Eric Laurent0499d4f2014-08-25 22:39:29 -05003514 adev->extspk = audio_extn_extspk_init(adev);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07003515 audio_extn_sound_trigger_init(adev);
Eric Laurent0499d4f2014-08-25 22:39:29 -05003516
Glenn Kasten6d53bfd2016-04-04 16:58:03 -07003517 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3518 if (adev->visualizer_lib == NULL) {
3519 ALOGW("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3520 } else {
3521 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3522 adev->visualizer_start_output =
3523 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
3524 "visualizer_hal_start_output");
3525 adev->visualizer_stop_output =
3526 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
3527 "visualizer_hal_stop_output");
Eric Laurentc4aef752013-09-12 17:45:53 -07003528 }
3529
Glenn Kasten6d53bfd2016-04-04 16:58:03 -07003530 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3531 if (adev->offload_effects_lib == NULL) {
3532 ALOGW("%s: DLOPEN failed for %s", __func__,
3533 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3534 } else {
3535 ALOGV("%s: DLOPEN successful for %s", __func__,
3536 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3537 adev->offload_effects_start_output =
3538 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3539 "offload_effects_bundle_hal_start_output");
3540 adev->offload_effects_stop_output =
3541 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3542 "offload_effects_bundle_hal_stop_output");
Haynes Mathew George41f86652014-06-17 14:22:15 -07003543 }
3544
Glenn Kasten6d53bfd2016-04-04 16:58:03 -07003545 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
3546 if (adev->adm_lib == NULL) {
3547 ALOGW("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
3548 } else {
3549 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
3550 adev->adm_init = (adm_init_t)
3551 dlsym(adev->adm_lib, "adm_init");
3552 adev->adm_deinit = (adm_deinit_t)
3553 dlsym(adev->adm_lib, "adm_deinit");
3554 adev->adm_register_input_stream = (adm_register_input_stream_t)
3555 dlsym(adev->adm_lib, "adm_register_input_stream");
3556 adev->adm_register_output_stream = (adm_register_output_stream_t)
3557 dlsym(adev->adm_lib, "adm_register_output_stream");
3558 adev->adm_deregister_stream = (adm_deregister_stream_t)
3559 dlsym(adev->adm_lib, "adm_deregister_stream");
3560 adev->adm_request_focus = (adm_request_focus_t)
3561 dlsym(adev->adm_lib, "adm_request_focus");
3562 adev->adm_abandon_focus = (adm_abandon_focus_t)
3563 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George03c40102016-01-29 17:57:48 -08003564 adev->adm_set_config = (adm_set_config_t)
3565 dlsym(adev->adm_lib, "adm_set_config");
3566 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
3567 dlsym(adev->adm_lib, "adm_request_focus_v2");
3568 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
3569 dlsym(adev->adm_lib, "adm_is_noirq_avail");
3570 adev->adm_on_routing_change = (adm_on_routing_change_t)
3571 dlsym(adev->adm_lib, "adm_on_routing_change");
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07003572 }
3573
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07003574 adev->bt_wb_speech_enabled = false;
Eric Laurentcefbbac2014-09-04 13:54:10 -05003575 adev->enable_voicerx = false;
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07003576
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003577 *device = &adev->device.common;
vivek mehta1a9b7c02015-06-25 11:49:38 -07003578
Andy Hung31aca912014-03-20 17:14:59 -07003579 if (k_enable_extended_precision)
3580 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003581
Glenn Kasten4f993392014-05-14 07:30:48 -07003582 char value[PROPERTY_VALUE_MAX];
3583 int trial;
3584 if (property_get("audio_hal.period_size", value, NULL) > 0) {
3585 trial = atoi(value);
3586 if (period_size_is_plausible_for_low_latency(trial)) {
3587 pcm_config_low_latency.period_size = trial;
3588 pcm_config_low_latency.start_threshold = trial / 4;
3589 pcm_config_low_latency.avail_min = trial / 4;
3590 configured_low_latency_capture_period_size = trial;
3591 }
3592 }
3593 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
3594 trial = atoi(value);
3595 if (period_size_is_plausible_for_low_latency(trial)) {
3596 configured_low_latency_capture_period_size = trial;
3597 }
3598 }
3599
vivek mehta1a9b7c02015-06-25 11:49:38 -07003600 audio_device_ref_count++;
Haynes Mathew George03c40102016-01-29 17:57:48 -08003601
3602 if (property_get("audio_hal.period_multiplier", value, NULL) > 0) {
3603 af_period_multiplier = atoi(value);
3604 if (af_period_multiplier < 0) {
3605 af_period_multiplier = 2;
3606 } else if (af_period_multiplier > 4) {
3607 af_period_multiplier = 4;
3608 }
3609 ALOGV("new period_multiplier = %d", af_period_multiplier);
3610 }
3611
vivek mehta1a9b7c02015-06-25 11:49:38 -07003612 pthread_mutex_unlock(&adev_init_lock);
3613
Haynes Mathew George88e6fb22015-08-19 11:51:34 -07003614 if (adev->adm_init)
3615 adev->adm_data = adev->adm_init();
3616
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07003617 audio_extn_perf_lock_init();
Haynes Mathew Georgec735fb02016-06-30 18:00:28 -07003618 audio_extn_snd_mon_init();
3619 pthread_mutex_lock(&adev->lock);
3620 audio_extn_snd_mon_register_listener(NULL, adev_snd_mon_cb);
3621 adev->card_status = CARD_STATUS_ONLINE;
3622 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda533bb722015-09-23 13:47:03 -07003623
Eric Laurent2bafff12016-03-17 12:17:23 -07003624 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003625 return 0;
3626}
3627
3628static struct hw_module_methods_t hal_module_methods = {
3629 .open = adev_open,
3630};
3631
3632struct audio_module HAL_MODULE_INFO_SYM = {
3633 .common = {
3634 .tag = HARDWARE_MODULE_TAG,
3635 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3636 .hal_api_version = HARDWARE_HAL_API_VERSION,
3637 .id = AUDIO_HARDWARE_MODULE_ID,
3638 .name = "QCOM Audio HAL",
3639 .author = "Code Aurora Forum",
3640 .methods = &hal_module_methods,
3641 },
3642};