1 /*
2 * Copyright (C) 2023-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 * Description: supply napi nums realization for interfaces.
15 * Author: zhangjingnan
16 * Create: 2023-4-11
17 */
18
19 #include <string>
20
21 #include "napi/native_common.h"
22 #include "cast_engine_common.h"
23 #include "napi_castengine_enum.h"
24 #include "cast_engine_log.h"
25
26 namespace OHOS {
27 namespace CastEngine {
28 namespace CastEngineClient {
29 DEFINE_CAST_ENGINE_LABEL("Cast-Napi-Enum");
30
SetNamedProperty(napi_env env,napi_value & obj,const std::string & name,int32_t value)31 static napi_status SetNamedProperty(napi_env env, napi_value &obj, const std::string &name, int32_t value)
32 {
33 napi_value property = nullptr;
34 napi_status status = napi_create_int32(env, value, &property);
35 if (status != napi_ok) {
36 CLOGE("napi_create_int32 failed");
37 return status;
38 }
39 status = napi_set_named_property(env, obj, name.c_str(), property);
40 if (status != napi_ok) {
41 CLOGD("napi_set_named_property failed");
42 return status;
43 }
44 return status;
45 }
46
ExportPlayerStates(napi_env env)47 static napi_value ExportPlayerStates(napi_env env)
48 {
49 napi_value result = nullptr;
50 NAPI_CALL(env, napi_create_object(env, &result));
51
52 (void)SetNamedProperty(env, result, "PLAYER_STATE_ERROR", static_cast<int32_t>(PlayerStates::PLAYER_STATE_ERROR));
53 (void)SetNamedProperty(env, result, "PLAYER_IDLE", static_cast<int32_t>(PlayerStates::PLAYER_IDLE));
54 (void)SetNamedProperty(env, result, "PLAYER_INITIALIZED", static_cast<int32_t>(PlayerStates::PLAYER_INITIALIZED));
55 (void)SetNamedProperty(env, result, "PLAYER_PREPARING", static_cast<int32_t>(PlayerStates::PLAYER_PREPARING));
56 (void)SetNamedProperty(env, result, "PLAYER_PREPARED", static_cast<int32_t>(PlayerStates::PLAYER_PREPARED));
57 (void)SetNamedProperty(env, result, "PLAYER_STARTED", static_cast<int32_t>(PlayerStates::PLAYER_STARTED));
58 (void)SetNamedProperty(env, result, "PLAYER_PAUSED", static_cast<int32_t>(PlayerStates::PLAYER_PAUSED));
59 (void)SetNamedProperty(env, result, "PLAYER_STOPPED", static_cast<int32_t>(PlayerStates::PLAYER_STOPPED));
60 (void)SetNamedProperty(env, result, "PLAYER_PLAYBACK_COMPLETE",
61 static_cast<int32_t>(PlayerStates::PLAYER_PLAYBACK_COMPLETE));
62 (void)SetNamedProperty(env, result, "PLAYER_RELEASED", static_cast<int32_t>(PlayerStates::PLAYER_RELEASED));
63 (void)SetNamedProperty(env, result, "PLAYER_BUFFERING", static_cast<int32_t>(PlayerStates::PLAYER_BUFFERING));
64
65 NAPI_CALL(env, napi_object_freeze(env, result));
66 return result;
67 }
68
ExportPlaybackSpeed(napi_env env)69 static napi_value ExportPlaybackSpeed(napi_env env)
70 {
71 napi_value result = nullptr;
72 NAPI_CALL(env, napi_create_object(env, &result));
73
74 (void)SetNamedProperty(env, result, "SPEED_FORWARD_0_75_X",
75 static_cast<int32_t>(PlaybackSpeed::SPEED_FORWARD_0_75_X));
76 (void)SetNamedProperty(env, result, "SPEED_FORWARD_1_00_X",
77 static_cast<int32_t>(PlaybackSpeed::SPEED_FORWARD_1_00_X));
78 (void)SetNamedProperty(env, result, "SPEED_FORWARD_1_25_X",
79 static_cast<int32_t>(PlaybackSpeed::SPEED_FORWARD_1_25_X));
80 (void)SetNamedProperty(env, result, "SPEED_FORWARD_1_75_X",
81 static_cast<int32_t>(PlaybackSpeed::SPEED_FORWARD_1_75_X));
82 (void)SetNamedProperty(env, result, "SPEED_FORWARD_2_00_X",
83 static_cast<int32_t>(PlaybackSpeed::SPEED_FORWARD_2_00_X));
84
85 NAPI_CALL(env, napi_object_freeze(env, result));
86 return result;
87 }
88
ExportLoopMode(napi_env env)89 static napi_value ExportLoopMode(napi_env env)
90 {
91 napi_value result = nullptr;
92 NAPI_CALL(env, napi_create_object(env, &result));
93
94 (void)SetNamedProperty(env, result, "LOOP_MODE_SEQUENCE", static_cast<int32_t>(LoopMode::LOOP_MODE_SEQUENCE));
95 (void)SetNamedProperty(env, result, "LOOP_MODE_SINGLE", static_cast<int32_t>(LoopMode::LOOP_MODE_SINGLE));
96 (void)SetNamedProperty(env, result, "LOOP_MODE_LIST", static_cast<int32_t>(LoopMode::LOOP_MODE_LIST));
97 (void)SetNamedProperty(env, result, "LOOP_MODE_SHUFFLE", static_cast<int32_t>(LoopMode::LOOP_MODE_SHUFFLE));
98
99 NAPI_CALL(env, napi_object_freeze(env, result));
100 return result;
101 }
102
ExportDeviceType(napi_env env)103 static napi_value ExportDeviceType(napi_env env)
104 {
105 napi_value result = nullptr;
106 NAPI_CALL(env, napi_create_object(env, &result));
107
108 (void)SetNamedProperty(env, result, "DEVICE_OTHERS", static_cast<int32_t>(DeviceType::DEVICE_OTHERS));
109 (void)SetNamedProperty(env, result, "DEVICE_SCREEN_PLAYER", static_cast<int32_t>(DeviceType::DEVICE_SCREEN_PLAYER));
110 (void)SetNamedProperty(env, result, "DEVICE_HW_TV", static_cast<int32_t>(DeviceType::DEVICE_HW_TV));
111 (void)SetNamedProperty(env, result, "DEVICE_SOUND_BOX", static_cast<int32_t>(DeviceType::DEVICE_SOUND_BOX));
112 (void)SetNamedProperty(env, result, "DEVICE_HICAR", static_cast<int32_t>(DeviceType::DEVICE_HICAR));
113 (void)SetNamedProperty(env, result, "DEVICE_MATEBOOK", static_cast<int32_t>(DeviceType::DEVICE_MATEBOOK));
114 (void)SetNamedProperty(env, result, "DEVICE_PAD", static_cast<int32_t>(DeviceType::DEVICE_PAD));
115 (void)SetNamedProperty(env, result, "DEVICE_CAST_PLUS", static_cast<int32_t>(DeviceType::DEVICE_CAST_PLUS));
116 (void)SetNamedProperty(env, result, "DEVICE_SMART_SCREEN_UNF",
117 static_cast<int32_t>(DeviceType::DEVICE_SMART_SCREEN_UNF));
118 (void)SetNamedProperty(env, result, "DEVICE_PAD_IN_CAR",
119 static_cast<int32_t>(DeviceType::DEVICE_PAD_IN_CAR));
120 (void)SetNamedProperty(env, result, "DEVICE_SUPER_LAUNCHER",
121 static_cast<int32_t>(DeviceType::DEVICE_SUPER_LAUNCHER));
122 (void)SetNamedProperty(env, result, "DEVICE_CAR_MULTI_SCREEN_PLAY",
123 static_cast<int32_t>(DeviceType::DEVICE_CAR_MULTI_SCREEN_PLAY));
124 NAPI_CALL(env, napi_object_freeze(env, result));
125 return result;
126 }
127
ExportSubDeviceType(napi_env env)128 static napi_value ExportSubDeviceType(napi_env env)
129 {
130 napi_value result = nullptr;
131 NAPI_CALL(env, napi_create_object(env, &result));
132
133 (void)SetNamedProperty(env, result, "SUB_DEVICE_DEFAULT", static_cast<int32_t>(SubDeviceType::SUB_DEVICE_DEFAULT));
134 (void)SetNamedProperty(env, result, "SUB_DEVICE_MATEBOOK_PAD",
135 static_cast<int32_t>(SubDeviceType::SUB_DEVICE_MATEBOOK_PAD));
136 (void)SetNamedProperty(env, result, "SUB_DEVICE_CAST_PLUS_WHITEBOARD",
137 static_cast<int32_t>(SubDeviceType::SUB_DEVICE_CAST_PLUS_WHITEBOARD));
138
139 NAPI_CALL(env, napi_object_freeze(env, result));
140 return result;
141 }
142
ExportTriggerType(napi_env env)143 static napi_value ExportTriggerType(napi_env env)
144 {
145 napi_value result = nullptr;
146 NAPI_CALL(env, napi_create_object(env, &result));
147
148 (void)SetNamedProperty(env, result, "UNSPEC_TAG", static_cast<int32_t>(TriggerType::UNSPEC_TAG));
149 (void)SetNamedProperty(env, result, "PASSIVE_MATCH_TAG", static_cast<int32_t>(TriggerType::PASSIVE_MATCH_TAG));
150 (void)SetNamedProperty(env, result, "ACTIVE_MATCH_TAG", static_cast<int32_t>(TriggerType::ACTIVE_MATCH_TAG));
151 (void)SetNamedProperty(env, result, "PASSIVE_BIND_TAG", static_cast<int32_t>(TriggerType::PASSIVE_BIND_TAG));
152
153 NAPI_CALL(env, napi_object_freeze(env, result));
154 return result;
155 }
156
ExportDeviceState(napi_env env)157 static napi_value ExportDeviceState(napi_env env)
158 {
159 napi_value result = nullptr;
160 NAPI_CALL(env, napi_create_object(env, &result));
161
162 (void)SetNamedProperty(env, result, "CONNECTING", static_cast<int32_t>(DeviceState::CONNECTING));
163 (void)SetNamedProperty(env, result, "CONNECTED", static_cast<int32_t>(DeviceState::CONNECTED));
164 (void)SetNamedProperty(env, result, "PAUSED", static_cast<int32_t>(DeviceState::PAUSED));
165 (void)SetNamedProperty(env, result, "PLAYING", static_cast<int32_t>(DeviceState::PLAYING));
166 (void)SetNamedProperty(env, result, "DISCONNECTING", static_cast<int32_t>(DeviceState::DISCONNECTING));
167 (void)SetNamedProperty(env, result, "DISCONNECTED", static_cast<int32_t>(DeviceState::DISCONNECTED));
168 (void)SetNamedProperty(env, result, "STREAM", static_cast<int32_t>(DeviceState::STREAM));
169 (void)SetNamedProperty(env, result, "AUTHING", static_cast<int32_t>(DeviceState::AUTHING));
170 (void)SetNamedProperty(env, result, "DEVICE_STATE_MAX", static_cast<int32_t>(DeviceState::DEVICE_STATE_MAX));
171
172 NAPI_CALL(env, napi_object_freeze(env, result));
173 return result;
174 }
175
ExportEventCode(napi_env env)176 static napi_value ExportEventCode(napi_env env)
177 {
178 napi_value result = nullptr;
179 NAPI_CALL(env, napi_create_object(env, &result));
180
181 (void)SetNamedProperty(env, result, "UNKNOWN_EVENT", static_cast<int32_t>(EventCode::UNKNOWN_EVENT));
182 (void)SetNamedProperty(env, result, "ERR_CONNECTION_FAILED", static_cast<int32_t>(
183 EventCode::ERR_CONNECTION_FAILED));
184 (void)SetNamedProperty(env, result, "ERR_PIN_CODE_RETRY_COUNT_EXCEEDED", static_cast<int32_t>(
185 EventCode::ERR_PIN_CODE_RETRY_COUNT_EXCEEDED));
186 (void)SetNamedProperty(env, result, "ERR_CANCEL_BY_SINK", static_cast<int32_t>(EventCode::ERR_CANCEL_BY_SINK));
187 (void)SetNamedProperty(env, result, "ERR_DISTRUST_BY_SINK", static_cast<int32_t>(EventCode::ERR_DISTRUST_BY_SINK));
188 (void)SetNamedProperty(env, result, "DEFAULT_EVENT", static_cast<int32_t>(EventCode::DEFAULT_EVENT));
189 (void)SetNamedProperty(env, result, "EVT_TRUST_BY_SINK", static_cast<int32_t>(EventCode::EVT_TRUST_BY_SINK));
190 (void)SetNamedProperty(env, result, "EVT_CANCEL_BY_SOURCE", static_cast<int32_t>(EventCode::EVT_CANCEL_BY_SOURCE));
191 (void)SetNamedProperty(env, result, "EVT_AUTHENTICATION_COMPLETED",
192 static_cast<int32_t>(EventCode::EVT_AUTHENTICATION_COMPLETED));
193 NAPI_CALL(env, napi_object_freeze(env, result));
194 return result;
195 }
196
ExportServiceStatus(napi_env env)197 static napi_value ExportServiceStatus(napi_env env)
198 {
199 napi_value result = nullptr;
200 NAPI_CALL(env, napi_create_object(env, &result));
201
202 (void)SetNamedProperty(env, result, "DISCONNECTED", static_cast<int32_t>(ServiceStatus::DISCONNECTED));
203 (void)SetNamedProperty(env, result, "DISCONNECTING", static_cast<int32_t>(ServiceStatus::DISCONNECTING));
204 (void)SetNamedProperty(env, result, "CONNECTING", static_cast<int32_t>(ServiceStatus::CONNECTING));
205 (void)SetNamedProperty(env, result, "CONNECTED", static_cast<int32_t>(ServiceStatus::CONNECTED));
206
207 NAPI_CALL(env, napi_object_freeze(env, result));
208 return result;
209 }
210
ExportDeviceStatusState(napi_env env)211 static napi_value ExportDeviceStatusState(napi_env env)
212 {
213 napi_value result = nullptr;
214 NAPI_CALL(env, napi_create_object(env, &result));
215
216 (void)SetNamedProperty(env, result, "DEVICE_AVAILABLE", static_cast<int32_t>(DeviceStatusState::DEVICE_AVAILABLE));
217 (void)SetNamedProperty(env, result, "DEVICE_CONNECTED", static_cast<int32_t>(DeviceStatusState::DEVICE_CONNECTED));
218 (void)SetNamedProperty(env, result, "DEVICE_DISCONNECTED",
219 static_cast<int32_t>(DeviceStatusState::DEVICE_DISCONNECTED));
220 (void)SetNamedProperty(env, result, "DEVICE_CONNECT_REQ",
221 static_cast<int32_t>(DeviceStatusState::DEVICE_CONNECT_REQ));
222
223 NAPI_CALL(env, napi_object_freeze(env, result));
224 return result;
225 }
226
ExportPropertyType(napi_env env)227 static napi_value ExportPropertyType(napi_env env)
228 {
229 napi_value result = nullptr;
230 NAPI_CALL(env, napi_create_object(env, &result));
231
232 (void)SetNamedProperty(env, result, "VIDEO_SIZE", static_cast<int32_t>(PropertyType::VIDEO_SIZE));
233 (void)SetNamedProperty(env, result, "VIDEO_FPS", static_cast<int32_t>(PropertyType::VIDEO_FPS));
234 (void)SetNamedProperty(env, result, "WINDOW_SIZE", static_cast<int32_t>(PropertyType::WINDOW_SIZE));
235
236 NAPI_CALL(env, napi_object_freeze(env, result));
237 return result;
238 }
239
ExportChannelType(napi_env env)240 static napi_value ExportChannelType(napi_env env)
241 {
242 napi_value result = nullptr;
243 NAPI_CALL(env, napi_create_object(env, &result));
244
245 (void)SetNamedProperty(env, result, "SOFT_BUS", static_cast<int32_t>(ChannelType::SOFT_BUS));
246 (void)SetNamedProperty(env, result, "LEGACY_CHANNEL", static_cast<int32_t>(ChannelType::LEGACY_CHANNEL));
247
248 NAPI_CALL(env, napi_object_freeze(env, result));
249 return result;
250 }
251
ExportProtocolType(napi_env env)252 static napi_value ExportProtocolType(napi_env env)
253 {
254 napi_value result = nullptr;
255 NAPI_CALL(env, napi_create_object(env, &result));
256
257 (void)SetNamedProperty(env, result, "CAST_PLUS_MIRROR", static_cast<int32_t>(ProtocolType::CAST_PLUS_MIRROR));
258 (void)SetNamedProperty(env, result, "CAST_PLUS_STREAM", static_cast<int32_t>(ProtocolType::CAST_PLUS_STREAM));
259 (void)SetNamedProperty(env, result, "MIRACAST", static_cast<int32_t>(ProtocolType::MIRACAST));
260 (void)SetNamedProperty(env, result, "DLNA", static_cast<int32_t>(ProtocolType::DLNA));
261 (void)SetNamedProperty(env, result, "COOPERATION", static_cast<int32_t>(ProtocolType::COOPERATION));
262 (void)SetNamedProperty(env, result, "COOPERATION_LEGACY", static_cast<int32_t>(ProtocolType::COOPERATION_LEGACY));
263 (void)SetNamedProperty(env, result, "HICAR", static_cast<int32_t>(ProtocolType::HICAR));
264 (void)SetNamedProperty(env, result, "SUPER_LAUNCHER", static_cast<int32_t>(ProtocolType::SUPER_LAUNCHER));
265 (void)SetNamedProperty(env, result, "CAST_COOPERATION", static_cast<int32_t>(ProtocolType::CAST_COOPERATION));
266 NAPI_CALL(env, napi_object_freeze(env, result));
267 return result;
268 }
269
ExportEndType(napi_env env)270 static napi_value ExportEndType(napi_env env)
271 {
272 napi_value result = nullptr;
273 NAPI_CALL(env, napi_create_object(env, &result));
274
275 (void)SetNamedProperty(env, result, "CAST_SINK", static_cast<int32_t>(EndType::CAST_SINK));
276 (void)SetNamedProperty(env, result, "CAST_SOURCE", static_cast<int32_t>(EndType::CAST_SOURCE));
277
278 NAPI_CALL(env, napi_object_freeze(env, result));
279 return result;
280 }
281
ExportEventId(napi_env env)282 static napi_value ExportEventId(napi_env env)
283 {
284 napi_value result = nullptr;
285 NAPI_CALL(env, napi_create_object(env, &result));
286
287 (void)SetNamedProperty(env, result, "EVENT_BEGIN", static_cast<int32_t>(EventId::EVENT_BEGIN));
288 (void)SetNamedProperty(env, result, "EVENT_END", static_cast<int32_t>(EventId::EVENT_END));
289
290 NAPI_CALL(env, napi_object_freeze(env, result));
291 return result;
292 }
293
ExportColorStandard(napi_env env)294 static napi_value ExportColorStandard(napi_env env)
295 {
296 napi_value result = nullptr;
297 NAPI_CALL(env, napi_create_object(env, &result));
298
299 (void)SetNamedProperty(env, result, "BT709", static_cast<int32_t>(ColorStandard::BT709));
300 (void)SetNamedProperty(env, result, "BT601_PAL", static_cast<int32_t>(ColorStandard::BT601_PAL));
301 (void)SetNamedProperty(env, result, "BT601_NTSC", static_cast<int32_t>(ColorStandard::BT601_NTSC));
302 (void)SetNamedProperty(env, result, "BT2020", static_cast<int32_t>(ColorStandard::BT2020));
303
304 NAPI_CALL(env, napi_object_freeze(env, result));
305 return result;
306 }
307
ExportVideoCodecType(napi_env env)308 static napi_value ExportVideoCodecType(napi_env env)
309 {
310 napi_value result = nullptr;
311 NAPI_CALL(env, napi_create_object(env, &result));
312
313 (void)SetNamedProperty(env, result, "H264", static_cast<int32_t>(VideoCodecType::H264));
314 (void)SetNamedProperty(env, result, "H265", static_cast<int32_t>(VideoCodecType::H265));
315
316 NAPI_CALL(env, napi_object_freeze(env, result));
317 return result;
318 }
319
ExportCastModeType(napi_env env)320 static napi_value ExportCastModeType(napi_env env)
321 {
322 napi_value result = nullptr;
323 NAPI_CALL(env, napi_create_object(env, &result));
324
325 (void)SetNamedProperty(env, result, "MIRROR_CAST", static_cast<int32_t>(CastMode::MIRROR_CAST));
326 (void)SetNamedProperty(env, result, "APP_CAST", static_cast<int32_t>(CastMode::APP_CAST));
327
328 NAPI_CALL(env, napi_object_freeze(env, result));
329 return result;
330 }
331
InitEnums(napi_env env,napi_value exports)332 napi_status InitEnums(napi_env env, napi_value exports)
333 {
334 const napi_property_descriptor properties[] = {
335 DECLARE_NAPI_PROPERTY("PlayerStates", ExportPlayerStates(env)),
336 DECLARE_NAPI_PROPERTY("PlaybackSpeed", ExportPlaybackSpeed(env)),
337 DECLARE_NAPI_PROPERTY("LoopMode", ExportLoopMode(env)),
338 DECLARE_NAPI_PROPERTY("DeviceType", ExportDeviceType(env)),
339 DECLARE_NAPI_PROPERTY("SubDeviceType", ExportSubDeviceType(env)),
340 DECLARE_NAPI_PROPERTY("TriggerType", ExportTriggerType(env)),
341 DECLARE_NAPI_PROPERTY("DeviceState", ExportDeviceState(env)),
342 DECLARE_NAPI_PROPERTY("EventCode", ExportEventCode(env)),
343 DECLARE_NAPI_PROPERTY("ServiceStatus", ExportServiceStatus(env)),
344 DECLARE_NAPI_PROPERTY("DeviceStatusState", ExportDeviceStatusState(env)),
345 DECLARE_NAPI_PROPERTY("PropertyType", ExportPropertyType(env)),
346 DECLARE_NAPI_PROPERTY("ChannelType", ExportChannelType(env)),
347 DECLARE_NAPI_PROPERTY("ProtocolType", ExportProtocolType(env)),
348 DECLARE_NAPI_PROPERTY("EndType", ExportEndType(env)),
349 DECLARE_NAPI_PROPERTY("EventId", ExportEventId(env)),
350 DECLARE_NAPI_PROPERTY("ColorStandard", ExportColorStandard(env)),
351 DECLARE_NAPI_PROPERTY("VideoCodecType", ExportVideoCodecType(env)),
352 DECLARE_NAPI_PROPERTY("CastMode", ExportCastModeType(env))
353 };
354
355 size_t count = sizeof(properties) / sizeof(napi_property_descriptor);
356 return napi_define_properties(env, exports, count, properties);
357 }
358 } // namespace CastEngineClient
359 } // namespace CastEngine
360 } // namespace OHOS
361