1 /*
2 * Copyright (C) 2013 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 #define _GNU_SOURCE
27 #include <errno.h>
28 #include <pthread.h>
29 #include <stdint.h>
30 #include <sys/time.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <dlfcn.h>
34 #include <sys/resource.h>
35 #include <sys/prctl.h>
36
37 #include <cutils/log.h>
38 #include <cutils/str_parms.h>
39 #include <cutils/atomic.h>
40 #include <cutils/sched_policy.h>
41 #include <cutils/properties.h>
42
43 #include <hardware/audio_effect.h>
44 #include <system/thread_defs.h>
45 #include <audio_effects/effect_aec.h>
46 #include <audio_effects/effect_ns.h>
47 #include "audio_hw.h"
48
49 #include "sound/compress_params.h"
50
51 #define MIXER_CTL_COMPRESS_PLAYBACK_VOLUME "Compress Playback Volume"
52 #define MIXER_CTL_HEADPHONE_JACK_SWITCH "Headphone Jack Switch"
53 #define MIXER_CTL_CODEC_VMIXER_CODEC_SWITCH "Codec VMixer Codec Switch"
54 #define MIXER_CTL_SPK_VMIXER_SPK_SWITCH "SPK VMixer SPK Switch"
55
56 /* TODO: the following PCM device profiles could be read from a config file */
57 static struct pcm_device_profile pcm_device_playback = {
58 .config = {
59 .channels = PLAYBACK_DEFAULT_CHANNEL_COUNT,
60 .rate = PLAYBACK_DEFAULT_SAMPLING_RATE,
61 .period_size = PLAYBACK_PERIOD_SIZE,
62 .period_count = PLAYBACK_PERIOD_COUNT,
63 .format = PCM_FORMAT_S16_LE,
64 .start_threshold = PLAYBACK_START_THRESHOLD(PLAYBACK_PERIOD_SIZE, PLAYBACK_PERIOD_COUNT),
65 .stop_threshold = PLAYBACK_STOP_THRESHOLD(PLAYBACK_PERIOD_SIZE, PLAYBACK_PERIOD_COUNT),
66 .silence_threshold = 0,
67 .silence_size = UINT_MAX,
68 .avail_min = PLAYBACK_AVAILABLE_MIN,
69 },
70 .card = SOUND_CARD,
71 .id = 9,
72 .type = PCM_PLAYBACK,
73 .devices = AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE|
74 AUDIO_DEVICE_OUT_SPEAKER,
75 };
76
77 static struct pcm_device_profile pcm_device_capture = {
78 .config = {
79 .channels = CAPTURE_DEFAULT_CHANNEL_COUNT,
80 .rate = CAPTURE_DEFAULT_SAMPLING_RATE,
81 .period_size = CAPTURE_PERIOD_SIZE,
82 .period_count = CAPTURE_PERIOD_COUNT,
83 .format = PCM_FORMAT_S16_LE,
84 .start_threshold = CAPTURE_START_THRESHOLD,
85 .stop_threshold = 0,
86 .silence_threshold = 0,
87 .avail_min = 0,
88 },
89 .card = SOUND_CARD,
90 .id = 0,
91 .type = PCM_CAPTURE,
92 .devices = AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_BACK_MIC,
93 };
94
95 static struct pcm_device_profile pcm_device_capture_low_latency = {
96 .config = {
97 .channels = CAPTURE_DEFAULT_CHANNEL_COUNT,
98 .rate = CAPTURE_DEFAULT_SAMPLING_RATE,
99 .period_size = CAPTURE_PERIOD_SIZE_LOW_LATENCY,
100 .period_count = CAPTURE_PERIOD_COUNT_LOW_LATENCY,
101 .format = PCM_FORMAT_S16_LE,
102 .start_threshold = CAPTURE_START_THRESHOLD,
103 .stop_threshold = 0,
104 .silence_threshold = 0,
105 .avail_min = 0,
106 },
107 .card = SOUND_CARD,
108 .id = 0,
109 .type = PCM_CAPTURE_LOW_LATENCY,
110 .devices = AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_BACK_MIC,
111 };
112
113 static struct pcm_device_profile pcm_device_capture_loopback_aec = {
114 .config = {
115 .channels = CAPTURE_DEFAULT_CHANNEL_COUNT,
116 .rate = CAPTURE_DEFAULT_SAMPLING_RATE,
117 .period_size = CAPTURE_PERIOD_SIZE,
118 .period_count = CAPTURE_PERIOD_COUNT,
119 .format = PCM_FORMAT_S16_LE,
120 .start_threshold = CAPTURE_START_THRESHOLD,
121 .stop_threshold = 0,
122 .silence_threshold = 0,
123 .avail_min = 0,
124 },
125 .card = SOUND_CARD,
126 .id = 1,
127 .type = PCM_CAPTURE,
128 .devices = SND_DEVICE_IN_LOOPBACK_AEC,
129 };
130
131 static struct pcm_device_profile pcm_device_playback_sco = {
132 .config = {
133 .channels = SCO_DEFAULT_CHANNEL_COUNT,
134 .rate = SCO_DEFAULT_SAMPLING_RATE,
135 .period_size = SCO_PERIOD_SIZE,
136 .period_count = SCO_PERIOD_COUNT,
137 .format = PCM_FORMAT_S16_LE,
138 .start_threshold = SCO_START_THRESHOLD,
139 .stop_threshold = SCO_STOP_THRESHOLD,
140 .silence_threshold = 0,
141 .avail_min = SCO_AVAILABLE_MIN,
142 },
143 .card = SOUND_CARD,
144 .id = 2,
145 .type = PCM_PLAYBACK,
146 .devices =
147 AUDIO_DEVICE_OUT_BLUETOOTH_SCO|AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET|
148 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT,
149 };
150
151 static struct pcm_device_profile pcm_device_capture_sco = {
152 .config = {
153 .channels = SCO_DEFAULT_CHANNEL_COUNT,
154 .rate = SCO_DEFAULT_SAMPLING_RATE,
155 .period_size = SCO_PERIOD_SIZE,
156 .period_count = SCO_PERIOD_COUNT,
157 .format = PCM_FORMAT_S16_LE,
158 .start_threshold = CAPTURE_START_THRESHOLD,
159 .stop_threshold = 0,
160 .silence_threshold = 0,
161 .avail_min = 0,
162 },
163 .card = SOUND_CARD,
164 .id = 2,
165 .type = PCM_CAPTURE,
166 .devices = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,
167 };
168
169 static struct pcm_device_profile pcm_device_hotword_streaming = {
170 .config = {
171 .channels = 1,
172 .rate = 16000,
173 .period_size = CAPTURE_PERIOD_SIZE,
174 .period_count = CAPTURE_PERIOD_COUNT,
175 .format = PCM_FORMAT_S16_LE,
176 .start_threshold = CAPTURE_START_THRESHOLD,
177 .stop_threshold = 0,
178 .silence_threshold = 0,
179 .avail_min = 0,
180 },
181 .card = SOUND_CARD,
182 .id = 0,
183 .type = PCM_HOTWORD_STREAMING,
184 .devices = AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_BACK_MIC
185 };
186
187 static struct pcm_device_profile * const pcm_devices[] = {
188 &pcm_device_playback,
189 &pcm_device_capture,
190 &pcm_device_capture_low_latency,
191 &pcm_device_playback_sco,
192 &pcm_device_capture_sco,
193 &pcm_device_capture_loopback_aec,
194 &pcm_device_hotword_streaming,
195 NULL,
196 };
197
198 static const char * const use_case_table[AUDIO_USECASE_MAX] = {
199 [USECASE_AUDIO_PLAYBACK] = "playback",
200 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "playback multi-channel",
201 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
202 [USECASE_AUDIO_CAPTURE] = "capture",
203 [USECASE_AUDIO_CAPTURE_HOTWORD] = "capture-hotword",
204 [USECASE_VOICE_CALL] = "voice-call",
205 };
206
207
208 #define STRING_TO_ENUM(string) { #string, string }
209
210 static unsigned int audio_device_ref_count;
211
212 static struct pcm_config pcm_config_deep_buffer = {
213 .channels = 2,
214 .rate = DEEP_BUFFER_OUTPUT_SAMPLING_RATE,
215 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
216 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
217 .format = PCM_FORMAT_S16_LE,
218 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
219 .stop_threshold = INT_MAX,
220 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
221 };
222
223 struct string_to_enum {
224 const char *name;
225 uint32_t value;
226 };
227
228 static const struct string_to_enum out_channels_name_to_enum_table[] = {
229 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
230 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
231 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
232 };
233
234 static void dummybuf_thread_close(struct audio_device *adev);
235
is_supported_format(audio_format_t format)236 static bool is_supported_format(audio_format_t format)
237 {
238 if (format == AUDIO_FORMAT_MP3 ||
239 ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC))
240 return true;
241
242 return false;
243 }
244
get_snd_codec_id(audio_format_t format)245 static int get_snd_codec_id(audio_format_t format)
246 {
247 int id = 0;
248
249 switch (format & AUDIO_FORMAT_MAIN_MASK) {
250 case AUDIO_FORMAT_MP3:
251 id = SND_AUDIOCODEC_MP3;
252 break;
253 case AUDIO_FORMAT_AAC:
254 id = SND_AUDIOCODEC_AAC;
255 break;
256 default:
257 ALOGE("%s: Unsupported audio format", __func__);
258 }
259
260 return id;
261 }
262
263 /* Array to store sound devices */
264 static const char * const device_table[SND_DEVICE_MAX] = {
265 [SND_DEVICE_NONE] = "none",
266 /* Playback sound devices */
267 [SND_DEVICE_OUT_HANDSET] = "handset",
268 [SND_DEVICE_OUT_SPEAKER] = "speaker",
269 [SND_DEVICE_OUT_HEADPHONES] = "headphones",
270 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
271 [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset",
272 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
273 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
274 [SND_DEVICE_OUT_HDMI] = "hdmi",
275 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
276 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
277 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
278 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
279 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
280
281 /* Capture sound devices */
282 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
283 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
284 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
285 [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
286 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic",
287 [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
288 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
289 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
290 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
291 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
292 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
293 [SND_DEVICE_IN_VOICE_DMIC_1] = "voice-dmic-1",
294 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_1] = "voice-speaker-dmic-1",
295 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
296 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
297 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
298 [SND_DEVICE_IN_VOICE_REC_HEADSET_MIC] = "voice-rec-headset-mic",
299 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
300 [SND_DEVICE_IN_VOICE_REC_DMIC_1] = "voice-rec-dmic-1",
301 [SND_DEVICE_IN_VOICE_REC_DMIC_NS_1] = "voice-rec-dmic-ns-1",
302 [SND_DEVICE_IN_LOOPBACK_AEC] = "loopback-aec",
303 };
304
adev_get_mixer_for_card(struct audio_device * adev,int card)305 static struct mixer_card *adev_get_mixer_for_card(struct audio_device *adev, int card)
306 {
307 struct mixer_card *mixer_card;
308 struct listnode *node;
309
310 list_for_each(node, &adev->mixer_list) {
311 mixer_card = node_to_item(node, struct mixer_card, adev_list_node);
312 if (mixer_card->card == card)
313 return mixer_card;
314 }
315 return NULL;
316 }
317
uc_get_mixer_for_card(struct audio_usecase * usecase,int card)318 static struct mixer_card *uc_get_mixer_for_card(struct audio_usecase *usecase, int card)
319 {
320 struct mixer_card *mixer_card;
321 struct listnode *node;
322
323 list_for_each(node, &usecase->mixer_list) {
324 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[usecase->id]);
325 if (mixer_card->card == card)
326 return mixer_card;
327 }
328 return NULL;
329 }
330
free_mixer_list(struct audio_device * adev)331 static void free_mixer_list(struct audio_device *adev)
332 {
333 struct mixer_card *mixer_card;
334 struct listnode *node;
335 struct listnode *next;
336
337 list_for_each_safe(node, next, &adev->mixer_list) {
338 mixer_card = node_to_item(node, struct mixer_card, adev_list_node);
339 list_remove(node);
340 audio_route_free(mixer_card->audio_route);
341 free(mixer_card);
342 }
343 }
344
mixer_init(struct audio_device * adev)345 static int mixer_init(struct audio_device *adev)
346 {
347 int i;
348 int card;
349 int retry_num;
350 struct mixer *mixer;
351 struct audio_route *audio_route;
352 char mixer_path[PATH_MAX];
353 struct mixer_card *mixer_card;
354 struct listnode *node;
355
356 list_init(&adev->mixer_list);
357
358 for (i = 0; pcm_devices[i] != NULL; i++) {
359 card = pcm_devices[i]->card;
360 if (adev_get_mixer_for_card(adev, card) == NULL) {
361 retry_num = 0;
362 do {
363 mixer = mixer_open(card);
364 if (mixer == NULL) {
365 if (++retry_num > RETRY_NUMBER) {
366 ALOGE("%s unable to open the mixer for--card %d, aborting.",
367 __func__, card);
368 goto error;
369 }
370 usleep(RETRY_US);
371 }
372 } while (mixer == NULL);
373
374 sprintf(mixer_path, "/system/etc/mixer_paths_%d.xml", card);
375 audio_route = audio_route_init(card, mixer_path);
376 if (!audio_route) {
377 ALOGE("%s: Failed to init audio route controls for card %d, aborting.",
378 __func__, card);
379 goto error;
380 }
381 mixer_card = calloc(1, sizeof(struct mixer_card));
382 mixer_card->card = card;
383 mixer_card->mixer = mixer;
384 mixer_card->audio_route = audio_route;
385 list_add_tail(&adev->mixer_list, &mixer_card->adev_list_node);
386 }
387 }
388
389 return 0;
390
391 error:
392 free_mixer_list(adev);
393 return -ENODEV;
394 }
395
get_snd_device_name(snd_device_t snd_device)396 static const char *get_snd_device_name(snd_device_t snd_device)
397 {
398 const char *name = NULL;
399
400 if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX)
401 name = device_table[snd_device];
402
403 ALOGE_IF(name == NULL, "%s: invalid snd device %d", __func__, snd_device);
404
405 return name;
406 }
407
get_snd_device_display_name(snd_device_t snd_device)408 static const char *get_snd_device_display_name(snd_device_t snd_device)
409 {
410 const char *name = get_snd_device_name(snd_device);
411
412 if (name == NULL)
413 name = "SND DEVICE NOT FOUND";
414
415 return name;
416 }
417
get_pcm_device(usecase_type_t uc_type,audio_devices_t devices)418 static struct pcm_device_profile *get_pcm_device(usecase_type_t uc_type, audio_devices_t devices)
419 {
420 int i;
421
422 devices &= ~AUDIO_DEVICE_BIT_IN;
423 for (i = 0; pcm_devices[i] != NULL; i++) {
424 if ((pcm_devices[i]->type == uc_type) &&
425 (devices & pcm_devices[i]->devices))
426 break;
427 }
428 return pcm_devices[i];
429 }
430
get_usecase_from_id(struct audio_device * adev,audio_usecase_t uc_id)431 static struct audio_usecase *get_usecase_from_id(struct audio_device *adev,
432 audio_usecase_t uc_id)
433 {
434 struct audio_usecase *usecase;
435 struct listnode *node;
436
437 list_for_each(node, &adev->usecase_list) {
438 usecase = node_to_item(node, struct audio_usecase, adev_list_node);
439 if (usecase->id == uc_id)
440 return usecase;
441 }
442 return NULL;
443 }
444
get_usecase_from_type(struct audio_device * adev,usecase_type_t type)445 static struct audio_usecase *get_usecase_from_type(struct audio_device *adev,
446 usecase_type_t type)
447 {
448 struct audio_usecase *usecase;
449 struct listnode *node;
450
451 list_for_each(node, &adev->usecase_list) {
452 usecase = node_to_item(node, struct audio_usecase, adev_list_node);
453 if (usecase->type & type)
454 return usecase;
455 }
456 return NULL;
457 }
458
459 /* always called with adev lock held */
set_voice_volume_l(struct audio_device * adev,float volume)460 static int set_voice_volume_l(struct audio_device *adev, float volume)
461 {
462 int err = 0;
463 (void)volume;
464
465 if (adev->mode == AUDIO_MODE_IN_CALL) {
466 /* TODO */
467 }
468 return err;
469 }
470
471
get_output_snd_device(struct audio_device * adev,audio_devices_t devices)472 static snd_device_t get_output_snd_device(struct audio_device *adev, audio_devices_t devices)
473 {
474
475 audio_mode_t mode = adev->mode;
476 snd_device_t snd_device = SND_DEVICE_NONE;
477
478 ALOGV("%s: enter: output devices(%#x), mode(%d)", __func__, devices, mode);
479 if (devices == AUDIO_DEVICE_NONE ||
480 devices & AUDIO_DEVICE_BIT_IN) {
481 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
482 goto exit;
483 }
484
485 if (mode == AUDIO_MODE_IN_CALL) {
486 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
487 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
488 if (adev->tty_mode == TTY_MODE_FULL)
489 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
490 else if (adev->tty_mode == TTY_MODE_VCO)
491 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
492 else if (adev->tty_mode == TTY_MODE_HCO)
493 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
494 else
495 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
496 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
497 snd_device = SND_DEVICE_OUT_BT_SCO;
498 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
499 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
500 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
501 snd_device = SND_DEVICE_OUT_HANDSET;
502 }
503 if (snd_device != SND_DEVICE_NONE) {
504 goto exit;
505 }
506 }
507
508 if (popcount(devices) == 2) {
509 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
510 AUDIO_DEVICE_OUT_SPEAKER)) {
511 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
512 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
513 AUDIO_DEVICE_OUT_SPEAKER)) {
514 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
515 } else {
516 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
517 goto exit;
518 }
519 if (snd_device != SND_DEVICE_NONE) {
520 goto exit;
521 }
522 }
523
524 if (popcount(devices) != 1) {
525 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
526 goto exit;
527 }
528
529 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
530 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
531 snd_device = SND_DEVICE_OUT_HEADPHONES;
532 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
533 snd_device = SND_DEVICE_OUT_SPEAKER;
534 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
535 snd_device = SND_DEVICE_OUT_BT_SCO;
536 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
537 snd_device = SND_DEVICE_OUT_HANDSET;
538 } else {
539 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
540 }
541 exit:
542 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
543 return snd_device;
544 }
545
get_input_snd_device(struct audio_device * adev,audio_devices_t out_device)546 static snd_device_t get_input_snd_device(struct audio_device *adev, audio_devices_t out_device)
547 {
548 audio_source_t source;
549 audio_mode_t mode = adev->mode;
550 audio_devices_t in_device;
551 audio_channel_mask_t channel_mask;
552 snd_device_t snd_device = SND_DEVICE_NONE;
553 struct stream_in *active_input = NULL;
554 struct audio_usecase *usecase;
555
556 usecase = get_usecase_from_type(adev, PCM_CAPTURE|VOICE_CALL);
557 if (usecase != NULL) {
558 active_input = (struct stream_in *)usecase->stream;
559 }
560 source = (active_input == NULL) ?
561 AUDIO_SOURCE_DEFAULT : active_input->source;
562
563 in_device = ((active_input == NULL) ?
564 AUDIO_DEVICE_NONE : active_input->devices)
565 & ~AUDIO_DEVICE_BIT_IN;
566 channel_mask = (active_input == NULL) ?
567 AUDIO_CHANNEL_IN_MONO : active_input->main_channels;
568
569 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
570 __func__, out_device, in_device);
571 if (mode == AUDIO_MODE_IN_CALL) {
572 if (out_device == AUDIO_DEVICE_NONE) {
573 ALOGE("%s: No output device set for voice call", __func__);
574 goto exit;
575 }
576 if (adev->tty_mode != TTY_MODE_OFF) {
577 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
578 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
579 switch (adev->tty_mode) {
580 case TTY_MODE_FULL:
581 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
582 break;
583 case TTY_MODE_VCO:
584 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
585 break;
586 case TTY_MODE_HCO:
587 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
588 break;
589 default:
590 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
591 }
592 goto exit;
593 }
594 }
595 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
596 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
597 snd_device = SND_DEVICE_IN_HANDSET_MIC;
598 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
599 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
600 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
601 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
602 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
603 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
604 }
605 } else if (source == AUDIO_SOURCE_CAMCORDER) {
606 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
607 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
608 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
609 }
610 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
611 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
612 if (adev->dualmic_config == DUALMIC_CONFIG_1) {
613 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
614 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_1;
615 else if (adev->ns_in_voice_rec)
616 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_NS_1;
617 }
618
619 if (snd_device == SND_DEVICE_NONE) {
620 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
621 }
622 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
623 snd_device = SND_DEVICE_IN_VOICE_REC_HEADSET_MIC;
624 }
625 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION || source == AUDIO_SOURCE_MIC) {
626 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
627 in_device = AUDIO_DEVICE_IN_BACK_MIC;
628 if (active_input) {
629 if (active_input->enable_aec) {
630 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
631 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
632 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
633 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
634 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
635 } else {
636 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
637 }
638 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
639 snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
640 }
641 }
642 /* TODO: set echo reference */
643 }
644 } else if (source == AUDIO_SOURCE_DEFAULT) {
645 goto exit;
646 }
647
648
649 if (snd_device != SND_DEVICE_NONE) {
650 goto exit;
651 }
652
653 if (in_device != AUDIO_DEVICE_NONE &&
654 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
655 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
656 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
657 snd_device = SND_DEVICE_IN_HANDSET_MIC;
658 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
659 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
660 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
661 snd_device = SND_DEVICE_IN_HEADSET_MIC;
662 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
663 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
664 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
665 snd_device = SND_DEVICE_IN_HDMI_MIC;
666 } else {
667 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
668 ALOGW("%s: Using default handset-mic", __func__);
669 snd_device = SND_DEVICE_IN_HANDSET_MIC;
670 }
671 } else {
672 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
673 snd_device = SND_DEVICE_IN_HANDSET_MIC;
674 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
675 snd_device = SND_DEVICE_IN_HEADSET_MIC;
676 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
677 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
678 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
679 snd_device = SND_DEVICE_IN_HANDSET_MIC;
680 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
681 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
682 } else {
683 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
684 ALOGW("%s: Using default handset-mic", __func__);
685 snd_device = SND_DEVICE_IN_HANDSET_MIC;
686 }
687 }
688 exit:
689 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
690 return snd_device;
691 }
692
set_hdmi_channels(struct audio_device * adev,int channel_count)693 static int set_hdmi_channels(struct audio_device *adev, int channel_count)
694 {
695 struct mixer_ctl *ctl;
696 const char *mixer_ctl_name = "";
697 (void)adev;
698 (void)channel_count;
699 /* TODO */
700
701 return 0;
702 }
703
edid_get_max_channels(struct audio_device * adev)704 static int edid_get_max_channels(struct audio_device *adev)
705 {
706 int max_channels = 2;
707 struct mixer_ctl *ctl;
708 (void)adev;
709
710 /* TODO */
711 return max_channels;
712 }
713
714 /* Delay in Us */
render_latency(audio_usecase_t usecase)715 static int64_t render_latency(audio_usecase_t usecase)
716 {
717 (void)usecase;
718 /* TODO */
719 return 0;
720 }
721
enable_snd_device(struct audio_device * adev,struct audio_usecase * uc_info,snd_device_t snd_device,bool update_mixer)722 static int enable_snd_device(struct audio_device *adev,
723 struct audio_usecase *uc_info,
724 snd_device_t snd_device,
725 bool update_mixer)
726 {
727 struct mixer_card *mixer_card;
728 struct listnode *node;
729 const char *snd_device_name = get_snd_device_name(snd_device);
730
731 if (snd_device_name == NULL)
732 return -EINVAL;
733
734 if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES) {
735 ALOGD("Request to enable combo device: enable individual devices\n");
736 enable_snd_device(adev, uc_info, SND_DEVICE_OUT_SPEAKER, update_mixer);
737 enable_snd_device(adev, uc_info, SND_DEVICE_OUT_HEADPHONES, update_mixer);
738 return 0;
739 }
740 adev->snd_dev_ref_cnt[snd_device]++;
741 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
742 ALOGV("%s: snd_device(%d: %s) is already active",
743 __func__, snd_device, snd_device_name);
744 return 0;
745 }
746
747 ALOGV("%s: snd_device(%d: %s)", __func__,
748 snd_device, snd_device_name);
749
750 list_for_each(node, &uc_info->mixer_list) {
751 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[uc_info->id]);
752 audio_route_apply_path(mixer_card->audio_route, snd_device_name);
753 if (update_mixer)
754 audio_route_update_mixer(mixer_card->audio_route);
755 }
756
757 return 0;
758 }
759
disable_snd_device(struct audio_device * adev,struct audio_usecase * uc_info,snd_device_t snd_device,bool update_mixer)760 static int disable_snd_device(struct audio_device *adev,
761 struct audio_usecase *uc_info,
762 snd_device_t snd_device,
763 bool update_mixer)
764 {
765 struct mixer_card *mixer_card;
766 struct listnode *node;
767 const char *snd_device_name = get_snd_device_name(snd_device);
768
769 if (snd_device_name == NULL)
770 return -EINVAL;
771
772 if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES) {
773 ALOGD("Request to disable combo device: disable individual devices\n");
774 disable_snd_device(adev, uc_info, SND_DEVICE_OUT_SPEAKER, update_mixer);
775 disable_snd_device(adev, uc_info, SND_DEVICE_OUT_HEADPHONES, update_mixer);
776 return 0;
777 }
778
779 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
780 ALOGE("%s: device ref cnt is already 0", __func__);
781 return -EINVAL;
782 }
783 adev->snd_dev_ref_cnt[snd_device]--;
784 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
785 ALOGV("%s: snd_device(%d: %s)", __func__,
786 snd_device, snd_device_name);
787 list_for_each(node, &uc_info->mixer_list) {
788 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[uc_info->id]);
789 audio_route_reset_path(mixer_card->audio_route, snd_device_name);
790 if (update_mixer)
791 audio_route_update_mixer(mixer_card->audio_route);
792 }
793 }
794 return 0;
795 }
796
select_devices(struct audio_device * adev,audio_usecase_t uc_id)797 static int select_devices(struct audio_device *adev,
798 audio_usecase_t uc_id)
799 {
800 snd_device_t out_snd_device = SND_DEVICE_NONE;
801 snd_device_t in_snd_device = SND_DEVICE_NONE;
802 struct audio_usecase *usecase = NULL;
803 struct audio_usecase *vc_usecase = NULL;
804 struct listnode *node;
805 struct stream_in *active_input = NULL;
806 struct stream_out *active_out;
807 struct mixer_card *mixer_card;
808
809 ALOGV("%s: usecase(%d)", __func__, uc_id);
810
811 if (uc_id == USECASE_AUDIO_CAPTURE_HOTWORD)
812 return 0;
813
814 usecase = get_usecase_from_type(adev, PCM_CAPTURE|VOICE_CALL);
815 if (usecase != NULL) {
816 active_input = (struct stream_in *)usecase->stream;
817 }
818
819 usecase = get_usecase_from_id(adev, uc_id);
820 if (usecase == NULL) {
821 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
822 return -EINVAL;
823 }
824 active_out = (struct stream_out *)usecase->stream;
825
826 if (usecase->type == VOICE_CALL) {
827 out_snd_device = get_output_snd_device(adev, active_out->devices);
828 in_snd_device = get_input_snd_device(adev, active_out->devices);
829 usecase->devices = active_out->devices;
830 } else {
831 /*
832 * If the voice call is active, use the sound devices of voice call usecase
833 * so that it would not result any device switch. All the usecases will
834 * be switched to new device when select_devices() is called for voice call
835 * usecase.
836 */
837 if (adev->in_call) {
838 vc_usecase = get_usecase_from_id(adev, USECASE_VOICE_CALL);
839 if (usecase == NULL) {
840 ALOGE("%s: Could not find the voice call usecase", __func__);
841 } else {
842 in_snd_device = vc_usecase->in_snd_device;
843 out_snd_device = vc_usecase->out_snd_device;
844 }
845 }
846 if (usecase->type == PCM_PLAYBACK) {
847 usecase->devices = active_out->devices;
848 in_snd_device = SND_DEVICE_NONE;
849 if (out_snd_device == SND_DEVICE_NONE) {
850 out_snd_device = get_output_snd_device(adev, active_out->devices);
851 if (active_out == adev->primary_output &&
852 active_input &&
853 active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
854 select_devices(adev, active_input->usecase);
855 }
856 }
857 } else if (usecase->type == PCM_CAPTURE) {
858 usecase->devices = ((struct stream_in *)usecase->stream)->devices;
859 out_snd_device = SND_DEVICE_NONE;
860 if (in_snd_device == SND_DEVICE_NONE) {
861 if (active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
862 adev->primary_output && !adev->primary_output->standby) {
863 in_snd_device = get_input_snd_device(adev, adev->primary_output->devices);
864 } else {
865 in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE);
866 }
867 }
868 }
869 }
870
871 if (out_snd_device == usecase->out_snd_device &&
872 in_snd_device == usecase->in_snd_device) {
873 return 0;
874 }
875
876 ALOGV("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
877 out_snd_device, get_snd_device_display_name(out_snd_device),
878 in_snd_device, get_snd_device_display_name(in_snd_device));
879
880
881 /* Disable current sound devices */
882 if (usecase->out_snd_device != SND_DEVICE_NONE) {
883 pthread_mutex_lock(&adev->tfa9895_lock);
884 disable_snd_device(adev, usecase, usecase->out_snd_device, false);
885 pthread_mutex_unlock(&adev->tfa9895_lock);
886 }
887
888 if (usecase->in_snd_device != SND_DEVICE_NONE) {
889 disable_snd_device(adev, usecase, usecase->in_snd_device, false);
890 }
891
892 /* Enable new sound devices */
893 if (out_snd_device != SND_DEVICE_NONE) {
894 enable_snd_device(adev, usecase, out_snd_device, false);
895 }
896
897 if (in_snd_device != SND_DEVICE_NONE) {
898 enable_snd_device(adev, usecase, in_snd_device, false);
899 }
900
901 list_for_each(node, &usecase->mixer_list) {
902 mixer_card = node_to_item(node, struct mixer_card, uc_list_node[usecase->id]);
903 audio_route_update_mixer(mixer_card->audio_route);
904 }
905
906 usecase->in_snd_device = in_snd_device;
907 usecase->out_snd_device = out_snd_device;
908
909 if (out_snd_device != SND_DEVICE_NONE)
910 if (usecase->devices & (AUDIO_DEVICE_OUT_WIRED_HEADSET | AUDIO_DEVICE_OUT_WIRED_HEADPHONE))
911 if (adev->htc_acoustic_set_rt5506_amp != NULL)
912 adev->htc_acoustic_set_rt5506_amp(adev->mode, usecase->devices);
913 return 0;
914 }
915
916
917 static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames);
918 static int do_in_standby_l(struct stream_in *in);
919
920 #ifdef PREPROCESSING_ENABLED
get_capture_reference_delay(struct stream_in * in,size_t frames __unused,struct echo_reference_buffer * buffer)921 static void get_capture_reference_delay(struct stream_in *in,
922 size_t frames __unused,
923 struct echo_reference_buffer *buffer)
924 {
925 ALOGVV("%s: enter:)", __func__);
926
927 /* read frames available in kernel driver buffer */
928 unsigned int kernel_frames;
929 struct timespec tstamp;
930 long buf_delay;
931 long kernel_delay;
932 long delay_ns;
933 struct pcm_device *ref_device;
934 long rsmp_delay = 0;
935
936 ref_device = node_to_item(list_tail(&in->pcm_dev_list),
937 struct pcm_device, stream_list_node);
938
939 if (pcm_get_htimestamp(ref_device->pcm, &kernel_frames, &tstamp) < 0) {
940 buffer->time_stamp.tv_sec = 0;
941 buffer->time_stamp.tv_nsec = 0;
942 buffer->delay_ns = 0;
943 ALOGW("read get_capture_reference_delay(): pcm_htimestamp error");
944 return;
945 }
946
947 /* adjust render time stamp with delay added by current driver buffer.
948 * Add the duration of current frame as we want the render time of the last
949 * sample being written. */
950
951 kernel_delay = (long)(((int64_t)kernel_frames * 1000000000) / ref_device->pcm_profile->config.rate);
952
953 buffer->time_stamp = tstamp;
954 buffer->delay_ns = kernel_delay;
955
956 ALOGVV("get_capture_reference_delay_time_stamp Secs: [%10ld], nSecs: [%9ld], kernel_frames: [%5d],"
957 " delay_ns: [%d] , frames:[%zd]",
958 buffer->time_stamp.tv_sec , buffer->time_stamp.tv_nsec, kernel_frames, buffer->delay_ns, frames);
959 }
960
get_capture_delay(struct stream_in * in,size_t frames __unused,struct echo_reference_buffer * buffer)961 static void get_capture_delay(struct stream_in *in,
962 size_t frames __unused,
963 struct echo_reference_buffer *buffer)
964 {
965 ALOGVV("%s: enter:)", __func__);
966 /* read frames available in kernel driver buffer */
967 unsigned int kernel_frames;
968 struct timespec tstamp;
969 long buf_delay;
970 long rsmp_delay;
971 long kernel_delay;
972 long delay_ns;
973 struct pcm_device *pcm_device;
974
975 pcm_device = node_to_item(list_head(&in->pcm_dev_list),
976 struct pcm_device, stream_list_node);
977
978 if (pcm_get_htimestamp(pcm_device->pcm, &kernel_frames, &tstamp) < 0) {
979 buffer->time_stamp.tv_sec = 0;
980 buffer->time_stamp.tv_nsec = 0;
981 buffer->delay_ns = 0;
982 ALOGW("read get_capture_delay(): pcm_htimestamp error");
983 return;
984 }
985
986 /* read frames available in audio HAL input buffer
987 * add number of frames being read as we want the capture time of first sample
988 * in current buffer */
989 /* frames in in->read_buf are at driver sampling rate while frames in in->proc_buf are
990 * at requested sampling rate */
991 buf_delay = (long)(((int64_t)(in->read_buf_frames) * 1000000000) / in->config.rate +
992 ((int64_t)(in->proc_buf_frames) * 1000000000) / in->requested_rate );
993
994 /* add delay introduced by resampler */
995 rsmp_delay = 0;
996 if (in->resampler) {
997 rsmp_delay = in->resampler->delay_ns(in->resampler);
998 }
999
1000 kernel_delay = (long)(((int64_t)kernel_frames * 1000000000) / in->config.rate);
1001
1002 delay_ns = kernel_delay + buf_delay + rsmp_delay;
1003
1004 buffer->time_stamp = tstamp;
1005 buffer->delay_ns = delay_ns;
1006 ALOGVV("get_capture_delay_time_stamp Secs: [%10ld], nSecs: [%9ld], kernel_frames:[%5d],"
1007 " delay_ns: [%d], kernel_delay:[%ld], buf_delay:[%ld], rsmp_delay:[%ld], "
1008 "in->read_buf_frames:[%zd], in->proc_buf_frames:[%zd], frames:[%zd]",
1009 buffer->time_stamp.tv_sec , buffer->time_stamp.tv_nsec, kernel_frames,
1010 buffer->delay_ns, kernel_delay, buf_delay, rsmp_delay,
1011 in->read_buf_frames, in->proc_buf_frames, frames);
1012 }
1013
update_echo_reference(struct stream_in * in,size_t frames)1014 static int32_t update_echo_reference(struct stream_in *in, size_t frames)
1015 {
1016 ALOGVV("%s: enter:), in->config.channels(%d)", __func__,in->config.channels);
1017 struct echo_reference_buffer b;
1018 b.delay_ns = 0;
1019 struct pcm_device *pcm_device;
1020
1021 pcm_device = node_to_item(list_head(&in->pcm_dev_list),
1022 struct pcm_device, stream_list_node);
1023
1024 ALOGVV("update_echo_reference, in->config.channels(%d), frames = [%zd], in->ref_buf_frames = [%zd], "
1025 "b.frame_count = [%zd]",
1026 in->config.channels, frames, in->ref_buf_frames, frames - in->ref_buf_frames);
1027 if (in->ref_buf_frames < frames) {
1028 if (in->ref_buf_size < frames) {
1029 in->ref_buf_size = frames;
1030 in->ref_buf = (int16_t *)realloc(in->ref_buf, pcm_frames_to_bytes(pcm_device->pcm, frames));
1031 ALOG_ASSERT((in->ref_buf != NULL),
1032 "update_echo_reference() failed to reallocate ref_buf");
1033 ALOGVV("update_echo_reference(): ref_buf %p extended to %d bytes",
1034 in->ref_buf, pcm_frames_to_bytes(pcm_device->pcm, frames));
1035 }
1036 b.frame_count = frames - in->ref_buf_frames;
1037 b.raw = (void *)(in->ref_buf + in->ref_buf_frames * in->config.channels);
1038
1039 get_capture_delay(in, frames, &b);
1040
1041 if (in->echo_reference->read(in->echo_reference, &b) == 0)
1042 {
1043 in->ref_buf_frames += b.frame_count;
1044 ALOGVV("update_echo_reference(): in->ref_buf_frames:[%zd], "
1045 "in->ref_buf_size:[%zd], frames:[%zd], b.frame_count:[%zd]",
1046 in->ref_buf_frames, in->ref_buf_size, frames, b.frame_count);
1047 }
1048 } else
1049 ALOGW("update_echo_reference(): NOT enough frames to read ref buffer");
1050 return b.delay_ns;
1051 }
1052
set_preprocessor_param(effect_handle_t handle,effect_param_t * param)1053 static int set_preprocessor_param(effect_handle_t handle,
1054 effect_param_t *param)
1055 {
1056 uint32_t size = sizeof(int);
1057 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1058 param->vsize;
1059
1060 int status = (*handle)->command(handle,
1061 EFFECT_CMD_SET_PARAM,
1062 sizeof (effect_param_t) + psize,
1063 param,
1064 &size,
1065 ¶m->status);
1066 if (status == 0)
1067 status = param->status;
1068
1069 return status;
1070 }
1071
set_preprocessor_echo_delay(effect_handle_t handle,int32_t delay_us)1072 static int set_preprocessor_echo_delay(effect_handle_t handle,
1073 int32_t delay_us)
1074 {
1075 struct {
1076 effect_param_t param;
1077 uint32_t data_0;
1078 int32_t data_1;
1079 } buf;
1080 memset(&buf, 0, sizeof(buf));
1081
1082 buf.param.psize = sizeof(uint32_t);
1083 buf.param.vsize = sizeof(uint32_t);
1084 buf.data_0 = AEC_PARAM_ECHO_DELAY;
1085 buf.data_1 = delay_us;
1086
1087 return set_preprocessor_param(handle, &buf.param);
1088 }
1089
push_echo_reference(struct stream_in * in,size_t frames)1090 static void push_echo_reference(struct stream_in *in, size_t frames)
1091 {
1092 ALOGVV("%s: enter:)", __func__);
1093 /* read frames from echo reference buffer and update echo delay
1094 * in->ref_buf_frames is updated with frames available in in->ref_buf */
1095
1096 int32_t delay_us = update_echo_reference(in, frames)/1000;
1097 int32_t size_in_bytes = 0;
1098 int i;
1099 audio_buffer_t buf;
1100
1101 if (in->ref_buf_frames < frames)
1102 frames = in->ref_buf_frames;
1103
1104 buf.frameCount = frames;
1105 buf.raw = in->ref_buf;
1106
1107 for (i = 0; i < in->num_preprocessors; i++) {
1108 if ((*in->preprocessors[i].effect_itfe)->process_reverse == NULL)
1109 continue;
1110 ALOGVV("%s: effect_itfe)->process_reverse() BEGIN i=(%d) ", __func__, i);
1111 (*in->preprocessors[i].effect_itfe)->process_reverse(in->preprocessors[i].effect_itfe,
1112 &buf,
1113 NULL);
1114 ALOGVV("%s: effect_itfe)->process_reverse() END i=(%d) ", __func__, i);
1115 set_preprocessor_echo_delay(in->preprocessors[i].effect_itfe, delay_us);
1116 }
1117
1118 in->ref_buf_frames -= buf.frameCount;
1119 ALOGVV("%s: in->ref_buf_frames(%zd), in->config.channels(%d) ",
1120 __func__, in->ref_buf_frames, in->config.channels);
1121 if (in->ref_buf_frames) {
1122 memcpy(in->ref_buf,
1123 in->ref_buf + buf.frameCount * in->config.channels,
1124 in->ref_buf_frames * in->config.channels * sizeof(int16_t));
1125 }
1126 }
1127
put_echo_reference(struct audio_device * adev,struct echo_reference_itfe * reference)1128 static void put_echo_reference(struct audio_device *adev,
1129 struct echo_reference_itfe *reference)
1130 {
1131 ALOGV("%s: enter:)", __func__);
1132 int32_t prev_generation = adev->echo_reference_generation;
1133 struct stream_out *out = adev->primary_output;
1134
1135 if (adev->echo_reference != NULL &&
1136 reference == adev->echo_reference) {
1137 /* echo reference is taken from the low latency output stream used
1138 * for voice use cases */
1139 adev->echo_reference = NULL;
1140 android_atomic_inc(&adev->echo_reference_generation);
1141 if (out != NULL && out->usecase == USECASE_AUDIO_PLAYBACK) {
1142 // if the primary output is in standby or did not pick the echo reference yet
1143 // we can safely get rid of it here.
1144 // otherwise, out_write() or out_standby() will detect the change in echo reference
1145 // generation and release the echo reference owned by the stream.
1146 if ((out->echo_reference_generation != prev_generation) || out->standby)
1147 release_echo_reference(reference);
1148 } else {
1149 release_echo_reference(reference);
1150 }
1151 ALOGV("release_echo_reference");
1152 }
1153 }
1154
get_echo_reference(struct audio_device * adev,audio_format_t format __unused,uint32_t channel_count,uint32_t sampling_rate)1155 static struct echo_reference_itfe *get_echo_reference(struct audio_device *adev,
1156 audio_format_t format __unused,
1157 uint32_t channel_count,
1158 uint32_t sampling_rate)
1159 {
1160 ALOGV("%s: enter:)", __func__);
1161 put_echo_reference(adev, adev->echo_reference);
1162 /* echo reference is taken from the low latency output stream used
1163 * for voice use cases */
1164 if (adev->primary_output!= NULL && adev->primary_output->usecase == USECASE_AUDIO_PLAYBACK &&
1165 !adev->primary_output->standby) {
1166 struct audio_stream *stream =
1167 &adev->primary_output->stream.common;
1168 uint32_t wr_channel_count = audio_channel_count_from_out_mask(stream->get_channels(stream));
1169 uint32_t wr_sampling_rate = stream->get_sample_rate(stream);
1170 ALOGV("Calling create_echo_reference");
1171 int status = create_echo_reference(AUDIO_FORMAT_PCM_16_BIT,
1172 channel_count,
1173 sampling_rate,
1174 AUDIO_FORMAT_PCM_16_BIT,
1175 wr_channel_count,
1176 wr_sampling_rate,
1177 &adev->echo_reference);
1178 if (status == 0)
1179 android_atomic_inc(&adev->echo_reference_generation);
1180 }
1181 return adev->echo_reference;
1182 }
1183
1184 #ifdef HW_AEC_LOOPBACK
get_hw_echo_reference(struct stream_in * in)1185 static int get_hw_echo_reference(struct stream_in *in)
1186 {
1187 struct pcm_device_profile *ref_pcm_profile;
1188 struct pcm_device *ref_device;
1189 struct audio_device *adev = in->dev;
1190
1191 in->hw_echo_reference = false;
1192
1193 if (adev->primary_output!= NULL &&
1194 !adev->primary_output->standby &&
1195 adev->primary_output->usecase == USECASE_AUDIO_PLAYBACK &&
1196 adev->primary_output->devices == AUDIO_DEVICE_OUT_SPEAKER) {
1197 struct audio_stream *stream = &adev->primary_output->stream.common;
1198
1199 // TODO: currently there is no low latency mode for aec reference.
1200 ref_pcm_profile = get_pcm_device(PCM_CAPTURE, pcm_device_capture_loopback_aec.devices);
1201 if (ref_pcm_profile == NULL) {
1202 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
1203 __func__, pcm_device_capture_loopback_aec.devices);
1204 return -EINVAL;
1205 }
1206
1207 ref_device = (struct pcm_device *)calloc(1, sizeof(struct pcm_device));
1208 ref_device->pcm_profile = ref_pcm_profile;
1209
1210 ALOGV("%s: ref_device rate:%d, ch:%d", __func__, ref_pcm_profile->config.rate, ref_pcm_profile->config.channels);
1211 ref_device->pcm = pcm_open(ref_device->pcm_profile->card, ref_device->pcm_profile->id, PCM_IN | PCM_MONOTONIC, &ref_device->pcm_profile->config);
1212
1213 if (ref_device->pcm && !pcm_is_ready(ref_device->pcm)) {
1214 ALOGE("%s: %s", __func__, pcm_get_error(ref_device->pcm));
1215 pcm_close(ref_device->pcm);
1216 ref_device->pcm = NULL;
1217 return -EIO;
1218 }
1219 list_add_tail(&in->pcm_dev_list, &ref_device->stream_list_node);
1220
1221 in->hw_echo_reference = true;
1222
1223 ALOGV("%s: hw_echo_reference is true", __func__);
1224 }
1225
1226 return 0;
1227 }
1228 #endif
1229
get_playback_delay(struct stream_out * out,size_t frames,struct echo_reference_buffer * buffer)1230 static int get_playback_delay(struct stream_out *out,
1231 size_t frames,
1232 struct echo_reference_buffer *buffer)
1233 {
1234 unsigned int kernel_frames;
1235 int status;
1236 int primary_pcm = 0;
1237 struct pcm_device *pcm_device;
1238
1239 pcm_device = node_to_item(list_head(&out->pcm_dev_list),
1240 struct pcm_device, stream_list_node);
1241
1242 status = pcm_get_htimestamp(pcm_device->pcm, &kernel_frames, &buffer->time_stamp);
1243 if (status < 0) {
1244 buffer->time_stamp.tv_sec = 0;
1245 buffer->time_stamp.tv_nsec = 0;
1246 buffer->delay_ns = 0;
1247 ALOGV("get_playback_delay(): pcm_get_htimestamp error,"
1248 "setting playbackTimestamp to 0");
1249 return status;
1250 }
1251
1252 kernel_frames = pcm_get_buffer_size(pcm_device->pcm) - kernel_frames;
1253
1254 /* adjust render time stamp with delay added by current driver buffer.
1255 * Add the duration of current frame as we want the render time of the last
1256 * sample being written. */
1257 buffer->delay_ns = (long)(((int64_t)(kernel_frames + frames)* 1000000000)/
1258 out->config.rate);
1259 ALOGVV("get_playback_delay_time_stamp Secs: [%10ld], nSecs: [%9ld], kernel_frames: [%5u], delay_ns: [%d],",
1260 buffer->time_stamp.tv_sec, buffer->time_stamp.tv_nsec, kernel_frames, buffer->delay_ns);
1261
1262 return 0;
1263 }
1264
1265 #define GET_COMMAND_STATUS(status, fct_status, cmd_status) \
1266 do { \
1267 if (fct_status != 0) \
1268 status = fct_status; \
1269 else if (cmd_status != 0) \
1270 status = cmd_status; \
1271 } while(0)
1272
in_configure_reverse(struct stream_in * in)1273 static int in_configure_reverse(struct stream_in *in)
1274 {
1275 int32_t cmd_status;
1276 uint32_t size = sizeof(int);
1277 effect_config_t config;
1278 int32_t status = 0;
1279 int32_t fct_status = 0;
1280 int i;
1281 ALOGV("%s: enter: in->num_preprocessors(%d)", __func__, in->num_preprocessors);
1282 if (in->num_preprocessors > 0) {
1283 config.inputCfg.channels = in->main_channels;
1284 config.outputCfg.channels = in->main_channels;
1285 config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1286 config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1287 config.inputCfg.samplingRate = in->requested_rate;
1288 config.outputCfg.samplingRate = in->requested_rate;
1289 config.inputCfg.mask =
1290 ( EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT );
1291 config.outputCfg.mask =
1292 ( EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT );
1293
1294 for (i = 0; i < in->num_preprocessors; i++)
1295 {
1296 if ((*in->preprocessors[i].effect_itfe)->process_reverse == NULL)
1297 continue;
1298 fct_status = (*(in->preprocessors[i].effect_itfe))->command(
1299 in->preprocessors[i].effect_itfe,
1300 EFFECT_CMD_SET_CONFIG_REVERSE,
1301 sizeof(effect_config_t),
1302 &config,
1303 &size,
1304 &cmd_status);
1305 ALOGV("%s: calling EFFECT_CMD_SET_CONFIG_REVERSE",__func__);
1306 GET_COMMAND_STATUS(status, fct_status, cmd_status);
1307 }
1308 }
1309 return status;
1310 }
1311
1312 #define MAX_NUM_CHANNEL_CONFIGS 10
1313
in_read_audio_effect_channel_configs(struct stream_in * in __unused,struct effect_info_s * effect_info)1314 static void in_read_audio_effect_channel_configs(struct stream_in *in __unused,
1315 struct effect_info_s *effect_info)
1316 {
1317 /* size and format of the cmd are defined in hardware/audio_effect.h */
1318 effect_handle_t effect = effect_info->effect_itfe;
1319 uint32_t cmd_size = 2 * sizeof(uint32_t);
1320 uint32_t cmd[] = { EFFECT_FEATURE_AUX_CHANNELS, MAX_NUM_CHANNEL_CONFIGS };
1321 /* reply = status + number of configs (n) + n x channel_config_t */
1322 uint32_t reply_size =
1323 2 * sizeof(uint32_t) + (MAX_NUM_CHANNEL_CONFIGS * sizeof(channel_config_t));
1324 int32_t reply[reply_size];
1325 int32_t cmd_status;
1326
1327 ALOG_ASSERT((effect_info->num_channel_configs == 0),
1328 "in_read_audio_effect_channel_configs() num_channel_configs not cleared");
1329 ALOG_ASSERT((effect_info->channel_configs == NULL),
1330 "in_read_audio_effect_channel_configs() channel_configs not cleared");
1331
1332 /* if this command is not supported, then the effect is supposed to return -EINVAL.
1333 * This error will be interpreted as if the effect supports the main_channels but does not
1334 * support any aux_channels */
1335 cmd_status = (*effect)->command(effect,
1336 EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS,
1337 cmd_size,
1338 (void*)&cmd,
1339 &reply_size,
1340 (void*)&reply);
1341
1342 if (cmd_status != 0) {
1343 ALOGV("in_read_audio_effect_channel_configs(): "
1344 "fx->command returned %d", cmd_status);
1345 return;
1346 }
1347
1348 if (reply[0] != 0) {
1349 ALOGW("in_read_audio_effect_channel_configs(): "
1350 "command EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS error %d num configs %d",
1351 reply[0], (reply[0] == -ENOMEM) ? reply[1] : MAX_NUM_CHANNEL_CONFIGS);
1352 return;
1353 }
1354
1355 /* the feature is not supported */
1356 ALOGV("in_read_audio_effect_channel_configs()(): "
1357 "Feature supported and adding %d channel configs to the list", reply[1]);
1358 effect_info->num_channel_configs = reply[1];
1359 effect_info->channel_configs =
1360 (channel_config_t *) malloc(sizeof(channel_config_t) * reply[1]); /* n x configs */
1361 memcpy(effect_info->channel_configs, (reply + 2), sizeof(channel_config_t) * reply[1]);
1362 }
1363
1364
1365 #define NUM_IN_AUX_CNL_CONFIGS 2
1366 static const channel_config_t in_aux_cnl_configs[NUM_IN_AUX_CNL_CONFIGS] = {
1367 { AUDIO_CHANNEL_IN_FRONT , AUDIO_CHANNEL_IN_BACK},
1368 { AUDIO_CHANNEL_IN_STEREO , AUDIO_CHANNEL_IN_RIGHT}
1369 };
in_get_aux_channels(struct stream_in * in)1370 static uint32_t in_get_aux_channels(struct stream_in *in)
1371 {
1372 int i;
1373 channel_config_t new_chcfg = {0, 0};
1374
1375 if (in->num_preprocessors == 0)
1376 return 0;
1377
1378 /* do not enable dual mic configurations when capturing from other microphones than
1379 * main or sub */
1380 if (!(in->devices & (AUDIO_DEVICE_IN_BUILTIN_MIC | AUDIO_DEVICE_IN_BACK_MIC)))
1381 return 0;
1382
1383 /* retain most complex aux channels configuration compatible with requested main channels and
1384 * supported by audio driver and all pre processors */
1385 for (i = 0; i < NUM_IN_AUX_CNL_CONFIGS; i++) {
1386 const channel_config_t *cur_chcfg = &in_aux_cnl_configs[i];
1387 if (cur_chcfg->main_channels == in->main_channels) {
1388 size_t match_cnt;
1389 size_t idx_preproc;
1390 for (idx_preproc = 0, match_cnt = 0;
1391 /* no need to continue if at least one preprocessor doesn't match */
1392 idx_preproc < (size_t)in->num_preprocessors && match_cnt == idx_preproc;
1393 idx_preproc++) {
1394 struct effect_info_s *effect_info = &in->preprocessors[idx_preproc];
1395 size_t idx_chcfg;
1396
1397 for (idx_chcfg = 0; idx_chcfg < effect_info->num_channel_configs; idx_chcfg++) {
1398 if (memcmp(effect_info->channel_configs + idx_chcfg,
1399 cur_chcfg,
1400 sizeof(channel_config_t)) == 0) {
1401 match_cnt++;
1402 break;
1403 }
1404 }
1405 }
1406 /* if all preprocessors match, we have a candidate */
1407 if (match_cnt == (size_t)in->num_preprocessors) {
1408 /* retain most complex aux channels configuration */
1409 if (audio_channel_count_from_in_mask(cur_chcfg->aux_channels) > audio_channel_count_from_in_mask(new_chcfg.aux_channels)) {
1410 new_chcfg = *cur_chcfg;
1411 }
1412 }
1413 }
1414 }
1415
1416 ALOGV("in_get_aux_channels(): return %04x", new_chcfg.aux_channels);
1417
1418 return new_chcfg.aux_channels;
1419 }
1420
in_configure_effect_channels(effect_handle_t effect,channel_config_t * channel_config)1421 static int in_configure_effect_channels(effect_handle_t effect,
1422 channel_config_t *channel_config)
1423 {
1424 int status = 0;
1425 int fct_status;
1426 int32_t cmd_status;
1427 uint32_t reply_size;
1428 effect_config_t config;
1429 uint32_t cmd[(sizeof(uint32_t) + sizeof(channel_config_t) - 1) / sizeof(uint32_t) + 1];
1430
1431 ALOGV("in_configure_effect_channels(): configure effect with channels: [%04x][%04x]",
1432 channel_config->main_channels,
1433 channel_config->aux_channels);
1434
1435 config.inputCfg.mask = EFFECT_CONFIG_CHANNELS;
1436 config.outputCfg.mask = EFFECT_CONFIG_CHANNELS;
1437 reply_size = sizeof(effect_config_t);
1438 fct_status = (*effect)->command(effect,
1439 EFFECT_CMD_GET_CONFIG,
1440 0,
1441 NULL,
1442 &reply_size,
1443 &config);
1444 if (fct_status != 0) {
1445 ALOGE("in_configure_effect_channels(): EFFECT_CMD_GET_CONFIG failed");
1446 return fct_status;
1447 }
1448
1449 config.inputCfg.channels = channel_config->main_channels | channel_config->aux_channels;
1450 config.outputCfg.channels = config.inputCfg.channels;
1451 reply_size = sizeof(uint32_t);
1452 fct_status = (*effect)->command(effect,
1453 EFFECT_CMD_SET_CONFIG,
1454 sizeof(effect_config_t),
1455 &config,
1456 &reply_size,
1457 &cmd_status);
1458 GET_COMMAND_STATUS(status, fct_status, cmd_status);
1459
1460 cmd[0] = EFFECT_FEATURE_AUX_CHANNELS;
1461 memcpy(cmd + 1, channel_config, sizeof(channel_config_t));
1462 reply_size = sizeof(uint32_t);
1463 fct_status = (*effect)->command(effect,
1464 EFFECT_CMD_SET_FEATURE_CONFIG,
1465 sizeof(cmd), //sizeof(uint32_t) + sizeof(channel_config_t),
1466 cmd,
1467 &reply_size,
1468 &cmd_status);
1469 GET_COMMAND_STATUS(status, fct_status, cmd_status);
1470
1471 /* some implementations need to be re-enabled after a config change */
1472 reply_size = sizeof(uint32_t);
1473 fct_status = (*effect)->command(effect,
1474 EFFECT_CMD_ENABLE,
1475 0,
1476 NULL,
1477 &reply_size,
1478 &cmd_status);
1479 GET_COMMAND_STATUS(status, fct_status, cmd_status);
1480
1481 return status;
1482 }
1483
in_reconfigure_channels(struct stream_in * in,effect_handle_t effect,channel_config_t * channel_config,bool config_changed)1484 static int in_reconfigure_channels(struct stream_in *in,
1485 effect_handle_t effect,
1486 channel_config_t *channel_config,
1487 bool config_changed) {
1488
1489 int status = 0;
1490
1491 ALOGV("in_reconfigure_channels(): config_changed %d effect %p",
1492 config_changed, effect);
1493
1494 /* if config changed, reconfigure all previously added effects */
1495 if (config_changed) {
1496 int i;
1497 ALOGV("%s: config_changed (%d)", __func__, config_changed);
1498 for (i = 0; i < in->num_preprocessors; i++)
1499 {
1500 int cur_status = in_configure_effect_channels(in->preprocessors[i].effect_itfe,
1501 channel_config);
1502 ALOGV("%s: in_configure_effect_channels i=(%d), [main_channel,aux_channel]=[%d|%d], status=%d",
1503 __func__, i, channel_config->main_channels, channel_config->aux_channels, cur_status);
1504 if (cur_status != 0) {
1505 ALOGV("in_reconfigure_channels(): error %d configuring effect "
1506 "%d with channels: [%04x][%04x]",
1507 cur_status,
1508 i,
1509 channel_config->main_channels,
1510 channel_config->aux_channels);
1511 status = cur_status;
1512 }
1513 }
1514 } else if (effect != NULL && channel_config->aux_channels) {
1515 /* if aux channels config did not change but aux channels are present,
1516 * we still need to configure the effect being added */
1517 status = in_configure_effect_channels(effect, channel_config);
1518 }
1519 return status;
1520 }
1521
in_update_aux_channels(struct stream_in * in,effect_handle_t effect)1522 static void in_update_aux_channels(struct stream_in *in,
1523 effect_handle_t effect)
1524 {
1525 uint32_t aux_channels;
1526 channel_config_t channel_config;
1527 int status;
1528
1529 aux_channels = in_get_aux_channels(in);
1530
1531 channel_config.main_channels = in->main_channels;
1532 channel_config.aux_channels = aux_channels;
1533 status = in_reconfigure_channels(in,
1534 effect,
1535 &channel_config,
1536 (aux_channels != in->aux_channels));
1537
1538 if (status != 0) {
1539 ALOGV("in_update_aux_channels(): in_reconfigure_channels error %d", status);
1540 /* resetting aux channels configuration */
1541 aux_channels = 0;
1542 channel_config.aux_channels = 0;
1543 in_reconfigure_channels(in, effect, &channel_config, true);
1544 }
1545 ALOGV("%s: aux_channels=%d, in->aux_channels_changed=%d", __func__, aux_channels, in->aux_channels_changed);
1546 if (in->aux_channels != aux_channels) {
1547 in->aux_channels_changed = true;
1548 in->aux_channels = aux_channels;
1549 do_in_standby_l(in);
1550 }
1551 }
1552 #endif
1553
1554 /* This function reads PCM data and:
1555 * - resample if needed
1556 * - process if pre-processors are attached
1557 * - discard unwanted channels
1558 */
read_and_process_frames(struct stream_in * in,void * buffer,ssize_t frames)1559 static ssize_t read_and_process_frames(struct stream_in *in, void* buffer, ssize_t frames)
1560 {
1561 ssize_t frames_wr = 0;
1562 audio_buffer_t in_buf;
1563 audio_buffer_t out_buf;
1564 size_t src_channels = in->config.channels;
1565 size_t dst_channels = audio_channel_count_from_in_mask(in->main_channels);
1566 int i;
1567 void *proc_buf_out;
1568 struct pcm_device *pcm_device;
1569 bool has_additional_channels = (dst_channels != src_channels) ? true : false;
1570 #ifdef PREPROCESSING_ENABLED
1571 bool has_processing = (in->num_preprocessors != 0) ? true : false;
1572 #endif
1573
1574 /* Additional channels might be added on top of main_channels:
1575 * - aux_channels (by processing effects)
1576 * - extra channels due to HW limitations
1577 * In case of additional channels, we cannot work inplace
1578 */
1579 if (has_additional_channels)
1580 proc_buf_out = in->proc_buf_out;
1581 else
1582 proc_buf_out = buffer;
1583
1584 if (list_empty(&in->pcm_dev_list)) {
1585 ALOGE("%s: pcm device list empty", __func__);
1586 return -EINVAL;
1587 }
1588
1589 pcm_device = node_to_item(list_head(&in->pcm_dev_list),
1590 struct pcm_device, stream_list_node);
1591
1592 #ifdef PREPROCESSING_ENABLED
1593 if (has_processing) {
1594 /* since all the processing below is done in frames and using the config.channels
1595 * as the number of channels, no changes is required in case aux_channels are present */
1596 while (frames_wr < frames) {
1597 /* first reload enough frames at the end of process input buffer */
1598 if (in->proc_buf_frames < (size_t)frames) {
1599 ssize_t frames_rd;
1600 if (in->proc_buf_size < (size_t)frames) {
1601 size_t size_in_bytes = pcm_frames_to_bytes(pcm_device->pcm, frames);
1602 in->proc_buf_size = (size_t)frames;
1603 in->proc_buf_in = (int16_t *)realloc(in->proc_buf_in, size_in_bytes);
1604 ALOG_ASSERT((in->proc_buf_in != NULL),
1605 "process_frames() failed to reallocate proc_buf_in");
1606 if (has_additional_channels) {
1607 in->proc_buf_out = (int16_t *)realloc(in->proc_buf_out, size_in_bytes);
1608 ALOG_ASSERT((in->proc_buf_out != NULL),
1609 "process_frames() failed to reallocate proc_buf_out");
1610 proc_buf_out = in->proc_buf_out;
1611 }
1612 }
1613 frames_rd = read_frames(in,
1614 in->proc_buf_in +
1615 in->proc_buf_frames * in->config.channels,
1616 frames - in->proc_buf_frames);
1617 if (frames_rd < 0) {
1618 /* Return error code */
1619 frames_wr = frames_rd;
1620 break;
1621 }
1622 in->proc_buf_frames += frames_rd;
1623 }
1624
1625 if (in->echo_reference != NULL) {
1626 push_echo_reference(in, in->proc_buf_frames);
1627 }
1628
1629 /* in_buf.frameCount and out_buf.frameCount indicate respectively
1630 * the maximum number of frames to be consumed and produced by process() */
1631 in_buf.frameCount = in->proc_buf_frames;
1632 in_buf.s16 = in->proc_buf_in;
1633 out_buf.frameCount = frames - frames_wr;
1634 out_buf.s16 = (int16_t *)proc_buf_out + frames_wr * in->config.channels;
1635
1636 /* FIXME: this works because of current pre processing library implementation that
1637 * does the actual process only when the last enabled effect process is called.
1638 * The generic solution is to have an output buffer for each effect and pass it as
1639 * input to the next.
1640 */
1641 for (i = 0; i < in->num_preprocessors; i++) {
1642 (*in->preprocessors[i].effect_itfe)->process(in->preprocessors[i].effect_itfe,
1643 &in_buf,
1644 &out_buf);
1645 }
1646
1647 /* process() has updated the number of frames consumed and produced in
1648 * in_buf.frameCount and out_buf.frameCount respectively
1649 * move remaining frames to the beginning of in->proc_buf_in */
1650 in->proc_buf_frames -= in_buf.frameCount;
1651
1652 if (in->proc_buf_frames) {
1653 memcpy(in->proc_buf_in,
1654 in->proc_buf_in + in_buf.frameCount * in->config.channels,
1655 in->proc_buf_frames * in->config.channels * sizeof(int16_t));
1656 }
1657
1658 /* if not enough frames were passed to process(), read more and retry. */
1659 if (out_buf.frameCount == 0) {
1660 ALOGW("No frames produced by preproc");
1661 continue;
1662 }
1663
1664 if ((frames_wr + (ssize_t)out_buf.frameCount) <= frames) {
1665 frames_wr += out_buf.frameCount;
1666 } else {
1667 /* The effect does not comply to the API. In theory, we should never end up here! */
1668 ALOGE("preprocessing produced too many frames: %d + %zd > %d !",
1669 (unsigned int)frames_wr, out_buf.frameCount, (unsigned int)frames);
1670 frames_wr = frames;
1671 }
1672 }
1673 }
1674 else
1675 #endif //PREPROCESSING_ENABLED
1676 {
1677 /* No processing effects attached */
1678 if (has_additional_channels) {
1679 /* With additional channels, we cannot use original buffer */
1680 if (in->proc_buf_size < (size_t)frames) {
1681 size_t size_in_bytes = pcm_frames_to_bytes(pcm_device->pcm, frames);
1682 in->proc_buf_size = (size_t)frames;
1683 in->proc_buf_out = (int16_t *)realloc(in->proc_buf_out, size_in_bytes);
1684 ALOG_ASSERT((in->proc_buf_out != NULL),
1685 "process_frames() failed to reallocate proc_buf_out");
1686 proc_buf_out = in->proc_buf_out;
1687 }
1688 }
1689 frames_wr = read_frames(in, proc_buf_out, frames);
1690 }
1691
1692 /* Remove all additional channels that have been added on top of main_channels:
1693 * - aux_channels
1694 * - extra channels from HW due to HW limitations
1695 * Assumption is made that the channels are interleaved and that the main
1696 * channels are first. */
1697
1698 if (has_additional_channels)
1699 {
1700 int16_t* src_buffer = (int16_t *)proc_buf_out;
1701 int16_t* dst_buffer = (int16_t *)buffer;
1702
1703 if (dst_channels == 1) {
1704 for (i = frames_wr; i > 0; i--)
1705 {
1706 *dst_buffer++ = *src_buffer;
1707 src_buffer += src_channels;
1708 }
1709 } else {
1710 for (i = frames_wr; i > 0; i--)
1711 {
1712 memcpy(dst_buffer, src_buffer, dst_channels*sizeof(int16_t));
1713 dst_buffer += dst_channels;
1714 src_buffer += src_channels;
1715 }
1716 }
1717 }
1718
1719 return frames_wr;
1720 }
1721
get_next_buffer(struct resampler_buffer_provider * buffer_provider,struct resampler_buffer * buffer)1722 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
1723 struct resampler_buffer* buffer)
1724 {
1725 struct stream_in *in;
1726 struct pcm_device *pcm_device;
1727
1728 if (buffer_provider == NULL || buffer == NULL)
1729 return -EINVAL;
1730
1731 in = (struct stream_in *)((char *)buffer_provider -
1732 offsetof(struct stream_in, buf_provider));
1733
1734 if (list_empty(&in->pcm_dev_list)) {
1735 buffer->raw = NULL;
1736 buffer->frame_count = 0;
1737 in->read_status = -ENODEV;
1738 return -ENODEV;
1739 }
1740
1741 pcm_device = node_to_item(list_head(&in->pcm_dev_list),
1742 struct pcm_device, stream_list_node);
1743
1744 if (in->read_buf_frames == 0) {
1745 size_t size_in_bytes = pcm_frames_to_bytes(pcm_device->pcm, in->config.period_size);
1746 if (in->read_buf_size < in->config.period_size) {
1747 in->read_buf_size = in->config.period_size;
1748 in->read_buf = (int16_t *) realloc(in->read_buf, size_in_bytes);
1749 ALOG_ASSERT((in->read_buf != NULL),
1750 "get_next_buffer() failed to reallocate read_buf");
1751 }
1752
1753 in->read_status = pcm_read(pcm_device->pcm, (void*)in->read_buf, size_in_bytes);
1754
1755 if (in->read_status != 0) {
1756 ALOGE("get_next_buffer() pcm_read error %d", in->read_status);
1757 buffer->raw = NULL;
1758 buffer->frame_count = 0;
1759 return in->read_status;
1760 }
1761 in->read_buf_frames = in->config.period_size;
1762
1763 #ifdef PREPROCESSING_ENABLED
1764 #ifdef HW_AEC_LOOPBACK
1765 if (in->hw_echo_reference) {
1766 struct pcm_device *temp_device = NULL;
1767 struct pcm_device *ref_device = NULL;
1768 struct listnode *node = NULL;
1769 struct echo_reference_buffer b;
1770 size_t size_hw_ref_bytes;
1771 size_t size_hw_ref_frames;
1772 int read_status = 0;
1773
1774 ref_device = node_to_item(list_tail(&in->pcm_dev_list),
1775 struct pcm_device, stream_list_node);
1776 list_for_each(node, &in->pcm_dev_list) {
1777 temp_device = node_to_item(node, struct pcm_device, stream_list_node);
1778 if (temp_device->pcm_profile->id == 1) {
1779 ref_device = temp_device;
1780 break;
1781 }
1782 }
1783 if (ref_device) {
1784 size_hw_ref_bytes = pcm_frames_to_bytes(ref_device->pcm, ref_device->pcm_profile->config.period_size);
1785 size_hw_ref_frames = ref_device->pcm_profile->config.period_size;
1786 if (in->hw_ref_buf_size < size_hw_ref_frames) {
1787 in->hw_ref_buf_size = size_hw_ref_frames;
1788 in->hw_ref_buf = (int16_t *) realloc(in->hw_ref_buf, size_hw_ref_bytes);
1789 ALOG_ASSERT((in->hw_ref_buf != NULL),
1790 "get_next_buffer() failed to reallocate hw_ref_buf");
1791 ALOGV("get_next_buffer(): hw_ref_buf %p extended to %zd bytes",
1792 in->hw_ref_buf, size_hw_ref_bytes);
1793 }
1794
1795 read_status = pcm_read(ref_device->pcm, (void*)in->hw_ref_buf, size_hw_ref_bytes);
1796 if (read_status != 0) {
1797 ALOGE("process_frames() pcm_read error for HW reference %d", read_status);
1798 b.raw = NULL;
1799 b.frame_count = 0;
1800 }
1801 else {
1802 get_capture_reference_delay(in, size_hw_ref_frames, &b);
1803 b.raw = (void *)in->hw_ref_buf;
1804 b.frame_count = size_hw_ref_frames;
1805 if (b.delay_ns != 0)
1806 b.delay_ns = -b.delay_ns; // as this is capture delay, it needs to be subtracted from the microphone delay
1807 in->echo_reference->write(in->echo_reference, &b);
1808 }
1809 }
1810 }
1811 #endif // HW_AEC_LOOPBACK
1812 #endif // PREPROCESSING_ENABLED
1813 }
1814
1815 buffer->frame_count = (buffer->frame_count > in->read_buf_frames) ?
1816 in->read_buf_frames : buffer->frame_count;
1817 buffer->i16 = in->read_buf + (in->config.period_size - in->read_buf_frames) *
1818 in->config.channels;
1819 return in->read_status;
1820 }
1821
release_buffer(struct resampler_buffer_provider * buffer_provider,struct resampler_buffer * buffer)1822 static void release_buffer(struct resampler_buffer_provider *buffer_provider,
1823 struct resampler_buffer* buffer)
1824 {
1825 struct stream_in *in;
1826
1827 if (buffer_provider == NULL || buffer == NULL)
1828 return;
1829
1830 in = (struct stream_in *)((char *)buffer_provider -
1831 offsetof(struct stream_in, buf_provider));
1832
1833 in->read_buf_frames -= buffer->frame_count;
1834 }
1835
1836 /* read_frames() reads frames from kernel driver, down samples to capture rate
1837 * if necessary and output the number of frames requested to the buffer specified */
read_frames(struct stream_in * in,void * buffer,ssize_t frames)1838 static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames)
1839 {
1840 ssize_t frames_wr = 0;
1841
1842 struct pcm_device *pcm_device;
1843
1844 if (list_empty(&in->pcm_dev_list)) {
1845 ALOGE("%s: pcm device list empty", __func__);
1846 return -EINVAL;
1847 }
1848
1849 pcm_device = node_to_item(list_head(&in->pcm_dev_list),
1850 struct pcm_device, stream_list_node);
1851
1852 while (frames_wr < frames) {
1853 size_t frames_rd = frames - frames_wr;
1854 ALOGVV("%s: frames_rd: %zd, frames_wr: %zd, in->config.channels: %d",
1855 __func__,frames_rd,frames_wr,in->config.channels);
1856 if (in->resampler != NULL) {
1857 in->resampler->resample_from_provider(in->resampler,
1858 (int16_t *)((char *)buffer +
1859 pcm_frames_to_bytes(pcm_device->pcm, frames_wr)),
1860 &frames_rd);
1861 } else {
1862 struct resampler_buffer buf = {
1863 { raw : NULL, },
1864 frame_count : frames_rd,
1865 };
1866 get_next_buffer(&in->buf_provider, &buf);
1867 if (buf.raw != NULL) {
1868 memcpy((char *)buffer +
1869 pcm_frames_to_bytes(pcm_device->pcm, frames_wr),
1870 buf.raw,
1871 pcm_frames_to_bytes(pcm_device->pcm, buf.frame_count));
1872 frames_rd = buf.frame_count;
1873 }
1874 release_buffer(&in->buf_provider, &buf);
1875 }
1876 /* in->read_status is updated by getNextBuffer() also called by
1877 * in->resampler->resample_from_provider() */
1878 if (in->read_status != 0)
1879 return in->read_status;
1880
1881 frames_wr += frames_rd;
1882 }
1883 return frames_wr;
1884 }
1885
in_release_pcm_devices(struct stream_in * in)1886 static int in_release_pcm_devices(struct stream_in *in)
1887 {
1888 struct pcm_device *pcm_device;
1889 struct listnode *node;
1890 struct listnode *next;
1891
1892 list_for_each_safe(node, next, &in->pcm_dev_list) {
1893 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
1894 list_remove(node);
1895 free(pcm_device);
1896 }
1897
1898 return 0;
1899 }
1900
stop_input_stream(struct stream_in * in)1901 static int stop_input_stream(struct stream_in *in)
1902 {
1903 struct audio_usecase *uc_info;
1904 struct audio_device *adev = in->dev;
1905
1906 adev->active_input = NULL;
1907 ALOGV("%s: enter: usecase(%d: %s)", __func__,
1908 in->usecase, use_case_table[in->usecase]);
1909 uc_info = get_usecase_from_id(adev, in->usecase);
1910 if (uc_info == NULL) {
1911 ALOGE("%s: Could not find the usecase (%d) in the list",
1912 __func__, in->usecase);
1913 return -EINVAL;
1914 }
1915
1916 /* Disable the tx device */
1917 disable_snd_device(adev, uc_info, uc_info->in_snd_device, true);
1918
1919 list_remove(&uc_info->adev_list_node);
1920 free(uc_info);
1921
1922 if (list_empty(&in->pcm_dev_list)) {
1923 ALOGE("%s: pcm device list empty", __func__);
1924 return -EINVAL;
1925 }
1926
1927 in_release_pcm_devices(in);
1928 list_init(&in->pcm_dev_list);
1929
1930 #ifdef HW_AEC_LOOPBACK
1931 if (in->hw_echo_reference)
1932 {
1933 in->hw_echo_reference = false;
1934 }
1935 #endif
1936
1937 ALOGV("%s: exit", __func__);
1938 return 0;
1939 }
1940
start_input_stream(struct stream_in * in)1941 static int start_input_stream(struct stream_in *in)
1942 {
1943 /* Enable output device and stream routing controls */
1944 int ret = 0;
1945 bool recreate_resampler = false;
1946 struct audio_usecase *uc_info;
1947 struct audio_device *adev = in->dev;
1948 struct pcm_device_profile *pcm_profile;
1949 struct pcm_device *pcm_device;
1950
1951 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
1952 adev->active_input = in;
1953 pcm_profile = get_pcm_device(in->usecase_type, in->devices);
1954 if (pcm_profile == NULL) {
1955 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
1956 __func__, in->usecase);
1957 ret = -EINVAL;
1958 goto error_config;
1959 }
1960
1961 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1962 uc_info->id = in->usecase;
1963 uc_info->type = PCM_CAPTURE;
1964 uc_info->stream = (struct audio_stream *)in;
1965 uc_info->devices = in->devices;
1966 uc_info->in_snd_device = SND_DEVICE_NONE;
1967 uc_info->out_snd_device = SND_DEVICE_NONE;
1968
1969 pcm_device = (struct pcm_device *)calloc(1, sizeof(struct pcm_device));
1970 pcm_device->pcm_profile = pcm_profile;
1971 list_init(&in->pcm_dev_list);
1972 list_add_tail(&in->pcm_dev_list, &pcm_device->stream_list_node);
1973
1974 list_init(&uc_info->mixer_list);
1975 list_add_tail(&uc_info->mixer_list,
1976 &adev_get_mixer_for_card(adev,
1977 pcm_device->pcm_profile->card)->uc_list_node[uc_info->id]);
1978
1979 list_add_tail(&adev->usecase_list, &uc_info->adev_list_node);
1980
1981 select_devices(adev, in->usecase);
1982
1983 /* Config should be updated as profile can be changed between different calls
1984 * to this function:
1985 * - Trigger resampler creation
1986 * - Config needs to be updated */
1987 if (in->config.rate != pcm_profile->config.rate) {
1988 recreate_resampler = true;
1989 }
1990 in->config = pcm_profile->config;
1991
1992 #ifdef PREPROCESSING_ENABLED
1993 if (in->aux_channels_changed) {
1994 in->config.channels = audio_channel_count_from_in_mask(in->main_channels | in->aux_channels);
1995 recreate_resampler = true;
1996 }
1997 #endif
1998
1999 if (in->requested_rate != in->config.rate) {
2000 recreate_resampler = true;
2001 }
2002
2003 if (recreate_resampler) {
2004 if (in->resampler) {
2005 release_resampler(in->resampler);
2006 in->resampler = NULL;
2007 }
2008 in->buf_provider.get_next_buffer = get_next_buffer;
2009 in->buf_provider.release_buffer = release_buffer;
2010 ret = create_resampler(in->config.rate,
2011 in->requested_rate,
2012 in->config.channels,
2013 RESAMPLER_QUALITY_DEFAULT,
2014 &in->buf_provider,
2015 &in->resampler);
2016 }
2017
2018 #ifdef PREPROCESSING_ENABLED
2019 if (in->enable_aec && in->echo_reference == NULL) {
2020 in->echo_reference = get_echo_reference(adev,
2021 AUDIO_FORMAT_PCM_16_BIT,
2022 audio_channel_count_from_in_mask(in->main_channels),
2023 in->requested_rate
2024 );
2025 }
2026
2027 #ifdef HW_AEC_LOOPBACK
2028 if (in->enable_aec) {
2029 ret = get_hw_echo_reference(in);
2030 if (ret!=0)
2031 goto error_open;
2032
2033 /* force ref buffer reallocation */
2034 in->hw_ref_buf_size = 0;
2035 }
2036 #endif
2037 #endif
2038
2039 /* Open the PCM device.
2040 * The HW is limited to support only the default pcm_profile settings.
2041 * As such a change in aux_channels will not have an effect.
2042 */
2043 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d, smp rate %d format %d, \
2044 period_size %d", __func__, pcm_device->pcm_profile->card, pcm_device->pcm_profile->id,
2045 pcm_device->pcm_profile->config.channels,pcm_device->pcm_profile->config.rate,
2046 pcm_device->pcm_profile->config.format, pcm_device->pcm_profile->config.period_size);
2047
2048 if (pcm_profile->type == PCM_HOTWORD_STREAMING) {
2049 if (!adev->sound_trigger_open_for_streaming) {
2050 ALOGE("%s: No handle to sound trigger HAL", __func__);
2051 ret = -EIO;
2052 goto error_open;
2053 }
2054 pcm_device->pcm = NULL;
2055 pcm_device->sound_trigger_handle = adev->sound_trigger_open_for_streaming();
2056 if (pcm_device->sound_trigger_handle <= 0) {
2057 ALOGE("%s: Failed to open DSP for streaming", __func__);
2058 ret = -EIO;
2059 goto error_open;
2060 }
2061 ALOGV("Opened DSP successfully");
2062 } else {
2063 pcm_device->sound_trigger_handle = 0;
2064 pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card, pcm_device->pcm_profile->id,
2065 PCM_IN | PCM_MONOTONIC, &pcm_device->pcm_profile->config);
2066
2067 if (pcm_device->pcm && !pcm_is_ready(pcm_device->pcm)) {
2068 ALOGE("%s: %s", __func__, pcm_get_error(pcm_device->pcm));
2069 pcm_close(pcm_device->pcm);
2070 pcm_device->pcm = NULL;
2071 ret = -EIO;
2072 goto error_open;
2073 }
2074 }
2075
2076 /* force read and proc buffer reallocation in case of frame size or
2077 * channel count change */
2078 in->proc_buf_frames = 0;
2079 in->proc_buf_size = 0;
2080 in->read_buf_size = 0;
2081 in->read_buf_frames = 0;
2082
2083 /* if no supported sample rate is available, use the resampler */
2084 if (in->resampler) {
2085 in->resampler->reset(in->resampler);
2086 }
2087
2088 ALOGV("%s: exit", __func__);
2089 return ret;
2090
2091 error_open:
2092 if (in->resampler) {
2093 release_resampler(in->resampler);
2094 in->resampler = NULL;
2095 }
2096 stop_input_stream(in);
2097
2098 error_config:
2099 ALOGD("%s: exit: status(%d)", __func__, ret);
2100 adev->active_input = NULL;
2101 return ret;
2102 }
2103
lock_input_stream(struct stream_in * in)2104 void lock_input_stream(struct stream_in *in)
2105 {
2106 pthread_mutex_lock(&in->pre_lock);
2107 pthread_mutex_lock(&in->lock);
2108 pthread_mutex_unlock(&in->pre_lock);
2109 }
2110
lock_output_stream(struct stream_out * out)2111 void lock_output_stream(struct stream_out *out)
2112 {
2113 pthread_mutex_lock(&out->pre_lock);
2114 pthread_mutex_lock(&out->lock);
2115 pthread_mutex_unlock(&out->pre_lock);
2116 }
2117
2118
2119 /* must be called with out->lock locked */
send_offload_cmd_l(struct stream_out * out,int command)2120 static int send_offload_cmd_l(struct stream_out* out, int command)
2121 {
2122 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
2123
2124 ALOGVV("%s %d", __func__, command);
2125
2126 cmd->cmd = command;
2127 list_add_tail(&out->offload_cmd_list, &cmd->node);
2128 pthread_cond_signal(&out->offload_cond);
2129 return 0;
2130 }
2131
2132 /* must be called iwth out->lock locked */
stop_compressed_output_l(struct stream_out * out)2133 static void stop_compressed_output_l(struct stream_out *out)
2134 {
2135 out->send_new_metadata = 1;
2136 if (out->compr != NULL) {
2137 compress_stop(out->compr);
2138 while (out->offload_thread_blocked) {
2139 pthread_cond_wait(&out->cond, &out->lock);
2140 }
2141 }
2142 }
2143
offload_thread_loop(void * context)2144 static void *offload_thread_loop(void *context)
2145 {
2146 struct stream_out *out = (struct stream_out *) context;
2147 struct listnode *item;
2148
2149 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
2150 set_sched_policy(0, SP_FOREGROUND);
2151 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
2152
2153 ALOGV("%s", __func__);
2154 lock_output_stream(out);
2155 for (;;) {
2156 struct offload_cmd *cmd = NULL;
2157 stream_callback_event_t event;
2158 bool send_callback = false;
2159
2160 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
2161 __func__, list_empty(&out->offload_cmd_list),
2162 out->offload_state);
2163 if (list_empty(&out->offload_cmd_list)) {
2164 ALOGV("%s SLEEPING", __func__);
2165 pthread_cond_wait(&out->offload_cond, &out->lock);
2166 ALOGV("%s RUNNING", __func__);
2167 continue;
2168 }
2169
2170 item = list_head(&out->offload_cmd_list);
2171 cmd = node_to_item(item, struct offload_cmd, node);
2172 list_remove(item);
2173
2174 ALOGVV("%s STATE %d CMD %d out->compr %p",
2175 __func__, out->offload_state, cmd->cmd, out->compr);
2176
2177 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
2178 free(cmd);
2179 break;
2180 }
2181
2182 if (out->compr == NULL) {
2183 ALOGE("%s: Compress handle is NULL", __func__);
2184 pthread_cond_signal(&out->cond);
2185 continue;
2186 }
2187 out->offload_thread_blocked = true;
2188 pthread_mutex_unlock(&out->lock);
2189 send_callback = false;
2190 switch(cmd->cmd) {
2191 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
2192 compress_wait(out->compr, -1);
2193 send_callback = true;
2194 event = STREAM_CBK_EVENT_WRITE_READY;
2195 break;
2196 case OFFLOAD_CMD_PARTIAL_DRAIN:
2197 compress_next_track(out->compr);
2198 compress_partial_drain(out->compr);
2199 send_callback = true;
2200 event = STREAM_CBK_EVENT_DRAIN_READY;
2201 break;
2202 case OFFLOAD_CMD_DRAIN:
2203 compress_drain(out->compr);
2204 send_callback = true;
2205 event = STREAM_CBK_EVENT_DRAIN_READY;
2206 break;
2207 default:
2208 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
2209 break;
2210 }
2211 lock_output_stream(out);
2212 out->offload_thread_blocked = false;
2213 pthread_cond_signal(&out->cond);
2214 if (send_callback) {
2215 out->offload_callback(event, NULL, out->offload_cookie);
2216 }
2217 free(cmd);
2218 }
2219
2220 pthread_cond_signal(&out->cond);
2221 while (!list_empty(&out->offload_cmd_list)) {
2222 item = list_head(&out->offload_cmd_list);
2223 list_remove(item);
2224 free(node_to_item(item, struct offload_cmd, node));
2225 }
2226 pthread_mutex_unlock(&out->lock);
2227
2228 return NULL;
2229 }
2230
create_offload_callback_thread(struct stream_out * out)2231 static int create_offload_callback_thread(struct stream_out *out)
2232 {
2233 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
2234 list_init(&out->offload_cmd_list);
2235 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
2236 offload_thread_loop, out);
2237 return 0;
2238 }
2239
destroy_offload_callback_thread(struct stream_out * out)2240 static int destroy_offload_callback_thread(struct stream_out *out)
2241 {
2242 lock_output_stream(out);
2243 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
2244
2245 pthread_mutex_unlock(&out->lock);
2246 pthread_join(out->offload_thread, (void **) NULL);
2247 pthread_cond_destroy(&out->offload_cond);
2248
2249 return 0;
2250 }
2251
uc_release_pcm_devices(struct audio_usecase * usecase)2252 static int uc_release_pcm_devices(struct audio_usecase *usecase)
2253 {
2254 struct stream_out *out = (struct stream_out *)usecase->stream;
2255 struct pcm_device *pcm_device;
2256 struct listnode *node;
2257 struct listnode *next;
2258
2259 list_for_each_safe(node, next, &out->pcm_dev_list) {
2260 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
2261 list_remove(node);
2262 free(pcm_device);
2263 }
2264 list_init(&usecase->mixer_list);
2265
2266 return 0;
2267 }
2268
uc_select_pcm_devices(struct audio_usecase * usecase)2269 static int uc_select_pcm_devices(struct audio_usecase *usecase)
2270
2271 {
2272 struct stream_out *out = (struct stream_out *)usecase->stream;
2273 struct pcm_device *pcm_device;
2274 struct pcm_device_profile *pcm_profile;
2275 struct mixer_card *mixer_card;
2276 audio_devices_t devices = usecase->devices;
2277
2278 list_init(&usecase->mixer_list);
2279 list_init(&out->pcm_dev_list);
2280
2281 while ((pcm_profile = get_pcm_device(usecase->type, devices)) != NULL) {
2282 pcm_device = calloc(1, sizeof(struct pcm_device));
2283 pcm_device->pcm_profile = pcm_profile;
2284 list_add_tail(&out->pcm_dev_list, &pcm_device->stream_list_node);
2285 mixer_card = uc_get_mixer_for_card(usecase, pcm_profile->card);
2286 if (mixer_card == NULL) {
2287 mixer_card = adev_get_mixer_for_card(out->dev, pcm_profile->card);
2288 list_add_tail(&usecase->mixer_list, &mixer_card->uc_list_node[usecase->id]);
2289 }
2290 devices &= ~pcm_profile->devices;
2291 }
2292
2293 return 0;
2294 }
2295
out_close_pcm_devices(struct stream_out * out)2296 static int out_close_pcm_devices(struct stream_out *out)
2297 {
2298 struct pcm_device *pcm_device;
2299 struct listnode *node;
2300 struct audio_device *adev = out->dev;
2301
2302 list_for_each(node, &out->pcm_dev_list) {
2303 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
2304 if (pcm_device->sound_trigger_handle > 0) {
2305 adev->sound_trigger_close_for_streaming(pcm_device->sound_trigger_handle);
2306 pcm_device->sound_trigger_handle = 0;
2307 }
2308 if (pcm_device->pcm) {
2309 pcm_close(pcm_device->pcm);
2310 pcm_device->pcm = NULL;
2311 }
2312 if (pcm_device->resampler) {
2313 release_resampler(pcm_device->resampler);
2314 pcm_device->resampler = NULL;
2315 }
2316 if (pcm_device->res_buffer) {
2317 free(pcm_device->res_buffer);
2318 pcm_device->res_buffer = NULL;
2319 }
2320 }
2321
2322 return 0;
2323 }
2324
out_open_pcm_devices(struct stream_out * out)2325 static int out_open_pcm_devices(struct stream_out *out)
2326 {
2327 struct pcm_device *pcm_device;
2328 struct listnode *node;
2329 int ret = 0;
2330
2331 list_for_each(node, &out->pcm_dev_list) {
2332 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
2333 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
2334 __func__, pcm_device->pcm_profile->card, pcm_device->pcm_profile->id);
2335
2336 pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card, pcm_device->pcm_profile->id,
2337 PCM_OUT | PCM_MONOTONIC, &pcm_device->pcm_profile->config);
2338
2339 if (pcm_device->pcm && !pcm_is_ready(pcm_device->pcm)) {
2340 ALOGE("%s: %s", __func__, pcm_get_error(pcm_device->pcm));
2341 pcm_device->pcm = NULL;
2342 ret = -EIO;
2343 goto error_open;
2344 }
2345 /*
2346 * If the stream rate differs from the PCM rate, we need to
2347 * create a resampler.
2348 */
2349 if (out->sample_rate != pcm_device->pcm_profile->config.rate) {
2350 ALOGV("%s: create_resampler(), pcm_device_card(%d), pcm_device_id(%d), \
2351 out_rate(%d), device_rate(%d)",__func__,
2352 pcm_device->pcm_profile->card, pcm_device->pcm_profile->id,
2353 out->sample_rate, pcm_device->pcm_profile->config.rate);
2354 ret = create_resampler(out->sample_rate,
2355 pcm_device->pcm_profile->config.rate,
2356 audio_channel_count_from_out_mask(out->channel_mask),
2357 RESAMPLER_QUALITY_DEFAULT,
2358 NULL,
2359 &pcm_device->resampler);
2360 pcm_device->res_byte_count = 0;
2361 pcm_device->res_buffer = NULL;
2362 }
2363 }
2364 return ret;
2365
2366 error_open:
2367 out_close_pcm_devices(out);
2368 return ret;
2369 }
2370
disable_output_path_l(struct stream_out * out)2371 static int disable_output_path_l(struct stream_out *out)
2372 {
2373 struct audio_device *adev = out->dev;
2374 struct audio_usecase *uc_info;
2375
2376 uc_info = get_usecase_from_id(adev, out->usecase);
2377 if (uc_info == NULL) {
2378 ALOGE("%s: Could not find the usecase (%d) in the list",
2379 __func__, out->usecase);
2380 return -EINVAL;
2381 }
2382 disable_snd_device(adev, uc_info, uc_info->out_snd_device, true);
2383 uc_release_pcm_devices(uc_info);
2384 list_remove(&uc_info->adev_list_node);
2385 free(uc_info);
2386
2387 return 0;
2388 }
2389
enable_output_path_l(struct stream_out * out)2390 static void enable_output_path_l(struct stream_out *out)
2391 {
2392 struct audio_device *adev = out->dev;
2393 struct audio_usecase *uc_info;
2394
2395 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
2396 uc_info->id = out->usecase;
2397 uc_info->type = PCM_PLAYBACK;
2398 uc_info->stream = (struct audio_stream *)out;
2399 uc_info->devices = out->devices;
2400 uc_info->in_snd_device = SND_DEVICE_NONE;
2401 uc_info->out_snd_device = SND_DEVICE_NONE;
2402 uc_select_pcm_devices(uc_info);
2403
2404 list_add_tail(&adev->usecase_list, &uc_info->adev_list_node);
2405 select_devices(adev, out->usecase);
2406 }
2407
stop_output_stream(struct stream_out * out)2408 static int stop_output_stream(struct stream_out *out)
2409 {
2410 int ret = 0;
2411 struct audio_device *adev = out->dev;
2412 bool do_disable = true;
2413
2414 ALOGV("%s: enter: usecase(%d: %s)", __func__,
2415 out->usecase, use_case_table[out->usecase]);
2416
2417 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
2418 adev->offload_fx_stop_output != NULL) {
2419 adev->offload_fx_stop_output(out->handle);
2420
2421 if (out->offload_state == OFFLOAD_STATE_PAUSED ||
2422 out->offload_state == OFFLOAD_STATE_PAUSED_FLUSHED)
2423 do_disable = false;
2424 out->offload_state = OFFLOAD_STATE_IDLE;
2425 }
2426 if (do_disable)
2427 ret = disable_output_path_l(out);
2428
2429 ALOGV("%s: exit: status(%d)", __func__, ret);
2430 return ret;
2431 }
2432
start_output_stream(struct stream_out * out)2433 static int start_output_stream(struct stream_out *out)
2434 {
2435 int ret = 0;
2436 struct audio_device *adev = out->dev;
2437
2438 ALOGV("%s: enter: usecase(%d: %s) devices(%#x) channels(%d)",
2439 __func__, out->usecase, use_case_table[out->usecase], out->devices, out->config.channels);
2440
2441 enable_output_path_l(out);
2442
2443 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2444 out->compr = NULL;
2445 ret = out_open_pcm_devices(out);
2446 if (ret != 0)
2447 goto error_open;
2448 #ifdef PREPROCESSING_ENABLED
2449 out->echo_reference = NULL;
2450 out->echo_reference_generation = adev->echo_reference_generation;
2451 if (adev->echo_reference != NULL)
2452 out->echo_reference = adev->echo_reference;
2453 #endif
2454 } else {
2455 out->compr = compress_open(COMPRESS_CARD, COMPRESS_DEVICE,
2456 COMPRESS_IN, &out->compr_config);
2457 if (out->compr && !is_compress_ready(out->compr)) {
2458 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
2459 compress_close(out->compr);
2460 out->compr = NULL;
2461 ret = -EIO;
2462 goto error_open;
2463 }
2464 if (out->offload_callback)
2465 compress_nonblock(out->compr, out->non_blocking);
2466
2467 if (adev->offload_fx_start_output != NULL)
2468 adev->offload_fx_start_output(out->handle);
2469 }
2470 ALOGV("%s: exit", __func__);
2471 return 0;
2472 error_open:
2473 stop_output_stream(out);
2474 error_config:
2475 return ret;
2476 }
2477
stop_voice_call(struct audio_device * adev)2478 static int stop_voice_call(struct audio_device *adev)
2479 {
2480 struct audio_usecase *uc_info;
2481
2482 ALOGV("%s: enter", __func__);
2483 adev->in_call = false;
2484
2485 /* TODO: implement voice call stop */
2486
2487 uc_info = get_usecase_from_id(adev, USECASE_VOICE_CALL);
2488 if (uc_info == NULL) {
2489 ALOGE("%s: Could not find the usecase (%d) in the list",
2490 __func__, USECASE_VOICE_CALL);
2491 return -EINVAL;
2492 }
2493
2494 disable_snd_device(adev, uc_info, uc_info->out_snd_device, false);
2495 disable_snd_device(adev, uc_info, uc_info->in_snd_device, true);
2496
2497 uc_release_pcm_devices(uc_info);
2498 list_remove(&uc_info->adev_list_node);
2499 free(uc_info);
2500
2501 ALOGV("%s: exit", __func__);
2502 return 0;
2503 }
2504
2505 /* always called with adev lock held */
start_voice_call(struct audio_device * adev)2506 static int start_voice_call(struct audio_device *adev)
2507 {
2508 struct audio_usecase *uc_info;
2509
2510 ALOGV("%s: enter", __func__);
2511
2512 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
2513 uc_info->id = USECASE_VOICE_CALL;
2514 uc_info->type = VOICE_CALL;
2515 uc_info->stream = (struct audio_stream *)adev->primary_output;
2516 uc_info->devices = adev->primary_output->devices;
2517 uc_info->in_snd_device = SND_DEVICE_NONE;
2518 uc_info->out_snd_device = SND_DEVICE_NONE;
2519
2520 uc_select_pcm_devices(uc_info);
2521
2522 list_add_tail(&adev->usecase_list, &uc_info->adev_list_node);
2523
2524 select_devices(adev, USECASE_VOICE_CALL);
2525
2526
2527 /* TODO: implement voice call start */
2528
2529 /* set cached volume */
2530 set_voice_volume_l(adev, adev->voice_volume);
2531
2532 adev->in_call = true;
2533 ALOGV("%s: exit", __func__);
2534 return 0;
2535 }
2536
check_input_parameters(uint32_t sample_rate,audio_format_t format,int channel_count)2537 static int check_input_parameters(uint32_t sample_rate,
2538 audio_format_t format,
2539 int channel_count)
2540 {
2541 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
2542
2543 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
2544
2545 switch (sample_rate) {
2546 case 8000:
2547 case 11025:
2548 case 12000:
2549 case 16000:
2550 case 22050:
2551 case 24000:
2552 case 32000:
2553 case 44100:
2554 case 48000:
2555 break;
2556 default:
2557 return -EINVAL;
2558 }
2559
2560 return 0;
2561 }
2562
get_input_buffer_size(uint32_t sample_rate,audio_format_t format,int channel_count,usecase_type_t usecase_type,audio_devices_t devices)2563 static size_t get_input_buffer_size(uint32_t sample_rate,
2564 audio_format_t format,
2565 int channel_count,
2566 usecase_type_t usecase_type,
2567 audio_devices_t devices)
2568 {
2569 size_t size = 0;
2570 struct pcm_device_profile *pcm_profile;
2571
2572 if (check_input_parameters(sample_rate, format, channel_count) != 0)
2573 return 0;
2574
2575 pcm_profile = get_pcm_device(usecase_type, devices);
2576 if (pcm_profile == NULL)
2577 return 0;
2578
2579 /*
2580 * take resampling into account and return the closest majoring
2581 * multiple of 16 frames, as audioflinger expects audio buffers to
2582 * be a multiple of 16 frames
2583 */
2584 size = (pcm_profile->config.period_size * sample_rate) / pcm_profile->config.rate;
2585 size = ((size + 15) / 16) * 16;
2586
2587 return (size * channel_count * audio_bytes_per_sample(format));
2588
2589 }
2590
out_get_sample_rate(const struct audio_stream * stream)2591 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
2592 {
2593 struct stream_out *out = (struct stream_out *)stream;
2594
2595 return out->sample_rate;
2596 }
2597
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)2598 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
2599 {
2600 (void)stream;
2601 (void)rate;
2602 return -ENOSYS;
2603 }
2604
out_get_buffer_size(const struct audio_stream * stream)2605 static size_t out_get_buffer_size(const struct audio_stream *stream)
2606 {
2607 struct stream_out *out = (struct stream_out *)stream;
2608
2609 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2610 return out->compr_config.fragment_size;
2611 }
2612
2613 return out->config.period_size *
2614 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
2615 }
2616
out_get_channels(const struct audio_stream * stream)2617 static uint32_t out_get_channels(const struct audio_stream *stream)
2618 {
2619 struct stream_out *out = (struct stream_out *)stream;
2620
2621 return out->channel_mask;
2622 }
2623
out_get_format(const struct audio_stream * stream)2624 static audio_format_t out_get_format(const struct audio_stream *stream)
2625 {
2626 struct stream_out *out = (struct stream_out *)stream;
2627
2628 return out->format;
2629 }
2630
out_set_format(struct audio_stream * stream,audio_format_t format)2631 static int out_set_format(struct audio_stream *stream, audio_format_t format)
2632 {
2633 (void)stream;
2634 (void)format;
2635 return -ENOSYS;
2636 }
2637
do_out_standby_l(struct stream_out * out)2638 static int do_out_standby_l(struct stream_out *out)
2639 {
2640 struct audio_device *adev = out->dev;
2641 int status = 0;
2642
2643 out->standby = true;
2644 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2645 out_close_pcm_devices(out);
2646 #ifdef PREPROCESSING_ENABLED
2647 /* stop writing to echo reference */
2648 if (out->echo_reference != NULL) {
2649 out->echo_reference->write(out->echo_reference, NULL);
2650 if (out->echo_reference_generation != adev->echo_reference_generation) {
2651 ALOGV("%s: release_echo_reference %p", __func__, out->echo_reference);
2652 release_echo_reference(out->echo_reference);
2653 out->echo_reference_generation = adev->echo_reference_generation;
2654 }
2655 out->echo_reference = NULL;
2656 }
2657 #endif
2658 } else {
2659 stop_compressed_output_l(out);
2660 out->gapless_mdata.encoder_delay = 0;
2661 out->gapless_mdata.encoder_padding = 0;
2662 if (out->compr != NULL) {
2663 compress_close(out->compr);
2664 out->compr = NULL;
2665 }
2666 }
2667 status = stop_output_stream(out);
2668
2669 return status;
2670 }
2671
out_standby(struct audio_stream * stream)2672 static int out_standby(struct audio_stream *stream)
2673 {
2674 struct stream_out *out = (struct stream_out *)stream;
2675 struct audio_device *adev = out->dev;
2676
2677 ALOGV("%s: enter: usecase(%d: %s)", __func__,
2678 out->usecase, use_case_table[out->usecase]);
2679 lock_output_stream(out);
2680 if (!out->standby) {
2681 pthread_mutex_lock(&adev->lock);
2682 do_out_standby_l(out);
2683 pthread_mutex_unlock(&adev->lock);
2684 }
2685 pthread_mutex_unlock(&out->lock);
2686 ALOGV("%s: exit", __func__);
2687 return 0;
2688 }
2689
out_dump(const struct audio_stream * stream,int fd)2690 static int out_dump(const struct audio_stream *stream, int fd)
2691 {
2692 (void)stream;
2693 (void)fd;
2694
2695 return 0;
2696 }
2697
parse_compress_metadata(struct stream_out * out,struct str_parms * parms)2698 static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
2699 {
2700 int ret = 0;
2701 char value[32];
2702 struct compr_gapless_mdata tmp_mdata;
2703
2704 if (!out || !parms) {
2705 return -EINVAL;
2706 }
2707
2708 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
2709 if (ret >= 0) {
2710 tmp_mdata.encoder_delay = atoi(value); /* what is a good limit check? */
2711 } else {
2712 return -EINVAL;
2713 }
2714
2715 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
2716 if (ret >= 0) {
2717 tmp_mdata.encoder_padding = atoi(value);
2718 } else {
2719 return -EINVAL;
2720 }
2721
2722 out->gapless_mdata = tmp_mdata;
2723 out->send_new_metadata = 1;
2724 ALOGV("%s new encoder delay %u and padding %u", __func__,
2725 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
2726
2727 return 0;
2728 }
2729
2730
out_set_parameters(struct audio_stream * stream,const char * kvpairs)2731 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
2732 {
2733 struct stream_out *out = (struct stream_out *)stream;
2734 struct audio_device *adev = out->dev;
2735 struct audio_usecase *usecase;
2736 struct listnode *node;
2737 struct str_parms *parms;
2738 char value[32];
2739 int ret, val = 0;
2740 struct audio_usecase *uc_info;
2741 bool do_standby = false;
2742 struct pcm_device *pcm_device;
2743 struct pcm_device_profile *pcm_profile;
2744 #ifdef PREPROCESSING_ENABLED
2745 struct stream_in *in = NULL; /* if non-NULL, then force input to standby */
2746 #endif
2747
2748 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s out->devices(%d) adev->mode(%d)",
2749 __func__, out->usecase, use_case_table[out->usecase], kvpairs, out->devices, adev->mode);
2750 parms = str_parms_create_str(kvpairs);
2751 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2752 if (ret >= 0) {
2753 val = atoi(value);
2754 pthread_mutex_lock(&adev->lock_inputs);
2755 lock_output_stream(out);
2756 pthread_mutex_lock(&adev->lock);
2757 #ifdef PREPROCESSING_ENABLED
2758 if (((int)out->devices != val) && (val != 0) && (!out->standby) &&
2759 (out->usecase == USECASE_AUDIO_PLAYBACK)) {
2760 /* reset active input:
2761 * - to attach the echo reference
2762 * - because a change in output device may change mic settings */
2763 if (adev->active_input && (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
2764 adev->active_input->source == AUDIO_SOURCE_MIC)) {
2765 in = adev->active_input;
2766 }
2767 }
2768 #endif
2769 if (val != 0) {
2770 out->devices = val;
2771
2772 if (!out->standby) {
2773 uc_info = get_usecase_from_id(adev, out->usecase);
2774 if (uc_info == NULL) {
2775 ALOGE("%s: Could not find the usecase (%d) in the list",
2776 __func__, out->usecase);
2777 } else {
2778 list_for_each(node, &out->pcm_dev_list) {
2779 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
2780 if ((pcm_device->pcm_profile->devices & val) == 0)
2781 do_standby = true;
2782 val &= ~pcm_device->pcm_profile->devices;
2783 }
2784 if (val != 0)
2785 do_standby = true;
2786 }
2787 if (do_standby)
2788 do_out_standby_l(out);
2789 else {
2790 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2791 uc_info = get_usecase_from_id(adev, USECASE_AUDIO_PLAYBACK);
2792 if (uc_info == NULL) {
2793 ALOGE("%s: Could not find the usecase (%d) in the list",
2794 __func__, USECASE_AUDIO_PLAYBACK);
2795 }
2796 if (uc_info != NULL && uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES) {
2797 ALOGD("Out_set_param: spk+headset enabled\n");
2798 disable_snd_device(adev, uc_info, SND_DEVICE_OUT_SPEAKER, true);
2799 uc_info->out_snd_device = SND_DEVICE_OUT_HEADPHONES;
2800 }
2801 }
2802 select_devices(adev, out->usecase);
2803 }
2804 }
2805
2806 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
2807 (out == adev->primary_output)) {
2808 start_voice_call(adev);
2809 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
2810 (out == adev->primary_output)) {
2811 select_devices(adev, USECASE_VOICE_CALL);
2812 }
2813 }
2814
2815 if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call &&
2816 (out == adev->primary_output)) {
2817 stop_voice_call(adev);
2818 }
2819 pthread_mutex_unlock(&adev->lock);
2820 pthread_mutex_unlock(&out->lock);
2821 #ifdef PREPROCESSING_ENABLED
2822 if (in) {
2823 /* The lock on adev->lock_inputs prevents input stream from being closed */
2824 lock_input_stream(in);
2825 pthread_mutex_lock(&adev->lock);
2826 LOG_ALWAYS_FATAL_IF(in != adev->active_input);
2827 do_in_standby_l(in);
2828 pthread_mutex_unlock(&adev->lock);
2829 pthread_mutex_unlock(&in->lock);
2830 }
2831 #endif
2832 pthread_mutex_unlock(&adev->lock_inputs);
2833 }
2834
2835 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2836 parse_compress_metadata(out, parms);
2837 }
2838
2839 str_parms_destroy(parms);
2840
2841 if (ret > 0)
2842 ret = 0;
2843 ALOGV("%s: exit: code(%d)", __func__, ret);
2844 return ret;
2845 }
2846
out_get_parameters(const struct audio_stream * stream,const char * keys)2847 static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
2848 {
2849 struct stream_out *out = (struct stream_out *)stream;
2850 struct str_parms *query = str_parms_create_str(keys);
2851 char *str;
2852 char value[256];
2853 struct str_parms *reply = str_parms_create();
2854 size_t i, j;
2855 int ret;
2856 bool first = true;
2857 ALOGV("%s: enter: keys - %s", __func__, keys);
2858 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
2859 if (ret >= 0) {
2860 value[0] = '\0';
2861 i = 0;
2862 while (out->supported_channel_masks[i] != 0) {
2863 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
2864 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
2865 if (!first) {
2866 strcat(value, "|");
2867 }
2868 strcat(value, out_channels_name_to_enum_table[j].name);
2869 first = false;
2870 break;
2871 }
2872 }
2873 i++;
2874 }
2875 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
2876 str = str_parms_to_str(reply);
2877 } else {
2878 str = strdup(keys);
2879 }
2880 str_parms_destroy(query);
2881 str_parms_destroy(reply);
2882 ALOGV("%s: exit: returns - %s", __func__, str);
2883 return str;
2884 }
2885
out_get_latency(const struct audio_stream_out * stream)2886 static uint32_t out_get_latency(const struct audio_stream_out *stream)
2887 {
2888 struct stream_out *out = (struct stream_out *)stream;
2889
2890 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
2891 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
2892
2893 return (out->config.period_count * out->config.period_size * 1000) /
2894 (out->config.rate);
2895 }
2896
out_set_volume(struct audio_stream_out * stream,float left,float right)2897 static int out_set_volume(struct audio_stream_out *stream, float left,
2898 float right)
2899 {
2900 struct stream_out *out = (struct stream_out *)stream;
2901 struct audio_device *adev = out->dev;
2902 int offload_volume[2];//For stereo
2903
2904 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
2905 /* only take left channel into account: the API is for stereo anyway */
2906 out->muted = (left == 0.0f);
2907 return 0;
2908 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2909 struct mixer_ctl *ctl;
2910 struct mixer *mixer = NULL;
2911
2912 offload_volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
2913 offload_volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
2914
2915 mixer = mixer_open(MIXER_CARD);
2916 if (!mixer) {
2917 ALOGE("%s unable to open the mixer for card %d, aborting.",
2918 __func__, MIXER_CARD);
2919 return -EINVAL;
2920 }
2921 ctl = mixer_get_ctl_by_name(mixer, MIXER_CTL_COMPRESS_PLAYBACK_VOLUME);
2922 if (!ctl) {
2923 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2924 __func__, MIXER_CTL_COMPRESS_PLAYBACK_VOLUME);
2925 mixer_close(mixer);
2926 return -EINVAL;
2927 }
2928 ALOGD("out_set_volume set offload volume (%f, %f)", left, right);
2929 mixer_ctl_set_array(ctl, offload_volume,
2930 sizeof(offload_volume)/sizeof(offload_volume[0]));
2931 mixer_close(mixer);
2932 return 0;
2933 }
2934
2935 return -ENOSYS;
2936 }
2937
tfa9895_config_thread(void * context)2938 static void *tfa9895_config_thread(void *context)
2939 {
2940 ALOGD("%s: enter", __func__);
2941 pthread_detach(pthread_self());
2942 struct audio_device *adev = (struct audio_device *)context;
2943 pthread_mutex_lock(&adev->tfa9895_lock);
2944 adev->tfa9895_init =
2945 adev->htc_acoustic_set_amp_mode(adev->mode, AUDIO_DEVICE_OUT_SPEAKER, 0, 0, false);
2946 if (!adev->tfa9895_init) {
2947 ALOGE("set_amp_mode failed, need to re-config again");
2948 adev->tfa9895_mode_change |= 0x1;
2949 }
2950 ALOGI("@@##tfa9895_config_thread Done!! tfa9895_mode_change=%d", adev->tfa9895_mode_change);
2951 pthread_mutex_unlock(&adev->tfa9895_lock);
2952 dummybuf_thread_close(adev);
2953 return NULL;
2954 }
2955
fast_set_affinity(pid_t tid)2956 static int fast_set_affinity(pid_t tid) {
2957 cpu_set_t cpu_set;
2958 int cpu_num;
2959 const char *irq_procfs = "/proc/asound/irq_affinity";
2960 FILE *fp;
2961
2962 if ((fp = fopen(irq_procfs, "r")) == NULL) {
2963 ALOGW("Procfs node %s not found", irq_procfs);
2964 return -1;
2965 }
2966
2967 if (fscanf(fp, "%d", &cpu_num) != 1) {
2968 ALOGW("Couldn't read CPU id from procfs node %s", irq_procfs);
2969 fclose(fp);
2970 return -1;
2971 }
2972 fclose(fp);
2973
2974 CPU_ZERO(&cpu_set);
2975 CPU_SET(cpu_num, &cpu_set);
2976 return sched_setaffinity(tid, sizeof(cpu_set), &cpu_set);
2977 }
2978
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)2979 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
2980 size_t bytes)
2981 {
2982 struct stream_out *out = (struct stream_out *)stream;
2983 struct audio_device *adev = out->dev;
2984 ssize_t ret = 0;
2985 struct pcm_device *pcm_device;
2986 struct listnode *node;
2987 size_t frame_size = audio_stream_out_frame_size(stream);
2988 size_t frames_wr = 0, frames_rq = 0;
2989 unsigned char *data = NULL;
2990 struct pcm_config config;
2991 #ifdef PREPROCESSING_ENABLED
2992 size_t in_frames = bytes / frame_size;
2993 size_t out_frames = in_frames;
2994 struct stream_in *in = NULL;
2995 #endif
2996 pid_t tid;
2997 int err;
2998
2999 lock_output_stream(out);
3000
3001 if (out->usecase == USECASE_AUDIO_PLAYBACK && !out->is_fastmixer_affinity_set) {
3002 tid = gettid();
3003 err = fast_set_affinity(tid);
3004 if (err < 0) {
3005 ALOGW("Couldn't set affinity for tid %d; error %d", tid, err);
3006 }
3007 out->is_fastmixer_affinity_set = true;
3008 }
3009
3010 if (out->standby) {
3011 #ifdef PREPROCESSING_ENABLED
3012 pthread_mutex_unlock(&out->lock);
3013 /* Prevent input stream from being closed */
3014 pthread_mutex_lock(&adev->lock_inputs);
3015 lock_output_stream(out);
3016 if (!out->standby) {
3017 pthread_mutex_unlock(&adev->lock_inputs);
3018 goto false_alarm;
3019 }
3020 #endif
3021 pthread_mutex_lock(&adev->lock);
3022 ret = start_output_stream(out);
3023 /* ToDo: If use case is compress offload should return 0 */
3024 if (ret != 0) {
3025 pthread_mutex_unlock(&adev->lock);
3026 #ifdef PREPROCESSING_ENABLED
3027 pthread_mutex_unlock(&adev->lock_inputs);
3028 #endif
3029 goto exit;
3030 }
3031 out->standby = false;
3032
3033 #ifdef PREPROCESSING_ENABLED
3034 /* A change in output device may change the microphone selection */
3035 if (adev->active_input &&
3036 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
3037 adev->active_input->source == AUDIO_SOURCE_MIC)) {
3038 in = adev->active_input;
3039 ALOGV("%s: enter:) force_input_standby true", __func__);
3040 }
3041 #endif
3042 pthread_mutex_unlock(&adev->lock);
3043 #ifdef PREPROCESSING_ENABLED
3044 if (!in) {
3045 /* Leave mutex locked iff in != NULL */
3046 pthread_mutex_unlock(&adev->lock_inputs);
3047 }
3048 #endif
3049 }
3050 false_alarm:
3051
3052 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3053 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
3054
3055 if (out->offload_state == OFFLOAD_STATE_PAUSED_FLUSHED) {
3056 ALOGV("start offload write from pause state");
3057 pthread_mutex_lock(&adev->lock);
3058 enable_output_path_l(out);
3059 pthread_mutex_unlock(&adev->lock);
3060 }
3061
3062 if (out->send_new_metadata) {
3063 ALOGVV("send new gapless metadata");
3064 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
3065 out->send_new_metadata = 0;
3066 }
3067
3068 ret = compress_write(out->compr, buffer, bytes);
3069 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
3070 if (ret >= 0 && ret < (ssize_t)bytes) {
3071 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
3072 }
3073 if (out->offload_state != OFFLOAD_STATE_PLAYING) {
3074 compress_start(out->compr);
3075 out->offload_state = OFFLOAD_STATE_PLAYING;
3076 }
3077 pthread_mutex_unlock(&out->lock);
3078 #ifdef PREPROCESSING_ENABLED
3079 if (in) {
3080 /* This mutex was left locked iff in != NULL */
3081 pthread_mutex_unlock(&adev->lock_inputs);
3082 }
3083 #endif
3084 return ret;
3085 } else {
3086 #ifdef PREPROCESSING_ENABLED
3087 if (android_atomic_acquire_load(&adev->echo_reference_generation)
3088 != out->echo_reference_generation) {
3089 pthread_mutex_lock(&adev->lock);
3090 if (out->echo_reference != NULL) {
3091 ALOGV("%s: release_echo_reference %p", __func__, out->echo_reference);
3092 release_echo_reference(out->echo_reference);
3093 }
3094 // note that adev->echo_reference_generation here can be different from the one
3095 // tested above but it doesn't matter as we now have the adev mutex and it is consistent
3096 // with what has been set by get_echo_reference() or put_echo_reference()
3097 out->echo_reference_generation = adev->echo_reference_generation;
3098 out->echo_reference = adev->echo_reference;
3099 ALOGV("%s: update echo reference generation %d", __func__,
3100 out->echo_reference_generation);
3101 pthread_mutex_unlock(&adev->lock);
3102 }
3103 #endif
3104
3105 if (out->muted)
3106 memset((void *)buffer, 0, bytes);
3107 list_for_each(node, &out->pcm_dev_list) {
3108 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
3109 if (pcm_device->resampler) {
3110 if (bytes * pcm_device->pcm_profile->config.rate / out->sample_rate + frame_size
3111 > pcm_device->res_byte_count) {
3112 pcm_device->res_byte_count =
3113 bytes * pcm_device->pcm_profile->config.rate / out->sample_rate + frame_size;
3114 pcm_device->res_buffer =
3115 realloc(pcm_device->res_buffer, pcm_device->res_byte_count);
3116 ALOGV("%s: resampler res_byte_count = %zu", __func__,
3117 pcm_device->res_byte_count);
3118 }
3119 frames_rq = bytes / frame_size;
3120 frames_wr = pcm_device->res_byte_count / frame_size;
3121 ALOGVV("%s: resampler request frames = %d frame_size = %d",
3122 __func__, frames_rq, frame_size);
3123 pcm_device->resampler->resample_from_input(pcm_device->resampler,
3124 (int16_t *)buffer, &frames_rq, (int16_t *)pcm_device->res_buffer, &frames_wr);
3125 ALOGVV("%s: resampler output frames_= %d", __func__, frames_wr);
3126 }
3127 if (pcm_device->pcm) {
3128 #ifdef PREPROCESSING_ENABLED
3129 if (out->echo_reference != NULL && pcm_device->pcm_profile->devices != SND_DEVICE_OUT_SPEAKER) {
3130 struct echo_reference_buffer b;
3131 b.raw = (void *)buffer;
3132 b.frame_count = in_frames;
3133
3134 get_playback_delay(out, out_frames, &b);
3135 out->echo_reference->write(out->echo_reference, &b);
3136 }
3137 #endif
3138 if (adev->tfa9895_mode_change == 0x1) {
3139 if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
3140 pthread_mutex_lock(&adev->tfa9895_lock);
3141 data = (unsigned char *)
3142 calloc(pcm_frames_to_bytes(pcm_device->pcm, out->config.period_size),
3143 sizeof(unsigned char));
3144 if (data) {
3145 int i;
3146
3147 // reopen pcm with stop_threshold = INT_MAX/2
3148 memcpy(&config, &pcm_device->pcm_profile->config,
3149 sizeof(struct pcm_config));
3150 config.stop_threshold = INT_MAX/2;
3151
3152 if (pcm_device->pcm)
3153 pcm_close(pcm_device->pcm);
3154
3155 for (i = 0; i < RETRY_NUMBER; i++) {
3156 pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card,
3157 pcm_device->pcm_profile->id,
3158 PCM_OUT | PCM_MONOTONIC, &config);
3159 if (pcm_device->pcm != NULL && pcm_is_ready(pcm_device->pcm))
3160 break;
3161 else
3162 usleep(10000);
3163 }
3164 if (i >= RETRY_NUMBER)
3165 ALOGE("%s: failed to reopen pcm device", __func__);
3166
3167 if (pcm_device->pcm) {
3168 for (i = out->config.period_count; i > 0; i--)
3169 pcm_write(pcm_device->pcm, (void *)data,
3170 pcm_frames_to_bytes(pcm_device->pcm,
3171 out->config.period_size));
3172 /* TODO: Hold on 100 ms and wait i2s signal ready
3173 before giving dsp related i2c commands */
3174 usleep(100000);
3175 adev->tfa9895_mode_change &= ~0x1;
3176 ALOGD("@@##checking - 2: tfa9895_config_thread: "
3177 "adev->tfa9895_mode_change=%d", adev->tfa9895_mode_change);
3178 adev->tfa9895_init =
3179 adev->htc_acoustic_set_amp_mode(
3180 adev->mode, AUDIO_DEVICE_OUT_SPEAKER, 0, 0, false);
3181 }
3182 free(data);
3183
3184 // reopen pcm with normal stop_threshold
3185 if (pcm_device->pcm)
3186 pcm_close(pcm_device->pcm);
3187
3188 for (i = 0; i < RETRY_NUMBER; i++) {
3189 pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card,
3190 pcm_device->pcm_profile->id,
3191 PCM_OUT | PCM_MONOTONIC, &pcm_device->pcm_profile->config);
3192 if (pcm_device->pcm != NULL && pcm_is_ready(pcm_device->pcm))
3193 break;
3194 else
3195 usleep(10000);
3196 }
3197 if (i >= RETRY_NUMBER) {
3198 ALOGE("%s: failed to reopen pcm device, error return", __func__);
3199 pthread_mutex_unlock(&adev->tfa9895_lock);
3200 pthread_mutex_unlock(&out->lock);
3201 return -1;
3202 }
3203 }
3204 }
3205 pthread_mutex_unlock(&adev->tfa9895_lock);
3206 }
3207 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
3208 if (pcm_device->resampler && pcm_device->res_buffer)
3209 pcm_device->status =
3210 pcm_write(pcm_device->pcm, (void *)pcm_device->res_buffer,
3211 frames_wr * frame_size);
3212 else
3213 pcm_device->status = pcm_write(pcm_device->pcm, (void *)buffer, bytes);
3214 if (pcm_device->status != 0)
3215 ret = pcm_device->status;
3216 }
3217 }
3218 if (ret == 0)
3219 out->written += bytes / (out->config.channels * sizeof(short));
3220 }
3221
3222 exit:
3223 pthread_mutex_unlock(&out->lock);
3224
3225 if (ret != 0) {
3226 list_for_each(node, &out->pcm_dev_list) {
3227 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
3228 if (pcm_device->pcm && pcm_device->status != 0)
3229 ALOGE("%s: error %zd - %s", __func__, ret, pcm_get_error(pcm_device->pcm));
3230 }
3231 out_standby(&out->stream.common);
3232 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
3233 out_get_sample_rate(&out->stream.common));
3234 }
3235
3236 #ifdef PREPROCESSING_ENABLED
3237 if (in) {
3238 /* The lock on adev->lock_inputs prevents input stream from being closed */
3239 lock_input_stream(in);
3240 pthread_mutex_lock(&adev->lock);
3241 LOG_ALWAYS_FATAL_IF(in != adev->active_input);
3242 do_in_standby_l(in);
3243 pthread_mutex_unlock(&adev->lock);
3244 pthread_mutex_unlock(&in->lock);
3245 /* This mutex was left locked iff in != NULL */
3246 pthread_mutex_unlock(&adev->lock_inputs);
3247 }
3248 #endif
3249
3250 return bytes;
3251 }
3252
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)3253 static int out_get_render_position(const struct audio_stream_out *stream,
3254 uint32_t *dsp_frames)
3255 {
3256 struct stream_out *out = (struct stream_out *)stream;
3257 *dsp_frames = 0;
3258 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
3259 lock_output_stream(out);
3260 if (out->compr != NULL) {
3261 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
3262 &out->sample_rate);
3263 ALOGVV("%s rendered frames %d sample_rate %d",
3264 __func__, *dsp_frames, out->sample_rate);
3265 }
3266 pthread_mutex_unlock(&out->lock);
3267 return 0;
3268 } else
3269 return -EINVAL;
3270 }
3271
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)3272 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
3273 {
3274 (void)stream;
3275 (void)effect;
3276 return 0;
3277 }
3278
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)3279 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
3280 {
3281 (void)stream;
3282 (void)effect;
3283 return 0;
3284 }
3285
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)3286 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
3287 int64_t *timestamp)
3288 {
3289 (void)stream;
3290 (void)timestamp;
3291 return -EINVAL;
3292 }
3293
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)3294 static int out_get_presentation_position(const struct audio_stream_out *stream,
3295 uint64_t *frames, struct timespec *timestamp)
3296 {
3297 struct stream_out *out = (struct stream_out *)stream;
3298 int ret = -1;
3299 unsigned long dsp_frames;
3300
3301 lock_output_stream(out);
3302
3303 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3304 if (out->compr != NULL) {
3305 compress_get_tstamp(out->compr, &dsp_frames,
3306 &out->sample_rate);
3307 ALOGVV("%s rendered frames %ld sample_rate %d",
3308 __func__, dsp_frames, out->sample_rate);
3309 *frames = dsp_frames;
3310 ret = 0;
3311 /* this is the best we can do */
3312 clock_gettime(CLOCK_MONOTONIC, timestamp);
3313 }
3314 } else {
3315 /* FIXME: which device to read from? */
3316 if (!list_empty(&out->pcm_dev_list)) {
3317 unsigned int avail;
3318 struct pcm_device *pcm_device = node_to_item(list_head(&out->pcm_dev_list),
3319 struct pcm_device, stream_list_node);
3320
3321 if (pcm_get_htimestamp(pcm_device->pcm, &avail, timestamp) == 0) {
3322 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
3323 int64_t signed_frames = out->written - kernel_buffer_size + avail;
3324 /* This adjustment accounts for buffering after app processor.
3325 It is based on estimated DSP latency per use case, rather than exact. */
3326 signed_frames -=
3327 (render_latency(out->usecase) * out->sample_rate / 1000000LL);
3328
3329 /* It would be unusual for this value to be negative, but check just in case ... */
3330 if (signed_frames >= 0) {
3331 *frames = signed_frames;
3332 ret = 0;
3333 }
3334 }
3335 }
3336 }
3337
3338 pthread_mutex_unlock(&out->lock);
3339
3340 return ret;
3341 }
3342
out_set_callback(struct audio_stream_out * stream,stream_callback_t callback,void * cookie)3343 static int out_set_callback(struct audio_stream_out *stream,
3344 stream_callback_t callback, void *cookie)
3345 {
3346 struct stream_out *out = (struct stream_out *)stream;
3347
3348 ALOGV("%s", __func__);
3349 lock_output_stream(out);
3350 out->offload_callback = callback;
3351 out->offload_cookie = cookie;
3352 pthread_mutex_unlock(&out->lock);
3353 return 0;
3354 }
3355
out_pause(struct audio_stream_out * stream)3356 static int out_pause(struct audio_stream_out* stream)
3357 {
3358 struct stream_out *out = (struct stream_out *)stream;
3359 int status = -ENOSYS;
3360 ALOGV("%s", __func__);
3361 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3362 lock_output_stream(out);
3363 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
3364 status = compress_pause(out->compr);
3365 out->offload_state = OFFLOAD_STATE_PAUSED;
3366 pthread_mutex_lock(&out->dev->lock);
3367 status = disable_output_path_l(out);
3368 pthread_mutex_unlock(&out->dev->lock);
3369 }
3370 pthread_mutex_unlock(&out->lock);
3371 }
3372 return status;
3373 }
3374
out_resume(struct audio_stream_out * stream)3375 static int out_resume(struct audio_stream_out* stream)
3376 {
3377 struct stream_out *out = (struct stream_out *)stream;
3378 int status = -ENOSYS;
3379 ALOGV("%s", __func__);
3380 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3381 status = 0;
3382 lock_output_stream(out);
3383 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
3384 pthread_mutex_lock(&out->dev->lock);
3385 enable_output_path_l(out);
3386 pthread_mutex_unlock(&out->dev->lock);
3387 status = compress_resume(out->compr);
3388 out->offload_state = OFFLOAD_STATE_PLAYING;
3389 }
3390 pthread_mutex_unlock(&out->lock);
3391 }
3392 return status;
3393 }
3394
out_drain(struct audio_stream_out * stream,audio_drain_type_t type)3395 static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
3396 {
3397 struct stream_out *out = (struct stream_out *)stream;
3398 int status = -ENOSYS;
3399 ALOGV("%s", __func__);
3400 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3401 lock_output_stream(out);
3402 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
3403 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
3404 else
3405 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
3406 pthread_mutex_unlock(&out->lock);
3407 }
3408 return status;
3409 }
3410
out_flush(struct audio_stream_out * stream)3411 static int out_flush(struct audio_stream_out* stream)
3412 {
3413 struct stream_out *out = (struct stream_out *)stream;
3414 ALOGV("%s", __func__);
3415 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
3416 lock_output_stream(out);
3417 if (out->offload_state == OFFLOAD_STATE_PLAYING) {
3418 ALOGE("out_flush() called in wrong state %d", out->offload_state);
3419 pthread_mutex_unlock(&out->lock);
3420 return -ENOSYS;
3421 }
3422 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
3423 stop_compressed_output_l(out);
3424 out->offload_state = OFFLOAD_STATE_PAUSED_FLUSHED;
3425 }
3426 pthread_mutex_unlock(&out->lock);
3427 return 0;
3428 }
3429 return -ENOSYS;
3430 }
3431
3432 /** audio_stream_in implementation **/
in_get_sample_rate(const struct audio_stream * stream)3433 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
3434 {
3435 struct stream_in *in = (struct stream_in *)stream;
3436
3437 return in->requested_rate;
3438 }
3439
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)3440 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
3441 {
3442 (void)stream;
3443 (void)rate;
3444 return -ENOSYS;
3445 }
3446
in_get_channels(const struct audio_stream * stream)3447 static uint32_t in_get_channels(const struct audio_stream *stream)
3448 {
3449 struct stream_in *in = (struct stream_in *)stream;
3450
3451 return in->main_channels;
3452 }
3453
in_get_format(const struct audio_stream * stream)3454 static audio_format_t in_get_format(const struct audio_stream *stream)
3455 {
3456 (void)stream;
3457 return AUDIO_FORMAT_PCM_16_BIT;
3458 }
3459
in_set_format(struct audio_stream * stream,audio_format_t format)3460 static int in_set_format(struct audio_stream *stream, audio_format_t format)
3461 {
3462 (void)stream;
3463 (void)format;
3464
3465 return -ENOSYS;
3466 }
3467
in_get_buffer_size(const struct audio_stream * stream)3468 static size_t in_get_buffer_size(const struct audio_stream *stream)
3469 {
3470 struct stream_in *in = (struct stream_in *)stream;
3471
3472 return get_input_buffer_size(in->requested_rate,
3473 in_get_format(stream),
3474 audio_channel_count_from_in_mask(in->main_channels),
3475 in->usecase_type,
3476 in->devices);
3477 }
3478
in_close_pcm_devices(struct stream_in * in)3479 static int in_close_pcm_devices(struct stream_in *in)
3480 {
3481 struct pcm_device *pcm_device;
3482 struct listnode *node;
3483 struct audio_device *adev = in->dev;
3484
3485 list_for_each(node, &in->pcm_dev_list) {
3486 pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
3487 if (pcm_device) {
3488 if (pcm_device->pcm)
3489 pcm_close(pcm_device->pcm);
3490 pcm_device->pcm = NULL;
3491 if (pcm_device->sound_trigger_handle > 0)
3492 adev->sound_trigger_close_for_streaming(pcm_device->sound_trigger_handle);
3493 pcm_device->sound_trigger_handle = 0;
3494 }
3495 }
3496 return 0;
3497 }
3498
3499
3500 /* must be called with stream and hw device mutex locked */
do_in_standby_l(struct stream_in * in)3501 static int do_in_standby_l(struct stream_in *in)
3502 {
3503 int status = 0;
3504
3505 #ifdef PREPROCESSING_ENABLED
3506 struct audio_device *adev = in->dev;
3507 #endif
3508 if (!in->standby) {
3509
3510 in_close_pcm_devices(in);
3511
3512 #ifdef PREPROCESSING_ENABLED
3513 if (in->echo_reference != NULL) {
3514 /* stop reading from echo reference */
3515 in->echo_reference->read(in->echo_reference, NULL);
3516 put_echo_reference(adev, in->echo_reference);
3517 in->echo_reference = NULL;
3518 }
3519 #ifdef HW_AEC_LOOPBACK
3520 if (in->hw_echo_reference)
3521 {
3522 if (in->hw_ref_buf) {
3523 free(in->hw_ref_buf);
3524 in->hw_ref_buf = NULL;
3525 }
3526 }
3527 #endif // HW_AEC_LOOPBACK
3528 #endif // PREPROCESSING_ENABLED
3529
3530 status = stop_input_stream(in);
3531
3532 if (in->read_buf) {
3533 free(in->read_buf);
3534 in->read_buf = NULL;
3535 }
3536
3537 in->standby = 1;
3538 }
3539 return 0;
3540 }
3541
3542 // called with adev->lock_inputs locked
in_standby_l(struct stream_in * in)3543 static int in_standby_l(struct stream_in *in)
3544 {
3545 struct audio_device *adev = in->dev;
3546 int status = 0;
3547 lock_input_stream(in);
3548 if (!in->standby) {
3549 pthread_mutex_lock(&adev->lock);
3550 status = do_in_standby_l(in);
3551 pthread_mutex_unlock(&adev->lock);
3552 }
3553 pthread_mutex_unlock(&in->lock);
3554 return status;
3555 }
3556
in_standby(struct audio_stream * stream)3557 static int in_standby(struct audio_stream *stream)
3558 {
3559 struct stream_in *in = (struct stream_in *)stream;
3560 struct audio_device *adev = in->dev;
3561 int status;
3562 ALOGV("%s: enter", __func__);
3563 pthread_mutex_lock(&adev->lock_inputs);
3564 status = in_standby_l(in);
3565 pthread_mutex_unlock(&adev->lock_inputs);
3566 ALOGV("%s: exit: status(%d)", __func__, status);
3567 return status;
3568 }
3569
in_dump(const struct audio_stream * stream,int fd)3570 static int in_dump(const struct audio_stream *stream, int fd)
3571 {
3572 (void)stream;
3573 (void)fd;
3574
3575 return 0;
3576 }
3577
in_set_parameters(struct audio_stream * stream,const char * kvpairs)3578 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
3579 {
3580 struct stream_in *in = (struct stream_in *)stream;
3581 struct audio_device *adev = in->dev;
3582 struct str_parms *parms;
3583 char *str;
3584 char value[32];
3585 int ret, val = 0;
3586 struct audio_usecase *uc_info;
3587 bool do_standby = false;
3588 struct listnode *node;
3589 struct pcm_device *pcm_device;
3590 struct pcm_device_profile *pcm_profile;
3591
3592 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
3593 parms = str_parms_create_str(kvpairs);
3594
3595 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
3596
3597 pthread_mutex_lock(&adev->lock_inputs);
3598 lock_input_stream(in);
3599 pthread_mutex_lock(&adev->lock);
3600 if (ret >= 0) {
3601 val = atoi(value);
3602 /* no audio source uses val == 0 */
3603 if (((int)in->source != val) && (val != 0)) {
3604 in->source = val;
3605 }
3606 }
3607
3608 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
3609 if (ret >= 0) {
3610 val = atoi(value);
3611 if (((int)in->devices != val) && (val != 0)) {
3612 in->devices = val;
3613 /* If recording is in progress, change the tx device to new device */
3614 if (!in->standby) {
3615 uc_info = get_usecase_from_id(adev, in->usecase);
3616 if (uc_info == NULL) {
3617 ALOGE("%s: Could not find the usecase (%d) in the list",
3618 __func__, in->usecase);
3619 } else {
3620 if (list_empty(&in->pcm_dev_list))
3621 ALOGE("%s: pcm device list empty", __func__);
3622 else {
3623 pcm_device = node_to_item(list_head(&in->pcm_dev_list),
3624 struct pcm_device, stream_list_node);
3625 if ((pcm_device->pcm_profile->devices & val & ~AUDIO_DEVICE_BIT_IN) == 0) {
3626 do_standby = true;
3627 }
3628 }
3629 }
3630 if (do_standby) {
3631 ret = do_in_standby_l(in);
3632 } else
3633 ret = select_devices(adev, in->usecase);
3634 }
3635 }
3636 }
3637 pthread_mutex_unlock(&adev->lock);
3638 pthread_mutex_unlock(&in->lock);
3639 pthread_mutex_unlock(&adev->lock_inputs);
3640 str_parms_destroy(parms);
3641
3642 if (ret > 0)
3643 ret = 0;
3644
3645 ALOGV("%s: exit: status(%d)", __func__, ret);
3646 return ret;
3647 }
3648
in_get_parameters(const struct audio_stream * stream,const char * keys)3649 static char* in_get_parameters(const struct audio_stream *stream,
3650 const char *keys)
3651 {
3652 (void)stream;
3653 (void)keys;
3654
3655 return strdup("");
3656 }
3657
in_set_gain(struct audio_stream_in * stream,float gain)3658 static int in_set_gain(struct audio_stream_in *stream, float gain)
3659 {
3660 (void)stream;
3661 (void)gain;
3662
3663 return 0;
3664 }
3665
read_bytes_from_dsp(struct stream_in * in,void * buffer,size_t bytes)3666 static ssize_t read_bytes_from_dsp(struct stream_in *in, void* buffer,
3667 size_t bytes)
3668 {
3669 struct pcm_device *pcm_device;
3670 struct audio_device *adev = in->dev;
3671
3672 pcm_device = node_to_item(list_head(&in->pcm_dev_list),
3673 struct pcm_device, stream_list_node);
3674
3675 if (pcm_device->sound_trigger_handle > 0)
3676 return adev->sound_trigger_read_samples(pcm_device->sound_trigger_handle, buffer, bytes);
3677 else
3678 return 0;
3679 }
3680
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)3681 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
3682 size_t bytes)
3683 {
3684 struct stream_in *in = (struct stream_in *)stream;
3685 struct audio_device *adev = in->dev;
3686 ssize_t frames = -1;
3687 int ret = -1;
3688 int read_and_process_successful = false;
3689
3690 size_t frames_rq = bytes / audio_stream_in_frame_size(stream);
3691 pid_t tid;
3692 int err;
3693
3694 /* no need to acquire adev->lock_inputs because API contract prevents a close */
3695 lock_input_stream(in);
3696
3697 if (in->usecase == USECASE_AUDIO_CAPTURE && !in->is_fastcapture_affinity_set) {
3698 tid = gettid();
3699 err = fast_set_affinity(tid);
3700 if (err < 0) {
3701 ALOGW("Couldn't set affinity for tid %d; error %d", tid, err);
3702 }
3703 in->is_fastcapture_affinity_set = true;
3704 }
3705
3706 if (in->standby) {
3707 pthread_mutex_unlock(&in->lock);
3708 pthread_mutex_lock(&adev->lock_inputs);
3709 lock_input_stream(in);
3710 if (!in->standby) {
3711 pthread_mutex_unlock(&adev->lock_inputs);
3712 goto false_alarm;
3713 }
3714 pthread_mutex_lock(&adev->lock);
3715 ret = start_input_stream(in);
3716 pthread_mutex_unlock(&adev->lock);
3717 pthread_mutex_unlock(&adev->lock_inputs);
3718 if (ret != 0) {
3719 goto exit;
3720 }
3721 in->standby = 0;
3722 }
3723 false_alarm:
3724
3725 if (!list_empty(&in->pcm_dev_list)) {
3726 if (in->usecase == USECASE_AUDIO_CAPTURE_HOTWORD) {
3727 bytes = read_bytes_from_dsp(in, buffer, bytes);
3728 if (bytes > 0)
3729 read_and_process_successful = true;
3730 } else {
3731 /*
3732 * Read PCM and:
3733 * - resample if needed
3734 * - process if pre-processors are attached
3735 * - discard unwanted channels
3736 */
3737 frames = read_and_process_frames(in, buffer, frames_rq);
3738 if (frames >= 0)
3739 read_and_process_successful = true;
3740 }
3741 }
3742
3743 /*
3744 * Instead of writing zeroes here, we could trust the hardware
3745 * to always provide zeroes when muted.
3746 */
3747 if (read_and_process_successful == true && adev->mic_mute)
3748 memset(buffer, 0, bytes);
3749
3750 exit:
3751 pthread_mutex_unlock(&in->lock);
3752
3753 if (read_and_process_successful == false) {
3754 in_standby(&in->stream.common);
3755 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
3756 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
3757 in->requested_rate);
3758 }
3759 return bytes;
3760 }
3761
in_get_input_frames_lost(struct audio_stream_in * stream)3762 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
3763 {
3764 (void)stream;
3765
3766 return 0;
3767 }
3768
add_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect,bool enable)3769 static int add_remove_audio_effect(const struct audio_stream *stream,
3770 effect_handle_t effect,
3771 bool enable)
3772 {
3773 struct stream_in *in = (struct stream_in *)stream;
3774 struct audio_device *adev = in->dev;
3775 int status = 0;
3776 effect_descriptor_t desc;
3777 #ifdef PREPROCESSING_ENABLED
3778 int i;
3779 #endif
3780 status = (*effect)->get_descriptor(effect, &desc);
3781 if (status != 0)
3782 return status;
3783
3784 ALOGI("add_remove_audio_effect(), effect type: %08x, enable: %d ", desc.type.timeLow, enable);
3785
3786 pthread_mutex_lock(&adev->lock_inputs);
3787 lock_input_stream(in);
3788 pthread_mutex_lock(&in->dev->lock);
3789 #ifndef PREPROCESSING_ENABLED
3790 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
3791 in->enable_aec != enable &&
3792 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
3793 in->enable_aec = enable;
3794 if (!in->standby)
3795 select_devices(in->dev, in->usecase);
3796 }
3797 #else
3798 if ( (in->num_preprocessors > MAX_PREPROCESSORS) && (enable == true) ) {
3799 status = -ENOSYS;
3800 goto exit;
3801 }
3802 if ( enable == true ) {
3803 in->preprocessors[in->num_preprocessors].effect_itfe = effect;
3804 /* add the supported channel of the effect in the channel_configs */
3805 in_read_audio_effect_channel_configs(in, &in->preprocessors[in->num_preprocessors]);
3806 in->num_preprocessors ++;
3807 /* check compatibility between main channel supported and possible auxiliary channels */
3808 in_update_aux_channels(in, effect);//wesley crash
3809 in->aux_channels_changed = true;
3810 } else {
3811 /* if ( enable == false ) */
3812 if (in->num_preprocessors <= 0) {
3813 status = -ENOSYS;
3814 goto exit;
3815 }
3816 status = -EINVAL;
3817 for (i=0; i < in->num_preprocessors; i++) {
3818 if (status == 0) { /* status == 0 means an effect was removed from a previous slot */
3819 in->preprocessors[i - 1].effect_itfe = in->preprocessors[i].effect_itfe;
3820 in->preprocessors[i - 1].channel_configs = in->preprocessors[i].channel_configs;
3821 in->preprocessors[i - 1].num_channel_configs =
3822 in->preprocessors[i].num_channel_configs;
3823 ALOGV("add_remove_audio_effect moving fx from %d to %d", i, i-1);
3824 continue;
3825 }
3826 if ( in->preprocessors[i].effect_itfe == effect ) {
3827 ALOGV("add_remove_audio_effect found fx at index %d", i);
3828 free(in->preprocessors[i].channel_configs);
3829 status = 0;
3830 }
3831 }
3832 if (status != 0)
3833 goto exit;
3834 in->num_preprocessors--;
3835 /* if we remove one effect, at least the last proproc should be reset */
3836 in->preprocessors[in->num_preprocessors].num_channel_configs = 0;
3837 in->preprocessors[in->num_preprocessors].effect_itfe = NULL;
3838 in->preprocessors[in->num_preprocessors].channel_configs = NULL;
3839 in->aux_channels_changed = false;
3840 ALOGV("%s: enable(%d), in->aux_channels_changed(%d)", __func__, enable, in->aux_channels_changed);
3841 }
3842 ALOGI("%s: num_preprocessors = %d", __func__, in->num_preprocessors);
3843
3844 if ( memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) {
3845 in->enable_aec = enable;
3846 ALOGV("add_remove_audio_effect(), FX_IID_AEC, enable: %d", enable);
3847 if (!in->standby) {
3848 select_devices(in->dev, in->usecase);
3849 do_in_standby_l(in);
3850 }
3851 if (in->enable_aec == true) {
3852 in_configure_reverse(in);
3853 }
3854 }
3855 exit:
3856 #endif
3857 ALOGW_IF(status != 0, "add_remove_audio_effect() error %d", status);
3858 pthread_mutex_unlock(&in->dev->lock);
3859 pthread_mutex_unlock(&in->lock);
3860 pthread_mutex_unlock(&adev->lock_inputs);
3861 return status;
3862 }
3863
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)3864 static int in_add_audio_effect(const struct audio_stream *stream,
3865 effect_handle_t effect)
3866 {
3867 ALOGV("%s: effect %p", __func__, effect);
3868 return add_remove_audio_effect(stream, effect, true);
3869 }
3870
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)3871 static int in_remove_audio_effect(const struct audio_stream *stream,
3872 effect_handle_t effect)
3873 {
3874 ALOGV("%s: effect %p", __func__, effect);
3875 return add_remove_audio_effect(stream, effect, false);
3876 }
3877
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)3878 static int adev_open_output_stream(struct audio_hw_device *dev,
3879 audio_io_handle_t handle,
3880 audio_devices_t devices,
3881 audio_output_flags_t flags,
3882 struct audio_config *config,
3883 struct audio_stream_out **stream_out,
3884 const char *address __unused)
3885 {
3886 struct audio_device *adev = (struct audio_device *)dev;
3887 struct stream_out *out;
3888 int i, ret;
3889 struct pcm_device_profile *pcm_profile;
3890
3891 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
3892 __func__, config->sample_rate, config->channel_mask, devices, flags);
3893 *stream_out = NULL;
3894 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
3895
3896 if (devices == AUDIO_DEVICE_NONE)
3897 devices = AUDIO_DEVICE_OUT_SPEAKER;
3898
3899 out->flags = flags;
3900 out->devices = devices;
3901 out->dev = adev;
3902 out->format = config->format;
3903 out->sample_rate = config->sample_rate;
3904 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
3905 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
3906 out->handle = handle;
3907
3908 pcm_profile = get_pcm_device(PCM_PLAYBACK, devices);
3909 if (pcm_profile == NULL) {
3910 ret = -EINVAL;
3911 goto error_open;
3912 }
3913 out->config = pcm_profile->config;
3914
3915 /* Init use case and pcm_config */
3916 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3917 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
3918 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
3919 ALOGE("%s: Unsupported Offload information", __func__);
3920 ret = -EINVAL;
3921 goto error_open;
3922 }
3923 if (!is_supported_format(config->offload_info.format)) {
3924 ALOGE("%s: Unsupported audio format", __func__);
3925 ret = -EINVAL;
3926 goto error_open;
3927 }
3928
3929 out->compr_config.codec = (struct snd_codec *)
3930 calloc(1, sizeof(struct snd_codec));
3931
3932 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
3933 if (config->offload_info.channel_mask)
3934 out->channel_mask = config->offload_info.channel_mask;
3935 else if (config->channel_mask)
3936 out->channel_mask = config->channel_mask;
3937 out->format = config->offload_info.format;
3938 out->sample_rate = config->offload_info.sample_rate;
3939
3940 out->stream.set_callback = out_set_callback;
3941 out->stream.pause = out_pause;
3942 out->stream.resume = out_resume;
3943 out->stream.drain = out_drain;
3944 out->stream.flush = out_flush;
3945
3946 out->compr_config.codec->id =
3947 get_snd_codec_id(config->offload_info.format);
3948 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
3949 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
3950 out->compr_config.codec->sample_rate = config->offload_info.sample_rate;
3951 out->compr_config.codec->bit_rate =
3952 config->offload_info.bit_rate;
3953 out->compr_config.codec->ch_in =
3954 audio_channel_count_from_out_mask(config->channel_mask);
3955 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
3956
3957 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
3958 out->non_blocking = 1;
3959
3960 out->send_new_metadata = 1;
3961 create_offload_callback_thread(out);
3962 out->offload_state = OFFLOAD_STATE_IDLE;
3963
3964 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
3965 __func__, config->offload_info.version,
3966 config->offload_info.bit_rate);
3967 } else if (out->flags & (AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) {
3968 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
3969 out->config = pcm_config_deep_buffer;
3970 out->sample_rate = out->config.rate;
3971 ALOGD("%s: use AUDIO_PLAYBACK_DEEP_BUFFER",__func__);
3972 } else {
3973 out->usecase = USECASE_AUDIO_PLAYBACK;
3974 out->sample_rate = out->config.rate;
3975 }
3976
3977 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
3978 if (adev->primary_output == NULL)
3979 adev->primary_output = out;
3980 else {
3981 ALOGE("%s: Primary output is already opened", __func__);
3982 ret = -EEXIST;
3983 goto error_open;
3984 }
3985 }
3986
3987 /* Check if this usecase is already existing */
3988 pthread_mutex_lock(&adev->lock);
3989 if (get_usecase_from_id(adev, out->usecase) != NULL) {
3990 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
3991 pthread_mutex_unlock(&adev->lock);
3992 ret = -EEXIST;
3993 goto error_open;
3994 }
3995 pthread_mutex_unlock(&adev->lock);
3996
3997 out->stream.common.get_sample_rate = out_get_sample_rate;
3998 out->stream.common.set_sample_rate = out_set_sample_rate;
3999 out->stream.common.get_buffer_size = out_get_buffer_size;
4000 out->stream.common.get_channels = out_get_channels;
4001 out->stream.common.get_format = out_get_format;
4002 out->stream.common.set_format = out_set_format;
4003 out->stream.common.standby = out_standby;
4004 out->stream.common.dump = out_dump;
4005 out->stream.common.set_parameters = out_set_parameters;
4006 out->stream.common.get_parameters = out_get_parameters;
4007 out->stream.common.add_audio_effect = out_add_audio_effect;
4008 out->stream.common.remove_audio_effect = out_remove_audio_effect;
4009 out->stream.get_latency = out_get_latency;
4010 out->stream.set_volume = out_set_volume;
4011 out->stream.write = out_write;
4012 out->stream.get_render_position = out_get_render_position;
4013 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
4014 out->stream.get_presentation_position = out_get_presentation_position;
4015
4016 out->standby = 1;
4017 /* out->muted = false; by calloc() */
4018 /* out->written = 0; by calloc() */
4019
4020 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
4021 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
4022 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
4023
4024 config->format = out->stream.common.get_format(&out->stream.common);
4025 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
4026 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
4027
4028 out->is_fastmixer_affinity_set = false;
4029
4030 *stream_out = &out->stream;
4031 ALOGV("%s: exit", __func__);
4032 return 0;
4033
4034 error_open:
4035 free(out);
4036 *stream_out = NULL;
4037 ALOGD("%s: exit: ret %d", __func__, ret);
4038 return ret;
4039 }
4040
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)4041 static void adev_close_output_stream(struct audio_hw_device *dev,
4042 struct audio_stream_out *stream)
4043 {
4044 struct stream_out *out = (struct stream_out *)stream;
4045 struct audio_device *adev = out->dev;
4046 (void)dev;
4047
4048 ALOGV("%s: enter", __func__);
4049 out_standby(&stream->common);
4050 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
4051 destroy_offload_callback_thread(out);
4052
4053 if (out->compr_config.codec != NULL)
4054 free(out->compr_config.codec);
4055 }
4056 pthread_cond_destroy(&out->cond);
4057 pthread_mutex_destroy(&out->lock);
4058 free(stream);
4059 ALOGV("%s: exit", __func__);
4060 }
4061
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)4062 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
4063 {
4064 struct audio_device *adev = (struct audio_device *)dev;
4065 struct str_parms *parms;
4066 char *str;
4067 char value[32];
4068 int val;
4069 int ret;
4070
4071 ALOGV("%s: enter: %s", __func__, kvpairs);
4072
4073 parms = str_parms_create_str(kvpairs);
4074 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
4075 if (ret >= 0) {
4076 int tty_mode;
4077
4078 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
4079 tty_mode = TTY_MODE_OFF;
4080 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
4081 tty_mode = TTY_MODE_VCO;
4082 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
4083 tty_mode = TTY_MODE_HCO;
4084 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
4085 tty_mode = TTY_MODE_FULL;
4086 else
4087 return -EINVAL;
4088
4089 pthread_mutex_lock(&adev->lock);
4090 if (tty_mode != adev->tty_mode) {
4091 adev->tty_mode = tty_mode;
4092 if (adev->in_call)
4093 select_devices(adev, USECASE_VOICE_CALL);
4094 }
4095 pthread_mutex_unlock(&adev->lock);
4096 }
4097
4098 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
4099 if (ret >= 0) {
4100 /* When set to false, HAL should disable EC and NS
4101 * But it is currently not supported.
4102 */
4103 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
4104 adev->bluetooth_nrec = true;
4105 else
4106 adev->bluetooth_nrec = false;
4107 }
4108
4109 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
4110 if (ret >= 0) {
4111 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
4112 adev->screen_off = false;
4113 else
4114 adev->screen_off = true;
4115 }
4116
4117 ret = str_parms_get_int(parms, "rotation", &val);
4118 if (ret >= 0) {
4119 bool reverse_speakers = false;
4120 switch(val) {
4121 /* FIXME: note that the code below assumes that the speakers are in the correct placement
4122 relative to the user when the device is rotated 90deg from its default rotation. This
4123 assumption is device-specific, not platform-specific like this code. */
4124 case 270:
4125 reverse_speakers = true;
4126 break;
4127 case 0:
4128 case 90:
4129 case 180:
4130 break;
4131 default:
4132 ALOGE("%s: unexpected rotation of %d", __func__, val);
4133 }
4134 pthread_mutex_lock(&adev->lock);
4135 if (adev->speaker_lr_swap != reverse_speakers) {
4136 adev->speaker_lr_swap = reverse_speakers;
4137 /* only update the selected device if there is active pcm playback */
4138 struct audio_usecase *usecase;
4139 struct listnode *node;
4140 list_for_each(node, &adev->usecase_list) {
4141 usecase = node_to_item(node, struct audio_usecase, adev_list_node);
4142 if (usecase->type == PCM_PLAYBACK) {
4143 select_devices(adev, usecase->id);
4144 if (adev->htc_acoustic_spk_reverse)
4145 adev->htc_acoustic_spk_reverse(adev->speaker_lr_swap);
4146 break;
4147 }
4148 }
4149 }
4150 pthread_mutex_unlock(&adev->lock);
4151 }
4152
4153 str_parms_destroy(parms);
4154
4155 if (ret > 0)
4156 ret = 0;
4157
4158 ALOGV("%s: exit with code(%d)", __func__, ret);
4159 return ret;
4160 }
4161
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)4162 static char* adev_get_parameters(const struct audio_hw_device *dev,
4163 const char *keys)
4164 {
4165 (void)dev;
4166 (void)keys;
4167
4168 return strdup("");
4169 }
4170
adev_init_check(const struct audio_hw_device * dev)4171 static int adev_init_check(const struct audio_hw_device *dev)
4172 {
4173 (void)dev;
4174
4175 return 0;
4176 }
4177
adev_set_voice_volume(struct audio_hw_device * dev,float volume)4178 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
4179 {
4180 int ret = 0;
4181 struct audio_device *adev = (struct audio_device *)dev;
4182 pthread_mutex_lock(&adev->lock);
4183 /* cache volume */
4184 adev->voice_volume = volume;
4185 ret = set_voice_volume_l(adev, adev->voice_volume);
4186 pthread_mutex_unlock(&adev->lock);
4187 return ret;
4188 }
4189
adev_set_master_volume(struct audio_hw_device * dev,float volume)4190 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
4191 {
4192 (void)dev;
4193 (void)volume;
4194
4195 return -ENOSYS;
4196 }
4197
adev_get_master_volume(struct audio_hw_device * dev,float * volume)4198 static int adev_get_master_volume(struct audio_hw_device *dev,
4199 float *volume)
4200 {
4201 (void)dev;
4202 (void)volume;
4203
4204 return -ENOSYS;
4205 }
4206
adev_set_master_mute(struct audio_hw_device * dev,bool muted)4207 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
4208 {
4209 (void)dev;
4210 (void)muted;
4211
4212 return -ENOSYS;
4213 }
4214
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)4215 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
4216 {
4217 (void)dev;
4218 (void)muted;
4219
4220 return -ENOSYS;
4221 }
4222
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)4223 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
4224 {
4225 struct audio_device *adev = (struct audio_device *)dev;
4226
4227 pthread_mutex_lock(&adev->lock);
4228 if (adev->mode != mode) {
4229 ALOGI("%s mode = %d", __func__, mode);
4230 adev->mode = mode;
4231 pthread_mutex_lock(&adev->tfa9895_lock);
4232 adev->tfa9895_mode_change |= 0x1;
4233 pthread_mutex_unlock(&adev->tfa9895_lock);
4234 }
4235 pthread_mutex_unlock(&adev->lock);
4236 return 0;
4237 }
4238
adev_set_mic_mute(struct audio_hw_device * dev,bool state)4239 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
4240 {
4241 struct audio_device *adev = (struct audio_device *)dev;
4242 int err = 0;
4243
4244 pthread_mutex_lock(&adev->lock);
4245 adev->mic_mute = state;
4246
4247 if (adev->mode == AUDIO_MODE_IN_CALL) {
4248 /* TODO */
4249 }
4250
4251 pthread_mutex_unlock(&adev->lock);
4252 return err;
4253 }
4254
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)4255 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
4256 {
4257 struct audio_device *adev = (struct audio_device *)dev;
4258
4259 *state = adev->mic_mute;
4260
4261 return 0;
4262 }
4263
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)4264 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
4265 const struct audio_config *config)
4266 {
4267 (void)dev;
4268
4269 /* NOTE: we default to built in mic which may cause a mismatch between what we
4270 * report here and the actual buffer size
4271 */
4272 return get_input_buffer_size(config->sample_rate,
4273 config->format,
4274 audio_channel_count_from_in_mask(config->channel_mask),
4275 PCM_CAPTURE /* usecase_type */,
4276 AUDIO_DEVICE_IN_BUILTIN_MIC);
4277 }
4278
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)4279 static int adev_open_input_stream(struct audio_hw_device *dev,
4280 audio_io_handle_t handle __unused,
4281 audio_devices_t devices,
4282 struct audio_config *config,
4283 struct audio_stream_in **stream_in,
4284 audio_input_flags_t flags,
4285 const char *address __unused,
4286 audio_source_t source)
4287 {
4288 struct audio_device *adev = (struct audio_device *)dev;
4289 struct stream_in *in;
4290 struct pcm_device_profile *pcm_profile;
4291
4292 ALOGV("%s: enter", __func__);
4293
4294 *stream_in = NULL;
4295 if (check_input_parameters(config->sample_rate, config->format,
4296 audio_channel_count_from_in_mask(config->channel_mask)) != 0)
4297 return -EINVAL;
4298
4299 usecase_type_t usecase_type = source == AUDIO_SOURCE_HOTWORD ?
4300 PCM_HOTWORD_STREAMING : flags & AUDIO_INPUT_FLAG_FAST ?
4301 PCM_CAPTURE_LOW_LATENCY : PCM_CAPTURE;
4302 pcm_profile = get_pcm_device(usecase_type, devices);
4303 if (pcm_profile == NULL && usecase_type == PCM_CAPTURE_LOW_LATENCY) {
4304 // a low latency profile may not exist for that device, fall back
4305 // to regular capture. the MixerThread automatically changes
4306 // to non-fast capture based on the buffer size.
4307 flags &= ~AUDIO_INPUT_FLAG_FAST;
4308 usecase_type = PCM_CAPTURE;
4309 pcm_profile = get_pcm_device(usecase_type, devices);
4310 }
4311 if (pcm_profile == NULL)
4312 return -EINVAL;
4313
4314 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
4315
4316 in->stream.common.get_sample_rate = in_get_sample_rate;
4317 in->stream.common.set_sample_rate = in_set_sample_rate;
4318 in->stream.common.get_buffer_size = in_get_buffer_size;
4319 in->stream.common.get_channels = in_get_channels;
4320 in->stream.common.get_format = in_get_format;
4321 in->stream.common.set_format = in_set_format;
4322 in->stream.common.standby = in_standby;
4323 in->stream.common.dump = in_dump;
4324 in->stream.common.set_parameters = in_set_parameters;
4325 in->stream.common.get_parameters = in_get_parameters;
4326 in->stream.common.add_audio_effect = in_add_audio_effect;
4327 in->stream.common.remove_audio_effect = in_remove_audio_effect;
4328 in->stream.set_gain = in_set_gain;
4329 in->stream.read = in_read;
4330 in->stream.get_input_frames_lost = in_get_input_frames_lost;
4331
4332 in->devices = devices;
4333 in->source = source;
4334 in->dev = adev;
4335 in->standby = 1;
4336 in->main_channels = config->channel_mask;
4337 in->requested_rate = config->sample_rate;
4338 if (config->sample_rate != CAPTURE_DEFAULT_SAMPLING_RATE)
4339 flags = flags & ~AUDIO_INPUT_FLAG_FAST;
4340 in->input_flags = flags;
4341 /* HW codec is limited to default channels. No need to update with
4342 * requested channels */
4343 in->config = pcm_profile->config;
4344
4345 /* Update config params with the requested sample rate and channels */
4346 if (source == AUDIO_SOURCE_HOTWORD) {
4347 in->usecase = USECASE_AUDIO_CAPTURE_HOTWORD;
4348 } else {
4349 in->usecase = USECASE_AUDIO_CAPTURE;
4350 }
4351 in->usecase_type = usecase_type;
4352
4353 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
4354 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
4355
4356 in->is_fastcapture_affinity_set = false;
4357
4358 *stream_in = &in->stream;
4359 ALOGV("%s: exit", __func__);
4360 return 0;
4361 }
4362
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * stream)4363 static void adev_close_input_stream(struct audio_hw_device *dev,
4364 struct audio_stream_in *stream)
4365 {
4366 struct audio_device *adev = (struct audio_device *)dev;
4367 struct stream_in *in = (struct stream_in*)stream;
4368 ALOGV("%s", __func__);
4369
4370 /* prevent concurrent out_set_parameters, or out_write from standby */
4371 pthread_mutex_lock(&adev->lock_inputs);
4372
4373 #ifdef PREPROCESSING_ENABLED
4374 int i;
4375
4376 for (i=0; i<in->num_preprocessors; i++) {
4377 free(in->preprocessors[i].channel_configs);
4378 }
4379
4380 if (in->read_buf) {
4381 free(in->read_buf);
4382 in->read_buf = NULL;
4383 }
4384
4385 if (in->proc_buf_in) {
4386 free(in->proc_buf_in);
4387 in->proc_buf_in = NULL;
4388 }
4389
4390 if (in->proc_buf_out) {
4391 free(in->proc_buf_out);
4392 in->proc_buf_out = NULL;
4393 }
4394
4395 if (in->ref_buf) {
4396 free(in->ref_buf);
4397 in->ref_buf = NULL;
4398 }
4399
4400 if (in->resampler) {
4401 release_resampler(in->resampler);
4402 in->resampler = NULL;
4403 }
4404 #endif
4405
4406 in_standby_l(in);
4407 free(stream);
4408
4409 pthread_mutex_unlock(&adev->lock_inputs);
4410
4411 return;
4412 }
4413
adev_dump(const audio_hw_device_t * device,int fd)4414 static int adev_dump(const audio_hw_device_t *device, int fd)
4415 {
4416 (void)device;
4417 (void)fd;
4418
4419 return 0;
4420 }
4421
adev_close(hw_device_t * device)4422 static int adev_close(hw_device_t *device)
4423 {
4424 struct audio_device *adev = (struct audio_device *)device;
4425 audio_device_ref_count--;
4426 free(adev->snd_dev_ref_cnt);
4427 free_mixer_list(adev);
4428 free(device);
4429 return 0;
4430 }
4431
dummybuf_thread(void * context)4432 static void *dummybuf_thread(void *context)
4433 {
4434 ALOGD("%s: enter", __func__);
4435 pthread_detach(pthread_self());
4436 struct audio_device *adev = (struct audio_device *)context;
4437 struct pcm_config config;
4438 struct mixer *mixer = NULL;
4439 struct mixer_ctl *ctl;
4440 unsigned char *data = NULL;
4441 struct pcm *pcm = NULL;
4442 struct pcm_device_profile *profile = NULL;
4443 audio_devices_t dummybuf_thread_devices = adev->dummybuf_thread_devices;
4444
4445 pthread_mutex_lock(&adev->dummybuf_thread_lock);
4446
4447 memset(&config, 0, sizeof(struct pcm_config));
4448
4449 profile = &pcm_device_playback;
4450
4451 memcpy(&config, &profile->config, sizeof(struct pcm_config));
4452 /* Use large value for stop_threshold so that automatic
4453 trigger for stop is avoided, when this thread fails to write data */
4454 config.stop_threshold = INT_MAX/2;
4455 pcm = pcm_open(profile->card, profile->id,
4456 (PCM_OUT | PCM_MONOTONIC), &config);
4457 if (pcm != NULL && !pcm_is_ready(pcm)) {
4458 ALOGE("pcm_open: card=%d, id=%d is not ready", profile->card, profile->id);
4459 pcm_close(pcm);
4460 pcm = NULL;
4461 } else {
4462 ALOGD("pcm_open: card=%d, id=%d", profile->card, profile->id);
4463 }
4464
4465 mixer = mixer_open(profile->card);
4466 if (mixer) {
4467 if (dummybuf_thread_devices == AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
4468 ctl = mixer_get_ctl_by_name(mixer, MIXER_CTL_HEADPHONE_JACK_SWITCH);
4469 if (ctl != NULL)
4470 mixer_ctl_set_value(ctl, 0, 1);
4471 else {
4472 ALOGE("Invalid mixer control: name(%s): skip dummy thread", MIXER_CTL_HEADPHONE_JACK_SWITCH);
4473 goto exit;
4474 }
4475
4476 ctl = mixer_get_ctl_by_name(mixer, MIXER_CTL_CODEC_VMIXER_CODEC_SWITCH);
4477 if (ctl != NULL)
4478 mixer_ctl_set_value(ctl, 0, 1);
4479 else {
4480 ALOGE("Invalid mixer control: name(%s): skip dummy thread", MIXER_CTL_CODEC_VMIXER_CODEC_SWITCH);
4481 goto exit;
4482 }
4483 } else {
4484 ctl = mixer_get_ctl_by_name(mixer, MIXER_CTL_SPK_VMIXER_SPK_SWITCH);
4485 if (ctl != NULL)
4486 mixer_ctl_set_value(ctl, 0, 1);
4487 else {
4488 ALOGE("Invalid mixer control: name(%s): skip dummy thread", MIXER_CTL_SPK_VMIXER_SPK_SWITCH);
4489 goto exit;
4490 }
4491 }
4492 }
4493
4494 while (1) {
4495 if (pcm) {
4496 if (data == NULL)
4497 data = (unsigned char *)calloc(DEEP_BUFFER_OUTPUT_PERIOD_SIZE * 8,
4498 sizeof(unsigned char));
4499 if (data) {
4500 pcm_write(pcm, (void *)data, DEEP_BUFFER_OUTPUT_PERIOD_SIZE * 8);
4501 adev->dummybuf_thread_active = 1;
4502 } else {
4503 ALOGD("%s: cant open a buffer, retry to open it", __func__);
4504 }
4505 } else {
4506 ALOGD("%s: cant open a output deep stream, retry to open it", __func__);
4507 pcm = pcm_open(profile->card, profile->id,
4508 (PCM_OUT | PCM_MONOTONIC), &config);
4509 if (pcm != NULL && !pcm_is_ready(pcm)) {
4510 ALOGE("pcm_open: card=%d, id=%d is not ready", profile->card, profile->id);
4511 pcm_close(pcm);
4512 pcm = NULL;
4513 } else {
4514 ALOGD("pcm_open: card=%d, id=%d", profile->card, profile->id);
4515 }
4516 }
4517
4518 if (adev->dummybuf_thread_cancel || adev->dummybuf_thread_timeout-- <= 0) {
4519 adev->dummybuf_thread_cancel = 0;
4520 break;
4521 }
4522
4523 pthread_mutex_unlock(&adev->dummybuf_thread_lock);
4524 usleep(3000);
4525 pthread_mutex_lock(&adev->dummybuf_thread_lock);
4526 }
4527
4528 exit:
4529 adev->dummybuf_thread_active = 0;
4530 if (mixer) {
4531 if (dummybuf_thread_devices == AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
4532 ctl = mixer_get_ctl_by_name(mixer, MIXER_CTL_HEADPHONE_JACK_SWITCH);
4533 if (ctl != NULL)
4534 mixer_ctl_set_value(ctl, 0, 0);
4535 ctl = mixer_get_ctl_by_name(mixer, MIXER_CTL_CODEC_VMIXER_CODEC_SWITCH);
4536 if (ctl != NULL)
4537 mixer_ctl_set_value(ctl, 0, 0);
4538 } else {
4539 ctl = mixer_get_ctl_by_name(mixer, MIXER_CTL_SPK_VMIXER_SPK_SWITCH);
4540 if (ctl != NULL)
4541 mixer_ctl_set_value(ctl, 0, 0);
4542 }
4543 mixer_close(mixer);
4544 }
4545 if (pcm) {
4546 pcm_close(pcm);
4547 pcm = NULL;
4548 }
4549 pthread_mutex_unlock(&adev->dummybuf_thread_lock);
4550
4551 if (data)
4552 free(data);
4553
4554 return NULL;
4555 }
4556
dummybuf_thread_open(struct audio_device * adev)4557 static void dummybuf_thread_open(struct audio_device *adev)
4558 {
4559 adev->dummybuf_thread_timeout = 6000; /* in 18 sec */
4560 adev->dummybuf_thread_cancel = 0;
4561 adev->dummybuf_thread_active = 0;
4562 pthread_mutex_init(&adev->dummybuf_thread_lock, (const pthread_mutexattr_t *) NULL);
4563 if (!adev->dummybuf_thread)
4564 pthread_create(&adev->dummybuf_thread, (const pthread_attr_t *) NULL, dummybuf_thread, adev);
4565 }
4566
dummybuf_thread_close(struct audio_device * adev)4567 static void dummybuf_thread_close(struct audio_device *adev)
4568 {
4569 ALOGD("%s: enter", __func__);
4570 int retry_cnt = 20;
4571
4572 if (adev->dummybuf_thread == 0)
4573 return;
4574
4575 pthread_mutex_lock(&adev->dummybuf_thread_lock);
4576 adev->dummybuf_thread_cancel = 1;
4577 pthread_mutex_unlock(&adev->dummybuf_thread_lock);
4578
4579 while (retry_cnt > 0) {
4580 pthread_mutex_lock(&adev->dummybuf_thread_lock);
4581 if (adev->dummybuf_thread_active == 0) {
4582 pthread_mutex_unlock(&adev->dummybuf_thread_lock);
4583 break;
4584 }
4585 pthread_mutex_unlock(&adev->dummybuf_thread_lock);
4586 retry_cnt--;
4587 usleep(1000);
4588 }
4589
4590 pthread_join(adev->dummybuf_thread, (void **) NULL);
4591 pthread_mutex_destroy(&adev->dummybuf_thread_lock);
4592 adev->dummybuf_thread = 0;
4593 }
4594
4595 /* This returns true if the input parameter looks at all plausible as a low latency period size,
4596 * or false otherwise. A return value of true doesn't mean the value is guaranteed to work,
4597 * just that it _might_ work.
4598 */
period_size_is_plausible_for_low_latency(int period_size)4599 static bool period_size_is_plausible_for_low_latency(int period_size)
4600 {
4601 switch (period_size) {
4602 case 64:
4603 case 96:
4604 case 128:
4605 case 192:
4606 case 256:
4607 return true;
4608 default:
4609 return false;
4610 }
4611 }
4612
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)4613 static int adev_open(const hw_module_t *module, const char *name,
4614 hw_device_t **device)
4615 {
4616 struct audio_device *adev;
4617 int retry_count = 0;
4618
4619 ALOGD("%s: enter", __func__);
4620 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
4621
4622 adev = calloc(1, sizeof(struct audio_device));
4623
4624 adev->device.common.tag = HARDWARE_DEVICE_TAG;
4625 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
4626 adev->device.common.module = (struct hw_module_t *)module;
4627 adev->device.common.close = adev_close;
4628
4629 adev->device.init_check = adev_init_check;
4630 adev->device.set_voice_volume = adev_set_voice_volume;
4631 adev->device.set_master_volume = adev_set_master_volume;
4632 adev->device.get_master_volume = adev_get_master_volume;
4633 adev->device.set_master_mute = adev_set_master_mute;
4634 adev->device.get_master_mute = adev_get_master_mute;
4635 adev->device.set_mode = adev_set_mode;
4636 adev->device.set_mic_mute = adev_set_mic_mute;
4637 adev->device.get_mic_mute = adev_get_mic_mute;
4638 adev->device.set_parameters = adev_set_parameters;
4639 adev->device.get_parameters = adev_get_parameters;
4640 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
4641 adev->device.open_output_stream = adev_open_output_stream;
4642 adev->device.close_output_stream = adev_close_output_stream;
4643 adev->device.open_input_stream = adev_open_input_stream;
4644 adev->device.close_input_stream = adev_close_input_stream;
4645 adev->device.dump = adev_dump;
4646
4647 /* Set the default route before the PCM stream is opened */
4648 adev->mode = AUDIO_MODE_NORMAL;
4649 adev->active_input = NULL;
4650 adev->primary_output = NULL;
4651 adev->voice_volume = 1.0f;
4652 adev->tty_mode = TTY_MODE_OFF;
4653 adev->bluetooth_nrec = true;
4654 adev->in_call = false;
4655 /* adev->cur_hdmi_channels = 0; by calloc() */
4656 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
4657
4658 adev->dualmic_config = DUALMIC_CONFIG_NONE;
4659 adev->ns_in_voice_rec = false;
4660
4661 list_init(&adev->usecase_list);
4662
4663 if (mixer_init(adev) != 0) {
4664 free(adev->snd_dev_ref_cnt);
4665 free(adev);
4666 ALOGE("%s: Failed to init, aborting.", __func__);
4667 *device = NULL;
4668 return -EINVAL;
4669 }
4670
4671 if (access(OFFLOAD_FX_LIBRARY_PATH, R_OK) == 0) {
4672 adev->offload_fx_lib = dlopen(OFFLOAD_FX_LIBRARY_PATH, RTLD_NOW);
4673 if (adev->offload_fx_lib == NULL) {
4674 ALOGE("%s: DLOPEN failed for %s", __func__, OFFLOAD_FX_LIBRARY_PATH);
4675 } else {
4676 ALOGV("%s: DLOPEN successful for %s", __func__, OFFLOAD_FX_LIBRARY_PATH);
4677 adev->offload_fx_start_output =
4678 (int (*)(audio_io_handle_t))dlsym(adev->offload_fx_lib,
4679 "visualizer_hal_start_output");
4680 adev->offload_fx_stop_output =
4681 (int (*)(audio_io_handle_t))dlsym(adev->offload_fx_lib,
4682 "visualizer_hal_stop_output");
4683 }
4684 }
4685
4686 if (access(HTC_ACOUSTIC_LIBRARY_PATH, R_OK) == 0) {
4687 adev->htc_acoustic_lib = dlopen(HTC_ACOUSTIC_LIBRARY_PATH, RTLD_NOW);
4688 if (adev->htc_acoustic_lib == NULL) {
4689 ALOGE("%s: DLOPEN failed for %s", __func__, HTC_ACOUSTIC_LIBRARY_PATH);
4690 } else {
4691 ALOGV("%s: DLOPEN successful for %s", __func__, HTC_ACOUSTIC_LIBRARY_PATH);
4692 adev->htc_acoustic_init_rt5506 =
4693 (int (*)())dlsym(adev->htc_acoustic_lib,
4694 "init_rt5506");
4695 adev->htc_acoustic_set_rt5506_amp =
4696 (int (*)(int, int))dlsym(adev->htc_acoustic_lib,
4697 "set_rt5506_amp");
4698 adev->htc_acoustic_set_amp_mode =
4699 (int (*)(int , int, int, int, bool))dlsym(adev->htc_acoustic_lib,
4700 "set_amp_mode");
4701 adev->htc_acoustic_spk_reverse =
4702 (int (*)(bool))dlsym(adev->htc_acoustic_lib,
4703 "spk_reverse");
4704 if (adev->htc_acoustic_spk_reverse)
4705 adev->htc_acoustic_spk_reverse(adev->speaker_lr_swap);
4706 }
4707 }
4708
4709 if (access(SOUND_TRIGGER_HAL_LIBRARY_PATH, R_OK) == 0) {
4710 adev->sound_trigger_lib = dlopen(SOUND_TRIGGER_HAL_LIBRARY_PATH, RTLD_NOW);
4711 if (adev->sound_trigger_lib == NULL) {
4712 ALOGE("%s: DLOPEN failed for %s", __func__, SOUND_TRIGGER_HAL_LIBRARY_PATH);
4713 } else {
4714 ALOGV("%s: DLOPEN successful for %s", __func__, SOUND_TRIGGER_HAL_LIBRARY_PATH);
4715 adev->sound_trigger_open_for_streaming =
4716 (int (*)(void))dlsym(adev->sound_trigger_lib,
4717 "sound_trigger_open_for_streaming");
4718 adev->sound_trigger_read_samples =
4719 (size_t (*)(int, void *, size_t))dlsym(adev->sound_trigger_lib,
4720 "sound_trigger_read_samples");
4721 adev->sound_trigger_close_for_streaming =
4722 (int (*)(int))dlsym(adev->sound_trigger_lib,
4723 "sound_trigger_close_for_streaming");
4724 if (!adev->sound_trigger_open_for_streaming ||
4725 !adev->sound_trigger_read_samples ||
4726 !adev->sound_trigger_close_for_streaming) {
4727
4728 ALOGE("%s: Error grabbing functions in %s", __func__, SOUND_TRIGGER_HAL_LIBRARY_PATH);
4729 adev->sound_trigger_open_for_streaming = 0;
4730 adev->sound_trigger_read_samples = 0;
4731 adev->sound_trigger_close_for_streaming = 0;
4732 }
4733 }
4734 }
4735
4736
4737 *device = &adev->device.common;
4738
4739 if (adev->htc_acoustic_init_rt5506 != NULL)
4740 adev->htc_acoustic_init_rt5506();
4741
4742 if (audio_device_ref_count == 0) {
4743 /* For HS GPIO initial config */
4744 adev->dummybuf_thread_devices = AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
4745 dummybuf_thread_open(adev);
4746 retry_count = RETRY_NUMBER;
4747 while (retry_count-- > 0) {
4748 pthread_mutex_lock(&adev->dummybuf_thread_lock);
4749 if (adev->dummybuf_thread_active != 0) {
4750 pthread_mutex_unlock(&adev->dummybuf_thread_lock);
4751 break;
4752 }
4753 pthread_mutex_unlock(&adev->dummybuf_thread_lock);
4754 usleep(10000);
4755 }
4756 dummybuf_thread_close(adev);
4757
4758 /* For NXP DSP config */
4759 if (adev->htc_acoustic_set_amp_mode) {
4760 pthread_t th;
4761 adev->dummybuf_thread_devices = AUDIO_DEVICE_OUT_SPEAKER;
4762 dummybuf_thread_open(adev);
4763 pthread_mutex_lock(&adev->dummybuf_thread_lock);
4764 retry_count = RETRY_NUMBER;
4765 while (retry_count-- > 0) {
4766 if (adev->dummybuf_thread_active) {
4767 break;
4768 }
4769 pthread_mutex_unlock(&adev->dummybuf_thread_lock);
4770 usleep(10000);
4771 pthread_mutex_lock(&adev->dummybuf_thread_lock);
4772 }
4773 if (adev->dummybuf_thread_active) {
4774 usleep(10000); /* tfa9895 spk amp need more than 1ms i2s signal before giving dsp related i2c commands*/
4775 if (pthread_create(&th, NULL, tfa9895_config_thread, (void* )adev) != 0) {
4776 ALOGE("@@##THREAD_FADE_IN_UPPER_SPEAKER thread create fail");
4777 }
4778 }
4779 pthread_mutex_unlock(&adev->dummybuf_thread_lock);
4780 /* Then, dummybuf_thread_close() is called by tfa9895_config_thread() */
4781 }
4782 }
4783 audio_device_ref_count++;
4784
4785 char value[PROPERTY_VALUE_MAX];
4786 if (property_get("audio_hal.period_size", value, NULL) > 0) {
4787 int trial = atoi(value);
4788 if (period_size_is_plausible_for_low_latency(trial)) {
4789
4790 pcm_device_playback.config.period_size = trial;
4791 pcm_device_playback.config.start_threshold =
4792 PLAYBACK_START_THRESHOLD(trial, PLAYBACK_PERIOD_COUNT);
4793 pcm_device_playback.config.stop_threshold =
4794 PLAYBACK_STOP_THRESHOLD(trial, PLAYBACK_PERIOD_COUNT);
4795
4796 pcm_device_capture_low_latency.config.period_size = trial;
4797 }
4798 }
4799
4800 ALOGV("%s: exit", __func__);
4801 return 0;
4802 }
4803
4804 static struct hw_module_methods_t hal_module_methods = {
4805 .open = adev_open,
4806 };
4807
4808 struct audio_module HAL_MODULE_INFO_SYM = {
4809 .common = {
4810 .tag = HARDWARE_MODULE_TAG,
4811 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
4812 .hal_api_version = HARDWARE_HAL_API_VERSION,
4813 .id = AUDIO_HARDWARE_MODULE_ID,
4814 .name = "NVIDIA Tegra Audio HAL",
4815 .author = "The Android Open Source Project",
4816 .methods = &hal_module_methods,
4817 },
4818 };
4819