-
Notifications
You must be signed in to change notification settings - Fork 24
/
play-audio.cpp
235 lines (200 loc) · 8.97 KB
/
play-audio.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
232
233
234
235
#include <assert.h>
#include <getopt.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
class AudioPlayer {
public:
AudioPlayer();
~AudioPlayer();
void play(char const* uri);
/**
* This allows setting the stream type (default:SL_ANDROID_STREAM_MEDIA):
* SL_ANDROID_STREAM_ALARM - same as android.media.AudioManager.STREAM_ALARM
* SL_ANDROID_STREAM_MEDIA - same as android.media.AudioManager.STREAM_MUSIC
* SL_ANDROID_STREAM_NOTIFICATION - same as android.media.AudioManager.STREAM_NOTIFICATION
* SL_ANDROID_STREAM_RING - same as android.media.AudioManager.STREAM_RING
* SL_ANDROID_STREAM_SYSTEM - same as android.media.AudioManager.STREAM_SYSTEM
* SL_ANDROID_STREAM_VOICE - same as android.media.AudioManager.STREAM_VOICE_CALL
*/
void setStreamType(SLint32 streamType) { this->androidStreamType = streamType; }
private:
SLObjectItf mSlEngineObject{NULL};
SLEngineItf mSlEngineInterface{NULL};
SLObjectItf mSlOutputMixObject{NULL};
SLint32 androidStreamType{SL_ANDROID_STREAM_MEDIA};
};
class MutexWithCondition {
public:
MutexWithCondition() {
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&condition, NULL);
pthread_mutex_lock(&mutex);
}
~MutexWithCondition() { pthread_mutex_unlock(&mutex); }
void waitFor() { while (!occurred) pthread_cond_wait(&condition, &mutex); }
/** From waking thread. */
void lockAndSignal() {
pthread_mutex_lock(&mutex);
occurred = true;
pthread_cond_signal(&condition);
pthread_mutex_unlock(&mutex);
}
private:
volatile bool occurred{false};
pthread_mutex_t mutex;
pthread_cond_t condition;
};
AudioPlayer::AudioPlayer() {
// "OpenSL ES for Android is designed for multi-threaded applications, and is thread-safe.
// OpenSL ES for Android supports a single engine per application, and up to 32 objects.
// Available device memory and CPU may further restrict the usable number of objects.
// slCreateEngine recognizes, but ignores, these engine options: SL_ENGINEOPTION_THREADSAFE SL_ENGINEOPTION_LOSSOFCONTROL"
SLresult result = slCreateEngine(&mSlEngineObject,
/*numOptions=*/0, /*options=*/NULL,
/*numWantedInterfaces=*/0, /*wantedInterfaces=*/NULL, /*wantedInterfacesRequired=*/NULL);
assert(SL_RESULT_SUCCESS == result);
result = (*mSlEngineObject)->Realize(mSlEngineObject, SL_BOOLEAN_FALSE);
assert(SL_RESULT_SUCCESS == result);
result = (*mSlEngineObject)->GetInterface(mSlEngineObject, SL_IID_ENGINE, &mSlEngineInterface);
assert(SL_RESULT_SUCCESS == result);
SLuint32 const numWantedInterfaces = 0;
result = (*mSlEngineInterface)->CreateOutputMix(mSlEngineInterface, &mSlOutputMixObject, numWantedInterfaces, NULL, NULL);
assert(SL_RESULT_SUCCESS == result);
result = (*mSlOutputMixObject)->Realize(mSlOutputMixObject, SL_BOOLEAN_FALSE);
assert(SL_RESULT_SUCCESS == result);
}
void opensl_prefetch_callback(SLPrefetchStatusItf caller, void* pContext, SLuint32 event) {
if (event & SL_PREFETCHEVENT_STATUSCHANGE) {
SLpermille level = 0;
(*caller)->GetFillLevel(caller, &level);
if (level == 0) {
SLuint32 status;
(*caller)->GetPrefetchStatus(caller, &status);
if (status == SL_PREFETCHSTATUS_UNDERFLOW) {
// Level is 0 but we have SL_PREFETCHSTATUS_UNDERFLOW, implying an error.
printf("play-audio: underflow when prefetching data\n");
MutexWithCondition* cond = (MutexWithCondition*) pContext;
cond->lockAndSignal();
}
}
}
}
void opensl_player_callback(SLPlayItf /*caller*/, void* pContext, SLuint32 /*event*/) {
MutexWithCondition* condition = (MutexWithCondition*) pContext;
condition->lockAndSignal();
}
void AudioPlayer::play(char const* uri)
{
SLDataLocator_URI loc_uri = {SL_DATALOCATOR_URI, (SLchar *) uri};
SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED};
SLDataSource audioSrc = {&loc_uri, &format_mime};
SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, mSlOutputMixObject};
SLDataSink audioSnk = {&loc_outmix, NULL};
// SL_IID_ANDROIDCONFIGURATION is Android specific interface, SL_IID_PREFETCHSTATUS is general:
SLuint32 const numWantedInterfaces = 2;
SLInterfaceID wantedInterfaces[numWantedInterfaces]{ SL_IID_ANDROIDCONFIGURATION, SL_IID_PREFETCHSTATUS };
SLboolean wantedInterfacesRequired[numWantedInterfaces]{ SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
SLObjectItf uriPlayerObject = NULL;
SLresult result = (*mSlEngineInterface)->CreateAudioPlayer(mSlEngineInterface, &uriPlayerObject, &audioSrc, &audioSnk,
numWantedInterfaces, wantedInterfaces, wantedInterfacesRequired);
assert(SL_RESULT_SUCCESS == result);
// Android specific interface - usage:
// SLresult (*GetInterface) (SLObjectItf self, const SLInterfaceID iid, void* pInterface);
// This function gives different interfaces. One is android-specific, from
// <SLES/OpenSLES_AndroidConfiguration.h>, done before realization:
SLAndroidConfigurationItf androidConfig;
result = (*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_ANDROIDCONFIGURATION, &androidConfig);
assert(SL_RESULT_SUCCESS == result);
result = (*androidConfig)->SetConfiguration(androidConfig, SL_ANDROID_KEY_STREAM_TYPE, &this->androidStreamType, sizeof(SLint32));
assert(SL_RESULT_SUCCESS == result);
// We now Realize(). Note that the android config needs to be done before, but getting the SLPrefetchStatusItf after.
result = (*uriPlayerObject)->Realize(uriPlayerObject, /*async=*/SL_BOOLEAN_FALSE);
assert(SL_RESULT_SUCCESS == result);
SLPrefetchStatusItf prefetchInterface;
result = (*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_PREFETCHSTATUS, &prefetchInterface);
assert(SL_RESULT_SUCCESS == result);
SLPlayItf uriPlayerPlay = NULL;
result = (*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_PLAY, &uriPlayerPlay);
assert(SL_RESULT_SUCCESS == result);
if (NULL == uriPlayerPlay) {
fprintf(stderr, "Cannot play '%s'\n", uri);
} else {
result = (*uriPlayerPlay)->SetCallbackEventsMask(uriPlayerPlay, SL_PLAYEVENT_HEADSTALLED | SL_PLAYEVENT_HEADATEND);
assert(SL_RESULT_SUCCESS == result);
MutexWithCondition condition;
result = (*uriPlayerPlay)->RegisterCallback(uriPlayerPlay, opensl_player_callback, &condition);
assert(SL_RESULT_SUCCESS == result);
result = (*prefetchInterface)->RegisterCallback(prefetchInterface, opensl_prefetch_callback, &condition);
assert(SL_RESULT_SUCCESS == result);
result = (*prefetchInterface)->SetCallbackEventsMask(prefetchInterface, SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE);
// "For an audio player with URI data source, Object::Realize allocates resources but does not
// connect to the data source (i.e. "prepare") or begin pre-fetching data. These occur once the
// player state is set to either SL_PLAYSTATE_PAUSED or SL_PLAYSTATE_PLAYING."
// - http://mobilepearls.com/labs/native-android-api/ndk/docs/opensles/index.html
result = (*uriPlayerPlay)->SetPlayState(uriPlayerPlay, SL_PLAYSTATE_PLAYING);
assert(SL_RESULT_SUCCESS == result);
condition.waitFor();
}
if (uriPlayerObject != NULL) (*uriPlayerObject)->Destroy(uriPlayerObject);
}
AudioPlayer::~AudioPlayer()
{
// "Be sure to destroy all objects on exit from your application. Objects should be destroyed in reverse order of their creation,
// as it is not safe to destroy an object that has any dependent objects. For example, destroy in this order: audio players
// and recorders, output mix, then finally the engine."
if (mSlOutputMixObject != NULL) { (*mSlOutputMixObject)->Destroy(mSlOutputMixObject); mSlOutputMixObject = NULL; }
if (mSlEngineObject != NULL) { (*mSlEngineObject)->Destroy(mSlEngineObject); mSlEngineObject = NULL; }
}
int main(int argc, char** argv)
{
bool help = false;
int c;
char* streamType = NULL;
while ((c = getopt(argc, argv, "hs:")) != -1) {
switch (c) {
case 'h':
case '?': help = true; break;
case 's': streamType = optarg; break;
}
}
if (help || optind == argc) {
printf("usage: play-audio [-s streamtype] [files]\n");
return 1;
}
AudioPlayer player;
if (streamType != NULL) {
SLint32 streamTypeEnum;
if (strcmp("alarm", streamType) == 0) {
streamTypeEnum = SL_ANDROID_STREAM_ALARM;
} else if (strcmp("media", streamType) == 0) {
streamTypeEnum = SL_ANDROID_STREAM_MEDIA;
} else if (strcmp("notification", streamType) == 0) {
streamTypeEnum = SL_ANDROID_STREAM_NOTIFICATION;
} else if (strcmp("ring", streamType) == 0) {
streamTypeEnum = SL_ANDROID_STREAM_RING;
} else if (strcmp("system", streamType) == 0) {
streamTypeEnum = SL_ANDROID_STREAM_SYSTEM;
} else if (strcmp("voice", streamType) == 0) {
streamTypeEnum = SL_ANDROID_STREAM_VOICE;
} else {
fprintf(stderr, "play-audio: invalid streamtype '%s'\n", streamType);
return 1;
}
player.setStreamType(streamTypeEnum);
}
for (int i = optind; i < argc; i++) {
if (access(argv[i], R_OK) != 0) {
fprintf(stderr, "play-audio: '%s' is not a readable file\n", argv[i]);
return 1;
}
}
for (int i = optind; i < argc; i++) {
player.play(argv[i]);
}
return 0;
}