1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtMultimedia module 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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 //
43 //  W A R N I N G
44 //  -------------
45 //
46 // This file is not part of the Qt API.  It exists for the convenience
47 // of other Qt classes.  This header file may change from version to
48 // version without notice, or even be removed.
49 //
50 // We mean it.
51 //
52 
53 #ifndef QAUDIO_SYMBIAN_P_H
54 #define QAUDIO_SYMBIAN_P_H
55 
56 #include <QtCore/QList>
57 #include <QtCore/QString>
58 #include <QtMultimedia/qaudioformat.h>
59 #include <QtMultimedia/qaudio.h>
60 #include <sounddevice.h>
61 
62 QT_BEGIN_NAMESPACE
63 
64 namespace SymbianAudio {
65 
66 /**
67  * Default values used by audio input and output classes, when underlying
68  * DevSound instance has not yet been created.
69  */
70 
71 const int DefaultBufferSize = 4096; // bytes
72 const int DefaultNotifyInterval = 1000; // ms
73 
74 /**
75  * Enumeration used to track state of internal DevSound instances.
76  * Values are translated to the corresponding QAudio::State values by
77  * SymbianAudio::Utils::stateNativeToQt.
78  */
79 enum State {
80         ClosedState
81     ,   InitializingState
82     ,   ActiveState
83     ,   IdleState
84     // QAudio is suspended; DevSound is paused
85     ,   SuspendedPausedState
86     // QAudio is suspended; DevSound is stopped
87     ,   SuspendedStoppedState
88 };
89 
90 /**
91  * Wrapper around DevSound instance
92  */
93 class DevSoundWrapper
94     :   public QObject
95     ,   public MDevSoundObserver
96 {
97     Q_OBJECT
98 
99 public:
100     DevSoundWrapper(QAudio::Mode mode, QObject *parent = 0);
101     ~DevSoundWrapper();
102 
103 public:
104     // List of supported codecs; can be called once object is constructed
105     const QList<QString>& supportedCodecs() const;
106 
107     // Asynchronous initialization function; emits devsoundInitializeComplete
108     void initialize(const QString& codec);
109 
110     // Capabilities, for selected codec.  Can be called once initialize has returned
111     // successfully.
112     const QList<int>& supportedFrequencies() const;
113     const QList<int>& supportedChannels() const;
114     const QList<int>& supportedSampleSizes() const;
115     const QList<QAudioFormat::Endian>& supportedByteOrders() const;
116     const QList<QAudioFormat::SampleType>& supportedSampleTypes() const;
117 
118     bool isFormatSupported(const QAudioFormat &format) const;
119 
120     int samplesProcessed() const;
121     bool setFormat(const QAudioFormat &format);
122     bool start();
123 
124     // If DevSound implementation supports pause, calls pause and returns true.
125     // Otherwise calls stop and returns false.  In this case, all DevSound buffers
126     // currently held by the backend must be discarded.
127     bool pause();
128 
129     void resume();
130 
131     void stop();
132     void bufferProcessed();
133 
134 public:
135     // MDevSoundObserver
136     void InitializeComplete(TInt aError);
137     void ToneFinished(TInt aError);
138     void BufferToBeFilled(CMMFBuffer *aBuffer);
139     void PlayError(TInt aError);
140     void BufferToBeEmptied(CMMFBuffer *aBuffer);
141     void RecordError(TInt aError);
142     void ConvertError(TInt aError);
143     void DeviceMessage(TUid aMessageType, const TDesC8 &aMsg);
144 
145 signals:
146     void initializeComplete(int error);
147     void bufferToBeProcessed(CMMFBuffer *buffer);
148     void processingError(int error);
149 
150 private:
151     void getSupportedCodecs();
152     void populateCapabilities();
153     bool isResumeSupported() const;
154 
155 private:
156     const QAudio::Mode              m_mode;
157     TMMFState                       m_nativeMode;
158 
159     enum State {
160         StateIdle,
161         StateInitializing,
162         StateInitialized
163     }                               m_state;
164 
165     CMMFDevSound*                   m_devsound;
166     TFourCC                         m_fourcc;
167 
168     QList<QString>                  m_supportedCodecs;
169     QList<int>                      m_supportedFrequencies;
170     QList<int>                      m_supportedChannels;
171     QList<int>                      m_supportedSampleSizes;
172     QList<QAudioFormat::Endian>     m_supportedByteOrders;
173     QList<QAudioFormat::SampleType> m_supportedSampleTypes;
174 
175 };
176 
177 
178 namespace Utils {
179 
180 /**
181  * Convert internal states to QAudio states.
182  */
183 QAudio::State stateNativeToQt(State nativeState);
184 
185 /**
186  * Convert data length to number of samples.
187  */
188 qint64 bytesToSamples(const QAudioFormat &format, qint64 length);
189 
190 /**
191  * Convert number of samples to data length.
192  */
193 qint64 samplesToBytes(const QAudioFormat &format, qint64 samples);
194 
195 } // namespace Utils
196 } // namespace SymbianAudio
197 
198 QT_END_NAMESPACE
199 
200 #endif
201