[SOLVED] How to restart pipline?

__vnv__
Posts: 17
Joined: Sat Feb 16, 2019 9:42 pm

[SOLVED] How to restart pipline?

Postby __vnv__ » Tue Apr 16, 2019 11:20 am

Hello,

I have tried to create task to play some audio file from SDCARD and then reload different file and play it again.
It seemed logical to have task do the playing, delete task, put another mp3 and then start the task for that mp3.

Stoping task didn't work, and it says:
(12928) STAGEFRIGHTMP3_DECODER: MP3 opened
E (12938) HTTP_CLIENT: REACHED END OF MP3 FILE
E (12938) STAGEFRIGHTMP3_DECODER: failed to read audio data (line 166)
E (12948) AUDIO_ELEMENT: [mp3] AEL_STATUS_ERROR_OPEN
I (12958) STAGEFRIGHTMP3_DECODER: Closed
So second time I start task it cannot read my file.

Ok , so I tried workaround and use one task which would play and block after it stops, then when I send signal to it, it would again load file and play it again.

To test it, I have used two while loops, one that works forever and restarts every 3 second and one which plays file.
But again it stops at: "E (11333) HTTP_CLIENT: [ 4 ] Start audio_pipeline" and no sound comes out , also it stucks on that output.

Code below works for playing once , and it works without issues, restarting is an issue.

static int my_read_cb(audio_element_handle_t el, char *buf, int len, TickType_t wait_time, void *ctx)
{
static FILE *file;
if (file == NULL) {
file = fopen("/sdcard/some.mp3", "r");
if (!file) {
printf("Error opening file\n");
return -1;
}
}
int read_len = fread(buf, 1, len, file);
if (read_len == 0) {
ESP_LOGE(TAG, "REACHED END OF MP3 FILE");
fclose(file);
file = NULL;
read_len = AEL_IO_DONE;
}
return read_len;
}




static void play_task(void *pvParameters)
{

audio_element_handle_t i2s_stream_writer, mp3_decoder;
ringbuf_handle_t ringbuf;
QueueHandle_t i2s_queue, mp3_queue;
QueueSetHandle_t queue_set;
QueueSetMemberHandle_t queue_set_member;
audio_pipeline_handle_t pipeline;


ESP_LOGE(TAG, "[ 2 ] Create audio pipeline, add all elements to pipeline, and subscribe pipeline event");
audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
pipeline = audio_pipeline_init(&pipeline_cfg);
mem_assert(pipeline);


ESP_LOGE(TAG, "[3.0] Create i2s stream to write data to codec chip");
i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_UDA1334A(); // I2S_STREAM_CFG_DEFAULT();
i2s_cfg.type = AUDIO_STREAM_WRITER;
i2s_stream_writer = i2s_stream_init(&i2s_cfg);

ESP_LOGE(TAG, "[3.1] Create mp3 decoder to decode mp3 data");
mp3_decoder_cfg_t mp3_cfg = DEFAULT_MP3_DECODER_CONFIG();
mp3_decoder = mp3_decoder_init(&mp3_cfg);
audio_element_set_read_cb(mp3_decoder, my_read_cb, NULL);


ESP_LOGE(TAG, "[2.3] Register all elements to audio pipeline");
audio_pipeline_register(pipeline, mp3_decoder, "mp3");
audio_pipeline_register(pipeline, i2s_stream_writer, "i2s");

audio_pipeline_link(pipeline, (const char *[]) {"mp3", "i2s"}, 2);


ESP_LOGE(TAG, "[ 3 ] Set up event listener");
audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);


ESP_LOGE(TAG, "[3.1] Listening event from all elements of pipeline");
audio_pipeline_set_listener(pipeline, evt);



while(1){ // Used to loop forever

ESP_LOGE(TAG, "[ 4 ] Start audio_pipeline");
audio_pipeline_run(pipeline);


while (1) {
audio_event_iface_msg_t msg;
esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
continue;
}

if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) mp3_decoder
&& msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
audio_element_info_t music_info = {0};
audio_element_getinfo(mp3_decoder, &music_info);

ESP_LOGE(TAG, "[ * ] Receive music info from mp3 decoder, sample_rates=%d, bits=%d, ch=%d",
music_info.sample_rates, music_info.bits, music_info.channels);

