• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*
18  * Copyright (c) 2009 The Khronos Group Inc.
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
21  * software and /or associated documentation files (the "Materials "), to deal in the
22  * Materials without restriction, including without limitation the rights to use, copy,
23  * modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials,
24  * and to permit persons to whom the Materials are furnished to do so, subject to
25  * the following conditions:
26  *
27  * The above copyright notice and this permission notice shall be included
28  * in all copies or substantial portions of the Materials.
29  *
30  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
34  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
35  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
36  * CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS IN THE
37  * MATERIALS.
38  */
39 
40 #include <stdlib.h>
41 #include <stdio.h>
42 //#include <string.h>
43 #include <unistd.h>
44 //#include <sys/time.h>
45 
46 #include <SLES/OpenSLES.h>
47 
48 //#define TEST_VOLUME_ITF
49 //#define TEST_COLD_START
50 
51 #define MAX_NUMBER_INTERFACES 2
52 
53 #define PREFETCHEVENT_ERROR_CANDIDATE \
54         (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE)
55 
56 //-----------------------------------------------------------------
57 //* Exits the application if an error is encountered */
58 #define CheckErr(x) ExitOnErrorFunc(x,__LINE__)
59 
ExitOnErrorFunc(SLresult result,int line)60 void ExitOnErrorFunc( SLresult result , int line)
61 {
62     if (SL_RESULT_SUCCESS != result) {
63         fprintf(stderr, "%u error code encountered at line %d, exiting\n", result, line);
64         exit(EXIT_FAILURE);
65     }
66 }
67 
68 bool prefetchError = false;
69 
70 //-----------------------------------------------------------------
71 /* PrefetchStatusItf callback for an audio player */
PrefetchEventCallback(SLPrefetchStatusItf caller,void * pContext __unused,SLuint32 event)72 void PrefetchEventCallback( SLPrefetchStatusItf caller,  void *pContext __unused, SLuint32 event)
73 {
74     SLpermille level = 0;
75     SLresult result;
76     result = (*caller)->GetFillLevel(caller, &level);
77     CheckErr(result);
78     SLuint32 status;
79     //fprintf(stdout, "PrefetchEventCallback: received event %u\n", event);
80     result = (*caller)->GetPrefetchStatus(caller, &status);
81     CheckErr(result);
82     if ((PREFETCHEVENT_ERROR_CANDIDATE == (event & PREFETCHEVENT_ERROR_CANDIDATE))
83             && (level == 0) && (status == SL_PREFETCHSTATUS_UNDERFLOW)) {
84         fprintf(stdout, "PrefetchEventCallback: Error while prefetching data, exiting\n");
85         prefetchError = true;
86     }
87     if (event & SL_PREFETCHEVENT_FILLLEVELCHANGE) {
88         fprintf(stdout, "PrefetchEventCallback: Buffer fill level is = %d\n", level);
89     }
90     if (event & SL_PREFETCHEVENT_STATUSCHANGE) {
91         fprintf(stdout, "PrefetchEventCallback: Prefetch Status is = %u\n", status);
92     }
93 }
94 
95 
96 //-----------------------------------------------------------------
97 /* PlayItf callback for playback events */
PlayEventCallback(SLPlayItf caller,void * pContext __unused,SLuint32 event)98 void PlayEventCallback(
99         SLPlayItf caller,
100         void *pContext __unused,
101         SLuint32 event)
102 {
103     if (SL_PLAYEVENT_HEADATEND & event) {
104         fprintf(stdout, "SL_PLAYEVENT_HEADATEND reached\n");
105         //SignalEos();
106     }
107 
108     if (SL_PLAYEVENT_HEADATNEWPOS & event) {
109         SLmillisecond pMsec = 0;
110         (*caller)->GetPosition(caller, &pMsec);
111         fprintf(stdout, "SL_PLAYEVENT_HEADATNEWPOS current position=%ums\n", pMsec);
112     }
113 
114     if (SL_PLAYEVENT_HEADATMARKER & event) {
115         SLmillisecond pMsec = 0;
116         (*caller)->GetPosition(caller, &pMsec);
117         fprintf(stdout, "SL_PLAYEVENT_HEADATMARKER current position=%ums\n", pMsec);
118     }
119 }
120 
121 
122 //-----------------------------------------------------------------
123 
124 /* Play some music from a URI  */
TestPlayUri(SLObjectItf sl,const char * path)125 void TestPlayUri( SLObjectItf sl, const char* path)
126 {
127     SLEngineItf                EngineItf;
128 
129     SLresult                   res;
130 
131     SLDataSource               audioSource;
132     SLDataLocator_URI          uri;
133     SLDataFormat_MIME          mime;
134 
135     SLDataSink                 audioSink;
136     SLDataLocator_OutputMix    locator_outputmix;
137 
138     SLObjectItf                player;
139     SLPlayItf                  playItf;
140     SLVolumeItf                volItf;
141     SLPrefetchStatusItf        prefetchItf;
142 
143     SLObjectItf                OutputMix;
144 
145     SLboolean required[MAX_NUMBER_INTERFACES];
146     SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
147 
148     /* Get the SL Engine Interface which is implicit */
149     res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
150     CheckErr(res);
151 
152     /* Initialize arrays required[] and iidArray[] */
153     for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
154         required[i] = SL_BOOLEAN_FALSE;
155         iidArray[i] = SL_IID_NULL;
156     }
157 
158     // Set arrays required[] and iidArray[] for VOLUME and PREFETCHSTATUS interface
159     required[0] = SL_BOOLEAN_TRUE;
160     iidArray[0] = SL_IID_VOLUME;
161     required[1] = SL_BOOLEAN_TRUE;
162     iidArray[1] = SL_IID_PREFETCHSTATUS;
163     // Create Output Mix object to be used by player
164     res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0,
165             iidArray, required); CheckErr(res);
166 
167     // Realizing the Output Mix object in synchronous mode.
168     res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);
169     CheckErr(res);
170 
171     /* Setup the data source structure for the URI */
172     uri.locatorType = SL_DATALOCATOR_URI;
173     uri.URI         =  (SLchar*) path;
174     mime.formatType    = SL_DATAFORMAT_MIME;
175     mime.mimeType      = (SLchar*)NULL;
176     mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
177 
178     audioSource.pFormat      = (void *)&mime;
179     audioSource.pLocator     = (void *)&uri;
180 
181     /* Setup the data sink structure */
182     locator_outputmix.locatorType   = SL_DATALOCATOR_OUTPUTMIX;
183     locator_outputmix.outputMix    = OutputMix;
184     audioSink.pLocator           = (void *)&locator_outputmix;
185     audioSink.pFormat            = NULL;
186 
187     /* Create the audio player */
188     res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink,
189             MAX_NUMBER_INTERFACES, iidArray, required); CheckErr(res);
190 
191     /* Realizing the player in synchronous mode. */
192     res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res);
193     fprintf(stdout, "URI example: after Realize\n");
194 
195     /* Get interfaces */
196     res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
197     CheckErr(res);
198 
199     res = (*player)->GetInterface(player, SL_IID_VOLUME,  (void*)&volItf);
200     CheckErr(res);
201 
202     res = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
203     CheckErr(res);
204     res = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, &prefetchItf);
205     CheckErr(res);
206     res = (*prefetchItf)->SetCallbackEventsMask(prefetchItf,
207             SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE);
208     CheckErr(res);
209 
210     /* Configure fill level updates every 5 percent */
211     (*prefetchItf)->SetFillUpdatePeriod(prefetchItf, 50);
212 
213     /* Set up the player callback to get events during the decoding */
214     res = (*playItf)->SetMarkerPosition(playItf, 2000);
215     CheckErr(res);
216     res = (*playItf)->SetPositionUpdatePeriod(playItf, 500);
217     CheckErr(res);
218     res = (*playItf)->SetCallbackEventsMask(playItf,
219             SL_PLAYEVENT_HEADATMARKER | SL_PLAYEVENT_HEADATNEWPOS | SL_PLAYEVENT_HEADATEND);
220     CheckErr(res);
221     res = (*playItf)->RegisterCallback(playItf, PlayEventCallback, NULL);
222     CheckErr(res);
223 
224     /* Display duration */
225     SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
226     res = (*playItf)->GetDuration(playItf, &durationInMsec);
227     CheckErr(res);
228     if (durationInMsec == SL_TIME_UNKNOWN) {
229         fprintf(stdout, "Content duration is unknown (before starting to prefetch)\n");
230     } else {
231         fprintf(stdout, "Content duration is %u ms (before starting to prefetch)\n",
232                 durationInMsec);
233     }
234 
235     /* Set the player volume */
236     res = (*volItf)->SetVolumeLevel( volItf, -300);
237     CheckErr(res);
238 
239     /* Play the URI */
240     /*     first cause the player to prefetch the data */
241     fprintf(stdout, "Before set to PAUSED\n");
242     res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
243     fprintf(stdout, "After set to PAUSED\n");
244     CheckErr(res);
245 
246     usleep(100 * 1000);
247     /*     wait until there's data to play */
248     //SLpermille fillLevel = 0;
249     SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
250     SLuint32 timeOutIndex = 100; // 10s
251     while ((prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) && (timeOutIndex > 0) &&
252             !prefetchError) {
253         usleep(100 * 1000);
254         (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
255         timeOutIndex--;
256     }
257 
258     if (timeOutIndex == 0 || prefetchError) {
259         fprintf(stderr, "We\'re done waiting, failed to prefetch data in time, exiting\n");
260         goto destroyRes;
261     }
262 
263     /* Display duration again, */
264     res = (*playItf)->GetDuration(playItf, &durationInMsec);
265     CheckErr(res);
266     if (durationInMsec == SL_TIME_UNKNOWN) {
267         fprintf(stdout, "Content duration is unknown (after prefetch completed)\n");
268     } else {
269         fprintf(stdout, "Content duration is %u ms (after prefetch completed)\n", durationInMsec);
270     }
271 
272     fprintf(stdout, "URI example: starting to play\n");
273     res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING );
274     CheckErr(res);
275 
276 #ifdef TEST_VOLUME_ITF
277     usleep(5*1000 * 1000);
278     fprintf(stdout, "setting vol to 0\n");
279     (*volItf)->SetVolumeLevel( volItf, 0);
280     usleep(3*1000 * 1000);
281     fprintf(stdout, "setting vol to -20dB\n");
282     (*volItf)->SetVolumeLevel( volItf, -2000);
283     usleep(3*1000 * 1000);
284     fprintf(stdout, "mute\n");
285     (*volItf)->SetMute( volItf, SL_BOOLEAN_TRUE);
286     usleep(3*1000 * 1000);
287     fprintf(stdout, "setting vol to 0dB while muted\n");
288     (*volItf)->SetVolumeLevel( volItf, 0);
289     usleep(3*1000 * 1000);
290     fprintf(stdout, "unmuting\n");
291     (*volItf)->SetMute( volItf, SL_BOOLEAN_FALSE);
292     usleep(3*1000 * 1000);
293 #endif
294 
295 #ifndef TEST_COLD_START
296     usleep(durationInMsec * 1000);
297 #else
298     /* Wait as long as the duration of the content before stopping */
299     /* Experiment: wait for the duration + 200ms: with a cold start of the audio hardware, we */
300     /*    won't see the SL_PLAYEVENT_HEADATEND event, due to hw wake up induced latency, but  */
301     /*    with a warm start it will be received.                                              */
302     usleep((durationInMsec + 200) * 1000);
303 #endif
304 
305     /* Make sure player is stopped */
306     fprintf(stdout, "URI example: stopping playback\n");
307     res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
308     CheckErr(res);
309 
310 destroyRes:
311 
312     /* Destroy the player */
313     (*player)->Destroy(player);
314 
315     /* Destroy Output Mix object */
316     (*OutputMix)->Destroy(OutputMix);
317 }
318 
319 //-----------------------------------------------------------------
main(int argc,char * const argv[])320 int main(int argc, char* const argv[])
321 {
322     SLresult    res;
323     SLObjectItf sl;
324 
325     fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLVolumeItf ", argv[0]);
326     fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n");
327     fprintf(stdout, "Plays a sound and stops after its reported duration\n\n");
328 
329     if (argc == 1) {
330         fprintf(stdout, "Usage: %s path \n\t%s url\n", argv[0], argv[0]);
331         fprintf(stdout, "Example: \"%s /sdcard/my.mp3\"  or \"%s file:///sdcard/my.mp3\"\n",
332                 argv[0], argv[0]);
333         exit(EXIT_FAILURE);
334     }
335 
336     SLEngineOption EngineOption[] = {
337             {(SLuint32) SL_ENGINEOPTION_THREADSAFE,
338             (SLuint32) SL_BOOLEAN_TRUE}};
339 
340     res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
341     CheckErr(res);
342     /* Realizing the SL Engine in synchronous mode. */
343     res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
344     CheckErr(res);
345 
346     TestPlayUri(sl, argv[1]);
347 
348     /* Shutdown OpenSL ES */
349     (*sl)->Destroy(sl);
350 
351     return EXIT_SUCCESS;
352 }
353