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
16 #ifndef LOG_TAG
17 #define LOG_TAG "StreamDfxManager"
18 #endif
19
20 #include "audio_common_log.h"
21 #include "stream_dfx_manager.h"
22 #include "media_monitor_manager.h"
23 #include "event_bean.h"
24 #include <cinttypes>
25
26 namespace {
27 constexpr uint32_t SESSION_TIMEOUT = 2 * 60 * 60 * 1000; // 2 hours
28 }
29
30 namespace OHOS {
31 namespace AudioStandard {
GetInstance()32 StreamDfxManager& StreamDfxManager::GetInstance()
33 {
34 static StreamDfxManager instance;
35 return instance;
36 }
37
CheckStreamOccupancy(uint32_t sessionId,const AudioProcessConfig & processConfig,bool isStart)38 void StreamDfxManager::CheckStreamOccupancy(uint32_t sessionId, const AudioProcessConfig &processConfig, bool isStart)
39 {
40 int64_t currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(
41 std::chrono::system_clock::now().time_since_epoch()).count();
42 AUDIO_DEBUG_LOG("check stream occupancy, sessionId %{public}u, isStart %{public}d, currentTime %{public}" PRId64,
43 sessionId, isStart, currentTime);
44 std::lock_guard<std::mutex> lock(streamMutex_);
45 for (auto item = streamRecordMap_.begin(); item != streamRecordMap_.end(); ++item) {
46 int64_t startTime = item->second.startTime;
47 int64_t lastUploadTime = item->second.lastUploadTime;
48 if ((currentTime - startTime) > SESSION_TIMEOUT && (currentTime - lastUploadTime) > SESSION_TIMEOUT) {
49 ReportStreamOccupancyTimeout(item->first, startTime, currentTime);
50 item->second.lastUploadTime = currentTime;
51 }
52 }
53
54 if (isStart) {
55 StreamRecord streamInfo = {processConfig, currentTime, currentTime};
56 streamRecordMap_[sessionId] = streamInfo;
57 } else {
58 streamRecordMap_.erase(sessionId);
59 }
60 }
61
ReportStreamOccupancyTimeout(uint32_t sessionId,int64_t startTime,int64_t currentTime)62 void StreamDfxManager::ReportStreamOccupancyTimeout(uint32_t sessionId, int64_t startTime, int64_t currentTime)
63 {
64 auto streamInfo = streamRecordMap_.find(sessionId);
65 CHECK_AND_RETURN_LOG(streamInfo != streamRecordMap_.end(), "sessionId not found");
66 AudioProcessConfig &processConfig = streamInfo->second.processConfig;
67 AUDIO_INFO_LOG("sessionId %{public}u timeout, start time %{public}" PRId64 ", "\
68 "current time %{public}" PRId64 ", streamUsage %{public}d, sourcetype %{public}d, uid %{private}d",
69 sessionId, startTime, currentTime, processConfig.rendererInfo.streamUsage,
70 processConfig.capturerInfo.sourceType, processConfig.appInfo.appUid);
71
72 std::shared_ptr<Media::MediaMonitor::EventBean> bean = std::make_shared<Media::MediaMonitor::EventBean>(
73 Media::MediaMonitor::ModuleId::AUDIO, Media::MediaMonitor::EventId::STREAM_OCCUPANCY,
74 Media::MediaMonitor::EventType::DURATION_AGGREGATION_EVENT);
75 CHECK_AND_RETURN_LOG(bean != nullptr, "bean is nullptr");
76
77 bean->Add("IS_PLAYBACK", processConfig.audioMode == AUDIO_MODE_PLAYBACK);
78 bean->Add("SESSIONID", static_cast<int32_t>(sessionId));
79 bean->Add("UID", processConfig.appInfo.appUid);
80 int32_t streamTypeInt = (processConfig.audioMode == AUDIO_MODE_PLAYBACK) ?
81 static_cast<int32_t>(processConfig.rendererInfo.streamUsage) :
82 static_cast<int32_t>(processConfig.capturerInfo.sourceType);
83 bean->Add("STREAM_OR_SOURCE_TYPE", streamTypeInt);
84 bean->Add("START_TIME", static_cast<uint64_t>(startTime));
85 bean->Add("UPLOAD_TIME", static_cast<uint64_t>(currentTime));
86 Media::MediaMonitor::MediaMonitorManager::GetInstance().WriteLogMsg(bean);
87 }
88 } // namespace AudioStandard
89 } // namespace OHOS