summaryrefslogtreecommitdiffstats
path: root/vlcwrapper/.svn/text-base/VLCVideoWidget.cpp.svn-base
blob: 69d111c5f4f08a2878a59d306967ba3ce3f11beb (plain) (blame)
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
#include "VLCVideoWidget.h"
#include "VLCLoader.h"

VLCVideoWidget::VLCVideoWidget(QWidget *parent) :
		QFrame(parent)
{
	m_mediaPlayer = libvlc_media_player_new(VLCLoader::instance()->vlc(), VLCLoader::instance()->exception());
	VLCLoader::instance()->checkError();
	m_eventManager = libvlc_media_player_event_manager(m_mediaPlayer, VLCLoader::instance()->exception());
	VLCLoader::instance()->checkError();
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerOpening, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerBuffering, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerPlaying, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerPaused, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerStopped, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerForward, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerBackward, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerEndReached, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerEncounteredError, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerTimeChanged, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerPositionChanged, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerSeekableChanged, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerPausableChanged, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerTitleChanged, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_attach(m_eventManager, libvlc_MediaPlayerSnapshotTaken, vlcCallbacks, this, VLCLoader::instance()->exception());
	VLCLoader::instance()->checkError();
	m_media = 0;
	setAttribute(Qt::WA_PaintOnScreen, true);
}
VLCVideoWidget::~VLCVideoWidget()
{
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerOpening, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerBuffering, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerPlaying, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerPaused, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerStopped, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerForward, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerBackward, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerEndReached, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerEncounteredError, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerTimeChanged, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerPositionChanged, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerSeekableChanged, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerPausableChanged, vlcCallbacks, this, VLCLoader::instance()->exception());	
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerTitleChanged, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_event_detach(m_eventManager, libvlc_MediaPlayerSnapshotTaken, vlcCallbacks, this, VLCLoader::instance()->exception());
	libvlc_media_player_stop(m_mediaPlayer, VLCLoader::instance()->exception());
	libvlc_media_player_release(m_mediaPlayer);
	if (m_media)
		libvlc_media_release(m_media);
}
void VLCVideoWidget::vlcCallbacks(const libvlc_event_t* event, void* ptr)
{
	VLCVideoWidget *self = reinterpret_cast<VLCVideoWidget*>(ptr);
	switch(event->type)
	{
 case libvlc_MediaPlayerOpening:
		self->emit opening();
		break;
 case libvlc_MediaPlayerBuffering:
		self->emit buffering();
		break;
 case libvlc_MediaPlayerPlaying:
		self->emit playing();
		break;
 case libvlc_MediaPlayerPaused:
		self->emit paused();
		break;
 case libvlc_MediaPlayerStopped:
		self->emit stopped();
		break;
 case libvlc_MediaPlayerForward:
		self->emit forward();
		break;
 case libvlc_MediaPlayerBackward:
		self->emit backward();
		break;
 case libvlc_MediaPlayerEndReached:
		self->emit endReached();
		break;
 case libvlc_MediaPlayerEncounteredError:
		self->emit error();
		break;
 case libvlc_MediaPlayerTimeChanged:
		self->emit timeChanged();
		break;
 case libvlc_MediaPlayerPositionChanged:
		self->emit positionChanged();
		break;
 case libvlc_MediaPlayerSeekableChanged:
		self->emit seekableChanged();
		break;
 case libvlc_MediaPlayerPausableChanged:
		self->emit pausableChanged();
		break;
 case libvlc_MediaPlayerTitleChanged:
		self->emit titleChanged();
		break;
 case libvlc_MediaPlayerSnapshotTaken:
		self->emit snapshotTaken();
		break;

	}
}

