I have setup an application to receive and decode an UDP video stream and
it's working fine. During initialization, I loop through all of the streams
in the AVFormatContext and use the first video stream, and I remember that
stream index. As I'm looping to receive packets using av_read_frame(), I
check the AVPacket.stream_index to make sure I only process packets with
the same stream index I found during initialization. If it's the same
stream index, I decode it and process it.
This all works fine, except in one circumstance. I'm using VLC to generate
the UDP stream, and it's simply looping an input file. When VLC gets loops
back to the beginning of the file, I don't get any error code returned from
av_read_frame(), but the packet that it reads has a different stream_index;
it is incremented by 1. Subsequently, my check to ensure I'm processing the
correct stream will fail, because all of a sudden the packets have a new
stream_index.
Is there some other indicator I can use to know this happened? I can put
some code in to try and infer this condition, and start processing the new
stream, but I would really much rather have a definitive indicator that the
stream_index has changed for a reason.
Here's some pseudo code showing what I'm doing:
AVFormatContext* fc = 0;
AVCodecContext* cc = 0;
AVCodec* codec = 0;
int streamIndex = -1;
initialize(char* url)
{
fc = avformat_alloc_context();
avformat_open_input(&fc, url, 0, 0);
avformat_find_stream_info(fc, 0);
for(int index = 0; index < fc->nb_streams; ++index)
{
if(fc->streams[index]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
streamIndex = index;
cc = fc->streams[streamIndex]->codec;
codec = avcodec_find_decoder(cc->codec_id);
break;
}
}
if(codec == 0)
{
exit(0);
}
avcodec_open2(cc, codec, 0);
}
readVideo()
{
while(1)
{
AVPacket* packet = new AVPacket();
int readStatus = av_read_frame(fc, packet);
if(readStatus < 0)
{
// Do error handling
}
else
{
// ----> This is where the AVPacket.stream_index shows up incremented after
VLC loops the video
if(packet->stream_index != streamIndex)
{
av_free_packet(packet);
continue;
}
// ... continue to decode and process packet ...
}
}
}
Thanks.
_______________________________________________
libav-api mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-api