<div dir="ltr">Hi everyone.<div><div><br></div><div>I'm using libav for streaming to YouTube through RTMP. I have function openRtmpStream() for opening RTMP stream. It works well. But recently i noticed the situation, when RTMP url is wrong (or server is down). In this situation my code is trying to reconnect, and calls this function about 15 times per second. With htop i can see about 20 MB per second memory leak in this situation. </div><div><br></div><div>Maybe my code for closing context and streams is wrong? Or any another<span style="color:rgb(0,0,0);white-space:pre-wrap"> idea what I'm doing wrong? Thanks in advance!</span></div><div><br></div><div>Here's my code:</div><div><br></div><div><div>bool openRtmpStream( const std::string& address )</div><div>{</div><div>    if( ! m_httpContext ) {</div><div>        m_httpContext = avformat_alloc_context( );</div><div><br></div><div>        m_httpContext->oformat = av_guess_format( "flv", address.c_str( ), nullptr );</div><div>        if( m_httpContext->oformat ) {</div><div>            strcpy( m_httpContext->filename, address.c_str( ) );</div><div><br></div><div>            auto codecID = AV_CODEC_ID_H264;</div><div><br></div><div>            auto codec = avcodec_find_encoder( codecID );</div><div><br></div><div>            if( codec ) {</div><div>                m_httpVideoStream = avformat_new_stream( m_httpContext, codec );</div><div><br></div><div>                // ... here's initalization of m_httpVideoStream->codec ...</div><div><br></div><div>                int res = avcodec_open2( codecContext, codec, nullptr );</div><div>                if( res >= 0 ) {</div><div>                    auto codecID = AV_CODEC_ID_MP3;</div><div>                    auto codec = avcodec_find_encoder( codecID );</div><div>                    if( codec ) {</div><div>                        m_httpAudioStream = avformat_new_stream( m_httpContext, codec );</div><div><br></div><div>                        // ... here's initalization of m_httpAudioStream->codec ...</div><div><br></div><div>                        res = avcodec_open2( codecContext, codec, nullptr );</div><div>                        if( res >= 0 ) {</div><div>                            m_httpStreamWriteStartTime = boost::chrono::high_resolution_clock::now( );</div><div>                            if( avio_open2( &m_httpContext->pb, m_httpContext->filename, AVIO_FLAG_WRITE, m_AVIOInterruptCB.get( ), nullptr ) >= 0 ) {</div><div>                                if( avformat_write_header( m_httpContext, nullptr ) >= 0 ) {</div><div>                                    return true; // success</div><div>                                }</div><div>                            }</div><div>                            avcodec_close( m_httpAudioStream->codec );</div><div>                        }</div><div>                    }</div><div>                    avcodec_close( m_httpVideoStream->codec );</div><div>                }</div><div>            }</div><div>        }</div><div><br></div><div>        // failed to open stream, close context and streams</div><div><br></div><div>        avio_close( m_httpContext->pb );</div><div>        avformat_free_context( m_httpContext );</div><div>        m_httpContext = nullptr;</div><div>        m_httpVideoStream = nullptr;</div><div>        m_httpAudioStream = nullptr;</div><div>    }</div><div>    return false;</div><div>}</div><div><br></div><div>Thanks.</div>
</div></div></div>