【问题标题】:How to stream video from PC to Windows Phone 8 mobile phone through internet如何通过互联网将视频从 PC 流式传输到 Windows Phone 8 手机
【发布时间】:2018-09-29 17:47:11
【问题描述】:

我开始创建一个应用程序,该应用程序大部分依赖于从 PC 到 WP8 手机的流式视频(少量视频流)。

  • 我必须选择以下视频格式之一:Motion JPEG,MPEG-4,H.264

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

  • 手机通过 Wi-Fi 连接到互联网。手机与PC服务器位于不同的wifi网络中。

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

PC-server 部分将用 C# 编写。

【问题讨论】:

    标签: c# security design-patterns architecture video-streaming


    【解决方案1】:

    基础设施

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


    媒体格式

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


    内容交付

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


    编辑

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

    下一部分是设置 IIS,您可以使用您的 IP 地址,或者购买一个域并将其 A 记录更改为指向您的 IP。

    然后,您可以创建一个小型数据库,其中包含一个名为 Videos 的表,列标记为 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 应用程序库下载媒体服务,您可以限制视频传输的比特率,以免占用您的全部带宽。

    【讨论】:

    • 我将只为少数用户流式传输。
    • 编辑为更适合您情况的内容。
    • 现在就足够了。你真的帮了我大忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 2016-11-18
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2016-02-08
    相关资源
    最近更新 更多