<p dir="ltr">Are you sure it gets stuck right there? Did you try to printf something behind if statement? Btw you are registering codecs twice with av_register_all and avcodec_ register_all, maybe thats giving you problems. </p>
<div class="gmail_quote">On Apr 5, 2016 8:26 AM, "Yu Ang Tan" <<a href="mailto:isoboy@gmail.com">isoboy@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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" target="_blank">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>
<br>_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
<br></blockquote></div>