1 /*
2  * Copyright (c) 2024 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 "AudioLogUtils"
17 #endif
18 
19 #include "audio_log_utils.h"
20 #include <cinttypes>
21 #include "audio_capturer_log.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
25 namespace {
26 static const uint32_t MEDIUM_FREQ_PRINT_LOG = 100;
27 static const uint32_t LOW_FREQ_PRINT_LOG = 1000;
28 }
ProcessVolumeData(const std::string & logTag,const ChannelVolumes & vols,int64_t & count)29 void AudioLogUtils::ProcessVolumeData(const std::string &logTag, const ChannelVolumes &vols, int64_t &count)
30 {
31     bool isDataSilent = true;
32     for (int32_t i = 0; i < vols.channel; i++) {
33         if (vols.volStart[i] != 0) {
34             isDataSilent = false;
35             break;
36         }
37     }
38     if (isDataSilent) {
39         if (count > 0) {
40             AUDIO_INFO_LOG("[%{public}s] not slient %{public}" PRId64 "frames change to slient",
41                 logTag.c_str(), count);
42             count = 0;
43         }
44         count--;
45         IncSilentData(logTag, vols, -count);
46     } else {
47         if (count < 0) {
48             AUDIO_INFO_LOG("[%{public}s] slient %{public}" PRId64 "frames change to not slient",
49                 logTag.c_str(), -count);
50             count = 0;
51         }
52         count++;
53         IncSoundData(logTag, vols, count);
54     }
55 }
56 
IncSilentData(const std::string & logTag,const ChannelVolumes & vols,int64_t count)57 void AudioLogUtils::IncSilentData(const std::string &logTag, const ChannelVolumes &vols, int64_t count)
58 {
59     if ((count < LOW_FREQ_PRINT_LOG && count % MEDIUM_FREQ_PRINT_LOG == 0) ||
60         (count >= LOW_FREQ_PRINT_LOG && count % LOW_FREQ_PRINT_LOG == 0)) {
61         AUDIO_INFO_LOG("[%{public}s], channel: %{public}d, counts: %{public}" PRId64 "",
62             logTag.c_str(), vols.channel, count);
63     }
64 }
65 
IncSoundData(const std::string & logTag,const ChannelVolumes & vols,int64_t count)66 void AudioLogUtils::IncSoundData(const std::string &logTag, const ChannelVolumes &vols, int64_t count)
67 {
68     if ((count < LOW_FREQ_PRINT_LOG && count % MEDIUM_FREQ_PRINT_LOG == 0) ||
69         (count >= LOW_FREQ_PRINT_LOG && count % LOW_FREQ_PRINT_LOG == 0)) {
70         if (vols.channel == MONO) {
71             AUDIO_INFO_LOG("[%{public}s] MONO = %{public}d, counts: %{public}" PRId64 "",
72                 logTag.c_str(), vols.volStart[0], count);
73         } else {
74             AUDIO_INFO_LOG("[%{public}s] channel: %{public}d, L=%{public}d, R=%{public}d, counts: %{public}" PRId64 "",
75                 logTag.c_str(), vols.channel, vols.volStart[0], vols.volStart[1], count);
76         }
77     }
78 }
79 } // namespace AudioStandard
80 } // namespace OHOS
81