Exemple #1
0
        public static TransportCommands Create(XDocument document)
        {
            var command = new TransportCommands();

            var actionList = document.Descendants(UPnpNamespaces.Svc + "actionList");

            foreach (var container in actionList.Descendants(UPnpNamespaces.Svc + "action"))
            {
                command.ServiceActions.Add(ServiceActionFromXml(container));
            }

            var stateValues = document.Descendants(UPnpNamespaces.ServiceStateTable).FirstOrDefault();

            if (stateValues != null)
            {
                foreach (var container in stateValues.Elements(UPnpNamespaces.Svc + "stateVariable"))
                {
                    command.StateVariables.Add(FromXml(container));
                }
            }

            return(command);
        }
Exemple #2
0
        private async Task <Tuple <bool, uBaseObject> > GetPositionInfo(TransportCommands avCommands, CancellationToken cancellationToken)
        {
            var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetPositionInfo");

            if (command == null)
            {
                return(new Tuple <bool, uBaseObject>(false, null));
            }

            var service = GetAvTransportService();

            if (service == null)
            {
                throw new InvalidOperationException("Unable to find service");
            }

            var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);

            var result = await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, rendererCommands.BuildPost(command, service.ServiceType), false)
                         .ConfigureAwait(false);

            if (result == null || result.Document == null)
            {
                return(new Tuple <bool, uBaseObject>(false, null));
            }

            var trackUriElem = result.Document.Descendants(uPnpNamespaces.AvTransport + "GetPositionInfoResponse").Select(i => i.Element("TrackURI")).FirstOrDefault(i => i != null);
            var trackUri     = trackUriElem == null ? null : trackUriElem.Value;

            var durationElem = result.Document.Descendants(uPnpNamespaces.AvTransport + "GetPositionInfoResponse").Select(i => i.Element("TrackDuration")).FirstOrDefault(i => i != null);
            var duration     = durationElem == null ? null : durationElem.Value;

            if (!string.IsNullOrWhiteSpace(duration) &&
                !string.Equals(duration, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
            {
                Duration = TimeSpan.Parse(duration, UsCulture);
            }
            else
            {
                Duration = null;
            }

            var positionElem = result.Document.Descendants(uPnpNamespaces.AvTransport + "GetPositionInfoResponse").Select(i => i.Element("RelTime")).FirstOrDefault(i => i != null);
            var position     = positionElem == null ? null : positionElem.Value;

            if (!string.IsNullOrWhiteSpace(position) && !string.Equals(position, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
            {
                Position = TimeSpan.Parse(position, UsCulture);
            }

            var track = result.Document.Descendants("TrackMetaData").FirstOrDefault();

            if (track == null)
            {
                //If track is null, some vendors do this, use GetMediaInfo instead
                return(new Tuple <bool, uBaseObject>(true, null));
            }

            var trackString = (string)track;

            if (string.IsNullOrWhiteSpace(trackString) || string.Equals(trackString, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
            {
                return(new Tuple <bool, uBaseObject>(true, null));
            }

            XElement uPnpResponse;

            // Handle different variations sent back by devices
            try
            {
                uPnpResponse = XElement.Parse(trackString);
            }
            catch (Exception)
            {
                // first try to add a root node with a dlna namesapce
                try
                {
                    uPnpResponse = XElement.Parse("<data xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">" + trackString + "</data>");
                    uPnpResponse = uPnpResponse.Descendants().First();
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Unable to parse xml {0}", trackString);
                    return(new Tuple <bool, uBaseObject>(true, null));
                }
            }

            var e = uPnpResponse.Element(uPnpNamespaces.items);

            var uTrack = CreateUBaseObject(e, trackUri);

            return(new Tuple <bool, uBaseObject>(true, uTrack));
        }
Exemple #3
0
        private async Task <uBaseObject> GetMediaInfo(TransportCommands avCommands, CancellationToken cancellationToken)
        {
            var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetMediaInfo");

            if (command == null)
            {
                return(null);
            }

            var service = GetAvTransportService();

            if (service == null)
            {
                throw new InvalidOperationException("Unable to find service");
            }

            var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);

            var result = await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, rendererCommands.BuildPost(command, service.ServiceType), false)
                         .ConfigureAwait(false);

            if (result == null || result.Document == null)
            {
                return(null);
            }

            var track = result.Document.Descendants("CurrentURIMetaData").FirstOrDefault();

            if (track == null)
            {
                return(null);
            }

            var e = track.Element(uPnpNamespaces.items) ?? track;

            var elementString = (string)e;

            if (!string.IsNullOrWhiteSpace(elementString))
            {
                return(UpnpContainer.Create(e));
            }

            track = result.Document.Descendants("CurrentURI").FirstOrDefault();

            if (track == null)
            {
                return(null);
            }

            e = track.Element(uPnpNamespaces.items) ?? track;

            elementString = (string)e;

            if (!string.IsNullOrWhiteSpace(elementString))
            {
                return(new uBaseObject
                {
                    Url = elementString
                });
            }

            return(null);
        }