media_player.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file media player interface
  8. */
  9. #define SYS_LOG_DOMAIN "media"
  10. #include <os_common_api.h>
  11. #include <sys/byteorder.h>
  12. #include <mem_manager.h>
  13. #include <msg_manager.h>
  14. #include <srv_manager.h>
  15. #ifdef CONFIG_BLUETOOTH
  16. #include <bt_manager.h>
  17. #endif
  18. #include <sys_wakelock.h>
  19. #include <string.h>
  20. #include "audio_policy.h"
  21. #include "audio_system.h"
  22. #include "media_player.h"
  23. #include "media_service.h"
  24. #include "media_mem.h"
  25. #ifdef CONFIG_PROPERTY
  26. #include <property_manager.h>
  27. #endif
  28. #ifdef CONFIG_DVFS
  29. #include "dvfs.h"
  30. #endif
  31. static OS_MUTEX_DEFINE(media_srv_mutex);
  32. static int media_player_ref_cnt;
  33. static bool force_disable_player = false;
  34. static media_player_t *current_media_dumpable_player;
  35. static media_player_t *current_media_main_player;
  36. /* the media_player_t will be return as parameter "uesr_data" */
  37. static media_srv_event_notify_t pfn_lifecycle_notify;
  38. static void _notify_player_lifecycle_changed(media_player_t *player, uint32_t event, void *data, uint32_t len)
  39. {
  40. if (pfn_lifecycle_notify)
  41. pfn_lifecycle_notify(event, data, len, player);
  42. }
  43. static void _media_service_default_callback(struct app_msg *msg, int result, void *reply)
  44. {
  45. if (msg->sync_sem) {
  46. os_sem_give((os_sem *)msg->sync_sem);
  47. }
  48. }
  49. #ifdef CONFIG_ACTS_DVFS_DYNAMIC_LEVEL
  50. static int _media_player_get_dvfs_level(int stream_type, int format, bool is_tws, bool is_dumpable)
  51. {
  52. int dvfs_level = DVFS_LEVEL_PERFORMANCE;
  53. switch (stream_type) {
  54. case AUDIO_STREAM_VOICE:
  55. dvfs_level = DVFS_LEVEL_PERFORMANCE;
  56. break;
  57. case AUDIO_STREAM_LOCAL_MUSIC:
  58. dvfs_level = DVFS_LEVEL_MID_PERFORMANCE;
  59. if (format == FLA_TYPE || format == APE_TYPE || format == M4A_TYPE) {
  60. dvfs_level = DVFS_LEVEL_HIGH_PERFORMANCE;
  61. }
  62. break;
  63. case AUDIO_STREAM_MUSIC:
  64. dvfs_level = DVFS_LEVEL_MID_PERFORMANCE;
  65. break;
  66. case AUDIO_STREAM_TTS:
  67. dvfs_level = DVFS_LEVEL_PERFORMANCE;
  68. break;
  69. case AUDIO_STREAM_AI:
  70. dvfs_level = DVFS_LEVEL_PERFORMANCE;
  71. break;
  72. }
  73. return dvfs_level;
  74. }
  75. #endif
  76. static bool voice_download_effect_bypass = false;
  77. static bool voice_upload_effect_bypass = false;
  78. static bool music_effect_bypass = false;
  79. int media_player_set_voice_effect_bypass(int type, bool bypass)
  80. {
  81. if (type == VOICE_DOWNLOAD_EFFECT){
  82. voice_download_effect_bypass = bypass;
  83. }
  84. if (type == VOICE_UPLOAD_EFFECT){
  85. voice_upload_effect_bypass = bypass;
  86. }
  87. return 0;
  88. }
  89. int media_player_set_effect_bypass(bool bypass)
  90. {
  91. music_effect_bypass = bypass;
  92. return 0;
  93. }
  94. static int _media_player_check_audio_effect(media_player_t *handle, int stream_type)
  95. {
  96. bool effect_enable = true;
  97. switch (stream_type) {
  98. case AUDIO_STREAM_VOICE:
  99. #ifndef CONFIG_VOICE_EFFECT
  100. effect_enable = false;
  101. #endif
  102. break;
  103. case AUDIO_STREAM_MUSIC:
  104. case AUDIO_STREAM_LOCAL_MUSIC:
  105. case AUDIO_STREAM_LOCAL_RECORD:
  106. case AUDIO_STREAM_GMA_RECORD:
  107. case AUDIO_STREAM_LINEIN:
  108. case AUDIO_STREAM_SPDIF_IN:
  109. case AUDIO_STREAM_MIC_IN:
  110. case AUDIO_STREAM_FM:
  111. case AUDIO_STREAM_USOUND:
  112. case AUDIO_STREAM_TTS:
  113. #ifndef CONFIG_MUSIC_EFFECT
  114. effect_enable = false;
  115. #else
  116. if(music_effect_bypass) {
  117. effect_enable = false;
  118. }
  119. #endif
  120. break;
  121. default:
  122. break;
  123. }
  124. if (!effect_enable) {
  125. media_player_set_effect_enable(handle, effect_enable);
  126. }
  127. if(voice_download_effect_bypass) {
  128. _media_player_set_voice_effect_bypass(handle, VOICE_DOWNLOAD_EFFECT, 1);
  129. } else {
  130. _media_player_set_voice_effect_bypass(handle, VOICE_DOWNLOAD_EFFECT, 0);
  131. }
  132. if(voice_upload_effect_bypass) {
  133. _media_player_set_voice_effect_bypass(handle, VOICE_UPLOAD_EFFECT, 1);
  134. } else {
  135. _media_player_set_voice_effect_bypass(handle, VOICE_UPLOAD_EFFECT, 0);
  136. }
  137. #if CONFIG_MEDIA_EFFECT_OUTMODE != MEDIA_EFFECT_OUTPUT_DEFAULT
  138. media_player_set_effect_output_mode(handle, CONFIG_MEDIA_EFFECT_OUTMODE);
  139. #endif
  140. return 0;
  141. }
  142. media_player_t *media_player_open(media_init_param_t *init_param)
  143. {
  144. struct app_msg msg = {0};
  145. os_sem return_notify;
  146. media_srv_init_param_t srv_param;
  147. #ifdef CONFIG_ACTS_DVFS_DYNAMIC_LEVEL
  148. int dvfs_level = DVFS_LEVEL_PERFORMANCE;
  149. #endif
  150. bool is_tws = false;
  151. #ifdef CONFIG_TWS
  152. uint8_t codec;
  153. #endif
  154. #ifdef CONFIG_PROPERTY
  155. property_flush_req(NULL);
  156. property_flush_req_deal();
  157. #endif
  158. if (force_disable_player) {
  159. return NULL;
  160. }
  161. media_player_t *handle = media_mem_malloc(sizeof(media_player_t), MCU_MEMORY);
  162. if (!handle) {
  163. return NULL;
  164. }
  165. /* fall back to media player handle */
  166. if (!init_param->user_data)
  167. init_param->user_data = handle;
  168. os_sem_init(&return_notify, 0, 1);
  169. #ifdef CONFIG_MEDIA_DSP_SLEEP
  170. init_param->dsp_sleep = 1;
  171. init_param->dsp_sleep_mode = CONFIG_MEDIA_DSP_SLEEP_MODE;
  172. init_param->sleep_stat_log = 0;
  173. #endif
  174. srv_param.user_param = init_param;
  175. srv_param.mediasrv_handle = NULL;
  176. #ifdef CONFIG_TWS
  177. /**this to get media runtime from media service*/
  178. srv_param.tws_observer = init_param->support_tws ? bt_manager_tws_get_runtime_observer() : NULL;
  179. if (srv_param.tws_observer && init_param->stream_type != AUDIO_STREAM_LOCAL_MUSIC) {
  180. if (init_param->format == AAC_TYPE) {
  181. codec = 2; /* BT_A2DP_MPEG2 */
  182. } else {
  183. codec = 0; /* BT_A2DP_SBC */
  184. }
  185. bt_manager_tws_notify_start_play(init_param->stream_type, codec, init_param->sample_rate);
  186. }
  187. #else
  188. //srv_param.tws_observer = NULL;
  189. #endif
  190. #ifdef CONFIG_TWS_MONO_MODE
  191. srv_param.tws_mode = MEDIA_TWS_MODE_MONO;
  192. #else
  193. srv_param.tws_mode = MEDIA_TWS_MODE_STEREO;
  194. #endif
  195. /* for compatibility */
  196. srv_param.tws_samplerate_44_48_only = 1;
  197. msg.type = MSG_MEDIA_SRV_OPEN;
  198. msg.ptr = &srv_param;
  199. msg.callback = _media_service_default_callback;
  200. msg.sync_sem = &return_notify;
  201. os_mutex_lock(&media_srv_mutex, OS_FOREVER);
  202. #ifdef CONFIG_TWS
  203. is_tws = srv_param.tws_observer ? true : false;
  204. #endif
  205. #ifdef CONFIG_ACTS_DVFS_DYNAMIC_LEVEL
  206. dvfs_level = _media_player_get_dvfs_level(init_param->stream_type,init_param->format, is_tws, init_param->dumpable);
  207. SYS_LOG_INF("tws %d type %d dvfs %d\n", is_tws, init_param->stream_type, dvfs_level);
  208. dvfs_set_level(dvfs_level, "media");
  209. #endif
  210. if (!send_async_msg(MEDIA_SERVICE_NAME, &msg)) {
  211. goto error_exit;
  212. }
  213. if (os_sem_take(&return_notify, OS_FOREVER) != 0) {
  214. goto error_exit;
  215. }
  216. if (!srv_param.mediasrv_handle) {
  217. goto error_exit;
  218. }
  219. #ifdef CONFIG_TWS
  220. if (srv_param.tws_observer && init_param->stream_type == AUDIO_STREAM_LOCAL_MUSIC) {
  221. codec = 0; /* BT_A2DP_SBC */
  222. bt_manager_tws_notify_start_play(init_param->stream_type, codec, init_param->sample_rate);
  223. }
  224. #endif
  225. handle->media_srv_handle = srv_param.mediasrv_handle;
  226. handle->type = init_param->type;
  227. handle->is_tws = is_tws;
  228. #ifdef CONFIG_ACTS_DVFS_DYNAMIC_LEVEL
  229. handle->dvfs_level = dvfs_level;
  230. #endif
  231. if (init_param->dumpable && !current_media_dumpable_player) {
  232. current_media_dumpable_player = handle;
  233. }
  234. if (!current_media_main_player)
  235. current_media_main_player = handle;
  236. media_player_ref_cnt++;
  237. _media_player_check_audio_effect(handle, init_param->stream_type);
  238. if (is_tws) {
  239. #if CONFIG_TWS_AUDIO_OUT_MODE == 0
  240. #ifdef CONFIG_PROPERTY
  241. int tws_mode = property_get_int("TWS_MODE", 0);
  242. #else
  243. int tws_mode = MEDIA_OUTPUT_MODE_DEFAULT;
  244. #endif
  245. if (tws_mode == MEDIA_OUTPUT_MODE_LEFT || tws_mode == MEDIA_OUTPUT_MODE_RIGHT) {
  246. media_player_set_output_mode(handle, tws_mode);
  247. }
  248. #elif CONFIG_TWS_AUDIO_OUT_MODE == 1
  249. media_player_set_output_mode(handle, MEDIA_OUTPUT_MODE_DEFAULT);
  250. #elif CONFIG_TWS_AUDIO_OUT_MODE == 2
  251. media_player_set_output_mode(handle, MEDIA_OUTPUT_MODE_DEFAULT);
  252. media_player_set_effect_output_mode(handle, MEDIA_EFFECT_OUTPUT_L_R_MIX);
  253. #endif
  254. }
  255. #ifdef CONFIG_SYS_WAKELOCK
  256. sys_wake_lock_ext(PARTIAL_WAKE_LOCK, MEDIA_WAKE_LOCK_USER);
  257. #endif
  258. os_mutex_unlock(&media_srv_mutex);
  259. _notify_player_lifecycle_changed(handle, PLAYER_EVENT_OPEN, init_param, sizeof(*init_param));
  260. return handle;
  261. error_exit:
  262. #ifdef CONFIG_ACTS_DVFS_DYNAMIC_LEVEL
  263. SYS_LOG_INF("unset tws %d type %d dvfs %d\n", is_tws, init_param->stream_type, dvfs_level);
  264. dvfs_unset_level(dvfs_level, "media");
  265. #endif
  266. os_mutex_unlock(&media_srv_mutex);
  267. media_mem_free(handle);
  268. return NULL;
  269. }
  270. int media_player_play(media_player_t *handle)
  271. {
  272. struct app_msg msg = {0};
  273. int ret = 0;
  274. if (!handle || !handle->media_srv_handle) {
  275. return -EINVAL;
  276. }
  277. msg.type = MSG_MEDIA_SRV_PLAY;
  278. msg.ptr = handle->media_srv_handle;
  279. ret = send_async_msg(MEDIA_SERVICE_NAME, &msg);
  280. if (ret)
  281. _notify_player_lifecycle_changed(handle, PLAYER_EVENT_PLAY, NULL, 0);
  282. return !ret;
  283. }
  284. int media_player_stop(media_player_t *handle)
  285. {
  286. struct app_msg msg = {0};
  287. int ret = 0;
  288. if (!handle || !handle->media_srv_handle) {
  289. return -EINVAL;
  290. }
  291. msg.type = MSG_MEDIA_SRV_STOP;
  292. msg.ptr = handle->media_srv_handle;
  293. #ifdef CONFIG_TWS
  294. if (handle->is_tws) {
  295. bt_manager_tws_notify_stop_play();
  296. }
  297. #endif
  298. ret = send_async_msg(MEDIA_SERVICE_NAME, &msg);
  299. if (ret)
  300. _notify_player_lifecycle_changed(handle, PLAYER_EVENT_STOP, NULL, 0);
  301. return !ret;
  302. }
  303. int media_player_pause(media_player_t *handle)
  304. {
  305. struct app_msg msg = {0};
  306. int ret = 0;
  307. if (!handle || !handle->media_srv_handle) {
  308. return -EINVAL;
  309. }
  310. msg.type = MSG_MEDIA_SRV_PAUSE;
  311. msg.ptr = handle->media_srv_handle;
  312. ret = send_async_msg(MEDIA_SERVICE_NAME, &msg);
  313. if (ret)
  314. _notify_player_lifecycle_changed(handle, PLAYER_EVENT_PAUSE, NULL, 0);
  315. return !ret;
  316. }
  317. int media_player_resume(media_player_t *handle)
  318. {
  319. struct app_msg msg = {0};
  320. int ret = 0;
  321. if (!handle || !handle->media_srv_handle) {
  322. return -EINVAL;
  323. }
  324. msg.type = MSG_MEDIA_SRV_RESUME;
  325. msg.ptr = handle->media_srv_handle;
  326. ret = send_async_msg(MEDIA_SERVICE_NAME, &msg);
  327. if (ret)
  328. _notify_player_lifecycle_changed(handle, PLAYER_EVENT_RESUNE, NULL, 0);
  329. return !ret;
  330. }
  331. int media_player_seek(media_player_t *handle, media_seek_info_t *info)
  332. {
  333. struct app_msg msg = {0};
  334. os_sem return_notify;
  335. media_srv_param_t *srv_param = NULL;
  336. if (!handle || !handle->media_srv_handle) {
  337. return -EINVAL;
  338. }
  339. srv_param = media_mem_malloc(sizeof(*srv_param), MCU_MEMORY);
  340. if (!srv_param)
  341. return -ENOMEM;
  342. os_sem_init(&return_notify, 0, 1);
  343. srv_param->handle = handle->media_srv_handle;
  344. srv_param->param.pbuf = info;
  345. srv_param->param.plen = sizeof(*info);
  346. msg.ptr = srv_param;
  347. msg.type = MSG_MEDIA_SRV_SEEK;
  348. msg.callback = _media_service_default_callback;
  349. msg.sync_sem = &return_notify;
  350. if (false == send_async_msg(MEDIA_SERVICE_NAME, &msg)) {
  351. media_mem_free(srv_param);
  352. return -EBUSY;
  353. }
  354. if (os_sem_take(&return_notify, OS_FOREVER)) {
  355. media_mem_free(srv_param);
  356. return -ETIME;
  357. }
  358. media_mem_free(srv_param);
  359. return 0;
  360. }
  361. int media_player_get_parameter(media_player_t *handle,
  362. int pname, void *param, unsigned int psize)
  363. {
  364. struct app_msg msg = {0};
  365. os_sem return_notify;
  366. media_srv_param_t *srv_param = NULL;
  367. int ret;
  368. if (!handle || !handle->media_srv_handle) {
  369. return -EINVAL;
  370. }
  371. srv_param = media_mem_malloc(sizeof(*srv_param),MCU_MEMORY);
  372. if (!srv_param)
  373. return -ENOMEM;
  374. os_sem_init(&return_notify, 0, 1);
  375. srv_param->handle = handle->media_srv_handle;
  376. srv_param->param.type = pname;
  377. srv_param->param.pbuf = param;
  378. srv_param->param.plen = psize;
  379. srv_param->result = 0;
  380. msg.ptr = srv_param;
  381. msg.type = MSG_MEDIA_SRV_GET_PARAMETER;
  382. msg.callback = _media_service_default_callback;
  383. msg.sync_sem = &return_notify;
  384. if (false == send_async_msg(MEDIA_SERVICE_NAME, &msg)) {
  385. media_mem_free(srv_param);
  386. return -EBUSY;
  387. }
  388. if (os_sem_take(&return_notify, OS_FOREVER)) {
  389. media_mem_free(srv_param);
  390. return -ETIME;
  391. }
  392. ret = srv_param->result;
  393. media_mem_free(srv_param);
  394. return ret;
  395. }
  396. /* use synchronized message to avoid mem_malloc for large parameter, whose psize is not zero */
  397. int media_player_set_parameter(media_player_t *handle,
  398. int pname, void *param, unsigned int psize)
  399. {
  400. struct app_msg msg = {0};
  401. os_sem return_notify;
  402. media_srv_param_t *srv_param = NULL;
  403. int ret = 0;
  404. if (!handle || !handle->media_srv_handle) {
  405. return -EINVAL;
  406. }
  407. srv_param = media_mem_malloc(sizeof(media_srv_param_t), MCU_MEMORY);
  408. if (!srv_param)
  409. return -ENOMEM;
  410. srv_param->handle = handle->media_srv_handle;
  411. srv_param->param.type = pname;
  412. srv_param->param.pbuf = param;
  413. srv_param->param.plen = psize;
  414. srv_param->result = 0;
  415. msg.ptr = srv_param;
  416. msg.type = MSG_MEDIA_SRV_SET_PARAMETER;
  417. if (psize > 0) {
  418. os_sem_init(&return_notify, 0, 1);
  419. msg.callback = _media_service_default_callback;
  420. msg.sync_sem = &return_notify;
  421. }
  422. if (false == send_async_msg(MEDIA_SERVICE_NAME, &msg)) {
  423. media_mem_free(srv_param);
  424. return -EBUSY;
  425. }
  426. if (psize > 0) {
  427. if (os_sem_take(&return_notify, OS_FOREVER)) {
  428. media_mem_free(srv_param);
  429. return -ETIME;
  430. }
  431. ret = srv_param->result;
  432. media_mem_free(srv_param);
  433. }
  434. return ret;
  435. }
  436. int media_player_get_global_parameter(media_player_t *handle,
  437. int pname, void *param, unsigned int psize)
  438. {
  439. struct app_msg msg = {0};
  440. os_sem return_notify;
  441. media_srv_param_t *srv_param = NULL;
  442. int ret;
  443. srv_param = media_mem_malloc(sizeof(*srv_param), MCU_MEMORY);
  444. if (!srv_param)
  445. return -ENOMEM;
  446. os_sem_init(&return_notify, 0, 1);
  447. srv_param->handle = handle ? handle->media_srv_handle : NULL;
  448. srv_param->param.type = pname;
  449. srv_param->param.pbuf = param;
  450. srv_param->param.plen = psize;
  451. srv_param->result = 0;
  452. msg.ptr = srv_param;
  453. msg.type = MSG_MEDIA_SRV_GET_GLOBAL_PARAMETER;
  454. msg.callback = _media_service_default_callback;
  455. msg.sync_sem = &return_notify;
  456. if (false == send_async_msg(MEDIA_SERVICE_NAME, &msg)) {
  457. media_mem_free(srv_param);
  458. return -EBUSY;
  459. }
  460. if (os_sem_take(&return_notify, OS_FOREVER)) {
  461. media_mem_free(srv_param);
  462. return -ETIME;
  463. }
  464. ret = srv_param->result;
  465. media_mem_free(srv_param);
  466. return ret;
  467. }
  468. /* use synchronized message to avoid mem_malloc for large parameter, whose psize is not zero */
  469. int media_player_set_global_parameter(media_player_t *handle, int pname, void *param, unsigned int psize)
  470. {
  471. struct app_msg msg = {0};
  472. os_sem return_notify;
  473. media_srv_param_t *srv_param = NULL;
  474. int ret = 0;
  475. srv_param = media_mem_malloc(sizeof(media_srv_param_t), MCU_MEMORY);
  476. if (!srv_param)
  477. return -ENOMEM;
  478. srv_param->handle = handle ? handle->media_srv_handle : NULL;
  479. srv_param->param.type = pname;
  480. srv_param->param.pbuf = param;
  481. srv_param->param.plen = psize;
  482. srv_param->result = 0;
  483. msg.ptr = srv_param;
  484. msg.type = MSG_MEDIA_SRV_SET_GLOBAL_PARAMETER;
  485. if (psize > 0) {
  486. os_sem_init(&return_notify, 0, 1);
  487. msg.callback = _media_service_default_callback;
  488. msg.sync_sem = &return_notify;
  489. }
  490. if (false == send_async_msg(MEDIA_SERVICE_NAME, &msg)) {
  491. media_mem_free(srv_param);
  492. return -EBUSY;
  493. }
  494. if (psize > 0) {
  495. if (os_sem_take(&return_notify, OS_FOREVER)) {
  496. media_mem_free(srv_param);
  497. return -ETIME;
  498. }
  499. ret = srv_param->result;
  500. media_mem_free(srv_param);
  501. }
  502. return ret;
  503. }
  504. int media_player_close(media_player_t *handle)
  505. {
  506. struct app_msg msg = {0};
  507. os_sem return_notify;
  508. os_sem_init(&return_notify, 0, 1);
  509. msg.type = MSG_MEDIA_SRV_CLOSE;
  510. msg.ptr = handle->media_srv_handle;
  511. msg.callback = _media_service_default_callback;
  512. msg.sync_sem = &return_notify;
  513. os_mutex_lock(&media_srv_mutex, OS_FOREVER);
  514. if (!send_async_msg(MEDIA_SERVICE_NAME, &msg)) {
  515. SYS_LOG_ERR("MSG_MEDIA_SRV_CLOSE send failed");
  516. goto error_exit;
  517. }
  518. if (os_sem_take(&return_notify, OS_FOREVER) != 0) {
  519. goto error_exit;
  520. }
  521. if (current_media_dumpable_player == handle)
  522. current_media_dumpable_player = NULL;
  523. if (current_media_main_player == handle)
  524. current_media_main_player = NULL;
  525. #ifdef CONFIG_SYS_WAKELOCK
  526. sys_wake_unlock_ext(PARTIAL_WAKE_LOCK,MEDIA_WAKE_LOCK_USER);
  527. #endif
  528. media_player_ref_cnt--;
  529. error_exit:
  530. #ifdef CONFIG_ACTS_DVFS_DYNAMIC_LEVEL
  531. SYS_LOG_INF("dvfs level %d\n", handle->dvfs_level);
  532. dvfs_unset_level(handle->dvfs_level, "media");
  533. #endif
  534. os_mutex_unlock(&media_srv_mutex);
  535. _notify_player_lifecycle_changed(handle, PLAYER_EVENT_CLOSE, NULL, 0);
  536. media_mem_free(handle);
  537. return 0;
  538. }
  539. int media_player_dump_data(media_player_t *handle, int num, const uint8_t tags[], struct acts_ringbuf *bufs[])
  540. {
  541. struct app_msg msg = {0};
  542. os_sem return_notify;
  543. media_data_dump_info_t dump_info;
  544. media_srv_param_t *srv_param = NULL;
  545. if (!handle)
  546. handle = current_media_dumpable_player;
  547. if (!handle || !handle->media_srv_handle) {
  548. return -EINVAL;
  549. }
  550. srv_param = media_mem_malloc(sizeof(*srv_param), MCU_MEMORY);
  551. if (!srv_param)
  552. return -ENOMEM;
  553. os_sem_init(&return_notify, 0, 1);
  554. dump_info.num = num;
  555. dump_info.tags = tags;
  556. dump_info.bufs = bufs;
  557. srv_param->handle = handle->media_srv_handle;
  558. srv_param->param.type = 0;
  559. srv_param->param.pbuf = &dump_info;
  560. srv_param->param.plen = sizeof(dump_info);
  561. msg.ptr = srv_param;
  562. msg.type = MSG_MEDIA_SRV_DUMP_DATA;
  563. msg.callback = _media_service_default_callback;
  564. msg.sync_sem = &return_notify;
  565. if (false == send_async_msg(MEDIA_SERVICE_NAME, &msg)) {
  566. media_mem_free(srv_param);
  567. return -EBUSY;
  568. }
  569. if (os_sem_take(&return_notify, OS_FOREVER)) {
  570. media_mem_free(srv_param);
  571. return -ETIME;
  572. }
  573. media_mem_free(srv_param);
  574. return 0;
  575. }
  576. media_player_t *media_player_get_current_dumpable_player(void)
  577. {
  578. return current_media_dumpable_player;
  579. }
  580. media_player_t *media_player_get_current_main_player(void)
  581. {
  582. return current_media_main_player;
  583. }
  584. bool media_player_is_working(void)
  585. {
  586. return (media_player_ref_cnt != 0);
  587. }
  588. int media_player_set_lifecycle_notifier(media_srv_event_notify_t notify)
  589. {
  590. os_mutex_lock(&media_srv_mutex, OS_FOREVER);
  591. pfn_lifecycle_notify = notify;
  592. SYS_LOG_INF("%p", notify);
  593. os_mutex_unlock(&media_srv_mutex);
  594. return 0;
  595. }
  596. void media_player_force_stop(void)
  597. {
  598. os_mutex_lock(&media_srv_mutex, OS_FOREVER);
  599. media_player_t * player = media_player_get_current_main_player();
  600. if (player) {
  601. media_player_stop(player);
  602. media_player_close(player);
  603. }
  604. force_disable_player = true;
  605. os_mutex_unlock(&media_srv_mutex);
  606. }
  607. int media_player_set_mix_stream(media_player_t *handle, mix_service_param_t *init_param)
  608. {
  609. struct app_msg msg = {0};
  610. media_srv_param_t *srv_param = NULL;
  611. os_sem return_notify;
  612. if (!handle)
  613. handle = current_media_dumpable_player;
  614. if (!handle || !handle->media_srv_handle) {
  615. return -EINVAL;
  616. }
  617. srv_param = media_mem_malloc(sizeof(*srv_param), MCU_MEMORY);
  618. if (!srv_param)
  619. return -ENOMEM;
  620. os_sem_init(&return_notify, 0, 1);
  621. //for mix stream only set once at the same time
  622. os_mutex_lock(&media_srv_mutex, OS_FOREVER);
  623. srv_param->handle = handle->media_srv_handle;
  624. srv_param->param.type = 0;
  625. srv_param->param.pbuf = init_param;
  626. srv_param->param.plen = sizeof(mix_service_param_t);
  627. msg.ptr = srv_param;
  628. msg.type = MSG_MEDIA_SRV_SET_MIX_STREAM;
  629. msg.callback = _media_service_default_callback;
  630. msg.sync_sem = &return_notify;
  631. if (false == send_async_msg(MEDIA_SERVICE_NAME, &msg)) {
  632. media_mem_free(srv_param);
  633. os_mutex_unlock(&media_srv_mutex);
  634. return -EBUSY;
  635. }
  636. if (os_sem_take(&return_notify, OS_FOREVER)) {
  637. media_mem_free(srv_param);
  638. os_mutex_unlock(&media_srv_mutex);
  639. return -ETIME;
  640. }
  641. media_mem_free(srv_param);
  642. os_mutex_unlock(&media_srv_mutex);
  643. return 0;
  644. }