|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtCore/qdebug.h> |
|
43 #include <QtCore/qurl.h> |
|
44 #include <QtCore/qdir.h> |
|
45 #include <qaudiodeviceinfo.h> |
|
46 |
|
47 #include "../../../src/multimedia/qmediarecorder.h" |
|
48 |
|
49 #include "audiocapturesession.h" |
|
50 |
|
51 AudioCaptureSession::AudioCaptureSession(QObject *parent): |
|
52 QObject(parent) |
|
53 { |
|
54 m_deviceInfo = new QAudioDeviceInfo(QAudioDeviceInfo::defaultInputDevice()); |
|
55 m_audioInput = 0; |
|
56 m_position = 0; |
|
57 m_state = QMediaRecorder::StoppedState; |
|
58 |
|
59 m_format.setFrequency(8000); |
|
60 m_format.setChannels(1); |
|
61 m_format.setSampleSize(8); |
|
62 m_format.setSampleType(QAudioFormat::UnSignedInt); |
|
63 m_format.setCodec("audio/pcm"); |
|
64 wavFile = true; |
|
65 } |
|
66 |
|
67 AudioCaptureSession::~AudioCaptureSession() |
|
68 { |
|
69 stop(); |
|
70 |
|
71 if(m_audioInput) |
|
72 delete m_audioInput; |
|
73 } |
|
74 |
|
75 QAudioDeviceInfo* AudioCaptureSession::deviceInfo() const |
|
76 { |
|
77 return m_deviceInfo; |
|
78 } |
|
79 |
|
80 QAudioFormat AudioCaptureSession::format() const |
|
81 { |
|
82 return m_format; |
|
83 } |
|
84 |
|
85 bool AudioCaptureSession::isFormatSupported(const QAudioFormat &format) const |
|
86 { |
|
87 if(m_deviceInfo) { |
|
88 if(format.codec().contains(QLatin1String("audio/x-wav"))) { |
|
89 QAudioFormat fmt = format; |
|
90 fmt.setCodec("audio/pcm"); |
|
91 return m_deviceInfo->isFormatSupported(fmt); |
|
92 } else |
|
93 return m_deviceInfo->isFormatSupported(format); |
|
94 } |
|
95 return false; |
|
96 } |
|
97 |
|
98 bool AudioCaptureSession::setFormat(const QAudioFormat &format) |
|
99 { |
|
100 if(m_deviceInfo) { |
|
101 |
|
102 QAudioFormat fmt = format; |
|
103 |
|
104 if(m_deviceInfo->isFormatSupported(fmt)) { |
|
105 m_format = fmt; |
|
106 if(m_audioInput) delete m_audioInput; |
|
107 m_audioInput = 0; |
|
108 QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput); |
|
109 for(int i=0;i<devices.size();i++) { |
|
110 if(qstrcmp(m_deviceInfo->deviceName().toLocal8Bit().constData(), |
|
111 devices.at(i).deviceName().toLocal8Bit().constData()) == 0) { |
|
112 m_audioInput = new QAudioInput(devices.at(i),m_format); |
|
113 connect(m_audioInput,SIGNAL(stateChanged(QAudio::State)),this,SLOT(stateChanged(QAudio::State))); |
|
114 connect(m_audioInput,SIGNAL(notify()),this,SLOT(notify())); |
|
115 break; |
|
116 } |
|
117 } |
|
118 } else { |
|
119 m_format = m_deviceInfo->preferredFormat(); |
|
120 qWarning()<<"failed to setFormat using preferred..."; |
|
121 } |
|
122 } |
|
123 return false; |
|
124 } |
|
125 |
|
126 QStringList AudioCaptureSession::supportedContainers() const |
|
127 { |
|
128 QStringList list; |
|
129 if(m_deviceInfo) { |
|
130 if (m_deviceInfo->supportedCodecs().size() > 0) { |
|
131 list << "audio/x-wav"; |
|
132 list << "audio/pcm"; |
|
133 } |
|
134 } |
|
135 return list; |
|
136 } |
|
137 |
|
138 QString AudioCaptureSession::containerDescription(const QString &formatMimeType) const |
|
139 { |
|
140 if(m_deviceInfo) { |
|
141 if (formatMimeType.contains(QLatin1String("audio/pcm"))) |
|
142 return QString(tr("RAW file format")); |
|
143 if (formatMimeType.contains(QLatin1String("audio/x-wav"))) |
|
144 return QString(tr("WAV file format")); |
|
145 } |
|
146 return QString(); |
|
147 } |
|
148 |
|
149 void AudioCaptureSession::setContainerMimeType(const QString &formatMimeType) |
|
150 { |
|
151 if (!formatMimeType.contains(QLatin1String("audio/x-wav")) && |
|
152 !formatMimeType.contains(QLatin1String("audio/pcm")) && |
|
153 !formatMimeType.isEmpty()) |
|
154 return; |
|
155 |
|
156 if(m_deviceInfo) { |
|
157 if (!m_deviceInfo->supportedCodecs().contains(QLatin1String("audio/pcm"))) |
|
158 return; |
|
159 |
|
160 if (formatMimeType.isEmpty() || formatMimeType.contains(QLatin1String("audio/x-wav"))) { |
|
161 wavFile = true; |
|
162 m_format.setCodec("audio/pcm"); |
|
163 } else { |
|
164 wavFile = false; |
|
165 m_format.setCodec(formatMimeType); |
|
166 } |
|
167 } |
|
168 } |
|
169 |
|
170 QString AudioCaptureSession::containerMimeType() const |
|
171 { |
|
172 if(wavFile) |
|
173 return QString("audio/x-wav"); |
|
174 |
|
175 return QString("audio/pcm"); |
|
176 } |
|
177 |
|
178 QUrl AudioCaptureSession::outputLocation() const |
|
179 { |
|
180 return m_actualSink; |
|
181 } |
|
182 |
|
183 bool AudioCaptureSession::setOutputLocation(const QUrl& sink) |
|
184 { |
|
185 m_sink = m_actualSink = sink; |
|
186 return true; |
|
187 } |
|
188 |
|
189 qint64 AudioCaptureSession::position() const |
|
190 { |
|
191 return m_position; |
|
192 } |
|
193 |
|
194 int AudioCaptureSession::state() const |
|
195 { |
|
196 return int(m_state); |
|
197 } |
|
198 |
|
199 QDir AudioCaptureSession::defaultDir() const |
|
200 { |
|
201 QStringList dirCandidates; |
|
202 |
|
203 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) |
|
204 dirCandidates << QLatin1String("/home/user/MyDocs"); |
|
205 #endif |
|
206 |
|
207 dirCandidates << QDir::home().filePath("Documents"); |
|
208 dirCandidates << QDir::home().filePath("My Documents"); |
|
209 dirCandidates << QDir::homePath(); |
|
210 dirCandidates << QDir::currentPath(); |
|
211 dirCandidates << QDir::tempPath(); |
|
212 |
|
213 foreach (const QString &path, dirCandidates) { |
|
214 QDir dir(path); |
|
215 if (dir.exists() && QFileInfo(path).isWritable()) |
|
216 return dir; |
|
217 } |
|
218 |
|
219 return QDir(); |
|
220 } |
|
221 |
|
222 QString AudioCaptureSession::generateFileName(const QDir &dir, const QString &ext) const |
|
223 { |
|
224 |
|
225 int lastClip = 0; |
|
226 foreach(QString fileName, dir.entryList(QStringList() << QString("clip_*.%1").arg(ext))) { |
|
227 int imgNumber = fileName.mid(5, fileName.size()-6-ext.length()).toInt(); |
|
228 lastClip = qMax(lastClip, imgNumber); |
|
229 } |
|
230 |
|
231 QString name = QString("clip_%1.%2").arg(lastClip+1, |
|
232 4, //fieldWidth |
|
233 10, |
|
234 QLatin1Char('0')).arg(ext); |
|
235 |
|
236 return dir.absoluteFilePath(name); |
|
237 } |
|
238 |
|
239 void AudioCaptureSession::record() |
|
240 { |
|
241 if(!m_audioInput) { |
|
242 setFormat(m_format); |
|
243 } |
|
244 |
|
245 m_actualSink = m_sink; |
|
246 |
|
247 if (m_actualSink.isEmpty()) { |
|
248 QString ext = wavFile ? QLatin1String("wav") : QLatin1String("raw"); |
|
249 m_actualSink = generateFileName(defaultDir(), ext); |
|
250 } |
|
251 |
|
252 if(m_actualSink.toLocalFile().length() > 0) |
|
253 file.setFileName(m_actualSink.toLocalFile()); |
|
254 else |
|
255 file.setFileName(m_actualSink.toString()); |
|
256 |
|
257 if(m_audioInput) { |
|
258 if(m_state == QMediaRecorder::StoppedState) { |
|
259 if(file.open(QIODevice::WriteOnly)) { |
|
260 memset(&header,0,sizeof(CombinedHeader)); |
|
261 memcpy(header.riff.descriptor.id,"RIFF",4); |
|
262 header.riff.descriptor.size = 0xFFFFFFFF; // This should be updated on stop(), filesize-8 |
|
263 memcpy(header.riff.type,"WAVE",4); |
|
264 memcpy(header.wave.descriptor.id,"fmt ",4); |
|
265 header.wave.descriptor.size = 16; |
|
266 header.wave.audioFormat = 1; // for PCM data |
|
267 header.wave.numChannels = m_format.channels(); |
|
268 header.wave.sampleRate = m_format.frequency(); |
|
269 header.wave.byteRate = m_format.frequency()*m_format.channels()*m_format.sampleSize()/8; |
|
270 header.wave.blockAlign = m_format.channels()*m_format.sampleSize()/8; |
|
271 header.wave.bitsPerSample = m_format.sampleSize(); |
|
272 memcpy(header.data.descriptor.id,"data",4); |
|
273 header.data.descriptor.size = 0xFFFFFFFF; // This should be updated on stop(),samples*channels*sampleSize/8 |
|
274 if (wavFile) |
|
275 file.write((char*)&header,sizeof(CombinedHeader)); |
|
276 |
|
277 m_audioInput->start(qobject_cast<QIODevice*>(&file)); |
|
278 } else { |
|
279 emit error(1,QString("can't open source, failed")); |
|
280 m_state = QMediaRecorder::StoppedState; |
|
281 emit stateChanged(m_state); |
|
282 } |
|
283 } |
|
284 } |
|
285 |
|
286 m_state = QMediaRecorder::RecordingState; |
|
287 } |
|
288 |
|
289 void AudioCaptureSession::pause() |
|
290 { |
|
291 if(m_audioInput) |
|
292 m_audioInput->stop(); |
|
293 |
|
294 m_state = QMediaRecorder::PausedState; |
|
295 } |
|
296 |
|
297 void AudioCaptureSession::stop() |
|
298 { |
|
299 if(m_audioInput) { |
|
300 m_audioInput->stop(); |
|
301 file.close(); |
|
302 if (wavFile) { |
|
303 qint32 fileSize = file.size()-8; |
|
304 file.open(QIODevice::ReadWrite | QIODevice::Unbuffered); |
|
305 file.read((char*)&header,sizeof(CombinedHeader)); |
|
306 header.riff.descriptor.size = fileSize; // filesize-8 |
|
307 header.data.descriptor.size = fileSize-44; // samples*channels*sampleSize/8 |
|
308 file.seek(0); |
|
309 file.write((char*)&header,sizeof(CombinedHeader)); |
|
310 file.close(); |
|
311 } |
|
312 m_position = 0; |
|
313 } |
|
314 m_state = QMediaRecorder::StoppedState; |
|
315 } |
|
316 |
|
317 void AudioCaptureSession::stateChanged(QAudio::State state) |
|
318 { |
|
319 switch(state) { |
|
320 case QAudio::ActiveState: |
|
321 emit stateChanged(QMediaRecorder::RecordingState); |
|
322 break; |
|
323 default: |
|
324 if(!((m_state == QMediaRecorder::PausedState)||(m_state == QMediaRecorder::StoppedState))) |
|
325 m_state = QMediaRecorder::StoppedState; |
|
326 |
|
327 emit stateChanged(m_state); |
|
328 break; |
|
329 } |
|
330 } |
|
331 |
|
332 void AudioCaptureSession::notify() |
|
333 { |
|
334 m_position += m_audioInput->notifyInterval(); |
|
335 emit positionChanged(m_position); |
|
336 } |
|
337 |
|
338 void AudioCaptureSession::setCaptureDevice(const QString &deviceName) |
|
339 { |
|
340 m_captureDevice = deviceName; |
|
341 if(m_deviceInfo) |
|
342 delete m_deviceInfo; |
|
343 |
|
344 m_deviceInfo = 0; |
|
345 |
|
346 QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput); |
|
347 for(int i = 0; i < devices.size(); i++) { |
|
348 if(qstrcmp(m_captureDevice.toLocal8Bit().constData(), |
|
349 devices.at(i).deviceName().toLocal8Bit().constData())==0){ |
|
350 m_deviceInfo = new QAudioDeviceInfo(QAudioDeviceInfo::defaultInputDevice()); |
|
351 return; |
|
352 } |
|
353 } |
|
354 m_deviceInfo = new QAudioDeviceInfo(QAudioDeviceInfo::defaultInputDevice()); |
|
355 } |
|
356 |
|
357 |
|
358 |