Audio probes for AudioCapture plugin.
authorLev Zelenskiy <lev.zelenskiy@nokia.com>
Mon, 30 Jul 2012 03:06:47 +0000 (13:06 +1000)
committerQt by Nokia <qt-info@nokia.com>
Mon, 30 Jul 2012 06:05:44 +0000 (08:05 +0200)
Change-Id: Iea37d455ee53c9d055af4f9ca62d8a9ea241d31f
Reviewed-by: Michael Goddard <michael.goddard@nokia.com>
Reviewed-by: Dmytro Poplavskiy <dmytro.poplavskiy@nokia.com>
src/plugins/audiocapture/audiocapture.pro
src/plugins/audiocapture/audiocaptureprobecontrol.cpp [new file with mode: 0644]
src/plugins/audiocapture/audiocaptureprobecontrol.h [new file with mode: 0644]
src/plugins/audiocapture/audiocaptureservice.cpp
src/plugins/audiocapture/audiocapturesession.cpp
src/plugins/audiocapture/audiocapturesession.h

index 9422682..6ff7676 100644 (file)
@@ -13,7 +13,8 @@ HEADERS += audioencodercontrol.h \
     audioinputselector.h \
     audiocaptureservice.h \
     audiocaptureserviceplugin.h \
-    audiocapturesession.h
+    audiocapturesession.h \
+    audiocaptureprobecontrol.h
 
 SOURCES += audioencodercontrol.cpp \
     audiocontainercontrol.cpp \
@@ -21,7 +22,8 @@ SOURCES += audioencodercontrol.cpp \
     audioinputselector.cpp \
     audiocaptureservice.cpp \
     audiocaptureserviceplugin.cpp \
-    audiocapturesession.cpp
+    audiocapturesession.cpp \
+    audiocaptureprobecontrol.cpp
 
 target.path += $$[QT_INSTALL_PLUGINS]/$${PLUGIN_TYPE}
 INSTALLS += target
diff --git a/src/plugins/audiocapture/audiocaptureprobecontrol.cpp b/src/plugins/audiocapture/audiocaptureprobecontrol.cpp
new file mode 100644 (file)
index 0000000..8e97c4a
--- /dev/null
@@ -0,0 +1,60 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "audiocaptureprobecontrol.h"
+
+AudioCaptureProbeControl::AudioCaptureProbeControl(QObject *parent):
+    QMediaAudioProbeControl(parent)
+{
+}
+
+AudioCaptureProbeControl::~AudioCaptureProbeControl()
+{
+}
+
+void AudioCaptureProbeControl::bufferProbed(const char *data, quint32 size, const QAudioFormat& format)
+{
+    if (!format.isValid())
+        return;
+
+    QAudioBuffer audioBuffer = QAudioBuffer(QByteArray::fromRawData(data, size), format);
+    QMetaObject::invokeMethod(this, "audioBufferProbed", Qt::QueuedConnection, Q_ARG(QAudioBuffer, audioBuffer));
+}
diff --git a/src/plugins/audiocapture/audiocaptureprobecontrol.h b/src/plugins/audiocapture/audiocaptureprobecontrol.h
new file mode 100644 (file)
index 0000000..2dc03fc
--- /dev/null
@@ -0,0 +1,61 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef AUDIOCAPTUREPROBECONTROL_H
+#define AUDIOCAPTUREPROBECONTROL_H
+
+#include <qmediaaudioprobecontrol.h>
+#include <QtCore/qmutex.h>
+#include <qaudiobuffer.h>
+
+QT_USE_NAMESPACE
+
+class AudioCaptureProbeControl : public QMediaAudioProbeControl
+{
+    Q_OBJECT
+public:
+    explicit AudioCaptureProbeControl(QObject *parent);
+    virtual ~AudioCaptureProbeControl();
+
+    void bufferProbed(const char *data, quint32 size, const QAudioFormat& format);
+};
+
+#endif
index e71b2e1..829e9d8 100644 (file)
@@ -45,6 +45,7 @@
 #include "audioencodercontrol.h"
 #include "audiocontainercontrol.h"
 #include "audiomediarecordercontrol.h"
+#include "audiocaptureprobecontrol.h"
 
 AudioCaptureService::AudioCaptureService(QObject *parent):
     QMediaService(parent)
