2
* Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
3
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
* Additional permission under GNU GPL version 3 section 7:
21
* If you modify this program, or any covered work, by linking or
22
* combining it with the OpenSSL project's OpenSSL library (or a
23
* modified version of that library), containing parts covered by the
24
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25
* grants you additional permission to convey the resulting work.
26
* Corresponding Source for a non-source form of such a combination
27
* shall include the source code for the parts of OpenSSL used as well
28
* as that of the covered work.
31
#ifndef _OPENSL_LAYER_H
32
#define _OPENSL_LAYER_H
34
#include <SLES/OpenSLES.h>
35
#include <SLES/OpenSLES_Android.h>
38
#include "audio/audiolayer.h"
43
SFL_PCM_BOTH = 0x0021, /** To open both playback and capture devices */
44
SFL_PCM_PLAYBACK = 0x0022, /** To open playback device only */
45
SFL_PCM_CAPTURE = 0x0023, /** To open capture device only */
46
SFL_PCM_RINGTONE = 0x0024 /** To open the ringtone device only */
49
class AudioPreference;
51
#include "noncopyable.h"
55
#define ANDROID_BUFFER_QUEUE_LENGTH 2U
56
#define BUFFER_SIZE 80U
58
#define MAX_NUMBER_INTERFACES 5
59
#define MAX_NUMBER_INPUT_DEVICES 3
64
* @brief Main sound class for android. Manages the data transfers between the application and the hardware.
67
class OpenSLLayer : public AudioLayer {
72
OpenSLLayer(const AudioPreference &pref);
80
* Start the capture stream and prepare the playback stream.
81
* The playback starts accordingly to its threshold
83
virtual void startStream();
86
* Stop the playback and capture streams.
87
* Drops the pending frames and put the capture and playback handles to PREPARED state
89
virtual void stopStream();
92
* Scan the sound card available for capture on the system
93
* @return std::vector<std::string> The vector containing the string description of the card
95
virtual std::vector<std::string> getCaptureDeviceList() const;
98
* Scan the sound card available for capture on the system
99
* @return std::vector<std::string> The vector containing the string description of the card
101
virtual std::vector<std::string> getPlaybackDeviceList() const;
103
void initAudioEngine();
105
void shutdownAudioEngine();
107
void initAudioPlayback();
109
void initAudioCapture();
111
void startAudioPlayback();
113
void startAudioCapture();
115
void stopAudioPlayback();
117
void stopAudioCapture();
119
virtual int getAudioDeviceIndex(const std::string&) const {
123
virtual std::string getAudioDeviceName(int, AudioLayer::PCMType) const {
128
typedef std::vector<AudioBuffer> AudioBufferStack;
131
bool audioBufferFillWithZeros(AudioBuffer &buffer);
134
* Here fill the input buffer with tone or ringtone samples
136
bool audioPlaybackFillWithToneOrRingtone(AudioBuffer &buffer);
138
bool audioPlaybackFillWithUrgent(AudioBuffer &buffer, size_t bytesAvail);
140
bool audioPlaybackFillWithVoice(AudioBuffer &buffer, size_t bytesAvail);
142
void audioCaptureFillBuffer(AudioBuffer &buffer);
146
* This is the main audio playabck callback called by the OpenSL layer
148
static void audioPlaybackCallback(SLAndroidSimpleBufferQueueItf bq, void *context);
151
* This is the main audio capture callback called by the OpenSL layer
153
static void audioCaptureCallback(SLAndroidSimpleBufferQueueItf bq, void *context);
156
* Get the index of the audio card for capture
157
* @return int The index of the card used for capture
158
* 0 for the first available card on the system, 1 ...
160
virtual int getIndexCapture() const {
165
* Get the index of the audio card for playback
166
* @return int The index of the card used for playback
167
* 0 for the first available card on the system, 1 ...
169
virtual int getIndexPlayback() const {
174
* Get the index of the audio card for ringtone (could be differnet from playback)
175
* @return int The index of the card used for ringtone
176
* 0 for the first available card on the system, 1 ...
178
virtual int getIndexRingtone() const {
182
AudioBuffer &getNextPlaybackBuffer(void) {
183
return playbackBufferStack_[playbackBufferIndex_];
186
AudioBuffer &getNextRecordBuffer(void) {
187
return recordBufferStack_[recordBufferIndex_];
190
void incrementPlaybackIndex(void) {
191
playbackBufferIndex_ = (playbackBufferIndex_ + 1) % NB_BUFFER_PLAYBACK_QUEUE;
194
void incrementRecordIndex(void) {
195
recordBufferIndex_ = (recordBufferIndex_ + 1) % NB_BUFFER_CAPTURE_QUEUE;
198
void CheckErr( SLresult res );
200
void playback(SLAndroidSimpleBufferQueueItf queue);
201
void capture(SLAndroidSimpleBufferQueueItf queue);
203
void dumpAvailableEngineInterfaces();
204
friend class OpenSLThread;
206
static const int NB_BUFFER_PLAYBACK_QUEUE;
208
static const int NB_BUFFER_CAPTURE_QUEUE;
211
* Number of audio cards on which capture stream has been opened
216
* Number of audio cards on which playback stream has been opened
221
* Number of audio cards on which ringtone stream has been opened
225
NON_COPYABLE(OpenSLLayer);
227
virtual void updatePreference(AudioPreference &pref, int index, PCMType type);
229
OpenSLThread *audioThread_;
232
* OpenSL standard object interface
234
SLObjectItf engineObject_;
237
* OpenSL sound engine interface
239
SLEngineItf engineInterface_;
242
* Output mix interface
244
SLObjectItf outputMixer_;
245
SLObjectItf playerObject_;
246
SLObjectItf recorderObject_;
249
SLOutputMixItf outputMixInterface_;
250
SLPlayItf playerInterface_;
252
SLRecordItf recorderInterface_;
254
SLAudioIODeviceCapabilitiesItf AudioIODeviceCapabilitiesItf;
255
SLAudioInputDescriptor AudioInputDescriptor;
258
* OpenSL playback buffer
260
SLAndroidSimpleBufferQueueItf playbackBufferQueue_;
261
SLAndroidSimpleBufferQueueItf recorderBufferQueue_;
263
int playbackBufferIndex_;
264
int recordBufferIndex_;
266
bool bufferIsFilled_;
268
AudioBufferStack playbackBufferStack_;
269
AudioBufferStack recordBufferStack_;
272
#endif // _OPENSL_LAYER_H_