• 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     json["frameWritten"] = data.frameWritten;
47 
48     ret = json.dump();
49     return ret;
50 }
51 
SerializeToJSONString(const CapturerStats & data)52 std::string DfxUtils::SerializeToJSONString(const CapturerStats &data)
53 {
54     std::string ret{};
55     nlohmann::json json;
56     json["sampleRate"] = data.samplingRate;
57     json["duration"] = data.duration;
58     ret = json.dump();
59 
60     return ret;
61 }
62 
SerializeToJSONString(const std::vector<InterruptEffect> & data)63 std::string DfxUtils::SerializeToJSONString(const std::vector<InterruptEffect> &data)
64 {
65     std::string ret{};
66     nlohmann::json jsonArray;
67     for (auto &item : data) {
68         nlohmann::json json;
69         json["bundleName"] = item.bundleName;
70         json["streamUsage"] = item.streamUsage;
71         json["appState"] = item.appState;
72         json["interruptEvent"] = item.interruptEvent;
73         jsonArray.push_back(json);
74     }
75 
76     ret = jsonArray.dump();
77     return ret;
78 }
79 
80 template<class T>
SerializeToJSONString(const std::vector<T> & data)81 std::string DfxUtils::SerializeToJSONString(const std::vector<T> &data)
82 {
83     std::string ret{};
84     nlohmann::json jsonArray;
85     for (auto &item : data) {
86         nlohmann::json json;
87         json["value"] = item;
88         jsonArray.push_back(json);
89     }
90 
91     ret = jsonArray.dump();
92     return ret;
93 }
94 
95 template std::string DfxUtils::SerializeToJSONString<uint8_t>(const std::vector<uint8_t> &data);
96 template std::string DfxUtils::SerializeToJSONString<uint32_t>(const std::vector<uint32_t> &data);
97 template std::string DfxUtils::SerializeToJSONString<uint64_t>(const std::vector<uint64_t> &data);
98 template std::string DfxUtils::SerializeToJSONString<std::string>(const std::vector<std::string> &data);
99 } // namespace AudioStandard
100 } // namespace OHOS