1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qaudiorecorder.h" |
41 | #include "qaudioinputselectorcontrol.h" |
42 | #include "qmediaobject_p.h" |
43 | #include "qmediarecorder_p.h" |
44 | #include <qmediaservice.h> |
45 | #include <qmediaserviceprovider_p.h> |
46 | |
47 | #include <QtCore/qdebug.h> |
48 | #include <QtCore/qurl.h> |
49 | #include <QtCore/qstringlist.h> |
50 | #include <QtCore/qmetaobject.h> |
51 | |
52 | #include <qaudioformat.h> |
53 | |
54 | QT_BEGIN_NAMESPACE |
55 | |
56 | /*! |
57 | \class QAudioRecorder |
58 | \inmodule QtMultimedia |
59 | \ingroup multimedia |
60 | \ingroup multimedia_recording |
61 | |
62 | \brief The QAudioRecorder class is used for the recording of audio. |
63 | |
64 | The QAudioRecorder class is a high level media recording class and contains |
65 | the same functionality as \l QMediaRecorder. |
66 | |
67 | \snippet multimedia-snippets/media.cpp Audio recorder |
68 | |
69 | In addition QAudioRecorder provides functionality for selecting the audio input. |
70 | |
71 | \snippet multimedia-snippets/media.cpp Audio recorder inputs |
72 | |
73 | The \l {Audio Recorder Example} shows how to use this class in more detail. |
74 | |
75 | \sa QMediaRecorder, QAudioInputSelectorControl |
76 | */ |
77 | |
78 | class QAudioRecorderObject : public QMediaObject |
79 | { |
80 | public: |
81 | QAudioRecorderObject(QObject *parent, QMediaService *service) |
82 | :QMediaObject(parent, service) |
83 | { |
84 | } |
85 | |
86 | ~QAudioRecorderObject() |
87 | { |
88 | } |
89 | }; |
90 | |
91 | class QAudioRecorderPrivate : public QMediaRecorderPrivate |
92 | { |
93 | Q_DECLARE_NON_CONST_PUBLIC(QAudioRecorder) |
94 | |
95 | public: |
96 | void initControls() |
97 | { |
98 | Q_Q(QAudioRecorder); |
99 | audioInputSelector = nullptr; |
100 | |
101 | QMediaService *service = mediaObject ? mediaObject->service() : nullptr; |
102 | |
103 | if (service != nullptr) |
104 | audioInputSelector = qobject_cast<QAudioInputSelectorControl*>(object: service->requestControl(QAudioInputSelectorControl_iid)); |
105 | |
106 | if (audioInputSelector) { |
107 | q->connect(asender: audioInputSelector, SIGNAL(activeInputChanged(QString)), |
108 | SIGNAL(audioInputChanged(QString))); |
109 | q->connect(asender: audioInputSelector, SIGNAL(availableInputsChanged()), |
110 | SIGNAL(availableAudioInputsChanged())); |
111 | } |
112 | } |
113 | |
114 | QAudioRecorderPrivate(): |
115 | QMediaRecorderPrivate(), |
116 | provider(nullptr), |
117 | audioInputSelector(nullptr) {} |
118 | |
119 | QMediaServiceProvider *provider; |
120 | QAudioInputSelectorControl *audioInputSelector; |
121 | }; |
122 | |
123 | |
124 | |
125 | /*! |
126 | Constructs an audio recorder. |
127 | The \a parent is passed to QMediaObject. |
128 | */ |
129 | |
130 | QAudioRecorder::QAudioRecorder(QObject *parent): |
131 | QMediaRecorder(*new QAudioRecorderPrivate, nullptr, parent) |
132 | { |
133 | Q_D(QAudioRecorder); |
134 | d->provider = QMediaServiceProvider::defaultServiceProvider(); |
135 | |
136 | QMediaService *service = d->provider->requestService(Q_MEDIASERVICE_AUDIOSOURCE); |
137 | setMediaObject(new QAudioRecorderObject(this, service)); |
138 | d->initControls(); |
139 | } |
140 | |
141 | /*! |
142 | Destroys an audio recorder object. |
143 | */ |
144 | |
145 | QAudioRecorder::~QAudioRecorder() |
146 | { |
147 | Q_D(QAudioRecorder); |
148 | QMediaService *service = d->mediaObject ? d->mediaObject->service() : nullptr; |
149 | QMediaObject *mediaObject = d->mediaObject; |
150 | setMediaObject(nullptr); |
151 | |
152 | if (service && d->audioInputSelector) |
153 | service->releaseControl(control: d->audioInputSelector); |
154 | |
155 | if (d->provider && service) |
156 | d->provider->releaseService(service); |
157 | |
158 | delete mediaObject; |
159 | } |
160 | |
161 | /*! |
162 | Returns a list of available audio inputs |
163 | */ |
164 | |
165 | QStringList QAudioRecorder::audioInputs() const |
166 | { |
167 | Q_D(const QAudioRecorder); |
168 | if (d->audioInputSelector) |
169 | return d->audioInputSelector->availableInputs(); |
170 | else |
171 | return QStringList(); |
172 | } |
173 | |
174 | /*! |
175 | Returns the readable translated description of the audio input device with \a name. |
176 | */ |
177 | |
178 | QString QAudioRecorder::audioInputDescription(const QString& name) const |
179 | { |
180 | Q_D(const QAudioRecorder); |
181 | |
182 | if (d->audioInputSelector) |
183 | return d->audioInputSelector->inputDescription(name); |
184 | else |
185 | return QString(); |
186 | } |
187 | |
188 | /*! |
189 | Returns the default audio input name. |
190 | */ |
191 | |
192 | QString QAudioRecorder::defaultAudioInput() const |
193 | { |
194 | Q_D(const QAudioRecorder); |
195 | |
196 | if (d->audioInputSelector) |
197 | return d->audioInputSelector->defaultInput(); |
198 | else |
199 | return QString(); |
200 | } |
201 | |
202 | /*! |
203 | \property QAudioRecorder::audioInput |
204 | \brief the active audio input name. |
205 | |
206 | */ |
207 | |
208 | /*! |
209 | Returns the active audio input name. |
210 | */ |
211 | |
212 | QString QAudioRecorder::audioInput() const |
213 | { |
214 | Q_D(const QAudioRecorder); |
215 | |
216 | if (d->audioInputSelector) |
217 | return d->audioInputSelector->activeInput(); |
218 | else |
219 | return QString(); |
220 | } |
221 | |
222 | /*! |
223 | Set the active audio input to \a name. |
224 | */ |
225 | |
226 | void QAudioRecorder::setAudioInput(const QString& name) |
227 | { |
228 | Q_D(const QAudioRecorder); |
229 | |
230 | if (d->audioInputSelector) |
231 | return d->audioInputSelector->setActiveInput(name); |
232 | } |
233 | |
234 | /*! |
235 | \fn QAudioRecorder::audioInputChanged(const QString& name) |
236 | |
237 | Signal emitted when active audio input changes to \a name. |
238 | */ |
239 | |
240 | /*! |
241 | \fn QAudioRecorder::availableAudioInputsChanged() |
242 | |
243 | Signal is emitted when the available audio inputs change. |
244 | */ |
245 | |
246 | QT_END_NAMESPACE |
247 | |
248 | #include "moc_qaudiorecorder.cpp" |
249 | |