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