1 /*
2 * Copyright (c) 2023-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 "napi_avcall_state.h"
17 #include "napi_utils.h"
18
19 namespace OHOS::AVSession {
20 std::map<std::string, NapiAVCallState::GetterType> NapiAVCallState::getterMap_ = {
21 { "state", GetAVCallState },
22 { "muted", IsAVCallMuted },
23 };
24
25 std::map<int32_t, NapiAVCallState::SetterType> NapiAVCallState::setterMap_ = {
26 { AVCallState::AVCALL_STATE_KEY_STATE, SetAVCallState },
27 { AVCallState::AVCALL_STATE_KEY_IS_MUTED, SetAVCallMuted },
28 };
29
30 std::map<std::string, int32_t> NapiAVCallState::filterMap_ = {
31 { "state", AVCallState::AVCALL_STATE_KEY_STATE },
32 { "muted", AVCallState::AVCALL_STATE_KEY_IS_MUTED },
33 };
34
ConvertFilter(napi_env env,napi_value filter,AVCallState::AVCallStateMaskType & mask)35 napi_status NapiAVCallState::ConvertFilter(napi_env env, napi_value filter,
36 AVCallState::AVCallStateMaskType& mask)
37 {
38 napi_valuetype type = napi_undefined;
39 auto status = napi_typeof(env, filter, &type);
40 CHECK_RETURN(status == napi_ok, "napi_typeof failed", status);
41
42 if (type == napi_string) {
43 std::string stringFilter;
44 status = NapiUtils::GetValue(env, filter, stringFilter);
45 CHECK_RETURN(status == napi_ok, "get string filter failed", status);
46 CHECK_RETURN(stringFilter == "all", "string filter only support all", napi_invalid_arg);
47
48 mask.set();
49 return napi_ok;
50 }
51
52 uint32_t count = 0;
53 status = napi_get_array_length(env, filter, &count);
54 CHECK_RETURN(status == napi_ok, "get array length failed", status);
55 for (uint32_t i = 0; i < count; i++) {
56 napi_value value {};
57 status = napi_get_element(env, filter, i, &value);
58 CHECK_RETURN(status == napi_ok, "get element failed", status);
59 std::string metaKey;
60 status = NapiUtils::GetValue(env, value, metaKey);
61 CHECK_RETURN(status == napi_ok, "get string value failed", status);
62 for (const auto& pair : filterMap_) {
63 if (pair.first == metaKey) {
64 mask.set(pair.second);
65 }
66 }
67 }
68 CHECK_RETURN(!mask.none(), "array element invalid.", napi_invalid_arg);
69
70 return napi_ok;
71 }
72
GetValue(napi_env env,napi_value in,AVCallState & out)73 napi_status NapiAVCallState::GetValue(napi_env env, napi_value in, AVCallState& out)
74 {
75 std::vector<std::string> propertyNames;
76 auto status = NapiUtils::GetPropertyNames(env, in, propertyNames);
77 CHECK_RETURN(status == napi_ok, "get property name failed", status);
78 CHECK_RETURN(propertyNames.size() == getterMap_.size(), "avcallstate property count invalid", napi_invalid_arg);
79
80 for (const auto& name : propertyNames) {
81 auto it = getterMap_.find(name);
82 if (it == getterMap_.end()) {
83 SLOGE("property %{public}s is not of metadata", name.c_str());
84 return napi_invalid_arg;
85 }
86 auto getter = it->second;
87 if (getter(env, in, out) != napi_ok) {
88 SLOGE("get property %{public}s failed", name.c_str());
89 return napi_generic_failure;
90 }
91 }
92
93 return napi_ok;
94 }
95
SetValue(napi_env env,const AVCallState & in,napi_value & out)96 napi_status NapiAVCallState::SetValue(napi_env env, const AVCallState& in, napi_value& out)
97 {
98 napi_status status = napi_create_object(env, &out);
99 CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
100
101 auto mask = in.GetMask();
102 for (int32_t i = 0; i < AVCallState::AVCALL_STATE_KEY_MAX; ++i) {
103 if (!mask.test(i)) {
104 continue;
105 }
106 auto setter = setterMap_[i];
107 if (setter(env, in, out) != napi_ok) {
108 SLOGE("set property %{public}d failed", i);
109 return napi_generic_failure;
110 }
111 }
112
113 return napi_ok;
114 }
115
GetAVCallState(napi_env env,napi_value in,AVCallState & out)116 napi_status NapiAVCallState::GetAVCallState(napi_env env, napi_value in, AVCallState& out)
117 {
118 int32_t property = AVCallState::AVCALL_STATE_IDLE;
119 auto status = NapiUtils::GetNamedProperty(env, in, "state", property);
120 CHECK_RETURN(status == napi_ok, "get property failed", status);
121 out.SetAVCallState(property);
122 return status;
123 }
124
SetAVCallState(napi_env env,const AVCallState & in,napi_value & out)125 napi_status NapiAVCallState::SetAVCallState(napi_env env, const AVCallState& in, napi_value& out)
126 {
127 napi_value property {};
128 auto status = NapiUtils::SetValue(env, in.GetAVCallState(), property);
129 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
130 status = napi_set_named_property(env, out, "state", property);
131 CHECK_RETURN(status == napi_ok, "set property failed", status);
132 return status;
133 }
134
IsAVCallMuted(napi_env env,napi_value in,AVCallState & out)135 napi_status NapiAVCallState::IsAVCallMuted(napi_env env, napi_value in, AVCallState& out)
136 {
137 bool property;
138 auto status = NapiUtils::GetNamedProperty(env, in, "muted", property);
139 CHECK_RETURN(status == napi_ok, "get property failed", status);
140 out.SetAVCallMuted(property);
141 return status;
142 }
143
SetAVCallMuted(napi_env env,const AVCallState & in,napi_value & out)144 napi_status NapiAVCallState::SetAVCallMuted(napi_env env, const AVCallState& in, napi_value& out)
145 {
146 napi_value property {};
147 auto status = NapiUtils::SetValue(env, in.IsAVCallMuted(), property);
148 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
149 status = napi_set_named_property(env, out, "muted", property);
150 CHECK_RETURN(status == napi_ok, "set property failed", status);
151 return status;
152 }
153 }
154