• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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_meta_data.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, NapiMetaData::GetterType> NapiMetaData::getterMap_ = {
24     { "assetId", GetAssetId },
25     { "title", GetTitle },
26     { "artist", GetArtist },
27     { "author", GetAuthor },
28     { "avQueueName",  GetAVQueueName },
29     { "avQueueId",  GetAVQueueId },
30     { "avQueueImage",  GetAVQueueImage },
31     { "bundleIcon",  GetBundleIcon },
32     { "album",  GetAlbum },
33     { "writer", GetWriter },
34     { "composer", GetComposer },
35     { "duration", GetDuration },
36     { "mediaImage", GetMediaImage },
37     { "publishDate", GetPublishDate },
38     { "subtitle", GetSubtitle },
39     { "description", GetDescription },
40     { "lyric", GetLyric },
41     { "previousAssetId", GetPreviousAssetId },
42     { "nextAssetId", GetNextAssetId },
43     { "skipIntervals", GetSkipIntervals },
44     { "filter", GetFilter },
45     { "displayTags", GetDisplayTags },
46     { "drmSchemes", GetDrmSchemes },
47     { "singleLyricText", GetSingleLyricText }
48 };
49 
50 std::map<int32_t, NapiMetaData::SetterType> NapiMetaData::setterMap_ = {
51     { AVMetaData::META_KEY_ASSET_ID, SetAssetId },
52     { AVMetaData::META_KEY_TITLE, SetTitle },
53     { AVMetaData::META_KEY_ARTIST, SetArtist },
54     { AVMetaData::META_KEY_AUTHOR, SetAuthor },
55     { AVMetaData::META_KEY_AVQUEUE_NAME, SetAVQueueName },
56     { AVMetaData::META_KEY_AVQUEUE_ID, SetAVQueueId },
57     { AVMetaData::META_KEY_AVQUEUE_IMAGE, SetAVQueueImage },
58     { AVMetaData::META_KEY_AVQUEUE_IMAGE_URI, SetAVQueueImageUri },
59     { AVMetaData::META_KEY_ALBUM, SetAlbum },
60     { AVMetaData::META_KEY_WRITER, SetWriter },
61     { AVMetaData::META_KEY_COMPOSER, SetComposer },
62     { AVMetaData::META_KEY_DURATION, SetDuration },
63     { AVMetaData::META_KEY_MEDIA_IMAGE, SetMediaImage },
64     { AVMetaData::META_KEY_MEDIA_IMAGE_URI, SetMediaImageUri },
65     { AVMetaData::META_KEY_PUBLISH_DATE, SetPublishDate },
66     { AVMetaData::META_KEY_SUBTITLE, SetSubtitle },
67     { AVMetaData::META_KEY_DESCRIPTION, SetDescription },
68     { AVMetaData::META_KEY_LYRIC, SetLyric },
69     { AVMetaData::META_KEY_PREVIOUS_ASSET_ID, SetPreviousAssetId },
70     { AVMetaData::META_KEY_NEXT_ASSET_ID, SetNextAssetId },
71     { AVMetaData::META_KEY_SKIP_INTERVALS, SetSkipIntervals },
72     { AVMetaData::META_KEY_FILTER, SetFilter },
73     { AVMetaData::META_KEY_DISPLAY_TAGS, SetDisplayTags },
74     { AVMetaData::META_KEY_DRM_SCHEMES, SetDrmSchemes },
75     { AVMetaData::META_KEY_BUNDLE_ICON, SetBundleIcon },
76     { AVMetaData::META_KEY_SINGLE_LYRIC_TEXT, SetSingleLyricText }
77 };
78 
79 std::pair<std::string, int32_t> NapiMetaData::filterMap_[] = {
80     { "assetId", AVMetaData::META_KEY_ASSET_ID },
81     { "title", AVMetaData::META_KEY_TITLE },
82     { "artist", AVMetaData::META_KEY_ARTIST },
83     { "author", AVMetaData::META_KEY_AUTHOR },
84     { "avQueueName", AVMetaData::META_KEY_AVQUEUE_NAME },
85     { "avQueueId",  AVMetaData::META_KEY_AVQUEUE_ID },
86     { "avQueueImage", AVMetaData::META_KEY_AVQUEUE_IMAGE },
87     { "avQueueImage", AVMetaData::META_KEY_AVQUEUE_IMAGE_URI },
88     { "album",  AVMetaData::META_KEY_ALBUM },
89     { "writer", AVMetaData::META_KEY_WRITER },
90     { "composer", AVMetaData::META_KEY_COMPOSER },
91     { "duration", AVMetaData::META_KEY_DURATION },
92     { "mediaImage", AVMetaData::META_KEY_MEDIA_IMAGE },
93     { "mediaImage", AVMetaData::META_KEY_MEDIA_IMAGE_URI },
94     { "publishDate", AVMetaData::META_KEY_PUBLISH_DATE },
95     { "subtitle", AVMetaData::META_KEY_SUBTITLE },
96     { "description", AVMetaData::META_KEY_DESCRIPTION },
97     { "lyric", AVMetaData::META_KEY_LYRIC },
98     { "previousAssetId", AVMetaData::META_KEY_PREVIOUS_ASSET_ID },
99     { "nextAssetId", AVMetaData::META_KEY_NEXT_ASSET_ID },
100     { "skipIntervals", AVMetaData::META_KEY_SKIP_INTERVALS },
101     { "filter", AVMetaData::META_KEY_FILTER },
102     { "displayTags", AVMetaData::META_KEY_DISPLAY_TAGS },
103     { "drmSchemes", AVMetaData::META_KEY_DRM_SCHEMES },
104     { "bundleIcon", AVMetaData::META_KEY_BUNDLE_ICON },
105     { "singleLyricText", AVMetaData::META_KEY_SINGLE_LYRIC_TEXT }
106 };
107 
ConvertFilter(napi_env env,napi_value filter,AVMetaData::MetaMaskType & mask)108 napi_status NapiMetaData::ConvertFilter(napi_env env, napi_value filter, AVMetaData::MetaMaskType& mask)
109 {
110     napi_valuetype type = napi_undefined;
111     auto status = napi_typeof(env, filter, &type);
112     CHECK_RETURN(status == napi_ok, "napi_typeof failed", status);
113 
114     if (type == napi_string) {
115         std::string stringFilter;
116         status = NapiUtils::GetValue(env, filter, stringFilter);
117         CHECK_RETURN(status == napi_ok, "get string filter failed", status);
118         if (stringFilter != "all") {
119             SLOGE("string filter only support all") ;
120             return napi_invalid_arg;
121         }
122         mask.set();
123         return napi_ok;
124     }
125 
126     uint32_t count = 0;
127     status = napi_get_array_length(env, filter, &count);
128     CHECK_RETURN(status == napi_ok, "get array length failed", status);
129     for (uint32_t i = 0; i < count; i++) {
130         napi_value value {};
131         status = napi_get_element(env, filter, i, &value);
132         CHECK_RETURN(status == napi_ok, "get element failed", status);
133         std::string metaKey;
134         status = NapiUtils::GetValue(env, value, metaKey);
135         CHECK_RETURN(status == napi_ok, "get string value failed", status);
136         for (const auto& pair : filterMap_) {
137             if (pair.first == metaKey) {
138                 mask.set(pair.second);
139             }
140         }
141     }
142 
143     return napi_ok;
144 }
145 
GetValue(napi_env env,napi_value in,AVMetaData & out)146 napi_status NapiMetaData::GetValue(napi_env env, napi_value in, AVMetaData& out)
147 {
148     std::vector<std::string> propertyNames;
149     auto status = NapiUtils::GetPropertyNames(env, in, propertyNames);
150     CHECK_RETURN(status == napi_ok, "get property name failed", status);
151 
152     for (const auto& name : propertyNames) {
153         auto it = getterMap_.find(name);
154         if (it == getterMap_.end()) {
155             SLOGE("property %{public}s is not of metadata", name.c_str());
156             return napi_invalid_arg;
157         }
158         auto getter = it->second;
159         if (getter(env, in, out) != napi_ok) {
160             SLOGE("get property %{public}s failed", name.c_str());
161             return napi_generic_failure;
162         }
163     }
164 
165     return napi_ok;
166 }
167 
SetValue(napi_env env,const AVMetaData & in,napi_value & out)168 napi_status NapiMetaData::SetValue(napi_env env, const AVMetaData& in, napi_value& out)
169 {
170     napi_status status = napi_create_object(env, &out);
171     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
172 
173     auto mask = in.GetMetaMask();
174     if (mask.none()) {
175         SLOGI("undefined meta");
176         return SetUndefinedMeta(env, out);
177     }
178 
179     for (int i = 0; i < AVMetaData::META_KEY_MAX; ++i) {
180         if (!mask.test(i)) {
181             continue;
182         }
183         auto setter = setterMap_[i];
184         if (setter(env, in, out) != napi_ok) {
185             SLOGE("set property %{public}d failed", i);
186             return napi_generic_failure;
187         }
188     }
189 
190     return napi_ok;
191 }
192 
SetUndefinedMeta(napi_env env,napi_value & meta)193 napi_status NapiMetaData::SetUndefinedMeta(napi_env env, napi_value& meta)
194 {
195     auto status = napi_set_named_property(env, meta, "assetId", NapiUtils::GetUndefinedValue(env));
196     CHECK_RETURN(status == napi_ok, "set assetId to undefined failed", status);
197     return napi_ok;
198 }
199 
GetAssetId(napi_env env,napi_value in,AVMetaData & out)200 napi_status NapiMetaData::GetAssetId(napi_env env, napi_value in, AVMetaData& out)
201 {
202     std::string property;
203     auto status = NapiUtils::GetNamedProperty(env, in, "assetId", property);
204     CHECK_RETURN(status == napi_ok, "get property failed", status);
205     out.SetAssetId(property);
206     return status;
207 }
208 
SetAssetId(napi_env env,const AVMetaData & in,napi_value & out)209 napi_status NapiMetaData::SetAssetId(napi_env env, const AVMetaData& in, napi_value& out)
210 {
211     napi_value property {};
212     auto status = NapiUtils::SetValue(env, in.GetAssetId(), property);
213     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
214     status = napi_set_named_property(env, out, "assetId", property);
215     CHECK_RETURN(status == napi_ok, "set property failed", status);
216     return status;
217 }
218 
GetTitle(napi_env env,napi_value in,AVMetaData & out)219 napi_status NapiMetaData::GetTitle(napi_env env, napi_value in, AVMetaData& out)
220 {
221     std::string property;
222     auto status = NapiUtils::GetNamedProperty(env, in, "title", property);
223     CHECK_RETURN(status == napi_ok, "get property failed", status);
224     out.SetTitle(property);
225     return status;
226 }
227 
SetTitle(napi_env env,const AVMetaData & in,napi_value & out)228 napi_status NapiMetaData::SetTitle(napi_env env, const AVMetaData& in, napi_value& out)
229 {
230     napi_value property {};
231     auto status = NapiUtils::SetValue(env, in.GetTitle(), property);
232     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
233     status = napi_set_named_property(env, out, "title", property);
234     CHECK_RETURN(status == napi_ok, "set property failed", status);
235     return status;
236 }
237 
GetArtist(napi_env env,napi_value in,AVMetaData & out)238 napi_status NapiMetaData::GetArtist(napi_env env, napi_value in, AVMetaData& out)
239 {
240     std::string property;
241     auto status = NapiUtils::GetNamedProperty(env, in, "artist", property);
242     CHECK_RETURN(status == napi_ok, "get property failed", status);
243     out.SetArtist(property);
244     return status;
245 }
246 
SetArtist(napi_env env,const AVMetaData & in,napi_value & out)247 napi_status NapiMetaData::SetArtist(napi_env env, const AVMetaData& in, napi_value& out)
248 {
249     napi_value property {};
250     auto status = NapiUtils::SetValue(env, in.GetArtist(), property);
251     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
252     status = napi_set_named_property(env, out, "artist", property);
253     CHECK_RETURN(status == napi_ok, "set property failed", status);
254     return status;
255 }
256 
GetAuthor(napi_env env,napi_value in,AVMetaData & out)257 napi_status NapiMetaData::GetAuthor(napi_env env, napi_value in, AVMetaData& out)
258 {
259     std::string property;
260     auto status = NapiUtils::GetNamedProperty(env, in, "author", property);
261     CHECK_RETURN(status == napi_ok, "get property failed", status);
262     out.SetAuthor(property);
263     return status;
264 }
265 
SetAuthor(napi_env env,const AVMetaData & in,napi_value & out)266 napi_status NapiMetaData::SetAuthor(napi_env env, const AVMetaData& in, napi_value& out)
267 {
268     napi_value property {};
269     auto status = NapiUtils::SetValue(env, in.GetAuthor(), property);
270     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
271     status = napi_set_named_property(env, out, "author", property);
272     CHECK_RETURN(status == napi_ok, "set property failed", status);
273     return status;
274 }
275 
GetAVQueueName(napi_env env,napi_value in,AVMetaData & out)276 napi_status NapiMetaData::GetAVQueueName(napi_env env, napi_value in, AVMetaData& out)
277 {
278     std::string property;
279     auto status = NapiUtils::GetNamedProperty(env, in, "avQueueName", property);
280     CHECK_RETURN(status == napi_ok, "get property failed", status);
281     out.SetAVQueueName(property);
282     return status;
283 }
284 
SetAVQueueName(napi_env env,const AVMetaData & in,napi_value & out)285 napi_status NapiMetaData::SetAVQueueName(napi_env env, const AVMetaData& in, napi_value& out)
286 {
287     napi_value property {};
288     auto status = NapiUtils::SetValue(env, in.GetAVQueueName(), property);
289     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
290     status = napi_set_named_property(env, out, "avQueueName", property);
291     CHECK_RETURN(status == napi_ok, "set property failed", status);
292     return status;
293 }
294 
GetAVQueueId(napi_env env,napi_value in,AVMetaData & out)295 napi_status NapiMetaData::GetAVQueueId(napi_env env, napi_value in, AVMetaData& out)
296 {
297     std::string property;
298     auto status = NapiUtils::GetNamedProperty(env, in, "avQueueId", property);
299     CHECK_RETURN(status == napi_ok, "get property failed", status);
300     out.SetAVQueueId(property);
301     return status;
302 }
303 
SetAVQueueId(napi_env env,const AVMetaData & in,napi_value & out)304 napi_status NapiMetaData::SetAVQueueId(napi_env env, const AVMetaData& in, napi_value& out)
305 {
306     napi_value property {};
307     auto status = NapiUtils::SetValue(env, in.GetAVQueueId(), property);
308     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
309     status = napi_set_named_property(env, out, "avQueueId", property);
310     CHECK_RETURN(status == napi_ok, "set property failed", status);
311     return status;
312 }
313 
GetAVQueueImage(napi_env env,napi_value in,AVMetaData & out)314 napi_status NapiMetaData::GetAVQueueImage(napi_env env, napi_value in, AVMetaData& out)
315 {
316     napi_value property {};
317     auto status = napi_get_named_property(env, in, "avQueueImage", &property);
318     CHECK_RETURN((status == napi_ok) && (property != nullptr), "get property failed", status);
319     napi_valuetype type = napi_undefined;
320     status = napi_typeof(env, property, &type);
321     CHECK_RETURN(status == napi_ok, "get napi_value type failed", status);
322     if (type == napi_string) {
323         std::string uri;
324         status = NapiUtils::GetValue(env, property, uri);
325         CHECK_RETURN(status == napi_ok, "get property failed", status);
326         out.SetAVQueueImageUri(uri);
327     } else if (type == napi_object) {
328         auto pixelMap = Media::PixelMapNapi::GetPixelMap(env, property);
329         if (pixelMap == nullptr) {
330             SLOGE("unwrap avqueue pixelMap failed");
331             return napi_invalid_arg;
332         }
333         out.SetAVQueueImage(AVSessionPixelMapAdapter::ConvertToInner(pixelMap));
334         SLOGD(" napi get avqueueimage");
335     } else {
336         SLOGE("avqueueimage property value type invalid");
337         return napi_invalid_arg;
338     }
339 
340     return status;
341 }
342 
SetAVQueueImage(napi_env env,const AVMetaData & in,napi_value & out)343 napi_status NapiMetaData::SetAVQueueImage(napi_env env, const AVMetaData& in, napi_value& out)
344 {
345     auto pixelMap = in.GetAVQueueImage();
346     if (pixelMap == nullptr) {
347         SLOGI("avqueue image is none");
348         return napi_ok;
349     }
350 
351     napi_value property = Media::PixelMapNapi::CreatePixelMap(env,
352         AVSessionPixelMapAdapter::ConvertFromInner(pixelMap));
353     auto status = napi_set_named_property(env, out, "avQueueImage", property);
354     CHECK_RETURN(status == napi_ok, "set property failed", status);
355     return status;
356 }
357 
GetBundleIcon(napi_env env,napi_value in,AVMetaData & out)358 napi_status NapiMetaData::GetBundleIcon(napi_env env, napi_value in, AVMetaData& out)
359 {
360     SLOGE("bundle icon readonly, can't be set");
361     return napi_ok;
362 }
363 
SetBundleIcon(napi_env env,const AVMetaData & in,napi_value & out)364 napi_status NapiMetaData::SetBundleIcon(napi_env env, const AVMetaData& in, napi_value& out)
365 {
366     auto pixelMap = in.GetBundleIcon();
367     if (pixelMap == nullptr) {
368         SLOGI("bundle icon is none");
369         return napi_ok;
370     }
371 
372     napi_value property = Media::PixelMapNapi::CreatePixelMap(env,
373         AVSessionPixelMapAdapter::ConvertFromInner(pixelMap));
374     auto status = napi_set_named_property(env, out, "bundleIcon", property);
375     CHECK_RETURN(status == napi_ok, "set bundleIcon property failed", status);
376     return status;
377 }
378 
SetAVQueueImageUri(napi_env env,const AVMetaData & in,napi_value & out)379 napi_status NapiMetaData::SetAVQueueImageUri(napi_env env, const AVMetaData& in, napi_value& out)
380 {
381     auto uri = in.GetAVQueueImageUri();
382     if (uri.empty()) {
383         SLOGI("avqueue image uri empty");
384         return napi_ok;
385     }
386 
387     napi_value property {};
388     auto status = NapiUtils::SetValue(env, uri, property);
389     CHECK_RETURN(status == napi_ok, "create property failed", status);
390     status = napi_set_named_property(env, out, "avQueueImage", property);
391     CHECK_RETURN(status == napi_ok, "set property failed", status);
392     return status;
393 }
394 
GetAlbum(napi_env env,napi_value in,AVMetaData & out)395 napi_status NapiMetaData::GetAlbum(napi_env env, napi_value in, AVMetaData& out)
396 {
397     std::string property;
398     auto status = NapiUtils::GetNamedProperty(env, in, "album", property);
399     CHECK_RETURN(status == napi_ok, "get property failed", status);
400     out.SetAlbum(property);
401     return status;
402 }
403 
SetAlbum(napi_env env,const AVMetaData & in,napi_value & out)404 napi_status NapiMetaData::SetAlbum(napi_env env, const AVMetaData& in, napi_value& out)
405 {
406     napi_value property {};
407     auto status = NapiUtils::SetValue(env, in.GetAlbum(), property);
408     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
409     status = napi_set_named_property(env, out, "album", property);
410     CHECK_RETURN(status == napi_ok, "set property failed", status);
411     return status;
412 }
413 
GetWriter(napi_env env,napi_value in,AVMetaData & out)414 napi_status NapiMetaData::GetWriter(napi_env env, napi_value in, AVMetaData& out)
415 {
416     std::string property;
417     auto status = NapiUtils::GetNamedProperty(env, in, "writer", property);
418     CHECK_RETURN(status == napi_ok, "get property failed", status);
419     out.SetWriter(property);
420     return status;
421 }
422 
SetWriter(napi_env env,const AVMetaData & in,napi_value & out)423 napi_status NapiMetaData::SetWriter(napi_env env, const AVMetaData& in, napi_value& out)
424 {
425     napi_value property {};
426     auto status = NapiUtils::SetValue(env, in.GetWriter(), property);
427     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
428     status = napi_set_named_property(env, out, "writer", property);
429     CHECK_RETURN(status == napi_ok, "set property failed", status);
430     return status;
431 }
432 
GetComposer(napi_env env,napi_value in,AVMetaData & out)433 napi_status NapiMetaData::GetComposer(napi_env env, napi_value in, AVMetaData& out)
434 {
435     std::string property;
436     auto status = NapiUtils::GetNamedProperty(env, in, "composer", property);
437     CHECK_RETURN(status == napi_ok, "get property failed", status);
438     out.SetComposer(property);
439     return status;
440 }
441 
SetComposer(napi_env env,const AVMetaData & in,napi_value & out)442 napi_status NapiMetaData::SetComposer(napi_env env, const AVMetaData& in, napi_value& out)
443 {
444     napi_value property {};
445     auto status = NapiUtils::SetValue(env, in.GetComposer(), property);
446     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
447     status = napi_set_named_property(env, out, "composer", property);
448     CHECK_RETURN(status == napi_ok, "set property failed", status);
449     return status;
450 }
451 
GetDuration(napi_env env,napi_value in,AVMetaData & out)452 napi_status NapiMetaData::GetDuration(napi_env env, napi_value in, AVMetaData& out)
453 {
454     int64_t property {};
455     auto status = NapiUtils::GetNamedProperty(env, in, "duration", property);
456     CHECK_RETURN(status == napi_ok, "get property failed", status);
457     out.SetDuration(property);
458     return status;
459 }
460 
SetDuration(napi_env env,const AVMetaData & in,napi_value & out)461 napi_status NapiMetaData::SetDuration(napi_env env, const AVMetaData& in, napi_value& out)
462 {
463     napi_value property {};
464     auto status = NapiUtils::SetValue(env, in.GetDuration(), property);
465     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
466     status = napi_set_named_property(env, out, "duration", property);
467     CHECK_RETURN(status == napi_ok, "set property failed", status);
468     return status;
469 }
470 
GetMediaImage(napi_env env,napi_value in,AVMetaData & out)471 napi_status NapiMetaData::GetMediaImage(napi_env env, napi_value in, AVMetaData& out)
472 {
473     napi_value property {};
474     auto status = napi_get_named_property(env, in, "mediaImage", &property);
475     CHECK_RETURN((status == napi_ok) && (property != nullptr), "get property failed", status);
476     napi_valuetype type = napi_undefined;
477     status = napi_typeof(env, property, &type);
478     CHECK_RETURN(status == napi_ok, "get napi_value type failed", status);
479     if (type == napi_string) {
480         std::string uri;
481         status = NapiUtils::GetValue(env, property, uri);
482         CHECK_RETURN(status == napi_ok, "get property failed", status);
483         out.SetMediaImageUri(uri);
484     } else if (type == napi_object) {
485         auto pixelMap = Media::PixelMapNapi::GetPixelMap(env, property);
486         if (pixelMap == nullptr) {
487             SLOGE("unwrap failed");
488             return napi_invalid_arg;
489         }
490         SLOGI("set mediaImage without small size");
491         out.SetMediaImage(AVSessionPixelMapAdapter::ConvertToInner(pixelMap));
492     } else {
493         SLOGE("mediaImage property value type invalid");
494         return napi_invalid_arg;
495     }
496 
497     return status;
498 }
499 
SetMediaImage(napi_env env,const AVMetaData & in,napi_value & out)500 napi_status NapiMetaData::SetMediaImage(napi_env env, const AVMetaData& in, napi_value& out)
501 {
502     auto pixelMap = in.GetMediaImage();
503     if (pixelMap == nullptr) {
504         SLOGI("media image is none");
505         return napi_ok;
506     }
507 
508     napi_value property = Media::PixelMapNapi::CreatePixelMap(env,
509         AVSessionPixelMapAdapter::ConvertFromInner(pixelMap));
510     auto status = napi_set_named_property(env, out, "mediaImage", property);
511     CHECK_RETURN(status == napi_ok, "set property failed", status);
512     return status;
513 }
514 
SetMediaImageUri(napi_env env,const AVMetaData & in,napi_value & out)515 napi_status NapiMetaData::SetMediaImageUri(napi_env env, const AVMetaData& in, napi_value& out)
516 {
517     auto uri = in.GetMediaImageUri();
518     if (uri.empty()) {
519         SLOGI("media image uri empty");
520         return napi_ok;
521     }
522 
523     napi_value property {};
524     auto status = NapiUtils::SetValue(env, uri, property);
525     CHECK_RETURN(status == napi_ok, "create property failed", status);
526     status = napi_set_named_property(env, out, "mediaImage", property);
527     CHECK_RETURN(status == napi_ok, "set property failed", status);
528     return status;
529 }
530 
GetPublishDate(napi_env env,napi_value in,AVMetaData & out)531 napi_status NapiMetaData::GetPublishDate(napi_env env, napi_value in, AVMetaData& out)
532 {
533     napi_value property;
534     auto status = napi_get_named_property(env, in, "publishDate", &property);
535     CHECK_RETURN(status == napi_ok, "get property failed", status);
536     double date {};
537     status = NapiUtils::GetDateValue(env, property, date);
538     CHECK_RETURN(status == napi_ok, "get date value failed", status);
539     out.SetPublishDate(date);
540     return status;
541 }
542 
SetPublishDate(napi_env env,const AVMetaData & in,napi_value & out)543 napi_status NapiMetaData::SetPublishDate(napi_env env, const AVMetaData& in, napi_value& out)
544 {
545     napi_value property {};
546     auto status = NapiUtils::SetDateValue(env, in.GetPublishDate(), property);
547     CHECK_RETURN(status == napi_ok, "create date object failed", status);
548     status = napi_set_named_property(env, out, "publishDate", property);
549     CHECK_RETURN(status == napi_ok, "set property failed", status);
550     return status;
551 }
552 
GetSubtitle(napi_env env,napi_value in,AVMetaData & out)553 napi_status NapiMetaData::GetSubtitle(napi_env env, napi_value in, AVMetaData& out)
554 {
555     std::string property;
556     auto status = NapiUtils::GetNamedProperty(env, in, "subtitle", property);
557     CHECK_RETURN(status == napi_ok, "get property failed", status);
558     out.SetSubTitle(property);
559     return status;
560 }
561 
SetSubtitle(napi_env env,const AVMetaData & in,napi_value & out)562 napi_status NapiMetaData::SetSubtitle(napi_env env, const AVMetaData& in, napi_value& out)
563 {
564     napi_value property {};
565     auto status = NapiUtils::SetValue(env, in.GetSubTitle(), property);
566     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
567     status = napi_set_named_property(env, out, "subtitle", property);
568     CHECK_RETURN(status == napi_ok, "set property failed", status);
569     return status;
570 }
571 
GetDescription(napi_env env,napi_value in,AVMetaData & out)572 napi_status NapiMetaData::GetDescription(napi_env env, napi_value in, AVMetaData& out)
573 {
574     std::string property;
575     auto status = NapiUtils::GetNamedProperty(env, in, "description", property);
576     CHECK_RETURN(status == napi_ok, "get property failed", status);
577     out.SetDescription(property);
578     return status;
579 }
580 
SetDescription(napi_env env,const AVMetaData & in,napi_value & out)581 napi_status NapiMetaData::SetDescription(napi_env env, const AVMetaData& in, napi_value& out)
582 {
583     napi_value property {};
584     auto status = NapiUtils::SetValue(env, in.GetDescription(), property);
585     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
586     status = napi_set_named_property(env, out, "description", property);
587     CHECK_RETURN(status == napi_ok, "set property failed", status);
588     return status;
589 }
590 
GetLyric(napi_env env,napi_value in,AVMetaData & out)591 napi_status NapiMetaData::GetLyric(napi_env env, napi_value in, AVMetaData& out)
592 {
593     std::string property;
594     auto status = NapiUtils::GetNamedProperty(env, in, "lyric", property);
595     CHECK_RETURN(status == napi_ok, "get property failed", status);
596     out.SetLyric(property);
597     return status;
598 }
599 
SetLyric(napi_env env,const AVMetaData & in,napi_value & out)600 napi_status NapiMetaData::SetLyric(napi_env env, const AVMetaData& in, napi_value& out)
601 {
602     napi_value property {};
603     auto status = NapiUtils::SetValue(env, in.GetLyric(), property);
604     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
605     status = napi_set_named_property(env, out, "lyric", property);
606     CHECK_RETURN(status == napi_ok, "set property failed", status);
607     return status;
608 }
609 
GetSingleLyricText(napi_env env,napi_value in,AVMetaData & out)610 napi_status NapiMetaData::GetSingleLyricText(napi_env env, napi_value in, AVMetaData& out)
611 {
612     std::string property;
613     auto status = NapiUtils::GetNamedProperty(env, in, "singleLyricText", property);
614     CHECK_RETURN(status == napi_ok, "get property failed", status);
615     out.SetSingleLyricText(property);
616     return status;
617 }
618 
SetSingleLyricText(napi_env env,const AVMetaData & in,napi_value & out)619 napi_status NapiMetaData::SetSingleLyricText(napi_env env, const AVMetaData& in, napi_value& out)
620 {
621     napi_value property {};
622     auto status = NapiUtils::SetValue(env, in.GetSingleLyricText(), property);
623     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
624     status = napi_set_named_property(env, out, "singleLyricText", property);
625     CHECK_RETURN(status == napi_ok, "set property failed", status);
626     return status;
627 }
628 
GetPreviousAssetId(napi_env env,napi_value in,AVMetaData & out)629 napi_status NapiMetaData::GetPreviousAssetId(napi_env env, napi_value in, AVMetaData& out)
630 {
631     std::string property;
632     auto status = NapiUtils::GetNamedProperty(env, in, "previousAssetId", property);
633     CHECK_RETURN(status == napi_ok, "get property failed", status);
634     out.SetPreviousAssetId(property);
635     return status;
636 }
637 
SetPreviousAssetId(napi_env env,const AVMetaData & in,napi_value & out)638 napi_status NapiMetaData::SetPreviousAssetId(napi_env env, const AVMetaData& in, napi_value& out)
639 {
640     napi_value property {};
641     auto status = NapiUtils::SetValue(env, in.GetPreviousAssetId(), property);
642     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
643     status = napi_set_named_property(env, out, "previousAssetId", property);
644     CHECK_RETURN(status == napi_ok, "set property failed", status);
645     return status;
646 }
647 
GetNextAssetId(napi_env env,napi_value in,AVMetaData & out)648 napi_status NapiMetaData::GetNextAssetId(napi_env env, napi_value in, AVMetaData& out)
649 {
650     std::string property;
651     auto status = NapiUtils::GetNamedProperty(env, in, "nextAssetId", property);
652     CHECK_RETURN(status == napi_ok, "get property failed", status);
653     out.SetNextAssetId(property);
654     return status;
655 }
656 
SetNextAssetId(napi_env env,const AVMetaData & in,napi_value & out)657 napi_status NapiMetaData::SetNextAssetId(napi_env env, const AVMetaData& in, napi_value& out)
658 {
659     napi_value property {};
660     auto status = NapiUtils::SetValue(env, in.GetNextAssetId(), property);
661     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
662     status = napi_set_named_property(env, out, "nextAssetId", property);
663     CHECK_RETURN(status == napi_ok, "set property failed", status);
664     return status;
665 }
666 
GetSkipIntervals(napi_env env,napi_value in,AVMetaData & out)667 napi_status NapiMetaData::GetSkipIntervals(napi_env env, napi_value in, AVMetaData& out)
668 {
669     int32_t property {};
670     auto status = NapiUtils::GetNamedProperty(env, in, "skipIntervals", property);
671     CHECK_RETURN(status == napi_ok, "get property failed", status);
672     SLOGD("GetSkipIntervals %{public}d", static_cast<int32_t>(property));
673     out.SetSkipIntervals(property);
674     return status;
675 }
676 
SetSkipIntervals(napi_env env,const AVMetaData & in,napi_value & out)677 napi_status NapiMetaData::SetSkipIntervals(napi_env env, const AVMetaData& in, napi_value& out)
678 {
679     napi_value property {};
680     SLOGD("SetSkipIntervals %{public}d", static_cast<int32_t>(in.GetSkipIntervals()));
681     auto status = NapiUtils::SetValue(env, in.GetSkipIntervals(), property);
682     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
683     status = napi_set_named_property(env, out, "skipIntervals", property);
684     CHECK_RETURN(status == napi_ok, "set property failed", status);
685     return status;
686 }
687 
GetFilter(napi_env env,napi_value in,AVMetaData & out)688 napi_status NapiMetaData::GetFilter(napi_env env, napi_value in, AVMetaData& out)
689 {
690     int32_t property {};
691     auto status = NapiUtils::GetNamedProperty(env, in, "filter", property);
692     CHECK_RETURN(status == napi_ok, "get property failed", status);
693     SLOGD("GetFilter %{public}d", static_cast<int32_t>(property));
694     out.SetFilter(property);
695     return status;
696 }
697 
SetFilter(napi_env env,const AVMetaData & in,napi_value & out)698 napi_status NapiMetaData::SetFilter(napi_env env, const AVMetaData& in, napi_value& out)
699 {
700     napi_value property {};
701     SLOGD("SetFilter %{public}d", static_cast<int32_t>(in.GetFilter()));
702     auto status = NapiUtils::SetValue(env, in.GetFilter(), property);
703     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
704     status = napi_set_named_property(env, out, "filter", property);
705     CHECK_RETURN(status == napi_ok, "set property failed", status);
706     return status;
707 }
708 
GetDisplayTags(napi_env env,napi_value in,AVMetaData & out)709 napi_status NapiMetaData::GetDisplayTags(napi_env env, napi_value in, AVMetaData& out)
710 {
711     int32_t property {};
712     auto status = NapiUtils::GetNamedProperty(env, in, "displayTags", property);
713     CHECK_RETURN(status == napi_ok, "get property displayTags failed", status);
714     SLOGD("GetDisplayTags %{public}d", static_cast<int32_t>(property));
715     out.SetDisplayTags(property);
716     return status;
717 }
718 
SetDisplayTags(napi_env env,const AVMetaData & in,napi_value & out)719 napi_status NapiMetaData::SetDisplayTags(napi_env env, const AVMetaData& in, napi_value& out)
720 {
721     napi_value property {};
722     SLOGD("SetDisplayTags %{public}d", static_cast<int32_t>(in.GetDisplayTags()));
723     auto status = NapiUtils::SetValue(env, in.GetDisplayTags(), property);
724     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property displayTags failed", status);
725     status = napi_set_named_property(env, out, "displayTags", property);
726     CHECK_RETURN(status == napi_ok, "set property displayTags failed", status);
727     return status;
728 }
729 
GetDrmSchemes(napi_env env,napi_value in,AVMetaData & out)730 napi_status NapiMetaData::GetDrmSchemes(napi_env env, napi_value in, AVMetaData& out)
731 {
732     std::vector<std::string> property;
733     auto status = NapiUtils::GetNamedProperty(env, in, "drmSchemes", property);
734     CHECK_RETURN(status == napi_ok, "get property drmSchemes failed", status);
735     out.SetDrmSchemes(property);
736     SLOGI("property %{public}zu", property.size());
737     return status;
738 }
739 
SetDrmSchemes(napi_env env,const AVMetaData & in,napi_value & out)740 napi_status NapiMetaData::SetDrmSchemes(napi_env env, const AVMetaData& in, napi_value& out)
741 {
742     napi_value property {};
743     SLOGD("SetDrmSchemes in, drmSchemes len: %{public}zu", in.GetDrmSchemes().size());
744     auto status = NapiUtils::SetValue(env, in.GetDrmSchemes(), property);
745     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property drmSchemes failed", status);
746     status = napi_set_named_property(env, out, "drmSchemes", property);
747     CHECK_RETURN(status == napi_ok, "set property drmSchemes failed", status);
748     return status;
749 }
750 }
751