opensllayer.h 7.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

#ifndef _OPENSL_LAYER_H
#define _OPENSL_LAYER_H

#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <vector>

38
#include "../audiolayer.h"
39
#include "logger.h"
40
41


42
43
44
45
46
47
48
49
50
51
52
53
54
enum PCMType {
    SFL_PCM_BOTH = 0x0021,          /** To open both playback and capture devices */
    SFL_PCM_PLAYBACK = 0x0022,      /** To open playback device only */
    SFL_PCM_CAPTURE = 0x0023,       /** To open capture device only */
    SFL_PCM_RINGTONE = 0x0024       /** To open the ringtone device only */
};

class AudioPreference;

#include "noncopyable.h"

class OpenSLThread;

55
56
#define ANDROID_BUFFER_QUEUE_LENGTH 2U
#define BUFFER_SIZE 80U
57

58
59
60
61
62
63

/**
 * @file  OpenSLLayer.h
 * @brief Main sound class for android. Manages the data transfers between the application and the hardware.
 */

64
class OpenSLLayer : public AudioLayer {
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    public:
        /**
         * Constructor
         */
        OpenSLLayer();

        /**
         * Destructor
         */
        ~OpenSLLayer();

        /**
         * Start the capture stream and prepare the playback stream.
         * The playback starts accordingly to its threshold
         */
        virtual void startStream();

        /**
         * Stop the playback and capture streams.
         * Drops the pending frames and put the capture and playback handles to PREPARED state
         */
        virtual void stopStream();

        /**
         * Scan the sound card available for capture on the system
         * @return std::vector<std::string> The vector containing the string description of the card
         */
        virtual std::vector<std::string> getCaptureDeviceList() const;

        /**
         * Scan the sound card available for capture on the system
         * @return std::vector<std::string> The vector containing the string description of the card
         */
        virtual std::vector<std::string> getPlaybackDeviceList() const;

        void initAudioEngine();

        void shutdownAudioEngine();

        void initAudioPlayback();

        void initAudioCapture();

        void startAudioPlayback();

        void startAudioCapture();

        void stopAudioPlayback();

114
        void stopAudioCapture();
115

116
117
118
        virtual int getAudioDeviceIndex(const std::string&) const {
            return 0;
        }
119

120
121
122
        virtual std::string getAudioDeviceName(int, AudioLayer::PCMType) const {
            return "";
        }
123

124
125
    private:

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
        bool audioBufferFillWithZeros(AudioBuffer &buffer);

        /**
         * Here fill the input buffer with tone or ringtone samples
         */
        bool audioPlaybackFillWithToneOrRingtone(AudioBuffer &buffer);

        bool audioPlaybackFillWithUrgent(AudioBuffer &buffer, size_t bytesAvail);

        bool audioPlaybackFillWithVoice(AudioBuffer &buffer, size_t bytesAvail);

        /**
         * The main logic to determine what should be played is determined here
         */
        bool audioPlaybackFillBuffer(AudioBuffer &buffer);

        void audioCaptureFillBuffer(AudioBuffer &buffer);


145
146
147
        /**
         * This is the main audio playabck callback called by the OpenSL layer
         */
148
        static void audioPlaybackCallback(SLAndroidSimpleBufferQueueItf bq, void *context);
149
150
151
152

        /**
         * This is the main audio capture callback called by the OpenSL layer
         */
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
        static void audioCaptureCallback(SLAndroidSimpleBufferQueueItf bq, void *context);

        /**
         * Get the index of the audio card for capture
         * @return int The index of the card used for capture
         *                     0 for the first available card on the system, 1 ...
         */
        virtual int getIndexCapture() const {
            return indexIn_;
        }

        /**
         * Get the index of the audio card for playback
         * @return int The index of the card used for playback
         *                     0 for the first available card on the system, 1 ...
         */
        virtual int getIndexPlayback() const {
            return indexOut_;
        }

        /**
         * Get the index of the audio card for ringtone (could be differnet from playback)
         * @return int The index of the card used for ringtone
         *                 0 for the first available card on the system, 1 ...
         */
        virtual int getIndexRingtone() const {
            return indexRing_;
        }

182
183
184
185
186
187
188
        AudioBuffer &getNextPlaybackBuffer(void) {
            return playbackBufferStack_[playbackBufferIndex_];
        }

        AudioBuffer &getNextRecordBuffer(void) {
            return recordBufferStack_[recordBufferIndex_];
        }
189

190
        void incrementPlaybackIndex(void) {
191
            playbackBufferIndex_ = (playbackBufferIndex_ + 1) % NB_BUFFER_PLAYBACK_QUEUE;
192
        }
193

194
        void incrementRecordIndex(void) {
195
            recordBufferIndex_ = (recordBufferIndex_ + 1) % NB_BUFFER_CAPTURE_QUEUE;
196
        }
197

198
        void playback(SLAndroidSimpleBufferQueueItf queue);
199
        void capture(SLAndroidSimpleBufferQueueItf queue);
200
201
        friend class OpenSLThread;

202
203
204
205
        static const int NB_BUFFER_PLAYBACK_QUEUE;

        static const int NB_BUFFER_CAPTURE_QUEUE;

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
        /**
         * Number of audio cards on which capture stream has been opened
         */
        int indexIn_;

        /**
         * Number of audio cards on which playback stream has been opened
         */
        int indexOut_;

        /**
         * Number of audio cards on which ringtone stream has been opened
         */
        int indexRing_;

        NON_COPYABLE(OpenSLLayer);

        virtual void updatePreference(AudioPreference &pref, int index, PCMType type);

        OpenSLThread *audioThread_;

        bool isStarted_;

        /**
         * OpenSL standard object interface
         */
232
        SLObjectItf engineObject_;
233
234
235
236

        /**
         * OpenSL sound engine interface
         */
237
        SLEngineItf engineInterface_;
238
239
240
241

        /**
         * Output mix interface
         */
242
243
244
        SLObjectItf outputMixer_;
        SLObjectItf playerObject_;
        SLObjectItf recorderObject_;
245
246

        /**
247
         *
248
         */
249
        SLPlayItf playerInterface_;
250

251
        SLRecordItf recorderInterface_;
252
253
254
255

        /**
         * OpenSL playback buffer
         */
256
257
        SLAndroidSimpleBufferQueueItf playbackBufferQueue_;
        SLAndroidSimpleBufferQueueItf recorderBufferQueue_;
258

259
260
        int playbackBufferIndex_;
        int recordBufferIndex_;
261

262
263
        AudioBufferStack playbackBufferStack_;
        AudioBufferStack recordBufferStack_;
264
265
266
};

#endif // _OPENSL_LAYER_H_