Compare commits

...

54 Commits
v1.4 ... v1.14

Author SHA1 Message Date
Devaev Maxim
dcc90341c9 Bump version: 1.13 → 1.14 2020-05-03 06:32:59 +03:00
Devaev Maxim
7e102c88cd log static errors as verbose 2020-05-03 06:17:43 +03:00
Devaev Maxim
e131a3ba49 typo 2020-04-30 17:27:55 +03:00
Devaev Maxim
e393be4c63 Bump version: 1.12 → 1.13 2020-03-04 03:04:44 +03:00
Devaev Maxim
cf4f5f5b2a notify parent using SIGUSR2 2020-03-04 03:04:28 +03:00
Maxim Devaev
1190059359 Update README.ru.md 2020-02-25 16:06:25 +03:00
Maxim Devaev
910b27feb4 Update README.md 2020-02-25 16:05:58 +03:00
Devaev Maxim
60ca92d367 Bump version: 1.11 → 1.12 2020-02-19 16:46:38 +03:00
Devaev Maxim
4f44c5efa1 hint 2020-02-19 16:46:13 +03:00
Devaev Maxim
3504095871 refactoring 2020-02-19 08:56:50 +03:00
Devaev Maxim
6eeb49ef75 merged ctl options 2020-02-19 08:56:50 +03:00
Devaev Maxim
9353b3474a options to reset image settings to default 2020-02-19 08:56:50 +03:00
Maxim Devaev
dfc98a67f2 Update README.ru.md 2020-02-10 06:40:25 +03:00
Maxim Devaev
904c76fa93 Update README.md 2020-02-10 06:39:37 +03:00
Devaev Maxim
f0a7ca5c94 Issue #14: added message about fileserver 2020-01-30 03:53:06 +03:00
Devaev Maxim
bb4e9db7e7 fixed serving of empty static files 2020-01-29 20:20:53 +03:00
Devaev Maxim
61f2bfa00e fixed mime guessing 2020-01-29 20:13:31 +03:00
Devaev Maxim
49eb7f6e51 fixed clang 'linker' input unused 2020-01-19 17:23:41 +03:00
Devaev Maxim
b5375b835a Fixed #11: clang warning 2020-01-19 01:07:57 +03:00
Devaev Maxim
e3e2c5cead Bump version: 1.10 → 1.11 2020-01-14 16:49:50 +03:00
Devaev Maxim
ef4150877b check for 8 bits 2020-01-14 16:46:31 +03:00
Devaev Maxim
cc00d0fea3 Bump version: 1.9 → 1.10 2019-10-21 08:20:45 +03:00
Devaev Maxim
91a1e48a7c refactoring; renamed X-UStreamer-*-Time to X-UStreamer-*-Timestamp 2019-10-21 08:16:19 +03:00
Devaev Maxim
f381113d50 new arch pkg extension 2019-10-17 02:58:56 +03:00
Devaev Maxim
a6304959f4 Bump version: 1.8 → 1.9 2019-10-11 23:37:25 +03:00
Devaev Maxim
57bc6e160d keep original argv 2019-10-11 23:37:05 +03:00
Devaev Maxim
03dd5dfebb Bump version: 1.7 → 1.8 2019-10-11 08:23:40 +03:00
Devaev Maxim
00ef8928b3 temporary disabled setproctitle to avoid memory corruption 2019-10-11 08:23:04 +03:00
Devaev Maxim
2b338cea90 Bump version: 1.6 → 1.7 2019-10-11 06:42:20 +03:00
Devaev Maxim
e64a1a1688 improved readme 2019-10-11 06:42:06 +03:00
Devaev Maxim
2ad196b95e --features 2019-10-11 06:39:15 +03:00
Devaev Maxim
70c7bcc209 --process-name-prefix 2019-10-11 06:23:32 +03:00
Devaev Maxim
890b248563 ifdef 2019-10-10 23:11:15 +03:00
Devaev Maxim
be2de06b09 manual check if the parent process is alive after prctl() 2019-10-10 23:09:24 +03:00
Devaev Maxim
6175e6974c refactoring 2019-10-10 10:44:12 +03:00
Devaev Maxim
6bc26b734e Bump version: 1.5 → 1.6 2019-10-10 02:58:59 +03:00
Devaev Maxim
da6b5fd786 fixed spell 2019-10-10 02:54:49 +03:00
Devaev Maxim
e97b512f79 refactoring 2019-10-10 02:18:37 +03:00
Devaev Maxim
a1c83fc765 --exit-on-parent-death 2019-10-08 23:32:23 +03:00
Devaev Maxim
57132c46ac Bump version: 1.4 → 1.5 2019-09-18 04:55:27 +03:00
Devaev Maxim
6a7a26360f messages fix 2019-09-18 04:51:39 +03:00
Devaev Maxim
7a699833c4 objects in separate directory 2019-09-18 03:43:37 +03:00
Devaev Maxim
fcdb8f5d42 headers sanity 2019-09-17 19:45:56 +03:00
Devaev Maxim
e827ad8a7a refactoring 2019-09-17 17:16:59 +03:00
Devaev Maxim
caf5b9191b refactoring 2019-09-17 10:53:36 +03:00
Devaev Maxim
163aa8b5af refactoring 2019-09-17 10:28:46 +03:00
Devaev Maxim
bc873e31d0 refactoring 2019-09-17 10:21:33 +03:00
Devaev Maxim
947924e900 refactoring 2019-09-17 09:57:54 +03:00
Devaev Maxim
0827cb2b65 Issue #9: --last-as-blank accept timeout or 0 2019-09-17 09:50:14 +03:00
Devaev Maxim
d24d48212f refactoring 2019-09-17 03:19:44 +03:00
Devaev Maxim
a23475be57 thread names 2019-09-14 04:28:11 +03:00
Devaev Maxim
b7d8c5bfa6 Issue #9: Option to show last frame as blank 2019-09-13 03:22:17 +03:00
Devaev Maxim
433e884fad unified picture_t with api 2019-09-13 02:18:24 +03:00
Devaev Maxim
1d1c7c705d refactoring 2019-09-12 18:23:45 +03:00
40 changed files with 1232 additions and 545 deletions

View File

@@ -1,7 +1,7 @@
[bumpversion]
commit = True
tag = True
current_version = 1.4
current_version = 1.14
parse = (?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\d+)(\-(?P<release>[a-z]+))?)?
serialize =
{major}.{minor}

3
.gitignore vendored
View File

