<div dir="ltr"><div>I am trying to send an encoded stream with the code below, but it only works when there is another application listening at the provided URL. </div><div><br></div><div>If the listening application isn't ready, this program gets stuck in avformat_write_header () without returning. Is there any way for me to provide an interrupt callback, or timeout so that I can resume the program?</div><div><br></div><div>Thanks in advance.</div><div><br></div><div>/*******************************************************************************</div><div> * Video encoding example</div><div> *******************************************************************************/</div><div><br></div><div>int main(int argc, char** argv)</div><div>{</div><div>    AVCodec *codec = NULL;</div><div>    AVCodecContext *codecCtx= NULL;</div><div>    AVFormatContext *pFormatCtx = NULL;</div><div>    AVStream * pVideoStream = NULL;</div><div>    AVFrame *picture = NULL;</div><div><br></div><div>    int i, x, y,            //</div><div>        ret,                 // Return value</div><div>        got_packet_ptr;     // Data encoded into packet</div><div><br></div><div>    // Register all formats and codecs</div><div>    avcodec_register_all();</div><div>    av_register_all();</div><div>    avformat_network_init();</div><div>    </div><div>    char filename[100] ;</div><div>    sprintf_s(filename,sizeof(filename),"%s","rtsp://<a href="http://192.168.1.7:8554/live.sdp">192.168.1.7:8554/live.sdp</a>");</div><div>    if(argc>1)</div><div>    {</div><div>        sprintf_s(filename,sizeof(filename),"%s",argv[1]);</div><div>    }</div><div>    printf_s("URL: %s\n", filename);</div><div>    </div><div>    // allocate context</div><div>    ret = avformat_alloc_output_context2( &pFormatCtx, NULL, "rtsp", filename );</div><div>    if ( !pFormatCtx || ret < 0 )</div><div>    {</div><div>        fprintf(stderr,"Could not allocate output context" );</div><div>    }</div><div><br></div><div>    pFormatCtx->flags |= AVFMT_FLAG_NOBUFFER|AVFMT_FLAG_FLUSH_PACKETS;</div><div>    pFormatCtx->max_interleave_delta = 1;</div><div>    pFormatCtx->oformat->video_codec = AV_CODEC_ID_H264;</div><div>    </div><div>    // Find the codec.</div><div>    codec = avcodec_find_encoder(pFormatCtx->oformat->video_codec);</div><div>    if (codec == NULL) {</div><div>        fprintf(stderr, "Codec not found\n");</div><div>        return -1;</div><div>    }</div><div><br></div><div>    // Add stream to pFormatCtx</div><div>    pVideoStream = avformat_new_stream(pFormatCtx, codec);</div><div>    if (!pVideoStream)</div><div>    {</div><div>        fprintf(stderr, "Cannot add new video stream\n");</div><div>        return -1;</div><div>    }</div><div>    </div><div>    int framerate = 10;</div><div>    pVideoStream->id = pFormatCtx->nb_streams-1;</div><div>    pVideoStream->time_base.den = framerate;</div><div>    pVideoStream->time_base.num = 1;</div><div>    </div><div>    // Set context</div><div>    codecCtx = pVideoStream->codec;</div><div>    codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;</div><div>    codecCtx->profile = FF_PROFILE_H264_BASELINE;</div><div>    // Resolution must be a multiple of two.</div><div>    codecCtx->width  = 320;</div><div>    codecCtx->height = 240;</div><div><br></div><div>    codecCtx->bit_rate = 1000000;</div><div>    codecCtx->time_base.den = framerate;</div><div>    codecCtx->time_base.num = 1;</div><div>    codecCtx->gop_size = 12; // emit one intra frame every twelve frames at most</div><div><br></div><div>    if (pFormatCtx->oformat->flags & AVFMT_GLOBALHEADER)</div><div>        codecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;</div><div><br></div><div>    // Open the codec.</div><div>    if (avcodec_open2(codecCtx, codec, NULL) < 0)</div><div>    {</div><div>        fprintf(stderr, "Cannot open video codec\n");</div><div>        return -1;</div><div>    }</div><div><br></div><div>    ret = avio_open2(&pFormatCtx->pb, filename, AVIO_FLAG_WRITE, NULL, NULL);</div><div>    if (ret < 0)</div><div>    {</div><div>        // Error "Protocol not found"</div><div>        av_strerror(ret,errbuf, ERRBUFFLEN);</div><div>        fprintf(stderr, "avio_open2() fail: %s\n", errbuf);</div><div>        //return -1;</div><div>    }</div><div><br></div><div>    // Write file header. (Gets stuck here)</div><div>    ret = avformat_write_header(pFormatCtx, NULL);</div><div>    if ( ret < 0 )</div><div>    {</div><div>        fprintf(stderr, "error writing header");</div><div>        return -1;</div><div>    }</div><div><br></div><div>    // ...more code</div><div>}</div></div><div dir="ltr">-- <br></div><div dir="ltr">Ang</div>