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 #ifndef LOG_TAG
16 #define LOG_TAG "HpaeCapturerStreamImpl"
17 #endif
18
19 #include "safe_map.h"
20 #include "hpae_capturer_stream_impl.h"
21 #include "audio_errors.h"
22 #include "audio_utils.h"
23 #include "policy_handler.h"
24 #include <iostream>
25 #include <cinttypes>
26 #include "i_hpae_manager.h"
27 #include "audio_engine_log.h"
28 using namespace OHOS::AudioStandard::HPAE;
29 namespace OHOS {
30 namespace AudioStandard {
31 static SafeMap<void *, std::weak_ptr<HpaeCapturerStreamImpl>> paCapturerMap_;
32 const int32_t MIN_BUFFER_SIZE = 2;
33 const int32_t FRAME_LEN_10MS = 2;
34 const int32_t TENMS_PER_SEC = 100;
35
HpaeCapturerStreamImpl(AudioProcessConfig processConfig)36 HpaeCapturerStreamImpl::HpaeCapturerStreamImpl(AudioProcessConfig processConfig)
37 {
38 processConfig_ = processConfig;
39 spanSizeInFrame_ = FRAME_LEN_10MS * processConfig.streamInfo.samplingRate / TENMS_PER_SEC;
40 byteSizePerFrame_ = (processConfig.streamInfo.channels *
41 static_cast<size_t>(GetSizeFromFormat(processConfig.streamInfo.format)));
42 minBufferSize_ = MIN_BUFFER_SIZE * byteSizePerFrame_ * spanSizeInFrame_;
43 }
44
~HpaeCapturerStreamImpl()45 HpaeCapturerStreamImpl::~HpaeCapturerStreamImpl()
46 {
47 AUDIO_INFO_LOG("~HpaeCapturerStreamImpl [%{public}u]", streamIndex_);
48 if (capturerServerDumpFile_) {
49 fclose(capturerServerDumpFile_);
50 capturerServerDumpFile_ = nullptr;
51 }
52 paCapturerMap_.Erase(this);
53 }
54
InitParams(const std::string & deviceName)55 int32_t HpaeCapturerStreamImpl::InitParams(const std::string &deviceName)
56 {
57 paCapturerMap_.Insert(this, weak_from_this());
58
59 HpaeStreamInfo streamInfo;
60 streamInfo.channels = processConfig_.streamInfo.channels;
61 streamInfo.samplingRate = processConfig_.streamInfo.samplingRate;
62 streamInfo.format = processConfig_.streamInfo.format;
63 streamInfo.frameLen = spanSizeInFrame_;
64 streamInfo.sessionId = processConfig_.originalSessionId;
65 streamInfo.streamType = processConfig_.streamType;
66 streamInfo.streamClassType = HPAE_STREAM_CLASS_TYPE_RECORD;
67 streamInfo.sourceType = processConfig_.capturerInfo.sourceType;
68 streamInfo.uid = processConfig_.appInfo.appUid;
69 streamInfo.pid = processConfig_.appInfo.appPid;
70 streamInfo.tokenId = processConfig_.appInfo.appTokenId;
71 streamInfo.deviceName = deviceName;
72 streamInfo.isMoveAble = true;
73 streamInfo.privacyType = processConfig_.privacyType;
74 auto &hpaeManager = IHpaeManager::GetHpaeManager();
75 int32_t ret = hpaeManager.CreateStream(streamInfo);
76 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR_INVALID_PARAM, "CreateStream is error");
77
78 // Register Callback
79 ret = hpaeManager.RegisterStatusCallback(HPAE_STREAM_CLASS_TYPE_RECORD, streamInfo.sessionId, shared_from_this());
80 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR_INVALID_PARAM, "RegisterStatusCallback is error!");
81 ret = hpaeManager.RegisterReadCallback(streamInfo.sessionId, shared_from_this());
82 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR_INVALID_PARAM, "RegisterReadCallback is error!");
83 return SUCCESS;
84 }
85
Start()86 int32_t HpaeCapturerStreamImpl::Start()
87 {
88 AUDIO_INFO_LOG("[%{public}u] Enter", streamIndex_);
89 int32_t ret = IHpaeManager::GetHpaeManager().Start(HPAE_STREAM_CLASS_TYPE_RECORD, processConfig_.originalSessionId);
90 CHECK_AND_RETURN_RET_LOG(ret == 0, ERR_INVALID_PARAM, "Start failed!");
91 state_ = RUNNING;
92 return SUCCESS;
93 }
94
Pause(bool isStandby)95 int32_t HpaeCapturerStreamImpl::Pause(bool isStandby)
96 {
97 AUDIO_INFO_LOG("[%{public}u] Enter", streamIndex_);
98 int32_t ret = IHpaeManager::GetHpaeManager().Pause(HPAE_STREAM_CLASS_TYPE_RECORD, processConfig_.originalSessionId);
99 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_INVALID_PARAM, "Pause error!");
100 return SUCCESS;
101 }
102
GetStreamFramesRead(uint64_t & framesRead)103 int32_t HpaeCapturerStreamImpl::GetStreamFramesRead(uint64_t &framesRead)
104 {
105 framesRead = framesRead_;
106 return SUCCESS;
107 }
108
GetCurrentTimeStamp(uint64_t & timestamp)109 int32_t HpaeCapturerStreamImpl::GetCurrentTimeStamp(uint64_t ×tamp)
110 {
111 std::shared_lock<std::shared_mutex> lock(latencyMutex_);
112 timestamp = timestamp_;
113 return SUCCESS;
114 }
115
GetLatency(uint64_t & latency)116 int32_t HpaeCapturerStreamImpl::GetLatency(uint64_t &latency)
117 {
118 std::shared_lock<std::shared_mutex> lock(latencyMutex_);
119 latency = latency_;
120 AUDIO_DEBUG_LOG("total latency: %{public}" PRIu64 "ms", latency);
121 return SUCCESS;
122 }
123
Flush()124 int32_t HpaeCapturerStreamImpl::Flush()
125 {
126 AUDIO_INFO_LOG("[%{public}u] Enter", streamIndex_);
127 int32_t ret = IHpaeManager::GetHpaeManager().Flush(HPAE_STREAM_CLASS_TYPE_RECORD, processConfig_.originalSessionId);
128 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_INVALID_PARAM, "Flush error!");
129 return SUCCESS;
130 }
131
Stop()132 int32_t HpaeCapturerStreamImpl::Stop()
133 {
134 AUDIO_INFO_LOG("[%{public}u] Enter", streamIndex_);
135 int32_t ret = IHpaeManager::GetHpaeManager().Stop(HPAE_STREAM_CLASS_TYPE_RECORD, processConfig_.originalSessionId);
136 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_INVALID_PARAM, "Stop failed");
137 state_ = STOPPING;
138 return SUCCESS;
139 }
140
Release()141 int32_t HpaeCapturerStreamImpl::Release()
142 {
143 if (state_ == RUNNING) {
144 AUDIO_ERR_LOG("%{public}u Release state_ is RUNNING", processConfig_.originalSessionId);
145 IHpaeManager::GetHpaeManager().Stop(HPAE_STREAM_CLASS_TYPE_RECORD, processConfig_.originalSessionId);
146 }
147 AUDIO_INFO_LOG("[%{public}u] Enter", streamIndex_);
148 int32_t ret = IHpaeManager::GetHpaeManager().Release(HPAE_STREAM_CLASS_TYPE_RECORD,
149 processConfig_.originalSessionId);
150 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERR_INVALID_PARAM, "Release is error");
151 state_ = RELEASED;
152 // to do check closeaudioport
153 if (processConfig_.capturerInfo.sourceType == SOURCE_TYPE_WAKEUP) {
154 PolicyHandler::GetInstance().NotifyWakeUpCapturerRemoved();
155 }
156 return SUCCESS;
157 }
158
RegisterStatusCallback(const std::weak_ptr<IStatusCallback> & callback)159 void HpaeCapturerStreamImpl::RegisterStatusCallback(const std::weak_ptr<IStatusCallback> &callback)
160 {
161 AUDIO_DEBUG_LOG("RegisterStatusCallback in");
162 statusCallback_ = callback;
163 }
164
RegisterReadCallback(const std::weak_ptr<IReadCallback> & callback)165 void HpaeCapturerStreamImpl::RegisterReadCallback(const std::weak_ptr<IReadCallback> &callback)
166 {
167 AUDIO_INFO_LOG("RegisterReadCallback start");
168 readCallback_ = callback;
169 }
170
OnStreamData(AudioCallBackCapturerStreamInfo & callBackStreamInfo)171 int32_t HpaeCapturerStreamImpl::OnStreamData(AudioCallBackCapturerStreamInfo &callBackStreamInfo)
172 {
173 {
174 std::unique_lock<std::shared_mutex> lock(latencyMutex_);
175 timestamp_ = callBackStreamInfo.timestamp;
176 latency_ = callBackStreamInfo.latency;
177 framesRead_ = callBackStreamInfo.framesRead;
178 }
179 if (readCallback_.lock()) {
180 return readCallback_.lock()->OnReadData(callBackStreamInfo.outputData, callBackStreamInfo.requestDataLen);
181 }
182 return SUCCESS;
183 }
184
OnStatusUpdate(IOperation operation,uint32_t streamIndex)185 void HpaeCapturerStreamImpl::OnStatusUpdate(IOperation operation, uint32_t streamIndex)
186 {
187 auto statusCallback = statusCallback_.lock();
188 if (statusCallback) {
189 statusCallback->OnStatusUpdate(operation);
190 }
191 }
192
DequeueBuffer(size_t length)193 BufferDesc HpaeCapturerStreamImpl::DequeueBuffer(size_t length)
194 {
195 BufferDesc bufferDesc;
196 return bufferDesc;
197 }
198
EnqueueBuffer(const BufferDesc & bufferDesc)199 int32_t HpaeCapturerStreamImpl::EnqueueBuffer(const BufferDesc &bufferDesc)
200 {
201 AUDIO_DEBUG_LOG("After capturere EnqueueBuffer");
202 return SUCCESS;
203 }
204
DropBuffer()205 int32_t HpaeCapturerStreamImpl::DropBuffer()
206 {
207 AUDIO_DEBUG_LOG("After capturere DropBuffer");
208 return SUCCESS;
209 }
210
GetMinimumBufferSize(size_t & minBufferSize) const211 int32_t HpaeCapturerStreamImpl::GetMinimumBufferSize(size_t &minBufferSize) const
212 {
213 minBufferSize = minBufferSize_;
214 return SUCCESS;
215 }
216
GetByteSizePerFrame(size_t & byteSizePerFrame) const217 void HpaeCapturerStreamImpl::GetByteSizePerFrame(size_t &byteSizePerFrame) const
218 {
219 byteSizePerFrame = byteSizePerFrame_;
220 }
221
GetSpanSizePerFrame(size_t & spanSizeInFrame) const222 void HpaeCapturerStreamImpl::GetSpanSizePerFrame(size_t &spanSizeInFrame) const
223 {
224 spanSizeInFrame = spanSizeInFrame_;
225 }
226
SetStreamIndex(uint32_t index)227 void HpaeCapturerStreamImpl::SetStreamIndex(uint32_t index)
228 {
229 AUDIO_INFO_LOG("Using index/sessionId %{public}u", index);
230 streamIndex_ = index;
231 }
232
GetStreamIndex()233 uint32_t HpaeCapturerStreamImpl::GetStreamIndex()
234 {
235 return streamIndex_;
236 }
237
AbortCallback(int32_t abortTimes)238 void HpaeCapturerStreamImpl::AbortCallback(int32_t abortTimes)
239 {
240 abortFlag_ += abortTimes;
241 }
242 } // namespace AudioStandard
243 } // namespace OHOS
244