summaryrefslogtreecommitdiffstats
path: root/opensles/tests/automated/BufferQueue_test.cpp
blob: aff85473bb4329b8af0148b3e58a7a9665802b3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** \file BufferQueue_test.cpp */

#define LOG_NDEBUG 0
#define LOG_TAG "BufferQueue_test"

#ifdef ANDROID
#include <utils/Log.h>
#else
#define LOGV printf
#endif

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "SLES/OpenSLES.h"
#include "SLES/OpenSLESUT.h"
#include <gtest/gtest.h>

typedef struct {
    short left;
    short right;
} stereo;

// volume of sine wave in range 0.0 to 1.0
static float gVolume = 1.0f;

// 1 second of stereo audio at 44.1 kHz
static stereo stereoBuffer1[44100 * 1];
static const SLuint32 invalidNumBuffers[] = { 0, 0xFFFFFFFF, 0x80000000, 0x10002, 0x102,
        0x101, 0x100 };
static const SLuint32 validNumBuffers[] = { 1, 2, 3, 4, 5, 6, 7, 8, 255 };

//-----------------------------------------------------------------
/* Checks for error. If any errors exit the application! */
void CheckErr(SLresult res) {
    if (SL_RESULT_SUCCESS != res) {
        const char *str = slesutResultToString(res);
        if (NULL == str)
            str = "unknown";
        fprintf(stderr, "CheckErr failure: %s (0x%lx), exiting\n", str, res);
        //Fail the test case
        FAIL();
    }
}

