1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dlna;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Dlna.ContentDirectory;
using MediaBrowser.Dlna.PlayTo;
using MediaBrowser.Dlna.Ssdp;
using MediaBrowser.Model.Channels;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Dlna.Channels
{
public class DlnaChannelFactory : IChannelFactory, IDisposable
{
private readonly IServerConfigurationManager _config;
private readonly ILogger _logger;
private readonly IHttpClient _httpClient;
private DeviceDiscovery _deviceDiscovery;
private readonly SemaphoreSlim _syncLock = new SemaphoreSlim(1, 1);
private List<Device> _servers = new List<Device>();
public static DlnaChannelFactory Instance;
private Func<List<string>> _localServersLookup;
public DlnaChannelFactory(IServerConfigurationManager config, IHttpClient httpClient, ILogger logger)
{
_config = config;
_httpClient = httpClient;
_logger = logger;
Instance = this;
}
internal void Start(DeviceDiscovery deviceDiscovery, Func<List<string>> localServersLookup)
{
_localServersLookup = localServersLookup;
_deviceDiscovery = deviceDiscovery;
//deviceDiscovery.DeviceDiscovered += deviceDiscovery_DeviceDiscovered;
deviceDiscovery.DeviceLeft += deviceDiscovery_DeviceLeft;
}
async void deviceDiscovery_DeviceDiscovered(object sender, SsdpMessageEventArgs e)
{
string usn;
if (!e.Headers.TryGetValue("USN", out usn)) usn = string.Empty;
string nt;
if (!e.Headers.TryGetValue("NT", out nt)) nt = string.Empty;
string location;
if (!e.Headers.TryGetValue("Location", out location)) location = string.Empty;
if (!IsValid(nt, usn))
{
return;
}
if (_localServersLookup != null)
{
if (_localServersLookup().Any(i => usn.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1))
{
// Don't add the local Dlna server to this
return;
}
}
if (GetExistingServers(usn).Any())
{
return;
}
await _syncLock.WaitAsync().ConfigureAwait(false);
try
{
if (GetExistingServers(usn).Any())
{
return;
}
var device = await Device.CreateuPnpDeviceAsync(new Uri(location), _httpClient, _config, _logger)
.ConfigureAwait(false);
if (!_servers.Any(i => string.Equals(i.Properties.UUID, device.Properties.UUID, StringComparison.OrdinalIgnoreCase)))
{
_servers.Add(device);
}
}
catch (Exception ex)
{
}
finally
{
_syncLock.Release();
}
}
async void deviceDiscovery_DeviceLeft(object sender, SsdpMessageEventArgs e)
{
string usn;
if (!e.Headers.TryGetValue("USN", out usn)) usn = String.Empty;
string nt;
if (!e.Headers.TryGetValue("NT", out nt)) nt = String.Empty;
if (!IsValid(nt, usn))
{
return;
}
if (!GetExistingServers(usn).Any())
{
return;
}
await _syncLock.WaitAsync().ConfigureAwait(false);
try
{
var list = _servers.ToList();
foreach (var device in GetExistingServers(usn).ToList())
{
list.Remove(device);
}
_servers = list;
}
finally
{
_syncLock.Release();
}
}
private bool IsValid(string nt, string usn)
{
// It has to report that it's a media renderer
if (usn.IndexOf("ContentDirectory:", StringComparison.OrdinalIgnoreCase) == -1 &&
nt.IndexOf("ContentDirectory:", StringComparison.OrdinalIgnoreCase) == -1 &&
usn.IndexOf("MediaServer:", StringComparison.OrdinalIgnoreCase) == -1 &&
nt.IndexOf("MediaServer:", StringComparison.OrdinalIgnoreCase) == -1)
{
return false;
}
return true;
}
private IEnumerable<Device> GetExistingServers(string usn)
{
return _servers
.Where(i => usn.IndexOf(i.Properties.UUID, StringComparison.OrdinalIgnoreCase) != -1);
}
public IEnumerable<IChannel> GetChannels()
{
//if (_servers.Count > 0)
//{
// var service = _servers[0].Properties.Services
// .FirstOrDefault(i => string.Equals(i.ServiceType, "urn:schemas-upnp-org:service:ContentDirectory:1", StringComparison.OrdinalIgnoreCase));
// var controlUrl = service == null ? null : (_servers[0].Properties.BaseUrl.TrimEnd('/') + "/" + service.ControlUrl.TrimStart('/'));
// if (!string.IsNullOrEmpty(controlUrl))
// {
// return new List<IChannel>
// {
// new ServerChannel(_servers.ToList(), _httpClient, _logger, controlUrl)
// };
// }
//}
return new List<IChannel>();
}
public void Dispose()
{
if (_deviceDiscovery != null)
{
_deviceDiscovery.DeviceDiscovered -= deviceDiscovery_DeviceDiscovered;
_deviceDiscovery.DeviceLeft -= deviceDiscovery_DeviceLeft;
}
}
}
public class ServerChannel : IChannel, IFactoryChannel
{
private readonly IHttpClient _httpClient;
private readonly ILogger _logger;
public string ControlUrl { get; set; }
public List<Device> Servers { get; set; }
public ServerChannel(IHttpClient httpClient, ILogger logger)
{
_httpClient = httpClient;
_logger = logger;
Servers = new List<Device>();
}
public string Name
{
get { return "Devices"; }
}
public string Description
{
get { return string.Empty; }
}
public string DataVersion
{
get { return DateTime.UtcNow.Ticks.ToString(); }
}
public string HomePageUrl
{
get { return string.Empty; }
}
public ChannelParentalRating ParentalRating
{
get { return ChannelParentalRating.GeneralAudience; }
}
public InternalChannelFeatures GetChannelFeatures()
{
return new InternalChannelFeatures
{
ContentTypes = new List<ChannelMediaContentType>
{
ChannelMediaContentType.Song,
ChannelMediaContentType.Clip
},
MediaTypes = new List<ChannelMediaType>
{
ChannelMediaType.Audio,
ChannelMediaType.Video,
ChannelMediaType.Photo
}
};
}
public bool IsEnabledFor(string userId)
{
return true;
}
public async Task<ChannelItemResult> GetChannelItems(InternalChannelItemQuery query, CancellationToken cancellationToken)
{
IEnumerable<ChannelItemInfo> items;
if (string.IsNullOrWhiteSpace(query.FolderId))
{
items = Servers.Select(i => new ChannelItemInfo
{
FolderType = ChannelFolderType.Container,
Id = GetServerId(i),
Name = i.Properties.Name,
Overview = i.Properties.ModelDescription,
Type = ChannelItemType.Folder
});
}
else
{
var idParts = query.FolderId.Split('|');
var folderId = idParts.Length == 2 ? idParts[1] : null;
var result = await new ContentDirectoryBrowser(_httpClient, _logger).Browse(new ContentDirectoryBrowseRequest
{
Limit = query.Limit,
StartIndex = query.StartIndex,
ParentId = folderId,
ContentDirectoryUrl = ControlUrl
}, cancellationToken).ConfigureAwait(false);
items = result.Items.ToList();
}
var list = items.ToList();
var count = list.Count;
list = ApplyPaging(list, query).ToList();
return new ChannelItemResult
{
Items = list,
TotalRecordCount = count
};
}
private string GetServerId(Device device)
{
return device.Properties.UUID.GetMD5().ToString("N");
}
private IEnumerable<T> ApplyPaging<T>(IEnumerable<T> items, InternalChannelItemQuery query)
{
if (query.StartIndex.HasValue)
{
items = items.Skip(query.StartIndex.Value);
}
if (query.Limit.HasValue)
{
items = items.Take(query.Limit.Value);
}
return items;
}
public Task<DynamicImageResponse> GetChannelImage(ImageType type, CancellationToken cancellationToken)
{
// TODO: Implement
return Task.FromResult(new DynamicImageResponse
{
HasImage = false
});
}
public IEnumerable<ImageType> GetSupportedChannelImages()
{
return new List<ImageType>
{
ImageType.Primary
};
}
}
}
|