本文介绍了如何通过Internet将视频从PC流传输到Windows Phone 8手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始创建一个应用程序,该应用程序很大程度上依赖于从PC到WP8手机的流视频(少量视频流).

I am starting to create an application of which large part relies on streaming Video(few video streams) from a PC to a WP8 mobile phone.

  • 我必须从以下视频格式之一中进行选择:Motion JPEGMPEG-4H.264.

应该以某种方式保护流,以便未经授权的人员将很难接收或解码该流.

The stream should be somehow protected so unauthorized person would have hard time to receive or maybe decode it.

问题是:(1.)如何将上述格式的视频从PC流传输到WP8电话;(2)如何合理地保护这种传输?

The question is: (1.)How to stream video of above named formats from a PC to WP8 phone and (2)how to reasonably secure this transmission?

PC服务器部分将用C#编写.

The PC-server part will be written in C#.

推荐答案

基础结构

由于这是一个小项目,您可以使用IIS设置家用PC并直接从您自己的家用服务器中流式传输内容.

Infrastructure

Since it's a small project you can set up your home PC with IIS and stream the content directly from your own home server.

在MP4文件中使用H.264编码的视频很重要,因为几乎所有设备都支持这种格式.

It's important that you use H.264-encoded videos in MP4 files because it's THE format which is supported on almost every device out there.

内容将通过HTTP流传输,并且可以在应用程序或浏览器中查看.

Content will be streamed over HTTP and can be viewed in app or in browser.

由于您将要实施的只是一个小型系统,因此您可以忽略很多安全性部分,而专注于首先获取视频流...您仍将需要一个数据库,该数据库涉及您存储了哪些视频以及它们在哪里存储位于您的硬盘驱动器上,我的建议是将所有视频存储在一个位置,例如C:\MP4\

Since it's just a small scale system you will be implementing, you can disregard a lot of the security part and concentrate on getting the video streaming first... You will still need a database concerning what videos you have stored and where they are located on your hard drive, my suggestion would be to store all the videos in one location such as C:\MP4\

下一步是设置IIS,您可以使用IP地址,也可以购买域并更改其A记录以指向您的IP.

Next part would be to setup IIS, you can either use your IP Address, or buy a domain and change its A Records to point to your IP.

然后,您可以使用名为视频"的表创建一个小型数据库,具有标记为VideoID和FilePath的列,并用视频填充数据库.

You can then create a small database with a table named Videos, have columns labeled VideoID and FilePath and fill the database with your videos.

完成数据库后,您可以继续编写通用处理程序,该处理程序将处理视频流.

After the database is done, you can proceed to writing a generic handler which will handle the streaming of the video.

创建一个名为Watch.ashx的新.ashx文件,现在无论您希望观看哪个视频,都可以通过将videoid参数像这样传递给Watch.ashx来实现.

Create a new .ashx file called Watch.ashx, now whichever video you wish to watch, you'll soon be able to do so by passing the videoid parameter to Watch.ashx like this...

http://127.0.0.1/Watch.ashx?v=VIDEOID

为了让您入门,我在下面列出了整个课程.

I've included the entire class below to get you started.

<%@ 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的工作改编了上面的类,原始文件可以在这里找到:

I adapted the class above from cipto0382's work, the original can be found here:

http://blogs.visigo.com/chriscoulson/easy-handling-of-http-range-requests-in-asp-net/

在分开的笔记上,为了节省带宽,我强烈建议您从IIS应用程序库下载媒体服务,您可以限制视频传输的比特率,以免占用整个带宽.

On parting notes, to save on bandwidth, I highly recommend that you download Media Services from the IIS Appplication Gallery, you can throttle the bitrate that videos are transmitted so that it doesn't eat up your entire bandwidth.

这篇关于如何通过Internet将视频从PC流传输到Windows Phone 8手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 19:31