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