-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathtizplaylist.cpp
More file actions
570 lines (504 loc) · 14.9 KB
/
tizplaylist.cpp
File metadata and controls
570 lines (504 loc) · 14.9 KB
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
/**
* Copyright (C) 2011-2020 Aratelia Limited - Juan A. Rubio and contributors
*
* This file is part of Tizonia
*
* Tizonia is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Tizonia 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 Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Tizonia. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file tizplaylist.cpp
* @author Juan A. Rubio <juan.rubio@aratelia.com>
*
* @brief Playlist utility
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <algorithm>
#include <cmath>
#include <string>
#include <random>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/system/error_code.hpp>
#include <tizplatform.h>
#include "tizplaylist.hpp"
#ifdef TIZ_LOG_CATEGORY_NAME
#undef TIZ_LOG_CATEGORY_NAME
#define TIZ_LOG_CATEGORY_NAME "tiz.play.playlist"
#endif
namespace // unnamed namespace
{
struct pathname_of
{
pathname_of (uri_lst_t &uri_list) : uri_list_ (uri_list)
{
}
void operator() (const boost::filesystem::directory_entry &p) const
{
uri_list_.push_back (p.path ().string ());
}
uri_lst_t &uri_list_;
};
void add_to_extension_list (file_extension_lst_t &list,
const std::string &extension)
{
list.insert (list.end (), extension);
}
OMX_ERRORTYPE
process_base_uri (const std::string &uri, uri_lst_t &uri_list,
bool recurse = false)
{
if (boost::filesystem::exists (uri)
&& boost::filesystem::is_regular_file (uri))
{
uri_list.push_back (uri);
return OMX_ErrorNone;
}
if (boost::filesystem::exists (uri)
&& boost::filesystem::is_directory (uri))
{
if (!recurse)
{
std::for_each (boost::filesystem::directory_iterator (
boost::filesystem::path (uri)),
boost::filesystem::directory_iterator (),
pathname_of (uri_list));
return uri_list.empty () ? OMX_ErrorContentURIError : OMX_ErrorNone;
}
else
{
std::for_each (boost::filesystem::recursive_directory_iterator (
boost::filesystem::path (uri)),
boost::filesystem::recursive_directory_iterator (),
pathname_of (uri_list));
return uri_list.empty () ? OMX_ErrorContentURIError : OMX_ErrorNone;
}
}
return OMX_ErrorContentURIError;
}
OMX_ERRORTYPE
filter_unknown_media (const file_extension_lst_t &extension_list,
uri_lst_t &uri_list,
file_extension_lst_t &extension_list_filtered)
{
uri_lst_t::iterator it (uri_list.begin ());
std::vector< uri_lst_t::iterator > iter_list;
file_extension_lst_t ext_lst_cpy (extension_list);
uri_lst_t filtered_uri_list;
while (it != uri_list.end ())
{
std::string extension (
boost::filesystem::path (*it).extension ().string ());
boost::algorithm::to_lower (extension);
file_extension_lst_t::const_iterator low = std::lower_bound (
ext_lst_cpy.begin (), ext_lst_cpy.end (), extension);
TIZ_LOG (TIZ_PRIORITY_TRACE, "ext [%s] it [%s]", extension.c_str (),
(*it).c_str ());
if (!(low == ext_lst_cpy.end ()) && boost::iequals ((*low), extension))
{
TIZ_LOG (TIZ_PRIORITY_TRACE, "[%s] added to playlist low [%s]",
(*it).c_str (), (*low).c_str ());
filtered_uri_list.push_back (*it);
extension_list_filtered.insert (extension);
}
++it;
}
uri_list = filtered_uri_list;
TIZ_LOG (TIZ_PRIORITY_TRACE, "%d elements in playlist", uri_list.size ());
return uri_list.empty () ? OMX_ErrorContentURIError : OMX_ErrorNone;
}
} // unnamed namespace
//
// playlist
//
tiz::playlist::playlist (const uri_lst_t &uri_list /* = uri_lst_t () */,
const bool shuffle /* = false */)
: uri_list_ (uri_list),
current_position_ (0),
loop_playback_ (false),
sub_list_positions_ (),
current_sub_list_ (-1),
shuffle_ (shuffle),
extension_list_ (),
single_format_ (Unknown)
{
const int list_size = uri_list_.size ();
if (list_size)
{
TIZ_LOG (TIZ_PRIORITY_TRACE, "first uri [%s]", uri_list_[0].c_str ());
TIZ_LOG (TIZ_PRIORITY_TRACE, "last list [%s]",
uri_list_[uri_list_.size () - 1].c_str ());
}
scan_list ();
}
tiz::playlist::playlist (const playlist ©_from)
: uri_list_ (copy_from.uri_list_),
current_position_ (copy_from.current_position_),
loop_playback_ (copy_from.loop_playback_),
sub_list_positions_ (copy_from.sub_list_positions_),
current_sub_list_ (copy_from.current_sub_list_),
shuffle_ (copy_from.shuffle_),
extension_list_ (copy_from.extension_list_),
single_format_ (copy_from.single_format_)
{
const int list_size = uri_list_.size ();
TIZ_LOG (TIZ_PRIORITY_TRACE, "uri list size [%d]", list_size);
if (list_size)
{
TIZ_LOG (TIZ_PRIORITY_TRACE, "first uri [%s]", uri_list_[0].c_str ());
TIZ_LOG (TIZ_PRIORITY_TRACE, "last list [%s]",
uri_list_[uri_list_.size () - 1].c_str ());
}
}
bool tiz::playlist::assemble_play_list (
const std::string &base_uri, const bool shuffle_playlist,
const bool recurse, const file_extension_lst_t &extension_list,
uri_lst_t &uri_list, std::string &error_msg)
{
bool list_assembled = false;
file_extension_lst_t extension_list_filtered;
try
{
if (base_uri.empty ())
{
error_msg.assign ("Empty media uri.");
goto end;
}
boost::system::error_code errcode;
std::string canonical_base_uri
= boost::filesystem::canonical (base_uri, errcode).string ();
if (errcode.value () != 0)
{
error_msg.assign (errcode.message ());
goto end;
}
if (OMX_ErrorNone
!= process_base_uri (canonical_base_uri, uri_list, recurse))
{
error_msg.assign ("File not found.");
goto end;
}
if (OMX_ErrorNone
!= filter_unknown_media (extension_list, uri_list,
extension_list_filtered))
{
error_msg.assign ("No supported media types found.");
goto end;
}
if (shuffle_playlist)
{
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(uri_list.begin(), uri_list.end(), g);
}
else
{
std::sort (uri_list.begin (), uri_list.end ());
}
list_assembled = true;
}
catch (std::exception const &e)
{
error_msg.assign (e.what ());
}
catch (...)
{
error_msg.assign ("Undefined file system error.");
}
end:
if (!list_assembled)
{
TIZ_LOG (TIZ_PRIORITY_ERROR, "[%s]", error_msg.c_str ());
}
return list_assembled;
}
void tiz::playlist::skip (const int jump)
{
const int list_size = uri_list_.size ();
TIZ_LOG (TIZ_PRIORITY_TRACE,
"jump [%d] current_position_ [%d]"
" loop_playback [%s]",
jump, current_position_, loop_playback_ ? "YES" : "NO");
current_position_ += jump;
if (loop_playback ())
{
if (current_position_ < 0)
{
current_position_ = list_size - abs (current_position_);
}
else if (current_position_ >= list_size)
{
current_position_ %= list_size;
}
}
TIZ_LOG (TIZ_PRIORITY_TRACE, "jump [%d] new position [%d]... [%s]", jump,
current_position_,
current_position_ < list_size && current_position_ >= 0
? uri_list_[current_position_].c_str ()
: "");
}
const std::string &tiz::playlist::get_current_uri () const
{
const int list_size = uri_list_.size ();
TIZ_LOG (TIZ_PRIORITY_TRACE, "uri list size [%d] current_position_ [%d]...",
list_size, current_position_);
assert (current_position_ >= 0 && current_position_ < list_size);
return uri_list_[current_position_];
}
tiz::playlist tiz::playlist::obtain_next_sub_playlist (
const list_direction_t up_or_down)
{
if (uri_list_.empty () || single_format ())
{
return playlist (get_uri_list ());
}
else
{
assert (up_or_down < DirMax);
if (up_or_down == DirUp)
{
const int sub_lists = sub_list_positions_.size () - 1;
current_sub_list_++;
if (current_sub_list_ >= sub_lists)
{
current_sub_list_ = 0;
}
}
else
{
if (current_sub_list_ <= 0)
{
current_sub_list_ = sub_list_positions_.size () - 1;
}
--current_sub_list_;
}
size_t position1 = sub_list_positions_[current_sub_list_];
size_t position2 = sub_list_positions_[current_sub_list_ + 1];
uri_lst_t::const_iterator first = uri_list_.begin () + position1;
uri_lst_t::const_iterator last = uri_list_.begin () + position2;
TIZ_LOG (TIZ_PRIORITY_TRACE,
"current_sub_list_ [%d] position1 [%d] position2_ [%d]...",
current_sub_list_, position1, position2);
playlist new_list (uri_lst_t (first, last));
assert (new_list.single_format ());
current_position_ = position1;
return new_list;
}
}
uri_lst_t tiz::playlist::get_sublist (const int from, const int to) const
{
const int list_size = uri_list_.size ();
uri_lst_t new_list;
if (from >= 0 && to < list_size && from < to)
{
// .assign: Replaces the vector contents with copies of those in the
// range [first, last)
new_list.assign (uri_list_.begin () + from, uri_list_.begin () + to);
}
return new_list;
}
const uri_lst_t &tiz::playlist::get_uri_list () const
{
return uri_list_;
}
int tiz::playlist::current_position () const
{
return current_position_;
}
int tiz::playlist::size () const
{
return uri_list_.size ();
}
bool tiz::playlist::empty () const
{
return uri_list_.empty ();
}
bool tiz::playlist::single_format () const
{
if (!uri_list_.empty ())
{
if (Unknown == single_format_)
{
single_format_ = Yes;
try
{
const int list_size = uri_list_.size ();
std::string current_extension (
boost::filesystem::path (uri_list_[0]).extension ().string ());
boost::algorithm::to_lower (current_extension);
for (int i = 0; i < list_size; ++i)
{
std::string extension (
boost::filesystem::path (uri_list_[i]).extension ().string ());
boost::algorithm::to_lower (extension);
if (extension.compare (current_extension) != 0)
{
single_format_ = No;
break;
}
}
assert (Yes == single_format_ || No == single_format_);
TIZ_LOG (TIZ_PRIORITY_TRACE, "Is single format? [%s]",
single_format_ == Yes ? "YES" : "NO");
}
catch (std::exception const &e)
{
TIZ_LOG (TIZ_PRIORITY_ERROR, "[%s]", e.what ());
}
catch (...)
{
TIZ_LOG (TIZ_PRIORITY_ERROR, "[Unknown exception]");
}
}
}
return (single_format_ == Yes);
}
bool tiz::playlist::before_begin () const
{
bool before_begin = (current_position_ < 0);
TIZ_LOG (TIZ_PRIORITY_TRACE, "current_position_ [%d] before begin? [%s]",
current_position_, before_begin ? "YES" : "NO");
return before_begin;
}
bool tiz::playlist::past_end () const
{
const int list_size = uri_list_.size ();
bool past_end = false;
if (current_position_ >= list_size)
{
past_end = true;
}
TIZ_LOG (TIZ_PRIORITY_TRACE,
"current_position_ [%d] uri_list_.size () [%d] past end? [%s]",
current_position_, list_size, past_end ? "YES" : "NO");
return past_end;
}
bool tiz::playlist::loop_playback () const
{
return loop_playback_;
}
void tiz::playlist::set_loop_playback (const bool loop_playback)
{
loop_playback_ = loop_playback;
}
bool tiz::playlist::shuffle () const
{
return shuffle_;
}
int tiz::playlist::position () const
{
return current_position_;
}
void tiz::playlist::set_position (const int position)
{
if (!uri_list_.empty ())
{
const int list_size = size ();
int capped_position = position;
if (capped_position >= list_size)
{
capped_position %= list_size;
}
else if (capped_position < 0)
{
capped_position = list_size - abs (capped_position);
}
assert (capped_position >= 0 && capped_position < list_size);
TIZ_LOG (TIZ_PRIORITY_TRACE,
"current_position_ [%d] position [%d] new position [%d]",
current_position_, position, capped_position);
current_position_ = capped_position;
}
}
void tiz::playlist::erase_uri (const int position)
{
const int list_size = size ();
assert (position < list_size);
if (position < list_size)
{
uri_list_.erase (uri_list_.begin () + position);
}
}
void tiz::playlist::scan_list ()
{
if (!uri_list_.empty ())
{
int position = 0;
while (position < size ())
{
TIZ_LOG (TIZ_PRIORITY_TRACE, "new sub list at position [%d]", position);
sub_list_positions_.push_back (position);
position = find_next_sub_list (position);
}
// For convenience, push one more position, a "last" position...
sub_list_positions_.push_back (uri_list_.size ());
// Find out whether this is a single-format playlist.
(void)single_format ();
}
}
int tiz::playlist::find_next_sub_list (const int position) const
{
const int list_size = uri_list_.size ();
int cur_pos = position;
assert (cur_pos < list_size);
try
{
std::string current_extension (
boost::filesystem::path (uri_list_[cur_pos]).extension ().string ());
boost::algorithm::to_lower (current_extension);
add_to_extension_list (extension_list_, current_extension);
for (; cur_pos < list_size; ++cur_pos)
{
std::string extension (
boost::filesystem::path (uri_list_[cur_pos]).extension ().string ());
boost::algorithm::to_lower (extension);
if (!extension.compare (current_extension) == 0)
{
break;
}
}
}
catch (std::exception const &e)
{
cur_pos = list_size;
TIZ_LOG (TIZ_PRIORITY_ERROR, "[%s]", e.what ());
}
catch (...)
{
cur_pos = list_size;
TIZ_LOG (TIZ_PRIORITY_ERROR, "[Unknown exception]");
}
return cur_pos;
}
void tiz::playlist::print_info ()
{
TIZ_PRINTF_C11 (
"[Playlist] [Tracks in queue] '%lu'. (File extensions in queue: %s)",
(long)uri_list_.size (),
boost::algorithm::join (extension_list_, ", ").c_str ());
}
void tiz::playlist::print_contents () const
{
unsigned int i = 1;
BOOST_FOREACH (std::string uri, uri_list_)
{
boost::filesystem::path p (uri);
TIZ_PRINTF_C11 ("[Playlist] [Track] [#%d] '%s'", i++,
p.filename ().c_str ());
}
}