蓝牙管道切换的流程问题

ChiShaoJun
Posts: 29
Joined: Mon Sep 17, 2018 3:24 am

蓝牙管道切换的流程问题

Postby ChiShaoJun » Fri Mar 15, 2019 8:11 am

我根据了“flexible_pipeline”例程的逻辑重新改了代码,如下。
发现几个问题
1、audio_pipeline_pause得换成audio_pipeline_stop+audio_pipeline_wait_for_stop才可用
2、得重新注册i2s_stream_writer——audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
但是audio_pipeline_stop注释不是说了“The link state of the elements in the pipeline is kept”吗?不清楚什么时候取消注册的
  1. /******************************
  2. * 文件名称:play_bt_music_example.c
  3. * 作    者:Kyle
  4. * 日    期:20190319
  5. * 描    述:bt与aux_in音频切换
  6. ******************************/
  7.  
  8. /* 头文件 --------------------------------------------------------------------*/
  9. #include <string.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_log.h"
  13. #include "nvs_flash.h"
  14. #include "audio_element.h"
  15. #include "audio_pipeline.h"
  16. #include "audio_event_iface.h"
  17. #include "audio_common.h"
  18. #include "i2s_stream.h"
  19. #include "esp_peripherals.h"
  20. #include "periph_button.h"
  21. #include "audio_hal.h"
  22. #include "board.h"
  23. #include "bluetooth_service.h"
  24.  
  25. /* 宏定义 --------------------------------------------------------------------*/
  26. static const char *TAG = "BLUETOOTH_EXAMPLE";
  27.  
  28. /* 变量 ----------------------------------------------------------------------*/
  29. //状态切换
  30. bool ex_audio_status = false;
  31. audio_pipeline_handle_t pipeline;
  32. audio_element_handle_t bt_stream_reader, i2s_stream_reader, i2s_stream_writer;
  33.  
  34. /* 函数定义 ------------------------------------------------------------------*/
  35. /******************************
  36. * 函数名称:app_main
  37. * 作    者:Kyle
  38. * 日    期:20190319
  39. * 描    述:主函数
  40. * 输入参数:无
  41. * 返 回 值:无
  42. ******************************/
  43. void app_main(void)
  44. {
  45.     //nvs flash初始化
  46.     esp_err_t err = nvs_flash_init();
  47.     if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
  48.         // NVS partition was truncated and needs to be erased
  49.         // Retry nvs_flash_init
  50.         ESP_ERROR_CHECK(nvs_flash_erase());
  51.         err = nvs_flash_init();
  52.     }
  53.  
  54.     esp_log_level_set("*", ESP_LOG_INFO);
  55.     esp_log_level_set(TAG, ESP_LOG_DEBUG);
  56.  
  57.     //1.创建蓝牙服务
  58.     ESP_LOGI(TAG, "[ 1 ] Create Bluetooth service");
  59.     bluetooth_service_cfg_t bt_cfg = {
  60.         .device_name = "ESP-ADF-SPEAKER",
  61.         .mode = BLUETOOTH_A2DP_SINK,
  62.     };
  63.     bluetooth_service_start(&bt_cfg);
  64.  
  65.     //2.启动编解码器芯片
  66.     ESP_LOGI(TAG, "[ 2 ] Start codec chip");
  67.     audio_hal_codec_config_t audio_hal_codec_cfg =  AUDIO_HAL_ES8388_DEFAULT();
  68.     audio_hal_codec_cfg.i2s_iface.samples = AUDIO_HAL_44K_SAMPLES;
  69.     audio_hal_codec_cfg.adc_input = AUDIO_HAL_ADC_INPUT_LINE2;
  70.     audio_hal_handle_t hal = audio_hal_init(&audio_hal_codec_cfg, 0);
  71.     audio_hal_ctrl_codec(hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START);
  72.  
  73.     //3.创建音频管道以进行播放
  74.     ESP_LOGI(TAG, "[ 3 ] Create audio pipeline for playback");
  75.     audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  76.     pipeline = audio_pipeline_init(&pipeline_cfg);
  77.  
  78.     //3.1.创建i2s流以将数据从编解码器芯片读取
  79.     ESP_LOGI(TAG, "[3.1] Create i2s stream to read data to codec chip");
  80.     i2s_stream_cfg_t i2sr_cfg = I2S_STREAM_CFG_DEFAULT();
  81.     i2sr_cfg.type = AUDIO_STREAM_READER;
  82.     i2s_stream_reader = i2s_stream_init(&i2sr_cfg);
  83.  
  84.     //3.2.创建i2s流以将数据写入编解码器芯片
  85.     ESP_LOGI(TAG, "[3.1] Create i2s stream to write data to codec chip");
  86.     i2s_stream_cfg_t i2sw_cfg = I2S_STREAM_CFG_DEFAULT();
  87.     i2sw_cfg.type = AUDIO_STREAM_WRITER;
  88.     i2s_stream_writer = i2s_stream_init(&i2sw_cfg);
  89.  
  90.     //3.3.获取蓝牙流
  91.     ESP_LOGI(TAG, "[3.2] Get Bluetooth stream");
  92.     bt_stream_reader = bluetooth_service_create_stream();
  93.  
  94.     //3.2.将所有元素注册到音频管道
  95.     ESP_LOGI(TAG, "[3.2] Register all elements to audio pipeline");
  96.     audio_pipeline_register(pipeline, i2s_stream_reader, "i2sr");
  97.     audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
  98.     audio_pipeline_register(pipeline, bt_stream_reader, "bt");
  99.  
  100.     //3.3.把它链接在一起
  101.     ESP_LOGI(TAG, "[3.3] Link it together [Bluetooth]-->bt_stream_reader-->i2s_stream_writer-->[codec_chip]");
  102.     audio_pipeline_link(pipeline, (const char *[]) {"i2sr", "i2sw"}, 2);
  103.  
  104.     //4.初始化外围设备
  105.     ESP_LOGI(TAG, "[ 4 ] Initialize peripherals");
  106.     esp_periph_config_t periph_cfg = { 0 };
  107.     esp_periph_init(&periph_cfg);
  108.  
  109.     //4.1.初始化button外围设备
  110.     ESP_LOGI(TAG, "[4.1] Initialize button peripheral");
  111.     periph_button_cfg_t btn_cfg = {
  112.         .gpio_mask = GPIO_SEL_36 | GPIO_SEL_39, // REC BTN & MODE BTN
  113.     };
  114.     esp_periph_handle_t button_periph = periph_button_init(&btn_cfg);
  115.  
  116.     //4.2.创建蓝牙外设
  117.     ESP_LOGI(TAG, "[4.2] Create Bluetooth peripheral");
  118.     esp_periph_handle_t bt_periph = bluetooth_service_create_periph();
  119.  
  120.     //4.2.启动所有外围设备
  121.     ESP_LOGI(TAG, "[4.2] Start all peripherals");
  122.     esp_periph_start(button_periph);
  123.     esp_periph_start(bt_periph);
  124.  
  125.     //5.设置事件监听器
  126.     ESP_LOGI(TAG, "[ 5 ] Setup event listener");
  127.     audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
  128.     audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
  129.  
  130.     //5.1.来自管道的所有元素的监听事件
  131.     ESP_LOGI(TAG, "[5.1] Listening event from all elements of pipeline");
  132.     audio_pipeline_set_listener(pipeline, evt);
  133.  
  134.     //5.2.外围设备的监听事件
  135.     ESP_LOGI(TAG, "[5.2] Listening event from peripherals");
  136.     audio_event_iface_set_listener(esp_periph_get_event_iface(), evt);
  137.  
  138.     //6.启动audio_pipeline
  139.     ESP_LOGI(TAG, "[ 6 ] Start audio_pipeline");
  140.     audio_pipeline_run(pipeline);
  141.  
  142.     //7.监听所有管道事件
  143.     ESP_LOGI(TAG, "[ 7 ] Listen for all pipeline events");
  144.     while (1) {
  145.         audio_event_iface_msg_t msg;
  146.         esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
  147.         if (ret != ESP_OK) {
  148.             ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
  149.             continue;
  150.         }
  151.         if (msg.source_type != PERIPH_ID_BUTTON) {
  152. //            audio_element_handle_t el = (audio_element_handle_t)msg.source;
  153. //            ESP_LOGI(TAG, "Element tag:[%s],src_type:%x, cmd:%d, data_len:%d, data:%p",
  154. //                     audio_element_get_tag(el), msg.source_type, msg.cmd, msg.data_len, msg.data);
  155.             continue;
  156.         }
  157.         if (((int)msg.data == GPIO_MODE) && (msg.cmd == PERIPH_BUTTON_PRESSED)){
  158.             ex_audio_status = !ex_audio_status;
  159.             if (!ex_audio_status) {
  160.                 periph_bluetooth_pause(bt_periph);
  161.             }
  162.  
  163.             audio_pipeline_stop(pipeline);
  164.             audio_pipeline_wait_for_stop(pipeline);
  165.             ESP_LOGE(TAG, "Changing music to %s", ex_audio_status ? "bt" : "i2sr");
  166.             if (ex_audio_status) {
  167.                 audio_pipeline_breakup_elements(pipeline, i2s_stream_reader);
  168.                 //蓝牙
  169.                 audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
  170.                 audio_pipeline_relink(pipeline, (const char *[]) {"bt", "i2sw"}, 2);
  171.                 audio_pipeline_set_listener(pipeline, evt);
  172.             } else {
  173.                 audio_pipeline_breakup_elements(pipeline, bt_stream_reader);
  174.                 //外部
  175.                 audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
  176.                 audio_pipeline_relink(pipeline, (const char *[]) {"i2sr", "i2sw"}, 2);
  177.                 audio_pipeline_set_listener(pipeline, evt);
  178.             }
  179.             audio_pipeline_run(pipeline);
  180.             audio_pipeline_resume(pipeline);
  181.  
  182.             if (ex_audio_status) {
  183.                 periph_bluetooth_play(bt_periph);
  184.             }
  185.         }
  186.     }
  187.  
  188.     //8.停止audio_pipeline
  189.     ESP_LOGI(TAG, "[ 8 ] Stop audio_pipeline");
  190.     audio_pipeline_terminate(pipeline);
  191.  
  192.     audio_pipeline_unregister(pipeline, bt_stream_reader);
  193.     audio_pipeline_unregister(pipeline, i2s_stream_reader);
  194.     audio_pipeline_unregister(pipeline, i2s_stream_writer);
  195.  
  196.     //在删除侦听器之前终止管道
  197.     /* Terminate the pipeline before removing the listener */
  198.     audio_pipeline_remove_listener(pipeline);
  199.  
  200.     //在删除侦听器之前停止所有外围设备
  201.     /* Stop all peripherals before removing the listener */
  202.     esp_periph_stop_all();
  203.     audio_event_iface_remove_listener(esp_periph_get_event_iface(), evt);
  204.  
  205.     //确保在销毁event_iface之前调用audio_pipeline_remove_listener和audio_event_iface_remove_listener
  206.     /* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
  207.     audio_event_iface_destroy(evt);
  208.  
  209.     //释放所有资源
  210.     /* Release all resources */
  211.     audio_pipeline_deinit(pipeline);
  212.     audio_element_deinit(bt_stream_reader);
  213.     audio_element_deinit(i2s_stream_reader);
  214.     audio_element_deinit(i2s_stream_writer);
  215.     esp_periph_destroy();
  216.     bluetooth_service_destroy();
  217. }

Who is online

Users browsing this forum: No registered users and 1 guest