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