/*
the file has to be in one of the following formats:
    [file://]filename              Plain media file
    http://ip:port/file            HTTP URL
    ftp://ip:port/file             FTP URL
    mms://ip:port/file             MMS URL
    screen://                      Screen capture
    [dvd://][device][@raw_device]  DVD device
    [vcd://][device]               VCD device
    [cdda://][device]              Audio CD device
    udp:[[<source address>]@[<bind address>][:<bind port>]]
*/
bool VLCVideoWidget::setSource(const QString &source)
{
	m_media = libvlc_media_new(VLCLoader::instance()->vlc(), source.toAscii(), VLCLoader::instance()->exception());
	if (!VLCLoader::instance()->checkError())
		return false;
	libvlc_media_player_set_media(m_mediaPlayer, m_media, VLCLoader::instance()->exception());
	if (!VLCLoader::instance()->checkError())
		return false;
	return true;
}
QString VLCVideoWidget::source() const
{
	QString mrl(libvlc_media_get_mrl(m_media, VLCLoader::instance()->exception()));
	if (VLCLoader::instance()->checkError())
		return mrl;
	return QString();
}
bool VLCVideoWidget::play()
{
#if defined(Q_WS_WIN)
	libvlc_media_player_set_hwnd(m_mediaPlayer, winId(), VLCLoader::instance()->exception());
#elif defined(Q_WS_MAC)
	libvlc_media_player_set_agl(m_mediaPlayer, winId(), VLCLoader::instance()->exception());
#elif defined(Q_WS_X11)
	libvlc_media_player_set_xwindow(m_mediaPlayer, winId(), VLCLoader::instance()->exception());
#endif
	if(!VLCLoader::instance()->checkError())
		return false;
	libvlc_media_player_play(m_mediaPlayer, VLCLoader::instance()->exception());
	return VLCLoader::instance()->checkError();
}
bool VLCVideoWidget::pause()
{
	libvlc_media_player_pause(m_mediaPlayer, VLCLoader::instance()->exception());
	return VLCLoader::instance()->checkError();
}
bool VLCVideoWidget::stop()
{
	libvlc_media_player_stop(m_mediaPlayer, VLCLoader::instance()->exception());
	return VLCLoader::instance()->checkError();
}
bool VLCVideoWidget::isPlaying() const
{
	bool retVal = libvlc_media_player_is_playing(m_mediaPlayer, VLCLoader::instance()->exception()) == 1;
	return VLCLoader::instance()->checkError() && retVal;
}
qint64 VLCVideoWidget::length() const
{
	qint64 retVal = libvlc_media_player_get_length(m_mediaPlayer, VLCLoader::instance()->exception());
	if(VLCLoader::instance()->checkError())
		return retVal;
	return -1;
}
qint64 VLCVideoWidget::time() const
{
	qint64 retVal = libvlc_media_player_get_time(m_mediaPlayer, VLCLoader::instance()->exception());
	if(VLCLoader::instance()->checkError())
		return retVal;
	return -1;
}
bool VLCVideoWidget::setTime(qint64 time)
{
	libvlc_media_player_set_time(m_mediaPlayer, time, VLCLoader::instance()->exception());
	return VLCLoader::instance()->checkError();
}
float VLCVideoWidget::position() const
{
	float retVal = libvlc_media_player_get_position(m_mediaPlayer, VLCLoader::instance()->exception());
	if(VLCLoader::instance()->checkError())
		return retVal;
	return -1.0;
}
bool VLCVideoWidget::setPosition(float position)
{
	libvlc_media_player_set_position(m_mediaPlayer, position, VLCLoader::instance()->exception());
	return VLCLoader::instance()->checkError();
}
int VLCVideoWidget::chapter() const
{
	int retVal = libvlc_media_player_get_chapter(m_mediaPlayer, VLCLoader::instance()->exception());
	if(VLCLoader::instance()->checkError())
		return retVal;
	return -1;
}
bool VLCVideoWidget::setChapter(int chapter)
{
	libvlc_media_player_set_chapter(m_mediaPlayer, chapter, VLCLoader::instance()->exception());
	return VLCLoader::instance()->checkError();
}
int VLCVideoWidget::chapterCount() const
{
	int retVal = libvlc_media_player_get_chapter_count(m_mediaPlayer, VLCLoader::instance()->exception());
	if(VLCLoader::instance()->checkError())
		return retVal;
	return -1;
}
bool VLCVideoWidget::willPlay() const
{
	bool retVal = libvlc_media_player_will_play(m_mediaPlayer, VLCLoader::instance()->exception()) == 1;
	return VLCLoader::instance()->checkError() && retVal;
}
int VLCVideoWidget::chapterCountForTitle(int title) const
{
	int retVal = libvlc_media_player_get_chapter_count_for_title(m_mediaPlayer, title, VLCLoader::instance()->exception());
	if(VLCLoader::instance()->checkError())
		return retVal;
	return -1;
}
int VLCVideoWidget::title() const
{
	int retVal = libvlc_media_player_get_title(m_mediaPlayer, VLCLoader::instance()->exception());
	if(VLCLoader::instance()->checkError())
		return retVal;
	return -1;
}
bool VLCVideoWidget::setTitle(int title)
{
	libvlc_media_player_set_title(m_mediaPlayer, title, VLCLoader::instance()->exception());
	return VLCLoader::instance()->checkError();
}
int VLCVideoWidget::titleCount() const
{
	int retVal = libvlc_media_player_get_title_count(m_mediaPlayer, VLCLoader::instance()->exception());
	if(VLCLoader::instance()->checkError())
		return retVal;
	return -1;
}
float VLCVideoWidget::fps() const
{
	float retVal = libvlc_media_player_get_fps(m_mediaPlayer, VLCLoader::instance()->exception());
	if(VLCLoader::instance()->checkError())
		return retVal;
	return -1.0;
}
libvlc_state_t VLCVideoWidget::state() const
{
	libvlc_state_t retVal = libvlc_media_player_get_state(m_mediaPlayer, VLCLoader::instance()->exception());
	if(VLCLoader::instance()->checkError())
		return retVal;
	return libvlc_Error;
}
bool VLCVideoWidget::moveToPreviousChapter()
{
	libvlc_media_player_previous_chapter(m_mediaPlayer, VLCLoader::instance()->exception());
	return VLCLoader::instance()->checkError();
}
bool VLCVideoWidget::moveToNextChapter()
{
	libvlc_media_player_next_chapter(m_mediaPlayer, VLCLoader::instance()->exception());
	return VLCLoader::instance()->checkError();
}
float VLCVideoWidget::rate() const
{
	float retVal = libvlc_media_player_get_rate(m_mediaPlayer, VLCLoader::instance()->exception());
	if(VLCLoader::instance()->checkError())
		return retVal;
	return -1.0;
}
bool VLCVideoWidget::setRate(float rate)
{
	libvlc_media_player_set_rate(m_mediaPlayer, rate, VLCLoader::instance()->exception());
	return VLCLoader::instance()->checkError();
}
bool VLCVideoWidget::isSeekable() const
{
	bool retVal = libvlc_media_player_is_seekable(m_mediaPlayer, VLCLoader::instance()->exception()) == 1;
	return VLCLoader::instance()->checkError() && retVal;
}
bool VLCVideoWidget::canPause() const
{
	bool retVal = libvlc_media_player_can_pause(m_mediaPlayer, VLCLoader::instance()->exception()) == 1;
	return VLCLoader::instance()->checkError() && retVal;
}
bool VLCVideoWidget::hasVideoOut() const
{
	bool retVal = libvlc_media_player_has_vout(m_mediaPlayer, VLCLoader::instance()->exception()) == 1;
	return VLCLoader::instance()->checkError() && retVal;
}
QSize VLCVideoWidget::sizeHint() const
{
	if (!hasVideoOut())
		return QSize(640, 480); //HACK: fix this?
	int height = libvlc_video_get_height(m_mediaPlayer, VLCLoader::instance()->exception());
	if (!VLCLoader::instance()->checkError())
		return QSize();
	int width = libvlc_video_get_width(m_mediaPlayer, VLCLoader::instance()->exception());
	if (!VLCLoader::instance()->checkError())
		return QSize();
	return QSize(width, height);
}