Hi guys

So I am writing a decoder that reads rtp video packets from the network,
but I am running into a problem where since av_read_frame is a blocking
function ( doesn't return until it actually reads something ) so that
means if the remote end suddenly stops sending video then my program
would get stuck on av_read_frame. Is there a way to make av_read_frame
to be nonblocking? I tried to set the AVFMT_FLAG_NONBLOCK, but it
doesn't seem to work, then again I am not sure if I am doing it
correctly. Here is what I did

Start_Decode()

{

av_register_all();

 

      // Open video file

      if(av_open_input_file(&pInFormatCtx, "stream.sdp", NULL, 0,
NULL)!=0) {

            exit( 2 );

      }

 

      // Retrieve stream information

      if(av_find_stream_info(pInFormatCtx)<0) {

            exit( 3 );

      }

  

      // Find the first video stream

      videoStream=-1;

      for(int i=0; i<(int) pInFormatCtx->nb_streams; i++)

            if(
pInFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {

                  videoStream=i;

                  break;

            }

      if( videoStream==-1 ) {

            exit( 4 );

      }

  

      // Get a pointer to the codec context for the video stream

      pInCodecCtx=pInFormatCtx->streams[videoStream]->codec;

  

      // Find the decoder for the video stream

      pInCodec=avcodec_find_decoder( pInCodecCtx->codec_id );

      if( pInCodec==NULL ) {

            fprintf(stderr, "Unsupported codec!\n");

            exit( 5 );

 

      }

  // Open codec

      if(avcodec_open( pInCodecCtx, pInCodec ) < 0 ) {

            exit( 6 );

      }

  

      // Allocate video frame

      pInFrame=avcodec_alloc_frame();

 

      pInFormatCtx->flags |= AVFMT_FLAG_NONBLOCK;

      // Read frames and save first five frames to disk

      while( av_read_frame(pInFormatCtx, &Inpacket)>=0 &&
ThreadIsRunning ) {

            // Is this a packet from the video stream?

            if(Inpacket.stream_index==videoStream) {

             // Decode video frame

                  avcodec_decode_video(pInCodecCtx, pInFrame,
&frameFinished, 

                                                Inpacket.data,
Inpacket.size);

      .

      .

      .

}

Any help would be greatly appreciated Thanks.

 

Xin Cai

 

_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to