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   ((SLuint32) -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 __unused,void * pContext,SLuint32 event)65 void PlayEventCallback( SLPlayItf caller __unused, 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     SLresult                   res;
183 
184     SLDataSource               audioSource;
185     SLDataLocator_URI          uri;
186     SLDataFormat_MIME          mime;
187 
188     SLDataSink                 audioSink;
189     SLDataLocator_OutputMix    locator_outputmix;
190 
191     SLObjectItf                player;
192     SLPlayItf                  playItf;
193     SLSeekItf                  seekItf;
194     SLPrefetchStatusItf        prefetchItf;
195     SLPlaybackRateItf          rateItf;
196 
197     SLObjectItf                OutputMix;
198 
199     SLboolean required[MAX_NUMBER_INTERFACES];
200     SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
201 
202     /* Get the SL Engine Interface which is implicit */
203     res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);   CheckErr(res);
204 
205     /* Initialize arrays required[] and iidArray[] */
206     for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
207         required[i] = SL_BOOLEAN_FALSE;
208         iidArray[i] = SL_IID_NULL;
209     }
210 
211     required[0] = SL_BOOLEAN_TRUE;
212     iidArray[0] = SL_IID_VOLUME;
213     // Create Output Mix object to be used by player
214     res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0,
215             iidArray, required);  CheckErr(res);
216 
217     // Realizing the Output Mix object in synchronous mode.
218     res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);  CheckErr(res);
219 
220     /* Setup the data source structure for the URI */
221     uri.locatorType = SL_DATALOCATOR_URI;
222     uri.URI         =  (SLchar*) path;
223     mime.formatType    = SL_DATAFORMAT_MIME;
224     mime.mimeType      = (SLchar*)NULL;
225     mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
226 
227     audioSource.pFormat  = (void *)&mime;
228     audioSource.pLocator = (void *)&uri;
229 
230     /* Setup the data sink structure */
231     locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
232     locator_outputmix.outputMix   = OutputMix;
233     audioSink.pLocator            = (void *)&locator_outputmix;
234     audioSink.pFormat             = NULL;
235 
236     /******************************************************/
237     /* Create the audio player */
238     required[0] = SL_BOOLEAN_TRUE;
239     iidArray[0] = SL_IID_SEEK;
240     required[1] = SL_BOOLEAN_TRUE;
241     iidArray[1] = SL_IID_PREFETCHSTATUS;
242     required[2] = SL_BOOLEAN_TRUE;
243     iidArray[2] = SL_IID_PLAYBACKRATE;
244     res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink,
245             MAX_NUMBER_INTERFACES, iidArray, required); CheckErr(res);
246 
247     /* Realizing the player in synchronous mode. */
248     res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res);
249     fprintf(stdout, "URI example: after Realize\n");
250 
251     /* Get interfaces */
252     res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);  CheckErr(res);
253 
254     res = (*player)->GetInterface(player, SL_IID_SEEK,  (void*)&seekItf);  CheckErr(res);
255 
256     res = (*player)->GetInterface(player, SL_IID_PLAYBACKRATE, (void*)&rateItf);  CheckErr(res);
257 
258     res = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
259     CheckErr(res);
260     res = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, NULL);
261     CheckErr(res);
262     res = (*prefetchItf)->SetCallbackEventsMask(prefetchItf,
263             SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE);  CheckErr(res);
264 
265     /* Configure fill level updates every 5 percent */
266     (*prefetchItf)->SetFillUpdatePeriod(prefetchItf, 50);  CheckErr(res);
267 
268     /* Display duration */
269     SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
270     res = (*playItf)->GetDuration(playItf, &durationInMsec);  CheckErr(res);
271     if (durationInMsec == SL_TIME_UNKNOWN) {
272         fprintf(stdout, "Content duration is unknown (before starting to prefetch)\n");
273     } else {
274         fprintf(stdout, "Content duration is %u ms (before starting to prefetch)\n",
275                 durationInMsec);
276     }
277 
278     /* Loop on the whole of the content */
279     res = (*seekItf)->SetLoop(seekItf, SL_BOOLEAN_TRUE, 0, SL_TIME_UNKNOWN);  CheckErr(res);
280 
281     /* Set up marker and position callbacks */
282     res = (*playItf)->RegisterCallback(playItf, PlayEventCallback, (void *) rateItf);
283             CheckErr(res);
284     res = (*playItf)->SetCallbackEventsMask(playItf,
285             SL_PLAYEVENT_HEADATEND | SL_PLAYEVENT_HEADATMARKER | SL_PLAYEVENT_HEADATNEWPOS);
286     res = (*playItf)->SetMarkerPosition(playItf, 1500); CheckErr(res);
287     res = (*playItf)->SetPositionUpdatePeriod(playItf, 500); CheckErr(res);
288 
289     /* Get the default rate */
290     SLpermille rate = 1234;
291     res = (*rateItf)->GetRate(rateItf, &rate); CheckErr(res);
292     printf("default rate = %d per mille\n", rate);
293     assert(1000 == rate);
294 
295     /* Get the default rate properties */
296     SLuint32 properties = 0;
297     res = (*rateItf)->GetProperties(rateItf, &properties); CheckErr(res);
298     printf("default rate properties: ");
299     printCapabilities(properties);
300     printf("\n");
301     assert(SL_RATEPROP_NOPITCHCORAUDIO == properties);
302 
303     /* Get all supported playback rate ranges */
304     SLuint8 index;
305     for (index = 0; ; ++index) {
306         SLpermille minRate, maxRate, stepSize;
307         SLuint32 capabilities;
308         res = (*rateItf)->GetRateRange(rateItf, index, &minRate, &maxRate, &stepSize,
309                 &capabilities);
310         if (res == SL_RESULT_PARAMETER_INVALID) {
311             if (index == 0) {
312                 fprintf(stderr, "implementation supports no rate ranges\n");
313             }
314             break;
315         }
316         CheckErr(res);
317         if (index == 255) {
318             fprintf(stderr, "implementation supports way too many rate ranges, I'm giving up\n");
319             break;
320         }
321         printf("range[%u]: min=%d, max=%d, capabilities=", index, minRate, maxRate);
322         printCapabilities(capabilities);
323         printf("\n");
324     }
325 
326     /* Change the playback rate before playback */
327     res = (*rateItf)->SetRate(rateItf, INITIAL_RATE);
328     if (res == SL_RESULT_FEATURE_UNSUPPORTED || res == SL_RESULT_PARAMETER_INVALID) {
329         fprintf(stderr, "initial playback rate %d per mille is unsupported\n", INITIAL_RATE);
330     } else {
331         CheckErr(res);
332     }
333 
334     /******************************************************/
335     /* Play the URI */
336     /*     first cause the player to prefetch the data */
337     res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); CheckErr(res);
338 
339     // wait for prefetch status callback to indicate either sufficient data or error
340     pthread_mutex_lock(&mutex);
341     while (prefetch_status == SL_PREFETCHSTATUS_UNKNOWN) {
342         pthread_cond_wait(&cond, &mutex);
343     }
344     pthread_mutex_unlock(&mutex);
345     if (prefetch_status == SL_PREFETCHSTATUS_ERROR) {
346         fprintf(stderr, "Error during prefetch, exiting\n");
347         goto destroyRes;
348     }
349 
350     /* Display duration again, */
351     res = (*playItf)->GetDuration(playItf, &durationInMsec); CheckErr(res);
352     if (durationInMsec == SL_TIME_UNKNOWN) {
353         fprintf(stdout, "Content duration is unknown (after prefetch completed)\n");
354     } else {
355         fprintf(stdout, "Content duration is %u ms (after prefetch completed)\n", durationInMsec);
356     }
357 
358     /* Start playing */
359     fprintf(stdout, "starting to play\n");
360     res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING ); CheckErr(res);
361 
362     /* Wait as long as the duration of the content, times the repetitions,
363      * before stopping the loop */
364 #if 1
365     usleep( (REPETITIONS-1) * durationInMsec * 1100);
366 #else
367     int ii;
368     for (ii = 0; ii < REPETITIONS; ++ii) {
369         usleep(durationInMsec * 1100);
370         PlayEventCallback(playItf, (void *) rateItf, SL_PLAYEVENT_HEADATEND);
371     }
372 #endif
373 
374     res = (*seekItf)->SetLoop(seekItf, SL_BOOLEAN_FALSE, 0, SL_TIME_UNKNOWN); CheckErr(res);
375     fprintf(stdout, "As of now, stopped looping (sound shouldn't repeat from now on)\n");
376     /* wait some more to make sure it doesn't repeat */
377     usleep(durationInMsec * 1000);
378 
379     /* Stop playback */
380     fprintf(stdout, "stopping playback\n");
381     res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); CheckErr(res);
382 
383 destroyRes:
384 
385     /* Destroy the player */
386     (*player)->Destroy(player);
387 
388     /* Destroy Output Mix object */
389     (*OutputMix)->Destroy(OutputMix);
390 }
391 
392 //-----------------------------------------------------------------
main(int argc,char * const argv[])393 int main(int argc, char* const argv[])
394 {
395     SLresult    res;
396     SLObjectItf sl;
397 
398     fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLSeekItf, SLPlaybackRateItf\n",
399             argv[0]);
400     fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n");
401     fprintf(stdout, "Plays a sound and loops it %d times while changing the \n", REPETITIONS);
402     fprintf(stdout, "playback rate each time.\n\n");
403 
404     if (argc == 1) {
405         fprintf(stdout, "Usage: \n\t%s path \n\t%s url\n", argv[0], argv[0]);
406         fprintf(stdout, "Example: \"%s /sdcard/my.mp3\"  or \"%s file:///sdcard/my.mp3\"\n",
407                 argv[0], argv[0]);
408         return EXIT_FAILURE;
409     }
410 
411     SLEngineOption EngineOption[] = {
412             {(SLuint32) SL_ENGINEOPTION_THREADSAFE,
413             (SLuint32) SL_BOOLEAN_TRUE}};
414 
415     res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
416     CheckErr(res);
417     /* Realizing the SL Engine in synchronous mode. */
418     res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
419     CheckErr(res);
420 
421     TestSlowDownUri(sl, argv[1]);
422 
423     /* Shutdown OpenSL ES */
424     (*sl)->Destroy(sl);
425 
426     return EXIT_SUCCESS;
427 }
428