1 /*
2 * Copyright (C) 2015 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 program for audio_utils FIFO library.
18 // This only tests the single-threaded aspects, not the barriers.
19
20 #include <errno.h>
21 #include <limits.h>
22 #include <memory>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <audio_utils/fifo.h>
26 #include <audio_utils/sndfile.h>
27
28 #ifndef min
29 #define min(x, y) (((x) < (y)) ? (x) : (y))
30 #endif
31
main(int argc,char ** argv)32 int main(int argc, char **argv)
33 {
34 size_t frameCount = 0;
35 size_t maxFramesPerRead = 0;
36 size_t maxFramesPerWrite = 0;
37 bool readerThrottlesWriter = true;
38 bool verbose = false;
39 int i;
40 for (i = 1; i < argc; i++) {
41 char *arg = argv[i];
42 if (arg[0] != '-')
43 break;
44 switch (arg[1]) {
45 case 'f': // FIFO frame count
46 frameCount = atoi(&arg[2]);
47 break;
48 case 'r': // maximum frame count per read from FIFO
49 maxFramesPerRead = atoi(&arg[2]);
50 break;
51 case 't': // disable throttling of writer by reader
52 readerThrottlesWriter = false;
53 break;
54 case 'v': // enable verbose logging
55 verbose = true;
56 break;
57 case 'w': // maximum frame count per write to FIFO
58 maxFramesPerWrite = atoi(&arg[2]);
59 break;
60 default:
61 fprintf(stderr, "%s: unknown option %s\n", argv[0], arg);
62 goto usage;
63 }
64 }
65 if (frameCount == 0) {
66 frameCount = 256;
67 }
68 if (maxFramesPerRead == 0) {
69 maxFramesPerRead = frameCount;
70 }
71 if (maxFramesPerWrite == 0) {
72 maxFramesPerWrite = frameCount;
73 }
74
75 if (argc - i != 2) {
76 usage:
77 fprintf(stderr, "usage: %s [-f#] [-r#] [-t] [-v] [-w#] in.wav out.wav\n", argv[0]);
78 return EXIT_FAILURE;
79 }
80 char *inputFile = argv[i];
81 char *outputFile = argv[i+1];
82
83 SF_INFO sfinfoin;
84 memset(&sfinfoin, 0, sizeof(sfinfoin));
85 SNDFILE *sfin = sf_open(inputFile, SFM_READ, &sfinfoin);
86 if (sfin == NULL) {
87 perror(inputFile);
88 return EXIT_FAILURE;
89 }
90 switch (sfinfoin.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) {
91 case SF_FORMAT_WAV | SF_FORMAT_PCM_16:
92 case SF_FORMAT_WAV | SF_FORMAT_PCM_U8:
93 break;
94 default:
95 fprintf(stderr, "%s: unsupported format\n", inputFile);
96 sf_close(sfin);
97 return EXIT_FAILURE;
98 }
99 size_t frameSize = sizeof(int16_t) * sfinfoin.channels;
100 std::unique_ptr<int16_t[]> inputBuffer(new int16_t[sfinfoin.frames * sfinfoin.channels]);
101 sf_count_t actualRead = sf_readf_short(sfin, inputBuffer.get(), sfinfoin.frames);
102 if (actualRead != sfinfoin.frames) {
103 fprintf(stderr, "%s: unexpected EOF or error\n", inputFile);
104 sf_close(sfin);
105 return EXIT_FAILURE;
106 }
107 sf_close(sfin);
108
109 std::unique_ptr<int16_t[]> outputBuffer(new int16_t[sfinfoin.frames * sfinfoin.channels]);
110 size_t framesWritten = 0;
111 size_t framesRead = 0;
112 std::unique_ptr<int16_t[]> fifoBuffer(new int16_t[frameCount * sfinfoin.channels]);
113 audio_utils_fifo fifo(frameCount, frameSize, fifoBuffer.get(), readerThrottlesWriter);
114 audio_utils_fifo_writer fifoWriter(fifo);
115 audio_utils_fifo_reader fifoReader(fifo, readerThrottlesWriter);
116 int fifoWriteCount = 0, fifoReadCount = 0;
117 int fifoFillLevel = 0, minFillLevel = INT_MAX, maxFillLevel = INT_MIN;
118 for (;;) {
119 size_t framesToWrite = sfinfoin.frames - framesWritten;
120 size_t framesToRead = sfinfoin.frames - framesRead;
121 if (framesToWrite == 0 && (framesToRead == 0 || !readerThrottlesWriter)) {
122 break;
123 }
124
125 if (framesToWrite > maxFramesPerWrite) {
126 framesToWrite = maxFramesPerWrite;
127 }
128 framesToWrite = rand() % (framesToWrite + 1);
129 ssize_t actualWritten = fifoWriter.write(
130 &inputBuffer[framesWritten * sfinfoin.channels], framesToWrite);
131 if (verbose) {
132 printf("wrote %d out of %d\n", (int) actualWritten, (int) framesToWrite);
133 }
134 if (actualWritten < 0 || (size_t) actualWritten > framesToWrite) {
135 fprintf(stderr, "write to FIFO failed\n");
136 break;
137 }
138 if (actualWritten < min((int) frameCount - fifoFillLevel, (int) framesToWrite)) {
139 fprintf(stderr, "only wrote %d when should have written min(%d, %d)\n",
140 (int) actualWritten, (int) frameCount - fifoFillLevel, (int) framesToWrite);
141 }
142 framesWritten += actualWritten;
143 if (actualWritten > 0) {
144 fifoWriteCount++;
145 }
146 fifoFillLevel += actualWritten;
147 if (verbose) {
148 printf("fill level after write %d\n", fifoFillLevel);
149 }
150 if (fifoFillLevel > maxFillLevel) {
151 maxFillLevel = fifoFillLevel;
152 if (maxFillLevel > (int) frameCount) {
153 if (readerThrottlesWriter) {
154 printf("maxFillLevel=%d > frameCount=%d\n", maxFillLevel, (int) frameCount);
155 abort();
156 }
157 }
158 }
159
160 if (framesToRead > maxFramesPerRead) {
161 framesToRead = maxFramesPerRead;
162 }
163 framesToRead = rand() % (framesToRead + 1);
164 ssize_t actualRead = fifoReader.read(
165 &outputBuffer[framesRead * sfinfoin.channels], framesToRead);
166 if (verbose) {
167 printf("read %d out of %d\n", (int) actualRead, (int) framesToRead);
168 }
169 if (actualRead < 0 || (size_t) actualRead > framesToRead) {
170 switch (actualRead) {
171 case -EIO:
172 fprintf(stderr, "read from FIFO failed: corrupted indices\n");
173 abort();
174 break;
175 case -EOVERFLOW:
176 if (readerThrottlesWriter) {
177 fprintf(stderr, "read from FIFO failed: unexpected overflow\n");
178 abort();
179 }
180 printf("warning: reader lost frames\n");
181 actualRead = 0;
182 break;
183 default:
184 if (actualRead < 0) {
185 fprintf(stderr, "read from FIFO failed: unexpected error code %d\n",
186 (int) actualRead);
187 } else {
188 fprintf(stderr, "read from FIFO failed: actualRead=%d > framesToRead=%d\n",
189 (int) actualRead, (int) framesToRead);
190 }
191 abort();
192 }
193 }
194 if (actualRead < min(fifoFillLevel, (int) framesToRead)) {
195 //fprintf(stderr, "only read %d when should have read min(%d, %d)\n",
196 // (int) actualRead, fifoFillLevel, (int) framesToRead);
197 }
198 framesRead += actualRead;
199 if (actualRead > 0) {
200 fifoReadCount++;
201 }
202 fifoFillLevel -= actualRead;
203 if (verbose) {
204 printf("fill level after read %d\n", fifoFillLevel);
205 }
206 if (fifoFillLevel < minFillLevel) {
207 minFillLevel = fifoFillLevel;
208 if (minFillLevel < 0) {
209 printf("minFillLevel=%d < 0\n", minFillLevel);
210 abort();
211 }
212 }
213 }
214
215 printf("FIFO non-empty writes: %d, non-empty reads: %d\n", fifoWriteCount, fifoReadCount);
216 printf("fill=%d, min=%d, max=%d\n", fifoFillLevel, minFillLevel, maxFillLevel);
217
218 printf("writing output\n");
219 SF_INFO sfinfoout;
220 memset(&sfinfoout, 0, sizeof(sfinfoout));
221 sfinfoout.samplerate = sfinfoin.samplerate;
222 sfinfoout.channels = sfinfoin.channels;
223 sfinfoout.format = sfinfoin.format;
224 SNDFILE *sfout = sf_open(outputFile, SFM_WRITE, &sfinfoout);
225 if (sfout == NULL) {
226 perror(outputFile);
227 return EXIT_FAILURE;
228 }
229 sf_count_t actualWritten = sf_writef_short(sfout, outputBuffer.get(), framesRead);
230
231 if (actualWritten != (sf_count_t) framesRead) {
232 fprintf(stderr, "%s: unexpected error\n", outputFile);
233 sf_close(sfout);
234 return EXIT_FAILURE;
235 }
236 sf_close(sfout);
237 printf("done\n");
238
239 return EXIT_SUCCESS;
240 }
241