[Libav-user] Close a RTSP stream

Jeremy Lugagne j.lugagne at spikenet-technology.com
Thu Aug 22 10:44:26 CEST 2013


Hello all,

I'm using the libav to grab images from a network camera with the rtsp protocol but I have an issue. When I release the stream, my camera don't release resources for the sessions, so when I do 4 tests with my applications I must restart the camera because I have no more slot.

This is how I open the stream :
<code>
  AVFormatContext *pFormatCtx;
  AVCodecContext* vCodecCtxp;
  AVPacket packet;
  AVFrame *pFrame;
  AVFrame *pFrameRGB = NULL;
  int numBytes;
  AVDictionary *optionsDict = NULL;
  struct SwsContext *sws_ctx = NULL;
  uint8_t *buffer = NULL;
  AVCodec *videoCodec;

  av_register_all();
  avformat_network_init();
  pFormatCtx = avformat_alloc_context();
  if(avformat_open_input(&pFormatCtx, qPrintable(url), NULL, 0)!=0)
     return false; // Couldn't open file
  videoStream=-1;

  if(avformat_find_stream_info(pFormatCtx,NULL) < 0){
    return false;
  }
  for(int i=0; i<pFormatCtx->nb_streams; i++) {
    if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO && videoStream < 0) {
      videoStream=i;
    }
  }
  vCodecCtxp = pFormatCtx->streams[videoStream]->codec;

  // Allocate an AVFrame structure
  pFrameRGB = avcodec_alloc_frame();
  pFrame = avcodec_alloc_frame();
  if(pFrameRGB==NULL)  return ;

  // Determine required buffer size and allocate buffer
  numBytes=avpicture_get_size(PIX_FMT_RGB24, vCodecCtxp->width, vCodecCtxp->height);
  buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

  sws_ctx = sws_getContext(vCodecCtxp->width, vCodecCtxp->height, vCodecCtxp->pix_fmt, vCodecCtxp->width, vCodecCtxp->height, PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL );

  // Assign appropriate parts of buffer to image planes in pFrameRGB. Note that pFrameRGB is an AVFrame, but AVFrame is a superset of AVPicture
  avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, vCodecCtxp->width, vCodecCtxp->height);
  
  // Assign appropriate parts of buffer to image planes in pFrameRGB. Note that pFrameRGB is an AVFrame, but AVFrame is a superset of AVPicture
  int res = avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, vCodecCtxp->width, vCodecCtxp->height);
  
  videoCodec=avcodec_find_decoder(vCodecCtxp->codec_id);
  avcodec_open2(vCodecCtxp, videoCodec, 0);

  av_read_play(pFormatCtx);
</code>

And how I close it :
<code>
  av_free(pFrameRGB);
  av_free(buffer);
  av_free(pFrame);
  sws_freeContext(sws_ctx);
  avformat_close_input(&pFormatCtx);
</code>

Do i forgetting something to close properly my stream ? Because when I watch what messages are sent to the camera I see the TEARDOWN message.

Thanks in advance,

Jeremy L.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20130822/439f0b65/attachment.html>


More information about the Libav-user mailing list