1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-02-20 07:48:15 +02:00

avdevice/alsa: simplify passing ff_alsa_open a channel layout

This also ensures the layout set during the indev init is used instead of the
blank one in st->codecpar.

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2025-02-07 23:52:01 -03:00
parent d62fd6e9c8
commit eb3d507b7f
4 changed files with 11 additions and 11 deletions

View File

@ -127,7 +127,8 @@ switch(format) {\
case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout; break;\
}
static av_cold int find_reorder_func(AlsaData *s, int codec_id, AVChannelLayout *layout, int out)
static av_cold int find_reorder_func(AlsaData *s, int codec_id,
const AVChannelLayout *layout, int out)
{
int format;
@ -172,10 +173,9 @@ static av_cold int find_reorder_func(AlsaData *s, int codec_id, AVChannelLayout
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
unsigned int *sample_rate,
int channels, enum AVCodecID *codec_id)
const AVChannelLayout *layout, enum AVCodecID *codec_id)
{
AlsaData *s = ctx->priv_data;
AVChannelLayout *layout = &ctx->streams[0]->codecpar->ch_layout;
const char *audio_device;
int res, flags = 0;
snd_pcm_format_t format;
@ -193,7 +193,7 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
return AVERROR(ENOSYS);
}
s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * layout->nb_channels;
if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
flags = SND_PCM_NONBLOCK;
@ -240,10 +240,10 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
goto fail;
}
res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
res = snd_pcm_hw_params_set_channels(h, hw_params, layout->nb_channels);
if (res < 0) {
av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
channels, snd_strerror(res));
layout->nb_channels, snd_strerror(res));
goto fail;
}
@ -277,7 +277,7 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
snd_pcm_hw_params_free(hw_params);
if (channels > 2 && layout->order != AV_CHANNEL_ORDER_UNSPEC) {
if (layout->nb_channels > 2 && layout->order != AV_CHANNEL_ORDER_UNSPEC) {
if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
char name[128];
av_channel_layout_describe(layout, name, sizeof(name));

View File

@ -72,7 +72,7 @@ typedef struct AlsaData {
* @param mode either SND_PCM_STREAM_CAPTURE or SND_PCM_STREAM_PLAYBACK
* @param sample_rate in: requested sample rate;
* out: actually selected sample rate
* @param channels number of channels
* @param layout channel layout
* @param codec_id in: requested AVCodecID or AV_CODEC_ID_NONE;
* out: actually selected AVCodecID, changed only if
* AV_CODEC_ID_NONE was requested
@ -82,7 +82,7 @@ typedef struct AlsaData {
av_warn_unused_result
int ff_alsa_open(AVFormatContext *s, snd_pcm_stream_t mode,
unsigned int *sample_rate,
int channels, enum AVCodecID *codec_id);
const AVChannelLayout *layout, enum AVCodecID *codec_id);
/**
* Close the ALSA PCM.

View File

@ -80,7 +80,7 @@ static av_cold int audio_read_header(AVFormatContext *s1)
}
#endif
ret = ff_alsa_open(s1, SND_PCM_STREAM_CAPTURE, &s->sample_rate, s->ch_layout.nb_channels,
ret = ff_alsa_open(s1, SND_PCM_STREAM_CAPTURE, &s->sample_rate, &s->ch_layout,
&codec_id);
if (ret < 0) {
return AVERROR(EIO);

View File

@ -66,7 +66,7 @@ static av_cold int audio_write_header(AVFormatContext *s1)
sample_rate = st->codecpar->sample_rate;
codec_id = st->codecpar->codec_id;
res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
st->codecpar->ch_layout.nb_channels, &codec_id);
&st->codecpar->ch_layout, &codec_id);
if (sample_rate != st->codecpar->sample_rate) {
av_log(s1, AV_LOG_ERROR,
"sample rate %d not available, nearest is %d\n",