-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcvlcctrl.cpp
708 lines (647 loc) · 19.1 KB
/
cvlcctrl.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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*********************** Information *************************\
| $HeadURL$
|
| Author: Jo2003
|
| Begin: 01.02.2010 / 10:50:35
|
| Last edited by: $Author$
|
| $Id$
\*************************************************************/
#include "cvlcctrl.h"
#include "externals_inc.h"
/* -----------------------------------------------------------------\
| Method: CVlcCtrl / constructor
| Begin: 01.02.2010 / 10:05:00
| Author: Jo2003
| Description: create object, init values
|
| Parameters: pointer to parent window
|
| Returns: --
\----------------------------------------------------------------- */
CVlcCtrl::CVlcCtrl(const QString &path, QObject *parent) : QProcess(parent)
{
bForcedTranslit = false;
sFrcMx = "no";
bTranslit = false;
pTranslit = NULL;
bUseLibVlc = false;
libVlcPlayState = IncPlay::PS_WTF;
reqState = IncPlay::PS_WTF;
bOwnDownloader = false;
if (path != "")
{
LoadPlayerModule(path);
}
setStandardOutputFile(QString("%1/%2").arg(pFolders->getDataDir()).arg(PLAYER_LOG_FILE),
QIODevice::Truncate);
setProcessChannelMode(QProcess::MergedChannels);
connect (this, SIGNAL(stateChanged(QProcess::ProcessState)), this,
SLOT(slotStateChanged(QProcess::ProcessState)));
connect (this, SIGNAL(sigOwnStateChanged(QProcess::ProcessState)), this,
SLOT(slotStateChanged(QProcess::ProcessState)));
}
/* -----------------------------------------------------------------\
| Method: ~CVlcCtrl / destructor
| Begin: 01.02.2010 / 10:05:00
| Author: Jo2003
| Description: clean at destruction, stop process, if running
|
| Parameters: --
|
| Returns: --
\----------------------------------------------------------------- */
CVlcCtrl::~CVlcCtrl()
{
// since the process isn't started detached, it will be closed
// when vlc-record closes ...
if (IsRunning())
{
// close vlc ...
terminate();
// wait until vlc closes ...
waitForFinished(3000);
}
}
/* -----------------------------------------------------------------\
| Method: SetLibVLCPlayer
| Begin: 03.03.2010 / 08:45:00
| Author: Jo2003
| Description: set libVlc player instance
|
| Parameters: pointer to CPlayer
|
| Returns: --
\----------------------------------------------------------------- */
void CVlcCtrl::UseLibVlc(bool bUsage)
{
bUseLibVlc = bUsage;
}
/* -----------------------------------------------------------------\
| Method: SetTranslitPointer
| Begin: 11.02.2010 / 12:05:00
| Author: Jo2003
| Description: set translit class pointer
|
| Parameters: pointer to CTranslit
|
| Returns: --
\----------------------------------------------------------------- */
void CVlcCtrl::SetTranslitPointer(CTranslit *pTr)
{
pTranslit = pTr;
}
/* -----------------------------------------------------------------\
| Method: SetTranslitSettings
| Begin: 11.02.2010 / 12:05:00
| Author: Jo2003
| Description: set translit settings
|
| Parameters: enable / disable translit
|
| Returns: --
\----------------------------------------------------------------- */
void CVlcCtrl::SetTranslitSettings(bool bTr)
{
bTranslit = bTr;
}
/* -----------------------------------------------------------------\
| Method: LoadPlayerModule
| Begin: 11.02.2010 / 10:05:00
| Author: Jo2003
| Description: load player module
|
| Parameters: path to player module
|
| Returns: 0 --> ok
| -1 --> any error
\----------------------------------------------------------------- */
int CVlcCtrl::LoadPlayerModule(const QString &sPath)
{
int iRV = -1;
if (sPath != "")
{
QFile fModule(sPath);
QRegExp rx("^([^ =]*).*=.*<<(.*)>>.*$");
if (fModule.open(QIODevice::ReadOnly))
{
// reset strings ...
sLivePlay = "";
sArchPlay = "";
sLiveRec = "";
sArchRec = "";
sLiveSilentRec = "";
sArchSilentRec = "";
bForcedTranslit = false;
sFrcMx = "no";
bOwnDownloader = false;
sAddVlcOpts = "";
QTextStream str(&fModule);
QString sLine;
str.setCodec ("UTF-8");
do
{
// read line by line from mod file ...
sLine = str.readLine();
if (rx.indexIn(sLine) > -1)
{
if (rx.cap(1) == CMD_PLAY_LIVE)
{
sLivePlay = rx.cap(2);
}
else if (rx.cap(1) == CMD_PLAY_ARCH)
{
sArchPlay = rx.cap(2);
}
else if (rx.cap(1) == CMD_REC_LIVE)
{
sLiveRec = rx.cap(2);
}
else if (rx.cap(1) == CMD_REC_ARCH)
{
sArchRec = rx.cap(2);
}
else if (rx.cap(1) == CMD_SIL_REC_LIVE)
{
sLiveSilentRec = rx.cap(2);
}
else if (rx.cap(1) == CMD_SIL_REC_ARCH)
{
sArchSilentRec = rx.cap(2);
}
else if (rx.cap(1) == FLAG_TRANSLIT)
{
bForcedTranslit = (rx.cap(2).toLower() == "yes") ? true : false;
}
else if (rx.cap(1) == FORCE_MUX)
{
sFrcMx = rx.cap(2);
sFrcMx = sFrcMx.toLower();
}
else if (rx.cap(1) == DOWN_FIRST) // don't use VLC downloader
{ // but our own ...
bOwnDownloader = (rx.cap(2).toLower() == "yes") ? true : false;
}
else if (rx.cap(1) == ADD_LVLC_OPTS) // additional libVLC options
{
sAddVlcOpts = rx.cap(2);
}
}
} while (!str.atEnd());
// if the http play stuff is set
// we assume that all is well ...
if (sLivePlay != "")
{
iRV = 0;
mInfo(tr("Player module '%1' successfully parsed ...").arg(sPath));
}
fModule.close();
}
}
if (iRV)
{
QMessageBox::warning(NULL, tr("Warning!"), tr("Sorry! Can't parse player module!"));
mWarn(tr("Can't parse player module!"));
}
return iRV;
}
/* -----------------------------------------------------------------\
| Method: start
| Begin: 01.02.2010 / 10:05:00
| Author: Jo2003
| Description: start vlc
|
| Parameters: command line, optional runtime in seconds, detach flag
|
| Returns: <= 0 --> error starting vlc
| else --> process id
\----------------------------------------------------------------- */
Q_PID CVlcCtrl::start(const QString &sCmdLine, int iRunTime, bool bDetach, IncPlay::ePlayStates req)
{
Q_PID vlcPid = 0;
// store what we request ...
if (req != IncPlay::PS_WTF)
{
reqState = req;
}
// -------------------------------------------------------
// use libVLC player ...
// -------------------------------------------------------
if (bUseLibVlc)
{
// detach isn't possible ...
bDetach = false;
// play media ...
emit sigLibVlcPlayMedia(sCmdLine, sAddVlcOpts);
// assume that all is well ...
vlcPid = (Q_PID)99; // anything but 0 ...
}
else
{
// -------------------------------------------------------
// external player ...
// -------------------------------------------------------
if (bDetach)
{
// detach player ...
mInfo(tr("Start player detached using folling command line:\n --> %1").arg(sCmdLine));
if (QProcess::startDetached(sCmdLine))
{
vlcPid = (Q_PID)99; // anything but 0 ...
}
}
else
{
// player is under control. check if it is running ...
QMessageBox::StandardButton btn = QMessageBox::Yes;
if (IsRunning())
{
btn = QMessageBox::question(NULL, tr("Warning!"),
tr("Player is already running! Do you want to proceed?"),
QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
// we want to proceed -> stop former started player ...
if (btn == QMessageBox::Yes)
{
// stop process kindly ...
QProcess::terminate();
if (!waitForFinished(1000))
{
// process not stopped -> kill it ...
kill();
}
}
}
// now player shouldn't run ... however ...
if (btn == QMessageBox::Yes)
{
mInfo(tr("Start player using folling command line:\n --> %1").arg(sCmdLine));
QProcess::start(sCmdLine);
if (waitForStarted(3000))
{
vlcPid = pid();
}
else
{
kill();
}
}
}
}
// should we stop the player after a while ... ?
if ((iRunTime > 0) && (vlcPid != 0) && !bDetach)
{
tRunTime.singleShot(iRunTime * 1000, this, SLOT(terminate()));
}
return vlcPid;
}
/* -----------------------------------------------------------------\
| Method: stop
| Begin: 01.02.2010 / 10:05:00
| Author: Jo2003
| Description: stop vlc
|
| Parameters: --
|
| Returns: --
\----------------------------------------------------------------- */
void CVlcCtrl::stop()
{
if (IsRunning())
{
terminate();
}
}
/* -----------------------------------------------------------------\
| Method: CancelTimer
| Begin: 01.02.2010 / 10:05:00
| Author: Jo2003
| Description: cancel run time timer
|
| Parameters: --
|
| Returns: --
\----------------------------------------------------------------- */
void CVlcCtrl::CancelTimer()
{
tRunTime.stop();
}
/* -----------------------------------------------------------------\
| Method: SetTimer
| Begin: 01.02.2010 / 10:05:00
| Author: Jo2003
| Description: (re) set stop timer
|
| Parameters: new timeout (sec.)
|
| Returns: --
\----------------------------------------------------------------- */
void CVlcCtrl::SetTimer(uint uiTime)
{
tRunTime.stop();
tRunTime.singleShot(uiTime * 1000, this, SLOT(terminate()));
}
/* -----------------------------------------------------------------\
| Method: IsRunning
| Begin: 01.02.2010 / 10:05:00
| Author: Jo2003
| Description: is vlc running ?
|
| Parameters: --
|
| Returns: true --> running
| false --> not running
\----------------------------------------------------------------- */
bool CVlcCtrl::IsRunning()
{
bool bRV = false;
if (bUseLibVlc)
{
switch (libVlcPlayState)
{
case IncPlay::PS_BUFFER:
case IncPlay::PS_OPEN:
case IncPlay::PS_PAUSE:
case IncPlay::PS_PLAY:
bRV = true;
break;
default:
break;
}
}
else
{
switch (state())
{
case QProcess::Running:
case QProcess::Starting:
bRV = true;
break;
default:
break;
}
}
return bRV;
}
/* -----------------------------------------------------------------\
| Method: CreateClArgs
| Begin: 01.02.2010 / 14:05:00
| Author: Jo2003
| Description: create command line arguments for vlc ...
|
| Parameters: vlc action enum, url, destination, mux
|
| Returns: command line arguments as string
\----------------------------------------------------------------- */
QString CVlcCtrl::CreateClArgs (vlcctrl::eVlcAct eAct, const QString &sPlayer,
const QString &url, int iCacheTime,
const QString &dst, const QString &mux)
{
QString sCmdLine;
QFileInfo dstInfo(dst);
QString sDstFile;
QString sMux;
QRegExp rx ("^([a-zA-Z]{1})://(.*)$");
// is muxing forced ... ?
if (sFrcMx == "no")
{
// standard muxing ...
sMux = mux;
}
else
{
mInfo(tr("Muxing forced to '%1'!").arg(sFrcMx));
sMux = sFrcMx;
}
if (doTranslit() && !bOwnDownloader)
{
sDstFile = QString("%1/%2").arg(dstInfo.path())
.arg(pTranslit->CyrToLat(dstInfo.baseName()));
}
else
{
sDstFile = dst;
}
// fix destination path to avoid double slashes ...
if (rx.indexIn(sDstFile) > -1)
{
// we found a double slash ...
mInfo(tr("Remove double slash in target path ..."));
sDstFile = QString("%1:/%2").arg(rx.cap(1)).arg(rx.cap(2));
}
#if defined Q_OS_WIN32
if (bOwnDownloader // use own downloader ...
&& !bUseLibVlc // don't use libvlc ...
&& (eAct == vlcctrl::VLC_REC_LIVE))
{
sDstFile.replace ("/", "\\");
}
#endif
switch (eAct)
{
// play stream using http protocol ...
case vlcctrl::VLC_PLAY_LIVE:
sCmdLine = sLivePlay;
sCmdLine.replace(TMPL_PLAYER, sPlayer);
sCmdLine.replace(TMPL_URL, url);
sCmdLine.replace(TMPL_CACHE, QString::number(iCacheTime));
sCmdLine.replace(TMPL_TEMP, QDir::tempPath());
break;
// play stream using rtsp protocol ...
case vlcctrl::VLC_PLAY_ARCH:
sCmdLine = sArchPlay;
sCmdLine.replace(TMPL_PLAYER, sPlayer);
sCmdLine.replace(TMPL_URL, url);
sCmdLine.replace(TMPL_CACHE, QString::number(iCacheTime));
sCmdLine.replace(TMPL_TEMP, QDir::tempPath());
break;
// record stream using http protocol ...
case vlcctrl::VLC_REC_LIVE:
sCmdLine = sLiveRec;
sCmdLine.replace(TMPL_PLAYER, sPlayer);
sCmdLine.replace(TMPL_URL, url);
sCmdLine.replace(TMPL_CACHE, QString::number(iCacheTime));
sCmdLine.replace(TMPL_MUX, sMux);
sCmdLine.replace(TMPL_DST, sDstFile);
break;
// record stream using rtsp protocol ...
case vlcctrl::VLC_REC_ARCH:
sCmdLine = sArchRec;
sCmdLine.replace(TMPL_PLAYER, sPlayer);
sCmdLine.replace(TMPL_URL, url);
sCmdLine.replace(TMPL_CACHE, QString::number(iCacheTime));
sCmdLine.replace(TMPL_MUX, sMux);
sCmdLine.replace(TMPL_DST, sDstFile);
break;
// silently record stream using http protocol ...
case vlcctrl::VLC_REC_LIVE_SILENT:
sCmdLine = sLiveSilentRec;
sCmdLine.replace(TMPL_PLAYER, sPlayer);
sCmdLine.replace(TMPL_URL, url);
sCmdLine.replace(TMPL_CACHE, QString::number(iCacheTime));
sCmdLine.replace(TMPL_MUX, sMux);
sCmdLine.replace(TMPL_DST, sDstFile);
break;
// silently record stream using rtsp protocol ...
case vlcctrl::VLC_REC_ARCH_SILENT:
sCmdLine = sArchSilentRec;
sCmdLine.replace(TMPL_PLAYER, sPlayer);
sCmdLine.replace(TMPL_URL, url);
sCmdLine.replace(TMPL_CACHE, QString::number(iCacheTime));
sCmdLine.replace(TMPL_MUX, sMux);
sCmdLine.replace(TMPL_DST, sDstFile);
break;
default:
break;
}
return sCmdLine;
}
/* -----------------------------------------------------------------\
| Method: slotStateChanged
| Begin: 02.02.2010 / 14:05:00
| Author: Jo2003
| Description: send signal when vlc starts / ends ...
|
| Parameters: new process state
|
| Returns: --
\----------------------------------------------------------------- */
void CVlcCtrl::slotStateChanged(QProcess::ProcessState newState)
{
switch (newState)
{
case QProcess::NotRunning:
mInfo(tr("Player has ended ..."));
emit sigVlcEnds((int)reqState);
break;
case QProcess::Running:
mInfo(tr("Player was started ..."));
emit sigVlcStarts((int)reqState);
break;
default:
break;
}
}
/* -----------------------------------------------------------------\
| Method: terminate [slot]
| Begin: 03.03.2010 / 10:05:00
| Author: Jo2003
| Description: override terminate slot with own one ...
|
| Parameters: --
|
| Returns: --
\----------------------------------------------------------------- */
void CVlcCtrl::terminate()
{
if (bUseLibVlc)
{
// This is no external process,
// simply stop the player ...
emit sigLibVlcStop();
emit finished(0, QProcess::NormalExit);
}
else
{
// terminate only if it is running ...
if (IsRunning())
{
QProcess::terminate();
}
else
{
emit finished(0, QProcess::NormalExit);
}
}
}
/* -----------------------------------------------------------------\
| Method: slotLibVlcStateChange [slot]
| Begin: 03.03.2010 / 12:05:00
| Author: Jo2003
| Description: state change from libVlc player ...
|
| Parameters: new play state
|
| Returns: --
\----------------------------------------------------------------- */
void CVlcCtrl::slotLibVlcStateChange (int ps)
{
// something changed ... ?
if (libVlcPlayState != (IncPlay::ePlayStates)ps)
{
libVlcPlayState = (IncPlay::ePlayStates)ps;
mInfo(tr("libVLC reports new state %1").arg((int)ps));
switch(libVlcPlayState)
{
case IncPlay::PS_OPEN:
emit sigOwnStateChanged(QProcess::Starting);
break;
case IncPlay::PS_PLAY:
emit sigOwnStateChanged(QProcess::Running);
break;
case IncPlay::PS_STOP:
case IncPlay::PS_END:
case IncPlay::PS_ERROR:
emit sigOwnStateChanged(QProcess::NotRunning);
break;
default:
break;
}
}
}
/* -----------------------------------------------------------------\
| Method: withLibVLC
| Begin: 24.06.2010 / 18:05:00
| Author: Jo2003
| Description: tell if libVLC is used
|
| Parameters: --
|
| Returns: true --> used
| false --> not used
\----------------------------------------------------------------- */
bool CVlcCtrl::withLibVLC()
{
return bUseLibVlc;
}
/* -----------------------------------------------------------------\
| Method: ownDwnld
| Begin: 13.12.2010 / 15:40
| Author: Jo2003
| Description: tell if own downloader should be used
|
| Parameters: --
|
| Returns: true --> used
| false --> not used
\----------------------------------------------------------------- */
bool CVlcCtrl::ownDwnld()
{
return bOwnDownloader;
}
/* -----------------------------------------------------------------\
| Method: doTranslit
| Begin: 14.08.2012
| Author: Jo2003
| Description: should we translit the filename
|
| Parameters: --
|
| Returns: true --> yes
| false --> no
\----------------------------------------------------------------- */
bool CVlcCtrl::doTranslit()
{
if (bTranslit || bForcedTranslit)
{
return true;
}
else
{
return false;
}
}
/************************* History ***************************\
| $Log$
\*************************************************************/