[libav-api] Release RTSP stream

Jeremy Lugagne j.lugagne at spikenet-technology.com
Fri Oct 18 15:37:14 CEST 2013


I'm grabbing frame from an ip camera with the rtsp protocol but I have an issue, I can't release the stream. I mean, my camera is limited at 4 connexions in a same time but if start 4 connections/disconnections one after the other, I have the error "453 Not enough Bandwidth" on av_read_play(). So my question is, how to stop properly the connection to avoid this issue. Here is my testing code, I used wireshark to track messages and the "TEARDOWN" message is sent but not used.

Code :
#include <QtCore/QCoreApplication>
#include <QDebug>

extern "C" {
  #include <libavcodec/avcodec.h>
  #include <libavformat/avformat.h>
  #include <libswscale/swscale.h>

bool grab()
  // Declarations
  AVPacket packet;
  AVFrame *pFrame;
  AVFrame *pFrameRGB = NULL;
  int numBytes;
  AVDictionary *optionsDict = NULL;
  struct SwsContext *sws_ctx = NULL;
  uint8_t *buffer = NULL;
  AVFormatContext *pFormatCtx;
  AVCodecContext *vCodecCtxp;
  int videoStream;
  int res;
  // Init libav
  pFormatCtx = avformat_alloc_context();

  // Open stream
  res = avformat_open_input(&pFormatCtx, "rtsp://", NULL, 0);
    return false; // Couldn't open file
  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) {
  vCodecCtxp = pFormatCtx->streams[videoStream]->codec;

  AVCodec *videoCodec;

  res = avcodec_open2(vCodecCtxp, videoCodec, 0);
  if(res != 0){
    return false;

  // Start playing
  res = av_read_play(pFormatCtx);
  if(res != 0){
    return false;

  // Stop playing and clean
  res =  av_read_pause(pFormatCtx);
  if(res != 0){
    return false;
  pFormatCtx->streams[videoStream]->discard = AVDISCARD_ALL;


  return true;

int main(int argc, char *argv[])
  QCoreApplication a(argc, argv);
  bool ok = true;
  for(int i = 0; ok && i < 6; i++){
    ok = grab();
      qDebug() << i << "************************** - OK";
      qDebug() << i << "************************** - PB";
    qDebug() << "It works";
    qDebug() << "TEST FAILED !";
  return a.exec();

Results :
0 ************************** - OK 
1 ************************** - OK 
2 ************************** - PB 

Thank you for your help.


Ce courriel (de même que les fichiers joints) est strictement réservé à l'usage de la personne ou de l'entité à qui il est adressé et peut contenir de l'information privilégiée et confidentielle. Toute divulgation, distribution ou copie de ce courriel est strictement prohibée. Si vous avez reçu ce courriel par erreur, veuillez nous en aviser sur-le-champ, détruire toutes les copies et le supprimer de votre système informatique.
 This communication (including any files transmitted with it) is intended solely for the person or entity to whom it is addressed, and may contain confidential or privileged information. The disclosure, distribution or copying of this message is strictly forbidden. Should you have received this communication in error, kindly contact the sender promptly, destroy any copies and delete this message from your computer system.

More information about the libav-api mailing list