Don Turner | 7dcf25d | 2018-07-18 15:21:07 +0100 | [diff] [blame] | 1 | #include <oboe/Oboe.h>/* |
| 2 | * Copyright 2018 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 | #include <gtest/gtest.h> |
| 18 | #include <oboe/Oboe.h> |
Don Turner | 67a5fe9 | 2018-08-24 12:50:00 +0100 | [diff] [blame] | 19 | #include <android/api-level.h> |
Don Turner | 7dcf25d | 2018-07-18 15:21:07 +0100 | [diff] [blame] | 20 | |
| 21 | using namespace oboe; |
| 22 | |
Phil Burk | 4706346 | 2020-01-28 10:21:07 -0800 | [diff] [blame] | 23 | class CallbackSizeMonitor : public AudioStreamCallback { |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 24 | public: |
| 25 | DataCallbackResult onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numFrames) override { |
Phil Burk | 4706346 | 2020-01-28 10:21:07 -0800 | [diff] [blame] | 26 | framesPerCallback = numFrames; |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 27 | return DataCallbackResult::Continue; |
| 28 | } |
Phil Burk | 4706346 | 2020-01-28 10:21:07 -0800 | [diff] [blame] | 29 | |
| 30 | // This is exposed publicly so that the number of frames per callback can be tested. |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 31 | std::atomic<int32_t> framesPerCallback{0}; |
| 32 | }; |
Don Turner | 7dcf25d | 2018-07-18 15:21:07 +0100 | [diff] [blame] | 33 | |
| 34 | class StreamOpen : public ::testing::Test { |
| 35 | |
| 36 | protected: |
| 37 | |
Don Turner | 67a5fe9 | 2018-08-24 12:50:00 +0100 | [diff] [blame] | 38 | bool openStream(){ |
Don Turner | 7dcf25d | 2018-07-18 15:21:07 +0100 | [diff] [blame] | 39 | Result r = mBuilder.openStream(&mStream); |
| 40 | EXPECT_EQ(r, Result::OK) << "Failed to open stream " << convertToText(r); |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 41 | EXPECT_EQ(0, openCount) << "Should start with a fresh object every time."; |
| 42 | openCount++; |
Don Turner | 7dcf25d | 2018-07-18 15:21:07 +0100 | [diff] [blame] | 43 | return (r == Result::OK); |
| 44 | } |
| 45 | |
| 46 | void closeStream(){ |
Don Turner | 67a5fe9 | 2018-08-24 12:50:00 +0100 | [diff] [blame] | 47 | if (mStream != nullptr){ |
| 48 | Result r = mStream->close(); |
| 49 | if (r != Result::OK){ |
| 50 | FAIL() << "Failed to close stream. " << convertToText(r); |
| 51 | } |
Don Turner | 7dcf25d | 2018-07-18 15:21:07 +0100 | [diff] [blame] | 52 | } |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 53 | usleep(500 * 1000); // give previous stream time to settle |
Don Turner | 7dcf25d | 2018-07-18 15:21:07 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | AudioStreamBuilder mBuilder; |
| 57 | AudioStream *mStream = nullptr; |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 58 | int32_t openCount = 0; |
Don Turner | 7dcf25d | 2018-07-18 15:21:07 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | TEST_F(StreamOpen, ForOpenSLESDefaultSampleRateIsUsed){ |
| 62 | |
| 63 | DefaultStreamValues::SampleRate = 44100; |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 64 | DefaultStreamValues::FramesPerBurst = 192; |
Don Turner | 67a5fe9 | 2018-08-24 12:50:00 +0100 | [diff] [blame] | 65 | mBuilder.setAudioApi(AudioApi::OpenSLES); |
| 66 | openStream(); |
Don Turner | dcb55a4 | 2018-07-19 20:21:16 +0100 | [diff] [blame] | 67 | ASSERT_EQ(mStream->getSampleRate(), 44100); |
Don Turner | 7dcf25d | 2018-07-18 15:21:07 +0100 | [diff] [blame] | 68 | closeStream(); |
| 69 | } |
| 70 | |
| 71 | TEST_F(StreamOpen, ForOpenSLESDefaultFramesPerBurstIsUsed){ |
| 72 | |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 73 | DefaultStreamValues::SampleRate = 48000; |
| 74 | DefaultStreamValues::FramesPerBurst = 128; // used for low latency |
Don Turner | 67a5fe9 | 2018-08-24 12:50:00 +0100 | [diff] [blame] | 75 | mBuilder.setAudioApi(AudioApi::OpenSLES); |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 76 | mBuilder.setPerformanceMode(PerformanceMode::LowLatency); |
Don Turner | 67a5fe9 | 2018-08-24 12:50:00 +0100 | [diff] [blame] | 77 | openStream(); |
Don Turner | dcb55a4 | 2018-07-19 20:21:16 +0100 | [diff] [blame] | 78 | ASSERT_EQ(mStream->getFramesPerBurst(), 128); |
Don Turner | 7dcf25d | 2018-07-18 15:21:07 +0100 | [diff] [blame] | 79 | closeStream(); |
| 80 | } |
| 81 | |
| 82 | TEST_F(StreamOpen, ForOpenSLESDefaultChannelCountIsUsed){ |
| 83 | |
| 84 | DefaultStreamValues::ChannelCount = 1; |
Don Turner | 67a5fe9 | 2018-08-24 12:50:00 +0100 | [diff] [blame] | 85 | mBuilder.setAudioApi(AudioApi::OpenSLES); |
| 86 | openStream(); |
Don Turner | dcb55a4 | 2018-07-19 20:21:16 +0100 | [diff] [blame] | 87 | ASSERT_EQ(mStream->getChannelCount(), 1); |
Don Turner | 7dcf25d | 2018-07-18 15:21:07 +0100 | [diff] [blame] | 88 | closeStream(); |
Don Turner | 09282b5 | 2018-07-19 20:22:12 +0100 | [diff] [blame] | 89 | } |
Phil Burk | 9a6262c | 2018-08-24 18:16:25 -0700 | [diff] [blame] | 90 | |
| 91 | TEST_F(StreamOpen, OutputForOpenSLESPerformanceModeShouldBeNone){ |
| 92 | // We will not get a LowLatency stream if we request 16000 Hz. |
| 93 | mBuilder.setSampleRate(16000); |
| 94 | mBuilder.setPerformanceMode(PerformanceMode::LowLatency); |
| 95 | mBuilder.setDirection(Direction::Output); |
Don Turner | 67a5fe9 | 2018-08-24 12:50:00 +0100 | [diff] [blame] | 96 | mBuilder.setAudioApi(AudioApi::OpenSLES); |
Phil Burk | 4706346 | 2020-01-28 10:21:07 -0800 | [diff] [blame] | 97 | openStream(); |
Phil Burk | 9a6262c | 2018-08-24 18:16:25 -0700 | [diff] [blame] | 98 | ASSERT_EQ((int)mStream->getPerformanceMode(), (int)PerformanceMode::None); |
| 99 | closeStream(); |
| 100 | } |
| 101 | |
| 102 | TEST_F(StreamOpen, InputForOpenSLESPerformanceModeShouldBeNone){ |
| 103 | // We will not get a LowLatency stream if we request 16000 Hz. |
| 104 | mBuilder.setSampleRate(16000); |
| 105 | mBuilder.setPerformanceMode(PerformanceMode::LowLatency); |
| 106 | mBuilder.setDirection(Direction::Input); |
Don Turner | 67a5fe9 | 2018-08-24 12:50:00 +0100 | [diff] [blame] | 107 | mBuilder.setAudioApi(AudioApi::OpenSLES); |
Phil Burk | 4706346 | 2020-01-28 10:21:07 -0800 | [diff] [blame] | 108 | openStream(); |
| 109 | ASSERT_EQ((int)mStream->getPerformanceMode(), (int)PerformanceMode::None); |
Don Turner | 67a5fe9 | 2018-08-24 12:50:00 +0100 | [diff] [blame] | 110 | closeStream(); |
| 111 | } |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 112 | |
| 113 | // Make sure the callback is called with the requested FramesPerCallback |
| 114 | TEST_F(StreamOpen, OpenSLESFramesPerCallback) { |
Phil Burk | 4706346 | 2020-01-28 10:21:07 -0800 | [diff] [blame] | 115 | const int kRequestedFramesPerCallback = 417; |
| 116 | CallbackSizeMonitor callback; |
| 117 | |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 118 | DefaultStreamValues::SampleRate = 48000; |
| 119 | DefaultStreamValues::ChannelCount = 2; |
| 120 | DefaultStreamValues::FramesPerBurst = 192; |
| 121 | mBuilder.setAudioApi(AudioApi::OpenSLES); |
| 122 | mBuilder.setFramesPerCallback(kRequestedFramesPerCallback); |
| 123 | mBuilder.setCallback(&callback); |
| 124 | openStream(); |
| 125 | ASSERT_EQ(mStream->requestStart(), Result::OK); |
| 126 | int timeout = 20; |
| 127 | while (callback.framesPerCallback == 0 && timeout > 0) { |
Phil Burk | 4706346 | 2020-01-28 10:21:07 -0800 | [diff] [blame] | 128 | usleep(50 * 1000); |
| 129 | timeout--; |
| 130 | } |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 131 | ASSERT_EQ(kRequestedFramesPerCallback, callback.framesPerCallback); |
| 132 | ASSERT_EQ(kRequestedFramesPerCallback, mStream->getFramesPerCallback()); |
| 133 | ASSERT_EQ(mStream->requestStop(), Result::OK); |
| 134 | closeStream(); |
| 135 | } |
| 136 | |
| 137 | /* TODO - This is hanging! |
| 138 | // Make sure the LowLatency callback has the requested FramesPerCallback. |
| 139 | TEST_F(StreamOpen, AAudioFramesPerCallbackLowLatency) { |
Phil Burk | 4706346 | 2020-01-28 10:21:07 -0800 | [diff] [blame] | 140 | const int kRequestedFramesPerCallback = 192; |
| 141 | CallbackSizeMonitor callback; |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 142 | |
| 143 | mBuilder.setAudioApi(AudioApi::AAudio); |
| 144 | mBuilder.setFramesPerCallback(kRequestedFramesPerCallback); |
| 145 | mBuilder.setCallback(&callback); |
| 146 | mBuilder.setPerformanceMode(PerformanceMode::LowLatency); |
| 147 | openStream(); |
| 148 | ASSERT_EQ(kRequestedFramesPerCallback, mStream->getFramesPerCallback()); |
| 149 | ASSERT_EQ(mStream->requestStart(), Result::OK); |
| 150 | int timeout = 20; |
| 151 | while (callback.framesPerCallback == 0 && timeout > 0) { |
Phil Burk | 4706346 | 2020-01-28 10:21:07 -0800 | [diff] [blame] | 152 | usleep(50 * 1000); |
| 153 | timeout--; |
| 154 | } |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 155 | ASSERT_EQ(kRequestedFramesPerCallback, callback.framesPerCallback); |
| 156 | ASSERT_EQ(mStream->requestStop(), Result::OK); |
| 157 | closeStream(); |
| 158 | } |
| 159 | */ |
| 160 | |
| 161 | /* TODO - This is hanging! |
| 162 | // Make sure the regular callback has the requested FramesPerCallback. |
| 163 | TEST_F(StreamOpen, AAudioFramesPerCallbackNone) { |
Phil Burk | 4706346 | 2020-01-28 10:21:07 -0800 | [diff] [blame] | 164 | const int kRequestedFramesPerCallback = 1024; |
| 165 | CallbackSizeMonitor callback; |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 166 | |
| 167 | mBuilder.setAudioApi(AudioApi::AAudio); |
| 168 | mBuilder.setFramesPerCallback(kRequestedFramesPerCallback); |
| 169 | mBuilder.setCallback(&callback); |
| 170 | mBuilder.setPerformanceMode(PerformanceMode::None); |
| 171 | openStream(); |
| 172 | ASSERT_EQ(kRequestedFramesPerCallback, mStream->getFramesPerCallback()); |
| 173 | ASSERT_EQ(mStream->setBufferSizeInFrames(mStream->getBufferCapacityInFrames()), Result::OK); |
| 174 | ASSERT_EQ(mStream->requestStart(), Result::OK); |
| 175 | int timeout = 20; |
| 176 | while (callback.framesPerCallback == 0 && timeout > 0) { |
Phil Burk | 4706346 | 2020-01-28 10:21:07 -0800 | [diff] [blame] | 177 | usleep(50 * 1000); |
| 178 | timeout--; |
| 179 | } |
Phil Burk | 8f49571 | 2020-01-27 14:30:10 -0800 | [diff] [blame] | 180 | ASSERT_EQ(kRequestedFramesPerCallback, callback.framesPerCallback); |
| 181 | ASSERT_EQ(mStream->requestStop(), Result::OK); |
| 182 | closeStream(); |
| 183 | } |
| 184 | */ |
| 185 | |
Don Turner | 67a5fe9 | 2018-08-24 12:50:00 +0100 | [diff] [blame] | 186 | TEST_F(StreamOpen, RecordingFormatUnspecifiedReturnsI16BeforeMarshmallow){ |
| 187 | |
| 188 | if (getSdkVersion() < __ANDROID_API_M__){ |
| 189 | mBuilder.setDirection(Direction::Input); |
| 190 | mBuilder.setFormat(AudioFormat::Unspecified); |
| 191 | openStream(); |
| 192 | ASSERT_EQ(mStream->getFormat(), AudioFormat::I16); |
| 193 | closeStream(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | TEST_F(StreamOpen, RecordingFormatUnspecifiedReturnsFloatOnMarshmallowAndLater){ |
| 198 | |
| 199 | if (getSdkVersion() >= __ANDROID_API_M__){ |
| 200 | mBuilder.setDirection(Direction::Input); |
| 201 | mBuilder.setFormat(AudioFormat::Unspecified); |
| 202 | openStream(); |
| 203 | ASSERT_EQ(mStream->getFormat(), AudioFormat::Float); |
| 204 | closeStream(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | TEST_F(StreamOpen, RecordingFormatFloatReturnsErrorBeforeMarshmallow){ |
| 209 | |
| 210 | if (getSdkVersion() < __ANDROID_API_M__){ |
| 211 | mBuilder.setDirection(Direction::Input); |
| 212 | mBuilder.setFormat(AudioFormat::Float); |
| 213 | Result r = mBuilder.openStream(&mStream); |
| 214 | ASSERT_EQ(r, Result::ErrorInvalidFormat) << convertToText(r); |
| 215 | closeStream(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | TEST_F(StreamOpen, RecordingFormatFloatReturnsFloatOnMarshmallowAndLater){ |
| 220 | |
| 221 | if (getSdkVersion() >= __ANDROID_API_M__){ |
| 222 | mBuilder.setDirection(Direction::Input); |
| 223 | mBuilder.setFormat(AudioFormat::Float); |
| 224 | openStream(); |
| 225 | ASSERT_EQ(mStream->getFormat(), AudioFormat::Float); |
| 226 | closeStream(); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | TEST_F(StreamOpen, RecordingFormatI16ReturnsI16){ |
| 231 | |
| 232 | mBuilder.setDirection(Direction::Input); |
| 233 | mBuilder.setFormat(AudioFormat::I16); |
| 234 | openStream(); |
| 235 | ASSERT_EQ(mStream->getFormat(), AudioFormat::I16); |
| 236 | closeStream(); |
| 237 | } |
| 238 | |
| 239 | TEST_F(StreamOpen, PlaybackFormatUnspecifiedReturnsI16BeforeLollipop){ |
| 240 | |
| 241 | if (getSdkVersion() < __ANDROID_API_L__){ |
| 242 | mBuilder.setDirection(Direction::Output); |
| 243 | mBuilder.setFormat(AudioFormat::Unspecified); |
| 244 | openStream(); |
| 245 | ASSERT_EQ(mStream->getFormat(), AudioFormat::I16); |
| 246 | closeStream(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | TEST_F(StreamOpen, PlaybackFormatUnspecifiedReturnsFloatOnLollipopAndLater){ |
| 251 | |
| 252 | if (getSdkVersion() >= __ANDROID_API_L__){ |
| 253 | mBuilder.setDirection(Direction::Output); |
| 254 | mBuilder.setFormat(AudioFormat::Unspecified); |
| 255 | openStream(); |
| 256 | ASSERT_EQ(mStream->getFormat(), AudioFormat::Float); |
| 257 | closeStream(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | TEST_F(StreamOpen, PlaybackFormatFloatReturnsErrorBeforeLollipop){ |
| 262 | |
| 263 | if (getSdkVersion() < __ANDROID_API_L__){ |
| 264 | mBuilder.setDirection(Direction::Output); |
| 265 | mBuilder.setFormat(AudioFormat::Float); |
| 266 | Result r = mBuilder.openStream(&mStream); |
| 267 | ASSERT_EQ(r, Result::ErrorInvalidFormat); |
| 268 | closeStream(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | TEST_F(StreamOpen, PlaybackFormatFloatReturnsFloatOnLollipopAndLater){ |
| 273 | |
| 274 | if (getSdkVersion() >= __ANDROID_API_L__){ |
| 275 | mBuilder.setDirection(Direction::Output); |
| 276 | mBuilder.setFormat(AudioFormat::Float); |
| 277 | openStream(); |
| 278 | ASSERT_EQ(mStream->getFormat(), AudioFormat::Float); |
| 279 | closeStream(); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | TEST_F(StreamOpen, PlaybackFormatI16ReturnsI16){ |
| 284 | |
| 285 | mBuilder.setDirection(Direction::Output); |
| 286 | mBuilder.setFormat(AudioFormat::I16); |
| 287 | openStream(); |
| 288 | ASSERT_EQ(mStream->getFormat(), AudioFormat::I16); |
Phil Burk | 9a6262c | 2018-08-24 18:16:25 -0700 | [diff] [blame] | 289 | closeStream(); |
| 290 | } |
Don Turner | 8c369cf | 2019-01-07 18:02:24 +0000 | [diff] [blame] | 291 | |
| 292 | TEST_F(StreamOpen, LowLatencyStreamHasBufferSizeOfTwoBursts){ |
| 293 | |
| 294 | if (mBuilder.isAAudioRecommended()){ |
| 295 | mBuilder.setDirection(Direction::Output); |
| 296 | mBuilder.setPerformanceMode(PerformanceMode::LowLatency); |
| 297 | openStream(); |
| 298 | ASSERT_EQ(mStream->getBufferSizeInFrames(), mStream->getFramesPerBurst() * 2); |
| 299 | closeStream(); |
| 300 | } |
| 301 | } |