1 /*
2  * Copyright (C) 2024 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 "media_source_napi.h"
17 #include "media_log.h"
18 
19 namespace {
20 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "MediaSourceNapi"};
21 }
22 
23 namespace OHOS {
24 namespace Media {
25 const std::string CLASS_NAME = "MediaSource";
26 thread_local napi_ref MediaSourceNapi::constructor_ = nullptr;
27 
Init(napi_env env,napi_value exports)28 napi_value MediaSourceNapi::Init(napi_env env, napi_value exports)
29 {
30     napi_property_descriptor staticProperty[] = {
31         DECLARE_NAPI_STATIC_FUNCTION("createMediaSourceWithUrl", JsCreateMediaSourceWithUrl),
32     };
33 
34     napi_property_descriptor properties[] = {
35         DECLARE_NAPI_FUNCTION("setMimeType", JsSetMimeType),
36     };
37 
38     napi_value constructor = nullptr;
39     napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr,
40         sizeof(properties) / sizeof(properties[0]), properties, &constructor);
41     CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define AVPlayer class");
42 
43     status = napi_create_reference(env, constructor, 1, &constructor_);
44     CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to create reference of constructor");
45 
46     status = napi_set_named_property(env, exports, CLASS_NAME.c_str(), constructor);
47     CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to set constructor");
48 
49     status = napi_define_properties(env, exports, sizeof(staticProperty) / sizeof(staticProperty[0]), staticProperty);
50     CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define static function");
51     return exports;
52 }
53 
GetMediaSource(napi_env env,napi_value jsMediaSource)54 std::shared_ptr<AVMediaSourceTmp> MediaSourceNapi::GetMediaSource(napi_env env, napi_value jsMediaSource)
55 {
56     MediaSourceNapi *mediaSource = nullptr;
57     napi_status status = napi_unwrap(env, jsMediaSource, reinterpret_cast<void **>(&mediaSource));
58     CHECK_AND_RETURN_RET_LOG(status == napi_ok && mediaSource != nullptr, nullptr, "failed to napi_unwrap");
59 
60     return mediaSource->mediaSource_;
61 }
62 
Constructor(napi_env env,napi_callback_info info)63 napi_value MediaSourceNapi::Constructor(napi_env env, napi_callback_info info)
64 {
65     napi_value result = nullptr;
66     napi_get_undefined(env, &result);
67 
68     size_t argCount = 0;
69     napi_value jsThis = nullptr;
70     napi_status status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
71     CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "failed to napi_get_cb_info");
72 
73     MediaSourceNapi *jsMediaSource = new(std::nothrow) MediaSourceNapi();
74     CHECK_AND_RETURN_RET_LOG(jsMediaSource != nullptr, result, "failed to new MediaSourceNapi");
75 
76     jsMediaSource->env_ = env;
77     jsMediaSource->mediaSource_ = std::make_shared<AVMediaSourceTmp>();
78     if (jsMediaSource->mediaSource_ == nullptr) {
79         delete jsMediaSource;
80         MEDIA_LOGE("Failed to Create MediaSource");
81         return result;
82     }
83 
84     status = napi_wrap(env, jsThis, reinterpret_cast<void *>(jsMediaSource),
85         MediaSourceNapi::Destructor, nullptr, nullptr);
86     if (status != napi_ok) {
87         delete jsMediaSource;
88         MEDIA_LOGE("Failed to wrap native instance");
89         return result;
90     }
91 
92     MEDIA_LOGI("0x%{public}06" PRIXPTR " Constructor success", FAKE_POINTER(jsMediaSource));
93     return jsThis;
94 }
95 
Destructor(napi_env env,void * nativeObject,void * finalize)96 void MediaSourceNapi::Destructor(napi_env env, void *nativeObject, void *finalize)
97 {
98     (void)env;
99     (void)finalize;
100     if (nativeObject != nullptr) {
101         MediaSourceNapi *jsMediaSource = reinterpret_cast<MediaSourceNapi *>(nativeObject);
102         jsMediaSource->mediaSource_ = nullptr;
103         delete jsMediaSource;
104     }
105     MEDIA_LOGI("Destructor success");
106 }
107 
JsCreateMediaSourceWithUrl(napi_env env,napi_callback_info info)108 napi_value MediaSourceNapi::JsCreateMediaSourceWithUrl(napi_env env, napi_callback_info info)
109 {
110     MEDIA_LOGD("JsCreateMediaSourceWithUrl In");
111     size_t argCount = 2;
112     napi_value args[2] = { nullptr };
113     napi_value jsMediaSource = nullptr;
114     napi_get_undefined(env, &jsMediaSource);
115     napi_status status = napi_get_cb_info(env, info, &argCount, args, nullptr, nullptr);
116     CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "failed to napi_get_cb_info");
117 
118     napi_valuetype valueType = napi_undefined;
119     if (argCount < 1 || napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_string) {
120         return nullptr;
121     }
122 
123     napi_value constructor = nullptr;
124     napi_status ret = napi_get_reference_value(env, constructor_, &constructor);
125     if (ret != napi_ok || constructor == nullptr) {
126         return nullptr;
127     }
128     napi_new_instance(env, constructor, 0, nullptr, &jsMediaSource);
129 
130     std::shared_ptr<AVMediaSourceTmp> mediaSource = GetMediaSource(env, jsMediaSource);
131     if (mediaSource == nullptr) {
132         MEDIA_LOGE("JsCreateMediaSourceWithUrl GetMediaSource fail");
133         return nullptr;
134     }
135     mediaSource->url = CommonNapi::GetStringArgument(env, args[0]);
136     MEDIA_LOGE("JsCreateMediaSourceWithUrl get map");
137     (void)CommonNapi::GetPropertyMap(env, args[1], mediaSource->header);
138     return jsMediaSource;
139 }
140 
JsSetMimeType(napi_env env,napi_callback_info info)141 napi_value MediaSourceNapi::JsSetMimeType(napi_env env, napi_callback_info info)
142 {
143     MEDIA_LOGI("JsSetMimeType In");
144     napi_value undefinedResult = nullptr;
145     napi_get_undefined(env, &undefinedResult);
146     size_t argCount = 1;
147     napi_value args[1] = {nullptr};
148     napi_value jsThis = nullptr;
149     napi_status status = napi_get_cb_info(env, info, &argCount, args, &jsThis, nullptr);
150     CHECK_AND_RETURN_RET_LOG(status == napi_ok, undefinedResult, "failed to napi_get_cb_info");
151 
152     napi_valuetype valueType = napi_undefined;
153     if (argCount < 1 || napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_string) {
154         return undefinedResult;
155     }
156 
157     std::string mimeType = CommonNapi::GetStringArgument(env, args[0]);
158     std::shared_ptr<AVMediaSourceTmp> mediaSource = GetMediaSource(env, jsThis);
159 
160     if (mediaSource == nullptr) {
161         MEDIA_LOGE("Fail to get mediaSource instance.");
162         return undefinedResult;
163     }
164     if (mimeType.empty()) {
165         MEDIA_LOGE("MimeType is empty.");
166         return undefinedResult;
167     }
168 
169     mediaSource->SetMimeType(mimeType);
170 
171     return undefinedResult;
172 }
173 } // Media
174 } // OHOS