Skip to content

Commit 811ce07

Browse files
committed
feat(avi_player): support multiple instances
1 parent 6e589f6 commit 811ce07

File tree

8 files changed

+245
-195
lines changed

8 files changed

+245
-195
lines changed

components/avi_player/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# ChangeLog
22

3+
## v2.0.0 - 2025-06-09
4+
5+
* Support multiple instances.
6+
37
## v1.0.0 - 2024-8-15
48

59
* publish official version

components/avi_player/avi_player.c

Lines changed: 157 additions & 133 deletions
Large diffs are not rendered by default.

components/avi_player/avifile.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ static int strl_parser(avi_typedef *AVI_file, const uint8_t *buffer, uint32_t le
6868
}
6969
#ifdef CONFIG_AVI_PLAYER_DEBUG_INFO
7070
printf("-----strh info------\r\n");
71-
printf("fourcc_type:0x%x\r\n", strh->fourcc_type);
72-
printf("fourcc_codec:0x%x\r\n", strh->fourcc_codec);
73-
printf("flags:%d\r\n", strh->flags);
71+
printf("fourcc_type:0x%lx\r\n", strh->fourcc_type);
72+
printf("fourcc_codec:0x%"PRIx32"\r\n", strh->fourcc_codec);
73+
printf("flags:%"PRIu32"\r\n", strh->flags);
7474
printf("Priority:%d\r\n", strh->priority);
7575
printf("Language:%d\r\n", strh->language);
76-
printf("InitFrames:%d\r\n", strh->init_frames);
77-
printf("Scale:%d\r\n", strh->scale);
78-
printf("Rate:%d\r\n", strh->rate);
79-
printf("Start:%d\r\n", strh->start);
80-
printf("Length:%d\r\n", strh->length);
81-
printf("RefBufSize:%d\r\n", strh->suggest_buff_size);
82-
printf("Quality:%d\r\n", strh->quality);
83-
printf("SampleSize:%d\r\n", strh->sample_size);
76+
printf("InitFrames:%"PRIu32"\r\n", strh->init_frames);
77+
printf("Scale:%"PRIu32"\r\n", strh->scale);
78+
printf("Rate:%"PRIu32"\r\n", strh->rate);
79+
printf("Start:%"PRIu32"\r\n", strh->start);
80+
printf("Length:%"PRIu32"\r\n", strh->length);
81+
printf("RefBufSize:%"PRIu32"\r\n", strh->suggest_buff_size);
82+
printf("Quality:%"PRIu32"\r\n", strh->quality);
83+
printf("SampleSize:%"PRIu32"\r\n", strh->sample_size);
8484
printf("FrameLeft:%d\r\n", strh->rcFrame.left);
8585
printf("FrameTop:%d\r\n", strh->rcFrame.top);
8686
printf("FrameRight:%d\r\n", strh->rcFrame.right);
@@ -104,17 +104,17 @@ static int strl_parser(avi_typedef *AVI_file, const uint8_t *buffer, uint32_t le
104104
}
105105
#ifdef CONFIG_AVI_PLAYER_DEBUG_INFO
106106
printf("-----video strf info------\r\n");
107-
printf("Size of this structure:%d\r\n", strf->size1);
108-
printf("Width of image:%d\r\n", strf->width);
109-
printf("Height of image:%d\r\n", strf->height);
107+
printf("Size of this structure:%"PRIu32"\r\n", strf->size1);
108+
printf("Width of image:%"PRIu32"\r\n", strf->width);
109+
printf("Height of image:%"PRIu32"\r\n", strf->height);
110110
printf("Number of planes:%d\r\n", strf->planes);
111111
printf("Number of bits per pixel:%d\r\n", strf->bitcount);
112-
printf("Compression type:0x%x\r\n", strf->fourcc_compression);
113-
printf("Image size:%d\r\n", strf->image_size);
114-
printf("Horizontal resolution:%d\r\n", strf->x_pixels_per_meter);
115-
printf("Vertical resolution:%d\r\n", strf->y_pixels_per_meter);
116-
printf("Number of colors in palette:%d\r\n", strf->num_colors);
117-
printf("Number of important colors:%d\r\n\n", strf->imp_colors);
112+
printf("Compression type:0x%"PRIx32"\r\n", strf->fourcc_compression);
113+
printf("Image size:%"PRIu32"\r\n", strf->image_size);
114+
printf("Horizontal resolution:%"PRIu32"\r\n", strf->x_pixels_per_meter);
115+
printf("Vertical resolution:%"PRIu32"\r\n", strf->y_pixels_per_meter);
116+
printf("Number of colors in palette:%"PRIu32"\r\n", strf->num_colors);
117+
printf("Number of important colors:%"PRIu32"\r\n\n", strf->imp_colors);
118118
#endif
119119
AVI_file->vids_fps = strh->rate / strh->scale;
120120
AVI_file->vids_width = strf->width;
@@ -132,10 +132,10 @@ static int strl_parser(avi_typedef *AVI_file, const uint8_t *buffer, uint32_t le
132132
printf("strf data block info(audio stream):");
133133
printf("format tag:%d\r\n", strf->format_tag);
134134
printf("number of channels:%d\r\n", strf->channels);
135-
printf("sampling rate:%d\r\n", strf->samples_per_sec);
136-
printf("bitrate:%d\r\n", strf->avg_bytes_per_sec);
135+
printf("sampling rate:%"PRIu32"\r\n", strf->samples_per_sec);
136+
printf("bitrate:%"PRIu32"\r\n", strf->avg_bytes_per_sec);
137137
printf("block align:%d\r\n", strf->block_align);
138-
printf("sample size:%d\r\n\n", strf->bits_per_sample);
138+
printf("sample size:%"PRIu32"\r\n\n", strf->bits_per_sample);
139139
#endif
140140
AVI_file->auds_channels = strf->channels;
141141
AVI_file->auds_sample_rate = strf->samples_per_sec;
@@ -176,16 +176,16 @@ int avi_parser(avi_typedef *AVI_file, const uint8_t *buffer, uint32_t length)
176176

177177
#ifdef CONFIG_AVI_PLAYER_DEBUG_INFO
178178
printf("-----avih info------\r\n");
179-
printf("us_per_frame:%d\r\n", avih->us_per_frame);
180-
printf("max_bytes_per_sec:%d\r\n", avih->max_bytes_per_sec);
181-
printf("padding:%d\r\n", avih->padding);
182-
printf("flags:%d\r\n", avih->flags);
183-
printf("total_frames:%d\r\n", avih->total_frames);
184-
printf("init_frames:%d\r\n", avih->init_frames);
185-
printf("streams:%d\r\n", avih->streams);
186-
printf("suggest_buff_size:%d\r\n", avih->suggest_buff_size);
187-
printf("Width:%d\r\n", avih->width);
188-
printf("Height:%d\r\n\n", avih->height);
179+
printf("us_per_frame:%"PRIu32"\r\n", avih->us_per_frame);
180+
printf("max_bytes_per_sec:%"PRIu32"\r\n", avih->max_bytes_per_sec);
181+
printf("padding:%"PRIu32"\r\n", avih->padding);
182+
printf("flags:%"PRIu32"\r\n", avih->flags);
183+
printf("total_frames:%"PRIu32"\r\n", avih->total_frames);
184+
printf("init_frames:%"PRIu32"\r\n", avih->init_frames);
185+
printf("streams:%"PRIu32"\r\n", avih->streams);
186+
printf("suggest_buff_size:%"PRIu32"\r\n", avih->suggest_buff_size);
187+
printf("Width:%"PRIu32"\r\n", avih->width);
188+
printf("Height:%"PRIu32"\r\n\n", avih->height);
189189
#endif
190190

191191
pdata += sizeof(AVI_AVIH_CHUNK);
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
version: "1.0.0"
1+
dependencies:
2+
espressif/cmake_utilities: "*"
3+
idf: '>=4.4'
24
description: Parse the video stream and audio stream of an AVI video file.
3-
url: https://github.com/espressif/esp-iot-solution/tree/master/components/avi_player
45
documentation: https://docs.espressif.com/projects/esp-iot-solution/en/latest/multimedia/avi_player.html
56
issues: https://github.com/espressif/esp-iot-solution/issues
6-
repository: https://github.com/espressif/esp-iot-solution.git
7-
dependencies:
8-
idf: ">=4.4"
9-
cmake_utilities: "0.*"
10-
sbom:
11-
supplier: 'Organization: Espressif Systems (Shanghai) CO LTD'
12-
originator: 'Organization: Espressif Systems (Shanghai) CO LTD'
7+
repository: git://github.com/espressif/esp-iot-solution.git
8+
repository_info:
9+
commit_sha: a4fdf46f42e57aa20521159dab5146fc511142a4
10+
path: components/avi_player
11+
url: https://github.com/espressif/esp-iot-solution/tree/master/components/avi_player
12+
version: 2.0.0

components/avi_player/include/avi_player.h

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "esp_err.h"
1010
#include "freertos/FreeRTOS.h"
11+
#include "esp_idf_version.h"
1112

1213
#ifdef __cplusplus
1314
extern "C" {
@@ -83,6 +84,8 @@ typedef void (*audio_write_cb)(frame_data_t *data, void *arg);
8384
typedef void (*audio_set_clock_cb)(uint32_t rate, uint32_t bits_cfg, uint32_t ch, void *arg);
8485
typedef void (*avi_play_end_cb)(void *arg);
8586

87+
typedef void *avi_player_handle_t;
88+
8689
/**
8790
* @brief avi player config
8891
*
@@ -96,32 +99,39 @@ typedef struct {
9699
UBaseType_t priority; /*!< FreeRTOS task priority */
97100
BaseType_t coreID; /*!< ESP32 core ID */
98101
void *user_data; /*!< User data */
102+
int stack_size; /*!< Stack size for the player task */
103+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
104+
bool stack_in_psram; /*!< If you read file/data from flash, do not set true*/
105+
#endif
99106
} avi_player_config_t;
100107

101108
/**
102109
* @brief Plays an AVI file from memory. The buffer of the AVI will be passed through the set callback function.
103110
*
104111
* This function initializes and plays an AVI file from a memory buffer.
105112
*
106-
* @param avi_data Pointer to the AVI file data in memory.
107-
* @param avi_size Size of the AVI file data in bytes.
113+
* @param[in] handle AVI player handle
114+
* @param[in] avi_data Pointer to the AVI file data in memory.
115+
* @param[in] avi_size Size of the AVI file data in bytes.
108116
* @return esp_err_t ESP_OK if successful, otherwise an error code.
109117
*/
110-
esp_err_t avi_player_play_from_memory(uint8_t *avi_data, size_t avi_size);
118+
esp_err_t avi_player_play_from_memory(avi_player_handle_t handle, uint8_t *avi_data, size_t avi_size);
111119

112120
/**
113121
* @brief Plays an AVI file from the filesystem. The buffer of the AVI will be passed through the set callback function.
114122
*
115123
* This function initializes and plays an AVI file from the filesystem using its filename.
116124
*
117-
* @param filename Path to the AVI file on the filesystem.
125+
* @param[in] handle AVI player handle
126+
* @param[in] filename Path to the AVI file on the filesystem.
118127
* @return esp_err_t ESP_OK if successful, otherwise an error code.
119128
*/
120-
esp_err_t avi_player_play_from_file(const char *filename);
129+
esp_err_t avi_player_play_from_file(avi_player_handle_t handle, const char *filename);
121130

122131
/**
123132
* @brief Get one video frame from AVI stream
124133
*
134+
* @param[in] handle AVI player handle
125135
* @param[out] buffer Pointer to external buffer to hold one frame
126136
* @param[in,out] buffer_size Size of external buffer
127137
* @param[out] info Information of the video frame
@@ -133,11 +143,12 @@ esp_err_t avi_player_play_from_file(const char *filename);
133143
* - ESP_ERR_INVALID_ARG NULL arguments
134144
* - ESP_ERR_NO_MEM External buffer not enough
135145
*/
136-
esp_err_t avi_player_get_video_buffer(void **buffer, size_t *buffer_size, video_frame_info_t *info, TickType_t ticks_to_wait);
146+
esp_err_t avi_player_get_video_buffer(avi_player_handle_t handle, void **buffer, size_t *buffer_size, video_frame_info_t *info, TickType_t ticks_to_wait);
137147

138148
/**
139149
* @brief Get the audio buffer from AVI file
140150
*
151+
* @param[in] handle AVI player handle
141152
* @param[out] buffer pointer to the audio buffer
142153
* @param[in] buffer_size size of the audio buffer
143154
* @param[out] info audio frame information
@@ -149,37 +160,40 @@ esp_err_t avi_player_get_video_buffer(void **buffer, size_t *buffer_size, video_
149160
* - ESP_ERR_INVALID_ARG if buffer or info is NULL or buffer_size is zero
150161
* - ESP_ERR_NO_MEM if buffer size is not enough
151162
*/
152-
esp_err_t avi_player_get_audio_buffer(void **buffer, size_t *buffer_size, audio_frame_info_t *info, TickType_t ticks_to_wait);
163+
esp_err_t avi_player_get_audio_buffer(avi_player_handle_t handle, void **buffer, size_t *buffer_size, audio_frame_info_t *info, TickType_t ticks_to_wait);
153164

154165
/**
155166
* @brief Stop AVI player
156167
*
168+
* @param[in] handle AVI player handle
157169
* @return
158170
* - ESP_OK: Stop AVI player successfully
159171
* - ESP_ERR_INVALID_STATE: AVI player not playing
160172
*/
161-
esp_err_t avi_player_play_stop(void);
173+
esp_err_t avi_player_play_stop(avi_player_handle_t handle);
162174

163175
/**
164176
* @brief Initialize the AVI player
165177
*
166178
* @param[in] config Configuration of AVI player
179+
* @param[out] handle Pointer to store the AVI player handle
167180
*
168181
* @return
169182
* - ESP_OK: succeed
170183
* - ESP_ERR_NO_MEM: Cannot allocate memory for AVI player
171184
* - ESP_ERR_INVALID_STATE: AVI player has already been initialized
172185
*/
173-
esp_err_t avi_player_init(avi_player_config_t config);
186+
esp_err_t avi_player_init(avi_player_config_t config, avi_player_handle_t *handle);
174187

175188
/**
176189
* @brief Deinitializes the AVI player.
177190
*
178191
* This function deinitializes and cleans up resources used by the AVI player.
179192
*
193+
* @param[in] handle AVI player handle
180194
* @return esp_err_t ESP_OK if successful, otherwise an error code.
181195
*/
182-
esp_err_t avi_player_deinit(void);
196+
esp_err_t avi_player_deinit(avi_player_handle_t handle);
183197

184198
#ifdef __cplusplus
185199
}

