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