• 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 "napi/native_api.h"
18 #include "napi/native_common.h"
19 #include "napi/native_node_api.h"
20 #include "avsession_log.h"
21 #include "avsession_pixel_map_adapter.h"
22 #include "napi_utils.h"
23 #include "pixel_map_napi.h"
24 
25 namespace OHOS::AVSession {
26 std::map<std::string, NapiQueueItem::GetterType> NapiQueueItem::getterMap_ = {
27     { "itemId", GetItemId },
28     { "description", GetDescription },
29 };
30 
31 std::map<int32_t, NapiQueueItem::SetterType> NapiQueueItem::setterMap_ = {
32     { AVQueueItem::QUEUE_ITEM_KEY_ITEM_ID, SetItemId },
33     { AVQueueItem::QUEUE_ITEM_KEY_DESCRIPTION, SetDescription },
34 };
35 
GetValue(napi_env env,napi_value in,AVQueueItem & out)36 napi_status NapiQueueItem::GetValue(napi_env env, napi_value in, AVQueueItem& out)
37 {
38     std::vector<std::string> propertyNames;
39     auto status = NapiUtils::GetPropertyNames(env, in, propertyNames);
40     CHECK_RETURN(status == napi_ok, "get property name failed", status);
41 
42     for (const auto& name : propertyNames) {
43         auto it = getterMap_.find(name);
44         if (it == getterMap_.end()) {
45             SLOGE("property %{public}s is not of queueitem", name.c_str());
46             return napi_invalid_arg;
47         }
48         auto getter = it->second;
49         if (getter(env, in, out) != napi_ok) {
50             SLOGE("get property %{public}s failed", name.c_str());
51             return napi_generic_failure;
52         }
53     }
54     return napi_ok;
55 }
56 
SetValue(napi_env env,const AVQueueItem & in,napi_value & out)57 napi_status NapiQueueItem::SetValue(napi_env env, const AVQueueItem& in, napi_value& out)
58 {
59     napi_status status = napi_create_object(env, &out);
60     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
61 
62     for (int i = 0; i < AVQueueItem::QUEUE_ITEM_KEY_MAX; ++i) {
63         auto setter = setterMap_[i];
64         if (setter(env, in, out) != napi_ok) {
65             SLOGE("set property %{public}d failed", i);
66             return napi_generic_failure;
67         }
68     }
69     return napi_ok;
70 }
71 
GetItemId(napi_env env,napi_value in,AVQueueItem & out)72 napi_status NapiQueueItem::GetItemId(napi_env env, napi_value in, AVQueueItem& out)
73 {
74     int32_t property;
75     auto status = NapiUtils::GetNamedProperty(env, in, "itemId", property);
76     CHECK_RETURN(status == napi_ok, "get property failed", status);
77     out.SetItemId(property);
78     return status;
79 }
80 
SetItemId(napi_env env,const AVQueueItem & in,napi_value & out)81 napi_status NapiQueueItem::SetItemId(napi_env env, const AVQueueItem& in, napi_value& out)
82 {
83     napi_value property {};
84     auto status = NapiUtils::SetValue(env, in.GetItemId(), property);
85     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
86     status = napi_set_named_property(env, out, "itemId", property);
87     CHECK_RETURN(status == napi_ok, "set property failed", status);
88     return status;
89 }
90 
GetDescription(napi_env env,napi_value in,AVQueueItem & out)91 napi_status NapiQueueItem::GetDescription(napi_env env, napi_value in, AVQueueItem& out)
92 {
93     AVMediaDescription property {};
94     auto status = NapiUtils::GetNamedProperty(env, in, "description", property);
95     CHECK_RETURN(status == napi_ok, "get property failed", status);
96     out.SetDescription(std::make_shared<AVMediaDescription>(property));
97     return status;
98 }
99 
SetDescription(napi_env env,const AVQueueItem & in,napi_value & out)100 napi_status NapiQueueItem::SetDescription(napi_env env, const AVQueueItem& in, napi_value& out)
101 {
102     napi_value property {};
103     auto status = NapiUtils::SetValue(env, *(in.GetDescription()), property);
104     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
105     status = napi_set_named_property(env, out, "description", property);
106     CHECK_RETURN(status == napi_ok, "set property failed", status);
107     return status;
108 }
109 
GetDataSrc(napi_env env,const napi_value in,napi_value * fileSize,napi_value * callback)110 napi_status NapiQueueItem::GetDataSrc(napi_env env, const napi_value in, napi_value* fileSize, napi_value* callback)
111 {
112     bool hasDescription = false;
113     napi_status status = napi_has_named_property(env, in, "description", &hasDescription);
114     CHECK_RETURN((status == napi_ok) && hasDescription, "has description failed", status);
115     napi_value descriptor = nullptr;
116     status = napi_get_named_property(env, in, "description", &descriptor);
117     CHECK_RETURN((status == napi_ok) && (descriptor != nullptr), "get description failed", status);
118 
119     bool hasDataSrc = false;
120     status = napi_has_named_property(env, descriptor, "dataSrc", &hasDataSrc);
121     CHECK_RETURN((status == napi_ok) && hasDataSrc, "has dataSrc failed", status);
122     napi_value dataSrc = nullptr;
123     status = napi_get_named_property(env, descriptor, "dataSrc", &dataSrc);
124     CHECK_RETURN((status == napi_ok) && (dataSrc != nullptr), "get dataSrc failed", status);
125 
126     bool hasFileSize = false;
127     status = napi_has_named_property(env, dataSrc, "fileSize", &hasFileSize);
128     CHECK_RETURN((status == napi_ok) && hasFileSize, "no file size", status);
129     status = napi_get_named_property(env, dataSrc, "fileSize", fileSize);
130     CHECK_RETURN(status == napi_ok, "get fileSize failed", status);
131 
132     int64_t fSize;
133     napi_get_value_int64(env, *fileSize, &fSize);
134     CHECK_RETURN(status == napi_ok, "get fileSize value failed", status);
135 
136     bool hasCallback = false;
137     status = napi_has_named_property(env, dataSrc, "callback", &hasCallback);
138     CHECK_RETURN((status == napi_ok) && hasCallback, "no callback", status);
139 
140     status = napi_get_named_property(env, dataSrc, "callback", callback);
141     CHECK_RETURN(status == napi_ok, "get callback failed", status);
142     return napi_ok;
143 }
144 }