From 8640bf35e76aa19710a563144e30d31db2685746 Mon Sep 17 00:00:00 2001 From: Mistivia Date: Sun, 14 Sep 2025 00:42:27 +0800 Subject: fix s3 client init bug --- config.c | 100 --------------------------------------------------- config.h | 22 ------------ ezlive_config.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ezlive_config.h | 22 ++++++++++++ main.c | 18 +++++++++- s3_client.cpp | 8 ++--- s3_worker.c | 3 +- transcode_talker.c | 3 +- 8 files changed, 150 insertions(+), 130 deletions(-) delete mode 100644 config.c delete mode 100644 config.h create mode 100644 ezlive_config.c create mode 100644 ezlive_config.h diff --git a/config.c b/config.c deleted file mode 100644 index 458c79c..0000000 --- a/config.c +++ /dev/null @@ -1,100 +0,0 @@ -#include "config.h" - -#include -#include -#include -#include - -EZLiveConfig *ezlive_config; - -static char *trim(char *s) { - char *end; - while (isspace((unsigned char)*s)) s++; - if (*s == 0) return s; - end = s + strlen(s) - 1; - while (end > s && isspace((unsigned char)*end)) end--; - end[1] = '\0'; - return s; -} - -void EZLiveConfig_init(EZLiveConfig *self) { - if (!self) return; - self->listening_addr = strdup("127.0.0.1"); - self->listening_port = 1935; - self->bucket = NULL; - self->endpoint = NULL; - self->s3_path = NULL; - self->access_key = NULL; - self->secret_key = NULL; - self->web_endpoint = NULL; - self->region = strdup("auto"); -} - -static void set_field(const char **field, const char *value) { - if (*field) { - free((void *)*field); - } - *field = strdup(value); -} - -void EZLiveConfig_load(EZLiveConfig *self, const char *filename) { - if (!self || !filename) return; - - FILE *fp = fopen(filename, "r"); - if (!fp) { - perror("fopen"); - return; - } - - char line[1024]; - while (fgets(line, sizeof(line), fp)) { - line[strcspn(line, "\n")] = 0; - char *hash = strchr(line, '#'); - if (hash) *hash = 0; - - char *trimmed = trim(line); - if (*trimmed == 0) continue; - - char *eq = strchr(trimmed, '='); - if (!eq) continue; - - *eq = 0; - char *key = trim(trimmed); - char *val = trim(eq + 1); - - if (strcmp(key, "listening_addr") == 0) { - set_field(&self->listening_addr, val); - } else if (strcmp(key, "listening_port") == 0) { - self->listening_port = atoi(val); - } else if (strcmp(key, "bucket") == 0) { - set_field(&self->bucket, val); - } else if (strcmp(key, "endpoint") == 0) { - set_field(&self->endpoint, val); - } else if (strcmp(key, "s3_path") == 0) { - set_field(&self->s3_path, val); - } else if (strcmp(key, "access_key") == 0) { - set_field(&self->access_key, val); - } else if (strcmp(key, "secret_key") == 0) { - set_field(&self->secret_key, val); - } else if (strcmp(key, "web_endpoint") == 0) { - set_field(&self->web_endpoint, val); - } else if (strcmp(key, "region") == 0) { - set_field(&self->region, val); - } - } - - fclose(fp); -} - -int EZLiveConfig_validate(EZLiveConfig *self) { - if (!self) return -1; - if (!self->listening_addr || strlen(self->listening_addr) == 0) return -2; - if (!self->bucket || strlen(self->bucket) == 0) return -3; - if (!self->endpoint || strlen(self->endpoint) == 0) return -4; - if (!self->s3_path || strlen(self->s3_path) == 0) return -5; - if (!self->access_key || strlen(self->access_key) == 0) return -6; - if (!self->secret_key || strlen(self->secret_key) == 0) return -7; - if (self->listening_port <= 0 || self->listening_port > 65535) return -8; - if (!self->region || strlen(self->region) == 0) return -9; - return 0; -} \ No newline at end of file diff --git a/config.h b/config.h deleted file mode 100644 index 58b3f7d..0000000 --- a/config.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef EZLIVE_CONFIG_H_ -#define EZLIVE_CONFIG_H_ - -typedef struct { - const char *listening_addr; - int listening_port; - const char *bucket; - const char *endpoint; - const char *s3_path; - const char *access_key; - const char *secret_key; - const char *web_endpoint; - const char *region; -} EZLiveConfig; - -extern EZLiveConfig *ezlive_config; - -void EZLiveConfig_init(EZLiveConfig *self); -void EZLiveConfig_load(EZLiveConfig *self, const char *filename); -int EZLiveConfig_validate(EZLiveConfig *self); - -#endif \ No newline at end of file diff --git a/ezlive_config.c b/ezlive_config.c new file mode 100644 index 0000000..08347d8 --- /dev/null +++ b/ezlive_config.c @@ -0,0 +1,104 @@ +#include "ezlive_config.h" + +#include +#include +#include +#include + +EZLiveConfig *ezlive_config; + +static char *trim(char *s) { + char *end; + while (isspace((unsigned char)*s)) s++; + if (*s == 0) return s; + end = s + strlen(s) - 1; + while (end > s && isspace((unsigned char)*end)) end--; + end[1] = '\0'; + return s; +} + +void EZLiveConfig_init(EZLiveConfig *self) { + if (!self) return; + self->listening_addr = strdup("127.0.0.1"); + self->listening_port = 1935; + self->bucket = NULL; + self->endpoint = NULL; + self->s3_path = NULL; + self->access_key = NULL; + self->secret_key = NULL; + self->web_endpoint = NULL; + self->region = strdup("auto"); +} + +static void set_field(const char **field, const char *value) { + if (*field) { + free((void *)*field); + } + *field = strdup(value); +} + +void EZLiveConfig_load(EZLiveConfig *self, const char *filename) { + if (!self || !filename) return; + + FILE *fp = fopen(filename, "r"); + if (!fp) { + perror("fopen"); + return; + } + + char line[1024]; + while (fgets(line, sizeof(line), fp)) { + line[strcspn(line, "\n")] = 0; + char *hash = strchr(line, '#'); + if (hash) *hash = 0; + + char *trimmed = trim(line); + if (*trimmed == 0) continue; + + char *eq = strchr(trimmed, '='); + if (!eq) continue; + + *eq = 0; + char *key = trim(trimmed); + char *val = trim(eq + 1); + + if (strcmp(key, "listening_addr") == 0) { + set_field(&self->listening_addr, val); + } else if (strcmp(key, "listening_port") == 0) { + self->listening_port = atoi(val); + } else if (strcmp(key, "bucket") == 0) { + set_field(&self->bucket, val); + } else if (strcmp(key, "endpoint") == 0) { + set_field(&self->endpoint, val); + } else if (strcmp(key, "s3_path") == 0) { + set_field(&self->s3_path, val); + } else if (strcmp(key, "access_key") == 0) { + set_field(&self->access_key, val); + } else if (strcmp(key, "secret_key") == 0) { + set_field(&self->secret_key, val); + } else if (strcmp(key, "web_endpoint") == 0) { + set_field(&self->web_endpoint, val); + } else if (strcmp(key, "region") == 0) { + set_field(&self->region, val); + } + } + + fclose(fp); +} + +int EZLiveConfig_validate(EZLiveConfig *self) { + if (!self) return -1; + if (!self->listening_addr || strlen(self->listening_addr) == 0) return -2; + if (!self->bucket || strlen(self->bucket) == 0) return -3; + if (!self->endpoint || strlen(self->endpoint) == 0) return -4; + if (!self->s3_path || strlen(self->s3_path) == 0) return -5; + if (self->s3_path[strlen(self->s3_path) - 1] != '/') { + fprintf(stderr, "invalid s3 path. path should end with '\'.\n"); + return -10; + } + if (!self->access_key || strlen(self->access_key) == 0) return -6; + if (!self->secret_key || strlen(self->secret_key) == 0) return -7; + if (self->listening_port <= 0 || self->listening_port > 65535) return -8; + if (!self->region || strlen(self->region) == 0) return -9; + return 0; +} \ No newline at end of file diff --git a/ezlive_config.h b/ezlive_config.h new file mode 100644 index 0000000..58b3f7d --- /dev/null +++ b/ezlive_config.h @@ -0,0 +1,22 @@ +#ifndef EZLIVE_CONFIG_H_ +#define EZLIVE_CONFIG_H_ + +typedef struct { + const char *listening_addr; + int listening_port; + const char *bucket; + const char *endpoint; + const char *s3_path; + const char *access_key; + const char *secret_key; + const char *web_endpoint; + const char *region; +} EZLiveConfig; + +extern EZLiveConfig *ezlive_config; + +void EZLiveConfig_init(EZLiveConfig *self); +void EZLiveConfig_load(EZLiveConfig *self, const char *filename); +int EZLiveConfig_validate(EZLiveConfig *self); + +#endif \ No newline at end of file diff --git a/main.c b/main.c index adc20c6..2abe3d0 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include +#include "ezlive_config.h" #include "rtmpserver.h" #include "ringbuf.h" #include "transcode_talker.h" @@ -57,7 +58,22 @@ void on_rtmp_audio(void *ctx, int64_t timestamp, char *buf, size_t size) { RingBuffer_write_word32be(rb, size + 11); } -int main() { +int main(int argc, char **argv) { + ezlive_config = malloc(sizeof(EZLiveConfig)); + EZLiveConfig_init(ezlive_config); + if (argc == 1) { + EZLiveConfig_load(ezlive_config, "./config"); + } else if (argc == 2) { + EZLiveConfig_load(ezlive_config, argv[1]); + } else { + fprintf(stderr, "wrong args.\n"); + exit(-1); + } + int ret; + if ((ret = EZLiveConfig_validate(ezlive_config)) < 0) { + fprintf(stderr, "ezlive config error: %d.\n", ret); + exit(-1); + } srand((unsigned) time(NULL)); MainCtx main_ctx; RtmpCallbacks rtmp_cbs = { diff --git a/s3_client.cpp b/s3_client.cpp index aeb3510..42aa05c 100644 --- a/s3_client.cpp +++ b/s3_client.cpp @@ -9,21 +9,21 @@ #include #include -#include "config.h" +#include "ezlive_config.h" #include namespace { Aws::S3::S3Client *s3client; -Aws::SDKOptions aws_options; -Aws::S3::S3ClientConfiguration config; -Aws::Auth::AWSCredentials credentials; } void S3Client_init() { + Aws::SDKOptions aws_options; Aws::InitAPI(aws_options); + Aws::S3::S3ClientConfiguration config; + Aws::Auth::AWSCredentials credentials; config.endpointOverride = ezlive_config->endpoint; config.region = ezlive_config->region; credentials = Aws::Auth::AWSCredentials(ezlive_config->access_key, ezlive_config->secret_key); diff --git a/s3_worker.c b/s3_worker.c index 15fdb09..a6ae2ab 100644 --- a/s3_worker.c +++ b/s3_worker.c @@ -4,7 +4,7 @@ #include #include -#include "config.h" +#include "ezlive_config.h" #include "task_queue.h" #include "s3_client.h" @@ -16,6 +16,7 @@ void exec_s3_task(void *vtask) { if (task->task_type == kUploadTask) { snprintf(obj_name_buf, 255, "%s%s", ezlive_config->s3_path, task->remote_name); S3Client_put(task->local_file, obj_name_buf); + remove(task->local_file); } else if (task->task_type == kDeleteTask) { snprintf(obj_name_buf, 255, "%s%s", ezlive_config->s3_path, task->remote_name); S3Client_delete(obj_name_buf); diff --git a/transcode_talker.c b/transcode_talker.c index 3d22de5..241c6c4 100644 --- a/transcode_talker.c +++ b/transcode_talker.c @@ -2,7 +2,6 @@ #include "fsutils.h" #include "ringbuf.h" -#include #include #include #include @@ -244,7 +243,7 @@ void* TranscodeTalker_main (void *vself) { // open new ts segment_start_pts = pts_time; - snprintf(out_filename, sizeof(out_filename), "segment_%03d.ts", segment_index); + tmp_local_filename("/tmp/ezlive", out_filename); output_stream = start_new_output_file(in_fmt_ctx, &out_fmt_ctx, out_filename, audio_stream_index, video_stream_index); } } -- cgit v1.0