DlnaChannelFactory.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Net;
  7. using MediaBrowser.Controller.Channels;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Controller.Dlna;
  10. using MediaBrowser.Controller.Providers;
  11. using MediaBrowser.Dlna.ContentDirectory;
  12. using MediaBrowser.Dlna.PlayTo;
  13. using MediaBrowser.Model.Channels;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.Logging;
  16. namespace MediaBrowser.Dlna.Channels
  17. {
  18. //public class DlnaChannel : IChannel, IDisposable
  19. //{
  20. // private readonly ILogger _logger;
  21. // private readonly IHttpClient _httpClient;
  22. // private readonly IServerConfigurationManager _config;
  23. // private List<Device> _servers = new List<Device>();
  24. // private readonly IDeviceDiscovery _deviceDiscovery;
  25. // private readonly SemaphoreSlim _syncLock = new SemaphoreSlim(1, 1);
  26. // private Func<List<string>> _localServersLookup;
  27. // public static DlnaChannel Current;
  28. // public DlnaChannel(ILogger logger, IHttpClient httpClient, IDeviceDiscovery deviceDiscovery, IServerConfigurationManager config)
  29. // {
  30. // _logger = logger;
  31. // _httpClient = httpClient;
  32. // _deviceDiscovery = deviceDiscovery;
  33. // _config = config;
  34. // Current = this;
  35. // }
  36. // public string Name
  37. // {
  38. // get { return "Devices"; }
  39. // }
  40. // public string Description
  41. // {
  42. // get { return string.Empty; }
  43. // }
  44. // public string DataVersion
  45. // {
  46. // get { return DateTime.UtcNow.Ticks.ToString(); }
  47. // }
  48. // public string HomePageUrl
  49. // {
  50. // get { return string.Empty; }
  51. // }
  52. // public ChannelParentalRating ParentalRating
  53. // {
  54. // get { return ChannelParentalRating.GeneralAudience; }
  55. // }
  56. // public InternalChannelFeatures GetChannelFeatures()
  57. // {
  58. // return new InternalChannelFeatures
  59. // {
  60. // ContentTypes = new List<ChannelMediaContentType>
  61. // {
  62. // ChannelMediaContentType.Song,
  63. // ChannelMediaContentType.Clip
  64. // },
  65. // MediaTypes = new List<ChannelMediaType>
  66. // {
  67. // ChannelMediaType.Audio,
  68. // ChannelMediaType.Video,
  69. // ChannelMediaType.Photo
  70. // }
  71. // };
  72. // }
  73. // public bool IsEnabledFor(string userId)
  74. // {
  75. // return true;
  76. // }
  77. // public Task<DynamicImageResponse> GetChannelImage(ImageType type, CancellationToken cancellationToken)
  78. // {
  79. // throw new NotImplementedException();
  80. // }
  81. // public IEnumerable<ImageType> GetSupportedChannelImages()
  82. // {
  83. // return new List<ImageType>
  84. // {
  85. // ImageType.Primary
  86. // };
  87. // }
  88. // public void Start(Func<List<string>> localServersLookup)
  89. // {
  90. // _localServersLookup = localServersLookup;
  91. // _deviceDiscovery.DeviceDiscovered -= deviceDiscovery_DeviceDiscovered;
  92. // _deviceDiscovery.DeviceLeft -= deviceDiscovery_DeviceLeft;
  93. // _deviceDiscovery.DeviceDiscovered += deviceDiscovery_DeviceDiscovered;
  94. // _deviceDiscovery.DeviceLeft += deviceDiscovery_DeviceLeft;
  95. // }
  96. // public async Task<ChannelItemResult> GetChannelItems(InternalChannelItemQuery query, CancellationToken cancellationToken)
  97. // {
  98. // if (string.IsNullOrWhiteSpace(query.FolderId))
  99. // {
  100. // return await GetServers(query, cancellationToken).ConfigureAwait(false);
  101. // }
  102. // return new ChannelItemResult();
  103. // //var idParts = query.FolderId.Split('|');
  104. // //var folderId = idParts.Length == 2 ? idParts[1] : null;
  105. // //var result = await new ContentDirectoryBrowser(_httpClient, _logger).Browse(new ContentDirectoryBrowseRequest
  106. // //{
  107. // // Limit = query.Limit,
  108. // // StartIndex = query.StartIndex,
  109. // // ParentId = folderId,
  110. // // ContentDirectoryUrl = ControlUrl
  111. // //}, cancellationToken).ConfigureAwait(false);
  112. // //items = result.Items.ToList();
  113. // //var list = items.ToList();
  114. // //var count = list.Count;
  115. // //list = ApplyPaging(list, query).ToList();
  116. // //return new ChannelItemResult
  117. // //{
  118. // // Items = list,
  119. // // TotalRecordCount = count
  120. // //};
  121. // }
  122. // public async Task<ChannelItemResult> GetServers(InternalChannelItemQuery query, CancellationToken cancellationToken)
  123. // {
  124. // await _syncLock.WaitAsync(cancellationToken).ConfigureAwait(false);
  125. // try
  126. // {
  127. // var items = _servers.Select(i =>
  128. // {
  129. // var service = i.Properties.Services
  130. // .FirstOrDefault(s => string.Equals(s.ServiceType, "urn:schemas-upnp-org:service:ContentDirectory:1", StringComparison.OrdinalIgnoreCase));
  131. // var controlUrl = service == null ? null : (_servers[0].Properties.BaseUrl.TrimEnd('/') + "/" + service.ControlUrl.TrimStart('/'));
  132. // if (string.IsNullOrWhiteSpace(controlUrl))
  133. // {
  134. // return null;
  135. // }
  136. // return new ChannelItemInfo
  137. // {
  138. // Id = i.Properties.UUID,
  139. // Name = i.Properties.Name,
  140. // Type = ChannelItemType.Folder
  141. // };
  142. // }).Where(i => i != null).ToList();
  143. // return new ChannelItemResult
  144. // {
  145. // TotalRecordCount = items.Count,
  146. // Items = items
  147. // };
  148. // }
  149. // finally
  150. // {
  151. // _syncLock.Release();
  152. // }
  153. // }
  154. // async void deviceDiscovery_DeviceDiscovered(object sender, SsdpMessageEventArgs e)
  155. // {
  156. // string usn;
  157. // if (!e.Headers.TryGetValue("USN", out usn)) usn = string.Empty;
  158. // string nt;
  159. // if (!e.Headers.TryGetValue("NT", out nt)) nt = string.Empty;
  160. // string location;
  161. // if (!e.Headers.TryGetValue("Location", out location)) location = string.Empty;
  162. // if (!IsValid(nt, usn))
  163. // {
  164. // return;
  165. // }
  166. // if (_localServersLookup != null)
  167. // {
  168. // if (_localServersLookup().Any(i => usn.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1))
  169. // {
  170. // // Don't add the local Dlna server to this
  171. // return;
  172. // }
  173. // }
  174. // await _syncLock.WaitAsync().ConfigureAwait(false);
  175. // var serverList = _servers.ToList();
  176. // try
  177. // {
  178. // if (GetExistingServers(serverList, usn).Any())
  179. // {
  180. // return;
  181. // }
  182. // var device = await Device.CreateuPnpDeviceAsync(new Uri(location), _httpClient, _config, _logger)
  183. // .ConfigureAwait(false);
  184. // if (!serverList.Any(i => string.Equals(i.Properties.UUID, device.Properties.UUID, StringComparison.OrdinalIgnoreCase)))
  185. // {
  186. // serverList.Add(device);
  187. // }
  188. // }
  189. // catch (Exception ex)
  190. // {
  191. // }
  192. // finally
  193. // {
  194. // _syncLock.Release();
  195. // }
  196. // }
  197. // async void deviceDiscovery_DeviceLeft(object sender, SsdpMessageEventArgs e)
  198. // {
  199. // string usn;
  200. // if (!e.Headers.TryGetValue("USN", out usn)) usn = String.Empty;
  201. // string nt;
  202. // if (!e.Headers.TryGetValue("NT", out nt)) nt = String.Empty;
  203. // if (!IsValid(nt, usn))
  204. // {
  205. // return;
  206. // }
  207. // await _syncLock.WaitAsync().ConfigureAwait(false);
  208. // try
  209. // {
  210. // var serverList = _servers.ToList();
  211. // var matchingServers = GetExistingServers(serverList, usn);
  212. // if (matchingServers.Count > 0)
  213. // {
  214. // foreach (var device in matchingServers)
  215. // {
  216. // serverList.Remove(device);
  217. // }
  218. // _servers = serverList;
  219. // }
  220. // }
  221. // finally
  222. // {
  223. // _syncLock.Release();
  224. // }
  225. // }
  226. // private bool IsValid(string nt, string usn)
  227. // {
  228. // // It has to report that it's a media renderer
  229. // if (usn.IndexOf("ContentDirectory:", StringComparison.OrdinalIgnoreCase) == -1 &&
  230. // nt.IndexOf("ContentDirectory:", StringComparison.OrdinalIgnoreCase) == -1 &&
  231. // usn.IndexOf("MediaServer:", StringComparison.OrdinalIgnoreCase) == -1 &&
  232. // nt.IndexOf("MediaServer:", StringComparison.OrdinalIgnoreCase) == -1)
  233. // {
  234. // return false;
  235. // }
  236. // return true;
  237. // }
  238. // private List<Device> GetExistingServers(List<Device> allDevices, string usn)
  239. // {
  240. // return allDevices
  241. // .Where(i => usn.IndexOf(i.Properties.UUID, StringComparison.OrdinalIgnoreCase) != -1)
  242. // .ToList();
  243. // }
  244. // public void Dispose()
  245. // {
  246. // _deviceDiscovery.DeviceDiscovered -= deviceDiscovery_DeviceDiscovered;
  247. // _deviceDiscovery.DeviceLeft -= deviceDiscovery_DeviceLeft;
  248. // }
  249. //}
  250. }