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 // Try to create as many streams as possible and report the maximum.
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <aaudio/AAudio.h>
22 #include <aaudio/AAudioTesting.h>
23 
24 //#define MMAP_POLICY              AAUDIO_UNSPECIFIED
25 //#define MMAP_POLICY              AAUDIO_POLICY_NEVER
26 #define MMAP_POLICY              AAUDIO_POLICY_AUTO
27 //#define MMAP_POLICY              AAUDIO_POLICY_ALWAYS
28 
29 #define MAX_STREAMS   200
30 
testMaxStreams(aaudio_direction_t direction)31 aaudio_result_t testMaxStreams(aaudio_direction_t direction) {
32     aaudio_result_t result = AAUDIO_OK;
33     AAudioStreamBuilder *aaudioBuilder = nullptr;
34     AAudioStream *aaudioStreams[MAX_STREAMS];
35     int32_t numStreams = 0;
36 
37     result = AAudio_createStreamBuilder(&aaudioBuilder);
38     if (result != AAUDIO_OK) {
39         return 1;
40     }
41 
42     AAudioStreamBuilder_setDirection(aaudioBuilder, direction);
43 
44     for (int i = 0; i < MAX_STREAMS; i++) {
45         // Create an AAudioStream using the Builder.
46         result = AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStreams[i]);
47         if (result != AAUDIO_OK) {
48             printf("ERROR could not open AAudio stream, %d %s\n",
49                    result, AAudio_convertResultToText(result));
50             break;
51         } else {
52             printf("AAudio stream[%2d] opened successfully. MMAP = %s\n",
53                    i, AAudioStream_isMMapUsed(aaudioStreams[i]) ? "YES" : "NO");
54             numStreams++;
55         }
56     }
57 
58     printf("Created %d streams!\n", numStreams);
59 
60     // Close all the streams.
61     for (int i = 0; i < numStreams; i++) {
62         result = AAudioStream_close(aaudioStreams[i]);
63         if (result != AAUDIO_OK) {
64             printf("ERROR could not close AAudio stream, %d %s\n",
65                    result, AAudio_convertResultToText(result));
66             break;
67         } else {
68             printf("AAudio stream[%2d] closed successfully.\n", i);
69         }
70     }
71 
72     AAudioStreamBuilder_delete(aaudioBuilder);
73 
74     return result;
75 }
76 
main(int argc,char ** argv)77 int main(int argc, char **argv) {
78     (void)argc; // unused
79     (void)argv; // unused
80 
81     // Make printf print immediately so that debug info is not stuck
82     // in a buffer if we hang or crash.
83     setvbuf(stdout, NULL, _IONBF, (size_t) 0);
84 
85     printf("Try to open a maximum of %d streams.\n", MAX_STREAMS);
86 
87     AAudio_setMMapPolicy(MMAP_POLICY);
88     printf("requested MMapPolicy = %d\n", AAudio_getMMapPolicy());
89 
90     printf("Test AAUDIO_DIRECTION_OUTPUT ---------\n");
91     aaudio_result_t result = testMaxStreams(AAUDIO_DIRECTION_OUTPUT);
92     if (result == AAUDIO_OK) {
93         printf("Test AAUDIO_DIRECTION_INPUT ---------\n");
94         result = testMaxStreams(AAUDIO_DIRECTION_INPUT);
95     }
96 
97     return (result != AAUDIO_OK) ? EXIT_FAILURE : EXIT_SUCCESS;
98 }
99