天天看点

ASP.NET WEBAPI异步视频流实现

要执行异步流任务,可以使用PushStreamContent。它允许服务器逐步推到接收客户端的数据包。

本示例中,从服务器的硬盘上的文件读取视频流并刷新到客户端 (使用PushStreamContent) 65536 字节的数据包。流媒体视频播放然后可以立即开始 (客户端不需要等待整个视频来冲进),而服务器异步写入客户端,这样不会造成不必要的负载。一旦客户端断开连接,将停止写作。

public PushStreamContent(Action<Stream, HttpContent, TransportContext> onStreamAvailable); 可以参考MSND相关说明

服务器以WEB API 来构建

1.注册路由

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;


namespace WebApplication2
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.MapHttpRoute(
               "DefaultVideo",
                "api/{controller}/{ext}/{filename}"
            );

        }
    }
}
           

2.实现异步发送

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;

namespace WebApplication2
{
    public class VideoStream
    {
        private readonly string _filename;

        public VideoStream(string filename, string ext)
        {
            _filename = @"D:\Downloads\" + filename + "." + ext;
        }
        public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
        {
            try
            {
                var buffer = new byte[65536];

                using (var video = File.Open(_filename, FileMode.Open, FileAccess.Read))
                {
                    var length = (int)video.Length;
                    var bytesRead = 1;

                    while (length > 0 && bytesRead > 0)
                    {
                        bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
                        await outputStream.WriteAsync(buffer, 0, bytesRead);
                        length -= bytesRead;
                    }
                }
            }
            catch (HttpException ex)
            {
                return;
            }
            finally
            {
                outputStream.Close();
            }
        }
    }

}
           

3.实现WEBAPI接口

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;

namespace WebApplication2
{
    public class VideosController : ApiController
    {
        public HttpResponseMessage Get(string filename, string ext)
        {
            var video = new VideoStream(filename, ext);
            Action<Stream , HttpContent , TransportContext> send=video.WriteToStream;
            var response = Request.CreateResponse();
            response.Content = new System.Net.Http.PushStreamContent(send,new MediaTypeHeaderValue("video/" + ext));
//调用异步数据推送接口
            return response;
        }
    }
}
           

4.实现一个HTML5页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <video width="480" height="320" controls="controls" autoplay="autoplay">
        <source src="/api/Videos/MP4/2" type="video/mp4">
    </video>
</body>

</html>