audio_hw.cpp revision eec87706d21fc0ac4ad10ede86943770b2533564
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 "r_submix"
18#define LOG_NDEBUG 0
19
20#include <errno.h>
21#include <pthread.h>
22#include <stdint.h>
23#include <sys/time.h>
24#include <stdlib.h>
25
26#include <cutils/log.h>
27#include <cutils/str_parms.h>
28#include <cutils/properties.h>
29
30#include <hardware/hardware.h>
31#include <system/audio.h>
32#include <hardware/audio.h>
33
34//#include <media/nbaio/Pipe.h>
35//#include <media/nbaio/PipeReader.h>
36#include <media/nbaio/MonoPipe.h>
37#include <media/nbaio/MonoPipeReader.h>
38#include <media/AudioBufferProvider.h>
39
40extern "C" {
41
42namespace android {
43
44#define MAX_PIPE_DEPTH_IN_FRAMES     (1024*8)
45// The duration of MAX_READ_ATTEMPTS * READ_ATTEMPT_SLEEP_MS must be stricly inferior to
46//   the duration of a record buffer at the current record sample rate (of the device, not of
47//   the recording itself). Here we have:
48//      3 * 5ms = 15ms < 1024 frames * 1000 / 48000 = 21.333ms
49#define MAX_READ_ATTEMPTS            3
50#define READ_ATTEMPT_SLEEP_MS        5 // 5ms between two read attempts when pipe is empty
51#define DEFAULT_RATE_HZ              48000 // default sample rate
52
53struct submix_config {
54    audio_format_t format;
55    audio_channel_mask_t channel_mask;
56    unsigned int rate; // sample rate for the device
57    unsigned int period_size; // size of the audio pipe is period_size * period_count in frames
58    unsigned int period_count;
59};
60
61struct submix_audio_device {
62    struct audio_hw_device device;
63    bool output_standby;
64    bool input_standby;
65    submix_config config;
66    // Pipe variables: they handle the ring buffer that "pipes" audio:
67    //  - from the submix virtual audio output == what needs to be played
68    //    remotely, seen as an output for AudioFlinger
69    //  - to the virtual audio source == what is captured by the component
70    //    which "records" the submix / virtual audio source, and handles it as needed.
71    // A usecase example is one where the component capturing the audio is then sending it over
72    // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
73    // TV with Wifi Display capabilities), or to a wireless audio player.
74    sp<MonoPipe>       rsxSink;
75    sp<MonoPipeReader> rsxSource;
76
77    // device lock, also used to protect access to the audio pipe
78    pthread_mutex_t lock;
79};
80
81struct submix_stream_out {
82    struct audio_stream_out stream;
83    struct submix_audio_device *dev;
84};
85
86struct submix_stream_in {
87    struct audio_stream_in stream;
88    struct submix_audio_device *dev;
89    bool output_standby; // output standby state as seen from record thread
90
91    // wall clock when recording starts
92    struct timespec record_start_time;
93    // how many frames have been requested to be read
94    int64_t read_counter_frames;
95};
96
97
98/* audio HAL functions */
99
100static uint32_t out_get_sample_rate(const struct audio_stream *stream)
101{
102    const struct submix_stream_out *out =
103            reinterpret_cast<const struct submix_stream_out *>(stream);
104    uint32_t out_rate = out->dev->config.rate;
105    //ALOGV("out_get_sample_rate() returns %u", out_rate);
106    return out_rate;
107}
108
109static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
110{
111    if ((rate != 44100) && (rate != 48000)) {
112        ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate);
113        return -ENOSYS;
114    }
115    struct submix_stream_out *out = reinterpret_cast<struct submix_stream_out *>(stream);
116    //ALOGV("out_set_sample_rate(rate=%u)", rate);
117    out->dev->config.rate = rate;
118    return 0;
119}
120
121static size_t out_get_buffer_size(const struct audio_stream *stream)
122{
123    const struct submix_stream_out *out =
124            reinterpret_cast<const struct submix_stream_out *>(stream);
125    const struct submix_config& config_out = out->dev->config;
126    size_t buffer_size = config_out.period_size * popcount(config_out.channel_mask)
127                            * sizeof(int16_t); // only PCM 16bit
128    //ALOGV("out_get_buffer_size() returns %u, period size=%u",
129    //        buffer_size, config_out.period_size);
130    return buffer_size;
131}
132
133static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
134{
135    const struct submix_stream_out *out =
136            reinterpret_cast<const struct submix_stream_out *>(stream);
137    uint32_t channels = out->dev->config.channel_mask;
138    //ALOGV("out_get_channels() returns %08x", channels);
139    return channels;
140}
141
142static audio_format_t out_get_format(const struct audio_stream *stream)
143{
144    return AUDIO_FORMAT_PCM_16_BIT;
145}
146
147static int out_set_format(struct audio_stream *stream, audio_format_t format)
148{
149    if (format != AUDIO_FORMAT_PCM_16_BIT) {
150        return -ENOSYS;
151    } else {
152        return 0;
153    }
154}
155
156static int out_standby(struct audio_stream *stream)
157{
158    ALOGI("out_standby()");
159
160    const struct submix_stream_out *out = reinterpret_cast<const struct submix_stream_out *>(stream);
161
162    pthread_mutex_lock(&out->dev->lock);
163
164    out->dev->output_standby = true;
165
166    pthread_mutex_unlock(&out->dev->lock);
167
168    return 0;
169}
170
171static int out_dump(const struct audio_stream *stream, int fd)
172{
173    return 0;
174}
175
176static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
177{
178    return 0;
179}
180
181static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
182{
183    return strdup("");
184}
185
186static uint32_t out_get_latency(const struct audio_stream_out *stream)
187{
188    const struct submix_stream_out *out =
189            reinterpret_cast<const struct submix_stream_out *>(stream);
190    const struct submix_config * config_out = &(out->dev->config);
191    uint32_t latency = (MAX_PIPE_DEPTH_IN_FRAMES * 1000) / config_out->rate;
192    ALOGV("out_get_latency() returns %u", latency);
193    return latency;
194}
195
196static int out_set_volume(struct audio_stream_out *stream, float left,
197                          float right)
198{
199    return -ENOSYS;
200}
201
202static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
203                         size_t bytes)
204{
205    //ALOGV("out_write(bytes=%d)", bytes);
206    ssize_t written_frames = 0;
207    struct submix_stream_out *out = reinterpret_cast<struct submix_stream_out *>(stream);
208
209    pthread_mutex_lock(&out->dev->lock);
210
211    out->dev->output_standby = false;
212
213    MonoPipe* sink = out->dev->rsxSink.get();
214    if (sink != NULL) {
215        out->dev->rsxSink->incStrong(buffer);
216    } else {
217        pthread_mutex_unlock(&out->dev->lock);
218        ALOGE("out_write without a pipe!");
219        ALOG_ASSERT("out_write without a pipe!");
220        return 0;
221    }
222
223    pthread_mutex_unlock(&out->dev->lock);
224
225    const size_t frame_size = audio_stream_frame_size(&stream->common);
226    const size_t frames = bytes / frame_size;
227    written_frames = sink->write(buffer, frames);
228    if (written_frames < 0) {
229        if (written_frames == (ssize_t)NEGOTIATE) {
230            ALOGE("out_write() write to pipe returned NEGOTIATE");
231
232            pthread_mutex_lock(&out->dev->lock);
233            out->dev->rsxSink->decStrong(buffer);
234            pthread_mutex_unlock(&out->dev->lock);
235
236            written_frames = 0;
237            return 0;
238        } else {
239            // write() returned UNDERRUN or WOULD_BLOCK, retry
240            ALOGE("out_write() write to pipe returned unexpected %16lx", written_frames);
241            written_frames = sink->write(buffer, frames);
242        }
243    }
244
245    pthread_mutex_lock(&out->dev->lock);
246
247    out->dev->rsxSink->decStrong(buffer);
248
249    pthread_mutex_unlock(&out->dev->lock);
250
251    if (written_frames < 0) {
252        ALOGE("out_write() failed writing to pipe with %16lx", written_frames);
253        return 0;
254    } else {
255        ALOGV("out_write() wrote %lu bytes)", written_frames * frame_size);
256        return written_frames * frame_size;
257    }
258}
259
260static int out_get_render_position(const struct audio_stream_out *stream,
261                                   uint32_t *dsp_frames)
262{
263    return -EINVAL;
264}
265
266static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
267{
268    return 0;
269}
270
271static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
272{
273    return 0;
274}
275
276static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
277                                        int64_t *timestamp)
278{
279    return -EINVAL;
280}
281
282/** audio_stream_in implementation **/
283static uint32_t in_get_sample_rate(const struct audio_stream *stream)
284{
285    const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream);
286    //ALOGV("in_get_sample_rate() returns %u", in->dev->config.rate);
287    return in->dev->config.rate;
288}
289
290static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
291{
292    return -ENOSYS;
293}
294
295static size_t in_get_buffer_size(const struct audio_stream *stream)
296{
297    const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream);
298    ALOGV("in_get_buffer_size() returns %u",
299            in->dev->config.period_size * audio_stream_frame_size(stream));
300    return in->dev->config.period_size * audio_stream_frame_size(stream);
301}
302
303static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
304{
305    return AUDIO_CHANNEL_IN_STEREO;
306}
307
308static audio_format_t in_get_format(const struct audio_stream *stream)
309{
310    return AUDIO_FORMAT_PCM_16_BIT;
311}
312
313static int in_set_format(struct audio_stream *stream, audio_format_t format)
314{
315    if (format != AUDIO_FORMAT_PCM_16_BIT) {
316        return -ENOSYS;
317    } else {
318        return 0;
319    }
320}
321
322static int in_standby(struct audio_stream *stream)
323{
324    ALOGI("in_standby()");
325    const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream);
326
327    pthread_mutex_lock(&in->dev->lock);
328
329    in->dev->input_standby = true;
330
331    pthread_mutex_unlock(&in->dev->lock);
332
333    return 0;
334}
335
336static int in_dump(const struct audio_stream *stream, int fd)
337{
338    return 0;
339}
340
341static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
342{
343    return 0;
344}
345
346static char * in_get_parameters(const struct audio_stream *stream,
347                                const char *keys)
348{
349    return strdup("");
350}
351
352static int in_set_gain(struct audio_stream_in *stream, float gain)
353{
354    return 0;
355}
356
357static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
358                       size_t bytes)
359{
360    //ALOGV("in_read bytes=%u", bytes);
361    ssize_t frames_read = -1977;
362    struct submix_stream_in *in = reinterpret_cast<struct submix_stream_in *>(stream);
363    const size_t frame_size = audio_stream_frame_size(&stream->common);
364    const size_t frames_to_read = bytes / frame_size;
365
366    pthread_mutex_lock(&in->dev->lock);
367
368    const bool output_standby_transition = (in->output_standby != in->dev->output_standby);
369    in->output_standby = in->dev->output_standby;
370
371    if (in->dev->input_standby || output_standby_transition) {
372        in->dev->input_standby = false;
373        // keep track of when we exit input standby (== first read == start "real recording")
374        // or when we start recording silence, and reset projected time
375        int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time);
376        if (rc == 0) {
377            in->read_counter_frames = 0;
378        }
379    }
380
381    in->read_counter_frames += frames_to_read;
382
383    MonoPipeReader* source = in->dev->rsxSource.get();
384    if (source != NULL) {
385        in->dev->rsxSource->incStrong(in);
386    } else {
387        ALOGE("no audio pipe yet we're trying to read!");
388        pthread_mutex_unlock(&in->dev->lock);
389        usleep((bytes / frame_size) * 1000000 / in_get_sample_rate(&stream->common));
390        memset(buffer, 0, bytes);
391        return bytes;
392    }
393
394    pthread_mutex_unlock(&in->dev->lock);
395
396    // read the data from the pipe (it's non blocking)
397    size_t remaining_frames = frames_to_read;
398    int attempts = 0;
399    char* buff = (char*)buffer;
400    while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
401        attempts++;
402        frames_read = source->read(buff, remaining_frames, AudioBufferProvider::kInvalidPTS);
403        if (frames_read > 0) {
404            remaining_frames -= frames_read;
405            buff += frames_read * frame_size;
406            //ALOGV("  in_read (att=%d) got %ld frames, remaining=%u",
407            //      attempts, frames_read, remaining_frames);
408        } else {
409            //ALOGE("  in_read read returned %ld", frames_read);
410            usleep(READ_ATTEMPT_SLEEP_MS * 1000);
411        }
412    }
413
414    // done using the source
415    pthread_mutex_lock(&in->dev->lock);
416
417    in->dev->rsxSource->decStrong(in);
418
419    pthread_mutex_unlock(&in->dev->lock);
420
421    if (remaining_frames > 0) {
422        ALOGV("  remaining_frames = %d", remaining_frames);
423        memset(((char*)buffer)+ bytes - (remaining_frames * frame_size), 0,
424                remaining_frames * frame_size);
425    }
426
427    // compute how much we need to sleep after reading the data by comparing the wall clock with
428    //   the projected time at which we should return.
429    struct timespec time_after_read;// wall clock after reading from the pipe
430    struct timespec record_duration;// observed record duration
431    int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read);
432    const uint32_t sample_rate = in_get_sample_rate(&stream->common);
433    if (rc == 0) {
434        // for how long have we been recording?
435        record_duration.tv_sec  = time_after_read.tv_sec - in->record_start_time.tv_sec;
436        record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec;
437        if (record_duration.tv_nsec < 0) {
438            record_duration.tv_sec--;
439            record_duration.tv_nsec += 1000000000;
440        }
441
442        // read_counter_frames contains the number of frames that have been read since the beginning
443        // of recording (including this call): it's converted to usec and compared to how long we've
444        // been recording for, which gives us how long we must wait to sync the projected recording
445        // time, and the observed recording time
446        long projected_vs_observed_offset_us =
447                ((int64_t)(in->read_counter_frames
448                            - (record_duration.tv_sec*sample_rate)))
449                        * 1000000 / sample_rate
450                - (record_duration.tv_nsec / 1000);
451
452        ALOGV("  record duration %5lds %3ldms, will wait: %7ldus",
453                record_duration.tv_sec, record_duration.tv_nsec/1000000,
454                projected_vs_observed_offset_us);
455        if (projected_vs_observed_offset_us > 0) {
456            usleep(projected_vs_observed_offset_us);
457        }
458    }
459
460
461    ALOGV("in_read returns %d", bytes);
462    return bytes;
463
464}
465
466static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
467{
468    return 0;
469}
470
471static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
472{
473    return 0;
474}
475
476static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
477{
478    return 0;
479}
480
481static int adev_open_output_stream(struct audio_hw_device *dev,
482                                   audio_io_handle_t handle,
483                                   audio_devices_t devices,
484                                   audio_output_flags_t flags,
485                                   struct audio_config *config,
486                                   struct audio_stream_out **stream_out)
487{
488    ALOGV("adev_open_output_stream()");
489    struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
490    struct submix_stream_out *out;
491    int ret;
492
493    out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out));
494    if (!out) {
495        ret = -ENOMEM;
496        goto err_open;
497    }
498
499    pthread_mutex_lock(&rsxadev->lock);
500
501    out->stream.common.get_sample_rate = out_get_sample_rate;
502    out->stream.common.set_sample_rate = out_set_sample_rate;
503    out->stream.common.get_buffer_size = out_get_buffer_size;
504    out->stream.common.get_channels = out_get_channels;
505    out->stream.common.get_format = out_get_format;
506    out->stream.common.set_format = out_set_format;
507    out->stream.common.standby = out_standby;
508    out->stream.common.dump = out_dump;
509    out->stream.common.set_parameters = out_set_parameters;
510    out->stream.common.get_parameters = out_get_parameters;
511    out->stream.common.add_audio_effect = out_add_audio_effect;
512    out->stream.common.remove_audio_effect = out_remove_audio_effect;
513    out->stream.get_latency = out_get_latency;
514    out->stream.set_volume = out_set_volume;
515    out->stream.write = out_write;
516    out->stream.get_render_position = out_get_render_position;
517    out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
518
519    config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
520    rsxadev->config.channel_mask = config->channel_mask;
521
522    if ((config->sample_rate != 48000) || (config->sample_rate != 44100)) {
523        config->sample_rate = DEFAULT_RATE_HZ;
524    }
525    rsxadev->config.rate = config->sample_rate;
526
527    config->format = AUDIO_FORMAT_PCM_16_BIT;
528    rsxadev->config.format = config->format;
529
530    rsxadev->config.period_size = 1024;
531    rsxadev->config.period_count = 4;
532    out->dev = rsxadev;
533
534    *stream_out = &out->stream;
535
536    // initialize pipe
537    {
538        ALOGV("  initializing pipe");
539        const NBAIO_Format format =
540                config->sample_rate == 48000 ? Format_SR48_C2_I16 : Format_SR44_1_C2_I16;
541        const NBAIO_Format offers[1] = {format};
542        size_t numCounterOffers = 0;
543        // creating a MonoPipe with optional blocking set to true.
544        MonoPipe* sink = new MonoPipe(MAX_PIPE_DEPTH_IN_FRAMES, format, true/*writeCanBlock*/);
545        ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers);
546        ALOG_ASSERT(index == 0);
547        MonoPipeReader* source = new MonoPipeReader(sink);
548        numCounterOffers = 0;
549        index = source->negotiate(offers, 1, NULL, numCounterOffers);
550        ALOG_ASSERT(index == 0);
551        rsxadev->rsxSink = sink;
552        rsxadev->rsxSource = source;
553    }
554
555    pthread_mutex_unlock(&rsxadev->lock);
556
557    return 0;
558
559err_open:
560    *stream_out = NULL;
561    return ret;
562}
563
564static void adev_close_output_stream(struct audio_hw_device *dev,
565                                     struct audio_stream_out *stream)
566{
567    ALOGV("adev_close_output_stream()");
568    struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
569
570    pthread_mutex_lock(&rsxadev->lock);
571
572    rsxadev->rsxSink.clear();
573    rsxadev->rsxSource.clear();
574    free(stream);
575
576    pthread_mutex_unlock(&rsxadev->lock);
577}
578
579static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
580{
581    return -ENOSYS;
582}
583
584static char * adev_get_parameters(const struct audio_hw_device *dev,
585                                  const char *keys)
586{
587    return strdup("");;
588}
589
590static int adev_init_check(const struct audio_hw_device *dev)
591{
592    ALOGI("adev_init_check()");
593    return 0;
594}
595
596static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
597{
598    return -ENOSYS;
599}
600
601static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
602{
603    return -ENOSYS;
604}
605
606static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
607{
608    return -ENOSYS;
609}
610
611static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
612{
613    return -ENOSYS;
614}
615
616static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
617{
618    return -ENOSYS;
619}
620
621static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
622{
623    return 0;
624}
625
626static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
627{
628    return -ENOSYS;
629}
630
631static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
632{
633    return -ENOSYS;
634}
635
636static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
637                                         const struct audio_config *config)
638{
639    //### TODO correlate this with pipe parameters
640    return 4096;
641}
642
643static int adev_open_input_stream(struct audio_hw_device *dev,
644                                  audio_io_handle_t handle,
645                                  audio_devices_t devices,
646                                  struct audio_config *config,
647                                  struct audio_stream_in **stream_in)
648{
649    ALOGI("adev_open_input_stream()");
650
651    struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
652    struct submix_stream_in *in;
653    int ret;
654
655    in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in));
656    if (!in) {
657        ret = -ENOMEM;
658        goto err_open;
659    }
660
661    pthread_mutex_lock(&rsxadev->lock);
662
663    in->stream.common.get_sample_rate = in_get_sample_rate;
664    in->stream.common.set_sample_rate = in_set_sample_rate;
665    in->stream.common.get_buffer_size = in_get_buffer_size;
666    in->stream.common.get_channels = in_get_channels;
667    in->stream.common.get_format = in_get_format;
668    in->stream.common.set_format = in_set_format;
669    in->stream.common.standby = in_standby;
670    in->stream.common.dump = in_dump;
671    in->stream.common.set_parameters = in_set_parameters;
672    in->stream.common.get_parameters = in_get_parameters;
673    in->stream.common.add_audio_effect = in_add_audio_effect;
674    in->stream.common.remove_audio_effect = in_remove_audio_effect;
675    in->stream.set_gain = in_set_gain;
676    in->stream.read = in_read;
677    in->stream.get_input_frames_lost = in_get_input_frames_lost;
678
679    config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
680    rsxadev->config.channel_mask = config->channel_mask;
681
682    if ((config->sample_rate != 48000) || (config->sample_rate != 44100)) {
683        config->sample_rate = DEFAULT_RATE_HZ;
684    }
685    rsxadev->config.rate = config->sample_rate;
686
687    config->format = AUDIO_FORMAT_PCM_16_BIT;
688    rsxadev->config.format = config->format;
689
690    rsxadev->config.period_size = 1024;
691    rsxadev->config.period_count = 4;
692
693    *stream_in = &in->stream;
694
695    in->dev = rsxadev;
696
697    in->read_counter_frames = 0;
698    in->output_standby = rsxadev->output_standby;
699
700    pthread_mutex_unlock(&rsxadev->lock);
701
702    return 0;
703
704err_open:
705    *stream_in = NULL;
706    return ret;
707}
708
709static void adev_close_input_stream(struct audio_hw_device *dev,
710                                   struct audio_stream_in *stream)
711{
712    ALOGV("adev_close_input_stream()");
713    struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev;
714
715    pthread_mutex_lock(&rsxadev->lock);
716
717    free(stream);
718
719    pthread_mutex_unlock(&rsxadev->lock);
720}
721
722static int adev_dump(const audio_hw_device_t *device, int fd)
723{
724    return 0;
725}
726
727static int adev_close(hw_device_t *device)
728{
729    ALOGI("adev_close()");
730    free(device);
731    return 0;
732}
733
734static int adev_open(const hw_module_t* module, const char* name,
735                     hw_device_t** device)
736{
737    ALOGI("adev_open(name=%s)", name);
738    struct submix_audio_device *rsxadev;
739
740    if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
741        return -EINVAL;
742
743    rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device));
744    if (!rsxadev)
745        return -ENOMEM;
746
747    rsxadev->device.common.tag = HARDWARE_DEVICE_TAG;
748    rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
749    rsxadev->device.common.module = (struct hw_module_t *) module;
750    rsxadev->device.common.close = adev_close;
751
752    rsxadev->device.init_check = adev_init_check;
753    rsxadev->device.set_voice_volume = adev_set_voice_volume;
754    rsxadev->device.set_master_volume = adev_set_master_volume;
755    rsxadev->device.get_master_volume = adev_get_master_volume;
756    rsxadev->device.set_master_mute = adev_set_master_mute;
757    rsxadev->device.get_master_mute = adev_get_master_mute;
758    rsxadev->device.set_mode = adev_set_mode;
759    rsxadev->device.set_mic_mute = adev_set_mic_mute;
760    rsxadev->device.get_mic_mute = adev_get_mic_mute;
761    rsxadev->device.set_parameters = adev_set_parameters;
762    rsxadev->device.get_parameters = adev_get_parameters;
763    rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size;
764    rsxadev->device.open_output_stream = adev_open_output_stream;
765    rsxadev->device.close_output_stream = adev_close_output_stream;
766    rsxadev->device.open_input_stream = adev_open_input_stream;
767    rsxadev->device.close_input_stream = adev_close_input_stream;
768    rsxadev->device.dump = adev_dump;
769
770    rsxadev->input_standby = true;
771    rsxadev->output_standby = true;
772
773    *device = &rsxadev->device.common;
774
775    return 0;
776}
777
778static struct hw_module_methods_t hal_module_methods = {
779    /* open */ adev_open,
780};
781
782struct audio_module HAL_MODULE_INFO_SYM = {
783    /* common */ {
784        /* tag */                HARDWARE_MODULE_TAG,
785        /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1,
786        /* hal_api_version */    HARDWARE_HAL_API_VERSION,
787        /* id */                 AUDIO_HARDWARE_MODULE_ID,
788        /* name */               "Wifi Display audio HAL",
789        /* author */             "The Android Open Source Project",
790        /* methods */            &hal_module_methods,
791        /* dso */                NULL,
792        /* reserved */           { 0 },
793    },
794};
795
796} //namespace android
797
798} //extern "C"
799