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 {
40 char line[MAX_INPUT_FILE_LINE_LENGTH];
41 uint32_t testCount = 0;
42 Vector<String16> args;
43 int ret = 0;
44 // TODO b/182392769: use attribution source util
45 AttributionSourceState attributionSource;
46 attributionSource.packageName = std::string(PACKAGE_NAME);
47 attributionSource.token = sp<BBinder>::make();
48
49 if (inputFile == nullptr) {
50 sp<AudioRecord> record = new AudioRecord(AUDIO_SOURCE_DEFAULT,
51 0 /* sampleRate */,
52 AUDIO_FORMAT_DEFAULT,
53 AUDIO_CHANNEL_IN_MONO,
54 attributionSource);
55 if (record == 0 || record->initCheck() != NO_ERROR) {
56 write(outputFileFd, "Error creating AudioRecord\n",
57 sizeof("Error creating AudioRecord\n"));
58 } else {
59 record->dump(outputFileFd, args);
60 }
61 return 0;
62 }
63
64 // check version
65 if (!checkVersion(inputFile, VERSION_VALUE)) {
66 return 1;
67 }
68
69 while (readLine(inputFile, line, MAX_INPUT_FILE_LINE_LENGTH) == 0) {
70 uint32_t sampleRate;
71 audio_format_t format;
72 audio_channel_mask_t channelMask;
73 size_t frameCount;
74 int32_t notificationFrames;
75 audio_input_flags_t flags;
76 audio_session_t sessionId;
77 audio_source_t inputSource;
78 audio_attributes_t attributes;
79 status_t status;
80 char statusStr[MAX_OUTPUT_FILE_LINE_LENGTH];
81 bool fast = false;
82
83 if (sscanf(line, " %u %x %x %zu %d %x %u %u",
84 &sampleRate, &format, &channelMask,
85 &frameCount, ¬ificationFrames,
86 &flags, &sessionId, &inputSource) != NUM_ARGUMENTS) {
87 fprintf(stderr, "Malformed line for test #%u in input file\n", testCount+1);
88 ret = 1;
89 continue;
90 }
91 testCount++;
92
93 if ((flags & AUDIO_INPUT_FLAG_FAST) != 0) {
94 fast = true;
95 }
96
97 memset(&attributes, 0, sizeof(attributes));
98 attributes.source = inputSource;
99
100 sp<AudioRecord> record = new AudioRecord(attributionSource);
101
102 record->set(AUDIO_SOURCE_DEFAULT,
103 sampleRate,
104 format,
105 channelMask,
106 frameCount,
107 fast ? callback : nullptr,
108 nullptr,
109 notificationFrames,
110 false,
111 sessionId,
112 fast ? AudioRecord::TRANSFER_CALLBACK : AudioRecord::TRANSFER_DEFAULT,
113 flags,
114 getuid(),
115 getpid(),
116 &attributes,
117 AUDIO_PORT_HANDLE_NONE);
118 status = record->initCheck();
119 sprintf(statusStr, "\n#### Test %u status %d\n", testCount, status);
120 write(outputFileFd, statusStr, strlen(statusStr));
121 if (status != NO_ERROR) {
122 continue;
123 }
124 record->dump(outputFileFd, args);
125 }
126 return ret;
127 }
128
129 }; // namespace android
130
131
main(int argc,char ** argv)132 int main(int argc, char **argv)
133 {
134 return android::main(argc, argv, android::testRecord);
135 }
136
137