slesTestVirtualizerPath.cpp revision cfe5b1c6f0b65499d9aed2cf4b2a7e805fb02758
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_virtualizer" 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#include <fcntl.h> 28 29#include "SLES/OpenSLES.h" 30#include "SLES/OpenSLES_Android.h" 31 32 33#define MAX_NUMBER_INTERFACES 3 34#define MAX_NUMBER_OUTPUT_DEVICES 6 35 36#define TIME_S_BETWEEN_VIRT_ON_OFF 3 37 38static int testMode; 39//----------------------------------------------------------------- 40/* Exits the application if an error is encountered */ 41#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__) 42 43void ExitOnErrorFunc( SLresult result , int line) 44{ 45 if (SL_RESULT_SUCCESS != result) { 46 fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line); 47 exit(1); 48 } 49} 50 51 52//----------------------------------------------------------------- 53 54/* Play an audio path by opening a file descriptor on that path */ 55void TestVirtualizerPathFromFD( SLObjectItf sl, const char* path, int16_t virtStrength) 56{ 57 SLresult result; 58 SLEngineItf EngineItf; 59 60 /* Objects this application uses: one player and an ouput mix */ 61 SLObjectItf player, outputMix; 62 63 /* Source of audio data to play */ 64 SLDataSource audioSource; 65 SLDataLocator_AndroidFD locatorFd; 66 SLDataFormat_MIME mime; 67 68 /* Data sinks for the audio player */ 69 SLDataSink audioSink; 70 SLDataLocator_OutputMix locator_outputmix; 71 72 /* Play and PrefetchStatus interfaces for the audio player */ 73 SLPlayItf playItf; 74 SLPrefetchStatusItf prefetchItf; 75 SLVirtualizerItf virtItf; 76 77 SLboolean required[MAX_NUMBER_INTERFACES]; 78 SLInterfaceID iidArray[MAX_NUMBER_INTERFACES]; 79 80 /* Get the SL Engine Interface which is implicit */ 81 result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 82 ExitOnError(result); 83 84 /* Initialize arrays required[] and iidArray[] */ 85 for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) { 86 required[i] = SL_BOOLEAN_FALSE; 87 iidArray[i] = SL_IID_NULL; 88 } 89 90 /* ------------------------------------------------------ */ 91 /* Configuration of the output mix */ 92 93 /* Create Output Mix object to be used by the player */ 94 result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 1, iidArray, required); 95 ExitOnError(result); 96 97 /* Realize the Output Mix object in synchronous mode */ 98 result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE); 99 ExitOnError(result); 100 101 /* Setup the data sink structure */ 102 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; 103 locator_outputmix.outputMix = outputMix; 104 audioSink.pLocator = (void*)&locator_outputmix; 105 audioSink.pFormat = NULL; 106 107 /* ------------------------------------------------------ */ 108 /* Configuration of the player */ 109 110 /* Set arrays required[] and iidArray[] for SLPrefetchStatusItf interfaces */ 111 /* (SLPlayItf is implicit) */ 112 required[0] = SL_BOOLEAN_TRUE; 113 iidArray[0] = SL_IID_PREFETCHSTATUS; 114 required[1] = SL_BOOLEAN_TRUE; 115 iidArray[1] = SL_IID_VIRTUALIZER; 116 117 /* Setup the data source structure for the URI */ 118 locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD; 119 int fd = open(path, O_RDONLY); 120 if (fd == -1) { 121 ExitOnError(SL_RESULT_RESOURCE_ERROR); 122 } 123 locatorFd.fd = (SLint32) fd; 124 locatorFd.length = SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE; 125 locatorFd.offset = 0; 126 127 mime.formatType = SL_DATAFORMAT_MIME; 128 /* this is how ignored mime information is specified, according to OpenSL ES spec 129 * in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */ 130 mime.mimeType = (SLchar*)NULL; 131 mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED; 132 133 audioSource.pFormat = (void*)&mime; 134 audioSource.pLocator = (void*)&locatorFd; 135 136 /* Create the audio player */ 137 result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 2, 138 iidArray, required); 139 ExitOnError(result); 140 141 /* Realize the player in synchronous mode. */ 142 result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result); 143 fprintf(stdout, "URI example: after Realize\n"); 144 145 /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidStreamTypeItf interfaces for the player*/ 146 result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); 147 ExitOnError(result); 148 149 result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf); 150 ExitOnError(result); 151 152 result = (*player)->GetInterface(player, SL_IID_VIRTUALIZER, (void*)&virtItf); 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 fprintf(stdout, "Starting to play\n"); 182 result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING ); 183 ExitOnError(result); 184 185 /* Configure Virtualizer */ 186 SLboolean strengthSupported = SL_BOOLEAN_FALSE; 187 result = (*virtItf)->IsStrengthSupported(virtItf, &strengthSupported); 188 ExitOnError(result); 189 if (SL_BOOLEAN_FALSE == strengthSupported) { 190 fprintf(stdout, "Virtualizer strength is not supported on this platform. Too bad!\n"); 191 } else { 192 fprintf(stdout, "Virtualizer strength is supported, setting strength to %d\n", virtStrength); 193 result = (*virtItf)->SetStrength(virtItf, virtStrength); 194 ExitOnError(result); 195 } 196 197 SLpermille strength = 0; 198 result = (*virtItf)->GetRoundedStrength(virtItf, &strength); 199 ExitOnError(result); 200 fprintf(stdout, "Rounded strength of virt = %d\n", strength); 201 202 203 /* Switch Virtualizer on/off every TIME_S_BETWEEN_VIRT_ON_OFF seconds */ 204 SLboolean enabled = SL_BOOLEAN_TRUE; 205 result = (*virtItf)->SetEnabled(virtItf, enabled); 206 ExitOnError(result); 207 for(unsigned int j=0 ; j<(durationInMsec/1000*TIME_S_BETWEEN_VIRT_ON_OFF) ; j++) { 208 usleep(TIME_S_BETWEEN_VIRT_ON_OFF * 1000 * 1000); 209 result = (*virtItf)->IsEnabled(virtItf, &enabled); 210 ExitOnError(result); 211 enabled = enabled == SL_BOOLEAN_TRUE ? SL_BOOLEAN_FALSE : SL_BOOLEAN_TRUE; 212 result = (*virtItf)->SetEnabled(virtItf, enabled); 213 if (SL_BOOLEAN_TRUE == enabled) { 214 fprintf(stdout, "Virtualizer on\n"); 215 } else { 216 fprintf(stdout, "Virtualizer off\n"); 217 } 218 ExitOnError(result); 219 } 220 221 /* Make sure player is stopped */ 222 fprintf(stdout, "Stopping playback\n"); 223 result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); 224 ExitOnError(result); 225 226 /* Destroy the player */ 227 (*player)->Destroy(player); 228 229 /* Destroy Output Mix object */ 230 (*outputMix)->Destroy(outputMix); 231 232 close(fd); 233} 234 235//----------------------------------------------------------------- 236int main(int argc, char* const argv[]) 237{ 238 LOGV("Starting %s\n", argv[0]); 239 240 SLresult result; 241 SLObjectItf sl; 242 243 fprintf(stdout, "OpenSL ES test %s: exercises SLVirtualizerItf ", argv[0]); 244 fprintf(stdout, "and AudioPlayer with SLDataLocator_AndroidFD source / OutputMix sink\n"); 245 fprintf(stdout, "Plays the sound file designated by the given path, "); 246 fprintf(stdout, "and applies a virtualization effect of the specified strength,\n"); 247 fprintf(stdout, "where strength is an integer value between 0 and 1000.\n"); 248 fprintf(stdout, "Every %d seconds, the Virtualizer will be turned on and off.\n", 249 TIME_S_BETWEEN_VIRT_ON_OFF); 250 251 if (argc < 3) { 252 fprintf(stdout, "Usage: \t%s path virtualization_strength\n", argv[0]); 253 fprintf(stdout, "Example: \"%s /sdcard/my.mp3 1000\" \n", argv[0]); 254 exit(1); 255 } 256 257 SLEngineOption EngineOption[] = { 258 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 259 }; 260 261 result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 262 ExitOnError(result); 263 264 /* Realizing the SL Engine in synchronous mode. */ 265 result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 266 ExitOnError(result); 267 268 // intentionally not checking that argv[2], the virtualizer strength, is between 0 and 1000 269 TestVirtualizerPathFromFD(sl, argv[1], (int16_t)atoi(argv[2])); 270 271 /* Shutdown OpenSL ES */ 272 (*sl)->Destroy(sl); 273 exit(0); 274 275 return 0; 276} 277