Device.cs 40 KB

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