bt_player.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief bt player
  9. */
  10. #define LOG_MODULE_CUSTOMER
  11. #include <stdlib.h>
  12. #include <audio_system.h>
  13. #include <audio_policy.h>
  14. #include <volume_manager.h>
  15. #include <media_mem.h>
  16. #include <media_player.h>
  17. #include <tts_manager.h>
  18. #include <bt_manager.h>
  19. #include <app_manager.h>
  20. #include <ringbuff_stream.h>
  21. #include "bt_player.h"
  22. LOG_MODULE_REGISTER(btmusic, LOG_LEVEL_INF);
  23. struct bt_player
  24. {
  25. media_player_t *player;
  26. io_stream_t bt_stream;
  27. /* user playing state */
  28. uint8_t user_playing;
  29. uint8_t avrcp_playing;
  30. uint8_t a2dp_playing;
  31. const btmusic_callback_t *play_cb;
  32. };
  33. static struct bt_player g_bt_player;
  34. static io_stream_t _bt_player_create_inputstream(void)
  35. {
  36. int ret = 0;
  37. io_stream_t input_stream = ringbuff_stream_create_ext(
  38. media_mem_get_cache_pool(INPUT_PLAYBACK, AUDIO_STREAM_MUSIC),
  39. media_mem_get_cache_pool_size(INPUT_PLAYBACK, AUDIO_STREAM_MUSIC));
  40. if (!input_stream)
  41. {
  42. return NULL;
  43. }
  44. ret = stream_open(input_stream, MODE_IN_OUT | MODE_READ_BLOCK | MODE_BLOCK_TIMEOUT);
  45. if (ret)
  46. {
  47. stream_destroy(input_stream);
  48. input_stream = NULL;
  49. }
  50. return input_stream;
  51. }
  52. static int bt_player_start_play(void)
  53. {
  54. struct bt_player *btplayer = &g_bt_player;
  55. media_init_param_t init_param;
  56. uint8_t codec_id = bt_manager_a2dp_get_codecid(BTSRV_DEVICE_PHONE);
  57. uint8_t sample_rate = bt_manager_a2dp_get_sample_rate(BTSRV_DEVICE_PHONE);
  58. if (btplayer->player)
  59. {
  60. if (btplayer->bt_stream)
  61. {
  62. bt_manager_set_codec(codec_id);
  63. bt_manager_set_stream(STREAM_TYPE_A2DP, btplayer->bt_stream);
  64. media_player_play(btplayer->player);
  65. SYS_LOG_INF("already open\n");
  66. return 0;
  67. }
  68. SYS_LOG_INF("reset player\n");
  69. media_player_stop(btplayer->player);
  70. media_player_close(btplayer->player);
  71. btplayer->player = NULL;
  72. os_sleep(200);
  73. }
  74. media_player_t *media_player = media_player_get_current_main_player();
  75. if (media_player != NULL)
  76. {
  77. return 0;
  78. }
  79. #ifdef CONFIG_PLAYTTS
  80. tts_manager_wait_finished(false);
  81. #endif
  82. SYS_LOG_INF("btmusic: codec_id: %d sample_rate %d\n", codec_id, sample_rate);
  83. memset(&init_param, 0, sizeof(media_init_param_t));
  84. if (codec_id == 0)
  85. {
  86. init_param.format = SBC_TYPE;
  87. }
  88. else if (codec_id == 2)
  89. {
  90. init_param.format = AAC_TYPE;
  91. }
  92. else
  93. {
  94. SYS_LOG_ERR("unknown code id %d", codec_id);
  95. return -EINVAL;
  96. }
  97. btplayer->bt_stream = _bt_player_create_inputstream();
  98. if (!btplayer->bt_stream)
  99. {
  100. SYS_LOG_ERR("stream create failed");
  101. return -ENOMEM;
  102. }
  103. init_param.type = MEDIA_SRV_TYPE_PLAYBACK;
  104. init_param.stream_type = AUDIO_STREAM_MUSIC;
  105. init_param.efx_stream_type = AUDIO_STREAM_MUSIC;
  106. init_param.sample_rate = sample_rate;
  107. init_param.input_stream = btplayer->bt_stream;
  108. init_param.support_tws = 1;
  109. init_param.dumpable = 1;
  110. init_param.dsp_output = 1;
  111. if (audio_policy_get_out_audio_mode(init_param.stream_type) == AUDIO_MODE_STEREO)
  112. {
  113. init_param.channels = 2;
  114. }
  115. else
  116. {
  117. init_param.channels = 1;
  118. }
  119. btplayer->player = media_player_open(&init_param);
  120. if (!btplayer->player)
  121. {
  122. SYS_LOG_ERR("player open failed");
  123. stream_close(btplayer->bt_stream);
  124. stream_destroy(btplayer->bt_stream);
  125. btplayer->bt_stream = NULL;
  126. return -EINVAL;
  127. }
  128. bt_manager_set_codec(codec_id);
  129. bt_manager_set_stream(STREAM_TYPE_A2DP, btplayer->bt_stream);
  130. #ifdef CONFIG_BT_AVRCP
  131. bt_manager_avrcp_sync_vol_to_remote(audio_system_get_stream_volume(AUDIO_STREAM_MUSIC));
  132. #endif
  133. media_player_fade_in(btplayer->player, 30);
  134. media_player_play(btplayer->player);
  135. SYS_LOG_INF("btmusic: start\n");
  136. return 0;
  137. }
  138. static void bt_player_stop_play(void)
  139. {
  140. struct bt_player *btplayer = &g_bt_player;
  141. if (!btplayer->player)
  142. return;
  143. media_player_fade_out(btplayer->player, 30);
  144. /** reserve time to fade out*/
  145. os_sleep(60);
  146. if (btplayer->bt_stream)
  147. {
  148. stream_close(btplayer->bt_stream);
  149. }
  150. bt_manager_set_stream(STREAM_TYPE_A2DP, NULL);
  151. media_player_stop(btplayer->player);
  152. media_player_close(btplayer->player);
  153. btplayer->player = NULL;
  154. if (btplayer->bt_stream)
  155. {
  156. stream_destroy(btplayer->bt_stream);
  157. btplayer->bt_stream = NULL;
  158. }
  159. SYS_LOG_INF("btmusic: stop\n");
  160. }
  161. void bt_player_bt_event_proc(struct app_msg *msg)
  162. {
  163. SYS_LOG_INF("bt cmd %d\n", msg->cmd);
  164. switch (msg->cmd)
  165. {
  166. case BT_A2DP_STREAM_START_EVENT: /* 6 */
  167. {
  168. SYS_LOG_INF("STREAM_START\n");
  169. bt_player_start_play();
  170. #ifdef CONFIG_BT_AVRCP
  171. bt_manager_avrcp_get_id3_info();
  172. #endif
  173. if (!g_bt_player.a2dp_playing)
  174. {
  175. g_bt_player.a2dp_playing = true;
  176. if (g_bt_player.play_cb && g_bt_player.play_cb->set_status)
  177. {
  178. g_bt_player.play_cb->set_status(g_bt_player.a2dp_playing);
  179. }
  180. }
  181. break;
  182. }
  183. case BT_A2DP_STREAM_SUSPEND_EVENT: /* 7 */
  184. {
  185. SYS_LOG_INF("STREAM_SUSPEND\n");
  186. bt_player_stop_play();
  187. if (g_bt_player.a2dp_playing)
  188. {
  189. g_bt_player.a2dp_playing = false;
  190. if (g_bt_player.play_cb && g_bt_player.play_cb->set_status)
  191. {
  192. g_bt_player.play_cb->set_status(g_bt_player.a2dp_playing);
  193. }
  194. }
  195. break;
  196. }
  197. case BT_AVRCP_PLAYBACK_STATUS_CHANGED_EVENT: /* 31 */
  198. {
  199. int param = *(int *)(msg->ptr);
  200. if (param == BT_STATUS_PAUSED)
  201. {
  202. g_bt_player.avrcp_playing = 0;
  203. }
  204. else if (param == BT_STATUS_PLAYING)
  205. {
  206. bt_manager_a2dp_check_state();
  207. g_bt_player.avrcp_playing = 1;
  208. }
  209. if (g_bt_player.play_cb && g_bt_player.play_cb->set_status)
  210. {
  211. g_bt_player.play_cb->set_status(g_bt_player.avrcp_playing);
  212. }
  213. break;
  214. }
  215. case BT_AVRCP_TRACK_CHANGED_EVENT: /* 32 */
  216. {
  217. #ifdef CONFIG_BT_AVRCP
  218. bt_manager_avrcp_get_id3_info();
  219. #endif
  220. break;
  221. }
  222. case BT_AVRCP_UPDATE_PLAYBACK_POS: /* 34 */
  223. {
  224. uint32_t pos = *(uint32_t *)msg->ptr;
  225. if (g_bt_player.play_cb && g_bt_player.play_cb->set_play_pos)
  226. {
  227. g_bt_player.play_cb->set_play_pos(pos);
  228. }
  229. break;
  230. }
  231. case BT_AVRCP_UPDATE_ID3_INFO_EVENT: /* 33 */
  232. {
  233. struct bt_id3_info *pinfo = (struct bt_id3_info *)msg->ptr;
  234. if (g_bt_player.play_cb && g_bt_player.play_cb->set_play_info)
  235. {
  236. g_bt_player.play_cb->set_play_info(pinfo->item[0].data, pinfo->item[1].data, atoi(pinfo->item[4].data));
  237. }
  238. break;
  239. }
  240. case BT_RMT_VOL_SYNC_EVENT: /* 39 */
  241. {
  242. audio_system_set_stream_volume(AUDIO_STREAM_MUSIC, msg->value);
  243. if (g_bt_player.player) {
  244. media_player_set_volume(g_bt_player.player, msg->value, msg->value);
  245. }
  246. if (g_bt_player.play_cb && g_bt_player.play_cb->set_volume) {
  247. g_bt_player.play_cb->set_volume(msg->value);
  248. }
  249. break;
  250. }
  251. case BT_AVRCP_DISCONNECTION_EVENT:
  252. {
  253. g_bt_player.avrcp_playing = 0;
  254. break;
  255. }
  256. case BT_DISCONNECTION_EVENT:
  257. {
  258. g_bt_player.avrcp_playing = 0;
  259. break;
  260. }
  261. default:
  262. break;
  263. }
  264. }
  265. void btmusic_view_input_event_proc(struct app_msg *msg)
  266. {
  267. SYS_LOG_INF("input cmd %d\n", msg->cmd);
  268. switch (msg->cmd)
  269. {
  270. case MSG_BT_PLAY_PAUSE: /* 208 */
  271. {
  272. if (g_bt_player.player)
  273. {
  274. #ifdef CONFIG_BT_AVRCP
  275. bt_manager_avrcp_pause();
  276. #endif
  277. /* FIXME: just wait until receiveing BT_A2DP_STREAM_SUSPEND_EVENT
  278. * if not required stop immediately.
  279. */
  280. if (msg->value)
  281. {
  282. bt_player_stop_play();
  283. }
  284. }
  285. break;
  286. }
  287. case MSG_BT_PLAY_RESUME: /* 209 */
  288. {
  289. #ifdef CONFIG_BT_AVRCP
  290. bt_manager_avrcp_play();
  291. #endif
  292. if (!g_bt_player.player)
  293. {
  294. bt_manager_a2dp_check_state();
  295. }
  296. break;
  297. }
  298. case MSG_BT_PLAY_NEXT: /* 209 */
  299. {
  300. #ifdef CONFIG_BT_AVRCP
  301. bt_manager_avrcp_play_next();
  302. #endif
  303. break;
  304. }
  305. case MSG_BT_PLAY_PREVIOUS: /* 210 */
  306. {
  307. #ifdef CONFIG_BT_AVRCP
  308. bt_manager_avrcp_play_previous();
  309. #endif
  310. break;
  311. }
  312. case MSG_BT_PLAY_VOLUP: /* 211 */
  313. {
  314. system_volume_up(AUDIO_STREAM_MUSIC, 1);
  315. break;
  316. }
  317. case MSG_BT_PLAY_VOLDOWN: /* 212 */
  318. {
  319. system_volume_down(AUDIO_STREAM_MUSIC, 1);
  320. break;
  321. }
  322. case MSG_BT_PLAY_VOLSYNC: /* 213 */
  323. {
  324. system_volume_set(AUDIO_STREAM_MUSIC, msg->value, 0);
  325. break;
  326. }
  327. case MSG_BT_PLAY_SYNC_REMOTE_STATE: {
  328. #ifdef CONFIG_BT_AVRCP
  329. bt_manager_avrcp_get_id3_info();
  330. #endif
  331. if (!g_bt_player.player) {
  332. SYS_LOG_INF("check a2dp state");
  333. bt_manager_a2dp_check_state();
  334. }
  335. break;
  336. }
  337. case MSG_BT_PLAY_STOP_PLAYER: /* 208 */
  338. {
  339. if (g_bt_player.player) {
  340. bt_player_stop_play();
  341. }
  342. break;
  343. }
  344. default:
  345. break;
  346. }
  347. }
  348. void btmusic_start(void)
  349. {
  350. SYS_LOG_INF("start %p %d %d %d", g_bt_player.player, g_bt_player.user_playing,
  351. g_bt_player.a2dp_playing, g_bt_player.avrcp_playing);
  352. if (!g_bt_player.user_playing || !g_bt_player.a2dp_playing || !g_bt_player.avrcp_playing) {
  353. struct app_msg msg = {
  354. .type = MSG_INPUT_EVENT,
  355. .cmd = MSG_BT_PLAY_RESUME,
  356. .reserve = 1,
  357. };
  358. g_bt_player.user_playing = 1;
  359. send_async_msg(app_manager_get_current_app(), &msg);
  360. }
  361. }
  362. void btmusic_stop(bool forced)
  363. {
  364. SYS_LOG_INF("stop %p %d, force %d", g_bt_player.player, g_bt_player.user_playing, forced);
  365. if (g_bt_player.user_playing || g_bt_player.player) {
  366. struct app_msg msg = {
  367. .type = MSG_INPUT_EVENT,
  368. .cmd = MSG_BT_PLAY_PAUSE,
  369. .reserve = 1,
  370. .value = forced,
  371. };
  372. g_bt_player.user_playing = 0;
  373. send_async_msg(app_manager_get_current_app(), &msg);
  374. }
  375. }
  376. void btmusic_play_next(void)
  377. {
  378. struct app_msg msg = {
  379. .type = MSG_INPUT_EVENT,
  380. .cmd = MSG_BT_PLAY_NEXT,
  381. .reserve = 1,
  382. };
  383. SYS_LOG_INF("play next");
  384. send_async_msg(app_manager_get_current_app(), &msg);
  385. }
  386. void btmusic_play_prev(void)
  387. {
  388. struct app_msg msg = {
  389. .type = MSG_INPUT_EVENT,
  390. .cmd = MSG_BT_PLAY_PREVIOUS,
  391. .reserve = 1,
  392. };
  393. SYS_LOG_INF("play prev");
  394. send_async_msg(app_manager_get_current_app(), &msg);
  395. }
  396. void btmusic_vol_adjust(bool is_add)
  397. {
  398. struct app_msg msg = {
  399. .type = MSG_INPUT_EVENT,
  400. .cmd = is_add ? MSG_BT_PLAY_VOLUP : MSG_BT_PLAY_VOLDOWN,
  401. .reserve = 1,
  402. };
  403. SYS_LOG_INF("is_add %d\n", is_add);
  404. send_async_msg(app_manager_get_current_app(), &msg);
  405. }
  406. void btmusic_vol_sync(int music_vol, bool forced)
  407. {
  408. struct app_msg msg = {
  409. .type = MSG_INPUT_EVENT,
  410. .cmd = MSG_BT_PLAY_VOLSYNC,
  411. .reserve = 1,
  412. .value = music_vol,
  413. };
  414. SYS_LOG_INF("vol %d\n", music_vol);
  415. if (forced)
  416. {
  417. send_async_msg(app_manager_get_current_app(), &msg);
  418. }
  419. else
  420. {
  421. msg_manager_send_async_msg_discardable(app_manager_get_current_app(), &msg);
  422. }
  423. }
  424. void btmusic_check_remote_state(void)
  425. {
  426. struct app_msg msg = {
  427. .type = MSG_INPUT_EVENT,
  428. .cmd = MSG_BT_PLAY_SYNC_REMOTE_STATE,
  429. .reserve = 1,
  430. };
  431. send_async_msg(app_manager_get_current_app(), &msg);
  432. }
  433. bool btmusic_get_avrcp_play_state(void)
  434. {
  435. return g_bt_player.avrcp_playing ? true : false;
  436. }
  437. bool btmusic_get_a2dp_play_state(void)
  438. {
  439. return g_bt_player.a2dp_playing ? true : false;
  440. }
  441. bool btmusic_get_player_state(void)
  442. {
  443. return g_bt_player.player ? true : false;
  444. }
  445. void btmusic_set_play_callback(const btmusic_callback_t *callback)
  446. {
  447. g_bt_player.play_cb = callback;
  448. }