Beispiel #1
0
        private void SendResource(HttpRequest request, string mime, bool encode = false)
        {
            HttpResponse response = request.GetResponse();

            string name = request.Url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Last();

            using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HomeMediaCenter.Resources." + name))
            {
                if (encode)
                {
                    EncoderBuilder builder = EncoderBuilder.GetEncoder(request.UrlParams);

                    using (MemoryStream memStream = new MemoryStream())
                    {
                        builder.StartEncode(resourceStream, memStream);

                        response.AddHreader(HttpHeader.ContentLength, memStream.Length.ToString());
                        response.AddHreader(HttpHeader.ContentType, builder.GetMime());
                        response.SendHeaders();

                        if (request.Method == "GET")
                        {
                            memStream.Position = 0;
                            memStream.CopyTo(response.GetStream());
                        }
                    }
                }
                else
                {
                    response.AddHreader(HttpHeader.ContentLength, resourceStream.Length.ToString());
                    response.AddHreader(HttpHeader.ContentType, mime);
                    response.SendHeaders();

                    if (request.Method == "GET")
                    {
                        resourceStream.CopyTo(response.GetStream());
                    }
                }
            }
        }
Beispiel #2
0
        private void GetEncode(HttpRequest request)
        {
            uint index;

            string[] values = request.GetFileName().Split(new char[] { '_' }, 2);
            if (values.Length < 2 || !uint.TryParse(values[0], out index))
            {
                throw new HttpException(404, "HLS file not found");
            }

            string sessionId;
            int    extIndex = values[1].ToLower().LastIndexOf(".ts");

            if (extIndex < 1)
            {
                throw new HttpException(404, "Unknown session id");
            }
            sessionId = values[1].Substring(0, extIndex);

            HlsItem item;

            this.itemsLock.EnterReadLock();
            try
            {
                if (!this.items.ContainsKey(sessionId))
                {
                    throw new HttpException(404, "Session id not found");
                }
                item = this.items[sessionId];
            }
            finally
            {
                this.itemsLock.ExitReadLock();
            }

            HttpResponse response = request.GetResponse();

            response.AddHreader(HttpHeader.ContentType, "video/MP2T");

            if (index == 0)
            {
                //Poslanie nulteho suboru so statickym obrazom, je pripraveny hned na stiahnutie
                //Jeho dlzka je rovnaka ako dlzka segmentu
                response.SendHeaders();

                Dictionary <string, string> parameters = item.GetParamCopy();
                parameters.Remove("starttime");
                parameters.Remove("subtitles");
                parameters.Remove("id");
                parameters["source"]  = "static";
                parameters["codec"]   = "mpeg2_ts_h264";
                parameters["endtime"] = parameters["hlsSegmentTime"];

                EncoderBuilder encoder = EncoderBuilder.GetEncoder(parameters);
                encoder.StartEncode(response.GetStream());
            }
            else
            {
                //Posielanie segmentovanych suborov pozadovaneho zdroja
                byte[] fileData = item.GetFileData(index);

                response.AddHreader(HttpHeader.ContentLength, fileData.Length.ToString());
                response.SendHeaders();
                response.GetStream().Write(fileData, 0, fileData.Length);
            }
        }
Beispiel #3
0
        private void GetEncode(HttpRequest request, string path, string subtitlesPath, string encodeFeature, TimeSpan?duration)
        {
            HttpResponse response = request.GetResponse();

            request.UrlParams["source"] = path;
            if (subtitlesPath != null)
            {
                request.UrlParams["subtitles"] = subtitlesPath;
            }
            if (encodeFeature != null)
            {
                response.AddHreader("ContentFeatures.dlna.org", encodeFeature);
            }

            if (duration.HasValue)
            {
                //Je mozne seekovanie pomocou casu

                double startTimeParam = 0;
                if (request.UrlParams.ContainsKey("starttime") && double.TryParse(request.UrlParams["starttime"],
                                                                                  System.Globalization.NumberStyles.Float | System.Globalization.NumberStyles.AllowThousands,
                                                                                  System.Globalization.CultureInfo.InvariantCulture, out startTimeParam))
                {
                    //Ak je nastaveny url parameter pozicie - treba upravit dlzku
                    duration -= TimeSpan.FromSeconds(startTimeParam);
                }

                response.AddHreader("X-AvailableSeekRange", string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                                          "1 npt=0.0-{0:0.000}", duration.Value.TotalSeconds));

                if (request.Headers.ContainsKey("TimeSeekRange.dlna.org"))
                {
                    //Zistenie a nastavenie pozicie
                    string[] range = request.Headers["TimeSeekRange.dlna.org"].Split('=').Last().Split('-').Select(a => a.Trim()).ToArray();
                    double   end, start;

                    //Cas moze byt vo formate 0.000 sekundy alebo 0:00:00.0
                    if (!double.TryParse(range[0], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out start))
                    {
                        start = TimeSpan.Parse(range[0]).TotalSeconds;
                    }
                    if (start < 0)
                    {
                        start += duration.Value.TotalSeconds;
                    }

                    if (range.Length < 2)
                    {
                        end = duration.Value.TotalSeconds;
                    }
                    else
                    {
                        TimeSpan endSpan;
                        if (double.TryParse(range[1], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out end))
                        {
                            request.UrlParams["endtime"] = end.ToString(System.Globalization.CultureInfo.InvariantCulture);
                        }
                        else if (TimeSpan.TryParse(range[1], out endSpan))
                        {
                            request.UrlParams["endtime"] = endSpan.TotalSeconds.ToString(System.Globalization.CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            end = duration.Value.TotalSeconds;
                        }
                    }

                    request.UrlParams["starttime"] = (start + startTimeParam).ToString(System.Globalization.CultureInfo.InvariantCulture);

                    response.AddHreader("Vary", "TimeSeekRange.dlna.org");
                    response.AddHreader("TimeSeekRange.dlna.org", string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                                                "npt={0:0.000}-{1:0.000}/{2:0.000}", start, end, duration.Value.TotalSeconds));
                }
            }

            string prefDsDemuxStr = GetPrefDsDemux();

            if (prefDsDemuxStr != null)
            {
                request.UrlParams["prefDsDemux"] = prefDsDemuxStr;
            }

            EncoderBuilder builder = EncoderBuilder.GetEncoder(request.UrlParams);

            response.AddHreader(HttpHeader.ContentType, builder.GetMime());
            response.AddHreader(HttpHeader.AcceptRanges, "none");
            response.AddHreader("TransferMode.dlna.org", "Streaming");
            response.SendHeaders();

            if (request.Method == "GET")
            {
                OnLogEvent(string.Format("Starting encode {0} as {1}", request.UrlParams["source"], builder.GetMime()));
                builder.StartEncode(response.GetStream());
            }
        }