1f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi/*
2f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi * Copyright (C) 2010 The Android Open Source Project
3f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi *
4f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
5f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi * you may not use this file except in compliance with the License.
6f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi * You may obtain a copy of the License at
7f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi *
8f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
9f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi *
10f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
11f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
12f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi * See the License for the specific language governing permissions and
14f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi * limitations under the License.
15f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi */
16f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
17f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi#include <stdlib.h>
18f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi#include <stdio.h>
19f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi#include <string.h>
20f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi#include <unistd.h>
21f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi#include <sys/time.h>
22f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi#include <fcntl.h>
23f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
24f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi#include "SLES/OpenSLES.h"
2548e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#ifdef ANDROID
26f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi#include "SLES/OpenSLES_Android.h"
2748e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#endif
28f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
29f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
30f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi#define MAX_NUMBER_INTERFACES 3
31f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
32f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi#define TIME_S_BETWEEN_VIRT_ON_OFF 3
33f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
34f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi//-----------------------------------------------------------------
35f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi/* Exits the application if an error is encountered */
36f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
37f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
38f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivivoid ExitOnErrorFunc( SLresult result , int line)
39f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi{
40f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    if (SL_RESULT_SUCCESS != result) {
41f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line);
424d7c8c742d5b09895e7ce3d07d314b6ada56123dGlenn Kasten        exit(EXIT_FAILURE);
43f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    }
44f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi}
45f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
46f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
47f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi//-----------------------------------------------------------------
48f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
49f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi/* Play an audio path by opening a file descriptor on that path  */
50f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivivoid TestVirtualizerPathFromFD( SLObjectItf sl, const char* path, int16_t virtStrength)
51f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi{
52f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLresult  result;
53f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLEngineItf EngineItf;
54f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
55f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Objects this application uses: one player and an ouput mix */
56f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLObjectItf  player, outputMix;
57f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
58f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Source of audio data to play */
59f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLDataSource            audioSource;
6048e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#ifdef ANDROID
61f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLDataLocator_AndroidFD locatorFd;
6248e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#else
6348e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten    SLDataLocator_URI       locatorUri;
6448e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#endif
65f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLDataFormat_MIME       mime;
66f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
67f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Data sinks for the audio player */
68f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLDataSink               audioSink;
69f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLDataLocator_OutputMix  locator_outputmix;
70f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
71f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Play and PrefetchStatus interfaces for the audio player */
72f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLPlayItf              playItf;
73f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLPrefetchStatusItf    prefetchItf;
74f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLVirtualizerItf       virtItf;
75f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
76f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLboolean required[MAX_NUMBER_INTERFACES];
77f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
78f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
79f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Get the SL Engine Interface which is implicit */
80f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
81f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
82f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
83f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Initialize arrays required[] and iidArray[] */
84f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
85f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        required[i] = SL_BOOLEAN_FALSE;
86f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        iidArray[i] = SL_IID_NULL;
87f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    }
88f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
89f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* ------------------------------------------------------ */
90f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Configuration of the output mix  */
91f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
92f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Create Output Mix object to be used by the player */
934d7c8c742d5b09895e7ce3d07d314b6ada56123dGlenn Kasten     result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 0, iidArray, required);
94f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi     ExitOnError(result);
95f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
96f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Realize the Output Mix object in synchronous mode */
97f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
98f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
99f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
100f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Setup the data sink structure */
101f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
102f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    locator_outputmix.outputMix   = outputMix;
103f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    audioSink.pLocator            = (void*)&locator_outputmix;
104f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    audioSink.pFormat             = NULL;
105f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
106f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* ------------------------------------------------------ */
107f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Configuration of the player  */
108f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
109f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Set arrays required[] and iidArray[] for SLPrefetchStatusItf interfaces */
110f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /*  (SLPlayItf is implicit) */
111f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    required[0] = SL_BOOLEAN_TRUE;
112f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    iidArray[0] = SL_IID_PREFETCHSTATUS;
113f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    required[1] = SL_BOOLEAN_TRUE;
114f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    iidArray[1] = SL_IID_VIRTUALIZER;
115f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
11648e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#ifdef ANDROID
117f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Setup the data source structure for the URI */
118f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD;
119f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    int fd = open(path, O_RDONLY);
120f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    if (fd == -1) {
121f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        ExitOnError(SL_RESULT_RESOURCE_ERROR);
122f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    }
123f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    locatorFd.fd = (SLint32) fd;
124f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    locatorFd.length = SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE;
125f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    locatorFd.offset = 0;
12648e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#else
12748e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten    locatorUri.locatorType = SL_DATALOCATOR_URI;
12848e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten    locatorUri.URI = (SLchar *) path;
12948e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#endif
130f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
131f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    mime.formatType = SL_DATAFORMAT_MIME;
132f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /*     this is how ignored mime information is specified, according to OpenSL ES spec
133f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi     *     in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
134f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    mime.mimeType      = (SLchar*)NULL;
135f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
136f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
137f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    audioSource.pFormat  = (void*)&mime;
13848e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#ifdef ANDROID
139f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    audioSource.pLocator = (void*)&locatorFd;
14048e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#else
14148e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten    audioSource.pLocator = (void*)&locatorUri;
14248e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#endif
143f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
144f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Create the audio player */
145f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 2,
146f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi            iidArray, required);
147f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
148f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
149f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Realize the player in synchronous mode. */
150f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result);
151f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    fprintf(stdout, "URI example: after Realize\n");
152f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
153f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Get the SLPlayItf, SLPrefetchStatusItf and SLAndroidStreamTypeItf interfaces for the player*/
154f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
155f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
156f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
157f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
158f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
159f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
160f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*player)->GetInterface(player, SL_IID_VIRTUALIZER, (void*)&virtItf);
161f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
162f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
163f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    fprintf(stdout, "Player configured\n");
164f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
165f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* ------------------------------------------------------ */
166f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Playback and test */
167f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
168f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Start the data prefetching by setting the player to the paused state */
169f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
170f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
171f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
172f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Wait until there's data to play */
173f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
174f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
175f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        usleep(100 * 1000);
176f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
177f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        ExitOnError(result);
178f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    }
179f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
180f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Get duration */
181f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
182f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*playItf)->GetDuration(playItf, &durationInMsec);
183f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
184f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    if (durationInMsec == SL_TIME_UNKNOWN) {
185f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        durationInMsec = 5000;
186f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    }
187f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
188f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Start playback */
189f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    fprintf(stdout, "Starting to play\n");
190f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING );
191f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
192f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
193f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Configure Virtualizer */
194f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLboolean strengthSupported = SL_BOOLEAN_FALSE;
195f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*virtItf)->IsStrengthSupported(virtItf, &strengthSupported);
196f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
197f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    if (SL_BOOLEAN_FALSE == strengthSupported) {
198f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        fprintf(stdout, "Virtualizer strength is not supported on this platform. Too bad!\n");
199f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    } else {
2001a6bb4f8e738c9387dc9629db294ea5de618a53cGlenn Kasten        fprintf(stdout, "Virtualizer strength is supported, setting strength to %d\n",
2011a6bb4f8e738c9387dc9629db294ea5de618a53cGlenn Kasten                virtStrength);
202f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        result = (*virtItf)->SetStrength(virtItf, virtStrength);
203f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        ExitOnError(result);
204f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    }
205f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
206f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLpermille strength = 0;
207f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*virtItf)->GetRoundedStrength(virtItf, &strength);
208f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
209f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    fprintf(stdout, "Rounded strength of virt = %d\n", strength);
210f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
211f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
212f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Switch Virtualizer on/off every TIME_S_BETWEEN_VIRT_ON_OFF seconds */
213f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLboolean enabled = SL_BOOLEAN_TRUE;
214f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*virtItf)->SetEnabled(virtItf, enabled);
215f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
21634c8a5deb922e414bb348f9c5e6b411866518da8Jean-Michel Trivi    for(unsigned int j=0 ; j<(durationInMsec/(1000*TIME_S_BETWEEN_VIRT_ON_OFF)) ; j++) {
217f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        usleep(TIME_S_BETWEEN_VIRT_ON_OFF * 1000 * 1000);
218f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        result = (*virtItf)->IsEnabled(virtItf, &enabled);
219f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        ExitOnError(result);
220f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        enabled = enabled == SL_BOOLEAN_TRUE ? SL_BOOLEAN_FALSE : SL_BOOLEAN_TRUE;
221f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        result = (*virtItf)->SetEnabled(virtItf, enabled);
222f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        if (SL_BOOLEAN_TRUE == enabled) {
223f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi            fprintf(stdout, "Virtualizer on\n");
224f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        } else {
225f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi            fprintf(stdout, "Virtualizer off\n");
226f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        }
227f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        ExitOnError(result);
228f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    }
229f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
230f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Make sure player is stopped */
231f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    fprintf(stdout, "Stopping playback\n");
232f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
233f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
234f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
235f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Destroy the player */
236f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    (*player)->Destroy(player);
237f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
238f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Destroy Output Mix object */
239f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    (*outputMix)->Destroy(outputMix);
240f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
24148e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#ifdef ANDROID
242f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    close(fd);
24348e85e379e506045aca3a33c2c4cf03b3be6d4b1Glenn Kasten#endif
244f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi}
245f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
246f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi//-----------------------------------------------------------------
247f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Triviint main(int argc, char* const argv[])
248f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi{
249f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLresult    result;
250f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLObjectItf sl;
251f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
252f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    fprintf(stdout, "OpenSL ES test %s: exercises SLVirtualizerItf ", argv[0]);
253f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    fprintf(stdout, "and AudioPlayer with SLDataLocator_AndroidFD source / OutputMix sink\n");
254f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    fprintf(stdout, "Plays the sound file designated by the given path, ");
255f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    fprintf(stdout, "and applies a virtualization effect of the specified strength,\n");
256f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    fprintf(stdout, "where strength is an integer value between 0 and 1000.\n");
257f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    fprintf(stdout, "Every %d seconds, the Virtualizer will be turned on and off.\n",
258f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi            TIME_S_BETWEEN_VIRT_ON_OFF);
259f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
260f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    if (argc < 3) {
261f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        fprintf(stdout, "Usage: \t%s path virtualization_strength\n", argv[0]);
262f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi        fprintf(stdout, "Example: \"%s /sdcard/my.mp3 1000\" \n", argv[0]);
2634d7c8c742d5b09895e7ce3d07d314b6ada56123dGlenn Kasten        exit(EXIT_FAILURE);
264f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    }
265f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
266f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    SLEngineOption EngineOption[] = {
267f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
268f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    };
269f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
270f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
271f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
272f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
273f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Realizing the SL Engine in synchronous mode. */
274f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
275f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    ExitOnError(result);
276f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
277f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    // intentionally not checking that argv[2], the virtualizer strength, is between 0 and 1000
278f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    TestVirtualizerPathFromFD(sl, argv[1], (int16_t)atoi(argv[2]));
279f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
280f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    /* Shutdown OpenSL ES */
281f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi    (*sl)->Destroy(sl);
282f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi
2834d7c8c742d5b09895e7ce3d07d314b6ada56123dGlenn Kasten    return EXIT_SUCCESS;
284f78c9326cc333afd30346602bfbd419a17371b21Jean-Michel Trivi}
285