audio_element_setinfo(i2s_stream_writer, &music_info);

continue;
}

/* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer
&& msg.cmd == AEL_MSG_CMD_REPORT_STATUS && (int) msg.data == AEL_STATUS_STATE_STOPPED) {
break;
}
}

ESP_LOGE(TAG, "NOVI WHAJL!");
audio_pipeline_wait_for_stop(pipeline);
vTaskDelay(3000 / portTICK_PERIOD_MS);

} // used for test - loop forever


ESP_LOGE(TAG, "[ 5 ] Stop audio_pipeline");
audio_pipeline_terminate(pipeline);

audio_pipeline_unregister(pipeline, mp3_decoder);
audio_pipeline_unregister(pipeline, i2s_stream_writer);

/* Terminate the pipeline before removing the listener */
audio_pipeline_remove_listener(pipeline);

/* Make sure audio_pipeline_remove_listener is called before destroying event_iface */
audio_event_iface_destroy(evt);

/* Release all resources */
audio_pipeline_unregister(pipeline, i2s_stream_writer);
audio_pipeline_unregister(pipeline, mp3_decoder);


audio_pipeline_deinit(pipeline);
audio_element_deinit(i2s_stream_writer);
audio_element_deinit(mp3_decoder);




ESP_LOGE(TAG, "FINISH PLAYING FILE!");

vTaskDelete( NULL );

}
Last edited by __vnv__ on Wed Apr 17, 2019 7:18 pm, edited 1 time in total.

__vnv__
Posts: 17
Joined: Sat Feb 16, 2019 9:42 pm

Re: How to restart pipline?

Postby __vnv__ » Wed Apr 17, 2019 7:17 pm

Hello everyone,

ok, so not looking at right place was the issue. I want to post answer so if anyone gets stuck can benefit at least something from my troubles :geek:

First, there is a good example at: https://github.com/espressif/esp-adf/tr ... p3_control

In that example there is part which looks like:
case USER_ID_SET:
ESP_LOGI(TAG, "[ * ] [Set] input key event");
audio_pipeline_terminate(pipeline);
ESP_LOGI(TAG, "[ * ] Stopped, advancing to the next song");
get_file(NEXT);
audio_pipeline_run(pipeline);
break;
So we can see that if you want to advance you have to terminate and then load some new file and then run again.
And that is the answer, just terminate and run it again, it WORKS!

shameem sanoodh
Posts: 2
Joined: Thu Jan 21, 2021 6:28 am

Re: [SOLVED] How to restart pipline?

Postby shameem sanoodh » Mon Jan 25, 2021 3:22 pm

pipeline restarting


Running idf_monitor in directory c:\esp\esp-adf\examples\get-started\play_mp3
Executing "C:\esp\tools\.espressif\python_env\idf4.1_py3.7_env\Scripts\python.exe C:\esp\esp-idf\tools/idf_monitor.py -p com3 -b 115200 --toolchain-prefix xtensa-esp32-elf- c:\esp\esp-adf\examples\get-started\play_mp3\build\play_mp3.elf -m 'C:\esp\tools\.espressif\python_env\idf4.1_py3.7_env\Scripts\python.exe' 'C:\esp\esp-idf\tools\idf.py' '-p' 'com3'"...
--- idf_monitor on com3 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun 8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4
load:0x3fff0034,len:6872
load:0x40078000,len:13880
load:0x40080400,len:4052
0x40080400: _init at ??:?

entry 0x40080688
I (28) boot: ESP-IDF v4.1-dirty 2nd stage bootloader
I (29) boot: compile time 16:53:53
I (29) boot: chip revision: 1
I (32) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (43) boot.esp32: SPI Speed : 80MHz
I (43) boot.esp32: SPI Mode : DIO
I (48) boot.esp32: SPI Flash Size : 8MB
I (52) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (61) boot: ## Label Usage Type ST Offset Length
I (69) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (76) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (84) boot: 2 factory factory app 00 00 00010000 00100000
I (91) boot: End of partition table
I (95) boot_comm: chip revision: 1, min. application chip revision: 0
I (102) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x22ea4 (143012) map
I (165) esp_image: segment 1: paddr=0x00032ecc vaddr=0x3ffb0000 size=0x021b8 ( 8632) load
I (169) esp_image: segment 2: paddr=0x0003508c vaddr=0x40080000 size=0x00404 ( 1028) load
0x40080000: _WindowOverflow4 at C:/esp/esp-idf/components/freertos/xtensa_vectors.S:1778

