File: PltSyncMediaBrowser.cpp

package info (click to toggle)
kodi 16.1%2Bdfsg1-2~bpo8%2B2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 73,324 kB
  • sloc: cpp: 592,417; xml: 59,388; ansic: 58,092; makefile: 4,978; sh: 4,938; python: 2,936; java: 1,065; perl: 997; objc: 982; cs: 624; asm: 294; sed: 16
file content (578 lines) | stat: -rw-r--r-- 22,154 bytes parent folder | download | duplicates (6)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*****************************************************************
|
|   Platinum - Synchronous Media Browser
|
| Copyright (c) 2004-2010, 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.
| licensing@plutinosoft.com
|  
| 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"
#include <algorithm>

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;
}

static void OnResult(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::OnBrowseResult
+---------------------------------------------------------------------*/
void
PLT_SyncMediaBrowser::OnBrowseResult(NPT_Result               res, 
                                     PLT_DeviceDataReference& device, 
                                     PLT_BrowseInfo*          info, 
                                     void*                    userdata)
{
  OnResult(res, device, info, userdata);
}

/*----------------------------------------------------------------------
|   PLT_SyncMediaBrowser::OnSearchResult
+---------------------------------------------------------------------*/
void
PLT_SyncMediaBrowser::OnSearchResult(NPT_Result               res, 
                                     PLT_DeviceDataReference& device, 
                                     PLT_BrowseInfo*          info, 
                                     void*                    userdata)
{
  OnResult(res, device, info, userdata);
}

/*----------------------------------------------------------------------
|   PLT_SyncMediaBrowser::OnGetSearchCapabilitiesResult
+---------------------------------------------------------------------*/
void
PLT_SyncMediaBrowser::OnGetSearchCapabilitiesResult(NPT_Result               res, 
                                                    PLT_DeviceDataReference& device, 
                                                    NPT_String               searchCapabilities, 
                                                    void*                    userdata)
{
  NPT_COMPILER_UNUSED(device);

  if (!userdata) return;

  PLT_CapabilitiesDataReference* data = (PLT_CapabilitiesDataReference*) userdata;
  (*data)->res = res;
  if (NPT_SUCCEEDED(res)) {
      (*data)->capabilities = searchCapabilities;
  }
  (*data)->shared_var.SetValue(1);
  delete data;
}

/*----------------------------------------------------------------------
|   PLT_SyncMediaBrowser::OnGetSortCapabilitiesResult
+---------------------------------------------------------------------*/
void
PLT_SyncMediaBrowser::OnGetSortCapabilitiesResult(NPT_Result               res,
                                                  PLT_DeviceDataReference& device,
                                                  NPT_String               sortCapabilities,
                                                  void*                    userdata)
{
  NPT_COMPILER_UNUSED(device);

  if (!userdata) return;

  PLT_CapabilitiesDataReference* data = (PLT_CapabilitiesDataReference*) userdata;
  (*data)->res = res;
  if (NPT_SUCCEEDED(res)) {
      (*data)->capabilities = sortCapabilities;
  }
  (*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);
    browse_data->info.si = index;

    // 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::SearchSync
+---------------------------------------------------------------------*/
NPT_Result 
PLT_SyncMediaBrowser::SearchSync(PLT_BrowseDataReference& browse_data,
                                 PLT_DeviceDataReference& device, 
                                 const char*              container_id,
                                 const char*              search_criteria,
                                 NPT_Int32                index, 
                                 NPT_Int32                count,
                                 const char*              filter)
{
    NPT_Result res;

    browse_data->shared_var.SetValue(0);
    browse_data->info.si = index;

    // send off the search 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::Search(device,
        container_id,
        search_criteria,
        index,
        count,
        filter,
        new PLT_BrowseDataReference(browse_data));
    NPT_CHECK_SEVERE(res);

    return WaitForResponse(browse_data->shared_var);
}

/*----------------------------------------------------------------------
|   PLT_SyncMediaBrowser::GetSearchCapabilitiesSync
+---------------------------------------------------------------------*/
NPT_Result
PLT_SyncMediaBrowser::GetSearchCapabilitiesSync(PLT_DeviceDataReference& device, 
                                                NPT_String&              searchCapabilities)
{
    NPT_Result res;

    PLT_CapabilitiesDataReference capabilities_data(new PLT_CapabilitiesData(), true);
    capabilities_data->shared_var.SetValue(0);

    // send of the GetSearchCapabilities 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::GetSearchCapabilities(device,
        new PLT_CapabilitiesDataReference(capabilities_data));
    NPT_CHECK_SEVERE(res);

    res = WaitForResponse(capabilities_data->shared_var);
    NPT_CHECK_LABEL_WARNING(res, done);

    if (NPT_FAILED(capabilities_data->res)) {
        res = capabilities_data->res;
        NPT_CHECK_LABEL_WARNING(res, done);
    }

    searchCapabilities = capabilities_data->capabilities;

done:
    return res;
}

