Home | History | Annotate | Download | only in hal
      1 /*
      2  * Copyright (C) 2013-2014 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 /*#define VERY_VERY_VERBOSE_LOGGING*/
     20 #ifdef VERY_VERY_VERBOSE_LOGGING
     21 #define ALOGVV ALOGV
     22 #else
     23 #define ALOGVV(a...) do { } while(0)
     24 #endif
     25 
     26 #include <errno.h>
     27 #include <pthread.h>
     28 #include <stdint.h>
     29 #include <sys/time.h>
     30 #include <stdlib.h>
     31 #include <math.h>
     32 #include <dlfcn.h>
     33 #include <sys/resource.h>
     34 #include <sys/prctl.h>
     35 
     36 #include <cutils/log.h>
     37 #include <cutils/str_parms.h>
     38 #include <cutils/properties.h>
     39 #include <cutils/atomic.h>
     40 #include <cutils/sched_policy.h>
     41 
     42 #include <hardware/audio_effect.h>
     43 #include <hardware/audio_alsaops.h>
     44 #include <system/thread_defs.h>
     45 #include <audio_effects/effect_aec.h>
     46 #include <audio_effects/effect_ns.h>
     47 #include "audio_hw.h"
     48 #include "audio_extn.h"
     49 #include "platform_api.h"
     50 #include <platform.h>
     51 #include "voice_extn.h"
     52 
     53 #include "sound/compress_params.h"
     54 
     55 #define COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024)
     56 // 2 buffers causes problems with high bitrate files
     57 #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 3
     58 /* ToDo: Check and update a proper value in msec */
     59 #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
     60 #define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
     61 
     62 #define PROXY_OPEN_RETRY_COUNT           100
     63 #define PROXY_OPEN_WAIT_TIME             20
     64 
     65 static unsigned int configured_low_latency_capture_period_size =
     66         LOW_LATENCY_CAPTURE_PERIOD_SIZE;
     67 
     68 /* This constant enables extended precision handling.
     69  * TODO The flag is off until more testing is done.
     70  */
     71 static const bool k_enable_extended_precision = false;
     72 
     73 struct pcm_config pcm_config_deep_buffer = {
     74     .channels = 2,
     75     .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
     76     .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
     77     .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
     78     .format = PCM_FORMAT_S16_LE,
     79     .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
     80     .stop_threshold = INT_MAX,
     81     .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
     82 };
     83 
     84 struct pcm_config pcm_config_low_latency = {
     85     .channels = 2,
     86     .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
     87     .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
     88     .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
     89     .format = PCM_FORMAT_S16_LE,
     90     .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
     91     .stop_threshold = INT_MAX,
     92     .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
     93 };
     94 
     95 struct pcm_config pcm_config_hdmi_multi = {
     96     .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
     97     .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
     98     .period_size = HDMI_MULTI_PERIOD_SIZE,
     99     .period_count = HDMI_MULTI_PERIOD_COUNT,
    100     .format = PCM_FORMAT_S16_LE,
    101     .start_threshold = 0,
    102     .stop_threshold = INT_MAX,
    103     .avail_min = 0,
    104 };
    105 
    106 struct pcm_config pcm_config_audio_capture = {
    107     .channels = 2,
    108     .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
    109     .format = PCM_FORMAT_S16_LE,
    110     .stop_threshold = INT_MAX,
    111     .avail_min = 0,
    112 };
    113 
    114 #define AFE_PROXY_CHANNEL_COUNT 2
    115 #define AFE_PROXY_SAMPLING_RATE 48000
    116 
    117 #define AFE_PROXY_PLAYBACK_PERIOD_SIZE  768
    118 #define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
    119 
    120 struct pcm_config pcm_config_afe_proxy_playback = {
    121     .channels = AFE_PROXY_CHANNEL_COUNT,
    122     .rate = AFE_PROXY_SAMPLING_RATE,
    123     .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
    124     .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
    125     .format = PCM_FORMAT_S16_LE,
    126     .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
    127     .stop_threshold = INT_MAX,
    128     .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
    129 };
    130 
    131 #define AFE_PROXY_RECORD_PERIOD_SIZE  768
    132 #define AFE_PROXY_RECORD_PERIOD_COUNT 4
    133 
    134 struct pcm_config pcm_config_afe_proxy_record = {
    135     .channels = AFE_PROXY_CHANNEL_COUNT,
    136     .rate = AFE_PROXY_SAMPLING_RATE,
    137     .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
    138     .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
    139     .format = PCM_FORMAT_S16_LE,
    140     .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
    141     .stop_threshold = INT_MAX,
    142     .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
    143 };
    144 
    145 const char * const use_case_table[AUDIO_USECASE_MAX] = {
    146     [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
    147     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
    148     [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
    149     [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
    150     [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
    151 
    152     [USECASE_AUDIO_RECORD] = "audio-record",
    153     [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
    154 
    155     [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
    156     [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
    157 
    158     [USECASE_VOICE_CALL] = "voice-call",
    159     [USECASE_VOICE2_CALL] = "voice2-call",
    160     [USECASE_VOLTE_CALL] = "volte-call",
    161     [USECASE_QCHAT_CALL] = "qchat-call",
    162     [USECASE_VOWLAN_CALL] = "vowlan-call",
    163 
    164     [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
    165     [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
    166 
    167     [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
    168     [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
    169 };
    170 
    171 
    172 #define STRING_TO_ENUM(string) { #string, string }
    173 
    174 struct string_to_enum {
    175     const char *name;
    176     uint32_t value;
    177 };
    178 
    179 static const struct string_to_enum out_channels_name_to_enum_table[] = {
    180     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
    181     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
    182     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
    183 };
    184 
    185 static int set_voice_volume_l(struct audio_device *adev, float volume);
    186 static struct audio_device *adev = NULL;
    187 static pthread_mutex_t adev_init_lock;
    188 static unsigned int audio_device_ref_count;
    189 
    190 __attribute__ ((visibility ("default")))
    191 bool audio_hw_send_gain_dep_calibration(int level) {
    192     bool ret_val = false;
    193     ALOGV("%s: enter ... ", __func__);
    194 
    195     pthread_mutex_lock(&adev_init_lock);
    196 
    197     if (adev != NULL && adev->platform != NULL) {
    198         pthread_mutex_lock(&adev->lock);
    199         ret_val = platform_send_gain_dep_cal(adev->platform, level);
    200         pthread_mutex_unlock(&adev->lock);
    201     } else {
    202         ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
    203     }
    204 
    205     pthread_mutex_unlock(&adev_init_lock);
    206 
    207     ALOGV("%s: exit with ret_val %d ", __func__, ret_val);
    208     return ret_val;
    209 }
    210 
    211 static bool is_supported_format(audio_format_t format)
    212 {
    213     switch (format) {
    214         case AUDIO_FORMAT_MP3:
    215         case AUDIO_FORMAT_AAC_LC:
    216         case AUDIO_FORMAT_AAC_HE_V1:
    217         case AUDIO_FORMAT_AAC_HE_V2:
    218             return true;
    219         default:
    220             break;
    221     }
    222     return false;
    223 }
    224 
    225 static int get_snd_codec_id(audio_format_t format)
    226 {
    227     int id = 0;
    228 
    229     switch (format & AUDIO_FORMAT_MAIN_MASK) {
    230     case AUDIO_FORMAT_MP3:
    231         id = SND_AUDIOCODEC_MP3;
    232         break;
    233     case AUDIO_FORMAT_AAC:
    234         id = SND_AUDIOCODEC_AAC;
    235         break;
    236     default:
    237         ALOGE("%s: Unsupported audio format", __func__);
    238     }
    239 
    240     return id;
    241 }
    242 
    243 int enable_audio_route(struct audio_device *adev,
    244                        struct audio_usecase *usecase)
    245 {
    246     snd_device_t snd_device;
    247     char mixer_path[50];
    248 
    249     if (usecase == NULL)
    250         return -EINVAL;
    251 
    252     ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
    253 
    254     if (usecase->type == PCM_CAPTURE)
    255         snd_device = usecase->in_snd_device;
    256     else
    257         snd_device = usecase->out_snd_device;
    258 
    259     strcpy(mixer_path, use_case_table[usecase->id]);
    260     platform_add_backend_name(adev->platform, mixer_path, snd_device);
    261     ALOGD("%s: apply and update mixer path: %s", __func__, mixer_path);
    262     audio_route_apply_and_update_path(adev->audio_route, mixer_path);
    263 
    264     ALOGV("%s: exit", __func__);
    265     return 0;
    266 }
    267 
    268 int disable_audio_route(struct audio_device *adev,
    269                         struct audio_usecase *usecase)
    270 {
    271     snd_device_t snd_device;
    272     char mixer_path[50];
    273 
    274     if (usecase == NULL)
    275         return -EINVAL;
    276 
    277     ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
    278     if (usecase->type == PCM_CAPTURE)
    279         snd_device = usecase->in_snd_device;
    280     else
    281         snd_device = usecase->out_snd_device;
    282     strcpy(mixer_path, use_case_table[usecase->id]);
    283     platform_add_backend_name(adev->platform, mixer_path, snd_device);
    284     ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
    285     audio_route_reset_and_update_path(adev->audio_route, mixer_path);
    286 
    287     ALOGV("%s: exit", __func__);
    288     return 0;
    289 }
    290 
    291 int enable_snd_device(struct audio_device *adev,
    292                       snd_device_t snd_device)
    293 {
    294     int i, num_devices = 0;
    295     snd_device_t new_snd_devices[2];
    296 
    297     if (snd_device < SND_DEVICE_MIN ||
    298         snd_device >= SND_DEVICE_MAX) {
    299         ALOGE("%s: Invalid sound device %d", __func__, snd_device);
    300         return -EINVAL;
    301     }
    302 
    303     adev->snd_dev_ref_cnt[snd_device]++;
    304     if (adev->snd_dev_ref_cnt[snd_device] > 1) {
    305         ALOGV("%s: snd_device(%d: %s) is already active",
    306               __func__, snd_device, platform_get_snd_device_name(snd_device));
    307         return 0;
    308     }
    309 
    310     /* due to the possibility of calibration overwrite between listen
    311         and audio, notify sound trigger hal before audio calibration is sent */
    312     audio_extn_sound_trigger_update_device_status(snd_device,
    313                                     ST_EVENT_SND_DEVICE_BUSY);
    314 
    315     if (audio_extn_spkr_prot_is_enabled())
    316          audio_extn_spkr_prot_calib_cancel(adev);
    317 
    318     if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
    319         adev->snd_dev_ref_cnt[snd_device]--;
    320         audio_extn_sound_trigger_update_device_status(snd_device,
    321                                     ST_EVENT_SND_DEVICE_FREE);
    322         return -EINVAL;
    323     }
    324 
    325     audio_extn_dsm_feedback_enable(adev, snd_device, true);
    326 
    327     if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
    328         snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
    329         audio_extn_spkr_prot_is_enabled()) {
    330         if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
    331             adev->snd_dev_ref_cnt[snd_device]--;
    332             return -EINVAL;
    333         }
    334         if (audio_extn_spkr_prot_start_processing(snd_device)) {
    335             ALOGE("%s: spkr_start_processing failed", __func__);
    336             return -EINVAL;
    337         }
    338     } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
    339         for (i = 0; i < num_devices; i++) {
    340             enable_snd_device(adev, new_snd_devices[i]);
    341         }
    342     } else {
    343         const char * dev_path = platform_get_snd_device_name(snd_device);
    344         ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
    345         audio_route_apply_and_update_path(adev->audio_route, dev_path);
    346     }
    347 
    348     return 0;
    349 }
    350 
    351 int disable_snd_device(struct audio_device *adev,
    352                        snd_device_t snd_device)
    353 {
    354     int i, num_devices = 0;
    355     snd_device_t new_snd_devices[2];
    356 
    357     if (snd_device < SND_DEVICE_MIN ||
    358         snd_device >= SND_DEVICE_MAX) {
    359         ALOGE("%s: Invalid sound device %d", __func__, snd_device);
    360         return -EINVAL;
    361     }
    362     if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
    363         ALOGE("%s: device ref cnt is already 0", __func__);
    364         return -EINVAL;
    365     }
    366     adev->snd_dev_ref_cnt[snd_device]--;
    367     if (adev->snd_dev_ref_cnt[snd_device] == 0) {
    368         const char * dev_path = platform_get_snd_device_name(snd_device);
    369         ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
    370 
    371         audio_extn_dsm_feedback_enable(adev, snd_device, false);
    372         if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
    373             snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
    374             audio_extn_spkr_prot_is_enabled()) {
    375             audio_extn_spkr_prot_stop_processing(snd_device);
    376         } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
    377             for (i = 0; i < num_devices; i++) {
    378                 disable_snd_device(adev, new_snd_devices[i]);
    379             }
    380         } else {
    381             audio_route_reset_and_update_path(adev->audio_route, dev_path);
    382         }
    383         audio_extn_sound_trigger_update_device_status(snd_device,
    384                                         ST_EVENT_SND_DEVICE_FREE);
    385     }
    386     return 0;
    387 }
    388 
    389 static void check_and_route_playback_usecases(struct audio_device *adev,
    390                                               struct audio_usecase *uc_info,
    391                                               snd_device_t snd_device)
    392 {
    393     struct listnode *node;
    394     struct audio_usecase *usecase;
    395     bool switch_device[AUDIO_USECASE_MAX];
    396     int i, num_uc_to_switch = 0;
    397 
    398     /*
    399      * This function is to make sure that all the usecases that are active on
    400      * the hardware codec backend are always routed to any one device that is
    401      * handled by the hardware codec.
    402      * For example, if low-latency and deep-buffer usecases are currently active
    403      * on speaker and out_set_parameters(headset) is received on low-latency
    404      * output, then we have to make sure deep-buffer is also switched to headset,
    405      * because of the limitation that both the devices cannot be enabled
    406      * at the same time as they share the same backend.
    407      */
    408     /* Disable all the usecases on the shared backend other than the
    409        specified usecase */
    410     for (i = 0; i < AUDIO_USECASE_MAX; i++)
    411         switch_device[i] = false;
    412 
    413     list_for_each(node, &adev->usecase_list) {
    414         usecase = node_to_item(node, struct audio_usecase, list);
    415         if (usecase->type != PCM_CAPTURE &&
    416                 usecase != uc_info &&
    417                 usecase->out_snd_device != snd_device &&
    418                 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND &&
    419                 platform_check_backends_match(snd_device, usecase->out_snd_device)) {
    420             ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
    421                   __func__, use_case_table[usecase->id],
    422                   platform_get_snd_device_name(usecase->out_snd_device));
    423             disable_audio_route(adev, usecase);
    424             switch_device[usecase->id] = true;
    425             num_uc_to_switch++;
    426         }
    427     }
    428 
    429     if (num_uc_to_switch) {
    430         list_for_each(node, &adev->usecase_list) {
    431             usecase = node_to_item(node, struct audio_usecase, list);
    432             if (switch_device[usecase->id]) {
    433                 disable_snd_device(adev, usecase->out_snd_device);
    434             }
    435         }
    436 
    437         list_for_each(node, &adev->usecase_list) {
    438             usecase = node_to_item(node, struct audio_usecase, list);
    439             if (switch_device[usecase->id]) {
    440                 enable_snd_device(adev, snd_device);
    441             }
    442         }
    443 
    444         /* Re-route all the usecases on the shared backend other than the
    445            specified usecase to new snd devices */
    446         list_for_each(node, &adev->usecase_list) {
    447             usecase = node_to_item(node, struct audio_usecase, list);
    448             /* Update the out_snd_device only before enabling the audio route */
    449             if (switch_device[usecase->id] ) {
    450                 usecase->out_snd_device = snd_device;
    451                 enable_audio_route(adev, usecase);
    452             }
    453         }
    454     }
    455 }
    456 
    457 static void check_and_route_capture_usecases(struct audio_device *adev,
    458                                              struct audio_usecase *uc_info,
    459                                              snd_device_t snd_device)
    460 {
    461     struct listnode *node;
    462     struct audio_usecase *usecase;
    463     bool switch_device[AUDIO_USECASE_MAX];
    464     int i, num_uc_to_switch = 0;
    465 
    466     /*
    467      * This function is to make sure that all the active capture usecases
    468      * are always routed to the same input sound device.
    469      * For example, if audio-record and voice-call usecases are currently
    470      * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
    471      * is received for voice call then we have to make sure that audio-record
    472      * usecase is also switched to earpiece i.e. voice-dmic-ef,
    473      * because of the limitation that two devices cannot be enabled
    474      * at the same time if they share the same backend.
    475      */
    476     for (i = 0; i < AUDIO_USECASE_MAX; i++)
    477         switch_device[i] = false;
    478 
    479     list_for_each(node, &adev->usecase_list) {
    480         usecase = node_to_item(node, struct audio_usecase, list);
    481         if (usecase->type != PCM_PLAYBACK &&
    482                 usecase != uc_info &&
    483                 usecase->in_snd_device != snd_device &&
    484                 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
    485             ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
    486                   __func__, use_case_table[usecase->id],
    487                   platform_get_snd_device_name(usecase->in_snd_device));
    488             disable_audio_route(adev, usecase);
    489             switch_device[usecase->id] = true;
    490             num_uc_to_switch++;
    491         }
    492     }
    493 
    494     if (num_uc_to_switch) {
    495         list_for_each(node, &adev->usecase_list) {
    496             usecase = node_to_item(node, struct audio_usecase, list);
    497             if (switch_device[usecase->id]) {
    498                 disable_snd_device(adev, usecase->in_snd_device);
    499             }
    500         }
    501 
    502         list_for_each(node, &adev->usecase_list) {
    503             usecase = node_to_item(node, struct audio_usecase, list);
    504             if (switch_device[usecase->id]) {
    505                 enable_snd_device(adev, snd_device);
    506             }
    507         }
    508 
    509         /* Re-route all the usecases on the shared backend other than the
    510            specified usecase to new snd devices */
    511         list_for_each(node, &adev->usecase_list) {
    512             usecase = node_to_item(node, struct audio_usecase, list);
    513             /* Update the in_snd_device only before enabling the audio route */
    514             if (switch_device[usecase->id] ) {
    515                 usecase->in_snd_device = snd_device;
    516                 enable_audio_route(adev, usecase);
    517             }
    518         }
    519     }
    520 }
    521 
    522 /* must be called with hw device mutex locked */
    523 static int read_hdmi_channel_masks(struct stream_out *out)
    524 {
    525     int ret = 0;
    526     int channels = platform_edid_get_max_channels(out->dev->platform);
    527 
    528     switch (channels) {
    529         /*
    530          * Do not handle stereo output in Multi-channel cases
    531          * Stereo case is handled in normal playback path
    532          */
    533     case 6:
    534         ALOGV("%s: HDMI supports 5.1", __func__);
    535         out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
    536         break;
    537     case 8:
    538         ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
    539         out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
    540         out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
    541         break;
    542     default:
    543         ALOGE("HDMI does not support multi channel playback");
    544         ret = -ENOSYS;
    545         break;
    546     }
    547     return ret;
    548 }
    549 
    550 static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
    551 {
    552     struct audio_usecase *usecase;
    553     struct listnode *node;
    554 
    555     list_for_each(node, &adev->usecase_list) {
    556         usecase = node_to_item(node, struct audio_usecase, list);
    557         if (usecase->type == VOICE_CALL) {
    558             ALOGV("%s: usecase id %d", __func__, usecase->id);
    559             return usecase->id;
    560         }
    561     }
    562     return USECASE_INVALID;
    563 }
    564 
    565 struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
    566                                             audio_usecase_t uc_id)
    567 {
    568     struct audio_usecase *usecase;
    569     struct listnode *node;
    570 
    571     list_for_each(node, &adev->usecase_list) {
    572         usecase = node_to_item(node, struct audio_usecase, list);
    573         if (usecase->id == uc_id)
    574             return usecase;
    575     }
    576     return NULL;
    577 }
    578 
    579 int select_devices(struct audio_device *adev,
    580                    audio_usecase_t uc_id)
    581 {
    582     snd_device_t out_snd_device = SND_DEVICE_NONE;
    583     snd_device_t in_snd_device = SND_DEVICE_NONE;
    584     struct audio_usecase *usecase = NULL;
    585     struct audio_usecase *vc_usecase = NULL;
    586     struct audio_usecase *hfp_usecase = NULL;
    587     audio_usecase_t hfp_ucid;
    588     struct listnode *node;
    589     int status = 0;
    590 
    591     usecase = get_usecase_from_list(adev, uc_id);
    592     if (usecase == NULL) {
    593         ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
    594         return -EINVAL;
    595     }
    596 
    597     if ((usecase->type == VOICE_CALL) ||
    598         (usecase->type == PCM_HFP_CALL)) {
    599         out_snd_device = platform_get_output_snd_device(adev->platform,
    600                                                         usecase->stream.out->devices);
    601         in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
    602         usecase->devices = usecase->stream.out->devices;
    603     } else {
    604         /*
    605          * If the voice call is active, use the sound devices of voice call usecase
    606          * so that it would not result any device switch. All the usecases will
    607          * be switched to new device when select_devices() is called for voice call
    608          * usecase. This is to avoid switching devices for voice call when
    609          * check_and_route_playback_usecases() is called below.
    610          */
    611         if (voice_is_in_call(adev)) {
    612             vc_usecase = get_usecase_from_list(adev,
    613                                                get_voice_usecase_id_from_list(adev));
    614             if ((vc_usecase != NULL) &&
    615                 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
    616                 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
    617                 in_snd_device = vc_usecase->in_snd_device;
    618                 out_snd_device = vc_usecase->out_snd_device;
    619             }
    620         } else if (audio_extn_hfp_is_active(adev)) {
    621             hfp_ucid = audio_extn_hfp_get_usecase();
    622             hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
    623             if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
    624                    in_snd_device = hfp_usecase->in_snd_device;
    625                    out_snd_device = hfp_usecase->out_snd_device;
    626             }
    627         }
    628         if (usecase->type == PCM_PLAYBACK) {
    629             usecase->devices = usecase->stream.out->devices;
    630             in_snd_device = SND_DEVICE_NONE;
    631             if (out_snd_device == SND_DEVICE_NONE) {
    632                 out_snd_device = platform_get_output_snd_device(adev->platform,
    633                                             usecase->stream.out->devices);
    634                 if (usecase->stream.out == adev->primary_output &&
    635                         adev->active_input &&
    636                         adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
    637                         out_snd_device != usecase->out_snd_device) {
    638                     select_devices(adev, adev->active_input->usecase);
    639                 }
    640             }
    641         } else if (usecase->type == PCM_CAPTURE) {
    642             usecase->devices = usecase->stream.in->device;
    643             out_snd_device = SND_DEVICE_NONE;
    644             if (in_snd_device == SND_DEVICE_NONE) {
    645                 audio_devices_t out_device = AUDIO_DEVICE_NONE;
    646                 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
    647                         adev->primary_output && !adev->primary_output->standby) {
    648                     out_device = adev->primary_output->devices;
    649                     platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
    650                 } else if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
    651                     out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
    652                 }
    653                 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
    654             }
    655         }
    656     }
    657 
    658     if (out_snd_device == usecase->out_snd_device &&
    659         in_snd_device == usecase->in_snd_device) {
    660         return 0;
    661     }
    662 
    663     ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
    664           out_snd_device, platform_get_snd_device_name(out_snd_device),
    665           in_snd_device,  platform_get_snd_device_name(in_snd_device));
    666 
    667     /*
    668      * Limitation: While in call, to do a device switch we need to disable
    669      * and enable both RX and TX devices though one of them is same as current
    670      * device.
    671      */
    672     if ((usecase->type == VOICE_CALL) &&
    673         (usecase->in_snd_device != SND_DEVICE_NONE) &&
    674         (usecase->out_snd_device != SND_DEVICE_NONE)) {
    675         status = platform_switch_voice_call_device_pre(adev->platform);
    676     }
    677 
    678     /* Disable current sound devices */
    679     if (usecase->out_snd_device != SND_DEVICE_NONE) {
    680         disable_audio_route(adev, usecase);
    681         disable_snd_device(adev, usecase->out_snd_device);
    682     }
    683 
    684     if (usecase->in_snd_device != SND_DEVICE_NONE) {
    685         disable_audio_route(adev, usecase);
    686         disable_snd_device(adev, usecase->in_snd_device);
    687     }
    688 
    689     /* Applicable only on the targets that has external modem.
    690      * New device information should be sent to modem before enabling
    691      * the devices to reduce in-call device switch time.
    692      */
    693     if ((usecase->type == VOICE_CALL) &&
    694         (usecase->in_snd_device != SND_DEVICE_NONE) &&
    695         (usecase->out_snd_device != SND_DEVICE_NONE)) {
    696         status = platform_switch_voice_call_enable_device_config(adev->platform,
    697                                                                  out_snd_device,
    698                                                                  in_snd_device);
    699     }
    700 
    701     /* Enable new sound devices */
    702     if (out_snd_device != SND_DEVICE_NONE) {
    703         if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
    704             check_and_route_playback_usecases(adev, usecase, out_snd_device);
    705         enable_snd_device(adev, out_snd_device);
    706     }
    707 
    708     if (in_snd_device != SND_DEVICE_NONE) {
    709         check_and_route_capture_usecases(adev, usecase, in_snd_device);
    710         enable_snd_device(adev, in_snd_device);
    711     }
    712 
    713     if (usecase->type == VOICE_CALL)
    714         status = platform_switch_voice_call_device_post(adev->platform,
    715                                                         out_snd_device,
    716                                                         in_snd_device);
    717 
    718     usecase->in_snd_device = in_snd_device;
    719     usecase->out_snd_device = out_snd_device;
    720 
    721     enable_audio_route(adev, usecase);
    722 
    723     /* Applicable only on the targets that has external modem.
    724      * Enable device command should be sent to modem only after
    725      * enabling voice call mixer controls
    726      */
    727     if (usecase->type == VOICE_CALL)
    728         status = platform_switch_voice_call_usecase_route_post(adev->platform,
    729                                                                out_snd_device,
    730                                                                in_snd_device);
    731 
    732     return status;
    733 }
    734 
    735 static int stop_input_stream(struct stream_in *in)
    736 {
    737     int i, ret = 0;
    738     struct audio_usecase *uc_info;
    739     struct audio_device *adev = in->dev;
    740 
    741     adev->active_input = NULL;
    742 
    743     ALOGV("%s: enter: usecase(%d: %s)", __func__,
    744           in->usecase, use_case_table[in->usecase]);
    745     uc_info = get_usecase_from_list(adev, in->usecase);
    746     if (uc_info == NULL) {
    747         ALOGE("%s: Could not find the usecase (%d) in the list",
    748               __func__, in->usecase);
    749         return -EINVAL;
    750     }
    751 
    752     /* 1. Disable stream specific mixer controls */
    753     disable_audio_route(adev, uc_info);
    754 
    755     /* 2. Disable the tx device */
    756     disable_snd_device(adev, uc_info->in_snd_device);
    757 
    758     list_remove(&uc_info->list);
    759     free(uc_info);
    760 
    761     ALOGV("%s: exit: status(%d)", __func__, ret);
    762     return ret;
    763 }
    764 
    765 int start_input_stream(struct stream_in *in)
    766 {
    767     /* 1. Enable output device and stream routing controls */
    768     int ret = 0;
    769     struct audio_usecase *uc_info;
    770     struct audio_device *adev = in->dev;
    771 
    772     ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
    773     in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
    774     if (in->pcm_device_id < 0) {
    775         ALOGE("%s: Could not find PCM device id for the usecase(%d)",
    776               __func__, in->usecase);
    777         ret = -EINVAL;
    778         goto error_config;
    779     }
    780 
    781     adev->active_input = in;
    782     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
    783     uc_info->id = in->usecase;
    784     uc_info->type = PCM_CAPTURE;
    785     uc_info->stream.in = in;
    786     uc_info->devices = in->device;
    787     uc_info->in_snd_device = SND_DEVICE_NONE;
    788     uc_info->out_snd_device = SND_DEVICE_NONE;
    789 
    790     list_add_tail(&adev->usecase_list, &uc_info->list);
    791     select_devices(adev, in->usecase);
    792 
    793     ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
    794           __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
    795 
    796     unsigned int flags = PCM_IN;
    797     unsigned int pcm_open_retry_count = 0;
    798 
    799     if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
    800         flags |= PCM_MMAP | PCM_NOIRQ;
    801         pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
    802     }
    803 
    804     while (1) {
    805         in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
    806                            flags, &in->config);
    807         if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
    808             ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
    809             if (in->pcm != NULL) {
    810                 pcm_close(in->pcm);
    811                 in->pcm = NULL;
    812             }
    813             if (pcm_open_retry_count-- == 0) {
    814                 ret = -EIO;
    815                 goto error_open;
    816             }
    817             usleep(PROXY_OPEN_WAIT_TIME * 1000);
    818             continue;
    819         }
    820         break;
    821     }
    822 
    823     ALOGV("%s: exit", __func__);
    824     return ret;
    825 
    826 error_open:
    827     stop_input_stream(in);
    828 
    829 error_config:
    830     adev->active_input = NULL;
    831     ALOGD("%s: exit: status(%d)", __func__, ret);
    832 
    833     return ret;
    834 }
    835 
    836 /* must be called with out->lock locked */
    837 static int send_offload_cmd_l(struct stream_out* out, int command)
    838 {
    839     struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
    840 
    841     ALOGVV("%s %d", __func__, command);
    842 
    843     cmd->cmd = command;
    844     list_add_tail(&out->offload_cmd_list, &cmd->node);
    845     pthread_cond_signal(&out->offload_cond);
    846     return 0;
    847 }
    848 
    849 /* must be called iwth out->lock locked */
    850 static void stop_compressed_output_l(struct stream_out *out)
    851 {
    852     out->offload_state = OFFLOAD_STATE_IDLE;
    853     out->playback_started = 0;
    854     out->send_new_metadata = 1;
    855     if (out->compr != NULL) {
    856         compress_stop(out->compr);
    857         while (out->offload_thread_blocked) {
    858             pthread_cond_wait(&out->cond, &out->lock);
    859         }
    860     }
    861 }
    862 
    863 static void *offload_thread_loop(void *context)
    864 {
    865     struct stream_out *out = (struct stream_out *) context;
    866     struct listnode *item;
    867 
    868     out->offload_state = OFFLOAD_STATE_IDLE;
    869     out->playback_started = 0;
    870 
    871     setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
    872     set_sched_policy(0, SP_FOREGROUND);
    873     prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
    874 
    875     ALOGV("%s", __func__);
    876     pthread_mutex_lock(&out->lock);
    877     for (;;) {
    878         struct offload_cmd *cmd = NULL;
    879         stream_callback_event_t event;
    880         bool send_callback = false;
    881 
    882         ALOGVV("%s offload_cmd_list %d out->offload_state %d",
    883               __func__, list_empty(&out->offload_cmd_list),
    884               out->offload_state);
    885         if (list_empty(&out->offload_cmd_list)) {
    886             ALOGV("%s SLEEPING", __func__);
    887             pthread_cond_wait(&out->offload_cond, &out->lock);
    888             ALOGV("%s RUNNING", __func__);
    889             continue;
    890         }
    891 
    892         item = list_head(&out->offload_cmd_list);
    893         cmd = node_to_item(item, struct offload_cmd, node);
    894         list_remove(item);
    895 
    896         ALOGVV("%s STATE %d CMD %d out->compr %p",
    897                __func__, out->offload_state, cmd->cmd, out->compr);
    898 
    899         if (cmd->cmd == OFFLOAD_CMD_EXIT) {
    900             free(cmd);
    901             break;
    902         }
    903 
    904         if (out->compr == NULL) {
    905             ALOGE("%s: Compress handle is NULL", __func__);
    906             pthread_cond_signal(&out->cond);
    907             continue;
    908         }
    909         out->offload_thread_blocked = true;
    910         pthread_mutex_unlock(&out->lock);
    911         send_callback = false;
    912         switch(cmd->cmd) {
    913         case OFFLOAD_CMD_WAIT_FOR_BUFFER:
    914             compress_wait(out->compr, -1);
    915             send_callback = true;
    916             event = STREAM_CBK_EVENT_WRITE_READY;
    917             break;
    918         case OFFLOAD_CMD_PARTIAL_DRAIN:
    919             compress_next_track(out->compr);
    920             compress_partial_drain(out->compr);
    921             send_callback = true;
    922             event = STREAM_CBK_EVENT_DRAIN_READY;
    923             /* Resend the metadata for next iteration */
    924             out->send_new_metadata = 1;
    925             break;
    926         case OFFLOAD_CMD_DRAIN:
    927             compress_drain(out->compr);
    928             send_callback = true;
    929             event = STREAM_CBK_EVENT_DRAIN_READY;
    930             break;
    931         default:
    932             ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
    933             break;
    934         }
    935         pthread_mutex_lock(&out->lock);
    936         out->offload_thread_blocked = false;
    937         pthread_cond_signal(&out->cond);
    938         if (send_callback) {
    939             ALOGVV("%s: sending offload_callback event %d", __func__, event);
    940             out->offload_callback(event, NULL, out->offload_cookie);
    941         }
    942         free(cmd);
    943     }
    944 
    945     pthread_cond_signal(&out->cond);
    946     while (!list_empty(&out->offload_cmd_list)) {
    947         item = list_head(&out->offload_cmd_list);
    948         list_remove(item);
    949         free(node_to_item(item, struct offload_cmd, node));
    950     }
    951     pthread_mutex_unlock(&out->lock);
    952 
    953     return NULL;
    954 }
    955 
    956 static int create_offload_callback_thread(struct stream_out *out)
    957 {
    958     pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
    959     list_init(&out->offload_cmd_list);
    960     pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
    961                     offload_thread_loop, out);
    962     return 0;
    963 }
    964 
    965 static int destroy_offload_callback_thread(struct stream_out *out)
    966 {
    967     pthread_mutex_lock(&out->lock);
    968     stop_compressed_output_l(out);
    969     send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
    970 
    971     pthread_mutex_unlock(&out->lock);
    972     pthread_join(out->offload_thread, (void **) NULL);
    973     pthread_cond_destroy(&out->offload_cond);
    974 
    975     return 0;
    976 }
    977 
    978 static bool allow_hdmi_channel_config(struct audio_device *adev)
    979 {
    980     struct listnode *node;
    981     struct audio_usecase *usecase;
    982     bool ret = true;
    983 
    984     list_for_each(node, &adev->usecase_list) {
    985         usecase = node_to_item(node, struct audio_usecase, list);
    986         if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
    987             /*
    988              * If voice call is already existing, do not proceed further to avoid
    989              * disabling/enabling both RX and TX devices, CSD calls, etc.
    990              * Once the voice call done, the HDMI channels can be configured to
    991              * max channels of remaining use cases.
    992              */
    993             if (usecase->id == USECASE_VOICE_CALL) {
    994                 ALOGD("%s: voice call is active, no change in HDMI channels",
    995                       __func__);
    996                 ret = false;
    997                 break;
    998             } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
    999                 ALOGD("%s: multi channel playback is active, "
   1000                       "no change in HDMI channels", __func__);
   1001                 ret = false;
   1002                 break;
   1003             }
   1004         }
   1005     }
   1006     return ret;
   1007 }
   1008 
   1009 static int check_and_set_hdmi_channels(struct audio_device *adev,
   1010                                        unsigned int channels)
   1011 {
   1012     struct listnode *node;
   1013     struct audio_usecase *usecase;
   1014 
   1015     /* Check if change in HDMI channel config is allowed */
   1016     if (!allow_hdmi_channel_config(adev))
   1017         return 0;
   1018 
   1019     if (channels == adev->cur_hdmi_channels) {
   1020         ALOGD("%s: Requested channels are same as current", __func__);
   1021         return 0;
   1022     }
   1023 
   1024     platform_set_hdmi_channels(adev->platform, channels);
   1025     adev->cur_hdmi_channels = channels;
   1026 
   1027     /*
   1028      * Deroute all the playback streams routed to HDMI so that
   1029      * the back end is deactivated. Note that backend will not
   1030      * be deactivated if any one stream is connected to it.
   1031      */
   1032     list_for_each(node, &adev->usecase_list) {
   1033         usecase = node_to_item(node, struct audio_usecase, list);
   1034         if (usecase->type == PCM_PLAYBACK &&
   1035                 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
   1036             disable_audio_route(adev, usecase);
   1037         }
   1038     }
   1039 
   1040     /*
   1041      * Enable all the streams disabled above. Now the HDMI backend
   1042      * will be activated with new channel configuration
   1043      */
   1044     list_for_each(node, &adev->usecase_list) {
   1045         usecase = node_to_item(node, struct audio_usecase, list);
   1046         if (usecase->type == PCM_PLAYBACK &&
   1047                 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
   1048             enable_audio_route(adev, usecase);
   1049         }
   1050     }
   1051 
   1052     return 0;
   1053 }
   1054 
   1055 static int stop_output_stream(struct stream_out *out)
   1056 {
   1057     int i, ret = 0;
   1058     struct audio_usecase *uc_info;
   1059     struct audio_device *adev = out->dev;
   1060 
   1061     ALOGV("%s: enter: usecase(%d: %s)", __func__,
   1062           out->usecase, use_case_table[out->usecase]);
   1063     uc_info = get_usecase_from_list(adev, out->usecase);
   1064     if (uc_info == NULL) {
   1065         ALOGE("%s: Could not find the usecase (%d) in the list",
   1066               __func__, out->usecase);
   1067         return -EINVAL;
   1068     }
   1069 
   1070     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1071         if (adev->visualizer_stop_output != NULL)
   1072             adev->visualizer_stop_output(out->handle, out->pcm_device_id);
   1073         if (adev->offload_effects_stop_output != NULL)
   1074             adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
   1075     }
   1076 
   1077     /* 1. Get and set stream specific mixer controls */
   1078     disable_audio_route(adev, uc_info);
   1079 
   1080     /* 2. Disable the rx device */
   1081     disable_snd_device(adev, uc_info->out_snd_device);
   1082 
   1083     list_remove(&uc_info->list);
   1084     free(uc_info);
   1085 
   1086     audio_extn_extspk_update(adev->extspk);
   1087 
   1088     /* Must be called after removing the usecase from list */
   1089     if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
   1090         check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
   1091 
   1092     ALOGV("%s: exit: status(%d)", __func__, ret);
   1093     return ret;
   1094 }
   1095 
   1096 int start_output_stream(struct stream_out *out)
   1097 {
   1098     int ret = 0;
   1099     struct audio_usecase *uc_info;
   1100     struct audio_device *adev = out->dev;
   1101 
   1102     ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
   1103           __func__, out->usecase, use_case_table[out->usecase], out->devices);
   1104     out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
   1105     if (out->pcm_device_id < 0) {
   1106         ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
   1107               __func__, out->pcm_device_id, out->usecase);
   1108         ret = -EINVAL;
   1109         goto error_config;
   1110     }
   1111 
   1112     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
   1113     uc_info->id = out->usecase;
   1114     uc_info->type = PCM_PLAYBACK;
   1115     uc_info->stream.out = out;
   1116     uc_info->devices = out->devices;
   1117     uc_info->in_snd_device = SND_DEVICE_NONE;
   1118     uc_info->out_snd_device = SND_DEVICE_NONE;
   1119 
   1120     /* This must be called before adding this usecase to the list */
   1121     if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
   1122         check_and_set_hdmi_channels(adev, out->config.channels);
   1123 
   1124     list_add_tail(&adev->usecase_list, &uc_info->list);
   1125 
   1126     select_devices(adev, out->usecase);
   1127 
   1128     audio_extn_extspk_update(adev->extspk);
   1129 
   1130     ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
   1131           __func__, adev->snd_card, out->pcm_device_id, out->config.format);
   1132     if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1133         unsigned int flags = PCM_OUT;
   1134         unsigned int pcm_open_retry_count = 0;
   1135         if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
   1136             flags |= PCM_MMAP | PCM_NOIRQ;
   1137             pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
   1138         } else
   1139             flags |= PCM_MONOTONIC;
   1140 
   1141         while (1) {
   1142             out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
   1143                                flags, &out->config);
   1144             if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
   1145                 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
   1146                 if (out->pcm != NULL) {
   1147                     pcm_close(out->pcm);
   1148                     out->pcm = NULL;
   1149                 }
   1150                 if (pcm_open_retry_count-- == 0) {
   1151                     ret = -EIO;
   1152                     goto error_open;
   1153                 }
   1154                 usleep(PROXY_OPEN_WAIT_TIME * 1000);
   1155                 continue;
   1156             }
   1157             break;
   1158         }
   1159     } else {
   1160         out->pcm = NULL;
   1161         out->compr = compress_open(adev->snd_card, out->pcm_device_id,
   1162                                    COMPRESS_IN, &out->compr_config);
   1163         if (out->compr && !is_compress_ready(out->compr)) {
   1164             ALOGE("%s: %s", __func__, compress_get_error(out->compr));
   1165             compress_close(out->compr);
   1166             out->compr = NULL;
   1167             ret = -EIO;
   1168             goto error_open;
   1169         }
   1170         if (out->offload_callback)
   1171             compress_nonblock(out->compr, out->non_blocking);
   1172 
   1173         if (adev->visualizer_start_output != NULL)
   1174             adev->visualizer_start_output(out->handle, out->pcm_device_id);
   1175         if (adev->offload_effects_start_output != NULL)
   1176             adev->offload_effects_start_output(out->handle, out->pcm_device_id);
   1177     }
   1178     ALOGV("%s: exit", __func__);
   1179     return 0;
   1180 error_open:
   1181     stop_output_stream(out);
   1182 error_config:
   1183     return ret;
   1184 }
   1185 
   1186 static int check_input_parameters(uint32_t sample_rate,
   1187                                   audio_format_t format,
   1188                                   int channel_count)
   1189 {
   1190     if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
   1191 
   1192     if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
   1193 
   1194     switch (sample_rate) {
   1195     case 8000:
   1196     case 11025:
   1197     case 12000:
   1198     case 16000:
   1199     case 22050:
   1200     case 24000:
   1201     case 32000:
   1202     case 44100:
   1203     case 48000:
   1204         break;
   1205     default:
   1206         return -EINVAL;
   1207     }
   1208 
   1209     return 0;
   1210 }
   1211 
   1212 static size_t get_input_buffer_size(uint32_t sample_rate,
   1213                                     audio_format_t format,
   1214                                     int channel_count,
   1215                                     bool is_low_latency)
   1216 {
   1217     size_t size = 0;
   1218 
   1219     if (check_input_parameters(sample_rate, format, channel_count) != 0)
   1220         return 0;
   1221 
   1222     size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
   1223     if (is_low_latency)
   1224         size = configured_low_latency_capture_period_size;
   1225     /* ToDo: should use frame_size computed based on the format and
   1226        channel_count here. */
   1227     size *= sizeof(short) * channel_count;
   1228 
   1229     /* make sure the size is multiple of 32 bytes
   1230      * At 48 kHz mono 16-bit PCM:
   1231      *  5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
   1232      *  3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
   1233      */
   1234     size += 0x1f;
   1235     size &= ~0x1f;
   1236 
   1237     return size;
   1238 }
   1239 
   1240 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
   1241 {
   1242     struct stream_out *out = (struct stream_out *)stream;
   1243 
   1244     return out->sample_rate;
   1245 }
   1246 
   1247 static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
   1248 {
   1249     return -ENOSYS;
   1250 }
   1251 
   1252 static size_t out_get_buffer_size(const struct audio_stream *stream)
   1253 {
   1254     struct stream_out *out = (struct stream_out *)stream;
   1255 
   1256     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1257         return out->compr_config.fragment_size;
   1258     }
   1259     return out->config.period_size *
   1260                 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
   1261 }
   1262 
   1263 static uint32_t out_get_channels(const struct audio_stream *stream)
   1264 {
   1265     struct stream_out *out = (struct stream_out *)stream;
   1266 
   1267     return out->channel_mask;
   1268 }
   1269 
   1270 static audio_format_t out_get_format(const struct audio_stream *stream)
   1271 {
   1272     struct stream_out *out = (struct stream_out *)stream;
   1273 
   1274     return out->format;
   1275 }
   1276 
   1277 static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
   1278 {
   1279     return -ENOSYS;
   1280 }
   1281 
   1282 static int out_standby(struct audio_stream *stream)
   1283 {
   1284     struct stream_out *out = (struct stream_out *)stream;
   1285     struct audio_device *adev = out->dev;
   1286 
   1287     ALOGV("%s: enter: usecase(%d: %s)", __func__,
   1288           out->usecase, use_case_table[out->usecase]);
   1289 
   1290     pthread_mutex_lock(&out->lock);
   1291     if (!out->standby) {
   1292         pthread_mutex_lock(&adev->lock);
   1293         out->standby = true;
   1294         if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1295             if (out->pcm) {
   1296                 pcm_close(out->pcm);
   1297                 out->pcm = NULL;
   1298             }
   1299         } else {
   1300             stop_compressed_output_l(out);
   1301             out->gapless_mdata.encoder_delay = 0;
   1302             out->gapless_mdata.encoder_padding = 0;
   1303             if (out->compr != NULL) {
   1304                 compress_close(out->compr);
   1305                 out->compr = NULL;
   1306             }
   1307         }
   1308         stop_output_stream(out);
   1309         pthread_mutex_unlock(&adev->lock);
   1310     }
   1311     pthread_mutex_unlock(&out->lock);
   1312     ALOGV("%s: exit", __func__);
   1313     return 0;
   1314 }
   1315 
   1316 static int out_dump(const struct audio_stream *stream __unused, int fd __unused)
   1317 {
   1318     return 0;
   1319 }
   1320 
   1321 static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
   1322 {
   1323     int ret = 0;
   1324     char value[32];
   1325     struct compr_gapless_mdata tmp_mdata;
   1326 
   1327     if (!out || !parms) {
   1328         return -EINVAL;
   1329     }
   1330 
   1331     ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
   1332     if (ret >= 0) {
   1333         tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
   1334     } else {
   1335         return -EINVAL;
   1336     }
   1337 
   1338     ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
   1339     if (ret >= 0) {
   1340         tmp_mdata.encoder_padding = atoi(value);
   1341     } else {
   1342         return -EINVAL;
   1343     }
   1344 
   1345     out->gapless_mdata = tmp_mdata;
   1346     out->send_new_metadata = 1;
   1347     ALOGV("%s new encoder delay %u and padding %u", __func__,
   1348           out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
   1349 
   1350     return 0;
   1351 }
   1352 
   1353 static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
   1354 {
   1355     return out == adev->primary_output || out == adev->voice_tx_output;
   1356 }
   1357 
   1358 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
   1359 {
   1360     struct stream_out *out = (struct stream_out *)stream;
   1361     struct audio_device *adev = out->dev;
   1362     struct audio_usecase *usecase;
   1363     struct listnode *node;
   1364     struct str_parms *parms;
   1365     char value[32];
   1366     int ret, val = 0;
   1367     bool select_new_device = false;
   1368     int status = 0;
   1369 
   1370     ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
   1371           __func__, out->usecase, use_case_table[out->usecase], kvpairs);
   1372     parms = str_parms_create_str(kvpairs);
   1373     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
   1374     if (ret >= 0) {
   1375         val = atoi(value);
   1376         pthread_mutex_lock(&out->lock);
   1377         pthread_mutex_lock(&adev->lock);
   1378 
   1379         /*
   1380          * When HDMI cable is unplugged the music playback is paused and
   1381          * the policy manager sends routing=0. But the audioflinger
   1382          * continues to write data until standby time (3sec).
   1383          * As the HDMI core is turned off, the write gets blocked.
   1384          * Avoid this by routing audio to speaker until standby.
   1385          */
   1386         if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
   1387                 val == AUDIO_DEVICE_NONE) {
   1388             val = AUDIO_DEVICE_OUT_SPEAKER;
   1389         }
   1390 
   1391         /*
   1392          * select_devices() call below switches all the usecases on the same
   1393          * backend to the new device. Refer to check_and_route_playback_usecases() in
   1394          * the select_devices(). But how do we undo this?
   1395          *
   1396          * For example, music playback is active on headset (deep-buffer usecase)
   1397          * and if we go to ringtones and select a ringtone, low-latency usecase
   1398          * will be started on headset+speaker. As we can't enable headset+speaker
   1399          * and headset devices at the same time, select_devices() switches the music
   1400          * playback to headset+speaker while starting low-lateny usecase for ringtone.
   1401          * So when the ringtone playback is completed, how do we undo the same?
   1402          *
   1403          * We are relying on the out_set_parameters() call on deep-buffer output,
   1404          * once the ringtone playback is ended.
   1405          * NOTE: We should not check if the current devices are same as new devices.
   1406          *       Because select_devices() must be called to switch back the music
   1407          *       playback to headset.
   1408          */
   1409         if (val != 0) {
   1410             out->devices = val;
   1411 
   1412             if (!out->standby)
   1413                 select_devices(adev, out->usecase);
   1414 
   1415             if (output_drives_call(adev, out)) {
   1416                 if (!voice_is_in_call(adev)) {
   1417                     if (adev->mode == AUDIO_MODE_IN_CALL) {
   1418                         adev->current_call_output = out;
   1419                         ret = voice_start_call(adev);
   1420                     }
   1421                 } else {
   1422                     adev->current_call_output = out;
   1423                     voice_update_devices_for_all_voice_usecases(adev);
   1424                 }
   1425             }
   1426         }
   1427 
   1428         pthread_mutex_unlock(&adev->lock);
   1429         pthread_mutex_unlock(&out->lock);
   1430 
   1431         /*handles device and call state changes*/
   1432         audio_extn_extspk_update(adev->extspk);
   1433     }
   1434 
   1435     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1436         parse_compress_metadata(out, parms);
   1437     }
   1438 
   1439     str_parms_destroy(parms);
   1440     ALOGV("%s: exit: code(%d)", __func__, status);
   1441     return status;
   1442 }
   1443 
   1444 static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
   1445 {
   1446     struct stream_out *out = (struct stream_out *)stream;
   1447     struct str_parms *query = str_parms_create_str(keys);
   1448     char *str;
   1449     char value[256];
   1450     struct str_parms *reply = str_parms_create();
   1451     size_t i, j;
   1452     int ret;
   1453     bool first = true;
   1454     ALOGV("%s: enter: keys - %s", __func__, keys);
   1455     ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
   1456     if (ret >= 0) {
   1457         value[0] = '\0';
   1458         i = 0;
   1459         while (out->supported_channel_masks[i] != 0) {
   1460             for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
   1461                 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
   1462                     if (!first) {
   1463                         strcat(value, "|");
   1464                     }
   1465                     strcat(value, out_channels_name_to_enum_table[j].name);
   1466                     first = false;
   1467                     break;
   1468                 }
   1469             }
   1470             i++;
   1471         }
   1472         str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
   1473         str = str_parms_to_str(reply);
   1474     } else {
   1475         str = strdup(keys);
   1476     }
   1477     str_parms_destroy(query);
   1478     str_parms_destroy(reply);
   1479     ALOGV("%s: exit: returns - %s", __func__, str);
   1480     return str;
   1481 }
   1482 
   1483 static uint32_t out_get_latency(const struct audio_stream_out *stream)
   1484 {
   1485     struct stream_out *out = (struct stream_out *)stream;
   1486 
   1487     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
   1488         return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
   1489 
   1490     return (out->config.period_count * out->config.period_size * 1000) /
   1491            (out->config.rate);
   1492 }
   1493 
   1494 static int out_set_volume(struct audio_stream_out *stream, float left,
   1495                           float right)
   1496 {
   1497     struct stream_out *out = (struct stream_out *)stream;
   1498     int volume[2];
   1499 
   1500     if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
   1501         /* only take left channel into account: the API is for stereo anyway */
   1502         out->muted = (left == 0.0f);
   1503         return 0;
   1504     } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1505         const char *mixer_ctl_name = "Compress Playback Volume";
   1506         struct audio_device *adev = out->dev;
   1507         struct mixer_ctl *ctl;
   1508         ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
   1509         if (!ctl) {
   1510             /* try with the control based on device id */
   1511             int pcm_device_id = platform_get_pcm_device_id(out->usecase,
   1512                                                        PCM_PLAYBACK);
   1513             char ctl_name[128] = {0};
   1514             snprintf(ctl_name, sizeof(ctl_name),
   1515                      "Compress Playback %d Volume", pcm_device_id);
   1516             ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name);
   1517             if (!ctl) {
   1518                 ALOGE("%s: Could not get volume ctl mixer cmd", __func__);
   1519                 return -EINVAL;
   1520             }
   1521         }
   1522         volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
   1523         volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
   1524         mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
   1525         return 0;
   1526     }
   1527 
   1528     return -ENOSYS;
   1529 }
   1530 
   1531 #ifdef NO_AUDIO_OUT
   1532 static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
   1533                                        const void *buffer, size_t bytes)
   1534 {
   1535     struct stream_out *out = (struct stream_out *)stream;
   1536 
   1537     /* No Output device supported other than BT for playback.
   1538      * Sleep for the amount of buffer duration
   1539      */
   1540     pthread_mutex_lock(&out->lock);
   1541     usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
   1542             out_get_sample_rate(&out->stream.common));
   1543     pthread_mutex_unlock(&out->lock);
   1544     return bytes;
   1545 }
   1546 #endif
   1547 
   1548 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
   1549                          size_t bytes)
   1550 {
   1551     struct stream_out *out = (struct stream_out *)stream;
   1552     struct audio_device *adev = out->dev;
   1553     ssize_t ret = 0;
   1554 
   1555     pthread_mutex_lock(&out->lock);
   1556     if (out->standby) {
   1557         out->standby = false;
   1558         pthread_mutex_lock(&adev->lock);
   1559         ret = start_output_stream(out);
   1560         pthread_mutex_unlock(&adev->lock);
   1561         /* ToDo: If use case is compress offload should return 0 */
   1562         if (ret != 0) {
   1563             out->standby = true;
   1564             goto exit;
   1565         }
   1566     }
   1567 
   1568     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1569         ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
   1570         if (out->send_new_metadata) {
   1571             ALOGVV("send new gapless metadata");
   1572             compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
   1573             out->send_new_metadata = 0;
   1574         }
   1575 
   1576         ret = compress_write(out->compr, buffer, bytes);
   1577         ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
   1578         if (ret >= 0 && ret < (ssize_t)bytes) {
   1579             send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
   1580         }
   1581         if (!out->playback_started) {
   1582             compress_start(out->compr);
   1583             out->playback_started = 1;
   1584             out->offload_state = OFFLOAD_STATE_PLAYING;
   1585         }
   1586         pthread_mutex_unlock(&out->lock);
   1587         return ret;
   1588     } else {
   1589         if (out->pcm) {
   1590             if (out->muted)
   1591                 memset((void *)buffer, 0, bytes);
   1592             ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
   1593             if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
   1594                 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
   1595             }
   1596             else
   1597                 ret = pcm_write(out->pcm, (void *)buffer, bytes);
   1598             if (ret == 0)
   1599                 out->written += bytes / (out->config.channels * sizeof(short));
   1600         }
   1601     }
   1602 
   1603 exit:
   1604     pthread_mutex_unlock(&out->lock);
   1605 
   1606     if (ret != 0) {
   1607         if (out->pcm)
   1608             ALOGE("%s: error %zu - %s", __func__, ret, pcm_get_error(out->pcm));
   1609         out_standby(&out->stream.common);
   1610         usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
   1611                out_get_sample_rate(&out->stream.common));
   1612     }
   1613     return bytes;
   1614 }
   1615 
   1616 static int out_get_render_position(const struct audio_stream_out *stream,
   1617                                    uint32_t *dsp_frames)
   1618 {
   1619     struct stream_out *out = (struct stream_out *)stream;
   1620     *dsp_frames = 0;
   1621     if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
   1622         pthread_mutex_lock(&out->lock);
   1623         if (out->compr != NULL) {
   1624             compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
   1625                     &out->sample_rate);
   1626             ALOGVV("%s rendered frames %d sample_rate %d",
   1627                    __func__, *dsp_frames, out->sample_rate);
   1628         }
   1629         pthread_mutex_unlock(&out->lock);
   1630         return 0;
   1631     } else
   1632         return -EINVAL;
   1633 }
   1634 
   1635 static int out_add_audio_effect(const struct audio_stream *stream __unused,
   1636                                 effect_handle_t effect __unused)
   1637 {
   1638     return 0;
   1639 }
   1640 
   1641 static int out_remove_audio_effect(const struct audio_stream *stream __unused,
   1642                                    effect_handle_t effect __unused)
   1643 {
   1644     return 0;
   1645 }
   1646 
   1647 static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
   1648                                         int64_t *timestamp __unused)
   1649 {
   1650     return -EINVAL;
   1651 }
   1652 
   1653 static int out_get_presentation_position(const struct audio_stream_out *stream,
   1654                                    uint64_t *frames, struct timespec *timestamp)
   1655 {
   1656     struct stream_out *out = (struct stream_out *)stream;
   1657     int ret = -1;
   1658     unsigned long dsp_frames;
   1659 
   1660     pthread_mutex_lock(&out->lock);
   1661 
   1662     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1663         if (out->compr != NULL) {
   1664             compress_get_tstamp(out->compr, &dsp_frames,
   1665                     &out->sample_rate);
   1666             ALOGVV("%s rendered frames %ld sample_rate %d",
   1667                    __func__, dsp_frames, out->sample_rate);
   1668             *frames = dsp_frames;
   1669             ret = 0;
   1670             /* this is the best we can do */
   1671             clock_gettime(CLOCK_MONOTONIC, timestamp);
   1672         }
   1673     } else {
   1674         if (out->pcm) {
   1675             unsigned int avail;
   1676             if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
   1677                 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
   1678                 int64_t signed_frames = out->written - kernel_buffer_size + avail;
   1679                 // This adjustment accounts for buffering after app processor.
   1680                 // It is based on estimated DSP latency per use case, rather than exact.
   1681                 signed_frames -=
   1682                     (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
   1683 
   1684                 // It would be unusual for this value to be negative, but check just in case ...
   1685                 if (signed_frames >= 0) {
   1686                     *frames = signed_frames;
   1687                     ret = 0;
   1688                 }
   1689             }
   1690         }
   1691     }
   1692 
   1693     pthread_mutex_unlock(&out->lock);
   1694 
   1695     return ret;
   1696 }
   1697 
   1698 static int out_set_callback(struct audio_stream_out *stream,
   1699             stream_callback_t callback, void *cookie)
   1700 {
   1701     struct stream_out *out = (struct stream_out *)stream;
   1702 
   1703     ALOGV("%s", __func__);
   1704     pthread_mutex_lock(&out->lock);
   1705     out->offload_callback = callback;
   1706     out->offload_cookie = cookie;
   1707     pthread_mutex_unlock(&out->lock);
   1708     return 0;
   1709 }
   1710 
   1711 static int out_pause(struct audio_stream_out* stream)
   1712 {
   1713     struct stream_out *out = (struct stream_out *)stream;
   1714     int status = -ENOSYS;
   1715     ALOGV("%s", __func__);
   1716     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1717         pthread_mutex_lock(&out->lock);
   1718         if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
   1719             status = compress_pause(out->compr);
   1720             out->offload_state = OFFLOAD_STATE_PAUSED;
   1721         }
   1722         pthread_mutex_unlock(&out->lock);
   1723     }
   1724     return status;
   1725 }
   1726 
   1727 static int out_resume(struct audio_stream_out* stream)
   1728 {
   1729     struct stream_out *out = (struct stream_out *)stream;
   1730     int status = -ENOSYS;
   1731     ALOGV("%s", __func__);
   1732     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1733         status = 0;
   1734         pthread_mutex_lock(&out->lock);
   1735         if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
   1736             status = compress_resume(out->compr);
   1737             out->offload_state = OFFLOAD_STATE_PLAYING;
   1738         }
   1739         pthread_mutex_unlock(&out->lock);
   1740     }
   1741     return status;
   1742 }
   1743 
   1744 static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
   1745 {
   1746     struct stream_out *out = (struct stream_out *)stream;
   1747     int status = -ENOSYS;
   1748     ALOGV("%s", __func__);
   1749     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1750         pthread_mutex_lock(&out->lock);
   1751         if (type == AUDIO_DRAIN_EARLY_NOTIFY)
   1752             status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
   1753         else
   1754             status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
   1755         pthread_mutex_unlock(&out->lock);
   1756     }
   1757     return status;
   1758 }
   1759 
   1760 static int out_flush(struct audio_stream_out* stream)
   1761 {
   1762     struct stream_out *out = (struct stream_out *)stream;
   1763     ALOGV("%s", __func__);
   1764     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   1765         pthread_mutex_lock(&out->lock);
   1766         stop_compressed_output_l(out);
   1767         pthread_mutex_unlock(&out->lock);
   1768         return 0;
   1769     }
   1770     return -ENOSYS;
   1771 }
   1772 
   1773 /** audio_stream_in implementation **/
   1774 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
   1775 {
   1776     struct stream_in *in = (struct stream_in *)stream;
   1777 
   1778     return in->config.rate;
   1779 }
   1780 
   1781 static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
   1782 {
   1783     return -ENOSYS;
   1784 }
   1785 
   1786 static size_t in_get_buffer_size(const struct audio_stream *stream)
   1787 {
   1788     struct stream_in *in = (struct stream_in *)stream;
   1789 
   1790     return in->config.period_size *
   1791                 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
   1792 }
   1793 
   1794 static uint32_t in_get_channels(const struct audio_stream *stream)
   1795 {
   1796     struct stream_in *in = (struct stream_in *)stream;
   1797 
   1798     return in->channel_mask;
   1799 }
   1800 
   1801 static audio_format_t in_get_format(const struct audio_stream *stream __unused)
   1802 {
   1803     return AUDIO_FORMAT_PCM_16_BIT;
   1804 }
   1805 
   1806 static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
   1807 {
   1808     return -ENOSYS;
   1809 }
   1810 
   1811 static int in_standby(struct audio_stream *stream)
   1812 {
   1813     struct stream_in *in = (struct stream_in *)stream;
   1814     struct audio_device *adev = in->dev;
   1815     int status = 0;
   1816     ALOGV("%s: enter", __func__);
   1817     pthread_mutex_lock(&in->lock);
   1818 
   1819     if (!in->standby && in->is_st_session) {
   1820         ALOGD("%s: sound trigger pcm stop lab", __func__);
   1821         audio_extn_sound_trigger_stop_lab(in);
   1822         in->standby = true;
   1823     }
   1824 
   1825     if (!in->standby) {
   1826         pthread_mutex_lock(&adev->lock);
   1827         in->standby = true;
   1828         if (in->pcm) {
   1829             pcm_close(in->pcm);
   1830             in->pcm = NULL;
   1831         }
   1832         adev->enable_voicerx = false;
   1833         platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE );
   1834         status = stop_input_stream(in);
   1835         pthread_mutex_unlock(&adev->lock);
   1836     }
   1837     pthread_mutex_unlock(&in->lock);
   1838     ALOGV("%s: exit:  status(%d)", __func__, status);
   1839     return status;
   1840 }
   1841 
   1842 static int in_dump(const struct audio_stream *stream __unused, int fd __unused)
   1843 {
   1844     return 0;
   1845 }
   1846 
   1847 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
   1848 {
   1849     struct stream_in *in = (struct stream_in *)stream;
   1850     struct audio_device *adev = in->dev;
   1851     struct str_parms *parms;
   1852     char *str;
   1853     char value[32];
   1854     int ret, val = 0;
   1855     int status = 0;
   1856 
   1857     ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
   1858     parms = str_parms_create_str(kvpairs);
   1859 
   1860     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
   1861 
   1862     pthread_mutex_lock(&in->lock);
   1863     pthread_mutex_lock(&adev->lock);
   1864     if (ret >= 0) {
   1865         val = atoi(value);
   1866         /* no audio source uses val == 0 */
   1867         if ((in->source != val) && (val != 0)) {
   1868             in->source = val;
   1869         }
   1870     }
   1871 
   1872     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
   1873 
   1874     if (ret >= 0) {
   1875         val = atoi(value);
   1876         if (((int)in->device != val) && (val != 0)) {
   1877             in->device = val;
   1878             /* If recording is in progress, change the tx device to new device */
   1879             if (!in->standby)
   1880                 status = select_devices(adev, in->usecase);
   1881         }
   1882     }
   1883 
   1884     pthread_mutex_unlock(&adev->lock);
   1885     pthread_mutex_unlock(&in->lock);
   1886 
   1887     str_parms_destroy(parms);
   1888     ALOGV("%s: exit: status(%d)", __func__, status);
   1889     return status;
   1890 }
   1891 
   1892 static char* in_get_parameters(const struct audio_stream *stream __unused,
   1893                                const char *keys __unused)
   1894 {
   1895     return strdup("");
   1896 }
   1897 
   1898 static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused)
   1899 {
   1900     return 0;
   1901 }
   1902 
   1903 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
   1904                        size_t bytes)
   1905 {
   1906     struct stream_in *in = (struct stream_in *)stream;
   1907     struct audio_device *adev = in->dev;
   1908     int i, ret = -1;
   1909 
   1910     pthread_mutex_lock(&in->lock);
   1911     if (in->is_st_session) {
   1912         ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
   1913         /* Read from sound trigger HAL */
   1914         audio_extn_sound_trigger_read(in, buffer, bytes);
   1915         pthread_mutex_unlock(&in->lock);
   1916         return bytes;
   1917     }
   1918 
   1919     if (in->standby) {
   1920         pthread_mutex_lock(&adev->lock);
   1921         ret = start_input_stream(in);
   1922         pthread_mutex_unlock(&adev->lock);
   1923         if (ret != 0) {
   1924             goto exit;
   1925         }
   1926         in->standby = 0;
   1927     }
   1928 
   1929     if (in->pcm) {
   1930         if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
   1931             ret = pcm_mmap_read(in->pcm, buffer, bytes);
   1932         } else
   1933             ret = pcm_read(in->pcm, buffer, bytes);
   1934     }
   1935 
   1936     /*
   1937      * Instead of writing zeroes here, we could trust the hardware
   1938      * to always provide zeroes when muted.
   1939      * No need to acquire adev->lock to read mic_muted here as we don't change its state.
   1940      */
   1941     if (ret == 0 && adev->mic_muted && in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
   1942         memset(buffer, 0, bytes);
   1943 
   1944 exit:
   1945     pthread_mutex_unlock(&in->lock);
   1946 
   1947     if (ret != 0) {
   1948         in_standby(&in->stream.common);
   1949         ALOGV("%s: read failed - sleeping for buffer duration", __func__);
   1950         usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
   1951                in_get_sample_rate(&in->stream.common));
   1952     }
   1953     return bytes;
   1954 }
   1955 
   1956 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
   1957 {
   1958     return 0;
   1959 }
   1960 
   1961 static int add_remove_audio_effect(const struct audio_stream *stream,
   1962                                    effect_handle_t effect,
   1963                                    bool enable)
   1964 {
   1965     struct stream_in *in = (struct stream_in *)stream;
   1966     struct audio_device *adev = in->dev;
   1967     int status = 0;
   1968     effect_descriptor_t desc;
   1969 
   1970     status = (*effect)->get_descriptor(effect, &desc);
   1971     if (status != 0)
   1972         return status;
   1973 
   1974     pthread_mutex_lock(&in->lock);
   1975     pthread_mutex_lock(&in->dev->lock);
   1976     if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
   1977             in->enable_aec != enable &&
   1978             (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
   1979         in->enable_aec = enable;
   1980         if (!enable)
   1981             platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
   1982         adev->enable_voicerx = enable;
   1983         struct audio_usecase *usecase;
   1984         struct listnode *node;
   1985         list_for_each(node, &adev->usecase_list) {
   1986             usecase = node_to_item(node, struct audio_usecase, list);
   1987             if (usecase->type == PCM_PLAYBACK) {
   1988                 select_devices(adev, usecase->id);
   1989             break;
   1990             }
   1991         }
   1992         if (!in->standby)
   1993             select_devices(in->dev, in->usecase);
   1994     }
   1995     if (in->enable_ns != enable &&
   1996             (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
   1997         in->enable_ns = enable;
   1998         if (!in->standby)
   1999             select_devices(in->dev, in->usecase);
   2000     }
   2001     pthread_mutex_unlock(&in->dev->lock);
   2002     pthread_mutex_unlock(&in->lock);
   2003 
   2004     return 0;
   2005 }
   2006 
   2007 static int in_add_audio_effect(const struct audio_stream *stream,
   2008                                effect_handle_t effect)
   2009 {
   2010     ALOGV("%s: effect %p", __func__, effect);
   2011     return add_remove_audio_effect(stream, effect, true);
   2012 }
   2013 
   2014 static int in_remove_audio_effect(const struct audio_stream *stream,
   2015                                   effect_handle_t effect)
   2016 {
   2017     ALOGV("%s: effect %p", __func__, effect);
   2018     return add_remove_audio_effect(stream, effect, false);
   2019 }
   2020 
   2021 static int adev_open_output_stream(struct audio_hw_device *dev,
   2022                                    audio_io_handle_t handle,
   2023                                    audio_devices_t devices,
   2024                                    audio_output_flags_t flags,
   2025                                    struct audio_config *config,
   2026                                    struct audio_stream_out **stream_out,
   2027                                    const char *address __unused)
   2028 {
   2029     struct audio_device *adev = (struct audio_device *)dev;
   2030     struct stream_out *out;
   2031     int i, ret;
   2032 
   2033     ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
   2034           __func__, config->sample_rate, config->channel_mask, devices, flags);
   2035     *stream_out = NULL;
   2036     out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
   2037 
   2038     if (devices == AUDIO_DEVICE_NONE)
   2039         devices = AUDIO_DEVICE_OUT_SPEAKER;
   2040 
   2041     out->flags = flags;
   2042     out->devices = devices;
   2043     out->dev = adev;
   2044     out->format = config->format;
   2045     out->sample_rate = config->sample_rate;
   2046     out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
   2047     out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
   2048     out->handle = handle;
   2049 
   2050     /* Init use case and pcm_config */
   2051     if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
   2052             !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
   2053         out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
   2054         pthread_mutex_lock(&adev->lock);
   2055         ret = read_hdmi_channel_masks(out);
   2056         pthread_mutex_unlock(&adev->lock);
   2057         if (ret != 0)
   2058             goto error_open;
   2059 
   2060         if (config->sample_rate == 0)
   2061             config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
   2062         if (config->channel_mask == 0)
   2063             config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
   2064 
   2065         out->channel_mask = config->channel_mask;
   2066         out->sample_rate = config->sample_rate;
   2067         out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
   2068         out->config = pcm_config_hdmi_multi;
   2069         out->config.rate = config->sample_rate;
   2070         out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
   2071         out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
   2072     } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
   2073         if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
   2074             config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
   2075             ALOGE("%s: Unsupported Offload information", __func__);
   2076             ret = -EINVAL;
   2077             goto error_open;
   2078         }
   2079         if (!is_supported_format(config->offload_info.format)) {
   2080             ALOGE("%s: Unsupported audio format", __func__);
   2081             ret = -EINVAL;
   2082             goto error_open;
   2083         }
   2084 
   2085         out->compr_config.codec = (struct snd_codec *)
   2086                                     calloc(1, sizeof(struct snd_codec));
   2087 
   2088         out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
   2089         if (config->offload_info.channel_mask)
   2090             out->channel_mask = config->offload_info.channel_mask;
   2091         else if (config->channel_mask)
   2092             out->channel_mask = config->channel_mask;
   2093         out->format = config->offload_info.format;
   2094         out->sample_rate = config->offload_info.sample_rate;
   2095 
   2096         out->stream.set_callback = out_set_callback;
   2097         out->stream.pause = out_pause;
   2098         out->stream.resume = out_resume;
   2099         out->stream.drain = out_drain;
   2100         out->stream.flush = out_flush;
   2101 
   2102         out->compr_config.codec->id =
   2103                 get_snd_codec_id(config->offload_info.format);
   2104         out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
   2105         out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
   2106         out->compr_config.codec->sample_rate = config->offload_info.sample_rate;
   2107         out->compr_config.codec->bit_rate =
   2108                     config->offload_info.bit_rate;
   2109         out->compr_config.codec->ch_in =
   2110                 audio_channel_count_from_out_mask(config->channel_mask);
   2111         out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
   2112 
   2113         if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
   2114             out->non_blocking = 1;
   2115 
   2116         out->send_new_metadata = 1;
   2117         create_offload_callback_thread(out);
   2118         ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
   2119                 __func__, config->offload_info.version,
   2120                 config->offload_info.bit_rate);
   2121     } else  if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
   2122         if (config->sample_rate == 0)
   2123             config->sample_rate = AFE_PROXY_SAMPLING_RATE;
   2124         if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
   2125                 config->sample_rate != 8000) {
   2126             config->sample_rate = AFE_PROXY_SAMPLING_RATE;
   2127             ret = -EINVAL;
   2128             goto error_open;
   2129         }
   2130         out->sample_rate = config->sample_rate;
   2131         out->config.rate = config->sample_rate;
   2132         if (config->format == AUDIO_FORMAT_DEFAULT)
   2133             config->format = AUDIO_FORMAT_PCM_16_BIT;
   2134         if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
   2135             config->format = AUDIO_FORMAT_PCM_16_BIT;
   2136             ret = -EINVAL;
   2137             goto error_open;
   2138         }
   2139         out->format = config->format;
   2140         out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
   2141         out->config = pcm_config_afe_proxy_playback;
   2142         adev->voice_tx_output = out;
   2143     } else {
   2144         if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
   2145             out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
   2146             out->config = pcm_config_deep_buffer;
   2147         } else if (out->flags & AUDIO_OUTPUT_FLAG_TTS) {
   2148             out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
   2149             out->config = pcm_config_deep_buffer;
   2150         } else {
   2151             out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
   2152             out->config = pcm_config_low_latency;
   2153         }
   2154         if (config->format != audio_format_from_pcm_format(out->config.format)) {
   2155             if (k_enable_extended_precision
   2156                     && pcm_params_format_test(adev->use_case_table[out->usecase],
   2157                             pcm_format_from_audio_format(config->format))) {
   2158                 out->config.format = pcm_format_from_audio_format(config->format);
   2159                 /* out->format already set to config->format */
   2160             } else {
   2161                 /* deny the externally proposed config format
   2162                  * and use the one specified in audio_hw layer configuration.
   2163                  * Note: out->format is returned by out->stream.common.get_format()
   2164                  * and is used to set config->format in the code several lines below.
   2165                  */
   2166                 out->format = audio_format_from_pcm_format(out->config.format);
   2167             }
   2168         }
   2169         out->sample_rate = out->config.rate;
   2170     }
   2171     ALOGV("%s: Usecase(%s) config->format %#x  out->config.format %#x\n",
   2172             __func__, use_case_table[out->usecase], config->format, out->config.format);
   2173 
   2174     if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
   2175         if (adev->primary_output == NULL)
   2176             adev->primary_output = out;
   2177         else {
   2178             ALOGE("%s: Primary output is already opened", __func__);
   2179             ret = -EEXIST;
   2180             goto error_open;
   2181         }
   2182     }
   2183 
   2184     /* Check if this usecase is already existing */
   2185     pthread_mutex_lock(&adev->lock);
   2186     if (get_usecase_from_list(adev, out->usecase) != NULL) {
   2187         ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
   2188         pthread_mutex_unlock(&adev->lock);
   2189         ret = -EEXIST;
   2190         goto error_open;
   2191     }
   2192     pthread_mutex_unlock(&adev->lock);
   2193 
   2194     out->stream.common.get_sample_rate = out_get_sample_rate;
   2195     out->stream.common.set_sample_rate = out_set_sample_rate;
   2196     out->stream.common.get_buffer_size = out_get_buffer_size;
   2197     out->stream.common.get_channels = out_get_channels;
   2198     out->stream.common.get_format = out_get_format;
   2199     out->stream.common.set_format = out_set_format;
   2200     out->stream.common.standby = out_standby;
   2201     out->stream.common.dump = out_dump;
   2202     out->stream.common.set_parameters = out_set_parameters;
   2203     out->stream.common.get_parameters = out_get_parameters;
   2204     out->stream.common.add_audio_effect = out_add_audio_effect;
   2205     out->stream.common.remove_audio_effect = out_remove_audio_effect;
   2206     out->stream.get_latency = out_get_latency;
   2207     out->stream.set_volume = out_set_volume;
   2208 #ifdef NO_AUDIO_OUT
   2209     out->stream.write = out_write_for_no_output;
   2210 #else
   2211     out->stream.write = out_write;
   2212 #endif
   2213     out->stream.get_render_position = out_get_render_position;
   2214     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
   2215     out->stream.get_presentation_position = out_get_presentation_position;
   2216 
   2217     out->standby = 1;
   2218     /* out->muted = false; by calloc() */
   2219     /* out->written = 0; by calloc() */
   2220 
   2221     pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
   2222     pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
   2223 
   2224     config->format = out->stream.common.get_format(&out->stream.common);
   2225     config->channel_mask = out->stream.common.get_channels(&out->stream.common);
   2226     config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
   2227 
   2228     *stream_out = &out->stream;
   2229     ALOGV("%s: exit", __func__);
   2230     return 0;
   2231 
   2232 error_open:
   2233     free(out);
   2234     *stream_out = NULL;
   2235     ALOGD("%s: exit: ret %d", __func__, ret);
   2236     return ret;
   2237 }
   2238 
   2239 static void adev_close_output_stream(struct audio_hw_device *dev __unused,
   2240                                      struct audio_stream_out *stream)
   2241 {
   2242     struct stream_out *out = (struct stream_out *)stream;
   2243     struct audio_device *adev = out->dev;
   2244 
   2245     ALOGV("%s: enter", __func__);
   2246     out_standby(&stream->common);
   2247     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
   2248         destroy_offload_callback_thread(out);
   2249 
   2250         if (out->compr_config.codec != NULL)
   2251             free(out->compr_config.codec);
   2252     }
   2253 
   2254     if (adev->voice_tx_output == out)
   2255         adev->voice_tx_output = NULL;
   2256 
   2257     pthread_cond_destroy(&out->cond);
   2258     pthread_mutex_destroy(&out->lock);
   2259     free(stream);
   2260     ALOGV("%s: exit", __func__);
   2261 }
   2262 
   2263 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
   2264 {
   2265     struct audio_device *adev = (struct audio_device *)dev;
   2266     struct str_parms *parms;
   2267     char *str;
   2268     char value[32];
   2269     int val;
   2270     int ret;
   2271     int status = 0;
   2272 
   2273     ALOGD("%s: enter: %s", __func__, kvpairs);
   2274 
   2275     pthread_mutex_lock(&adev->lock);
   2276 
   2277     parms = str_parms_create_str(kvpairs);
   2278     status = voice_set_parameters(adev, parms);
   2279     if (status != 0) {
   2280         goto done;
   2281     }
   2282 
   2283     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
   2284     if (ret >= 0) {
   2285         /* When set to false, HAL should disable EC and NS */
   2286         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
   2287             adev->bluetooth_nrec = true;
   2288         else
   2289             adev->bluetooth_nrec = false;
   2290     }
   2291 
   2292     ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
   2293     if (ret >= 0) {
   2294         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
   2295             adev->screen_off = false;
   2296         else
   2297             adev->screen_off = true;
   2298     }
   2299 
   2300     ret = str_parms_get_int(parms, "rotation", &val);
   2301     if (ret >= 0) {
   2302         bool reverse_speakers = false;
   2303         switch(val) {
   2304         // FIXME: note that the code below assumes that the speakers are in the correct placement
   2305         //   relative to the user when the device is rotated 90deg from its default rotation. This
   2306         //   assumption is device-specific, not platform-specific like this code.
   2307         case 270:
   2308             reverse_speakers = true;
   2309             break;
   2310         case 0:
   2311         case 90:
   2312         case 180:
   2313             break;
   2314         default:
   2315             ALOGE("%s: unexpected rotation of %d", __func__, val);
   2316             status = -EINVAL;
   2317         }
   2318         if (status == 0) {
   2319             platform_swap_lr_channels(adev, reverse_speakers);
   2320         }
   2321     }
   2322 
   2323     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
   2324     if (ret >= 0) {
   2325         adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
   2326     }
   2327 
   2328     audio_extn_hfp_set_parameters(adev, parms);
   2329 done:
   2330     str_parms_destroy(parms);
   2331     pthread_mutex_unlock(&adev->lock);
   2332     ALOGV("%s: exit with code(%d)", __func__, status);
   2333     return status;
   2334 }
   2335 
   2336 static char* adev_get_parameters(const struct audio_hw_device *dev,
   2337                                  const char *keys)
   2338 {
   2339     struct audio_device *adev = (struct audio_device *)dev;
   2340     struct str_parms *reply = str_parms_create();
   2341     struct str_parms *query = str_parms_create_str(keys);
   2342     char *str;
   2343 
   2344     pthread_mutex_lock(&adev->lock);
   2345 
   2346     voice_get_parameters(adev, query, reply);
   2347     str = str_parms_to_str(reply);
   2348     str_parms_destroy(query);
   2349     str_parms_destroy(reply);
   2350 
   2351     pthread_mutex_unlock(&adev->lock);
   2352     ALOGV("%s: exit: returns - %s", __func__, str);
   2353     return str;
   2354 }
   2355 
   2356 static int adev_init_check(const struct audio_hw_device *dev __unused)
   2357 {
   2358     return 0;
   2359 }
   2360 
   2361 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
   2362 {
   2363     int ret;
   2364     struct audio_device *adev = (struct audio_device *)dev;
   2365 
   2366     audio_extn_extspk_set_voice_vol(adev->extspk, volume);
   2367 
   2368     pthread_mutex_lock(&adev->lock);
   2369     ret = voice_set_volume(adev, volume);
   2370     pthread_mutex_unlock(&adev->lock);
   2371 
   2372     return ret;
   2373 }
   2374 
   2375 static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
   2376 {
   2377     return -ENOSYS;
   2378 }
   2379 
   2380 static int adev_get_master_volume(struct audio_hw_device *dev __unused,
   2381                                   float *volume __unused)
   2382 {
   2383     return -ENOSYS;
   2384 }
   2385 
   2386 static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused)
   2387 {
   2388     return -ENOSYS;
   2389 }
   2390 
   2391 static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
   2392 {
   2393     return -ENOSYS;
   2394 }
   2395 
   2396 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
   2397 {
   2398     struct audio_device *adev = (struct audio_device *)dev;
   2399 
   2400     pthread_mutex_lock(&adev->lock);
   2401     if (adev->mode != mode) {
   2402         ALOGD("%s: mode %d\n", __func__, mode);
   2403         adev->mode = mode;
   2404         if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) &&
   2405                 voice_is_in_call(adev)) {
   2406             voice_stop_call(adev);
   2407             adev->current_call_output = NULL;
   2408         }
   2409     }
   2410     pthread_mutex_unlock(&adev->lock);
   2411 
   2412     audio_extn_extspk_set_mode(adev->extspk, mode);
   2413 
   2414     return 0;
   2415 }
   2416 
   2417 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
   2418 {
   2419     int ret;
   2420     struct audio_device *adev = (struct audio_device *)dev;
   2421 
   2422     ALOGD("%s: state %d\n", __func__, state);
   2423     pthread_mutex_lock(&adev->lock);
   2424     ret = voice_set_mic_mute(adev, state);
   2425     adev->mic_muted = state;
   2426     pthread_mutex_unlock(&adev->lock);
   2427 
   2428     return ret;
   2429 }
   2430 
   2431 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
   2432 {
   2433     *state = voice_get_mic_mute((struct audio_device *)dev);
   2434     return 0;
   2435 }
   2436 
   2437 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
   2438                                          const struct audio_config *config)
   2439 {
   2440     int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
   2441 
   2442     return get_input_buffer_size(config->sample_rate, config->format, channel_count,
   2443             false /* is_low_latency: since we don't know, be conservative */);
   2444 }
   2445 
   2446 static int adev_open_input_stream(struct audio_hw_device *dev,
   2447                                   audio_io_handle_t handle,
   2448                                   audio_devices_t devices,
   2449                                   struct audio_config *config,
   2450                                   struct audio_stream_in **stream_in,
   2451                                   audio_input_flags_t flags,
   2452                                   const char *address __unused,
   2453                                   audio_source_t source )
   2454 {
   2455     struct audio_device *adev = (struct audio_device *)dev;
   2456     struct stream_in *in;
   2457     int ret = 0, buffer_size, frame_size;
   2458     int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
   2459     bool is_low_latency = false;
   2460 
   2461     ALOGV("%s: enter", __func__);
   2462     *stream_in = NULL;
   2463     if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
   2464         return -EINVAL;
   2465 
   2466     in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
   2467 
   2468     pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
   2469 
   2470     in->stream.common.get_sample_rate = in_get_sample_rate;
   2471     in->stream.common.set_sample_rate = in_set_sample_rate;
   2472     in->stream.common.get_buffer_size = in_get_buffer_size;
   2473     in->stream.common.get_channels = in_get_channels;
   2474     in->stream.common.get_format = in_get_format;
   2475     in->stream.common.set_format = in_set_format;
   2476     in->stream.common.standby = in_standby;
   2477     in->stream.common.dump = in_dump;
   2478     in->stream.common.set_parameters = in_set_parameters;
   2479     in->stream.common.get_parameters = in_get_parameters;
   2480     in->stream.common.add_audio_effect = in_add_audio_effect;
   2481     in->stream.common.remove_audio_effect = in_remove_audio_effect;
   2482     in->stream.set_gain = in_set_gain;
   2483     in->stream.read = in_read;
   2484     in->stream.get_input_frames_lost = in_get_input_frames_lost;
   2485 
   2486     in->device = devices;
   2487     in->source = source;
   2488     in->dev = adev;
   2489     in->standby = 1;
   2490     in->channel_mask = config->channel_mask;
   2491     in->capture_handle = handle;
   2492 
   2493     /* Update config params with the requested sample rate and channels */
   2494     if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
   2495         if (config->sample_rate == 0)
   2496             config->sample_rate = AFE_PROXY_SAMPLING_RATE;
   2497         if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
   2498                 config->sample_rate != 8000) {
   2499             config->sample_rate = AFE_PROXY_SAMPLING_RATE;
   2500             ret = -EINVAL;
   2501             goto err_open;
   2502         }
   2503         if (config->format == AUDIO_FORMAT_DEFAULT)
   2504             config->format = AUDIO_FORMAT_PCM_16_BIT;
   2505         if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
   2506             config->format = AUDIO_FORMAT_PCM_16_BIT;
   2507             ret = -EINVAL;
   2508             goto err_open;
   2509         }
   2510 
   2511         in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
   2512         in->config = pcm_config_afe_proxy_record;
   2513     } else {
   2514         in->usecase = USECASE_AUDIO_RECORD;
   2515         if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
   2516                 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
   2517             is_low_latency = true;
   2518 #if LOW_LATENCY_CAPTURE_USE_CASE
   2519             in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
   2520 #endif
   2521         }
   2522         in->config = pcm_config_audio_capture;
   2523 
   2524         frame_size = audio_stream_in_frame_size(&in->stream);
   2525         buffer_size = get_input_buffer_size(config->sample_rate,
   2526                                             config->format,
   2527                                             channel_count,
   2528                                             is_low_latency);
   2529         in->config.period_size = buffer_size / frame_size;
   2530     }
   2531     in->config.channels = channel_count;
   2532     in->config.rate = config->sample_rate;
   2533 
   2534     /* This stream could be for sound trigger lab,
   2535        get sound trigger pcm if present */
   2536     audio_extn_sound_trigger_check_and_get_session(in);
   2537 
   2538     *stream_in = &in->stream;
   2539     ALOGV("%s: exit", __func__);
   2540     return 0;
   2541 
   2542 err_open:
   2543     free(in);
   2544     *stream_in = NULL;
   2545     return ret;
   2546 }
   2547 
   2548 static void adev_close_input_stream(struct audio_hw_device *dev __unused,
   2549                                     struct audio_stream_in *stream)
   2550 {
   2551     ALOGV("%s", __func__);
   2552 
   2553     in_standby(&stream->common);
   2554     free(stream);
   2555 
   2556     return;
   2557 }
   2558 
   2559 static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
   2560 {
   2561     return 0;
   2562 }
   2563 
   2564 /* verifies input and output devices and their capabilities.
   2565  *
   2566  * This verification is required when enabling extended bit-depth or
   2567  * sampling rates, as not all qcom products support it.
   2568  *
   2569  * Suitable for calling only on initialization such as adev_open().
   2570  * It fills the audio_device use_case_table[] array.
   2571  *
   2572  * Has a side-effect that it needs to configure audio routing / devices
   2573  * in order to power up the devices and read the device parameters.
   2574  * It does not acquire any hw device lock. Should restore the devices
   2575  * back to "normal state" upon completion.
   2576  */
   2577 static int adev_verify_devices(struct audio_device *adev)
   2578 {
   2579     /* enumeration is a bit difficult because one really wants to pull
   2580      * the use_case, device id, etc from the hidden pcm_device_table[].
   2581      * In this case there are the following use cases and device ids.
   2582      *
   2583      * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
   2584      * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
   2585      * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
   2586      * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
   2587      * [USECASE_AUDIO_RECORD] = {0, 0},
   2588      * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
   2589      * [USECASE_VOICE_CALL] = {2, 2},
   2590      *
   2591      * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
   2592      * USECASE_VOICE_CALL omitted, but possible for either input or output.
   2593      */
   2594 
   2595     /* should be the usecases enabled in adev_open_input_stream() */
   2596     static const int test_in_usecases[] = {
   2597              USECASE_AUDIO_RECORD,
   2598              USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
   2599     };
   2600     /* should be the usecases enabled in adev_open_output_stream()*/
   2601     static const int test_out_usecases[] = {
   2602             USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
   2603             USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
   2604     };
   2605     static const usecase_type_t usecase_type_by_dir[] = {
   2606             PCM_PLAYBACK,
   2607             PCM_CAPTURE,
   2608     };
   2609     static const unsigned flags_by_dir[] = {
   2610             PCM_OUT,
   2611             PCM_IN,
   2612     };
   2613 
   2614     size_t i;
   2615     unsigned dir;
   2616     const unsigned card_id = adev->snd_card;
   2617     char info[512]; /* for possible debug info */
   2618 
   2619     for (dir = 0; dir < 2; ++dir) {
   2620         const usecase_type_t usecase_type = usecase_type_by_dir[dir];
   2621         const unsigned flags_dir = flags_by_dir[dir];
   2622         const size_t testsize =
   2623                 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
   2624         const int *testcases =
   2625                 dir ? test_in_usecases : test_out_usecases;
   2626         const audio_devices_t audio_device =
   2627                 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
   2628 
   2629         for (i = 0; i < testsize; ++i) {
   2630             const audio_usecase_t audio_usecase = testcases[i];
   2631             int device_id;
   2632             snd_device_t snd_device;
   2633             struct pcm_params **pparams;
   2634             struct stream_out out;
   2635             struct stream_in in;
   2636             struct audio_usecase uc_info;
   2637             int retval;
   2638 
   2639             pparams = &adev->use_case_table[audio_usecase];
   2640             pcm_params_free(*pparams); /* can accept null input */
   2641             *pparams = NULL;
   2642 
   2643             /* find the device ID for the use case (signed, for error) */
   2644             device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
   2645             if (device_id < 0)
   2646                 continue;
   2647 
   2648             /* prepare structures for device probing */
   2649             memset(&uc_info, 0, sizeof(uc_info));
   2650             uc_info.id = audio_usecase;
   2651             uc_info.type = usecase_type;
   2652             if (dir) {
   2653                 adev->active_input = &in;
   2654                 memset(&in, 0, sizeof(in));
   2655                 in.device = audio_device;
   2656                 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
   2657                 uc_info.stream.in = &in;
   2658             }  else {
   2659                 adev->active_input = NULL;
   2660             }
   2661             memset(&out, 0, sizeof(out));
   2662             out.devices = audio_device; /* only field needed in select_devices */
   2663             uc_info.stream.out = &out;
   2664             uc_info.devices = audio_device;
   2665             uc_info.in_snd_device = SND_DEVICE_NONE;
   2666             uc_info.out_snd_device = SND_DEVICE_NONE;
   2667             list_add_tail(&adev->usecase_list, &uc_info.list);
   2668 
   2669             /* select device - similar to start_(in/out)put_stream() */
   2670             retval = select_devices(adev, audio_usecase);
   2671             if (retval >= 0) {
   2672                 *pparams = pcm_params_get(card_id, device_id, flags_dir);
   2673 #if LOG_NDEBUG == 0
   2674                 if (*pparams) {
   2675                     ALOGV("%s: (%s) card %d  device %d", __func__,
   2676                             dir ? "input" : "output", card_id, device_id);
   2677                     pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
   2678                     ALOGV(info); /* print parameters */
   2679                 } else {
   2680                     ALOGV("%s: cannot locate card %d  device %d", __func__, card_id, device_id);
   2681                 }
   2682 #endif
   2683             }
   2684 
   2685             /* deselect device - similar to stop_(in/out)put_stream() */
   2686             /* 1. Get and set stream specific mixer controls */
   2687             retval = disable_audio_route(adev, &uc_info);
   2688             /* 2. Disable the rx device */
   2689             retval = disable_snd_device(adev,
   2690                     dir ? uc_info.in_snd_device : uc_info.out_snd_device);
   2691             list_remove(&uc_info.list);
   2692         }
   2693     }
   2694     adev->active_input = NULL; /* restore adev state */
   2695     return 0;
   2696 }
   2697 
   2698 static int adev_close(hw_device_t *device)
   2699 {
   2700     size_t i;
   2701     struct audio_device *adev = (struct audio_device *)device;
   2702 
   2703     if (!adev)
   2704         return 0;
   2705 
   2706     pthread_mutex_lock(&adev_init_lock);
   2707 
   2708     if ((--audio_device_ref_count) == 0) {
   2709         audio_route_free(adev->audio_route);
   2710         free(adev->snd_dev_ref_cnt);
   2711         platform_deinit(adev->platform);
   2712         audio_extn_extspk_deinit(adev->extspk);
   2713         audio_extn_sound_trigger_deinit(adev);
   2714         for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
   2715             pcm_params_free(adev->use_case_table[i]);
   2716         }
   2717         free(device);
   2718     }
   2719 
   2720     pthread_mutex_unlock(&adev_init_lock);
   2721     return 0;
   2722 }
   2723 
   2724 /* This returns 1 if the input parameter looks at all plausible as a low latency period size,
   2725  * or 0 otherwise.  A return value of 1 doesn't mean the value is guaranteed to work,
   2726  * just that it _might_ work.
   2727  */
   2728 static int period_size_is_plausible_for_low_latency(int period_size)
   2729 {
   2730     switch (period_size) {
   2731     case 48:
   2732     case 96:
   2733     case 144:
   2734     case 160:
   2735     case 192:
   2736     case 240:
   2737     case 320:
   2738     case 480:
   2739         return 1;
   2740     default:
   2741         return 0;
   2742     }
   2743 }
   2744 
   2745 static int adev_open(const hw_module_t *module, const char *name,
   2746                      hw_device_t **device)
   2747 {
   2748     int i, ret;
   2749 
   2750     ALOGD("%s: enter", __func__);
   2751     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
   2752     pthread_mutex_lock(&adev_init_lock);
   2753     if (audio_device_ref_count != 0) {
   2754         *device = &adev->device.common;
   2755         audio_device_ref_count++;
   2756         ALOGV("%s: returning existing instance of adev", __func__);
   2757         ALOGV("%s: exit", __func__);
   2758         pthread_mutex_unlock(&adev_init_lock);
   2759         return 0;
   2760     }
   2761     adev = calloc(1, sizeof(struct audio_device));
   2762 
   2763     pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
   2764 
   2765     adev->device.common.tag = HARDWARE_DEVICE_TAG;
   2766     adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
   2767     adev->device.common.module = (struct hw_module_t *)module;
   2768     adev->device.common.close = adev_close;
   2769 
   2770     adev->device.init_check = adev_init_check;
   2771     adev->device.set_voice_volume = adev_set_voice_volume;
   2772     adev->device.set_master_volume = adev_set_master_volume;
   2773     adev->device.get_master_volume = adev_get_master_volume;
   2774     adev->device.set_master_mute = adev_set_master_mute;
   2775     adev->device.get_master_mute = adev_get_master_mute;
   2776     adev->device.set_mode = adev_set_mode;
   2777     adev->device.set_mic_mute = adev_set_mic_mute;
   2778     adev->device.get_mic_mute = adev_get_mic_mute;
   2779     adev->device.set_parameters = adev_set_parameters;
   2780     adev->device.get_parameters = adev_get_parameters;
   2781     adev->device.get_input_buffer_size = adev_get_input_buffer_size;
   2782     adev->device.open_output_stream = adev_open_output_stream;
   2783     adev->device.close_output_stream = adev_close_output_stream;
   2784     adev->device.open_input_stream = adev_open_input_stream;
   2785     adev->device.close_input_stream = adev_close_input_stream;
   2786     adev->device.dump = adev_dump;
   2787 
   2788     /* Set the default route before the PCM stream is opened */
   2789     pthread_mutex_lock(&adev->lock);
   2790     adev->mode = AUDIO_MODE_NORMAL;
   2791     adev->active_input = NULL;
   2792     adev->primary_output = NULL;
   2793     adev->bluetooth_nrec = true;
   2794     adev->acdb_settings = TTY_MODE_OFF;
   2795     /* adev->cur_hdmi_channels = 0;  by calloc() */
   2796     adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
   2797     voice_init(adev);
   2798     list_init(&adev->usecase_list);
   2799     pthread_mutex_unlock(&adev->lock);
   2800 
   2801     /* Loads platform specific libraries dynamically */
   2802     adev->platform = platform_init(adev);
   2803     if (!adev->platform) {
   2804         free(adev->snd_dev_ref_cnt);
   2805         free(adev);
   2806         ALOGE("%s: Failed to init platform data, aborting.", __func__);
   2807         *device = NULL;
   2808         pthread_mutex_unlock(&adev_init_lock);
   2809         return -EINVAL;
   2810     }
   2811 
   2812     adev->extspk = audio_extn_extspk_init(adev);
   2813     audio_extn_sound_trigger_init(adev);
   2814 
   2815     if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
   2816         adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
   2817         if (adev->visualizer_lib == NULL) {
   2818             ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
   2819         } else {
   2820             ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
   2821             adev->visualizer_start_output =
   2822                         (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
   2823                                                         "visualizer_hal_start_output");
   2824             adev->visualizer_stop_output =
   2825                         (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
   2826                                                         "visualizer_hal_stop_output");
   2827         }
   2828     }
   2829 
   2830     if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
   2831         adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
   2832         if (adev->offload_effects_lib == NULL) {
   2833             ALOGE("%s: DLOPEN failed for %s", __func__,
   2834                   OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
   2835         } else {
   2836             ALOGV("%s: DLOPEN successful for %s", __func__,
   2837                   OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
   2838             adev->offload_effects_start_output =
   2839                         (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
   2840                                          "offload_effects_bundle_hal_start_output");
   2841             adev->offload_effects_stop_output =
   2842                         (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
   2843                                          "offload_effects_bundle_hal_stop_output");
   2844         }
   2845     }
   2846 
   2847     adev->bt_wb_speech_enabled = false;
   2848     adev->enable_voicerx = false;
   2849 
   2850     *device = &adev->device.common;
   2851 
   2852     if (k_enable_extended_precision)
   2853         adev_verify_devices(adev);
   2854 
   2855     char value[PROPERTY_VALUE_MAX];
   2856     int trial;
   2857     if (property_get("audio_hal.period_size", value, NULL) > 0) {
   2858         trial = atoi(value);
   2859         if (period_size_is_plausible_for_low_latency(trial)) {
   2860             pcm_config_low_latency.period_size = trial;
   2861             pcm_config_low_latency.start_threshold = trial / 4;
   2862             pcm_config_low_latency.avail_min = trial / 4;
   2863             configured_low_latency_capture_period_size = trial;
   2864         }
   2865     }
   2866     if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
   2867         trial = atoi(value);
   2868         if (period_size_is_plausible_for_low_latency(trial)) {
   2869             configured_low_latency_capture_period_size = trial;
   2870         }
   2871     }
   2872 
   2873     audio_device_ref_count++;
   2874     pthread_mutex_unlock(&adev_init_lock);
   2875 
   2876     ALOGV("%s: exit", __func__);
   2877     return 0;
   2878 }
   2879 
   2880 static struct hw_module_methods_t hal_module_methods = {
   2881     .open = adev_open,
   2882 };
   2883 
   2884 struct audio_module HAL_MODULE_INFO_SYM = {
   2885     .common = {
   2886         .tag = HARDWARE_MODULE_TAG,
   2887         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
   2888         .hal_api_version = HARDWARE_HAL_API_VERSION,
   2889         .id = AUDIO_HARDWARE_MODULE_ID,
   2890         .name = "QCOM Audio HAL",
   2891         .author = "Code Aurora Forum",
   2892         .methods = &hal_module_methods,
   2893     },
   2894 };
   2895