slesTestPlayUri.cpp revision c2303eb5497c488db786dcb2b8514db229452536
11be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi/* 21be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * Copyright (C) 2010 The Android Open Source Project 31be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * 41be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License"); 51be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * you may not use this file except in compliance with the License. 61be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * You may obtain a copy of the License at 71be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * 81be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * http://www.apache.org/licenses/LICENSE-2.0 91be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * 101be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software 111be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS, 121be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * See the License for the specific language governing permissions and 141be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * limitations under the License. 151be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi */ 161be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 171be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi/* 181be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * Copyright (c) 2009 The Khronos Group Inc. 191be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * 201be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * Permission is hereby granted, free of charge, to any person obtaining a copy of this 211be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * software and /or associated documentation files (the "Materials "), to deal in the 221be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * Materials without restriction, including without limitation the rights to use, copy, 231be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, 241be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * and to permit persons to whom the Materials are furnished to do so, subject to 251be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * the following conditions: 261be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * 271be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * The above copyright notice and this permission notice shall be included 281be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * in all copies or substantial portions of the Materials. 291be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * 301be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 311be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 321be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 331be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 341be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 351be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 361be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS IN THE 371be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi * MATERIALS. 381be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi */ 391be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 401be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi#include <stdlib.h> 411be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi#include <stdio.h> 42c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten//#include <string.h> 431be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi#include <unistd.h> 44c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten//#include <sys/time.h> 451be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 46ee21d26b4b66d0f19b826685b3070497523994d5Glenn Kasten#include "SLES/OpenSLES.h" 471be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 481be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 4943e7743b5297cc5afcb246a7e2b5d825a607f8afJean-Michel Trivi#define MAX_NUMBER_INTERFACES 2 501be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 511be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi//----------------------------------------------------------------- 52603d9aec6b2d96693bf91cba4b0a678027c34572Jean-Michel Trivi//* Exits the application if an error is encountered */ 53603d9aec6b2d96693bf91cba4b0a678027c34572Jean-Michel Trivi#define CheckErr(x) ExitOnErrorFunc(x,__LINE__) 54603d9aec6b2d96693bf91cba4b0a678027c34572Jean-Michel Trivi 55603d9aec6b2d96693bf91cba4b0a678027c34572Jean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line) 561be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi{ 57603d9aec6b2d96693bf91cba4b0a678027c34572Jean-Michel Trivi if (SL_RESULT_SUCCESS != result) { 58ff037a1f697a15fb4249e62fe783f22398572cbeJean-Michel Trivi fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line); 59c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten exit(EXIT_FAILURE); 601be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi } 611be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi} 621be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 6306a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi//----------------------------------------------------------------- 6406a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi/* PrefetchStatusItf callback for an audio player */ 6506a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivivoid PrefetchEventCallback( SLPrefetchStatusItf caller, void *pContext, SLuint32 event) 6606a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi{ 6706a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi SLpermille level = 0; 6806a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi (*caller)->GetFillLevel(caller, &level); 6906a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi SLuint32 status; 70c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten //fprintf(stdout, "PrefetchEventCallback: received event %lu\n", event); 7106a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi (*caller)->GetPrefetchStatus(caller, &status); 7206a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi if ((event & (SL_PREFETCHEVENT_STATUSCHANGE|SL_PREFETCHEVENT_FILLLEVELCHANGE)) 7306a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi && (level == 0) && (status == SL_PREFETCHSTATUS_UNDERFLOW)) { 74c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten fprintf(stdout, "PrefetchEventCallback: Error while prefetching data, exiting\n"); 75c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten //exit(EXIT_FAILURE); 7606a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi } 7706a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi if (event & SL_PREFETCHEVENT_FILLLEVELCHANGE) { 78c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten fprintf(stdout, "PrefetchEventCallback: Buffer fill level is = %d\n", level); 79de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi } 80de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi if (event & SL_PREFETCHEVENT_STATUSCHANGE) { 81c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten fprintf(stdout, "PrefetchEventCallback: Prefetch Status is = %lu\n", status); 8206a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi } 8306a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi 8406a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi} 8506a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi 861be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 871be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi//----------------------------------------------------------------- 881be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 89f8acf4b469cdc9d2fe08fb7f6ca007365efc8bc1Jean-Michel Trivi/* Play some music from a URI */ 901be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivivoid TestPlayUri( SLObjectItf sl, const char* path) 911be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi{ 921be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLEngineItf EngineItf; 931be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 941be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLint32 numOutputs = 0; 951be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLuint32 deviceID = 0; 961be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 971be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLresult res; 981be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 991be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLDataSource audioSource; 1001be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLDataLocator_URI uri; 1011be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLDataFormat_MIME mime; 1021be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1031be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLDataSink audioSink; 1041be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLDataLocator_OutputMix locator_outputmix; 1051be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1061be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLObjectItf player; 1071be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLPlayItf playItf; 1081be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLVolumeItf volItf; 10906a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi SLPrefetchStatusItf prefetchItf; 1101be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1111be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLObjectItf OutputMix; 1121be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1131be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLboolean required[MAX_NUMBER_INTERFACES]; 1141be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLInterfaceID iidArray[MAX_NUMBER_INTERFACES]; 1151be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1161be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Get the SL Engine Interface which is implicit */ 1171be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf); 1181be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi CheckErr(res); 1191be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1201be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Initialize arrays required[] and iidArray[] */ 121f8acf4b469cdc9d2fe08fb7f6ca007365efc8bc1Jean-Michel Trivi for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) { 122f8acf4b469cdc9d2fe08fb7f6ca007365efc8bc1Jean-Michel Trivi required[i] = SL_BOOLEAN_FALSE; 123f8acf4b469cdc9d2fe08fb7f6ca007365efc8bc1Jean-Michel Trivi iidArray[i] = SL_IID_NULL; 124f8acf4b469cdc9d2fe08fb7f6ca007365efc8bc1Jean-Michel Trivi } 1251be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 12606a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi // Set arrays required[] and iidArray[] for VOLUME and PREFETCHSTATUS interface 1271be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi required[0] = SL_BOOLEAN_TRUE; 1281be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi iidArray[0] = SL_IID_VOLUME; 12906a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi required[1] = SL_BOOLEAN_TRUE; 13006a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi iidArray[1] = SL_IID_PREFETCHSTATUS; 1311be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi // Create Output Mix object to be used by player 132c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, 1331be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi iidArray, required); CheckErr(res); 1341be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1351be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi // Realizing the Output Mix object in synchronous mode. 1361be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE); 1371be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi CheckErr(res); 1381be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1391be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Setup the data source structure for the URI */ 1401be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi uri.locatorType = SL_DATALOCATOR_URI; 1411be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi uri.URI = (SLchar*) path; 1421be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi mime.formatType = SL_DATAFORMAT_MIME; 143daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten mime.mimeType = (SLchar*)NULL; 144daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED; 1451be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1461be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi audioSource.pFormat = (void *)&mime; 1471be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi audioSource.pLocator = (void *)&uri; 1481be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1491be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Setup the data sink structure */ 1501be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; 1511be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi locator_outputmix.outputMix = OutputMix; 1521be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi audioSink.pLocator = (void *)&locator_outputmix; 1531be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi audioSink.pFormat = NULL; 1541be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1551be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Create the audio player */ 15643e7743b5297cc5afcb246a7e2b5d825a607f8afJean-Michel Trivi res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 15743e7743b5297cc5afcb246a7e2b5d825a607f8afJean-Michel Trivi MAX_NUMBER_INTERFACES, iidArray, required); CheckErr(res); 1581be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1591be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Realizing the player in synchronous mode. */ 1601be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res); 16143e7743b5297cc5afcb246a7e2b5d825a607f8afJean-Michel Trivi fprintf(stdout, "URI example: after Realize\n"); 1621be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 16306a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi /* Get interfaces */ 1641be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf); 1651be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi CheckErr(res); 1661be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 16706a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi res = (*player)->GetInterface(player, SL_IID_VOLUME, (void*)&volItf); 16806a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi CheckErr(res); 16906a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi 17006a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi res = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf); 17106a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi CheckErr(res); 17206a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi res = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, &prefetchItf); 1731be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi CheckErr(res); 174de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi res = (*prefetchItf)->SetCallbackEventsMask(prefetchItf, 175de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE); 17643e7743b5297cc5afcb246a7e2b5d825a607f8afJean-Michel Trivi CheckErr(res); 177de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi 178ff037a1f697a15fb4249e62fe783f22398572cbeJean-Michel Trivi /* Configure fill level updates every 5 percent */ 179ff037a1f697a15fb4249e62fe783f22398572cbeJean-Michel Trivi (*prefetchItf)->SetFillUpdatePeriod(prefetchItf, 50); 1801be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 181dc181a4a041fe4be7c91b92646b236b6d652f4a3Jean-Michel Trivi /* Display duration */ 182dc181a4a041fe4be7c91b92646b236b6d652f4a3Jean-Michel Trivi SLmillisecond durationInMsec = SL_TIME_UNKNOWN; 183dc181a4a041fe4be7c91b92646b236b6d652f4a3Jean-Michel Trivi res = (*playItf)->GetDuration(playItf, &durationInMsec); 184dc181a4a041fe4be7c91b92646b236b6d652f4a3Jean-Michel Trivi CheckErr(res); 185dc181a4a041fe4be7c91b92646b236b6d652f4a3Jean-Michel Trivi if (durationInMsec == SL_TIME_UNKNOWN) { 18606a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi fprintf(stdout, "Content duration is unknown (before starting to prefetch)\n"); 187dc181a4a041fe4be7c91b92646b236b6d652f4a3Jean-Michel Trivi } else { 18806a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi fprintf(stdout, "Content duration is %lu ms (before starting to prefetch)\n", 18906a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi durationInMsec); 190dc181a4a041fe4be7c91b92646b236b6d652f4a3Jean-Michel Trivi } 191dc181a4a041fe4be7c91b92646b236b6d652f4a3Jean-Michel Trivi 1921be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Set the player volume */ 1931be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi res = (*volItf)->SetVolumeLevel( volItf, -300); 1941be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi CheckErr(res); 1951be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 1961be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Play the URI */ 19706a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi /* first cause the player to prefetch the data */ 198c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten fprintf(stdout, "Before set to PAUSED\n"); 19906a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED ); 200c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten fprintf(stdout, "After set to PAUSED\n"); 20106a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi CheckErr(res); 20206a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi 20306a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi /* wait until there's data to play */ 20406a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi //SLpermille fillLevel = 0; 20506a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW; 206de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi SLuint32 timeOutIndex = 100; // 10s 207de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi while ((prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) && (timeOutIndex > 0)) { 20806a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi usleep(100 * 1000); 20906a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus); 210de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi timeOutIndex--; 211de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi } 212de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi 213de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi if (timeOutIndex == 0) { 214c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten fprintf(stderr, "We\'re done waiting, failed to prefetch data in time, exiting\n"); 215de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi goto destroyRes; 21606a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi } 21706a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi 21806a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi /* Display duration again, */ 21906a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi res = (*playItf)->GetDuration(playItf, &durationInMsec); 22006a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi CheckErr(res); 22106a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi if (durationInMsec == SL_TIME_UNKNOWN) { 22206a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi fprintf(stdout, "Content duration is unknown (after prefetch completed)\n"); 22306a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi } else { 22406a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi fprintf(stdout, "Content duration is %lu ms (after prefetch completed)\n", durationInMsec); 22506a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi } 22606a1b91fb42d3ecc9da725e673b56ca849b9b9a4Jean-Michel Trivi 2271be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi fprintf(stdout, "URI example: starting to play\n"); 2281be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING ); 2291be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi CheckErr(res); 2301be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 231dc181a4a041fe4be7c91b92646b236b6d652f4a3Jean-Michel Trivi /* Wait as long as the duration of the content before stopping */ 232dc181a4a041fe4be7c91b92646b236b6d652f4a3Jean-Michel Trivi usleep(durationInMsec * 1000); 2331be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 2341be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Make sure player is stopped */ 2351be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi fprintf(stdout, "URI example: stopping playback\n"); 2361be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); 2371be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi CheckErr(res); 2381be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 239de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel TrividestroyRes: 240de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi 2411be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Destroy the player */ 2421be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi (*player)->Destroy(player); 2431be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 2441be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Destroy Output Mix object */ 2451be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi (*OutputMix)->Destroy(OutputMix); 2461be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi} 2471be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 2481be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi//----------------------------------------------------------------- 2491be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Triviint main(int argc, char* const argv[]) 2501be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi{ 2511be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLresult res; 2521be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLObjectItf sl; 2531be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 254f8acf4b469cdc9d2fe08fb7f6ca007365efc8bc1Jean-Michel Trivi fprintf(stdout, "OpenSL ES test %s: exercises SLPlayItf, SLVolumeItf ", argv[0]); 255f8acf4b469cdc9d2fe08fb7f6ca007365efc8bc1Jean-Michel Trivi fprintf(stdout, "and AudioPlayer with SLDataLocator_URI source / OutputMix sink\n"); 256de16b4c2bc71c163c7c821a475a53a3b567789b2Jean-Michel Trivi fprintf(stdout, "Plays a sound and stops after its reported duration\n\n"); 257f8acf4b469cdc9d2fe08fb7f6ca007365efc8bc1Jean-Michel Trivi 2581be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi if (argc == 1) { 259c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten fprintf(stdout, "Usage: %s path \n\t%s url\n", argv[0], argv[0]); 26001e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten fprintf(stdout, "Example: \"%s /sdcard/my.mp3\" or \"%s file:///sdcard/my.mp3\"\n", 26101e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten argv[0], argv[0]); 262c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten exit(EXIT_FAILURE); 2631be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi } 2641be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 2651be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi SLEngineOption EngineOption[] = { 2661be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi {(SLuint32) SL_ENGINEOPTION_THREADSAFE, 2671be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi (SLuint32) SL_BOOLEAN_TRUE}}; 2681be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 2691be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi res = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL); 2701be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi CheckErr(res); 2711be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Realizing the SL Engine in synchronous mode. */ 2721be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE); 2731be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi CheckErr(res); 2741be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 2751be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi TestPlayUri(sl, argv[1]); 2761be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 2771be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi /* Shutdown OpenSL ES */ 2781be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi (*sl)->Destroy(sl); 2791be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi 280c2303eb5497c488db786dcb2b8514db229452536Glenn Kasten return EXIT_SUCCESS; 2811be90f9197d7a48e788b5e40fd15d73a877acef0Jean-Michel Trivi} 282