diff --git a/Makefile b/Makefile index e3c4347..de7499d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ -include config.mk -PROG ?= ustreamer +USTR ?= ustreamer +REC ?= ustreamer-recorder DESTDIR ?= PREFIX ?= /usr/local MANPREFIX ?= $(PREFIX)/share/man @@ -14,14 +15,17 @@ RPI_VC_LIBS ?= /opt/vc/lib BUILD ?= build -LINTERS_IMAGE ?= $(PROG)-linters +LINTERS_IMAGE ?= $(USTR)-linters # ===== -_PROG_LIBS = -lm -ljpeg -pthread -levent -levent_pthreads -luuid override CFLAGS += -c -std=c11 -Wall -Wextra -D_GNU_SOURCE -_PROG_SRCS = $(shell ls \ - src/common/*.c \ + +_COMMON_LIBS = -lm -ljpeg -pthread + +_USTR_LIBS = $(_COMMON_LIBS) -levent -levent_pthreads -luuid +_USTR_SRCS = $(shell ls \ + src/libs/common/*.c \ src/ustreamer/*.c \ src/ustreamer/http/*.c \ src/ustreamer/data/*.c \ @@ -29,6 +33,14 @@ _PROG_SRCS = $(shell ls \ src/ustreamer/encoders/hw/*.c \ ) +_REC_LIBS = $(_COMMON_LIBS) -lrt -lbcm_host -lvcos -lmmal -lmmal_core -lmmal_util -lmmal_vc_client -lmmal_components -L$(RPI_VC_LIBS) +_REC_SRCS = $(shell ls \ + src/libs/common/*.c \ + src/libs/rawsink/*.c \ + src/libs/h264/*.c \ + src/recorder/*.c \ +) + define optbool $(filter $(shell echo $(1) | tr A-Z a-z), yes on 1) @@ -36,23 +48,23 @@ endef ifneq ($(call optbool,$(WITH_RAWSINK)),) -_PROG_LIBS += -lrt +_USTR_LIBS += -lrt override CFLAGS += -DWITH_RAWSINK -_PROG_SRCS += $(shell ls src/rawsink/*.c) +_USTR_SRCS += $(shell ls src/libs/rawsink/*.c) endif ifneq ($(call optbool,$(WITH_OMX)),) -_PROG_LIBS += -lbcm_host -lvcos -lopenmaxil -L$(RPI_VC_LIBS) +_USTR_LIBS += -lbcm_host -lvcos -lopenmaxil -L$(RPI_VC_LIBS) override CFLAGS += -DWITH_OMX -DOMX_SKIP64BIT -I$(RPI_VC_HEADERS) -_PROG_SRCS += $(shell ls src/ustreamer/encoders/omx/*.c) +_USTR_SRCS += $(shell ls src/ustreamer/encoders/omx/*.c) endif ifneq ($(call optbool,$(WITH_GPIO)),) -_PROG_LIBS += -lgpiod +_USTR_LIBS += -lgpiod override CFLAGS += -DWITH_GPIO -_PROG_SRCS += $(shell ls src/ustreamer/gpio/*.c) +_USTR_SRCS += $(shell ls src/ustreamer/gpio/*.c) endif @@ -65,28 +77,43 @@ endif WITH_SETPROCTITLE ?= 1 ifneq ($(call optbool,$(WITH_SETPROCTITLE)),) ifeq ($(shell uname -s | tr A-Z a-z),linux) -_PROG_LIBS += -lbsd +_USTR_LIBS += -lbsd endif override CFLAGS += -DWITH_SETPROCTITLE endif +ifneq ($(call optbool,$(WITH_RAWSINK)),) +ifneq ($(call optbool,$(WITH_OMX)),) +_ENABLE_REC = 1 +endif +endif + + # ===== -all: $(PROG) +all: $(USTR) $(REC) -install: $(PROG) - install -Dm755 $(PROG) $(DESTDIR)$(PREFIX)/bin/$(PROG) - install -Dm644 $(PROG).1 $(DESTDIR)$(MANPREFIX)/man1/$(PROG).1 - gzip $(DESTDIR)$(MANPREFIX)/man1/$(PROG).1 +install: $(USTR) $(REC) + install -Dm755 $(USTR) $(DESTDIR)$(PREFIX)/bin/$(USTR) + install -Dm644 $(USTR).1 $(DESTDIR)$(MANPREFIX)/man1/$(USTR).1 + gzip $(DESTDIR)$(MANPREFIX)/man1/$(USTR).1 +ifneq ($(_ENABLE_REC),) + install -Dm755 $(DESTDIR)$(PREFIX)/bin/$(REC) +endif install-strip: install - strip $(DESTDIR)$(PREFIX)/bin/$(PROG) + strip $(DESTDIR)$(PREFIX)/bin/$(USTR) +ifneq ($(_ENABLE_REC),) + strip $(DESTDIR)$(PREFIX)/bin/$(REC) +endif uninstall: - rm $(DESTDIR)$(PREFIX)/bin/$(PROG) + rm -f $(DESTDIR)$(PREFIX)/bin/$(USTR) \ + $(DESTDIR)$(PREFIX)/bin/$(REC) \ + $(DESTDIR)$(MANPREFIX)/man1/$(USTR).1 regen: @@ -94,16 +121,29 @@ regen: tools/make-html-h.py src/ustreamer/data/index.html src/ustreamer/data/index_html.c INDEX -$(PROG): $(_PROG_SRCS:%.c=$(BUILD)/%.o) +$(USTR): $(_USTR_SRCS:%.c=$(BUILD)/%.o) $(info -- LD $@) - @ $(CC) $^ -o $@ $(LDFLAGS) $(_PROG_LIBS) - $(info ===== Build complete =====) + @ $(CC) $^ -o $@ $(LDFLAGS) $(_USTR_LIBS) $(info == CC = $(CC)) - $(info == LIBS = $(_PROG_LIBS)) + $(info == LIBS = $(_USTR_LIBS)) $(info == CFLAGS = $(CFLAGS)) $(info == LDFLAGS = $(LDFLAGS)) +ifneq ($(_ENABLE_REC),) +$(REC): $(_REC_SRCS:%.c=$(BUILD)/%.o) + $(info -- LD $@) + @ $(CC) $^ -o $@ $(LDFLAGS) $(_REC_LIBS) + $(info == CC = $(CC)) + $(info == LIBS = $(_REC_LIBS)) + $(info == CFLAGS = $(CFLAGS)) + $(info == LDFLAGS = $(LDFLAGS)) +else +$(REC): + @ true +endif + + $(BUILD)/%.o: %.c $(info -- CC $<) @ mkdir -p $(dir $@) || true @@ -152,6 +192,6 @@ clean-all: linters clean -it $(LINTERS_IMAGE) bash -c "cd src && rm -rf linters/{.tox,.mypy_cache}" clean: rm -rf pkg/arch/pkg pkg/arch/src pkg/arch/v*.tar.gz pkg/arch/ustreamer-*.pkg.tar.{xz,zst} - rm -rf $(PROG) $(BUILD) vgcore.* *.sock + rm -rf $(USTR) $(REC) $(BUILD) vgcore.* *.sock .PHONY: linters diff --git a/src/common/config.h b/src/libs/common/config.h similarity index 100% rename from src/common/config.h rename to src/libs/common/config.h diff --git a/src/common/frame.c b/src/libs/common/frame.c similarity index 100% rename from src/common/frame.c rename to src/libs/common/frame.c diff --git a/src/common/frame.h b/src/libs/common/frame.h similarity index 100% rename from src/common/frame.h rename to src/libs/common/frame.h diff --git a/src/common/logging.c b/src/libs/common/logging.c similarity index 100% rename from src/common/logging.c rename to src/libs/common/logging.c diff --git a/src/common/logging.h b/src/libs/common/logging.h similarity index 100% rename from src/common/logging.h rename to src/libs/common/logging.h diff --git a/src/common/process.h b/src/libs/common/process.h similarity index 100% rename from src/common/process.h rename to src/libs/common/process.h diff --git a/src/common/threading.h b/src/libs/common/threading.h similarity index 100% rename from src/common/threading.h rename to src/libs/common/threading.h diff --git a/src/common/tools.h b/src/libs/common/tools.h similarity index 100% rename from src/common/tools.h rename to src/libs/common/tools.h diff --git a/src/h264/encoder.c b/src/libs/h264/encoder.c similarity index 100% rename from src/h264/encoder.c rename to src/libs/h264/encoder.c diff --git a/src/h264/encoder.h b/src/libs/h264/encoder.h similarity index 100% rename from src/h264/encoder.h rename to src/libs/h264/encoder.h diff --git a/src/h264/unjpeg.c b/src/libs/h264/unjpeg.c similarity index 100% rename from src/h264/unjpeg.c rename to src/libs/h264/unjpeg.c diff --git a/src/h264/unjpeg.h b/src/libs/h264/unjpeg.h similarity index 100% rename from src/h264/unjpeg.h rename to src/libs/h264/unjpeg.h diff --git a/src/rawsink/rawsink.c b/src/libs/rawsink/rawsink.c similarity index 100% rename from src/rawsink/rawsink.c rename to src/libs/rawsink/rawsink.c diff --git a/src/rawsink/rawsink.h b/src/libs/rawsink/rawsink.h similarity index 100% rename from src/rawsink/rawsink.h rename to src/libs/rawsink/rawsink.h diff --git a/src/recorder/main.c b/src/recorder/main.c new file mode 100644 index 0000000..63d3222 --- /dev/null +++ b/src/recorder/main.c @@ -0,0 +1,30 @@ +#include +#include "../common/logging.h" +#include "../common/frame.h" +#include "../rawsink/rawsink.h" +#include "../h264/encoder.h" + +int main(void) { + LOGGING_INIT; + log_level = 3; + + frame_s *src = frame_init("src"); + frame_s *dest = frame_init("dest"); + h264_encoder_s *encoder = h264_encoder_init(); + rawsink_s *rawsink = rawsink_init("test", false, 0, 0, (long double)encoder->fps / (long double)encoder->gop); + FILE *fp = fopen("test.h264", "wb"); + + if (rawsink) { + int error = 0; + while ((error = rawsink_client_get(rawsink, src)) != -1) { + if (error == 0 /*|| (error == -2 && src->used > 0)*/) { + if (!h264_encoder_compress(encoder, src, dest, false)) { + LOG_INFO("frame %Lf", get_now_monotonic() - dest->grab_ts); + /*fwrite(dest->data, 1, dest->used, fp); + fflush(fp);*/ + } + } + } + } + return 0; +} diff --git a/src/ustreamer/blank.h b/src/ustreamer/blank.h index 0858b25..d7acf3a 100644 --- a/src/ustreamer/blank.h +++ b/src/ustreamer/blank.h @@ -29,9 +29,9 @@ #include #include -#include "../common/tools.h" -#include "../common/logging.h" -#include "../common/frame.h" +#include "../libs/common/tools.h" +#include "../libs/common/logging.h" +#include "../libs/common/frame.h" #include "data/blank_jpeg.h" diff --git a/src/ustreamer/data/index_html.h b/src/ustreamer/data/index_html.h index ff4fac2..718a828 100644 --- a/src/ustreamer/data/index_html.h +++ b/src/ustreamer/data/index_html.h @@ -24,7 +24,7 @@ #include -#include "../../common/config.h" +#include "../../libs/common/config.h" extern const char *const HTML_INDEX_PAGE; diff --git a/src/ustreamer/device.h b/src/ustreamer/device.h index 54b0141..57efb60 100644 --- a/src/ustreamer/device.h +++ b/src/ustreamer/device.h @@ -41,15 +41,15 @@ #include #include -#include "../common/tools.h" -#include "../common/logging.h" -#include "../common/threading.h" -#include "../common/frame.h" +#include "../libs/common/tools.h" +#include "../libs/common/logging.h" +#include "../libs/common/threading.h" +#include "../libs/common/frame.h" #include "xioctl.h" #ifdef WITH_RAWSINK -# include "../rawsink/rawsink.h" +# include "../libs/rawsink/rawsink.h" #endif diff --git a/src/ustreamer/encoder.h b/src/ustreamer/encoder.h index 09b4557..b5009a7 100644 --- a/src/ustreamer/encoder.h +++ b/src/ustreamer/encoder.h @@ -30,10 +30,10 @@ #include #include -#include "../common/tools.h" -#include "../common/threading.h" -#include "../common/logging.h" -#include "../common/frame.h" +#include "../libs/common/tools.h" +#include "../libs/common/threading.h" +#include "../libs/common/logging.h" +#include "../libs/common/frame.h" #include "device.h" diff --git a/src/ustreamer/encoders/cpu/encoder.h b/src/ustreamer/encoders/cpu/encoder.h index 8bdcfa5..ecb211a 100644 --- a/src/ustreamer/encoders/cpu/encoder.h +++ b/src/ustreamer/encoders/cpu/encoder.h @@ -31,8 +31,8 @@ #include -#include "../../../common/tools.h" -#include "../../../common/frame.h" +#include "../../../libs/common/tools.h" +#include "../../../libs/common/frame.h" void cpu_encoder_compress(frame_s *src, frame_s *dest, unsigned quality); diff --git a/src/ustreamer/encoders/hw/encoder.h b/src/ustreamer/encoders/hw/encoder.h index 68a7e27..2b78d9f 100644 --- a/src/ustreamer/encoders/hw/encoder.h +++ b/src/ustreamer/encoders/hw/encoder.h @@ -29,9 +29,9 @@ #include -#include "../../../common/tools.h" -#include "../../../common/logging.h" -#include "../../../common/frame.h" +#include "../../../libs/common/tools.h" +#include "../../../libs/common/logging.h" +#include "../../../libs/common/frame.h" #include "../../xioctl.h" #include "../../device.h" diff --git a/src/ustreamer/encoders/omx/component.h b/src/ustreamer/encoders/omx/component.h index 945ad69..8ef31ca 100644 --- a/src/ustreamer/encoders/omx/component.h +++ b/src/ustreamer/encoders/omx/component.h @@ -28,7 +28,7 @@ #include #include -#include "../../../common/logging.h" +#include "../../../libs/common/logging.h" #include "formatters.h" diff --git a/src/ustreamer/encoders/omx/encoder.h b/src/ustreamer/encoders/omx/encoder.h index 00c98ed..be4c343 100644 --- a/src/ustreamer/encoders/omx/encoder.h +++ b/src/ustreamer/encoders/omx/encoder.h @@ -36,9 +36,9 @@ #include #include -#include "../../../common/tools.h" -#include "../../../common/logging.h" -#include "../../../common/frame.h" +#include "../../../libs/common/tools.h" +#include "../../../libs/common/logging.h" +#include "../../../libs/common/frame.h" #include "../../device.h" #include "formatters.h" diff --git a/src/ustreamer/encoders/omx/formatters.c b/src/ustreamer/encoders/omx/formatters.c index 50448b9..d5b4ad7 100644 --- a/src/ustreamer/encoders/omx/formatters.c +++ b/src/ustreamer/encoders/omx/formatters.c @@ -26,12 +26,6 @@ #define CASE_TO_STRING(_value) \ case _value: { return #_value; } -#define CASE_ASSERT(_msg, _value) default: { \ - char *_assert_buf; A_CALLOC(_assert_buf, 128); \ - sprintf(_assert_buf, _msg ": 0x%08x", _value); \ - assert(0 && _assert_buf); \ - } - const char *omx_error_to_string(OMX_ERRORTYPE error) { switch (error) { CASE_TO_STRING(OMX_ErrorNone); @@ -93,9 +87,8 @@ const char *omx_state_to_string(OMX_STATETYPE state) { CASE_TO_STRING(OMX_StateWaitForResources); // cppcheck-suppress constArgument // cppcheck-suppress knownArgument - CASE_ASSERT("Unsupported OMX state", state); } + assert(0 && "Unsupported OMX state"); } -#undef CASE_ASSERT #undef CASE_TO_STRING diff --git a/src/ustreamer/encoders/omx/formatters.h b/src/ustreamer/encoders/omx/formatters.h index d6f4420..7913ff7 100644 --- a/src/ustreamer/encoders/omx/formatters.h +++ b/src/ustreamer/encoders/omx/formatters.h @@ -29,8 +29,8 @@ #include #include -#include "../../../common/tools.h" -#include "../../../common/logging.h" +#include "../../../libs/common/tools.h" +#include "../../../libs/common/logging.h" #define LOG_ERROR_OMX(_error, _msg, ...) { \ diff --git a/src/ustreamer/gpio/gpio.h b/src/ustreamer/gpio/gpio.h index d4a64fe..bc08983 100644 --- a/src/ustreamer/gpio/gpio.h +++ b/src/ustreamer/gpio/gpio.h @@ -30,9 +30,9 @@ #include #include -#include "../../common/tools.h" -#include "../../common/logging.h" -#include "../../common/threading.h" +#include "../../libs/common/tools.h" +#include "../../libs/common/logging.h" +#include "../../libs/common/threading.h" typedef struct { diff --git a/src/ustreamer/http/base64.h b/src/ustreamer/http/base64.h index 8d5febd..226fa53 100644 --- a/src/ustreamer/http/base64.h +++ b/src/ustreamer/http/base64.h @@ -26,7 +26,7 @@ #include #include -#include "../../common/tools.h" +#include "../../libs/common/tools.h" char *base64_encode(const uint8_t *str); diff --git a/src/ustreamer/http/mime.h b/src/ustreamer/http/mime.h index 5bfda6a..03d4eab 100644 --- a/src/ustreamer/http/mime.h +++ b/src/ustreamer/http/mime.h @@ -26,7 +26,7 @@ #include -#include "../../common/tools.h" +#include "../../libs/common/tools.h" const char *guess_mime_type(const char *str); diff --git a/src/ustreamer/http/path.h b/src/ustreamer/http/path.h index 679070e..7174a8b 100644 --- a/src/ustreamer/http/path.h +++ b/src/ustreamer/http/path.h @@ -28,7 +28,7 @@ #endif #include -#include "../../common/tools.h" +#include "../../libs/common/tools.h" char *simplify_request_path(const char *str); diff --git a/src/ustreamer/http/server.h b/src/ustreamer/http/server.h index d73864c..ad72a83 100644 --- a/src/ustreamer/http/server.h +++ b/src/ustreamer/http/server.h @@ -53,11 +53,11 @@ # error Required libevent-pthreads support #endif -#include "../../common/tools.h" -#include "../../common/threading.h" -#include "../../common/logging.h" -#include "../../common/process.h" -#include "../../common/frame.h" +#include "../../libs/common/tools.h" +#include "../../libs/common/threading.h" +#include "../../libs/common/logging.h" +#include "../../libs/common/process.h" +#include "../../libs/common/frame.h" #include "../data/index_html.h" #include "../encoder.h" #include "../stream.h" diff --git a/src/ustreamer/http/static.h b/src/ustreamer/http/static.h index 2300aec..3e6644f 100644 --- a/src/ustreamer/http/static.h +++ b/src/ustreamer/http/static.h @@ -29,8 +29,8 @@ #include -#include "../../common/tools.h" -#include "../../common/logging.h" +#include "../../libs/common/tools.h" +#include "../../libs/common/logging.h" #include "path.h" diff --git a/src/ustreamer/http/unix.h b/src/ustreamer/http/unix.h index b3d4b5f..4a9e3f6 100644 --- a/src/ustreamer/http/unix.h +++ b/src/ustreamer/http/unix.h @@ -35,8 +35,8 @@ #include #include -#include "../../common/tools.h" -#include "../../common/logging.h" +#include "../../libs/common/tools.h" +#include "../../libs/common/logging.h" evutil_socket_t evhttp_my_bind_unix(struct evhttp *http, const char *path, bool rm, mode_t mode); diff --git a/src/ustreamer/main.c b/src/ustreamer/main.c index 8ea82da..e63048e 100644 --- a/src/ustreamer/main.c +++ b/src/ustreamer/main.c @@ -36,9 +36,9 @@ #include -#include "../common/tools.h" -#include "../common/threading.h" -#include "../common/logging.h" +#include "../libs/common/tools.h" +#include "../libs/common/threading.h" +#include "../libs/common/logging.h" #include "options.h" #include "device.h" diff --git a/src/ustreamer/options.h b/src/ustreamer/options.h index 77ec9e0..578c103 100644 --- a/src/ustreamer/options.h +++ b/src/ustreamer/options.h @@ -33,12 +33,12 @@ #include #include -#include "../common/config.h" -#include "../common/logging.h" -#include "../common/process.h" -#include "../common/frame.h" +#include "../libs/common/config.h" +#include "../libs/common/logging.h" +#include "../libs/common/process.h" +#include "../libs/common/frame.h" #ifdef WITH_RAWSINK -# include "../rawsink/rawsink.h" +# include "../libs/rawsink/rawsink.h" #endif #include "device.h" diff --git a/src/ustreamer/stream.h b/src/ustreamer/stream.h index cb3ede1..44b8df3 100644 --- a/src/ustreamer/stream.h +++ b/src/ustreamer/stream.h @@ -32,16 +32,16 @@ #include #include -#include "../common/tools.h" -#include "../common/threading.h" -#include "../common/logging.h" -#include "../common/frame.h" +#include "../libs/common/tools.h" +#include "../libs/common/threading.h" +#include "../libs/common/logging.h" +#include "../libs/common/frame.h" #include "blank.h" #include "device.h" #include "encoder.h" #ifdef WITH_RAWSINK -# include "../rawsink/rawsink.h" +# include "../libs/rawsink/rawsink.h" #endif #ifdef WITH_GPIO # include "gpio/gpio.h" diff --git a/src/ustreamer/xioctl.h b/src/ustreamer/xioctl.h index 2fef8a9..ffa5ba2 100644 --- a/src/ustreamer/xioctl.h +++ b/src/ustreamer/xioctl.h @@ -26,8 +26,8 @@ #include -#include "../common/tools.h" -#include "../common/logging.h" +#include "../libs/common/tools.h" +#include "../libs/common/logging.h" #ifndef CFG_XIOCTL_RETRIES