Skip to content

Commit eb3d507

Browse files
committed
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 <[email protected]>
1 parent d62fd6e commit eb3d507

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

libavdevice/alsa.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ switch(format) {\
127127
case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout; break;\
128128
}
129129

130-
static av_cold int find_reorder_func(AlsaData *s, int codec_id, AVChannelLayout *layout, int out)
130+
static av_cold int find_reorder_func(AlsaData *s, int codec_id,
131+
const AVChannelLayout *layout, int out)
131132
{
132133
int format;
133134

@@ -172,10 +173,9 @@ static av_cold int find_reorder_func(AlsaData *s, int codec_id, AVChannelLayout
172173

173174
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
174175
unsigned int *sample_rate,
175-
int channels, enum AVCodecID *codec_id)
176+
const AVChannelLayout *layout, enum AVCodecID *codec_id)
176177
{
177178
AlsaData *s = ctx->priv_data;
178-
AVChannelLayout *layout = &ctx->streams[0]->codecpar->ch_layout;
179179
const char *audio_device;
180180
int res, flags = 0;
181181
snd_pcm_format_t format;
@@ -193,7 +193,7 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
193193
av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
194194
return AVERROR(ENOSYS);
195195
}
196-
s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
196+
s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * layout->nb_channels;
197197

198198
if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
199199
flags = SND_PCM_NONBLOCK;
@@ -240,10 +240,10 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
240240
goto fail;
241241
}
242242

243-
res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
243+
res = snd_pcm_hw_params_set_channels(h, hw_params, layout->nb_channels);
244244
if (res < 0) {
245245
av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
246-
channels, snd_strerror(res));
246+
layout->nb_channels, snd_strerror(res));
247247
goto fail;
248248
}
249249

@@ -277,7 +277,7 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
277277

278278
snd_pcm_hw_params_free(hw_params);
279279

280-
if (channels > 2 && layout->order != AV_CHANNEL_ORDER_UNSPEC) {
280+
if (layout->nb_channels > 2 && layout->order != AV_CHANNEL_ORDER_UNSPEC) {
281281
if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
282282
char name[128];
283283
av_channel_layout_describe(layout, name, sizeof(name));

libavdevice/alsa.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ typedef struct AlsaData {
7272
* @param mode either SND_PCM_STREAM_CAPTURE or SND_PCM_STREAM_PLAYBACK
7373
* @param sample_rate in: requested sample rate;
7474
* out: actually selected sample rate
75-
* @param channels number of channels
75+
* @param layout channel layout
7676
* @param codec_id in: requested AVCodecID or AV_CODEC_ID_NONE;
7777
* out: actually selected AVCodecID, changed only if
7878
* AV_CODEC_ID_NONE was requested
@@ -82,7 +82,7 @@ typedef struct AlsaData {
8282
av_warn_unused_result
8383
int ff_alsa_open(AVFormatContext *s, snd_pcm_stream_t mode,
8484
unsigned int *sample_rate,
85-
int channels, enum AVCodecID *codec_id);
85+
const AVChannelLayout *layout, enum AVCodecID *codec_id);
8686

8787
/**
8888
* Close the ALSA PCM.

libavdevice/alsa_dec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static av_cold int audio_read_header(AVFormatContext *s1)
8080
}
8181
#endif
8282

83-
ret = ff_alsa_open(s1, SND_PCM_STREAM_CAPTURE, &s->sample_rate, s->ch_layout.nb_channels,
83+
ret = ff_alsa_open(s1, SND_PCM_STREAM_CAPTURE, &s->sample_rate, &s->ch_layout,
8484
&codec_id);
8585
if (ret < 0) {
8686
return AVERROR(EIO);

libavdevice/alsa_enc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static av_cold int audio_write_header(AVFormatContext *s1)
6666
sample_rate = st->codecpar->sample_rate;
6767
codec_id = st->codecpar->codec_id;
6868
res = ff_alsa_open(s1, SND_PCM_STREAM_PLAYBACK, &sample_rate,
69-
st->codecpar->ch_layout.nb_channels, &codec_id);
69+
&st->codecpar->ch_layout, &codec_id);
7070
if (sample_rate != st->codecpar->sample_rate) {
7171
av_log(s1, AV_LOG_ERROR,
7272
"sample rate %d not available, nearest is %d\n",

0 commit comments

Comments
 (0)