output_bt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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, char *device, 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. device = "BT";
  152. output_init_common(level, device, output_buf_size, rates, idle);
  153. //#if LINUX || OSX || FREEBSD || POSIX
  154. // pthread_attr_t attr;
  155. // pthread_attr_init(&attr);
  156. //#ifdef PTHREAD_STACK_MIN
  157. // pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + OUTPUT_THREAD_STACK_SIZE);
  158. //#endif
  159. // pthread_create(&thread, &attr, output_thread, NULL);
  160. // pthread_attr_destroy(&attr);
  161. //#endif
  162. //#if WIN
  163. // thread = CreateThread(NULL, OUTPUT_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&output_thread, NULL, 0, NULL);
  164. //#endif
  165. }
  166. void output_close_dac(void) {
  167. LOG_INFO("close output");
  168. LOCK;
  169. running = false;
  170. UNLOCK;
  171. output_close_common();
  172. }
  173. static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  174. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
  175. if (!silence) {
  176. /* TODO need 16 bit fix
  177. if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
  178. _apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
  179. }
  180. if (gainL != FIXED_ONE || gainR!= FIXED_ONE) {
  181. _apply_gain(outputbuf, out_frames, gainL, gainR);
  182. }
  183. */
  184. #if BYTES_PER_FRAME == 4
  185. memcpy(optr, outputbuf->readp, out_frames * BYTES_PER_FRAME);
  186. #else
  187. {
  188. frames_t count = out_frames;
  189. s32_t *_iptr = (s32_t*) outputbuf->readp;
  190. s16_t *_optr = (s16_t*) optr;
  191. while (count--) {
  192. *_optr++ = *_iptr++ >> 16;
  193. *_optr++ = *_iptr++ >> 16;
  194. }
  195. }
  196. #endif
  197. } else {
  198. u8_t *buf = silencebuf;
  199. memcpy(optr, buf, out_frames * 4);
  200. }
  201. optr += out_frames * 4;
  202. return (int)out_frames;
  203. }
  204. //static void *output_thread() {
  205. //
  206. //
  207. // while (running) {
  208. //
  209. // //nothing to do here, for now. Feeding the buffer is
  210. // usleep(500000);
  211. // continue;
  212. // }
  213. //
  214. // output.device_frames = 0;
  215. // output.updated = gettime_ms();
  216. // output.frames_played_dmp = output.frames_played;
  217. //
  218. // _output_frames(FRAME_BLOCK);
  219. //
  220. // UNLOCK;
  221. //
  222. // if (buffill) {
  223. //// Do Stuff here
  224. // usleep((buffill * 1000 * 1000) / output.current_sample_rate);
  225. // buffill = 0;
  226. // } else {
  227. // usleep((FRAME_BLOCK * 1000 * 1000) / output.current_sample_rate);
  228. // }
  229. //
  230. // }
  231. //
  232. // return 0;
  233. //}
  234. static bool get_name_from_eir(uint8_t *eir, uint8_t *bdname, uint8_t *bdname_len)
  235. {
  236. uint8_t *rmt_bdname = NULL;
  237. uint8_t rmt_bdname_len = 0;
  238. if (!eir) {
  239. return false;
  240. }
  241. rmt_bdname = esp_bt_gap_resolve_eir_data(eir, ESP_BT_EIR_TYPE_CMPL_LOCAL_NAME, &rmt_bdname_len);
  242. if (!rmt_bdname) {
  243. rmt_bdname = esp_bt_gap_resolve_eir_data(eir, ESP_BT_EIR_TYPE_SHORT_LOCAL_NAME, &rmt_bdname_len);
  244. }
  245. if (rmt_bdname) {
  246. if (rmt_bdname_len > ESP_BT_GAP_MAX_BDNAME_LEN) {
  247. rmt_bdname_len = ESP_BT_GAP_MAX_BDNAME_LEN;
  248. }
  249. if (bdname) {
  250. memcpy(bdname, rmt_bdname, rmt_bdname_len);
  251. bdname[rmt_bdname_len] = '\0';
  252. }
  253. if (bdname_len) {
  254. *bdname_len = rmt_bdname_len;
  255. }
  256. return true;
  257. }
  258. return false;
  259. }
  260. static void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param)
  261. {
  262. char bda_str[18];
  263. uint32_t cod = 0;
  264. int32_t rssi = -129; /* invalid value */
  265. uint8_t *eir = NULL;
  266. esp_bt_gap_dev_prop_t *p;
  267. LOG_INFO("Scanned device: %s", bda2str(param->disc_res.bda, bda_str, 18));
  268. for (int i = 0; i < param->disc_res.num_prop; i++) {
  269. p = param->disc_res.prop + i;
  270. switch (p->type) {
  271. case ESP_BT_GAP_DEV_PROP_COD:
  272. cod = *(uint32_t *)(p->val);
  273. LOG_INFO("--Class of Device: 0x%x", cod);
  274. break;
  275. case ESP_BT_GAP_DEV_PROP_RSSI:
  276. rssi = *(int8_t *)(p->val);
  277. LOG_INFO("--RSSI: %d", rssi);
  278. break;
  279. case ESP_BT_GAP_DEV_PROP_EIR:
  280. eir = (uint8_t *)(p->val);
  281. break;
  282. case ESP_BT_GAP_DEV_PROP_BDNAME:
  283. default:
  284. break;
  285. }
  286. }
  287. /* search for device with MAJOR service class as "rendering" in COD */
  288. if (!esp_bt_gap_is_valid_cod(cod) ||
  289. !(esp_bt_gap_get_cod_srvc(cod) & ESP_BT_COD_SRVC_RENDERING)) {
  290. return;
  291. }
  292. /* search for device named "ESP_SPEAKER" in its extended inqury response */
  293. if (eir) {
  294. get_name_from_eir(eir, s_peer_bdname, NULL);
  295. if (strcmp((char *)s_peer_bdname, CONFIG_A2DP_SINK_NAME) != 0) {
  296. return;
  297. }
  298. LOG_INFO("Found a target device, address %s, name %s", bda_str, s_peer_bdname);
  299. s_a2d_state = APP_AV_STATE_DISCOVERED;
  300. memcpy(s_peer_bda, param->disc_res.bda, ESP_BD_ADDR_LEN);
  301. LOG_INFO("Cancel device discovery ...");
  302. esp_bt_gap_cancel_discovery();
  303. }
  304. }
  305. void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
  306. {
  307. switch (event) {
  308. case ESP_BT_GAP_DISC_RES_EVT: {
  309. filter_inquiry_scan_result(param);
  310. break;
  311. }
  312. case ESP_BT_GAP_DISC_STATE_CHANGED_EVT: {
  313. if (param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STOPPED) {
  314. if (s_a2d_state == APP_AV_STATE_DISCOVERED) {
  315. s_a2d_state = APP_AV_STATE_CONNECTING;
  316. LOG_INFO("Device discovery stopped.");
  317. LOG_INFO("a2dp connecting to peer: %s", s_peer_bdname);
  318. esp_a2d_source_connect(s_peer_bda);
  319. } else {
  320. // not discovered, continue to discover
  321. LOG_INFO("Device discovery failed, continue to discover...");
  322. esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
  323. }
  324. } else if (param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STARTED) {
  325. LOG_INFO("Discovery started.");
  326. }
  327. break;
  328. }
  329. case ESP_BT_GAP_RMT_SRVCS_EVT:
  330. case ESP_BT_GAP_RMT_SRVC_REC_EVT:
  331. break;
  332. case ESP_BT_GAP_AUTH_CMPL_EVT: {
  333. if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
  334. LOG_INFO("authentication success: %s", param->auth_cmpl.device_name);
  335. //esp_log_buffer_hex(param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
  336. } else {
  337. LOG_ERROR("authentication failed, status:%d", param->auth_cmpl.stat);
  338. }
  339. break;
  340. }
  341. case ESP_BT_GAP_PIN_REQ_EVT: {
  342. LOG_INFO("ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit);
  343. if (param->pin_req.min_16_digit) {
  344. LOG_INFO("Input pin code: 0000 0000 0000 0000");
  345. esp_bt_pin_code_t pin_code = {0};
  346. esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
  347. } else {
  348. LOG_INFO("Input pin code: 1234");
  349. esp_bt_pin_code_t pin_code;
  350. pin_code[0] = '1';
  351. pin_code[1] = '2';
  352. pin_code[2] = '3';
  353. pin_code[3] = '4';
  354. esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
  355. }
  356. break;
  357. }
  358. #if (CONFIG_BT_SSP_ENABLED == true)
  359. case ESP_BT_GAP_CFM_REQ_EVT:
  360. LOG_INFO("ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
  361. esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
  362. break;
  363. case ESP_BT_GAP_KEY_NOTIF_EVT:
  364. LOG_INFO("ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
  365. break;
  366. case ESP_BT_GAP_KEY_REQ_EVT:
  367. LOG_INFO("ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
  368. break;
  369. #endif
  370. default: {
  371. LOG_INFO("event: %d", event);
  372. break;
  373. }
  374. }
  375. return;
  376. }
  377. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
  378. {
  379. LOG_DEBUG("%s evt %d", __func__, event);
  380. switch (event) {
  381. case BT_APP_EVT_STACK_UP: {
  382. /* set up device name */
  383. char *dev_name = CONFIG_A2DP_DEV_NAME;
  384. esp_bt_dev_set_device_name(dev_name);
  385. /* register GAP callback function */
  386. esp_bt_gap_register_callback(bt_app_gap_cb);
  387. /* initialize A2DP source */
  388. esp_a2d_register_callback(&bt_app_a2d_cb);
  389. esp_a2d_source_register_data_callback(bt_app_a2d_data_cb);
  390. esp_a2d_source_init();
  391. /* set discoverable and connectable mode */
  392. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  393. /* start device discovery */
  394. LOG_INFO("Starting device discovery...");
  395. s_a2d_state = APP_AV_STATE_DISCOVERING;
  396. esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
  397. /* create and start heart beat timer */
  398. do {
  399. int tmr_id = 0;
  400. s_tmr = xTimerCreate("connTmr", (10000 / portTICK_RATE_MS),
  401. pdTRUE, (void *)tmr_id, a2d_app_heart_beat);
  402. xTimerStart(s_tmr, portMAX_DELAY);
  403. } while (0);
  404. break;
  405. }
  406. default:
  407. LOG_ERROR("%s unhandled evt %d", __func__, event);
  408. break;
  409. }
  410. }
  411. static void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
  412. {
  413. bt_app_work_dispatch(bt_app_av_sm_hdlr, event, param, sizeof(esp_a2d_cb_param_t), NULL);
  414. }
  415. static int32_t bt_app_a2d_data_cb(uint8_t *data, int32_t len)
  416. {
  417. frames_t frames;
  418. static int count = 0;
  419. static unsigned min_o = -1, max_o = 0, min_s = -1, max_s = 0;
  420. unsigned o, s;
  421. if (len < 0 || data == NULL) {
  422. return 0;
  423. }
  424. LOCK;
  425. /* TODO
  426. Normally, we would want BT to not call us back unless we are not in BUFFERING state.
  427. That requires BT to not start until we are > OUTPUT_BUFFER
  428. // come back later, we are buffering (or stopped, need to handle that case ...) but we don't want silence
  429. if (output.state <= OUTPUT_BUFFER) {
  430. UNLOCK;
  431. return 0;
  432. }
  433. */
  434. frames = len / 4;
  435. output.device_frames = 0;
  436. output.updated = gettime_ms();
  437. output.frames_played_dmp = output.frames_played;
  438. if (output.threshold < 20) output.threshold = 20;
  439. optr = data;
  440. frames = _output_frames(frames);
  441. UNLOCK;
  442. o = _buf_used(outputbuf);
  443. if (o < min_o) min_o = o;
  444. if (o > max_o) max_o = o;
  445. s = _buf_used(streambuf);
  446. if (s < min_s) min_s = s;
  447. if (s > max_s) max_s = s;
  448. if (!(count++ & 0x1ff)) {
  449. LOG_INFO("frames %d (count:%d) (out:%d/%d/%d, stream:%d/%d/%d)", frames, count, max_o, min_o, o, max_s, min_s, s);
  450. min_o = min_s = -1;
  451. max_o = max_s = -0;
  452. }
  453. return frames * 4;
  454. }
  455. bool test_open(const char *device, unsigned rates[], bool userdef_rates) {
  456. memset(rates, 0, MAX_SUPPORTED_SAMPLERATES * sizeof(unsigned));
  457. if (!strcmp(device, "BT")) {
  458. rates[0] = 44100;
  459. } else {
  460. unsigned _rates[] = { 96000, 88200, 48000, 44100, 32000, 0 };
  461. memcpy(rates, _rates, sizeof(_rates));
  462. }
  463. return true;
  464. }
  465. static void a2d_app_heart_beat(void *arg)
  466. {
  467. bt_app_work_dispatch(bt_app_av_sm_hdlr, BT_APP_HEART_BEAT_EVT, NULL, 0, NULL);
  468. }
  469. static void bt_app_av_sm_hdlr(uint16_t event, void *param)
  470. {
  471. LOG_INFO("%s state %d, evt 0x%x", __func__, s_a2d_state, event);
  472. switch (s_a2d_state) {
  473. case APP_AV_STATE_DISCOVERING:
  474. case APP_AV_STATE_DISCOVERED:
  475. break;
  476. case APP_AV_STATE_UNCONNECTED:
  477. bt_app_av_state_unconnected(event, param);
  478. break;
  479. case APP_AV_STATE_CONNECTING:
  480. bt_app_av_state_connecting(event, param);
  481. break;
  482. case APP_AV_STATE_CONNECTED:
  483. bt_app_av_state_connected(event, param);
  484. break;
  485. case APP_AV_STATE_DISCONNECTING:
  486. bt_app_av_state_disconnecting(event, param);
  487. break;
  488. default:
  489. LOG_ERROR("%s invalid state %d", __func__, s_a2d_state);
  490. break;
  491. }
  492. }
  493. static void bt_app_av_state_unconnected(uint16_t event, void *param)
  494. {
  495. switch (event) {
  496. case ESP_A2D_CONNECTION_STATE_EVT:
  497. case ESP_A2D_AUDIO_STATE_EVT:
  498. case ESP_A2D_AUDIO_CFG_EVT:
  499. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  500. break;
  501. case BT_APP_HEART_BEAT_EVT: {
  502. uint8_t *p = s_peer_bda;
  503. LOG_INFO("a2dp connecting to peer: %02x:%02x:%02x:%02x:%02x:%02x",
  504. p[0], p[1], p[2], p[3], p[4], p[5]);
  505. esp_a2d_source_connect(s_peer_bda);
  506. s_a2d_state = APP_AV_STATE_CONNECTING;
  507. s_connecting_intv = 0;
  508. break;
  509. }
  510. default:
  511. LOG_ERROR("%s unhandled evt %d", __func__, event);
  512. break;
  513. }
  514. }
  515. static void bt_app_av_state_connecting(uint16_t event, void *param)
  516. {
  517. esp_a2d_cb_param_t *a2d = NULL;
  518. switch (event) {
  519. case ESP_A2D_CONNECTION_STATE_EVT: {
  520. a2d = (esp_a2d_cb_param_t *)(param);
  521. if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED) {
  522. LOG_INFO("a2dp connected");
  523. s_a2d_state = APP_AV_STATE_CONNECTED;
  524. s_media_state = APP_AV_MEDIA_STATE_IDLE;
  525. esp_bt_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE);
  526. } else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
  527. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  528. }
  529. break;
  530. }
  531. case ESP_A2D_AUDIO_STATE_EVT:
  532. case ESP_A2D_AUDIO_CFG_EVT:
  533. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  534. break;
  535. case BT_APP_HEART_BEAT_EVT:
  536. if (++s_connecting_intv >= 2) {
  537. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  538. s_connecting_intv = 0;
  539. }
  540. break;
  541. default:
  542. LOG_ERROR("%s unhandled evt %d", __func__, event);
  543. break;
  544. }
  545. }
  546. static void bt_app_av_media_proc(uint16_t event, void *param)
  547. {
  548. esp_a2d_cb_param_t *a2d = NULL;
  549. switch (s_media_state) {
  550. case APP_AV_MEDIA_STATE_IDLE: {
  551. if (event == BT_APP_HEART_BEAT_EVT) {
  552. LOG_INFO("a2dp media ready checking ...");
  553. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_CHECK_SRC_RDY);
  554. } else if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  555. a2d = (esp_a2d_cb_param_t *)(param);
  556. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_CHECK_SRC_RDY &&
  557. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS) {
  558. LOG_INFO("a2dp media ready, starting ...");
  559. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_START);
  560. s_media_state = APP_AV_MEDIA_STATE_STARTING;
  561. }
  562. }
  563. break;
  564. }
  565. case APP_AV_MEDIA_STATE_STARTING: {
  566. if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  567. a2d = (esp_a2d_cb_param_t *)(param);
  568. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_START &&
  569. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS) {
  570. LOG_INFO("a2dp media start successfully.");
  571. s_intv_cnt = 0;
  572. s_media_state = APP_AV_MEDIA_STATE_STARTED;
  573. } else {
  574. // not started succesfully, transfer to idle state
  575. LOG_INFO("a2dp media start failed.");
  576. s_media_state = APP_AV_MEDIA_STATE_IDLE;
  577. }
  578. }
  579. break;
  580. }
  581. case APP_AV_MEDIA_STATE_STARTED: {
  582. if (event == BT_APP_HEART_BEAT_EVT) {
  583. if (++s_intv_cnt >= 10) {
  584. LOG_INFO("a2dp media stopping...");
  585. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_STOP);
  586. s_media_state = APP_AV_MEDIA_STATE_STOPPING;
  587. s_intv_cnt = 0;
  588. }
  589. }
  590. break;
  591. }
  592. case APP_AV_MEDIA_STATE_STOPPING: {
  593. if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  594. a2d = (esp_a2d_cb_param_t *)(param);
  595. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_STOP &&
  596. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS) {
  597. LOG_INFO("a2dp media stopped successfully, disconnecting...");
  598. s_media_state = APP_AV_MEDIA_STATE_IDLE;
  599. esp_a2d_source_disconnect(s_peer_bda);
  600. s_a2d_state = APP_AV_STATE_DISCONNECTING;
  601. } else {
  602. LOG_INFO("a2dp media stopping...");
  603. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_STOP);
  604. }
  605. }
  606. break;
  607. }
  608. }
  609. }
  610. static void bt_app_av_state_connected(uint16_t event, void *param)
  611. {
  612. esp_a2d_cb_param_t *a2d = NULL;
  613. switch (event) {
  614. case ESP_A2D_CONNECTION_STATE_EVT: {
  615. a2d = (esp_a2d_cb_param_t *)(param);
  616. if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
  617. LOG_INFO("a2dp disconnected");
  618. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  619. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  620. }
  621. break;
  622. }
  623. case ESP_A2D_AUDIO_STATE_EVT: {
  624. a2d = (esp_a2d_cb_param_t *)(param);
  625. if (ESP_A2D_AUDIO_STATE_STARTED == a2d->audio_stat.state) {
  626. s_pkt_cnt = 0;
  627. }
  628. break;
  629. }
  630. case ESP_A2D_AUDIO_CFG_EVT:
  631. // not suppposed to occur for A2DP source
  632. break;
  633. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  634. case BT_APP_HEART_BEAT_EVT: {
  635. bt_app_av_media_proc(event, param);
  636. break;
  637. }
  638. default:
  639. LOG_ERROR("%s unhandled evt %d", __func__, event);
  640. break;
  641. }
  642. }
  643. static void bt_app_av_state_disconnecting(uint16_t event, void *param)
  644. {
  645. esp_a2d_cb_param_t *a2d = NULL;
  646. switch (event) {
  647. case ESP_A2D_CONNECTION_STATE_EVT: {
  648. a2d = (esp_a2d_cb_param_t *)(param);
  649. if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
  650. LOG_INFO("a2dp disconnected");
  651. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  652. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  653. }
  654. break;
  655. }
  656. case ESP_A2D_AUDIO_STATE_EVT:
  657. case ESP_A2D_AUDIO_CFG_EVT:
  658. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  659. case BT_APP_HEART_BEAT_EVT:
  660. break;
  661. default:
  662. LOG_ERROR("%s unhandled evt %d", __func__, event);
  663. break;
  664. }
  665. }