1 /*
2 * Copyright (c) 2022-2022 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 #ifndef OHOS_LITE
16 #include "test_recorder.hpp"
17
18 #include <thread>
19 #include <chrono>
20 #include <dirent.h>
21 #include <sys/stat.h>
22
23 #include "i_engine_factory.h"
24 #include "foundation/log.h"
25
26 extern "C" {
27 __attribute__((visibility("default"))) OHOS::Media::IEngineFactory* CreateEngineFactory();
28 }
29
30 using namespace OHOS::Media;
31
32 namespace OHOS::Media::Test {
33 class RecorderEngineObs : public IRecorderEngineObs {
34 public:
35 ~RecorderEngineObs() override = default;
36
OnError(ErrorType errorType,int32_t errorCode)37 void OnError(ErrorType errorType, int32_t errorCode) override
38 {
39 MEDIA_LOG_I("player_framework recorder error : " PUBLIC_LOG_D32, errorCode);
40 }
41
OnInfo(InfoType type,int32_t extra)42 void OnInfo(InfoType type, int32_t extra) override
43 {
44 }
45 };
46
47 class TestRecorderImpl : public TestRecorder {
48 public:
TestRecorderImpl(std::unique_ptr<IRecorderEngine> recorder)49 explicit TestRecorderImpl(std::unique_ptr<IRecorderEngine> recorder) : recorder_(std::move(recorder)) {}
50 int32_t Configure(const AudioRecordSource& recordSource) override;
51 int32_t Prepare() override;
52 int32_t Start() override;
53 int32_t Pause() override;
54 int32_t Resume() override;
55 int32_t Stop() override;
56 int32_t Reset() override;
57 int32_t Release() override;
58 int32_t GetRecordedFile(std::string& path) override;
59 private:
60 std::unique_ptr<IRecorderEngine> recorder_;
61 };
62
CreateAudioRecorder()63 std::unique_ptr<TestRecorder> TestRecorder::CreateAudioRecorder()
64 {
65 auto engineFactory = std::unique_ptr<OHOS::Media::IEngineFactory>(CreateEngineFactory());
66 auto recorder = engineFactory->CreateRecorderEngine(0, 0, 0); // 0
67 auto obs = std::make_shared<RecorderEngineObs>();
68 recorder->SetObs(obs);
69 return std::make_unique<TestRecorderImpl>(std::move(recorder));
70 }
71
GetOutputDir()72 std::string TestRecorder::GetOutputDir()
73 {
74 return std::string(HST_WORKING_DIR) + "/" + outputDirName;
75 }
76
Configure(const AudioRecordSource & recordSource)77 int32_t TestRecorderImpl::Configure(const AudioRecordSource& recordSource)
78 {
79 int32_t audioSourceId = 0;
80 OHOS::AudioStandard::AudioCaptureCreator::GetInstance().SetPcmPath(recordSource.pcmPath_);
81 recorder_->SetAudioSource(recordSource.sourceType_, audioSourceId);
82 recorder_->SetOutputFormat(recordSource.outputFormat_);
83 auto audSampleRate = AudSampleRate {recordSource.sampleRate_};
84 auto audChannel = AudChannel {recordSource.channel_};
85 auto audBitRate = AudBitRate {recordSource.bitRate_};
86 auto auEncoder = AudEnc {recordSource.encodeType_};
87
88 FALSE_RETURN_V((recorder_->Configure(audioSourceId, audSampleRate)) == 0, ERR_INVALID_VALUE);
89 FALSE_RETURN_V((recorder_->Configure(audioSourceId, audChannel)) == 0, ERR_INVALID_VALUE);
90 FALSE_RETURN_V((recorder_->Configure(audioSourceId, audBitRate)) == 0, ERR_INVALID_VALUE);
91 FALSE_RETURN_V((recorder_->Configure(audioSourceId, auEncoder)) == 0, ERR_INVALID_VALUE);
92
93 if (recordSource.isFD_) {
94 auto outFileFD = OutFd {recordSource.outFD_};
95 return recorder_->Configure(DUMMY_SOURCE_ID, outFileFD);
96 } else {
97 auto outFilePath = OutFilePath {TestRecorder::GetOutputDir()};
98 return recorder_->Configure(DUMMY_SOURCE_ID, outFilePath); // result record file name
99 }
100
101 return 0;
102 }
103
Prepare()104 int32_t TestRecorderImpl::Prepare()
105 {
106 return recorder_->Prepare();
107 }
108
Start()109 int32_t TestRecorderImpl::Start()
110 {
111 return recorder_->Start();
112 }
113
Pause()114 int32_t TestRecorderImpl::Pause()
115 {
116 return recorder_->Pause();
117 }
118
Resume()119 int32_t TestRecorderImpl::Resume()
120 {
121 return recorder_->Resume();
122 }
123
Stop()124 int32_t TestRecorderImpl::Stop()
125 {
126 return recorder_->Stop();
127 }
128
Reset()129 int32_t TestRecorderImpl::Reset()
130 {
131 return recorder_->Reset();
132 }
133
Release()134 int32_t TestRecorderImpl::Release()
135 {
136 recorder_ = nullptr;
137 return 0;
138 }
139
140 // The file in output dir is the latest recorded file.
GetRecordedFile(std::string & path)141 int32_t TestRecorderImpl::GetRecordedFile(std::string& path)
142 {
143 DIR *directory;
144 struct dirent *info;
145 if ((directory = opendir(GetOutputDir().c_str())) != nullptr) {
146 while ((info = readdir(directory)) != nullptr) {
147 if (strcmp(info->d_name, ".") == 0 || strcmp(info->d_name, "..") == 0) {
148 continue;
149 }
150 path = GetOutputDir() + "/" + info->d_name;
151 MEDIA_LOG_D("GetRecordedFile : " PUBLIC_LOG_S, path.c_str());
152 }
153 closedir(directory);
154 }
155
156 int32_t fileSize = 0;
157 struct stat fileStatus {};
158 if (stat(path.c_str(), &fileStatus) == 0) {
159 fileSize = static_cast<int32_t>(fileStatus.st_size);
160 }
161 return fileSize;
162 }
163 }
164 #endif