From 503bafe5fc2e0fc0403b2d3e5ec1eef4d7aec635 Mon Sep 17 00:00:00 2001 From: Konsta Date: Mon, 31 Mar 2025 00:38:12 +0300 Subject: [PATCH] audio: cache pcm card and device * It's always been intended that changing audio output devices requires a reboot. Get the PCM card and device once when the HAL is initialized. Might save a few ms on start_output_stream. --- audio/audio_hw.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/audio/audio_hw.c b/audio/audio_hw.c index e546df3..a62f8fb 100644 --- a/audio/audio_hw.c +++ b/audio/audio_hw.c @@ -57,6 +57,9 @@ #define CHANNEL_STEREO 2 #define MIN_WRITE_SLEEP_US 5000 +int pcm_card; +int pcm_device; + struct stub_stream_in { struct audio_stream_in stream; }; @@ -148,7 +151,7 @@ static int start_output_stream(struct alsa_stream_out *out) out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PERIOD_SIZE; out->config.avail_min = PERIOD_SIZE; - out->pcm = pcm_open(get_pcm_card(), get_pcm_device(), PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC, &out->config); + out->pcm = pcm_open(pcm_card, pcm_device, PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC, &out->config); if (!pcm_is_ready(out->pcm)) { ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm)); @@ -484,7 +487,7 @@ static int adev_open_output_stream(struct audio_hw_device *dev, struct pcm_params *params; int ret = 0; - params = pcm_params_get(get_pcm_card(), get_pcm_device(), PCM_OUT); + params = pcm_params_get(pcm_card, pcm_device, PCM_OUT); if (!params) return -ENOSYS; @@ -690,6 +693,10 @@ static int adev_open(const hw_module_t* module, const char* name, ALOGV("adev_open: %s", name); + pcm_card = get_pcm_card(); + pcm_device = get_pcm_device(); + ALOGI("adev_open: pcm_card %d, pcm_device %d", pcm_card, pcm_device); + if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;