mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-02-19 08:16:31 +00:00
Compare commits
122 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e61d7c55e | ||
|
|
4e42c42bae | ||
|
|
b53e3edef1 | ||
|
|
0afbf02451 | ||
|
|
dd79efd6f5 | ||
|
|
97ac19a2fe | ||
|
|
755e0c2a2a | ||
|
|
ccab33a290 | ||
|
|
76a8e65e80 | ||
|
|
3b86e64222 | ||
|
|
020482a05a | ||
|
|
1896e22dff | ||
|
|
56df20fe84 | ||
|
|
ed7dabbfcb | ||
|
|
b1d40d1b3a | ||
|
|
43939c7475 | ||
|
|
8fa6db0be1 | ||
|
|
d57e9864a4 | ||
|
|
077f236a43 | ||
|
|
d57277877e | ||
|
|
1b0db859b2 | ||
|
|
d1d8c645a8 | ||
|
|
a54541ff10 | ||
|
|
e5a57ac2e0 | ||
|
|
da0e00252d | ||
|
|
526ae8c3e6 | ||
|
|
d5081b2c18 | ||
|
|
758b5558f9 | ||
|
|
97b2183038 | ||
|
|
2732482d36 | ||
|
|
ca52f12378 | ||
|
|
ab437d402b | ||
|
|
79d9214084 | ||
|
|
b5db16e1ba | ||
|
|
a68e27f09e | ||
|
|
4af8c6a121 | ||
|
|
693c89ae6b | ||
|
|
8be5d6d370 | ||
|
|
5a7f3d30a7 | ||
|
|
fb6331b64a | ||
|
|
77b5e6eabc | ||
|
|
ca07a9155b | ||
|
|
6cc202133e | ||
|
|
797e9427e5 | ||
|
|
b624ea2005 | ||
|
|
189aba1488 | ||
|
|
5f43fb7d0a | ||
|
|
86fada184c | ||
|
|
f0eece484f | ||
|
|
67f8e6665b | ||
|
|
6569e27312 | ||
|
|
4284ad3734 | ||
|
|
1c2f85cd59 | ||
|
|
2f83742f1d | ||
|
|
26d884c2da | ||
|
|
2d054fa9a8 | ||
|
|
f2863d1108 | ||
|
|
281e1b36ce | ||
|
|
160ad5c10f | ||
|
|
667137638b | ||
|
|
b5dffa927a | ||
|
|
651a82d1ca | ||
|
|
d963c95af8 | ||
|
|
7c524a1196 | ||
|
|
0c85aad5a2 | ||
|
|
5515654497 | ||
|
|
5955310bf3 | ||
|
|
a9df6da912 | ||
|
|
f3c56d5774 | ||
|
|
9a86793923 | ||
|
|
93c6248fdb | ||
|
|
ec738b18dc | ||
|
|
6029408564 | ||
|
|
8734834341 | ||
|
|
809f86955d | ||
|
|
2f557617d8 | ||
|
|
35c8196103 | ||
|
|
c71df1bb25 | ||
|
|
bc107d2870 | ||
|
|
294ed36b8f | ||
|
|
e01c7640b7 | ||
|
|
90125dcce4 | ||
|
|
a0c87c1c04 | ||
|
|
8924cdcac4 | ||
|
|
0d396e3f0a | ||
|
|
28daefc5ff | ||
|
|
895db6a8c9 | ||
|
|
73b894419a | ||
|
|
23b25634e8 | ||
|
|
b42a6c4124 | ||
|
|
f18a3ef992 | ||
|
|
5146314725 | ||
|
|
4fc59de042 | ||
|
|
494993fe39 | ||
|
|
e9ec65cfde | ||
|
|
ec2a704ca0 | ||
|
|
05b18fb68d | ||
|
|
32120ae12d | ||
|
|
386396dc84 | ||
|
|
3c086c2129 | ||
|
|
fb9a8e31b1 | ||
|
|
0c192ddcfb | ||
|
|
38b4246b9b | ||
|
|
a46ca0f702 | ||
|
|
ddf290e811 | ||
|
|
98b37cb48f | ||
|
|
d7ab090a69 | ||
|
|
1b160eb177 | ||
|
|
fad5bec927 | ||
|
|
ac036a04b5 | ||
|
|
cf5d4b1d2a | ||
|
|
102edd6880 | ||
|
|
1e87410035 | ||
|
|
993ef4e884 | ||
|
|
ea5ec96874 | ||
|
|
d76c5da4de | ||
|
|
4c07d047b4 | ||
|
|
609fa89d09 | ||
|
|
ce26bd0a65 | ||
|
|
74699b63e7 | ||
|
|
43800a5ed6 | ||
|
|
0318f33479 |
@@ -1,7 +1,7 @@
|
||||
[bumpversion]
|
||||
commit = True
|
||||
tag = True
|
||||
current_version = 0.6
|
||||
current_version = 0.41
|
||||
parse = (?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\d+)(\-(?P<release>[a-z]+))?)?
|
||||
serialize =
|
||||
{major}.{minor}
|
||||
|
||||
10
Makefile
10
Makefile
@@ -3,8 +3,10 @@ PREFIX ?= /usr/local
|
||||
CFLAGS ?= -O3
|
||||
LDFLAGS ?=
|
||||
|
||||
|
||||
# =====
|
||||
CC = gcc
|
||||
LIBS = -lm -ljpeg -pthread -levent -levent_pthreads
|
||||
LIBS = -lm -ljpeg -pthread -levent -levent_pthreads -luuid
|
||||
override CFLAGS += -c -std=c99 -Wall -Wextra -D_GNU_SOURCE
|
||||
SOURCES = $(shell ls src/*.c src/jpeg/*.c)
|
||||
OBJECTS = $(SOURCES:.c=.o)
|
||||
@@ -18,6 +20,7 @@ override CFLAGS += -DOMX_ENCODER -DOMX_SKIP64BIT -I/opt/vc/include
|
||||
endif
|
||||
|
||||
|
||||
# =====
|
||||
all: $(SOURCES) $(PROG)
|
||||
|
||||
|
||||
@@ -26,7 +29,8 @@ install: $(PROG)
|
||||
|
||||
|
||||
regen:
|
||||
tools/make-jpg-h.py src/data/blank.jpg src/data/blank.h BLANK 640 480
|
||||
tools/make-jpeg-h.py src/data/blank.jpeg src/data/blank_jpeg.h BLANK 640 480
|
||||
tools/make-html-h.py src/data/index.html src/data/index_html.h HTML_INDEX_PAGE
|
||||
|
||||
|
||||
$(PROG): $(OBJECTS)
|
||||
@@ -53,7 +57,7 @@ push:
|
||||
git push
|
||||
git push --tags
|
||||
|
||||
|
||||
clean-all: clean
|
||||
clean:
|
||||
rm -f src/*.o src/{jpeg,omx}/*.o vgcore.* $(PROG)
|
||||
rm -rf pkg src/$(PROG)-* src/v*.tar.gz v*.tar.gz $(PROG)-*.pkg.tar.xz
|
||||
|
||||
4
PKGBUILD
4
PKGBUILD
@@ -3,13 +3,13 @@
|
||||
|
||||
|
||||
pkgname=ustreamer
|
||||
pkgver=0.6
|
||||
pkgver=0.41
|
||||
pkgrel=1
|
||||
pkgdesc="Lightweight and fast MJPG-HTTP streamer"
|
||||
url="https://github.com/pi-kvm/ustreamer"
|
||||
license=(GPL)
|
||||
arch=(i686 x86_64 armv6h armv7h)
|
||||
depends=(libjpeg libevent)
|
||||
depends=(libjpeg libevent libutil-linux)
|
||||
# optional: raspberrypi-firmware for OMX JPEG compressor
|
||||
makedepends=(gcc make)
|
||||
source=("$url/archive/v$pkgver.tar.gz")
|
||||
|
||||
83
README.md
Normal file
83
README.md
Normal file
@@ -0,0 +1,83 @@
|
||||
# µStreamer
|
||||
[[Русская версия]](README.ru.md)
|
||||
|
||||
µStreamer is a lightweight and very quick server to broadcast [MJPG](https://en.wikipedia.org/wiki/Motion_JPEG) video from any V4L2 device to the net. All new browsers have native support of this video format, as well as most video players such as mplayer, VLC etc.
|
||||
µStreamer is a part of the [Pi-KVM](https://github.com/pi-kvm) project designed to stream [VGA](https://www.amazon.com/dp/B0126O0RDC) and [HDMI](https://auvidea.com/b101-hdmi-to-csi-2-bridge-15-pin-fpc/) screencast hardware data with the highest resolution and FPS possible.
|
||||
|
||||
µStreamer is very similar to [mjpg-streamer](https://github.com/jacksonliam/mjpg-streamer) with ```input_uvc.so``` and ```output_http.so``` plugins, however, there are some major differences. The key ones are:
|
||||
|
||||
| **Feature** | **µStreamer** | **mjpg-streamer** |
|
||||
|----------|---------------|-------------------|
|
||||
| Multithreaded JPEG encoding |  Yes |  No |
|
||||
| [OpenMAX IL](https://www.khronos.org/openmaxil) hardware acceleration<br>on Raspberry Pi |  Yes |  No |
|
||||
| Behavior when the device<br>is disconnected while streaming |  Shows a black screen<br>with ```NO SIGNAL``` on it<br>until reconnected |  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 |  Yes |  Partially yes <sup>1</sup> |
|
||||
| Option to skip frames when streaming<br>static images by HTTP to save traffic |  Yes <sup>2</sup> |  No |
|
||||
| Debug logs without recompiling,<br>performance statistics log,<br>access to HTTP broadcast parameters |  Yes |  No |
|
||||
| Supported input devices |  YUYV, UYVY,<br>RGB565, ~~MJPG~~ <sup>3</sup> |  YUYV, UYVY,<br>RGB565, MJPG |
|
||||
| Access to webcam controls (focus, servos)<br>and settings such as brightness via HTTP |  No |  Yes |
|
||||
| Option to serve files<br>with a built-in HTTP server, auth settings |  No <sup>4</sup> |  Yes |
|
||||
|
||||
Footnotes:
|
||||
* ```1``` Long before µStreamer, I made a [patch](https://github.com/jacksonliam/mjpg-streamer/pull/164) to add DV-timings support to mjpg-streamer and to keep it from hanging up no device disconnection. Alas, the patch is far from perfect and I can't guarantee it will work every time - mjpg-streamer's source code is very complicated and its structure is hard to understand. With this in mind, along with needing multithreading and JPEG hardware acceleration in the future, I decided to make my own stream server from scratch instead of supporting legacy code.
|
||||
|
||||
* ```2``` This feature allows to cut down outgoing traffic several-fold when broadcasting HDMI, but it increases CPU usage a little bit. The idea is that HDMI is a fully digital interface and each captured frame can be identical to the previous one byte-wise. There's no need to broadcast the same image over the net several times a second. With the `--drop-same-frames=20` option enabled, µStreamer will drop all the matching frames (with a limit of 20 in a row). Each new frame is matched with the previous one first by length, then using ```memcmp()```.
|
||||
|
||||
* ```3``` As µStreamer was made mainly to be used with screencast hardware it supports video formats they usually need. MJPG input means that the screencast device can compress images to JPEG and feed it to the software, which allows to cut down CPU usage and avoid software image encoding. This video format is supported by most webcams, but I've never seen it supported by screencast hardware: neither [Auvidea B101](https://auvidea.com/b101-hdmi-to-csi-2-bridge-15-pin-fpc/), nor [EasyCap UTV 007](https://www.amazon.com/dp/B0126O0RDC) offer such support. It's not hard to add hardware MJPG sources support, it's just not done yet.
|
||||
|
||||
* ```4``` ...and there'll never be. µStreamer is designed UNIX-way, so if you need a small website with your broadcast, install NGINX.
|
||||
|
||||
-----
|
||||
# TL;DR
|
||||
If you're going to live-stream from your backyard webcam and need to control it, use mjpg-streamer. If you need a high-quality image with high FPS - µStreamer for the win.
|
||||
|
||||
-----
|
||||
# Building
|
||||
You'll need ```make```, ```gcc```, ```libevent``` with ```pthreads``` support, ```libjpeg8```/```libjpeg-turbo``` and ```libuuid```.
|
||||
|
||||
It should compile automatically with OpenMAX IL on Raspberry Pi, if the corresponding headers are present in ```/opt/vc/include```.
|
||||
|
||||
```
|
||||
$ git clone --depth=1 https://github.com/pi-kvm/ustreamer
|
||||
$ cd ustreamer
|
||||
$ make
|
||||
$ ./ustreamer --help
|
||||
```
|
||||
|
||||
AUR has a package for Arch Linux: https://aur.archlinux.org/packages/ustreamer
|
||||
|
||||
-----
|
||||
# Usage
|
||||
Without arguments, ```ustreamer``` will try to open ```/dev/video0``` with 640x480 resolution and start broadcasting on ```http://127.0.0.1:8080```. You can override this behavior using parameters ```--device```, ```--host``` and ```--port```. For example, to broadcast to the world, run:
|
||||
```
|
||||
# ./ustreamer --device=/dev/video1 --host=0.0.0.0 --port=80
|
||||
```
|
||||
|
||||
The recommended way of running µStreamer with [Auvidea B101](https://www.raspberrypi.org/forums/viewtopic.php?f=38&t=120702&start=400#p1339178) on Raspberry Pi:
|
||||
```bash
|
||||
$ ./ustreamer \
|
||||
--format=uyvy \ # Device input format
|
||||
--encoder=omx \ # Hardware encoding with OpenMAX
|
||||
--dv-timings \ # Use DV-timings
|
||||
--quality=20 \ # OpenMAX has a non-linear quality scale
|
||||
--drop-same-frames=30 # Save that traffic
|
||||
```
|
||||
|
||||
You can always view the full list of options with ```ustreamer --help```.
|
||||
|
||||
-----
|
||||
# License
|
||||
Copyright (C) 2018 by 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/.
|
||||
83
README.ru.md
Normal file
83
README.ru.md
Normal file
@@ -0,0 +1,83 @@
|
||||
# µStreamer
|
||||
[[English version]](README.md)
|
||||
|
||||
|
||||
µStreamer - это маленький и очень быстрый сервер, который позволяет организовать трансляцию видео в формате [MJPG](https://en.wikipedia.org/wiki/Motion_JPEG) с любого устройства V4L2 в сеть. Этот формат нативно поддерживается всеми современными браузерами и большинством приложений для просмотра видео (mplayer, VLC и так далее). µStreamer был разработан в рамках проекта [Pi-KVM](https://github.com/pi-kvm) специально для стриминга с устройств видеозахвата [VGA](https://www.amazon.com/dp/B0126O0RDC) и [HDMI](https://auvidea.com/b101-hdmi-to-csi-2-bridge-15-pin-fpc/) с максимально возможным разрешением и FPS, которые только позволяет железо.
|
||||
|
||||
Функционально µStreamer очень похож на [mjpg-streamer](https://github.com/jacksonliam/mjpg-streamer) при использовании им плагинов ```input_uvc.so``` и ```output_http.so```, однако имеет ряд серьезных отличий. Основные приведены в этой таблице:
|
||||
|
||||
| **Фича** | **µStreamer** | **mjpg-streamer** |
|
||||
|----------|---------------|-------------------|
|
||||
| Многопоточное кодирование JPEG |  Есть |  Нет |
|
||||
| Аппаратное кодирование с помощью [OpenMAX IL](https://www.khronos.org/openmaxil) на Raspberry Pi |  Есть |  Нет |
|
||||
| Поведение при физическом отключении устройства<br>от сервера во время работы |  Транслирует черный экран<br>с надписью ```NO SIGNAL```,<br>пока устройство не будет подключено снова |  Прерывает трансляцию <sup>1</sup> |
|
||||
| Поддержка [DV-таймингов](https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/dv-timings.html) - возможности изменения <br>параметров разрешения трансляции на лету<br>по сигналу источника (устройства видеозахвата) |  Есть |  Условно есть <sup>1</sup> |
|
||||
| Возможность пропуска фреймов при передаче<br>статического изображения по HTTP<br>для экономии трафика |  Есть <sup>2</sup> |  Нет |
|
||||
| Дебаг-логи без перекомпиляции,<br>логгирование статистики производительности,<br>возможность получения параметров<br>трансляции по HTTP |  Есть |  Нет |
|
||||
| Поддерживаемые входные форматы устройств |  YUYV, UYVY,<br>RGB565, ~~MJPG~~ <sup>3</sup> |  YUYV, UYVY,<br>RGB565, MJPG |
|
||||
| Поддержка контролов веб-камер (фокус,<br> движение сервами) и всяких настроек,<br> типа яркости, через HTTP |  Нет |  Есть |
|
||||
| Возможность сервить файлы встроенным<br>HTTP-сервером, настройки авторизации |  Нет <sup>4</sup> |  Есть |
|
||||
|
||||
Сносочки:
|
||||
* ```1``` Еще до написания µStreamer, я запилил [патч](https://github.com/jacksonliam/mjpg-streamer/pull/164), добавляющий в mjpg-streamer поддержку DV-таймингов и предотвращающий его зависание при отключении устройства. Однако патч, увы, далек от совершенства и я не гарантирую его стопроцентную работоспособность, поскольку код mjpg-streamer чрезвычайно запутан и очень плохо структурирован. Учитывая это, а также то, что в дальнейшем мне потребовались многопоточность и аппаратное кодирование JPEG, было принято решение написать свой стрим-сервер с нуля, чтобы не тратить силы на поддержку лишнего легаси.
|
||||
|
||||
* ```2``` Это фича позволяет в несколько раз снизить объем исходящего трафика при трансляции HDMI, однако немного увеличивает загрузку процессора. Суть в том, что HDMI - полностью цифровой интерфейс, и новый захваченный фрейм может быть идентичен предыдущему в точности до байта. В этом случае нет нужды передавать одну и ту же картинку по сети несколько раз в секунду. При использовании опции `--drop-same-frames=20`, µStreamer будет дропать все одинаковые фреймы, но не более 20 подряд. Новый фрейм сравнивается с предыдущим сначала по длине, а затем помощью ```memcmp()```.
|
||||
|
||||
* ```3``` Поскольку µStreamer писался в первую очередь для устройств видеозахвата, в нем реализованы только те форматы, которые для них были нужны. MJPG в контексте входных данных означает, что устройство умеет самостоятельно сжимать картинку в JPEG и отдавать ее программе, что позволяет значительно снизить загрузку процессора и избавить его от необходимости кодировать картинку софтом. Этот формат поддерживается большинством веб-камер, но не поддерживается ни одним из встреченных мной устройств видеозахвата; его не умеет ни [Auvidea B101](https://auvidea.com/b101-hdmi-to-csi-2-bridge-15-pin-fpc/), ни [EasyCap UTV 007](https://www.amazon.com/dp/B0126O0RDC). Нет никаких технических сложностей добавить поддержку аппаратного MJPG источника, но у меня просто пока не дошли до этого руки.
|
||||
|
||||
* ```4``` ... и не будет. µStreamer придерживается концепции UNIX-way, так что если вам нужно нарисовать маленький сайтик со встроенной трансляцией - просто поставьте NGINX.
|
||||
|
||||
-----
|
||||
# TL;DR
|
||||
Если вам нужно вещать стрим с уличной камеры и управлять ее параметрами - возьмите mjpg-streamer. Если же вам нужно очень качественное изображение с высоким FPS - µStreamer ваш бро.
|
||||
|
||||
-----
|
||||
# Сборка
|
||||
Для сборки вам понадобятся ```make```, ```gcc```, ```libevent``` с поддержкой ```pthreads```, ```libjpeg8```/```libjpeg-turbo``` и ```libuuid```.
|
||||
|
||||
На Raspberry Pi програма автоматически собирается с поддержкой OpenMAX IL, если обнаружит нужные хедеры в ```/opt/vc/include```.
|
||||
|
||||
```
|
||||
$ git clone --depth=1 https://github.com/pi-kvm/ustreamer
|
||||
$ cd ustreamer
|
||||
$ make
|
||||
$ ./ustreamer --help
|
||||
```
|
||||
|
||||
Для Arch Linux в AUR есть готовый пакет: https://aur.archlinux.org/packages/ustreamer
|
||||
|
||||
-----
|
||||
# Использование
|
||||
Будучи запущенным без аргументов, ```ustreamer``` попробует открыть устройство ```/dev/video0``` с разрешением 640x480 и начать трансляцию на ```http://127.0.0.1:8080```. Это поведение может быть изменено с помощью опций ```--device```, ```--host``` и ```--port```. Пример вещания на всю сеть по 80-м порту:
|
||||
```
|
||||
# ./ustreamer --device=/dev/video1 --host=0.0.0.0 --port=80
|
||||
```
|
||||
|
||||
Рекомендуемый способ запуска µStreamer для работы с [Auvidea B101](https://www.raspberrypi.org/forums/viewtopic.php?f=38&t=120702&start=400#p1339178) на Raspberry Pi:
|
||||
```bash
|
||||
$ ./ustreamer \
|
||||
--format=uyvy \ # Настройка входного формата устройства
|
||||
--encoder=omx \ # Использование аппаратного кодирования с помощью OpenMAX
|
||||
--dv-timings \ # Включение DV-таймингов
|
||||
--quality=20 \ # У OpenMAX нелинейная шкала качества
|
||||
--drop-same-frames=30 # Экономим трафик
|
||||
```
|
||||
|
||||
За полным списком опций обращайтесь ко встроенной справке: ```ustreamer --help```.
|
||||
|
||||
-----
|
||||
# Лицензия
|
||||
Copyright (C) 2018 by 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/.
|
||||
@@ -21,4 +21,4 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#define VERSION "0.6"
|
||||
#define VERSION "0.41"
|
||||
|
||||
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
@@ -685,4 +685,4 @@ const unsigned char BLANK_JPG_DATA[] = {
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x7, 0xff, 0xd9
|
||||
};
|
||||
};
|
||||
59
src/data/index.html
Normal file
59
src/data/index.html
Normal file
@@ -0,0 +1,59 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>uStreamer</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h3>µStreamer v%VERSION%</h3>
|
||||
<hr>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="/state"><b><samp>/state</samp></b></a><br>
|
||||
Get JSON structure with state of the server.
|
||||
</li>
|
||||
<br>
|
||||
<li>
|
||||
<a href="/snapshot"><b><samp>/snapshot</samp></b></a><br>
|
||||
Get a current actual image from the server.
|
||||
</li>
|
||||
<br>
|
||||
<li>
|
||||
<a href="/stream"><b><samp>/stream</samp></b></a><br>
|
||||
Get a live stream. Query params:<br>
|
||||
<br>
|
||||
<ul>
|
||||
<li>
|
||||
<b><samp>key=abc123</samp></b><br>
|
||||
User-defined key, which is part of cookie <samp>stream_client</samp>, which allows<br>
|
||||
the stream client to determine its identifier and view statistics using <a href="/state"><samp>/state</samp></a>.
|
||||
</li>
|
||||
<br>
|
||||
<li>
|
||||
<b><samp>extra_headers=1</samp></b><br>
|
||||
Add <samp>X-UStreamer-*</samp> headers to /stream handle (like on <a href="/snapshot"><samp>/snapshot</samp></a>).
|
||||
</li>
|
||||
<br>
|
||||
<li>
|
||||
<b><samp>advance_headers=1</samp></b><br>
|
||||
Enable workaround for Chromium/Blink
|
||||
<a href="https://bugs.chromium.org/p/chromium/issues/detail?id=527446">Bug #527446</a>.
|
||||
</li>
|
||||
<br>
|
||||
<li>
|
||||
<b><samp>dual_final_frames=1</samp></b><br>
|
||||
Enable workaround for Safari/WebKit bug when using option <samp>--drop-same-frames</samp>.<br>
|
||||
Without this option, when the frame series is completed, WebKit-based browsers<br>
|
||||
renders the last frame with a delay.
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
<br>
|
||||
</ul>
|
||||
<br>
|
||||
<hr>
|
||||
<a href="https://github.com/pi-kvm/ustreamer">Sources & docs</a>
|
||||
</body>
|
||||
</html>
|
||||
87
src/data/index_html.h
Normal file
87
src/data/index_html.h
Normal file
@@ -0,0 +1,87 @@
|
||||
/*****************************************************************************
|
||||
# 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 "../config.h"
|
||||
|
||||
|
||||
const char *HTML_INDEX_PAGE = " \
|
||||
<!DOCTYPE html> \
|
||||
\
|
||||
<html> \
|
||||
<head> \
|
||||
<meta charset=\"utf-8\" /> \
|
||||
<title>uStreamer</title> \
|
||||
</head> \
|
||||
\
|
||||
<body> \
|
||||
<h3>µStreamer v" VERSION "</h3> \
|
||||
<hr> \
|
||||
<ul> \
|
||||
<li> \
|
||||
<a href=\"/state\"><b><samp>/state</samp></b></a><br> \
|
||||
Get JSON structure with state of the server. \
|
||||
</li> \
|
||||
<br> \
|
||||
<li> \
|
||||
<a href=\"/snapshot\"><b><samp>/snapshot</samp></b></a><br> \
|
||||
Get a current actual image from the server. \
|
||||
</li> \
|
||||
<br> \
|
||||
<li> \
|
||||
<a href=\"/stream\"><b><samp>/stream</samp></b></a><br> \
|
||||
Get a live stream. Query params:<br> \
|
||||
<br> \
|
||||
<ul> \
|
||||
<li> \
|
||||
<b><samp>key=abc123</samp></b><br> \
|
||||
User-defined key, which is part of cookie <samp>stream_client</samp>, which allows<br> \
|
||||
the stream client to determine its identifier and view statistics using <a href=\"/state\"><samp>/state</samp></a>. \
|
||||
</li> \
|
||||
<br> \
|
||||
<li> \
|
||||
<b><samp>extra_headers=1</samp></b><br> \
|
||||
Add <samp>X-UStreamer-*</samp> headers to /stream handle (like on <a href=\"/snapshot\"><samp>/snapshot</samp></a>). \
|
||||
</li> \
|
||||
<br> \
|
||||
<li> \
|
||||
<b><samp>advance_headers=1</samp></b><br> \
|
||||
Enable workaround for Chromium/Blink \
|
||||
<a href=\"https://bugs.chromium.org/p/chromium/issues/detail?id=527446\">Bug #527446</a>. \
|
||||
</li> \
|
||||
<br> \
|
||||
<li> \
|
||||
<b><samp>dual_final_frames=1</samp></b><br> \
|
||||
Enable workaround for Safari/WebKit bug when using option <samp>--drop-same-frames</samp>.<br> \
|
||||
Without this option, when the frame series is completed, WebKit-based browsers<br> \
|
||||
renders the last frame with a delay. \
|
||||
</li> \
|
||||
</ul> \
|
||||
</li> \
|
||||
<br> \
|
||||
</ul> \
|
||||
<br> \
|
||||
<hr> \
|
||||
<a href=\"https://github.com/pi-kvm/ustreamer\">Sources & docs</a> \
|
||||
</body> \
|
||||
</html> \
|
||||
";
|
||||
50
src/device.c
50
src/device.c
@@ -39,7 +39,7 @@
|
||||
static const struct {
|
||||
const char *name;
|
||||
const v4l2_std_id standard;
|
||||
} STANDARDS[] = {
|
||||
} _STANDARDS[] = {
|
||||
{"UNKNOWN", V4L2_STD_UNKNOWN},
|
||||
{"PAL", V4L2_STD_PAL},
|
||||
{"NTSC", V4L2_STD_NTSC},
|
||||
@@ -49,7 +49,7 @@ static const struct {
|
||||
static const struct {
|
||||
const char *name;
|
||||
const unsigned format;
|
||||
} FORMATS[] = {
|
||||
} _FORMATS[] = {
|
||||
{"YUYV", V4L2_PIX_FMT_YUYV},
|
||||
{"UYVY", V4L2_PIX_FMT_UYVY},
|
||||
{"RGB565", V4L2_PIX_FMT_RGB565},
|
||||
@@ -84,9 +84,8 @@ struct device_t *device_init() {
|
||||
dev->standard = V4L2_STD_UNKNOWN;
|
||||
dev->n_buffers = max_u(sysconf(_SC_NPROCESSORS_ONLN), 1) + 1;
|
||||
dev->n_workers = dev->n_buffers;
|
||||
dev->jpeg_quality = 80;
|
||||
dev->timeout = 1;
|
||||
dev->error_timeout = 1;
|
||||
dev->error_delay = 1;
|
||||
dev->run = run;
|
||||
return dev;
|
||||
}
|
||||
@@ -97,18 +96,18 @@ void device_destroy(struct device_t *dev) {
|
||||
}
|
||||
|
||||
int device_parse_format(const char *const str) {
|
||||
for (unsigned index = 0; index < sizeof(FORMATS) / sizeof(FORMATS[0]); ++index) {
|
||||
if (!strcasecmp(str, FORMATS[index].name)) {
|
||||
return FORMATS[index].format;
|
||||
for (unsigned index = 0; index < sizeof(_FORMATS) / sizeof(_FORMATS[0]); ++index) {
|
||||
if (!strcasecmp(str, _FORMATS[index].name)) {
|
||||
return _FORMATS[index].format;
|
||||
}
|
||||
}
|
||||
return FORMAT_UNKNOWN;
|
||||
}
|
||||
|
||||
v4l2_std_id device_parse_standard(const char *const str) {
|
||||
for (unsigned index = 1; index < sizeof(STANDARDS) / sizeof(STANDARDS[0]); ++index) {
|
||||
if (!strcasecmp(str, STANDARDS[index].name)) {
|
||||
return STANDARDS[index].standard;
|
||||
for (unsigned index = 1; index < sizeof(_STANDARDS) / sizeof(_STANDARDS[0]); ++index) {
|
||||
if (!strcasecmp(str, _STANDARDS[index].name)) {
|
||||
return _STANDARDS[index].standard;
|
||||
}
|
||||
}
|
||||
return STANDARD_UNKNOWN;
|
||||
@@ -138,8 +137,6 @@ int device_open(struct device_t *dev) {
|
||||
}
|
||||
_device_open_alloc_picbufs(dev);
|
||||
|
||||
dev->run->n_workers = dev->n_workers;
|
||||
|
||||
LOG_DEBUG("Device fd=%d initialized", dev->run->fd);
|
||||
return 0;
|
||||
|
||||
@@ -186,6 +183,7 @@ void device_close(struct device_t *dev) {
|
||||
|
||||
static int _device_open_check_cap(struct device_t *dev) {
|
||||
struct v4l2_capability cap;
|
||||
int input = dev->input; // Needs pointer to int for ioctl()
|
||||
|
||||
MEMSET_ZERO(cap);
|
||||
|
||||
@@ -205,10 +203,16 @@ static int _device_open_check_cap(struct device_t *dev) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
LOG_INFO("Using input channel: %d", input);
|
||||
if (xioctl(dev->run->fd, VIDIOC_S_INPUT, &input) < 0) {
|
||||
LOG_ERROR("Can't set input channel");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dev->standard != V4L2_STD_UNKNOWN) {
|
||||
LOG_INFO("Using TV standard: %s", _standard_to_string(dev->standard));
|
||||
if (xioctl(dev->run->fd, VIDIOC_S_STD, &dev->standard) < 0) {
|
||||
LOG_PERROR("Can't set video standard");
|
||||
LOG_ERROR("Can't set video standard");
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
@@ -251,7 +255,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_timings) == 0) {
|
||||
LOG_INFO(
|
||||
"Got new DV timings: resolution=%dx%d; pixclk=%llu\n",
|
||||
"Got new DV timings: resolution=%dx%d; pixclk=%llu",
|
||||
dv_timings.bt.width,
|
||||
dv_timings.bt.height,
|
||||
dv_timings.bt.pixelclock
|
||||
@@ -409,9 +413,9 @@ static void _device_open_alloc_picbufs(struct device_t *dev) {
|
||||
LOG_DEBUG("Allocating picture buffers ...");
|
||||
A_CALLOC(dev->run->pictures, dev->run->n_buffers);
|
||||
|
||||
dev->run->max_picture_size = (dev->run->width * dev->run->height) << 1;
|
||||
dev->run->max_picture_size = ((dev->run->width * dev->run->height) << 1) * 2;
|
||||
for (unsigned index = 0; index < dev->run->n_buffers; ++index) {
|
||||
LOG_DEBUG("Allocating picture buffer %d ...", index);
|
||||
LOG_DEBUG("Allocating picture buffer %d sized %lu bytes... ", index, dev->run->max_picture_size);
|
||||
A_CALLOC(dev->run->pictures[index].data, dev->run->max_picture_size);
|
||||
dev->run->pictures[index].allocated = dev->run->max_picture_size;
|
||||
}
|
||||
@@ -435,19 +439,19 @@ static const char *_format_to_string_auto(char *buf, const size_t size, const un
|
||||
}
|
||||
|
||||
static const char *_format_to_string_null(const unsigned format) {
|
||||
for (unsigned index = 0; index < sizeof(FORMATS) / sizeof(FORMATS[0]); ++index) {
|
||||
if (format == FORMATS[index].format) {
|
||||
return FORMATS[index].name;
|
||||
for (unsigned index = 0; index < sizeof(_FORMATS) / sizeof(_FORMATS[0]); ++index) {
|
||||
if (format == _FORMATS[index].format) {
|
||||
return _FORMATS[index].name;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *_standard_to_string(v4l2_std_id standard) {
|
||||
for (unsigned index = 0; index < sizeof(STANDARDS) / sizeof(STANDARDS[0]); ++index) {
|
||||
if (standard == STANDARDS[index].standard) {
|
||||
return STANDARDS[index].name;
|
||||
for (unsigned index = 0; index < sizeof(_STANDARDS) / sizeof(_STANDARDS[0]); ++index) {
|
||||
if (standard == _STANDARDS[index].standard) {
|
||||
return _STANDARDS[index].name;
|
||||
}
|
||||
}
|
||||
return STANDARDS[0].name;
|
||||
return _STANDARDS[0].name;
|
||||
}
|
||||
|
||||
10
src/device.h
10
src/device.h
@@ -44,6 +44,9 @@ struct picture_t {
|
||||
unsigned char *data;
|
||||
unsigned long size;
|
||||
unsigned long allocated;
|
||||
long double grab_time;
|
||||
long double encode_begin_time;
|
||||
long double encode_end_time;
|
||||
};
|
||||
|
||||
struct device_runtime_t {
|
||||
@@ -61,6 +64,7 @@ struct device_runtime_t {
|
||||
|
||||
struct device_t {
|
||||
char *path;
|
||||
unsigned input;
|
||||
unsigned width;
|
||||
unsigned height;
|
||||
unsigned format;
|
||||
@@ -68,11 +72,11 @@ struct device_t {
|
||||
bool dv_timings;
|
||||
unsigned n_buffers;
|
||||
unsigned n_workers;
|
||||
unsigned every_frame;
|
||||
unsigned desired_fps;
|
||||
unsigned min_frame_size;
|
||||
unsigned jpeg_quality;
|
||||
bool persistent;
|
||||
unsigned timeout;
|
||||
unsigned error_timeout;
|
||||
unsigned error_delay;
|
||||
|
||||
struct device_runtime_t *run;
|
||||
sig_atomic_t volatile stop;
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
static const struct {
|
||||
const char *name;
|
||||
const enum encoder_type_t type;
|
||||
} ENCODER_TYPES[] = {
|
||||
} _ENCODER_TYPES[] = {
|
||||
{"CPU", ENCODER_TYPE_CPU},
|
||||
# ifdef OMX_ENCODER
|
||||
{"OMX", ENCODER_TYPE_OMX},
|
||||
@@ -50,20 +50,39 @@ struct encoder_t *encoder_init() {
|
||||
|
||||
A_CALLOC(encoder, 1);
|
||||
encoder->type = ENCODER_TYPE_CPU;
|
||||
encoder->quality = 80;
|
||||
return encoder;
|
||||
}
|
||||
|
||||
void encoder_prepare(struct encoder_t *encoder) {
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
#pragma GCC diagnostic push
|
||||
void encoder_prepare(struct encoder_t *encoder, struct device_t *dev) {
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
assert(encoder->type != ENCODER_TYPE_UNKNOWN);
|
||||
|
||||
if (encoder->type != ENCODER_TYPE_CPU) {
|
||||
LOG_DEBUG("Initializing encoder ...");
|
||||
}
|
||||
|
||||
LOG_INFO("Using JPEG quality: %d%%", encoder->quality);
|
||||
|
||||
# ifdef OMX_ENCODER
|
||||
if (encoder->type == ENCODER_TYPE_OMX) {
|
||||
if ((encoder->omx = omx_encoder_init()) == NULL) {
|
||||
goto use_fallback;
|
||||
if (dev->n_workers > OMX_MAX_ENCODERS) {
|
||||
LOG_INFO(
|
||||
"OMX-based encoder can only work with %u worker threads; forced --workers=%u",
|
||||
OMX_MAX_ENCODERS, OMX_MAX_ENCODERS
|
||||
);
|
||||
dev->n_workers = OMX_MAX_ENCODERS;
|
||||
}
|
||||
encoder->n_omxs = dev->n_workers;
|
||||
|
||||
A_CALLOC(encoder->omxs, encoder->n_omxs);
|
||||
for (unsigned index = 0; index < encoder->n_omxs; ++index) {
|
||||
if ((encoder->omxs[index] = omx_encoder_init()) == NULL) {
|
||||
goto use_fallback;
|
||||
}
|
||||
}
|
||||
}
|
||||
# endif
|
||||
@@ -80,17 +99,22 @@ void encoder_prepare(struct encoder_t *encoder) {
|
||||
|
||||
void encoder_destroy(struct encoder_t *encoder) {
|
||||
# ifdef OMX_ENCODER
|
||||
if (encoder->omx) {
|
||||
omx_encoder_destroy(encoder->omx);
|
||||
if (encoder->omxs) {
|
||||
for (unsigned index = 0; index < encoder->n_omxs; ++index) {
|
||||
if (encoder->omxs[index]) {
|
||||
omx_encoder_destroy(encoder->omxs[index]);
|
||||
}
|
||||
}
|
||||
free(encoder->omxs);
|
||||
}
|
||||
# endif
|
||||
free(encoder);
|
||||
}
|
||||
|
||||
enum encoder_type_t encoder_parse_type(const char *const str) {
|
||||
for (unsigned index = 0; index < sizeof(ENCODER_TYPES) / sizeof(ENCODER_TYPES[0]); ++index) {
|
||||
if (!strcasecmp(str, ENCODER_TYPES[index].name)) {
|
||||
return ENCODER_TYPES[index].type;
|
||||
for (unsigned index = 0; index < sizeof(_ENCODER_TYPES) / sizeof(_ENCODER_TYPES[0]); ++index) {
|
||||
if (!strcasecmp(str, _ENCODER_TYPES[index].name)) {
|
||||
return _ENCODER_TYPES[index].type;
|
||||
}
|
||||
}
|
||||
return ENCODER_TYPE_UNKNOWN;
|
||||
@@ -98,18 +122,16 @@ enum encoder_type_t encoder_parse_type(const char *const str) {
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
#pragma GCC diagnostic push
|
||||
void encoder_prepare_for_device(struct encoder_t *encoder, struct device_t *dev) {
|
||||
void encoder_prepare_live(struct encoder_t *encoder, struct device_t *dev) {
|
||||
assert(encoder->type != ENCODER_TYPE_UNKNOWN);
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
# ifdef OMX_ENCODER
|
||||
if (encoder->type == ENCODER_TYPE_OMX) {
|
||||
if (omx_encoder_prepare_for_device(encoder->omx, dev) < 0) {
|
||||
goto use_fallback;
|
||||
}
|
||||
if (dev->run->n_workers > 1) {
|
||||
LOG_INFO("OMX encoder can only work with one worker thread; forcing n_workers to 1");
|
||||
dev->run->n_workers = 1;
|
||||
for (unsigned index = 0; index < encoder->n_omxs; ++index) {
|
||||
if (omx_encoder_prepare_live(encoder->omxs[index], dev, encoder->quality) < 0) {
|
||||
goto use_fallback;
|
||||
}
|
||||
}
|
||||
}
|
||||
# endif
|
||||
@@ -121,19 +143,23 @@ void encoder_prepare_for_device(struct encoder_t *encoder, struct device_t *dev)
|
||||
use_fallback:
|
||||
LOG_ERROR("Can't prepare selected encoder, falling back to CPU");
|
||||
encoder->type = ENCODER_TYPE_CPU;
|
||||
dev->run->n_workers = dev->n_workers;
|
||||
# pragma GCC diagnostic pop
|
||||
}
|
||||
|
||||
int encoder_compress_buffer(struct encoder_t *encoder, struct device_t *dev, int index) {
|
||||
#pragma GCC diagnostic ignored "-Wunused-label"
|
||||
#pragma GCC diagnostic push
|
||||
int encoder_compress_buffer(struct encoder_t *encoder, struct device_t *dev,
|
||||
const unsigned worker_number, const unsigned buf_index) {
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
assert(encoder->type != ENCODER_TYPE_UNKNOWN);
|
||||
|
||||
if (encoder->type == ENCODER_TYPE_CPU) {
|
||||
jpeg_encoder_compress_buffer(dev, index);
|
||||
jpeg_encoder_compress_buffer(dev, buf_index, encoder->quality);
|
||||
}
|
||||
# ifdef OMX_ENCODER
|
||||
else if (encoder->type == ENCODER_TYPE_OMX) {
|
||||
if (omx_encoder_compress_buffer(encoder->omx, dev, index) < 0) {
|
||||
if (omx_encoder_compress_buffer(encoder->omxs[worker_number], dev, buf_index) < 0) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
@@ -146,7 +172,6 @@ int encoder_compress_buffer(struct encoder_t *encoder, struct device_t *dev, int
|
||||
error:
|
||||
LOG_INFO("HW compressing error, falling back to CPU");
|
||||
encoder->type = ENCODER_TYPE_CPU;
|
||||
dev->run->n_workers = dev->n_workers;
|
||||
return -1;
|
||||
# pragma GCC diagnostic pop
|
||||
}
|
||||
|
||||
@@ -39,17 +39,17 @@
|
||||
enum encoder_type_t {
|
||||
ENCODER_TYPE_UNKNOWN, // Only for encoder_parse_type() and main()
|
||||
ENCODER_TYPE_CPU,
|
||||
|
||||
#ifdef OMX_ENCODER
|
||||
ENCODER_TYPE_OMX,
|
||||
#endif
|
||||
};
|
||||
|
||||
struct encoder_t {
|
||||
enum encoder_type_t type;
|
||||
|
||||
enum encoder_type_t type;
|
||||
unsigned quality;
|
||||
#ifdef OMX_ENCODER
|
||||
struct omx_encoder_t *omx;
|
||||
unsigned n_omxs;
|
||||
struct omx_encoder_t **omxs;
|
||||
#endif
|
||||
};
|
||||
|
||||
@@ -59,6 +59,8 @@ void encoder_destroy(struct encoder_t *encoder);
|
||||
|
||||
enum encoder_type_t encoder_parse_type(const char *const str);
|
||||
|
||||
void encoder_prepare(struct encoder_t *encoder);
|
||||
void encoder_prepare_for_device(struct encoder_t *encoder, struct device_t *dev);
|
||||
int encoder_compress_buffer(struct encoder_t *encoder, struct device_t *dev, int index);
|
||||
void encoder_prepare(struct encoder_t *encoder, struct device_t *dev);
|
||||
void encoder_prepare_live(struct encoder_t *encoder, struct device_t *dev);
|
||||
|
||||
int encoder_compress_buffer(struct encoder_t *encoder, struct device_t *dev,
|
||||
const unsigned worker_number, const unsigned buf_index);
|
||||
|
||||
501
src/http.c
501
src/http.c
@@ -22,7 +22,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <strings.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <event2/event.h>
|
||||
@@ -30,6 +30,9 @@
|
||||
#include <event2/http.h>
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/bufferevent.h>
|
||||
#include <event2/keyvalq_struct.h>
|
||||
|
||||
#include <uuid/uuid.h>
|
||||
|
||||
#ifndef EVTHREAD_USE_PTHREADS_IMPLEMENTED
|
||||
# error Required libevent-pthreads support
|
||||
@@ -40,11 +43,15 @@
|
||||
#include "stream.h"
|
||||
#include "http.h"
|
||||
|
||||
#include "data/blank.h"
|
||||
#include "data/index_html.h"
|
||||
#include "data/blank_jpeg.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 void _http_callback_root(struct evhttp_request *request, void *arg);
|
||||
static void _http_callback_ping(struct evhttp_request *request, void *v_server);
|
||||
static void _http_callback_state(struct evhttp_request *request, void *v_server);
|
||||
static void _http_callback_snapshot(struct evhttp_request *request, void *v_server);
|
||||
|
||||
static void _http_callback_stream(struct evhttp_request *request, void *v_server);
|
||||
@@ -52,10 +59,10 @@ static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_c
|
||||
static void _http_callback_stream_error(struct bufferevent *buf_event, short what, void *v_ctx);
|
||||
|
||||
static void _http_exposed_refresh(int fd, short event, void *v_server);
|
||||
static void _http_queue_send_stream(struct http_server_t *server);
|
||||
static void _http_queue_send_stream(struct http_server_t *server, const bool stream_updated, const bool picture_updated);
|
||||
|
||||
static void _expose_new_picture(struct http_server_t *server);
|
||||
static void _expose_blank_picture(struct http_server_t *server);
|
||||
static bool _expose_new_picture(struct http_server_t *server);
|
||||
static bool _expose_blank_picture(struct http_server_t *server);
|
||||
|
||||
|
||||
struct http_server_t *http_server_init(struct stream_t *stream) {
|
||||
@@ -68,11 +75,10 @@ struct http_server_t *http_server_init(struct stream_t *stream) {
|
||||
A_CALLOC(run, 1);
|
||||
run->stream = stream;
|
||||
run->exposed = exposed;
|
||||
run->refresh_interval.tv_sec = 0;
|
||||
run->refresh_interval.tv_usec = 30000; // ~30 refreshes per second
|
||||
run->drop_same_frames_blank = 10;
|
||||
|
||||
A_CALLOC(server, 1);
|
||||
server->host = "localhost";
|
||||
server->host = "127.0.0.1";
|
||||
server->port = 8080;
|
||||
server->timeout = 10;
|
||||
server->run = run;
|
||||
@@ -85,18 +91,19 @@ struct http_server_t *http_server_init(struct stream_t *stream) {
|
||||
evhttp_set_allowed_methods(run->http, EVHTTP_REQ_GET|EVHTTP_REQ_HEAD);
|
||||
|
||||
assert(!evhttp_set_cb(run->http, "/", _http_callback_root, NULL));
|
||||
assert(!evhttp_set_cb(run->http, "/ping", _http_callback_ping, (void *)server));
|
||||
assert(!evhttp_set_cb(run->http, "/snapshot", _http_callback_snapshot, (void *)exposed));
|
||||
assert(!evhttp_set_cb(run->http, "/state", _http_callback_state, (void *)server));
|
||||
assert(!evhttp_set_cb(run->http, "/snapshot", _http_callback_snapshot, (void *)server));
|
||||
assert(!evhttp_set_cb(run->http, "/stream", _http_callback_stream, (void *)server));
|
||||
|
||||
assert((run->refresh = event_new(run->base, -1, EV_PERSIST, _http_exposed_refresh, server)));
|
||||
assert(!event_add(run->refresh, &run->refresh_interval));
|
||||
return server;
|
||||
}
|
||||
|
||||
void http_server_destroy(struct http_server_t *server) {
|
||||
event_del(server->run->refresh);
|
||||
event_free(server->run->refresh);
|
||||
if (server->run->refresh) {
|
||||
event_del(server->run->refresh);
|
||||
event_free(server->run->refresh);
|
||||
}
|
||||
|
||||
evhttp_free(server->run->http);
|
||||
event_base_free(server->run->base);
|
||||
libevent_global_shutdown();
|
||||
@@ -108,12 +115,27 @@ void http_server_destroy(struct http_server_t *server) {
|
||||
}
|
||||
|
||||
int http_server_listen(struct http_server_t *server) {
|
||||
struct timeval refresh_interval;
|
||||
|
||||
refresh_interval.tv_sec = 0;
|
||||
if (server->run->stream->dev->desired_fps > 0) {
|
||||
refresh_interval.tv_usec = 1000000 / (server->run->stream->dev->desired_fps * 2);
|
||||
} else {
|
||||
refresh_interval.tv_usec = 16000; // ~60fps
|
||||
}
|
||||
assert((server->run->refresh = event_new(server->run->base, -1, EV_PERSIST, _http_exposed_refresh, server)));
|
||||
assert(!event_add(server->run->refresh, &refresh_interval));
|
||||
|
||||
server->run->drop_same_frames_blank = max_u(server->drop_same_frames, server->run->drop_same_frames_blank);
|
||||
|
||||
LOG_DEBUG("Binding HTTP to [%s]:%d ...", server->host, server->port);
|
||||
evhttp_set_timeout(server->run->http, server->timeout);
|
||||
if (evhttp_bind_socket(server->run->http, server->host, server->port) != 0) {
|
||||
|
||||
if (evhttp_bind_socket(server->run->http, server->host, server->port) < 0) {
|
||||
LOG_PERROR("Can't listen HTTP on [%s]:%d", server->host, server->port)
|
||||
return -1;
|
||||
}
|
||||
|
||||
LOG_INFO("Listening HTTP on [%s]:%d", server->host, server->port);
|
||||
return 0;
|
||||
}
|
||||
@@ -129,6 +151,26 @@ void http_server_loop_break(struct http_server_t *server) {
|
||||
}
|
||||
|
||||
|
||||
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 (!strcasecmp(value_str, "true") || !strcasecmp(value_str, "yes") || value_str[0] == '1') {
|
||||
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))
|
||||
|
||||
@@ -145,20 +187,13 @@ static void _http_callback_root(struct evhttp_request *request, UNUSED void *arg
|
||||
PROCESS_HEAD_REQUEST;
|
||||
|
||||
assert((buf = evbuffer_new()));
|
||||
assert(evbuffer_add_printf(buf,
|
||||
"<!DOCTYPE html><html><head><meta charset=\"utf-8\">"
|
||||
"<title>uStreamer</title></head><body><ul>"
|
||||
"<li><a href=\"/ping\">/ping</a></li>"
|
||||
"<li><a href=\"/snapshot\">/snapshot</a></li>"
|
||||
"<li><a href=\"/stream\">/stream</a></li>"
|
||||
"</body></html>"
|
||||
));
|
||||
assert(evbuffer_add_printf(buf, HTML_INDEX_PAGE));
|
||||
ADD_HEADER("Content-Type", "text/html");
|
||||
evhttp_send_reply(request, HTTP_OK, "OK", buf);
|
||||
evbuffer_free(buf);
|
||||
}
|
||||
|
||||
static void _http_callback_ping(struct evhttp_request *request, void *v_server) {
|
||||
static void _http_callback_state(struct evhttp_request *request, void *v_server) {
|
||||
struct http_server_t *server = (struct http_server_t *)v_server;
|
||||
struct evbuffer *buf;
|
||||
|
||||
@@ -166,45 +201,82 @@ static void _http_callback_ping(struct evhttp_request *request, void *v_server)
|
||||
|
||||
assert((buf = evbuffer_new()));
|
||||
assert(evbuffer_add_printf(buf,
|
||||
"{\"stream\": {\"resolution\":"
|
||||
" {\"width\": %u, \"height\": %u},"
|
||||
" \"online\": %s}}",
|
||||
"{\"ok\": true, \"result\":"
|
||||
" {\"source\": {\"resolution\": {\"width\": %u, \"height\": %u},"
|
||||
" \"online\": %s, \"quality\": %u, \"desired_fps\": %u, \"captured_fps\": %u},"
|
||||
" \"stream\": {\"queued_fps\": %u, \"clients\": %u, \"clients_stat\": {",
|
||||
(server->fake_width ? server->fake_width : server->run->exposed->width),
|
||||
(server->fake_height ? server->fake_height : server->run->exposed->height),
|
||||
(server->run->exposed->online ? "true" : "false")
|
||||
bool_to_string(server->run->exposed->online),
|
||||
server->run->stream->encoder->quality,
|
||||
server->run->stream->dev->desired_fps,
|
||||
server->run->exposed->captured_fps,
|
||||
server->run->exposed->queued_fps,
|
||||
server->run->stream_clients_count
|
||||
));
|
||||
for (struct stream_client_t * client = server->run->stream_clients; client != NULL; client = client->next) {
|
||||
assert(evbuffer_add_printf(buf,
|
||||
"\"%s\": {\"fps\": %u, \"extra_headers\": %s, \"advance_headers\": %s, \"dual_final_frames\": %s}%s",
|
||||
client->id,
|
||||
client->fps,
|
||||
bool_to_string(client->extra_headers),
|
||||
bool_to_string(client->advance_headers),
|
||||
bool_to_string(client->dual_final_frames),
|
||||
(client->next ? ", " : "")
|
||||
));
|
||||
}
|
||||
assert(evbuffer_add_printf(buf, "}}}}"));
|
||||
|
||||
ADD_HEADER("Content-Type", "application/json");
|
||||
evhttp_send_reply(request, HTTP_OK, "OK", buf);
|
||||
evbuffer_free(buf);
|
||||
}
|
||||
|
||||
static void _http_callback_snapshot(struct evhttp_request *request, void *v_exposed) {
|
||||
struct exposed_t *exposed = (struct exposed_t *)v_exposed;
|
||||
static void _http_callback_snapshot(struct evhttp_request *request, void *v_server) {
|
||||
struct http_server_t *server = (struct http_server_t *)v_server;
|
||||
struct evbuffer *buf;
|
||||
struct timespec x_timestamp_spec;
|
||||
char x_timestamp_buf[64];
|
||||
char header_buf[64];
|
||||
|
||||
PROCESS_HEAD_REQUEST;
|
||||
|
||||
assert((buf = evbuffer_new()));
|
||||
assert(!evbuffer_add(buf, (const void *)exposed->picture.data, exposed->picture.size));
|
||||
# define EXPOSED(_next) server->run->exposed->_next
|
||||
|
||||
assert(!clock_gettime(CLOCK_REALTIME, &x_timestamp_spec));
|
||||
sprintf(
|
||||
x_timestamp_buf, "%u.%06u",
|
||||
(unsigned)x_timestamp_spec.tv_sec,
|
||||
(unsigned)(x_timestamp_spec.tv_nsec / 1000) // TODO: round?
|
||||
);
|
||||
assert((buf = evbuffer_new()));
|
||||
assert(!evbuffer_add(buf, (const void *)EXPOSED(picture.data), EXPOSED(picture.size)));
|
||||
|
||||
ADD_HEADER("Access-Control-Allow-Origin:", "*");
|
||||
ADD_HEADER("Cache-Control", "no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0");
|
||||
ADD_HEADER("Pragma", "no-cache");
|
||||
ADD_HEADER("Expires", "Mon, 3 Jan 2000 12:34:56 GMT");
|
||||
ADD_HEADER("X-Timestamp", x_timestamp_buf);
|
||||
|
||||
# define ADD_TIME_HEADER(_key, _value) \
|
||||
{ sprintf(header_buf, "%.06Lf", _value); ADD_HEADER(_key, header_buf); }
|
||||
|
||||
# define ADD_UNSIGNED_HEADER(_key, _value) \
|
||||
{ sprintf(header_buf, "%u", _value); ADD_HEADER(_key, header_buf); }
|
||||
|
||||
ADD_TIME_HEADER("X-Timestamp", get_now_real());
|
||||
|
||||
ADD_HEADER("X-UStreamer-Online", bool_to_string(EXPOSED(online)));
|
||||
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());
|
||||
|
||||
# undef ADD_UNSUGNED_HEADER
|
||||
# undef ADD_TIME_HEADER
|
||||
|
||||
ADD_HEADER("Content-Type", "image/jpeg");
|
||||
|
||||
evhttp_send_reply(request, HTTP_OK, "OK", buf);
|
||||
evbuffer_free(buf);
|
||||
|
||||
# undef EXPOSED
|
||||
}
|
||||
|
||||
#undef ADD_HEADER
|
||||
@@ -218,8 +290,12 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server
|
||||
|
||||
struct http_server_t *server = (struct http_server_t *)v_server;
|
||||
struct evhttp_connection *conn;
|
||||
struct evkeyvalq params;
|
||||
struct bufferevent *buf_event;
|
||||
struct stream_client_t *client;
|
||||
char *client_addr;
|
||||
unsigned short client_port;
|
||||
uuid_t uuid;
|
||||
|
||||
PROCESS_HEAD_REQUEST;
|
||||
|
||||
@@ -229,6 +305,17 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server
|
||||
client->server = server;
|
||||
client->request = request;
|
||||
client->need_initial = true;
|
||||
client->need_first_frame = true;
|
||||
|
||||
evhttp_parse_query(evhttp_request_get_uri(request), ¶ms);
|
||||
client->key = _http_get_param_uri(¶ms, "key");
|
||||
client->extra_headers = _http_get_param_true(¶ms, "extra_headers");
|
||||
client->advance_headers = _http_get_param_true(¶ms, "advance_headers");
|
||||
client->dual_final_frames = _http_get_param_true(¶ms, "dual_final_frames");
|
||||
evhttp_clear_headers(¶ms);
|
||||
|
||||
uuid_generate(uuid);
|
||||
uuid_unparse_lower(uuid, client->id);
|
||||
|
||||
if (server->run->stream_clients == NULL) {
|
||||
server->run->stream_clients = client;
|
||||
@@ -239,6 +326,19 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server
|
||||
client->prev = last;
|
||||
last->next = client;
|
||||
}
|
||||
server->run->stream_clients_count += 1;
|
||||
|
||||
evhttp_connection_get_peer(conn, &client_addr, &client_port);
|
||||
LOG_INFO(
|
||||
"HTTP: Registered the new stream client: [%s]:%u; id=%s;"
|
||||
" advance_headers=%s; dual_final_frames=%s; clients now: %u",
|
||||
client_addr,
|
||||
client_port,
|
||||
client->id,
|
||||
bool_to_string(client->advance_headers),
|
||||
bool_to_string(client->dual_final_frames),
|
||||
server->run->stream_clients_count
|
||||
);
|
||||
|
||||
buf_event = evhttp_connection_get_bufferevent(conn);
|
||||
bufferevent_setcb(buf_event, NULL, NULL, _http_callback_stream_error, (void *)client);
|
||||
@@ -250,16 +350,46 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server
|
||||
|
||||
#undef PROCESS_HEAD_REQUEST
|
||||
|
||||
#define BOUNDARY "boundarydonotcross"
|
||||
#define RN "\r\n"
|
||||
|
||||
static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_client) {
|
||||
# define BOUNDARY "boundarydonotcross"
|
||||
# define RN "\r\n"
|
||||
|
||||
struct stream_client_t *client = (struct stream_client_t *)v_client;
|
||||
struct evbuffer *buf;
|
||||
struct timespec x_timestamp_spec;
|
||||
long double now = get_now_monotonic();
|
||||
long long now_second = floor_ms(now);
|
||||
|
||||
if (now_second != client->fps_accum_second) {
|
||||
client->fps = client->fps_accum;
|
||||
client->fps_accum = 0;
|
||||
client->fps_accum_second = now_second;
|
||||
}
|
||||
client->fps_accum += 1;
|
||||
|
||||
assert((buf = evbuffer_new()));
|
||||
assert(!clock_gettime(CLOCK_REALTIME, &x_timestamp_spec));
|
||||
|
||||
// В хроме и его производных есть фундаментальный баг: он отрисовывает
|
||||
// фрейм с задержкой на один, как только ему придут заголовки следующего.
|
||||
// В сочетании с drop_same_frames это дает значительный лаг стрима
|
||||
// при большом количестве дропов (на статичном изображении, где внезапно
|
||||
// что-то изменилось.
|
||||
//
|
||||
// https://bugs.chromium.org/p/chromium/issues/detail?id=527446
|
||||
//
|
||||
// Включение advance_headers заставляет стример отсылать заголовки
|
||||
// будущего фрейма сразу после данных текущего, чтобы триггернуть отрисовку.
|
||||
// Естественным следствием этого является невозможность установки заголовка
|
||||
// Content-Length, так как предсказывать будущее мы еще не научились.
|
||||
// Его наличие не требуется RFC, однако никаких стандартов на MJPG over HTTP
|
||||
// в природе не существует, и никто не может гарантировать, что отсутствие
|
||||
// Content-Length не сломает вещание для каких-нибудь маргинальных браузеров.
|
||||
//
|
||||
// Кроме того, advance_headers форсит отключение заголовков X-UStreamer-*
|
||||
// по тем же причинам, по которым у нас нет Content-Length.
|
||||
|
||||
# define ADD_ADVANCE_HEADERS \
|
||||
{ assert(evbuffer_add_printf(buf, \
|
||||
"Content-Type: image/jpeg" RN "X-Timestamp: %.06Lf" RN RN, get_now_real())); }
|
||||
|
||||
if (client->need_initial) {
|
||||
assert(evbuffer_add_printf(buf,
|
||||
@@ -268,44 +398,100 @@ static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_c
|
||||
"Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0" RN
|
||||
"Pragma: no-cache" RN
|
||||
"Expires: Mon, 3 Jan 2000 12:34:56 GMT" RN
|
||||
"Set-Cookie: stream_client=%s/%s; path=/; max-age=30" RN
|
||||
"Content-Type: multipart/x-mixed-replace;boundary=" BOUNDARY RN
|
||||
RN
|
||||
"--" BOUNDARY RN
|
||||
"--" BOUNDARY RN,
|
||||
(client->key != NULL ? client->key : "0"),
|
||||
client->id
|
||||
));
|
||||
|
||||
if (client->advance_headers) {
|
||||
ADD_ADVANCE_HEADERS;
|
||||
}
|
||||
|
||||
assert(!bufferevent_write_buffer(buf_event, buf));
|
||||
client->need_initial = false;
|
||||
}
|
||||
|
||||
assert(evbuffer_add_printf(buf,
|
||||
"Content-Type: image/jpeg" RN
|
||||
"Content-Length: %lu" RN
|
||||
"X-Timestamp: %u.%06u" RN
|
||||
RN,
|
||||
client->server->run->exposed->picture.size * sizeof(*client->server->run->exposed->picture.data),
|
||||
(unsigned)x_timestamp_spec.tv_sec,
|
||||
(unsigned)(x_timestamp_spec.tv_nsec / 1000) // TODO: round?
|
||||
));
|
||||
# define EXPOSED(_next) client->server->run->exposed->_next
|
||||
|
||||
if (!client->advance_headers) {
|
||||
assert(evbuffer_add_printf(buf,
|
||||
"Content-Type: image/jpeg" RN
|
||||
"Content-Length: %lu" RN
|
||||
"X-Timestamp: %.06Lf" RN
|
||||
"%s",
|
||||
EXPOSED(picture.size) * sizeof(*EXPOSED(picture.data)),
|
||||
get_now_real(), (client->extra_headers ? "" : RN)
|
||||
));
|
||||
if (client->extra_headers) {
|
||||
assert(evbuffer_add_printf(buf,
|
||||
"X-UStreamer-Online: %s" RN
|
||||
"X-UStreamer-Width: %u" RN
|
||||
"X-UStreamer-Height: %u" RN
|
||||
"X-UStreamer-Client-FPS: %u" RN
|
||||
"X-UStreamer-Grab-Time: %.06Lf" RN
|
||||
"X-UStreamer-Encode-Begin-Time: %.06Lf" RN
|
||||
"X-UStreamer-Encode-End-Time: %.06Lf" RN
|
||||
"X-UStreamer-Expose-Begin-Time: %.06Lf" RN
|
||||
"X-UStreamer-Expose-Cmp-Time: %.06Lf" RN
|
||||
"X-UStreamer-Expose-End-Time: %.06Lf" RN
|
||||
"X-UStreamer-Send-Time: %.06Lf" RN
|
||||
RN,
|
||||
bool_to_string(EXPOSED(online)),
|
||||
EXPOSED(width),
|
||||
EXPOSED(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),
|
||||
now
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
assert(!evbuffer_add(buf,
|
||||
(void *)client->server->run->exposed->picture.data,
|
||||
client->server->run->exposed->picture.size * sizeof(*client->server->run->exposed->picture.data)
|
||||
(void *)EXPOSED(picture.data),
|
||||
EXPOSED(picture.size) * sizeof(*EXPOSED(picture.data))
|
||||
));
|
||||
assert(evbuffer_add_printf(buf, RN "--" BOUNDARY RN));
|
||||
|
||||
if (client->advance_headers) {
|
||||
ADD_ADVANCE_HEADERS;
|
||||
}
|
||||
|
||||
assert(!bufferevent_write_buffer(buf_event, buf));
|
||||
evbuffer_free(buf);
|
||||
|
||||
bufferevent_setcb(buf_event, NULL, NULL, _http_callback_stream_error, (void *)client);
|
||||
bufferevent_enable(buf_event, EV_READ);
|
||||
}
|
||||
|
||||
#undef BOUNDARY
|
||||
#undef RN
|
||||
# undef EXPOSED
|
||||
# undef ADD_ADVANCE_HEADERS
|
||||
# undef RN
|
||||
# undef BOUNDARY
|
||||
}
|
||||
|
||||
static void _http_callback_stream_error(UNUSED struct bufferevent *buf_event, UNUSED short what, void *v_client) {
|
||||
struct stream_client_t *client = (struct stream_client_t *)v_client;
|
||||
struct evhttp_connection *conn;
|
||||
char *client_addr = "???";
|
||||
unsigned short client_port = 0;
|
||||
|
||||
client->server->run->stream_clients_count -= 1;
|
||||
|
||||
conn = evhttp_request_get_connection(client->request);
|
||||
if (conn != NULL) {
|
||||
evhttp_connection_get_peer(conn, &client_addr, &client_port);
|
||||
}
|
||||
LOG_INFO(
|
||||
"HTTP: Disconnected the stream client: [%s]:%u; clients now: %u",
|
||||
client_addr, client_port, client->server->run->stream_clients_count
|
||||
);
|
||||
if (conn != NULL) {
|
||||
evhttp_connection_free(conn);
|
||||
}
|
||||
@@ -318,85 +504,198 @@ static void _http_callback_stream_error(UNUSED struct bufferevent *buf_event, UN
|
||||
if (client->next != NULL) {
|
||||
client->next->prev = client->prev;
|
||||
}
|
||||
free(client->key);
|
||||
free(client);
|
||||
}
|
||||
|
||||
static void _http_queue_send_stream(struct http_server_t *server) {
|
||||
struct stream_client_t *client;
|
||||
static void _http_queue_send_stream(struct http_server_t *server, const bool stream_updated, const bool picture_updated) {
|
||||
struct evhttp_connection *conn;
|
||||
struct bufferevent *buf_event;
|
||||
long long now;
|
||||
bool queued = false;
|
||||
static unsigned queued_fps_accum = 0;
|
||||
static long long queued_fps_second = 0;
|
||||
|
||||
for (client = server->run->stream_clients; client != NULL; client = client->next) {
|
||||
for (struct stream_client_t *client = server->run->stream_clients; client != NULL; client = client->next) {
|
||||
conn = evhttp_request_get_connection(client->request);
|
||||
if (conn != NULL) {
|
||||
buf_event = evhttp_connection_get_bufferevent(conn);
|
||||
bufferevent_setcb(buf_event, NULL, _http_callback_stream_write, _http_callback_stream_error, (void *)client);
|
||||
bufferevent_enable(buf_event, EV_READ|EV_WRITE);
|
||||
// Фикс для бага WebKit. При включенной опции дропа одинаковых фреймов,
|
||||
// WebKit отрисовывает последний фрейм в серии с некоторой задержкой,
|
||||
// и нужно послать два фрейма, чтобы серия была вовремя завершена.
|
||||
// Это похоже на баг Blink (см. _http_callback_stream_write() и advance_headers),
|
||||
// но фикс для него не лечит проблему вебкита. Такие дела.
|
||||
|
||||
bool dual_update = (
|
||||
server->drop_same_frames
|
||||
&& client->dual_final_frames
|
||||
&& stream_updated
|
||||
&& client->updated_prev
|
||||
&& !picture_updated
|
||||
);
|
||||
|
||||
if (dual_update || picture_updated || client->need_first_frame) {
|
||||
buf_event = evhttp_connection_get_bufferevent(conn);
|
||||
bufferevent_setcb(buf_event, NULL, _http_callback_stream_write, _http_callback_stream_error, (void *)client);
|
||||
bufferevent_enable(buf_event, EV_READ|EV_WRITE);
|
||||
|
||||
client->need_first_frame = false;
|
||||
client->updated_prev = (picture_updated || client->need_first_frame); // Игнорировать dual
|
||||
queued = true;
|
||||
} else if (stream_updated) { // Для dual
|
||||
client->updated_prev = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (queued) {
|
||||
if ((now = floor_ms(get_now_monotonic())) != queued_fps_second) {
|
||||
server->run->exposed->queued_fps = queued_fps_accum;
|
||||
queued_fps_accum = 0;
|
||||
queued_fps_second = now;
|
||||
}
|
||||
queued_fps_accum += 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void _http_exposed_refresh(UNUSED int fd, UNUSED short what, void *v_server) {
|
||||
struct http_server_t *server = (struct http_server_t *)v_server;
|
||||
bool stream_updated = false;
|
||||
bool picture_updated = false;
|
||||
|
||||
#define LOCK_STREAM \
|
||||
A_PTHREAD_M_LOCK(&server->run->stream->mutex);
|
||||
|
||||
#define UNLOCK_STREAM \
|
||||
{ server->run->stream->updated = false; A_PTHREAD_M_UNLOCK(&server->run->stream->mutex); }
|
||||
# define UNLOCK_STREAM \
|
||||
{ server->run->stream->updated = false; A_PTHREAD_M_UNLOCK(&server->run->stream->mutex); }
|
||||
|
||||
if (server->run->stream->updated) {
|
||||
LOG_DEBUG("Refreshing HTTP exposed ...");
|
||||
LOCK_STREAM;
|
||||
A_PTHREAD_M_LOCK(&server->run->stream->mutex);
|
||||
if (server->run->stream->picture.size > 0) { // If online
|
||||
_expose_new_picture(server);
|
||||
picture_updated = _expose_new_picture(server);
|
||||
UNLOCK_STREAM;
|
||||
} else {
|
||||
UNLOCK_STREAM;
|
||||
_expose_blank_picture(server);
|
||||
picture_updated = _expose_blank_picture(server);
|
||||
}
|
||||
_http_queue_send_stream(server);
|
||||
stream_updated = true;
|
||||
} else if (!server->run->exposed->online) {
|
||||
LOG_DEBUG("Refreshing HTTP exposed (BLANK) ...");
|
||||
_http_queue_send_stream(server);
|
||||
picture_updated = _expose_blank_picture(server);
|
||||
stream_updated = true;
|
||||
}
|
||||
|
||||
# undef LOCK_STREAM
|
||||
# undef UNLOCK_STREAM
|
||||
|
||||
_http_queue_send_stream(server, stream_updated, picture_updated);
|
||||
}
|
||||
|
||||
void _expose_new_picture(struct http_server_t *server) {
|
||||
if (server->run->exposed->picture.allocated < server->run->stream->picture.allocated) {
|
||||
A_REALLOC(server->run->exposed->picture.data, server->run->stream->picture.allocated);
|
||||
server->run->exposed->picture.allocated = server->run->stream->picture.allocated;
|
||||
static bool _expose_new_picture(struct http_server_t *server) {
|
||||
# define STREAM(_next) server->run->stream->_next
|
||||
# define EXPOSED(_next) server->run->exposed->_next
|
||||
|
||||
assert(STREAM(picture.size) > 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.size) * sizeof(*STREAM(picture.data))
|
||||
|
||||
if (server->drop_same_frames) {
|
||||
if (
|
||||
EXPOSED(online)
|
||||
&& EXPOSED(dropped) < server->drop_same_frames
|
||||
&& EXPOSED(picture.size) == STREAM(picture.size)
|
||||
&& !memcmp(MEM_STREAM_TO_EXPOSED)
|
||||
) {
|
||||
EXPOSED(expose_cmp_time) = get_now_monotonic();
|
||||
EXPOSED(expose_end_time) = EXPOSED(expose_cmp_time);
|
||||
LOG_VERBOSE(
|
||||
"HTTP: dropped same frame number %u; comparsion time = %.06Lf",
|
||||
EXPOSED(dropped), EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time)
|
||||
);
|
||||
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); comparsion time = %.06Lf",
|
||||
EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(
|
||||
server->run->exposed->picture.data, server->run->stream->picture.data,
|
||||
server->run->stream->picture.size * sizeof(*server->run->exposed->picture.data)
|
||||
if (EXPOSED(picture.allocated) < STREAM(picture.allocated)) {
|
||||
A_REALLOC(EXPOSED(picture.data), STREAM(picture.allocated));
|
||||
EXPOSED(picture.allocated) = STREAM(picture.allocated);
|
||||
}
|
||||
|
||||
memcpy(MEM_STREAM_TO_EXPOSED);
|
||||
|
||||
# undef MEM_STREAM_TO_EXPOSED
|
||||
|
||||
EXPOSED(picture.size) = STREAM(picture.size);
|
||||
|
||||
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();
|
||||
|
||||
LOG_VERBOSE(
|
||||
"HTTP: exposed new frame; full exposition time = %.06Lf",
|
||||
EXPOSED(expose_end_time) - EXPOSED(expose_begin_time)
|
||||
);
|
||||
|
||||
server->run->exposed->picture.size = server->run->stream->picture.size;
|
||||
server->run->exposed->width = server->run->stream->width;
|
||||
server->run->exposed->height = server->run->stream->height;
|
||||
server->run->exposed->online = true;
|
||||
# undef EXPOSED
|
||||
# undef STREAM
|
||||
return true; // Updated
|
||||
}
|
||||
|
||||
void _expose_blank_picture(struct http_server_t *server) {
|
||||
if (server->run->exposed->online || server->run->exposed->picture.size == 0) {
|
||||
if (server->run->exposed->picture.allocated < BLANK_JPG_SIZE) {
|
||||
A_REALLOC(server->run->exposed->picture.data, BLANK_JPG_SIZE);
|
||||
server->run->exposed->picture.allocated = BLANK_JPG_SIZE;
|
||||
static bool _expose_blank_picture(struct http_server_t *server) {
|
||||
# define EXPOSED(_next) server->run->exposed->_next
|
||||
|
||||
EXPOSED(expose_begin_time) = get_now_monotonic();
|
||||
EXPOSED(expose_cmp_time) = EXPOSED(expose_begin_time);
|
||||
|
||||
if (EXPOSED(online) || EXPOSED(picture.size) == 0) {
|
||||
if (EXPOSED(picture.allocated) < BLANK_JPG_SIZE) {
|
||||
A_REALLOC(EXPOSED(picture.data), BLANK_JPG_SIZE);
|
||||
EXPOSED(picture.allocated) = BLANK_JPG_SIZE;
|
||||
}
|
||||
|
||||
memcpy(
|
||||
server->run->exposed->picture.data, BLANK_JPG_DATA,
|
||||
BLANK_JPG_SIZE * sizeof(*server->run->exposed->picture.data)
|
||||
EXPOSED(picture.data), BLANK_JPG_DATA,
|
||||
BLANK_JPG_SIZE * sizeof(*EXPOSED(picture.data))
|
||||
);
|
||||
|
||||
server->run->exposed->picture.size = BLANK_JPG_SIZE;
|
||||
server->run->exposed->width = BLANK_JPG_WIDTH;
|
||||
server->run->exposed->height = BLANK_JPG_HEIGHT;
|
||||
server->run->exposed->online = false;
|
||||
EXPOSED(picture.size) = BLANK_JPG_SIZE;
|
||||
|
||||
EXPOSED(picture.grab_time) = 0;
|
||||
EXPOSED(picture.encode_begin_time) = 0;
|
||||
EXPOSED(picture.encode_end_time) = 0;
|
||||
|
||||
EXPOSED(width) = BLANK_JPG_WIDTH;
|
||||
EXPOSED(height) = BLANK_JPG_HEIGHT;
|
||||
EXPOSED(captured_fps) = 0;
|
||||
EXPOSED(online) = false;
|
||||
goto updated;
|
||||
}
|
||||
|
||||
if (EXPOSED(dropped) < server->run->drop_same_frames_blank) {
|
||||
LOG_PERF("HTTP: dropped same frame (BLANK) number %u", EXPOSED(dropped));
|
||||
EXPOSED(dropped) += 1;
|
||||
EXPOSED(expose_end_time) = get_now_monotonic();
|
||||
return false; // Not updated
|
||||
}
|
||||
|
||||
updated:
|
||||
EXPOSED(dropped) = 0;
|
||||
EXPOSED(expose_end_time) = get_now_monotonic();
|
||||
return true; // Updated
|
||||
|
||||
# undef EXPOSED
|
||||
}
|
||||
|
||||
24
src/http.h
24
src/http.h
@@ -20,7 +20,6 @@
|
||||
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <event2/event.h>
|
||||
#include <event2/http.h>
|
||||
@@ -32,7 +31,19 @@
|
||||
struct stream_client_t {
|
||||
struct http_server_t *server;
|
||||
struct evhttp_request *request;
|
||||
|
||||
char *key;
|
||||
bool extra_headers;
|
||||
bool advance_headers;
|
||||
bool dual_final_frames;
|
||||
|
||||
char id[37]; // ex. "1b4e28ba-2fa1-11d2-883f-0016d3cca427" + "\0"
|
||||
bool need_initial;
|
||||
bool need_first_frame;
|
||||
bool updated_prev;
|
||||
unsigned fps;
|
||||
unsigned fps_accum;
|
||||
long long fps_accum_second;
|
||||
|
||||
struct stream_client_t *prev;
|
||||
struct stream_client_t *next;
|
||||
@@ -42,7 +53,13 @@ struct exposed_t {
|
||||
struct picture_t picture;
|
||||
unsigned width;
|
||||
unsigned height;
|
||||
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;
|
||||
};
|
||||
|
||||
struct http_server_runtime_t {
|
||||
@@ -51,14 +68,15 @@ struct http_server_runtime_t {
|
||||
struct event *refresh;
|
||||
struct stream_t *stream;
|
||||
struct exposed_t *exposed;
|
||||
struct timeval refresh_interval;
|
||||
|
||||
struct stream_client_t *stream_clients;
|
||||
unsigned stream_clients_count;
|
||||
unsigned drop_same_frames_blank;
|
||||
};
|
||||
|
||||
struct http_server_t {
|
||||
char *host;
|
||||
unsigned port;
|
||||
unsigned drop_same_frames;
|
||||
unsigned fake_width;
|
||||
unsigned fake_height;
|
||||
unsigned timeout;
|
||||
|
||||
@@ -38,10 +38,7 @@
|
||||
#include "encoder.h"
|
||||
|
||||
|
||||
#define JPEG_OUTPUT_BUFFER_SIZE 4096
|
||||
|
||||
|
||||
struct mjpg_destination_mgr {
|
||||
struct _mjpg_destination_mgr {
|
||||
struct jpeg_destination_mgr mgr; // Default manager
|
||||
JOCTET *buffer; // Start of buffer
|
||||
unsigned char *outbuffer_cursor;
|
||||
@@ -68,7 +65,7 @@ static boolean _jpeg_empty_output_buffer(j_compress_ptr jpeg);
|
||||
static void _jpeg_term_destination(j_compress_ptr jpeg);
|
||||
|
||||
|
||||
void jpeg_encoder_compress_buffer(struct device_t *dev, int index) {
|
||||
void jpeg_encoder_compress_buffer(struct device_t *dev, const unsigned index, const unsigned quality) {
|
||||
// This function based on compress_image_to_jpeg() from mjpg-streamer
|
||||
|
||||
struct jpeg_compress_struct jpeg;
|
||||
@@ -89,7 +86,7 @@ void jpeg_encoder_compress_buffer(struct device_t *dev, int index) {
|
||||
jpeg.in_color_space = JCS_RGB;
|
||||
|
||||
jpeg_set_defaults(&jpeg);
|
||||
jpeg_set_quality(&jpeg, dev->jpeg_quality, TRUE);
|
||||
jpeg_set_quality(&jpeg, quality, TRUE);
|
||||
|
||||
jpeg_start_compress(&jpeg, TRUE);
|
||||
|
||||
@@ -110,18 +107,19 @@ void jpeg_encoder_compress_buffer(struct device_t *dev, int index) {
|
||||
jpeg_destroy_compress(&jpeg);
|
||||
free(line_buffer);
|
||||
assert(dev->run->pictures[index].size > 0);
|
||||
assert(dev->run->pictures[index].size <= dev->run->max_picture_size);
|
||||
}
|
||||
|
||||
static void _jpeg_set_dest_picture(j_compress_ptr jpeg, unsigned char *picture, unsigned long *written) {
|
||||
struct mjpg_destination_mgr *dest;
|
||||
struct _mjpg_destination_mgr *dest;
|
||||
|
||||
if (jpeg->dest == NULL) {
|
||||
assert((jpeg->dest = (struct jpeg_destination_mgr *)(*jpeg->mem->alloc_small)(
|
||||
(j_common_ptr) jpeg, JPOOL_PERMANENT, sizeof(struct mjpg_destination_mgr)
|
||||
(j_common_ptr) jpeg, JPOOL_PERMANENT, sizeof(struct _mjpg_destination_mgr)
|
||||
)));
|
||||
}
|
||||
|
||||
dest = (struct mjpg_destination_mgr *) jpeg->dest;
|
||||
dest = (struct _mjpg_destination_mgr *) jpeg->dest;
|
||||
dest->mgr.init_destination = _jpeg_init_destination;
|
||||
dest->mgr.empty_output_buffer = _jpeg_empty_output_buffer;
|
||||
dest->mgr.term_destination = _jpeg_term_destination;
|
||||
@@ -139,7 +137,7 @@ static void _jpeg_write_scanlines_yuyv(struct jpeg_compress_struct *jpeg,
|
||||
while (jpeg->next_scanline < height) {
|
||||
unsigned char *ptr = line_buffer;
|
||||
|
||||
for (unsigned x = 0; x < width; x++) {
|
||||
for (unsigned x = 0; x < width; ++x) {
|
||||
int y = (!z ? data[0] << 8 : data[2] << 8);
|
||||
int u = data[1] - 128;
|
||||
int v = data[3] - 128;
|
||||
@@ -173,7 +171,7 @@ static void _jpeg_write_scanlines_uyvy(struct jpeg_compress_struct *jpeg,
|
||||
while(jpeg->next_scanline < height) {
|
||||
unsigned char *ptr = line_buffer;
|
||||
|
||||
for(unsigned x = 0; x < width; x++) {
|
||||
for(unsigned x = 0; x < width; ++x) {
|
||||
int y = (!z ? data[1] << 8 : data[3] << 8);
|
||||
int u = data[0] - 128;
|
||||
int v = data[2] - 128;
|
||||
@@ -206,7 +204,7 @@ static void _jpeg_write_scanlines_rgb565(struct jpeg_compress_struct *jpeg,
|
||||
while(jpeg->next_scanline < height) {
|
||||
unsigned char *ptr = line_buffer;
|
||||
|
||||
for(unsigned x = 0; x < width; x++) {
|
||||
for(unsigned x = 0; x < width; ++x) {
|
||||
unsigned int two_byte = (data[1] << 8) + data[0];
|
||||
|
||||
*(ptr++) = data[1] & 248;
|
||||
@@ -221,8 +219,10 @@ static void _jpeg_write_scanlines_rgb565(struct jpeg_compress_struct *jpeg,
|
||||
}
|
||||
}
|
||||
|
||||
#define JPEG_OUTPUT_BUFFER_SIZE 4096
|
||||
|
||||
static void _jpeg_init_destination(j_compress_ptr jpeg) {
|
||||
struct mjpg_destination_mgr *dest = (struct mjpg_destination_mgr *) jpeg->dest;
|
||||
struct _mjpg_destination_mgr *dest = (struct _mjpg_destination_mgr *) jpeg->dest;
|
||||
|
||||
// Allocate the output buffer - it will be released when done with image
|
||||
assert((dest->buffer = (JOCTET *)(*jpeg->mem->alloc_small)(
|
||||
@@ -236,7 +236,7 @@ static void _jpeg_init_destination(j_compress_ptr jpeg) {
|
||||
static boolean _jpeg_empty_output_buffer(j_compress_ptr jpeg) {
|
||||
// Called whenever local jpeg buffer fills up
|
||||
|
||||
struct mjpg_destination_mgr *dest = (struct mjpg_destination_mgr *) jpeg->dest;
|
||||
struct _mjpg_destination_mgr *dest = (struct _mjpg_destination_mgr *) jpeg->dest;
|
||||
|
||||
memcpy(dest->outbuffer_cursor, dest->buffer, JPEG_OUTPUT_BUFFER_SIZE);
|
||||
dest->outbuffer_cursor += JPEG_OUTPUT_BUFFER_SIZE;
|
||||
@@ -252,7 +252,7 @@ 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
|
||||
|
||||
struct mjpg_destination_mgr *dest = (struct mjpg_destination_mgr *) jpeg->dest;
|
||||
struct _mjpg_destination_mgr *dest = (struct _mjpg_destination_mgr *) jpeg->dest;
|
||||
size_t data_count = JPEG_OUTPUT_BUFFER_SIZE - dest->mgr.free_in_buffer;
|
||||
|
||||
// Write any data remaining in the buffer
|
||||
@@ -260,3 +260,5 @@ static void _jpeg_term_destination(j_compress_ptr jpeg) {
|
||||
dest->outbuffer_cursor += data_count;
|
||||
*dest->written += data_count;
|
||||
}
|
||||
|
||||
#undef JPEG_OUTPUT_BUFFER_SIZE
|
||||
|
||||
@@ -24,4 +24,4 @@
|
||||
#include "../device.h"
|
||||
|
||||
|
||||
void jpeg_encoder_compress_buffer(struct device_t *dev, int index);
|
||||
void jpeg_encoder_compress_buffer(struct device_t *dev, const unsigned index, const unsigned quality);
|
||||
|
||||
@@ -54,65 +54,72 @@ pthread_mutex_t log_mutex;
|
||||
#define LOGGING_UNLOCK assert(!pthread_mutex_unlock(&log_mutex))
|
||||
|
||||
|
||||
#define SEP_INFO(_x_ch) { \
|
||||
#define SEP_INFO(_ch) { \
|
||||
LOGGING_LOCK; \
|
||||
for (int _i = 0; _i < 80; ++_i) { \
|
||||
putchar(_x_ch); \
|
||||
putchar(_ch); \
|
||||
} \
|
||||
putchar('\n'); \
|
||||
fflush(stdout); \
|
||||
LOGGING_UNLOCK; \
|
||||
}
|
||||
|
||||
#define SEP_DEBUG(_x_ch) { \
|
||||
#define SEP_DEBUG(_ch) { \
|
||||
if (log_level >= LOG_LEVEL_DEBUG) { \
|
||||
SEP_INFO(_x_ch); \
|
||||
SEP_INFO(_ch); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define LOG_ERROR(_x_msg, ...) { \
|
||||
#define LOG_PRINTF_NOLOCK(_label, _msg, ...) { \
|
||||
printf("-- " _label " [%.03Lf tid=%ld] -- " _msg "\n", get_now_monotonic(), syscall(SYS_gettid), ##__VA_ARGS__); \
|
||||
fflush(stdout); \
|
||||
}
|
||||
|
||||
#define LOG_ERROR(_msg, ...) { \
|
||||
LOGGING_LOCK; \
|
||||
printf("-- ERROR [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \
|
||||
LOG_PRINTF_NOLOCK("ERROR", _msg, ##__VA_ARGS__); \
|
||||
LOGGING_UNLOCK; \
|
||||
}
|
||||
|
||||
#define LOG_PERROR(_x_msg, ...) { \
|
||||
#define LOG_PERROR(_msg, ...) { \
|
||||
char _buf[1024] = ""; \
|
||||
strerror_r(errno, _buf, 1024); \
|
||||
char *_ptr = strerror_r(errno, _buf, 1024); \
|
||||
LOGGING_LOCK; \
|
||||
printf("-- ERROR [%.03Lf tid=%ld] -- " _x_msg ": %s\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__, _buf); \
|
||||
printf("-- ERROR [%.03Lf tid=%ld] -- " _msg ": %s\n", get_now_monotonic(), syscall(SYS_gettid), ##__VA_ARGS__, _ptr); \
|
||||
fflush(stdout); \
|
||||
LOGGING_UNLOCK; \
|
||||
}
|
||||
|
||||
#define LOG_INFO(_x_msg, ...) { \
|
||||
#define LOG_INFO(_msg, ...) { \
|
||||
LOGGING_LOCK; \
|
||||
printf("-- INFO [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \
|
||||
LOG_PRINTF_NOLOCK("INFO ", _msg, ##__VA_ARGS__); \
|
||||
LOGGING_UNLOCK; \
|
||||
}
|
||||
|
||||
#define LOG_INFO_NOLOCK(_x_msg, ...) { \
|
||||
printf("-- INFO [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \
|
||||
#define LOG_INFO_NOLOCK(_msg, ...) { \
|
||||
LOG_PRINTF_NOLOCK("INFO ", _msg, ##__VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define LOG_PERF(_x_msg, ...) { \
|
||||
#define LOG_PERF(_msg, ...) { \
|
||||
if (log_level >= LOG_LEVEL_PERF) { \
|
||||
LOGGING_LOCK; \
|
||||
printf("-- PERF [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \
|
||||
LOG_PRINTF_NOLOCK("PERF ", _msg, ##__VA_ARGS__); \
|
||||
LOGGING_UNLOCK; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define LOG_VERBOSE(_x_msg, ...) { \
|
||||
#define LOG_VERBOSE(_msg, ...) { \
|
||||
if (log_level >= LOG_LEVEL_VERBOSE) { \
|
||||
LOGGING_LOCK; \
|
||||
printf("-- VERB [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \
|
||||
LOG_PRINTF_NOLOCK("VERB ", _msg, ##__VA_ARGS__); \
|
||||
LOGGING_UNLOCK; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define LOG_DEBUG(_x_msg, ...) { \
|
||||
#define LOG_DEBUG(_msg, ...) { \
|
||||
if (log_level >= LOG_LEVEL_DEBUG) { \
|
||||
LOGGING_LOCK; \
|
||||
printf("-- DEBUG [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \
|
||||
LOG_PRINTF_NOLOCK("DEBUG", _msg, ##__VA_ARGS__); \
|
||||
LOGGING_UNLOCK; \
|
||||
} \
|
||||
}
|
||||
|
||||
129
src/main.c
129
src/main.c
@@ -25,7 +25,7 @@
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <signal.h>
|
||||
#include <getopt.h>
|
||||
|
||||
@@ -40,50 +40,69 @@
|
||||
#include "http.h"
|
||||
|
||||
|
||||
static const char _short_opts[] = "d:x:y:f:a:e:z:tn:w:q:c:s:p:h";
|
||||
static const char _short_opts[] = "d:i:x:y:f:a:z:tn:w:q:c:s:p:r:h";
|
||||
static const struct option _long_opts[] = {
|
||||
{"device", required_argument, NULL, 'd'},
|
||||
{"input", required_argument, NULL, 'i'},
|
||||
{"width", required_argument, NULL, 'x'},
|
||||
{"height", required_argument, NULL, 'y'},
|
||||
{"format", required_argument, NULL, 'f'},
|
||||
{"format", required_argument, NULL, 'm'},
|
||||
{"tv-standard", required_argument, NULL, 'a'},
|
||||
{"every-frame", required_argument, NULL, 'e'},
|
||||
{"desired-fps", required_argument, NULL, 'f'},
|
||||
{"min-frame-size", required_argument, NULL, 'z'},
|
||||
{"dv-timings", no_argument, NULL, 't'},
|
||||
{"buffers", required_argument, NULL, 'b'},
|
||||
{"workers", required_argument, NULL, 'w'},
|
||||
{"jpeg-quality", required_argument, NULL, 'q'},
|
||||
{"quality", required_argument, NULL, 'q'},
|
||||
{"encoder", required_argument, NULL, 'c'},
|
||||
{"device-timeout", required_argument, NULL, 1000},
|
||||
{"device-error-timeout", required_argument, NULL, 1001},
|
||||
{"device-persistent", no_argument, NULL, 1001},
|
||||
{"device-error-delay", required_argument, NULL, 1002},
|
||||
|
||||
{"host", required_argument, NULL, 's'},
|
||||
{"port", required_argument, NULL, 'p'},
|
||||
{"fake-width", required_argument, NULL, 2000},
|
||||
{"fake-height", required_argument, NULL, 2001},
|
||||
{"server-timeout", required_argument, NULL, 2002},
|
||||
{"drop-same-frames", required_argument, NULL, 'r'},
|
||||
{"fake-width", required_argument, NULL, 2001},
|
||||
{"fake-height", required_argument, NULL, 2002},
|
||||
{"server-timeout", required_argument, NULL, 2003},
|
||||
|
||||
{"debug", no_argument, NULL, 5000},
|
||||
{"log-level", required_argument, NULL, 5001},
|
||||
{"perf", no_argument, NULL, 5000},
|
||||
{"verbose", no_argument, NULL, 5001},
|
||||
{"debug", no_argument, NULL, 5002},
|
||||
{"log-level", required_argument, NULL, 5010},
|
||||
{"help", no_argument, NULL, 'h'},
|
||||
{"version", no_argument, NULL, 6000},
|
||||
{NULL, 0, NULL, 0},
|
||||
};
|
||||
|
||||
static void _help(struct device_t *dev, struct http_server_t *server) {
|
||||
static void _version(bool nl) {
|
||||
printf(VERSION);
|
||||
# ifdef OMX_ENCODER
|
||||
printf(" + OMX");
|
||||
# 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: %s; license: GPLv3\n", VERSION);
|
||||
printf("Version: ");
|
||||
_version(false);
|
||||
printf("; license: GPLv3\n");
|
||||
printf("Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com>\n\n");
|
||||
printf("Capturing options:\n");
|
||||
printf("------------------\n");
|
||||
printf(" -d|--device </dev/path> -- Path to V4L2 device. Default: %s\n\n", dev->path);
|
||||
printf(" -x|--width <N> -- Initial image width. Default: %d\n\n", dev->width);
|
||||
printf(" -y|--height <N> -- Initial image height. Default: %d\n\n", dev->height);
|
||||
printf(" -f|--format <fmt> -- Image format.\n");
|
||||
printf(" -d|--device </dev/path> -- Path to V4L2 device. Default: %s.\n\n", dev->path);
|
||||
printf(" -i|--input <N> -- Input channel. Default: %u.\n\n", dev->input);
|
||||
printf(" -x|--width <N> -- Initial image width. Default: %d.\n\n", dev->width);
|
||||
printf(" -y|--height <N> -- Initial image height. Default: %d.\n\n", dev->height);
|
||||
printf(" -m|--format <fmt> -- Image format.\n");
|
||||
printf(" Available: %s; default: YUYV.\n\n", FORMATS_STR);
|
||||
printf(" -a|--tv-standard <std> -- Force TV standard.\n");
|
||||
printf(" Available: %s; default: disabled.\n\n", STANDARDS_STR);
|
||||
printf(" -e|--every-frame <N> -- Drop all input frames except specified. Default: disabled.\n\n");
|
||||
printf(" -f|--desired-fps <N> -- Desired FPS; default: maximum as possible.\n\n");
|
||||
printf(" -z|--min-frame-size <N> -- Drop frames smaller then this limit.\n");
|
||||
printf(" Useful if the device produces small-sized garbage frames.\n\n");
|
||||
printf(" -t|--dv-timings -- Enable DV timings queriyng and events processing.\n");
|
||||
@@ -92,40 +111,44 @@ static void _help(struct device_t *dev, struct http_server_t *server) {
|
||||
printf(" Each buffer may processed using an intermediate thread.\n");
|
||||
printf(" Default: %d (number of CPU cores + 1)\n\n", dev->n_buffers);
|
||||
printf(" -w|--workers <N> -- The number of compressing threads. Default: %d (== --buffers).\n\n", dev->n_workers);
|
||||
printf(" -q|--jpeg-quality <N> -- Set quality of JPEG encoding from 1 to 100 (best). Default: %d.\n\n", dev->jpeg_quality);
|
||||
printf(" -q|--quality <N> -- Set quality of JPEG encoding from 1 to 100 (best). Default: %d.\n\n", encoder->quality);
|
||||
printf(" --encoder <type> -- Use specified encoder. It may affects to workers number.\n");
|
||||
printf(" -- Available: %s; default: CPU.\n\n", ENCODER_TYPES_STR);
|
||||
printf(" --device-timeout <seconds> -- Timeout for device querying. Default: %d\n\n", dev->timeout);
|
||||
printf(" --device-error-timeout <seconds> -- Delay before trying to connect to the device again\n");
|
||||
printf(" after a timeout. Default: %d\n\n", dev->error_timeout);
|
||||
printf(" --device-persistent -- Don't re-initialize device on timeout. Default: disabled.\n\n");
|
||||
printf(" --device-error-delay <seconds> -- Delay before trying to connect to the device again\n");
|
||||
printf(" after a timeout. Default: %d\n\n", dev->error_delay);
|
||||
printf("HTTP server options:\n");
|
||||
printf("--------------------\n");
|
||||
printf(" --host <address> -- Listen on Hostname or IP. Default: %s\n\n", server->host);
|
||||
printf(" --port <N> -- Bind to this TCP port. Default: %d\n\n", server->port);
|
||||
printf(" --fake-width <N> -- Override image width for /ping. Default: disabled\n\n");
|
||||
printf(" --fake-height <N> -- Override image height for /ping. Default: disabled.\n\n");
|
||||
printf(" --drop-same-frames <N> -- Don't send same 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(" --fake-width <N> -- Override image width for /state. Default: disabled\n\n");
|
||||
printf(" --fake-height <N> -- Override image height for /state. Default: disabled.\n\n");
|
||||
printf(" --server-timeout <seconds> -- Timeout for client connections. Default: %d\n\n", server->timeout);
|
||||
printf("Misc options:\n");
|
||||
printf("-------------\n");
|
||||
printf(" --debug -- Enabled debug messages (same as --log-level=3). Default: disabled.\n\n");
|
||||
printf(" --log-level <N> -- Verbosity level of messages from 0 (info) to 3 (debug).\n");
|
||||
printf(" Enabling debugging messages can slow down the program.\n");
|
||||
printf(" Available levels: 0=info, 1=performance, 2=verbose, 3=debug.\n");
|
||||
printf(" Default: %d.\n\n", log_level);
|
||||
printf(" --perf -- Enable performance messages (same as log-level=1). Default: disabled.\n\n");
|
||||
printf(" --verbose -- Enable verbose messages and lower (same as log-level=2). Default: disabled.\n\n");
|
||||
printf(" --debug -- Enable debug messages and lower (same as --log-level=3). Default: disabled.\n\n");
|
||||
printf(" -h|--help -- Print this messages and exit.\n\n");
|
||||
}
|
||||
|
||||
static int _parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server) {
|
||||
# define OPT_ARG(_dest) \
|
||||
{ _dest = optarg; break; }
|
||||
|
||||
# define OPT_TRUE(_dest) \
|
||||
{ _dest = true; break; }
|
||||
# define OPT_SET(_dest, _value) \
|
||||
{ _dest = _value; break; }
|
||||
|
||||
# define OPT_UNSIGNED(_dest, _name, _min, _max) \
|
||||
{ errno = 0; int _tmp = strtol(optarg, NULL, 0); \
|
||||
if (errno || _tmp < _min || _tmp > _max) \
|
||||
{ printf("Invalid value for '%s=%u'; minimal=%u; maximum=%u\n", _name, _tmp, _min, _max); return -1; } \
|
||||
{ printf("Invalid value for '%s=%u'; min=%u; max=%u\n", _name, _tmp, _min, _max); return -1; } \
|
||||
_dest = _tmp; break; }
|
||||
|
||||
# define OPT_PARSE(_dest, _func, _invalid, _name) \
|
||||
@@ -139,41 +162,47 @@ static int _parse_options(int argc, char *argv[], struct device_t *dev, struct e
|
||||
log_level = LOG_LEVEL_INFO;
|
||||
while ((ch = getopt_long(argc, argv, _short_opts, _long_opts, &index)) >= 0) {
|
||||
switch (ch) {
|
||||
case 'd': OPT_ARG(dev->path);
|
||||
case 'd': OPT_SET(dev->path, optarg);
|
||||
case 'i': OPT_UNSIGNED(dev->input, "--input", 0, 128);
|
||||
case 'x': OPT_UNSIGNED(dev->width, "--width", 320, 1920);
|
||||
case 'y': OPT_UNSIGNED(dev->height, "--height", 180, 1200);
|
||||
# pragma GCC diagnostic ignored "-Wsign-compare"
|
||||
# pragma GCC diagnostic push
|
||||
case 'f': OPT_PARSE(dev->format, device_parse_format, FORMAT_UNKNOWN, "pixel format");
|
||||
case 'm': OPT_PARSE(dev->format, device_parse_format, FORMAT_UNKNOWN, "pixel format");
|
||||
# pragma GCC diagnostic pop
|
||||
case 'a': OPT_PARSE(dev->standard, device_parse_standard, STANDARD_UNKNOWN, "TV standard");
|
||||
case 'e': OPT_UNSIGNED(dev->every_frame, "--every-frame", 1, 30);
|
||||
case 'f': OPT_UNSIGNED(dev->desired_fps, "--desired-fps", 0, 30);
|
||||
case 'z': OPT_UNSIGNED(dev->min_frame_size, "--min-frame-size", 0, 8192);
|
||||
case 't': OPT_TRUE(dev->dv_timings);
|
||||
case 't': OPT_SET(dev->dv_timings, true);
|
||||
case 'b': OPT_UNSIGNED(dev->n_buffers, "--buffers", 1, 32);
|
||||
case 'w': OPT_UNSIGNED(dev->n_workers, "--workers", 1, 32);
|
||||
case 'q': OPT_UNSIGNED(dev->jpeg_quality, "--jpeg-quality", 1, 100);
|
||||
case 'c': OPT_PARSE(encoder->type, encoder_parse_type, ENCODER_TYPE_UNKNOWN, "encoder type")
|
||||
case 1000: OPT_UNSIGNED(dev->timeout, "--timeout", 1, 60);
|
||||
case 1001: OPT_UNSIGNED(dev->error_timeout, "--error-timeout", 1, 60);
|
||||
case 'q': OPT_UNSIGNED(encoder->quality, "--quality", 1, 100);
|
||||
case 'c': OPT_PARSE(encoder->type, encoder_parse_type, ENCODER_TYPE_UNKNOWN, "encoder type");
|
||||
case 1000: OPT_UNSIGNED(dev->timeout, "--device-timeout", 1, 60);
|
||||
case 1001: OPT_SET(dev->persistent, true);
|
||||
case 1002: OPT_UNSIGNED(dev->error_delay, "--device-error-delay", 1, 60);
|
||||
|
||||
case 's': server->host = optarg; break;
|
||||
case 's': OPT_SET(server->host, optarg);
|
||||
case 'p': OPT_UNSIGNED(server->port, "--port", 1, 65535);
|
||||
case 2000: OPT_UNSIGNED(server->fake_width, "--fake-width", 0, 1920);
|
||||
case 2001: OPT_UNSIGNED(server->fake_height, "--fake-height", 0, 1200);
|
||||
case 2002: OPT_UNSIGNED(server->timeout, "--server-timeout", 1, 60);
|
||||
case 'r': OPT_UNSIGNED(server->drop_same_frames, "--drop-same-frames", 0, 30);
|
||||
case 2001: OPT_UNSIGNED(server->fake_width, "--fake-width", 0, 1920);
|
||||
case 2002: OPT_UNSIGNED(server->fake_height, "--fake-height", 0, 1200);
|
||||
case 2003: OPT_UNSIGNED(server->timeout, "--server-timeout", 1, 60);
|
||||
|
||||
case 5000: log_level = LOG_LEVEL_DEBUG; break;
|
||||
case 5001: OPT_UNSIGNED(log_level, "--log-level", 0, 3);
|
||||
case 5000: OPT_SET(log_level, LOG_LEVEL_PERF);
|
||||
case 5001: OPT_SET(log_level, LOG_LEVEL_VERBOSE);
|
||||
case 5002: OPT_SET(log_level, LOG_LEVEL_DEBUG);
|
||||
case 5010: OPT_UNSIGNED(log_level, "--log-level", 0, 3);
|
||||
case 'h': _help(dev, encoder, server); return 1;
|
||||
case 6000: _version(true); return 1;
|
||||
case 0: break;
|
||||
case 'h': default: _help(dev, server); return -1;
|
||||
default: _help(dev, encoder, server); return -1;
|
||||
}
|
||||
}
|
||||
|
||||
# undef OPT_PARSE
|
||||
# undef OPT_UNSIGNED
|
||||
# undef OPT_TRUE
|
||||
# undef OPT_ARG
|
||||
# undef OPT_SET
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -239,13 +268,13 @@ int main(int argc, char *argv[]) {
|
||||
LOGGING_INIT;
|
||||
|
||||
dev = device_init();
|
||||
encoder = encoder_init(ENCODER_TYPE_CPU);
|
||||
encoder = encoder_init();
|
||||
stream = stream_init(dev, encoder);
|
||||
server = http_server_init(stream);
|
||||
|
||||
if ((exit_code = _parse_options(argc, argv, dev, encoder, server)) == 0) {
|
||||
_install_signal_handlers();
|
||||
encoder_prepare(encoder);
|
||||
encoder_prepare(encoder, dev);
|
||||
|
||||
pthread_t stream_loop_tid;
|
||||
pthread_t server_loop_tid;
|
||||
@@ -269,5 +298,5 @@ int main(int argc, char *argv[]) {
|
||||
device_destroy(dev);
|
||||
|
||||
LOGGING_DESTROY;
|
||||
return abs(exit_code);
|
||||
return (exit_code < 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@
|
||||
|
||||
int component_enable_port(OMX_HANDLETYPE *component, const OMX_U32 port);
|
||||
int component_disable_port(OMX_HANDLETYPE *component, const OMX_U32 port);
|
||||
|
||||
int component_get_portdef(OMX_HANDLETYPE *component, OMX_PARAM_PORTDEFINITIONTYPE *portdef, const OMX_U32 port);
|
||||
int component_set_portdef(OMX_HANDLETYPE *component, OMX_PARAM_PORTDEFINITIONTYPE *portdef);
|
||||
|
||||
int component_set_state(OMX_HANDLETYPE *component, const OMX_STATETYPE state);
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
*****************************************************************************/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
@@ -28,6 +29,7 @@
|
||||
#include <bcm_host.h>
|
||||
#include <IL/OMX_Core.h>
|
||||
#include <IL/OMX_Component.h>
|
||||
#include <IL/OMX_Broadcom.h>
|
||||
#include <interface/vcos/vcos_semaphore.h>
|
||||
|
||||
#include "../logging.h"
|
||||
@@ -43,10 +45,13 @@
|
||||
#define OUTPUT_PORT 341
|
||||
|
||||
|
||||
static int _i_omx = 0;
|
||||
|
||||
|
||||
static int _omx_init_component(struct omx_encoder_t *omx);
|
||||
static int _omx_init_disable_ports(struct omx_encoder_t *omx);
|
||||
static int _omx_setup_input(struct omx_encoder_t *omx, struct device_t *dev);
|
||||
static int _omx_setup_output(struct omx_encoder_t *omx, struct device_t *dev);
|
||||
static int _omx_setup_output(struct omx_encoder_t *omx, const unsigned quality);
|
||||
static int _omx_encoder_clear_ports(struct omx_encoder_t *omx);
|
||||
|
||||
static OMX_ERRORTYPE _omx_event_handler(UNUSED OMX_HANDLETYPE encoder,
|
||||
@@ -75,17 +80,20 @@ struct omx_encoder_t *omx_encoder_init() {
|
||||
|
||||
A_CALLOC(omx, 1);
|
||||
|
||||
LOG_INFO("Initializing OMX JPEG encoder ...");
|
||||
assert(_i_omx >= 0);
|
||||
if (_i_omx == 0) {
|
||||
LOG_INFO("Initializing BCM ...");
|
||||
bcm_host_init();
|
||||
|
||||
LOG_DEBUG("Initializing BCM ...");
|
||||
bcm_host_init();
|
||||
|
||||
LOG_DEBUG("Initializing OMX ...");
|
||||
if ((error = OMX_Init()) != OMX_ErrorNone) {
|
||||
LOG_OMX_ERROR(error, "Can't initialize OMX");
|
||||
goto error;
|
||||
LOG_INFO("Initializing OMX ...");
|
||||
if ((error = OMX_Init()) != OMX_ErrorNone) {
|
||||
LOG_OMX_ERROR(error, "Can't initialize OMX");
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
omx->i_omx = true;
|
||||
_i_omx += 1;
|
||||
|
||||
LOG_INFO("Initializing OMX JPEG encoder ...");
|
||||
|
||||
if (vcos_semaphore_create(&omx->handler_lock, "handler_lock", 0) != VCOS_SUCCESS) {
|
||||
LOG_ERROR("Can't create VCOS semaphore");
|
||||
@@ -127,14 +135,20 @@ void omx_encoder_destroy(struct omx_encoder_t *omx) {
|
||||
}
|
||||
}
|
||||
|
||||
if (omx->i_omx) {
|
||||
assert(_i_omx >= 0);
|
||||
_i_omx -= 1;
|
||||
if (_i_omx == 0) {
|
||||
LOG_INFO("Destroying OMX ...");
|
||||
OMX_Deinit();
|
||||
|
||||
LOG_INFO("Destroying BCM ...");
|
||||
bcm_host_deinit();
|
||||
}
|
||||
bcm_host_deinit();
|
||||
|
||||
free(omx);
|
||||
}
|
||||
|
||||
int omx_encoder_prepare_for_device(struct omx_encoder_t *omx, struct device_t *dev) {
|
||||
int omx_encoder_prepare_live(struct omx_encoder_t *omx, struct device_t *dev, const unsigned quality) {
|
||||
if (component_set_state(&omx->encoder, OMX_StateIdle) < 0) {
|
||||
return -1;
|
||||
}
|
||||
@@ -144,7 +158,7 @@ int omx_encoder_prepare_for_device(struct omx_encoder_t *omx, struct device_t *d
|
||||
if (_omx_setup_input(omx, dev) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (_omx_setup_output(omx, dev) < 0) {
|
||||
if (_omx_setup_output(omx, quality) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (component_set_state(&omx->encoder, OMX_StateExecuting) < 0) {
|
||||
@@ -153,7 +167,7 @@ int omx_encoder_prepare_for_device(struct omx_encoder_t *omx, struct device_t *d
|
||||
return 0;
|
||||
}
|
||||
|
||||
int omx_encoder_compress_buffer(struct omx_encoder_t *omx, struct device_t *dev, int index) {
|
||||
int omx_encoder_compress_buffer(struct omx_encoder_t *omx, struct device_t *dev, const unsigned index) {
|
||||
OMX_ERRORTYPE error;
|
||||
bool loaded = false;
|
||||
|
||||
@@ -179,6 +193,7 @@ int omx_encoder_compress_buffer(struct omx_encoder_t *omx, struct device_t *dev,
|
||||
omx->output_buffer->pBuffer,
|
||||
omx->output_buffer->nFilledLen
|
||||
);
|
||||
assert(dev->run->pictures[index].size + omx->output_buffer->nFilledLen <= dev->run->max_picture_size);
|
||||
dev->run->pictures[index].size += omx->output_buffer->nFilledLen;
|
||||
|
||||
if (omx->output_buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) {
|
||||
@@ -215,6 +230,8 @@ int omx_encoder_compress_buffer(struct omx_encoder_t *omx, struct device_t *dev,
|
||||
}
|
||||
|
||||
static int _omx_init_component(struct omx_encoder_t *omx) {
|
||||
// http://home.nouwen.name/RaspberryPi/documentation/ilcomponents/image_encode.html
|
||||
|
||||
OMX_ERRORTYPE error;
|
||||
|
||||
OMX_CALLBACKTYPE callbacks;
|
||||
@@ -312,10 +329,9 @@ static int _omx_setup_input(struct omx_encoder_t *omx, struct device_t *dev) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _omx_setup_output(struct omx_encoder_t *omx, struct device_t *dev) {
|
||||
static int _omx_setup_output(struct omx_encoder_t *omx, const unsigned quality) {
|
||||
OMX_ERRORTYPE error;
|
||||
OMX_PARAM_PORTDEFINITIONTYPE portdef;
|
||||
OMX_IMAGE_PARAM_QFACTORTYPE quality_factor;
|
||||
|
||||
LOG_DEBUG("Setting up OMX JPEG output port ...");
|
||||
|
||||
@@ -337,13 +353,42 @@ static int _omx_setup_output(struct omx_encoder_t *omx, struct device_t *dev) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
OMX_INIT_STRUCTURE(quality_factor);
|
||||
quality_factor.nPortIndex = OUTPUT_PORT;
|
||||
quality_factor.nQFactor = dev->jpeg_quality;
|
||||
{
|
||||
OMX_CONFIG_BOOLEANTYPE exif;
|
||||
|
||||
if ((error = OMX_SetParameter(omx->encoder, OMX_IndexParamQFactor, &quality_factor)) != OMX_ErrorNone) {
|
||||
LOG_OMX_ERROR(error, "Can't set OMX JPEG quality");
|
||||
return -1;
|
||||
OMX_INIT_STRUCTURE(exif);
|
||||
exif.bEnabled = OMX_FALSE;
|
||||
|
||||
if ((error = OMX_SetParameter(omx->encoder, OMX_IndexParamBrcmDisableEXIF, &exif)) != OMX_ErrorNone) {
|
||||
LOG_OMX_ERROR(error, "Can't disable EXIF on OMX JPEG");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
OMX_PARAM_IJGSCALINGTYPE ijg;
|
||||
|
||||
OMX_INIT_STRUCTURE(ijg);
|
||||
ijg.nPortIndex = OUTPUT_PORT;
|
||||
ijg.bEnabled = OMX_TRUE;
|
||||
|
||||
if ((error = OMX_SetParameter(omx->encoder, OMX_IndexParamBrcmEnableIJGTableScaling, &ijg)) != OMX_ErrorNone) {
|
||||
LOG_OMX_ERROR(error, "Can't set OMX JPEG IJG settings");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
OMX_IMAGE_PARAM_QFACTORTYPE qfactor;
|
||||
|
||||
OMX_INIT_STRUCTURE(qfactor);
|
||||
qfactor.nPortIndex = OUTPUT_PORT;
|
||||
qfactor.nQFactor = quality;
|
||||
|
||||
if ((error = OMX_SetParameter(omx->encoder, OMX_IndexParamQFactor, &qfactor)) != OMX_ErrorNone) {
|
||||
LOG_OMX_ERROR(error, "Can't set OMX JPEG quality");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (component_enable_port(&omx->encoder, OUTPUT_PORT) < 0) {
|
||||
|
||||
@@ -29,6 +29,9 @@
|
||||
#include "../device.h"
|
||||
|
||||
|
||||
#define OMX_MAX_ENCODERS 3
|
||||
|
||||
|
||||
struct omx_encoder_t {
|
||||
OMX_HANDLETYPE encoder;
|
||||
OMX_BUFFERHEADERTYPE *input_buffer;
|
||||
@@ -38,7 +41,6 @@ struct omx_encoder_t {
|
||||
bool failed;
|
||||
VCOS_SEMAPHORE_T handler_lock;
|
||||
|
||||
bool i_omx;
|
||||
bool i_handler_lock;
|
||||
bool i_encoder;
|
||||
bool i_input_port_enabled;
|
||||
@@ -49,5 +51,5 @@ struct omx_encoder_t {
|
||||
struct omx_encoder_t *omx_encoder_init();
|
||||
void omx_encoder_destroy(struct omx_encoder_t *omx);
|
||||
|
||||
int omx_encoder_prepare_for_device(struct omx_encoder_t *omx, struct device_t *dev);
|
||||
int omx_encoder_compress_buffer(struct omx_encoder_t *omx, struct device_t *dev, int index);
|
||||
int omx_encoder_prepare_live(struct omx_encoder_t *omx, struct device_t *dev, const unsigned quality);
|
||||
int omx_encoder_compress_buffer(struct omx_encoder_t *omx, struct device_t *dev, const unsigned index);
|
||||
|
||||
@@ -29,12 +29,12 @@
|
||||
#include "formatters.h"
|
||||
|
||||
|
||||
#define CASE_TO_STRING(_val) \
|
||||
case _val: { return #_val; }
|
||||
#define CASE_TO_STRING(_value) \
|
||||
case _value: { return #_value; }
|
||||
|
||||
#define CASE_ASSERT(_msg, _val) default: { \
|
||||
#define CASE_ASSERT(_msg, _value) default: { \
|
||||
char *_buf; A_CALLOC(_buf, 128); \
|
||||
sprintf(_buf, _msg ": 0x%08x", _val); \
|
||||
sprintf(_buf, _msg ": 0x%08x", _value); \
|
||||
assert(0 && _buf); \
|
||||
}
|
||||
|
||||
@@ -78,5 +78,5 @@ const char *omx_state_to_string(const OMX_STATETYPE state) {
|
||||
}
|
||||
}
|
||||
|
||||
#undef CASE_TO_STRING
|
||||
#undef CASE_ASSERT
|
||||
#undef CASE_TO_STRING
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
|
||||
#define LOG_OMX_ERROR(_error, _msg, ...) { \
|
||||
LOGGING_LOCK; \
|
||||
printf("-- ERROR [%.03Lf tid=%ld] -- " _msg ": %s\n", now_ms_ld(), \
|
||||
printf("-- ERROR [%.03Lf tid=%ld] -- " _msg ": %s\n", get_now_monotonic(), \
|
||||
syscall(SYS_gettid), ##__VA_ARGS__, omx_error_to_string(_error)); \
|
||||
LOGGING_UNLOCK; \
|
||||
}
|
||||
|
||||
159
src/stream.c
159
src/stream.c
@@ -78,20 +78,22 @@ void stream_loop(struct stream_t *stream) {
|
||||
pool.workers_stop = &workers_stop;
|
||||
|
||||
LOG_INFO("Using V4L2 device: %s", stream->dev->path);
|
||||
LOG_INFO("Using JPEG quality: %d%%", stream->dev->jpeg_quality);
|
||||
LOG_INFO("Using desired FPS: %u", stream->dev->desired_fps);
|
||||
|
||||
while (_stream_init_loop(stream->dev, &pool) == 0) {
|
||||
struct worker_t *oldest_worker = NULL;
|
||||
struct worker_t *last_worker = NULL;
|
||||
unsigned frames_count = 0;
|
||||
long double grab_after = 0;
|
||||
unsigned fluency_passed = 0;
|
||||
unsigned fps = 0;
|
||||
long long fps_second = 0;
|
||||
unsigned captured_fps_accum = 0;
|
||||
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_picture_size);
|
||||
|
||||
LOG_INFO("Capturing ...");
|
||||
|
||||
while (!stream->dev->stop) {
|
||||
int free_worker_number = -1;
|
||||
|
||||
@@ -112,9 +114,9 @@ void stream_loop(struct stream_t *stream) {
|
||||
free_worker_number = oldest_worker->ctx.number;
|
||||
oldest_worker = oldest_worker->order_next;
|
||||
|
||||
LOG_PERF("##### ACCEPT : %u", free_worker_number);
|
||||
LOG_PERF("##### Raw frame accepted; worker = %u", free_worker_number);
|
||||
} else {
|
||||
for (unsigned number = 0; number < stream->dev->run->n_workers; ++number) {
|
||||
for (unsigned number = 0; number < stream->dev->n_workers; ++number) {
|
||||
if (!pool.workers[number].has_job && (free_worker_number == -1
|
||||
|| pool.workers[free_worker_number].job_start_time < pool.workers[number].job_start_time
|
||||
)) {
|
||||
@@ -126,7 +128,7 @@ void stream_loop(struct stream_t *stream) {
|
||||
assert(free_worker_number >= 0);
|
||||
assert(!pool.workers[free_worker_number].has_job);
|
||||
|
||||
LOG_PERF("----- DROP : %u", free_worker_number);
|
||||
LOG_PERF("----- Raw frame dropped; worker = %u", free_worker_number);
|
||||
}
|
||||
|
||||
if (stream->dev->stop) {
|
||||
@@ -155,27 +157,31 @@ void stream_loop(struct stream_t *stream) {
|
||||
}
|
||||
|
||||
} else if (retval == 0) {
|
||||
LOG_ERROR("Mainloop select() timeout");
|
||||
break;
|
||||
if (stream->dev->persistent) {
|
||||
if (!persistent_timeout_reported) {
|
||||
LOG_ERROR("Mainloop select() timeout, polling ...")
|
||||
persistent_timeout_reported = true;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
LOG_ERROR("Mainloop select() timeout");
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
persistent_timeout_reported = false;
|
||||
|
||||
if (FD_ISSET(stream->dev->run->fd, &read_fds)) {
|
||||
LOG_DEBUG("Frame is ready");
|
||||
|
||||
struct v4l2_buffer buf_info;
|
||||
long double now = get_now_monotonic();
|
||||
long long now_second = floor_ms(now);
|
||||
|
||||
if (_stream_grab_buffer(stream->dev, &buf_info) < 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (stream->dev->every_frame) {
|
||||
if (frames_count < stream->dev->every_frame - 1) {
|
||||
LOG_DEBUG("Dropping frame %d for option --every-frame=%d", frames_count + 1, stream->dev->every_frame);
|
||||
++frames_count;
|
||||
goto pass_frame;
|
||||
}
|
||||
frames_count = 0;
|
||||
}
|
||||
stream->dev->run->pictures[buf_info.index].grab_time = now;
|
||||
|
||||
// Workaround for broken, corrupted frames:
|
||||
// Under low light conditions corrupted frames may get captured.
|
||||
@@ -188,8 +194,6 @@ void stream_loop(struct stream_t *stream) {
|
||||
}
|
||||
|
||||
{
|
||||
long double now = now_ms_ld();
|
||||
|
||||
if (now < grab_after) {
|
||||
fluency_passed += 1;
|
||||
LOG_VERBOSE("Passed %u frames for fluency: now=%.03Lf; grab_after=%.03Lf", fluency_passed, now, grab_after);
|
||||
@@ -197,14 +201,13 @@ void stream_loop(struct stream_t *stream) {
|
||||
}
|
||||
fluency_passed = 0;
|
||||
|
||||
if (log_level >= LOG_LEVEL_PERF) {
|
||||
if ((long long)now != fps_second) {
|
||||
LOG_PERF("Oldest worker complete, encoding FPS = %u", fps);
|
||||
fps = 0;
|
||||
fps_second = (long long)now;
|
||||
}
|
||||
++fps;
|
||||
if (now_second != captured_fps_second) {
|
||||
stream->captured_fps = captured_fps_accum;
|
||||
captured_fps_accum = 0;
|
||||
captured_fps_second = now_second;
|
||||
LOG_PERF("Oldest worker complete, Captured-FPS = %u", stream->captured_fps);
|
||||
}
|
||||
captured_fps_accum += 1;
|
||||
|
||||
long double fluency_delay = _stream_get_fluency_delay(stream->dev, &pool);
|
||||
|
||||
@@ -285,34 +288,53 @@ void stream_loop_break(struct stream_t *stream) {
|
||||
}
|
||||
|
||||
static void _stream_expose_picture(struct stream_t *stream, unsigned buf_index) {
|
||||
# define PICTURE(_next) stream->dev->run->pictures[buf_index]._next
|
||||
|
||||
A_PTHREAD_M_LOCK(&stream->mutex);
|
||||
|
||||
stream->picture.size = stream->dev->run->pictures[buf_index].size;
|
||||
stream->picture.allocated = stream->dev->run->pictures[buf_index].allocated;
|
||||
stream->picture.size = PICTURE(size);
|
||||
stream->picture.allocated = PICTURE(allocated);
|
||||
memcpy(
|
||||
stream->picture.data, stream->dev->run->pictures[buf_index].data,
|
||||
stream->picture.data, PICTURE(data),
|
||||
stream->picture.size * sizeof(*stream->picture.data)
|
||||
);
|
||||
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->updated = true;
|
||||
|
||||
A_PTHREAD_M_UNLOCK(&stream->mutex);
|
||||
|
||||
# undef PICTURE
|
||||
}
|
||||
|
||||
static long double _stream_get_fluency_delay(struct device_t *dev, struct workers_pool_t *pool) {
|
||||
long double delay = 0;
|
||||
long double sum_comp_time = 0;
|
||||
long double avg_comp_time;
|
||||
long double min_delay;
|
||||
long double soft_delay;
|
||||
|
||||
for (unsigned number = 0; number < dev->run->n_workers; ++number) {
|
||||
for (unsigned number = 0; number < dev->n_workers; ++number) {
|
||||
A_PTHREAD_M_LOCK(&pool->workers[number].last_comp_time_mutex);
|
||||
if (pool->workers[number].last_comp_time > 0) {
|
||||
delay += pool->workers[number].last_comp_time;
|
||||
sum_comp_time += pool->workers[number].last_comp_time;
|
||||
}
|
||||
A_PTHREAD_M_UNLOCK(&pool->workers[number].last_comp_time_mutex);
|
||||
}
|
||||
// Среднее арифметическое деленное на количество воркеров
|
||||
return delay / dev->run->n_workers / dev->run->n_workers;
|
||||
avg_comp_time = sum_comp_time / dev->n_workers; // Среднее время работы воркеров
|
||||
|
||||
min_delay = avg_comp_time / dev->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 device_t *dev, struct workers_pool_t *pool) {
|
||||
@@ -321,8 +343,8 @@ static int _stream_init_loop(struct device_t *dev, struct workers_pool_t *pool)
|
||||
LOG_DEBUG("%s: *dev->stop = %d", __FUNCTION__, dev->stop);
|
||||
while (!dev->stop) {
|
||||
if ((retval = _stream_init(dev, pool)) < 0) {
|
||||
LOG_INFO("Sleeping %d seconds before new stream init ...", dev->error_timeout);
|
||||
sleep(dev->error_timeout);
|
||||
LOG_INFO("Sleeping %d seconds before new stream init ...", dev->error_delay);
|
||||
sleep(dev->error_delay);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -344,7 +366,7 @@ static int _stream_init(struct device_t *dev, struct workers_pool_t *pool) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
encoder_prepare_for_device(pool->encoder, dev);
|
||||
encoder_prepare_live(pool->encoder, dev);
|
||||
|
||||
_stream_init_workers(dev, pool);
|
||||
|
||||
@@ -356,39 +378,43 @@ static int _stream_init(struct device_t *dev, struct workers_pool_t *pool) {
|
||||
}
|
||||
|
||||
static void _stream_init_workers(struct device_t *dev, struct workers_pool_t *pool) {
|
||||
LOG_INFO("Spawning %d workers ...", dev->run->n_workers);
|
||||
LOG_INFO("Spawning %d workers ...", dev->n_workers);
|
||||
|
||||
*pool->workers_stop = false;
|
||||
A_CALLOC(pool->workers, dev->run->n_workers);
|
||||
A_CALLOC(pool->workers, dev->n_workers);
|
||||
|
||||
A_PTHREAD_M_INIT(&pool->free_workers_mutex);
|
||||
A_PTHREAD_C_INIT(&pool->free_workers_cond);
|
||||
|
||||
for (unsigned number = 0; number < dev->run->n_workers; ++number) {
|
||||
for (unsigned number = 0; number < dev->n_workers; ++number) {
|
||||
pool->free_workers += 1;
|
||||
|
||||
A_PTHREAD_M_INIT(&pool->workers[number].has_job_mutex);
|
||||
A_PTHREAD_C_INIT(&pool->workers[number].has_job_cond);
|
||||
|
||||
pool->workers[number].ctx.number = number;
|
||||
pool->workers[number].ctx.dev = dev;
|
||||
pool->workers[number].ctx.dev_stop = (sig_atomic_t *volatile)&dev->stop;
|
||||
pool->workers[number].ctx.workers_stop = pool->workers_stop;
|
||||
# define CTX(_next) pool->workers[number].ctx._next
|
||||
|
||||
pool->workers[number].ctx.encoder = pool->encoder;
|
||||
CTX(number) = number;
|
||||
CTX(dev) = dev;
|
||||
CTX(dev_stop) = (sig_atomic_t *volatile)&dev->stop;
|
||||
CTX(workers_stop) = pool->workers_stop;
|
||||
|
||||
pool->workers[number].ctx.last_comp_time_mutex = &pool->workers[number].last_comp_time_mutex;
|
||||
pool->workers[number].ctx.last_comp_time = &pool->workers[number].last_comp_time;
|
||||
CTX(encoder) = pool->encoder;
|
||||
|
||||
pool->workers[number].ctx.has_job_mutex = &pool->workers[number].has_job_mutex;
|
||||
pool->workers[number].ctx.has_job = &pool->workers[number].has_job;
|
||||
pool->workers[number].ctx.job_failed = &pool->workers[number].job_failed;
|
||||
pool->workers[number].ctx.job_start_time = &pool->workers[number].job_start_time;
|
||||
pool->workers[number].ctx.has_job_cond = &pool->workers[number].has_job_cond;
|
||||
CTX(last_comp_time_mutex) = &pool->workers[number].last_comp_time_mutex;
|
||||
CTX(last_comp_time) = &pool->workers[number].last_comp_time;
|
||||
|
||||
pool->workers[number].ctx.free_workers_mutex = &pool->free_workers_mutex;
|
||||
pool->workers[number].ctx.free_workers = &pool->free_workers;
|
||||
pool->workers[number].ctx.free_workers_cond = &pool->free_workers_cond;
|
||||
CTX(has_job_mutex) = &pool->workers[number].has_job_mutex;
|
||||
CTX(has_job) = &pool->workers[number].has_job;
|
||||
CTX(job_failed) = &pool->workers[number].job_failed;
|
||||
CTX(job_start_time) = &pool->workers[number].job_start_time;
|
||||
CTX(has_job_cond) = &pool->workers[number].has_job_cond;
|
||||
|
||||
CTX(free_workers_mutex) = &pool->free_workers_mutex;
|
||||
CTX(free_workers) = &pool->free_workers;
|
||||
CTX(free_workers_cond) = &pool->free_workers_cond;
|
||||
|
||||
# undef CTX
|
||||
|
||||
A_PTHREAD_CREATE(&pool->workers[number].tid, _stream_worker_thread, (void *)&pool->workers[number].ctx);
|
||||
}
|
||||
@@ -405,23 +431,22 @@ static void *_stream_worker_thread(void *v_ctx) {
|
||||
A_PTHREAD_C_WAIT_TRUE(*ctx->has_job, ctx->has_job_cond, ctx->has_job_mutex);
|
||||
A_PTHREAD_M_UNLOCK(ctx->has_job_mutex);
|
||||
|
||||
# define PICTURE(_next) ctx->dev->run->pictures[ctx->buf_index]._next
|
||||
|
||||
if (!*ctx->workers_stop) {
|
||||
long double start_time;
|
||||
long double last_comp_time;
|
||||
|
||||
start_time = now_ms_ld();
|
||||
|
||||
LOG_DEBUG("Worker %u compressing JPEG from buffer %d ...", ctx->number, ctx->buf_index);
|
||||
|
||||
if (encoder_compress_buffer(ctx->encoder, ctx->dev, ctx->buf_index) < 0) {
|
||||
PICTURE(encode_begin_time) = get_now_monotonic();
|
||||
if (encoder_compress_buffer(ctx->encoder, ctx->dev, ctx->number, ctx->buf_index) < 0) {
|
||||
*ctx->job_failed = true;
|
||||
}
|
||||
PICTURE(encode_end_time) = get_now_monotonic();
|
||||
|
||||
if (_stream_release_buffer(ctx->dev, &ctx->buf_info) == 0) {
|
||||
*ctx->job_start_time = start_time;
|
||||
*ctx->job_start_time = PICTURE(encode_begin_time);
|
||||
*ctx->has_job = false;
|
||||
|
||||
last_comp_time = now_ms_ld() - start_time;
|
||||
long double last_comp_time = PICTURE(encode_end_time) - *ctx->job_start_time;
|
||||
|
||||
A_PTHREAD_M_LOCK(ctx->last_comp_time_mutex);
|
||||
*ctx->last_comp_time = last_comp_time;
|
||||
@@ -429,7 +454,7 @@ static void *_stream_worker_thread(void *v_ctx) {
|
||||
|
||||
LOG_VERBOSE(
|
||||
"Compressed JPEG size=%ld; time=%0.3Lf; worker=%u; buffer=%d",
|
||||
ctx->dev->run->pictures[ctx->buf_index].size, last_comp_time, ctx->number, ctx->buf_index
|
||||
PICTURE(size), last_comp_time, ctx->number, ctx->buf_index
|
||||
);
|
||||
} else {
|
||||
*ctx->job_failed = true;
|
||||
@@ -437,6 +462,8 @@ static void *_stream_worker_thread(void *v_ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
# undef PICTURE
|
||||
|
||||
A_PTHREAD_M_LOCK(ctx->free_workers_mutex);
|
||||
*ctx->free_workers += 1;
|
||||
A_PTHREAD_M_UNLOCK(ctx->free_workers_mutex);
|
||||
@@ -452,7 +479,7 @@ static void _stream_destroy_workers(struct device_t *dev, struct workers_pool_t
|
||||
LOG_INFO("Destroying workers ...");
|
||||
|
||||
*pool->workers_stop = true;
|
||||
for (unsigned number = 0; number < dev->run->n_workers; ++number) {
|
||||
for (unsigned number = 0; number < dev->n_workers; ++number) {
|
||||
A_PTHREAD_M_LOCK(&pool->workers[number].has_job_mutex);
|
||||
pool->workers[number].has_job = true; // Final job: die
|
||||
A_PTHREAD_M_UNLOCK(&pool->workers[number].has_job_mutex);
|
||||
|
||||
@@ -72,7 +72,7 @@ struct worker_t {
|
||||
};
|
||||
|
||||
struct workers_pool_t {
|
||||
struct worker_t*workers;
|
||||
struct worker_t *workers;
|
||||
bool *workers_stop;
|
||||
|
||||
pthread_mutex_t free_workers_mutex;
|
||||
@@ -86,6 +86,7 @@ struct stream_t {
|
||||
struct picture_t picture;
|
||||
unsigned width;
|
||||
unsigned height;
|
||||
unsigned captured_fps;
|
||||
bool updated;
|
||||
pthread_mutex_t mutex;
|
||||
struct device_t *dev;
|
||||
|
||||
29
src/tools.h
29
src/tools.h
@@ -23,6 +23,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
#include <pthread.h>
|
||||
@@ -49,22 +50,30 @@
|
||||
|
||||
#define A_CALLOC(_dest, _nmemb) assert((_dest = calloc(_nmemb, sizeof(*(_dest)))))
|
||||
#define A_REALLOC(_dest, _nmemb) assert((_dest = realloc(_dest, _nmemb * sizeof(*(_dest)))))
|
||||
#define MEMSET_ZERO(_x_obj) memset(&(_x_obj), 0, sizeof(_x_obj))
|
||||
#define MEMSET_ZERO_PTR(_x_ptr) memset(_x_ptr, 0, sizeof(*(_x_ptr)))
|
||||
#define MEMSET_ZERO(_obj) memset(&(_obj), 0, sizeof(_obj))
|
||||
#define MEMSET_ZERO_PTR(_ptr) memset(_ptr, 0, sizeof(*(_ptr)))
|
||||
|
||||
|
||||
#define INLINE inline __attribute__((always_inline))
|
||||
#define UNUSED __attribute__((unused))
|
||||
|
||||
|
||||
INLINE char *bool_to_string(const bool flag) {
|
||||
return (flag ? "true" : "false");
|
||||
}
|
||||
|
||||
INLINE unsigned max_u(unsigned a, unsigned b) {
|
||||
return (a > b ? a : b);
|
||||
}
|
||||
|
||||
INLINE void now_ms(time_t *sec, long *msec) {
|
||||
INLINE long long floor_ms(long double now) {
|
||||
return (long long) now - (now < (long long) now); // floor()
|
||||
}
|
||||
|
||||
INLINE void get_now(clockid_t clk_id, time_t *sec, long *msec) {
|
||||
struct timespec spec;
|
||||
|
||||
assert(!clock_gettime(CLOCK_MONOTONIC_RAW, &spec));
|
||||
assert(!clock_gettime(clk_id, &spec));
|
||||
*sec = spec.tv_sec;
|
||||
*msec = round(spec.tv_nsec / 1.0e6);
|
||||
|
||||
@@ -74,10 +83,18 @@ INLINE void now_ms(time_t *sec, long *msec) {
|
||||
}
|
||||
}
|
||||
|
||||
INLINE long double now_ms_ld(void) {
|
||||
INLINE long double get_now_monotonic(void) {
|
||||
time_t sec;
|
||||
long msec;
|
||||
|
||||
now_ms(&sec, &msec);
|
||||
get_now(CLOCK_MONOTONIC_RAW, &sec, &msec);
|
||||
return (long double)sec + ((long double)msec) / 1000;
|
||||
}
|
||||
|
||||
INLINE long double get_now_real(void) {
|
||||
time_t sec;
|
||||
long msec;
|
||||
|
||||
get_now(CLOCK_REALTIME, &sec, &msec);
|
||||
return (long double)sec + ((long double)msec) / 1000;
|
||||
}
|
||||
|
||||
78
tools/make-html-h.py
Executable file
78
tools/make-html-h.py
Executable file
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
#============================================================================#
|
||||
# #
|
||||
# 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/>. #
|
||||
# #
|
||||
#============================================================================#
|
||||
|
||||
|
||||
import sys
|
||||
import textwrap
|
||||
|
||||
|
||||
# =====
|
||||
def main():
|
||||
assert len(sys.argv) == 4, "%s <src> <dest> <name>" % (sys.argv[0])
|
||||
|
||||
src = sys.argv[1]
|
||||
dest = sys.argv[2]
|
||||
name = sys.argv[3]
|
||||
|
||||
with open(src, "r") as html_file:
|
||||
text = html_file.read()
|
||||
|
||||
text = text.strip()
|
||||
text = text.replace("\"", "\\\"")
|
||||
text = text.replace("%VERSION%", "\" VERSION \"")
|
||||
text = textwrap.indent(text, "\t", (lambda line: True))
|
||||
text = "\n".join(("%s \\" if line.strip() else "%s\\") % (line) for line in text.split("\n"))
|
||||
text = "const char *%s = \" \\\n%s\n\";\n" % (name, text)
|
||||
text = textwrap.dedent("""
|
||||
/*****************************************************************************
|
||||
# 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 "../config.h"
|
||||
""").strip() + "\n\n\n" + text
|
||||
|
||||
with open(dest, "w") as h_file:
|
||||
h_file.write(text)
|
||||
|
||||
|
||||
# =====
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -45,7 +45,7 @@ def main():
|
||||
rows[-1].append(hex(ch))
|
||||
|
||||
text = ",\n\t".join(", ".join(row) for row in rows)
|
||||
text = "const unsigned char %s_JPG_DATA[] = {\n\t%s\n};" % (prefix, text)
|
||||
text = "const unsigned char %s_JPG_DATA[] = {\n\t%s\n};\n" % (prefix, text)
|
||||
text = "const unsigned long %s_JPG_SIZE = %d;\n\n" % (prefix, len(jpg_data)) + text
|
||||
text = "const unsigned %s_JPG_HEIGHT = %d;\n\n" % (prefix, height) + text
|
||||
text = "const unsigned %s_JPG_WIDTH = %d;\n" % (prefix, width) + text
|
||||
@@ -71,7 +71,6 @@ def main():
|
||||
*****************************************************************************/
|
||||
""").strip() + "\n\n\n" + text
|
||||
|
||||
|
||||
with open(dest, "w") as h_file:
|
||||
h_file.write(text)
|
||||
|
||||
Reference in New Issue
Block a user