我开始创建一个应用程序,其中很大一部分依赖于从PC到WP8手机的流媒体视频(很少有视频流)。
Motion JPEG、MPEG-4、H.264。问题是:(1)如何将上述指定格式的视频从PC机传输到WP8电话;(2)如何合理地保护这种传输?
PC服务器部分将用C#编写.
发布于 2013-12-05 19:57:02
基础设施
因为这是一个小项目,您可以使用IIS设置您的家庭PC,并直接从您自己的家庭服务器上流内容。
媒体格式
在MP4文件中使用H.264编码的视频是很重要的,因为它是几乎所有设备都支持的格式。
内容传递
内容将通过HTTP流,可以在应用程序或浏览器中查看。
编辑
由于它只是一个小规模的系统,您将实现,您可以忽略许多安全部分,并集中精力,首先获得视频流.您仍然需要一个数据库,说明您存储了哪些视频以及它们位于硬盘驱动器上的位置,我的建议是将所有视频存储在一个位置,如C:\MP4\。
下一步是设置IIS,您可以使用IP地址,也可以购买域并将其A记录更改为指向您的IP。
然后,您可以使用一个名为视频的表创建一个小型数据库,有标记为VideoID和FilePath的列,并将视频填充到数据库中。
数据库完成后,您可以继续编写一个通用处理程序,它将处理视频流。
创建一个名为.ashx的新的Watch.ashx文件,不管你想看什么视频,你很快就可以通过像这样把视频参数传递给Watch.ashx .
http://127.0.0.1/Watch.ashx?v=VIDEOID
为了让你开始,我把下面的全班都包括进来了。
<%@ WebHandler Language="C#" Class="Watch" %>
using System;
using System.Globalization;
using System.IO;
using System.Web;
public class Watch : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
// Get the VideoID from the requests `v` parameters.
var videoId = context.Request.QueryString["v"];
/* With the videoId you'll need to retrieve the filepath
/ from the database. You'll need to replace the method
/ below with your own depending on whichever
/ DBMS you decide to work with.
*////////////////////////////////////////////////////////
var videoPath = DataBase.GetVideoPath(videoId);
// This method will stream the video.
this.RangeDownload(videoPath, context);
}
private void RangeDownload(string fullpath, HttpContext context)
{
long size;
long start;
long theend;
long length;
long fp = 0;
using (StreamReader reader = new StreamReader(fullpath))
{
size = reader.BaseStream.Length;
start = 0;
theend = size - 1;
length = size;
context.Response.AddHeader("Accept-Ranges", "0-" + size);
if (!string.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
{
long anotherStart;
long anotherEnd = theend;
string[] arrSplit = context.Request.ServerVariables["HTTP_RANGE"].Split('=');
string range = arrSplit[1];
if ((range.IndexOf(",", StringComparison.Ordinal) > -1))
{
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + theend + "/" + size);
throw new HttpException(416, "Requested Range Not Satisfiable");
}
if ((range.StartsWith("-")))
{
anotherStart = size - Convert.ToInt64(range.Substring(1));
}
else
{
arrSplit = range.Split('-');
anotherStart = Convert.ToInt64(arrSplit[0]);
long temp;
if ((arrSplit.Length > 1 && Int64.TryParse(arrSplit[1], out temp)))
{
anotherEnd = Convert.ToInt64(arrSplit[1]);
}
else
{
anotherEnd = size;
}
}
anotherEnd = (anotherEnd > theend) ? theend : anotherEnd;
if ((anotherStart > anotherEnd | anotherStart > size - 1 | anotherEnd >= size))
{
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + theend + "/" + size);
throw new HttpException(416, "Requested Range Not Satisfiable");
}
start = anotherStart;
theend = anotherEnd;
length = theend - start + 1;
fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
context.Response.StatusCode = 206;
}
}
context.Response.ContentType = "video/mp4";
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + theend + "/" + size);
context.Response.AddHeader("Content-Length", length.ToString(CultureInfo.InvariantCulture));
context.Response.TransmitFile(fullpath, fp, length);
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}我从cipto0382's的工作中改编了上面的这个类,原始的可以在这里找到:
http://blogs.visigo.com/chriscoulson/easy-handling-of-http-range-requests-in-asp-net/
在临别说明中,为了节省带宽,我强烈建议您从IIS应用程序库下载Media Services,您可以控制视频传输的比特率,这样它就不会耗尽您的全部带宽。
https://stackoverflow.com/questions/20317270
复制相似问题