components/avi_player/test_apps/main/avi_player_test.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "esp_heap_caps.h"
1313
#include "esp_log.h"
1414
#include "esp_timer.h"
15+
#include "esp_idf_version.h"
1516
#include "esp_spiffs.h"
1617
#include "avi_player.h"
1718

@@ -54,16 +55,22 @@ TEST_CASE("avi_player_test", "[avi_player]")
5455
.video_cb = video_write,
5556
.audio_set_clock_cb = audio_set_clock,
5657
.avi_play_end_cb = avi_play_end,
58+
.stack_size = 4096,
59+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
60+
// It must not be set to `true` when reading data from flash.
61+
.stack_in_psram = false,
62+
#endif
5763
};
5864

59-
avi_player_init(config);
65+
avi_player_handle_t handle;
66+
avi_player_init(config, &handle);
6067

61-
avi_player_play_from_file("/spiffs/p4_introduce.avi");
68+
avi_player_play_from_file(handle, "/spiffs/p4_introduce.avi");
6269

6370
while (!end_play) {
6471
vTaskDelay(500 / portTICK_PERIOD_MS);
6572
}
66-
avi_player_deinit();
73+
avi_player_deinit(handle);
6774
vTaskDelay(500 / portTICK_PERIOD_MS);
6875
}
6976

