• 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_queue_item.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, NapiQueueItem::GetterType> NapiQueueItem::getterMap_ = {
24     { "itemId", GetItemId },
25     { "description", GetDescription },
26 };
27 
28 std::map<int32_t, NapiQueueItem::SetterType> NapiQueueItem::setterMap_ = {
29     { AVQueueItem::QUEUE_ITEM_KEY_ITEM_ID, SetItemId },
30     { AVQueueItem::QUEUE_ITEM_KEY_DESCRIPTION, SetDescription },
31 };
32 
GetValue(napi_env env,napi_value in,AVQueueItem & out)33 napi_status NapiQueueItem::GetValue(napi_env env, napi_value in, AVQueueItem& out)
34 {
35     std::vector<std::string> propertyNames;
36     auto status = NapiUtils::GetPropertyNames(env, in, propertyNames);
37     CHECK_RETURN(status == napi_ok, "get property name failed", status);
38 
39     for (const auto& name : propertyNames) {
40         auto it = getterMap_.find(name);
41         if (it == getterMap_.end()) {
42             SLOGE("property %{public}s is not of queueitem", name.c_str());
43             return napi_invalid_arg;
44         }
45         auto getter = it->second;
46         if (getter(env, in, out) != napi_ok) {
47             SLOGE("get property %{public}s failed", name.c_str());
48             return napi_generic_failure;
49         }
50     }
51     return napi_ok;
52 }
53 
SetValue(napi_env env,const AVQueueItem & in,napi_value & out)54 napi_status NapiQueueItem::SetValue(napi_env env, const AVQueueItem& in, napi_value& out)
55 {
56     napi_status status = napi_create_object(env, &out);
57     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
58 
59     for (int i = 0; i < AVQueueItem::QUEUE_ITEM_KEY_MAX; ++i) {
60         auto setter = setterMap_[i];
61         if (setter(env, in, out) != napi_ok) {
62             SLOGE("set property %{public}d failed", i);
63             return napi_generic_failure;
64         }
65     }
66     return napi_ok;
67 }
68 
GetItemId(napi_env env,napi_value in,AVQueueItem & out)69 napi_status NapiQueueItem::GetItemId(napi_env env, napi_value in, AVQueueItem& out)
70 {
71     int32_t property;
72     auto status = NapiUtils::GetNamedProperty(env, in, "itemId", property);
73     CHECK_RETURN(status == napi_ok, "get property failed", status);
74     out.SetItemId(property);
75     return status;
76 }
77 
SetItemId(napi_env env,const AVQueueItem & in,napi_value & out)78 napi_status NapiQueueItem::SetItemId(napi_env env, const AVQueueItem& in, napi_value& out)
79 {
80     napi_value property {};
81     auto status = NapiUtils::SetValue(env, in.GetItemId(), property);
82     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
83     status = napi_set_named_property(env, out, "itemId", property);
84     CHECK_RETURN(status == napi_ok, "set property failed", status);
85     return status;
86 }
87 
GetDescription(napi_env env,napi_value in,AVQueueItem & out)88 napi_status NapiQueueItem::GetDescription(napi_env env, napi_value in, AVQueueItem& out)
89 {
90     AVMediaDescription property {};
91     auto status = NapiUtils::GetNamedProperty(env, in, "description", property);
92     CHECK_RETURN(status == napi_ok, "get property failed", status);
93     out.SetDescription(std::make_shared<AVMediaDescription>(property));
94     return status;
95 }
96 
SetDescription(napi_env env,const AVQueueItem & in,napi_value & out)97 napi_status NapiQueueItem::SetDescription(napi_env env, const AVQueueItem& in, napi_value& out)
98 {
99     napi_value property {};
100     auto status = NapiUtils::SetValue(env, *(in.GetDescription()), property);
101     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
102     status = napi_set_named_property(env, out, "description", property);
103     CHECK_RETURN(status == napi_ok, "set property failed", status);
104     return status;
105 }
106 }
107