1/* 2 * Copyright (C) 2023 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 "avcodec_dump_utils.h" 17#include "avcodec_errors.h" 18#include "avcodec_log.h" 19 20namespace { 21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "AVCodecDumpUtils"}; 22 constexpr uint32_t DUMP_LEVEL_4 = 4; 23 constexpr uint32_t DUMP_LEVEL_3 = 3; 24 constexpr uint32_t DUMP_LEVEL_2 = 2; 25 constexpr uint32_t DUMP_SPACE_LENGTH = 4; 26 constexpr uint32_t DUMP_OFFSET_24 = 24; 27 constexpr uint32_t DUMP_OFFSET_16 = 16; 28 constexpr uint32_t DUMP_OFFSET_8 = 8; 29} 30 31namespace OHOS { 32namespace MediaAVCodec { 33using namespace Media; 34int32_t AVCodecDumpControler::AddInfo(const uint32_t dumpIdx, const std::string &name, const std::string &value) 35{ 36 CHECK_AND_RETURN_RET_LOG((dumpIdx >> DUMP_OFFSET_24) > 0, AVCS_ERR_INVALID_VAL, 37 "Add dump info failed, get a invalid dump index."); 38 CHECK_AND_RETURN_RET_LOG(!name.empty(), AVCS_ERR_INVALID_VAL, 39 "Add dump info failed, get a empty name."); 40 if (dumpInfoMap_.find(dumpIdx) != dumpInfoMap_.end()) { 41 AVCODEC_LOGW("Dump info index already exist, index: %{public}d, name: %{public}s.", dumpIdx, name.c_str()); 42 return AVCS_ERR_OK; 43 } 44 45 auto level = GetLevel(dumpIdx); 46 length_[level - 1] = length_[level - 1] > name.length() ? length_[level - 1] : name.length(); 47 dumpInfoMap_.emplace(dumpIdx, make_pair(name, value)); 48 return AVCS_ERR_OK; 49} 50 51int32_t AVCodecDumpControler::AddInfoFromFormat(const uint32_t dumpIdx, const Format &format, 52 const std::string_view &key, const std::string &name) 53{ 54 CHECK_AND_RETURN_RET_LOG(!key.empty(), AVCS_ERR_INVALID_VAL, "Add dump info failed, get a empty key."); 55 56 std::string value; 57 bool ret = false; 58 switch (format.GetValueType(key)) { 59 case FORMAT_TYPE_INT32: { 60 int32_t valueTemp = 0; 61 ret = format.GetIntValue(key, valueTemp); 62 value = std::to_string(valueTemp); 63 break; 64 } 65 case FORMAT_TYPE_INT64: { 66 int64_t valueTemp = 0; 67 ret = format.GetLongValue(key, valueTemp); 68 value = std::to_string(valueTemp); 69 break; 70 } 71 case FORMAT_TYPE_FLOAT: { 72 float valueTemp = 0; 73 ret = format.GetFloatValue(key, valueTemp); 74 value = std::to_string(valueTemp); 75 break; 76 } 77 case FORMAT_TYPE_DOUBLE: { 78 double valueTemp = 0; 79 ret = format.GetDoubleValue(key, valueTemp); 80 value = std::to_string(valueTemp); 81 break; 82 } 83 case FORMAT_TYPE_STRING: { 84 ret = format.GetStringValue(key, value); 85 break; 86 } 87 case FORMAT_TYPE_ADDR: 88 break; 89 default: 90 AVCODEC_LOGE("Add info from format failed. Key: %{public}s", key.data()); 91 } 92 if (ret != true) { 93 return AVCS_ERR_INVALID_VAL; 94 } 95 96 this->AddInfo(dumpIdx, name, value); 97 return AVCS_ERR_OK; 98} 99 100int32_t AVCodecDumpControler::AddInfoFromFormatWithMapping(const uint32_t dumpIdx, 101 const Format &format, const std::string_view &key, 102 const std::string &name, 103 std::map<int32_t, const std::string> mapping) 104{ 105 int32_t val; 106 if (format.GetIntValue(key, val) == true) { 107 auto itMappingString = mapping.find(val); 108 const std::string mappingString = itMappingString != mapping.end() ? itMappingString->second : ""; 109 AddInfo(dumpIdx, name, mappingString); 110 } else { 111 return AVCS_ERR_INVALID_VAL; 112 } 113 return AVCS_ERR_OK; 114} 115 116int32_t AVCodecDumpControler::GetDumpString(std::string &dumpString) 117{ 118 for (auto iter : dumpInfoMap_) { 119 auto level = GetLevel(iter.first); 120 std::string name = iter.second.first; 121 std::string value = iter.second.second; 122 dumpString += std::string((level - 1) * DUMP_SPACE_LENGTH, ' ') 123 + name + std::string(length_[level - 1] - name.length(), ' '); 124 if (!value.empty()) { 125 dumpString += " - " + value; 126 } 127 dumpString += std::string("\n"); 128 } 129 return AVCS_ERR_OK; 130} 131 132uint32_t AVCodecDumpControler::GetLevel(const uint32_t dumpIdx) 133{ 134 uint32_t level = 1; 135 if (dumpIdx & UINT8_MAX) { 136 level = DUMP_LEVEL_4; 137 } else if ((dumpIdx >> DUMP_OFFSET_8) & UINT8_MAX) { 138 level = DUMP_LEVEL_3; 139 } else if ((dumpIdx >> DUMP_OFFSET_16) & UINT8_MAX) { 140 level = DUMP_LEVEL_2; 141 } 142 return level; 143} 144} // namespace OHOS 145} // namespace MediaAVCodec