slesTestPlayFdPath.cpp revision 4c71179974933c5c36cbfc3e8227c8df63248d91
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_playFdPath" 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 TEST_MUTE 0 37#define TEST_SOLO 1 38 39static int testMode; 40//----------------------------------------------------------------- 41/* Exits the application if an error is encountered */ 42#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__) 43 44void ExitOnErrorFunc( SLresult result , int line) 45{ 46 if (SL_RESULT_SUCCESS != result) { 47 fprintf(stdout, "%lu error code encountered at line %d, exiting\n", result, line); 48 exit(1); 49 } 50} 51 52 53//----------------------------------------------------------------- 54 55/* Play an audio path by opening a file descriptor on that path */ 56void TestPlayPathFromFD( SLObjectItf sl, const char* path, SLAint64 offset, SLAint64 size) 57{ 58 SLresult result; 59 SLEngineItf EngineItf; 60 61 /* Objects this application uses: one player and an ouput mix */ 62 SLObjectItf player, outputMix; 63 64 /* Source of audio data to play */ 65 SLDataSource audioSource; 66 SLDataLocator_AndroidFD locatorFd; 67 SLDataFormat_MIME mime; 68 69 /* Data sinks for the audio player */ 70 SLDataSink audioSink; 71 SLDataLocator_OutputMix locator_outputmix; 72 73 /* Play and PrefetchStatus interfaces for the audio player */ 74 SLPlayItf playItf; 75 SLPrefetchStatusItf prefetchItf; 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 115 /* Setup the data source structure for the URI */ 116 locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD; 117 int fd = open(path, O_RDONLY); 118 if (fd == -1) { 119 ExitOnError(SL_RESULT_RESOURCE_ERROR); 120 } 121 locatorFd.fd = (SLint32) fd; 122 locatorFd.length = size; 123 locatorFd.offset = offset; 124 125 mime.formatType = SL_DATAFORMAT_MIME; 126 /* this is how ignored mime information is specified, according to OpenSL ES spec 127 * in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */ 128 mime.mimeType = (SLchar*)NULL; 129 mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED; 130 131 audioSource.pFormat = (void*)&mime; 132 audioSource.pLocator = (void*)&locatorFd; 133 134 /* Create the audio player */ 135 result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 1, 136 iidArray, required); 137 ExitOnError(result); 138 139 /* Realize the player in synchronous mode. */ 140 result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result); 141 fprintf(stdout, "URI example: after Realize\n"); 142 143 /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidStreamTypeItf interfaces for the player*/ 144 result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); 145 ExitOnError(result); 146 147 result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf); 148 ExitOnError(result); 149 150 fprintf(stdout, "Player configured\n"); 151 152 /* ------------------------------------------------------ */ 153 /* Playback and test */ 154 155 /* Start the data prefetching by setting the player to the paused state */ 156 result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); 157 ExitOnError(result); 158 159 /* Wait until there's data to play */ 160 SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW; 161 while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) { 162 usleep(100 * 1000); 163 (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus); 164 ExitOnError(result); 165 } 166 167 /* Get duration */ 168 SLmillisecond durationInMsec = SL_TIME_UNKNOWN; 169 result = (*playItf)->GetDuration(playItf, &durationInMsec); 170 ExitOnError(result); 171 if (durationInMsec == SL_TIME_UNKNOWN) { 172 durationInMsec = 5000; 173 } 174 175 /* Start playback */ 176 result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING ); 177 ExitOnError(result); 178 179 usleep(durationInMsec * 1000); 180 181 /* Make sure player is stopped */ 182 fprintf(stdout, "Stopping playback\n"); 183 result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); 184 ExitOnError(result); 185 186 /* Destroy the player */ 187 (*player)->Destroy(player); 188 189 /* Destroy Output Mix object */ 190 (*outputMix)->Destroy(outputMix); 191 192 close(fd); 193} 194 195//----------------------------------------------------------------- 196int main(int argc, char* const argv[]) 197{ 198 LOGV("Starting %s\n", argv[0]); 199 200 SLresult result; 201 SLObjectItf sl; 202 203 fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf ", argv[0]); 204 fprintf(stdout, "and AudioPlayer with SLDataLocator_AndroidFD source / OutputMix sink\n"); 205 fprintf(stdout, "Plays the sound file designated by the given path, "); 206 fprintf(stdout, "starting at the specified offset, and using the specified length.\n"); 207 fprintf(stdout, "Omit the length of the file for it to be computed by the system.\n"); 208 209 if (argc < 3) { 210 fprintf(stdout, "Usage: \t%s path offsetInBytes [sizeInBytes]\n", argv[0]); 211 fprintf(stdout, "Example: \"%s /sdcard/my.mp3 0 344460\" \n", argv[0]); 212 exit(1); 213 } 214 215 SLEngineOption EngineOption[] = { 216 {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE} 217 }; 218 219 result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 220 ExitOnError(result); 221 222 /* Realizing the SL Engine in synchronous mode. */ 223 result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 224 ExitOnError(result); 225 226 if (argc == 3) { 227 fprintf(stdout, "\nno file size given, using SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE\n\n"); 228 TestPlayPathFromFD(sl, argv[1], (SLAint64)atoi(argv[2]), 229 SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE); 230 } else { 231 TestPlayPathFromFD(sl, argv[1], (SLAint64)atoi(argv[2]), (SLAint64)atoi(argv[3])); 232 } 233 234 /* Shutdown OpenSL ES */ 235 (*sl)->Destroy(sl); 236 exit(0); 237 238 return 0; 239} 240