Esempio n. 1
0
 private void ProcessM3u8(HttpListenerContext context, JT1078AVInfo jT1078AVInfo)
 {
     if (authorization.Authorization(context, out IPrincipal principal))
     {
         hLSRequestManager.HandleHlsRequest(context, principal, jT1078AVInfo);
     }
 }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="jT1078AVInfo"></param>
        /// <returns></returns>
        public static bool TryGetAVInfo(this HttpListenerContext context, out JT1078AVInfo jT1078AVInfo)
        {
            if (context.Request.QueryString.Count < 2)
            {
                jT1078AVInfo = default;
                return(false);
            }
            string sim     = context.Request.QueryString.Get("sim");
            string channel = context.Request.QueryString.Get("channel");

            if (string.IsNullOrEmpty(sim) || string.IsNullOrEmpty(channel))
            {
                jT1078AVInfo = default;
                return(false);
            }
            int.TryParse(channel, out int channelNo);
            jT1078AVInfo = new JT1078AVInfo(sim, channelNo);
            return(true);
        }
Esempio n. 3
0
 private void ProcessTs(HttpListenerContext context, JT1078AVInfo jT1078AVInfo)
 {
     //ts 无需验证
     hLSRequestManager.HandleHlsRequest(context, default, jT1078AVInfo);
        /// <summary>
        /// 处理hls实时视频请求
        /// </summary>
        /// <param name="context"></param>
        /// <param name="principal"></param>
        /// <param name="jT1078AVInfo"></param>
        public async void HandleHlsRequest(HttpListenerContext context, IPrincipal principal, JT1078AVInfo jT1078AVInfo)
        {
            string filename = Path.GetFileName(context.Request.Url.AbsolutePath.ToString());
            string filepath = Path.Combine(Configuration.HlsRootDirectory, jT1078AVInfo.ToString(), filename);

            if (hLSPathStorage.ExsitPath(filepath))
            {
                try
                {
                    using (FileStream sr = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        if (filename.Contains("m3u8"))
                        {
                            await context.HttpM3U8Async(sr);
                        }
                        else if (filename.Contains("ts"))
                        {
                            await context.HttpTsAsync(sr);
                        }
                        else
                        {
                            context.Http404();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, ex.Message);
                    context.Http404();
                }
            }
            else
            {
                if (!File.Exists(filepath))
                {
                    if (filename.ToLower().Contains("m3u8"))
                    {
                        var directory = Path.Combine(Configuration.HlsRootDirectory, jT1078AVInfo.ToString());
                        if (!Directory.Exists(directory))
                        {
                            Directory.CreateDirectory(directory);
                        }
                        if (!hLSPathStorage.ExistFileSystemWatcher(directory))
                        {
                            var fileSystemWatcher = new FileSystemWatcher();
                            fileSystemWatcher.Path         = directory;
                            fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite; //NotifyFilters.CreateTime
                            fileSystemWatcher.Filter       = "*.m3u8";                // Only watch text files.
                            fileSystemWatcher.Changed     += async(sender, arg) =>
                            {
                                if (context.Response.ContentLength64 != 0)
                                {
                                    return;
                                }
                                //wwwroot\1234_2\live.m3u8
                                //var key = arg.FullPath.Replace(arg.Name, "").Substring(arg.FullPath.Replace(arg.Name, "").IndexOf("\\")).Replace("\\", "");
                                var key     = arg.FullPath.Substring(arg.FullPath.IndexOf("\\") + 1, (arg.FullPath.LastIndexOf("\\") - arg.FullPath.IndexOf("\\")) - 1);
                                var sim     = key.Split("_")[0];
                                var channel = int.Parse(key.Split("_")[1]);
                                try
                                {
                                    using (FileStream sr = new FileStream(arg.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                                    {
                                        hLSPathStorage.AddPath(arg.FullPath, key);
                                        await context.HttpM3U8Async(sr);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogError(ex, $"{context.Request.Url}");
                                    context.Http404();
                                }
                                finally
                                {
                                    hLSPathStorage.DeleteFileSystemWatcher(directory);
                                }
                            };
                            fileSystemWatcher.EnableRaisingEvents = true;         // Begin watching.
                            hLSPathStorage.AddFileSystemWatcher(directory, fileSystemWatcher);
                        }
                    }
                    else
                    {
                        context.Http404();
                        return;
                    }
                }
                else
                {
                    hLSPathStorage.AddPath(filepath, jT1078AVInfo.ToString());
                    using (FileStream sr = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        if (filename.Contains("m3u8"))
                        {
                            await context.HttpM3U8Async(sr);
                        }
                        else if (filename.Contains("ts"))
                        {
                            await context.HttpTsAsync(sr);
                        }
                        else
                        {
                            context.Http404();
                        }
                    }
                }
                var jT1078HttpContext = new JT1078HttpContext(context, principal);
                jT1078HttpContext.Sim          = jT1078AVInfo.Sim;
                jT1078HttpContext.ChannelNo    = jT1078AVInfo.ChannelNo;
                jT1078HttpContext.RTPVideoType = RTPVideoType.Http_Hls;
                HttpSessionManager.AddOrUpdateHlsSession(jT1078HttpContext);
            }
        }