|
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:BSD$ |
|
10 ** You may use this file under the terms of the BSD license as follows: |
|
11 ** |
|
12 ** "Redistribution and use in source and binary forms, with or without |
|
13 ** modification, are permitted provided that the following conditions are |
|
14 ** met: |
|
15 ** * Redistributions of source code must retain the above copyright |
|
16 ** notice, this list of conditions and the following disclaimer. |
|
17 ** * Redistributions in binary form must reproduce the above copyright |
|
18 ** notice, this list of conditions and the following disclaimer in |
|
19 ** the documentation and/or other materials provided with the |
|
20 ** distribution. |
|
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
|
22 ** the names of its contributors may be used to endorse or promote |
|
23 ** products derived from this software without specific prior written |
|
24 ** permission. |
|
25 ** |
|
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
|
37 ** $QT_END_LICENSE$ |
|
38 ** |
|
39 ****************************************************************************/ |
|
40 |
|
41 #include <QtCore/qdir.h> |
|
42 #include <QtGui/qfiledialog.h> |
|
43 |
|
44 #include <qaudiocapturesource.h> |
|
45 #include <qmediarecorder.h> |
|
46 |
|
47 #include "audiorecorder.h" |
|
48 |
|
49 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) |
|
50 #include "ui_audiorecorder_small.h" |
|
51 #else |
|
52 #include "ui_audiorecorder.h" |
|
53 #endif |
|
54 |
|
55 AudioRecorder::AudioRecorder(QWidget *parent) |
|
56 : QMainWindow(parent), |
|
57 ui(new Ui::AudioRecorder) |
|
58 { |
|
59 ui->setupUi(this); |
|
60 |
|
61 audiosource = new QAudioCaptureSource(this); |
|
62 capture = new QMediaRecorder(audiosource, this); |
|
63 |
|
64 //audio devices |
|
65 ui->audioDeviceBox->addItem(tr("Default"), QVariant(QString())); |
|
66 foreach(const QString &device, audiosource->audioInputs()) { |
|
67 ui->audioDeviceBox->addItem(device, QVariant(device)); |
|
68 } |
|
69 |
|
70 //audio codecs |
|
71 ui->audioCodecBox->addItem(tr("Default"), QVariant(QString())); |
|
72 foreach(const QString &codecName, capture->supportedAudioCodecs()) { |
|
73 ui->audioCodecBox->addItem(codecName, QVariant(codecName)); |
|
74 } |
|
75 |
|
76 //containers |
|
77 ui->containerBox->addItem(tr("Default"), QVariant(QString())); |
|
78 foreach(const QString &containerName, capture->supportedContainers()) { |
|
79 ui->containerBox->addItem(containerName, QVariant(containerName)); |
|
80 } |
|
81 |
|
82 //sample rate: |
|
83 ui->sampleRateBox->addItem(tr("Default"), QVariant(0)); |
|
84 foreach(int sampleRate, capture->supportedAudioSampleRates()) { |
|
85 ui->sampleRateBox->addItem(QString::number(sampleRate), QVariant(sampleRate)); |
|
86 } |
|
87 |
|
88 ui->qualitySlider->setRange(0, int(QtMultimediaKit::VeryHighQuality)); |
|
89 ui->qualitySlider->setValue(int(QtMultimediaKit::NormalQuality)); |
|
90 |
|
91 //bitrates: |
|
92 ui->bitrateBox->addItem(QString("Default"), QVariant(0)); |
|
93 ui->bitrateBox->addItem(QString("32000"), QVariant(32000)); |
|
94 ui->bitrateBox->addItem(QString("64000"), QVariant(64000)); |
|
95 ui->bitrateBox->addItem(QString("96000"), QVariant(96000)); |
|
96 ui->bitrateBox->addItem(QString("128000"), QVariant(128000)); |
|
97 |
|
98 connect(capture, SIGNAL(durationChanged(qint64)), this, SLOT(updateProgress(qint64))); |
|
99 connect(capture, SIGNAL(stateChanged(QMediaRecorder::State)), this, SLOT(updateState(QMediaRecorder::State))); |
|
100 connect(capture, SIGNAL(error(QMediaRecorder::Error)), this, SLOT(displayErrorMessage())); |
|
101 } |
|
102 |
|
103 AudioRecorder::~AudioRecorder() |
|
104 { |
|
105 delete capture; |
|
106 delete audiosource; |
|
107 } |
|
108 |
|
109 void AudioRecorder::updateProgress(qint64 duration) |
|
110 { |
|
111 if (capture->error() != QMediaRecorder::NoError || duration < 2000) |
|
112 return; |
|
113 |
|
114 ui->statusbar->showMessage(tr("Recorded %1 sec").arg(qRound(duration/1000))); |
|
115 } |
|
116 |
|
117 void AudioRecorder::updateState(QMediaRecorder::State state) |
|
118 { |
|
119 QString statusMessage; |
|
120 |
|
121 switch(state) { |
|
122 case QMediaRecorder::RecordingState: |
|
123 ui->recordButton->setText(tr("Stop")); |
|
124 ui->pauseButton->setText(tr("Pause")); |
|
125 if (capture->outputLocation().isEmpty()) |
|
126 statusMessage = tr("Recording"); |
|
127 else |
|
128 statusMessage = tr("Recording to %1").arg(capture->outputLocation().toString()); |
|
129 break; |
|
130 case QMediaRecorder::PausedState: |
|
131 ui->recordButton->setText(tr("Stop")); |
|
132 ui->pauseButton->setText(tr("Resume")); |
|
133 statusMessage = tr("Paused"); |
|
134 break; |
|
135 case QMediaRecorder::StoppedState: |
|
136 ui->recordButton->setText(tr("Record")); |
|
137 ui->pauseButton->setText(tr("Pause")); |
|
138 statusMessage = tr("Stopped"); |
|
139 } |
|
140 |
|
141 ui->pauseButton->setEnabled(state != QMediaRecorder::StoppedState); |
|
142 |
|
143 if (capture->error() == QMediaRecorder::NoError) |
|
144 ui->statusbar->showMessage(statusMessage); |
|
145 } |
|
146 |
|
147 static QVariant boxValue(const QComboBox *box) |
|
148 { |
|
149 int idx = box->currentIndex(); |
|
150 if (idx == -1) |
|
151 return QVariant(); |
|
152 |
|
153 return box->itemData(idx); |
|
154 } |
|
155 |
|
156 |
|
157 void AudioRecorder::toggleRecord() |
|
158 { |
|
159 if (capture->state() == QMediaRecorder::StoppedState) { |
|
160 audiosource->setAudioInput(boxValue(ui->audioDeviceBox).toString()); |
|
161 |
|
162 |
|
163 QAudioEncoderSettings settings; |
|
164 settings.setCodec(boxValue(ui->audioCodecBox).toString()); |
|
165 settings.setSampleRate(boxValue(ui->sampleRateBox).toInt()); |
|
166 settings.setBitRate(boxValue(ui->bitrateBox).toInt()); |
|
167 settings.setQuality(QtMultimediaKit::EncodingQuality(ui->qualitySlider->value())); |
|
168 settings.setEncodingMode(ui->constantQualityRadioButton->isChecked() ? |
|
169 QtMultimediaKit::ConstantQualityEncoding : |
|
170 QtMultimediaKit::ConstantBitRateEncoding); |
|
171 |
|
172 QString container = boxValue(ui->containerBox).toString(); |
|
173 |
|
174 capture->setEncodingSettings(settings, QVideoEncoderSettings(), container); |
|
175 capture->record(); |
|
176 } else { |
|
177 capture->stop(); |
|
178 } |
|
179 } |
|
180 |
|
181 void AudioRecorder::togglePause() |
|
182 { |
|
183 if (capture->state() != QMediaRecorder::PausedState) |
|
184 capture->pause(); |
|
185 else |
|
186 capture->record(); |
|
187 } |
|
188 |
|
189 void AudioRecorder::setOutputLocation() |
|
190 { |
|
191 QString fileName = QFileDialog::getSaveFileName(); |
|
192 |
|
193 capture->setOutputLocation(QUrl(fileName)); |
|
194 } |
|
195 |
|
196 void AudioRecorder::displayErrorMessage() |
|
197 { |
|
198 ui->statusbar->showMessage(capture->errorString()); |
|
199 } |