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