Device.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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 || TransportState == TransportState.PausedPlayback;
  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;
  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.Item2;
  423. if (tuple.Item1 && 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, UBaseObject)> 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. private static DeviceIcon CreateIcon(XElement element)
  940. {
  941. if (element == null)
  942. {
  943. throw new ArgumentNullException(nameof(element));
  944. }
  945. var mimeType = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("mimetype"));
  946. var width = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("width"));
  947. var height = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("height"));
  948. var depth = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("depth"));
  949. var url = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("url"));
  950. var widthValue = int.Parse(width, NumberStyles.Integer, CultureInfo.InvariantCulture);
  951. var heightValue = int.Parse(height, NumberStyles.Integer, CultureInfo.InvariantCulture);
  952. return new DeviceIcon
  953. {
  954. Depth = depth,
  955. Height = heightValue,
  956. MimeType = mimeType,
  957. Url = url,
  958. Width = widthValue
  959. };
  960. }
  961. private static DeviceService Create(XElement element)
  962. {
  963. var type = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("serviceType"));
  964. var id = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("serviceId"));
  965. var scpdUrl = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("SCPDURL"));
  966. var controlURL = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("controlURL"));
  967. var eventSubURL = element.GetDescendantValue(UPnpNamespaces.Ud.GetName("eventSubURL"));
  968. return new DeviceService
  969. {
  970. ControlUrl = controlURL,
  971. EventSubUrl = eventSubURL,
  972. ScpdUrl = scpdUrl,
  973. ServiceId = id,
  974. ServiceType = type
  975. };
  976. }
  977. private void UpdateMediaInfo(UBaseObject mediaInfo, TransportState state)
  978. {
  979. TransportState = state;
  980. var previousMediaInfo = CurrentMediaInfo;
  981. CurrentMediaInfo = mediaInfo;
  982. if (previousMediaInfo == null && mediaInfo != null)
  983. {
  984. if (state != TransportState.Stopped)
  985. {
  986. OnPlaybackStart(mediaInfo);
  987. }
  988. }
  989. else if (mediaInfo != null && previousMediaInfo != null && !mediaInfo.Equals(previousMediaInfo))
  990. {
  991. OnMediaChanged(previousMediaInfo, mediaInfo);
  992. }
  993. else if (mediaInfo == null && previousMediaInfo != null)
  994. {
  995. OnPlaybackStop(previousMediaInfo);
  996. }
  997. else if (mediaInfo != null && mediaInfo.Equals(previousMediaInfo))
  998. {
  999. OnPlaybackProgress(mediaInfo);
  1000. }
  1001. }
  1002. private void OnPlaybackStart(UBaseObject mediaInfo)
  1003. {
  1004. if (string.IsNullOrWhiteSpace(mediaInfo.Url))
  1005. {
  1006. return;
  1007. }
  1008. PlaybackStart?.Invoke(this, new PlaybackStartEventArgs(mediaInfo));
  1009. }
  1010. private void OnPlaybackProgress(UBaseObject mediaInfo)
  1011. {
  1012. if (string.IsNullOrWhiteSpace(mediaInfo.Url))
  1013. {
  1014. return;
  1015. }
  1016. PlaybackProgress?.Invoke(this, new PlaybackProgressEventArgs(mediaInfo));
  1017. }
  1018. private void OnPlaybackStop(UBaseObject mediaInfo)
  1019. {
  1020. PlaybackStopped?.Invoke(this, new PlaybackStoppedEventArgs(mediaInfo));
  1021. }
  1022. private void OnMediaChanged(UBaseObject old, UBaseObject newMedia)
  1023. {
  1024. MediaChanged?.Invoke(this, new MediaChangedEventArgs(old, newMedia));
  1025. }
  1026. /// <inheritdoc />
  1027. public void Dispose()
  1028. {
  1029. Dispose(true);
  1030. GC.SuppressFinalize(this);
  1031. }
  1032. /// <summary>
  1033. /// Releases unmanaged and optionally managed resources.
  1034. /// </summary>
  1035. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  1036. protected virtual void Dispose(bool disposing)
  1037. {
  1038. if (_disposed)
  1039. {
  1040. return;
  1041. }
  1042. if (disposing)
  1043. {
  1044. _timer?.Dispose();
  1045. }
  1046. _timer = null;
  1047. Properties = null;
  1048. _disposed = true;
  1049. }
  1050. /// <inheritdoc />
  1051. public override string ToString()
  1052. {
  1053. return string.Format(CultureInfo.InvariantCulture, "{0} - {1}", Properties.Name, Properties.BaseUrl);
  1054. }
  1055. }
  1056. }