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