title: FFMPEG: avcodec_send_packet(); error while using multithread
body: <p>I wrote 2 threads to decode RTSP stream from IP camera as below:</p>
  
  <p>RTSP_read_paket function used to read Packets from RTSP link, packets stored in a queue named Packet_buf.</p>
  
  <pre><code>std::queue&lt;AVPacket&gt; Packet_buf;
  bool pkt_pending_k = false;
  
  int RTSP_read_packet (string url)
  {
      rtsp_init(url);
  
      int ret;
      AVPacket packet;
      av_init_packet(&amp;packet);
  
      while(1)
      {
         ret = av_read_frame(pFormatCtx,&amp;packet);
         if(ret==0)
         {
             if (packet.stream_index == video_stream_index)
             {
                 Packet_buf.push(packet);
                 if((ready1 == false))
                 {
                      ready1 = true;
                      conv1.notify_one();
                 }
             }
             av_packet_unref(&amp;packet);
             cout&lt;&lt;"number of RTSP packet: "&lt;&lt;Packet_buf.size()&lt;&lt;endl;
         }
      }
      return 0;
  }
  </code></pre>
  
  <p>ffmpeg_decode read packets from Packet_buf to decode frames</p>
  
  <pre><code>AVFrame ffmpeg_decode( void )
  {
        AVPacket avpkt;
        av_init_packet(&amp;avpkt);
        int ret;
  
        conv1.wait(lk1,[]{return ready1;});
        while(1)
        {
              while(1)
              {
                   ret = avcodec_receive_frame(pCodecCtx,pFrame);
                   if(ret == AVERROR(EAGAIN)||ret==AVERROR_EOF){
                         break;
                   }
                   return pFrame;
              }
  
              if(!Packet_buf.empty())
              {
                  if(pkt_pending_k == false)
                  {
                    avpkt = Packet_buf.front();
                    Packet_buf.pop();
                  }else{
                      pkt_pending_k = false;
                  }
              }
  
  
              ret = avcodec_send_packet(pCodecCtx, &amp;avpkt); //program halting here
              cout&lt;&lt;"-------------&gt; ret = "&lt;&lt;ret&lt;&lt;endl;
  
              if(ret==AVERROR(EAGAIN))
              {
                  pkt_pending_k = true;
              }
  
                 if(ret&lt;0||ret==AVERROR_EOF)
                 {
                       cout&lt;&lt;"avcodec_send_packet: "&lt;&lt;ret&lt;&lt;endl;
                       break;
                 }
          }
  }
  
  int main () {
          thread Camera2_readPackets(RTSP_read_packet,url);
          thread Camera2_decode(ffmpeg_decode,url);
          Camera2_decode.join();
          return 0;
  }
  </code></pre>
  
  <p>My program halt at line: </p>
  
  <pre><code>ret = avcodec_send_packet(pCodecCtx, &amp;avpkt);
  </code></pre>
  
  <p>Anyone can help me find the problem, thanks !</p>
  
  <p>P/s: 
  rtsp_init function:</p>
  
  <pre><code>int rtsp_init (string url)
  {
      av_register_all();
      avdevice_register_all();
      avcodec_register_all();
      avformat_network_init();
      const char  *filenameSrc = url.c_str();
  
      pFormatCtx = avformat_alloc_context();
      if ( pFormatCtx == NULL )
              return -8;
  
      AVDictionary *options = NULL;
      av_dict_set(&amp;options,"rtsp_flags","prefer_tcp",0);
      av_dict_set(&amp;options,"stimeout","1000000",0);
      int avret = avformat_open_input( &amp;pFormatCtx, filenameSrc, NULL, &amp;options );
      av_dict_free(&amp;options);
  
      if ( avret != 0 ) {
              std::cout &lt;&lt; "Open File Error 12" &lt;&lt; std::endl;
              return -12;
          }
  
      avret = avformat_find_stream_info( pFormatCtx, NULL );
      if (  avret &lt; 0 ) {
              std::cout &lt;&lt; "Get Stream Information Error 13" &lt;&lt; std::endl;
              avformat_close_input( &amp;pFormatCtx );
              pFormatCtx = NULL;
              return -13;
       }
      av_dump_format( pFormatCtx, 0, filenameSrc, 0 );
  
      video_stream_index = av_find_best_stream(pFormatCtx,AVMEDIA_TYPE_VIDEO,-1,-1,NULL,0);
      if ( video_stream_index &lt; 0 ) {
          std::cout &lt;&lt; "Video stream was not found Error 14" &lt;&lt; std::endl;
          avformat_close_input( &amp;pFormatCtx );
          pFormatCtx = NULL;
          return -14;
      }
  
      pCodecCtx = avcodec_alloc_context3(NULL);
      avret = avcodec_parameters_to_context(pCodecCtx,pFormatCtx-&gt;streams[video_stream_index]-&gt;codecpar);
      if(avret&lt;0)
      {
          std::cout &lt;&lt; "codec not found Error 15" &lt;&lt; std::endl;
          return -15;
      }
  
      pCodec  = avcodec_find_decoder( pCodecCtx-&gt;codec_id );
      avret = avcodec_open2( pCodecCtx, pCodec, NULL );
      if ( avret &lt; 0) {
          std::cout &lt;&lt; "Open Codec Error 16" &lt;&lt; std::endl;
         return -16;
      }
  
      pFrame    = av_frame_alloc();
      pFrameRGB = av_frame_alloc();
  
      pFrame-&gt;width = pCodecCtx-&gt;width;
      pFrame-&gt;height = pCodecCtx-&gt;height;
      pFrame-&gt;format = pCodecCtx-&gt;pix_fmt;
      avret = av_frame_get_buffer(pFrame,0);
      if (avret &lt; 0)
      {
          return -17;
      }
  
      pFrameRGB-&gt;width = pCodecCtx-&gt;width;
      pFrameRGB-&gt;height = pCodecCtx-&gt;height;
      pFrameRGB-&gt;format = AV_PIX_FMT_BGR24;
  
      avret = av_frame_get_buffer(pFrameRGB, 0);
      if (avret &lt; 0)
      {
          return -18;
      }
  
      return ( EXIT_SUCCESS );
  }
  </code></pre>