-
Notifications
You must be signed in to change notification settings - Fork 753
/
Copy pathrtmp_src_app.c
94 lines (86 loc) · 2.76 KB
/
rtmp_src_app.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
/* RTMP source application to fetch data from server and callback in FLV container
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 <string.h>
#include "esp_timer.h"
#include "rtmp_src_app.h"
#include "esp_rtmp_src.h"
#include "rtmp_app_setting.h"
#include "esp_log.h"
#define TAG "RTMP Src_App"
#define RTMP_SRC_CHUNK_SIZE (4 * 1024)
#define RTMP_SRC_RING_FIFO_SIZE (500 * 1024)
#define RTMP_SRC_FILE_PATH "/sdcard/src.flv"
static int write_file(void *data, int size)
{
static FILE *fp = NULL;
if (fp == NULL && size > 0) {
fp = fopen(RTMP_SRC_FILE_PATH, "wb");
}
if (fp == NULL) {
ESP_LOGE(TAG, "Fail to open file %s for write", RTMP_SRC_FILE_PATH);
return -1;
}
if (data == NULL || size == 0) {
fclose(fp);
} else {
if (fwrite(data, 1, size, fp) != size) {
ESP_LOGE(TAG, "Fail to write file");
return -1;
}
}
return 0;
}
int rtmp_src_app_run(char *uri, uint32_t duration)
{
int ret;
media_lib_tls_cfg_t ssl_cfg;
rtmp_src_cfg_t cfg = {
.url = uri,
.chunk_size = RTMP_SRC_CHUNK_SIZE,
.fifo_size = RTMP_SRC_RING_FIFO_SIZE,
.thread_cfg = {.priority = 10, .stack_size = 5*1024},
};
if (strncmp(uri, "rtmps://", 8) == 0) {
cfg.ssl_cfg = &ssl_cfg;
rtmp_setting_get_client_ssl_cfg(uri, &ssl_cfg);
}
rtmp_src_handle_t *rtmp_src = esp_rtmp_src_open(&cfg);
if (rtmp_src == NULL) {
ESP_LOGE(TAG, "Fail to open rtmp src");
return -1;
}
uint32_t start_time = esp_timer_get_time() / 1000;
ret = esp_rtmp_src_connect(rtmp_src);
if (ret != 0) {
ESP_LOGE(TAG, "Fail to connect to server");
} else {
uint8_t *data = (uint8_t *) malloc(1024);
if (data) {
while (rtmp_setting_get_allow_run()) {
ret = esp_rtmp_src_read(rtmp_src, data, sizeof(data));
if (ret == 0) {
ret = write_file(data, sizeof(data));
if (ret != 0) {
break;
}
} else {
break;
}
uint32_t cur_time = esp_timer_get_time() / 1000;
if (cur_time > duration && cur_time > start_time + duration) {
break;
}
}
free(data);
}
}
// sync data and close
write_file(NULL, 0);
ret = esp_rtmp_src_close(rtmp_src);
ESP_LOGI(TAG, "Close rtmp return %d\n", ret);
return ret;
}