jellyfin/Emby.Dlna/PlayTo/Device.cs

1270 lines
43 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
2016-10-30 00:22:20 +02:00
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
2020-07-19 21:31:14 +02:00
using System.Security;
2016-10-30 00:22:20 +02:00
using System.Threading;
using System.Threading.Tasks;
2019-02-13 20:23:13 +01:00
using System.Xml;
2016-10-30 00:22:20 +02:00
using System.Xml.Linq;
2019-01-13 20:16:19 +01:00
using Emby.Dlna.Common;
using Emby.Dlna.Ssdp;
using Microsoft.Extensions.Logging;
2016-10-30 00:22:20 +02:00
2016-10-30 00:34:54 +02:00
namespace Emby.Dlna.PlayTo
2016-10-30 00:22:20 +02:00
{
public class Device : IDisposable
{
private readonly IHttpClientFactory _httpClientFactory;
2020-08-20 21:04:57 +02:00
private readonly ILogger _logger;
private readonly object _timerLock = new object();
2023-07-29 21:35:38 +02:00
private Timer? _timer;
2020-08-20 21:04:57 +02:00
private int _muteVol;
private int _volume;
private DateTime _lastVolumeRefresh;
private bool _volumeRefreshActive;
private int _connectFailureCount;
private bool _disposed;
public Device(DeviceInfo deviceProperties, IHttpClientFactory httpClientFactory, ILogger logger)
2020-08-20 21:04:57 +02:00
{
Properties = deviceProperties;
_httpClientFactory = httpClientFactory;
2020-08-20 21:04:57 +02:00
_logger = logger;
}
2023-07-29 21:35:38 +02:00
public event EventHandler<PlaybackStartEventArgs>? PlaybackStart;
2020-08-20 21:04:57 +02:00
2023-07-29 21:35:38 +02:00
public event EventHandler<PlaybackProgressEventArgs>? PlaybackProgress;
2020-08-20 21:04:57 +02:00
2023-07-29 21:35:38 +02:00
public event EventHandler<PlaybackStoppedEventArgs>? PlaybackStopped;
2020-08-20 21:04:57 +02:00
2023-07-29 21:35:38 +02:00
public event EventHandler<MediaChangedEventArgs>? MediaChanged;
2016-10-30 00:22:20 +02:00
public DeviceInfo Properties { get; set; }
public bool IsMuted { get; set; }
public int Volume
{
get
{
2020-05-25 23:52:51 +02:00
RefreshVolumeIfNeeded().GetAwaiter().GetResult();
2016-10-30 00:22:20 +02:00
return _volume;
}
2020-06-15 23:43:52 +02:00
set => _volume = value;
2016-10-30 00:22:20 +02:00
}
public TimeSpan? Duration { get; set; }
public TimeSpan Position { get; set; } = TimeSpan.FromSeconds(0);
2016-10-30 00:22:20 +02:00
2020-08-20 17:01:04 +02:00
public TransportState TransportState { get; private set; }
2016-10-30 00:22:20 +02:00
public bool IsPlaying => TransportState == TransportState.PLAYING;
2016-10-30 00:22:20 +02:00
public bool IsPaused => TransportState == TransportState.PAUSED_PLAYBACK;
2016-10-30 00:22:20 +02:00
public bool IsStopped => TransportState == TransportState.STOPPED;
2016-10-30 00:22:20 +02:00
2023-07-29 21:35:38 +02:00
public Action? OnDeviceUnavailable { get; set; }
2020-06-20 08:20:33 +02:00
2023-07-29 21:35:38 +02:00
private TransportCommands? AvCommands { get; set; }
2020-06-20 08:20:33 +02:00
2023-07-29 21:35:38 +02:00
private TransportCommands? RendererCommands { get; set; }
2016-10-30 00:22:20 +02:00
2023-07-29 21:35:38 +02:00
public UBaseObject? CurrentMediaInfo { get; private set; }
2016-10-30 00:22:20 +02:00
public void Start()
{
_logger.LogDebug("Dlna Device.Start");
2019-02-05 09:49:46 +01:00
_timer = new Timer(TimerCallback, null, 1000, Timeout.Infinite);
2016-10-30 00:22:20 +02:00
}
2020-05-25 23:52:51 +02:00
private Task RefreshVolumeIfNeeded()
2016-10-30 00:22:20 +02:00
{
2020-05-25 23:52:51 +02:00
if (_volumeRefreshActive
&& DateTime.UtcNow >= _lastVolumeRefresh.AddSeconds(5))
2016-10-30 00:22:20 +02:00
{
_lastVolumeRefresh = DateTime.UtcNow;
2020-05-25 23:52:51 +02:00
return RefreshVolume();
2016-10-30 00:22:20 +02:00
}
2020-05-25 23:52:51 +02:00
return Task.CompletedTask;
2016-10-30 00:22:20 +02:00
}
2020-05-25 23:52:51 +02:00
private async Task RefreshVolume(CancellationToken cancellationToken = default)
2016-10-30 00:22:20 +02:00
{
if (_disposed)
2020-05-25 23:52:51 +02:00
{
2016-10-30 00:22:20 +02:00
return;
2020-05-25 23:52:51 +02:00
}
2016-10-30 00:22:20 +02:00
try
{
2018-09-12 19:26:21 +02:00
await GetVolume(cancellationToken).ConfigureAwait(false);
await GetMute(cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
}
catch (Exception ex)
{
2018-12-20 13:39:58 +01:00
_logger.LogError(ex, "Error updating device volume info for {DeviceName}", Properties.Name);
2016-10-30 00:22:20 +02:00
}
}
2018-09-12 19:26:21 +02:00
private void RestartTimer(bool immediate = false)
2016-10-30 00:22:20 +02:00
{
2017-08-10 22:06:36 +02:00
lock (_timerLock)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
if (_disposed)
{
2018-09-12 19:26:21 +02:00
return;
}
2018-09-12 19:26:21 +02:00
_volumeRefreshActive = true;
2017-08-10 22:06:36 +02:00
2018-09-12 19:26:21 +02:00
var time = immediate ? 100 : 10000;
2023-07-29 21:35:38 +02:00
_timer?.Change(time, Timeout.Infinite);
2016-10-30 00:22:20 +02:00
}
}
/// <summary>
/// Restarts the timer in inactive mode.
/// </summary>
private void RestartTimerInactive()
{
2017-08-10 22:06:36 +02:00
lock (_timerLock)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
if (_disposed)
{
2018-09-12 19:26:21 +02:00
return;
}
2016-10-30 00:22:20 +02:00
2018-09-12 19:26:21 +02:00
_volumeRefreshActive = false;
2017-08-10 22:06:36 +02:00
2023-07-29 21:35:38 +02:00
_timer?.Change(Timeout.Infinite, Timeout.Infinite);
2016-10-30 00:22:20 +02:00
}
}
2018-09-12 19:26:21 +02:00
public Task VolumeDown(CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
var sendVolume = Math.Max(Volume - 5, 0);
2018-09-12 19:26:21 +02:00
return SetVolume(sendVolume, cancellationToken);
2016-10-30 00:22:20 +02:00
}
2018-09-12 19:26:21 +02:00
public Task VolumeUp(CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
var sendVolume = Math.Min(Volume + 5, 100);
2018-09-12 19:26:21 +02:00
return SetVolume(sendVolume, cancellationToken);
2016-10-30 00:22:20 +02:00
}
2018-09-12 19:26:21 +02:00
public Task ToggleMute(CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
if (IsMuted)
{
2019-02-05 18:16:10 +01:00
return Unmute(cancellationToken);
2016-10-30 00:22:20 +02:00
}
2018-09-12 19:26:21 +02:00
return Mute(cancellationToken);
2016-10-30 00:22:20 +02:00
}
2018-09-12 19:26:21 +02:00
public async Task Mute(CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var success = await SetMute(true, cancellationToken).ConfigureAwait(true);
2016-10-30 00:22:20 +02:00
if (!success)
{
2018-09-12 19:26:21 +02:00
await SetVolume(0, cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
}
}
2019-02-05 18:16:10 +01:00
public async Task Unmute(CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var success = await SetMute(false, cancellationToken).ConfigureAwait(true);
2016-10-30 00:22:20 +02:00
if (!success)
{
var sendVolume = _muteVol <= 0 ? 20 : _muteVol;
2018-09-12 19:26:21 +02:00
await SetVolume(sendVolume, cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
}
}
2023-07-29 21:35:38 +02:00
private DeviceService? GetServiceRenderingControl()
2016-12-15 08:12:52 +01:00
{
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));
}
2023-07-29 21:35:38 +02:00
private DeviceService? GetAvTransportService()
2016-12-15 08:12:52 +01:00
{
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 19:26:21 +02:00
private async Task<bool> SetMute(bool mute, CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
2021-03-22 18:21:12 +01:00
var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "SetMute");
2022-12-05 15:00:20 +01:00
if (command is null)
2020-06-20 11:12:36 +02:00
{
2016-10-30 00:22:20 +02:00
return false;
2020-06-20 11:12:36 +02:00
}
2016-10-30 00:22:20 +02:00
2016-12-15 08:12:52 +01:00
var service = GetServiceRenderingControl();
2016-10-30 00:22:20 +02:00
2022-12-05 15:00:20 +01:00
if (service is null)
2016-10-30 00:22:20 +02:00
{
return false;
}
_logger.LogDebug("Setting mute");
2016-10-30 00:22:20 +02:00
var value = mute ? 1 : 0;
2022-03-08 22:57:47 +01:00
await new DlnaHttpClient(_logger, _httpClientFactory)
2021-02-15 14:19:08 +01:00
.SendCommandAsync(
Properties.BaseUrl,
service,
command.Name,
2023-07-29 21:35:38 +02:00
rendererCommands!.BuildPost(command, service.ServiceType, value), // null checked above
2021-02-15 14:19:08 +01:00
cancellationToken: cancellationToken)
2016-10-30 00:22:20 +02:00
.ConfigureAwait(false);
IsMuted = mute;
return true;
}
/// <summary>
/// Sets volume on a scale of 0-100.
2016-10-30 00:22:20 +02:00
/// </summary>
2020-08-20 21:04:57 +02:00
/// <param name="value">The volume on a scale of 0-100.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
2018-09-12 19:26:21 +02:00
public async Task SetVolume(int value, CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
2021-03-22 18:21:12 +01:00
var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "SetVolume");
2022-12-05 15:00:20 +01:00
if (command is null)
2020-06-20 11:12:36 +02:00
{
2016-10-30 00:22:20 +02:00
return;
2020-06-20 11:12:36 +02:00
}
2016-10-30 00:22:20 +02:00
2023-07-29 21:35:38 +02:00
var service = GetServiceRenderingControl() ?? throw new InvalidOperationException("Unable to find service");
2016-10-30 00:22:20 +02:00
// Set it early and assume it will succeed
// Remote control will perform better
Volume = value;
2022-03-08 22:57:47 +01:00
await new DlnaHttpClient(_logger, _httpClientFactory)
2021-02-15 14:19:08 +01:00
.SendCommandAsync(
Properties.BaseUrl,
service,
command.Name,
2023-07-29 21:35:38 +02:00
rendererCommands!.BuildPost(command, service.ServiceType, value), // null checked above
2021-02-15 14:19:08 +01:00
cancellationToken: cancellationToken)
2016-10-30 00:22:20 +02:00
.ConfigureAwait(false);
}
2018-09-12 19:26:21 +02:00
public async Task Seek(TimeSpan value, CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
2021-03-22 18:21:12 +01:00
var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "Seek");
2022-12-05 15:00:20 +01:00
if (command is null)
2020-06-20 11:12:36 +02:00
{
2016-10-30 00:22:20 +02:00
return;
2020-06-20 11:12:36 +02:00
}
2016-10-30 00:22:20 +02:00
2023-07-29 21:35:38 +02:00
var service = GetAvTransportService() ?? throw new InvalidOperationException("Unable to find service");
2022-03-08 22:57:47 +01:00
await new DlnaHttpClient(_logger, _httpClientFactory)
2021-02-15 14:19:08 +01:00
.SendCommandAsync(
Properties.BaseUrl,
service,
command.Name,
2023-07-29 21:35:38 +02:00
avCommands!.BuildPost(command, service.ServiceType, string.Format(CultureInfo.InvariantCulture, "{0:hh}:{0:mm}:{0:ss}", value), "REL_TIME"), // null checked above
2021-02-15 14:19:08 +01:00
cancellationToken: cancellationToken)
2016-10-30 00:22:20 +02:00
.ConfigureAwait(false);
2018-09-12 19:26:21 +02:00
RestartTimer(true);
2016-10-30 00:22:20 +02:00
}
2023-07-29 21:35:38 +02:00
public async Task SetAvTransport(string url, string? header, string metaData, CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
2020-08-07 19:26:28 +02:00
url = url.Replace("&", "&amp;", StringComparison.Ordinal);
2018-09-12 19:26:21 +02:00
_logger.LogDebug("{0} - SetAvTransport Uri: {1} DlnaHeaders: {2}", Properties.Name, url, header);
2016-10-30 00:22:20 +02:00
2021-03-22 18:21:12 +01:00
var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "SetAVTransportURI");
2022-12-05 15:00:20 +01:00
if (command is null)
2020-06-20 11:12:36 +02:00
{
2016-10-30 00:22:20 +02:00
return;
2020-06-20 11:12:36 +02:00
}
2016-10-30 00:22:20 +02:00
var dictionary = new Dictionary<string, string>
{
2020-08-07 19:26:28 +02:00
{ "CurrentURI", url },
{ "CurrentURIMetaData", CreateDidlMeta(metaData) }
2016-10-30 00:22:20 +02:00
};
2023-07-29 21:35:38 +02:00
var service = GetAvTransportService() ?? throw new InvalidOperationException("Unable to find service");
var post = avCommands!.BuildPost(command, service.ServiceType, url, dictionary); // null checked above
2022-03-08 22:57:47 +01:00
await new DlnaHttpClient(_logger, _httpClientFactory)
2021-02-15 14:19:08 +01:00
.SendCommandAsync(
Properties.BaseUrl,
service,
command.Name,
post,
header: header,
cancellationToken: cancellationToken)
2016-10-30 00:22:20 +02:00
.ConfigureAwait(false);
2021-02-15 14:19:08 +01:00
await Task.Delay(50, cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
try
{
2021-02-15 14:19:08 +01:00
await SetPlay(avCommands, cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02: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 19:26:21 +02:00
RestartTimer(true);
2016-10-30 00:22:20 +02:00
}
/*
* SetNextAvTransport is used to specify to the DLNA device what is the next track to play.
* Without that information, the next track command on the device does not work.
*/
2023-07-29 21:35:38 +02:00
public async Task SetNextAvTransport(string url, string? header, string metaData, CancellationToken cancellationToken = default)
{
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
url = url.Replace("&", "&amp;", StringComparison.Ordinal);
_logger.LogDebug("{PropertyName} - SetNextAvTransport Uri: {Url} DlnaHeaders: {Header}", Properties.Name, url, header);
2023-07-29 21:35:38 +02:00
var command = avCommands?.ServiceActions.FirstOrDefault(c => string.Equals(c.Name, "SetNextAVTransportURI", StringComparison.OrdinalIgnoreCase));
2022-12-05 15:00:20 +01:00
if (command is null)
{
return;
}
var dictionary = new Dictionary<string, string>
{
{ "NextURI", url },
{ "NextURIMetaData", CreateDidlMeta(metaData) }
};
2023-07-29 21:35:38 +02:00
var service = GetAvTransportService() ?? throw new InvalidOperationException("Unable to find service");
var post = avCommands!.BuildPost(command, service.ServiceType, url, dictionary); // null checked above
2022-03-08 22:57:47 +01:00
await new DlnaHttpClient(_logger, _httpClientFactory)
.SendCommandAsync(Properties.BaseUrl, service, command.Name, post, header, cancellationToken)
.ConfigureAwait(false);
}
2020-12-02 15:38:52 +01:00
private static string CreateDidlMeta(string value)
2016-10-30 00:22:20 +02:00
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
2016-10-30 00:22:20 +02:00
2020-07-19 21:31:14 +02:00
return SecurityElement.Escape(value);
2016-10-30 00:22:20 +02:00
}
2018-09-12 19:26:21 +02:00
private Task SetPlay(TransportCommands avCommands, CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "Play");
2022-12-05 15:00:20 +01:00
if (command is null)
{
2018-09-12 19:26:21 +02:00
return Task.CompletedTask;
}
2016-10-30 00:22:20 +02:00
2023-07-29 21:35:38 +02:00
var service = GetAvTransportService() ?? throw new InvalidOperationException("Unable to find service");
2022-03-08 22:57:47 +01:00
return new DlnaHttpClient(_logger, _httpClientFactory).SendCommandAsync(
2020-04-02 16:49:58 +02:00
Properties.BaseUrl,
service,
command.Name,
avCommands.BuildPost(command, service.ServiceType, 1),
cancellationToken: cancellationToken);
2016-10-30 00:22:20 +02:00
}
2018-09-12 19:26:21 +02:00
public async Task SetPlay(CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
2022-12-05 15:00:20 +01:00
if (avCommands is null)
2021-03-22 18:21:12 +01:00
{
return;
}
2018-09-12 19:26:21 +02:00
await SetPlay(avCommands, cancellationToken).ConfigureAwait(false);
RestartTimer(true);
}
public async Task SetStop(CancellationToken cancellationToken)
{
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
2021-03-22 18:21:12 +01:00
var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "Stop");
2022-12-05 15:00:20 +01:00
if (command is null)
{
2016-10-30 00:22:20 +02:00
return;
}
2016-10-30 00:22:20 +02:00
2023-07-29 21:35:38 +02:00
var service = GetAvTransportService() ?? throw new InvalidOperationException("Unable to find service");
2022-03-08 22:57:47 +01:00
await new DlnaHttpClient(_logger, _httpClientFactory)
2021-02-15 14:19:08 +01:00
.SendCommandAsync(
Properties.BaseUrl,
service,
command.Name,
2023-07-29 21:35:38 +02:00
avCommands!.BuildPost(command, service.ServiceType, 1), // null checked above
2021-02-15 14:19:08 +01:00
cancellationToken: cancellationToken)
2016-10-30 00:22:20 +02:00
.ConfigureAwait(false);
2018-09-12 19:26:21 +02:00
RestartTimer(true);
2016-10-30 00:22:20 +02:00
}
2018-09-12 19:26:21 +02:00
public async Task SetPause(CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
2021-03-22 18:21:12 +01:00
var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "Pause");
2022-12-05 15:00:20 +01:00
if (command is null)
{
2016-10-30 00:22:20 +02:00
return;
}
2016-10-30 00:22:20 +02:00
2023-07-29 21:35:38 +02:00
var service = GetAvTransportService() ?? throw new InvalidOperationException("Unable to find service");
2022-03-08 22:57:47 +01:00
await new DlnaHttpClient(_logger, _httpClientFactory)
2021-02-15 14:19:08 +01:00
.SendCommandAsync(
Properties.BaseUrl,
service,
command.Name,
2023-07-29 21:35:38 +02:00
avCommands!.BuildPost(command, service.ServiceType, 1), // null checked above
2021-02-15 14:19:08 +01:00
cancellationToken: cancellationToken)
2016-10-30 00:22:20 +02:00
.ConfigureAwait(false);
TransportState = TransportState.PAUSED_PLAYBACK;
2018-09-12 19:26:21 +02:00
RestartTimer(true);
2016-10-30 00:22:20 +02:00
}
2023-07-29 21:35:38 +02:00
private async void TimerCallback(object? sender)
2016-10-30 00:22:20 +02:00
{
if (_disposed)
{
2016-10-30 00:22:20 +02:00
return;
}
2016-10-30 00:22:20 +02:00
try
{
2018-09-12 19:26:21 +02:00
var cancellationToken = CancellationToken.None;
var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
2022-12-05 15:00:20 +01:00
if (avCommands is null)
2018-09-12 19:26:21 +02:00
{
return;
}
var transportState = await GetTransportInfo(avCommands, cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
2017-01-24 20:54:18 +01:00
if (_disposed)
{
return;
}
2016-10-30 00:22:20 +02:00
if (transportState.HasValue)
{
// If we're not playing anything no need to get additional data
if (transportState.Value == TransportState.STOPPED)
2016-10-30 00:22:20 +02:00
{
UpdateMediaInfo(null, transportState.Value);
}
else
{
2018-09-12 19:26:21 +02:00
var tuple = await GetPositionInfo(avCommands, cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
2022-01-07 11:48:59 +01:00
var currentObject = tuple.Track;
2016-10-30 00:22:20 +02:00
2022-12-05 15:00:20 +01:00
if (tuple.Success && currentObject is null)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
currentObject = await GetMediaInfo(avCommands, cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
}
2022-12-05 15:01:13 +01:00
if (currentObject is not null)
2016-10-30 00:22:20 +02:00
{
UpdateMediaInfo(currentObject, transportState.Value);
}
}
_connectFailureCount = 0;
if (_disposed)
2020-06-20 11:12:36 +02:00
{
2016-10-30 00:22:20 +02:00
return;
2020-06-20 11:12:36 +02:00
}
2016-10-30 00:22:20 +02:00
2020-11-18 14:23:45 +01:00
// If we're not playing anything make sure we don't get data more often than necessary to keep the Session alive
if (transportState.Value == TransportState.STOPPED)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
RestartTimerInactive();
2016-10-30 00:22:20 +02:00
}
else
{
RestartTimer();
}
}
2017-08-10 22:06:36 +02:00
else
{
RestartTimerInactive();
}
2016-10-30 00:22:20 +02:00
}
2018-09-12 19:26:21 +02:00
catch (Exception ex)
2016-10-30 00:22:20 +02:00
{
if (_disposed)
2020-06-20 11:12:36 +02:00
{
2016-10-30 00:22:20 +02:00
return;
2020-06-20 11:12:36 +02:00
}
2016-10-30 00:22:20 +02:00
2018-12-20 13:39:58 +01:00
_logger.LogError(ex, "Error updating device info for {DeviceName}", Properties.Name);
2016-10-30 00:22:20 +02:00
_connectFailureCount++;
if (_connectFailureCount >= 3)
{
2018-09-12 19:26:21 +02:00
var action = OnDeviceUnavailable;
2022-12-05 15:01:13 +01:00
if (action is not null)
2016-10-30 00:22:20 +02:00
{
_logger.LogDebug("Disposing device due to loss of connection");
2018-09-12 19:26:21 +02:00
action();
2016-10-30 00:22:20 +02:00
return;
}
}
2020-06-15 23:43:52 +02:00
2018-09-12 19:26:21 +02:00
RestartTimerInactive();
2016-10-30 00:22:20 +02:00
}
}
2018-09-12 19:26:21 +02:00
private async Task GetVolume(CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2017-01-24 20:54:18 +01:00
if (_disposed)
{
return;
}
2018-09-12 19:26:21 +02:00
var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
2021-03-22 18:21:12 +01:00
var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "GetVolume");
2022-12-05 15:00:20 +01:00
if (command is null)
{
2016-10-30 00:22:20 +02:00
return;
}
2016-10-30 00:22:20 +02:00
2016-12-15 08:12:52 +01:00
var service = GetServiceRenderingControl();
2016-10-30 00:22:20 +02:00
2022-12-05 15:00:20 +01:00
if (service is null)
2016-10-30 00:22:20 +02:00
{
return;
}
2022-03-08 22:57:47 +01:00
var result = await new DlnaHttpClient(_logger, _httpClientFactory).SendCommandAsync(
2020-04-02 17:07:37 +02:00
Properties.BaseUrl,
service,
command.Name,
2023-07-29 21:35:38 +02:00
rendererCommands!.BuildPost(command, service.ServiceType), // null checked above
2020-04-03 23:13:45 +02:00
cancellationToken: cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
2022-12-05 15:00:20 +01:00
if (result is null || result.Document is null)
{
2016-10-30 00:22:20 +02:00
return;
}
2016-10-30 00:22:20 +02:00
2022-12-05 15:01:13 +01:00
var volume = result.Document.Descendants(UPnpNamespaces.RenderingControl + "GetVolumeResponse").Select(i => i.Element("CurrentVolume")).FirstOrDefault(i => i is not null);
var volumeValue = volume?.Value;
2016-10-30 00:22:20 +02:00
if (string.IsNullOrWhiteSpace(volumeValue))
{
2016-10-30 00:22:20 +02:00
return;
}
2016-10-30 00:22:20 +02:00
2021-09-26 16:14:36 +02:00
Volume = int.Parse(volumeValue, CultureInfo.InvariantCulture);
2016-10-30 00:22:20 +02:00
if (Volume > 0)
{
_muteVol = Volume;
}
}
2018-09-12 19:26:21 +02:00
private async Task GetMute(CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2017-01-24 20:54:18 +01:00
if (_disposed)
{
return;
}
2018-09-12 19:26:21 +02:00
var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
2021-03-22 18:21:12 +01:00
var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "GetMute");
2022-12-05 15:00:20 +01:00
if (command is null)
{
2016-10-30 00:22:20 +02:00
return;
}
2016-10-30 00:22:20 +02:00
2016-12-15 08:12:52 +01:00
var service = GetServiceRenderingControl();
2016-10-30 00:22:20 +02:00
2022-12-05 15:00:20 +01:00
if (service is null)
2016-10-30 00:22:20 +02:00
{
return;
}
2022-03-08 22:57:47 +01:00
var result = await new DlnaHttpClient(_logger, _httpClientFactory).SendCommandAsync(
2020-04-02 17:07:37 +02:00
Properties.BaseUrl,
service,
command.Name,
2023-07-29 21:35:38 +02:00
rendererCommands!.BuildPost(command, service.ServiceType), // null checked above
2020-04-03 16:46:14 +02:00
cancellationToken: cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
2022-12-05 15:00:20 +01:00
if (result is null || result.Document is null)
2020-06-20 11:12:36 +02:00
{
2016-10-30 00:22:20 +02:00
return;
2020-06-20 11:12:36 +02:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var valueNode = result.Document.Descendants(UPnpNamespaces.RenderingControl + "GetMuteResponse")
.Select(i => i.Element("CurrentMute"))
2022-12-05 15:01:13 +01:00
.FirstOrDefault(i => i is not null);
2016-10-30 00:22:20 +02:00
IsMuted = string.Equals(valueNode?.Value, "1", StringComparison.OrdinalIgnoreCase);
2016-10-30 00:22:20 +02:00
}
2020-08-20 17:01:04 +02:00
private async Task<TransportState?> GetTransportInfo(TransportCommands avCommands, CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetTransportInfo");
2022-12-05 15:00:20 +01:00
if (command is null)
{
2016-10-30 00:22:20 +02:00
return null;
}
2016-10-30 00:22:20 +02:00
2016-12-15 08:12:52 +01:00
var service = GetAvTransportService();
2022-12-05 15:00:20 +01:00
if (service is null)
{
2016-10-30 00:22:20 +02:00
return null;
}
2016-10-30 00:22:20 +02:00
2022-03-08 22:57:47 +01:00
var result = await new DlnaHttpClient(_logger, _httpClientFactory).SendCommandAsync(
2020-04-02 16:49:58 +02:00
Properties.BaseUrl,
service,
command.Name,
2020-04-03 17:30:01 +02:00
avCommands.BuildPost(command, service.ServiceType),
2020-04-02 16:49:58 +02:00
cancellationToken: cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
2022-12-05 15:00:20 +01:00
if (result is null || result.Document is null)
{
2016-10-30 00:22:20 +02:00
return null;
}
2016-10-30 00:22:20 +02:00
var transportState =
2022-12-05 15:01:13 +01:00
result.Document.Descendants(UPnpNamespaces.AvTransport + "GetTransportInfoResponse").Select(i => i.Element("CurrentTransportState")).FirstOrDefault(i => i is not null);
2016-10-30 00:22:20 +02:00
2020-04-02 16:49:58 +02:00
var transportStateValue = transportState?.Value;
2016-10-30 00:22:20 +02:00
2022-12-05 15:01:13 +01:00
if (transportStateValue is not null
2020-08-20 17:01:04 +02:00
&& Enum.TryParse(transportStateValue, true, out TransportState state))
2016-10-30 00:22:20 +02:00
{
return state;
2016-10-30 00:22:20 +02:00
}
return null;
}
2023-07-29 21:35:38 +02:00
private async Task<UBaseObject?> GetMediaInfo(TransportCommands avCommands, CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetMediaInfo");
2022-12-05 15:00:20 +01:00
if (command is null)
{
2016-10-30 00:22:20 +02:00
return null;
}
2016-10-30 00:22:20 +02:00
2016-12-15 08:12:52 +01:00
var service = GetAvTransportService();
2022-12-05 15:00:20 +01:00
if (service is null)
2016-10-30 00:22:20 +02:00
{
throw new InvalidOperationException("Unable to find service");
}
2018-09-12 19:26:21 +02:00
var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
2022-12-05 15:00:20 +01:00
if (rendererCommands is null)
2021-03-22 18:21:12 +01:00
{
return null;
}
2018-09-12 19:26:21 +02:00
2022-03-08 22:57:47 +01:00
var result = await new DlnaHttpClient(_logger, _httpClientFactory).SendCommandAsync(
2020-04-02 17:07:37 +02:00
Properties.BaseUrl,
service,
command.Name,
2020-04-03 17:30:01 +02:00
rendererCommands.BuildPost(command, service.ServiceType),
cancellationToken: cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
2022-12-05 15:00:20 +01:00
if (result is null || result.Document is null)
{
2016-10-30 00:22:20 +02:00
return null;
}
2016-10-30 00:22:20 +02:00
var track = result.Document.Descendants("CurrentURIMetaData").FirstOrDefault();
2022-12-05 15:00:20 +01:00
if (track is null)
2016-10-30 00:22:20 +02:00
{
return null;
}
2020-08-20 21:04:57 +02:00
var e = track.Element(UPnpNamespaces.Items) ?? track;
2016-10-30 00:22:20 +02:00
2017-09-27 16:51:36 +02:00
var elementString = (string)e;
if (!string.IsNullOrWhiteSpace(elementString))
{
return UpnpContainer.Create(e);
}
track = result.Document.Descendants("CurrentURI").FirstOrDefault();
2022-12-05 15:00:20 +01:00
if (track is null)
2017-09-27 16:51:36 +02:00
{
return null;
}
2020-08-20 21:04:57 +02:00
e = track.Element(UPnpNamespaces.Items) ?? track;
2017-09-27 16:51:36 +02:00
elementString = (string)e;
if (!string.IsNullOrWhiteSpace(elementString))
{
2020-08-20 21:04:57 +02:00
return new UBaseObject
2017-09-27 16:51:36 +02:00
{
Url = elementString
};
}
return null;
2016-10-30 00:22:20 +02:00
}
2023-07-29 21:35:38 +02:00
private async Task<(bool Success, UBaseObject? Track)> GetPositionInfo(TransportCommands avCommands, CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2018-09-12 19:26:21 +02:00
var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetPositionInfo");
2022-12-05 15:00:20 +01:00
if (command is null)
{
return (false, null);
}
2016-10-30 00:22:20 +02:00
2016-12-15 08:12:52 +01:00
var service = GetAvTransportService();
2016-10-30 00:22:20 +02:00
2022-12-05 15:00:20 +01:00
if (service is null)
2016-10-30 00:22:20 +02:00
{
throw new InvalidOperationException("Unable to find service");
}
2018-09-12 19:26:21 +02:00
var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
2022-12-05 15:00:20 +01:00
if (rendererCommands is null)
2021-03-22 18:21:12 +01:00
{
return (false, null);
}
2022-03-08 22:57:47 +01:00
var result = await new DlnaHttpClient(_logger, _httpClientFactory).SendCommandAsync(
2020-04-02 17:07:37 +02:00
Properties.BaseUrl,
service,
command.Name,
2020-04-04 00:24:36 +02:00
rendererCommands.BuildPost(command, service.ServiceType),
2020-04-04 00:21:26 +02:00
cancellationToken: cancellationToken).ConfigureAwait(false);
2016-10-30 00:22:20 +02:00
2022-12-05 15:00:20 +01:00
if (result is null || result.Document is null)
{
return (false, null);
}
2016-10-30 00:22:20 +02:00
2022-12-05 15:01:13 +01:00
var trackUriElem = result.Document.Descendants(UPnpNamespaces.AvTransport + "GetPositionInfoResponse").Select(i => i.Element("TrackURI")).FirstOrDefault(i => i is not null);
2020-08-07 19:26:28 +02:00
var trackUri = trackUriElem?.Value;
2016-10-30 00:22:20 +02:00
2022-12-05 15:01:13 +01:00
var durationElem = result.Document.Descendants(UPnpNamespaces.AvTransport + "GetPositionInfoResponse").Select(i => i.Element("TrackDuration")).FirstOrDefault(i => i is not null);
2020-08-07 19:26:28 +02:00
var duration = durationElem?.Value;
2016-10-30 00:22:20 +02:00
if (!string.IsNullOrWhiteSpace(duration)
&& !string.Equals(duration, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
2016-10-30 00:22:20 +02:00
{
2021-09-26 16:14:36 +02:00
Duration = TimeSpan.Parse(duration, CultureInfo.InvariantCulture);
2016-10-30 00:22:20 +02:00
}
else
{
Duration = null;
}
2022-12-05 15:01:13 +01:00
var positionElem = result.Document.Descendants(UPnpNamespaces.AvTransport + "GetPositionInfoResponse").Select(i => i.Element("RelTime")).FirstOrDefault(i => i is not null);
2020-08-07 19:26:28 +02:00
var position = positionElem?.Value;
2016-10-30 00:22:20 +02:00
if (!string.IsNullOrWhiteSpace(position) && !string.Equals(position, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
{
2021-09-26 16:14:36 +02:00
Position = TimeSpan.Parse(position, CultureInfo.InvariantCulture);
2016-10-30 00:22:20 +02:00
}
var track = result.Document.Descendants("TrackMetaData").FirstOrDefault();
2022-12-05 15:00:20 +01:00
if (track is null)
2016-10-30 00:22:20 +02:00
{
2020-11-18 14:48:31 +01:00
// If track is null, some vendors do this, use GetMediaInfo instead.
return (true, null);
2016-10-30 00:22:20 +02:00
}
var trackString = (string)track;
if (string.IsNullOrWhiteSpace(trackString) || string.Equals(trackString, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
{
return (true, null);
2016-10-30 00:22:20 +02:00
}
2023-07-29 21:35:38 +02:00
XElement? uPnpResponse = null;
2016-12-15 08:12:52 +01:00
2016-10-30 00:22:20 +02:00
try
{
2019-02-13 20:23:13 +01:00
uPnpResponse = ParseResponse(trackString);
2016-10-30 00:22:20 +02:00
}
2019-02-13 20:23:13 +01:00
catch (Exception ex)
2016-10-30 00:22:20 +02:00
{
2019-02-13 20:23:13 +01:00
_logger.LogError(ex, "Uncaught exception while parsing xml");
}
2022-12-05 15:00:20 +01:00
if (uPnpResponse is null)
2019-02-13 20:23:13 +01:00
{
_logger.LogError("Failed to parse xml: \n {Xml}", trackString);
return (true, null);
2016-10-30 00:22:20 +02:00
}
2020-08-20 21:04:57 +02:00
var e = uPnpResponse.Element(UPnpNamespaces.Items);
2016-10-30 00:22:20 +02:00
var uTrack = CreateUBaseObject(e, trackUri);
return (true, uTrack);
2016-10-30 00:22:20 +02:00
}
2023-07-29 21:35:38 +02:00
private XElement? ParseResponse(string xml)
2019-02-13 20:23:13 +01:00
{
2020-11-18 14:48:31 +01:00
// Handle different variations sent back by devices.
2019-02-13 20:23:13 +01:00
try
{
return XElement.Parse(xml);
}
catch (XmlException)
{
}
2020-11-18 14:48:31 +01:00
// first try to add a root node with a dlna namespace.
2019-02-13 20:23:13 +01:00
try
{
return XElement.Parse("<data xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">" + xml + "</data>")
.Descendants()
.First();
}
catch (XmlException)
{
}
// some devices send back invalid xml
try
{
2020-08-07 19:26:28 +02:00
return XElement.Parse(xml.Replace("&", "&amp;", StringComparison.Ordinal));
2019-02-13 20:23:13 +01:00
}
catch (XmlException)
{
}
return null;
}
2023-07-29 21:35:38 +02:00
private static UBaseObject CreateUBaseObject(XElement? container, string? trackUri)
2016-10-30 00:22:20 +02:00
{
ArgumentNullException.ThrowIfNull(container);
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var url = container.GetValue(UPnpNamespaces.Res);
2016-10-30 00:22:20 +02:00
if (string.IsNullOrWhiteSpace(url))
{
url = trackUri;
}
2020-08-20 21:04:57 +02:00
return new UBaseObject
2016-10-30 00:22:20 +02:00
{
2020-08-20 21:04:57 +02:00
Id = container.GetAttributeValue(UPnpNamespaces.Id),
ParentId = container.GetAttributeValue(UPnpNamespaces.ParentId),
Title = container.GetValue(UPnpNamespaces.Title),
IconUrl = container.GetValue(UPnpNamespaces.Artwork),
2020-08-07 19:26:28 +02:00
SecondText = string.Empty,
2016-10-30 00:22:20 +02:00
Url = url,
ProtocolInfo = GetProtocolInfo(container),
MetaData = container.ToString()
};
}
private static string[] GetProtocolInfo(XElement container)
{
ArgumentNullException.ThrowIfNull(container);
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var resElement = container.Element(UPnpNamespaces.Res);
2016-10-30 00:22:20 +02:00
2022-12-05 15:01:13 +01:00
if (resElement is not null)
2016-10-30 00:22:20 +02:00
{
2020-08-20 21:04:57 +02:00
var info = resElement.Attribute(UPnpNamespaces.ProtocolInfo);
2016-10-30 00:22:20 +02:00
2022-12-05 15:01:13 +01:00
if (info is not null && !string.IsNullOrWhiteSpace(info.Value))
2016-10-30 00:22:20 +02:00
{
return info.Value.Split(':');
}
}
return new string[4];
}
2023-07-29 21:35:38 +02:00
private async Task<TransportCommands?> GetAVProtocolAsync(CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2022-12-05 15:01:13 +01:00
if (AvCommands is not null)
2018-09-12 19:26:21 +02:00
{
return AvCommands;
2018-09-12 19:26:21 +02:00
}
2017-01-24 20:54:18 +01:00
if (_disposed)
{
2018-09-12 19:26:21 +02:00
throw new ObjectDisposedException(GetType().Name);
2017-01-24 20:54:18 +01:00
}
2016-12-15 08:12:52 +01:00
var avService = GetAvTransportService();
2022-12-05 15:00:20 +01:00
if (avService is null)
2018-09-12 19:26:21 +02:00
{
return null;
}
2016-10-30 00:22:20 +02:00
string url = NormalizeUrl(Properties.BaseUrl, avService.ScpdUrl);
2022-03-08 22:57:47 +01:00
var httpClient = new DlnaHttpClient(_logger, _httpClientFactory);
2018-09-12 19:26:21 +02:00
2017-11-23 16:46:16 +01:00
var document = await httpClient.GetDataAsync(url, cancellationToken).ConfigureAwait(false);
2022-12-05 15:00:20 +01:00
if (document is null)
2021-03-22 18:21:12 +01:00
{
return null;
}
2016-10-30 00:22:20 +02:00
AvCommands = TransportCommands.Create(document);
return AvCommands;
2016-10-30 00:22:20 +02:00
}
2023-07-29 21:35:38 +02:00
private async Task<TransportCommands?> GetRenderingProtocolAsync(CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2022-12-05 15:01:13 +01:00
if (RendererCommands is not null)
2018-09-12 19:26:21 +02:00
{
return RendererCommands;
2018-09-12 19:26:21 +02:00
}
2017-01-24 20:54:18 +01:00
if (_disposed)
{
2018-09-12 19:26:21 +02:00
throw new ObjectDisposedException(GetType().Name);
2017-01-24 20:54:18 +01:00
}
2016-12-15 08:12:52 +01:00
var avService = GetServiceRenderingControl();
ArgumentNullException.ThrowIfNull(avService);
2018-09-12 19:26:21 +02:00
2016-10-30 00:22:20 +02:00
string url = NormalizeUrl(Properties.BaseUrl, avService.ScpdUrl);
2022-03-08 22:57:47 +01:00
var httpClient = new DlnaHttpClient(_logger, _httpClientFactory);
_logger.LogDebug("Dlna Device.GetRenderingProtocolAsync");
2017-11-23 16:46:16 +01:00
var document = await httpClient.GetDataAsync(url, cancellationToken).ConfigureAwait(false);
2022-12-05 15:00:20 +01:00
if (document is null)
2021-03-22 18:21:12 +01:00
{
return null;
}
2016-10-30 00:22:20 +02:00
RendererCommands = TransportCommands.Create(document);
return RendererCommands;
2016-10-30 00:22:20 +02: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;
}
2020-08-07 19:26:28 +02:00
if (!url.Contains('/', StringComparison.Ordinal))
{
2016-10-30 00:22:20 +02:00
url = "/dmr/" + url;
}
2020-12-02 15:38:52 +01:00
if (!url.StartsWith('/'))
{
2016-10-30 00:22:20 +02:00
url = "/" + url;
}
2016-10-30 00:22:20 +02:00
return baseUrl + url;
}
2023-07-29 21:35:38 +02:00
public static async Task<Device?> CreateuPnpDeviceAsync(Uri url, IHttpClientFactory httpClientFactory, ILogger logger, CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2022-03-08 22:57:47 +01:00
var ssdpHttpClient = new DlnaHttpClient(logger, httpClientFactory);
2016-10-30 00:22:20 +02:00
2017-11-23 16:46:16 +01:00
var document = await ssdpHttpClient.GetDataAsync(url.ToString(), cancellationToken).ConfigureAwait(false);
2022-12-05 15:00:20 +01:00
if (document is null)
2021-03-22 18:21:12 +01:00
{
return null;
}
2016-10-30 00:22:20 +02:00
var friendlyNames = new List<string>();
2020-08-20 21:04:57 +02:00
var name = document.Descendants(UPnpNamespaces.Ud.GetName("friendlyName")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (name is not null && !string.IsNullOrWhiteSpace(name.Value))
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
friendlyNames.Add(name.Value);
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var room = document.Descendants(UPnpNamespaces.Ud.GetName("roomName")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (room is not null && !string.IsNullOrWhiteSpace(room.Value))
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
friendlyNames.Add(room.Value);
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
var deviceProperties = new DeviceInfo()
{
2021-02-13 00:39:18 +01:00
Name = string.Join(' ', friendlyNames),
2020-08-07 19:26:28 +02:00
BaseUrl = string.Format(CultureInfo.InvariantCulture, "http://{0}:{1}", url.Host, url.Port)
};
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var model = document.Descendants(UPnpNamespaces.Ud.GetName("modelName")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (model is not null)
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
deviceProperties.ModelName = model.Value;
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var modelNumber = document.Descendants(UPnpNamespaces.Ud.GetName("modelNumber")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (modelNumber is not null)
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
deviceProperties.ModelNumber = modelNumber.Value;
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var uuid = document.Descendants(UPnpNamespaces.Ud.GetName("UDN")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (uuid is not null)
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
deviceProperties.UUID = uuid.Value;
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var manufacturer = document.Descendants(UPnpNamespaces.Ud.GetName("manufacturer")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (manufacturer is not null)
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
deviceProperties.Manufacturer = manufacturer.Value;
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var manufacturerUrl = document.Descendants(UPnpNamespaces.Ud.GetName("manufacturerURL")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (manufacturerUrl is not null)
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
deviceProperties.ManufacturerUrl = manufacturerUrl.Value;
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var presentationUrl = document.Descendants(UPnpNamespaces.Ud.GetName("presentationURL")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (presentationUrl is not null)
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
deviceProperties.PresentationUrl = presentationUrl.Value;
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var modelUrl = document.Descendants(UPnpNamespaces.Ud.GetName("modelURL")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (modelUrl is not null)
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
deviceProperties.ModelUrl = modelUrl.Value;
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var serialNumber = document.Descendants(UPnpNamespaces.Ud.GetName("serialNumber")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (serialNumber is not null)
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
deviceProperties.SerialNumber = serialNumber.Value;
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var modelDescription = document.Descendants(UPnpNamespaces.Ud.GetName("modelDescription")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (modelDescription is not null)
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
deviceProperties.ModelDescription = modelDescription.Value;
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var icon = document.Descendants(UPnpNamespaces.Ud.GetName("icon")).FirstOrDefault();
2022-12-05 15:01:13 +01:00
if (icon is not null)
2016-10-30 00:22:20 +02:00
{
deviceProperties.Icon = CreateIcon(icon);
}
2020-08-20 21:04:57 +02:00
foreach (var services in document.Descendants(UPnpNamespaces.Ud.GetName("serviceList")))
2016-10-30 00:22:20 +02:00
{
2022-12-05 15:00:20 +01:00
if (services is null)
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
continue;
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var servicesList = services.Descendants(UPnpNamespaces.Ud.GetName("service"));
2022-12-05 15:00:20 +01:00
if (servicesList is null)
2019-01-27 12:03:43 +01:00
{
2016-10-30 00:22:20 +02:00
continue;
2019-01-27 12:03:43 +01:00
}
2016-10-30 00:22:20 +02:00
foreach (var element in servicesList)
{
var service = Create(element);
2022-12-05 15:01:13 +01:00
if (service is not null)
2016-10-30 00:22:20 +02:00
{
deviceProperties.Services.Add(service);
}
}
}
return new Device(deviceProperties, httpClientFactory, logger);
2016-10-30 00:22:20 +02:00
}
2021-12-15 18:25:36 +01:00
#nullable enable
2016-10-30 00:22:20 +02:00
private static DeviceIcon CreateIcon(XElement element)
{
ArgumentNullException.ThrowIfNull(element);
2016-10-30 00:22:20 +02:00
2020-08-20 21:04:57 +02:00
var width = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("width"));
var height = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("height"));
2016-10-30 00:22:20 +02:00
2021-12-15 18:25:36 +01:00
_ = int.TryParse(width, NumberStyles.Integer, CultureInfo.InvariantCulture, out var widthValue);
_ = int.TryParse(height, NumberStyles.Integer, CultureInfo.InvariantCulture, out var heightValue);
2016-10-30 00:22:20 +02:00
return new DeviceIcon
{
2021-12-15 18:25:36 +01:00
Depth = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("depth")) ?? string.Empty,
2016-10-30 00:22:20 +02:00
Height = heightValue,
2021-12-15 18:25:36 +01:00
MimeType = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("mimetype")) ?? string.Empty,
Url = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("url")) ?? string.Empty,
2016-10-30 00:22:20 +02:00
Width = widthValue
};
}
private static DeviceService Create(XElement element)
2021-12-15 18:25:36 +01:00
=> new DeviceService()
{
ControlUrl = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("controlURL")) ?? string.Empty,
EventSubUrl = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("eventSubURL")) ?? string.Empty,
ScpdUrl = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("SCPDURL")) ?? string.Empty,
ServiceId = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("serviceId")) ?? string.Empty,
ServiceType = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("serviceType")) ?? string.Empty
2016-10-30 00:22:20 +02:00
};
2021-12-15 18:25:36 +01:00
private void UpdateMediaInfo(UBaseObject? mediaInfo, TransportState state)
2016-10-30 00:22:20 +02:00
{
TransportState = state;
var previousMediaInfo = CurrentMediaInfo;
CurrentMediaInfo = mediaInfo;
2022-12-05 15:00:20 +01:00
if (mediaInfo is null)
2016-10-30 00:22:20 +02:00
{
2022-12-05 15:01:13 +01:00
if (previousMediaInfo is not null)
2016-10-30 00:22:20 +02:00
{
2021-12-15 18:25:36 +01:00
OnPlaybackStop(previousMediaInfo);
2016-10-30 00:22:20 +02:00
}
}
2022-12-05 15:00:20 +01:00
else if (previousMediaInfo is null)
2016-10-30 00:22:20 +02:00
{
if (state != TransportState.STOPPED)
2021-12-15 18:25:36 +01:00
{
OnPlaybackStart(mediaInfo);
}
2016-10-30 00:22:20 +02:00
}
2021-12-15 18:25:36 +01:00
else if (mediaInfo.Equals(previousMediaInfo))
2016-10-30 00:22:20 +02:00
{
2021-12-15 18:25:36 +01:00
OnPlaybackProgress(mediaInfo);
2016-10-30 00:22:20 +02:00
}
2021-12-15 18:25:36 +01:00
else
2016-10-30 00:22:20 +02:00
{
2021-12-15 18:25:36 +01:00
OnMediaChanged(previousMediaInfo, mediaInfo);
2016-10-30 00:22:20 +02:00
}
}
2020-08-20 21:04:57 +02:00
private void OnPlaybackStart(UBaseObject mediaInfo)
2016-10-30 00:22:20 +02:00
{
2019-02-24 15:47:59 +01:00
if (string.IsNullOrWhiteSpace(mediaInfo.Url))
{
return;
}
2021-07-26 23:02:32 +02:00
PlaybackStart?.Invoke(this, new PlaybackStartEventArgs(mediaInfo));
2016-10-30 00:22:20 +02:00
}
2020-08-20 21:04:57 +02:00
private void OnPlaybackProgress(UBaseObject mediaInfo)
2016-10-30 00:22:20 +02:00
{
2019-02-24 15:47:59 +01:00
if (string.IsNullOrWhiteSpace(mediaInfo.Url))
2018-09-12 19:26:21 +02:00
{
return;
}
2021-07-26 23:02:32 +02:00
PlaybackProgress?.Invoke(this, new PlaybackProgressEventArgs(mediaInfo));
2016-10-30 00:22:20 +02:00
}
2020-08-20 21:04:57 +02:00
private void OnPlaybackStop(UBaseObject mediaInfo)
2016-10-30 00:22:20 +02:00
{
2021-07-26 23:02:32 +02:00
PlaybackStopped?.Invoke(this, new PlaybackStoppedEventArgs(mediaInfo));
2016-10-30 00:22:20 +02:00
}
2020-08-20 21:04:57 +02:00
private void OnMediaChanged(UBaseObject old, UBaseObject newMedia)
2016-10-30 00:22:20 +02:00
{
2021-07-26 23:02:32 +02:00
MediaChanged?.Invoke(this, new MediaChangedEventArgs(old, newMedia));
2016-10-30 00:22:20 +02:00
}
2020-08-20 21:04:57 +02:00
/// <inheritdoc />
2016-10-30 00:22:20 +02:00
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
2016-10-30 00:22:20 +02:00
}
2020-08-20 17:59:27 +02:00
/// <summary>
/// Releases unmanaged and optionally managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
2016-10-30 00:22:20 +02:00
{
if (_disposed)
2016-10-30 00:22:20 +02:00
{
return;
2016-10-30 00:22:20 +02:00
}
if (disposing)
{
_timer?.Dispose();
}
_timer = null;
2023-07-29 21:35:38 +02:00
Properties = null!;
_disposed = true;
2016-10-30 00:22:20 +02:00
}
2020-08-20 21:04:57 +02:00
/// <inheritdoc />
2016-10-30 00:22:20 +02:00
public override string ToString()
{
2020-08-07 19:26:28 +02:00
return string.Format(CultureInfo.InvariantCulture, "{0} - {1}", Properties.Name, Properties.BaseUrl);
2016-10-30 00:22:20 +02:00
}
}
}