-
Notifications
You must be signed in to change notification settings - Fork 20
/
UPnP.cs
274 lines (226 loc) · 9.67 KB
/
UPnP.cs
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
namespace RoliSoft.TVShowTracker
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows;
using OpenSource.UPnP;
using OpenSource.UPnP.AV.CdsMetadata;
using OpenSource.UPnP.AV.MediaServer.DV;
using RoliSoft.TVShowTracker.Parsers.Downloads;
using RoliSoft.TVShowTracker.Parsers.Guides;
using RoliSoft.TVShowTracker.Parsers.Subtitles;
using RoliSoft.TVShowTracker.TaskDialogs;
/// <summary>
/// Provides support for streaming the library to UPnP/DLNA-compatible devices.
/// </summary>
public static class UPnP
{
private static DeviceInfo _di;
private static MediaServerDevice _ms;
private static UPnPDeviceWatcher _dw;
private static Image _icon, _icon2;
private static Icon _favicon;
private static HashSet<string> _mimes;
/// <summary>
/// Gets or sets a value indicating whether this server is running.
/// </summary>
/// <value><c>true</c> if this server is running; otherwise, <c>false</c>.</value>
public static bool IsRunning { get; set; }
/// <summary>
/// Initializes static members of the <see cref="UPnP"/> class.
/// </summary>
static UPnP()
{
_di = new DeviceInfo
{
AllowRemoteContentManagement = true,
FriendlyName = Signature.Software + " on " + Environment.MachineName,
Manufacturer = Signature.Developer,
ManufacturerURL = "http://lab.rolisoft.net/tvshowtracker.html",
ModelName = "RS TV Show Tracker",
ModelDescription = "Provides access to TV shows tracked by " + Signature.Software + " on " + Environment.MachineName,
ModelURL = "http://lab.rolisoft.net/tvshowtracker.html",
ModelNumber = Signature.Version,
LocalRootDirectory = "",
SearchCapabilities = "dc:title,dc:creator,upnp:class,upnp:album,res@protocolInfo,res@size,res@bitrate",
SortCapabilities = "dc:title,dc:creator,upnp:class,upnp:album",
EnableSearch = true,
CacheTime = 1800,
CustomUDN = "",
INMPR03 = true
};
MediaObject.ENCODE_UTF8 = false;
}
/// <summary>
/// Starts the UPnP server.
/// </summary>
public static void Start()
{
if (IsRunning)
{
Log.Info("An UPnP/DLNA server start was requested, but it is already running.");
Stop();
}
if (!Signature.IsActivated)
{
Log.Error("An UPnP/DLNA server start was requested, but the software is not activated.");
return;
}
Log.Info("Starting UPnP/DLNA server...");
_ms = new MediaServerDevice(_di, null, true, "http-get:*:*:*", "");
_dw = new UPnPDeviceWatcher(_ms._Device);
var dv = _ms.GetUPnPDevice();
if (_favicon == null)
{
_favicon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,/RSTVShowTracker;component/tv.ico")).Stream);
_icon = new Bitmap(Application.GetResourceStream(new Uri("pack://application:,,,/RSTVShowTracker;component/Images/tv48.png")).Stream);
_icon2 = new Bitmap(Application.GetResourceStream(new Uri("pack://application:,,,/RSTVShowTracker;component/Images/tv64.png")).Stream);
}
dv.favicon = _favicon;
dv.Icon = _icon;
dv.Icon2 = _icon2;
_ms.Start();
IsRunning = true;
_mimes = new HashSet<string>();
RebuildList();
}
/// <summary>
/// Stops the UPnP server.
/// </summary>
public static void Stop()
{
Log.Info("Stopping UPnP/DLNA server...");
IsRunning = false;
_ms.Stop();
_ms.Dispose();
_ms = null;
_dw = null;
}
/// <summary>
/// Resets the hierarchy from the root.
/// </summary>
public static void RebuildList()
{
if (!Signature.IsActivated && IsRunning)
{
Stop();
return;
}
var st = DateTime.Now;
Log.Debug("Rebuilding UPnP/DLNA library...");
MediaBuilder.SetNextID(0);
var allEps = DvMediaBuilder.CreateContainer(new MediaBuilder.container("-- All Episodes --") { IsRestricted = true });
var unwEps = DvMediaBuilder.CreateContainer(new MediaBuilder.container("-- Unwatched Episodes --") { IsRestricted = true });
_ms.Root.AddObject(allEps, true);
_ms.Root.AddObject(unwEps, true);
var shows = new Dictionary<int, DvMediaContainer>();
foreach (var tvs in Database.TVShows.OrderBy(t => t.Value.Title))
{
shows[tvs.Key] = DvMediaBuilder.CreateContainer(new MediaBuilder.container(tvs.Value.Title) { IsRestricted = true });
}
foreach (var item in Library.Files)
{
var sid = (int)Math.Floor((double)item.Key / 1000 / 1000);
TVShow show;
if (!Database.TVShows.TryGetValue(sid, out show))
{
continue;
}
Episode ep;
if (!show.EpisodeByID.TryGetValue(item.Key - (sid * 1000 * 1000), out ep))
{
continue;
}
var txt = string.Format("S{0:00}E{1:00}: {2}", ep.Season, ep.Number, ep.Title);
foreach (var file in Library.Files[item.Key])
{
if (OpenArchiveTaskDialog.SupportedArchives.Contains(Path.GetExtension(file).ToLower()))
{
continue;
}
var ltxt = txt;
var q = FileNames.Parser.ParseQuality(file);
if (q != Qualities.Unknown)
{
ltxt += " [" + q.GetAttribute<DescriptionAttribute>().Description + "]";
}
else
{
var ed = FileNames.Parser.ParseEdition(file);
if (ed != Editions.Unknown)
{
ltxt += " [" + ed.GetAttribute<DescriptionAttribute>().Description + "]";
}
else
{
ltxt += " [" + Path.GetExtension(file) + "]";
}
}
allEps.AddBranch(CreateObject(show.Title + " " + ltxt, file));
if (!ep.Watched)
{
unwEps.AddBranch(CreateObject(show.Title + " " + ltxt, file));
}
shows[sid].AddBranch(CreateObject(ltxt, file));
}
}
foreach (var tvs in shows)
{
if (tvs.Value.ChildCount != 0)
{
_ms.Root.AddObject(tvs.Value, true);
}
}
Log.Debug("Rebuilt UPnP/DLNA library in " + (DateTime.Now - st).TotalSeconds + "s, all episodes menu has " + allEps.ChildCount + " entries.");
}
/// <summary>
/// Enumerates the currently transfered files.
/// </summary>
/// <returns>List of active transfer names.</returns>
public static IEnumerable<FileInfo> GetActiveTransfers()
{
foreach (MediaServerDevice.HttpTransfer t in _ms.HttpTransfers)
{
yield return (FileInfo)t.Resource.Tag;
}
}
/// <summary>
/// Creates a media object.
/// </summary>
/// <param name="title">The title of the object.</param>
/// <param name="file">The full path to the file.</param>
/// <returns>Media object.</returns>
private static IDvMedia CreateObject(string title, string file)
{
var fi = new FileInfo(file);
var media = DvMediaBuilder.CreateItem(new MediaBuilder.item(title));
string mime, mediaClass;
MimeTypes.ExtensionToMimeType(fi.Extension, out mime, out mediaClass);
var resInfo = new ResourceBuilder.VideoItem
{
contentUri = MediaResource.AUTOMAPFILE + fi.FullName,
protocolInfo = new ProtocolInfoString("http-get:*:" + mime + ":*"),
size = new _ULong((ulong)fi.Length)
};
var res = DvResourceBuilder.CreateResource(resInfo, true);
res.Tag = fi;
media.AddResource(res);
if (_mimes.Contains(mime) == false)
{
_mimes.Add(mime);
var ps = new ProtocolInfoString[_mimes.Count];
var i = 0;
foreach (var mime2 in _mimes)
{
ps[i++] = new ProtocolInfoString("http-get:*:" + mime2 + ":*");
}
_ms.SourceProtocolInfoSet = ps;
}
return media;
}
}
}