• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "audio_hw_primary"
18 /*#define LOG_NDEBUG 0*/
19 /*#define VERY_VERY_VERBOSE_LOGGING*/
20 #ifdef VERY_VERY_VERBOSE_LOGGING
21 #define ALOGVV ALOGV
22 #else
23 #define ALOGVV(a...) do { } while(0)
24 #endif
25 
26 #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_hw.h"
47 #include "platform_api.h"
48 #include <platform.h>
49 
50 #include "sound/compress_params.h"
51 
52 #define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
53 #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
54 /* ToDo: Check and update a proper value in msec */
55 #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
56 #define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
57 
58 struct pcm_config pcm_config_deep_buffer = {
59     .channels = 2,
60     .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
61     .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
62     .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
63     .format = PCM_FORMAT_S16_LE,
64     .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
65     .stop_threshold = INT_MAX,
66     .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
67 };
68 
69 struct pcm_config pcm_config_low_latency = {
70     .channels = 2,
71     .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
72     .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
73     .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
74     .format = PCM_FORMAT_S16_LE,
75     .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
76     .stop_threshold = INT_MAX,
77     .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
78 };
79 
80 struct pcm_config pcm_config_hdmi_multi = {
81     .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
82     .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
83     .period_size = HDMI_MULTI_PERIOD_SIZE,
84     .period_count = HDMI_MULTI_PERIOD_COUNT,
85     .format = PCM_FORMAT_S16_LE,
86     .start_threshold = 0,
87     .stop_threshold = INT_MAX,
88     .avail_min = 0,
89 };
90 
91 struct pcm_config pcm_config_audio_capture = {
92     .channels = 2,
93     .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
94     .format = PCM_FORMAT_S16_LE,
95 };
96 
97 struct pcm_config pcm_config_voice_call = {
98     .channels = 1,
99     .rate = 8000,
100     .period_size = 160,
101     .period_count = 2,
102     .format = PCM_FORMAT_S16_LE,
103 };
104 
105 static const char * const use_case_table[AUDIO_USECASE_MAX] = {
106     [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
107     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
108     [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
109     [USECASE_AUDIO_RECORD] = "audio-record",
110     [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
111     [USECASE_VOICE_CALL] = "voice-call",
112     [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
113 };
114 
115 
116 #define STRING_TO_ENUM(string) { #string, string }
117 
118 struct string_to_enum {
119     const char *name;
120     uint32_t value;
121 };
122 
123 static const struct string_to_enum out_channels_name_to_enum_table[] = {
124     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
125     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
126     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
127 };
128 
129 static int set_voice_volume_l(struct audio_device *adev, float volume);
130 
is_supported_format(audio_format_t format)131 static bool is_supported_format(audio_format_t format)
132 {
133     if (format == AUDIO_FORMAT_MP3 ||
134             format == AUDIO_FORMAT_AAC)
135         return true;
136 
137     return false;
138 }
139 
get_snd_codec_id(audio_format_t format)140 static int get_snd_codec_id(audio_format_t format)
141 {
142     int id = 0;
143 
144     switch (format) {
145     case AUDIO_FORMAT_MP3:
146         id = SND_AUDIOCODEC_MP3;
147         break;
148     case AUDIO_FORMAT_AAC:
149         id = SND_AUDIOCODEC_AAC;
150         break;
151     default:
152         ALOGE("%s: Unsupported audio format", __func__);
153     }
154 
155     return id;
156 }
157 
enable_audio_route(struct audio_device * adev,struct audio_usecase * usecase,bool update_mixer)158 static int enable_audio_route(struct audio_device *adev,
159                               struct audio_usecase *usecase,
160                               bool update_mixer)
161 {
162     snd_device_t snd_device;
163     char mixer_path[50];
164 
165     if (usecase == NULL)
166         return -EINVAL;
167 
168     ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
169 
170     if (usecase->type == PCM_CAPTURE)
171         snd_device = usecase->in_snd_device;
172     else
173         snd_device = usecase->out_snd_device;
174 
175     strcpy(mixer_path, use_case_table[usecase->id]);
176     platform_add_backend_name(mixer_path, snd_device);
177     ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
178     audio_route_apply_path(adev->audio_route, mixer_path);
179     if (update_mixer)
180         audio_route_update_mixer(adev->audio_route);
181 
182     ALOGV("%s: exit", __func__);
183     return 0;
184 }
185 
disable_audio_route(struct audio_device * adev,struct audio_usecase * usecase,bool update_mixer)186 static int disable_audio_route(struct audio_device *adev,
187                                struct audio_usecase *usecase,
188                                bool update_mixer)
189 {
190     snd_device_t snd_device;
191     char mixer_path[50];
192 
193     if (usecase == NULL)
194         return -EINVAL;
195 
196     ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
197     if (usecase->type == PCM_CAPTURE)
198         snd_device = usecase->in_snd_device;
199     else
200         snd_device = usecase->out_snd_device;
201     strcpy(mixer_path, use_case_table[usecase->id]);
202     platform_add_backend_name(mixer_path, snd_device);
203     ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
204     audio_route_reset_path(adev->audio_route, mixer_path);
205     if (update_mixer)
206         audio_route_update_mixer(adev->audio_route);
207 
208     ALOGV("%s: exit", __func__);
209     return 0;
210 }
211 
enable_snd_device(struct audio_device * adev,snd_device_t snd_device,bool update_mixer)212 static int enable_snd_device(struct audio_device *adev,
213                              snd_device_t snd_device,
214                              bool update_mixer)
215 {
216     if (snd_device < SND_DEVICE_MIN ||
217         snd_device >= SND_DEVICE_MAX) {
218         ALOGE("%s: Invalid sound device %d", __func__, snd_device);
219         return -EINVAL;
220     }
221 
222     adev->snd_dev_ref_cnt[snd_device]++;
223     if (adev->snd_dev_ref_cnt[snd_device] > 1) {
224         ALOGV("%s: snd_device(%d: %s) is already active",
225               __func__, snd_device, platform_get_snd_device_name(snd_device));
226         return 0;
227     }
228 
229     if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
230         adev->snd_dev_ref_cnt[snd_device]--;
231         return -EINVAL;
232     }
233 
234     ALOGV("%s: snd_device(%d: %s)", __func__,
235           snd_device, platform_get_snd_device_name(snd_device));
236     audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device));
237     if (update_mixer)
238         audio_route_update_mixer(adev->audio_route);
239 
240     return 0;
241 }
242 
disable_snd_device(struct audio_device * adev,snd_device_t snd_device,bool update_mixer)243 static int disable_snd_device(struct audio_device *adev,
244                               snd_device_t snd_device,
245                               bool update_mixer)
246 {
247     if (snd_device < SND_DEVICE_MIN ||
248         snd_device >= SND_DEVICE_MAX) {
249         ALOGE("%s: Invalid sound device %d", __func__, snd_device);
250         return -EINVAL;
251     }
252     if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
253         ALOGE("%s: device ref cnt is already 0", __func__);
254         return -EINVAL;
255     }
256     adev->snd_dev_ref_cnt[snd_device]--;
257     if (adev->snd_dev_ref_cnt[snd_device] == 0) {
258         ALOGV("%s: snd_device(%d: %s)", __func__,
259               snd_device, platform_get_snd_device_name(snd_device));
260         audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device));
261         if (update_mixer)
262             audio_route_update_mixer(adev->audio_route);
263     }
264     return 0;
265 }
266 
check_usecases_codec_backend(struct audio_device * adev,struct audio_usecase * uc_info,snd_device_t snd_device)267 static void check_usecases_codec_backend(struct audio_device *adev,
268                                           struct audio_usecase *uc_info,
269                                           snd_device_t snd_device)
270 {
271     struct listnode *node;
272     struct audio_usecase *usecase;
273     bool switch_device[AUDIO_USECASE_MAX];
274     int i, num_uc_to_switch = 0;
275 
276     /*
277      * This function is to make sure that all the usecases that are active on
278      * the hardware codec backend are always routed to any one device that is
279      * handled by the hardware codec.
280      * For example, if low-latency and deep-buffer usecases are currently active
281      * on speaker and out_set_parameters(headset) is received on low-latency
282      * output, then we have to make sure deep-buffer is also switched to headset,
283      * because of the limitation that both the devices cannot be enabled
284      * at the same time as they share the same backend.
285      */
286     /* Disable all the usecases on the shared backend other than the
287        specified usecase */
288     for (i = 0; i < AUDIO_USECASE_MAX; i++)
289         switch_device[i] = false;
290 
291     list_for_each(node, &adev->usecase_list) {
292         usecase = node_to_item(node, struct audio_usecase, list);
293         if (usecase->type != PCM_CAPTURE &&
294                 usecase != uc_info &&
295                 usecase->out_snd_device != snd_device &&
296                 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
297             ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
298                   __func__, use_case_table[usecase->id],
299                   platform_get_snd_device_name(usecase->out_snd_device));
300             disable_audio_route(adev, usecase, false);
301             switch_device[usecase->id] = true;
302             num_uc_to_switch++;
303         }
304     }
305 
306     if (num_uc_to_switch) {
307         /* Make sure all the streams are de-routed before disabling the device */
308         audio_route_update_mixer(adev->audio_route);
309 
310         list_for_each(node, &adev->usecase_list) {
311             usecase = node_to_item(node, struct audio_usecase, list);
312             if (switch_device[usecase->id]) {
313                 disable_snd_device(adev, usecase->out_snd_device, false);
314             }
315         }
316 
317         list_for_each(node, &adev->usecase_list) {
318             usecase = node_to_item(node, struct audio_usecase, list);
319             if (switch_device[usecase->id]) {
320                 enable_snd_device(adev, snd_device, false);
321             }
322         }
323 
324         /* Make sure new snd device is enabled before re-routing the streams */
325         audio_route_update_mixer(adev->audio_route);
326 
327         /* Re-route all the usecases on the shared backend other than the
328            specified usecase to new snd devices */
329         list_for_each(node, &adev->usecase_list) {
330             usecase = node_to_item(node, struct audio_usecase, list);
331             /* Update the out_snd_device only before enabling the audio route */
332             if (switch_device[usecase->id] ) {
333                 usecase->out_snd_device = snd_device;
334                 enable_audio_route(adev, usecase, false);
335             }
336         }
337 
338         audio_route_update_mixer(adev->audio_route);
339     }
340 }
341 
check_and_route_capture_usecases(struct audio_device * adev,struct audio_usecase * uc_info,snd_device_t snd_device)342 static void check_and_route_capture_usecases(struct audio_device *adev,
343                                              struct audio_usecase *uc_info,
344                                              snd_device_t snd_device)
345 {
346     struct listnode *node;
347     struct audio_usecase *usecase;
348     bool switch_device[AUDIO_USECASE_MAX];
349     int i, num_uc_to_switch = 0;
350 
351     /*
352      * This function is to make sure that all the active capture usecases
353      * are always routed to the same input sound device.
354      * For example, if audio-record and voice-call usecases are currently
355      * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
356      * is received for voice call then we have to make sure that audio-record
357      * usecase is also switched to earpiece i.e. voice-dmic-ef,
358      * because of the limitation that two devices cannot be enabled
359      * at the same time if they share the same backend.
360      */
361     for (i = 0; i < AUDIO_USECASE_MAX; i++)
362         switch_device[i] = false;
363 
364     list_for_each(node, &adev->usecase_list) {
365         usecase = node_to_item(node, struct audio_usecase, list);
366         if (usecase->type != PCM_PLAYBACK &&
367                 usecase != uc_info &&
368                 usecase->in_snd_device != snd_device) {
369             ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
370                   __func__, use_case_table[usecase->id],
371                   platform_get_snd_device_name(usecase->in_snd_device));
372             disable_audio_route(adev, usecase, false);
373             switch_device[usecase->id] = true;
374             num_uc_to_switch++;
375         }
376     }
377 
378     if (num_uc_to_switch) {
379         /* Make sure all the streams are de-routed before disabling the device */
380         audio_route_update_mixer(adev->audio_route);
381 
382         list_for_each(node, &adev->usecase_list) {
383             usecase = node_to_item(node, struct audio_usecase, list);
384             if (switch_device[usecase->id]) {
385                 disable_snd_device(adev, usecase->in_snd_device, false);
386                 enable_snd_device(adev, snd_device, false);
387             }
388         }
389 
390         /* Make sure new snd device is enabled before re-routing the streams */
391         audio_route_update_mixer(adev->audio_route);
392 
393         /* Re-route all the usecases on the shared backend other than the
394            specified usecase to new snd devices */
395         list_for_each(node, &adev->usecase_list) {
396             usecase = node_to_item(node, struct audio_usecase, list);
397             /* Update the in_snd_device only before enabling the audio route */
398             if (switch_device[usecase->id] ) {
399                 usecase->in_snd_device = snd_device;
400                 enable_audio_route(adev, usecase, false);
401             }
402         }
403 
404         audio_route_update_mixer(adev->audio_route);
405     }
406 }
407 
408 
409 /* must be called with hw device mutex locked */
read_hdmi_channel_masks(struct stream_out * out)410 static int read_hdmi_channel_masks(struct stream_out *out)
411 {
412     int ret = 0;
413     int channels = platform_edid_get_max_channels(out->dev->platform);
414 
415     switch (channels) {
416         /*
417          * Do not handle stereo output in Multi-channel cases
418          * Stereo case is handled in normal playback path
419          */
420     case 6:
421         ALOGV("%s: HDMI supports 5.1", __func__);
422         out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
423         break;
424     case 8:
425         ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
426         out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
427         out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
428         break;
429     default:
430         ALOGE("HDMI does not support multi channel playback");
431         ret = -ENOSYS;
432         break;
433     }
434     return ret;
435 }
436 
get_usecase_from_list(struct audio_device * adev,audio_usecase_t uc_id)437 static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
438                                                    audio_usecase_t uc_id)
439 {
440     struct audio_usecase *usecase;
441     struct listnode *node;
442 
443     list_for_each(node, &adev->usecase_list) {
444         usecase = node_to_item(node, struct audio_usecase, list);
445         if (usecase->id == uc_id)
446             return usecase;
447     }
448     return NULL;
449 }
450 
select_devices(struct audio_device * adev,audio_usecase_t uc_id)451 static int select_devices(struct audio_device *adev,
452                           audio_usecase_t uc_id)
453 {
454     snd_device_t out_snd_device = SND_DEVICE_NONE;
455     snd_device_t in_snd_device = SND_DEVICE_NONE;
456     struct audio_usecase *usecase = NULL;
457     struct audio_usecase *vc_usecase = NULL;
458     struct listnode *node;
459     int status = 0;
460 
461     usecase = get_usecase_from_list(adev, uc_id);
462     if (usecase == NULL) {
463         ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
464         return -EINVAL;
465     }
466 
467     if (usecase->type == VOICE_CALL) {
468         out_snd_device = platform_get_output_snd_device(adev->platform,
469                                                         usecase->stream.out->devices);
470         in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
471         usecase->devices = usecase->stream.out->devices;
472     } else {
473         /*
474          * If the voice call is active, use the sound devices of voice call usecase
475          * so that it would not result any device switch. All the usecases will
476          * be switched to new device when select_devices() is called for voice call
477          * usecase. This is to avoid switching devices for voice call when
478          * check_usecases_codec_backend() is called below.
479          */
480         if (adev->in_call) {
481             vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
482             if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
483                 in_snd_device = vc_usecase->in_snd_device;
484                 out_snd_device = vc_usecase->out_snd_device;
485             }
486         }
487         if (usecase->type == PCM_PLAYBACK) {
488             usecase->devices = usecase->stream.out->devices;
489             in_snd_device = SND_DEVICE_NONE;
490             if (out_snd_device == SND_DEVICE_NONE) {
491                 out_snd_device = platform_get_output_snd_device(adev->platform,
492                                             usecase->stream.out->devices);
493                 if (usecase->stream.out == adev->primary_output &&
494                         adev->active_input &&
495                         adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
496                     select_devices(adev, adev->active_input->usecase);
497                 }
498             }
499         } else if (usecase->type == PCM_CAPTURE) {
500             usecase->devices = usecase->stream.in->device;
501             out_snd_device = SND_DEVICE_NONE;
502             if (in_snd_device == SND_DEVICE_NONE) {
503                 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
504                         adev->primary_output && !adev->primary_output->standby) {
505                     in_snd_device = platform_get_input_snd_device(adev->platform,
506                                         adev->primary_output->devices);
507                 } else {
508                     in_snd_device = platform_get_input_snd_device(adev->platform,
509                                                                   AUDIO_DEVICE_NONE);
510                 }
511             }
512         }
513     }
514 
515     if (out_snd_device == usecase->out_snd_device &&
516         in_snd_device == usecase->in_snd_device) {
517         return 0;
518     }
519 
520     ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
521           out_snd_device, platform_get_snd_device_name(out_snd_device),
522           in_snd_device,  platform_get_snd_device_name(in_snd_device));
523 
524     /*
525      * Limitation: While in call, to do a device switch we need to disable
526      * and enable both RX and TX devices though one of them is same as current
527      * device.
528      */
529     if (usecase->type == VOICE_CALL) {
530         status = platform_switch_voice_call_device_pre(adev->platform);
531     }
532 
533     /* Disable current sound devices */
534     if (usecase->out_snd_device != SND_DEVICE_NONE) {
535         disable_audio_route(adev, usecase, true);
536         disable_snd_device(adev, usecase->out_snd_device, false);
537     }
538 
539     if (usecase->in_snd_device != SND_DEVICE_NONE) {
540         disable_audio_route(adev, usecase, true);
541         disable_snd_device(adev, usecase->in_snd_device, false);
542     }
543 
544     /* Enable new sound devices */
545     if (out_snd_device != SND_DEVICE_NONE) {
546         if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
547             check_usecases_codec_backend(adev, usecase, out_snd_device);
548         enable_snd_device(adev, out_snd_device, false);
549     }
550 
551     if (in_snd_device != SND_DEVICE_NONE) {
552         check_and_route_capture_usecases(adev, usecase, in_snd_device);
553         enable_snd_device(adev, in_snd_device, false);
554     }
555 
556     if (usecase->type == VOICE_CALL)
557         status = platform_switch_voice_call_device_post(adev->platform,
558                                                         out_snd_device,
559                                                         in_snd_device);
560 
561     audio_route_update_mixer(adev->audio_route);
562 
563     usecase->in_snd_device = in_snd_device;
564     usecase->out_snd_device = out_snd_device;
565 
566     enable_audio_route(adev, usecase, true);
567 
568     return status;
569 }
570 
stop_input_stream(struct stream_in * in)571 static int stop_input_stream(struct stream_in *in)
572 {
573     int i, ret = 0;
574     struct audio_usecase *uc_info;
575     struct audio_device *adev = in->dev;
576 
577     adev->active_input = NULL;
578 
579     ALOGV("%s: enter: usecase(%d: %s)", __func__,
580           in->usecase, use_case_table[in->usecase]);
581     uc_info = get_usecase_from_list(adev, in->usecase);
582     if (uc_info == NULL) {
583         ALOGE("%s: Could not find the usecase (%d) in the list",
584               __func__, in->usecase);
585         return -EINVAL;
586     }
587 
588     /* 1. Disable stream specific mixer controls */
589     disable_audio_route(adev, uc_info, true);
590 
591     /* 2. Disable the tx device */
592     disable_snd_device(adev, uc_info->in_snd_device, true);
593 
594     list_remove(&uc_info->list);
595     free(uc_info);
596 
597     ALOGV("%s: exit: status(%d)", __func__, ret);
598     return ret;
599 }
600 
start_input_stream(struct stream_in * in)601 int start_input_stream(struct stream_in *in)
602 {
603     /* 1. Enable output device and stream routing controls */
604     int ret = 0;
605     struct audio_usecase *uc_info;
606     struct audio_device *adev = in->dev;
607 
608     ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
609     in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
610     if (in->pcm_device_id < 0) {
611         ALOGE("%s: Could not find PCM device id for the usecase(%d)",
612               __func__, in->usecase);
613         ret = -EINVAL;
614         goto error_config;
615     }
616 
617     adev->active_input = in;
618     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
619     uc_info->id = in->usecase;
620     uc_info->type = PCM_CAPTURE;
621     uc_info->stream.in = in;
622     uc_info->devices = in->device;
623     uc_info->in_snd_device = SND_DEVICE_NONE;
624     uc_info->out_snd_device = SND_DEVICE_NONE;
625 
626     list_add_tail(&adev->usecase_list, &uc_info->list);
627     select_devices(adev, in->usecase);
628 
629     ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
630           __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
631     in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
632                            PCM_IN, &in->config);
633     if (in->pcm && !pcm_is_ready(in->pcm)) {
634         ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
635         pcm_close(in->pcm);
636         in->pcm = NULL;
637         ret = -EIO;
638         goto error_open;
639     }
640     ALOGV("%s: exit", __func__);
641     return ret;
642 
643 error_open:
644     stop_input_stream(in);
645 
646 error_config:
647     adev->active_input = NULL;
648     ALOGD("%s: exit: status(%d)", __func__, ret);
649 
650     return ret;
651 }
652 
653 /* must be called with out->lock locked */
send_offload_cmd_l(struct stream_out * out,int command)654 static int send_offload_cmd_l(struct stream_out* out, int command)
655 {
656     struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
657 
658     ALOGVV("%s %d", __func__, command);
659 
660     cmd->cmd = command;
661     list_add_tail(&out->offload_cmd_list, &cmd->node);
662     pthread_cond_signal(&out->offload_cond);
663     return 0;
664 }
665 
666 /* must be called iwth out->lock locked */
stop_compressed_output_l(struct stream_out * out)667 static void stop_compressed_output_l(struct stream_out *out)
668 {
669     out->offload_state = OFFLOAD_STATE_IDLE;
670     out->playback_started = 0;
671     out->send_new_metadata = 1;
672     if (out->compr != NULL) {
673         compress_stop(out->compr);
674         while (out->offload_thread_blocked) {
675             pthread_cond_wait(&out->cond, &out->lock);
676         }
677     }
678 }
679 
offload_thread_loop(void * context)680 static void *offload_thread_loop(void *context)
681 {
682     struct stream_out *out = (struct stream_out *) context;
683     struct listnode *item;
684 
685     out->offload_state = OFFLOAD_STATE_IDLE;
686     out->playback_started = 0;
687 
688     setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
689     set_sched_policy(0, SP_FOREGROUND);
690     prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
691 
692     ALOGV("%s", __func__);
693     pthread_mutex_lock(&out->lock);
694     for (;;) {
695         struct offload_cmd *cmd = NULL;
696         stream_callback_event_t event;
697         bool send_callback = false;
698 
699         ALOGVV("%s offload_cmd_list %d out->offload_state %d",
700               __func__, list_empty(&out->offload_cmd_list),
701               out->offload_state);
702         if (list_empty(&out->offload_cmd_list)) {
703             ALOGV("%s SLEEPING", __func__);
704             pthread_cond_wait(&out->offload_cond, &out->lock);
705             ALOGV("%s RUNNING", __func__);
706             continue;
707         }
708 
709         item = list_head(&out->offload_cmd_list);
710         cmd = node_to_item(item, struct offload_cmd, node);
711         list_remove(item);
712 
713         ALOGVV("%s STATE %d CMD %d out->compr %p",
714                __func__, out->offload_state, cmd->cmd, out->compr);
715 
716         if (cmd->cmd == OFFLOAD_CMD_EXIT) {
717             free(cmd);
718             break;
719         }
720 
721         if (out->compr == NULL) {
722             ALOGE("%s: Compress handle is NULL", __func__);
723             pthread_cond_signal(&out->cond);
724             continue;
725         }
726         out->offload_thread_blocked = true;
727         pthread_mutex_unlock(&out->lock);
728         send_callback = false;
729         switch(cmd->cmd) {
730         case OFFLOAD_CMD_WAIT_FOR_BUFFER:
731             compress_wait(out->compr, -1);
732             send_callback = true;
733             event = STREAM_CBK_EVENT_WRITE_READY;
734             break;
735         case OFFLOAD_CMD_PARTIAL_DRAIN:
736             compress_next_track(out->compr);
737             compress_partial_drain(out->compr);
738             send_callback = true;
739             event = STREAM_CBK_EVENT_DRAIN_READY;
740             break;
741         case OFFLOAD_CMD_DRAIN:
742             compress_drain(out->compr);
743             send_callback = true;
744             event = STREAM_CBK_EVENT_DRAIN_READY;
745             break;
746         default:
747             ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
748             break;
749         }
750         pthread_mutex_lock(&out->lock);
751         out->offload_thread_blocked = false;
752         pthread_cond_signal(&out->cond);
753         if (send_callback) {
754             out->offload_callback(event, NULL, out->offload_cookie);
755         }
756         free(cmd);
757     }
758 
759     pthread_cond_signal(&out->cond);
760     while (!list_empty(&out->offload_cmd_list)) {
761         item = list_head(&out->offload_cmd_list);
762         list_remove(item);
763         free(node_to_item(item, struct offload_cmd, node));
764     }
765     pthread_mutex_unlock(&out->lock);
766 
767     return NULL;
768 }
769 
create_offload_callback_thread(struct stream_out * out)770 static int create_offload_callback_thread(struct stream_out *out)
771 {
772     pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
773     list_init(&out->offload_cmd_list);
774     pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
775                     offload_thread_loop, out);
776     return 0;
777 }
778 
destroy_offload_callback_thread(struct stream_out * out)779 static int destroy_offload_callback_thread(struct stream_out *out)
780 {
781     pthread_mutex_lock(&out->lock);
782     stop_compressed_output_l(out);
783     send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
784 
785     pthread_mutex_unlock(&out->lock);
786     pthread_join(out->offload_thread, (void **) NULL);
787     pthread_cond_destroy(&out->offload_cond);
788 
789     return 0;
790 }
791 
allow_hdmi_channel_config(struct audio_device * adev)792 static bool allow_hdmi_channel_config(struct audio_device *adev)
793 {
794     struct listnode *node;
795     struct audio_usecase *usecase;
796     bool ret = true;
797 
798     list_for_each(node, &adev->usecase_list) {
799         usecase = node_to_item(node, struct audio_usecase, list);
800         if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
801             /*
802              * If voice call is already existing, do not proceed further to avoid
803              * disabling/enabling both RX and TX devices, CSD calls, etc.
804              * Once the voice call done, the HDMI channels can be configured to
805              * max channels of remaining use cases.
806              */
807             if (usecase->id == USECASE_VOICE_CALL) {
808                 ALOGD("%s: voice call is active, no change in HDMI channels",
809                       __func__);
810                 ret = false;
811                 break;
812             } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
813                 ALOGD("%s: multi channel playback is active, "
814                       "no change in HDMI channels", __func__);
815                 ret = false;
816                 break;
817             }
818         }
819     }
820     return ret;
821 }
822 
check_and_set_hdmi_channels(struct audio_device * adev,unsigned int channels)823 static int check_and_set_hdmi_channels(struct audio_device *adev,
824                                        unsigned int channels)
825 {
826     struct listnode *node;
827     struct audio_usecase *usecase;
828 
829     /* Check if change in HDMI channel config is allowed */
830     if (!allow_hdmi_channel_config(adev))
831         return 0;
832 
833     if (channels == adev->cur_hdmi_channels) {
834         ALOGD("%s: Requested channels are same as current", __func__);
835         return 0;
836     }
837 
838     platform_set_hdmi_channels(adev->platform, channels);
839     adev->cur_hdmi_channels = channels;
840 
841     /*
842      * Deroute all the playback streams routed to HDMI so that
843      * the back end is deactivated. Note that backend will not
844      * be deactivated if any one stream is connected to it.
845      */
846     list_for_each(node, &adev->usecase_list) {
847         usecase = node_to_item(node, struct audio_usecase, list);
848         if (usecase->type == PCM_PLAYBACK &&
849                 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
850             disable_audio_route(adev, usecase, true);
851         }
852     }
853 
854     /*
855      * Enable all the streams disabled above. Now the HDMI backend
856      * will be activated with new channel configuration
857      */
858     list_for_each(node, &adev->usecase_list) {
859         usecase = node_to_item(node, struct audio_usecase, list);
860         if (usecase->type == PCM_PLAYBACK &&
861                 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
862             enable_audio_route(adev, usecase, true);
863         }
864     }
865 
866     return 0;
867 }
868 
stop_output_stream(struct stream_out * out)869 static int stop_output_stream(struct stream_out *out)
870 {
871     int i, ret = 0;
872     struct audio_usecase *uc_info;
873     struct audio_device *adev = out->dev;
874 
875     ALOGV("%s: enter: usecase(%d: %s)", __func__,
876           out->usecase, use_case_table[out->usecase]);
877     uc_info = get_usecase_from_list(adev, out->usecase);
878     if (uc_info == NULL) {
879         ALOGE("%s: Could not find the usecase (%d) in the list",
880               __func__, out->usecase);
881         return -EINVAL;
882     }
883 
884     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
885             adev->visualizer_stop_output != NULL)
886         adev->visualizer_stop_output(out->handle);
887 
888     /* 1. Get and set stream specific mixer controls */
889     disable_audio_route(adev, uc_info, true);
890 
891     /* 2. Disable the rx device */
892     disable_snd_device(adev, uc_info->out_snd_device, true);
893 
894     list_remove(&uc_info->list);
895     free(uc_info);
896 
897     /* Must be called after removing the usecase from list */
898     if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
899         check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
900 
901     ALOGV("%s: exit: status(%d)", __func__, ret);
902     return ret;
903 }
904 
start_output_stream(struct stream_out * out)905 int start_output_stream(struct stream_out *out)
906 {
907     int ret = 0;
908     struct audio_usecase *uc_info;
909     struct audio_device *adev = out->dev;
910 
911     ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
912           __func__, out->usecase, use_case_table[out->usecase], out->devices);
913     out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
914     if (out->pcm_device_id < 0) {
915         ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
916               __func__, out->pcm_device_id, out->usecase);
917         ret = -EINVAL;
918         goto error_config;
919     }
920 
921     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
922     uc_info->id = out->usecase;
923     uc_info->type = PCM_PLAYBACK;
924     uc_info->stream.out = out;
925     uc_info->devices = out->devices;
926     uc_info->in_snd_device = SND_DEVICE_NONE;
927     uc_info->out_snd_device = SND_DEVICE_NONE;
928 
929     /* This must be called before adding this usecase to the list */
930     if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
931         check_and_set_hdmi_channels(adev, out->config.channels);
932 
933     list_add_tail(&adev->usecase_list, &uc_info->list);
934 
935     select_devices(adev, out->usecase);
936 
937     ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
938           __func__, 0, out->pcm_device_id);
939     if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
940         out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
941                                PCM_OUT | PCM_MONOTONIC, &out->config);
942         if (out->pcm && !pcm_is_ready(out->pcm)) {
943             ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
944             pcm_close(out->pcm);
945             out->pcm = NULL;
946             ret = -EIO;
947             goto error_open;
948         }
949     } else {
950         out->pcm = NULL;
951         out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
952                                    COMPRESS_IN, &out->compr_config);
953         if (out->compr && !is_compress_ready(out->compr)) {
954             ALOGE("%s: %s", __func__, compress_get_error(out->compr));
955             compress_close(out->compr);
956             out->compr = NULL;
957             ret = -EIO;
958             goto error_open;
959         }
960         if (out->offload_callback)
961             compress_nonblock(out->compr, out->non_blocking);
962 
963         if (adev->visualizer_start_output != NULL)
964             adev->visualizer_start_output(out->handle);
965     }
966     ALOGV("%s: exit", __func__);
967     return 0;
968 error_open:
969     stop_output_stream(out);
970 error_config:
971     return ret;
972 }
973 
stop_voice_call(struct audio_device * adev)974 static int stop_voice_call(struct audio_device *adev)
975 {
976     int i, ret = 0;
977     struct audio_usecase *uc_info;
978 
979     ALOGV("%s: enter", __func__);
980     adev->in_call = false;
981 
982     ret = platform_stop_voice_call(adev->platform);
983 
984     /* 1. Close the PCM devices */
985     if (adev->voice_call_rx) {
986         pcm_close(adev->voice_call_rx);
987         adev->voice_call_rx = NULL;
988     }
989     if (adev->voice_call_tx) {
990         pcm_close(adev->voice_call_tx);
991         adev->voice_call_tx = NULL;
992     }
993 
994     uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
995     if (uc_info == NULL) {
996         ALOGE("%s: Could not find the usecase (%d) in the list",
997               __func__, USECASE_VOICE_CALL);
998         return -EINVAL;
999     }
1000 
1001     /* 2. Get and set stream specific mixer controls */
1002     disable_audio_route(adev, uc_info, true);
1003 
1004     /* 3. Disable the rx and tx devices */
1005     disable_snd_device(adev, uc_info->out_snd_device, false);
1006     disable_snd_device(adev, uc_info->in_snd_device, true);
1007 
1008     list_remove(&uc_info->list);
1009     free(uc_info);
1010 
1011     ALOGV("%s: exit: status(%d)", __func__, ret);
1012     return ret;
1013 }
1014 
start_voice_call(struct audio_device * adev)1015 static int start_voice_call(struct audio_device *adev)
1016 {
1017     int i, ret = 0;
1018     struct audio_usecase *uc_info;
1019     int pcm_dev_rx_id, pcm_dev_tx_id;
1020 
1021     ALOGV("%s: enter", __func__);
1022 
1023     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1024     uc_info->id = USECASE_VOICE_CALL;
1025     uc_info->type = VOICE_CALL;
1026     uc_info->stream.out = adev->primary_output;
1027     uc_info->devices = adev->primary_output->devices;
1028     uc_info->in_snd_device = SND_DEVICE_NONE;
1029     uc_info->out_snd_device = SND_DEVICE_NONE;
1030 
1031     list_add_tail(&adev->usecase_list, &uc_info->list);
1032 
1033     select_devices(adev, USECASE_VOICE_CALL);
1034 
1035     pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
1036     pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
1037 
1038     if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1039         ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1040               __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
1041         ret = -EIO;
1042         goto error_start_voice;
1043     }
1044 
1045     ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
1046           __func__, SOUND_CARD, pcm_dev_rx_id);
1047     adev->voice_call_rx = pcm_open(SOUND_CARD,
1048                                   pcm_dev_rx_id,
1049                                   PCM_OUT | PCM_MONOTONIC, &pcm_config_voice_call);
1050     if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1051         ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
1052         ret = -EIO;
1053         goto error_start_voice;
1054     }
1055 
1056     ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
1057           __func__, SOUND_CARD, pcm_dev_tx_id);
1058     adev->voice_call_tx = pcm_open(SOUND_CARD,
1059                                    pcm_dev_tx_id,
1060                                    PCM_IN, &pcm_config_voice_call);
1061     if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1062         ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
1063         ret = -EIO;
1064         goto error_start_voice;
1065     }
1066 
1067     /* set cached volume */
1068     set_voice_volume_l(adev, adev->voice_volume);
1069 
1070     pcm_start(adev->voice_call_rx);
1071     pcm_start(adev->voice_call_tx);
1072 
1073     ret = platform_start_voice_call(adev->platform);
1074     if (ret < 0) {
1075         ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
1076         goto error_start_voice;
1077     }
1078     adev->in_call = true;
1079     return 0;
1080 
1081 error_start_voice:
1082     stop_voice_call(adev);
1083 
1084     ALOGD("%s: exit: status(%d)", __func__, ret);
1085     return ret;
1086 }
1087 
check_input_parameters(uint32_t sample_rate,audio_format_t format,int channel_count)1088 static int check_input_parameters(uint32_t sample_rate,
1089                                   audio_format_t format,
1090                                   int channel_count)
1091 {
1092     if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1093 
1094     if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1095 
1096     switch (sample_rate) {
1097     case 8000:
1098     case 11025:
1099     case 12000:
1100     case 16000:
1101     case 22050:
1102     case 24000:
1103     case 32000:
1104     case 44100:
1105     case 48000:
1106         break;
1107     default:
1108         return -EINVAL;
1109     }
1110 
1111     return 0;
1112 }
1113 
get_input_buffer_size(uint32_t sample_rate,audio_format_t format,int channel_count)1114 static size_t get_input_buffer_size(uint32_t sample_rate,
1115                                     audio_format_t format,
1116                                     int channel_count)
1117 {
1118     size_t size = 0;
1119 
1120     if (check_input_parameters(sample_rate, format, channel_count) != 0)
1121         return 0;
1122 
1123     size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1124     /* ToDo: should use frame_size computed based on the format and
1125        channel_count here. */
1126     size *= sizeof(short) * channel_count;
1127 
1128     /* make sure the size is multiple of 64 */
1129     size += 0x3f;
1130     size &= ~0x3f;
1131 
1132     return size;
1133 }
1134 
out_get_sample_rate(const struct audio_stream * stream)1135 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1136 {
1137     struct stream_out *out = (struct stream_out *)stream;
1138 
1139     return out->sample_rate;
1140 }
1141 
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)1142 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1143 {
1144     return -ENOSYS;
1145 }
1146 
out_get_buffer_size(const struct audio_stream * stream)1147 static size_t out_get_buffer_size(const struct audio_stream *stream)
1148 {
1149     struct stream_out *out = (struct stream_out *)stream;
1150 
1151     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1152         return out->compr_config.fragment_size;
1153     }
1154 
1155     return out->config.period_size * audio_stream_frame_size(stream);
1156 }
1157 
out_get_channels(const struct audio_stream * stream)1158 static uint32_t out_get_channels(const struct audio_stream *stream)
1159 {
1160     struct stream_out *out = (struct stream_out *)stream;
1161 
1162     return out->channel_mask;
1163 }
1164 
out_get_format(const struct audio_stream * stream)1165 static audio_format_t out_get_format(const struct audio_stream *stream)
1166 {
1167     struct stream_out *out = (struct stream_out *)stream;
1168 
1169     return out->format;
1170 }
1171 
out_set_format(struct audio_stream * stream,audio_format_t format)1172 static int out_set_format(struct audio_stream *stream, audio_format_t format)
1173 {
1174     return -ENOSYS;
1175 }
1176 
out_standby(struct audio_stream * stream)1177 static int out_standby(struct audio_stream *stream)
1178 {
1179     struct stream_out *out = (struct stream_out *)stream;
1180     struct audio_device *adev = out->dev;
1181 
1182     ALOGV("%s: enter: usecase(%d: %s)", __func__,
1183           out->usecase, use_case_table[out->usecase]);
1184 
1185     pthread_mutex_lock(&out->lock);
1186     if (!out->standby) {
1187         pthread_mutex_lock(&adev->lock);
1188         out->standby = true;
1189         if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1190             if (out->pcm) {
1191                 pcm_close(out->pcm);
1192                 out->pcm = NULL;
1193             }
1194         } else {
1195             stop_compressed_output_l(out);
1196             out->gapless_mdata.encoder_delay = 0;
1197             out->gapless_mdata.encoder_padding = 0;
1198             if (out->compr != NULL) {
1199                 compress_close(out->compr);
1200                 out->compr = NULL;
1201             }
1202         }
1203         stop_output_stream(out);
1204         pthread_mutex_unlock(&adev->lock);
1205     }
1206     pthread_mutex_unlock(&out->lock);
1207     ALOGV("%s: exit", __func__);
1208     return 0;
1209 }
1210 
out_dump(const struct audio_stream * stream,int fd)1211 static int out_dump(const struct audio_stream *stream, int fd)
1212 {
1213     return 0;
1214 }
1215 
parse_compress_metadata(struct stream_out * out,struct str_parms * parms)1216 static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1217 {
1218     int ret = 0;
1219     char value[32];
1220     struct compr_gapless_mdata tmp_mdata;
1221 
1222     if (!out || !parms) {
1223         return -EINVAL;
1224     }
1225 
1226     ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1227     if (ret >= 0) {
1228         tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1229     } else {
1230         return -EINVAL;
1231     }
1232 
1233     ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1234     if (ret >= 0) {
1235         tmp_mdata.encoder_padding = atoi(value);
1236     } else {
1237         return -EINVAL;
1238     }
1239 
1240     out->gapless_mdata = tmp_mdata;
1241     out->send_new_metadata = 1;
1242     ALOGV("%s new encoder delay %u and padding %u", __func__,
1243           out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1244 
1245     return 0;
1246 }
1247 
1248 
out_set_parameters(struct audio_stream * stream,const char * kvpairs)1249 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1250 {
1251     struct stream_out *out = (struct stream_out *)stream;
1252     struct audio_device *adev = out->dev;
1253     struct audio_usecase *usecase;
1254     struct listnode *node;
1255     struct str_parms *parms;
1256     char value[32];
1257     int ret, val = 0;
1258     bool select_new_device = false;
1259 
1260     ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
1261           __func__, out->usecase, use_case_table[out->usecase], kvpairs);
1262     parms = str_parms_create_str(kvpairs);
1263     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1264     if (ret >= 0) {
1265         val = atoi(value);
1266         pthread_mutex_lock(&out->lock);
1267         pthread_mutex_lock(&adev->lock);
1268 
1269         /*
1270          * When HDMI cable is unplugged the music playback is paused and
1271          * the policy manager sends routing=0. But the audioflinger
1272          * continues to write data until standby time (3sec).
1273          * As the HDMI core is turned off, the write gets blocked.
1274          * Avoid this by routing audio to speaker until standby.
1275          */
1276         if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1277                 val == AUDIO_DEVICE_NONE) {
1278             val = AUDIO_DEVICE_OUT_SPEAKER;
1279         }
1280 
1281         /*
1282          * select_devices() call below switches all the usecases on the same
1283          * backend to the new device. Refer to check_usecases_codec_backend() in
1284          * the select_devices(). But how do we undo this?
1285          *
1286          * For example, music playback is active on headset (deep-buffer usecase)
1287          * and if we go to ringtones and select a ringtone, low-latency usecase
1288          * will be started on headset+speaker. As we can't enable headset+speaker
1289          * and headset devices at the same time, select_devices() switches the music
1290          * playback to headset+speaker while starting low-lateny usecase for ringtone.
1291          * So when the ringtone playback is completed, how do we undo the same?
1292          *
1293          * We are relying on the out_set_parameters() call on deep-buffer output,
1294          * once the ringtone playback is ended.
1295          * NOTE: We should not check if the current devices are same as new devices.
1296          *       Because select_devices() must be called to switch back the music
1297          *       playback to headset.
1298          */
1299         if (val != 0) {
1300             out->devices = val;
1301 
1302             if (!out->standby)
1303                 select_devices(adev, out->usecase);
1304 
1305             if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1306                     (out == adev->primary_output)) {
1307                 start_voice_call(adev);
1308             } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1309                        (out == adev->primary_output)) {
1310                 select_devices(adev, USECASE_VOICE_CALL);
1311             }
1312         }
1313 
1314         if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call &&
1315                 (out == adev->primary_output)) {
1316             stop_voice_call(adev);
1317         }
1318 
1319         pthread_mutex_unlock(&adev->lock);
1320         pthread_mutex_unlock(&out->lock);
1321     }
1322 
1323     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1324         parse_compress_metadata(out, parms);
1325     }
1326 
1327     str_parms_destroy(parms);
1328     ALOGV("%s: exit: code(%d)", __func__, ret);
1329     return ret;
1330 }
1331 
out_get_parameters(const struct audio_stream * stream,const char * keys)1332 static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1333 {
1334     struct stream_out *out = (struct stream_out *)stream;
1335     struct str_parms *query = str_parms_create_str(keys);
1336     char *str;
1337     char value[256];
1338     struct str_parms *reply = str_parms_create();
1339     size_t i, j;
1340     int ret;
1341     bool first = true;
1342     ALOGV("%s: enter: keys - %s", __func__, keys);
1343     ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1344     if (ret >= 0) {
1345         value[0] = '\0';
1346         i = 0;
1347         while (out->supported_channel_masks[i] != 0) {
1348             for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1349                 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1350                     if (!first) {
1351                         strcat(value, "|");
1352                     }
1353                     strcat(value, out_channels_name_to_enum_table[j].name);
1354                     first = false;
1355                     break;
1356                 }
1357             }
1358             i++;
1359         }
1360         str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1361         str = str_parms_to_str(reply);
1362     } else {
1363         str = strdup(keys);
1364     }
1365     str_parms_destroy(query);
1366     str_parms_destroy(reply);
1367     ALOGV("%s: exit: returns - %s", __func__, str);
1368     return str;
1369 }
1370 
out_get_latency(const struct audio_stream_out * stream)1371 static uint32_t out_get_latency(const struct audio_stream_out *stream)
1372 {
1373     struct stream_out *out = (struct stream_out *)stream;
1374 
1375     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1376         return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1377 
1378     return (out->config.period_count * out->config.period_size * 1000) /
1379            (out->config.rate);
1380 }
1381 
out_set_volume(struct audio_stream_out * stream,float left,float right)1382 static int out_set_volume(struct audio_stream_out *stream, float left,
1383                           float right)
1384 {
1385     struct stream_out *out = (struct stream_out *)stream;
1386     int volume[2];
1387 
1388     if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1389         /* only take left channel into account: the API is for stereo anyway */
1390         out->muted = (left == 0.0f);
1391         return 0;
1392     } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1393         const char *mixer_ctl_name = "Compress Playback Volume";
1394         struct audio_device *adev = out->dev;
1395         struct mixer_ctl *ctl;
1396 
1397         ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1398         if (!ctl) {
1399             ALOGE("%s: Could not get ctl for mixer cmd - %s",
1400                   __func__, mixer_ctl_name);
1401             return -EINVAL;
1402         }
1403         volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1404         volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1405         mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1406         return 0;
1407     }
1408 
1409     return -ENOSYS;
1410 }
1411 
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)1412 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1413                          size_t bytes)
1414 {
1415     struct stream_out *out = (struct stream_out *)stream;
1416     struct audio_device *adev = out->dev;
1417     ssize_t ret = 0;
1418 
1419     pthread_mutex_lock(&out->lock);
1420     if (out->standby) {
1421         out->standby = false;
1422         pthread_mutex_lock(&adev->lock);
1423         ret = start_output_stream(out);
1424         pthread_mutex_unlock(&adev->lock);
1425         /* ToDo: If use case is compress offload should return 0 */
1426         if (ret != 0) {
1427             out->standby = true;
1428             goto exit;
1429         }
1430     }
1431 
1432     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1433         ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1434         if (out->send_new_metadata) {
1435             ALOGVV("send new gapless metadata");
1436             compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1437             out->send_new_metadata = 0;
1438         }
1439 
1440         ret = compress_write(out->compr, buffer, bytes);
1441         ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
1442         if (ret >= 0 && ret < (ssize_t)bytes) {
1443             send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1444         }
1445         if (!out->playback_started) {
1446             compress_start(out->compr);
1447             out->playback_started = 1;
1448             out->offload_state = OFFLOAD_STATE_PLAYING;
1449         }
1450         pthread_mutex_unlock(&out->lock);
1451         return ret;
1452     } else {
1453         if (out->pcm) {
1454             if (out->muted)
1455                 memset((void *)buffer, 0, bytes);
1456             ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1457             ret = pcm_write(out->pcm, (void *)buffer, bytes);
1458             if (ret == 0)
1459                 out->written += bytes / (out->config.channels * sizeof(short));
1460         }
1461     }
1462 
1463 exit:
1464     pthread_mutex_unlock(&out->lock);
1465 
1466     if (ret != 0) {
1467         if (out->pcm)
1468             ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
1469         out_standby(&out->stream.common);
1470         usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1471                out_get_sample_rate(&out->stream.common));
1472     }
1473     return bytes;
1474 }
1475 
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)1476 static int out_get_render_position(const struct audio_stream_out *stream,
1477                                    uint32_t *dsp_frames)
1478 {
1479     struct stream_out *out = (struct stream_out *)stream;
1480     *dsp_frames = 0;
1481     if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1482         pthread_mutex_lock(&out->lock);
1483         if (out->compr != NULL) {
1484             compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1485                     &out->sample_rate);
1486             ALOGVV("%s rendered frames %d sample_rate %d",
1487                    __func__, *dsp_frames, out->sample_rate);
1488         }
1489         pthread_mutex_unlock(&out->lock);
1490         return 0;
1491     } else
1492         return -EINVAL;
1493 }
1494 
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1495 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1496 {
1497     return 0;
1498 }
1499 
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1500 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1501 {
1502     return 0;
1503 }
1504 
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)1505 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1506                                         int64_t *timestamp)
1507 {
1508     return -EINVAL;
1509 }
1510 
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)1511 static int out_get_presentation_position(const struct audio_stream_out *stream,
1512                                    uint64_t *frames, struct timespec *timestamp)
1513 {
1514     struct stream_out *out = (struct stream_out *)stream;
1515     int ret = -1;
1516     unsigned long dsp_frames;
1517 
1518     pthread_mutex_lock(&out->lock);
1519 
1520     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1521         if (out->compr != NULL) {
1522             compress_get_tstamp(out->compr, &dsp_frames,
1523                     &out->sample_rate);
1524             ALOGVV("%s rendered frames %ld sample_rate %d",
1525                    __func__, dsp_frames, out->sample_rate);
1526             *frames = dsp_frames;
1527             ret = 0;
1528             /* this is the best we can do */
1529             clock_gettime(CLOCK_MONOTONIC, timestamp);
1530         }
1531     } else {
1532         if (out->pcm) {
1533             size_t avail;
1534             if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1535                 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
1536                 int64_t signed_frames = out->written - kernel_buffer_size + avail;
1537                 // This adjustment accounts for buffering after app processor.
1538                 // It is based on estimated DSP latency per use case, rather than exact.
1539                 signed_frames -=
1540                     (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1541 
1542                 // It would be unusual for this value to be negative, but check just in case ...
1543                 if (signed_frames >= 0) {
1544                     *frames = signed_frames;
1545                     ret = 0;
1546                 }
1547             }
1548         }
1549     }
1550 
1551     pthread_mutex_unlock(&out->lock);
1552 
1553     return ret;
1554 }
1555 
out_set_callback(struct audio_stream_out * stream,stream_callback_t callback,void * cookie)1556 static int out_set_callback(struct audio_stream_out *stream,
1557             stream_callback_t callback, void *cookie)
1558 {
1559     struct stream_out *out = (struct stream_out *)stream;
1560 
1561     ALOGV("%s", __func__);
1562     pthread_mutex_lock(&out->lock);
1563     out->offload_callback = callback;
1564     out->offload_cookie = cookie;
1565     pthread_mutex_unlock(&out->lock);
1566     return 0;
1567 }
1568 
out_pause(struct audio_stream_out * stream)1569 static int out_pause(struct audio_stream_out* stream)
1570 {
1571     struct stream_out *out = (struct stream_out *)stream;
1572     int status = -ENOSYS;
1573     ALOGV("%s", __func__);
1574     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1575         pthread_mutex_lock(&out->lock);
1576         if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1577             status = compress_pause(out->compr);
1578             out->offload_state = OFFLOAD_STATE_PAUSED;
1579         }
1580         pthread_mutex_unlock(&out->lock);
1581     }
1582     return status;
1583 }
1584 
out_resume(struct audio_stream_out * stream)1585 static int out_resume(struct audio_stream_out* stream)
1586 {
1587     struct stream_out *out = (struct stream_out *)stream;
1588     int status = -ENOSYS;
1589     ALOGV("%s", __func__);
1590     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1591         status = 0;
1592         pthread_mutex_lock(&out->lock);
1593         if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1594             status = compress_resume(out->compr);
1595             out->offload_state = OFFLOAD_STATE_PLAYING;
1596         }
1597         pthread_mutex_unlock(&out->lock);
1598     }
1599     return status;
1600 }
1601 
out_drain(struct audio_stream_out * stream,audio_drain_type_t type)1602 static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1603 {
1604     struct stream_out *out = (struct stream_out *)stream;
1605     int status = -ENOSYS;
1606     ALOGV("%s", __func__);
1607     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1608         pthread_mutex_lock(&out->lock);
1609         if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1610             status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1611         else
1612             status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1613         pthread_mutex_unlock(&out->lock);
1614     }
1615     return status;
1616 }
1617 
out_flush(struct audio_stream_out * stream)1618 static int out_flush(struct audio_stream_out* stream)
1619 {
1620     struct stream_out *out = (struct stream_out *)stream;
1621     ALOGV("%s", __func__);
1622     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1623         pthread_mutex_lock(&out->lock);
1624         stop_compressed_output_l(out);
1625         pthread_mutex_unlock(&out->lock);
1626         return 0;
1627     }
1628     return -ENOSYS;
1629 }
1630 
1631 /** audio_stream_in implementation **/
in_get_sample_rate(const struct audio_stream * stream)1632 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1633 {
1634     struct stream_in *in = (struct stream_in *)stream;
1635 
1636     return in->config.rate;
1637 }
1638 
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)1639 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1640 {
1641     return -ENOSYS;
1642 }
1643 
in_get_buffer_size(const struct audio_stream * stream)1644 static size_t in_get_buffer_size(const struct audio_stream *stream)
1645 {
1646     struct stream_in *in = (struct stream_in *)stream;
1647 
1648     return in->config.period_size * audio_stream_frame_size(stream);
1649 }
1650 
in_get_channels(const struct audio_stream * stream)1651 static uint32_t in_get_channels(const struct audio_stream *stream)
1652 {
1653     struct stream_in *in = (struct stream_in *)stream;
1654 
1655     return in->channel_mask;
1656 }
1657 
in_get_format(const struct audio_stream * stream)1658 static audio_format_t in_get_format(const struct audio_stream *stream)
1659 {
1660     return AUDIO_FORMAT_PCM_16_BIT;
1661 }
1662 
in_set_format(struct audio_stream * stream,audio_format_t format)1663 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1664 {
1665     return -ENOSYS;
1666 }
1667 
in_standby(struct audio_stream * stream)1668 static int in_standby(struct audio_stream *stream)
1669 {
1670     struct stream_in *in = (struct stream_in *)stream;
1671     struct audio_device *adev = in->dev;
1672     int status = 0;
1673     ALOGV("%s: enter", __func__);
1674     pthread_mutex_lock(&in->lock);
1675     if (!in->standby) {
1676         pthread_mutex_lock(&adev->lock);
1677         in->standby = true;
1678         if (in->pcm) {
1679             pcm_close(in->pcm);
1680             in->pcm = NULL;
1681         }
1682         status = stop_input_stream(in);
1683         pthread_mutex_unlock(&adev->lock);
1684     }
1685     pthread_mutex_unlock(&in->lock);
1686     ALOGV("%s: exit:  status(%d)", __func__, status);
1687     return status;
1688 }
1689 
in_dump(const struct audio_stream * stream,int fd)1690 static int in_dump(const struct audio_stream *stream, int fd)
1691 {
1692     return 0;
1693 }
1694 
in_set_parameters(struct audio_stream * stream,const char * kvpairs)1695 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1696 {
1697     struct stream_in *in = (struct stream_in *)stream;
1698     struct audio_device *adev = in->dev;
1699     struct str_parms *parms;
1700     char *str;
1701     char value[32];
1702     int ret, val = 0;
1703 
1704     ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
1705     parms = str_parms_create_str(kvpairs);
1706 
1707     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1708 
1709     pthread_mutex_lock(&in->lock);
1710     pthread_mutex_lock(&adev->lock);
1711     if (ret >= 0) {
1712         val = atoi(value);
1713         /* no audio source uses val == 0 */
1714         if ((in->source != val) && (val != 0)) {
1715             in->source = val;
1716         }
1717     }
1718 
1719     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1720     if (ret >= 0) {
1721         val = atoi(value);
1722         if ((in->device != val) && (val != 0)) {
1723             in->device = val;
1724             /* If recording is in progress, change the tx device to new device */
1725             if (!in->standby)
1726                 ret = select_devices(adev, in->usecase);
1727         }
1728     }
1729 
1730     pthread_mutex_unlock(&adev->lock);
1731     pthread_mutex_unlock(&in->lock);
1732 
1733     str_parms_destroy(parms);
1734     ALOGV("%s: exit: status(%d)", __func__, ret);
1735     return ret;
1736 }
1737 
in_get_parameters(const struct audio_stream * stream,const char * keys)1738 static char* in_get_parameters(const struct audio_stream *stream,
1739                                const char *keys)
1740 {
1741     return strdup("");
1742 }
1743 
in_set_gain(struct audio_stream_in * stream,float gain)1744 static int in_set_gain(struct audio_stream_in *stream, float gain)
1745 {
1746     return 0;
1747 }
1748 
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)1749 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1750                        size_t bytes)
1751 {
1752     struct stream_in *in = (struct stream_in *)stream;
1753     struct audio_device *adev = in->dev;
1754     int i, ret = -1;
1755 
1756     pthread_mutex_lock(&in->lock);
1757     if (in->standby) {
1758         pthread_mutex_lock(&adev->lock);
1759         ret = start_input_stream(in);
1760         pthread_mutex_unlock(&adev->lock);
1761         if (ret != 0) {
1762             goto exit;
1763         }
1764         in->standby = 0;
1765     }
1766 
1767     if (in->pcm) {
1768         ret = pcm_read(in->pcm, buffer, bytes);
1769     }
1770 
1771     /*
1772      * Instead of writing zeroes here, we could trust the hardware
1773      * to always provide zeroes when muted.
1774      */
1775     if (ret == 0 && adev->mic_mute)
1776         memset(buffer, 0, bytes);
1777 
1778 exit:
1779     pthread_mutex_unlock(&in->lock);
1780 
1781     if (ret != 0) {
1782         in_standby(&in->stream.common);
1783         ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1784         usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1785                in_get_sample_rate(&in->stream.common));
1786     }
1787     return bytes;
1788 }
1789 
in_get_input_frames_lost(struct audio_stream_in * stream)1790 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1791 {
1792     return 0;
1793 }
1794 
add_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect,bool enable)1795 static int add_remove_audio_effect(const struct audio_stream *stream,
1796                                    effect_handle_t effect,
1797                                    bool enable)
1798 {
1799     struct stream_in *in = (struct stream_in *)stream;
1800     int status = 0;
1801     effect_descriptor_t desc;
1802 
1803     status = (*effect)->get_descriptor(effect, &desc);
1804     if (status != 0)
1805         return status;
1806 
1807     pthread_mutex_lock(&in->lock);
1808     pthread_mutex_lock(&in->dev->lock);
1809     if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1810             in->enable_aec != enable &&
1811             (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1812         in->enable_aec = enable;
1813         if (!in->standby)
1814             select_devices(in->dev, in->usecase);
1815     }
1816     pthread_mutex_unlock(&in->dev->lock);
1817     pthread_mutex_unlock(&in->lock);
1818 
1819     return 0;
1820 }
1821 
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1822 static int in_add_audio_effect(const struct audio_stream *stream,
1823                                effect_handle_t effect)
1824 {
1825     ALOGV("%s: effect %p", __func__, effect);
1826     return add_remove_audio_effect(stream, effect, true);
1827 }
1828 
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1829 static int in_remove_audio_effect(const struct audio_stream *stream,
1830                                   effect_handle_t effect)
1831 {
1832     ALOGV("%s: effect %p", __func__, effect);
1833     return add_remove_audio_effect(stream, effect, false);
1834 }
1835 
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)1836 static int adev_open_output_stream(struct audio_hw_device *dev,
1837                                    audio_io_handle_t handle,
1838                                    audio_devices_t devices,
1839                                    audio_output_flags_t flags,
1840                                    struct audio_config *config,
1841                                    struct audio_stream_out **stream_out)
1842 {
1843     struct audio_device *adev = (struct audio_device *)dev;
1844     struct stream_out *out;
1845     int i, ret;
1846 
1847     ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
1848           __func__, config->sample_rate, config->channel_mask, devices, flags);
1849     *stream_out = NULL;
1850     out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1851 
1852     if (devices == AUDIO_DEVICE_NONE)
1853         devices = AUDIO_DEVICE_OUT_SPEAKER;
1854 
1855     out->flags = flags;
1856     out->devices = devices;
1857     out->dev = adev;
1858     out->format = config->format;
1859     out->sample_rate = config->sample_rate;
1860     out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1861     out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1862     out->handle = handle;
1863 
1864     /* Init use case and pcm_config */
1865     if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1866             !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
1867         out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1868         pthread_mutex_lock(&adev->lock);
1869         ret = read_hdmi_channel_masks(out);
1870         pthread_mutex_unlock(&adev->lock);
1871         if (ret != 0)
1872             goto error_open;
1873 
1874         if (config->sample_rate == 0)
1875             config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1876         if (config->channel_mask == 0)
1877             config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1878 
1879         out->channel_mask = config->channel_mask;
1880         out->sample_rate = config->sample_rate;
1881         out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1882         out->config = pcm_config_hdmi_multi;
1883         out->config.rate = config->sample_rate;
1884         out->config.channels = popcount(out->channel_mask);
1885         out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1886     } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1887         out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1888         out->config = pcm_config_deep_buffer;
1889         out->sample_rate = out->config.rate;
1890     } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1891         if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1892             config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1893             ALOGE("%s: Unsupported Offload information", __func__);
1894             ret = -EINVAL;
1895             goto error_open;
1896         }
1897         if (!is_supported_format(config->offload_info.format)) {
1898             ALOGE("%s: Unsupported audio format", __func__);
1899             ret = -EINVAL;
1900             goto error_open;
1901         }
1902 
1903         out->compr_config.codec = (struct snd_codec *)
1904                                     calloc(1, sizeof(struct snd_codec));
1905 
1906         out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1907         if (config->offload_info.channel_mask)
1908             out->channel_mask = config->offload_info.channel_mask;
1909         else if (config->channel_mask)
1910             out->channel_mask = config->channel_mask;
1911         out->format = config->offload_info.format;
1912         out->sample_rate = config->offload_info.sample_rate;
1913 
1914         out->stream.set_callback = out_set_callback;
1915         out->stream.pause = out_pause;
1916         out->stream.resume = out_resume;
1917         out->stream.drain = out_drain;
1918         out->stream.flush = out_flush;
1919 
1920         out->compr_config.codec->id =
1921                 get_snd_codec_id(config->offload_info.format);
1922         out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1923         out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1924         out->compr_config.codec->sample_rate =
1925                     compress_get_alsa_rate(config->offload_info.sample_rate);
1926         out->compr_config.codec->bit_rate =
1927                     config->offload_info.bit_rate;
1928         out->compr_config.codec->ch_in =
1929                     popcount(config->channel_mask);
1930         out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1931 
1932         if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1933             out->non_blocking = 1;
1934 
1935         out->send_new_metadata = 1;
1936         create_offload_callback_thread(out);
1937         ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1938                 __func__, config->offload_info.version,
1939                 config->offload_info.bit_rate);
1940     } else {
1941         out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1942         out->config = pcm_config_low_latency;
1943         out->sample_rate = out->config.rate;
1944     }
1945 
1946     if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1947         if(adev->primary_output == NULL)
1948             adev->primary_output = out;
1949         else {
1950             ALOGE("%s: Primary output is already opened", __func__);
1951             ret = -EEXIST;
1952             goto error_open;
1953         }
1954     }
1955 
1956     /* Check if this usecase is already existing */
1957     pthread_mutex_lock(&adev->lock);
1958     if (get_usecase_from_list(adev, out->usecase) != NULL) {
1959         ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
1960         pthread_mutex_unlock(&adev->lock);
1961         ret = -EEXIST;
1962         goto error_open;
1963     }
1964     pthread_mutex_unlock(&adev->lock);
1965 
1966     out->stream.common.get_sample_rate = out_get_sample_rate;
1967     out->stream.common.set_sample_rate = out_set_sample_rate;
1968     out->stream.common.get_buffer_size = out_get_buffer_size;
1969     out->stream.common.get_channels = out_get_channels;
1970     out->stream.common.get_format = out_get_format;
1971     out->stream.common.set_format = out_set_format;
1972     out->stream.common.standby = out_standby;
1973     out->stream.common.dump = out_dump;
1974     out->stream.common.set_parameters = out_set_parameters;
1975     out->stream.common.get_parameters = out_get_parameters;
1976     out->stream.common.add_audio_effect = out_add_audio_effect;
1977     out->stream.common.remove_audio_effect = out_remove_audio_effect;
1978     out->stream.get_latency = out_get_latency;
1979     out->stream.set_volume = out_set_volume;
1980     out->stream.write = out_write;
1981     out->stream.get_render_position = out_get_render_position;
1982     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1983     out->stream.get_presentation_position = out_get_presentation_position;
1984 
1985     out->standby = 1;
1986     /* out->muted = false; by calloc() */
1987     /* out->written = 0; by calloc() */
1988 
1989     pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
1990     pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
1991 
1992     config->format = out->stream.common.get_format(&out->stream.common);
1993     config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1994     config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1995 
1996     *stream_out = &out->stream;
1997     ALOGV("%s: exit", __func__);
1998     return 0;
1999 
2000 error_open:
2001     free(out);
2002     *stream_out = NULL;
2003     ALOGD("%s: exit: ret %d", __func__, ret);
2004     return ret;
2005 }
2006 
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)2007 static void adev_close_output_stream(struct audio_hw_device *dev,
2008                                      struct audio_stream_out *stream)
2009 {
2010     struct stream_out *out = (struct stream_out *)stream;
2011     struct audio_device *adev = out->dev;
2012 
2013     ALOGV("%s: enter", __func__);
2014     out_standby(&stream->common);
2015     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2016         destroy_offload_callback_thread(out);
2017 
2018         if (out->compr_config.codec != NULL)
2019             free(out->compr_config.codec);
2020     }
2021     pthread_cond_destroy(&out->cond);
2022     pthread_mutex_destroy(&out->lock);
2023     free(stream);
2024     ALOGV("%s: exit", __func__);
2025 }
2026 
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)2027 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2028 {
2029     struct audio_device *adev = (struct audio_device *)dev;
2030     struct str_parms *parms;
2031     char *str;
2032     char value[32];
2033     int val;
2034     int ret;
2035 
2036     ALOGV("%s: enter: %s", __func__, kvpairs);
2037 
2038     parms = str_parms_create_str(kvpairs);
2039     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
2040     if (ret >= 0) {
2041         int tty_mode;
2042 
2043         if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
2044             tty_mode = TTY_MODE_OFF;
2045         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
2046             tty_mode = TTY_MODE_VCO;
2047         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
2048             tty_mode = TTY_MODE_HCO;
2049         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
2050             tty_mode = TTY_MODE_FULL;
2051         else
2052             return -EINVAL;
2053 
2054         pthread_mutex_lock(&adev->lock);
2055         if (tty_mode != adev->tty_mode) {
2056             adev->tty_mode = tty_mode;
2057             adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
2058             if (adev->in_call)
2059                 select_devices(adev, USECASE_VOICE_CALL);
2060         }
2061         pthread_mutex_unlock(&adev->lock);
2062     }
2063 
2064     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2065     if (ret >= 0) {
2066         /* When set to false, HAL should disable EC and NS
2067          * But it is currently not supported.
2068          */
2069         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2070             adev->bluetooth_nrec = true;
2071         else
2072             adev->bluetooth_nrec = false;
2073     }
2074 
2075     ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2076     if (ret >= 0) {
2077         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2078             adev->screen_off = false;
2079         else
2080             adev->screen_off = true;
2081     }
2082 
2083     ret = str_parms_get_int(parms, "rotation", &val);
2084     if (ret >= 0) {
2085         bool reverse_speakers = false;
2086         switch(val) {
2087         // FIXME: note that the code below assumes that the speakers are in the correct placement
2088         //   relative to the user when the device is rotated 90deg from its default rotation. This
2089         //   assumption is device-specific, not platform-specific like this code.
2090         case 270:
2091             reverse_speakers = true;
2092             break;
2093         case 0:
2094         case 90:
2095         case 180:
2096             break;
2097         default:
2098             ALOGE("%s: unexpected rotation of %d", __func__, val);
2099         }
2100         pthread_mutex_lock(&adev->lock);
2101         if (adev->speaker_lr_swap != reverse_speakers) {
2102             adev->speaker_lr_swap = reverse_speakers;
2103             // only update the selected device if there is active pcm playback
2104             struct audio_usecase *usecase;
2105             struct listnode *node;
2106             list_for_each(node, &adev->usecase_list) {
2107                 usecase = node_to_item(node, struct audio_usecase, list);
2108                 if (usecase->type == PCM_PLAYBACK) {
2109                     select_devices(adev, usecase->id);
2110                     break;
2111                 }
2112             }
2113         }
2114         pthread_mutex_unlock(&adev->lock);
2115     }
2116 
2117     str_parms_destroy(parms);
2118     ALOGV("%s: exit with code(%d)", __func__, ret);
2119     return ret;
2120 }
2121 
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)2122 static char* adev_get_parameters(const struct audio_hw_device *dev,
2123                                  const char *keys)
2124 {
2125     return strdup("");
2126 }
2127 
adev_init_check(const struct audio_hw_device * dev)2128 static int adev_init_check(const struct audio_hw_device *dev)
2129 {
2130     return 0;
2131 }
2132 
2133 /* always called with adev lock held */
set_voice_volume_l(struct audio_device * adev,float volume)2134 static int set_voice_volume_l(struct audio_device *adev, float volume)
2135 {
2136     int vol, err = 0;
2137 
2138     if (adev->mode == AUDIO_MODE_IN_CALL) {
2139         if (volume < 0.0) {
2140             volume = 0.0;
2141         } else if (volume > 1.0) {
2142             volume = 1.0;
2143         }
2144 
2145         vol = lrint(volume * 100.0);
2146 
2147         // Voice volume levels from android are mapped to driver volume levels as follows.
2148         // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
2149         // So adjust the volume to get the correct volume index in driver
2150         vol = 100 - vol;
2151 
2152         err = platform_set_voice_volume(adev->platform, vol);
2153     }
2154     return err;
2155 }
2156 
adev_set_voice_volume(struct audio_hw_device * dev,float volume)2157 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2158 {
2159     int ret;
2160     struct audio_device *adev = (struct audio_device *)dev;
2161     pthread_mutex_lock(&adev->lock);
2162     /* cache volume */
2163     adev->voice_volume = volume;
2164     ret = set_voice_volume_l(adev, adev->voice_volume);
2165     pthread_mutex_unlock(&adev->lock);
2166     return ret;
2167 }
2168 
adev_set_master_volume(struct audio_hw_device * dev,float volume)2169 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2170 {
2171     return -ENOSYS;
2172 }
2173 
adev_get_master_volume(struct audio_hw_device * dev,float * volume)2174 static int adev_get_master_volume(struct audio_hw_device *dev,
2175                                   float *volume)
2176 {
2177     return -ENOSYS;
2178 }
2179 
adev_set_master_mute(struct audio_hw_device * dev,bool muted)2180 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2181 {
2182     return -ENOSYS;
2183 }
2184 
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)2185 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2186 {
2187     return -ENOSYS;
2188 }
2189 
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)2190 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2191 {
2192     struct audio_device *adev = (struct audio_device *)dev;
2193 
2194     pthread_mutex_lock(&adev->lock);
2195     if (adev->mode != mode) {
2196         adev->mode = mode;
2197     }
2198     pthread_mutex_unlock(&adev->lock);
2199     return 0;
2200 }
2201 
adev_set_mic_mute(struct audio_hw_device * dev,bool state)2202 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2203 {
2204     struct audio_device *adev = (struct audio_device *)dev;
2205     int err = 0;
2206 
2207     pthread_mutex_lock(&adev->lock);
2208     adev->mic_mute = state;
2209 
2210     err = platform_set_mic_mute(adev->platform, state);
2211     pthread_mutex_unlock(&adev->lock);
2212     return err;
2213 }
2214 
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)2215 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2216 {
2217     struct audio_device *adev = (struct audio_device *)dev;
2218 
2219     *state = adev->mic_mute;
2220 
2221     return 0;
2222 }
2223 
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)2224 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2225                                          const struct audio_config *config)
2226 {
2227     int channel_count = popcount(config->channel_mask);
2228 
2229     return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2230 }
2231 
adev_open_input_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,struct audio_stream_in ** stream_in)2232 static int adev_open_input_stream(struct audio_hw_device *dev,
2233                                   audio_io_handle_t handle,
2234                                   audio_devices_t devices,
2235                                   struct audio_config *config,
2236                                   struct audio_stream_in **stream_in)
2237 {
2238     struct audio_device *adev = (struct audio_device *)dev;
2239     struct stream_in *in;
2240     int ret, buffer_size, frame_size;
2241     int channel_count = popcount(config->channel_mask);
2242 
2243     ALOGV("%s: enter", __func__);
2244     *stream_in = NULL;
2245     if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2246         return -EINVAL;
2247 
2248     in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2249 
2250     in->stream.common.get_sample_rate = in_get_sample_rate;
2251     in->stream.common.set_sample_rate = in_set_sample_rate;
2252     in->stream.common.get_buffer_size = in_get_buffer_size;
2253     in->stream.common.get_channels = in_get_channels;
2254     in->stream.common.get_format = in_get_format;
2255     in->stream.common.set_format = in_set_format;
2256     in->stream.common.standby = in_standby;
2257     in->stream.common.dump = in_dump;
2258     in->stream.common.set_parameters = in_set_parameters;
2259     in->stream.common.get_parameters = in_get_parameters;
2260     in->stream.common.add_audio_effect = in_add_audio_effect;
2261     in->stream.common.remove_audio_effect = in_remove_audio_effect;
2262     in->stream.set_gain = in_set_gain;
2263     in->stream.read = in_read;
2264     in->stream.get_input_frames_lost = in_get_input_frames_lost;
2265 
2266     in->device = devices;
2267     in->source = AUDIO_SOURCE_DEFAULT;
2268     in->dev = adev;
2269     in->standby = 1;
2270     in->channel_mask = config->channel_mask;
2271 
2272     /* Update config params with the requested sample rate and channels */
2273     in->usecase = USECASE_AUDIO_RECORD;
2274     in->config = pcm_config_audio_capture;
2275     in->config.channels = channel_count;
2276     in->config.rate = config->sample_rate;
2277 
2278     frame_size = audio_stream_frame_size((struct audio_stream *)in);
2279     buffer_size = get_input_buffer_size(config->sample_rate,
2280                                         config->format,
2281                                         channel_count);
2282     in->config.period_size = buffer_size / frame_size;
2283 
2284     *stream_in = &in->stream;
2285     ALOGV("%s: exit", __func__);
2286     return 0;
2287 
2288 err_open:
2289     free(in);
2290     *stream_in = NULL;
2291     return ret;
2292 }
2293 
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * stream)2294 static void adev_close_input_stream(struct audio_hw_device *dev,
2295                                     struct audio_stream_in *stream)
2296 {
2297     ALOGV("%s", __func__);
2298 
2299     in_standby(&stream->common);
2300     free(stream);
2301 
2302     return;
2303 }
2304 
adev_dump(const audio_hw_device_t * device,int fd)2305 static int adev_dump(const audio_hw_device_t *device, int fd)
2306 {
2307     return 0;
2308 }
2309 
adev_close(hw_device_t * device)2310 static int adev_close(hw_device_t *device)
2311 {
2312     struct audio_device *adev = (struct audio_device *)device;
2313     audio_route_free(adev->audio_route);
2314     free(adev->snd_dev_ref_cnt);
2315     platform_deinit(adev->platform);
2316     free(device);
2317     return 0;
2318 }
2319 
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)2320 static int adev_open(const hw_module_t *module, const char *name,
2321                      hw_device_t **device)
2322 {
2323     struct audio_device *adev;
2324     int i, ret;
2325 
2326     ALOGD("%s: enter", __func__);
2327     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2328 
2329     adev = calloc(1, sizeof(struct audio_device));
2330 
2331     adev->device.common.tag = HARDWARE_DEVICE_TAG;
2332     adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2333     adev->device.common.module = (struct hw_module_t *)module;
2334     adev->device.common.close = adev_close;
2335 
2336     adev->device.init_check = adev_init_check;
2337     adev->device.set_voice_volume = adev_set_voice_volume;
2338     adev->device.set_master_volume = adev_set_master_volume;
2339     adev->device.get_master_volume = adev_get_master_volume;
2340     adev->device.set_master_mute = adev_set_master_mute;
2341     adev->device.get_master_mute = adev_get_master_mute;
2342     adev->device.set_mode = adev_set_mode;
2343     adev->device.set_mic_mute = adev_set_mic_mute;
2344     adev->device.get_mic_mute = adev_get_mic_mute;
2345     adev->device.set_parameters = adev_set_parameters;
2346     adev->device.get_parameters = adev_get_parameters;
2347     adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2348     adev->device.open_output_stream = adev_open_output_stream;
2349     adev->device.close_output_stream = adev_close_output_stream;
2350     adev->device.open_input_stream = adev_open_input_stream;
2351     adev->device.close_input_stream = adev_close_input_stream;
2352     adev->device.dump = adev_dump;
2353 
2354     /* Set the default route before the PCM stream is opened */
2355     pthread_mutex_lock(&adev->lock);
2356     adev->mode = AUDIO_MODE_NORMAL;
2357     adev->active_input = NULL;
2358     adev->primary_output = NULL;
2359     adev->out_device = AUDIO_DEVICE_NONE;
2360     adev->voice_call_rx = NULL;
2361     adev->voice_call_tx = NULL;
2362     adev->voice_volume = 1.0f;
2363     adev->tty_mode = TTY_MODE_OFF;
2364     adev->bluetooth_nrec = true;
2365     adev->in_call = false;
2366     adev->acdb_settings = TTY_MODE_OFF;
2367     /* adev->cur_hdmi_channels = 0;  by calloc() */
2368     adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
2369     list_init(&adev->usecase_list);
2370     pthread_mutex_unlock(&adev->lock);
2371 
2372     /* Loads platform specific libraries dynamically */
2373     adev->platform = platform_init(adev);
2374     if (!adev->platform) {
2375         free(adev->snd_dev_ref_cnt);
2376         free(adev);
2377         ALOGE("%s: Failed to init platform data, aborting.", __func__);
2378         *device = NULL;
2379         return -EINVAL;
2380     }
2381 
2382     if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2383         adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2384         if (adev->visualizer_lib == NULL) {
2385             ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2386         } else {
2387             ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2388             adev->visualizer_start_output =
2389                         (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2390                                                         "visualizer_hal_start_output");
2391             adev->visualizer_stop_output =
2392                         (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2393                                                         "visualizer_hal_stop_output");
2394         }
2395     }
2396 
2397     *device = &adev->device.common;
2398 
2399     ALOGV("%s: exit", __func__);
2400     return 0;
2401 }
2402 
2403 static struct hw_module_methods_t hal_module_methods = {
2404     .open = adev_open,
2405 };
2406 
2407 struct audio_module HAL_MODULE_INFO_SYM = {
2408     .common = {
2409         .tag = HARDWARE_MODULE_TAG,
2410         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2411         .hal_api_version = HARDWARE_HAL_API_VERSION,
2412         .id = AUDIO_HARDWARE_MODULE_ID,
2413         .name = "QCOM Audio HAL",
2414         .author = "Code Aurora Forum",
2415         .methods = &hal_module_methods,
2416     },
2417 };
2418