• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
1027     /* Additional channels might be added on top of main_channels:
1028     * - aux_channels (by processing effects)
1029     * - extra channels due to HW limitations
1030     * In case of additional channels, we cannot work inplace
1031     */
1032     size_t src_channels = in->config.channels;
1033     size_t dst_channels = audio_channel_count_from_in_mask(in->main_channels);
1034     bool channel_remapping_needed = (dst_channels != src_channels);
1035     const size_t src_frame_size = src_channels * bytes_per_sample;
1036 
1037 #ifdef PREPROCESSING_ENABLED
1038     const bool has_processing = in->num_preprocessors != 0;
1039 #else
1040     const bool has_processing = false;
1041 #endif
1042 
1043     /* With additional channels or processing, we need intermediate buffers */
1044     if (channel_remapping_needed || has_processing) {
1045         const size_t src_buffer_size = frames_num * src_frame_size;
1046 
1047         if (in->proc_buf_size < src_buffer_size) {
1048             in->proc_buf_size = src_buffer_size;
1049 #ifdef PREPROCESSING_ENABLED
1050             /* we always reallocate both buffers in case # of effects change dynamically. */
1051             in->proc_buf_in = realloc(in->proc_buf_in, src_buffer_size);
1052             ALOG_ASSERT((in->proc_buf_in != NULL),
1053                     "process_frames() failed to reallocate proc_buf_in");
1054 #endif
1055             in->proc_buf_out = realloc(in->proc_buf_out, src_buffer_size);
1056             ALOG_ASSERT((in->proc_buf_out != NULL),
1057                     "process_frames() failed to reallocate proc_buf_out");
1058         }
1059         if (channel_remapping_needed) {
1060             proc_buf_out = in->proc_buf_out;
1061         }
1062     }
1063 
1064 #ifdef PREPROCESSING_ENABLED
1065     if (has_processing) {
1066         /* since all the processing below is done in frames and using the config.channels
1067          * as the number of channels, no changes is required in case aux_channels are present */
1068         while (frames_wr < frames_num) {
1069             /* first reload enough frames at the end of process input buffer */
1070             if (in->proc_buf_frames < (size_t)frames_num) {
1071                 ssize_t frames_rd = read_frames(in,
1072                         (char *)in->proc_buf_in + in->proc_buf_frames * src_frame_size,
1073                         frames_num - in->proc_buf_frames);
1074                 if (frames_rd < 0) {
1075                     /* Return error code */
1076                     frames_wr = frames_rd;
1077                     break;
1078                 }
1079                 in->proc_buf_frames += frames_rd;
1080             }
1081 
1082              /* in_buf.frameCount and out_buf.frameCount indicate respectively
1083               * the maximum number of frames to be consumed and produced by process() */
1084             audio_buffer_t in_buf;
1085             audio_buffer_t out_buf;
1086 
1087             in_buf.frameCount = in->proc_buf_frames;
1088             in_buf.s16 = in->proc_buf_in;  /* currently assumes PCM 16 effects */
1089             out_buf.frameCount = frames_num - frames_wr;
1090             out_buf.s16 = (int16_t *)proc_buf_out + frames_wr * src_channels;
1091 
1092             /* FIXME: this works because of current pre processing library implementation that
1093              * does the actual process only when the last enabled effect process is called.
1094              * The generic solution is to have an output buffer for each effect and pass it as
1095              * input to the next.
1096              */
1097             for (int i = 0; i < in->num_preprocessors; i++) {
1098                 (*in->preprocessors[i].effect_itfe)->process(in->preprocessors[i].effect_itfe,
1099                                                    &in_buf,
1100                                                    &out_buf);
1101             }
1102 
1103             /* process() has updated the number of frames consumed and produced in
1104              * in_buf.frameCount and out_buf.frameCount respectively
1105              * move remaining frames to the beginning of in->proc_buf_in */
1106             in->proc_buf_frames -= in_buf.frameCount;
1107 
1108             if (in->proc_buf_frames) {
1109                 memcpy(in->proc_buf_in,
1110                        (char *)in->proc_buf_in + in_buf.frameCount * src_frame_size,
1111                        in->proc_buf_frames * src_frame_size);
1112             }
1113 
1114             /* if not enough frames were passed to process(), read more and retry. */
1115             if (out_buf.frameCount == 0) {
1116                 ALOGW("No frames produced by preproc");
1117                 continue;
1118             }
1119 
1120             if ((frames_wr + (ssize_t)out_buf.frameCount) <= frames_num) {
1121                 frames_wr += out_buf.frameCount;
1122             } else {
1123                 /* The effect does not comply to the API. In theory, we should never end up here! */
1124                 ALOGE("preprocessing produced too many frames: %d + %zd  > %d !",
1125                       (unsigned int)frames_wr, out_buf.frameCount, (unsigned int)frames_num);
1126                 frames_wr = frames_num;
1127             }
1128         }
1129     }
1130     else
1131 #endif //PREPROCESSING_ENABLED
1132     {
1133         /* No processing effects attached */
1134         frames_wr = read_frames(in, proc_buf_out, frames_num);
1135         ALOG_ASSERT(frames_wr <= frames_num, "read more frames than requested");
1136     }
1137 
1138     /* check negative frames_wr (error) before channel remapping to avoid overwriting memory. */
1139     if (channel_remapping_needed && frames_wr > 0) {
1140         size_t ret = adjust_channels(proc_buf_out, src_channels, buffer, dst_channels,
1141             bytes_per_sample, frames_wr * src_frame_size);
1142         ALOG_ASSERT(ret == (frames_wr * dst_channels * bytes_per_sample));
1143     }
1144 
1145     return frames_wr;
1146 }
1147 
get_next_buffer(struct resampler_buffer_provider * buffer_provider,struct resampler_buffer * buffer)1148 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
1149                                    struct resampler_buffer* buffer)
1150 {
1151     struct stream_in *in;
1152     struct pcm_device *pcm_device;
1153 
1154     if (buffer_provider == NULL || buffer == NULL)
1155         return -EINVAL;
1156 
1157     in = (struct stream_in *)((char *)buffer_provider -
1158                                    offsetof(struct stream_in, buf_provider));
1159 
1160     if (list_empty(&in->pcm_dev_list)) {
1161         buffer->raw = NULL;
1162         buffer->frame_count = 0;
1163         in->read_status = -ENODEV;
1164         return -ENODEV;
1165     }
1166 
1167     pcm_device = node_to_item(list_head(&in->pcm_dev_list),
1168                               struct pcm_device, stream_list_node);
1169 
1170     if (in->read_buf_frames == 0) {
1171         size_t size_in_bytes = pcm_frames_to_bytes(pcm_device->pcm, in->config.period_size);
1172         if (in->read_buf_size < in->config.period_size) {
1173             in->read_buf_size = in->config.period_size;
1174             in->read_buf = (int16_t *) realloc(in->read_buf, size_in_bytes);
1175             ALOG_ASSERT((in->read_buf != NULL),
1176                         "get_next_buffer() failed to reallocate read_buf");
1177         }
1178 
1179         in->read_status = pcm_read(pcm_device->pcm, (void*)in->read_buf, size_in_bytes);
1180 
1181         if (in->read_status != 0) {
1182             ALOGE("get_next_buffer() pcm_read error %d", in->read_status);
1183             buffer->raw = NULL;
1184             buffer->frame_count = 0;
1185             return in->read_status;
1186         }
1187         in->read_buf_frames = in->config.period_size;
1188     }
1189 
1190     buffer->frame_count = (buffer->frame_count > in->read_buf_frames) ?
1191                                 in->read_buf_frames : buffer->frame_count;
1192     buffer->i16 = in->read_buf + (in->config.period_size - in->read_buf_frames) *
1193                                                 in->config.channels;
1194     return in->read_status;
1195 }
1196 
release_buffer(struct resampler_buffer_provider * buffer_provider,struct resampler_buffer * buffer)1197 static void release_buffer(struct resampler_buffer_provider *buffer_provider,
1198                                   struct resampler_buffer* buffer)
1199 {
1200     struct stream_in *in;
1201 
1202     if (buffer_provider == NULL || buffer == NULL)
1203         return;
1204 
1205     in = (struct stream_in *)((char *)buffer_provider -
1206                                    offsetof(struct stream_in, buf_provider));
1207 
1208     in->read_buf_frames -= buffer->frame_count;
1209 }
1210 
1211 /* read_frames() reads frames from kernel driver, down samples to capture rate
1212  * if necessary and output the number of frames requested to the buffer specified */
read_frames(struct stream_in * in,void * buffer,ssize_t frames)1213 static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames)
1214 {
1215     ssize_t frames_wr = 0;
1216 
1217     struct pcm_device *pcm_device;
1218 
1219     if (list_empty(&in->pcm_dev_list)) {
1220         ALOGE("%s: pcm device list empty", __func__);
1221         return -EINVAL;
1222     }
1223 
1224     pcm_device = node_to_item(list_head(&in->pcm_dev_list),
1225                               struct pcm_device, stream_list_node);
1226 
1227     while (frames_wr < frames) {
1228         size_t frames_rd = frames - frames_wr;
1229         ALOGVV("%s: frames_rd: %zd, frames_wr: %zd, in->config.channels: %d",
1230                __func__,frames_rd,frames_wr,in->config.channels);
1231         if (in->resampler != NULL) {
1232             in->resampler->resample_from_provider(in->resampler,
1233                     (int16_t *)((char *)buffer +
1234                             pcm_frames_to_bytes(pcm_device->pcm, frames_wr)),
1235                     &frames_rd);
1236         } else {
1237             struct resampler_buffer buf = {
1238                     { raw : NULL, },
1239                     frame_count : frames_rd,
1240             };
1241             get_next_buffer(&in->buf_provider, &buf);
1242             if (buf.raw != NULL) {
1243                 memcpy((char *)buffer +
1244                             pcm_frames_to_bytes(pcm_device->pcm, frames_wr),
1245                         buf.raw,
1246                         pcm_frames_to_bytes(pcm_device->pcm, buf.frame_count));
1247                 frames_rd = buf.frame_count;
1248             }
1249             release_buffer(&in->buf_provider, &buf);
1250         }
1251         /* in->read_status is updated by getNextBuffer() also called by
1252          * in->resampler->resample_from_provider() */
1253         if (in->read_status != 0)
1254             return in->read_status;
1255 
1256         frames_wr += frames_rd;
1257     }
1258     return frames_wr;
1259 }
1260 
in_release_pcm_devices(struct stream_in * in)1261 static int in_release_pcm_devices(struct stream_in *in)
1262 {
1263     struct pcm_device *pcm_device;
1264     struct listnode *node;
1265     struct listnode *next;
1266 
1267     list_for_each_safe(node, next, &in->pcm_dev_list) {
1268         pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
1269         list_remove(node);
1270         free(pcm_device);
1271     }
1272 
1273     return 0;
1274 }
1275 
stop_input_stream(struct stream_in * in)1276 static int stop_input_stream(struct stream_in *in)
1277 {
1278     struct audio_usecase *uc_info;
1279     struct audio_device *adev = in->dev;
1280 
1281     adev->active_input = NULL;
1282     ALOGV("%s: enter: usecase(%d: %s)", __func__,
1283           in->usecase, use_case_table[in->usecase]);
1284     uc_info = get_usecase_from_id(adev, in->usecase);
1285     if (uc_info == NULL) {
1286         ALOGE("%s: Could not find the usecase (%d) in the list",
1287               __func__, in->usecase);
1288         return -EINVAL;
1289     }
1290 
1291     /* Disable the tx device */
1292     disable_snd_device(adev, uc_info, uc_info->in_snd_device, true);
1293 
1294     list_remove(&uc_info->adev_list_node);
1295     free(uc_info);
1296 
1297     if (list_empty(&in->pcm_dev_list)) {
1298         ALOGE("%s: pcm device list empty", __func__);
1299         return -EINVAL;
1300     }
1301 
1302     in_release_pcm_devices(in);
1303     list_init(&in->pcm_dev_list);
1304 
1305     return 0;
1306 }
1307 
start_input_stream(struct stream_in * in)1308 int start_input_stream(struct stream_in *in)
1309 {
1310     /* Enable output device and stream routing controls */
1311     int ret = 0;
1312     bool recreate_resampler = false;
1313     struct audio_usecase *uc_info;
1314     struct audio_device *adev = in->dev;
1315     struct pcm_device_profile *pcm_profile;
1316     struct pcm_device *pcm_device;
1317 
1318     ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
1319     adev->active_input = in;
1320     pcm_profile = get_pcm_device(in->usecase_type, in->devices);
1321     if (pcm_profile == NULL) {
1322         ALOGE("%s: Could not find PCM device id for the usecase(%d)",
1323               __func__, in->usecase);
1324         ret = -EINVAL;
1325         goto error_config;
1326     }
1327 
1328     if (in->input_flags & AUDIO_INPUT_FLAG_FAST) {
1329         ALOGV("%s: change capture period size to low latency size %d",
1330               __func__, CAPTURE_PERIOD_SIZE_LOW_LATENCY);
1331         pcm_profile->config.period_size = CAPTURE_PERIOD_SIZE_LOW_LATENCY;
1332     }
1333 
1334     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1335     uc_info->id = in->usecase;
1336     uc_info->type = PCM_CAPTURE;
1337     uc_info->stream = (struct audio_stream *)in;
1338     uc_info->devices = in->devices;
1339     uc_info->in_snd_device = SND_DEVICE_NONE;
1340     uc_info->out_snd_device = SND_DEVICE_NONE;
1341 
1342     pcm_device = (struct pcm_device *)calloc(1, sizeof(struct pcm_device));
1343     pcm_device->pcm_profile = pcm_profile;
1344     list_init(&in->pcm_dev_list);
1345     list_add_tail(&in->pcm_dev_list, &pcm_device->stream_list_node);
1346 
1347     list_init(&uc_info->mixer_list);
1348     list_add_tail(&uc_info->mixer_list,
1349                   &adev_get_mixer_for_card(adev,
1350                                        pcm_device->pcm_profile->card)->uc_list_node[uc_info->id]);
1351 
1352     list_add_tail(&adev->usecase_list, &uc_info->adev_list_node);
1353 
1354     select_devices(adev, in->usecase);
1355 
1356     /* Config should be updated as profile can be changed between different calls
1357      * to this function:
1358      * - Trigger resampler creation
1359      * - Config needs to be updated */
1360     if (in->config.rate != pcm_profile->config.rate) {
1361         recreate_resampler = true;
1362     }
1363     in->config = pcm_profile->config;
1364 
1365 #ifdef PREPROCESSING_ENABLED
1366     if (in->aux_channels_changed) {
1367         in->config.channels = audio_channel_count_from_in_mask(in->aux_channels);
1368         recreate_resampler = true;
1369     }
1370 #endif
1371 
1372     if (in->requested_rate != in->config.rate) {
1373         recreate_resampler = true;
1374     }
1375 
1376     if (recreate_resampler) {
1377         if (in->resampler) {
1378             release_resampler(in->resampler);
1379             in->resampler = NULL;
1380         }
1381         in->buf_provider.get_next_buffer = get_next_buffer;
1382         in->buf_provider.release_buffer = release_buffer;
1383         ret = create_resampler(in->config.rate,
1384                                in->requested_rate,
1385                                in->config.channels,
1386                                RESAMPLER_QUALITY_DEFAULT,
1387                                &in->buf_provider,
1388                                &in->resampler);
1389     }
1390 
1391     /* Open the PCM device.
1392      * The HW is limited to support only the default pcm_profile settings.
1393      * As such a change in aux_channels will not have an effect.
1394      */
1395     ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d, smp rate %d format %d, \
1396           period_size %d", __func__, pcm_device->pcm_profile->card, pcm_device->pcm_profile->device,
1397           pcm_device->pcm_profile->config.channels,pcm_device->pcm_profile->config.rate,
1398           pcm_device->pcm_profile->config.format, pcm_device->pcm_profile->config.period_size);
1399 
1400     if (pcm_profile->type == PCM_HOTWORD_STREAMING) {
1401         if (!adev->sound_trigger_open_for_streaming) {
1402             ALOGE("%s: No handle to sound trigger HAL", __func__);
1403             ret = -EIO;
1404             goto error_open;
1405         }
1406         pcm_device->pcm = NULL;
1407         pcm_device->sound_trigger_handle =
1408                 adev->sound_trigger_open_for_streaming();
1409         if (pcm_device->sound_trigger_handle <= 0) {
1410             ALOGE("%s: Failed to open DSP for streaming", __func__);
1411             ret = -EIO;
1412             goto error_open;
1413         }
1414         ALOGV("Opened DSP successfully");
1415     } else {
1416         pcm_device->sound_trigger_handle = 0;
1417         pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card,
1418                                    pcm_device->pcm_profile->device,
1419                                    PCM_IN | PCM_MONOTONIC,
1420                                    &pcm_device->pcm_profile->config);
1421         if (pcm_device->pcm && !pcm_is_ready(pcm_device->pcm)) {
1422             ALOGE("%s: %s", __func__, pcm_get_error(pcm_device->pcm));
1423             pcm_close(pcm_device->pcm);
1424             pcm_device->pcm = NULL;
1425             ret = -EIO;
1426             goto error_open;
1427         }
1428     }
1429 
1430     /* force read and proc buffer reallocation in case of frame size or
1431      * channel count change */
1432 #ifdef PREPROCESSING_ENABLED
1433     in->proc_buf_frames = 0;
1434 #endif
1435     in->proc_buf_size = 0;
1436     in->read_buf_size = 0;
1437     in->read_buf_frames = 0;
1438 
1439     /* if no supported sample rate is available, use the resampler */
1440     if (in->resampler) {
1441         in->resampler->reset(in->resampler);
1442     }
1443 
1444     ALOGV("%s: exit", __func__);
1445     return ret;
1446 
1447 error_open:
1448     if (in->resampler) {
1449         release_resampler(in->resampler);
1450         in->resampler = NULL;
1451     }
1452     stop_input_stream(in);
1453 
1454 error_config:
1455     ALOGV("%s: exit: status(%d)", __func__, ret);
1456     adev->active_input = NULL;
1457     return ret;
1458 }
1459 
lock_input_stream(struct stream_in * in)1460 static void lock_input_stream(struct stream_in *in)
1461 {
1462     pthread_mutex_lock(&in->pre_lock);
1463     pthread_mutex_lock(&in->lock);
1464     pthread_mutex_unlock(&in->pre_lock);
1465 }
1466 
lock_output_stream(struct stream_out * out)1467 static void lock_output_stream(struct stream_out *out)
1468 {
1469     pthread_mutex_lock(&out->pre_lock);
1470     pthread_mutex_lock(&out->lock);
1471     pthread_mutex_unlock(&out->pre_lock);
1472 }
1473 
uc_release_pcm_devices(struct audio_usecase * usecase)1474 static int uc_release_pcm_devices(struct audio_usecase *usecase)
1475 {
1476     struct stream_out *out = (struct stream_out *)usecase->stream;
1477     struct pcm_device *pcm_device;
1478     struct listnode *node;
1479     struct listnode *next;
1480 
1481     list_for_each_safe(node, next, &out->pcm_dev_list) {
1482         pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
1483         list_remove(node);
1484         free(pcm_device);
1485     }
1486     list_init(&usecase->mixer_list);
1487 
1488     return 0;
1489 }
1490 
uc_select_pcm_devices(struct audio_usecase * usecase)1491 static int uc_select_pcm_devices(struct audio_usecase *usecase)
1492 
1493 {
1494     struct stream_out *out = (struct stream_out *)usecase->stream;
1495     struct pcm_device *pcm_device;
1496     struct pcm_device_profile *pcm_profile;
1497     struct mixer_card *mixer_card;
1498     audio_devices_t devices = usecase->devices;
1499 
1500     list_init(&usecase->mixer_list);
1501     list_init(&out->pcm_dev_list);
1502 
1503     pcm_profile = get_pcm_device(usecase->type, devices);
1504     if (pcm_profile) {
1505         pcm_device = calloc(1, sizeof(struct pcm_device));
1506         pcm_device->pcm_profile = pcm_profile;
1507         list_add_tail(&out->pcm_dev_list, &pcm_device->stream_list_node);
1508         mixer_card = uc_get_mixer_for_card(usecase, pcm_profile->card);
1509         if (mixer_card == NULL) {
1510             mixer_card = adev_get_mixer_for_card(out->dev, pcm_profile->card);
1511             list_add_tail(&usecase->mixer_list, &mixer_card->uc_list_node[usecase->id]);
1512         }
1513         devices &= ~pcm_profile->devices;
1514     } else {
1515         ALOGE("usecase type=%d, devices=%d did not find exact match",
1516             usecase->type, devices);
1517     }
1518 
1519     return 0;
1520 }
1521 
out_close_pcm_devices(struct stream_out * out)1522 static int out_close_pcm_devices(struct stream_out *out)
1523 {
1524     struct pcm_device *pcm_device;
1525     struct listnode *node;
1526     struct audio_device *adev = out->dev;
1527 
1528     list_for_each(node, &out->pcm_dev_list) {
1529         pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
1530         if (pcm_device->sound_trigger_handle > 0) {
1531             adev->sound_trigger_close_for_streaming(
1532                     pcm_device->sound_trigger_handle);
1533             pcm_device->sound_trigger_handle = 0;
1534         }
1535         if (pcm_device->pcm) {
1536             pcm_close(pcm_device->pcm);
1537             pcm_device->pcm = NULL;
1538         }
1539         if (pcm_device->resampler) {
1540             release_resampler(pcm_device->resampler);
1541             pcm_device->resampler = NULL;
1542         }
1543         if (pcm_device->res_buffer) {
1544             free(pcm_device->res_buffer);
1545             pcm_device->res_buffer = NULL;
1546         }
1547         if (pcm_device->dsp_context) {
1548             cras_dsp_context_free(pcm_device->dsp_context);
1549             pcm_device->dsp_context = NULL;
1550         }
1551     }
1552 
1553     return 0;
1554 }
1555 
out_open_pcm_devices(struct stream_out * out)1556 static int out_open_pcm_devices(struct stream_out *out)
1557 {
1558     struct pcm_device *pcm_device;
1559     struct listnode *node;
1560     struct audio_device *adev = out->dev;
1561     int ret = 0;
1562 
1563     list_for_each(node, &out->pcm_dev_list) {
1564         pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
1565         ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1566               __func__, pcm_device->pcm_profile->card, pcm_device->pcm_profile->device);
1567 
1568         if (pcm_device->pcm_profile->dsp_name) {
1569             pcm_device->dsp_context = cras_dsp_context_new(pcm_device->pcm_profile->config.rate,
1570                     (adev->mode == AUDIO_MODE_IN_CALL || adev->mode == AUDIO_MODE_IN_COMMUNICATION)
1571                         ? "voice-comm" : "playback");
1572             if (pcm_device->dsp_context) {
1573                 cras_dsp_set_variable(pcm_device->dsp_context, "dsp_name",
1574                                       pcm_device->pcm_profile->dsp_name);
1575                 cras_dsp_load_pipeline(pcm_device->dsp_context);
1576             }
1577         }
1578 
1579         pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card, pcm_device->pcm_profile->device,
1580                                PCM_OUT | PCM_MONOTONIC, &pcm_device->pcm_profile->config);
1581 
1582         if (pcm_device->pcm && !pcm_is_ready(pcm_device->pcm)) {
1583             ALOGE("%s: %s", __func__, pcm_get_error(pcm_device->pcm));
1584             pcm_device->pcm = NULL;
1585             ret = -EIO;
1586             goto error_open;
1587         }
1588         /*
1589         * If the stream rate differs from the PCM rate, we need to
1590         * create a resampler.
1591         */
1592         if (out->sample_rate != pcm_device->pcm_profile->config.rate) {
1593             ALOGV("%s: create_resampler(), pcm_device_card(%d), pcm_device_id(%d), \
1594                     out_rate(%d), device_rate(%d)",__func__,
1595                     pcm_device->pcm_profile->card, pcm_device->pcm_profile->device,
1596                     out->sample_rate, pcm_device->pcm_profile->config.rate);
1597             ret = create_resampler(out->sample_rate,
1598                     pcm_device->pcm_profile->config.rate,
1599                     audio_channel_count_from_out_mask(out->channel_mask),
1600                     RESAMPLER_QUALITY_DEFAULT,
1601                     NULL,
1602                     &pcm_device->resampler);
1603             pcm_device->res_byte_count = 0;
1604             pcm_device->res_buffer = NULL;
1605         }
1606     }
1607     return ret;
1608 
1609 error_open:
1610     out_close_pcm_devices(out);
1611     return ret;
1612 }
1613 
disable_output_path_l(struct stream_out * out)1614 static int disable_output_path_l(struct stream_out *out)
1615 {
1616     struct audio_device *adev = out->dev;
1617     struct audio_usecase *uc_info;
1618 
1619     uc_info = get_usecase_from_id(adev, out->usecase);
1620     if (uc_info == NULL) {
1621         ALOGE("%s: Could not find the usecase (%d) in the list",
1622              __func__, out->usecase);
1623         return -EINVAL;
1624     }
1625     disable_snd_device(adev, uc_info, uc_info->out_snd_device, true);
1626     uc_release_pcm_devices(uc_info);
1627     list_remove(&uc_info->adev_list_node);
1628     free(uc_info);
1629 
1630     return 0;
1631 }
1632 
enable_output_path_l(struct stream_out * out)1633 static void enable_output_path_l(struct stream_out *out)
1634 {
1635     struct audio_device *adev = out->dev;
1636     struct audio_usecase *uc_info;
1637 
1638     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1639     uc_info->id = out->usecase;
1640     uc_info->type = PCM_PLAYBACK;
1641     uc_info->stream = (struct audio_stream *)out;
1642     uc_info->devices = out->devices;
1643     uc_info->in_snd_device = SND_DEVICE_NONE;
1644     uc_info->out_snd_device = SND_DEVICE_NONE;
1645     uc_select_pcm_devices(uc_info);
1646 
1647     list_add_tail(&adev->usecase_list, &uc_info->adev_list_node);
1648 
1649     select_devices(adev, out->usecase);
1650 }
1651 
stop_output_stream(struct stream_out * out)1652 static int stop_output_stream(struct stream_out *out)
1653 {
1654     int ret = 0;
1655     struct audio_device *adev = out->dev;
1656     bool do_disable = true;
1657 
1658     ALOGV("%s: enter: usecase(%d: %s)", __func__,
1659           out->usecase, use_case_table[out->usecase]);
1660 
1661     ret = disable_output_path_l(out);
1662 
1663     ALOGV("%s: exit: status(%d)", __func__, ret);
1664     return ret;
1665 }
1666 
start_output_stream(struct stream_out * out)1667 int start_output_stream(struct stream_out *out)
1668 {
1669     int ret = 0;
1670     struct audio_device *adev = out->dev;
1671 
1672     ALOGV("%s: enter: usecase(%d: %s) devices(%#x) channels(%d)",
1673           __func__, out->usecase, use_case_table[out->usecase], out->devices, out->config.channels);
1674 
1675     enable_output_path_l(out);
1676 
1677     ret = out_open_pcm_devices(out);
1678     if (ret != 0)
1679         goto error_open;
1680     ALOGV("%s: exit", __func__);
1681     return 0;
1682 error_open:
1683     stop_output_stream(out);
1684     return ret;
1685 }
1686 
stop_voice_call(struct audio_device * adev)1687 static int stop_voice_call(struct audio_device *adev)
1688 {
1689     struct audio_usecase *uc_info;
1690 
1691     ALOGV("%s: enter", __func__);
1692     adev->in_call = false;
1693 
1694     /* TODO: implement voice call stop */
1695 
1696     uc_info = get_usecase_from_id(adev, USECASE_VOICE_CALL);
1697     if (uc_info == NULL) {
1698         ALOGE("%s: Could not find the usecase (%d) in the list",
1699               __func__, USECASE_VOICE_CALL);
1700         return -EINVAL;
1701     }
1702 
1703     disable_snd_device(adev, uc_info, uc_info->out_snd_device, false);
1704     disable_snd_device(adev, uc_info, uc_info->in_snd_device, true);
1705 
1706     uc_release_pcm_devices(uc_info);
1707     list_remove(&uc_info->adev_list_node);
1708     free(uc_info);
1709 
1710     ALOGV("%s: exit", __func__);
1711     return 0;
1712 }
1713 
1714 /* always called with adev lock held */
start_voice_call(struct audio_device * adev)1715 static int start_voice_call(struct audio_device *adev)
1716 {
1717     struct audio_usecase *uc_info;
1718 
1719     ALOGV("%s: enter", __func__);
1720 
1721     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1722     uc_info->id = USECASE_VOICE_CALL;
1723     uc_info->type = VOICE_CALL;
1724     uc_info->stream = (struct audio_stream *)adev->primary_output;
1725     uc_info->devices = adev->primary_output->devices;
1726     uc_info->in_snd_device = SND_DEVICE_NONE;
1727     uc_info->out_snd_device = SND_DEVICE_NONE;
1728 
1729     uc_select_pcm_devices(uc_info);
1730 
1731     list_add_tail(&adev->usecase_list, &uc_info->adev_list_node);
1732 
1733     select_devices(adev, USECASE_VOICE_CALL);
1734 
1735 
1736     /* TODO: implement voice call start */
1737 
1738     /* set cached volume */
1739     set_voice_volume_l(adev, adev->voice_volume);
1740 
1741     adev->in_call = true;
1742     ALOGV("%s: exit", __func__);
1743     return 0;
1744 }
1745 
check_input_parameters(uint32_t sample_rate,audio_format_t format,int channel_count)1746 static int check_input_parameters(uint32_t sample_rate,
1747                                   audio_format_t format,
1748                                   int channel_count)
1749 {
1750     if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1751 
1752     if ((channel_count < 1) || (channel_count > 4)) return -EINVAL;
1753 
1754     switch (sample_rate) {
1755     case 8000:
1756     case 11025:
1757     case 12000:
1758     case 16000:
1759     case 22050:
1760     case 24000:
1761     case 32000:
1762     case 44100:
1763     case 48000:
1764         break;
1765     default:
1766         return -EINVAL;
1767     }
1768 
1769     return 0;
1770 }
1771 
get_input_buffer_size(uint32_t sample_rate,audio_format_t format,int channel_count,usecase_type_t usecase_type,audio_devices_t devices)1772 static size_t get_input_buffer_size(uint32_t sample_rate,
1773                                     audio_format_t format,
1774                                     int channel_count,
1775                                     usecase_type_t usecase_type,
1776                                     audio_devices_t devices)
1777 {
1778     size_t size = 0;
1779     struct pcm_device_profile *pcm_profile;
1780 
1781     if (check_input_parameters(sample_rate, format, channel_count) != 0)
1782         return 0;
1783 
1784     pcm_profile = get_pcm_device(usecase_type, devices);
1785     if (pcm_profile == NULL)
1786         return 0;
1787 
1788     /*
1789      * take resampling into account and return the closest majoring
1790      * multiple of 16 frames, as audioflinger expects audio buffers to
1791      * be a multiple of 16 frames
1792      */
1793     size = (pcm_profile->config.period_size * sample_rate) / pcm_profile->config.rate;
1794     size = ((size + 15) / 16) * 16;
1795 
1796     return (size * channel_count * audio_bytes_per_sample(format));
1797 
1798 }
1799 
out_get_sample_rate(const struct audio_stream * stream)1800 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1801 {
1802     struct stream_out *out = (struct stream_out *)stream;
1803 
1804     return out->sample_rate;
1805 }
1806 
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)1807 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1808 {
1809     (void)stream;
1810     (void)rate;
1811     return -ENOSYS;
1812 }
1813 
out_get_buffer_size(const struct audio_stream * stream)1814 static size_t out_get_buffer_size(const struct audio_stream *stream)
1815 {
1816     struct stream_out *out = (struct stream_out *)stream;
1817 
1818     return out->config.period_size *
1819                audio_stream_out_frame_size((const struct audio_stream_out *)stream);
1820 }
1821 
out_get_channels(const struct audio_stream * stream)1822 static uint32_t out_get_channels(const struct audio_stream *stream)
1823 {
1824     struct stream_out *out = (struct stream_out *)stream;
1825 
1826     return out->channel_mask;
1827 }
1828 
out_get_format(const struct audio_stream * stream)1829 static audio_format_t out_get_format(const struct audio_stream *stream)
1830 {
1831     struct stream_out *out = (struct stream_out *)stream;
1832 
1833     return out->format;
1834 }
1835 
out_set_format(struct audio_stream * stream,audio_format_t format)1836 static int out_set_format(struct audio_stream *stream, audio_format_t format)
1837 {
1838     (void)stream;
1839     (void)format;
1840     return -ENOSYS;
1841 }
1842 
do_out_standby_l(struct stream_out * out)1843 static int do_out_standby_l(struct stream_out *out)
1844 {
1845     struct audio_device *adev = out->dev;
1846     int status = 0;
1847 
1848     out->standby = true;
1849     out_close_pcm_devices(out);
1850     status = stop_output_stream(out);
1851 
1852     return status;
1853 }
1854 
out_standby(struct audio_stream * stream)1855 static int out_standby(struct audio_stream *stream)
1856 {
1857     struct stream_out *out = (struct stream_out *)stream;
1858     struct audio_device *adev = out->dev;
1859 
1860     ALOGV("%s: enter: usecase(%d: %s)", __func__,
1861           out->usecase, use_case_table[out->usecase]);
1862     lock_output_stream(out);
1863     if (!out->standby) {
1864         pthread_mutex_lock(&adev->lock);
1865         do_out_standby_l(out);
1866         pthread_mutex_unlock(&adev->lock);
1867     }
1868     pthread_mutex_unlock(&out->lock);
1869     ALOGV("%s: exit", __func__);
1870     return 0;
1871 }
1872 
out_dump(const struct audio_stream * stream,int fd)1873 static int out_dump(const struct audio_stream *stream, int fd)
1874 {
1875     (void)stream;
1876     (void)fd;
1877 
1878     return 0;
1879 }
1880 
out_set_parameters(struct audio_stream * stream,const char * kvpairs)1881 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1882 {
1883     struct stream_out *out = (struct stream_out *)stream;
1884     struct audio_device *adev = out->dev;
1885     struct audio_usecase *usecase;
1886     struct listnode *node;
1887     struct str_parms *parms;
1888     char value[32];
1889     int ret, val = 0;
1890     bool devices_changed;
1891     struct pcm_device *pcm_device;
1892     struct pcm_device_profile *pcm_profile;
1893 #ifdef PREPROCESSING_ENABLED
1894     struct stream_in *in = NULL;    /* if non-NULL, then force input to standby */
1895 #endif
1896 
1897     ALOGV("%s: enter: usecase(%d: %s) kvpairs: %s out->devices(%d) adev->mode(%d)",
1898           __func__, out->usecase, use_case_table[out->usecase], kvpairs, out->devices, adev->mode);
1899     parms = str_parms_create_str(kvpairs);
1900     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1901     if (ret >= 0) {
1902         val = atoi(value);
1903         pthread_mutex_lock(&adev->lock_inputs);
1904         lock_output_stream(out);
1905         pthread_mutex_lock(&adev->lock);
1906 #ifdef PREPROCESSING_ENABLED
1907         if (((int)out->devices != val) && (val != 0) && (!out->standby) &&
1908             (out->usecase == USECASE_AUDIO_PLAYBACK)) {
1909             /* reset active input:
1910              *  - to attach the echo reference
1911              *  - because a change in output device may change mic settings */
1912             if (adev->active_input && (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
1913                     adev->active_input->source == AUDIO_SOURCE_MIC)) {
1914                 in = adev->active_input;
1915             }
1916         }
1917 #endif
1918         if (val != 0) {
1919             devices_changed = out->devices != (audio_devices_t)val;
1920             out->devices = val;
1921 
1922             if (!out->standby) {
1923                 if (devices_changed)
1924                     do_out_standby_l(out);
1925                 else
1926                     select_devices(adev, out->usecase);
1927             }
1928 
1929             if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1930                     (out == adev->primary_output)) {
1931                 start_voice_call(adev);
1932             } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1933                        (out == adev->primary_output)) {
1934                 select_devices(adev, USECASE_VOICE_CALL);
1935             }
1936         }
1937 
1938         if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call &&
1939                 (out == adev->primary_output)) {
1940             stop_voice_call(adev);
1941         }
1942         pthread_mutex_unlock(&adev->lock);
1943         pthread_mutex_unlock(&out->lock);
1944 #ifdef PREPROCESSING_ENABLED
1945         if (in) {
1946             /* The lock on adev->lock_inputs prevents input stream from being closed */
1947             lock_input_stream(in);
1948             pthread_mutex_lock(&adev->lock);
1949             LOG_ALWAYS_FATAL_IF(in != adev->active_input);
1950             do_in_standby_l(in);
1951             pthread_mutex_unlock(&adev->lock);
1952             pthread_mutex_unlock(&in->lock);
1953         }
1954 #endif
1955         pthread_mutex_unlock(&adev->lock_inputs);
1956     }
1957 
1958     str_parms_destroy(parms);
1959     ALOGV("%s: exit: code(%d)", __func__, ret);
1960     return ret;
1961 }
1962 
out_get_parameters(const struct audio_stream * stream,const char * keys)1963 static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1964 {
1965     struct stream_out *out = (struct stream_out *)stream;
1966     struct str_parms *query = str_parms_create_str(keys);
1967     char *str;
1968     char value[256];
1969     struct str_parms *reply = str_parms_create();
1970     size_t i, j;
1971     int ret;
1972     bool first = true;
1973     ALOGV("%s: enter: keys - %s", __func__, keys);
1974     ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1975     if (ret >= 0) {
1976         value[0] = '\0';
1977         i = 0;
1978         while (out->supported_channel_masks[i] != 0) {
1979             for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1980                 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1981                     if (!first) {
1982                         strcat(value, "|");
1983                     }
1984                     strcat(value, out_channels_name_to_enum_table[j].name);
1985                     first = false;
1986                     break;
1987                 }
1988             }
1989             i++;
1990         }
1991         str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1992         str = str_parms_to_str(reply);
1993     } else {
1994         str = strdup(keys);
1995     }
1996     str_parms_destroy(query);
1997     str_parms_destroy(reply);
1998     ALOGV("%s: exit: returns - %s", __func__, str);
1999     return str;
2000 }
2001 
out_get_latency(const struct audio_stream_out * stream)2002 static uint32_t out_get_latency(const struct audio_stream_out *stream)
2003 {
2004     struct stream_out *out = (struct stream_out *)stream;
2005 
2006     return (out->config.period_count * out->config.period_size * 1000) /
2007            (out->config.rate);
2008 }
2009 
out_set_volume(struct audio_stream_out * stream,float left,float right)2010 static int out_set_volume(struct audio_stream_out *stream, float left,
2011                           float right)
2012 {
2013     struct stream_out *out = (struct stream_out *)stream;
2014     struct audio_device *adev = out->dev;
2015     (void)right;
2016 
2017     if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
2018         /* only take left channel into account: the API is for stereo anyway */
2019         out->muted = (left == 0.0f);
2020         return 0;
2021     }
2022 
2023     return -ENOSYS;
2024 }
2025 
2026 /* Applies the DSP to the samples for the iodev if applicable. */
apply_dsp(struct pcm_device * iodev,uint8_t * buf,size_t frames)2027 static void apply_dsp(struct pcm_device *iodev, uint8_t *buf, size_t frames)
2028 {
2029 	struct cras_dsp_context *ctx;
2030 	struct pipeline *pipeline;
2031 
2032 	ctx = iodev->dsp_context;
2033 	if (!ctx)
2034 		return;
2035 
2036 	pipeline = cras_dsp_get_pipeline(ctx);
2037 	if (!pipeline)
2038 		return;
2039 
2040 	cras_dsp_pipeline_apply(pipeline,
2041 				buf,
2042 				frames);
2043 
2044 	cras_dsp_put_pipeline(ctx);
2045 }
2046 
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)2047 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
2048                          size_t bytes)
2049 {
2050     struct stream_out *out = (struct stream_out *)stream;
2051     struct audio_device *adev = out->dev;
2052     ssize_t ret = 0;
2053     struct pcm_device *pcm_device;
2054     struct listnode *node;
2055     size_t frame_size = audio_stream_out_frame_size(stream);
2056     size_t frames_wr = 0, frames_rq = 0;
2057     unsigned char *data = NULL;
2058     struct pcm_config config;
2059 #ifdef PREPROCESSING_ENABLED
2060     size_t in_frames = bytes / frame_size;
2061     size_t out_frames = in_frames;
2062     struct stream_in *in = NULL;
2063 #endif
2064 
2065     lock_output_stream(out);
2066     if (out->standby) {
2067 #ifdef PREPROCESSING_ENABLED
2068         pthread_mutex_unlock(&out->lock);
2069         /* Prevent input stream from being closed */
2070         pthread_mutex_lock(&adev->lock_inputs);
2071         lock_output_stream(out);
2072         if (!out->standby) {
2073             pthread_mutex_unlock(&adev->lock_inputs);
2074             goto false_alarm;
2075         }
2076 #endif
2077         pthread_mutex_lock(&adev->lock);
2078         ret = start_output_stream(out);
2079         if (ret != 0) {
2080             pthread_mutex_unlock(&adev->lock);
2081 #ifdef PREPROCESSING_ENABLED
2082             pthread_mutex_unlock(&adev->lock_inputs);
2083 #endif
2084             goto exit;
2085         }
2086         out->standby = false;
2087 
2088 #ifdef PREPROCESSING_ENABLED
2089         /* A change in output device may change the microphone selection */
2090         if (adev->active_input &&
2091             (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
2092                 adev->active_input->source == AUDIO_SOURCE_MIC)) {
2093                     in = adev->active_input;
2094                     ALOGV("%s: enter: force_input_standby true", __func__);
2095         }
2096 #endif
2097         pthread_mutex_unlock(&adev->lock);
2098 #ifdef PREPROCESSING_ENABLED
2099         if (!in) {
2100             /* Leave mutex locked iff in != NULL */
2101             pthread_mutex_unlock(&adev->lock_inputs);
2102         }
2103 #endif
2104     }
2105 false_alarm:
2106 
2107     if (out->muted)
2108         memset((void *)buffer, 0, bytes);
2109     list_for_each(node, &out->pcm_dev_list) {
2110         pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
2111         if (pcm_device->resampler) {
2112             if (bytes * pcm_device->pcm_profile->config.rate / out->sample_rate + frame_size
2113                     > pcm_device->res_byte_count) {
2114                 pcm_device->res_byte_count =
2115                     bytes * pcm_device->pcm_profile->config.rate / out->sample_rate + frame_size;
2116                 pcm_device->res_buffer =
2117                     realloc(pcm_device->res_buffer, pcm_device->res_byte_count);
2118                 ALOGV("%s: resampler res_byte_count = %zu", __func__,
2119                     pcm_device->res_byte_count);
2120             }
2121             frames_rq = bytes / frame_size;
2122             frames_wr = pcm_device->res_byte_count / frame_size;
2123             ALOGVV("%s: resampler request frames = %zu frame_size = %zu",
2124                 __func__, frames_rq, frame_size);
2125             pcm_device->resampler->resample_from_input(pcm_device->resampler,
2126                 (int16_t *)buffer, &frames_rq, (int16_t *)pcm_device->res_buffer, &frames_wr);
2127             ALOGVV("%s: resampler output frames_= %zu", __func__, frames_wr);
2128         }
2129         if (pcm_device->pcm) {
2130             size_t src_channels = audio_channel_count_from_out_mask(out->channel_mask);
2131             size_t dst_channels = pcm_device->pcm_profile->config.channels;
2132             bool channel_remapping_needed = (dst_channels != src_channels);
2133             unsigned audio_bytes;
2134             const void *audio_data;
2135 
2136             ALOGVV("%s: writing buffer (%zd bytes) to pcm device", __func__, bytes);
2137             if (pcm_device->resampler && pcm_device->res_buffer) {
2138                 audio_data = pcm_device->res_buffer;
2139                 audio_bytes = frames_wr * frame_size;
2140             } else {
2141                 audio_data = buffer;
2142                 audio_bytes = bytes;
2143             }
2144 
2145             /*
2146              * This can only be S16_LE stereo because of the supported formats,
2147              * 4 bytes per frame.
2148              */
2149             apply_dsp(pcm_device, audio_data, audio_bytes/4);
2150 
2151             if (channel_remapping_needed) {
2152                 const void *remapped_audio_data;
2153                 size_t dest_buffer_size = audio_bytes * dst_channels / src_channels;
2154                 size_t new_size;
2155                 size_t bytes_per_sample = audio_bytes_per_sample(stream->common.get_format(&stream->common));
2156 
2157                 /* With additional channels, we cannot use original buffer */
2158                 if (out->proc_buf_size < dest_buffer_size) {
2159                     out->proc_buf_size = dest_buffer_size;
2160                     out->proc_buf_out = realloc(out->proc_buf_out, dest_buffer_size);
2161                     ALOG_ASSERT((out->proc_buf_out != NULL),
2162                                 "out_write() failed to reallocate proc_buf_out");
2163                 }
2164                 new_size = adjust_channels(audio_data, src_channels, out->proc_buf_out, dst_channels,
2165                     bytes_per_sample, audio_bytes);
2166                 ALOG_ASSERT(new_size == dest_buffer_size);
2167                 audio_data = out->proc_buf_out;
2168                 audio_bytes = dest_buffer_size;
2169             }
2170 
2171             pcm_device->status = pcm_write(pcm_device->pcm, audio_data, audio_bytes);
2172             if (pcm_device->status != 0)
2173                 ret = pcm_device->status;
2174         }
2175     }
2176     if (ret == 0)
2177         out->written += bytes / frame_size;
2178 
2179 exit:
2180     pthread_mutex_unlock(&out->lock);
2181 
2182     if (ret != 0) {
2183         list_for_each(node, &out->pcm_dev_list) {
2184             pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
2185             if (pcm_device->pcm && pcm_device->status != 0)
2186                 ALOGE("%s: error %zd - %s", __func__, ret, pcm_get_error(pcm_device->pcm));
2187         }
2188         out_standby(&out->stream.common);
2189         usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
2190                out_get_sample_rate(&out->stream.common));
2191     }
2192 
2193 #ifdef PREPROCESSING_ENABLED
2194     if (in) {
2195         /* The lock on adev->lock_inputs prevents input stream from being closed */
2196         lock_input_stream(in);
2197         pthread_mutex_lock(&adev->lock);
2198         LOG_ALWAYS_FATAL_IF(in != adev->active_input);
2199         do_in_standby_l(in);
2200         pthread_mutex_unlock(&adev->lock);
2201         pthread_mutex_unlock(&in->lock);
2202         /* This mutex was left locked iff in != NULL */
2203         pthread_mutex_unlock(&adev->lock_inputs);
2204     }
2205 #endif
2206 
2207     return bytes;
2208 }
2209 
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)2210 static int out_get_render_position(const struct audio_stream_out *stream,
2211                                    uint32_t *dsp_frames)
2212 {
2213     (void)stream;
2214     *dsp_frames = 0;
2215     return -EINVAL;
2216 }
2217 
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)2218 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
2219 {
2220     (void)stream;
2221     (void)effect;
2222     return 0;
2223 }
2224 
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)2225 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
2226 {
2227     (void)stream;
2228     (void)effect;
2229     return 0;
2230 }
2231 
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)2232 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
2233                                         int64_t *timestamp)
2234 {
2235     (void)stream;
2236     (void)timestamp;
2237     return -EINVAL;
2238 }
2239 
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)2240 static int out_get_presentation_position(const struct audio_stream_out *stream,
2241                                    uint64_t *frames, struct timespec *timestamp)
2242 {
2243     struct stream_out *out = (struct stream_out *)stream;
2244     int ret = -1;
2245     unsigned long dsp_frames;
2246 
2247     lock_output_stream(out);
2248 
2249     /* FIXME: which device to read from? */
2250     if (!list_empty(&out->pcm_dev_list)) {
2251         unsigned int avail;
2252         struct pcm_device *pcm_device = node_to_item(list_head(&out->pcm_dev_list),
2253                                                struct pcm_device, stream_list_node);
2254 
2255         if (pcm_get_htimestamp(pcm_device->pcm, &avail, timestamp) == 0) {
2256             size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
2257             int64_t signed_frames = out->written - kernel_buffer_size + avail;
2258             /* This adjustment accounts for buffering after app processor.
2259                It is based on estimated DSP latency per use case, rather than exact. */
2260             signed_frames -=
2261                 (render_latency(out->usecase) * out->sample_rate / 1000000LL);
2262 
2263             /* It would be unusual for this value to be negative, but check just in case ... */
2264             if (signed_frames >= 0) {
2265                 *frames = signed_frames;
2266                 ret = 0;
2267             }
2268         }
2269     }
2270 
2271     pthread_mutex_unlock(&out->lock);
2272 
2273     return ret;
2274 }
2275 
2276 /** audio_stream_in implementation **/
in_get_sample_rate(const struct audio_stream * stream)2277 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2278 {
2279     struct stream_in *in = (struct stream_in *)stream;
2280 
2281     return in->requested_rate;
2282 }
2283 
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)2284 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
2285 {
2286     (void)stream;
2287     (void)rate;
2288     return -ENOSYS;
2289 }
2290 
in_get_channels(const struct audio_stream * stream)2291 static uint32_t in_get_channels(const struct audio_stream *stream)
2292 {
2293     struct stream_in *in = (struct stream_in *)stream;
2294 
2295     return in->main_channels;
2296 }
2297 
in_get_format(const struct audio_stream * stream)2298 static audio_format_t in_get_format(const struct audio_stream *stream)
2299 {
2300     (void)stream;
2301     return AUDIO_FORMAT_PCM_16_BIT;
2302 }
2303 
in_set_format(struct audio_stream * stream,audio_format_t format)2304 static int in_set_format(struct audio_stream *stream, audio_format_t format)
2305 {
2306     (void)stream;
2307     (void)format;
2308 
2309     return -ENOSYS;
2310 }
2311 
in_get_buffer_size(const struct audio_stream * stream)2312 static size_t in_get_buffer_size(const struct audio_stream *stream)
2313 {
2314     struct stream_in *in = (struct stream_in *)stream;
2315 
2316     return get_input_buffer_size(in->requested_rate,
2317                                  in_get_format(stream),
2318                                  audio_channel_count_from_in_mask(in->main_channels),
2319                                  in->usecase_type,
2320                                  in->devices);
2321 }
2322 
in_close_pcm_devices(struct stream_in * in)2323 static int in_close_pcm_devices(struct stream_in *in)
2324 {
2325     struct pcm_device *pcm_device;
2326     struct listnode *node;
2327     struct audio_device *adev = in->dev;
2328 
2329     list_for_each(node, &in->pcm_dev_list) {
2330         pcm_device = node_to_item(node, struct pcm_device, stream_list_node);
2331         if (pcm_device) {
2332             if (pcm_device->pcm)
2333                 pcm_close(pcm_device->pcm);
2334             pcm_device->pcm = NULL;
2335             if (pcm_device->sound_trigger_handle > 0)
2336                 adev->sound_trigger_close_for_streaming(
2337                         pcm_device->sound_trigger_handle);
2338             pcm_device->sound_trigger_handle = 0;
2339         }
2340     }
2341     return 0;
2342 }
2343 
2344 
2345 /* must be called with stream and hw device mutex locked */
do_in_standby_l(struct stream_in * in)2346 static int do_in_standby_l(struct stream_in *in)
2347 {
2348     int status = 0;
2349 
2350     if (!in->standby) {
2351 
2352         in_close_pcm_devices(in);
2353 
2354         status = stop_input_stream(in);
2355 
2356         if (in->read_buf) {
2357             free(in->read_buf);
2358             in->read_buf = NULL;
2359         }
2360 
2361         in->standby = 1;
2362     }
2363     return 0;
2364 }
2365 
2366 // called with adev->lock_inputs locked
in_standby_l(struct stream_in * in)2367 static int in_standby_l(struct stream_in *in)
2368 {
2369     struct audio_device *adev = in->dev;
2370     int status = 0;
2371     lock_input_stream(in);
2372     if (!in->standby) {
2373         pthread_mutex_lock(&adev->lock);
2374         status = do_in_standby_l(in);
2375         pthread_mutex_unlock(&adev->lock);
2376     }
2377     pthread_mutex_unlock(&in->lock);
2378     return status;
2379 }
2380 
in_standby(struct audio_stream * stream)2381 static int in_standby(struct audio_stream *stream)
2382 {
2383     struct stream_in *in = (struct stream_in *)stream;
2384     struct audio_device *adev = in->dev;
2385     int status;
2386     ALOGV("%s: enter", __func__);
2387     pthread_mutex_lock(&adev->lock_inputs);
2388     status = in_standby_l(in);
2389     pthread_mutex_unlock(&adev->lock_inputs);
2390     ALOGV("%s: exit:  status(%d)", __func__, status);
2391     return status;
2392 }
2393 
in_dump(const struct audio_stream * stream,int fd)2394 static int in_dump(const struct audio_stream *stream, int fd)
2395 {
2396     (void)stream;
2397     (void)fd;
2398 
2399     return 0;
2400 }
2401 
in_set_parameters(struct audio_stream * stream,const char * kvpairs)2402 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2403 {
2404     struct stream_in *in = (struct stream_in *)stream;
2405     struct audio_device *adev = in->dev;
2406     struct str_parms *parms;
2407     char *str;
2408     char value[32];
2409     int ret, val = 0;
2410     struct audio_usecase *uc_info;
2411     bool do_standby = false;
2412     struct listnode *node;
2413     struct pcm_device *pcm_device;
2414     struct pcm_device_profile *pcm_profile;
2415 
2416     ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
2417     parms = str_parms_create_str(kvpairs);
2418 
2419     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2420 
2421     pthread_mutex_lock(&adev->lock_inputs);
2422     lock_input_stream(in);
2423     pthread_mutex_lock(&adev->lock);
2424     if (ret >= 0) {
2425         val = atoi(value);
2426         /* no audio source uses val == 0 */
2427         if (((int)in->source != val) && (val != 0)) {
2428             in->source = val;
2429         }
2430     }
2431 
2432     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2433     if (ret >= 0) {
2434         val = atoi(value);
2435         if (((int)in->devices != val) && (val != 0)) {
2436             in->devices = val;
2437             /* If recording is in progress, change the tx device to new device */
2438             if (!in->standby) {
2439                 uc_info = get_usecase_from_id(adev, in->usecase);
2440                 if (uc_info == NULL) {
2441                     ALOGE("%s: Could not find the usecase (%d) in the list",
2442                           __func__, in->usecase);
2443                 } else {
2444                     if (list_empty(&in->pcm_dev_list))
2445                         ALOGE("%s: pcm device list empty", __func__);
2446                     else {
2447                         pcm_device = node_to_item(list_head(&in->pcm_dev_list),
2448                                                   struct pcm_device, stream_list_node);
2449                         if ((pcm_device->pcm_profile->devices & val & ~AUDIO_DEVICE_BIT_IN) == 0) {
2450                             do_standby = true;
2451                         }
2452                     }
2453                 }
2454                 if (do_standby) {
2455                     ret = do_in_standby_l(in);
2456                 } else
2457                     ret = select_devices(adev, in->usecase);
2458             }
2459         }
2460     }
2461     pthread_mutex_unlock(&adev->lock);
2462     pthread_mutex_unlock(&in->lock);
2463     pthread_mutex_unlock(&adev->lock_inputs);
2464     str_parms_destroy(parms);
2465 
2466     if (ret > 0)
2467         ret = 0;
2468 
2469     return ret;
2470 }
2471 
in_get_parameters(const struct audio_stream * stream,const char * keys)2472 static char* in_get_parameters(const struct audio_stream *stream,
2473                                const char *keys)
2474 {
2475     (void)stream;
2476     (void)keys;
2477 
2478     return strdup("");
2479 }
2480 
in_set_gain(struct audio_stream_in * stream,float gain)2481 static int in_set_gain(struct audio_stream_in *stream, float gain)
2482 {
2483     (void)stream;
2484     (void)gain;
2485 
2486     return 0;
2487 }
2488 
read_bytes_from_dsp(struct stream_in * in,void * buffer,size_t bytes)2489 static ssize_t read_bytes_from_dsp(struct stream_in *in, void* buffer,
2490                                    size_t bytes)
2491 {
2492     struct pcm_device *pcm_device;
2493     struct audio_device *adev = in->dev;
2494 
2495     pcm_device = node_to_item(list_head(&in->pcm_dev_list),
2496                               struct pcm_device, stream_list_node);
2497 
2498     if (pcm_device->sound_trigger_handle > 0)
2499         return adev->sound_trigger_read_samples(
2500                 pcm_device->sound_trigger_handle, buffer, bytes);
2501     else
2502         return 0;
2503 }
2504 
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)2505 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2506                        size_t bytes)
2507 {
2508     struct stream_in *in = (struct stream_in *)stream;
2509     struct audio_device *adev = in->dev;
2510     ssize_t frames = -1;
2511     int ret = -1;
2512     int read_and_process_successful = false;
2513 
2514     size_t frames_rq = bytes / audio_stream_in_frame_size(stream);
2515 
2516     /* no need to acquire adev->lock_inputs because API contract prevents a close */
2517     lock_input_stream(in);
2518     if (in->standby) {
2519         pthread_mutex_unlock(&in->lock);
2520         pthread_mutex_lock(&adev->lock_inputs);
2521         lock_input_stream(in);
2522         if (!in->standby) {
2523             pthread_mutex_unlock(&adev->lock_inputs);
2524             goto false_alarm;
2525         }
2526         pthread_mutex_lock(&adev->lock);
2527         ret = start_input_stream(in);
2528         pthread_mutex_unlock(&adev->lock);
2529         pthread_mutex_unlock(&adev->lock_inputs);
2530         if (ret != 0) {
2531             goto exit;
2532         }
2533         in->standby = 0;
2534     }
2535 false_alarm:
2536 
2537     if (!list_empty(&in->pcm_dev_list)) {
2538         if (in->usecase == USECASE_AUDIO_CAPTURE_HOTWORD) {
2539             bytes = read_bytes_from_dsp(in, buffer, bytes);
2540             if (bytes > 0)
2541                 read_and_process_successful = true;
2542         } else {
2543             /*
2544              * Read PCM and:
2545              * - resample if needed
2546              * - process if pre-processors are attached
2547              * - discard unwanted channels
2548              */
2549             frames = read_and_process_frames(stream, buffer, frames_rq);
2550             if (frames >= 0)
2551                 read_and_process_successful = true;
2552         }
2553     }
2554 
2555     /*
2556      * Instead of writing zeroes here, we could trust the hardware
2557      * to always provide zeroes when muted.
2558      */
2559     if (read_and_process_successful == true && adev->mic_mute)
2560         memset(buffer, 0, bytes);
2561 
2562 exit:
2563     pthread_mutex_unlock(&in->lock);
2564 
2565     if (read_and_process_successful == false) {
2566         in_standby(&in->stream.common);
2567         ALOGV("%s: read failed - sleeping for buffer duration", __func__);
2568         usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
2569                in->requested_rate);
2570     }
2571     return bytes;
2572 }
2573 
in_get_input_frames_lost(struct audio_stream_in * stream)2574 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
2575 {
2576     (void)stream;
2577 
2578     return 0;
2579 }
2580 
add_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect,bool enable)2581 static int add_remove_audio_effect(const struct audio_stream *stream,
2582                                    effect_handle_t effect,
2583                                    bool enable)
2584 {
2585     struct stream_in *in = (struct stream_in *)stream;
2586     struct audio_device *adev = in->dev;
2587     int status = 0;
2588     effect_descriptor_t desc;
2589 #ifdef PREPROCESSING_ENABLED
2590     int i;
2591 #endif
2592     status = (*effect)->get_descriptor(effect, &desc);
2593     if (status != 0)
2594         return status;
2595 
2596     ALOGI("add_remove_audio_effect(), effect type: %08x, enable: %d ", desc.type.timeLow, enable);
2597 
2598     pthread_mutex_lock(&adev->lock_inputs);
2599     lock_input_stream(in);
2600     pthread_mutex_lock(&in->dev->lock);
2601 #ifndef PREPROCESSING_ENABLED
2602     if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2603             in->enable_aec != enable &&
2604             (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2605         in->enable_aec = enable;
2606         if (!in->standby)
2607             select_devices(in->dev, in->usecase);
2608     }
2609 #else
2610     if (enable) {
2611         if (in->num_preprocessors >= MAX_PREPROCESSORS) {
2612             status = -ENOSYS;
2613             goto exit;
2614         }
2615         in->preprocessors[in->num_preprocessors].effect_itfe = effect;
2616         in->num_preprocessors ++;
2617         /* check compatibility between main channel supported and possible auxiliary channels */
2618         in_update_aux_channels(in, effect);//wesley crash
2619         in->aux_channels_changed = true;
2620     } else {
2621         /* if ( enable == false ) */
2622         if (in->num_preprocessors <= 0) {
2623             status = -ENOSYS;
2624             goto exit;
2625         }
2626         status = -EINVAL;
2627         for (i = 0; i < in->num_preprocessors && status != 0; i++) {
2628             if ( in->preprocessors[i].effect_itfe == effect ) {
2629                 ALOGV("add_remove_audio_effect found fx at index %d", i);
2630                 free(in->preprocessors[i].channel_configs);
2631                 in->num_preprocessors--;
2632                 memcpy(in->preprocessors + i,
2633                        in->preprocessors + i + 1,
2634                        (in->num_preprocessors - i) * sizeof(in->preprocessors[0]));
2635                 memset(in->preprocessors + in->num_preprocessors,
2636                        0,
2637                        sizeof(in->preprocessors[0]));
2638                 status = 0;
2639             }
2640         }
2641         if (status != 0)
2642             goto exit;
2643         in->aux_channels_changed = false;
2644         ALOGV("%s: enable(%d), in->aux_channels_changed(%d)",
2645               __func__, enable, in->aux_channels_changed);
2646     }
2647     ALOGI("%s:  num_preprocessors = %d", __func__, in->num_preprocessors);
2648 
2649 exit:
2650 #endif
2651     ALOGW_IF(status != 0, "add_remove_audio_effect() error %d", status);
2652     pthread_mutex_unlock(&in->dev->lock);
2653     pthread_mutex_unlock(&in->lock);
2654     pthread_mutex_unlock(&adev->lock_inputs);
2655     return status;
2656 }
2657 
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)2658 static int in_add_audio_effect(const struct audio_stream *stream,
2659                                effect_handle_t effect)
2660 {
2661     ALOGV("%s: effect %p", __func__, effect);
2662     return add_remove_audio_effect(stream, effect, true /* enabled */);
2663 }
2664 
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)2665 static int in_remove_audio_effect(const struct audio_stream *stream,
2666                                   effect_handle_t effect)
2667 {
2668     ALOGV("%s: effect %p", __func__, effect);
2669     return add_remove_audio_effect(stream, effect, false /* disabled */);
2670 }
2671 
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)2672 static int adev_open_output_stream(struct audio_hw_device *dev,
2673                                    audio_io_handle_t handle,
2674                                    audio_devices_t devices,
2675                                    audio_output_flags_t flags,
2676                                    struct audio_config *config,
2677                                    struct audio_stream_out **stream_out,
2678                                    const char *address __unused)
2679 {
2680     struct audio_device *adev = (struct audio_device *)dev;
2681     struct stream_out *out;
2682     int i, ret;
2683     struct pcm_device_profile *pcm_profile;
2684 
2685     ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
2686           __func__, config->sample_rate, config->channel_mask, devices, flags);
2687     *stream_out = NULL;
2688     out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2689 
2690     if (devices == AUDIO_DEVICE_NONE)
2691         devices = AUDIO_DEVICE_OUT_SPEAKER;
2692 
2693     out->flags = flags;
2694     out->devices = devices;
2695     out->dev = adev;
2696     out->format = config->format;
2697     out->sample_rate = config->sample_rate;
2698     out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2699     out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
2700     out->handle = handle;
2701 
2702     pcm_profile = get_pcm_device(PCM_PLAYBACK, devices);
2703     if (pcm_profile == NULL) {
2704         ret = -EINVAL;
2705         goto error_open;
2706     }
2707     out->config = pcm_profile->config;
2708 
2709     /* Init use case and pcm_config */
2710     if (out->flags & (AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) {
2711         out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2712         out->config = pcm_config_deep_buffer;
2713         out->sample_rate = out->config.rate;
2714         ALOGV("%s: use AUDIO_PLAYBACK_DEEP_BUFFER",__func__);
2715     } else {
2716         out->usecase = USECASE_AUDIO_PLAYBACK;
2717         out->sample_rate = out->config.rate;
2718     }
2719 
2720     if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2721         if (adev->primary_output == NULL)
2722             adev->primary_output = out;
2723         else {
2724             ALOGE("%s: Primary output is already opened", __func__);
2725             ret = -EEXIST;
2726             goto error_open;
2727         }
2728     }
2729 
2730     /* Check if this usecase is already existing */
2731     pthread_mutex_lock(&adev->lock);
2732     if (get_usecase_from_id(adev, out->usecase) != NULL) {
2733         ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
2734         pthread_mutex_unlock(&adev->lock);
2735         ret = -EEXIST;
2736         goto error_open;
2737     }
2738     pthread_mutex_unlock(&adev->lock);
2739 
2740     out->stream.common.get_sample_rate = out_get_sample_rate;
2741     out->stream.common.set_sample_rate = out_set_sample_rate;
2742     out->stream.common.get_buffer_size = out_get_buffer_size;
2743     out->stream.common.get_channels = out_get_channels;
2744     out->stream.common.get_format = out_get_format;
2745     out->stream.common.set_format = out_set_format;
2746     out->stream.common.standby = out_standby;
2747     out->stream.common.dump = out_dump;
2748     out->stream.common.set_parameters = out_set_parameters;
2749     out->stream.common.get_parameters = out_get_parameters;
2750     out->stream.common.add_audio_effect = out_add_audio_effect;
2751     out->stream.common.remove_audio_effect = out_remove_audio_effect;
2752     out->stream.get_latency = out_get_latency;
2753     out->stream.set_volume = out_set_volume;
2754     out->stream.write = out_write;
2755     out->stream.get_render_position = out_get_render_position;
2756     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
2757     out->stream.get_presentation_position = out_get_presentation_position;
2758 
2759     out->standby = 1;
2760     /* out->muted = false; by calloc() */
2761     /* out->written = 0; by calloc() */
2762 
2763     pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2764     pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
2765     pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2766 
2767     config->format = out->stream.common.get_format(&out->stream.common);
2768     config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2769     config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2770 
2771     *stream_out = &out->stream;
2772     ALOGV("%s: exit", __func__);
2773     return 0;
2774 
2775 error_open:
2776     free(out);
2777     *stream_out = NULL;
2778     ALOGV("%s: exit: ret %d", __func__, ret);
2779     return ret;
2780 }
2781 
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)2782 static void adev_close_output_stream(struct audio_hw_device *dev,
2783                                      struct audio_stream_out *stream)
2784 {
2785     struct stream_out *out = (struct stream_out *)stream;
2786     struct audio_device *adev = out->dev;
2787     (void)dev;
2788 
2789     ALOGV("%s: enter", __func__);
2790     out_standby(&stream->common);
2791     pthread_cond_destroy(&out->cond);
2792     pthread_mutex_destroy(&out->lock);
2793     pthread_mutex_destroy(&out->pre_lock);
2794     free(out->proc_buf_out);
2795     free(stream);
2796     ALOGV("%s: exit", __func__);
2797 }
2798 
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)2799 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2800 {
2801     struct audio_device *adev = (struct audio_device *)dev;
2802     struct str_parms *parms;
2803     char *str;
2804     char value[32];
2805     int val;
2806     int ret;
2807 
2808     ALOGV("%s: enter: %s", __func__, kvpairs);
2809 
2810     parms = str_parms_create_str(kvpairs);
2811     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
2812     if (ret >= 0) {
2813         int tty_mode;
2814 
2815         if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
2816             tty_mode = TTY_MODE_OFF;
2817         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
2818             tty_mode = TTY_MODE_VCO;
2819         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
2820             tty_mode = TTY_MODE_HCO;
2821         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
2822             tty_mode = TTY_MODE_FULL;
2823         else
2824             return -EINVAL;
2825 
2826         pthread_mutex_lock(&adev->lock);
2827         if (tty_mode != adev->tty_mode) {
2828             adev->tty_mode = tty_mode;
2829             if (adev->in_call)
2830                 select_devices(adev, USECASE_VOICE_CALL);
2831         }
2832         pthread_mutex_unlock(&adev->lock);
2833     }
2834 
2835     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2836     if (ret >= 0) {
2837         /* When set to false, HAL should disable EC and NS
2838          * But it is currently not supported.
2839          */
2840         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2841             adev->bluetooth_nrec = true;
2842         else
2843             adev->bluetooth_nrec = false;
2844     }
2845 
2846     ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2847     if (ret >= 0) {
2848         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2849             adev->screen_off = false;
2850         else
2851             adev->screen_off = true;
2852     }
2853 
2854     ret = str_parms_get_int(parms, "rotation", &val);
2855     if (ret >= 0) {
2856         bool reverse_speakers = false;
2857         switch(val) {
2858         /* Assume 0deg rotation means the front camera is up with the usb port
2859          * on the lower left when the user is facing the screen. This assumption
2860          * is device-specific, not platform-specific like this code.
2861          */
2862         case 180:
2863             reverse_speakers = true;
2864             break;
2865         case 0:
2866         case 90:
2867         case 270:
2868             break;
2869         default:
2870             ALOGE("%s: unexpected rotation of %d", __func__, val);
2871         }
2872         pthread_mutex_lock(&adev->lock);
2873         if (adev->speaker_lr_swap != reverse_speakers) {
2874             adev->speaker_lr_swap = reverse_speakers;
2875             struct mixer_card *mixer_card;
2876             mixer_card = adev_get_mixer_for_card(adev, SOUND_CARD);
2877             if (mixer_card)
2878                 audio_route_apply_and_update_path(mixer_card->audio_route,
2879                         reverse_speakers ? "speaker-lr-reverse" :
2880                                            "speaker-lr-normal");
2881         }
2882         pthread_mutex_unlock(&adev->lock);
2883     }
2884 
2885     str_parms_destroy(parms);
2886     ALOGV("%s: exit with code(%d)", __func__, ret);
2887     return ret;
2888 }
2889 
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)2890 static char* adev_get_parameters(const struct audio_hw_device *dev,
2891                                  const char *keys)
2892 {
2893     (void)dev;
2894     (void)keys;
2895 
2896     return strdup("");
2897 }
2898 
adev_init_check(const struct audio_hw_device * dev)2899 static int adev_init_check(const struct audio_hw_device *dev)
2900 {
2901     (void)dev;
2902 
2903     return 0;
2904 }
2905 
adev_set_voice_volume(struct audio_hw_device * dev,float volume)2906 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2907 {
2908     int ret = 0;
2909     struct audio_device *adev = (struct audio_device *)dev;
2910     pthread_mutex_lock(&adev->lock);
2911     /* cache volume */
2912     adev->voice_volume = volume;
2913     ret = set_voice_volume_l(adev, adev->voice_volume);
2914     pthread_mutex_unlock(&adev->lock);
2915     return ret;
2916 }
2917 
adev_set_master_volume(struct audio_hw_device * dev,float volume)2918 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2919 {
2920     (void)dev;
2921     (void)volume;
2922 
2923     return -ENOSYS;
2924 }
2925 
adev_get_master_volume(struct audio_hw_device * dev,float * volume)2926 static int adev_get_master_volume(struct audio_hw_device *dev,
2927                                   float *volume)
2928 {
2929     (void)dev;
2930     (void)volume;
2931 
2932     return -ENOSYS;
2933 }
2934 
adev_set_master_mute(struct audio_hw_device * dev,bool muted)2935 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2936 {
2937     (void)dev;
2938     (void)muted;
2939 
2940     return -ENOSYS;
2941 }
2942 
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)2943 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2944 {
2945     (void)dev;
2946     (void)muted;
2947 
2948     return -ENOSYS;
2949 }
2950 
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)2951 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2952 {
2953     struct audio_device *adev = (struct audio_device *)dev;
2954 
2955     pthread_mutex_lock(&adev->lock);
2956     if (adev->mode != mode) {
2957         ALOGI("%s mode = %d", __func__, mode);
2958         adev->mode = mode;
2959     }
2960     pthread_mutex_unlock(&adev->lock);
2961     return 0;
2962 }
2963 
adev_set_mic_mute(struct audio_hw_device * dev,bool state)2964 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2965 {
2966     struct audio_device *adev = (struct audio_device *)dev;
2967     int err = 0;
2968 
2969     pthread_mutex_lock(&adev->lock);
2970     adev->mic_mute = state;
2971 
2972     if (adev->mode == AUDIO_MODE_IN_CALL) {
2973         /* TODO */
2974     }
2975 
2976     pthread_mutex_unlock(&adev->lock);
2977     return err;
2978 }
2979 
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)2980 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2981 {
2982     struct audio_device *adev = (struct audio_device *)dev;
2983 
2984     *state = adev->mic_mute;
2985 
2986     return 0;
2987 }
2988 
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)2989 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2990                                          const struct audio_config *config)
2991 {
2992     (void)dev;
2993 
2994     /* NOTE: we default to built in mic which may cause a mismatch between what we
2995      * report here and the actual buffer size
2996      */
2997     return get_input_buffer_size(config->sample_rate,
2998                                  config->format,
2999                                  audio_channel_count_from_in_mask(config->channel_mask),
3000                                  PCM_CAPTURE /* usecase_type */,
3001                                  AUDIO_DEVICE_IN_BUILTIN_MIC);
3002 }
3003 
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)3004 static int adev_open_input_stream(struct audio_hw_device *dev,
3005                                   audio_io_handle_t handle __unused,
3006                                   audio_devices_t devices,
3007                                   struct audio_config *config,
3008                                   struct audio_stream_in **stream_in,
3009                                   audio_input_flags_t flags,
3010                                   const char *address __unused,
3011                                   audio_source_t source)
3012 {
3013     struct audio_device *adev = (struct audio_device *)dev;
3014     struct stream_in *in;
3015     struct pcm_device_profile *pcm_profile;
3016 
3017     ALOGV("%s: enter", __func__);
3018 
3019     *stream_in = NULL;
3020     if (check_input_parameters(config->sample_rate, config->format,
3021                                audio_channel_count_from_in_mask(config->channel_mask)) != 0)
3022         return -EINVAL;
3023 
3024     usecase_type_t usecase_type = (source == AUDIO_SOURCE_HOTWORD) ?
3025                 PCM_HOTWORD_STREAMING : PCM_CAPTURE;
3026     pcm_profile = get_pcm_device(usecase_type, devices);
3027     if (pcm_profile == NULL)
3028         return -EINVAL;
3029 
3030     in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
3031 
3032     in->stream.common.get_sample_rate = in_get_sample_rate;
3033     in->stream.common.set_sample_rate = in_set_sample_rate;
3034     in->stream.common.get_buffer_size = in_get_buffer_size;
3035     in->stream.common.get_channels = in_get_channels;
3036     in->stream.common.get_format = in_get_format;
3037     in->stream.common.set_format = in_set_format;
3038     in->stream.common.standby = in_standby;
3039     in->stream.common.dump = in_dump;
3040     in->stream.common.set_parameters = in_set_parameters;
3041     in->stream.common.get_parameters = in_get_parameters;
3042     in->stream.common.add_audio_effect = in_add_audio_effect;
3043     in->stream.common.remove_audio_effect = in_remove_audio_effect;
3044     in->stream.set_gain = in_set_gain;
3045     in->stream.read = in_read;
3046     in->stream.get_input_frames_lost = in_get_input_frames_lost;
3047 
3048     in->devices = devices;
3049     in->source = source;
3050     in->dev = adev;
3051     in->standby = 1;
3052     in->main_channels = config->channel_mask;
3053     in->requested_rate = config->sample_rate;
3054     if (config->sample_rate != CAPTURE_DEFAULT_SAMPLING_RATE)
3055         flags = flags & ~AUDIO_INPUT_FLAG_FAST;
3056     in->input_flags = flags;
3057     /* HW codec is limited to default channels. No need to update with
3058      * requested channels */
3059     in->config = pcm_profile->config;
3060 
3061     /* Update config params with the requested sample rate and channels */
3062     if (source == AUDIO_SOURCE_HOTWORD) {
3063         in->usecase = USECASE_AUDIO_CAPTURE_HOTWORD;
3064     } else {
3065         in->usecase = USECASE_AUDIO_CAPTURE;
3066     }
3067     in->usecase_type = usecase_type;
3068 
3069     pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
3070     pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
3071 
3072     *stream_in = &in->stream;
3073     ALOGV("%s: exit", __func__);
3074     return 0;
3075 }
3076 
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * stream)3077 static void adev_close_input_stream(struct audio_hw_device *dev,
3078                                     struct audio_stream_in *stream)
3079 {
3080     struct audio_device *adev = (struct audio_device *)dev;
3081     struct stream_in *in = (struct stream_in*)stream;
3082     ALOGV("%s", __func__);
3083 
3084     /* prevent concurrent out_set_parameters, or out_write from standby */
3085     pthread_mutex_lock(&adev->lock_inputs);
3086 
3087     in_standby_l(in);
3088     pthread_mutex_destroy(&in->lock);
3089     pthread_mutex_destroy(&in->pre_lock);
3090     free(in->proc_buf_out);
3091 
3092 #ifdef PREPROCESSING_ENABLED
3093     int i;
3094 
3095     for (i=0; i<in->num_preprocessors; i++) {
3096         free(in->preprocessors[i].channel_configs);
3097     }
3098 
3099     if (in->read_buf) {
3100         free(in->read_buf);
3101     }
3102 
3103     if (in->proc_buf_in) {
3104         free(in->proc_buf_in);
3105     }
3106 
3107     if (in->resampler) {
3108         release_resampler(in->resampler);
3109     }
3110 #endif
3111 
3112     free(stream);
3113 
3114     pthread_mutex_unlock(&adev->lock_inputs);
3115 
3116     return;
3117 }
3118 
adev_dump(const audio_hw_device_t * device,int fd)3119 static int adev_dump(const audio_hw_device_t *device, int fd)
3120 {
3121     (void)device;
3122     (void)fd;
3123 
3124     return 0;
3125 }
3126 
adev_close(hw_device_t * device)3127 static int adev_close(hw_device_t *device)
3128 {
3129     struct audio_device *adev = (struct audio_device *)device;
3130     free(adev->snd_dev_ref_cnt);
3131     free_mixer_list(adev);
3132     free(device);
3133     return 0;
3134 }
3135 
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)3136 static int adev_open(const hw_module_t *module, const char *name,
3137                      hw_device_t **device)
3138 {
3139     struct audio_device *adev;
3140     int i, ret, retry_count;
3141 
3142     ALOGV("%s: enter", __func__);
3143     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3144 
3145     adev = calloc(1, sizeof(struct audio_device));
3146 
3147     adev->device.common.tag = HARDWARE_DEVICE_TAG;
3148     adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3149     adev->device.common.module = (struct hw_module_t *)module;
3150     adev->device.common.close = adev_close;
3151 
3152     adev->device.init_check = adev_init_check;
3153     adev->device.set_voice_volume = adev_set_voice_volume;
3154     adev->device.set_master_volume = adev_set_master_volume;
3155     adev->device.get_master_volume = adev_get_master_volume;
3156     adev->device.set_master_mute = adev_set_master_mute;
3157     adev->device.get_master_mute = adev_get_master_mute;
3158     adev->device.set_mode = adev_set_mode;
3159     adev->device.set_mic_mute = adev_set_mic_mute;
3160     adev->device.get_mic_mute = adev_get_mic_mute;
3161     adev->device.set_parameters = adev_set_parameters;
3162     adev->device.get_parameters = adev_get_parameters;
3163     adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3164     adev->device.open_output_stream = adev_open_output_stream;
3165     adev->device.close_output_stream = adev_close_output_stream;
3166     adev->device.open_input_stream = adev_open_input_stream;
3167     adev->device.close_input_stream = adev_close_input_stream;
3168     adev->device.dump = adev_dump;
3169 
3170     /* Set the default route before the PCM stream is opened */
3171     adev->mode = AUDIO_MODE_NORMAL;
3172     adev->active_input = NULL;
3173     adev->primary_output = NULL;
3174     adev->voice_volume = 1.0f;
3175     adev->tty_mode = TTY_MODE_OFF;
3176     adev->bluetooth_nrec = true;
3177     adev->in_call = false;
3178     /* adev->cur_hdmi_channels = 0;  by calloc() */
3179     adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
3180 
3181     adev->dualmic_config = DUALMIC_CONFIG_NONE;
3182     adev->ns_in_voice_rec = false;
3183 
3184     list_init(&adev->usecase_list);
3185 
3186     if (mixer_init(adev) != 0) {
3187         free(adev->snd_dev_ref_cnt);
3188         free(adev);
3189         ALOGE("%s: Failed to init, aborting.", __func__);
3190         *device = NULL;
3191         return -EINVAL;
3192     }
3193 
3194 
3195     if (access(SOUND_TRIGGER_HAL_LIBRARY_PATH, R_OK) == 0) {
3196         adev->sound_trigger_lib = dlopen(SOUND_TRIGGER_HAL_LIBRARY_PATH,
3197                                          RTLD_NOW);
3198         if (adev->sound_trigger_lib == NULL) {
3199             ALOGE("%s: DLOPEN failed for %s", __func__,
3200                   SOUND_TRIGGER_HAL_LIBRARY_PATH);
3201         } else {
3202             ALOGV("%s: DLOPEN successful for %s", __func__,
3203                   SOUND_TRIGGER_HAL_LIBRARY_PATH);
3204             adev->sound_trigger_open_for_streaming =
3205                     (int (*)(void))dlsym(adev->sound_trigger_lib,
3206                                          "sound_trigger_open_for_streaming");
3207             adev->sound_trigger_read_samples =
3208                     (size_t (*)(int, void *, size_t))dlsym(
3209                             adev->sound_trigger_lib,
3210                             "sound_trigger_read_samples");
3211             adev->sound_trigger_close_for_streaming =
3212                         (int (*)(int))dlsym(
3213                                 adev->sound_trigger_lib,
3214                                 "sound_trigger_close_for_streaming");
3215             if (!adev->sound_trigger_open_for_streaming ||
3216                 !adev->sound_trigger_read_samples ||
3217                 !adev->sound_trigger_close_for_streaming) {
3218 
3219                 ALOGE("%s: Error grabbing functions in %s", __func__,
3220                       SOUND_TRIGGER_HAL_LIBRARY_PATH);
3221                 adev->sound_trigger_open_for_streaming = 0;
3222                 adev->sound_trigger_read_samples = 0;
3223                 adev->sound_trigger_close_for_streaming = 0;
3224             }
3225         }
3226     }
3227 
3228     *device = &adev->device.common;
3229 
3230     cras_dsp_init("/system/etc/cras/speakerdsp.ini");
3231 
3232     ALOGV("%s: exit", __func__);
3233     return 0;
3234 }
3235 
3236 static struct hw_module_methods_t hal_module_methods = {
3237     .open = adev_open,
3238 };
3239 
3240 struct audio_module HAL_MODULE_INFO_SYM = {
3241     .common = {
3242         .tag = HARDWARE_MODULE_TAG,
3243         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3244         .hal_api_version = HARDWARE_HAL_API_VERSION,
3245         .id = AUDIO_HARDWARE_MODULE_ID,
3246         .name = "NVIDIA Tegra Audio HAL",
3247         .author = "The Android Open Source Project",
3248         .methods = &hal_module_methods,
3249     },
3250 };
3251