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 #undef LOG_TAG
16 #define LOG_TAG "AudioCapturerDfxCollector"
17
18 #include <map>
19
20 #include "audio_capturer_dfx_collector.h"
21 #include "media_monitor_manager.h"
22 #include "audio_common_log.h"
23 #include "dfx_msg_manager.h"
24
25 namespace OHOS {
26 namespace AudioStandard {
27
28
29 static const std::map<RecorderType, DfxRecorderType> DFX_RECORDER_TYPE_MAP = {
30 {RECORDER_TYPE_DEFAULT, DFX_RECORDER_TYPE_NATIVE},
31 {RECORDER_TYPE_ARKTS_AUDIO_RECORDER, DFX_RECORDER_TYPE_ARKTS},
32 {RECORDER_TYPE_OPENSL_ES, DFX_RECORDER_TYPE_OPENSL_ES},
33 {RECORDER_TYPE_AV_RECORDER, DFX_RECORDER_TYPE_AV_RECORDER}
34 };
35
FlushDfxMsg(uint32_t index,int32_t appUid)36 void AudioCapturerDfxCollector::FlushDfxMsg(uint32_t index, int32_t appUid)
37 {
38 if (appUid == DFX_INVALID_APP_UID) {
39 AUDIO_INFO_LOG("flush failed index=%{public}d, appUid=%{public}d", index, appUid);
40 return;
41 }
42
43 std::lock_guard<std::mutex> lock(mutex_);
44 for (auto &item : dfxInfos_) {
45 AUDIO_INFO_LOG("FlushDfxMsg..., index=%{public}u, appUid=%{public}d, size=%{public}d", item.first, appUid,
46 static_cast<int32_t>(item.second.size()));
47 DfxMsgManager::GetInstance().Enqueue({.appUid = appUid, .captureInfo = item.second});
48 }
49 dfxInfos_.clear();
50 }
51
WriteActionMsg(uint32_t dfxIndex,CapturerStage stage)52 CapturerDfxBuilder &CapturerDfxBuilder::WriteActionMsg(uint32_t dfxIndex, CapturerStage stage)
53 {
54 dfxInfo_.capturerAction = {dfxIndex, 0, 0, stage};
55 return *this;
56 }
57
WriteInfoMsg(const AudioCapturerInfo & capturerInfo)58 CapturerDfxBuilder &CapturerDfxBuilder::WriteInfoMsg(const AudioCapturerInfo &capturerInfo)
59 {
60 auto pos = DFX_RECORDER_TYPE_MAP.find(capturerInfo.recorderType);
61 DfxRecorderType recorderType = (pos == DFX_RECORDER_TYPE_MAP.end()) ? DFX_RECORDER_TYPE_NATIVE : pos->second;
62
63 dfxInfo_.capturerInfo = {0, 0, recorderType, capturerInfo.sourceType};
64 return *this;
65 }
66
WriteStatMsg(const AudioProcessConfig & processConfig,const RecordStat & stat)67 CapturerDfxBuilder &CapturerDfxBuilder::WriteStatMsg(const AudioProcessConfig &processConfig, const RecordStat &stat)
68 {
69 dfxInfo_.capturerStat = {processConfig.streamInfo.samplingRate, stat.recordDuration_};
70 return *this;
71 }
72
GetResult()73 CapturerDfxInfo CapturerDfxBuilder::GetResult()
74 {
75 return dfxInfo_;
76 }
77
78 } // namespace AudioStandard
79 } // namespace OHOS
80