Example #1
0
        public void Initialise()
        {
            var rs = ServiceRegistration.Get <IResourceServer>();

            var ipaddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];

            var baseUrl = "http://" + GetLocalIp() + ":" + rs.PortIPv4;
            var url     = baseUrl + DlnaResourceAccessUtils.GetResourceUrl(Item.MediaItemId);

            ProtocolInfo = DlnaProtocolInfoFactory.GetProfileInfo(Item).ToString();

            if (Item.Aspects.ContainsKey(VideoAspect.ASPECT_ID))
            {
                // Load Video Specific items
                var videoAspect = Item.Aspects[VideoAspect.ASPECT_ID];

                Resolution = videoAspect.GetAttributeValue(VideoAspect.ATTR_WIDTH)
                             + "x"
                             + videoAspect.GetAttributeValue(VideoAspect.ATTR_HEIGHT);

                var vidBitRate = Convert.ToInt32(videoAspect.GetAttributeValue(VideoAspect.ATTR_VIDEOBITRATE));
                var audBitRate = Convert.ToInt32(videoAspect.GetAttributeValue(VideoAspect.ATTR_AUDIOBITRATE));
                BitRate = (uint)(vidBitRate + audBitRate);
            }

            Uri = url;
        }
        public void Initialise(MediaItem item)
        {
            DlnaMediaItem dlnaItem = Client.GetDlnaItem(item);
            var           url      = DlnaResourceAccessUtils.GetBaseResourceURL() + DlnaResourceAccessUtils.GetResourceUrl(item.MediaItemId.ToString() + (dlnaItem.IsSegmented ? "/playlist.m3u8" : ""), Client.ClientId);

            BitRate               = null;
            SampleFrequency       = null;
            NumberOfAudioChannels = null;
            Size          = null;
            BitsPerSample = null;
            ColorDepth    = null;
            Duration      = null;
            var dlnaProtocolInfo = DlnaProtocolInfoFactory.GetProfileInfo(dlnaItem, Client.Profile.ProtocolInfo);

            if (dlnaProtocolInfo != null)
            {
                ProtocolInfo = dlnaProtocolInfo.ToString();
            }
            if (dlnaItem.Metadata == null)
            {
                throw new DlnaAspectMissingException("No DLNA metadata found for MediaItem " + dlnaItem.MediaItemId);
            }
            if (dlnaItem.Metadata.Size > 0)
            {
                Size = Convert.ToUInt64(dlnaItem.Metadata.Size);
            }
            if (dlnaItem.IsImage == false)
            {
                if (dlnaItem.Metadata.Bitrate > 0)
                {
                    BitRate = Convert.ToUInt32((double)dlnaItem.Metadata.Bitrate / 8.0);
                }
                if (dlnaItem.Audio != null && dlnaItem.Audio.Count > 0)
                {
                    if (dlnaItem.Audio[0].Frequency > 0)
                    {
                        SampleFrequency = Convert.ToUInt32(dlnaItem.Audio[0].Frequency);
                    }
                    if (dlnaItem.Audio[0].Channels > 0)
                    {
                        NumberOfAudioChannels = Convert.ToUInt32(dlnaItem.Audio[0].Channels);
                    }
                }
                if (dlnaItem.Metadata.Duration > 0)
                {
                    Duration = TimeSpan.FromSeconds(dlnaItem.Metadata.Duration.Value).ToString(@"hh\:mm\:ss\.fff");
                }
            }
            if (dlnaItem.IsVideo == true)
            {
                Resolution = dlnaItem.Video.Width + "x" + dlnaItem.Video.Height;
            }
            else if (dlnaItem.IsImage == true)
            {
                Resolution = dlnaItem.Image.Width + "x" + dlnaItem.Image.Height;
            }

            Uri = url;
        }
Example #3
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);
        }