Home | History | Annotate | Download | only in audio
      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 
     26 #include <cutils/log.h>
     27 #include <cutils/properties.h>
     28 #include <cutils/str_parms.h>
     29 
     30 #include <hardware/audio.h>
     31 #include <hardware/hardware.h>
     32 
     33 #include <system/audio.h>
     34 
     35 #include <tinyalsa/asoundlib.h>
     36 
     37 #include <audio_utils/resampler.h>
     38 #include <audio_route/audio_route.h>
     39 
     40 #define PCM_CARD 1
     41 #define PCM_DEVICE 0
     42 #define PCM_DEVICE_SCO 2
     43 
     44 #define MIXER_CARD 1
     45 
     46 #define OUT_PERIOD_SIZE 512
     47 #define OUT_SHORT_PERIOD_COUNT 2
     48 #define OUT_LONG_PERIOD_COUNT 8
     49 #define OUT_SAMPLING_RATE 44100
     50 
     51 #define IN_PERIOD_SIZE 1024
     52 #define IN_PERIOD_SIZE_LOW_LATENCY 512
     53 #define IN_PERIOD_COUNT 2
     54 #define IN_SAMPLING_RATE 44100
     55 
     56 #define SCO_PERIOD_SIZE 256
     57 #define SCO_PERIOD_COUNT 4
     58 #define SCO_SAMPLING_RATE 8000
     59 
     60 /* minimum sleep time in out_write() when write threshold is not reached */
     61 #define MIN_WRITE_SLEEP_US 2000
     62 #define MAX_WRITE_SLEEP_US ((OUT_PERIOD_SIZE * OUT_SHORT_PERIOD_COUNT * 1000000) \
     63                                 / OUT_SAMPLING_RATE)
     64 
     65 enum {
     66     OUT_BUFFER_TYPE_UNKNOWN,
     67     OUT_BUFFER_TYPE_SHORT,
     68     OUT_BUFFER_TYPE_LONG,
     69 };
     70 
     71 struct pcm_config pcm_config_out = {
     72     .channels = 2,
     73     .rate = OUT_SAMPLING_RATE,
     74     .period_size = OUT_PERIOD_SIZE,
     75     .period_count = OUT_LONG_PERIOD_COUNT,
     76     .format = PCM_FORMAT_S16_LE,
     77     .start_threshold = OUT_PERIOD_SIZE * OUT_SHORT_PERIOD_COUNT,
     78 };
     79 
     80 struct pcm_config pcm_config_in = {
     81     .channels = 2,
     82     .rate = IN_SAMPLING_RATE,
     83     .period_size = IN_PERIOD_SIZE,
     84     .period_count = IN_PERIOD_COUNT,
     85     .format = PCM_FORMAT_S16_LE,
     86     .start_threshold = 1,
     87     .stop_threshold = (IN_PERIOD_SIZE * IN_PERIOD_COUNT),
     88 };
     89 
     90 struct pcm_config pcm_config_in_low_latency = {
     91     .channels = 2,
     92     .rate = IN_SAMPLING_RATE,
     93     .period_size = IN_PERIOD_SIZE_LOW_LATENCY,
     94     .period_count = IN_PERIOD_COUNT,
     95     .format = PCM_FORMAT_S16_LE,
     96     .start_threshold = 1,
     97     .stop_threshold = (IN_PERIOD_SIZE_LOW_LATENCY * IN_PERIOD_COUNT),
     98 };
     99 
    100 struct pcm_config pcm_config_sco = {
    101     .channels = 1,
    102     .rate = SCO_SAMPLING_RATE,
    103     .period_size = SCO_PERIOD_SIZE,
    104     .period_count = SCO_PERIOD_COUNT,
    105     .format = PCM_FORMAT_S16_LE,
    106 };
    107 
    108 struct audio_device {
    109     struct audio_hw_device hw_device;
    110 
    111     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    112     unsigned int out_device;
    113     unsigned int in_device;
    114     bool standby;
    115     bool mic_mute;
    116     struct audio_route *ar;
    117     int orientation;
    118     bool screen_off;
    119 
    120     struct stream_out *active_out;
    121     struct stream_in *active_in;
    122 };
    123 
    124 struct stream_out {
    125     struct audio_stream_out stream;
    126 
    127     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    128     struct pcm *pcm;
    129     struct pcm_config *pcm_config;
    130     bool standby;
    131     uint64_t written; /* total frames written, not cleared when entering standby */
    132 
    133     struct resampler_itfe *resampler;
    134     int16_t *buffer;
    135     size_t buffer_frames;
    136 
    137     int write_threshold;
    138     int cur_write_threshold;
    139     int buffer_type;
    140 
    141     struct audio_device *dev;
    142 };
    143 
    144 struct stream_in {
    145     struct audio_stream_in stream;
    146 
    147     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    148     struct pcm *pcm;
    149     struct pcm_config *pcm_config;          /* current configuration */
    150     struct pcm_config *pcm_config_non_sco;  /* configuration to return after SCO is done */
    151     bool standby;
    152 
    153     unsigned int requested_rate;
    154     struct resampler_itfe *resampler;
    155     struct resampler_buffer_provider buf_provider;
    156     int16_t *buffer;
    157     size_t buffer_size;
    158     size_t frames_in;
    159     int read_status;
    160 
    161     struct audio_device *dev;
    162 };
    163 
    164 enum {
    165     ORIENTATION_LANDSCAPE,
    166     ORIENTATION_PORTRAIT,
    167     ORIENTATION_SQUARE,
    168     ORIENTATION_UNDEFINED,
    169 };
    170 
    171 static uint32_t out_get_sample_rate(const struct audio_stream *stream);
    172 static size_t out_get_buffer_size(const struct audio_stream *stream);
    173 static audio_format_t out_get_format(const struct audio_stream *stream);
    174 static uint32_t in_get_sample_rate(const struct audio_stream *stream);
    175 static size_t in_get_buffer_size(const struct audio_stream *stream);
    176 static audio_format_t in_get_format(const struct audio_stream *stream);
    177 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
    178                                    struct resampler_buffer* buffer);
    179 static void release_buffer(struct resampler_buffer_provider *buffer_provider,
    180                                   struct resampler_buffer* buffer);
    181 
    182 /*
    183  * NOTE: when multiple mutexes have to be acquired, always take the
    184  * audio_device mutex first, followed by the stream_in and/or
    185  * stream_out mutexes.
    186  */
    187 
    188 /* Helper functions */
    189 
    190 static void select_devices(struct audio_device *adev)
    191 {
    192     int headphone_on;
    193     int speaker_on;
    194     int docked;
    195     int main_mic_on;
    196 
    197     headphone_on = adev->out_device & (AUDIO_DEVICE_OUT_WIRED_HEADSET |
    198                                     AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
    199     speaker_on = adev->out_device & AUDIO_DEVICE_OUT_SPEAKER;
    200     docked = adev->out_device & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
    201     main_mic_on = adev->in_device & AUDIO_DEVICE_IN_BUILTIN_MIC;
    202 
    203     audio_route_reset(adev->ar);
    204 
    205     if (speaker_on)
    206         audio_route_apply_path(adev->ar, "speaker");
    207     if (headphone_on)
    208         audio_route_apply_path(adev->ar, "headphone");
    209     if (docked)
    210         audio_route_apply_path(adev->ar, "dock");
    211     if (main_mic_on) {
    212         if (adev->orientation == ORIENTATION_LANDSCAPE)
    213             audio_route_apply_path(adev->ar, "main-mic-left");
    214         else
    215             audio_route_apply_path(adev->ar, "main-mic-top");
    216     }
    217 
    218     audio_route_update_mixer(adev->ar);
    219 
    220     ALOGV("hp=%c speaker=%c dock=%c main-mic=%c", headphone_on ? 'y' : 'n',
    221           speaker_on ? 'y' : 'n', docked ? 'y' : 'n', main_mic_on ? 'y' : 'n');
    222 }
    223 
    224 /* must be called with hw device and output stream mutexes locked */
    225 static void do_out_standby(struct stream_out *out)
    226 {
    227     struct audio_device *adev = out->dev;
    228 
    229     if (!out->standby) {
    230         pcm_close(out->pcm);
    231         out->pcm = NULL;
    232         adev->active_out = NULL;
    233         if (out->resampler) {
    234             release_resampler(out->resampler);
    235             out->resampler = NULL;
    236         }
    237         if (out->buffer) {
    238             free(out->buffer);
    239             out->buffer = NULL;
    240         }
    241         out->standby = true;
    242     }
    243 }
    244 
    245 /* must be called with hw device and input stream mutexes locked */
    246 static void do_in_standby(struct stream_in *in)
    247 {
    248     struct audio_device *adev = in->dev;
    249 
    250     if (!in->standby) {
    251         pcm_close(in->pcm);
    252         in->pcm = NULL;
    253         adev->active_in = NULL;
    254         if (in->resampler) {
    255             release_resampler(in->resampler);
    256             in->resampler = NULL;
    257         }
    258         if (in->buffer) {
    259             free(in->buffer);
    260             in->buffer = NULL;
    261         }
    262         in->standby = true;
    263     }
    264 }
    265 
    266 /* must be called with hw device and output stream mutexes locked */
    267 static int start_output_stream(struct stream_out *out)
    268 {
    269     struct audio_device *adev = out->dev;
    270     unsigned int device;
    271     int ret;
    272 
    273     /*
    274      * Due to the lack of sample rate converters in the SoC,
    275      * it greatly simplifies things to have only the main
    276      * (speaker/headphone) PCM or the BC SCO PCM open at
    277      * the same time.
    278      */
    279     if (adev->out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
    280         device = PCM_DEVICE_SCO;
    281         out->pcm_config = &pcm_config_sco;
    282     } else {
    283         device = PCM_DEVICE;
    284         out->pcm_config = &pcm_config_out;
    285         out->buffer_type = OUT_BUFFER_TYPE_UNKNOWN;
    286     }
    287 
    288     /*
    289      * All open PCMs can only use a single group of rates at once:
    290      * Group 1: 11.025, 22.05, 44.1
    291      * Group 2: 8, 16, 32, 48
    292      * Group 1 is used for digital audio playback since 44.1 is
    293      * the most common rate, but group 2 is required for SCO.
    294      */
    295     if (adev->active_in) {
    296         struct stream_in *in = adev->active_in;
    297         pthread_mutex_lock(&in->lock);
    298         if (((out->pcm_config->rate % 8000 == 0) &&
    299                  (in->pcm_config->rate % 8000) != 0) ||
    300                  ((out->pcm_config->rate % 11025 == 0) &&
    301                  (in->pcm_config->rate % 11025) != 0))
    302             do_in_standby(in);
    303         pthread_mutex_unlock(&in->lock);
    304     }
    305 
    306     out->pcm = pcm_open(PCM_CARD, device, PCM_OUT | PCM_NORESTART | PCM_MONOTONIC, out->pcm_config);
    307 
    308     if (out->pcm && !pcm_is_ready(out->pcm)) {
    309         ALOGE("pcm_open(out) failed: %s", pcm_get_error(out->pcm));
    310         pcm_close(out->pcm);
    311         return -ENOMEM;
    312     }
    313 
    314     /*
    315      * If the stream rate differs from the PCM rate, we need to
    316      * create a resampler.
    317      */
    318     if (out_get_sample_rate(&out->stream.common) != out->pcm_config->rate) {
    319         ret = create_resampler(out_get_sample_rate(&out->stream.common),
    320                                out->pcm_config->rate,
    321                                out->pcm_config->channels,
    322                                RESAMPLER_QUALITY_DEFAULT,
    323                                NULL,
    324                                &out->resampler);
    325         out->buffer_frames = (pcm_config_out.period_size * out->pcm_config->rate) /
    326                 out_get_sample_rate(&out->stream.common) + 1;
    327 
    328         out->buffer = malloc(pcm_frames_to_bytes(out->pcm, out->buffer_frames));
    329     }
    330 
    331     adev->active_out = out;
    332 
    333     return 0;
    334 }
    335 
    336 /* must be called with hw device and input stream mutexes locked */
    337 static int start_input_stream(struct stream_in *in)
    338 {
    339     struct audio_device *adev = in->dev;
    340     unsigned int device;
    341     int ret;
    342 
    343     /*
    344      * Due to the lack of sample rate converters in the SoC,
    345      * it greatly simplifies things to have only the main
    346      * mic PCM or the BC SCO PCM open at the same time.
    347      */
    348     if (adev->in_device & AUDIO_DEVICE_IN_ALL_SCO) {
    349         device = PCM_DEVICE_SCO;
    350         in->pcm_config = &pcm_config_sco;
    351     } else {
    352         device = PCM_DEVICE;
    353         in->pcm_config = in->pcm_config_non_sco;
    354     }
    355 
    356     /*
    357      * All open PCMs can only use a single group of rates at once:
    358      * Group 1: 11.025, 22.05, 44.1
    359      * Group 2: 8, 16, 32, 48
    360      * Group 1 is used for digital audio playback since 44.1 is
    361      * the most common rate, but group 2 is required for SCO.
    362      */
    363     if (adev->active_out) {
    364         struct stream_out *out = adev->active_out;
    365         pthread_mutex_lock(&out->lock);
    366         if (((in->pcm_config->rate % 8000 == 0) &&
    367                  (out->pcm_config->rate % 8000) != 0) ||
    368                  ((in->pcm_config->rate % 11025 == 0) &&
    369                  (out->pcm_config->rate % 11025) != 0))
    370             do_out_standby(out);
    371         pthread_mutex_unlock(&out->lock);
    372     }
    373 
    374     in->pcm = pcm_open(PCM_CARD, device, PCM_IN, in->pcm_config);
    375 
    376     if (in->pcm && !pcm_is_ready(in->pcm)) {
    377         ALOGE("pcm_open(in) failed: %s", pcm_get_error(in->pcm));
    378         pcm_close(in->pcm);
    379         return -ENOMEM;
    380     }
    381 
    382     /*
    383      * If the stream rate differs from the PCM rate, we need to
    384      * create a resampler.
    385      */
    386     if (in_get_sample_rate(&in->stream.common) != in->pcm_config->rate) {
    387         in->buf_provider.get_next_buffer = get_next_buffer;
    388         in->buf_provider.release_buffer = release_buffer;
    389 
    390         ret = create_resampler(in->pcm_config->rate,
    391                                in_get_sample_rate(&in->stream.common),
    392                                1,
    393                                RESAMPLER_QUALITY_DEFAULT,
    394                                &in->buf_provider,
    395                                &in->resampler);
    396     }
    397     in->buffer_size = pcm_frames_to_bytes(in->pcm,
    398                                           in->pcm_config->period_size);
    399     in->buffer = malloc(in->buffer_size);
    400     in->frames_in = 0;
    401 
    402     adev->active_in = in;
    403 
    404     return 0;
    405 }
    406 
    407 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
    408                                    struct resampler_buffer* buffer)
    409 {
    410     struct stream_in *in;
    411 
    412     if (buffer_provider == NULL || buffer == NULL)
    413         return -EINVAL;
    414 
    415     in = (struct stream_in *)((char *)buffer_provider -
    416                                    offsetof(struct stream_in, buf_provider));
    417 
    418     if (in->pcm == NULL) {
    419         buffer->raw = NULL;
    420         buffer->frame_count = 0;
    421         in->read_status = -ENODEV;
    422         return -ENODEV;
    423     }
    424 
    425     if (in->frames_in == 0) {
    426         in->read_status = pcm_read(in->pcm,
    427                                    (void*)in->buffer,
    428                                    in->buffer_size);
    429         if (in->read_status != 0) {
    430             ALOGE("get_next_buffer() pcm_read error %d", in->read_status);
    431             buffer->raw = NULL;
    432             buffer->frame_count = 0;
    433             return in->read_status;
    434         }
    435         in->frames_in = in->pcm_config->period_size;
    436         if (in->pcm_config->channels == 2) {
    437             unsigned int i;
    438 
    439             /* Discard right channel */
    440             for (i = 1; i < in->frames_in; i++)
    441                 in->buffer[i] = in->buffer[i * 2];
    442         }
    443     }
    444 
    445     buffer->frame_count = (buffer->frame_count > in->frames_in) ?
    446                                 in->frames_in : buffer->frame_count;
    447     buffer->i16 = in->buffer + (in->pcm_config->period_size - in->frames_in);
    448 
    449     return in->read_status;
    450 
    451 }
    452 
    453 static void release_buffer(struct resampler_buffer_provider *buffer_provider,
    454                                   struct resampler_buffer* buffer)
    455 {
    456     struct stream_in *in;
    457 
    458     if (buffer_provider == NULL || buffer == NULL)
    459         return;
    460 
    461     in = (struct stream_in *)((char *)buffer_provider -
    462                                    offsetof(struct stream_in, buf_provider));
    463 
    464     in->frames_in -= buffer->frame_count;
    465 }
    466 
    467 /* read_frames() reads frames from kernel driver, down samples to capture rate
    468  * if necessary and output the number of frames requested to the buffer specified */
    469 static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames)
    470 {
    471     ssize_t frames_wr = 0;
    472 
    473     while (frames_wr < frames) {
    474         size_t frames_rd = frames - frames_wr;
    475         if (in->resampler != NULL) {
    476             in->resampler->resample_from_provider(in->resampler,
    477                     (int16_t *)((char *)buffer +
    478                             frames_wr * audio_stream_in_frame_size(&in->stream)),
    479                     &frames_rd);
    480         } else {
    481             struct resampler_buffer buf = {
    482                     { raw : NULL, },
    483                     frame_count : frames_rd,
    484             };
    485             get_next_buffer(&in->buf_provider, &buf);
    486             if (buf.raw != NULL) {
    487                 memcpy((char *)buffer +
    488                            frames_wr * audio_stream_in_frame_size(&in->stream),
    489                         buf.raw,
    490                         buf.frame_count * audio_stream_in_frame_size(&in->stream));
    491                 frames_rd = buf.frame_count;
    492             }
    493             release_buffer(&in->buf_provider, &buf);
    494         }
    495         /* in->read_status is updated by getNextBuffer() also called by
    496          * in->resampler->resample_from_provider() */
    497         if (in->read_status != 0)
    498             return in->read_status;
    499 
    500         frames_wr += frames_rd;
    501     }
    502     return frames_wr;
    503 }
    504 
    505 /* API functions */
    506 
    507 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
    508 {
    509     return pcm_config_out.rate;
    510 }
    511 
    512 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
    513 {
    514     return -ENOSYS;
    515 }
    516 
    517 static size_t out_get_buffer_size(const struct audio_stream *stream)
    518 {
    519     return pcm_config_out.period_size *
    520                audio_stream_out_frame_size((const struct audio_stream_out *)stream);
    521 }
    522 
    523 static uint32_t out_get_channels(const struct audio_stream *stream)
    524 {
    525     return AUDIO_CHANNEL_OUT_STEREO;
    526 }
    527 
    528 static audio_format_t out_get_format(const struct audio_stream *stream)
    529 {
    530     return AUDIO_FORMAT_PCM_16_BIT;
    531 }
    532 
    533 static int out_set_format(struct audio_stream *stream, audio_format_t format)
    534 {
    535     return -ENOSYS;
    536 }
    537 
    538 static int out_standby(struct audio_stream *stream)
    539 {
    540     struct stream_out *out = (struct stream_out *)stream;
    541 
    542     pthread_mutex_lock(&out->dev->lock);
    543     pthread_mutex_lock(&out->lock);
    544     do_out_standby(out);
    545     pthread_mutex_unlock(&out->lock);
    546     pthread_mutex_unlock(&out->dev->lock);
    547 
    548     return 0;
    549 }
    550 
    551 static int out_dump(const struct audio_stream *stream, int fd)
    552 {
    553     return 0;
    554 }
    555 
    556 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
    557 {
    558     struct stream_out *out = (struct stream_out *)stream;
    559     struct audio_device *adev = out->dev;
    560     struct str_parms *parms;
    561     char value[32];
    562     int ret;
    563     unsigned int val;
    564 
    565     parms = str_parms_create_str(kvpairs);
    566 
    567     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
    568                             value, sizeof(value));
    569     pthread_mutex_lock(&adev->lock);
    570     if (ret >= 0) {
    571         val = atoi(value);
    572         if ((adev->out_device != val) && (val != 0)) {
    573             /*
    574              * If SCO is turned on/off, we need to put audio into standby
    575              * because SCO uses a different PCM.
    576              */
    577             if ((val & AUDIO_DEVICE_OUT_ALL_SCO) ^
    578                     (adev->out_device & AUDIO_DEVICE_OUT_ALL_SCO)) {
    579                 pthread_mutex_lock(&out->lock);
    580                 do_out_standby(out);
    581                 pthread_mutex_unlock(&out->lock);
    582             }
    583 
    584             adev->out_device = val;
    585             select_devices(adev);
    586         }
    587     }
    588     pthread_mutex_unlock(&adev->lock);
    589 
    590     str_parms_destroy(parms);
    591     return ret;
    592 }
    593 
    594 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
    595 {
    596     return strdup("");
    597 }
    598 
    599 static uint32_t out_get_latency(const struct audio_stream_out *stream)
    600 {
    601     struct stream_out *out = (struct stream_out *)stream;
    602     struct audio_device *adev = out->dev;
    603     size_t period_count;
    604 
    605     pthread_mutex_lock(&adev->lock);
    606 
    607     if (adev->screen_off && !adev->active_in && !(adev->out_device & AUDIO_DEVICE_OUT_ALL_SCO))
    608         period_count = OUT_LONG_PERIOD_COUNT;
    609     else
    610         period_count = OUT_SHORT_PERIOD_COUNT;
    611 
    612     pthread_mutex_unlock(&adev->lock);
    613 
    614     return (pcm_config_out.period_size * period_count * 1000) / pcm_config_out.rate;
    615 }
    616 
    617 static int out_set_volume(struct audio_stream_out *stream, float left,
    618                           float right)
    619 {
    620     return -ENOSYS;
    621 }
    622 
    623 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
    624                          size_t bytes)
    625 {
    626     int ret = 0;
    627     struct stream_out *out = (struct stream_out *)stream;
    628     struct audio_device *adev = out->dev;
    629     size_t frame_size = audio_stream_out_frame_size(stream);
    630     int16_t *in_buffer = (int16_t *)buffer;
    631     size_t in_frames = bytes / frame_size;
    632     size_t out_frames;
    633     int buffer_type;
    634     int kernel_frames;
    635     bool sco_on;
    636 
    637     /*
    638      * acquiring hw device mutex systematically is useful if a low
    639      * priority thread is waiting on the output stream mutex - e.g.
    640      * executing out_set_parameters() while holding the hw device
    641      * mutex
    642      */
    643     pthread_mutex_lock(&adev->lock);
    644     pthread_mutex_lock(&out->lock);
    645     if (out->standby) {
    646         ret = start_output_stream(out);
    647         if (ret != 0) {
    648             pthread_mutex_unlock(&adev->lock);
    649             goto exit;
    650         }
    651         out->standby = false;
    652     }
    653     buffer_type = (adev->screen_off && !adev->active_in) ?
    654             OUT_BUFFER_TYPE_LONG : OUT_BUFFER_TYPE_SHORT;
    655     sco_on = (adev->out_device & AUDIO_DEVICE_OUT_ALL_SCO);
    656     pthread_mutex_unlock(&adev->lock);
    657 
    658     /* detect changes in screen ON/OFF state and adapt buffer size
    659      * if needed. Do not change buffer size when routed to SCO device. */
    660     if (!sco_on && (buffer_type != out->buffer_type)) {
    661         size_t period_count;
    662 
    663         if (buffer_type == OUT_BUFFER_TYPE_LONG)
    664             period_count = OUT_LONG_PERIOD_COUNT;
    665         else
    666             period_count = OUT_SHORT_PERIOD_COUNT;
    667 
    668         out->write_threshold = out->pcm_config->period_size * period_count;
    669         /* reset current threshold if exiting standby */
    670         if (out->buffer_type == OUT_BUFFER_TYPE_UNKNOWN)
    671             out->cur_write_threshold = out->write_threshold;
    672         out->buffer_type = buffer_type;
    673     }
    674 
    675     /* Reduce number of channels, if necessary */
    676     if (audio_channel_count_from_out_mask(out_get_channels(&stream->common)) >
    677                  (int)out->pcm_config->channels) {
    678         unsigned int i;
    679 
    680         /* Discard right channel */
    681         for (i = 1; i < in_frames; i++)
    682             in_buffer[i] = in_buffer[i * 2];
    683 
    684         /* The frame size is now half */
    685         frame_size /= 2;
    686     }
    687 
    688     /* Change sample rate, if necessary */
    689     if (out_get_sample_rate(&stream->common) != out->pcm_config->rate) {
    690         out_frames = out->buffer_frames;
    691         out->resampler->resample_from_input(out->resampler,
    692                                             in_buffer, &in_frames,
    693                                             out->buffer, &out_frames);
    694         in_buffer = out->buffer;
    695     } else {
    696         out_frames = in_frames;
    697     }
    698 
    699     if (!sco_on) {
    700         int total_sleep_time_us = 0;
    701         size_t period_size = out->pcm_config->period_size;
    702 
    703         /* do not allow more than out->cur_write_threshold frames in kernel
    704          * pcm driver buffer */
    705         do {
    706             struct timespec time_stamp;
    707             if (pcm_get_htimestamp(out->pcm,
    708                                    (unsigned int *)&kernel_frames,
    709                                    &time_stamp) < 0)
    710                 break;
    711             kernel_frames = pcm_get_buffer_size(out->pcm) - kernel_frames;
    712 
    713             if (kernel_frames > out->cur_write_threshold) {
    714                 int sleep_time_us =
    715                     (int)(((int64_t)(kernel_frames - out->cur_write_threshold)
    716                                     * 1000000) / out->pcm_config->rate);
    717                 if (sleep_time_us < MIN_WRITE_SLEEP_US)
    718                     break;
    719                 total_sleep_time_us += sleep_time_us;
    720                 if (total_sleep_time_us > MAX_WRITE_SLEEP_US) {
    721                     ALOGW("out_write() limiting sleep time %d to %d",
    722                           total_sleep_time_us, MAX_WRITE_SLEEP_US);
    723                     sleep_time_us = MAX_WRITE_SLEEP_US -
    724                                         (total_sleep_time_us - sleep_time_us);
    725                 }
    726                 usleep(sleep_time_us);
    727             }
    728 
    729         } while ((kernel_frames > out->cur_write_threshold) &&
    730                 (total_sleep_time_us <= MAX_WRITE_SLEEP_US));
    731 
    732         /* do not allow abrupt changes on buffer size. Increasing/decreasing
    733          * the threshold by steps of 1/4th of the buffer size keeps the write
    734          * time within a reasonable range during transitions.
    735          * Also reset current threshold just above current filling status when
    736          * kernel buffer is really depleted to allow for smooth catching up with
    737          * target threshold.
    738          */
    739         if (out->cur_write_threshold > out->write_threshold) {
    740             out->cur_write_threshold -= period_size / 4;
    741             if (out->cur_write_threshold < out->write_threshold) {
    742                 out->cur_write_threshold = out->write_threshold;
    743             }
    744         } else if (out->cur_write_threshold < out->write_threshold) {
    745             out->cur_write_threshold += period_size / 4;
    746             if (out->cur_write_threshold > out->write_threshold) {
    747                 out->cur_write_threshold = out->write_threshold;
    748             }
    749         } else if ((kernel_frames < out->write_threshold) &&
    750             ((out->write_threshold - kernel_frames) >
    751                 (int)(period_size * OUT_SHORT_PERIOD_COUNT))) {
    752             out->cur_write_threshold = (kernel_frames / period_size + 1) * period_size;
    753             out->cur_write_threshold += period_size / 4;
    754         }
    755     }
    756 
    757     ret = pcm_write(out->pcm, in_buffer, out_frames * frame_size);
    758     if (ret == -EPIPE) {
    759         /* In case of underrun, don't sleep since we want to catch up asap */
    760         pthread_mutex_unlock(&out->lock);
    761         return ret;
    762     }
    763     if (ret == 0) {
    764         out->written += out_frames;
    765     }
    766 
    767 exit:
    768     pthread_mutex_unlock(&out->lock);
    769 
    770     if (ret != 0) {
    771         usleep(bytes * 1000000 / audio_stream_out_frame_size(&stream->common) /
    772                out_get_sample_rate(&stream->common));
    773     }
    774 
    775     return bytes;
    776 }
    777 
    778 static int out_get_render_position(const struct audio_stream_out *stream,
    779                                    uint32_t *dsp_frames)
    780 {
    781     return -EINVAL;
    782 }
    783 
    784 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
    785 {
    786     return 0;
    787 }
    788 
    789 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
    790 {
    791     return 0;
    792 }
    793 
    794 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
    795                                         int64_t *timestamp)
    796 {
    797     return -EINVAL;
    798 }
    799 
    800 static int out_get_presentation_position(const struct audio_stream_out *stream,
    801                                    uint64_t *frames, struct timespec *timestamp)
    802 {
    803     struct stream_out *out = (struct stream_out *)stream;
    804     int ret = -1;
    805 
    806     pthread_mutex_lock(&out->lock);
    807 
    808     size_t avail;
    809     if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
    810         size_t kernel_buffer_size = out->pcm_config->period_size * out->pcm_config->period_count;
    811         // FIXME This calculation is incorrect if there is buffering after app processor
    812         int64_t signed_frames = out->written - kernel_buffer_size + avail;
    813         // It would be unusual for this value to be negative, but check just in case ...
    814         if (signed_frames >= 0) {
    815             *frames = signed_frames;
    816             ret = 0;
    817         }
    818     }
    819 
    820     pthread_mutex_unlock(&out->lock);
    821 
    822     return ret;
    823 }
    824 
    825 /** audio_stream_in implementation **/
    826 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
    827 {
    828     struct stream_in *in = (struct stream_in *)stream;
    829 
    830     return in->requested_rate;
    831 }
    832 
    833 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
    834 {
    835     return 0;
    836 }
    837 
    838 static size_t in_get_buffer_size(const struct audio_stream *stream)
    839 {
    840     struct stream_in *in = (struct stream_in *)stream;
    841     size_t size;
    842 
    843     /*
    844      * take resampling into account and return the closest majoring
    845      * multiple of 16 frames, as audioflinger expects audio buffers to
    846      * be a multiple of 16 frames
    847      */
    848     size = (in->pcm_config->period_size * in_get_sample_rate(stream)) /
    849             in->pcm_config->rate;
    850     size = ((size + 15) / 16) * 16;
    851 
    852     return size * audio_stream_in_frame_size(&in->stream);
    853 }
    854 
    855 static uint32_t in_get_channels(const struct audio_stream *stream)
    856 {
    857     return AUDIO_CHANNEL_IN_MONO;
    858 }
    859 
    860 static audio_format_t in_get_format(const struct audio_stream *stream)
    861 {
    862     return AUDIO_FORMAT_PCM_16_BIT;
    863 }
    864 
    865 static int in_set_format(struct audio_stream *stream, audio_format_t format)
    866 {
    867     return -ENOSYS;
    868 }
    869 
    870 static int in_standby(struct audio_stream *stream)
    871 {
    872     struct stream_in *in = (struct stream_in *)stream;
    873 
    874     pthread_mutex_lock(&in->dev->lock);
    875     pthread_mutex_lock(&in->lock);
    876     do_in_standby(in);
    877     pthread_mutex_unlock(&in->lock);
    878     pthread_mutex_unlock(&in->dev->lock);
    879 
    880     return 0;
    881 }
    882 
    883 static int in_dump(const struct audio_stream *stream, int fd)
    884 {
    885     return 0;
    886 }
    887 
    888 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
    889 {
    890     struct stream_in *in = (struct stream_in *)stream;
    891     struct audio_device *adev = in->dev;
    892     struct str_parms *parms;
    893     char value[32];
    894     int ret;
    895     unsigned int val;
    896 
    897     parms = str_parms_create_str(kvpairs);
    898 
    899     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
    900                             value, sizeof(value));
    901     pthread_mutex_lock(&adev->lock);
    902     if (ret >= 0) {
    903         val = atoi(value) & ~AUDIO_DEVICE_BIT_IN;
    904         if ((adev->in_device != val) && (val != 0)) {
    905             /*
    906              * If SCO is turned on/off, we need to put audio into standby
    907              * because SCO uses a different PCM.
    908              */
    909             if ((val & AUDIO_DEVICE_IN_ALL_SCO) ^
    910                     (adev->in_device & AUDIO_DEVICE_IN_ALL_SCO)) {
    911                 pthread_mutex_lock(&in->lock);
    912                 do_in_standby(in);
    913                 pthread_mutex_unlock(&in->lock);
    914             }
    915 
    916             adev->in_device = val;
    917             select_devices(adev);
    918         }
    919     }
    920     pthread_mutex_unlock(&adev->lock);
    921 
    922     str_parms_destroy(parms);
    923     return ret;
    924 }
    925 
    926 static char * in_get_parameters(const struct audio_stream *stream,
    927                                 const char *keys)
    928 {
    929     return strdup("");
    930 }
    931 
    932 static int in_set_gain(struct audio_stream_in *stream, float gain)
    933 {
    934     return 0;
    935 }
    936 
    937 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
    938                        size_t bytes)
    939 {
    940     int ret = 0;
    941     struct stream_in *in = (struct stream_in *)stream;
    942     struct audio_device *adev = in->dev;
    943     size_t frames_rq = bytes / audio_stream_in_frame_size(stream);
    944 
    945     /*
    946      * acquiring hw device mutex systematically is useful if a low
    947      * priority thread is waiting on the input stream mutex - e.g.
    948      * executing in_set_parameters() while holding the hw device
    949      * mutex
    950      */
    951     pthread_mutex_lock(&adev->lock);
    952     pthread_mutex_lock(&in->lock);
    953     if (in->standby) {
    954         ret = start_input_stream(in);
    955         if (ret == 0)
    956             in->standby = 0;
    957     }
    958     pthread_mutex_unlock(&adev->lock);
    959 
    960     if (ret < 0)
    961         goto exit;
    962 
    963     /*if (in->num_preprocessors != 0) {
    964         ret = process_frames(in, buffer, frames_rq);
    965     } else */if (in->resampler != NULL) {
    966         ret = read_frames(in, buffer, frames_rq);
    967     } else if (in->pcm_config->channels == 2) {
    968         /*
    969          * If the PCM is stereo, capture twice as many frames and
    970          * discard the right channel.
    971          */
    972         unsigned int i;
    973         int16_t *in_buffer = (int16_t *)buffer;
    974 
    975         ret = pcm_read(in->pcm, in->buffer, bytes * 2);
    976 
    977         /* Discard right channel */
    978         for (i = 0; i < frames_rq; i++)
    979             in_buffer[i] = in->buffer[i * 2];
    980     } else {
    981         ret = pcm_read(in->pcm, buffer, bytes);
    982     }
    983 
    984     if (ret > 0)
    985         ret = 0;
    986 
    987     /*
    988      * Instead of writing zeroes here, we could trust the hardware
    989      * to always provide zeroes when muted.
    990      */
    991     if (ret == 0 && adev->mic_mute)
    992         memset(buffer, 0, bytes);
    993 
    994 exit:
    995     if (ret < 0)
    996         usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
    997                in_get_sample_rate(&stream->common));
    998 
    999     pthread_mutex_unlock(&in->lock);
   1000     return bytes;
   1001 }
   1002 
   1003 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
   1004 {
   1005     return 0;
   1006 }
   1007 
   1008 static int in_add_audio_effect(const struct audio_stream *stream,
   1009                                effect_handle_t effect)
   1010 {
   1011     return 0;
   1012 }
   1013 
   1014 static int in_remove_audio_effect(const struct audio_stream *stream,
   1015                                   effect_handle_t effect)
   1016 {
   1017     return 0;
   1018 }
   1019 
   1020 
   1021 static int adev_open_output_stream(struct audio_hw_device *dev,
   1022                                    audio_io_handle_t handle,
   1023                                    audio_devices_t devices,
   1024                                    audio_output_flags_t flags,
   1025                                    struct audio_config *config,
   1026                                    struct audio_stream_out **stream_out,
   1027                                    const char *address __unused)
   1028 {
   1029     struct audio_device *adev = (struct audio_device *)dev;
   1030     struct stream_out *out;
   1031     int ret;
   1032 
   1033     out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
   1034     if (!out)
   1035         return -ENOMEM;
   1036 
   1037     out->stream.common.get_sample_rate = out_get_sample_rate;
   1038     out->stream.common.set_sample_rate = out_set_sample_rate;
   1039     out->stream.common.get_buffer_size = out_get_buffer_size;
   1040     out->stream.common.get_channels = out_get_channels;
   1041     out->stream.common.get_format = out_get_format;
   1042     out->stream.common.set_format = out_set_format;
   1043     out->stream.common.standby = out_standby;
   1044     out->stream.common.dump = out_dump;
   1045     out->stream.common.set_parameters = out_set_parameters;
   1046     out->stream.common.get_parameters = out_get_parameters;
   1047     out->stream.common.add_audio_effect = out_add_audio_effect;
   1048     out->stream.common.remove_audio_effect = out_remove_audio_effect;
   1049     out->stream.get_latency = out_get_latency;
   1050     out->stream.set_volume = out_set_volume;
   1051     out->stream.write = out_write;
   1052     out->stream.get_render_position = out_get_render_position;
   1053     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
   1054     out->stream.get_presentation_position = out_get_presentation_position;
   1055 
   1056     out->dev = adev;
   1057 
   1058     config->format = out_get_format(&out->stream.common);
   1059     config->channel_mask = out_get_channels(&out->stream.common);
   1060     config->sample_rate = out_get_sample_rate(&out->stream.common);
   1061 
   1062     out->standby = true;
   1063     /* out->written = 0; by calloc() */
   1064 
   1065     *stream_out = &out->stream;
   1066     return 0;
   1067 
   1068 err_open:
   1069     free(out);
   1070     *stream_out = NULL;
   1071     return ret;
   1072 }
   1073 
   1074 static void adev_close_output_stream(struct audio_hw_device *dev,
   1075                                      struct audio_stream_out *stream)
   1076 {
   1077     out_standby(&stream->common);
   1078     free(stream);
   1079 }
   1080 
   1081 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
   1082 {
   1083     struct audio_device *adev = (struct audio_device *)dev;
   1084     struct str_parms *parms;
   1085     char *str;
   1086     char value[32];
   1087     int ret;
   1088 
   1089     parms = str_parms_create_str(kvpairs);
   1090     ret = str_parms_get_str(parms, "orientation", value, sizeof(value));
   1091     if (ret >= 0) {
   1092         int orientation;
   1093 
   1094         if (strcmp(value, "landscape") == 0)
   1095             orientation = ORIENTATION_LANDSCAPE;
   1096         else if (strcmp(value, "portrait") == 0)
   1097             orientation = ORIENTATION_PORTRAIT;
   1098         else if (strcmp(value, "square") == 0)
   1099             orientation = ORIENTATION_SQUARE;
   1100         else
   1101             orientation = ORIENTATION_UNDEFINED;
   1102 
   1103         pthread_mutex_lock(&adev->lock);
   1104         if (orientation != adev->orientation) {
   1105             adev->orientation = orientation;
   1106             /*
   1107              * Orientation changes can occur with the input device
   1108              * closed so we must call select_devices() here to set
   1109              * up the mixer. This is because select_devices() will
   1110              * not be called when the input device is opened if no
   1111              * other input parameter is changed.
   1112              */
   1113             select_devices(adev);
   1114         }
   1115         pthread_mutex_unlock(&adev->lock);
   1116     }
   1117 
   1118     ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
   1119     if (ret >= 0) {
   1120         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
   1121             adev->screen_off = false;
   1122         else
   1123             adev->screen_off = true;
   1124     }
   1125 
   1126     str_parms_destroy(parms);
   1127     return ret;
   1128 }
   1129 
   1130 static char * adev_get_parameters(const struct audio_hw_device *dev,
   1131                                   const char *keys)
   1132 {
   1133     return strdup("");
   1134 }
   1135 
   1136 static int adev_init_check(const struct audio_hw_device *dev)
   1137 {
   1138     return 0;
   1139 }
   1140 
   1141 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
   1142 {
   1143     return -ENOSYS;
   1144 }
   1145 
   1146 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
   1147 {
   1148     return -ENOSYS;
   1149 }
   1150 
   1151 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
   1152 {
   1153     return 0;
   1154 }
   1155 
   1156 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
   1157 {
   1158     struct audio_device *adev = (struct audio_device *)dev;
   1159 
   1160     adev->mic_mute = state;
   1161 
   1162     return 0;
   1163 }
   1164 
   1165 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
   1166 {
   1167     struct audio_device *adev = (struct audio_device *)dev;
   1168 
   1169     *state = adev->mic_mute;
   1170 
   1171     return 0;
   1172 }
   1173 
   1174 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
   1175                                          const struct audio_config *config)
   1176 {
   1177     size_t size;
   1178 
   1179     /*
   1180      * take resampling into account and return the closest majoring
   1181      * multiple of 16 frames, as audioflinger expects audio buffers to
   1182      * be a multiple of 16 frames
   1183      */
   1184     size = (pcm_config_in.period_size * config->sample_rate) / pcm_config_in.rate;
   1185     size = ((size + 15) / 16) * 16;
   1186 
   1187     return (size * audio_channel_count_from_in_mask(config->channel_mask) *
   1188                 audio_bytes_per_sample(config->format));
   1189 }
   1190 
   1191 static int adev_open_input_stream(struct audio_hw_device *dev,
   1192                                   audio_io_handle_t handle,
   1193                                   audio_devices_t devices,
   1194                                   struct audio_config *config,
   1195                                   struct audio_stream_in **stream_in,
   1196                                   audio_input_flags_t flags,
   1197                                   const char *address __unused,
   1198                                   audio_source_t source __unused)
   1199 {
   1200     struct audio_device *adev = (struct audio_device *)dev;
   1201     struct stream_in *in;
   1202     int ret;
   1203 
   1204     *stream_in = NULL;
   1205 
   1206     /* Respond with a request for mono if a different format is given. */
   1207     if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
   1208         config->channel_mask = AUDIO_CHANNEL_IN_MONO;
   1209         return -EINVAL;
   1210     }
   1211 
   1212     in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
   1213     if (!in)
   1214         return -ENOMEM;
   1215 
   1216     in->stream.common.get_sample_rate = in_get_sample_rate;
   1217     in->stream.common.set_sample_rate = in_set_sample_rate;
   1218     in->stream.common.get_buffer_size = in_get_buffer_size;
   1219     in->stream.common.get_channels = in_get_channels;
   1220     in->stream.common.get_format = in_get_format;
   1221     in->stream.common.set_format = in_set_format;
   1222     in->stream.common.standby = in_standby;
   1223     in->stream.common.dump = in_dump;
   1224     in->stream.common.set_parameters = in_set_parameters;
   1225     in->stream.common.get_parameters = in_get_parameters;
   1226     in->stream.common.add_audio_effect = in_add_audio_effect;
   1227     in->stream.common.remove_audio_effect = in_remove_audio_effect;
   1228     in->stream.set_gain = in_set_gain;
   1229     in->stream.read = in_read;
   1230     in->stream.get_input_frames_lost = in_get_input_frames_lost;
   1231 
   1232     in->dev = adev;
   1233     in->standby = true;
   1234     in->requested_rate = config->sample_rate;
   1235     /* default PCM config */
   1236     in->pcm_config = (config->sample_rate == IN_SAMPLING_RATE) && (flags & AUDIO_INPUT_FLAG_FAST) ?
   1237             &pcm_config_in_low_latency : &pcm_config_in;
   1238     in->pcm_config_non_sco = in->pcm_config;
   1239 
   1240     *stream_in = &in->stream;
   1241     return 0;
   1242 }
   1243 
   1244 static void adev_close_input_stream(struct audio_hw_device *dev,
   1245                                    struct audio_stream_in *stream)
   1246 {
   1247     struct stream_in *in = (struct stream_in *)stream;
   1248 
   1249     in_standby(&stream->common);
   1250     free(stream);
   1251 }
   1252 
   1253 static int adev_dump(const audio_hw_device_t *device, int fd)
   1254 {
   1255     return 0;
   1256 }
   1257 
   1258 static int adev_close(hw_device_t *device)
   1259 {
   1260     struct audio_device *adev = (struct audio_device *)device;
   1261 
   1262     audio_route_free(adev->ar);
   1263 
   1264     free(device);
   1265     return 0;
   1266 }
   1267 
   1268 static int adev_open(const hw_module_t* module, const char* name,
   1269                      hw_device_t** device)
   1270 {
   1271     struct audio_device *adev;
   1272     int ret;
   1273 
   1274     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
   1275         return -EINVAL;
   1276 
   1277     adev = calloc(1, sizeof(struct audio_device));
   1278     if (!adev)
   1279         return -ENOMEM;
   1280 
   1281     adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
   1282     adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
   1283     adev->hw_device.common.module = (struct hw_module_t *) module;
   1284     adev->hw_device.common.close = adev_close;
   1285 
   1286     adev->hw_device.init_check = adev_init_check;
   1287     adev->hw_device.set_voice_volume = adev_set_voice_volume;
   1288     adev->hw_device.set_master_volume = adev_set_master_volume;
   1289     adev->hw_device.set_mode = adev_set_mode;
   1290     adev->hw_device.set_mic_mute = adev_set_mic_mute;
   1291     adev->hw_device.get_mic_mute = adev_get_mic_mute;
   1292     adev->hw_device.set_parameters = adev_set_parameters;
   1293     adev->hw_device.get_parameters = adev_get_parameters;
   1294     adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
   1295     adev->hw_device.open_output_stream = adev_open_output_stream;
   1296     adev->hw_device.close_output_stream = adev_close_output_stream;
   1297     adev->hw_device.open_input_stream = adev_open_input_stream;
   1298     adev->hw_device.close_input_stream = adev_close_input_stream;
   1299     adev->hw_device.dump = adev_dump;
   1300 
   1301     adev->ar = audio_route_init(MIXER_CARD, NULL);
   1302     adev->orientation = ORIENTATION_UNDEFINED;
   1303     adev->out_device = AUDIO_DEVICE_OUT_SPEAKER;
   1304     adev->in_device = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN;
   1305 
   1306     *device = &adev->hw_device.common;
   1307 
   1308     return 0;
   1309 }
   1310 
   1311 static struct hw_module_methods_t hal_module_methods = {
   1312     .open = adev_open,
   1313 };
   1314 
   1315 struct audio_module HAL_MODULE_INFO_SYM = {
   1316     .common = {
   1317         .tag = HARDWARE_MODULE_TAG,
   1318         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
   1319         .hal_api_version = HARDWARE_HAL_API_VERSION,
   1320         .id = AUDIO_HARDWARE_MODULE_ID,
   1321         .name = "Grouper audio HW HAL",
   1322         .author = "The Android Open Source Project",
   1323         .methods = &hal_module_methods,
   1324     },
   1325 };
   1326