@@ -2,8 +2,9 @@
/pkg/arch/src/
/pkg/arch/v*.tar.gz
/pkg/arch/ustreamer-*.pkg.tar.xz
/pkg/arch/ustreamer-*.pkg.tar.zst
/build/
/config.mk
/vgcore.*
/ustreamer
/*.sock
*.o

View File

@@ -11,11 +11,13 @@ LDFLAGS ?=
RPI_VC_HEADERS ?= /opt/vc/include
RPI_VC_LIBS ?= /opt/vc/lib
BUILD ?= build
# =====
LIBS = -lm -ljpeg -pthread -levent -levent_pthreads -luuid
_LIBS = -lm -ljpeg -pthread -levent -levent_pthreads -luuid
override CFLAGS += -c -std=c11 -Wall -Wextra -D_GNU_SOURCE
SOURCES = $(shell ls src/*.c src/http/*.c src/encoders/cpu/*.c src/encoders/hw/*.c)
_SRCS = $(shell ls src/*.c src/http/*.c src/encoders/cpu/*.c src/encoders/hw/*.c)
define optbool
@@ -24,20 +26,35 @@ endef
ifneq ($(call optbool,$(WITH_OMX)),)
LIBS += -lbcm_host -lvcos -lopenmaxil -L$(RPI_VC_LIBS)
_LIBS += -lbcm_host -lvcos -lopenmaxil -L$(RPI_VC_LIBS)
override CFLAGS += -DWITH_OMX -DOMX_SKIP64BIT -I$(RPI_VC_HEADERS)
SOURCES += $(shell ls src/encoders/omx/*.c)
_SRCS += $(shell ls src/encoders/omx/*.c)
endif
ifneq ($(call optbool,$(WITH_GPIO)),)
LIBS += -lwiringPi
_LIBS += -lwiringPi
override CFLAGS += -DWITH_GPIO
endif
WITH_PTHREAD_NP ?= 1
ifneq ($(call optbool,$(WITH_PTHREAD_NP)),)
override CFLAGS += -DWITH_PTHREAD_NP
endif
WITH_SETPROCTITLE ?= 1
ifneq ($(call optbool,$(WITH_SETPROCTITLE)),)
ifeq ($(shell uname -s | tr A-Z a-z),linux)
_LIBS += -lbsd
endif
override CFLAGS += -DWITH_SETPROCTITLE
endif
# =====
all: $(SOURCES) $(PROG)
all: $(PROG)
install: $(PROG)
@@ -57,19 +74,20 @@ regen:
tools/make-html-h.py src/http/data/index.html src/http/data/index_html.h INDEX
$(PROG): $(SOURCES:.c=.o)
$(PROG): $(_SRCS:%.c=$(BUILD)/%.o)
$(info -- LD $@)
@ $(CC) $(SOURCES:.c=.o) -o $@ $(LDFLAGS) $(LIBS)
@ $(CC) $^ -o $@ $(LDFLAGS) $(_LIBS)
$(info ===== Build complete =====)
$(info == CC = $(CC))
$(info == LIBS = $(LIBS))
$(info == LIBS = $(_LIBS))
$(info == CFLAGS = $(CFLAGS))
$(info == LDFLAGS = $(LDFLAGS))
.c.o:
$(BUILD)/%.o: %.c
$(info -- CC $<)
@ $(CC) $< -o $@ $(CFLAGS) $(LIBS)
@ mkdir -p $(dir $@) || true
@ $(CC) $< -o $@ $(CFLAGS)
release:
@@ -91,6 +109,5 @@ push:
clean-all: clean
clean:
find src -name '*.o' -exec rm '{}' \;
rm -rf pkg/arch/pkg pkg/arch/src pkg/arch/v*.tar.gz pkg/arch/ustreamer-*.pkg.tar.xz
rm -f vgcore.* *.sock $(PROG)
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

View File

@@ -12,7 +12,7 @@
| [OpenMAX IL](https://www.khronos.org/openmaxil) hardware acceleration<br>on Raspberry Pi | ![#00aa00](https://placehold.it/15/00aa00/000000?text=+) Yes | ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) No |
| Behavior when the device<br>is disconnected while streaming | ![#00aa00](https://placehold.it/15/00aa00/000000?text=+) Shows a black screen<br>with ```NO SIGNAL``` on it<br>until reconnected | ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) Stops the broadcast <sup>1</sup> |
| [DV-timings](https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/dv-timings.html) support -<br>the ability to change resolution<br>on the fly by source signal | ![#00aa00](https://placehold.it/15/00aa00/000000?text=+) Yes | ![#ffaa00](https://placehold.it/15/ffaa00/000000?text=+) Partially yes <sup>1</sup> |
| Option to skip frames when streaming<br>static images by HTTP to save traffic | ![#00aa00](https://placehold.it/15/00aa00/000000?text=+) Yes <sup>2</sup> | ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) No |
| Option to skip frames when streaming<br>static images by HTTP to save the traffic | ![#00aa00](https://placehold.it/15/00aa00/000000?text=+) Yes <sup>2</sup> | ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) No |
| Streaming via UNIX domain socket | ![#00aa00](https://placehold.it/15/00aa00/000000?text=+) Yes | ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) No |
| Debug logs without recompiling,<br>performance statistics log,<br>access to HTTP broadcast parameters | ![#00aa00](https://placehold.it/15/00aa00/000000?text=+) Yes | ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) No |
| Option to serve files<br>with a built-in HTTP server | ![#00aa00](https://placehold.it/15/00aa00/000000?text=+) Yes | ![#ffaa00](https://placehold.it/15/ffaa00/000000?text=+) Regular files only |
@@ -30,9 +30,13 @@ If you're going to live-stream from your backyard webcam and need to control it,
-----
# Building
You'll need ```make```, ```gcc```, ```libevent``` with ```pthreads``` support, ```libjpeg8```/```libjpeg-turbo``` and ```libuuid```.
You'll need ```make```, ```gcc```, ```libevent``` with ```pthreads``` support, ```libjpeg8```/```libjpeg-turbo```, ```libuuid``` and ```libbsd``` (only for Linux).
On Raspberry Pi you can build the program with OpenMAX IL. To do this pass option ```WITH_OMX=1``` to ```make```. To enable GPIO support install [wiringPi](http://wiringpi.com) and pass option ```WITH_GPIO=1```.
* Arch: `sudo pacman -S libevent libjpeg-turbo libutil-linux libbsd`.
* Raspbian: `sudo apt install libevent-dev libevent-pthreads-2.1-6-dev libjpeg8-dev uuid-dev libbsd-dev`.
* Debian: `sudo apt install build-essential libevent-dev libjpeg62-turbo-dev uuid-dev libbsd-dev`.
On Raspberry Pi you can build the program with OpenMAX IL. To do this pass option ```WITH_OMX=1``` to ```make```. To enable GPIO support install [wiringPi](http://wiringpi.com) and pass option ```WITH_GPIO=1```. If the compiler reports about a missing function ```pthread_get_name_np()``` (or similar), add option ```WITH_PTHREAD_NP=0``` (it's enabled by default). For the similar error with ```setproctitle()``` add option ```WITH_SETPROCTITLE=0```.
```
$ git clone --depth=1 https://github.com/pikvm/ustreamer
@@ -59,11 +63,15 @@ $ ./ustreamer \
--workers=3 \ # Maximum workers for OpenMAX
--persistent \ # Don't re-initialize device on timeout (for example when HDMI cable was disconnected)
--dv-timings \ # Use DV-timings
--drop-same-frames=30 # Save that traffic
--drop-same-frames=30 # Save the traffic
```
You can always view the full list of options with ```ustreamer --help```.
-----
# Tips
* [Running uStreamer via systemd service](https://github.com/pikvm/ustreamer/issues/16)
-----
# License
Copyright (C) 2018 by Maxim Devaev mdevaev@gmail.com

View File

@@ -30,9 +30,13 @@
-----
# Сборка
Для сборки вам понадобятся ```make```, ```gcc```, ```libevent``` с поддержкой ```pthreads```, ```libjpeg8```/```libjpeg-turbo``` и ```libuuid```.
Для сборки вам понадобятся ```make```, ```gcc```, ```libevent``` с поддержкой ```pthreads```, ```libjpeg8```/```libjpeg-turbo```, ```libuuid``` и ```libbsd``` (только для Linux).
На Raspberry Pi программу можно собрать с поддержкой OpenMAX IL. Для этого передайте ```make``` параметр ```WITH_OMX=1```. Для включения сборки с поддержкой GPIO установите [wiringPi](http://wiringpi.com) и добавьте параметр ```WITH_GPIO=1```.
* Arch: `sudo pacman -S libevent libjpeg-turbo libutil-linux libbsd`.
* Raspbian: `sudo apt install libevent-dev libevent-pthreads-2.1-6-dev libjpeg8-dev uuid-dev libbsd-dev`.
* Debian: `sudo apt install build-essential libevent-dev libjpeg62-turbo-dev uuid-dev libbsd-dev`.
На Raspberry Pi программу можно собрать с поддержкой OpenMAX IL. Для этого передайте ```make``` параметр ```WITH_OMX=1```. Для включения сборки с поддержкой GPIO установите [wiringPi](http://wiringpi.com) и добавьте параметр ```WITH_GPIO=1```. Если при сборке компилятор ругается на отсутствие функции ```pthread_get_name_np()``` или другой подобной, добавьте параметр ```WITH_PTHREAD_NP=0``` (по умолчанию он включен). При аналогичной ошибке с функцией ```setproctitle()``` добавьте параметр ```WITH_SETPROCTITLE=0```.
```
$ git clone --depth=1 https://github.com/pikvm/ustreamer
@@ -64,6 +68,10 @@ $ ./ustreamer \
За полным списком опций обращайтесь ко встроенной справке: ```ustreamer --help```.
-----
# Советы
* [Запуск с помощью systemd-сервиса](https://github.com/pikvm/ustreamer/issues/16)
-----
# Лицензия
Copyright (C) 2018 by Maxim Devaev mdevaev@gmail.com

View File

@@ -3,13 +3,13 @@
pkgname=ustreamer
pkgver=1.4
pkgver=1.14
pkgrel=1
pkgdesc="Lightweight and fast MJPG-HTTP streamer"
url="https://github.com/pikvm/ustreamer"
license=(GPL)
arch=(i686 x86_64 armv6h armv7h)
depends=(libjpeg libevent libutil-linux)
depends=(libjpeg libevent libutil-linux libbsd)
# optional: raspberrypi-firmware for OMX encoder
# optional: wiringpi for GPIO support
makedepends=(gcc make)

View File

@@ -18,6 +18,7 @@ DEPEND="
>=dev-libs/libevent-2.1.8
>=media-libs/libjpeg-turbo-1.5.3
>=sys-apps/util-linux-2.33
>=dev-libs/libbsd-0.9.1
"
RDEPEND="${DEPEND}"
BDEPEND=""

View File

@@ -6,7 +6,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=ustreamer
PKG_VERSION:=1.4
PKG_VERSION:=1.14
PKG_RELEASE:=1
PKG_MAINTAINER:=Maxim Devaev <mdevaev@gmail.com>
@@ -25,7 +25,7 @@ define Package/ustreamer
SECTION:=multimedia
CATEGORY:=Multimedia
TITLE:=uStreamer
DEPENDS:=+libpthread +libjpeg +libv4l +libuuid +libevent2 +libevent2-core +libevent2-extra +libevent2-pthreads
DEPENDS:=+libpthread +libjpeg +libv4l +libuuid +libbsd +libevent2 +libevent2-core +libevent2-extra +libevent2-pthreads
URL:=https://github.com/pikvm/ustreamer
endef

View File

@@ -23,5 +23,5 @@
#pragma once
#ifndef VERSION
# define VERSION "1.4"
# define VERSION "1.14"
#endif

View File

@@ -34,11 +34,15 @@
#include <sys/select.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <linux/videodev2.h>
#include <linux/v4l2-controls.h>
#include "tools.h"
#include "logging.h"
#include "xioctl.h"
#include "picture.h"
static const struct {
@@ -73,9 +77,10 @@ static int _device_open_mmap(struct device_t *dev);
static int _device_open_queue_buffers(struct device_t *dev);
static void _device_open_alloc_picbufs(struct device_t *dev);
static int _device_apply_resolution(struct device_t *dev, unsigned width, unsigned height);
static void _device_apply_controls(struct device_t *dev);
static int _device_check_control(struct device_t *dev, const char *name, unsigned cid, int value, bool quiet);
static void _device_set_control(struct device_t *dev, const char *name, unsigned cid, int value, bool quiet);
static int _device_query_control(struct device_t *dev, struct v4l2_queryctrl *query, const char *name, unsigned cid, bool quiet);
static void _device_set_control(struct device_t *dev, struct v4l2_queryctrl *query, const char *name, unsigned cid, int value, bool quiet);
static const char *_format_to_string_fourcc(char *buf, size_t size, unsigned format);
static const char *_format_to_string_nullable(unsigned format);
@@ -174,9 +179,8 @@ void device_close(struct device_t *dev) {
if (dev->run->pictures) {
LOG_DEBUG("Releasing picture buffers ...");
for (unsigned index = 0; index < dev->run->n_buffers && dev->run->pictures[index].data; ++index) {
free(dev->run->pictures[index].data);
dev->run->pictures[index].data = NULL;
for (unsigned index = 0; index < dev->run->n_buffers; ++index) {
picture_destroy(dev->run->pictures[index]);
}
free(dev->run->pictures);
dev->run->pictures = NULL;
@@ -283,7 +287,7 @@ int device_grab_buffer(struct device_t *dev) {
dev->run->hw_buffers[buf_info.index].used = buf_info.bytesused;
memcpy(&dev->run->hw_buffers[buf_info.index].buf_info, &buf_info, sizeof(struct v4l2_buffer));
dev->run->pictures[buf_info.index].grab_time = get_now_monotonic();
dev->run->pictures[buf_info.index]->grab_ts = get_now_monotonic();
return buf_info.index;
}
@@ -387,7 +391,7 @@ static int _device_apply_dv_timings(struct device_t *dev) {
LOG_DEBUG("Calling ioctl(VIDIOC_QUERY_DV_TIMINGS) ...");
if (xioctl(dev->run->fd, VIDIOC_QUERY_DV_TIMINGS, &dv) == 0) {
LOG_INFO("Got new DV timings: resolution=%ux%u, pixclk=%llu",
dv.bt.width, dv.bt.height, dv.bt.pixelclock);
dv.bt.width, dv.bt.height, (unsigned long long)dv.bt.pixelclock); // Issue #11
LOG_DEBUG("Calling ioctl(VIDIOC_S_DV_TIMINGS) ...");
if (xioctl(dev->run->fd, VIDIOC_S_DV_TIMINGS, &dv) < 0) {
@@ -578,14 +582,15 @@ static int _device_open_queue_buffers(struct device_t *dev) {
}
static void _device_open_alloc_picbufs(struct device_t *dev) {
size_t picture_size = picture_get_generous_size(dev->run->width, dev->run->height);
LOG_DEBUG("Allocating picture buffers ...");
A_CALLOC(dev->run->pictures, dev->run->n_buffers);
dev->run->max_raw_image_size = ((dev->run->width * dev->run->height) << 1) * 2;
for (unsigned index = 0; index < dev->run->n_buffers; ++index) {
LOG_DEBUG("Allocating picture buffer %u sized %zu bytes... ", index, dev->run->max_raw_image_size);
A_CALLOC(dev->run->pictures[index].data, dev->run->max_raw_image_size);
dev->run->pictures[index].allocated = dev->run->max_raw_image_size;
dev->run->pictures[index] = picture_init();
LOG_DEBUG("Pre-allocating picture buffer %u sized %zu bytes... ", index, picture_size);
picture_realloc_data(dev->run->pictures[index], picture_size);
}
}
@@ -606,65 +611,81 @@ static int _device_apply_resolution(struct device_t *dev, unsigned width, unsign
}
static void _device_apply_controls(struct device_t *dev) {
# define SET_CID(_cid, _dest, _value, _quiet) { \
if (_device_check_control(dev, #_dest, _cid, _value, _quiet) == 0) { \
_device_set_control(dev, #_dest, _cid, _value, _quiet); \
# define SET_CID_VALUE(_cid, _field, _value, _quiet) { \
struct v4l2_queryctrl query; \
if (_device_query_control(dev, &query, #_field, _cid, _quiet) == 0) { \
_device_set_control(dev, &query, #_field, _cid, _value, _quiet); \
} \
}
# define SET_CID_MANUAL(_cid, _dest) { \
if (dev->ctl._dest.value_set) { \
SET_CID(_cid, _dest, dev->ctl._dest.value, false); \
# define SET_CID_DEFAULT(_cid, _field, _quiet) { \
struct v4l2_queryctrl query; \
if (_device_query_control(dev, &query, #_field, _cid, _quiet) == 0) { \
_device_set_control(dev, &query, #_field, _cid, query.default_value, _quiet); \
} \
}
# define SET_CID_AUTO(_cid_auto, _cid_manual, _dest) { \
if (dev->ctl._dest.value_set || dev->ctl._dest.auto_set) { \
SET_CID(_cid_auto, _dest##_auto, dev->ctl._dest.auto_set, dev->ctl._dest.value_set); \
SET_CID_MANUAL(_cid_manual, _dest); \
# define CONTROL_MANUAL_CID(_cid, _field) { \
if (dev->ctl._field.mode == CTL_MODE_VALUE) { \
SET_CID_VALUE(_cid, _field, dev->ctl._field.value, false); \
} else if (dev->ctl._field.mode == CTL_MODE_DEFAULT) { \
SET_CID_DEFAULT(_cid, _field, false); \
} \
}
SET_CID_AUTO (V4L2_CID_AUTOBRIGHTNESS, V4L2_CID_BRIGHTNESS, brightness);
SET_CID_MANUAL ( V4L2_CID_CONTRAST, contrast);
SET_CID_MANUAL ( V4L2_CID_SATURATION, saturation);
SET_CID_AUTO (V4L2_CID_HUE_AUTO, V4L2_CID_HUE, hue);
SET_CID_MANUAL ( V4L2_CID_GAMMA, gamma);
SET_CID_MANUAL ( V4L2_CID_SHARPNESS, sharpness);
SET_CID_MANUAL ( V4L2_CID_BACKLIGHT_COMPENSATION, backlight_compensation);
SET_CID_AUTO (V4L2_CID_AUTO_WHITE_BALANCE, V4L2_CID_WHITE_BALANCE_TEMPERATURE, white_balance);
SET_CID_AUTO (V4L2_CID_AUTOGAIN, V4L2_CID_GAIN, gain);
# define CONTROL_AUTO_CID(_cid_auto, _cid_manual, _field) { \
if (dev->ctl._field.mode == CTL_MODE_VALUE) { \
SET_CID_VALUE(_cid_auto, _field##_auto, 0, true); \
SET_CID_VALUE(_cid_manual, _field, dev->ctl._field.value, false); \
} else if (dev->ctl._field.mode == CTL_MODE_AUTO) { \
SET_CID_VALUE(_cid_auto, _field##_auto, 1, false); \
} else if (dev->ctl._field.mode == CTL_MODE_DEFAULT) { \
SET_CID_VALUE(_cid_auto, _field##_auto, 0, true); /* Reset inactive flag */ \
SET_CID_DEFAULT(_cid_manual, _field, false); \
SET_CID_DEFAULT(_cid_auto, _field##_auto, false); \
} \
}
# undef SET_CID_AUTO
# undef SET_CID_MANUAL
# undef SET_CID
CONTROL_AUTO_CID (V4L2_CID_AUTOBRIGHTNESS, V4L2_CID_BRIGHTNESS, brightness);
CONTROL_MANUAL_CID ( V4L2_CID_CONTRAST, contrast);
CONTROL_MANUAL_CID ( V4L2_CID_SATURATION, saturation);
CONTROL_AUTO_CID (V4L2_CID_HUE_AUTO, V4L2_CID_HUE, hue);
CONTROL_MANUAL_CID ( V4L2_CID_GAMMA, gamma);
CONTROL_MANUAL_CID ( V4L2_CID_SHARPNESS, sharpness);
CONTROL_MANUAL_CID ( V4L2_CID_BACKLIGHT_COMPENSATION, backlight_compensation);
CONTROL_AUTO_CID (V4L2_CID_AUTO_WHITE_BALANCE, V4L2_CID_WHITE_BALANCE_TEMPERATURE, white_balance);
CONTROL_AUTO_CID (V4L2_CID_AUTOGAIN, V4L2_CID_GAIN, gain);
# undef CONTROL_AUTO_CID
# undef CONTROL_MANUAL_CID
# undef SET_CID_DEFAULT
# undef SET_CID_VALUE
}
static int _device_check_control(struct device_t *dev, const char *name, unsigned cid, int value, bool quiet) {
struct v4l2_queryctrl query;
static int _device_query_control(struct device_t *dev, struct v4l2_queryctrl *query, const char *name, unsigned cid, bool quiet) {
MEMSET_ZERO(*query);
query->id = cid;
MEMSET_ZERO(query);
query.id = cid;
if (xioctl(dev->run->fd, VIDIOC_QUERYCTRL, &query) < 0 || query.flags & V4L2_CTRL_FLAG_DISABLED) {
if (xioctl(dev->run->fd, VIDIOC_QUERYCTRL, query) < 0 || query->flags & V4L2_CTRL_FLAG_DISABLED) {
if (!quiet) {
LOG_ERROR("Changing control %s is unsupported", name);
}
return -1;
}
if (value < query.minimum || value > query.maximum || value % query.step != 0) {
if (!quiet) {
LOG_ERROR("Invalid value %d of control %s: min=%d, max=%d, default=%d, step=%u",
value, name, query.minimum, query.maximum, query.default_value, query.step);
}
return -2;
}
return 0;
}
static void _device_set_control(struct device_t *dev, const char *name, unsigned cid, int value, bool quiet) {
static void _device_set_control(struct device_t *dev, struct v4l2_queryctrl *query, const char *name, unsigned cid, int value, bool quiet) {
struct v4l2_control ctl;
if (value < query->minimum || value > query->maximum || value % query->step != 0) {
if (!quiet) {
LOG_ERROR("Invalid value %d of control %s: min=%d, max=%d, default=%d, step=%u",
value, name, query->minimum, query->maximum, query->default_value, query->step);
}
return;
}
MEMSET_ZERO(ctl);
ctl.id = cid;
ctl.value = value;
@@ -674,7 +695,7 @@ static void _device_set_control(struct device_t *dev, const char *name, unsigned
LOG_PERROR("Can't set control %s", name);
}
} else if (!quiet) {
LOG_INFO("Using control %s: %d", name, ctl.value);
LOG_INFO("Applying control %s: %d", name, ctl.value);
}
}

View File

@@ -27,6 +27,8 @@
#include <linux/videodev2.h>
#include "picture.h"
#define VIDEO_MIN_WIDTH 160
#define VIDEO_MAX_WIDTH 10240
@@ -50,15 +52,6 @@ struct hw_buffer_t {
struct v4l2_buffer buf_info;
};
struct picture_t {
unsigned char *data;
size_t used;
size_t allocated;
long double grab_time;
long double encode_begin_time;
long double encode_end_time;
};
struct device_runtime_t {
int fd;
unsigned width;
@@ -67,15 +60,20 @@ struct device_runtime_t {
unsigned n_buffers;
unsigned n_workers;
struct hw_buffer_t *hw_buffers;
struct picture_t *pictures;
size_t max_raw_image_size;
struct picture_t **pictures;
bool capturing;
};
enum control_mode_t {
CTL_MODE_NONE = 0,
CTL_MODE_VALUE,
CTL_MODE_AUTO,
CTL_MODE_DEFAULT,
};
struct control_t {
int value;
bool value_set;
bool auto_set;
enum control_mode_t mode;
int value;
};
struct controls_t {

View File

@@ -22,6 +22,7 @@
#include "encoder.h"
#include <stdlib.h>
#include <stdbool.h>
#include <strings.h>
#include <assert.h>
@@ -29,12 +30,12 @@
#include <linux/videodev2.h>
#include "tools.h"
#include "threading.h"
#include "logging.h"
#include "device.h"
#include "encoders/cpu/encoder.h"
#include "encoders/hw/encoder.h"
#ifdef WITH_OMX
# include "encoders/omx/encoder.h"
#endif
@@ -200,7 +201,7 @@ int encoder_compress_buffer(struct encoder_t *encoder, struct device_t *dev, uns
assert(encoder->run->type != ENCODER_TYPE_UNKNOWN);
dev->run->pictures[buf_index].encode_begin_time = get_now_monotonic();
dev->run->pictures[buf_index]->encode_begin_ts = get_now_monotonic();
if (encoder->run->type == ENCODER_TYPE_CPU) {
cpu_encoder_compress_buffer(dev, buf_index, encoder->run->quality);
@@ -215,7 +216,10 @@ int encoder_compress_buffer(struct encoder_t *encoder, struct device_t *dev, uns
}
# endif
dev->run->pictures[buf_index].encode_end_time = get_now_monotonic();
dev->run->pictures[buf_index]->encode_end_ts = get_now_monotonic();
dev->run->pictures[buf_index]->width = dev->run->width;
dev->run->pictures[buf_index]->height = dev->run->height;
return 0;

View File

@@ -24,9 +24,8 @@
#include <stdbool.h>
#include "pthread.h"
#include <pthread.h>
#include "tools.h"
#include "device.h"
#ifdef WITH_OMX

View File

@@ -29,13 +29,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <jpeglib.h>
#include <linux/videodev2.h>
#include "../../tools.h"
#include "../../picture.h"
#include "../../device.h"
@@ -43,7 +44,6 @@ struct _jpeg_dest_manager_t {
struct jpeg_destination_mgr mgr; // Default manager
JOCTET *buffer; // Start of buffer
struct picture_t *picture;
unsigned char *picture_data_cursor;
};
@@ -79,7 +79,7 @@ void cpu_encoder_compress_buffer(struct device_t *dev, unsigned index, unsigned
jpeg.err = jpeg_std_error(&jpeg_error);
jpeg_create_compress(&jpeg);
_jpeg_set_picture(&jpeg, &dev->run->pictures[index]);
_jpeg_set_picture(&jpeg, dev->run->pictures[index]);
jpeg.image_width = dev->run->width;
jpeg.image_height = dev->run->height;
@@ -108,7 +108,7 @@ void cpu_encoder_compress_buffer(struct device_t *dev, unsigned index, unsigned
jpeg_finish_compress(&jpeg);
jpeg_destroy_compress(&jpeg);
assert(dev->run->pictures[index].used > 0);
assert(dev->run->pictures[index]->used > 0);
}
static void _jpeg_set_picture(j_compress_ptr jpeg, struct picture_t *picture) {
@@ -125,7 +125,6 @@ static void _jpeg_set_picture(j_compress_ptr jpeg, struct picture_t *picture) {
dest->mgr.empty_output_buffer = _jpeg_empty_output_buffer;
dest->mgr.term_destination = _jpeg_term_destination;
dest->picture = picture;
dest->picture_data_cursor = picture->data;
picture->used = 0;
}
@@ -277,13 +276,8 @@ static boolean _jpeg_empty_output_buffer(j_compress_ptr jpeg) {
// Called whenever local jpeg buffer fills up
struct _jpeg_dest_manager_t *dest = (struct _jpeg_dest_manager_t *)jpeg->dest;
size_t new_used = dest->picture->used + JPEG_OUTPUT_BUFFER_SIZE;
assert(new_used <= dest->picture->allocated);
memcpy(dest->picture_data_cursor, dest->buffer, JPEG_OUTPUT_BUFFER_SIZE);
dest->picture_data_cursor += JPEG_OUTPUT_BUFFER_SIZE;
dest->picture->used = new_used;
picture_append_data(dest->picture, dest->buffer, JPEG_OUTPUT_BUFFER_SIZE);
dest->mgr.next_output_byte = dest->buffer;
dest->mgr.free_in_buffer = JPEG_OUTPUT_BUFFER_SIZE;
@@ -293,18 +287,13 @@ static boolean _jpeg_empty_output_buffer(j_compress_ptr jpeg) {
static void _jpeg_term_destination(j_compress_ptr jpeg) {
// Called by jpeg_finish_compress after all data has been written.
// Usually needs to flush buffer
// Usually needs to flush buffer.
struct _jpeg_dest_manager_t *dest = (struct _jpeg_dest_manager_t *)jpeg->dest;
size_t final = JPEG_OUTPUT_BUFFER_SIZE - dest->mgr.free_in_buffer;
size_t new_used = dest->picture->used + final;
assert(new_used <= dest->picture->allocated);
// Write any data remaining in the buffer
memcpy(dest->picture_data_cursor, dest->buffer, final);
dest->picture_data_cursor += final;
dest->picture->used = new_used;
// Write any data remaining in the buffer.
picture_append_data(dest->picture, dest->buffer, final);
}
#undef JPEG_OUTPUT_BUFFER_SIZE

View File

@@ -36,13 +36,14 @@
#include "../../tools.h"
#include "../../logging.h"
#include "../../xioctl.h"
#include "../../picture.h"
#include "../../device.h"
#include "huffman.h"
void _copy_plus_huffman(const struct hw_buffer_t *src, struct picture_t *dest);
static bool _is_huffman(const unsigned char *data);
static size_t _memcpy_with_huffman(unsigned char *dest, const unsigned char *src, size_t size);
int hw_encoder_prepare(struct device_t *dev, unsigned quality) {
@@ -66,15 +67,30 @@ void hw_encoder_compress_buffer(struct device_t *dev, unsigned index) {
if (dev->run->format != V4L2_PIX_FMT_MJPEG && dev->run->format != V4L2_PIX_FMT_JPEG) {
assert(0 && "Unsupported input format for HW encoder");
}
_copy_plus_huffman(&dev->run->hw_buffers[index], dev->run->pictures[index]);
}
# define PICTURE(_next) dev->run->pictures[index]._next
# define HW_BUFFER(_next) dev->run->hw_buffers[index]._next
void _copy_plus_huffman(const struct hw_buffer_t *src, struct picture_t *dest) {
if (!_is_huffman(src->data)) {
const unsigned char *src_ptr = src->data;
const unsigned char *src_end = src->data + src->used;
size_t paste;
assert(PICTURE(allocated) >= HW_BUFFER(used) + sizeof(HUFFMAN_TABLE));
PICTURE(used) = _memcpy_with_huffman(PICTURE(data), HW_BUFFER(data), HW_BUFFER(used));
while ((((src_ptr[0] << 8) | src_ptr[1]) != 0xFFC0) && (src_ptr < src_end)) {
src_ptr += 1;
}
if (src_ptr >= src_end) {
dest->used = 0; // Error
return;
}
paste = src_ptr - src->data;
# undef HW_BUFFER
# undef PICTURE
picture_set_data(dest, src->data, paste);
picture_append_data(dest, HUFFMAN_TABLE, sizeof(HUFFMAN_TABLE));
picture_append_data(dest, src_ptr, src->used - paste);
} else {
picture_set_data(dest, src->data, src->used);
}
}
static bool _is_huffman(const unsigned char *data) {
@@ -91,27 +107,3 @@ static bool _is_huffman(const unsigned char *data) {
}
return false;
}
static size_t _memcpy_with_huffman(unsigned char *dest, const unsigned char *src, size_t size) {
if (!_is_huffman(src)) {
const unsigned char *src_ptr = src;
const unsigned char *src_end = src + size;
size_t paste;
while ((((src_ptr[0] << 8) | src_ptr[1]) != 0xFFC0) && (src_ptr < src_end)) {
src_ptr += 1;
}
if (src_ptr >= src_end) {
return 0;
}
paste = src_ptr - src;
memcpy(dest, src, paste);
memcpy(dest + paste, HUFFMAN_TABLE, sizeof(HUFFMAN_TABLE));
memcpy(dest + paste + sizeof(HUFFMAN_TABLE), src_ptr, size - paste);
return (size + sizeof(HUFFMAN_TABLE));
} else {
memcpy(dest, src, size);
return size;
}
}

View File

@@ -37,6 +37,7 @@
#include "../../logging.h"
#include "../../tools.h"
#include "../../picture.h"
#include "../../device.h"
#include "formatters.h"
@@ -174,7 +175,6 @@ int omx_encoder_prepare(struct omx_encoder_t *omx, struct device_t *dev, unsigne
}
int omx_encoder_compress_buffer(struct omx_encoder_t *omx, struct device_t *dev, unsigned index) {
# define PICTURE(_next) dev->run->pictures[index]._next
# define HW_BUFFER(_next) dev->run->hw_buffers[index]._next
# define IN(_next) omx->input_buffer->_next
# define OUT(_next) omx->output_buffer->_next
@@ -188,7 +188,7 @@ int omx_encoder_compress_buffer(struct omx_encoder_t *omx, struct device_t *dev,
return -1;
}
PICTURE(used) = 0;
dev->run->pictures[index]->used = 0;
omx->output_available = false;
omx->input_required = true;
@@ -200,9 +200,7 @@ int omx_encoder_compress_buffer(struct omx_encoder_t *omx, struct device_t *dev,
if (omx->output_available) {
omx->output_available = false;
assert(PICTURE(used) + OUT(nFilledLen) <= PICTURE(allocated));
memcpy(PICTURE(data) + PICTURE(used), OUT(pBuffer) + OUT(nOffset), OUT(nFilledLen));
PICTURE(used) += OUT(nFilledLen);
picture_append_data(dev->run->pictures[index], OUT(pBuffer) + OUT(nOffset), OUT(nFilledLen));
if (OUT(nFlags) & OMX_BUFFERFLAG_ENDOFFRAME) {
OUT(nFlags) = 0;
@@ -244,7 +242,6 @@ int omx_encoder_compress_buffer(struct omx_encoder_t *omx, struct device_t *dev,
# undef OUT
# undef IN
# undef HW_BUFFER
# undef PICTURE
return 0;
}
@@ -313,7 +310,7 @@ static int _omx_setup_input(struct omx_encoder_t *omx, struct device_t *dev) {
# undef ALIGN_HEIGHT
portdef.format.image.bFlagErrorConcealment = OMX_FALSE;
portdef.format.image.eCompressionFormat = OMX_IMAGE_CodingUnused;
portdef.nBufferSize = dev->run->max_raw_image_size;
portdef.nBufferSize = picture_get_generous_size(dev->run->width, dev->run->height);
# define MAP_FORMAT(_v4l2_format, _omx_format) \
case _v4l2_format: { portdef.format.image.eColorFormat = _omx_format; break; }

View File

@@ -24,12 +24,11 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "../tools.h"
static const char ENCODING_TABLE[] = {
static const char _ENCODING_TABLE[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
@@ -40,7 +39,7 @@ static const char ENCODING_TABLE[] = {
'4', '5', '6', '7', '8', '9', '+', '/',
};
static const unsigned MOD_TABLE[] = {0, 2, 1};
static const unsigned _MOD_TABLE[] = {0, 2, 1};
char *base64_encode(const unsigned char *str) {
@@ -57,13 +56,13 @@ char *base64_encode(const unsigned char *str) {
unsigned triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
encoded[encoded_index++] = ENCODING_TABLE[(triple >> 3 * 6) & 0x3F];
encoded[encoded_index++] = ENCODING_TABLE[(triple >> 2 * 6) & 0x3F];
encoded[encoded_index++] = ENCODING_TABLE[(triple >> 1 * 6) & 0x3F];
encoded[encoded_index++] = ENCODING_TABLE[(triple >> 0 * 6) & 0x3F];
encoded[encoded_index++] = _ENCODING_TABLE[(triple >> 3 * 6) & 0x3F];
encoded[encoded_index++] = _ENCODING_TABLE[(triple >> 2 * 6) & 0x3F];
encoded[encoded_index++] = _ENCODING_TABLE[(triple >> 1 * 6) & 0x3F];
encoded[encoded_index++] = _ENCODING_TABLE[(triple >> 0 * 6) & 0x3F];
}
for (unsigned index = 0; index < MOD_TABLE[str_len % 3]; index++) {
for (unsigned index = 0; index < _MOD_TABLE[str_len % 3]; index++) {
encoded[encoded_size - 2 - index] = '=';
}

View File

@@ -22,7 +22,5 @@
#pragma once
#include <stdlib.h>
char *base64_encode(const unsigned char *str);

View File

@@ -24,13 +24,13 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <setjmp.h>
#include <jpeglib.h>
#include "../tools.h"
#include "../logging.h"
#include "../picture.h"
#include "data/blank_jpeg.h"
@@ -41,55 +41,44 @@ struct _jpeg_error_manager_t {
};
static struct blank_t *_blank_init_internal(void);
static struct blank_t *_blank_init_external(const char *path);
static struct picture_t *_init_internal(void);
static struct picture_t *_init_external(const char *path);
static int _jpeg_read_geometry(FILE *fp, unsigned *width, unsigned *height);
static void _jpeg_error_handler(j_common_ptr jpeg);
struct blank_t *blank_init(const char *path) {
struct blank_t *blank = NULL;
struct picture_t *blank_picture_init(const char *path) {
struct picture_t *blank = NULL;
if (path) {
blank = _blank_init_external(path);
blank = _init_external(path);
}
if (blank) {
LOG_INFO("Using external blank placeholder: %s", path);
} else {
blank = _blank_init_internal();
blank = _init_internal();
LOG_INFO("Using internal blank placeholder");
}
return blank;
}
void blank_destroy(struct blank_t *blank) {
free(blank->picture.data);
free(blank);
}
static struct blank_t *_blank_init_internal(void) {
struct blank_t *blank;
A_CALLOC(blank, 1);
A_CALLOC(blank->picture.data, ARRAY_LEN(BLANK_JPEG_DATA));
memcpy(blank->picture.data, BLANK_JPEG_DATA, ARRAY_LEN(BLANK_JPEG_DATA));
blank->picture.used = ARRAY_LEN(BLANK_JPEG_DATA);
blank->picture.allocated = ARRAY_LEN(BLANK_JPEG_DATA);
static struct picture_t *_init_internal(void) {
struct picture_t *blank;
blank = picture_init();
picture_set_data(blank, BLANK_JPEG_DATA, ARRAY_LEN(BLANK_JPEG_DATA));
blank->width = BLANK_JPEG_WIDTH;
blank->height = BLANK_JPEG_HEIGHT;
return blank;
}
static struct blank_t *_blank_init_external(const char *path) {
static struct picture_t *_init_external(const char *path) {
FILE *fp = NULL;
struct blank_t *blank;
struct picture_t *blank;
A_CALLOC(blank, 1);
blank = picture_init();
if ((fp = fopen(path, "rb")) == NULL) {
LOG_PERROR("Can't open blank placeholder '%s'", path);
@@ -107,13 +96,12 @@ static struct blank_t *_blank_init_external(const char *path) {
# define CHUNK_SIZE (100 * 1024)
while (true) {
if (blank->picture.used + CHUNK_SIZE >= blank->picture.allocated) {
blank->picture.allocated = blank->picture.used + CHUNK_SIZE * 2;
A_REALLOC(blank->picture.data, blank->picture.allocated);
if (blank->used + CHUNK_SIZE >= blank->allocated) {
picture_realloc_data(blank, blank->used + CHUNK_SIZE * 2);
}
size_t readed = fread(blank->picture.data + blank->picture.used, 1, CHUNK_SIZE, fp);
blank->picture.used += readed;
size_t readed = fread(blank->data + blank->used, 1, CHUNK_SIZE, fp);
blank->used += readed;
if (readed < CHUNK_SIZE) {
if (feof(fp)) {
@@ -127,8 +115,7 @@ static struct blank_t *_blank_init_external(const char *path) {
# undef CHUNK_SIZE
error:
free(blank->picture.data);
free(blank);
picture_destroy(blank);
blank = NULL;
ok:

View File

@@ -22,17 +22,7 @@
#pragma once
#include <stdlib.h>
#include "../device.h"
#include "../picture.h"
struct blank_t {
struct picture_t picture;
unsigned width;
unsigned height;
};
struct blank_t *blank_init(const char *path);
void blank_destroy(struct blank_t *blank);
struct picture_t *blank_picture_init(const char *path);

View File

@@ -56,7 +56,7 @@ const char *guess_mime_type(const char *path) {
char *dot;
char *ext;
dot = strchr(path, '.');
dot = strrchr(path, '.');
if (dot == NULL || strchr(dot, '/') != NULL) {
goto misc;
}

View File

@@ -22,10 +22,11 @@
#include "path.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef TEST_HTTP_PATH
# include <stdio.h>
# include <stdlib.h>
#endif
#include <string.h>
#include <assert.h>
#include "../tools.h"
@@ -116,7 +117,7 @@ char *simplify_request_path(const char *str) {
return simplified;
}
#if 0
#ifdef TEST_HTTP_PATH
int test_simplify_request_path(const char *sample, const char *expected) {
char *result = simplify_request_path(sample);

View File

@@ -22,19 +22,17 @@
#include "server.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <event2/event.h>
#include <event2/thread.h>
@@ -50,23 +48,26 @@
#endif
#include "../tools.h"
#include "../threading.h"
#include "../logging.h"
#include "../process.h"
#include "../picture.h"
#include "../encoder.h"
#include "../stream.h"
#ifdef WITH_GPIO
# include "../gpio.h"
#endif
#include "blank.h"
#include "unix.h"
#include "uri.h"
#include "base64.h"
#include "mime.h"
#include "static.h"
#include "blank.h"
#include "data/index_html.h"
static bool _http_get_param_true(struct evkeyvalq *params, const char *key);
static char *_http_get_param_uri(struct evkeyvalq *params, const char *key);
static int _http_preprocess_request(struct evhttp_request *request, struct http_server_t *server);
static void _http_callback_root(struct evhttp_request *request, void *v_server);
@@ -91,6 +92,7 @@ struct http_server_t *http_server_init(struct stream_t *stream) {
struct exposed_t *exposed;
A_CALLOC(exposed, 1);
exposed->picture = picture_init();
A_CALLOC(run, 1);
run->stream = stream;
@@ -105,6 +107,7 @@ struct http_server_t *http_server_init(struct stream_t *stream) {
server->passwd = "";
server->static_path = "";
server->timeout = 10;
server->last_as_blank = -1;
server->run = run;
assert(!evthread_use_pthreads());
@@ -143,10 +146,10 @@ void http_server_destroy(struct http_server_t *server) {
}
if (server->run->blank) {
blank_destroy(server->run->blank);
picture_destroy(server->run->blank);
}
free(server->run->exposed->picture.data);
picture_destroy(server->run->exposed->picture);
free(server->run->exposed);
free(server->run);
free(server);
@@ -155,6 +158,7 @@ void http_server_destroy(struct http_server_t *server) {
int http_server_listen(struct http_server_t *server) {
{
if (server->static_path[0] != '\0') {
LOG_INFO("Enabling HTTP file server: %s", server->static_path);
evhttp_set_gencb(server->run->http, _http_callback_static, (void *)server);
} else {
assert(!evhttp_set_cb(server->run->http, "/", _http_callback_root, (void *)server));
@@ -165,8 +169,18 @@ int http_server_listen(struct http_server_t *server) {
}
server->run->drop_same_frames_blank = max_u(server->drop_same_frames, server->run->drop_same_frames_blank);
server->run->blank = blank_init(server->blank_path);
_expose_blank_picture(server);
server->run->blank = blank_picture_init(server->blank_path);
# define EXPOSED(_next) server->run->exposed->_next
// See _expose_blank_picture()
picture_copy(server->run->blank, EXPOSED(picture));
EXPOSED(expose_begin_ts) = get_now_monotonic();
EXPOSED(expose_cmp_ts) = EXPOSED(expose_begin_ts);
EXPOSED(expose_end_ts) = EXPOSED(expose_begin_ts);
// See _http_exposed_refresh()
EXPOSED(notify_last_width) = EXPOSED(picture->width);
EXPOSED(notify_last_height) = EXPOSED(picture->height);
# undef EXPOSED
{
struct timeval refresh_interval;
@@ -205,51 +219,16 @@ int http_server_listen(struct http_server_t *server) {
}
if (server->unix_path[0] != '\0') {
struct sockaddr_un unix_addr;
LOG_DEBUG("Binding HTTP to UNIX socket '%s' ...", server->unix_path);
# define MAX_SUN_PATH (sizeof(unix_addr.sun_path) - 1)
if (strlen(server->unix_path) > MAX_SUN_PATH) {
LOG_ERROR("UNIX socket path is too long; max=%zu", MAX_SUN_PATH);
if ((server->run->unix_fd = evhttp_my_bind_unix(
server->run->http,
server->unix_path,
server->unix_rm,
server->unix_mode)) < 0
) {
return -1;
}
MEMSET_ZERO(unix_addr);
strncpy(unix_addr.sun_path, server->unix_path, MAX_SUN_PATH);
unix_addr.sun_family = AF_UNIX;
# undef MAX_SUN_PATH
assert((server->run->unix_fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0);
assert(!evutil_make_socket_nonblocking(server->run->unix_fd));
if (server->unix_rm && unlink(server->unix_path) < 0) {
if (errno != ENOENT) {
LOG_PERROR("Can't remove old UNIX socket '%s'", server->unix_path);
return -1;
}
}
if (bind(server->run->unix_fd, (struct sockaddr *)&unix_addr, sizeof(struct sockaddr_un)) < 0) {
LOG_PERROR("Can't bind HTTP to UNIX socket '%s'", server->unix_path);
return -1;
}
if (server->unix_mode && chmod(server->unix_path, server->unix_mode) < 0) {
LOG_PERROR("Can't set permissions %o to UNIX socket '%s'", server->unix_mode, server->unix_path);
return -1;
}
if (listen(server->run->unix_fd, 128) < 0) {
LOG_PERROR("Can't listen UNIX socket '%s'", server->unix_path);
return -1;
}
if (evhttp_accept_socket(server->run->http, server->run->unix_fd) < 0) {
LOG_PERROR("Can't evhttp_accept_socket() UNIX socket '%s'", server->unix_path);
return -1;
}
LOG_INFO("Listening HTTP on UNIX socket '%s'", server->unix_path);
} else {
LOG_DEBUG("Binding HTTP to [%s]:%u ...", server->host, server->port);
if (evhttp_bind_socket(server->run->http, server->host, server->port) < 0) {
@@ -272,31 +251,6 @@ void http_server_loop_break(struct http_server_t *server) {
event_base_loopbreak(server->run->base);
}
static bool _http_get_param_true(struct evkeyvalq *params, const char *key) {
const char *value_str;
if ((value_str = evhttp_find_header(params, key)) != NULL) {
if (
value_str[0] == '1'
|| !evutil_ascii_strcasecmp(value_str, "true")
|| !evutil_ascii_strcasecmp(value_str, "yes")
) {
return true;
}
}
return false;
}
static char *_http_get_param_uri(struct evkeyvalq *params, const char *key) {
const char *value_str;
if ((value_str = evhttp_find_header(params, key)) != NULL) {
return evhttp_encode_uri(value_str);
}
return NULL;
}
#define ADD_HEADER(_key, _value) \
assert(!evhttp_add_header(evhttp_request_get_output_headers(request), _key, _value))
@@ -377,7 +331,7 @@ static void _http_callback_static(struct evhttp_request *request, void *v_server
goto not_found;
}
if (evbuffer_add_file(buf, fd, 0, st.st_size) < 0) {
if (st.st_size > 0 && evbuffer_add_file(buf, fd, 0, st.st_size) < 0) {
LOG_ERROR("HTTP: Can't serve static file %s", static_path);
goto not_found;
}
@@ -420,6 +374,7 @@ static void _http_callback_state(struct evhttp_request *request, void *v_server)
PREPROCESS_REQUEST;
# define ENCODER(_next) server->run->stream->encoder->_next
# define EXPOSED(_next) server->run->exposed->_next
A_MUTEX_LOCK(&ENCODER(run->mutex));
encoder_run_type = ENCODER(run->type);
@@ -436,15 +391,16 @@ static void _http_callback_state(struct evhttp_request *request, void *v_server)
" \"stream\": {\"queued_fps\": %u, \"clients\": %u, \"clients_stat\": {",
encoder_type_to_string(encoder_run_type),
encoder_run_quality,
(server->fake_width ? server->fake_width : server->run->exposed->width),
(server->fake_height ? server->fake_height : server->run->exposed->height),
bool_to_string(server->run->exposed->online),
(server->fake_width ? server->fake_width : EXPOSED(picture->width)),
(server->fake_height ? server->fake_height : EXPOSED(picture->height)),
bool_to_string(EXPOSED(online)),
server->run->stream->dev->desired_fps,
server->run->exposed->captured_fps,
server->run->exposed->queued_fps,
EXPOSED(captured_fps),
EXPOSED(queued_fps),
server->run->stream_clients_count
));
# undef EXPOSED
# undef ENCODER
for (struct stream_client_t * client = server->run->stream_clients; client != NULL; client = client->next) {
@@ -476,7 +432,7 @@ static void _http_callback_snapshot(struct evhttp_request *request, void *v_serv
# define EXPOSED(_next) server->run->exposed->_next
assert((buf = evbuffer_new()));
assert(!evbuffer_add(buf, (const void *)EXPOSED(picture.data), EXPOSED(picture.used)));
assert(!evbuffer_add(buf, (const void *)EXPOSED(picture->data), EXPOSED(picture->used)));
ADD_HEADER("Access-Control-Allow-Origin:", "*");
ADD_HEADER("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate, pre-check=0, post-check=0, max-age=0");
@@ -495,27 +451,26 @@ static void _http_callback_snapshot(struct evhttp_request *request, void *v_serv
ADD_TIME_HEADER("X-Timestamp", get_now_real());
ADD_HEADER("X-UStreamer-Online", bool_to_string(EXPOSED(online)));
ADD_UNSIGNED_HEADER("X-UStreamer-Dropped", EXPOSED(dropped));
ADD_UNSIGNED_HEADER("X-UStreamer-Width", EXPOSED(width));
ADD_UNSIGNED_HEADER("X-UStreamer-Height", EXPOSED(height));
ADD_TIME_HEADER("X-UStreamer-Grab-Time", EXPOSED(picture.grab_time));
ADD_TIME_HEADER("X-UStreamer-Encode-Begin-Time", EXPOSED(picture.encode_begin_time));
ADD_TIME_HEADER("X-UStreamer-Encode-End-Time", EXPOSED(picture.encode_end_time));
ADD_TIME_HEADER("X-UStreamer-Expose-Begin-Time", EXPOSED(expose_begin_time));
ADD_TIME_HEADER("X-UStreamer-Expose-Cmp-Time", EXPOSED(expose_cmp_time));
ADD_TIME_HEADER("X-UStreamer-Expose-End-Time", EXPOSED(expose_end_time));
ADD_TIME_HEADER("X-UStreamer-Send-Time", get_now_monotonic());
ADD_HEADER("X-UStreamer-Online", bool_to_string(EXPOSED(online)));
ADD_UNSIGNED_HEADER("X-UStreamer-Dropped", EXPOSED(dropped));
ADD_UNSIGNED_HEADER("X-UStreamer-Width", EXPOSED(picture->width));
ADD_UNSIGNED_HEADER("X-UStreamer-Height", EXPOSED(picture->height));
ADD_TIME_HEADER("X-UStreamer-Grab-Timestamp", EXPOSED(picture->grab_ts));
ADD_TIME_HEADER("X-UStreamer-Encode-Begin-Timestamp", EXPOSED(picture->encode_begin_ts));
ADD_TIME_HEADER("X-UStreamer-Encode-End-Timestamp", EXPOSED(picture->encode_end_ts));
ADD_TIME_HEADER("X-UStreamer-Expose-Begin-Timestamp", EXPOSED(expose_begin_ts));
ADD_TIME_HEADER("X-UStreamer-Expose-Cmp-Timestamp", EXPOSED(expose_cmp_ts));
ADD_TIME_HEADER("X-UStreamer-Expose-End-Timestamp", EXPOSED(expose_end_ts));
ADD_TIME_HEADER("X-UStreamer-Send-Timestamp", get_now_monotonic());
# undef ADD_UNSUGNED_HEADER
# undef ADD_TIME_HEADER
# undef EXPOSED
ADD_HEADER("Content-Type", "image/jpeg");
evhttp_send_reply(request, HTTP_OK, "OK", buf);
evbuffer_free(buf);
# undef EXPOSED
}
#undef ADD_HEADER
@@ -547,10 +502,10 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server
client->need_first_frame = true;
evhttp_parse_query(evhttp_request_get_uri(request), &params);
client->key = _http_get_param_uri(&params, "key");
client->extra_headers = _http_get_param_true(&params, "extra_headers");
client->advance_headers = _http_get_param_true(&params, "advance_headers");
client->dual_final_frames = _http_get_param_true(&params, "dual_final_frames");
client->key = uri_get_string(&params, "key");
client->extra_headers = uri_get_true(&params, "extra_headers");
client->advance_headers = uri_get_true(&params, "advance_headers");
client->dual_final_frames = uri_get_true(&params, "dual_final_frames");
evhttp_clear_headers(&params);
uuid_generate(uuid);
@@ -663,7 +618,7 @@ static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_c
"Content-Length: %zu" RN
"X-Timestamp: %.06Lf" RN
"%s",
EXPOSED(picture.used),
EXPOSED(picture->used),
get_now_real(),
(client->extra_headers ? "" : RN)
));
@@ -684,21 +639,21 @@ static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_c
RN,
bool_to_string(EXPOSED(online)),
EXPOSED(dropped),
EXPOSED(width),
EXPOSED(height),
EXPOSED(picture->width),
EXPOSED(picture->height),
client->fps,
EXPOSED(picture.grab_time),
EXPOSED(picture.encode_begin_time),
EXPOSED(picture.encode_end_time),
EXPOSED(expose_begin_time),
EXPOSED(expose_cmp_time),
EXPOSED(expose_end_time),
EXPOSED(picture->grab_ts),
EXPOSED(picture->encode_begin_ts),
EXPOSED(picture->encode_end_ts),
EXPOSED(expose_begin_ts),
EXPOSED(expose_cmp_ts),
EXPOSED(expose_end_ts),
now
));
}
}
assert(!evbuffer_add(buf, (void *)EXPOSED(picture.data), EXPOSED(picture.used)));
assert(!evbuffer_add(buf, (void *)EXPOSED(picture->data), EXPOSED(picture->used)));
assert(evbuffer_add_printf(buf, RN "--" BOUNDARY RN));
if (client->advance_headers) {
@@ -823,7 +778,7 @@ static void _http_exposed_refresh(UNUSED int fd, UNUSED short what, void *v_serv
if (atomic_load(&server->run->stream->updated)) {
LOG_DEBUG("Refreshing HTTP exposed ...");
A_MUTEX_LOCK(&server->run->stream->mutex);
if (server->run->stream->picture.used > 0) { // If online
if (server->run->stream->online) {
picture_updated = _expose_new_picture_unsafe(server);
UNLOCK_STREAM;
} else {
@@ -840,109 +795,116 @@ static void _http_exposed_refresh(UNUSED int fd, UNUSED short what, void *v_serv
# undef UNLOCK_STREAM
_http_queue_send_stream(server, stream_updated, picture_updated);
# define EXPOSED(_next) server->run->exposed->_next
if (
picture_updated
&& server->notify_parent
&& (
EXPOSED(notify_last_online) != EXPOSED(online)
|| EXPOSED(notify_last_width) != EXPOSED(picture->width)
|| EXPOSED(notify_last_height) != EXPOSED(picture->height)
)
) {
EXPOSED(notify_last_online) = EXPOSED(online);
EXPOSED(notify_last_width) = EXPOSED(picture->width);
EXPOSED(notify_last_height) = EXPOSED(picture->height);
process_notify_parent();
}
# undef EXPOSED
}
static bool _expose_new_picture_unsafe(struct http_server_t *server) {
# define STREAM(_next) server->run->stream->_next
# define EXPOSED(_next) server->run->exposed->_next
# define STREAM(_next) server->run->stream->_next
assert(STREAM(picture.used) > 0);
EXPOSED(captured_fps) = STREAM(captured_fps);
EXPOSED(expose_begin_time) = get_now_monotonic();
# define MEM_STREAM_TO_EXPOSED \
EXPOSED(picture.data), STREAM(picture.data), STREAM(picture.used)
EXPOSED(expose_begin_ts) = get_now_monotonic();
if (server->drop_same_frames) {
if (
EXPOSED(online)
&& EXPOSED(dropped) < server->drop_same_frames
&& EXPOSED(picture.used) == STREAM(picture.used)
&& !memcmp(MEM_STREAM_TO_EXPOSED)
&& picture_compare(EXPOSED(picture), STREAM(picture))
) {
EXPOSED(expose_cmp_time) = get_now_monotonic();
EXPOSED(expose_end_time) = EXPOSED(expose_cmp_time);
LOG_VERBOSE("HTTP: dropped same frame number %u; cmp_time=%.06Lf",
EXPOSED(dropped), EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time));
EXPOSED(expose_cmp_ts) = get_now_monotonic();
EXPOSED(expose_end_ts) = EXPOSED(expose_cmp_ts);
LOG_VERBOSE("HTTP: Dropped same frame number %u; cmp_time=%.06Lf",
EXPOSED(dropped), EXPOSED(expose_cmp_ts) - EXPOSED(expose_begin_ts));
EXPOSED(dropped) += 1;
return false; // Not updated
} else {
EXPOSED(expose_cmp_time) = get_now_monotonic();
LOG_VERBOSE("HTTP: passed same frame check (frames are differ); cmp_time=%.06Lf",
EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time));
EXPOSED(expose_cmp_ts) = get_now_monotonic();
LOG_VERBOSE("HTTP: Passed same frame check (frames are differ); cmp_time=%.06Lf",
EXPOSED(expose_cmp_ts) - EXPOSED(expose_begin_ts));
}
}
if (EXPOSED(picture.allocated) < STREAM(picture.allocated)) {
A_REALLOC(EXPOSED(picture.data), STREAM(picture.allocated));
EXPOSED(picture.allocated) = STREAM(picture.allocated);
}
picture_copy(STREAM(picture), EXPOSED(picture));
memcpy(MEM_STREAM_TO_EXPOSED);
# undef STREAM
# undef MEM_STREAM_TO_EXPOSED
EXPOSED(picture.used) = STREAM(picture.used);
EXPOSED(picture.grab_time) = STREAM(picture.grab_time);
EXPOSED(picture.encode_begin_time) = STREAM(picture.encode_begin_time);
EXPOSED(picture.encode_end_time) = STREAM(picture.encode_end_time);
EXPOSED(width) = STREAM(width);
EXPOSED(height) = STREAM(height);
EXPOSED(online) = true;
EXPOSED(dropped) = 0;
EXPOSED(expose_cmp_time) = EXPOSED(expose_begin_time);
EXPOSED(expose_end_time) = get_now_monotonic();
EXPOSED(expose_cmp_ts) = EXPOSED(expose_begin_ts);
EXPOSED(expose_end_ts) = get_now_monotonic();
LOG_VERBOSE("HTTP: exposed new frame; full exposition time = %.06Lf",
EXPOSED(expose_end_time) - EXPOSED(expose_begin_time));
LOG_VERBOSE("HTTP: Exposed new frame; full exposition time = %.06Lf",
EXPOSED(expose_end_ts) - EXPOSED(expose_begin_ts));
# undef EXPOSED
# undef STREAM
return true; // Updated
}
static bool _expose_blank_picture(struct http_server_t *server) {
# define BLANK(_next) server->run->blank->_next
# define EXPOSED(_next) server->run->exposed->_next
# define EXPOSED(_next) server->run->exposed->_next
EXPOSED(expose_begin_time) = get_now_monotonic();
EXPOSED(expose_cmp_time) = EXPOSED(expose_begin_time);
EXPOSED(expose_begin_ts) = get_now_monotonic();
EXPOSED(expose_cmp_ts) = EXPOSED(expose_begin_ts);
if (EXPOSED(online) || EXPOSED(picture.used) == 0) {
if (EXPOSED(picture.allocated) < BLANK(picture.used)) {
A_REALLOC(EXPOSED(picture.data), BLANK(picture.used));
EXPOSED(picture.allocated) = BLANK(picture.used);
# define EXPOSE_BLANK picture_copy(server->run->blank, EXPOSED(picture))
if (EXPOSED(online)) { // Если переходим из online в offline
if (server->last_as_blank < 0) { // Если last_as_blank выключено, просто покажем картинку
LOG_INFO("HTTP: Changed picture to BLANK");
EXPOSE_BLANK;
} else if (server->last_as_blank > 0) { // Если нужен таймер - запустим
LOG_INFO("HTTP: Freezing last alive frame for %d seconds", server->last_as_blank);
EXPOSED(last_as_blank_ts) = get_now_monotonic();
} else { // last_as_blank == 0 - показываем последний фрейм вечно
LOG_INFO("HTTP: Freezing last alive frame forever");
}
memcpy(EXPOSED(picture.data), BLANK(picture.data), BLANK(picture.used));
EXPOSED(picture.used) = BLANK(picture.used);
EXPOSED(picture.grab_time) = 0;
EXPOSED(picture.encode_begin_time) = 0;
EXPOSED(picture.encode_end_time) = 0;
EXPOSED(width) = BLANK(width);
EXPOSED(height) = BLANK(height);
EXPOSED(captured_fps) = 0;
EXPOSED(online) = false;
goto updated;
}
if ( // Если уже оффлайн, включена фича last_as_blank с таймером и он запущен
server->last_as_blank > 0
&& EXPOSED(last_as_blank_ts) > 0
&& EXPOSED(last_as_blank_ts) + server->last_as_blank < EXPOSED(expose_begin_ts)
) {
LOG_INFO("HTTP: Changed last alive frame to BLANK");
EXPOSE_BLANK;
EXPOSED(last_as_blank_ts) = 0; // Останавливаем таймер
goto updated;
}
# undef EXPOSE_BLANK
if (EXPOSED(dropped) < server->run->drop_same_frames_blank) {
LOG_PERF("HTTP: dropped same frame (BLANK) number %u", EXPOSED(dropped));
LOG_PERF("HTTP: Dropped same frame (BLANK) number %u", EXPOSED(dropped));
EXPOSED(dropped) += 1;
EXPOSED(expose_end_time) = get_now_monotonic();
EXPOSED(expose_end_ts) = get_now_monotonic();
return false; // Not updated
}
updated:
EXPOSED(captured_fps) = 0;
EXPOSED(online) = false;
EXPOSED(dropped) = 0;
EXPOSED(expose_end_time) = get_now_monotonic();
EXPOSED(expose_end_ts) = get_now_monotonic();
return true; // Updated
# undef EXPOSED
# undef BLANK
}

View File

@@ -30,11 +30,9 @@
#include <event2/http.h>
#include <event2/util.h>
#include "../tools.h"
#include "../picture.h"
#include "../stream.h"
#include "blank.h"
struct stream_client_t {
struct http_server_t *server;
@@ -58,16 +56,19 @@ struct stream_client_t {
};
struct exposed_t {
struct picture_t picture;
unsigned width;
unsigned height;
struct picture_t *picture;
unsigned captured_fps;
unsigned queued_fps;
bool online;
unsigned dropped;
long double expose_begin_time;
long double expose_cmp_time;
long double expose_end_time;
long double expose_begin_ts;
long double expose_cmp_ts;
long double expose_end_ts;
long double last_as_blank_ts;
bool notify_last_online;
unsigned notify_last_width;
unsigned notify_last_height;
};
struct http_server_runtime_t {
@@ -80,7 +81,7 @@ struct http_server_runtime_t {
struct exposed_t *exposed;
struct stream_client_t *stream_clients;
unsigned stream_clients_count;
struct blank_t *blank;
struct picture_t *blank;
unsigned drop_same_frames_blank;
};
@@ -97,11 +98,14 @@ struct http_server_t {
char *static_path;
char *blank_path;
int last_as_blank;
unsigned drop_same_frames;
bool slowdown;
unsigned fake_width;
unsigned fake_height;
bool notify_parent;
struct http_server_runtime_t *run;
};

View File

@@ -27,7 +27,6 @@
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "../tools.h"
@@ -43,6 +42,7 @@ char *find_static_file_path(const char *root_path, const char *request_path) {
simplified_path = simplify_request_path(request_path);
if (simplified_path[0] == '\0') {
LOG_VERBOSE("HTTP: Invalid request path %s to static", request_path);
goto error;
}
@@ -51,13 +51,14 @@ char *find_static_file_path(const char *root_path, const char *request_path) {
# define LOAD_STAT { \
if (lstat(path, &st) < 0) { \
/* LOG_PERROR("Can't stat() file %s", path); */ \
LOG_VERBOSE_PERROR("HTTP: Can't stat() static path %s", path); \
goto error; \
} \
}
LOAD_STAT;
if (S_ISDIR(st.st_mode)) {
LOG_VERBOSE("HTTP: Requested static path %s is a directory, trying %s/index.html", path, path);
strcat(path, "/index.html");
LOAD_STAT;
}
@@ -65,12 +66,12 @@ char *find_static_file_path(const char *root_path, const char *request_path) {
# undef LOAD_STAT
if (!S_ISREG(st.st_mode)) {
// LOG_ERROR("Not a regulary file: %s", path);
LOG_VERBOSE("HTTP: Not a regular file: %s", path);
goto error;
}
if (access(path, R_OK) < 0) {
// LOG_PERROR("Can't access() R_OK file %s", path);
LOG_VERBOSE_PERROR("HTTP: Can't access() R_OK file %s", path);
goto error;
}

85
src/http/unix.c Normal file
View File

@@ -0,0 +1,85 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include "unix.h"
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <event2/http.h>
#include <event2/util.h>
#include "../tools.h"
#include "../logging.h"
evutil_socket_t evhttp_my_bind_unix(struct evhttp *http, const char *path, bool rm, mode_t mode) {
evutil_socket_t fd = -1;
struct sockaddr_un addr;
# define MAX_SUN_PATH (sizeof(addr.sun_path) - 1)
if (strlen(path) > MAX_SUN_PATH) {
LOG_ERROR("UNIX socket path is too long; max=%zu", MAX_SUN_PATH);
return -1;
}
MEMSET_ZERO(addr);
strncpy(addr.sun_path, path, MAX_SUN_PATH);
addr.sun_family = AF_UNIX;
# undef MAX_SUN_PATH
assert((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0);
assert(!evutil_make_socket_nonblocking(fd));
if (rm && unlink(path) < 0) {
if (errno != ENOENT) {
LOG_PERROR("Can't remove old UNIX socket '%s'", path);
return -1;
}
}
if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
LOG_PERROR("Can't bind HTTP to UNIX socket '%s'", path);
return -1;
}
if (mode && chmod(path, mode) < 0) {
LOG_PERROR("Can't set permissions %o to UNIX socket '%s'", mode, path);
return -1;
}
if (listen(fd, 128) < 0) {
LOG_PERROR("Can't listen UNIX socket '%s'", path);
return -1;
}
if (evhttp_accept_socket(http, fd) < 0) {
LOG_PERROR("Can't evhttp_accept_socket() UNIX socket '%s'", path);
return -1;
}
return fd;
}

33
src/http/unix.h Normal file
View File

@@ -0,0 +1,33 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#pragma once
#include <stdbool.h>
#include <sys/stat.h>
#include <event2/http.h>
#include <event2/util.h>
evutil_socket_t evhttp_my_bind_unix(struct evhttp *http, const char *path, bool rm, mode_t mode);

54
src/http/uri.c Normal file
View File

@@ -0,0 +1,54 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include "uri.h"
#include <stdbool.h>
#include <event2/http.h>
#include <event2/util.h>
#include <event2/keyvalq_struct.h>
bool uri_get_true(struct evkeyvalq *params, const char *key) {
const char *value_str;
if ((value_str = evhttp_find_header(params, key)) != NULL) {
if (
value_str[0] == '1'
|| !evutil_ascii_strcasecmp(value_str, "true")
|| !evutil_ascii_strcasecmp(value_str, "yes")
) {
return true;
}
}
return false;
}
char *uri_get_string(struct evkeyvalq *params, const char *key) {
const char *value_str;
if ((value_str = evhttp_find_header(params, key)) != NULL) {
return evhttp_encode_uri(value_str);
}
return NULL;
}

32
src/http/uri.h Normal file
View File

@@ -0,0 +1,32 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#pragma once
#include <stdbool.h>
#include <event2/http.h>
#include <event2/keyvalq_struct.h>
bool uri_get_true(struct evkeyvalq *params, const char *key);
char *uri_get_string(struct evkeyvalq *params, const char *key);

View File

@@ -34,6 +34,7 @@
#include <pthread.h>
#include "tools.h"
#include "threading.h"
enum {
@@ -51,13 +52,13 @@ pthread_mutex_t log_mutex;
#define LOGGING_INIT { \
log_level = LOG_LEVEL_INFO; \
log_colored = isatty(1); \
assert(!pthread_mutex_init(&log_mutex, NULL)); \
A_MUTEX_INIT(&log_mutex); \
}
#define LOGGING_DESTROY assert(!pthread_mutex_destroy(&log_mutex))
#define LOGGING_DESTROY A_MUTEX_DESTROY(&log_mutex)
#define LOGGING_LOCK assert(!pthread_mutex_lock(&log_mutex))
#define LOGGING_UNLOCK assert(!pthread_mutex_unlock(&log_mutex))
#define LOGGING_LOCK A_MUTEX_LOCK(&log_mutex)
#define LOGGING_UNLOCK A_MUTEX_UNLOCK(&log_mutex)
#define COLOR_GRAY "\x1b[30;1m"
@@ -87,12 +88,14 @@ pthread_mutex_t log_mutex;
#define LOG_PRINTF_NOLOCK(_label_color, _label, _msg_color, _msg, ...) { \
char _tname_buf[MAX_THREAD_NAME] = {0}; \
thread_get_name(_tname_buf); \
if (log_colored) { \
printf(COLOR_GRAY "-- " _label_color _label COLOR_GRAY " [%.03Lf tid=%d]" " -- " COLOR_RESET _msg_color _msg COLOR_RESET, \
get_now_monotonic(), get_thread_id(), ##__VA_ARGS__); \
printf(COLOR_GRAY "-- " _label_color _label COLOR_GRAY " [%.03Lf %9s]" " -- " COLOR_RESET _msg_color _msg COLOR_RESET, \
get_now_monotonic(), _tname_buf, ##__VA_ARGS__); \
} else { \
printf("-- " _label " [%.03Lf tid=%d] -- " _msg, \
get_now_monotonic(), get_thread_id(), ##__VA_ARGS__); \
printf("-- " _label " [%.03Lf %9s] -- " _msg, \
get_now_monotonic(), _tname_buf, ##__VA_ARGS__); \
} \
putchar('\n'); \
fflush(stdout); \
@@ -109,9 +112,9 @@ pthread_mutex_t log_mutex;
}
#define LOG_PERROR(_msg, ...) { \
char _buf[1024] = ""; \
char *_ptr = errno_to_string(_buf, 1024); \
LOG_ERROR(_msg ": %s", ##__VA_ARGS__, _ptr); \
char _perror_buf[1024] = {0}; \
char *_perror_ptr = errno_to_string(_perror_buf, 1024); \
LOG_ERROR(_msg ": %s", ##__VA_ARGS__, _perror_ptr); \
}
#define LOG_INFO(_msg, ...) { \
@@ -140,6 +143,14 @@ pthread_mutex_t log_mutex;
} \
}
#define LOG_VERBOSE_PERROR(_msg, ...) { \
if (log_level >= LOG_LEVEL_VERBOSE) { \
char _perror_buf[1024] = {0}; \
char *_perror_ptr = errno_to_string(_perror_buf, 1024); \
LOG_PRINTF(COLOR_BLUE, "VERB ", COLOR_BLUE, _msg ": %s", ##__VA_ARGS__, _perror_ptr); \
} \
}
#define LOG_DEBUG(_msg, ...) { \
if (log_level >= LOG_LEVEL_DEBUG) { \
LOG_PRINTF(COLOR_GRAY, "DEBUG", COLOR_GRAY, _msg, ##__VA_ARGS__); \

View File

@@ -25,14 +25,18 @@
# error WTF dude? Asserts are good things!
#endif
#include <limits.h>
#if CHAR_BIT != 8
# error There are not 8 bits in a char!
#endif
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include "tools.h"
#include "threading.h"
#include "logging.h"
#include "options.h"
#include "device.h"
@@ -60,12 +64,14 @@ static void _block_thread_signals(void) {
}
static void *_stream_loop_thread(UNUSED void *arg) {
A_THREAD_RENAME("stream");
_block_thread_signals();
stream_loop(_ctx->stream);
return NULL;
}
static void *_server_loop_thread(UNUSED void *arg) {
A_THREAD_RENAME("http");
_block_thread_signals();
http_server_loop(_ctx->server);
return NULL;
@@ -97,6 +103,7 @@ static void _install_signal_handlers(void) {
}
int main(int argc, char *argv[]) {
struct options_t *options;
struct device_t *dev;
struct encoder_t *encoder;
struct stream_t *stream;
@@ -104,6 +111,8 @@ int main(int argc, char *argv[]) {
int exit_code = 0;
LOGGING_INIT;
A_THREAD_RENAME("main");
options = options_init(argc, argv);
# ifdef WITH_GPIO
GPIO_INIT;
@@ -114,7 +123,7 @@ int main(int argc, char *argv[]) {
stream = stream_init(dev, encoder);
server = http_server_init(stream);
if ((exit_code = parse_options(argc, argv, dev, encoder, server)) == 0) {
if ((exit_code = options_parse(options, dev, encoder, server)) == 0) {
# ifdef WITH_GPIO
GPIO_INIT_PINOUT;
# endif
@@ -150,6 +159,10 @@ int main(int argc, char *argv[]) {
GPIO_SET_LOW(prog_running);
# endif
options_destroy(options);
if (exit_code == 0) {
LOG_INFO("Bye-bye");
}
LOGGING_DESTROY;
return (exit_code < 0 ? 1 : 0);
}

View File

@@ -23,20 +23,21 @@
#include "options.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <limits.h>
#include <signal.h>
#include <getopt.h>
#include <errno.h>
#include <assert.h>
#include "config.h"
#include "tools.h"
#include "logging.h"
#include "process.h"
#include "device.h"
#include "encoder.h"
#include "stream.h"
#include "http/server.h"
#ifdef WITH_GPIO
# include "gpio.h"
@@ -67,6 +68,7 @@ enum _OPT_VALUES {
_O_UNIX_RM = 'D',
_O_UNIX_MODE = 'M',
_O_BLANK = 'k',
_O_LAST_AS_BLANK = 'K',
_O_DROP_SAME_FRAMES = 'e',
_O_SLOWDOWN = 'l',
_O_FAKE_RESOLUTION = 'R',
@@ -79,19 +81,16 @@ enum _OPT_VALUES {
_O_DEVICE_TIMEOUT = 10000,
_O_DEVICE_ERROR_DELAY,
_O_IMAGE_DEFAULT,
_O_BRIGHTNESS,
_O_BRIGHTNESS_AUTO,
_O_CONTRAST,
_O_SATURATION,
_O_HUE,
_O_HUE_AUTO,
_O_GAMMA,
_O_SHARPNESS,
_O_BACKLIGHT_COMPENSATION,
_O_WHITE_BALANCE,
_O_WHITE_BALANCE_AUTO,
_O_GAIN,
_O_GAIN_AUTO,
_O_USER,
_O_PASSWD,
@@ -105,12 +104,22 @@ enum _OPT_VALUES {
_O_GPIO_WORKERS_BUSY_AT,
#endif
#ifdef HAS_PDEATHSIG
_O_EXIT_ON_PARENT_DEATH,
#endif
#ifdef WITH_SETPROCTITLE
_O_PROCESS_NAME_PREFIX,
#endif
_O_NOTIFY_PARENT,
_O_LOG_LEVEL,
_O_PERF,
_O_VERBOSE,
_O_DEBUG,
_O_FORCE_LOG_COLORS,
_O_NO_LOG_COLORS,
_O_FEATURES,
};
static const struct option _LONG_OPTS[] = {
@@ -133,19 +142,16 @@ static const struct option _LONG_OPTS[] = {
{"device-timeout", required_argument, NULL, _O_DEVICE_TIMEOUT},
{"device-error-delay", required_argument, NULL, _O_DEVICE_ERROR_DELAY},
{"image-default", no_argument, NULL, _O_IMAGE_DEFAULT},
{"brightness", required_argument, NULL, _O_BRIGHTNESS},
{"brightness-auto", no_argument, NULL, _O_BRIGHTNESS_AUTO},
{"contrast", required_argument, NULL, _O_CONTRAST},
{"saturation", required_argument, NULL, _O_SATURATION},
{"hue", required_argument, NULL, _O_HUE},
{"hue-auto", no_argument, NULL, _O_HUE_AUTO},
{"gamma", required_argument, NULL, _O_GAMMA},
{"sharpness", required_argument, NULL, _O_SHARPNESS},
{"backlight-compensation", required_argument, NULL, _O_BACKLIGHT_COMPENSATION},
{"white-balance", required_argument, NULL, _O_WHITE_BALANCE},
{"white-balance-auto", no_argument, NULL, _O_WHITE_BALANCE_AUTO},
{"gain", required_argument, NULL, _O_GAIN},
{"gain-auto", no_argument, NULL, _O_GAIN_AUTO},
{"host", required_argument, NULL, _O_HOST},
{"port", required_argument, NULL, _O_PORT},
@@ -156,6 +162,7 @@ static const struct option _LONG_OPTS[] = {
{"passwd", required_argument, NULL, _O_PASSWD},
{"static", required_argument, NULL, _O_STATIC},
{"blank", required_argument, NULL, _O_BLANK},
{"last-as-blank", required_argument, NULL, _O_LAST_AS_BLANK},
{"drop-same-frames", required_argument, NULL, _O_DROP_SAME_FRAMES},
{"slowdown", no_argument, NULL, _O_SLOWDOWN},
{"fake-resolution", required_argument, NULL, _O_FAKE_RESOLUTION},
@@ -168,6 +175,14 @@ static const struct option _LONG_OPTS[] = {
{"gpio-workers-busy-at", required_argument, NULL, _O_GPIO_WORKERS_BUSY_AT},
#endif
#ifdef HAS_PDEATHSIG
{"exit-on-parent-death", no_argument, NULL, _O_EXIT_ON_PARENT_DEATH},
#endif
#ifdef WITH_SETPROCTITLE
{"process-name-prefix", required_argument, NULL, _O_PROCESS_NAME_PREFIX},
#endif
{"notify-parent", no_argument, NULL, _O_NOTIFY_PARENT},
{"log-level", required_argument, NULL, _O_LOG_LEVEL},
{"perf", no_argument, NULL, _O_PERF},
{"verbose", no_argument, NULL, _O_VERBOSE},
@@ -177,6 +192,7 @@ static const struct option _LONG_OPTS[] = {
{"help", no_argument, NULL, _O_HELP},
{"version", no_argument, NULL, _O_VERSION},
{"features", no_argument, NULL, _O_FEATURES},
{NULL, 0, NULL, 0},
};
@@ -187,11 +203,35 @@ static int _parse_resolution(const char *str, unsigned *width, unsigned *height,
static int _parse_glitched_resolutions(const char *str, struct encoder_t *encoder);
#endif
static void _version(bool nl);
static void _features(void);
static void _help(struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server);
int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server) {
struct options_t *options_init(int argc, char *argv[]) {
struct options_t *options;
A_CALLOC(options, 1);
options->argc = argc;
options->argv = argv;
A_CALLOC(options->argv_copy, argc);
for (int index = 0; index < argc; ++index) {
assert(options->argv_copy[index] = strdup(argv[index]));
}
return options;
}
void options_destroy(struct options_t *options) {
for (int index = 0; index < options->argc; ++index) {
free(options->argv_copy[index]);
}
free(options->argv_copy);
free(options);
}
int options_parse(struct options_t *options, struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server) {
# define OPT_SET(_dest, _value) { \
_dest = _value; \
break; \
@@ -224,15 +264,6 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t
break; \
}
# ifdef WITH_OMX
# define OPT_GLITCHED_RESOLUTIONS { \
if (_parse_glitched_resolutions(optarg, encoder) < 0) { \
return -1; \
} \
break; \
}
# endif
# define OPT_PARSE(_name, _dest, _func, _invalid, _available) { \
if ((_dest = _func(optarg)) == _invalid) { \
printf("Unknown " _name ": %s; available: %s\n", optarg, _available); \
@@ -241,16 +272,29 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t
break; \
}
# define OPT_CTL(_dest) { \
dev->ctl._dest.value_set = true; \
dev->ctl._dest.auto_set = false; \
OPT_NUMBER("--"#_dest, dev->ctl._dest.value, INT_MIN, INT_MAX, 0); \
# define OPT_CTL_DEFAULT_NOBREAK(_dest) { \
dev->ctl._dest.mode = CTL_MODE_DEFAULT; \
}
# define OPT_CTL_MANUAL(_dest) { \
if (!strcasecmp(optarg, "default")) { \
OPT_CTL_DEFAULT_NOBREAK(_dest); \
} else { \
dev->ctl._dest.mode = CTL_MODE_VALUE; \
OPT_NUMBER("--"#_dest, dev->ctl._dest.value, INT_MIN, INT_MAX, 0); \
} \
break; \
}
# define OPT_CTL_AUTO(_dest) { \
dev->ctl._dest.value_set = false; \
dev->ctl._dest.auto_set = true; \
if (!strcasecmp(optarg, "default")) { \
OPT_CTL_DEFAULT_NOBREAK(_dest); \
} else if (!strcasecmp(optarg, "auto")) { \
dev->ctl._dest.mode = CTL_MODE_AUTO; \
} else { \
dev->ctl._dest.mode = CTL_MODE_VALUE; \
OPT_NUMBER("--"#_dest, dev->ctl._dest.value, INT_MIN, INT_MAX, 0); \
} \
break; \
}
@@ -258,6 +302,9 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t
int short_index;
int opt_index;
char short_opts[1024] = {0};
# ifdef WITH_SETPROCTITLE
char *process_name_prefix = NULL;
# endif
for (short_index = 0, opt_index = 0; _LONG_OPTS[opt_index].name != NULL; ++opt_index) {
if (isalpha(_LONG_OPTS[opt_index].val)) {
@@ -270,7 +317,7 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t
}
}
while ((ch = getopt_long(argc, argv, short_opts, _LONG_OPTS, NULL)) >= 0) {
while ((ch = getopt_long(options->argc, options->argv_copy, short_opts, _LONG_OPTS, NULL)) >= 0) {
switch (ch) {
case _O_DEVICE: OPT_SET(dev->path, optarg);
case _O_INPUT: OPT_NUMBER("--input", dev->input, 0, 128, 0);
@@ -289,24 +336,35 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t
case _O_QUALITY: OPT_NUMBER("--quality", encoder->quality, 1, 100, 0);
case _O_ENCODER: OPT_PARSE("encoder type", encoder->type, encoder_parse_type, ENCODER_TYPE_UNKNOWN, ENCODER_TYPES_STR);
# ifdef WITH_OMX
case _O_GLITCHED_RESOLUTIONS: OPT_GLITCHED_RESOLUTIONS;
case _O_GLITCHED_RESOLUTIONS:
if (_parse_glitched_resolutions(optarg, encoder) < 0) {
return -1;
}
break;
# endif
case _O_DEVICE_TIMEOUT: OPT_NUMBER("--device-timeout", dev->timeout, 1, 60, 0);
case _O_DEVICE_ERROR_DELAY: OPT_NUMBER("--device-error-delay", dev->error_delay, 1, 60, 0);
case _O_BRIGHTNESS: OPT_CTL(brightness);
case _O_BRIGHTNESS_AUTO: OPT_CTL_AUTO(brightness);
case _O_CONTRAST: OPT_CTL(contrast);
case _O_SATURATION: OPT_CTL(saturation);
case _O_HUE: OPT_CTL(hue);
case _O_HUE_AUTO: OPT_CTL_AUTO(hue);
case _O_GAMMA: OPT_CTL(gamma);
case _O_SHARPNESS: OPT_CTL(sharpness);
case _O_BACKLIGHT_COMPENSATION: OPT_CTL(backlight_compensation);
case _O_WHITE_BALANCE: OPT_CTL(white_balance);
case _O_WHITE_BALANCE_AUTO: OPT_CTL_AUTO(white_balance);
case _O_GAIN: OPT_CTL(gain);
case _O_GAIN_AUTO: OPT_CTL_AUTO(gain);
case _O_IMAGE_DEFAULT:
OPT_CTL_DEFAULT_NOBREAK(brightness);
OPT_CTL_DEFAULT_NOBREAK(contrast);
OPT_CTL_DEFAULT_NOBREAK(saturation);
OPT_CTL_DEFAULT_NOBREAK(hue);
OPT_CTL_DEFAULT_NOBREAK(gamma);
OPT_CTL_DEFAULT_NOBREAK(sharpness);
OPT_CTL_DEFAULT_NOBREAK(backlight_compensation);
OPT_CTL_DEFAULT_NOBREAK(white_balance);
OPT_CTL_DEFAULT_NOBREAK(gain);
break;
case _O_BRIGHTNESS: OPT_CTL_AUTO(brightness);
case _O_CONTRAST: OPT_CTL_MANUAL(contrast);
case _O_SATURATION: OPT_CTL_MANUAL(saturation);
case _O_HUE: OPT_CTL_AUTO(hue);
case _O_GAMMA: OPT_CTL_MANUAL(gamma);
case _O_SHARPNESS: OPT_CTL_MANUAL(sharpness);
case _O_BACKLIGHT_COMPENSATION: OPT_CTL_MANUAL(backlight_compensation);
case _O_WHITE_BALANCE: OPT_CTL_AUTO(white_balance);
case _O_GAIN: OPT_CTL_AUTO(gain);
case _O_HOST: OPT_SET(server->host, optarg);
case _O_PORT: OPT_NUMBER("--port", server->port, 1, 65535, 0);
@@ -317,6 +375,7 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t
case _O_PASSWD: OPT_SET(server->passwd, optarg);
case _O_STATIC: OPT_SET(server->static_path, optarg);
case _O_BLANK: OPT_SET(server->blank_path, optarg);
case _O_LAST_AS_BLANK: OPT_NUMBER("--last-as-blank", server->last_as_blank, 0, 86400, 0);
case _O_DROP_SAME_FRAMES: OPT_NUMBER("--drop-same-frames", server->drop_same_frames, 0, VIDEO_MAX_FPS, 0);
case _O_SLOWDOWN: OPT_SET(server->slowdown, true);
case _O_FAKE_RESOLUTION: OPT_RESOLUTION("--fake-resolution", server->fake_width, server->fake_height, false);
@@ -329,6 +388,18 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t
case _O_GPIO_WORKERS_BUSY_AT: OPT_NUMBER("--gpio-workers-busy-at", gpio_pin_workers_busy_at, 0, 256, 0);
# endif
# ifdef HAS_PDEATHSIG
case _O_EXIT_ON_PARENT_DEATH:
if (process_track_parent_death() < 0) {
return -1;
};
break;
# endif
# ifdef WITH_SETPROCTITLE
case _O_PROCESS_NAME_PREFIX: OPT_SET(process_name_prefix, optarg);
# endif
case _O_NOTIFY_PARENT: OPT_SET(server->notify_parent, true);
case _O_LOG_LEVEL: OPT_NUMBER("--log-level", log_level, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, 0);
case _O_PERF: OPT_SET(log_level, LOG_LEVEL_PERF);
case _O_VERBOSE: OPT_SET(log_level, LOG_LEVEL_VERBOSE);
@@ -337,19 +408,24 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t
case _O_NO_LOG_COLORS: OPT_SET(log_colored, false);
case _O_HELP: _help(dev, encoder, server); return 1;
case _O_VERSION: _version(true); return 1;
case _O_VERSION: puts(VERSION); return 1;
case _O_FEATURES: _features(); return 1;
case 0: break;
default: _help(dev, encoder, server); return -1;
}
}
# undef OPT_CTL_AUTO
# undef OPT_CTL
# undef OPT_PARSE
# ifdef WITH_OMX
# undef OPT_GLITCHED_RESOLUTIONS
# ifdef WITH_SETPROCTITLE
if (process_name_prefix != NULL) {
process_set_name_prefix(options->argc, options->argv, process_name_prefix);
}
# endif
# undef OPT_CTL_AUTO
# undef OPT_CTL_MANUAL
# undef OPT_CTL_DEFAULT_NOBREAK
# undef OPT_PARSE
# undef OPT_RESOLUTION
# undef OPT_NUMBER
# undef OPT_SET
@@ -426,25 +502,42 @@ static int _parse_glitched_resolutions(const char *str, struct encoder_t *encode
}
#endif
static void _version(bool nl) {
printf(VERSION);
static void _features(void) {
# ifdef WITH_OMX
printf(" + OMX");
puts("+ WITH_OMX");
# else
puts("- WITH_OMX");
# endif
# ifdef WITH_GPIO
printf(" + GPIO");
puts("+ WITH_GPIO");
# else
puts("- WITH_GPIO");
# endif
# ifdef WITH_PTHREAD_NP
puts("+ WITH_PTHREAD_NP");
# else
puts("- WITH_PTHREAD_NP");
# endif
# ifdef WITH_SETPROCTITLE
puts("+ WITH_SETPROCTITLE");
# else
puts("- WITH_SETPROCTITLE");
# endif
# ifdef HAS_PDEATHSIG
puts("+ HAS_PDEATHSIG");
# else
puts("- HAS_PDEATHSIG");
# endif
if (nl) {
putchar('\n');
}
}
static void _help(struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server) {
printf("\nuStreamer - Lightweight and fast MJPG-HTTP streamer\n");
printf("═══════════════════════════════════════════════════\n\n");
printf("Version: ");
_version(false);
printf("; license: GPLv3\n");
printf("Version: %s; license: GPLv3\n", VERSION);
printf("Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com>\n\n");
printf("Capturing options:\n");
printf("══════════════════\n");
@@ -483,24 +576,22 @@ static void _help(struct device_t *dev, struct encoder_t *encoder, struct http_s
printf(" -g|--glitched-resolutions <WxH,...> ─ Comma-separated list of resolutions that require forced\n");
printf(" encoding on CPU instead of OMX. Default: disabled.\n\n");
# endif
printf(" --device-timeout <seconds> ────────── Timeout for device querying. Default: %u.\n\n", dev->timeout);
printf(" --device-error-delay <seconds> ────── Delay before trying to connect to the device again\n");
printf(" --device-timeout <sec> ────────────── Timeout for device querying. Default: %u.\n\n", dev->timeout);
printf(" --device-error-delay <sec> ────────── Delay before trying to connect to the device again\n");
printf(" after an error (timeout for example). Default: %u.\n\n", dev->error_delay);
printf("Image control options:\n");
printf("══════════════════════\n");
printf(" --brightness <N> ───────────── Set brightness. Default: no change.\n\n");
printf(" --brightness-auto ──────────── Enable automatic brightness control. Default: no change.\n\n");
printf(" --contrast <N> ─────────────── Set contrast. Default: no change.\n\n");
printf(" --saturation <N> ───────────── Set saturation. Default: no change.\n\n");
printf(" --hue <N> ──────────────────── Set hue. Default: no change.\n\n");
printf(" --hue-auto ─────────────────── Enable automatic hue control. Default: no change.\n\n");
printf(" --gamma <N> ────────────────── Set gamma. Default: no change.\n\n");
printf(" --sharpness <N> ────────────── Set sharpness. Default: no change.\n\n");
printf(" --backlight-compensation <N> ─ Set backlight compensation. Default: no change.\n\n");
printf(" --white-balance <N> ────────── Set white balance. Default: no change.\n\n");
printf(" --white-balance-auto ───────── Enable automatic white balance control. Default: no change.\n\n");
printf(" --gain <N> ─────────────────── Set gain. Default: no change.\n\n");
printf(" --gain-auto ────────────────── Enable automatic gain control. Default: no change.\n\n");
printf(" --image-default ────────────────────── Reset all image settings bellow to default. Default: no change.\n\n");
printf(" --brightness <N|auto|default> ──────── Set brightness. Default: no change.\n\n");
printf(" --contrast <N|default> ─────────────── Set contrast. Default: no change.\n\n");
printf(" --saturation <N|default> ───────────── Set saturation. Default: no change.\n\n");
printf(" --hue <N|auto|default> ─────────────── Set hue. Default: no change.\n\n");
printf(" --gamma <N|default> ─────────────────── Set gamma. Default: no change.\n\n");
printf(" --sharpness <N|default> ────────────── Set sharpness. Default: no change.\n\n");
printf(" --backlight-compensation <N|default> ─ Set backlight compensation. Default: no change.\n\n");
printf(" --white-balance <N|auto|default> ───── Set white balance. Default: no change.\n\n");
printf(" --gain <N|auto|default> ────────────── Set gain. Default: no change.\n\n");
printf(" Hint: use v4l2-ctl --list-ctrls-menus to query available controls of the device.\n\n");
printf("HTTP server options:\n");
printf("════════════════════\n");
printf(" -s|--host <address> ──────── Listen on Hostname or IP. Default: %s.\n\n", server->host);
@@ -512,16 +603,20 @@ static void _help(struct device_t *dev, struct encoder_t *encoder, struct http_s
printf(" --passwd <str> ───────────── HTTP basic auth passwd. Default: empty.\n\n");
printf(" --static <path> ───────────── Path to dir with static files instead of embedded root index page.\n");
printf(" Symlinks are not supported for security reasons. Default: disabled.\n\n");
printf(" -k|--blank <path> ────────── Path to JPEG file that will be shown when the device is disconnected\n");
printf(" -k|--blank <path> ────────── Path to JPEG file that will be shown when the device is disconnected\n");
printf(" during the streaming. Default: black screen 640x480 with 'NO SIGNAL'.\n\n");
printf(" -K|--last-as-blank <sec> ─── Show the last frame received from the camera after it was disconnected,\n");
printf(" but no more than specified time (or endlessly if 0 is specified).\n");
printf(" If the device has not yet been online, display 'NO SIGNAL' or the image\n");
printf(" specified by option --blank. Default: disabled.\n\n");
printf(" -e|--drop-same-frames <N> ── Don't send identical frames to clients, but no more than specified number.\n");
printf(" It can significantly reduce the outgoing traffic, but will increase\n");
printf(" the CPU loading. Don't use this option with analog signal sources\n");
printf(" or webcams, it's useless. Default: disabled.\n\n");
printf(" -l|--slowdown ────────────── Slowdown capturing to 1 FPS or less when no stream clients are connected.\n");
printf(" Useful to reduce CPU consumption. Default: disabled.\n\n");
printf(" -R|--fake-resolution <WxH> ─ Override image resolution for state. Default: disabled.\n\n");
printf(" --server-timeout <seconds> ─ Timeout for client connections. Default: %u.\n\n", server->timeout);
printf(" -R|--fake-resolution <WxH> ─ Override image resolution for the /state. Default: disabled.\n\n");
printf(" --server-timeout <sec> ───── Timeout for client connections. Default: %u.\n\n", server->timeout);
#ifdef WITH_GPIO
printf("GPIO options:\n");
printf("═════════════\n");
@@ -530,6 +625,19 @@ static void _help(struct device_t *dev, struct encoder_t *encoder, struct http_s
printf(" --gpio-has-http-clients <pin> ─ Set 1 while stream has at least one client. Default: disabled.\n\n");
printf(" --gpio-workers-busy-at <pin> ── Set 1 on (pin + N) while worker with number N has a job.\n");
printf(" The worker's numbering starts from 0. Default: disabled\n\n");
#endif
#if (defined(HAS_PDEATHSIG) || defined(WITH_SETPROCTITLE))
printf("Process options:\n");
printf("════════════════\n");
#endif
#ifdef HAS_PDEATHSIG
printf(" --exit-on-parent-death ─────── Exit the program if the parent process is dead. Default: disabled.\n\n");
#endif
#ifdef WITH_SETPROCTITLE
printf(" --process-name-prefix <str> ── Set process name prefix which will be displayed in the process list\n");
printf(" like '<str>: ustreamer --blah-blah-blah'. Default: disabled.\n\n");
printf(" --notify-parent ────────────── Send SIGUSR2 to the parent process when the stream parameters are changed.\n");
printf(" Checking changes is performed for the online flag and image resolution.\n\n");
#endif
printf("Logging options:\n");
printf("════════════════\n");
@@ -546,4 +654,5 @@ static void _help(struct device_t *dev, struct encoder_t *encoder, struct http_s
printf("═════════════\n");
printf(" -h|--help ─────── Print this text and exit.\n\n");
printf(" -v|--version ──── Print version and exit.\n\n");
printf(" --features ────── Print list of supporeted features.\n\n");
}

View File

@@ -27,4 +27,14 @@
#include "http/server.h"
int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server);
struct options_t {
int argc;
char **argv;
char **argv_copy;
};
struct options_t *options_init(int argc, char *argv[]);
void options_destroy(struct options_t *options);
int options_parse(struct options_t *options, struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server);

96
src/picture.c Normal file
View File

@@ -0,0 +1,96 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include "picture.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include "tools.h"
#include "logging.h"
struct picture_t *picture_init(void) {
struct picture_t *picture;
A_CALLOC(picture, 1);
return picture;
}
void picture_destroy(struct picture_t *picture) {
if (picture->data) {
free(picture->data);
}
free(picture);
}
size_t picture_get_generous_size(unsigned width, unsigned height) {
return ((width * height) << 1) * 2;
}
void picture_realloc_data(struct picture_t *picture, size_t size) {
if (picture->allocated < size) {
LOG_DEBUG("Increasing picture 0x%p buffer: %zu -> %zu (+%zu)",
picture, picture->allocated, size, size - picture->allocated);
A_REALLOC(picture->data, size);
picture->allocated = size;
}
}
void picture_set_data(struct picture_t *picture, const unsigned char *data, size_t size) {
picture_realloc_data(picture, size);
memcpy(picture->data, data, size);
picture->used = size;
}
void picture_append_data(struct picture_t *picture, const unsigned char *data, size_t size) {
size_t new_used = picture->used + size;
picture_realloc_data(picture, new_used);
memcpy(picture->data + picture->used, data, size);
picture->used = new_used;
}
void picture_copy(const struct picture_t *src, struct picture_t *dest) {
assert(src->allocated);
picture_set_data(dest, src->data, src->allocated);
# define COPY(_field) dest->_field = src->_field
COPY(used);
COPY(width);
COPY(height);
COPY(grab_ts);
COPY(encode_begin_ts);
COPY(encode_end_ts);
# undef COPY
}
bool picture_compare(const struct picture_t *a, const struct picture_t *b) {
return (a->used == b->used && !memcmp(a->data, b->data, b->used));
}

51
src/picture.h Normal file
View File

@@ -0,0 +1,51 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#pragma once
#include <stddef.h>
#include <stdbool.h>
struct picture_t {
unsigned char *data;
size_t used;
size_t allocated;
unsigned width;
unsigned height;
long double grab_ts;
long double encode_begin_ts;
long double encode_end_ts;
};
struct picture_t *picture_init(void);
void picture_destroy(struct picture_t *picture);
size_t picture_get_generous_size(unsigned width, unsigned height);
void picture_realloc_data(struct picture_t *picture, size_t size);
void picture_set_data(struct picture_t *picture, const unsigned char *data, size_t size);
void picture_append_data(struct picture_t *picture, const unsigned char *data, size_t size);
void picture_copy(const struct picture_t *src, struct picture_t *dest);
bool picture_compare(const struct picture_t *a, const struct picture_t *b);

140
src/process.h Normal file
View File

@@ -0,0 +1,140 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#pragma once
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#if defined(__linux__)
# define HAS_PDEATHSIG
#elif defined(__FreeBSD__)
# include <sys/param.h>
# if __FreeBSD_version >= 1102000
# define HAS_PDEATHSIG
# endif
#endif
#ifdef WITH_SETPROCTITLE
# include <stdlib.h>
# include <string.h>
# if defined(__linux__)
# include <bsd/unistd.h>
# elif (defined(__FreeBSD__) || defined(__DragonFly__))
//# include <unistd.h>
//# include <sys/types.h>
# elif (defined(__NetBSD__) || defined(__OpenBSD__)) // setproctitle() placed in stdlib.h
# else
# error setproctitle() not implemented, you can disable it using WITH_SETPROCTITLE=0
# endif
#endif
#ifdef HAS_PDEATHSIG
# if defined(__linux__)
# include <sys/prctl.h>
# elif defined(__FreeBSD__)
# include <sys/procctl.h>
# endif
#endif
#ifdef WITH_SETPROCTITLE
# include "tools.h"
#endif
#ifdef HAS_PDEATHSIG
# include "logging.h"
#endif
#ifdef WITH_SETPROCTITLE
extern char **environ;
#endif
#ifdef HAS_PDEATHSIG
INLINE int process_track_parent_death(void) {
pid_t parent = getppid();
int signum = SIGTERM;
# if defined(__linux__)
int retval = prctl(PR_SET_PDEATHSIG, signum);
# elif defined(__FreeBSD__)
int retval = procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signum);
# else
# error WTF?
# endif
if (retval < 0) {
LOG_PERROR("Can't set to receive SIGTERM on parent process death");
return -1;
}
if (kill(parent, 0) < 0) {
LOG_PERROR("The parent process %d is already dead", parent);
return -1;
}
return 0;
}
#endif
#ifdef WITH_SETPROCTITLE
# pragma GCC diagnostic ignored "-Wunused-parameter"
# pragma GCC diagnostic push
INLINE void process_set_name_prefix(int argc, char *argv[], const char *prefix) {
# pragma GCC diagnostic pop
char *cmdline = NULL;
size_t allocated = 2048;
size_t used = 0;
size_t arg_len = 0;
A_REALLOC(cmdline, allocated);
cmdline[0] = '\0';
for (int index = 0; index < argc; ++index) {
arg_len = strlen(argv[index]);
if (used + arg_len + 16 >= allocated) {
allocated += arg_len + 2048;
A_REALLOC(cmdline, allocated);
}
strcat(cmdline, " ");
strcat(cmdline, argv[index]);
used = strlen(cmdline); // Не считаем вручную, так надежнее
}
# ifdef __linux__
setproctitle_init(argc, argv, environ);
# endif
setproctitle("-%s:%s", prefix, cmdline);
free(cmdline);
}
#endif
INLINE void process_notify_parent(void) {
pid_t parent = getppid();
if (kill(parent, SIGUSR2) < 0) {
LOG_PERROR("Can't send SIGUSR2 to the parent process %d", parent);
}
}

View File

@@ -22,20 +22,19 @@
#include "stream.h"
#include <stdlib.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <assert.h>
#include <pthread.h>
#include <linux/videodev2.h>
#include "tools.h"
#include "threading.h"
#include "logging.h"
#include "xioctl.h"
#include "picture.h"
#include "device.h"
#include "encoder.h"
#ifdef WITH_GPIO
@@ -56,7 +55,7 @@ struct _worker_t {
atomic_bool has_job;
bool job_timely;
bool job_failed;
long double job_start_time;
long double job_start_ts;
pthread_cond_t has_job_cond;
pthread_mutex_t *free_workers_mutex;
@@ -111,6 +110,7 @@ struct stream_t *stream_init(struct device_t *dev, struct encoder_t *encoder) {
atomic_init(&proc->slowdown, false);
A_CALLOC(stream, 1);
stream->picture = picture_init();
stream->dev = dev;
stream->encoder = encoder;
atomic_init(&stream->updated, false);
@@ -121,6 +121,7 @@ struct stream_t *stream_init(struct device_t *dev, struct encoder_t *encoder) {
void stream_destroy(struct stream_t *stream) {
A_MUTEX_DESTROY(&stream->mutex);
picture_destroy(stream->picture);
free(stream->proc);
free(stream);
}
@@ -139,11 +140,11 @@ void stream_loop(struct stream_t *stream) {
long long captured_fps_second = 0;
bool persistent_timeout_reported = false;
LOG_DEBUG("Allocation memory for stream picture ...");
A_CALLOC(stream->picture.data, stream->dev->run->max_raw_image_size);
LOG_INFO("Capturing ...");
LOG_DEBUG("Pre-allocating memory for stream picture ...");
picture_realloc_data(stream->picture, picture_get_generous_size(stream->dev->run->width, stream->dev->run->height));
while (!atomic_load(&stream->proc->stop)) {
struct _worker_t *ready_worker;
@@ -278,10 +279,7 @@ void stream_loop(struct stream_t *stream) {
}
A_MUTEX_LOCK(&stream->mutex);
stream->picture.used = 0; // On stream offline
free(stream->picture.data);
stream->width = 0;
stream->height = 0;
stream->online = false;
atomic_store(&stream->updated, true);
A_MUTEX_UNLOCK(&stream->mutex);
@@ -338,27 +336,15 @@ static struct _workers_pool_t *_stream_init(struct stream_t *stream) {
}
static void _stream_expose_picture(struct stream_t *stream, unsigned buf_index, unsigned captured_fps) {
# define PICTURE(_next) stream->dev->run->pictures[buf_index]._next
A_MUTEX_LOCK(&stream->mutex);
stream->picture.used = PICTURE(used);
stream->picture.allocated = PICTURE(allocated);
picture_copy(stream->dev->run->pictures[buf_index], stream->picture);
memcpy(stream->picture.data, PICTURE(data), stream->picture.used);
stream->picture.grab_time = PICTURE(grab_time);
stream->picture.encode_begin_time = PICTURE(encode_begin_time);
stream->picture.encode_end_time = PICTURE(encode_end_time);
stream->width = stream->dev->run->width;
stream->height = stream->dev->run->height;
stream->online = true;
stream->captured_fps = captured_fps;
atomic_store(&stream->updated, true);
A_MUTEX_UNLOCK(&stream->mutex);
# undef PICTURE
}
static struct _workers_pool_t *_workers_pool_init(struct stream_t *stream) {
@@ -436,6 +422,7 @@ static void _workers_pool_destroy(struct _workers_pool_t *pool) {
static void *_worker_thread(void *v_worker) {
struct _worker_t *worker = (struct _worker_t *)v_worker;
A_THREAD_RENAME("worker-%u", worker->number);
LOG_DEBUG("Hello! I am a worker #%u ^_^", worker->number);
# ifdef WITH_GPIO
@@ -454,7 +441,7 @@ static void *_worker_thread(void *v_worker) {
A_MUTEX_UNLOCK(&worker->has_job_mutex);
if (!atomic_load(worker->workers_stop)) {
# define PICTURE(_next) worker->dev->run->pictures[worker->buf_index]._next
# define PICTURE(_next) worker->dev->run->pictures[worker->buf_index]->_next
LOG_DEBUG("Worker %u compressing JPEG from buffer %u ...", worker->number, worker->buf_index);
@@ -467,10 +454,10 @@ static void *_worker_thread(void *v_worker) {
}
if (device_release_buffer(worker->dev, worker->buf_index) == 0) {
worker->job_start_time = PICTURE(encode_begin_time);
worker->job_start_ts = PICTURE(encode_begin_ts);
atomic_store(&worker->has_job, false);
worker->last_comp_time = PICTURE(encode_end_time) - worker->job_start_time;
worker->last_comp_time = PICTURE(encode_end_ts) - worker->job_start_ts;
LOG_VERBOSE("Compressed new JPEG: size=%zu, time=%0.3Lf, worker=%u, buffer=%u",
PICTURE(used), worker->last_comp_time, worker->number, worker->buf_index);
@@ -511,7 +498,7 @@ static struct _worker_t *_workers_pool_wait(struct _workers_pool_t *pool) {
if (
!atomic_load(&pool->workers[number].has_job) && (
ready_worker == NULL
|| ready_worker->job_start_time < pool->workers[number].job_start_time
|| ready_worker->job_start_ts < pool->workers[number].job_start_ts
)
) {
ready_worker = &pool->workers[number];

View File

@@ -22,8 +22,12 @@
#pragma once
#include <stdbool.h>
#include <stdatomic.h>
#include <pthread.h>
#include "picture.h"
#include "device.h"
#include "encoder.h"
@@ -34,9 +38,8 @@ struct process_t {
};
struct stream_t {
struct picture_t picture;
unsigned width;
unsigned height;
struct picture_t *picture;
bool online;
unsigned captured_fps;
atomic_bool updated;
pthread_mutex_t mutex;

109
src/threading.h Normal file
View File

@@ -0,0 +1,109 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#pragma once
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>
#ifdef WITH_PTHREAD_NP
# if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
# include <pthread_np.h>
# include <sys/param.h>
# endif
#endif
#include "tools.h"
#ifdef PTHREAD_MAX_NAMELEN_NP
# define MAX_THREAD_NAME PTHREAD_MAX_NAMELEN_NP
#else
# define MAX_THREAD_NAME 16
#endif
#define A_THREAD_CREATE(_tid, _func, _arg) assert(!pthread_create(_tid, NULL, _func, _arg))
#define A_THREAD_JOIN(_tid) assert(!pthread_join(_tid, NULL))
#ifdef WITH_PTHREAD_NP
# define A_THREAD_RENAME(_fmt, ...) { \
char _buf[MAX_THREAD_NAME] = {0}; \
assert(snprintf(_buf, MAX_THREAD_NAME, _fmt, ##__VA_ARGS__) > 0); \
thread_set_name(_buf); \
}
#else
# define A_THREAD_RENAME(_fmt, ...)
#endif
#define A_MUTEX_INIT(_mutex) assert(!pthread_mutex_init(_mutex, NULL))
#define A_MUTEX_DESTROY(_mutex) assert(!pthread_mutex_destroy(_mutex))
#define A_MUTEX_LOCK(_mutex) assert(!pthread_mutex_lock(_mutex))
#define A_MUTEX_UNLOCK(_mutex) assert(!pthread_mutex_unlock(_mutex))
#define A_COND_INIT(_cond) assert(!pthread_cond_init(_cond, NULL))
#define A_COND_DESTROY(_cond) assert(!pthread_cond_destroy(_cond))
#define A_COND_SIGNAL(...) assert(!pthread_cond_signal(__VA_ARGS__))
#define A_COND_WAIT_TRUE(_var, _cond, _mutex) { while(!_var) assert(!pthread_cond_wait(_cond, _mutex)); }
#ifdef WITH_PTHREAD_NP
INLINE void thread_set_name(const char *name) {
# if defined(__linux__)
pthread_setname_np(pthread_self(), name);
# elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
pthread_set_name_np(pthread_self(), name);
# elif defined(__NetBSD__)
pthread_setname_np(pthread_self(), "%s", (void *)name);
# else
# error thread_set_name() not implemented, you can disable it using WITH_PTHREAD_NP=0
# endif
}
#endif
INLINE void thread_get_name(char *name) { // Always required for logging
#ifdef WITH_PTHREAD_NP
int retval = -1;
# if defined(__linux__) || defined (__NetBSD__)
retval = pthread_getname_np(pthread_self(), name, MAX_THREAD_NAME);
# elif \
(defined(__FreeBSD__) && defined(__FreeBSD_version) && __FreeBSD_version >= 1103500) \
|| (defined(__OpenBSD__) && defined(OpenBSD) && OpenBSD >= 201905) \
|| defined(__DragonFly__)
pthread_get_name_np(pthread_self(), name, MAX_THREAD_NAME);
if (name[0] != '\0') {
retval = 0;
}
# else
# error thread_get_name() not implemented, you can disable it using WITH_PTHREAD_NP=0
# endif
if (retval < 0) {
#endif
assert(snprintf(name, MAX_THREAD_NAME, "tid=%d", (pid_t)syscall(SYS_gettid)) > 0);
#ifdef WITH_PTHREAD_NP
}
#endif
}

View File

@@ -25,30 +25,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <time.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#define A_THREAD_CREATE(_tid, _func, _arg) assert(!pthread_create(_tid, NULL, _func, _arg))
#define A_THREAD_JOIN(_tid) assert(!pthread_join(_tid, NULL))
#define A_MUTEX_INIT(_mutex) assert(!pthread_mutex_init(_mutex, NULL))
#define A_MUTEX_DESTROY(_mutex) assert(!pthread_mutex_destroy(_mutex))
#define A_MUTEX_LOCK(_mutex) assert(!pthread_mutex_lock(_mutex))
#define A_MUTEX_UNLOCK(_mutex) assert(!pthread_mutex_unlock(_mutex))
#define A_COND_INIT(_cond) assert(!pthread_cond_init(_cond, NULL))
#define A_COND_DESTROY(_cond) assert(!pthread_cond_destroy(_cond))
#define A_COND_SIGNAL(...) assert(!pthread_cond_signal(__VA_ARGS__))
#define A_COND_WAIT_TRUE(_var, _cond, _mutex) { while(!_var) assert(!pthread_cond_wait(_cond, _mutex)); }
#define A_CALLOC(_dest, _nmemb) assert((_dest = calloc(_nmemb, sizeof(*(_dest)))))
#define A_REALLOC(_dest, _nmemb) assert((_dest = realloc(_dest, _nmemb * sizeof(*(_dest)))))
@@ -104,7 +85,3 @@ INLINE long double get_now_real(void) {
get_now(CLOCK_REALTIME, &sec, &msec);
return (long double)sec + ((long double)msec) / 1000;
}
INLINE pid_t get_thread_id(void) {
return syscall(SYS_gettid);
}