1 /*
2  * Copyright (C) 2020 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 // #define LOG_NDEBUG 0
18 #define LOG_TAG "StagefrightRecorderTest"
19 #include <utils/Log.h>
20 
21 #include <gtest/gtest.h>
22 
23 #include <chrono>
24 #include <ctime>
25 #include <iostream>
26 #include <string>
27 #include <thread>
28 
29 #include <MediaPlayerService.h>
30 #include <media/NdkMediaExtractor.h>
31 #include <media/stagefright/MediaCodec.h>
32 #include <system/audio.h>
33 
34 #include "StagefrightRecorder.h"
35 
36 #define OUTPUT_INFO_FILE_NAME "/data/local/tmp/stfrecorder_audio.info"
37 #define OUTPUT_FILE_NAME_AUDIO "/data/local/tmp/stfrecorder_audio.raw"
38 
39 const bool kDebug = false;
40 constexpr int32_t kMaxLoopCount = 10;
41 constexpr int32_t kClipDurationInSec = 4;
42 constexpr int32_t kPauseTimeInSec = 2;
43 // Tolerance value for extracted clipduration is maximum 10% of total clipduration
44 constexpr int32_t kToleranceValueInUs = kClipDurationInSec * 100000;
45 
46 using namespace android;
47 
48 class StagefrightRecorderTest
49     : public ::testing::TestWithParam<std::pair<output_format, audio_encoder>> {
50   public:
StagefrightRecorderTest()51     StagefrightRecorderTest() : mStfRecorder(nullptr), mOutputAudioFp(nullptr) {
52         mExpectedDurationInMs = 0;
53         mExpectedPauseInMs = 0;
54     }
55 
~StagefrightRecorderTest()56     ~StagefrightRecorderTest() {
57         if (mStfRecorder) free(mStfRecorder);
58         if (mOutputAudioFp) fclose(mOutputAudioFp);
59     }
60 
SetUp()61     void SetUp() override {
62         // TODO b/182392769: use attribution source util
63         AttributionSourceState attributionSource;
64         attributionSource.packageName = std::string(LOG_TAG);
65         attributionSource.token = sp<BBinder>::make();
66         mStfRecorder = new StagefrightRecorder(attributionSource);
67         ASSERT_NE(mStfRecorder, nullptr) << "Failed to create the instance of recorder";
68 
69         mOutputAudioFp = fopen(OUTPUT_FILE_NAME_AUDIO, "wb");
70         ASSERT_NE(mOutputAudioFp, nullptr) << "Failed to open output file "
71                                            << OUTPUT_FILE_NAME_AUDIO << " for stagefright recorder";
72 
73         int32_t fd = fileno(mOutputAudioFp);
74         ASSERT_GE(fd, 0) << "Failed to get the file descriptor of the output file for "
75                          << OUTPUT_FILE_NAME_AUDIO;
76 
77         status_t status = mStfRecorder->setOutputFile(fd);
78         ASSERT_EQ(status, OK) << "Failed to set the output file " << OUTPUT_FILE_NAME_AUDIO
79                               << " for stagefright recorder";
80     }
81 
TearDown()82     void TearDown() override {
83         if (mOutputAudioFp) {
84             fclose(mOutputAudioFp);
85             mOutputAudioFp = nullptr;
86         }
87         if (!kDebug) {
88             int32_t status = remove(OUTPUT_FILE_NAME_AUDIO);
89             ASSERT_EQ(status, 0) << "Unable to delete the output file " << OUTPUT_FILE_NAME_AUDIO;
90         }
91     }
92 
93     void setAudioRecorderFormat(output_format outputFormat, audio_encoder encoder,
94                                 audio_source_t audioSource = AUDIO_SOURCE_DEFAULT);
95     void recordMedia(bool isPaused = false, int32_t numStart = 0, int32_t numPause = 0);
96     void dumpInfo();
97     void setupExtractor(AMediaExtractor *extractor, int32_t &trackCount);
98     void validateOutput();
99 
100     MediaRecorderBase *mStfRecorder;
101     FILE *mOutputAudioFp;
102     double mExpectedDurationInMs;
103     double mExpectedPauseInMs;
104 };
105 
setAudioRecorderFormat(output_format outputFormat,audio_encoder encoder,audio_source_t audioSource)106 void StagefrightRecorderTest::setAudioRecorderFormat(output_format outputFormat,
107                                                      audio_encoder encoder,
108                                                      audio_source_t audioSource) {
109     status_t status = mStfRecorder->setAudioSource(audioSource);
110     ASSERT_EQ(status, OK) << "Failed to set the audio source: " << audioSource;
111 
112     status = mStfRecorder->setOutputFormat(outputFormat);
113     ASSERT_EQ(status, OK) << "Failed to set the output format: " << outputFormat;
114 
115     status = mStfRecorder->setAudioEncoder(encoder);
116     ASSERT_EQ(status, OK) << "Failed to set the audio encoder: " << encoder;
117 }
118 
recordMedia(bool isPause,int32_t numStart,int32_t numPause)119 void StagefrightRecorderTest::recordMedia(bool isPause, int32_t numStart, int32_t numPause) {
120     status_t status = mStfRecorder->init();
121     ASSERT_EQ(status, OK) << "Failed to initialize stagefright recorder";
122 
123     status = mStfRecorder->prepare();
124     ASSERT_EQ(status, OK) << "Failed to preapre the reorder";
125 
126     // first start should succeed.
127     status = mStfRecorder->start();
128     ASSERT_EQ(status, OK) << "Failed to start the recorder";
129 
130     for (int32_t count = 0; count < numStart; count++) {
131         status = mStfRecorder->start();
132     }
133 
134     auto tStart = std::chrono::high_resolution_clock::now();
135     // Recording media for 4 secs
136     std::this_thread::sleep_for(std::chrono::seconds(kClipDurationInSec));
137     auto tEnd = std::chrono::high_resolution_clock::now();
138     mExpectedDurationInMs = std::chrono::duration<double, std::milli>(tEnd - tStart).count();
139 
140     if (isPause) {
141         // first pause should succeed.
142         status = mStfRecorder->pause();
143         ASSERT_EQ(status, OK) << "Failed to pause the recorder";
144 
145         tStart = std::chrono::high_resolution_clock::now();
146         // Paused recorder for 2 secs
147         std::this_thread::sleep_for(std::chrono::seconds(kPauseTimeInSec));
148 
149         for (int32_t count = 0; count < numPause; count++) {
150             status = mStfRecorder->pause();
151         }
152 
153         tEnd = std::chrono::high_resolution_clock::now();
154         mExpectedPauseInMs = std::chrono::duration<double, std::milli>(tEnd - tStart).count();
155 
156         status = mStfRecorder->resume();
157         ASSERT_EQ(status, OK) << "Failed to resume the recorder";
158 
159         auto tStart = std::chrono::high_resolution_clock::now();
160         // Recording media for 4 secs
161         std::this_thread::sleep_for(std::chrono::seconds(kClipDurationInSec));
162         auto tEnd = std::chrono::high_resolution_clock::now();
163         mExpectedDurationInMs += std::chrono::duration<double, std::milli>(tEnd - tStart).count();
164     }
165     status = mStfRecorder->stop();
166     ASSERT_EQ(status, OK) << "Failed to stop the recorder";
167 }
168 
dumpInfo()169 void StagefrightRecorderTest::dumpInfo() {
170     FILE *dumpOutput = fopen(OUTPUT_INFO_FILE_NAME, "wb");
171     int32_t dumpFd = fileno(dumpOutput);
172     Vector<String16> args;
173     status_t status = mStfRecorder->dump(dumpFd, args);
174     ASSERT_EQ(status, OK) << "Failed to dump the info for the recorder";
175     fclose(dumpOutput);
176 }
177 
setupExtractor(AMediaExtractor * extractor,int32_t & trackCount)178 void StagefrightRecorderTest::setupExtractor(AMediaExtractor *extractor, int32_t &trackCount) {
179     int32_t fd = open(OUTPUT_FILE_NAME_AUDIO, O_RDONLY);
180     ASSERT_GE(fd, 0) << "Failed to open recorder's output file " << OUTPUT_FILE_NAME_AUDIO
181                      << " to validate";
182 
183     struct stat buf;
184     int32_t status = fstat(fd, &buf);
185     ASSERT_EQ(status, 0) << "Failed to get properties of input file " << OUTPUT_FILE_NAME_AUDIO
186                          << " for extractor";
187 
188     size_t fileSize = buf.st_size;
189     ASSERT_GT(fileSize, 0) << "Size of input file " << OUTPUT_FILE_NAME_AUDIO
190                            << " to extractor cannot be zero";
191     ALOGV("Size of input file to extractor: %zu", fileSize);
192 
193     status = AMediaExtractor_setDataSourceFd(extractor, fd, 0, fileSize);
194     ASSERT_EQ(status, AMEDIA_OK) << "Failed to set data source for extractor";
195 
196     trackCount = AMediaExtractor_getTrackCount(extractor);
197     ALOGV("Number of tracks reported by extractor : %d", trackCount);
198 }
199 
200 // Validate recoder's output using extractor
validateOutput()201 void StagefrightRecorderTest::validateOutput() {
202     int32_t trackCount = -1;
203     AMediaExtractor *extractor = AMediaExtractor_new();
204     ASSERT_NE(extractor, nullptr) << "Failed to create extractor";
205     ASSERT_NO_FATAL_FAILURE(setupExtractor(extractor, trackCount));
206     ASSERT_EQ(trackCount, 1) << "Expected 1 track, saw " << trackCount;
207 
208     for (int32_t idx = 0; idx < trackCount; idx++) {
209         AMediaExtractor_selectTrack(extractor, idx);
210         AMediaFormat *format = AMediaExtractor_getTrackFormat(extractor, idx);
211         ASSERT_NE(format, nullptr) << "Track format is NULL";
212         ALOGI("Track format = %s", AMediaFormat_toString(format));
213 
214         int64_t clipDurationUs;
215         AMediaFormat_getInt64(format, AMEDIAFORMAT_KEY_DURATION, &clipDurationUs);
216         int32_t diff = abs((mExpectedDurationInMs * 1000) - clipDurationUs);
217         ASSERT_LE(diff, kToleranceValueInUs)
218                 << "Expected duration: " << (mExpectedDurationInMs * 1000)
219                 << " Actual duration: " << clipDurationUs << " Difference: " << diff
220                 << " Difference is expected to be less than tolerance value: " << kToleranceValueInUs;
221 
222         const char *mime = nullptr;
223         AMediaFormat_getString(format, AMEDIAFORMAT_KEY_MIME, &mime);
224         ASSERT_NE(mime, nullptr) << "Track mime is NULL";
225         ALOGI("Track mime = %s", mime);
226 
227         int32_t sampleRate, channelCount, bitRate;
228         AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_CHANNEL_COUNT, &channelCount);
229         ALOGI("Channel count reported by extractor: %d", channelCount);
230         AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, &sampleRate);
231         ALOGI("Sample Rate reported by extractor: %d", sampleRate);
232         AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, &bitRate);
233         ALOGI("Bit Rate reported by extractor: %d", bitRate);
234     }
235 }
236 
TEST_F(StagefrightRecorderTest,RecordingAudioSanityTest)237 TEST_F(StagefrightRecorderTest, RecordingAudioSanityTest) {
238     ASSERT_NO_FATAL_FAILURE(setAudioRecorderFormat(OUTPUT_FORMAT_DEFAULT, AUDIO_ENCODER_DEFAULT));
239 
240     int32_t maxAmplitude = -1;
241     status_t status = mStfRecorder->getMaxAmplitude(&maxAmplitude);
242     ASSERT_EQ(maxAmplitude, 0) << "Invalid value of max amplitude";
243 
244     ASSERT_NO_FATAL_FAILURE(recordMedia());
245 
246     // Verify getMetrics() behavior
247     Parcel parcel;
248     status = mStfRecorder->getMetrics(&parcel);
249     ASSERT_EQ(status, OK) << "Failed to get the parcel from getMetrics";
250     ALOGV("Size of the Parcel returned by getMetrics: %zu", parcel.dataSize());
251     ASSERT_GT(parcel.dataSize(), 0) << "Parcel size reports empty record";
252     ASSERT_NO_FATAL_FAILURE(validateOutput());
253     if (kDebug) {
254         ASSERT_NO_FATAL_FAILURE(dumpInfo());
255     }
256 }
257 
TEST_P(StagefrightRecorderTest,MultiFormatAudioRecordTest)258 TEST_P(StagefrightRecorderTest, MultiFormatAudioRecordTest) {
259     output_format outputFormat = GetParam().first;
260     audio_encoder audioEncoder = GetParam().second;
261     ASSERT_NO_FATAL_FAILURE(setAudioRecorderFormat(outputFormat, audioEncoder));
262     ASSERT_NO_FATAL_FAILURE(recordMedia());
263     // TODO(b/161687761)
264     // Skip for AMR-NB/WB output format
265     if (!(outputFormat == OUTPUT_FORMAT_AMR_NB || outputFormat == OUTPUT_FORMAT_AMR_WB)) {
266         ASSERT_NO_FATAL_FAILURE(validateOutput());
267     }
268     if (kDebug) {
269         ASSERT_NO_FATAL_FAILURE(dumpInfo());
270     }
271 }
272 
TEST_F(StagefrightRecorderTest,GetActiveMicrophonesTest)273 TEST_F(StagefrightRecorderTest, GetActiveMicrophonesTest) {
274     ASSERT_NO_FATAL_FAILURE(
275             setAudioRecorderFormat(OUTPUT_FORMAT_DEFAULT, AUDIO_ENCODER_DEFAULT, AUDIO_SOURCE_MIC));
276 
277     status_t status = mStfRecorder->init();
278     ASSERT_EQ(status, OK) << "Init failed for stagefright recorder";
279 
280     status = mStfRecorder->prepare();
281     ASSERT_EQ(status, OK) << "Failed to preapre the reorder";
282 
283     status = mStfRecorder->start();
284     ASSERT_EQ(status, OK) << "Failed to start the recorder";
285 
286     // Record media for 4 secs
287     std::this_thread::sleep_for(std::chrono::seconds(kClipDurationInSec));
288 
289     std::vector<media::MicrophoneInfo> activeMicrophones{};
290     status = mStfRecorder->getActiveMicrophones(&activeMicrophones);
291     ASSERT_EQ(status, OK) << "Failed to get Active Microphones";
292     ASSERT_GT(activeMicrophones.size(), 0) << "No active microphones are found";
293 
294     status = mStfRecorder->stop();
295     ASSERT_EQ(status, OK) << "Failed to stop the recorder";
296     if (kDebug) {
297         ASSERT_NO_FATAL_FAILURE(dumpInfo());
298     }
299 }
300 
TEST_F(StagefrightRecorderTest,MultiStartPauseTest)301 TEST_F(StagefrightRecorderTest, MultiStartPauseTest) {
302     ASSERT_NO_FATAL_FAILURE(setAudioRecorderFormat(OUTPUT_FORMAT_DEFAULT, AUDIO_ENCODER_DEFAULT));
303     ASSERT_NO_FATAL_FAILURE(recordMedia(true, kMaxLoopCount, kMaxLoopCount));
304     ASSERT_NO_FATAL_FAILURE(validateOutput());
305     if (kDebug) {
306         ASSERT_NO_FATAL_FAILURE(dumpInfo());
307     }
308 }
309 
310 INSTANTIATE_TEST_SUITE_P(
311         StagefrightRecorderTestAll, StagefrightRecorderTest,
312         ::testing::Values(std::make_pair(OUTPUT_FORMAT_AMR_NB, AUDIO_ENCODER_AMR_NB),
313                           std::make_pair(OUTPUT_FORMAT_AMR_WB, AUDIO_ENCODER_AMR_WB),
314                           std::make_pair(OUTPUT_FORMAT_AAC_ADTS, AUDIO_ENCODER_AAC),
315                           std::make_pair(OUTPUT_FORMAT_OGG, AUDIO_ENCODER_OPUS)));
316 
main(int argc,char ** argv)317 int main(int argc, char **argv) {
318     ::testing::InitGoogleTest(&argc, argv);
319     int status = RUN_ALL_TESTS();
320     ALOGV("Test result = %d\n", status);
321     return status;
322 }
323