blob: 123e481aed447824e5d9d7deef7e712ffebea292 [file] [log] [blame]
Glenn Kastena15af1c2010-07-29 18:22:23 -07001/*
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/** \file BufferQueue_test.cpp */
Glenn Kastenb12deb52010-08-27 15:35:35 -070018
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070019#define LOG_NDEBUG 0
Glenn Kastenb12deb52010-08-27 15:35:35 -070020#define LOG_TAG "BufferQueue_test"
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070021
22#ifdef ANDROID
23#include <utils/Log.h>
24#else
Steve Blockde7c7da2011-10-26 11:12:08 +010025#define ALOGV printf
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070026#endif
Glenn Kastena15af1c2010-07-29 18:22:23 -070027
28#include <assert.h>
29#include <math.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <unistd.h>
Glenn Kastenc6853892011-07-19 11:16:07 -070033#include <SLES/OpenSLES.h>
Glenn Kastenbe221892011-01-05 08:03:38 -080034#include "OpenSLESUT.h"
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070035#include <gtest/gtest.h>
36
Glenn Kastena15af1c2010-07-29 18:22:23 -070037typedef struct {
38 short left;
39 short right;
40} stereo;
41
Glenn Kasten04c73542010-09-24 14:16:15 -070042// volume of sine wave in range 0.0 to 1.0
43static float gVolume = 1.0f;
44
Glenn Kastena15af1c2010-07-29 18:22:23 -070045// 1 second of stereo audio at 44.1 kHz
46static stereo stereoBuffer1[44100 * 1];
Glenn Kastenb12deb52010-08-27 15:35:35 -070047static const SLuint32 invalidNumBuffers[] = { 0, 0xFFFFFFFF, 0x80000000, 0x10002, 0x102,
48 0x101, 0x100 };
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070049static const SLuint32 validNumBuffers[] = { 1, 2, 3, 4, 5, 6, 7, 8, 255 };
Glenn Kastena15af1c2010-07-29 18:22:23 -070050
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070051//-----------------------------------------------------------------
52/* Checks for error. If any errors exit the application! */
53void CheckErr(SLresult res) {
Glenn Kastenb12deb52010-08-27 15:35:35 -070054 if (SL_RESULT_SUCCESS != res) {
Glenn Kasten7126c252010-10-13 09:52:53 -070055 const char *str = slesutResultToString(res);
56 if (NULL == str)
57 str = "unknown";
Glenn Kasten58432eb2011-06-13 11:30:22 -070058 fprintf(stderr, "CheckErr failure: %s (0x%x), exiting\n", str, res);
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070059 //Fail the test case
Glenn Kastenb12deb52010-08-27 15:35:35 -070060 FAIL();
Glenn Kastena15af1c2010-07-29 18:22:23 -070061 }
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070062}
Glenn Kastena15af1c2010-07-29 18:22:23 -070063
Glenn Kastenb12deb52010-08-27 15:35:35 -070064static const SLInterfaceID ids[1] = { SL_IID_BUFFERQUEUE };
65static const SLboolean flags[1] = { SL_BOOLEAN_TRUE };
Glenn Kasten104c0002010-10-06 09:52:44 -070066static const SLInterfaceID ids_mutesolo[2] = { SL_IID_BUFFERQUEUE, SL_IID_MUTESOLO };
67static const SLboolean flags_mutesolo[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
68static const SLInterfaceID ids_seek[2] = { SL_IID_BUFFERQUEUE, SL_IID_SEEK };
69static const SLboolean flags_seek[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
Glenn Kastenb12deb52010-08-27 15:35:35 -070070
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070071// The fixture for testing class BufferQueue
72class TestBufferQueue: public ::testing::Test {
73public:
74 SLresult res;
Glenn Kastena15af1c2010-07-29 18:22:23 -070075 SLObjectItf outputmixObject;
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070076 SLObjectItf engineObject;
Glenn Kastena15af1c2010-07-29 18:22:23 -070077
Glenn Kastena15af1c2010-07-29 18:22:23 -070078 SLDataSource audiosrc;
79 SLDataSink audiosnk;
80 SLDataFormat_PCM pcm;
81 SLDataLocator_OutputMix locator_outputmix;
82 SLDataLocator_BufferQueue locator_bufferqueue;
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070083 SLBufferQueueItf playerBufferQueue;
84 SLBufferQueueState bufferqueueState;
85 SLPlayItf playerPlay;
Glenn Kastena15af1c2010-07-29 18:22:23 -070086 SLObjectItf playerObject;
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070087 SLEngineItf engineEngine;
88 SLuint32 playerState;
Glenn Kastena15af1c2010-07-29 18:22:23 -070089
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070090protected:
91 TestBufferQueue() {
Glenn Kastena15af1c2010-07-29 18:22:23 -070092 }
93
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070094 virtual ~TestBufferQueue() {
95
96 }
97
Glenn Kastenb12deb52010-08-27 15:35:35 -070098 /*Test setup*/
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -070099 virtual void SetUp() {
Glenn Kastenb12deb52010-08-27 15:35:35 -0700100
101 // create engine
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700102 res = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
103 CheckErr(res);
104 res = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
105 CheckErr(res);
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700106 res = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
107 CheckErr(res);
108
109 // create output mix
110 res = (*engineEngine)->CreateOutputMix(engineEngine, &outputmixObject, 0, NULL, NULL);
111 CheckErr(res);
112 res = (*outputmixObject)->Realize(outputmixObject, SL_BOOLEAN_FALSE);
113 CheckErr(res);
114
115 locator_bufferqueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
116 locator_bufferqueue.numBuffers = 0;
117 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
118 locator_outputmix.outputMix = outputmixObject;
119
120 pcm.formatType = SL_DATAFORMAT_PCM;
121 pcm.numChannels = 2;
122 pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
123 pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
124 pcm.containerSize = 16;
125 pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
126 pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
Glenn Kastenb12deb52010-08-27 15:35:35 -0700127
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700128 audiosrc.pLocator = &locator_bufferqueue;
129 audiosrc.pFormat = &pcm;
130 audiosnk.pLocator = &locator_outputmix;
131 audiosnk.pFormat = NULL;
132
133 // initialize the test tone to be a sine sweep from 441 Hz to 882 Hz
134 unsigned nframes = sizeof(stereoBuffer1) / sizeof(stereoBuffer1[0]);
135 float nframes_ = (float) nframes;
136 SLuint32 i;
137 for (i = 0; i < nframes; ++i) {
138 float i_ = (float) i;
139 float pcm_ = sin((i_ * (1.0f + 0.5f * (i_ / nframes_)) * 0.01 * M_PI * 2.0));
Glenn Kasten04c73542010-09-24 14:16:15 -0700140 int pcm = (int) (pcm_ * 32766.0 * gVolume);
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700141 ASSERT_TRUE(-32768 <= pcm && pcm <= 32767) << "pcm out of bound " << pcm;
142 stereoBuffer1[i].left = pcm;
143 stereoBuffer1[nframes - 1 - i].right = pcm;
144 }
145 }
146
147 virtual void TearDown() {
Glenn Kastend48ff332010-09-01 09:22:23 -0700148 // Clean up the mixer and the engine
149 // (must be done in that order, and after player destroyed)
Glenn Kasten4597a742010-08-26 10:46:17 -0700150 if (outputmixObject){
151 (*outputmixObject)->Destroy(outputmixObject);
152 outputmixObject = NULL;
153 }
154 if (engineObject){
155 (*engineObject)->Destroy(engineObject);
156 engineObject = NULL;
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700157 }
158 }
159
Glenn Kastend48ff332010-09-01 09:22:23 -0700160 void DestroyPlayer() {
161 if (playerObject){
162 //printf("destroy player\n");
163 (*playerObject)->Destroy(playerObject);
164 playerObject = NULL;
165 }
166 }
167
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700168 /* Test case for creating audio player with various invalid values for numBuffers*/
169 void InvalidBuffer() {
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700170
Glenn Kastenb12deb52010-08-27 15:35:35 -0700171 for (unsigned i = 0; i < sizeof(invalidNumBuffers) / sizeof(invalidNumBuffers[0]); ++i) {
172 SLuint32 numBuffers = invalidNumBuffers[i];
173
174 locator_bufferqueue.numBuffers = numBuffers;
Glenn Kastend48ff332010-09-01 09:22:23 -0700175 //printf("create audio player - invalid\n");
Glenn Kasten4597a742010-08-26 10:46:17 -0700176 SLresult result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject,
177 &audiosrc, &audiosnk, 1, ids, flags);
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700178 ASSERT_EQ(SL_RESULT_PARAMETER_INVALID, result);
Glenn Kastend48ff332010-09-01 09:22:23 -0700179 ASSERT_EQ(NULL, playerObject);
Glenn Kastenb12deb52010-08-27 15:35:35 -0700180
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700181 }
182 }
183
184 /*Prepare the buffer*/
Glenn Kastenb12deb52010-08-27 15:35:35 -0700185 void PrepareValidBuffer(SLuint32 numBuffers) {
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700186
Glenn Kastenb12deb52010-08-27 15:35:35 -0700187 locator_bufferqueue.numBuffers = numBuffers;
Glenn Kastend48ff332010-09-01 09:22:23 -0700188 //printf("create audio player - valid\n");
Glenn Kasten4597a742010-08-26 10:46:17 -0700189 res = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audiosrc, &audiosnk,
Glenn Kastenb12deb52010-08-27 15:35:35 -0700190 1, ids, flags);
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700191 CheckErr(res);
192 res = (*playerObject)->Realize(playerObject, SL_BOOLEAN_FALSE);
193 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700194 // get the play interface
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700195 res = (*playerObject)->GetInterface(playerObject, SL_IID_PLAY, &playerPlay);
196 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700197 // verify that player is initially stopped
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700198 res = (*playerPlay)->GetPlayState(playerPlay, &playerState);
199 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700200 ASSERT_EQ(SL_PLAYSTATE_STOPPED, playerState);
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700201
Glenn Kastena15af1c2010-07-29 18:22:23 -0700202 // get the buffer queue interface
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700203 res = (*playerObject)->GetInterface(playerObject, SL_IID_BUFFERQUEUE, &playerBufferQueue);
204 CheckErr(res);
205
Glenn Kastena15af1c2010-07-29 18:22:23 -0700206 // verify that buffer queue is initially empty
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700207 res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
208 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700209 ASSERT_EQ((SLuint32) 0, bufferqueueState.count);
210 ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex);
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700211 }
212
Glenn Kastenb12deb52010-08-27 15:35:35 -0700213 void EnqueueMaxBuffer(SLuint32 numBuffers) {
Glenn Kastena15af1c2010-07-29 18:22:23 -0700214 SLuint32 j;
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700215
Glenn Kastenb12deb52010-08-27 15:35:35 -0700216 for (j = 0; j < numBuffers; ++j) {
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700217 res = (*playerBufferQueue)->Enqueue(playerBufferQueue, "test", 4);
218 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700219 // verify that each buffer is enqueued properly and increments the buffer count
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700220 res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
221 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700222 ASSERT_EQ(j + 1, bufferqueueState.count);
223 ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700224 }
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700225 }
226
Glenn Kastenb12deb52010-08-27 15:35:35 -0700227 void EnqueueExtraBuffer(SLuint32 numBuffers) {
Glenn Kastena15af1c2010-07-29 18:22:23 -0700228 // enqueue one more buffer and make sure it fails
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700229 res = (*playerBufferQueue)->Enqueue(playerBufferQueue, "test", 4);
230 ASSERT_EQ(SL_RESULT_BUFFER_INSUFFICIENT, res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700231 // verify that the failed enqueue did not affect the buffer count
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700232 res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
233 CheckErr(res);
Glenn Kastenb12deb52010-08-27 15:35:35 -0700234 ASSERT_EQ(numBuffers, bufferqueueState.count);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700235 ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex);
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700236 }
237
238 void SetPlayerState(SLuint32 state) {
239 res = (*playerPlay)->SetPlayState(playerPlay, state);
240 CheckErr(res);
241 //verify the state can set correctly
242 GetPlayerState(state);
243 }
244
245 void GetPlayerState(SLuint32 state) {
246 res = (*playerPlay)->GetPlayState(playerPlay, &playerState);
247 CheckErr(res);
248 ASSERT_EQ(state, playerState);
249 }
250
251 void ClearQueue() {
Glenn Kastena15af1c2010-07-29 18:22:23 -0700252 // now clear the buffer queue
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700253 res = (*playerBufferQueue)->Clear(playerBufferQueue);
254 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700255 // make sure the clear works
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700256 res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
257 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700258 ASSERT_EQ((SLuint32) 0, bufferqueueState.count);
259 ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex);
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700260 }
Glenn Kastena15af1c2010-07-29 18:22:23 -0700261
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700262 void CheckBufferCount(SLuint32 ExpectedCount, SLuint32 ExpectedPlayIndex) {
263 // changing the play state should not affect the buffer count
264 res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
265 CheckErr(res);
266 ASSERT_EQ(ExpectedCount, bufferqueueState.count);
267 ASSERT_EQ(ExpectedPlayIndex, bufferqueueState.playIndex);
268 }
Glenn Kastena15af1c2010-07-29 18:22:23 -0700269
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700270 void PlayBufferQueue() {
Glenn Kastena15af1c2010-07-29 18:22:23 -0700271 // enqueue a buffer
Glenn Kasten4597a742010-08-26 10:46:17 -0700272 res = (*playerBufferQueue)->Enqueue(playerBufferQueue, stereoBuffer1,
273 sizeof(stereoBuffer1));
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700274 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700275 // set play state to playing
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700276 res = (*playerPlay)->SetPlayState(playerPlay, SL_PLAYSTATE_PLAYING);
277 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700278 // state should be playing immediately after enqueue
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700279 res = (*playerPlay)->GetPlayState(playerPlay, &playerState);
280 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700281 ASSERT_EQ(SL_PLAYSTATE_PLAYING, playerState);
282 // buffer should still be on the queue
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700283 res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
284 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700285 ASSERT_EQ((SLuint32) 1, bufferqueueState.count);
286 ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex);
Steve Blockde7c7da2011-10-26 11:12:08 +0100287 //ALOGV("Before 1.5 sec");
Glenn Kastena15af1c2010-07-29 18:22:23 -0700288 // wait 1.5 seconds
289 usleep(1500000);
Steve Blockde7c7da2011-10-26 11:12:08 +0100290 //ALOGV("After 1.5 sec");
Glenn Kastena15af1c2010-07-29 18:22:23 -0700291 // state should still be playing
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700292 res = (*playerPlay)->GetPlayState(playerPlay, &playerState);
Steve Blockde7c7da2011-10-26 11:12:08 +0100293 //ALOGV("GetPlayState");
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700294 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700295 ASSERT_EQ(SL_PLAYSTATE_PLAYING, playerState);
296 // buffer should be removed from the queue
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700297 res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState);
298 CheckErr(res);
Glenn Kastena15af1c2010-07-29 18:22:23 -0700299 ASSERT_EQ((SLuint32) 0, bufferqueueState.count);
300 ASSERT_EQ((SLuint32) 1, bufferqueueState.playIndex);
Steve Blockde7c7da2011-10-26 11:12:08 +0100301 //ALOGV("TestEnd");
Glenn Kastena15af1c2010-07-29 18:22:23 -0700302 }
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700303};
Glenn Kasten4597a742010-08-26 10:46:17 -0700304
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700305TEST_F(TestBufferQueue, testInvalidBuffer){
Steve Blockde7c7da2011-10-26 11:12:08 +0100306 //ALOGV("Test Fixture: InvalidBuffer");
Glenn Kastenb12deb52010-08-27 15:35:35 -0700307 InvalidBuffer();
Glenn Kasten4597a742010-08-26 10:46:17 -0700308}
Glenn Kastena15af1c2010-07-29 18:22:23 -0700309
Glenn Kasten104c0002010-10-06 09:52:44 -0700310TEST_F(TestBufferQueue, testMuteSolo) {
311 // create audio player with buffer queue data source in stereo PCM format and ask for mute solo
312 locator_bufferqueue.numBuffers = 1;
313 SLresult result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audiosrc,
314 &audiosnk, 2, ids_mutesolo, flags_mutesolo);
315 ASSERT_EQ(SL_RESULT_SUCCESS, result);
316 ASSERT_TRUE(NULL != playerObject);
317 DestroyPlayer();
318 // create audio player with buffer queue data source in mono PCM format and ask for mute solo
319 pcm.numChannels = 1;
320 pcm.channelMask = SL_SPEAKER_FRONT_CENTER;
321 result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audiosrc, &audiosnk,
322 2, ids_mutesolo, flags_mutesolo);
323 ASSERT_EQ(SL_RESULT_FEATURE_UNSUPPORTED, result);
324 ASSERT_EQ(NULL, playerObject);
325 DestroyPlayer();
326}
327
328TEST_F(TestBufferQueue, testSeek) {
329 // can create audio player with buffer queue data source and ask for seek
330 locator_bufferqueue.numBuffers = 1;
331 SLresult result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject,
332 &audiosrc, &audiosnk, 2, ids_seek, flags_seek);
333 ASSERT_EQ(SL_RESULT_FEATURE_UNSUPPORTED, result);
334 ASSERT_EQ(NULL, playerObject);
335 DestroyPlayer();
336}
337
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700338TEST_F(TestBufferQueue, testValidBuffer) {
Glenn Kastenb12deb52010-08-27 15:35:35 -0700339 for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
340 SLuint32 numBuffers = validNumBuffers[i];
341 PrepareValidBuffer(numBuffers);
Glenn Kastend48ff332010-09-01 09:22:23 -0700342 DestroyPlayer();
Glenn Kastenb12deb52010-08-27 15:35:35 -0700343 }
Glenn Kastena15af1c2010-07-29 18:22:23 -0700344}
345
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700346TEST_F(TestBufferQueue, testEnqueueMaxBuffer) {
Glenn Kastenb12deb52010-08-27 15:35:35 -0700347 for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
348 SLuint32 numBuffers = validNumBuffers[i];
349 PrepareValidBuffer(numBuffers);
350 EnqueueMaxBuffer(numBuffers);
Glenn Kastend48ff332010-09-01 09:22:23 -0700351 DestroyPlayer();
Glenn Kastenb12deb52010-08-27 15:35:35 -0700352 }
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700353}
354
Glenn Kastenb12deb52010-08-27 15:35:35 -0700355TEST_F(TestBufferQueue, testEnqueueExtraBuffer) {
356 for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
357 SLuint32 numBuffers = validNumBuffers[i];
358 PrepareValidBuffer(numBuffers);
359 EnqueueMaxBuffer(numBuffers);
360 EnqueueExtraBuffer(numBuffers);
361 GetPlayerState(SL_PLAYSTATE_STOPPED);
Glenn Kastend48ff332010-09-01 09:22:23 -0700362 DestroyPlayer();
Glenn Kastenb12deb52010-08-27 15:35:35 -0700363 }
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700364}
365
Glenn Kastenb12deb52010-08-27 15:35:35 -0700366TEST_F(TestBufferQueue, testEnqueueAtStopped) {
367 for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
368 SLuint32 numBuffers = validNumBuffers[i];
369 PrepareValidBuffer(numBuffers);
370 SetPlayerState(SL_PLAYSTATE_STOPPED);
371 EnqueueMaxBuffer(numBuffers);
372 CheckBufferCount(numBuffers, (SLuint32) 0);
Glenn Kastend48ff332010-09-01 09:22:23 -0700373 DestroyPlayer();
Glenn Kastenb12deb52010-08-27 15:35:35 -0700374 }
375}
376
377TEST_F(TestBufferQueue, testEnqueueAtPaused) {
378 for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
379 SLuint32 numBuffers = validNumBuffers[i];
380 PrepareValidBuffer(numBuffers);
381 SetPlayerState(SL_PLAYSTATE_PAUSED);
382 EnqueueMaxBuffer(numBuffers);
383 CheckBufferCount(numBuffers, (SLuint32) 0);
Glenn Kastend48ff332010-09-01 09:22:23 -0700384 DestroyPlayer();
Glenn Kastenb12deb52010-08-27 15:35:35 -0700385 }
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700386}
387
388TEST_F(TestBufferQueue, testClearQueue) {
Glenn Kastenb12deb52010-08-27 15:35:35 -0700389 for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
390 SLuint32 numBuffers = validNumBuffers[i];
391 PrepareValidBuffer(numBuffers);
392 EnqueueMaxBuffer(numBuffers);
393 ClearQueue();
Glenn Kastend48ff332010-09-01 09:22:23 -0700394 DestroyPlayer();
Glenn Kastenb12deb52010-08-27 15:35:35 -0700395 }
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700396}
397
398TEST_F(TestBufferQueue, testStateTransitionEmptyQueue) {
399 static const SLuint32 newStates[] = {
400 SL_PLAYSTATE_PAUSED, // paused -> paused
401 SL_PLAYSTATE_STOPPED, // paused -> stopped
402 SL_PLAYSTATE_PAUSED, // stopped -> paused
403 SL_PLAYSTATE_PLAYING, // paused -> playing
404 SL_PLAYSTATE_PLAYING, // playing -> playing
405 SL_PLAYSTATE_STOPPED, // playing -> stopped
406 SL_PLAYSTATE_STOPPED, // stopped -> stopped
407 SL_PLAYSTATE_PLAYING, // stopped -> playing
408 SL_PLAYSTATE_PAUSED // playing -> paused
409 };
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700410
Glenn Kastenb12deb52010-08-27 15:35:35 -0700411 for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
412 SLuint32 numBuffers = validNumBuffers[i];
413 SLuint32 j;
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700414
Glenn Kastenb12deb52010-08-27 15:35:35 -0700415 PrepareValidBuffer(numBuffers);
416 /* Set initial state to paused*/
417 SetPlayerState(SL_PLAYSTATE_PAUSED);
418
419 for (j = 0; j < sizeof(newStates) / sizeof(newStates[0]); ++j) {
420 SetPlayerState(newStates[j]);
421 CheckBufferCount((SLuint32) 0, (SLuint32) 0);
422 }
Glenn Kastend48ff332010-09-01 09:22:23 -0700423 DestroyPlayer();
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700424 }
425}
426
427TEST_F(TestBufferQueue, testStateTransitionNonEmptyQueue) {
428 static const SLuint32 newStates[] = {
429 SL_PLAYSTATE_PAUSED, // paused -> paused
430 SL_PLAYSTATE_STOPPED, // paused -> stopped
431 SL_PLAYSTATE_STOPPED, // stopped -> stopped
Glenn Kastenb12deb52010-08-27 15:35:35 -0700432 SL_PLAYSTATE_PAUSED // stopped -> paused
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700433 };
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700434
Glenn Kastenb12deb52010-08-27 15:35:35 -0700435 for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
436 SLuint32 numBuffers = validNumBuffers[i];
437 SLuint32 j;
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700438
Glenn Kastenb12deb52010-08-27 15:35:35 -0700439 /* Prepare the player */
440 PrepareValidBuffer(numBuffers);
441 EnqueueMaxBuffer(numBuffers);
442 SetPlayerState(SL_PLAYSTATE_PAUSED);
443
444 for (j = 0; j < sizeof(newStates) / sizeof(newStates[0]); ++j) {
445 SetPlayerState(newStates[j]);
446 CheckBufferCount(numBuffers, (SLuint32) 0);
447 }
Glenn Kastend48ff332010-09-01 09:22:23 -0700448 DestroyPlayer();
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700449 }
450}
451
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700452TEST_F(TestBufferQueue, testStatePlayBuffer){
Glenn Kastenb12deb52010-08-27 15:35:35 -0700453 for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) {
454 SLuint32 numBuffers = validNumBuffers[i];
455 PrepareValidBuffer(numBuffers);
456 PlayBufferQueue();
Glenn Kastend48ff332010-09-01 09:22:23 -0700457 DestroyPlayer();
Glenn Kastenb12deb52010-08-27 15:35:35 -0700458 }
Glenn Kasten4597a742010-08-26 10:46:17 -0700459}
460
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700461int main(int argc, char **argv) {
462 testing::InitGoogleTest(&argc, argv);
Glenn Kasten04c73542010-09-24 14:16:15 -0700463#if 1 // temporary workaround if hardware volume control is not working
464 const char *VOLUME = getenv("BufferQueue_test_VOLUME");
465 if (NULL != VOLUME) {
466 float volume = atof(VOLUME);
467 if (volume >= 0.0f && volume <= 1.0f) {
468 gVolume = volume;
469 }
470 }
471#endif
Yu Shan Emily Lau3b2a7f62010-08-17 11:08:20 -0700472 return RUN_ALL_TESTS();
Glenn Kastena15af1c2010-07-29 18:22:23 -0700473}