• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dfx_utils.h"
17 #include "nlohmann/json.hpp"
18 
19 namespace OHOS {
20 namespace AudioStandard {
21 
SerializeToUint32(const DfxStatInt32 & data)22 uint32_t DfxUtils::SerializeToUint32(const DfxStatInt32 &data)
23 {
24     uint32_t result = 0;
25     const uint32_t BYTE_3_OFFSET = 8;
26     const uint32_t BYTE_2_OFFSET = 16;
27     const uint32_t BYTE_1_OFFSET = 24;
28 
29     result |= ((uint32_t)data.fourthByte) & 0xFF;
30     result |= (((uint32_t)data.thirdByte) & 0xFF) << BYTE_3_OFFSET;
31     result |= (((uint32_t)data.secondByte) & 0xFF) << BYTE_2_OFFSET;
32     result |= (((uint32_t)data.firstByte) & 0xFF) << BYTE_1_OFFSET;
33 
34     return result;
35 }
36 
SerializeToJSONString(const RendererStats & data)37 std::string DfxUtils::SerializeToJSONString(const RendererStats &data)
38 {
39     std::string ret{};
40     nlohmann::json json;
41     json["sampleRate"] = data.samplingRate;
42     json["duration"] = data.duration;
43     json["underrunCnt"] = data.underrunCnt;
44     json["originalFlag"] = data.originalFlag;
45     json["zeroDataPercent"] = data.zeroDataPercent;
46 
47     ret = json.dump();
48     return ret;
49 }
50 
SerializeToJSONString(const CapturerStats & data)51 std::string DfxUtils::SerializeToJSONString(const CapturerStats &data)
52 {
53     std::string ret{};
54     nlohmann::json json;
55     json["sampleRate"] = data.samplingRate;
56     json["duration"] = data.duration;
57     ret = json.dump();
58 
59     return ret;
60 }
61 
SerializeToJSONString(const std::vector<InterruptEffect> & data)62 std::string DfxUtils::SerializeToJSONString(const std::vector<InterruptEffect> &data)
63 {
64     std::string ret{};
65     nlohmann::json jsonArray;
66     for (auto &item : data) {
67         nlohmann::json json;
68         json["bundleName"] = item.bundleName;
69         json["streamUsage"] = item.streamUsage;
70         json["appState"] = item.appState;
71         json["interruptEvent"] = item.interruptEvent;
72         jsonArray.push_back(json);
73     }
74 
75     ret = jsonArray.dump();
76     return ret;
77 }
78 
79 template<class T>
SerializeToJSONString(const std::vector<T> & data)80 std::string DfxUtils::SerializeToJSONString(const std::vector<T> &data)
81 {
82     std::string ret{};
83     nlohmann::json jsonArray;
84     for (auto &item : data) {
85         nlohmann::json json;
86         json["value"] = item;
87         jsonArray.push_back(json);
88     }
89 
90     ret = jsonArray.dump();
91     return ret;
92 }
93 
94 template std::string DfxUtils::SerializeToJSONString<uint8_t>(const std::vector<uint8_t> &data);
95 template std::string DfxUtils::SerializeToJSONString<uint32_t>(const std::vector<uint32_t> &data);
96 template std::string DfxUtils::SerializeToJSONString<uint64_t>(const std::vector<uint64_t> &data);
97 template std::string DfxUtils::SerializeToJSONString<std::string>(const std::vector<std::string> &data);
98 } // namespace AudioStandard
99 } // namespace OHOS