From 2711563b0ec712fe680c0b2f9056fcfd5bd16ce1 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Tue, 25 Aug 2026 10:33:29 -0400 Subject: [PATCH] MT#65420 pick correct stream when reading file Look for a media stream of the appropriate type, and read packets only belonging to that stream Change-Id: I5e3532b913f2a429df3349aa8c392002eaeb8575 --- daemon/media_player.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/daemon/media_player.c b/daemon/media_player.c index e1c7692df..8cc51c5ca 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -1159,7 +1159,19 @@ static bool media_player_read_packet(struct media_player *mp) { if (!mp->coder.fmtctx) return true; - int ret = av_read_frame(mp->coder.fmtctx, mp->coder.pkt); + int ret; + + while (true) { + ret = av_read_frame(mp->coder.fmtctx, mp->coder.pkt); + + if (ret != 0) + break; + if (mp->coder.pkt->stream_index == mp->coder.avstream->index) + break; + + av_packet_unref(mp->coder.pkt); + } + if (ret < 0) { if (ret == AVERROR_EOF) { /* Duration counter cannot underflow and is always aligned to 0 when used. @@ -1254,12 +1266,27 @@ static bool media_player_play_start(struct media_player *mp, const rtp_payload_t int ret = 0; // needed to have usable duration for some formats. ignore errors. - if (!mp->coder.fmtctx->streams || !mp->coder.fmtctx->streams[0]) + if (!mp->coder.fmtctx->streams || !mp->coder.fmtctx->streams[0] || !mp->coder.fmtctx->nb_streams) avformat_find_stream_info(mp->coder.fmtctx, NULL); - mp->coder.avstream = mp->coder.fmtctx->streams[0]; + mp->coder.avstream = NULL; + + for (unsigned int i = 0; i < mp->coder.fmtctx->nb_streams; i++) { + AVStream *st = mp->coder.fmtctx->streams[i]; + + if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO && mp->media->type_id == MT_AUDIO) + continue; + + ilog(LOG_DEBUG, "Using stream #%u for " STR_FORMAT, + i, STR_FMT(&mp->media->type)); + mp->coder.avstream = st; + break; + + } + if (!mp->coder.avstream) { - ilog(LOG_ERR, "No AVStream present in format context"); + ilog(LOG_ERR, "No usable " STR_FORMAT " media stream present in file", + STR_FMT(&mp->media->type)); return false; }