I (172) esp_image: segment 3: paddr=0x00035498 vaddr=0x40080404 size=0x0ab80 ( 43904) load
I (199) esp_image: segment 4: paddr=0x00040020 vaddr=0x400d0020 size=0x265fc (157180) map
0x400d0020: _stext at ??:?

I (258) esp_image: segment 5: paddr=0x00066624 vaddr=0x4008af84 size=0x039a0 ( 14752) load
0x4008af84: prvSwitchTimerLists at C:/esp/esp-idf/components/freertos/timers.c:898

I (273) boot: Loaded app from partition at offset 0x10000
I (273) boot: Disabling RNG early entropy source...
I (274) psram: This chip is ESP32-D0WD
I (279) spiram: Found 64MBit SPI RAM device
I (283) spiram: SPI RAM mode: flash 80m sram 40m
I (288) spiram: PSRAM initialized, cache is in low/high (2-core) mode.
I (295) cpu_start: Pro cpu up.
I (299) cpu_start: Application information:
I (304) cpu_start: Project name: play_mp3
I (309) cpu_start: App version: v2.2-52-g66eb22d-dirty
I (315) cpu_start: Compile time: Jan 25 2021 16:53:23
I (321) cpu_start: ELF file SHA256: 8f413979338aa4ef...
I (327) cpu_start: ESP-IDF: v4.1-dirty
I (332) cpu_start: Starting app cpu, entry point is 0x4008125c
0x4008125c: call_start_cpu1 at C:/esp/esp-idf/components/esp32/cpu_start.c:271

I (0) cpu_start: App cpu up.
I (1223) spiram: SPI SRAM memory test OK
I (1224) heap_init: Initializing. RAM available for dynamic allocation:
I (1224) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (1230) heap_init: At 3FFB2A78 len 0002D588 (181 KiB): DRAM
I (1236) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (1243) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (1249) heap_init: At 4008E924 len 000116DC (69 KiB): IRAM
I (1256) cpu_start: Pro cpu start user code
I (1260) spiram: Adding pool of 4096K of external SPI memory to heap allocator
I (1281) spi_flash: detected chip: gd
I (1281) spi_flash: flash io: dio
I (1282) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (1290) spiram: Reserving pool of 32K of internal memory for DMA/internal allocations
I (1300) PLAY_MP3_FLASH: [ 1 ] Start audio codec chip
I (1300) PLAY_MP3_FLASH: [ 2 ] Create audio pipeline, add all elements to pipeline, and subscribe pipeline event
I (1320) PLAY_MP3_FLASH: [2.1] Create mp3 decoder to decode mp3 file and set custom read callback
I (1330) PLAY_MP3_FLASH: [2.2] Create i2s stream to write data to codec chip
I (1360) PLAY_MP3_FLASH: [2.3] Register all elements to audio pipeline
I (1360) PLAY_MP3_FLASH: [2.4] Link it together [mp3_music_read_cb]-->mp3_decoder-->i2s_stream-->[codec_chip]
I (1370) PLAY_MP3_FLASH: [ 3 ] Set up event listener
I (1370) PLAY_MP3_FLASH: h
I (1370) PLAY_MP3_FLASH: [3.1] Listening event from all elements of pipeline
E (1380) AUDIO_PIPELINE: Error register event with: mp3
I (1390) PLAY_MP3_FLASH: hello
I (1390) PLAY_MP3_FLASH: [ 4 ] Start audio_pipeline
Guru Meditation Error: Core 0 panic'ed (StoreProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x40089baa PS : 0x00060633 A0 : 0x8008904f A1 : 0x3ffb46d0
0x40089baa: uxPortCompareSet at C:/esp/esp-idf/components/freertos/include/freertos/portmacro.h:363
(inlined by) vPortCPUAcquireMutexIntsDisabledInternal at C:/esp/esp-idf/components/freertos/portmux_impl.inc.h:86
(inlined by) vPortCPUAcquireMutexIntsDisabled at C:/esp/esp-idf/components/freertos/portmux_impl.h:100
(inlined by) vTaskEnterCritical at C:/esp/esp-idf/components/freertos/tasks.c:4201

