slesTestLoopUri.cpp revision d94d32190f845b41f212c9c1918758e33fef6382
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#define LOG_NDEBUG 0
18#define LOG_TAG "slesTestLoopUri"
19
20#ifdef ANDROID
21#include <utils/Log.h>
22#else
23#define LOGV printf
24#endif
25#include <getopt.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29#include <unistd.h>
30#include <sys/time.h>
31
32#include "SLES/OpenSLES.h"
33
34
35#define MAX_NUMBER_INTERFACES 2
36
37#define REPETITIONS 4
38
39//-----------------------------------------------------------------
40//* Exits the application if an error is encountered */
41#define CheckErr(x) ExitOnErrorFunc(x,__LINE__)
42
43void ExitOnErrorFunc( SLresult result , int line)
44{
45    if (SL_RESULT_SUCCESS != result) {
46        fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line);
47        exit(1);
48    }
49}
50
51//-----------------------------------------------------------------
52/* PrefetchStatusItf callback for an audio player */
53void PrefetchEventCallback( SLPrefetchStatusItf caller,  void *pContext, SLuint32 event)
54{
55    SLpermille level = 0;
56    (*caller)->GetFillLevel(caller, &level);
57    SLuint32 status;
58    //fprintf(stdout, "\t\tPrefetchEventCallback: received event %lu\n", event);
59    (*caller)->GetPrefetchStatus(caller, &status);
60    if ((event & (SL_PREFETCHEVENT_STATUSCHANGE|SL_PREFETCHEVENT_FILLLEVELCHANGE))
61            && (level == 0) && (status == SL_PREFETCHSTATUS_UNDERFLOW)) {
62        fprintf(stdout, "\t\tPrefetchEventCallback: Error while prefetching data, exiting\n");
63        //exit(1);
64    }
65    if (event & SL_PREFETCHEVENT_FILLLEVELCHANGE) {
66        fprintf(stdout, "\t\tPrefetchEventCallback: Buffer fill level is = %d\n", level);
67    }
68    if (event & SL_PREFETCHEVENT_STATUSCHANGE) {
69        fprintf(stdout, "\t\tPrefetchEventCallback: Prefetch Status is = %lu\n", status);
70    }
71
72}
73
74
75//-----------------------------------------------------------------
76
77/* Play some music from a URI  */
78void TestLoopUri( SLObjectItf sl, const char* path)
79{
80    SLEngineItf                EngineItf;
81
82    SLint32                    numOutputs = 0;
83    SLuint32                   deviceID = 0;
84
85    SLresult                   res;
86
87    SLDataSource               audioSource;
88    SLDataLocator_URI          uri;
89    SLDataFormat_MIME          mime;
90
91    SLDataSink                 audioSink;
92    SLDataLocator_OutputMix    locator_outputmix;
93
94    SLObjectItf                player;
95    SLPlayItf                  playItf;
96    SLSeekItf                  seekItf;
97    SLPrefetchStatusItf        prefetchItf;
98
99    SLObjectItf                OutputMix;
100
101    SLboolean required[MAX_NUMBER_INTERFACES];
102    SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
103
104    /* Get the SL Engine Interface which is implicit */
105    res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
106    CheckErr(res);
107
108    /* Initialize arrays required[] and iidArray[] */
109    for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
110        required[i] = SL_BOOLEAN_FALSE;
111        iidArray[i] = SL_IID_NULL;
112    }
113
114    required[0] = SL_BOOLEAN_TRUE;
115    iidArray[0] = SL_IID_VOLUME;
116    // Create Output Mix object to be used by player
117    res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 1,
118            iidArray, required); CheckErr(res);
119
120    // Realizing the Output Mix object in synchronous mode.
121    res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);
122    CheckErr(res);
123
124    /* Setup the data source structure for the URI */
125    uri.locatorType = SL_DATALOCATOR_URI;
126    uri.URI         =  (SLchar*) path;
127    mime.formatType    = SL_DATAFORMAT_MIME;
128    mime.mimeType      = (SLchar*)NULL;
129    mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
130
131    audioSource.pFormat      = (void *)&mime;
132    audioSource.pLocator     = (void *)&uri;
133
134    /* Setup the data sink structure */
135    locator_outputmix.locatorType   = SL_DATALOCATOR_OUTPUTMIX;
136    locator_outputmix.outputMix    = OutputMix;
137    audioSink.pLocator           = (void *)&locator_outputmix;
138    audioSink.pFormat            = NULL;
139
140    /* Create the audio player */
141    required[0] = SL_BOOLEAN_TRUE;
142    iidArray[0] = SL_IID_SEEK;
143    required[1] = SL_BOOLEAN_TRUE;
144    iidArray[1] = SL_IID_PREFETCHSTATUS;
145    res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink,
146            MAX_NUMBER_INTERFACES, iidArray, required); CheckErr(res);
147
148    /* Realizing the player in synchronous mode. */
149    res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res);
150    fprintf(stdout, "URI example: after Realize\n");
151
152    /* Get interfaces */
153    res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
154    CheckErr(res);
155
156    res = (*player)->GetInterface(player, SL_IID_SEEK,  (void*)&seekItf);
157    CheckErr(res);
158
159    res = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
160    CheckErr(res);
161    res = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, &prefetchItf);
162    CheckErr(res);
163    res = (*prefetchItf)->SetCallbackEventsMask(prefetchItf,
164            SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE);
165    CheckErr(res);
166
167    /* Configure fill level updates every 5 percent */
168    (*prefetchItf)->SetFillUpdatePeriod(prefetchItf, 50);
169
170    /* Display duration */
171    SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
172    res = (*playItf)->GetDuration(playItf, &durationInMsec);
173    CheckErr(res);
174    if (durationInMsec == SL_TIME_UNKNOWN) {
175        fprintf(stdout, "Content duration is unknown (before starting to prefetch)\n");
176    } else {
177        fprintf(stdout, "Content duration is %lu ms (before starting to prefetch)\n",
178                durationInMsec);
179    }
180
181    /* Loop on the whole of the content */
182    res = (*seekItf)->SetLoop(seekItf, SL_BOOLEAN_TRUE, 0, SL_TIME_UNKNOWN);
183    CheckErr(res);
184
185    /* Play the URI */
186    /*     first cause the player to prefetch the data */
187    res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
188    CheckErr(res);
189
190    /*     wait until there's data to play */
191    //SLpermille fillLevel = 0;
192    SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
193    SLuint32 timeOutIndex = 100; // 10s
194    while ((prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) && (timeOutIndex > 0)) {
195        usleep(100 * 1000);
196        (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
197        timeOutIndex--;
198    }
199
200    if (timeOutIndex == 0) {
201        fprintf(stderr, "\nWe\'re done waiting, failed to prefetch data in time, exiting\n");
202        goto destroyRes;
203    }
204
205    /* Display duration again, */
206    res = (*playItf)->GetDuration(playItf, &durationInMsec);
207    CheckErr(res);
208    if (durationInMsec == SL_TIME_UNKNOWN) {
209        fprintf(stdout, "Content duration is unknown (after prefetch completed)\n");
210    } else {
211        fprintf(stdout, "Content duration is %lu ms (after prefetch completed)\n", durationInMsec);
212    }
213
214    fprintf(stdout, "starting to play\n");
215    res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING );
216    CheckErr(res);
217
218    /* Wait as long as the duration of the content, times the repetitions,
219     * before stopping the loop */
220    usleep( (REPETITIONS-1) * durationInMsec * 1100);
221    res = (*seekItf)->SetLoop(seekItf, SL_BOOLEAN_FALSE, 0, SL_TIME_UNKNOWN);
222    CheckErr(res);
223    fprintf(stdout, "As of now, stopped looping (sound shouldn't repeat from now on)\n");
224    /* wait some more to make sure it doesn't repeat */
225    usleep(durationInMsec * 1000);
226
227    /* Stop playback */
228    fprintf(stdout, "stopping playback\n");
229    res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
230    CheckErr(res);
231
232destroyRes:
233
234    /* Destroy the player */
235    (*player)->Destroy(player);
236
237    /* Destroy Output Mix object */
238    (*OutputMix)->Destroy(OutputMix);
239}
240
241//-----------------------------------------------------------------
242int main(int argc, char* const argv[])
243{
244    LOGV("Starting slesTestLoopUri\n");
245
246    SLresult    res;
247    SLObjectItf sl;
248
249    fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLSeekItf ", argv[0]);
250    fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n");
251    fprintf(stdout, "Plays a sound and loops it %d times.\n\n", REPETITIONS);
252
253    if (argc == 1) {
254        fprintf(stdout, "Usage: \n\t%s path \n\t%s url\n", argv[0], argv[0]);
255        fprintf(stdout, "Example: \"%s /sdcard/my.mp3\"  or \"%s file:///sdcard/my.mp3\"\n",
256                argv[0], argv[0]);
257        exit(1);
258    }
259
260    SLEngineOption EngineOption[] = {
261            {(SLuint32) SL_ENGINEOPTION_THREADSAFE,
262            (SLuint32) SL_BOOLEAN_TRUE}};
263
264    res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
265    CheckErr(res);
266    /* Realizing the SL Engine in synchronous mode. */
267    res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
268    CheckErr(res);
269
270    TestLoopUri(sl, argv[1]);
271
272    /* Shutdown OpenSL ES */
273    (*sl)->Destroy(sl);
274    exit(0);
275
276    return 0;
277}
278