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 "params_config_operator.h"
17 #include "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "file_ex.h"
20 #include "nlohmann/json.hpp"
21
22 namespace OHOS::AVSession {
23 // LCOV_EXCL_START
ParamsConfigOperator()24 ParamsConfigOperator::ParamsConfigOperator()
25 {
26 SLOGI("construct");
27 }
28 // LCOV_EXCL_STOP
29
~ParamsConfigOperator()30 ParamsConfigOperator::~ParamsConfigOperator()
31 {
32 SLOGI("destroy");
33 }
34
GetInstance()35 ParamsConfigOperator& ParamsConfigOperator::GetInstance()
36 {
37 static ParamsConfigOperator paramsConfigOperator;
38 return paramsConfigOperator;
39 }
40
InitConfig()41 void ParamsConfigOperator::InitConfig()
42 {
43 SLOGI("Init configuration params");
44 std::string content;
45 if (!LoadStringFromFile(avsessionFileDir + PARAMS_FILE_NAME, content)) {
46 SLOGE("LoadStringFromFile failed, filename=%{public}s", PARAMS_FILE_NAME);
47 return;
48 }
49 // LCOV_EXCL_START
50 nlohmann::json configs = nlohmann::json::parse(content, nullptr, false);
51 CHECK_AND_RETURN_LOG(configs.is_discarded(), "configs is invalid");
52 SLOGD("InitConfig::parse json object finished");
53 for (auto config : configs.items()) {
54 if (config.value().is_number()) {
55 configIntParams.insert(std::pair<std::string, int32_t>(config.key(), config.value()));
56 }
57 if (config.value().is_string()) {
58 configStringParams.insert(std::pair<std::string, std::string>(config.key(), config.value()));
59 }
60 }
61 // LCOV_EXCL_STOP
62 }
63
GetValueIntByKey(const std::string & key,int32_t * value)64 int32_t ParamsConfigOperator::GetValueIntByKey(const std::string& key, int32_t *value)
65 {
66 auto param = configIntParams.find(key);
67 if (param == configIntParams.end()) {
68 SLOGE("GetValueIntByKey failed, key=%{public}s", key.c_str());
69 return AVSESSION_ERROR;
70 }
71 *value = static_cast<int>(param->second);
72 return AVSESSION_SUCCESS;
73 }
74 }