1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <assert.h>
18 #include <pthread.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/time.h>
24
25 #include <SLES/OpenSLES.h>
26
27
28 #define MAX_NUMBER_INTERFACES 3
29
30 #define REPETITIONS 4 // 4 repetitions, but will stop the looping before the end
31
32 #define INITIAL_RATE 2000 // 2x normal playback speed
33
34 // These are extensions to OpenSL ES 1.0.1 values
35
36 #define SL_PREFETCHSTATUS_UNKNOWN 0
37 #define SL_PREFETCHSTATUS_ERROR (-1)
38
39 // Mutex and condition shared with main program to protect prefetch_status
40
41 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
42 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
43 SLuint32 prefetch_status = SL_PREFETCHSTATUS_UNKNOWN;
44
45 /* used to detect errors likely to have occured when the OpenSL ES framework fails to open
46 * a resource, for instance because a file URI is invalid, or an HTTP server doesn't respond.
47 */
48 #define PREFETCHEVENT_ERROR_CANDIDATE \
49 (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE)
50
51 //-----------------------------------------------------------------
52 //* Exits the application if an error is encountered */
53 #define CheckErr(x) ExitOnErrorFunc(x,__LINE__)
54
ExitOnErrorFunc(SLresult result,int line)55 void ExitOnErrorFunc( SLresult result , int line)
56 {
57 if (SL_RESULT_SUCCESS != result) {
58 fprintf(stderr, "%u error code encountered at line %d, exiting\n", result, line);
59 exit(EXIT_FAILURE);
60 }
61 }
62
63 //-----------------------------------------------------------------
64 /* PlayItf callback for an audio player */
PlayEventCallback(SLPlayItf caller,void * pContext,SLuint32 event)65 void PlayEventCallback( SLPlayItf caller, void *pContext, SLuint32 event)
66 {
67 fprintf(stdout, "PlayEventCallback event = ");
68 if (event & SL_PLAYEVENT_HEADATEND) {
69 fprintf(stdout, "SL_PLAYEVENT_HEADATEND \n");
70 /* slow playback down by 2x for next loop, if possible */
71 SLpermille minRate, maxRate, stepSize, rate = 1000;
72 SLuint32 capa;
73 assert(NULL != pContext);
74 SLPlaybackRateItf pRateItf = (SLPlaybackRateItf)pContext;
75 SLresult res = (*pRateItf)->GetRate(pRateItf, &rate); CheckErr(res);
76 res = (*pRateItf)->GetRateRange(pRateItf, 0, &minRate, &maxRate, &stepSize, &capa);
77 CheckErr(res);
78 fprintf(stdout, "old rate = %d, minRate=%d, maxRate=%d\n", rate, minRate, maxRate);
79 rate /= 2;
80 if (rate < minRate) {
81 rate = minRate;
82 }
83 fprintf(stdout, "new rate = %d\n", rate);
84 res = (*pRateItf)->SetRate(pRateItf, rate); CheckErr(res);
85 if (res == SL_RESULT_FEATURE_UNSUPPORTED) {
86 fprintf(stderr, "new playback rate %d per mille is unsupported\n", rate);
87 } else {
88 CheckErr(res);
89 }
90 }
91 if (event & SL_PLAYEVENT_HEADATMARKER) {
92 fprintf(stdout, "SL_PLAYEVENT_HEADATMARKER ");
93 }
94 if (event & SL_PLAYEVENT_HEADATNEWPOS) {
95 fprintf(stdout, "SL_PLAYEVENT_HEADATNEWPOS ");
96 }
97 if (event & SL_PLAYEVENT_HEADMOVING) {
98 fprintf(stdout, "SL_PLAYEVENT_HEADMOVING ");
99 }
100 if (event & SL_PLAYEVENT_HEADSTALLED) {
101 fprintf(stdout, "SL_PLAYEVENT_HEADSTALLED");
102 }
103 fprintf(stdout, "\n");
104 }
105
106 //-----------------------------------------------------------------
107 /* PrefetchStatusItf callback for an audio player */
PrefetchEventCallback(SLPrefetchStatusItf caller,void * pContext,SLuint32 event)108 void PrefetchEventCallback( SLPrefetchStatusItf caller, void *pContext, SLuint32 event)
109 {
110 //fprintf(stdout, "\t\tPrefetchEventCallback: received event %u\n", event);
111 SLresult result;
112 assert(pContext == NULL);
113 SLpermille level = 0;
114 result = (*caller)->GetFillLevel(caller, &level);
115 CheckErr(result);
116 SLuint32 status;
117 result = (*caller)->GetPrefetchStatus(caller, &status);
118 CheckErr(result);
119 if (event & SL_PREFETCHEVENT_FILLLEVELCHANGE) {
120 fprintf(stdout, "\t\tPrefetchEventCallback: Buffer fill level is = %d\n", level);
121 }
122 if (event & SL_PREFETCHEVENT_STATUSCHANGE) {
123 fprintf(stdout, "\t\tPrefetchEventCallback: Prefetch Status is = %u\n", status);
124 }
125 SLuint32 new_prefetch_status;
126 if ((event & PREFETCHEVENT_ERROR_CANDIDATE) == PREFETCHEVENT_ERROR_CANDIDATE
127 && level == 0 && status == SL_PREFETCHSTATUS_UNDERFLOW) {
128 fprintf(stdout, "\t\tPrefetchEventCallback: Error while prefetching data, exiting\n");
129 new_prefetch_status = SL_PREFETCHSTATUS_ERROR;
130 } else if (event == SL_PREFETCHEVENT_STATUSCHANGE &&
131 status == SL_PREFETCHSTATUS_SUFFICIENTDATA) {
132 new_prefetch_status = status;
133 } else {
134 return;
135 }
136 int ok;
137 ok = pthread_mutex_lock(&mutex);
138 assert(ok == 0);
139 prefetch_status = new_prefetch_status;
140 ok = pthread_cond_signal(&cond);
141 assert(ok == 0);
142 ok = pthread_mutex_unlock(&mutex);
143 assert(ok == 0);
144 }
145
146 // Display rate capabilities in a nicely formatted way
147
printCapabilities(SLuint32 capabilities)148 void printCapabilities(SLuint32 capabilities)
149 {
150 bool needBar = false;
151 printf("0x%x (", capabilities);
152 #define _(x) \
153 if (capabilities & SL_RATEPROP_##x) { \
154 if (needBar) \
155 printf("|"); \
156 printf("SL_RATEPROP_" #x); \
157 needBar = true; \
158 capabilities &= ~SL_RATEPROP_##x; \
159 }
160 _(SILENTAUDIO)
161 _(STAGGEREDAUDIO)
162 _(NOPITCHCORAUDIO)
163 _(PITCHCORAUDIO)
164 if (capabilities != 0) {
165 if (needBar)
166 printf("|");
167 printf("0x%x", capabilities);
168 needBar = true;
169 }
170 if (!needBar)
171 printf("N/A");
172 printf(")");
173 }
174
175 //-----------------------------------------------------------------
176
177 /* Play some music from a URI */
TestSlowDownUri(SLObjectItf sl,const char * path)178 void TestSlowDownUri( SLObjectItf sl, const char* path)
179 {
180 SLEngineItf EngineItf;
181
182 SLint32 numOutputs = 0;
183 SLuint32 deviceID = 0;
184
185 SLresult res;
186
187 SLDataSource audioSource;
188 SLDataLocator_URI uri;
189 SLDataFormat_MIME mime;
190
191 SLDataSink audioSink;
192 SLDataLocator_OutputMix locator_outputmix;
193
194 SLObjectItf player;
195 SLPlayItf playItf;
196 SLSeekItf seekItf;
197 SLPrefetchStatusItf prefetchItf;
198 SLPlaybackRateItf rateItf;
199
200 SLObjectItf OutputMix;
201
202 SLboolean required[MAX_NUMBER_INTERFACES];
203 SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
204
205 /* Get the SL Engine Interface which is implicit */
206 res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); CheckErr(res);
207
208 /* Initialize arrays required[] and iidArray[] */
209 for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
210 required[i] = SL_BOOLEAN_FALSE;
211 iidArray[i] = SL_IID_NULL;
212 }
213
214 required[0] = SL_BOOLEAN_TRUE;
215 iidArray[0] = SL_IID_VOLUME;
216 // Create Output Mix object to be used by player
217 res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0,
218 iidArray, required); CheckErr(res);
219
220 // Realizing the Output Mix object in synchronous mode.
221 res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE); CheckErr(res);
222
223 /* Setup the data source structure for the URI */
224 uri.locatorType = SL_DATALOCATOR_URI;
225 uri.URI = (SLchar*) path;
226 mime.formatType = SL_DATAFORMAT_MIME;
227 mime.mimeType = (SLchar*)NULL;
228 mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
229
230 audioSource.pFormat = (void *)&mime;
231 audioSource.pLocator = (void *)&uri;
232
233 /* Setup the data sink structure */
234 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
235 locator_outputmix.outputMix = OutputMix;
236 audioSink.pLocator = (void *)&locator_outputmix;
237 audioSink.pFormat = NULL;
238
239 /******************************************************/
240 /* Create the audio player */
241 required[0] = SL_BOOLEAN_TRUE;
242 iidArray[0] = SL_IID_SEEK;
243 required[1] = SL_BOOLEAN_TRUE;
244 iidArray[1] = SL_IID_PREFETCHSTATUS;
245 required[2] = SL_BOOLEAN_TRUE;
246 iidArray[2] = SL_IID_PLAYBACKRATE;
247 res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink,
248 MAX_NUMBER_INTERFACES, iidArray, required); CheckErr(res);
249
250 /* Realizing the player in synchronous mode. */
251 res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res);
252 fprintf(stdout, "URI example: after Realize\n");
253
254 /* Get interfaces */
255 res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); CheckErr(res);
256
257 res = (*player)->GetInterface(player, SL_IID_SEEK, (void*)&seekItf); CheckErr(res);
258
259 res = (*player)->GetInterface(player, SL_IID_PLAYBACKRATE, (void*)&rateItf); CheckErr(res);
260
261 res = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
262 CheckErr(res);
263 res = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, NULL);
264 CheckErr(res);
265 res = (*prefetchItf)->SetCallbackEventsMask(prefetchItf,
266 SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE); CheckErr(res);
267
268 /* Configure fill level updates every 5 percent */
269 (*prefetchItf)->SetFillUpdatePeriod(prefetchItf, 50); CheckErr(res);
270
271 /* Display duration */
272 SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
273 res = (*playItf)->GetDuration(playItf, &durationInMsec); CheckErr(res);
274 if (durationInMsec == SL_TIME_UNKNOWN) {
275 fprintf(stdout, "Content duration is unknown (before starting to prefetch)\n");
276 } else {
277 fprintf(stdout, "Content duration is %u ms (before starting to prefetch)\n",
278 durationInMsec);
279 }
280
281 /* Loop on the whole of the content */
282 res = (*seekItf)->SetLoop(seekItf, SL_BOOLEAN_TRUE, 0, SL_TIME_UNKNOWN); CheckErr(res);
283
284 /* Set up marker and position callbacks */
285 res = (*playItf)->RegisterCallback(playItf, PlayEventCallback, (void *) rateItf); CheckErr(res);
286 res = (*playItf)->SetCallbackEventsMask(playItf,
287 SL_PLAYEVENT_HEADATEND | SL_PLAYEVENT_HEADATMARKER | SL_PLAYEVENT_HEADATNEWPOS);
288 res = (*playItf)->SetMarkerPosition(playItf, 1500); CheckErr(res);
289 res = (*playItf)->SetPositionUpdatePeriod(playItf, 500); CheckErr(res);
290
291 /* Get the default rate */
292 SLpermille rate = 1234;
293 res = (*rateItf)->GetRate(rateItf, &rate); CheckErr(res);
294 printf("default rate = %d per mille\n", rate);
295 assert(1000 == rate);
296
297 /* Get the default rate properties */
298 SLuint32 properties = 0;
299 res = (*rateItf)->GetProperties(rateItf, &properties); CheckErr(res);
300 printf("default rate properties: ");
301 printCapabilities(properties);
302 printf("\n");
303 assert(SL_RATEPROP_NOPITCHCORAUDIO == properties);
304
305 /* Get all supported playback rate ranges */
306 SLuint8 index;
307 for (index = 0; ; ++index) {
308 SLpermille minRate, maxRate, stepSize;
309 SLuint32 capabilities;
310 res = (*rateItf)->GetRateRange(rateItf, index, &minRate, &maxRate, &stepSize, &capabilities);
311 if (res == SL_RESULT_PARAMETER_INVALID) {
312 if (index == 0) {
313 fprintf(stderr, "implementation supports no rate ranges\n");
314 }
315 break;
316 }
317 CheckErr(res);
318 if (index == 255) {
319 fprintf(stderr, "implementation supports way too many rate ranges, I'm giving up\n");
320 break;
321 }
322 printf("range[%u]: min=%d, max=%d, capabilities=", index, minRate, maxRate);
323 printCapabilities(capabilities);
324 printf("\n");
325 }
326
327 /* Change the playback rate before playback */
328 res = (*rateItf)->SetRate(rateItf, INITIAL_RATE);
329 if (res == SL_RESULT_FEATURE_UNSUPPORTED || res == SL_RESULT_PARAMETER_INVALID) {
330 fprintf(stderr, "initial playback rate %d per mille is unsupported\n", INITIAL_RATE);
331 } else {
332 CheckErr(res);
333 }
334
335 /******************************************************/
336 /* Play the URI */
337 /* first cause the player to prefetch the data */
338 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); CheckErr(res);
339
340 // wait for prefetch status callback to indicate either sufficient data or error
341 pthread_mutex_lock(&mutex);
342 while (prefetch_status == SL_PREFETCHSTATUS_UNKNOWN) {
343 pthread_cond_wait(&cond, &mutex);
344 }
345 pthread_mutex_unlock(&mutex);
346 if (prefetch_status == SL_PREFETCHSTATUS_ERROR) {
347 fprintf(stderr, "Error during prefetch, exiting\n");
348 goto destroyRes;
349 }
350
351 /* Display duration again, */
352 res = (*playItf)->GetDuration(playItf, &durationInMsec); CheckErr(res);
353 if (durationInMsec == SL_TIME_UNKNOWN) {
354 fprintf(stdout, "Content duration is unknown (after prefetch completed)\n");
355 } else {
356 fprintf(stdout, "Content duration is %u ms (after prefetch completed)\n", durationInMsec);
357 }
358
359 /* Start playing */
360 fprintf(stdout, "starting to play\n");
361 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING ); CheckErr(res);
362
363 /* Wait as long as the duration of the content, times the repetitions,
364 * before stopping the loop */
365 #if 1
366 usleep( (REPETITIONS-1) * durationInMsec * 1100);
367 #else
368 int ii;
369 for (ii = 0; ii < REPETITIONS; ++ii) {
370 usleep(durationInMsec * 1100);
371 PlayEventCallback(playItf, (void *) rateItf, SL_PLAYEVENT_HEADATEND);
372 }
373 #endif
374
375 res = (*seekItf)->SetLoop(seekItf, SL_BOOLEAN_FALSE, 0, SL_TIME_UNKNOWN); CheckErr(res);
376 fprintf(stdout, "As of now, stopped looping (sound shouldn't repeat from now on)\n");
377 /* wait some more to make sure it doesn't repeat */
378 usleep(durationInMsec * 1000);
379
380 /* Stop playback */
381 fprintf(stdout, "stopping playback\n");
382 res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); CheckErr(res);
383
384 destroyRes:
385
386 /* Destroy the player */
387 (*player)->Destroy(player);
388
389 /* Destroy Output Mix object */
390 (*OutputMix)->Destroy(OutputMix);
391 }
392
393 //-----------------------------------------------------------------
main(int argc,char * const argv[])394 int main(int argc, char* const argv[])
395 {
396 SLresult res;
397 SLObjectItf sl;
398
399 fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLSeekItf, SLPlaybackRateItf\n",
400 argv[0]);
401 fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n");
402 fprintf(stdout, "Plays a sound and loops it %d times while changing the \n", REPETITIONS);
403 fprintf(stdout, "playback rate each time.\n\n");
404
405 if (argc == 1) {
406 fprintf(stdout, "Usage: \n\t%s path \n\t%s url\n", argv[0], argv[0]);
407 fprintf(stdout, "Example: \"%s /sdcard/my.mp3\" or \"%s file:///sdcard/my.mp3\"\n",
408 argv[0], argv[0]);
409 return EXIT_FAILURE;
410 }
411
412 SLEngineOption EngineOption[] = {
413 {(SLuint32) SL_ENGINEOPTION_THREADSAFE,
414 (SLuint32) SL_BOOLEAN_TRUE}};
415
416 res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
417 CheckErr(res);
418 /* Realizing the SL Engine in synchronous mode. */
419 res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
420 CheckErr(res);
421
422 TestSlowDownUri(sl, argv[1]);
423
424 /* Shutdown OpenSL ES */
425 (*sl)->Destroy(sl);
426
427 return EXIT_SUCCESS;
428 }
429