Phil Burk | 9a9e600 | 2017-11-17 12:17:37 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | // Test various AAudio features including AAudioStream_setBufferSizeInFrames(). |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | //#include <stdlib.h> |
| 21 | //#include <math.h> |
| 22 | |
| 23 | #include <android-base/macros.h> |
| 24 | #include <aaudio/AAudio.h> |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | // Callback function that does nothing. |
| 29 | aaudio_data_callback_result_t MyDataCallbackProc( |
| 30 | AAudioStream *stream, |
| 31 | void *userData, |
| 32 | void *audioData, |
| 33 | int32_t numFrames |
| 34 | ) { |
| 35 | (void) stream; |
| 36 | (void) userData; |
| 37 | (void) audioData; |
| 38 | (void) numFrames; |
| 39 | return AAUDIO_CALLBACK_RESULT_CONTINUE; |
| 40 | } |
| 41 | |
| 42 | // Test AAudioStream_setBufferSizeInFrames() |
| 43 | |
| 44 | //int main() { // To fix Android Studio formatting when editing. |
| 45 | TEST(test_various, aaudio_set_buffer_size) { |
| 46 | |
| 47 | aaudio_result_t result = AAUDIO_OK; |
| 48 | int32_t bufferCapacity; |
| 49 | int32_t framesPerBurst = 0; |
| 50 | int32_t actualSize = 0; |
| 51 | |
| 52 | AAudioStreamBuilder *aaudioBuilder = nullptr; |
| 53 | AAudioStream *aaudioStream = nullptr; |
| 54 | |
| 55 | // Use an AAudioStreamBuilder to contain requested parameters. |
| 56 | ASSERT_EQ(AAUDIO_OK, AAudio_createStreamBuilder(&aaudioBuilder)); |
| 57 | |
| 58 | // Request stream properties. |
| 59 | AAudioStreamBuilder_setDataCallback(aaudioBuilder, MyDataCallbackProc, nullptr); |
| 60 | AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); |
| 61 | |
| 62 | // Create an AAudioStream using the Builder. |
| 63 | EXPECT_EQ(AAUDIO_OK, AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream)); |
| 64 | |
| 65 | // This is the number of frames that are read in one chunk by a DMA controller |
| 66 | // or a DSP or a mixer. |
| 67 | framesPerBurst = AAudioStream_getFramesPerBurst(aaudioStream); |
| 68 | bufferCapacity = AAudioStream_getBufferCapacityInFrames(aaudioStream); |
| 69 | printf(" bufferCapacity = %d, remainder = %d\n", |
| 70 | bufferCapacity, bufferCapacity % framesPerBurst); |
| 71 | |
| 72 | actualSize = AAudioStream_setBufferSizeInFrames(aaudioStream, 0); |
| 73 | EXPECT_GT(actualSize, 0); |
| 74 | EXPECT_LE(actualSize, bufferCapacity); |
| 75 | |
| 76 | actualSize = AAudioStream_setBufferSizeInFrames(aaudioStream, 2 * framesPerBurst); |
| 77 | EXPECT_GT(actualSize, framesPerBurst); |
| 78 | EXPECT_LE(actualSize, bufferCapacity); |
| 79 | |
| 80 | actualSize = AAudioStream_setBufferSizeInFrames(aaudioStream, bufferCapacity - 1); |
| 81 | EXPECT_GT(actualSize, framesPerBurst); |
| 82 | EXPECT_LE(actualSize, bufferCapacity); |
| 83 | |
| 84 | actualSize = AAudioStream_setBufferSizeInFrames(aaudioStream, bufferCapacity); |
| 85 | EXPECT_GT(actualSize, framesPerBurst); |
| 86 | EXPECT_LE(actualSize, bufferCapacity); |
| 87 | |
| 88 | actualSize = AAudioStream_setBufferSizeInFrames(aaudioStream, bufferCapacity + 1); |
| 89 | EXPECT_GT(actualSize, framesPerBurst); |
| 90 | EXPECT_LE(actualSize, bufferCapacity); |
| 91 | |
| 92 | actualSize = AAudioStream_setBufferSizeInFrames(aaudioStream, 1234567); |
| 93 | EXPECT_GT(actualSize, framesPerBurst); |
| 94 | EXPECT_LE(actualSize, bufferCapacity); |
| 95 | |
| 96 | actualSize = AAudioStream_setBufferSizeInFrames(aaudioStream, INT32_MAX); |
| 97 | EXPECT_GT(actualSize, framesPerBurst); |
| 98 | EXPECT_LE(actualSize, bufferCapacity); |
| 99 | |
| 100 | actualSize = AAudioStream_setBufferSizeInFrames(aaudioStream, INT32_MIN); |
| 101 | EXPECT_GT(actualSize, 0); |
| 102 | EXPECT_LE(actualSize, bufferCapacity); |
| 103 | |
| 104 | AAudioStream_close(aaudioStream); |
| 105 | AAudioStreamBuilder_delete(aaudioBuilder); |
| 106 | printf(" result = %d = %s\n", result, AAudio_convertResultToText(result)); |
| 107 | } |