jellyfin-server/Emby.Dlna/PlayTo/Device.cs

1180 lines
40 KiB
C#
Raw Normal View History

2016-10-29 22:22:20 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
2016-10-29 22:34:54 +00:00
using Emby.Dlna.Common;
using Emby.Dlna.Ssdp;
2016-10-29 22:22:20 +00:00
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
2016-10-29 22:34:54 +00:00
using Emby.Dlna.Server;
2016-11-04 08:31:05 +00:00
using MediaBrowser.Model.Threading;
using MediaBrowser.Model.Extensions;
2016-10-29 22:22:20 +00:00
2016-10-29 22:34:54 +00:00
namespace Emby.Dlna.PlayTo
2016-10-29 22:22:20 +00:00
{
public class Device : IDisposable
{
#region Fields & Properties
2016-11-04 08:31:05 +00:00
private ITimer _timer;
2016-10-29 22:22:20 +00:00
public DeviceInfo Properties { get; set; }
private int _muteVol;
public bool IsMuted { get; set; }
private int _volume;
public int Volume
{
get
{
RefreshVolumeIfNeeded();
return _volume;
}
set
{
_volume = value;
}
}
public TimeSpan? Duration { get; set; }
private TimeSpan _position = TimeSpan.FromSeconds(0);
public TimeSpan Position
{
get
{
return _position;
}
set
{
_position = value;
}
}
public TRANSPORTSTATE TransportState { get; private set; }
public bool IsPlaying
{
get
{
return TransportState == TRANSPORTSTATE.PLAYING;
}
}
public bool IsPaused
{
get
{
return TransportState == TRANSPORTSTATE.PAUSED || TransportState == TRANSPORTSTATE.PAUSED_PLAYBACK;
}
}
public bool IsStopped
{
get
{
return TransportState == TRANSPORTSTATE.STOPPED;
}
}
#endregion
private readonly IHttpClient _httpClient;
private readonly ILogger _logger;
private readonly IServerConfigurationManager _config;
public DateTime DateLastActivity { get; private set; }
public Action OnDeviceUnavailable { get; set; }
2016-11-04 08:31:05 +00:00
private readonly ITimerFactory _timerFactory;
public Device(DeviceInfo deviceProperties, IHttpClient httpClient, ILogger logger, IServerConfigurationManager config, ITimerFactory timerFactory)
2016-10-29 22:22:20 +00:00
{
Properties = deviceProperties;
_httpClient = httpClient;
_logger = logger;
_config = config;
2016-11-04 08:31:05 +00:00
_timerFactory = timerFactory;
2016-10-29 22:22:20 +00:00
}
public void Start()
{
2018-09-12 17:26:21 +00:00
_logger.Debug("Dlna Device.Start");
_timer = _timerFactory.Create(TimerCallback, null, 1000, Timeout.Infinite);
2016-10-29 22:22:20 +00:00
}
private DateTime _lastVolumeRefresh;
2018-09-12 17:26:21 +00:00
private bool _volumeRefreshActive;
2016-10-29 22:22:20 +00:00
private void RefreshVolumeIfNeeded()
{
2018-09-12 17:26:21 +00:00
if (!_volumeRefreshActive)
2016-10-29 22:22:20 +00:00
{
return;
}
if (DateTime.UtcNow >= _lastVolumeRefresh.AddSeconds(5))
{
_lastVolumeRefresh = DateTime.UtcNow;
2018-09-12 17:26:21 +00:00
RefreshVolume(CancellationToken.None);
2016-10-29 22:22:20 +00:00
}
}
2018-09-12 17:26:21 +00:00
private async void RefreshVolume(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
if (_disposed)
return;
try
{
2018-09-12 17:26:21 +00:00
await GetVolume(cancellationToken).ConfigureAwait(false);
await GetMute(cancellationToken).ConfigureAwait(false);
2016-10-29 22:22:20 +00:00
}
catch (Exception ex)
{
_logger.ErrorException("Error updating device volume info for {0}", ex, Properties.Name);
}
}
private readonly object _timerLock = new object();
2018-09-12 17:26:21 +00:00
private void RestartTimer(bool immediate = false)
2016-10-29 22:22:20 +00:00
{
2017-08-10 20:06:36 +00:00
lock (_timerLock)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
if (_disposed)
return;
_volumeRefreshActive = true;
2017-08-10 20:06:36 +00:00
2018-09-12 17:26:21 +00:00
var time = immediate ? 100 : 10000;
_timer.Change(time, Timeout.Infinite);
2016-10-29 22:22:20 +00:00
}
}
/// <summary>
/// Restarts the timer in inactive mode.
/// </summary>
private void RestartTimerInactive()
{
2017-08-10 20:06:36 +00:00
lock (_timerLock)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
if (_disposed)
return;
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
_volumeRefreshActive = false;
2017-08-10 20:06:36 +00:00
2018-09-12 17:26:21 +00:00
_timer.Change(Timeout.Infinite, Timeout.Infinite);
2016-10-29 22:22:20 +00:00
}
}
2018-09-12 17:26:21 +00:00
public void OnPlaybackStartedExternally()
{
RestartTimer(true);
}
2016-10-29 22:22:20 +00:00
#region Commanding
2018-09-12 17:26:21 +00:00
public Task VolumeDown(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
var sendVolume = Math.Max(Volume - 5, 0);
2018-09-12 17:26:21 +00:00
return SetVolume(sendVolume, cancellationToken);
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
public Task VolumeUp(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
var sendVolume = Math.Min(Volume + 5, 100);
2018-09-12 17:26:21 +00:00
return SetVolume(sendVolume, cancellationToken);
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
public Task ToggleMute(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
if (IsMuted)
{
2018-09-12 17:26:21 +00:00
return Unmute(cancellationToken);
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
return Mute(cancellationToken);
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
public async Task Mute(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var success = await SetMute(true, cancellationToken).ConfigureAwait(true);
2016-10-29 22:22:20 +00:00
if (!success)
{
2018-09-12 17:26:21 +00:00
await SetVolume(0, cancellationToken).ConfigureAwait(false);
2016-10-29 22:22:20 +00:00
}
}
2018-09-12 17:26:21 +00:00
public async Task Unmute(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var success = await SetMute(false, cancellationToken).ConfigureAwait(true);
2016-10-29 22:22:20 +00:00
if (!success)
{
var sendVolume = _muteVol <= 0 ? 20 : _muteVol;
2018-09-12 17:26:21 +00:00
await SetVolume(sendVolume, cancellationToken).ConfigureAwait(false);
2016-10-29 22:22:20 +00:00
}
}
2016-12-15 07:12:52 +00:00
private DeviceService GetServiceRenderingControl()
{
var services = Properties.Services;
return services.FirstOrDefault(s => string.Equals(s.ServiceType, "urn:schemas-upnp-org:service:RenderingControl:1", StringComparison.OrdinalIgnoreCase)) ??
services.FirstOrDefault(s => (s.ServiceType ?? string.Empty).StartsWith("urn:schemas-upnp-org:service:RenderingControl", StringComparison.OrdinalIgnoreCase));
}
private DeviceService GetAvTransportService()
{
var services = Properties.Services;
return services.FirstOrDefault(s => string.Equals(s.ServiceType, "urn:schemas-upnp-org:service:AVTransport:1", StringComparison.OrdinalIgnoreCase)) ??
services.FirstOrDefault(s => (s.ServiceType ?? string.Empty).StartsWith("urn:schemas-upnp-org:service:AVTransport", StringComparison.OrdinalIgnoreCase));
}
2018-09-12 17:26:21 +00:00
private async Task<bool> SetMute(bool mute, CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
var command = rendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetMute");
2016-10-29 22:22:20 +00:00
if (command == null)
return false;
2016-12-15 07:12:52 +00:00
var service = GetServiceRenderingControl();
2016-10-29 22:22:20 +00:00
if (service == null)
{
return false;
}
_logger.Debug("Setting mute");
var value = mute ? 1 : 0;
2018-09-12 17:26:21 +00:00
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, rendererCommands.BuildPost(command, service.ServiceType, value))
2016-10-29 22:22:20 +00:00
.ConfigureAwait(false);
IsMuted = mute;
return true;
}
/// <summary>
/// Sets volume on a scale of 0-100
/// </summary>
2018-09-12 17:26:21 +00:00
public async Task SetVolume(int value, CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
var command = rendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetVolume");
2016-10-29 22:22:20 +00:00
if (command == null)
return;
2016-12-15 07:12:52 +00:00
var service = GetServiceRenderingControl();
2016-10-29 22:22:20 +00:00
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;
2018-09-12 17:26:21 +00:00
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, rendererCommands.BuildPost(command, service.ServiceType, value))
2016-10-29 22:22:20 +00:00
.ConfigureAwait(false);
}
2018-09-12 17:26:21 +00:00
public async Task Seek(TimeSpan value, CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "Seek");
2016-10-29 22:22:20 +00:00
if (command == null)
return;
2016-12-15 07:12:52 +00:00
var service = GetAvTransportService();
2016-10-29 22:22:20 +00:00
if (service == null)
{
throw new InvalidOperationException("Unable to find service");
}
2018-09-12 17:26:21 +00:00
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, avCommands.BuildPost(command, service.ServiceType, String.Format("{0:hh}:{0:mm}:{0:ss}", value), "REL_TIME"))
2016-10-29 22:22:20 +00:00
.ConfigureAwait(false);
2018-09-12 17:26:21 +00:00
RestartTimer(true);
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
public async Task SetAvTransport(string url, string header, string metaData, CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
url = url.Replace("&", "&amp;");
2016-10-29 22:22:20 +00:00
_logger.Debug("{0} - SetAvTransport Uri: {1} DlnaHeaders: {2}", Properties.Name, url, header);
2018-09-12 17:26:21 +00:00
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetAVTransportURI");
2016-10-29 22:22:20 +00:00
if (command == null)
return;
var dictionary = new Dictionary<string, string>
{
{"CurrentURI", url},
{"CurrentURIMetaData", CreateDidlMeta(metaData)}
};
2016-12-15 07:12:52 +00:00
var service = GetAvTransportService();
2016-10-29 22:22:20 +00:00
if (service == null)
{
throw new InvalidOperationException("Unable to find service");
}
2018-09-12 17:26:21 +00:00
var post = avCommands.BuildPost(command, service.ServiceType, url, dictionary);
2016-10-29 22:22:20 +00:00
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, post, header: header)
.ConfigureAwait(false);
await Task.Delay(50).ConfigureAwait(false);
try
{
2018-09-12 17:26:21 +00:00
await SetPlay(avCommands, CancellationToken.None).ConfigureAwait(false);
2016-10-29 22:22:20 +00:00
}
catch
{
// Some devices will throw an error if you tell it to play when it's already playing
// Others won't
}
2018-09-12 17:26:21 +00:00
RestartTimer(true);
2016-10-29 22:22:20 +00:00
}
private string CreateDidlMeta(string value)
{
if (string.IsNullOrEmpty(value))
return String.Empty;
return DescriptionXmlBuilder.Escape(value);
}
2018-09-12 17:26:21 +00:00
private Task SetPlay(TransportCommands avCommands, CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "Play");
2016-10-29 22:22:20 +00:00
if (command == null)
2018-09-12 17:26:21 +00:00
return Task.CompletedTask;
2016-10-29 22:22:20 +00:00
2016-12-15 07:12:52 +00:00
var service = GetAvTransportService();
2016-10-29 22:22:20 +00:00
if (service == null)
{
throw new InvalidOperationException("Unable to find service");
}
2018-09-12 17:26:21 +00:00
return new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, avCommands.BuildPost(command, service.ServiceType, 1));
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
public async Task SetPlay(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
await SetPlay(avCommands, cancellationToken).ConfigureAwait(false);
RestartTimer(true);
}
public async Task SetStop(CancellationToken cancellationToken)
{
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "Stop");
2016-10-29 22:22:20 +00:00
if (command == null)
return;
2016-12-15 07:12:52 +00:00
var service = GetAvTransportService();
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, avCommands.BuildPost(command, service.ServiceType, 1))
2016-10-29 22:22:20 +00:00
.ConfigureAwait(false);
2018-09-12 17:26:21 +00:00
RestartTimer(true);
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
public async Task SetPause(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "Pause");
2016-10-29 22:22:20 +00:00
if (command == null)
return;
2016-12-15 07:12:52 +00:00
var service = GetAvTransportService();
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, avCommands.BuildPost(command, service.ServiceType, 1))
2016-10-29 22:22:20 +00:00
.ConfigureAwait(false);
TransportState = TRANSPORTSTATE.PAUSED;
2018-09-12 17:26:21 +00:00
RestartTimer(true);
2016-10-29 22:22:20 +00:00
}
#endregion
#region Get data
private int _connectFailureCount;
private async void TimerCallback(object sender)
{
if (_disposed)
return;
try
{
2018-09-12 17:26:21 +00:00
var cancellationToken = CancellationToken.None;
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
if (avCommands == null)
{
return;
}
var transportState = await GetTransportInfo(avCommands, cancellationToken).ConfigureAwait(false);
2016-10-29 22:22:20 +00:00
2017-01-24 19:54:18 +00:00
if (_disposed)
{
return;
}
2016-10-29 22:22:20 +00:00
DateLastActivity = DateTime.UtcNow;
if (transportState.HasValue)
{
// If we're not playing anything no need to get additional data
if (transportState.Value == TRANSPORTSTATE.STOPPED)
{
UpdateMediaInfo(null, transportState.Value);
}
else
{
2018-09-12 17:26:21 +00:00
var tuple = await GetPositionInfo(avCommands, cancellationToken).ConfigureAwait(false);
2016-10-29 22:22:20 +00:00
var currentObject = tuple.Item2;
if (tuple.Item1 && currentObject == null)
{
2018-09-12 17:26:21 +00:00
currentObject = await GetMediaInfo(avCommands, cancellationToken).ConfigureAwait(false);
2016-10-29 22:22:20 +00:00
}
if (currentObject != null)
{
UpdateMediaInfo(currentObject, transportState.Value);
}
}
_connectFailureCount = 0;
if (_disposed)
return;
// If we're not playing anything make sure we don't get data more often than neccessry to keep the Session alive
if (transportState.Value == TRANSPORTSTATE.STOPPED)
{
2018-09-12 17:26:21 +00:00
RestartTimerInactive();
2016-10-29 22:22:20 +00:00
}
else
{
RestartTimer();
}
}
2017-08-10 20:06:36 +00:00
else
{
RestartTimerInactive();
}
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
catch (Exception ex)
2016-10-29 22:22:20 +00:00
{
if (_disposed)
return;
//_logger.ErrorException("Error updating device info for {0}", ex, Properties.Name);
_connectFailureCount++;
if (_connectFailureCount >= 3)
{
2018-09-12 17:26:21 +00:00
var action = OnDeviceUnavailable;
if (action != null)
2016-10-29 22:22:20 +00:00
{
_logger.Debug("Disposing device due to loss of connection");
2018-09-12 17:26:21 +00:00
action();
2016-10-29 22:22:20 +00:00
return;
}
}
2018-09-12 17:26:21 +00:00
RestartTimerInactive();
2016-10-29 22:22:20 +00:00
}
}
2018-09-12 17:26:21 +00:00
private async Task GetVolume(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2017-01-24 19:54:18 +00:00
if (_disposed)
{
return;
}
2018-09-12 17:26:21 +00:00
var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
var command = rendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetVolume");
2016-10-29 22:22:20 +00:00
if (command == null)
return;
2016-12-15 07:12:52 +00:00
var service = GetServiceRenderingControl();
2016-10-29 22:22:20 +00:00
if (service == null)
{
return;
}
2018-09-12 17:26:21 +00:00
var result = await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, rendererCommands.BuildPost(command, service.ServiceType), true)
2016-10-29 22:22:20 +00:00
.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;
}
}
2018-09-12 17:26:21 +00:00
private async Task GetMute(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2017-01-24 19:54:18 +00:00
if (_disposed)
{
return;
}
2018-09-12 17:26:21 +00:00
var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
var command = rendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetMute");
2016-10-29 22:22:20 +00:00
if (command == null)
return;
2016-12-15 07:12:52 +00:00
var service = GetServiceRenderingControl();
2016-10-29 22:22:20 +00:00
if (service == null)
{
return;
}
2018-09-12 17:26:21 +00:00
var result = await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, rendererCommands.BuildPost(command, service.ServiceType), true)
2016-10-29 22:22:20 +00:00
.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);
}
2018-09-12 17:26:21 +00:00
private async Task<TRANSPORTSTATE?> GetTransportInfo(TransportCommands avCommands, CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetTransportInfo");
2016-10-29 22:22:20 +00:00
if (command == null)
return null;
2016-12-15 07:12:52 +00:00
var service = GetAvTransportService();
2016-10-29 22:22:20 +00:00
if (service == null)
return null;
2018-09-12 17:26:21 +00:00
var result = await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, avCommands.BuildPost(command, service.ServiceType), false)
2016-10-29 22:22:20 +00:00
.ConfigureAwait(false);
if (result == null || result.Document == null)
return null;
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 state;
if (Enum.TryParse(transportStateValue, true, out state))
{
return state;
}
}
return null;
}
2018-09-12 17:26:21 +00:00
private async Task<uBaseObject> GetMediaInfo(TransportCommands avCommands, CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetMediaInfo");
2016-10-29 22:22:20 +00:00
if (command == null)
return null;
2016-12-15 07:12:52 +00:00
var service = GetAvTransportService();
2016-10-29 22:22:20 +00:00
if (service == null)
{
throw new InvalidOperationException("Unable to find service");
}
2018-09-12 17:26:21 +00:00
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)
2016-10-29 22:22:20 +00:00
.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;
2017-09-27 14:51:36 +00:00
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;
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
private async Task<Tuple<bool, uBaseObject>> GetPositionInfo(TransportCommands avCommands, CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetPositionInfo");
2016-10-29 22:22:20 +00:00
if (command == null)
return new Tuple<bool, uBaseObject>(false, null);
2016-12-15 07:12:52 +00:00
var service = GetAvTransportService();
2016-10-29 22:22:20 +00:00
if (service == null)
{
throw new InvalidOperationException("Unable to find service");
}
2018-09-12 17:26:21 +00:00
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)
2016-10-29 22:22:20 +00:00
.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))
{
2017-09-27 14:51:36 +00:00
return new Tuple<bool, uBaseObject>(true, null);
2016-10-29 22:22:20 +00:00
}
XElement uPnpResponse;
2016-12-15 07:12:52 +00:00
2016-10-29 22:22:20 +00:00
// 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);
}
private static uBaseObject CreateUBaseObject(XElement container, string trackUri)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
var url = container.GetValue(uPnpNamespaces.Res);
if (string.IsNullOrWhiteSpace(url))
{
url = trackUri;
}
return new uBaseObject
{
Id = container.GetAttributeValue(uPnpNamespaces.Id),
ParentId = container.GetAttributeValue(uPnpNamespaces.ParentId),
Title = container.GetValue(uPnpNamespaces.title),
IconUrl = container.GetValue(uPnpNamespaces.Artwork),
SecondText = "",
Url = url,
ProtocolInfo = GetProtocolInfo(container),
MetaData = container.ToString()
};
}
private static string[] GetProtocolInfo(XElement container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
var resElement = container.Element(uPnpNamespaces.Res);
if (resElement != null)
{
var info = resElement.Attribute(uPnpNamespaces.ProtocolInfo);
if (info != null && !string.IsNullOrWhiteSpace(info.Value))
{
return info.Value.Split(':');
}
}
return new string[4];
}
#endregion
#region From XML
2018-09-12 17:26:21 +00:00
private async Task<TransportCommands> GetAVProtocolAsync(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var avCommands = AvCommands;
if (avCommands != null)
{
return avCommands;
}
2017-01-24 19:54:18 +00:00
if (_disposed)
{
2018-09-12 17:26:21 +00:00
throw new ObjectDisposedException(GetType().Name);
2017-01-24 19:54:18 +00:00
}
2016-12-15 07:12:52 +00:00
var avService = GetAvTransportService();
2016-10-29 22:22:20 +00:00
if (avService == null)
2018-09-12 17:26:21 +00:00
{
return null;
}
2016-10-29 22:22:20 +00:00
string url = NormalizeUrl(Properties.BaseUrl, avService.ScpdUrl);
var httpClient = new SsdpHttpClient(_httpClient, _config);
2018-09-12 17:26:21 +00:00
2017-11-23 15:46:16 +00:00
var document = await httpClient.GetDataAsync(url, cancellationToken).ConfigureAwait(false);
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
avCommands = TransportCommands.Create(document);
AvCommands = avCommands;
return avCommands;
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
private async Task<TransportCommands> GetRenderingProtocolAsync(CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var rendererCommands = RendererCommands;
if (rendererCommands != null)
{
return rendererCommands;
}
2017-01-24 19:54:18 +00:00
if (_disposed)
{
2018-09-12 17:26:21 +00:00
throw new ObjectDisposedException(GetType().Name);
2017-01-24 19:54:18 +00:00
}
2016-12-15 07:12:52 +00:00
var avService = GetServiceRenderingControl();
2016-10-29 22:22:20 +00:00
if (avService == null)
2018-09-12 17:26:21 +00:00
{
throw new ArgumentException("Device AvService is null");
}
2016-10-29 22:22:20 +00:00
string url = NormalizeUrl(Properties.BaseUrl, avService.ScpdUrl);
var httpClient = new SsdpHttpClient(_httpClient, _config);
2018-09-12 17:26:21 +00:00
_logger.Debug("Dlna Device.GetRenderingProtocolAsync");
2017-11-23 15:46:16 +00:00
var document = await httpClient.GetDataAsync(url, cancellationToken).ConfigureAwait(false);
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
rendererCommands = TransportCommands.Create(document);
RendererCommands = rendererCommands;
return rendererCommands;
2016-10-29 22:22:20 +00:00
}
private string NormalizeUrl(string baseUrl, string url)
{
// If it's already a complete url, don't stick anything onto the front of it
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
return url;
}
if (!url.Contains("/"))
url = "/dmr/" + url;
if (!url.StartsWith("/"))
url = "/" + url;
return baseUrl + url;
}
private TransportCommands AvCommands
{
get;
set;
}
2018-09-12 17:26:21 +00:00
private TransportCommands RendererCommands
2016-10-29 22:22:20 +00:00
{
get;
set;
}
2017-11-23 15:46:16 +00:00
public static async Task<Device> CreateuPnpDeviceAsync(Uri url, IHttpClient httpClient, IServerConfigurationManager config, ILogger logger, ITimerFactory timerFactory, CancellationToken cancellationToken)
2016-10-29 22:22:20 +00:00
{
var ssdpHttpClient = new SsdpHttpClient(httpClient, config);
2017-11-23 15:46:16 +00:00
var document = await ssdpHttpClient.GetDataAsync(url.ToString(), cancellationToken).ConfigureAwait(false);
2016-10-29 22:22:20 +00:00
var deviceProperties = new DeviceInfo();
var friendlyNames = new List<string>();
var name = document.Descendants(uPnpNamespaces.ud.GetName("friendlyName")).FirstOrDefault();
if (name != null && !string.IsNullOrWhiteSpace(name.Value))
friendlyNames.Add(name.Value);
var room = document.Descendants(uPnpNamespaces.ud.GetName("roomName")).FirstOrDefault();
if (room != null && !string.IsNullOrWhiteSpace(room.Value))
friendlyNames.Add(room.Value);
deviceProperties.Name = string.Join(" ", friendlyNames.ToArray(friendlyNames.Count));
2016-10-29 22:22:20 +00:00
var model = document.Descendants(uPnpNamespaces.ud.GetName("modelName")).FirstOrDefault();
if (model != null)
deviceProperties.ModelName = model.Value;
var modelNumber = document.Descendants(uPnpNamespaces.ud.GetName("modelNumber")).FirstOrDefault();
if (modelNumber != null)
deviceProperties.ModelNumber = modelNumber.Value;
var uuid = document.Descendants(uPnpNamespaces.ud.GetName("UDN")).FirstOrDefault();
if (uuid != null)
deviceProperties.UUID = uuid.Value;
var manufacturer = document.Descendants(uPnpNamespaces.ud.GetName("manufacturer")).FirstOrDefault();
if (manufacturer != null)
deviceProperties.Manufacturer = manufacturer.Value;
var manufacturerUrl = document.Descendants(uPnpNamespaces.ud.GetName("manufacturerURL")).FirstOrDefault();
if (manufacturerUrl != null)
deviceProperties.ManufacturerUrl = manufacturerUrl.Value;
var presentationUrl = document.Descendants(uPnpNamespaces.ud.GetName("presentationURL")).FirstOrDefault();
if (presentationUrl != null)
deviceProperties.PresentationUrl = presentationUrl.Value;
var modelUrl = document.Descendants(uPnpNamespaces.ud.GetName("modelURL")).FirstOrDefault();
if (modelUrl != null)
deviceProperties.ModelUrl = modelUrl.Value;
var serialNumber = document.Descendants(uPnpNamespaces.ud.GetName("serialNumber")).FirstOrDefault();
if (serialNumber != null)
deviceProperties.SerialNumber = serialNumber.Value;
var modelDescription = document.Descendants(uPnpNamespaces.ud.GetName("modelDescription")).FirstOrDefault();
if (modelDescription != null)
deviceProperties.ModelDescription = modelDescription.Value;
deviceProperties.BaseUrl = String.Format("http://{0}:{1}", url.Host, url.Port);
var icon = document.Descendants(uPnpNamespaces.ud.GetName("icon")).FirstOrDefault();
if (icon != null)
{
deviceProperties.Icon = CreateIcon(icon);
}
foreach (var services in document.Descendants(uPnpNamespaces.ud.GetName("serviceList")))
{
if (services == null)
continue;
var servicesList = services.Descendants(uPnpNamespaces.ud.GetName("service"));
if (servicesList == null)
continue;
foreach (var element in servicesList)
{
var service = Create(element);
if (service != null)
{
deviceProperties.Services.Add(service);
}
}
}
2016-11-04 08:31:05 +00:00
var device = new Device(deviceProperties, httpClient, logger, config, timerFactory);
2016-10-29 22:22:20 +00:00
return device;
}
#endregion
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
private static DeviceIcon CreateIcon(XElement element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
var mimeType = element.GetDescendantValue(uPnpNamespaces.ud.GetName("mimetype"));
var width = element.GetDescendantValue(uPnpNamespaces.ud.GetName("width"));
var height = element.GetDescendantValue(uPnpNamespaces.ud.GetName("height"));
var depth = element.GetDescendantValue(uPnpNamespaces.ud.GetName("depth"));
var url = element.GetDescendantValue(uPnpNamespaces.ud.GetName("url"));
2018-09-12 17:26:21 +00:00
var widthValue = int.Parse(width, NumberStyles.Integer, UsCulture);
var heightValue = int.Parse(height, NumberStyles.Integer, UsCulture);
2016-10-29 22:22:20 +00:00
return new DeviceIcon
{
Depth = depth,
Height = heightValue,
MimeType = mimeType,
Url = url,
Width = widthValue
};
}
private static DeviceService Create(XElement element)
{
var type = element.GetDescendantValue(uPnpNamespaces.ud.GetName("serviceType"));
var id = element.GetDescendantValue(uPnpNamespaces.ud.GetName("serviceId"));
var scpdUrl = element.GetDescendantValue(uPnpNamespaces.ud.GetName("SCPDURL"));
var controlURL = element.GetDescendantValue(uPnpNamespaces.ud.GetName("controlURL"));
var eventSubURL = element.GetDescendantValue(uPnpNamespaces.ud.GetName("eventSubURL"));
return new DeviceService
{
ControlUrl = controlURL,
EventSubUrl = eventSubURL,
ScpdUrl = scpdUrl,
ServiceId = id,
ServiceType = type
};
}
public event EventHandler<PlaybackStartEventArgs> PlaybackStart;
public event EventHandler<PlaybackProgressEventArgs> PlaybackProgress;
public event EventHandler<PlaybackStoppedEventArgs> PlaybackStopped;
public event EventHandler<MediaChangedEventArgs> MediaChanged;
public uBaseObject CurrentMediaInfo { get; private set; }
private void UpdateMediaInfo(uBaseObject mediaInfo, TRANSPORTSTATE state)
{
TransportState = state;
var previousMediaInfo = CurrentMediaInfo;
CurrentMediaInfo = mediaInfo;
if (previousMediaInfo == null && mediaInfo != null)
{
if (state != TRANSPORTSTATE.STOPPED)
{
OnPlaybackStart(mediaInfo);
}
}
else if (mediaInfo != null && previousMediaInfo != null && !mediaInfo.Equals(previousMediaInfo))
{
OnMediaChanged(previousMediaInfo, mediaInfo);
}
else if (mediaInfo == null && previousMediaInfo != null)
{
OnPlaybackStop(previousMediaInfo);
}
else if (mediaInfo != null && mediaInfo.Equals(previousMediaInfo))
{
OnPlaybackProgress(mediaInfo);
}
}
private void OnPlaybackStart(uBaseObject mediaInfo)
{
if (PlaybackStart != null)
{
PlaybackStart.Invoke(this, new PlaybackStartEventArgs
{
MediaInfo = mediaInfo
});
}
}
private void OnPlaybackProgress(uBaseObject mediaInfo)
{
2018-09-12 17:26:21 +00:00
var mediaUrl = mediaInfo.Url;
if (string.IsNullOrWhiteSpace(mediaUrl))
{
return;
}
2016-10-29 22:22:20 +00:00
if (PlaybackProgress != null)
{
PlaybackProgress.Invoke(this, new PlaybackProgressEventArgs
{
MediaInfo = mediaInfo
});
}
}
private void OnPlaybackStop(uBaseObject mediaInfo)
{
if (PlaybackStopped != null)
{
PlaybackStopped.Invoke(this, new PlaybackStoppedEventArgs
{
MediaInfo = mediaInfo
});
}
}
private void OnMediaChanged(uBaseObject old, uBaseObject newMedia)
{
if (MediaChanged != null)
{
MediaChanged.Invoke(this, new MediaChangedEventArgs
{
OldMediaInfo = old,
NewMediaInfo = newMedia
});
}
}
#region IDisposable
bool _disposed;
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
DisposeTimer();
}
}
private void DisposeTimer()
{
if (_timer != null)
{
_timer.Dispose();
_timer = null;
}
}
#endregion
public override string ToString()
{
return String.Format("{0} - {1}", Properties.Name, Properties.BaseUrl);
}
}
}