title: Streaming H.264 over UDP using FFmpeg, and "dimensions not set" error
body: <p>I'm trying to stream H.264 over UDP with no luck so far. Here is a minimal code that you can reproduce the problem.</p>
  
  <p>To compile,</p>
  
  <pre><code>g++ -o test -lavcodec -lavformat -lavutil test.cpp
  </code></pre>
  
  <p>Extra information, I start <code>ffplay</code> as follows. Currently it's of no use.</p>
  
  <pre><code>ffplay -i udp://127.0.0.1:8554/live.sdp
  </code></pre>
  
  <p>Output of my code (see <code>avio_open()</code> call),</p>
  
  <pre><code>[libx264 @ 0x6a26c0] using mv_range_thread = 24
  [libx264 @ 0x6a26c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.1 Cache64
  [libx264 @ 0x6a26c0] profile High, level 3.1
  Output #0, h264, to 'udp://127.0.0.1:8554/live.sdp':
      Stream #0:0, 0, 0/0: Video: h264 (libx264), -1 reference frame, none, q=-1--1
  [h264 @ 0x6a2020] dimensions not set
  Cannot write header to stream: Success
  </code></pre>
  
  <p>And the code,</p>
  
  <pre><code>extern "C" {
      #include &lt;libavcodec/avcodec.h&gt;
      #include &lt;libavformat/avformat.h&gt;
      #include &lt;libavutil/avutil.h&gt;
  }
  
  #include &lt;iostream&gt;
  using namespace std;
  
  int main() {
      AVCodecContext* m_codecContext;
      AVCodec* m_codec;
      AVFormatContext* m_formatContext;
      AVStream* m_stream;
  
      unsigned m_outWidth = 768;
      unsigned m_outHeight = 608;
  
      av_register_all();
      avcodec_register_all();
      avformat_network_init();
  
      int errorStatus = 0;
      char errorLog[128] = { 0 };
      av_log_set_level(AV_LOG_TRACE);
  
      string m_output("udp://127.0.0.1:8554/live.sdp");
  
      if (avformat_alloc_output_context2(&amp;m_formatContext, NULL, "h264", m_output.c_str()) &lt; 0) {
          cerr &lt;&lt; "Cannot allocate output context: "
               &lt;&lt; av_make_error_string(errorLog, 128, errorStatus) &lt;&lt; endl;
          return -1;
      }
  
      AVOutputFormat *m_outputFormat = m_formatContext-&gt;oformat;
  
      m_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
      if (!m_codec) {
          cerr &lt;&lt; "Cannot find an encoder: "
               &lt;&lt; av_make_error_string(errorLog, 128, errorStatus) &lt;&lt; endl;
          return -1;
      }
  
      m_codecContext = avcodec_alloc_context3(m_codec);
      if (!m_codecContext) {
          cerr &lt;&lt; "Cannot allocate a codec context: "
               &lt;&lt; av_make_error_string(errorLog, 128, errorStatus) &lt;&lt; endl;
          return -1;
      }
  
      m_codecContext-&gt;pix_fmt = AV_PIX_FMT_YUV420P;
      m_codecContext-&gt;width = m_outWidth;
      m_codecContext-&gt;height = m_outHeight;
  
      if (avcodec_open2(m_codecContext, m_codec, NULL) &lt; 0) {
          cerr &lt;&lt; "Cannot open codec: "
               &lt;&lt; av_make_error_string(errorLog, 128, errorStatus) &lt;&lt; endl;
          return -1;
      }
  
      m_stream = avformat_new_stream(m_formatContext, m_codec);
      if (!m_stream) {
          cerr &lt;&lt; "Cannot create a new stream: "
               &lt;&lt; av_make_error_string(errorLog, 128, errorStatus) &lt;&lt; endl;
          return -1;
      }
  
      av_dump_format(m_formatContext, 0, m_output.c_str(), 1);
  
      if ((errorStatus = avio_open(&amp;m_formatContext-&gt;pb, m_output.c_str(), AVIO_FLAG_WRITE)) &lt; 0) {
          cerr &lt;&lt; "Cannot open output: "
               &lt;&lt; av_make_error_string(errorLog, 128, errorStatus) &lt;&lt; endl;
          return -1;
      }
  
      if (avformat_write_header(m_formatContext, NULL) &lt; 0) {
          cerr &lt;&lt; "Cannot write header to stream: "
               &lt;&lt; av_make_error_string(errorLog, 128, errorStatus) &lt;&lt; endl;
          return -1;
      }
  
      cout &lt;&lt; "All done." &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>For those who has even more time to spare on my problem, when I change <code>m_output</code> to <code>rtsp://127.0.0.1:8554/live.sdp</code>, and <code>ffplay</code> command to <code>ffplay -rtsp_flags listen -i rtsp://127.0.0.1:8554/live.sdp</code> I get the error,</p>
  
  <pre><code>[libx264 @ 0x1e056c0] using mv_range_thread = 24
  [libx264 @ 0x1e056c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.1 Cache64
  [libx264 @ 0x1e056c0] profile High, level 3.1
  Output #0, h264, to 'rtsp://127.0.0.1:8554/live.sdp':
      Stream #0:0, 0, 0/0: Video: h264 (libx264), -1 reference frame, none, q=-1--1
  Cannot open output: Protocol not found
  </code></pre>
  
  <p>Am I naive to expect that streaming protocol will be changed like this?</p>