slesTest_playMuteSolo.cpp revision 85bae78453492b42c7095cafc404a049e52bbc22
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_playMuteSolo" 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 "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/* PlayItf callback for an audio player, will be called for every SL_PLAYEVENT_HEADATNEWPOS event */ 52void PlayEventCallback( SLPlayItf caller, void *pContext, SLuint32 event) 53{ 54 SLMuteSoloItf muteSolo = (SLMuteSoloItf)pContext; 55 SLuint8 numChannels = 0; 56 SLresult res = (*muteSolo)->GetNumChannels(muteSolo, &numChannels); ExitOnError(res); 57 //fprintf(stdout, "Content has %d channel(s)\n", numChannels); 58 59 switch (testMode) { 60 case TEST_MUTE: { 61 //--------------------------------------------------- 62 if (numChannels > 1) { // SLMuteSoloItf only works if more than one channel 63 SLboolean leftMuted = SL_BOOLEAN_TRUE; 64 res = (*muteSolo)->GetChannelMute(muteSolo, 0, &leftMuted); ExitOnError(res); 65 // swap channel mute 66 res = (*muteSolo)->SetChannelMute(muteSolo, 0, 67 leftMuted == SL_BOOLEAN_TRUE ? SL_BOOLEAN_FALSE : SL_BOOLEAN_TRUE); 68 ExitOnError(res); 69 res = (*muteSolo)->SetChannelMute(muteSolo, 1, 70 leftMuted == SL_BOOLEAN_TRUE ? SL_BOOLEAN_TRUE : SL_BOOLEAN_FALSE); 71 ExitOnError(res); 72 if (leftMuted == SL_BOOLEAN_TRUE) { // we've swapped the channel mute above 73 fprintf(stdout, "channel 0: playing, channel 1: muted\n"); 74 } else { 75 fprintf(stdout, "channel 0: muted, channel 1: playing\n"); 76 } 77 } 78 } break; 79 80 case TEST_SOLO: { 81 //--------------------------------------------------- 82 if (numChannels > 1) { // SLMuteSoloItf only works if more than one channel 83 SLboolean leftSoloed = SL_BOOLEAN_TRUE; 84 res = (*muteSolo)->GetChannelSolo(muteSolo, 0, &leftSoloed); ExitOnError(res); 85 // swap channel solo 86 res = (*muteSolo)->SetChannelSolo(muteSolo, 0, 87 leftSoloed == SL_BOOLEAN_TRUE ? SL_BOOLEAN_FALSE : SL_BOOLEAN_TRUE); 88 ExitOnError(res); 89 res = (*muteSolo)->SetChannelSolo(muteSolo, 1, 90 leftSoloed == SL_BOOLEAN_TRUE ? SL_BOOLEAN_TRUE : SL_BOOLEAN_FALSE); 91 ExitOnError(res); 92 if (leftSoloed == SL_BOOLEAN_TRUE) { // we've swapped the channel solo above 93 fprintf(stdout, "channel 0: normal, channel 1: soloed\n"); 94 } else { 95 fprintf(stdout, "channel 0: soloed, channel 1: normal\n"); 96 } 97 } 98 } break; 99 100 default: 101 break; 102 } 103} 104 105//----------------------------------------------------------------- 106 107/* Play an audio URIs, mute and solo channels */ 108void TestPlayUri( SLObjectItf sl, const char* path) 109{ 110 SLresult result; 111 SLEngineItf EngineItf; 112 113 /* Objects this application uses: one player and an ouput mix */ 114 SLObjectItf player, outputMix; 115 116 /* Source of audio data to play */ 117 SLDataSource audioSource; 118 SLDataLocator_URI uri; 119 SLDataFormat_MIME mime; 120 121 /* Data sinks for the audio player */ 122 SLDataSink audioSink; 123 SLDataLocator_OutputMix locator_outputmix; 124 125 /* Play, Volume and PrefetchStatus interfaces for the audio player */ 126 SLPlayItf playItf; 127 SLMuteSoloItf muteSoloItf; 128 SLPrefetchStatusItf prefetchItf; 129 130 SLboolean required[MAX_NUMBER_INTERFACES]; 131 SLInterfaceID iidArray[MAX_NUMBER_INTERFACES]; 132 133 /* Get the SL Engine Interface which is implicit */ 134 result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 135 ExitOnError(result); 136 137 /* Initialize arrays required[] and iidArray[] */ 138 for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) { 139 required[i] = SL_BOOLEAN_FALSE; 140 iidArray[i] = SL_IID_NULL; 141 } 142 143 /* ------------------------------------------------------ */ 144 /* Configuration of the output mix */ 145 146 /* Create Output Mix object to be used by the player */ 147 result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 1, iidArray, required); 148 ExitOnError(result); 149 150 /* Realize the Output Mix object in synchronous mode */ 151 result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE); 152 ExitOnError(result); 153 154 /* Setup the data sink structure */ 155 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; 156 locator_outputmix.outputMix = outputMix; 157 audioSink.pLocator = (void*)&locator_outputmix; 158 audioSink.pFormat = NULL; 159 160 /* ------------------------------------------------------ */ 161 /* Configuration of the player */ 162 163 /* Set arrays required[] and iidArray[] for SLMuteSoloItf and SLPrefetchStatusItf interfaces */ 164 /* (SLPlayItf is implicit) */ 165 required[0] = SL_BOOLEAN_TRUE; 166 iidArray[0] = SL_IID_MUTESOLO; 167 required[1] = SL_BOOLEAN_TRUE; 168 iidArray[1] = SL_IID_PREFETCHSTATUS; 169 170 /* Setup the data source structure for the URI */ 171 uri.locatorType = SL_DATALOCATOR_URI; 172 uri.URI = (SLchar*) path; 173 mime.formatType = SL_DATAFORMAT_MIME; 174 /* this is how ignored mime information is specified, according to OpenSL ES spec 175 * in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */ 176 mime.mimeType = (SLchar*)NULL; 177 mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED; 178 179 audioSource.pFormat = (void*)&mime; 180 audioSource.pLocator = (void*)&uri; 181 182 /* Create the audio player */ 183 result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 1, 184 iidArray, required); 185 ExitOnError(result); 186 187 /* Realize the player in synchronous mode. */ 188 result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result); 189 fprintf(stdout, "URI example: after Realize\n"); 190 191 /* Get the SLPlayItf, SLPrefetchStatusItf and SLMuteSoloItf interfaces for the player */ 192 result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); 193 ExitOnError(result); 194 195 result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf); 196 ExitOnError(result); 197 198 result = (*player)->GetInterface(player, SL_IID_MUTESOLO, (void*)&muteSoloItf); 199 ExitOnError(result); 200 201 /* Setup to receive playback events on position updates */ 202 result = (*playItf)->RegisterCallback(playItf, PlayEventCallback, (void*)muteSoloItf); 203 ExitOnError(result); 204 result = (*playItf)->SetCallbackEventsMask(playItf, SL_PLAYEVENT_HEADATNEWPOS); 205 ExitOnError(result); 206 result = (*playItf)->SetPositionUpdatePeriod(playItf, 1000); 207 ExitOnError(result); 208 209 fprintf(stdout, "Player configured\n"); 210 211 /* ------------------------------------------------------ */ 212 /* Playback and test */ 213 214 /* Start the data prefetching by setting the player to the paused state */ 215 result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); 216 ExitOnError(result); 217 218 /* Wait until there's data to play */ 219 SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW; 220 while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) { 221 usleep(100 * 1000); 222 (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus); 223 } 224 225 /* Query the number of channels */ 226 SLuint8 numChannels = 0; 227 result = (*muteSoloItf)->GetNumChannels(muteSoloItf, &numChannels); 228 ExitOnError(result); 229 fprintf(stdout, "Content has %d channel(s)\n", numChannels); 230 231 if (numChannels == 1) { 232 fprintf(stdout, "SLMuteSolotItf only works one content with more than one channel. Bye\n"); 233 goto destroyKillKill; 234 } else { 235 /* Mute left channel */ 236 result = (*muteSoloItf)->SetChannelMute(muteSoloItf, 0, SL_BOOLEAN_TRUE); ExitOnError(result); 237 result = (*muteSoloItf)->SetChannelMute(muteSoloItf, 1, SL_BOOLEAN_FALSE); ExitOnError(result); 238 } 239 240 /* Run the test for 10s */ 241 /* see PlayEventCallback() for more of the test of the SLMuteSoloItf interface */ 242 fprintf(stdout, "\nTesting mute functionality:\n"); 243 testMode = TEST_MUTE; 244 result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING ); ExitOnError(result); 245 usleep( 5 * 1000 * 1000); 246 result = (*muteSoloItf)->SetChannelMute(muteSoloItf, 0, SL_BOOLEAN_FALSE); ExitOnError(result); 247 result = (*muteSoloItf)->SetChannelMute(muteSoloItf, 1, SL_BOOLEAN_FALSE); ExitOnError(result); 248 fprintf(stdout, "\nTesting solo functionality:\n"); 249 testMode = TEST_SOLO; 250 usleep( 5 * 1000 * 1000); 251 252 /* Make sure player is stopped */ 253 fprintf(stdout, "Stopping playback\n"); 254 result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); 255 ExitOnError(result); 256 257destroyKillKill: 258 259 /* Destroy the players */ 260 (*player)->Destroy(player); 261 262 /* Destroy Output Mix object */ 263 (*outputMix)->Destroy(outputMix); 264} 265 266//----------------------------------------------------------------- 267int main(int argc, char* const argv[]) 268{ 269 LOGV("Starting %s\n", argv[0]); 270 271 SLresult result; 272 SLObjectItf sl; 273 274 fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLVolumeItf, SLMuteSoloItf\n", 275 argv[0]); 276 fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n"); 277 fprintf(stdout, "Plays a sound and alternates the muting of the channels (for 5s).\n"); 278 fprintf(stdout, " and then alternates the solo\'ing of the channels (for 5s).\n"); 279 fprintf(stdout, "Stops after 10s\n"); 280 281 if (argc == 1) { 282 fprintf(stdout, "Usage: \t%s url\n", argv[0]); 283 fprintf(stdout, "Example: \"%s /sdcard/my.mp3\" ", argv[0]); 284 exit(1); 285 } 286 287 SLEngineOption EngineOption[] = { 288 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 289 }; 290 291 result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 292 ExitOnError(result); 293 294 /* Realizing the SL Engine in synchronous mode. */ 295 result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 296 ExitOnError(result); 297 298 if (argc > 1) { 299 TestPlayUri(sl, argv[1]); 300 } 301 302 /* Shutdown OpenSL ES */ 303 (*sl)->Destroy(sl); 304 exit(0); 305 306 return 0; 307} 308