Device.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Security;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Xml;
  12. using System.Xml.Linq;
  13. using Emby.Dlna.Common;
  14. using Emby.Dlna.Ssdp;
  15. using Microsoft.Extensions.Logging;
  16. namespace Emby.Dlna.PlayTo
  17. {
  18. public class Device : IDisposable
  19. {
  20. private readonly IHttpClientFactory _httpClientFactory;
  21. private readonly ILogger _logger;
  22. private readonly object _timerLock = new object();
  23. private Timer _timer;
  24. private int _muteVol;
  25. private int _volume;
  26. private DateTime _lastVolumeRefresh;
  27. private bool _volumeRefreshActive;
  28. private int _connectFailureCount;
  29. private bool _disposed;
  30. public Device(DeviceInfo deviceProperties, IHttpClientFactory httpClientFactory, ILogger logger)
  31. {
  32. Properties = deviceProperties;
  33. _httpClientFactory = httpClientFactory;
  34. _logger = logger;
  35. }
  36. public event EventHandler<PlaybackStartEventArgs> PlaybackStart;
  37. public event EventHandler<PlaybackProgressEventArgs> PlaybackProgress;
  38. public event EventHandler<PlaybackStoppedEventArgs> PlaybackStopped;
  39. public event EventHandler<MediaChangedEventArgs> MediaChanged;
  40. public DeviceInfo Properties { get; set; }
  41. public bool IsMuted { get; set; }
  42. public int Volume
  43. {
  44. get
  45. {
  46. RefreshVolumeIfNeeded().GetAwaiter().GetResult();
  47. return _volume;
  48. }
  49. set => _volume = value;
  50. }
  51. public TimeSpan? Duration { get; set; }
  52. public TimeSpan Position { get; set; } = TimeSpan.FromSeconds(0);
  53. public TransportState TransportState { get; private set; }
  54. public bool IsPlaying => TransportState == TransportState.PLAYING;
  55. public bool IsPaused => TransportState == TransportState.PAUSED_PLAYBACK;
  56. public bool IsStopped => TransportState == TransportState.STOPPED;
  57. public Action OnDeviceUnavailable { get; set; }
  58. private TransportCommands AvCommands { get; set; }
  59. private TransportCommands RendererCommands { get; set; }
  60. public UBaseObject CurrentMediaInfo { get; private set; }
  61. public void Start()
  62. {
  63. _logger.LogDebug("Dlna Device.Start");
  64. _timer = new Timer(TimerCallback, null, 1000, Timeout.Infinite);
  65. }
  66. private Task RefreshVolumeIfNeeded()
  67. {
  68. if (_volumeRefreshActive
  69. && DateTime.UtcNow >= _lastVolumeRefresh.AddSeconds(5))
  70. {
  71. _lastVolumeRefresh = DateTime.UtcNow;
  72. return RefreshVolume();
  73. }
  74. return Task.CompletedTask;
  75. }
  76. private async Task RefreshVolume(CancellationToken cancellationToken = default)
  77. {
  78. if (_disposed)
  79. {
  80. return;
  81. }
  82. try
  83. {
  84. await GetVolume(cancellationToken).ConfigureAwait(false);
  85. await GetMute(cancellationToken).ConfigureAwait(false);
  86. }
  87. catch (Exception ex)
  88. {
  89. _logger.LogError(ex, "Error updating device volume info for {DeviceName}", Properties.Name);
  90. }
  91. }
  92. private void RestartTimer(bool immediate = false)
  93. {
  94. lock (_timerLock)
  95. {
  96. if (_disposed)
  97. {
  98. return;
  99. }
  100. _volumeRefreshActive = true;
  101. var time = immediate ? 100 : 10000;
  102. _timer.Change(time, Timeout.Infinite);
  103. }
  104. }
  105. /// <summary>
  106. /// Restarts the timer in inactive mode.
  107. /// </summary>
  108. private void RestartTimerInactive()
  109. {
  110. lock (_timerLock)
  111. {
  112. if (_disposed)
  113. {
  114. return;
  115. }
  116. _volumeRefreshActive = false;
  117. _timer.Change(Timeout.Infinite, Timeout.Infinite);
  118. }
  119. }
  120. public Task VolumeDown(CancellationToken cancellationToken)
  121. {
  122. var sendVolume = Math.Max(Volume - 5, 0);
  123. return SetVolume(sendVolume, cancellationToken);
  124. }
  125. public Task VolumeUp(CancellationToken cancellationToken)
  126. {
  127. var sendVolume = Math.Min(Volume + 5, 100);
  128. return SetVolume(sendVolume, cancellationToken);
  129. }
  130. public Task ToggleMute(CancellationToken cancellationToken)
  131. {
  132. if (IsMuted)
  133. {
  134. return Unmute(cancellationToken);
  135. }
  136. return Mute(cancellationToken);
  137. }
  138. public async Task Mute(CancellationToken cancellationToken)
  139. {
  140. var success = await SetMute(true, cancellationToken).ConfigureAwait(true);
  141. if (!success)
  142. {
  143. await SetVolume(0, cancellationToken).ConfigureAwait(false);
  144. }
  145. }
  146. public async Task Unmute(CancellationToken cancellationToken)
  147. {
  148. var success = await SetMute(false, cancellationToken).ConfigureAwait(true);
  149. if (!success)
  150. {
  151. var sendVolume = _muteVol <= 0 ? 20 : _muteVol;
  152. await SetVolume(sendVolume, cancellationToken).ConfigureAwait(false);
  153. }
  154. }
  155. private DeviceService GetServiceRenderingControl()
  156. {
  157. var services = Properties.Services;
  158. return services.FirstOrDefault(s => string.Equals(s.ServiceType, "urn:schemas-upnp-org:service:RenderingControl:1", StringComparison.OrdinalIgnoreCase)) ??
  159. services.FirstOrDefault(s => (s.ServiceType ?? string.Empty).StartsWith("urn:schemas-upnp-org:service:RenderingControl", StringComparison.OrdinalIgnoreCase));
  160. }
  161. private DeviceService GetAvTransportService()
  162. {
  163. var services = Properties.Services;
  164. return services.FirstOrDefault(s => string.Equals(s.ServiceType, "urn:schemas-upnp-org:service:AVTransport:1", StringComparison.OrdinalIgnoreCase)) ??
  165. services.FirstOrDefault(s => (s.ServiceType ?? string.Empty).StartsWith("urn:schemas-upnp-org:service:AVTransport", StringComparison.OrdinalIgnoreCase));
  166. }
  167. private async Task<bool> SetMute(bool mute, CancellationToken cancellationToken)
  168. {
  169. var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
  170. var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "SetMute");
  171. if (command == null)
  172. {
  173. return false;
  174. }
  175. var service = GetServiceRenderingControl();
  176. if (service == null)
  177. {
  178. return false;
  179. }
  180. _logger.LogDebug("Setting mute");
  181. var value = mute ? 1 : 0;
  182. await new SsdpHttpClient(_httpClientFactory)
  183. .SendCommandAsync(
  184. Properties.BaseUrl,
  185. service,
  186. command.Name,
  187. rendererCommands.BuildPost(command, service.ServiceType, value),
  188. cancellationToken: cancellationToken)
  189. .ConfigureAwait(false);
  190. IsMuted = mute;
  191. return true;
  192. }
  193. /// <summary>
  194. /// Sets volume on a scale of 0-100.
  195. /// </summary>
  196. /// <param name="value">The volume on a scale of 0-100.</param>
  197. /// <param name="cancellationToken">The cancellation token to cancel operation.</param>
  198. /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
  199. public async Task SetVolume(int value, CancellationToken cancellationToken)
  200. {
  201. var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
  202. var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "SetVolume");
  203. if (command == null)
  204. {
  205. return;
  206. }
  207. var service = GetServiceRenderingControl();
  208. if (service == null)
  209. {
  210. throw new InvalidOperationException("Unable to find service");
  211. }
  212. // Set it early and assume it will succeed
  213. // Remote control will perform better
  214. Volume = value;
  215. await new SsdpHttpClient(_httpClientFactory)
  216. .SendCommandAsync(
  217. Properties.BaseUrl,
  218. service,
  219. command.Name,
  220. rendererCommands.BuildPost(command, service.ServiceType, value),
  221. cancellationToken: cancellationToken)
  222. .ConfigureAwait(false);
  223. }
  224. public async Task Seek(TimeSpan value, CancellationToken cancellationToken)
  225. {
  226. var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
  227. var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "Seek");
  228. if (command == null)
  229. {
  230. return;
  231. }
  232. var service = GetAvTransportService();
  233. if (service == null)
  234. {
  235. throw new InvalidOperationException("Unable to find service");
  236. }
  237. await new SsdpHttpClient(_httpClientFactory)
  238. .SendCommandAsync(
  239. Properties.BaseUrl,
  240. service,
  241. command.Name,
  242. avCommands.BuildPost(command, service.ServiceType, string.Format(CultureInfo.InvariantCulture, "{0:hh}:{0:mm}:{0:ss}", value), "REL_TIME"),
  243. cancellationToken: cancellationToken)
  244. .ConfigureAwait(false);
  245. RestartTimer(true);
  246. }
  247. public async Task SetAvTransport(string url, string header, string metaData, CancellationToken cancellationToken)
  248. {
  249. var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
  250. url = url.Replace("&", "&amp;", StringComparison.Ordinal);
  251. _logger.LogDebug("{0} - SetAvTransport Uri: {1} DlnaHeaders: {2}", Properties.Name, url, header);
  252. var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "SetAVTransportURI");
  253. if (command == null)
  254. {
  255. return;
  256. }
  257. var dictionary = new Dictionary<string, string>
  258. {
  259. { "CurrentURI", url },
  260. { "CurrentURIMetaData", CreateDidlMeta(metaData) }
  261. };
  262. var service = GetAvTransportService();
  263. if (service == null)
  264. {
  265. throw new InvalidOperationException("Unable to find service");
  266. }
  267. var post = avCommands.BuildPost(command, service.ServiceType, url, dictionary);
  268. await new SsdpHttpClient(_httpClientFactory)
  269. .SendCommandAsync(
  270. Properties.BaseUrl,
  271. service,
  272. command.Name,
  273. post,
  274. header: header,
  275. cancellationToken: cancellationToken)
  276. .ConfigureAwait(false);
  277. await Task.Delay(50, cancellationToken).ConfigureAwait(false);
  278. try
  279. {
  280. await SetPlay(avCommands, cancellationToken).ConfigureAwait(false);
  281. }
  282. catch
  283. {
  284. // Some devices will throw an error if you tell it to play when it's already playing
  285. // Others won't
  286. }
  287. RestartTimer(true);
  288. }
  289. /*
  290. * SetNextAvTransport is used to specify to the DLNA device what is the next track to play.
  291. * Without that information, the next track command on the device does not work.
  292. */
  293. public async Task SetNextAvTransport(string url, string header, string metaData, CancellationToken cancellationToken = default)
  294. {
  295. var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
  296. url = url.Replace("&", "&amp;", StringComparison.Ordinal);
  297. _logger.LogDebug("{PropertyName} - SetNextAvTransport Uri: {Url} DlnaHeaders: {Header}", Properties.Name, url, header);
  298. var command = avCommands.ServiceActions.FirstOrDefault(c => string.Equals(c.Name, "SetNextAVTransportURI", StringComparison.OrdinalIgnoreCase));
  299. if (command == null)
  300. {
  301. return;
  302. }
  303. var dictionary = new Dictionary<string, string>
  304. {
  305. { "NextURI", url },
  306. { "NextURIMetaData", CreateDidlMeta(metaData) }
  307. };
  308. var service = GetAvTransportService();
  309. if (service == null)
  310. {
  311. throw new InvalidOperationException("Unable to find service");
  312. }
  313. var post = avCommands.BuildPost(command, service.ServiceType, url, dictionary);
  314. await new SsdpHttpClient(_httpClientFactory).SendCommandAsync(Properties.BaseUrl, service, command.Name, post, header: header, cancellationToken)
  315. .ConfigureAwait(false);
  316. }
  317. private static string CreateDidlMeta(string value)
  318. {
  319. if (string.IsNullOrEmpty(value))
  320. {
  321. return string.Empty;
  322. }
  323. return SecurityElement.Escape(value);
  324. }
  325. private Task SetPlay(TransportCommands avCommands, CancellationToken cancellationToken)
  326. {
  327. var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "Play");
  328. if (command == null)
  329. {
  330. return Task.CompletedTask;
  331. }
  332. var service = GetAvTransportService();
  333. if (service == null)
  334. {
  335. throw new InvalidOperationException("Unable to find service");
  336. }
  337. return new SsdpHttpClient(_httpClientFactory).SendCommandAsync(
  338. Properties.BaseUrl,
  339. service,
  340. command.Name,
  341. avCommands.BuildPost(command, service.ServiceType, 1),
  342. cancellationToken: cancellationToken);
  343. }
  344. public async Task SetPlay(CancellationToken cancellationToken)
  345. {
  346. var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
  347. if (avCommands == null)
  348. {
  349. return;
  350. }
  351. await SetPlay(avCommands, cancellationToken).ConfigureAwait(false);
  352. RestartTimer(true);
  353. }
  354. public async Task SetStop(CancellationToken cancellationToken)
  355. {
  356. var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
  357. var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "Stop");
  358. if (command == null)
  359. {
  360. return;
  361. }
  362. var service = GetAvTransportService();
  363. await new SsdpHttpClient(_httpClientFactory)
  364. .SendCommandAsync(
  365. Properties.BaseUrl,
  366. service,
  367. command.Name,
  368. avCommands.BuildPost(command, service.ServiceType, 1),
  369. cancellationToken: cancellationToken)
  370. .ConfigureAwait(false);
  371. RestartTimer(true);
  372. }
  373. public async Task SetPause(CancellationToken cancellationToken)
  374. {
  375. var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
  376. var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "Pause");
  377. if (command == null)
  378. {
  379. return;
  380. }
  381. var service = GetAvTransportService();
  382. await new SsdpHttpClient(_httpClientFactory)
  383. .SendCommandAsync(
  384. Properties.BaseUrl,
  385. service,
  386. command.Name,
  387. avCommands.BuildPost(command, service.ServiceType, 1),
  388. cancellationToken: cancellationToken)
  389. .ConfigureAwait(false);
  390. TransportState = TransportState.PAUSED_PLAYBACK;
  391. RestartTimer(true);
  392. }
  393. private async void TimerCallback(object sender)
  394. {
  395. if (_disposed)
  396. {
  397. return;
  398. }
  399. try
  400. {
  401. var cancellationToken = CancellationToken.None;
  402. var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
  403. if (avCommands == null)
  404. {
  405. return;
  406. }
  407. var transportState = await GetTransportInfo(avCommands, cancellationToken).ConfigureAwait(false);
  408. if (_disposed)
  409. {
  410. return;
  411. }
  412. if (transportState.HasValue)
  413. {
  414. // If we're not playing anything no need to get additional data
  415. if (transportState.Value == TransportState.STOPPED)
  416. {
  417. UpdateMediaInfo(null, transportState.Value);
  418. }
  419. else
  420. {
  421. var tuple = await GetPositionInfo(avCommands, cancellationToken).ConfigureAwait(false);
  422. var currentObject = tuple.Track;
  423. if (tuple.Success && currentObject == null)
  424. {
  425. currentObject = await GetMediaInfo(avCommands, cancellationToken).ConfigureAwait(false);
  426. }
  427. if (currentObject != null)
  428. {
  429. UpdateMediaInfo(currentObject, transportState.Value);
  430. }
  431. }
  432. _connectFailureCount = 0;
  433. if (_disposed)
  434. {
  435. return;
  436. }
  437. // If we're not playing anything make sure we don't get data more often than necessary to keep the Session alive
  438. if (transportState.Value == TransportState.STOPPED)
  439. {
  440. RestartTimerInactive();
  441. }
  442. else
  443. {
  444. RestartTimer();
  445. }
  446. }
  447. else
  448. {
  449. RestartTimerInactive();
  450. }
  451. }
  452. catch (Exception ex)
  453. {
  454. if (_disposed)
  455. {
  456. return;
  457. }
  458. _logger.LogError(ex, "Error updating device info for {DeviceName}", Properties.Name);
  459. _connectFailureCount++;
  460. if (_connectFailureCount >= 3)
  461. {
  462. var action = OnDeviceUnavailable;
  463. if (action != null)
  464. {
  465. _logger.LogDebug("Disposing device due to loss of connection");
  466. action();
  467. return;
  468. }
  469. }
  470. RestartTimerInactive();
  471. }
  472. }
  473. private async Task GetVolume(CancellationToken cancellationToken)
  474. {
  475. if (_disposed)
  476. {
  477. return;
  478. }
  479. var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
  480. var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "GetVolume");
  481. if (command == null)
  482. {
  483. return;
  484. }
  485. var service = GetServiceRenderingControl();
  486. if (service == null)
  487. {
  488. return;
  489. }
  490. var result = await new SsdpHttpClient(_httpClientFactory).SendCommandAsync(
  491. Properties.BaseUrl,
  492. service,
  493. command.Name,
  494. rendererCommands.BuildPost(command, service.ServiceType),
  495. cancellationToken: cancellationToken).ConfigureAwait(false);
  496. if (result == null || result.Document == null)
  497. {
  498. return;
  499. }
  500. var volume = result.Document.Descendants(UPnpNamespaces.RenderingControl + "GetVolumeResponse").Select(i => i.Element("CurrentVolume")).FirstOrDefault(i => i != null);
  501. var volumeValue = volume?.Value;
  502. if (string.IsNullOrWhiteSpace(volumeValue))
  503. {
  504. return;
  505. }
  506. Volume = int.Parse(volumeValue, CultureInfo.InvariantCulture);
  507. if (Volume > 0)
  508. {
  509. _muteVol = Volume;
  510. }
  511. }
  512. private async Task GetMute(CancellationToken cancellationToken)
  513. {
  514. if (_disposed)
  515. {
  516. return;
  517. }
  518. var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
  519. var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "GetMute");
  520. if (command == null)
  521. {
  522. return;
  523. }
  524. var service = GetServiceRenderingControl();
  525. if (service == null)
  526. {
  527. return;
  528. }
  529. var result = await new SsdpHttpClient(_httpClientFactory).SendCommandAsync(
  530. Properties.BaseUrl,
  531. service,
  532. command.Name,
  533. rendererCommands.BuildPost(command, service.ServiceType),
  534. cancellationToken: cancellationToken).ConfigureAwait(false);
  535. if (result == null || result.Document == null)
  536. {
  537. return;
  538. }
  539. var valueNode = result.Document.Descendants(UPnpNamespaces.RenderingControl + "GetMuteResponse")
  540. .Select(i => i.Element("CurrentMute"))
  541. .FirstOrDefault(i => i != null);
  542. IsMuted = string.Equals(valueNode?.Value, "1", StringComparison.OrdinalIgnoreCase);
  543. }
  544. private async Task<TransportState?> GetTransportInfo(TransportCommands avCommands, CancellationToken cancellationToken)
  545. {
  546. var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetTransportInfo");
  547. if (command == null)
  548. {
  549. return null;
  550. }
  551. var service = GetAvTransportService();
  552. if (service == null)
  553. {
  554. return null;
  555. }
  556. var result = await new SsdpHttpClient(_httpClientFactory).SendCommandAsync(
  557. Properties.BaseUrl,
  558. service,
  559. command.Name,
  560. avCommands.BuildPost(command, service.ServiceType),
  561. cancellationToken: cancellationToken).ConfigureAwait(false);
  562. if (result == null || result.Document == null)
  563. {
  564. return null;
  565. }
  566. var transportState =
  567. result.Document.Descendants(UPnpNamespaces.AvTransport + "GetTransportInfoResponse").Select(i => i.Element("CurrentTransportState")).FirstOrDefault(i => i != null);
  568. var transportStateValue = transportState?.Value;
  569. if (transportStateValue != null
  570. && Enum.TryParse(transportStateValue, true, out TransportState state))
  571. {
  572. return state;
  573. }
  574. return null;
  575. }
  576. private async Task<UBaseObject> GetMediaInfo(TransportCommands avCommands, CancellationToken cancellationToken)
  577. {
  578. var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetMediaInfo");
  579. if (command == null)
  580. {
  581. return null;
  582. }
  583. var service = GetAvTransportService();
  584. if (service == null)
  585. {
  586. throw new InvalidOperationException("Unable to find service");
  587. }
  588. var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
  589. if (rendererCommands == null)
  590. {
  591. return null;
  592. }
  593. var result = await new SsdpHttpClient(_httpClientFactory).SendCommandAsync(
  594. Properties.BaseUrl,
  595. service,
  596. command.Name,
  597. rendererCommands.BuildPost(command, service.ServiceType),
  598. cancellationToken: cancellationToken).ConfigureAwait(false);
  599. if (result == null || result.Document == null)
  600. {
  601. return null;
  602. }
  603. var track = result.Document.Descendants("CurrentURIMetaData").FirstOrDefault();
  604. if (track == null)
  605. {
  606. return null;
  607. }
  608. var e = track.Element(UPnpNamespaces.Items) ?? track;
  609. var elementString = (string)e;
  610. if (!string.IsNullOrWhiteSpace(elementString))
  611. {
  612. return UpnpContainer.Create(e);
  613. }
  614. track = result.Document.Descendants("CurrentURI").FirstOrDefault();
  615. if (track == null)
  616. {
  617. return null;
  618. }
  619. e = track.Element(UPnpNamespaces.Items) ?? track;
  620. elementString = (string)e;
  621. if (!string.IsNullOrWhiteSpace(elementString))
  622. {
  623. return new UBaseObject
  624. {
  625. Url = elementString
  626. };
  627. }
  628. return null;
  629. }
  630. private async Task<(bool Success, UBaseObject Track)> GetPositionInfo(TransportCommands avCommands, CancellationToken cancellationToken)
  631. {
  632. var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetPositionInfo");
  633. if (command == null)
  634. {
  635. return (false, null);
  636. }
  637. var service = GetAvTransportService();
  638. if (service == null)
  639. {
  640. throw new InvalidOperationException("Unable to find service");
  641. }
  642. var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
  643. if (rendererCommands == null)
  644. {
  645. return (false, null);
  646. }
  647. var result = await new SsdpHttpClient(_httpClientFactory).SendCommandAsync(
  648. Properties.BaseUrl,
  649. service,
  650. command.Name,
  651. rendererCommands.BuildPost(command, service.ServiceType),
  652. cancellationToken: cancellationToken).ConfigureAwait(false);
  653. if (result == null || result.Document == null)
  654. {
  655. return (false, null);
  656. }
  657. var trackUriElem = result.Document.Descendants(UPnpNamespaces.AvTransport + "GetPositionInfoResponse").Select(i => i.Element("TrackURI")).FirstOrDefault(i => i != null);
  658. var trackUri = trackUriElem?.Value;
  659. var durationElem = result.Document.Descendants(UPnpNamespaces.AvTransport + "GetPositionInfoResponse").Select(i => i.Element("TrackDuration")).FirstOrDefault(i => i != null);
  660. var duration = durationElem?.Value;
  661. if (!string.IsNullOrWhiteSpace(duration)
  662. && !string.Equals(duration, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
  663. {
  664. Duration = TimeSpan.Parse(duration, CultureInfo.InvariantCulture);
  665. }
  666. else
  667. {
  668. Duration = null;
  669. }
  670. var positionElem = result.Document.Descendants(UPnpNamespaces.AvTransport + "GetPositionInfoResponse").Select(i => i.Element("RelTime")).FirstOrDefault(i => i != null);
  671. var position = positionElem?.Value;
  672. if (!string.IsNullOrWhiteSpace(position) && !string.Equals(position, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
  673. {
  674. Position = TimeSpan.Parse(position, CultureInfo.InvariantCulture);
  675. }
  676. var track = result.Document.Descendants("TrackMetaData").FirstOrDefault();
  677. if (track == null)
  678. {
  679. // If track is null, some vendors do this, use GetMediaInfo instead.
  680. return (true, null);
  681. }
  682. var trackString = (string)track;
  683. if (string.IsNullOrWhiteSpace(trackString) || string.Equals(trackString, "NOT_IMPLEMENTED", StringComparison.OrdinalIgnoreCase))
  684. {
  685. return (true, null);
  686. }
  687. XElement uPnpResponse = null;
  688. try
  689. {
  690. uPnpResponse = ParseResponse(trackString);
  691. }
  692. catch (Exception ex)
  693. {
  694. _logger.LogError(ex, "Uncaught exception while parsing xml");
  695. }
  696. if (uPnpResponse == null)
  697. {
  698. _logger.LogError("Failed to parse xml: \n {Xml}", trackString);
  699. return (true, null);
  700. }
  701. var e = uPnpResponse.Element(UPnpNamespaces.Items);
  702. var uTrack = CreateUBaseObject(e, trackUri);
  703. return (true, uTrack);
  704. }
  705. private XElement ParseResponse(string xml)
  706. {
  707. // Handle different variations sent back by devices.
  708. try
  709. {
  710. return XElement.Parse(xml);
  711. }
  712. catch (XmlException)
  713. {
  714. }
  715. // first try to add a root node with a dlna namespace.
  716. try
  717. {
  718. return XElement.Parse("<data xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">" + xml + "</data>")
  719. .Descendants()
  720. .First();
  721. }
  722. catch (XmlException)
  723. {
  724. }
  725. // some devices send back invalid xml
  726. try
  727. {
  728. return XElement.Parse(xml.Replace("&", "&amp;", StringComparison.Ordinal));
  729. }
  730. catch (XmlException)
  731. {
  732. }
  733. return null;
  734. }
  735. private static UBaseObject CreateUBaseObject(XElement container, string trackUri)
  736. {
  737. if (container == null)
  738. {
  739. throw new ArgumentNullException(nameof(container));
  740. }
  741. var url = container.GetValue(UPnpNamespaces.Res);
  742. if (string.IsNullOrWhiteSpace(url))
  743. {
  744. url = trackUri;
  745. }
  746. return new UBaseObject
  747. {
  748. Id = container.GetAttributeValue(UPnpNamespaces.Id),
  749. ParentId = container.GetAttributeValue(UPnpNamespaces.ParentId),
  750. Title = container.GetValue(UPnpNamespaces.Title),
  751. IconUrl = container.GetValue(UPnpNamespaces.Artwork),
  752. SecondText = string.Empty,
  753. Url = url,
  754. ProtocolInfo = GetProtocolInfo(container),
  755. MetaData = container.ToString()
  756. };
  757. }
  758. private static string[] GetProtocolInfo(XElement container)
  759. {
  760. if (container == null)
  761. {
  762. throw new ArgumentNullException(nameof(container));
  763. }
  764. var resElement = container.Element(UPnpNamespaces.Res);
  765. if (resElement != null)
  766. {
  767. var info = resElement.Attribute(UPnpNamespaces.ProtocolInfo);
  768. if (info != null && !string.IsNullOrWhiteSpace(info.Value))
  769. {
  770. return info.Value.Split(':');
  771. }
  772. }
  773. return new string[4];
  774. }
  775. private async Task<TransportCommands> GetAVProtocolAsync(CancellationToken cancellationToken)
  776. {
  777. if (AvCommands != null)
  778. {
  779. return AvCommands;
  780. }
  781. if (_disposed)
  782. {
  783. throw new ObjectDisposedException(GetType().Name);
  784. }
  785. var avService = GetAvTransportService();
  786. if (avService == null)
  787. {
  788. return null;
  789. }
  790. string url = NormalizeUrl(Properties.BaseUrl, avService.ScpdUrl);
  791. var httpClient = new SsdpHttpClient(_httpClientFactory);
  792. var document = await httpClient.GetDataAsync(url, cancellationToken).ConfigureAwait(false);
  793. if (document == null)
  794. {
  795. return null;
  796. }
  797. AvCommands = TransportCommands.Create(document);
  798. return AvCommands;
  799. }
  800. private async Task<TransportCommands> GetRenderingProtocolAsync(CancellationToken cancellationToken)
  801. {
  802. if (RendererCommands != null)
  803. {
  804. return RendererCommands;
  805. }
  806. if (_disposed)
  807. {
  808. throw new ObjectDisposedException(GetType().Name);
  809. }
  810. var avService = GetServiceRenderingControl();
  811. if (avService == null)
  812. {
  813. throw new ArgumentException("Device AvService is null");
  814. }
  815. string url = NormalizeUrl(Properties.BaseUrl, avService.ScpdUrl);
  816. var httpClient = new SsdpHttpClient(_httpClientFactory);
  817. _logger.LogDebug("Dlna Device.GetRenderingProtocolAsync");
  818. var document = await httpClient.GetDataAsync(url, cancellationToken).ConfigureAwait(false);
  819. if (document == null)
  820. {
  821. return null;
  822. }
  823. RendererCommands = TransportCommands.Create(document);
  824. return RendererCommands;
  825. }
  826. private string NormalizeUrl(string baseUrl, string url)
  827. {
  828. // If it's already a complete url, don't stick anything onto the front of it
  829. if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  830. {
  831. return url;
  832. }
  833. if (!url.Contains('/', StringComparison.Ordinal))
  834. {
  835. url = "/dmr/" + url;
  836. }
  837. if (!url.StartsWith('/'))
  838. {
  839. url = "/" + url;
  840. }
  841. return baseUrl + url;
  842. }
  843. public static async Task<Device> CreateuPnpDeviceAsync(Uri url, IHttpClientFactory httpClientFactory, ILogger logger, CancellationToken cancellationToken)
  844. {
  845. var ssdpHttpClient = new SsdpHttpClient(httpClientFactory);
  846. var document = await ssdpHttpClient.GetDataAsync(url.ToString(), cancellationToken).ConfigureAwait(false);
  847. if (document == null)
  848. {
  849. return null;
  850. }
  851. var friendlyNames = new List<string>();
  852. var name = document.Descendants(UPnpNamespaces.Ud.GetName("friendlyName")).FirstOrDefault();
  853. if (name != null && !string.IsNullOrWhiteSpace(name.Value))
  854. {
  855. friendlyNames.Add(name.Value);
  856. }
  857. var room = document.Descendants(UPnpNamespaces.Ud.GetName("roomName")).FirstOrDefault();
  858. if (room != null && !string.IsNullOrWhiteSpace(room.Value))
  859. {
  860. friendlyNames.Add(room.Value);
  861. }
  862. var deviceProperties = new DeviceInfo()
  863. {
  864. Name = string.Join(' ', friendlyNames),
  865. BaseUrl = string.Format(CultureInfo.InvariantCulture, "http://{0}:{1}", url.Host, url.Port)
  866. };
  867. var model = document.Descendants(UPnpNamespaces.Ud.GetName("modelName")).FirstOrDefault();
  868. if (model != null)
  869. {
  870. deviceProperties.ModelName = model.Value;
  871. }
  872. var modelNumber = document.Descendants(UPnpNamespaces.Ud.GetName("modelNumber")).FirstOrDefault();
  873. if (modelNumber != null)
  874. {
  875. deviceProperties.ModelNumber = modelNumber.Value;
  876. }
  877. var uuid = document.Descendants(UPnpNamespaces.Ud.GetName("UDN")).FirstOrDefault();
  878. if (uuid != null)
  879. {
  880. deviceProperties.UUID = uuid.Value;
  881. }
  882. var manufacturer = document.Descendants(UPnpNamespaces.Ud.GetName("manufacturer")).FirstOrDefault();
  883. if (manufacturer != null)
  884. {
  885. deviceProperties.Manufacturer = manufacturer.Value;
  886. }
  887. var manufacturerUrl = document.Descendants(UPnpNamespaces.Ud.GetName("manufacturerURL")).FirstOrDefault();
  888. if (manufacturerUrl != null)
  889. {
  890. deviceProperties.ManufacturerUrl = manufacturerUrl.Value;
  891. }
  892. var presentationUrl = document.Descendants(UPnpNamespaces.Ud.GetName("presentationURL")).FirstOrDefault();
  893. if (presentationUrl != null)
  894. {
  895. deviceProperties.PresentationUrl = presentationUrl.Value;
  896. }
  897. var modelUrl = document.Descendants(UPnpNamespaces.Ud.GetName("modelURL")).FirstOrDefault();
  898. if (modelUrl != null)
  899. {
  900. deviceProperties.ModelUrl = modelUrl.Value;
  901. }
  902. var serialNumber = document.Descendants(UPnpNamespaces.Ud.GetName("serialNumber")).FirstOrDefault();
  903. if (serialNumber != null)
  904. {
  905. deviceProperties.SerialNumber = serialNumber.Value;
  906. }
  907. var modelDescription = document.Descendants(UPnpNamespaces.Ud.GetName("modelDescription")).FirstOrDefault();
  908. if (modelDescription != null)
  909. {
  910. deviceProperties.ModelDescription = modelDescription.Value;
  911. }
  912. var icon = document.Descendants(UPnpNamespaces.Ud.GetName("icon")).FirstOrDefault();
  913. if (icon != null)
  914. {
  915. deviceProperties.Icon = CreateIcon(icon);
  916. }
  917. foreach (var services in document.Descendants(UPnpNamespaces.Ud.GetName("serviceList")))
  918. {
  919. if (services == null)
  920. {
  921. continue;
  922. }
  923. var servicesList = services.Descendants(UPnpNamespaces.Ud.GetName("service"));
  924. if (servicesList == null)
  925. {
  926. continue;
  927. }
  928. foreach (var element in servicesList)
  929. {
  930. var service = Create(element);
  931. if (service != null)
  932. {
  933. deviceProperties.Services.Add(service);
  934. }
  935. }
  936. }
  937. return new Device(deviceProperties, httpClientFactory, logger);
  938. }
  939. #nullable enable
  940. private static DeviceIcon CreateIcon(XElement element)
  941. {
  942. if (element == null)
  943. {
  944. throw new ArgumentNullException(nameof(element));
  945. }
  946. var width = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("width"));
  947. var height = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("height"));
  948. _ = int.TryParse(width, NumberStyles.Integer, CultureInfo.InvariantCulture, out var widthValue);
  949. _ = int.TryParse(height, NumberStyles.Integer, CultureInfo.InvariantCulture, out var heightValue);
  950. return new DeviceIcon
  951. {
  952. Depth = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("depth")) ?? string.Empty,
  953. Height = heightValue,
  954. MimeType = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("mimetype")) ?? string.Empty,
  955. Url = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("url")) ?? string.Empty,
  956. Width = widthValue
  957. };
  958. }
  959. private static DeviceService Create(XElement element)
  960. => new DeviceService()
  961. {
  962. ControlUrl = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("controlURL")) ?? string.Empty,
  963. EventSubUrl = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("eventSubURL")) ?? string.Empty,
  964. ScpdUrl = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("SCPDURL")) ?? string.Empty,
  965. ServiceId = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("serviceId")) ?? string.Empty,
  966. ServiceType = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("serviceType")) ?? string.Empty
  967. };
  968. private void UpdateMediaInfo(UBaseObject? mediaInfo, TransportState state)
  969. {
  970. TransportState = state;
  971. var previousMediaInfo = CurrentMediaInfo;
  972. CurrentMediaInfo = mediaInfo;
  973. if (mediaInfo == null)
  974. {
  975. if (previousMediaInfo != null)
  976. {
  977. OnPlaybackStop(previousMediaInfo);
  978. }
  979. }
  980. else if (previousMediaInfo == null)
  981. {
  982. if (state != TransportState.STOPPED)
  983. {
  984. OnPlaybackStart(mediaInfo);
  985. }
  986. }
  987. else if (mediaInfo.Equals(previousMediaInfo))
  988. {
  989. OnPlaybackProgress(mediaInfo);
  990. }
  991. else
  992. {
  993. OnMediaChanged(previousMediaInfo, mediaInfo);
  994. }
  995. }
  996. private void OnPlaybackStart(UBaseObject mediaInfo)
  997. {
  998. if (string.IsNullOrWhiteSpace(mediaInfo.Url))
  999. {
  1000. return;
  1001. }
  1002. PlaybackStart?.Invoke(this, new PlaybackStartEventArgs(mediaInfo));
  1003. }
  1004. private void OnPlaybackProgress(UBaseObject mediaInfo)
  1005. {
  1006. if (string.IsNullOrWhiteSpace(mediaInfo.Url))
  1007. {
  1008. return;
  1009. }
  1010. PlaybackProgress?.Invoke(this, new PlaybackProgressEventArgs(mediaInfo));
  1011. }
  1012. private void OnPlaybackStop(UBaseObject mediaInfo)
  1013. {
  1014. PlaybackStopped?.Invoke(this, new PlaybackStoppedEventArgs(mediaInfo));
  1015. }
  1016. private void OnMediaChanged(UBaseObject old, UBaseObject newMedia)
  1017. {
  1018. MediaChanged?.Invoke(this, new MediaChangedEventArgs(old, newMedia));
  1019. }
  1020. /// <inheritdoc />
  1021. public void Dispose()
  1022. {
  1023. Dispose(true);
  1024. GC.SuppressFinalize(this);
  1025. }
  1026. /// <summary>
  1027. /// Releases unmanaged and optionally managed resources.
  1028. /// </summary>
  1029. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  1030. protected virtual void Dispose(bool disposing)
  1031. {
  1032. if (_disposed)
  1033. {
  1034. return;
  1035. }
  1036. if (disposing)
  1037. {
  1038. _timer?.Dispose();
  1039. }
  1040. _timer = null;
  1041. Properties = null;
  1042. _disposed = true;
  1043. }
  1044. /// <inheritdoc />
  1045. public override string ToString()
  1046. {
  1047. return string.Format(CultureInfo.InvariantCulture, "{0} - {1}", Properties.Name, Properties.BaseUrl);
  1048. }
  1049. }
  1050. }