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