• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 private:
59     std::unique_ptr<IRecorderEngine> recorder_;
60 };
61 
CreateAudioRecorder()62 std::unique_ptr<TestRecorder> TestRecorder::CreateAudioRecorder()
63 {
64     auto engineFactory = std::unique_ptr<OHOS::Media::IEngineFactory>(CreateEngineFactory());
65     auto recorder = engineFactory->CreateRecorderEngine(0, 0, 0, 0); // 0
66     auto obs = std::make_shared<RecorderEngineObs>();
67     recorder->SetObs(obs);
68     return std::make_unique<TestRecorderImpl>(std::move(recorder));
69 }
70 
GetOutputDir()71 std::string TestRecorder::GetOutputDir()
72 {
73     return std::string(HST_WORKING_DIR) + "/" + outputDirName;
74 }
75 
Configure(const AudioRecordSource & recordSource)76 int32_t TestRecorderImpl::Configure(const AudioRecordSource& recordSource)
77 {
78     int32_t audioSourceId = 0;
79     OHOS::AudioStandard::AudioCaptureCreator::GetInstance().SetPcmPath(recordSource.pcmPath_);
80     recorder_->SetAudioSource(recordSource.sourceType_, audioSourceId);
81     recorder_->SetOutputFormat(recordSource.outputFormat_);
82     auto audSampleRate = AudSampleRate {recordSource.sampleRate_};
83     auto audChannel = AudChannel {recordSource.channel_};
84     auto audBitRate = AudBitRate {recordSource.bitRate_};
85     auto auEncoder = AudEnc {recordSource.encodeType_};
86 
87     FALSE_RETURN_V((recorder_->Configure(audioSourceId, audSampleRate)) == 0, ERR_INVALID_VALUE);
88     FALSE_RETURN_V((recorder_->Configure(audioSourceId, audChannel)) == 0, ERR_INVALID_VALUE);
89     FALSE_RETURN_V((recorder_->Configure(audioSourceId, audBitRate)) == 0, ERR_INVALID_VALUE);
90     FALSE_RETURN_V((recorder_->Configure(audioSourceId, auEncoder)) == 0, ERR_INVALID_VALUE);
91 
92     if (recordSource.isFD_) {
93         auto outFileFD = OutFd {recordSource.outFD_};
94         return recorder_->Configure(DUMMY_SOURCE_ID, outFileFD);
95     } else {
96         auto outFilePath = OutFilePath {TestRecorder::GetOutputDir()};
97         return recorder_->Configure(DUMMY_SOURCE_ID, outFilePath); // result record file name
98     }
99 
100     return 0;
101 }
102 
Prepare()103 int32_t TestRecorderImpl::Prepare()
104 {
105     return recorder_->Prepare();
106 }
107 
Start()108 int32_t TestRecorderImpl::Start()
109 {
110     return recorder_->Start();
111 }
112 
Pause()113 int32_t TestRecorderImpl::Pause()
114 {
115     return recorder_->Pause();
116 }
117 
Resume()118 int32_t TestRecorderImpl::Resume()
119 {
120     return recorder_->Resume();
121 }
122 
Stop()123 int32_t TestRecorderImpl::Stop()
124 {
125     return recorder_->Stop();
126 }
127 
Reset()128 int32_t TestRecorderImpl::Reset()
129 {
130     return recorder_->Reset();
131 }
132 
Release()133 int32_t TestRecorderImpl::Release()
134 {
135     recorder_ = nullptr;
136     return 0;
137 }
138 }
139 #endif