1 /*
2  * Copyright (C) 2016 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_hal_poplar"
18 //#define LOG_NDEBUG 0
19 
20 #include <errno.h>
21 #include <malloc.h>
22 #include <pthread.h>
23 #include <stdint.h>
24 #include <sys/time.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 
28 #include <log/log.h>
29 #include <cutils/str_parms.h>
30 #include <cutils/properties.h>
31 
32 #include <hardware/hardware.h>
33 #include <system/audio.h>
34 #include <hardware/audio.h>
35 
36 #include <sound/asound.h>
37 #include <tinyalsa/asoundlib.h>
38 #include <audio_utils/resampler.h>
39 #include <audio_utils/echo_reference.h>
40 #include <hardware/audio_effect.h>
41 #include <hardware/audio_alsaops.h>
42 #include <audio_effects/effect_aec.h>
43 
44 
45 #define CARD_OUT 0
46 #define PORT_CODEC 0
47 /* Minimum granularity - Arbitrary but small value */
48 #define CODEC_BASE_FRAME_COUNT 32
49 
50 /* number of base blocks in a short period (low latency) */
51 #define PERIOD_MULTIPLIER 32  /* 21 ms */
52 /* number of frames per short period (low latency) */
53 #define PERIOD_SIZE (CODEC_BASE_FRAME_COUNT * PERIOD_MULTIPLIER)
54 /* number of pseudo periods for low latency playback */
55 #define PLAYBACK_PERIOD_COUNT 4
56 #define PLAYBACK_PERIOD_START_THRESHOLD 2
57 #define CODEC_SAMPLING_RATE 48000
58 #define CHANNEL_STEREO 2
59 
60 struct stub_stream_in {
61     struct audio_stream_in stream;
62 };
63 
64 struct alsa_audio_device {
65     struct audio_hw_device hw_device;
66 
67     pthread_mutex_t lock;   /* see note below on mutex acquisition order */
68     int devices;
69     struct alsa_stream_in *active_input;
70     struct alsa_stream_out *active_output;
71     bool mic_mute;
72 };
73 
74 struct alsa_stream_out {
75     struct audio_stream_out stream;
76 
77     pthread_mutex_t lock;   /* see note below on mutex acquisition order */
78     struct pcm_config config;
79     struct pcm *pcm;
80     bool unavailable;
81     int standby;
82     struct alsa_audio_device *dev;
83     int write_threshold;
84     unsigned int written;
85 };
86 
87 
88 /* must be called with hw device and output stream mutexes locked */
start_output_stream(struct alsa_stream_out * out)89 static int start_output_stream(struct alsa_stream_out *out)
90 {
91     struct alsa_audio_device *adev = out->dev;
92 
93     if (out->unavailable)
94         return -ENODEV;
95 
96     /* default to low power: will be corrected in out_write if necessary before first write to
97      * tinyalsa.
98      */
99     out->write_threshold = PLAYBACK_PERIOD_COUNT * PERIOD_SIZE;
100     out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PERIOD_SIZE;
101     out->config.avail_min = PERIOD_SIZE;
102 
103     out->pcm = pcm_open(CARD_OUT, PORT_CODEC, PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC, &out->config);
104 
105     if (!pcm_is_ready(out->pcm)) {
106         ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm));
107         pcm_close(out->pcm);
108         adev->active_output = NULL;
109         out->unavailable = true;
110         return -ENODEV;
111     }
112 
113     adev->active_output = out;
114     return 0;
115 }
116 
out_get_sample_rate(const struct audio_stream * stream)117 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
118 {
119     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
120     return out->config.rate;
121 }
122 
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)123 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
124 {
125     ALOGV("out_set_sample_rate: %d", 0);
126     return -ENOSYS;
127 }
128 
out_get_buffer_size(const struct audio_stream * stream)129 static size_t out_get_buffer_size(const struct audio_stream *stream)
130 {
131     ALOGV("out_get_buffer_size: %d", 4096);
132 
133     /* return the closest majoring multiple of 16 frames, as
134      * audioflinger expects audio buffers to be a multiple of 16 frames */
135     size_t size = PERIOD_SIZE;
136     size = ((size + 15) / 16) * 16;
137     return size * audio_stream_out_frame_size((struct audio_stream_out *)stream);
138 }
139 
out_get_channels(const struct audio_stream * stream)140 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
141 {
142     ALOGV("out_get_channels");
143     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
144     return audio_channel_out_mask_from_count(out->config.channels);
145 }
146 
out_get_format(const struct audio_stream * stream)147 static audio_format_t out_get_format(const struct audio_stream *stream)
148 {
149     ALOGV("out_get_format");
150     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
151     return audio_format_from_pcm_format(out->config.format);
152 }
153 
out_set_format(struct audio_stream * stream,audio_format_t format)154 static int out_set_format(struct audio_stream *stream, audio_format_t format)
155 {
156     ALOGV("out_set_format: %d",format);
157     return -ENOSYS;
158 }
159 
do_output_standby(struct alsa_stream_out * out)160 static int do_output_standby(struct alsa_stream_out *out)
161 {
162     struct alsa_audio_device *adev = out->dev;
163 
164     if (!out->standby) {
165         pcm_close(out->pcm);
166         out->pcm = NULL;
167         adev->active_output = NULL;
168         out->standby = 1;
169     }
170     return 0;
171 }
172 
out_standby(struct audio_stream * stream)173 static int out_standby(struct audio_stream *stream)
174 {
175     ALOGV("out_standby");
176     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
177     int status;
178 
179     pthread_mutex_lock(&out->dev->lock);
180     pthread_mutex_lock(&out->lock);
181     status = do_output_standby(out);
182     pthread_mutex_unlock(&out->lock);
183     pthread_mutex_unlock(&out->dev->lock);
184     return status;
185 }
186 
out_dump(const struct audio_stream * stream,int fd)187 static int out_dump(const struct audio_stream *stream, int fd)
188 {
189     ALOGV("out_dump");
190     return 0;
191 }
192 
out_set_parameters(struct audio_stream * stream,const char * kvpairs)193 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
194 {
195     ALOGV("out_set_parameters");
196     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
197     struct alsa_audio_device *adev = out->dev;
198     struct str_parms *parms;
199     char value[32];
200     int ret, val = 0;
201 
202     parms = str_parms_create_str(kvpairs);
203 
204     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
205     if (ret >= 0) {
206         val = atoi(value);
207         pthread_mutex_lock(&adev->lock);
208         pthread_mutex_lock(&out->lock);
209         if (((adev->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) {
210             adev->devices &= ~AUDIO_DEVICE_OUT_ALL;
211             adev->devices |= val;
212         }
213         pthread_mutex_unlock(&out->lock);
214         pthread_mutex_unlock(&adev->lock);
215     }
216 
217     str_parms_destroy(parms);
218     return ret;
219 }
220 
out_get_parameters(const struct audio_stream * stream,const char * keys)221 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
222 {
223     ALOGV("out_get_parameters");
224     return strdup("");
225 }
226 
out_get_latency(const struct audio_stream_out * stream)227 static uint32_t out_get_latency(const struct audio_stream_out *stream)
228 {
229     ALOGV("out_get_latency");
230     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
231     return (PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate;
232 }
233 
out_set_volume(struct audio_stream_out * stream,float left,float right)234 static int out_set_volume(struct audio_stream_out *stream, float left,
235         float right)
236 {
237     ALOGV("out_set_volume: Left:%f Right:%f", left, right);
238     return 0;
239 }
240 
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)241 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
242         size_t bytes)
243 {
244     int ret;
245     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
246     struct alsa_audio_device *adev = out->dev;
247     size_t frame_size = audio_stream_out_frame_size(stream);
248     size_t out_frames = bytes / frame_size;
249 
250     /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
251      * on the output stream mutex - e.g. executing select_mode() while holding the hw device
252      * mutex
253      */
254     pthread_mutex_lock(&adev->lock);
255     pthread_mutex_lock(&out->lock);
256     if (out->standby) {
257         ret = start_output_stream(out);
258         if (ret != 0) {
259             pthread_mutex_unlock(&adev->lock);
260             goto exit;
261         }
262         out->standby = 0;
263     }
264 
265     pthread_mutex_unlock(&adev->lock);
266 
267     ret = pcm_mmap_write(out->pcm, buffer, out_frames * frame_size);
268     if (ret == 0) {
269         out->written += out_frames;
270     }
271 exit:
272     pthread_mutex_unlock(&out->lock);
273 
274     if (ret != 0) {
275         usleep((int64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) /
276                 out_get_sample_rate(&stream->common));
277     }
278 
279     return bytes;
280 }
281 
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)282 static int out_get_render_position(const struct audio_stream_out *stream,
283         uint32_t *dsp_frames)
284 {
285     *dsp_frames = 0;
286     ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames);
287     return -EINVAL;
288 }
289 
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)290 static int out_get_presentation_position(const struct audio_stream_out *stream,
291                                    uint64_t *frames, struct timespec *timestamp)
292 {
293     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
294     int ret = -1;
295 
296         if (out->pcm) {
297             unsigned int avail;
298             if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
299                 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
300                 int64_t signed_frames = out->written - kernel_buffer_size + avail;
301                 if (signed_frames >= 0) {
302                     *frames = signed_frames;
303                     ret = 0;
304                 }
305             }
306         }
307 
308     return ret;
309 }
310 
311 
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)312 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
313 {
314     ALOGV("out_add_audio_effect: %p", effect);
315     return 0;
316 }
317 
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)318 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
319 {
320     ALOGV("out_remove_audio_effect: %p", effect);
321     return 0;
322 }
323 
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)324 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
325         int64_t *timestamp)
326 {
327     *timestamp = 0;
328     ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp));
329     return -EINVAL;
330 }
331 
332 /** audio_stream_in implementation **/
in_get_sample_rate(const struct audio_stream * stream)333 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
334 {
335     ALOGV("in_get_sample_rate");
336     return 8000;
337 }
338 
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)339 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
340 {
341     ALOGV("in_set_sample_rate: %d", rate);
342     return -ENOSYS;
343 }
344 
in_get_buffer_size(const struct audio_stream * stream)345 static size_t in_get_buffer_size(const struct audio_stream *stream)
346 {
347     ALOGV("in_get_buffer_size: %d", 320);
348     return 320;
349 }
350 
in_get_channels(const struct audio_stream * stream)351 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
352 {
353     ALOGV("in_get_channels: %d", AUDIO_CHANNEL_IN_MONO);
354     return AUDIO_CHANNEL_IN_MONO;
355 }
356 
in_get_format(const struct audio_stream * stream)357 static audio_format_t in_get_format(const struct audio_stream *stream)
358 {
359     return AUDIO_FORMAT_PCM_16_BIT;
360 }
361 
in_set_format(struct audio_stream * stream,audio_format_t format)362 static int in_set_format(struct audio_stream *stream, audio_format_t format)
363 {
364     return -ENOSYS;
365 }
366 
in_standby(struct audio_stream * stream)367 static int in_standby(struct audio_stream *stream)
368 {
369     return 0;
370 }
371 
in_dump(const struct audio_stream * stream,int fd)372 static int in_dump(const struct audio_stream *stream, int fd)
373 {
374     return 0;
375 }
376 
in_set_parameters(struct audio_stream * stream,const char * kvpairs)377 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
378 {
379     return 0;
380 }
381 
in_get_parameters(const struct audio_stream * stream,const char * keys)382 static char * in_get_parameters(const struct audio_stream *stream,
383         const char *keys)
384 {
385     return strdup("");
386 }
387 
in_set_gain(struct audio_stream_in * stream,float gain)388 static int in_set_gain(struct audio_stream_in *stream, float gain)
389 {
390     return 0;
391 }
392 
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)393 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
394         size_t bytes)
395 {
396     ALOGV("in_read: bytes %zu", bytes);
397     /* XXX: fake timing for audio input */
398     usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
399             in_get_sample_rate(&stream->common));
400     memset(buffer, 0, bytes);
401     return bytes;
402 }
403 
in_get_input_frames_lost(struct audio_stream_in * stream)404 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
405 {
406     return 0;
407 }
408 
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)409 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
410 {
411     return 0;
412 }
413 
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)414 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
415 {
416     return 0;
417 }
418 
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)419 static int adev_open_output_stream(struct audio_hw_device *dev,
420         audio_io_handle_t handle,
421         audio_devices_t devices,
422         audio_output_flags_t flags,
423         struct audio_config *config,
424         struct audio_stream_out **stream_out,
425         const char *address __unused)
426 {
427     ALOGV("adev_open_output_stream...");
428 
429     struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
430     struct alsa_stream_out *out;
431     struct pcm_params *params;
432     int ret = 0;
433 
434     params = pcm_params_get(CARD_OUT, PORT_CODEC, PCM_OUT);
435     if (!params)
436         return -ENOSYS;
437 
438     out = (struct alsa_stream_out *)calloc(1, sizeof(struct alsa_stream_out));
439     if (!out)
440         return -ENOMEM;
441 
442     out->stream.common.get_sample_rate = out_get_sample_rate;
443     out->stream.common.set_sample_rate = out_set_sample_rate;
444     out->stream.common.get_buffer_size = out_get_buffer_size;
445     out->stream.common.get_channels = out_get_channels;
446     out->stream.common.get_format = out_get_format;
447     out->stream.common.set_format = out_set_format;
448     out->stream.common.standby = out_standby;
449     out->stream.common.dump = out_dump;
450     out->stream.common.set_parameters = out_set_parameters;
451     out->stream.common.get_parameters = out_get_parameters;
452     out->stream.common.add_audio_effect = out_add_audio_effect;
453     out->stream.common.remove_audio_effect = out_remove_audio_effect;
454     out->stream.get_latency = out_get_latency;
455     out->stream.set_volume = out_set_volume;
456     out->stream.write = out_write;
457     out->stream.get_render_position = out_get_render_position;
458     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
459     out->stream.get_presentation_position = out_get_presentation_position;
460 
461     out->config.channels = CHANNEL_STEREO;
462     out->config.rate = CODEC_SAMPLING_RATE;
463     out->config.format = PCM_FORMAT_S16_LE;
464     out->config.period_size = PERIOD_SIZE;
465     out->config.period_count = PLAYBACK_PERIOD_COUNT;
466 
467     if (out->config.rate != config->sample_rate ||
468            audio_channel_count_from_out_mask(config->channel_mask) != CHANNEL_STEREO ||
469                out->config.format !=  pcm_format_from_audio_format(config->format) ) {
470         config->sample_rate = out->config.rate;
471         config->format = audio_format_from_pcm_format(out->config.format);
472         config->channel_mask = audio_channel_out_mask_from_count(CHANNEL_STEREO);
473         ret = -EINVAL;
474     }
475 
476     ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d",
477                 out->config.channels, out->config.rate, out->config.format);
478 
479     out->dev = ladev;
480     out->standby = 1;
481     out->unavailable = false;
482 
483     config->format = out_get_format(&out->stream.common);
484     config->channel_mask = out_get_channels(&out->stream.common);
485     config->sample_rate = out_get_sample_rate(&out->stream.common);
486 
487     *stream_out = &out->stream;
488 
489     /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
490     ret = 0;
491 
492     return ret;
493 }
494 
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)495 static void adev_close_output_stream(struct audio_hw_device *dev,
496         struct audio_stream_out *stream)
497 {
498     ALOGV("adev_close_output_stream...");
499     free(stream);
500 }
501 
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)502 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
503 {
504     ALOGV("adev_set_parameters");
505     return -ENOSYS;
506 }
507 
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)508 static char * adev_get_parameters(const struct audio_hw_device *dev,
509         const char *keys)
510 {
511     ALOGV("adev_get_parameters");
512     return strdup("");
513 }
514 
adev_init_check(const struct audio_hw_device * dev)515 static int adev_init_check(const struct audio_hw_device *dev)
516 {
517     ALOGV("adev_init_check");
518     return 0;
519 }
520 
adev_set_voice_volume(struct audio_hw_device * dev,float volume)521 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
522 {
523     ALOGV("adev_set_voice_volume: %f", volume);
524     return -ENOSYS;
525 }
526 
adev_set_master_volume(struct audio_hw_device * dev,float volume)527 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
528 {
529     ALOGV("adev_set_master_volume: %f", volume);
530     return -ENOSYS;
531 }
532 
adev_get_master_volume(struct audio_hw_device * dev,float * volume)533 static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
534 {
535     ALOGV("adev_get_master_volume: %f", *volume);
536     return -ENOSYS;
537 }
538 
adev_set_master_mute(struct audio_hw_device * dev,bool muted)539 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
540 {
541     ALOGV("adev_set_master_mute: %d", muted);
542     return -ENOSYS;
543 }
544 
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)545 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
546 {
547     ALOGV("adev_get_master_mute: %d", *muted);
548     return -ENOSYS;
549 }
550 
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)551 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
552 {
553     ALOGV("adev_set_mode: %d", mode);
554     return 0;
555 }
556 
adev_set_mic_mute(struct audio_hw_device * dev,bool state)557 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
558 {
559     ALOGV("adev_set_mic_mute: %d",state);
560     return -ENOSYS;
561 }
562 
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)563 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
564 {
565     ALOGV("adev_get_mic_mute");
566     return -ENOSYS;
567 }
568 
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)569 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
570         const struct audio_config *config)
571 {
572     ALOGV("adev_get_input_buffer_size: %d", 320);
573     return 320;
574 }
575 
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 __unused,const char * address __unused,audio_source_t source __unused)576 static int adev_open_input_stream(struct audio_hw_device *dev,
577         audio_io_handle_t handle,
578         audio_devices_t devices,
579         struct audio_config *config,
580         struct audio_stream_in **stream_in,
581         audio_input_flags_t flags __unused,
582         const char *address __unused,
583         audio_source_t source __unused)
584 {
585     ALOGV("adev_open_input_stream...");
586 
587     struct stub_stream_in *in;
588 
589     in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in));
590     if (!in)
591         return -ENOMEM;
592 
593     in->stream.common.get_sample_rate = in_get_sample_rate;
594     in->stream.common.set_sample_rate = in_set_sample_rate;
595     in->stream.common.get_buffer_size = in_get_buffer_size;
596     in->stream.common.get_channels = in_get_channels;
597     in->stream.common.get_format = in_get_format;
598     in->stream.common.set_format = in_set_format;
599     in->stream.common.standby = in_standby;
600     in->stream.common.dump = in_dump;
601     in->stream.common.set_parameters = in_set_parameters;
602     in->stream.common.get_parameters = in_get_parameters;
603     in->stream.common.add_audio_effect = in_add_audio_effect;
604     in->stream.common.remove_audio_effect = in_remove_audio_effect;
605     in->stream.set_gain = in_set_gain;
606     in->stream.read = in_read;
607     in->stream.get_input_frames_lost = in_get_input_frames_lost;
608 
609     *stream_in = &in->stream;
610     return 0;
611 }
612 
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * in)613 static void adev_close_input_stream(struct audio_hw_device *dev,
614         struct audio_stream_in *in)
615 {
616     ALOGV("adev_close_input_stream...");
617     return;
618 }
619 
adev_dump(const audio_hw_device_t * device,int fd)620 static int adev_dump(const audio_hw_device_t *device, int fd)
621 {
622     ALOGV("adev_dump");
623     return 0;
624 }
625 
adev_close(hw_device_t * device)626 static int adev_close(hw_device_t *device)
627 {
628     ALOGV("adev_close");
629     free(device);
630     return 0;
631 }
632 
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)633 static int adev_open(const hw_module_t* module, const char* name,
634         hw_device_t** device)
635 {
636     ALOGV("adev_open: %s", name);
637 
638     struct alsa_audio_device *adev;
639 
640     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
641         return -EINVAL;
642 
643     adev = calloc(1, sizeof(struct alsa_audio_device));
644     if (!adev)
645         return -ENOMEM;
646 
647     adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
648     adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
649     adev->hw_device.common.module = (struct hw_module_t *) module;
650     adev->hw_device.common.close = adev_close;
651     adev->hw_device.init_check = adev_init_check;
652     adev->hw_device.set_voice_volume = adev_set_voice_volume;
653     adev->hw_device.set_master_volume = adev_set_master_volume;
654     adev->hw_device.get_master_volume = adev_get_master_volume;
655     adev->hw_device.set_master_mute = adev_set_master_mute;
656     adev->hw_device.get_master_mute = adev_get_master_mute;
657     adev->hw_device.set_mode = adev_set_mode;
658     adev->hw_device.set_mic_mute = adev_set_mic_mute;
659     adev->hw_device.get_mic_mute = adev_get_mic_mute;
660     adev->hw_device.set_parameters = adev_set_parameters;
661     adev->hw_device.get_parameters = adev_get_parameters;
662     adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
663     adev->hw_device.open_output_stream = adev_open_output_stream;
664     adev->hw_device.close_output_stream = adev_close_output_stream;
665     adev->hw_device.open_input_stream = adev_open_input_stream;
666     adev->hw_device.close_input_stream = adev_close_input_stream;
667     adev->hw_device.dump = adev_dump;
668 
669     adev->devices = AUDIO_DEVICE_NONE;
670 
671     *device = &adev->hw_device.common;
672 
673     return 0;
674 }
675 
676 static struct hw_module_methods_t hal_module_methods = {
677     .open = adev_open,
678 };
679 
680 struct audio_module HAL_MODULE_INFO_SYM = {
681     .common = {
682         .tag = HARDWARE_MODULE_TAG,
683         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
684         .hal_api_version = HARDWARE_HAL_API_VERSION,
685         .id = AUDIO_HARDWARE_MODULE_ID,
686         .name = "Poplar audio HW HAL",
687         .author = "The Android Open Source Project",
688         .methods = &hal_module_methods,
689     },
690 };
691