static const SLInterfaceID ids[1] = { SL_IID_BUFFERQUEUE };
static const SLboolean flags[1] = { SL_BOOLEAN_TRUE };
static const SLInterfaceID ids_mutesolo[2] = { SL_IID_BUFFERQUEUE, SL_IID_MUTESOLO };
static const SLboolean flags_mutesolo[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
static const SLInterfaceID ids_seek[2] = { SL_IID_BUFFERQUEUE, SL_IID_SEEK };
static const SLboolean flags_seek[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };

// The fixture for testing class BufferQueue
class TestBufferQueue: public ::testing::Test {
public:
    SLresult res;
    SLObjectItf outputmixObject;
    SLObjectItf engineObject;

    SLDataSource audiosrc;
    SLDataSink audiosnk;
    SLDataFormat_PCM pcm;
    SLDataLocator_OutputMix locator_outputmix;
    SLDataLocator_BufferQueue locator_bufferqueue;
    SLBufferQueueItf playerBufferQueue;
    SLBufferQueueState bufferqueueState;
    SLPlayItf playerPlay;
    SLObjectItf playerObject;
    SLEngineItf engineEngine;
    SLuint32 playerState;

protected:
    TestBufferQueue() {
    }

    virtual ~TestBufferQueue() {

    }

    /*Test setup*/
    virtual void SetUp() {

        // create engine
        res = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
        CheckErr(res);
        res = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
        CheckErr(res);
        res = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
        CheckErr(res);

        // create output mix
        res = (*engineEngine)->CreateOutputMix(engineEngine, &outputmixObject, 0, NULL, NULL);
        CheckErr(res);
        res = (*outputmixObject)->Realize(outputmixObject, SL_BOOLEAN_FALSE);
        CheckErr(res);

        locator_bufferqueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
        locator_bufferqueue.numBuffers = 0;
        locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
        locator_outputmix.outputMix = outputmixObject;

        pcm.formatType = SL_DATAFORMAT_PCM;
        pcm.numChannels = 2;
        pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
        pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
        pcm.containerSize = 16;
        pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
        pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;

        audiosrc.pLocator = &locator_bufferqueue;
        audiosrc.pFormat = &pcm;
        audiosnk.pLocator = &locator_outputmix;
        audiosnk.pFormat = NULL;

        // initialize the test tone to be a sine sweep from 441 Hz to 882 Hz
        unsigned nframes = sizeof(stereoBuffer1) / sizeof(stereoBuffer1[0]);
        float nframes_ = (float) nframes;
        SLuint32 i;
        for (i = 0; i < nframes; ++i) {
            float i_ = (float) i;
            float pcm_ = sin((i_ * (1.0f + 0.5f * (i_ / nframes_)) * 0.01 * M_PI * 2.0));
            int pcm = (int) (pcm_ * 32766.0 * gVolume);
            ASSERT_TRUE(-32768 <= pcm && pcm <= 32767) << "pcm out of bound " << pcm;
            stereoBuffer1[i].left = pcm;
            stereoBuffer1[nframes - 1 - i].right = pcm;
        }
    }

    virtual void TearDown() {
        // Clean up the mixer and the engine
        // (must be done in that order, and after player destroyed)
        if (outputmixObject){
            (*outputmixObject)->Destroy(outputmixObject);
            outputmixObject = NULL;
        }
        if (engineObject){
            (*engineObject)->Destroy(engineObject);
            engineObject = NULL;
        }
    }

    void DestroyPlayer() {
        if (playerObject){
            //printf("destroy player\n");
            (*playerObject)->Destroy(playerObject);
            playerObject = NULL;
        }
    }

    /* Test case for creating audio player with various invalid values for numBuffers*/
    void InvalidBuffer() {

        for (unsigned i = 0; i < sizeof(invalidNumBuffers) / sizeof(invalidNumBuffers[0]); ++i) {
            SLuint32 numBuffers = invalidNumBuffers[i];

            locator_bufferqueue.numBuffers = numBuffers;
            //printf("create audio player - invalid\n");
            SLresult result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject,
                                                            &audiosrc, &audiosnk, 1, ids, flags);
            ASSERT_EQ(SL_RESULT_PARAMETER_INVALID, result);
            ASSERT_EQ(NULL, playerObject);

        }
    }

    /*Prepare the buffer*/
    void PrepareValidBuffer(SLuint32 numBuffers) {

        locator_bufferqueue.numBuffers = numBuffers;
        //printf("create audio player - valid\n");
        res = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audiosrc, &audiosnk,
                                                1, ids, flags);
        CheckErr(res);
        res = (*playerObject)->Realize(playerObject, SL_BOOLEAN_FALSE);
        CheckErr(res);
        // get the play interface
        res = (*playerObject)->GetInterface(playerObject, SL_IID_PLAY, &playerPlay);
        CheckErr(res);
        // verify that player is initially stopped
        res = (*playerPlay)->GetPlayState(playerPlay, &playerState);
        CheckErr(res);
        ASSERT_EQ(SL_PLAYSTATE_STOPPED, playerState);

        // get the buffer queue interface
        res = (*playerObject)->GetInterface(playerObject, SL_IID_BUFFERQUEUE, &playerBufferQueue);
        CheckErr(res);

        // verify that buffer queue is initially empty
        res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
        CheckErr(res);
        ASSERT_EQ((SLuint32) 0, bufferqueueState.count);
        ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex);
    }

    void EnqueueMaxBuffer(SLuint32 numBuffers) {
        SLuint32 j;

        for (j = 0; j < numBuffers; ++j) {
            res = (*playerBufferQueue)->Enqueue(playerBufferQueue, "test", 4);
            CheckErr(res);
            // verify that each buffer is enqueued properly and increments the buffer count
            res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
            CheckErr(res);
            ASSERT_EQ(j + 1, bufferqueueState.count);
            ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex);
        }
    }

    void EnqueueExtraBuffer(SLuint32 numBuffers) {
        // enqueue one more buffer and make sure it fails
        res = (*playerBufferQueue)->Enqueue(playerBufferQueue, "test", 4);
        ASSERT_EQ(SL_RESULT_BUFFER_INSUFFICIENT, res);
        // verify that the failed enqueue did not affect the buffer count
        res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
        CheckErr(res);
        ASSERT_EQ(numBuffers, bufferqueueState.count);
        ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex);
    }

    void SetPlayerState(SLuint32 state) {
        res = (*playerPlay)->SetPlayState(playerPlay, state);
        CheckErr(res);
        //verify the state can set correctly
        GetPlayerState(state);
    }

    void GetPlayerState(SLuint32 state) {
        res = (*playerPlay)->GetPlayState(playerPlay, &playerState);
        CheckErr(res);
        ASSERT_EQ(state, playerState);
    }

    void ClearQueue() {
        // now clear the buffer queue
        res = (*playerBufferQueue)->Clear(playerBufferQueue);
        CheckErr(res);
        // make sure the clear works
        res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
        CheckErr(res);
        ASSERT_EQ((SLuint32) 0, bufferqueueState.count);
        ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex);
    }

    void CheckBufferCount(SLuint32 ExpectedCount, SLuint32 ExpectedPlayIndex) {
        // changing the play state should not affect the buffer count
        res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
        CheckErr(res);
        ASSERT_EQ(ExpectedCount, bufferqueueState.count);
        ASSERT_EQ(ExpectedPlayIndex, bufferqueueState.playIndex);
    }

    void PlayBufferQueue() {
        // enqueue a buffer
        res = (*playerBufferQueue)->Enqueue(playerBufferQueue, stereoBuffer1,
            sizeof(stereoBuffer1));
        CheckErr(res);
        // set play state to playing
        res = (*playerPlay)->SetPlayState(playerPlay, SL_PLAYSTATE_PLAYING);
        CheckErr(res);
        // state should be playing immediately after enqueue
        res = (*playerPlay)->GetPlayState(playerPlay, &playerState);
        CheckErr(res);
        ASSERT_EQ(SL_PLAYSTATE_PLAYING, playerState);
        // buffer should still be on the queue
        res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
        CheckErr(res);
        ASSERT_EQ((SLuint32) 1, bufferqueueState.count);
        ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex);
        //LOGV("Before 1.5 sec");
        // wait 1.5 seconds
        usleep(1500000);
        //LOGV("After 1.5 sec");
        // state should still be playing
        res = (*playerPlay)->GetPlayState(playerPlay, &playerState);
        //LOGV("GetPlayState");
        CheckErr(res);
        ASSERT_EQ(SL_PLAYSTATE_PLAYING, playerState);
        // buffer should be removed from the queue
        res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
        CheckErr(res);
        ASSERT_EQ((SLuint32) 0, bufferqueueState.count);
        ASSERT_EQ((SLuint32) 1, bufferqueueState.playIndex);
        //LOGV("TestEnd");
    }
};

