mimeUri_test.cpp revision 5940f26b6c2da96c0f0b2293623ed692d1fd9026
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/* 18 * Copyright (c) 2009 The Khronos Group Inc. 19 * 20 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 21 * software and /or associated documentation files (the "Materials "), to deal in the 22 * Materials without restriction, including without limitation the rights to use, copy, 23 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, 24 * and to permit persons to whom the Materials are furnished to do so, subject to 25 * the following conditions: 26 * 27 * The above copyright notice and this permission notice shall be included 28 * in all copies or substantial portions of the Materials. 29 * 30 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 34 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 35 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 36 * CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS IN THE 37 * MATERIALS. 38 */ 39 40#define LOG_NDEBUG 0 41#define LOG_TAG "slesTestPlayUri" 42 43#include <utils/Log.h> 44#include <getopt.h> 45#include <stdlib.h> 46#include <stdio.h> 47#include <string.h> 48#include <unistd.h> 49#include <sys/time.h> 50#include <gtest/gtest.h> 51#include "SLES/OpenSLES.h" 52 53 54#define MAX_NUMBER_INTERFACES 3 55#define MAX_NUMBER_OUTPUT_DEVICES 6 56 57//The expected playback duration 58const int MP3_DURATION = 75000; //75 secs 59 60 61//----------------------------------------------------------------- 62/* Checks for error. If any errors exit the application! */ 63void CheckErr( SLresult res ) 64{ 65 if ( res != SL_RESULT_SUCCESS ) { 66 fprintf(stderr, "%lu SL failure, exiting\n", res); 67 //Fail the test case 68 ASSERT_TRUE(false); 69 } 70} 71 72//----------------------------------------------------------------- 73/* PrefetchStatusItf callback for an audio player */ 74void PrefetchEventCallback( SLPrefetchStatusItf caller, void *pContext, SLuint32 event) 75{ 76 SLpermille level = 0; 77 (*caller)->GetFillLevel(caller, &level); 78 SLuint32 status; 79 fprintf(stdout, "\t\tPrefetchEventCallback: received event %lu\n", event); 80 (*caller)->GetPrefetchStatus(caller, &status); 81 if ((event & (SL_PREFETCHEVENT_STATUSCHANGE|SL_PREFETCHEVENT_FILLLEVELCHANGE)) 82 && (level == 0) && (status == SL_PREFETCHSTATUS_UNDERFLOW)) { 83 fprintf(stderr, "\t\tPrefetchEventCallback: Error while prefetching data, exiting\n"); 84 ASSERT_TRUE(false); 85 } 86 if (event & SL_PREFETCHEVENT_FILLLEVELCHANGE) { 87 fprintf(stdout, "\t\tPrefetchEventCallback: Buffer fill level is = %d\n", level); 88 } 89 if (event & SL_PREFETCHEVENT_STATUSCHANGE) { 90 fprintf(stdout, "\t\tPrefetchEventCallback: Prefetch Status is = %lu\n", status); 91 } 92 93} 94 95 96//----------------------------------------------------------------- 97 98/* Play some music from a URI */ 99void TestPlayUri( SLObjectItf sl, const char* path) 100{ 101 SLEngineItf EngineItf; 102 103 SLint32 numOutputs = 0; 104 SLuint32 deviceID = 0; 105 106 SLresult res; 107 108 SLDataSource audioSource; 109 SLDataLocator_URI uri; 110 SLDataFormat_MIME mime; 111 112 SLDataSink audioSink; 113 SLDataLocator_OutputMix locator_outputmix; 114 115 SLObjectItf player; 116 SLPlayItf playItf; 117 SLVolumeItf volItf; 118 SLPrefetchStatusItf prefetchItf; 119 120 SLObjectItf OutputMix; 121 122 SLboolean required[MAX_NUMBER_INTERFACES]; 123 SLInterfaceID iidArray[MAX_NUMBER_INTERFACES]; 124 125 /* Get the SL Engine Interface which is implicit */ 126 res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 127 CheckErr(res); 128 129 /* Initialize arrays required[] and iidArray[] */ 130 for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) { 131 required[i] = SL_BOOLEAN_FALSE; 132 iidArray[i] = SL_IID_NULL; 133 } 134 135 // Set arrays required[] and iidArray[] for VOLUME and PREFETCHSTATUS interface 136 required[0] = SL_BOOLEAN_TRUE; 137 iidArray[0] = SL_IID_VOLUME; 138 required[1] = SL_BOOLEAN_TRUE; 139 iidArray[1] = SL_IID_PREFETCHSTATUS; 140 // Create Output Mix object to be used by player 141 res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 1, 142 iidArray, required); CheckErr(res); 143 144 // Realizing the Output Mix object in synchronous mode. 145 res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE); 146 CheckErr(res); 147 148 /* Setup the data source structure for the URI */ 149 uri.locatorType = SL_DATALOCATOR_URI; 150 uri.URI = (SLchar*) path; 151 mime.formatType = SL_DATAFORMAT_MIME; 152 mime.mimeType = (SLchar*)NULL; 153 mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED; 154 155 audioSource.pFormat = (void *)&mime; 156 audioSource.pLocator = (void *)&uri; 157 158 /* Setup the data sink structure */ 159 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; 160 locator_outputmix.outputMix = OutputMix; 161 audioSink.pLocator = (void *)&locator_outputmix; 162 audioSink.pFormat = NULL; 163 164 /* Create the audio player */ 165 res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, 166 &audioSource, &audioSink, 2, iidArray, required); CheckErr(res); 167 168 /* Realizing the player in synchronous mode. */ 169 res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res); 170 171 /* Get interfaces */ 172 res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); 173 CheckErr(res); 174 175 res = (*player)->GetInterface(player, SL_IID_VOLUME, (void*)&volItf); 176 CheckErr(res); 177 178 res = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf); 179 CheckErr(res); 180 res = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, &prefetchItf); 181 CheckErr(res); 182 res = (*prefetchItf)->SetCallbackEventsMask(prefetchItf, 183 SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE); 184 185 /* Display duration */ 186 SLmillisecond durationInMsec = SL_TIME_UNKNOWN; 187 res = (*playItf)->GetDuration(playItf, &durationInMsec); 188 CheckErr(res); 189 if (durationInMsec == SL_TIME_UNKNOWN) { 190 fprintf(stderr, "Content duration is unknown (before starting to prefetch)\n"); 191 ASSERT_TRUE(false); 192 } 193 194 /* Set the player volume */ 195 res = (*volItf)->SetVolumeLevel( volItf, -300); 196 CheckErr(res); 197 198 /* Play the URI */ 199 /* first cause the player to prefetch the data */ 200 fprintf(stdout, "\nbefore set to PAUSED\n\n"); 201 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); 202 fprintf(stdout, "\nafter set to PAUSED\n\n"); 203 CheckErr(res); 204 205 /* wait until there's data to play */ 206 //SLpermille fillLevel = 0; 207 SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW; 208 SLuint32 timeOutIndex = 100; // 10s 209 while ((prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) && (timeOutIndex > 0)) { 210 usleep(100 * 1000); 211 (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus); 212 timeOutIndex--; 213 } 214 215 if (timeOutIndex == 0) { 216 fprintf(stderr, "We\'re done here, failed to prefetch data in time, exiting\n"); 217 ASSERT_TRUE(false); 218 // goto destroyRes; 219 } 220 221 /* Display duration again, */ 222 res = (*playItf)->GetDuration(playItf, &durationInMsec); 223 CheckErr(res); 224 if (durationInMsec == SL_TIME_UNKNOWN) { 225 fprintf(stderr, "Content duration is unknown (after prefetch completed)\n"); 226 ASSERT_TRUE(false); 227 } 228 229 res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING ); 230 CheckErr(res); 231 232 /* Play for the song duration*/ 233 usleep(MP3_DURATION * 1000); 234 235 /* Validate the play position*/ 236 SLmillisecond currentPositionInMsec = SL_TIME_UNKNOWN; 237 res = (*playItf)->GetPosition(playItf, ¤tPositionInMsec); 238 CheckErr(res); 239 if (currentPositionInMsec == SL_TIME_UNKNOWN) { 240 fprintf(stderr, "GetPosition returns UNKNOWN\n"); 241 ASSERT_TRUE(false); 242 } else if ( currentPositionInMsec <= 0 || 243 currentPositionInMsec > (MP3_DURATION * 1.1) ){ 244 fprintf(stderr, "Current play position is %i : beyond the duration\n", 245 (int) currentPositionInMsec); 246 ASSERT_TRUE(false); 247 } 248 249 /* Make sure player is stopped */ 250 fprintf(stdout, "URI example: stopping playback\n"); 251 res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); 252 CheckErr(res); 253 254destroyRes: 255 256 /* Destroy the player */ 257 (*player)->Destroy(player); 258 259 /* Destroy Output Mix object */ 260 (*OutputMix)->Destroy(OutputMix); 261} 262 263 264// The fixture for testing class MimeUri 265class MimeUri: public ::testing::Test { 266public: 267 SLresult res; 268 SLObjectItf sl; 269 270protected: 271 MimeUri() { 272 // You can do set-up work for each test here. 273 SLEngineOption EngineOption[] = { { (SLuint32) SL_ENGINEOPTION_THREADSAFE, 274 (SLuint32) SL_BOOLEAN_TRUE } }; 275 276 res = slCreateEngine(&sl, 1, EngineOption, 0, NULL, NULL); 277 CheckErr(res); 278 279 /* Realizing the SL Engine in synchronous mode. */ 280 res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 281 CheckErr(res); 282 } 283 284 virtual ~MimeUri() { 285 // You can do clean-up work that doesn't throw exceptions here. 286 (*sl)->Destroy(sl); 287 } 288 289 virtual void SetUp() { 290 // Code here will be called immediately after the constructor (right 291 // before each test). 292 293 } 294 295 virtual void TearDown() { 296 // Code here will be called immediately after each test (right 297 // before the destructor). 298 299 } 300}; 301 302TEST_F(MimeUri, testPlayAbsPath){ 303 TestPlayUri(sl, "/sdcard/media_api/music/MP3_256kbps_2ch.mp3"); 304} 305 306TEST_F(MimeUri, testPlayfilePath){ 307 TestPlayUri(sl, "file:///sdcard/media_api/music/MP3_256kbps_2ch.mp3"); 308} 309 310//----------------------------------------------------------------- 311int main(int argc, char **argv) 312{ 313 testing::InitGoogleTest(&argc, argv); 314 return RUN_ALL_TESTS(); 315} 316