/*----------------------------------------------------------------------
|   PLT_SyncMediaBrowser::GetSortCapabilitiesSync
+---------------------------------------------------------------------*/
NPT_Result
PLT_SyncMediaBrowser::GetSortCapabilitiesSync(PLT_DeviceDataReference& device, 
                                              NPT_String&              sortCapabilities)
{
    NPT_Result res;

    PLT_CapabilitiesDataReference capabilities_data(new PLT_CapabilitiesData(), true);
    capabilities_data->shared_var.SetValue(0);

    // send of the GetSortCapabilities 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::GetSortCapabilities(device,
        new PLT_CapabilitiesDataReference(capabilities_data));
    NPT_CHECK_SEVERE(res);

    res = WaitForResponse(capabilities_data->shared_var);
    NPT_CHECK_LABEL_WARNING(res, done);

    if (NPT_FAILED(capabilities_data->res)) {
        res = capabilities_data->res;
        NPT_CHECK_LABEL_WARNING(res, done);
    }

    sortCapabilities = capabilities_data->capabilities;

done:
    return res;
}

/*----------------------------------------------------------------------
|   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;
    NPT_UInt32 count = 0;
    
    // only cache metadata or if starting from 0 and asking for maximum
    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:200, // 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);
        }

        // server returned no more, bail now
        if (browse_data->info.nr == 0)
            break;

        if (browse_data->info.nr != browse_data->info.items->GetItemCount()) {
            NPT_LOG_WARNING_2("Server returned unexpected number of items (%d vs %d)",
                              browse_data->info.nr, browse_data->info.items->GetItemCount());
        }
        count += std::max<NPT_UInt32>(browse_data->info.nr, browse_data->info.items->GetItemCount());

        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
        // (some servers may return a total matches out of whack at some point too)
        if ((browse_data->info.tm && browse_data->info.tm <= count) ||
            (max_results && count >= max_results))
            break;

        // ask for the next chunk of entries
        index = count;
    } 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) && cache) m_Cache.Clear(device->GetUUID());
    
    return res;
}

/*----------------------------------------------------------------------
|   PLT_SyncMediaBrowser::SearchSync
+---------------------------------------------------------------------*/
NPT_Result
PLT_SyncMediaBrowser::SearchSync(PLT_DeviceDataReference&      device,
                                 const char*                   container_id,
                                 const char*                   search_criteria,
                                 PLT_MediaObjectListReference& list,
                                 NPT_Int32                     start, /* = 0 */
                                 NPT_Cardinal                  max_results /* = 0 */)
{
    NPT_Result res = NPT_FAILURE;
    NPT_Int32  index = start;
    NPT_UInt32 count = 0;

    // reset output params
    list = NULL;

    do {	
        PLT_BrowseDataReference browse_data(new PLT_BrowseData(), true);

        // send off the search packet.  Note that this will
        // not block.  There is a call to WaitForResponse in order
        // to block until the response comes back.
        res = SearchSync(
            browse_data,
            device,
            container_id,
            search_criteria,
            index,
            200); // DLNA recommendations for browsing children is no more than 30 at a time

        NPT_CHECK_LABEL_WARNING(res, done);
        
        if (NPT_FAILED(browse_data->res)) {
            res = browse_data->res;
            NPT_CHECK_LABEL_WARNING(res, done);
        }

        // server returned no more, bail now
        if (browse_data->info.nr == 0)
            break;

        if (browse_data->info.nr != browse_data->info.items->GetItemCount()) {
            NPT_LOG_WARNING_2("Server returned unexpected number of items (%d vs %d)",
                              browse_data->info.nr, browse_data->info.items->GetItemCount());
        }
        count += std::max<NPT_UInt32>(browse_data->info.nr, browse_data->info.items->GetItemCount());

        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
        // (some servers may return a total matches out of whack at some point too)
        if ((browse_data->info.tm && browse_data->info.tm <= count) ||
            (max_results && count >= max_results))
            break;

        // ask for the next chunk of entries
        index = count;
    } while(1);

done:
    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;
}