TEST_F(TestBufferQueue, testInvalidBuffer){
    //LOGV("Test Fixture: InvalidBuffer");
    InvalidBuffer();
}

TEST_F(TestBufferQueue, testMuteSolo) {
    // create audio player with buffer queue data source in stereo PCM format and ask for mute solo
    locator_bufferqueue.numBuffers = 1;
    SLresult result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audiosrc,
            &audiosnk, 2, ids_mutesolo, flags_mutesolo);
    ASSERT_EQ(SL_RESULT_SUCCESS, result);
    ASSERT_TRUE(NULL != playerObject);
    DestroyPlayer();
    // create audio player with buffer queue data source in mono PCM format and ask for mute solo
    pcm.numChannels = 1;
    pcm.channelMask = SL_SPEAKER_FRONT_CENTER;
    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audiosrc, &audiosnk,
            2, ids_mutesolo, flags_mutesolo);
    ASSERT_EQ(SL_RESULT_FEATURE_UNSUPPORTED, result);
    ASSERT_EQ(NULL, playerObject);
    DestroyPlayer();
}

TEST_F(TestBufferQueue, testSeek) {
    // can create audio player with buffer queue data source and ask for seek
    locator_bufferqueue.numBuffers = 1;
    SLresult result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject,
                                                    &audiosrc, &audiosnk, 2, ids_seek, flags_seek);
    ASSERT_EQ(SL_RESULT_FEATURE_UNSUPPORTED, result);
    ASSERT_EQ(NULL, playerObject);
    DestroyPlayer();
}

TEST_F(TestBufferQueue, testValidBuffer) {
    for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
        SLuint32 numBuffers = validNumBuffers[i];
        PrepareValidBuffer(numBuffers);
        DestroyPlayer();
    }
}

TEST_F(TestBufferQueue, testEnqueueMaxBuffer) {
    for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
        SLuint32 numBuffers = validNumBuffers[i];
        PrepareValidBuffer(numBuffers);
        EnqueueMaxBuffer(numBuffers);
        DestroyPlayer();
    }
}

TEST_F(TestBufferQueue, testEnqueueExtraBuffer) {
    for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
        SLuint32 numBuffers = validNumBuffers[i];
        PrepareValidBuffer(numBuffers);
        EnqueueMaxBuffer(numBuffers);
        EnqueueExtraBuffer(numBuffers);
        GetPlayerState(SL_PLAYSTATE_STOPPED);
        DestroyPlayer();
    }
}

