Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: James Almer <jamrial@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH] avdevice/alsa: simplify passing ff_alsa_open a channel layout
Date: Fri,  7 Feb 2025 23:53:27 -0300
Message-ID: <20250208025327.7733-1-jamrial@gmail.com> (raw)

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>
---
 libavdevice/alsa.c     | 14 +++++++-------
 libavdevice/alsa.h     |  4 ++--
 libavdevice/alsa_dec.c |  2 +-
 libavdevice/alsa_enc.c |  2 +-
 4 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/libavdevice/alsa.c b/libavdevice/alsa.c
index d62ccc09c6..cfdb28ff49 100644
--- a/libavdevice/alsa.c
+++ b/libavdevice/alsa.c
@@ -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));
diff --git a/libavdevice/alsa.h b/libavdevice/alsa.h
index 3e1ba31384..d3dfa478c5 100644
--- a/libavdevice/alsa.h
+++ b/libavdevice/alsa.h
@@ -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.
diff --git a/libavdevice/alsa_dec.c b/libavdevice/alsa_dec.c
index f0738e3dea..63409a7785 100644
--- a/libavdevice/alsa_dec.c
+++ b/libavdevice/alsa_dec.c
@@ -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);
diff --git a/libavdevice/alsa_enc.c b/libavdevice/alsa_enc.c
index 0b4c7834f7..971cff688c 100644
--- a/libavdevice/alsa_enc.c
+++ b/libavdevice/alsa_enc.c
@@ -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",
-- 
2.48.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

                 reply	other threads:[~2025-02-08  2:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250208025327.7733-1-jamrial@gmail.com \
    --to=jamrial@gmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git