Skip to content

How to reconnect on disconnection? #51

@mridah

Description

@mridah

When the RTSP server is not running, it throws an error. Also when the stream breaks in between, I can't get it to auto reconnect.

Can you help me out in reconnecting logic? @Sebanisu

    public RtspStream(string url)
    {
        Console.WriteLine(ffmpeg.av_version_info());
        ffmpeg.avformat_network_init();

        AVDictionary* options = null;

        ffmpeg.av_dict_set(&options, "rtsp_transport", "tcp", 0);
        ffmpeg.av_dict_set(&options, "fflags", "nobuffer", 0);
        ffmpeg.av_dict_set(&options, "flags", "low_delay", 0);
        ffmpeg.av_dict_set(&options, "flush_packets", "1", 0);
        ffmpeg.av_dict_set(&options, "max_delay", "500000", 0); // microseconds (0.5 sec)
        ffmpeg.av_dict_set(&options, "analyzeduration", "1000000", 0);  // reduce analyze duration
        ffmpeg.av_dict_set(&options, "probesize", "2048", 0);           // small probe size
        ffmpeg.av_dict_set(&options, "stimeout", "3000000", 0);         // 3 seconds timeout

        AVFormatContext* tempFmtCtx = ffmpeg.avformat_alloc_context();
        if (ffmpeg.avformat_open_input(&tempFmtCtx, url, null, &options) != 0)
            throw new ApplicationException("Could not open stream");

        _fmtCtx = tempFmtCtx;

        if (ffmpeg.avformat_find_stream_info(_fmtCtx, null) != 0)
            throw new ApplicationException("Could not find stream info");

        for (int i = 0; i < _fmtCtx->nb_streams; i++)
        {
            if (_fmtCtx->streams[i]->codecpar->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
            {
                _videoStreamIndex = i;
                break;
            }
        }

        if (_videoStreamIndex == -1)
            throw new ApplicationException("Could not find video stream");

        Fps = ffmpeg.av_q2d(_fmtCtx->streams[_videoStreamIndex]->avg_frame_rate);
        if (Fps <= 1e-2)
            Fps = 25.0;

        AVCodec* codec = ffmpeg.avcodec_find_decoder(_fmtCtx->streams[_videoStreamIndex]->codecpar->codec_id);
        _codecCtx = ffmpeg.avcodec_alloc_context3(codec);
        ffmpeg.avcodec_parameters_to_context(_codecCtx, _fmtCtx->streams[_videoStreamIndex]->codecpar);
        if (ffmpeg.avcodec_open2(_codecCtx, codec, null) != 0)
            throw new ApplicationException("Could not open codec");

        Width = _codecCtx->width;
        Height = _codecCtx->height;

        _swsCtx = ffmpeg.sws_getContext(
            Width, Height, _codecCtx->pix_fmt,
            Width, Height, AVPixelFormat.AV_PIX_FMT_BGR24,
            ffmpeg.SWS_FAST_BILINEAR, null, null, null);

        _frame = ffmpeg.av_frame_alloc();
        _packet = ffmpeg.av_packet_alloc();

        _bgrBufferSize = Width * Height * 3;
        _bgrPtr = Marshal.AllocHGlobal(_bgrBufferSize);

        _dstData = new byte*[4];
        _dstLinesize = new int[4];

        ffmpeg.av_dict_free(&options); // clean up
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions