• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "napi_avcall_meta_data.h"
17 #include "avsession_log.h"
18 #include "avsession_pixel_map_adapter.h"
19 #include "napi_utils.h"
20 #include "pixel_map_napi.h"
21 
22 namespace OHOS::AVSession {
23 std::map<std::string, NapiAVCallMetaData::GetterType> NapiAVCallMetaData::getterMap_ = {
24     {"name", GetName},
25     {"phoneNumber", GetPhoneNumber},
26     {"avatar", GetMediaImage}
27 };
28 
29 std::map<int32_t, NapiAVCallMetaData::SetterType> NapiAVCallMetaData::setterMap_ = {
30     {AVCallMetaData::AVCALL_META_KEY_NAME, SetName},
31     {AVCallMetaData::AVCALL_META_KEY_PHONE_NUMBER, SetPhoneNumber},
32     {AVCallMetaData::AVCALL_META_KEY_MEDIA_IMAGE, SetMediaImage},
33     {AVCallMetaData::AVCALL_META_KEY_MEDIA_IMAGE_URI, SetMediaImageUri}
34 };
35 
36 std::pair<std::string, int32_t> NapiAVCallMetaData::filterMap_[] = {
37     {"name", AVCallMetaData::AVCALL_META_KEY_NAME},
38     {"phoneNumber", AVCallMetaData::AVCALL_META_KEY_PHONE_NUMBER},
39     {"avatar", AVCallMetaData::AVCALL_META_KEY_MEDIA_IMAGE},
40     {"avatar", AVCallMetaData::AVCALL_META_KEY_MEDIA_IMAGE_URI},
41 };
42 
ConvertFilter(napi_env env,napi_value filter,AVCallMetaData::AVCallMetaMaskType & mask)43 napi_status NapiAVCallMetaData::ConvertFilter(napi_env env, napi_value filter, AVCallMetaData::AVCallMetaMaskType& mask)
44 {
45     napi_valuetype type = napi_undefined;
46     auto status = napi_typeof(env, filter, &type);
47     CHECK_RETURN(status == napi_ok, "napi_typeof failed", status);
48 
49     if (type == napi_string) {
50         std::string stringFilter;
51         status = NapiUtils::GetValue(env, filter, stringFilter);
52         CHECK_RETURN(status == napi_ok, "get string filter failed", status);
53         if (stringFilter != "all") {
54             SLOGE("string filter only support all") ;
55             return napi_invalid_arg;
56         }
57         mask.set();
58         return napi_ok;
59     }
60 
61     uint32_t count = 0;
62     status = napi_get_array_length(env, filter, &count);
63     CHECK_RETURN(status == napi_ok, "get array length failed", status);
64     for (uint32_t i = 0; i < count; i++) {
65         napi_value value {};
66         status = napi_get_element(env, filter, i, &value);
67         CHECK_RETURN(status == napi_ok, "get element failed", status);
68         std::string metaKey;
69         status = NapiUtils::GetValue(env, value, metaKey);
70         CHECK_RETURN(status == napi_ok, "get string value failed", status);
71         for (const auto& pair : filterMap_) {
72             if (pair.first == metaKey) {
73                 mask.set(pair.second);
74             }
75         }
76     }
77 
78     CHECK_RETURN(!mask.none(), "array element invalid.", napi_invalid_arg);
79     return napi_ok;
80 }
81 
GetValue(napi_env env,napi_value in,AVCallMetaData & out)82 napi_status NapiAVCallMetaData::GetValue(napi_env env, napi_value in, AVCallMetaData& out)
83 {
84     std::vector<std::string> propertyNames;
85     auto status = NapiUtils::GetPropertyNames(env, in, propertyNames);
86     CHECK_RETURN(status == napi_ok, "get property name failed", status);
87 
88     for (const auto& name : propertyNames) {
89         auto it = getterMap_.find(name);
90         if (it == getterMap_.end()) {
91             SLOGE("property %{public}s is not of metadata", name.c_str());
92             return napi_invalid_arg;
93         }
94         auto getter = it->second;
95         if (getter(env, in, out) != napi_ok) {
96             SLOGE("get property %{public}s failed", name.c_str());
97         }
98     }
99 
100     return napi_ok;
101 }
102 
SetValue(napi_env env,const AVCallMetaData & in,napi_value & out)103 napi_status NapiAVCallMetaData::SetValue(napi_env env, const AVCallMetaData& in, napi_value& out)
104 {
105     napi_status status = napi_create_object(env, &out);
106     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
107 
108     auto mask = in.GetAVCallMetaMask();
109     if (mask.none()) {
110         SLOGI("undefined meta");
111         return SetUndefinedMeta(env, out);
112     }
113 
114     for (int i = 0; i < AVCallMetaData::AVCALL_META_KEY_MAX; ++i) {
115         if (!mask.test(i)) {
116             continue;
117         }
118         auto setter = setterMap_[i];
119         if (setter(env, in, out) != napi_ok) {
120             SLOGE("set property %{public}d failed", i);
121             return napi_generic_failure;
122         }
123     }
124 
125     return napi_ok;
126 }
127 
SetUndefinedMeta(napi_env env,napi_value & meta)128 napi_status NapiAVCallMetaData::SetUndefinedMeta(napi_env env, napi_value& meta)
129 {
130     auto status = napi_set_named_property(env, meta, "name", NapiUtils::GetUndefinedValue(env));
131     CHECK_RETURN(status == napi_ok, "set name to undefined failed", status);
132     return napi_ok;
133 }
134 
GetName(napi_env env,napi_value in,AVCallMetaData & out)135 napi_status NapiAVCallMetaData::GetName(napi_env env, napi_value in, AVCallMetaData& out)
136 {
137     std::string property;
138     auto status = NapiUtils::GetNamedProperty(env, in, "name", property);
139     CHECK_RETURN(status == napi_ok, "get property failed", status);
140     out.SetName(property);
141     return status;
142 }
143 
SetName(napi_env env,const AVCallMetaData & in,napi_value & out)144 napi_status NapiAVCallMetaData::SetName(napi_env env, const AVCallMetaData& in, napi_value& out)
145 {
146     napi_value property {};
147     auto status = NapiUtils::SetValue(env, in.GetName(), property);
148     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
149     status = napi_set_named_property(env, out, "name", property);
150     CHECK_RETURN(status == napi_ok, "set property failed", status);
151     return status;
152 }
153 
GetPhoneNumber(napi_env env,napi_value in,AVCallMetaData & out)154 napi_status NapiAVCallMetaData::GetPhoneNumber(napi_env env, napi_value in, AVCallMetaData& out)
155 {
156     std::string property;
157     auto status = NapiUtils::GetNamedProperty(env, in, "phoneNumber", property);
158     CHECK_RETURN(status == napi_ok, "get property failed", status);
159     out.SetPhoneNumber(property);
160     return status;
161 }
162 
SetPhoneNumber(napi_env env,const AVCallMetaData & in,napi_value & out)163 napi_status NapiAVCallMetaData::SetPhoneNumber(napi_env env, const AVCallMetaData& in, napi_value& out)
164 {
165     napi_value property {};
166     auto status = NapiUtils::SetValue(env, in.GetPhoneNumber(), property);
167     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
168     status = napi_set_named_property(env, out, "phoneNumber", property);
169     CHECK_RETURN(status == napi_ok, "set property failed", status);
170     return status;
171 }
172 
GetMediaImage(napi_env env,napi_value in,AVCallMetaData & out)173 napi_status NapiAVCallMetaData::GetMediaImage(napi_env env, napi_value in, AVCallMetaData& out)
174 {
175     napi_value property {};
176     auto status = napi_get_named_property(env, in, "avatar", &property);
177     CHECK_RETURN((status == napi_ok) && (property != nullptr), "get property failed", status);
178     napi_valuetype type = napi_undefined;
179     status = napi_typeof(env, property, &type);
180     CHECK_RETURN(status == napi_ok, "get napi_value type failed", status);
181     if (type == napi_string) {
182         std::string uri;
183         status = NapiUtils::GetValue(env, property, uri);
184         CHECK_RETURN(status == napi_ok, "get property failed", status);
185         out.SetMediaImageUri(uri);
186     } else if (type == napi_object) {
187         auto pixelMap = Media::PixelMapNapi::GetPixelMap(env, property);
188         if (pixelMap == nullptr) {
189             SLOGE("unwrap failed");
190             return napi_invalid_arg;
191         }
192         out.SetMediaImage(AVSessionPixelMapAdapter::ConvertToInner(pixelMap));
193     } else {
194         SLOGE("avatar property value type invalid");
195         return napi_invalid_arg;
196     }
197 
198     return status;
199 }
200 
SetMediaImage(napi_env env,const AVCallMetaData & in,napi_value & out)201 napi_status NapiAVCallMetaData::SetMediaImage(napi_env env, const AVCallMetaData& in, napi_value& out)
202 {
203     auto pixelMap = in.GetMediaImage();
204     if (pixelMap == nullptr) {
205         SLOGI("media image is none");
206         return napi_ok;
207     }
208 
209     napi_value property = Media::PixelMapNapi::CreatePixelMap(env,
210         AVSessionPixelMapAdapter::ConvertFromInner(pixelMap));
211     auto status = napi_set_named_property(env, out, "avatar", property);
212     CHECK_RETURN(status == napi_ok, "set property failed", status);
213     return status;
214 }
215 
SetMediaImageUri(napi_env env,const AVCallMetaData & in,napi_value & out)216 napi_status NapiAVCallMetaData::SetMediaImageUri(napi_env env, const AVCallMetaData& in, napi_value& out)
217 {
218     auto uri = in.GetMediaImageUri();
219     if (uri.empty()) {
220         SLOGI("media image uri empty");
221         return napi_ok;
222     }
223 
224     napi_value property {};
225     auto status = NapiUtils::SetValue(env, uri, property);
226     CHECK_RETURN(status == napi_ok, "create property failed", status);
227     status = napi_set_named_property(env, out, "avtar", property);
228     CHECK_RETURN(status == napi_ok, "set property failed", status);
229     return status;
230 }
231 }
232