@@ -79,6 +80,12 @@ QMediaControl *AudioCaptureService::requestControl(const char *name)
     if (qstrcmp(name,QMediaContainerControl_iid) == 0)
         return m_containerControl;
 
+    if (qstrcmp(name,QMediaAudioProbeControl_iid) == 0) {
+        AudioCaptureProbeControl *probe = new AudioCaptureProbeControl(this);
+        m_session->addProbe(probe);
+        return probe;
+    }
+
     return 0;
 }
 
index 9493420..8fd36d3 100644 (file)
 #include "qmediarecorder.h"
 
 #include "audiocapturesession.h"
+#include "audiocaptureprobecontrol.h"
+
+void FileProbeProxy::startProbes(const QAudioFormat &format)
+{
+    m_format = format;
+}
+
+void FileProbeProxy::stopProbes()
+{
+    m_format = QAudioFormat();
+}
+
+void FileProbeProxy::addProbe(AudioCaptureProbeControl *probe)
+{
+    QMutexLocker locker(&m_probeMutex);
+
+    if (m_probes.contains(probe))
+        return;
+
+    m_probes.append(probe);
+}
+
+void FileProbeProxy::removeProbe(AudioCaptureProbeControl *probe)
+{
+    QMutexLocker locker(&m_probeMutex);
+    m_probes.removeOne(probe);
+}
+
+qint64 FileProbeProxy::writeData(const char *data, qint64 len)
+{
+    if (m_format.isValid()) {
+        QMutexLocker locker(&m_probeMutex);
+
+        foreach (AudioCaptureProbeControl* probe, m_probes)
+            probe->bufferProbed(data, len, m_format);
+    }
+
+    return QFile::writeData(data, len);
+}
 
 AudioCaptureSession::AudioCaptureSession(QObject *parent):
     QObject(parent)
@@ -274,6 +313,7 @@ void AudioCaptureSession::record()
                 if (wavFile)
                     file.write((char*)&header,sizeof(CombinedHeader));
 
+                file.startProbes(m_format);
                 m_audioInput->start(qobject_cast<QIODevice*>(&file));
             } else {
                 emit error(1,QString("can't open source, failed"));
@@ -298,6 +338,7 @@ void AudioCaptureSession::stop()
 {
     if(m_audioInput) {
         m_audioInput->stop();
+        file.stopProbes();
         file.close();
         if (wavFile) {
             qint32 fileSize = file.size()-8;
@@ -314,6 +355,16 @@ void AudioCaptureSession::stop()
     m_state = QMediaRecorder::StoppedState;
 }
 
+void AudioCaptureSession::addProbe(AudioCaptureProbeControl *probe)
+{
+    file.addProbe(probe);
+}
+
+void AudioCaptureSession::removeProbe(AudioCaptureProbeControl *probe)
+{
+    file.removeProbe(probe);
+}
+
 void AudioCaptureSession::stateChanged(QAudio::State state)
 {
     switch(state) {
index 4d13743..d1241a8 100644 (file)
@@ -45,6 +45,7 @@
 #include <QFile>
 #include <QUrl>
 #include <QDir>
+#include <QMutex>
 
 #include "audioencodercontrol.h"
 #include "audioinputselector.h"
 
 QT_USE_NAMESPACE
 
+class AudioCaptureProbeControl;
+
+class FileProbeProxy: public QFile {
+public:
+    void startProbes(const QAudioFormat& format);
+    void stopProbes();
+    void addProbe(AudioCaptureProbeControl *probe);
+    void removeProbe(AudioCaptureProbeControl *probe);
+
+protected:
+    virtual qint64 writeData(const char *data, qint64 len);
+
+private:
+    QAudioFormat m_format;
+    QList<AudioCaptureProbeControl*> m_probes;
+    QMutex m_probeMutex;
+};
+
+
 class AudioCaptureSession : public QObject
 {
     Q_OBJECT
@@ -80,6 +100,8 @@ public:
     void record();
     void pause();
     void stop();
+    void addProbe(AudioCaptureProbeControl *probe);
+    void removeProbe(AudioCaptureProbeControl *probe);
 
 public slots:
     void setCaptureDevice(const QString &deviceName);
@@ -97,7 +119,7 @@ private:
     QDir defaultDir() const;
     QString generateFileName(const QDir &dir, const QString &ext) const;
 
-    QFile file;
+    FileProbeProxy file;
     QString m_captureDevice;
     QUrl m_sink;
     QUrl m_actualSink;