Compare commits

...

77 Commits
v5.1 ... v5.15

Author SHA1 Message Date
Maxim Devaev
2f86f818cc Bump version: 5.14 → 5.15 2022-07-16 06:31:36 +03:00
Maxim Devaev
1ffcd83993 commented playout-delay 2022-07-16 06:17:02 +03:00
Maxim Devaev
9b46c2e597 Bump version: 5.13 → 5.14 2022-07-16 02:16:02 +03:00
Maxim Devaev
ad1b63890a playout delay 2022-07-15 22:07:27 +03:00
Maxim Devaev
ad79bd0957 Revert "latency test"
This reverts commit 021823bcba.
2022-07-13 15:28:52 +03:00
Maxim Devaev
021823bcba latency test 2022-07-13 15:08:25 +03:00
Maxim Devaev
7b0e171e74 refactoring 2022-07-13 14:41:54 +03:00
Maxim Devaev
b24f106ce7 moved ready flag to _plugin_init() 2022-07-13 08:42:24 +03:00
Maxim Devaev
20f056668f separate memsink and rtp threads 2022-07-13 06:41:04 +03:00
Maxim Devaev
f9439c785f refactoring 2022-07-12 10:02:26 +03:00
Maxim Devaev
5e364fb88b refactoring 2022-07-12 08:58:55 +03:00
Maxim Devaev
69dc9b8b49 separate locks for audio and video 2022-07-11 07:48:23 +03:00
Maxim Devaev
42237d9728 reverted client lock 2022-07-11 00:22:16 +03:00
Maxim Devaev
17bd25d497 increased video queue size 2022-07-11 00:18:31 +03:00
Maxim Devaev
a2ac1f8067 fix 2022-07-10 23:35:48 +03:00
Maxim Devaev
fdb1b2d562 using 0 timeout in audio.c 2022-07-10 23:32:00 +03:00
Maxim Devaev
fd2bf5ea25 separate thread for each client 2022-07-10 23:31:35 +03:00
Maxim Devaev
c874929e9d long double queue timeout 2022-07-10 03:24:38 +03:00
Maxim Devaev
db5b9d3cd7 renamed jlogging.h to logging.h 2022-07-08 22:51:50 +03:00
Maxim Devaev
12ab66be43 refactoring 2022-07-08 22:48:03 +03:00
Maxim Devaev
27d25a59d8 refactoring 2022-07-08 20:50:38 +03:00
Maxim Devaev
71991254a5 renamed config.h to const.h 2022-07-08 20:33:30 +03:00
Maxim Devaev
50e8469a59 refactoring 2022-07-08 20:29:57 +03:00
Maxim Devaev
3f45debca0 refactoring 2022-07-08 07:43:32 +03:00
Maxim Devaev
627b614ab5 refactoring 2022-07-08 04:29:13 +03:00
Maxim Devaev
f11d390b22 Bump version: 5.12 → 5.13 2022-07-05 08:08:20 +03:00
Maxim Devaev
f1e50b6f9b refactoring, using h264 5.1 profile for resolutions > 1920x1080 2022-07-05 07:53:41 +03:00
Maxim Devaev
fdf3340a7d Bump version: 5.11 → 5.12 2022-07-05 00:50:48 +03:00
Maxim Devaev
02513be220 don't assert if m2m encoder is not successfully prepared 2022-07-05 00:48:22 +03:00
Maxim Devaev
d29ce42f08 Bump version: 5.10 → 5.11 2022-06-28 23:12:59 +03:00
Maxim Devaev
aa6fc7fe04 Bump version: 5.9 → 5.10 2022-06-28 23:03:46 +03:00
Maxim Devaev
c91341a375 fixed missing frame_encoding_begin() for noop encoder 2022-06-28 18:51:30 +03:00
Maxim Devaev
3de7e26a36 Bump version: 5.8 → 5.9 2022-06-09 03:37:10 +03:00
Maxim Devaev
63cc66e8a7 improved logging 2022-06-09 03:30:35 +03:00
Maxim Devaev
92a090dec3 Bump version: 5.7 → 5.8 2022-06-07 07:53:00 +03:00
Maxim Devaev
8b0ef8a271 renambed memsink.object to video.sink 2022-06-07 07:48:48 +03:00
Maxim Devaev
a360f1901e Bump version: 5.6 → 5.7 2022-06-07 05:32:38 +03:00
Maxim Devaev
ed2d5f3af4 not based 2022-06-07 05:25:05 +03:00
Maxim Devaev
b935dd1fe8 refactoring 2022-06-07 05:00:35 +03:00
Maxim Devaev
6e1f60a36d get rid of ATOMIC_VAR_INIT 2022-06-07 04:58:09 +03:00
Maxim Devaev
210dfcfa4f lint fix 2022-06-07 04:51:46 +03:00
Maxim Devaev
ec10a9e3fe using c17 2022-06-07 04:48:28 +03:00
Maxim Devaev
217d146378 log fix 2022-06-07 03:01:27 +03:00
Maxim Devaev
3e2a43e2af speed up cppcheck 2022-06-07 02:54:39 +03:00
Maxim Devaev
2e0a19c1cb tc358743 hacks 2022-06-06 20:26:09 +03:00
Maxim Devaev
054748234e refactoring 2022-06-06 19:15:55 +03:00
Maxim Devaev
53873e9ddb refactoring 2022-06-06 17:39:03 +03:00
Maxim Devaev
c21d0aef7e moved xioctl() to libs 2022-06-06 17:06:00 +03:00
Maxim Devaev
e505a56910 refactoring 2022-06-06 16:59:03 +03:00
Maxim Devaev
f4278f32c4 refactoring 2022-06-06 16:36:55 +03:00
Maxim Devaev
e9a6db02f6 refactoring 2022-06-06 16:17:34 +03:00
Maxim Devaev
63fe32ddd9 refactoring 2022-06-06 15:15:37 +03:00
Maxim Devaev
710652073a refactoring 2022-06-06 14:35:31 +03:00
Maxim Devaev
dded49cd83 resampler 2022-06-06 14:19:11 +03:00
Maxim Devaev
0f753dc654 queue fix 2022-06-06 02:04:16 +03:00
Maxim Devaev
c505a423af refactoring 2022-06-03 07:25:41 +03:00
Maxim Devaev
1cff2545b1 Bump version: 5.5 → 5.6 2022-06-02 02:18:13 +03:00
Maxim Devaev
3d994d6e67 fixed deps 2022-06-02 02:15:50 +03:00
Maxim Devaev
7175f8d569 Bump version: 5.4 → 5.5 2022-06-02 01:47:54 +03:00
Maxim Devaev
d4a9862a18 webrtc audio 2022-06-01 07:57:34 +03:00
Maxim Devaev
3d4e9fbb1a Bump version: 5.3 → 5.4 2022-04-29 16:45:30 +03:00
Maxim Devaev
4a59b038ec refactoring 2022-04-29 16:44:13 +03:00
Maxim Devaev
6795744aea Merge pull request #153 from FallingSnow/cors
Better CORS support
2022-04-29 13:59:25 +03:00
Maxim Devaev
798fe04905 Merge pull request #152 from FallingSnow/patch-1
Add alpine linux instructions to readme
2022-04-29 13:56:27 +03:00
Ayrton Sparling
1460de95c1 Add "better cors" to /stream http endpoint
Signed-off-by: Ayrton Sparling <ayrton@sparling.us>
2022-04-28 08:40:15 -07:00
Ayrton Sparling
358950d0c2 Better CORS support
Current CORS support only adds an Access-Control-Allow-Origin header to some requests. It also does not support the OPTIONS preflight sent by modern browsers. This commit adds support for OPTIONS preflight as well as more CORS headers.

The OPTIONS preflight is sent without any credentials attached to it so it mist take place before Authorization header processesing. Firefox 99 requires other access control headers like Access-Control-Allow-Headers be returned for a successful CORS interaction.

