• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #include "audio_capturer_file_source.h"
17 
18 #include <cerrno>
19 #include <climits>
20 #include <cstdlib>
21 #include <string>
22 
23 #include "audio_errors.h"
24 #include "audio_log.h"
25 
26 using namespace std;
27 
28 namespace OHOS {
29 namespace AudioStandard {
AudioCapturerFileSource()30 AudioCapturerFileSource::AudioCapturerFileSource()
31 {
32 }
33 
~AudioCapturerFileSource()34 AudioCapturerFileSource::~AudioCapturerFileSource()
35 {
36     AudioCapturerFileSource::DeInit();
37 }
38 
SetVolume(float left,float right)39 int32_t AudioCapturerFileSource::SetVolume(float left, float right)
40 {
41     return SUCCESS;
42 }
43 
GetVolume(float & left,float & right)44 int32_t AudioCapturerFileSource::GetVolume(float &left, float &right)
45 {
46     return SUCCESS;
47 }
48 
SetMute(bool isMute)49 int32_t AudioCapturerFileSource::SetMute(bool isMute)
50 {
51     return SUCCESS;
52 }
53 
GetMute(bool & isMute)54 int32_t AudioCapturerFileSource::GetMute(bool &isMute)
55 {
56     return SUCCESS;
57 }
58 
SetInputRoute(DeviceType inputDevice)59 int32_t AudioCapturerFileSource::SetInputRoute(DeviceType inputDevice)
60 {
61     return SUCCESS;
62 }
63 
RegisterWakeupCloseCallback(IAudioSourceCallback * callback)64 void AudioCapturerFileSource::RegisterWakeupCloseCallback(IAudioSourceCallback* callback)
65 {
66     AUDIO_ERR_LOG("RegisterWakeupCloseCallback FAILED");
67 }
68 
RegisterAudioCapturerSourceCallback(IAudioSourceCallback * callback)69 void AudioCapturerFileSource::RegisterAudioCapturerSourceCallback(IAudioSourceCallback* callback)
70 {
71     AUDIO_ERR_LOG("RegisterAudioCapturerSourceCallback FAILED");
72 }
73 
SetAudioScene(AudioScene audioScene,DeviceType activeDevice)74 int32_t AudioCapturerFileSource::SetAudioScene(AudioScene audioScene, DeviceType activeDevice)
75 {
76     return SUCCESS;
77 }
78 
GetTransactionId(void)79 uint64_t AudioCapturerFileSource::GetTransactionId(void)
80 {
81     uint64_t res = -1L;
82     return res;
83 }
84 
Pause(void)85 int32_t AudioCapturerFileSource::Pause(void)
86 {
87     return SUCCESS;
88 }
89 
Resume(void)90 int32_t AudioCapturerFileSource::Resume(void)
91 {
92     return SUCCESS;
93 }
94 
Reset(void)95 int32_t AudioCapturerFileSource::Reset(void)
96 {
97     return SUCCESS;
98 }
99 
Flush(void)100 int32_t AudioCapturerFileSource::Flush(void)
101 {
102     return SUCCESS;
103 }
104 
IsInited(void)105 bool AudioCapturerFileSource::IsInited(void)
106 {
107     return capturerInited_;
108 }
109 
DeInit()110 void AudioCapturerFileSource::DeInit()
111 {
112     if (filePtr != nullptr) {
113         fclose(filePtr);
114         filePtr = nullptr;
115     }
116     capturerInited_ = false;
117 }
118 
Init(IAudioSourceAttr & attr)119 int32_t AudioCapturerFileSource::Init(IAudioSourceAttr &attr)
120 {
121     const char *filePath = attr.filePath;
122     char realPath[PATH_MAX + 1] = {0x00};
123     std::string sourceFilePath(filePath);
124     std::string rootPath;
125     std::string fileName;
126 
127     auto pos = sourceFilePath.rfind("/");
128     if (pos!= std::string::npos) {
129         rootPath = sourceFilePath.substr(0, pos);
130         fileName = sourceFilePath.substr(pos);
131     }
132 
133     if ((strlen(sourceFilePath.c_str()) >= PATH_MAX) || (realpath(rootPath.c_str(), realPath) == nullptr)) {
134         AUDIO_ERR_LOG("AudioCapturerFileSource:: Invalid path errno = %{public}d", errno);
135         return ERROR;
136     }
137 
138     std::string verifiedPath(realPath);
139     filePtr = fopen(verifiedPath.append(fileName).c_str(), "rb");
140     if (filePtr == nullptr) {
141         AUDIO_ERR_LOG("Error opening pcm test file!");
142         return ERROR;
143     }
144 
145     capturerInited_ = true;
146     return SUCCESS;
147 }
148 
CaptureFrame(char * frame,uint64_t requestBytes,uint64_t & replyBytes)149 int32_t AudioCapturerFileSource::CaptureFrame(char *frame, uint64_t requestBytes, uint64_t &replyBytes)
150 {
151     if (filePtr == nullptr) {
152         AUDIO_ERR_LOG("Invalid filePtr!");
153         return ERROR;
154     }
155 
156     if (feof(filePtr)) {
157         AUDIO_INFO_LOG("End of the file reached, start reading from beginning");
158         rewind(filePtr);
159     }
160 
161     replyBytes = fread(frame, 1, requestBytes, filePtr);
162 
163     return SUCCESS;
164 }
165 
Start(void)166 int32_t AudioCapturerFileSource::Start(void)
167 {
168     return SUCCESS;
169 }
170 
Stop(void)171 int32_t AudioCapturerFileSource::Stop(void)
172 {
173     if (filePtr != nullptr) {
174         fclose(filePtr);
175         filePtr = nullptr;
176     }
177     return SUCCESS;
178 }
179 } // namespace AudioStandard
180 } // namespace OHOS
181