• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <sys/time.h>
25 #include <fcntl.h>
26 
27 #include <cutils/log.h>
28 #include <cutils/properties.h>
29 #include <cutils/str_parms.h>
30 
31 #include <hardware/audio.h>
32 #include <hardware/hardware.h>
33 
34 #include <linux/videodev2.h>
35 #include <videodev2_exynos_media.h>
36 
37 #include <system/audio.h>
38 
39 #include <tinyalsa/asoundlib.h>
40 
41 #include <audio_utils/resampler.h>
42 #include <audio_route/audio_route.h>
43 
44 #include <BubbleLevel.h>
45 
46 #include <eS305VoiceProcessing.h>
47 
48 #define PCM_CARD 0
49 #define PCM_CARD_SPDIF 1
50 #define PCM_TOTAL 2
51 
52 #define PCM_DEVICE 0
53 #define PCM_DEVICE_DEEP 1
54 #define PCM_DEVICE_VOICE 2
55 #define PCM_DEVICE_SCO 3
56 
57 #define MIXER_CARD 0
58 
59 /* duration in ms of volume ramp applied when starting capture to remove plop */
60 #define CAPTURE_START_RAMP_MS 100
61 
62 /* default sampling for HDMI multichannel output */
63 #define HDMI_MULTI_DEFAULT_SAMPLING_RATE  44100
64 /* maximum number of channel mask configurations supported. Currently the primary
65  * output only supports 1 (stereo) and the multi channel HDMI output 2 (5.1 and 7.1) */
66 #define MAX_SUPPORTED_CHANNEL_MASKS 2
67 
68 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
69 
70 struct pcm_config pcm_config = {
71     .channels = 2,
72     .rate = 44100,
73     .period_size = 256,
74     .period_count = 2,
75     .format = PCM_FORMAT_S16_LE,
76 };
77 
78 struct pcm_config pcm_config_in = {
79     .channels = 2,
80     .rate = 44100,
81     .period_size = 1024,
82     .period_count = 2,
83     .format = PCM_FORMAT_S16_LE,
84 };
85 
86 struct pcm_config pcm_config_in_low_latency = {
87     .channels = 2,
88     .rate = 44100,
89     .period_size = 256,
90     .period_count = 2,
91     .format = PCM_FORMAT_S16_LE,
92 };
93 
94 struct pcm_config pcm_config_sco = {
95     .channels = 1,
96     .rate = 8000,
97     .period_size = 128,
98     .period_count = 2,
99     .format = PCM_FORMAT_S16_LE,
100 };
101 
102 struct pcm_config pcm_config_deep = {
103     .channels = 2,
104     .rate = 44100,
105     /* FIXME This is an arbitrary number, may change.
106      * Dynamic configuration based on screen on/off is not implemented;
107      * let's see what power consumption is first to see if necessary.
108      */
109     .period_size = 8192,
110     .period_count = 2,
111     .format = PCM_FORMAT_S16_LE,
112 };
113 
114 struct pcm_config pcm_config_hdmi_multi = {
115     .channels = 6, /* changed when the stream is opened */
116     .rate = HDMI_MULTI_DEFAULT_SAMPLING_RATE,
117     .period_size = 1024,
118     .period_count = 4,
119     .format = PCM_FORMAT_S16_LE,
120 };
121 
122 enum output_type {
123     OUTPUT_DEEP_BUF,      // deep PCM buffers output stream
124     OUTPUT_LOW_LATENCY,   // low latency output stream
125     OUTPUT_HDMI,          // HDMI multi channel
126     OUTPUT_TOTAL
127 };
128 
129 struct audio_device {
130     struct audio_hw_device hw_device;
131 
132     pthread_mutex_t lock; /* see note below on mutex acquisition order */
133     audio_devices_t out_device; /* "or" of stream_out.device for all active output streams */
134     audio_devices_t in_device;
135     bool mic_mute;
136     struct audio_route *ar;
137     audio_source_t input_source;
138     int cur_route_id;     /* current route ID: combination of input source
139                            * and output device IDs */
140     struct pcm *pcm_voice_out;
141     struct pcm *pcm_sco_out;
142     struct pcm *pcm_voice_in;
143     struct pcm *pcm_sco_in;
144     int es305_preset;
145     int es305_new_mode;
146     int es305_mode;
147     int hdmi_drv_fd;    /* either an fd >= 0 or -1 */
148     struct bubble_level *bubble_level;
149     audio_channel_mask_t in_channel_mask;
150     unsigned int sco_on_count;
151 
152     struct stream_out *outputs[OUTPUT_TOTAL];
153     pthread_mutex_t lock_outputs; /* see note below on mutex acquisition order */
154 };
155 
156 struct stream_out {
157     struct audio_stream_out stream;
158 
159     pthread_mutex_t lock; /* see note below on mutex acquisition order */
160     struct pcm *pcm[PCM_TOTAL];
161     struct pcm_config config;
162     unsigned int pcm_device;
163     bool standby; /* true if all PCMs are inactive */
164     audio_devices_t device;
165     /* FIXME: when HDMI multichannel output is active, other outputs must be disabled as
166      * HDMI and WM1811 share the same I2S. This means that notifications and other sounds are
167      * silent when watching a 5.1 movie. */
168     bool disabled;
169     audio_channel_mask_t channel_mask;
170     /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
171     audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
172     bool muted;
173     uint64_t written; /* total frames written, not cleared when entering standby */
174 
175     struct audio_device *dev;
176 };
177 
178 struct stream_in {
179     struct audio_stream_in stream;
180 
181     pthread_mutex_t lock; /* see note below on mutex acquisition order */
182     struct pcm *pcm;
183     bool standby;
184 
185     unsigned int requested_rate;
186     struct resampler_itfe *resampler;
187     struct resampler_buffer_provider buf_provider;
188     int16_t *buffer;
189     size_t frames_in;
190     int read_status;
191     audio_source_t input_source;
192     audio_io_handle_t io_handle;
193     audio_devices_t device;
194     uint16_t ramp_vol;
195     uint16_t ramp_step;
196     size_t  ramp_frames;
197     audio_channel_mask_t channel_mask;
198     audio_input_flags_t flags;
199     struct pcm_config *config;
200 
201     struct audio_device *dev;
202 };
203 
204 #define STRING_TO_ENUM(string) { #string, string }
205 
206 struct string_to_enum {
207     const char *name;
208     uint32_t value;
209 };
210 
211 const struct string_to_enum out_channels_name_to_enum_table[] = {
212     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
213     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
214     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
215 };
216 
217 enum {
218     OUT_DEVICE_SPEAKER,
219     OUT_DEVICE_HEADSET,
220     OUT_DEVICE_HEADPHONES,
221     OUT_DEVICE_BT_SCO,
222     OUT_DEVICE_SPEAKER_AND_HEADSET,
223     OUT_DEVICE_TAB_SIZE,           /* number of rows in route_configs[][] */
224     OUT_DEVICE_NONE,
225     OUT_DEVICE_CNT
226 };
227 
228 enum {
229     IN_SOURCE_MIC,
230     IN_SOURCE_CAMCORDER,
231     IN_SOURCE_VOICE_RECOGNITION,
232     IN_SOURCE_VOICE_COMMUNICATION,
233     IN_SOURCE_TAB_SIZE,            /* number of lines in route_configs[][] */
234     IN_SOURCE_NONE,
235     IN_SOURCE_CNT
236 };
237 
238 enum {
239     ES305_MODE_DEFAULT,
240     ES305_MODE_LEVEL,
241     ES305_NUM_MODES,
242 };
243 
get_output_device_id(audio_devices_t device)244 int get_output_device_id(audio_devices_t device)
245 {
246     if (device == AUDIO_DEVICE_NONE)
247         return OUT_DEVICE_NONE;
248 
249     if (popcount(device) == 2) {
250         if ((device == (AUDIO_DEVICE_OUT_SPEAKER |
251                        AUDIO_DEVICE_OUT_WIRED_HEADSET)) ||
252                 (device == (AUDIO_DEVICE_OUT_SPEAKER |
253                         AUDIO_DEVICE_OUT_WIRED_HEADPHONE)))
254             return OUT_DEVICE_SPEAKER_AND_HEADSET;
255         else
256             return OUT_DEVICE_NONE;
257     }
258 
259     if (popcount(device) != 1)
260         return OUT_DEVICE_NONE;
261 
262     switch (device) {
263     case AUDIO_DEVICE_OUT_SPEAKER:
264         return OUT_DEVICE_SPEAKER;
265     case AUDIO_DEVICE_OUT_WIRED_HEADSET:
266         return OUT_DEVICE_HEADSET;
267     case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
268         return OUT_DEVICE_HEADPHONES;
269     case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
270     case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
271     case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
272         return OUT_DEVICE_BT_SCO;
273     default:
274         return OUT_DEVICE_NONE;
275     }
276 }
277 
get_input_source_id(audio_source_t source)278 int get_input_source_id(audio_source_t source)
279 {
280     switch (source) {
281     case AUDIO_SOURCE_DEFAULT:
282         return IN_SOURCE_NONE;
283     case AUDIO_SOURCE_MIC:
284         return IN_SOURCE_MIC;
285     case AUDIO_SOURCE_CAMCORDER:
286         return IN_SOURCE_CAMCORDER;
287     case AUDIO_SOURCE_VOICE_RECOGNITION:
288         return IN_SOURCE_VOICE_RECOGNITION;
289     case AUDIO_SOURCE_VOICE_COMMUNICATION:
290         return IN_SOURCE_VOICE_COMMUNICATION;
291     default:
292         return IN_SOURCE_NONE;
293     }
294 }
295 
296 struct route_config {
297     const char * const output_route;
298     const char * const input_route;
299     int es305_preset[ES305_NUM_MODES]; // es305 preset for this route.
300                                        // -1 means es305 bypass
301 };
302 
303 const struct route_config media_speaker = {
304     "media-speaker",
305     "media-main-mic",
306     { ES305_PRESET_OFF,
307       ES305_PRESET_OFF }
308 };
309 
310 const struct route_config media_headphones = {
311     "media-headphones",
312     "media-main-mic",
313     { ES305_PRESET_OFF,
314       ES305_PRESET_OFF }
315 };
316 
317 const struct route_config media_headset = {
318     "media-headphones",
319     "media-headset-mic",
320     { ES305_PRESET_OFF,
321       ES305_PRESET_OFF }
322 };
323 
324 const struct route_config camcorder_speaker = {
325     "media-speaker",
326     "media-second-mic",
327     { ES305_PRESET_CAMCORDER,
328       ES305_PRESET_CAMCORDER }
329 };
330 
331 const struct route_config camcorder_headphones = {
332     "media-headphones",
333     "media-second-mic",
334     { ES305_PRESET_CAMCORDER,
335       ES305_PRESET_CAMCORDER }
336 };
337 
338 const struct route_config voice_rec_speaker = {
339     "voice-rec-speaker",
340     "voice-rec-main-mic",
341     { ES305_PRESET_ASRA_HANDHELD,
342       ES305_PRESET_ASRA_DESKTOP }
343 };
344 
345 const struct route_config voice_rec_headphones = {
346     "voice-rec-headphones",
347     "voice-rec-main-mic",
348     { ES305_PRESET_ASRA_HANDHELD,
349       ES305_PRESET_ASRA_DESKTOP }
350 };
351 
352 const struct route_config voice_rec_headset = {
353     "voice-rec-headphones",
354     "voice-rec-headset-mic",
355     { ES305_PRESET_ASRA_HEADSET,
356       ES305_PRESET_ASRA_HEADSET }
357 };
358 
359 const struct route_config communication_speaker = {
360     "communication-speaker",
361     "communication-main-mic",
362     { ES305_PRESET_VOIP_HANDHELD,
363       ES305_PRESET_VOIP_DESKTOP }
364 };
365 
366 const struct route_config communication_headphones = {
367     "communication-headphones",
368     "communication-main-mic",
369     { ES305_PRESET_VOIP_HEADPHONES,
370       ES305_PRESET_VOIP_HP_DESKTOP}
371 };
372 
373 const struct route_config communication_headset = {
374     "communication-headphones",
375     "communication-headset-mic",
376     { ES305_PRESET_VOIP_HEADSET,
377       ES305_PRESET_VOIP_HEADSET }
378 };
379 
380 const struct route_config speaker_and_headphones = {
381     "speaker-and-headphones",
382     "main-mic",
383     { ES305_PRESET_CURRENT,
384       ES305_PRESET_CURRENT }
385 };
386 
387 const struct route_config bluetooth_sco = {
388     "bt-sco-headset",
389     "bt-sco-mic",
390     { ES305_PRESET_OFF,
391       ES305_PRESET_OFF }
392 };
393 
394 const struct route_config * const route_configs[IN_SOURCE_TAB_SIZE]
395                                                [OUT_DEVICE_TAB_SIZE] = {
396     {   /* IN_SOURCE_MIC */
397         &media_speaker,             /* OUT_DEVICE_SPEAKER */
398         &media_headset,             /* OUT_DEVICE_HEADSET */
399         &media_headphones,          /* OUT_DEVICE_HEADPHONES */
400         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
401         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
402     },
403     {   /* IN_SOURCE_CAMCORDER */
404         &camcorder_speaker,         /* OUT_DEVICE_SPEAKER */
405         &camcorder_headphones,      /* OUT_DEVICE_HEADSET */
406         &camcorder_headphones,      /* OUT_DEVICE_HEADPHONES */
407         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
408         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
409     },
410     {   /* IN_SOURCE_VOICE_RECOGNITION */
411         &voice_rec_speaker,         /* OUT_DEVICE_SPEAKER */
412         &voice_rec_headset,         /* OUT_DEVICE_HEADSET */
413         &voice_rec_headphones,      /* OUT_DEVICE_HEADPHONES */
414         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
415         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
416     },
417     {   /* IN_SOURCE_VOICE_COMMUNICATION */
418         &communication_speaker,     /* OUT_DEVICE_SPEAKER */
419         &communication_headset,     /* OUT_DEVICE_HEADSET */
420         &communication_headphones,  /* OUT_DEVICE_HEADPHONES */
421         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
422         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
423     }
424 };
425 
426 static void do_out_standby(struct stream_out *out);
427 
428 /**
429  * NOTE: when multiple mutexes have to be acquired, always respect the following order:
430  *   lock_outputs for hw device outputs list only
431  *   in stream
432  *   out stream(s) in enum output_type order
433  *   hw device
434  * TODO investigate whether we ever actually take both in stream and out stream
435  */
436 
437 /* Helper functions */
438 
439 /* must be called with hw device mutex locked */
open_hdmi_driver(struct audio_device * adev)440 static int open_hdmi_driver(struct audio_device *adev)
441 {
442     if (adev->hdmi_drv_fd < 0) {
443         adev->hdmi_drv_fd = open("/dev/video16", O_RDWR);
444         if (adev->hdmi_drv_fd < 0)
445             ALOGE("%s cannot open video16 (%d)", __func__, adev->hdmi_drv_fd);
446     }
447     return adev->hdmi_drv_fd;
448 }
449 
450 /* must be called with hw device mutex locked */
enable_hdmi_audio(struct audio_device * adev,int enable)451 static int enable_hdmi_audio(struct audio_device *adev, int enable)
452 {
453     int ret;
454     struct v4l2_control ctrl;
455 
456     ret = open_hdmi_driver(adev);
457     if (ret < 0)
458         return ret;
459 
460     ctrl.id = V4L2_CID_TV_ENABLE_HDMI_AUDIO;
461     ctrl.value = !!enable;
462     ret = ioctl(adev->hdmi_drv_fd, VIDIOC_S_CTRL, &ctrl);
463 
464     if (ret < 0)
465         ALOGE("V4L2_CID_TV_ENABLE_HDMI_AUDIO ioctl error (%d)", errno);
466 
467     return ret;
468 }
469 
470 /* must be called with hw device mutex locked
471  * Called from adev_open_output_stream with no stream lock,
472  * but this is OK because stream is not yet visible
473  */
read_hdmi_channel_masks(struct audio_device * adev,struct stream_out * out)474 static int read_hdmi_channel_masks(struct audio_device *adev, struct stream_out *out) {
475     int ret;
476     struct v4l2_control ctrl;
477 
478     ret = open_hdmi_driver(adev);
479     if (ret < 0)
480         return ret;
481 
482     ctrl.id = V4L2_CID_TV_MAX_AUDIO_CHANNELS;
483     ret = ioctl(adev->hdmi_drv_fd, VIDIOC_G_CTRL, &ctrl);
484     if (ret < 0) {
485         ALOGE("V4L2_CID_TV_MAX_AUDIO_CHANNELS ioctl error (%d)", errno);
486         return ret;
487     }
488 
489     ALOGV("%s ioctl %d got %d max channels", __func__, ret, ctrl.value);
490 
491     if (ctrl.value != 6 && ctrl.value != 8)
492         return -ENOSYS;
493 
494     out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
495     if (ctrl.value == 8)
496         out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
497 
498     return ret;
499 }
500 
501 /* must be called with hw device mutex locked */
set_hdmi_channels(struct audio_device * adev,int channels)502 static int set_hdmi_channels(struct audio_device *adev, int channels) {
503     int ret;
504     struct v4l2_control ctrl;
505 
506     ret = open_hdmi_driver(adev);
507     if (ret < 0)
508         return ret;
509 
510     ctrl.id = V4L2_CID_TV_SET_NUM_CHANNELS;
511     ctrl.value = channels;
512     ret = ioctl(adev->hdmi_drv_fd, VIDIOC_S_CTRL, &ctrl);
513     if (ret < 0)
514         ALOGE("V4L2_CID_TV_SET_NUM_CHANNELS ioctl error (%d)", errno);
515 
516     return ret;
517 }
518 
519 /* must be called with hw device mutex locked */
select_devices(struct audio_device * adev)520 static void select_devices(struct audio_device *adev)
521 {
522     int output_device_id = get_output_device_id(adev->out_device);
523     int input_source_id = get_input_source_id(adev->input_source);
524     const char *output_route = NULL;
525     const char *input_route = NULL;
526     int new_route_id;
527     int new_es305_preset = -1;
528 
529     audio_route_reset(adev->ar);
530 
531     enable_hdmi_audio(adev, adev->out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL);
532 
533     new_route_id = (1 << (input_source_id + OUT_DEVICE_CNT)) + (1 << output_device_id);
534     if ((new_route_id == adev->cur_route_id) && (adev->es305_mode == adev->es305_new_mode))
535         return;
536     adev->cur_route_id = new_route_id;
537     adev->es305_mode = adev->es305_new_mode;
538 
539     if (input_source_id != IN_SOURCE_NONE) {
540         if (output_device_id != OUT_DEVICE_NONE) {
541             input_route =
542                     route_configs[input_source_id][output_device_id]->input_route;
543             output_route =
544                     route_configs[input_source_id][output_device_id]->output_route;
545             new_es305_preset =
546                 route_configs[input_source_id][output_device_id]->es305_preset[adev->es305_mode];
547         } else {
548             switch (adev->in_device) {
549             case AUDIO_DEVICE_IN_WIRED_HEADSET & ~AUDIO_DEVICE_BIT_IN:
550                 output_device_id = OUT_DEVICE_HEADSET;
551                 break;
552             case AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & ~AUDIO_DEVICE_BIT_IN:
553                 output_device_id = OUT_DEVICE_BT_SCO;
554                 break;
555             default:
556                 output_device_id = OUT_DEVICE_SPEAKER;
557                 break;
558             }
559             input_route =
560                     route_configs[input_source_id][output_device_id]->input_route;
561             new_es305_preset =
562                 route_configs[input_source_id][output_device_id]->es305_preset[adev->es305_mode];
563         }
564         // disable noise suppression when capturing front and back mic for voice recognition
565         if ((adev->input_source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
566                 (adev->in_channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK))
567             new_es305_preset = -1;
568     } else {
569         if (output_device_id != OUT_DEVICE_NONE) {
570             output_route =
571                     route_configs[IN_SOURCE_MIC][output_device_id]->output_route;
572         }
573     }
574 
575     ALOGV("select_devices() devices %#x input src %d output route %s input route %s",
576           adev->out_device, adev->input_source,
577           output_route ? output_route : "none",
578           input_route ? input_route : "none");
579 
580     if (output_route)
581         audio_route_apply_path(adev->ar, output_route);
582     if (input_route)
583         audio_route_apply_path(adev->ar, input_route);
584 
585     if ((new_es305_preset != ES305_PRESET_CURRENT) &&
586             (new_es305_preset != adev->es305_preset)) {
587         ALOGV("  select_devices() changing es305 preset from %d to %d",
588               adev->es305_preset, new_es305_preset);
589         if (eS305_UsePreset(new_es305_preset) == 0) {
590             adev->es305_preset = new_es305_preset;
591         }
592     }
593 
594     audio_route_update_mixer(adev->ar);
595 }
596 
597 /* must be called with hw device mutex unlocked */
bubblelevel_callback(bool is_level,void * user_data)598 void bubblelevel_callback(bool is_level, void *user_data)
599 {
600     struct audio_device *adev = (struct audio_device *)user_data;
601     int es305_mode;
602 
603     if (is_level)
604         es305_mode = ES305_MODE_LEVEL;
605     else
606         es305_mode = ES305_MODE_DEFAULT;
607 
608     pthread_mutex_lock(&adev->lock);
609     if (es305_mode != adev->es305_mode) {
610         adev->es305_new_mode = es305_mode;
611         select_devices(adev);
612         ALOGV("bubblelevel_callback is_level %d es305_mode %d", is_level, es305_mode);
613     }
614     pthread_mutex_unlock(&adev->lock);
615 }
616 
617 /* must be called with hw device mutex locked */
get_bubblelevel(struct audio_device * adev)618 bool get_bubblelevel(struct audio_device *adev)
619 {
620     if (!adev->bubble_level) {
621         adev->bubble_level = bubble_level_create();
622         if (adev->bubble_level)
623             adev->bubble_level->set_callback(adev->bubble_level, bubblelevel_callback, adev);
624     }
625     return (adev->bubble_level != NULL);
626 }
627 
628 /* must be called with hw device outputs list, all out streams, and hw device mutexes locked */
force_non_hdmi_out_standby(struct audio_device * adev)629 static void force_non_hdmi_out_standby(struct audio_device *adev)
630 {
631     enum output_type type;
632     struct stream_out *out;
633 
634     for (type = 0; type < OUTPUT_TOTAL; ++type) {
635         out = adev->outputs[type];
636         if (type == OUTPUT_HDMI || !out)
637             continue;
638         /* This will never recurse more than 2 levels deep. */
639         do_out_standby(out);
640     }
641 }
642 
643 /* must be called with the hw device mutex locked, OK to hold other mutexes */
start_bt_sco(struct audio_device * adev)644 static void start_bt_sco(struct audio_device *adev) {
645     if (adev->sco_on_count++ > 0)
646         return;
647 
648     adev->pcm_voice_out = pcm_open(PCM_CARD, PCM_DEVICE_VOICE, PCM_OUT | PCM_MONOTONIC,
649                               &pcm_config_sco);
650     if (adev->pcm_voice_out && !pcm_is_ready(adev->pcm_voice_out)) {
651         ALOGE("pcm_open(VOICE_OUT) failed: %s", pcm_get_error(adev->pcm_voice_out));
652         goto err_voice_out;
653     }
654     adev->pcm_sco_out = pcm_open(PCM_CARD, PCM_DEVICE_SCO, PCM_OUT | PCM_MONOTONIC,
655                             &pcm_config_sco);
656     if (adev->pcm_sco_out && !pcm_is_ready(adev->pcm_sco_out)) {
657         ALOGE("pcm_open(SCO_OUT) failed: %s", pcm_get_error(adev->pcm_sco_out));
658         goto err_sco_out;
659     }
660     adev->pcm_voice_in = pcm_open(PCM_CARD, PCM_DEVICE_VOICE, PCM_IN,
661                                  &pcm_config_sco);
662     if (adev->pcm_voice_in && !pcm_is_ready(adev->pcm_voice_in)) {
663         ALOGE("pcm_open(VOICE_IN) failed: %s", pcm_get_error(adev->pcm_voice_in));
664         goto err_voice_in;
665     }
666     adev->pcm_sco_in = pcm_open(PCM_CARD, PCM_DEVICE_SCO, PCM_IN,
667                                &pcm_config_sco);
668     if (adev->pcm_sco_in && !pcm_is_ready(adev->pcm_sco_in)) {
669         ALOGE("pcm_open(SCO_IN) failed: %s", pcm_get_error(adev->pcm_sco_in));
670         goto err_sco_in;
671     }
672 
673     pcm_start(adev->pcm_voice_out);
674     pcm_start(adev->pcm_sco_out);
675     pcm_start(adev->pcm_voice_in);
676     pcm_start(adev->pcm_sco_in);
677 
678     return;
679 
680 err_sco_in:
681     pcm_close(adev->pcm_sco_in);
682 err_voice_in:
683     pcm_close(adev->pcm_voice_in);
684 err_sco_out:
685     pcm_close(adev->pcm_sco_out);
686 err_voice_out:
687     pcm_close(adev->pcm_voice_out);
688 }
689 
690 /* must be called with the hw device mutex locked, OK to hold other mutexes */
stop_bt_sco(struct audio_device * adev)691 static void stop_bt_sco(struct audio_device *adev) {
692     if (adev->sco_on_count == 0 || --adev->sco_on_count > 0)
693         return;
694 
695     pcm_stop(adev->pcm_voice_out);
696     pcm_stop(adev->pcm_sco_out);
697     pcm_stop(adev->pcm_voice_in);
698     pcm_stop(adev->pcm_sco_in);
699 
700     pcm_close(adev->pcm_voice_out);
701     pcm_close(adev->pcm_sco_out);
702     pcm_close(adev->pcm_voice_in);
703     pcm_close(adev->pcm_sco_in);
704 }
705 
706 /* must be called with hw device outputs list, output stream, and hw device mutexes locked */
start_output_stream(struct stream_out * out)707 static int start_output_stream(struct stream_out *out)
708 {
709     struct audio_device *adev = out->dev;
710     int type;
711 
712     if (out == adev->outputs[OUTPUT_HDMI]) {
713         force_non_hdmi_out_standby(adev);
714     } else if (adev->outputs[OUTPUT_HDMI] && !adev->outputs[OUTPUT_HDMI]->standby) {
715         out->disabled = true;
716         return 0;
717     }
718 
719     out->disabled = false;
720 
721     if (out->device & (AUDIO_DEVICE_OUT_SPEAKER |
722                        AUDIO_DEVICE_OUT_WIRED_HEADSET |
723                        AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
724                        AUDIO_DEVICE_OUT_AUX_DIGITAL |
725                        AUDIO_DEVICE_OUT_ALL_SCO)) {
726 
727         out->pcm[PCM_CARD] = pcm_open(PCM_CARD, out->pcm_device,
728                                       PCM_OUT | PCM_MONOTONIC, &out->config);
729 
730         if (out->pcm[PCM_CARD] && !pcm_is_ready(out->pcm[PCM_CARD])) {
731             ALOGE("pcm_open(PCM_CARD) failed: %s",
732                   pcm_get_error(out->pcm[PCM_CARD]));
733             pcm_close(out->pcm[PCM_CARD]);
734             return -ENOMEM;
735         }
736     }
737 
738     if (out->device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
739         out->pcm[PCM_CARD_SPDIF] = pcm_open(PCM_CARD_SPDIF, out->pcm_device,
740                                             PCM_OUT | PCM_MONOTONIC, &out->config);
741 
742         if (out->pcm[PCM_CARD_SPDIF] &&
743                 !pcm_is_ready(out->pcm[PCM_CARD_SPDIF])) {
744             ALOGE("pcm_open(PCM_CARD_SPDIF) failed: %s",
745                   pcm_get_error(out->pcm[PCM_CARD_SPDIF]));
746             pcm_close(out->pcm[PCM_CARD_SPDIF]);
747             return -ENOMEM;
748         }
749     }
750 
751     adev->out_device |= out->device;
752     select_devices(adev);
753 
754     if (out->device & AUDIO_DEVICE_OUT_ALL_SCO)
755         start_bt_sco(adev);
756 
757     if (out->device & AUDIO_DEVICE_OUT_AUX_DIGITAL)
758         set_hdmi_channels(adev, out->config.channels);
759 
760     /* anticipate level measurement in case we start capture later */
761     if (get_bubblelevel(adev))
762         adev->bubble_level->poll_once(adev->bubble_level);
763 
764     return 0;
765 }
766 
767 /* must be called with input stream and hw device mutexes locked */
start_input_stream(struct stream_in * in)768 static int start_input_stream(struct stream_in *in)
769 {
770     struct audio_device *adev = in->dev;
771 
772     in->pcm = pcm_open(PCM_CARD, PCM_DEVICE, PCM_IN, in->config);
773 
774     if (in->pcm && !pcm_is_ready(in->pcm)) {
775         ALOGE("pcm_open() failed: %s", pcm_get_error(in->pcm));
776         pcm_close(in->pcm);
777         return -ENOMEM;
778     }
779 
780     /* if no supported sample rate is available, use the resampler */
781     if (in->resampler)
782         in->resampler->reset(in->resampler);
783 
784     in->frames_in = 0;
785     adev->input_source = in->input_source;
786     adev->in_device = in->device;
787     adev->in_channel_mask = in->channel_mask;
788 
789     eS305_SetActiveIoHandle(in->io_handle);
790     select_devices(adev);
791 
792     if (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)
793         start_bt_sco(adev);
794 
795     /* initialize volume ramp */
796     in->ramp_frames = (CAPTURE_START_RAMP_MS * in->requested_rate) / 1000;
797     in->ramp_step = (uint16_t)(USHRT_MAX / in->ramp_frames);
798     in->ramp_vol = 0;;
799 
800     if (get_bubblelevel(adev)) {
801         adev->bubble_level->set_poll_interval(adev->bubble_level, BL_POLL_INTERVAL_MIN_SEC);
802         adev->bubble_level->start_polling(adev->bubble_level);
803     }
804 
805     return 0;
806 }
807 
get_input_buffer_size(unsigned int sample_rate,audio_format_t format,unsigned int channel_count,bool is_low_latency)808 static size_t get_input_buffer_size(unsigned int sample_rate,
809                                     audio_format_t format,
810                                     unsigned int channel_count,
811                                     bool is_low_latency)
812 {
813     const struct pcm_config *config = is_low_latency ?
814             &pcm_config_in_low_latency : &pcm_config_in;
815     size_t size;
816 
817     /*
818      * take resampling into account and return the closest majoring
819      * multiple of 16 frames, as audioflinger expects audio buffers to
820      * be a multiple of 16 frames
821      */
822     size = (config->period_size * sample_rate) / config->rate;
823     size = ((size + 15) / 16) * 16;
824 
825     return size * channel_count * audio_bytes_per_sample(format);
826 }
827 
get_next_buffer(struct resampler_buffer_provider * buffer_provider,struct resampler_buffer * buffer)828 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
829                                    struct resampler_buffer* buffer)
830 {
831     struct stream_in *in;
832     size_t i;
833 
834     if (buffer_provider == NULL || buffer == NULL)
835         return -EINVAL;
836 
837     in = (struct stream_in *)((char *)buffer_provider -
838                                    offsetof(struct stream_in, buf_provider));
839 
840     if (in->pcm == NULL) {
841         buffer->raw = NULL;
842         buffer->frame_count = 0;
843         in->read_status = -ENODEV;
844         return -ENODEV;
845     }
846 
847     if (in->frames_in == 0) {
848         in->read_status = pcm_read(in->pcm,
849                                    (void*)in->buffer,
850                                    pcm_frames_to_bytes(in->pcm, in->config->period_size));
851         if (in->read_status != 0) {
852             ALOGE("get_next_buffer() pcm_read error %d", in->read_status);
853             buffer->raw = NULL;
854             buffer->frame_count = 0;
855             return in->read_status;
856         }
857 
858         in->frames_in = in->config->period_size;
859 
860         /* Do stereo to mono conversion in place by discarding right channel */
861         if (in->channel_mask == AUDIO_CHANNEL_IN_MONO)
862             for (i = 1; i < in->frames_in; i++)
863                 in->buffer[i] = in->buffer[i * 2];
864     }
865 
866     buffer->frame_count = (buffer->frame_count > in->frames_in) ?
867                                 in->frames_in : buffer->frame_count;
868     buffer->i16 = in->buffer +
869             (in->config->period_size - in->frames_in) *
870                 audio_channel_count_from_in_mask(in->channel_mask);
871 
872     return in->read_status;
873 
874 }
875 
release_buffer(struct resampler_buffer_provider * buffer_provider,struct resampler_buffer * buffer)876 static void release_buffer(struct resampler_buffer_provider *buffer_provider,
877                                   struct resampler_buffer* buffer)
878 {
879     struct stream_in *in;
880 
881     if (buffer_provider == NULL || buffer == NULL)
882         return;
883 
884     in = (struct stream_in *)((char *)buffer_provider -
885                                    offsetof(struct stream_in, buf_provider));
886 
887     in->frames_in -= buffer->frame_count;
888 }
889 
890 /* read_frames() reads frames from kernel driver, down samples to capture rate
891  * if necessary and output the number of frames requested to the buffer specified */
read_frames(struct stream_in * in,void * buffer,ssize_t frames)892 static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames)
893 {
894     ssize_t frames_wr = 0;
895     size_t frame_size = audio_stream_in_frame_size(&in->stream);
896 
897     while (frames_wr < frames) {
898         size_t frames_rd = frames - frames_wr;
899         if (in->resampler != NULL) {
900             in->resampler->resample_from_provider(in->resampler,
901                     (int16_t *)((char *)buffer +
902                             frames_wr * frame_size),
903                     &frames_rd);
904         } else {
905             struct resampler_buffer buf = {
906                     { raw : NULL, },
907                     frame_count : frames_rd,
908             };
909             get_next_buffer(&in->buf_provider, &buf);
910             if (buf.raw != NULL) {
911                 memcpy((char *)buffer +
912                            frames_wr * frame_size,
913                         buf.raw,
914                         buf.frame_count * frame_size);
915                 frames_rd = buf.frame_count;
916             }
917             release_buffer(&in->buf_provider, &buf);
918         }
919         /* in->read_status is updated by getNextBuffer() also called by
920          * in->resampler->resample_from_provider() */
921         if (in->read_status != 0)
922             return in->read_status;
923 
924         frames_wr += frames_rd;
925     }
926     return frames_wr;
927 }
928 
929 /* API functions */
930 
out_get_sample_rate(const struct audio_stream * stream)931 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
932 {
933     struct stream_out *out = (struct stream_out *)stream;
934 
935     return out->config.rate;
936 }
937 
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)938 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
939 {
940     return -ENOSYS;
941 }
942 
out_get_buffer_size(const struct audio_stream * stream)943 static size_t out_get_buffer_size(const struct audio_stream *stream)
944 {
945     struct stream_out *out = (struct stream_out *)stream;
946 
947     return out->config.period_size *
948             audio_stream_out_frame_size((const struct audio_stream_out *)stream);
949 }
950 
out_get_channels(const struct audio_stream * stream)951 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
952 {
953     struct stream_out *out = (struct stream_out *)stream;
954 
955     return out->channel_mask;
956 }
957 
out_get_format(const struct audio_stream * stream)958 static audio_format_t out_get_format(const struct audio_stream *stream)
959 {
960     return AUDIO_FORMAT_PCM_16_BIT;
961 }
962 
out_set_format(struct audio_stream * stream,audio_format_t format)963 static int out_set_format(struct audio_stream *stream, audio_format_t format)
964 {
965     return -ENOSYS;
966 }
967 
968 /* Return the set of output devices associated with active streams
969  * other than out.  Assumes out is non-NULL and out->dev is locked.
970  */
output_devices(struct stream_out * out)971 static audio_devices_t output_devices(struct stream_out *out)
972 {
973     struct audio_device *dev = out->dev;
974     enum output_type type;
975     audio_devices_t devices = AUDIO_DEVICE_NONE;
976 
977     for (type = 0; type < OUTPUT_TOTAL; ++type) {
978         struct stream_out *other = dev->outputs[type];
979         if (other && (other != out) && !other->standby) {
980             // TODO no longer accurate
981             /* safe to access other stream without a mutex,
982              * because we hold the dev lock,
983              * which prevents the other stream from being closed
984              */
985             devices |= other->device;
986         }
987     }
988 
989     return devices;
990 }
991 
992 /* must be called with hw device outputs list, all out streams, and hw device mutex locked */
do_out_standby(struct stream_out * out)993 static void do_out_standby(struct stream_out *out)
994 {
995     struct audio_device *adev = out->dev;
996     int i;
997 
998     if (!out->standby) {
999         for (i = 0; i < PCM_TOTAL; i++) {
1000             if (out->pcm[i]) {
1001                 pcm_close(out->pcm[i]);
1002                 out->pcm[i] = NULL;
1003             }
1004         }
1005         out->standby = true;
1006 
1007         if (out == adev->outputs[OUTPUT_HDMI]) {
1008             /* force standby on low latency output stream so that it can reuse HDMI driver if
1009              * necessary when restarted */
1010             force_non_hdmi_out_standby(adev);
1011         }
1012 
1013         if (out->device & AUDIO_DEVICE_OUT_ALL_SCO)
1014             stop_bt_sco(adev);
1015 
1016         /* re-calculate the set of active devices from other streams */
1017         adev->out_device = output_devices(out);
1018 
1019         /* Skip resetting the mixer if no output device is active */
1020         if (adev->out_device)
1021             select_devices(adev);
1022     }
1023 }
1024 
1025 /* lock outputs list, all output streams, and device */
lock_all_outputs(struct audio_device * adev)1026 static void lock_all_outputs(struct audio_device *adev)
1027 {
1028     enum output_type type;
1029     pthread_mutex_lock(&adev->lock_outputs);
1030     for (type = 0; type < OUTPUT_TOTAL; ++type) {
1031         struct stream_out *out = adev->outputs[type];
1032         if (out)
1033             pthread_mutex_lock(&out->lock);
1034     }
1035     pthread_mutex_lock(&adev->lock);
1036 }
1037 
1038 /* unlock device, all output streams (except specified stream), and outputs list */
unlock_all_outputs(struct audio_device * adev,struct stream_out * except)1039 static void unlock_all_outputs(struct audio_device *adev, struct stream_out *except)
1040 {
1041     /* unlock order is irrelevant, but for cleanliness we unlock in reverse order */
1042     pthread_mutex_unlock(&adev->lock);
1043     enum output_type type = OUTPUT_TOTAL;
1044     do {
1045         struct stream_out *out = adev->outputs[--type];
1046         if (out && out != except)
1047             pthread_mutex_unlock(&out->lock);
1048     } while (type != (enum output_type) 0);
1049     pthread_mutex_unlock(&adev->lock_outputs);
1050 }
1051 
out_standby(struct audio_stream * stream)1052 static int out_standby(struct audio_stream *stream)
1053 {
1054     struct stream_out *out = (struct stream_out *)stream;
1055     struct audio_device *adev = out->dev;
1056 
1057     lock_all_outputs(adev);
1058 
1059     do_out_standby(out);
1060 
1061     unlock_all_outputs(adev, NULL);
1062 
1063     return 0;
1064 }
1065 
out_dump(const struct audio_stream * stream,int fd)1066 static int out_dump(const struct audio_stream *stream, int fd)
1067 {
1068     return 0;
1069 }
1070 
out_set_parameters(struct audio_stream * stream,const char * kvpairs)1071 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1072 {
1073     struct stream_out *out = (struct stream_out *)stream;
1074     struct audio_device *adev = out->dev;
1075     struct str_parms *parms;
1076     char value[32];
1077     int ret;
1078     unsigned int val;
1079 
1080     parms = str_parms_create_str(kvpairs);
1081 
1082     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
1083                             value, sizeof(value));
1084     lock_all_outputs(adev);
1085     if (ret >= 0) {
1086         val = atoi(value);
1087         if ((out->device != val) && (val != 0)) {
1088             /* Force standby if moving to/from SPDIF or if the output
1089              * device changes when in SPDIF mode */
1090             if (((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^
1091                  (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) ||
1092                 (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) {
1093                 do_out_standby(out);
1094             }
1095 
1096             /* force output standby to start or stop SCO pcm stream if needed */
1097             if ((val & AUDIO_DEVICE_OUT_ALL_SCO) ^
1098                     (out->device & AUDIO_DEVICE_OUT_ALL_SCO)) {
1099                 do_out_standby(out);
1100             }
1101 
1102             if (!out->standby && (out == adev->outputs[OUTPUT_HDMI] ||
1103                     !adev->outputs[OUTPUT_HDMI] ||
1104                     adev->outputs[OUTPUT_HDMI]->standby)) {
1105                 adev->out_device = output_devices(out) | val;
1106                 select_devices(adev);
1107             }
1108             out->device = val;
1109         }
1110     }
1111     unlock_all_outputs(adev, NULL);
1112 
1113     str_parms_destroy(parms);
1114     return ret;
1115 }
1116 
out_get_parameters(const struct audio_stream * stream,const char * keys)1117 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
1118 {
1119     struct stream_out *out = (struct stream_out *)stream;
1120     struct str_parms *query = str_parms_create_str(keys);
1121     char *str;
1122     char value[256];
1123     struct str_parms *reply = str_parms_create();
1124     size_t i, j;
1125     int ret;
1126     bool first = true;
1127 
1128     ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1129     if (ret >= 0) {
1130         value[0] = '\0';
1131         i = 0;
1132         /* the last entry in supported_channel_masks[] is always 0 */
1133         while (out->supported_channel_masks[i] != 0) {
1134             for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1135                 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1136                     if (!first) {
1137                         strcat(value, "|");
1138                     }
1139                     strcat(value, out_channels_name_to_enum_table[j].name);
1140                     first = false;
1141                     break;
1142                 }
1143             }
1144             i++;
1145         }
1146         str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1147         str = str_parms_to_str(reply);
1148     } else {
1149         str = strdup(keys);
1150     }
1151 
1152     str_parms_destroy(query);
1153     str_parms_destroy(reply);
1154     return str;
1155 }
1156 
out_get_latency(const struct audio_stream_out * stream)1157 static uint32_t out_get_latency(const struct audio_stream_out *stream)
1158 {
1159     struct stream_out *out = (struct stream_out *)stream;
1160 
1161     return (out->config.period_size * out->config.period_count * 1000) /
1162             out->config.rate;
1163 }
1164 
out_set_volume(struct audio_stream_out * stream,float left,float right)1165 static int out_set_volume(struct audio_stream_out *stream, float left,
1166                           float right)
1167 {
1168     struct stream_out *out = (struct stream_out *)stream;
1169     struct audio_device *adev = out->dev;
1170 
1171     /* The mutex lock is not needed, because the client
1172      * is not allowed to close the stream concurrently with this API
1173      *  pthread_mutex_lock(&adev->lock_outputs);
1174      */
1175     bool is_HDMI = out == adev->outputs[OUTPUT_HDMI];
1176     /*  pthread_mutex_unlock(&adev->lock_outputs); */
1177     if (is_HDMI) {
1178         /* only take left channel into account: the API is for stereo anyway */
1179         out->muted = (left == 0.0f);
1180         return 0;
1181     }
1182     return -ENOSYS;
1183 }
1184 
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)1185 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
1186                          size_t bytes)
1187 {
1188     int ret = 0;
1189     struct stream_out *out = (struct stream_out *)stream;
1190     struct audio_device *adev = out->dev;
1191     int i;
1192 
1193     /* FIXME This comment is no longer correct
1194      * acquiring hw device mutex systematically is useful if a low
1195      * priority thread is waiting on the output stream mutex - e.g.
1196      * executing out_set_parameters() while holding the hw device
1197      * mutex
1198      */
1199     pthread_mutex_lock(&out->lock);
1200     if (out->standby) {
1201         pthread_mutex_unlock(&out->lock);
1202         lock_all_outputs(adev);
1203         if (!out->standby) {
1204             unlock_all_outputs(adev, out);
1205             goto false_alarm;
1206         }
1207         ret = start_output_stream(out);
1208         if (ret < 0) {
1209             unlock_all_outputs(adev, NULL);
1210             goto final_exit;
1211         }
1212         out->standby = false;
1213         unlock_all_outputs(adev, out);
1214     }
1215 false_alarm:
1216 
1217     if (out->disabled) {
1218         ret = -EPIPE;
1219         goto exit;
1220     }
1221 
1222     if (out->muted)
1223         memset((void *)buffer, 0, bytes);
1224 
1225     /* Write to all active PCMs */
1226     for (i = 0; i < PCM_TOTAL; i++)
1227         if (out->pcm[i]) {
1228             ret = pcm_write(out->pcm[i], (void *)buffer, bytes);
1229             if (ret != 0)
1230                 break;
1231         }
1232     if (ret == 0)
1233         out->written += bytes / (out->config.channels * sizeof(short));
1234 
1235 exit:
1236     pthread_mutex_unlock(&out->lock);
1237 final_exit:
1238 
1239     if (ret != 0) {
1240         usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
1241                out_get_sample_rate(&stream->common));
1242     }
1243 
1244     return bytes;
1245 }
1246 
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)1247 static int out_get_render_position(const struct audio_stream_out *stream,
1248                                    uint32_t *dsp_frames)
1249 {
1250     return -EINVAL;
1251 }
1252 
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1253 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1254 {
1255     return 0;
1256 }
1257 
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1258 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1259 {
1260     return 0;
1261 }
1262 
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)1263 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1264                                         int64_t *timestamp)
1265 {
1266     return -EINVAL;
1267 }
1268 
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)1269 static int out_get_presentation_position(const struct audio_stream_out *stream,
1270                                    uint64_t *frames, struct timespec *timestamp)
1271 {
1272     struct stream_out *out = (struct stream_out *)stream;
1273     int ret = -1;
1274 
1275     pthread_mutex_lock(&out->lock);
1276 
1277     int i;
1278     // There is a question how to implement this correctly when there is more than one PCM stream.
1279     // We are just interested in the frames pending for playback in the kernel buffer here,
1280     // not the total played since start.  The current behavior should be safe because the
1281     // cases where both cards are active are marginal.
1282     for (i = 0; i < PCM_TOTAL; i++)
1283         if (out->pcm[i]) {
1284             size_t avail;
1285             if (pcm_get_htimestamp(out->pcm[i], &avail, timestamp) == 0) {
1286                 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
1287                 // FIXME This calculation is incorrect if there is buffering after app processor
1288                 int64_t signed_frames = out->written - kernel_buffer_size + avail;
1289                 // It would be unusual for this value to be negative, but check just in case ...
1290                 if (signed_frames >= 0) {
1291                     *frames = signed_frames;
1292                     ret = 0;
1293                 }
1294                 break;
1295             }
1296         }
1297 
1298     pthread_mutex_unlock(&out->lock);
1299 
1300     return ret;
1301 }
1302 
1303 /** audio_stream_in implementation **/
in_get_sample_rate(const struct audio_stream * stream)1304 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1305 {
1306     struct stream_in *in = (struct stream_in *)stream;
1307 
1308     return in->requested_rate;
1309 }
1310 
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)1311 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1312 {
1313     return 0;
1314 }
1315 
in_get_channels(const struct audio_stream * stream)1316 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
1317 {
1318     struct stream_in *in = (struct stream_in *)stream;
1319 
1320     return in->channel_mask;
1321 }
1322 
1323 
in_get_buffer_size(const struct audio_stream * stream)1324 static size_t in_get_buffer_size(const struct audio_stream *stream)
1325 {
1326     struct stream_in *in = (struct stream_in *)stream;
1327 
1328     return get_input_buffer_size(in->requested_rate,
1329                                  AUDIO_FORMAT_PCM_16_BIT,
1330                                  audio_channel_count_from_in_mask(in_get_channels(stream)),
1331                                  (in->flags & AUDIO_INPUT_FLAG_FAST) != 0);
1332 }
1333 
in_get_format(const struct audio_stream * stream)1334 static audio_format_t in_get_format(const struct audio_stream *stream)
1335 {
1336     return AUDIO_FORMAT_PCM_16_BIT;
1337 }
1338 
in_set_format(struct audio_stream * stream,audio_format_t format)1339 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1340 {
1341     return -ENOSYS;
1342 }
1343 
1344 /* must be called with in stream and hw device mutex locked */
do_in_standby(struct stream_in * in)1345 static void do_in_standby(struct stream_in *in)
1346 {
1347     struct audio_device *adev = in->dev;
1348 
1349     if (!in->standby) {
1350         pcm_close(in->pcm);
1351         in->pcm = NULL;
1352 
1353         if (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)
1354             stop_bt_sco(adev);
1355 
1356         in->dev->input_source = AUDIO_SOURCE_DEFAULT;
1357         in->dev->in_device = AUDIO_DEVICE_NONE;
1358         in->dev->in_channel_mask = 0;
1359         select_devices(adev);
1360         in->standby = true;
1361 
1362         if (get_bubblelevel(adev))
1363             in->dev->bubble_level->stop_polling(adev->bubble_level);
1364     }
1365 
1366     eS305_SetActiveIoHandle(ES305_IO_HANDLE_NONE);
1367 }
1368 
in_standby(struct audio_stream * stream)1369 static int in_standby(struct audio_stream *stream)
1370 {
1371     struct stream_in *in = (struct stream_in *)stream;
1372 
1373     pthread_mutex_lock(&in->lock);
1374     pthread_mutex_lock(&in->dev->lock);
1375 
1376     do_in_standby(in);
1377 
1378     pthread_mutex_unlock(&in->dev->lock);
1379     pthread_mutex_unlock(&in->lock);
1380 
1381     return 0;
1382 }
1383 
in_dump(const struct audio_stream * stream,int fd)1384 static int in_dump(const struct audio_stream *stream, int fd)
1385 {
1386     return 0;
1387 }
1388 
in_set_parameters(struct audio_stream * stream,const char * kvpairs)1389 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1390 {
1391     struct stream_in *in = (struct stream_in *)stream;
1392     struct audio_device *adev = in->dev;
1393     struct str_parms *parms;
1394     char value[32];
1395     int ret;
1396     unsigned int val;
1397     bool apply_now = false;
1398 
1399     parms = str_parms_create_str(kvpairs);
1400 
1401     pthread_mutex_lock(&in->lock);
1402     pthread_mutex_lock(&adev->lock);
1403     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE,
1404                             value, sizeof(value));
1405     if (ret >= 0) {
1406         val = atoi(value);
1407         /* no audio source uses val == 0 */
1408         if ((in->input_source != val) && (val != 0)) {
1409             in->input_source = val;
1410             apply_now = !in->standby;
1411         }
1412     }
1413 
1414     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
1415                             value, sizeof(value));
1416     if (ret >= 0) {
1417         /* strip AUDIO_DEVICE_BIT_IN to allow bitwise comparisons */
1418         val = atoi(value) & ~AUDIO_DEVICE_BIT_IN;
1419         /* no audio device uses val == 0 */
1420         if ((in->device != val) && (val != 0)) {
1421             /* force output standby to start or stop SCO pcm stream if needed */
1422             if ((val & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) ^
1423                     (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
1424                 do_in_standby(in);
1425             }
1426             in->device = val;
1427             apply_now = !in->standby;
1428         }
1429     }
1430 
1431     if (apply_now) {
1432         adev->input_source = in->input_source;
1433         adev->in_device = in->device;
1434         select_devices(adev);
1435     }
1436 
1437     pthread_mutex_unlock(&adev->lock);
1438     pthread_mutex_unlock(&in->lock);
1439 
1440     str_parms_destroy(parms);
1441     return ret;
1442 }
1443 
in_get_parameters(const struct audio_stream * stream,const char * keys)1444 static char * in_get_parameters(const struct audio_stream *stream,
1445                                 const char *keys)
1446 {
1447     return strdup("");
1448 }
1449 
in_set_gain(struct audio_stream_in * stream,float gain)1450 static int in_set_gain(struct audio_stream_in *stream, float gain)
1451 {
1452     return 0;
1453 }
1454 
in_apply_ramp(struct stream_in * in,int16_t * buffer,size_t frames)1455 static void in_apply_ramp(struct stream_in *in, int16_t *buffer, size_t frames)
1456 {
1457     size_t i;
1458     uint16_t vol = in->ramp_vol;
1459     uint16_t step = in->ramp_step;
1460 
1461     frames = (frames < in->ramp_frames) ? frames : in->ramp_frames;
1462 
1463     if (in->channel_mask == AUDIO_CHANNEL_IN_MONO)
1464         for (i = 0; i < frames; i++)
1465         {
1466             buffer[i] = (int16_t)((buffer[i] * vol) >> 16);
1467             vol += step;
1468         }
1469     else
1470         for (i = 0; i < frames; i++)
1471         {
1472             buffer[2*i] = (int16_t)((buffer[2*i] * vol) >> 16);
1473             buffer[2*i + 1] = (int16_t)((buffer[2*i + 1] * vol) >> 16);
1474             vol += step;
1475         }
1476 
1477 
1478     in->ramp_vol = vol;
1479     in->ramp_frames -= frames;
1480 }
1481 
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)1482 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
1483                        size_t bytes)
1484 {
1485     int ret = 0;
1486     struct stream_in *in = (struct stream_in *)stream;
1487     struct audio_device *adev = in->dev;
1488     size_t frames_rq = bytes / audio_stream_in_frame_size(stream);
1489 
1490     /*
1491      * acquiring hw device mutex systematically is useful if a low
1492      * priority thread is waiting on the input stream mutex - e.g.
1493      * executing in_set_parameters() while holding the hw device
1494      * mutex
1495      */
1496     pthread_mutex_lock(&in->lock);
1497     if (in->standby) {
1498         pthread_mutex_lock(&adev->lock);
1499         ret = start_input_stream(in);
1500         pthread_mutex_unlock(&adev->lock);
1501         if (ret < 0)
1502             goto exit;
1503         in->standby = false;
1504     }
1505 
1506     /*if (in->num_preprocessors != 0)
1507         ret = process_frames(in, buffer, frames_rq);
1508       else */
1509     ret = read_frames(in, buffer, frames_rq);
1510 
1511     if (ret > 0)
1512         ret = 0;
1513 
1514     if (in->ramp_frames > 0)
1515         in_apply_ramp(in, buffer, frames_rq);
1516 
1517     /*
1518      * Instead of writing zeroes here, we could trust the hardware
1519      * to always provide zeroes when muted.
1520      */
1521     if (ret == 0 && adev->mic_mute)
1522         memset(buffer, 0, bytes);
1523 
1524 exit:
1525     if (ret < 0)
1526         usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
1527                in_get_sample_rate(&stream->common));
1528 
1529     pthread_mutex_unlock(&in->lock);
1530     return bytes;
1531 }
1532 
in_get_input_frames_lost(struct audio_stream_in * stream)1533 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1534 {
1535     return 0;
1536 }
1537 
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1538 static int in_add_audio_effect(const struct audio_stream *stream,
1539                                effect_handle_t effect)
1540 {
1541     struct stream_in *in = (struct stream_in *)stream;
1542     effect_descriptor_t descr;
1543     if ((*effect)->get_descriptor(effect, &descr) == 0) {
1544 
1545         pthread_mutex_lock(&in->lock);
1546         pthread_mutex_lock(&in->dev->lock);
1547 
1548         eS305_AddEffect(&descr, in->io_handle);
1549 
1550         pthread_mutex_unlock(&in->dev->lock);
1551         pthread_mutex_unlock(&in->lock);
1552     }
1553 
1554     return 0;
1555 }
1556 
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1557 static int in_remove_audio_effect(const struct audio_stream *stream,
1558                                   effect_handle_t effect)
1559 {
1560     struct stream_in *in = (struct stream_in *)stream;
1561     effect_descriptor_t descr;
1562     if ((*effect)->get_descriptor(effect, &descr) == 0) {
1563 
1564         pthread_mutex_lock(&in->lock);
1565         pthread_mutex_lock(&in->dev->lock);
1566 
1567         eS305_RemoveEffect(&descr, in->io_handle);
1568 
1569         pthread_mutex_unlock(&in->dev->lock);
1570         pthread_mutex_unlock(&in->lock);
1571     }
1572 
1573     return 0;
1574 }
1575 
adev_open_output_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out,const char * address __unused)1576 static int adev_open_output_stream(struct audio_hw_device *dev,
1577                                    audio_io_handle_t handle,
1578                                    audio_devices_t devices,
1579                                    audio_output_flags_t flags,
1580                                    struct audio_config *config,
1581                                    struct audio_stream_out **stream_out,
1582                                    const char *address __unused)
1583 {
1584     struct audio_device *adev = (struct audio_device *)dev;
1585     struct stream_out *out;
1586     int ret;
1587     enum output_type type;
1588 
1589     out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1590     if (!out)
1591         return -ENOMEM;
1592 
1593     out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1594     out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1595     if (devices == AUDIO_DEVICE_NONE)
1596         devices = AUDIO_DEVICE_OUT_SPEAKER;
1597     out->device = devices;
1598 
1599     if (flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1600                    devices == AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1601         pthread_mutex_lock(&adev->lock);
1602         ret = read_hdmi_channel_masks(adev, out);
1603         pthread_mutex_unlock(&adev->lock);
1604         if (ret != 0)
1605             goto err_open;
1606         if (config->sample_rate == 0)
1607             config->sample_rate = HDMI_MULTI_DEFAULT_SAMPLING_RATE;
1608         if (config->channel_mask == 0)
1609             config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1610         out->channel_mask = config->channel_mask;
1611         out->config = pcm_config_hdmi_multi;
1612         out->config.rate = config->sample_rate;
1613         out->config.channels = audio_channel_count_from_out_mask(config->channel_mask);
1614         out->pcm_device = PCM_DEVICE;
1615         type = OUTPUT_HDMI;
1616     } else if (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1617         out->config = pcm_config_deep;
1618         out->pcm_device = PCM_DEVICE_DEEP;
1619         type = OUTPUT_DEEP_BUF;
1620     } else {
1621         out->config = pcm_config;
1622         out->pcm_device = PCM_DEVICE;
1623         type = OUTPUT_LOW_LATENCY;
1624     }
1625 
1626     out->stream.common.get_sample_rate = out_get_sample_rate;
1627     out->stream.common.set_sample_rate = out_set_sample_rate;
1628     out->stream.common.get_buffer_size = out_get_buffer_size;
1629     out->stream.common.get_channels = out_get_channels;
1630     out->stream.common.get_format = out_get_format;
1631     out->stream.common.set_format = out_set_format;
1632     out->stream.common.standby = out_standby;
1633     out->stream.common.dump = out_dump;
1634     out->stream.common.set_parameters = out_set_parameters;
1635     out->stream.common.get_parameters = out_get_parameters;
1636     out->stream.common.add_audio_effect = out_add_audio_effect;
1637     out->stream.common.remove_audio_effect = out_remove_audio_effect;
1638     out->stream.get_latency = out_get_latency;
1639     out->stream.set_volume = out_set_volume;
1640     out->stream.write = out_write;
1641     out->stream.get_render_position = out_get_render_position;
1642     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1643     out->stream.get_presentation_position = out_get_presentation_position;
1644 
1645     out->dev = adev;
1646 
1647     config->format = out_get_format(&out->stream.common);
1648     config->channel_mask = out_get_channels(&out->stream.common);
1649     config->sample_rate = out_get_sample_rate(&out->stream.common);
1650 
1651     out->standby = true;
1652     /* out->muted = false; by calloc() */
1653     /* out->written = 0; by calloc() */
1654 
1655     pthread_mutex_lock(&adev->lock_outputs);
1656     if (adev->outputs[type]) {
1657         pthread_mutex_unlock(&adev->lock_outputs);
1658         ret = -EBUSY;
1659         goto err_open;
1660     }
1661     adev->outputs[type] = out;
1662     pthread_mutex_unlock(&adev->lock_outputs);
1663 
1664     *stream_out = &out->stream;
1665 
1666     return 0;
1667 
1668 err_open:
1669     free(out);
1670     *stream_out = NULL;
1671     return ret;
1672 }
1673 
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)1674 static void adev_close_output_stream(struct audio_hw_device *dev,
1675                                      struct audio_stream_out *stream)
1676 {
1677     struct audio_device *adev;
1678     enum output_type type;
1679 
1680     out_standby(&stream->common);
1681     adev = (struct audio_device *)dev;
1682     pthread_mutex_lock(&adev->lock_outputs);
1683     for (type = 0; type < OUTPUT_TOTAL; ++type) {
1684         if (adev->outputs[type] == (struct stream_out *) stream) {
1685             adev->outputs[type] = NULL;
1686             break;
1687         }
1688     }
1689     pthread_mutex_unlock(&adev->lock_outputs);
1690     free(stream);
1691 }
1692 
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)1693 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1694 {
1695     return 0;
1696 }
1697 
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)1698 static char * adev_get_parameters(const struct audio_hw_device *dev,
1699                                   const char *keys)
1700 {
1701     struct audio_device *adev = (struct audio_device *)dev;
1702     struct str_parms *parms = str_parms_create_str(keys);
1703     char value[32];
1704     int ret = str_parms_get_str(parms, "ec_supported", value, sizeof(value));
1705     char *str;
1706 
1707     str_parms_destroy(parms);
1708     if (ret >= 0) {
1709         parms = str_parms_create_str("ec_supported=yes");
1710         str = str_parms_to_str(parms);
1711         str_parms_destroy(parms);
1712         return str;
1713     }
1714     return strdup("");
1715 }
1716 
adev_init_check(const struct audio_hw_device * dev)1717 static int adev_init_check(const struct audio_hw_device *dev)
1718 {
1719     return 0;
1720 }
1721 
adev_set_voice_volume(struct audio_hw_device * dev,float volume)1722 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1723 {
1724     return -ENOSYS;
1725 }
1726 
adev_set_master_volume(struct audio_hw_device * dev,float volume)1727 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1728 {
1729     return -ENOSYS;
1730 }
1731 
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)1732 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1733 {
1734     return 0;
1735 }
1736 
adev_set_mic_mute(struct audio_hw_device * dev,bool state)1737 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1738 {
1739     struct audio_device *adev = (struct audio_device *)dev;
1740 
1741     adev->mic_mute = state;
1742 
1743     return 0;
1744 }
1745 
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)1746 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1747 {
1748     struct audio_device *adev = (struct audio_device *)dev;
1749 
1750     *state = adev->mic_mute;
1751 
1752     return 0;
1753 }
1754 
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)1755 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1756                                          const struct audio_config *config)
1757 {
1758 
1759     return get_input_buffer_size(config->sample_rate, config->format,
1760                                  audio_channel_count_from_in_mask(config->channel_mask),
1761                                  false /* is_low_latency: since we don't know, be conservative */);
1762 }
1763 
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,audio_input_flags_t flags,const char * address __unused,audio_source_t source __unused)1764 static int adev_open_input_stream(struct audio_hw_device *dev,
1765                                   audio_io_handle_t handle,
1766                                   audio_devices_t devices,
1767                                   struct audio_config *config,
1768                                   struct audio_stream_in **stream_in,
1769                                   audio_input_flags_t flags,
1770                                   const char *address __unused,
1771                                   audio_source_t source __unused)
1772 {
1773     struct audio_device *adev = (struct audio_device *)dev;
1774     struct stream_in *in;
1775     int ret;
1776 
1777     *stream_in = NULL;
1778 
1779     /* Respond with a request for mono if a different format is given. */
1780     if (config->channel_mask != AUDIO_CHANNEL_IN_MONO &&
1781             config->channel_mask != AUDIO_CHANNEL_IN_FRONT_BACK) {
1782         config->channel_mask = AUDIO_CHANNEL_IN_MONO;
1783         return -EINVAL;
1784     }
1785 
1786     in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1787     if (!in)
1788         return -ENOMEM;
1789 
1790     in->stream.common.get_sample_rate = in_get_sample_rate;
1791     in->stream.common.set_sample_rate = in_set_sample_rate;
1792     in->stream.common.get_buffer_size = in_get_buffer_size;
1793     in->stream.common.get_channels = in_get_channels;
1794     in->stream.common.get_format = in_get_format;
1795     in->stream.common.set_format = in_set_format;
1796     in->stream.common.standby = in_standby;
1797     in->stream.common.dump = in_dump;
1798     in->stream.common.set_parameters = in_set_parameters;
1799     in->stream.common.get_parameters = in_get_parameters;
1800     in->stream.common.add_audio_effect = in_add_audio_effect;
1801     in->stream.common.remove_audio_effect = in_remove_audio_effect;
1802     in->stream.set_gain = in_set_gain;
1803     in->stream.read = in_read;
1804     in->stream.get_input_frames_lost = in_get_input_frames_lost;
1805 
1806     in->dev = adev;
1807     in->standby = true;
1808     in->requested_rate = config->sample_rate;
1809     in->input_source = AUDIO_SOURCE_DEFAULT;
1810     /* strip AUDIO_DEVICE_BIT_IN to allow bitwise comparisons */
1811     in->device = devices & ~AUDIO_DEVICE_BIT_IN;
1812     in->io_handle = handle;
1813     in->channel_mask = config->channel_mask;
1814     in->flags = flags;
1815     struct pcm_config *pcm_config = flags & AUDIO_INPUT_FLAG_FAST ?
1816             &pcm_config_in_low_latency : &pcm_config_in;
1817     in->config = pcm_config;
1818 
1819     in->buffer = malloc(pcm_config->period_size * pcm_config->channels
1820                                                * audio_stream_in_frame_size(&in->stream));
1821 
1822     if (!in->buffer) {
1823         ret = -ENOMEM;
1824         goto err_malloc;
1825     }
1826 
1827     if (in->requested_rate != pcm_config->rate) {
1828         in->buf_provider.get_next_buffer = get_next_buffer;
1829         in->buf_provider.release_buffer = release_buffer;
1830 
1831         ret = create_resampler(pcm_config->rate,
1832                                in->requested_rate,
1833                                audio_channel_count_from_in_mask(in->channel_mask),
1834                                RESAMPLER_QUALITY_DEFAULT,
1835                                &in->buf_provider,
1836                                &in->resampler);
1837         if (ret != 0) {
1838             ret = -EINVAL;
1839             goto err_resampler;
1840         }
1841     }
1842 
1843     *stream_in = &in->stream;
1844     return 0;
1845 
1846 err_resampler:
1847     free(in->buffer);
1848 err_malloc:
1849     free(in);
1850     return ret;
1851 }
1852 
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * stream)1853 static void adev_close_input_stream(struct audio_hw_device *dev,
1854                                    struct audio_stream_in *stream)
1855 {
1856     struct stream_in *in = (struct stream_in *)stream;
1857 
1858     in_standby(&stream->common);
1859     if (in->resampler) {
1860         release_resampler(in->resampler);
1861         in->resampler = NULL;
1862     }
1863     free(in->buffer);
1864     free(stream);
1865 }
1866 
adev_dump(const audio_hw_device_t * device,int fd)1867 static int adev_dump(const audio_hw_device_t *device, int fd)
1868 {
1869     return 0;
1870 }
1871 
adev_close(hw_device_t * device)1872 static int adev_close(hw_device_t *device)
1873 {
1874     struct audio_device *adev = (struct audio_device *)device;
1875 
1876     audio_route_free(adev->ar);
1877 
1878     eS305_Release();
1879 
1880     if (adev->hdmi_drv_fd >= 0)
1881         close(adev->hdmi_drv_fd);
1882 
1883     if (adev->bubble_level)
1884         bubble_level_release(adev->bubble_level);
1885 
1886     free(device);
1887     return 0;
1888 }
1889 
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)1890 static int adev_open(const hw_module_t* module, const char* name,
1891                      hw_device_t** device)
1892 {
1893     struct audio_device *adev;
1894     int ret;
1895 
1896     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1897         return -EINVAL;
1898 
1899     adev = calloc(1, sizeof(struct audio_device));
1900     if (!adev)
1901         return -ENOMEM;
1902 
1903     adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
1904     adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1905     adev->hw_device.common.module = (struct hw_module_t *) module;
1906     adev->hw_device.common.close = adev_close;
1907 
1908     adev->hw_device.init_check = adev_init_check;
1909     adev->hw_device.set_voice_volume = adev_set_voice_volume;
1910     adev->hw_device.set_master_volume = adev_set_master_volume;
1911     adev->hw_device.set_mode = adev_set_mode;
1912     adev->hw_device.set_mic_mute = adev_set_mic_mute;
1913     adev->hw_device.get_mic_mute = adev_get_mic_mute;
1914     adev->hw_device.set_parameters = adev_set_parameters;
1915     adev->hw_device.get_parameters = adev_get_parameters;
1916     adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1917     adev->hw_device.open_output_stream = adev_open_output_stream;
1918     adev->hw_device.close_output_stream = adev_close_output_stream;
1919     adev->hw_device.open_input_stream = adev_open_input_stream;
1920     adev->hw_device.close_input_stream = adev_close_input_stream;
1921     adev->hw_device.dump = adev_dump;
1922 
1923     adev->ar = audio_route_init(MIXER_CARD, NULL);
1924     adev->input_source = AUDIO_SOURCE_DEFAULT;
1925     /* adev->cur_route_id initial value is 0 and such that first device
1926      * selection is always applied by select_devices() */
1927 
1928     adev->es305_preset = ES305_PRESET_INIT;
1929     adev->es305_new_mode = ES305_MODE_LEVEL;
1930     adev->es305_mode = ES305_MODE_LEVEL;
1931     adev->hdmi_drv_fd = -1;
1932 
1933     *device = &adev->hw_device.common;
1934 
1935     char value[PROPERTY_VALUE_MAX];
1936     if (property_get("audio_hal.period_size", value, NULL) > 0) {
1937         pcm_config.period_size = atoi(value);
1938         pcm_config_in.period_size = pcm_config.period_size;
1939     }
1940     if (property_get("audio_hal.in_period_size", value, NULL) > 0)
1941         pcm_config_in.period_size = atoi(value);
1942 
1943     return 0;
1944 }
1945 
1946 static struct hw_module_methods_t hal_module_methods = {
1947     .open = adev_open,
1948 };
1949 
1950 struct audio_module HAL_MODULE_INFO_SYM = {
1951     .common = {
1952         .tag = HARDWARE_MODULE_TAG,
1953         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1954         .hal_api_version = HARDWARE_HAL_API_VERSION,
1955         .id = AUDIO_HARDWARE_MODULE_ID,
1956         .name = "Manta audio HW HAL",
1957         .author = "The Android Open Source Project",
1958         .methods = &hal_module_methods,
1959     },
1960 };
1961