slesTestPlayStreamType.cpp revision c2303eb5497c488db786dcb2b8514db229452536
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 <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20#include <unistd.h>
21#include <sys/time.h>
22
23#include "SLES/OpenSLES.h"
24#ifdef ANDROID
25#include "SLES/OpenSLES_Android.h"
26#include "SLES/OpenSLES_AndroidConfiguration.h"
27#endif
28
29
30#define MAX_NUMBER_INTERFACES 2
31
32
33//-----------------------------------------------------------------
34/* Exits the application if an error is encountered */
35#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
36
37void ExitOnErrorFunc( SLresult result , int line)
38{
39    if (SL_RESULT_SUCCESS != result) {
40        fprintf(stdout, "%lu error code encountered at line %d, exiting\n", result, line);
41        exit(EXIT_FAILURE);
42    }
43}
44
45
46//-----------------------------------------------------------------
47
48/* Play an audio URIs on the given stream type  */
49void TestStreamTypeConfiguration( SLObjectItf sl, const char* path, const SLint32 type)
50{
51    SLresult  result;
52    SLEngineItf EngineItf;
53
54    /* Objects this application uses: one player and an ouput mix */
55    SLObjectItf  player, outputMix;
56
57    /* Source of audio data to play */
58    SLDataSource      audioSource;
59    SLDataLocator_URI uri;
60    SLDataFormat_MIME mime;
61
62    /* Data sinks for the audio player */
63    SLDataSink               audioSink;
64    SLDataLocator_OutputMix  locator_outputmix;
65
66    /* Play, Volume and AndroidStreamType interfaces for the audio player */
67    SLPlayItf              playItf;
68    SLPrefetchStatusItf    prefetchItf;
69#ifdef ANDROID
70    SLAndroidConfigurationItf configItf;
71#endif
72
73    SLboolean required[MAX_NUMBER_INTERFACES];
74    SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
75
76    /* Get the SL Engine Interface which is implicit */
77    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
78    ExitOnError(result);
79
80    /* Initialize arrays required[] and iidArray[] */
81    for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
82        required[i] = SL_BOOLEAN_FALSE;
83        iidArray[i] = SL_IID_NULL;
84    }
85
86    /* ------------------------------------------------------ */
87    /* Configuration of the output mix  */
88
89    /* Create Output Mix object to be used by the player */
90     result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 0, iidArray, required);
91     ExitOnError(result);
92
93    /* Realize the Output Mix object in synchronous mode */
94    result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
95    ExitOnError(result);
96
97    /* Setup the data sink structure */
98    locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
99    locator_outputmix.outputMix   = outputMix;
100    audioSink.pLocator            = (void*)&locator_outputmix;
101    audioSink.pFormat             = NULL;
102
103    /* ------------------------------------------------------ */
104    /* Configuration of the player  */
105
106    /* Set arrays required[] and iidArray[] for SLAndroidConfigurationItf interfaces */
107    /*  (SLPlayItf is implicit) */
108    required[0] = SL_BOOLEAN_TRUE;
109    iidArray[0] = SL_IID_PREFETCHSTATUS;
110    required[1] = SL_BOOLEAN_TRUE;
111    iidArray[1] = SL_IID_ANDROIDCONFIGURATION;
112
113
114    /* Setup the data source structure for the URI */
115    uri.locatorType = SL_DATALOCATOR_URI;
116    uri.URI         =  (SLchar*) path;
117    mime.formatType = SL_DATAFORMAT_MIME;
118    /*     this is how ignored mime information is specified, according to OpenSL ES spec
119     *     in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
120    mime.mimeType      = (SLchar*)NULL;
121    mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
122
123    audioSource.pFormat  = (void*)&mime;
124    audioSource.pLocator = (void*)&uri;
125
126    /* Create the audio player */
127    result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink,
128            MAX_NUMBER_INTERFACES, iidArray, required);
129    ExitOnError(result);
130
131    /* Retrieve the configuration interface before the player is realized so its resources
132     * can be configured.
133     */
134#ifdef ANDROID
135    result = (*player)->GetInterface(player, SL_IID_ANDROIDCONFIGURATION, (void*)&configItf);
136    ExitOnError(result);
137
138    /* Set the Android audio stream type on the player */
139    result = (*configItf)->SetConfiguration(configItf,
140            SL_ANDROID_KEY_STREAM_TYPE, &type, sizeof(SLint32));
141    ExitOnError(result);
142#endif
143
144    /* Realize the player in synchronous mode. */
145    result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result);
146    fprintf(stdout, "URI example: after Realize\n");
147
148    /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidConfigurationItf interfaces for player */
149    result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
150    ExitOnError(result);
151
152    result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
153    ExitOnError(result);
154
155    fprintf(stdout, "Player configured\n");
156
157    /* ------------------------------------------------------ */
158    /* Playback and test */
159
160    /* Start the data prefetching by setting the player to the paused state */
161    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
162    ExitOnError(result);
163
164    /* Wait until there's data to play */
165    SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
166    while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
167        usleep(100 * 1000);
168        (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
169        ExitOnError(result);
170    }
171
172    /* Get duration */
173    SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
174    result = (*playItf)->GetDuration(playItf, &durationInMsec);
175    ExitOnError(result);
176    if (durationInMsec == SL_TIME_UNKNOWN) {
177        durationInMsec = 5000;
178    }
179
180    /* Start playback */
181    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING );
182    ExitOnError(result);
183
184    usleep((durationInMsec/2) * 1000);
185
186#ifdef ANDROID
187    /* Get the stream type during playback  */
188    SLint32 currentType = -1;
189    SLuint32 valueSize = sizeof(SLint32) * 2; // trying too big on purpose
190    result = (*configItf)->GetConfiguration(configItf,
191            SL_ANDROID_KEY_STREAM_TYPE, &valueSize, NULL);
192    ExitOnError(result);
193    if (valueSize != sizeof(SLint32)) {
194        fprintf(stderr, "ERROR: size for stream type is %lu, should be %u\n",
195                valueSize, sizeof(SLint32));
196    }
197    result = (*configItf)->GetConfiguration(configItf,
198                SL_ANDROID_KEY_STREAM_TYPE, &valueSize, &currentType);
199    ExitOnError(result);
200    if (currentType != type) {
201        fprintf(stderr, "ERROR: stream type is %lu, should be %lu\n", currentType, type);
202    }
203#endif
204
205    usleep((durationInMsec/2) * 1000);
206
207    /* Make sure player is stopped */
208    fprintf(stdout, "Stopping playback\n");
209    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
210    ExitOnError(result);
211
212#ifdef ANDROID
213    /* Try again to get the stream type, just in case it changed behind our back */
214    result = (*configItf)->GetConfiguration(configItf,
215            SL_ANDROID_KEY_STREAM_TYPE, &valueSize, &currentType);
216    ExitOnError(result);
217    if (currentType != type) {
218        fprintf(stderr, "ERROR: stream type is %lu, should be %lu\n", currentType, type);
219    }
220#endif
221
222    /* Destroy the player */
223    (*player)->Destroy(player);
224
225    /* Destroy Output Mix object */
226    (*outputMix)->Destroy(outputMix);
227}
228
229//-----------------------------------------------------------------
230int main(int argc, char* const argv[])
231{
232    SLresult    result;
233    SLObjectItf sl;
234
235    fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLAndroidConfigurationItf\n",
236            argv[0]);
237    fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n");
238    fprintf(stdout, "Plays a sound on the specified android stream type\n");
239
240    if (argc < 3) {
241        fprintf(stdout, "Usage: \t%s url stream_type\n", argv[0]);
242        fprintf(stdout, " where stream_type is one of the SL_ANDROID_STREAM_ constants.\n");
243        fprintf(stdout, "Example: \"%s /sdcard/my.mp3 3\" \n", argv[0]);
244        return EXIT_FAILURE;
245    }
246
247    SLEngineOption EngineOption[] = {
248            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
249    };
250
251    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
252    ExitOnError(result);
253
254    /* Realizing the SL Engine in synchronous mode. */
255    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
256    ExitOnError(result);
257
258    TestStreamTypeConfiguration(sl, argv[1], (SLint32)atoi(argv[2]));
259
260    /* Shutdown OpenSL ES */
261    (*sl)->Destroy(sl);
262
263    return EXIT_SUCCESS;
264}
265