examples/usb/device/usb_dual_uvc_device/main/idf_component.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 0.1.0
1+
version: 0.2.0
22
targets:
33
- esp32s2
44
- esp32s3
@@ -12,7 +12,7 @@ dependencies:
1212
version: "1.1.*"
1313
override_path: "../../../../../components/usb/usb_device_uvc"
1414
avi_player:
15-
version: "1.*"
15+
version: "2.*"
1616
override_path: "../../../../../components/avi_player"
1717
lvgl/lvgl: #temp to workaround bsp issue
1818
version: "9.2.0"

examples/usb/device/usb_dual_uvc_device/main/usb_cam1.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
static const char *TAG = "usb_cam1";
1919
static uvc_fb_t s_fb;
2020
static bool running = false;
21+
static avi_player_handle_t avi_handle;
2122

2223
static void camera_stop_cb(void *cb_ctx)
2324
{
2425
(void)cb_ctx;
2526
if (running) {
26-
avi_player_play_stop();
27+
avi_player_play_stop(avi_handle);
2728
running = false;
2829
}
2930
ESP_LOGI(TAG, "Camera:%"PRIu32" Stop", (uint32_t)cb_ctx);
@@ -36,7 +37,7 @@ static esp_err_t camera_start_cb(uvc_format_t format, int width, int height, int
3637
ESP_LOGI(TAG, "Format: %d, width: %d, height: %d, rate: %d", format, width, height, rate);
3738

3839
running = true;
39-
avi_player_play_from_file("/spiffs/p4_introduce.avi");
40+
avi_player_play_from_file(avi_handle, "/spiffs/p4_introduce.avi");
4041

4142
return ESP_OK;
4243
}
@@ -46,7 +47,7 @@ static uvc_fb_t* camera_fb_get_cb(void *cb_ctx)
4647
(void)cb_ctx;
4748
video_frame_info_t info;
4849
size_t buf_size = UVC_MAX_FRAMESIZE_SIZE;
49-
avi_player_get_video_buffer((void **)&s_fb.buf, &buf_size, &info, pdMS_TO_TICKS(200));
50+
avi_player_get_video_buffer(avi_handle, (void **)&s_fb.buf, &buf_size, &info, pdMS_TO_TICKS(200));
5051
ESP_LOGD(TAG, "Camera buf_size: %d", buf_size);
5152
uint64_t us = (uint64_t)esp_timer_get_time();
5253
s_fb.timestamp.tv_sec = us / 1000000UL;
@@ -77,7 +78,7 @@ static void camera_fb_return_cb(uvc_fb_t *fb, void *cb_ctx)
7778
static void avi_end_cb(void *arg)
7879
{
7980
if (running) {
80-
avi_player_play_from_file("/spiffs/p4_introduce.avi");
81+
avi_player_play_from_file(avi_handle, "/spiffs/p4_introduce.avi");
8182
}
8283
}
8384

@@ -88,7 +89,7 @@ esp_err_t usb_cam1_init(void)
8889
.buffer_size = UVC_MAX_FRAMESIZE_SIZE,
8990
};
9091

91-
avi_player_init(avi_cfg);
92+
avi_player_init(avi_cfg, &avi_handle);
9293

9394
s_fb.buf = (uint8_t *)malloc(UVC_MAX_FRAMESIZE_SIZE);
9495
if (s_fb.buf == NULL) {

0 commit comments

Comments
 (0)