forked from RSATom/ya-libvlc-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlc_player.cpp
423 lines (328 loc) · 10.5 KB
/
vlc_player.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
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
/*******************************************************************************
* Copyright (c) 2013-2014, Sergey Radionov <rsatom_gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
#include "vlc_player.h"
#include <cassert>
using namespace vlc;
player::player()
: _libvlc_instance( 0 ),
_video( _player ), _audio( _player ), _subtitles( _player ),
_mode( mode_normal ), _current_idx( -1 )
{
}
player::~player()
{
close();
}
void player::libvlc_event_proxy( const struct libvlc_event_t* event, void* user_data )
{
static_cast<player*>( user_data )->libvlc_event( event );
}
void player::libvlc_event( const struct libvlc_event_t* event )
{
//come from another thread
if( libvlc_MediaPlayerEndReached == event->type ||
libvlc_MediaPlayerEncounteredError == event->type )
{
if( mode_single != get_playback_mode() ) {
//to avoid deadlock we should execute commands on another thread
thread th( &player::next, this );
th.detach();
}
}
}
bool player::open( libvlc_instance_t* inst )
{
_libvlc_instance = inst;
if( _player.open( inst ) ) {
libvlc_event_manager_t* em = libvlc_media_player_event_manager( _player.get_mp() );
libvlc_event_attach( em, libvlc_MediaPlayerEndReached, libvlc_event_proxy, this );
libvlc_event_attach( em, libvlc_MediaPlayerEncounteredError, libvlc_event_proxy, this );
return true;
}
return false;
}
void player::close()
{
_player.close();
clear_items();
_libvlc_instance = 0;
}
int player::add_media( const char * mrl_or_path,
unsigned optc, const char **optv,
unsigned trusted_optc, const char **trusted_optv,
bool is_path /*= false*/ )
{
if( !is_open() )
return -1;
libvlc_media_t* media = is_path ?
libvlc_media_new_path( _libvlc_instance, mrl_or_path ) :
libvlc_media_new_location( _libvlc_instance, mrl_or_path );
if( !media )
return -1;
unsigned i;
for( i = 0; i < optc; ++i )
libvlc_media_add_option_flag( media, optv[i], libvlc_media_option_unique );
for( i = 0; i < trusted_optc; ++i )
libvlc_media_add_option_flag( media, trusted_optv[i],
libvlc_media_option_unique | libvlc_media_option_trusted );
lock_guard<mutex_t> lock( _playlist_guard );
playlist_item item = { vlc::media( media, false ) };
playlist_it it = _playlist.insert( _playlist.end(), item );
return it - _playlist.begin();
}
bool player::delete_item( unsigned idx )
{
lock_guard<mutex_t> lock( _playlist_guard );
unsigned sz = _playlist.size();
assert( _current_idx >= 0 && unsigned( _current_idx ) < sz );
if( sz && idx < sz ) {
if( _current_idx >= 0 &&
( unsigned( _current_idx ) > idx ||
( unsigned( _current_idx ) == idx &&
unsigned( _current_idx ) == ( sz - 1 ) ) ) )
{
//if current is after idx or is idx and is last
--_current_idx;
}
playlist_it it = ( _playlist.begin() + idx );
_playlist.erase( it );
assert( _current_idx < 0 || unsigned( _current_idx ) < _playlist.size() );
return true;
}
return false;
}
void player::clear_items()
{
lock_guard<mutex_t> lock( _playlist_guard );
_playlist.clear();
_current_idx = -1;
//we shouldn't stop/clear current playing item in _player
//since some users want to refill playlist
//while current media is still playing
}
int player::item_count()
{
lock_guard<mutex_t> lock( _playlist_guard );
return _playlist.size();
}
vlc::media player::get_media( unsigned idx )
{
lock_guard<mutex_t> lock( _playlist_guard );
if( idx >= _playlist.size() )
return vlc::media();
return _playlist[idx].media;
}
int player::current_item()
{
lock_guard<mutex_t> lock( _playlist_guard );
return _current_idx;
}
void player::set_current( unsigned idx )
{
lock_guard<mutex_t> lock( _playlist_guard );
if( idx < _playlist.size() ) {
_current_idx = idx;
_player.set_media( _playlist[_current_idx].media );
}
}
void player::internalPlay( int idx )
{
if( idx < 0 || static_cast<unsigned>( idx ) >= _playlist.size() ) {
return;
}
if( _player.current_media() != _playlist[idx].media )
set_current( idx );
_player.play();
}
void player::play()
{
lock_guard<mutex_t> lock( _playlist_guard );
if( _playlist.empty() ) {
//special case for empty playlist ( usually after clear_items() )
_player.play();
} else {
const unsigned sz = _playlist.size();
int idx = _current_idx;
if( idx < 0 )
idx = 0;
else if( unsigned( idx ) >= sz )
idx = ( sz - 1 );
internalPlay( idx );
}
}
bool player::play( unsigned idx )
{
lock_guard<mutex_t> lock( _playlist_guard );
if( idx < _playlist.size() ) {
internalPlay( idx );
return true;
}
return false;
}
void player::prev()
{
lock_guard<mutex_t> lock( _playlist_guard );
if( _playlist.empty() )
return;
const unsigned sz = _playlist.size();
if( _current_idx <= 0 ) {
if( mode_loop == _mode )
internalPlay( sz - 1 );
} else if( unsigned( _current_idx ) >= sz ) {
internalPlay( sz - 1 );
} else
internalPlay( _current_idx - 1 );
}
void player::get_media_sub_items( const vlc::media& media, playlist_t* out )
{
assert( out );
if( !media || !out )
return;
libvlc_media_list_t* sub_items = libvlc_media_subitems( media.libvlc_media_t() );
if( !sub_items )
return;
libvlc_media_list_lock( sub_items );
int sub_items_count = libvlc_media_list_count( sub_items );
for( int i = 0; i < sub_items_count; ++i ) {
libvlc_media_t* sub_item = libvlc_media_list_item_at_index( sub_items, i );
if( sub_item ) {
playlist_item item = { vlc::media( sub_item, false ) };
out->push_back( item );
}
}
libvlc_media_list_unlock( sub_items );
libvlc_media_list_release( sub_items );
}
bool player::try_expand_current()
{
vlc::media current_media =
( _current_idx < 0 || unsigned( _current_idx ) >= _playlist.size() ) ?
_player.current_media() : _playlist[_current_idx].media;
playlist_t sub_items;
get_media_sub_items( current_media, &sub_items );
if( !sub_items.empty() ) {
playlist_it insert_it;
if( _current_idx < 0 ) {
insert_it = _playlist.begin();
} else if( unsigned( _current_idx ) >= _playlist.size() ) {
insert_it = _playlist.end();
} else {
insert_it = _playlist.erase( _playlist.begin() + _current_idx );
}
_current_idx = insert_it - _playlist.begin();
_playlist.insert( insert_it, sub_items.begin(), sub_items.end() );
return true;
}
return false;
}
void player::next()
{
lock_guard<mutex_t> lock( _playlist_guard );
bool expanded = try_expand_current();
if( _playlist.empty() )
return;
const unsigned sz = _playlist.size();
if( expanded )
internalPlay( _current_idx );
else if( ( _current_idx < 0 && sz > 0 ) ||
( unsigned( _current_idx ) == ( sz - 1 ) && ( mode_loop == _mode ) ) )
{
//if current not set or current is last and loop mode enabled
internalPlay( 0 );
} else
internalPlay( _current_idx + 1 );
}
void player::pause()
{
_player.pause();
}
void player::togglePause()
{
_player.togglePause();
}
void player::stop( bool async /*= false*/ )
{
_player.stop( async );
}
float player::get_rate()
{
if( !_player.is_open() )
return 1.f;
return libvlc_media_player_get_rate( _player.get_mp() );
}
void player::set_rate( float rate )
{
if( !_player.is_open() )
return;
libvlc_media_player_set_rate( _player.get_mp(), rate );
}
float player::get_position()
{
if( !is_open() )
return 0.f;
float p = libvlc_media_player_get_position( _player.get_mp() );
return p < 0 ? 0 : p;
}
void player::set_position( float p )
{
if( !is_open() )
return;
libvlc_media_player_set_position( _player.get_mp(), p );
}
libvlc_time_t player::get_time()
{
if( !is_open() )
return 0;
libvlc_time_t t = libvlc_media_player_get_time( _player.get_mp() );
return t < 0 ? 0 : t ;
}
void player::set_time( libvlc_time_t t )
{
if( !is_open() )
return;
libvlc_media_player_set_time( _player.get_mp(), t );
}
libvlc_time_t player::get_length()
{
if( !_player.is_open() )
return 0;
libvlc_time_t t = libvlc_media_player_get_length( _player.get_mp() );
return t < 0 ? 0 : t ;
}
float player::get_fps()
{
if( !_player.is_open() )
return 0;
return libvlc_media_player_get_fps( _player.get_mp() );
}
playback_mode_e player::get_playback_mode()
{
lock_guard<mutex_t> lock( _playlist_guard );
return _mode;
}
void player::set_playback_mode( playback_mode_e m )
{
lock_guard<mutex_t> lock( _playlist_guard );
_mode = m;
}