1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <chrono>
17 #include <fcntl.h>
18 #include <format.h>
19 #include <fstream>
20 #include <thread>
21 #include "gtest/gtest.h"
22 #include "scenetest/helper/test_player.hpp"
23 #include "foundation/log.h"
24 #include "foundation/osal/filesystem/file_system.h"
25 #include "i_engine_factory.h"
26 #include "i_recorder_engine.h"
27 #include "recorder_param.h"
28 #include "media_errors.h"
29 #ifndef WIN32
30 #include <sys/types.h>
31 #include <unistd.h>
32 #define O_BINARY 0 // which is not defined for Linux
33 #define RESOURCE_DIR "/data/test/media/"
34 #define HST_WORKING_DIR "/data/test/media/"
35 using namespace testing::ext;
36 #endif
37 
38 extern "C" {
39 __attribute__((visibility("default"))) OHOS::Media::IEngineFactory* CreateEngineFactory();
40 }
41 
42 using namespace OHOS::Media::Plugin;
43 using namespace OHOS::Media::Test;
44 
45 namespace OHOS {
46 namespace Media {
47 namespace Test {
48 
49 using namespace OHOS::Media;
50 
51 class RecorderEngineObs : public IRecorderEngineObs {
52 public:
53     ~RecorderEngineObs() override = default;
54 
OnError(ErrorType errorType,int32_t errorCode)55     void OnError(ErrorType errorType, int32_t errorCode) override
56     {
57         std::cout<< "error"<< errorCode <<std::endl;
58     }
59 
OnInfo(InfoType type,int32_t extra)60     void OnInfo(InfoType type, int32_t extra) override
61     {
62     }
63 };
64 
CreateRecorder()65 std::shared_ptr<IRecorderEngine> CreateRecorder()
66 {
67     auto engineFactory = std::shared_ptr<OHOS::Media::IEngineFactory>(CreateEngineFactory());
68     auto recorder = engineFactory->CreateRecorderEngine(0, 0, 0, 0); // 0
69     auto obs = std::make_shared<RecorderEngineObs>();
70     recorder->SetObs(obs);
71     return recorder;
72 }
73 
CheckAudio(std::string filePath)74 void CheckAudio(std::string filePath)
75 {
76     std::unique_ptr<TestPlayer> player = TestPlayer::Create();
77     ASSERT_EQ(0, player->SetSource(TestSource(filePath)));
78     ASSERT_EQ(0, player->Prepare());
79     ASSERT_EQ(0, player->Play());
80     std::this_thread::sleep_for(std::chrono::milliseconds(500)); // 500 MS
81     ASSERT_EQ(0, player->Stop());
82     ASSERT_EQ(0, player->Release());
83 }
84 
85 HWTEST(TestFastAudioRecorder, Test_single_audio_fd_recorder_1, TestSize.Level1)
86 {
87     std::string filePath = std::string(std::string(HST_WORKING_DIR) + "/test.m4a");
88     OHOS::Media::OSAL::FileSystem::MakeMultipleDir(std::string(HST_WORKING_DIR));
89     OHOS::Media::OSAL::FileSystem::RemoveFilesInDir(std::string(HST_WORKING_DIR));
90     int fd;
91     fd = open(filePath.c_str(), O_RDWR | O_CREAT | O_BINARY, 0644); // 0644, permission;
92     ASSERT_TRUE(fd >= 0);
93     auto recorder = CreateRecorder();
94     int32_t audioSourceId = 0;
95     recorder->SetAudioSource(AudioSourceType::AUDIO_MIC, audioSourceId);
96     recorder->SetOutputFormat(OutputFormatType::FORMAT_M4A);
97     auto audSampleRate = AudSampleRate{44100};
98     auto audChannel = AudChannel{2};
99     auto audBitRate = AudBitRate{320000};
100     auto auEncoder = AudEnc{AudioCodecFormat::AAC_LC};
101     recorder->Configure(audioSourceId, audSampleRate);
102     recorder->Configure(audioSourceId, audChannel);
103     recorder->Configure(audioSourceId, audBitRate);
104     recorder->Configure(audioSourceId, auEncoder);
105     auto outFileFD = OutFd {fd};
106     recorder->Configure(DUMMY_SOURCE_ID, outFileFD);
107     ASSERT_EQ(0, recorder->Prepare());
108     ASSERT_EQ(0, recorder->Start());
109     std::this_thread::sleep_for(std::chrono::milliseconds(50));
110     ASSERT_EQ(0, recorder->Stop());
111     ASSERT_EQ(0, close(fd));
112 }
113 
114 HWTEST(TestFastAudioRecorder, Test_single_audio_fd_recorder_2, TestSize.Level1)
115 {
116     std::string filePath = std::string(std::string(HST_WORKING_DIR) + "/test.m4a");
117     OHOS::Media::OSAL::FileSystem::MakeMultipleDir(std::string(HST_WORKING_DIR));
118     OHOS::Media::OSAL::FileSystem::RemoveFilesInDir(std::string(HST_WORKING_DIR));
119     int fd;
120     fd = open(filePath.c_str(), O_RDWR | O_CREAT | O_BINARY, 0644); // 0644, permission;
121     ASSERT_TRUE(fd >= 0);
122     auto recorder = CreateRecorder();
123     int32_t audioSourceId = 0;
124     recorder->SetAudioSource(AudioSourceType::AUDIO_MIC, audioSourceId);
125     recorder->SetOutputFormat(OutputFormatType::FORMAT_M4A);
126     auto audSampleRate = AudSampleRate{44100};
127     auto audChannel = AudChannel{2};
128     auto audBitRate = AudBitRate{320000};
129     auto auEncoder = AudEnc{AudioCodecFormat::AAC_LC};
130     recorder->Configure(audioSourceId, audSampleRate);
131     recorder->Configure(audioSourceId, audChannel);
132     recorder->Configure(audioSourceId, audBitRate);
133     recorder->Configure(audioSourceId, auEncoder);
134     auto outFileFD = OutFd {fd};
135     recorder->Configure(DUMMY_SOURCE_ID, outFileFD);
136     ASSERT_EQ(0, recorder->Prepare());
137     ASSERT_EQ(0, recorder->Start());
138     std::this_thread::sleep_for(std::chrono::milliseconds(50));
139     ASSERT_EQ(0, recorder->Stop());
140     std::this_thread::sleep_for(std::chrono::milliseconds(50));
141     ASSERT_EQ(0, recorder->Reset());
142     ASSERT_EQ(0, close(fd));
143 }
144 
145 HWTEST(TestFastAudioRecorder, Test_single_audio_fd_recorder_3, TestSize.Level1)
146 {
147     std::string filePath = std::string(std::string(HST_WORKING_DIR) + "/test.m4a");
148     OHOS::Media::OSAL::FileSystem::MakeMultipleDir(std::string(HST_WORKING_DIR));
149     OHOS::Media::OSAL::FileSystem::RemoveFilesInDir(std::string(HST_WORKING_DIR));
150     int fd;
151     fd = open(filePath.c_str(), O_RDWR | O_CREAT | O_BINARY, 0644); // 0644, permission;
152     ASSERT_TRUE(fd >= 0);
153     auto recorder = CreateRecorder();
154     int32_t audioSourceId = 0;
155     recorder->SetAudioSource(AudioSourceType::AUDIO_MIC, audioSourceId);
156     recorder->SetOutputFormat(OutputFormatType::FORMAT_M4A);
157     auto audSampleRate = AudSampleRate{44100};
158     auto audChannel = AudChannel{2};
159     auto audBitRate = AudBitRate{320000};
160     auto auEncoder = AudEnc{AudioCodecFormat::AAC_LC};
161     recorder->Configure(audioSourceId, audSampleRate);
162     recorder->Configure(audioSourceId, audChannel);
163     recorder->Configure(audioSourceId, audBitRate);
164     recorder->Configure(audioSourceId, auEncoder);
165     auto outFileFD = OutFd {fd};
166     recorder->Configure(DUMMY_SOURCE_ID, outFileFD);
167     ASSERT_EQ(0, recorder->Prepare());
168     ASSERT_EQ(0, recorder->Start());
169     std::this_thread::sleep_for(std::chrono::milliseconds(50));
170     ASSERT_EQ(0, recorder->Pause());
171     std::this_thread::sleep_for(std::chrono::milliseconds(50));
172     ASSERT_EQ(0, recorder->Stop());
173     ASSERT_EQ(0, close(fd));
174 }
175 
176 HWTEST(TestFastAudioRecorder, Test_single_audio_fd_recorder_4, TestSize.Level1)
177 {
178     std::string filePath = std::string(std::string(HST_WORKING_DIR) + "/test.m4a");
179     OHOS::Media::OSAL::FileSystem::MakeMultipleDir(std::string(HST_WORKING_DIR));
180     OHOS::Media::OSAL::FileSystem::RemoveFilesInDir(std::string(HST_WORKING_DIR));
181     int fd;
182     fd = open(filePath.c_str(), O_RDWR | O_CREAT | O_BINARY, 0644); // 0644, permission;
183     ASSERT_TRUE(fd >= 0);
184     auto recorder = CreateRecorder();
185     int32_t audioSourceId = 0;
186     recorder->SetAudioSource(AudioSourceType::AUDIO_MIC, audioSourceId);
187     recorder->SetOutputFormat(OutputFormatType::FORMAT_M4A);
188     auto audSampleRate = AudSampleRate{44100};
189     auto audChannel = AudChannel{2};
190     auto audBitRate = AudBitRate{320000};
191     auto auEncoder = AudEnc{AudioCodecFormat::AAC_LC};
192     recorder->Configure(audioSourceId, audSampleRate);
193     recorder->Configure(audioSourceId, audChannel);
194     recorder->Configure(audioSourceId, audBitRate);
195     recorder->Configure(audioSourceId, auEncoder);
196     auto outFileFD = OutFd {fd};
197     recorder->Configure(DUMMY_SOURCE_ID, outFileFD);
198     ASSERT_EQ(0, recorder->Prepare());
199     ASSERT_EQ(0, recorder->Start());
200     std::this_thread::sleep_for(std::chrono::milliseconds(50));
201     ASSERT_EQ(0, recorder->Pause());
202     std::this_thread::sleep_for(std::chrono::milliseconds(50));
203     ASSERT_EQ(0, recorder->Stop());
204     std::this_thread::sleep_for(std::chrono::milliseconds(50));
205     ASSERT_EQ(0, recorder->Reset());
206     ASSERT_EQ(0, close(fd));
207 }
208 
209 HWTEST(TestFastAudioRecorder, Test_single_audio_fd_recorder_5, TestSize.Level1)
210 {
211     std::string filePath = std::string(std::string(HST_WORKING_DIR) + "/test.m4a");
212     OHOS::Media::OSAL::FileSystem::MakeMultipleDir(std::string(HST_WORKING_DIR));
213     OHOS::Media::OSAL::FileSystem::RemoveFilesInDir(std::string(HST_WORKING_DIR));
214     int fd;
215     fd = open(filePath.c_str(), O_RDWR | O_CREAT | O_BINARY, 0644); // 0644, permission;
216     ASSERT_TRUE(fd >= 0);
217     auto recorder = CreateRecorder();
218     int32_t audioSourceId = 0;
219     recorder->SetAudioSource(AudioSourceType::AUDIO_MIC, audioSourceId);
220     recorder->SetOutputFormat(OutputFormatType::FORMAT_M4A);
221     auto audSampleRate = AudSampleRate{44100};
222     auto audChannel = AudChannel{2};
223     auto audBitRate = AudBitRate{320000};
224     auto auEncoder = AudEnc{AudioCodecFormat::AAC_LC};
225     recorder->Configure(audioSourceId, audSampleRate);
226     recorder->Configure(audioSourceId, audChannel);
227     recorder->Configure(audioSourceId, audBitRate);
228     recorder->Configure(audioSourceId, auEncoder);
229     auto outFileFD = OutFd {fd};
230     recorder->Configure(DUMMY_SOURCE_ID, outFileFD);
231     ASSERT_EQ(0, recorder->Prepare());
232     ASSERT_EQ(0, recorder->Start());
233     std::this_thread::sleep_for(std::chrono::milliseconds(500));
234     ASSERT_EQ(0, recorder->Pause());
235     std::this_thread::sleep_for(std::chrono::milliseconds(1000));
236     ASSERT_EQ(0, recorder->Resume());
237     std::this_thread::sleep_for(std::chrono::milliseconds(500));
238     ASSERT_EQ(0, recorder->Stop());
239     CheckAudio(filePath);
240     ASSERT_EQ(0, close(fd));
241 }
242 
243 } // namespace Test
244 } // namespace Media
245 } // namespace OHOS
246