mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-02-28 04:36:33 +00:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c751e4ff08 | ||
|
|
99a00ca57c | ||
|
|
c009a7efe4 | ||
|
|
291d7431b0 | ||
|
|
a1cd490fdf | ||
|
|
7a85774085 | ||
|
|
2ed3c4815b | ||
|
|
724c6e118f | ||
|
|
da3a3adc65 | ||
|
|
32013a6360 | ||
|
|
16a2495766 | ||
|
|
05246706f0 | ||
|
|
9355055a7f | ||
|
|
9f16de13fe | ||
|
|
36a987fb9d | ||
|
|
652397f569 | ||
|
|
3333fc56a3 | ||
|
|
47378a17db | ||
|
|
5f320786f5 | ||
|
|
498c6e1f5d | ||
|
|
6c09adc689 | ||
|
|
8bf7ac3005 | ||
|
|
6aebd7167e | ||
|
|
3f03575222 | ||
|
|
9ca43f17a3 | ||
|
|
66ef566c9a | ||
|
|
07ecc5b0a0 | ||
|
|
8d19711f2c |
@@ -1,7 +1,7 @@
|
|||||||
[bumpversion]
|
[bumpversion]
|
||||||
commit = True
|
commit = True
|
||||||
tag = True
|
tag = True
|
||||||
current_version = 0.76
|
current_version = 0.79
|
||||||
parse = (?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\d+)(\-(?P<release>[a-z]+))?)?
|
parse = (?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\d+)(\-(?P<release>[a-z]+))?)?
|
||||||
serialize =
|
serialize =
|
||||||
{major}.{minor}
|
{major}.{minor}
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,6 +2,7 @@
|
|||||||
/pkg/arch/src/
|
/pkg/arch/src/
|
||||||
/pkg/arch/v*.tar.gz
|
/pkg/arch/v*.tar.gz
|
||||||
/pkg/arch/ustreamer-*.pkg.tar.xz
|
/pkg/arch/ustreamer-*.pkg.tar.xz
|
||||||
|
/config.mk
|
||||||
/vgcore.*
|
/vgcore.*
|
||||||
/ustreamer
|
/ustreamer
|
||||||
/*.sock
|
/*.sock
|
||||||
|
|||||||
17
Makefile
17
Makefile
@@ -1,3 +1,5 @@
|
|||||||
|
-include config.mk
|
||||||
|
|
||||||
PROG ?= ustreamer
|
PROG ?= ustreamer
|
||||||
DESTDIR ?=
|
DESTDIR ?=
|
||||||
PREFIX ?= /usr/local
|
PREFIX ?= /usr/local
|
||||||
@@ -15,14 +17,25 @@ LIBS = -lm -ljpeg -pthread -levent -levent_pthreads -luuid
|
|||||||
override CFLAGS += -c -std=c11 -Wall -Wextra -D_GNU_SOURCE
|
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)
|
SOURCES = $(shell ls src/*.c src/http/*.c src/encoders/cpu/*.c src/encoders/hw/*.c)
|
||||||
|
|
||||||
ifeq ($(WITH_OMX_ENCODER),)
|
|
||||||
else
|
define optbool
|
||||||
|
$(filter $(shell echo $(1) | tr A-Z a-z), yes on 1)
|
||||||
|
endef
|
||||||
|
|
||||||
|
|
||||||
|
ifneq ($(call optbool,$(WITH_OMX_ENCODER)),)
|
||||||
LIBS += -lbcm_host -lvcos -lopenmaxil -L$(RPI_VC_LIBS)
|
LIBS += -lbcm_host -lvcos -lopenmaxil -L$(RPI_VC_LIBS)
|
||||||
override CFLAGS += -DWITH_OMX_ENCODER -DOMX_SKIP64BIT -I$(RPI_VC_HEADERS)
|
override CFLAGS += -DWITH_OMX_ENCODER -DOMX_SKIP64BIT -I$(RPI_VC_HEADERS)
|
||||||
SOURCES += $(shell ls src/encoders/omx/*.c)
|
SOURCES += $(shell ls src/encoders/omx/*.c)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
ifneq ($(call optbool,$(WITH_WORKERS_GPIO_DEBUG)),)
|
||||||
|
LIBS += -lwiringPi
|
||||||
|
override CFLAGS += -DWITH_WORKERS_GPIO_DEBUG
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
all: $(SOURCES) $(PROG)
|
all: $(SOURCES) $(PROG)
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
|
|
||||||
pkgname=ustreamer
|
pkgname=ustreamer
|
||||||
pkgver=0.76
|
pkgver=0.79
|
||||||
pkgrel=1
|
pkgrel=1
|
||||||
pkgdesc="Lightweight and fast MJPG-HTTP streamer"
|
pkgdesc="Lightweight and fast MJPG-HTTP streamer"
|
||||||
url="https://github.com/pi-kvm/ustreamer"
|
url="https://github.com/pi-kvm/ustreamer"
|
||||||
@@ -12,7 +12,7 @@ arch=(i686 x86_64 armv6h armv7h)
|
|||||||
depends=(libjpeg libevent libutil-linux)
|
depends=(libjpeg libevent libutil-linux)
|
||||||
# optional: raspberrypi-firmware for OMX JPEG encoder
|
# optional: raspberrypi-firmware for OMX JPEG encoder
|
||||||
makedepends=(gcc make)
|
makedepends=(gcc make)
|
||||||
source=(${pkgname}::"git+https://github.com/pi-kvm/ustreamer#commit=${pkgver}")
|
source=(${pkgname}::"git+https://github.com/pi-kvm/ustreamer#commit=v${pkgver}")
|
||||||
md5sums=(SKIP)
|
md5sums=(SKIP)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
include $(TOPDIR)/rules.mk
|
include $(TOPDIR)/rules.mk
|
||||||
|
|
||||||
PKG_NAME:=ustreamer
|
PKG_NAME:=ustreamer
|
||||||
PKG_VERSION:=0.76
|
PKG_VERSION:=0.79
|
||||||
PKG_RELEASE:=1
|
PKG_RELEASE:=1
|
||||||
PKG_MAINTAINER:=Maxim Devaev <mdevaev@gmail.com>
|
PKG_MAINTAINER:=Maxim Devaev <mdevaev@gmail.com>
|
||||||
|
|
||||||
|
|||||||
@@ -23,5 +23,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef VERSION
|
#ifndef VERSION
|
||||||
# define VERSION "0.76"
|
# define VERSION "0.79"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
64
src/device.c
64
src/device.c
@@ -20,6 +20,8 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "device.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@@ -30,13 +32,13 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <sys/select.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <linux/videodev2.h>
|
#include <linux/videodev2.h>
|
||||||
|
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "xioctl.h"
|
#include "xioctl.h"
|
||||||
#include "device.h"
|
|
||||||
|
|
||||||
|
|
||||||
static const struct {
|
static const struct {
|
||||||
@@ -81,7 +83,7 @@ static const char *_format_to_string_supported(unsigned format);
|
|||||||
static const char *_standard_to_string(v4l2_std_id standard);
|
static const char *_standard_to_string(v4l2_std_id standard);
|
||||||
|
|
||||||
|
|
||||||
struct device_t *device_init() {
|
struct device_t *device_init(void) {
|
||||||
struct controls_t *ctl;
|
struct controls_t *ctl;
|
||||||
struct device_runtime_t *run;
|
struct device_runtime_t *run;
|
||||||
struct device_t *dev;
|
struct device_t *dev;
|
||||||
@@ -232,6 +234,39 @@ int device_switch_capturing(struct device_t *dev, bool enable) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int device_select(struct device_t *dev, bool *has_read, bool *has_write, bool *has_error) {
|
||||||
|
struct timeval timeout;
|
||||||
|
int retval;
|
||||||
|
|
||||||
|
# define INIT_FD_SET(_set) \
|
||||||
|
fd_set _set; FD_ZERO(&_set); FD_SET(dev->run->fd, &_set);
|
||||||
|
|
||||||
|
INIT_FD_SET(read_fds);
|
||||||
|
INIT_FD_SET(write_fds);
|
||||||
|
INIT_FD_SET(error_fds);
|
||||||
|
|
||||||
|
# undef INIT_FD_SET
|
||||||
|
|
||||||
|
timeout.tv_sec = dev->timeout;
|
||||||
|
timeout.tv_usec = 0;
|
||||||
|
|
||||||
|
LOG_DEBUG("Calling select() on video device ...");
|
||||||
|
|
||||||
|
retval = select(dev->run->fd + 1, &read_fds, &write_fds, &error_fds, &timeout);
|
||||||
|
if (retval > 0) {
|
||||||
|
*has_read = FD_ISSET(dev->run->fd, &read_fds);
|
||||||
|
*has_write = FD_ISSET(dev->run->fd, &write_fds);
|
||||||
|
*has_error = FD_ISSET(dev->run->fd, &error_fds);
|
||||||
|
} else {
|
||||||
|
has_read = false;
|
||||||
|
has_write = false;
|
||||||
|
has_error = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_DEBUG("Device select() --> %d", retval);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
int device_grab_buffer(struct device_t *dev) {
|
int device_grab_buffer(struct device_t *dev) {
|
||||||
struct v4l2_buffer buf_info;
|
struct v4l2_buffer buf_info;
|
||||||
|
|
||||||
@@ -245,14 +280,15 @@ int device_grab_buffer(struct device_t *dev) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_DEBUG("Got a new frame in buffer index=%u; bytesused=%u", buf_info.index, buf_info.bytesused);
|
LOG_DEBUG("Got a new frame in buffer: index=%u, bytesused=%u", buf_info.index, buf_info.bytesused);
|
||||||
if (buf_info.index >= dev->run->n_buffers) {
|
if (buf_info.index >= dev->run->n_buffers) {
|
||||||
LOG_ERROR("Got invalid buffer index=%u; nbuffers=%u", buf_info.index, dev->run->n_buffers);
|
LOG_ERROR("Got invalid buffer: index=%u, nbuffers=%u", buf_info.index, dev->run->n_buffers);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
dev->run->hw_buffers[buf_info.index].used = buf_info.bytesused;
|
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));
|
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();
|
||||||
return buf_info.index;
|
return buf_info.index;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,24 +385,22 @@ static int _device_open_dv_timings(struct device_t *dev) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int _device_apply_dv_timings(struct device_t *dev) {
|
static int _device_apply_dv_timings(struct device_t *dev) {
|
||||||
struct v4l2_dv_timings dv_timings;
|
struct v4l2_dv_timings dv;
|
||||||
|
|
||||||
MEMSET_ZERO(dv_timings);
|
MEMSET_ZERO(dv);
|
||||||
|
|
||||||
LOG_DEBUG("Calling ioctl(VIDIOC_QUERY_DV_TIMINGS) ...");
|
LOG_DEBUG("Calling ioctl(VIDIOC_QUERY_DV_TIMINGS) ...");
|
||||||
if (xioctl(dev->run->fd, VIDIOC_QUERY_DV_TIMINGS, &dv_timings) == 0) {
|
if (xioctl(dev->run->fd, VIDIOC_QUERY_DV_TIMINGS, &dv) == 0) {
|
||||||
LOG_INFO("Got new DV timings: resolution=%ux%u; pixclk=%llu",
|
LOG_INFO("Got new DV timings: resolution=%ux%u, pixclk=%llu",
|
||||||
dv_timings.bt.width,
|
dv.bt.width, dv.bt.height, dv.bt.pixelclock);
|
||||||
dv_timings.bt.height,
|
|
||||||
dv_timings.bt.pixelclock);
|
|
||||||
|
|
||||||
LOG_DEBUG("Calling ioctl(VIDIOC_S_DV_TIMINGS) ...");
|
LOG_DEBUG("Calling ioctl(VIDIOC_S_DV_TIMINGS) ...");
|
||||||
if (xioctl(dev->run->fd, VIDIOC_S_DV_TIMINGS, &dv_timings) < 0) {
|
if (xioctl(dev->run->fd, VIDIOC_S_DV_TIMINGS, &dv) < 0) {
|
||||||
LOG_PERROR("Failed to set DV timings");
|
LOG_PERROR("Failed to set DV timings");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_device_apply_resolution(dev, dv_timings.bt.width, dv_timings.bt.height) < 0) {
|
if (_device_apply_resolution(dev, dv.bt.width, dv.bt.height) < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -396,7 +430,7 @@ static int _device_open_format(struct device_t *dev) {
|
|||||||
// Set format
|
// Set format
|
||||||
LOG_DEBUG("Calling ioctl(VIDIOC_S_FMT) ...");
|
LOG_DEBUG("Calling ioctl(VIDIOC_S_FMT) ...");
|
||||||
if (xioctl(dev->run->fd, VIDIOC_S_FMT, &fmt) < 0) {
|
if (xioctl(dev->run->fd, VIDIOC_S_FMT, &fmt) < 0) {
|
||||||
LOG_PERROR("Unable to set pixelformat=%s; resolution=%ux%u",
|
LOG_PERROR("Unable to set pixelformat=%s, resolution=%ux%u",
|
||||||
_format_to_string_supported(dev->format),
|
_format_to_string_supported(dev->format),
|
||||||
dev->run->width,
|
dev->run->width,
|
||||||
dev->run->height);
|
dev->run->height);
|
||||||
@@ -567,7 +601,7 @@ static int _device_apply_resolution(struct device_t *dev, unsigned width, unsign
|
|||||||
width == 0 || width > VIDEO_MAX_WIDTH
|
width == 0 || width > VIDEO_MAX_WIDTH
|
||||||
|| height == 0 || height > VIDEO_MAX_HEIGHT
|
|| height == 0 || height > VIDEO_MAX_HEIGHT
|
||||||
) {
|
) {
|
||||||
LOG_ERROR("Requested forbidden resolution=%ux%u: min=1x1; max=%ux%u",
|
LOG_ERROR("Requested forbidden resolution=%ux%u: min=1x1, max=%ux%u",
|
||||||
width, height, VIDEO_MAX_WIDTH, VIDEO_MAX_HEIGHT);
|
width, height, VIDEO_MAX_WIDTH, VIDEO_MAX_HEIGHT);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ struct device_t {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct device_t *device_init();
|
struct device_t *device_init(void);
|
||||||
void device_destroy(struct device_t *dev);
|
void device_destroy(struct device_t *dev);
|
||||||
|
|
||||||
int device_parse_format(const char *str);
|
int device_parse_format(const char *str);
|
||||||
@@ -120,6 +120,7 @@ int device_open(struct device_t *dev);
|
|||||||
void device_close(struct device_t *dev);
|
void device_close(struct device_t *dev);
|
||||||
|
|
||||||
int device_switch_capturing(struct device_t *dev, bool enable);
|
int device_switch_capturing(struct device_t *dev, bool enable);
|
||||||
|
int device_select(struct device_t *dev, bool *has_read, bool *has_write, bool *has_error);
|
||||||
int device_grab_buffer(struct device_t *dev);
|
int device_grab_buffer(struct device_t *dev);
|
||||||
int device_release_buffer(struct device_t *dev, unsigned index);
|
int device_release_buffer(struct device_t *dev, unsigned index);
|
||||||
int device_consume_event(struct device_t *dev);
|
int device_consume_event(struct device_t *dev);
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "encoder.h"
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@@ -29,7 +31,6 @@
|
|||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
#include "encoder.h"
|
|
||||||
|
|
||||||
#include "encoders/cpu/encoder.h"
|
#include "encoders/cpu/encoder.h"
|
||||||
#include "encoders/hw/encoder.h"
|
#include "encoders/hw/encoder.h"
|
||||||
@@ -51,7 +52,7 @@ static const struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct encoder_t *encoder_init() {
|
struct encoder_t *encoder_init(void) {
|
||||||
struct encoder_runtime_t *run;
|
struct encoder_runtime_t *run;
|
||||||
struct encoder_t *encoder;
|
struct encoder_t *encoder;
|
||||||
|
|
||||||
@@ -188,6 +189,8 @@ int encoder_compress_buffer(struct encoder_t *encoder, struct device_t *dev, uns
|
|||||||
|
|
||||||
assert(encoder->run->type != ENCODER_TYPE_UNKNOWN);
|
assert(encoder->run->type != ENCODER_TYPE_UNKNOWN);
|
||||||
|
|
||||||
|
dev->run->pictures[buf_index].encode_begin_time = get_now_monotonic();
|
||||||
|
|
||||||
if (encoder->run->type == ENCODER_TYPE_CPU) {
|
if (encoder->run->type == ENCODER_TYPE_CPU) {
|
||||||
cpu_encoder_compress_buffer(dev, buf_index, encoder->run->quality);
|
cpu_encoder_compress_buffer(dev, buf_index, encoder->run->quality);
|
||||||
} else if (encoder->run->type == ENCODER_TYPE_HW) {
|
} else if (encoder->run->type == ENCODER_TYPE_HW) {
|
||||||
@@ -201,6 +204,8 @@ int encoder_compress_buffer(struct encoder_t *encoder, struct device_t *dev, uns
|
|||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
|
dev->run->pictures[buf_index].encode_end_time = get_now_monotonic();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
# pragma GCC diagnostic ignored "-Wunused-label"
|
# pragma GCC diagnostic ignored "-Wunused-label"
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ struct encoder_t {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct encoder_t *encoder_init();
|
struct encoder_t *encoder_init(void);
|
||||||
void encoder_destroy(struct encoder_t *encoder);
|
void encoder_destroy(struct encoder_t *encoder);
|
||||||
|
|
||||||
enum encoder_type_t encoder_parse_type(const char *str);
|
enum encoder_type_t encoder_parse_type(const char *str);
|
||||||
|
|||||||
@@ -25,6 +25,8 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "encoder.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -36,8 +38,6 @@
|
|||||||
#include "../../tools.h"
|
#include "../../tools.h"
|
||||||
#include "../../device.h"
|
#include "../../device.h"
|
||||||
|
|
||||||
#include "encoder.h"
|
|
||||||
|
|
||||||
|
|
||||||
struct _jpeg_dest_manager_t {
|
struct _jpeg_dest_manager_t {
|
||||||
struct jpeg_destination_mgr mgr; // Default manager
|
struct jpeg_destination_mgr mgr; // Default manager
|
||||||
|
|||||||
@@ -25,6 +25,8 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "encoder.h"
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@@ -37,7 +39,6 @@
|
|||||||
#include "../../device.h"
|
#include "../../device.h"
|
||||||
|
|
||||||
#include "huffman.h"
|
#include "huffman.h"
|
||||||
#include "encoder.h"
|
|
||||||
|
|
||||||
|
|
||||||
static bool _is_huffman(const unsigned char *data);
|
static bool _is_huffman(const unsigned char *data);
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "component.h"
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <IL/OMX_Core.h>
|
#include <IL/OMX_Core.h>
|
||||||
@@ -28,7 +30,6 @@
|
|||||||
#include "../../logging.h"
|
#include "../../logging.h"
|
||||||
|
|
||||||
#include "formatters.h"
|
#include "formatters.h"
|
||||||
#include "component.h"
|
|
||||||
|
|
||||||
|
|
||||||
static int _component_wait_port_changed(OMX_HANDLETYPE *component, OMX_U32 port, OMX_BOOL enabled);
|
static int _component_wait_port_changed(OMX_HANDLETYPE *component, OMX_U32 port, OMX_BOOL enabled);
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "encoder.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -39,7 +41,6 @@
|
|||||||
|
|
||||||
#include "formatters.h"
|
#include "formatters.h"
|
||||||
#include "component.h"
|
#include "component.h"
|
||||||
#include "encoder.h"
|
|
||||||
|
|
||||||
|
|
||||||
static const OMX_U32 _INPUT_PORT = 340;
|
static const OMX_U32 _INPUT_PORT = 340;
|
||||||
@@ -69,7 +70,7 @@ static OMX_ERRORTYPE _omx_output_available_handler(
|
|||||||
OMX_PTR v_omx, UNUSED OMX_BUFFERHEADERTYPE *buffer);
|
OMX_PTR v_omx, UNUSED OMX_BUFFERHEADERTYPE *buffer);
|
||||||
|
|
||||||
|
|
||||||
struct omx_encoder_t *omx_encoder_init() {
|
struct omx_encoder_t *omx_encoder_init(void) {
|
||||||
// Some theory:
|
// Some theory:
|
||||||
// - http://www.fourcc.org/yuv.php
|
// - http://www.fourcc.org/yuv.php
|
||||||
// - https://kwasi-ich.de/blog/2017/11/26/omx/
|
// - https://kwasi-ich.de/blog/2017/11/26/omx/
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ struct omx_encoder_t {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct omx_encoder_t *omx_encoder_init();
|
struct omx_encoder_t *omx_encoder_init(void);
|
||||||
void omx_encoder_destroy(struct omx_encoder_t *omx);
|
void omx_encoder_destroy(struct omx_encoder_t *omx);
|
||||||
|
|
||||||
int omx_encoder_prepare(struct omx_encoder_t *omx, struct device_t *dev, unsigned quality);
|
int omx_encoder_prepare(struct omx_encoder_t *omx, struct device_t *dev, unsigned quality);
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "formatters.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
@@ -27,7 +29,6 @@
|
|||||||
#include <IL/OMX_Core.h>
|
#include <IL/OMX_Core.h>
|
||||||
|
|
||||||
#include "../../tools.h"
|
#include "../../tools.h"
|
||||||
#include "formatters.h"
|
|
||||||
|
|
||||||
|
|
||||||
#define CASE_TO_STRING(_value) \
|
#define CASE_TO_STRING(_value) \
|
||||||
|
|||||||
@@ -20,12 +20,13 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "base64.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include "../tools.h"
|
#include "../tools.h"
|
||||||
#include "base64.h"
|
|
||||||
|
|
||||||
|
|
||||||
static const char ENCODING_TABLE[] = {
|
static const char ENCODING_TABLE[] = {
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "blank.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -32,8 +34,6 @@
|
|||||||
|
|
||||||
#include "data/blank_jpeg.h"
|
#include "data/blank_jpeg.h"
|
||||||
|
|
||||||
#include "blank.h"
|
|
||||||
|
|
||||||
|
|
||||||
struct _jpeg_error_manager_t {
|
struct _jpeg_error_manager_t {
|
||||||
struct jpeg_error_mgr mgr; // Default manager
|
struct jpeg_error_mgr mgr; // Default manager
|
||||||
@@ -41,7 +41,7 @@ struct _jpeg_error_manager_t {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static struct blank_t *_blank_init_internal();
|
static struct blank_t *_blank_init_internal(void);
|
||||||
static struct blank_t *_blank_init_external(const char *path);
|
static struct blank_t *_blank_init_external(const char *path);
|
||||||
static int _jpeg_read_geometry(FILE *fp, unsigned *width, unsigned *height);
|
static int _jpeg_read_geometry(FILE *fp, unsigned *width, unsigned *height);
|
||||||
static void _jpeg_error_handler(j_common_ptr jpeg);
|
static void _jpeg_error_handler(j_common_ptr jpeg);
|
||||||
@@ -68,7 +68,7 @@ void blank_destroy(struct blank_t *blank) {
|
|||||||
free(blank);
|
free(blank);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct blank_t *_blank_init_internal() {
|
static struct blank_t *_blank_init_internal(void) {
|
||||||
struct blank_t *blank;
|
struct blank_t *blank;
|
||||||
|
|
||||||
A_CALLOC(blank, 1);
|
A_CALLOC(blank, 1);
|
||||||
|
|||||||
@@ -20,14 +20,14 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "mime.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <event2/util.h>
|
#include <event2/util.h>
|
||||||
|
|
||||||
#include "../tools.h"
|
#include "../tools.h"
|
||||||
|
|
||||||
#include "mime.h"
|
|
||||||
|
|
||||||
|
|
||||||
static const struct {
|
static const struct {
|
||||||
const char *ext;
|
const char *ext;
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "path.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -27,8 +29,6 @@
|
|||||||
|
|
||||||
#include "../tools.h"
|
#include "../tools.h"
|
||||||
|
|
||||||
#include "path.h"
|
|
||||||
|
|
||||||
|
|
||||||
char *simplify_request_path(const char *str) {
|
char *simplify_request_path(const char *str) {
|
||||||
// Based on Lighttpd sources:
|
// Based on Lighttpd sources:
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "server.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdatomic.h>
|
#include <stdatomic.h>
|
||||||
@@ -56,7 +58,6 @@
|
|||||||
#include "base64.h"
|
#include "base64.h"
|
||||||
#include "mime.h"
|
#include "mime.h"
|
||||||
#include "static.h"
|
#include "static.h"
|
||||||
#include "server.h"
|
|
||||||
|
|
||||||
#include "data/index_html.h"
|
#include "data/index_html.h"
|
||||||
|
|
||||||
@@ -77,7 +78,7 @@ static void _http_callback_stream_error(struct bufferevent *buf_event, short wha
|
|||||||
static void _http_exposed_refresh(int fd, short event, void *v_server);
|
static void _http_exposed_refresh(int fd, short event, void *v_server);
|
||||||
static void _http_queue_send_stream(struct http_server_t *server, bool stream_updated, bool picture_updated);
|
static void _http_queue_send_stream(struct http_server_t *server, bool stream_updated, bool picture_updated);
|
||||||
|
|
||||||
static bool _expose_new_picture(struct http_server_t *server);
|
static bool _expose_new_picture_unsafe(struct http_server_t *server);
|
||||||
static bool _expose_blank_picture(struct http_server_t *server);
|
static bool _expose_blank_picture(struct http_server_t *server);
|
||||||
|
|
||||||
|
|
||||||
@@ -208,7 +209,7 @@ int http_server_listen(struct http_server_t *server) {
|
|||||||
# define MAX_SUN_PATH (sizeof(unix_addr.sun_path) - 1)
|
# define MAX_SUN_PATH (sizeof(unix_addr.sun_path) - 1)
|
||||||
|
|
||||||
if (strlen(server->unix_path) > MAX_SUN_PATH) {
|
if (strlen(server->unix_path) > MAX_SUN_PATH) {
|
||||||
LOG_ERROR("UNIX socket path is too long, max=%zu", MAX_SUN_PATH);
|
LOG_ERROR("UNIX socket path is too long; max=%zu", MAX_SUN_PATH);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -568,7 +569,7 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server
|
|||||||
}
|
}
|
||||||
|
|
||||||
evhttp_connection_get_peer(conn, &client_addr, &client_port);
|
evhttp_connection_get_peer(conn, &client_addr, &client_port);
|
||||||
LOG_INFO("HTTP: Registered the new stream client: [%s]:%u; id=%s; clients now: %u",
|
LOG_INFO("HTTP: Registered the new stream client: [%s]:%u, id=%s; clients now: %u",
|
||||||
client_addr, client_port, client->id, server->run->stream_clients_count);
|
client_addr, client_port, client->id, server->run->stream_clients_count);
|
||||||
|
|
||||||
buf_event = evhttp_connection_get_bufferevent(conn);
|
buf_event = evhttp_connection_get_bufferevent(conn);
|
||||||
@@ -807,7 +808,7 @@ static void _http_exposed_refresh(UNUSED int fd, UNUSED short what, void *v_serv
|
|||||||
LOG_DEBUG("Refreshing HTTP exposed ...");
|
LOG_DEBUG("Refreshing HTTP exposed ...");
|
||||||
A_MUTEX_LOCK(&server->run->stream->mutex);
|
A_MUTEX_LOCK(&server->run->stream->mutex);
|
||||||
if (server->run->stream->picture.used > 0) { // If online
|
if (server->run->stream->picture.used > 0) { // If online
|
||||||
picture_updated = _expose_new_picture(server);
|
picture_updated = _expose_new_picture_unsafe(server);
|
||||||
UNLOCK_STREAM;
|
UNLOCK_STREAM;
|
||||||
} else {
|
} else {
|
||||||
UNLOCK_STREAM;
|
UNLOCK_STREAM;
|
||||||
@@ -825,7 +826,7 @@ static void _http_exposed_refresh(UNUSED int fd, UNUSED short what, void *v_serv
|
|||||||
_http_queue_send_stream(server, stream_updated, picture_updated);
|
_http_queue_send_stream(server, stream_updated, picture_updated);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool _expose_new_picture(struct http_server_t *server) {
|
static bool _expose_new_picture_unsafe(struct http_server_t *server) {
|
||||||
# define STREAM(_next) server->run->stream->_next
|
# define STREAM(_next) server->run->stream->_next
|
||||||
# define EXPOSED(_next) server->run->exposed->_next
|
# define EXPOSED(_next) server->run->exposed->_next
|
||||||
|
|
||||||
@@ -845,13 +846,13 @@ static bool _expose_new_picture(struct http_server_t *server) {
|
|||||||
) {
|
) {
|
||||||
EXPOSED(expose_cmp_time) = get_now_monotonic();
|
EXPOSED(expose_cmp_time) = get_now_monotonic();
|
||||||
EXPOSED(expose_end_time) = EXPOSED(expose_cmp_time);
|
EXPOSED(expose_end_time) = EXPOSED(expose_cmp_time);
|
||||||
LOG_VERBOSE("HTTP: dropped same frame number %u; comparsion time = %.06Lf",
|
LOG_VERBOSE("HTTP: dropped same frame number %u; cmp_time=%.06Lf",
|
||||||
EXPOSED(dropped), EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time));
|
EXPOSED(dropped), EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time));
|
||||||
EXPOSED(dropped) += 1;
|
EXPOSED(dropped) += 1;
|
||||||
return false; // Not updated
|
return false; // Not updated
|
||||||
} else {
|
} else {
|
||||||
EXPOSED(expose_cmp_time) = get_now_monotonic();
|
EXPOSED(expose_cmp_time) = get_now_monotonic();
|
||||||
LOG_VERBOSE("HTTP: passed same frame check (frames are differ); comparsion time = %.06Lf",
|
LOG_VERBOSE("HTTP: passed same frame check (frames are differ); cmp_time=%.06Lf",
|
||||||
EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time));
|
EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "static.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -32,7 +34,6 @@
|
|||||||
#include "../logging.h"
|
#include "../logging.h"
|
||||||
|
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
#include "static.h"
|
|
||||||
|
|
||||||
|
|
||||||
char *find_static_file_path(const char *root_path, const char *request_path) {
|
char *find_static_file_path(const char *root_path, const char *request_path) {
|
||||||
|
|||||||
19
src/main.c
19
src/main.c
@@ -32,6 +32,10 @@
|
|||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#ifdef WITH_WORKERS_GPIO_DEBUG
|
||||||
|
# include <wiringPi.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
@@ -199,7 +203,7 @@ static int _parse_options(int argc, char *argv[], struct device_t *dev, struct e
|
|||||||
# define OPT_UNSIGNED(_dest, _name, _min, _max) { \
|
# define OPT_UNSIGNED(_dest, _name, _min, _max) { \
|
||||||
errno = 0; char *_end = NULL; int _tmp = strtol(optarg, &_end, 0); \
|
errno = 0; char *_end = NULL; int _tmp = strtol(optarg, &_end, 0); \
|
||||||
if (errno || *_end || _tmp < _min || _tmp > _max) { \
|
if (errno || *_end || _tmp < _min || _tmp > _max) { \
|
||||||
printf("Invalid value for '%s=%s'; min=%u; max=%u\n", _name, optarg, _min, _max); \
|
printf("Invalid value for '%s=%s': min=%u, max=%u\n", _name, optarg, _min, _max); \
|
||||||
return -1; \
|
return -1; \
|
||||||
} \
|
} \
|
||||||
_dest = _tmp; \
|
_dest = _tmp; \
|
||||||
@@ -323,7 +327,7 @@ struct main_context_t {
|
|||||||
|
|
||||||
static struct main_context_t *_ctx;
|
static struct main_context_t *_ctx;
|
||||||
|
|
||||||
static void _block_thread_signals() {
|
static void _block_thread_signals(void) {
|
||||||
sigset_t mask;
|
sigset_t mask;
|
||||||
assert(!sigemptyset(&mask));
|
assert(!sigemptyset(&mask));
|
||||||
assert(!sigaddset(&mask, SIGINT));
|
assert(!sigaddset(&mask, SIGINT));
|
||||||
@@ -349,7 +353,7 @@ static void _signal_handler(int signum) {
|
|||||||
http_server_loop_break(_ctx->server);
|
http_server_loop_break(_ctx->server);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _install_signal_handlers() {
|
static void _install_signal_handlers(void) {
|
||||||
struct sigaction sig_act;
|
struct sigaction sig_act;
|
||||||
|
|
||||||
MEMSET_ZERO(sig_act);
|
MEMSET_ZERO(sig_act);
|
||||||
@@ -377,6 +381,15 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
LOGGING_INIT;
|
LOGGING_INIT;
|
||||||
|
|
||||||
|
# ifdef WITH_WORKERS_GPIO_DEBUG
|
||||||
|
if (wiringPiSetupGpio() < 0) {
|
||||||
|
LOG_PERROR("Can't initialize wiringPi GPIO");
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
LOG_INFO("Using wiringPi to debug using GPIO");
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
|
||||||
dev = device_init();
|
dev = device_init();
|
||||||
encoder = encoder_init();
|
encoder = encoder_init();
|
||||||
stream = stream_init(dev, encoder);
|
stream = stream_init(dev, encoder);
|
||||||
|
|||||||
511
src/stream.c
511
src/stream.c
@@ -20,6 +20,9 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "stream.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdatomic.h>
|
#include <stdatomic.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@@ -28,7 +31,6 @@
|
|||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
#include <sys/select.h>
|
|
||||||
#include <linux/videodev2.h>
|
#include <linux/videodev2.h>
|
||||||
|
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
@@ -36,18 +38,72 @@
|
|||||||
#include "xioctl.h"
|
#include "xioctl.h"
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
#include "encoder.h"
|
#include "encoder.h"
|
||||||
#include "stream.h"
|
|
||||||
|
#ifdef WITH_WORKERS_GPIO_DEBUG
|
||||||
|
# include <wiringPi.h>
|
||||||
|
# ifndef WORKERS_GPIO_DEBUG_START_PIN
|
||||||
|
# define WORKERS_GPIO_DEBUG_START_PIN 5
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static long double _stream_get_fluency_delay(struct device_t *dev, struct workers_pool_t *pool);
|
struct _worker_t {
|
||||||
static void _stream_expose_picture(struct stream_t *stream, unsigned buf_index);
|
pthread_t tid;
|
||||||
|
unsigned number;
|
||||||
|
atomic_bool *proc_stop;
|
||||||
|
atomic_bool *workers_stop;
|
||||||
|
|
||||||
static int _stream_init_loop(struct stream_t *stream, struct workers_pool_t *pool);
|
long double last_comp_time;
|
||||||
static int _stream_init(struct stream_t *stream, struct workers_pool_t *pool);
|
|
||||||
|
|
||||||
static void _stream_init_workers(struct stream_t *stream, struct workers_pool_t *pool);
|
pthread_mutex_t has_job_mutex;
|
||||||
static void *_stream_worker_thread(void *v_worker);
|
int buf_index;
|
||||||
static void _stream_destroy_workers(struct stream_t *stream, struct workers_pool_t *pool);
|
atomic_bool has_job;
|
||||||
|
bool job_timely;
|
||||||
|
bool job_failed;
|
||||||
|
long double job_start_time;
|
||||||
|
pthread_cond_t has_job_cond;
|
||||||
|
|
||||||
|
pthread_mutex_t *free_workers_mutex;
|
||||||
|
unsigned *free_workers;
|
||||||
|
pthread_cond_t *free_workers_cond;
|
||||||
|
|
||||||
|
struct _worker_t *order_prev;
|
||||||
|
struct _worker_t *order_next;
|
||||||
|
|
||||||
|
struct device_t *dev;
|
||||||
|
struct encoder_t *encoder;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct _workers_pool_t {
|
||||||
|
unsigned n_workers;
|
||||||
|
struct _worker_t *workers;
|
||||||
|
struct _worker_t *oldest_worker;
|
||||||
|
struct _worker_t *latest_worker;
|
||||||
|
|
||||||
|
long double approx_comp_time;
|
||||||
|
|
||||||
|
pthread_mutex_t free_workers_mutex;
|
||||||
|
unsigned free_workers;
|
||||||
|
pthread_cond_t free_workers_cond;
|
||||||
|
|
||||||
|
atomic_bool workers_stop;
|
||||||
|
|
||||||
|
long double desired_frames_interval;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static struct _workers_pool_t *_stream_init_loop(struct stream_t *stream);
|
||||||
|
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);
|
||||||
|
|
||||||
|
static struct _workers_pool_t *_workers_pool_init(struct stream_t *stream);
|
||||||
|
static void _workers_pool_destroy(struct _workers_pool_t *pool);
|
||||||
|
|
||||||
|
static void *__worker_thread(void *v_worker);
|
||||||
|
|
||||||
|
static struct _worker_t *_workers_pool_wait(struct _workers_pool_t *pool);
|
||||||
|
static void _workers_pool_assign(struct _workers_pool_t *pool, struct _worker_t *ready_worker, unsigned buf_index);
|
||||||
|
static long double _workers_pool_get_fluency_delay(struct _workers_pool_t *pool, struct _worker_t *ready_worker);
|
||||||
|
|
||||||
|
|
||||||
struct stream_t *stream_init(struct device_t *dev, struct encoder_t *encoder) {
|
struct stream_t *stream_init(struct device_t *dev, struct encoder_t *encoder) {
|
||||||
@@ -74,20 +130,15 @@ void stream_destroy(struct stream_t *stream) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void stream_loop(struct stream_t *stream) {
|
void stream_loop(struct stream_t *stream) {
|
||||||
struct workers_pool_t pool;
|
struct _workers_pool_t *pool;
|
||||||
|
|
||||||
MEMSET_ZERO(pool);
|
|
||||||
atomic_init(&pool.workers_stop, false);
|
|
||||||
pool.encoder = stream->encoder;
|
|
||||||
|
|
||||||
LOG_INFO("Using V4L2 device: %s", stream->dev->path);
|
LOG_INFO("Using V4L2 device: %s", stream->dev->path);
|
||||||
LOG_INFO("Using desired FPS: %u", stream->dev->desired_fps);
|
LOG_INFO("Using desired FPS: %u", stream->dev->desired_fps);
|
||||||
|
|
||||||
while (_stream_init_loop(stream, &pool) == 0) {
|
while ((pool = _stream_init_loop(stream)) != NULL) {
|
||||||
struct worker_t *oldest_worker = NULL;
|
|
||||||
struct worker_t *last_worker = NULL;
|
|
||||||
long double grab_after = 0;
|
long double grab_after = 0;
|
||||||
unsigned fluency_passed = 0;
|
unsigned fluency_passed = 0;
|
||||||
|
unsigned captured_fps = 0;
|
||||||
unsigned captured_fps_accum = 0;
|
unsigned captured_fps_accum = 0;
|
||||||
long long captured_fps_second = 0;
|
long long captured_fps_second = 0;
|
||||||
bool persistent_timeout_reported = false;
|
bool persistent_timeout_reported = false;
|
||||||
@@ -98,43 +149,22 @@ void stream_loop(struct stream_t *stream) {
|
|||||||
LOG_INFO("Capturing ...");
|
LOG_INFO("Capturing ...");
|
||||||
|
|
||||||
while (!atomic_load(&stream->proc->stop)) {
|
while (!atomic_load(&stream->proc->stop)) {
|
||||||
int free_worker_number = -1;
|
struct _worker_t *ready_worker;
|
||||||
|
|
||||||
SEP_DEBUG('-');
|
SEP_DEBUG('-');
|
||||||
|
LOG_DEBUG("Waiting for worker ...");
|
||||||
|
|
||||||
LOG_DEBUG("Waiting for workers ...");
|
ready_worker = _workers_pool_wait(pool);
|
||||||
A_MUTEX_LOCK(&pool.free_workers_mutex);
|
|
||||||
A_COND_WAIT_TRUE(pool.free_workers, &pool.free_workers_cond, &pool.free_workers_mutex);
|
|
||||||
A_MUTEX_UNLOCK(&pool.free_workers_mutex);
|
|
||||||
|
|
||||||
if (oldest_worker && !atomic_load(&oldest_worker->has_job) && oldest_worker->buf_index >= 0) {
|
if (!ready_worker->job_failed) {
|
||||||
if (oldest_worker->job_failed) {
|
if (ready_worker->job_timely) {
|
||||||
break;
|
_stream_expose_picture(stream, ready_worker->buf_index, captured_fps);
|
||||||
|
LOG_PERF("##### Encoded picture exposed; worker=%u", ready_worker->number);
|
||||||
|
} else {
|
||||||
|
LOG_PERF("----- Encoded picture dropped; worker=%u", ready_worker->number);
|
||||||
}
|
}
|
||||||
|
|
||||||
_stream_expose_picture(stream, oldest_worker->buf_index);
|
|
||||||
|
|
||||||
free_worker_number = oldest_worker->number;
|
|
||||||
oldest_worker = oldest_worker->order_next;
|
|
||||||
|
|
||||||
LOG_PERF("##### Raw frame accepted; worker = %u", free_worker_number);
|
|
||||||
} else {
|
} else {
|
||||||
for (unsigned number = 0; number < stream->dev->run->n_workers; ++number) {
|
break;
|
||||||
if (
|
|
||||||
!atomic_load(&pool.workers[number].has_job) && (
|
|
||||||
free_worker_number == -1
|
|
||||||
|| pool.workers[free_worker_number].job_start_time < pool.workers[number].job_start_time
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
free_worker_number = number;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(free_worker_number >= 0);
|
|
||||||
assert(!atomic_load(&pool.workers[free_worker_number].has_job));
|
|
||||||
|
|
||||||
LOG_PERF("----- Raw frame dropped; worker = %u", free_worker_number);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (atomic_load(&stream->proc->stop)) {
|
if (atomic_load(&stream->proc->stop)) {
|
||||||
@@ -145,30 +175,18 @@ void stream_loop(struct stream_t *stream) {
|
|||||||
usleep(1000000);
|
usleep(1000000);
|
||||||
}
|
}
|
||||||
|
|
||||||
# define INIT_FD_SET(_set) \
|
bool has_read;
|
||||||
fd_set _set; FD_ZERO(&_set); FD_SET(stream->dev->run->fd, &_set);
|
bool has_write;
|
||||||
|
bool has_error;
|
||||||
|
int selected = device_select(stream->dev, &has_read, &has_write, &has_error);
|
||||||
|
|
||||||
INIT_FD_SET(read_fds);
|
if (selected < 0) {
|
||||||
INIT_FD_SET(write_fds);
|
|
||||||
INIT_FD_SET(error_fds);
|
|
||||||
|
|
||||||
# undef INIT_FD_SET
|
|
||||||
|
|
||||||
struct timeval timeout;
|
|
||||||
timeout.tv_sec = stream->dev->timeout;
|
|
||||||
timeout.tv_usec = 0;
|
|
||||||
|
|
||||||
LOG_DEBUG("Calling select() on video device ...");
|
|
||||||
int retval = select(stream->dev->run->fd + 1, &read_fds, &write_fds, &error_fds, &timeout);
|
|
||||||
LOG_DEBUG("Device select() --> %d", retval);
|
|
||||||
|
|
||||||
if (retval < 0) {
|
|
||||||
if (errno != EINTR) {
|
if (errno != EINTR) {
|
||||||
LOG_PERROR("Mainloop select() error");
|
LOG_PERROR("Mainloop select() error");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (retval == 0) {
|
} else if (selected == 0) {
|
||||||
if (stream->dev->persistent) {
|
if (stream->dev->persistent) {
|
||||||
if (!persistent_timeout_reported) {
|
if (!persistent_timeout_reported) {
|
||||||
LOG_ERROR("Mainloop select() timeout, polling ...")
|
LOG_ERROR("Mainloop select() timeout, polling ...")
|
||||||
@@ -183,7 +201,7 @@ void stream_loop(struct stream_t *stream) {
|
|||||||
} else {
|
} else {
|
||||||
persistent_timeout_reported = false;
|
persistent_timeout_reported = false;
|
||||||
|
|
||||||
if (FD_ISSET(stream->dev->run->fd, &read_fds)) {
|
if (has_read) {
|
||||||
LOG_DEBUG("Frame is ready");
|
LOG_DEBUG("Frame is ready");
|
||||||
|
|
||||||
int buf_index;
|
int buf_index;
|
||||||
@@ -193,7 +211,6 @@ void stream_loop(struct stream_t *stream) {
|
|||||||
if ((buf_index = device_grab_buffer(stream->dev)) < 0) {
|
if ((buf_index = device_grab_buffer(stream->dev)) < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
stream->dev->run->pictures[buf_index].grab_time = now;
|
|
||||||
|
|
||||||
// Workaround for broken, corrupted frames:
|
// Workaround for broken, corrupted frames:
|
||||||
// Under low light conditions corrupted frames may get captured.
|
// Under low light conditions corrupted frames may get captured.
|
||||||
@@ -201,7 +218,7 @@ void stream_loop(struct stream_t *stream) {
|
|||||||
// For example a VGA (640x480) webcam picture is normally >= 8kByte large,
|
// For example a VGA (640x480) webcam picture is normally >= 8kByte large,
|
||||||
// corrupted frames are smaller.
|
// corrupted frames are smaller.
|
||||||
if (stream->dev->run->hw_buffers[buf_index].used < stream->dev->min_frame_size) {
|
if (stream->dev->run->hw_buffers[buf_index].used < stream->dev->min_frame_size) {
|
||||||
LOG_DEBUG("Dropping too small frame sized %zu bytes, assuming it as broken",
|
LOG_DEBUG("Dropped too small frame sized %zu bytes, assuming it was broken",
|
||||||
stream->dev->run->hw_buffers[buf_index].used);
|
stream->dev->run->hw_buffers[buf_index].used);
|
||||||
goto pass_frame;
|
goto pass_frame;
|
||||||
}
|
}
|
||||||
@@ -209,56 +226,26 @@ void stream_loop(struct stream_t *stream) {
|
|||||||
{
|
{
|
||||||
if (now < grab_after) {
|
if (now < grab_after) {
|
||||||
fluency_passed += 1;
|
fluency_passed += 1;
|
||||||
LOG_VERBOSE("Passed %u frames for fluency: now=%.03Lf; grab_after=%.03Lf", fluency_passed, now, grab_after);
|
LOG_VERBOSE("Passed %u frames for fluency: now=%.03Lf, grab_after=%.03Lf", fluency_passed, now, grab_after);
|
||||||
goto pass_frame;
|
goto pass_frame;
|
||||||
}
|
}
|
||||||
fluency_passed = 0;
|
fluency_passed = 0;
|
||||||
|
|
||||||
if (now_second != captured_fps_second) {
|
if (now_second != captured_fps_second) {
|
||||||
stream->captured_fps = captured_fps_accum;
|
captured_fps = captured_fps_accum;
|
||||||
captured_fps_accum = 0;
|
captured_fps_accum = 0;
|
||||||
captured_fps_second = now_second;
|
captured_fps_second = now_second;
|
||||||
LOG_PERF("Oldest worker complete, Captured-FPS = %u", stream->captured_fps);
|
LOG_PERF("A new second has come; captured_fps=%u", captured_fps);
|
||||||
}
|
}
|
||||||
captured_fps_accum += 1;
|
captured_fps_accum += 1;
|
||||||
|
|
||||||
long double fluency_delay = _stream_get_fluency_delay(stream->dev, &pool);
|
long double fluency_delay = _workers_pool_get_fluency_delay(pool, ready_worker);
|
||||||
|
|
||||||
grab_after = now + fluency_delay;
|
grab_after = now + fluency_delay;
|
||||||
LOG_VERBOSE("Fluency: delay=%.03Lf; grab_after=%.03Lf", fluency_delay, grab_after);
|
LOG_VERBOSE("Fluency: delay=%.03Lf, grab_after=%.03Lf", fluency_delay, grab_after);
|
||||||
}
|
}
|
||||||
|
|
||||||
# define FREE_WORKER(_next) pool.workers[free_worker_number]._next
|
_workers_pool_assign(pool, ready_worker, buf_index);
|
||||||
|
|
||||||
LOG_DEBUG("Grabbed a new frame to buffer %u", buf_index);
|
|
||||||
|
|
||||||
if (oldest_worker == NULL) {
|
|
||||||
oldest_worker = &pool.workers[free_worker_number];
|
|
||||||
last_worker = oldest_worker;
|
|
||||||
} else {
|
|
||||||
if (FREE_WORKER(order_next)) {
|
|
||||||
FREE_WORKER(order_next->order_prev) = FREE_WORKER(order_prev);
|
|
||||||
}
|
|
||||||
if (FREE_WORKER(order_prev)) {
|
|
||||||
FREE_WORKER(order_prev->order_next) = FREE_WORKER(order_next);
|
|
||||||
}
|
|
||||||
FREE_WORKER(order_prev) = last_worker;
|
|
||||||
last_worker->order_next = &pool.workers[free_worker_number];
|
|
||||||
last_worker = &pool.workers[free_worker_number];
|
|
||||||
}
|
|
||||||
last_worker->order_next = NULL;
|
|
||||||
|
|
||||||
A_MUTEX_LOCK(&FREE_WORKER(has_job_mutex));
|
|
||||||
FREE_WORKER(buf_index) = buf_index;
|
|
||||||
atomic_store(&FREE_WORKER(has_job), true);
|
|
||||||
A_MUTEX_UNLOCK(&FREE_WORKER(has_job_mutex));
|
|
||||||
A_COND_SIGNAL(&FREE_WORKER(has_job_cond));
|
|
||||||
|
|
||||||
# undef FREE_WORKER
|
|
||||||
|
|
||||||
A_MUTEX_LOCK(&pool.free_workers_mutex);
|
|
||||||
pool.free_workers -= 1;
|
|
||||||
A_MUTEX_UNLOCK(&pool.free_workers_mutex);
|
|
||||||
|
|
||||||
goto next_handlers; // Поток сам освободит буфер
|
goto next_handlers; // Поток сам освободит буфер
|
||||||
|
|
||||||
@@ -271,12 +258,12 @@ void stream_loop(struct stream_t *stream) {
|
|||||||
|
|
||||||
next_handlers:
|
next_handlers:
|
||||||
|
|
||||||
if (FD_ISSET(stream->dev->run->fd, &write_fds)) {
|
if (has_write) {
|
||||||
LOG_ERROR("Got unexpected writing event, seems device was disconnected");
|
LOG_ERROR("Got unexpected writing event, seems device was disconnected");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FD_ISSET(stream->dev->run->fd, &error_fds)) {
|
if (has_error) {
|
||||||
LOG_INFO("Got V4L2 event");
|
LOG_INFO("Got V4L2 event");
|
||||||
if (device_consume_event(stream->dev) < 0) {
|
if (device_consume_event(stream->dev) < 0) {
|
||||||
break;
|
break;
|
||||||
@@ -292,11 +279,11 @@ void stream_loop(struct stream_t *stream) {
|
|||||||
stream->height = 0;
|
stream->height = 0;
|
||||||
atomic_store(&stream->updated, true);
|
atomic_store(&stream->updated, true);
|
||||||
A_MUTEX_UNLOCK(&stream->mutex);
|
A_MUTEX_UNLOCK(&stream->mutex);
|
||||||
}
|
|
||||||
|
|
||||||
_stream_destroy_workers(stream, &pool);
|
_workers_pool_destroy(pool);
|
||||||
device_switch_capturing(stream->dev, false);
|
device_switch_capturing(stream->dev, false);
|
||||||
device_close(stream->dev);
|
device_close(stream->dev);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stream_loop_break(struct stream_t *stream) {
|
void stream_loop_break(struct stream_t *stream) {
|
||||||
@@ -307,7 +294,41 @@ void stream_switch_slowdown(struct stream_t *stream, bool slowdown) {
|
|||||||
atomic_store(&stream->proc->slowdown, slowdown);
|
atomic_store(&stream->proc->slowdown, slowdown);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _stream_expose_picture(struct stream_t *stream, unsigned buf_index) {
|
static struct _workers_pool_t *_stream_init_loop(struct stream_t *stream) {
|
||||||
|
struct _workers_pool_t *pool = NULL;
|
||||||
|
|
||||||
|
LOG_DEBUG("%s: stream->proc->stop=%d", __FUNCTION__, atomic_load(&stream->proc->stop));
|
||||||
|
|
||||||
|
while (!atomic_load(&stream->proc->stop)) {
|
||||||
|
SEP_INFO('=');
|
||||||
|
|
||||||
|
if ((pool = _stream_init(stream)) == NULL) {
|
||||||
|
LOG_INFO("Sleeping %u seconds before new stream init ...", stream->dev->error_delay);
|
||||||
|
sleep(stream->dev->error_delay);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct _workers_pool_t *_stream_init(struct stream_t *stream) {
|
||||||
|
if (device_open(stream->dev) < 0) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
if (device_switch_capturing(stream->dev, true) < 0) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
encoder_prepare(stream->encoder, stream->dev);
|
||||||
|
return _workers_pool_init(stream);
|
||||||
|
|
||||||
|
error:
|
||||||
|
device_close(stream->dev);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
# define PICTURE(_next) stream->dev->run->pictures[buf_index]._next
|
||||||
|
|
||||||
A_MUTEX_LOCK(&stream->mutex);
|
A_MUTEX_LOCK(&stream->mutex);
|
||||||
@@ -323,6 +344,7 @@ static void _stream_expose_picture(struct stream_t *stream, unsigned buf_index)
|
|||||||
|
|
||||||
stream->width = stream->dev->run->width;
|
stream->width = stream->dev->run->width;
|
||||||
stream->height = stream->dev->run->height;
|
stream->height = stream->dev->run->height;
|
||||||
|
stream->captured_fps = captured_fps;
|
||||||
atomic_store(&stream->updated, true);
|
atomic_store(&stream->updated, true);
|
||||||
|
|
||||||
A_MUTEX_UNLOCK(&stream->mutex);
|
A_MUTEX_UNLOCK(&stream->mutex);
|
||||||
@@ -330,118 +352,98 @@ static void _stream_expose_picture(struct stream_t *stream, unsigned buf_index)
|
|||||||
# undef PICTURE
|
# undef PICTURE
|
||||||
}
|
}
|
||||||
|
|
||||||
static long double _stream_get_fluency_delay(struct device_t *dev, struct workers_pool_t *pool) {
|
static struct _workers_pool_t *_workers_pool_init(struct stream_t *stream) {
|
||||||
long double sum_comp_time = 0;
|
struct _workers_pool_t *pool;
|
||||||
long double avg_comp_time;
|
|
||||||
long double min_delay;
|
|
||||||
long double soft_delay;
|
|
||||||
|
|
||||||
for (unsigned number = 0; number < dev->run->n_workers; ++number) {
|
LOG_INFO("Creating pool with %u workers ...", stream->dev->run->n_workers);
|
||||||
# define WORKER(_next) pool->workers[number]._next
|
|
||||||
|
|
||||||
A_MUTEX_LOCK(&WORKER(last_comp_time_mutex));
|
A_CALLOC(pool, 1);
|
||||||
if (WORKER(last_comp_time) > 0) {
|
|
||||||
sum_comp_time += WORKER(last_comp_time);
|
|
||||||
}
|
|
||||||
A_MUTEX_UNLOCK(&WORKER(last_comp_time_mutex));
|
|
||||||
|
|
||||||
# undef WORKER
|
pool->n_workers = stream->dev->run->n_workers;
|
||||||
}
|
A_CALLOC(pool->workers, pool->n_workers);
|
||||||
avg_comp_time = sum_comp_time / dev->run->n_workers; // Среднее время работы воркеров
|
|
||||||
|
|
||||||
min_delay = avg_comp_time / dev->run->n_workers; // Среднее время работы размазывается на N воркеров
|
|
||||||
|
|
||||||
if (dev->desired_fps > 0 && min_delay > 0) {
|
|
||||||
// Искусственное время задержки на основе желаемого FPS, если включен --desired-fps
|
|
||||||
soft_delay = ((long double) 1) / dev->desired_fps - sum_comp_time;
|
|
||||||
return (min_delay > soft_delay ? min_delay : soft_delay);
|
|
||||||
}
|
|
||||||
|
|
||||||
return min_delay;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int _stream_init_loop(struct stream_t *stream, struct workers_pool_t *pool) {
|
|
||||||
int retval = -1;
|
|
||||||
|
|
||||||
LOG_DEBUG("%s: stream->proc->stop = %d", __FUNCTION__, atomic_load(&stream->proc->stop));
|
|
||||||
while (!atomic_load(&stream->proc->stop)) {
|
|
||||||
if ((retval = _stream_init(stream, pool)) < 0) {
|
|
||||||
LOG_INFO("Sleeping %u seconds before new stream init ...", stream->dev->error_delay);
|
|
||||||
sleep(stream->dev->error_delay);
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int _stream_init(struct stream_t *stream, struct workers_pool_t *pool) {
|
|
||||||
_stream_destroy_workers(stream, pool);
|
|
||||||
device_switch_capturing(stream->dev, false);
|
|
||||||
device_close(stream->dev);
|
|
||||||
|
|
||||||
SEP_INFO('=');
|
|
||||||
|
|
||||||
if (device_open(stream->dev) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
if (device_switch_capturing(stream->dev, true) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
encoder_prepare(pool->encoder, stream->dev);
|
|
||||||
|
|
||||||
_stream_init_workers(stream, pool);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
error:
|
|
||||||
device_close(stream->dev);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _stream_init_workers(struct stream_t *stream, struct workers_pool_t *pool) {
|
|
||||||
LOG_INFO("Spawning %u workers ...", stream->dev->run->n_workers);
|
|
||||||
|
|
||||||
atomic_store(&pool->workers_stop, false);
|
|
||||||
A_CALLOC(pool->workers, stream->dev->run->n_workers);
|
|
||||||
|
|
||||||
A_MUTEX_INIT(&pool->free_workers_mutex);
|
A_MUTEX_INIT(&pool->free_workers_mutex);
|
||||||
A_COND_INIT(&pool->free_workers_cond);
|
A_COND_INIT(&pool->free_workers_cond);
|
||||||
|
|
||||||
for (unsigned number = 0; number < stream->dev->run->n_workers; ++number) {
|
atomic_init(&pool->workers_stop, false);
|
||||||
# define WORKER(_next) pool->workers[number]._next
|
|
||||||
|
|
||||||
pool->free_workers += 1;
|
if (stream->dev->desired_fps > 0) {
|
||||||
|
pool->desired_frames_interval = (long double)1 / stream->dev->desired_fps;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned number = 0; number < pool->n_workers; ++number) {
|
||||||
|
# define WORKER(_next) pool->workers[number]._next
|
||||||
|
|
||||||
A_MUTEX_INIT(&WORKER(has_job_mutex));
|
A_MUTEX_INIT(&WORKER(has_job_mutex));
|
||||||
atomic_init(&WORKER(has_job), false);
|
atomic_init(&WORKER(has_job), false);
|
||||||
A_COND_INIT(&WORKER(has_job_cond));
|
A_COND_INIT(&WORKER(has_job_cond));
|
||||||
|
|
||||||
WORKER(number) = number;
|
WORKER(number) = number;
|
||||||
WORKER(proc_stop) = &stream->proc->stop;
|
WORKER(proc_stop) = &stream->proc->stop;
|
||||||
WORKER(workers_stop) = &pool->workers_stop;
|
WORKER(workers_stop) = &pool->workers_stop;
|
||||||
|
|
||||||
WORKER(free_workers_mutex) = &pool->free_workers_mutex;
|
WORKER(free_workers_mutex) = &pool->free_workers_mutex;
|
||||||
WORKER(free_workers) = &pool->free_workers;
|
WORKER(free_workers) = &pool->free_workers;
|
||||||
WORKER(free_workers_cond) = &pool->free_workers_cond;
|
WORKER(free_workers_cond) = &pool->free_workers_cond;
|
||||||
|
|
||||||
WORKER(dev) = stream->dev;
|
WORKER(dev) = stream->dev;
|
||||||
WORKER(encoder) = pool->encoder;
|
WORKER(encoder) = stream->encoder;
|
||||||
|
|
||||||
A_THREAD_CREATE(&WORKER(tid), _stream_worker_thread, (void *)&(pool->workers[number]));
|
A_THREAD_CREATE(&WORKER(tid), __worker_thread, (void *)&(pool->workers[number]));
|
||||||
|
|
||||||
|
pool->free_workers += 1;
|
||||||
|
|
||||||
# undef WORKER
|
# undef WORKER
|
||||||
}
|
}
|
||||||
|
return pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *_stream_worker_thread(void *v_worker) {
|
static void _workers_pool_destroy(struct _workers_pool_t *pool) {
|
||||||
struct worker_t *worker = (struct worker_t *)v_worker;
|
LOG_INFO("Destroying workers pool ...");
|
||||||
|
|
||||||
|
atomic_store(&pool->workers_stop, true);
|
||||||
|
for (unsigned number = 0; number < pool->n_workers; ++number) {
|
||||||
|
# define WORKER(_next) pool->workers[number]._next
|
||||||
|
|
||||||
|
A_MUTEX_LOCK(&WORKER(has_job_mutex));
|
||||||
|
atomic_store(&WORKER(has_job), true); // Final job: die
|
||||||
|
A_MUTEX_UNLOCK(&WORKER(has_job_mutex));
|
||||||
|
A_COND_SIGNAL(&WORKER(has_job_cond));
|
||||||
|
|
||||||
|
A_THREAD_JOIN(WORKER(tid));
|
||||||
|
A_MUTEX_DESTROY(&WORKER(has_job_mutex));
|
||||||
|
A_COND_DESTROY(&WORKER(has_job_cond));
|
||||||
|
|
||||||
|
# undef WORKER
|
||||||
|
}
|
||||||
|
|
||||||
|
A_MUTEX_DESTROY(&pool->free_workers_mutex);
|
||||||
|
A_COND_DESTROY(&pool->free_workers_cond);
|
||||||
|
|
||||||
|
free(pool->workers);
|
||||||
|
free(pool);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *__worker_thread(void *v_worker) {
|
||||||
|
struct _worker_t *worker = (struct _worker_t *)v_worker;
|
||||||
|
|
||||||
LOG_DEBUG("Hello! I am a worker #%u ^_^", worker->number);
|
LOG_DEBUG("Hello! I am a worker #%u ^_^", worker->number);
|
||||||
|
|
||||||
|
# ifdef WITH_WORKERS_GPIO_DEBUG
|
||||||
|
# define WORKER_GPIO_DEBUG_BUSY digitalWrite(WORKERS_GPIO_DEBUG_START_PIN + worker->number, HIGH)
|
||||||
|
# define WORKER_GPIO_DEBUG_FREE digitalWrite(WORKERS_GPIO_DEBUG_START_PIN + worker->number, LOW)
|
||||||
|
pinMode(WORKERS_GPIO_DEBUG_START_PIN + worker->number, OUTPUT);
|
||||||
|
WORKER_GPIO_DEBUG_FREE;
|
||||||
|
# else
|
||||||
|
# define WORKER_GPIO_DEBUG_BUSY
|
||||||
|
# define WORKER_GPIO_DEBUG_FREE
|
||||||
|
# endif
|
||||||
|
|
||||||
while (!atomic_load(worker->proc_stop) && !atomic_load(worker->workers_stop)) {
|
while (!atomic_load(worker->proc_stop) && !atomic_load(worker->workers_stop)) {
|
||||||
LOG_DEBUG("Worker %u waiting for a new job ...", worker->number);
|
LOG_DEBUG("Worker %u waiting for a new job ...", worker->number);
|
||||||
|
|
||||||
|
WORKER_GPIO_DEBUG_FREE;
|
||||||
|
|
||||||
A_MUTEX_LOCK(&worker->has_job_mutex);
|
A_MUTEX_LOCK(&worker->has_job_mutex);
|
||||||
A_COND_WAIT_TRUE(atomic_load(&worker->has_job), &worker->has_job_cond, &worker->has_job_mutex);
|
A_COND_WAIT_TRUE(atomic_load(&worker->has_job), &worker->has_job_cond, &worker->has_job_mutex);
|
||||||
A_MUTEX_UNLOCK(&worker->has_job_mutex);
|
A_MUTEX_UNLOCK(&worker->has_job_mutex);
|
||||||
@@ -451,24 +453,20 @@ static void *_stream_worker_thread(void *v_worker) {
|
|||||||
|
|
||||||
LOG_DEBUG("Worker %u compressing JPEG from buffer %u ...", worker->number, worker->buf_index);
|
LOG_DEBUG("Worker %u compressing JPEG from buffer %u ...", worker->number, worker->buf_index);
|
||||||
|
|
||||||
PICTURE(encode_begin_time) = get_now_monotonic();
|
WORKER_GPIO_DEBUG_BUSY;
|
||||||
|
|
||||||
if (encoder_compress_buffer(worker->encoder, worker->dev, worker->number, worker->buf_index) < 0) {
|
if (encoder_compress_buffer(worker->encoder, worker->dev, worker->number, worker->buf_index) < 0) {
|
||||||
worker->job_failed = true;
|
worker->job_failed = false;
|
||||||
}
|
}
|
||||||
PICTURE(encode_end_time) = get_now_monotonic();
|
|
||||||
|
|
||||||
if (device_release_buffer(worker->dev, worker->buf_index) == 0) {
|
if (device_release_buffer(worker->dev, worker->buf_index) == 0) {
|
||||||
worker->job_start_time = PICTURE(encode_begin_time);
|
worker->job_start_time = PICTURE(encode_begin_time);
|
||||||
atomic_store(&worker->has_job, false);
|
atomic_store(&worker->has_job, false);
|
||||||
|
|
||||||
long double last_comp_time = PICTURE(encode_end_time) - worker->job_start_time;
|
worker->last_comp_time = PICTURE(encode_end_time) - worker->job_start_time;
|
||||||
|
|
||||||
A_MUTEX_LOCK(&worker->last_comp_time_mutex);
|
LOG_VERBOSE("Compressed new JPEG: size=%zu, time=%0.3Lf, worker=%u, buffer=%u",
|
||||||
worker->last_comp_time = last_comp_time;
|
PICTURE(used), worker->last_comp_time, worker->number, worker->buf_index);
|
||||||
A_MUTEX_UNLOCK(&worker->last_comp_time_mutex);
|
|
||||||
|
|
||||||
LOG_VERBOSE("Compressed JPEG size=%zu; time=%0.3Lf; worker=%u; buffer=%u",
|
|
||||||
PICTURE(used), last_comp_time, worker->number, worker->buf_index);
|
|
||||||
} else {
|
} else {
|
||||||
worker->job_failed = true;
|
worker->job_failed = true;
|
||||||
atomic_store(&worker->has_job, false);
|
atomic_store(&worker->has_job, false);
|
||||||
@@ -484,34 +482,85 @@ static void *_stream_worker_thread(void *v_worker) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
LOG_DEBUG("Bye-bye (worker %u)", worker->number);
|
LOG_DEBUG("Bye-bye (worker %u)", worker->number);
|
||||||
|
WORKER_GPIO_DEBUG_FREE;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _stream_destroy_workers(struct stream_t *stream, struct workers_pool_t *pool) {
|
static struct _worker_t *_workers_pool_wait(struct _workers_pool_t *pool) {
|
||||||
if (pool->workers) {
|
struct _worker_t *ready_worker = NULL;
|
||||||
LOG_INFO("Destroying workers ...");
|
|
||||||
|
|
||||||
atomic_store(&pool->workers_stop, true);
|
A_MUTEX_LOCK(&pool->free_workers_mutex);
|
||||||
for (unsigned number = 0; number < stream->dev->run->n_workers; ++number) {
|
A_COND_WAIT_TRUE(pool->free_workers, &pool->free_workers_cond, &pool->free_workers_mutex);
|
||||||
# define WORKER(_next) pool->workers[number]._next
|
A_MUTEX_UNLOCK(&pool->free_workers_mutex);
|
||||||
|
|
||||||
A_MUTEX_LOCK(&WORKER(has_job_mutex));
|
if (pool->oldest_worker && !atomic_load(&pool->oldest_worker->has_job) && pool->oldest_worker->buf_index >= 0) {
|
||||||
atomic_store(&WORKER(has_job), true); // Final job: die
|
ready_worker = pool->oldest_worker;
|
||||||
A_MUTEX_UNLOCK(&WORKER(has_job_mutex));
|
ready_worker->job_timely = true;
|
||||||
A_COND_SIGNAL(&WORKER(has_job_cond));
|
pool->oldest_worker = pool->oldest_worker->order_next;
|
||||||
|
} else {
|
||||||
A_THREAD_JOIN(WORKER(tid));
|
for (unsigned number = 0; number < pool->n_workers; ++number) {
|
||||||
A_MUTEX_DESTROY(&WORKER(has_job_mutex));
|
if (
|
||||||
A_COND_DESTROY(&WORKER(has_job_cond));
|
!atomic_load(&pool->workers[number].has_job) && (
|
||||||
|
ready_worker == NULL
|
||||||
# undef WORKER
|
|| ready_worker->job_start_time < pool->workers[number].job_start_time
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
ready_worker = &pool->workers[number];
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
assert(ready_worker != NULL);
|
||||||
A_MUTEX_DESTROY(&pool->free_workers_mutex);
|
ready_worker->job_timely = false; // Освободился воркер, получивший задание позже (или самый первый при самом первом захвате)
|
||||||
A_COND_DESTROY(&pool->free_workers_cond);
|
|
||||||
|
|
||||||
free(pool->workers);
|
|
||||||
}
|
}
|
||||||
pool->free_workers = 0;
|
return ready_worker;
|
||||||
pool->workers = NULL;
|
}
|
||||||
|
|
||||||
|
static void _workers_pool_assign(struct _workers_pool_t *pool, struct _worker_t *ready_worker, unsigned buf_index) {
|
||||||
|
if (pool->oldest_worker == NULL) {
|
||||||
|
pool->oldest_worker = ready_worker;
|
||||||
|
pool->latest_worker = pool->oldest_worker;
|
||||||
|
} else {
|
||||||
|
if (ready_worker->order_next) {
|
||||||
|
ready_worker->order_next->order_prev = ready_worker->order_prev;
|
||||||
|
}
|
||||||
|
if (ready_worker->order_prev) {
|
||||||
|
ready_worker->order_prev->order_next = ready_worker->order_next;
|
||||||
|
}
|
||||||
|
ready_worker->order_prev = pool->latest_worker;
|
||||||
|
pool->latest_worker->order_next = ready_worker;
|
||||||
|
pool->latest_worker = ready_worker;
|
||||||
|
}
|
||||||
|
pool->latest_worker->order_next = NULL;
|
||||||
|
|
||||||
|
A_MUTEX_LOCK(&ready_worker->has_job_mutex);
|
||||||
|
ready_worker->buf_index = buf_index;
|
||||||
|
atomic_store(&ready_worker->has_job, true);
|
||||||
|
A_MUTEX_UNLOCK(&ready_worker->has_job_mutex);
|
||||||
|
A_COND_SIGNAL(&ready_worker->has_job_cond);
|
||||||
|
|
||||||
|
A_MUTEX_LOCK(&pool->free_workers_mutex);
|
||||||
|
pool->free_workers -= 1;
|
||||||
|
A_MUTEX_UNLOCK(&pool->free_workers_mutex);
|
||||||
|
|
||||||
|
LOG_DEBUG("Assigned new frame in buffer %u to worker %u", buf_index, ready_worker->number);
|
||||||
|
}
|
||||||
|
|
||||||
|
static long double _workers_pool_get_fluency_delay(struct _workers_pool_t *pool, struct _worker_t *ready_worker) {
|
||||||
|
long double approx_comp_time;
|
||||||
|
long double min_delay;
|
||||||
|
|
||||||
|
approx_comp_time = pool->approx_comp_time * 0.9 + ready_worker->last_comp_time * 0.1;
|
||||||
|
|
||||||
|
LOG_VERBOSE("Correcting approx_comp_time: %.3Lf -> %.3Lf (last_comp_time=%.3Lf)",
|
||||||
|
pool->approx_comp_time, approx_comp_time, ready_worker->last_comp_time);
|
||||||
|
|
||||||
|
pool->approx_comp_time = approx_comp_time;
|
||||||
|
|
||||||
|
min_delay = pool->approx_comp_time / pool->n_workers; // Среднее время работы размазывается на N воркеров
|
||||||
|
|
||||||
|
if (pool->desired_frames_interval > 0 && min_delay > 0 && pool->desired_frames_interval > min_delay) {
|
||||||
|
// Искусственное время задержки на основе желаемого FPS, если включен --desired-fps
|
||||||
|
return pool->desired_frames_interval;
|
||||||
|
}
|
||||||
|
return min_delay;
|
||||||
}
|
}
|
||||||
|
|||||||
41
src/stream.h
41
src/stream.h
@@ -22,53 +22,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdatomic.h>
|
#include <stdatomic.h>
|
||||||
|
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
#include "encoder.h"
|
#include "encoder.h"
|
||||||
|
|
||||||
|
|
||||||
struct worker_t {
|
|
||||||
pthread_t tid;
|
|
||||||
unsigned number;
|
|
||||||
atomic_bool *proc_stop;
|
|
||||||
atomic_bool *workers_stop;
|
|
||||||
|
|
||||||
pthread_mutex_t last_comp_time_mutex;
|
|
||||||
long double last_comp_time;
|
|
||||||
|
|
||||||
pthread_mutex_t has_job_mutex;
|
|
||||||
int buf_index;
|
|
||||||
atomic_bool has_job;
|
|
||||||
bool job_failed;
|
|
||||||
long double job_start_time;
|
|
||||||
pthread_cond_t has_job_cond;
|
|
||||||
|
|
||||||
pthread_mutex_t *free_workers_mutex;
|
|
||||||
unsigned *free_workers;
|
|
||||||
pthread_cond_t *free_workers_cond;
|
|
||||||
|
|
||||||
struct worker_t *order_prev;
|
|
||||||
struct worker_t *order_next;
|
|
||||||
|
|
||||||
struct device_t *dev;
|
|
||||||
struct encoder_t *encoder;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct workers_pool_t {
|
|
||||||
struct worker_t *workers;
|
|
||||||
atomic_bool workers_stop;
|
|
||||||
|
|
||||||
pthread_mutex_t free_workers_mutex;
|
|
||||||
unsigned free_workers;
|
|
||||||
pthread_cond_t free_workers_cond;
|
|
||||||
|
|
||||||
struct encoder_t *encoder;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct process_t {
|
struct process_t {
|
||||||
atomic_bool stop;
|
atomic_bool stop;
|
||||||
atomic_bool slowdown;
|
atomic_bool slowdown;
|
||||||
|
|||||||
@@ -25,8 +25,7 @@ import textwrap
|
|||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
def get_prepend() -> str:
|
C_PREPEND = textwrap.dedent("""
|
||||||
return textwrap.dedent("""
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
# #
|
# #
|
||||||
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
|
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import common
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
assert len(sys.argv) == 4, "%s <file.html> <file.h> <name>" % (sys.argv[0])
|
assert len(sys.argv) == 4, f"{sys.argv[0]} <file.html> <file.h> <name>"
|
||||||
html_path = sys.argv[1]
|
html_path = sys.argv[1]
|
||||||
header_path = sys.argv[2]
|
header_path = sys.argv[2]
|
||||||
name = sys.argv[3]
|
name = sys.argv[3]
|
||||||
@@ -42,11 +42,11 @@ def main() -> None:
|
|||||||
text = text.replace("%VERSION%", "\" VERSION \"")
|
text = text.replace("%VERSION%", "\" VERSION \"")
|
||||||
text = textwrap.indent(text, "\t", (lambda line: True))
|
text = textwrap.indent(text, "\t", (lambda line: True))
|
||||||
text = "\n".join(
|
text = "\n".join(
|
||||||
("%s \\" if line.strip() else "%s\\") % (line)
|
(f"{line} \\" if line.strip() else f"{line}\\")
|
||||||
for line in text.split("\n")
|
for line in text.split("\n")
|
||||||
)
|
)
|
||||||
text = "const char HTML_%s_PAGE[] = \" \\\n%s\n\";\n" % (name, text)
|
text = f"const char HTML_{name}_PAGE[] = \" \\\n{text}\n\";\n"
|
||||||
text = common.get_prepend() + "\n#include \"../../config.h\"\n\n\n" + text
|
text = f"{common.C_PREPEND}\n#include \"../../config.h\"\n\n\n{text}"
|
||||||
|
|
||||||
with open(header_path, "w") as header_file:
|
with open(header_path, "w") as header_file:
|
||||||
header_file.write(text)
|
header_file.write(text)
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ def _get_jpeg_size(data: bytes) -> Tuple[int, int]:
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
assert len(sys.argv) == 4, "%s <file.jpeg> <file.h> <name>" % (sys.argv[0])
|
assert len(sys.argv) == 4, f"{sys.argv[0]} <file.jpeg> <file.h> <name>"
|
||||||
jpeg_path = sys.argv[1]
|
jpeg_path = sys.argv[1]
|
||||||
header_path = sys.argv[2]
|
header_path = sys.argv[2]
|
||||||
name = sys.argv[3]
|
name = sys.argv[3]
|
||||||
@@ -74,13 +74,13 @@ def main() -> None:
|
|||||||
for ch in jpeg_data:
|
for ch in jpeg_data:
|
||||||
if len(rows[-1]) > 20:
|
if len(rows[-1]) > 20:
|
||||||
rows.append([])
|
rows.append([])
|
||||||
rows[-1].append("0x%.2X" % (ch))
|
rows[-1].append(f"0x{ch:02X}")
|
||||||
|
|
||||||
text = ",\n\t".join(", ".join(row) for row in rows)
|
text = ",\n\t".join(", ".join(row) for row in rows)
|
||||||
text = "const unsigned char %s_JPEG_DATA[] = {\n\t%s\n};\n" % (name, text)
|
text = f"const unsigned char {name}_JPEG_DATA[] = {{\n\t{text}\n}};\n"
|
||||||
text = "const unsigned %s_JPEG_HEIGHT = %d;\n\n" % (name, height) + text
|
text = f"const unsigned {name}_JPEG_HEIGHT = {height};\n\n{text}"
|
||||||
text = "const unsigned %s_JPEG_WIDTH = %d;\n" % (name, width) + text
|
text = f"const unsigned {name}_JPEG_WIDTH = {width};\n{text}"
|
||||||
text = common.get_prepend() + "\n\n" + text
|
text = f"{common.C_PREPEND}\n\n{text}"
|
||||||
|
|
||||||
with open(header_path, "w") as header_file:
|
with open(header_path, "w") as header_file:
|
||||||
header_file.write(text)
|
header_file.write(text)
|
||||||
|
|||||||
Reference in New Issue
Block a user