ijkplayer_napi.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*
  2. * Copyright (C) 2022 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. #include <unistd.h>
  16. #include "ijkplayer_napi.h"
  17. #include <hilog/log.h>
  18. const int32_t STR_DEFAULT_SIZE = 2048;
  19. const int32_t INDEX_0 = 0;
  20. const int32_t INDEX_1 = 1;
  21. const int32_t INDEX_2 = 2;
  22. const int32_t INDEX_3 = 3;
  23. const int32_t PARAM_COUNT_1 = 1;
  24. const int32_t PARAM_COUNT_2 = 2;
  25. const int32_t PARAM_COUNT_3 = 3;
  26. const int32_t PARAM_COUNT_4 = 4;
  27. OH_NativeXComponent_Callback IJKPlayerNapi::callback_;
  28. std::unordered_map<std::string, IJKPlayerNapi *> IJKPlayerNapi::ijkPlayerNapi_;
  29. std::string xcomponentId_;
  30. bool destroyResource;
  31. bool IJKPlayerNapi::gIsVideo = true;
  32. struct CallbackContext {
  33. napi_env env = nullptr;
  34. napi_ref callbackRef = nullptr;
  35. int what = 0;
  36. int arg1 = 0;
  37. int arg2 = 0;
  38. char *obj;
  39. };
  40. static std::string SanitizeUrlForLog(const std::string &url)
  41. {
  42. std::string sanitized = url;
  43. auto maskQueryParam = [&](const std::string &key) {
  44. std::string needle = key + "=";
  45. size_t start = sanitized.find(needle);
  46. if (start == std::string::npos) {
  47. return;
  48. }
  49. size_t valueStart = start + needle.length();
  50. size_t valueEnd = sanitized.find('&', valueStart);
  51. if (valueEnd == std::string::npos) {
  52. valueEnd = sanitized.length();
  53. }
  54. sanitized.replace(valueStart, valueEnd - valueStart, "***");
  55. };
  56. maskQueryParam("access_token");
  57. maskQueryParam("refresh_token");
  58. if (sanitized.length() > 220) {
  59. sanitized = sanitized.substr(0, 220) + "...";
  60. }
  61. return sanitized;
  62. }
  63. void messageCallBack(int what, int arg1, int arg2, char *obj, std::string id)
  64. {
  65. LOGI("napi-->messageCallBack");
  66. struct CallbackContext *context = new CallbackContext();
  67. IJKPlayerNapi *instance = IJKPlayerNapi::getInstance(id);
  68. context->env = instance->envMessage_;
  69. uv_loop_s *loopMessage = nullptr;
  70. napi_get_uv_event_loop(context->env, &loopMessage);
  71. if (loopMessage == nullptr) {
  72. LOGI("napi-->loopMessage null");
  73. return;
  74. }
  75. uv_work_t *work = new (std::nothrow) uv_work_t;
  76. if (work == nullptr) {
  77. LOGI("napi-->work null");
  78. return;
  79. }
  80. context->what = what;
  81. context->arg1 = arg1;
  82. context->arg2 = arg2;
  83. context->obj = obj;
  84. context->callbackRef = instance->callBackRefMessage_;
  85. work->data = (void *)context;
  86. uv_queue_work(
  87. loopMessage, work, [](uv_work_t *work) {},
  88. [](uv_work_t *work, int status) {
  89. LOGI("napi-->uv_queue_work");
  90. CallbackContext *context = static_cast<CallbackContext *>(work->data);
  91. napi_value callback = nullptr;
  92. napi_get_reference_value(context->env, context->callbackRef, &callback);
  93. napi_value what_;
  94. napi_value arg1_;
  95. napi_value arg2_;
  96. napi_value obj_;
  97. napi_create_int32(context->env, context->what, &what_);
  98. napi_create_int32(context->env, context->arg1, &arg1_);
  99. napi_create_int32(context->env, context->arg2, &arg2_);
  100. napi_value ret = 0;
  101. if (context->obj) {
  102. napi_create_string_utf8(context->env, context->obj, NAPI_AUTO_LENGTH, &obj_);
  103. napi_value argv_4[] = {what_, arg1_, arg2_, obj_};
  104. napi_call_function(context->env, nullptr, callback, PARAM_COUNT_4, argv_4, &ret);
  105. } else {
  106. napi_value argv_3[] = {what_, arg1_, arg2_};
  107. napi_call_function(context->env, nullptr, callback, PARAM_COUNT_3, argv_3, &ret);
  108. }
  109. if (work != nullptr) {
  110. delete work;
  111. }
  112. delete context;
  113. LOGI("napi-->uv_queue_work end");
  114. });
  115. }
  116. void post_event(int what, int arg1, int arg2, char *obj, std::string id)
  117. {
  118. LOGI("napi-->post_event-->what:%d", what);
  119. messageCallBack(what, arg1, arg2, obj, id);
  120. }
  121. void setEnvMessage(const napi_env &env, std::string id)
  122. {
  123. IJKPlayerNapi::getInstance(id)->envMessage_ = env;
  124. }
  125. void setCallBackRefMessage(const napi_ref &callbackRef, std::string id)
  126. {
  127. IJKPlayerNapi::getInstance(id)->callBackRefMessage_ = callbackRef;
  128. }
  129. void setXComponentId(std::string &id) {
  130. xcomponentId_ = id;
  131. }
  132. std::string IJKPlayerNapi::getXComponentId(napi_env env, napi_callback_info info)
  133. {
  134. LOGI("napi-->IJKPlayerNapi::getInstance_()->getXComponentId");
  135. if (destroyResource && !xcomponentId_.empty()) {
  136. return xcomponentId_;
  137. }
  138. napi_value exportInstance;
  139. napi_value thisArg;
  140. napi_status status;
  141. OH_NativeXComponent *nativeXComponent = nullptr;
  142. int32_t ret;
  143. char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
  144. uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
  145. NAPI_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &thisArg, NULL));
  146. status = napi_get_named_property(env, thisArg, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance);
  147. if (status != napi_ok) {
  148. LOGE("napi-->getXComponentId napi_get_named_property failed!");
  149. return "";
  150. };
  151. status = napi_unwrap(env, exportInstance, reinterpret_cast<void **>(&nativeXComponent));
  152. if (status != napi_ok) {
  153. LOGE("napi-->getXComponentId napi_get_named_property failed!");
  154. return "";
  155. }
  156. ret = OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize);
  157. if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
  158. LOGE("napi-->getXComponentId napi_get_named_property failed!");
  159. return "";
  160. }
  161. std::string id(idStr);
  162. LOGI("napi-->IJKPlayerNapi::getInstance_()->getXComponentId id:%s", (char *)id.c_str());
  163. return id;
  164. }
  165. napi_value IJKPlayerNapi::setMessageListener(napi_env env, napi_callback_info info) {
  166. LOGI("napi-->msg----setMessageListener");
  167. size_t argc = PARAM_COUNT_2;
  168. napi_value args[PARAM_COUNT_2] = {nullptr};
  169. napi_get_cb_info(env, info, &argc, args, NULL, NULL);
  170. napi_value width;
  171. napi_value height;
  172. napi_value result = 0;
  173. napi_value callback = args[1];
  174. napi_ref callBackRefMessage_;
  175. napi_create_reference(env, callback, 1, &callBackRefMessage_);
  176. std::string xcomponentId;
  177. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  178. if (xcomponentId == "") {
  179. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  180. }
  181. setCallBackRefMessage(callBackRefMessage_, xcomponentId);
  182. setEnvMessage(env, xcomponentId);
  183. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->message_loop_callback(post_event);
  184. return nullptr;
  185. }
  186. napi_value IJKPlayerNapi::native_setup(napi_env env, napi_callback_info info) {
  187. LOGI("napi-->native_setup");
  188. size_t argc = PARAM_COUNT_1;
  189. napi_value args[PARAM_COUNT_1] = {nullptr};
  190. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  191. std::string xcomponentId;
  192. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  193. if (xcomponentId == "") {
  194. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  195. }
  196. LOGI("napi-->native_setup id->%s", (char *)xcomponentId.c_str());
  197. setXComponentId(xcomponentId);
  198. OH_NativeXComponent *xcomponent = IJKPlayerNapi::getInstance(xcomponentId)->getXComponent(xcomponentId);
  199. void *nativeWindow = IJKPlayerNapi::getInstance(xcomponentId)->getNativeWindow(xcomponentId);
  200. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_native_setup(xcomponent, nativeWindow);
  201. return nullptr;
  202. }
  203. napi_value IJKPlayerNapi::native_setup_audio(napi_env env, napi_callback_info info) {
  204. LOGI("napi-->native_setup");
  205. size_t argc = PARAM_COUNT_1;
  206. napi_value args[PARAM_COUNT_1] = {nullptr};
  207. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  208. std::string xcomponentId;
  209. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  210. if (xcomponentId == "") {
  211. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  212. }
  213. LOGI("napi-->native_setup id->%s", (char *)xcomponentId.c_str());
  214. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_native_setup_audio();
  215. return nullptr;
  216. }
  217. napi_value IJKPlayerNapi::setDataSource(napi_env env, napi_callback_info info) {
  218. LOGI("napi-->setDataSource");
  219. size_t argc = PARAM_COUNT_2;
  220. napi_value args[PARAM_COUNT_2] = {nullptr};
  221. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  222. std::string xcomponentId;
  223. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  224. if (xcomponentId == "") {
  225. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  226. }
  227. std::string url;
  228. NapiUtil::JsValueToString(env, args[INDEX_1], STR_DEFAULT_SIZE, url);
  229. std::string safeUrl = SanitizeUrlForLog(url);
  230. LOGI("napi-->setDataSource-->url:%s", (char *)safeUrl.c_str());
  231. LOGI("napi-->setDataSource end id->%s", (char *)xcomponentId.c_str());
  232. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_setDataSource((char *)url.c_str());
  233. return nullptr;
  234. }
  235. napi_value IJKPlayerNapi::setOption(napi_env env, napi_callback_info info) {
  236. LOGI("napi-->setOption");
  237. size_t argc = PARAM_COUNT_4;
  238. napi_value args[PARAM_COUNT_4] = {nullptr};
  239. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  240. std::string xcomponentId;
  241. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  242. if (xcomponentId == "") {
  243. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  244. }
  245. std::string category;
  246. NapiUtil::JsValueToString(env, args[INDEX_1], STR_DEFAULT_SIZE, category);
  247. std::string key;
  248. NapiUtil::JsValueToString(env, args[INDEX_2], STR_DEFAULT_SIZE, key);
  249. std::string value;
  250. NapiUtil::JsValueToString(env, args[INDEX_3], STR_DEFAULT_SIZE, value);
  251. LOGI("napi-->setOption-->category:%d,key:%s,value:%s", NapiUtil::StringToInt(category), (char *)key.c_str(), (char *)value.c_str());
  252. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_setOption(NapiUtil::StringToInt(category), (char *)key.c_str(), (char *)value.c_str());
  253. return nullptr;
  254. }
  255. napi_value IJKPlayerNapi::setOptionLong(napi_env env, napi_callback_info info) {
  256. LOGI("napi-->setOptionLong");
  257. size_t argc = PARAM_COUNT_4;
  258. napi_value args[PARAM_COUNT_4] = {nullptr};
  259. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  260. std::string xcomponentId;
  261. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  262. if (xcomponentId == "") {
  263. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  264. }
  265. std::string category;
  266. NapiUtil::JsValueToString(env, args[INDEX_1], STR_DEFAULT_SIZE, category);
  267. std::string key;
  268. NapiUtil::JsValueToString(env, args[INDEX_2], STR_DEFAULT_SIZE, key);
  269. std::string value;
  270. NapiUtil::JsValueToString(env, args[INDEX_3], STR_DEFAULT_SIZE, value);
  271. LOGI("napi-->setOptionLong-->category:%d,key:%s,value:%s", NapiUtil::StringToInt(category), (char *)key.c_str(), (char *)value.c_str());
  272. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_setOptionLong(NapiUtil::StringToInt(category), (char *)key.c_str(), NapiUtil::StringToInt(value));
  273. return nullptr;
  274. }
  275. napi_value IJKPlayerNapi::setVolume(napi_env env, napi_callback_info info) {
  276. LOGI("napi-->setVolume");
  277. size_t argc = PARAM_COUNT_3;
  278. napi_value args[PARAM_COUNT_3] = {nullptr};
  279. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  280. std::string xcomponentId;
  281. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  282. if (xcomponentId == "") {
  283. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  284. }
  285. std::string leftVolume;
  286. NapiUtil::JsValueToString(env, args[INDEX_1], STR_DEFAULT_SIZE, leftVolume);
  287. std::string rightVolume;
  288. NapiUtil::JsValueToString(env, args[INDEX_2], STR_DEFAULT_SIZE, rightVolume);
  289. LOGI("napi-->setVolume-->leftVolume:%s,rightVolume:%s", (char *)leftVolume.c_str(), (char *)rightVolume.c_str());
  290. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_setVolume(NapiUtil::StringToFloat(leftVolume), NapiUtil::StringToFloat(rightVolume));
  291. return nullptr;
  292. }
  293. napi_value IJKPlayerNapi::prepareAsync(napi_env env, napi_callback_info info) {
  294. LOGI("napi-->prepareAsync");
  295. size_t argc = PARAM_COUNT_1;
  296. napi_value args[PARAM_COUNT_1] = {nullptr};
  297. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  298. std::string xcomponentId;
  299. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  300. if (xcomponentId == "") {
  301. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  302. }
  303. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_prepareAsync();
  304. return nullptr;
  305. }
  306. napi_value IJKPlayerNapi::start(napi_env env, napi_callback_info info) {
  307. LOGI("napi-->start");
  308. size_t argc = PARAM_COUNT_1;
  309. napi_value args[PARAM_COUNT_1] = {nullptr};
  310. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  311. std::string xcomponentId;
  312. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  313. if (xcomponentId == "") {
  314. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  315. }
  316. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_start();
  317. return nullptr;
  318. }
  319. napi_value IJKPlayerNapi::stop(napi_env env, napi_callback_info info) {
  320. LOGI("napi-->stop");
  321. size_t argc = PARAM_COUNT_1;
  322. napi_value args[PARAM_COUNT_1] = {nullptr};
  323. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  324. std::string xcomponentId;
  325. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  326. if (xcomponentId == "") {
  327. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  328. }
  329. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_stop();
  330. return nullptr;
  331. }
  332. napi_value IJKPlayerNapi::pause(napi_env env, napi_callback_info info) {
  333. LOGI("napi-->pause");
  334. size_t argc = PARAM_COUNT_1;
  335. napi_value args[PARAM_COUNT_1] = {nullptr};
  336. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  337. std::string xcomponentId;
  338. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  339. if (xcomponentId == "") {
  340. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  341. }
  342. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_pause();
  343. return nullptr;
  344. }
  345. napi_value IJKPlayerNapi::reset(napi_env env, napi_callback_info info) {
  346. LOGI("napi-->reset");
  347. size_t argc = PARAM_COUNT_1;
  348. napi_value args[PARAM_COUNT_1] = {nullptr};
  349. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  350. std::string xcomponentId;
  351. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  352. if (xcomponentId == "") {
  353. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  354. }
  355. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_reset();
  356. return nullptr;
  357. }
  358. napi_value IJKPlayerNapi::release(napi_env env, napi_callback_info info) {
  359. LOGI("napi-->release");
  360. size_t argc = PARAM_COUNT_1;
  361. napi_value args[PARAM_COUNT_1] = {nullptr};
  362. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  363. std::string xcomponentId;
  364. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  365. if (xcomponentId == "") {
  366. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  367. }
  368. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_release();
  369. return nullptr;
  370. }
  371. napi_value IJKPlayerNapi::getDuration(napi_env env, napi_callback_info info) {
  372. LOGI("napi-->getDuration");
  373. size_t argc = PARAM_COUNT_1;
  374. napi_value args[PARAM_COUNT_1] = {nullptr};
  375. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  376. std::string xcomponentId;
  377. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  378. if (xcomponentId == "") {
  379. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  380. }
  381. int duration = IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_getDuration();
  382. return NapiUtil::SetNapiCallInt32(env, duration);
  383. }
  384. napi_value IJKPlayerNapi::getCurrentPosition(napi_env env, napi_callback_info info) {
  385. LOGI("napi-->getCurrentPosition");
  386. size_t argc = PARAM_COUNT_1;
  387. napi_value args[PARAM_COUNT_1] = {nullptr};
  388. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  389. std::string xcomponentId;
  390. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  391. if (xcomponentId == "") {
  392. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  393. }
  394. int currentPosition =
  395. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_getCurrentPosition();
  396. return NapiUtil::SetNapiCallInt32(env, currentPosition);
  397. }
  398. napi_value IJKPlayerNapi::seekTo(napi_env env, napi_callback_info info) {
  399. LOGI("napi-->seekTo");
  400. size_t argc = PARAM_COUNT_2;
  401. napi_value args[PARAM_COUNT_2] = {nullptr};
  402. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  403. std::string xcomponentId;
  404. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  405. if (xcomponentId == "") {
  406. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  407. }
  408. std::string msec;
  409. NapiUtil::JsValueToString(env, args[INDEX_1], STR_DEFAULT_SIZE, msec);
  410. LOGI("napi-->seekTo-->msec:%d", NapiUtil::StringToInt(msec));
  411. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_seekTo(NapiUtil::StringToInt(msec));
  412. return nullptr;
  413. }
  414. napi_value IJKPlayerNapi::isPlaying(napi_env env, napi_callback_info info) {
  415. LOGI("napi-->isPlaying");
  416. size_t argc = PARAM_COUNT_1;
  417. napi_value args[PARAM_COUNT_1] = {nullptr};
  418. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  419. std::string xcomponentId;
  420. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  421. if (xcomponentId == "") {
  422. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  423. }
  424. return NapiUtil::SetNapiCallBool(env, IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_isPlaying());
  425. }
  426. napi_value IJKPlayerNapi::setPropertyFloat(napi_env env, napi_callback_info info) {
  427. LOGI("napi-->setPropertyFloat");
  428. size_t argc = PARAM_COUNT_3;
  429. napi_value args[PARAM_COUNT_3] = {nullptr};
  430. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  431. std::string xcomponentId;
  432. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  433. if (xcomponentId == "") {
  434. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  435. }
  436. std::string id;
  437. NapiUtil::JsValueToString(env, args[INDEX_1], STR_DEFAULT_SIZE, id);
  438. std::string value;
  439. NapiUtil::JsValueToString(env, args[INDEX_2], STR_DEFAULT_SIZE, value);
  440. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->ijkMediaPlayer_setPropertyFloat(NapiUtil::StringToInt(id), NapiUtil::StringToFloat(value));
  441. return nullptr;
  442. }
  443. napi_value IJKPlayerNapi::getPropertyFloat(napi_env env, napi_callback_info info) {
  444. LOGI("napi-->getPropertyFloat");
  445. size_t argc = PARAM_COUNT_3;
  446. napi_value args[PARAM_COUNT_3] = {nullptr};
  447. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  448. std::string xcomponentId;
  449. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  450. if (xcomponentId == "") {
  451. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  452. }
  453. std::string id;
  454. NapiUtil::JsValueToString(env, args[INDEX_1], STR_DEFAULT_SIZE, id);
  455. std::string value;
  456. NapiUtil::JsValueToString(env, args[INDEX_2], STR_DEFAULT_SIZE, value);
  457. float result = IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->ijkMediaPlayer_getPropertyFloat(NapiUtil::StringToInt(id), NapiUtil::StringToFloat(value));
  458. napi_value napi_result;
  459. napi_create_string_utf8(env, (char *)((std::to_string(result)).c_str()), NAPI_AUTO_LENGTH, &napi_result);
  460. return napi_result;
  461. }
  462. napi_value IJKPlayerNapi::setPropertyLong(napi_env env, napi_callback_info info) {
  463. LOGI("napi-->setPropertyLong");
  464. size_t argc = PARAM_COUNT_3;
  465. napi_value args[PARAM_COUNT_3] = {nullptr};
  466. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  467. std::string xcomponentId;
  468. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  469. if (xcomponentId == "") {
  470. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  471. }
  472. std::string id;
  473. NapiUtil::JsValueToString(env, args[INDEX_1], STR_DEFAULT_SIZE, id);
  474. std::string value;
  475. NapiUtil::JsValueToString(env, args[INDEX_2], STR_DEFAULT_SIZE, value);
  476. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->ijkMediaPlayer_setPropertyLong(NapiUtil::StringToInt(id), NapiUtil::StringToLong(value));
  477. return nullptr;
  478. }
  479. napi_value IJKPlayerNapi::getPropertyLong(napi_env env, napi_callback_info info) {
  480. LOGI("napi-->getPropertyLong");
  481. size_t argc = PARAM_COUNT_3;
  482. napi_value args[PARAM_COUNT_3] = {nullptr};
  483. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  484. std::string xcomponentId;
  485. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  486. if (xcomponentId == "") {
  487. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  488. }
  489. std::string id;
  490. NapiUtil::JsValueToString(env, args[INDEX_1], STR_DEFAULT_SIZE, id);
  491. std::string value;
  492. NapiUtil::JsValueToString(env, args[INDEX_2], STR_DEFAULT_SIZE, value);
  493. long result = IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->ijkMediaPlayer_getPropertyLong(NapiUtil::StringToInt(id), NapiUtil::StringToLong(value));
  494. napi_value napi_result;
  495. napi_create_string_utf8(env, (char *)((std::to_string(result)).c_str()), NAPI_AUTO_LENGTH, &napi_result);
  496. return napi_result;
  497. }
  498. napi_value IJKPlayerNapi::getAudioSessionId(napi_env env, napi_callback_info info) {
  499. LOGI("napi-->getAudioSessionId");
  500. size_t argc = PARAM_COUNT_1;
  501. napi_value args[PARAM_COUNT_1] = {nullptr};
  502. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  503. std::string xcomponentId;
  504. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  505. if (xcomponentId == "") {
  506. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  507. }
  508. int getAudioSessionId = IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_getAudioSessionId();
  509. return NapiUtil::SetNapiCallInt32(env, getAudioSessionId);
  510. }
  511. napi_value IJKPlayerNapi::setLoopCount(napi_env env, napi_callback_info info) {
  512. LOGI("napi-->setLoopCount");
  513. size_t argc = PARAM_COUNT_2;
  514. napi_value args[PARAM_COUNT_2] = {nullptr};
  515. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  516. std::string xcomponentId;
  517. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  518. if (xcomponentId == "") {
  519. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  520. }
  521. std::string loop_count;
  522. NapiUtil::JsValueToString(env, args[INDEX_1], STR_DEFAULT_SIZE, loop_count);
  523. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_setLoopCount(NapiUtil::StringToInt(loop_count));
  524. return nullptr;
  525. }
  526. napi_value IJKPlayerNapi::getLoopCount(napi_env env, napi_callback_info info) {
  527. LOGI("napi-->getLoopCount");
  528. size_t argc = PARAM_COUNT_1;
  529. napi_value args[PARAM_COUNT_1] = {nullptr};
  530. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  531. std::string xcomponentId;
  532. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  533. if (xcomponentId == "") {
  534. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  535. }
  536. int loop_count = IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_getLoopCount();
  537. return NapiUtil::SetNapiCallInt32(env, loop_count);
  538. }
  539. napi_value IJKPlayerNapi::getVideoCodecInfo(napi_env env, napi_callback_info info) {
  540. LOGI("napi-->getVideoCodecInfo");
  541. size_t argc = PARAM_COUNT_1;
  542. napi_value args[PARAM_COUNT_1] = {nullptr};
  543. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  544. std::string xcomponentId;
  545. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  546. if (xcomponentId == "") {
  547. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  548. }
  549. const char *result = IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_getVideoCodecInfo();
  550. if (result == nullptr) {
  551. result = "";
  552. }
  553. LOGI("napi-->getVideoCodecInfo result:%s", result);
  554. napi_value napi_result;
  555. napi_create_string_utf8(env, result, NAPI_AUTO_LENGTH, &napi_result);
  556. return napi_result;
  557. }
  558. napi_value IJKPlayerNapi::getAudioCodecInfo(napi_env env, napi_callback_info info) {
  559. LOGI("napi-->getAudioCodecInfo");
  560. size_t argc = PARAM_COUNT_1;
  561. napi_value args[PARAM_COUNT_1] = {nullptr};
  562. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  563. std::string xcomponentId;
  564. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  565. if (xcomponentId == "") {
  566. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  567. }
  568. const char *result = IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_getAudioCodecInfo();
  569. if (result == nullptr) {
  570. result = "";
  571. }
  572. napi_value napi_result;
  573. napi_create_string_utf8(env, result, NAPI_AUTO_LENGTH, &napi_result);
  574. return napi_result;
  575. }
  576. napi_value IJKPlayerNapi::setStreamSelected(napi_env env, napi_callback_info info) {
  577. LOGI("napi-->setStreamSelected");
  578. size_t argc = PARAM_COUNT_3;
  579. napi_value args[PARAM_COUNT_3] = {nullptr};
  580. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  581. std::string xcomponentId;
  582. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  583. if (xcomponentId == "") {
  584. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  585. }
  586. std::string stream;
  587. NapiUtil::JsValueToString(env, args[INDEX_1], STR_DEFAULT_SIZE, stream);
  588. std::string select;
  589. NapiUtil::JsValueToString(env, args[INDEX_2], STR_DEFAULT_SIZE, select);
  590. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->ijkMediaPlayer_setStreamSelected(NapiUtil::StringToInt(stream), NapiUtil::StringToBool(select));
  591. return nullptr;
  592. }
  593. napi_value IJKPlayerNapi::getMediaMeta(napi_env env, napi_callback_info info) {
  594. LOGI("napi-->getMediaMeta");
  595. size_t argc = PARAM_COUNT_1;
  596. napi_value args[PARAM_COUNT_1] = {nullptr};
  597. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  598. std::string xcomponentId;
  599. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  600. if (xcomponentId == "") {
  601. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  602. }
  603. HashMap map = IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_getMediaMeta();
  604. HashMapIterator iterator = hashmap_iterator(map);
  605. std::string result = "";
  606. while (hashmap_hasNext(iterator)) {
  607. iterator = hashmap_next(iterator);
  608. LOGI("napi-->getMediaMeta { key: %s, value: %s, hashcode: %d }\n",
  609. (STRING)iterator->entry->key, (STRING)iterator->entry->value, iterator->hashCode);
  610. result.append("key:");
  611. result.append((STRING)iterator->entry->key);
  612. result.append(",");
  613. result.append("value:");
  614. result.append((STRING)iterator->entry->value);
  615. result.append(";");
  616. }
  617. hashmap_delete(map);
  618. napi_value napi_result;
  619. napi_create_string_utf8(env, (char *)(result.c_str()), NAPI_AUTO_LENGTH, &napi_result);
  620. return napi_result;
  621. }
  622. napi_value IJKPlayerNapi::nativeOpenlog(napi_env env, napi_callback_info info) {
  623. LOGI("napi-->nativeOpenlog");
  624. size_t argc = PARAM_COUNT_1;
  625. napi_value args[PARAM_COUNT_1] = {nullptr};
  626. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  627. std::string xcomponentId;
  628. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  629. if (xcomponentId == "") {
  630. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  631. }
  632. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_native_openlog();
  633. return nullptr;
  634. }
  635. napi_value IJKPlayerNapi::setEqualizerEnabled(napi_env env, napi_callback_info info) {
  636. LOGI("napi-->setEqualizerEnabled");
  637. size_t argc = PARAM_COUNT_2;
  638. napi_value args[PARAM_COUNT_2] = {nullptr};
  639. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  640. std::string xcomponentId;
  641. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  642. if (xcomponentId == "") {
  643. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  644. }
  645. bool enabled = false;
  646. napi_get_value_bool(env, args[INDEX_1], &enabled);
  647. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_setEqualizerEnabled(enabled);
  648. return nullptr;
  649. }
  650. napi_value IJKPlayerNapi::getSpectrumData(napi_env env, napi_callback_info info) {
  651. size_t argc = PARAM_COUNT_1;
  652. napi_value args[PARAM_COUNT_1] = {nullptr};
  653. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  654. std::string xcomponentId;
  655. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  656. if (xcomponentId == "") {
  657. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  658. }
  659. SpectrumBuffer *buf = IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_getSpectrumBuffer();
  660. napi_value result;
  661. if (buf == NULL || !buf->has_data) {
  662. napi_create_array_with_length(env, 0, &result);
  663. return result;
  664. }
  665. const int OUTPUT_SAMPLES = 1024;
  666. napi_create_array_with_length(env, OUTPUT_SAMPLES, &result);
  667. int channels = buf->channels;
  668. int available = buf->write_pos;
  669. if (channels <= 0) channels = 1;
  670. int sampleCount = available / channels;
  671. if (sampleCount > OUTPUT_SAMPLES) sampleCount = OUTPUT_SAMPLES;
  672. for (int i = 0; i < sampleCount; i++) {
  673. double value = 0.0;
  674. if (channels >= 2) {
  675. int idx = i * 2;
  676. if (idx + 1 < available) {
  677. value = ((double)buf->data[idx] + (double)buf->data[idx + 1]) / 2.0;
  678. }
  679. } else {
  680. if (i < available) {
  681. value = (double)buf->data[i];
  682. }
  683. }
  684. napi_value element;
  685. napi_create_double(env, value, &element);
  686. napi_set_element(env, result, i, element);
  687. }
  688. for (int i = sampleCount; i < OUTPUT_SAMPLES; i++) {
  689. napi_value element;
  690. napi_create_double(env, 0.0, &element);
  691. napi_set_element(env, result, i, element);
  692. }
  693. return result;
  694. }
  695. napi_value IJKPlayerNapi::setEqualizerGains(napi_env env, napi_callback_info info) {
  696. size_t argc = PARAM_COUNT_2;
  697. napi_value args[PARAM_COUNT_2] = {nullptr};
  698. napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  699. std::string xcomponentId;
  700. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, xcomponentId);
  701. if (xcomponentId == "") {
  702. xcomponentId = IJKPlayerNapi::getXComponentId(env, info);
  703. }
  704. // Check if args[1] is array
  705. bool isArray = false;
  706. napi_is_array(env, args[INDEX_1], &isArray);
  707. if (!isArray) {
  708. return nullptr;
  709. }
  710. // Parse the gains array (10 bands)
  711. napi_value gainsArray = args[INDEX_1];
  712. uint32_t length = 0;
  713. napi_get_array_length(env, gainsArray, &length);
  714. constexpr uint32_t kEqBandCount = EQ_BAND_COUNT;
  715. float gains[EQ_BAND_COUNT] = {0.0f};
  716. for (uint32_t i = 0; i < length && i < kEqBandCount; i++) {
  717. napi_value element;
  718. napi_get_element(env, gainsArray, i, &element);
  719. double value = 0.0;
  720. napi_get_value_double(env, element, &value);
  721. gains[i] = (float)value;
  722. }
  723. IJKPlayerNapi::getInstance(xcomponentId)->ijkPlayerNapiProxy_->IjkMediaPlayer_setEqualizerGains(gains);
  724. return nullptr;
  725. }
  726. napi_value IJKPlayerNapi::JsConstructor(napi_env env, napi_callback_info info) {
  727. LOGI("napi-->JsConstructor");
  728. napi_value targetObj = nullptr;
  729. size_t argc = PARAM_COUNT_2;
  730. napi_value args[PARAM_COUNT_2] = {nullptr};
  731. napi_get_cb_info(env, info, &argc, args, &targetObj, nullptr);
  732. std::string Id;
  733. NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, Id);
  734. bool isVideo;
  735. napi_get_value_bool(env, args[INDEX_1], &isVideo);
  736. gIsVideo = isVideo;
  737. auto ijkplayer = getInstance(Id);
  738. napi_wrap(
  739. env, nullptr, ijkplayer,
  740. [](napi_env env, void *data, void *hint) {
  741. IJKPlayerNapi *bind = (IJKPlayerNapi *)data;
  742. delete bind;
  743. bind = nullptr;
  744. },
  745. nullptr, nullptr);
  746. return targetObj;
  747. }
  748. /////////////////////////////XComponent////////////////////////////////
  749. void IJKPlayerNapi::setXComponentAndNativeWindow(std::string &id, OH_NativeXComponent *component, void *window) {
  750. LOGI("napi-->IJKPlayerNapi::SetXComponentAndNativeWindow");
  751. if (nativeXComponentMap_.find(id) == nativeXComponentMap_.end()) {
  752. nativeXComponentMap_[id] = component;
  753. } else {
  754. if (nativeXComponentMap_[id] != component) {
  755. nativeXComponentMap_[id] = component;
  756. }
  757. }
  758. if (nativeWindowMap_.find(id) == nativeWindowMap_.end()) {
  759. nativeWindowMap_[id] = window;
  760. } else {
  761. if (nativeWindowMap_[id] != window) {
  762. nativeWindowMap_[id] = window;
  763. }
  764. }
  765. }
  766. OH_NativeXComponent *IJKPlayerNapi::getXComponent(std::string &id) {
  767. LOGI("napi-->IJKPlayerNapi::getXComponent");
  768. if (nativeXComponentMap_.find(id) == nativeXComponentMap_.end()) {
  769. LOGI("napi-->IJKPlayerNapi::getXComponent null");
  770. return nullptr;
  771. } else {
  772. LOGI("napi-->IJKPlayerNapi::getXComponent success");
  773. return nativeXComponentMap_[id];
  774. }
  775. }
  776. void *IJKPlayerNapi::getNativeWindow(std::string &id) {
  777. LOGI("napi-->IJKPlayerNapi::getNativeWindow");
  778. if (nativeWindowMap_.find(id) == nativeWindowMap_.end()) {
  779. LOGI("napi-->IJKPlayerNapi::getNativeWindow null");
  780. return nullptr;
  781. } else {
  782. LOGI("napi-->IJKPlayerNapi::getNativeWindow success");
  783. return nativeWindowMap_[id];
  784. }
  785. }
  786. void onSurfaceCreatedCB(OH_NativeXComponent *component, void *window) {
  787. destroyResource = false;
  788. LOGI("napi-->OnSurfaceCreatedCB");
  789. int32_t ret;
  790. char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
  791. uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
  792. ret = OH_NativeXComponent_GetXComponentId(component, idStr, &idSize);
  793. if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
  794. return;
  795. }
  796. LOGI("napi-->OnSurfaceCreatedCB-->success");
  797. std::string id(idStr);
  798. auto ijkplayerNapi = IJKPlayerNapi::getInstance(id);
  799. ijkplayerNapi->onSurfaceCreated(component, window);
  800. ijkplayerNapi->setXComponentAndNativeWindow(id, component, window);
  801. }
  802. void onSurfaceChangedCB(OH_NativeXComponent *component, void *window) {
  803. LOGI("napi-->OnSurfaceChangedCB");
  804. int32_t ret;
  805. char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
  806. uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
  807. ret = OH_NativeXComponent_GetXComponentId(component, idStr, &idSize);
  808. if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
  809. return;
  810. }
  811. std::string id(idStr);
  812. auto ijkplayerNapi = IJKPlayerNapi::getInstance(id);
  813. ijkplayerNapi->onSurfaceChanged(component, window);
  814. }
  815. void onSurfaceDestroyedCB(OH_NativeXComponent *component, void *window) {
  816. destroyResource = true;
  817. LOGI("napi-->OnSurfaceDestroyedCB");
  818. int32_t ret;
  819. char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
  820. uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
  821. ret = OH_NativeXComponent_GetXComponentId(component, idStr, &idSize);
  822. if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
  823. return;
  824. }
  825. std::string id(idStr);
  826. auto ijkplayerNapi = IJKPlayerNapi::getInstance(id);
  827. ijkplayerNapi->ijkPlayerNapiProxy_->delete_media_player(id);
  828. ijkplayerNapi->onSurfaceDestroyed(component, window);
  829. }
  830. void dispatchTouchEventCB(OH_NativeXComponent *component, void *window) {
  831. LOGI("napi-->DispatchTouchEventCB");
  832. int32_t ret;
  833. char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
  834. uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
  835. ret = OH_NativeXComponent_GetXComponentId(component, idStr, &idSize);
  836. if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
  837. return;
  838. }
  839. std::string id(idStr);
  840. auto ijkplayerNapi = IJKPlayerNapi::getInstance(id);
  841. ijkplayerNapi->dispatchTouchEvent(component, window);
  842. }
  843. void IJKPlayerNapi::onSurfaceCreated(OH_NativeXComponent *component, void *window) {
  844. LOGI("napi-->OnSurfaceCreated");
  845. int32_t ret = OH_NativeXComponent_GetXComponentSize(component, window, &width_, &height_);
  846. if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
  847. LOGI("napi-->OnSurfaceCreated-->success");
  848. }
  849. }
  850. void IJKPlayerNapi::onSurfaceChanged(OH_NativeXComponent *component, void *window) {
  851. LOGI("napi-->OnSurfaceChanged");
  852. }
  853. void IJKPlayerNapi::onSurfaceDestroyed(OH_NativeXComponent *component, void *window) {
  854. LOGI("napi-->OnSurfaceDestroyed");
  855. }
  856. void IJKPlayerNapi::dispatchTouchEvent(OH_NativeXComponent *component, void *window) {
  857. LOGI("napi-->DispatchTouchEvent");
  858. int32_t ret = OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent_);
  859. if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
  860. LOGI("napi-->DispatchTouchEvent-->success");
  861. }
  862. }
  863. IJKPlayerNapi::IJKPlayerNapi(std::string &id) : id_(id), component_(nullptr) {
  864. LOGI("IJKPlayerNapi::IJKPlayerNapi");
  865. ijkPlayerNapiProxy_ = new IJKPlayerNapiProxy(id);
  866. if (gIsVideo) {
  867. auto ijkplayerNapiCallback = IJKPlayerNapi::getNXComponentCallback();
  868. ijkplayerNapiCallback->OnSurfaceCreated = onSurfaceCreatedCB;
  869. ijkplayerNapiCallback->OnSurfaceChanged = onSurfaceChangedCB;
  870. ijkplayerNapiCallback->OnSurfaceDestroyed = onSurfaceDestroyedCB;
  871. ijkplayerNapiCallback->DispatchTouchEvent = dispatchTouchEventCB;
  872. }
  873. }
  874. IJKPlayerNapi *IJKPlayerNapi::getInstance(std::string &id) {
  875. LOGI("napi-->IJKPlayerNapi::getInstance");
  876. if (ijkPlayerNapi_.find(id) == ijkPlayerNapi_.end()) {
  877. LOGI("napi-->IJKPlayerNapi::getInstance create object");
  878. IJKPlayerNapi *instance = new IJKPlayerNapi(id);
  879. ijkPlayerNapi_[id] = instance;
  880. return instance;
  881. } else {
  882. LOGI("napi-->IJKPlayerNapi::getInstance return");
  883. return ijkPlayerNapi_[id];
  884. }
  885. }
  886. OH_NativeXComponent_Callback *IJKPlayerNapi::getNXComponentCallback() {
  887. LOGI("IJKPlayerNapi::getNXComponentCallback");
  888. return &IJKPlayerNapi::callback_;
  889. }
  890. void IJKPlayerNapi::setNativeXComponent(OH_NativeXComponent *component) {
  891. LOGI("IJKPlayerNapi::setNativeXComponent");
  892. component_ = component;
  893. OH_NativeXComponent_RegisterCallback(component_, &IJKPlayerNapi::callback_);
  894. }
  895. napi_value IJKPlayerNapi::Export(napi_env env, napi_value exports) {
  896. LOGI("IJKPlayerNapi::Export");
  897. napi_property_descriptor desc[] = {
  898. DECLARE_NAPI_FUNCTION("_setDataSource", IJKPlayerNapi::setDataSource),
  899. DECLARE_NAPI_FUNCTION("_setOption", IJKPlayerNapi::setOption),
  900. DECLARE_NAPI_FUNCTION("_setOptionLong", IJKPlayerNapi::setOptionLong),
  901. DECLARE_NAPI_FUNCTION("_prepareAsync", IJKPlayerNapi::prepareAsync),
  902. DECLARE_NAPI_FUNCTION("_start", IJKPlayerNapi::start),
  903. DECLARE_NAPI_FUNCTION("_stop", IJKPlayerNapi::stop),
  904. DECLARE_NAPI_FUNCTION("_pause", IJKPlayerNapi::pause),
  905. DECLARE_NAPI_FUNCTION("_reset", IJKPlayerNapi::reset),
  906. DECLARE_NAPI_FUNCTION("_release", IJKPlayerNapi::release),
  907. DECLARE_NAPI_FUNCTION("_seekTo", IJKPlayerNapi::seekTo),
  908. DECLARE_NAPI_FUNCTION("_isPlaying", IJKPlayerNapi::isPlaying),
  909. DECLARE_NAPI_FUNCTION("_setMessageListener", IJKPlayerNapi::setMessageListener),
  910. DECLARE_NAPI_FUNCTION("_getDuration", IJKPlayerNapi::getDuration),
  911. DECLARE_NAPI_FUNCTION("_getCurrentPosition", IJKPlayerNapi::getCurrentPosition),
  912. DECLARE_NAPI_FUNCTION("_setPropertyFloat", IJKPlayerNapi::setPropertyFloat),
  913. DECLARE_NAPI_FUNCTION("_getPropertyFloat", IJKPlayerNapi::getPropertyFloat),
  914. DECLARE_NAPI_FUNCTION("_setPropertyLong", IJKPlayerNapi::setPropertyLong),
  915. DECLARE_NAPI_FUNCTION("_getPropertyLong", IJKPlayerNapi::getPropertyLong),
  916. DECLARE_NAPI_FUNCTION("_getAudioSessionId", IJKPlayerNapi::getAudioSessionId),
  917. DECLARE_NAPI_FUNCTION("_setVolume", IJKPlayerNapi::setVolume),
  918. DECLARE_NAPI_FUNCTION("_setLoopCount", IJKPlayerNapi::setLoopCount),
  919. DECLARE_NAPI_FUNCTION("_getLoopCount", IJKPlayerNapi::getLoopCount),
  920. DECLARE_NAPI_FUNCTION("_getVideoCodecInfo", IJKPlayerNapi::getVideoCodecInfo),
  921. DECLARE_NAPI_FUNCTION("_getAudioCodecInfo", IJKPlayerNapi::getAudioCodecInfo),
  922. DECLARE_NAPI_FUNCTION("_setStreamSelected", IJKPlayerNapi::setStreamSelected),
  923. DECLARE_NAPI_FUNCTION("_getMediaMeta", IJKPlayerNapi::getMediaMeta),
  924. DECLARE_NAPI_FUNCTION("_nativeOpenlog", IJKPlayerNapi::nativeOpenlog),
  925. DECLARE_NAPI_FUNCTION("_native_setup", IJKPlayerNapi::native_setup),
  926. DECLARE_NAPI_FUNCTION("_setEqualizerEnabled", IJKPlayerNapi::setEqualizerEnabled),
  927. DECLARE_NAPI_FUNCTION("_setEqualizerGains", IJKPlayerNapi::setEqualizerGains),
  928. DECLARE_NAPI_FUNCTION("_getSpectrumData", IJKPlayerNapi::getSpectrumData),
  929. };
  930. NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
  931. return exports;
  932. }