A2DP sink controls stop working

error404
Posts: 12
Joined: Thu May 12, 2022 8:54 am

A2DP sink controls stop working

Postby error404 » Thu Aug 25, 2022 1:30 pm

Hi,

I'm currently trying to get this example to work on my custom-board: https://github.com/espressif/esp-adf/bl ... _example.c

I changed it up a litte. The code is below.

My problem is, that the controls (prev, play, pause, next) stop working after a short time. I can use them like 2-10 times and then the connected device doesn't react to them anymore. Volume up and down always work fine, even after the others stopped working.
The functions (periph_bt_avrc_next, ect.) always return ESP_OK.
There also isn't a single debug/error message on the UART (except for my own ones created with debug_writeLine()).

Unfortunately, I have no idea how to search for or find the problem. :(

Thank you!

Edit:
If I just reconnect my bluetooth device it starts working again for a few times. ESP32 didn't get restarted in that time.

It happens with all bluetooth devices connected to the ESP32.
  1. #include "music.h"
  2. #include "debug.h"
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5.  
  6. static esp_periph_handle_t bt_periph = NULL;
  7. audio_event_iface_handle_t music_evt;
  8. audio_element_handle_t bt_stream_reader, i2s_stream_writer;
  9.  
  10. bool music_isPaused = false;
  11.  
  12. void music_init(void)
  13. {
  14.     audio_pipeline_handle_t pipeline;
  15.  
  16.     debug_writeLine("music: [ 1 ] Init Bluetooth");
  17.     ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
  18.     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  19.     ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
  20.     ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT));
  21.     ESP_ERROR_CHECK(esp_bluedroid_init());
  22.     ESP_ERROR_CHECK(esp_bluedroid_enable());
  23.  
  24.     esp_bt_dev_set_device_name("Play me daddy :)");
  25.     esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  26.  
  27.  
  28.     debug_writeLine("music: [ 2 ] Start codec chip");
  29.     audio_board_handle_t board_handle = audio_board_init();
  30.     audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START);
  31.  
  32.     debug_writeLine("music: [ 3 ] Create audio pipeline for playback");
  33.     audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  34.     pipeline = audio_pipeline_init(&pipeline_cfg);
  35.  
  36.     debug_writeLine("music: [4] Create i2s stream to write data to codec chip");
  37.     i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT();
  38.     i2s_cfg.type = AUDIO_STREAM_WRITER;
  39.     i2s_cfg.i2s_config.use_apll = false;
  40.     i2s_cfg.i2s_config.intr_alloc_flags = ESP_INTR_FLAG_IRAM,
  41.     i2s_stream_writer = i2s_stream_init(&i2s_cfg);
  42.  
  43.     debug_writeLine("music: [4.1] Get Bluetooth stream");
  44.     a2dp_stream_config_t a2dp_config = {
  45.         .type = AUDIO_STREAM_READER,
  46.         .user_callback = { 0 },
  47.         .audio_hal = board_handle->audio_hal,
  48.     };
  49.     bt_stream_reader = a2dp_stream_init(&a2dp_config);
  50.  
  51.     debug_writeLine("music: [4.2] Register all elements to audio pipeline");
  52.     audio_pipeline_register(pipeline, bt_stream_reader, "bt");
  53.     audio_pipeline_register(pipeline, i2s_stream_writer, "i2s");
  54.  
  55.     debug_writeLine("music: [4.3] Link it together [Bluetooth]-->bt_stream_reader-->i2s_stream_writer-->[codec_chip]");
  56.  
  57.     const char *link_tag[2] = { "bt", "i2s" };
  58.     audio_pipeline_link(pipeline, &link_tag[0], 2);
  59.  
  60.     debug_writeLine("music: [ 5 ] Initialize peripherals");
  61.     esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
  62.     esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);
  63.  
  64.     debug_writeLine("music: [5.2] Create Bluetooth peripheral");
  65.     bt_periph = bt_create_periph();
  66.  
  67.     debug_writeLine("music: [5.3] Start all peripherals");
  68.     esp_periph_start(set, bt_periph);
  69.  
  70.     debug_writeLine("music: [ 6 ] Set up  event listener");
  71.     audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
  72.     music_evt = audio_event_iface_init(&evt_cfg);
  73.  
  74.     debug_writeLine("music: [6.1] Listening event from all elements of pipeline");
  75.     audio_pipeline_set_listener(pipeline, music_evt);
  76.  
  77.     debug_writeLine("music: [ 7 ] Start audio_pipeline");
  78.     audio_pipeline_run(pipeline);
  79.  
  80.     debug_writeLine("music: [ 8 ] Listen for all pipeline events");
  81.    
  82.     xTaskCreate(music_mainTask, "music_mainTask", 4096, NULL, 5, NULL);
  83. }
  84.  
  85. void music_mainTask(void *pvParameters)
  86. {
  87.     while (true)
  88.     {
  89.         audio_event_iface_msg_t msg;
  90.         esp_err_t ret = audio_event_iface_listen(music_evt, &msg, portMAX_DELAY);
  91.         debug_writeLine("TEST");
  92.         if (ret != ESP_OK)
  93.         {
  94.             debug_writeLine("music: [ * ] Event interface error : %d", ret);
  95.             continue;
  96.         }
  97.  
  98.         if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) bt_stream_reader
  99.             && msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO)
  100.         {
  101.             audio_element_info_t music_info = { 0 };
  102.             audio_element_getinfo(bt_stream_reader, &music_info);
  103.  
  104.             debug_writeLine("music: [ * ] Receive music info from Bluetooth, sample_rates=%d, bits=%d, ch=%d",
  105.                 music_info.sample_rates,
  106.                 music_info.bits,
  107.                 music_info.channels);
  108.  
  109.             audio_element_setinfo(i2s_stream_writer, &music_info);
  110.             i2s_stream_set_clk(i2s_stream_writer, music_info.sample_rates, music_info.bits, music_info.channels);
  111.             continue;
  112.         }
  113.  
  114.         /* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
  115.         if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer
  116.             && msg.cmd == AEL_MSG_CMD_REPORT_STATUS
  117.             && (((int)msg.data == AEL_STATUS_STATE_STOPPED) || ((int)msg.data == AEL_STATUS_STATE_FINISHED)))
  118.         {
  119.             debug_writeLine("music: [ * ] Stop event received");
  120.             break;
  121.         }
  122.     }
  123. }
  124.  
  125. void music_volUp(void)
  126. {
  127.     debug_writeLine("music: Vol+");
  128.     esp_err_t err = periph_bt_volume_up(bt_periph);
  129.     debug_writeLine("music: err was %d", err);
  130. }
  131.  
  132. void music_volDown(void)
  133. {
  134.     debug_writeLine("music: Vol-");
  135.     esp_err_t err = periph_bt_volume_down(bt_periph);
  136.     debug_writeLine("music: err was %d", err);
  137. }
  138.  
  139. void music_nextSong(void)
  140. {
  141.     debug_writeLine("music: Next");
  142.     esp_err_t err = periph_bt_avrc_next(bt_periph);
  143.     debug_writeLine("music: err was %d", err);
  144. }
  145.  
  146. void music_prevSong(void)
  147. {
  148.     debug_writeLine("music: Prev");
  149.     esp_err_t err = periph_bt_avrc_prev(bt_periph);
  150.     debug_writeLine("music: err was %d", err);
  151. }
  152.  
  153. void music_pausePlay(void)
  154. {
  155.     if (music_isPaused)
  156.     {
  157.         debug_writeLine("music: Play");
  158.         esp_err_t err = periph_bt_play(bt_periph);
  159.         debug_writeLine("music: err was %d", err);
  160.     }
  161.     else
  162.     {
  163.         debug_writeLine("music: Pause");
  164.         esp_err_t err = periph_bt_pause(bt_periph);
  165.         debug_writeLine("music: err was %d", err);
  166.     }
  167.    
  168.     music_isPaused = !music_isPaused;
  169. }

Who is online

Users browsing this forum: No registered users and 27 guests