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