forked from kevin-shanghai/Strong_RTSP_Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
executable file
·275 lines (212 loc) · 7.47 KB
/
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
/******************************
* Qt player using libVLC *
* By protonux *
* *
* Under WTFPL *
******************************/
#include "player.h"
#include "vlc.h"
#define qtu( i ) ((i).toUtf8().constData())
#include <QtGui>
class OpenURLDialog;
VLCMainwindow::VLCMainwindow() {
vlcPlayer = NULL;
this->setWindowTitle("RTSP player");
/* Init libVLC */
if((vlcObject = libvlc_new(0,NULL)) == NULL) {
printf("Could not init libVLC");
exit(1);
}
/* Display libVLC version */
printf("libVLC version: %s\n",libvlc_get_version());
/* Interface initialisation */
initMenus();
initComponents();
}
VLCMainwindow::~VLCMainwindow() {
if(vlcObject)
libvlc_release(vlcObject);
}
void VLCMainwindow::initMenus() {
centralWidget = new QWidget;
videoWidget = new QWidget;
videoWidget->setAutoFillBackground( true );
QPalette plt = palette();
plt.setColor( QPalette::Window, Qt::darkGray);
videoWidget->setPalette( plt );
QMenu *fileMenu = menuBar()->addMenu("&File");
QMenu *editMenu = menuBar()->addMenu("&Edit");
QAction *Open = new QAction("&Open", this);
QAction *Openurl = new QAction("&OpenURL", this);
QAction *Quit = new QAction("&Quit", this);
QAction *playAc = new QAction("&Play/Pause", this);
Open->setShortcut(QKeySequence("Ctrl+O"));
Quit->setShortcut(QKeySequence("Ctrl+Q"));
fileMenu->addAction(Open);
fileMenu->addAction(Openurl);
fileMenu->addAction(Quit);
editMenu->addAction(playAc);
connect(Open, SIGNAL(triggered()), this, SLOT(openFile()));
connect(Openurl, SIGNAL(triggered()), this, SLOT(ShowopenURLDialog()));
connect(playAc, SIGNAL(triggered()), this, SLOT(play()));
connect(Quit, SIGNAL(triggered()), qApp, SLOT(quit()));
}
void VLCMainwindow::initComponents() {
playBut = new QPushButton("Play");
QObject::connect(playBut, SIGNAL(clicked()), this, SLOT(play()));
stopBut = new QPushButton("Stop");
QObject::connect(stopBut, SIGNAL(clicked()), this, SLOT(stop()));
muteBut = new QPushButton("Mute");
QObject::connect(muteBut, SIGNAL(clicked()), this, SLOT(mute()));
volumeSlider = new QSlider(Qt::Horizontal);
QObject::connect(volumeSlider, SIGNAL(sliderMoved(int)), this, SLOT(changeVolume(int)));
volumeSlider->setValue(80);
slider = new QSlider(Qt::Horizontal);
slider->setMaximum(1000);
QObject::connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(changePosition(int)));
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateInterface()));
timer->start(100);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(playBut);
layout->addWidget(stopBut);
layout->addWidget(muteBut);
layout->addWidget(volumeSlider);
QVBoxLayout *layout2 = new QVBoxLayout;
layout2->addWidget(videoWidget);
layout2->addWidget(slider);
layout2->addLayout(layout);
centralWidget->setLayout(layout2);
setCentralWidget(centralWidget);
resize( 600, 400);
}
void VLCMainwindow::ShowopenURLDialog()
{
URLDialog = new OpenURLDialog;
URLDialog->resize(700, 100);
URLDialog->show();
}
void VLCMainwindow::openFile() {
/* Just the basic file-select box */
QString fileOpen = QFileDialog::getOpenFileName(this,tr("Load a file"), "~");
fileOpen.replace("/", "\\");
/* Stop if something is playing */
if( vlcPlayer && libvlc_media_player_is_playing(vlcPlayer) )
stop();
/* New Media */
libvlc_media_t *vlcMedia = libvlc_media_new_path(vlcObject,qtu(fileOpen));
if( !vlcMedia )
return;
vlcPlayer = libvlc_media_player_new_from_media (vlcMedia);
libvlc_media_release(vlcMedia);
/* Integrate the video in the interface */
#if defined(Q_OS_MAC)
libvlc_media_player_set_nsobject(vlcPlayer, videoWidget->winId());
#elif defined(Q_OS_UNIX)
libvlc_media_player_set_xwindow(vlcPlayer, videoWidget->winId());
#elif defined(Q_OS_WIN)
libvlc_media_player_set_hwnd(vlcPlayer, videoWidget->winId());
#endif
//int windid = videoWidget->winId();
//libvlc_media_player_set_xwindow (vlcPlayer, windid );
/* And play */
libvlc_media_player_play (vlcPlayer);
//Set vars and text correctly
playBut->setText("Pause");
}
void VLCMainwindow::play() {
if(vlcPlayer)
{
if (libvlc_media_player_is_playing(vlcPlayer))
{
libvlc_media_player_pause(vlcPlayer);
playBut->setText("Play");
}
else
{
libvlc_media_player_play(vlcPlayer);
playBut->setText("Pause");
}
}
}
int VLCMainwindow::changeVolume(int vol) { //Called if you change the volume slider
if(vlcPlayer)
return libvlc_audio_set_volume (vlcPlayer,vol);
return 0;
}
void VLCMainwindow::changePosition(int pos) { //Called if you change the position slider
if(vlcPlayer) //It segfault if vlcPlayer don't exist
libvlc_media_player_set_position(vlcPlayer,(float)pos/(float)1000);
}
void VLCMainwindow::updateInterface() { //Update interface and check if song is finished
if(vlcPlayer) //It segfault if vlcPlayer don't exist
{
/* update the timeline */
float pos = libvlc_media_player_get_position(vlcPlayer);
int siderPos=(int)(pos*(float)(1000));
slider->setValue(siderPos);
/* Stop the media */
if (libvlc_media_player_get_state(vlcPlayer) == 6) { this->stop(); }
}
}
void VLCMainwindow::stop() {
if(vlcPlayer) {
libvlc_media_player_stop(vlcPlayer);
libvlc_media_player_release(vlcPlayer);
slider->setValue(0);
playBut->setText("Play");
}
vlcPlayer = NULL;
}
void VLCMainwindow::mute() {
if(vlcPlayer) {
if(volumeSlider->value() == 0) { //if already muted...
this->changeVolume(40);
volumeSlider->setValue(40);
} else { //else mute volume
this->changeVolume(0);
volumeSlider->setValue(0);
}
}
}
void VLCMainwindow::closeEvent(QCloseEvent *event) {
stop();
event->accept();
}
void VLCMainwindow::StartPlay()
{
URLDialog = new OpenURLDialog;
URLDialog->hide();
if( vlcPlayer && libvlc_media_player_is_playing(vlcPlayer) )
stop();
// New Media
// libvlc_media_t *vlcMedia = libvlc_media_new_path(vlcObject,qtu(fileOpen));
libvlc_media_t *vlcMedia = libvlc_media_new_location(vlcObject,qtu (URLDialog->urlLineEdit->text()));
if( !vlcMedia )
return;
vlcPlayer = libvlc_media_player_new_from_media (vlcMedia);
libvlc_media_release(vlcMedia);
//libvlc_media_player_set_xwindow(vlcPlayer, windid);
/* And play */
libvlc_media_player_play (vlcPlayer);
//Set vars and text correctly
playBut->setText("Pause");
}
void VLCMainwindow::StartPlayMainURL(QString rtspURL)
{
if( vlcPlayer && libvlc_media_player_is_playing(vlcPlayer) )
stop();
// New Media
// libvlc_media_t *vlcMedia = libvlc_media_new_path(vlcObject,qtu(fileOpen));
libvlc_media_t *vlcMedia = libvlc_media_new_location(vlcObject,qtu(rtspURL));
if( !vlcMedia )
return;
QMessageBox::warning(NULL, "Hello", "startPlayMainURL");
vlcPlayer = libvlc_media_player_new_from_media (vlcMedia);
libvlc_media_release(vlcMedia);
//libvlc_media_player_set_xwindow(vlcPlayer, windid);
/* And play */
libvlc_media_player_play (vlcPlayer);
//Set vars and text correctly
playBut->setText("Pause");
}