1 /*
2  * Copyright (c) 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 "napi_media_info_holder.h"
17 #include "avsession_log.h"
18 #include "avsession_pixel_map_adapter.h"
19 #include "napi_utils.h"
20 #include "pixel_map_napi.h"
21 
22 namespace OHOS::AVSession {
23 std::map<std::string, NapiMediaInfoHolder::GetterType> NapiMediaInfoHolder::getterMap_ = {
24     { "currentIndex", GetCurrentIndex },
25     { "playInfos", GetPlayInfos },
26 };
27 
28 std::map<int32_t, NapiMediaInfoHolder::SetterType> NapiMediaInfoHolder::setterMap_ = {
29     { MediaInfoHolder::MEDIA_INFO_HOLDER_KEY_CURRENT_INDEX, SetCurrentIndex },
30     { MediaInfoHolder::MEDIA_INFO_HOLDER_KEY_PLAY_INFOS, SetPlayInfos },
31 };
32 
GetValue(napi_env env,napi_value in,MediaInfoHolder & out)33 napi_status NapiMediaInfoHolder::GetValue(napi_env env, napi_value in, MediaInfoHolder& out)
34 {
35     bool hasCurrentIndex = false;
36     napi_has_named_property(env, in, "currentIndex", &hasCurrentIndex);
37     if (!hasCurrentIndex) {
38         SLOGE("currentIndex is not exit in MediaInfoHolder");
39         return napi_invalid_arg;
40     }
41 
42     bool hasProperty = false;
43     napi_has_named_property(env, in, "playInfos", &hasProperty);
44     if (!hasProperty) {
45         SLOGE("playInfos is not exit in MediaInfoHolder");
46         return napi_invalid_arg;
47     }
48 
49     std::vector<std::string> propertyNames;
50     auto status = NapiUtils::GetPropertyNames(env, in, propertyNames);
51     CHECK_RETURN(status == napi_ok, "get property name failed", status);
52 
53     for (const auto& name : propertyNames) {
54         auto it = getterMap_.find(name);
55         if (it == getterMap_.end()) {
56             SLOGE("property %{public}s is not of queueitem", name.c_str());
57             return napi_invalid_arg;
58         }
59         auto getter = it->second;
60         if (getter(env, in, out) != napi_ok) {
61             SLOGE("get property %{public}s failed", name.c_str());
62             return napi_generic_failure;
63         }
64     }
65     return napi_ok;
66 }
67 
SetValue(napi_env env,const MediaInfoHolder & in,napi_value & out)68 napi_status NapiMediaInfoHolder::SetValue(napi_env env, const MediaInfoHolder& in, napi_value& out)
69 {
70     napi_status status = napi_create_object(env, &out);
71     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
72 
73     for (int i = 0; i < MediaInfoHolder::MEDIA_INFO_HOLDER_KEY_MAX; ++i) {
74         auto setter = setterMap_[i];
75         if (setter(env, in, out) != napi_ok) {
76             SLOGE("set property %{public}d failed", i);
77             return napi_generic_failure;
78         }
79     }
80     return napi_ok;
81 }
82 
GetCurrentIndex(napi_env env,napi_value in,MediaInfoHolder & out)83 napi_status NapiMediaInfoHolder::GetCurrentIndex(napi_env env, napi_value in, MediaInfoHolder& out)
84 {
85     int32_t property {};
86     auto status = NapiUtils::GetNamedProperty(env, in, "currentIndex", property);
87     CHECK_RETURN(status == napi_ok, "get property failed", status);
88     out.SetCurrentIndex(property);
89     return status;
90 }
91 
SetCurrentIndex(napi_env env,const MediaInfoHolder & in,napi_value & out)92 napi_status NapiMediaInfoHolder::SetCurrentIndex(napi_env env, const MediaInfoHolder& in, napi_value& out)
93 {
94     napi_value property {};
95     auto status = NapiUtils::SetValue(env, in.GetCurrentIndex(), property);
96     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
97     status = napi_set_named_property(env, out, "currentIndex", property);
98     CHECK_RETURN(status == napi_ok, "set property failed", status);
99     return status;
100 }
101 
GetPlayInfos(napi_env env,napi_value in,MediaInfoHolder & out)102 napi_status NapiMediaInfoHolder::GetPlayInfos(napi_env env, napi_value in, MediaInfoHolder& out)
103 {
104     std::vector<AVQueueItem> property {};
105     auto status = NapiUtils::GetNamedProperty(env, in, "playInfos", property);
106     CHECK_RETURN(status == napi_ok, "get property failed", status);
107     out.SetPlayInfos(property);
108     return status;
109 }
110 
SetPlayInfos(napi_env env,const MediaInfoHolder & in,napi_value & out)111 napi_status NapiMediaInfoHolder::SetPlayInfos(napi_env env, const MediaInfoHolder& in, napi_value& out)
112 {
113     napi_value property {};
114     auto status = NapiUtils::SetValue(env, in.GetPlayInfos(), property);
115     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
116     status = napi_set_named_property(env, out, "playInfos", property);
117     CHECK_RETURN(status == napi_ok, "set property failed", status);
118     return status;
119 }
120 }
121