forked from FreakyJ/MediaServer-fo-MP2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UPnPContentDirectoryServiceImpl.cs
376 lines (329 loc) · 21.1 KB
/
UPnPContentDirectoryServiceImpl.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using MediaPortal.Backend.MediaLibrary;
using MediaPortal.Common;
using MediaPortal.Common.Logging;
using MediaPortal.Common.MediaManagement.DefaultItemAspects;
using MediaPortal.Common.MediaManagement.MLQueries;
using MediaPortal.Extensions.MediaServer.DIDL;
using MediaPortal.Extensions.MediaServer.Parser;
using MediaPortal.Utilities.UPnP;
using Peg.Base;
using UPnP.Infrastructure.Common;
using UPnP.Infrastructure.Dv;
using UPnP.Infrastructure.Dv.DeviceTree;
namespace MediaPortal.Extensions.MediaServer
{
public class UPnPContentDirectoryServiceImpl : DvService
{
public UPnPContentDirectoryServiceImpl()
: base(
UPnPMediaServerDevice.CONTENT_DIRECTORY_SERVICE_TYPE, UPnPMediaServerDevice.CONTENT_DIRECTORY_SERVICE_TYPE_VERSION,
UPnPMediaServerDevice.CONTENT_DIRECTORY_SERVICE_ID)
{
// Used for a boolean value
DvStateVariable A_ARG_TYPE_BrowseFlag = new DvStateVariable("A_ARG_TYPE_BrowseFlag",
new DvStandardDataType(
UPnPStandardDataType.String))
{
SendEvents = false,
AllowedValueList = new List<string>() { "BrowseMetadata", "BrowseDirectChildren" }
};
AddStateVariable(A_ARG_TYPE_BrowseFlag);
DvStateVariable A_ARG_TYPE_Count = new DvStateVariable("A_ARG_TYPE_Count",
new DvStandardDataType(UPnPStandardDataType.Ui4))
{
SendEvents = false
};
AddStateVariable(A_ARG_TYPE_Count);
DvStateVariable A_ARG_TYPE_Filter = new DvStateVariable("A_ARG_TYPE_Filter",
new DvStandardDataType(UPnPStandardDataType.String))
{
SendEvents = false
};
AddStateVariable(A_ARG_TYPE_Filter);
DvStateVariable A_ARG_TYPE_Index = new DvStateVariable("A_ARG_TYPE_Index",
new DvStandardDataType(UPnPStandardDataType.Ui4))
{
SendEvents = false
};
AddStateVariable(A_ARG_TYPE_Index);
DvStateVariable A_ARG_TYPE_ObjectID = new DvStateVariable("A_ARG_TYPE_ObjectID",
new DvStandardDataType(UPnPStandardDataType.String))
{
SendEvents = false
};
AddStateVariable(A_ARG_TYPE_ObjectID);
DvStateVariable A_ARG_TYPE_Result = new DvStateVariable("A_ARG_TYPE_Result",
new DvStandardDataType(UPnPStandardDataType.String))
{
SendEvents = false
};
AddStateVariable(A_ARG_TYPE_Result);
DvStateVariable A_ARG_TYPE_SearchCriteria = new DvStateVariable("A_ARG_TYPE_SearchCriteria",
new DvStandardDataType(
UPnPStandardDataType.String))
{
SendEvents = false
};
AddStateVariable(A_ARG_TYPE_SearchCriteria);
DvStateVariable A_ARG_TYPE_SortCriteria = new DvStateVariable("A_ARG_TYPE_SortCriteria",
new DvStandardDataType(
UPnPStandardDataType.String))
{
SendEvents = false
};
AddStateVariable(A_ARG_TYPE_SortCriteria);
DvStateVariable A_ARG_TYPE_UpdateID = new DvStateVariable("A_ARG_TYPE_UpdateID",
new DvStandardDataType(UPnPStandardDataType.Ui4))
{
SendEvents = false
};
AddStateVariable(A_ARG_TYPE_UpdateID);
DvStateVariable SearchCapabilities = new DvStateVariable("SearchCapabilities",
new DvStandardDataType(UPnPStandardDataType.String))
{
SendEvents = false
};
AddStateVariable(SearchCapabilities);
DvStateVariable SortCapabilities = new DvStateVariable("SortCapabilities",
new DvStandardDataType(UPnPStandardDataType.String))
{
SendEvents = false
};
AddStateVariable(SortCapabilities);
DvStateVariable SystemUpdateID = new DvStateVariable("SystemUpdateID",
new DvStandardDataType(UPnPStandardDataType.Ui4))
{
SendEvents = true
};
AddStateVariable(SystemUpdateID);
DvAction browseAction = new DvAction("Browse", OnBrowse,
new DvArgument[]
{
new DvArgument("ObjectID", A_ARG_TYPE_ObjectID,
ArgumentDirection.In),
new DvArgument("BrowseFlag", A_ARG_TYPE_BrowseFlag,
ArgumentDirection.In),
new DvArgument("Filter", A_ARG_TYPE_Filter,
ArgumentDirection.In),
new DvArgument("StartingIndex",
A_ARG_TYPE_Index,
ArgumentDirection.In),
new DvArgument("RequestedCount",
A_ARG_TYPE_Count,
ArgumentDirection.In),
new DvArgument("SortCriteria",
A_ARG_TYPE_SortCriteria,
ArgumentDirection.In)
},
new DvArgument[]
{
new DvArgument("Result",
A_ARG_TYPE_Result,
ArgumentDirection.Out),
new DvArgument("NumberReturned",
A_ARG_TYPE_Count,
ArgumentDirection.Out),
new DvArgument("TotalMatches",
A_ARG_TYPE_Count,
ArgumentDirection.Out),
new DvArgument("UpdateID",
A_ARG_TYPE_Count,
ArgumentDirection.Out)
});
AddAction(browseAction);
DvAction getSearchCapabilitiesAction = new DvAction("GetSearchCapabilities", OnGetSearchCapabilities,
new DvArgument[]
{
},
new DvArgument[]
{
new DvArgument("SearchCaps",
SearchCapabilities,
ArgumentDirection.Out),
});
AddAction(getSearchCapabilitiesAction);
DvAction getSortCapabilitiesAction = new DvAction("GetSortCapabilities", OnGetSortCapabilities,
new DvArgument[]
{
},
new DvArgument[]
{
new DvArgument("SortCaps",
SortCapabilities,
ArgumentDirection.Out),
});
AddAction(getSortCapabilitiesAction);
DvAction getSystemUpdateIDAcion = new DvAction("GetSystemUpdateID", OnGetSystemUpdateID,
new DvArgument[]
{
},
new DvArgument[]
{
new DvArgument("Id",
SystemUpdateID,
ArgumentDirection.Out),
});
AddAction(getSystemUpdateIDAcion);
DvAction searchAction = new DvAction("Search", OnSearch,
new DvArgument[]
{
new DvArgument("ContainerID", A_ARG_TYPE_ObjectID,
ArgumentDirection.In),
new DvArgument("SearchCriteria", A_ARG_TYPE_SearchCriteria,
ArgumentDirection.In),
new DvArgument("Filter", A_ARG_TYPE_Filter,
ArgumentDirection.In),
new DvArgument("StartingIndex",
A_ARG_TYPE_Index,
ArgumentDirection.In),
new DvArgument("RequestedCount",
A_ARG_TYPE_Count,
ArgumentDirection.In),
new DvArgument("SortCriteria",
A_ARG_TYPE_SortCriteria,
ArgumentDirection.In)
},
new DvArgument[]
{
new DvArgument("Result",
A_ARG_TYPE_Result,
ArgumentDirection.Out),
new DvArgument("NumberReturned",
A_ARG_TYPE_Count,
ArgumentDirection.Out),
new DvArgument("TotalMatches",
A_ARG_TYPE_Count,
ArgumentDirection.Out),
new DvArgument("UpdateID",
A_ARG_TYPE_Count,
ArgumentDirection.Out)
});
AddAction(searchAction);
}
static UPnPError OnBrowse(DvAction action, IList<object> inParams, out IList<object> outParams, CallContext context)
{
// In parameters
var objectId = (string) inParams[0];
var browseFlag = inParams[1].ToString();
var filter = inParams[2].ToString();
var startingIndex = Convert.ToInt32(inParams[3]);
var requestedCount = Convert.ToInt32(inParams[4]);
var sortCriteria = (string) inParams[5];
// Out parameters
int numberReturned = 0;
int totalMatches = 0;
int containterUpdateId;
Logger.Debug("MediaServer - OnBrowse(objectId=\"{0}\",browseFlag=\"{1}\",filter=\"{2}\",startingIndex=\"{3}\",requestedCount=\"{4}\",sortCriteria=\"{5}\")", objectId, browseFlag, filter, startingIndex, requestedCount, sortCriteria);
// Find the container object requested
//var parentDirectoryId = objectId == "0" ? Guid.Empty : MarshallingHelper.DeserializeGuid(objectId);
var o = MediaServerPlugin.RootContainer.FindObject(objectId);
if (o == null)
{
// We failed to find the container requested
// throw error!
throw new ArgumentException("ObjectID not found");
}
var msgBuilder = new GenericDidlMessageBuilder();
// Start to build the XML DIDL-Lite document.
switch (browseFlag)
{
case "BrowseMetadata":
// Render the container as XML
msgBuilder.Build(filter, o);
// We are only after information about 1 container
numberReturned = 1;
totalMatches = 1;
break;
case "BrowseDirectChildren":
// Create a new ContainerList based on search criteria
var resultList = o.Search(filter, sortCriteria);
totalMatches = resultList.Count;
// Reduce number of items down to a specific range
if (requestedCount != 0)
{
var itemCount = requestedCount;
// Make sure that the requested itemCount value doesn't exceed total items in the list
// otherwise we will get an exception.
if (itemCount + startingIndex > resultList.Count) itemCount = resultList.Count - startingIndex;
if (itemCount > 0) resultList = resultList.GetRange(startingIndex, itemCount);
else resultList.Clear();
}
numberReturned = resultList.Count;
// Render this list of containers as XML.
msgBuilder.BuildAll(filter, resultList);
break;
default:
// Error! invalid browseFlag value.
break;
}
// Grab the container updateid
//TODO: sort out object updating
containterUpdateId = 0;// c.UpdateId;
// Construct the return arguments.
var xml = msgBuilder.ToString();
outParams = new List<object>(4) {xml, numberReturned, totalMatches, containterUpdateId};
Logger.Debug("MediaServer - OnBrowse(objectId=\"{0}\"...) = (numberReturned=\"{1}\",totalMatches=\"{2}\",containerUpdateId=\"{3}\") {4}", objectId, numberReturned, totalMatches, containterUpdateId, xml);
// This upnp action doesn't have a return type.
return null;
}
static UPnPError OnGetSearchCapabilities(DvAction action, IList<object> inParams, out IList<object> outParams, CallContext context)
{
// Current implementation doesn't support searching
outParams = new List<object> { "" };
return null;
}
static UPnPError OnGetSortCapabilities(DvAction action, IList<object> inParams, out IList<object> outParams, CallContext context)
{
// Current implementation doesn't support sorting
outParams = new List<object> { "" };
return null;
}
static UPnPError OnGetSystemUpdateID(DvAction action, IList<object> inParams, out IList<object> outParams, CallContext context)
{
outParams = new List<object> { 0 };
return null;
}
static UPnPError OnSearch(DvAction action, IList<object> inParams, out IList<object> outParams, CallContext context)
{
// In parameters
var containerId = (string)inParams[0];
var searchCriteria = inParams[1].ToString();
var filter = inParams[2].ToString();
var startingIndex = Convert.ToInt32(inParams[3]);
var requestedCount = Convert.ToInt32(inParams[4]);
var sortCriteria = (string)inParams[5];
// Out parameters
int numberReturned = 0;
int totalMatches = 0;
int containterUpdateId = 0;
UPnPContentDirectorySearch query = new UPnPContentDirectorySearch();
StringWriter sw = new StringWriter();
query.Construct(searchCriteria, sw);
query.searchCrit();
PegNode pn = query.GetRoot();
string xml = ParserHelper.PegNodeToXml(pn, searchCriteria);
Logger.Debug("MediaServer - Parsed: \"{0}\" to make \"{1}\"", searchCriteria, xml);
var parentDirectoryId = containerId == "0" ? Guid.Empty : MarshallingHelper.DeserializeGuid(containerId);
var necessaryMIATypes = new List<Guid> {DirectoryAspect.ASPECT_ID};
var searchQuery = new MediaItemQuery(necessaryMIATypes, null);
//searchQuery.Filter
var searchItems = ServiceRegistration.Get<IMediaLibrary>().Search(searchQuery, true);
/*
foreach (var item in browseItems)
{
}
*/
outParams = new List<object>(3) { numberReturned, totalMatches, containterUpdateId };
return null;
}
internal static ILogger Logger
{
get { return ServiceRegistration.Get<ILogger>(); }
}
}
}