1 /*
2 * Copyright (C) 2013-2016 The Android Open Source Project
3 *
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"
18 #define ATRACE_TAG ATRACE_TAG_AUDIO
19 /*#define LOG_NDEBUG 0*/
20 /*#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
26
27 #include <errno.h>
28 #include <pthread.h>
29 #include <stdint.h>
30 #include <sys/time.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <dlfcn.h>
34 #include <sys/resource.h>
35 #include <sys/prctl.h>
36 #include <limits.h>
37
38 #include <log/log.h>
39 #include <cutils/trace.h>
40 #include <cutils/str_parms.h>
41 #include <cutils/properties.h>
42 #include <cutils/atomic.h>
43 #include <utils/Timers.h> // systemTime
44
45 #include <hardware/audio_effect.h>
46 #include <hardware/audio_alsaops.h>
47 #include <processgroup/sched_policy.h>
48 #include <system/thread_defs.h>
49 #include <tinyalsa/asoundlib.h>
50 #include <audio_effects/effect_aec.h>
51 #include <audio_effects/effect_ns.h>
52 #include <audio_utils/clock.h>
53 #include "audio_hw.h"
54 #include "audio_extn.h"
55 #include "audio_perf.h"
56 #include "platform_api.h"
57 #include <platform.h>
58 #include "voice_extn.h"
59
60 #include "sound/compress_params.h"
61 #include "audio_extn/tfa_98xx.h"
62 #include "audio_extn/maxxaudio.h"
63 #include "audio_extn/audiozoom.h"
64
65 /* COMPRESS_OFFLOAD_FRAGMENT_SIZE must be more than 8KB and a multiple of 32KB if more than 32KB.
66 * COMPRESS_OFFLOAD_FRAGMENT_SIZE * COMPRESS_OFFLOAD_NUM_FRAGMENTS must be less than 8MB. */
67 #define COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024)
68 // 2 buffers causes problems with high bitrate files
69 #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 3
70 /* ToDo: Check and update a proper value in msec */
71 #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
72 /* treat as unsigned Q1.13 */
73 #define APP_TYPE_GAIN_DEFAULT 0x2000
74 #define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
75 #define PCM_PLAYBACK_VOLUME_MAX 0x2000
76 #define INVALID_OUT_VOLUME -1
77
78 /* treat as unsigned Q1.13 */
79 #define VOIP_PLAYBACK_VOLUME_MAX 0x2000
80
81 #define RECORD_GAIN_MIN 0.0f
82 #define RECORD_GAIN_MAX 1.0f
83 #define RECORD_VOLUME_CTL_MAX 0x2000
84
85 #define PROXY_OPEN_RETRY_COUNT 100
86 #define PROXY_OPEN_WAIT_TIME 20
87
88 #define MIN_CHANNEL_COUNT 1
89 #define DEFAULT_CHANNEL_COUNT 2
90
91 #ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT
92 #define MAX_CHANNEL_COUNT 1
93 #else
94 #define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT))
95 #define XSTR(x) STR(x)
96 #define STR(x) #x
97 #endif
98 #define MAX_HIFI_CHANNEL_COUNT 8
99
100 #define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
101
102 static unsigned int configured_low_latency_capture_period_size =
103 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
104
105
106 #define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
107 #define MMAP_PERIOD_COUNT_MIN 32
108 #define MMAP_PERIOD_COUNT_MAX 512
109 #define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX)
110 #define MMAP_MIN_SIZE_FRAMES_MAX 64 * 1024
111
112 /* This constant enables extended precision handling.
113 * TODO The flag is off until more testing is done.
114 */
115 static const bool k_enable_extended_precision = false;
116
117 struct pcm_config pcm_config_deep_buffer = {
118 .channels = DEFAULT_CHANNEL_COUNT,
119 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
120 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
121 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
122 .format = PCM_FORMAT_S16_LE,
123 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
124 .stop_threshold = INT_MAX,
125 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
126 };
127
128 struct pcm_config pcm_config_low_latency = {
129 .channels = DEFAULT_CHANNEL_COUNT,
130 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
131 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
132 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
133 .format = PCM_FORMAT_S16_LE,
134 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
135 .stop_threshold = INT_MAX,
136 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
137 };
138
139 struct pcm_config pcm_config_haptics_audio = {
140 .channels = 1,
141 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
142 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
143 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
144 .format = PCM_FORMAT_S16_LE,
145 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
146 .stop_threshold = INT_MAX,
147 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
148 };
149
150 struct pcm_config pcm_config_haptics = {
151 .channels = 1,
152 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
153 .period_count = 2,
154 .format = PCM_FORMAT_S16_LE,
155 .stop_threshold = INT_MAX,
156 .avail_min = 0,
157 };
158
159 static int af_period_multiplier = 4;
160 struct pcm_config pcm_config_rt = {
161 .channels = DEFAULT_CHANNEL_COUNT,
162 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
163 .period_size = ULL_PERIOD_SIZE, //1 ms
164 .period_count = 512, //=> buffer size is 512ms
165 .format = PCM_FORMAT_S16_LE,
166 .start_threshold = ULL_PERIOD_SIZE*8, //8ms
167 .stop_threshold = INT_MAX,
168 .silence_threshold = 0,
169 .silence_size = 0,
170 .avail_min = ULL_PERIOD_SIZE, //1 ms
171 };
172
173 struct pcm_config pcm_config_hdmi_multi = {
174 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
175 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
176 .period_size = HDMI_MULTI_PERIOD_SIZE,
177 .period_count = HDMI_MULTI_PERIOD_COUNT,
178 .format = PCM_FORMAT_S16_LE,
179 .start_threshold = 0,
180 .stop_threshold = INT_MAX,
181 .avail_min = 0,
182 };
183
184 struct pcm_config pcm_config_mmap_playback = {
185 .channels = DEFAULT_CHANNEL_COUNT,
186 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
187 .period_size = MMAP_PERIOD_SIZE,
188 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
189 .format = PCM_FORMAT_S16_LE,
190 .start_threshold = MMAP_PERIOD_SIZE*8,
191 .stop_threshold = INT32_MAX,
192 .silence_threshold = 0,
193 .silence_size = 0,
194 .avail_min = MMAP_PERIOD_SIZE, //1 ms
195 };
196
197 struct pcm_config pcm_config_hifi = {
198 .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
199 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
200 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
201 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
202 .format = PCM_FORMAT_S24_3LE,
203 .start_threshold = 0,
204 .stop_threshold = INT_MAX,
205 .avail_min = 0,
206 };
207
208 struct pcm_config pcm_config_audio_capture = {
209 .channels = DEFAULT_CHANNEL_COUNT,
210 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
211 .format = PCM_FORMAT_S16_LE,
212 .stop_threshold = INT_MAX,
213 .avail_min = 0,
214 };
215
216 struct pcm_config pcm_config_audio_capture_rt = {
217 .channels = DEFAULT_CHANNEL_COUNT,
218 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
219 .period_size = ULL_PERIOD_SIZE,
220 .period_count = 512,
221 .format = PCM_FORMAT_S16_LE,
222 .start_threshold = 0,
223 .stop_threshold = INT_MAX,
224 .silence_threshold = 0,
225 .silence_size = 0,
226 .avail_min = ULL_PERIOD_SIZE, //1 ms
227 };
228
229 struct pcm_config pcm_config_mmap_capture = {
230 .channels = DEFAULT_CHANNEL_COUNT,
231 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
232 .period_size = MMAP_PERIOD_SIZE,
233 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
234 .format = PCM_FORMAT_S16_LE,
235 .start_threshold = 0,
236 .stop_threshold = INT_MAX,
237 .silence_threshold = 0,
238 .silence_size = 0,
239 .avail_min = MMAP_PERIOD_SIZE, //1 ms
240 };
241
242 struct pcm_config pcm_config_voip = {
243 .channels = 1,
244 .period_count = 2,
245 .format = PCM_FORMAT_S16_LE,
246 .stop_threshold = INT_MAX,
247 .avail_min = 0,
248 };
249
250 #define AFE_PROXY_CHANNEL_COUNT 2
251 #define AFE_PROXY_SAMPLING_RATE 48000
252
253 #define AFE_PROXY_PLAYBACK_PERIOD_SIZE 256
254 #define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
255
256 struct pcm_config pcm_config_afe_proxy_playback = {
257 .channels = AFE_PROXY_CHANNEL_COUNT,
258 .rate = AFE_PROXY_SAMPLING_RATE,
259 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
260 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
261 .format = PCM_FORMAT_S16_LE,
262 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
263 .stop_threshold = INT_MAX,
264 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
265 };
266
267 #define AFE_PROXY_RECORD_PERIOD_SIZE 256
268 #define AFE_PROXY_RECORD_PERIOD_COUNT 4
269
270 struct pcm_config pcm_config_afe_proxy_record = {
271 .channels = AFE_PROXY_CHANNEL_COUNT,
272 .rate = AFE_PROXY_SAMPLING_RATE,
273 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
274 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
275 .format = PCM_FORMAT_S16_LE,
276 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
277 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
278 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
279 };
280
281 const char * const use_case_table[AUDIO_USECASE_MAX] = {
282 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
283 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
284 [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback",
285 [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
286 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
287 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
288 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
289 [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback",
290
291 [USECASE_AUDIO_RECORD] = "audio-record",
292 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
293 [USECASE_AUDIO_RECORD_MMAP] = "mmap-record",
294 [USECASE_AUDIO_RECORD_HIFI] = "hifi-record",
295
296 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
297 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
298
299 [USECASE_VOICE_CALL] = "voice-call",
300 [USECASE_VOICE2_CALL] = "voice2-call",
301 [USECASE_VOLTE_CALL] = "volte-call",
302 [USECASE_QCHAT_CALL] = "qchat-call",
303 [USECASE_VOWLAN_CALL] = "vowlan-call",
304 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
305 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
306
307 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
308 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
309
310 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
311 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
312
313 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
314 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
315 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
316
317 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
318 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
319
320 [USECASE_INCALL_MUSIC_UPLINK] = "incall-music-uplink",
321 [USECASE_INCALL_MUSIC_UPLINK2] = "incall-music-uplink2",
322
323 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback",
324 };
325
326
327 #define STRING_TO_ENUM(string) { #string, string }
328
329 struct string_to_enum {
330 const char *name;
331 uint32_t value;
332 };
333
334 static const struct string_to_enum channels_name_to_enum_table[] = {
335 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
336 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
337 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
338 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
339 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
340 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
341 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
342 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
343 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
344 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
345 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
346 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
347 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
348 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
349 };
350
351 struct in_effect_list {
352 struct listnode list;
353 effect_handle_t handle;
354 };
355
356 static int set_voice_volume_l(struct audio_device *adev, float volume);
357 static struct audio_device *adev = NULL;
358 static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
359 static unsigned int audio_device_ref_count;
360 //cache last MBDRC cal step level
361 static int last_known_cal_step = -1 ;
362
363 static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore);
364 static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right);
365 static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right);
366
367 static int in_set_microphone_direction(const struct audio_stream_in *stream,
368 audio_microphone_direction_t dir);
369 static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom);
370
may_use_noirq_mode(struct audio_device * adev,audio_usecase_t uc_id,int flags __unused)371 static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
372 int flags __unused)
373 {
374 int dir = 0;
375 switch (uc_id) {
376 case USECASE_AUDIO_RECORD_LOW_LATENCY:
377 dir = 1;
378 case USECASE_AUDIO_PLAYBACK_ULL:
379 break;
380 default:
381 return false;
382 }
383
384 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
385 PCM_PLAYBACK : PCM_CAPTURE);
386 if (adev->adm_is_noirq_avail)
387 return adev->adm_is_noirq_avail(adev->adm_data,
388 adev->snd_card, dev_id, dir);
389 return false;
390 }
391
register_out_stream(struct stream_out * out)392 static void register_out_stream(struct stream_out *out)
393 {
394 struct audio_device *adev = out->dev;
395 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
396 return;
397
398 if (!adev->adm_register_output_stream)
399 return;
400
401 adev->adm_register_output_stream(adev->adm_data,
402 out->handle,
403 out->flags);
404
405 if (!adev->adm_set_config)
406 return;
407
408 if (out->realtime) {
409 adev->adm_set_config(adev->adm_data,
410 out->handle,
411 out->pcm, &out->config);
412 }
413 }
414
register_in_stream(struct stream_in * in)415 static void register_in_stream(struct stream_in *in)
416 {
417 struct audio_device *adev = in->dev;
418 if (!adev->adm_register_input_stream)
419 return;
420
421 adev->adm_register_input_stream(adev->adm_data,
422 in->capture_handle,
423 in->flags);
424
425 if (!adev->adm_set_config)
426 return;
427
428 if (in->realtime) {
429 adev->adm_set_config(adev->adm_data,
430 in->capture_handle,
431 in->pcm,
432 &in->config);
433 }
434 }
435
request_out_focus(struct stream_out * out,long ns)436 static void request_out_focus(struct stream_out *out, long ns)
437 {
438 struct audio_device *adev = out->dev;
439
440 if (adev->adm_request_focus_v2) {
441 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
442 } else if (adev->adm_request_focus) {
443 adev->adm_request_focus(adev->adm_data, out->handle);
444 }
445 }
446
request_in_focus(struct stream_in * in,long ns)447 static void request_in_focus(struct stream_in *in, long ns)
448 {
449 struct audio_device *adev = in->dev;
450
451 if (adev->adm_request_focus_v2) {
452 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
453 } else if (adev->adm_request_focus) {
454 adev->adm_request_focus(adev->adm_data, in->capture_handle);
455 }
456 }
457
release_out_focus(struct stream_out * out,long ns __unused)458 static void release_out_focus(struct stream_out *out, long ns __unused)
459 {
460 struct audio_device *adev = out->dev;
461
462 if (adev->adm_abandon_focus)
463 adev->adm_abandon_focus(adev->adm_data, out->handle);
464 }
465
release_in_focus(struct stream_in * in,long ns __unused)466 static void release_in_focus(struct stream_in *in, long ns __unused)
467 {
468 struct audio_device *adev = in->dev;
469 if (adev->adm_abandon_focus)
470 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
471 }
472
parse_snd_card_status(struct str_parms * parms,int * card,card_status_t * status)473 static int parse_snd_card_status(struct str_parms * parms, int * card,
474 card_status_t * status)
475 {
476 char value[32]={0};
477 char state[32]={0};
478
479 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
480
481 if (ret < 0)
482 return -1;
483
484 // sscanf should be okay as value is of max length 32.
485 // same as sizeof state.
486 if (sscanf(value, "%d,%s", card, state) < 2)
487 return -1;
488
489 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
490 CARD_STATUS_OFFLINE;
491 return 0;
492 }
493
494 // always call with adev lock held
send_gain_dep_calibration_l()495 void send_gain_dep_calibration_l() {
496 if (last_known_cal_step >= 0)
497 platform_send_gain_dep_cal(adev->platform, last_known_cal_step);
498 }
499
500 __attribute__ ((visibility ("default")))
audio_hw_send_gain_dep_calibration(int level)501 bool audio_hw_send_gain_dep_calibration(int level) {
502 bool ret_val = false;
503 ALOGV("%s: enter ... ", __func__);
504
505 pthread_mutex_lock(&adev_init_lock);
506
507 if (adev != NULL && adev->platform != NULL) {
508 pthread_mutex_lock(&adev->lock);
509 last_known_cal_step = level;
510 send_gain_dep_calibration_l();
511 pthread_mutex_unlock(&adev->lock);
512 } else {
513 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
514 }
515
516 pthread_mutex_unlock(&adev_init_lock);
517
518 ALOGV("%s: exit with ret_val %d ", __func__, ret_val);
519 return ret_val;
520 }
521
522 #ifdef MAXXAUDIO_QDSP_ENABLED
audio_hw_send_ma_parameter(int stream_type,float vol,bool active)523 bool audio_hw_send_ma_parameter(int stream_type, float vol, bool active)
524 {
525 bool ret = false;
526 ALOGV("%s: enter ...", __func__);
527
528 pthread_mutex_lock(&adev_init_lock);
529
530 if (adev != NULL && adev->platform != NULL) {
531 pthread_mutex_lock(&adev->lock);
532 ret = audio_extn_ma_set_state(adev, stream_type, vol, active);
533 pthread_mutex_unlock(&adev->lock);
534 }
535
536 pthread_mutex_unlock(&adev_init_lock);
537
538 ALOGV("%s: exit with ret %d", __func__, ret);
539 return ret;
540 }
541 #else
542 #define audio_hw_send_ma_parameter(stream_type, vol, active) (0)
543 #endif
544
545 __attribute__ ((visibility ("default")))
audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table * mapping_tbl,int table_size)546 int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
547 int table_size) {
548 int ret_val = 0;
549 ALOGV("%s: enter ... ", __func__);
550
551 pthread_mutex_lock(&adev_init_lock);
552 if (adev == NULL) {
553 ALOGW("%s: adev is NULL .... ", __func__);
554 goto done;
555 }
556
557 pthread_mutex_lock(&adev->lock);
558 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
559 pthread_mutex_unlock(&adev->lock);
560 done:
561 pthread_mutex_unlock(&adev_init_lock);
562 ALOGV("%s: exit ... ", __func__);
563 return ret_val;
564 }
565
is_supported_format(audio_format_t format)566 static bool is_supported_format(audio_format_t format)
567 {
568 switch (format) {
569 case AUDIO_FORMAT_MP3:
570 case AUDIO_FORMAT_AAC_LC:
571 case AUDIO_FORMAT_AAC_HE_V1:
572 case AUDIO_FORMAT_AAC_HE_V2:
573 return true;
574 default:
575 break;
576 }
577 return false;
578 }
579
is_supported_24bits_audiosource(audio_source_t source)580 static bool is_supported_24bits_audiosource(audio_source_t source)
581 {
582 switch (source) {
583 case AUDIO_SOURCE_UNPROCESSED:
584 #ifdef ENABLED_24BITS_CAMCORDER
585 case AUDIO_SOURCE_CAMCORDER:
586 #endif
587 return true;
588 default:
589 break;
590 }
591 return false;
592 }
593
is_mmap_usecase(audio_usecase_t uc_id)594 static inline bool is_mmap_usecase(audio_usecase_t uc_id)
595 {
596 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
597 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
598 }
599
get_snd_codec_id(audio_format_t format)600 static int get_snd_codec_id(audio_format_t format)
601 {
602 int id = 0;
603
604 switch (format & AUDIO_FORMAT_MAIN_MASK) {
605 case AUDIO_FORMAT_MP3:
606 id = SND_AUDIOCODEC_MP3;
607 break;
608 case AUDIO_FORMAT_AAC:
609 id = SND_AUDIOCODEC_AAC;
610 break;
611 default:
612 ALOGE("%s: Unsupported audio format", __func__);
613 }
614
615 return id;
616 }
617
audio_ssr_status(struct audio_device * adev)618 static int audio_ssr_status(struct audio_device *adev)
619 {
620 int ret = 0;
621 struct mixer_ctl *ctl;
622 const char *mixer_ctl_name = "Audio SSR Status";
623
624 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
625 ret = mixer_ctl_get_value(ctl, 0);
626 ALOGD("%s: value: %d", __func__, ret);
627 return ret;
628 }
629
stream_app_type_cfg_init(struct stream_app_type_cfg * cfg)630 static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg)
631 {
632 cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT;
633 }
634
is_btsco_device(snd_device_t out_snd_device,snd_device_t in_snd_device)635 static bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
636 {
637 return out_snd_device == SND_DEVICE_OUT_BT_SCO ||
638 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
639 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
640 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
641 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
642 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC;
643
644 }
645
is_a2dp_device(snd_device_t out_snd_device)646 static bool is_a2dp_device(snd_device_t out_snd_device)
647 {
648 return out_snd_device == SND_DEVICE_OUT_BT_A2DP;
649 }
650
enable_audio_route(struct audio_device * adev,struct audio_usecase * usecase)651 int enable_audio_route(struct audio_device *adev,
652 struct audio_usecase *usecase)
653 {
654 snd_device_t snd_device;
655 char mixer_path[MIXER_PATH_MAX_LENGTH];
656
657 if (usecase == NULL)
658 return -EINVAL;
659
660 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
661
662 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
663
664 if (usecase->type == PCM_CAPTURE) {
665 struct stream_in *in = usecase->stream.in;
666 struct audio_usecase *uinfo;
667 snd_device = usecase->in_snd_device;
668
669 if (in) {
670 if (in->enable_aec || in->enable_ec_port) {
671 audio_devices_t out_device = AUDIO_DEVICE_OUT_SPEAKER;
672 struct listnode *node;
673 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
674 USECASE_AUDIO_PLAYBACK_VOIP);
675 if (voip_usecase) {
676 out_device = voip_usecase->stream.out->devices;
677 } else if (adev->primary_output &&
678 !adev->primary_output->standby) {
679 out_device = adev->primary_output->devices;
680 } else {
681 list_for_each(node, &adev->usecase_list) {
682 uinfo = node_to_item(node, struct audio_usecase, list);
683 if (uinfo->type != PCM_CAPTURE) {
684 out_device = uinfo->stream.out->devices;
685 break;
686 }
687 }
688 }
689 platform_set_echo_reference(adev, true, out_device);
690 in->ec_opened = true;
691 }
692 }
693 } else
694 snd_device = usecase->out_snd_device;
695 audio_extn_utils_send_app_type_cfg(adev, usecase);
696 audio_extn_ma_set_device(usecase);
697 audio_extn_utils_send_audio_calibration(adev, usecase);
698
699 // we shouldn't truncate mixer_path
700 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
701 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
702 // this also appends to mixer_path
703 platform_add_backend_name(adev->platform, mixer_path, snd_device);
704
705 ALOGD("%s: usecase(%d) apply and update mixer path: %s", __func__, usecase->id, mixer_path);
706 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
707
708 ALOGV("%s: exit", __func__);
709 return 0;
710 }
711
disable_audio_route(struct audio_device * adev,struct audio_usecase * usecase)712 int disable_audio_route(struct audio_device *adev,
713 struct audio_usecase *usecase)
714 {
715 snd_device_t snd_device;
716 char mixer_path[MIXER_PATH_MAX_LENGTH];
717
718 if (usecase == NULL)
719 return -EINVAL;
720
721 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
722 if (usecase->type == PCM_CAPTURE)
723 snd_device = usecase->in_snd_device;
724 else
725 snd_device = usecase->out_snd_device;
726
727 // we shouldn't truncate mixer_path
728 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
729 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
730 // this also appends to mixer_path
731 platform_add_backend_name(adev->platform, mixer_path, snd_device);
732 ALOGD("%s: usecase(%d) reset and update mixer path: %s", __func__, usecase->id, mixer_path);
733
734 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
735 if (usecase->type == PCM_CAPTURE) {
736 struct stream_in *in = usecase->stream.in;
737 if (in && in->ec_opened) {
738 platform_set_echo_reference(in->dev, false, AUDIO_DEVICE_NONE);
739 in->ec_opened = false;
740 }
741 }
742 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
743
744 ALOGV("%s: exit", __func__);
745 return 0;
746 }
747
enable_snd_device(struct audio_device * adev,snd_device_t snd_device)748 int enable_snd_device(struct audio_device *adev,
749 snd_device_t snd_device)
750 {
751 int i, num_devices = 0;
752 snd_device_t new_snd_devices[2];
753 int ret_val = -EINVAL;
754 if (snd_device < SND_DEVICE_MIN ||
755 snd_device >= SND_DEVICE_MAX) {
756 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
757 goto on_error;
758 }
759
760 platform_send_audio_calibration(adev->platform, snd_device);
761
762 if (adev->snd_dev_ref_cnt[snd_device] >= 1) {
763 ALOGV("%s: snd_device(%d: %s) is already active",
764 __func__, snd_device, platform_get_snd_device_name(snd_device));
765 goto on_success;
766 }
767
768 /* due to the possibility of calibration overwrite between listen
769 and audio, notify sound trigger hal before audio calibration is sent */
770 audio_extn_sound_trigger_update_device_status(snd_device,
771 ST_EVENT_SND_DEVICE_BUSY);
772
773 if (audio_extn_spkr_prot_is_enabled())
774 audio_extn_spkr_prot_calib_cancel(adev);
775
776 audio_extn_dsm_feedback_enable(adev, snd_device, true);
777
778 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
779 snd_device == SND_DEVICE_OUT_SPEAKER_SAFE ||
780 snd_device == SND_DEVICE_OUT_SPEAKER_REVERSE ||
781 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
782 audio_extn_spkr_prot_is_enabled()) {
783 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
784 goto on_error;
785 }
786 if (audio_extn_spkr_prot_start_processing(snd_device)) {
787 ALOGE("%s: spkr_start_processing failed", __func__);
788 goto on_error;
789 }
790 } else if (platform_can_split_snd_device(snd_device,
791 &num_devices,
792 new_snd_devices) == 0) {
793 for (i = 0; i < num_devices; i++) {
794 enable_snd_device(adev, new_snd_devices[i]);
795 }
796 platform_set_speaker_gain_in_combo(adev, snd_device, true);
797 } else {
798 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
799 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
800 ALOGE(" %s: Invalid sound device returned", __func__);
801 goto on_error;
802 }
803
804 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
805
806 if (is_a2dp_device(snd_device) &&
807 (audio_extn_a2dp_start_playback() < 0)) {
808 ALOGE("%s: failed to configure A2DP control path", __func__);
809 goto on_error;
810 }
811
812 audio_route_apply_and_update_path(adev->audio_route, device_name);
813 }
814 on_success:
815 adev->snd_dev_ref_cnt[snd_device]++;
816 ret_val = 0;
817 on_error:
818 return ret_val;
819 }
820
disable_snd_device(struct audio_device * adev,snd_device_t snd_device)821 int disable_snd_device(struct audio_device *adev,
822 snd_device_t snd_device)
823 {
824 int i, num_devices = 0;
825 snd_device_t new_snd_devices[2];
826
827 if (snd_device < SND_DEVICE_MIN ||
828 snd_device >= SND_DEVICE_MAX) {
829 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
830 return -EINVAL;
831 }
832 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
833 ALOGE("%s: device ref cnt is already 0", __func__);
834 return -EINVAL;
835 }
836 audio_extn_tfa_98xx_disable_speaker(snd_device);
837
838 adev->snd_dev_ref_cnt[snd_device]--;
839 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
840 audio_extn_dsm_feedback_enable(adev, snd_device, false);
841
842 if (is_a2dp_device(snd_device))
843 audio_extn_a2dp_stop_playback();
844
845 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
846 snd_device == SND_DEVICE_OUT_SPEAKER_SAFE ||
847 snd_device == SND_DEVICE_OUT_SPEAKER_REVERSE ||
848 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
849 audio_extn_spkr_prot_is_enabled()) {
850 audio_extn_spkr_prot_stop_processing(snd_device);
851
852 // FIXME b/65363602: bullhead is the only Nexus with audio_extn_spkr_prot_is_enabled()
853 // and does not use speaker swap. As this code causes a problem with device enable ref
854 // counting we remove it for now.
855 // when speaker device is disabled, reset swap.
856 // will be renabled on usecase start
857 // platform_set_swap_channels(adev, false);
858
859 } else if (platform_can_split_snd_device(snd_device,
860 &num_devices,
861 new_snd_devices) == 0) {
862 for (i = 0; i < num_devices; i++) {
863 disable_snd_device(adev, new_snd_devices[i]);
864 }
865 platform_set_speaker_gain_in_combo(adev, snd_device, false);
866 } else {
867 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
868 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
869 ALOGE(" %s: Invalid sound device returned", __func__);
870 return -EINVAL;
871 }
872
873 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
874 audio_route_reset_and_update_path(adev->audio_route, device_name);
875 }
876 audio_extn_sound_trigger_update_device_status(snd_device,
877 ST_EVENT_SND_DEVICE_FREE);
878 }
879
880 return 0;
881 }
882
883 #ifdef DYNAMIC_ECNS_ENABLED
send_effect_enable_disable_mixer_ctl(struct audio_device * adev,struct stream_in * in,struct audio_effect_config effect_config,unsigned int param_value)884 static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev,
885 struct stream_in *in,
886 struct audio_effect_config effect_config,
887 unsigned int param_value)
888 {
889 char mixer_ctl_name[] = "Audio Effect";
890 long set_values[6];
891
892 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
893 if (!ctl) {
894 ALOGE("%s: Could not get mixer ctl - %s",
895 __func__, mixer_ctl_name);
896 return -EINVAL;
897 }
898
899 set_values[0] = 1; //0:Rx 1:Tx
900 set_values[1] = in->app_type_cfg.app_type;
901 set_values[2] = (long)effect_config.module_id;
902 set_values[3] = (long)effect_config.instance_id;
903 set_values[4] = (long)effect_config.param_id;
904 set_values[5] = param_value;
905
906 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
907
908 return 0;
909
910 }
911
update_effect_param_ecns(struct audio_usecase * usecase,unsigned int module_id,int effect_type,unsigned int * param_value)912 static int update_effect_param_ecns(struct audio_usecase *usecase,
913 unsigned int module_id, int effect_type,
914 unsigned int *param_value)
915 {
916 int ret = 0;
917 struct audio_effect_config other_effect_config;
918 struct stream_in *in = NULL;
919
920 if (!usecase)
921 return -EINVAL;
922
923 in = usecase->stream.in;
924
925 /* Get the effect config data of the other effect */
926 ret = platform_get_effect_config_data(usecase->in_snd_device,
927 &other_effect_config,
928 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC);
929 if (ret < 0) {
930 ALOGE("%s Failed to get effect params %d", __func__, ret);
931 return ret;
932 }
933
934 if (module_id == other_effect_config.module_id) {
935 //Same module id for AEC/NS. Values need to be combined
936 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) ||
937 ((effect_type == EFFECT_NS) && (in->enable_aec)))
938 *param_value |= other_effect_config.param_value;
939 }
940
941 return ret;
942 }
943
enable_disable_effect(struct audio_device * adev,struct stream_in * in,int effect_type,bool enable)944 static int enable_disable_effect(struct audio_device *adev, struct stream_in *in,
945 int effect_type, bool enable)
946 {
947 struct audio_effect_config effect_config;
948 struct audio_usecase *usecase = NULL;
949 int ret = 0;
950 unsigned int param_value = 0;
951
952 if (!in) {
953 ALOGE("%s: Invalid input stream", __func__);
954 return -EINVAL;
955 }
956
957 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
958
959 usecase = get_usecase_from_list(adev, in->usecase);
960
961 ret = platform_get_effect_config_data(usecase->in_snd_device,
962 &effect_config, effect_type);
963 if (ret < 0) {
964 ALOGE("%s Failed to get module id %d", __func__, ret);
965 return ret;
966 }
967 ALOGV("%s: module %d app_type %d usecase->id:%d usecase->in_snd_device:%d",
968 __func__, effect_config.module_id, in->app_type_cfg.app_type,
969 usecase->id, usecase->in_snd_device);
970
971 if (enable)
972 param_value = effect_config.param_value;
973
974 /*Special handling for AEC & NS effects Param values need to be
975 updated if module ids are same*/
976
977 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) {
978 ret = update_effect_param_ecns(usecase, effect_config.module_id,
979 effect_type, ¶m_value);
980 if (ret < 0)
981 return ret;
982 }
983
984 ret = send_effect_enable_disable_mixer_ctl(adev, in,
985 effect_config, param_value);
986
987 return ret;
988 }
989
check_and_enable_effect(struct audio_device * adev)990 static int check_and_enable_effect(struct audio_device *adev)
991 {
992 int ret = 0;
993
994 struct listnode *node;
995 struct stream_in *in = NULL;
996
997 list_for_each(node, &adev->usecase_list)
998 {
999 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
1000 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL) {
1001 in = usecase->stream.in;
1002
1003 if (in->standby)
1004 continue;
1005
1006 if (in->enable_aec) {
1007 ret = enable_disable_effect(adev, in, EFFECT_AEC, true);
1008 }
1009
1010 if (in->enable_ns &&
1011 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1012 ret = enable_disable_effect(adev, in, EFFECT_NS, true);
1013 }
1014 }
1015 }
1016
1017 return ret;
1018 }
1019 #else
1020 #define enable_disable_effect(w, x, y, z) -ENOSYS
1021 #define check_and_enable_effect(x) -ENOSYS
1022 #endif
1023
1024 /*
1025 legend:
1026 uc - existing usecase
1027 new_uc - new usecase
1028 d1, d11, d2 - SND_DEVICE enums
1029 a1, a2 - corresponding ANDROID device enums
1030 B, B1, B2 - backend strings
1031
1032 case 1
1033 uc->dev d1 (a1) B1
1034 new_uc->dev d1 (a1), d2 (a2) B1, B2
1035
1036 resolution: disable and enable uc->dev on d1
1037
1038 case 2
1039 uc->dev d1 (a1) B1
1040 new_uc->dev d11 (a1) B1
1041
1042 resolution: need to switch uc since d1 and d11 are related
1043 (e.g. speaker and voice-speaker)
1044 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1045
1046 case 3
1047 uc->dev d1 (a1) B1
1048 new_uc->dev d2 (a2) B2
1049
1050 resolution: no need to switch uc
1051
1052 case 4
1053 uc->dev d1 (a1) B
1054 new_uc->dev d2 (a2) B
1055
1056 resolution: disable enable uc-dev on d2 since backends match
1057 we cannot enable two streams on two different devices if they
1058 share the same backend. e.g. if offload is on speaker device using
1059 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1060 using the same backend, offload must also be switched to voice-handset.
1061
1062 case 5
1063 uc->dev d1 (a1) B
1064 new_uc->dev d1 (a1), d2 (a2) B
1065
1066 resolution: disable enable uc-dev on d2 since backends match
1067 we cannot enable two streams on two different devices if they
1068 share the same backend.
1069
1070 case 6
1071 uc->dev d1 a1 B1
1072 new_uc->dev d2 a1 B2
1073
1074 resolution: no need to switch
1075
1076 case 7
1077
1078 uc->dev d1 (a1), d2 (a2) B1, B2
1079 new_uc->dev d1 B1
1080
1081 resolution: no need to switch
1082
1083 */
derive_playback_snd_device(struct audio_usecase * uc,struct audio_usecase * new_uc,snd_device_t new_snd_device)1084 static snd_device_t derive_playback_snd_device(struct audio_usecase *uc,
1085 struct audio_usecase *new_uc,
1086 snd_device_t new_snd_device)
1087 {
1088 audio_devices_t a1 = uc->stream.out->devices;
1089 audio_devices_t a2 = new_uc->stream.out->devices;
1090
1091 snd_device_t d1 = uc->out_snd_device;
1092 snd_device_t d2 = new_snd_device;
1093
1094 // Treat as a special case when a1 and a2 are not disjoint
1095 if ((a1 != a2) && (a1 & a2)) {
1096 snd_device_t d3[2];
1097 int num_devices = 0;
1098 int ret = platform_can_split_snd_device(popcount(a1) > 1 ? d1 : d2,
1099 &num_devices,
1100 d3);
1101 if (ret < 0) {
1102 if (ret != -ENOSYS) {
1103 ALOGW("%s failed to split snd_device %d",
1104 __func__,
1105 popcount(a1) > 1 ? d1 : d2);
1106 }
1107 goto end;
1108 }
1109
1110 // NB: case 7 is hypothetical and isn't a practical usecase yet.
1111 // But if it does happen, we need to give priority to d2 if
1112 // the combo devices active on the existing usecase share a backend.
1113 // This is because we cannot have a usecase active on a combo device
1114 // and a new usecase requests one device in this combo pair.
1115 if (platform_check_backends_match(d3[0], d3[1])) {
1116 return d2; // case 5
1117 } else {
1118 return d1; // case 1
1119 }
1120 } else {
1121 if (platform_check_backends_match(d1, d2)) {
1122 return d2; // case 2, 4
1123 } else {
1124 return d1; // case 6, 3
1125 }
1126 }
1127
1128 end:
1129 return d2; // return whatever was calculated before.
1130 }
1131
check_and_route_playback_usecases(struct audio_device * adev,struct audio_usecase * uc_info,snd_device_t snd_device)1132 static void check_and_route_playback_usecases(struct audio_device *adev,
1133 struct audio_usecase *uc_info,
1134 snd_device_t snd_device)
1135 {
1136 struct listnode *node;
1137 struct audio_usecase *usecase;
1138 bool switch_device[AUDIO_USECASE_MAX];
1139 int i, num_uc_to_switch = 0;
1140
1141 bool force_routing = platform_check_and_set_playback_backend_cfg(adev,
1142 uc_info,
1143 snd_device);
1144
1145 /* For a2dp device reconfigure all active sessions
1146 * with new AFE encoder format based on a2dp state
1147 */
1148 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
1149 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device ||
1150 SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device) &&
1151 audio_extn_a2dp_is_force_device_switch()) {
1152 force_routing = true;
1153 }
1154
1155 /*
1156 * This function is to make sure that all the usecases that are active on
1157 * the hardware codec backend are always routed to any one device that is
1158 * handled by the hardware codec.
1159 * For example, if low-latency and deep-buffer usecases are currently active
1160 * on speaker and out_set_parameters(headset) is received on low-latency
1161 * output, then we have to make sure deep-buffer is also switched to headset,
1162 * because of the limitation that both the devices cannot be enabled
1163 * at the same time as they share the same backend.
1164 */
1165 /* Disable all the usecases on the shared backend other than the
1166 specified usecase */
1167 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1168 switch_device[i] = false;
1169
1170 list_for_each(node, &adev->usecase_list) {
1171 usecase = node_to_item(node, struct audio_usecase, list);
1172 if (usecase->type == PCM_CAPTURE || usecase == uc_info)
1173 continue;
1174
1175 if (force_routing ||
1176 (usecase->out_snd_device != snd_device &&
1177 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND ||
1178 usecase->devices & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) &&
1179 platform_check_backends_match(snd_device, usecase->out_snd_device))) {
1180 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
1181 __func__, use_case_table[usecase->id],
1182 platform_get_snd_device_name(usecase->out_snd_device));
1183 disable_audio_route(adev, usecase);
1184 switch_device[usecase->id] = true;
1185 num_uc_to_switch++;
1186 }
1187 }
1188
1189 if (num_uc_to_switch) {
1190 list_for_each(node, &adev->usecase_list) {
1191 usecase = node_to_item(node, struct audio_usecase, list);
1192 if (switch_device[usecase->id]) {
1193 disable_snd_device(adev, usecase->out_snd_device);
1194 }
1195 }
1196
1197 snd_device_t d_device;
1198 list_for_each(node, &adev->usecase_list) {
1199 usecase = node_to_item(node, struct audio_usecase, list);
1200 if (switch_device[usecase->id]) {
1201 d_device = derive_playback_snd_device(usecase, uc_info,
1202 snd_device);
1203 enable_snd_device(adev, d_device);
1204 /* Update the out_snd_device before enabling the audio route */
1205 usecase->out_snd_device = d_device;
1206 }
1207 }
1208
1209 /* Re-route all the usecases on the shared backend other than the
1210 specified usecase to new snd devices */
1211 list_for_each(node, &adev->usecase_list) {
1212 usecase = node_to_item(node, struct audio_usecase, list);
1213 if (switch_device[usecase->id] ) {
1214 enable_audio_route(adev, usecase);
1215 if (usecase->stream.out && usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1216 struct stream_out *out = usecase->stream.out;
1217 audio_extn_utils_send_app_type_gain(out->dev,
1218 out->app_type_cfg.app_type,
1219 &out->app_type_cfg.gain[0]);
1220 }
1221 }
1222 }
1223 }
1224 }
1225
check_and_route_capture_usecases(struct audio_device * adev,struct audio_usecase * uc_info,snd_device_t snd_device)1226 static void check_and_route_capture_usecases(struct audio_device *adev,
1227 struct audio_usecase *uc_info,
1228 snd_device_t snd_device)
1229 {
1230 struct listnode *node;
1231 struct audio_usecase *usecase;
1232 bool switch_device[AUDIO_USECASE_MAX];
1233 int i, num_uc_to_switch = 0;
1234
1235 platform_check_and_set_capture_backend_cfg(adev, uc_info, snd_device);
1236
1237 /*
1238 * This function is to make sure that all the active capture usecases
1239 * are always routed to the same input sound device.
1240 * For example, if audio-record and voice-call usecases are currently
1241 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
1242 * is received for voice call then we have to make sure that audio-record
1243 * usecase is also switched to earpiece i.e. voice-dmic-ef,
1244 * because of the limitation that two devices cannot be enabled
1245 * at the same time if they share the same backend.
1246 */
1247 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1248 switch_device[i] = false;
1249
1250 list_for_each(node, &adev->usecase_list) {
1251 usecase = node_to_item(node, struct audio_usecase, list);
1252 if (usecase->type != PCM_PLAYBACK &&
1253 usecase != uc_info &&
1254 usecase->in_snd_device != snd_device &&
1255 ((uc_info->type == VOICE_CALL &&
1256 usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL) ||
1257 platform_check_backends_match(snd_device,\
1258 usecase->in_snd_device)) &&
1259 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
1260 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
1261 __func__, use_case_table[usecase->id],
1262 platform_get_snd_device_name(usecase->in_snd_device));
1263 disable_audio_route(adev, usecase);
1264 switch_device[usecase->id] = true;
1265 num_uc_to_switch++;
1266 }
1267 }
1268
1269 if (num_uc_to_switch) {
1270 list_for_each(node, &adev->usecase_list) {
1271 usecase = node_to_item(node, struct audio_usecase, list);
1272 if (switch_device[usecase->id]) {
1273 disable_snd_device(adev, usecase->in_snd_device);
1274 }
1275 }
1276
1277 list_for_each(node, &adev->usecase_list) {
1278 usecase = node_to_item(node, struct audio_usecase, list);
1279 if (switch_device[usecase->id]) {
1280 enable_snd_device(adev, snd_device);
1281 }
1282 }
1283
1284 /* Re-route all the usecases on the shared backend other than the
1285 specified usecase to new snd devices */
1286 list_for_each(node, &adev->usecase_list) {
1287 usecase = node_to_item(node, struct audio_usecase, list);
1288 /* Update the in_snd_device only before enabling the audio route */
1289 if (switch_device[usecase->id] ) {
1290 usecase->in_snd_device = snd_device;
1291 enable_audio_route(adev, usecase);
1292 }
1293 }
1294 }
1295 }
1296
1297 /* must be called with hw device mutex locked */
read_hdmi_channel_masks(struct stream_out * out)1298 static int read_hdmi_channel_masks(struct stream_out *out)
1299 {
1300 int ret = 0;
1301 int channels = platform_edid_get_max_channels(out->dev->platform);
1302
1303 switch (channels) {
1304 /*
1305 * Do not handle stereo output in Multi-channel cases
1306 * Stereo case is handled in normal playback path
1307 */
1308 case 6:
1309 ALOGV("%s: HDMI supports 5.1", __func__);
1310 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
1311 break;
1312 case 8:
1313 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
1314 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
1315 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
1316 break;
1317 default:
1318 ALOGE("HDMI does not support multi channel playback");
1319 ret = -ENOSYS;
1320 break;
1321 }
1322 return ret;
1323 }
1324
read_usb_sup_sample_rates(bool is_playback,uint32_t * supported_sample_rates,uint32_t max_rates)1325 static ssize_t read_usb_sup_sample_rates(bool is_playback,
1326 uint32_t *supported_sample_rates,
1327 uint32_t max_rates)
1328 {
1329 ssize_t count = audio_extn_usb_sup_sample_rates(is_playback,
1330 supported_sample_rates,
1331 max_rates);
1332 #if !LOG_NDEBUG
1333 for (ssize_t i=0; i<count; i++) {
1334 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
1335 supported_sample_rates[i]);
1336 }
1337 #endif
1338 return count;
1339 }
1340
read_usb_sup_channel_masks(bool is_playback,audio_channel_mask_t * supported_channel_masks,uint32_t max_masks)1341 static int read_usb_sup_channel_masks(bool is_playback,
1342 audio_channel_mask_t *supported_channel_masks,
1343 uint32_t max_masks)
1344 {
1345 int channels = audio_extn_usb_get_max_channels(is_playback);
1346 int channel_count;
1347 uint32_t num_masks = 0;
1348 if (channels > MAX_HIFI_CHANNEL_COUNT) {
1349 channels = MAX_HIFI_CHANNEL_COUNT;
1350 }
1351 if (is_playback) {
1352 // start from 2 channels as framework currently doesn't support mono.
1353 if (channels >= FCC_2) {
1354 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(FCC_2);
1355 }
1356 for (channel_count = FCC_2;
1357 channel_count <= channels && num_masks < max_masks;
1358 ++channel_count) {
1359 supported_channel_masks[num_masks++] =
1360 audio_channel_mask_for_index_assignment_from_count(channel_count);
1361 }
1362 } else {
1363 // For capture we report all supported channel masks from 1 channel up.
1364 channel_count = MIN_CHANNEL_COUNT;
1365 // audio_channel_in_mask_from_count() does the right conversion to either positional or
1366 // indexed mask
1367 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
1368 audio_channel_mask_t mask = AUDIO_CHANNEL_NONE;
1369 if (channel_count <= FCC_2) {
1370 mask = audio_channel_in_mask_from_count(channel_count);
1371 supported_channel_masks[num_masks++] = mask;
1372 }
1373 const audio_channel_mask_t index_mask =
1374 audio_channel_mask_for_index_assignment_from_count(channel_count);
1375 if (mask != index_mask && num_masks < max_masks) { // ensure index mask added.
1376 supported_channel_masks[num_masks++] = index_mask;
1377 }
1378 }
1379 }
1380 #ifdef NDEBUG
1381 for (size_t i = 0; i < num_masks; ++i) {
1382 ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__,
1383 is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks);
1384 }
1385 #endif
1386 return num_masks;
1387 }
1388
read_usb_sup_formats(bool is_playback __unused,audio_format_t * supported_formats,uint32_t max_formats __unused)1389 static int read_usb_sup_formats(bool is_playback __unused,
1390 audio_format_t *supported_formats,
1391 uint32_t max_formats __unused)
1392 {
1393 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
1394 switch (bitwidth) {
1395 case 24:
1396 // XXX : usb.c returns 24 for s24 and s24_le?
1397 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1398 break;
1399 case 32:
1400 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
1401 break;
1402 case 16:
1403 default :
1404 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
1405 break;
1406 }
1407 ALOGV("%s: %s supported format %d", __func__,
1408 is_playback ? "P" : "C", bitwidth);
1409 return 1;
1410 }
1411
read_usb_sup_params_and_compare(bool is_playback,audio_format_t * format,audio_format_t * supported_formats,uint32_t max_formats,audio_channel_mask_t * mask,audio_channel_mask_t * supported_channel_masks,uint32_t max_masks,uint32_t * rate,uint32_t * supported_sample_rates,uint32_t max_rates)1412 static int read_usb_sup_params_and_compare(bool is_playback,
1413 audio_format_t *format,
1414 audio_format_t *supported_formats,
1415 uint32_t max_formats,
1416 audio_channel_mask_t *mask,
1417 audio_channel_mask_t *supported_channel_masks,
1418 uint32_t max_masks,
1419 uint32_t *rate,
1420 uint32_t *supported_sample_rates,
1421 uint32_t max_rates) {
1422 int ret = 0;
1423 int num_formats;
1424 int num_masks;
1425 int num_rates;
1426 int i;
1427
1428 num_formats = read_usb_sup_formats(is_playback, supported_formats,
1429 max_formats);
1430 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
1431 max_masks);
1432
1433 num_rates = read_usb_sup_sample_rates(is_playback,
1434 supported_sample_rates, max_rates);
1435
1436 #define LUT(table, len, what, dflt) \
1437 for (i=0; i<len && (table[i] != what); i++); \
1438 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
1439
1440 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
1441 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
1442 LUT(supported_sample_rates, num_rates, *rate, 0);
1443
1444 #undef LUT
1445 return ret < 0 ? -EINVAL : 0; // HACK TBD
1446 }
1447
is_usb_ready(struct audio_device * adev,bool is_playback)1448 static bool is_usb_ready(struct audio_device *adev, bool is_playback)
1449 {
1450 // Check if usb is ready.
1451 // The usb device may have been removed quickly after insertion and hence
1452 // no longer available. This will show up as empty channel masks, or rates.
1453
1454 pthread_mutex_lock(&adev->lock);
1455 uint32_t supported_sample_rate;
1456
1457 // we consider usb ready if we can fetch at least one sample rate.
1458 const bool ready = read_usb_sup_sample_rates(
1459 is_playback, &supported_sample_rate, 1 /* max_rates */) > 0;
1460 pthread_mutex_unlock(&adev->lock);
1461 return ready;
1462 }
1463
get_voice_usecase_id_from_list(struct audio_device * adev)1464 static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
1465 {
1466 struct audio_usecase *usecase;
1467 struct listnode *node;
1468
1469 list_for_each(node, &adev->usecase_list) {
1470 usecase = node_to_item(node, struct audio_usecase, list);
1471 if (usecase->type == VOICE_CALL) {
1472 ALOGV("%s: usecase id %d", __func__, usecase->id);
1473 return usecase->id;
1474 }
1475 }
1476 return USECASE_INVALID;
1477 }
1478
get_usecase_from_list(struct audio_device * adev,audio_usecase_t uc_id)1479 struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
1480 audio_usecase_t uc_id)
1481 {
1482 struct audio_usecase *usecase;
1483 struct listnode *node;
1484
1485 list_for_each(node, &adev->usecase_list) {
1486 usecase = node_to_item(node, struct audio_usecase, list);
1487 if (usecase->id == uc_id)
1488 return usecase;
1489 }
1490 return NULL;
1491 }
1492
force_device_switch(struct audio_usecase * usecase)1493 static bool force_device_switch(struct audio_usecase *usecase)
1494 {
1495 if (usecase->type == PCM_CAPTURE || usecase->stream.out == NULL) {
1496 return false;
1497 }
1498
1499 // Force all A2DP output devices to reconfigure for proper AFE encode format
1500 // Also handle a case where in earlier A2DP start failed as A2DP stream was
1501 // in suspended state, hence try to trigger a retry when we again get a routing request.
1502 if ((usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
1503 audio_extn_a2dp_is_force_device_switch()) {
1504 ALOGD("%s: Force A2DP device switch to update new encoder config", __func__);
1505 return true;
1506 }
1507
1508 return false;
1509 }
1510
adev_get_active_input(const struct audio_device * adev)1511 struct stream_in *adev_get_active_input(const struct audio_device *adev)
1512 {
1513 struct listnode *node;
1514 struct stream_in *last_active_in = NULL;
1515
1516 /* Get last added active input.
1517 * TODO: We may use a priority mechanism to pick highest priority active source */
1518 list_for_each(node, &adev->usecase_list)
1519 {
1520 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
1521 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL) {
1522 last_active_in = usecase->stream.in;
1523 }
1524 }
1525
1526 return last_active_in;
1527 }
1528
get_voice_communication_input(const struct audio_device * adev)1529 struct stream_in *get_voice_communication_input(const struct audio_device *adev)
1530 {
1531 struct listnode *node;
1532
1533 /* First check active inputs with voice communication source and then
1534 * any input if audio mode is in communication */
1535 list_for_each(node, &adev->usecase_list)
1536 {
1537 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
1538 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL &&
1539 usecase->stream.in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1540 return usecase->stream.in;
1541 }
1542 }
1543 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
1544 return adev_get_active_input(adev);
1545 }
1546 return NULL;
1547 }
1548
1549 /*
1550 * Aligned with policy.h
1551 */
source_priority(int inputSource)1552 static inline int source_priority(int inputSource)
1553 {
1554 switch (inputSource) {
1555 case AUDIO_SOURCE_VOICE_COMMUNICATION:
1556 return 9;
1557 case AUDIO_SOURCE_CAMCORDER:
1558 return 8;
1559 case AUDIO_SOURCE_VOICE_PERFORMANCE:
1560 return 7;
1561 case AUDIO_SOURCE_UNPROCESSED:
1562 return 6;
1563 case AUDIO_SOURCE_MIC:
1564 return 5;
1565 case AUDIO_SOURCE_ECHO_REFERENCE:
1566 return 4;
1567 case AUDIO_SOURCE_FM_TUNER:
1568 return 3;
1569 case AUDIO_SOURCE_VOICE_RECOGNITION:
1570 return 2;
1571 case AUDIO_SOURCE_HOTWORD:
1572 return 1;
1573 default:
1574 break;
1575 }
1576 return 0;
1577 }
1578
get_priority_input(struct audio_device * adev)1579 static struct stream_in *get_priority_input(struct audio_device *adev)
1580 {
1581 struct listnode *node;
1582 struct audio_usecase *usecase;
1583 int last_priority = 0, priority;
1584 struct stream_in *priority_in = NULL;
1585 struct stream_in *in;
1586
1587 list_for_each(node, &adev->usecase_list) {
1588 usecase = node_to_item(node, struct audio_usecase, list);
1589 if (usecase->type == PCM_CAPTURE) {
1590 in = usecase->stream.in;
1591 if (!in)
1592 continue;
1593 priority = source_priority(in->source);
1594
1595 if (priority > last_priority) {
1596 last_priority = priority;
1597 priority_in = in;
1598 }
1599 }
1600 }
1601 return priority_in;
1602 }
1603
select_devices_with_force_switch(struct audio_device * adev,audio_usecase_t uc_id,bool force_switch)1604 int select_devices_with_force_switch(struct audio_device *adev,
1605 audio_usecase_t uc_id,
1606 bool force_switch)
1607 {
1608 snd_device_t out_snd_device = SND_DEVICE_NONE;
1609 snd_device_t in_snd_device = SND_DEVICE_NONE;
1610 struct audio_usecase *usecase = NULL;
1611 struct audio_usecase *vc_usecase = NULL;
1612 struct audio_usecase *hfp_usecase = NULL;
1613 audio_usecase_t hfp_ucid;
1614 struct listnode *node;
1615 int status = 0;
1616 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
1617 USECASE_AUDIO_PLAYBACK_VOIP);
1618
1619 usecase = get_usecase_from_list(adev, uc_id);
1620 if (usecase == NULL) {
1621 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
1622 return -EINVAL;
1623 }
1624
1625 if ((usecase->type == VOICE_CALL) ||
1626 (usecase->type == PCM_HFP_CALL)) {
1627 out_snd_device = platform_get_output_snd_device(adev->platform,
1628 usecase->stream.out->devices);
1629 in_snd_device = platform_get_input_snd_device(adev->platform,
1630 NULL,
1631 usecase->stream.out->devices);
1632 usecase->devices = usecase->stream.out->devices;
1633 } else {
1634 /*
1635 * If the voice call is active, use the sound devices of voice call usecase
1636 * so that it would not result any device switch. All the usecases will
1637 * be switched to new device when select_devices() is called for voice call
1638 * usecase. This is to avoid switching devices for voice call when
1639 * check_and_route_playback_usecases() is called below.
1640 */
1641 if (voice_is_in_call(adev)) {
1642 vc_usecase = get_usecase_from_list(adev,
1643 get_voice_usecase_id_from_list(adev));
1644 if ((vc_usecase != NULL) &&
1645 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
1646 (vc_usecase->devices == AUDIO_DEVICE_OUT_HEARING_AID) ||
1647 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
1648 in_snd_device = vc_usecase->in_snd_device;
1649 out_snd_device = vc_usecase->out_snd_device;
1650 }
1651 } else if (audio_extn_hfp_is_active(adev)) {
1652 hfp_ucid = audio_extn_hfp_get_usecase();
1653 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
1654 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
1655 in_snd_device = hfp_usecase->in_snd_device;
1656 out_snd_device = hfp_usecase->out_snd_device;
1657 }
1658 }
1659 if (usecase->type == PCM_PLAYBACK) {
1660 usecase->devices = usecase->stream.out->devices;
1661 in_snd_device = SND_DEVICE_NONE;
1662 if (out_snd_device == SND_DEVICE_NONE) {
1663 struct stream_out *voip_out = adev->primary_output;
1664 struct stream_in *voip_in = get_voice_communication_input(adev);
1665
1666 out_snd_device = platform_get_output_snd_device(adev->platform,
1667 usecase->stream.out->devices);
1668
1669 if (voip_usecase)
1670 voip_out = voip_usecase->stream.out;
1671
1672 if (usecase->stream.out == voip_out && voip_in != NULL) {
1673 select_devices(adev, voip_in->usecase);
1674 }
1675 }
1676 } else if (usecase->type == PCM_CAPTURE) {
1677 usecase->devices = usecase->stream.in->device;
1678 out_snd_device = SND_DEVICE_NONE;
1679 if (in_snd_device == SND_DEVICE_NONE) {
1680 audio_devices_t out_device = AUDIO_DEVICE_NONE;
1681 struct stream_in *voip_in = get_voice_communication_input(adev);
1682 struct stream_in *priority_in = NULL;
1683
1684 if (voip_in != NULL) {
1685 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
1686 USECASE_AUDIO_PLAYBACK_VOIP);
1687
1688 usecase->stream.in->enable_ec_port = false;
1689
1690 if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
1691 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
1692 } else if (voip_usecase) {
1693 out_device = voip_usecase->stream.out->devices;
1694 } else if (adev->primary_output &&
1695 !adev->primary_output->standby) {
1696 out_device = adev->primary_output->devices;
1697 } else {
1698 /* forcing speaker o/p device to get matching i/p pair
1699 in case o/p is not routed from same primary HAL */
1700 out_device = AUDIO_DEVICE_OUT_SPEAKER;
1701 }
1702 priority_in = voip_in;
1703 } else {
1704 /* get the input with the highest priority source*/
1705 priority_in = get_priority_input(adev);
1706
1707 if (!priority_in)
1708 priority_in = usecase->stream.in;
1709 }
1710
1711 in_snd_device = platform_get_input_snd_device(adev->platform,
1712 priority_in,
1713 out_device);
1714 }
1715 }
1716 }
1717
1718 if (out_snd_device == usecase->out_snd_device &&
1719 in_snd_device == usecase->in_snd_device) {
1720 if (!force_device_switch(usecase) && !force_switch)
1721 return 0;
1722 }
1723
1724 if (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_is_ready()) {
1725 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
1726 return 0;
1727 }
1728
1729 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
1730 out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) &&
1731 (!audio_extn_a2dp_is_ready())) {
1732 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
1733 if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)
1734 out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
1735 else
1736 out_snd_device = SND_DEVICE_OUT_SPEAKER;
1737 }
1738
1739 if (usecase->id == USECASE_INCALL_MUSIC_UPLINK ||
1740 usecase->id == USECASE_INCALL_MUSIC_UPLINK2) {
1741 out_snd_device = SND_DEVICE_OUT_VOICE_MUSIC_TX;
1742 }
1743
1744 if (out_snd_device != SND_DEVICE_NONE &&
1745 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
1746 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
1747 __func__,
1748 use_case_table[uc_id],
1749 adev->last_logged_snd_device[uc_id][0],
1750 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
1751 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
1752 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
1753 -1,
1754 out_snd_device,
1755 platform_get_snd_device_name(out_snd_device),
1756 platform_get_snd_device_acdb_id(out_snd_device));
1757 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
1758 }
1759 if (in_snd_device != SND_DEVICE_NONE &&
1760 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
1761 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
1762 __func__,
1763 use_case_table[uc_id],
1764 adev->last_logged_snd_device[uc_id][1],
1765 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
1766 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
1767 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
1768 -1,
1769 in_snd_device,
1770 platform_get_snd_device_name(in_snd_device),
1771 platform_get_snd_device_acdb_id(in_snd_device));
1772 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
1773 }
1774
1775 /*
1776 * Limitation: While in call, to do a device switch we need to disable
1777 * and enable both RX and TX devices though one of them is same as current
1778 * device.
1779 */
1780 if ((usecase->type == VOICE_CALL) &&
1781 (usecase->in_snd_device != SND_DEVICE_NONE) &&
1782 (usecase->out_snd_device != SND_DEVICE_NONE)) {
1783 status = platform_switch_voice_call_device_pre(adev->platform);
1784 /* Disable sidetone only if voice call already exists */
1785 if (voice_is_call_state_active(adev))
1786 voice_set_sidetone(adev, usecase->out_snd_device, false);
1787 }
1788
1789 /* Disable current sound devices */
1790 if (usecase->out_snd_device != SND_DEVICE_NONE) {
1791 disable_audio_route(adev, usecase);
1792 disable_snd_device(adev, usecase->out_snd_device);
1793 }
1794
1795 if (usecase->in_snd_device != SND_DEVICE_NONE) {
1796 disable_audio_route(adev, usecase);
1797 disable_snd_device(adev, usecase->in_snd_device);
1798 }
1799
1800 /* Applicable only on the targets that has external modem.
1801 * New device information should be sent to modem before enabling
1802 * the devices to reduce in-call device switch time.
1803 */
1804 if ((usecase->type == VOICE_CALL) &&
1805 (usecase->in_snd_device != SND_DEVICE_NONE) &&
1806 (usecase->out_snd_device != SND_DEVICE_NONE)) {
1807 status = platform_switch_voice_call_enable_device_config(adev->platform,
1808 out_snd_device,
1809 in_snd_device);
1810 }
1811
1812 /* Enable new sound devices */
1813 if (out_snd_device != SND_DEVICE_NONE) {
1814 if ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
1815 (usecase->devices & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) ||
1816 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP))
1817 check_and_route_playback_usecases(adev, usecase, out_snd_device);
1818 enable_snd_device(adev, out_snd_device);
1819 }
1820
1821 if (in_snd_device != SND_DEVICE_NONE) {
1822 check_and_route_capture_usecases(adev, usecase, in_snd_device);
1823 enable_snd_device(adev, in_snd_device);
1824 }
1825
1826 if (usecase->type == VOICE_CALL)
1827 status = platform_switch_voice_call_device_post(adev->platform,
1828 out_snd_device,
1829 in_snd_device);
1830
1831 usecase->in_snd_device = in_snd_device;
1832 usecase->out_snd_device = out_snd_device;
1833
1834 audio_extn_tfa_98xx_set_mode();
1835
1836 enable_audio_route(adev, usecase);
1837
1838 /* If input stream is already running the effect needs to be
1839 applied on the new input device that's being enabled here. */
1840 if (in_snd_device != SND_DEVICE_NONE)
1841 check_and_enable_effect(adev);
1842
1843 /* Applicable only on the targets that has external modem.
1844 * Enable device command should be sent to modem only after
1845 * enabling voice call mixer controls
1846 */
1847 if (usecase->type == VOICE_CALL) {
1848 status = platform_switch_voice_call_usecase_route_post(adev->platform,
1849 out_snd_device,
1850 in_snd_device);
1851 /* Enable sidetone only if voice call already exists */
1852 if (voice_is_call_state_active(adev))
1853 voice_set_sidetone(adev, out_snd_device, true);
1854 }
1855
1856 if (usecase->type != PCM_CAPTURE && voip_usecase) {
1857 struct stream_out *voip_out = voip_usecase->stream.out;
1858 audio_extn_utils_send_app_type_gain(adev,
1859 voip_out->app_type_cfg.app_type,
1860 &voip_out->app_type_cfg.gain[0]);
1861 }
1862 return status;
1863 }
1864
select_devices(struct audio_device * adev,audio_usecase_t uc_id)1865 int select_devices(struct audio_device *adev,
1866 audio_usecase_t uc_id)
1867 {
1868 return select_devices_with_force_switch(adev, uc_id, false);
1869 }
1870
stop_input_stream(struct stream_in * in)1871 static int stop_input_stream(struct stream_in *in)
1872 {
1873 int i, ret = 0;
1874 struct audio_usecase *uc_info;
1875 struct audio_device *adev = in->dev;
1876 struct stream_in *priority_in = NULL;
1877
1878 ALOGV("%s: enter: usecase(%d: %s)", __func__,
1879 in->usecase, use_case_table[in->usecase]);
1880
1881 uc_info = get_usecase_from_list(adev, in->usecase);
1882 if (uc_info == NULL) {
1883 ALOGE("%s: Could not find the usecase (%d) in the list",
1884 __func__, in->usecase);
1885 return -EINVAL;
1886 }
1887
1888 priority_in = get_priority_input(adev);
1889
1890 /* Close in-call recording streams */
1891 voice_check_and_stop_incall_rec_usecase(adev, in);
1892
1893 /* 1. Disable stream specific mixer controls */
1894 disable_audio_route(adev, uc_info);
1895
1896 /* 2. Disable the tx device */
1897 disable_snd_device(adev, uc_info->in_snd_device);
1898
1899 list_remove(&uc_info->list);
1900 free(uc_info);
1901
1902 if (priority_in == in) {
1903 priority_in = get_priority_input(adev);
1904 if (priority_in)
1905 select_devices(adev, priority_in->usecase);
1906 }
1907
1908 ALOGV("%s: exit: status(%d)", __func__, ret);
1909 return ret;
1910 }
1911
start_input_stream(struct stream_in * in)1912 int start_input_stream(struct stream_in *in)
1913 {
1914 /* 1. Enable output device and stream routing controls */
1915 int ret = 0;
1916 struct audio_usecase *uc_info;
1917 struct audio_device *adev = in->dev;
1918
1919 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
1920
1921 if (audio_extn_tfa_98xx_is_supported() && !audio_ssr_status(adev))
1922 return -EIO;
1923
1924 if (in->card_status == CARD_STATUS_OFFLINE ||
1925 adev->card_status == CARD_STATUS_OFFLINE) {
1926 ALOGW("in->card_status or adev->card_status offline, try again");
1927 ret = -EAGAIN;
1928 goto error_config;
1929 }
1930
1931 /* Check if source matches incall recording usecase criteria */
1932 ret = voice_check_and_set_incall_rec_usecase(adev, in);
1933 if (ret)
1934 goto error_config;
1935 else
1936 ALOGV("%s: usecase(%d)", __func__, in->usecase);
1937
1938 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
1939 if (in->pcm_device_id < 0) {
1940 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
1941 __func__, in->usecase);
1942 ret = -EINVAL;
1943 goto error_config;
1944 }
1945
1946 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1947 uc_info->id = in->usecase;
1948 uc_info->type = PCM_CAPTURE;
1949 uc_info->stream.in = in;
1950 uc_info->devices = in->device;
1951 uc_info->in_snd_device = SND_DEVICE_NONE;
1952 uc_info->out_snd_device = SND_DEVICE_NONE;
1953
1954 list_add_tail(&adev->usecase_list, &uc_info->list);
1955
1956 audio_streaming_hint_start();
1957 audio_extn_perf_lock_acquire();
1958
1959 select_devices(adev, in->usecase);
1960
1961 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
1962 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
1963 ALOGE("%s: pcm stream not ready", __func__);
1964 goto error_open;
1965 }
1966 ret = pcm_start(in->pcm);
1967 if (ret < 0) {
1968 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
1969 goto error_open;
1970 }
1971 } else {
1972 unsigned int flags = PCM_IN | PCM_MONOTONIC;
1973 unsigned int pcm_open_retry_count = 0;
1974
1975 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1976 flags |= PCM_MMAP | PCM_NOIRQ;
1977 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1978 } else if (in->realtime) {
1979 flags |= PCM_MMAP | PCM_NOIRQ;
1980 }
1981
1982 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
1983 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
1984
1985 while (1) {
1986 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
1987 flags, &in->config);
1988 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
1989 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
1990 if (in->pcm != NULL) {
1991 pcm_close(in->pcm);
1992 in->pcm = NULL;
1993 }
1994 if (pcm_open_retry_count-- == 0) {
1995 ret = -EIO;
1996 goto error_open;
1997 }
1998 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1999 continue;
2000 }
2001 break;
2002 }
2003
2004 ALOGV("%s: pcm_prepare", __func__);
2005 ret = pcm_prepare(in->pcm);
2006 if (ret < 0) {
2007 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
2008 pcm_close(in->pcm);
2009 in->pcm = NULL;
2010 goto error_open;
2011 }
2012 if (in->realtime) {
2013 ret = pcm_start(in->pcm);
2014 if (ret < 0) {
2015 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
2016 pcm_close(in->pcm);
2017 in->pcm = NULL;
2018 goto error_open;
2019 }
2020 }
2021 }
2022 register_in_stream(in);
2023 check_and_enable_effect(adev);
2024 audio_extn_audiozoom_set_microphone_direction(in, in->zoom);
2025 audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction);
2026 audio_streaming_hint_end();
2027 audio_extn_perf_lock_release();
2028 ALOGV("%s: exit", __func__);
2029
2030 return 0;
2031
2032 error_open:
2033 stop_input_stream(in);
2034 audio_streaming_hint_end();
2035 audio_extn_perf_lock_release();
2036
2037 error_config:
2038 ALOGW("%s: exit: status(%d)", __func__, ret);
2039 return ret;
2040 }
2041
lock_input_stream(struct stream_in * in)2042 void lock_input_stream(struct stream_in *in)
2043 {
2044 pthread_mutex_lock(&in->pre_lock);
2045 pthread_mutex_lock(&in->lock);
2046 pthread_mutex_unlock(&in->pre_lock);
2047 }
2048
lock_output_stream(struct stream_out * out)2049 void lock_output_stream(struct stream_out *out)
2050 {
2051 pthread_mutex_lock(&out->pre_lock);
2052 pthread_mutex_lock(&out->lock);
2053 pthread_mutex_unlock(&out->pre_lock);
2054 }
2055
2056 /* must be called with out->lock locked */
send_offload_cmd_l(struct stream_out * out,int command)2057 static int send_offload_cmd_l(struct stream_out* out, int command)
2058 {
2059 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
2060
2061 ALOGVV("%s %d", __func__, command);
2062
2063 cmd->cmd = command;
2064 list_add_tail(&out->offload_cmd_list, &cmd->node);
2065 pthread_cond_signal(&out->offload_cond);
2066 return 0;
2067 }
2068
2069 /* must be called iwth out->lock locked */
stop_compressed_output_l(struct stream_out * out)2070 static void stop_compressed_output_l(struct stream_out *out)
2071 {
2072 out->offload_state = OFFLOAD_STATE_IDLE;
2073 out->playback_started = 0;
2074 out->send_new_metadata = 1;
2075 if (out->compr != NULL) {
2076 compress_stop(out->compr);
2077 while (out->offload_thread_blocked) {
2078 pthread_cond_wait(&out->cond, &out->lock);
2079 }
2080 }
2081 }
2082
offload_thread_loop(void * context)2083 static void *offload_thread_loop(void *context)
2084 {
2085 struct stream_out *out = (struct stream_out *) context;
2086 struct listnode *item;
2087
2088 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
2089 set_sched_policy(0, SP_FOREGROUND);
2090 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
2091
2092 ALOGV("%s", __func__);
2093
2094 lock_output_stream(out);
2095 out->offload_state = OFFLOAD_STATE_IDLE;
2096 out->playback_started = 0;
2097 for (;;) {
2098 struct offload_cmd *cmd = NULL;
2099 stream_callback_event_t event;
2100 bool send_callback = false;
2101
2102 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
2103 __func__, list_empty(&out->offload_cmd_list),
2104 out->offload_state);
2105 if (list_empty(&out->offload_cmd_list)) {
2106 ALOGV("%s SLEEPING", __func__);
2107 pthread_cond_wait(&out->offload_cond, &out->lock);
2108 ALOGV("%s RUNNING", __func__);
2109 continue;
2110 }
2111
2112 item = list_head(&out->offload_cmd_list);
2113 cmd = node_to_item(item, struct offload_cmd, node);
2114 list_remove(item);
2115
2116 ALOGVV("%s STATE %d CMD %d out->compr %p",
2117 __func__, out->offload_state, cmd->cmd, out->compr);
2118
2119 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
2120 free(cmd);
2121 break;
2122 }
2123
2124 if (out->compr == NULL) {
2125 ALOGE("%s: Compress handle is NULL", __func__);
2126 free(cmd);
2127 pthread_cond_signal(&out->cond);
2128 continue;
2129 }
2130 out->offload_thread_blocked = true;
2131 pthread_mutex_unlock(&out->lock);
2132 send_callback = false;
2133 switch (cmd->cmd) {
2134 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
2135 compress_wait(out->compr, -1);
2136 send_callback = true;
2137 event = STREAM_CBK_EVENT_WRITE_READY;
2138 break;
2139 case OFFLOAD_CMD_PARTIAL_DRAIN:
2140 compress_next_track(out->compr);
2141 compress_partial_drain(out->compr);
2142 send_callback = true;
2143 event = STREAM_CBK_EVENT_DRAIN_READY;
2144 /* Resend the metadata for next iteration */
2145 out->send_new_metadata = 1;
2146 break;
2147 case OFFLOAD_CMD_DRAIN:
2148 compress_drain(out->compr);
2149 send_callback = true;
2150 event = STREAM_CBK_EVENT_DRAIN_READY;
2151 break;
2152 case OFFLOAD_CMD_ERROR:
2153 send_callback = true;
2154 event = STREAM_CBK_EVENT_ERROR;
2155 break;
2156 default:
2157 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
2158 break;
2159 }
2160 lock_output_stream(out);
2161 out->offload_thread_blocked = false;
2162 pthread_cond_signal(&out->cond);
2163 if (send_callback) {
2164 ALOGVV("%s: sending offload_callback event %d", __func__, event);
2165 out->offload_callback(event, NULL, out->offload_cookie);
2166 }
2167 free(cmd);
2168 }
2169
2170 pthread_cond_signal(&out->cond);
2171 while (!list_empty(&out->offload_cmd_list)) {
2172 item = list_head(&out->offload_cmd_list);
2173 list_remove(item);
2174 free(node_to_item(item, struct offload_cmd, node));
2175 }
2176 pthread_mutex_unlock(&out->lock);
2177
2178 return NULL;
2179 }
2180
create_offload_callback_thread(struct stream_out * out)2181 static int create_offload_callback_thread(struct stream_out *out)
2182 {
2183 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
2184 list_init(&out->offload_cmd_list);
2185 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
2186 offload_thread_loop, out);
2187 return 0;
2188 }
2189
destroy_offload_callback_thread(struct stream_out * out)2190 static int destroy_offload_callback_thread(struct stream_out *out)
2191 {
2192 lock_output_stream(out);
2193 stop_compressed_output_l(out);
2194 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
2195
2196 pthread_mutex_unlock(&out->lock);
2197 pthread_join(out->offload_thread, (void **) NULL);
2198 pthread_cond_destroy(&out->offload_cond);
2199
2200 return 0;
2201 }
2202
allow_hdmi_channel_config(struct audio_device * adev)2203 static bool allow_hdmi_channel_config(struct audio_device *adev)
2204 {
2205 struct listnode *node;
2206 struct audio_usecase *usecase;
2207 bool ret = true;
2208
2209 list_for_each(node, &adev->usecase_list) {
2210 usecase = node_to_item(node, struct audio_usecase, list);
2211 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
2212 /*
2213 * If voice call is already existing, do not proceed further to avoid
2214 * disabling/enabling both RX and TX devices, CSD calls, etc.
2215 * Once the voice call done, the HDMI channels can be configured to
2216 * max channels of remaining use cases.
2217 */
2218 if (usecase->id == USECASE_VOICE_CALL) {
2219 ALOGV("%s: voice call is active, no change in HDMI channels",
2220 __func__);
2221 ret = false;
2222 break;
2223 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_HIFI) {
2224 ALOGV("%s: hifi playback is active, "
2225 "no change in HDMI channels", __func__);
2226 ret = false;
2227 break;
2228 }
2229 }
2230 }
2231 return ret;
2232 }
2233
check_and_set_hdmi_channels(struct audio_device * adev,unsigned int channels)2234 static int check_and_set_hdmi_channels(struct audio_device *adev,
2235 unsigned int channels)
2236 {
2237 struct listnode *node;
2238 struct audio_usecase *usecase;
2239
2240 /* Check if change in HDMI channel config is allowed */
2241 if (!allow_hdmi_channel_config(adev))
2242 return 0;
2243
2244 if (channels == adev->cur_hdmi_channels) {
2245 ALOGV("%s: Requested channels are same as current", __func__);
2246 return 0;
2247 }
2248
2249 platform_set_hdmi_channels(adev->platform, channels);
2250 adev->cur_hdmi_channels = channels;
2251
2252 /*
2253 * Deroute all the playback streams routed to HDMI so that
2254 * the back end is deactivated. Note that backend will not
2255 * be deactivated if any one stream is connected to it.
2256 */
2257 list_for_each(node, &adev->usecase_list) {
2258 usecase = node_to_item(node, struct audio_usecase, list);
2259 if (usecase->type == PCM_PLAYBACK &&
2260 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
2261 disable_audio_route(adev, usecase);
2262 }
2263 }
2264
2265 /*
2266 * Enable all the streams disabled above. Now the HDMI backend
2267 * will be activated with new channel configuration
2268 */
2269 list_for_each(node, &adev->usecase_list) {
2270 usecase = node_to_item(node, struct audio_usecase, list);
2271 if (usecase->type == PCM_PLAYBACK &&
2272 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
2273 enable_audio_route(adev, usecase);
2274 }
2275 }
2276
2277 return 0;
2278 }
2279
check_and_set_usb_service_interval(struct audio_device * adev,struct audio_usecase * uc_info,bool min)2280 static int check_and_set_usb_service_interval(struct audio_device *adev,
2281 struct audio_usecase *uc_info,
2282 bool min)
2283 {
2284 struct listnode *node;
2285 struct audio_usecase *usecase;
2286 bool switch_usecases = false;
2287 bool reconfig = false;
2288
2289 if ((uc_info->id != USECASE_AUDIO_PLAYBACK_MMAP) &&
2290 (uc_info->id != USECASE_AUDIO_PLAYBACK_ULL))
2291 return -1;
2292
2293 /* set if the valid usecase do not already exist */
2294 list_for_each(node, &adev->usecase_list) {
2295 usecase = node_to_item(node, struct audio_usecase, list);
2296 if (usecase->type == PCM_PLAYBACK &&
2297 (audio_is_usb_out_device(usecase->devices & AUDIO_DEVICE_OUT_ALL_USB))) {
2298 switch (usecase->id) {
2299 case USECASE_AUDIO_PLAYBACK_MMAP:
2300 case USECASE_AUDIO_PLAYBACK_ULL:
2301 // cannot reconfig while mmap/ull is present.
2302 return -1;
2303 default:
2304 switch_usecases = true;
2305 break;
2306 }
2307 }
2308 if (switch_usecases)
2309 break;
2310 }
2311 /*
2312 * client can try to set service interval in start_output_stream
2313 * to min or to 0 (i.e reset) in stop_output_stream .
2314 */
2315 unsigned long service_interval =
2316 audio_extn_usb_find_service_interval(min, true /*playback*/);
2317 int ret = platform_set_usb_service_interval(adev->platform,
2318 true /*playback*/,
2319 service_interval,
2320 &reconfig);
2321 /* no change or not supported or no active usecases */
2322 if (ret || !reconfig || !switch_usecases)
2323 return -1;
2324 return 0;
2325 #undef VALID_USECASE
2326 }
2327
stop_output_stream(struct stream_out * out)2328 static int stop_output_stream(struct stream_out *out)
2329 {
2330 int i, ret = 0;
2331 struct audio_usecase *uc_info;
2332 struct audio_device *adev = out->dev;
2333
2334 ALOGV("%s: enter: usecase(%d: %s)", __func__,
2335 out->usecase, use_case_table[out->usecase]);
2336 uc_info = get_usecase_from_list(adev, out->usecase);
2337 if (uc_info == NULL) {
2338 ALOGE("%s: Could not find the usecase (%d) in the list",
2339 __func__, out->usecase);
2340 return -EINVAL;
2341 }
2342
2343 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2344 if (adev->visualizer_stop_output != NULL)
2345 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
2346 if (adev->offload_effects_stop_output != NULL)
2347 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
2348 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
2349 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
2350 audio_low_latency_hint_end();
2351 }
2352
2353 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
2354 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
2355 voice_set_device_mute_flag(adev, false);
2356 }
2357
2358 /* 1. Get and set stream specific mixer controls */
2359 disable_audio_route(adev, uc_info);
2360
2361 /* 2. Disable the rx device */
2362 disable_snd_device(adev, uc_info->out_snd_device);
2363
2364 list_remove(&uc_info->list);
2365
2366 audio_extn_extspk_update(adev->extspk);
2367
2368 /* Must be called after removing the usecase from list */
2369 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2370 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
2371 else if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
2372 ret = check_and_set_usb_service_interval(adev, uc_info, false /*min*/);
2373 if (ret == 0) {
2374 /* default service interval was successfully updated,
2375 reopen USB backend with new service interval */
2376 check_and_route_playback_usecases(adev, uc_info, uc_info->out_snd_device);
2377 }
2378 ret = 0;
2379 }
2380 /* 1) media + voip output routing to handset must route media back to
2381 speaker when voip stops.
2382 2) trigger voip input to reroute when voip output changes to
2383 hearing aid. */
2384 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP ||
2385 out->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
2386 struct listnode *node;
2387 struct audio_usecase *usecase;
2388 list_for_each(node, &adev->usecase_list) {
2389 usecase = node_to_item(node, struct audio_usecase, list);
2390 if ((usecase->type == PCM_CAPTURE &&
2391 usecase->id != USECASE_AUDIO_RECORD_VOIP)
2392 || usecase == uc_info)
2393 continue;
2394
2395 ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)",
2396 __func__, usecase->id, use_case_table[usecase->id],
2397 out->usecase, use_case_table[out->usecase]);
2398 select_devices(adev, usecase->id);
2399 }
2400 }
2401
2402 free(uc_info);
2403 ALOGV("%s: exit: status(%d)", __func__, ret);
2404 return ret;
2405 }
2406
pcm_open_prepare_helper(unsigned int snd_card,unsigned int pcm_device_id,unsigned int flags,unsigned int pcm_open_retry_count,struct pcm_config * config)2407 struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id,
2408 unsigned int flags, unsigned int pcm_open_retry_count,
2409 struct pcm_config *config)
2410 {
2411 struct pcm* pcm = NULL;
2412
2413 while (1) {
2414 pcm = pcm_open(snd_card, pcm_device_id, flags, config);
2415 if (pcm == NULL || !pcm_is_ready(pcm)) {
2416 ALOGE("%s: %s", __func__, pcm_get_error(pcm));
2417 if (pcm != NULL) {
2418 pcm_close(pcm);
2419 pcm = NULL;
2420 }
2421 if (pcm_open_retry_count-- == 0)
2422 return NULL;
2423
2424 usleep(PROXY_OPEN_WAIT_TIME * 1000);
2425 continue;
2426 }
2427 break;
2428 }
2429
2430 if (pcm_is_ready(pcm)) {
2431 int ret = pcm_prepare(pcm);
2432 if (ret < 0) {
2433 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
2434 pcm_close(pcm);
2435 pcm = NULL;
2436 }
2437 }
2438
2439 return pcm;
2440 }
2441
start_output_stream(struct stream_out * out)2442 int start_output_stream(struct stream_out *out)
2443 {
2444 int ret = 0;
2445 struct audio_usecase *uc_info;
2446 struct audio_device *adev = out->dev;
2447 bool a2dp_combo = false;
2448
2449 ALOGV("%s: enter: usecase(%d: %s) %s devices(%#x)",
2450 __func__, out->usecase, use_case_table[out->usecase],
2451 out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS ? "(with haptics)" : "",
2452 out->devices);
2453
2454 if (out->card_status == CARD_STATUS_OFFLINE ||
2455 adev->card_status == CARD_STATUS_OFFLINE) {
2456 ALOGW("out->card_status or adev->card_status offline, try again");
2457 ret = -EAGAIN;
2458 goto error_config;
2459 }
2460
2461 //Update incall music usecase to reflect correct voice session
2462 if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2463 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
2464 if (ret != 0) {
2465 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2466 __func__, ret);
2467 goto error_config;
2468 }
2469 }
2470
2471 if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
2472 if (!audio_extn_a2dp_is_ready()) {
2473 if (out->devices & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
2474 a2dp_combo = true;
2475 } else {
2476 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
2477 ALOGE("%s: A2DP profile is not ready, return error", __func__);
2478 ret = -EAGAIN;
2479 goto error_config;
2480 }
2481 }
2482 }
2483 }
2484 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
2485 if (out->pcm_device_id < 0) {
2486 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
2487 __func__, out->pcm_device_id, out->usecase);
2488 ret = -EINVAL;
2489 goto error_config;
2490 }
2491
2492 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
2493 uc_info->id = out->usecase;
2494 uc_info->type = PCM_PLAYBACK;
2495 uc_info->stream.out = out;
2496 uc_info->devices = out->devices;
2497 uc_info->in_snd_device = SND_DEVICE_NONE;
2498 uc_info->out_snd_device = SND_DEVICE_NONE;
2499
2500 /* This must be called before adding this usecase to the list */
2501 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2502 check_and_set_hdmi_channels(adev, out->config.channels);
2503 else if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
2504 check_and_set_usb_service_interval(adev, uc_info, true /*min*/);
2505 /* USB backend is not reopened immediately.
2506 This is eventually done as part of select_devices */
2507 }
2508
2509 list_add_tail(&adev->usecase_list, &uc_info->list);
2510
2511 audio_streaming_hint_start();
2512 audio_extn_perf_lock_acquire();
2513
2514 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
2515 (!audio_extn_a2dp_is_ready())) {
2516 if (!a2dp_combo) {
2517 check_a2dp_restore_l(adev, out, false);
2518 } else {
2519 audio_devices_t dev = out->devices;
2520 if (dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE)
2521 out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
2522 else
2523 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
2524 select_devices(adev, out->usecase);
2525 out->devices = dev;
2526 }
2527 } else {
2528 select_devices(adev, out->usecase);
2529 }
2530
2531 audio_extn_extspk_update(adev->extspk);
2532
2533 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
2534 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
2535 voice_set_device_mute_flag(adev, true);
2536 }
2537
2538 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
2539 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
2540 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2541 out->pcm = NULL;
2542 out->compr = compress_open(adev->snd_card, out->pcm_device_id,
2543 COMPRESS_IN, &out->compr_config);
2544 if (out->compr && !is_compress_ready(out->compr)) {
2545 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
2546 compress_close(out->compr);
2547 out->compr = NULL;
2548 ret = -EIO;
2549 goto error_open;
2550 }
2551 if (out->offload_callback)
2552 compress_nonblock(out->compr, out->non_blocking);
2553
2554 if (adev->visualizer_start_output != NULL) {
2555 int capture_device_id =
2556 platform_get_pcm_device_id(USECASE_AUDIO_RECORD_AFE_PROXY,
2557 PCM_CAPTURE);
2558 adev->visualizer_start_output(out->handle, out->pcm_device_id,
2559 adev->snd_card, capture_device_id);
2560 }
2561 if (adev->offload_effects_start_output != NULL)
2562 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
2563 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
2564 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
2565 ALOGE("%s: pcm stream not ready", __func__);
2566 goto error_open;
2567 }
2568 ret = pcm_start(out->pcm);
2569 if (ret < 0) {
2570 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
2571 goto error_open;
2572 }
2573 } else {
2574 unsigned int flags = PCM_OUT | PCM_MONOTONIC;
2575 unsigned int pcm_open_retry_count = 0;
2576
2577 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
2578 flags |= PCM_MMAP | PCM_NOIRQ;
2579 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
2580 } else if (out->realtime) {
2581 flags |= PCM_MMAP | PCM_NOIRQ;
2582 }
2583
2584 out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id,
2585 flags, pcm_open_retry_count,
2586 &(out->config));
2587 if (out->pcm == NULL) {
2588 ret = -EIO;
2589 goto error_open;
2590 }
2591
2592 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
2593 if (adev->haptic_pcm != NULL) {
2594 pcm_close(adev->haptic_pcm);
2595 adev->haptic_pcm = NULL;
2596 }
2597 adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card,
2598 adev->haptic_pcm_device_id,
2599 flags, pcm_open_retry_count,
2600 &(adev->haptics_config));
2601 // failure to open haptics pcm shouldnt stop audio,
2602 // so do not close audio pcm in case of error
2603 }
2604
2605 if (out->realtime) {
2606 ret = pcm_start(out->pcm);
2607 if (ret < 0) {
2608 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
2609 pcm_close(out->pcm);
2610 out->pcm = NULL;
2611 goto error_open;
2612 }
2613 }
2614 if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY
2615 || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
2616 || out->usecase == USECASE_AUDIO_PLAYBACK_ULL)) {
2617 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
2618 }
2619 }
2620
2621 register_out_stream(out);
2622 audio_streaming_hint_end();
2623 audio_extn_perf_lock_release();
2624 audio_extn_tfa_98xx_enable_speaker();
2625
2626 if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
2627 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
2628 audio_low_latency_hint_start();
2629 }
2630
2631 // consider a scenario where on pause lower layers are tear down.
2632 // so on resume, swap mixer control need to be sent only when
2633 // backend is active, hence rather than sending from enable device
2634 // sending it from start of stream
2635
2636 platform_set_swap_channels(adev, true);
2637
2638 ALOGV("%s: exit", __func__);
2639 return 0;
2640 error_open:
2641 if (adev->haptic_pcm) {
2642 pcm_close(adev->haptic_pcm);
2643 adev->haptic_pcm = NULL;
2644 }
2645 audio_streaming_hint_end();
2646 audio_extn_perf_lock_release();
2647 stop_output_stream(out);
2648 error_config:
2649 return ret;
2650 }
2651
check_input_parameters(uint32_t sample_rate,audio_format_t format,int channel_count,bool is_usb_hifi)2652 static int check_input_parameters(uint32_t sample_rate,
2653 audio_format_t format,
2654 int channel_count, bool is_usb_hifi)
2655 {
2656 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
2657 (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
2658 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) &&
2659 !(is_usb_hifi && (format == AUDIO_FORMAT_PCM_32_BIT))) {
2660 ALOGE("%s: unsupported AUDIO FORMAT (%d) ", __func__, format);
2661 return -EINVAL;
2662 }
2663
2664 int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT;
2665 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) {
2666 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
2667 channel_count, MIN_CHANNEL_COUNT, max_channel_count);
2668 return -EINVAL;
2669 }
2670
2671 switch (sample_rate) {
2672 case 8000:
2673 case 11025:
2674 case 12000:
2675 case 16000:
2676 case 22050:
2677 case 24000:
2678 case 32000:
2679 case 44100:
2680 case 48000:
2681 case 96000:
2682 break;
2683 default:
2684 ALOGE("%s: unsupported (%d) samplerate passed ", __func__, sample_rate);
2685 return -EINVAL;
2686 }
2687
2688 return 0;
2689 }
2690
2691 /** Add a value in a list if not already present.
2692 * @return true if value was successfully inserted or already present,
2693 * false if the list is full and does not contain the value.
2694 */
register_uint(uint32_t value,uint32_t * list,size_t list_length)2695 static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
2696 for (size_t i = 0; i < list_length; i++) {
2697 if (list[i] == value) return true; // value is already present
2698 if (list[i] == 0) { // no values in this slot
2699 list[i] = value;
2700 return true; // value inserted
2701 }
2702 }
2703 return false; // could not insert value
2704 }
2705
2706 /** Add channel_mask in supported_channel_masks if not already present.
2707 * @return true if channel_mask was successfully inserted or already present,
2708 * false if supported_channel_masks is full and does not contain channel_mask.
2709 */
register_channel_mask(audio_channel_mask_t channel_mask,audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS])2710 static void register_channel_mask(audio_channel_mask_t channel_mask,
2711 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
2712 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
2713 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
2714 }
2715
2716 /** Add format in supported_formats if not already present.
2717 * @return true if format was successfully inserted or already present,
2718 * false if supported_formats is full and does not contain format.
2719 */
register_format(audio_format_t format,audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS])2720 static void register_format(audio_format_t format,
2721 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
2722 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
2723 "%s: stream can not declare supporting its format %x", __func__, format);
2724 }
2725 /** Add sample_rate in supported_sample_rates if not already present.
2726 * @return true if sample_rate was successfully inserted or already present,
2727 * false if supported_sample_rates is full and does not contain sample_rate.
2728 */
register_sample_rate(uint32_t sample_rate,uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES])2729 static void register_sample_rate(uint32_t sample_rate,
2730 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
2731 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
2732 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
2733 }
2734
get_stream_buffer_size(size_t duration_ms,uint32_t sample_rate,audio_format_t format,int channel_count,bool is_low_latency)2735 static size_t get_stream_buffer_size(size_t duration_ms,
2736 uint32_t sample_rate,
2737 audio_format_t format,
2738 int channel_count,
2739 bool is_low_latency)
2740 {
2741 // Compute target frames based on time or period size.
2742 size_t target_frames = is_low_latency
2743 ? configured_low_latency_capture_period_size // record only
2744 : (sample_rate * duration_ms) / 1000;
2745
2746 // Round up to a multiple of 16 frames in case sizing for the MixerThread.
2747 if (!is_low_latency) { // low latency flag set for record only
2748 target_frames = (target_frames + 0xf) & ~0xf;
2749 }
2750
2751 // Buffer size is the target frames multiplied by the frame size in bytes.
2752 const size_t frame_size = channel_count * audio_bytes_per_sample(format);
2753 const size_t buffer_size = target_frames * frame_size;
2754
2755 return buffer_size;
2756 }
2757
out_get_sample_rate(const struct audio_stream * stream)2758 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
2759 {
2760 struct stream_out *out = (struct stream_out *)stream;
2761
2762 return out->sample_rate;
2763 }
2764
out_set_sample_rate(struct audio_stream * stream __unused,uint32_t rate __unused)2765 static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
2766 {
2767 return -ENOSYS;
2768 }
2769
out_get_buffer_size(const struct audio_stream * stream)2770 static size_t out_get_buffer_size(const struct audio_stream *stream)
2771 {
2772 struct stream_out *out = (struct stream_out *)stream;
2773
2774 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2775 return out->compr_config.fragment_size;
2776 }
2777 return out->config.period_size * out->af_period_multiplier *
2778 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
2779 }
2780
out_get_channels(const struct audio_stream * stream)2781 static uint32_t out_get_channels(const struct audio_stream *stream)
2782 {
2783 struct stream_out *out = (struct stream_out *)stream;
2784
2785 return out->channel_mask;
2786 }
2787
out_get_format(const struct audio_stream * stream)2788 static audio_format_t out_get_format(const struct audio_stream *stream)
2789 {
2790 struct stream_out *out = (struct stream_out *)stream;
2791
2792 return out->format;
2793 }
2794
out_set_format(struct audio_stream * stream __unused,audio_format_t format __unused)2795 static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
2796 {
2797 return -ENOSYS;
2798 }
2799
2800 /* must be called with out->lock locked */
out_standby_l(struct audio_stream * stream)2801 static int out_standby_l(struct audio_stream *stream)
2802 {
2803 struct stream_out *out = (struct stream_out *)stream;
2804 struct audio_device *adev = out->dev;
2805 bool do_stop = true;
2806
2807 if (!out->standby) {
2808 if (adev->adm_deregister_stream)
2809 adev->adm_deregister_stream(adev->adm_data, out->handle);
2810 pthread_mutex_lock(&adev->lock);
2811 out->standby = true;
2812 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2813 if (out->pcm) {
2814 pcm_close(out->pcm);
2815 out->pcm = NULL;
2816
2817 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
2818 if (adev->haptic_pcm) {
2819 pcm_close(adev->haptic_pcm);
2820 adev->haptic_pcm = NULL;
2821 }
2822
2823 if (adev->haptic_buffer != NULL) {
2824 free(adev->haptic_buffer);
2825 adev->haptic_buffer = NULL;
2826 adev->haptic_buffer_size = 0;
2827 }
2828 }
2829 }
2830 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
2831 do_stop = out->playback_started;
2832 out->playback_started = false;
2833
2834 if (out->mmap_shared_memory_fd >= 0) {
2835 ALOGV("%s: closing mmap_shared_memory_fd = %d",
2836 __func__, out->mmap_shared_memory_fd);
2837 close(out->mmap_shared_memory_fd);
2838 out->mmap_shared_memory_fd = -1;
2839 }
2840
2841 }
2842 } else {
2843 stop_compressed_output_l(out);
2844 out->gapless_mdata.encoder_delay = 0;
2845 out->gapless_mdata.encoder_padding = 0;
2846 if (out->compr != NULL) {
2847 compress_close(out->compr);
2848 out->compr = NULL;
2849 }
2850 }
2851 if (do_stop) {
2852 stop_output_stream(out);
2853 }
2854 pthread_mutex_unlock(&adev->lock);
2855 }
2856 return 0;
2857 }
2858
out_standby(struct audio_stream * stream)2859 static int out_standby(struct audio_stream *stream)
2860 {
2861 struct stream_out *out = (struct stream_out *)stream;
2862
2863 ALOGV("%s: enter: usecase(%d: %s)", __func__,
2864 out->usecase, use_case_table[out->usecase]);
2865
2866 lock_output_stream(out);
2867 out_standby_l(stream);
2868 pthread_mutex_unlock(&out->lock);
2869 ALOGV("%s: exit", __func__);
2870 return 0;
2871 }
2872
out_on_error(struct audio_stream * stream)2873 static int out_on_error(struct audio_stream *stream)
2874 {
2875 struct stream_out *out = (struct stream_out *)stream;
2876 struct audio_device *adev = out->dev;
2877 bool do_standby = false;
2878
2879 lock_output_stream(out);
2880 if (!out->standby) {
2881 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2882 stop_compressed_output_l(out);
2883 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
2884 } else
2885 do_standby = true;
2886 }
2887 pthread_mutex_unlock(&out->lock);
2888
2889 if (do_standby)
2890 return out_standby(&out->stream.common);
2891
2892 return 0;
2893 }
2894
out_dump(const struct audio_stream * stream,int fd)2895 static int out_dump(const struct audio_stream *stream, int fd)
2896 {
2897 struct stream_out *out = (struct stream_out *)stream;
2898
2899 // We try to get the lock for consistency,
2900 // but it isn't necessary for these variables.
2901 // If we're not in standby, we may be blocked on a write.
2902 const bool locked = (pthread_mutex_trylock(&out->lock) == 0);
2903 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no");
2904 dprintf(fd, " Frames written: %lld\n", (long long)out->written);
2905
2906 char buffer[256]; // for statistics formatting
2907 simple_stats_to_string(&out->fifo_underruns, buffer, sizeof(buffer));
2908 dprintf(fd, " Fifo frame underruns: %s\n", buffer);
2909
2910 if (out->start_latency_ms.n > 0) {
2911 simple_stats_to_string(&out->start_latency_ms, buffer, sizeof(buffer));
2912 dprintf(fd, " Start latency ms: %s\n", buffer);
2913 }
2914
2915 if (locked) {
2916 pthread_mutex_unlock(&out->lock);
2917 }
2918
2919 // dump error info
2920 (void)error_log_dump(
2921 out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
2922
2923 return 0;
2924 }
2925
parse_compress_metadata(struct stream_out * out,struct str_parms * parms)2926 static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
2927 {
2928 int ret = 0;
2929 char value[32];
2930 struct compr_gapless_mdata tmp_mdata;
2931
2932 if (!out || !parms) {
2933 return -EINVAL;
2934 }
2935
2936 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
2937 if (ret >= 0) {
2938 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
2939 } else {
2940 return -EINVAL;
2941 }
2942
2943 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
2944 if (ret >= 0) {
2945 tmp_mdata.encoder_padding = atoi(value);
2946 } else {
2947 return -EINVAL;
2948 }
2949
2950 out->gapless_mdata = tmp_mdata;
2951 out->send_new_metadata = 1;
2952 ALOGV("%s new encoder delay %u and padding %u", __func__,
2953 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
2954
2955 return 0;
2956 }
2957
output_drives_call(struct audio_device * adev,struct stream_out * out)2958 static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
2959 {
2960 return out == adev->primary_output || out == adev->voice_tx_output;
2961 }
2962
get_alive_usb_card(struct str_parms * parms)2963 static int get_alive_usb_card(struct str_parms* parms) {
2964 int card;
2965 if ((str_parms_get_int(parms, "card", &card) >= 0) &&
2966 !audio_extn_usb_alive(card)) {
2967 return card;
2968 }
2969 return -ENODEV;
2970 }
2971
out_set_parameters(struct audio_stream * stream,const char * kvpairs)2972 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
2973 {
2974 struct stream_out *out = (struct stream_out *)stream;
2975 struct audio_device *adev = out->dev;
2976 struct audio_usecase *usecase;
2977 struct listnode *node;
2978 struct str_parms *parms;
2979 char value[32];
2980 int ret, val = 0;
2981 bool select_new_device = false;
2982 int status = 0;
2983 bool bypass_a2dp = false;
2984 bool forced_speaker_fallback = false;
2985
2986 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
2987 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
2988 parms = str_parms_create_str(kvpairs);
2989 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2990 if (ret >= 0) {
2991 val = atoi(value);
2992
2993 lock_output_stream(out);
2994
2995 if (val == AUDIO_DEVICE_NONE &&
2996 audio_is_usb_out_device(out->devices)) {
2997 val = AUDIO_DEVICE_OUT_SPEAKER;
2998 forced_speaker_fallback = true;
2999 }
3000
3001 pthread_mutex_lock(&adev->lock);
3002
3003 /*
3004 * When HDMI cable is unplugged the music playback is paused and
3005 * the policy manager sends routing=0. But the audioflinger
3006 * continues to write data until standby time (3sec).
3007 * As the HDMI core is turned off, the write gets blocked.
3008 * Avoid this by routing audio to speaker until standby.
3009 */
3010 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
3011 val == AUDIO_DEVICE_NONE) {
3012 val = AUDIO_DEVICE_OUT_SPEAKER;
3013 forced_speaker_fallback = true;
3014 }
3015
3016 /*
3017 * When A2DP is disconnected the
3018 * music playback is paused and the policy manager sends routing=0
3019 * But the audioflingercontinues to write data until standby time
3020 * (3sec). As BT is turned off, the write gets blocked.
3021 * Avoid this by routing audio to speaker until standby.
3022 */
3023 if ((out->devices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) &&
3024 (val == AUDIO_DEVICE_NONE) &&
3025 !audio_extn_a2dp_is_ready() &&
3026 !adev->bt_sco_on) {
3027 val = AUDIO_DEVICE_OUT_SPEAKER;
3028 forced_speaker_fallback = true;
3029 }
3030
3031 /* To avoid a2dp to sco overlapping / BT device improper state
3032 * check with BT lib about a2dp streaming support before routing
3033 */
3034 if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
3035 if (!audio_extn_a2dp_is_ready()) {
3036 if (val & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
3037 //combo usecase just by pass a2dp
3038 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
3039 bypass_a2dp = true;
3040 } else {
3041 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
3042 /* update device to a2dp and don't route as BT returned error
3043 * However it is still possible a2dp routing called because
3044 * of current active device disconnection (like wired headset)
3045 */
3046 out->devices = val;
3047 pthread_mutex_unlock(&out->lock);
3048 pthread_mutex_unlock(&adev->lock);
3049 status = -ENOSYS;
3050 goto routing_fail;
3051 }
3052 }
3053 }
3054
3055 audio_devices_t new_dev = val;
3056
3057 // Workaround: If routing to an non existing usb device, fail gracefully
3058 // The routing request will otherwise block during 10 second
3059 int card;
3060 if (audio_is_usb_out_device(new_dev) &&
3061 (card = get_alive_usb_card(parms)) >= 0) {
3062
3063 ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d", card);
3064 pthread_mutex_unlock(&adev->lock);
3065 pthread_mutex_unlock(&out->lock);
3066 status = -ENOSYS;
3067 goto routing_fail;
3068 }
3069
3070 /*
3071 * select_devices() call below switches all the usecases on the same
3072 * backend to the new device. Refer to check_and_route_playback_usecases() in
3073 * the select_devices(). But how do we undo this?
3074 *
3075 * For example, music playback is active on headset (deep-buffer usecase)
3076 * and if we go to ringtones and select a ringtone, low-latency usecase
3077 * will be started on headset+speaker. As we can't enable headset+speaker
3078 * and headset devices at the same time, select_devices() switches the music
3079 * playback to headset+speaker while starting low-lateny usecase for ringtone.
3080 * So when the ringtone playback is completed, how do we undo the same?
3081 *
3082 * We are relying on the out_set_parameters() call on deep-buffer output,
3083 * once the ringtone playback is ended.
3084 * NOTE: We should not check if the current devices are same as new devices.
3085 * Because select_devices() must be called to switch back the music
3086 * playback to headset.
3087 */
3088 if (new_dev != AUDIO_DEVICE_NONE) {
3089 bool same_dev = out->devices == new_dev;
3090 out->devices = new_dev;
3091
3092 if (output_drives_call(adev, out)) {
3093 if (!voice_is_call_state_active(adev)) {
3094 if (adev->mode == AUDIO_MODE_IN_CALL) {
3095 adev->current_call_output = out;
3096 ret = voice_start_call(adev);
3097 }
3098 } else {
3099 adev->current_call_output = out;
3100 voice_update_devices_for_all_voice_usecases(adev);
3101 }
3102 }
3103
3104 if (!out->standby) {
3105 int volume_delay_us = 0;
3106 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3107 pthread_mutex_lock(&out->compr_mute_lock);
3108 if (out->a2dp_compress_mute &&
3109 (!(new_dev & AUDIO_DEVICE_OUT_ALL_A2DP) ||
3110 audio_extn_a2dp_is_ready())) {
3111 out->a2dp_compress_mute = false;
3112 }
3113 float volume_l = out->volume_l;
3114 float volume_r = out->volume_r;
3115 if (out->a2dp_compress_mute || forced_speaker_fallback) {
3116 volume_l = 0.0;
3117 volume_r = 0.0;
3118 }
3119 if (volume_l != out->applied_volume_l || volume_r != out->applied_volume_r)
3120 volume_delay_us = COMPRESS_OFFLOAD_PLAYBACK_LATENCY * 2000;
3121
3122 out_set_compr_volume(&out->stream, volume_l, volume_r);
3123 pthread_mutex_unlock(&out->compr_mute_lock);
3124 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY ||
3125 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
3126 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) {
3127 float volume_l = out->volume_l;
3128 float volume_r = out->volume_r;
3129 if (forced_speaker_fallback) {
3130 volume_l = 0.0;
3131 volume_r = 0.0;
3132 }
3133 if (volume_l != out->applied_volume_l || volume_r != out->applied_volume_r)
3134 volume_delay_us = (int)platform_render_latency(out) * 2;
3135
3136 out_set_pcm_volume(&out->stream, volume_l, volume_r);
3137 }
3138 if (volume_delay_us > 0)
3139 usleep(volume_delay_us * 2);
3140
3141 if (!same_dev) {
3142 ALOGV("update routing change");
3143 // inform adm before actual routing to prevent glitches.
3144 if (adev->adm_on_routing_change) {
3145 adev->adm_on_routing_change(adev->adm_data,
3146 out->handle);
3147 }
3148 }
3149 if (!bypass_a2dp) {
3150 select_devices(adev, out->usecase);
3151 } else {
3152 if (new_dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE)
3153 out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3154 else
3155 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
3156 select_devices(adev, out->usecase);
3157 out->devices = new_dev;
3158 }
3159 audio_extn_tfa_98xx_update();
3160
3161 // on device switch force swap, lower functions will make sure
3162 // to check if swap is allowed or not.
3163
3164 if (!same_dev)
3165 platform_set_swap_channels(adev, true);
3166
3167
3168 }
3169
3170 }
3171
3172 pthread_mutex_unlock(&adev->lock);
3173 pthread_mutex_unlock(&out->lock);
3174
3175 /*handles device and call state changes*/
3176 audio_extn_extspk_update(adev->extspk);
3177 }
3178 routing_fail:
3179
3180 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3181 parse_compress_metadata(out, parms);
3182 }
3183
3184 str_parms_destroy(parms);
3185 ALOGV("%s: exit: code(%d)", __func__, status);
3186 return status;
3187 }
3188
stream_get_parameter_channels(struct str_parms * query,struct str_parms * reply,audio_channel_mask_t * supported_channel_masks)3189 static bool stream_get_parameter_channels(struct str_parms *query,
3190 struct str_parms *reply,
3191 audio_channel_mask_t *supported_channel_masks) {
3192 int ret = -1;
3193 char value[ARRAY_SIZE(channels_name_to_enum_table) * 32 /* max channel name size */];
3194 bool first = true;
3195 size_t i, j;
3196
3197 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
3198 ret = 0;
3199 value[0] = '\0';
3200 i = 0;
3201 while (supported_channel_masks[i] != 0) {
3202 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
3203 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
3204 if (!first) {
3205 strcat(value, "|");
3206 }
3207 strcat(value, channels_name_to_enum_table[j].name);
3208 first = false;
3209 break;
3210 }
3211 }
3212 i++;
3213 }
3214 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
3215 }
3216 return ret >= 0;
3217 }
3218
stream_get_parameter_formats(struct str_parms * query,struct str_parms * reply,audio_format_t * supported_formats)3219 static bool stream_get_parameter_formats(struct str_parms *query,
3220 struct str_parms *reply,
3221 audio_format_t *supported_formats) {
3222 int ret = -1;
3223 char value[256];
3224 int i;
3225
3226 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
3227 ret = 0;
3228 value[0] = '\0';
3229 switch (supported_formats[0]) {
3230 case AUDIO_FORMAT_PCM_16_BIT:
3231 strcat(value, "AUDIO_FORMAT_PCM_16_BIT");
3232 break;
3233 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
3234 strcat(value, "AUDIO_FORMAT_PCM_24_BIT_PACKED");
3235 break;
3236 case AUDIO_FORMAT_PCM_32_BIT:
3237 strcat(value, "AUDIO_FORMAT_PCM_32_BIT");
3238 break;
3239 default:
3240 ALOGE("%s: unsupported format %#x", __func__,
3241 supported_formats[0]);
3242 break;
3243 }
3244 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
3245 }
3246 return ret >= 0;
3247 }
3248
stream_get_parameter_rates(struct str_parms * query,struct str_parms * reply,uint32_t * supported_sample_rates)3249 static bool stream_get_parameter_rates(struct str_parms *query,
3250 struct str_parms *reply,
3251 uint32_t *supported_sample_rates) {
3252
3253 int i;
3254 char value[256];
3255 int ret = -1;
3256 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
3257 ret = 0;
3258 value[0] = '\0';
3259 i=0;
3260 int cursor = 0;
3261 while (supported_sample_rates[i]) {
3262 int avail = sizeof(value) - cursor;
3263 ret = snprintf(value + cursor, avail, "%s%d",
3264 cursor > 0 ? "|" : "",
3265 supported_sample_rates[i]);
3266 if (ret < 0 || ret >= avail) {
3267 // if cursor is at the last element of the array
3268 // overwrite with \0 is duplicate work as
3269 // snprintf already put a \0 in place.
3270 // else
3271 // we had space to write the '|' at value[cursor]
3272 // (which will be overwritten) or no space to fill
3273 // the first element (=> cursor == 0)
3274 value[cursor] = '\0';
3275 break;
3276 }
3277 cursor += ret;
3278 ++i;
3279 }
3280 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
3281 value);
3282 }
3283 return ret >= 0;
3284 }
3285
out_get_parameters(const struct audio_stream * stream,const char * keys)3286 static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
3287 {
3288 struct stream_out *out = (struct stream_out *)stream;
3289 struct str_parms *query = str_parms_create_str(keys);
3290 char *str;
3291 struct str_parms *reply = str_parms_create();
3292 bool replied = false;
3293 ALOGV("%s: enter: keys - %s", __func__, keys);
3294
3295 replied |= stream_get_parameter_channels(query, reply,
3296 &out->supported_channel_masks[0]);
3297 replied |= stream_get_parameter_formats(query, reply,
3298 &out->supported_formats[0]);
3299 replied |= stream_get_parameter_rates(query, reply,
3300 &out->supported_sample_rates[0]);
3301 if (replied) {
3302 str = str_parms_to_str(reply);
3303 } else {
3304 str = strdup("");
3305 }
3306 str_parms_destroy(query);
3307 str_parms_destroy(reply);
3308 ALOGV("%s: exit: returns - %s", __func__, str);
3309 return str;
3310 }
3311
out_get_latency(const struct audio_stream_out * stream)3312 static uint32_t out_get_latency(const struct audio_stream_out *stream)
3313 {
3314 uint32_t hw_delay, period_ms;
3315 struct stream_out *out = (struct stream_out *)stream;
3316 uint32_t latency;
3317
3318 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
3319 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
3320 else if ((out->realtime) ||
3321 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
3322 // since the buffer won't be filled up faster than realtime,
3323 // return a smaller number
3324 period_ms = (out->af_period_multiplier * out->config.period_size *
3325 1000) / (out->config.rate);
3326 hw_delay = platform_render_latency(out)/1000;
3327 return period_ms + hw_delay;
3328 }
3329
3330 latency = (out->config.period_count * out->config.period_size * 1000) /
3331 (out->config.rate);
3332
3333 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices)
3334 latency += audio_extn_a2dp_get_encoder_latency();
3335
3336 return latency;
3337 }
3338
out_set_compr_volume(struct audio_stream_out * stream,float left,float right)3339 static int out_set_compr_volume(struct audio_stream_out *stream, float left,
3340 float right)
3341 {
3342 struct stream_out *out = (struct stream_out *)stream;
3343 int volume[2];
3344 char mixer_ctl_name[128];
3345 struct audio_device *adev = out->dev;
3346 struct mixer_ctl *ctl;
3347 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
3348 PCM_PLAYBACK);
3349
3350 if (left == out->applied_volume_l && right == out->applied_volume_r)
3351 return 0;
3352
3353 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
3354 "Compress Playback %d Volume", pcm_device_id);
3355 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3356 if (!ctl) {
3357 ALOGE("%s: Could not get ctl for mixer cmd - %s",
3358 __func__, mixer_ctl_name);
3359 return -EINVAL;
3360 }
3361 ALOGV("%s: ctl for mixer cmd - %s, left %f, right %f",
3362 __func__, mixer_ctl_name, left, right);
3363 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
3364 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
3365 mixer_ctl_set_array(ctl, volume, sizeof(volume) / sizeof(volume[0]));
3366
3367 out->applied_volume_l = left;
3368 out->applied_volume_r = right;
3369 return 0;
3370 }
3371
out_set_pcm_volume(struct audio_stream_out * stream,float left,float right)3372 static int out_set_pcm_volume(struct audio_stream_out *stream, float left,
3373 float right)
3374 {
3375 struct stream_out *out = (struct stream_out *)stream;
3376
3377 if (left == out->applied_volume_l && right == out->applied_volume_r)
3378 return 0;
3379
3380 /* Volume control for pcm playback */
3381 if (left != right) {
3382 return -EINVAL;
3383 } else {
3384 char mixer_ctl_name[128];
3385 struct audio_device *adev = out->dev;
3386 struct mixer_ctl *ctl;
3387 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
3388 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id);
3389 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3390 if (!ctl) {
3391 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
3392 return -EINVAL;
3393 }
3394
3395 int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX);
3396 int ret = mixer_ctl_set_value(ctl, 0, volume);
3397 if (ret < 0) {
3398 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
3399 return -EINVAL;
3400 }
3401
3402 ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left);
3403
3404 out->applied_volume_l = left;
3405 out->applied_volume_r = right;
3406 return 0;
3407 }
3408 }
3409
out_set_volume(struct audio_stream_out * stream,float left,float right)3410 static int out_set_volume(struct audio_stream_out *stream, float left,
3411 float right)
3412 {
3413 struct stream_out *out = (struct stream_out *)stream;
3414 int ret = 0;
3415
3416 if (out->usecase == USECASE_AUDIO_PLAYBACK_HIFI) {
3417 /* only take left channel into account: the API is for stereo anyway */
3418 out->muted = (left == 0.0f);
3419 return 0;
3420 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3421 pthread_mutex_lock(&out->compr_mute_lock);
3422 ALOGV("%s: compress mute %d", __func__, out->a2dp_compress_mute);
3423 if (!out->a2dp_compress_mute)
3424 ret = out_set_compr_volume(stream, left, right);
3425 out->volume_l = left;
3426 out->volume_r = right;
3427 pthread_mutex_unlock(&out->compr_mute_lock);
3428 return ret;
3429 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
3430 out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX);
3431 out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX);
3432 if (!out->standby) {
3433 // if in standby, cached volume will be sent after stream is opened
3434 audio_extn_utils_send_app_type_gain(out->dev,
3435 out->app_type_cfg.app_type,
3436 &out->app_type_cfg.gain[0]);
3437 }
3438 return 0;
3439 }
3440
3441 return -ENOSYS;
3442 }
3443
3444 // note: this call is safe only if the stream_cb is
3445 // removed first in close_output_stream (as is done now).
out_snd_mon_cb(void * stream,struct str_parms * parms)3446 static void out_snd_mon_cb(void * stream, struct str_parms * parms)
3447 {
3448 if (!stream || !parms)
3449 return;
3450
3451 struct stream_out *out = (struct stream_out *)stream;
3452 struct audio_device *adev = out->dev;
3453
3454 card_status_t status;
3455 int card;
3456 if (parse_snd_card_status(parms, &card, &status) < 0)
3457 return;
3458
3459 pthread_mutex_lock(&adev->lock);
3460 bool valid_cb = (card == adev->snd_card);
3461 pthread_mutex_unlock(&adev->lock);
3462
3463 if (!valid_cb)
3464 return;
3465
3466 lock_output_stream(out);
3467 if (out->card_status != status)
3468 out->card_status = status;
3469 pthread_mutex_unlock(&out->lock);
3470
3471 ALOGW("out_snd_mon_cb for card %d usecase %s, status %s", card,
3472 use_case_table[out->usecase],
3473 status == CARD_STATUS_OFFLINE ? "offline" : "online");
3474
3475 if (status == CARD_STATUS_OFFLINE)
3476 out_on_error(stream);
3477
3478 return;
3479 }
3480
3481 #ifdef NO_AUDIO_OUT
out_write_for_no_output(struct audio_stream_out * stream,const void * buffer __unused,size_t bytes)3482 static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
3483 const void *buffer __unused, size_t bytes)
3484 {
3485 struct stream_out *out = (struct stream_out *)stream;
3486
3487 /* No Output device supported other than BT for playback.
3488 * Sleep for the amount of buffer duration
3489 */
3490 lock_output_stream(out);
3491 usleep(bytes * 1000000 / audio_stream_out_frame_size(
3492 (const struct audio_stream_out *)&out->stream) /
3493 out_get_sample_rate(&out->stream.common));
3494 pthread_mutex_unlock(&out->lock);
3495 return bytes;
3496 }
3497 #endif
3498
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)3499 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
3500 size_t bytes)
3501 {
3502 struct stream_out *out = (struct stream_out *)stream;
3503 struct audio_device *adev = out->dev;
3504 ssize_t ret = 0;
3505 int error_code = ERROR_CODE_STANDBY;
3506
3507 lock_output_stream(out);
3508 // this is always nonzero
3509 const size_t frame_size = audio_stream_out_frame_size(stream);
3510 const size_t frames = bytes / frame_size;
3511
3512 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3513 error_code = ERROR_CODE_WRITE;
3514 goto exit;
3515 }
3516
3517 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
3518 (audio_extn_a2dp_is_suspended())) {
3519 if (!(out->devices & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
3520 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
3521 ret = -EIO;
3522 goto exit;
3523 }
3524 }
3525 }
3526
3527 const bool was_in_standby = out->standby;
3528 if (out->standby) {
3529 out->standby = false;
3530 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
3531
3532 pthread_mutex_lock(&adev->lock);
3533 ret = start_output_stream(out);
3534
3535 /* ToDo: If use case is compress offload should return 0 */
3536 if (ret != 0) {
3537 out->standby = true;
3538 pthread_mutex_unlock(&adev->lock);
3539 goto exit;
3540 }
3541
3542 // after standby always force set last known cal step
3543 // dont change level anywhere except at the audio_hw_send_gain_dep_calibration
3544 ALOGD("%s: retry previous failed cal level set", __func__);
3545 send_gain_dep_calibration_l();
3546 pthread_mutex_unlock(&adev->lock);
3547
3548 // log startup time in ms.
3549 simple_stats_log(
3550 &out->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
3551 out->last_fifo_valid = false; // we're coming out of standby, last_fifo isn't valid.
3552 }
3553
3554 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3555 ALOGVV("%s: writing buffer (%zu bytes) to compress device", __func__, bytes);
3556 if (out->send_new_metadata) {
3557 ALOGVV("send new gapless metadata");
3558 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
3559 out->send_new_metadata = 0;
3560 }
3561 unsigned int avail;
3562 struct timespec tstamp;
3563 ret = compress_get_hpointer(out->compr, &avail, &tstamp);
3564 /* Do not limit write size if the available frames count is unknown */
3565 if (ret != 0) {
3566 avail = bytes;
3567 }
3568 if (avail == 0) {
3569 ret = 0;
3570 } else {
3571 // check for compressed format underrun, essentially an empty buffer check
3572 // for a lack of better measurement.
3573 if (!was_in_standby && avail == out->kernel_buffer_size) {
3574 ALOGW("%s: compressed buffer empty (underrun)", __func__);
3575 simple_stats_log(&out->fifo_underruns, 1.); // Note: log one frame for compressed.
3576 }
3577
3578 if (avail > bytes) {
3579 avail = bytes;
3580 }
3581 ret = compress_write(out->compr, buffer, avail);
3582 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %zd",
3583 __func__, avail, ret);
3584 }
3585
3586 if (ret >= 0 && ret < (ssize_t)bytes) {
3587 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
3588 }
3589 if (ret > 0 && !out->playback_started) {
3590 compress_start(out->compr);
3591 out->playback_started = 1;
3592 out->offload_state = OFFLOAD_STATE_PLAYING;
3593 }
3594 if (ret < 0) {
3595 error_log_log(out->error_log, ERROR_CODE_WRITE, audio_utils_get_real_time_ns());
3596 } else {
3597 out->written += ret; // accumulate bytes written for offload.
3598 }
3599 pthread_mutex_unlock(&out->lock);
3600 // TODO: consider logging offload pcm
3601 return ret;
3602 } else {
3603 error_code = ERROR_CODE_WRITE;
3604 if (out->pcm) {
3605 size_t bytes_to_write = bytes;
3606
3607 if (out->muted)
3608 memset((void *)buffer, 0, bytes);
3609 // FIXME: this can be removed once audio flinger mixer supports mono output
3610 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP ||
3611 out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
3612 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
3613 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
3614 int16_t *src = (int16_t *)buffer;
3615 int16_t *dst = (int16_t *)buffer;
3616
3617 LOG_ALWAYS_FATAL_IF(out->config.channels != 1 || channel_count != 2 ||
3618 out->format != AUDIO_FORMAT_PCM_16_BIT,
3619 "out_write called for VOIP use case with wrong properties");
3620
3621 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
3622 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
3623 }
3624 bytes_to_write /= 2;
3625 }
3626
3627 // Note: since out_get_presentation_position() is called alternating with out_write()
3628 // by AudioFlinger, we can check underruns using the prior timestamp read.
3629 // (Alternately we could check if the buffer is empty using pcm_get_htimestamp().
3630 if (out->last_fifo_valid) {
3631 // compute drain to see if there is an underrun.
3632 const int64_t current_ns = systemTime(SYSTEM_TIME_MONOTONIC); // sys call
3633 const int64_t frames_by_time =
3634 (current_ns - out->last_fifo_time_ns) * out->config.rate / NANOS_PER_SECOND;
3635 const int64_t underrun = frames_by_time - out->last_fifo_frames_remaining;
3636
3637 if (underrun > 0) {
3638 simple_stats_log(&out->fifo_underruns, underrun);
3639
3640 ALOGW("%s: underrun(%lld) "
3641 "frames_by_time(%lld) > out->last_fifo_frames_remaining(%lld)",
3642 __func__,
3643 (long long)out->fifo_underruns.n,
3644 (long long)frames_by_time,
3645 (long long)out->last_fifo_frames_remaining);
3646 }
3647 out->last_fifo_valid = false; // we're writing below, mark fifo info as stale.
3648 }
3649
3650 long ns = (frames * (int64_t) NANOS_PER_SECOND) / out->config.rate;
3651 request_out_focus(out, ns);
3652
3653 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
3654 if (use_mmap) {
3655 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
3656 } else {
3657 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
3658 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
3659 size_t bytes_per_sample = audio_bytes_per_sample(out->format);
3660 size_t frame_size = channel_count * bytes_per_sample;
3661 size_t frame_count = bytes_to_write / frame_size;
3662
3663 bool force_haptic_path =
3664 property_get_bool("vendor.audio.test_haptic", false);
3665
3666 // extract Haptics data from Audio buffer
3667 bool alloc_haptic_buffer = false;
3668 int haptic_channel_count = adev->haptics_config.channels;
3669 size_t haptic_frame_size = bytes_per_sample * haptic_channel_count;
3670 size_t audio_frame_size = frame_size - haptic_frame_size;
3671 size_t total_haptic_buffer_size = frame_count * haptic_frame_size;
3672
3673 if (adev->haptic_buffer == NULL) {
3674 alloc_haptic_buffer = true;
3675 } else if (adev->haptic_buffer_size < total_haptic_buffer_size) {
3676 free(adev->haptic_buffer);
3677 adev->haptic_buffer_size = 0;
3678 alloc_haptic_buffer = true;
3679 }
3680
3681 if (alloc_haptic_buffer) {
3682 adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size);
3683 adev->haptic_buffer_size = total_haptic_buffer_size;
3684 }
3685
3686 size_t src_index = 0, aud_index = 0, hap_index = 0;
3687 uint8_t *audio_buffer = (uint8_t *)buffer;
3688 uint8_t *haptic_buffer = adev->haptic_buffer;
3689
3690 // This is required for testing only. This works for stereo data only.
3691 // One channel is fed to audio stream and other to haptic stream for testing.
3692 if (force_haptic_path) {
3693 audio_frame_size = haptic_frame_size = bytes_per_sample;
3694 }
3695
3696 for (size_t i = 0; i < frame_count; i++) {
3697 for (size_t j = 0; j < audio_frame_size; j++)
3698 audio_buffer[aud_index++] = audio_buffer[src_index++];
3699
3700 for (size_t j = 0; j < haptic_frame_size; j++)
3701 haptic_buffer[hap_index++] = audio_buffer[src_index++];
3702 }
3703
3704 // This is required for testing only.
3705 // Discard haptic channel data.
3706 if (force_haptic_path) {
3707 src_index += haptic_frame_size;
3708 }
3709
3710 // write to audio pipeline
3711 ret = pcm_write(out->pcm,
3712 (void *)audio_buffer,
3713 frame_count * audio_frame_size);
3714
3715 // write to haptics pipeline
3716 if (adev->haptic_pcm)
3717 ret = pcm_write(adev->haptic_pcm,
3718 (void *)adev->haptic_buffer,
3719 frame_count * haptic_frame_size);
3720
3721 } else {
3722 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
3723 }
3724 }
3725 release_out_focus(out, ns);
3726 } else {
3727 LOG_ALWAYS_FATAL("out->pcm is NULL after starting output stream");
3728 }
3729 }
3730
3731 exit:
3732 // For PCM we always consume the buffer and return #bytes regardless of ret.
3733 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3734 out->written += frames;
3735 }
3736 long long sleeptime_us = 0;
3737
3738 if (ret != 0) {
3739 error_log_log(out->error_log, error_code, audio_utils_get_real_time_ns());
3740 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3741 ALOGE_IF(out->pcm != NULL,
3742 "%s: error %zd - %s", __func__, ret, pcm_get_error(out->pcm));
3743 sleeptime_us = frames * 1000000LL / out_get_sample_rate(&out->stream.common);
3744 // usleep not guaranteed for values over 1 second but we don't limit here.
3745 }
3746 }
3747
3748 pthread_mutex_unlock(&out->lock);
3749
3750 if (ret != 0) {
3751 out_on_error(&out->stream.common);
3752 if (sleeptime_us != 0)
3753 usleep(sleeptime_us);
3754 }
3755 return bytes;
3756 }
3757
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)3758 static int out_get_render_position(const struct audio_stream_out *stream,
3759 uint32_t *dsp_frames)
3760 {
3761 struct stream_out *out = (struct stream_out *)stream;
3762 *dsp_frames = 0;
3763 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
3764 lock_output_stream(out);
3765 if (out->compr != NULL) {
3766 unsigned long frames = 0;
3767 // TODO: check return value
3768 compress_get_tstamp(out->compr, &frames, &out->sample_rate);
3769 *dsp_frames = (uint32_t)frames;
3770 ALOGVV("%s rendered frames %d sample_rate %d",
3771 __func__, *dsp_frames, out->sample_rate);
3772 }
3773 pthread_mutex_unlock(&out->lock);
3774 return 0;
3775 } else
3776 return -ENODATA;
3777 }
3778
out_add_audio_effect(const struct audio_stream * stream __unused,effect_handle_t effect __unused)3779 static int out_add_audio_effect(const struct audio_stream *stream __unused,
3780 effect_handle_t effect __unused)
3781 {
3782 return 0;
3783 }
3784
out_remove_audio_effect(const struct audio_stream * stream __unused,effect_handle_t effect __unused)3785 static int out_remove_audio_effect(const struct audio_stream *stream __unused,
3786 effect_handle_t effect __unused)
3787 {
3788 return 0;
3789 }
3790
out_get_next_write_timestamp(const struct audio_stream_out * stream __unused,int64_t * timestamp __unused)3791 static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
3792 int64_t *timestamp __unused)
3793 {
3794 return -ENOSYS;
3795 }
3796
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)3797 static int out_get_presentation_position(const struct audio_stream_out *stream,
3798 uint64_t *frames, struct timespec *timestamp)
3799 {
3800 struct stream_out *out = (struct stream_out *)stream;
3801 int ret = -ENODATA;
3802 unsigned long dsp_frames;
3803
3804 lock_output_stream(out);
3805
3806 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3807 if (out->compr != NULL) {
3808 // TODO: check return value
3809 compress_get_tstamp(out->compr, &dsp_frames,
3810 &out->sample_rate);
3811 // Adjustment accounts for A2DP encoder latency with offload usecases
3812 // Note: Encoder latency is returned in ms.
3813 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
3814 unsigned long offset =
3815 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
3816 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
3817 }
3818 ALOGVV("%s rendered frames %ld sample_rate %d",
3819 __func__, dsp_frames, out->sample_rate);
3820 *frames = dsp_frames;
3821 ret = 0;
3822 /* this is the best we can do */
3823 clock_gettime(CLOCK_MONOTONIC, timestamp);
3824 }
3825 } else {
3826 if (out->pcm) {
3827 unsigned int avail;
3828 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
3829
3830 // pcm_get_htimestamp() computes the available frames by comparing
3831 // the alsa driver hw_ptr and the appl_ptr levels.
3832 // In underrun, the hw_ptr may keep running and report an excessively
3833 // large number available number.
3834 if (avail > out->kernel_buffer_size) {
3835 ALOGW("%s: avail:%u > kernel_buffer_size:%zu clamping!",
3836 __func__, avail, out->kernel_buffer_size);
3837 avail = out->kernel_buffer_size;
3838 out->last_fifo_frames_remaining = 0;
3839 } else {
3840 out->last_fifo_frames_remaining = out->kernel_buffer_size - avail;
3841 }
3842 out->last_fifo_valid = true;
3843 out->last_fifo_time_ns = audio_utils_ns_from_timespec(timestamp);
3844
3845 int64_t signed_frames = out->written - out->last_fifo_frames_remaining;
3846
3847 ALOGVV("%s: frames:%lld avail:%u kernel_buffer_size:%zu",
3848 __func__, (long long)signed_frames, avail, out->kernel_buffer_size);
3849
3850 // This adjustment accounts for buffering after app processor.
3851 // It is based on estimated DSP latency per use case, rather than exact.
3852 signed_frames -=
3853 (platform_render_latency(out) * out->sample_rate / 1000000LL);
3854
3855 // Adjustment accounts for A2DP encoder latency with non-offload usecases
3856 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
3857 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
3858 signed_frames -=
3859 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
3860 }
3861
3862 // It would be unusual for this value to be negative, but check just in case ...
3863 if (signed_frames >= 0) {
3864 *frames = signed_frames;
3865 ret = 0;
3866 }
3867 }
3868 }
3869 }
3870
3871 pthread_mutex_unlock(&out->lock);
3872
3873 return ret;
3874 }
3875
out_set_callback(struct audio_stream_out * stream,stream_callback_t callback,void * cookie)3876 static int out_set_callback(struct audio_stream_out *stream,
3877 stream_callback_t callback, void *cookie)
3878 {
3879 struct stream_out *out = (struct stream_out *)stream;
3880
3881 ALOGV("%s", __func__);
3882 lock_output_stream(out);
3883 out->offload_callback = callback;
3884 out->offload_cookie = cookie;
3885 pthread_mutex_unlock(&out->lock);
3886 return 0;
3887 }
3888
out_pause(struct audio_stream_out * stream)3889 static int out_pause(struct audio_stream_out* stream)
3890 {
3891 struct stream_out *out = (struct stream_out *)stream;
3892 int status = -ENOSYS;
3893 ALOGV("%s", __func__);
3894 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3895 status = -ENODATA;
3896 lock_output_stream(out);
3897 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
3898 status = compress_pause(out->compr);
3899 out->offload_state = OFFLOAD_STATE_PAUSED;
3900 }
3901 pthread_mutex_unlock(&out->lock);
3902 }
3903 return status;
3904 }
3905
out_resume(struct audio_stream_out * stream)3906 static int out_resume(struct audio_stream_out* stream)
3907 {
3908 struct stream_out *out = (struct stream_out *)stream;
3909 int status = -ENOSYS;
3910 ALOGV("%s", __func__);
3911 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3912 status = -ENODATA;
3913 lock_output_stream(out);
3914 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
3915 status = compress_resume(out->compr);
3916 out->offload_state = OFFLOAD_STATE_PLAYING;
3917 }
3918 pthread_mutex_unlock(&out->lock);
3919 }
3920 return status;
3921 }
3922
out_drain(struct audio_stream_out * stream,audio_drain_type_t type)3923 static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
3924 {
3925 struct stream_out *out = (struct stream_out *)stream;
3926 int status = -ENOSYS;
3927 ALOGV("%s", __func__);
3928 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3929 lock_output_stream(out);
3930 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
3931 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
3932 else
3933 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
3934 pthread_mutex_unlock(&out->lock);
3935 }
3936 return status;
3937 }
3938
out_flush(struct audio_stream_out * stream)3939 static int out_flush(struct audio_stream_out* stream)
3940 {
3941 struct stream_out *out = (struct stream_out *)stream;
3942 ALOGV("%s", __func__);
3943 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3944 lock_output_stream(out);
3945 stop_compressed_output_l(out);
3946 pthread_mutex_unlock(&out->lock);
3947 return 0;
3948 }
3949 return -ENOSYS;
3950 }
3951
out_stop(const struct audio_stream_out * stream)3952 static int out_stop(const struct audio_stream_out* stream)
3953 {
3954 struct stream_out *out = (struct stream_out *)stream;
3955 struct audio_device *adev = out->dev;
3956 int ret = -ENOSYS;
3957
3958 ALOGV("%s", __func__);
3959 pthread_mutex_lock(&adev->lock);
3960 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
3961 out->playback_started && out->pcm != NULL) {
3962 pcm_stop(out->pcm);
3963 ret = stop_output_stream(out);
3964 out->playback_started = false;
3965 }
3966 pthread_mutex_unlock(&adev->lock);
3967 return ret;
3968 }
3969
out_start(const struct audio_stream_out * stream)3970 static int out_start(const struct audio_stream_out* stream)
3971 {
3972 struct stream_out *out = (struct stream_out *)stream;
3973 struct audio_device *adev = out->dev;
3974 int ret = -ENOSYS;
3975
3976 ALOGV("%s", __func__);
3977 pthread_mutex_lock(&adev->lock);
3978 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
3979 !out->playback_started && out->pcm != NULL) {
3980 ret = start_output_stream(out);
3981 if (ret == 0) {
3982 out->playback_started = true;
3983 }
3984 }
3985 pthread_mutex_unlock(&adev->lock);
3986 return ret;
3987 }
3988
3989 /*
3990 * Modify config->period_count based on min_size_frames
3991 */
adjust_mmap_period_count(struct pcm_config * config,int32_t min_size_frames)3992 static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
3993 {
3994 int periodCountRequested = (min_size_frames + config->period_size - 1)
3995 / config->period_size;
3996 int periodCount = MMAP_PERIOD_COUNT_MIN;
3997
3998 ALOGV("%s original config.period_size = %d config.period_count = %d",
3999 __func__, config->period_size, config->period_count);
4000
4001 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
4002 periodCount *= 2;
4003 }
4004 config->period_count = periodCount;
4005
4006 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
4007 }
4008
4009 // Read offset for the positional timestamp from a persistent vendor property.
4010 // This is to workaround apparent inaccuracies in the timing information that
4011 // is used by the AAudio timing model. The inaccuracies can cause glitches.
get_mmap_out_time_offset()4012 static int64_t get_mmap_out_time_offset() {
4013 const int32_t kDefaultOffsetMicros = 0;
4014 int32_t mmap_time_offset_micros = property_get_int32(
4015 "persist.audio.out_mmap_delay_micros", kDefaultOffsetMicros);
4016 ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros);
4017 return mmap_time_offset_micros * (int64_t)1000;
4018 }
4019
out_create_mmap_buffer(const struct audio_stream_out * stream,int32_t min_size_frames,struct audio_mmap_buffer_info * info)4020 static int out_create_mmap_buffer(const struct audio_stream_out *stream,
4021 int32_t min_size_frames,
4022 struct audio_mmap_buffer_info *info)
4023 {
4024 struct stream_out *out = (struct stream_out *)stream;
4025 struct audio_device *adev = out->dev;
4026 int ret = 0;
4027 unsigned int offset1;
4028 unsigned int frames1;
4029 const char *step = "";
4030 uint32_t mmap_size;
4031 uint32_t buffer_size;
4032
4033 ALOGV("%s", __func__);
4034 lock_output_stream(out);
4035 pthread_mutex_lock(&adev->lock);
4036
4037 if (info == NULL || min_size_frames <= 0 || min_size_frames > MMAP_MIN_SIZE_FRAMES_MAX) {
4038 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
4039 ret = -EINVAL;
4040 goto exit;
4041 }
4042 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
4043 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
4044 ret = -ENOSYS;
4045 goto exit;
4046 }
4047 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
4048 if (out->pcm_device_id < 0) {
4049 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
4050 __func__, out->pcm_device_id, out->usecase);
4051 ret = -EINVAL;
4052 goto exit;
4053 }
4054
4055 adjust_mmap_period_count(&out->config, min_size_frames);
4056
4057 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
4058 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
4059 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
4060 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
4061 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
4062 step = "open";
4063 ret = -ENODEV;
4064 goto exit;
4065 }
4066 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
4067 if (ret < 0) {
4068 step = "begin";
4069 goto exit;
4070 }
4071 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
4072 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
4073 info->burst_size_frames = out->config.period_size;
4074 ret = platform_get_mmap_data_fd(adev->platform,
4075 out->pcm_device_id, 0 /*playback*/,
4076 &info->shared_memory_fd,
4077 &mmap_size);
4078 if (ret < 0) {
4079 // Fall back to non exclusive mode
4080 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
4081 } else {
4082 out->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
4083 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, out->mmap_shared_memory_fd);
4084
4085 if (mmap_size < buffer_size) {
4086 step = "mmap";
4087 goto exit;
4088 }
4089 // FIXME: indicate exclusive mode support by returning a negative buffer size
4090 info->buffer_size_frames *= -1;
4091 }
4092 memset(info->shared_memory_address, 0, buffer_size);
4093
4094 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
4095 if (ret < 0) {
4096 step = "commit";
4097 goto exit;
4098 }
4099
4100 out->mmap_time_offset_nanos = get_mmap_out_time_offset();
4101
4102 out->standby = false;
4103 ret = 0;
4104
4105 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
4106 __func__, info->shared_memory_address, info->buffer_size_frames);
4107
4108 exit:
4109 if (ret != 0) {
4110 if (out->pcm == NULL) {
4111 ALOGE("%s: %s - %d", __func__, step, ret);
4112 } else {
4113 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
4114 pcm_close(out->pcm);
4115 out->pcm = NULL;
4116 }
4117 }
4118 pthread_mutex_unlock(&adev->lock);
4119 pthread_mutex_unlock(&out->lock);
4120 return ret;
4121 }
4122
out_get_mmap_position(const struct audio_stream_out * stream,struct audio_mmap_position * position)4123 static int out_get_mmap_position(const struct audio_stream_out *stream,
4124 struct audio_mmap_position *position)
4125 {
4126 int ret = 0;
4127 struct stream_out *out = (struct stream_out *)stream;
4128 ALOGVV("%s", __func__);
4129 if (position == NULL) {
4130 return -EINVAL;
4131 }
4132 lock_output_stream(out);
4133 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP ||
4134 out->pcm == NULL) {
4135 ret = -ENOSYS;
4136 goto exit;
4137 }
4138
4139 struct timespec ts = { 0, 0 };
4140 ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
4141 if (ret < 0) {
4142 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
4143 goto exit;
4144 }
4145 position->time_nanoseconds = audio_utils_ns_from_timespec(&ts)
4146 + out->mmap_time_offset_nanos;
4147
4148 exit:
4149 pthread_mutex_unlock(&out->lock);
4150 return ret;
4151 }
4152
4153
4154 /** audio_stream_in implementation **/
in_get_sample_rate(const struct audio_stream * stream)4155 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
4156 {
4157 struct stream_in *in = (struct stream_in *)stream;
4158
4159 return in->config.rate;
4160 }
4161
in_set_sample_rate(struct audio_stream * stream __unused,uint32_t rate __unused)4162 static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
4163 {
4164 return -ENOSYS;
4165 }
4166
in_get_buffer_size(const struct audio_stream * stream)4167 static size_t in_get_buffer_size(const struct audio_stream *stream)
4168 {
4169 struct stream_in *in = (struct stream_in *)stream;
4170 return in->config.period_size * in->af_period_multiplier *
4171 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
4172 }
4173
in_get_channels(const struct audio_stream * stream)4174 static uint32_t in_get_channels(const struct audio_stream *stream)
4175 {
4176 struct stream_in *in = (struct stream_in *)stream;
4177
4178 return in->channel_mask;
4179 }
4180
in_get_format(const struct audio_stream * stream)4181 static audio_format_t in_get_format(const struct audio_stream *stream)
4182 {
4183 struct stream_in *in = (struct stream_in *)stream;
4184 return in->format;
4185 }
4186
in_set_format(struct audio_stream * stream __unused,audio_format_t format __unused)4187 static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
4188 {
4189 return -ENOSYS;
4190 }
4191
in_standby(struct audio_stream * stream)4192 static int in_standby(struct audio_stream *stream)
4193 {
4194 struct stream_in *in = (struct stream_in *)stream;
4195 struct audio_device *adev = in->dev;
4196 int status = 0;
4197 bool do_stop = true;
4198
4199 ALOGV("%s: enter", __func__);
4200
4201 lock_input_stream(in);
4202
4203 if (!in->standby && (in->flags & AUDIO_INPUT_FLAG_HW_HOTWORD)) {
4204 ALOGV("%s: sound trigger pcm stop lab", __func__);
4205 audio_extn_sound_trigger_stop_lab(in);
4206 in->standby = true;
4207 }
4208
4209 if (!in->standby) {
4210 if (adev->adm_deregister_stream)
4211 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
4212
4213 pthread_mutex_lock(&adev->lock);
4214 in->standby = true;
4215 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
4216 do_stop = in->capture_started;
4217 in->capture_started = false;
4218
4219 if (in->mmap_shared_memory_fd >= 0) {
4220 ALOGV("%s: closing mmap_shared_memory_fd = %d",
4221 __func__, in->mmap_shared_memory_fd);
4222 close(in->mmap_shared_memory_fd);
4223 in->mmap_shared_memory_fd = -1;
4224 }
4225
4226 }
4227 if (in->pcm) {
4228 pcm_close(in->pcm);
4229 in->pcm = NULL;
4230 }
4231
4232 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
4233 adev->enable_voicerx = false;
4234
4235 if (do_stop) {
4236 status = stop_input_stream(in);
4237 }
4238
4239 pthread_mutex_unlock(&adev->lock);
4240 }
4241 pthread_mutex_unlock(&in->lock);
4242 ALOGV("%s: exit: status(%d)", __func__, status);
4243 return status;
4244 }
4245
in_dump(const struct audio_stream * stream,int fd)4246 static int in_dump(const struct audio_stream *stream, int fd)
4247 {
4248 struct stream_in *in = (struct stream_in *)stream;
4249
4250 // We try to get the lock for consistency,
4251 // but it isn't necessary for these variables.
4252 // If we're not in standby, we may be blocked on a read.
4253 const bool locked = (pthread_mutex_trylock(&in->lock) == 0);
4254 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no");
4255 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read);
4256 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted);
4257
4258 char buffer[256]; // for statistics formatting
4259 if (in->start_latency_ms.n > 0) {
4260 simple_stats_to_string(&in->start_latency_ms, buffer, sizeof(buffer));
4261 dprintf(fd, " Start latency ms: %s\n", buffer);
4262 }
4263
4264 if (locked) {
4265 pthread_mutex_unlock(&in->lock);
4266 }
4267
4268 // dump error info
4269 (void)error_log_dump(
4270 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
4271 return 0;
4272 }
4273
in_set_parameters(struct audio_stream * stream,const char * kvpairs)4274 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
4275 {
4276 struct stream_in *in = (struct stream_in *)stream;
4277 struct audio_device *adev = in->dev;
4278 struct str_parms *parms;
4279 char *str;
4280 char value[32];
4281 int ret, val = 0;
4282 int status = 0;
4283
4284 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
4285 parms = str_parms_create_str(kvpairs);
4286
4287 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
4288
4289 lock_input_stream(in);
4290
4291 pthread_mutex_lock(&adev->lock);
4292 if (ret >= 0) {
4293 val = atoi(value);
4294 /* no audio source uses val == 0 */
4295 if ((in->source != val) && (val != 0)) {
4296 in->source = val;
4297 }
4298 }
4299
4300 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
4301
4302 if (ret >= 0) {
4303 val = atoi(value);
4304 if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) {
4305
4306 // Workaround: If routing to an non existing usb device, fail gracefully
4307 // The routing request will otherwise block during 10 second
4308 int card;
4309 if (audio_is_usb_in_device(val) &&
4310 (card = get_alive_usb_card(parms)) >= 0) {
4311
4312 ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d", card);
4313 status = -ENOSYS;
4314 } else {
4315
4316 in->device = val;
4317 /* If recording is in progress, change the tx device to new device */
4318 if (!in->standby) {
4319 ALOGV("update input routing change");
4320 // inform adm before actual routing to prevent glitches.
4321 if (adev->adm_on_routing_change) {
4322 adev->adm_on_routing_change(adev->adm_data,
4323 in->capture_handle);
4324 }
4325 select_devices(adev, in->usecase);
4326 }
4327 }
4328 }
4329 }
4330
4331 pthread_mutex_unlock(&adev->lock);
4332 pthread_mutex_unlock(&in->lock);
4333
4334 str_parms_destroy(parms);
4335 ALOGV("%s: exit: status(%d)", __func__, status);
4336 return status;
4337 }
4338
in_get_parameters(const struct audio_stream * stream,const char * keys)4339 static char* in_get_parameters(const struct audio_stream *stream,
4340 const char *keys)
4341 {
4342 struct stream_in *in = (struct stream_in *)stream;
4343 struct str_parms *query = str_parms_create_str(keys);
4344 char *str;
4345 struct str_parms *reply = str_parms_create();
4346 bool replied = false;
4347
4348 ALOGV("%s: enter: keys - %s", __func__, keys);
4349 replied |= stream_get_parameter_channels(query, reply,
4350 &in->supported_channel_masks[0]);
4351 replied |= stream_get_parameter_formats(query, reply,
4352 &in->supported_formats[0]);
4353 replied |= stream_get_parameter_rates(query, reply,
4354 &in->supported_sample_rates[0]);
4355 if (replied) {
4356 str = str_parms_to_str(reply);
4357 } else {
4358 str = strdup("");
4359 }
4360 str_parms_destroy(query);
4361 str_parms_destroy(reply);
4362 ALOGV("%s: exit: returns - %s", __func__, str);
4363 return str;
4364 }
4365
in_set_gain(struct audio_stream_in * stream,float gain)4366 static int in_set_gain(struct audio_stream_in *stream, float gain)
4367 {
4368 struct stream_in *in = (struct stream_in *)stream;
4369 char mixer_ctl_name[128];
4370 struct mixer_ctl *ctl;
4371 int ctl_value;
4372
4373 ALOGV("%s: gain %f", __func__, gain);
4374
4375 if (stream == NULL)
4376 return -EINVAL;
4377
4378 /* in_set_gain() only used to silence MMAP capture for now */
4379 if (in->usecase != USECASE_AUDIO_RECORD_MMAP)
4380 return -ENOSYS;
4381
4382 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id);
4383
4384 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name);
4385 if (!ctl) {
4386 ALOGW("%s: Could not get ctl for mixer cmd - %s",
4387 __func__, mixer_ctl_name);
4388 return -ENOSYS;
4389 }
4390
4391 if (gain < RECORD_GAIN_MIN)
4392 gain = RECORD_GAIN_MIN;
4393 else if (gain > RECORD_GAIN_MAX)
4394 gain = RECORD_GAIN_MAX;
4395 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain);
4396
4397 mixer_ctl_set_value(ctl, 0, ctl_value);
4398 return 0;
4399 }
4400
in_snd_mon_cb(void * stream,struct str_parms * parms)4401 static void in_snd_mon_cb(void * stream, struct str_parms * parms)
4402 {
4403 if (!stream || !parms)
4404 return;
4405
4406 struct stream_in *in = (struct stream_in *)stream;
4407 struct audio_device *adev = in->dev;
4408
4409 card_status_t status;
4410 int card;
4411 if (parse_snd_card_status(parms, &card, &status) < 0)
4412 return;
4413
4414 pthread_mutex_lock(&adev->lock);
4415 bool valid_cb = (card == adev->snd_card);
4416 pthread_mutex_unlock(&adev->lock);
4417
4418 if (!valid_cb)
4419 return;
4420
4421 lock_input_stream(in);
4422 if (in->card_status != status)
4423 in->card_status = status;
4424 pthread_mutex_unlock(&in->lock);
4425
4426 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
4427 use_case_table[in->usecase],
4428 status == CARD_STATUS_OFFLINE ? "offline" : "online");
4429
4430 // a better solution would be to report error back to AF and let
4431 // it put the stream to standby
4432 if (status == CARD_STATUS_OFFLINE)
4433 in_standby(&in->stream.common);
4434
4435 return;
4436 }
4437
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)4438 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
4439 size_t bytes)
4440 {
4441 struct stream_in *in = (struct stream_in *)stream;
4442 struct audio_device *adev = in->dev;
4443 int i, ret = -1;
4444 int *int_buf_stream = NULL;
4445 int error_code = ERROR_CODE_STANDBY; // initial errors are considered coming out of standby.
4446
4447 lock_input_stream(in);
4448 const size_t frame_size = audio_stream_in_frame_size(stream);
4449 const size_t frames = bytes / frame_size;
4450
4451 if (in->flags & AUDIO_INPUT_FLAG_HW_HOTWORD) {
4452 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
4453 /* Read from sound trigger HAL */
4454 audio_extn_sound_trigger_read(in, buffer, bytes);
4455 pthread_mutex_unlock(&in->lock);
4456 return bytes;
4457 }
4458
4459 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
4460 ret = -ENOSYS;
4461 goto exit;
4462 }
4463
4464 if (in->standby) {
4465 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
4466
4467 pthread_mutex_lock(&adev->lock);
4468 ret = start_input_stream(in);
4469 pthread_mutex_unlock(&adev->lock);
4470 if (ret != 0) {
4471 goto exit;
4472 }
4473 in->standby = 0;
4474
4475 // log startup time in ms.
4476 simple_stats_log(
4477 &in->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
4478 }
4479
4480 // errors that occur here are read errors.
4481 error_code = ERROR_CODE_READ;
4482
4483 //what's the duration requested by the client?
4484 long ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
4485 in->config.rate;
4486 request_in_focus(in, ns);
4487
4488 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
4489 if (in->pcm) {
4490 if (use_mmap) {
4491 ret = pcm_mmap_read(in->pcm, buffer, bytes);
4492 } else {
4493 ret = pcm_read(in->pcm, buffer, bytes);
4494 }
4495 if (ret < 0) {
4496 ALOGE("Failed to read w/err %s", strerror(errno));
4497 ret = -errno;
4498 }
4499 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
4500 if (bytes % 4 == 0) {
4501 /* data from DSP comes in 24_8 format, convert it to 8_24 */
4502 int_buf_stream = buffer;
4503 for (size_t itt=0; itt < bytes/4 ; itt++) {
4504 int_buf_stream[itt] >>= 8;
4505 }
4506 } else {
4507 ALOGE("%s: !!! something wrong !!! ... data not 32 bit aligned ", __func__);
4508 ret = -EINVAL;
4509 goto exit;
4510 }
4511 }
4512 }
4513
4514 release_in_focus(in, ns);
4515
4516 /*
4517 * Instead of writing zeroes here, we could trust the hardware
4518 * to always provide zeroes when muted.
4519 * No need to acquire adev->lock to read mic_muted here as we don't change its state.
4520 */
4521 if (ret == 0 && adev->mic_muted &&
4522 !voice_is_in_call_rec_stream(in) &&
4523 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) {
4524 memset(buffer, 0, bytes);
4525 in->frames_muted += frames;
4526 }
4527
4528 exit:
4529 pthread_mutex_unlock(&in->lock);
4530
4531 if (ret != 0) {
4532 error_log_log(in->error_log, error_code, audio_utils_get_real_time_ns());
4533 in_standby(&in->stream.common);
4534 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
4535 usleep(frames * 1000000LL / in_get_sample_rate(&in->stream.common));
4536 memset(buffer, 0, bytes); // clear return data
4537 in->frames_muted += frames;
4538 }
4539 if (bytes > 0) {
4540 in->frames_read += frames;
4541 }
4542 return bytes;
4543 }
4544
in_get_input_frames_lost(struct audio_stream_in * stream __unused)4545 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
4546 {
4547 return 0;
4548 }
4549
in_get_capture_position(const struct audio_stream_in * stream,int64_t * frames,int64_t * time)4550 static int in_get_capture_position(const struct audio_stream_in *stream,
4551 int64_t *frames, int64_t *time)
4552 {
4553 if (stream == NULL || frames == NULL || time == NULL) {
4554 return -EINVAL;
4555 }
4556 struct stream_in *in = (struct stream_in *)stream;
4557 int ret = -ENOSYS;
4558
4559 lock_input_stream(in);
4560 // note: ST sessions do not close the alsa pcm driver synchronously
4561 // on standby. Therefore, we may return an error even though the
4562 // pcm stream is still opened.
4563 if (in->standby) {
4564 ALOGE_IF(in->pcm != NULL && !(in->flags & AUDIO_INPUT_FLAG_HW_HOTWORD),
4565 "%s stream in standby but pcm not NULL for non ST session", __func__);
4566 goto exit;
4567 }
4568 if (in->pcm) {
4569 struct timespec timestamp;
4570 unsigned int avail;
4571 if (pcm_get_htimestamp(in->pcm, &avail, ×tamp) == 0) {
4572 *frames = in->frames_read + avail;
4573 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec
4574 - platform_capture_latency(in) * 1000LL;
4575 ret = 0;
4576 }
4577 }
4578 exit:
4579 pthread_mutex_unlock(&in->lock);
4580 return ret;
4581 }
4582
in_update_effect_list(bool add,effect_handle_t effect,struct listnode * head)4583 static int in_update_effect_list(bool add, effect_handle_t effect,
4584 struct listnode *head)
4585 {
4586 struct listnode *node;
4587 struct in_effect_list *elist = NULL;
4588 struct in_effect_list *target = NULL;
4589 int ret = 0;
4590
4591 if (!head)
4592 return ret;
4593
4594 list_for_each(node, head) {
4595 elist = node_to_item(node, struct in_effect_list, list);
4596 if (elist->handle == effect) {
4597 target = elist;
4598 break;
4599 }
4600 }
4601
4602 if (add) {
4603 if (target) {
4604 ALOGD("effect %p already exist", effect);
4605 return ret;
4606 }
4607
4608 target = (struct in_effect_list *)
4609 calloc(1, sizeof(struct in_effect_list));
4610
4611 if (!target) {
4612 ALOGE("%s:fail to allocate memory", __func__);
4613 return -ENOMEM;
4614 }
4615
4616 target->handle = effect;
4617 list_add_tail(head, &target->list);
4618 } else {
4619 if (target) {
4620 list_remove(&target->list);
4621 free(target);
4622 }
4623 }
4624
4625 return ret;
4626 }
4627
add_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect,bool enable)4628 static int add_remove_audio_effect(const struct audio_stream *stream,
4629 effect_handle_t effect,
4630 bool enable)
4631 {
4632 struct stream_in *in = (struct stream_in *)stream;
4633 struct audio_device *adev = in->dev;
4634 int status = 0;
4635 effect_descriptor_t desc;
4636
4637 status = (*effect)->get_descriptor(effect, &desc);
4638 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
4639
4640 if (status != 0)
4641 return status;
4642
4643 lock_input_stream(in);
4644 pthread_mutex_lock(&in->dev->lock);
4645 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
4646 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
4647 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
4648 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
4649
4650 in_update_effect_list(enable, effect, &in->aec_list);
4651 enable = !list_empty(&in->aec_list);
4652 if (enable == in->enable_aec)
4653 goto exit;
4654
4655 in->enable_aec = enable;
4656 ALOGD("AEC enable %d", enable);
4657
4658 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
4659 adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
4660 adev->enable_voicerx = enable;
4661 struct audio_usecase *usecase;
4662 struct listnode *node;
4663 list_for_each(node, &adev->usecase_list) {
4664 usecase = node_to_item(node, struct audio_usecase, list);
4665 if (usecase->type == PCM_PLAYBACK)
4666 select_devices(adev, usecase->id);
4667 }
4668 }
4669 if (!in->standby
4670 && enable_disable_effect(in->dev, in, EFFECT_AEC, enable) == -ENOSYS)
4671 select_devices(in->dev, in->usecase);
4672 }
4673 if (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0) {
4674
4675 in_update_effect_list(enable, effect, &in->ns_list);
4676 enable = !list_empty(&in->ns_list);
4677 if (enable == in->enable_ns)
4678 goto exit;
4679
4680 in->enable_ns = enable;
4681 ALOGD("NS enable %d", enable);
4682 if (!in->standby) {
4683 if (in->source != AUDIO_SOURCE_VOICE_COMMUNICATION
4684 || enable_disable_effect(in->dev, in, EFFECT_NS, enable) == -ENOSYS)
4685 select_devices(in->dev, in->usecase);
4686 }
4687 }
4688 exit:
4689 pthread_mutex_unlock(&in->dev->lock);
4690 pthread_mutex_unlock(&in->lock);
4691
4692 return 0;
4693 }
4694
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)4695 static int in_add_audio_effect(const struct audio_stream *stream,
4696 effect_handle_t effect)
4697 {
4698 ALOGV("%s: effect %p", __func__, effect);
4699 return add_remove_audio_effect(stream, effect, true);
4700 }
4701
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)4702 static int in_remove_audio_effect(const struct audio_stream *stream,
4703 effect_handle_t effect)
4704 {
4705 ALOGV("%s: effect %p", __func__, effect);
4706 return add_remove_audio_effect(stream, effect, false);
4707 }
4708
in_stop(const struct audio_stream_in * stream)4709 static int in_stop(const struct audio_stream_in* stream)
4710 {
4711 struct stream_in *in = (struct stream_in *)stream;
4712 struct audio_device *adev = in->dev;
4713
4714 int ret = -ENOSYS;
4715 ALOGV("%s", __func__);
4716 pthread_mutex_lock(&adev->lock);
4717 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
4718 in->capture_started && in->pcm != NULL) {
4719 pcm_stop(in->pcm);
4720 ret = stop_input_stream(in);
4721 in->capture_started = false;
4722 }
4723 pthread_mutex_unlock(&adev->lock);
4724 return ret;
4725 }
4726
in_start(const struct audio_stream_in * stream)4727 static int in_start(const struct audio_stream_in* stream)
4728 {
4729 struct stream_in *in = (struct stream_in *)stream;
4730 struct audio_device *adev = in->dev;
4731 int ret = -ENOSYS;
4732
4733 ALOGV("%s in %p", __func__, in);
4734 pthread_mutex_lock(&adev->lock);
4735 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
4736 !in->capture_started && in->pcm != NULL) {
4737 if (!in->capture_started) {
4738 ret = start_input_stream(in);
4739 if (ret == 0) {
4740 in->capture_started = true;
4741 }
4742 }
4743 }
4744 pthread_mutex_unlock(&adev->lock);
4745 return ret;
4746 }
4747
4748 // Read offset for the positional timestamp from a persistent vendor property.
4749 // This is to workaround apparent inaccuracies in the timing information that
4750 // is used by the AAudio timing model. The inaccuracies can cause glitches.
in_get_mmap_time_offset()4751 static int64_t in_get_mmap_time_offset() {
4752 const int32_t kDefaultOffsetMicros = 0;
4753 int32_t mmap_time_offset_micros = property_get_int32(
4754 "persist.audio.in_mmap_delay_micros", kDefaultOffsetMicros);
4755 ALOGI("in_get_mmap_time_offset set to %d micros", mmap_time_offset_micros);
4756 return mmap_time_offset_micros * (int64_t)1000;
4757 }
4758
in_create_mmap_buffer(const struct audio_stream_in * stream,int32_t min_size_frames,struct audio_mmap_buffer_info * info)4759 static int in_create_mmap_buffer(const struct audio_stream_in *stream,
4760 int32_t min_size_frames,
4761 struct audio_mmap_buffer_info *info)
4762 {
4763 struct stream_in *in = (struct stream_in *)stream;
4764 struct audio_device *adev = in->dev;
4765 int ret = 0;
4766 unsigned int offset1;
4767 unsigned int frames1;
4768 const char *step = "";
4769 uint32_t mmap_size;
4770 uint32_t buffer_size;
4771
4772 lock_input_stream(in);
4773 pthread_mutex_lock(&adev->lock);
4774 ALOGV("%s in %p", __func__, in);
4775
4776 if (info == NULL || min_size_frames <= 0 || min_size_frames > MMAP_MIN_SIZE_FRAMES_MAX) {
4777 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
4778 ret = -EINVAL;
4779 goto exit;
4780 }
4781 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
4782 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
4783 ALOGV("%s in %p", __func__, in);
4784 ret = -ENOSYS;
4785 goto exit;
4786 }
4787 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
4788 if (in->pcm_device_id < 0) {
4789 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
4790 __func__, in->pcm_device_id, in->usecase);
4791 ret = -EINVAL;
4792 goto exit;
4793 }
4794
4795 adjust_mmap_period_count(&in->config, min_size_frames);
4796
4797 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
4798 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
4799 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
4800 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
4801 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
4802 step = "open";
4803 ret = -ENODEV;
4804 goto exit;
4805 }
4806
4807 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
4808 if (ret < 0) {
4809 step = "begin";
4810 goto exit;
4811 }
4812 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
4813 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
4814 info->burst_size_frames = in->config.period_size;
4815 ret = platform_get_mmap_data_fd(adev->platform,
4816 in->pcm_device_id, 1 /*capture*/,
4817 &info->shared_memory_fd,
4818 &mmap_size);
4819 if (ret < 0) {
4820 // Fall back to non exclusive mode
4821 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
4822 } else {
4823 in->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
4824 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, in->mmap_shared_memory_fd);
4825
4826 if (mmap_size < buffer_size) {
4827 step = "mmap";
4828 goto exit;
4829 }
4830 // FIXME: indicate exclusive mode support by returning a negative buffer size
4831 info->buffer_size_frames *= -1;
4832 }
4833
4834 memset(info->shared_memory_address, 0, buffer_size);
4835
4836 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
4837 if (ret < 0) {
4838 step = "commit";
4839 goto exit;
4840 }
4841
4842 in->mmap_time_offset_nanos = in_get_mmap_time_offset();
4843
4844 in->standby = false;
4845 ret = 0;
4846
4847 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
4848 __func__, info->shared_memory_address, info->buffer_size_frames);
4849
4850 exit:
4851 if (ret != 0) {
4852 if (in->pcm == NULL) {
4853 ALOGE("%s: %s - %d", __func__, step, ret);
4854 } else {
4855 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
4856 pcm_close(in->pcm);
4857 in->pcm = NULL;
4858 }
4859 }
4860 pthread_mutex_unlock(&adev->lock);
4861 pthread_mutex_unlock(&in->lock);
4862 return ret;
4863 }
4864
in_get_mmap_position(const struct audio_stream_in * stream,struct audio_mmap_position * position)4865 static int in_get_mmap_position(const struct audio_stream_in *stream,
4866 struct audio_mmap_position *position)
4867 {
4868 int ret = 0;
4869 struct stream_in *in = (struct stream_in *)stream;
4870 ALOGVV("%s", __func__);
4871 if (position == NULL) {
4872 return -EINVAL;
4873 }
4874 lock_input_stream(in);
4875 if (in->usecase != USECASE_AUDIO_RECORD_MMAP ||
4876 in->pcm == NULL) {
4877 ret = -ENOSYS;
4878 goto exit;
4879 }
4880 struct timespec ts = { 0, 0 };
4881 ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
4882 if (ret < 0) {
4883 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
4884 goto exit;
4885 }
4886 position->time_nanoseconds = audio_utils_ns_from_timespec(&ts)
4887 + in->mmap_time_offset_nanos;
4888
4889 exit:
4890 pthread_mutex_unlock(&in->lock);
4891 return ret;
4892 }
4893
in_get_active_microphones(const struct audio_stream_in * stream,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)4894 static int in_get_active_microphones(const struct audio_stream_in *stream,
4895 struct audio_microphone_characteristic_t *mic_array,
4896 size_t *mic_count) {
4897 struct stream_in *in = (struct stream_in *)stream;
4898 struct audio_device *adev = in->dev;
4899 ALOGVV("%s", __func__);
4900
4901 lock_input_stream(in);
4902 pthread_mutex_lock(&adev->lock);
4903 int ret = platform_get_active_microphones(adev->platform,
4904 audio_channel_count_from_in_mask(in->channel_mask),
4905 in->usecase, mic_array, mic_count);
4906 pthread_mutex_unlock(&adev->lock);
4907 pthread_mutex_unlock(&in->lock);
4908
4909 return ret;
4910 }
4911
adev_get_microphones(const struct audio_hw_device * dev,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)4912 static int adev_get_microphones(const struct audio_hw_device *dev,
4913 struct audio_microphone_characteristic_t *mic_array,
4914 size_t *mic_count) {
4915 struct audio_device *adev = (struct audio_device *)dev;
4916 ALOGVV("%s", __func__);
4917
4918 pthread_mutex_lock(&adev->lock);
4919 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
4920 pthread_mutex_unlock(&adev->lock);
4921
4922 return ret;
4923 }
4924
in_set_microphone_direction(const struct audio_stream_in * stream,audio_microphone_direction_t dir)4925 static int in_set_microphone_direction(const struct audio_stream_in *stream,
4926 audio_microphone_direction_t dir) {
4927 struct stream_in *in = (struct stream_in *)stream;
4928
4929 ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir);
4930
4931 in->direction = dir;
4932
4933 if (in->standby)
4934 return 0;
4935
4936 return audio_extn_audiozoom_set_microphone_direction(in, dir);
4937 }
4938
in_set_microphone_field_dimension(const struct audio_stream_in * stream,float zoom)4939 static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
4940 struct stream_in *in = (struct stream_in *)stream;
4941
4942 ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom);
4943
4944 if (zoom > 1.0 || zoom < -1.0)
4945 return -EINVAL;
4946
4947 in->zoom = zoom;
4948
4949 if (in->standby)
4950 return 0;
4951
4952 return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom);
4953 }
4954
in_update_sink_metadata(struct audio_stream_in * stream,const struct sink_metadata * sink_metadata)4955 static void in_update_sink_metadata(struct audio_stream_in *stream,
4956 const struct sink_metadata *sink_metadata) {
4957
4958 if (stream == NULL
4959 || sink_metadata == NULL
4960 || sink_metadata->tracks == NULL) {
4961 return;
4962 }
4963
4964 int error = 0;
4965 struct stream_in *in = (struct stream_in *)stream;
4966 struct audio_device *adev = in->dev;
4967 audio_devices_t device = AUDIO_DEVICE_NONE;
4968
4969 if (sink_metadata->track_count != 0)
4970 device = sink_metadata->tracks->dest_device;
4971
4972 lock_input_stream(in);
4973 pthread_mutex_lock(&adev->lock);
4974 ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, device);
4975
4976 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY
4977 && device != AUDIO_DEVICE_NONE
4978 && adev->voice_tx_output != NULL) {
4979 /* Use the rx device from afe-proxy record to route voice call because
4980 there is no routing if tx device is on primary hal and rx device
4981 is on other hal during voice call. */
4982 adev->voice_tx_output->devices = device;
4983
4984 if (!voice_is_call_state_active(adev)) {
4985 if (adev->mode == AUDIO_MODE_IN_CALL) {
4986 adev->current_call_output = adev->voice_tx_output;
4987 error = voice_start_call(adev);
4988 if (error != 0)
4989 ALOGE("%s: start voice call failed %d", __func__, error);
4990 }
4991 } else {
4992 adev->current_call_output = adev->voice_tx_output;
4993 voice_update_devices_for_all_voice_usecases(adev);
4994 }
4995 }
4996
4997 pthread_mutex_unlock(&adev->lock);
4998 pthread_mutex_unlock(&in->lock);
4999 }
5000
check_and_set_gapless_mode(struct audio_device * adev)5001 static int check_and_set_gapless_mode(struct audio_device *adev)
5002 {
5003 bool gapless_enabled = false;
5004 const char *mixer_ctl_name = "Compress Gapless Playback";
5005 struct mixer_ctl *ctl;
5006
5007 ALOGV("%s:", __func__);
5008 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false);
5009
5010 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5011 if (!ctl) {
5012 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5013 __func__, mixer_ctl_name);
5014 return -EINVAL;
5015 }
5016
5017 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
5018 ALOGE("%s: Could not set gapless mode %d",
5019 __func__, gapless_enabled);
5020 return -EINVAL;
5021 }
5022 return 0;
5023 }
5024
adev_open_output_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out,const char * address __unused)5025 static int adev_open_output_stream(struct audio_hw_device *dev,
5026 audio_io_handle_t handle,
5027 audio_devices_t devices,
5028 audio_output_flags_t flags,
5029 struct audio_config *config,
5030 struct audio_stream_out **stream_out,
5031 const char *address __unused)
5032 {
5033 struct audio_device *adev = (struct audio_device *)dev;
5034 struct stream_out *out;
5035 int i, ret = 0;
5036 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
5037 bool is_usb_dev = audio_is_usb_out_device(devices) &&
5038 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
5039 bool force_haptic_path =
5040 property_get_bool("vendor.audio.test_haptic", false);
5041
5042 if (is_usb_dev && !is_usb_ready(adev, true /* is_playback */)) {
5043 return -ENOSYS;
5044 }
5045
5046 ALOGV("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
5047 __func__, config->format, config->sample_rate, config->channel_mask, devices, flags);
5048
5049 *stream_out = NULL;
5050 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
5051
5052 pthread_mutex_init(&out->compr_mute_lock, (const pthread_mutexattr_t *) NULL);
5053
5054 if (devices == AUDIO_DEVICE_NONE)
5055 devices = AUDIO_DEVICE_OUT_SPEAKER;
5056
5057 out->flags = flags;
5058 out->devices = devices;
5059 out->dev = adev;
5060 out->handle = handle;
5061 out->a2dp_compress_mute = false;
5062 out->mmap_shared_memory_fd = -1; // not open
5063
5064 /* Init use case and pcm_config */
5065 if ((is_hdmi || is_usb_dev) &&
5066 (audio_is_linear_pcm(config->format) || config->format == AUDIO_FORMAT_DEFAULT) &&
5067 (flags == AUDIO_OUTPUT_FLAG_NONE ||
5068 (flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0)) {
5069 audio_format_t req_format = config->format;
5070 audio_channel_mask_t req_channel_mask = config->channel_mask;
5071 uint32_t req_sample_rate = config->sample_rate;
5072
5073 pthread_mutex_lock(&adev->lock);
5074 if (is_hdmi) {
5075 ret = read_hdmi_channel_masks(out);
5076 if (config->sample_rate == 0)
5077 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
5078 if (config->channel_mask == AUDIO_CHANNEL_NONE)
5079 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
5080 if (config->format == AUDIO_FORMAT_DEFAULT)
5081 config->format = AUDIO_FORMAT_PCM_16_BIT;
5082 } else if (is_usb_dev) {
5083 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
5084 &config->format,
5085 &out->supported_formats[0],
5086 MAX_SUPPORTED_FORMATS,
5087 &config->channel_mask,
5088 &out->supported_channel_masks[0],
5089 MAX_SUPPORTED_CHANNEL_MASKS,
5090 &config->sample_rate,
5091 &out->supported_sample_rates[0],
5092 MAX_SUPPORTED_SAMPLE_RATES);
5093 ALOGV("plugged dev USB ret %d", ret);
5094 }
5095 pthread_mutex_unlock(&adev->lock);
5096 if (ret != 0) {
5097 // For MMAP NO IRQ, allow conversions in ADSP
5098 if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0)
5099 goto error_open;
5100
5101 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate)
5102 config->sample_rate = req_sample_rate;
5103 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask)
5104 config->channel_mask = req_channel_mask;
5105 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format)
5106 config->format = req_format;
5107 }
5108
5109 out->sample_rate = config->sample_rate;
5110 out->channel_mask = config->channel_mask;
5111 out->format = config->format;
5112 if (is_hdmi) {
5113 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
5114 out->config = pcm_config_hdmi_multi;
5115 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
5116 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
5117 out->config = pcm_config_mmap_playback;
5118 out->stream.start = out_start;
5119 out->stream.stop = out_stop;
5120 out->stream.create_mmap_buffer = out_create_mmap_buffer;
5121 out->stream.get_mmap_position = out_get_mmap_position;
5122 } else {
5123 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
5124 out->config = pcm_config_hifi;
5125 }
5126
5127 out->config.rate = out->sample_rate;
5128 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
5129 if (is_hdmi) {
5130 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
5131 audio_bytes_per_sample(out->format));
5132 }
5133 out->config.format = pcm_format_from_audio_format(out->format);
5134 } else if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
5135 pthread_mutex_lock(&adev->lock);
5136 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
5137 pthread_mutex_unlock(&adev->lock);
5138
5139 // reject offload during card offline to allow
5140 // fallback to s/w paths
5141 if (offline) {
5142 ret = -ENODEV;
5143 goto error_open;
5144 }
5145
5146 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
5147 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
5148 ALOGE("%s: Unsupported Offload information", __func__);
5149 ret = -EINVAL;
5150 goto error_open;
5151 }
5152 if (!is_supported_format(config->offload_info.format)) {
5153 ALOGE("%s: Unsupported audio format", __func__);
5154 ret = -EINVAL;
5155 goto error_open;
5156 }
5157 out->sample_rate = config->offload_info.sample_rate;
5158 if (config->offload_info.channel_mask != AUDIO_CHANNEL_NONE)
5159 out->channel_mask = config->offload_info.channel_mask;
5160 else if (config->channel_mask != AUDIO_CHANNEL_NONE)
5161 out->channel_mask = config->channel_mask;
5162 else
5163 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
5164
5165 out->format = config->offload_info.format;
5166
5167 out->compr_config.codec = (struct snd_codec *)
5168 calloc(1, sizeof(struct snd_codec));
5169
5170 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
5171
5172 out->stream.set_callback = out_set_callback;
5173 out->stream.pause = out_pause;
5174 out->stream.resume = out_resume;
5175 out->stream.drain = out_drain;
5176 out->stream.flush = out_flush;
5177
5178 out->compr_config.codec->id =
5179 get_snd_codec_id(config->offload_info.format);
5180 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
5181 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
5182 out->compr_config.codec->sample_rate = out->sample_rate;
5183 out->compr_config.codec->bit_rate =
5184 config->offload_info.bit_rate;
5185 out->compr_config.codec->ch_in =
5186 audio_channel_count_from_out_mask(out->channel_mask);
5187 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
5188
5189 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
5190 out->non_blocking = 1;
5191
5192 out->send_new_metadata = 1;
5193
5194 check_and_set_gapless_mode(adev);
5195
5196 create_offload_callback_thread(out);
5197 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
5198 __func__, config->offload_info.version,
5199 config->offload_info.bit_rate);
5200 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
5201 switch (config->sample_rate) {
5202 case 0:
5203 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
5204 break;
5205 case 8000:
5206 case 16000:
5207 case 48000:
5208 out->sample_rate = config->sample_rate;
5209 break;
5210 default:
5211 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
5212 config->sample_rate);
5213 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
5214 ret = -EINVAL;
5215 goto error_open;
5216 }
5217 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
5218 switch (config->channel_mask) {
5219 case AUDIO_CHANNEL_NONE:
5220 case AUDIO_CHANNEL_OUT_STEREO:
5221 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
5222 break;
5223 default:
5224 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
5225 config->channel_mask);
5226 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
5227 ret = -EINVAL;
5228 goto error_open;
5229 }
5230 switch (config->format) {
5231 case AUDIO_FORMAT_DEFAULT:
5232 case AUDIO_FORMAT_PCM_16_BIT:
5233 out->format = AUDIO_FORMAT_PCM_16_BIT;
5234 break;
5235 default:
5236 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
5237 config->format);
5238 config->format = AUDIO_FORMAT_PCM_16_BIT;
5239 ret = -EINVAL;
5240 goto error_open;
5241 }
5242
5243 voice_extn_check_and_set_incall_music_usecase(adev, out);
5244 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
5245 switch (config->sample_rate) {
5246 case 0:
5247 out->sample_rate = AFE_PROXY_SAMPLING_RATE;
5248 break;
5249 case 8000:
5250 case 16000:
5251 case 48000:
5252 out->sample_rate = config->sample_rate;
5253 break;
5254 default:
5255 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
5256 config->sample_rate);
5257 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
5258 ret = -EINVAL;
5259 break;
5260 }
5261 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
5262 switch (config->channel_mask) {
5263 case AUDIO_CHANNEL_NONE:
5264 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
5265 break;
5266 case AUDIO_CHANNEL_OUT_STEREO:
5267 out->channel_mask = config->channel_mask;
5268 break;
5269 default:
5270 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
5271 config->channel_mask);
5272 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
5273 ret = -EINVAL;
5274 break;
5275 }
5276 switch (config->format) {
5277 case AUDIO_FORMAT_DEFAULT:
5278 out->format = AUDIO_FORMAT_PCM_16_BIT;
5279 break;
5280 case AUDIO_FORMAT_PCM_16_BIT:
5281 out->format = config->format;
5282 break;
5283 default:
5284 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
5285 config->format);
5286 config->format = AUDIO_FORMAT_PCM_16_BIT;
5287 ret = -EINVAL;
5288 break;
5289 }
5290 if (ret != 0)
5291 goto error_open;
5292
5293 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
5294 out->config = pcm_config_afe_proxy_playback;
5295 out->config.rate = out->sample_rate;
5296 out->config.channels =
5297 audio_channel_count_from_out_mask(out->channel_mask);
5298 out->config.format = pcm_format_from_audio_format(out->format);
5299 adev->voice_tx_output = out;
5300 } else if (flags == AUDIO_OUTPUT_FLAG_VOIP_RX) {
5301 switch (config->sample_rate) {
5302 case 0:
5303 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
5304 break;
5305 case 8000:
5306 case 16000:
5307 case 32000:
5308 case 48000:
5309 out->sample_rate = config->sample_rate;
5310 break;
5311 default:
5312 ALOGE("%s: Unsupported sampling rate %d for Voip RX", __func__,
5313 config->sample_rate);
5314 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
5315 ret = -EINVAL;
5316 break;
5317 }
5318 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
5319 switch (config->channel_mask) {
5320 case AUDIO_CHANNEL_NONE:
5321 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
5322 break;
5323 case AUDIO_CHANNEL_OUT_STEREO:
5324 out->channel_mask = config->channel_mask;
5325 break;
5326 default:
5327 ALOGE("%s: Unsupported channel mask %#x for Voip RX", __func__,
5328 config->channel_mask);
5329 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
5330 ret = -EINVAL;
5331 break;
5332 }
5333 switch (config->format) {
5334 case AUDIO_FORMAT_DEFAULT:
5335 out->format = AUDIO_FORMAT_PCM_16_BIT;
5336 break;
5337 case AUDIO_FORMAT_PCM_16_BIT:
5338 out->format = config->format;
5339 break;
5340 default:
5341 ALOGE("%s: Unsupported format %#x for Voip RX", __func__,
5342 config->format);
5343 config->format = AUDIO_FORMAT_PCM_16_BIT;
5344 ret = -EINVAL;
5345 break;
5346 }
5347 if (ret != 0)
5348 goto error_open;
5349
5350 uint32_t buffer_size, frame_size;
5351 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
5352 out->config = pcm_config_voip;
5353 out->config.rate = out->sample_rate;
5354 out->config.format = pcm_format_from_audio_format(out->format);
5355 buffer_size = get_stream_buffer_size(VOIP_PLAYBACK_PERIOD_DURATION_MSEC,
5356 out->sample_rate,
5357 out->format,
5358 out->config.channels,
5359 false /*is_low_latency*/);
5360 frame_size = audio_bytes_per_sample(out->format) * out->config.channels;
5361 out->config.period_size = buffer_size / frame_size;
5362 out->config.period_count = VOIP_PLAYBACK_PERIOD_COUNT;
5363 out->af_period_multiplier = 1;
5364 } else {
5365 if (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
5366 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
5367 out->config = pcm_config_deep_buffer;
5368 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
5369 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
5370 out->config = pcm_config_deep_buffer;
5371 } else if (flags & AUDIO_OUTPUT_FLAG_RAW) {
5372 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
5373 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL, out->flags);
5374 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
5375 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
5376 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
5377 out->config = pcm_config_mmap_playback;
5378 out->stream.start = out_start;
5379 out->stream.stop = out_stop;
5380 out->stream.create_mmap_buffer = out_create_mmap_buffer;
5381 out->stream.get_mmap_position = out_get_mmap_position;
5382 } else {
5383 if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
5384 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS;
5385 adev->haptic_pcm_device_id = platform_get_haptics_pcm_device_id();
5386 if (adev->haptic_pcm_device_id < 0) {
5387 ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)",
5388 __func__, adev->haptic_pcm_device_id, out->usecase);
5389 ret = -ENOSYS;
5390 goto error_open;
5391 }
5392 out->config = pcm_config_haptics_audio;
5393 if (force_haptic_path)
5394 adev->haptics_config = pcm_config_haptics_audio;
5395 else
5396 adev->haptics_config = pcm_config_haptics;
5397 } else {
5398 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
5399 out->config = pcm_config_low_latency;
5400 }
5401 }
5402
5403 if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY ||
5404 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
5405 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) {
5406 out->volume_l = 1.0;
5407 out->volume_r = 1.0;
5408 }
5409
5410 if (config->sample_rate == 0) {
5411 out->sample_rate = out->config.rate;
5412 } else {
5413 out->sample_rate = config->sample_rate;
5414 }
5415
5416 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
5417 out->channel_mask = audio_channel_out_mask_from_count(out->config.channels);
5418 } else {
5419 out->channel_mask = config->channel_mask;
5420 }
5421
5422 if (config->format == AUDIO_FORMAT_DEFAULT)
5423 out->format = audio_format_from_pcm_format(out->config.format);
5424 else if (!audio_is_linear_pcm(config->format)) {
5425 config->format = AUDIO_FORMAT_PCM_16_BIT;
5426 ret = -EINVAL;
5427 goto error_open;
5428 } else {
5429 out->format = config->format;
5430 }
5431
5432 out->config.rate = out->sample_rate;
5433
5434 if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
5435 out->config.channels =
5436 audio_channel_count_from_out_mask(out->channel_mask &
5437 ~AUDIO_CHANNEL_HAPTIC_ALL);
5438
5439 if (force_haptic_path) {
5440 out->config.channels = 1;
5441 adev->haptics_config.channels = 1;
5442 } else {
5443 adev->haptics_config.channels =
5444 audio_channel_count_from_out_mask(out->channel_mask &
5445 AUDIO_CHANNEL_HAPTIC_ALL);
5446 }
5447 } else {
5448 out->config.channels =
5449 audio_channel_count_from_out_mask(out->channel_mask);
5450 }
5451
5452 if (out->format != audio_format_from_pcm_format(out->config.format)) {
5453 out->config.format = pcm_format_from_audio_format(out->format);
5454 }
5455 }
5456
5457 if ((config->sample_rate != 0 && config->sample_rate != out->sample_rate) ||
5458 (config->format != AUDIO_FORMAT_DEFAULT && config->format != out->format) ||
5459 (config->channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != out->channel_mask)) {
5460 ALOGI("%s: Unsupported output config. sample_rate:%u format:%#x channel_mask:%#x",
5461 __func__, config->sample_rate, config->format, config->channel_mask);
5462 config->sample_rate = out->sample_rate;
5463 config->format = out->format;
5464 config->channel_mask = out->channel_mask;
5465 ret = -EINVAL;
5466 goto error_open;
5467 }
5468
5469 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
5470 __func__, use_case_table[out->usecase], config->format, out->config.format);
5471
5472 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
5473 if (adev->primary_output == NULL)
5474 adev->primary_output = out;
5475 else {
5476 ALOGE("%s: Primary output is already opened", __func__);
5477 ret = -EEXIST;
5478 goto error_open;
5479 }
5480 }
5481
5482 /* Check if this usecase is already existing */
5483 pthread_mutex_lock(&adev->lock);
5484 if (get_usecase_from_list(adev, out->usecase) != NULL) {
5485 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
5486 pthread_mutex_unlock(&adev->lock);
5487 ret = -EEXIST;
5488 goto error_open;
5489 }
5490 pthread_mutex_unlock(&adev->lock);
5491
5492 out->applied_volume_l = INVALID_OUT_VOLUME;
5493 out->applied_volume_r = INVALID_OUT_VOLUME;
5494
5495 out->stream.common.get_sample_rate = out_get_sample_rate;
5496 out->stream.common.set_sample_rate = out_set_sample_rate;
5497 out->stream.common.get_buffer_size = out_get_buffer_size;
5498 out->stream.common.get_channels = out_get_channels;
5499 out->stream.common.get_format = out_get_format;
5500 out->stream.common.set_format = out_set_format;
5501 out->stream.common.standby = out_standby;
5502 out->stream.common.dump = out_dump;
5503 out->stream.common.set_parameters = out_set_parameters;
5504 out->stream.common.get_parameters = out_get_parameters;
5505 out->stream.common.add_audio_effect = out_add_audio_effect;
5506 out->stream.common.remove_audio_effect = out_remove_audio_effect;
5507 out->stream.get_latency = out_get_latency;
5508 out->stream.set_volume = out_set_volume;
5509 #ifdef NO_AUDIO_OUT
5510 out->stream.write = out_write_for_no_output;
5511 #else
5512 out->stream.write = out_write;
5513 #endif
5514 out->stream.get_render_position = out_get_render_position;
5515 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
5516 out->stream.get_presentation_position = out_get_presentation_position;
5517
5518 if (out->realtime)
5519 out->af_period_multiplier = af_period_multiplier;
5520 else
5521 out->af_period_multiplier = 1;
5522
5523 out->kernel_buffer_size = out->config.period_size * out->config.period_count;
5524
5525 out->standby = 1;
5526 /* out->muted = false; by calloc() */
5527 /* out->written = 0; by calloc() */
5528
5529 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
5530 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
5531 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
5532
5533 config->format = out->stream.common.get_format(&out->stream.common);
5534 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
5535 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
5536
5537 register_format(out->format, out->supported_formats);
5538 register_channel_mask(out->channel_mask, out->supported_channel_masks);
5539 register_sample_rate(out->sample_rate, out->supported_sample_rates);
5540
5541 out->error_log = error_log_create(
5542 ERROR_LOG_ENTRIES,
5543 1000000000 /* aggregate consecutive identical errors within one second in ns */);
5544
5545 /*
5546 By locking output stream before registering, we allow the callback
5547 to update stream's state only after stream's initial state is set to
5548 adev state.
5549 */
5550 lock_output_stream(out);
5551 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
5552 pthread_mutex_lock(&adev->lock);
5553 out->card_status = adev->card_status;
5554 pthread_mutex_unlock(&adev->lock);
5555 pthread_mutex_unlock(&out->lock);
5556
5557 stream_app_type_cfg_init(&out->app_type_cfg);
5558
5559 *stream_out = &out->stream;
5560
5561 ALOGV("%s: exit", __func__);
5562 return 0;
5563
5564 error_open:
5565 free(out);
5566 *stream_out = NULL;
5567 ALOGW("%s: exit: ret %d", __func__, ret);
5568 return ret;
5569 }
5570
adev_close_output_stream(struct audio_hw_device * dev __unused,struct audio_stream_out * stream)5571 static void adev_close_output_stream(struct audio_hw_device *dev __unused,
5572 struct audio_stream_out *stream)
5573 {
5574 struct stream_out *out = (struct stream_out *)stream;
5575 struct audio_device *adev = out->dev;
5576
5577 ALOGV("%s: enter", __func__);
5578
5579 // must deregister from sndmonitor first to prevent races
5580 // between the callback and close_stream
5581 audio_extn_snd_mon_unregister_listener(out);
5582 out_standby(&stream->common);
5583 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
5584 destroy_offload_callback_thread(out);
5585
5586 if (out->compr_config.codec != NULL)
5587 free(out->compr_config.codec);
5588 }
5589
5590 out->a2dp_compress_mute = false;
5591
5592 if (adev->voice_tx_output == out)
5593 adev->voice_tx_output = NULL;
5594
5595 error_log_destroy(out->error_log);
5596 out->error_log = NULL;
5597
5598 pthread_cond_destroy(&out->cond);
5599 pthread_mutex_destroy(&out->pre_lock);
5600 pthread_mutex_destroy(&out->lock);
5601 free(stream);
5602 ALOGV("%s: exit", __func__);
5603 }
5604
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)5605 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
5606 {
5607 struct audio_device *adev = (struct audio_device *)dev;
5608 struct str_parms *parms;
5609 char *str;
5610 char value[32];
5611 int val;
5612 int ret;
5613 int status = 0;
5614 bool a2dp_reconfig = false;
5615
5616 ALOGV("%s: enter: %s", __func__, kvpairs);
5617
5618 pthread_mutex_lock(&adev->lock);
5619
5620 parms = str_parms_create_str(kvpairs);
5621 status = voice_set_parameters(adev, parms);
5622 if (status != 0) {
5623 goto done;
5624 }
5625
5626 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
5627 if (ret >= 0) {
5628 /* When set to false, HAL should disable EC and NS */
5629 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
5630 adev->bluetooth_nrec = true;
5631 else
5632 adev->bluetooth_nrec = false;
5633 }
5634
5635 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
5636 if (ret >= 0) {
5637 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
5638 adev->screen_off = false;
5639 else
5640 adev->screen_off = true;
5641 }
5642
5643 ret = str_parms_get_int(parms, "rotation", &val);
5644 if (ret >= 0) {
5645 bool reverse_speakers = false;
5646 int camera_rotation = CAMERA_ROTATION_LANDSCAPE;
5647 switch (val) {
5648 // FIXME: note that the code below assumes that the speakers are in the correct placement
5649 // relative to the user when the device is rotated 90deg from its default rotation. This
5650 // assumption is device-specific, not platform-specific like this code.
5651 case 270:
5652 reverse_speakers = true;
5653 camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE;
5654 break;
5655 case 0:
5656 case 180:
5657 camera_rotation = CAMERA_ROTATION_PORTRAIT;
5658 break;
5659 case 90:
5660 camera_rotation = CAMERA_ROTATION_LANDSCAPE;
5661 break;
5662 default:
5663 ALOGE("%s: unexpected rotation of %d", __func__, val);
5664 status = -EINVAL;
5665 }
5666 if (status == 0) {
5667 // check and set swap
5668 // - check if orientation changed and speaker active
5669 // - set rotation and cache the rotation value
5670 adev->camera_orientation =
5671 (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation;
5672 #ifndef MAXXAUDIO_QDSP_ENABLED
5673 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
5674 #endif
5675 }
5676 }
5677
5678 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
5679 if (ret >= 0) {
5680 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
5681 }
5682
5683 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
5684 if (ret >= 0) {
5685 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
5686 adev->bt_sco_on = true;
5687 else
5688 adev->bt_sco_on = false;
5689 }
5690
5691 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
5692 if (ret >= 0) {
5693 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
5694 if (audio_is_usb_out_device(device)) {
5695 ret = str_parms_get_str(parms, "card", value, sizeof(value));
5696 if (ret >= 0) {
5697 const int card = atoi(value);
5698 audio_extn_usb_add_device(device, card);
5699 }
5700 } else if (audio_is_usb_in_device(device)) {
5701 ret = str_parms_get_str(parms, "card", value, sizeof(value));
5702 if (ret >= 0) {
5703 const int card = atoi(value);
5704 audio_extn_usb_add_device(device, card);
5705 }
5706 }
5707 }
5708
5709 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
5710 if (ret >= 0) {
5711 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
5712 if (audio_is_usb_out_device(device)) {
5713 ret = str_parms_get_str(parms, "card", value, sizeof(value));
5714 if (ret >= 0) {
5715 const int card = atoi(value);
5716 audio_extn_usb_remove_device(device, card);
5717 }
5718 } else if (audio_is_usb_in_device(device)) {
5719 ret = str_parms_get_str(parms, "card", value, sizeof(value));
5720 if (ret >= 0) {
5721 const int card = atoi(value);
5722 audio_extn_usb_remove_device(device, card);
5723 }
5724 }
5725 }
5726
5727 audio_extn_hfp_set_parameters(adev, parms);
5728 audio_extn_ma_set_parameters(adev, parms);
5729
5730 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig);
5731 if (status >= 0 && a2dp_reconfig) {
5732 struct audio_usecase *usecase;
5733 struct listnode *node;
5734 list_for_each(node, &adev->usecase_list) {
5735 usecase = node_to_item(node, struct audio_usecase, list);
5736 if ((usecase->type == PCM_PLAYBACK) &&
5737 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
5738 ALOGD("%s: reconfigure A2DP... forcing device switch", __func__);
5739
5740 pthread_mutex_unlock(&adev->lock);
5741 lock_output_stream(usecase->stream.out);
5742 pthread_mutex_lock(&adev->lock);
5743 audio_extn_a2dp_set_handoff_mode(true);
5744 // force device switch to reconfigure encoder
5745 select_devices(adev, usecase->id);
5746 audio_extn_a2dp_set_handoff_mode(false);
5747 pthread_mutex_unlock(&usecase->stream.out->lock);
5748 break;
5749 }
5750 }
5751 }
5752
5753 //FIXME: to be replaced by proper video capture properties API
5754 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value));
5755 if (ret >= 0) {
5756 int camera_facing = CAMERA_FACING_BACK;
5757 if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0)
5758 camera_facing = CAMERA_FACING_FRONT;
5759 else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0)
5760 camera_facing = CAMERA_FACING_BACK;
5761 else {
5762 ALOGW("%s: invalid camera facing value: %s", __func__, value);
5763 goto done;
5764 }
5765 adev->camera_orientation =
5766 (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing;
5767 struct audio_usecase *usecase;
5768 struct listnode *node;
5769 list_for_each(node, &adev->usecase_list) {
5770 usecase = node_to_item(node, struct audio_usecase, list);
5771 struct stream_in *in = usecase->stream.in;
5772 if (usecase->type == PCM_CAPTURE && in != NULL &&
5773 in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) {
5774 select_devices(adev, in->usecase);
5775 }
5776 }
5777 }
5778
5779 done:
5780 str_parms_destroy(parms);
5781 pthread_mutex_unlock(&adev->lock);
5782 ALOGV("%s: exit with code(%d)", __func__, status);
5783 return status;
5784 }
5785
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)5786 static char* adev_get_parameters(const struct audio_hw_device *dev,
5787 const char *keys)
5788 {
5789 struct audio_device *adev = (struct audio_device *)dev;
5790 struct str_parms *reply = str_parms_create();
5791 struct str_parms *query = str_parms_create_str(keys);
5792 char *str;
5793
5794 pthread_mutex_lock(&adev->lock);
5795
5796 voice_get_parameters(adev, query, reply);
5797 audio_extn_a2dp_get_parameters(query, reply);
5798
5799 str = str_parms_to_str(reply);
5800 str_parms_destroy(query);
5801 str_parms_destroy(reply);
5802
5803 pthread_mutex_unlock(&adev->lock);
5804 ALOGV("%s: exit: returns - %s", __func__, str);
5805 return str;
5806 }
5807
adev_init_check(const struct audio_hw_device * dev __unused)5808 static int adev_init_check(const struct audio_hw_device *dev __unused)
5809 {
5810 return 0;
5811 }
5812
adev_set_voice_volume(struct audio_hw_device * dev,float volume)5813 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
5814 {
5815 int ret;
5816 struct audio_device *adev = (struct audio_device *)dev;
5817
5818 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
5819
5820 pthread_mutex_lock(&adev->lock);
5821 ret = voice_set_volume(adev, volume);
5822 pthread_mutex_unlock(&adev->lock);
5823
5824 return ret;
5825 }
5826
adev_set_master_volume(struct audio_hw_device * dev __unused,float volume __unused)5827 static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
5828 {
5829 return -ENOSYS;
5830 }
5831
adev_get_master_volume(struct audio_hw_device * dev __unused,float * volume __unused)5832 static int adev_get_master_volume(struct audio_hw_device *dev __unused,
5833 float *volume __unused)
5834 {
5835 return -ENOSYS;
5836 }
5837
adev_set_master_mute(struct audio_hw_device * dev __unused,bool muted __unused)5838 static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused)
5839 {
5840 return -ENOSYS;
5841 }
5842
adev_get_master_mute(struct audio_hw_device * dev __unused,bool * muted __unused)5843 static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
5844 {
5845 return -ENOSYS;
5846 }
5847
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)5848 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
5849 {
5850 struct audio_device *adev = (struct audio_device *)dev;
5851
5852 pthread_mutex_lock(&adev->lock);
5853 if (adev->mode != mode) {
5854 ALOGD("%s: mode %d", __func__, (int)mode);
5855 adev->mode = mode;
5856 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) &&
5857 voice_is_in_call(adev)) {
5858 voice_stop_call(adev);
5859 adev->current_call_output = NULL;
5860
5861 /*
5862 * After stopping the call, it must check if any active capture
5863 * activity device needs to be re-selected.
5864 */
5865 struct audio_usecase *usecase;
5866 struct listnode *node;
5867 list_for_each(node, &adev->usecase_list) {
5868 usecase = node_to_item(node, struct audio_usecase, list);
5869 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL) {
5870 select_devices_with_force_switch(adev, usecase->id, true);
5871 }
5872 }
5873 }
5874 }
5875 pthread_mutex_unlock(&adev->lock);
5876
5877 audio_extn_extspk_set_mode(adev->extspk, mode);
5878
5879 return 0;
5880 }
5881
adev_set_mic_mute(struct audio_hw_device * dev,bool state)5882 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
5883 {
5884 int ret;
5885 struct audio_device *adev = (struct audio_device *)dev;
5886
5887 ALOGD("%s: state %d", __func__, (int)state);
5888 pthread_mutex_lock(&adev->lock);
5889 if (audio_extn_tfa_98xx_is_supported() && adev->enable_hfp) {
5890 ret = audio_extn_hfp_set_mic_mute(adev, state);
5891 } else {
5892 ret = voice_set_mic_mute(adev, state);
5893 }
5894 adev->mic_muted = state;
5895 pthread_mutex_unlock(&adev->lock);
5896
5897 return ret;
5898 }
5899
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)5900 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
5901 {
5902 *state = voice_get_mic_mute((struct audio_device *)dev);
5903 return 0;
5904 }
5905
adev_get_input_buffer_size(const struct audio_hw_device * dev __unused,const struct audio_config * config)5906 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
5907 const struct audio_config *config)
5908 {
5909 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
5910
5911 /* Don't know if USB HIFI in this context so use true to be conservative */
5912 if (check_input_parameters(config->sample_rate, config->format, channel_count,
5913 true /*is_usb_hifi */) != 0)
5914 return 0;
5915
5916 return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
5917 config->sample_rate, config->format,
5918 channel_count,
5919 false /* is_low_latency: since we don't know, be conservative */);
5920 }
5921
adev_input_allow_hifi_record(struct audio_device * adev,audio_devices_t devices,audio_input_flags_t flags,audio_source_t source)5922 static bool adev_input_allow_hifi_record(struct audio_device *adev,
5923 audio_devices_t devices,
5924 audio_input_flags_t flags,
5925 audio_source_t source) {
5926 const bool allowed = true;
5927
5928 if (!audio_is_usb_in_device(devices))
5929 return !allowed;
5930
5931 switch (flags) {
5932 case AUDIO_INPUT_FLAG_NONE:
5933 case AUDIO_INPUT_FLAG_FAST: // just fast, not fast|raw || fast|mmap
5934 break;
5935 default:
5936 return !allowed;
5937 }
5938
5939 switch (source) {
5940 case AUDIO_SOURCE_DEFAULT:
5941 case AUDIO_SOURCE_MIC:
5942 case AUDIO_SOURCE_UNPROCESSED:
5943 break;
5944 default:
5945 return !allowed;
5946 }
5947
5948 switch (adev->mode) {
5949 case 0:
5950 break;
5951 default:
5952 return !allowed;
5953 }
5954
5955 return allowed;
5956 }
5957
adev_open_input_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,struct audio_stream_in ** stream_in,audio_input_flags_t flags,const char * address __unused,audio_source_t source)5958 static int adev_open_input_stream(struct audio_hw_device *dev,
5959 audio_io_handle_t handle,
5960 audio_devices_t devices,
5961 struct audio_config *config,
5962 struct audio_stream_in **stream_in,
5963 audio_input_flags_t flags,
5964 const char *address __unused,
5965 audio_source_t source )
5966 {
5967 struct audio_device *adev = (struct audio_device *)dev;
5968 struct stream_in *in;
5969 int ret = 0, buffer_size, frame_size;
5970 int channel_count;
5971 bool is_low_latency = false;
5972 bool is_usb_dev = audio_is_usb_in_device(devices);
5973 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
5974 devices,
5975 flags,
5976 source);
5977 ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d,"
5978 " sample_rate %u, channel_mask %#x, format %#x",
5979 __func__, flags, is_usb_dev, may_use_hifi_record,
5980 config->sample_rate, config->channel_mask, config->format);
5981 *stream_in = NULL;
5982
5983 if (is_usb_dev && !is_usb_ready(adev, false /* is_playback */)) {
5984 return -ENOSYS;
5985 }
5986
5987 if (!(is_usb_dev && may_use_hifi_record)) {
5988 if (config->sample_rate == 0)
5989 config->sample_rate = DEFAULT_INPUT_SAMPLING_RATE;
5990 if (config->channel_mask == AUDIO_CHANNEL_NONE)
5991 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
5992 if (config->format == AUDIO_FORMAT_DEFAULT)
5993 config->format = AUDIO_FORMAT_PCM_16_BIT;
5994
5995 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
5996
5997 if (check_input_parameters(config->sample_rate, config->format, channel_count, false) != 0)
5998 return -EINVAL;
5999 }
6000
6001 if (audio_extn_tfa_98xx_is_supported() &&
6002 (audio_extn_hfp_is_active(adev) || voice_is_in_call(adev)))
6003 return -EINVAL;
6004
6005 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
6006
6007 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
6008 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
6009
6010 in->stream.common.get_sample_rate = in_get_sample_rate;
6011 in->stream.common.set_sample_rate = in_set_sample_rate;
6012 in->stream.common.get_buffer_size = in_get_buffer_size;
6013 in->stream.common.get_channels = in_get_channels;
6014 in->stream.common.get_format = in_get_format;
6015 in->stream.common.set_format = in_set_format;
6016 in->stream.common.standby = in_standby;
6017 in->stream.common.dump = in_dump;
6018 in->stream.common.set_parameters = in_set_parameters;
6019 in->stream.common.get_parameters = in_get_parameters;
6020 in->stream.common.add_audio_effect = in_add_audio_effect;
6021 in->stream.common.remove_audio_effect = in_remove_audio_effect;
6022 in->stream.set_gain = in_set_gain;
6023 in->stream.read = in_read;
6024 in->stream.get_input_frames_lost = in_get_input_frames_lost;
6025 in->stream.get_capture_position = in_get_capture_position;
6026 in->stream.get_active_microphones = in_get_active_microphones;
6027 in->stream.set_microphone_direction = in_set_microphone_direction;
6028 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
6029 in->stream.update_sink_metadata = in_update_sink_metadata;
6030
6031 in->device = devices;
6032 in->source = source;
6033 in->dev = adev;
6034 in->standby = 1;
6035 in->capture_handle = handle;
6036 in->flags = flags;
6037 in->direction = MIC_DIRECTION_UNSPECIFIED;
6038 in->zoom = 0;
6039 in->mmap_shared_memory_fd = -1; // not open
6040 list_init(&in->aec_list);
6041 list_init(&in->ns_list);
6042
6043 ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask);
6044 if (source == AUDIO_SOURCE_VOICE_UPLINK ||
6045 source == AUDIO_SOURCE_VOICE_DOWNLINK) {
6046 /* Force channel config requested to mono if incall
6047 record is being requested for only uplink/downlink */
6048 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
6049 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
6050 ret = -EINVAL;
6051 goto err_open;
6052 }
6053 }
6054
6055 if (is_usb_dev && may_use_hifi_record) {
6056 /* HiFi record selects an appropriate format, channel, rate combo
6057 depending on sink capabilities*/
6058 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
6059 &config->format,
6060 &in->supported_formats[0],
6061 MAX_SUPPORTED_FORMATS,
6062 &config->channel_mask,
6063 &in->supported_channel_masks[0],
6064 MAX_SUPPORTED_CHANNEL_MASKS,
6065 &config->sample_rate,
6066 &in->supported_sample_rates[0],
6067 MAX_SUPPORTED_SAMPLE_RATES);
6068 if (ret != 0) {
6069 ret = -EINVAL;
6070 goto err_open;
6071 }
6072 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
6073 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
6074 config->format = AUDIO_FORMAT_PCM_16_BIT;
6075 } else if (config->format == AUDIO_FORMAT_PCM_FLOAT ||
6076 config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
6077 config->format == AUDIO_FORMAT_PCM_8_24_BIT) {
6078 bool ret_error = false;
6079 /* 24 bit is restricted to UNPROCESSED source only,also format supported
6080 from HAL is 8_24
6081 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
6082 8_24 return error indicating supported format is 8_24
6083 *> In case of any other source requesting 24 bit or float return error
6084 indicating format supported is 16 bit only.
6085
6086 on error flinger will retry with supported format passed
6087 */
6088 if (!is_supported_24bits_audiosource(source)) {
6089 config->format = AUDIO_FORMAT_PCM_16_BIT;
6090 ret_error = true;
6091 } else if (config->format != AUDIO_FORMAT_PCM_8_24_BIT) {
6092 config->format = AUDIO_FORMAT_PCM_8_24_BIT;
6093 ret_error = true;
6094 }
6095
6096 if (ret_error) {
6097 ret = -EINVAL;
6098 goto err_open;
6099 }
6100 }
6101
6102 in->format = config->format;
6103 in->channel_mask = config->channel_mask;
6104
6105 /* Update config params with the requested sample rate and channels */
6106 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
6107 if (config->sample_rate == 0)
6108 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
6109 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
6110 config->sample_rate != 8000) {
6111 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
6112 ret = -EINVAL;
6113 goto err_open;
6114 }
6115
6116 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
6117 config->format = AUDIO_FORMAT_PCM_16_BIT;
6118 ret = -EINVAL;
6119 goto err_open;
6120 }
6121
6122 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
6123 in->config = pcm_config_afe_proxy_record;
6124 in->af_period_multiplier = 1;
6125 } else if (is_usb_dev && may_use_hifi_record) {
6126 in->usecase = USECASE_AUDIO_RECORD_HIFI;
6127 in->config = pcm_config_audio_capture;
6128 frame_size = audio_stream_in_frame_size(&in->stream);
6129 buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
6130 config->sample_rate,
6131 config->format,
6132 channel_count,
6133 false /*is_low_latency*/);
6134 in->config.period_size = buffer_size / frame_size;
6135 in->config.rate = config->sample_rate;
6136 in->af_period_multiplier = 1;
6137 in->config.format = pcm_format_from_audio_format(config->format);
6138 } else {
6139 in->usecase = USECASE_AUDIO_RECORD;
6140 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
6141 (in->flags & AUDIO_INPUT_FLAG_FAST) != 0) {
6142 is_low_latency = true;
6143 #if LOW_LATENCY_CAPTURE_USE_CASE
6144 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
6145 #endif
6146 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
6147 if (!in->realtime) {
6148 in->config = pcm_config_audio_capture;
6149 frame_size = audio_stream_in_frame_size(&in->stream);
6150 buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
6151 config->sample_rate,
6152 config->format,
6153 channel_count,
6154 is_low_latency);
6155 in->config.period_size = buffer_size / frame_size;
6156 in->config.rate = config->sample_rate;
6157 in->af_period_multiplier = 1;
6158 } else {
6159 // period size is left untouched for rt mode playback
6160 in->config = pcm_config_audio_capture_rt;
6161 in->af_period_multiplier = af_period_multiplier;
6162 }
6163 } else if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) &&
6164 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
6165 // FIXME: Add support for multichannel capture over USB using MMAP
6166 in->usecase = USECASE_AUDIO_RECORD_MMAP;
6167 in->config = pcm_config_mmap_capture;
6168 in->stream.start = in_start;
6169 in->stream.stop = in_stop;
6170 in->stream.create_mmap_buffer = in_create_mmap_buffer;
6171 in->stream.get_mmap_position = in_get_mmap_position;
6172 in->af_period_multiplier = 1;
6173 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
6174 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
6175 in->flags & AUDIO_INPUT_FLAG_VOIP_TX &&
6176 (config->sample_rate == 8000 ||
6177 config->sample_rate == 16000 ||
6178 config->sample_rate == 32000 ||
6179 config->sample_rate == 48000) &&
6180 channel_count == 1) {
6181 in->usecase = USECASE_AUDIO_RECORD_VOIP;
6182 in->config = pcm_config_audio_capture;
6183 frame_size = audio_stream_in_frame_size(&in->stream);
6184 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC,
6185 config->sample_rate,
6186 config->format,
6187 channel_count, false /*is_low_latency*/);
6188 in->config.period_size = buffer_size / frame_size;
6189 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT;
6190 in->config.rate = config->sample_rate;
6191 in->af_period_multiplier = 1;
6192 } else {
6193 in->config = pcm_config_audio_capture;
6194 frame_size = audio_stream_in_frame_size(&in->stream);
6195 buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
6196 config->sample_rate,
6197 config->format,
6198 channel_count,
6199 is_low_latency);
6200 in->config.period_size = buffer_size / frame_size;
6201 in->config.rate = config->sample_rate;
6202 in->af_period_multiplier = 1;
6203 }
6204 if (config->format == AUDIO_FORMAT_PCM_8_24_BIT)
6205 in->config.format = PCM_FORMAT_S24_LE;
6206 }
6207
6208 in->config.channels = channel_count;
6209 in->sample_rate = in->config.rate;
6210
6211
6212 register_format(in->format, in->supported_formats);
6213 register_channel_mask(in->channel_mask, in->supported_channel_masks);
6214 register_sample_rate(in->sample_rate, in->supported_sample_rates);
6215
6216 in->error_log = error_log_create(
6217 ERROR_LOG_ENTRIES,
6218 NANOS_PER_SECOND /* aggregate consecutive identical errors within one second */);
6219
6220 /* This stream could be for sound trigger lab,
6221 get sound trigger pcm if present */
6222 audio_extn_sound_trigger_check_and_get_session(in);
6223
6224 if (in->is_st_session)
6225 in->flags |= AUDIO_INPUT_FLAG_HW_HOTWORD;
6226
6227 lock_input_stream(in);
6228 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
6229 pthread_mutex_lock(&adev->lock);
6230 in->card_status = adev->card_status;
6231 pthread_mutex_unlock(&adev->lock);
6232 pthread_mutex_unlock(&in->lock);
6233
6234 stream_app_type_cfg_init(&in->app_type_cfg);
6235
6236 *stream_in = &in->stream;
6237 ALOGV("%s: exit", __func__);
6238 return 0;
6239
6240 err_open:
6241 free(in);
6242 *stream_in = NULL;
6243 return ret;
6244 }
6245
adev_close_input_stream(struct audio_hw_device * dev __unused,struct audio_stream_in * stream)6246 static void adev_close_input_stream(struct audio_hw_device *dev __unused,
6247 struct audio_stream_in *stream)
6248 {
6249 struct stream_in *in = (struct stream_in *)stream;
6250 ALOGV("%s", __func__);
6251
6252 // must deregister from sndmonitor first to prevent races
6253 // between the callback and close_stream
6254 audio_extn_snd_mon_unregister_listener(stream);
6255 in_standby(&stream->common);
6256
6257 error_log_destroy(in->error_log);
6258 in->error_log = NULL;
6259
6260 pthread_mutex_destroy(&in->pre_lock);
6261 pthread_mutex_destroy(&in->lock);
6262
6263 free(stream);
6264
6265 return;
6266 }
6267
adev_dump(const audio_hw_device_t * device __unused,int fd __unused)6268 static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
6269 {
6270 return 0;
6271 }
6272
6273 /* verifies input and output devices and their capabilities.
6274 *
6275 * This verification is required when enabling extended bit-depth or
6276 * sampling rates, as not all qcom products support it.
6277 *
6278 * Suitable for calling only on initialization such as adev_open().
6279 * It fills the audio_device use_case_table[] array.
6280 *
6281 * Has a side-effect that it needs to configure audio routing / devices
6282 * in order to power up the devices and read the device parameters.
6283 * It does not acquire any hw device lock. Should restore the devices
6284 * back to "normal state" upon completion.
6285 */
adev_verify_devices(struct audio_device * adev)6286 static int adev_verify_devices(struct audio_device *adev)
6287 {
6288 /* enumeration is a bit difficult because one really wants to pull
6289 * the use_case, device id, etc from the hidden pcm_device_table[].
6290 * In this case there are the following use cases and device ids.
6291 *
6292 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
6293 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
6294 * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
6295 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
6296 * [USECASE_AUDIO_RECORD] = {0, 0},
6297 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
6298 * [USECASE_VOICE_CALL] = {2, 2},
6299 *
6300 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted.
6301 * USECASE_VOICE_CALL omitted, but possible for either input or output.
6302 */
6303
6304 /* should be the usecases enabled in adev_open_input_stream() */
6305 static const int test_in_usecases[] = {
6306 USECASE_AUDIO_RECORD,
6307 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
6308 };
6309 /* should be the usecases enabled in adev_open_output_stream()*/
6310 static const int test_out_usecases[] = {
6311 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
6312 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
6313 };
6314 static const usecase_type_t usecase_type_by_dir[] = {
6315 PCM_PLAYBACK,
6316 PCM_CAPTURE,
6317 };
6318 static const unsigned flags_by_dir[] = {
6319 PCM_OUT,
6320 PCM_IN,
6321 };
6322
6323 size_t i;
6324 unsigned dir;
6325 const unsigned card_id = adev->snd_card;
6326 char info[512]; /* for possible debug info */
6327
6328 for (dir = 0; dir < 2; ++dir) {
6329 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
6330 const unsigned flags_dir = flags_by_dir[dir];
6331 const size_t testsize =
6332 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
6333 const int *testcases =
6334 dir ? test_in_usecases : test_out_usecases;
6335 const audio_devices_t audio_device =
6336 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
6337
6338 for (i = 0; i < testsize; ++i) {
6339 const audio_usecase_t audio_usecase = testcases[i];
6340 int device_id;
6341 snd_device_t snd_device;
6342 struct pcm_params **pparams;
6343 struct stream_out out;
6344 struct stream_in in;
6345 struct audio_usecase uc_info;
6346 int retval;
6347
6348 pparams = &adev->use_case_table[audio_usecase];
6349 pcm_params_free(*pparams); /* can accept null input */
6350 *pparams = NULL;
6351
6352 /* find the device ID for the use case (signed, for error) */
6353 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
6354 if (device_id < 0)
6355 continue;
6356
6357 /* prepare structures for device probing */
6358 memset(&uc_info, 0, sizeof(uc_info));
6359 uc_info.id = audio_usecase;
6360 uc_info.type = usecase_type;
6361 if (dir) {
6362 memset(&in, 0, sizeof(in));
6363 in.device = audio_device;
6364 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
6365 uc_info.stream.in = ∈
6366 }
6367 memset(&out, 0, sizeof(out));
6368 out.devices = audio_device; /* only field needed in select_devices */
6369 uc_info.stream.out = &out;
6370 uc_info.devices = audio_device;
6371 uc_info.in_snd_device = SND_DEVICE_NONE;
6372 uc_info.out_snd_device = SND_DEVICE_NONE;
6373 list_add_tail(&adev->usecase_list, &uc_info.list);
6374
6375 /* select device - similar to start_(in/out)put_stream() */
6376 retval = select_devices(adev, audio_usecase);
6377 if (retval >= 0) {
6378 *pparams = pcm_params_get(card_id, device_id, flags_dir);
6379 #if LOG_NDEBUG == 0
6380 if (*pparams) {
6381 ALOGV("%s: (%s) card %d device %d", __func__,
6382 dir ? "input" : "output", card_id, device_id);
6383 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
6384 } else {
6385 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
6386 }
6387 #endif
6388 }
6389
6390 /* deselect device - similar to stop_(in/out)put_stream() */
6391 /* 1. Get and set stream specific mixer controls */
6392 retval = disable_audio_route(adev, &uc_info);
6393 /* 2. Disable the rx device */
6394 retval = disable_snd_device(adev,
6395 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
6396 list_remove(&uc_info.list);
6397 }
6398 }
6399 return 0;
6400 }
6401
adev_close(hw_device_t * device)6402 static int adev_close(hw_device_t *device)
6403 {
6404 size_t i;
6405
6406 pthread_mutex_lock(&adev_init_lock);
6407 if (!device || ((struct audio_device *)device != adev))
6408 goto done;
6409
6410 if ((--audio_device_ref_count) == 0) {
6411 audio_extn_snd_mon_unregister_listener(adev);
6412 audio_extn_tfa_98xx_deinit();
6413 audio_extn_ma_deinit();
6414 audio_route_free(adev->audio_route);
6415 free(adev->snd_dev_ref_cnt);
6416 platform_deinit(adev->platform);
6417 audio_extn_extspk_deinit(adev->extspk);
6418 audio_extn_sound_trigger_deinit(adev);
6419 audio_extn_snd_mon_deinit();
6420 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
6421 pcm_params_free(adev->use_case_table[i]);
6422 }
6423 if (adev->adm_deinit)
6424 adev->adm_deinit(adev->adm_data);
6425 pthread_mutex_destroy(&adev->lock);
6426 free(device);
6427 adev = NULL;
6428 }
6429
6430 done:
6431 pthread_mutex_unlock(&adev_init_lock);
6432 return 0;
6433 }
6434
6435 /* This returns 1 if the input parameter looks at all plausible as a low latency period size,
6436 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
6437 * just that it _might_ work.
6438 */
period_size_is_plausible_for_low_latency(int period_size)6439 static int period_size_is_plausible_for_low_latency(int period_size)
6440 {
6441 switch (period_size) {
6442 case 48:
6443 case 96:
6444 case 144:
6445 case 160:
6446 case 192:
6447 case 240:
6448 case 320:
6449 case 480:
6450 return 1;
6451 default:
6452 return 0;
6453 }
6454 }
6455
adev_snd_mon_cb(void * stream __unused,struct str_parms * parms)6456 static void adev_snd_mon_cb(void * stream __unused, struct str_parms * parms)
6457 {
6458 int card;
6459 card_status_t status;
6460
6461 if (!parms)
6462 return;
6463
6464 if (parse_snd_card_status(parms, &card, &status) < 0)
6465 return;
6466
6467 pthread_mutex_lock(&adev->lock);
6468 bool valid_cb = (card == adev->snd_card);
6469 if (valid_cb) {
6470 if (adev->card_status != status) {
6471 adev->card_status = status;
6472 platform_snd_card_update(adev->platform, status);
6473 }
6474 }
6475 pthread_mutex_unlock(&adev->lock);
6476 return;
6477 }
6478
6479 /* out and adev lock held */
check_a2dp_restore_l(struct audio_device * adev,struct stream_out * out,bool restore)6480 static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
6481 {
6482 struct audio_usecase *uc_info;
6483 float left_p;
6484 float right_p;
6485 audio_devices_t devices;
6486
6487 uc_info = get_usecase_from_list(adev, out->usecase);
6488 if (uc_info == NULL) {
6489 ALOGE("%s: Could not find the usecase (%d) in the list",
6490 __func__, out->usecase);
6491 return -EINVAL;
6492 }
6493
6494 ALOGD("%s: enter: usecase(%d: %s)", __func__,
6495 out->usecase, use_case_table[out->usecase]);
6496
6497 if (restore) {
6498 // restore A2DP device for active usecases and unmute if required
6499 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
6500 !is_a2dp_device(uc_info->out_snd_device)) {
6501 ALOGD("%s: restoring A2DP and unmuting stream", __func__);
6502 select_devices(adev, uc_info->id);
6503 pthread_mutex_lock(&out->compr_mute_lock);
6504 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
6505 (out->a2dp_compress_mute)) {
6506 out->a2dp_compress_mute = false;
6507 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
6508 }
6509 pthread_mutex_unlock(&out->compr_mute_lock);
6510 }
6511 } else {
6512 // mute compress stream if suspended
6513 pthread_mutex_lock(&out->compr_mute_lock);
6514 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
6515 (!out->a2dp_compress_mute)) {
6516 if (!out->standby) {
6517 ALOGD("%s: selecting speaker and muting stream", __func__);
6518 devices = out->devices;
6519 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
6520 left_p = out->volume_l;
6521 right_p = out->volume_r;
6522 if (out->offload_state == OFFLOAD_STATE_PLAYING)
6523 compress_pause(out->compr);
6524 out_set_compr_volume(&out->stream, 0.0f, 0.0f);
6525 out->a2dp_compress_mute = true;
6526 select_devices(adev, out->usecase);
6527 if (out->offload_state == OFFLOAD_STATE_PLAYING)
6528 compress_resume(out->compr);
6529 out->devices = devices;
6530 out->volume_l = left_p;
6531 out->volume_r = right_p;
6532 }
6533 }
6534 pthread_mutex_unlock(&out->compr_mute_lock);
6535 }
6536 ALOGV("%s: exit", __func__);
6537 return 0;
6538 }
6539
check_a2dp_restore(struct audio_device * adev,struct stream_out * out,bool restore)6540 int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore)
6541 {
6542 int ret = 0;
6543
6544 lock_output_stream(out);
6545 pthread_mutex_lock(&adev->lock);
6546
6547 ret = check_a2dp_restore_l(adev, out, restore);
6548
6549 pthread_mutex_unlock(&adev->lock);
6550 pthread_mutex_unlock(&out->lock);
6551 return ret;
6552 }
6553
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)6554 static int adev_open(const hw_module_t *module, const char *name,
6555 hw_device_t **device)
6556 {
6557 int i, ret;
6558
6559 ALOGD("%s: enter", __func__);
6560 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
6561 pthread_mutex_lock(&adev_init_lock);
6562 if (audio_device_ref_count != 0) {
6563 *device = &adev->device.common;
6564 audio_device_ref_count++;
6565 ALOGV("%s: returning existing instance of adev", __func__);
6566 ALOGV("%s: exit", __func__);
6567 pthread_mutex_unlock(&adev_init_lock);
6568 return 0;
6569 }
6570 adev = calloc(1, sizeof(struct audio_device));
6571
6572 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
6573
6574 adev->device.common.tag = HARDWARE_DEVICE_TAG;
6575 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
6576 adev->device.common.module = (struct hw_module_t *)module;
6577 adev->device.common.close = adev_close;
6578
6579 adev->device.init_check = adev_init_check;
6580 adev->device.set_voice_volume = adev_set_voice_volume;
6581 adev->device.set_master_volume = adev_set_master_volume;
6582 adev->device.get_master_volume = adev_get_master_volume;
6583 adev->device.set_master_mute = adev_set_master_mute;
6584 adev->device.get_master_mute = adev_get_master_mute;
6585 adev->device.set_mode = adev_set_mode;
6586 adev->device.set_mic_mute = adev_set_mic_mute;
6587 adev->device.get_mic_mute = adev_get_mic_mute;
6588 adev->device.set_parameters = adev_set_parameters;
6589 adev->device.get_parameters = adev_get_parameters;
6590 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
6591 adev->device.open_output_stream = adev_open_output_stream;
6592 adev->device.close_output_stream = adev_close_output_stream;
6593 adev->device.open_input_stream = adev_open_input_stream;
6594
6595 adev->device.close_input_stream = adev_close_input_stream;
6596 adev->device.dump = adev_dump;
6597 adev->device.get_microphones = adev_get_microphones;
6598
6599 /* Set the default route before the PCM stream is opened */
6600 pthread_mutex_lock(&adev->lock);
6601 adev->mode = AUDIO_MODE_NORMAL;
6602 adev->primary_output = NULL;
6603 adev->bluetooth_nrec = true;
6604 adev->acdb_settings = TTY_MODE_OFF;
6605 /* adev->cur_hdmi_channels = 0; by calloc() */
6606 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
6607 voice_init(adev);
6608 list_init(&adev->usecase_list);
6609 pthread_mutex_unlock(&adev->lock);
6610
6611 /* Loads platform specific libraries dynamically */
6612 adev->platform = platform_init(adev);
6613 if (!adev->platform) {
6614 free(adev->snd_dev_ref_cnt);
6615 free(adev);
6616 ALOGE("%s: Failed to init platform data, aborting.", __func__);
6617 *device = NULL;
6618 pthread_mutex_unlock(&adev_init_lock);
6619 return -EINVAL;
6620 }
6621 adev->extspk = audio_extn_extspk_init(adev);
6622
6623 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
6624 if (adev->visualizer_lib == NULL) {
6625 ALOGW("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
6626 } else {
6627 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
6628 adev->visualizer_start_output =
6629 (int (*)(audio_io_handle_t, int, int, int))dlsym(adev->visualizer_lib,
6630 "visualizer_hal_start_output");
6631 adev->visualizer_stop_output =
6632 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
6633 "visualizer_hal_stop_output");
6634 }
6635
6636 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
6637 if (adev->offload_effects_lib == NULL) {
6638 ALOGW("%s: DLOPEN failed for %s", __func__,
6639 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
6640 } else {
6641 ALOGV("%s: DLOPEN successful for %s", __func__,
6642 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
6643 adev->offload_effects_start_output =
6644 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
6645 "offload_effects_bundle_hal_start_output");
6646 adev->offload_effects_stop_output =
6647 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
6648 "offload_effects_bundle_hal_stop_output");
6649 }
6650
6651 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
6652 if (adev->adm_lib == NULL) {
6653 ALOGW("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
6654 } else {
6655 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
6656 adev->adm_init = (adm_init_t)
6657 dlsym(adev->adm_lib, "adm_init");
6658 adev->adm_deinit = (adm_deinit_t)
6659 dlsym(adev->adm_lib, "adm_deinit");
6660 adev->adm_register_input_stream = (adm_register_input_stream_t)
6661 dlsym(adev->adm_lib, "adm_register_input_stream");
6662 adev->adm_register_output_stream = (adm_register_output_stream_t)
6663 dlsym(adev->adm_lib, "adm_register_output_stream");
6664 adev->adm_deregister_stream = (adm_deregister_stream_t)
6665 dlsym(adev->adm_lib, "adm_deregister_stream");
6666 adev->adm_request_focus = (adm_request_focus_t)
6667 dlsym(adev->adm_lib, "adm_request_focus");
6668 adev->adm_abandon_focus = (adm_abandon_focus_t)
6669 dlsym(adev->adm_lib, "adm_abandon_focus");
6670 adev->adm_set_config = (adm_set_config_t)
6671 dlsym(adev->adm_lib, "adm_set_config");
6672 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
6673 dlsym(adev->adm_lib, "adm_request_focus_v2");
6674 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
6675 dlsym(adev->adm_lib, "adm_is_noirq_avail");
6676 adev->adm_on_routing_change = (adm_on_routing_change_t)
6677 dlsym(adev->adm_lib, "adm_on_routing_change");
6678 }
6679
6680 adev->bt_wb_speech_enabled = false;
6681 adev->enable_voicerx = false;
6682
6683 *device = &adev->device.common;
6684
6685 if (k_enable_extended_precision)
6686 adev_verify_devices(adev);
6687
6688 char value[PROPERTY_VALUE_MAX];
6689 int trial;
6690 if ((property_get("vendor.audio_hal.period_size", value, NULL) > 0) ||
6691 (property_get("audio_hal.period_size", value, NULL) > 0)) {
6692 trial = atoi(value);
6693 if (period_size_is_plausible_for_low_latency(trial)) {
6694 pcm_config_low_latency.period_size = trial;
6695 pcm_config_low_latency.start_threshold = trial / 4;
6696 pcm_config_low_latency.avail_min = trial / 4;
6697 configured_low_latency_capture_period_size = trial;
6698 }
6699 }
6700 if ((property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) ||
6701 (property_get("audio_hal.in_period_size", value, NULL) > 0)) {
6702 trial = atoi(value);
6703 if (period_size_is_plausible_for_low_latency(trial)) {
6704 configured_low_latency_capture_period_size = trial;
6705 }
6706 }
6707
6708 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
6709
6710 adev->camera_orientation = CAMERA_DEFAULT;
6711
6712 // commented as full set of app type cfg is sent from platform
6713 // audio_extn_utils_send_default_app_type_cfg(adev->platform, adev->mixer);
6714 audio_device_ref_count++;
6715
6716 if ((property_get("vendor.audio_hal.period_multiplier", value, NULL) > 0) ||
6717 (property_get("audio_hal.period_multiplier", value, NULL) > 0)) {
6718 af_period_multiplier = atoi(value);
6719 if (af_period_multiplier < 0) {
6720 af_period_multiplier = 2;
6721 } else if (af_period_multiplier > 4) {
6722 af_period_multiplier = 4;
6723 }
6724 ALOGV("new period_multiplier = %d", af_period_multiplier);
6725 }
6726
6727 audio_extn_tfa_98xx_init(adev);
6728 audio_extn_ma_init(adev->platform);
6729 audio_extn_audiozoom_init();
6730
6731 pthread_mutex_unlock(&adev_init_lock);
6732
6733 if (adev->adm_init)
6734 adev->adm_data = adev->adm_init();
6735
6736 audio_extn_perf_lock_init();
6737 audio_extn_snd_mon_init();
6738 pthread_mutex_lock(&adev->lock);
6739 audio_extn_snd_mon_register_listener(NULL, adev_snd_mon_cb);
6740 adev->card_status = CARD_STATUS_ONLINE;
6741 pthread_mutex_unlock(&adev->lock);
6742 audio_extn_sound_trigger_init(adev);/* dependent on snd_mon_init() */
6743
6744 ALOGD("%s: exit", __func__);
6745 return 0;
6746 }
6747
6748 static struct hw_module_methods_t hal_module_methods = {
6749 .open = adev_open,
6750 };
6751
6752 struct audio_module HAL_MODULE_INFO_SYM = {
6753 .common = {
6754 .tag = HARDWARE_MODULE_TAG,
6755 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
6756 .hal_api_version = HARDWARE_HAL_API_VERSION,
6757 .id = AUDIO_HARDWARE_MODULE_ID,
6758 .name = "QCOM Audio HAL",
6759 .author = "Code Aurora Forum",
6760 .methods = &hal_module_methods,
6761 },
6762 };
6763