• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 || property == nullptr) {
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         CLOGE("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     (void)SetNamedProperty(env, result, "DEVICE_MIRACAST",
125                            static_cast<int32_t>(DeviceType::DEVICE_MIRACAST));
126 
127     NAPI_CALL(env, napi_object_freeze(env, result));
128     return result;
129 }
130 
ExportSubDeviceType(napi_env env)131 static napi_value ExportSubDeviceType(napi_env env)
132 {
133     napi_value result = nullptr;
134     NAPI_CALL(env, napi_create_object(env, &result));
135 
136     (void)SetNamedProperty(env, result, "SUB_DEVICE_DEFAULT", static_cast<int32_t>(SubDeviceType::SUB_DEVICE_DEFAULT));
137     (void)SetNamedProperty(env, result, "SUB_DEVICE_MATEBOOK_PAD",
138         static_cast<int32_t>(SubDeviceType::SUB_DEVICE_MATEBOOK_PAD));
139     (void)SetNamedProperty(env, result, "SUB_DEVICE_CAST_PLUS_WHITEBOARD",
140         static_cast<int32_t>(SubDeviceType::SUB_DEVICE_CAST_PLUS_WHITEBOARD));
141 
142     NAPI_CALL(env, napi_object_freeze(env, result));
143     return result;
144 }
145 
ExportTriggerType(napi_env env)146 static napi_value ExportTriggerType(napi_env env)
147 {
148     napi_value result = nullptr;
149     NAPI_CALL(env, napi_create_object(env, &result));
150 
151     (void)SetNamedProperty(env, result, "UNSPEC_TAG", static_cast<int32_t>(TriggerType::UNSPEC_TAG));
152     (void)SetNamedProperty(env, result, "PASSIVE_MATCH_TAG", static_cast<int32_t>(TriggerType::PASSIVE_MATCH_TAG));
153     (void)SetNamedProperty(env, result, "ACTIVE_MATCH_TAG", static_cast<int32_t>(TriggerType::ACTIVE_MATCH_TAG));
154     (void)SetNamedProperty(env, result, "PASSIVE_BIND_TAG", static_cast<int32_t>(TriggerType::PASSIVE_BIND_TAG));
155 
156     NAPI_CALL(env, napi_object_freeze(env, result));
157     return result;
158 }
159 
ExportDeviceState(napi_env env)160 static napi_value ExportDeviceState(napi_env env)
161 {
162     napi_value result = nullptr;
163     NAPI_CALL(env, napi_create_object(env, &result));
164 
165     (void)SetNamedProperty(env, result, "CONNECTING", static_cast<int32_t>(DeviceState::CONNECTING));
166     (void)SetNamedProperty(env, result, "CONNECTED", static_cast<int32_t>(DeviceState::CONNECTED));
167     (void)SetNamedProperty(env, result, "PAUSED", static_cast<int32_t>(DeviceState::PAUSED));
168     (void)SetNamedProperty(env, result, "PLAYING", static_cast<int32_t>(DeviceState::PLAYING));
169     (void)SetNamedProperty(env, result, "DISCONNECTING", static_cast<int32_t>(DeviceState::DISCONNECTING));
170     (void)SetNamedProperty(env, result, "DISCONNECTED", static_cast<int32_t>(DeviceState::DISCONNECTED));
171     (void)SetNamedProperty(env, result, "STREAM", static_cast<int32_t>(DeviceState::STREAM));
172     (void)SetNamedProperty(env, result, "MIRROR_TO_UI", static_cast<int32_t>(DeviceState::MIRROR_TO_UI));
173     (void)SetNamedProperty(env, result, "UI_TO_MIRROR", static_cast<int32_t>(DeviceState::UI_TO_MIRROR));
174     (void)SetNamedProperty(env, result, "UICAST", static_cast<int32_t>(DeviceState::UICAST));
175     (void)SetNamedProperty(env, result, "AUTHING", static_cast<int32_t>(DeviceState::AUTHING));
176     (void)SetNamedProperty(env, result, "DEVICE_STATE_MAX", static_cast<int32_t>(DeviceState::DEVICE_STATE_MAX));
177 
178     NAPI_CALL(env, napi_object_freeze(env, result));
179     return result;
180 }
181 
ExportEventCode(napi_env env)182 static napi_value ExportEventCode(napi_env env)
183 {
184     napi_value result = nullptr;
185     NAPI_CALL(env, napi_create_object(env, &result));
186 
187     (void)SetNamedProperty(env, result, "REASON_DEFAULT", static_cast<int32_t>(ReasonCode::REASON_DEFAULT));
188     (void)SetNamedProperty(env, result, "REASON_TRUST_BY_SINK", static_cast<int32_t>(ReasonCode::REASON_TRUST_BY_SINK));
189     (void)SetNamedProperty(env, result, "REASON_CANCEL_BY_SOURCE",
190                            static_cast<int32_t>(ReasonCode::REASON_CANCEL_BY_SOURCE));
191     (void)SetNamedProperty(env, result, "REASON_BIND_COMPLETED",
192                            static_cast<int32_t>(ReasonCode::REASON_BIND_COMPLETED));
193     (void)SetNamedProperty(env, result, "REASON_SHOW_TRUST_SELECT_UI",
194                            static_cast<int32_t>(ReasonCode::REASON_SHOW_TRUST_SELECT_UI));
195     (void)SetNamedProperty(env, result, "REASON_STOP_BIND_BY_SOURCE",
196                            static_cast<int32_t>(ReasonCode::REASON_STOP_BIND_BY_SOURCE));
197     (void)SetNamedProperty(env, result, "REASON_PIN_CODE_OVER_RETRY",
198                            static_cast<int32_t>(ReasonCode::REASON_PIN_CODE_OVER_RETRY));
199     (void)SetNamedProperty(env, result, "REASON_CANCEL_BY_SINK",
200                            static_cast<int32_t>(ReasonCode::REASON_CANCEL_BY_SINK));
201     (void)SetNamedProperty(env, result, "REASON_DISTRUST_BY_SINK",
202                            static_cast<int32_t>(ReasonCode::REASON_DISTRUST_BY_SINK));
203     (void)SetNamedProperty(env, result, "REASON_USER_TIMEOUT", static_cast<int32_t>(ReasonCode::REASON_USER_TIMEOUT));
204     (void)SetNamedProperty(env, result, "REASON_DEVICE_IS_BUSY",
205                            static_cast<int32_t>(ReasonCode::REASON_DEVICE_IS_BUSY));
206 
207     NAPI_CALL(env, napi_object_freeze(env, result));
208     return result;
209 }
210 
ExportServiceStatus(napi_env env)211 static napi_value ExportServiceStatus(napi_env env)
212 {
213     napi_value result = nullptr;
214     NAPI_CALL(env, napi_create_object(env, &result));
215 
216     (void)SetNamedProperty(env, result, "DISCONNECTED", static_cast<int32_t>(ServiceStatus::DISCONNECTED));
217     (void)SetNamedProperty(env, result, "DISCONNECTING", static_cast<int32_t>(ServiceStatus::DISCONNECTING));
218     (void)SetNamedProperty(env, result, "CONNECTING", static_cast<int32_t>(ServiceStatus::CONNECTING));
219     (void)SetNamedProperty(env, result, "CONNECTED", static_cast<int32_t>(ServiceStatus::CONNECTED));
220 
221     NAPI_CALL(env, napi_object_freeze(env, result));
222     return result;
223 }
224 
ExportDeviceStatusState(napi_env env)225 static napi_value ExportDeviceStatusState(napi_env env)
226 {
227     napi_value result = nullptr;
228     NAPI_CALL(env, napi_create_object(env, &result));
229 
230     (void)SetNamedProperty(env, result, "DEVICE_AVAILABLE", static_cast<int32_t>(DeviceStatusState::DEVICE_AVAILABLE));
231     (void)SetNamedProperty(env, result, "DEVICE_CONNECTED", static_cast<int32_t>(DeviceStatusState::DEVICE_CONNECTED));
232     (void)SetNamedProperty(env, result, "DEVICE_DISCONNECTED",
233         static_cast<int32_t>(DeviceStatusState::DEVICE_DISCONNECTED));
234     (void)SetNamedProperty(env, result, "DEVICE_CONNECT_REQ",
235         static_cast<int32_t>(DeviceStatusState::DEVICE_CONNECT_REQ));
236 
237     NAPI_CALL(env, napi_object_freeze(env, result));
238     return result;
239 }
240 
ExportPropertyType(napi_env env)241 static napi_value ExportPropertyType(napi_env env)
242 {
243     napi_value result = nullptr;
244     NAPI_CALL(env, napi_create_object(env, &result));
245 
246     (void)SetNamedProperty(env, result, "VIDEO_SIZE", static_cast<int32_t>(PropertyType::VIDEO_SIZE));
247     (void)SetNamedProperty(env, result, "VIDEO_FPS", static_cast<int32_t>(PropertyType::VIDEO_FPS));
248     (void)SetNamedProperty(env, result, "WINDOW_SIZE", static_cast<int32_t>(PropertyType::WINDOW_SIZE));
249 
250     NAPI_CALL(env, napi_object_freeze(env, result));
251     return result;
252 }
253 
ExportChannelType(napi_env env)254 static napi_value ExportChannelType(napi_env env)
255 {
256     napi_value result = nullptr;
257     NAPI_CALL(env, napi_create_object(env, &result));
258 
259     (void)SetNamedProperty(env, result, "SOFT_BUS", static_cast<int32_t>(ChannelType::SOFT_BUS));
260     (void)SetNamedProperty(env, result, "LEGACY_CHANNEL", static_cast<int32_t>(ChannelType::LEGACY_CHANNEL));
261 
262     NAPI_CALL(env, napi_object_freeze(env, result));
263     return result;
264 }
265 
ExportProtocolType(napi_env env)266 static napi_value ExportProtocolType(napi_env env)
267 {
268     napi_value result = nullptr;
269     NAPI_CALL(env, napi_create_object(env, &result));
270 
271     (void)SetNamedProperty(env, result, "CAST_PLUS_MIRROR", static_cast<int32_t>(ProtocolType::CAST_PLUS_MIRROR));
272     (void)SetNamedProperty(env, result, "CAST_PLUS_STREAM", static_cast<int32_t>(ProtocolType::CAST_PLUS_STREAM));
273     (void)SetNamedProperty(env, result, "MIRACAST", static_cast<int32_t>(ProtocolType::MIRACAST));
274     (void)SetNamedProperty(env, result, "DLNA", static_cast<int32_t>(ProtocolType::DLNA));
275     (void)SetNamedProperty(env, result, "COOPERATION", static_cast<int32_t>(ProtocolType::COOPERATION));
276     (void)SetNamedProperty(env, result, "HICAR", static_cast<int32_t>(ProtocolType::HICAR));
277     (void)SetNamedProperty(env, result, "SUPER_LAUNCHER", static_cast<int32_t>(ProtocolType::SUPER_LAUNCHER));
278     NAPI_CALL(env, napi_object_freeze(env, result));
279     return result;
280 }
281 
ExportEndType(napi_env env)282 static napi_value ExportEndType(napi_env env)
283 {
284     napi_value result = nullptr;
285     NAPI_CALL(env, napi_create_object(env, &result));
286 
287     (void)SetNamedProperty(env, result, "CAST_SINK", static_cast<int32_t>(EndType::CAST_SINK));
288     (void)SetNamedProperty(env, result, "CAST_SOURCE", static_cast<int32_t>(EndType::CAST_SOURCE));
289 
290     NAPI_CALL(env, napi_object_freeze(env, result));
291     return result;
292 }
293 
ExportEventId(napi_env env)294 static napi_value ExportEventId(napi_env env)
295 {
296     napi_value result = nullptr;
297     NAPI_CALL(env, napi_create_object(env, &result));
298 
299     (void)SetNamedProperty(env, result, "EVENT_BEGIN", static_cast<int32_t>(EventId::EVENT_BEGIN));
300     (void)SetNamedProperty(env, result, "EVENT_END", static_cast<int32_t>(EventId::EVENT_END));
301 
302     NAPI_CALL(env, napi_object_freeze(env, result));
303     return result;
304 }
305 
ExportColorStandard(napi_env env)306 static napi_value ExportColorStandard(napi_env env)
307 {
308     napi_value result = nullptr;
309     NAPI_CALL(env, napi_create_object(env, &result));
310 
311     (void)SetNamedProperty(env, result, "BT709", static_cast<int32_t>(ColorStandard::BT709));
312     (void)SetNamedProperty(env, result, "BT601_PAL", static_cast<int32_t>(ColorStandard::BT601_PAL));
313     (void)SetNamedProperty(env, result, "BT601_NTSC", static_cast<int32_t>(ColorStandard::BT601_NTSC));
314     (void)SetNamedProperty(env, result, "BT2020", static_cast<int32_t>(ColorStandard::BT2020));
315 
316     NAPI_CALL(env, napi_object_freeze(env, result));
317     return result;
318 }
319 
ExportVideoCodecType(napi_env env)320 static napi_value ExportVideoCodecType(napi_env env)
321 {
322     napi_value result = nullptr;
323     NAPI_CALL(env, napi_create_object(env, &result));
324 
325     (void)SetNamedProperty(env, result, "H264", static_cast<int32_t>(VideoCodecType::H264));
326     (void)SetNamedProperty(env, result, "H265", static_cast<int32_t>(VideoCodecType::H265));
327 
328     NAPI_CALL(env, napi_object_freeze(env, result));
329     return result;
330 }
331 
ExportCastModeType(napi_env env)332 static napi_value ExportCastModeType(napi_env env)
333 {
334     napi_value result = nullptr;
335     NAPI_CALL(env, napi_create_object(env, &result));
336 
337     (void)SetNamedProperty(env, result, "MIRROR_CAST", static_cast<int32_t>(CastMode::MIRROR_CAST));
338     (void)SetNamedProperty(env, result, "APP_CAST", static_cast<int32_t>(CastMode::APP_CAST));
339 
340     NAPI_CALL(env, napi_object_freeze(env, result));
341     return result;
342 }
343 
InitEnums(napi_env env,napi_value exports)344 napi_status InitEnums(napi_env env, napi_value exports)
345 {
346     const napi_property_descriptor properties[] = {
347         DECLARE_NAPI_PROPERTY("PlayerStates", ExportPlayerStates(env)),
348         DECLARE_NAPI_PROPERTY("PlaybackSpeed", ExportPlaybackSpeed(env)),
349         DECLARE_NAPI_PROPERTY("LoopMode", ExportLoopMode(env)),
350         DECLARE_NAPI_PROPERTY("DeviceType", ExportDeviceType(env)),
351         DECLARE_NAPI_PROPERTY("SubDeviceType", ExportSubDeviceType(env)),
352         DECLARE_NAPI_PROPERTY("TriggerType", ExportTriggerType(env)),
353         DECLARE_NAPI_PROPERTY("DeviceState", ExportDeviceState(env)),
354         DECLARE_NAPI_PROPERTY("ReasonCode", ExportEventCode(env)),
355         DECLARE_NAPI_PROPERTY("ServiceStatus", ExportServiceStatus(env)),
356         DECLARE_NAPI_PROPERTY("DeviceStatusState", ExportDeviceStatusState(env)),
357         DECLARE_NAPI_PROPERTY("PropertyType", ExportPropertyType(env)),
358         DECLARE_NAPI_PROPERTY("ChannelType", ExportChannelType(env)),
359         DECLARE_NAPI_PROPERTY("ProtocolType", ExportProtocolType(env)),
360         DECLARE_NAPI_PROPERTY("EndType", ExportEndType(env)),
361         DECLARE_NAPI_PROPERTY("EventId", ExportEventId(env)),
362         DECLARE_NAPI_PROPERTY("ColorStandard", ExportColorStandard(env)),
363         DECLARE_NAPI_PROPERTY("VideoCodecType", ExportVideoCodecType(env)),
364         DECLARE_NAPI_PROPERTY("CastMode", ExportCastModeType(env))
365     };
366 
367     size_t count = sizeof(properties) / sizeof(napi_property_descriptor);
368     return napi_define_properties(env, exports, count, properties);
369 }
370 } // namespace CastEngineClient
371 } // namespace CastEngine
372 } // namespace OHOS
373