-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMJPEG_2task_Audio_1task.ino
More file actions
312 lines (279 loc) · 12.5 KB
/
Copy pathMJPEG_2task_Audio_1task.ino
File metadata and controls
312 lines (279 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/***
* Required libraries:
* Arduino_GFX: https://github.com/moononournation/Arduino_GFX.git
* libhelix: https://github.com/pschatzmann/arduino-libhelix.git
* JPEGDEC: https://github.com/bitbank2/JPEGDEC.git
*/
// auto fall back to MP3 if AAC file not available
#define AAC_FILENAME "/44100.aac"
#define MP3_FILENAME "/44100.mp3"
#define MJPEG_FILENAME "/320_30fps.mjpeg"
// #define MJPEG_FILENAME "/320_30fps.mjpeg"
#define FPS 30
#define MJPEG_BUFFER_SIZE (320 * 240 * 2 / 8)
// #define MJPEG_BUFFER_SIZE (320 * 240 * 2 / 8)
#define AUDIOASSIGNCORE 1
#define DECODEASSIGNCORE 0
#define DRAWASSIGNCORE 1
#define SDMMC_D3 15 // SDMMC Data3 / SPI CS
#define SDMMC_CMD 13 // SDMMC CMD / SPI MOSI
#define SDMMC_CLK 14 // SDMMC CLK / SPI SCK
// #define SDMMC_D0 2 // SDMMC Data0 / SPI MISO
#define SDMMC_D0 12 // SDMMC Data0 / SPI MISO
#include <WiFi.h>
#include "FS.h"
#include "SPI.h"
// #include <FFat.h>
// #include <LittleFS.h>
#include "SD.h"
// #include <SD_MMC.h>
// #include <SPIFFS.h>
#if __has_include(<driver/i2s.h>)
#include <driver/i2s.h>
#endif
#ifndef BLACK
#define BLACK 0x0000
#endif
#ifndef WHITE
#define WHITE 0xFFFF
#endif
/*******************************************************************************
* Start of Arduino_GFX setting
******************************************************************************/
#include <Arduino_GFX_Library.h>
#define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin
Arduino_DataBus *bus = create_default_Arduino_DataBus();
// Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 3 /* rotation */, false /* IPS */);
Arduino_GFX *gfx = new Arduino_ST7789(bus, DF_GFX_RST, 1 /* rotation */, false /* IPS */, 240 /* width */, 320 /* height */, 0 /* col offset 1 */, 0 /* row offset 1 */, 0 /* col offset 2 */, 0 /* row offset 2 */);
/*******************************************************************************
* End of Arduino_GFX setting
******************************************************************************/
/* variables */
static int next_frame = 0;
static int skipped_frames = 0;
static unsigned long start_ms, curr_ms, next_frame_ms;
/* audio */
#include "esp32_audio_task.h"
/* MJPEG Video */
#include "mjpeg_decode_draw_task.h"
// pixel drawing callback
static int drawMCU(JPEGDRAW *pDraw) {
// Serial.printf("Draw pos = (%d, %d), size = %d x %d\n", pDraw->x, pDraw->y, pDraw->iWidth, pDraw->iHeight);
unsigned long s = millis();
gfx->draw16bitRGBBitmap(pDraw->x, pDraw->y, pDraw->pPixels, pDraw->iWidth, pDraw->iHeight);
total_show_video_ms += millis() - s;
return 1;
} /* drawMCU() */
void setup() {
WiFi.mode(WIFI_OFF);
Serial.begin(115200);
// Serial.setDebugOutput(true);
// while(!Serial);
Serial.println("MJPEG_2task_Audio_1task");
#ifdef GFX_EXTRA_PRE_INIT
GFX_EXTRA_PRE_INIT();
#endif
Serial.println("Init display");
if (!gfx->begin(80000000)) {
Serial.println("Init display failed!");
}
gfx->fillScreen(BLACK);
#ifdef GFX_BL
pinMode(GFX_BL, OUTPUT);
digitalWrite(GFX_BL, HIGH);
#endif
Serial.println("Init audio output");
gfx->println("Init audio output");
#if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32)
esp_err_t ret_val = i2s_init_internal_dac(I2S_NUM_0, 44100);
#elif defined(ESP32) && (CONFIG_IDF_TARGET_ESP32S2)
esp_err_t ret_val = i2s_init(I2S_NUM_0, 44100, -1 /* MCLK */, 4 /* SCLK */, 5 /* LRCK */, 18 /* DOUT */, -1 /* DIN */);
#elif defined(ESP32) && (CONFIG_IDF_TARGET_ESP32S3)
esp_err_t ret_val = i2s_init(I2S_NUM_0, 44100, 42 /* MCLK */, 46 /* SCLK */, 45 /* LRCK */, 43 /* DOUT */, 44 /* DIN */);
#elif defined(ESP32) && (CONFIG_IDF_TARGET_ESP32C3)
esp_err_t ret_val = i2s_init(I2S_NUM_0, 44100, -1 /* MCLK */, 10 /* SCLK */, 19 /* LRCK */, 18 /* DOUT */, -1 /* DIN */);
#endif
if (ret_val != ESP_OK) {
#if defined(ESP32) && (CONFIG_IDF_TARGET_ESP32)
Serial.printf("i2s_init_internal_dac failed: %d\n", ret_val);
#else
Serial.printf("i2s_init failed: %d\n", ret_val);
#endif
}
i2s_zero_dma_buffer(I2S_NUM_0);
Serial.println("Init FS");
gfx->println("Init FS");
SPI.begin(SDMMC_CLK, SDMMC_D0 /* MISO */, SDMMC_CMD /* MOSI */);
if (!SD.begin(SDMMC_D3 /* SS */))
{
Serial.println("ERROR: File system mount failed!");
gfx->println("ERROR: File system mount failed!");
} else {
bool aac_file_available = false;
Serial.println("Open AAC file: " AAC_FILENAME);
gfx->println("Open AAC file: " AAC_FILENAME);
File aFile = SD.open(AAC_FILENAME);
if (aFile) {
aac_file_available = true;
} else {
Serial.println("Open MP3 file: " MP3_FILENAME);
gfx->println("Open MP3 file: " MP3_FILENAME);
// aFile = LittleFS.open(MP3_FILENAME);
// aFile = SPIFFS.open(MP3_FILENAME);
// aFile = FFat.open(MP3_FILENAME);
aFile = SD.open(MP3_FILENAME);
// aFile = SD_MMC.open(MP3_FILENAME);
}
if (!aFile || aFile.isDirectory()) {
Serial.println("ERROR: Failed to open " AAC_FILENAME " or " MP3_FILENAME " file for reading");
gfx->println("ERROR: Failed to open " AAC_FILENAME " or " MP3_FILENAME " file for reading");
} else {
Serial.println("Open MJPEG file: " MJPEG_FILENAME);
gfx->println("Open MJPEG file: " MJPEG_FILENAME);
// File vFile = LittleFS.open(MJPEG_FILENAME);
// File vFile = SPIFFS.open(MJPEG_FILENAME);
// File vFile = FFat.open(MJPEG_FILENAME);
File vFile = SD.open(MJPEG_FILENAME);
// File vFile = SD_MMC.open(MJPEG_FILENAME);
if (!vFile || vFile.isDirectory()) {
Serial.println("ERROR: Failed to open " MJPEG_FILENAME " file for reading");
gfx->println("ERROR: Failed to open " MJPEG_FILENAME " file for reading");
} else {
Serial.println("Init video");
gfx->println("Init video");
mjpeg_setup(&vFile, MJPEG_BUFFER_SIZE, drawMCU,
false /* useBigEndian */, DECODEASSIGNCORE, DRAWASSIGNCORE);
Serial.println("Start play audio task");
gfx->println("Start play audio task");
BaseType_t ret_val;
if (aac_file_available) {
ret_val = aac_player_task_start(&aFile, AUDIOASSIGNCORE);
} else {
ret_val = mp3_player_task_start(&aFile, AUDIOASSIGNCORE);
}
if (ret_val != pdPASS) {
Serial.printf("Audio player task start failed: %d\n", ret_val);
gfx->printf("Audio player task start failed: %d\n", ret_val);
}
Serial.println("Start play video");
gfx->println("Start play video");
start_ms = millis();
curr_ms = millis();
next_frame_ms = start_ms + (++next_frame * 1000 / FPS / 2);
while (vFile.available() && mjpeg_read_frame()) // Read video
{
total_read_video_ms += millis() - curr_ms;
curr_ms = millis();
if (millis() < next_frame_ms) // check show frame or skip frame
{
// Play video
mjpeg_draw_frame();
total_decode_video_ms += millis() - curr_ms;
curr_ms = millis();
} else {
++skipped_frames;
Serial.println("Skip frame");
}
while (millis() < next_frame_ms) {
vTaskDelay(pdMS_TO_TICKS(1));
}
curr_ms = millis();
next_frame_ms = start_ms + (++next_frame * 1000 / FPS);
}
int time_used = millis() - start_ms;
int total_frames = next_frame - 1;
Serial.println("AV end");
vFile.close();
aFile.close();
delay(200);
int played_frames = total_frames - skipped_frames;
float fps = 1000.0 * played_frames / time_used;
total_decode_audio_ms -= total_play_audio_ms;
// total_decode_video_ms -= total_show_video_ms;
Serial.printf("Played frames: %d\n", played_frames);
Serial.printf("Skipped frames: %d (%0.1f %%)\n", skipped_frames, 100.0 * skipped_frames / total_frames);
Serial.printf("Time used: %d ms\n", time_used);
Serial.printf("Expected FPS: %d\n", FPS);
Serial.printf("Actual FPS: %0.1f\n", fps);
Serial.printf("Read audio: %lu ms (%0.1f %%)\n", total_read_audio_ms, 100.0 * total_read_audio_ms / time_used);
Serial.printf("Decode audio: %lu ms (%0.1f %%)\n", total_decode_audio_ms, 100.0 * total_decode_audio_ms / time_used);
Serial.printf("Play audio: %lu ms (%0.1f %%)\n", total_play_audio_ms, 100.0 * total_play_audio_ms / time_used);
Serial.printf("Read video: %lu ms (%0.1f %%)\n", total_read_video_ms, 100.0 * total_read_video_ms / time_used);
Serial.printf("Decode video: %lu ms (%0.1f %%)\n", total_decode_video_ms, 100.0 * total_decode_video_ms / time_used);
Serial.printf("Show video: %lu ms (%0.1f %%)\n", total_show_video_ms, 100.0 * total_show_video_ms / time_used);
#define CHART_MARGIN 64
#define LEGEND_A_COLOR 0x1BB6
#define LEGEND_B_COLOR 0xFBE1
#define LEGEND_C_COLOR 0x2D05
#define LEGEND_D_COLOR 0xD125
#define LEGEND_E_COLOR 0x9337
#define LEGEND_F_COLOR 0x8AA9
#define LEGEND_G_COLOR 0xE3B8
#define LEGEND_H_COLOR 0x7BEF
#define LEGEND_I_COLOR 0xBDE4
#define LEGEND_J_COLOR 0x15F9
// gfx->setCursor(0, 0);
gfx->setTextColor(WHITE);
gfx->printf("Played frames: %d\n", played_frames);
gfx->printf("Skipped frames: %d (%0.1f %%)\n", skipped_frames, 100.0 * skipped_frames / total_frames);
gfx->printf("Time used: %d ms\n", time_used);
gfx->printf("Expected FPS: %d\n", FPS);
gfx->printf("Actual FPS: %0.1f\n\n", fps);
int16_t r1 = ((gfx->height() - CHART_MARGIN - CHART_MARGIN) / 2);
int16_t r2 = r1 / 2;
int16_t cx = gfx->width() - r1 - 10;
int16_t cy = r1 + CHART_MARGIN;
float arc_start1 = 0;
float arc_end1 = arc_start1 + max(2.0, 360.0 * total_read_audio_ms / time_used);
for (int i = arc_start1 + 1; i < arc_end1; i += 2) {
gfx->fillArc(cx, cy, r1, r2, arc_start1 - 90.0, i - 90.0, LEGEND_A_COLOR);
}
gfx->fillArc(cx, cy, r1, r2, arc_start1 - 90.0, arc_end1 - 90.0, LEGEND_A_COLOR);
gfx->setTextColor(LEGEND_A_COLOR);
gfx->printf("Read audio: %lu ms (%0.1f %%)\n", total_read_audio_ms, 100.0 * total_read_audio_ms / time_used);
float arc_start2 = arc_end1;
float arc_end2 = arc_start2 + max(2.0, 360.0 * total_decode_audio_ms / time_used);
for (int i = arc_start2 + 1; i < arc_end2; i += 2) {
gfx->fillArc(cx, cy, r1, r2, arc_start2 - 90.0, i - 90.0, LEGEND_B_COLOR);
}
gfx->fillArc(cx, cy, r1, r2, arc_start2 - 90.0, arc_end2 - 90.0, LEGEND_B_COLOR);
gfx->setTextColor(LEGEND_B_COLOR);
gfx->printf("Decode audio: %lu ms (%0.1f %%)\n", total_decode_audio_ms, 100.0 * total_decode_audio_ms / time_used);
gfx->setTextColor(LEGEND_J_COLOR);
gfx->printf("Play audio: %lu ms (%0.1f %%)\n", total_play_audio_ms, 100.0 * total_play_audio_ms / time_used);
float arc_start3 = arc_end2;
float arc_end3 = arc_start3 + max(2.0, 360.0 * total_read_video_ms / time_used);
for (int i = arc_start3 + 1; i < arc_end3; i += 2) {
gfx->fillArc(cx, cy, r1, r2, arc_start3 - 90.0, i - 90.0, LEGEND_C_COLOR);
}
gfx->fillArc(cx, cy, r1, r2, arc_start3 - 90.0, arc_end3 - 90.0, LEGEND_C_COLOR);
gfx->setTextColor(LEGEND_C_COLOR);
gfx->printf("Read video: %lu ms (%0.1f %%)\n", total_read_video_ms, 100.0 * total_read_video_ms / time_used);
float arc_start4 = arc_end3;
float arc_end4 = arc_start4 + max(2.0, 360.0 * total_show_video_ms / time_used);
for (int i = arc_start4 + 1; i < arc_end4; i += 2) {
gfx->fillArc(cx, cy, r1, r2, arc_start4 - 90.0, i - 90.0, LEGEND_D_COLOR);
}
gfx->fillArc(cx, cy, r1, r2, arc_start4 - 90.0, arc_end4 - 90.0, LEGEND_D_COLOR);
gfx->setTextColor(LEGEND_D_COLOR);
gfx->printf("Show video: %lu ms (%0.1f %%)\n", total_show_video_ms, 100.0 * total_show_video_ms / time_used);
float arc_start5 = 0;
float arc_end5 = arc_start5 + max(2.0, 360.0 * total_decode_video_ms / time_used);
for (int i = arc_start5 + 1; i < arc_end5; i += 2) {
gfx->fillArc(cx, cy, r2, 0, arc_start5 - 90.0, i - 90.0, LEGEND_E_COLOR);
}
gfx->fillArc(cx, cy, r2, 0, arc_start5 - 90.0, arc_end5 - 90.0, LEGEND_E_COLOR);
gfx->setTextColor(LEGEND_E_COLOR);
gfx->printf("Decode video: %lu ms (%0.1f %%)\n", total_decode_video_ms, 100.0 * total_decode_video_ms / time_used);
}
// delay(60000);
#ifdef GFX_BL
// digitalWrite(GFX_BL, LOW);
#endif
// gfx->displayOff();
// esp_deep_sleep_start();
}
}
}
void loop() {
}