Beispiel #1
0
        private async Task <uBaseObject> GetMediaInfo()
        {
            var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetMediaInfo");

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

            var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceAvtransportType);

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

            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;

            return(UpnpContainer.Create(e));
        }
Beispiel #2
0
        private async Task <Tuple <bool, uBaseObject> > GetPositionInfo()
        {
            var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetPositionInfo");

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

            var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceAvtransportType);

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

            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>(false, 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.ErrorException("Unable to parse xml {0}", ex, 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));
        }
Beispiel #3
0
        private async Task GetVolume()
        {
            var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetVolume");

            if (command == null)
            {
                return;
            }

            var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceRenderingType);

            if (service == null)
            {
                return;
            }

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

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

            var volume      = result.Document.Descendants(uPnpNamespaces.RenderingControl + "GetVolumeResponse").Select(i => i.Element("CurrentVolume")).FirstOrDefault(i => i != null);
            var volumeValue = volume == null ? null : volume.Value;

            if (string.IsNullOrWhiteSpace(volumeValue))
            {
                return;
            }

            Volume = int.Parse(volumeValue, UsCulture);

            if (Volume > 0)
            {
                _muteVol = Volume;
            }
        }
Beispiel #4
0
        private async Task GetMute()
        {
            var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetMute");

            if (command == null)
            {
                return;
            }

            var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceRenderingType);

            if (service == null)
            {
                return;
            }

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

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

            var valueNode = result.Document.Descendants(uPnpNamespaces.RenderingControl + "GetMuteResponse").Select(i => i.Element("CurrentMute")).FirstOrDefault(i => i != null);
            var value     = valueNode == null ? null : valueNode.Value;

            IsMuted = string.Equals(value, "1", StringComparison.OrdinalIgnoreCase);
        }
Beispiel #5
0
        private async Task <bool> SetMute(bool mute)
        {
            var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetMute");

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

            var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceRenderingType);

            if (service == null)
            {
                return(false);
            }

            _logger.Debug("Setting mute");
            var value = mute ? 1 : 0;

            await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType, value))
            .ConfigureAwait(false);

            IsMuted = mute;

            return(true);
        }
Beispiel #6
0
        /// <summary>
        /// Sets volume on a scale of 0-100
        /// </summary>
        public async Task SetVolume(int value)
        {
            var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetVolume");

            if (command == null)
            {
                return;
            }

            var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceRenderingType);

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

            // Set it early and assume it will succeed
            // Remote control will perform better
            Volume = value;

            await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType, value))
            .ConfigureAwait(false);
        }
Beispiel #7
0
        private async Task GetMediaInfo()
        {
            var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetMediaInfo");

            if (command == null)
            {
                return;
            }

            var service = Properties.Services.FirstOrDefault(s => s.ServiceId == ServiceAvtransportId);

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

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

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

            var track = result.Document.Descendants("CurrentURIMetaData").Select(i => i.Value).FirstOrDefault();

            if (String.IsNullOrEmpty(track))
            {
                CurrentId = "0";
                return;
            }

            var uPnpResponse = XElement.Parse(track);

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

            var uTrack = uParser.CreateObjectFromXML(new uParserObject
            {
                Type    = e.GetValue(uPnpNamespaces.uClass),
                Element = e
            });

            if (uTrack != null)
            {
                CurrentId = uTrack.Id;
            }
        }
Beispiel #8
0
        private async Task <bool> GetPositionInfo()
        {
            var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetPositionInfo");

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

            var service = Properties.Services.FirstOrDefault(s => s.ServiceId == ServiceAvtransportId);

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

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

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

            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 (duration != null)
            {
                Duration = TimeSpan.Parse(duration);
            }

            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 (position != null)
            {
                Position = TimeSpan.Parse(position);
            }

            var track = result.Document.Descendants("TrackMetaData").Select(i => i.Value)
                        .FirstOrDefault();

            if (String.IsNullOrEmpty(track))
            {
                //If track is null, some vendors do this, use GetMediaInfo instead
                return(false);
            }

            var uPnpResponse = XElement.Parse(track);

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

            var uTrack = uBaseObject.Create(e);

            if (uTrack == null)
            {
                return(true);
            }

            CurrentId = uTrack.Id;

            return(true);
        }
Beispiel #9
0
        private async Task GetTransportInfo()
        {
            var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetTransportInfo");

            if (command == null)
            {
                return;
            }

            var service = Properties.Services.FirstOrDefault(s => s.ServiceId == ServiceAvtransportId);

            if (service == null)
            {
                return;
            }

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

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

            var transportState =
                result.Document.Descendants(uPnpNamespaces.AvTransport + "GetTransportInfoResponse").Select(i => i.Element("CurrentTransportState")).FirstOrDefault(i => i != null);

            var transportStateValue = transportState == null ? null : transportState.Value;

            if (transportStateValue != null)
            {
                TransportState = transportStateValue;
            }

            UpdateTime = DateTime.UtcNow;
        }
Beispiel #10
0
        private async Task GetVolume()
        {
            var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetVolume");

            if (command == null)
            {
                return;
            }

            var service = Properties.Services.FirstOrDefault(s => s.ServiceId == ServiceRenderingId);

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

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

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

            var volume      = result.Document.Descendants(uPnpNamespaces.RenderingControl + "GetVolumeResponse").Select(i => i.Element("CurrentVolume")).FirstOrDefault(i => i != null);
            var volumeValue = volume == null ? null : volume.Value;

            if (volumeValue == null)
            {
                return;
            }

            Volume = Int32.Parse(volumeValue);

            //Reset the Mute value if Volume is bigger than zero
            if (Volume > 0 && _muteVol > 0)
            {
                _muteVol = 0;
            }
        }
Beispiel #11
0
        public async Task <bool> SetPause()
        {
            var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "Pause");

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

            var service = Properties.Services.FirstOrDefault(s => s.ServiceId == ServiceAvtransportId);

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

            await Task.Delay(50).ConfigureAwait(false);

            TransportState = "PAUSED_PLAYBACK";
            return(true);
        }
Beispiel #12
0
        public async Task <bool> SetPlay()
        {
            var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "Play");

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

            var service = Properties.Services.FirstOrDefault(s => s.ServiceId == ServiceAvtransportId);

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

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

            _count = 5;
            return(true);
        }
Beispiel #13
0
        public async Task <bool> SetVolume(int value)
        {
            var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetVolume");

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

            var service = Properties.Services.FirstOrDefault(s => s.ServiceId == ServiceRenderingId);

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

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

            Volume = value;
            return(true);
        }