-
Notifications
You must be signed in to change notification settings - Fork 754
/
Copy pathmain.c
344 lines (323 loc) · 11.4 KB
/
main.c
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* RTMP Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_debug_helpers.h"
#include "freertos/xtensa_context.h"
#include "audio_event_iface.h"
#include "audio_common.h"
#include "esp_peripherals.h"
#include "periph_wifi.h"
#include "esp_wifi.h"
#include "board.h"
#include "media_lib_adapter.h"
#include "media_lib_os.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "rtmp_src_app.h"
#include "rtmp_push_app.h"
#include "rtmp_server_app.h"
#include "audio_mem.h"
#include "rtmp_app_setting.h"
#include "audio_sys.h"
#include "esp_console.h"
#include "esp_idf_version.h"
#include "esp_err.h"
/**
* @brief Setup test duration for RTMP applications (unit ms)
*/
#define RTMP_APP_RUN_DURATION (6000000)
#define TAG "RTMP_APP"
typedef enum {
RTMP_APP_TYPE_PUSHER = 1,
RTMP_APP_TYPE_SRC = 2,
RTMP_APP_TYPE_SERVER = 4,
} rtmp_app_type_t;
static int app_running = 0;
static char *rtmp_server_url = NULL;
static esp_periph_set_handle_t set;
static esp_periph_handle_t wifi_handle;
static bool wifi_connected = false;
static rtmp_app_type_t rtmp_app_type = RTMP_APP_TYPE_PUSHER;
#define SERVER_URL (rtmp_server_url ? rtmp_server_url : CONFIG_RTMP_SERVER_URI)
static int connect_sta(const char *ssid, const char *psw)
{
periph_wifi_cfg_t wifi_cfg = {0};
memset(&wifi_cfg, 0, sizeof(wifi_cfg));
strncpy((char *)wifi_cfg.wifi_config.sta.ssid, ssid ? ssid : CONFIG_WIFI_SSID, sizeof(wifi_cfg.wifi_config.sta.ssid) - 1);
strncpy((char *)wifi_cfg.wifi_config.sta.password, psw ? psw : CONFIG_WIFI_PASSWORD, sizeof(wifi_cfg.wifi_config.sta.password) - 1);
if (wifi_handle) {
esp_wifi_disconnect();
esp_wifi_stop();
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg.wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
} else {
wifi_handle = periph_wifi_init(&wifi_cfg);
esp_periph_init(wifi_handle);
ESP_LOGI(TAG, "Waiting for wifi connected");
}
periph_wifi_wait_for_connected(wifi_handle, 6000 / portTICK_PERIOD_MS);
if (periph_wifi_is_connected(wifi_handle) == PERIPH_WIFI_CONNECTED) {
ESP_LOGI(TAG, "Wifi connected OK");
wifi_connected = true;
} else {
wifi_connected = false;
ESP_LOGI(TAG, "Wifi connect fail, please check your settings");
}
return 0;
}
static int mount_sdcard(esp_periph_set_handle_t set)
{
audio_board_sdcard_init(set, SD_MODE_1_LINE);
return 0;
}
static void app_thread(void *arg)
{
rtmp_app_type_t app_type = (rtmp_app_type_t) (uint32_t) arg;
if (app_type == RTMP_APP_TYPE_PUSHER) {
rtmp_push_app_run(SERVER_URL , RTMP_APP_RUN_DURATION);
app_running--;
} else if (app_type == RTMP_APP_TYPE_SRC) {
rtmp_src_app_run(SERVER_URL, RTMP_APP_RUN_DURATION);
app_running--;
} else if (app_type == RTMP_APP_TYPE_SERVER) {
rtmp_server_app_run(SERVER_URL, RTMP_APP_RUN_DURATION + 1000);
app_running--;
}
media_lib_thread_destroy(NULL);
}
static void start_app()
{
rtmp_app_type_t app_type = RTMP_APP_TYPE_PUSHER;
#ifdef CONFIG_RTMP_APP_MODE_PUSHER
app_type = RTMP_APP_TYPE_PUSHER;
#endif
#ifdef CONFIG_RTMP_APP_MODE_SOURCE
app_type = RTMP_APP_TYPE_SRC;
#endif
#ifdef CONFIG_RTMP_APP_MODE_PUSHER_SERVER
app_type = RTMP_APP_TYPE_PUSHER | RTMP_APP_TYPE_SERVER;
#endif
app_type = rtmp_app_type ? rtmp_app_type : app_type;
if (app_running == 0) {
media_lib_thread_handle_t thread_handle;
audio_mem_print(TAG, __LINE__, __func__);
if (app_type & RTMP_APP_TYPE_SERVER) {
app_running++;
media_lib_thread_create(&thread_handle, "server", app_thread, (void *) RTMP_APP_TYPE_SERVER, 5 * 1024, 10,
0);
// Waiting for server started
media_lib_thread_sleep(500);
}
if (app_type & RTMP_APP_TYPE_PUSHER) {
app_running++;
media_lib_thread_create(&thread_handle, "pusher", app_thread, (void *) RTMP_APP_TYPE_PUSHER, 8 * 1024, 10,
0);
}
if (app_type & RTMP_APP_TYPE_SRC) {
app_running++;
media_lib_thread_create(&thread_handle, "source", app_thread, (void *) RTMP_APP_TYPE_SRC, 5 * 1024, 10, 0);
}
media_lib_thread_sleep(5000);
audio_mem_print(TAG, __LINE__, __func__);
} else {
ESP_LOGI(TAG, "Application still running, please wait!");
}
}
static int setting_cli(int argc, char **argv)
{
int i = 1;
while (i + 1 < argc) {
if (strcmp(argv[i], "url") == 0) {
if (rtmp_server_url) {
free(rtmp_server_url);
}
rtmp_server_url = strdup(argv[++i]);
} else if (strcmp(argv[i], "sw_jpeg") == 0) {
rtmp_setting_set_sw_jpeg((bool)atoi(argv[++i]));
} else if (strcmp(argv[i], "quality") == 0) {
rtmp_setting_set_video_quality((av_record_video_quality_t)atoi(argv[++i]));
} else if (strcmp(argv[i], "fps") == 0) {
rtmp_setting_set_video_fps((uint8_t)atoi(argv[++i]));
} else if (strcmp(argv[i], "channel") == 0) {
rtmp_setting_set_audio_channel((uint8_t)atoi(argv[++i]));
} else if (strcmp(argv[i], "sample_rate") == 0) {
rtmp_setting_set_audio_sample_rate(atoi(argv[++i]));
} else if (strcmp(argv[i], "afmt") == 0) {
rtmp_setting_set_audio_fmt((av_record_audio_fmt_t)atoi(argv[++i]));
} else if (strcmp(argv[i], "vfmt") == 0) {
rtmp_setting_set_video_fmt((av_record_video_fmt_t)atoi(argv[++i]));
} else if (strcmp(argv[i], "insecure") == 0) {
rtmp_setting_set_allow_insecure((bool)atoi(argv[++i]));
}
i++;
}
uint16_t width, height;
av_record_get_video_size(rtmp_setting_get_video_quality(), &width, &height);
ESP_LOGI(TAG, "RTMP server url: %s", SERVER_URL);
ESP_LOGI(TAG, "Video setting | format:%s sw_jpeg:%d quality:%dx%d fps:%d",
av_record_get_vfmt_str(rtmp_setting_get_video_fmt()),
rtmp_setting_get_sw_jpeg(), width, height, rtmp_setting_get_video_fps());
ESP_LOGI(TAG, "Audio setting | format:%s channel:%d sample_rate:%d",
av_record_get_afmt_str(rtmp_setting_get_audio_fmt()),
rtmp_setting_get_audio_channel(), rtmp_setting_get_audio_sample_rate());
return 0;
}
static int start_cli(int argc, char **argv)
{
if (wifi_connected == false) {
connect_sta(NULL, NULL);
if (wifi_connected == false) {
ESP_LOGI(TAG, "Please connect to wifi firstly");
return 0;
}
}
rtmp_setting_set_allow_run(true);
start_app();
return 0;
}
static int connect_cli(int argc, char **argv)
{
if (argc == 3) {
connect_sta(argv[1], argv[2]);
} else if (argc == 1) {
connect_sta(NULL, NULL);
}
return 0;
}
static int stop_cli(int argc, char **argv)
{
rtmp_setting_set_allow_run(false);
return 0;
}
static int assert_cli(int argc, char **argv)
{
*(int *) 0 = 0;
return 0;
}
static const char *get_mode_str(rtmp_app_type_t app_type)
{
switch ((int)app_type) {
case RTMP_APP_TYPE_PUSHER:
return "Pusher";
case RTMP_APP_TYPE_SRC:
return "Src";
case RTMP_APP_TYPE_SERVER:
return "Server";
case (RTMP_APP_TYPE_SRC | RTMP_APP_TYPE_PUSHER):
return "Src + Pusher";
case (RTMP_APP_TYPE_SRC | RTMP_APP_TYPE_SERVER):
return "Src + Server";
case (RTMP_APP_TYPE_PUSHER | RTMP_APP_TYPE_SERVER):
return "Pusher + Server";
case (RTMP_APP_TYPE_SRC | RTMP_APP_TYPE_PUSHER | RTMP_APP_TYPE_SERVER):
return "Pusher + Server + Src";
default:
return "Undef";
}
}
static int mode_cli(int argc, char **argv)
{
if (argc > 1) {
rtmp_app_type = (rtmp_app_type_t) atoi(argv[1]);
}
ESP_LOGI(TAG, "Working in mode: %s", get_mode_str(rtmp_app_type));
return 0;
}
static int cpu_cli(int argc, char **argv)
{
audio_mem_print(TAG, __LINE__, __func__);
audio_sys_get_real_time_stats();
return 0;
}
static int init_console()
{
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0))
esp_console_repl_t *repl = NULL;
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
repl_config.prompt = "esp>";
// install console REPL environment
#if CONFIG_ESP_CONSOLE_UART
esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
#elif CONFIG_ESP_CONSOLE_USB_CDC
esp_console_dev_usb_cdc_config_t cdc_config = ESP_CONSOLE_DEV_CDC_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_usb_cdc(&cdc_config, &repl_config, &repl));
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
esp_console_dev_usb_serial_jtag_config_t usbjtag_config = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&usbjtag_config, &repl_config, &repl));
#endif
#endif
esp_console_cmd_t console_cmd[] = {
{
.func = connect_cli,
.help = "Connect to wifi: ssid(%s) psw(%s)",
.command = "connect",
},
{
.func = setting_cli,
.help = "Settings: url(%s) vfmt(%d) sw_jpeg(%d) fps(%d) quality(%d) afmt(%d) channel(%d) sample_rate(%d)",
.command = "set",
},
{
.func = start_cli,
.help = "Start to run RTMP applications",
.command = "start",
},
{
.func = stop_cli,
.help = "Stop RTMP applications",
.command = "stop",
},
{
.func = mode_cli,
.help = "Setting RTMP working mode:(%d) 1:(Pusher|default) 2:(Src) 4:(Server) 5:(Pusher+Server) etc",
.command = "mode",
},
{
.func = cpu_cli,
.help = "CPU usage",
.command = "i",
},
{
.func = assert_cli,
.help = "Assert system to debug system hang issue",
.command = "assert",
},
};
for (int i = 0; i < sizeof(console_cmd) / sizeof(esp_console_cmd_t); i++) {
ESP_ERROR_CHECK(esp_console_cmd_register(&console_cmd[i]));
}
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0))
ESP_ERROR_CHECK(esp_console_start_repl(repl));
#endif
return 0;
}
void app_main(void)
{
esp_log_level_set("*", ESP_LOG_INFO);
media_lib_add_default_adapter();
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
}
ESP_ERROR_CHECK(esp_netif_init());
audio_board_handle_t audio_board = audio_board_init();
if (audio_board == NULL) {
ESP_LOGE(TAG, "Fail to init audio board");
return;
}
audio_hal_ctrl_codec(audio_board->audio_hal, AUDIO_HAL_CODEC_MODE_BOTH, AUDIO_HAL_CTRL_START);
audio_hal_set_volume(audio_board->audio_hal, 0);
esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
set = esp_periph_set_init(&periph_cfg);
init_console();
mount_sdcard(set);
}