1 /*
2 * Copyright (c) 2022-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 <string>
17
18 #include "napi/native_common.h"
19 #include "avcontrol_command.h"
20 #include "avplayback_state.h"
21 #include "avsession_info.h"
22 #include "napi_avsession_enum.h"
23
24 namespace OHOS::AVSession {
SetNamedProperty(napi_env env,napi_value & obj,const std::string & name,int32_t value)25 static napi_status SetNamedProperty(napi_env env, napi_value& obj, const std::string& name, int32_t value)
26 {
27 napi_value property = nullptr;
28 napi_status status = napi_create_int32(env, value, &property);
29 if (status != napi_ok) {
30 return status;
31 }
32 status = napi_set_named_property(env, obj, name.c_str(), property);
33 if (status != napi_ok) {
34 return status;
35 }
36 return status;
37 }
38
ExportLoopMode(napi_env env)39 static napi_value ExportLoopMode(napi_env env)
40 {
41 napi_value result = nullptr;
42 napi_create_object(env, &result);
43
44 (void)SetNamedProperty(env, result, "LOOP_MODE_SEQUENCE", AVPlaybackState::LOOP_MODE_SEQUENCE);
45 (void)SetNamedProperty(env, result, "LOOP_MODE_SINGLE", AVPlaybackState::LOOP_MODE_SINGLE);
46 (void)SetNamedProperty(env, result, "LOOP_MODE_LIST", AVPlaybackState::LOOP_MODE_LIST);
47 (void)SetNamedProperty(env, result, "LOOP_MODE_SHUFFLE", AVPlaybackState::LOOP_MODE_SHUFFLE);
48
49 napi_object_freeze(env, result);
50 return result;
51 }
52
ExportAVCastCategory(napi_env env)53 static napi_value ExportAVCastCategory(napi_env env)
54 {
55 napi_value result = nullptr;
56 napi_create_object(env, &result);
57
58 (void)SetNamedProperty(env, result, "CATEGORY_LOCAL", AVCastCategory::CATEGORY_LOCAL);
59 (void)SetNamedProperty(env, result, "CATEGORY_REMOTE", AVCastCategory::CATEGORY_REMOTE);
60
61 napi_object_freeze(env, result);
62 return result;
63 }
64
ExportProtocolType(napi_env env)65 static napi_value ExportProtocolType(napi_env env)
66 {
67 napi_value result = nullptr;
68 napi_create_object(env, &result);
69
70 (void)SetNamedProperty(env, result, "TYPE_LOCAL", ProtocolType::TYPE_LOCAL);
71 (void)SetNamedProperty(env, result, "TYPE_CAST_PLUS_MIRROR", ProtocolType::TYPE_CAST_PLUS_MIRROR);
72 (void)SetNamedProperty(env, result, "TYPE_CAST_PLUS_STREAM", ProtocolType::TYPE_CAST_PLUS_STREAM);
73
74 napi_object_freeze(env, result);
75 return result;
76 }
77
ExportConnectionState(napi_env env)78 static napi_value ExportConnectionState(napi_env env)
79 {
80 napi_value result = nullptr;
81 napi_create_object(env, &result);
82
83 (void)SetNamedProperty(env, result, "STATE_CONNECTING", ConnectionState::STATE_CONNECTING);
84 (void)SetNamedProperty(env, result, "STATE_CONNECTED", ConnectionState::STATE_CONNECTED);
85 (void)SetNamedProperty(env, result, "STATE_DISCONNECTED", ConnectionState::STATE_DISCONNECTED);
86
87 napi_object_freeze(env, result);
88 return result;
89 }
90
ExportDeviceType(napi_env env)91 static napi_value ExportDeviceType(napi_env env)
92 {
93 napi_value result = nullptr;
94 napi_create_object(env, &result);
95
96 (void)SetNamedProperty(env, result, "DEVICE_TYPE_LOCAL", DeviceType::DEVICE_TYPE_LOCAL);
97 (void)SetNamedProperty(env, result, "DEVICE_TYPE_TV", DeviceType::DEVICE_TYPE_TV);
98 (void)SetNamedProperty(env, result, "DEVICE_TYPE_SMART_SPEAKER", DeviceType::DEVICE_TYPE_SPEAKER);
99 (void)SetNamedProperty(env, result, "DEVICE_TYPE_BLUETOOTH", DeviceType::DEVICE_TYPE_BLUETOOTH);
100
101 napi_object_freeze(env, result);
102 return result;
103 }
104
ExportPlaybackState(napi_env env)105 static napi_value ExportPlaybackState(napi_env env)
106 {
107 napi_value result = nullptr;
108 napi_create_object(env, &result);
109
110 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_INITIAL", AVPlaybackState::PLAYBACK_STATE_INITIAL);
111 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_PREPARE", AVPlaybackState::PLAYBACK_STATE_PREPARE);
112 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_PLAY", AVPlaybackState::PLAYBACK_STATE_PLAY);
113 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_PAUSE", AVPlaybackState::PLAYBACK_STATE_PAUSE);
114 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_FAST_FORWARD", AVPlaybackState::PLAYBACK_STATE_FAST_FORWARD);
115 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_REWIND", AVPlaybackState::PLAYBACK_STATE_REWIND);
116 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_STOP", AVPlaybackState::PLAYBACK_STATE_STOP);
117 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_COMPLETED", AVPlaybackState::PLAYBACK_STATE_COMPLETED);
118 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_RELEASED", AVPlaybackState::PLAYBACK_STATE_RELEASED);
119 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_ERROR", AVPlaybackState::PLAYBACK_STATE_ERROR);
120
121 napi_object_freeze(env, result);
122 return result;
123 }
124
ExportAVSessionErrorCode(napi_env env)125 static napi_value ExportAVSessionErrorCode(napi_env env)
126 {
127 napi_value result = nullptr;
128 napi_create_object(env, &result);
129
130 (void)SetNamedProperty(env, result, "ERR_CODE_SERVICE_EXCEPTION", AVSessionErrorCode::ERR_CODE_SERVICE_EXCEPTION);
131 (void)SetNamedProperty(env, result, "ERR_CODE_SESSION_NOT_EXIST", AVSessionErrorCode::ERR_CODE_SESSION_NOT_EXIST);
132 (void)SetNamedProperty(env, result, "ERR_CODE_CONTROLLER_NOT_EXIST",
133 AVSessionErrorCode::ERR_CODE_CONTROLLER_NOT_EXIST);
134 (void)SetNamedProperty(env, result, "ERR_CODE_REMOTE_CONNECTION_ERR",
135 AVSessionErrorCode::ERR_CODE_REMOTE_CONNECTION_ERR);
136 (void)SetNamedProperty(env, result, "ERR_CODE_COMMAND_INVALID", AVSessionErrorCode::ERR_CODE_COMMAND_INVALID);
137 (void)SetNamedProperty(env, result, "ERR_CODE_SESSION_INACTIVE", AVSessionErrorCode::ERR_CODE_SESSION_INACTIVE);
138 (void)SetNamedProperty(env, result, "ERR_CODE_MESSAGE_OVERLOAD", AVSessionErrorCode::ERR_CODE_MESSAGE_OVERLOAD);
139 (void)SetNamedProperty(env, result, "ERR_CODE_DEVICE_CONNECTION_FAILED",
140 AVSessionErrorCode::ERR_CODE_DEVICE_CONNECTION_FAILED);
141 (void)SetNamedProperty(env, result, "ERR_CODE_REMOTE_CONNECTION_NOT_EXIST",
142 AVSessionErrorCode::ERR_CODE_REMOTE_CONNECTION_NOT_EXIST);
143
144 napi_object_freeze(env, result);
145 return result;
146 }
147
InitEnums(napi_env env,napi_value exports)148 napi_status InitEnums(napi_env env, napi_value exports)
149 {
150 const napi_property_descriptor properties[] = {
151 DECLARE_NAPI_PROPERTY("AVCastCategory", ExportAVCastCategory(env)),
152 DECLARE_NAPI_PROPERTY("ProtocolType", ExportProtocolType(env)),
153 DECLARE_NAPI_PROPERTY("ConnectionState", ExportConnectionState(env)),
154 DECLARE_NAPI_PROPERTY("DeviceType", ExportDeviceType(env)),
155 DECLARE_NAPI_PROPERTY("LoopMode", ExportLoopMode(env)),
156 DECLARE_NAPI_PROPERTY("PlaybackState", ExportPlaybackState(env)),
157 DECLARE_NAPI_PROPERTY("AVSessionErrorCode", ExportAVSessionErrorCode(env)),
158 };
159
160 size_t count = sizeof(properties) / sizeof(napi_property_descriptor);
161 return napi_define_properties(env, exports, count, properties);
162 }
163 }