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 
SetAudioScene(AudioScene audioScene,std::vector<DeviceType> & activeDevices)66 int32_t AudioRendererFileSink::SetAudioScene(AudioScene audioScene, std::vector<DeviceType> &activeDevices)
67 {
68     return ERR_NOT_SUPPORTED;
69 }
70 
SetOutputRoutes(std::vector<DeviceType> & outputDevices)71 int32_t AudioRendererFileSink::SetOutputRoutes(std::vector<DeviceType> &outputDevices)
72 {
73     AUDIO_DEBUG_LOG("SetOutputRoutes not supported.");
74     return ERR_NOT_SUPPORTED;
75 }
76 
SetAudioParameter(const AudioParamKey key,const std::string & condition,const std::string & value)77 void AudioRendererFileSink::SetAudioParameter(const AudioParamKey key, const std::string &condition,
78     const std::string &value)
79 {
80     AUDIO_ERR_LOG("AudioRendererFileSink SetAudioParameter not supported.");
81     return;
82 }
83 
GetAudioParameter(const AudioParamKey key,const std::string & condition)84 std::string AudioRendererFileSink::GetAudioParameter(const AudioParamKey key, const std::string &condition)
85 {
86     AUDIO_ERR_LOG("AudioRendererFileSink GetAudioParameter not supported.");
87     return "";
88 }
89 
RegisterParameterCallback(IAudioSinkCallback * callback)90 void AudioRendererFileSink::RegisterParameterCallback(IAudioSinkCallback* callback)
91 {
92     AUDIO_ERR_LOG("AudioRendererFileSink RegisterParameterCallback not supported.");
93 }
94 
SetAudioMonoState(bool audioMono)95 void AudioRendererFileSink::SetAudioMonoState(bool audioMono)
96 {
97     AUDIO_ERR_LOG("AudioRendererFileSink SetAudioMonoState not supported.");
98     return;
99 }
100 
SetAudioBalanceValue(float audioBalance)101 void AudioRendererFileSink::SetAudioBalanceValue(float audioBalance)
102 {
103     AUDIO_ERR_LOG("AudioRendererFileSink SetAudioBalanceValue not supported.");
104     return;
105 }
106 
GetPresentationPosition(uint64_t & frames,int64_t & timeSec,int64_t & timeNanoSec)107 int32_t AudioRendererFileSink::GetPresentationPosition(uint64_t& frames, int64_t& timeSec, int64_t& timeNanoSec)
108 {
109     return ERR_NOT_SUPPORTED;
110 }
111 
DeInit()112 void AudioRendererFileSink::DeInit()
113 {
114     if (filePtr_ != nullptr) {
115         fclose(filePtr_);
116         filePtr_ = nullptr;
117     }
118 }
119 
Init(const IAudioSinkAttr & attr)120 int32_t AudioRendererFileSink::Init(const IAudioSinkAttr &attr)
121 {
122     filePath_.assign(attr.filePath);
123 
124     return SUCCESS;
125 }
126 
RenderFrame(char & data,uint64_t len,uint64_t & writeLen)127 int32_t AudioRendererFileSink::RenderFrame(char &data, uint64_t len, uint64_t &writeLen)
128 {
129     CHECK_AND_RETURN_RET_LOG(filePtr_ != nullptr, ERROR, "Invalid file ptr");
130 
131     size_t writeResult = fwrite(static_cast<void*>(&data), 1, len, filePtr_);
132     if (writeResult != len) {
133         AUDIO_WARNING_LOG("Failed to write the file.");
134     }
135 
136     writeLen = writeResult;
137 
138     return SUCCESS;
139 }
140 
Start(void)141 int32_t AudioRendererFileSink::Start(void)
142 {
143     char realPath[PATH_MAX + 1] = {0x00};
144     std::string rootPath;
145     std::string fileName;
146 
147     auto pos = filePath_.rfind("/");
148     if (pos!= std::string::npos) {
149         rootPath = filePath_.substr(0, pos);
150         fileName = filePath_.substr(pos);
151     }
152 
153     if (filePtr_ == nullptr) {
154         CHECK_AND_RETURN_RET_LOG((filePath_.length() < PATH_MAX) && (realpath(rootPath.c_str(), realPath) != nullptr),
155             ERROR, "AudioRendererFileSink:: Invalid path  errno = %{public}d", errno);
156 
157         std::string verifiedPath(realPath);
158         filePtr_ = fopen(verifiedPath.append(fileName).c_str(), "wb+");
159         CHECK_AND_RETURN_RET_LOG(filePtr_ != nullptr, ERROR, "Failed to open file, errno = %{public}d", errno);
160     }
161 
162     return SUCCESS;
163 }
164 
Stop(void)165 int32_t AudioRendererFileSink::Stop(void)
166 {
167     if (filePtr_ != nullptr) {
168         fclose(filePtr_);
169         filePtr_ = nullptr;
170     }
171 
172     return SUCCESS;
173 }
174 
Pause(void)175 int32_t AudioRendererFileSink::Pause(void)
176 {
177     return SUCCESS;
178 }
179 
Resume(void)180 int32_t AudioRendererFileSink::Resume(void)
181 {
182     return SUCCESS;
183 }
184 
SuspendRenderSink(void)185 int32_t AudioRendererFileSink::SuspendRenderSink(void)
186 {
187     return SUCCESS;
188 }
189 
RestoreRenderSink(void)190 int32_t AudioRendererFileSink::RestoreRenderSink(void)
191 {
192     return SUCCESS;
193 }
194 
Reset(void)195 int32_t AudioRendererFileSink::Reset(void)
196 {
197     return SUCCESS;
198 }
199 
Flush(void)200 int32_t AudioRendererFileSink::Flush(void)
201 {
202     return SUCCESS;
203 }
204 
SetVolume(float left,float right)205 int32_t AudioRendererFileSink::SetVolume(float left, float right)
206 {
207     return ERR_NOT_SUPPORTED;
208 }
209 
GetLatency(uint32_t * latency)210 int32_t AudioRendererFileSink::GetLatency(uint32_t *latency)
211 {
212     return ERR_NOT_SUPPORTED;
213 }
214 
GetTransactionId(uint64_t * transactionId)215 int32_t AudioRendererFileSink::GetTransactionId(uint64_t *transactionId)
216 {
217     AUDIO_ERR_LOG("AudioRendererFileSink %{public}s", __func__);
218     return ERR_NOT_SUPPORTED;
219 }
220 
ResetOutputRouteForDisconnect(DeviceType device)221 void AudioRendererFileSink::ResetOutputRouteForDisconnect(DeviceType device)
222 {
223     AUDIO_WARNING_LOG("not supported.");
224 }
225 
GetMaxAmplitude()226 float AudioRendererFileSink::GetMaxAmplitude()
227 {
228     AUDIO_WARNING_LOG("getMaxAmplitude in render file sink not support");
229     return 0;
230 }
231 
SetPaPower(int32_t flag)232 int32_t AudioRendererFileSink::SetPaPower(int32_t flag)
233 {
234     (void)flag;
235     return ERR_NOT_SUPPORTED;
236 }
237 
SetPriPaPower()238 int32_t AudioRendererFileSink::SetPriPaPower()
239 {
240     return ERR_NOT_SUPPORTED;
241 }
242 
UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS],const size_t size)243 int32_t AudioRendererFileSink::UpdateAppsUid(const int32_t appsUid[MAX_MIX_CHANNELS], const size_t size)
244 {
245     return SUCCESS;
246 }
247 
UpdateAppsUid(const std::vector<int32_t> & appsUid)248 int32_t AudioRendererFileSink::UpdateAppsUid(const std::vector<int32_t> &appsUid)
249 {
250     return SUCCESS;
251 }
252 
253 } // namespace AudioStandard
254 } // namespace OHOS
255