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 #include <fcntl.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 
22 #include <android/content/AttributionSourceState.h>
23 #include <binder/MemoryBase.h>
24 #include <binder/MemoryDealer.h>
25 #include <binder/MemoryHeapBase.h>
26 #include <media/AudioRecord.h>
27 
28 #include "test_create_utils.h"
29 
30 #define NUM_ARGUMENTS 8
31 #define VERSION_VALUE "1.0"
32 #define PACKAGE_NAME "AudioRecord test"
33 
34 namespace android {
35 
36 using android::content::AttributionSourceState;
37 
testRecord(FILE * inputFile,int outputFileFd)38 int testRecord(FILE* inputFile, int outputFileFd) {
39     char line[MAX_INPUT_FILE_LINE_LENGTH];
40     uint32_t testCount = 0;
41     Vector<String16> args;
42     int ret = 0;
43     // TODO b/182392769: use attribution source util
44     AttributionSourceState attributionSource;
45     attributionSource.packageName = std::string(PACKAGE_NAME);
46     attributionSource.token = sp<BBinder>::make();
47 
48     if (inputFile == nullptr) {
49         sp<AudioRecord> record =
50                 new AudioRecord(AUDIO_SOURCE_DEFAULT, 0 /* sampleRate */, AUDIO_FORMAT_DEFAULT,
51                                 AUDIO_CHANNEL_IN_MONO, attributionSource);
52         if (record == 0 || record->initCheck() != NO_ERROR) {
53             write(outputFileFd, "Error creating AudioRecord\n",
54                   sizeof("Error creating AudioRecord\n"));
55         } else {
56             record->dump(outputFileFd, args);
57         }
58         return 0;
59     }
60 
61     // check version
62     if (!checkVersion(inputFile, VERSION_VALUE)) {
63         return 1;
64     }
65 
66     while (readLine(inputFile, line, MAX_INPUT_FILE_LINE_LENGTH) == 0) {
67         uint32_t sampleRate;
68         audio_format_t format;
69         audio_channel_mask_t channelMask;
70         size_t frameCount;
71         int32_t notificationFrames;
72         audio_input_flags_t flags;
73         audio_session_t sessionId;
74         audio_source_t inputSource;
75         audio_attributes_t attributes;
76         status_t status;
77         char statusStr[MAX_OUTPUT_FILE_LINE_LENGTH];
78         bool fast = false;
79 
80         if (sscanf(line, " %u %x %x %zu %d %x %u %u", &sampleRate, &format, &channelMask,
81                    &frameCount, &notificationFrames, &flags, &sessionId,
82                    &inputSource) != NUM_ARGUMENTS) {
83             fprintf(stderr, "Malformed line for test #%u in input file\n", testCount + 1);
84             ret = 1;
85             continue;
86         }
87         testCount++;
88 
89         if ((flags & AUDIO_INPUT_FLAG_FAST) != 0) {
90             fast = true;
91         }
92 
93         memset(&attributes, 0, sizeof(attributes));
94         attributes.source = inputSource;
95 
96         sp<AudioRecord> record = new AudioRecord(attributionSource);
97         const auto emptyCallback = sp<AudioRecord::IAudioRecordCallback>::make();
98 
99         record->set(AUDIO_SOURCE_DEFAULT, sampleRate, format, channelMask, frameCount,
100                     fast ? emptyCallback : nullptr, notificationFrames, false, sessionId,
101                     fast ? AudioRecord::TRANSFER_CALLBACK : AudioRecord::TRANSFER_DEFAULT, flags,
102                     getuid(), getpid(), &attributes, AUDIO_PORT_HANDLE_NONE);
103         status = record->initCheck();
104         sprintf(statusStr, "\n#### Test %u status %d\n", testCount, status);
105         write(outputFileFd, statusStr, strlen(statusStr));
106         if (status != NO_ERROR) {
107             continue;
108         }
109         record->dump(outputFileFd, args);
110     }
111     return ret;
112 }
113 
114 };  // namespace android
115 
main(int argc,char ** argv)116 int main(int argc, char** argv) {
117     return android::main(argc, argv, android::testRecord);
118 }
119