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
|
/***************************************************************************
* Copyright (C) 2008-2009 by Ilya Kotov *
* forkotov02@hotmail.ru *
* *
* 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 <qmmp/constants.h>
#include <qmmp/buffer.h>
#include <qmmp/output.h>
#include <qmmp/recycler.h>
#include <qmmp/fileinfo.h>
#include <qmmp/decoderfactory.h>
#include <QObject>
#include <QProcess>
#include <QFile>
#include <QApplication>
#include <QAction>
#include <QMetaObject>
#include <QKeyEvent>
#include <QMenu>
#include <QRegExp>
#include <QSettings>
#include "decoder_mplayer.h"
#define MPLAYER_DEBUG
static QRegExp rx_av("^[AV]: *([0-9,:.-]+)");
static QRegExp rx_pause("^(.*)=(.*)PAUSE(.*)");
static QRegExp rx_end("^(.*)End of file(.*)");
static QRegExp rx_quit("^(.*)Quit(.*)");
static QRegExp rx_audio("^AUDIO: *([0-9,.]+) *Hz.*([0-9,.]+) *ch.*([0-9]+).* ([0-9,.]+) *kbit.*");
FileInfo *MplayerInfo::createFileInfo(const QString &path)
{
QRegExp rx_id_length("^ID_LENGTH=([0-9,.]+)*");
QStringList args;
args << "-slave";
args << "-identify";
args << "-frames";
args << "0";
args << "-vo";
args << "null";
args << "-ao";
args << "null";
args << path;
QProcess mplayer_process;
mplayer_process.start("mplayer", args);
mplayer_process.waitForFinished();
QString str = QString::fromLocal8Bit(mplayer_process.readAll()).trimmed();
FileInfo *info = new FileInfo(path);
QStringList lines = str.split("\n");
foreach(QString line, lines)
{
if (rx_id_length.indexIn(line) > -1)
info->setLength((qint64) rx_id_length.cap(1).toDouble());
}
#ifdef MPLAYER_DEBUG
qDebug(qPrintable(str));
#endif
return info;
}
QStringList MplayerInfo::filters()
{
QStringList filters;
filters << "*.avi" << "*.mpg" << "*.mpeg" << "*.divx" << "*.qt" << "*.mov" << "*.wmv" << "*.asf"
<< "*.flv" << "*.3gp";
return filters;
}
DecoderMplayer::DecoderMplayer(QObject *parent, DecoderFactory *d, const QString &url)
: Decoder(parent, d)
{
m_url = url;
m_bitrate = 0;
m_samplerate = 0;
m_channels = 0;
m_bitsPerSample = 0;
m_length = 0;
m_currentTime = 0;
m_process = new QProcess(this);
}
DecoderMplayer::~DecoderMplayer()
{
qDebug(__FUNCTION__);
m_process->close();
}
bool DecoderMplayer::initialize()
{
FileInfo *info = MplayerInfo::createFileInfo(m_url);
m_length = info->length();
delete info;
m_args.clear();
m_args << "-slave";
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
QString ao_str = settings.value("mplayer/ao","default").toString();
QString vo_str = settings.value("mplayer/vo","default").toString();
if (ao_str != "default")
m_args << "ao=" + ao_str;
if (vo_str != "default")
m_args << "vo=" + vo_str;
m_args << m_url;
connect(m_process, SIGNAL(readyReadStandardOutput()), SLOT(readStdOut()));
return TRUE;
}
qint64 DecoderMplayer::lengthInSeconds()
{
return m_length;
}
void DecoderMplayer::seek(qint64 pos)
{
if (m_process->state() == QProcess::Running)
m_process->write(QString("seek %1 \n").arg(pos - m_currentTime).toLocal8Bit ());
}
void DecoderMplayer::stop()
{
if (m_process->state() == QProcess::Running)
{
m_process->write("quit\n");
m_process->waitForFinished(1500);
}
StateHandler::instance()->dispatch(Qmmp::Stopped);
}
void DecoderMplayer::pause()
{
m_process->write("pause\n");
}
void DecoderMplayer::setEQ(double bands[10], double preamp)
{
}
void DecoderMplayer::setEQEnabled(bool on)
{
}
void DecoderMplayer::run()
{
QMetaObject::invokeMethod(this, "startMplayerProcess");
StateHandler::instance()->dispatch(Qmmp::Playing);
}
void DecoderMplayer::readStdOut()
{
QString line = QString::fromLocal8Bit(m_process->readAll ()).trimmed();
QStringList lines = line.split("\n");
foreach(line, lines)
{
if (rx_av.indexIn(line) > -1)
{
StateHandler::instance()->dispatch(Qmmp::Playing);
m_currentTime = (qint64) rx_av.cap(1).toDouble();
StateHandler::instance()->dispatch(m_currentTime,
m_length,
m_bitrate,
m_samplerate,
m_bitsPerSample,
m_channels);
}
else if (rx_pause.indexIn(line) > -1)
{
StateHandler::instance()->dispatch(Qmmp::Paused);
}
else if (rx_end.indexIn(line) > -1)
{
if (m_process->state() == QProcess::Running)
m_process->waitForFinished(1500);
finish();
}
else if (rx_quit.indexIn(line) > -1)
{
if (m_process->state() == QProcess::Running)
m_process->waitForFinished(1500);
StateHandler::instance()->dispatch(Qmmp::Stopped);
}
else if (rx_audio.indexIn(line) > -1)
{
m_samplerate = rx_audio.cap(1).toInt();
m_channels = rx_audio.cap(2).toInt();
m_bitsPerSample = rx_audio.cap(3).toDouble();
m_bitrate = rx_audio.cap(4).toDouble();
}
#ifdef MPLAYER_DEBUG
else
qDebug(qPrintable(line));
#endif
}
}
void DecoderMplayer::startMplayerProcess()
{
m_process->start ("mplayer", m_args);
}
|