slesTest_playStates.cpp revision 8a1b7f28c1c3de212a302182022310ab7b227788
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 "slesTest_playStates"
19
20#include <utils/Log.h>
21#include <getopt.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/time.h>
27
28#include "SLES/OpenSLES.h"
29
30
31#define MAX_NUMBER_INTERFACES 2
32
33#define TEST_MUTE 0
34#define TEST_SOLO 1
35
36static int testMode;
37//-----------------------------------------------------------------
38/* Exits the application if an error is encountered */
39#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
40
41void ExitOnErrorFunc( SLresult result , int line)
42{
43    if (SL_RESULT_SUCCESS != result) {
44        fprintf(stdout, "%lu error code encountered at line %d, exiting\n", result, line);
45        exit(1);
46    }
47}
48
49
50//-----------------------------------------------------------------
51
52/* Play an audio URIs, play, pause, stop  */
53void TestPlayUri( SLObjectItf sl, const char* path)
54{
55    SLresult  result;
56    SLEngineItf EngineItf;
57
58    /* Objects this application uses: one player and an ouput mix */
59    SLObjectItf  player, outputMix;
60
61    /* Source of audio data to play */
62    SLDataSource      audioSource;
63    SLDataLocator_URI uri;
64    SLDataFormat_MIME mime;
65
66    /* Data sinks for the audio player */
67    SLDataSink               audioSink;
68    SLDataLocator_OutputMix  locator_outputmix;
69
70    /* Play, Volume and PrefetchStatus interfaces for the audio player */
71    SLPlayItf           playItf;
72    SLMuteSoloItf       muteSoloItf;
73    SLPrefetchStatusItf prefetchItf;
74
75    SLboolean required[MAX_NUMBER_INTERFACES];
76    SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
77
78    /* Get the SL Engine Interface which is implicit */
79    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
80    ExitOnError(result);
81
82    /* Initialize arrays required[] and iidArray[] */
83    for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
84        required[i] = SL_BOOLEAN_FALSE;
85        iidArray[i] = SL_IID_NULL;
86    }
87
88    /* ------------------------------------------------------ */
89    /* Configuration of the output mix  */
90
91    /* Create Output Mix object to be used by the player */
92     result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 1, iidArray, required);
93     ExitOnError(result);
94
95    /* Realize the Output Mix object in synchronous mode */
96    result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
97    ExitOnError(result);
98
99    /* Setup the data sink structure */
100    locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
101    locator_outputmix.outputMix   = outputMix;
102    audioSink.pLocator            = (void*)&locator_outputmix;
103    audioSink.pFormat             = NULL;
104
105    /* ------------------------------------------------------ */
106    /* Configuration of the player  */
107
108    /* Set arrays required[] and iidArray[] for SLMuteSoloItf and SLPrefetchStatusItf interfaces */
109    /*  (SLPlayItf is implicit) */
110    required[0] = SL_BOOLEAN_TRUE;
111    iidArray[0] = SL_IID_MUTESOLO;
112    required[1] = SL_BOOLEAN_TRUE;
113    iidArray[1] = SL_IID_PREFETCHSTATUS;
114
115    /* Setup the data source structure for the URI */
116    uri.locatorType = SL_DATALOCATOR_URI;
117    uri.URI         =  (SLchar*) path;
118    mime.formatType = SL_DATAFORMAT_MIME;
119    /*     this is how ignored mime information is specified, according to OpenSL ES spec
120     *     in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
121    mime.mimeType      = (SLchar*)NULL;
122    mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
123
124    audioSource.pFormat  = (void*)&mime;
125    audioSource.pLocator = (void*)&uri;
126
127    /* Create the audio player */
128    result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink,
129            MAX_NUMBER_INTERFACES, iidArray, required);
130    ExitOnError(result);
131
132    /* Realize the player in synchronous mode. */
133    result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result);
134    fprintf(stdout, "URI example: after Realize\n");
135
136    /* Get the SLPlayItf, SLPrefetchStatusItf and SLMuteSoloItf interfaces for the player */
137    result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
138    ExitOnError(result);
139
140    result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
141    ExitOnError(result);
142
143    result = (*player)->GetInterface(player, SL_IID_MUTESOLO, (void*)&muteSoloItf);
144    ExitOnError(result);
145
146
147    fprintf(stdout, "Player configured\n");
148
149    /* ------------------------------------------------------ */
150    /* Playback and test */
151
152    /* Start the data prefetching by setting the player to the paused state */
153    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
154    ExitOnError(result);
155
156    /* Wait until there's data to play */
157    SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
158    while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
159        usleep(100 * 1000);
160        (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
161    }
162
163
164    /* Testing play states */
165    /* let it play for 2s */
166    fprintf(stdout, "----- Playing\n");
167    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING );
168    ExitOnError(result);
169    usleep(2 * 1000 * 1000);
170
171    /* pause for 1s*/
172    fprintf(stdout, "----- Pausing (1s)\n");
173    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
174    ExitOnError(result);
175    usleep(2 * 1000 * 1000);
176
177    /* resume */
178    fprintf(stdout, "----- Playing (2s, should have resumed where it paused)\n");
179    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING );
180    ExitOnError(result);
181    usleep(2 * 1000 * 1000);
182
183    /* stop */
184    fprintf(stdout, "----- Stopping\n");
185    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
186    ExitOnError(result);
187
188    /* play for 2s */
189    fprintf(stdout, "----- Playing (2s, should have started from the beginning\n");
190    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING );
191    ExitOnError(result);
192    usleep(2 * 1000 * 1000);
193
194    /* stop */
195    fprintf(stdout, "----- Stopping\n");
196    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
197    ExitOnError(result);
198
199destroyKillKill:
200
201    /* Destroy the players */
202    (*player)->Destroy(player);
203
204    /* Destroy Output Mix object */
205    (*outputMix)->Destroy(outputMix);
206}
207
208//-----------------------------------------------------------------
209int main(int argc, char* const argv[])
210{
211    LOGV("Starting %s\n", argv[0]);
212
213    SLresult    result;
214    SLObjectItf sl;
215
216    fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLVolumeItf, SLMuteSoloItf\n",
217            argv[0]);
218    fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n");
219    fprintf(stdout, "Plays a sound and alternates the muting of the channels (for 5s).\n");
220    fprintf(stdout, " and then alternates the solo\'ing of the channels (for 5s).\n");
221    fprintf(stdout, "Stops after 10s\n");
222
223    if (argc == 1) {
224        fprintf(stdout, "Usage: \t%s url\n", argv[0]);
225        fprintf(stdout, "Example: \"%s /sdcard/my.mp3\"\n", argv[0]);
226        exit(1);
227    }
228
229    SLEngineOption EngineOption[] = {
230            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
231    };
232
233    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
234    ExitOnError(result);
235
236    /* Realizing the SL Engine in synchronous mode. */
237    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
238    ExitOnError(result);
239
240    if (argc > 1) {
241        TestPlayUri(sl, argv[1]);
242    }
243
244    /* Shutdown OpenSL ES */
245    (*sl)->Destroy(sl);
246    exit(0);
247
248    return 0;
249}
250