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 // Play silence and recover from dead servers or disconnected devices.
18
19 #include <stdio.h>
20
21 #include <aaudio/AAudio.h>
22
23
24 #define DEFAULT_TIMEOUT_NANOS ((int64_t)1000000000)
25
getSharingModeText(aaudio_sharing_mode_t mode)26 static const char *getSharingModeText(aaudio_sharing_mode_t mode) {
27 const char *modeText = "unknown";
28 switch (mode) {
29 case AAUDIO_SHARING_MODE_EXCLUSIVE:
30 modeText = "EXCLUSIVE";
31 break;
32 case AAUDIO_SHARING_MODE_SHARED:
33 modeText = "SHARED";
34 break;
35 default:
36 break;
37 }
38 return modeText;
39 }
40
main(int argc,char ** argv)41 int main(int argc, char **argv) {
42 (void) argc;
43 (void *)argv;
44
45 aaudio_result_t result = AAUDIO_OK;
46
47 int32_t triesLeft = 3;
48 int32_t bufferCapacity;
49 int32_t framesPerBurst = 0;
50 float *buffer = nullptr;
51
52 int32_t actualChannelCount = 0;
53 int32_t actualSampleRate = 0;
54 aaudio_format_t actualDataFormat = AAUDIO_FORMAT_PCM_FLOAT;
55 aaudio_sharing_mode_t actualSharingMode = AAUDIO_SHARING_MODE_SHARED;
56
57 AAudioStreamBuilder *aaudioBuilder = nullptr;
58 AAudioStream *aaudioStream = nullptr;
59
60 // Make printf print immediately so that debug info is not stuck
61 // in a buffer if we hang or crash.
62 setvbuf(stdout, nullptr, _IONBF, (size_t) 0);
63
64 printf("TestRecovery:\n");
65
66 // Use an AAudioStreamBuilder to contain requested parameters.
67 result = AAudio_createStreamBuilder(&aaudioBuilder);
68 if (result != AAUDIO_OK) {
69 printf("AAudio_createStreamBuilder returned %s",
70 AAudio_convertResultToText(result));
71 goto finish;
72 }
73
74 // Request stream properties.
75 AAudioStreamBuilder_setFormat(aaudioBuilder, AAUDIO_FORMAT_PCM_FLOAT);
76
77 while (triesLeft-- > 0) {
78 // Create an AAudioStream using the Builder.
79 result = AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream);
80 if (result != AAUDIO_OK) {
81 printf("AAudioStreamBuilder_openStream returned %s",
82 AAudio_convertResultToText(result));
83 goto finish;
84 }
85
86 // Check to see what kind of stream we actually got.
87 actualSampleRate = AAudioStream_getSampleRate(aaudioStream);
88 actualChannelCount = AAudioStream_getChannelCount(aaudioStream);
89 actualDataFormat = AAudioStream_getFormat(aaudioStream);
90
91 printf("-------- chans = %3d, rate = %6d format = %d\n",
92 actualChannelCount, actualSampleRate, actualDataFormat);
93
94 // This is the number of frames that are read in one chunk by a DMA controller
95 // or a DSP or a mixer.
96 framesPerBurst = AAudioStream_getFramesPerBurst(aaudioStream);
97 bufferCapacity = AAudioStream_getBufferCapacityInFrames(aaudioStream);
98 printf(" bufferCapacity = %d, framesPerBurst = %d\n",
99 bufferCapacity, framesPerBurst);
100
101 int samplesPerBurst = framesPerBurst * actualChannelCount;
102 buffer = new float[samplesPerBurst];
103
104 result = AAudioStream_requestStart(aaudioStream);
105 if (result != AAUDIO_OK) {
106 printf("AAudioStream_requestStart returned %s",
107 AAudio_convertResultToText(result));
108 goto finish;
109 }
110
111 // Play silence for awhile.
112 int32_t framesMax = actualSampleRate * 20;
113 int64_t framesTotal = 0;
114 int64_t printAt = actualSampleRate;
115 while (result == AAUDIO_OK && framesTotal < framesMax) {
116 int32_t framesWritten = AAudioStream_write(aaudioStream,
117 buffer, framesPerBurst,
118 DEFAULT_TIMEOUT_NANOS);
119 if (framesWritten < 0) {
120 result = framesWritten;
121 printf("write() returned %s, frames = %d\n",
122 AAudio_convertResultToText(result), (int)framesTotal);
123 printf(" frames = %d\n", (int)framesTotal);
124 } else if (framesWritten != framesPerBurst) {
125 printf("write() returned %d, frames = %d\n", framesWritten, (int)framesTotal);
126 result = AAUDIO_ERROR_TIMEOUT;
127 } else {
128 framesTotal += framesWritten;
129 if (framesTotal >= printAt) {
130 printf("frames = %d\n", (int)framesTotal);
131 printAt += actualSampleRate;
132 }
133 }
134 }
135 result = AAudioStream_requestStop(aaudioStream);
136 if (result != AAUDIO_OK) {
137 printf("AAudioStream_requestStop returned %s\n",
138 AAudio_convertResultToText(result));
139 }
140 result = AAudioStream_close(aaudioStream);
141 if (result != AAUDIO_OK) {
142 printf("AAudioStream_close returned %s\n",
143 AAudio_convertResultToText(result));
144 }
145 aaudioStream = nullptr;
146 }
147
148 finish:
149 if (aaudioStream != nullptr) {
150 AAudioStream_close(aaudioStream);
151 }
152 AAudioStreamBuilder_delete(aaudioBuilder);
153 delete[] buffer;
154 printf(" result = %d = %s\n", result, AAudio_convertResultToText(result));
155 }
156