Device.cs 36 KB

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