1 /*
2  * Copyright (C) 2019 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 whether a stream dies if it is written to after a delay.
18 // Maybe because the message queue from the AAudio service fills up.
19 
20 #include <stdio.h>
21 #include <unistd.h>
22 
23 #include <aaudio/AAudio.h>
24 #include <gtest/gtest.h>
25 
26 constexpr int64_t kNanosPerSecond = 1000000000;
27 constexpr int64_t kTimeoutNanos = kNanosPerSecond / 2;
28 constexpr int kNumFrames = 256;
29 constexpr int kChannelCount = 2;
30 
checkFullQueue(aaudio_performance_mode_t perfMode,int32_t sleepMillis)31 static void checkFullQueue(aaudio_performance_mode_t perfMode,
32                            int32_t sleepMillis) {
33     std::unique_ptr<float[]> buffer = std::make_unique<float[]>(
34             kNumFrames * kChannelCount);
35 
36     AAudioStreamBuilder *aaudioBuilder = nullptr;
37 
38     // Use an AAudioStreamBuilder to contain requested parameters.
39     ASSERT_EQ(AAUDIO_OK, AAudio_createStreamBuilder(&aaudioBuilder));
40 
41     AAudioStreamBuilder_setChannelCount(aaudioBuilder, kChannelCount);
42 
43     // Request stream properties.
44     AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, perfMode);
45 
46     // Create an AAudioStream using the Builder.
47     AAudioStream *aaudioStream = nullptr;
48     ASSERT_EQ(AAUDIO_OK, AAudioStreamBuilder_openStream(aaudioBuilder,
49             &aaudioStream));
50     AAudioStreamBuilder_delete(aaudioBuilder);
51 
52     EXPECT_EQ(AAUDIO_OK, AAudioStream_requestStart(aaudioStream));
53 
54     // Sleep for awhile. This might kill the stream.
55     usleep(sleepMillis * 1000); // 1000 millis in a microsecond
56 
57     for (int i = 0; i < 10; i++) {
58         const aaudio_result_t result = AAudioStream_write(aaudioStream,
59                 buffer.get(),
60                 kNumFrames,
61                 kTimeoutNanos);
62         EXPECT_EQ(kNumFrames, result);
63         if (kNumFrames != result) break;
64     }
65 
66     EXPECT_EQ(AAUDIO_OK, AAudioStream_requestStop(aaudioStream));
67 
68     EXPECT_EQ(AAUDIO_OK, AAudioStream_close(aaudioStream));
69 }
70 
TEST(test_full_queue,aaudio_full_queue_perf_none_50)71 TEST(test_full_queue, aaudio_full_queue_perf_none_50) {
72     checkFullQueue(AAUDIO_PERFORMANCE_MODE_NONE, 50 /* sleepMillis */);
73 }
74 
TEST(test_full_queue,aaudio_full_queue_perf_none_200)75 TEST(test_full_queue, aaudio_full_queue_perf_none_200) {
76     checkFullQueue(AAUDIO_PERFORMANCE_MODE_NONE, 200 /* sleepMillis */);
77 }
78 
TEST(test_full_queue,aaudio_full_queue_perf_none_1000)79 TEST(test_full_queue, aaudio_full_queue_perf_none_1000) {
80     checkFullQueue(AAUDIO_PERFORMANCE_MODE_NONE, 1000 /* sleepMillis */);
81 }
82 
TEST(test_full_queue,aaudio_full_queue_low_latency_50)83 TEST(test_full_queue, aaudio_full_queue_low_latency_50) {
84     checkFullQueue(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY, 50 /* sleepMillis */);
85 }
86 
TEST(test_full_queue,aaudio_full_queue_low_latency_200)87 TEST(test_full_queue, aaudio_full_queue_low_latency_200) {
88     checkFullQueue(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY, 200 /* sleepMillis */);
89 }
90 
TEST(test_full_queue,aaudio_full_queue_low_latency_1000)91 TEST(test_full_queue, aaudio_full_queue_low_latency_1000) {
92     checkFullQueue(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY, 1000 /* sleepMillis */);
93 }
94