简体   繁体   中英

Streaming live video from my c# application to ASP.NET webpage

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.

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:

public class VideosController : ApiController
{
    /// <summary>
    /// Web API method to paste into videocontainer in "video" tag on web page
    /// </summary>
    /// <param name="filename">File name with location path</param>
    /// <param name="ext">Videofile extension</param>
    /// <returns>Return stream content</returns>
    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;
    }
}

and video.WrtieToStream method:

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();
    }
}

I debugged the code and it seemed to be working correct, ie it receives incoming video stream and writes it to output stream, but I always get black screen instead of video being streamed.

When I change WriteToStream method to read video from local file (saved earlier from same scene), not from network, then video is working well.

What am I doing wrong? Or may be there is other solution to get live video from video server to a web client?

There might be an infinite loop. I do not know the NetStream into detail, but for me, most of the streams must not throw an exception, when end of file is reached. If I am not missing something, there could possibly happen a situation, where you do never reach outputStream.Close();

while (true)
{
    bytesRead = netStream.Read(buffer, 0, buffer.Length);
    outputStream.Write(buffer, 0, bytesRead);
}

I would change it to

do
{
    bytesRead = netStream.Read(buffer, 0, buffer.Length);
    outputStream.Write(buffer, 0, bytesRead);
}
while (bytesRead > 0)

Which might not help you, but at least, the loop body just seems more natural to me.

To the concept: If your application has state on server side, I would just have a buffer of bytes on both sides, the client probably having two buffers, one, from which he is playing and other, he saves data for future playing, first buffer would initially be filled full and when playing the video, he would ask for new data from the server (this could happen asynchronously) and save them to the second buffer, whereas on the server, only certain part of the file would be held in memory.

使用 nginx 和 rtmp 模块,通过 Ffmpeg 将视频数据写入 rtmp 流,然后使用 rtmp 播放器在网页中播放视频

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM