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 AUDIO_STREAM_CHECKER 17 #define AUDIO_STREAM_CHECKER 18 #include <mutex> 19 #include <thread> 20 #include <vector> 21 #include <atomic> 22 #include "audio_info.h" 23 #include "audio_stutter.h" 24 25 namespace OHOS { 26 namespace AudioStandard { 27 28 struct CheckerParam { 29 int32_t pid = 0; 30 int32_t callbackId = 0; 31 DataTransferMonitorParam para; 32 bool isMonitorMuteFrame = false; 33 bool isMonitorNoDataFrame = false; 34 int64_t normalFrameCount = 0; 35 int64_t noDataFrameNum = 0; 36 int64_t muteFrameNum = 0; 37 int64_t sumFrameCount = 0; 38 int64_t lastUpdateTime = 0; 39 bool hasInitCheck = false; 40 int64_t standbyStartTime = 0; 41 int64_t standbyStopTime = 0; 42 bool isInStandby = false; 43 DataTransferStateChangeType lastStatus = DATA_TRANS_RESUME; 44 }; 45 46 class AudioStreamChecker : public std::enable_shared_from_this<AudioStreamChecker> { 47 public: 48 AudioStreamChecker(AudioProcessConfig cfg); 49 ~AudioStreamChecker(); 50 void MonitorCheckFrameSub(CheckerParam ¶); 51 void MonitorCheckFrame(); 52 void CleanRecordData(CheckerParam ¶); 53 void MonitorOnCallback(DataTransferStateChangeType type, bool isNeedCallback, CheckerParam ¶); 54 void MonitorOnAllCallback(DataTransferStateChangeType type, bool isStandby); 55 int32_t GetAppUid(); 56 void InitChecker(DataTransferMonitorParam para, const int32_t pid, const int32_t callbackId); 57 void RecordMuteFrame(); 58 void RecordNodataFrame(); 59 void RecordNormalFrame(); 60 void DeleteCheckerPara(const int32_t pid, const int32_t callbackId); 61 void StopCheckStreamThread(); 62 void OnRemoteAppDied(const int32_t pid); 63 void RecordStandbyTime(bool isStart); 64 void UpdateAppState(bool isBackground); 65 void SetVolume(float volume); 66 private: 67 bool IsMonitorMuteFrame(const CheckerParam ¶); 68 bool IsMonitorNoDataFrame(const CheckerParam ¶); 69 void InitCallbackInfo(DataTransferStateChangeType type, AudioRendererDataTransferStateChangeInfo &callbackInfo); 70 void CheckStreamThread(); 71 void MonitorCheckFrameAction(CheckerParam ¶, int64_t abnormalFrameNum, float badFrameRatio); 72 void CalculateFrameAfterStandby(CheckerParam ¶, int64_t &abnormalFrameNum); 73 void CheckVolume(); 74 std::vector<CheckerParam> checkParaVector_; 75 bool monitorSwitch_ = false; 76 bool isBackground_ = false; 77 std::recursive_mutex checkLock_; 78 std::recursive_mutex backgroundStateLock_; 79 AudioProcessConfig streamConfig_; 80 std::thread checkThread_; 81 std::atomic<bool> isKeepCheck_ = false; 82 std::atomic<bool> isNeedCreateThread_ = true; 83 std::mutex volumeLock_; 84 float curVolume_ = 1.0f; 85 float preVolume_ = 1.0f; 86 }; 87 88 } 89 } 90 #endif 91