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 "FileAudioRenderSink"
18 #endif
19
20 #include "sink/file_audio_render_sink.h"
21 #include "audio_hdi_log.h"
22 #include "audio_errors.h"
23
24 namespace OHOS {
25 namespace AudioStandard {
~FileAudioRenderSink()26 FileAudioRenderSink::~FileAudioRenderSink()
27 {
28 DeInit();
29 }
30
Init(const IAudioSinkAttr & attr)31 int32_t FileAudioRenderSink::Init(const IAudioSinkAttr &attr)
32 {
33 filePath_.assign(attr.filePath);
34 return SUCCESS;
35 }
36
DeInit(void)37 void FileAudioRenderSink::DeInit(void)
38 {
39 if (file_ != nullptr) {
40 fclose(file_);
41 file_ = nullptr;
42 }
43 }
44
IsInited(void)45 bool FileAudioRenderSink::IsInited(void)
46 {
47 return !filePath_.empty();
48 }
49
Start(void)50 int32_t FileAudioRenderSink::Start(void)
51 {
52 std::string dirPath;
53 std::string fileName;
54
55 auto pos = filePath_.rfind("/");
56 if (pos != std::string::npos) {
57 dirPath = filePath_.substr(0, pos);
58 fileName = filePath_.substr(pos);
59 }
60
61 if (file_ == nullptr) {
62 char realPath[PATH_MAX + 1] = { 0x00 };
63 CHECK_AND_RETURN_RET_LOG((filePath_.length() < PATH_MAX) && (realpath(dirPath.c_str(), realPath) != nullptr),
64 ERR_INVALID_HANDLE, "invalid path, errno: %{public}d", errno);
65
66 std::string realPathStr(realPath);
67 file_ = fopen(realPathStr.append(fileName).c_str(), "wb+");
68 CHECK_AND_RETURN_RET_LOG(file_ != nullptr, ERR_OPERATION_FAILED, "open file fail, errno: %{public}d", errno);
69 }
70
71 return SUCCESS;
72 }
73
Stop(void)74 int32_t FileAudioRenderSink::Stop(void)
75 {
76 if (file_ != nullptr) {
77 fclose(file_);
78 file_ = nullptr;
79 }
80
81 return SUCCESS;
82 }
83
Resume(void)84 int32_t FileAudioRenderSink::Resume(void)
85 {
86 return SUCCESS;
87 }
88
Pause(void)89 int32_t FileAudioRenderSink::Pause(void)
90 {
91 return SUCCESS;
92 }
93
Flush(void)94 int32_t FileAudioRenderSink::Flush(void)
95 {
96 return SUCCESS;
97 }
98
Reset(void)99 int32_t FileAudioRenderSink::Reset(void)
100 {
101 return SUCCESS;
102 }
103
RenderFrame(char & data,uint64_t len,uint64_t & writeLen)104 int32_t FileAudioRenderSink::RenderFrame(char &data, uint64_t len, uint64_t &writeLen)
105 {
106 CHECK_AND_RETURN_RET_LOG(file_ != nullptr, ERR_INVALID_HANDLE, "file is nullptr");
107 size_t realWriteLen = fwrite(static_cast<void *>(&data), 1, len, file_);
108 if (realWriteLen != len) {
109 AUDIO_WARNING_LOG("write file fail");
110 }
111 writeLen = realWriteLen;
112
113 return SUCCESS;
114 }
115
GetVolumeDataCount()116 int64_t FileAudioRenderSink::GetVolumeDataCount()
117 {
118 AUDIO_WARNING_LOG("not supported");
119 return 0;
120 }
121
SuspendRenderSink(void)122 int32_t FileAudioRenderSink::SuspendRenderSink(void)
123 {
124 return SUCCESS;
125 }
126
RestoreRenderSink(void)127 int32_t FileAudioRenderSink::RestoreRenderSink(void)
128 {
129 return SUCCESS;
130 }
131
SetAudioParameter(const AudioParamKey key,const std::string & condition,const std::string & value)132 void FileAudioRenderSink::SetAudioParameter(const AudioParamKey key, const std::string &condition,
133 const std::string &value)
134 {
135 }
136
GetAudioParameter(const AudioParamKey key,const std::string & condition)137 std::string FileAudioRenderSink::GetAudioParameter(const AudioParamKey key, const std::string &condition)
138 {
139 return "";
140 }
141
SetVolume(float left,float right)142 int32_t FileAudioRenderSink::SetVolume(float left, float right)
143 {
144 AUDIO_INFO_LOG("not support");
145 return ERR_NOT_SUPPORTED;
146 }
147
GetVolume(float & left,float & right)148 int32_t FileAudioRenderSink::GetVolume(float &left, float &right)
149 {
150 AUDIO_INFO_LOG("not support");
151 return ERR_NOT_SUPPORTED;
152 }
153
GetLatency(uint32_t & latency)154 int32_t FileAudioRenderSink::GetLatency(uint32_t &latency)
155 {
156 AUDIO_INFO_LOG("not support");
157 return SUCCESS;
158 }
159
GetTransactionId(uint64_t & transactionId)160 int32_t FileAudioRenderSink::GetTransactionId(uint64_t &transactionId)
161 {
162 AUDIO_INFO_LOG("not support");
163 return ERR_NOT_SUPPORTED;
164 }
165
GetPresentationPosition(uint64_t & frames,int64_t & timeSec,int64_t & timeNanoSec)166 int32_t FileAudioRenderSink::GetPresentationPosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec)
167 {
168 AUDIO_INFO_LOG("not support");
169 return ERR_NOT_SUPPORTED;
170 }
171
GetMaxAmplitude(void)172 float FileAudioRenderSink::GetMaxAmplitude(void)
173 {
174 AUDIO_INFO_LOG("not support");
175 return 0;
176 }
177
SetAudioMonoState(bool audioMono)178 void FileAudioRenderSink::SetAudioMonoState(bool audioMono)
179 {
180 AUDIO_INFO_LOG("not support");
181 }
182
SetAudioBalanceValue(float audioBalance)183 void FileAudioRenderSink::SetAudioBalanceValue(float audioBalance)
184 {
185 AUDIO_INFO_LOG("not support");
186 }
187
SetAudioScene(AudioScene audioScene,bool scoExcludeFlag)188 int32_t FileAudioRenderSink::SetAudioScene(AudioScene audioScene, bool scoExcludeFlag)
189 {
190 AUDIO_INFO_LOG("not support");
191 return SUCCESS;
192 }
193
GetAudioScene(void)194 int32_t FileAudioRenderSink::GetAudioScene(void)
195 {
196 AUDIO_INFO_LOG("not support");
197 return ERR_NOT_SUPPORTED;
198 }
199
UpdateActiveDevice(std::vector<DeviceType> & outputDevices)200 int32_t FileAudioRenderSink::UpdateActiveDevice(std::vector<DeviceType> &outputDevices)
201 {
202 AUDIO_INFO_LOG("not support");
203 return ERR_NOT_SUPPORTED;
204 }
205
ResetActiveDeviceForDisconnect(DeviceType device)206 void FileAudioRenderSink::ResetActiveDeviceForDisconnect(DeviceType device)
207 {
208 AUDIO_INFO_LOG("not support");
209 }
210
SetPaPower(int32_t flag)211 int32_t FileAudioRenderSink::SetPaPower(int32_t flag)
212 {
213 AUDIO_INFO_LOG("not support");
214 return ERR_NOT_SUPPORTED;
215 }
216
SetPriPaPower(void)217 int32_t FileAudioRenderSink::SetPriPaPower(void)
218 {
219 AUDIO_INFO_LOG("not support");
220 return ERR_NOT_SUPPORTED;
221 }
222
UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS],const size_t size)223 int32_t FileAudioRenderSink::UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS], const size_t size)
224 {
225 AUDIO_INFO_LOG("not support");
226 return ERR_NOT_SUPPORTED;
227 }
228
UpdateAppsUid(const std::vector<int32_t> & appsUid)229 int32_t FileAudioRenderSink::UpdateAppsUid(const std::vector<int32_t> &appsUid)
230 {
231 AUDIO_INFO_LOG("not support");
232 return ERR_NOT_SUPPORTED;
233 }
234
DumpInfo(std::string & dumpString)235 void FileAudioRenderSink::DumpInfo(std::string &dumpString)
236 {
237 dumpString += "type: FileSink\tfilePath: " + filePath_ + "\n";
238 }
239
SetDmDeviceType(uint16_t dmDeviceType,DeviceType deviceType)240 void FileAudioRenderSink::SetDmDeviceType(uint16_t dmDeviceType, DeviceType deviceType)
241 {
242 AUDIO_INFO_LOG("not support");
243 }
244
245 } // namespace AudioStandard
246 } // namespace OHOS
247