1
/* smplayer, GUI front-end for mplayer.
2
Copyright (C) 2006-2008 Ricardo Villalba <rvm@escomposlinux.org>
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
#include "mplayerprocess.h"
21
#include <QStringList>
22
#include <QApplication>
25
#include "preferences.h"
26
#include "mplayerversion.h"
29
using namespace Global;
31
MplayerProcess::MplayerProcess(QObject * parent) : MyProcess(parent)
33
#if NOTIFY_SUB_CHANGES
34
qRegisterMetaType<SubTracks>("SubTracks");
37
#if NOTIFY_AUDIO_CHANGES
38
qRegisterMetaType<Tracks>("Tracks");
41
connect( this, SIGNAL(lineAvailable(QByteArray)),
42
this, SLOT(parseLine(QByteArray)) );
44
connect( this, SIGNAL(finished(int,QProcess::ExitStatus)),
45
this, SLOT(processFinished(int,QProcess::ExitStatus)) );
47
connect( this, SIGNAL(error(QProcess::ProcessError)),
48
this, SLOT(gotError(QProcess::ProcessError)) );
50
notified_mplayer_is_running = false;
52
mplayer_svn = -1; // Not found yet
55
MplayerProcess::~MplayerProcess() {
58
bool MplayerProcess::start() {
60
notified_mplayer_is_running = false;
62
mplayer_svn = -1; // Not found yet
63
received_end_of_file = false;
65
#if NOTIFY_SUB_CHANGES
67
subtitle_info_received = false;
68
subtitle_info_changed = false;
71
#if NOTIFY_AUDIO_CHANGES
73
audio_info_changed = false;
76
#if GENERIC_CHAPTER_SUPPORT
77
dvd_current_title = -1;
81
return waitForStarted();
84
void MplayerProcess::writeToStdin(QString text) {
86
//qDebug("MplayerProcess::writeToStdin");
87
write( text.toLocal8Bit() + "\n");
89
qWarning("MplayerProcess::writeToStdin: process not running");
93
static QRegExp rx_av("^[AV]: *([0-9,:.-]+)");
94
static QRegExp rx_frame("^[AV]:.* (\\d+)\\/.\\d+");// [0-9,.]+");
95
static QRegExp rx("^(.*)=(.*)");
96
#if !NOTIFY_AUDIO_CHANGES
97
static QRegExp rx_audio_mat("^ID_AID_(\\d+)_(LANG|NAME)=(.*)");
99
static QRegExp rx_video("^ID_VID_(\\d+)_(LANG|NAME)=(.*)");
100
static QRegExp rx_title("^ID_DVD_TITLE_(\\d+)_(LENGTH|CHAPTERS|ANGLES)=(.*)");
101
static QRegExp rx_winresolution("^VO: \\[(.*)\\] (\\d+)x(\\d+) => (\\d+)x(\\d+)");
102
static QRegExp rx_ao("^AO: \\[(.*)\\]");
103
static QRegExp rx_paused("^ID_PAUSED");
104
#if !CHECK_VIDEO_CODEC_FOR_NO_VIDEO
105
static QRegExp rx_novideo("^Video: no video");
107
static QRegExp rx_cache("^Cache fill:.*");
108
static QRegExp rx_create_index("^Generating Index:.*");
109
static QRegExp rx_play("^Starting playback...");
110
static QRegExp rx_connecting("^Connecting to .*");
111
static QRegExp rx_resolving("^Resolving .*");
112
static QRegExp rx_screenshot("^\\*\\*\\* screenshot '(.*)'");
113
static QRegExp rx_endoffile("^Exiting... \\(End of file\\)|^ID_EXIT=EOF");
114
static QRegExp rx_mkvchapters("\\[mkv\\] Chapter (\\d+) from");
115
static QRegExp rx_aspect2("^Movie-Aspect is ([0-9,.]+):1");
116
static QRegExp rx_fontcache("^\\[ass\\] Updating font cache|^\\[ass\\] Init");
119
static QRegExp rx_vcd("^ID_VCD_TRACK_(\\d+)_MSF=(.*)");
122
static QRegExp rx_cdda("^ID_CDDA_TRACK_(\\d+)_MSF=(.*)");
126
static QRegExp rx_subtitle("^ID_(SUBTITLE|FILE_SUB|VOBSUB)_ID=(\\d+)");
127
static QRegExp rx_sid("^ID_(SID|VSID)_(\\d+)_(LANG|NAME)=(.*)");
128
static QRegExp rx_subtitle_file("^ID_FILE_SUB_FILENAME=(.*)");
131
#if NOTIFY_AUDIO_CHANGES
132
static QRegExp rx_audio("^ID_AUDIO_ID=(\\d+)");
133
static QRegExp rx_audio_info("^ID_AID_(\\d+)_(LANG|NAME)=(.*)");
137
static QRegExp rx_clip_name("^ (name|title): (.*)", Qt::CaseInsensitive);
138
static QRegExp rx_clip_artist("^ artist: (.*)", Qt::CaseInsensitive);
139
static QRegExp rx_clip_author("^ author: (.*)", Qt::CaseInsensitive);
140
static QRegExp rx_clip_album("^ album: (.*)", Qt::CaseInsensitive);
141
static QRegExp rx_clip_genre("^ genre: (.*)", Qt::CaseInsensitive);
142
static QRegExp rx_clip_date("^ (creation date|year): (.*)", Qt::CaseInsensitive);
143
static QRegExp rx_clip_track("^ track: (.*)", Qt::CaseInsensitive);
144
static QRegExp rx_clip_copyright("^ copyright: (.*)", Qt::CaseInsensitive);
145
static QRegExp rx_clip_comment("^ comment: (.*)", Qt::CaseInsensitive);
146
static QRegExp rx_clip_software("^ software: (.*)", Qt::CaseInsensitive);
148
static QRegExp rx_stream_title("^.* StreamTitle='(.*)';StreamUrl='(.*)';");
151
void MplayerProcess::parseLine(QByteArray ba) {
152
//qDebug("MplayerProcess::parseLine: '%s'", ba.data() );
157
#if COLOR_OUTPUT_SUPPORT
158
QString line = Helper::stripColorsTags(QString::fromLocal8Bit(ba));
160
QString line = QString::fromLocal8Bit(ba);
164
//qDebug("%s", line.toUtf8().data());
165
if (rx_av.indexIn(line) > -1) {
166
double sec = rx_av.cap(1).toDouble();
167
//qDebug("cap(1): '%s'", rx_av.cap(1).toUtf8().data() );
168
//qDebug("sec: %f", sec);
170
#if NOTIFY_SUB_CHANGES
171
if (notified_mplayer_is_running) {
172
if (subtitle_info_changed) {
173
qDebug("MplayerProcess::parseLine: subtitle_info_changed");
174
subtitle_info_changed = false;
175
subtitle_info_received = false;
176
emit subtitleInfoChanged(subs);
178
if (subtitle_info_received) {
179
qDebug("MplayerProcess::parseLine: subtitle_info_received");
180
subtitle_info_received = false;
181
emit subtitleInfoReceivedAgain(subs);
186
#if NOTIFY_AUDIO_CHANGES
187
if (notified_mplayer_is_running) {
188
if (audio_info_changed) {
189
qDebug("MplayerProcess::parseLine: audio_info_changed");
190
audio_info_changed = false;
191
emit audioInfoChanged(audios);
196
if (!notified_mplayer_is_running) {
197
qDebug("MplayerProcess::parseLine: starting sec: %f", sec);
198
#if GENERIC_CHAPTER_SUPPORT
199
if ( (md.chapters <= 0) && (dvd_current_title > 0) &&
200
(md.titles.find(dvd_current_title) != -1) )
202
int idx = md.titles.find(dvd_current_title);
203
md.chapters = md.titles.itemAt(idx).chapters();
204
qDebug("MplayerProcess::parseLine: setting chapters to %d", md.chapters);
208
#if CHECK_VIDEO_CODEC_FOR_NO_VIDEO
209
// Another way to find out if there's no video
210
if (md.video_codec.isEmpty()) {
212
emit receivedNoVideo();
216
emit receivedStartingTime(sec);
217
emit mplayerFullyLoaded();
219
emit receivedCurrentFrame(0); // Ugly hack: set the frame counter to 0
221
notified_mplayer_is_running = true;
224
emit receivedCurrentSec( sec );
227
if (rx_frame.indexIn(line) > -1) {
228
int frame = rx_frame.cap(1).toInt();
229
//qDebug(" frame: %d", frame);
230
emit receivedCurrentFrame(frame);
234
// Emulates mplayer version in Ubuntu:
235
//if (line.startsWith("MPlayer 1.0rc1")) line = "MPlayer 2:1.0~rc1-0ubuntu13.1 (C) 2000-2006 MPlayer Team";
237
emit lineAvailable(line);
239
// Parse other things
240
qDebug("MplayerProcess::parseLine: '%s'", line.toUtf8().data() );
243
if (rx_screenshot.indexIn(line) > -1) {
244
QString shot = rx_screenshot.cap(1);
245
qDebug("MplayerProcess::parseLine: screenshot: '%s'", shot.toUtf8().data());
246
emit receivedScreenshot( shot );
251
if (rx_endoffile.indexIn(line) > -1) {
252
qDebug("MplayerProcess::parseLine: detected end of file");
253
if (!received_end_of_file) {
254
// In case of playing VCDs or DVDs, maybe the first title
255
// is not playable, so the GUI doesn't get the info about
256
// available titles. So if we received the end of file
257
// first let's pretend the file has started so the GUI can have
259
if ( !notified_mplayer_is_running) {
260
emit mplayerFullyLoaded();
263
//emit receivedEndOfFile();
264
// Send signal once the process is finished, not now!
265
received_end_of_file = true;
271
if (rx_winresolution.indexIn(line) > -1) {
273
md.win_width = rx_winresolution.cap(4).toInt();
274
md.win_height = rx_winresolution.cap(5).toInt();
275
md.video_aspect = (double) md.win_width / md.win_height;
278
int w = rx_winresolution.cap(4).toInt();
279
int h = rx_winresolution.cap(5).toInt();
281
emit receivedVO( rx_winresolution.cap(1) );
282
emit receivedWindowResolution( w, h );
283
//emit mplayerFullyLoaded();
287
#if !CHECK_VIDEO_CODEC_FOR_NO_VIDEO
289
if (rx_novideo.indexIn(line) > -1) {
291
emit receivedNoVideo();
292
//emit mplayerFullyLoaded();
298
if (rx_paused.indexIn(line) > -1) {
299
emit receivedPause();
303
if (rx_stream_title.indexIn(line) > -1) {
304
QString s = rx_stream_title.cap(1);
305
QString url = rx_stream_title.cap(2);
306
qDebug("MplayerProcess::parseLine: stream_title: '%s'", s.toUtf8().data());
307
qDebug("MplayerProcess::parseLine: stream_url: '%s'", url.toUtf8().data());
310
emit receivedStreamTitleAndUrl( s, url );
313
#if NOTIFY_SUB_CHANGES
315
if ((rx_subtitle.indexIn(line) > -1) || (rx_sid.indexIn(line) > -1) || (rx_subtitle_file.indexIn(line) > -1)) {
316
int r = subs.parse(line);
317
subtitle_info_received = true;
318
subtitle_info_changed = ((r == SubTracks::SubtitleAdded) || (r == SubTracks::SubtitleChanged));
322
#if NOTIFY_AUDIO_CHANGES
324
if (rx_audio.indexIn(line) > -1) {
325
int ID = rx_audio.cap(1).toInt();
326
qDebug("MplayerProcess::parseLine: ID_AUDIO_ID: %d", ID);
327
if (audios.find(ID) == -1) audio_info_changed = true;
331
if (rx_audio_info.indexIn(line) > -1) {
332
int ID = rx_audio_info.cap(1).toInt();
333
QString lang = rx_audio_info.cap(3);
334
QString t = rx_audio_info.cap(2);
335
qDebug("MplayerProcess::parseLine: Audio: ID: %d, Lang: '%s' Type: '%s'",
336
ID, lang.toUtf8().data(), t.toUtf8().data());
338
int idx = audios.find(ID);
340
qDebug("MplayerProcess::parseLine: audio %d doesn't exist, adding it", ID);
342
audio_info_changed = true;
344
audios.addName(ID, lang);
346
audios.addLang(ID, lang);
348
qDebug("MplayerProcess::parseLine: audio %d exists, modifing it", ID);
351
//qDebug("MplayerProcess::parseLine: name of audio %d: %s", ID, audios.itemAt(idx).name().toUtf8().constData());
352
if (audios.itemAt(idx).name() != lang) {
353
audio_info_changed = true;
354
audios.addName(ID, lang);
357
//qDebug("MplayerProcess::parseLine: language of audio %d: %s", ID, audios.itemAt(idx).lang().toUtf8().constData());
358
if (audios.itemAt(idx).lang() != lang) {
359
audio_info_changed = true;
360
audios.addLang(ID, lang);
367
// The following things are not sent when the file has started to play
368
// (or if sent, smplayer will ignore anyway...)
369
// So not process anymore, if video is playing to save some time
370
if (notified_mplayer_is_running) {
374
if ( (mplayer_svn == -1) && (line.startsWith("MPlayer ")) ) {
375
mplayer_svn = MplayerVersion::mplayerVersion(line);
376
qDebug("MplayerProcess::parseLine: MPlayer SVN: %d", mplayer_svn);
377
if (mplayer_svn <= 0) {
378
qWarning("MplayerProcess::parseLine: couldn't parse mplayer version!");
379
emit failedToParseMplayerVersion(line);
383
#if !NOTIFY_SUB_CHANGES
385
if (rx_subtitle.indexIn(line) > -1) {
389
if (rx_sid.indexIn(line) > -1) {
393
if (rx_subtitle_file.indexIn(line) > -1) {
398
if (rx_ao.indexIn(line) > -1) {
399
emit receivedAO( rx_ao.cap(1) );
403
#if !NOTIFY_AUDIO_CHANGES
405
if (rx_audio_mat.indexIn(line) > -1) {
406
int ID = rx_audio_mat.cap(1).toInt();
407
QString lang = rx_audio_mat.cap(3);
408
QString t = rx_audio_mat.cap(2);
409
qDebug("MplayerProcess::parseLine: Audio: ID: %d, Lang: '%s' Type: '%s'",
410
ID, lang.toUtf8().data(), t.toUtf8().data());
413
md.audios.addName(ID, lang);
415
md.audios.addLang(ID, lang);
421
if (rx_video.indexIn(line) > -1) {
422
int ID = rx_video.cap(1).toInt();
423
QString lang = rx_video.cap(3);
424
QString t = rx_video.cap(2);
425
qDebug("MplayerProcess::parseLine: Video: ID: %d, Lang: '%s' Type: '%s'",
426
ID, lang.toUtf8().data(), t.toUtf8().data());
429
md.videos.addName(ID, lang);
431
md.videos.addLang(ID, lang);
435
// Matroshka chapters
436
if (rx_mkvchapters.indexIn(line)!=-1) {
437
int c = rx_mkvchapters.cap(1).toInt();
438
qDebug("MplayerProcess::parseLine: mkv chapters: %d", c);
439
#if GENERIC_CHAPTER_SUPPORT
440
if ((c+1) > md.chapters) {
442
qDebug("MplayerProcess::parseLine: chapters set to: %d", md.chapters);
445
if ((c+1) > md.mkv_chapters) {
446
md.mkv_chapters = c+1;
447
qDebug("MplayerProcess::parseLine: mkv_chapters set to: %d", md.mkv_chapters);
454
if (rx_vcd.indexIn(line) > -1 ) {
455
int ID = rx_vcd.cap(1).toInt();
456
QString length = rx_vcd.cap(2);
457
//md.titles.addID( ID );
458
md.titles.addName( ID, length );
463
if (rx_cdda.indexIn(line) > -1 ) {
464
int ID = rx_cdda.cap(1).toInt();
465
QString length = rx_cdda.cap(2);
467
QRegExp r("(\\d+):(\\d+):(\\d+)");
468
if ( r.indexIn(length) > -1 ) {
469
duration = r.cap(1).toInt() * 60;
470
duration += r.cap(2).toInt();
472
md.titles.addID( ID );
474
QString name = QString::number(ID) + " (" + length + ")";
475
md.titles.addName( ID, name );
477
md.titles.addDuration( ID, duration );
482
if (rx_title.indexIn(line) > -1) {
483
int ID = rx_title.cap(1).toInt();
484
QString t = rx_title.cap(2);
487
double length = rx_title.cap(3).toDouble();
488
qDebug("MplayerProcess::parseLine: Title: ID: %d, Length: '%f'", ID, length);
489
md.titles.addDuration(ID, length);
493
int chapters = rx_title.cap(3).toInt();
494
qDebug("MplayerProcess::parseLine: Title: ID: %d, Chapters: '%d'", ID, chapters);
495
md.titles.addChapters(ID, chapters);
499
int angles = rx_title.cap(3).toInt();
500
qDebug("MplayerProcess::parseLine: Title: ID: %d, Angles: '%d'", ID, angles);
501
md.titles.addAngles(ID, angles);
506
// Catch cache messages
507
if (rx_cache.indexIn(line) > -1) {
508
emit receivedCacheMessage(line);
513
if (rx_create_index.indexIn(line) > -1) {
514
emit receivedCreatingIndex(line);
518
// Catch connecting message
519
if (rx_connecting.indexIn(line) > -1) {
520
emit receivedConnectingToMessage(line);
524
// Catch resolving message
525
if (rx_resolving.indexIn(line) > -1) {
526
emit receivedResolvingMessage(line);
530
// Aspect ratio for old versions of mplayer
531
if (rx_aspect2.indexIn(line) > -1) {
532
md.video_aspect = rx_aspect2.cap(1).toDouble();
533
qDebug("MplayerProcess::parseLine: md.video_aspect set to %f", md.video_aspect);
539
//QString::trimmed() is used for removing leading and trailing whitespaces
540
//Some .mp3 files contain tags with starting and ending whitespaces
541
//Unfortunately MPlayer gives us leading and trailing whitespaces, Winamp for example doesn't show them
544
if (rx_clip_name.indexIn(line) > -1) {
545
QString s = rx_clip_name.cap(2).trimmed();
546
qDebug("MplayerProcess::parseLine: clip_name: '%s'", s.toUtf8().data());
552
if (rx_clip_artist.indexIn(line) > -1) {
553
QString s = rx_clip_artist.cap(1).trimmed();
554
qDebug("MplayerProcess::parseLine: clip_artist: '%s'", s.toUtf8().data());
560
if (rx_clip_author.indexIn(line) > -1) {
561
QString s = rx_clip_author.cap(1).trimmed();
562
qDebug("MplayerProcess::parseLine: clip_author: '%s'", s.toUtf8().data());
568
if (rx_clip_album.indexIn(line) > -1) {
569
QString s = rx_clip_album.cap(1).trimmed();
570
qDebug("MplayerProcess::parseLine: clip_album: '%s'", s.toUtf8().data());
576
if (rx_clip_genre.indexIn(line) > -1) {
577
QString s = rx_clip_genre.cap(1).trimmed();
578
qDebug("MplayerProcess::parseLine: clip_genre: '%s'", s.toUtf8().data());
584
if (rx_clip_date.indexIn(line) > -1) {
585
QString s = rx_clip_date.cap(2).trimmed();
586
qDebug("MplayerProcess::parseLine: clip_date: '%s'", s.toUtf8().data());
592
if (rx_clip_track.indexIn(line) > -1) {
593
QString s = rx_clip_track.cap(1).trimmed();
594
qDebug("MplayerProcess::parseLine: clip_track: '%s'", s.toUtf8().data());
600
if (rx_clip_copyright.indexIn(line) > -1) {
601
QString s = rx_clip_copyright.cap(1).trimmed();
602
qDebug("MplayerProcess::parseLine: clip_copyright: '%s'", s.toUtf8().data());
603
md.clip_copyright = s;
608
if (rx_clip_comment.indexIn(line) > -1) {
609
QString s = rx_clip_comment.cap(1).trimmed();
610
qDebug("MplayerProcess::parseLine: clip_comment: '%s'", s.toUtf8().data());
616
if (rx_clip_software.indexIn(line) > -1) {
617
QString s = rx_clip_software.cap(1).trimmed();
618
qDebug("MplayerProcess::parseLine: clip_software: '%s'", s.toUtf8().data());
619
md.clip_software = s;
623
if (rx_fontcache.indexIn(line) > -1) {
624
//qDebug("MplayerProcess::parseLine: updating font cache");
625
emit receivedUpdatingFontCache();
629
// Catch starting message
631
pos = rx_play.indexIn(line);
633
emit mplayerFullyLoaded();
638
if (rx.indexIn(line) > -1) {
641
//qDebug("MplayerProcess::parseLine: tag: %s, value: %s", tag.toUtf8().data(), value.toUtf8().data());
643
#if !NOTIFY_AUDIO_CHANGES
645
if (tag == "ID_AUDIO_ID") {
646
int ID = value.toInt();
647
qDebug("MplayerProcess::parseLine: ID_AUDIO_ID: %d", ID);
648
md.audios.addID( ID );
654
if (tag == "ID_VIDEO_ID") {
655
int ID = value.toInt();
656
qDebug("MplayerProcess::parseLine: ID_VIDEO_ID: %d", ID);
657
md.videos.addID( ID );
660
if (tag == "ID_LENGTH") {
661
md.duration = value.toDouble();
662
qDebug("MplayerProcess::parseLine: md.duration set to %f", md.duration);
665
if (tag == "ID_VIDEO_WIDTH") {
666
md.video_width = value.toInt();
667
qDebug("MplayerProcess::parseLine: md.video_width set to %d", md.video_width);
670
if (tag == "ID_VIDEO_HEIGHT") {
671
md.video_height = value.toInt();
672
qDebug("MplayerProcess::parseLine: md.video_height set to %d", md.video_height);
675
if (tag == "ID_VIDEO_ASPECT") {
676
md.video_aspect = value.toDouble();
677
if ( md.video_aspect == 0.0 ) {
678
// I hope width & height are already set.
679
md.video_aspect = (double) md.video_width / md.video_height;
681
qDebug("MplayerProcess::parseLine: md.video_aspect set to %f", md.video_aspect);
684
if (tag == "ID_DVD_DISC_ID") {
686
qDebug("MplayerProcess::parseLine: md.dvd_id set to '%s'", md.dvd_id.toUtf8().data());
689
if (tag == "ID_DEMUXER") {
693
if (tag == "ID_VIDEO_FORMAT") {
694
md.video_format = value;
697
if (tag == "ID_AUDIO_FORMAT") {
698
md.audio_format = value;
701
if (tag == "ID_VIDEO_BITRATE") {
702
md.video_bitrate = value.toInt();
705
if (tag == "ID_VIDEO_FPS") {
706
md.video_fps = value;
709
if (tag == "ID_AUDIO_BITRATE") {
710
md.audio_bitrate = value.toInt();
713
if (tag == "ID_AUDIO_RATE") {
714
md.audio_rate = value.toInt();
717
if (tag == "ID_AUDIO_NCH") {
718
md.audio_nch = value.toInt();
721
if (tag == "ID_VIDEO_CODEC") {
722
md.video_codec = value;
725
if (tag == "ID_AUDIO_CODEC") {
726
md.audio_codec = value;
728
#if GENERIC_CHAPTER_SUPPORT
730
if (tag == "ID_CHAPTERS") {
731
md.chapters = value.toInt();
734
if (tag == "ID_DVD_CURRENT_TITLE") {
735
dvd_current_title = value.toInt();
742
// Called when the process is finished
743
void MplayerProcess::processFinished(int exitCode, QProcess::ExitStatus exitStatus) {
744
qDebug("MplayerProcess::processFinished: exitCode: %d, status: %d", exitCode, (int) exitStatus);
745
// Send this signal before the endoffile one, otherwise
746
// the playlist will start to play next file before all
747
// objects are notified that the process has exited.
748
emit processExited();
749
if (received_end_of_file) emit receivedEndOfFile();
752
void MplayerProcess::gotError(QProcess::ProcessError error) {
753
qDebug("MplayerProcess::gotError: %d", (int) error);
756
#include "moc_mplayerprocess.cpp"