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 #define LOG_TAG "VideoNapi"
16 #include "video_napi.h"
17
18 #include "file_napi.h"
19 #include "unified_record_napi.h"
20 #include "video.h"
21
22 namespace OHOS {
23 namespace UDMF {
Constructor(napi_env env)24 napi_value VideoNapi::Constructor(napi_env env)
25 {
26 LOG_DEBUG(UDMF_KITS_NAPI, "VideoNapi");
27 napi_property_descriptor properties[] = {
28 /* Video extends UnifiedRecord */
29 DECLARE_NAPI_FUNCTION("getType", UnifiedRecordNapi::GetType),
30 DECLARE_NAPI_FUNCTION("getValue", UnifiedRecordNapi::GetValue),
31 /* Video extends File */
32 DECLARE_NAPI_GETTER_SETTER("details", FileNapi::GetDetails, FileNapi::SetDetails),
33 DECLARE_NAPI_GETTER_SETTER("uri", FileNapi::GetUri, FileNapi::SetUri),
34 /* Video properties */
35 DECLARE_NAPI_GETTER_SETTER("videoUri", GetVideoUri, SetVideoUri),
36 };
37 size_t count = sizeof(properties) / sizeof(properties[0]);
38 return NapiDataUtils::DefineClass(env, "Video", properties, count, VideoNapi::New);
39 }
40
New(napi_env env,napi_callback_info info)41 napi_value VideoNapi::New(napi_env env, napi_callback_info info)
42 {
43 LOG_DEBUG(UDMF_KITS_NAPI, "VideoNapi");
44 auto ctxt = std::make_shared<ContextBase>();
45 ctxt->GetCbInfoSync(env, info);
46 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
47
48 auto *video = new (std::nothrow) VideoNapi();
49 ASSERT_ERR(ctxt->env, video != nullptr, Status::E_ERROR, "no memory for video!");
50 video->value_ = std::make_shared<Video>();
51 ASSERT_CALL(env, napi_wrap(env, ctxt->self, video, Destructor, nullptr, nullptr), video);
52 return ctxt->self;
53 }
54
NewInstance(napi_env env,std::shared_ptr<UnifiedRecord> in,napi_value & out)55 void VideoNapi::NewInstance(napi_env env, std::shared_ptr<UnifiedRecord> in, napi_value &out)
56 {
57 LOG_DEBUG(UDMF_KITS_NAPI, "VideoNapi");
58 ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
59 auto *video = new (std::nothrow) VideoNapi();
60 ASSERT_ERR_VOID(env, video != nullptr, Status::E_ERROR, "no memory for video!");
61 video->value_ = std::static_pointer_cast<Video>(in);
62 ASSERT_CALL_DELETE(env, napi_wrap(env, out, video, Destructor, nullptr, nullptr), video);
63 }
64
Destructor(napi_env env,void * data,void * hint)65 void VideoNapi::Destructor(napi_env env, void *data, void *hint)
66 {
67 LOG_DEBUG(UDMF_KITS_NAPI, "VideoNapi finalize.");
68 auto *video = static_cast<VideoNapi *>(data);
69 ASSERT_VOID(video != nullptr, "finalize null!");
70 delete video;
71 }
72
GetVideo(napi_env env,napi_callback_info info,std::shared_ptr<ContextBase> ctxt)73 VideoNapi *VideoNapi::GetVideo(napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
74 {
75 LOG_DEBUG(UDMF_KITS_NAPI, "VideoNapi");
76 ctxt->GetCbInfoSync(env, info);
77 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
78 return static_cast<VideoNapi *>(ctxt->native);
79 }
80
GetVideoUri(napi_env env,napi_callback_info info)81 napi_value VideoNapi::GetVideoUri(napi_env env, napi_callback_info info)
82 {
83 LOG_DEBUG(UDMF_KITS_NAPI, "VideoNapi");
84 auto ctxt = std::make_shared<ContextBase>();
85 auto video = GetVideo(env, info, ctxt);
86 ASSERT_ERR(
87 ctxt->env, (video != nullptr && video->value_ != nullptr), Status::E_ERROR, "invalid object!");
88 ctxt->status = NapiDataUtils::SetValue(env, video->value_->GetUri(), ctxt->output);
89 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set video uri failed!");
90 return ctxt->output;
91 }
92
SetVideoUri(napi_env env,napi_callback_info info)93 napi_value VideoNapi::SetVideoUri(napi_env env, napi_callback_info info)
94 {
95 LOG_DEBUG(UDMF_KITS_NAPI, "VideoNapi");
96 auto ctxt = std::make_shared<ContextBase>();
97 std::string uri;
98 auto input = [env, ctxt, &uri](size_t argc, napi_value *argv) {
99 ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
100 Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
101 ctxt->status = NapiDataUtils::GetValue(env, argv[0], uri);
102 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
103 Status::E_INVALID_PARAMETERS, "Parameter error: parameter uri type must be string");
104 };
105 ctxt->GetCbInfoSync(env, info, input);
106 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
107 auto video = static_cast<VideoNapi *>(ctxt->native);
108 ASSERT_ERR(
109 ctxt->env, (video != nullptr && video->value_ != nullptr), Status::E_ERROR, "invalid object!");
110 video->value_->SetUri(uri);
111 return nullptr;
112 }
113 } // namespace UDMF
114 } // namespace OHOS