Exemplo n.º 1
0
        /// <summary>
        /// Returns a DLNA media item based on the given client and request
        /// </summary>
        /// <returns>Returns the requested DLNA media item otherwise null</returns>
        internal static StreamItem GetNewStreamItem(EndPointSettings client, Uri uri)
        {
            if (STREAM_ITEMS.TryGetValue(client.ClientId, out var currentStreamItem))
            {
                return(currentStreamItem);
            }

            currentStreamItem = new StreamItem(uri.Host);

            Guid          mediaItemGuid = Guid.Empty;
            DlnaMediaItem dlnaItem      = null;
            int           channel       = 0;

            if (DlnaResourceAccessUtils.ParseMediaItem(uri, out mediaItemGuid))
            {
                if (mediaItemGuid == Guid.Empty)
                {
                    throw new InvalidOperationException(string.Format("Illegal request syntax. Correct syntax is '{0}'", DlnaResourceAccessUtils.SYNTAX));
                }

                if (!client.DlnaMediaItems.TryGetValue(mediaItemGuid, out dlnaItem))
                {
                    // Attempt to grab the media item from the database.
                    MediaItem item = MediaLibraryHelper.GetMediaItem(mediaItemGuid);
                    if (item == null)
                    {
                        throw new Exception(string.Format("Media item '{0}' not found.", mediaItemGuid));
                    }

                    dlnaItem = client.GetDlnaItem(item);
                }

                if (dlnaItem == null)
                {
                    throw new Exception(string.Format("DLNA media item '{0}' not found.", mediaItemGuid));
                }
            }
            else if (DlnaResourceAccessUtils.ParseTVChannel(uri, out channel))
            {
                dlnaItem = client.GetLiveDlnaItem(channel);

                if (dlnaItem == null)
                {
                    throw new Exception(string.Format("DLNA TV channel '{0}' was never tuned.", channel));
                }
            }
            else if (DlnaResourceAccessUtils.ParseRadioChannel(uri, out channel))
            {
                dlnaItem = client.GetLiveDlnaItem(channel);

                if (dlnaItem == null)
                {
                    throw new Exception(string.Format("DLNA radio channel '{0}' was never tuned.", channel));
                }
            }

            currentStreamItem.RequestedMediaItem = mediaItemGuid;
            currentStreamItem.TranscoderObject   = dlnaItem;
            currentStreamItem.Title         = dlnaItem?.MediaItemTitle;
            currentStreamItem.LiveChannelId = channel > 0 ? channel : 0;

            return(currentStreamItem);
        }
Exemplo n.º 2
0
        public override bool Process(IHttpRequest request, IHttpResponse response, IHttpSession session)
        {
            var uri = request.Uri;

            // Check the request path to see if it's for us.
            if (!uri.AbsolutePath.StartsWith(DlnaResourceAccessUtils.RESOURCE_ACCESS_PATH))
            {
                return(false);
            }

            // Grab the media item given in the request.
            Guid mediaItemGuid;

            if (!DlnaResourceAccessUtils.ParseMediaItem(uri, out mediaItemGuid))
            {
                throw new BadRequestException(string.Format("Illegal request syntax. Correct syntax is '{0}'", DlnaResourceAccessUtils.SYNTAX));
            }

            try
            {
                // Attempt to grab the media item from the database.
                var item = MediaLibraryHelper.GetMediaItem(mediaItemGuid);
                if (item == null)
                {
                    throw new BadRequestException(string.Format("Media item '{0}' not found.", mediaItemGuid));
                }

                // Grab the mimetype from the media item and set the Content Type header.
                var mimeType = MediaLibraryHelper.GetOrGuessMimeType(item);
                if (mimeType == null)
                {
                    throw new InternalServerException("Media item has bad mime type, re-import media item");
                }
                response.ContentType = mimeType;

                // Grab the resource path for the media item.
                var resourcePathStr =
                    item.Aspects[ProviderResourceAspect.ASPECT_ID].GetAttributeValue(
                        ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH);
                var resourcePath = ResourcePath.Deserialize(resourcePathStr.ToString());

                var ra = GetResourceAccessor(resourcePath);
                using (var resourceStream = ra.OpenRead())
                {
                    // HTTP/1.1 RFC2616 section 14.25 'If-Modified-Since'
                    if (!string.IsNullOrEmpty(request.Headers["If-Modified-Since"]))
                    {
                        DateTime lastRequest = DateTime.Parse(request.Headers["If-Modified-Since"]);
                        if (lastRequest.CompareTo(ra.LastChanged) <= 0)
                        {
                            response.Status = HttpStatusCode.NotModified;
                        }
                    }

                    // HTTP/1.1 RFC2616 section 14.29 'Last-Modified'
                    response.AddHeader("Last-Modified", ra.LastChanged.ToUniversalTime().ToString("r"));

                    // DLNA Requirement: [7.4.26.1-6]
                    // Since the DLNA spec allows contentFeatures.dlna.org with any request, we'll put it in.
                    if (!string.IsNullOrEmpty(request.Headers["getcontentFeatures.dlna.org"]))
                    {
                        if (request.Headers["getcontentFeatures.dlna.org"] != "1")
                        {
                            // DLNA Requirement [7.4.26.5]
                            throw new BadRequestException("Illegal value for getcontentFeatures.dlna.org");
                        }
                    }
                    var dlnaString = DlnaProtocolInfoFactory.GetProfileInfo(item).ToString();
                    response.AddHeader("contentFeatures.dlna.org", dlnaString);

                    // DLNA Requirement: [7.4.55-57]
                    // TODO: Bad implementation of requirement
                    if (!string.IsNullOrEmpty(request.Headers["transferMode.dlna.org"]))
                    {
                        if (request.Headers["transferMode.dlna.org"] == "Streaming")
                        {
                            response.AddHeader("transferMode.dlna.org", "Streaming");
                        }
                        if (request.Headers["transferMode.dlna.org"] == "Interactive")
                        {
                            response.AddHeader("transferMode.dlna.org", "Interactive");
                        }
                        if (request.Headers["transferMode.dlna.org"] == "Background")
                        {
                            response.AddHeader("transferMode.dlna.org", "Background");
                        }
                    }

                    string        byteRangesSpecifier = request.Headers["Range"];
                    IList <Range> ranges      = ParseRanges(byteRangesSpecifier, resourceStream.Length);
                    bool          onlyHeaders = request.Method == Method.Header || response.Status == HttpStatusCode.NotModified;
                    if (ranges != null && ranges.Count == 1)
                    {
                        // We only support one range
                        SendRange(response, resourceStream, ranges[0], onlyHeaders);
                    }
                    else
                    {
                        SendWholeFile(response, resourceStream, onlyHeaders);
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                throw new InternalServerException(string.Format("Failed to proccess media item '{0}'", mediaItemGuid), ex);
            }

            return(true);
        }