slesTestPlayStreamType.cpp revision c6853892c94800e72c0bd676d5d2136d48cea76e
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, "%u 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#ifdef ANDROID 111 required[1] = SL_BOOLEAN_TRUE; 112 iidArray[1] = SL_IID_ANDROIDCONFIGURATION; 113#endif 114 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, 130 MAX_NUMBER_INTERFACES, iidArray, required); 131 ExitOnError(result); 132 133 /* Retrieve the configuration interface before the player is realized so its resources 134 * can be configured. 135 */ 136#ifdef ANDROID 137 result = (*player)->GetInterface(player, SL_IID_ANDROIDCONFIGURATION, (void*)&configItf); 138 ExitOnError(result); 139 140 /* Set the Android audio stream type on the player */ 141 result = (*configItf)->SetConfiguration(configItf, 142 SL_ANDROID_KEY_STREAM_TYPE, &type, sizeof(SLint32)); 143 if (SL_RESULT_PARAMETER_INVALID == result) { 144 fprintf(stderr, "invalid stream type %d\n", type); 145 } else { 146 ExitOnError(result); 147 } 148#endif 149 150 /* Realize the player in synchronous mode. */ 151 result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result); 152 fprintf(stdout, "URI example: after Realize\n"); 153 154 /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidConfigurationItf interfaces for player */ 155 result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); 156 ExitOnError(result); 157 158 result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf); 159 ExitOnError(result); 160 161 fprintf(stdout, "Player configured\n"); 162 163 /* ------------------------------------------------------ */ 164 /* Playback and test */ 165 166 /* Start the data prefetching by setting the player to the paused state */ 167 result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); 168 ExitOnError(result); 169 170 /* Wait until there's data to play */ 171 SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW; 172 while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) { 173 usleep(100 * 1000); 174 (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus); 175 ExitOnError(result); 176 } 177 178 /* Get duration */ 179 SLmillisecond durationInMsec = SL_TIME_UNKNOWN; 180 result = (*playItf)->GetDuration(playItf, &durationInMsec); 181 ExitOnError(result); 182 if (durationInMsec == SL_TIME_UNKNOWN) { 183 durationInMsec = 5000; 184 } 185 186 /* Start playback */ 187 result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING ); 188 ExitOnError(result); 189 190 usleep((durationInMsec/2) * 1000); 191 192#ifdef ANDROID 193 /* Get the stream type during playback */ 194 SLint32 currentType = -1; 195 SLuint32 valueSize = sizeof(SLint32) * 2; // trying too big on purpose 196 result = (*configItf)->GetConfiguration(configItf, 197 SL_ANDROID_KEY_STREAM_TYPE, &valueSize, NULL); 198 ExitOnError(result); 199 if (valueSize != sizeof(SLint32)) { 200 fprintf(stderr, "ERROR: size for stream type is %u, should be %u\n", 201 valueSize, sizeof(SLint32)); 202 } 203 result = (*configItf)->GetConfiguration(configItf, 204 SL_ANDROID_KEY_STREAM_TYPE, &valueSize, ¤tType); 205 ExitOnError(result); 206 if (currentType != type) { 207 fprintf(stderr, "ERROR: stream type is %u, should be %u\n", currentType, type); 208 } 209#endif 210 211 usleep((durationInMsec/2) * 1000); 212 213 /* Make sure player is stopped */ 214 fprintf(stdout, "Stopping playback\n"); 215 result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); 216 ExitOnError(result); 217 218#ifdef ANDROID 219 /* Try again to get the stream type, just in case it changed behind our back */ 220 result = (*configItf)->GetConfiguration(configItf, 221 SL_ANDROID_KEY_STREAM_TYPE, &valueSize, ¤tType); 222 ExitOnError(result); 223 if (currentType != type) { 224 fprintf(stderr, "ERROR: stream type is %u, should be %u\n", currentType, type); 225 } 226#endif 227 228 /* Destroy the player */ 229 (*player)->Destroy(player); 230 231 /* Destroy Output Mix object */ 232 (*outputMix)->Destroy(outputMix); 233} 234 235//----------------------------------------------------------------- 236int main(int argc, char* const argv[]) 237{ 238 SLresult result; 239 SLObjectItf sl; 240 241 fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLAndroidConfigurationItf\n", 242 argv[0]); 243 fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n"); 244 fprintf(stdout, "Plays a sound on the specified android stream type\n"); 245 246 if (argc < 3) { 247 fprintf(stdout, "Usage: \t%s url stream_type\n", argv[0]); 248 fprintf(stdout, " where stream_type is one of the SL_ANDROID_STREAM_ constants.\n"); 249 fprintf(stdout, "Example: \"%s /sdcard/my.mp3 5\" \n", argv[0]); 250 fprintf(stdout, "Stream type %d is the default (media or music), %d is notifications\n", 251 SL_ANDROID_STREAM_MEDIA, SL_ANDROID_STREAM_NOTIFICATION); 252 return EXIT_FAILURE; 253 } 254 255 SLEngineOption EngineOption[] = { 256 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 257 }; 258 259 result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 260 ExitOnError(result); 261 262 /* Realizing the SL Engine in synchronous mode. */ 263 result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 264 ExitOnError(result); 265 266 TestStreamTypeConfiguration(sl, argv[1], (SLint32)atoi(argv[2])); 267 268 /* Shutdown OpenSL ES */ 269 (*sl)->Destroy(sl); 270 271 return EXIT_SUCCESS; 272} 273