123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- #define LOG_NDEBUG 0
- #define LOG_TAG "slesTestPlayUri"
- #include <utils/Log.h>
- #include <getopt.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <gtest/gtest.h>
- #include <SLES/OpenSLES.h>
- #define MAX_NUMBER_INTERFACES 3
- #define MAX_NUMBER_OUTPUT_DEVICES 6
- const int MP3_DURATION = 71030;
- void CheckErr( SLresult res )
- {
- if ( res != SL_RESULT_SUCCESS ) {
- fprintf(stderr, "%u SL failure, exiting\n", res);
-
- ASSERT_TRUE(false);
- }
- }
- void PrefetchEventCallback( SLPrefetchStatusItf caller, void *pContext __unused, SLuint32 event)
- {
- SLpermille level = 0;
- (*caller)->GetFillLevel(caller, &level);
- SLuint32 status;
- fprintf(stdout, "\t\tPrefetchEventCallback: received event %u\n", event);
- (*caller)->GetPrefetchStatus(caller, &status);
- if ((event & (SL_PREFETCHEVENT_STATUSCHANGE|SL_PREFETCHEVENT_FILLLEVELCHANGE))
- && (level == 0) && (status == SL_PREFETCHSTATUS_UNDERFLOW)) {
- fprintf(stderr, "\t\tPrefetchEventCallback: Error while prefetching data, exiting\n");
- ASSERT_TRUE(false);
- }
- if (event & SL_PREFETCHEVENT_FILLLEVELCHANGE) {
- fprintf(stdout, "\t\tPrefetchEventCallback: Buffer fill level is = %d\n", level);
- }
- if (event & SL_PREFETCHEVENT_STATUSCHANGE) {
- fprintf(stdout, "\t\tPrefetchEventCallback: Prefetch Status is = %u\n", status);
- }
- }
- void TestPlayUri( SLObjectItf sl, const char* path)
- {
- SLEngineItf EngineItf;
- SLresult res;
- SLDataSource audioSource;
- SLDataLocator_URI uri;
- SLDataFormat_MIME mime;
- SLDataSink audioSink;
- SLDataLocator_OutputMix locator_outputmix;
- SLObjectItf player;
- SLPlayItf playItf;
- SLVolumeItf volItf;
- SLPrefetchStatusItf prefetchItf;
- SLObjectItf OutputMix;
- SLboolean required[MAX_NUMBER_INTERFACES];
- SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
-
- res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
- CheckErr(res);
-
- for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
- required[i] = SL_BOOLEAN_FALSE;
- iidArray[i] = SL_IID_NULL;
- }
-
- required[0] = SL_BOOLEAN_TRUE;
- iidArray[0] = SL_IID_VOLUME;
- required[1] = SL_BOOLEAN_TRUE;
- iidArray[1] = SL_IID_PREFETCHSTATUS;
-
- res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0,
- iidArray, required); CheckErr(res);
-
- res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);
- CheckErr(res);
-
- uri.locatorType = SL_DATALOCATOR_URI;
- uri.URI = (SLchar*) path;
- mime.formatType = SL_DATAFORMAT_MIME;
- mime.mimeType = (SLchar*)NULL;
- mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
- audioSource.pFormat = (void *)&mime;
- audioSource.pLocator = (void *)&uri;
-
- locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
- locator_outputmix.outputMix = OutputMix;
- audioSink.pLocator = (void *)&locator_outputmix;
- audioSink.pFormat = NULL;
-
- res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player,
- &audioSource, &audioSink, 2, iidArray, required); CheckErr(res);
-
- res = (*player)->Realize(player, SL_BOOLEAN_FALSE); CheckErr(res);
-
- res = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
- CheckErr(res);
- res = (*player)->GetInterface(player, SL_IID_VOLUME, (void*)&volItf);
- CheckErr(res);
- res = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
- CheckErr(res);
- res = (*prefetchItf)->RegisterCallback(prefetchItf, PrefetchEventCallback, &prefetchItf);
- CheckErr(res);
- res = (*prefetchItf)->SetCallbackEventsMask(prefetchItf,
- SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE);
-
- SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
- res = (*playItf)->GetDuration(playItf, &durationInMsec);
- CheckErr(res);
-
- res = (*volItf)->SetVolumeLevel( volItf, -300);
- CheckErr(res);
-
-
- res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
- CheckErr(res);
-
-
- SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
- SLuint32 timeOutIndex = 100;
- while ((prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) && (timeOutIndex > 0)) {
- usleep(100 * 1000);
- (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
- timeOutIndex--;
- }
- if (timeOutIndex == 0) {
- fprintf(stderr, "Error: Failed to prefetch data in time, exiting\n");
- ASSERT_TRUE(false);
-
- }
-
- res = (*playItf)->GetDuration(playItf, &durationInMsec);
- CheckErr(res);
- if (durationInMsec == SL_TIME_UNKNOWN) {
- fprintf(stderr, "Error: GetDuration returned SL_TIME_UNKNOWN (after prefetch completed)\n");
- ASSERT_TRUE(false);
- }
- SLint32 durationDiffMsec = durationInMsec - MP3_DURATION;
- if (durationDiffMsec < 0) { durationDiffMsec *= -1; }
- if (durationDiffMsec > (MP3_DURATION/20)) {
- fprintf(stderr, "Error: GetDuration returned %d, more than 5percent off from expected %d\n",
- durationInMsec, MP3_DURATION);
- ASSERT_TRUE(false);
- }
- res = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PLAYING );
- CheckErr(res);
-
- usleep(MP3_DURATION * 1000);
-
- SLmillisecond currentPositionInMsec = SL_TIME_UNKNOWN;
- res = (*playItf)->GetPosition(playItf, ¤tPositionInMsec);
- CheckErr(res);
- if (currentPositionInMsec == SL_TIME_UNKNOWN) {
- fprintf(stderr, "Error: GetPosition returns SL_TIME_UNKNOWN after expected duration\n");
- ASSERT_TRUE(false);
- } else if ( currentPositionInMsec <= 0 ||
- currentPositionInMsec > (MP3_DURATION * 1.1) ){
- fprintf(stderr, "Error: GetPosition returns %i, should be expected duration for test\n",
- (int) currentPositionInMsec);
- ASSERT_TRUE(false);
- }
-
- res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
- CheckErr(res);
-
- (*player)->Destroy(player);
-
- (*OutputMix)->Destroy(OutputMix);
- fprintf(stdout, "End of test reached\n");
- }
- class MimeUri: public ::testing::Test {
- public:
- SLresult res;
- SLObjectItf sl;
- protected:
- MimeUri() {
-
- SLEngineOption EngineOption[] = { { (SLuint32) SL_ENGINEOPTION_THREADSAFE,
- (SLuint32) SL_BOOLEAN_TRUE } };
- res = slCreateEngine(&sl, 1, EngineOption, 0, NULL, NULL);
- CheckErr(res);
-
- res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
- CheckErr(res);
- }
- virtual ~MimeUri() {
-
- (*sl)->Destroy(sl);
- }
- virtual void SetUp() {
-
-
- }
- virtual void TearDown() {
-
-
- }
- };
- TEST_F(MimeUri, testPlayAbsPath){
- TestPlayUri(sl, "/sdcard/media_api/music/MP3_256kbps_2ch.mp3");
- }
- TEST_F(MimeUri, testPlayfilePath){
- TestPlayUri(sl, "file:///sdcard/media_api/music/MP3_256kbps_2ch.mp3");
- }
- int main(int argc, char **argv)
- {
- testing::InitGoogleTest(&argc, argv);
- return RUN_ALL_TESTS();
- }
|