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 #include <fcntl.h>
     26 
     27 #include <cutils/log.h>
     28 #include <cutils/properties.h>
     29 #include <cutils/str_parms.h>
     30 
     31 #include <hardware/audio.h>
     32 #include <hardware/hardware.h>
     33 
     34 #include <linux/videodev2.h>
     35 #include <videodev2_exynos_media.h>
     36 
     37 #include <system/audio.h>
     38 
     39 #include <tinyalsa/asoundlib.h>
     40 
     41 #include <audio_utils/resampler.h>
     42 #include <audio_route/audio_route.h>
     43 
     44 #include <BubbleLevel.h>
     45 
     46 #include <eS305VoiceProcessing.h>
     47 
     48 #define PCM_CARD 0
     49 #define PCM_CARD_SPDIF 1
     50 #define PCM_TOTAL 2
     51 
     52 #define PCM_DEVICE 0
     53 #define PCM_DEVICE_DEEP 1
     54 #define PCM_DEVICE_VOICE 2
     55 #define PCM_DEVICE_SCO 3
     56 
     57 #define MIXER_CARD 0
     58 
     59 /* duration in ms of volume ramp applied when starting capture to remove plop */
     60 #define CAPTURE_START_RAMP_MS 100
     61 
     62 /* default sampling for HDMI multichannel output */
     63 #define HDMI_MULTI_DEFAULT_SAMPLING_RATE  44100
     64 /* maximum number of channel mask configurations supported. Currently the primary
     65  * output only supports 1 (stereo) and the multi channel HDMI output 2 (5.1 and 7.1) */
     66 #define MAX_SUPPORTED_CHANNEL_MASKS 2
     67 
     68 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
     69 
     70 struct pcm_config pcm_config = {
     71     .channels = 2,
     72     .rate = 44100,
     73     .period_size = 256,
     74     .period_count = 2,
     75     .format = PCM_FORMAT_S16_LE,
     76 };
     77 
     78 struct pcm_config pcm_config_in = {
     79     .channels = 2,
     80     .rate = 44100,
     81     .period_size = 1024,
     82     .period_count = 2,
     83     .format = PCM_FORMAT_S16_LE,
     84 };
     85 
     86 struct pcm_config pcm_config_sco = {
     87     .channels = 1,
     88     .rate = 8000,
     89     .period_size = 128,
     90     .period_count = 2,
     91     .format = PCM_FORMAT_S16_LE,
     92 };
     93 
     94 struct pcm_config pcm_config_deep = {
     95     .channels = 2,
     96     .rate = 44100,
     97     /* FIXME This is an arbitrary number, may change.
     98      * Dynamic configuration based on screen on/off is not implemented;
     99      * let's see what power consumption is first to see if necessary.
    100      */
    101     .period_size = 8192,
    102     .period_count = 2,
    103     .format = PCM_FORMAT_S16_LE,
    104 };
    105 
    106 struct pcm_config pcm_config_hdmi_multi = {
    107     .channels = 6, /* changed when the stream is opened */
    108     .rate = HDMI_MULTI_DEFAULT_SAMPLING_RATE,
    109     .period_size = 1024,
    110     .period_count = 4,
    111     .format = PCM_FORMAT_S16_LE,
    112 };
    113 
    114 enum output_type {
    115     OUTPUT_DEEP_BUF,      // deep PCM buffers output stream
    116     OUTPUT_LOW_LATENCY,   // low latency output stream
    117     OUTPUT_HDMI,          // HDMI multi channel
    118     OUTPUT_TOTAL
    119 };
    120 
    121 struct audio_device {
    122     struct audio_hw_device hw_device;
    123 
    124     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    125     audio_devices_t out_device; /* "or" of stream_out.device for all active output streams */
    126     audio_devices_t in_device;
    127     bool mic_mute;
    128     struct audio_route *ar;
    129     audio_source_t input_source;
    130     int cur_route_id;     /* current route ID: combination of input source
    131                            * and output device IDs */
    132     struct pcm *pcm_voice_out;
    133     struct pcm *pcm_sco_out;
    134     struct pcm *pcm_voice_in;
    135     struct pcm *pcm_sco_in;
    136     int es305_preset;
    137     int es305_new_mode;
    138     int es305_mode;
    139     int hdmi_drv_fd;
    140     struct bubble_level *bubble_level;
    141     audio_channel_mask_t in_channel_mask;
    142     unsigned int sco_on_count;
    143 
    144     struct stream_out *outputs[OUTPUT_TOTAL];
    145 };
    146 
    147 struct stream_out {
    148     struct audio_stream_out stream;
    149 
    150     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    151     struct pcm *pcm[PCM_TOTAL];
    152     struct pcm_config config;
    153     unsigned int pcm_device;
    154     bool standby; /* true if all PCMs are inactive */
    155     audio_devices_t device;
    156     /* FIXME: when HDMI multichannel output is active, other outputs must be disabled as
    157      * HDMI and WM1811 share the same I2S. This means that notifications and other sounds are
    158      * silent when watching a 5.1 movie. */
    159     bool disabled;
    160     audio_channel_mask_t channel_mask;
    161     /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
    162     audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
    163     bool muted;
    164 
    165     struct audio_device *dev;
    166 };
    167 
    168 struct stream_in {
    169     struct audio_stream_in stream;
    170 
    171     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    172     struct pcm *pcm;
    173     bool standby;
    174 
    175     unsigned int requested_rate;
    176     struct resampler_itfe *resampler;
    177     struct resampler_buffer_provider buf_provider;
    178     int16_t *buffer;
    179     size_t frames_in;
    180     int read_status;
    181     audio_source_t input_source;
    182     audio_io_handle_t io_handle;
    183     audio_devices_t device;
    184     uint16_t ramp_vol;
    185     uint16_t ramp_step;
    186     size_t  ramp_frames;
    187     audio_channel_mask_t channel_mask;
    188 
    189     struct audio_device *dev;
    190 };
    191 
    192 #define STRING_TO_ENUM(string) { #string, string }
    193 
    194 struct string_to_enum {
    195     const char *name;
    196     uint32_t value;
    197 };
    198 
    199 const struct string_to_enum out_channels_name_to_enum_table[] = {
    200     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
    201     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
    202     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
    203 };
    204 
    205 enum {
    206     OUT_DEVICE_SPEAKER,
    207     OUT_DEVICE_HEADSET,
    208     OUT_DEVICE_HEADPHONES,
    209     OUT_DEVICE_BT_SCO,
    210     OUT_DEVICE_SPEAKER_AND_HEADSET,
    211     OUT_DEVICE_TAB_SIZE,           /* number of rows in route_configs[][] */
    212     OUT_DEVICE_NONE,
    213     OUT_DEVICE_CNT
    214 };
    215 
    216 enum {
    217     IN_SOURCE_MIC,
    218     IN_SOURCE_CAMCORDER,
    219     IN_SOURCE_VOICE_RECOGNITION,
    220     IN_SOURCE_VOICE_COMMUNICATION,
    221     IN_SOURCE_TAB_SIZE,            /* number of lines in route_configs[][] */
    222     IN_SOURCE_NONE,
    223     IN_SOURCE_CNT
    224 };
    225 
    226 enum {
    227     ES305_MODE_DEFAULT,
    228     ES305_MODE_LEVEL,
    229     ES305_NUM_MODES,
    230 };
    231 
    232 int get_output_device_id(audio_devices_t device)
    233 {
    234     if (device == AUDIO_DEVICE_NONE)
    235         return OUT_DEVICE_NONE;
    236 
    237     if (popcount(device) == 2) {
    238         if ((device == (AUDIO_DEVICE_OUT_SPEAKER |
    239                        AUDIO_DEVICE_OUT_WIRED_HEADSET)) ||
    240                 (device == (AUDIO_DEVICE_OUT_SPEAKER |
    241                         AUDIO_DEVICE_OUT_WIRED_HEADPHONE)))
    242             return OUT_DEVICE_SPEAKER_AND_HEADSET;
    243         else
    244             return OUT_DEVICE_NONE;
    245     }
    246 
    247     if (popcount(device) != 1)
    248         return OUT_DEVICE_NONE;
    249 
    250     switch (device) {
    251     case AUDIO_DEVICE_OUT_SPEAKER:
    252         return OUT_DEVICE_SPEAKER;
    253     case AUDIO_DEVICE_OUT_WIRED_HEADSET:
    254         return OUT_DEVICE_HEADSET;
    255     case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
    256         return OUT_DEVICE_HEADPHONES;
    257     case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
    258     case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
    259     case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
    260         return OUT_DEVICE_BT_SCO;
    261     default:
    262         return OUT_DEVICE_NONE;
    263     }
    264 }
    265 
    266 int get_input_source_id(audio_source_t source)
    267 {
    268     switch (source) {
    269     case AUDIO_SOURCE_DEFAULT:
    270         return IN_SOURCE_NONE;
    271     case AUDIO_SOURCE_MIC:
    272         return IN_SOURCE_MIC;
    273     case AUDIO_SOURCE_CAMCORDER:
    274         return IN_SOURCE_CAMCORDER;
    275     case AUDIO_SOURCE_VOICE_RECOGNITION:
    276         return IN_SOURCE_VOICE_RECOGNITION;
    277     case AUDIO_SOURCE_VOICE_COMMUNICATION:
    278         return IN_SOURCE_VOICE_COMMUNICATION;
    279     default:
    280         return IN_SOURCE_NONE;
    281     }
    282 }
    283 
    284 struct route_config {
    285     const char * const output_route;
    286     const char * const input_route;
    287     int es305_preset[ES305_NUM_MODES]; // es305 preset for this route.
    288                                        // -1 means es305 bypass
    289 };
    290 
    291 const struct route_config media_speaker = {
    292     "media-speaker",
    293     "media-main-mic",
    294     { ES305_PRESET_OFF,
    295       ES305_PRESET_OFF }
    296 };
    297 
    298 const struct route_config media_headphones = {
    299     "media-headphones",
    300     "media-main-mic",
    301     { ES305_PRESET_OFF,
    302       ES305_PRESET_OFF }
    303 };
    304 
    305 const struct route_config media_headset = {
    306     "media-headphones",
    307     "media-headset-mic",
    308     { ES305_PRESET_OFF,
    309       ES305_PRESET_OFF }
    310 };
    311 
    312 const struct route_config camcorder_speaker = {
    313     "media-speaker",
    314     "media-second-mic",
    315     { ES305_PRESET_CAMCORDER,
    316       ES305_PRESET_CAMCORDER }
    317 };
    318 
    319 const struct route_config camcorder_headphones = {
    320     "media-headphones",
    321     "media-second-mic",
    322     { ES305_PRESET_CAMCORDER,
    323       ES305_PRESET_CAMCORDER }
    324 };
    325 
    326 const struct route_config voice_rec_speaker = {
    327     "voice-rec-speaker",
    328     "voice-rec-main-mic",
    329     { ES305_PRESET_ASRA_HANDHELD,
    330       ES305_PRESET_ASRA_DESKTOP }
    331 };
    332 
    333 const struct route_config voice_rec_headphones = {
    334     "voice-rec-headphones",
    335     "voice-rec-main-mic",
    336     { ES305_PRESET_ASRA_HANDHELD,
    337       ES305_PRESET_ASRA_DESKTOP }
    338 };
    339 
    340 const struct route_config voice_rec_headset = {
    341     "voice-rec-headphones",
    342     "voice-rec-headset-mic",
    343     { ES305_PRESET_ASRA_HEADSET,
    344       ES305_PRESET_ASRA_HEADSET }
    345 };
    346 
    347 const struct route_config communication_speaker = {
    348     "communication-speaker",
    349     "communication-main-mic",
    350     { ES305_PRESET_VOIP_HANDHELD,
    351       ES305_PRESET_VOIP_DESKTOP }
    352 };
    353 
    354 const struct route_config communication_headphones = {
    355     "communication-headphones",
    356     "communication-main-mic",
    357     { ES305_PRESET_VOIP_HEADPHONES,
    358       ES305_PRESET_VOIP_HP_DESKTOP}
    359 };
    360 
    361 const struct route_config communication_headset = {
    362     "communication-headphones",
    363     "communication-headset-mic",
    364     { ES305_PRESET_VOIP_HEADSET,
    365       ES305_PRESET_VOIP_HEADSET }
    366 };
    367 
    368 const struct route_config speaker_and_headphones = {
    369     "speaker-and-headphones",
    370     "main-mic",
    371     { ES305_PRESET_CURRENT,
    372       ES305_PRESET_CURRENT }
    373 };
    374 
    375 const struct route_config bluetooth_sco = {
    376     "bt-sco-headset",
    377     "bt-sco-mic",
    378     { ES305_PRESET_OFF,
    379       ES305_PRESET_OFF }
    380 };
    381 
    382 const struct route_config * const route_configs[IN_SOURCE_TAB_SIZE]
    383                                                [OUT_DEVICE_TAB_SIZE] = {
    384     {   /* IN_SOURCE_MIC */
    385         &media_speaker,             /* OUT_DEVICE_SPEAKER */
    386         &media_headset,             /* OUT_DEVICE_HEADSET */
    387         &media_headphones,          /* OUT_DEVICE_HEADPHONES */
    388         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
    389         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
    390     },
    391     {   /* IN_SOURCE_CAMCORDER */
    392         &camcorder_speaker,         /* OUT_DEVICE_SPEAKER */
    393         &camcorder_headphones,      /* OUT_DEVICE_HEADSET */
    394         &camcorder_headphones,      /* OUT_DEVICE_HEADPHONES */
    395         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
    396         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
    397     },
    398     {   /* IN_SOURCE_VOICE_RECOGNITION */
    399         &voice_rec_speaker,         /* OUT_DEVICE_SPEAKER */
    400         &voice_rec_headset,         /* OUT_DEVICE_HEADSET */
    401         &voice_rec_headphones,      /* OUT_DEVICE_HEADPHONES */
    402         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
    403         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
    404     },
    405     {   /* IN_SOURCE_VOICE_COMMUNICATION */
    406         &communication_speaker,     /* OUT_DEVICE_SPEAKER */
    407         &communication_headset,     /* OUT_DEVICE_HEADSET */
    408         &communication_headphones,  /* OUT_DEVICE_HEADPHONES */
    409         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
    410         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
    411     }
    412 };
    413 
    414 static int do_out_standby(struct stream_out *out);
    415 
    416 /**
    417  * NOTE: when multiple mutexes have to be acquired, always respect the
    418  * following order: hw device > in stream > out stream
    419  */
    420 
    421 /* Helper functions */
    422 
    423 static int open_hdmi_driver(struct audio_device *adev)
    424 {
    425     if (adev->hdmi_drv_fd < 0) {
    426         adev->hdmi_drv_fd = open("/dev/video16", O_RDWR);
    427         if (adev->hdmi_drv_fd < 0)
    428             ALOGE("%s cannot open video16 (%d)", __func__, adev->hdmi_drv_fd);
    429     }
    430     return adev->hdmi_drv_fd;
    431 }
    432 
    433 /* must be called with hw device mutex locked */
    434 static int enable_hdmi_audio(struct audio_device *adev, int enable)
    435 {
    436     int ret;
    437     struct v4l2_control ctrl;
    438 
    439     ret = open_hdmi_driver(adev);
    440     if (ret < 0)
    441         return ret;
    442 
    443     ctrl.id = V4L2_CID_TV_ENABLE_HDMI_AUDIO;
    444     ctrl.value = !!enable;
    445     ret = ioctl(adev->hdmi_drv_fd, VIDIOC_S_CTRL, &ctrl);
    446 
    447     if (ret < 0)
    448         ALOGE("V4L2_CID_TV_ENABLE_HDMI_AUDIO ioctl error (%d)", errno);
    449 
    450     return ret;
    451 }
    452 
    453 /* must be called with hw device mutex locked */
    454 static int read_hdmi_channel_masks(struct audio_device *adev, struct stream_out *out) {
    455     int ret;
    456     struct v4l2_control ctrl;
    457 
    458     ret = open_hdmi_driver(adev);
    459     if (ret < 0)
    460         return ret;
    461 
    462     ctrl.id = V4L2_CID_TV_MAX_AUDIO_CHANNELS;
    463     ret = ioctl(adev->hdmi_drv_fd, VIDIOC_G_CTRL, &ctrl);
    464     if (ret < 0) {
    465         ALOGE("V4L2_CID_TV_MAX_AUDIO_CHANNELS ioctl error (%d)", errno);
    466         return ret;
    467     }
    468 
    469     ALOGV("%s ioctl %d got %d max channels", __func__, ret, ctrl.value);
    470 
    471     if (ctrl.value != 6 && ctrl.value != 8)
    472         return -ENOSYS;
    473 
    474     out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
    475     if (ctrl.value == 8)
    476         out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
    477 
    478     return ret;
    479 }
    480 
    481 /* must be called with hw device mutex locked */
    482 static int set_hdmi_channels(struct audio_device *adev, int channels) {
    483     int ret;
    484     struct v4l2_control ctrl;
    485 
    486     ret = open_hdmi_driver(adev);
    487     if (ret < 0)
    488         return ret;
    489 
    490     ctrl.id = V4L2_CID_TV_SET_NUM_CHANNELS;
    491     ctrl.value = channels;
    492     ret = ioctl(adev->hdmi_drv_fd, VIDIOC_S_CTRL, &ctrl);
    493     if (ret < 0)
    494         ALOGE("V4L2_CID_TV_SET_NUM_CHANNELS ioctl error (%d)", errno);
    495 
    496     return ret;
    497 }
    498 
    499 static void select_devices(struct audio_device *adev)
    500 {
    501     int output_device_id = get_output_device_id(adev->out_device);
    502     int input_source_id = get_input_source_id(adev->input_source);
    503     const char *output_route = NULL;
    504     const char *input_route = NULL;
    505     int new_route_id;
    506     int new_es305_preset = -1;
    507 
    508     audio_route_reset(adev->ar);
    509 
    510     enable_hdmi_audio(adev, adev->out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL);
    511 
    512     new_route_id = (1 << (input_source_id + OUT_DEVICE_CNT)) + (1 << output_device_id);
    513     if ((new_route_id == adev->cur_route_id) && (adev->es305_mode == adev->es305_new_mode))
    514         return;
    515     adev->cur_route_id = new_route_id;
    516     adev->es305_mode = adev->es305_new_mode;
    517 
    518     if (input_source_id != IN_SOURCE_NONE) {
    519         if (output_device_id != OUT_DEVICE_NONE) {
    520             input_route =
    521                     route_configs[input_source_id][output_device_id]->input_route;
    522             output_route =
    523                     route_configs[input_source_id][output_device_id]->output_route;
    524             new_es305_preset =
    525                 route_configs[input_source_id][output_device_id]->es305_preset[adev->es305_mode];
    526         } else {
    527             switch (adev->in_device) {
    528             case AUDIO_DEVICE_IN_WIRED_HEADSET & ~AUDIO_DEVICE_BIT_IN:
    529                 output_device_id = OUT_DEVICE_HEADSET;
    530                 break;
    531             case AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & ~AUDIO_DEVICE_BIT_IN:
    532                 output_device_id = OUT_DEVICE_BT_SCO;
    533                 break;
    534             default:
    535                 output_device_id = OUT_DEVICE_SPEAKER;
    536                 break;
    537             }
    538             input_route =
    539                     route_configs[input_source_id][output_device_id]->input_route;
    540             new_es305_preset =
    541                 route_configs[input_source_id][output_device_id]->es305_preset[adev->es305_mode];
    542         }
    543         // disable noise suppression when capturing front and back mic for voice recognition
    544         if ((adev->input_source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
    545                 (adev->in_channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK))
    546             new_es305_preset = -1;
    547     } else {
    548         if (output_device_id != OUT_DEVICE_NONE) {
    549             output_route =
    550                     route_configs[IN_SOURCE_MIC][output_device_id]->output_route;
    551         }
    552     }
    553 
    554     ALOGV("select_devices() devices %#x input src %d output route %s input route %s",
    555           adev->out_device, adev->input_source,
    556           output_route ? output_route : "none",
    557           input_route ? input_route : "none");
    558 
    559     if (output_route)
    560         audio_route_apply_path(adev->ar, output_route);
    561     if (input_route)
    562         audio_route_apply_path(adev->ar, input_route);
    563 
    564     if ((new_es305_preset != ES305_PRESET_CURRENT) &&
    565             (new_es305_preset != adev->es305_preset)) {
    566         ALOGV("  select_devices() changing es305 preset from %d to %d",
    567               adev->es305_preset, new_es305_preset);
    568         if (eS305_UsePreset(new_es305_preset) == 0) {
    569             adev->es305_preset = new_es305_preset;
    570         }
    571     }
    572 
    573     audio_route_update_mixer(adev->ar);
    574 }
    575 
    576 void bubblelevel_callback(bool is_level, void *user_data)
    577 {
    578     struct audio_device *adev = (struct audio_device *)user_data;
    579     int es305_mode;
    580 
    581     if (is_level)
    582         es305_mode = ES305_MODE_LEVEL;
    583     else
    584         es305_mode = ES305_MODE_DEFAULT;
    585 
    586     pthread_mutex_lock(&adev->lock);
    587     if (es305_mode != adev->es305_mode) {
    588         adev->es305_new_mode = es305_mode;
    589         select_devices(adev);
    590         ALOGV("bubblelevel_callback is_level %d es305_mode %d", is_level, es305_mode);
    591     }
    592     pthread_mutex_unlock(&adev->lock);
    593 }
    594 
    595 /* must be called with hw device mutex locked */
    596 bool get_bubblelevel(struct audio_device *adev)
    597 {
    598     if (!adev->bubble_level) {
    599         adev->bubble_level = bubble_level_create();
    600         if (adev->bubble_level)
    601             adev->bubble_level->set_callback(adev->bubble_level, bubblelevel_callback, adev);
    602     }
    603     return (adev->bubble_level != NULL);
    604 }
    605 
    606 static void force_non_hdmi_out_standby(struct audio_device *adev)
    607 {
    608     enum output_type type;
    609     struct stream_out *out;
    610 
    611     for (type = 0; type < OUTPUT_TOTAL; ++type) {
    612         out = adev->outputs[type];
    613         if (type == OUTPUT_HDMI || !out)
    614             continue;
    615         pthread_mutex_lock(&out->lock);
    616         do_out_standby(out);
    617         pthread_mutex_unlock(&out->lock);
    618     }
    619 }
    620 
    621 /* must be called with the hw device mutex locked, OK to hold other mutexes */
    622 static void start_bt_sco(struct audio_device *adev) {
    623     if (adev->sco_on_count++ > 0)
    624         return;
    625 
    626     adev->pcm_voice_out = pcm_open(PCM_CARD, PCM_DEVICE_VOICE, PCM_OUT,
    627                               &pcm_config_sco);
    628     if (adev->pcm_voice_out && !pcm_is_ready(adev->pcm_voice_out)) {
    629         ALOGE("pcm_open(VOICE_OUT) failed: %s", pcm_get_error(adev->pcm_voice_out));
    630         goto err_voice_out;
    631     }
    632     adev->pcm_sco_out = pcm_open(PCM_CARD, PCM_DEVICE_SCO, PCM_OUT,
    633                             &pcm_config_sco);
    634     if (adev->pcm_sco_out && !pcm_is_ready(adev->pcm_sco_out)) {
    635         ALOGE("pcm_open(SCO_OUT) failed: %s", pcm_get_error(adev->pcm_sco_out));
    636         goto err_sco_out;
    637     }
    638     adev->pcm_voice_in = pcm_open(PCM_CARD, PCM_DEVICE_VOICE, PCM_IN,
    639                                  &pcm_config_sco);
    640     if (adev->pcm_voice_in && !pcm_is_ready(adev->pcm_voice_in)) {
    641         ALOGE("pcm_open(VOICE_IN) failed: %s", pcm_get_error(adev->pcm_voice_in));
    642         goto err_voice_in;
    643     }
    644     adev->pcm_sco_in = pcm_open(PCM_CARD, PCM_DEVICE_SCO, PCM_IN,
    645                                &pcm_config_sco);
    646     if (adev->pcm_sco_in && !pcm_is_ready(adev->pcm_sco_in)) {
    647         ALOGE("pcm_open(SCO_IN) failed: %s", pcm_get_error(adev->pcm_sco_in));
    648         goto err_sco_in;
    649     }
    650 
    651     pcm_start(adev->pcm_voice_out);
    652     pcm_start(adev->pcm_sco_out);
    653     pcm_start(adev->pcm_voice_in);
    654     pcm_start(adev->pcm_sco_in);
    655 
    656     return;
    657 
    658 err_sco_in:
    659     pcm_close(adev->pcm_sco_in);
    660 err_voice_in:
    661     pcm_close(adev->pcm_voice_in);
    662 err_sco_out:
    663     pcm_close(adev->pcm_sco_out);
    664 err_voice_out:
    665     pcm_close(adev->pcm_voice_out);
    666 }
    667 
    668 /* must be called with the hw device mutex locked, OK to hold other mutexes */
    669 static void stop_bt_sco(struct audio_device *adev) {
    670     if (adev->sco_on_count == 0 || --adev->sco_on_count > 0)
    671         return;
    672 
    673     pcm_stop(adev->pcm_voice_out);
    674     pcm_stop(adev->pcm_sco_out);
    675     pcm_stop(adev->pcm_voice_in);
    676     pcm_stop(adev->pcm_sco_in);
    677 
    678     pcm_close(adev->pcm_voice_out);
    679     pcm_close(adev->pcm_sco_out);
    680     pcm_close(adev->pcm_voice_in);
    681     pcm_close(adev->pcm_sco_in);
    682 }
    683 
    684 /* must be called with hw device and output stream mutexes locked */
    685 static int start_output_stream(struct stream_out *out)
    686 {
    687     struct audio_device *adev = out->dev;
    688     int type;
    689 
    690     if (out == adev->outputs[OUTPUT_HDMI]) {
    691         force_non_hdmi_out_standby(adev);
    692     } else if (adev->outputs[OUTPUT_HDMI] && !adev->outputs[OUTPUT_HDMI]->standby) {
    693         out->disabled = true;
    694         return 0;
    695     }
    696 
    697     out->disabled = false;
    698 
    699     if (out->device & (AUDIO_DEVICE_OUT_SPEAKER |
    700                        AUDIO_DEVICE_OUT_WIRED_HEADSET |
    701                        AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
    702                        AUDIO_DEVICE_OUT_AUX_DIGITAL |
    703                        AUDIO_DEVICE_OUT_ALL_SCO)) {
    704 
    705         out->pcm[PCM_CARD] = pcm_open(PCM_CARD, out->pcm_device,
    706                                       PCM_OUT, &out->config);
    707 
    708         if (out->pcm[PCM_CARD] && !pcm_is_ready(out->pcm[PCM_CARD])) {
    709             ALOGE("pcm_open(PCM_CARD) failed: %s",
    710                   pcm_get_error(out->pcm[PCM_CARD]));
    711             pcm_close(out->pcm[PCM_CARD]);
    712             return -ENOMEM;
    713         }
    714     }
    715 
    716     if (out->device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
    717         out->pcm[PCM_CARD_SPDIF] = pcm_open(PCM_CARD_SPDIF, out->pcm_device,
    718                                             PCM_OUT, &out->config);
    719 
    720         if (out->pcm[PCM_CARD_SPDIF] &&
    721                 !pcm_is_ready(out->pcm[PCM_CARD_SPDIF])) {
    722             ALOGE("pcm_open(PCM_CARD_SPDIF) failed: %s",
    723                   pcm_get_error(out->pcm[PCM_CARD_SPDIF]));
    724             pcm_close(out->pcm[PCM_CARD_SPDIF]);
    725             return -ENOMEM;
    726         }
    727     }
    728 
    729     adev->out_device |= out->device;
    730     select_devices(adev);
    731 
    732     if (out->device & AUDIO_DEVICE_OUT_ALL_SCO)
    733         start_bt_sco(adev);
    734 
    735     if (out->device & AUDIO_DEVICE_OUT_AUX_DIGITAL)
    736         set_hdmi_channels(adev, out->config.channels);
    737 
    738     /* anticipate level measurement in case we start capture later */
    739     if (get_bubblelevel(adev))
    740         adev->bubble_level->poll_once(adev->bubble_level);
    741 
    742     return 0;
    743 }
    744 
    745 /* must be called with hw device and input stream mutexes locked */
    746 static int start_input_stream(struct stream_in *in)
    747 {
    748     struct audio_device *adev = in->dev;
    749 
    750     in->pcm = pcm_open(PCM_CARD, PCM_DEVICE, PCM_IN, &pcm_config_in);
    751 
    752     if (in->pcm && !pcm_is_ready(in->pcm)) {
    753         ALOGE("pcm_open() failed: %s", pcm_get_error(in->pcm));
    754         pcm_close(in->pcm);
    755         return -ENOMEM;
    756     }
    757 
    758     /* if no supported sample rate is available, use the resampler */
    759     if (in->resampler)
    760         in->resampler->reset(in->resampler);
    761 
    762     in->frames_in = 0;
    763     adev->input_source = in->input_source;
    764     adev->in_device = in->device;
    765     adev->in_channel_mask = in->channel_mask;
    766 
    767     eS305_SetActiveIoHandle(in->io_handle);
    768     select_devices(adev);
    769 
    770     if (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)
    771         start_bt_sco(adev);
    772 
    773     /* initialize volume ramp */
    774     in->ramp_frames = (CAPTURE_START_RAMP_MS * in->requested_rate) / 1000;
    775     in->ramp_step = (uint16_t)(USHRT_MAX / in->ramp_frames);
    776     in->ramp_vol = 0;;
    777 
    778     if (get_bubblelevel(adev)) {
    779         adev->bubble_level->set_poll_interval(adev->bubble_level, BL_POLL_INTERVAL_MIN_SEC);
    780         adev->bubble_level->start_polling(adev->bubble_level);
    781     }
    782 
    783     return 0;
    784 }
    785 
    786 static size_t get_input_buffer_size(unsigned int sample_rate,
    787                                     audio_format_t format,
    788                                     unsigned int channel_count)
    789 {
    790     size_t size;
    791 
    792     /*
    793      * take resampling into account and return the closest majoring
    794      * multiple of 16 frames, as audioflinger expects audio buffers to
    795      * be a multiple of 16 frames
    796      */
    797     size = (pcm_config_in.period_size * sample_rate) / pcm_config_in.rate;
    798     size = ((size + 15) / 16) * 16;
    799 
    800     return size * channel_count * audio_bytes_per_sample(format);
    801 }
    802 
    803 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
    804                                    struct resampler_buffer* buffer)
    805 {
    806     struct stream_in *in;
    807     size_t i;
    808 
    809     if (buffer_provider == NULL || buffer == NULL)
    810         return -EINVAL;
    811 
    812     in = (struct stream_in *)((char *)buffer_provider -
    813                                    offsetof(struct stream_in, buf_provider));
    814 
    815     if (in->pcm == NULL) {
    816         buffer->raw = NULL;
    817         buffer->frame_count = 0;
    818         in->read_status = -ENODEV;
    819         return -ENODEV;
    820     }
    821 
    822     if (in->frames_in == 0) {
    823         in->read_status = pcm_read(in->pcm,
    824                                    (void*)in->buffer,
    825                                    pcm_frames_to_bytes(in->pcm, pcm_config_in.period_size));
    826         if (in->read_status != 0) {
    827             ALOGE("get_next_buffer() pcm_read error %d", in->read_status);
    828             buffer->raw = NULL;
    829             buffer->frame_count = 0;
    830             return in->read_status;
    831         }
    832 
    833         in->frames_in = pcm_config_in.period_size;
    834 
    835         /* Do stereo to mono conversion in place by discarding right channel */
    836         if (in->channel_mask == AUDIO_CHANNEL_IN_MONO)
    837             for (i = 1; i < in->frames_in; i++)
    838                 in->buffer[i] = in->buffer[i * 2];
    839     }
    840 
    841     buffer->frame_count = (buffer->frame_count > in->frames_in) ?
    842                                 in->frames_in : buffer->frame_count;
    843     buffer->i16 = in->buffer +
    844             (pcm_config_in.period_size - in->frames_in) * popcount(in->channel_mask);
    845 
    846     return in->read_status;
    847 
    848 }
    849 
    850 static void release_buffer(struct resampler_buffer_provider *buffer_provider,
    851                                   struct resampler_buffer* buffer)
    852 {
    853     struct stream_in *in;
    854 
    855     if (buffer_provider == NULL || buffer == NULL)
    856         return;
    857 
    858     in = (struct stream_in *)((char *)buffer_provider -
    859                                    offsetof(struct stream_in, buf_provider));
    860 
    861     in->frames_in -= buffer->frame_count;
    862 }
    863 
    864 /* read_frames() reads frames from kernel driver, down samples to capture rate
    865  * if necessary and output the number of frames requested to the buffer specified */
    866 static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames)
    867 {
    868     ssize_t frames_wr = 0;
    869     size_t frame_size = audio_stream_frame_size(&in->stream.common);
    870 
    871     while (frames_wr < frames) {
    872         size_t frames_rd = frames - frames_wr;
    873         if (in->resampler != NULL) {
    874             in->resampler->resample_from_provider(in->resampler,
    875                     (int16_t *)((char *)buffer +
    876                             frames_wr * frame_size),
    877                     &frames_rd);
    878         } else {
    879             struct resampler_buffer buf = {
    880                     { raw : NULL, },
    881                     frame_count : frames_rd,
    882             };
    883             get_next_buffer(&in->buf_provider, &buf);
    884             if (buf.raw != NULL) {
    885                 memcpy((char *)buffer +
    886                            frames_wr * frame_size,
    887                         buf.raw,
    888                         buf.frame_count * frame_size);
    889                 frames_rd = buf.frame_count;
    890             }
    891             release_buffer(&in->buf_provider, &buf);
    892         }
    893         /* in->read_status is updated by getNextBuffer() also called by
    894          * in->resampler->resample_from_provider() */
    895         if (in->read_status != 0)
    896             return in->read_status;
    897 
    898         frames_wr += frames_rd;
    899     }
    900     return frames_wr;
    901 }
    902 
    903 /* API functions */
    904 
    905 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
    906 {
    907     struct stream_out *out = (struct stream_out *)stream;
    908 
    909     return out->config.rate;
    910 }
    911 
    912 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
    913 {
    914     return -ENOSYS;
    915 }
    916 
    917 static size_t out_get_buffer_size(const struct audio_stream *stream)
    918 {
    919     struct stream_out *out = (struct stream_out *)stream;
    920 
    921     return out->config.period_size *
    922             audio_stream_frame_size((struct audio_stream *)stream);
    923 }
    924 
    925 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
    926 {
    927     struct stream_out *out = (struct stream_out *)stream;
    928 
    929     return out->channel_mask;
    930 }
    931 
    932 static audio_format_t out_get_format(const struct audio_stream *stream)
    933 {
    934     return AUDIO_FORMAT_PCM_16_BIT;
    935 }
    936 
    937 static int out_set_format(struct audio_stream *stream, audio_format_t format)
    938 {
    939     return -ENOSYS;
    940 }
    941 
    942 /* Return the set of output devices associated with active streams
    943  * other than out.  Assumes out is non-NULL and out->dev is locked.
    944  */
    945 static audio_devices_t output_devices(struct stream_out *out)
    946 {
    947     struct audio_device *dev = out->dev;
    948     enum output_type type;
    949     audio_devices_t devices = AUDIO_DEVICE_NONE;
    950 
    951     for (type = 0; type < OUTPUT_TOTAL; ++type) {
    952         struct stream_out *other = dev->outputs[type];
    953         if (other && (other != out) && !other->standby) {
    954             /* safe to access other stream without a mutex,
    955              * because we hold the dev lock,
    956              * which prevents the other stream from being closed
    957              */
    958             devices |= other->device;
    959         }
    960     }
    961 
    962     return devices;
    963 }
    964 
    965 static int do_out_standby(struct stream_out *out)
    966 {
    967     struct audio_device *adev = out->dev;
    968     int i;
    969 
    970     if (!out->standby) {
    971         for (i = 0; i < PCM_TOTAL; i++) {
    972             if (out->pcm[i]) {
    973                 pcm_close(out->pcm[i]);
    974                 out->pcm[i] = NULL;
    975             }
    976         }
    977         out->standby = true;
    978 
    979         if (out == adev->outputs[OUTPUT_HDMI]) {
    980             /* force standby on low latency output stream so that it can reuse HDMI driver if
    981              * necessary when restarted */
    982             force_non_hdmi_out_standby(adev);
    983         }
    984 
    985         if (out->device & AUDIO_DEVICE_OUT_ALL_SCO)
    986             stop_bt_sco(adev);
    987 
    988         /* re-calculate the set of active devices from other streams */
    989         adev->out_device = output_devices(out);
    990 
    991         /* Skip resetting the mixer if no output device is active */
    992         if (adev->out_device)
    993             select_devices(adev);
    994     }
    995 
    996     return 0;
    997 }
    998 
    999 static int out_standby(struct audio_stream *stream)
   1000 {
   1001     struct stream_out *out = (struct stream_out *)stream;
   1002     int ret;
   1003 
   1004     pthread_mutex_lock(&out->dev->lock);
   1005     pthread_mutex_lock(&out->lock);
   1006 
   1007     ret = do_out_standby(out);
   1008 
   1009     pthread_mutex_unlock(&out->lock);
   1010     pthread_mutex_unlock(&out->dev->lock);
   1011 
   1012     return ret;
   1013 }
   1014 
   1015 static int out_dump(const struct audio_stream *stream, int fd)
   1016 {
   1017     return 0;
   1018 }
   1019 
   1020 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
   1021 {
   1022     struct stream_out *out = (struct stream_out *)stream;
   1023     struct audio_device *adev = out->dev;
   1024     struct str_parms *parms;
   1025     char value[32];
   1026     int ret;
   1027     unsigned int val;
   1028 
   1029     parms = str_parms_create_str(kvpairs);
   1030 
   1031     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
   1032                             value, sizeof(value));
   1033     pthread_mutex_lock(&adev->lock);
   1034     pthread_mutex_lock(&out->lock);
   1035     if (ret >= 0) {
   1036         val = atoi(value);
   1037         if ((out->device != val) && (val != 0)) {
   1038             /* Force standby if moving to/from SPDIF or if the output
   1039              * device changes when in SPDIF mode */
   1040             if (((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^
   1041                  (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) ||
   1042                 (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) {
   1043                 do_out_standby(out);
   1044             }
   1045 
   1046             /* force output standby to start or stop SCO pcm stream if needed */
   1047             if ((val & AUDIO_DEVICE_OUT_ALL_SCO) ^
   1048                     (out->device & AUDIO_DEVICE_OUT_ALL_SCO)) {
   1049                 do_out_standby(out);
   1050             }
   1051 
   1052             if (!out->standby && (out == adev->outputs[OUTPUT_HDMI] ||
   1053                     !adev->outputs[OUTPUT_HDMI] ||
   1054                     adev->outputs[OUTPUT_HDMI]->standby)) {
   1055                 adev->out_device = output_devices(out) | val;
   1056                 select_devices(adev);
   1057             }
   1058             out->device = val;
   1059         }
   1060     }
   1061     pthread_mutex_unlock(&out->lock);
   1062     pthread_mutex_unlock(&adev->lock);
   1063 
   1064     str_parms_destroy(parms);
   1065     return ret;
   1066 }
   1067 
   1068 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
   1069 {
   1070     struct stream_out *out = (struct stream_out *)stream;
   1071     struct str_parms *query = str_parms_create_str(keys);
   1072     char *str;
   1073     char value[256];
   1074     struct str_parms *reply = str_parms_create();
   1075     size_t i, j;
   1076     int ret;
   1077     bool first = true;
   1078 
   1079     ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
   1080     if (ret >= 0) {
   1081         value[0] = '\0';
   1082         i = 0;
   1083         /* the last entry in supported_channel_masks[] is always 0 */
   1084         while (out->supported_channel_masks[i] != 0) {
   1085             for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
   1086                 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
   1087                     if (!first) {
   1088                         strcat(value, "|");
   1089                     }
   1090                     strcat(value, out_channels_name_to_enum_table[j].name);
   1091                     first = false;
   1092                     break;
   1093                 }
   1094             }
   1095             i++;
   1096         }
   1097         str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
   1098         str = str_parms_to_str(reply);
   1099     } else {
   1100         str = strdup(keys);
   1101     }
   1102 
   1103     str_parms_destroy(query);
   1104     str_parms_destroy(reply);
   1105     return str;
   1106 }
   1107 
   1108 static uint32_t out_get_latency(const struct audio_stream_out *stream)
   1109 {
   1110     struct stream_out *out = (struct stream_out *)stream;
   1111 
   1112     return (out->config.period_size * out->config.period_count * 1000) /
   1113             out->config.rate;
   1114 }
   1115 
   1116 static int out_set_volume(struct audio_stream_out *stream, float left,
   1117                           float right)
   1118 {
   1119     struct stream_out *out = (struct stream_out *)stream;
   1120     struct audio_device *adev = out->dev;
   1121 
   1122     if (out == adev->outputs[OUTPUT_HDMI]) {
   1123         /* only take left channel into account: the API is for stereo anyway */
   1124         out->muted = (left == 0.0f);
   1125         return 0;
   1126     }
   1127     return -ENOSYS;
   1128 }
   1129 
   1130 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
   1131                          size_t bytes)
   1132 {
   1133     int ret;
   1134     struct stream_out *out = (struct stream_out *)stream;
   1135     struct audio_device *adev = out->dev;
   1136     int i;
   1137 
   1138     /*
   1139      * acquiring hw device mutex systematically is useful if a low
   1140      * priority thread is waiting on the output stream mutex - e.g.
   1141      * executing out_set_parameters() while holding the hw device
   1142      * mutex
   1143      */
   1144     pthread_mutex_lock(&adev->lock);
   1145     pthread_mutex_lock(&out->lock);
   1146     if (out->standby) {
   1147         ret = start_output_stream(out);
   1148         if (ret != 0) {
   1149             pthread_mutex_unlock(&adev->lock);
   1150             goto exit;
   1151         }
   1152         out->standby = false;
   1153     }
   1154     pthread_mutex_unlock(&adev->lock);
   1155 
   1156     if (out->disabled) {
   1157         ret = -EPIPE;
   1158         goto exit;
   1159     }
   1160 
   1161     if (out->muted)
   1162         memset((void *)buffer, 0, bytes);
   1163 
   1164     /* Write to all active PCMs */
   1165     for (i = 0; i < PCM_TOTAL; i++)
   1166         if (out->pcm[i])
   1167            pcm_write(out->pcm[i], (void *)buffer, bytes);
   1168 
   1169 
   1170 exit:
   1171     pthread_mutex_unlock(&out->lock);
   1172 
   1173     if (ret != 0) {
   1174         usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
   1175                out_get_sample_rate(&stream->common));
   1176     }
   1177 
   1178     return bytes;
   1179 }
   1180 
   1181 static int out_get_render_position(const struct audio_stream_out *stream,
   1182                                    uint32_t *dsp_frames)
   1183 {
   1184     return -EINVAL;
   1185 }
   1186 
   1187 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
   1188 {
   1189     return 0;
   1190 }
   1191 
   1192 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
   1193 {
   1194     return 0;
   1195 }
   1196 
   1197 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
   1198                                         int64_t *timestamp)
   1199 {
   1200     return -EINVAL;
   1201 }
   1202 
   1203 /** audio_stream_in implementation **/
   1204 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
   1205 {
   1206     struct stream_in *in = (struct stream_in *)stream;
   1207 
   1208     return in->requested_rate;
   1209 }
   1210 
   1211 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
   1212 {
   1213     return 0;
   1214 }
   1215 
   1216 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
   1217 {
   1218     struct stream_in *in = (struct stream_in *)stream;
   1219 
   1220     return in->channel_mask;
   1221 }
   1222 
   1223 
   1224 static size_t in_get_buffer_size(const struct audio_stream *stream)
   1225 {
   1226     struct stream_in *in = (struct stream_in *)stream;
   1227 
   1228     return get_input_buffer_size(in->requested_rate,
   1229                                  AUDIO_FORMAT_PCM_16_BIT,
   1230                                  popcount(in_get_channels(stream)));
   1231 }
   1232 
   1233 static audio_format_t in_get_format(const struct audio_stream *stream)
   1234 {
   1235     return AUDIO_FORMAT_PCM_16_BIT;
   1236 }
   1237 
   1238 static int in_set_format(struct audio_stream *stream, audio_format_t format)
   1239 {
   1240     return -ENOSYS;
   1241 }
   1242 
   1243 static int do_in_standby(struct stream_in *in)
   1244 {
   1245     struct audio_device *adev = in->dev;
   1246 
   1247     if (!in->standby) {
   1248         pcm_close(in->pcm);
   1249         in->pcm = NULL;
   1250 
   1251         if (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)
   1252             stop_bt_sco(adev);
   1253 
   1254         in->dev->input_source = AUDIO_SOURCE_DEFAULT;
   1255         in->dev->in_device = AUDIO_DEVICE_NONE;
   1256         in->dev->in_channel_mask = 0;
   1257         select_devices(adev);
   1258         in->standby = true;
   1259 
   1260         if (get_bubblelevel(adev))
   1261             in->dev->bubble_level->stop_polling(adev->bubble_level);
   1262     }
   1263 
   1264     eS305_SetActiveIoHandle(ES305_IO_HANDLE_NONE);
   1265     return 0;
   1266 }
   1267 
   1268 static int in_standby(struct audio_stream *stream)
   1269 {
   1270     struct stream_in *in = (struct stream_in *)stream;
   1271     int ret;
   1272 
   1273     pthread_mutex_lock(&in->dev->lock);
   1274     pthread_mutex_lock(&in->lock);
   1275 
   1276     ret = do_in_standby(in);
   1277 
   1278     pthread_mutex_unlock(&in->lock);
   1279     pthread_mutex_unlock(&in->dev->lock);
   1280 
   1281     return ret;
   1282 }
   1283 
   1284 static int in_dump(const struct audio_stream *stream, int fd)
   1285 {
   1286     return 0;
   1287 }
   1288 
   1289 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
   1290 {
   1291     struct stream_in *in = (struct stream_in *)stream;
   1292     struct audio_device *adev = in->dev;
   1293     struct str_parms *parms;
   1294     char value[32];
   1295     int ret;
   1296     unsigned int val;
   1297     bool apply_now = false;
   1298 
   1299     parms = str_parms_create_str(kvpairs);
   1300 
   1301     pthread_mutex_lock(&adev->lock);
   1302     pthread_mutex_lock(&in->lock);
   1303     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE,
   1304                             value, sizeof(value));
   1305     if (ret >= 0) {
   1306         val = atoi(value);
   1307         /* no audio source uses val == 0 */
   1308         if ((in->input_source != val) && (val != 0)) {
   1309             in->input_source = val;
   1310             apply_now = !in->standby;
   1311         }
   1312     }
   1313 
   1314     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
   1315                             value, sizeof(value));
   1316     if (ret >= 0) {
   1317         /* strip AUDIO_DEVICE_BIT_IN to allow bitwise comparisons */
   1318         val = atoi(value) & ~AUDIO_DEVICE_BIT_IN;
   1319         /* no audio device uses val == 0 */
   1320         if ((in->device != val) && (val != 0)) {
   1321             /* force output standby to start or stop SCO pcm stream if needed */
   1322             if ((val & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) ^
   1323                     (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
   1324                 do_in_standby(in);
   1325             }
   1326             in->device = val;
   1327             apply_now = !in->standby;
   1328         }
   1329     }
   1330 
   1331     if (apply_now) {
   1332         adev->input_source = in->input_source;
   1333         adev->in_device = in->device;
   1334         select_devices(adev);
   1335     }
   1336 
   1337     pthread_mutex_unlock(&in->lock);
   1338     pthread_mutex_unlock(&adev->lock);
   1339 
   1340     str_parms_destroy(parms);
   1341     return ret;
   1342 }
   1343 
   1344 static char * in_get_parameters(const struct audio_stream *stream,
   1345                                 const char *keys)
   1346 {
   1347     return strdup("");
   1348 }
   1349 
   1350 static int in_set_gain(struct audio_stream_in *stream, float gain)
   1351 {
   1352     return 0;
   1353 }
   1354 
   1355 static void in_apply_ramp(struct stream_in *in, int16_t *buffer, size_t frames)
   1356 {
   1357     size_t i;
   1358     uint16_t vol = in->ramp_vol;
   1359     uint16_t step = in->ramp_step;
   1360 
   1361     frames = (frames < in->ramp_frames) ? frames : in->ramp_frames;
   1362 
   1363     if (in->channel_mask == AUDIO_CHANNEL_IN_MONO)
   1364         for (i = 0; i < frames; i++)
   1365         {
   1366             buffer[i] = (int16_t)((buffer[i] * vol) >> 16);
   1367             vol += step;
   1368         }
   1369     else
   1370         for (i = 0; i < frames; i++)
   1371         {
   1372             buffer[2*i] = (int16_t)((buffer[2*i] * vol) >> 16);
   1373             buffer[2*i + 1] = (int16_t)((buffer[2*i + 1] * vol) >> 16);
   1374             vol += step;
   1375         }
   1376 
   1377 
   1378     in->ramp_vol = vol;
   1379     in->ramp_frames -= frames;
   1380 }
   1381 
   1382 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
   1383                        size_t bytes)
   1384 {
   1385     int ret = 0;
   1386     struct stream_in *in = (struct stream_in *)stream;
   1387     struct audio_device *adev = in->dev;
   1388     size_t frames_rq = bytes / audio_stream_frame_size(&stream->common);
   1389 
   1390     /*
   1391      * acquiring hw device mutex systematically is useful if a low
   1392      * priority thread is waiting on the input stream mutex - e.g.
   1393      * executing in_set_parameters() while holding the hw device
   1394      * mutex
   1395      */
   1396     pthread_mutex_lock(&adev->lock);
   1397     pthread_mutex_lock(&in->lock);
   1398     if (in->standby) {
   1399         ret = start_input_stream(in);
   1400         if (ret == 0)
   1401             in->standby = 0;
   1402     }
   1403     pthread_mutex_unlock(&adev->lock);
   1404 
   1405     if (ret < 0)
   1406         goto exit;
   1407 
   1408     /*if (in->num_preprocessors != 0)
   1409         ret = process_frames(in, buffer, frames_rq);
   1410       else */
   1411     ret = read_frames(in, buffer, frames_rq);
   1412 
   1413     if (ret > 0)
   1414         ret = 0;
   1415 
   1416     if (in->ramp_frames > 0)
   1417         in_apply_ramp(in, buffer, frames_rq);
   1418 
   1419     /*
   1420      * Instead of writing zeroes here, we could trust the hardware
   1421      * to always provide zeroes when muted.
   1422      */
   1423     if (ret == 0 && adev->mic_mute)
   1424         memset(buffer, 0, bytes);
   1425 
   1426 exit:
   1427     if (ret < 0)
   1428         usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
   1429                in_get_sample_rate(&stream->common));
   1430 
   1431     pthread_mutex_unlock(&in->lock);
   1432     return bytes;
   1433 }
   1434 
   1435 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
   1436 {
   1437     return 0;
   1438 }
   1439 
   1440 static int in_add_audio_effect(const struct audio_stream *stream,
   1441                                effect_handle_t effect)
   1442 {
   1443     struct stream_in *in = (struct stream_in *)stream;
   1444     effect_descriptor_t descr;
   1445     if ((*effect)->get_descriptor(effect, &descr) == 0) {
   1446 
   1447         pthread_mutex_lock(&in->dev->lock);
   1448         pthread_mutex_lock(&in->lock);
   1449 
   1450         eS305_AddEffect(&descr, in->io_handle);
   1451 
   1452         pthread_mutex_unlock(&in->lock);
   1453         pthread_mutex_unlock(&in->dev->lock);
   1454     }
   1455 
   1456     return 0;
   1457 }
   1458 
   1459 static int in_remove_audio_effect(const struct audio_stream *stream,
   1460                                   effect_handle_t effect)
   1461 {
   1462     struct stream_in *in = (struct stream_in *)stream;
   1463     effect_descriptor_t descr;
   1464     if ((*effect)->get_descriptor(effect, &descr) == 0) {
   1465 
   1466         pthread_mutex_lock(&in->dev->lock);
   1467         pthread_mutex_lock(&in->lock);
   1468 
   1469         eS305_RemoveEffect(&descr, in->io_handle);
   1470 
   1471         pthread_mutex_unlock(&in->lock);
   1472         pthread_mutex_unlock(&in->dev->lock);
   1473     }
   1474 
   1475     return 0;
   1476 }
   1477 
   1478 static int adev_open_output_stream(struct audio_hw_device *dev,
   1479                                    audio_io_handle_t handle,
   1480                                    audio_devices_t devices,
   1481                                    audio_output_flags_t flags,
   1482                                    struct audio_config *config,
   1483                                    struct audio_stream_out **stream_out)
   1484 {
   1485     struct audio_device *adev = (struct audio_device *)dev;
   1486     struct stream_out *out;
   1487     int ret;
   1488     enum output_type type;
   1489 
   1490     out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
   1491     if (!out)
   1492         return -ENOMEM;
   1493 
   1494     out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
   1495     out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
   1496     if (devices == AUDIO_DEVICE_NONE)
   1497         devices = AUDIO_DEVICE_OUT_SPEAKER;
   1498     out->device = devices;
   1499 
   1500     if (flags & AUDIO_OUTPUT_FLAG_DIRECT &&
   1501                    devices == AUDIO_DEVICE_OUT_AUX_DIGITAL) {
   1502         pthread_mutex_lock(&adev->lock);
   1503         ret = read_hdmi_channel_masks(adev, out);
   1504         pthread_mutex_unlock(&adev->lock);
   1505         if (ret != 0)
   1506             goto err_open;
   1507         if (config->sample_rate == 0)
   1508             config->sample_rate = HDMI_MULTI_DEFAULT_SAMPLING_RATE;
   1509         if (config->channel_mask == 0)
   1510             config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
   1511         out->channel_mask = config->channel_mask;
   1512         out->config = pcm_config_hdmi_multi;
   1513         out->config.rate = config->sample_rate;
   1514         out->config.channels = popcount(config->channel_mask);
   1515         out->pcm_device = PCM_DEVICE;
   1516         type = OUTPUT_HDMI;
   1517     } else if (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
   1518         out->config = pcm_config_deep;
   1519         out->pcm_device = PCM_DEVICE_DEEP;
   1520         type = OUTPUT_DEEP_BUF;
   1521     } else {
   1522         out->config = pcm_config;
   1523         out->pcm_device = PCM_DEVICE;
   1524         type = OUTPUT_LOW_LATENCY;
   1525     }
   1526 
   1527     out->stream.common.get_sample_rate = out_get_sample_rate;
   1528     out->stream.common.set_sample_rate = out_set_sample_rate;
   1529     out->stream.common.get_buffer_size = out_get_buffer_size;
   1530     out->stream.common.get_channels = out_get_channels;
   1531     out->stream.common.get_format = out_get_format;
   1532     out->stream.common.set_format = out_set_format;
   1533     out->stream.common.standby = out_standby;
   1534     out->stream.common.dump = out_dump;
   1535     out->stream.common.set_parameters = out_set_parameters;
   1536     out->stream.common.get_parameters = out_get_parameters;
   1537     out->stream.common.add_audio_effect = out_add_audio_effect;
   1538     out->stream.common.remove_audio_effect = out_remove_audio_effect;
   1539     out->stream.get_latency = out_get_latency;
   1540     out->stream.set_volume = out_set_volume;
   1541     out->stream.write = out_write;
   1542     out->stream.get_render_position = out_get_render_position;
   1543     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
   1544 
   1545     out->dev = adev;
   1546 
   1547     config->format = out_get_format(&out->stream.common);
   1548     config->channel_mask = out_get_channels(&out->stream.common);
   1549     config->sample_rate = out_get_sample_rate(&out->stream.common);
   1550 
   1551     out->standby = true;
   1552     /* out->muted = false; by calloc() */
   1553 
   1554     pthread_mutex_lock(&adev->lock);
   1555     if (adev->outputs[type]) {
   1556         pthread_mutex_unlock(&adev->lock);
   1557         ret = -EBUSY;
   1558         goto err_open;
   1559     }
   1560     adev->outputs[type] = out;
   1561     pthread_mutex_unlock(&adev->lock);
   1562 
   1563     *stream_out = &out->stream;
   1564 
   1565     return 0;
   1566 
   1567 err_open:
   1568     free(out);
   1569     *stream_out = NULL;
   1570     return ret;
   1571 }
   1572 
   1573 static void adev_close_output_stream(struct audio_hw_device *dev,
   1574                                      struct audio_stream_out *stream)
   1575 {
   1576     struct audio_device *adev;
   1577     enum output_type type;
   1578 
   1579     out_standby(&stream->common);
   1580     adev = (struct audio_device *)dev;
   1581     pthread_mutex_lock(&adev->lock);
   1582     for (type = 0; type < OUTPUT_TOTAL; ++type) {
   1583         if (adev->outputs[type] == (struct stream_out *) stream) {
   1584             adev->outputs[type] = NULL;
   1585             break;
   1586         }
   1587     }
   1588     pthread_mutex_unlock(&adev->lock);
   1589     free(stream);
   1590 }
   1591 
   1592 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
   1593 {
   1594     return 0;
   1595 }
   1596 
   1597 static char * adev_get_parameters(const struct audio_hw_device *dev,
   1598                                   const char *keys)
   1599 {
   1600     struct audio_device *adev = (struct audio_device *)dev;
   1601     struct str_parms *parms = str_parms_create_str(keys);
   1602     char value[32];
   1603     int ret = str_parms_get_str(parms, "ec_supported", value, sizeof(value));
   1604     char *str;
   1605 
   1606     str_parms_destroy(parms);
   1607     if (ret >= 0) {
   1608         parms = str_parms_create_str("ec_supported=yes");
   1609         str = str_parms_to_str(parms);
   1610         str_parms_destroy(parms);
   1611         return str;
   1612     }
   1613     return strdup("");
   1614 }
   1615 
   1616 static int adev_init_check(const struct audio_hw_device *dev)
   1617 {
   1618     return 0;
   1619 }
   1620 
   1621 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
   1622 {
   1623     return -ENOSYS;
   1624 }
   1625 
   1626 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
   1627 {
   1628     return -ENOSYS;
   1629 }
   1630 
   1631 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
   1632 {
   1633     return 0;
   1634 }
   1635 
   1636 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
   1637 {
   1638     struct audio_device *adev = (struct audio_device *)dev;
   1639 
   1640     adev->mic_mute = state;
   1641 
   1642     return 0;
   1643 }
   1644 
   1645 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
   1646 {
   1647     struct audio_device *adev = (struct audio_device *)dev;
   1648 
   1649     *state = adev->mic_mute;
   1650 
   1651     return 0;
   1652 }
   1653 
   1654 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
   1655                                          const struct audio_config *config)
   1656 {
   1657 
   1658     return get_input_buffer_size(config->sample_rate, config->format,
   1659                                  popcount(config->channel_mask));
   1660 }
   1661 
   1662 static int adev_open_input_stream(struct audio_hw_device *dev,
   1663                                   audio_io_handle_t handle,
   1664                                   audio_devices_t devices,
   1665                                   struct audio_config *config,
   1666                                   struct audio_stream_in **stream_in)
   1667 {
   1668     struct audio_device *adev = (struct audio_device *)dev;
   1669     struct stream_in *in;
   1670     int ret;
   1671 
   1672     *stream_in = NULL;
   1673 
   1674     /* Respond with a request for mono if a different format is given. */
   1675     if (config->channel_mask != AUDIO_CHANNEL_IN_MONO &&
   1676             config->channel_mask != AUDIO_CHANNEL_IN_FRONT_BACK) {
   1677         config->channel_mask = AUDIO_CHANNEL_IN_MONO;
   1678         return -EINVAL;
   1679     }
   1680 
   1681     in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
   1682     if (!in)
   1683         return -ENOMEM;
   1684 
   1685     in->stream.common.get_sample_rate = in_get_sample_rate;
   1686     in->stream.common.set_sample_rate = in_set_sample_rate;
   1687     in->stream.common.get_buffer_size = in_get_buffer_size;
   1688     in->stream.common.get_channels = in_get_channels;
   1689     in->stream.common.get_format = in_get_format;
   1690     in->stream.common.set_format = in_set_format;
   1691     in->stream.common.standby = in_standby;
   1692     in->stream.common.dump = in_dump;
   1693     in->stream.common.set_parameters = in_set_parameters;
   1694     in->stream.common.get_parameters = in_get_parameters;
   1695     in->stream.common.add_audio_effect = in_add_audio_effect;
   1696     in->stream.common.remove_audio_effect = in_remove_audio_effect;
   1697     in->stream.set_gain = in_set_gain;
   1698     in->stream.read = in_read;
   1699     in->stream.get_input_frames_lost = in_get_input_frames_lost;
   1700 
   1701     in->dev = adev;
   1702     in->standby = true;
   1703     in->requested_rate = config->sample_rate;
   1704     in->input_source = AUDIO_SOURCE_DEFAULT;
   1705     /* strip AUDIO_DEVICE_BIT_IN to allow bitwise comparisons */
   1706     in->device = devices & ~AUDIO_DEVICE_BIT_IN;
   1707     in->io_handle = handle;
   1708     in->channel_mask = config->channel_mask;
   1709 
   1710     in->buffer = malloc(pcm_config_in.period_size * pcm_config_in.channels
   1711                                                * audio_stream_frame_size(&in->stream.common));
   1712 
   1713     if (!in->buffer) {
   1714         ret = -ENOMEM;
   1715         goto err_malloc;
   1716     }
   1717 
   1718     if (in->requested_rate != pcm_config_in.rate) {
   1719         in->buf_provider.get_next_buffer = get_next_buffer;
   1720         in->buf_provider.release_buffer = release_buffer;
   1721 
   1722         ret = create_resampler(pcm_config_in.rate,
   1723                                in->requested_rate,
   1724                                popcount(in->channel_mask),
   1725                                RESAMPLER_QUALITY_DEFAULT,
   1726                                &in->buf_provider,
   1727                                &in->resampler);
   1728         if (ret != 0) {
   1729             ret = -EINVAL;
   1730             goto err_resampler;
   1731         }
   1732     }
   1733 
   1734     *stream_in = &in->stream;
   1735     return 0;
   1736 
   1737 err_resampler:
   1738     free(in->buffer);
   1739 err_malloc:
   1740     free(in);
   1741     return ret;
   1742 }
   1743 
   1744 static void adev_close_input_stream(struct audio_hw_device *dev,
   1745                                    struct audio_stream_in *stream)
   1746 {
   1747     struct stream_in *in = (struct stream_in *)stream;
   1748 
   1749     in_standby(&stream->common);
   1750     if (in->resampler) {
   1751         release_resampler(in->resampler);
   1752         in->resampler = NULL;
   1753     }
   1754     free(in->buffer);
   1755     free(stream);
   1756 }
   1757 
   1758 static int adev_dump(const audio_hw_device_t *device, int fd)
   1759 {
   1760     return 0;
   1761 }
   1762 
   1763 static int adev_close(hw_device_t *device)
   1764 {
   1765     struct audio_device *adev = (struct audio_device *)device;
   1766 
   1767     audio_route_free(adev->ar);
   1768 
   1769     eS305_Release();
   1770 
   1771     if (adev->hdmi_drv_fd >= 0)
   1772         close(adev->hdmi_drv_fd);
   1773 
   1774     if (adev->bubble_level)
   1775         bubble_level_release(adev->bubble_level);
   1776 
   1777     free(device);
   1778     return 0;
   1779 }
   1780 
   1781 static int adev_open(const hw_module_t* module, const char* name,
   1782                      hw_device_t** device)
   1783 {
   1784     struct audio_device *adev;
   1785     int ret;
   1786 
   1787     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
   1788         return -EINVAL;
   1789 
   1790     adev = calloc(1, sizeof(struct audio_device));
   1791     if (!adev)
   1792         return -ENOMEM;
   1793 
   1794     adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
   1795     adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
   1796     adev->hw_device.common.module = (struct hw_module_t *) module;
   1797     adev->hw_device.common.close = adev_close;
   1798 
   1799     adev->hw_device.init_check = adev_init_check;
   1800     adev->hw_device.set_voice_volume = adev_set_voice_volume;
   1801     adev->hw_device.set_master_volume = adev_set_master_volume;
   1802     adev->hw_device.set_mode = adev_set_mode;
   1803     adev->hw_device.set_mic_mute = adev_set_mic_mute;
   1804     adev->hw_device.get_mic_mute = adev_get_mic_mute;
   1805     adev->hw_device.set_parameters = adev_set_parameters;
   1806     adev->hw_device.get_parameters = adev_get_parameters;
   1807     adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
   1808     adev->hw_device.open_output_stream = adev_open_output_stream;
   1809     adev->hw_device.close_output_stream = adev_close_output_stream;
   1810     adev->hw_device.open_input_stream = adev_open_input_stream;
   1811     adev->hw_device.close_input_stream = adev_close_input_stream;
   1812     adev->hw_device.dump = adev_dump;
   1813 
   1814     adev->ar = audio_route_init(MIXER_CARD, NULL);
   1815     adev->input_source = AUDIO_SOURCE_DEFAULT;
   1816     /* adev->cur_route_id initial value is 0 and such that first device
   1817      * selection is always applied by select_devices() */
   1818 
   1819     adev->es305_preset = ES305_PRESET_INIT;
   1820     adev->es305_new_mode = ES305_MODE_LEVEL;
   1821     adev->es305_mode = ES305_MODE_LEVEL;
   1822     adev->hdmi_drv_fd = -1;
   1823 
   1824     *device = &adev->hw_device.common;
   1825 
   1826     return 0;
   1827 }
   1828 
   1829 static struct hw_module_methods_t hal_module_methods = {
   1830     .open = adev_open,
   1831 };
   1832 
   1833 struct audio_module HAL_MODULE_INFO_SYM = {
   1834     .common = {
   1835         .tag = HARDWARE_MODULE_TAG,
   1836         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
   1837         .hal_api_version = HARDWARE_HAL_API_VERSION,
   1838         .id = AUDIO_HARDWARE_MODULE_ID,
   1839         .name = "Manta audio HW HAL",
   1840         .author = "The Android Open Source Project",
   1841         .methods = &hal_module_methods,
   1842     },
   1843 };
   1844