TEST_F(TestBufferQueue, testEnqueueAtStopped) {
    for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
        SLuint32 numBuffers = validNumBuffers[i];
        PrepareValidBuffer(numBuffers);
        SetPlayerState(SL_PLAYSTATE_STOPPED);
        EnqueueMaxBuffer(numBuffers);
        CheckBufferCount(numBuffers, (SLuint32) 0);
        DestroyPlayer();
    }
}

TEST_F(TestBufferQueue, testEnqueueAtPaused) {
    for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
        SLuint32 numBuffers = validNumBuffers[i];
        PrepareValidBuffer(numBuffers);
        SetPlayerState(SL_PLAYSTATE_PAUSED);
        EnqueueMaxBuffer(numBuffers);
        CheckBufferCount(numBuffers, (SLuint32) 0);
        DestroyPlayer();
    }
}

TEST_F(TestBufferQueue, testClearQueue) {
    for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
        SLuint32 numBuffers = validNumBuffers[i];
        PrepareValidBuffer(numBuffers);
        EnqueueMaxBuffer(numBuffers);
        ClearQueue();
        DestroyPlayer();
    }
}

TEST_F(TestBufferQueue, testStateTransitionEmptyQueue) {
    static const SLuint32 newStates[] = {
        SL_PLAYSTATE_PAUSED,    // paused -> paused
        SL_PLAYSTATE_STOPPED,   // paused -> stopped
        SL_PLAYSTATE_PAUSED,    // stopped -> paused
        SL_PLAYSTATE_PLAYING,   // paused -> playing
        SL_PLAYSTATE_PLAYING,   // playing -> playing
        SL_PLAYSTATE_STOPPED,   // playing -> stopped
        SL_PLAYSTATE_STOPPED,   // stopped -> stopped
        SL_PLAYSTATE_PLAYING,   // stopped -> playing
        SL_PLAYSTATE_PAUSED     // playing -> paused
    };

    for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
        SLuint32 numBuffers = validNumBuffers[i];
        SLuint32 j;

        PrepareValidBuffer(numBuffers);
        /* Set initial state to paused*/
        SetPlayerState(SL_PLAYSTATE_PAUSED);

        for (j = 0; j < sizeof(newStates) / sizeof(newStates[0]); ++j) {
            SetPlayerState(newStates[j]);
            CheckBufferCount((SLuint32) 0, (SLuint32) 0);
        }
        DestroyPlayer();
    }
}

TEST_F(TestBufferQueue, testStateTransitionNonEmptyQueue) {
    static const SLuint32 newStates[] = {
        SL_PLAYSTATE_PAUSED,    // paused -> paused
        SL_PLAYSTATE_STOPPED,   // paused -> stopped
        SL_PLAYSTATE_STOPPED,   // stopped -> stopped
        SL_PLAYSTATE_PAUSED     // stopped -> paused
    };

    for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
        SLuint32 numBuffers = validNumBuffers[i];
        SLuint32 j;

        /* Prepare the player */
        PrepareValidBuffer(numBuffers);
        EnqueueMaxBuffer(numBuffers);
        SetPlayerState(SL_PLAYSTATE_PAUSED);

        for (j = 0; j < sizeof(newStates) / sizeof(newStates[0]); ++j) {
            SetPlayerState(newStates[j]);
            CheckBufferCount(numBuffers, (SLuint32) 0);
        }
        DestroyPlayer();
    }
}

TEST_F(TestBufferQueue, testStatePlayBuffer){
    for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
        SLuint32 numBuffers = validNumBuffers[i];
        PrepareValidBuffer(numBuffers);
        PlayBufferQueue();
        DestroyPlayer();
    }
}

int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
#if 1   // temporary workaround if hardware volume control is not working
    const char *VOLUME = getenv("BufferQueue_test_VOLUME");
    if (NULL != VOLUME) {
        float volume = atof(VOLUME);
        if (volume >= 0.0f && volume <= 1.0f) {
            gVolume = volume;
        }
    }
#endif
    return RUN_ALL_TESTS();
}