title: Live broadcast of the video site with Asp.Net WebForms + WebApi + HTML5
body: <p>The problem is this:</p>
  
  <ol>
  <li>on the server have a video file;</li>
  <li>The administrator runs it on the play (video broadcast begins);</li>
  <li>user is connected to the server - must be given to the video stream that is currently playing. A live webcast in real time.</li>
  </ol>
  
  <p>To implement this task, I took as a basis for the article:
  <a href="http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/" rel="nofollow">http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/</a><br>
  It worked. To give the stream a video file independently and in parallel.
  I was looking on.</p>
  
  <p>Next it was necessary to solve the problem of broadcasting to multiple customers (paragraph 3 in the job). I took this article:
  <a href="http://gigi.nullneuron.net/gigilabs/streaming-data-with-asp-net-web-api-and-pushcontentstream/" rel="nofollow">http://gigi.nullneuron.net/gigilabs/streaming-data-with-asp-net-web-api-and-pushcontentstream/</a></p>
  
  <p>Since I have to give evidence in the video byte - I replaced the StreamWriter class to Stream.<br>
  It works for one of the first client.</p>
  
  <p>I made a website Asp.Net WebForms + WebApi + HTML5.
  Web page - to run a video manager and viewed by users.
  WebApi gives the player for <code>&lt;video&gt;</code> (HTML5) video stream.</p>
  
  <p>HTML5:</p>
  
  <pre><code>&lt;video&gt;
      &lt;source src="http://localhost:8080/SiteVideoStreaming/api/live/?filename=nameFile" /&gt;
  &lt;/video&gt;
  </code></pre>
  
  <p>WebApi controllers:</p>
  
  <pre><code>//Controllers
  public class LiveController : ApiController
  {
      private static ConcurrentBag&lt;Stream&gt; clients; // List of clients who need to simultaneously deliver video data
      static string fileName = "";
  
      static LiveController()
      {
          clients = new ConcurrentBag&lt;Stream&gt;();
          WriteToStream(); // The first call - start to play a video file
      }
  
      [HttpGet]
      public HttpResponseMessage Subscribe(string filename)
      {
          fileName = HostingEnvironment.MapPath("~/Videos/") + filename;
  
          var response = Request.CreateResponse();
          response.Content = new PushStreamContent((a, b, c) =&gt; { OnStreamAvailable(a, b, c); }, "video/mp4");
          return response;
      }
  
  
      private void OnStreamAvailable(Stream stream, HttpContent content, TransportContext context)
      {
          clients.Add(stream); // Add new client
      }
  
      //Class record a video file into a streams
      public async static void WriteToStream()
      {
          var buffer = new byte[65536];
  
          using (var video = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
          {
              var length = (int)video.Length;
              var bytesRead = 1;
  
              while (length &gt; 0 &amp;&amp; bytesRead &gt; 0)
              {
                  bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
  
                  foreach (var client in clients)// Each client in turn we return video data
                  {
                      try
                      {
                          await client.WriteAsync(buffer, 0, bytesRead); // ERROR - here !!! when you connect a second client
                          await client.FlushAsync();
                      }
                      catch (Exception ex)
                      {
                          Stream ignore;
                          clients.TryTake(out ignore);
                      }
                  }
  
                  length -= bytesRead;
              }
          }
      }
  }
  </code></pre>
  
  <p>If the request first came from one client - is given to video. Working.
  If the request from the second client - when you try to start to give him a stream error occurs.
  In this connection drops and the first client.</p>
  
  <p>The error is as follows:</p>
  
  <blockquote>
    <p>[System.Web.HttpException] = {"The remote host closed the connection.
    The error code is 0x800704CD."}</p>
  </blockquote>
  
  <p>As I understood after a search on the Internet is:</p>
  
  <blockquote>
    <p>0x800704CD "An operation was attempted on a nonexistent network
    connection."</p>
  </blockquote>
  
  <p>Tell me that I'm not doing right?
  Thank you.</p>