Signed-off-by: Ayrton Sparling <ayrton@sparling.us>
2022-04-26 16:28:57 -07:00
Ayrton Sparling
caef82d96f Change WITH_PTHREAD_NP=false to WITH_PTHREAD_NP=0 2022-04-21 13:34:05 -07:00
Ayrton Sparling
dff7dd7087 Add alpine linux instructions to readme
Alpine's uses musl rather than libc and does not have support for `pthread_getname_np`. Therefore it must be build with `WITH_PTHREAD_NP=false`.
2022-04-21 10:34:25 -07:00
Maxim Devaev
79bb881a34 Merge pull request #150 from tiny-pilot/h264-guide
Add documentation for integrating with Janus to stream H.264 video
2022-04-16 19:34:19 +03:00
Michael Lynch
f0763e3865 Tweak language in H264 guide (#3)
* Adjust wording in uStreamer Janus guide

* Small tweaks

* Small changes

* Updating based on review notes
2022-04-13 09:50:15 -04:00
jotaen4tinypilot
a8dfa96db0 Document guide for H.264 (#2)
* Setup guide for H.264

* Phrasing, structure

* Remove MJPEG primer and rewrite intro

* Link to library files directly

* Adjust page title

* Remove todo

* Resolve todo (Janus setup)

* Comment all JavaScript

* Rephrase to use active voice, clarify details

* Phrasing and clarifications

* Write backend instructions

* Grammar

* Use generic URL with port number

* Use consistent terminology

* Use term “V4L2 device” to refer to the video device

* Link “building” section of uStreamer README

* Use active voice

* Change page title to “demo”

* Add comment about the `janus.js` library file

* Use window location to construct server URL

* Move `videoElement` variable closer to it’s usage

* Elaborate why we clone the media stream

* Drop obsolete `WITH_OMX` option

* Correct path of shared object file

* Fix shadowing variable of same name

* Send `start` request to avoid (harmless) `400` response

* Add `refcount.h` fix

Co-authored-by: Jan Heuermann <jan@jotaen.net>
2022-04-13 06:31:15 -04:00
Maxim Devaev
7ceb8a3da5 Bump version: 5.2 → 5.3 2022-04-01 18:35:34 +03:00
Maxim Devaev
9344059e0e systemd is required for arch now 2022-04-01 18:33:05 +03:00
Maxim Devaev
ee2b1afe5b note about kernel support 2022-04-01 18:30:21 +03:00
Maxim Devaev
31f47f2eac Bump version: 5.1 → 5.2 2022-03-21 15:01:23 +03:00
Maxim Devaev
69a9bafcd5 moved platform checks to tools.h 2022-03-21 15:00:06 +03:00
Maxim Devaev
dbecdf5d9b disabled paypal 2022-03-20 03:05:23 +03:00
77 changed files with 2183 additions and 647 deletions

View File

@@ -1,18 +1,18 @@
[bumpversion]
commit = True
tag = True
current_version = 5.1
current_version = 5.15
parse = (?P<major>\d+)\.(?P<minor>\d+)
serialize =
{major}.{minor}
[bumpversion:file:src/libs/config.h]
[bumpversion:file:src/libs/const.h]
parse = (?P<major>\d+)
serialize = {major}
search = VERSION_MAJOR {current_version}
replace = VERSION_MAJOR {new_version}
[bumpversion:file:./src/libs/config.h]
[bumpversion:file:./src/libs/const.h]
parse = <major>\d+\.(?P<minor>\d+)
serialize = {minor}
search = VERSION_MINOR {current_version}

2
.github/FUNDING.yml vendored
View File

@@ -1,4 +1,4 @@
# These are supported funding model platforms
patreon: pikvm
custom: https://www.paypal.me/mdevaev
#custom: https://www.paypal.me/mdevaev

View File

@@ -40,6 +40,7 @@ You'll need ```make```, ```gcc```, ```libevent``` with ```pthreads``` support,
* Arch: `sudo pacman -S libevent libjpeg-turbo libutil-linux libbsd`.
* Raspbian: `sudo apt install libevent-dev libjpeg8-dev libbsd-dev`. Add `libgpiod-dev` for `WITH_GPIO=1` and `libsystemd-dev` for `WITH_SYSTEMD=1`.
* Debian/Ubuntu: `sudo apt install build-essential libevent-dev libjpeg-dev libbsd-dev`.
* Alpine: `sudo apk add libevent-dev libbsd-dev libjpeg-turbo-dev musl-dev`. Build with `WITH_PTHREAD_NP=0`.
To enable GPIO support install [libgpiod](https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/about) and pass option ```WITH_GPIO=1```. If the compiler reports about a missing function ```pthread_get_name_np()``` (or similar), add option ```WITH_PTHREAD_NP=0``` (it's enabled by default). For the similar error with ```setproctitle()``` add option ```WITH_SETPROCTITLE=0```.
@@ -56,6 +57,8 @@ FreeBSD port: https://www.freshports.org/multimedia/ustreamer.
-----
# Usage
**For M2M hardware encoding on Raspberry Pi, you need at least 5.15.32 kernel. OpenMAX and MMAL support on older kernels is deprecated and removed.**
Without arguments, ```ustreamer``` will try to open ```/dev/video0``` with 640x480 resolution and start streaming on ```http://127.0.0.1:8080```. You can override this behavior using parameters ```--device```, ```--host``` and ```--port```. For example, to stream to the world, run:
```
# ./ustreamer --device=/dev/video1 --host=0.0.0.0 --port=80
@@ -97,6 +100,9 @@ $ modprobe bcm2835-v4l2 max_video_width=2592 max_video_height=1944
-----
# Integrations
## Janus
µStreamer supports bandwidth-efficient streaming using [H.264 compression](https://en.wikipedia.org/wiki/Advanced_Video_Coding) and the Janus WebRTC server. See the [Janus integration guide](docs/h264.md) for full details.
## Nginx
When uStreamer is behind an Nginx proxy, it's buffering behavior introduces latency into the video stream. It's possible to disable Nginx's buffering to eliminate the additional latency:

View File

@@ -56,6 +56,8 @@ $ ./ustreamer --help
-----
# Использование
**Для аппаратного кодирования M2M на Raspberry Pi, вам нужно ядро минимальной версии 5.15.32. Поддержка OpenMAX и MMAL для более старых ядер объявлена устаревшей и была удалена.**
Будучи запущенным без аргументов, ```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

231
docs/h264.md Normal file
View File

@@ -0,0 +1,231 @@
# Streaming H.264 Video over WebRTC with Janus
µStreamer supports bandwidth-efficient streaming using [H.264 compression](https://en.wikipedia.org/wiki/Advanced_Video_Coding) and the Janus WebRTC server.
This guide explains how to configure µStreamer to provide an H.264 video stream and consume it within a web application using WebRTC.
## Components
In addition to µStreamer, H.264 streaming involves the following components:
- [**Janus**](https://janus.conf.meetecho.com/): A general-purpose WebRTC server.
- [**µStreamer Janus Plugin**](https://github.com/pikvm/ustreamer/tree/master/janus): A Janus plugin that allows Janus to consume a video stream from µStreamer via shared memory.
- [**Janus JavaScript Client**](https://janus.conf.meetecho.com/docs/JS.html): A frontend library that communicates with the Janus server and the µStreamer Janus plugin.
## Control Flow
To connect to a µStreamer video stream over WebRTC, an application must establish the following control flow:
1. The backend server starts the µStreamer service.
1. The backend server starts the Janus WebRTC server with the µStreamer Janus plugin.
1. The client-side JavaScript application establishes a connection to the Janus server.
1. The client-side JavaScript application instructs the Janus server to attach the µStreamer Janus plugin and start the video stream.
1. The client-side JavaScript application renders the video stream in the web browser.
## Server Setup
### Prerequisites
To integrate with Janus, µStreamer has the following prerequisites:
- The system packages that µStreamer depends on (see the [main build instructions](../README.md#building)).
- The Janus WebRTC server with WebSockets transport enabled (see the [Janus documentation](https://github.com/meetecho/janus-gateway)).
### Fixing Janus C Headers
To compile µStreamer with the Janus option, (see [“Installation”](#installation)), you need to make the Janus header files available within µStreamer's build context.
First, create a symbolic link from `/usr/include` to the Janus install directory. By default, Janus installs to `/opt/janus`.
```sh
ln -s /opt/janus/include/janus /usr/include/janus
```
Janus uses `#include` paths that make it difficult for third-party libraries to build against its headers. To fix this, modify the `#include` directive in `janus/plugins/plugin.h` to prepend a `../` to the included file name (`#include "refcount.h"``#include "../refcount.h"`).
```sh
sed \
--in-place \
--expression 's|^#include "refcount.h"$|#include "../refcount.h"|g' \
/usr/include/janus/plugins/plugin.h
```
### Installation
First, compile µStreamer with the Janus plugin option (`WITH_JANUS`).
```sh
git clone --depth=1 https://github.com/pikvm/ustreamer
cd ustreamer
make WITH_JANUS=1
```
The `WITH_JANUS` option compiles the µStreamer Janus plugin and outputs it to `janus/libjanus_ustreamer.so`. Move this file to the plugin directory of your Janus installation.
```sh
mv \
janus/libjanus_ustreamer.so \
/opt/janus/lib/janus/plugins/libjanus_ustreamer.so
```
Finally, specify a qualifier for the shared memory object so that the µStreamer Janus plugin can read µStreamer's video data.
```sh
cat > /opt/janus/lib/janus/configs/janus.plugin.ustreamer.jcfg <<EOF
memsink: {
object = "demo::ustreamer::h264"
}
EOF
```
### Start µStreamer and the Janus WebRTC Server
For µStreamer to share the video stream with the µStreamer Janus plugin, µStreamer must run with the following command-line flags:
- `--h264-sink` with the qualifier of the shared memory object you specified above (`demo::ustreamer::h264`)
- `--h264-sink-mode` with the permissions bitmask for the shared memory object (e.g., `660`)
- `--h264-sink-rm` to clean up the shared memory object when the µStreamer process exits
To load the µStreamer Janus plugin and configuration, the Janus WebRTC server must run with the following command-line flags:
- `--configs-folder` with the path to the Janus configuration directory (e.g., `/opt/janus/lib/janus/configs/`)
- `--plugins-folder` with the path to the Janus plugin directory (e.g., `/opt/janus/lib/janus/plugins/`)
## Client Setup
Once an application's backend server is running µStreamer and Janus, a browser-based JavaScript application can consume the server's video stream.
### Prerequisites
The client needs to load the following JavaScript libraries:
- [WebRTC Adapter library](https://webrtc.github.io/adapter/adapter-8.1.0.js) (`webrtc-adapter.js`, version `8.1.0`)
- [Janus Gateway JavaScript library](https://raw.githubusercontent.com/meetecho/janus-gateway/v1.0.0/html/janus.js) (`janus.js`, version `1.0.0`)
### Control Flow
The client-side JavaScript application uses the following control flow:
1. The client loads and initializes the Janus client library.
1. The client establishes a WebSockets connection to the Janus server.
1. The client instructs the Janus server to attach the µStreamer Janus plugin.
1. On success, the client obtains a plugin handle through which it can send requests directly to the µStreamer Janus plugin. The client processes responses via the `attach` callbacks:
- `onmessage` for general messages
- `onremotetrack` for the H.264 video stream
1. The client issues a `watch` request to the µStreamer Janus plugin, which initiates the H.264 stream in the plugin itself.
- It takes a few seconds for uStreamer's video stream to become available to Janus. The first `watch` request may fail, so the client must retry the `watch` request.
1. The client and server negotiate the underlying parameters of the WebRTC session. This procedure is called JavaScript Session Establishment Protocol (JSEP). The server makes a `jsepOffer` to the client, and the client responds with a `jsepAnswer`.
1. The client issues a `start` request to the µStreamer Janus plugin to indicate that the client wants to begin consuming the video stream.
1. The µStreamer Janus plugin delivers the H.264 video stream to the client via WebRTC.
1. The Janus client library invokes the `onremotetrack` callback. The client attaches the video stream to the `<video>` element, rendering the video stream in the browser window.
### Sample Code
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>µStreamer H.264 demo</title>
<script src="https://webrtc.github.io/adapter/adapter-8.1.0.js"></script>
<!-- janus.js is the JavaScript client library of Janus, as specified above in
the prerequisites section of the client setup. You might need to change
the `src` path, depending on where you serve this file from. -->
<script src="janus.js"></script>
<style>
video {
height: 100%;
width: 100%;
object-fit: contain;
}
</style>
</head>
<body>
<video id="webrtc-output" autoplay playsinline muted></video>
<script type="text/javascript">
// Initialize Janus library.
Janus.init({
// Turn on debug logs in the browser console.
debug: true,
// Configure Janus to use standard browser APIs internally.
dependencies: Janus.useDefaultDependencies(),
});
// Establish a WebSockets connection to the server.
const janus = new Janus({
// Specify the URL of the Janus servers WebSockets endpoint.
server: `ws://${window.location.hostname}:8188/`,
// Callback function if the client connects successfully.
success: attachUStreamerPlugin,
// Callback function if the client fails to connect.
error: console.error,
});
let uStreamerPluginHandle = null;
function attachUStreamerPlugin() {
// Instruct the server to attach the µStreamer Janus plugin.
janus.attach({
// Qualifier of the plugin.
plugin: "janus.plugin.ustreamer",
// Callback function, for when the server attached the plugin
// successfully.
success: function (pluginHandle) {
uStreamerPluginHandle = pluginHandle;
// Instruct the µStreamer Janus plugin to initiate the video stream.
uStreamerPluginHandle.send({ message: { request: "watch" } });
},
// Callback function if the server fails to attach the plugin.
error: console.error,
// Callback function for processing messages from the Janus server.
onmessage: function (msg, jsepOffer) {
// 503 indicates that the plugin is not ready to stream yet. Retry the
// watch request until the video stream is available.
if (msg.error_code === 503) {
uStreamerPluginHandle.send({ message: { request: "watch" } });
return;
}
// If there is a JSEP offer, respond to it. This starts the WebRTC
// connection.
if (jsepOffer) {
uStreamerPluginHandle.createAnswer({
jsep: jsepOffer,
// Prevent the client from sending audio and video, as this would
// trigger a permission dialog in the browser.
media: { audioSend: false, videoSend: false },
success: function (jsepAnswer) {
uStreamerPluginHandle.send({
message: { request: "start" },
jsep: jsepAnswer,
});
},
error: console.error,
});
}
},
// Callback function, for when the video stream arrives.
onremotetrack: function (mediaStreamTrack, mediaId, isAdded) {
if (isAdded) {
// Attach the received media track to the video element. Cloning the
// mediaStreamTrack creates a new object with a distinct, globally
// unique stream identifier.
const videoElement = document.getElementById("webrtc-output");
const stream = new MediaStream();
stream.addTrack(mediaStreamTrack.clone());
videoElement.srcObject = stream;
}
},
});
}
</script>
</body>
</html>
```

View File

@@ -9,10 +9,10 @@ LDFLAGS ?=
# =====
_PLUGIN = libjanus_ustreamer.so
_CFLAGS = -fPIC -MD -c -std=c11 -Wall -Wextra -D_GNU_SOURCE $(shell pkg-config --cflags glib-2.0) $(CFLAGS)
_LDFLAGS = -shared -lm -pthread -lrt -ljansson $(shell pkg-config --libs glib-2.0) $(LDFLAGS)
_CFLAGS = -fPIC -MD -c -std=c17 -Wall -Wextra -D_GNU_SOURCE $(shell pkg-config --cflags glib-2.0) $(CFLAGS)
_LDFLAGS = -shared -lm -pthread -lrt -ljansson -lopus -lasound -lspeexdsp $(shell pkg-config --libs glib-2.0) $(LDFLAGS)
_SRCS = $(shell ls src/*.c)
_SRCS = $(shell ls src/uslibs/*.c src/*.c)
_BUILD = build

250
janus/src/audio.c Normal file
View File

@@ -0,0 +1,250 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include "audio.h"
#define JLOG_PERROR_ALSA(_err, _prefix, _msg, ...) JLOG_ERROR(_prefix, _msg ": %s", ##__VA_ARGS__, snd_strerror(_err))
#define JLOG_PERROR_RES(_err, _prefix, _msg, ...) JLOG_ERROR(_prefix, _msg ": %s", ##__VA_ARGS__, speex_resampler_strerror(_err))
#define JLOG_PERROR_OPUS(_err, _prefix, _msg, ...) JLOG_ERROR(_prefix, _msg ": %s", ##__VA_ARGS__, opus_strerror(_err))
// A number of frames per 1 channel:
// - https://github.com/xiph/opus/blob/7b05f44/src/opus_demo.c#L368
#define HZ_TO_FRAMES(_hz) (6 * (_hz) / 50) // 120ms
#define HZ_TO_BUF16(_hz) (HZ_TO_FRAMES(_hz) * 2) // One stereo frame = (16bit L) + (16bit R)
#define HZ_TO_BUF8(_hz) (HZ_TO_BUF16(_hz) * sizeof(int16_t))
#define MIN_PCM_HZ 8000
#define MAX_PCM_HZ 192000
#define MAX_BUF16 HZ_TO_BUF16(MAX_PCM_HZ)
#define MAX_BUF8 HZ_TO_BUF8(MAX_PCM_HZ)
#define ENCODER_INPUT_HZ 48000
typedef struct {
int16_t data[MAX_BUF16];
} _pcm_buffer_s;
typedef struct {
uint8_t data[MAX_BUF8]; // Worst case
size_t used;
uint64_t pts;
} _enc_buffer_s;
static void *_pcm_thread(void *v_audio);
static void *_encoder_thread(void *v_audio);
audio_s *audio_init(const char *name, unsigned pcm_hz) {
audio_s *audio;
A_CALLOC(audio, 1);
audio->pcm_hz = pcm_hz;
audio->pcm_queue = queue_init(8);
audio->enc_queue = queue_init(8);
atomic_init(&audio->stop, false);
int err;
{
if ((err = snd_pcm_open(&audio->pcm, name, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
audio->pcm = NULL;
JLOG_PERROR_ALSA(err, "audio", "Can't open PCM capture");
goto error;
}
assert(!snd_pcm_hw_params_malloc(&audio->pcm_params));
# define SET_PARAM(_msg, _func, ...) { \
if ((err = _func(audio->pcm, audio->pcm_params, ##__VA_ARGS__)) < 0) { \
JLOG_PERROR_ALSA(err, "audio", _msg); \
goto error; \
} \
}
SET_PARAM("Can't initialize PCM params", snd_pcm_hw_params_any);
SET_PARAM("Can't set PCM access type", snd_pcm_hw_params_set_access, SND_PCM_ACCESS_RW_INTERLEAVED);
SET_PARAM("Can't set PCM channels numbre", snd_pcm_hw_params_set_channels, 2);
SET_PARAM("Can't set PCM sampling format", snd_pcm_hw_params_set_format, SND_PCM_FORMAT_S16_LE);
SET_PARAM("Can't set PCM sampling rate", snd_pcm_hw_params_set_rate_near, &audio->pcm_hz, 0);
if (audio->pcm_hz < MIN_PCM_HZ || audio->pcm_hz > MAX_PCM_HZ) {
JLOG_ERROR("audio", "Unsupported PCM freq: %u; should be: %u <= F <= %u",
audio->pcm_hz, MIN_PCM_HZ, MAX_PCM_HZ);
goto error;
}
audio->pcm_frames = HZ_TO_FRAMES(audio->pcm_hz);
audio->pcm_size = HZ_TO_BUF8(audio->pcm_hz);
SET_PARAM("Can't apply PCM params", snd_pcm_hw_params);
# undef SET_PARAM
}
if (audio->pcm_hz != ENCODER_INPUT_HZ) {
audio->res = speex_resampler_init(2, audio->pcm_hz, ENCODER_INPUT_HZ, SPEEX_RESAMPLER_QUALITY_DESKTOP, &err);
if (err < 0) {
audio->res = NULL;
JLOG_PERROR_RES(err, "audio", "Can't create resampler");
goto error;
}
}
{
// OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY
audio->enc = opus_encoder_create(ENCODER_INPUT_HZ, 2, OPUS_APPLICATION_AUDIO, &err);
assert(err == 0);
assert(!opus_encoder_ctl(audio->enc, OPUS_SET_BITRATE(48000)));
assert(!opus_encoder_ctl(audio->enc, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND)));
assert(!opus_encoder_ctl(audio->enc, OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC)));
// OPUS_SET_INBAND_FEC(1), OPUS_SET_PACKET_LOSS_PERC(10): see rtpa.c
}
JLOG_INFO("audio", "Pipeline configured on %uHz; capturing ...", audio->pcm_hz);
audio->tids_created = true;
A_THREAD_CREATE(&audio->enc_tid, _encoder_thread, audio);
A_THREAD_CREATE(&audio->pcm_tid, _pcm_thread, audio);
return audio;
error:
audio_destroy(audio);
return NULL;
}
void audio_destroy(audio_s *audio) {
if (audio->tids_created) {
atomic_store(&audio->stop, true);
A_THREAD_JOIN(audio->pcm_tid);
A_THREAD_JOIN(audio->enc_tid);
}
if (audio->enc) {
opus_encoder_destroy(audio->enc);
}
if (audio->res) {
speex_resampler_destroy(audio->res);
}
if (audio->pcm) {
snd_pcm_close(audio->pcm);
}
if (audio->pcm_params) {
snd_pcm_hw_params_free(audio->pcm_params);
}
QUEUE_FREE_ITEMS_AND_DESTROY(audio->enc_queue, free);
QUEUE_FREE_ITEMS_AND_DESTROY(audio->pcm_queue, free);
if (audio->tids_created) {
JLOG_INFO("audio", "Pipeline closed");
}
free(audio);
}
int audio_get_encoded(audio_s *audio, uint8_t *data, size_t *size, uint64_t *pts) {
if (atomic_load(&audio->stop)) {
return -1;
}
_enc_buffer_s *buf;
if (!queue_get(audio->enc_queue, (void **)&buf, 0.1)) {
if (*size < buf->used) {
free(buf);
return -3;
}
memcpy(data, buf->data, buf->used);
*size = buf->used;
*pts = buf->pts;
free(buf);
return 0;
}
return -2;
}
static void *_pcm_thread(void *v_audio) {
A_THREAD_RENAME("us_a_pcm");
audio_s *audio = (audio_s *)v_audio;
uint8_t in[MAX_BUF8];
while (!atomic_load(&audio->stop)) {
int frames = snd_pcm_readi(audio->pcm, in, audio->pcm_frames);
if (frames < 0) {
JLOG_PERROR_ALSA(frames, "audio", "Fatal: Can't capture PCM frames");
break;
} else if (frames < (int)audio->pcm_frames) {
JLOG_ERROR("audio", "Fatal: Too few PCM frames captured");
break;
}
if (queue_get_free(audio->pcm_queue)) {
_pcm_buffer_s *out;
A_CALLOC(out, 1);
memcpy(out->data, in, audio->pcm_size);
assert(!queue_put(audio->pcm_queue, out, 0));
} else {
JLOG_ERROR("audio", "PCM queue is full");
}
}
atomic_store(&audio->stop, true);
return NULL;
}
static void *_encoder_thread(void *v_audio) {
A_THREAD_RENAME("us_a_enc");
audio_s *audio = (audio_s *)v_audio;
int16_t in_res[MAX_BUF16];
while (!atomic_load(&audio->stop)) {
_pcm_buffer_s *in;
if (!queue_get(audio->pcm_queue, (void **)&in, 0.1)) {
int16_t *in_ptr;
if (audio->res) {
assert(audio->pcm_hz != ENCODER_INPUT_HZ);
uint32_t in_count = audio->pcm_frames;
uint32_t out_count = HZ_TO_FRAMES(ENCODER_INPUT_HZ);
speex_resampler_process_interleaved_int(audio->res, in->data, &in_count, in_res, &out_count);
in_ptr = in_res;
} else {
assert(audio->pcm_hz == ENCODER_INPUT_HZ);
in_ptr = in->data;
}
_enc_buffer_s *out;
A_CALLOC(out, 1);
int size = opus_encode(audio->enc, in_ptr, HZ_TO_FRAMES(ENCODER_INPUT_HZ), out->data, ARRAY_LEN(out->data));
free(in);
if (size < 0) {
JLOG_PERROR_OPUS(size, "audio", "Fatal: Can't encode PCM frame to OPUS");
free(out);
break;
}
out->used = size;
out->pts = audio->pts;
// https://datatracker.ietf.org/doc/html/rfc7587#section-4.2
audio->pts += HZ_TO_FRAMES(ENCODER_INPUT_HZ);
if (queue_put(audio->enc_queue, out, 0) != 0) {
JLOG_ERROR("audio", "OPUS encoder queue is full");
free(out);
}
}
}
atomic_store(&audio->stop, true);
return NULL;
}

68
janus/src/audio.h Normal file
View File

@@ -0,0 +1,68 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 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 <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <assert.h>
#include <sys/types.h>
#include <pthread.h>
#include <alsa/asoundlib.h>
#include <speex/speex_resampler.h>
#include <opus/opus.h>
#include "uslibs/tools.h"
#include "uslibs/threading.h"
#include "logging.h"
#include "queue.h"
typedef struct {
snd_pcm_t *pcm;
unsigned pcm_hz;
unsigned pcm_frames;
size_t pcm_size;
snd_pcm_hw_params_t *pcm_params;
SpeexResamplerState *res;
OpusEncoder *enc;
queue_s *pcm_queue;
queue_s *enc_queue;
uint32_t pts;
pthread_t pcm_tid;
pthread_t enc_tid;
bool tids_created;
atomic_bool stop;
} audio_s;
audio_s *audio_init(const char *name, unsigned pcm_hz);
void audio_destroy(audio_s *audio);
int audio_get_encoded(audio_s *audio, uint8_t *data, size_t *size, uint64_t *pts);

View File

@@ -1 +0,0 @@
../../src/libs/base64.c

View File

@@ -1 +0,0 @@
../../src/libs/base64.h

120
janus/src/client.c Normal file
View File

@@ -0,0 +1,120 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include "client.h"
static void *_video_thread(void *v_client);
static void *_audio_thread(void *v_client);
static void *_common_thread(void *v_client, bool video);
client_s *client_init(janus_callbacks *gw, janus_plugin_session *session, bool has_audio) {
client_s *client;
A_CALLOC(client, 1);
client->gw = gw;
client->session = session;
atomic_init(&client->transmit, true);
atomic_init(&client->stop, false);
client->video_queue = queue_init(1024);
A_THREAD_CREATE(&client->video_tid, _video_thread, client);
if (has_audio) {
client->audio_queue = queue_init(64);
A_THREAD_CREATE(&client->audio_tid, _audio_thread, client);
}
return client;
}
void client_destroy(client_s *client) {
atomic_store(&client->stop, true);
queue_put(client->video_queue, NULL, 0);
if (client->audio_queue != NULL) {
queue_put(client->audio_queue, NULL, 0);
}
A_THREAD_JOIN(client->video_tid);
QUEUE_FREE_ITEMS_AND_DESTROY(client->video_queue, rtp_destroy);
if (client->audio_queue != NULL) {
A_THREAD_JOIN(client->audio_tid);
QUEUE_FREE_ITEMS_AND_DESTROY(client->audio_queue, rtp_destroy);
}
free(client);
}
void client_send(client_s *client, const rtp_s *rtp) {
if (
!atomic_load(&client->transmit)
|| (!rtp->video && client->audio_queue == NULL)
) {
return;
}
rtp_s *new = rtp_dup(rtp);
if (queue_put((new->video ? client->video_queue : client->audio_queue), new, 0) != 0) {
JLOG_ERROR("client", "Session %p %s queue is full",
client->session, (new->video ? "video" : "audio"));
rtp_destroy(new);
}
}
static void *_video_thread(void *v_client) {
return _common_thread(v_client, true);
}
static void *_audio_thread(void *v_client) {
return _common_thread(v_client, false);
}
static void *_common_thread(void *v_client, bool video) {
client_s *client = (client_s *)v_client;
queue_s *queue = (video ? client->video_queue : client->audio_queue);
assert(queue != NULL); // Audio may be NULL
while (!atomic_load(&client->stop)) {
rtp_s *rtp;
if (!queue_get(queue, (void **)&rtp, 0.1)) {
if (rtp == NULL) {
break;
}
if (atomic_load(&client->transmit)) {
janus_plugin_rtp packet = {0};
packet.video = rtp->video;
packet.buffer = (char *)rtp->datagram;
packet.length = rtp->used;
janus_plugin_rtp_extensions_reset(&packet.extensions);
// FIXME: See rtpv.c
// Это очень эффективный способ уменьшить задержку, но WebRTC стек в хроме и фоксе
// слишком корявый, чтобы обработать это, тз-за чего на кейфреймах начинаются заикания.
// - https://github.com/Glimesh/janus-ftl-plugin/issues/101
/*if (video) {
packet.extensions.min_delay = 0;
packet.extensions.max_delay = 0;
}*/
client->gw->relay_rtp(client->session, &packet);
}
rtp_destroy(rtp);
}
}
return NULL;
}

61
janus/src/client.h Normal file
View File

@@ -0,0 +1,61 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 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 <stdlib.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <string.h>
#include <pthread.h>
#include <janus/plugins/plugin.h>
#include "uslibs/tools.h"
#include "uslibs/threading.h"
#include "uslibs/list.h"
#include "logging.h"
#include "queue.h"
#include "rtp.h"
typedef struct client_sx {
janus_callbacks *gw;
janus_plugin_session *session;
atomic_bool transmit;
pthread_t video_tid;
pthread_t audio_tid;
atomic_bool stop;
queue_s *video_queue;
queue_s *audio_queue;
LIST_STRUCT(struct client_sx);
} client_s;
client_s *client_init(janus_callbacks *gw, janus_plugin_session *session, bool has_audio);
void client_destroy(client_s *client);
void client_send(client_s *client, const rtp_s *rtp);

78
janus/src/config.c Normal file
View File

@@ -0,0 +1,78 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include "config.h"
static char *_get_value(janus_config *config, const char *section, const char *option);
int read_config(const char *config_dir_path, char **video_sink_name, char **audio_dev_name, char **tc358743_dev_path) {
int retval = 0;
char *config_file_path;
janus_config *config = NULL;
A_ASPRINTF(config_file_path, "%s/%s.jcfg", config_dir_path, PLUGIN_PACKAGE);
JLOG_INFO("config", "Reading config file '%s' ...", config_file_path);
config = janus_config_parse(config_file_path);
if (config == NULL) {
JLOG_ERROR("config", "Can't read config");
goto error;
}
janus_config_print(config);
if (
(*video_sink_name = _get_value(config, "memsink", "object")) == NULL
&& (*video_sink_name = _get_value(config, "video", "sink")) == NULL
) {
JLOG_ERROR("config", "Missing config value: video.sink (ex. memsink.object)");
goto error;
}
if ((*audio_dev_name = _get_value(config, "audio", "device")) != NULL) {
JLOG_INFO("config", "Enabled the experimental AUDIO feature");
if ((*tc358743_dev_path = _get_value(config, "audio", "tc358743")) == NULL) {
JLOG_INFO("config", "Missing config value: audio.tc358743");
goto error;
}
}
goto ok;
error:
retval = -1;
ok:
if (config) {
janus_config_destroy(config);
}
free(config_file_path);
return retval;
}
static char *_get_value(janus_config *config, const char *section, const char *option) {
janus_config_category *section_obj = janus_config_get_create(config, NULL, janus_config_type_category, section);
janus_config_item *option_obj = janus_config_get(config, section_obj, janus_config_type_item, option);
if (option_obj == NULL || option_obj->value == NULL || option_obj->value[0] == '\0') {
return NULL;
}
return strdup(option_obj->value);
}

View File

@@ -1 +0,0 @@
../../src/libs/config.h

37
janus/src/config.h Normal file
View File

@@ -0,0 +1,37 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 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 <stdlib.h>
#include <string.h>
#include <janus/config.h>
#include <janus/plugins/plugin.h>
#include "uslibs/tools.h"
#include "const.h"
#include "logging.h"
int read_config(const char *config_dir_path, char **video_sink_name, char **audio_dev_name, char **tc358743_dev_path);

26
janus/src/const.h Normal file
View File

@@ -0,0 +1,26 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 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
#define PLUGIN_NAME "ustreamer"
#define PLUGIN_PACKAGE "janus.plugin.ustreamer"

View File

@@ -1 +0,0 @@
../../src/libs/frame.c

View File

@@ -1 +0,0 @@
../../src/libs/frame.h

View File

@@ -1 +0,0 @@
../../src/libs/list.h

38
janus/src/logging.h Normal file
View File

@@ -0,0 +1,38 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include <janus/plugins/plugin.h>
#include "uslibs/tools.h"
#include "const.h"
#define JLOG_INFO(_prefix, _msg, ...) JANUS_LOG(LOG_INFO, "== %s/%-9s -- " _msg "\n", PLUGIN_NAME, _prefix, ##__VA_ARGS__)
#define JLOG_WARN(_prefix, _msg, ...) JANUS_LOG(LOG_WARN, "== %s/%-9s -- " _msg "\n", PLUGIN_NAME, _prefix, ##__VA_ARGS__)
#define JLOG_ERROR(_prefix, _msg, ...) JANUS_LOG(LOG_ERR, "== %s/%-9s -- " _msg "\n", PLUGIN_NAME, _prefix, ##__VA_ARGS__)
#define JLOG_PERROR(_prefix, _msg, ...) { \
char _perror_buf[1024] = {0}; \
char *_perror_ptr = errno_to_string(errno, _perror_buf, 1023); \
JANUS_LOG(LOG_ERR, "[%s/%-9s] " _msg ": %s\n", PLUGIN_NAME, _prefix, ##__VA_ARGS__, _perror_ptr); \
}

70
janus/src/memsinkfd.c Normal file
View File

@@ -0,0 +1,70 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include "memsinkfd.h"
int memsink_fd_wait_frame(int fd, memsink_shared_s* mem, uint64_t last_id) {
long double deadline_ts = get_now_monotonic() + 1; // wait_timeout
long double now;
do {
int result = flock_timedwait_monotonic(fd, 1); // lock_timeout
now = get_now_monotonic();
if (result < 0 && errno != EWOULDBLOCK) {
JLOG_PERROR("video", "Can't lock memsink");
return -1;
} else if (result == 0) {
if (mem->magic == MEMSINK_MAGIC && mem->version == MEMSINK_VERSION && mem->id != last_id) {
return 0;
}
if (flock(fd, LOCK_UN) < 0) {
JLOG_PERROR("video", "Can't unlock memsink");
return -1;
}
}
usleep(1000); // lock_polling
} while (now < deadline_ts);
return -2;
}
frame_s *memsink_fd_get_frame(int fd, memsink_shared_s *mem, uint64_t *frame_id) {
frame_s *frame = frame_init();
frame_set_data(frame, mem->data, mem->used);
FRAME_COPY_META(mem, frame);
*frame_id = mem->id;
mem->last_client_ts = get_now_monotonic();
bool ok = true;
if (frame->format != V4L2_PIX_FMT_H264) {
JLOG_ERROR("video", "Got non-H264 frame from memsink");
ok = false;
}
if (flock(fd, LOCK_UN) < 0) {
JLOG_PERROR("video", "Can't unlock memsink");
ok = false;
}
if (!ok) {
frame_destroy(frame);
frame = NULL;
}
return frame;
}

38
janus/src/memsinkfd.h Normal file
View File

@@ -0,0 +1,38 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 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 <stdint.h>
#include <unistd.h>
#include <linux/videodev2.h>
#include "uslibs/tools.h"
#include "uslibs/frame.h"
#include "uslibs/memsinksh.h"
#include "logging.h"
int memsink_fd_wait_frame(int fd, memsink_shared_s* mem, uint64_t last_id);
frame_s *memsink_fd_get_frame(int fd, memsink_shared_s *mem, uint64_t *frame_id);

View File

@@ -1 +0,0 @@
../../src/libs/memsinksh.h

View File

@@ -24,194 +24,111 @@
#include <stdbool.h>
#include <stdatomic.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <linux/videodev2.h>
#include <pthread.h>
#include <jansson.h>
#include <janus/config.h>
#include <janus/plugins/plugin.h>
#include "config.h"
#include "tools.h"
#include "threading.h"
#include "list.h"
#include "memsinksh.h"
#include "uslibs/const.h"
#include "uslibs/tools.h"
#include "uslibs/threading.h"
#include "uslibs/list.h"
#include "uslibs/memsinksh.h"
#include "const.h"
#include "logging.h"
#include "queue.h"
#include "client.h"
#include "audio.h"
#include "tc358743.h"
#include "rtp.h"
#include "rtpv.h"
#include "rtpa.h"
#include "memsinkfd.h"
#include "config.h"
static int _plugin_init(janus_callbacks *gw, const char *config_file_path);
static void _plugin_destroy(void);
static char *_g_video_sink_name = NULL;
static char *_g_audio_dev_name = NULL;
static char *_g_tc358743_dev_path = NULL;
static void _plugin_create_session(janus_plugin_session *session, int *error);
static void _plugin_destroy_session(janus_plugin_session *session, int *error);
static json_t *_plugin_query_session(janus_plugin_session *session);
static void _plugin_setup_media(janus_plugin_session *session);
static void _plugin_hangup_media(janus_plugin_session *session);
static struct janus_plugin_result *_plugin_handle_message(
janus_plugin_session *session, char *transaction, json_t *msg, json_t *jsep);
static int _plugin_get_api_compatibility(void);
static int _plugin_get_version(void);
static const char *_plugin_get_version_string(void);
static const char *_plugin_get_description(void);
static const char *_plugin_get_name(void);
static const char *_plugin_get_author(void);
static const char *_plugin_get_package(void);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverride-init"
static janus_plugin _plugin = JANUS_PLUGIN_INIT(
.init = _plugin_init,
.destroy = _plugin_destroy,
.create_session = _plugin_create_session,
.destroy_session = _plugin_destroy_session,
.query_session = _plugin_query_session,
.setup_media = _plugin_setup_media,
.hangup_media = _plugin_hangup_media,
.handle_message = _plugin_handle_message,
.get_api_compatibility = _plugin_get_api_compatibility,
.get_version = _plugin_get_version,
.get_version_string = _plugin_get_version_string,
.get_description = _plugin_get_description,
.get_name = _plugin_get_name,
.get_author = _plugin_get_author,
.get_package = _plugin_get_package,
);
#pragma GCC diagnostic pop
janus_plugin *create(void) { // cppcheck-suppress unusedFunction
return &_plugin;
}
typedef struct _client_sx {
janus_plugin_session *session;
bool transmit;
LIST_STRUCT(struct _client_sx);
} _client_s;
static char *_g_memsink_obj = NULL;
const long double _g_wait_timeout = 1;
const long double _g_lock_timeout = 1;
const useconds_t _g_lock_polling = 1000;
const useconds_t _g_watchers_polling = 100000;
static _client_s *_g_clients = NULL;
static client_s *_g_clients = NULL;
static janus_callbacks *_g_gw = NULL;
static rtp_s *_g_rtp = NULL;
static queue_s *_g_video_queue = NULL;
static rtpv_s *_g_rtpv = NULL;
static rtpa_s *_g_rtpa = NULL;
static pthread_t _g_tid;
static pthread_mutex_t _g_lock = PTHREAD_MUTEX_INITIALIZER;
static atomic_bool _g_ready = ATOMIC_VAR_INIT(false);
static atomic_bool _g_stop = ATOMIC_VAR_INIT(false);
static atomic_bool _g_has_watchers = ATOMIC_VAR_INIT(false);
static pthread_t _g_video_rtp_tid;
static atomic_bool _g_video_rtp_tid_created = false;
static pthread_t _g_video_sink_tid;
static atomic_bool _g_video_sink_tid_created = false;
static pthread_t _g_audio_tid;
static atomic_bool _g_audio_tid_created = false;
static pthread_mutex_t _g_video_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t _g_audio_lock = PTHREAD_MUTEX_INITIALIZER;
static atomic_bool _g_ready = false;
static atomic_bool _g_stop = false;
static atomic_bool _g_has_watchers = false;
#define JLOG_INFO(_msg, ...) JANUS_LOG(LOG_INFO, "== %s -- " _msg "\n", _plugin_get_name(), ##__VA_ARGS__)
#define JLOG_WARN(_msg, ...) JANUS_LOG(LOG_WARN, "== %s -- " _msg "\n", _plugin_get_name(), ##__VA_ARGS__)
#define JLOG_ERROR(_msg, ...) JANUS_LOG(LOG_ERR, "== %s -- " _msg "\n", _plugin_get_name(), ##__VA_ARGS__)
#define LOCK_VIDEO A_MUTEX_LOCK(&_g_video_lock)
#define UNLOCK_VIDEO A_MUTEX_UNLOCK(&_g_video_lock)
#define JLOG_PERROR(_msg, ...) { \
char _perror_buf[1024] = {0}; \
char *_perror_ptr = errno_to_string(errno, _perror_buf, 1023); \
JANUS_LOG(LOG_ERR, "[%s] " _msg ": %s\n", _plugin_get_name(), ##__VA_ARGS__, _perror_ptr); \
}
#define LOCK_AUDIO A_MUTEX_LOCK(&_g_audio_lock)
#define UNLOCK_AUDIO A_MUTEX_UNLOCK(&_g_audio_lock)
#define LOCK_ALL { LOCK_VIDEO; LOCK_AUDIO; }
#define UNLOCK_ALL { UNLOCK_AUDIO; UNLOCK_VIDEO; }
#define LOCK A_MUTEX_LOCK(&_g_lock)
#define UNLOCK A_MUTEX_UNLOCK(&_g_lock)
#define READY atomic_load(&_g_ready)
#define STOP atomic_load(&_g_stop)
#define HAS_WATCHERS atomic_load(&_g_has_watchers)
static void _relay_rtp_clients(const uint8_t *datagram, size_t size) {
janus_plugin_rtp packet = {
.video = true,
.buffer = (char *)datagram,
.length = size,
};
janus_plugin_rtp_extensions_reset(&packet.extensions);
LIST_ITERATE(_g_clients, client, {
if (client->transmit) {
_g_gw->relay_rtp(client->session, &packet);
}
});
}
janus_plugin *create(void);
static int _wait_frame(int fd, memsink_shared_s* mem, uint64_t last_id) {
long double deadline_ts = get_now_monotonic() + _g_wait_timeout;
long double now;
do {
int retval = flock_timedwait_monotonic(fd, _g_lock_timeout);
now = get_now_monotonic();
if (retval < 0 && errno != EWOULDBLOCK) {
JLOG_PERROR("Can't lock memsink");
return -1;
} else if (retval == 0) {
if (mem->magic == MEMSINK_MAGIC && mem->version == MEMSINK_VERSION && mem->id != last_id) {
return 0;
}
if (flock(fd, LOCK_UN) < 0) {
JLOG_PERROR("Can't unlock memsink");
return -1;
}
}
usleep(_g_lock_polling);
} while (now < deadline_ts);
return -2;
}
static int _get_frame(int fd, memsink_shared_s *mem, frame_s *frame, uint64_t *frame_id) {
frame_set_data(frame, mem->data, mem->used);
FRAME_COPY_META(mem, frame);
*frame_id = mem->id;
mem->last_client_ts = get_now_monotonic();
int retval = 0;
if (frame->format != V4L2_PIX_FMT_H264) {
JLOG_ERROR("Got non-H264 frame from memsink");
retval = -1;
#define IF_NOT_REPORTED(...) { \
unsigned _error_code = __LINE__; \
if (error_reported != _error_code) { __VA_ARGS__; error_reported = _error_code; } \
}
if (flock(fd, LOCK_UN) < 0) {
JLOG_PERROR("Can't unlock memsink");
retval = -1;
static void *_video_rtp_thread(UNUSED void *arg) {
A_THREAD_RENAME("us_video_rtp");
atomic_store(&_g_video_rtp_tid_created, true);
while (!STOP) {
frame_s *frame;
if (queue_get(_g_video_queue, (void **)&frame, 0.1) == 0) {
LOCK_VIDEO;
rtpv_wrap(_g_rtpv, frame);
UNLOCK_VIDEO;
frame_destroy(frame);
}
}
return retval;
return NULL;
}
static void *_clients_thread(UNUSED void *arg) {
A_THREAD_RENAME("us_clients");
atomic_store(&_g_ready, true);
static void *_video_sink_thread(UNUSED void *arg) {
A_THREAD_RENAME("us_video_sink");
atomic_store(&_g_video_sink_tid_created, true);
frame_s *frame = frame_init();
uint64_t frame_id = 0;
unsigned error_reported = 0;
# define IF_NOT_REPORTED(_error, ...) { \
if (error_reported != _error) { __VA_ARGS__; error_reported = _error; } \
}
while (!STOP) {
if (!HAS_WATCHERS) {
IF_NOT_REPORTED(1, {
JLOG_INFO("No active watchers, memsink disconnected");
});
IF_NOT_REPORTED({ JLOG_INFO("video", "No active watchers, memsink disconnected"); });
usleep(_g_watchers_polling);
continue;
}
@@ -219,32 +136,30 @@ static void *_clients_thread(UNUSED void *arg) {
int fd = -1;
memsink_shared_s *mem = NULL;
if ((fd = shm_open(_g_memsink_obj, O_RDWR, 0)) <= 0) {
IF_NOT_REPORTED(2, {
JLOG_PERROR("Can't open memsink");
});
if ((fd = shm_open(_g_video_sink_name, O_RDWR, 0)) <= 0) {
IF_NOT_REPORTED({ JLOG_PERROR("video", "Can't open memsink"); });
goto close_memsink;
}
if ((mem = memsink_shared_map(fd)) == NULL) {
IF_NOT_REPORTED(3, {
JLOG_PERROR("Can't map memsink");
});
IF_NOT_REPORTED({ JLOG_PERROR("video", "Can't map memsink"); });
goto close_memsink;
}
error_reported = 0;
JLOG_INFO("Memsink opened; reading frames ...");
JLOG_INFO("video", "Memsink opened; reading frames ...");
while (!STOP && HAS_WATCHERS) {
int result = _wait_frame(fd, mem, frame_id);
int result = memsink_fd_wait_frame(fd, mem, frame_id);
if (result == 0) {
if (_get_frame(fd, mem, frame, &frame_id) != 0) {
frame_s *frame = memsink_fd_get_frame(fd, mem, &frame_id);
if (frame == NULL) {
goto close_memsink;
}
LOCK;
rtp_wrap_h264(_g_rtp, frame, _relay_rtp_clients);
UNLOCK;
if (queue_put(_g_video_queue, frame, 0) != 0) {
IF_NOT_REPORTED({ JLOG_PERROR("video", "Video queue is full"); });
frame_destroy(frame);
}
} else if (result == -1) {
goto close_memsink;
}
@@ -252,7 +167,7 @@ static void *_clients_thread(UNUSED void *arg) {
close_memsink:
if (mem != NULL) {
JLOG_INFO("Memsink closed");
JLOG_INFO("video", "Memsink closed");
memsink_shared_unmap(mem);
mem = NULL;
}
@@ -262,45 +177,77 @@ static void *_clients_thread(UNUSED void *arg) {
}
sleep(1); // error_delay
}
# undef IF_NOT_REPORTED
frame_destroy(frame);
return NULL;
}
static int _read_config(const char *config_dir_path) {
char *config_file_path;
janus_config *config = NULL;
static void *_audio_thread(UNUSED void *arg) {
A_THREAD_RENAME("us_audio");
atomic_store(&_g_audio_tid_created, true);
assert(_g_audio_dev_name);
assert(_g_tc358743_dev_path);
A_ASPRINTF(config_file_path, "%s/%s.jcfg", config_dir_path, _plugin_get_package());
JLOG_INFO("Reading config file '%s' ...", config_file_path);
unsigned error_reported = 0;
config = janus_config_parse(config_file_path);
if (config == NULL) {
JLOG_ERROR("Can't read config");
goto error;
}
janus_config_print(config);
janus_config_category *config_memsink = janus_config_get_create(config, NULL, janus_config_type_category, "memsink");
janus_config_item *config_memsink_obj = janus_config_get(config, config_memsink, janus_config_type_item, "object");
if (config_memsink_obj == NULL || config_memsink_obj->value == NULL || config_memsink_obj->value[0] == '\0') {
JLOG_ERROR("Missing config value: memsink.object");
goto error;
}
_g_memsink_obj = strdup(config_memsink_obj->value);
int retval = 0;
goto ok;
error:
retval = -1;
ok:
if (config) {
janus_config_destroy(config);
while (!STOP) {
if (!HAS_WATCHERS) {
usleep(_g_watchers_polling);
continue;
}
free(config_file_path);
return retval;
tc358743_info_s info = {0};
audio_s *audio = NULL;
if (tc358743_read_info(_g_tc358743_dev_path, &info) < 0) {
goto close_audio;
}
if (!info.has_audio) {
IF_NOT_REPORTED({ JLOG_INFO("audio", "No audio presented from the host"); });
goto close_audio;
}
IF_NOT_REPORTED({ JLOG_INFO("audio", "Detected host audio"); });
if ((audio = audio_init(_g_audio_dev_name, info.audio_hz)) == NULL) {
goto close_audio;
}
error_reported = 0;
while (!STOP && HAS_WATCHERS) {
if (
tc358743_read_info(_g_tc358743_dev_path, &info) < 0
|| !info.has_audio
|| audio->pcm_hz != info.audio_hz
) {
goto close_audio;
}
size_t size = RTP_DATAGRAM_SIZE - RTP_HEADER_SIZE;
uint8_t data[size];
uint64_t pts;
int result = audio_get_encoded(audio, data, &size, &pts);
if (result == 0) {
LOCK_AUDIO;
rtpa_wrap(_g_rtpa, data, size, pts);
UNLOCK_AUDIO;
} else if (result == -1) {
goto close_audio;
}
}
close_audio:
if (audio != NULL) {
audio_destroy(audio);
}
sleep(1); // error_delay
}
return NULL;
}
#undef IF_NOT_REPORTED
static void _relay_rtp_clients(const rtp_s *rtp) {
LIST_ITERATE(_g_clients, client, {
client_send(client, rtp);
});
}
static int _plugin_init(janus_callbacks *gw, const char *config_dir_path) {
@@ -310,112 +257,122 @@ static int _plugin_init(janus_callbacks *gw, const char *config_dir_path) {
// sysctl -w net.core.rmem_max=1000000
// sysctl -w net.core.wmem_max=1000000
JLOG_INFO("Initializing plugin ...");
assert(!READY);
assert(!STOP);
if (gw == NULL || config_dir_path == NULL || _read_config(config_dir_path) < 0) {
JLOG_INFO("main", "Initializing plugin ...");
if (gw == NULL || config_dir_path == NULL || read_config(config_dir_path,
&_g_video_sink_name,
&_g_audio_dev_name,
&_g_tc358743_dev_path
) < 0) {
return -1;
}
_g_gw = gw;
_g_rtp = rtp_init();
A_THREAD_CREATE(&_g_tid, _clients_thread, NULL);
_g_video_queue = queue_init(1024);
_g_rtpv = rtpv_init(_relay_rtp_clients);
if (_g_audio_dev_name) {
_g_rtpa = rtpa_init(_relay_rtp_clients);
A_THREAD_CREATE(&_g_audio_tid, _audio_thread, NULL);
}
A_THREAD_CREATE(&_g_video_rtp_tid, _video_rtp_thread, NULL);
A_THREAD_CREATE(&_g_video_sink_tid, _video_sink_thread, NULL);
atomic_store(&_g_ready, true);
return 0;
}
static void _plugin_destroy(void) {
JLOG_INFO("Destroying plugin ...");
JLOG_INFO("main", "Destroying plugin ...");
atomic_store(&_g_stop, true);
if (READY) {
A_THREAD_JOIN(_g_tid);
}
# define JOIN(_tid) { if (atomic_load(&_tid##_created)) { A_THREAD_JOIN(_tid); } }
JOIN(_g_video_sink_tid);
JOIN(_g_video_rtp_tid);
JOIN(_g_audio_tid);
# undef JOIN
LIST_ITERATE(_g_clients, client, {
LIST_REMOVE(_g_clients, client);
free(client);
client_destroy(client);
});
_g_clients = NULL;
rtp_destroy(_g_rtp);
_g_rtp = NULL;
QUEUE_FREE_ITEMS_AND_DESTROY(_g_video_queue, frame_destroy);
_g_gw = NULL;
if (_g_memsink_obj) {
free(_g_memsink_obj);
_g_memsink_obj = NULL;
}
# define DEL(_func, _var) { if (_var) { _func(_var); } }
DEL(rtpa_destroy, _g_rtpa);
DEL(rtpv_destroy, _g_rtpv);
DEL(free, _g_tc358743_dev_path);
DEL(free, _g_audio_dev_name);
DEL(free, _g_video_sink_name);
# undef DEL
}
#define IF_DISABLED(...) { if (!READY || STOP) { __VA_ARGS__ } }
static void _plugin_create_session(janus_plugin_session *session, int *error) {
IF_DISABLED({ *error = -1; return; });
LOCK;
JLOG_INFO("Creating session %p ...", session);
_client_s *client;
A_CALLOC(client, 1);
client->session = session;
client->transmit = true;
static void _plugin_create_session(janus_plugin_session *session, int *err) {
IF_DISABLED({ *err = -1; return; });
LOCK_ALL;
JLOG_INFO("main", "Creating session %p ...", session);
client_s *client = client_init(_g_gw, session, (_g_audio_dev_name != NULL));
LIST_APPEND(_g_clients, client);
atomic_store(&_g_has_watchers, true);
UNLOCK;
UNLOCK_ALL;
}
static void _plugin_destroy_session(janus_plugin_session* session, int *error) {
IF_DISABLED({ *error = -1; return; });
LOCK;
static void _plugin_destroy_session(janus_plugin_session* session, int *err) {
IF_DISABLED({ *err = -1; return; });
LOCK_ALL;
bool found = false;
bool has_watchers = false;
LIST_ITERATE(_g_clients, client, {
if (client->session == session) {
JLOG_INFO("Removing session %p ...", session);
JLOG_INFO("main", "Removing session %p ...", session);
LIST_REMOVE(_g_clients, client);
free(client);
client_destroy(client);
found = true;
} else {
has_watchers = (has_watchers || client->transmit);
has_watchers = (has_watchers || atomic_load(&client->transmit));
}
});
if (!found) {
JLOG_WARN("No session %p", session);
*error = -2;
JLOG_WARN("main", "No session %p", session);
*err = -2;
}
atomic_store(&_g_has_watchers, has_watchers);
UNLOCK;
UNLOCK_ALL;
}
static json_t *_plugin_query_session(janus_plugin_session *session) {
IF_DISABLED({ return NULL; });
json_t *info = NULL;
LOCK;
LOCK_ALL;
LIST_ITERATE(_g_clients, client, {
if (client->session == session) {
info = json_string("session_found");
break;
}
});
UNLOCK;
UNLOCK_ALL;
return info;
}
static void _set_transmit(janus_plugin_session *session, UNUSED const char *msg, bool transmit) {
IF_DISABLED({ return; });
LOCK;
LOCK_ALL;
bool found = false;
bool has_watchers = false;
LIST_ITERATE(_g_clients, client, {
if (client->session == session) {
client->transmit = transmit;
//JLOG_INFO("%s session %p", msg, session);
atomic_store(&client->transmit, transmit);
// JLOG_INFO("main", "%s session %p", msg, session);
found = true;
}
has_watchers = (has_watchers || client->transmit);
has_watchers = (has_watchers || atomic_load(&client->transmit));
});
if (!found) {
JLOG_WARN("No session %p", session);
JLOG_WARN("main", "No session %p", session);
}
atomic_store(&_g_has_watchers, has_watchers);
UNLOCK;
UNLOCK_ALL;
}
#undef IF_DISABLED
@@ -440,12 +397,12 @@ static struct janus_plugin_result *_plugin_handle_message(
}
# define PUSH_ERROR(_error, _reason) { \
/*JLOG_ERROR("Message error in session %p: %s", session, _reason);*/ \
/*JLOG_ERROR("main", "Message error in session %p: %s", session, _reason);*/ \
json_t *_event = json_object(); \
json_object_set_new(_event, "ustreamer", json_string("event")); \
json_object_set_new(_event, "error_code", json_integer(_error)); \
json_object_set_new(_event, "error", json_string(_reason)); \
_g_gw->push_event(session, &_plugin, transaction, _event, NULL); \
_g_gw->push_event(session, create(), transaction, _event, NULL); \
json_decref(_event); \
}
@@ -460,7 +417,7 @@ static struct janus_plugin_result *_plugin_handle_message(
PUSH_ERROR(400, "Request not a string");
goto ok_wait;
}
//JLOG_INFO("Message: %s", request_str);
// JLOG_INFO("main", "Message: %s", request_str);
# define PUSH_STATUS(_status, _jsep) { \
json_t *_event = json_object(); \
@@ -468,7 +425,7 @@ static struct janus_plugin_result *_plugin_handle_message(
json_t *_result = json_object(); \
json_object_set_new(_result, "status", json_string(_status)); \
json_object_set_new(_event, "result", _result); \
_g_gw->push_event(session, &_plugin, transaction, _event, _jsep); \
_g_gw->push_event(session, create(), transaction, _event, _jsep); \
json_decref(_event); \
}
@@ -479,12 +436,25 @@ static struct janus_plugin_result *_plugin_handle_message(
PUSH_STATUS("stopped", NULL);
} else if (!strcmp(request_str, "watch")) {
char *sdp = rtp_make_sdp(_g_rtp);
if (sdp == NULL) {
PUSH_ERROR(503, "Haven't received SPS/PPS from memsink yet");
goto ok_wait;
char *sdp;
{
char *video_sdp = rtpv_make_sdp(_g_rtpv);
if (video_sdp == NULL) {
PUSH_ERROR(503, "Haven't received SPS/PPS from memsink yet");
goto ok_wait;
}
char *audio_sdp = (_g_rtpa ? rtpa_make_sdp(_g_rtpa) : strdup(""));
A_ASPRINTF(sdp,
"v=0" RN
"o=- %" PRIu64 " 1 IN IP4 0.0.0.0" RN
"s=PiKVM uStreamer" RN
"t=0 0" RN
"%s%s",
get_now_id() >> 1, audio_sdp, video_sdp
);
free(audio_sdp);
free(video_sdp);
}
//JLOG_INFO("SDP generated:\n%s", sdp);
json_t *offer_jsep = json_pack("{ssss}", "type", "offer", "sdp", sdp);
free(sdp);
PUSH_STATUS("started", offer_jsep);
@@ -498,25 +468,52 @@ static struct janus_plugin_result *_plugin_handle_message(
FREE_MSG_JSEP;
return janus_plugin_result_new(JANUS_PLUGIN_OK_WAIT, NULL, NULL);
# undef PUSH_STATUS
# undef PUSH_ERROR
# undef FREE_MSG_JSEP
}
// ***** Plugin *****
static int _plugin_get_api_compatibility(void) { return JANUS_PLUGIN_API_VERSION; }
static int _plugin_get_version(void) { return VERSION_U; }
static const char *_plugin_get_version_string(void) { return VERSION; }
static const char *_plugin_get_description(void) { return "Pi-KVM uStreamer Janus plugin for H.264 video"; }
static const char *_plugin_get_name(void) { return "ustreamer"; }
static const char *_plugin_get_description(void) { return "PiKVM uStreamer Janus plugin for H.264 video"; }
static const char *_plugin_get_name(void) { return PLUGIN_NAME; }
static const char *_plugin_get_author(void) { return "Maxim Devaev <mdevaev@gmail.com>"; }
static const char *_plugin_get_package(void) { return "janus.plugin.ustreamer"; }
static const char *_plugin_get_package(void) { return PLUGIN_PACKAGE; }
static void _plugin_incoming_rtp(UNUSED janus_plugin_session *handle, UNUSED janus_plugin_rtp *packet) {
// Just a stub to avoid logging spam about the plugin's purpose
}
#undef STOP
#undef READY
#undef UNLOCK
#undef LOCK
janus_plugin *create(void) {
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Woverride-init"
static janus_plugin plugin = JANUS_PLUGIN_INIT(
.init = _plugin_init,
.destroy = _plugin_destroy,
#undef JLOG_PERROR
#undef JLOG_ERROR
#undef JLOG_WARN
#undef JLOG_INFO
.create_session = _plugin_create_session,
.destroy_session = _plugin_destroy_session,
.query_session = _plugin_query_session,
.setup_media = _plugin_setup_media,
.hangup_media = _plugin_hangup_media,
.handle_message = _plugin_handle_message,
.get_api_compatibility = _plugin_get_api_compatibility,
.get_version = _plugin_get_version,
.get_version_string = _plugin_get_version_string,
.get_description = _plugin_get_description,
.get_name = _plugin_get_name,
.get_author = _plugin_get_author,
.get_package = _plugin_get_package,
.incoming_rtp = _plugin_incoming_rtp,
);
# pragma GCC diagnostic pop
return &plugin;
}

99
janus/src/queue.c Normal file
View File

@@ -0,0 +1,99 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include "queue.h"
queue_s *queue_init(unsigned capacity) {
queue_s *queue;
A_CALLOC(queue, 1);
A_CALLOC(queue->items, capacity);
queue->capacity = capacity;
A_MUTEX_INIT(&queue->mutex);
pthread_condattr_t attrs;
assert(!pthread_condattr_init(&attrs));
assert(!pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC));
assert(!pthread_cond_init(&queue->full_cond, &attrs));
assert(!pthread_cond_init(&queue->empty_cond, &attrs));
assert(!pthread_condattr_destroy(&attrs));
return queue;
}
void queue_destroy(queue_s *queue) {
free(queue->items);
free(queue);
}
#define WAIT_OR_UNLOCK(_var, _cond) { \
struct timespec _ts; \
assert(!clock_gettime(CLOCK_MONOTONIC, &_ts)); \
ld_to_timespec(timespec_to_ld(&_ts) + timeout, &_ts); \
while (_var) { \
int err = pthread_cond_timedwait(_cond, &queue->mutex, &_ts); \
if (err == ETIMEDOUT) { \
A_MUTEX_UNLOCK(&queue->mutex); \
return -1; \
} \
assert(!err); \
} \
}
int queue_put(queue_s *queue, void *item, long double timeout) {
A_MUTEX_LOCK(&queue->mutex);
if (timeout == 0) {
if (queue->size == queue->capacity) {
A_MUTEX_UNLOCK(&queue->mutex);
return -1;
}
} else {
WAIT_OR_UNLOCK(queue->size == queue->capacity, &queue->full_cond);
}
queue->items[queue->in] = item;
++queue->size;
++queue->in;
queue->in %= queue->capacity;
A_MUTEX_UNLOCK(&queue->mutex);
assert(!pthread_cond_broadcast(&queue->empty_cond));
return 0;
}
int queue_get(queue_s *queue, void **item, long double timeout) {
A_MUTEX_LOCK(&queue->mutex);
WAIT_OR_UNLOCK(queue->size == 0, &queue->empty_cond);
*item = queue->items[queue->out];
--queue->size;
++queue->out;
queue->out %= queue->capacity;
A_MUTEX_UNLOCK(&queue->mutex);
assert(!pthread_cond_broadcast(&queue->full_cond));
return 0;
}
#undef WAIT_OR_UNLOCK
int queue_get_free(queue_s *queue) {
A_MUTEX_LOCK(&queue->mutex);
unsigned size = queue->size;
A_MUTEX_UNLOCK(&queue->mutex);
return queue->capacity - size;
}

67
janus/src/queue.h Normal file
View File

@@ -0,0 +1,67 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 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 <errno.h>
#include <time.h>
#include <assert.h>
#include <pthread.h>
#include "uslibs/tools.h"
#include "uslibs/threading.h"
// Based on https://github.com/seifzadeh/c-pthread-queue/blob/master/queue.h
typedef struct {
void **items;
unsigned size;
unsigned capacity;
unsigned in;
unsigned out;
pthread_mutex_t mutex;
pthread_cond_t full_cond;
pthread_cond_t empty_cond;
} queue_s;
#define QUEUE_FREE_ITEMS_AND_DESTROY(_queue, _free_item) { \
while (!queue_get_free(_queue)) { \
void *_ptr; \
assert(!queue_get(_queue, &_ptr, 0.1)); \
if (_ptr != NULL) { \
_free_item(_ptr); \
} \
} \
queue_destroy(_queue); \
}
queue_s *queue_init(unsigned capacity);
void queue_destroy(queue_s *queue);
int queue_put(queue_s *queue, void *item, long double timeout);
int queue_get(queue_s *queue, void **item, long double timeout);
int queue_get_free(queue_s *queue);

View File

@@ -26,182 +26,32 @@
#include "rtp.h"
#define PAYLOAD 96 // Payload type
#define PRE 3 // Annex B prefix length
void _rtp_process_nalu(rtp_s *rtp, const uint8_t *data, size_t size, uint32_t pts, bool marked, rtp_callback_f callback);
static void _rtp_write_header(rtp_s *rtp, uint32_t pts, bool marked);
static ssize_t _find_annexb(const uint8_t *data, size_t size);
rtp_s *rtp_init(void) {
rtp_s *rtp_init(unsigned payload, bool video) {
rtp_s *rtp;
A_CALLOC(rtp, 1);
rtp->payload = payload;
rtp->video = video;
rtp->ssrc = triple_u32(get_now_monotonic_u64());
rtp->sps = frame_init();
rtp->pps = frame_init();
A_MUTEX_INIT(&rtp->mutex);
return rtp;
}
rtp_s *rtp_dup(const rtp_s *rtp) {
rtp_s *new;
A_CALLOC(new, 1);
memcpy(new, rtp, sizeof(rtp_s));
return new;
}
void rtp_destroy(rtp_s *rtp) {
A_MUTEX_DESTROY(&rtp->mutex);
frame_destroy(rtp->pps);
frame_destroy(rtp->sps);
free(rtp);
}
char *rtp_make_sdp(rtp_s *rtp) {
A_MUTEX_LOCK(&rtp->mutex);
if (rtp->sps->used == 0 || rtp->pps->used == 0) {
A_MUTEX_UNLOCK(&rtp->mutex);
return NULL;
}
char *sps = NULL;
char *pps = NULL;
base64_encode(rtp->sps->data, rtp->sps->used, &sps, NULL);
base64_encode(rtp->pps->data, rtp->pps->used, &pps, NULL);
A_MUTEX_UNLOCK(&rtp->mutex);
// https://tools.ietf.org/html/rfc6184
// https://github.com/meetecho/janus-gateway/issues/2443
char *sdp;
A_ASPRINTF(sdp,
"v=0" RN
"o=- %" PRIu64 " 1 IN IP4 127.0.0.1" RN
"s=Pi-KVM uStreamer" RN
"t=0 0" RN
"m=video 1 RTP/SAVPF %d" RN
"c=IN IP4 0.0.0.0" RN
"a=rtpmap:%d H264/90000" RN
"a=fmtp:%d profile-level-id=42E01F" RN
"a=fmtp:%d packetization-mode=1" RN
"a=fmtp:%d sprop-sps=%s" RN
"a=fmtp:%d sprop-pps=%s" RN
"a=rtcp-fb:%d nack" RN
"a=rtcp-fb:%d nack pli" RN
"a=rtcp-fb:%d goog-remb" RN
"a=sendonly" RN,
get_now_id() >> 1, PAYLOAD, PAYLOAD, PAYLOAD, PAYLOAD,
PAYLOAD, sps,
PAYLOAD, pps,
PAYLOAD, PAYLOAD, PAYLOAD
);
free(sps);
free(pps);
return sdp;
}
void rtp_wrap_h264(rtp_s *rtp, const frame_s *frame, rtp_callback_f callback) {
// There is a complicated logic here but everything works as it should:
// - https://github.com/pikvm/ustreamer/issues/115#issuecomment-893071775
assert(frame->format == V4L2_PIX_FMT_H264);
const uint32_t pts = get_now_monotonic_u64() * 9 / 100; // PTS units are in 90 kHz
ssize_t last_offset = -PRE;
while (true) { // Find and iterate by nalus
const size_t next_start = last_offset + PRE;
ssize_t offset = _find_annexb(frame->data + next_start, frame->used - next_start);
if (offset < 0) {
break;
}
offset += next_start;
if (last_offset >= 0) {
const uint8_t *data = frame->data + last_offset + PRE;
size_t size = offset - last_offset - PRE;
if (data[size - 1] == 0) { // Check for extra 00
--size;
}
_rtp_process_nalu(rtp, data, size, pts, false, callback);
}
last_offset = offset;
}
if (last_offset >= 0) {
const uint8_t *data = frame->data + last_offset + PRE;
size_t size = frame->used - last_offset - PRE;
_rtp_process_nalu(rtp, data, size, pts, true, callback);
}
}
void _rtp_process_nalu(rtp_s *rtp, const uint8_t *data, size_t size, uint32_t pts, bool marked, rtp_callback_f callback) {
const unsigned ref_idc = (data[0] >> 5) & 3;
const unsigned type = data[0] & 0x1F;
frame_s *ps = NULL;
switch (type) {
case 7: ps = rtp->sps; break;
case 8: ps = rtp->pps; break;
}
if (ps) {
A_MUTEX_LOCK(&rtp->mutex);
frame_set_data(ps, data, size);
A_MUTEX_UNLOCK(&rtp->mutex);
}
# define HEADER_SIZE 12
if (size + HEADER_SIZE <= RTP_DATAGRAM_SIZE) {
_rtp_write_header(rtp, pts, marked);
memcpy(rtp->datagram + HEADER_SIZE, data, size);
callback(rtp->datagram, size + HEADER_SIZE);
return;
}
const size_t fu_overhead = HEADER_SIZE + 2; // FU-A overhead
const uint8_t *src = data + 1;
ssize_t remaining = size - 1;
bool first = true;
while (remaining > 0) {
ssize_t frag_size = RTP_DATAGRAM_SIZE - fu_overhead;
const bool last = (remaining <= frag_size);
if (last) {
frag_size = remaining;
}
_rtp_write_header(rtp, pts, (marked && last));
rtp->datagram[HEADER_SIZE] = 28 | (ref_idc << 5);
uint8_t fu = type;
if (first) {
fu |= 0x80;
}
if (last) {
fu |= 0x40;
}
rtp->datagram[HEADER_SIZE + 1] = fu;
memcpy(rtp->datagram + fu_overhead, src, frag_size);
callback(rtp->datagram, fu_overhead + frag_size);
src += frag_size;
remaining -= frag_size;
first = false;
}
# undef HEADER_SIZE
}
static void _rtp_write_header(rtp_s *rtp, uint32_t pts, bool marked) {
void rtp_write_header(rtp_s *rtp, uint32_t pts, bool marked) {
uint32_t word0 = 0x80000000;
if (marked) {
word0 |= 1 << 23;
}
word0 |= (PAYLOAD & 0x7F) << 16;
word0 |= (rtp->payload & 0x7F) << 16;
word0 |= rtp->seq;
++rtp->seq;
@@ -211,18 +61,3 @@ static void _rtp_write_header(rtp_s *rtp, uint32_t pts, bool marked) {
WRITE_BE_U32(8, rtp->ssrc);
# undef WRITE_BE_U32
}
static ssize_t _find_annexb(const uint8_t *data, size_t size) {
// Parses buffer for 00 00 01 start codes
if (size >= PRE) {
for (size_t index = 0; index <= size - PRE; ++index) {
if (data[index] == 0 && data[index + 1] == 0 && data[index + 2] == 1) {
return index;
}
}
}
return -1;
}
#undef PRE
#undef PAYLOAD

View File

@@ -2,9 +2,6 @@
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# This source file is partially based on this code: #
# - https://github.com/catid/kvm/blob/master/kvm_pipeline/src #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
@@ -26,44 +23,34 @@
#pragma once
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <linux/videodev2.h>
#include <pthread.h>
#include "tools.h"
#include "threading.h"
#include "frame.h"
#include "base64.h"
#include "uslibs/tools.h"
// https://stackoverflow.com/questions/47635545/why-webrtc-chose-rtp-max-packet-size-to-1200-bytes
#define RTP_DATAGRAM_SIZE 1200
#define RTP_DATAGRAM_SIZE 1200
#define RTP_HEADER_SIZE 12
typedef struct {
uint32_t ssrc;
uint16_t seq;
unsigned payload;
bool video;
uint32_t ssrc;
uint8_t datagram[RTP_DATAGRAM_SIZE];
frame_s *sps; // Actually not a frame, just a bytes storage
frame_s *pps;
pthread_mutex_t mutex;
uint16_t seq;
uint8_t datagram[RTP_DATAGRAM_SIZE];
size_t used;
} rtp_s;
typedef void (*rtp_callback_f)(const uint8_t *datagram, size_t size);
typedef void (*rtp_callback_f)(const rtp_s *rtp);
rtp_s *rtp_init(void);
rtp_s *rtp_init(unsigned payload, bool video);
rtp_s *rtp_dup(const rtp_s *rtp);
void rtp_destroy(rtp_s *rtp);
char *rtp_make_sdp(rtp_s *rtp);
void rtp_wrap_h264(rtp_s *rtp, const frame_s *frame, rtp_callback_f callback);
void rtp_write_header(rtp_s *rtp, uint32_t pts, bool marked);

66
janus/src/rtpa.c Normal file
View File

@@ -0,0 +1,66 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include "rtpa.h"
rtpa_s *rtpa_init(rtp_callback_f callback) {
rtpa_s *rtpa;
A_CALLOC(rtpa, 1);
rtpa->rtp = rtp_init(111, false);
rtpa->callback = callback;
return rtpa;
}
void rtpa_destroy(rtpa_s *rtpa) {
rtp_destroy(rtpa->rtp);
free(rtpa);
}
char *rtpa_make_sdp(rtpa_s *rtpa) {
# define PAYLOAD rtpa->rtp->payload
char *sdp;
A_ASPRINTF(sdp,
"m=audio 1 RTP/SAVPF %u" RN
"c=IN IP4 0.0.0.0" RN
"a=rtpmap:%u OPUS/48000/2" RN
// "a=fmtp:%u useinbandfec=1" RN
"a=rtcp-fb:%u nack" RN
"a=rtcp-fb:%u nack pli" RN
"a=rtcp-fb:%u goog-remb" RN
"a=ssrc:%" PRIu32 " cname:ustreamer" RN
"a=sendonly" RN,
PAYLOAD, PAYLOAD, PAYLOAD, PAYLOAD, PAYLOAD, // PAYLOAD,
rtpa->rtp->ssrc
);
# undef PAYLOAD
return sdp;
}
void rtpa_wrap(rtpa_s *rtpa, const uint8_t *data, size_t size, uint32_t pts) {
if (size + RTP_HEADER_SIZE <= RTP_DATAGRAM_SIZE) {
rtp_write_header(rtpa->rtp, pts, false);
memcpy(rtpa->rtp->datagram + RTP_HEADER_SIZE, data, size);
rtpa->rtp->used = size + RTP_HEADER_SIZE;
rtpa->callback(rtpa->rtp);
}
}

48
janus/src/rtpa.h Normal file
View File

@@ -0,0 +1,48 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 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 <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <sys/types.h>
#include "uslibs/tools.h"
#include "uslibs/threading.h"
#include "rtp.h"
typedef struct {
rtp_s *rtp;
rtp_callback_f callback;
} rtpa_s;
rtpa_s *rtpa_init(rtp_callback_f callback);
void rtpa_destroy(rtpa_s *rtpa);
char *rtpa_make_sdp(rtpa_s *rtpa);
void rtpa_wrap(rtpa_s *rtpa, const uint8_t *data, size_t size, uint32_t pts);

213
janus/src/rtpv.c Normal file
View File

@@ -0,0 +1,213 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# This source file is partially based on this code: #
# - https://github.com/catid/kvm/blob/master/kvm_pipeline/src #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include "rtpv.h"
void _rtpv_process_nalu(rtpv_s *rtpv, const uint8_t *data, size_t size, uint32_t pts, bool marked);
static ssize_t _find_annexb(const uint8_t *data, size_t size);
rtpv_s *rtpv_init(rtp_callback_f callback) {
rtpv_s *rtpv;
A_CALLOC(rtpv, 1);
rtpv->rtp = rtp_init(96, true);
rtpv->callback = callback;
rtpv->sps = frame_init();
rtpv->pps = frame_init();
A_MUTEX_INIT(&rtpv->mutex);
return rtpv;
}
void rtpv_destroy(rtpv_s *rtpv) {
A_MUTEX_DESTROY(&rtpv->mutex);
frame_destroy(rtpv->pps);
frame_destroy(rtpv->sps);
rtp_destroy(rtpv->rtp);
free(rtpv);
}
char *rtpv_make_sdp(rtpv_s *rtpv) {
A_MUTEX_LOCK(&rtpv->mutex);
if (rtpv->sps->used == 0 || rtpv->pps->used == 0) {
A_MUTEX_UNLOCK(&rtpv->mutex);
return NULL;
}
char *sps = NULL;
char *pps = NULL;
base64_encode(rtpv->sps->data, rtpv->sps->used, &sps, NULL);
base64_encode(rtpv->pps->data, rtpv->pps->used, &pps, NULL);
A_MUTEX_UNLOCK(&rtpv->mutex);
# define PAYLOAD rtpv->rtp->payload
// https://tools.ietf.org/html/rfc6184
// https://github.com/meetecho/janus-gateway/issues/2443
char *sdp;
A_ASPRINTF(sdp,
"m=video 1 RTP/SAVPF %u" RN
"c=IN IP4 0.0.0.0" RN
"a=rtpmap:%u H264/90000" RN
"a=fmtp:%u profile-level-id=42E01F" RN
"a=fmtp:%u packetization-mode=1" RN
"a=fmtp:%u sprop-sps=%s" RN
"a=fmtp:%u sprop-pps=%s" RN
"a=rtcp-fb:%u nack" RN
"a=rtcp-fb:%u nack pli" RN
"a=rtcp-fb:%u goog-remb" RN
"a=ssrc:%" PRIu32 " cname:ustreamer" RN
// XXX: See client.c
// "a=extmap:1 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay" RN
"a=sendonly" RN,
PAYLOAD, PAYLOAD, PAYLOAD, PAYLOAD,
PAYLOAD, sps,
PAYLOAD, pps,
PAYLOAD, PAYLOAD, PAYLOAD,
rtpv->rtp->ssrc
);
# undef PAYLOAD
free(sps);
free(pps);
return sdp;
}
#define PRE 3 // Annex B prefix length
void rtpv_wrap(rtpv_s *rtpv, const frame_s *frame) {
// There is a complicated logic here but everything works as it should:
// - https://github.com/pikvm/ustreamer/issues/115#issuecomment-893071775
assert(frame->format == V4L2_PIX_FMT_H264);
const uint32_t pts = get_now_monotonic_u64() * 9 / 100; // PTS units are in 90 kHz
ssize_t last_offset = -PRE;
while (true) { // Find and iterate by nalus
const size_t next_start = last_offset + PRE;
ssize_t offset = _find_annexb(frame->data + next_start, frame->used - next_start);
if (offset < 0) {
break;
}
offset += next_start;
if (last_offset >= 0) {
const uint8_t *data = frame->data + last_offset + PRE;
size_t size = offset - last_offset - PRE;
if (data[size - 1] == 0) { // Check for extra 00
--size;
}
_rtpv_process_nalu(rtpv, data, size, pts, false);
}
last_offset = offset;
}
if (last_offset >= 0) {
const uint8_t *data = frame->data + last_offset + PRE;
size_t size = frame->used - last_offset - PRE;
_rtpv_process_nalu(rtpv, data, size, pts, true);
}
}
void _rtpv_process_nalu(rtpv_s *rtpv, const uint8_t *data, size_t size, uint32_t pts, bool marked) {
const unsigned ref_idc = (data[0] >> 5) & 3;
const unsigned type = data[0] & 0x1F;
frame_s *ps = NULL;
switch (type) {
case 7: ps = rtpv->sps; break;
case 8: ps = rtpv->pps; break;
}
if (ps) {
A_MUTEX_LOCK(&rtpv->mutex);
frame_set_data(ps, data, size);
A_MUTEX_UNLOCK(&rtpv->mutex);
}
# define DG rtpv->rtp->datagram
if (size + RTP_HEADER_SIZE <= RTP_DATAGRAM_SIZE) {
rtp_write_header(rtpv->rtp, pts, marked);
memcpy(DG + RTP_HEADER_SIZE, data, size);
rtpv->rtp->used = size + RTP_HEADER_SIZE;
rtpv->callback(rtpv->rtp);
return;
}
const size_t fu_overhead = RTP_HEADER_SIZE + 2; // FU-A overhead
const uint8_t *src = data + 1;
ssize_t remaining = size - 1;
bool first = true;
while (remaining > 0) {
ssize_t frag_size = RTP_DATAGRAM_SIZE - fu_overhead;
const bool last = (remaining <= frag_size);
if (last) {
frag_size = remaining;
}
rtp_write_header(rtpv->rtp, pts, (marked && last));
DG[RTP_HEADER_SIZE] = 28 | (ref_idc << 5);
uint8_t fu = type;
if (first) {
fu |= 0x80;
}
if (last) {
fu |= 0x40;
}
DG[RTP_HEADER_SIZE + 1] = fu;
memcpy(DG + fu_overhead, src, frag_size);
rtpv->rtp->used = fu_overhead + frag_size;
rtpv->callback(rtpv->rtp);
src += frag_size;
remaining -= frag_size;
first = false;
}
# undef DG
}
static ssize_t _find_annexb(const uint8_t *data, size_t size) {
// Parses buffer for 00 00 01 start codes
if (size >= PRE) {
for (size_t index = 0; index <= size - PRE; ++index) {
if (data[index] == 0 && data[index + 1] == 0 && data[index + 2] == 1) {
return index;
}
}
}
return -1;
}
#undef PRE

57
janus/src/rtpv.h Normal file
View File

@@ -0,0 +1,57 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 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 <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <assert.h>
#include <sys/types.h>
#include <linux/videodev2.h>
#include <pthread.h>
#include "uslibs/tools.h"
#include "uslibs/threading.h"
#include "uslibs/frame.h"
#include "uslibs/base64.h"
#include "rtp.h"
typedef struct {
rtp_s *rtp;
rtp_callback_f callback;
frame_s *sps; // Actually not a frame, just a bytes storage
frame_s *pps;
pthread_mutex_t mutex;
} rtpv_s;
rtpv_s *rtpv_init(rtp_callback_f callback);
void rtpv_destroy(rtpv_s *rtpv);
char *rtpv_make_sdp(rtpv_s *rtpv);
void rtpv_wrap(rtpv_s *rtpv, const frame_s *frame);

64
janus/src/tc358743.c Normal file
View File

@@ -0,0 +1,64 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#include "tc358743.h"
#ifndef V4L2_CID_USER_TC358743_BASE
# define V4L2_CID_USER_TC358743_BASE (V4L2_CID_USER_BASE + 0x1080)
#endif
#ifndef TC358743_CID_AUDIO_PRESENT
# define TC358743_CID_AUDIO_PRESENT (V4L2_CID_USER_TC358743_BASE + 1)
#endif
#ifndef TC358743_CID_AUDIO_SAMPLING_RATE
# define TC358743_CID_AUDIO_SAMPLING_RATE (V4L2_CID_USER_TC358743_BASE + 0)
#endif
int tc358743_read_info(const char *path, tc358743_info_s *info) {
MEMSET_ZERO(*info);
int fd = -1;
if ((fd = open(path, O_RDWR)) < 0) {
JLOG_PERROR("audio", "Can't open TC358743 V4L2 device");
return -1;
}
# define READ_CID(_cid, _field) { \
struct v4l2_control ctl = {0}; \
ctl.id = _cid; \
if (xioctl(fd, VIDIOC_G_CTRL, &ctl) < 0) { \
JLOG_PERROR("audio", "Can't get value of " #_cid); \
close(fd); \
return -1; \
} \
info->_field = ctl.value; \
}
READ_CID(TC358743_CID_AUDIO_PRESENT, has_audio);
READ_CID(TC358743_CID_AUDIO_SAMPLING_RATE, audio_hz);
# undef READ_CID
close(fd);
return 0;
}

46
janus/src/tc358743.h Normal file
View File

@@ -0,0 +1,46 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 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 <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <sys/types.h>
#include <linux/videodev2.h>
#include <linux/v4l2-controls.h>
#include "uslibs/tools.h"
#include "uslibs/xioctl.h"
#include "logging.h"
typedef struct {
bool has_audio;
unsigned audio_hz;
} tc358743_info_s;
int tc358743_read_info(const char *path, tc358743_info_s *info);

View File

@@ -1 +0,0 @@
../../src/libs/threading.h

View File

@@ -1 +0,0 @@
../../src/libs/tools.h

1
janus/src/uslibs/base64.c Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/base64.c

1
janus/src/uslibs/base64.h Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/base64.h

1
janus/src/uslibs/const.h Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/const.h

1
janus/src/uslibs/frame.c Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/frame.c

1
janus/src/uslibs/frame.h Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/frame.h

1
janus/src/uslibs/list.h Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/list.h

View File

@@ -0,0 +1 @@
../../../src/libs/memsinksh.h

View File

@@ -0,0 +1 @@
../../../src/libs/threading.h

1
janus/src/uslibs/tools.h Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/tools.h

1
janus/src/uslibs/xioctl.h Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/xioctl.h

View File

@@ -1,3 +1,6 @@
#define CHAR_BIT 8
#define WITH_GPIO
#define JANUS_PLUGIN_INIT(...) { __VA_ARGS__ }
#define EVTHREAD_USE_PTHREADS_IMPLEMENTED 1
#define CLOCK_MONOTONIC_RAW 1
#define CLOCK_MONOTONIC_FAST 1

View File

@@ -15,15 +15,8 @@ disable =
locally-disabled,
fixme,
missing-docstring,
no-init,
no-self-use,
superfluous-parens,
abstract-class-not-used,
abstract-class-little-used,
duplicate-code,
bad-continuation,
bad-whitespace,
star-args,
broad-except,
redundant-keyword-arg,
wrong-import-order,
@@ -39,9 +32,6 @@ msg-template = {symbol} -- {path}:{line}({obj}): {msg}
max-line-length = 160
[BASIC]
# List of builtins function names that should not be used, separated by a comma
bad-functions =
# Good variable names which should always be accepted, separated by a comma
good-names = _, __, x, y, ws, make-html-h, make-jpeg-h

View File

@@ -9,8 +9,9 @@ changedir = /src
[testenv:cppcheck]
whitelist_externals = cppcheck
commands = cppcheck \
-j4 \
--force \
--std=c11 \
--std=c17 \
--error-exitcode=1 \
--quiet \
--enable=warning,unusedFunction,portability,performance,style \
@@ -19,7 +20,7 @@ commands = cppcheck \
--inline-suppr \
--library=python \
--include=linters/cppcheck.h \
src python/ustreamer.c janus/rtp.h janus/rtp.c janus/plugin.c
src python/*.? janus/*.?
[testenv:flake8]
whitelist_externals = bash

View File

@@ -1,6 +1,6 @@
.\" Manpage for ustreamer-dump.
.\" Open an issue or pull request to https://github.com/pikvm/ustreamer to correct errors or typos
.TH USTREAMER-DUMP 1 "version 5.1" "January 2021"
.TH USTREAMER-DUMP 1 "version 5.15" "January 2021"
.SH NAME
ustreamer-dump \- Dump uStreamer's memory sink to file

View File

@@ -1,6 +1,6 @@
.\" Manpage for ustreamer.
.\" Open an issue or pull request to https://github.com/pikvm/ustreamer to correct errors or typos
.TH USTREAMER 1 "version 5.1" "November 2020"
.TH USTREAMER 1 "version 5.15" "November 2020"
.SH NAME
ustreamer \- stream MJPEG video from any V4L2 device to the network

View File

@@ -3,34 +3,29 @@
pkgname=ustreamer
pkgver=5.1
pkgver=5.15
pkgrel=1
pkgdesc="Lightweight and fast MJPEG-HTTP streamer"
url="https://github.com/pikvm/ustreamer"
license=(GPL)
arch=(i686 x86_64 armv6h armv7h aarch64)
depends=(libjpeg libevent libbsd libgpiod)
makedepends=(gcc make)
depends=(libjpeg libevent libbsd libgpiod systemd)
makedepends=(gcc make systemd)
source=(${pkgname}::"git+https://github.com/pikvm/ustreamer#commit=v${pkgver}")
md5sums=(SKIP)
_options="WITH_GPIO=1"
_options="WITH_GPIO=1 WITH_SYSTEMD=1"
if [ -e /usr/bin/python3 ]; then
_options="$_options WITH_PYTHON=1"
depends+=(python)
makedepends+=(python-setuptools)
fi
if [ -e /usr/include/janus/plugins/plugin.h ];then
depends+=(janus-gateway-pikvm)
makedepends+=(janus-gateway-pikvm)
depends+=(janus-gateway-pikvm alsa-lib opus)
makedepends+=(janus-gateway-pikvm alsa-lib opus)
_options="$_options WITH_JANUS=1"
fi
if [ -e /usr/include/systemd/sd-daemon.h ];then
depends+=(systemd)
makedepends+=(systemd)
_options="$_options WITH_SYSTEMD=1"
fi
# LD does not link mmal with this option

View File

@@ -6,7 +6,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=ustreamer
PKG_VERSION:=5.1
PKG_VERSION:=5.15
PKG_RELEASE:=1
PKG_MAINTAINER:=Maxim Devaev <mdevaev@gmail.com>

View File

@@ -1,14 +1,25 @@
import os
from typing import List
from setuptools import Extension
from setuptools import setup
# =====
def _find_sources(suffix: str) -> List[str]:
sources: List[str] = []
for (root_path, _, names) in os.walk("src"):
for name in names:
if name.endswith(suffix):
sources.append(os.path.join(root_path, name))
return sources
if __name__ == "__main__":
setup(
name="ustreamer",
version="5.1",
version="5.15",
description="uStreamer tools",
author="Maxim Devaev",
author_email="mdevaev@gmail.com",
@@ -17,9 +28,10 @@ if __name__ == "__main__":
Extension(
"ustreamer",
libraries=["rt", "m", "pthread"],
extra_compile_args=["-std=c17", "-D_GNU_SOURCE"],
undef_macros=["NDEBUG"],
sources=["src/" + name for name in os.listdir("src") if name.endswith(".c")],
depends=["src/" + name for name in os.listdir("src") if name.endswith(".h")],
sources=_find_sources(".c"),
depends=_find_sources(".h"),
),
],
)

View File

@@ -1 +0,0 @@
../../src/libs/frame.c

View File

@@ -1 +0,0 @@
../../src/libs/frame.h

View File

@@ -1 +0,0 @@
../../src/libs/memsinksh.h

View File

@@ -1 +0,0 @@
../../src/libs/tools.h

1
python/src/uslibs/frame.c Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/frame.c

1
python/src/uslibs/frame.h Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/frame.h

View File

@@ -0,0 +1 @@
../../../src/libs/memsinksh.h

1
python/src/uslibs/tools.h Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/tools.h

View File

@@ -13,9 +13,9 @@
#include <Python.h>
#include "tools.h"
#include "frame.h"
#include "memsinksh.h"
#include "uslibs/tools.h"
#include "uslibs/frame.h"
#include "uslibs/memsinksh.h"
typedef struct {

View File

@@ -10,7 +10,7 @@ LDFLAGS ?=
_USTR = ustreamer.bin
_DUMP = ustreamer-dump.bin
_CFLAGS = -MD -c -std=c11 -Wall -Wextra -D_GNU_SOURCE $(CFLAGS)
_CFLAGS = -MD -c -std=c17 -Wall -Wextra -D_GNU_SOURCE $(CFLAGS)
_LDFLAGS = $(LDFLAGS)
_COMMON_LIBS = -lm -ljpeg -pthread -lrt

View File

@@ -31,7 +31,7 @@
#include <errno.h>
#include <assert.h>
#include "../libs/config.h"
#include "../libs/const.h"
#include "../libs/tools.h"
#include "../libs/logging.h"
#include "../libs/frame.h"

View File

@@ -23,7 +23,7 @@
#pragma once
#define VERSION_MAJOR 5
#define VERSION_MINOR 1
#define VERSION_MINOR 15
#define MAKE_VERSION2(_major, _minor) #_major "." #_minor
#define MAKE_VERSION1(_major, _minor) MAKE_VERSION2(_major, _minor)

View File

@@ -67,7 +67,7 @@
#define A_COND_INIT(_cond) assert(!pthread_cond_init(_cond, NULL))
#define A_COND_DESTROY(_cond) assert(!pthread_cond_destroy(_cond))
#define A_COND_SIGNAL(...) assert(!pthread_cond_signal(__VA_ARGS__))
#define A_COND_WAIT_TRUE(_var, _cond, _mutex) { while(!_var) assert(!pthread_cond_wait(_cond, _mutex)); }
#define A_COND_WAIT_TRUE(_var, _cond, _mutex) { while(!(_var)) assert(!pthread_cond_wait(_cond, _mutex)); }
#ifdef WITH_PTHREAD_NP

View File

@@ -28,15 +28,26 @@
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <locale.h>
#include <errno.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/file.h>
#ifdef NDEBUG
# error WTF dude? Asserts are good things!
#endif
#if CHAR_BIT != 8
# error There are not 8 bits in a char!
#endif
#define RN "\r\n"
#define INLINE inline __attribute__((always_inline))
@@ -116,13 +127,13 @@ INLINE uint64_t get_now_monotonic_u64(void) {
return (uint64_t)(ts.tv_nsec / 1000) + (uint64_t)ts.tv_sec * 1000000;
}
#undef X_CLOCK_MONOTONIC
INLINE uint64_t get_now_id(void) {
uint64_t now = get_now_monotonic_u64();
return (uint64_t)triple_u32(now) | ((uint64_t)triple_u32(now + 12345) << 32);
}
#undef X_CLOCK_MONOTONIC
INLINE long double get_now_real(void) {
time_t sec;
long msec;
@@ -136,6 +147,19 @@ INLINE unsigned get_cores_available(void) {
return max_u(min_u(cores_sysconf, 4), 1);
}
INLINE void ld_to_timespec(long double ld, struct timespec *ts) {
ts->tv_sec = (long)ld;
ts->tv_nsec = (ld - ts->tv_sec) * 1000000000L;
if (ts->tv_nsec > 999999999L) {
ts->tv_sec += 1;
ts->tv_nsec = 0;
}
}
INLINE long double timespec_to_ld(const struct timespec *ts) {
return ts->tv_sec + ((long double)ts->tv_nsec) / 1000000000;
}
INLINE int flock_timedwait_monotonic(int fd, long double timeout) {
long double deadline_ts = get_now_monotonic() + timeout;
int retval = -1;

View File

@@ -26,8 +26,7 @@
#include <sys/ioctl.h>
#include "../libs/tools.h"
#include "../libs/logging.h"
#include "tools.h"
#ifndef CFG_XIOCTL_RETRIES
@@ -51,10 +50,5 @@ INLINE int xioctl(int fd, int request, void *arg) {
|| errno == ETIMEDOUT
)
);
// cppcheck-suppress knownConditionTrueFalse
if (retval && retries <= 0) {
LOG_PERROR("ioctl(%d) retried %u times; giving up", request, XIOCTL_RETRIES);
}
return retval;
}

View File

@@ -24,7 +24,7 @@
#include <sys/types.h>
#include "../../libs/config.h"
#include "../../libs/const.h"
extern const char *const HTML_INDEX_PAGE;

View File

@@ -80,7 +80,8 @@ static const char *_standard_to_string(v4l2_std_id standard);
static const char *_io_method_to_string_supported(enum v4l2_memory io_method);
#define RUN(_next) dev->run->_next
#define RUN(_next) dev->run->_next
#define D_XIOCTL(...) xioctl(RUN(fd), __VA_ARGS__)
device_s *device_init(void) {
@@ -222,7 +223,7 @@ int device_export_to_dma(device_s *dev) {
exp.index = index;
LOG_DEBUG("Exporting device buffer=%u to DMA ...", index);
if (xioctl(RUN(fd), VIDIOC_EXPBUF, &exp) < 0) {
if (D_XIOCTL(VIDIOC_EXPBUF, &exp) < 0) {
LOG_PERROR("Can't export device buffer=%u to DMA", index);
goto error;
}
@@ -248,7 +249,7 @@ int device_switch_capturing(device_s *dev, bool enable) {
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
LOG_DEBUG("%s device capturing ...", (enable ? "Starting" : "Stopping"));
if (xioctl(RUN(fd), (enable ? VIDIOC_STREAMON : VIDIOC_STREAMOFF), &type) < 0) {
if (D_XIOCTL((enable ? VIDIOC_STREAMON : VIDIOC_STREAMOFF), &type) < 0) {
LOG_PERROR("Can't %s capturing", (enable ? "start" : "stop"));
if (enable) {
return -1;
@@ -315,7 +316,7 @@ int device_grab_buffer(device_s *dev, hw_buffer_s **hw) {
buf.memory = dev->io_method;
LOG_DEBUG("Grabbing device buffer ...");
if (xioctl(RUN(fd), VIDIOC_DQBUF, &buf) < 0) {
if (D_XIOCTL(VIDIOC_DQBUF, &buf) < 0) {
LOG_PERROR("Can't grab device buffer");
return -1;
}
@@ -336,7 +337,7 @@ int device_grab_buffer(device_s *dev, hw_buffer_s **hw) {
LOG_DEBUG("Dropped too small frame, assuming it was broken: buffer=%u, bytesused=%u",
buf.index, buf.bytesused);
LOG_DEBUG("Releasing device buffer=%u (broken frame) ...", buf.index);
if (xioctl(RUN(fd), VIDIOC_QBUF, &buf) < 0) {
if (D_XIOCTL(VIDIOC_QBUF, &buf) < 0) {
LOG_PERROR("Can't release device buffer=%u (broken frame)", buf.index);
return -1;
}
@@ -370,7 +371,7 @@ int device_release_buffer(device_s *dev, hw_buffer_s *hw) {
const unsigned index = hw->buf.index;
LOG_DEBUG("Releasing device buffer=%u ...", index);
if (xioctl(RUN(fd), VIDIOC_QBUF, &hw->buf) < 0) {
if (D_XIOCTL(VIDIOC_QBUF, &hw->buf) < 0) {
LOG_PERROR("Can't release device buffer=%u", index);
return -1;
}
@@ -382,7 +383,7 @@ int device_consume_event(device_s *dev) {
struct v4l2_event event;
LOG_DEBUG("Consuming V4L2 event ...");
if (xioctl(RUN(fd), VIDIOC_DQEVENT, &event) == 0) {
if (D_XIOCTL(VIDIOC_DQEVENT, &event) == 0) {
switch (event.type) {
case V4L2_EVENT_SOURCE_CHANGE:
LOG_INFO("Got V4L2_EVENT_SOURCE_CHANGE: source changed");
@@ -401,7 +402,7 @@ static int _device_open_check_cap(device_s *dev) {
struct v4l2_capability cap = {0};
LOG_DEBUG("Querying device capabilities ...");
if (xioctl(RUN(fd), VIDIOC_QUERYCAP, &cap) < 0) {
if (D_XIOCTL(VIDIOC_QUERYCAP, &cap) < 0) {
LOG_PERROR("Can't query device capabilities");
return -1;
}
@@ -418,14 +419,14 @@ static int _device_open_check_cap(device_s *dev) {
int input = dev->input; // Needs a pointer to int for ioctl()
LOG_INFO("Using input channel: %d", input);
if (xioctl(RUN(fd), VIDIOC_S_INPUT, &input) < 0) {
if (D_XIOCTL(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(RUN(fd), VIDIOC_S_STD, &dev->standard) < 0) {
if (D_XIOCTL(VIDIOC_S_STD, &dev->standard) < 0) {
LOG_ERROR("Can't set video standard");
return -1;
}
@@ -448,7 +449,7 @@ static int _device_open_dv_timings(device_s *dev) {
sub.type = V4L2_EVENT_SOURCE_CHANGE;
LOG_DEBUG("Subscribing to DV-timings events ...")
if (xioctl(RUN(fd), VIDIOC_SUBSCRIBE_EVENT, &sub) < 0) {
if (D_XIOCTL(VIDIOC_SUBSCRIBE_EVENT, &sub) < 0) {
LOG_PERROR("Can't subscribe to DV-timings events");
return -1;
}
@@ -459,8 +460,8 @@ static int _device_open_dv_timings(device_s *dev) {
static int _device_apply_dv_timings(device_s *dev) {
struct v4l2_dv_timings dv = {0};
LOG_DEBUG("Calling ioctl(VIDIOC_QUERY_DV_TIMINGS) ...");
if (xioctl(RUN(fd), VIDIOC_QUERY_DV_TIMINGS, &dv) == 0) {
LOG_DEBUG("Calling xioctl(VIDIOC_QUERY_DV_TIMINGS) ...");
if (D_XIOCTL(VIDIOC_QUERY_DV_TIMINGS, &dv) == 0) {
if (dv.type == V4L2_DV_BT_656_1120) {
// See v4l2_print_dv_timings() in the kernel
unsigned htot = V4L2_DV_BT_FRAME_WIDTH(&dv.bt);
@@ -478,8 +479,8 @@ static int _device_apply_dv_timings(device_s *dev) {
(unsigned long long)dv.bt.pixelclock, dv.bt.vsync, dv.bt.hsync);
}
LOG_DEBUG("Calling ioctl(VIDIOC_S_DV_TIMINGS) ...");
if (xioctl(RUN(fd), VIDIOC_S_DV_TIMINGS, &dv) < 0) {
LOG_DEBUG("Calling xioctl(VIDIOC_S_DV_TIMINGS) ...");
if (D_XIOCTL(VIDIOC_S_DV_TIMINGS, &dv) < 0) {
LOG_PERROR("Failed to set DV-timings");
return -1;
}
@@ -489,10 +490,10 @@ static int _device_apply_dv_timings(device_s *dev) {
}
} else {
LOG_DEBUG("Calling ioctl(VIDIOC_QUERYSTD) ...");
if (xioctl(RUN(fd), VIDIOC_QUERYSTD, &dev->standard) == 0) {
LOG_DEBUG("Calling xioctl(VIDIOC_QUERYSTD) ...");
if (D_XIOCTL(VIDIOC_QUERYSTD, &dev->standard) == 0) {
LOG_INFO("Applying the new VIDIOC_S_STD: %s ...", _standard_to_string(dev->standard));
if (xioctl(RUN(fd), VIDIOC_S_STD, &dev->standard) < 0) {
if (D_XIOCTL(VIDIOC_S_STD, &dev->standard) < 0) {
LOG_PERROR("Can't set video standard");
return -1;
}
@@ -515,7 +516,7 @@ static int _device_open_format(device_s *dev, bool first) {
// Set format
LOG_DEBUG("Probing device format=%s, stride=%u, resolution=%ux%u ...",
_format_to_string_supported(dev->format), stride, RUN(width), RUN(height));
if (xioctl(RUN(fd), VIDIOC_S_FMT, &fmt) < 0) {
if (D_XIOCTL(VIDIOC_S_FMT, &fmt) < 0) {
LOG_PERROR("Can't set device format");
return -1;
}
@@ -566,7 +567,7 @@ static void _device_open_hw_fps(device_s *dev) {
setfps.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
LOG_DEBUG("Querying HW FPS ...");
if (xioctl(RUN(fd), VIDIOC_G_PARM, &setfps) < 0) {
if (D_XIOCTL(VIDIOC_G_PARM, &setfps) < 0) {
if (errno == ENOTTY) { // Quiet message for TC358743
LOG_INFO("Querying HW FPS changing is not supported");
} else {
@@ -587,7 +588,7 @@ static void _device_open_hw_fps(device_s *dev) {
SETFPS_TPF(numerator) = 1;
SETFPS_TPF(denominator) = (dev->desired_fps == 0 ? 255 : dev->desired_fps);
if (xioctl(RUN(fd), VIDIOC_S_PARM, &setfps) < 0) {
if (D_XIOCTL(VIDIOC_S_PARM, &setfps) < 0) {
LOG_PERROR("Can't set HW FPS");
return;
}
@@ -618,11 +619,11 @@ static void _device_open_jpeg_quality(device_s *dev) {
if (is_jpeg(RUN(format))) {
struct v4l2_jpegcompression comp = {0};
if (xioctl(RUN(fd), VIDIOC_G_JPEGCOMP, &comp) < 0) {
if (D_XIOCTL(VIDIOC_G_JPEGCOMP, &comp) < 0) {
LOG_ERROR("Device doesn't support setting of HW encoding quality parameters");
} else {
comp.quality = dev->jpeg_quality;
if (xioctl(RUN(fd), VIDIOC_S_JPEGCOMP, &comp) < 0) {
if (D_XIOCTL(VIDIOC_S_JPEGCOMP, &comp) < 0) {
LOG_ERROR("Can't change MJPEG quality for JPEG source with HW pass-through encoder");
} else {
quality = dev->jpeg_quality;
@@ -650,7 +651,7 @@ static int _device_open_io_method_mmap(device_s *dev) {
req.memory = V4L2_MEMORY_MMAP;
LOG_DEBUG("Requesting %u device buffers for MMAP ...", req.count);
if (xioctl(RUN(fd), VIDIOC_REQBUFS, &req) < 0) {
if (D_XIOCTL(VIDIOC_REQBUFS, &req) < 0) {
LOG_PERROR("Device '%s' doesn't support MMAP method", dev->path);
return -1;
}
@@ -671,8 +672,8 @@ static int _device_open_io_method_mmap(device_s *dev) {
buf.memory = V4L2_MEMORY_MMAP;
buf.index = RUN(n_bufs);
LOG_DEBUG("Calling ioctl(VIDIOC_QUERYBUF) for device buffer=%u ...", RUN(n_bufs));
if (xioctl(RUN(fd), VIDIOC_QUERYBUF, &buf) < 0) {
LOG_DEBUG("Calling xioctl(VIDIOC_QUERYBUF) for device buffer=%u ...", RUN(n_bufs));
if (D_XIOCTL(VIDIOC_QUERYBUF, &buf) < 0) {
LOG_PERROR("Can't VIDIOC_QUERYBUF");
return -1;
}
@@ -707,7 +708,7 @@ static int _device_open_io_method_userptr(device_s *dev) {
req.memory = V4L2_MEMORY_USERPTR;
LOG_DEBUG("Requesting %u device buffers for USERPTR ...", req.count);
if (xioctl(RUN(fd), VIDIOC_REQBUFS, &req) < 0) {
if (D_XIOCTL(VIDIOC_REQBUFS, &req) < 0) {
LOG_PERROR("Device '%s' doesn't support USERPTR method", dev->path);
return -1;
}
@@ -747,8 +748,8 @@ static int _device_open_queue_buffers(device_s *dev) {
buf.length = RUN(hw_bufs)[index].raw.allocated;
}
LOG_DEBUG("Calling ioctl(VIDIOC_QBUF) for buffer=%u ...", index);
if (xioctl(RUN(fd), VIDIOC_QBUF, &buf) < 0) {
LOG_DEBUG("Calling xioctl(VIDIOC_QBUF) for buffer=%u ...", index);
if (D_XIOCTL(VIDIOC_QBUF, &buf) < 0) {
LOG_PERROR("Can't VIDIOC_QBUF");
return -1;
}
@@ -836,7 +837,7 @@ static int _device_query_control(
MEMSET_ZERO(*query);
query->id = cid;
if (xioctl(RUN(fd), VIDIOC_QUERYCTRL, query) < 0 || query->flags & V4L2_CTRL_FLAG_DISABLED) {
if (D_XIOCTL(VIDIOC_QUERYCTRL, query) < 0 || query->flags & V4L2_CTRL_FLAG_DISABLED) {
if (!quiet) {
LOG_ERROR("Changing control %s is unsupported", name);
}
@@ -861,7 +862,7 @@ static void _device_set_control(
ctl.id = cid;
ctl.value = value;
if (xioctl(RUN(fd), VIDIOC_S_CTRL, &ctl) < 0) {
if (D_XIOCTL(VIDIOC_S_CTRL, &ctl) < 0) {
if (!quiet) {
LOG_PERROR("Can't set control %s", name);
}
@@ -902,4 +903,5 @@ static const char *_io_method_to_string_supported(enum v4l2_memory io_method) {
return "unsupported";
}
# undef RUN
#undef D_XIOCTL
#undef RUN

View File

@@ -45,8 +45,7 @@
#include "../libs/logging.h"
#include "../libs/threading.h"
#include "../libs/frame.h"
#include "xioctl.h"
#include "../libs/xioctl.h"
#define VIDEO_MIN_WIDTH ((unsigned)160)

View File

@@ -224,8 +224,9 @@ static bool _worker_run_job(worker_s *wr) {
} else if (ER(type) == ENCODER_TYPE_NOOP) {
LOG_VERBOSE("Compressing JPEG using NOOP (do nothing): worker=%s, buffer=%u",
wr->name, job->hw->buf.index);
frame_encoding_begin(src, dest, V4L2_PIX_FMT_JPEG);
usleep(5000); // Просто чтобы работала логика desired_fps
dest->encode_end_ts = get_now_monotonic();
dest->encode_end_ts = get_now_monotonic(); // frame_encoding_end()
}
LOG_VERBOSE("Compressed new JPEG: size=%zu, time=%0.3Lf, worker=%s, buffer=%u",

View File

@@ -76,7 +76,7 @@ server_s *server_init(stream_s *stream) {
assert(!evthread_use_pthreads());
assert((run->base = event_base_new()));
assert((run->http = evhttp_new(run->base)));
evhttp_set_allowed_methods(run->http, EVHTTP_REQ_GET|EVHTTP_REQ_HEAD);
evhttp_set_allowed_methods(run->http, EVHTTP_REQ_GET|EVHTTP_REQ_HEAD|EVHTTP_REQ_OPTIONS);
return server;
}
@@ -212,14 +212,36 @@ void server_loop_break(server_s *server) {
event_base_loopbreak(RUN(base));
}
#define GET_HEADER(_key) \
evhttp_find_header(evhttp_request_get_input_headers(request), _key)
#define ADD_HEADER(_key, _value) \
assert(!evhttp_add_header(evhttp_request_get_output_headers(request), _key, _value))
static int _http_preprocess_request(struct evhttp_request *request, server_s *server) {
RUN(last_request_ts) = get_now_monotonic();
if (server->allow_origin[0] != '\0') {
const char *cors_headers = GET_HEADER("Access-Control-Request-Headers");
const char *cors_method = GET_HEADER("Access-Control-Request-Method");
ADD_HEADER("Access-Control-Allow-Origin", server->allow_origin);
ADD_HEADER("Access-Control-Allow-Credentials", "true");
if (cors_headers != NULL) {
ADD_HEADER("Access-Control-Allow-Headers", cors_headers);
}
if (cors_method != NULL) {
ADD_HEADER("Access-Control-Allow-Methods", cors_method);
}
}
if (evhttp_request_get_command(request) == EVHTTP_REQ_OPTIONS) {
evhttp_send_reply(request, HTTP_OK, "OK", NULL);
return -1;
}
if (RUN(auth_token)) {
const char *token = evhttp_find_header(evhttp_request_get_input_headers(request), "Authorization");
const char *token = GET_HEADER("Authorization");
if (token == NULL || strcmp(token, RUN(auth_token)) != 0) {
ADD_HEADER("WWW-Authenticate", "Basic realm=\"Restricted area\"");
@@ -228,8 +250,8 @@ static int _http_preprocess_request(struct evhttp_request *request, server_s *se
}
}
if (evhttp_request_get_command(request) == EVHTTP_REQ_HEAD) { \
evhttp_send_reply(request, HTTP_OK, "OK", NULL); \
if (evhttp_request_get_command(request) == EVHTTP_REQ_HEAD) {
evhttp_send_reply(request, HTTP_OK, "OK", NULL);
return -1;
}
@@ -464,9 +486,6 @@ static void _http_callback_snapshot(struct evhttp_request *request, void *v_serv
assert((buf = evbuffer_new()));
assert(!evbuffer_add(buf, (const void *)EX(frame->data), EX(frame->used)));
if (server->allow_origin[0] != '\0') {
ADD_HEADER("Access-Control-Allow-Origin", server->allow_origin);
}
ADD_HEADER("Cache-Control", "no-store, no-cache, must-revalidate, proxy-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");
@@ -580,6 +599,7 @@ static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_c
# define BOUNDARY "boundarydonotcross"
stream_client_s *client = (stream_client_s *)v_client;
struct evhttp_request *request = client->request; // for GET_HEADER
server_s *server = client->server;
long double now = get_now_monotonic();
@@ -620,9 +640,24 @@ static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_c
if (client->need_initial) {
assert(evbuffer_add_printf(buf, "HTTP/1.0 200 OK" RN));
if (client->server->allow_origin[0] != '\0') {
assert(evbuffer_add_printf(buf, "Access-Control-Allow-Origin: %s" RN, client->server->allow_origin));
const char *cors_headers = GET_HEADER("Access-Control-Request-Headers");
const char *cors_method = GET_HEADER("Access-Control-Request-Method");
assert(evbuffer_add_printf(buf,
"Access-Control-Allow-Origin: %s" RN
"Access-Control-Allow-Credentials: true" RN,
client->server->allow_origin
));
if (cors_headers != NULL) {
assert(evbuffer_add_printf(buf, "Access-Control-Allow-Headers: %s" RN, cors_headers));
}
if (cors_method != NULL) {
assert(evbuffer_add_printf(buf, "Access-Control-Allow-Methods: %s" RN, cors_method));
}
}
assert(evbuffer_add_printf(buf,
"Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, pre-check=0, post-check=0, max-age=0" RN
"Pragma: no-cache" RN
@@ -895,7 +930,7 @@ static char *_http_get_client_hostport(struct evhttp_request *request) {
assert(addr = strdup(peer));
}
const char *xff = evhttp_find_header(evhttp_request_get_input_headers(request), "X-Forwarded-For");
const char *xff = GET_HEADER("X-Forwarded-For");
if (xff) {
if (addr) {
free(addr);
@@ -918,3 +953,5 @@ static char *_http_get_client_hostport(struct evhttp_request *request) {
free(addr);
return hostport;
}
#undef GET_HEADER

View File

@@ -25,9 +25,9 @@
static m2m_encoder_s *_m2m_encoder_init(
const char *name, const char *path, unsigned output_format,
unsigned fps, bool allow_dma, m2m_option_s *options);
unsigned fps, unsigned bitrate, unsigned gop, unsigned quality, bool allow_dma);
static int _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame);
static void _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame);
static int _m2m_encoder_init_buffers(
m2m_encoder_s *enc, const char *name, enum v4l2_buf_type type,
@@ -46,26 +46,11 @@ static int _m2m_encoder_compress_raw(m2m_encoder_s *enc, const frame_s *src, fra
m2m_encoder_s *m2m_h264_encoder_init(const char *name, const char *path, unsigned bitrate, unsigned gop) {
# define OPTION(_key, _value) {#_key, V4L2_CID_MPEG_VIDEO_##_key, _value}
m2m_option_s options[] = {
OPTION(BITRATE, bitrate * 1000),
// OPTION(BITRATE_PEAK, bitrate * 1000),
OPTION(H264_I_PERIOD, gop),
OPTION(H264_PROFILE, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE),
OPTION(H264_LEVEL, V4L2_MPEG_VIDEO_H264_LEVEL_4_0),
OPTION(REPEAT_SEQ_HEADER, 1),
OPTION(H264_MIN_QP, 16),
OPTION(H264_MAX_QP, 32),
{NULL, 0, 0},
};
# undef OPTION
// FIXME: 30 or 0? https://github.com/6by9/yavta/blob/master/yavta.c#L2100
// По логике вещей правильно 0, но почему-то на низких разрешениях типа 640x480
// енкодер через несколько секунд перестает производить корректные фреймы.
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_H264, 30, true, options);
bitrate *= 1000; // From Kbps
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_H264, 30, bitrate, gop, 0, true);
}
m2m_encoder_s *m2m_mjpeg_encoder_init(const char *name, const char *path, unsigned quality) {
@@ -76,30 +61,18 @@ m2m_encoder_s *m2m_mjpeg_encoder_init(const char *name, const char *path, unsign
bitrate = step * round(bitrate / step);
bitrate *= 1000; // From Kbps
assert(bitrate > 0);
m2m_option_s options[] = {
{"BITRATE", V4L2_CID_MPEG_VIDEO_BITRATE, bitrate},
{NULL, 0, 0},
};
// FIXME: То же самое про 30 or 0, но еще даже не проверено на низких разрешениях
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_MJPEG, 30, true, options);
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_MJPEG, 30, bitrate, 0, 0, true);
}
m2m_encoder_s *m2m_jpeg_encoder_init(const char *name, const char *path, unsigned quality) {
m2m_option_s options[] = {
{"QUALITY", V4L2_CID_JPEG_COMPRESSION_QUALITY, quality},
{NULL, 0, 0},
};
// FIXME: DMA не работает
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_JPEG, 30, false, options);
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_JPEG, 30, 0, 0, quality, false);
}
void m2m_encoder_destroy(m2m_encoder_s *enc) {
E_LOG_INFO("Destroying encoder ...");
_m2m_encoder_cleanup(enc);
free(enc->options);
free(enc->path);
free(enc->name);
free(enc);
@@ -117,9 +90,10 @@ int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, frame_s *dest,
|| RUN(stride) != src->stride
|| RUN(dma) != (enc->allow_dma && src->dma_fd >= 0)
) {
if (_m2m_encoder_prepare(enc, src) < 0) {
return -1;
}
_m2m_encoder_prepare(enc, src);
}
if (!RUN(ready)) { // Already prepared but failed
return -1;
}
force_key = (enc->output_format == V4L2_PIX_FMT_H264 && (force_key || RUN(last_online) != src->online));
@@ -141,7 +115,7 @@ int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, frame_s *dest,
static m2m_encoder_s *_m2m_encoder_init(
const char *name, const char *path, unsigned output_format,
unsigned fps, bool allow_dma, m2m_option_s *options) {
unsigned fps, unsigned bitrate, unsigned gop, unsigned quality, bool allow_dma) {
LOG_INFO("%s: Initializing encoder ...", name);
@@ -160,15 +134,11 @@ static m2m_encoder_s *_m2m_encoder_init(
}
enc->output_format = output_format;
enc->fps = fps;
enc->bitrate = bitrate;
enc->gop = gop;
enc->quality = quality;
enc->allow_dma = allow_dma;
enc->run = run;
unsigned count = 0;
for (; options[count].name != NULL; ++count);
++count;
A_CALLOC(enc->options, count);
memcpy(enc->options, options, sizeof(m2m_option_s) * count);
return enc;
}
@@ -179,7 +149,7 @@ static m2m_encoder_s *_m2m_encoder_init(
} \
}
static int _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame) {
static void _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame) {
bool dma = (enc->allow_dma && frame->dma_fd >= 0);
E_LOG_INFO("Configuring encoder: DMA=%d ...", dma);
@@ -198,15 +168,34 @@ static int _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame) {
}
E_LOG_DEBUG("Encoder device fd=%d opened", RUN(fd));
for (m2m_option_s *option = enc->options; option->name != NULL; ++option) {
struct v4l2_control ctl = {0};
ctl.id = option->id;
ctl.value = option->value;
# define SET_OPTION(_cid, _value) { \
struct v4l2_control _ctl = {0}; \
_ctl.id = _cid; \
_ctl.value = _value; \
E_LOG_DEBUG("Configuring option " #_cid " ..."); \
E_XIOCTL(VIDIOC_S_CTRL, &_ctl, "Can't set option " #_cid); \
}
E_LOG_DEBUG("Configuring option %s ...", option->name);
E_XIOCTL(VIDIOC_S_CTRL, &ctl, "Can't set option %s", option->name);
if (enc->output_format == V4L2_PIX_FMT_H264) {
SET_OPTION(V4L2_CID_MPEG_VIDEO_BITRATE, enc->bitrate);
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, enc->gop);
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_PROFILE, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE);
if (RUN(width) * RUN(height) <= 1920 * 1080) { // https://forums.raspberrypi.com/viewtopic.php?t=291447#p1762296
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_LEVEL, V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
} else {
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_LEVEL, V4L2_MPEG_VIDEO_H264_LEVEL_5_1);
}
SET_OPTION(V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, 1);
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 16);
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 32);
} else if (enc->output_format == V4L2_PIX_FMT_MJPEG) {
SET_OPTION(V4L2_CID_MPEG_VIDEO_BITRATE, enc->bitrate);
} else if (enc->output_format == V4L2_PIX_FMT_JPEG) {
SET_OPTION(V4L2_CID_JPEG_COMPRESSION_QUALITY, enc->quality);
}
# undef SET_OPTION
{
struct v4l2_format fmt = {0};
fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
@@ -273,12 +262,11 @@ static int _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame) {
RUN(ready) = true;
E_LOG_DEBUG("Encoder state: *** READY ***");
return 0;
return;
error:
_m2m_encoder_cleanup(enc);
E_LOG_ERROR("Encoder destroyed due an error (prepare)");
return -1;
}
static int _m2m_encoder_init_buffers(

View File

@@ -39,8 +39,7 @@
#include "../libs/tools.h"
#include "../libs/logging.h"
#include "../libs/frame.h"
#include "xioctl.h"
#include "../libs/xioctl.h"
typedef struct {
@@ -48,12 +47,6 @@ typedef struct {
size_t allocated;
} m2m_buffer_s;
typedef struct {
char *name;
uint32_t id;
int32_t value;
} m2m_option_s;
typedef struct {
int fd;
m2m_buffer_s *input_bufs;
@@ -76,8 +69,10 @@ typedef struct {
char *path;
unsigned output_format;
unsigned fps;
unsigned bitrate;
unsigned gop;
unsigned quality;
bool allow_dma;
m2m_option_s *options;
m2m_encoder_runtime_s *run;
} m2m_encoder_s;

View File

@@ -20,16 +20,6 @@
*****************************************************************************/
#include <assert.h>
#ifdef NDEBUG
# error WTF dude? Asserts are good things!
#endif
#include <limits.h>
#if CHAR_BIT != 8
# error There are not 8 bits in a char!
#endif
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>

View File

@@ -32,7 +32,7 @@
#include <errno.h>
#include <assert.h>
#include "../libs/config.h"
#include "../libs/const.h"
#include "../libs/logging.h"
#include "../libs/process.h"
#include "../libs/frame.h"