title: Streaming live video from my c# application to ASP.NET webpage
body: <p>I have a project that requires sending live video stream to a web-client. I have a server app that actually makes live video stream out of Direct3D scene using DirectShow. I have my own DirectShow source filter and its output is compressed using WEBM codec. It works well when saving video stream to a file, that can later be viewed without any problems.</p>
  
  <p>But next step is to translate live video stream to a web client. I tryied to send it via TCP to my ASP.NET MVC4 application. My page controller looks as folows:</p>
  
  <pre><code>public class VideosController : ApiController
  {
      /// &lt;summary&gt;
      /// Web API method to paste into videocontainer in "video" tag on web page
      /// &lt;/summary&gt;
      /// &lt;param name="filename"&gt;File name with location path&lt;/param&gt;
      /// &lt;param name="ext"&gt;Videofile extension&lt;/param&gt;
      /// &lt;returns&gt;Return stream content&lt;/returns&gt;
      public HttpResponseMessage Get(string filename, string ext)
      {
          var video = new VideoStream();
  
          var response = Request.CreateResponse();
          response.Content = new PushStreamContent(video.WriteToStream, new MediaTypeHeaderValue("video/" + ext));
  
          return response;
      }
  }
  </code></pre>
  
  <p>and video.WrtieToStream method:</p>
  
  <pre><code>public void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
  {
      try
      {
          var buffer = new byte[65536];
  
          TcpClient serverSocket;
          serverSocket = new TcpClient("localHost", 333);
          NetworkStream netStream = serverSocket.GetStream();
  
          StreamReader streamReader = new StreamReader(netStream);
          try
          {
              var bytesRead = 0;
              while (true)
              {
                  bytesRead = netStream.Read(buffer, 0, buffer.Length);
                  outputStream.Write(buffer, 0, bytesRead);
              }
          }
          catch (EndOfStreamException)
          {
              Console.WriteLine("End of file transaction");
          }
          catch (Exception e)
          {
              Console.WriteLine(e.Message);
          }
      }
      catch (HttpException ex)
      {
          return;
      }
      finally
      {
          outputStream.Close();
      }
  }
  </code></pre>
  
  <p>I debugged the code and it seemed to be working correct, i.e. it receives incoming video stream and writes it to output stream, but I always get black screen instead of video being streamed.</p>
  
  <p>When I change WriteToStream method to read video from local file (saved earlier from same scene), not from network, then video is working well. </p>
  
  <p>What am I doing wrong? Or may be there is other solution to get live video from video server to a web client?</p>