This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathQmlVlcPlayerProxy.cpp
359 lines (315 loc) · 10.6 KB
/
QmlVlcPlayerProxy.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
/*******************************************************************************
* Copyright © 2014-2015, 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 "QmlVlcPlayerProxy.h"
#include <QDebug>
#include <QCoreApplication>
QmlVlcPlayerProxy::LibvlcEvent::LibvlcEvent( const libvlc_event_t& libvlcEvent )
: QEvent( static_cast<QEvent::Type>( LibvlcEventId ) ),
_libvlcEvent( libvlcEvent )
{
}
QmlVlcPlayerProxy::QmlVlcPlayerProxy( const std::shared_ptr<vlc::playlist_player_core>& player,
QObject* parent /*= 0*/ )
: QmlVlcVideoSource( player, parent ), m_player( player ), m_audio( *player ), m_input( *player ),
m_playlist( this ), m_subtitle( *player ), m_video( *player ),
m_currentMediaDesc( this )
{
qRegisterMetaType<QmlVlcPlayerProxy::State>( "QmlVlcPlayerProxy::State" );
m_errorTimer.setInterval( 1000 );
m_errorTimer.setSingleShot( true );
connect( this, SIGNAL( mediaPlayerEncounteredError() ),
&m_errorTimer, SLOT( start() ) );
connect( &m_errorTimer, SIGNAL( timeout() ),
this, SLOT( currentItemEndReached() ) );
connect( this, SIGNAL( mediaPlayerEndReached() ),
&m_errorTimer, SLOT( stop() ) );
connect( this, SIGNAL( mediaPlayerEndReached() ),
this, SLOT( currentItemEndReached() ) );
connect( this, SIGNAL( mediaPlayerMediaChanged() ),
&m_errorTimer, SLOT( stop() ) );
connect( this, &QmlVlcPlayerProxy::mediaPlayerMediaChanged,
get_subtitle(), &QmlVlcSubtitle::eraseLoaded );
connect( get_audio(), SIGNAL( volumeChanged() ),
this, SIGNAL( volumeChanged() ) );
}
void QmlVlcPlayerProxy::classBegin()
{
player().register_callback( this );
QmlVlcVideoSource::classBegin();
}
void QmlVlcPlayerProxy::componentComplete()
{
}
void QmlVlcPlayerProxy::classEnd()
{
if( m_player ) {
player().unregister_callback( this );
}
}
QmlVlcPlayerProxy::~QmlVlcPlayerProxy()
{
classEnd();
}
static void LogEvent( const libvlc_event_t* e )
{
switch ( e->type ) {
case libvlc_MediaPlayerMediaChanged:
qDebug() << "mediaPlayerMediaChanged";
break;
case libvlc_MediaPlayerNothingSpecial:
qDebug() << "mediaPlayerNothingSpecial";
break;
case libvlc_MediaPlayerOpening:
qDebug() << "mediaPlayerOpening";
break;
case libvlc_MediaPlayerBuffering:
qDebug() << "mediaPlayerBuffering";
break;
case libvlc_MediaPlayerPlaying:
qDebug() << "mediaPlayerPlaying";
break;
case libvlc_MediaPlayerPaused:
qDebug() << "mediaPlayerPaused";
break;
case libvlc_MediaPlayerStopped:
qDebug() << "mediaPlayerStopped";
break;
case libvlc_MediaPlayerForward:
qDebug() << "mediaPlayerForward";
break;
case libvlc_MediaPlayerBackward:
qDebug() << "mediaPlayerBackward";
break;
case libvlc_MediaPlayerEndReached:
qDebug() << "mediaPlayerEndReached";
break;
case libvlc_MediaPlayerEncounteredError:
qDebug() << "mediaPlayerEncounteredError";
break;
case libvlc_MediaPlayerTimeChanged:
qDebug() << "mediaPlayerTimeChanged";
break;
case libvlc_MediaPlayerPositionChanged:
qDebug() << "mediaPlayerPositionChanged";
break;
case libvlc_MediaPlayerSeekableChanged:
qDebug() << "mediaPlayerSeekableChanged";
break;
case libvlc_MediaPlayerPausableChanged:
qDebug() << "mediaPlayerPausableChanged";
break;
case libvlc_MediaPlayerTitleChanged:
qDebug() << "mediaPlayerTitleChanged";
break;
case libvlc_MediaPlayerLengthChanged:
qDebug() << "mediaPlayerLengthChanged";
break;
};
}
//libvlc events could arrive from separate thread
void QmlVlcPlayerProxy::media_player_event( const libvlc_event_t* e )
{
//to avoid deadlocks have to always queue libvlc events,
//since libvlc is not reentrant
QCoreApplication::postEvent( this, new LibvlcEvent( *e ) );
}
bool QmlVlcPlayerProxy::event( QEvent* event )
{
if( static_cast<unsigned>( event->type() ) == LibvlcEvent::LibvlcEventId ) {
LibvlcEvent* libvlcEvent = static_cast<LibvlcEvent*>( event );
handleLibvlcEvent( *libvlcEvent );
return true;
}
return QObject::event( event );
}
void QmlVlcPlayerProxy::handleLibvlcEvent( const LibvlcEvent& event )
{
//it's highly possible libvlc_event_t will have wrong pointers inside at this point
const libvlc_event_t* e = &event._libvlcEvent;
//LogEvent( e );
switch ( e->type ) {
case libvlc_MediaPlayerMediaChanged:
Q_EMIT mediaPlayerMediaChanged();
Q_EMIT m_playlist.currentItemChanged();
break;
case libvlc_MediaPlayerNothingSpecial:
Q_EMIT mediaPlayerNothingSpecial();
Q_EMIT stateChanged( NothingSpecial );
break;
case libvlc_MediaPlayerOpening:
Q_EMIT mediaPlayerOpening();
Q_EMIT stateChanged( Opening );
break;
case libvlc_MediaPlayerBuffering:
Q_EMIT mediaPlayerBuffering( e->u.media_player_buffering.new_cache );
Q_EMIT stateChanged( Buffering );
break;
case libvlc_MediaPlayerPlaying:
Q_EMIT mediaPlayerPlaying();
Q_EMIT stateChanged( Playing );
Q_EMIT playingChanged();
break;
case libvlc_MediaPlayerPaused:
Q_EMIT mediaPlayerPaused();
Q_EMIT stateChanged( Paused );
Q_EMIT playingChanged();
break;
case libvlc_MediaPlayerStopped:
Q_EMIT mediaPlayerStopped();
Q_EMIT stateChanged( Stopped );
Q_EMIT playingChanged();
break;
case libvlc_MediaPlayerForward:
Q_EMIT mediaPlayerForward();
break;
case libvlc_MediaPlayerBackward:
Q_EMIT mediaPlayerBackward();
break;
case libvlc_MediaPlayerEndReached:
Q_EMIT mediaPlayerEndReached();
Q_EMIT stateChanged( Ended );
Q_EMIT playingChanged();
break;
case libvlc_MediaPlayerEncounteredError:
Q_EMIT mediaPlayerEncounteredError();
Q_EMIT stateChanged( Error );
Q_EMIT playingChanged();
break;
case libvlc_MediaPlayerTimeChanged: {
double new_time = static_cast<double>( e->u.media_player_time_changed.new_time );
Q_EMIT mediaPlayerTimeChanged( new_time );
break;
}
case libvlc_MediaPlayerPositionChanged:
Q_EMIT mediaPlayerPositionChanged( e->u.media_player_position_changed.new_position );
break;
case libvlc_MediaPlayerSeekableChanged:
Q_EMIT mediaPlayerSeekableChanged( e->u.media_player_seekable_changed.new_seekable != 0 );
break;
case libvlc_MediaPlayerPausableChanged:
Q_EMIT mediaPlayerPausableChanged( e->u.media_player_pausable_changed.new_pausable != 0 );
break;
case libvlc_MediaPlayerTitleChanged:
Q_EMIT mediaPlayerTitleChanged();
Q_EMIT m_currentMediaDesc.titleChanged();
break;
case libvlc_MediaPlayerLengthChanged: {
double new_length =
static_cast<double>( e->u.media_player_length_changed.new_length );
Q_EMIT mediaPlayerLengthChanged( new_length );
}
break;
};
}
void QmlVlcPlayerProxy::currentItemEndReached()
{
if( vlc::mode_single != player().get_playback_mode() )
player().next();
}
QString QmlVlcPlayerProxy::get_vlcVersion()
{
return QString::fromLatin1( libvlc_get_version() );
}
void QmlVlcPlayerProxy::play( const QString& mrl )
{
vlc::playlist_player_core& p = player();
p.clear_items();
int item = p.add_media( mrl.toUtf8().data(), 0, 0, 0, 0 );
if( item >= 0) {
p.play( item );
}
}
void QmlVlcPlayerProxy::play()
{
player().play();
}
void QmlVlcPlayerProxy::pause()
{
player().pause();
}
void QmlVlcPlayerProxy::togglePause()
{
player().togglePause();
}
void QmlVlcPlayerProxy::stop()
{
player().stop();
}
void QmlVlcPlayerProxy::mute()
{
player().audio().set_mute( true );
}
void QmlVlcPlayerProxy::unMute()
{
player().audio().set_mute( false );
}
void QmlVlcPlayerProxy::toggleMute()
{
player().audio().toggle_mute();
}
QString QmlVlcPlayerProxy::get_mrl()
{
std::string mrl = player().current_media().mrl();
return QString::fromUtf8( mrl.data(), mrl.size() );
}
void QmlVlcPlayerProxy::set_mrl( const QString& mrl )
{
play( mrl );
}
bool QmlVlcPlayerProxy::get_playing()
{
return player().is_playing();
}
double QmlVlcPlayerProxy::get_length()
{
return static_cast<double>( player().playback().get_length() );
}
double QmlVlcPlayerProxy::get_position()
{
return player().playback().get_position();
}
void QmlVlcPlayerProxy::set_position( double pos )
{
player().playback().set_position( static_cast<float>( pos ) );
}
double QmlVlcPlayerProxy::get_time()
{
return static_cast<double>( player().playback().get_time() );
}
void QmlVlcPlayerProxy::set_time( double t )
{
player().playback().set_time( static_cast<libvlc_time_t>( t ) );
}
unsigned int QmlVlcPlayerProxy::get_volume()
{
return player().audio().get_volume();
}
void QmlVlcPlayerProxy::set_volume( unsigned int v )
{
player().audio().set_volume( v );
}
QmlVlcPlayerProxy::State QmlVlcPlayerProxy::get_state()
{
return static_cast<State>( player().get_state() );
}