A2 : 0x0000004b A3 : 0x00000001 A4 : 0x00060620 A5 : 0x0000cdcd
A6 : 0xb33fffff A7 : 0x0000abab A8 : 0x0000cdcd A9 : 0x3ffb46d0
A10 : 0x00000003 A11 : 0x00060623 A12 : 0x00060620 A13 : 0x00000000
A14 : 0x3ffae948 A15 : 0x00000021 SAR : 0x00000004 EXCCAUSE: 0x0000001d
EXCVADDR: 0x0000004b LBEG : 0x400871f5 LEND : 0x40087205 LCOUNT : 0xfffffffe
0x400871f5: strlen at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/machine/xtensa/strlen.S:84

0x40087205: strlen at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/machine/xtensa/strlen.S:96


ELF file SHA256: 8f413979338aa4ef

Backtrace: 0x40089ba7:0x3ffb46d0 0x4008904c:0x3ffb4700 0x400d8eca:0x3ffb4740 0x400d8449:0x3ffb4780 0x400d94c6:0x3ffb47d0 0x400d7367:0x3ffb47f0 0x400d16e8:0x3ffb4910 0x400881a1:0x3ffb4930
0x40089ba7: uxPortCompareSet at C:/esp/esp-idf/components/freertos/include/freertos/portmacro.h:363
(inlined by) vPortCPUAcquireMutexIntsDisabledInternal at C:/esp/esp-idf/components/freertos/portmux_impl.inc.h:86
(inlined by) vPortCPUAcquireMutexIntsDisabled at C:/esp/esp-idf/components/freertos/portmux_impl.h:100
(inlined by) vTaskEnterCritical at C:/esp/esp-idf/components/freertos/tasks.c:4201

0x4008904c: xQueueGenericReceive at C:/esp/esp-idf/components/freertos/queue.c:1461 (discriminator 4)

0x400d8eca: audio_event_iface_discard at C:/esp/esp-adf/components/audio_pipeline/audio_event_iface.c:287 (discriminator 1)

0x400d8449: audio_element_run at C:/esp/esp-adf/components/audio_pipeline/audio_element.c:1068

0x400d94c6: audio_pipeline_run at C:/esp/esp-adf/components/audio_pipeline/audio_pipeline.c:356

0x400d7367: app_main at c:\esp\esp-adf\examples\get-started\play_mp3\build/../main/play_mp3_example.c:108

0x400d16e8: main_task at C:/esp/esp-idf/components/esp32/cpu_start.c:565

0x400881a1: vPortTaskWrapper at C:/esp/esp-idf/components/freertos/port.c:143


Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4
load:0x3fff0034,len:6872
load:0x40078000,len:13880
load:0x40080400,len:4052
0x40080400: _init at ??:?

entry 0x40080688
I (29) boot: ESP-IDF v4.1-dirty 2nd stage bootloader
I (29) boot: compile time 16:53:53
I (29) boot: chip revision: 1
I (32) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (43) boot.esp32: SPI Speed : 80MHz
I (44) boot.esp32: SPI Mode : DIO
I (48) boot.esp32: SPI Flash Size : 8MB
I (53) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (62) boot: ## Label Usage Type ST Offset Length
I (69) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (77) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (84) boot: 2 factory factory app 00 00 00010000 00100000
I (92) boot: End of partition table
I (96) boot_comm: chip revision: 1, min. application chip revision: 0
I (103) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x22ea4 (143012) map
I (165) esp_image: segment 1: paddr=0x00032ecc vaddr=0x3ffb0000 size=0x021b8 ( 8632) load
I (169) esp_image: segment 2: paddr=0x0003508c vaddr=0x40080000 size=0x00404 ( 1028) load
0x40080000: _WindowOverflow4 at C:/esp/esp-idf/components/freertos/xtensa_vectors.S:1778

I (173) esp_image: segment 3: paddr=0x00035498 vaddr=0x40080404 size=0x0ab80 ( 43904) load
I (200) esp_image: segment 4: paddr=0x00040020 vaddr=0x400d0020 size=0x265fc (157180) map
0x400d0020: _stext at ??:?

Who is online

Users browsing this forum: No registered users and 24 guests