bluetooth_tws_observer.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file bluetooth tws observer interface
  8. */
  9. #include "bluetooth_tws_observer.h"
  10. #include "utils/acts_ringbuf.h"
  11. #define SYS_LOG_NO_NEWLINE
  12. #ifdef SYS_LOG_DOMAIN
  13. #undef SYS_LOG_DOMAIN
  14. #endif
  15. #define SYS_LOG_DOMAIN "bt_tws"
  16. /*
  17. 1、如果两边都处于等待播放状态,设备收到对方的splay后,
  18. 若收到的对方的起始播放时钟小于当前bt时钟,则做出错处理
  19. 包号相同,则用时间较晚的中断时钟作为开始播放时间,重新设置tws中断时间
  20. 若包号不同,则以包号大的作为起始播放包号和时钟,设置为PLAYER_TWS_USE_LOCAL_SPLAY模式,丢弃包号小的码流包
  21. 2、如果当前设备已经开始播放,收到到对方的splay包,
  22. 则从当前缓存的码流包中,挑选一个比较靠后的包,计算播放时间,给对方发一个PLAYER_TWS_USE_LOCAL_SPLAY
  23. 若码流缓存信息不足,则向对方发送PLAYER_TWS_NEED_NEW_SPLAY,对方收到消息后,重新发送splay过来
  24. */
  25. //tws中断启动最小间隔,us
  26. #define TWS_MIN_INTERVAL (20000)
  27. /* splay_flag的说明
  28. * 1: 本端处于PLAYING状态, 暂时只支持对端处于WAIT PLAY状态, 希望对端重新发一次splay包过来, 因为本端缓存的包里面没有对端的包号
  29. * 2: 本端处于PLAYING状态, 希望对端以本端的包号和时间来播放
  30. * 3: 本端处于WAIT PLAY状态, 若对端不是PLAYING, 希望对端考虑是否接受本端的包号和时间, 若对端是PLAYING, 希望对端提供可以播放的包号和时间
  31. */
  32. #define PLAYER_TWS_NEED_NEW_SPLAY 1
  33. #define PLAYER_TWS_USE_LOCAL_SPLAY 2
  34. #define PLAYER_TWS_USE_START_SPLAY 3
  35. #define PLAYER_TWS_SYNCITEM_MAXNUM 10 /* TWS同步信息包最大缓存个数 */
  36. #define PLAYER_TWS_ERRCOUNT_MAXNUM 10 /* TWS失去同步状态的超时时间 */
  37. #define PLAYER_TWS_DATA_MAXNUM 5 /* 暂存从对端接收到的消息个数 */
  38. #define PLAYER_TWS_MSG_MAXNUM 5 /* 暂存从中间件api接收到的消息个数 */
  39. #define BT_TWS_TIMER_INTERVAL (3)
  40. typedef enum
  41. {
  42. TWS_STARTPLAY_PKT = 1, /* 启动播放包 */
  43. TWS_STARTSTOP_PKT, /* 停止播放包 */
  44. TWS_SYNCINFO_PKT, /* 同步信息包 */
  45. TWS_PLAYRATE_PKT, /* 速度调节包 */
  46. TWS_INTTIME_PKT, /* 中断时间包 */
  47. TWS_BTTIME_PKT, /* 蓝牙时间包 */
  48. } player_tws_type_e;
  49. /* TWS数据包, 需和tws_sync_cmd_t保持一致
  50. */
  51. typedef struct
  52. {
  53. uint8_t group; /* 参考 player_tws_group_e */
  54. uint8_t type; /* 参考 player_tws_type_e */
  55. uint8_t reserve;
  56. uint8_t payload_len;
  57. uint8_t payload_data[16]; /* buf需要4字节对齐, 避免直接赋值导致死机 */
  58. } player_tws_pkt_t;
  59. typedef struct
  60. {
  61. uint8_t scene; /* 播放场景, 避免不同场景的数据包误判, player_id_e */
  62. uint8_t splay_flag; /* 本端发送SPLAY的时候告知对端的操作类型, PLAYER_TWS_NEED_NEW_SPLAY... */
  63. uint16_t pkt_num; /* 开始播放的首包号 */
  64. uint64_t splay_time; /* 开始播放的时钟 us */
  65. uint32_t first_clk; /* 本端接收到第一个数据包时的时钟 */
  66. } player_tws_startplay_pkt_t;
  67. typedef struct
  68. {
  69. uint8_t scene; /* 播放场景, 避免不同场景的数据包误判 */
  70. uint8_t reserve[3];
  71. uint64_t startstop_time; /* 开始播放的时钟 us */
  72. } player_tws_startstop_pkt_t;
  73. typedef struct
  74. {
  75. uint8_t scene;
  76. uint16_t pkt_num; /* 表示播放的数据包号 */
  77. uint8_t reserve;
  78. uint64_t bttime_us; /* 表示播放对应包号时对应的蓝牙时钟 us */
  79. } player_tws_syncinfo_pkt_t;
  80. typedef struct
  81. {
  82. uint8_t scene;
  83. uint8_t aps_level;
  84. uint8_t reserve[2];
  85. uint64_t inttime; /* 中断时间 us */
  86. } player_tws_playrate_pkt_t;
  87. typedef enum
  88. {
  89. PLAYER_TWS_STATUS_INIT, /* 执行init后的状态 */
  90. PLAYER_TWS_STATUS_WAIT, /* 执行wait play后的状态 */
  91. PLAYER_TWS_STATUS_DEAL_WAIT,/* 执行deal start play后的状态 */
  92. PLAYER_TWS_STATUS_PLAY, /* 真正播放数据的状态 */
  93. } player_tws_status_e;
  94. typedef struct
  95. {
  96. uint16_t pkt_num; /* 包号 */
  97. uint16_t pkt_len; /* 包的数据长度 */
  98. uint16_t samples; /* 样点数 */
  99. uint64_t pkt_bttime_us; /* 蓝牙时钟 us */
  100. } player_tws_pinfo_unit_t;
  101. /* TWS消息
  102. */
  103. typedef struct
  104. {
  105. uint8_t type; /* 参考 player_tws_type_e */
  106. uint8_t data[15];
  107. } __attribute__((packed)) player_tws_msg_t;
  108. typedef struct {
  109. bt_tws_observer_t tws_observer;
  110. media_observer_t *media_observer;
  111. struct k_timer timer;
  112. uint8_t format;
  113. uint16_t sample_rate; //hz
  114. uint16_t first_pktnum;
  115. uint32_t first_clk;
  116. //以下信息会被中断函数访问
  117. uint8_t tws_status;
  118. int8_t aps_level_pending; /* 即将要设置的aps等级 */
  119. uint64_t aps_inttime;
  120. //以下信息会被多线程访问
  121. struct acts_ringbuf tws_data_buffer;
  122. player_tws_pkt_t tws_data[PLAYER_TWS_DATA_MAXNUM];
  123. uint8_t tws_role;
  124. uint8_t last_sinfo_flag;
  125. uint8_t first_pkt_flag;
  126. uint8_t sync_error_count; /* 同步错误计数 */
  127. int8_t adjust_shake_flag; /* 防抖,master和slave前后两个包的时间差一致 */
  128. uint64_t splay_local_time; /* 近端开始播放的时间 */
  129. uint64_t forbit_aps_time; /* 在此时间前禁止调节水位aps */
  130. struct acts_ringbuf sinfo_master_buffer;
  131. struct acts_ringbuf sinfo_slave_buffer;
  132. struct acts_ringbuf msg_buffer;
  133. player_tws_syncinfo_pkt_t sinfo_master[PLAYER_TWS_SYNCITEM_MAXNUM];
  134. player_tws_syncinfo_pkt_t sinfo_slave[PLAYER_TWS_SYNCITEM_MAXNUM];
  135. player_tws_msg_t msg_data[PLAYER_TWS_MSG_MAXNUM];
  136. /* 包信息单元
  137. */
  138. uint8_t pinfo_unit_idx;
  139. uint8_t save_pinfo_count; /* 包信息保存计数 */
  140. player_tws_pinfo_unit_t pinfo_unit[2];
  141. uint16_t BM_TWS_WPlay_Mintime;
  142. uint16_t BM_TWS_WPlay_Maxtime;
  143. uint16_t BM_TWS_Sync_interval;
  144. } bt_tws_observer_inner_t;
  145. static bt_tws_observer_inner_t observer_inner;
  146. /*----------------------------------------------------------------------------------------------------*/
  147. //callback from irq
  148. static void _tws_irq_handle(uint64_t bt_clk_us)
  149. {
  150. bt_tws_observer_inner_t *handle = &observer_inner;
  151. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  152. if(handle->tws_status == PLAYER_TWS_STATUS_DEAL_WAIT)
  153. {
  154. handle->tws_status = PLAYER_TWS_STATUS_PLAY;
  155. handle->media_observer->start_playback(handle->media_observer->media_handle);
  156. printf("[ATW]start playback\n");
  157. return;
  158. }
  159. /* 调节速度中断(TWS播放时才有效)
  160. */
  161. if((handle->aps_level_pending >= 0)
  162. && (handle->aps_inttime == bt_clk_us)
  163. && (handle->tws_status == PLAYER_TWS_STATUS_PLAY))
  164. {
  165. handle->media_observer->set_base_aps_level(handle->media_observer->media_handle, handle->aps_level_pending);
  166. printf("[ATW]aps int:%d\n", handle->aps_level_pending);
  167. handle->aps_level_pending = -1;
  168. }
  169. }
  170. //callback from normal thread
  171. static void _tws_recv_pkt_handle(void *buf, int32_t size)
  172. {
  173. bt_tws_observer_inner_t *handle = &observer_inner;
  174. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  175. player_tws_pkt_t *pkt = (player_tws_pkt_t*)buf;
  176. if(handle->tws_status == PLAYER_TWS_STATUS_INIT)
  177. return;
  178. if(size != sizeof(player_tws_pkt_t))
  179. {
  180. SYS_LOG_ERR("size: %d invalid, normal size: %d", size, sizeof(player_tws_pkt_t));
  181. return;
  182. }
  183. if(acts_ringbuf_space(&handle->tws_data_buffer) < sizeof(player_tws_pkt_t))
  184. {
  185. SYS_LOG_ERR("tws data buffer full");
  186. return;
  187. }
  188. acts_ringbuf_put(&handle->tws_data_buffer, buf, size);
  189. }
  190. /*----------------------------------------------------------------------------------------------------*/
  191. static int32_t _tws_send_start_play(bt_tws_observer_inner_t *handle, player_tws_startplay_pkt_t *startplay)
  192. {
  193. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  194. player_tws_pkt_t pkt;
  195. int32_t ret;
  196. SYS_LOG_INF("set SPlay, pktnum: %d, time: %lld, fclk: %d, flag: %d",
  197. startplay->pkt_num, startplay->splay_time, startplay->first_clk, startplay->splay_flag);
  198. startplay->scene = handle->format;
  199. pkt.group = 0;
  200. pkt.type = TWS_STARTPLAY_PKT;
  201. pkt.payload_len = sizeof(player_tws_startplay_pkt_t);
  202. memcpy(pkt.payload_data, startplay, sizeof(player_tws_startplay_pkt_t));
  203. ret = observer->send_pkt(&pkt, sizeof(pkt));
  204. if(ret != sizeof(pkt))
  205. return -1;
  206. return 0;
  207. }
  208. /* 返回0: 表示数据包没有发送出去
  209. */
  210. static int32_t _tws_send_start_stop(bt_tws_observer_inner_t *handle, player_tws_startstop_pkt_t *startstop)
  211. {
  212. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  213. player_tws_pkt_t pkt;
  214. int32_t ret;
  215. SYS_LOG_INF("set SStop: %lld", startstop->startstop_time);
  216. startstop->scene = handle->format;
  217. pkt.group = 0;
  218. pkt.type = TWS_STARTSTOP_PKT;
  219. pkt.payload_len = sizeof(player_tws_startstop_pkt_t);
  220. memcpy(pkt.payload_data, startstop, sizeof(player_tws_startstop_pkt_t));
  221. ret = observer->send_pkt(&pkt, sizeof(pkt));
  222. if(ret != sizeof(pkt))
  223. return -1;
  224. return 0;
  225. }
  226. /* 返回0: 表示数据包没有发送出去
  227. */
  228. static int32_t _tws_send_sync_info(bt_tws_observer_inner_t *handle, player_tws_syncinfo_pkt_t *syncinfo)
  229. {
  230. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  231. player_tws_pkt_t pkt;
  232. int32_t ret;
  233. SYS_LOG_INF("set SInfo, pktnum: %d, time: %lld", syncinfo->pkt_num, syncinfo->bttime_us);
  234. syncinfo->scene = handle->format;
  235. pkt.group = 0;
  236. pkt.type = TWS_SYNCINFO_PKT;
  237. pkt.payload_len = sizeof(player_tws_syncinfo_pkt_t);
  238. memcpy(pkt.payload_data, syncinfo, sizeof(player_tws_syncinfo_pkt_t));
  239. ret = observer->send_pkt(&pkt, sizeof(pkt));
  240. if(ret != sizeof(pkt))
  241. return -1;
  242. return 0;
  243. }
  244. /* 返回0: 表示数据包没有发送出去
  245. */
  246. static int32_t _tws_send_play_rate(bt_tws_observer_inner_t *handle, player_tws_playrate_pkt_t *playrate)
  247. {
  248. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  249. player_tws_pkt_t pkt;
  250. int32_t ret;
  251. SYS_LOG_INF("set PRate, aps: %d, time: %lld\n", playrate->aps_level, playrate->inttime);
  252. playrate->scene = handle->format;
  253. pkt.group = 0;
  254. pkt.type = TWS_PLAYRATE_PKT;
  255. pkt.payload_len = sizeof(player_tws_playrate_pkt_t);
  256. memcpy(pkt.payload_data, (uint8_t *)playrate, sizeof(player_tws_playrate_pkt_t));
  257. ret = observer->send_pkt(&pkt, sizeof(pkt));
  258. if(ret != sizeof(pkt))
  259. return -1;
  260. return 0;
  261. }
  262. /* 检测包信息: 0 表示不保存包信息单元, -1 表示需要重启播放器, 1 表示正常存储包信息单元
  263. */
  264. static int32_t _tws_check_pinfo_unit(bt_tws_observer_inner_t *handle, uint16_t cur_pkt_num, uint16_t cur_pkt_len, uint16_t samples)
  265. {
  266. if(cur_pkt_len == 0)
  267. return 0;
  268. if((cur_pkt_num == handle->pinfo_unit[handle->pinfo_unit_idx].pkt_num)
  269. && (handle->pinfo_unit[handle->pinfo_unit_idx].pkt_len != 0))
  270. {
  271. handle->pinfo_unit[handle->pinfo_unit_idx].samples += samples;
  272. return 0;
  273. }
  274. if(cur_pkt_num != (handle->pinfo_unit[handle->pinfo_unit_idx].pkt_num + 1))
  275. {
  276. /* 正在播放如果包号不连续立即重启播放(!=1:避免包号循环, !=0:避免包号开始)
  277. */
  278. if((cur_pkt_num != 1) && (handle->pinfo_unit[handle->pinfo_unit_idx].pkt_num != 0))
  279. return -1;
  280. }
  281. return 1;
  282. }
  283. static int32_t _tws_update_sync_info(bt_tws_observer_inner_t *handle, tws_pkt_info_t *pkt_info)
  284. {
  285. /* 更新idx及其内容
  286. */
  287. handle->pinfo_unit_idx ++;
  288. if(handle->pinfo_unit_idx > 1)
  289. handle->pinfo_unit_idx = 0;
  290. handle->pinfo_unit[handle->pinfo_unit_idx].pkt_len = pkt_info->pkt_len;
  291. handle->pinfo_unit[handle->pinfo_unit_idx].pkt_num = pkt_info->pkt_num;
  292. handle->pinfo_unit[handle->pinfo_unit_idx].samples = pkt_info->samples;
  293. handle->pinfo_unit[handle->pinfo_unit_idx].pkt_bttime_us = pkt_info->pkt_bttime_us;
  294. /* 确保3次之后才处理SPLAY包避免开始的包不全导致计算对端播放时间出错
  295. */
  296. handle->save_pinfo_count ++;
  297. if(handle->save_pinfo_count > 3)
  298. handle->save_pinfo_count = 3;
  299. return 0;
  300. }
  301. /* 返回-1:slave_pool或maste_pool没有item
  302. 返回 0:遍历完后没有找到相同包号的item
  303. 返回 1:找到相同包号的item并从参数传出来
  304. */
  305. static int32_t _tws_search_sinfo_item(bt_tws_observer_inner_t *handle, player_tws_syncinfo_pkt_t *slave, player_tws_syncinfo_pkt_t *maste)
  306. {
  307. uint8_t slave_keep = 1, maste_keep = 1;
  308. player_tws_syncinfo_pkt_t slave_item, maste_item;
  309. uint8_t item_size = sizeof(player_tws_syncinfo_pkt_t);
  310. struct acts_ringbuf *slave_pool = &handle->sinfo_slave_buffer;
  311. struct acts_ringbuf *maste_pool = &handle->sinfo_master_buffer;
  312. for(;;)
  313. {
  314. if(slave_keep == 1)
  315. {
  316. if(acts_ringbuf_length(slave_pool) < item_size)
  317. return -1;
  318. slave_keep = 0;
  319. acts_ringbuf_peek(slave_pool, &slave_item, item_size);
  320. }
  321. if(maste_keep == 1)
  322. {
  323. if(acts_ringbuf_length(maste_pool) < item_size)
  324. return -1;
  325. maste_keep = 0;
  326. acts_ringbuf_peek(maste_pool, &maste_item, item_size);
  327. }
  328. if(slave_item.pkt_num > maste_item.pkt_num)
  329. {
  330. maste_keep = 1;
  331. acts_ringbuf_get(maste_pool, NULL, item_size);
  332. printf("[ATW]item mkp:%d_%d\n", maste_item.pkt_num, slave_item.pkt_num);
  333. }
  334. else if(slave_item.pkt_num < maste_item.pkt_num)
  335. {
  336. slave_keep = 1;
  337. acts_ringbuf_get(slave_pool, NULL, item_size);
  338. printf("[ATW]item skp:%d_%d\n", maste_item.pkt_num, slave_item.pkt_num);
  339. }
  340. else
  341. {
  342. printf("[ATW]item get:%d\n", maste_item.pkt_num);
  343. acts_ringbuf_get(maste_pool, NULL, item_size);
  344. acts_ringbuf_get(slave_pool, NULL, item_size);
  345. memcpy(slave, &slave_item, item_size);
  346. memcpy(maste, &maste_item, item_size);
  347. break;
  348. }
  349. }
  350. return 1;
  351. }
  352. static int32_t _tws_align_samples(bt_tws_observer_inner_t *handle)
  353. {
  354. player_tws_syncinfo_pkt_t slave_item, maste_item;
  355. int32_t clk_diff;
  356. int32_t result = 0;
  357. uint8_t item_size = sizeof(player_tws_syncinfo_pkt_t);
  358. int8_t shake_flag;
  359. result = _tws_search_sinfo_item(handle, &slave_item, &maste_item);
  360. if(result <= 0)
  361. {
  362. handle->sync_error_count = (result == 0) ? (handle->sync_error_count + 1) : 0;
  363. if(handle->sync_error_count >= PLAYER_TWS_ERRCOUNT_MAXNUM)
  364. {
  365. SYS_LOG_ERR("long time no sync info");
  366. handle->sync_error_count = 0;
  367. //todo: restart playback
  368. }
  369. return 0;
  370. }
  371. handle->sync_error_count = 0;
  372. if(slave_item.bttime_us == maste_item.bttime_us)
  373. return 0;
  374. clk_diff = (int32_t)(maste_item.bttime_us - slave_item.bttime_us);
  375. shake_flag = (clk_diff < 0) ? 1 : -1;
  376. if((slave_item.pkt_num % handle->BM_TWS_Sync_interval) == 0)
  377. {
  378. handle->adjust_shake_flag = shake_flag;
  379. printf("[ATW]adjust e1:%d_%d\n", slave_item.pkt_num, clk_diff);
  380. return 0;
  381. }
  382. if(handle->adjust_shake_flag == 0)
  383. return 0;
  384. if(handle->adjust_shake_flag == shake_flag)
  385. {
  386. printf("[ATW]adjust e2:%d_%d\n", slave_item.pkt_num, clk_diff);
  387. handle->media_observer->notify_time_diff(handle->media_observer->media_handle, clk_diff);
  388. }
  389. handle->adjust_shake_flag = 0;
  390. return 0;
  391. }
  392. /* 根据本端和对端的当前信息计算对端的SPLAY
  393. */
  394. static int32_t _tws_calculate_new_splay(bt_tws_observer_inner_t *handle, player_tws_startplay_pkt_t *startplay)
  395. {
  396. uint8_t first_idx = (handle->pinfo_unit_idx == 1) ? 0 : 1;
  397. /* 要求对端以指定帧播放(当本端还没有足够的播放信息时, 让对端重新发起SPLAY)
  398. */
  399. if(handle->save_pinfo_count < 3)
  400. {
  401. printf("[ATW]need new splay\n");
  402. startplay->splay_flag = PLAYER_TWS_NEED_NEW_SPLAY;
  403. return 0;
  404. }
  405. startplay->splay_flag = PLAYER_TWS_USE_LOCAL_SPLAY;
  406. startplay->first_clk = handle->first_clk;
  407. if(startplay->pkt_num < (handle->pinfo_unit[handle->pinfo_unit_idx].pkt_num + 10))
  408. startplay->pkt_num = handle->pinfo_unit[handle->pinfo_unit_idx].pkt_num + 10;
  409. //todo: 根据已经接收的码流计算时间,sbc包长可能会变
  410. startplay->splay_time = handle->pinfo_unit[first_idx].pkt_bttime_us
  411. + (startplay->pkt_num - handle->pinfo_unit[first_idx].pkt_num)
  412. * handle->pinfo_unit[first_idx].samples * 1000000ll / handle->sample_rate;
  413. printf("[ATW]new splay:%u_%u_%u_%u, %u_%u_%u\n",
  414. startplay->pkt_num, startplay->first_clk,
  415. (uint32_t)(startplay->splay_time >> 32) & 0xffffffff,
  416. (uint32_t)startplay->splay_time & 0xffffffff,
  417. handle->pinfo_unit[first_idx].pkt_num,
  418. handle->first_clk,
  419. handle->pinfo_unit[first_idx].samples);
  420. return 0;
  421. }
  422. /* 对端要求本端发相同的SPLAY过去
  423. */
  424. static int32_t _tws_handle_newly_splay(bt_tws_observer_inner_t *handle, player_tws_startplay_pkt_t *startplay, uint64_t *bttus)
  425. {
  426. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  427. if((handle->tws_status == PLAYER_TWS_STATUS_PLAY)
  428. || (handle->tws_status == PLAYER_TWS_STATUS_DEAL_WAIT))
  429. {
  430. SYS_LOG_ERR("%u_%u, %u_%u, %u_%u",
  431. startplay->pkt_num, handle->first_pktnum,
  432. (uint32_t)(handle->splay_local_time >> 32) & 0xffffffff,
  433. (uint32_t)handle->splay_local_time & 0xffffffff,
  434. (uint32_t)(startplay->splay_time >> 32) & 0xffffffff,
  435. (uint32_t)startplay->splay_time & 0xffffffff);
  436. return 0;
  437. }
  438. startplay->splay_flag = PLAYER_TWS_USE_START_SPLAY;
  439. startplay->pkt_num = handle->first_pktnum;
  440. startplay->splay_time = bttus + handle->BM_TWS_WPlay_Mintime * 1000;
  441. startplay->first_clk = handle->first_clk;
  442. if(_tws_send_start_play(handle, &startplay) < 0)
  443. {
  444. SYS_LOG_ERR("_tws_send_start_play fail.");
  445. return -1;
  446. }
  447. handle->splay_local_time = startplay->splay_time;
  448. handle->forbit_aps_time = startplay->splay_time + 1500000;
  449. observer->set_interrupt_time(startplay.splay_time);
  450. printf("[ATW]handle new splay:%u_%u_%u\n",
  451. handle->first_pktnum,
  452. (uint32_t)(startplay->splay_time >> 32) & 0xffffffff,
  453. (uint32_t)startplay->splay_time & 0xffffffff);
  454. return 0;
  455. }
  456. /* 对端要求本端解码指定的包播放
  457. */
  458. static int32_t _tws_handle_local_splay(bt_tws_observer_inner_t *handle, player_tws_startplay_pkt_t *startplay, uint64_t *bttus)
  459. {
  460. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  461. if((handle->tws_status == PLAYER_TWS_STATUS_PLAY)
  462. || (handle->tws_status == PLAYER_TWS_STATUS_DEAL_WAIT))
  463. {
  464. SYS_LOG_ERR("%u_%u, %u_%u, %u_%u",
  465. startplay->pkt_num, handle->first_pktnum,
  466. (uint32_t)(handle->splay_local_time >> 32) & 0xffffffff,
  467. (uint32_t)handle->splay_local_time & 0xffffffff,
  468. (uint32_t)(startplay->splay_time >> 32) & 0xffffffff,
  469. (uint32_t)startplay->splay_time & 0xffffffff);
  470. return 0;
  471. }
  472. /* 对端的包号小于本端则对端需要继续播放并重新指定解码包
  473. */
  474. if((startplay->pkt_num < handle->first_pktnum)
  475. || (bttus >= (startplay->splay_time - TWS_MIN_INTERVAL)))
  476. {
  477. SYS_LOG_ERR("%u_%u, %u_%u, %u_%u",
  478. startplay->pkt_num, handle->first_pktnum,
  479. (uint32_t)(bttus >> 32) & 0xffffffff,
  480. (uint32_t)bttus & 0xffffffff,
  481. (uint32_t)(startplay->splay_time >> 32) & 0xffffffff,
  482. (uint32_t)startplay->splay_time & 0xffffffff);
  483. return _tws_handle_newly_splay(handle, startplay, bttus);
  484. }
  485. handle->splay_local_time = startplay.splay_time;
  486. handle->first_pktnum = startplay.pkt_num;
  487. observer->set_interrupt_time(startplay.splay_time);
  488. printf("[ATW]handle local splay:%u_%u_%u\n",
  489. handle->first_pktnum,
  490. (uint32_t)(startplay->splay_time >> 32) & 0xffffffff,
  491. (uint32_t)startplay->splay_time & 0xffffffff);
  492. return 0;
  493. }
  494. /* 对端告诉本端正在启动播放
  495. */
  496. static int32_t _tws_handle_start_splay(bt_tws_observer_inner_t *handle, player_tws_startplay_pkt_t *startplay, uint64_t *bttus)
  497. {
  498. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  499. uint32_t flags;
  500. if((handle->tws_status == PLAYER_TWS_STATUS_PLAY)
  501. || (handle->tws_status == PLAYER_TWS_STATUS_DEAL_WAIT))
  502. {
  503. /* 确定对端开始播放的蓝牙时钟
  504. */
  505. if(_tws_calculate_new_splay(handle, startplay) < 0)
  506. {
  507. SYS_LOG_ERR("_tws_calculate_new_splay fail");
  508. return -1;
  509. }
  510. /* 发送给对端样机
  511. */
  512. if(_tws_send_start_play(handle, startplay) < 0)
  513. {
  514. SYS_LOG_ERR("_tws_send_start_play fail.");
  515. return -1;
  516. }
  517. flags = irq_lock();
  518. //有接入时一段时间不能进行缓冲区调节
  519. handle->forbit_aps_time = bttus + 1500000;
  520. handle->aps_level_pending = -1;
  521. //对端接入后使用基准时钟播放
  522. handle->media_observer->set_base_aps_level(handle->media_observer->media_handle, 0xFF);
  523. irq_unlock(flags);
  524. printf("[ATW]handle start splay @1\n");
  525. return 0;
  526. }
  527. /* 对端包号小于当前包号, 使用当前包号, 否则使用对端包号和时间)
  528. */
  529. if((startplay->pkt_num > handle->first_pktnum)
  530. || ((startplay->pkt_num == handle->first_pktnum)
  531. && (handle->splay_local_time < startplay->splay_time)))
  532. {
  533. handle->first_pktnum = startplay->pkt_num;
  534. handle->splay_local_time = startplay->splay_time;
  535. observer->set_interrupt_time(startplay->splay_time);
  536. printf("[ATW]handle start splay @2:%u_%u_%u\n",
  537. (uint32_t)(startplay->splay_time >> 32) & 0xffffffff,
  538. (uint32_t)startplay->splay_time & 0xffffffff,
  539. startplay->pkt_num);
  540. }
  541. return 0;
  542. }
  543. /* 返回-1: 表示失败, 需重启播放器
  544. */
  545. static void _tws_deal_startplay(bt_tws_observer_inner_t *handle, player_tws_pkt_t *tws_pkt)
  546. {
  547. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  548. player_tws_startplay_pkt_t *startplay = (player_tws_startplay_pkt_t *)(tws_pkt->payload_data);
  549. uint64_t bt_clk;
  550. if((startplay->scene != handle->format) || (tws_pkt->type != TWS_STARTPLAY_PKT))
  551. return;
  552. bt_clk = observer->get_bt_clk_us();
  553. if(bt_clk == 0)
  554. {
  555. SYS_LOG_ERR("get_bt_clk_us fail");
  556. return;
  557. }
  558. printf("[ATW]deal SPlay:%u_%u_%u_%u_%u, %u_%u_%u_%u_%u\n",
  559. startplay->pkt_num, startplay->first_clk,
  560. (uint32_t)(startplay->splay_time >> 32) & 0xffffffff, (uint32_t)startplay->splay_time & 0xffffffff,
  561. startplay->splay_flag,
  562. handle->first_pktnum, handle->first_clk,
  563. (uint32_t)(handle->splay_local_time >> 32) & 0xffffffff, (uint32_t)handle->splay_local_time & 0xffffffff,
  564. handle->tws_status);
  565. switch(startplay->splay_flag)
  566. {
  567. case PLAYER_TWS_NEED_NEW_SPLAY:
  568. _tws_handle_newly_splay(handle, startplay, bt_clk);
  569. break;
  570. case PLAYER_TWS_USE_LOCAL_SPLAY:
  571. _tws_handle_local_splay(handle, startplay, bt_clk);
  572. break;
  573. case PLAYER_TWS_USE_START_SPLAY:
  574. _tws_handle_start_splay(handle, startplay, bt_clk);
  575. break;
  576. default:
  577. SYS_LOG_ERR("splay_flag invalid");
  578. break;
  579. }
  580. }
  581. /* 返回-1: 表示失败, 需重启播放器
  582. */
  583. static int32_t _tws_deal_startstop(bt_tws_observer_inner_t *handle, player_tws_pkt_t *tws_pkt)
  584. {
  585. //todo
  586. return 0;
  587. }
  588. static int32_t _tws_deal_syncinfo(bt_tws_observer_inner_t *handle, player_tws_pkt_t *tws_pkt)
  589. {
  590. player_tws_syncinfo_pkt_t *syncinfo = (player_tws_syncinfo_pkt_t *)(tws_pkt->payload_data);
  591. if((syncinfo->scene != handle->format) || (tws_pkt->type != TWS_SYNCINFO_PKT))
  592. return 0;
  593. printf("[ATW]deal SInfo:%d_%u_%u_%d\n",
  594. syncinfo->pkt_num,
  595. (uint32_t)(syncinfo->bttime_us >> 32) & 0xffffffff,
  596. (uint32_t) syncinfo->bttime_us & 0xffffffff,
  597. handle->tws_status);
  598. if(handle->tws_status != PLAYER_TWS_STATUS_PLAY)
  599. return -1;
  600. if(acts_ringbuf_space(&handle->sinfo_master_buffer) < sizeof(player_tws_syncinfo_pkt_t))
  601. {
  602. SYS_LOG_ERR("sinfo_master_buffer full");
  603. acts_ringbuf_get(&handle->sinfo_master_buffer, NULL, sizeof(player_tws_syncinfo_pkt_t));
  604. }
  605. acts_ringbuf_put(&handle->sinfo_master_buffer, syncinfo, sizeof(player_tws_syncinfo_pkt_t));
  606. return 1;
  607. }
  608. static int32_t _tws_deal_playrate(bt_tws_observer_inner_t *handle, player_tws_pkt_t *tws_pkt)
  609. {
  610. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  611. player_tws_playrate_pkt_t *playrate = (player_tws_playrate_pkt_t *)(tws_pkt->payload_data);
  612. uint32_t flags;
  613. if((playrate->scene != handle->format) || (tws_pkt->type != TWS_PLAYRATE_PKT))
  614. return 0;
  615. printf("[ATW]deal APSInfo:%u_%u_%u_%u\n",
  616. (uint32_t)(playrate->inttime >> 32) & 0xffffffff,
  617. (uint32_t)playrate->inttime & 0xffffffff,
  618. handle->tws_status,
  619. playrate->aps_level);
  620. if(handle->tws_status != PLAYER_TWS_STATUS_PLAY)
  621. return 0;
  622. /* 设置中断
  623. */
  624. observer->set_interrupt_time(playrate->inttime);
  625. /* 加入中断信号队列
  626. */
  627. flags = irq_lock();
  628. handle->aps_level_pending = playrate->aps_level;
  629. handle->aps_inttime = playrate.inttime;
  630. irq_unlock(flags);
  631. return 0;
  632. }
  633. static int32_t _tws_handle_aps_change_request(bt_tws_observer_inner_t *handle, uint8_t level)
  634. {
  635. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  636. player_tws_playrate_pkt_t playrate;
  637. uint32_t flags;
  638. if((handle->tws_role == BTSRV_TWS_SLAVE)
  639. || (handle->tws_status != PLAYER_TWS_STATUS_PLAY)
  640. || (handle->aps_level_pending == level))
  641. return 0;
  642. if(handle->tws_role == BTSRV_TWS_NONE)
  643. goto ERROUT;
  644. /* 获取蓝牙时钟
  645. */
  646. playrate.inttime = observer->get_bt_clk_us();
  647. if(playrate.inttime == 0)
  648. {
  649. SYS_LOG_ERR("get_bt_clk_us fail.");
  650. goto ERROUT;
  651. }
  652. if(playrate.inttime < handle->forbit_aps_time)
  653. return 0;
  654. /* 告诉对端调节速度
  655. */
  656. playrate.aps_level = level;
  657. playrate.inttime += 150000; /* 150 ms */
  658. if(_tws_send_play_rate(handle, &playrate) < 0)
  659. {
  660. SYS_LOG_ERR("_tws_send_play_rate fail.");
  661. goto ERROUT;
  662. }
  663. /* 设置中断
  664. */
  665. observer->set_interrupt_time(playrate.inttime);
  666. /* 加入中断信号队列
  667. */
  668. flags = irq_lock();
  669. handle->aps_level_pending = level;
  670. handle->aps_inttime = playrate.inttime;
  671. irq_unlock(flags);
  672. return 0;
  673. ERROUT:
  674. handle->media_observer->set_base_aps_level(handle->media_observer->media_handle, level);
  675. return 0;
  676. }
  677. static int32_t _tws_handle_new_pkt_info(bt_tws_observer_inner_t *handle, tws_pkt_info_t *info)
  678. {
  679. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  680. int32_t ret;
  681. if(handle->tws_status != PLAYER_TWS_STATUS_PLAY)
  682. return 0;
  683. //aac两包一帧,第一包解码没有输出,清除包信息,避免时间计算不准确
  684. if(info->samples == 0)
  685. {
  686. SYS_LOG_INF("AAC output 0");
  687. goto ERROUT;
  688. }
  689. //丢弃不完整的包避免统计时间不正确
  690. if(handle->first_pkt_flag)
  691. {
  692. if(handle->first_pkt_flag == 1)
  693. {
  694. handle->first_pktnum = info->pkt_num;
  695. handle->first_pkt_flag = 2;
  696. }
  697. if(info->pkt_num == handle->first_pktnum)
  698. return 0;
  699. handle->first_pkt_flag = 0;
  700. }
  701. /* 检查包头信息
  702. */
  703. ret = _tws_check_pinfo_unit(handle, info->pkt_num, info->pkt_len, info->samples);
  704. if(ret == 0)
  705. return 0;
  706. if(ret < 0)
  707. {
  708. SYS_LOG_ERR("pkt miss, restart: %u_%u",
  709. info->pkt_num, handle->pinfo_unit[handle->pinfo_unit_idx].pkt_num);
  710. goto ERROUT;
  711. }
  712. _tws_update_sync_info(handle, info);
  713. if(((info->pkt_num % handle->BM_TWS_Sync_interval) == 0) || (handle->last_sinfo_flag == 1))
  714. {
  715. player_tws_syncinfo_pkt_t syncinfo;
  716. if(handle->last_sinfo_flag == 1)
  717. handle->last_sinfo_flag = 0;
  718. if((info->pkt_num % handle->BM_TWS_Sync_interval) == 0)
  719. handle->last_sinfo_flag = 1;
  720. memset(&syncinfo, 0, sizeof(player_tws_syncinfo_pkt_t));
  721. syncinfo.pkt_num = info->pkt_num;
  722. syncinfo.bttime_us = handle->pinfo_unit[handle->pinfo_unit_idx].pkt_bttime_us;
  723. if(acts_ringbuf_space(&handle->sinfo_slave_buffer) < sizeof(player_tws_syncinfo_pkt_t))
  724. acts_ringbuf_get(&handle->sinfo_slave_buffer, NULL, sizeof(player_tws_syncinfo_pkt_t));
  725. acts_ringbuf_put(&handle->sinfo_slave_buffer, &syncinfo, sizeof(player_tws_syncinfo_pkt_t));
  726. if(handle->tws_role == BTSRV_TWS_MASTER)
  727. _tws_send_sync_info(handle, &syncinfo);
  728. printf("[ATW]SInfo:%u_%d_%u_%u\n", info->pkt_num, info->pkt_len,
  729. (uint32_t)(syncinfo.bttime_us >> 32) & 0xffffffff, (uint32_t)syncinfo.bttime_us & 0xffffffff);
  730. }
  731. return 0;
  732. ERROUT:
  733. handle->pinfo_unit_idx = 0;
  734. handle->save_pinfo_count = 0;
  735. memset(handle->pinfo_unit, 0, sizeof(handle->pinfo_unit));
  736. //丢包重启播放
  737. if((info->samples != 0) && (handle->tws_role != BTSRV_TWS_NONE))
  738. ;//todo: restart playback
  739. return 0;
  740. }
  741. static void _bt_tws_loop(struct k_timer *timer)
  742. {
  743. bt_tws_observer_inner_t *handle = &observer_inner;
  744. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  745. int32_t role;
  746. role = observer->get_role();
  747. if(role != handle->tws_role)
  748. {
  749. acts_ringbuf_drop_all(&handle->sinfo_master_buffer);
  750. handle->aps_level_pending = -1;
  751. handle->forbit_aps_time = observer->get_bt_clk_us() + 1500000;
  752. handle->tws_role = role;
  753. }
  754. while(acts_ringbuf_length(&handle->tws_data_buffer) >= sizeof(tws_pkt))
  755. {
  756. player_tws_pkt_t tws_pkt;
  757. acts_ringbuf_get(&handle->tws_data_buffer, &tws_pkt, sizeof(tws_pkt));
  758. switch(tws_pkt.type)
  759. {
  760. case TWS_STARTPLAY_PKT:
  761. _tws_deal_startplay(handle, tws_pkt);
  762. break;
  763. case TWS_STARTSTOP_PKT:
  764. _tws_deal_startstop(handle, tws_pkt);
  765. break;
  766. case TWS_SYNCINFO_PKT:
  767. if(role == BTSRV_TWS_SLAVE)
  768. _tws_deal_syncinfo(handle, tws_pkt);
  769. break;
  770. case TWS_PLAYRATE_PKT:
  771. if(role == BTSRV_TWS_SLAVE)
  772. _tws_deal_playrate(handle, tws_pkt);
  773. break;
  774. default:
  775. SYS_LOG_ERR("recv invalid pkt, group: %d, type: %d, len: %d",
  776. tws_pkt.group, tws_pkt.type, tws_pkt.payload_len);
  777. break;
  778. }
  779. }
  780. while(acts_ringbuf_length(&handle->msg_buffer) >= sizeof(player_tws_msg_t))
  781. {
  782. player_tws_msg_t tws_msg;
  783. acts_ringbuf_get(&handle->msg_buffer, &tws_msg, sizeof(tws_msg));
  784. switch(tws_msg.type)
  785. {
  786. case TWS_SYNCINFO_PKT:
  787. _tws_handle_new_pkt_info(handle, (tws_pkt_info_t*)tws_msg.data);
  788. break;
  789. case TWS_PLAYRATE_PKT:
  790. _tws_handle_aps_change_request(handle, tws_msg.data[0]);
  791. break;
  792. default:
  793. SYS_LOG_ERR("recv invalid msg, type: %d", tws_msg.type);
  794. break;
  795. }
  796. }
  797. if((handle->tws_status == PLAYER_TWS_STATUS_PLAY) && (role == BTSRV_TWS_SLAVE))
  798. _tws_align_samples(handle);
  799. if(handle->tws_status == PLAYER_TWS_STATUS_WAIT)
  800. {
  801. uint64_t bt_clk;
  802. bt_clk = observer->get_bt_clk_us();
  803. if(bt_clk == 0)
  804. {
  805. SYS_LOG_ERR("get_bt_clk_us fail");
  806. return;
  807. }
  808. bt_clk += TWS_MIN_INTERVAL;
  809. if(bt_clk >= handle->splay_local_time)
  810. {
  811. handle->tws_status == PLAYER_TWS_STATUS_DEAL_WAIT;
  812. handle->media_observer->set_start_pkt_num(handle->media_observer, handle->first_pktnum);
  813. }
  814. }
  815. }
  816. /*----------------------------------------------------------------------------------------------------*/
  817. static uint64_t _bt_tws_get_bt_clk_us(void)
  818. {
  819. bt_tws_observer_inner_t *handle = &observer_inner;
  820. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  821. return observer->get_bt_clk_us();
  822. }
  823. static uint8_t _bt_tws_get_role(void)
  824. {
  825. bt_tws_observer_inner_t *handle = &observer_inner;
  826. return handle->tws_role;
  827. }
  828. static int32_t _bt_tws_set_stream_info(uint8_t format, uint16_t first_pktnum, uint16_t sample_rate)
  829. {
  830. bt_tws_observer_inner_t *handle = &observer_inner;
  831. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)handle->media_observer->tws_observer;
  832. player_tws_startplay_pkt_t startplay;
  833. if(handle->tws_status != PLAYER_TWS_STATUS_INIT)
  834. {
  835. SYS_LOG_ERR("bt tws status: %d", handle->tws_status);
  836. return -1;
  837. }
  838. handle->format = format;
  839. handle->first_pktnum = first_pktnum;
  840. handle->sample_rate = sample_rate;
  841. handle->first_clk = observer->get_first_pkt_clk();
  842. handle->tws_status = PLAYER_TWS_STATUS_WAIT;
  843. handle->aps_level_pending = -1;
  844. handle->first_pkt_flag = 1;
  845. if(format == MSBC_TYPE || format == CVSD_TYPE)
  846. {
  847. handle->BM_TWS_WPlay_Mintime = 80;
  848. handle->BM_TWS_WPlay_Maxtime = 600;
  849. handle->BM_TWS_Sync_interval = 200;
  850. }
  851. else
  852. {
  853. handle->BM_TWS_WPlay_Mintime = 100;
  854. handle->BM_TWS_WPlay_Maxtime = 1000;
  855. handle->BM_TWS_Sync_interval = 40;
  856. }
  857. if(handle->tws_role == BTSRV_TWS_NONE)
  858. goto ERROUT;
  859. startplay.splay_flag = PLAYER_TWS_USE_START_SPLAY;
  860. startplay.pkt_num = first_pktnum;
  861. startplay.splay_time = observer->get_bt_clk_us() + handle->BM_TWS_WPlay_Mintime * 1000;
  862. startplay.first_clk = handle->first_clk;
  863. if(_tws_send_start_play(handle, &startplay) < 0)
  864. {
  865. SYS_LOG_ERR("_tws_send_start_play fail.");
  866. goto ERROUT;
  867. }
  868. handle->splay_local_time = startplay.splay_time;
  869. handle->forbit_aps_time = startplay.splay_time + 1500000;
  870. observer->set_interrupt_time(startplay.splay_time);
  871. os_timer_start(&handle->timer, BT_TWS_TIMER_INTERVAL, BT_TWS_TIMER_INTERVAL);
  872. return 0;
  873. ERROUT:
  874. handle->tws_status = PLAYER_TWS_STATUS_PLAY;
  875. handle->media_observer->set_start_pkt_num(handle->media_observer->media_handle, first_pktnum);
  876. handle->media_observer->start_playback(handle->media_observer->media_handle);
  877. return 0;
  878. }
  879. static int32_t _bt_tws_aps_change_request(uint8_t level)
  880. {
  881. bt_tws_observer_inner_t *handle = &observer_inner;
  882. player_tws_msg_t tws_msg;
  883. int32_t wait_time = 5;
  884. if(handle->tws_status == PLAYER_TWS_STATUS_INIT)
  885. return -1;
  886. tws_msg.type = TWS_PLAYRATE_PKT;
  887. tws_msg.data[0] = level;
  888. while(acts_ringbuf_space(&handle->msg_buffer) < sizeof(player_tws_msg_t))
  889. {
  890. if(wait_time <= 0)
  891. {
  892. SYS_LOG_ERR("msg buffer full");
  893. return -1;
  894. }
  895. os_sleep(1);
  896. wait_time--;
  897. }
  898. acts_ringbuf_put(&handle->msg_buffer, &tws_msg, sizeof(tws_msg));
  899. return 0;
  900. }
  901. static int32_t _bt_tws_set_pkt_info(tws_pkt_info_t *info)
  902. {
  903. bt_tws_observer_inner_t *handle = &observer_inner;
  904. player_tws_msg_t tws_msg;
  905. int32_t wait_time = 5;
  906. if(handle->tws_status == PLAYER_TWS_STATUS_INIT)
  907. return -1;
  908. tws_msg.type = TWS_SYNCINFO_PKT;
  909. memcpy(tws_msg.data, info, sizeof(tws_pkt_info_t));
  910. while(acts_ringbuf_space(&handle->msg_buffer) < sizeof(player_tws_msg_t))
  911. {
  912. if(wait_time <= 0)
  913. {
  914. SYS_LOG_ERR("msg buffer full");
  915. return -1;
  916. }
  917. os_sleep(1);
  918. wait_time--;
  919. }
  920. acts_ringbuf_put(&handle->msg_buffer, &tws_msg, sizeof(tws_msg));
  921. return 0;
  922. }
  923. bt_tws_observer_t* bluetooth_tws_observer_init(media_observer_t *media_observer)
  924. {
  925. bt_tws_observer_inner_t *handle = &observer_inner;
  926. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)media_observer->tws_observer;
  927. memset(handle, 0, sizeof(bt_tws_observer_inner_t));
  928. acts_ringbuf_init(&handle->tws_data_buffer, handle->tws_data, sizeof(handle->tws_data));
  929. acts_ringbuf_init(&handle->sinfo_master_buffer, handle->sinfo_master, sizeof(handle->sinfo_master));
  930. acts_ringbuf_init(&handle->sinfo_slave_buffer, handle->sinfo_slave, sizeof(handle->sinfo_slave));
  931. acts_ringbuf_init(&handle->msg_buffer, handle->msg_data, sizeof(handle->msg_data));
  932. handle->media_observer = media_observer;
  933. handle->tws_observer.get_bt_clk_us = _bt_tws_get_bt_clk_us;
  934. handle->tws_observer.get_role = _bt_tws_get_role;
  935. handle->tws_observer.set_stream_info = _bt_tws_set_stream_info;
  936. handle->tws_observer.aps_change_request = _bt_tws_aps_change_request;
  937. handle->tws_observer.set_pkt_info = _bt_tws_set_pkt_info;
  938. handle->tws_status = PLAYER_TWS_STATUS_INIT;
  939. handle->aps_level_pending = -1;
  940. handle->tws_role = observer->get_role();
  941. os_timer_init(&handle->timer, _bt_tws_loop, NULL);
  942. observer->set_interrupt_cb(_tws_irq_handle);
  943. observer->set_recv_pkt_cb(_tws_recv_pkt_handle);
  944. SYS_LOG_ERR("sizeof(player_tws_msg_t): %d, %d", sizeof(player_tws_msg_t), sizeof(tws_pkt_info_t));
  945. return &handle->tws_observer;
  946. }
  947. void bluetooth_tws_observer_deinit(bt_tws_observer_t *tws_observer)
  948. {
  949. bt_tws_observer_inner_t *handle = &observer_inner;
  950. tws_runtime_observer_t *observer = (tws_runtime_observer_t *)media_observer->tws_observer;
  951. uint32_t flags;
  952. flags = irq_lock();
  953. handle->aps_level_pending = -1;
  954. handle->tws_status = PLAYER_TWS_STATUS_INIT;
  955. irq_unlock(flags);
  956. os_timer_stop(&handle->timer);
  957. observer->set_interrupt_time(0);
  958. observer->set_interrupt_cb(NULL);
  959. observer->set_recv_pkt_cb(NULL);
  960. }