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