OSDN Git Service

958fdce10235825c9beae44ee8983d8a522ade9a
[lamexp/LameXP.git] / src / Thread_Process.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Thread_Process.h"
23
24 #include "Global.h"
25 #include "Model_AudioFile.h"
26 #include "Model_Progress.h"
27 #include "Encoder_Abstract.h"
28
29 #include "Model_Settings.h"
30
31 #include <QUuid>
32 #include <QFileInfo>
33 #include <QDir>
34 #include <QMutex>
35 #include <QMutexLocker>
36
37 #include <limits.h>
38 #include <time.h>
39
40 QMutex *ProcessThread::m_mutex_genFileName = NULL;
41
42 ////////////////////////////////////////////////////////////
43 // Constructor
44 ////////////////////////////////////////////////////////////
45
46 ProcessThread::ProcessThread(const AudioFileModel &audioFile, const QString &outputDirectory, AbstractEncoder *encoder)
47 :
48         m_audioFile(audioFile),
49         m_outputDirectory(outputDirectory),
50         m_encoder(encoder),
51         m_jobId(QUuid::createUuid()),
52         m_aborted(false)
53 {
54         if(m_mutex_genFileName)
55         {
56                 m_mutex_genFileName = new QMutex;
57         }
58
59         connect(m_encoder, SIGNAL(statusUpdated(int)), this, SLOT(handleUpdate(int)), Qt::DirectConnection);
60         connect(m_encoder, SIGNAL(messageLogged(QString)), this, SLOT(handleMessage(QString)), Qt::DirectConnection);
61 }
62
63 ProcessThread::~ProcessThread(void)
64 {
65         LAMEXP_DELETE(m_encoder);
66 }
67
68 void ProcessThread::run()
69 {
70         try
71         {
72                 processFile();
73         }
74         catch(...)
75         {
76                 fflush(stdout);
77                 fflush(stderr);
78                 fprintf(stderr, "\nEXCEPTION ERROR !!!\n");
79                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
80                 TerminateProcess(GetCurrentProcess(), -1);
81         }
82 }
83
84 void ProcessThread::processFile()
85 {
86         m_aborted = false;
87
88         qDebug("Process thread %s has started.", m_jobId.toString().toLatin1().constData());
89         emit processStateInitialized(m_jobId, QFileInfo(m_audioFile.filePath()).fileName(), "Starting...", ProgressModel::JobRunning);
90
91         QString outFileName = generateOutFileName();
92         if(outFileName.isEmpty())
93         {
94                 emit processStateChanged(m_jobId, "Not found!", ProgressModel::JobFailed);
95                 emit processStateFinished(m_jobId, outFileName, false);
96                 return;
97         }
98
99         if(!m_encoder->isFormatSupported(m_audioFile.formatContainerType(), m_audioFile.formatContainerProfile(), m_audioFile.formatAudioType(), m_audioFile.formatAudioProfile(), m_audioFile.formatAudioVersion()))
100         {
101                 handleMessage(QString("The format of this file is NOT supported:\n%1\n\nContainer Format:\t%2\nAudio Format:\t%3").arg(m_audioFile.filePath(), m_audioFile.formatContainerInfo(), m_audioFile.formatAudioCompressInfo()));
102                 emit processStateChanged(m_jobId, "Unsupported!", ProgressModel::JobFailed);
103                 emit processStateFinished(m_jobId, outFileName, false);
104                 return;
105         }
106
107         bool bSuccess = m_encoder->encode(m_audioFile, outFileName, &m_aborted);
108
109         if(bSuccess)
110         {
111                 QFileInfo fileInfo(outFileName);
112                 bSuccess = fileInfo.exists() && fileInfo.isFile() && (fileInfo.size() > 0);
113         }
114
115         emit processStateChanged(m_jobId, (bSuccess ? "Done." : (m_aborted ? "Aborted!" : "Failed!")), (bSuccess ? ProgressModel::JobComplete : ProgressModel::JobFailed));
116         emit processStateFinished(m_jobId, outFileName, bSuccess);
117
118         qDebug("Process thread is done.");
119 }
120
121 ////////////////////////////////////////////////////////////
122 // SLOTS
123 ////////////////////////////////////////////////////////////
124
125 void ProcessThread::handleUpdate(int progress)
126 {
127         emit processStateChanged(m_jobId, QString("Encoding (%1%)").arg(QString::number(progress)), ProgressModel::JobRunning);
128 }
129
130 void ProcessThread::handleMessage(const QString &line)
131 {
132         emit processMessageLogged(m_jobId, line);
133 }
134
135 ////////////////////////////////////////////////////////////
136 // PRIVAE FUNCTIONS
137 ////////////////////////////////////////////////////////////
138
139 QString ProcessThread::generateOutFileName(void)
140 {
141         QMutexLocker lock(m_mutex_genFileName);
142         
143         int n = 1;
144
145         QFileInfo sourceFile(m_audioFile.filePath());
146         if(!sourceFile.exists() || !sourceFile.isFile())
147         {
148                 handleMessage(QString("The source audio file could not be found:\n%1").arg(sourceFile.absoluteFilePath()));
149                 return QString();
150         }
151
152         QFile readTest(sourceFile.canonicalFilePath());
153         if(!readTest.open(QIODevice::ReadOnly))
154         {
155                 handleMessage(QString("The source audio file could not be opened for reading:\n%1").arg(readTest.fileName()));
156                 return QString();
157         }
158         else
159         {
160                 readTest.close();
161         }
162
163         QString baseName = sourceFile.completeBaseName();
164         QDir targetDir(m_outputDirectory.isEmpty() ? sourceFile.canonicalPath() : m_outputDirectory);
165         
166         if(!targetDir.exists())
167         {
168                 targetDir.mkpath(".");
169                 if(!targetDir.exists())
170                 {
171                         handleMessage(QString("The target output directory doesn't exist and could NOT be created:\n%1").arg(targetDir.absolutePath()));
172                         return QString();
173                 }
174         }
175         
176         QFile writeTest(QString("%1/%2").arg(targetDir.canonicalPath(), QUuid::createUuid().toString()));
177         if(!writeTest.open(QIODevice::ReadWrite))
178         {
179                 handleMessage(QString("The target output directory is NOT writable:\n%1").arg(targetDir.absolutePath()));
180                 return QString();
181         }
182         else
183         {
184                 writeTest.close();
185                 writeTest.remove();
186         }
187
188         QString outFileName = QString("%1/%2.%3").arg(targetDir.canonicalPath(), baseName, m_encoder->extension());
189         while(QFileInfo(outFileName).exists())
190         {
191                 outFileName = QString("%1/%2 (%3).%4").arg(targetDir.canonicalPath(), baseName, QString::number(++n), m_encoder->extension());
192         }
193
194         QFile placeholder(outFileName);
195         if(placeholder.open(QIODevice::WriteOnly))
196         {
197                 placeholder.close();
198         }
199
200         return outFileName;
201 }
202
203 ////////////////////////////////////////////////////////////
204 // EVENTS
205 ////////////////////////////////////////////////////////////
206
207 /*NONE*/