audio: import alsa_utils

* system/media/ at android-16.0.0_r1.
This commit is contained in:
Konsta
2025-10-22 17:23:45 +03:00
parent 7d6df38051
commit 487b7689f6
9 changed files with 1689 additions and 0 deletions

View File

@@ -0,0 +1,101 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_DEVICE_PROFILE_H
#define ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_DEVICE_PROFILE_H
#include <stdbool.h>
#include <system/audio.h>
#include <tinyalsa/asoundlib.h>
#define MAX_PROFILE_FORMATS 6 /* We long support the 5 standard formats defined
* in asound.h, so we just need this to be 1 more
* than that */
#define MAX_PROFILE_SAMPLE_RATES 14 /* this number needs to be 1 more than the number of
* sample rates in std_sample_rates[]
* (in alsa_device_profile.c) */
#define MAX_PROFILE_CHANNEL_COUNTS (FCC_LIMIT + 1)
/* this number need to be 1 more than the number of
* standard channel formats in std_channel_counts[]
* (in alsa_device_profile.c) */
#define DEFAULT_SAMPLE_RATE 44100
#define DEFAULT_SAMPLE_FORMAT PCM_FORMAT_S16_LE
#define DEFAULT_CHANNEL_COUNT 2
#define DEFAULT_PERIOD_COUNT 4
typedef struct {
int card;
int device;
int direction; /* PCM_OUT or PCM_IN */
int extra_latency_ms; /* any extra latency in addition to the buffer */
enum pcm_format formats[MAX_PROFILE_FORMATS];
/* note that this list is sorted highest rate to lowest */
unsigned sample_rates[MAX_PROFILE_SAMPLE_RATES];
unsigned channel_counts[MAX_PROFILE_CHANNEL_COUNTS];
bool is_valid;
/* read from the hardware device */
struct pcm_config default_config;
unsigned min_period_size;
unsigned max_period_size;
unsigned min_channel_count;
unsigned max_channel_count;
} alsa_device_profile;
void profile_init(alsa_device_profile* profile, int direction);
bool profile_is_initialized(const alsa_device_profile* profile);
bool profile_is_valid(const alsa_device_profile* profile);
bool profile_is_cached_for(const alsa_device_profile* profile, int card, int device);
void profile_decache(alsa_device_profile* profile);
bool profile_fill_builtin_device_info(alsa_device_profile* profile, struct pcm_config* config,
unsigned buffer_frame_count);
bool profile_read_device_info(alsa_device_profile* profile);
/* Audio Config Strings Methods */
char * profile_get_sample_rate_strs(const alsa_device_profile* profile);
char * profile_get_format_strs(const alsa_device_profile* profile);
char * profile_get_channel_count_strs(const alsa_device_profile* profile);
/* Sample Rate Methods */
unsigned profile_get_default_sample_rate(const alsa_device_profile* profile);
unsigned profile_get_highest_sample_rate(const alsa_device_profile* profile);
bool profile_is_sample_rate_valid(const alsa_device_profile* profile, unsigned rate);
/* Format Methods */
enum pcm_format profile_get_default_format(const alsa_device_profile* profile);
bool profile_is_format_valid(const alsa_device_profile* profile, enum pcm_format fmt);
/* Channel Methods */
unsigned profile_get_default_channel_count(const alsa_device_profile* profile);
unsigned profile_get_closest_channel_count(const alsa_device_profile* profile, unsigned count);
bool profile_is_channel_count_valid(const alsa_device_profile* profile, unsigned count);
/* Utility */
unsigned profile_calc_min_period_size(const alsa_device_profile* profile, unsigned sample_rate);
unsigned int profile_get_period_size(const alsa_device_profile* profile, unsigned sample_rate);
/* Debugging */
void profile_dump(const alsa_device_profile* profile, int fd);
#endif /* ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_DEVICE_PROFILE_H */

View File

@@ -0,0 +1,76 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_DEVICE_PROXY_H
#define ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_DEVICE_PROXY_H
#include <tinyalsa/asoundlib.h>
#include "alsa_device_profile.h"
typedef struct {
const alsa_device_profile* profile;
struct pcm_config alsa_config;
struct pcm * pcm;
size_t frame_size; /* valid after proxy_prepare(), the frame size in bytes */
uint64_t transferred; /* the total frames transferred, not cleared on standby */
} alsa_device_proxy;
/* State */
int proxy_prepare(alsa_device_proxy * proxy, const alsa_device_profile * profile,
struct pcm_config * config, bool require_exact_match);
int proxy_prepare_from_default_config(
alsa_device_proxy * proxy, const alsa_device_profile * profile);
int proxy_open(alsa_device_proxy * proxy);
void proxy_close(alsa_device_proxy * proxy);
int proxy_get_presentation_position(const alsa_device_proxy * proxy,
uint64_t *frames, struct timespec *timestamp);
int proxy_get_capture_position(const alsa_device_proxy * proxy,
int64_t *frames, int64_t *time);
int proxy_stop(alsa_device_proxy * proxy);
/* Attributes */
unsigned proxy_get_sample_rate(const alsa_device_proxy * proxy);
enum pcm_format proxy_get_format(const alsa_device_proxy * proxy);
unsigned proxy_get_channel_count(const alsa_device_proxy * proxy);
unsigned int proxy_get_period_size(const alsa_device_proxy * proxy);
unsigned proxy_get_latency(const alsa_device_proxy * proxy);
/*
* Scans the provided list of sample rates and finds the first one that works.
*
* returns the index of the first rate for which the ALSA device can be opened.
* return negative value if none work or an error occurs.
*/
int proxy_scan_rates(alsa_device_proxy * proxy, const unsigned sample_rates[],
bool require_exact_match);
/* I/O */
int proxy_write(alsa_device_proxy * proxy, const void *data, unsigned int count);
int proxy_write_with_retries(
alsa_device_proxy * proxy, const void *data, unsigned int count, int tries);
int proxy_read(alsa_device_proxy * proxy, void *data, unsigned int count);
int proxy_read_with_retries(
alsa_device_proxy * proxy, void *data, unsigned int count, int tries);
/* Debugging */
void proxy_dump(const alsa_device_proxy * proxy, int fd);
#endif /* ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_DEVICE_PROXY_H */

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_FORMAT_H
#define ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_FORMAT_H
#include <system/audio.h>
#include <tinyalsa/asoundlib.h>
enum pcm_format get_pcm_format_for_mask(const struct pcm_mask* mask);
#endif /* ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_FORMAT_H */

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_LOGGING_H
#define ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_LOGGING_H
#include <tinyalsa/asoundlib.h>
void log_pcm_mask(const char* mask_name, const struct pcm_mask* mask);
void log_pcm_params(const struct pcm_params * alsa_hw_params);
void log_pcm_config(const struct pcm_config * config, const char* label);
#endif /* ANDROID_SYSTEM_MEDIA_ALSA_UTILS_ALSA_LOGGING_H */