2014-04-26 02:55:07 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-03-14 17:09:22 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
using MediaBrowser.Dlna.Common;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
using System.Globalization;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
using System.Linq;
|
2014-04-26 02:55:07 +00:00
|
|
|
|
using System.Security;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Dlna.PlayTo
|
|
|
|
|
{
|
2014-03-14 17:09:22 +00:00
|
|
|
|
public class Device : IDisposable
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-03-18 17:35:38 +00:00
|
|
|
|
const string ServiceAvtransportType = "urn:schemas-upnp-org:service:AVTransport:1";
|
|
|
|
|
const string ServiceRenderingType = "urn:schemas-upnp-org:service:RenderingControl:1";
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
#region Fields & Properties
|
|
|
|
|
|
|
|
|
|
private Timer _timer;
|
2014-04-22 17:25:54 +00:00
|
|
|
|
private Timer _volumeTimer;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-13 19:08:02 +00:00
|
|
|
|
public DeviceInfo Properties { get; set; }
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
private int _muteVol;
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public bool IsMuted { get; set; }
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
public int Volume { get; set; }
|
|
|
|
|
|
|
|
|
|
public TimeSpan Duration { get; set; }
|
|
|
|
|
|
|
|
|
|
private TimeSpan _position = TimeSpan.FromSeconds(0);
|
|
|
|
|
public TimeSpan Position
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _position;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_position = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public TRANSPORTSTATE TransportState { get; private set; }
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
public bool IsPlaying
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-03-10 10:16:03 +00:00
|
|
|
|
return TransportState == TRANSPORTSTATE.PLAYING;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsPaused
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-03-10 10:16:03 +00:00
|
|
|
|
return TransportState == TRANSPORTSTATE.PAUSED || TransportState == TRANSPORTSTATE.PAUSED_PLAYBACK;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsStopped
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-03-10 10:16:03 +00:00
|
|
|
|
return TransportState == TRANSPORTSTATE.STOPPED;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly ILogger _logger;
|
2014-03-14 17:09:22 +00:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-14 17:09:22 +00:00
|
|
|
|
public Device(DeviceInfo deviceProperties, IHttpClient httpClient, ILogger logger, IServerConfigurationManager config)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
Properties = deviceProperties;
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_logger = logger;
|
2014-03-14 17:09:22 +00:00
|
|
|
|
_config = config;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-10 10:16:03 +00:00
|
|
|
|
private int GetPlaybackTimerIntervalMs()
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return 1000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetVolumeTimerIntervalMs()
|
|
|
|
|
{
|
|
|
|
|
return 5000;
|
2014-03-10 10:16:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetInactiveTimerIntervalMs()
|
|
|
|
|
{
|
|
|
|
|
return 20000;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
_timer = new Timer(TimerCallback, null, GetPlaybackTimerIntervalMs(), GetInactiveTimerIntervalMs());
|
|
|
|
|
|
|
|
|
|
_volumeTimer = new Timer(VolumeTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
_timerActive = false;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
private readonly object _timerLock = new object();
|
|
|
|
|
private bool _timerActive;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
private void RestartTimer()
|
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
if (!_timerActive)
|
|
|
|
|
{
|
|
|
|
|
lock (_timerLock)
|
|
|
|
|
{
|
|
|
|
|
if (!_timerActive)
|
|
|
|
|
{
|
2014-04-26 02:55:07 +00:00
|
|
|
|
_logger.Debug("RestartTimer");
|
2014-04-22 17:25:54 +00:00
|
|
|
|
_timer.Change(10, GetPlaybackTimerIntervalMs());
|
2014-03-10 10:16:03 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
_volumeTimer.Change(100, GetVolumeTimerIntervalMs());
|
|
|
|
|
}
|
2014-03-10 10:16:03 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
_timerActive = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-10 10:16:03 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Restarts the timer in inactive mode.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void RestartTimerInactive()
|
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
if (_timerActive)
|
|
|
|
|
{
|
|
|
|
|
lock (_timerLock)
|
|
|
|
|
{
|
|
|
|
|
if (_timerActive)
|
|
|
|
|
{
|
2014-04-26 02:55:07 +00:00
|
|
|
|
_logger.Debug("RestartTimerInactive");
|
2014-04-22 17:25:54 +00:00
|
|
|
|
var interval = GetInactiveTimerIntervalMs();
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
_timer.Change(interval, interval);
|
|
|
|
|
_volumeTimer.Change(Timeout.Infinite, Timeout.Infinite);
|
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
_timerActive = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Commanding
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public Task VolumeDown()
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
var sendVolume = Math.Max(Volume - 5, 0);
|
|
|
|
|
|
2014-03-10 10:10:16 +00:00
|
|
|
|
return SetVolume(sendVolume);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public Task VolumeUp()
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
var sendVolume = Math.Min(Volume + 5, 100);
|
|
|
|
|
|
2014-03-10 10:10:16 +00:00
|
|
|
|
return SetVolume(sendVolume);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task ToggleMute()
|
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
if (IsMuted)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return Unmute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Mute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Mute()
|
|
|
|
|
{
|
|
|
|
|
var success = await SetMute(true).ConfigureAwait(true);
|
|
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
await SetVolume(0).ConfigureAwait(false);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
2014-04-22 17:25:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Unmute()
|
|
|
|
|
{
|
|
|
|
|
var success = await SetMute(false).ConfigureAwait(true);
|
|
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
var sendVolume = _muteVol <= 0 ? 20 : _muteVol;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
await SetVolume(sendVolume).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 03:54:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets volume on a scale of 0-100
|
|
|
|
|
/// </summary>
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public async Task SetVolume(int value)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetVolume");
|
|
|
|
|
if (command == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-18 17:35:38 +00:00
|
|
|
|
var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceRenderingType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
if (service == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Unable to find service");
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
// Set it early and assume it will succeed
|
|
|
|
|
// Remote control will perform better
|
2014-03-10 10:10:16 +00:00
|
|
|
|
Volume = value;
|
2014-04-22 17:25:54 +00:00
|
|
|
|
|
|
|
|
|
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType, value))
|
|
|
|
|
.ConfigureAwait(false);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public async Task Seek(TimeSpan value)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "Seek");
|
|
|
|
|
if (command == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-18 17:35:38 +00:00
|
|
|
|
var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceAvtransportType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
if (service == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Unable to find service");
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +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"))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public async Task SetAvTransport(string url, string header, string metaData)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-26 02:55:07 +00:00
|
|
|
|
//await SetStop().ConfigureAwait(false);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetAVTransportURI");
|
|
|
|
|
if (command == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
var dictionary = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"CurrentURI", url},
|
|
|
|
|
{"CurrentURIMetaData", CreateDidlMeta(metaData)}
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-18 17:35:38 +00:00
|
|
|
|
var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceAvtransportType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
if (service == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Unable to find service");
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, AvCommands.BuildPost(command, service.ServiceType, url, dictionary), header)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2014-03-14 14:27:32 +00:00
|
|
|
|
await Task.Delay(50).ConfigureAwait(false);
|
|
|
|
|
await SetPlay().ConfigureAwait(false);
|
|
|
|
|
|
2014-03-10 10:10:16 +00:00
|
|
|
|
RestartTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string CreateDidlMeta(string value)
|
|
|
|
|
{
|
2014-04-23 16:29:21 +00:00
|
|
|
|
if (string.IsNullOrEmpty(value))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
return String.Empty;
|
|
|
|
|
|
2014-04-23 16:29:21 +00:00
|
|
|
|
return SecurityElement.Escape(value);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public async Task SetNextAvTransport(string value, string header, string metaData)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetNextAVTransportURI");
|
|
|
|
|
if (command == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
var dictionary = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"NextURI", value},
|
|
|
|
|
{"NextURIMetaData", CreateDidlMeta(metaData)}
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-18 17:35:38 +00:00
|
|
|
|
var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceAvtransportType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
if (service == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Unable to find service");
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, AvCommands.BuildPost(command, service.ServiceType, value, dictionary), header)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
RestartTimer();
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public async Task SetPlay()
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "Play");
|
|
|
|
|
if (command == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-18 17:35:38 +00:00
|
|
|
|
var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceAvtransportType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
if (service == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Unable to find service");
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, AvCommands.BuildPost(command, service.ServiceType, 1))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public async Task SetStop()
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "Stop");
|
|
|
|
|
if (command == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-26 02:55:07 +00:00
|
|
|
|
var service = Properties.Services.First(s => s.ServiceType == ServiceAvtransportType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, AvCommands.BuildPost(command, service.ServiceType, 1))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public async Task SetPause()
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "Pause");
|
|
|
|
|
if (command == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-26 02:55:07 +00:00
|
|
|
|
var service = Properties.Services.First(s => s.ServiceType == ServiceAvtransportType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, AvCommands.BuildPost(command, service.ServiceType, 1))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
TransportState = TRANSPORTSTATE.PAUSED;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Get data
|
|
|
|
|
|
2014-04-26 02:55:07 +00:00
|
|
|
|
private int _successiveStopCount;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
private async void TimerCallback(object sender)
|
|
|
|
|
{
|
|
|
|
|
if (_disposed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
var transportState = await GetTransportInfo().ConfigureAwait(false);
|
2014-03-10 10:16:03 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
if (transportState.HasValue)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
// If we're not playing anything no need to get additional data
|
|
|
|
|
if (transportState.Value == TRANSPORTSTATE.STOPPED)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
UpdateMediaInfo(null, transportState.Value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var tuple = await GetPositionInfo().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var currentObject = tuple.Item2;
|
|
|
|
|
|
|
|
|
|
if (tuple.Item1 && currentObject == null)
|
|
|
|
|
{
|
|
|
|
|
currentObject = await GetMediaInfo().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentObject != null)
|
2014-03-10 10:16:03 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
UpdateMediaInfo(currentObject, transportState.Value);
|
2014-03-10 10:16:03 +00:00
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-27 03:42:05 +00:00
|
|
|
|
if (_disposed)
|
|
|
|
|
return;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-27 03:42:05 +00:00
|
|
|
|
// 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)
|
|
|
|
|
{
|
|
|
|
|
_successiveStopCount++;
|
2014-04-26 02:55:07 +00:00
|
|
|
|
|
2014-04-27 03:42:05 +00:00
|
|
|
|
if (_successiveStopCount >= 10)
|
|
|
|
|
{
|
|
|
|
|
RestartTimerInactive();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_successiveStopCount = 0;
|
|
|
|
|
RestartTimer();
|
|
|
|
|
}
|
2014-04-26 02:55:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-27 03:42:05 +00:00
|
|
|
|
catch (Exception ex)
|
2014-04-26 02:55:07 +00:00
|
|
|
|
{
|
2014-04-27 03:42:05 +00:00
|
|
|
|
_logger.ErrorException("Error updating device info", ex);
|
2014-04-26 02:55:07 +00:00
|
|
|
|
}
|
2014-04-22 17:25:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void VolumeTimerCallback(object sender)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await GetVolume().ConfigureAwait(false);
|
|
|
|
|
await GetMute().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error updating device info", ex);
|
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task GetVolume()
|
|
|
|
|
{
|
|
|
|
|
var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetVolume");
|
|
|
|
|
if (command == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-03-18 17:35:38 +00:00
|
|
|
|
var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceRenderingType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
if (service == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Unable to find service");
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-14 17:09:22 +00:00
|
|
|
|
var result = await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType))
|
2014-03-10 10:10:16 +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;
|
|
|
|
|
|
2014-04-01 22:23:07 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(volumeValue))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2014-04-01 22:23:07 +00:00
|
|
|
|
Volume = int.Parse(volumeValue, UsCulture);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
if (Volume > 0)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
_muteVol = Volume;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
private async Task GetMute()
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetMute");
|
2014-03-10 10:10:16 +00:00
|
|
|
|
if (command == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceRenderingType);
|
|
|
|
|
|
2014-03-10 10:10:16 +00:00
|
|
|
|
if (service == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
{
|
|
|
|
|
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))
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (result == null || result.Document == null)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<TRANSPORTSTATE?> GetTransportInfo()
|
|
|
|
|
{
|
|
|
|
|
var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetTransportInfo");
|
|
|
|
|
if (command == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceAvtransportType);
|
|
|
|
|
if (service == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2014-04-08 13:23:49 +00:00
|
|
|
|
var result = await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, AvCommands.BuildPost(command, service.ServiceType))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (result == null || result.Document == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return null;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
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)
|
2014-04-19 17:43:12 +00:00
|
|
|
|
{
|
|
|
|
|
TRANSPORTSTATE state;
|
|
|
|
|
|
|
|
|
|
if (Enum.TryParse(transportStateValue, true, out state))
|
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return state;
|
2014-04-19 17:43:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return null;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
private async Task<uBaseObject> GetMediaInfo()
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetMediaInfo");
|
|
|
|
|
if (command == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return null;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-18 17:35:38 +00:00
|
|
|
|
var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceAvtransportType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
if (service == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Unable to find service");
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-14 17:09:22 +00:00
|
|
|
|
var result = await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (result == null || result.Document == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return null;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
var track = result.Document.Descendants("CurrentURIMetaData").FirstOrDefault();
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
if (track == null)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return null;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
var e = track.Element(uPnpNamespaces.items) ?? track;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return UpnpContainer.Create(e);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
private async Task<Tuple<bool, uBaseObject>> GetPositionInfo()
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
var command = AvCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetPositionInfo");
|
|
|
|
|
if (command == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return new Tuple<bool, uBaseObject>(false, null);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-18 17:35:38 +00:00
|
|
|
|
var service = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceAvtransportType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
if (service == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Unable to find service");
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-14 17:09:22 +00:00
|
|
|
|
var result = await new SsdpHttpClient(_httpClient, _config).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (result == null || result.Document == null)
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return new Tuple<bool, uBaseObject>(false, null);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
var durationElem = result.Document.Descendants(uPnpNamespaces.AvTransport + "GetPositionInfoResponse").Select(i => i.Element("TrackDuration")).FirstOrDefault(i => i != null);
|
|
|
|
|
var duration = durationElem == null ? null : durationElem.Value;
|
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(duration) && !string.Equals(duration, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-01 22:23:07 +00:00
|
|
|
|
Duration = TimeSpan.Parse(duration, UsCulture);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var positionElem = result.Document.Descendants(uPnpNamespaces.AvTransport + "GetPositionInfoResponse").Select(i => i.Element("RelTime")).FirstOrDefault(i => i != null);
|
|
|
|
|
var position = positionElem == null ? null : positionElem.Value;
|
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(position) && !string.Equals(position, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-01 22:23:07 +00:00
|
|
|
|
Position = TimeSpan.Parse(position, UsCulture);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
var track = result.Document.Descendants("TrackMetaData").FirstOrDefault();
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
if (track == null)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
//If track is null, some vendors do this, use GetMediaInfo instead
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return new Tuple<bool, uBaseObject>(true, null);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 15:06:54 +00:00
|
|
|
|
var trackString = (string)track;
|
2014-04-08 04:17:18 +00:00
|
|
|
|
|
2014-04-08 12:27:22 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(trackString) || string.Equals(trackString, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2014-04-23 16:29:21 +00:00
|
|
|
|
return new Tuple<bool, uBaseObject>(false, null);
|
2014-04-08 12:27:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-08 04:17:18 +00:00
|
|
|
|
XElement uPnpResponse;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2014-04-08 04:17:18 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
uPnpResponse = XElement.Parse(trackString);
|
|
|
|
|
}
|
2014-04-23 16:29:21 +00:00
|
|
|
|
catch (Exception ex)
|
2014-04-08 04:17:18 +00:00
|
|
|
|
{
|
2014-04-23 16:29:21 +00:00
|
|
|
|
_logger.ErrorException("Unable to parse xml {0}", ex, trackString);
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return new Tuple<bool, uBaseObject>(true, null);
|
2014-04-08 04:17:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var e = uPnpResponse.Element(uPnpNamespaces.items);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
var uTrack = CreateUBaseObject(e);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
return new Tuple<bool, uBaseObject>(true, uTrack);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
private static uBaseObject CreateUBaseObject(XElement container)
|
|
|
|
|
{
|
|
|
|
|
if (container == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("container");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 = container.GetValue(uPnpNamespaces.Res),
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-10 10:10:16 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region From XML
|
|
|
|
|
|
|
|
|
|
private async Task GetAVProtocolAsync()
|
|
|
|
|
{
|
2014-03-18 17:35:38 +00:00
|
|
|
|
var avService = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceAvtransportType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
if (avService == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-03-13 19:08:02 +00:00
|
|
|
|
var url = avService.ScpdUrl;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
if (!url.Contains("/"))
|
|
|
|
|
url = "/dmr/" + url;
|
|
|
|
|
if (!url.StartsWith("/"))
|
|
|
|
|
url = "/" + url;
|
|
|
|
|
|
2014-03-14 17:09:22 +00:00
|
|
|
|
var httpClient = new SsdpHttpClient(_httpClient, _config);
|
2014-03-25 05:25:03 +00:00
|
|
|
|
var document = await httpClient.GetDataAsync(Properties.BaseUrl + url);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
AvCommands = TransportCommands.Create(document);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task GetRenderingProtocolAsync()
|
|
|
|
|
{
|
2014-03-18 17:35:38 +00:00
|
|
|
|
var avService = Properties.Services.FirstOrDefault(s => s.ServiceType == ServiceRenderingType);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
if (avService == null)
|
|
|
|
|
return;
|
2014-03-13 19:08:02 +00:00
|
|
|
|
string url = avService.ScpdUrl;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
if (!url.Contains("/"))
|
|
|
|
|
url = "/dmr/" + url;
|
|
|
|
|
if (!url.StartsWith("/"))
|
|
|
|
|
url = "/" + url;
|
|
|
|
|
|
2014-03-14 17:09:22 +00:00
|
|
|
|
var httpClient = new SsdpHttpClient(_httpClient, _config);
|
2014-03-25 05:25:03 +00:00
|
|
|
|
var document = await httpClient.GetDataAsync(Properties.BaseUrl + url);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
RendererCommands = TransportCommands.Create(document);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 19:30:21 +00:00
|
|
|
|
private TransportCommands AvCommands
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal TransportCommands RendererCommands
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-14 17:09:22 +00:00
|
|
|
|
public static async Task<Device> CreateuPnpDeviceAsync(Uri url, IHttpClient httpClient, IServerConfigurationManager config, ILogger logger)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-03-14 17:09:22 +00:00
|
|
|
|
var ssdpHttpClient = new SsdpHttpClient(httpClient, config);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-25 05:25:03 +00:00
|
|
|
|
var document = await ssdpHttpClient.GetDataAsync(url.ToString()).ConfigureAwait(false);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-13 19:08:02 +00:00
|
|
|
|
var deviceProperties = new DeviceInfo();
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
var name = document.Descendants(uPnpNamespaces.ud.GetName("friendlyName")).FirstOrDefault();
|
|
|
|
|
if (name != null)
|
|
|
|
|
deviceProperties.Name = name.Value;
|
|
|
|
|
|
|
|
|
|
var name2 = document.Descendants(uPnpNamespaces.ud.GetName("roomName")).FirstOrDefault();
|
|
|
|
|
if (name2 != null)
|
|
|
|
|
deviceProperties.Name = name2.Value;
|
|
|
|
|
|
|
|
|
|
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;
|
2014-03-25 05:25:03 +00:00
|
|
|
|
|
2014-03-24 07:47:23 +00:00
|
|
|
|
var modelUrl = document.Descendants(uPnpNamespaces.ud.GetName("modelURL")).FirstOrDefault();
|
|
|
|
|
if (modelUrl != null)
|
|
|
|
|
deviceProperties.ModelUrl = modelUrl.Value;
|
2014-03-25 05:25:03 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
deviceProperties.BaseUrl = String.Format("http://{0}:{1}", url.Host, url.Port);
|
|
|
|
|
|
|
|
|
|
var icon = document.Descendants(uPnpNamespaces.ud.GetName("icon")).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (icon != null)
|
|
|
|
|
{
|
2014-04-01 22:23:07 +00:00
|
|
|
|
deviceProperties.Icon = CreateIcon(icon);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var isRenderer = false;
|
|
|
|
|
|
|
|
|
|
foreach (var services in document.Descendants(uPnpNamespaces.ud.GetName("serviceList")))
|
|
|
|
|
{
|
|
|
|
|
if (services == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var servicesList = services.Descendants(uPnpNamespaces.ud.GetName("service"));
|
|
|
|
|
|
|
|
|
|
if (servicesList == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
foreach (var element in servicesList)
|
|
|
|
|
{
|
2014-03-13 19:08:02 +00:00
|
|
|
|
var service = Create(element);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
if (service != null)
|
|
|
|
|
{
|
|
|
|
|
deviceProperties.Services.Add(service);
|
2014-03-18 17:35:38 +00:00
|
|
|
|
if (service.ServiceType == ServiceAvtransportType)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
isRenderer = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isRenderer)
|
|
|
|
|
{
|
2014-03-14 17:09:22 +00:00
|
|
|
|
var device = new Device(deviceProperties, httpClient, logger, config);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
await device.GetRenderingProtocolAsync().ConfigureAwait(false);
|
|
|
|
|
await device.GetAVProtocolAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return device;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2014-04-01 22:23:07 +00:00
|
|
|
|
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"));
|
|
|
|
|
|
|
|
|
|
var widthValue = int.Parse(width, NumberStyles.Any, UsCulture);
|
|
|
|
|
var heightValue = int.Parse(height, NumberStyles.Any, UsCulture);
|
|
|
|
|
|
|
|
|
|
return new DeviceIcon
|
|
|
|
|
{
|
|
|
|
|
Depth = depth,
|
|
|
|
|
Height = heightValue,
|
|
|
|
|
MimeType = mimeType,
|
|
|
|
|
Url = url,
|
|
|
|
|
Width = widthValue
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-13 19:08:02 +00:00
|
|
|
|
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"));
|
|
|
|
|
|
2014-04-10 15:06:54 +00:00
|
|
|
|
return new DeviceService
|
|
|
|
|
{
|
|
|
|
|
ControlUrl = controlURL,
|
|
|
|
|
EventSubUrl = eventSubURL,
|
|
|
|
|
ScpdUrl = scpdUrl,
|
|
|
|
|
ServiceId = id,
|
|
|
|
|
ServiceType = type
|
|
|
|
|
};
|
2014-03-13 19:08:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
public event EventHandler<PlaybackStartEventArgs> PlaybackStart;
|
|
|
|
|
public event EventHandler<PlaybackProgressEventArgs> PlaybackProgress;
|
|
|
|
|
public event EventHandler<PlaybackStoppedEventArgs> PlaybackStopped;
|
|
|
|
|
|
|
|
|
|
private uBaseObject _lastMediaInfo;
|
|
|
|
|
private void UpdateMediaInfo(uBaseObject mediaInfo, TRANSPORTSTATE state)
|
|
|
|
|
{
|
|
|
|
|
TransportState = state;
|
|
|
|
|
|
|
|
|
|
var previousMediaInfo = _lastMediaInfo;
|
|
|
|
|
_lastMediaInfo = mediaInfo;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
if (previousMediaInfo == null && mediaInfo != null)
|
|
|
|
|
{
|
|
|
|
|
OnPlaybackStart(mediaInfo);
|
|
|
|
|
}
|
|
|
|
|
else if (mediaInfo == null && previousMediaInfo != null)
|
|
|
|
|
{
|
|
|
|
|
OnPlaybackStop(previousMediaInfo);
|
|
|
|
|
}
|
|
|
|
|
else if (mediaInfo != null && mediaInfo.Equals(previousMediaInfo))
|
|
|
|
|
{
|
|
|
|
|
OnPlaybackProgress(mediaInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
private void OnPlaybackStart(uBaseObject mediaInfo)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
if (PlaybackStart != null)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
PlaybackStart.Invoke(this, new PlaybackStartEventArgs
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
MediaInfo = mediaInfo
|
2014-03-10 10:10:16 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
private void OnPlaybackProgress(uBaseObject mediaInfo)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-22 17:25:54 +00:00
|
|
|
|
if (PlaybackProgress != null)
|
|
|
|
|
{
|
|
|
|
|
PlaybackProgress.Invoke(this, new PlaybackProgressEventArgs
|
|
|
|
|
{
|
|
|
|
|
MediaInfo = mediaInfo
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 17:25:54 +00:00
|
|
|
|
private void OnPlaybackStop(uBaseObject mediaInfo)
|
|
|
|
|
{
|
|
|
|
|
if (PlaybackStopped != null)
|
|
|
|
|
{
|
|
|
|
|
PlaybackStopped.Invoke(this, new PlaybackStoppedEventArgs
|
|
|
|
|
{
|
|
|
|
|
MediaInfo = mediaInfo
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
#region IDisposable
|
|
|
|
|
|
|
|
|
|
bool _disposed;
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (!_disposed)
|
|
|
|
|
{
|
|
|
|
|
_disposed = true;
|
2014-04-22 17:25:54 +00:00
|
|
|
|
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
DisposeVolumeTimer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DisposeTimer()
|
|
|
|
|
{
|
|
|
|
|
if (_timer != null)
|
|
|
|
|
{
|
2014-03-10 10:10:16 +00:00
|
|
|
|
_timer.Dispose();
|
2014-04-22 17:25:54 +00:00
|
|
|
|
_timer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DisposeVolumeTimer()
|
|
|
|
|
{
|
|
|
|
|
if (_volumeTimer != null)
|
|
|
|
|
{
|
|
|
|
|
_volumeTimer.Dispose();
|
|
|
|
|
_volumeTimer = null;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return String.Format("{0} - {1}", Properties.Name, Properties.BaseUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|