1 /*
2 * Copyright (C) 2015 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 LOG_NDEBUG 0*/
19 /*#define VERY_VERY_VERBOSE_LOGGING*/
20 #ifdef VERY_VERY_VERBOSE_LOGGING
21 #define ALOGVV ALOGV
22 #else
23 #define ALOGVV(a...) do { } while(0)
24 #endif
25
26 #include <errno.h>
27 #include <pthread.h>
28 #include <stdint.h>
29 #include <sys/time.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #include <dlfcn.h>
33 #include <sys/resource.h>
34 #include <sys/prctl.h>
35
36 #include <cutils/log.h>
37 #include <cutils/str_parms.h>
38 #include <cutils/properties.h>
39 #include <cutils/atomic.h>
40 #include <cutils/sched_policy.h>
41
42 #include <hardware/audio_effect.h>
43 #include <system/thread_defs.h>
44 #include <audio_effects/effect_aec.h>
45 #include <audio_effects/effect_ns.h>
46 #include <audio_utils/channels.h>
47 #include "audio_hw.h"
48 #include "cras_dsp.h"
49
50 /* TODO: the following PCM device profiles could be read from a config file */
51 struct pcm_device_profile pcm_device_playback_hs = {
52 .config = {
53 .channels = PLAYBACK_DEFAULT_CHANNEL_COUNT,
54 .rate = PLAYBACK_DEFAULT_SAMPLING_RATE,
55 .period_size = PLAYBACK_PERIOD_SIZE,
56 .period_count = PLAYBACK_PERIOD_COUNT,
57 .format = PCM_FORMAT_S16_LE,
58 .start_threshold = PLAYBACK_START_THRESHOLD,
59 .stop_threshold = PLAYBACK_STOP_THRESHOLD,
60 .silence_threshold = 0,
61 .avail_min = PLAYBACK_AVAILABLE_MIN,
62 },
63 .card = SOUND_CARD,
64 .id = 1,
65 .device = 0,
66 .type = PCM_PLAYBACK,
67 .devices = AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
68 .dsp_name = "invert_lr",
69 };
70
71 struct pcm_device_profile pcm_device_capture = {
72 .config = {
73 .channels = CAPTURE_DEFAULT_CHANNEL_COUNT,
74 .rate = CAPTURE_DEFAULT_SAMPLING_RATE,
75 .period_size = CAPTURE_PERIOD_SIZE,
76 .period_count = CAPTURE_PERIOD_COUNT,
77 .format = PCM_FORMAT_S16_LE,
78 .start_threshold = CAPTURE_START_THRESHOLD,
79 .stop_threshold = 0,
80 .silence_threshold = 0,
81 .avail_min = 0,
82 },
83 .card = SOUND_CARD,
84 .id = 2,
85 .device = 0,
86 .type = PCM_CAPTURE,
87 .devices = AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_BACK_MIC,
88 };
89
90 struct pcm_device_profile pcm_device_capture_loopback_aec = {
91 .config = {
92 .channels = CAPTURE_DEFAULT_CHANNEL_COUNT,
93 .rate = CAPTURE_DEFAULT_SAMPLING_RATE,
94 .period_size = CAPTURE_PERIOD_SIZE,
95 .period_count = CAPTURE_PERIOD_COUNT,
96 .format = PCM_FORMAT_S16_LE,
97 .start_threshold = CAPTURE_START_THRESHOLD,
98 .stop_threshold = 0,
99 .silence_threshold = 0,
100 .avail_min = 0,
101 },
102 .card = SOUND_CARD,
103 .id = 3,
104 .device = 1,
105 .type = PCM_CAPTURE,
106 .devices = SND_DEVICE_IN_LOOPBACK_AEC,
107 };
108
109 struct pcm_device_profile pcm_device_playback_spk_and_headset = {
110 .config = {
111 .channels = PLAYBACK_DEFAULT_CHANNEL_COUNT,
112 .rate = PLAYBACK_DEFAULT_SAMPLING_RATE,
113 .period_size = PLAYBACK_PERIOD_SIZE,
114 .period_count = PLAYBACK_PERIOD_COUNT,
115 .format = PCM_FORMAT_S16_LE,
116 .start_threshold = PLAYBACK_START_THRESHOLD,
117 .stop_threshold = PLAYBACK_STOP_THRESHOLD,
118 .silence_threshold = 0,
119 .avail_min = PLAYBACK_AVAILABLE_MIN,
120 },
121 .card = SOUND_CARD,
122 .id = 4,
123 .device = 0,
124 .type = PCM_PLAYBACK,
125 .devices = AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
126 .dsp_name = "speaker_eq",
127 };
128
129 struct pcm_device_profile pcm_device_playback_spk = {
130 .config = {
131 .channels = PLAYBACK_DEFAULT_CHANNEL_COUNT,
132 .rate = PLAYBACK_DEFAULT_SAMPLING_RATE,
133 .period_size = PLAYBACK_PERIOD_SIZE,
134 .period_count = PLAYBACK_PERIOD_COUNT,
135 .format = PCM_FORMAT_S16_LE,
136 .start_threshold = PLAYBACK_START_THRESHOLD,
137 .stop_threshold = PLAYBACK_STOP_THRESHOLD,
138 .silence_threshold = 0,
139 .avail_min = PLAYBACK_AVAILABLE_MIN,
140 },
141 .card = SOUND_CARD,
142 .id = 5,
143 .device = 0,
144 .type = PCM_PLAYBACK,
145 .devices = AUDIO_DEVICE_OUT_SPEAKER,
146 .dsp_name = "speaker_eq",
147 };
148
149 static struct pcm_device_profile pcm_device_hotword_streaming = {
150 .config = {
151 .channels = 1,
152 .rate = 16000,
153 .period_size = CAPTURE_PERIOD_SIZE,
154 .period_count = CAPTURE_PERIOD_COUNT,
155 .format = PCM_FORMAT_S16_LE,
156 .start_threshold = CAPTURE_START_THRESHOLD,
157 .stop_threshold = 0,
158 .silence_threshold = 0,
159 .avail_min = 0,
160 },
161 .card = SOUND_CARD,
162 .id = 0,
163 .type = PCM_HOTWORD_STREAMING,
164 .devices = AUDIO_DEVICE_IN_BUILTIN_MIC |
165 AUDIO_DEVICE_IN_WIRED_HEADSET |
166 AUDIO_DEVICE_IN_BACK_MIC,
167 };
168
169 struct pcm_device_profile *pcm_devices[] = {
170 &pcm_device_playback_hs,
171 &pcm_device_capture,
172 &pcm_device_playback_spk,
173 &pcm_device_capture_loopback_aec,
174 &pcm_device_playback_spk_and_headset,
175 &pcm_device_hotword_streaming,
176 NULL,
177 };
178
179 static const char * const use_case_table[AUDIO_USECASE_MAX] = {
180 [USECASE_AUDIO_PLAYBACK] = "playback",
181 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "playback multi-channel",
182 [USECASE_AUDIO_CAPTURE] = "capture",
183 [USECASE_AUDIO_CAPTURE_HOTWORD] = "capture-hotword",
184 [USECASE_VOICE_CALL] = "voice-call",
185 };
186
187
188 #define STRING_TO_ENUM(string) { #string, string }
189
190 struct pcm_config pcm_config_deep_buffer = {
191 .channels = 2,
192 .rate = DEEP_BUFFER_OUTPUT_SAMPLING_RATE,
193 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
194 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
195 .format = PCM_FORMAT_S16_LE,
196 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
197 .stop_threshold = INT_MAX,
198 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
199 };
200
201 struct string_to_enum {
202 const char *name;
203 uint32_t value;
204 };
205
206 static const struct string_to_enum out_channels_name_to_enum_table[] = {
207 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
208 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
209 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
210 };
211
is_supported_format(audio_format_t format)212 static bool is_supported_format(audio_format_t format)
213 {
214 if (format == AUDIO_FORMAT_MP3 ||
215 ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC))
216 return true;
217
218 return false;
219 }
220
get_snd_codec_id(audio_format_t format)221 static int get_snd_codec_id(audio_format_t format)
222 {
223 int id = 0;
224
225 switch (format & AUDIO_FORMAT_MAIN_MASK) {
226 default:
227 ALOGE("%s: Unsupported audio format", __func__);
228 }
229
230 return id;
231 }
232
233 /* Array to store sound devices */
234 static const char * const device_table[SND_DEVICE_MAX] = {
235 [SND_DEVICE_NONE] = "none",
236 /* Playback sound devices */
237 [SND_DEVICE_OUT_HANDSET] = "handset",
238 [SND_DEVICE_OUT_SPEAKER] = "speaker",
239 [SND_DEVICE_OUT_HEADPHONES] = "headphones",
240 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
241 [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset",
242 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
243 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
244 [SND_DEVICE_OUT_HDMI] = "hdmi",
245 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
246 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
247 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
248 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
249
250 /* Capture sound devices */
251 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
252 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
253 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
254 [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
255 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic",
256 [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
257 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
258 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
259 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
260 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
261 [SND_DEVICE_IN_VOICE_DMIC_1] = "voice-dmic-1",
262 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_1] = "voice-speaker-dmic-1",
263 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
264 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
265 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
266 [SND_DEVICE_IN_VOICE_REC_HEADSET_MIC] = "voice-rec-headset-mic",
267 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
268 [SND_DEVICE_IN_VOICE_REC_DMIC_1] = "voice-rec-dmic-1",
269 [SND_DEVICE_IN_VOICE_REC_DMIC_NS_1] = "voice-rec-dmic-ns-1",
270 [SND_DEVICE_IN_LOOPBACK_AEC] = "loopback-aec",
271 };
272
adev_get_mixer_for_card(struct audio_device * adev,int card)273 struct mixer_card *adev_get_mixer_for_card(struct audio_device *adev, int card)
274 {
275 struct mixer_card *mixer_card;
276 struct listnode *node;
277
278 list_for_each(node, &adev->mixer_list) {
279 mixer_card = node_to_item(node, struct mixer_card, adev_list_node);
280 if (mixer_card->card == card)
281 return mixer_card;
282 }
283 return NULL;
284 }
285
uc_get_mixer_for_card(struct audio_usecase * usecase,int card)286 struct mixer_card *uc_get_mixer_for_card(struct audio_usecase *usecase, int card)
287 {
288 struct mixer_card *mixer_card;
289 struct listnode *node;
290
291 list_for_each(node, &usecase->mixer_list) {
292 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[usecase->id]);
293 if (mixer_card->card == card)
294 return mixer_card;
295 }
296 return NULL;
297 }
298
free_mixer_list(struct audio_device * adev)299 void free_mixer_list(struct audio_device *adev)
300 {
301 struct mixer_card *mixer_card;
302 struct listnode *node;
303 struct listnode *next;
304
305 list_for_each_safe(node, next, &adev->mixer_list) {
306 mixer_card = node_to_item(node, struct mixer_card, adev_list_node);
307 list_remove(node);
308 audio_route_free(mixer_card->audio_route);
309 free(mixer_card);
310 }
311 }
312
mixer_init(struct audio_device * adev)313 int mixer_init(struct audio_device *adev)
314 {
315 int i;
316 int card;
317 int retry_num;
318 struct mixer *mixer;
319 struct audio_route *audio_route;
320 char mixer_path[PATH_MAX];
321 struct mixer_card *mixer_card;
322 struct listnode *node;
323
324 list_init(&adev->mixer_list);
325
326 for (i = 0; pcm_devices[i] != NULL; i++) {
327 card = pcm_devices[i]->card;
328 if (adev_get_mixer_for_card(adev, card) == NULL) {
329 retry_num = 0;
330 do {
331 mixer = mixer_open(card);
332 if (mixer == NULL) {
333 if (++retry_num > RETRY_NUMBER) {
334 ALOGE("%s unable to open the mixer for--card %d, aborting.",
335 __func__, card);
336 goto error;
337 }
338 usleep(RETRY_US);
339 }
340 } while (mixer == NULL);
341
342 sprintf(mixer_path, "/system/etc/mixer_paths_%d.xml", card);
343 audio_route = audio_route_init(card, mixer_path);
344 if (!audio_route) {
345 ALOGE("%s: Failed to init audio route controls for card %d, aborting.",
346 __func__, card);
347 goto error;
348 }
349 mixer_card = calloc(1, sizeof(struct mixer_card));
350 mixer_card->card = card;
351 mixer_card->mixer = mixer;
352 mixer_card->audio_route = audio_route;
353 list_add_tail(&adev->mixer_list, &mixer_card->adev_list_node);
354 }
355 }
356
357 return 0;
358
359 error:
360 free_mixer_list(adev);
361 return -ENODEV;
362 }
363
get_snd_device_name(snd_device_t snd_device)364 const char *get_snd_device_name(snd_device_t snd_device)
365 {
366 const char *name = NULL;
367
368 if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX)
369 name = device_table[snd_device];
370
371 ALOGE_IF(name == NULL, "%s: invalid snd device %d", __func__, snd_device);
372
373 return name;
374 }
375
get_snd_device_display_name(snd_device_t snd_device)376 const char *get_snd_device_display_name(snd_device_t snd_device)
377 {
378 const char *name = get_snd_device_name(snd_device);
379
380 if (name == NULL)
381 name = "SND DEVICE NOT FOUND";
382
383 return name;
384 }
385
get_pcm_device(usecase_type_t uc_type,audio_devices_t devices)386 struct pcm_device_profile *get_pcm_device(usecase_type_t uc_type, audio_devices_t devices)
387 {
388 int i;
389
390 devices &= ~AUDIO_DEVICE_BIT_IN;
391
392 if (!devices)
393 return NULL;
394
395 for (i = 0; pcm_devices[i] != NULL; i++) {
396 if ((pcm_devices[i]->type == uc_type) &&
397 (devices & pcm_devices[i]->devices) == devices)
398 return pcm_devices[i];
399 }
400
401 return NULL;
402 }
403
get_usecase_from_id(struct audio_device * adev,audio_usecase_t uc_id)404 static struct audio_usecase *get_usecase_from_id(struct audio_device *adev,
405 audio_usecase_t uc_id)
406 {
407 struct audio_usecase *usecase;
408 struct listnode *node;
409
410 list_for_each(node, &adev->usecase_list) {
411 usecase = node_to_item(node, struct audio_usecase, adev_list_node);
412 if (usecase->id == uc_id)
413 return usecase;
414 }
415 return NULL;
416 }
417
get_usecase_from_type(struct audio_device * adev,usecase_type_t type)418 static struct audio_usecase *get_usecase_from_type(struct audio_device *adev,
419 usecase_type_t type)
420 {
421 struct audio_usecase *usecase;
422 struct listnode *node;
423
424 list_for_each(node, &adev->usecase_list) {
425 usecase = node_to_item(node, struct audio_usecase, adev_list_node);
426 if (usecase->type & type)
427 return usecase;
428 }
429 return NULL;
430 }
431
432 /* always called with adev lock held */
set_voice_volume_l(struct audio_device * adev,float volume)433 static int set_voice_volume_l(struct audio_device *adev, float volume)
434 {
435 int err = 0;
436 (void)volume;
437
438 if (adev->mode == AUDIO_MODE_IN_CALL) {
439 /* TODO */
440 }
441 return err;
442 }
443
444
get_output_snd_device(struct audio_device * adev,audio_devices_t devices)445 snd_device_t get_output_snd_device(struct audio_device *adev, audio_devices_t devices)
446 {
447
448 audio_mode_t mode = adev->mode;
449 snd_device_t snd_device = SND_DEVICE_NONE;
450
451 ALOGV("%s: enter: output devices(%#x), mode(%d)", __func__, devices, mode);
452 if (devices == AUDIO_DEVICE_NONE ||
453 devices & AUDIO_DEVICE_BIT_IN) {
454 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
455 goto exit;
456 }
457
458 if (mode == AUDIO_MODE_IN_CALL) {
459 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
460 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
461 if (adev->tty_mode == TTY_MODE_FULL)
462 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
463 else if (adev->tty_mode == TTY_MODE_VCO)
464 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
465 else if (adev->tty_mode == TTY_MODE_HCO)
466 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
467 else
468 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
469 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
470 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
471 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
472 snd_device = SND_DEVICE_OUT_HANDSET;
473 }
474 if (snd_device != SND_DEVICE_NONE) {
475 goto exit;
476 }
477 }
478
479 if (popcount(devices) == 2) {
480 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
481 AUDIO_DEVICE_OUT_SPEAKER)) {
482 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
483 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
484 AUDIO_DEVICE_OUT_SPEAKER)) {
485 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
486 } else {
487 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
488 goto exit;
489 }
490 if (snd_device != SND_DEVICE_NONE) {
491 goto exit;
492 }
493 }
494
495 if (popcount(devices) != 1) {
496 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
497 goto exit;
498 }
499
500 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
501 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
502 snd_device = SND_DEVICE_OUT_HEADPHONES;
503 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
504 snd_device = SND_DEVICE_OUT_SPEAKER;
505 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
506 snd_device = SND_DEVICE_OUT_HANDSET;
507 } else {
508 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
509 }
510 exit:
511 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
512 return snd_device;
513 }
514
get_input_snd_device(struct audio_device * adev,audio_devices_t out_device)515 snd_device_t get_input_snd_device(struct audio_device *adev, audio_devices_t out_device)
516 {
517 audio_source_t source;
518 audio_mode_t mode = adev->mode;
519 audio_devices_t in_device;
520 audio_channel_mask_t channel_mask;
521 snd_device_t snd_device = SND_DEVICE_NONE;
522 struct stream_in *active_input = NULL;
523 struct audio_usecase *usecase;
524
525 usecase = get_usecase_from_type(adev, PCM_CAPTURE|VOICE_CALL);
526 if (usecase != NULL) {
527 active_input = (struct stream_in *)usecase->stream;
528 }
529 source = (active_input == NULL) ?
530 AUDIO_SOURCE_DEFAULT : active_input->source;
531
532 in_device = ((active_input == NULL) ?
533 AUDIO_DEVICE_NONE : active_input->devices)
534 & ~AUDIO_DEVICE_BIT_IN;
535 channel_mask = (active_input == NULL) ?
536 AUDIO_CHANNEL_IN_MONO : active_input->main_channels;
537
538 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
539 __func__, out_device, in_device);
540 if (mode == AUDIO_MODE_IN_CALL) {
541 if (out_device == AUDIO_DEVICE_NONE) {
542 ALOGE("%s: No output device set for voice call", __func__);
543 goto exit;
544 }
545 if (adev->tty_mode != TTY_MODE_OFF) {
546 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
547 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
548 switch (adev->tty_mode) {
549 case TTY_MODE_FULL:
550 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
551 break;
552 case TTY_MODE_VCO:
553 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
554 break;
555 case TTY_MODE_HCO:
556 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
557 break;
558 default:
559 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
560 }
561 goto exit;
562 }
563 }
564 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
565 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
566 snd_device = SND_DEVICE_IN_HANDSET_MIC;
567 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
568 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
569 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
570 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
571 }
572 } else if (source == AUDIO_SOURCE_CAMCORDER) {
573 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
574 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
575 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
576 }
577 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
578 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
579 if (adev->dualmic_config == DUALMIC_CONFIG_1) {
580 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
581 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_1;
582 else if (adev->ns_in_voice_rec)
583 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_NS_1;
584 }
585
586 if (snd_device == SND_DEVICE_NONE) {
587 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
588 }
589 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
590 snd_device = SND_DEVICE_IN_VOICE_REC_HEADSET_MIC;
591 }
592 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION || source == AUDIO_SOURCE_MIC) {
593 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
594 in_device = AUDIO_DEVICE_IN_BACK_MIC;
595 if (active_input) {
596 if (active_input->enable_aec) {
597 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
598 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
599 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
600 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
601 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
602 } else {
603 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
604 }
605 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
606 snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
607 }
608 }
609 /* TODO: set echo reference */
610 }
611 } else if (source == AUDIO_SOURCE_DEFAULT) {
612 goto exit;
613 }
614
615
616 if (snd_device != SND_DEVICE_NONE) {
617 goto exit;
618 }
619
620 if (in_device != AUDIO_DEVICE_NONE &&
621 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
622 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
623 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
624 snd_device = SND_DEVICE_IN_HANDSET_MIC;
625 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
626 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
627 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
628 snd_device = SND_DEVICE_IN_HEADSET_MIC;
629 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
630 snd_device = SND_DEVICE_IN_HDMI_MIC;
631 } else {
632 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
633 ALOGW("%s: Using default handset-mic", __func__);
634 snd_device = SND_DEVICE_IN_HANDSET_MIC;
635 }
636 } else {
637 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
638 snd_device = SND_DEVICE_IN_HANDSET_MIC;
639 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
640 snd_device = SND_DEVICE_IN_HEADSET_MIC;
641 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
642 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
643 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
644 snd_device = SND_DEVICE_IN_HANDSET_MIC;
645 } else {
646 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
647 ALOGW("%s: Using default handset-mic", __func__);
648 snd_device = SND_DEVICE_IN_HANDSET_MIC;
649 }
650 }
651 exit:
652 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
653 return snd_device;
654 }
655
set_hdmi_channels(struct audio_device * adev,int channel_count)656 int set_hdmi_channels(struct audio_device *adev, int channel_count)
657 {
658 struct mixer_ctl *ctl;
659 const char *mixer_ctl_name = "";
660 (void)adev;
661 (void)channel_count;
662 /* TODO */
663
664 return 0;
665 }
666
edid_get_max_channels(struct audio_device * adev)667 int edid_get_max_channels(struct audio_device *adev)
668 {
669 int max_channels = 2;
670 struct mixer_ctl *ctl;
671 (void)adev;
672
673 /* TODO */
674 return max_channels;
675 }
676
677 /* Delay in Us */
render_latency(audio_usecase_t usecase)678 int64_t render_latency(audio_usecase_t usecase)
679 {
680 (void)usecase;
681 /* TODO */
682 return 0;
683 }
684
enable_snd_device(struct audio_device * adev,struct audio_usecase * uc_info,snd_device_t snd_device,bool update_mixer)685 static int enable_snd_device(struct audio_device *adev,
686 struct audio_usecase *uc_info,
687 snd_device_t snd_device,
688 bool update_mixer)
689 {
690 struct mixer_card *mixer_card;
691 struct listnode *node;
692 const char *snd_device_name = get_snd_device_name(snd_device);
693
694 if (snd_device_name == NULL)
695 return -EINVAL;
696
697 adev->snd_dev_ref_cnt[snd_device]++;
698 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
699 ALOGV("%s: snd_device(%d: %s) is already active",
700 __func__, snd_device, snd_device_name);
701 return 0;
702 }
703
704 ALOGV("%s: snd_device(%d: %s)", __func__,
705 snd_device, snd_device_name);
706
707 list_for_each(node, &uc_info->mixer_list) {
708 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[uc_info->id]);
709 audio_route_apply_path(mixer_card->audio_route, snd_device_name);
710 if (update_mixer)
711 audio_route_update_mixer(mixer_card->audio_route);
712 }
713
714 return 0;
715 }
716
disable_snd_device(struct audio_device * adev,struct audio_usecase * uc_info,snd_device_t snd_device,bool update_mixer)717 static int disable_snd_device(struct audio_device *adev,
718 struct audio_usecase *uc_info,
719 snd_device_t snd_device,
720 bool update_mixer)
721 {
722 struct mixer_card *mixer_card;
723 struct listnode *node;
724 const char *snd_device_name = get_snd_device_name(snd_device);
725
726 if (snd_device_name == NULL)
727 return -EINVAL;
728
729 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
730 ALOGE("%s: device ref cnt is already 0", __func__);
731 return -EINVAL;
732 }
733 adev->snd_dev_ref_cnt[snd_device]--;
734 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
735 ALOGV("%s: snd_device(%d: %s)", __func__,
736 snd_device, snd_device_name);
737 list_for_each(node, &uc_info->mixer_list) {
738 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[uc_info->id]);
739 audio_route_reset_path(mixer_card->audio_route, snd_device_name);
740 if (update_mixer)
741 audio_route_update_mixer(mixer_card->audio_route);
742 }
743 }
744 return 0;
745 }
746
select_devices(struct audio_device * adev,audio_usecase_t uc_id)747 static int select_devices(struct audio_device *adev,
748 audio_usecase_t uc_id)
749 {
750 snd_device_t out_snd_device = SND_DEVICE_NONE;
751 snd_device_t in_snd_device = SND_DEVICE_NONE;
752 struct audio_usecase *usecase = NULL;
753 struct audio_usecase *vc_usecase = NULL;
754 struct listnode *node;
755 struct stream_in *active_input = NULL;
756 struct stream_out *active_out;
757 struct mixer_card *mixer_card;
758
759 ALOGV("%s: usecase(%d)", __func__, uc_id);
760
761 if (uc_id == USECASE_AUDIO_CAPTURE_HOTWORD)
762 return 0;
763
764 usecase = get_usecase_from_type(adev, PCM_CAPTURE|VOICE_CALL);
765 if (usecase != NULL) {
766 active_input = (struct stream_in *)usecase->stream;
767 }
768
769 usecase = get_usecase_from_id(adev, uc_id);
770 if (usecase == NULL) {
771 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
772 return -EINVAL;
773 }
774 active_out = (struct stream_out *)usecase->stream;
775
776 if (usecase->type == VOICE_CALL) {
777 out_snd_device = get_output_snd_device(adev, active_out->devices);
778 in_snd_device = get_input_snd_device(adev, active_out->devices);
779 usecase->devices = active_out->devices;
780 } else {
781 /*
782 * If the voice call is active, use the sound devices of voice call usecase
783 * so that it would not result any device switch. All the usecases will
784 * be switched to new device when select_devices() is called for voice call
785 * usecase.
786 */
787 if (adev->in_call) {
788 vc_usecase = get_usecase_from_id(adev, USECASE_VOICE_CALL);
789 if (usecase == NULL) {
790 ALOGE("%s: Could not find the voice call usecase", __func__);
791 } else {
792 in_snd_device = vc_usecase->in_snd_device;
793 out_snd_device = vc_usecase->out_snd_device;
794 }
795 }
796 if (usecase->type == PCM_PLAYBACK) {
797 usecase->devices = active_out->devices;
798 in_snd_device = SND_DEVICE_NONE;
799 if (out_snd_device == SND_DEVICE_NONE) {
800 out_snd_device = get_output_snd_device(adev, active_out->devices);
801 if (active_out == adev->primary_output &&
802 active_input &&
803 active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
804 select_devices(adev, active_input->usecase);
805 }
806 }
807 } else if (usecase->type == PCM_CAPTURE) {
808 usecase->devices = ((struct stream_in *)usecase->stream)->devices;
809 out_snd_device = SND_DEVICE_NONE;
810 if (in_snd_device == SND_DEVICE_NONE) {
811 if (active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
812 adev->primary_output && !adev->primary_output->standby) {
813 in_snd_device = get_input_snd_device(adev, adev->primary_output->devices);
814 } else {
815 in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE);
816 }
817 }
818 }
819 }
820
821 if (out_snd_device == usecase->out_snd_device &&
822 in_snd_device == usecase->in_snd_device) {
823 return 0;
824 }
825
826 ALOGV("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
827 out_snd_device, get_snd_device_display_name(out_snd_device),
828 in_snd_device, get_snd_device_display_name(in_snd_device));
829
830
831 /* Disable current sound devices */
832 if (usecase->out_snd_device != SND_DEVICE_NONE) {
833 disable_snd_device(adev, usecase, usecase->out_snd_device, false);
834 }
835
836 if (usecase->in_snd_device != SND_DEVICE_NONE) {
837 disable_snd_device(adev, usecase, usecase->in_snd_device, false);
838 }
839
840 /* Enable new sound devices */
841 if (out_snd_device != SND_DEVICE_NONE) {
842 enable_snd_device(adev, usecase, out_snd_device, false);
843 }
844
845 if (in_snd_device != SND_DEVICE_NONE) {
846 enable_snd_device(adev, usecase, in_snd_device, false);
847 }
848
849 list_for_each(node, &usecase->mixer_list) {
850 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[usecase->id]);
851 audio_route_update_mixer(mixer_card->audio_route);
852 }
853
854 usecase->in_snd_device = in_snd_device;
855 usecase->out_snd_device = out_snd_device;
856
857 return 0;
858 }
859
860 static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames);
861 static int do_in_standby_l(struct stream_in *in);
862 static audio_format_t in_get_format(const struct audio_stream *stream);
863
864 #ifdef PREPROCESSING_ENABLED
get_command_status(int status,int fct_status,uint32_t cmd_status)865 static int get_command_status(int status, int fct_status, uint32_t cmd_status) {
866 if (fct_status != 0)
867 status = fct_status;
868 else if (cmd_status != 0)
869 status = cmd_status;
870 return status;
871 }
872
in_get_aux_channels(struct stream_in * in)873 static uint32_t in_get_aux_channels(struct stream_in *in)
874 {
875 if (in->num_preprocessors == 0)
876 return 0;
877
878 /* do not enable quad mic configurations when capturing from other
879 * microphones than main */
880 if (!(in->devices & AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN))
881 return 0;
882
883 return AUDIO_CHANNEL_INDEX_MASK_4;
884 }
885
in_configure_effect_channels(effect_handle_t effect,channel_config_t * channel_config)886 static int in_configure_effect_channels(effect_handle_t effect,
887 channel_config_t *channel_config)
888 {
889 int status = 0;
890 int fct_status;
891 int32_t cmd_status;
892 uint32_t reply_size;
893 effect_config_t config;
894 uint32_t cmd[(sizeof(uint32_t) + sizeof(channel_config_t) - 1) / sizeof(uint32_t) + 1];
895
896 ALOGV("in_configure_effect_channels(): configure effect with channels: [%04x][%04x]",
897 channel_config->main_channels,
898 channel_config->aux_channels);
899
900 config.inputCfg.mask = EFFECT_CONFIG_CHANNELS;
901 config.outputCfg.mask = EFFECT_CONFIG_CHANNELS;
902 reply_size = sizeof(effect_config_t);
903 fct_status = (*effect)->command(effect,
904 EFFECT_CMD_GET_CONFIG,
905 0,
906 NULL,
907 &reply_size,
908 &config);
909 if (fct_status != 0) {
910 ALOGE("in_configure_effect_channels(): EFFECT_CMD_GET_CONFIG failed");
911 return fct_status;
912 }
913
914 config.inputCfg.channels = channel_config->aux_channels;
915 config.outputCfg.channels = config.inputCfg.channels;
916 reply_size = sizeof(uint32_t);
917 fct_status = (*effect)->command(effect,
918 EFFECT_CMD_SET_CONFIG,
919 sizeof(effect_config_t),
920 &config,
921 &reply_size,
922 &cmd_status);
923 status = get_command_status(status, fct_status, cmd_status);
924 if (status != 0) {
925 ALOGE("in_configure_effect_channels(): EFFECT_CMD_SET_CONFIG failed");
926 return status;
927 }
928
929 /* some implementations need to be re-enabled after a config change */
930 reply_size = sizeof(uint32_t);
931 fct_status = (*effect)->command(effect,
932 EFFECT_CMD_ENABLE,
933 0,
934 NULL,
935 &reply_size,
936 &cmd_status);
937 status = get_command_status(status, fct_status, cmd_status);
938 if (status != 0) {
939 ALOGE("in_configure_effect_channels(): EFFECT_CMD_ENABLE failed");
940 return status;
941 }
942
943 return status;
944 }
945
in_reconfigure_channels(struct stream_in * in,effect_handle_t effect,channel_config_t * channel_config,bool config_changed)946 static int in_reconfigure_channels(struct stream_in *in,
947 effect_handle_t effect,
948 channel_config_t *channel_config,
949 bool config_changed) {
950
951 int status = 0;
952
953 ALOGV("in_reconfigure_channels(): config_changed %d effect %p",
954 config_changed, effect);
955
956 /* if config changed, reconfigure all previously added effects */
957 if (config_changed) {
958 int i;
959 ALOGV("%s: config_changed (%d)", __func__, config_changed);
960 for (i = 0; i < in->num_preprocessors; i++) {
961 int cur_status = in_configure_effect_channels(in->preprocessors[i].effect_itfe,
962 channel_config);
963 ALOGV("%s: in_configure_effect_channels i=(%d), [main_channel,aux_channel]=[%d|%d], status=%d",
964 __func__, i, channel_config->main_channels, channel_config->aux_channels, cur_status);
965 if (cur_status != 0) {
966 ALOGV("in_reconfigure_channels(): error %d configuring effect "
967 "%d with channels: [%04x][%04x]",
968 cur_status,
969 i,
970 channel_config->main_channels,
971 channel_config->aux_channels);
972 status = cur_status;
973 }
974 }
975 } else if (effect != NULL && channel_config->aux_channels) {
976 /* if aux channels config did not change but aux channels are present,
977 * we still need to configure the effect being added */
978 status = in_configure_effect_channels(effect, channel_config);
979 }
980 return status;
981 }
982
in_update_aux_channels(struct stream_in * in,effect_handle_t effect)983 static void in_update_aux_channels(struct stream_in *in,
984 effect_handle_t effect)
985 {
986 uint32_t aux_channels;
987 channel_config_t channel_config;
988 int status;
989
990 aux_channels = in_get_aux_channels(in);
991
992 channel_config.main_channels = in->main_channels;
993 channel_config.aux_channels = aux_channels;
994 status = in_reconfigure_channels(in,
995 effect,
996 &channel_config,
997 (aux_channels != in->aux_channels));
998
999 if (status != 0) {
1000 ALOGV("in_update_aux_channels(): in_reconfigure_channels error %d", status);
1001 /* resetting aux channels configuration */
1002 aux_channels = 0;
1003 channel_config.aux_channels = 0;
1004 in_reconfigure_channels(in, effect, &channel_config, true);
1005 }
1006 ALOGV("%s: aux_channels=%d, in->aux_channels_changed=%d", __func__, aux_channels, in->aux_channels_changed);
1007 if (in->aux_channels != aux_channels) {
1008 in->aux_channels_changed = true;
1009 in->aux_channels = aux_channels;
1010 do_in_standby_l(in);
1011 }
1012 }
1013 #endif
1014
1015 /* This function reads PCM data and:
1016 * - resample if needed
1017 * - process if pre-processors are attached
1018 * - discard unwanted channels
1019 */
read_and_process_frames(struct audio_stream_in * stream,void * buffer,ssize_t frames_num)1020 static ssize_t read_and_process_frames(struct audio_stream_in *stream, void* buffer, ssize_t frames_num)
1021 {
1022 struct stream_in *in = (struct stream_in *)stream;
1023 ssize_t frames_wr = 0; /* Number of frames actually read */
1024 size_t bytes_per_sample = audio_bytes_per_sample(stream->common.get_format(&stream->common));
1025 void *proc_buf_out = buffer;
1026 #ifdef PREPROCESSING_ENABLED
1027 audio_buffer_t in_buf;
1028 audio_buffer_t out_buf;
1029 int i;
1030 bool has_processing = in->num_preprocessors != 0;
1031 #endif
1032 /* Additional channels might be added on top of main_channels:
1033 * - aux_channels (by processing effects)
1034 * - extra channels due to HW limitations
1035 * In case of additional channels, we cannot work inplace
1036 */
1037 size_t src_channels = in->config.channels;
1038 size_t dst_channels = audio_channel_count_from_in_mask(in->main_channels);
1039 bool channel_remapping_needed = (dst_channels != src_channels);
1040 size_t src_buffer_size = frames_num * src_channels * bytes_per_sample;
1041
1042 #ifdef PREPROCESSING_ENABLED
1043 if (has_processing) {
1044 /* since all the processing below is done in frames and using the config.channels
1045 * as the number of channels, no changes is required in case aux_channels are present */
1046 while (frames_wr < frames_num) {
1047 /* first reload enough frames at the end of process input buffer */
1048 if (in->proc_buf_frames < (size_t)frames_num) {
1049 ssize_t frames_rd;
1050 if (in->proc_buf_size < (size_t)frames_num) {
1051 in->proc_buf_size = (size_t)frames_num;
1052 in->proc_buf_in = realloc(in->proc_buf_in, src_buffer_size);
1053 ALOG_ASSERT((in->proc_buf_in != NULL),
1054 "process_frames() failed to reallocate proc_buf_in");
1055 if (channel_remapping_needed) {
1056 in->proc_buf_out = realloc(in->proc_buf_out, src_buffer_size);
1057 ALOG_ASSERT((in->proc_buf_out != NULL),
1058 "process_frames() failed to reallocate proc_buf_out");
1059 proc_buf_out = in->proc_buf_out;
1060 }
1061 }
1062 frames_rd = read_frames(in,
1063 in->proc_buf_in +
1064 in->proc_buf_frames * src_channels * bytes_per_sample,
1065 frames_num - in->proc_buf_frames);
1066 if (frames_rd < 0) {
1067 /* Return error code */
1068 frames_wr = frames_rd;
1069 break;
1070 }
1071 in->proc_buf_frames += frames_rd;
1072 }
1073
1074 /* in_buf.frameCount and out_buf.frameCount indicate respectively
1075 * the maximum number of frames to be consumed and produced by process() */
1076 in_buf.frameCount = in->proc_buf_frames;
1077 in_buf.s16 = in->proc_buf_in;
1078 out_buf.frameCount = frames_num - frames_wr;
1079 out_buf.s16 = (int16_t *)proc_buf_out + frames_wr * in->config.channels;
1080
1081 /* FIXME: this works because of current pre processing library implementation that
1082 * does the actual process only when the last enabled effect process is called.
1083 * The generic solution is to have an output buffer for each effect and pass it as
1084 * input to the next.
1085 */
1086 for (i = 0; i < in->num_preprocessors; i++) {
1087 (*in->preprocessors[i].effect_itfe)->process(in->preprocessors[i].effect_itfe,
1088 &in_buf,
1089 &out_buf);
1090 }
1091
1092 /* process() has updated the number of frames consumed and produced in
1093 * in_buf.frameCount and out_buf.frameCount respectively
1094 * move remaining frames to the beginning of in->proc_buf_in */
1095 in->proc_buf_frames -= in_buf.frameCount;
1096
1097 if (in->proc_buf_frames) {
1098 memcpy(in->proc_buf_in,
1099 in->proc_buf_in + in_buf.frameCount * src_channels * bytes_per_sample,
1100 in->proc_buf_frames * in->config.channels * audio_bytes_per_sample(in_get_format(in)));
1101 }
1102
1103 /* if not enough frames were passed to process(), read more and retry. */
1104 if (out_buf.frameCount == 0) {
1105 ALOGW("No frames produced by preproc");
1106 continue;
1107 }
1108
1109 if ((frames_wr + (ssize_t)out_buf.frameCount) <= frames_num) {
1110 frames_wr += out_buf.frameCount;
1111 } else {
1112 /* The effect does not comply to the API. In theory, we should never end up here! */
1113 ALOGE("preprocessing produced too many frames: %d + %zd > %d !",
1114 (unsigned int)frames_wr, out_buf.frameCount, (unsigned int)frames_num);
1115 frames_wr = frames_num;
1116 }
1117 }
1118 }
1119 else
1120 #endif //PREPROCESSING_ENABLED
1121 {
1122 /* No processing effects attached */
1123 if (channel_remapping_needed) {
1124 /* With additional channels, we cannot use original buffer */
1125 if (in->proc_buf_size < src_buffer_size) {
1126 in->proc_buf_size = src_buffer_size;
1127 in->proc_buf_out = realloc(in->proc_buf_out, src_buffer_size);
1128 ALOG_ASSERT((in->proc_buf_out != NULL),
1129 "process_frames() failed to reallocate proc_buf_out");
1130 }
1131 proc_buf_out = in->proc_buf_out;
1132 }
1133 frames_wr = read_frames(in, proc_buf_out, frames_num);
1134 ALOG_ASSERT(frames_wr <= frames_num, "read more frames than requested");
1135 }
1136
1137 if (channel_remapping_needed) {
1138 size_t ret = adjust_channels(proc_buf_out, src_channels, buffer, dst_channels,
1139 bytes_per_sample, frames_wr * src_channels * bytes_per_sample);
1140 ALOG_ASSERT(ret == (frames_wr * dst_channels * bytes_per_sample));
1141 }
1142
1143 return frames_wr;
1144 }
1145
get_next_buffer(struct resampler_buffer_provider * buffer_provider,struct resampler_buffer * buffer)1146 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
1147 struct resampler_buffer* buffer)
1148 {
1149 struct stream_in *in;
1150 struct pcm_device *pcm_device;
1151
1152 if (buffer_provider == NULL || buffer == NULL)
1153 return -EINVAL;
1154
1155 in = (struct stream_in *)((char *)buffer_provider -
1156 offsetof(struct stream_in, buf_provider));
1157
1158 if (list_empty(&in->pcm_dev_list)) {
1159 buffer->raw = NULL;
1160 buffer->frame_count = 0;
1161 in->read_status = -ENODEV;
1162 return -ENODEV;
1163 }
1164
1165 pcm_device = node_to_item(list_head(&in->pcm_dev_list),
1166 struct pcm_device, stream_list_node);
1167
1168 if (in->read_buf_frames == 0) {
1169 size_t size_in_bytes = pcm_frames_to_bytes(pcm_device->pcm, in->config.period_size);
1170 if (in->read_buf_size < in->config.period_size) {
1171 in->read_buf_size = in->config.period_size;
1172 in->read_buf = (int16_t *) realloc(in->read_buf, size_in_bytes);
1173 ALOG_ASSERT((in->read_buf != NULL),
1174 "get_next_buffer() failed to reallocate read_buf");
1175 }
1176
1177 in->read_status = pcm_read(pcm_device->pcm, (void*)in->read_buf, size_in_bytes);
1178
1179 if (in->read_status != 0) {
1180 ALOGE("get_next_buffer() pcm_read error %d", in->read_status);
1181 buffer->raw = NULL;
1182 buffer->frame_count = 0;
1183 return in->read_status;
1184 }
1185 in->read_buf_frames = in->config.period_size;
1186 }
1187
1188 buffer->frame_count = (buffer->frame_count > in->read_buf_frames) ?
1189 in->read_buf_frames : buffer->frame_count;
1190 buffer->i16 = in->read_buf + (in->config.period_size - in->read_buf_frames) *
1191 in->config.channels;
1192 return in->read_status;
1193 }
1194
release_buffer(struct resampler_buffer_provider * buffer_provider,struct resampler_buffer * buffer)1195 static void release_buffer(struct resampler_buffer_provider *buffer_provider,
1196 struct resampler_buffer* buffer)
1197 {
1198 struct stream_in *in;
1199
1200 if (buffer_provider == NULL || buffer == NULL)
1201 return;
1202
1203 in = (struct stream_in *)((char *)buffer_provider -
1204 offsetof(struct stream_in, buf_provider));
1205
1206 in->read_buf_frames -= buffer->frame_count;
1207 }
1208
1209 /* read_frames() reads frames from kernel driver, down samples to capture rate
1210 * if necessary and output the number of frames requested to the buffer specified */
read_frames(struct stream_in * in,void * buffer,ssize_t frames)1211 static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames)
1212 {
1213 ssize_t frames_wr = 0;
1214
1215 struct pcm_device *pcm_device;
1216
1217 if (list_empty(&in->pcm_dev_list)) {
1218 ALOGE("%s: pcm device list empty", __func__);
1219 return -EINVAL;
1220 }
1221
1222 pcm_device = node_to_item(list_head(&in->pcm_dev_list),
1223 struct pcm_device, stream_list_node);
1224
1225 while (frames_wr < frames) {
1226 size_t frames_rd = frames - frames_wr;
1227 ALOGVV("%s: frames_rd: %zd, frames_wr: %zd, in->config.channels: %d",
1228 __func__,frames_rd,frames_wr,in->config.channels);
1229 if (in->resampler != NULL) {
1230 in->resampler->resample_from_provider(in->resampler,
1231 (int16_t *)((char *)buffer +
1232 pcm_frames_to_bytes(pcm_device->pcm, frames_wr)),
1233 &frames_rd);
1234 } else {
1235 struct resampler_buffer buf = {
1236 { raw : NULL, },
1237 frame_count : frames_rd,
1238 };
1239 get_next_buffer(&in->buf_provider, &buf);
1240 if (buf.raw != NULL) {
1241 memcpy((char *)buffer +
1242 pcm_frames_to_bytes(pcm_device->pcm, frames_wr),
1243 buf.raw,
1244 pcm_frames_to_bytes(pcm_device->pcm, buf.frame_count));
1245 frames_rd = buf.frame_count;
1246 }
1247 release_buffer(&in->buf_provider, &buf);
1248 }
1249 /* in->read_status is updated by getNextBuffer() also called by
1250 * in->resampler->resample_from_provider() */
1251 if (in->read_status != 0)
1252 return in->read_status;
1253
1254 frames_wr += frames_rd;
1255 }
1256 return frames_wr;
1257 }
1258
in_release_pcm_devices(struct stream_in * in)1259 static int in_release_pcm_devices(struct stream_in *in)
1260 {
1261 struct pcm_device *pcm_device;
1262 struct listnode *node;
1263 struct listnode *next;
1264
1265 list_for_each_safe(node, next, &in->pcm_dev_list) {
1266 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
1267 list_remove(node);
1268 free(pcm_device);
1269 }
1270
1271 return 0;
1272 }
1273
stop_input_stream(struct stream_in * in)1274 static int stop_input_stream(struct stream_in *in)
1275 {
1276 struct audio_usecase *uc_info;
1277 struct audio_device *adev = in->dev;
1278
1279 adev->active_input = NULL;
1280 ALOGV("%s: enter: usecase(%d: %s)", __func__,
1281 in->usecase, use_case_table[in->usecase]);
1282 uc_info = get_usecase_from_id(adev, in->usecase);
1283 if (uc_info == NULL) {
1284 ALOGE("%s: Could not find the usecase (%d) in the list",
1285 __func__, in->usecase);
1286 return -EINVAL;
1287 }
1288
1289 /* Disable the tx device */
1290 disable_snd_device(adev, uc_info, uc_info->in_snd_device, true);
1291
1292 list_remove(&uc_info->adev_list_node);
1293 free(uc_info);
1294
1295 if (list_empty(&in->pcm_dev_list)) {
1296 ALOGE("%s: pcm device list empty", __func__);
1297 return -EINVAL;
1298 }
1299
1300 in_release_pcm_devices(in);
1301 list_init(&in->pcm_dev_list);
1302
1303 return 0;
1304 }
1305
start_input_stream(struct stream_in * in)1306 int start_input_stream(struct stream_in *in)
1307 {
1308 /* Enable output device and stream routing controls */
1309 int ret = 0;
1310 bool recreate_resampler = false;
1311 struct audio_usecase *uc_info;
1312 struct audio_device *adev = in->dev;
1313 struct pcm_device_profile *pcm_profile;
1314 struct pcm_device *pcm_device;
1315
1316 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
1317 adev->active_input = in;
1318 pcm_profile = get_pcm_device(in->usecase_type, in->devices);
1319 if (pcm_profile == NULL) {
1320 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
1321 __func__, in->usecase);
1322 ret = -EINVAL;
1323 goto error_config;
1324 }
1325
1326 if (in->input_flags & AUDIO_INPUT_FLAG_FAST) {
1327 ALOGV("%s: change capture period size to low latency size %d",
1328 __func__, CAPTURE_PERIOD_SIZE_LOW_LATENCY);
1329 pcm_profile->config.period_size = CAPTURE_PERIOD_SIZE_LOW_LATENCY;
1330 }
1331
1332 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1333 uc_info->id = in->usecase;
1334 uc_info->type = PCM_CAPTURE;
1335 uc_info->stream = (struct audio_stream *)in;
1336 uc_info->devices = in->devices;
1337 uc_info->in_snd_device = SND_DEVICE_NONE;
1338 uc_info->out_snd_device = SND_DEVICE_NONE;
1339
1340 pcm_device = (struct pcm_device *)calloc(1, sizeof(struct pcm_device));
1341 pcm_device->pcm_profile = pcm_profile;
1342 list_init(&in->pcm_dev_list);
1343 list_add_tail(&in->pcm_dev_list, &pcm_device->stream_list_node);
1344
1345 list_init(&uc_info->mixer_list);
1346 list_add_tail(&uc_info->mixer_list,
1347 &adev_get_mixer_for_card(adev,
1348 pcm_device->pcm_profile->card)->uc_list_node[uc_info->id]);
1349
1350 list_add_tail(&adev->usecase_list, &uc_info->adev_list_node);
1351
1352 select_devices(adev, in->usecase);
1353
1354 /* Config should be updated as profile can be changed between different calls
1355 * to this function:
1356 * - Trigger resampler creation
1357 * - Config needs to be updated */
1358 if (in->config.rate != pcm_profile->config.rate) {
1359 recreate_resampler = true;
1360 }
1361 in->config = pcm_profile->config;
1362
1363 #ifdef PREPROCESSING_ENABLED
1364 if (in->aux_channels_changed) {
1365 in->config.channels = audio_channel_count_from_in_mask(in->aux_channels);
1366 recreate_resampler = true;
1367 }
1368 #endif
1369
1370 if (in->requested_rate != in->config.rate) {
1371 recreate_resampler = true;
1372 }
1373
1374 if (recreate_resampler) {
1375 if (in->resampler) {
1376 release_resampler(in->resampler);
1377 in->resampler = NULL;
1378 }
1379 in->buf_provider.get_next_buffer = get_next_buffer;
1380 in->buf_provider.release_buffer = release_buffer;
1381 ret = create_resampler(in->config.rate,
1382 in->requested_rate,
1383 in->config.channels,
1384 RESAMPLER_QUALITY_DEFAULT,
1385 &in->buf_provider,
1386 &in->resampler);
1387 }
1388
1389 /* Open the PCM device.
1390 * The HW is limited to support only the default pcm_profile settings.
1391 * As such a change in aux_channels will not have an effect.
1392 */
1393 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d, smp rate %d format %d, \
1394 period_size %d", __func__, pcm_device->pcm_profile->card, pcm_device->pcm_profile->device,
1395 pcm_device->pcm_profile->config.channels,pcm_device->pcm_profile->config.rate,
1396 pcm_device->pcm_profile->config.format, pcm_device->pcm_profile->config.period_size);
1397
1398 if (pcm_profile->type == PCM_HOTWORD_STREAMING) {
1399 if (!adev->sound_trigger_open_for_streaming) {
1400 ALOGE("%s: No handle to sound trigger HAL", __func__);
1401 ret = -EIO;
1402 goto error_open;
1403 }
1404 pcm_device->pcm = NULL;
1405 pcm_device->sound_trigger_handle =
1406 adev->sound_trigger_open_for_streaming();
1407 if (pcm_device->sound_trigger_handle <= 0) {
1408 ALOGE("%s: Failed to open DSP for streaming", __func__);
1409 ret = -EIO;
1410 goto error_open;
1411 }
1412 ALOGV("Opened DSP successfully");
1413 } else {
1414 pcm_device->sound_trigger_handle = 0;
1415 pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card,
1416 pcm_device->pcm_profile->device,
1417 PCM_IN | PCM_MONOTONIC,
1418 &pcm_device->pcm_profile->config);
1419 if (pcm_device->pcm && !pcm_is_ready(pcm_device->pcm)) {
1420 ALOGE("%s: %s", __func__, pcm_get_error(pcm_device->pcm));
1421 pcm_close(pcm_device->pcm);
1422 pcm_device->pcm = NULL;
1423 ret = -EIO;
1424 goto error_open;
1425 }
1426 }
1427
1428 /* force read and proc buffer reallocation in case of frame size or
1429 * channel count change */
1430 #ifdef PREPROCESSING_ENABLED
1431 in->proc_buf_frames = 0;
1432 #endif
1433 in->proc_buf_size = 0;
1434 in->read_buf_size = 0;
1435 in->read_buf_frames = 0;
1436
1437 /* if no supported sample rate is available, use the resampler */
1438 if (in->resampler) {
1439 in->resampler->reset(in->resampler);
1440 }
1441
1442 ALOGV("%s: exit", __func__);
1443 return ret;
1444
1445 error_open:
1446 if (in->resampler) {
1447 release_resampler(in->resampler);
1448 in->resampler = NULL;
1449 }
1450 stop_input_stream(in);
1451
1452 error_config:
1453 ALOGV("%s: exit: status(%d)", __func__, ret);
1454 adev->active_input = NULL;
1455 return ret;
1456 }
1457
lock_input_stream(struct stream_in * in)1458 static void lock_input_stream(struct stream_in *in)
1459 {
1460 pthread_mutex_lock(&in->pre_lock);
1461 pthread_mutex_lock(&in->lock);
1462 pthread_mutex_unlock(&in->pre_lock);
1463 }
1464
lock_output_stream(struct stream_out * out)1465 static void lock_output_stream(struct stream_out *out)
1466 {
1467 pthread_mutex_lock(&out->pre_lock);
1468 pthread_mutex_lock(&out->lock);
1469 pthread_mutex_unlock(&out->pre_lock);
1470 }
1471
uc_release_pcm_devices(struct audio_usecase * usecase)1472 static int uc_release_pcm_devices(struct audio_usecase *usecase)
1473 {
1474 struct stream_out *out = (struct stream_out *)usecase->stream;
1475 struct pcm_device *pcm_device;
1476 struct listnode *node;
1477 struct listnode *next;
1478
1479 list_for_each_safe(node, next, &out->pcm_dev_list) {
1480 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
1481 list_remove(node);
1482 free(pcm_device);
1483 }
1484 list_init(&usecase->mixer_list);
1485
1486 return 0;
1487 }
1488
uc_select_pcm_devices(struct audio_usecase * usecase)1489 static int uc_select_pcm_devices(struct audio_usecase *usecase)
1490
1491 {
1492 struct stream_out *out = (struct stream_out *)usecase->stream;
1493 struct pcm_device *pcm_device;
1494 struct pcm_device_profile *pcm_profile;
1495 struct mixer_card *mixer_card;
1496 audio_devices_t devices = usecase->devices;
1497
1498 list_init(&usecase->mixer_list);
1499 list_init(&out->pcm_dev_list);
1500
1501 pcm_profile = get_pcm_device(usecase->type, devices);
1502 if (pcm_profile) {
1503 pcm_device = calloc(1, sizeof(struct pcm_device));
1504 pcm_device->pcm_profile = pcm_profile;
1505 list_add_tail(&out->pcm_dev_list, &pcm_device->stream_list_node);
1506 mixer_card = uc_get_mixer_for_card(usecase, pcm_profile->card);
1507 if (mixer_card == NULL) {
1508 mixer_card = adev_get_mixer_for_card(out->dev, pcm_profile->card);
1509 list_add_tail(&usecase->mixer_list, &mixer_card->uc_list_node[usecase->id]);
1510 }
1511 devices &= ~pcm_profile->devices;
1512 } else {
1513 ALOGE("usecase type=%d, devices=%d did not find exact match",
1514 usecase->type, devices);
1515 }
1516
1517 return 0;
1518 }
1519
out_close_pcm_devices(struct stream_out * out)1520 static int out_close_pcm_devices(struct stream_out *out)
1521 {
1522 struct pcm_device *pcm_device;
1523 struct listnode *node;
1524 struct audio_device *adev = out->dev;
1525
1526 list_for_each(node, &out->pcm_dev_list) {
1527 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
1528 if (pcm_device->sound_trigger_handle > 0) {
1529 adev->sound_trigger_close_for_streaming(
1530 pcm_device->sound_trigger_handle);
1531 pcm_device->sound_trigger_handle = 0;
1532 }
1533 if (pcm_device->pcm) {
1534 pcm_close(pcm_device->pcm);
1535 pcm_device->pcm = NULL;
1536 }
1537 if (pcm_device->resampler) {
1538 release_resampler(pcm_device->resampler);
1539 pcm_device->resampler = NULL;
1540 }
1541 if (pcm_device->res_buffer) {
1542 free(pcm_device->res_buffer);
1543 pcm_device->res_buffer = NULL;
1544 }
1545 if (pcm_device->dsp_context) {
1546 cras_dsp_context_free(pcm_device->dsp_context);
1547 pcm_device->dsp_context = NULL;
1548 }
1549 }
1550
1551 return 0;
1552 }
1553
out_open_pcm_devices(struct stream_out * out)1554 static int out_open_pcm_devices(struct stream_out *out)
1555 {
1556 struct pcm_device *pcm_device;
1557 struct listnode *node;
1558 struct audio_device *adev = out->dev;
1559 int ret = 0;
1560
1561 list_for_each(node, &out->pcm_dev_list) {
1562 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
1563 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1564 __func__, pcm_device->pcm_profile->card, pcm_device->pcm_profile->device);
1565
1566 if (pcm_device->pcm_profile->dsp_name) {
1567 pcm_device->dsp_context = cras_dsp_context_new(pcm_device->pcm_profile->config.rate,
1568 (adev->mode == AUDIO_MODE_IN_CALL || adev->mode == AUDIO_MODE_IN_COMMUNICATION)
1569 ? "voice-comm" : "playback");
1570 if (pcm_device->dsp_context) {
1571 cras_dsp_set_variable(pcm_device->dsp_context, "dsp_name",
1572 pcm_device->pcm_profile->dsp_name);
1573 cras_dsp_load_pipeline(pcm_device->dsp_context);
1574 }
1575 }
1576
1577 pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card, pcm_device->pcm_profile->device,
1578 PCM_OUT | PCM_MONOTONIC, &pcm_device->pcm_profile->config);
1579
1580 if (pcm_device->pcm && !pcm_is_ready(pcm_device->pcm)) {
1581 ALOGE("%s: %s", __func__, pcm_get_error(pcm_device->pcm));
1582 pcm_device->pcm = NULL;
1583 ret = -EIO;
1584 goto error_open;
1585 }
1586 /*
1587 * If the stream rate differs from the PCM rate, we need to
1588 * create a resampler.
1589 */
1590 if (out->sample_rate != pcm_device->pcm_profile->config.rate) {
1591 ALOGV("%s: create_resampler(), pcm_device_card(%d), pcm_device_id(%d), \
1592 out_rate(%d), device_rate(%d)",__func__,
1593 pcm_device->pcm_profile->card, pcm_device->pcm_profile->device,
1594 out->sample_rate, pcm_device->pcm_profile->config.rate);
1595 ret = create_resampler(out->sample_rate,
1596 pcm_device->pcm_profile->config.rate,
1597 audio_channel_count_from_out_mask(out->channel_mask),
1598 RESAMPLER_QUALITY_DEFAULT,
1599 NULL,
1600 &pcm_device->resampler);
1601 pcm_device->res_byte_count = 0;
1602 pcm_device->res_buffer = NULL;
1603 }
1604 }
1605 return ret;
1606
1607 error_open:
1608 out_close_pcm_devices(out);
1609 return ret;
1610 }
1611
disable_output_path_l(struct stream_out * out)1612 static int disable_output_path_l(struct stream_out *out)
1613 {
1614 struct audio_device *adev = out->dev;
1615 struct audio_usecase *uc_info;
1616
1617 uc_info = get_usecase_from_id(adev, out->usecase);
1618 if (uc_info == NULL) {
1619 ALOGE("%s: Could not find the usecase (%d) in the list",
1620 __func__, out->usecase);
1621 return -EINVAL;
1622 }
1623 disable_snd_device(adev, uc_info, uc_info->out_snd_device, true);
1624 uc_release_pcm_devices(uc_info);
1625 list_remove(&uc_info->adev_list_node);
1626 free(uc_info);
1627
1628 return 0;
1629 }
1630
enable_output_path_l(struct stream_out * out)1631 static void enable_output_path_l(struct stream_out *out)
1632 {
1633 struct audio_device *adev = out->dev;
1634 struct audio_usecase *uc_info;
1635
1636 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1637 uc_info->id = out->usecase;
1638 uc_info->type = PCM_PLAYBACK;
1639 uc_info->stream = (struct audio_stream *)out;
1640 uc_info->devices = out->devices;
1641 uc_info->in_snd_device = SND_DEVICE_NONE;
1642 uc_info->out_snd_device = SND_DEVICE_NONE;
1643 uc_select_pcm_devices(uc_info);
1644
1645 list_add_tail(&adev->usecase_list, &uc_info->adev_list_node);
1646
1647 select_devices(adev, out->usecase);
1648 }
1649
stop_output_stream(struct stream_out * out)1650 static int stop_output_stream(struct stream_out *out)
1651 {
1652 int ret = 0;
1653 struct audio_device *adev = out->dev;
1654 bool do_disable = true;
1655
1656 ALOGV("%s: enter: usecase(%d: %s)", __func__,
1657 out->usecase, use_case_table[out->usecase]);
1658
1659 ret = disable_output_path_l(out);
1660
1661 ALOGV("%s: exit: status(%d)", __func__, ret);
1662 return ret;
1663 }
1664
start_output_stream(struct stream_out * out)1665 int start_output_stream(struct stream_out *out)
1666 {
1667 int ret = 0;
1668 struct audio_device *adev = out->dev;
1669
1670 ALOGV("%s: enter: usecase(%d: %s) devices(%#x) channels(%d)",
1671 __func__, out->usecase, use_case_table[out->usecase], out->devices, out->config.channels);
1672
1673 enable_output_path_l(out);
1674
1675 ret = out_open_pcm_devices(out);
1676 if (ret != 0)
1677 goto error_open;
1678 ALOGV("%s: exit", __func__);
1679 return 0;
1680 error_open:
1681 stop_output_stream(out);
1682 return ret;
1683 }
1684
stop_voice_call(struct audio_device * adev)1685 static int stop_voice_call(struct audio_device *adev)
1686 {
1687 struct audio_usecase *uc_info;
1688
1689 ALOGV("%s: enter", __func__);
1690 adev->in_call = false;
1691
1692 /* TODO: implement voice call stop */
1693
1694 uc_info = get_usecase_from_id(adev, USECASE_VOICE_CALL);
1695 if (uc_info == NULL) {
1696 ALOGE("%s: Could not find the usecase (%d) in the list",
1697 __func__, USECASE_VOICE_CALL);
1698 return -EINVAL;
1699 }
1700
1701 disable_snd_device(adev, uc_info, uc_info->out_snd_device, false);
1702 disable_snd_device(adev, uc_info, uc_info->in_snd_device, true);
1703
1704 uc_release_pcm_devices(uc_info);
1705 list_remove(&uc_info->adev_list_node);
1706 free(uc_info);
1707
1708 ALOGV("%s: exit", __func__);
1709 return 0;
1710 }
1711
1712 /* always called with adev lock held */
start_voice_call(struct audio_device * adev)1713 static int start_voice_call(struct audio_device *adev)
1714 {
1715 struct audio_usecase *uc_info;
1716
1717 ALOGV("%s: enter", __func__);
1718
1719 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1720 uc_info->id = USECASE_VOICE_CALL;
1721 uc_info->type = VOICE_CALL;
1722 uc_info->stream = (struct audio_stream *)adev->primary_output;
1723 uc_info->devices = adev->primary_output->devices;
1724 uc_info->in_snd_device = SND_DEVICE_NONE;
1725 uc_info->out_snd_device = SND_DEVICE_NONE;
1726
1727 uc_select_pcm_devices(uc_info);
1728
1729 list_add_tail(&adev->usecase_list, &uc_info->adev_list_node);
1730
1731 select_devices(adev, USECASE_VOICE_CALL);
1732
1733
1734 /* TODO: implement voice call start */
1735
1736 /* set cached volume */
1737 set_voice_volume_l(adev, adev->voice_volume);
1738
1739 adev->in_call = true;
1740 ALOGV("%s: exit", __func__);
1741 return 0;
1742 }
1743
check_input_parameters(uint32_t sample_rate,audio_format_t format,int channel_count)1744 static int check_input_parameters(uint32_t sample_rate,
1745 audio_format_t format,
1746 int channel_count)
1747 {
1748 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1749
1750 if ((channel_count < 1) || (channel_count > 4)) return -EINVAL;
1751
1752 switch (sample_rate) {
1753 case 8000:
1754 case 11025:
1755 case 12000:
1756 case 16000:
1757 case 22050:
1758 case 24000:
1759 case 32000:
1760 case 44100:
1761 case 48000:
1762 break;
1763 default:
1764 return -EINVAL;
1765 }
1766
1767 return 0;
1768 }
1769
get_input_buffer_size(uint32_t sample_rate,audio_format_t format,int channel_count,usecase_type_t usecase_type,audio_devices_t devices)1770 static size_t get_input_buffer_size(uint32_t sample_rate,
1771 audio_format_t format,
1772 int channel_count,
1773 usecase_type_t usecase_type,
1774 audio_devices_t devices)
1775 {
1776 size_t size = 0;
1777 struct pcm_device_profile *pcm_profile;
1778
1779 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1780 return 0;
1781
1782 pcm_profile = get_pcm_device(usecase_type, devices);
1783 if (pcm_profile == NULL)
1784 return 0;
1785
1786 /*
1787 * take resampling into account and return the closest majoring
1788 * multiple of 16 frames, as audioflinger expects audio buffers to
1789 * be a multiple of 16 frames
1790 */
1791 size = (pcm_profile->config.period_size * sample_rate) / pcm_profile->config.rate;
1792 size = ((size + 15) / 16) * 16;
1793
1794 return (size * channel_count * audio_bytes_per_sample(format));
1795
1796 }
1797
out_get_sample_rate(const struct audio_stream * stream)1798 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1799 {
1800 struct stream_out *out = (struct stream_out *)stream;
1801
1802 return out->sample_rate;
1803 }
1804
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)1805 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1806 {
1807 (void)stream;
1808 (void)rate;
1809 return -ENOSYS;
1810 }
1811
out_get_buffer_size(const struct audio_stream * stream)1812 static size_t out_get_buffer_size(const struct audio_stream *stream)
1813 {
1814 struct stream_out *out = (struct stream_out *)stream;
1815
1816 return out->config.period_size *
1817 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
1818 }
1819
out_get_channels(const struct audio_stream * stream)1820 static uint32_t out_get_channels(const struct audio_stream *stream)
1821 {
1822 struct stream_out *out = (struct stream_out *)stream;
1823
1824 return out->channel_mask;
1825 }
1826
out_get_format(const struct audio_stream * stream)1827 static audio_format_t out_get_format(const struct audio_stream *stream)
1828 {
1829 struct stream_out *out = (struct stream_out *)stream;
1830
1831 return out->format;
1832 }
1833
out_set_format(struct audio_stream * stream,audio_format_t format)1834 static int out_set_format(struct audio_stream *stream, audio_format_t format)
1835 {
1836 (void)stream;
1837 (void)format;
1838 return -ENOSYS;
1839 }
1840
do_out_standby_l(struct stream_out * out)1841 static int do_out_standby_l(struct stream_out *out)
1842 {
1843 struct audio_device *adev = out->dev;
1844 int status = 0;
1845
1846 out->standby = true;
1847 out_close_pcm_devices(out);
1848 status = stop_output_stream(out);
1849
1850 return status;
1851 }
1852
out_standby(struct audio_stream * stream)1853 static int out_standby(struct audio_stream *stream)
1854 {
1855 struct stream_out *out = (struct stream_out *)stream;
1856 struct audio_device *adev = out->dev;
1857
1858 ALOGV("%s: enter: usecase(%d: %s)", __func__,
1859 out->usecase, use_case_table[out->usecase]);
1860 lock_output_stream(out);
1861 if (!out->standby) {
1862 pthread_mutex_lock(&adev->lock);
1863 do_out_standby_l(out);
1864 pthread_mutex_unlock(&adev->lock);
1865 }
1866 pthread_mutex_unlock(&out->lock);
1867 ALOGV("%s: exit", __func__);
1868 return 0;
1869 }
1870
out_dump(const struct audio_stream * stream,int fd)1871 static int out_dump(const struct audio_stream *stream, int fd)
1872 {
1873 (void)stream;
1874 (void)fd;
1875
1876 return 0;
1877 }
1878
out_set_parameters(struct audio_stream * stream,const char * kvpairs)1879 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1880 {
1881 struct stream_out *out = (struct stream_out *)stream;
1882 struct audio_device *adev = out->dev;
1883 struct audio_usecase *usecase;
1884 struct listnode *node;
1885 struct str_parms *parms;
1886 char value[32];
1887 int ret, val = 0;
1888 struct audio_usecase *uc_info;
1889 bool do_standby = false;
1890 struct pcm_device *pcm_device;
1891 struct pcm_device_profile *pcm_profile;
1892 #ifdef PREPROCESSING_ENABLED
1893 struct stream_in *in = NULL; /* if non-NULL, then force input to standby */
1894 #endif
1895
1896 ALOGV("%s: enter: usecase(%d: %s) kvpairs: %s out->devices(%d) adev->mode(%d)",
1897 __func__, out->usecase, use_case_table[out->usecase], kvpairs, out->devices, adev->mode);
1898 parms = str_parms_create_str(kvpairs);
1899 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1900 if (ret >= 0) {
1901 val = atoi(value);
1902 pthread_mutex_lock(&adev->lock_inputs);
1903 lock_output_stream(out);
1904 pthread_mutex_lock(&adev->lock);
1905 #ifdef PREPROCESSING_ENABLED
1906 if (((int)out->devices != val) && (val != 0) && (!out->standby) &&
1907 (out->usecase == USECASE_AUDIO_PLAYBACK)) {
1908 /* reset active input:
1909 * - to attach the echo reference
1910 * - because a change in output device may change mic settings */
1911 if (adev->active_input && (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
1912 adev->active_input->source == AUDIO_SOURCE_MIC)) {
1913 in = adev->active_input;
1914 }
1915 }
1916 #endif
1917 if (val != 0) {
1918 out->devices = val;
1919
1920 if (!out->standby) {
1921 uc_info = get_usecase_from_id(adev, out->usecase);
1922 if (uc_info == NULL) {
1923 ALOGE("%s: Could not find the usecase (%d) in the list",
1924 __func__, out->usecase);
1925 } else {
1926 list_for_each(node, &out->pcm_dev_list) {
1927 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
1928 if ((pcm_device->pcm_profile->devices & val) == 0)
1929 do_standby = true;
1930 val &= ~pcm_device->pcm_profile->devices;
1931 }
1932 if (val != 0)
1933 do_standby = true;
1934 }
1935 if (do_standby)
1936 do_out_standby_l(out);
1937 else
1938 select_devices(adev, out->usecase);
1939 }
1940
1941 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1942 (out == adev->primary_output)) {
1943 start_voice_call(adev);
1944 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1945 (out == adev->primary_output)) {
1946 select_devices(adev, USECASE_VOICE_CALL);
1947 }
1948 }
1949
1950 if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call &&
1951 (out == adev->primary_output)) {
1952 stop_voice_call(adev);
1953 }
1954 pthread_mutex_unlock(&adev->lock);
1955 pthread_mutex_unlock(&out->lock);
1956 #ifdef PREPROCESSING_ENABLED
1957 if (in) {
1958 /* The lock on adev->lock_inputs prevents input stream from being closed */
1959 lock_input_stream(in);
1960 pthread_mutex_lock(&adev->lock);
1961 LOG_ALWAYS_FATAL_IF(in != adev->active_input);
1962 do_in_standby_l(in);
1963 pthread_mutex_unlock(&adev->lock);
1964 pthread_mutex_unlock(&in->lock);
1965 }
1966 #endif
1967 pthread_mutex_unlock(&adev->lock_inputs);
1968 }
1969
1970 str_parms_destroy(parms);
1971 ALOGV("%s: exit: code(%d)", __func__, ret);
1972 return ret;
1973 }
1974
out_get_parameters(const struct audio_stream * stream,const char * keys)1975 static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1976 {
1977 struct stream_out *out = (struct stream_out *)stream;
1978 struct str_parms *query = str_parms_create_str(keys);
1979 char *str;
1980 char value[256];
1981 struct str_parms *reply = str_parms_create();
1982 size_t i, j;
1983 int ret;
1984 bool first = true;
1985 ALOGV("%s: enter: keys - %s", __func__, keys);
1986 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1987 if (ret >= 0) {
1988 value[0] = '\0';
1989 i = 0;
1990 while (out->supported_channel_masks[i] != 0) {
1991 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1992 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1993 if (!first) {
1994 strcat(value, "|");
1995 }
1996 strcat(value, out_channels_name_to_enum_table[j].name);
1997 first = false;
1998 break;
1999 }
2000 }
2001 i++;
2002 }
2003 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
2004 str = str_parms_to_str(reply);
2005 } else {
2006 str = strdup(keys);
2007 }
2008 str_parms_destroy(query);
2009 str_parms_destroy(reply);
2010 ALOGV("%s: exit: returns - %s", __func__, str);
2011 return str;
2012 }
2013
out_get_latency(const struct audio_stream_out * stream)2014 static uint32_t out_get_latency(const struct audio_stream_out *stream)
2015 {
2016 struct stream_out *out = (struct stream_out *)stream;
2017
2018 return (out->config.period_count * out->config.period_size * 1000) /
2019 (out->config.rate);
2020 }
2021
out_set_volume(struct audio_stream_out * stream,float left,float right)2022 static int out_set_volume(struct audio_stream_out *stream, float left,
2023 float right)
2024 {
2025 struct stream_out *out = (struct stream_out *)stream;
2026 struct audio_device *adev = out->dev;
2027 (void)right;
2028
2029 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
2030 /* only take left channel into account: the API is for stereo anyway */
2031 out->muted = (left == 0.0f);
2032 return 0;
2033 }
2034
2035 return -ENOSYS;
2036 }
2037
2038 /* Applies the DSP to the samples for the iodev if applicable. */
apply_dsp(struct pcm_device * iodev,uint8_t * buf,size_t frames)2039 static void apply_dsp(struct pcm_device *iodev, uint8_t *buf, size_t frames)
2040 {
2041 struct cras_dsp_context *ctx;
2042 struct pipeline *pipeline;
2043
2044 ctx = iodev->dsp_context;
2045 if (!ctx)
2046 return;
2047
2048 pipeline = cras_dsp_get_pipeline(ctx);
2049 if (!pipeline)
2050 return;
2051
2052 cras_dsp_pipeline_apply(pipeline,
2053 buf,
2054 frames);
2055
2056 cras_dsp_put_pipeline(ctx);
2057 }
2058
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)2059 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
2060 size_t bytes)
2061 {
2062 struct stream_out *out = (struct stream_out *)stream;
2063 struct audio_device *adev = out->dev;
2064 ssize_t ret = 0;
2065 struct pcm_device *pcm_device;
2066 struct listnode *node;
2067 size_t frame_size = audio_stream_out_frame_size(stream);
2068 size_t frames_wr = 0, frames_rq = 0;
2069 unsigned char *data = NULL;
2070 struct pcm_config config;
2071 #ifdef PREPROCESSING_ENABLED
2072 size_t in_frames = bytes / frame_size;
2073 size_t out_frames = in_frames;
2074 struct stream_in *in = NULL;
2075 #endif
2076
2077 lock_output_stream(out);
2078 if (out->standby) {
2079 #ifdef PREPROCESSING_ENABLED
2080 pthread_mutex_unlock(&out->lock);
2081 /* Prevent input stream from being closed */
2082 pthread_mutex_lock(&adev->lock_inputs);
2083 lock_output_stream(out);
2084 if (!out->standby) {
2085 pthread_mutex_unlock(&adev->lock_inputs);
2086 goto false_alarm;
2087 }
2088 #endif
2089 pthread_mutex_lock(&adev->lock);
2090 ret = start_output_stream(out);
2091 if (ret != 0) {
2092 pthread_mutex_unlock(&adev->lock);
2093 #ifdef PREPROCESSING_ENABLED
2094 pthread_mutex_unlock(&adev->lock_inputs);
2095 #endif
2096 goto exit;
2097 }
2098 out->standby = false;
2099
2100 #ifdef PREPROCESSING_ENABLED
2101 /* A change in output device may change the microphone selection */
2102 if (adev->active_input &&
2103 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
2104 adev->active_input->source == AUDIO_SOURCE_MIC)) {
2105 in = adev->active_input;
2106 ALOGV("%s: enter: force_input_standby true", __func__);
2107 }
2108 #endif
2109 pthread_mutex_unlock(&adev->lock);
2110 #ifdef PREPROCESSING_ENABLED
2111 if (!in) {
2112 /* Leave mutex locked iff in != NULL */
2113 pthread_mutex_unlock(&adev->lock_inputs);
2114 }
2115 #endif
2116 }
2117 false_alarm:
2118
2119 if (out->muted)
2120 memset((void *)buffer, 0, bytes);
2121 list_for_each(node, &out->pcm_dev_list) {
2122 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
2123 if (pcm_device->resampler) {
2124 if (bytes * pcm_device->pcm_profile->config.rate / out->sample_rate + frame_size
2125 > pcm_device->res_byte_count) {
2126 pcm_device->res_byte_count =
2127 bytes * pcm_device->pcm_profile->config.rate / out->sample_rate + frame_size;
2128 pcm_device->res_buffer =
2129 realloc(pcm_device->res_buffer, pcm_device->res_byte_count);
2130 ALOGV("%s: resampler res_byte_count = %zu", __func__,
2131 pcm_device->res_byte_count);
2132 }
2133 frames_rq = bytes / frame_size;
2134 frames_wr = pcm_device->res_byte_count / frame_size;
2135 ALOGVV("%s: resampler request frames = %zu frame_size = %zu",
2136 __func__, frames_rq, frame_size);
2137 pcm_device->resampler->resample_from_input(pcm_device->resampler,
2138 (int16_t *)buffer, &frames_rq, (int16_t *)pcm_device->res_buffer, &frames_wr);
2139 ALOGVV("%s: resampler output frames_= %zu", __func__, frames_wr);
2140 }
2141 if (pcm_device->pcm) {
2142 size_t src_channels = audio_channel_count_from_out_mask(out->channel_mask);
2143 size_t dst_channels = pcm_device->pcm_profile->config.channels;
2144 bool channel_remapping_needed = (dst_channels != src_channels);
2145 unsigned audio_bytes;
2146 const void *audio_data;
2147
2148 ALOGVV("%s: writing buffer (%zd bytes) to pcm device", __func__, bytes);
2149 if (pcm_device->resampler && pcm_device->res_buffer) {
2150 audio_data = pcm_device->res_buffer;
2151 audio_bytes = frames_wr * frame_size;
2152 } else {
2153 audio_data = buffer;
2154 audio_bytes = bytes;
2155 }
2156
2157 /*
2158 * This can only be S16_LE stereo because of the supported formats,
2159 * 4 bytes per frame.
2160 */
2161 apply_dsp(pcm_device, audio_data, audio_bytes/4);
2162
2163 if (channel_remapping_needed) {
2164 const void *remapped_audio_data;
2165 size_t dest_buffer_size = audio_bytes * dst_channels / src_channels;
2166 size_t new_size;
2167 size_t bytes_per_sample = audio_bytes_per_sample(stream->common.get_format(&stream->common));
2168
2169 /* With additional channels, we cannot use original buffer */
2170 if (out->proc_buf_size < dest_buffer_size) {
2171 out->proc_buf_size = dest_buffer_size;
2172 out->proc_buf_out = realloc(out->proc_buf_out, dest_buffer_size);
2173 ALOG_ASSERT((out->proc_buf_out != NULL),
2174 "out_write() failed to reallocate proc_buf_out");
2175 }
2176 new_size = adjust_channels(audio_data, src_channels, out->proc_buf_out, dst_channels,
2177 bytes_per_sample, audio_bytes);
2178 ALOG_ASSERT(new_size == dest_buffer_size);
2179 audio_data = out->proc_buf_out;
2180 audio_bytes = dest_buffer_size;
2181 }
2182
2183 pcm_device->status = pcm_write(pcm_device->pcm, audio_data, audio_bytes);
2184 if (pcm_device->status != 0)
2185 ret = pcm_device->status;
2186 }
2187 }
2188 if (ret == 0)
2189 out->written += bytes / frame_size;
2190
2191 exit:
2192 pthread_mutex_unlock(&out->lock);
2193
2194 if (ret != 0) {
2195 list_for_each(node, &out->pcm_dev_list) {
2196 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
2197 if (pcm_device->pcm && pcm_device->status != 0)
2198 ALOGE("%s: error %zd - %s", __func__, ret, pcm_get_error(pcm_device->pcm));
2199 }
2200 out_standby(&out->stream.common);
2201 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
2202 out_get_sample_rate(&out->stream.common));
2203 }
2204
2205 #ifdef PREPROCESSING_ENABLED
2206 if (in) {
2207 /* The lock on adev->lock_inputs prevents input stream from being closed */
2208 lock_input_stream(in);
2209 pthread_mutex_lock(&adev->lock);
2210 LOG_ALWAYS_FATAL_IF(in != adev->active_input);
2211 do_in_standby_l(in);
2212 pthread_mutex_unlock(&adev->lock);
2213 pthread_mutex_unlock(&in->lock);
2214 /* This mutex was left locked iff in != NULL */
2215 pthread_mutex_unlock(&adev->lock_inputs);
2216 }
2217 #endif
2218
2219 return bytes;
2220 }
2221
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)2222 static int out_get_render_position(const struct audio_stream_out *stream,
2223 uint32_t *dsp_frames)
2224 {
2225 (void)stream;
2226 *dsp_frames = 0;
2227 return -EINVAL;
2228 }
2229
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)2230 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
2231 {
2232 (void)stream;
2233 (void)effect;
2234 return 0;
2235 }
2236
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)2237 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
2238 {
2239 (void)stream;
2240 (void)effect;
2241 return 0;
2242 }
2243
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)2244 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
2245 int64_t *timestamp)
2246 {
2247 (void)stream;
2248 (void)timestamp;
2249 return -EINVAL;
2250 }
2251
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)2252 static int out_get_presentation_position(const struct audio_stream_out *stream,
2253 uint64_t *frames, struct timespec *timestamp)
2254 {
2255 struct stream_out *out = (struct stream_out *)stream;
2256 int ret = -1;
2257 unsigned long dsp_frames;
2258
2259 lock_output_stream(out);
2260
2261 /* FIXME: which device to read from? */
2262 if (!list_empty(&out->pcm_dev_list)) {
2263 unsigned int avail;
2264 struct pcm_device *pcm_device = node_to_item(list_head(&out->pcm_dev_list),
2265 struct pcm_device, stream_list_node);
2266
2267 if (pcm_get_htimestamp(pcm_device->pcm, &avail, timestamp) == 0) {
2268 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
2269 int64_t signed_frames = out->written - kernel_buffer_size + avail;
2270 /* This adjustment accounts for buffering after app processor.
2271 It is based on estimated DSP latency per use case, rather than exact. */
2272 signed_frames -=
2273 (render_latency(out->usecase) * out->sample_rate / 1000000LL);
2274
2275 /* It would be unusual for this value to be negative, but check just in case ... */
2276 if (signed_frames >= 0) {
2277 *frames = signed_frames;
2278 ret = 0;
2279 }
2280 }
2281 }
2282
2283 pthread_mutex_unlock(&out->lock);
2284
2285 return ret;
2286 }
2287
2288 /** audio_stream_in implementation **/
in_get_sample_rate(const struct audio_stream * stream)2289 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2290 {
2291 struct stream_in *in = (struct stream_in *)stream;
2292
2293 return in->requested_rate;
2294 }
2295
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)2296 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
2297 {
2298 (void)stream;
2299 (void)rate;
2300 return -ENOSYS;
2301 }
2302
in_get_channels(const struct audio_stream * stream)2303 static uint32_t in_get_channels(const struct audio_stream *stream)
2304 {
2305 struct stream_in *in = (struct stream_in *)stream;
2306
2307 return in->main_channels;
2308 }
2309
in_get_format(const struct audio_stream * stream)2310 static audio_format_t in_get_format(const struct audio_stream *stream)
2311 {
2312 (void)stream;
2313 return AUDIO_FORMAT_PCM_16_BIT;
2314 }
2315
in_set_format(struct audio_stream * stream,audio_format_t format)2316 static int in_set_format(struct audio_stream *stream, audio_format_t format)
2317 {
2318 (void)stream;
2319 (void)format;
2320
2321 return -ENOSYS;
2322 }
2323
in_get_buffer_size(const struct audio_stream * stream)2324 static size_t in_get_buffer_size(const struct audio_stream *stream)
2325 {
2326 struct stream_in *in = (struct stream_in *)stream;
2327
2328 return get_input_buffer_size(in->requested_rate,
2329 in_get_format(stream),
2330 audio_channel_count_from_in_mask(in->main_channels),
2331 in->usecase_type,
2332 in->devices);
2333 }
2334
in_close_pcm_devices(struct stream_in * in)2335 static int in_close_pcm_devices(struct stream_in *in)
2336 {
2337 struct pcm_device *pcm_device;
2338 struct listnode *node;
2339 struct audio_device *adev = in->dev;
2340
2341 list_for_each(node, &in->pcm_dev_list) {
2342 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
2343 if (pcm_device) {
2344 if (pcm_device->pcm)
2345 pcm_close(pcm_device->pcm);
2346 pcm_device->pcm = NULL;
2347 if (pcm_device->sound_trigger_handle > 0)
2348 adev->sound_trigger_close_for_streaming(
2349 pcm_device->sound_trigger_handle);
2350 pcm_device->sound_trigger_handle = 0;
2351 }
2352 }
2353 return 0;
2354 }
2355
2356
2357 /* must be called with stream and hw device mutex locked */
do_in_standby_l(struct stream_in * in)2358 static int do_in_standby_l(struct stream_in *in)
2359 {
2360 int status = 0;
2361
2362 if (!in->standby) {
2363
2364 in_close_pcm_devices(in);
2365
2366 status = stop_input_stream(in);
2367
2368 if (in->read_buf) {
2369 free(in->read_buf);
2370 in->read_buf = NULL;
2371 }
2372
2373 in->standby = 1;
2374 }
2375 return 0;
2376 }
2377
2378 // called with adev->lock_inputs locked
in_standby_l(struct stream_in * in)2379 static int in_standby_l(struct stream_in *in)
2380 {
2381 struct audio_device *adev = in->dev;
2382 int status = 0;
2383 lock_input_stream(in);
2384 if (!in->standby) {
2385 pthread_mutex_lock(&adev->lock);
2386 status = do_in_standby_l(in);
2387 pthread_mutex_unlock(&adev->lock);
2388 }
2389 pthread_mutex_unlock(&in->lock);
2390 return status;
2391 }
2392
in_standby(struct audio_stream * stream)2393 static int in_standby(struct audio_stream *stream)
2394 {
2395 struct stream_in *in = (struct stream_in *)stream;
2396 struct audio_device *adev = in->dev;
2397 int status;
2398 ALOGV("%s: enter", __func__);
2399 pthread_mutex_lock(&adev->lock_inputs);
2400 status = in_standby_l(in);
2401 pthread_mutex_unlock(&adev->lock_inputs);
2402 ALOGV("%s: exit: status(%d)", __func__, status);
2403 return status;
2404 }
2405
in_dump(const struct audio_stream * stream,int fd)2406 static int in_dump(const struct audio_stream *stream, int fd)
2407 {
2408 (void)stream;
2409 (void)fd;
2410
2411 return 0;
2412 }
2413
in_set_parameters(struct audio_stream * stream,const char * kvpairs)2414 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2415 {
2416 struct stream_in *in = (struct stream_in *)stream;
2417 struct audio_device *adev = in->dev;
2418 struct str_parms *parms;
2419 char *str;
2420 char value[32];
2421 int ret, val = 0;
2422 struct audio_usecase *uc_info;
2423 bool do_standby = false;
2424 struct listnode *node;
2425 struct pcm_device *pcm_device;
2426 struct pcm_device_profile *pcm_profile;
2427
2428 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
2429 parms = str_parms_create_str(kvpairs);
2430
2431 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2432
2433 pthread_mutex_lock(&adev->lock_inputs);
2434 lock_input_stream(in);
2435 pthread_mutex_lock(&adev->lock);
2436 if (ret >= 0) {
2437 val = atoi(value);
2438 /* no audio source uses val == 0 */
2439 if (((int)in->source != val) && (val != 0)) {
2440 in->source = val;
2441 }
2442 }
2443
2444 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2445 if (ret >= 0) {
2446 val = atoi(value);
2447 if (((int)in->devices != val) && (val != 0)) {
2448 in->devices = val;
2449 /* If recording is in progress, change the tx device to new device */
2450 if (!in->standby) {
2451 uc_info = get_usecase_from_id(adev, in->usecase);
2452 if (uc_info == NULL) {
2453 ALOGE("%s: Could not find the usecase (%d) in the list",
2454 __func__, in->usecase);
2455 } else {
2456 if (list_empty(&in->pcm_dev_list))
2457 ALOGE("%s: pcm device list empty", __func__);
2458 else {
2459 pcm_device = node_to_item(list_head(&in->pcm_dev_list),
2460 struct pcm_device, stream_list_node);
2461 if ((pcm_device->pcm_profile->devices & val & ~AUDIO_DEVICE_BIT_IN) == 0) {
2462 do_standby = true;
2463 }
2464 }
2465 }
2466 if (do_standby) {
2467 ret = do_in_standby_l(in);
2468 } else
2469 ret = select_devices(adev, in->usecase);
2470 }
2471 }
2472 }
2473 pthread_mutex_unlock(&adev->lock);
2474 pthread_mutex_unlock(&in->lock);
2475 pthread_mutex_unlock(&adev->lock_inputs);
2476 str_parms_destroy(parms);
2477
2478 if (ret > 0)
2479 ret = 0;
2480
2481 return ret;
2482 }
2483
in_get_parameters(const struct audio_stream * stream,const char * keys)2484 static char* in_get_parameters(const struct audio_stream *stream,
2485 const char *keys)
2486 {
2487 (void)stream;
2488 (void)keys;
2489
2490 return strdup("");
2491 }
2492
in_set_gain(struct audio_stream_in * stream,float gain)2493 static int in_set_gain(struct audio_stream_in *stream, float gain)
2494 {
2495 (void)stream;
2496 (void)gain;
2497
2498 return 0;
2499 }
2500
read_bytes_from_dsp(struct stream_in * in,void * buffer,size_t bytes)2501 static ssize_t read_bytes_from_dsp(struct stream_in *in, void* buffer,
2502 size_t bytes)
2503 {
2504 struct pcm_device *pcm_device;
2505 struct audio_device *adev = in->dev;
2506
2507 pcm_device = node_to_item(list_head(&in->pcm_dev_list),
2508 struct pcm_device, stream_list_node);
2509
2510 if (pcm_device->sound_trigger_handle > 0)
2511 return adev->sound_trigger_read_samples(
2512 pcm_device->sound_trigger_handle, buffer, bytes);
2513 else
2514 return 0;
2515 }
2516
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)2517 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2518 size_t bytes)
2519 {
2520 struct stream_in *in = (struct stream_in *)stream;
2521 struct audio_device *adev = in->dev;
2522 ssize_t frames = -1;
2523 int ret = -1;
2524 int read_and_process_successful = false;
2525
2526 size_t frames_rq = bytes / audio_stream_in_frame_size(stream);
2527
2528 /* no need to acquire adev->lock_inputs because API contract prevents a close */
2529 lock_input_stream(in);
2530 if (in->standby) {
2531 pthread_mutex_unlock(&in->lock);
2532 pthread_mutex_lock(&adev->lock_inputs);
2533 lock_input_stream(in);
2534 if (!in->standby) {
2535 pthread_mutex_unlock(&adev->lock_inputs);
2536 goto false_alarm;
2537 }
2538 pthread_mutex_lock(&adev->lock);
2539 ret = start_input_stream(in);
2540 pthread_mutex_unlock(&adev->lock);
2541 pthread_mutex_unlock(&adev->lock_inputs);
2542 if (ret != 0) {
2543 goto exit;
2544 }
2545 in->standby = 0;
2546 }
2547 false_alarm:
2548
2549 if (!list_empty(&in->pcm_dev_list)) {
2550 if (in->usecase == USECASE_AUDIO_CAPTURE_HOTWORD) {
2551 bytes = read_bytes_from_dsp(in, buffer, bytes);
2552 if (bytes > 0)
2553 read_and_process_successful = true;
2554 } else {
2555 /*
2556 * Read PCM and:
2557 * - resample if needed
2558 * - process if pre-processors are attached
2559 * - discard unwanted channels
2560 */
2561 frames = read_and_process_frames(stream, buffer, frames_rq);
2562 if (frames >= 0)
2563 read_and_process_successful = true;
2564 }
2565 }
2566
2567 /*
2568 * Instead of writing zeroes here, we could trust the hardware
2569 * to always provide zeroes when muted.
2570 */
2571 if (read_and_process_successful == true && adev->mic_mute)
2572 memset(buffer, 0, bytes);
2573
2574 exit:
2575 pthread_mutex_unlock(&in->lock);
2576
2577 if (read_and_process_successful == false) {
2578 in_standby(&in->stream.common);
2579 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
2580 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
2581 in->requested_rate);
2582 }
2583 return bytes;
2584 }
2585
in_get_input_frames_lost(struct audio_stream_in * stream)2586 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
2587 {
2588 (void)stream;
2589
2590 return 0;
2591 }
2592
add_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect,bool enable)2593 static int add_remove_audio_effect(const struct audio_stream *stream,
2594 effect_handle_t effect,
2595 bool enable)
2596 {
2597 struct stream_in *in = (struct stream_in *)stream;
2598 struct audio_device *adev = in->dev;
2599 int status = 0;
2600 effect_descriptor_t desc;
2601 #ifdef PREPROCESSING_ENABLED
2602 int i;
2603 #endif
2604 status = (*effect)->get_descriptor(effect, &desc);
2605 if (status != 0)
2606 return status;
2607
2608 ALOGI("add_remove_audio_effect(), effect type: %08x, enable: %d ", desc.type.timeLow, enable);
2609
2610 pthread_mutex_lock(&adev->lock_inputs);
2611 lock_input_stream(in);
2612 pthread_mutex_lock(&in->dev->lock);
2613 #ifndef PREPROCESSING_ENABLED
2614 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2615 in->enable_aec != enable &&
2616 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2617 in->enable_aec = enable;
2618 if (!in->standby)
2619 select_devices(in->dev, in->usecase);
2620 }
2621 #else
2622 if (enable) {
2623 if (in->num_preprocessors >= MAX_PREPROCESSORS) {
2624 status = -ENOSYS;
2625 goto exit;
2626 }
2627 in->preprocessors[in->num_preprocessors].effect_itfe = effect;
2628 in->num_preprocessors ++;
2629 /* check compatibility between main channel supported and possible auxiliary channels */
2630 in_update_aux_channels(in, effect);//wesley crash
2631 in->aux_channels_changed = true;
2632 } else {
2633 /* if ( enable == false ) */
2634 if (in->num_preprocessors <= 0) {
2635 status = -ENOSYS;
2636 goto exit;
2637 }
2638 status = -EINVAL;
2639 for (i = 0; i < in->num_preprocessors && status != 0; i++) {
2640 if ( in->preprocessors[i].effect_itfe == effect ) {
2641 ALOGV("add_remove_audio_effect found fx at index %d", i);
2642 free(in->preprocessors[i].channel_configs);
2643 in->num_preprocessors--;
2644 memcpy(in->preprocessors + i,
2645 in->preprocessors + i + 1,
2646 (in->num_preprocessors - i) * sizeof(in->preprocessors[0]));
2647 memset(in->preprocessors + in->num_preprocessors,
2648 0,
2649 sizeof(in->preprocessors[0]));
2650 status = 0;
2651 }
2652 }
2653 if (status != 0)
2654 goto exit;
2655 in->aux_channels_changed = false;
2656 ALOGV("%s: enable(%d), in->aux_channels_changed(%d)",
2657 __func__, enable, in->aux_channels_changed);
2658 }
2659 ALOGI("%s: num_preprocessors = %d", __func__, in->num_preprocessors);
2660
2661 exit:
2662 #endif
2663 ALOGW_IF(status != 0, "add_remove_audio_effect() error %d", status);
2664 pthread_mutex_unlock(&in->dev->lock);
2665 pthread_mutex_unlock(&in->lock);
2666 pthread_mutex_unlock(&adev->lock_inputs);
2667 return status;
2668 }
2669
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)2670 static int in_add_audio_effect(const struct audio_stream *stream,
2671 effect_handle_t effect)
2672 {
2673 ALOGV("%s: effect %p", __func__, effect);
2674 return add_remove_audio_effect(stream, effect, true /* enabled */);
2675 }
2676
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)2677 static int in_remove_audio_effect(const struct audio_stream *stream,
2678 effect_handle_t effect)
2679 {
2680 ALOGV("%s: effect %p", __func__, effect);
2681 return add_remove_audio_effect(stream, effect, false /* disabled */);
2682 }
2683
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)2684 static int adev_open_output_stream(struct audio_hw_device *dev,
2685 audio_io_handle_t handle,
2686 audio_devices_t devices,
2687 audio_output_flags_t flags,
2688 struct audio_config *config,
2689 struct audio_stream_out **stream_out,
2690 const char *address __unused)
2691 {
2692 struct audio_device *adev = (struct audio_device *)dev;
2693 struct stream_out *out;
2694 int i, ret;
2695 struct pcm_device_profile *pcm_profile;
2696
2697 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
2698 __func__, config->sample_rate, config->channel_mask, devices, flags);
2699 *stream_out = NULL;
2700 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2701
2702 if (devices == AUDIO_DEVICE_NONE)
2703 devices = AUDIO_DEVICE_OUT_SPEAKER;
2704
2705 out->flags = flags;
2706 out->devices = devices;
2707 out->dev = adev;
2708 out->format = config->format;
2709 out->sample_rate = config->sample_rate;
2710 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2711 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
2712 out->handle = handle;
2713
2714 pcm_profile = get_pcm_device(PCM_PLAYBACK, devices);
2715 if (pcm_profile == NULL) {
2716 ret = -EINVAL;
2717 goto error_open;
2718 }
2719 out->config = pcm_profile->config;
2720
2721 /* Init use case and pcm_config */
2722 if (out->flags & (AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) {
2723 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2724 out->config = pcm_config_deep_buffer;
2725 out->sample_rate = out->config.rate;
2726 ALOGV("%s: use AUDIO_PLAYBACK_DEEP_BUFFER",__func__);
2727 } else {
2728 out->usecase = USECASE_AUDIO_PLAYBACK;
2729 out->sample_rate = out->config.rate;
2730 }
2731
2732 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2733 if (adev->primary_output == NULL)
2734 adev->primary_output = out;
2735 else {
2736 ALOGE("%s: Primary output is already opened", __func__);
2737 ret = -EEXIST;
2738 goto error_open;
2739 }
2740 }
2741
2742 /* Check if this usecase is already existing */
2743 pthread_mutex_lock(&adev->lock);
2744 if (get_usecase_from_id(adev, out->usecase) != NULL) {
2745 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
2746 pthread_mutex_unlock(&adev->lock);
2747 ret = -EEXIST;
2748 goto error_open;
2749 }
2750 pthread_mutex_unlock(&adev->lock);
2751
2752 out->stream.common.get_sample_rate = out_get_sample_rate;
2753 out->stream.common.set_sample_rate = out_set_sample_rate;
2754 out->stream.common.get_buffer_size = out_get_buffer_size;
2755 out->stream.common.get_channels = out_get_channels;
2756 out->stream.common.get_format = out_get_format;
2757 out->stream.common.set_format = out_set_format;
2758 out->stream.common.standby = out_standby;
2759 out->stream.common.dump = out_dump;
2760 out->stream.common.set_parameters = out_set_parameters;
2761 out->stream.common.get_parameters = out_get_parameters;
2762 out->stream.common.add_audio_effect = out_add_audio_effect;
2763 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2764 out->stream.get_latency = out_get_latency;
2765 out->stream.set_volume = out_set_volume;
2766 out->stream.write = out_write;
2767 out->stream.get_render_position = out_get_render_position;
2768 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
2769 out->stream.get_presentation_position = out_get_presentation_position;
2770
2771 out->standby = 1;
2772 /* out->muted = false; by calloc() */
2773 /* out->written = 0; by calloc() */
2774
2775 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2776 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
2777 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2778
2779 config->format = out->stream.common.get_format(&out->stream.common);
2780 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2781 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2782
2783 *stream_out = &out->stream;
2784 ALOGV("%s: exit", __func__);
2785 return 0;
2786
2787 error_open:
2788 free(out);
2789 *stream_out = NULL;
2790 ALOGV("%s: exit: ret %d", __func__, ret);
2791 return ret;
2792 }
2793
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)2794 static void adev_close_output_stream(struct audio_hw_device *dev,
2795 struct audio_stream_out *stream)
2796 {
2797 struct stream_out *out = (struct stream_out *)stream;
2798 struct audio_device *adev = out->dev;
2799 (void)dev;
2800
2801 ALOGV("%s: enter", __func__);
2802 out_standby(&stream->common);
2803 pthread_cond_destroy(&out->cond);
2804 pthread_mutex_destroy(&out->lock);
2805 pthread_mutex_destroy(&out->pre_lock);
2806 free(out->proc_buf_out);
2807 free(stream);
2808 ALOGV("%s: exit", __func__);
2809 }
2810
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)2811 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2812 {
2813 struct audio_device *adev = (struct audio_device *)dev;
2814 struct str_parms *parms;
2815 char *str;
2816 char value[32];
2817 int val;
2818 int ret;
2819
2820 ALOGV("%s: enter: %s", __func__, kvpairs);
2821
2822 parms = str_parms_create_str(kvpairs);
2823 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
2824 if (ret >= 0) {
2825 int tty_mode;
2826
2827 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
2828 tty_mode = TTY_MODE_OFF;
2829 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
2830 tty_mode = TTY_MODE_VCO;
2831 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
2832 tty_mode = TTY_MODE_HCO;
2833 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
2834 tty_mode = TTY_MODE_FULL;
2835 else
2836 return -EINVAL;
2837
2838 pthread_mutex_lock(&adev->lock);
2839 if (tty_mode != adev->tty_mode) {
2840 adev->tty_mode = tty_mode;
2841 if (adev->in_call)
2842 select_devices(adev, USECASE_VOICE_CALL);
2843 }
2844 pthread_mutex_unlock(&adev->lock);
2845 }
2846
2847 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2848 if (ret >= 0) {
2849 /* When set to false, HAL should disable EC and NS
2850 * But it is currently not supported.
2851 */
2852 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2853 adev->bluetooth_nrec = true;
2854 else
2855 adev->bluetooth_nrec = false;
2856 }
2857
2858 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2859 if (ret >= 0) {
2860 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2861 adev->screen_off = false;
2862 else
2863 adev->screen_off = true;
2864 }
2865
2866 ret = str_parms_get_int(parms, "rotation", &val);
2867 if (ret >= 0) {
2868 bool reverse_speakers = false;
2869 switch(val) {
2870 /* FIXME: note that the code below assumes that the speakers are in the correct placement
2871 relative to the user when the device is rotated 90deg from its default rotation. This
2872 assumption is device-specific, not platform-specific like this code. */
2873 case 270:
2874 reverse_speakers = true;
2875 break;
2876 case 0:
2877 case 90:
2878 case 180:
2879 break;
2880 default:
2881 ALOGE("%s: unexpected rotation of %d", __func__, val);
2882 }
2883 pthread_mutex_lock(&adev->lock);
2884 if (adev->speaker_lr_swap != reverse_speakers) {
2885 adev->speaker_lr_swap = reverse_speakers;
2886 /* only update the selected device if there is active pcm playback */
2887 struct audio_usecase *usecase;
2888 struct listnode *node;
2889 list_for_each(node, &adev->usecase_list) {
2890 usecase = node_to_item(node, struct audio_usecase, adev_list_node);
2891 if (usecase->type == PCM_PLAYBACK) {
2892 select_devices(adev, usecase->id);
2893 /* TODO(dgreid) speaker flip */
2894 break;
2895 }
2896 }
2897 }
2898 pthread_mutex_unlock(&adev->lock);
2899 }
2900
2901 str_parms_destroy(parms);
2902 ALOGV("%s: exit with code(%d)", __func__, ret);
2903 return ret;
2904 }
2905
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)2906 static char* adev_get_parameters(const struct audio_hw_device *dev,
2907 const char *keys)
2908 {
2909 (void)dev;
2910 (void)keys;
2911
2912 return strdup("");
2913 }
2914
adev_init_check(const struct audio_hw_device * dev)2915 static int adev_init_check(const struct audio_hw_device *dev)
2916 {
2917 (void)dev;
2918
2919 return 0;
2920 }
2921
adev_set_voice_volume(struct audio_hw_device * dev,float volume)2922 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2923 {
2924 int ret = 0;
2925 struct audio_device *adev = (struct audio_device *)dev;
2926 pthread_mutex_lock(&adev->lock);
2927 /* cache volume */
2928 adev->voice_volume = volume;
2929 ret = set_voice_volume_l(adev, adev->voice_volume);
2930 pthread_mutex_unlock(&adev->lock);
2931 return ret;
2932 }
2933
adev_set_master_volume(struct audio_hw_device * dev,float volume)2934 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2935 {
2936 (void)dev;
2937 (void)volume;
2938
2939 return -ENOSYS;
2940 }
2941
adev_get_master_volume(struct audio_hw_device * dev,float * volume)2942 static int adev_get_master_volume(struct audio_hw_device *dev,
2943 float *volume)
2944 {
2945 (void)dev;
2946 (void)volume;
2947
2948 return -ENOSYS;
2949 }
2950
adev_set_master_mute(struct audio_hw_device * dev,bool muted)2951 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2952 {
2953 (void)dev;
2954 (void)muted;
2955
2956 return -ENOSYS;
2957 }
2958
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)2959 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2960 {
2961 (void)dev;
2962 (void)muted;
2963
2964 return -ENOSYS;
2965 }
2966
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)2967 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2968 {
2969 struct audio_device *adev = (struct audio_device *)dev;
2970
2971 pthread_mutex_lock(&adev->lock);
2972 if (adev->mode != mode) {
2973 ALOGI("%s mode = %d", __func__, mode);
2974 adev->mode = mode;
2975 }
2976 pthread_mutex_unlock(&adev->lock);
2977 return 0;
2978 }
2979
adev_set_mic_mute(struct audio_hw_device * dev,bool state)2980 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2981 {
2982 struct audio_device *adev = (struct audio_device *)dev;
2983 int err = 0;
2984
2985 pthread_mutex_lock(&adev->lock);
2986 adev->mic_mute = state;
2987
2988 if (adev->mode == AUDIO_MODE_IN_CALL) {
2989 /* TODO */
2990 }
2991
2992 pthread_mutex_unlock(&adev->lock);
2993 return err;
2994 }
2995
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)2996 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2997 {
2998 struct audio_device *adev = (struct audio_device *)dev;
2999
3000 *state = adev->mic_mute;
3001
3002 return 0;
3003 }
3004
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)3005 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
3006 const struct audio_config *config)
3007 {
3008 (void)dev;
3009
3010 /* NOTE: we default to built in mic which may cause a mismatch between what we
3011 * report here and the actual buffer size
3012 */
3013 return get_input_buffer_size(config->sample_rate,
3014 config->format,
3015 audio_channel_count_from_in_mask(config->channel_mask),
3016 PCM_CAPTURE /* usecase_type */,
3017 AUDIO_DEVICE_IN_BUILTIN_MIC);
3018 }
3019
adev_open_input_stream(struct audio_hw_device * dev,audio_io_handle_t handle __unused,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)3020 static int adev_open_input_stream(struct audio_hw_device *dev,
3021 audio_io_handle_t handle __unused,
3022 audio_devices_t devices,
3023 struct audio_config *config,
3024 struct audio_stream_in **stream_in,
3025 audio_input_flags_t flags,
3026 const char *address __unused,
3027 audio_source_t source)
3028 {
3029 struct audio_device *adev = (struct audio_device *)dev;
3030 struct stream_in *in;
3031 struct pcm_device_profile *pcm_profile;
3032
3033 ALOGV("%s: enter", __func__);
3034
3035 *stream_in = NULL;
3036 if (check_input_parameters(config->sample_rate, config->format,
3037 audio_channel_count_from_in_mask(config->channel_mask)) != 0)
3038 return -EINVAL;
3039
3040 usecase_type_t usecase_type = (source == AUDIO_SOURCE_HOTWORD) ?
3041 PCM_HOTWORD_STREAMING : PCM_CAPTURE;
3042 pcm_profile = get_pcm_device(usecase_type, devices);
3043 if (pcm_profile == NULL)
3044 return -EINVAL;
3045
3046 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
3047
3048 in->stream.common.get_sample_rate = in_get_sample_rate;
3049 in->stream.common.set_sample_rate = in_set_sample_rate;
3050 in->stream.common.get_buffer_size = in_get_buffer_size;
3051 in->stream.common.get_channels = in_get_channels;
3052 in->stream.common.get_format = in_get_format;
3053 in->stream.common.set_format = in_set_format;
3054 in->stream.common.standby = in_standby;
3055 in->stream.common.dump = in_dump;
3056 in->stream.common.set_parameters = in_set_parameters;
3057 in->stream.common.get_parameters = in_get_parameters;
3058 in->stream.common.add_audio_effect = in_add_audio_effect;
3059 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3060 in->stream.set_gain = in_set_gain;
3061 in->stream.read = in_read;
3062 in->stream.get_input_frames_lost = in_get_input_frames_lost;
3063
3064 in->devices = devices;
3065 in->source = source;
3066 in->dev = adev;
3067 in->standby = 1;
3068 in->main_channels = config->channel_mask;
3069 in->requested_rate = config->sample_rate;
3070 if (config->sample_rate != CAPTURE_DEFAULT_SAMPLING_RATE)
3071 flags = flags & ~AUDIO_INPUT_FLAG_FAST;
3072 in->input_flags = flags;
3073 /* HW codec is limited to default channels. No need to update with
3074 * requested channels */
3075 in->config = pcm_profile->config;
3076
3077 /* Update config params with the requested sample rate and channels */
3078 if (source == AUDIO_SOURCE_HOTWORD) {
3079 in->usecase = USECASE_AUDIO_CAPTURE_HOTWORD;
3080 } else {
3081 in->usecase = USECASE_AUDIO_CAPTURE;
3082 }
3083 in->usecase_type = usecase_type;
3084
3085 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
3086 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
3087
3088 *stream_in = &in->stream;
3089 ALOGV("%s: exit", __func__);
3090 return 0;
3091 }
3092
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * stream)3093 static void adev_close_input_stream(struct audio_hw_device *dev,
3094 struct audio_stream_in *stream)
3095 {
3096 struct audio_device *adev = (struct audio_device *)dev;
3097 struct stream_in *in = (struct stream_in*)stream;
3098 ALOGV("%s", __func__);
3099
3100 /* prevent concurrent out_set_parameters, or out_write from standby */
3101 pthread_mutex_lock(&adev->lock_inputs);
3102
3103 in_standby_l(in);
3104 pthread_mutex_destroy(&in->lock);
3105 pthread_mutex_destroy(&in->pre_lock);
3106 free(in->proc_buf_out);
3107
3108 #ifdef PREPROCESSING_ENABLED
3109 int i;
3110
3111 for (i=0; i<in->num_preprocessors; i++) {
3112 free(in->preprocessors[i].channel_configs);
3113 }
3114
3115 if (in->read_buf) {
3116 free(in->read_buf);
3117 }
3118
3119 if (in->proc_buf_in) {
3120 free(in->proc_buf_in);
3121 }
3122
3123 if (in->resampler) {
3124 release_resampler(in->resampler);
3125 }
3126 #endif
3127
3128 free(stream);
3129
3130 pthread_mutex_unlock(&adev->lock_inputs);
3131
3132 return;
3133 }
3134
adev_dump(const audio_hw_device_t * device,int fd)3135 static int adev_dump(const audio_hw_device_t *device, int fd)
3136 {
3137 (void)device;
3138 (void)fd;
3139
3140 return 0;
3141 }
3142
adev_close(hw_device_t * device)3143 static int adev_close(hw_device_t *device)
3144 {
3145 struct audio_device *adev = (struct audio_device *)device;
3146 free(adev->snd_dev_ref_cnt);
3147 free_mixer_list(adev);
3148 free(device);
3149 return 0;
3150 }
3151
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)3152 static int adev_open(const hw_module_t *module, const char *name,
3153 hw_device_t **device)
3154 {
3155 struct audio_device *adev;
3156 int i, ret, retry_count;
3157
3158 ALOGV("%s: enter", __func__);
3159 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3160
3161 adev = calloc(1, sizeof(struct audio_device));
3162
3163 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3164 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3165 adev->device.common.module = (struct hw_module_t *)module;
3166 adev->device.common.close = adev_close;
3167
3168 adev->device.init_check = adev_init_check;
3169 adev->device.set_voice_volume = adev_set_voice_volume;
3170 adev->device.set_master_volume = adev_set_master_volume;
3171 adev->device.get_master_volume = adev_get_master_volume;
3172 adev->device.set_master_mute = adev_set_master_mute;
3173 adev->device.get_master_mute = adev_get_master_mute;
3174 adev->device.set_mode = adev_set_mode;
3175 adev->device.set_mic_mute = adev_set_mic_mute;
3176 adev->device.get_mic_mute = adev_get_mic_mute;
3177 adev->device.set_parameters = adev_set_parameters;
3178 adev->device.get_parameters = adev_get_parameters;
3179 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3180 adev->device.open_output_stream = adev_open_output_stream;
3181 adev->device.close_output_stream = adev_close_output_stream;
3182 adev->device.open_input_stream = adev_open_input_stream;
3183 adev->device.close_input_stream = adev_close_input_stream;
3184 adev->device.dump = adev_dump;
3185
3186 /* Set the default route before the PCM stream is opened */
3187 adev->mode = AUDIO_MODE_NORMAL;
3188 adev->active_input = NULL;
3189 adev->primary_output = NULL;
3190 adev->voice_volume = 1.0f;
3191 adev->tty_mode = TTY_MODE_OFF;
3192 adev->bluetooth_nrec = true;
3193 adev->in_call = false;
3194 /* adev->cur_hdmi_channels = 0; by calloc() */
3195 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
3196
3197 adev->dualmic_config = DUALMIC_CONFIG_NONE;
3198 adev->ns_in_voice_rec = false;
3199
3200 list_init(&adev->usecase_list);
3201
3202 if (mixer_init(adev) != 0) {
3203 free(adev->snd_dev_ref_cnt);
3204 free(adev);
3205 ALOGE("%s: Failed to init, aborting.", __func__);
3206 *device = NULL;
3207 return -EINVAL;
3208 }
3209
3210
3211 if (access(SOUND_TRIGGER_HAL_LIBRARY_PATH, R_OK) == 0) {
3212 adev->sound_trigger_lib = dlopen(SOUND_TRIGGER_HAL_LIBRARY_PATH,
3213 RTLD_NOW);
3214 if (adev->sound_trigger_lib == NULL) {
3215 ALOGE("%s: DLOPEN failed for %s", __func__,
3216 SOUND_TRIGGER_HAL_LIBRARY_PATH);
3217 } else {
3218 ALOGV("%s: DLOPEN successful for %s", __func__,
3219 SOUND_TRIGGER_HAL_LIBRARY_PATH);
3220 adev->sound_trigger_open_for_streaming =
3221 (int (*)(void))dlsym(adev->sound_trigger_lib,
3222 "sound_trigger_open_for_streaming");
3223 adev->sound_trigger_read_samples =
3224 (size_t (*)(int, void *, size_t))dlsym(
3225 adev->sound_trigger_lib,
3226 "sound_trigger_read_samples");
3227 adev->sound_trigger_close_for_streaming =
3228 (int (*)(int))dlsym(
3229 adev->sound_trigger_lib,
3230 "sound_trigger_close_for_streaming");
3231 if (!adev->sound_trigger_open_for_streaming ||
3232 !adev->sound_trigger_read_samples ||
3233 !adev->sound_trigger_close_for_streaming) {
3234
3235 ALOGE("%s: Error grabbing functions in %s", __func__,
3236 SOUND_TRIGGER_HAL_LIBRARY_PATH);
3237 adev->sound_trigger_open_for_streaming = 0;
3238 adev->sound_trigger_read_samples = 0;
3239 adev->sound_trigger_close_for_streaming = 0;
3240 }
3241 }
3242 }
3243
3244 *device = &adev->device.common;
3245
3246 cras_dsp_init("/system/etc/cras/speakerdsp.ini");
3247
3248 ALOGV("%s: exit", __func__);
3249 return 0;
3250 }
3251
3252 static struct hw_module_methods_t hal_module_methods = {
3253 .open = adev_open,
3254 };
3255
3256 struct audio_module HAL_MODULE_INFO_SYM = {
3257 .common = {
3258 .tag = HARDWARE_MODULE_TAG,
3259 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3260 .hal_api_version = HARDWARE_HAL_API_VERSION,
3261 .id = AUDIO_HARDWARE_MODULE_ID,
3262 .name = "NVIDIA Tegra Audio HAL",
3263 .author = "The Android Open Source Project",
3264 .methods = &hal_module_methods,
3265 },
3266 };
3267