output_bt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. #include "squeezelite.h"
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "freertos/timers.h"
  10. #include "nvs.h"
  11. #include "nvs_flash.h"
  12. #include "esp_system.h"
  13. #include "esp_log.h"
  14. #include "esp_bt.h"
  15. #include "bt_app_core.h"
  16. #include "esp_bt_main.h"
  17. #include "esp_bt_device.h"
  18. #include "esp_gap_bt_api.h"
  19. #include "esp_a2dp_api.h"
  20. #include "esp_avrc_api.h"
  21. #define BT_AV_TAG "BT_AV"
  22. static log_level loglevel;
  23. static bool running = true;
  24. extern struct outputstate output;
  25. extern struct buffer *outputbuf;
  26. extern struct buffer *streambuf;
  27. #define LOCK mutex_lock(outputbuf->mutex)
  28. #define UNLOCK mutex_unlock(outputbuf->mutex)
  29. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  30. extern u8_t *silencebuf;
  31. static u8_t *optr;
  32. static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  33. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr);
  34. void set_volume(unsigned left, unsigned right) {
  35. LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
  36. LOCK;
  37. output.gainL = left;
  38. output.gainR = right;
  39. // TODO
  40. output.gainL = FIXED_ONE;
  41. output.gainR = FIXED_ONE;
  42. UNLOCK;
  43. }
  44. /* event for handler "bt_av_hdl_stack_up */
  45. enum {
  46. BT_APP_EVT_STACK_UP = 0,
  47. };
  48. /* A2DP global state */
  49. enum {
  50. APP_AV_STATE_IDLE,
  51. APP_AV_STATE_DISCOVERING,
  52. APP_AV_STATE_DISCOVERED,
  53. APP_AV_STATE_UNCONNECTED,
  54. APP_AV_STATE_CONNECTING,
  55. APP_AV_STATE_CONNECTED,
  56. APP_AV_STATE_DISCONNECTING,
  57. };
  58. /* sub states of APP_AV_STATE_CONNECTED */
  59. enum {
  60. APP_AV_MEDIA_STATE_IDLE,
  61. APP_AV_MEDIA_STATE_STARTING,
  62. APP_AV_MEDIA_STATE_STARTED,
  63. APP_AV_MEDIA_STATE_STOPPING,
  64. };
  65. #define BT_APP_HEART_BEAT_EVT (0xff00)
  66. /// handler for bluetooth stack enabled events
  67. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
  68. /// callback function for A2DP source
  69. static void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
  70. /// callback function for A2DP source audio data stream
  71. static int32_t bt_app_a2d_data_cb(uint8_t *data, int32_t len);
  72. static void a2d_app_heart_beat(void *arg);
  73. /// A2DP application state machine
  74. static void bt_app_av_sm_hdlr(uint16_t event, void *param);
  75. /* A2DP application state machine handler for each state */
  76. static void bt_app_av_state_unconnected(uint16_t event, void *param);
  77. static void bt_app_av_state_connecting(uint16_t event, void *param);
  78. static void bt_app_av_state_connected(uint16_t event, void *param);
  79. static void bt_app_av_state_disconnecting(uint16_t event, void *param);
  80. static esp_bd_addr_t s_peer_bda = {0};
  81. static uint8_t s_peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
  82. static int s_a2d_state = APP_AV_STATE_IDLE;
  83. static int s_media_state = APP_AV_MEDIA_STATE_IDLE;
  84. static int s_intv_cnt = 0;
  85. static int s_connecting_intv = 0;
  86. static uint32_t s_pkt_cnt = 0;
  87. static TimerHandle_t s_tmr;
  88. static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
  89. {
  90. if (bda == NULL || str == NULL || size < 18) {
  91. return NULL;
  92. }
  93. uint8_t *p = bda;
  94. sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
  95. p[0], p[1], p[2], p[3], p[4], p[5]);
  96. return str;
  97. }
  98. void output_init_dac(log_level level, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
  99. loglevel = level;
  100. LOG_INFO("init output BT");
  101. memset(&output, 0, sizeof(output));
  102. output.start_frames = FRAME_BLOCK * 2;
  103. output.write_cb = &_write_frames;
  104. output.rate_delay = rate_delay;
  105. // ensure output rate is specified to avoid test open
  106. if (!rates[0]) {
  107. rates[0] = 44100;
  108. }
  109. /*
  110. * Bluetooth audio source init Start
  111. */
  112. bt_set_log_level(level);
  113. ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
  114. esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  115. if (esp_bt_controller_init(&bt_cfg) != ESP_OK) {
  116. LOG_ERROR("%s initialize controller failed\n", __func__);
  117. return;
  118. }
  119. if (esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT) != ESP_OK) {
  120. LOG_ERROR("%s enable controller failed\n", __func__);
  121. return;
  122. }
  123. if (esp_bluedroid_init() != ESP_OK) {
  124. LOG_ERROR("%s initialize bluedroid failed\n", __func__);
  125. return;
  126. }
  127. if (esp_bluedroid_enable() != ESP_OK) {
  128. LOG_ERROR("%s enable bluedroid failed\n", __func__);
  129. return;
  130. }
  131. /* create application task */
  132. bt_app_task_start_up();
  133. /* Bluetooth device name, connection mode and profile set up */
  134. bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL);
  135. #if (CONFIG_BT_SSP_ENABLED == true)
  136. /* Set default parameters for Secure Simple Pairing */
  137. esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
  138. esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
  139. esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
  140. #endif
  141. /*
  142. * Set default parameters for Legacy Pairing
  143. * Use variable pin, input pin code when pairing
  144. */
  145. esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
  146. esp_bt_pin_code_t pin_code;
  147. esp_bt_gap_set_pin(pin_type, 0, pin_code);
  148. /*
  149. * Bluetooth audio source init Start
  150. */
  151. output_init_common(level, "-", output_buf_size, rates, idle);
  152. //#if LINUX || OSX || FREEBSD || POSIX
  153. // pthread_attr_t attr;
  154. // pthread_attr_init(&attr);
  155. //#ifdef PTHREAD_STACK_MIN
  156. // pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + OUTPUT_THREAD_STACK_SIZE);
  157. //#endif
  158. // pthread_create(&thread, &attr, output_thread, NULL);
  159. // pthread_attr_destroy(&attr);
  160. //#endif
  161. //#if WIN
  162. // thread = CreateThread(NULL, OUTPUT_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&output_thread, NULL, 0, NULL);
  163. //#endif
  164. }
  165. void output_close_dac(void) {
  166. LOG_INFO("close output");
  167. LOCK;
  168. running = false;
  169. UNLOCK;
  170. output_close_common();
  171. }
  172. static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  173. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
  174. if (!silence) {
  175. /* TODO need 16 bit fix
  176. if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
  177. _apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
  178. }
  179. if (gainL != FIXED_ONE || gainR!= FIXED_ONE) {
  180. _apply_gain(outputbuf, out_frames, gainL, gainR);
  181. }
  182. */
  183. #if BYTES_PER_FRAME == 4
  184. memcpy(optr, outputbuf->readp, out_frames * BYTES_PER_FRAME);
  185. #else
  186. {
  187. frames_t count = out_frames;
  188. s32_t *_iptr = (s32_t*) outputbuf->readp;
  189. s16_t *_optr = (s32_t*) optr;
  190. while (count--) {
  191. *_optr++ = *_iptr++ >> 16;
  192. *_optr++ = *_iptr++ >> 16;
  193. }
  194. }
  195. #endif
  196. } else {
  197. u8_t *buf = silencebuf;
  198. memcpy(optr, buf, out_frames * 4);
  199. }
  200. optr += out_frames * 4;
  201. return (int)out_frames;
  202. }
  203. //static void *output_thread() {
  204. //
  205. //
  206. // while (running) {
  207. //
  208. // //nothing to do here, for now. Feeding the buffer is
  209. // usleep(500000);
  210. // continue;
  211. // }
  212. //
  213. // output.device_frames = 0;
  214. // output.updated = gettime_ms();
  215. // output.frames_played_dmp = output.frames_played;
  216. //
  217. // _output_frames(FRAME_BLOCK);
  218. //
  219. // UNLOCK;
  220. //
  221. // if (buffill) {
  222. //// Do Stuff here
  223. // usleep((buffill * 1000 * 1000) / output.current_sample_rate);
  224. // buffill = 0;
  225. // } else {
  226. // usleep((FRAME_BLOCK * 1000 * 1000) / output.current_sample_rate);
  227. // }
  228. //
  229. // }
  230. //
  231. // return 0;
  232. //}
  233. static bool get_name_from_eir(uint8_t *eir, uint8_t *bdname, uint8_t *bdname_len)
  234. {
  235. uint8_t *rmt_bdname = NULL;
  236. uint8_t rmt_bdname_len = 0;
  237. if (!eir) {
  238. return false;
  239. }
  240. rmt_bdname = esp_bt_gap_resolve_eir_data(eir, ESP_BT_EIR_TYPE_CMPL_LOCAL_NAME, &rmt_bdname_len);
  241. if (!rmt_bdname) {
  242. rmt_bdname = esp_bt_gap_resolve_eir_data(eir, ESP_BT_EIR_TYPE_SHORT_LOCAL_NAME, &rmt_bdname_len);
  243. }
  244. if (rmt_bdname) {
  245. if (rmt_bdname_len > ESP_BT_GAP_MAX_BDNAME_LEN) {
  246. rmt_bdname_len = ESP_BT_GAP_MAX_BDNAME_LEN;
  247. }
  248. if (bdname) {
  249. memcpy(bdname, rmt_bdname, rmt_bdname_len);
  250. bdname[rmt_bdname_len] = '\0';
  251. }
  252. if (bdname_len) {
  253. *bdname_len = rmt_bdname_len;
  254. }
  255. return true;
  256. }
  257. return false;
  258. }
  259. static void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param)
  260. {
  261. char bda_str[18];
  262. uint32_t cod = 0;
  263. int32_t rssi = -129; /* invalid value */
  264. uint8_t *eir = NULL;
  265. esp_bt_gap_dev_prop_t *p;
  266. LOG_INFO("Scanned device: %s", bda2str(param->disc_res.bda, bda_str, 18));
  267. for (int i = 0; i < param->disc_res.num_prop; i++) {
  268. p = param->disc_res.prop + i;
  269. switch (p->type) {
  270. case ESP_BT_GAP_DEV_PROP_COD:
  271. cod = *(uint32_t *)(p->val);
  272. LOG_INFO("--Class of Device: 0x%x", cod);
  273. break;
  274. case ESP_BT_GAP_DEV_PROP_RSSI:
  275. rssi = *(int8_t *)(p->val);
  276. LOG_INFO("--RSSI: %d", rssi);
  277. break;
  278. case ESP_BT_GAP_DEV_PROP_EIR:
  279. eir = (uint8_t *)(p->val);
  280. break;
  281. case ESP_BT_GAP_DEV_PROP_BDNAME:
  282. default:
  283. break;
  284. }
  285. }
  286. /* search for device with MAJOR service class as "rendering" in COD */
  287. if (!esp_bt_gap_is_valid_cod(cod) ||
  288. !(esp_bt_gap_get_cod_srvc(cod) & ESP_BT_COD_SRVC_RENDERING)) {
  289. return;
  290. }
  291. /* search for device named "ESP_SPEAKER" in its extended inqury response */
  292. if (eir) {
  293. get_name_from_eir(eir, s_peer_bdname, NULL);
  294. if (strcmp((char *)s_peer_bdname, CONFIG_A2DP_SINK_NAME) != 0) {
  295. return;
  296. }
  297. LOG_INFO("Found a target device, address %s, name %s", bda_str, s_peer_bdname);
  298. s_a2d_state = APP_AV_STATE_DISCOVERED;
  299. memcpy(s_peer_bda, param->disc_res.bda, ESP_BD_ADDR_LEN);
  300. LOG_INFO("Cancel device discovery ...");
  301. esp_bt_gap_cancel_discovery();
  302. }
  303. }
  304. void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
  305. {
  306. switch (event) {
  307. case ESP_BT_GAP_DISC_RES_EVT: {
  308. filter_inquiry_scan_result(param);
  309. break;
  310. }
  311. case ESP_BT_GAP_DISC_STATE_CHANGED_EVT: {
  312. if (param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STOPPED) {
  313. if (s_a2d_state == APP_AV_STATE_DISCOVERED) {
  314. s_a2d_state = APP_AV_STATE_CONNECTING;
  315. LOG_INFO("Device discovery stopped.");
  316. LOG_INFO("a2dp connecting to peer: %s", s_peer_bdname);
  317. esp_a2d_source_connect(s_peer_bda);
  318. } else {
  319. // not discovered, continue to discover
  320. LOG_INFO("Device discovery failed, continue to discover...");
  321. esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
  322. }
  323. } else if (param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STARTED) {
  324. LOG_INFO("Discovery started.");
  325. }
  326. break;
  327. }
  328. case ESP_BT_GAP_RMT_SRVCS_EVT:
  329. case ESP_BT_GAP_RMT_SRVC_REC_EVT:
  330. break;
  331. case ESP_BT_GAP_AUTH_CMPL_EVT: {
  332. if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
  333. LOG_INFO("authentication success: %s", param->auth_cmpl.device_name);
  334. //esp_log_buffer_hex(param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
  335. } else {
  336. LOG_ERROR("authentication failed, status:%d", param->auth_cmpl.stat);
  337. }
  338. break;
  339. }
  340. case ESP_BT_GAP_PIN_REQ_EVT: {
  341. LOG_INFO("ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit);
  342. if (param->pin_req.min_16_digit) {
  343. LOG_INFO("Input pin code: 0000 0000 0000 0000");
  344. esp_bt_pin_code_t pin_code = {0};
  345. esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
  346. } else {
  347. LOG_INFO("Input pin code: 1234");
  348. esp_bt_pin_code_t pin_code;
  349. pin_code[0] = '1';
  350. pin_code[1] = '2';
  351. pin_code[2] = '3';
  352. pin_code[3] = '4';
  353. esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
  354. }
  355. break;
  356. }
  357. #if (CONFIG_BT_SSP_ENABLED == true)
  358. case ESP_BT_GAP_CFM_REQ_EVT:
  359. LOG_INFO("ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
  360. esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
  361. break;
  362. case ESP_BT_GAP_KEY_NOTIF_EVT:
  363. LOG_INFO("ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
  364. break;
  365. case ESP_BT_GAP_KEY_REQ_EVT:
  366. LOG_INFO("ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
  367. break;
  368. #endif
  369. default: {
  370. LOG_INFO("event: %d", event);
  371. break;
  372. }
  373. }
  374. return;
  375. }
  376. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
  377. {
  378. LOG_DEBUG("%s evt %d", __func__, event);
  379. switch (event) {
  380. case BT_APP_EVT_STACK_UP: {
  381. /* set up device name */
  382. char *dev_name = CONFIG_A2DP_DEV_NAME;
  383. esp_bt_dev_set_device_name(dev_name);
  384. /* register GAP callback function */
  385. esp_bt_gap_register_callback(bt_app_gap_cb);
  386. /* initialize A2DP source */
  387. esp_a2d_register_callback(&bt_app_a2d_cb);
  388. esp_a2d_source_register_data_callback(bt_app_a2d_data_cb);
  389. esp_a2d_source_init();
  390. /* set discoverable and connectable mode */
  391. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  392. /* start device discovery */
  393. LOG_INFO("Starting device discovery...");
  394. s_a2d_state = APP_AV_STATE_DISCOVERING;
  395. esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
  396. /* create and start heart beat timer */
  397. do {
  398. int tmr_id = 0;
  399. s_tmr = xTimerCreate("connTmr", (10000 / portTICK_RATE_MS),
  400. pdTRUE, (void *)tmr_id, a2d_app_heart_beat);
  401. xTimerStart(s_tmr, portMAX_DELAY);
  402. } while (0);
  403. break;
  404. }
  405. default:
  406. LOG_ERROR("%s unhandled evt %d", __func__, event);
  407. break;
  408. }
  409. }
  410. static void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
  411. {
  412. bt_app_work_dispatch(bt_app_av_sm_hdlr, event, param, sizeof(esp_a2d_cb_param_t), NULL);
  413. }
  414. static int32_t bt_app_a2d_data_cb(uint8_t *data, int32_t len)
  415. {
  416. frames_t frames;
  417. static int count = 0;
  418. if (len < 0 || data == NULL) {
  419. return 0;
  420. }
  421. LOCK;
  422. /* TODO
  423. Normally, we would want BT to not call us back unless we are not in BUFFERING state.
  424. That requires BT to not start until we are > OUTPUT_BUFFER
  425. // come back later, we are buffering (or stopped, need to handle that case ...) but we don't want silence
  426. if (output.state <= OUTPUT_BUFFER) {
  427. UNLOCK;
  428. return 0;
  429. }
  430. */
  431. frames = len / 4;
  432. output.device_frames = 0;
  433. output.updated = gettime_ms();
  434. output.frames_played_dmp = output.frames_played;
  435. if (!output.threshold) output.threshold = 20;
  436. optr = data;
  437. frames = _output_frames(frames);
  438. UNLOCK;
  439. if (!(count++ & 0x1ff)) {
  440. LOG_INFO("frames %d (count:%d) (out:%d, stream:%d)", frames, count, _buf_used(outputbuf), _buf_used(streambuf));
  441. }
  442. return frames * 4;
  443. }
  444. static void a2d_app_heart_beat(void *arg)
  445. {
  446. bt_app_work_dispatch(bt_app_av_sm_hdlr, BT_APP_HEART_BEAT_EVT, NULL, 0, NULL);
  447. }
  448. static void bt_app_av_sm_hdlr(uint16_t event, void *param)
  449. {
  450. LOG_INFO("%s state %d, evt 0x%x", __func__, s_a2d_state, event);
  451. switch (s_a2d_state) {
  452. case APP_AV_STATE_DISCOVERING:
  453. case APP_AV_STATE_DISCOVERED:
  454. break;
  455. case APP_AV_STATE_UNCONNECTED:
  456. bt_app_av_state_unconnected(event, param);
  457. break;
  458. case APP_AV_STATE_CONNECTING:
  459. bt_app_av_state_connecting(event, param);
  460. break;
  461. case APP_AV_STATE_CONNECTED:
  462. bt_app_av_state_connected(event, param);
  463. break;
  464. case APP_AV_STATE_DISCONNECTING:
  465. bt_app_av_state_disconnecting(event, param);
  466. break;
  467. default:
  468. LOG_ERROR("%s invalid state %d", __func__, s_a2d_state);
  469. break;
  470. }
  471. }
  472. static void bt_app_av_state_unconnected(uint16_t event, void *param)
  473. {
  474. switch (event) {
  475. case ESP_A2D_CONNECTION_STATE_EVT:
  476. case ESP_A2D_AUDIO_STATE_EVT:
  477. case ESP_A2D_AUDIO_CFG_EVT:
  478. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  479. break;
  480. case BT_APP_HEART_BEAT_EVT: {
  481. uint8_t *p = s_peer_bda;
  482. LOG_INFO("a2dp connecting to peer: %02x:%02x:%02x:%02x:%02x:%02x",
  483. p[0], p[1], p[2], p[3], p[4], p[5]);
  484. esp_a2d_source_connect(s_peer_bda);
  485. s_a2d_state = APP_AV_STATE_CONNECTING;
  486. s_connecting_intv = 0;
  487. break;
  488. }
  489. default:
  490. LOG_ERROR("%s unhandled evt %d", __func__, event);
  491. break;
  492. }
  493. }
  494. static void bt_app_av_state_connecting(uint16_t event, void *param)
  495. {
  496. esp_a2d_cb_param_t *a2d = NULL;
  497. switch (event) {
  498. case ESP_A2D_CONNECTION_STATE_EVT: {
  499. a2d = (esp_a2d_cb_param_t *)(param);
  500. if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED) {
  501. LOG_INFO("a2dp connected");
  502. s_a2d_state = APP_AV_STATE_CONNECTED;
  503. s_media_state = APP_AV_MEDIA_STATE_IDLE;
  504. esp_bt_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE);
  505. } else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
  506. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  507. }
  508. break;
  509. }
  510. case ESP_A2D_AUDIO_STATE_EVT:
  511. case ESP_A2D_AUDIO_CFG_EVT:
  512. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  513. break;
  514. case BT_APP_HEART_BEAT_EVT:
  515. if (++s_connecting_intv >= 2) {
  516. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  517. s_connecting_intv = 0;
  518. }
  519. break;
  520. default:
  521. LOG_ERROR("%s unhandled evt %d", __func__, event);
  522. break;
  523. }
  524. }
  525. static void bt_app_av_media_proc(uint16_t event, void *param)
  526. {
  527. esp_a2d_cb_param_t *a2d = NULL;
  528. switch (s_media_state) {
  529. case APP_AV_MEDIA_STATE_IDLE: {
  530. if (event == BT_APP_HEART_BEAT_EVT) {
  531. LOG_INFO("a2dp media ready checking ...");
  532. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_CHECK_SRC_RDY);
  533. } else if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  534. a2d = (esp_a2d_cb_param_t *)(param);
  535. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_CHECK_SRC_RDY &&
  536. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS) {
  537. LOG_INFO("a2dp media ready, starting ...");
  538. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_START);
  539. s_media_state = APP_AV_MEDIA_STATE_STARTING;
  540. }
  541. }
  542. break;
  543. }
  544. case APP_AV_MEDIA_STATE_STARTING: {
  545. if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  546. a2d = (esp_a2d_cb_param_t *)(param);
  547. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_START &&
  548. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS) {
  549. LOG_INFO("a2dp media start successfully.");
  550. s_intv_cnt = 0;
  551. s_media_state = APP_AV_MEDIA_STATE_STARTED;
  552. } else {
  553. // not started succesfully, transfer to idle state
  554. LOG_INFO("a2dp media start failed.");
  555. s_media_state = APP_AV_MEDIA_STATE_IDLE;
  556. }
  557. }
  558. break;
  559. }
  560. case APP_AV_MEDIA_STATE_STARTED: {
  561. if (event == BT_APP_HEART_BEAT_EVT) {
  562. if (++s_intv_cnt >= 10) {
  563. LOG_INFO("a2dp media stopping...");
  564. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_STOP);
  565. s_media_state = APP_AV_MEDIA_STATE_STOPPING;
  566. s_intv_cnt = 0;
  567. }
  568. }
  569. break;
  570. }
  571. case APP_AV_MEDIA_STATE_STOPPING: {
  572. if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  573. a2d = (esp_a2d_cb_param_t *)(param);
  574. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_STOP &&
  575. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS) {
  576. LOG_INFO("a2dp media stopped successfully, disconnecting...");
  577. s_media_state = APP_AV_MEDIA_STATE_IDLE;
  578. esp_a2d_source_disconnect(s_peer_bda);
  579. s_a2d_state = APP_AV_STATE_DISCONNECTING;
  580. } else {
  581. LOG_INFO("a2dp media stopping...");
  582. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_STOP);
  583. }
  584. }
  585. break;
  586. }
  587. }
  588. }
  589. static void bt_app_av_state_connected(uint16_t event, void *param)
  590. {
  591. esp_a2d_cb_param_t *a2d = NULL;
  592. switch (event) {
  593. case ESP_A2D_CONNECTION_STATE_EVT: {
  594. a2d = (esp_a2d_cb_param_t *)(param);
  595. if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
  596. LOG_INFO("a2dp disconnected");
  597. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  598. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  599. }
  600. break;
  601. }
  602. case ESP_A2D_AUDIO_STATE_EVT: {
  603. a2d = (esp_a2d_cb_param_t *)(param);
  604. if (ESP_A2D_AUDIO_STATE_STARTED == a2d->audio_stat.state) {
  605. s_pkt_cnt = 0;
  606. }
  607. break;
  608. }
  609. case ESP_A2D_AUDIO_CFG_EVT:
  610. // not suppposed to occur for A2DP source
  611. break;
  612. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  613. case BT_APP_HEART_BEAT_EVT: {
  614. bt_app_av_media_proc(event, param);
  615. break;
  616. }
  617. default:
  618. LOG_ERROR("%s unhandled evt %d", __func__, event);
  619. break;
  620. }
  621. }
  622. static void bt_app_av_state_disconnecting(uint16_t event, void *param)
  623. {
  624. esp_a2d_cb_param_t *a2d = NULL;
  625. switch (event) {
  626. case ESP_A2D_CONNECTION_STATE_EVT: {
  627. a2d = (esp_a2d_cb_param_t *)(param);
  628. if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
  629. LOG_INFO("a2dp disconnected");
  630. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  631. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  632. }
  633. break;
  634. }
  635. case ESP_A2D_AUDIO_STATE_EVT:
  636. case ESP_A2D_AUDIO_CFG_EVT:
  637. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  638. case BT_APP_HEART_BEAT_EVT:
  639. break;
  640. default:
  641. LOG_ERROR("%s unhandled evt %d", __func__, event);
  642. break;
  643. }
  644. }