Device.cs 44 KB

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