From patchwork Mon Mar 1 22:20:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 26050 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 0869F44852F for ; Tue, 2 Mar 2021 00:21:42 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B2A3368A84E; Tue, 2 Mar 2021 00:21:41 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 23DED68A73D for ; Tue, 2 Mar 2021 00:21:35 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id D78B2E5019; Mon, 1 Mar 2021 23:21:33 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9rZXjPNsbG9r; Mon, 1 Mar 2021 23:21:05 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 434DBE424E; Mon, 1 Mar 2021 23:21:05 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 1 Mar 2021 23:20:49 +0100 Message-Id: <20210301222049.15456-1-cus@passwd.hu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210228134613.ul7bb7onk6cmpsvp@phare.normalesup.org> References: <20210228134613.ul7bb7onk6cmpsvp@phare.normalesup.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 2/5] avdevice/alsa_dec: make sure we have enough data in non-blocking mode X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Otherwise we might return 1-2 samples per packet if av_read_frame() call rate is only sligthly less than the stream sample rate. Signed-off-by: Marton Balint --- libavdevice/alsa.c | 5 +++++ libavdevice/alsa.h | 1 + libavdevice/alsa_dec.c | 22 ++++++++++++---------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/libavdevice/alsa.c b/libavdevice/alsa.c index 117b2ea144..ee282fac16 100644 --- a/libavdevice/alsa.c +++ b/libavdevice/alsa.c @@ -286,6 +286,10 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, } } + s->pkt = av_packet_alloc(); + if (!s->pkt) + goto fail1; + s->h = h; return 0; @@ -308,6 +312,7 @@ av_cold int ff_alsa_close(AVFormatContext *s1) if (CONFIG_ALSA_INDEV) ff_timefilter_destroy(s->timefilter); snd_pcm_close(s->h); + av_packet_free(&s->pkt); return 0; } diff --git a/libavdevice/alsa.h b/libavdevice/alsa.h index 1ed8c82199..07783c983a 100644 --- a/libavdevice/alsa.h +++ b/libavdevice/alsa.h @@ -58,6 +58,7 @@ typedef struct AlsaData { void *reorder_buf; int reorder_buf_size; ///< in frames int64_t timestamp; ///< current timestamp, without latency applied. + AVPacket *pkt; } AlsaData; /** diff --git a/libavdevice/alsa_dec.c b/libavdevice/alsa_dec.c index 6d568737b3..88bf32d25f 100644 --- a/libavdevice/alsa_dec.c +++ b/libavdevice/alsa_dec.c @@ -104,34 +104,36 @@ static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt) int64_t dts; snd_pcm_sframes_t delay = 0; - if (av_new_packet(pkt, s->period_size * s->frame_size) < 0) { - return AVERROR(EIO); + if (!s->pkt->data) { + int ret = av_new_packet(s->pkt, s->period_size * s->frame_size); + if (ret < 0) + return ret; + s->pkt->size = 0; } - while ((res = snd_pcm_readi(s->h, pkt->data, s->period_size)) < 0) { + do { + while ((res = snd_pcm_readi(s->h, s->pkt->data + s->pkt->size, s->period_size - s->pkt->size / s->frame_size)) < 0) { if (res == -EAGAIN) { - av_packet_unref(pkt); - return AVERROR(EAGAIN); } + s->pkt->size = 0; if (ff_alsa_xrun_recover(s1, res) < 0) { av_log(s1, AV_LOG_ERROR, "ALSA read error: %s\n", snd_strerror(res)); - av_packet_unref(pkt); - return AVERROR(EIO); } ff_timefilter_reset(s->timefilter); - } + } + s->pkt->size += res * s->frame_size; + } while (s->pkt->size < s->period_size * s->frame_size); + av_packet_move_ref(pkt, s->pkt); dts = av_gettime(); snd_pcm_delay(s->h, &delay); dts -= av_rescale(delay + res, 1000000, s->sample_rate); pkt->pts = ff_timefilter_update(s->timefilter, dts, s->last_period); s->last_period = res; - pkt->size = res * s->frame_size; - return 0; }