1 /*
2 * Copyright (c) 2025 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 #ifndef LOG_TAG
17 #define LOG_TAG "FileAudioCaptureSource"
18 #endif
19
20 #include "source/file_audio_capture_source.h"
21 #include "audio_hdi_log.h"
22 #include "audio_errors.h"
23
24 namespace OHOS {
25 namespace AudioStandard {
~FileAudioCaptureSource()26 FileAudioCaptureSource::~FileAudioCaptureSource()
27 {
28 DeInit();
29 }
30
Init(const IAudioSourceAttr & attr)31 int32_t FileAudioCaptureSource::Init(const IAudioSourceAttr &attr)
32 {
33 std::string filePath(attr.filePath);
34 std::string dirPath;
35 std::string fileName;
36
37 auto pos = filePath.rfind("/");
38 if (pos != std::string::npos) {
39 dirPath = filePath.substr(0, pos);
40 fileName = filePath.substr(pos);
41 }
42
43 char realPath[PATH_MAX + 1] = { 0x00 };
44 CHECK_AND_RETURN_RET_LOG((filePath.length() < PATH_MAX) && (realpath(dirPath.c_str(), realPath) != nullptr),
45 ERR_INVALID_HANDLE, "invalid path, errno: %{public}d", errno);
46
47 std::string realPathStr(realPath);
48 file_ = fopen(realPathStr.append(fileName).c_str(), "rb");
49 CHECK_AND_RETURN_RET_LOG(file_ != nullptr, ERROR, "open file fail, errno: %{public}d", errno);
50
51 sourceInited_ = true;
52 return SUCCESS;
53 }
54
DeInit(void)55 void FileAudioCaptureSource::DeInit(void)
56 {
57 if (file_ != nullptr) {
58 fclose(file_);
59 file_ = nullptr;
60 }
61
62 sourceInited_ = false;
63 }
64
IsInited(void)65 bool FileAudioCaptureSource::IsInited(void)
66 {
67 return sourceInited_;
68 }
69
Start(void)70 int32_t FileAudioCaptureSource::Start(void)
71 {
72 return SUCCESS;
73 }
74
Stop(void)75 int32_t FileAudioCaptureSource::Stop(void)
76 {
77 if (file_ != nullptr) {
78 fclose(file_);
79 file_ = nullptr;
80 }
81
82 return SUCCESS;
83 }
84
Resume(void)85 int32_t FileAudioCaptureSource::Resume(void)
86 {
87 return SUCCESS;
88 }
89
Pause(void)90 int32_t FileAudioCaptureSource::Pause(void)
91 {
92 return SUCCESS;
93 }
94
Flush(void)95 int32_t FileAudioCaptureSource::Flush(void)
96 {
97 return SUCCESS;
98 }
99
Reset(void)100 int32_t FileAudioCaptureSource::Reset(void)
101 {
102 return SUCCESS;
103 }
104
CaptureFrame(char * frame,uint64_t requestBytes,uint64_t & replyBytes)105 int32_t FileAudioCaptureSource::CaptureFrame(char *frame, uint64_t requestBytes, uint64_t &replyBytes)
106 {
107 CHECK_AND_RETURN_RET_LOG(file_ != nullptr, ERROR, "file is nullptr");
108 if (feof(file_)) {
109 AUDIO_INFO_LOG("reach end of the file, start reading from beginning");
110 rewind(file_);
111 }
112 replyBytes = fread(frame, 1, requestBytes, file_);
113
114 return SUCCESS;
115 }
116
CaptureFrameWithEc(FrameDesc * fdesc,uint64_t & replyBytes,FrameDesc * fdescEc,uint64_t & replyBytesEc)117 int32_t FileAudioCaptureSource::CaptureFrameWithEc(FrameDesc *fdesc, uint64_t &replyBytes, FrameDesc *fdescEc,
118 uint64_t &replyBytesEc)
119 {
120 AUDIO_INFO_LOG("not support");
121 return ERR_NOT_SUPPORTED;
122 }
123
GetAudioParameter(const AudioParamKey key,const std::string & condition)124 std::string FileAudioCaptureSource::GetAudioParameter(const AudioParamKey key, const std::string &condition)
125 {
126 return "";
127 }
128
SetVolume(float left,float right)129 int32_t FileAudioCaptureSource::SetVolume(float left, float right)
130 {
131 return SUCCESS;
132 }
133
GetVolume(float & left,float & right)134 int32_t FileAudioCaptureSource::GetVolume(float &left, float &right)
135 {
136 return SUCCESS;
137 }
138
SetMute(bool isMute)139 int32_t FileAudioCaptureSource::SetMute(bool isMute)
140 {
141 return SUCCESS;
142 }
143
GetMute(bool & isMute)144 int32_t FileAudioCaptureSource::GetMute(bool &isMute)
145 {
146 return SUCCESS;
147 }
148
GetTransactionId(void)149 uint64_t FileAudioCaptureSource::GetTransactionId(void)
150 {
151 uint64_t res = -1L;
152 return res;
153 }
154
GetPresentationPosition(uint64_t & frames,int64_t & timeSec,int64_t & timeNanoSec)155 int32_t FileAudioCaptureSource::GetPresentationPosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec)
156 {
157 return SUCCESS;
158 }
159
GetMaxAmplitude(void)160 float FileAudioCaptureSource::GetMaxAmplitude(void)
161 {
162 AUDIO_INFO_LOG("not support");
163 return 0;
164 }
165
SetAudioScene(AudioScene audioScene,DeviceType activeDevice)166 int32_t FileAudioCaptureSource::SetAudioScene(AudioScene audioScene, DeviceType activeDevice)
167 {
168 return SUCCESS;
169 }
170
UpdateActiveDevice(DeviceType inputDevice)171 int32_t FileAudioCaptureSource::UpdateActiveDevice(DeviceType inputDevice)
172 {
173 return SUCCESS;
174 }
175
UpdateAppsUid(const int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE],const size_t size)176 int32_t FileAudioCaptureSource::UpdateAppsUid(const int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE], const size_t size)
177 {
178 AUDIO_INFO_LOG("not support");
179 return ERR_NOT_SUPPORTED;
180 }
181
UpdateAppsUid(const std::vector<int32_t> & appsUid)182 int32_t FileAudioCaptureSource::UpdateAppsUid(const std::vector<int32_t> &appsUid)
183 {
184 AUDIO_INFO_LOG("not support");
185 return ERR_NOT_SUPPORTED;
186 }
187
DumpInfo(std::string & dumpString)188 void FileAudioCaptureSource::DumpInfo(std::string &dumpString)
189 {
190 dumpString += "type: FileSource\n";
191 }
192
193 } // namespace AudioStandard
194 } // namespace OHOS
195