Device.cs 41 KB

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