From 215b19773af21a7d33fe52e67a789820ebbc6831 Mon Sep 17 00:00:00 2001 From: Will Storey Date: Tue, 14 Mar 2017 11:07:53 -0700 Subject: [PATCH] Ensure we always write to output stream 0 If an input has multiple streams, then the packet's stream index for the video stream we are reading may not match our output's stream index. This can lead to errors such as: [mp4 @ 0x5631c5836000] Invalid packet stream index: 1 Update the packet's stream index, prior to writing it, to match what it is in our output. I also update a comment (around line 294) to clarify that while we're skipping a packet, it's not because it's not video. --- videostreamer.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/videostreamer.c b/videostreamer.c index b1edabb..0581909 100644 --- a/videostreamer.c +++ b/videostreamer.c @@ -91,9 +91,7 @@ vs_open_input(const char * const input_format_name, continue; } - // I take the first video stream only. input->video_stream_index = (int) i; - break; } @@ -296,7 +294,8 @@ vs_read_packet(const struct VSInput * input, AVPacket * const pkt, if (pkt->stream_index != input->video_stream_index) { if (verbose) { - printf("skipping non video packet\n"); + printf("skipping packet from input stream %d, our video is from stream %d\n", + pkt->stream_index, input->video_stream_index); } av_packet_unref(pkt); @@ -317,7 +316,7 @@ vs_read_packet(const struct VSInput * input, AVPacket * const pkt, // // Returns: // -1 if error -// 1 if wrote packet +// 1 if we wrote the packet int vs_write_packet(const struct VSInput * const input, const struct VSOutput * const output, AVPacket * const pkt, @@ -329,7 +328,34 @@ vs_write_packet(const struct VSInput * const input, } AVStream * const in_stream = input->format_ctx->streams[pkt->stream_index]; + if (!in_stream) { + printf("input stream not found with stream index %d\n", pkt->stream_index); + return -1; + } + + + // If there are multiple input streams, then the stream index on the packet + // may not match the stream index in our output. We need to ensure the index + // matches. Note by this point we have checked that it is indeed a packet + // from the stream we want (we do this when reading the packet). + // + // As we only ever have a single output stream (one, video), the index will + // be 0. + if (pkt->stream_index != 0) { + if (verbose) { + printf("updating packet stream index to 0 (from %d)\n", + pkt->stream_index); + } + + pkt->stream_index = 0; + } + + AVStream * const out_stream = output->format_ctx->streams[pkt->stream_index]; + if (!out_stream) { + printf("output stream not found with stream index %d\n", pkt->stream_index); + return -1; + } // Set pts/dts if not set. Otherwise we will receive warnings like //