/
PltSyncMediaBrowser.cpp
324 lines (281 loc) · 12.1 KB
/
PltSyncMediaBrowser.cpp
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
/*****************************************************************
|
| Platinum - Synchronous Media Browser
|
| Copyright (c) 2004-2008, Plutinosoft, LLC.
| All rights reserved.
| http://www.plutinosoft.com
|
| This program is free software; you can redistribute it and/or
| modify it under the terms of the GNU General Public License
| as published by the Free Software Foundation; either version 2
| of the License, or (at your option) any later version.
|
| OEMs, ISVs, VARs and other distributors that combine and
| distribute commercially licensed software with Platinum software
| and do not wish to distribute the source code for the commercially
| licensed software under version 2, or (at your option) any later
| version, of the GNU General Public License (the "GPL") must enter
| into a commercial license agreement with Plutinosoft, LLC.
|
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program; see the file LICENSE.txt. If not, write to
| the Free Software Foundation, Inc.,
| 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
| http://www.gnu.org/licenses/gpl-2.0.html
|
****************************************************************/
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#include "PltSyncMediaBrowser.h"
NPT_SET_LOCAL_LOGGER("platinum.media.server.syncbrowser")
/*----------------------------------------------------------------------
| PLT_SyncMediaBrowser::PLT_SyncMediaBrowser
+---------------------------------------------------------------------*/
PLT_SyncMediaBrowser::PLT_SyncMediaBrowser(PLT_CtrlPointReference& ctrlPoint,
bool use_cache /* = false */,
PLT_MediaContainerChangesListener* listener /* = NULL */) :
PLT_MediaBrowser(ctrlPoint),
m_ContainerListener(listener),
m_UseCache(use_cache)
{
SetDelegate(this);
}
/*----------------------------------------------------------------------
| PLT_SyncMediaBrowser::~PLT_SyncMediaBrowser
+---------------------------------------------------------------------*/
PLT_SyncMediaBrowser::~PLT_SyncMediaBrowser()
{
}
/* Blocks forever waiting for a response from a request
* It is expected the request to succeed or to timeout and return an error eventually
*/
/*----------------------------------------------------------------------
| PLT_SyncMediaBrowser::WaitForResponse
+---------------------------------------------------------------------*/
NPT_Result
PLT_SyncMediaBrowser::WaitForResponse(NPT_SharedVariable& shared_var)
{
return shared_var.WaitUntilEquals(1, 30000);
}
/*----------------------------------------------------------------------
| PLT_SyncMediaBrowser::OnDeviceAdded
+---------------------------------------------------------------------*/
NPT_Result
PLT_SyncMediaBrowser::OnDeviceAdded(PLT_DeviceDataReference& device)
{
NPT_String uuid = device->GetUUID();
// test if it's a media server
PLT_Service* service;
if (NPT_SUCCEEDED(device->FindServiceByType("urn:schemas-upnp-org:service:ContentDirectory:*", service))) {
NPT_AutoLock lock(m_MediaServers);
m_MediaServers.Put(uuid, device);
}
return PLT_MediaBrowser::OnDeviceAdded(device);
}
/*----------------------------------------------------------------------
| PLT_SyncMediaBrowser::OnDeviceRemoved
+---------------------------------------------------------------------*/
NPT_Result
PLT_SyncMediaBrowser::OnDeviceRemoved(PLT_DeviceDataReference& device)
{
NPT_String uuid = device->GetUUID();
// Remove from our list of servers first if found
{
NPT_AutoLock lock(m_MediaServers);
m_MediaServers.Erase(uuid);
}
// clear cache for that device
if (m_UseCache) m_Cache.Clear(device.AsPointer()->GetUUID());
return PLT_MediaBrowser::OnDeviceRemoved(device);
}
/*----------------------------------------------------------------------
| PLT_SyncMediaBrowser::Find
+---------------------------------------------------------------------*/
NPT_Result
PLT_SyncMediaBrowser::Find(const char* ip, PLT_DeviceDataReference& device)
{
NPT_AutoLock lock(m_MediaServers);
const NPT_List<PLT_DeviceMapEntry*>::Iterator it =
m_MediaServers.GetEntries().Find(PLT_DeviceMapFinderByIp(ip));
if (it) {
device = (*it)->GetValue();
return NPT_SUCCESS;
}
return NPT_FAILURE;
}
/*----------------------------------------------------------------------
| PLT_SyncMediaBrowser::OnBrowseResult
+---------------------------------------------------------------------*/
void
PLT_SyncMediaBrowser::OnBrowseResult(NPT_Result res,
PLT_DeviceDataReference& device,
PLT_BrowseInfo* info,
void* userdata)
{
NPT_COMPILER_UNUSED(device);
if (!userdata) return;
PLT_BrowseDataReference* data = (PLT_BrowseDataReference*) userdata;
(*data)->res = res;
if (NPT_SUCCEEDED(res) && info) {
(*data)->info = *info;
}
(*data)->shared_var.SetValue(1);
delete data;
}
/*----------------------------------------------------------------------
| PLT_SyncMediaBrowser::OnMSStateVariablesChanged
+---------------------------------------------------------------------*/
void
PLT_SyncMediaBrowser::OnMSStateVariablesChanged(PLT_Service* service,
NPT_List<PLT_StateVariable*>* vars)
{
NPT_AutoLock lock(m_MediaServers);
PLT_DeviceDataReference device;
const NPT_List<PLT_DeviceMapEntry*>::Iterator it =
m_MediaServers.GetEntries().Find(PLT_DeviceMapFinderByUUID(service->GetDevice()->GetUUID()));
if (!it) return; // device with this service has gone away
device = (*it)->GetValue();
PLT_StateVariable* var = PLT_StateVariable::Find(*vars, "ContainerUpdateIDs");
if (var) {
// variable found, parse value
NPT_String value = var->GetValue();
NPT_String item_id, update_id;
int index;
while (value.GetLength()) {
// look for container id
index = value.Find(',');
if (index < 0) break;
item_id = value.Left(index);
value = value.SubString(index+1);
// look for update id
if (value.GetLength()) {
index = value.Find(',');
update_id = (index<0)?value:value.Left(index);
value = (index<0)?"":value.SubString(index+1);
// clear cache for that device
if (m_UseCache) m_Cache.Clear(device->GetUUID(), item_id);
// notify listener
if (m_ContainerListener) m_ContainerListener->OnContainerChanged(device, item_id, update_id);
}
}
}
}
/*----------------------------------------------------------------------
| PLT_SyncMediaBrowser::BrowseSync
+---------------------------------------------------------------------*/
NPT_Result
PLT_SyncMediaBrowser::BrowseSync(PLT_BrowseDataReference& browse_data,
PLT_DeviceDataReference& device,
const char* object_id,
NPT_Int32 index,
NPT_Int32 count,
bool browse_metadata,
const char* filter,
const char* sort)
{
NPT_Result res;
browse_data->shared_var.SetValue(0);
// send off the browse packet. Note that this will
// not block. There is a call to WaitForResponse in order
// to block until the response comes back.
res = PLT_MediaBrowser::Browse(device,
(const char*)object_id,
index,
count,
browse_metadata,
filter,
sort,
new PLT_BrowseDataReference(browse_data));
NPT_CHECK_SEVERE(res);
return WaitForResponse(browse_data->shared_var);
}
/*----------------------------------------------------------------------
| PLT_SyncMediaBrowser::BrowseSync
+---------------------------------------------------------------------*/
NPT_Result
PLT_SyncMediaBrowser::BrowseSync(PLT_DeviceDataReference& device,
const char* object_id,
PLT_MediaObjectListReference& list,
bool metadata, /* = false */
NPT_Int32 start, /* = 0 */
NPT_Cardinal max_results /* = 0 */)
{
NPT_Result res = NPT_FAILURE;
NPT_Int32 index = start;
bool cache = m_UseCache && metadata && start == 0 && max_results == 0;
// reset output params
list = NULL;
// look into cache first
if (cache && NPT_SUCCEEDED(m_Cache.Get(device->GetUUID(), object_id, list))) return NPT_SUCCESS;
do {
PLT_BrowseDataReference browse_data(new PLT_BrowseData());
// send off the browse packet. Note that this will
// not block. There is a call to WaitForResponse in order
// to block until the response comes back.
res = BrowseSync(
browse_data,
device,
(const char*)object_id,
index,
metadata?1:30, // DLNA recommendations for browsing children is no more than 30 at a time
metadata);
NPT_CHECK_LABEL_WARNING(res, done);
if (NPT_FAILED(browse_data->res)) {
res = browse_data->res;
NPT_CHECK_LABEL_WARNING(res, done);
}
if (browse_data->info.items->GetItemCount() == 0)
break;
if (list.IsNull()) {
list = browse_data->info.items;
} else {
list->Add(*browse_data->info.items);
// clear the list items so that the data inside is not
// cleaned up by PLT_MediaItemList dtor since we copied
// each pointer into the new list.
browse_data->info.items->Clear();
}
// stop now if our list contains exactly what the server said it had.
// Note that the server could return 0 if it didn't know how many items were
// available. In this case we have to continue browsing until
// nothing is returned back by the server.
// Unless we were told to stop after reaching a certain amount to avoid
// length delays
if ((browse_data->info.tm && browse_data->info.tm <= list->GetItemCount()) ||
(max_results && list->GetItemCount() >= max_results))
break;
// ask for the next chunk of entries
index = list->GetItemCount();
} while(1);
done:
// cache the result
if (cache && NPT_SUCCEEDED(res) && !list.IsNull() && list->GetItemCount()) {
m_Cache.Put(device->GetUUID(), object_id, list);
}
// clear entire cache data for device if failed, the device could be gone
if (NPT_FAILED(res) && m_UseCache) m_Cache.Clear(device->GetUUID());
return res;
}
/*----------------------------------------------------------------------
| PLT_SyncMediaBrowser::IsCached
+---------------------------------------------------------------------*/
bool
PLT_SyncMediaBrowser::IsCached(const char* uuid, const char* object_id)
{
NPT_AutoLock lock(m_MediaServers);
const NPT_List<PLT_DeviceMapEntry*>::Iterator it =
m_MediaServers.GetEntries().Find(PLT_DeviceMapFinderByUUID(uuid));
if (!it) {
m_Cache.Clear(uuid);
return false; // device with this service has gone away
}
PLT_MediaObjectListReference list;
return NPT_SUCCEEDED(m_Cache.Get(uuid, object_id, list))?true:false;
}