1 /*
2 * Copyright (C) 2021 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 "media_enum_napi.h"
17 #include <map>
18 #include <vector>
19 #include "media_log.h"
20 #include "media_errors.h"
21 #include "player.h"
22 #include "recorder.h"
23 #include "avcodec_info.h"
24 #include "avcodec_common.h"
25 #include "recorder_profiles.h"
26 #include "av_common.h"
27
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "MediaEnumNapi"};
30 }
31
32 namespace OHOS {
33 namespace Media {
34 struct JsEnumInt {
35 std::string_view enumName;
36 int32_t enumInt;
37 };
38
39 struct JsEnumString {
40 std::string_view enumName;
41 std::string_view enumString;
42 };
43
44 static const std::vector<struct JsEnumInt> g_mediaErrorCode = {
45 { "MSERR_OK", MediaServiceExtErrCode::MSERR_EXT_OK },
46 { "MSERR_NO_MEMORY", MediaServiceExtErrCode::MSERR_EXT_NO_MEMORY },
47 { "MSERR_OPERATION_NOT_PERMIT", MediaServiceExtErrCode::MSERR_EXT_OPERATE_NOT_PERMIT },
48 { "MSERR_INVALID_VAL", MediaServiceExtErrCode::MSERR_EXT_INVALID_VAL },
49 { "MSERR_IO", MediaServiceExtErrCode::MSERR_EXT_IO },
50 { "MSERR_TIMEOUT", MediaServiceExtErrCode::MSERR_EXT_TIMEOUT },
51 { "MSERR_UNKNOWN", MediaServiceExtErrCode::MSERR_EXT_UNKNOWN },
52 { "MSERR_SERVICE_DIED", MediaServiceExtErrCode::MSERR_EXT_SERVICE_DIED },
53 { "MSERR_INVALID_STATE", MediaServiceExtErrCode::MSERR_EXT_INVALID_STATE },
54 { "MSERR_UNSUPPORTED", MediaServiceExtErrCode::MSERR_EXT_UNSUPPORT },
55 };
56
57 static const std::vector<struct JsEnumInt> g_AVErrorCode = {
58 { "AVERR_OK", MediaServiceExtErrCodeAPI9::MSERR_EXT_API9_OK },
59 { "AVERR_NO_PERMISSION", MediaServiceExtErrCodeAPI9::MSERR_EXT_API9_NO_PERMISSION },
60 { "AVERR_INVALID_PARAMETER", MediaServiceExtErrCodeAPI9::MSERR_EXT_API9_INVALID_PARAMETER },
61 { "AVERR_UNSUPPORT_CAPABILITY", MediaServiceExtErrCodeAPI9::MSERR_EXT_API9_UNSUPPORT_CAPABILITY },
62 { "AVERR_NO_MEMORY", MediaServiceExtErrCodeAPI9::MSERR_EXT_API9_NO_MEMORY },
63 { "AVERR_OPERATE_NOT_PERMIT", MediaServiceExtErrCodeAPI9::MSERR_EXT_API9_OPERATE_NOT_PERMIT },
64 { "AVERR_IO", MediaServiceExtErrCodeAPI9::MSERR_EXT_API9_IO },
65 { "AVERR_TIMEOUT", MediaServiceExtErrCodeAPI9::MSERR_EXT_API9_TIMEOUT },
66 { "AVERR_SERVICE_DIED", MediaServiceExtErrCodeAPI9::MSERR_EXT_API9_SERVICE_DIED },
67 { "AVERR_UNSUPPORT_FORMAT", MediaServiceExtErrCodeAPI9::MSERR_EXT_API9_UNSUPPORT_FORMAT },
68 };
69
70 static const std::vector<struct JsEnumInt> g_avDataSourceError = {
71 { "SOURCE_ERROR_IO", MediaDataSourceError::SOURCE_ERROR_IO },
72 { "SOURCE_ERROR_EOF", MediaDataSourceError::SOURCE_ERROR_EOF },
73 };
74
75 static const std::vector<struct JsEnumInt> g_bufferingInfoType = {
76 { "BUFFERING_START", BufferingInfoType::BUFFERING_START },
77 { "BUFFERING_END", BufferingInfoType::BUFFERING_END },
78 { "BUFFERING_PERCENT", BufferingInfoType::BUFFERING_PERCENT },
79 { "CACHED_DURATION", BufferingInfoType::CACHED_DURATION },
80 };
81
82 static const std::vector<struct JsEnumInt> g_recorderAudioEncoder = {
83 { "DEFAULT", AudioCodecFormat::AUDIO_DEFAULT },
84 { "AMR_NB", 1 }, // Provides implementation only
85 { "AMR_WB", 2 }, // Provides implementation only
86 { "AAC_LC", AudioCodecFormat::AAC_LC },
87 { "HE_AAC", 4 }, // Provides implementation only
88 };
89
90 static const std::vector<struct JsEnumInt> g_recorderAudioOutputFormat = {
91 { "DEFAULT", OutputFormatType::FORMAT_DEFAULT },
92 { "MPEG_4", OutputFormatType::FORMAT_MPEG_4 },
93 { "AMR_NB", 3 }, // Provides implementation only
94 { "AMR_WB", 4 }, // Provides implementation only
95 { "AAC_ADTS", OutputFormatType::FORMAT_M4A },
96 };
97
98 static const std::vector<struct JsEnumInt> g_playbackSpeed = {
99 { "SPEED_FORWARD_0_75_X", PlaybackRateMode::SPEED_FORWARD_0_75_X },
100 { "SPEED_FORWARD_1_00_X", PlaybackRateMode::SPEED_FORWARD_1_00_X },
101 { "SPEED_FORWARD_1_25_X", PlaybackRateMode::SPEED_FORWARD_1_25_X },
102 { "SPEED_FORWARD_1_75_X", PlaybackRateMode::SPEED_FORWARD_1_75_X },
103 { "SPEED_FORWARD_2_00_X", PlaybackRateMode::SPEED_FORWARD_2_00_X },
104 };
105
106 static const std::vector<struct JsEnumInt> g_mediaType = {
107 { "MEDIA_TYPE_AUD", MediaType::MEDIA_TYPE_AUD },
108 { "MEDIA_TYPE_VID", MediaType::MEDIA_TYPE_VID },
109 { "MEDIA_TYPE_SUBTITLE", MediaType::MEDIA_TYPE_SUBTITLE },
110 };
111
112 static const std::vector<struct JsEnumInt> g_videoRecorderQualityLevel = {
113 { "RECORDER_QUALITY_LOW", VideoRecorderQualityLevel::RECORDER_QUALITY_LOW },
114 { "RECORDER_QUALITY_HIGH", VideoRecorderQualityLevel::RECORDER_QUALITY_HIGH },
115 { "RECORDER_QUALITY_QCIF", VideoRecorderQualityLevel::RECORDER_QUALITY_QCIF },
116 { "RECORDER_QUALITY_CIF", VideoRecorderQualityLevel::RECORDER_QUALITY_CIF },
117 { "RECORDER_QUALITY_480P", VideoRecorderQualityLevel::RECORDER_QUALITY_480P },
118 { "RECORDER_QUALITY_720P", VideoRecorderQualityLevel::RECORDER_QUALITY_720P },
119 { "RECORDER_QUALITY_1080P", VideoRecorderQualityLevel::RECORDER_QUALITY_1080P },
120 { "RECORDER_QUALITY_QVGA", VideoRecorderQualityLevel::RECORDER_QUALITY_QVGA },
121 { "RECORDER_QUALITY_2160P", VideoRecorderQualityLevel::RECORDER_QUALITY_2160P },
122 };
123
124 static const std::vector<struct JsEnumInt> g_audioSourceType = {
125 { "AUDIO_SOURCE_TYPE_DEFAULT", AudioSourceType::AUDIO_SOURCE_DEFAULT },
126 { "AUDIO_SOURCE_TYPE_MIC", AudioSourceType::AUDIO_MIC },
127 };
128
129 static const std::vector<struct JsEnumInt> g_videoSourceType = {
130 { "VIDEO_SOURCE_TYPE_SURFACE_YUV", VideoSourceType::VIDEO_SOURCE_SURFACE_YUV },
131 { "VIDEO_SOURCE_TYPE_SURFACE_ES", VideoSourceType::VIDEO_SOURCE_SURFACE_ES },
132 };
133
134 static const std::vector<struct JsEnumInt> g_frameFlags = {
135 { "EOS_FRAME", AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS },
136 { "SYNC_FRAME", AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_SYNC_FRAME },
137 { "PARTIAL_FRAME", AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_PARTIAL_FRAME },
138 { "CODEC_DATA", AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_CODEC_DATA },
139 };
140
141 static const std::vector<struct JsEnumInt> g_seekMode = {
142 { "SEEK_NEXT_SYNC", PlayerSeekMode::SEEK_NEXT_SYNC },
143 { "SEEK_PREV_SYNC", PlayerSeekMode::SEEK_PREVIOUS_SYNC },
144 { "SEEK_CLOSEST_SYNC", PlayerSeekMode::SEEK_CLOSEST_SYNC },
145 { "SEEK_CLOSEST", PlayerSeekMode::SEEK_CLOSEST },
146 };
147
148 static const std::vector<struct JsEnumInt> g_AVCodecType = {
149 { "AVCODEC_TYPE_VIDEO_ENCODER", AVCodecType::AVCODEC_TYPE_VIDEO_ENCODER },
150 { "AVCODEC_TYPE_VIDEO_DECODER", AVCodecType::AVCODEC_TYPE_VIDEO_DECODER },
151 { "AVCODEC_TYPE_AUDIO_ENCODER", AVCodecType::AVCODEC_TYPE_AUDIO_ENCODER },
152 { "AVCODEC_TYPE_AUDIO_DECODER", AVCodecType::AVCODEC_TYPE_AUDIO_DECODER },
153 };
154
155 static const std::vector<struct JsEnumInt> g_AACProfile = {
156 { "AAC_PROFILE_LC", AACProfile::AAC_PROFILE_LC },
157 { "AAC_PROFILE_ELD", AACProfile::AAC_PROFILE_ELD },
158 { "AAC_PROFILE_ERLC", AACProfile::AAC_PROFILE_ERLC },
159 { "AAC_PROFILE_HE", AACProfile::AAC_PROFILE_HE },
160 { "AAC_PROFILE_HE_V2", AACProfile::AAC_PROFILE_HE_V2 },
161 { "AAC_PROFILE_LD", AACProfile::AAC_PROFILE_LD },
162 { "AAC_PROFILE_MAIN", AACProfile::AAC_PROFILE_MAIN },
163 };
164
165 static const std::vector<struct JsEnumInt> g_videoEncodeBitrateMode = {
166 { "CBR", VideoEncodeBitrateMode::CBR },
167 { "VBR", VideoEncodeBitrateMode::VBR },
168 { "CQ", VideoEncodeBitrateMode::CQ },
169 };
170
171 static const std::vector<struct JsEnumInt> g_videoPixelFormat = {
172 { "YUVI420", VideoPixelFormat::YUVI420 },
173 { "NV12", VideoPixelFormat::NV12 },
174 { "NV21", VideoPixelFormat::NV21 },
175 { "SURFACE_FORMAT", VideoPixelFormat::SURFACE_FORMAT },
176 { "RGBA", VideoPixelFormat::RGBA },
177 };
178
179 static const std::vector<struct JsEnumInt> g_AVCProfile = {
180 { "AVC_PROFILE_BASELINE", AVCProfile::AVC_PROFILE_BASELINE },
181 { "AVC_PROFILE_CONSTRAINED_BASELINE", AVCProfile::AVC_PROFILE_CONSTRAINED_BASELINE },
182 { "AVC_PROFILE_CONSTRAINED_HIGH", AVCProfile::AVC_PROFILE_CONSTRAINED_HIGH },
183 { "AVC_PROFILE_EXTENDED", AVCProfile::AVC_PROFILE_EXTENDED },
184 { "AVC_PROFILE_HIGH", AVCProfile::AVC_PROFILE_HIGH },
185 { "AVC_PROFILE_HIGH_10", AVCProfile::AVC_PROFILE_HIGH_10 },
186 { "AVC_PROFILE_HIGH_422", AVCProfile::AVC_PROFILE_HIGH_422 },
187 { "AVC_PROFILE_HIGH_444", AVCProfile::AVC_PROFILE_HIGH_444 },
188 { "AVC_PROFILE_MAIN", AVCProfile::AVC_PROFILE_MAIN },
189 };
190
191 static const std::vector<struct JsEnumInt> g_HEVCProfile = {
192 { "HEVC_PROFILE_MAIN", HEVCProfile::HEVC_PROFILE_MAIN },
193 { "HEVC_PROFILE_MAIN_10", HEVCProfile::HEVC_PROFILE_MAIN_10 },
194 { "HEVC_PROFILE_MAIN_STILL", HEVCProfile::HEVC_PROFILE_MAIN_STILL },
195 };
196
197 static const std::vector<struct JsEnumInt> g_MPEG2Profile = {
198 { "MPEG2_PROFILE_422", MPEG2Profile::MPEG2_PROFILE_422 },
199 { "MPEG2_PROFILE_HIGH", MPEG2Profile::MPEG2_PROFILE_HIGH },
200 { "MPEG2_PROFILE_MAIN", MPEG2Profile::MPEG2_PROFILE_MAIN },
201 { "MPEG2_PROFILE_SNR", MPEG2Profile::MPEG2_PROFILE_SNR },
202 { "MPEG2_PROFILE_SIMPLE", MPEG2Profile::MPEG2_PROFILE_SIMPLE },
203 { "MPEG2_PROFILE_SPATIAL", MPEG2Profile::MPEG2_PROFILE_SPATIAL },
204 };
205
206 static const std::vector<struct JsEnumInt> g_MPEG4Profile = {
207 { "MPEG4_PROFILE_ADVANCED_CODING", MPEG4Profile::MPEG4_PROFILE_ADVANCED_CODING },
208 { "MPEG4_PROFILE_ADVANCED_CORE", MPEG4Profile::MPEG4_PROFILE_ADVANCED_CORE },
209 { "MPEG4_PROFILE_ADVANCED_REAL_TIME", MPEG4Profile::MPEG4_PROFILE_ADVANCED_REAL_TIME },
210 { "MPEG4_PROFILE_ADVANCED_SCALABLE", MPEG4Profile::MPEG4_PROFILE_ADVANCED_SCALABLE },
211 { "MPEG4_PROFILE_ADVANCED_SIMPLE", MPEG4Profile::MPEG4_PROFILE_ADVANCED_SIMPLE },
212 { "MPEG4_PROFILE_BASIC_ANIMATED", MPEG4Profile::MPEG4_PROFILE_BASIC_ANIMATED },
213 { "MPEG4_PROFILE_CORE", MPEG4Profile::MPEG4_PROFILE_CORE },
214 { "MPEG4_PROFILE_CORE_SCALABLE", MPEG4Profile::MPEG4_PROFILE_CORE_SCALABLE },
215 { "MPEG4_PROFILE_HYBRID", MPEG4Profile::MPEG4_PROFILE_HYBRID },
216 { "MPEG4_PROFILE_MAIN", MPEG4Profile::MPEG4_PROFILE_MAIN },
217 { "MPEG4_PROFILE_NBIT", MPEG4Profile::MPEG4_PROFILE_NBIT },
218 { "MPEG4_PROFILE_SCALABLE_TEXTURE", MPEG4Profile::MPEG4_PROFILE_SCALABLE_TEXTURE },
219 { "MPEG4_PROFILE_SIMPLE", MPEG4Profile::MPEG4_PROFILE_SIMPLE },
220 { "MPEG4_PROFILE_SIMPLE_FBA", MPEG4Profile::MPEG4_PROFILE_SIMPLE_FBA },
221 { "MPEG4_PROFILE_SIMPLE_FACE", MPEG4Profile::MPEG4_PROFILE_SIMPLE_FACE },
222 { "MPEG4_PROFILE_SIMPLE_SCALABLE", MPEG4Profile::MPEG4_PROFILE_SIMPLE_SCALABLE },
223 };
224
225 static const std::vector<struct JsEnumInt> g_H263Profile = {
226 { "H263_PROFILE_BACKWARD_COMPATIBLE", H263Profile::H263_PROFILE_BACKWARD_COMPATIBLE },
227 { "H263_PROFILE_BASELINE", H263Profile::H263_PROFILE_BASELINE },
228 { "H263_PROFILE_H320_CODING", H263Profile::H263_PROFILE_H320_CODING },
229 { "H263_PROFILE_HIGH_COMPRESSION", H263Profile::H263_PROFILE_HIGH_COMPRESSION },
230 { "H263_PROFILE_HIGH_LATENCY", H263Profile::H263_PROFILE_HIGH_LATENCY },
231 { "H263_PROFILE_ISW_V2", H263Profile::H263_PROFILE_ISW_V2 },
232 { "H263_PROFILE_ISW_V3", H263Profile::H263_PROFILE_ISW_V3 },
233 { "H263_PROFILE_INTERLACE", H263Profile::H263_PROFILE_INTERLACE },
234 { "H263_PROFILE_INTERNET", H263Profile::H263_PROFILE_INTERNET },
235 };
236
237 static const std::vector<struct JsEnumInt> g_VP8Profile = {
238 { "VP8_PROFILE_MAIN", VP8Profile::VP8_PROFILE_MAIN },
239 };
240
241 static const std::vector<struct JsEnumInt> g_VideoScaleType = {
242 { "VIDEO_SCALE_TYPE_FIT", VideoScaleType::VIDEO_SCALE_TYPE_FIT },
243 { "VIDEO_SCALE_TYPE_FIT_CROP", VideoScaleType::VIDEO_SCALE_TYPE_FIT_CROP },
244 };
245
246 static const std::vector<struct JsEnumInt> g_stateChangeReason = {
247 { "USER", StateChangeReason::USER },
248 { "BACKGROUND", StateChangeReason::BACKGROUND },
249 };
250
251 static const std::vector<struct JsEnumString> g_containerFormatType = {
252 { "CFT_MPEG_4", ContainerFormatType::CFT_MPEG_4 },
253 { "CFT_MPEG_4A", ContainerFormatType::CFT_MPEG_4A },
254 };
255
256 static const std::vector<struct JsEnumString> g_codecMimeType = {
257 { "VIDEO_H263", CodecMimeType::VIDEO_H263 },
258 { "VIDEO_AVC", CodecMimeType::VIDEO_AVC },
259 { "VIDEO_MPEG2", CodecMimeType::VIDEO_MPEG2 },
260 { "VIDEO_HEVC", CodecMimeType::VIDEO_HEVC },
261 { "VIDEO_MPEG4", CodecMimeType::VIDEO_MPEG4 },
262 { "VIDEO_VP8", CodecMimeType::VIDEO_VP8 },
263 { "VIDEO_VP9", CodecMimeType::VIDEO_VP9 },
264 { "AUDIO_AMR_NB", CodecMimeType::AUDIO_AMR_NB },
265 { "AUDIO_AMR_WB", CodecMimeType::AUDIO_AMR_WB },
266 { "AUDIO_MPEG", CodecMimeType::AUDIO_MPEG },
267 { "AUDIO_AAC", CodecMimeType::AUDIO_AAC },
268 { "AUDIO_VORBIS", CodecMimeType::AUDIO_VORBIS },
269 { "AUDIO_OPUS", CodecMimeType::AUDIO_OPUS },
270 { "AUDIO_FLAC", CodecMimeType::AUDIO_FLAC },
271 { "AUDIO_RAW", CodecMimeType::AUDIO_RAW },
272 };
273
274 static const std::vector<struct JsEnumString> g_mediaDescriptionKey = {
275 { "MD_KEY_TRACK_INDEX", "track_index" },
276 { "MD_KEY_TRACK_TYPE", "track_type" },
277 { "MD_KEY_CODEC_MIME", "codec_mime" },
278 { "MD_KEY_DURATION", "duration" },
279 { "MD_KEY_BITRATE", "bitrate" },
280 { "MD_KEY_MAX_INPUT_SIZE", "max_input_size" },
281 { "MD_KEY_MAX_ENCODER_FPS", "max_encoder_fps" },
282 { "MD_KEY_WIDTH", "width" },
283 { "MD_KEY_HEIGHT", "height" },
284 { "MD_KEY_PIXEL_FORMAT", "pixel_format" },
285 { "MD_KEY_AUDIO_SAMPLE_FORMAT", "audio_sample_format" },
286 { "MD_KEY_FRAME_RATE", "frame_rate" },
287 { "MD_KEY_CAPTURE_RATE", "capture_rate" },
288 { "MD_KEY_I_FRAME_INTERVAL", "i_frame_interval" },
289 { "MD_KEY_REQUEST_I_FRAME", "req_i_frame" },
290 { "MD_KEY_REPEAT_FRAME_AFTER", "repeat_frame_after" },
291 { "MD_KEY_SUSPEND_INPUT_SURFACE", "suspend_input_surface" },
292 { "MD_KEY_VIDEO_ENCODE_BITRATE_MODE", "video_encode_bitrate_mode" },
293 { "MD_KEY_PROFILE", "codec_profile" },
294 { "MD_KEY_QUALITY", "codec_quality" },
295 { "MD_KEY_RECT_TOP", "rect_top" },
296 { "MD_KEY_RECT_BOTTOM", "rect_bottom" },
297 { "MD_KEY_RECT_LEFT", "rect_left" },
298 { "MD_KEY_RECT_RIGHT", "rect_right" },
299 { "MD_KEY_COLOR_STANDARD", "color_standard" },
300 { "MD_KEY_AUD_CHANNEL_COUNT", "channel_count" },
301 { "MD_KEY_AUD_SAMPLE_RATE", "sample_rate" },
302 { "MD_KEY_CUSTOM", "vendor.custom" },
303 { "MD_KEY_LANGUAGE", "language" },
304 };
305
306 static const std::map<std::string_view, const std::vector<struct JsEnumInt>&> g_intEnumClassMap = {
307 { "AVErrorCode", g_AVErrorCode},
308 { "MediaErrorCode", g_mediaErrorCode },
309 { "AVDataSourceError", g_avDataSourceError },
310 { "BufferingInfoType", g_bufferingInfoType },
311 { "AudioEncoder", g_recorderAudioEncoder },
312 { "AudioOutputFormat", g_recorderAudioOutputFormat },
313 { "PlaybackSpeed", g_playbackSpeed },
314 { "MediaType", g_mediaType },
315 { "VideoRecorderQualityLevel", g_videoRecorderQualityLevel },
316 { "AudioSourceType", g_audioSourceType },
317 { "VideoSourceType", g_videoSourceType },
318 { "FrameFlags", g_frameFlags },
319 { "SeekMode", g_seekMode },
320 { "AVCodecType", g_AVCodecType },
321 { "AACProfile", g_AACProfile },
322 { "VideoEncodeBitrateMode", g_videoEncodeBitrateMode },
323 { "VideoPixelFormat", g_videoPixelFormat },
324 { "AVCProfile", g_AVCProfile },
325 { "HEVCProfile", g_HEVCProfile },
326 { "MPEG2Profile", g_MPEG2Profile },
327 { "MPEG4Profile", g_MPEG4Profile },
328 { "H263Profile", g_H263Profile},
329 { "VP8Profile", g_VP8Profile },
330 { "VideoScaleType", g_VideoScaleType},
331 { "StateChangeReason", g_stateChangeReason},
332 };
333
334 static const std::map<std::string_view, const std::vector<struct JsEnumString>&> g_stringEnumClassMap = {
335 { "MediaDescriptionKey", g_mediaDescriptionKey },
336 { "ContainerFormatType", g_containerFormatType },
337 { "CodecMimeType", g_codecMimeType },
338 };
339
JsEnumIntInit(napi_env env,napi_value exports)340 napi_value MediaEnumNapi::JsEnumIntInit(napi_env env, napi_value exports)
341 {
342 for (auto it = g_intEnumClassMap.begin(); it != g_intEnumClassMap.end(); it++) {
343 auto &enumClassName = it->first;
344 auto &enumItemVec = it->second;
345 int32_t vecSize = enumItemVec.size();
346 std::vector<napi_value> value;
347 value.resize(vecSize);
348 for (int32_t index = 0; index < vecSize; ++index) {
349 napi_create_int32(env, enumItemVec[index].enumInt, &value[index]);
350 }
351
352 std::vector<napi_property_descriptor> property;
353 property.resize(vecSize);
354 for (int32_t index = 0; index < vecSize; ++index) {
355 property[index] = napi_property_descriptor DECLARE_NAPI_STATIC_PROPERTY(
356 enumItemVec[index].enumName.data(), value[index]);
357 }
358
359 auto constructor = [](napi_env env, napi_callback_info info) {
360 napi_value jsThis = nullptr;
361 napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
362 return jsThis;
363 };
364
365 napi_value result = nullptr;
366 napi_status status = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, constructor,
367 nullptr, property.size(), property.data(), &result);
368 CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define enum");
369
370 status = napi_set_named_property(env, exports, enumClassName.data(), result);
371 CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to set result");
372 }
373 return exports;
374 }
375
JsEnumStringInit(napi_env env,napi_value exports)376 napi_value MediaEnumNapi::JsEnumStringInit(napi_env env, napi_value exports)
377 {
378 for (auto it = g_stringEnumClassMap.begin(); it != g_stringEnumClassMap.end(); it++) {
379 auto &enumClassName = it->first;
380 auto &enumItemVec = it->second;
381 int32_t vecSize = enumItemVec.size();
382 std::vector<napi_value> value;
383 value.resize(vecSize);
384 for (int32_t index = 0; index < vecSize; ++index) {
385 napi_create_string_utf8(env, enumItemVec[index].enumString.data(), NAPI_AUTO_LENGTH, &value[index]);
386 }
387
388 std::vector<napi_property_descriptor> property;
389 property.resize(vecSize);
390 for (int32_t index = 0; index < vecSize; ++index) {
391 property[index] = napi_property_descriptor DECLARE_NAPI_STATIC_PROPERTY(
392 enumItemVec[index].enumName.data(), value[index]);
393 }
394
395 auto constructor = [](napi_env env, napi_callback_info info) {
396 napi_value jsThis = nullptr;
397 napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
398 return jsThis;
399 };
400
401 napi_value result = nullptr;
402 napi_status status = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, constructor,
403 nullptr, property.size(), property.data(), &result);
404 CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define enum");
405
406 status = napi_set_named_property(env, exports, enumClassName.data(), result);
407 CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to set result");
408 }
409 return exports;
410 }
411
Init(napi_env env,napi_value exports)412 napi_value MediaEnumNapi::Init(napi_env env, napi_value exports)
413 {
414 JsEnumIntInit(env, exports);
415 JsEnumStringInit(env, exports);
416 return exports;
417 }
418 } // namespace Media
419 } // namespace OHOS