tts_manager.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file tts manager interface
  8. */
  9. #define SYS_LOG_DOMAIN "TTS"
  10. #include <os_common_api.h>
  11. #include <audio_system.h>
  12. #include <media_player.h>
  13. #include <buffer_stream.h>
  14. #include <file_stream.h>
  15. #include <app_manager.h>
  16. #include <mem_manager.h>
  17. #include <tts_manager.h>
  18. #ifdef CONFIG_SD_FS
  19. #include <sdfs.h>
  20. #endif
  21. #ifdef CONFIG_BT_TRANSMIT
  22. #include "../bt_transmit/bt_transmit.h"
  23. #endif
  24. struct tts_item_t {
  25. /* 1st word reserved for use by list */
  26. sys_snode_t node;
  27. uint64_t bt_clk;
  28. char *pattern;
  29. uint8_t tts_mode;
  30. uint8_t tele_num_index;
  31. char tts_file_name[0];
  32. };
  33. struct tts_manager_ctx_t {
  34. const struct tts_config_t *tts_config;
  35. sys_slist_t tts_list;
  36. struct tts_item_t *tts_curr;
  37. os_mutex tts_mutex;
  38. uint32_t tts_manager_locked:8;
  39. uint32_t tts_item_num:8;
  40. void *player_handle;
  41. io_stream_t tts_stream;
  42. tts_event_nodify lisener;
  43. os_delayed_work stop_work;
  44. #ifdef CONFIG_PLAY_TIPTONE
  45. uint32_t tip_seq:4;
  46. uint32_t tip_seq_stopped:4;
  47. uint32_t tip_reserved:8;
  48. void *tip_player_handle;
  49. io_stream_t tip_stream;
  50. char tip_file_name[12];
  51. os_delayed_work tip_stop_work;
  52. os_mutex tip_mutex;
  53. #endif
  54. };
  55. static struct tts_manager_ctx_t *tts_manager_ctx;
  56. #ifdef CONFIG_TTS_MFILE_STREAM
  57. static io_stream_t mfile_stream_create(struct buffer_t *param);
  58. #endif
  59. static struct tts_manager_ctx_t *_tts_get_ctx(void)
  60. {
  61. return tts_manager_ctx;
  62. }
  63. static void _tts_event_nodify_callback(struct app_msg *msg, int result, void *reply)
  64. {
  65. if (msg->sync_sem) {
  66. os_sem_give((os_sem *)msg->sync_sem);
  67. }
  68. }
  69. static void _tts_event_nodify(uint32_t event, uint8_t sync_mode)
  70. {
  71. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  72. uint8_t *current_app = app_manager_get_current_app();
  73. if (current_app) {
  74. struct app_msg msg = {0};
  75. msg.type = MSG_TTS_EVENT;
  76. msg.value = event;
  77. if (sync_mode) {
  78. os_sem return_notify;
  79. os_sem_init(&return_notify, 0, 1);
  80. msg.callback = _tts_event_nodify_callback;
  81. msg.sync_sem = &return_notify;
  82. if (!send_async_msg(current_app, &msg)) {
  83. return;
  84. }
  85. os_mutex_unlock(&tts_ctx->tts_mutex);
  86. if (os_sem_take(&return_notify, OS_FOREVER) != 0) {
  87. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  88. return;
  89. }
  90. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  91. } else {
  92. if (!send_async_msg(current_app, &msg)) {
  93. return;
  94. }
  95. }
  96. }
  97. }
  98. static io_stream_t _tts_create_stream(uint8_t *filename, bool tele_num)
  99. {
  100. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  101. io_stream_t stream = NULL;
  102. #ifdef CONFIG_FILE_STREAM
  103. uint8_t *tts_fs_file = NULL;
  104. uint32_t size;
  105. #endif
  106. if (tts_ctx->tts_config->tts_storage_media == TTS_SOTRAGE_SDFS) {
  107. struct buffer_t buffer;
  108. #ifdef CONFIG_SD_FS
  109. if (sd_fmap(filename, (void **)&buffer.base, &buffer.length) != 0) {
  110. SYS_LOG_ERR("file no exist\n");
  111. goto exit;
  112. }
  113. #endif
  114. buffer.cache_size = 0;
  115. #ifdef CONFIG_TTS_MFILE_STREAM
  116. if(tele_num) {
  117. stream = mfile_stream_create(&buffer);
  118. } else {
  119. stream = buffer_stream_create(&buffer);
  120. }
  121. #else
  122. stream = buffer_stream_create(&buffer);
  123. #endif
  124. if (!stream) {
  125. SYS_LOG_ERR("create failed\n");
  126. goto exit;
  127. }
  128. } else if (tts_ctx->tts_config->tts_storage_media == TTS_SOTRAGE_SDCARD) {
  129. #ifdef CONFIG_FILE_STREAM
  130. if ((tts_ctx->tts_config->tts_def_path != NULL) && (filename[0] != '/')) {
  131. size = strlen(tts_ctx->tts_config->tts_def_path) + strlen(filename) + 1;
  132. tts_fs_file = mem_malloc(size);
  133. if (!tts_fs_file) {
  134. SYS_LOG_ERR("tts_file alloc failed\n");
  135. goto exit;
  136. }
  137. snprintf(tts_fs_file, size, "%s%s", tts_ctx->tts_config->tts_def_path, filename);
  138. } else {
  139. size = 0;
  140. tts_fs_file = filename;
  141. }
  142. stream = file_stream_create(tts_fs_file);
  143. if (!stream) {
  144. SYS_LOG_ERR("create failed\n");
  145. goto exit;
  146. }
  147. if (size > 0) {
  148. mem_free(tts_fs_file);
  149. }
  150. #endif
  151. }
  152. if (stream) {
  153. if (stream_open(stream, MODE_IN) != 0) {
  154. SYS_LOG_ERR("open failed\n");
  155. stream_destroy(stream);
  156. stream = NULL;
  157. goto exit;
  158. }
  159. }
  160. SYS_LOG_INF("tts stream %p tts_item %s tts_path %s\n", stream, filename, tts_ctx->tts_config->tts_def_path);
  161. exit:
  162. return stream;
  163. }
  164. static void _tts_remove_current_node(struct tts_manager_ctx_t *tts_ctx, struct tts_item_t *tts_item, bool fadeout)
  165. {
  166. if (!tts_item)
  167. return;
  168. if (tts_ctx->player_handle) {
  169. if(fadeout) {
  170. media_player_fade_out(tts_ctx->player_handle, 10);
  171. os_sleep(10);
  172. }
  173. #ifdef CONFIG_BT_TRANSMIT
  174. bt_transmit_catpure_stop();
  175. #endif
  176. media_player_stop(tts_ctx->player_handle);
  177. media_player_close(tts_ctx->player_handle);
  178. tts_ctx->player_handle = NULL;
  179. }
  180. if (tts_ctx->tts_stream) {
  181. stream_close(tts_ctx->tts_stream);
  182. stream_destroy(tts_ctx->tts_stream);
  183. tts_ctx->tts_stream = NULL;
  184. }
  185. tts_manager_ctx->tts_curr = NULL;
  186. if (tts_ctx->lisener) {
  187. os_mutex_unlock(&tts_ctx->tts_mutex);
  188. tts_ctx->lisener(&tts_item->tts_file_name[0], TTS_EVENT_STOP_PLAY);
  189. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  190. }
  191. _tts_event_nodify(TTS_EVENT_STOP_PLAY, 0);
  192. mem_free(tts_item);
  193. }
  194. static void _tts_manager_play_event_notify(uint32_t event, void *data, uint32_t len, void *user_data)
  195. {
  196. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  197. SYS_LOG_DBG("event %d , data %p , len %d\n", event, data, len);
  198. switch (event) {
  199. case PLAYBACK_EVENT_STOP_COMPLETE:
  200. #ifdef CONFIG_USER_WORK_Q
  201. os_delayed_work_submit_to_queue(os_get_user_work_queue(),&tts_ctx->stop_work, OS_NO_WAIT);
  202. #else
  203. os_delayed_work_submit(&tts_ctx->stop_work, OS_NO_WAIT);
  204. #endif
  205. break;
  206. }
  207. }
  208. static void _tts_manager_triggle_play_tts(void)
  209. {
  210. struct app_msg msg = {0};
  211. msg.type = MSG_TTS_EVENT;
  212. msg.cmd = TTS_EVENT_START_PLAY;
  213. send_async_msg("main", &msg);
  214. }
  215. static int _tts_start_play(struct tts_item_t *tts_item, const char *file_name)
  216. {
  217. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  218. int ret = 0;
  219. media_init_param_t init_param;
  220. memset(&init_param, 0, sizeof(media_init_param_t));
  221. tts_ctx->tts_curr = tts_item;
  222. if(strstr(file_name, ".act")) {
  223. init_param.format = ACT_TYPE;
  224. init_param.type = MEDIA_SRV_TYPE_PLAYBACK;
  225. } else {
  226. init_param.format = MP3_TYPE;
  227. init_param.type = MEDIA_SRV_TYPE_PARSER_AND_PLAYBACK;
  228. }
  229. init_param.stream_type = AUDIO_STREAM_TTS;
  230. init_param.efx_stream_type = AUDIO_STREAM_TTS;
  231. init_param.sample_rate = 16;
  232. init_param.sample_bits = 16;
  233. init_param.channels = 1;
  234. init_param.input_stream = tts_ctx->tts_stream;
  235. init_param.output_stream = NULL;
  236. init_param.event_notify_handle = _tts_manager_play_event_notify;
  237. init_param.support_tws = (tts_item->bt_clk == UINT64_MAX) ? 0:1;
  238. init_param.dsp_output = 1;
  239. #ifdef CONFIG_BT_TRANSMIT
  240. bt_transmit_catpure_pre_start();
  241. #endif
  242. tts_ctx->player_handle = media_player_open(&init_param);
  243. if (!tts_ctx->player_handle) {
  244. _tts_remove_current_node(tts_ctx, tts_item, false);
  245. ret = -1;
  246. goto exit;
  247. }
  248. #ifdef CONFIG_BT_TRANSMIT
  249. //now dsp decode output always 2 channels
  250. bt_transmit_catpure_start(init_param.output_stream, init_param.sample_rate, 2);
  251. #endif
  252. if(init_param.support_tws) {
  253. media_player_set_sync_play_time(tts_ctx->player_handle, tts_item->bt_clk);
  254. }
  255. media_player_play(tts_ctx->player_handle);
  256. exit:
  257. return ret;
  258. }
  259. int tts_manager_play_process(void)
  260. {
  261. sys_snode_t *head;
  262. struct tts_item_t *tts_item;
  263. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  264. char tele_name[14];
  265. int res = 0;
  266. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  267. if (sys_slist_is_empty(&tts_ctx->tts_list)) {
  268. res = -ENOENT;
  269. goto exit;
  270. }
  271. SYS_LOG_INF("tts_curr %p, locked: %d\n", tts_ctx->tts_curr, tts_ctx->tts_manager_locked);
  272. if (tts_ctx->tts_curr || tts_ctx->tts_manager_locked) {
  273. res = -ENOLCK;
  274. goto exit;
  275. }
  276. head = sys_slist_peek_head(&tts_ctx->tts_list);
  277. tts_item = CONTAINER_OF(head, struct tts_item_t, node);
  278. sys_slist_find_and_remove(&tts_ctx->tts_list, head);
  279. tts_ctx->tts_item_num--;
  280. if (tts_ctx->lisener) {
  281. tts_ctx->lisener(&tts_item->tts_file_name[0], TTS_EVENT_START_PLAY);
  282. }
  283. if(tts_item->tts_mode & TTS_MODE_TELE_NUM) {
  284. sprintf(tele_name, tts_item->pattern, tts_item->tts_file_name[tts_item->tele_num_index++]);
  285. tts_ctx->tts_stream = _tts_create_stream(tele_name, true);
  286. } else {
  287. tts_ctx->tts_stream = _tts_create_stream(tts_item->tts_file_name, false);
  288. }
  289. if (!tts_ctx->tts_stream) {
  290. res = -EEXIST;
  291. goto ERROUT;
  292. }
  293. //fg app should forbid play media between TTS_EVENT_START_PLAY and TTS_EVENT_START_STOP
  294. _tts_event_nodify(TTS_EVENT_START_PLAY, 1);
  295. if(tts_item->tts_mode & TTS_MODE_TELE_NUM) {
  296. _tts_start_play(tts_item, tele_name);
  297. } else {
  298. _tts_start_play(tts_item, tts_item->tts_file_name);
  299. }
  300. exit:
  301. os_mutex_unlock(&tts_ctx->tts_mutex);
  302. return res;
  303. ERROUT:
  304. _tts_remove_current_node(tts_ctx, tts_item, false);
  305. _tts_manager_triggle_play_tts();
  306. os_mutex_unlock(&tts_ctx->tts_mutex);
  307. return res;
  308. }
  309. static void _tts_manager_stop_work(os_work *work)
  310. {
  311. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  312. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  313. _tts_remove_current_node(tts_ctx, tts_ctx->tts_curr, false);
  314. os_mutex_unlock(&tts_ctx->tts_mutex);
  315. _tts_manager_triggle_play_tts();
  316. }
  317. static struct tts_item_t *_tts_create_item(uint8_t *tts_name, uint32_t mode, uint64_t bt_clk, char *pattern)
  318. {
  319. struct tts_item_t *item;
  320. int size;
  321. size = sizeof(struct tts_item_t) + strlen(tts_name) + 1 + (pattern ? (strlen(pattern) + 1) : 0);
  322. item = mem_malloc(size);
  323. if (!item) {
  324. goto exit;
  325. }
  326. memset(item, 0, size);
  327. item->bt_clk = bt_clk;
  328. item->tts_mode = mode;
  329. strcpy(item->tts_file_name, tts_name);
  330. if(pattern) {
  331. item->pattern = (char*)item + sizeof(struct tts_item_t) + strlen(tts_name) + 1;
  332. strcpy(item->pattern, pattern);
  333. }
  334. exit:
  335. return item;
  336. }
  337. void tts_manager_unlock(void)
  338. {
  339. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  340. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  341. tts_ctx->tts_manager_locked--;
  342. SYS_LOG_INF("%d\n",tts_ctx->tts_manager_locked);
  343. os_mutex_unlock(&tts_ctx->tts_mutex);
  344. }
  345. void tts_manager_lock(void)
  346. {
  347. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  348. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  349. tts_ctx->tts_manager_locked++;
  350. SYS_LOG_INF("%d\n",tts_ctx->tts_manager_locked);
  351. os_mutex_unlock(&tts_ctx->tts_mutex);
  352. }
  353. bool tts_manager_is_locked(void)
  354. {
  355. bool result = false;
  356. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  357. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  358. result = (tts_ctx->tts_manager_locked > 0);
  359. SYS_LOG_INF("%d\n",tts_ctx->tts_manager_locked);
  360. os_mutex_unlock(&tts_ctx->tts_mutex);
  361. return result;
  362. }
  363. void tts_manager_wait_finished(bool clean_all)
  364. {
  365. int try_cnt = 10000;
  366. struct tts_item_t *temp, *tts_item;
  367. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  368. /**avoid tts wait finished death loop*/
  369. while (tts_ctx->tts_curr && try_cnt-- > 0) {
  370. os_sleep(4);
  371. }
  372. if (try_cnt == 0) {
  373. SYS_LOG_WRN("timeout\n");
  374. }
  375. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  376. if (!sys_slist_is_empty(&tts_ctx->tts_list) && clean_all) {
  377. SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&tts_ctx->tts_list, tts_item, temp, node) {
  378. if (tts_item) {
  379. sys_slist_find_and_remove(&tts_ctx->tts_list, &tts_item->node);
  380. mem_free(tts_item);
  381. }
  382. }
  383. tts_ctx->tts_item_num = 0;
  384. }
  385. os_mutex_unlock(&tts_ctx->tts_mutex);
  386. }
  387. int tts_manager_play_with_time(uint8_t *tts_name, uint32_t mode, uint64_t bt_clk, char *pattern)
  388. {
  389. int res = 0;
  390. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  391. struct tts_item_t *item = NULL;
  392. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  393. if (tts_ctx->tts_manager_locked > 0) {
  394. SYS_LOG_INF("%d\n",tts_ctx->tts_manager_locked);
  395. res = -ENOLCK;
  396. goto exit;
  397. }
  398. if (strstr(tts_name, ".pcm")) {
  399. #ifdef CONFIG_PLAY_KEYTONE
  400. key_tone_play(tts_name);
  401. #endif
  402. goto exit;
  403. }
  404. if (tts_ctx->tts_item_num >= 3) {
  405. sys_snode_t *head = sys_slist_peek_head(&tts_ctx->tts_list);
  406. item = CONTAINER_OF(head, struct tts_item_t, node);
  407. sys_slist_find_and_remove(&tts_ctx->tts_list, head);
  408. tts_ctx->tts_item_num--;
  409. SYS_LOG_ERR("drop: tts %s\n", &item->tts_file_name[0]);
  410. mem_free(item);
  411. }
  412. item = _tts_create_item(tts_name, mode, bt_clk, pattern);
  413. if (!item) {
  414. res = -ENOMEM;
  415. goto exit;
  416. }
  417. sys_slist_append(&tts_ctx->tts_list, (sys_snode_t *)item);
  418. tts_ctx->tts_item_num++;
  419. exit:
  420. os_mutex_unlock(&tts_ctx->tts_mutex);
  421. if((res == 0) && !tts_ctx->tts_curr) {
  422. if(mode & PLAY_IMMEDIATELY) {
  423. res = tts_manager_play_process();
  424. } else {
  425. _tts_manager_triggle_play_tts();
  426. }
  427. }
  428. return res;
  429. }
  430. static bool block_tts_finised = false;
  431. static void _tts_manager_play_block_event_notify(uint32_t event, void *data, uint32_t len, void *user_data)
  432. {
  433. switch (event) {
  434. case PLAYBACK_EVENT_STOP_COMPLETE:
  435. block_tts_finised = true;
  436. break;
  437. }
  438. }
  439. int tts_manager_play_block(uint8_t *tts_name)
  440. {
  441. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  442. int ret = 0;
  443. media_init_param_t init_param;
  444. io_stream_t stream = NULL;
  445. struct buffer_t buffer;
  446. void *player_handle = NULL;
  447. int try_cnt = 0;
  448. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  449. if (tts_ctx->tts_manager_locked > 0) {
  450. SYS_LOG_INF("%d\n",tts_ctx->tts_manager_locked);
  451. ret = -ENOLCK;
  452. goto exit;
  453. }
  454. if (sd_fmap(tts_name, (void **)&buffer.base, &buffer.length) != 0) {
  455. goto exit;
  456. }
  457. buffer.cache_size = 0;
  458. stream = buffer_stream_create(&buffer);
  459. if (!stream) {
  460. SYS_LOG_ERR("create failed\n");
  461. goto exit;
  462. }
  463. if (stream_open(stream, MODE_IN) != 0) {
  464. SYS_LOG_ERR("open failed\n");
  465. stream_destroy(stream);
  466. stream = NULL;
  467. goto exit;
  468. }
  469. memset(&init_param, 0, sizeof(media_init_param_t));
  470. if(strstr(tts_name, ".act")) {
  471. init_param.format = ACT_TYPE;
  472. } else {
  473. init_param.format = MP3_TYPE;
  474. }
  475. init_param.type = MEDIA_SRV_TYPE_PLAYBACK;
  476. init_param.stream_type = AUDIO_STREAM_TTS;
  477. init_param.efx_stream_type = AUDIO_STREAM_TTS;
  478. init_param.sample_rate = 16;
  479. init_param.sample_bits = 16;
  480. init_param.channels = 1;
  481. init_param.input_stream = stream;
  482. init_param.output_stream = NULL;
  483. init_param.event_notify_handle = _tts_manager_play_block_event_notify;
  484. #ifdef CONFIG_BT_TRANSMIT
  485. bt_transmit_catpure_pre_start();
  486. #endif
  487. player_handle = media_player_open(&init_param);
  488. if (!player_handle) {
  489. goto exit;
  490. }
  491. #ifdef CONFIG_BT_TRANSMIT
  492. bt_transmit_catpure_start(init_param.output_stream, init_param.sample_rate, 2);
  493. #endif
  494. block_tts_finised = false;
  495. media_player_play(player_handle);
  496. while (!block_tts_finised && try_cnt ++ < 100) {
  497. os_sleep(100);
  498. }
  499. #ifdef CONFIG_BT_TRANSMIT
  500. bt_transmit_catpure_stop();
  501. #endif
  502. media_player_stop(player_handle);
  503. media_player_close(player_handle);
  504. exit:
  505. if (stream) {
  506. stream_close(stream);
  507. stream_destroy(stream);
  508. }
  509. os_mutex_unlock(&tts_ctx->tts_mutex);
  510. return ret;
  511. }
  512. int tts_manager_stop(uint8_t *tts_name)
  513. {
  514. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  515. struct tts_item_t *tts_item;
  516. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  517. if (!tts_ctx->tts_curr)
  518. goto exit;
  519. tts_item = tts_ctx->tts_curr;
  520. if (tts_item->tts_mode == UNBLOCK_UNINTERRUPTABLE
  521. || tts_item->tts_mode == BLOCK_UNINTERRUPTABLE) {
  522. goto exit;
  523. }
  524. _tts_remove_current_node(tts_ctx, tts_item, true);
  525. exit:
  526. os_mutex_unlock(&tts_ctx->tts_mutex);
  527. _tts_manager_triggle_play_tts();
  528. return 0;
  529. }
  530. int tts_manager_register_tts_config(const struct tts_config_t *config)
  531. {
  532. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  533. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  534. tts_ctx->tts_config = config;
  535. os_mutex_unlock(&tts_ctx->tts_mutex);
  536. return 0;
  537. }
  538. int tts_manager_add_event_lisener(tts_event_nodify lisener)
  539. {
  540. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  541. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  542. tts_ctx->lisener = lisener;
  543. os_mutex_unlock(&tts_ctx->tts_mutex);
  544. return 0;
  545. }
  546. int tts_manager_remove_event_lisener(tts_event_nodify lisener)
  547. {
  548. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  549. os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  550. tts_ctx->lisener = NULL;
  551. os_mutex_unlock(&tts_ctx->tts_mutex);
  552. return 0;
  553. }
  554. const struct tts_config_t tts_config = {
  555. .tts_storage_media = TTS_SOTRAGE_SDFS,
  556. };
  557. int tts_manager_process_ui_event(int ui_event)
  558. {
  559. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  560. uint8_t *tts_file_name = NULL;
  561. int mode = 0;
  562. if (!tts_ctx->tts_config || !tts_ctx->tts_config->tts_map)
  563. return -ENOENT;
  564. for (int i = 0; i < tts_ctx->tts_config->tts_map_size; i++) {
  565. const tts_ui_event_map_t *tts_ui_event_map = &tts_ctx->tts_config->tts_map[i];
  566. if (tts_ui_event_map->ui_event == ui_event) {
  567. tts_file_name = (uint8_t *)tts_ui_event_map->tts_file_name;
  568. mode = tts_ui_event_map->mode;
  569. break;
  570. }
  571. }
  572. SYS_LOG_INF(" %d %s \n",ui_event,tts_file_name);
  573. if (tts_file_name)
  574. tts_manager_play(tts_file_name, mode);
  575. return 0;
  576. }
  577. #ifdef CONFIG_PLAY_TIPTONE
  578. static void _tip_manager_stop(struct tts_manager_ctx_t *tts_ctx, bool fadeout)
  579. {
  580. tts_ctx->tip_file_name[0] = 0;
  581. if (tts_ctx->tip_player_handle) {
  582. if(fadeout) {
  583. media_player_fade_out(tts_ctx->tip_player_handle, 10);
  584. os_sleep(10);
  585. }
  586. #ifdef CONFIG_BT_TRANSMIT
  587. bt_transmit_catpure_stop();
  588. #endif
  589. media_player_stop(tts_ctx->tip_player_handle);
  590. media_player_close(tts_ctx->tip_player_handle);
  591. tts_ctx->tip_player_handle = NULL;
  592. }
  593. if (tts_ctx->tip_stream) {
  594. stream_close(tts_ctx->tip_stream);
  595. stream_destroy(tts_ctx->tip_stream);
  596. tts_ctx->tip_stream = NULL;
  597. }
  598. }
  599. static void _tip_manager_stop_work(os_work *work)
  600. {
  601. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  602. os_mutex_lock(&tts_ctx->tip_mutex, OS_FOREVER);
  603. if(tts_ctx->tip_seq_stopped == tts_ctx->tip_seq)
  604. {
  605. SYS_LOG_INF("tip stopped, tip_seq: %u, name: %s\n", tts_ctx->tip_seq, tts_ctx->tip_file_name);
  606. _tip_manager_stop(tts_ctx, false);
  607. }
  608. else
  609. {
  610. SYS_LOG_INF("no cur tip stopped, tip_seq_stopped: %u, tip_seq: %u\n", tts_ctx->tip_seq_stopped, tts_ctx->tip_seq);
  611. }
  612. os_mutex_unlock(&tts_ctx->tip_mutex);
  613. }
  614. static void _tip_manager_play_event_notify(uint32_t event, void *data, uint32_t len, void *user_data)
  615. {
  616. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  617. switch (event) {
  618. case PLAYBACK_EVENT_STOP_COMPLETE:
  619. tts_ctx->tip_seq_stopped = (uint32_t)user_data;
  620. SYS_LOG_INF("tip_seq_stopped: %u\n", tts_ctx->tip_seq_stopped);
  621. #ifdef CONFIG_USER_WORK_Q
  622. os_delayed_work_submit_to_queue(os_get_user_work_queue(),&tts_ctx->tip_stop_work, OS_NO_WAIT);
  623. #else
  624. os_delayed_work_submit(&tts_ctx->tip_stop_work, OS_NO_WAIT);
  625. #endif
  626. break;
  627. }
  628. }
  629. int tip_manager_play(uint8_t *tip_name, uint64_t bt_clk)
  630. {
  631. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  632. int ret = 0;
  633. media_init_param_t init_param;
  634. tip_manager_stop();
  635. os_mutex_lock(&tts_ctx->tip_mutex, OS_FOREVER);
  636. if(strlen(tip_name) >= 12){
  637. SYS_LOG_INF("tip name too long: %s\n", tip_name);
  638. ret = -1;
  639. goto exit;
  640. }
  641. tts_ctx->tip_stream = _tts_create_stream(tip_name, false);
  642. if (!tts_ctx->tip_stream) {
  643. ret = -1;
  644. goto exit;
  645. }
  646. tts_ctx->tip_seq++;
  647. if(tts_ctx->tip_seq == 0)
  648. tts_ctx->tip_seq = 1;
  649. strcpy(tts_ctx->tip_file_name, tip_name);
  650. memset(&init_param, 0, sizeof(media_init_param_t));
  651. if(strstr(tip_name, ".act")) {
  652. init_param.format = ACT_TYPE;
  653. init_param.sample_rate = 16;
  654. } else {
  655. init_param.format = PCM_TYPE;
  656. init_param.sample_rate = 8;
  657. }
  658. init_param.type = MEDIA_SRV_TYPE_PLAYBACK;
  659. init_param.stream_type = AUDIO_STREAM_TIP;
  660. init_param.efx_stream_type = AUDIO_STREAM_TIP;
  661. init_param.sample_bits = 16;
  662. init_param.channels = 1;
  663. init_param.input_stream = tts_ctx->tip_stream;
  664. init_param.output_stream = NULL;
  665. init_param.event_notify_handle = _tip_manager_play_event_notify;
  666. uint32_t tmp_tip_seq = tts_ctx->tip_seq;
  667. init_param.user_data = (void*)tmp_tip_seq;
  668. init_param.support_tws = (bt_clk == UINT64_MAX) ? 0:1;
  669. #ifdef CONFIG_BT_TRANSMIT
  670. bt_transmit_catpure_pre_start();
  671. #endif
  672. tts_ctx->tip_player_handle = media_player_open(&init_param);
  673. if (!tts_ctx->tip_player_handle) {
  674. ret = -1;
  675. _tip_manager_stop(tts_ctx, false);
  676. goto exit;
  677. }
  678. #ifdef CONFIG_BT_TRANSMIT
  679. bt_transmit_catpure_start(init_param.output_stream, init_param.sample_rate, 2);
  680. #endif
  681. if(init_param.support_tws) {
  682. media_player_set_sync_play_time(tts_ctx->tip_player_handle, bt_clk);
  683. }
  684. media_player_play(tts_ctx->tip_player_handle);
  685. exit:
  686. os_mutex_unlock(&tts_ctx->tip_mutex);
  687. return ret;
  688. }
  689. int tip_manager_stop(void)
  690. {
  691. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  692. os_mutex_lock(&tts_ctx->tip_mutex, OS_FOREVER);
  693. _tip_manager_stop(tts_ctx, true);
  694. os_mutex_unlock(&tts_ctx->tip_mutex);
  695. return 0;
  696. }
  697. const char* tip_manager_get_playing_filename(void)
  698. {
  699. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  700. return tts_ctx->tip_file_name;
  701. }
  702. #endif // CONFIG_PLAY_TIPTONE
  703. #ifdef CONFIG_TTS_MFILE_STREAM
  704. /** buffer info ,used by buffer stream */
  705. typedef struct
  706. {
  707. /** len of buffer*/
  708. int length;
  709. /** pointer to base of buffer */
  710. char *buffer_base;
  711. /** mutex used for sync*/
  712. os_mutex lock;
  713. }mfile_info_t;
  714. static int mfile_stream_open(io_stream_t handle, stream_mode mode)
  715. {
  716. handle->mode = mode;
  717. SYS_LOG_INF("mode %d \n",mode);
  718. return 0;
  719. }
  720. static int mfile_stream_read(io_stream_t handle, unsigned char * buf, int num)
  721. {
  722. int brw = 0;
  723. int read_len = 0;
  724. mfile_info_t *info = (mfile_info_t *)handle->data;
  725. brw = os_mutex_lock(&info->lock, OS_FOREVER);
  726. if (brw < 0) {
  727. SYS_LOG_ERR("lock failed %d \n",brw);
  728. return -brw;
  729. }
  730. if(handle->state != STATE_OPEN) {
  731. goto err_out;
  732. }
  733. #ifdef CONFIG_SD_FS
  734. if (handle->rofs >= info->length) {
  735. struct buffer_t buffer;
  736. struct tts_manager_ctx_t *tts_ctx = _tts_get_ctx();
  737. //os_mutex_lock(&tts_ctx->tts_mutex, OS_FOREVER);
  738. while (tts_ctx->tts_curr->tts_file_name[tts_ctx->tts_curr->tele_num_index] != 0) {
  739. char tele_name[14];
  740. sprintf(tele_name, tts_ctx->tts_curr->pattern,
  741. tts_ctx->tts_curr->tts_file_name[tts_ctx->tts_curr->tele_num_index++]);
  742. if (sd_fmap(tele_name, (void **)&buffer.base, &buffer.length) != 0) {
  743. SYS_LOG_ERR("file %s no exist\n", tele_name);
  744. break;
  745. }
  746. info->buffer_base = buffer.base;
  747. info->length = buffer.length;
  748. handle->total_size = buffer.length;
  749. //skip 2 bytes of header
  750. #ifdef CONFIG_DECODER_ACT_HW_ACCELERATION
  751. handle->rofs = 0;
  752. #else
  753. handle->rofs = 2;
  754. #endif
  755. printk("play next tts %s\n", tele_name);
  756. break;
  757. }
  758. //os_mutex_unlock(&tts_ctx->tts_mutex);
  759. }
  760. #endif
  761. if (handle->rofs + num > info->length) {
  762. read_len = info->length - handle->rofs;
  763. } else {
  764. read_len = num;
  765. }
  766. memcpy(buf, info->buffer_base + handle->rofs, read_len);
  767. handle->rofs += read_len;
  768. err_out:
  769. os_mutex_unlock(&info->lock);
  770. return read_len;
  771. }
  772. static int mfile_stream_close(io_stream_t handle)
  773. {
  774. int res;
  775. mfile_info_t *info = (mfile_info_t *)handle->data;
  776. res = os_mutex_lock(&info->lock, OS_FOREVER);
  777. if (res < 0) {
  778. SYS_LOG_ERR("lock failed %d \n",res);
  779. return res;
  780. }
  781. handle->wofs = 0;
  782. handle->rofs = 0;
  783. handle->state = STATE_CLOSE;
  784. os_mutex_unlock(&info->lock);
  785. return res;
  786. }
  787. static int mfile_stream_destory(io_stream_t handle)
  788. {
  789. mfile_info_t *info = (mfile_info_t *)handle->data;
  790. mem_free(info);
  791. handle->data = NULL;
  792. return 0;
  793. }
  794. static int mfile_stream_init(io_stream_t handle, void *param)
  795. {
  796. mfile_info_t *info = NULL;
  797. struct buffer_t *buffer = (struct buffer_t *)param;
  798. info = mem_malloc(sizeof(mfile_info_t));
  799. if (!info) {
  800. SYS_LOG_ERR(" malloc failed \n");
  801. return -ENOMEM;
  802. }
  803. info->buffer_base = buffer->base;
  804. info->length = buffer->length;
  805. os_mutex_init(&info->lock);
  806. handle->data = info;
  807. handle->cache_size = buffer->cache_size;
  808. handle->total_size = buffer->length;
  809. return 0;
  810. }
  811. static const stream_ops_t mfile_stream_ops = {
  812. .init = mfile_stream_init,
  813. .open = mfile_stream_open,
  814. .read = mfile_stream_read,
  815. .seek = NULL,
  816. .tell = NULL,
  817. .write = NULL,
  818. .close = mfile_stream_close,
  819. .destroy = mfile_stream_destory,
  820. };
  821. static io_stream_t mfile_stream_create(struct buffer_t *param)
  822. {
  823. return stream_create(&mfile_stream_ops, param);
  824. }
  825. #endif
  826. static struct tts_manager_ctx_t global_tts_manager_ctx;
  827. int tts_manager_init(void)
  828. {
  829. tts_manager_ctx = &global_tts_manager_ctx;
  830. memset(tts_manager_ctx, 0, sizeof(struct tts_manager_ctx_t));
  831. sys_slist_init(&tts_manager_ctx->tts_list);
  832. os_mutex_init(&tts_manager_ctx->tts_mutex);
  833. #ifdef CONFIG_PLAY_TIPTONE
  834. os_mutex_init(&tts_manager_ctx->tip_mutex);
  835. #endif
  836. os_delayed_work_init(&tts_manager_ctx->stop_work, _tts_manager_stop_work);
  837. #ifdef CONFIG_PLAY_TIPTONE
  838. os_delayed_work_init(&tts_manager_ctx->tip_stop_work, _tip_manager_stop_work);
  839. #endif
  840. tts_manager_register_tts_config(&tts_config);
  841. #ifdef CONFIG_PLAY_KEYTONE
  842. key_tone_manager_init();
  843. #endif
  844. return 0;
  845. }
  846. int tts_manager_deinit(void)
  847. {
  848. tts_manager_wait_finished(true);
  849. os_delayed_work_cancel(&tts_manager_ctx->stop_work);
  850. #ifdef CONFIG_PLAY_TIPTONE
  851. os_delayed_work_cancel(&tts_manager_ctx->tip_stop_work);
  852. #endif
  853. return 0;
  854. }