title: How to initialize HLS stream?
body: <p><S> My IP Camera push stream only when somebody request. So there is no ts segment or m3u8 file when a client requests stream. Is there a way to tell the client to retry the same request a second later, so that the server has time to send a command, wait for streaming video and generate a m3u8 file.</p>
  
  <p>I don't want to hold the connection to the client while waiting for the first m3u8 file because it may cause too many connections which the server can't handle.</S></p>
  
  <p><strong>Edit:</strong><br/>
  The server running Nginx(actually OpenResty) receives audio/video data from an IP Camera, then do transcoding with ffmpeg library and finally publish via HLS protocol.</p>
  
  <p><strong>The problem:</strong>
  The IP Camera does not push stream all the time. It pushes only when client requests. It takes several seconds for the server to receive media stream and generate the first m3u8 file after the client (HLS player such as ffplay or video.js with videojs-contrib-hls plugin) requests. So the client will get a 404 and fail to open the HLS stream.<br/></p>
  
  <p><strong>What I tried:</strong>
  I don't want to hold the connection to the client while waiting for the first m3u8 file because it may cause too many connections which the server can't handle.<br/>
  The client can check if the stream is ready by adding custom code. But I want to know if there is a better way first.</p>
  
  <p><strong>Question:</strong>
  How can the server Initial the HLS stream and avoid the problem?<br/>
  Is there a way to generate a fake m3u8 file to tell the standard HLS players to retry again automatically?<br/>
  Or is there an way to configure the HLS player so that it will retry automatically when it get 404 from server. Any players that can run in browser is acceptable, such as <a href="https://github.com/mangui/flashls" rel="nofollow">flowplayer with flashls</a>, <a href="https://github.com/videojs/videojs-contrib-hls" rel="nofollow">video.js with videojs-contrib-hls</a>. </p>
  
  <p><strong>Edit 2:</strong><br/></p>
  
  <p><strong>How does the camera start:</strong><br/>
  My system has a control module to communicate with the camera. It will control the camera to start or stop pushing media. I'm sure the camera will start only after a client request. So it doesn't matter how the camera starts<br/></p>
  
  <p><strong>About Nginx in my system:</strong><br/>
  The camera POST raw media (alaw audio and h264 video) to OpenResty server (which based on Nginx),  with timestamp in custom HTTP header. Every request post media of two seconds. The server take the raw media and transcode it to mpeg-ts format and also m3u8 file as playlist of HLS protocol. The transcoding is done with ffmpeg library.</p>
  
  <p><strong>Player:</strong><br/></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;meta name="robots" content="noindex"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset=utf-8 /&gt;
  &lt;title&gt;videojs-contrib-hls embed&lt;/title&gt;
  
    &lt;link href="https://cdnjs.cloudflare.com/ajax/libs/video.js/5.10.2/alt/video-js-cdn.css" rel="stylesheet"&gt;
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/video.js/5.10.2/video.js"&gt;&lt;/script&gt;
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/3.0.2/videojs-contrib-hls.js"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
    &lt;video id="my_video_1" class="video-js vjs-default-skin" controls preload="auto" width="640" height="268" 
    data-setup='{}'&gt;
      &lt;source src="http://xxx.xxx.xxx.xxx/camera.m3u8" type="application/x-mpegURL"&gt;
    &lt;/video&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>