[英]How to stream video from PC to Windows Phone 8 mobile phone through internet
我開始創建一個應用程序,該應用程序很大程度上依賴於從PC到WP8手機的流視頻(少量視頻流)。
我必須從以下一種視頻格式中進行選擇: Motion JPEG
, MPEG-4
, H.264
。
流應該以某種方式受到保護,這樣未經授權的人將很難接收或解碼它。
問題是:(1。)如何將上述格式的視頻從PC流傳輸到WP8電話;(2)如何合理地保護這種傳輸?
PC服務器部分將用C#編寫。
由於這是一個小項目,您可以使用IIS設置您的家用PC,並直接從您自己的家用服務器中傳輸內容。
在MP4文件中使用H.264編碼的視頻很重要,因為幾乎所有設備都支持該格式。
內容將通過HTTP流傳輸,並且可以在應用程序或瀏覽器中查看。
由於您將要實施的只是一個小型系統,因此您可以忽略很多安全性部分,而專注於首先獲取視頻流...您仍然需要一個數據庫,該數據庫與您存儲了哪些視頻以及它們位於何處有關您的硬盤驅動器,我的建議是將所有視頻存儲在一個位置,例如C:\\MP4\\
下一部分將是設置IIS,您可以使用IP地址,也可以購買域並更改其A記錄以指向您的IP。
然后,您可以使用名為“視頻”的表創建一個小型數據庫,具有標記為“ VideoID”和“ FilePath”的列,並用視頻填充數據庫。
數據庫完成后,您可以繼續編寫通用處理程序,該處理程序將處理視頻流。
創建一個名為Watch.ashx
的新.ashx文件,現在無論您希望觀看哪個視頻,都可以通過將videoid參數像這樣傳遞給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應用程序庫下載媒體服務,您可以限制視頻傳輸的比特率,以免占用整個帶寬。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.