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