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
connect( this, SIGNAL(lineAvailable(QByteArray)),
34
this, SLOT(parseLine(QByteArray)) );
36
connect( this, SIGNAL(finished(int,QProcess::ExitStatus)),
37
this, SLOT(processFinished(int,QProcess::ExitStatus)) );
39
connect( this, SIGNAL(error(QProcess::ProcessError)),
40
this, SLOT(gotError(QProcess::ProcessError)) );
42
notified_mplayer_is_running = false;
44
mplayer_svn = -1; // Not found yet
49
MplayerProcess::~MplayerProcess() {
52
bool MplayerProcess::start() {
53
init_rx(); // Update configurable regular expressions
56
notified_mplayer_is_running = false;
58
mplayer_svn = -1; // Not found yet
59
received_end_of_file = false;
61
#if NOTIFY_AUDIO_SUB_CHANGES
62
audio_tracks_changed = false;
63
subtitle_tracks_changed = false;
67
return waitForStarted();
70
void MplayerProcess::writeToStdin(QString text) {
72
//qDebug("MplayerProcess::writeToStdin");
73
write( text.toLocal8Bit() + "\n");
75
qWarning("MplayerProcess::writeToStdin: process not running");
79
static QRegExp rx_av("^[AV]: *([0-9,:.-]+)");
80
static QRegExp rx_frame("^[AV]:.* (\\d+)\\/.\\d+");// [0-9,.]+");
81
static QRegExp rx("^(.*)=(.*)");
82
static QRegExp rx_audio_mat("^ID_AID_(\\d+)_(LANG|NAME)=(.*)");
83
static QRegExp rx_title("^ID_DVD_TITLE_(\\d+)_(LENGTH|CHAPTERS|ANGLES)=(.*)");
84
static QRegExp rx_winresolution("^VO: \\[(.*)\\] (\\d+)x(\\d+) => (\\d+)x(\\d+)");
85
static QRegExp rx_ao("^AO: \\[(.*)\\]");
86
static QRegExp rx_paused("^ID_PAUSED");
87
static QRegExp rx_novideo("^Video: no video");
88
static QRegExp rx_cache("^Cache fill:.*");
89
static QRegExp rx_create_index("^Generating Index:.*");
90
static QRegExp rx_play("^Starting playback...");
91
static QRegExp rx_connecting("^Connecting to .*");
92
static QRegExp rx_resolving("^Resolving .*");
93
static QRegExp rx_screenshot("^\\*\\*\\* screenshot '(.*)'");
94
static QRegExp rx_endoffile("^Exiting... \\(End of file\\)");
95
static QRegExp rx_mkvchapters("\\[mkv\\] Chapter (\\d+) from");
96
static QRegExp rx_aspect2("^Movie-Aspect is ([0-9,.]+):1");
99
static QRegExp rx_vcd("^ID_VCD_TRACK_(\\d+)_MSF=(.*)");
102
static QRegExp rx_cdda("^ID_CDDA_TRACK_(\\d+)_MSF=(.*)");
106
static QRegExp rx_subtitle("^ID_(SUBTITLE|FILE_SUB|VOBSUB)_ID=(\\d+)");
107
static QRegExp rx_sid("^ID_(SID|VSID)_(\\d+)_(LANG|NAME)=(.*)");
108
static QRegExp rx_subtitle_file("^ID_FILE_SUB_FILENAME=(.*)");
111
static QRegExp rx_clip_name("^ (name|title): (.*)", Qt::CaseInsensitive);
112
static QRegExp rx_clip_artist("^ artist: (.*)", Qt::CaseInsensitive);
113
static QRegExp rx_clip_author("^ author: (.*)", Qt::CaseInsensitive);
114
static QRegExp rx_clip_album("^ album: (.*)", Qt::CaseInsensitive);
115
static QRegExp rx_clip_genre("^ genre: (.*)", Qt::CaseInsensitive);
116
static QRegExp rx_clip_date("^ (creation date|year): (.*)", Qt::CaseInsensitive);
117
static QRegExp rx_clip_track("^ track: (.*)", Qt::CaseInsensitive);
118
static QRegExp rx_clip_copyright("^ copyright: (.*)", Qt::CaseInsensitive);
119
static QRegExp rx_clip_comment("^ comment: (.*)", Qt::CaseInsensitive);
120
static QRegExp rx_clip_software("^ software: (.*)", Qt::CaseInsensitive);
122
static QRegExp rx_stream_title("^.* StreamTitle='(.*)';StreamUrl='(.*)';");
124
void MplayerProcess::init_rx() {
125
qDebug("MplayerProcess::init_rx");
127
if (!pref->rx_endoffile.isEmpty())
128
rx_endoffile.setPattern(pref->rx_endoffile);
130
if (!pref->rx_novideo.isEmpty())
131
rx_novideo.setPattern(pref->rx_novideo);
135
void MplayerProcess::parseLine(QByteArray ba) {
136
//qDebug("MplayerProcess::parseLine: '%s'", ba.data() );
141
#if COLOR_OUTPUT_SUPPORT
142
QString line = Helper::stripColorsTags(QString::fromLocal8Bit(ba));
144
QString line = QString::fromLocal8Bit(ba);
148
//qDebug("%s", line.toUtf8().data());
149
if (rx_av.indexIn(line) > -1) {
150
double sec = rx_av.cap(1).toDouble();
151
//qDebug("cap(1): '%s'", rx_av.cap(1).toUtf8().data() );
152
//qDebug("sec: %f", sec);
154
if (!notified_mplayer_is_running) {
155
qDebug("MplayerProcess::parseLine: starting sec: %f", sec);
156
emit receivedStartingTime(sec);
157
emit mplayerFullyLoaded();
158
notified_mplayer_is_running = true;
161
emit receivedCurrentSec( sec );
164
if (rx_frame.indexIn(line) > -1) {
165
int frame = rx_frame.cap(1).toInt();
166
//qDebug(" frame: %d", frame);
167
emit receivedCurrentFrame(frame);
171
// Emulates mplayer version in Ubuntu:
172
//if (line.startsWith("MPlayer 1.0rc1")) line = "MPlayer 2:1.0~rc1-0ubuntu13.1 (C) 2000-2006 MPlayer Team";
174
emit lineAvailable(line);
176
// Parse other things
177
qDebug("MplayerProcess::parseLine: '%s'", line.toUtf8().data() );
180
if (rx_screenshot.indexIn(line) > -1) {
181
QString shot = rx_screenshot.cap(1);
182
qDebug("MplayerProcess::parseLine: screenshot: '%s'", shot.toUtf8().data());
183
emit receivedScreenshot( shot );
188
if (rx_endoffile.indexIn(line) > -1) {
189
qDebug("MplayerProcess::parseLine: detected end of file");
191
// In case of playing VCDs or DVDs, maybe the first title
192
// is not playable, so the GUI doesn't get the info about
193
// available titles. So if we received the end of file
194
// first let's pretend the file has started so the GUI can have
196
if ( !notified_mplayer_is_running) {
197
emit mplayerFullyLoaded();
200
//emit receivedEndOfFile();
201
// Send signal once the process is finished, not now!
202
received_end_of_file = true;
207
if (rx_winresolution.indexIn(line) > -1) {
209
md.win_width = rx_winresolution.cap(4).toInt();
210
md.win_height = rx_winresolution.cap(5).toInt();
211
md.video_aspect = (double) md.win_width / md.win_height;
214
int w = rx_winresolution.cap(4).toInt();
215
int h = rx_winresolution.cap(5).toInt();
217
emit receivedVO( rx_winresolution.cap(1) );
218
emit receivedWindowResolution( w, h );
219
//emit mplayerFullyLoaded();
224
if (rx_novideo.indexIn(line) > -1) {
226
emit receivedNoVideo();
227
//emit mplayerFullyLoaded();
232
if (rx_paused.indexIn(line) > -1) {
233
emit receivedPause();
237
if (rx_stream_title.indexIn(line) > -1) {
238
QString s = rx_stream_title.cap(1);
239
QString url = rx_stream_title.cap(2);
240
qDebug("MplayerProcess::parseLine: stream_title: '%s'", s.toUtf8().data());
241
qDebug("MplayerProcess::parseLine: stream_url: '%s'", url.toUtf8().data());
244
emit receivedStreamTitleAndUrl( s, url );
247
// The following things are not sent when the file has started to play
248
// (or if sent, smplayer will ignore anyway...)
249
// So not process anymore, if video is playing to save some time
250
if (notified_mplayer_is_running) {
254
if ( (mplayer_svn == -1) && (line.startsWith("MPlayer ")) ) {
255
mplayer_svn = MplayerVersion::mplayerVersion(line);
256
qDebug("MplayerProcess::parseLine: MPlayer SVN: %d", mplayer_svn);
257
if (mplayer_svn <= 0) {
258
qWarning("MplayerProcess::parseLine: couldn't parse mplayer version!");
259
emit failedToParseMplayerVersion(line);
263
#if !NOTIFY_AUDIO_SUB_CHANGES
265
if (rx_subtitle.indexIn(line) > -1) {
266
md.subs.process(line);
269
if (rx_sid.indexIn(line) > -1) {
270
md.subs.process(line);
273
if (rx_subtitle_file.indexIn(line) > -1) {
274
md.subs.process(line);
279
if (rx_ao.indexIn(line) > -1) {
280
emit receivedAO( rx_ao.cap(1) );
285
if (rx_audio_mat.indexIn(line) > -1) {
286
int ID = rx_audio_mat.cap(1).toInt();
287
QString lang = rx_audio_mat.cap(3);
288
QString t = rx_audio_mat.cap(2);
289
qDebug("MplayerProcess::parseLine: Audio: ID: %d, Lang: '%s' Type: '%s'",
290
ID, lang.toUtf8().data(), t.toUtf8().data());
293
md.audios.addName(ID, lang);
295
md.audios.addLang(ID, lang);
299
// Matroshka chapters
300
if (rx_mkvchapters.indexIn(line)!=-1) {
301
int c = rx_mkvchapters.cap(1).toInt();
302
qDebug("MplayerProcess::parseLine: mkv chapters: %d", c);
303
if ((c+1) > md.mkv_chapters) {
304
md.mkv_chapters = c+1;
305
qDebug("MplayerProcess::parseLine: mkv_chapters set to: %d", md.mkv_chapters);
311
if (rx_vcd.indexIn(line) > -1 ) {
312
int ID = rx_vcd.cap(1).toInt();
313
QString length = rx_vcd.cap(2);
314
//md.titles.addID( ID );
315
md.titles.addName( ID, length );
320
if (rx_cdda.indexIn(line) > -1 ) {
321
int ID = rx_cdda.cap(1).toInt();
322
QString length = rx_cdda.cap(2);
324
QRegExp r("(\\d+):(\\d+):(\\d+)");
325
if ( r.indexIn(length) > -1 ) {
326
duration = r.cap(1).toInt() * 60;
327
duration += r.cap(2).toInt();
329
md.titles.addID( ID );
331
QString name = QString::number(ID) + " (" + length + ")";
332
md.titles.addName( ID, name );
334
md.titles.addDuration( ID, duration );
339
if (rx_title.indexIn(line) > -1) {
340
int ID = rx_title.cap(1).toInt();
341
QString t = rx_title.cap(2);
344
double length = rx_title.cap(3).toDouble();
345
qDebug("MplayerProcess::parseLine: Title: ID: %d, Length: '%f'", ID, length);
346
md.titles.addDuration(ID, length);
350
int chapters = rx_title.cap(3).toInt();
351
qDebug("MplayerProcess::parseLine: Title: ID: %d, Chapters: '%d'", ID, chapters);
352
md.titles.addChapters(ID, chapters);
356
int angles = rx_title.cap(3).toInt();
357
qDebug("MplayerProcess::parseLine: Title: ID: %d, Angles: '%d'", ID, angles);
358
md.titles.addAngles(ID, angles);
363
// Catch cache messages
364
if (rx_cache.indexIn(line) > -1) {
365
emit receivedCacheMessage(line);
370
if (rx_create_index.indexIn(line) > -1) {
371
emit receivedCreatingIndex(line);
375
// Catch connecting message
376
if (rx_connecting.indexIn(line) > -1) {
377
emit receivedConnectingToMessage(line);
381
// Catch resolving message
382
if (rx_resolving.indexIn(line) > -1) {
383
emit receivedResolvingMessage(line);
387
// Aspect ratio for old versions of mplayer
388
if (rx_aspect2.indexIn(line) > -1) {
389
md.video_aspect = rx_aspect2.cap(1).toDouble();
390
qDebug("MplayerProcess::parseLine: md.video_aspect set to %f", md.video_aspect);
396
//QString::trimmed() is used for removing leading and trailing whitespaces
397
//Some .mp3 files contain tags with starting and ending whitespaces
398
//Unfortunately MPlayer gives us leading and trailing whitespaces, Winamp for example doesn't show them
401
if (rx_clip_name.indexIn(line) > -1) {
402
QString s = rx_clip_name.cap(2).trimmed();
403
qDebug("MplayerProcess::parseLine: clip_name: '%s'", s.toUtf8().data());
409
if (rx_clip_artist.indexIn(line) > -1) {
410
QString s = rx_clip_artist.cap(1).trimmed();
411
qDebug("MplayerProcess::parseLine: clip_artist: '%s'", s.toUtf8().data());
417
if (rx_clip_author.indexIn(line) > -1) {
418
QString s = rx_clip_author.cap(1).trimmed();
419
qDebug("MplayerProcess::parseLine: clip_author: '%s'", s.toUtf8().data());
425
if (rx_clip_album.indexIn(line) > -1) {
426
QString s = rx_clip_album.cap(1).trimmed();
427
qDebug("MplayerProcess::parseLine: clip_album: '%s'", s.toUtf8().data());
433
if (rx_clip_genre.indexIn(line) > -1) {
434
QString s = rx_clip_genre.cap(1).trimmed();
435
qDebug("MplayerProcess::parseLine: clip_genre: '%s'", s.toUtf8().data());
441
if (rx_clip_date.indexIn(line) > -1) {
442
QString s = rx_clip_date.cap(2).trimmed();
443
qDebug("MplayerProcess::parseLine: clip_date: '%s'", s.toUtf8().data());
449
if (rx_clip_track.indexIn(line) > -1) {
450
QString s = rx_clip_track.cap(1).trimmed();
451
qDebug("MplayerProcess::parseLine: clip_track: '%s'", s.toUtf8().data());
457
if (rx_clip_copyright.indexIn(line) > -1) {
458
QString s = rx_clip_copyright.cap(1).trimmed();
459
qDebug("MplayerProcess::parseLine: clip_copyright: '%s'", s.toUtf8().data());
460
md.clip_copyright = s;
465
if (rx_clip_comment.indexIn(line) > -1) {
466
QString s = rx_clip_comment.cap(1).trimmed();
467
qDebug("MplayerProcess::parseLine: clip_comment: '%s'", s.toUtf8().data());
473
if (rx_clip_software.indexIn(line) > -1) {
474
QString s = rx_clip_software.cap(1).trimmed();
475
qDebug("MplayerProcess::parseLine: clip_software: '%s'", s.toUtf8().data());
476
md.clip_software = s;
480
// Catch starting message
482
pos = rx_play.indexIn(line);
484
emit mplayerFullyLoaded();
489
if (rx.indexIn(line) > -1) {
492
//qDebug("MplayerProcess::parseLine: tag: %s, value: %s", tag.toUtf8().data(), value.toUtf8().data());
494
#if !NOTIFY_AUDIO_SUB_CHANGES
496
if (tag == "ID_AUDIO_ID") {
497
int ID = value.toInt();
498
qDebug("MplayerProcess::parseLine: ID_AUDIO_ID: %d", ID);
499
md.audios.addID( ID );
503
if (tag == "ID_LENGTH") {
504
md.duration = value.toDouble();
505
qDebug("MplayerProcess::parseLine: md.duration set to %f", md.duration);
508
if (tag == "ID_VIDEO_WIDTH") {
509
md.video_width = value.toInt();
510
qDebug("MplayerProcess::parseLine: md.video_width set to %d", md.video_width);
513
if (tag == "ID_VIDEO_HEIGHT") {
514
md.video_height = value.toInt();
515
qDebug("MplayerProcess::parseLine: md.video_height set to %d", md.video_height);
518
if (tag == "ID_VIDEO_ASPECT") {
519
md.video_aspect = value.toDouble();
520
if ( md.video_aspect == 0.0 ) {
521
// I hope width & height are already set.
522
md.video_aspect = (double) md.video_width / md.video_height;
524
qDebug("MplayerProcess::parseLine: md.video_aspect set to %f", md.video_aspect);
527
if (tag == "ID_DVD_DISC_ID") {
529
qDebug("MplayerProcess::parseLine: md.dvd_id set to '%s'", md.dvd_id.toUtf8().data());
532
if (tag == "ID_DEMUXER") {
536
if (tag == "ID_VIDEO_FORMAT") {
537
md.video_format = value;
540
if (tag == "ID_AUDIO_FORMAT") {
541
md.audio_format = value;
544
if (tag == "ID_VIDEO_BITRATE") {
545
md.video_bitrate = value.toInt();
548
if (tag == "ID_VIDEO_FPS") {
549
md.video_fps = value;
552
if (tag == "ID_AUDIO_BITRATE") {
553
md.audio_bitrate = value.toInt();
556
if (tag == "ID_AUDIO_RATE") {
557
md.audio_rate = value.toInt();
560
if (tag == "ID_AUDIO_NCH") {
561
md.audio_nch = value.toInt();
564
if (tag == "ID_VIDEO_CODEC") {
565
md.video_codec = value;
568
if (tag == "ID_AUDIO_CODEC") {
569
md.audio_codec = value;
575
// Called when the process is finished
576
void MplayerProcess::processFinished(int exitCode, QProcess::ExitStatus exitStatus) {
577
qDebug("MplayerProcess::processFinished: exitCode: %d, status: %d", exitCode, (int) exitStatus);
578
// Send this signal before the endoffile one, otherwise
579
// the playlist will start to play next file before all
580
// objects are notified that the process has exited.
581
emit processExited();
582
if (received_end_of_file) emit receivedEndOfFile();
585
void MplayerProcess::gotError(QProcess::ProcessError error) {
586
qDebug("MplayerProcess::gotError: %d", (int) error);
589
#include "moc_mplayerprocess.cpp"