1 /*
2 * Copyright (c) 2022 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 "napi_avsession_enum.h"
22
23 namespace OHOS::AVSession {
SetNamedProperty(napi_env env,napi_value & obj,const std::string & name,int32_t value)24 static napi_status SetNamedProperty(napi_env env, napi_value& obj, const std::string& name, int32_t value)
25 {
26 napi_value property = nullptr;
27 napi_status status = napi_create_int32(env, value, &property);
28 if (status != napi_ok) {
29 return status;
30 }
31 status = napi_set_named_property(env, obj, name.c_str(), property);
32 if (status != napi_ok) {
33 return status;
34 }
35 return status;
36 }
37
ExportLoopMode(napi_env env)38 static napi_value ExportLoopMode(napi_env env)
39 {
40 napi_value result = nullptr;
41 napi_create_object(env, &result);
42
43 (void)SetNamedProperty(env, result, "LOOP_MODE_SEQUENCE", AVPlaybackState::LOOP_MODE_SEQUENCE);
44 (void)SetNamedProperty(env, result, "LOOP_MODE_SINGLE", AVPlaybackState::LOOP_MODE_SINGLE);
45 (void)SetNamedProperty(env, result, "LOOP_MODE_LIST", AVPlaybackState::LOOP_MODE_LIST);
46 (void)SetNamedProperty(env, result, "LOOP_MODE_SHUFFLE", AVPlaybackState::LOOP_MODE_SHUFFLE);
47
48 napi_object_freeze(env, result);
49 return result;
50 }
51
ExportPlaybackState(napi_env env)52 static napi_value ExportPlaybackState(napi_env env)
53 {
54 napi_value result = nullptr;
55 napi_create_object(env, &result);
56
57 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_INITIAL", AVPlaybackState::PLAYBACK_STATE_INITIAL);
58 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_PREPARE", AVPlaybackState::PLAYBACK_STATE_PREPARING);
59 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_PLAY", AVPlaybackState::PLAYBACK_STATE_PLAYING);
60 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_PAUSE", AVPlaybackState::PLAYBACK_STATE_PAUSED);
61 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_FAST_FORWARD", AVPlaybackState::PLAYBACK_STATE_FAST_FORWARD);
62 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_REWIND", AVPlaybackState::PLAYBACK_STATE_REWIND);
63 (void)SetNamedProperty(env, result, "PLAYBACK_STATE_STOP", AVPlaybackState::PLAYBACK_STATE_STOP);
64
65 napi_object_freeze(env, result);
66 return result;
67 }
68
InitEnums(napi_env env,napi_value exports)69 napi_status InitEnums(napi_env env, napi_value exports)
70 {
71 const napi_property_descriptor properties[] = {
72 DECLARE_NAPI_PROPERTY("LoopMode", ExportLoopMode(env)),
73 DECLARE_NAPI_PROPERTY("PlaybackState", ExportPlaybackState(env)),
74 };
75
76 size_t count = sizeof(properties) / sizeof(napi_property_descriptor);
77 return napi_define_properties(env, exports, count, properties);
78 }
79 }