Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 1 | /* |
| 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 Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 18 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 19 | #define LOG_NDEBUG 0 |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 20 | #define LOG_TAG "BufferQueue_test" |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 21 | |
| 22 | #ifdef ANDROID |
| 23 | #include <utils/Log.h> |
| 24 | #else |
Steve Block | de7c7da | 2011-10-26 11:12:08 +0100 | [diff] [blame] | 25 | #define ALOGV printf |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 26 | #endif |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 27 | |
| 28 | #include <assert.h> |
| 29 | #include <math.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <unistd.h> |
Glenn Kasten | c685389 | 2011-07-19 11:16:07 -0700 | [diff] [blame] | 33 | #include <SLES/OpenSLES.h> |
Glenn Kasten | be22189 | 2011-01-05 08:03:38 -0800 | [diff] [blame] | 34 | #include "OpenSLESUT.h" |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 35 | #include <gtest/gtest.h> |
| 36 | |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 37 | typedef struct { |
| 38 | short left; |
| 39 | short right; |
| 40 | } stereo; |
| 41 | |
Glenn Kasten | 04c7354 | 2010-09-24 14:16:15 -0700 | [diff] [blame] | 42 | // volume of sine wave in range 0.0 to 1.0 |
| 43 | static float gVolume = 1.0f; |
| 44 | |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 45 | // 1 second of stereo audio at 44.1 kHz |
| 46 | static stereo stereoBuffer1[44100 * 1]; |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 47 | static const SLuint32 invalidNumBuffers[] = { 0, 0xFFFFFFFF, 0x80000000, 0x10002, 0x102, |
| 48 | 0x101, 0x100 }; |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 49 | static const SLuint32 validNumBuffers[] = { 1, 2, 3, 4, 5, 6, 7, 8, 255 }; |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 50 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 51 | //----------------------------------------------------------------- |
| 52 | /* Checks for error. If any errors exit the application! */ |
| 53 | void CheckErr(SLresult res) { |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 54 | if (SL_RESULT_SUCCESS != res) { |
Glenn Kasten | 7126c25 | 2010-10-13 09:52:53 -0700 | [diff] [blame] | 55 | const char *str = slesutResultToString(res); |
| 56 | if (NULL == str) |
| 57 | str = "unknown"; |
Glenn Kasten | 58432eb | 2011-06-13 11:30:22 -0700 | [diff] [blame] | 58 | fprintf(stderr, "CheckErr failure: %s (0x%x), exiting\n", str, res); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 59 | //Fail the test case |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 60 | FAIL(); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 61 | } |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 62 | } |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 63 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 64 | static const SLInterfaceID ids[1] = { SL_IID_BUFFERQUEUE }; |
| 65 | static const SLboolean flags[1] = { SL_BOOLEAN_TRUE }; |
Glenn Kasten | 104c000 | 2010-10-06 09:52:44 -0700 | [diff] [blame] | 66 | static const SLInterfaceID ids_mutesolo[2] = { SL_IID_BUFFERQUEUE, SL_IID_MUTESOLO }; |
| 67 | static const SLboolean flags_mutesolo[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE }; |
| 68 | static const SLInterfaceID ids_seek[2] = { SL_IID_BUFFERQUEUE, SL_IID_SEEK }; |
| 69 | static const SLboolean flags_seek[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE }; |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 70 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 71 | // The fixture for testing class BufferQueue |
| 72 | class TestBufferQueue: public ::testing::Test { |
| 73 | public: |
| 74 | SLresult res; |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 75 | SLObjectItf outputmixObject; |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 76 | SLObjectItf engineObject; |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 77 | |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 78 | SLDataSource audiosrc; |
| 79 | SLDataSink audiosnk; |
| 80 | SLDataFormat_PCM pcm; |
| 81 | SLDataLocator_OutputMix locator_outputmix; |
| 82 | SLDataLocator_BufferQueue locator_bufferqueue; |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 83 | SLBufferQueueItf playerBufferQueue; |
| 84 | SLBufferQueueState bufferqueueState; |
| 85 | SLPlayItf playerPlay; |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 86 | SLObjectItf playerObject; |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 87 | SLEngineItf engineEngine; |
| 88 | SLuint32 playerState; |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 89 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 90 | protected: |
| 91 | TestBufferQueue() { |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 94 | virtual ~TestBufferQueue() { |
| 95 | |
| 96 | } |
| 97 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 98 | /*Test setup*/ |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 99 | virtual void SetUp() { |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 100 | |
| 101 | // create engine |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 102 | 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 Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 106 | 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 Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 127 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 128 | 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 Kasten | 04c7354 | 2010-09-24 14:16:15 -0700 | [diff] [blame] | 140 | int pcm = (int) (pcm_ * 32766.0 * gVolume); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 141 | 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 Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 148 | // Clean up the mixer and the engine |
| 149 | // (must be done in that order, and after player destroyed) |
Glenn Kasten | 4597a74 | 2010-08-26 10:46:17 -0700 | [diff] [blame] | 150 | if (outputmixObject){ |
| 151 | (*outputmixObject)->Destroy(outputmixObject); |
| 152 | outputmixObject = NULL; |
| 153 | } |
| 154 | if (engineObject){ |
| 155 | (*engineObject)->Destroy(engineObject); |
| 156 | engineObject = NULL; |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Glenn Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 160 | void DestroyPlayer() { |
| 161 | if (playerObject){ |
| 162 | //printf("destroy player\n"); |
| 163 | (*playerObject)->Destroy(playerObject); |
| 164 | playerObject = NULL; |
| 165 | } |
| 166 | } |
| 167 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 168 | /* Test case for creating audio player with various invalid values for numBuffers*/ |
| 169 | void InvalidBuffer() { |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 170 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 171 | for (unsigned i = 0; i < sizeof(invalidNumBuffers) / sizeof(invalidNumBuffers[0]); ++i) { |
| 172 | SLuint32 numBuffers = invalidNumBuffers[i]; |
| 173 | |
| 174 | locator_bufferqueue.numBuffers = numBuffers; |
Glenn Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 175 | //printf("create audio player - invalid\n"); |
Glenn Kasten | 4597a74 | 2010-08-26 10:46:17 -0700 | [diff] [blame] | 176 | SLresult result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, |
| 177 | &audiosrc, &audiosnk, 1, ids, flags); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 178 | ASSERT_EQ(SL_RESULT_PARAMETER_INVALID, result); |
Glenn Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 179 | ASSERT_EQ(NULL, playerObject); |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 180 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | /*Prepare the buffer*/ |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 185 | void PrepareValidBuffer(SLuint32 numBuffers) { |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 186 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 187 | locator_bufferqueue.numBuffers = numBuffers; |
Glenn Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 188 | //printf("create audio player - valid\n"); |
Glenn Kasten | 4597a74 | 2010-08-26 10:46:17 -0700 | [diff] [blame] | 189 | res = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audiosrc, &audiosnk, |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 190 | 1, ids, flags); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 191 | CheckErr(res); |
| 192 | res = (*playerObject)->Realize(playerObject, SL_BOOLEAN_FALSE); |
| 193 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 194 | // get the play interface |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 195 | res = (*playerObject)->GetInterface(playerObject, SL_IID_PLAY, &playerPlay); |
| 196 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 197 | // verify that player is initially stopped |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 198 | res = (*playerPlay)->GetPlayState(playerPlay, &playerState); |
| 199 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 200 | ASSERT_EQ(SL_PLAYSTATE_STOPPED, playerState); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 201 | |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 202 | // get the buffer queue interface |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 203 | res = (*playerObject)->GetInterface(playerObject, SL_IID_BUFFERQUEUE, &playerBufferQueue); |
| 204 | CheckErr(res); |
| 205 | |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 206 | // verify that buffer queue is initially empty |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 207 | res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState); |
| 208 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 209 | ASSERT_EQ((SLuint32) 0, bufferqueueState.count); |
| 210 | ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 213 | void EnqueueMaxBuffer(SLuint32 numBuffers) { |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 214 | SLuint32 j; |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 215 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 216 | for (j = 0; j < numBuffers; ++j) { |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 217 | res = (*playerBufferQueue)->Enqueue(playerBufferQueue, "test", 4); |
| 218 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 219 | // verify that each buffer is enqueued properly and increments the buffer count |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 220 | res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState); |
| 221 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 222 | ASSERT_EQ(j + 1, bufferqueueState.count); |
| 223 | ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 224 | } |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 227 | void EnqueueExtraBuffer(SLuint32 numBuffers) { |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 228 | // enqueue one more buffer and make sure it fails |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 229 | res = (*playerBufferQueue)->Enqueue(playerBufferQueue, "test", 4); |
| 230 | ASSERT_EQ(SL_RESULT_BUFFER_INSUFFICIENT, res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 231 | // verify that the failed enqueue did not affect the buffer count |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 232 | res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState); |
| 233 | CheckErr(res); |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 234 | ASSERT_EQ(numBuffers, bufferqueueState.count); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 235 | ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 236 | } |
| 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 Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 252 | // now clear the buffer queue |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 253 | res = (*playerBufferQueue)->Clear(playerBufferQueue); |
| 254 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 255 | // make sure the clear works |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 256 | res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState); |
| 257 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 258 | ASSERT_EQ((SLuint32) 0, bufferqueueState.count); |
| 259 | ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 260 | } |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 261 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 262 | 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 Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 269 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 270 | void PlayBufferQueue() { |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 271 | // enqueue a buffer |
Glenn Kasten | 4597a74 | 2010-08-26 10:46:17 -0700 | [diff] [blame] | 272 | res = (*playerBufferQueue)->Enqueue(playerBufferQueue, stereoBuffer1, |
| 273 | sizeof(stereoBuffer1)); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 274 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 275 | // set play state to playing |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 276 | res = (*playerPlay)->SetPlayState(playerPlay, SL_PLAYSTATE_PLAYING); |
| 277 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 278 | // state should be playing immediately after enqueue |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 279 | res = (*playerPlay)->GetPlayState(playerPlay, &playerState); |
| 280 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 281 | ASSERT_EQ(SL_PLAYSTATE_PLAYING, playerState); |
| 282 | // buffer should still be on the queue |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 283 | res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState); |
| 284 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 285 | ASSERT_EQ((SLuint32) 1, bufferqueueState.count); |
| 286 | ASSERT_EQ((SLuint32) 0, bufferqueueState.playIndex); |
Steve Block | de7c7da | 2011-10-26 11:12:08 +0100 | [diff] [blame] | 287 | //ALOGV("Before 1.5 sec"); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 288 | // wait 1.5 seconds |
| 289 | usleep(1500000); |
Steve Block | de7c7da | 2011-10-26 11:12:08 +0100 | [diff] [blame] | 290 | //ALOGV("After 1.5 sec"); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 291 | // state should still be playing |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 292 | res = (*playerPlay)->GetPlayState(playerPlay, &playerState); |
Steve Block | de7c7da | 2011-10-26 11:12:08 +0100 | [diff] [blame] | 293 | //ALOGV("GetPlayState"); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 294 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 295 | ASSERT_EQ(SL_PLAYSTATE_PLAYING, playerState); |
| 296 | // buffer should be removed from the queue |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 297 | res = (*playerBufferQueue)->GetState(playerBufferQueue, &bufferqueueState); |
| 298 | CheckErr(res); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 299 | ASSERT_EQ((SLuint32) 0, bufferqueueState.count); |
| 300 | ASSERT_EQ((SLuint32) 1, bufferqueueState.playIndex); |
Steve Block | de7c7da | 2011-10-26 11:12:08 +0100 | [diff] [blame] | 301 | //ALOGV("TestEnd"); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 302 | } |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 303 | }; |
Glenn Kasten | 4597a74 | 2010-08-26 10:46:17 -0700 | [diff] [blame] | 304 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 305 | TEST_F(TestBufferQueue, testInvalidBuffer){ |
Steve Block | de7c7da | 2011-10-26 11:12:08 +0100 | [diff] [blame] | 306 | //ALOGV("Test Fixture: InvalidBuffer"); |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 307 | InvalidBuffer(); |
Glenn Kasten | 4597a74 | 2010-08-26 10:46:17 -0700 | [diff] [blame] | 308 | } |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 309 | |
Glenn Kasten | 104c000 | 2010-10-06 09:52:44 -0700 | [diff] [blame] | 310 | TEST_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 | |
| 328 | TEST_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 Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 338 | TEST_F(TestBufferQueue, testValidBuffer) { |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 339 | for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) { |
| 340 | SLuint32 numBuffers = validNumBuffers[i]; |
| 341 | PrepareValidBuffer(numBuffers); |
Glenn Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 342 | DestroyPlayer(); |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 343 | } |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 346 | TEST_F(TestBufferQueue, testEnqueueMaxBuffer) { |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 347 | for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) { |
| 348 | SLuint32 numBuffers = validNumBuffers[i]; |
| 349 | PrepareValidBuffer(numBuffers); |
| 350 | EnqueueMaxBuffer(numBuffers); |
Glenn Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 351 | DestroyPlayer(); |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 352 | } |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 355 | TEST_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 Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 362 | DestroyPlayer(); |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 363 | } |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 366 | TEST_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 Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 373 | DestroyPlayer(); |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | TEST_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 Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 384 | DestroyPlayer(); |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 385 | } |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | TEST_F(TestBufferQueue, testClearQueue) { |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 389 | 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 Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 394 | DestroyPlayer(); |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 395 | } |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | TEST_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 Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 410 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 411 | for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) { |
| 412 | SLuint32 numBuffers = validNumBuffers[i]; |
| 413 | SLuint32 j; |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 414 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 415 | 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 Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 423 | DestroyPlayer(); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | TEST_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 Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 432 | SL_PLAYSTATE_PAUSED // stopped -> paused |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 433 | }; |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 434 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 435 | for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) { |
| 436 | SLuint32 numBuffers = validNumBuffers[i]; |
| 437 | SLuint32 j; |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 438 | |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 439 | /* 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 Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 448 | DestroyPlayer(); |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 452 | TEST_F(TestBufferQueue, testStatePlayBuffer){ |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 453 | for (unsigned i = 0; i < sizeof(validNumBuffers) / sizeof(validNumBuffers[0]); ++i) { |
| 454 | SLuint32 numBuffers = validNumBuffers[i]; |
| 455 | PrepareValidBuffer(numBuffers); |
| 456 | PlayBufferQueue(); |
Glenn Kasten | d48ff33 | 2010-09-01 09:22:23 -0700 | [diff] [blame] | 457 | DestroyPlayer(); |
Glenn Kasten | b12deb5 | 2010-08-27 15:35:35 -0700 | [diff] [blame] | 458 | } |
Glenn Kasten | 4597a74 | 2010-08-26 10:46:17 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Yu Shan Emily Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 461 | int main(int argc, char **argv) { |
| 462 | testing::InitGoogleTest(&argc, argv); |
Glenn Kasten | 04c7354 | 2010-09-24 14:16:15 -0700 | [diff] [blame] | 463 | #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 Lau | 3b2a7f6 | 2010-08-17 11:08:20 -0700 | [diff] [blame] | 472 | return RUN_ALL_TESTS(); |
Glenn Kasten | a15af1c | 2010-07-29 18:22:23 -0700 | [diff] [blame] | 473 | } |