summaryrefslogtreecommitdiff
path: root/src/inforeader.cpp
blob: ed5d6cb1d810c1bdb4ad106e7bd68aa07c76c281 (plain)
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
/*  smplayer, GUI front-end for mplayer.
    Copyright (C) 2006-2012 Ricardo Villalba <rvm@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "inforeader.h"
#include <QStringList>
#include <QApplication>
#include <QRegExp>

#include "colorutils.h"
#include "global.h"
#include "preferences.h"
#include "mplayerversion.h"

#if USE_QPROCESS
#include <QProcess>
#else
#include "myprocess.h"
#endif

using namespace Global;

#define NOME 0
#define VO 1
#define AO 2
#define DEMUXER 3
#define VC 4
#define AC 5

InfoReader * InfoReader::static_obj = 0;

InfoReader * InfoReader::obj() {
	if (!static_obj) {
		static_obj = new InfoReader( pref->mplayer_bin );
		static_obj->getInfo();
	}
	return static_obj;
}

InfoReader::InfoReader( QString mplayer_bin, QObject * parent )
	: QObject(parent)
{
	mplayerbin = mplayer_bin;

#if USE_QPROCESS
	proc = new QProcess(this);
	proc->setProcessChannelMode( QProcess::MergedChannels );
#else
	proc = new MyProcess(this);

	connect( proc, SIGNAL(lineAvailable(QByteArray)), 
             this, SLOT(readLine(QByteArray)) );
#endif
}

InfoReader::~InfoReader() {
}

void InfoReader::getInfo() {
	waiting_for_key = TRUE;
	vo_list.clear();
	ao_list.clear();
	demuxer_list.clear();
	mplayer_svn = -1;

	run("-identify -vo help -ao help -demuxer help -vc help -ac help");

	//list();
}

void InfoReader::list() {
	qDebug("InfoReader::list");

	InfoList::iterator it;

	qDebug(" vo_list:");
	for ( it = vo_list.begin(); it != vo_list.end(); ++it ) {
		qDebug( "driver: '%s', desc: '%s'", (*it).name().toUtf8().data(), (*it).desc().toUtf8().data());
	}

	qDebug(" ao_list:");
	for ( it = ao_list.begin(); it != ao_list.end(); ++it ) {
		qDebug( "driver: '%s', desc: '%s'", (*it).name().toUtf8().data(), (*it).desc().toUtf8().data());
	}

	qDebug(" demuxer_list:");
	for ( it = demuxer_list.begin(); it != demuxer_list.end(); ++it ) {
		qDebug( "demuxer: '%s', desc: '%s'", (*it).name().toUtf8().data(), (*it).desc().toUtf8().data());
	}

	qDebug(" vc_list:");
	for ( it = vc_list.begin(); it != vc_list.end(); ++it ) {
		qDebug( "codec: '%s', desc: '%s'", (*it).name().toUtf8().data(), (*it).desc().toUtf8().data());
	}

	qDebug(" ac_list:");
	for ( it = ac_list.begin(); it != ac_list.end(); ++it ) {
		qDebug( "codec: '%s', desc: '%s'", (*it).name().toUtf8().data(), (*it).desc().toUtf8().data());
	}

}

static QRegExp rx_vo_key("^ID_VIDEO_OUTPUTS");
static QRegExp rx_ao_key("^ID_AUDIO_OUTPUTS");
static QRegExp rx_demuxer_key("^ID_DEMUXERS");
static QRegExp rx_ac_key("^ID_AUDIO_CODECS");
static QRegExp rx_vc_key("^ID_VIDEO_CODECS");

static QRegExp rx_driver("\\t(.*)\\t(.*)");
static QRegExp rx_demuxer("^\\s+([A-Z,a-z,0-9]+)\\s+(\\d+)\\s+(\\S.*)");
static QRegExp rx_demuxer2("^\\s+([A-Z,a-z,0-9]+)\\s+(\\S.*)");
static QRegExp rx_codec("^([A-Z,a-z,0-9]+)\\s+([A-Z,a-z,0-9]+)\\s+([A-Z,a-z,0-9]+)\\s+(\\S.*)");

void InfoReader::readLine(QByteArray ba) {
#if COLOR_OUTPUT_SUPPORT
    QString line = ColorUtils::stripColorsTags(QString::fromLocal8Bit(ba));
#else
	QString line = QString::fromLocal8Bit(ba);
#endif

	if (line.isEmpty()) return;

	qDebug("InfoReader::readLine: line: '%s'", line.toUtf8().data());
	//qDebug("waiting_for_key: %d", waiting_for_key);

	if (!waiting_for_key) {
		if ((reading_type == VO) || (reading_type == AO)) {
			if ( rx_driver.indexIn(line) > -1 ) {
				QString name = rx_driver.cap(1);
				QString desc = rx_driver.cap(2);
				qDebug("InfoReader::readLine: found driver: '%s' '%s'", name.toUtf8().data(), desc.toUtf8().data());
				if (reading_type==VO) {
					vo_list.append( InfoData(name, desc) );
				} 
				else
				if (reading_type==AO) {
					ao_list.append( InfoData(name, desc) );
				}
			} else {
				qWarning("InfoReader::readLine: can't parse output driver from line '%s'", line.toUtf8().constData());
			}
		}
		else
		if (reading_type == DEMUXER) {
			if ( rx_demuxer.indexIn(line) > -1 ) {
				QString name = rx_demuxer.cap(1);
				QString desc = rx_demuxer.cap(3);
				qDebug("InfoReader::readLine: found demuxer: '%s' '%s'", name.toUtf8().data(), desc.toUtf8().data());
				demuxer_list.append( InfoData(name, desc) );
			}
			else 
			if ( rx_demuxer2.indexIn(line) > -1 ) {
				QString name = rx_demuxer2.cap(1);
				QString desc = rx_demuxer2.cap(2);
				qDebug("InfoReader::readLine: found demuxer: '%s' '%s'", name.toUtf8().data(), desc.toUtf8().data());
				demuxer_list.append( InfoData(name, desc) );
			}
			else {
				qWarning("InfoReader::readLine: can't parse demuxer from line '%s'", line.toUtf8().constData());
			}
		}
		else
		if ((reading_type == VC) || (reading_type == AC)) {
			if ( rx_codec.indexIn(line) > -1 ) {
				QString name = rx_codec.cap(1);
				QString desc = rx_codec.cap(4);
				qDebug("InfoReader::readLine: found codec: '%s' '%s'", name.toUtf8().data(), desc.toUtf8().data());
				if (reading_type==VC) {
					vc_list.append( InfoData(name, desc) );
				} 
				else
				if (reading_type==AC) {
					ac_list.append( InfoData(name, desc) );
				}
			} else {
				qWarning("InfoReader::readLine: can't parse codec from line '%s'", line.toUtf8().constData());
			}
		}
	}

	if ( rx_vo_key.indexIn(line) > -1 ) {
		reading_type = VO;
		waiting_for_key = FALSE;
		qDebug("InfoReader::readLine: found key: vo");
	}

	if ( rx_ao_key.indexIn(line) > -1 ) {
		reading_type = AO;
		waiting_for_key = FALSE;
		qDebug("InfoReader::readLine: found key: ao");
	}

	if ( rx_demuxer_key.indexIn(line) > -1 ) {
		reading_type = DEMUXER;
		waiting_for_key = FALSE;
		qDebug("InfoReader::readLine: found key: demuxer");
	}

	if ( rx_ac_key.indexIn(line) > -1 ) {
		reading_type = AC;
		waiting_for_key = FALSE;
		qDebug("InfoReader::readLine: found key: ac");
	}

	if ( rx_vc_key.indexIn(line) > -1 ) {
		reading_type = VC;
		waiting_for_key = FALSE;
		qDebug("InfoReader::readLines: found key: vc");
	}

	if (line.startsWith("MPlayer ")) {
		mplayer_svn = MplayerVersion::mplayerVersion(line);
	}
}

#if USE_QPROCESS
bool InfoReader::run(QString options) {
	qDebug("InfoReader::run: '%s'", options.toUtf8().data());
	qDebug("InfoReader::run: using QProcess");

	if (proc->state() == QProcess::Running) {
		qWarning("InfoReader::run: process already running");
		return false;
	}

	QStringList args = options.split(" ");

	proc->start(mplayerbin, args);
	if (!proc->waitForStarted()) {
		qWarning("InfoReader::run: process can't start!");
		return false;
	}

	//Wait until finish
	if (!proc->waitForFinished()) {
		qWarning("InfoReader::run: process didn't finish. Killing it...");
		proc->kill();
	}

	qDebug("InfoReader::run : terminating");

	QByteArray ba;
	while (proc->canReadLine()) {
		ba = proc->readLine();
		ba.replace("\n", "");
		ba.replace("\r", "");
		readLine( ba );
	}

	return true;
}
#else
bool InfoReader::run(QString options) {
	qDebug("InfoReader::run: '%s'", options.toUtf8().data());
	qDebug("InfoReader::run: using myprocess");

	if (proc->isRunning()) {
		qWarning("InfoReader::run: process already running");
		return false;
	}

	proc->clearArguments();

	proc->addArgument(mplayerbin);

	QStringList args = options.split(" ");
	QStringList::Iterator it = args.begin();
	while( it != args.end() ) {
		proc->addArgument( (*it) );
		++it;
	}

	proc->start();
	if (!proc->waitForStarted()) {
		qWarning("InfoReader::run: process can't start!");
		return false;
	}

	//Wait until finish
	if (!proc->waitForFinished()) {
		qWarning("InfoReader::run: process didn't finish. Killing it...");
		proc->kill();
	}

	qDebug("InfoReader::run : terminating");

	return true;
}
#endif

#include "moc_inforeader.cpp"