install go2rtc on bob
This commit is contained in:
@@ -0,0 +1,24 @@
|
|||||||
|
.idea/
|
||||||
|
.tmp/
|
||||||
|
|
||||||
|
go2rtc.yaml
|
||||||
|
go2rtc.json
|
||||||
|
|
||||||
|
go2rtc_freebsd*
|
||||||
|
go2rtc_linux*
|
||||||
|
go2rtc_mac*
|
||||||
|
go2rtc_win*
|
||||||
|
|
||||||
|
/go2rtc
|
||||||
|
/go2rtc.exe
|
||||||
|
|
||||||
|
0_test.go
|
||||||
|
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
website/.vitepress/cache
|
||||||
|
website/.vitepress/dist
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
package-lock.json
|
||||||
|
CLAUDE.md
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2022 Alexey Khit
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -0,0 +1,538 @@
|
|||||||
|
<h1 align="center">
|
||||||
|
<a href="https://github.com/AlexxIT/go2rtc">
|
||||||
|
<img src="./website/images/logo.gif" alt="go2rtc - GitHub">
|
||||||
|
</a>
|
||||||
|
</h1>
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://github.com/AlexxIT/go2rtc/stargazers" target="_blank">
|
||||||
|
<img style="display: inline" src="https://img.shields.io/github/stars/AlexxIT/go2rtc?style=flat-square&logo=github" alt="go2rtc - GitHub Stars">
|
||||||
|
</a>
|
||||||
|
<a href="https://hub.docker.com/r/alexxit/go2rtc" target="_blank">
|
||||||
|
<img style="display: inline" src="https://img.shields.io/docker/pulls/alexxit/go2rtc?style=flat-square&logo=docker&logoColor=white&label=pulls" alt="go2rtc - Docker Pulls">
|
||||||
|
</a>
|
||||||
|
<a href="https://github.com/AlexxIT/go2rtc/releases" target="_blank">
|
||||||
|
<img style="display: inline" src="https://img.shields.io/github/downloads/AlexxIT/go2rtc/total?color=blue&style=flat-square&logo=github" alt="go2rtc - GitHub Downloads">
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://trendshift.io/repositories/4628" target="_blank">
|
||||||
|
<img src="https://trendshift.io/api/badge/repositories/4628" alt="go2rtc - Trendshift"/>
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
Ultimate camera streaming application with support for dozens formats and protocols.
|
||||||
|
|
||||||
|
- zero-dependency [small app](#go2rtc-binary) for all OS (Windows, macOS, Linux, FreeBSD)
|
||||||
|
- zero-delay for many [supported protocols](#codecs-madness) (lowest possible streaming latency)
|
||||||
|
- [streaming input](#streaming-input) from dozens formats and protocols
|
||||||
|
- [streaming output](#streaming-output) in all popular formats
|
||||||
|
- [streaming ingest](#streaming-ingest) in a number of popular formats
|
||||||
|
- [publish](#publish-stream) any source to popular streaming services (YouTube, Telegram)
|
||||||
|
- on-the-fly transcoding only if necessary via [FFmpeg](internal/ffmpeg/README.md)
|
||||||
|
- [two-way audio](#two-way-audio) support for many formats
|
||||||
|
- [streaming audio](#stream-to-camera) to all cameras with [two-way audio](#two-way-audio) support
|
||||||
|
- mixing tracks from different sources to single stream
|
||||||
|
- [auto-match](www/README.md#javascript-api) client-supported streaming formats and codecs
|
||||||
|
- [streaming stats](#streaming-stats) for all active connections
|
||||||
|
- can be [integrated to any project](#projects-using-go2rtc) or be used as [standalone app](#go2rtc-binary)
|
||||||
|
|
||||||
|
#### Inspired by
|
||||||
|
|
||||||
|
- series of streaming projects from [@deepch](https://github.com/deepch)
|
||||||
|
- [webrtc](https://github.com/pion/webrtc) go library and whole [@pion](https://github.com/pion) team
|
||||||
|
- [rtsp-simple-server](https://github.com/aler9/rtsp-simple-server) idea from [@aler9](https://github.com/aler9)
|
||||||
|
- [GStreamer](https://gstreamer.freedesktop.org/) framework pipeline idea
|
||||||
|
- [MediaSoup](https://mediasoup.org/) framework routing idea
|
||||||
|
- HomeKit Accessory Protocol from [@brutella](https://github.com/brutella/hap)
|
||||||
|
- creator of the project's logo [@v_novoseltsev](https://www.instagram.com/v_novoseltsev)
|
||||||
|
|
||||||
|
<br>
|
||||||
|
<details>
|
||||||
|
<summary><b>Table of Contents</b></summary>
|
||||||
|
|
||||||
|
- [Installation](#installation)
|
||||||
|
- [go2rtc: Binary](#go2rtc-binary)
|
||||||
|
- [go2rtc: Docker](#go2rtc-docker)
|
||||||
|
- [go2rtc: Home Assistant add-on](#go2rtc-home-assistant-add-on)
|
||||||
|
- [go2rtc: Home Assistant Integration](#go2rtc-home-assistant-integration)
|
||||||
|
- [go2rtc: Master version](#go2rtc-master-version)
|
||||||
|
- [Configuration](#configuration)
|
||||||
|
- [Features](#features)
|
||||||
|
- [Streaming input](#streaming-input)
|
||||||
|
- [Streaming output](#streaming-output)
|
||||||
|
- [Streaming ingest](#streaming-ingest)
|
||||||
|
- [Two-way audio](#two-way-audio)
|
||||||
|
- [Stream to camera](#stream-to-camera)
|
||||||
|
- [Publish stream](#publish-stream)
|
||||||
|
- [Preload stream](#preload-stream)
|
||||||
|
- [Streaming stats](#streaming-stats)
|
||||||
|
- [Codecs](#codecs)
|
||||||
|
- [Codecs filters](#codecs-filters)
|
||||||
|
- [Codecs madness](#codecs-madness)
|
||||||
|
- [Built-in transcoding](#built-in-transcoding)
|
||||||
|
- [Codecs negotiation](#codecs-negotiation)
|
||||||
|
- [Security](#security)
|
||||||
|
- [Projects using go2rtc](#projects-using-go2rtc)
|
||||||
|
- [Camera experience](#camera-experience)
|
||||||
|
- [Tips](#tips)
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
1. Download [binary](#go2rtc-binary) or use [Docker](#go2rtc-docker) or Home Assistant [add-on](#go2rtc-home-assistant-add-on) or [integration](#go2rtc-home-assistant-integration)
|
||||||
|
2. Open web interface: `http://localhost:1984/`
|
||||||
|
3. Add [streams](#streaming-input) to [config](#configuration)
|
||||||
|
|
||||||
|
**Developers:** integrate [HTTP API](internal/api/README.md) into your smart home platform.
|
||||||
|
|
||||||
|
### go2rtc: Binary
|
||||||
|
|
||||||
|
Download binary for your OS from [latest release](https://github.com/AlexxIT/go2rtc/releases/):
|
||||||
|
|
||||||
|
| name | description |
|
||||||
|
|-----------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------|
|
||||||
|
| [go2rtc_win64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_win64.zip) | Windows 10+ 64-bit |
|
||||||
|
| [go2rtc_win32.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_win32.zip) | Windows 10+ 32-bit |
|
||||||
|
| [go2rtc_win_arm64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_win_arm64.zip) | Windows ARM 64-bit |
|
||||||
|
| [go2rtc_linux_amd64](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_amd64) | Linux 64-bit |
|
||||||
|
| [go2rtc_linux_i386](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_i386) | Linux 32-bit |
|
||||||
|
| [go2rtc_linux_arm64](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_arm64) | Linux ARM 64-bit (ex. Raspberry 64-bit OS) |
|
||||||
|
| [go2rtc_linux_arm](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_arm) | Linux ARM 32-bit (ex. Raspberry 32-bit OS) |
|
||||||
|
| [go2rtc_linux_armv6](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_armv6) | Linux ARMv6 (for old Raspberry 1 and Zero) |
|
||||||
|
| [go2rtc_linux_mipsel](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_mipsel) | Linux MIPS (ex. [Xiaomi Gateway 3](https://github.com/AlexxIT/XiaomiGateway3), [Wyze cameras](https://github.com/gtxaspec/wz_mini_hacks)) |
|
||||||
|
| [go2rtc_mac_amd64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_mac_amd64.zip) | macOS 11+ Intel 64-bit |
|
||||||
|
| [go2rtc_mac_arm64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_mac_arm64.zip) | macOS ARM 64-bit |
|
||||||
|
| [go2rtc_freebsd_amd64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_freebsd_amd64.zip) | FreeBSD 64-bit |
|
||||||
|
| [go2rtc_freebsd_arm64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_freebsd_arm64.zip) | FreeBSD ARM 64-bit |
|
||||||
|
|
||||||
|
Don't forget to fix the rights `chmod +x go2rtc_xxx_xxx` on Linux and Mac.
|
||||||
|
|
||||||
|
PS. The application is compiled with the latest versions of the Go language for maximum speed and security. Therefore, the [minimum OS versions](https://go.dev/wiki/MinimumRequirements) depend on the Go language.
|
||||||
|
|
||||||
|
### go2rtc: Docker
|
||||||
|
|
||||||
|
The Docker containers [`alexxit/go2rtc`](https://hub.docker.com/r/alexxit/go2rtc) and [`ghcr.io/alexxit/go2rtc`](https://github.com/AlexxIT/go2rtc/pkgs/container/go2rtc) support multiple architectures including `386`, `amd64`, `arm/v6`, `arm/v7` and `arm64`.
|
||||||
|
These containers offer the same functionality as the Home Assistant [add-on](#go2rtc-home-assistant-add-on) but are designed to operate independently of Home Assistant.
|
||||||
|
It comes preinstalled with [FFmpeg](internal/ffmpeg/README.md) and [Python](internal/echo/README.md).
|
||||||
|
|
||||||
|
### go2rtc: Home Assistant add-on
|
||||||
|
|
||||||
|
[](https://my.home-assistant.io/redirect/supervisor_add_addon_repository/?repository_url=https%3A%2F%2Fgithub.com%2FAlexxIT%2Fhassio-addons)
|
||||||
|
|
||||||
|
1. Settings > Add-ons > Plus > Repositories > Add
|
||||||
|
```
|
||||||
|
https://github.com/AlexxIT/hassio-addons
|
||||||
|
```
|
||||||
|
2. go2rtc > Install > Start
|
||||||
|
|
||||||
|
### go2rtc: Home Assistant Integration
|
||||||
|
|
||||||
|
[WebRTC Camera](https://github.com/AlexxIT/WebRTC) custom component can be used on any Home Assistant [installation](https://www.home-assistant.io/installation/), including [HassWP](https://github.com/AlexxIT/HassWP) on Windows. It can automatically download and use the latest version of go2rtc. Or it can connect to an existing version of go2rtc. Addon installation in this case is optional.
|
||||||
|
|
||||||
|
### go2rtc: Master version
|
||||||
|
|
||||||
|
Latest, but maybe unstable version:
|
||||||
|
|
||||||
|
- Binary: [latest master build](https://nightly.link/AlexxIT/go2rtc/workflows/build/master)
|
||||||
|
- Docker: `alexxit/go2rtc:master` or `alexxit/go2rtc:master-hardware` versions
|
||||||
|
- Home Assistant add-on: `go2rtc master` or `go2rtc master hardware` versions
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
This is the `go2rtc.yaml` file in [YAML-format](https://en.wikipedia.org/wiki/YAML).
|
||||||
|
The configuration can be changed in the [WebUI](www/README.md) at `http://localhost:1984`.
|
||||||
|
The editor provides syntax highlighting and checking.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
The simplest config looks like this:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
hall-camera: rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0
|
||||||
|
```
|
||||||
|
|
||||||
|
- by default go2rtc will search `go2rtc.yaml` in the current work directory
|
||||||
|
- `api` server will start on default **1984 port** (TCP)
|
||||||
|
- `rtsp` server will start on default **8554 port** (TCP)
|
||||||
|
- `webrtc` will use port **8555** (TCP/UDP) for connections
|
||||||
|
|
||||||
|
More information can be [found here](internal/app/README.md).
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
A summary table of all modules and features can be found [here](internal/README.md).
|
||||||
|
|
||||||
|
**Core modules**
|
||||||
|
|
||||||
|
- [`app`](internal/app/README.md) - Reading [configs](internal/app/README.md) and setting up [logs](internal/app/README.md#log).
|
||||||
|
- [`api`](internal/api/README.md) - Handle [HTTP](internal/api/README.md) and [WebSocket](internal/api/ws/README.md) API.
|
||||||
|
- [`streams`](internal/streams/README.md) - Handle a list of streams.
|
||||||
|
|
||||||
|
### Streaming input
|
||||||
|
|
||||||
|
#### public protocols
|
||||||
|
|
||||||
|
- [`mpjpeg`](internal/mjpeg/README.md#mjpeg-client) - The legacy but still used [MJPEG](https://en.wikipedia.org/wiki/Motion_JPEG) protocol for real-time media transmission.
|
||||||
|
- [`onvif`](internal/onvif/README.md#onvif-client) - A popular [ONVIF](https://en.wikipedia.org/wiki/ONVIF) protocol for receiving media in RTSP format.
|
||||||
|
- [`rtmp`](internal/rtmp/README.md#rtmp-client) - The legacy but still used [RTMP](https://en.wikipedia.org/wiki/Real-Time_Messaging_Protocol) protocol for real-time media transmission.
|
||||||
|
- [`rtsp`](internal/rtsp/README.md#rtsp-client) - The most common [RTSP](https://en.wikipedia.org/wiki/Real-Time_Streaming_Protocol) protocol for real-time media transmission.
|
||||||
|
- [`webrtc`](internal/webrtc/README.md#webrtc-client) - [WebRTC](https://en.wikipedia.org/wiki/WebRTC) web-compatible protocol for real-time media transmission.
|
||||||
|
- [`yuv4mpegpipe`](internal/http/README.md#tcp) - Raw [YUV](https://en.wikipedia.org/wiki/Y%E2%80%B2UV) frame stream with [YUV4MPEG](https://manned.org/yuv4mpeg) header.
|
||||||
|
|
||||||
|
#### private protocols
|
||||||
|
|
||||||
|
- [`bubble`](internal/bubble/README.md) - Some NVR from [dvr163.com](http://help.dvr163.com/) and [eseecloud.com](http://www.eseecloud.com/).
|
||||||
|
- [`doorbird`](internal/doorbird/README.md) - [Doorbird](https://www.doorbird.com/) devices with two-way audio.
|
||||||
|
- [`dvrip`](internal/dvrip/README.md) - DVR-IP NVR, NetSurveillance, Sofia protocol (XMeye SDK).
|
||||||
|
- [`eseecloud`](internal/eseecloud/README.md) - Some NVR from [dvr163.com](http://help.dvr163.com/) and [eseecloud.com](http://www.eseecloud.com/).
|
||||||
|
- [`gopro`](internal/gopro/README.md) - [GoPro](https://gopro.com/) cameras, connected via USB or Wi-Fi.
|
||||||
|
- [`hass`](internal/hass/README.md) - Import cameras from [Home Assistant](https://www.home-assistant.io/) config files.
|
||||||
|
- [`homekit`](internal/homekit/README.md) - Cameras with [Apple HomeKit](https://www.apple.com/home-app/accessories/) protocol.
|
||||||
|
- [`isapi`](internal/isapi/README.md) - Two-way audio for [Hikvision ISAPI](https://tpp.hikvision.com/download/ISAPI_OTAP) protocol.
|
||||||
|
- [`kasa`](internal/kasa/README.md) - [TP-Link Kasa](https://www.kasasmart.com/) cameras.
|
||||||
|
- [`multitrans`](internal/multitrans/README.md) - Two-way audio for Chinese version of [TP-Link](https://www.tp-link.com.cn/) cameras.
|
||||||
|
- [`nest`](internal/nest/README.md) - [Google Nest](https://developers.google.com/nest/device-access/supported-devices) cameras through user-unfriendly and paid APIs.
|
||||||
|
- [`ring`](internal/ring/README.md) - Ring cameras with two-way audio support.
|
||||||
|
- [`roborock`](internal/roborock/README.md) - [Roborock](https://roborock.com/) vacuums with cameras with two-way audio support.
|
||||||
|
- [`tapo`](internal/tapo/README.md) - [TP-Link Tapo](https://www.tapo.com/) cameras with two-way audio support.
|
||||||
|
- [`vigi`](internal/tapo/README.md#tp-link-vigi) - TP-Link Vigi cameras.
|
||||||
|
- [`tuya`](internal/tuya/README.md) - [Tuya](https://www.tuya.com/) ecosystem cameras with two-way audio support.
|
||||||
|
- [`webtorrent`](internal/webtorrent/README.md) - Stream from another go2rtc via [WebTorrent](https://en.wikipedia.org/wiki/WebTorrent) protocol.
|
||||||
|
- [`wyze`](internal/wyze/README.md) - [Wyze](https://wyze.com/) cameras using native P2P protocol
|
||||||
|
- [`xiaomi`](internal/xiaomi/README.md) - [Xiaomi Mi Home](https://home.mi.com/) ecosystem cameras with two-way audio support.
|
||||||
|
|
||||||
|
#### devices
|
||||||
|
|
||||||
|
- [`alsa`](internal/alsa/README.md) - A [framework](https://en.wikipedia.org/wiki/Advanced_Linux_Sound_Architecture) for receiving audio from devices on Linux OS.
|
||||||
|
- [`v4l2`](internal/v4l2/README.md) - A [framework](https://en.wikipedia.org/wiki/Video4Linux) for receiving video from devices on Linux OS.
|
||||||
|
|
||||||
|
#### files
|
||||||
|
|
||||||
|
- [`adts`](internal/http/README.md#tcp) - Audio stream in [AAC](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) codec with Audio Data Transport Stream headers.
|
||||||
|
- [`flv`](internal/http/README.md#tcp) - The legacy but still used [Flash Video](https://en.wikipedia.org/wiki/Flash_Video) format.
|
||||||
|
- [`h264`](internal/http/README.md#tcp) - AVC/H.264 bitstream.
|
||||||
|
- [`hevc`](internal/http/README.md#tcp) - HEVC/H.265 bitstream.
|
||||||
|
- [`hls`](internal/http/README.md) - A popular [HTTP Live Streaming](https://en.wikipedia.org/wiki/HTTP_Live_Streaming) format.
|
||||||
|
- [`mjpeg`](internal/http/README.md#tcp) - A continuous sequence of JPEG frames (without HTTP headers).
|
||||||
|
- [`mpegts`](internal/http/README.md#tcp) - The legacy [MPEG transport stream](https://en.wikipedia.org/wiki/MPEG_transport_stream) format.
|
||||||
|
- [`wav`](internal/http/README.md#tcp) - Audio stream in [Waveform Audio File](https://en.wikipedia.org/wiki/WAV) format.
|
||||||
|
|
||||||
|
#### scripts
|
||||||
|
|
||||||
|
- [`echo`](internal/echo/README.md) - If the source has a dynamic link, you can use a bash or python script to get it.
|
||||||
|
- [`exec`](internal/exec/README.md) - You can run an external application (`ffmpeg`, `gstreamer`, `rpicam`, etc.) and receive a media stream from it.
|
||||||
|
- [`expr`](internal/expr/README.md) - If the source has a dynamic link, you can use [Expr](https://github.com/expr-lang/expr) language to get it.
|
||||||
|
- [`ffmpeg`](internal/ffmpeg/README.md) - Use [FFmpeg](https://ffmpeg.org/) as a stream source. Hardware-accelerated transcoding and streaming from USB devices are supported.
|
||||||
|
|
||||||
|
#### webrtc
|
||||||
|
|
||||||
|
- [`creality`](internal/webrtc/README.md#creality) - [Creality](https://www.creality.com/) 3D printer cameras.
|
||||||
|
- [`kinesis`](internal/webrtc/README.md#kinesis) - [Amazon Kinesis](https://aws.amazon.com/kinesis/video-streams/) video streams.
|
||||||
|
- [`openipc`](internal/webrtc/README.md#openipc) - Cameras on open-source [OpenIPC](https://openipc.org/) firmware.
|
||||||
|
- [`switchbot`](internal/webrtc/README.md#switchbot) - [SwitchBot](https://us.switch-bot.com/) cameras.
|
||||||
|
- [`whep`](internal/webrtc/README.md#whep) - [WebRTC/WHEP](https://datatracker.ietf.org/doc/draft-murillo-whep/) is replaced by [WebRTC/WISH](https://datatracker.ietf.org/doc/charter-ietf-wish/02/) standard for WebRTC video/audio viewers.
|
||||||
|
- [`wyze`](internal/webrtc/README.md#wyze) - Legacy method to connect to [Wyze](https://www.wyze.com/) cameras via [docker-wyze-bridge](https://github.com/mrlt8/docker-wyze-bridge).
|
||||||
|
|
||||||
|
### Streaming output
|
||||||
|
|
||||||
|
- [`adts`](internal/mpeg/README.md) - Output stream in ADTS format with [AAC](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) audio.
|
||||||
|
- [`ascii`](internal/mjpeg/README.md#ascii) - Just for fun stream as [ASCII to Terminal](https://www.youtube.com/watch?v=sHj_3h_sX7M).
|
||||||
|
- [`flv`](internal/rtmp/README.md) - Output stream in [Flash Video](https://en.wikipedia.org/wiki/Flash_Video) format.
|
||||||
|
- [`hls`](internal/hls/README.md) - Output stream in [HTTP Live Streaming](https://en.wikipedia.org/wiki/HTTP_Live_Streaming) format.
|
||||||
|
- [`homekit`](internal/homekit/README.md#homekit-server) - Output stream to [Apple Home](https://www.apple.com/home-app/) using [HomeKit](https://en.wikipedia.org/wiki/Apple_Home) protocol.
|
||||||
|
- [`jpeg`](internal/mjpeg/README.md#jpeg) - Output snapshots in [JPEG](https://en.wikipedia.org/wiki/JPEG) format.
|
||||||
|
- [`mpjpeg`](internal/mjpeg/README.md#mpjpeg) - Output a stream in [MJPEG](https://en.wikipedia.org/wiki/Motion_JPEG) format.
|
||||||
|
- [`mp4`](internal/mp4/README.md) - Output as [MP4 stream](https://en.wikipedia.org/wiki/Progressive_download) or [Media Source Extensions](https://developer.mozilla.org/en-US/docs/Web/API/Media_Source_Extensions_API) (MSE) compatible format.
|
||||||
|
- [`mpegts`](internal/mpeg/README.md) - Output stream in [MPEG transport stream](https://en.wikipedia.org/wiki/MPEG_transport_stream) format.
|
||||||
|
- [`onvif`](internal/onvif/README.md#onvif-server) - Output stream using [ONVIF](https://en.wikipedia.org/wiki/ONVIF) protocol.
|
||||||
|
- [`rtmp`](internal/rtmp/README.md#rtmp-server) - Output stream using [Real-Time Messaging](https://en.wikipedia.org/wiki/Real-Time_Messaging_Protocol) protocol.
|
||||||
|
- [`rtsp`](internal/rtsp/README.md#rtsp-server) - Output stream using [Real-Time Streaming](https://en.wikipedia.org/wiki/Real-Time_Streaming_Protocol) protocol.
|
||||||
|
- [`webrtc`](internal/webrtc/README.md#webrtc-server) - Output stream using [Web Real-Time Communication](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API) API.
|
||||||
|
- [`webtorrent`](internal/webtorrent/README.md#webtorrent-server) - Output stream using [WebTorrent](https://en.wikipedia.org/wiki/WebTorrent) protocol.
|
||||||
|
- [`yuv4mpegpipe`](internal/mjpeg/README.md#yuv4mpegpipe) - Output in raw [YUV](https://en.wikipedia.org/wiki/Y%E2%80%B2UV) frame stream with [YUV4MPEG](https://manned.org/yuv4mpeg) header.
|
||||||
|
|
||||||
|
### Streaming ingest
|
||||||
|
|
||||||
|
Supported for:
|
||||||
|
[`flv`](internal/rtmp/README.md#flv-server),
|
||||||
|
[`mjpeg`](internal/mjpeg/README.md#streaming-ingest),
|
||||||
|
[`mpegts`](internal/mpeg/README.md#streaming-ingest),
|
||||||
|
[`rtmp`](internal/rtmp/README.md#rtmp-server),
|
||||||
|
[`rtsp`](internal/rtsp/README.md#streaming-ingest),
|
||||||
|
[`webrtc`](internal/webrtc/README.md#streaming-ingest).
|
||||||
|
|
||||||
|
This is a feature when go2rtc expects to receive an incoming stream from an external application. The stream transmission is started and stopped by an external application.
|
||||||
|
|
||||||
|
- You can push data only to an existing stream (create a stream with empty source in config).
|
||||||
|
- You can push multiple incoming sources to the same stream.
|
||||||
|
- You can push data to a non-empty stream, so it will have additional codecs inside.
|
||||||
|
|
||||||
|
### Two-way audio
|
||||||
|
|
||||||
|
Supported for:
|
||||||
|
[`doorbird`](internal/doorbird/README.md),
|
||||||
|
[`dvrip`](internal/dvrip/README.md),
|
||||||
|
[`exec`](internal/exec/README.md),
|
||||||
|
[`isapi`](internal/isapi/README.md),
|
||||||
|
[`multitrans`](internal/multitrans/README.md),
|
||||||
|
[`ring`](internal/ring/README.md),
|
||||||
|
[`roborock`](internal/roborock/README.md),
|
||||||
|
[`rtsp`](internal/rtsp/README.md#two-way-audio),
|
||||||
|
[`tapo`](internal/tapo/README.md),
|
||||||
|
[`tuya`](internal/tuya/README.md),
|
||||||
|
[`webrtc`](internal/webrtc/README.md),
|
||||||
|
[`wyze`](internal/wyze/README.md),
|
||||||
|
[`xiaomi`](internal/xiaomi/README.md).
|
||||||
|
|
||||||
|
Two-way audio can be used in browser with [WebRTC](internal/webrtc/README.md) technology. The browser will give access to the microphone only for HTTPS sites ([read more](https://stackoverflow.com/questions/52759992/how-to-access-camera-and-microphone-in-chrome-without-https)).
|
||||||
|
|
||||||
|
### Stream to camera
|
||||||
|
|
||||||
|
You can play audio files or live streams on any camera with [two-way audio](#two-way-audio) support.
|
||||||
|
|
||||||
|
[read more](internal/streams/README.md#stream-to-camera)
|
||||||
|
|
||||||
|
### Publish stream
|
||||||
|
|
||||||
|
You can publish any stream to streaming services (YouTube, Telegram, etc.) via RTMP/RTMPS.
|
||||||
|
|
||||||
|
[read more](internal/streams/README.md#publish-stream)
|
||||||
|
|
||||||
|
### Preload stream
|
||||||
|
|
||||||
|
You can preload any stream on go2rtc start. This is useful for cameras that take a long time to start up.
|
||||||
|
|
||||||
|
[read more](internal/streams/README.md#preload-stream)
|
||||||
|
|
||||||
|
### Streaming stats
|
||||||
|
|
||||||
|
[WebUI](www/README.md) provides detailed information about all active connections, including IP-addresses, formats, protocols, number of packets and bytes transferred.
|
||||||
|
Via the [HTTP API](internal/api/README.md) in [`json`](https://en.wikipedia.org/wiki/JSON) or [`dot`](https://en.wikipedia.org/wiki/DOT_(graph_description_language)) format on an interactive connection map.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Codecs
|
||||||
|
|
||||||
|
If you have questions about why video or audio is not displayed, you need to read the following sections.
|
||||||
|
|
||||||
|
| Name | FFmpeg | RTSP | Aliases |
|
||||||
|
|------------------------------|----------|---------------|-------------|
|
||||||
|
| Advanced Audio Coding | `aac` | MPEG4-GENERIC | |
|
||||||
|
| Advanced Video Coding | `h264` | H264 | AVC, H.264 |
|
||||||
|
| G.711 PCM (A-law) | `alaw` | PCMA | G711A |
|
||||||
|
| G.711 PCM (µ-law) | `mulaw` | PCMU | G711u |
|
||||||
|
| High Efficiency Video Coding | `hevc` | H265 | HEVC, H.265 |
|
||||||
|
| Motion JPEG | `mpjpeg` | JPEG | |
|
||||||
|
| MPEG-1 Audio Layer III | `mp3` | MPA | |
|
||||||
|
| Opus Codec | `opus` | OPUS | |
|
||||||
|
| PCM signed 16-bit big-endian | `s16be` | L16 | |
|
||||||
|
|
||||||
|
### Codecs filters
|
||||||
|
|
||||||
|
go2rtc can automatically detect which codecs your device supports for [WebRTC](internal/webrtc/README.md) and [MSE](internal/mp4/README.md) technologies.
|
||||||
|
|
||||||
|
But it cannot be done for [RTSP](internal/rtsp/README.md), [HTTP progressive streaming](internal/mp4/README.md), [HLS](internal/hls/README.md) technologies.
|
||||||
|
You can manually add a codec filter when you create a link to a stream.
|
||||||
|
The filters work the same for all three technologies.
|
||||||
|
Filters do not create a new codec, they only select the suitable codec from existing sources.
|
||||||
|
You can add new codecs to the stream using the [FFmpeg transcoding](internal/ffmpeg/README.md).
|
||||||
|
|
||||||
|
Without filters:
|
||||||
|
|
||||||
|
- RTSP will provide only the first video and only the first audio (any codec)
|
||||||
|
- MP4 will include only compatible codecs (H264, H265, AAC)
|
||||||
|
- HLS will output in the legacy TS format (H264 without audio)
|
||||||
|
|
||||||
|
Some examples:
|
||||||
|
|
||||||
|
- `rtsp://192.168.1.123:8554/camera1?mp4` - useful for recording as MP4 files (e.g. Home Assistant or Frigate)
|
||||||
|
- `rtsp://192.168.1.123:8554/camera1?video=h264,h265&audio=aac` - full version of the filter above
|
||||||
|
- `rtsp://192.168.1.123:8554/camera1?video=h264&audio=aac&audio=opus` - H264 video codec and two separate audio tracks
|
||||||
|
- `rtsp://192.168.1.123:8554/camera1?video&audio=all` - any video codec and all audio codecs as separate tracks
|
||||||
|
- `http://192.168.1.123:1984/api/stream.m3u8?src=camera1&mp4` - HLS stream with MP4 compatible codecs (HLS/fMP4)
|
||||||
|
- `http://192.168.1.123:1984/api/stream.m3u8?src=camera1&mp4=flac` - HLS stream with PCMA/PCMU/PCM audio support (HLS/fMP4), won't work on old devices
|
||||||
|
- `http://192.168.1.123:1984/api/stream.mp4?src=camera1&mp4=flac` - MP4 file with PCMA/PCMU/PCM audio support, won't work on old devices (ex. iOS 12)
|
||||||
|
- `http://192.168.1.123:1984/api/stream.mp4?src=camera1&mp4=all` - MP4 file with non-standard audio codecs, won't work on some players
|
||||||
|
|
||||||
|
### Codecs madness
|
||||||
|
|
||||||
|
`AVC/H.264` video can be played almost anywhere. But `HEVC/H.265` has many limitations in supporting different devices and browsers.
|
||||||
|
|
||||||
|
| Device | WebRTC | MSE | HTTP* | HLS |
|
||||||
|
|--------------------------------------------------------------------|-----------------------------------------|-----------------------------------------|----------------------------------------------|-----------------------------|
|
||||||
|
| *latency* | best | medium | bad | bad |
|
||||||
|
| Desktop Chrome 136+ <br/> Desktop Edge <br/> Android Chrome 136+ | H264, H265* <br/> PCMU, PCMA <br/> OPUS | H264, H265* <br/> AAC, FLAC* <br/> OPUS | H264, H265* <br/> AAC, FLAC* <br/> OPUS, MP3 | no |
|
||||||
|
| Desktop Firefox | H264 <br/> PCMU, PCMA <br/> OPUS | H264 <br/> AAC, FLAC* <br/> OPUS | H264 <br/> AAC, FLAC* <br/> OPUS | no |
|
||||||
|
| Desktop Safari 14+ <br/> iPad Safari 14+ <br/> iPhone Safari 17.1+ | H264, H265* <br/> PCMU, PCMA <br/> OPUS | H264, H265 <br/> AAC, FLAC* | **no!** | H264, H265 <br/> AAC, FLAC* |
|
||||||
|
| iPhone Safari 14+ | H264, H265* <br/> PCMU, PCMA <br/> OPUS | **no!** | **no!** | H264, H265 <br/> AAC, FLAC* |
|
||||||
|
| macOS [Hass App][1] | no | no | no | H264, H265 <br/> AAC, FLAC* |
|
||||||
|
|
||||||
|
[1]: https://apps.apple.com/app/home-assistant/id1099568401
|
||||||
|
|
||||||
|
- `HTTP*` - HTTP Progressive Streaming, not related to [progressive download](https://en.wikipedia.org/wiki/Progressive_download), because the file has no size and no end
|
||||||
|
- `WebRTC H265` - supported in [Chrome 136+](https://developer.chrome.com/release-notes/136), supported in [Safari 18+](https://developer.apple.com/documentation/safari-release-notes/safari-18-release-notes)
|
||||||
|
- `MSE iPhone` - supported in [iOS 17.1+](https://webkit.org/blog/14735/webkit-features-in-safari-17-1/)
|
||||||
|
|
||||||
|
**Audio**
|
||||||
|
|
||||||
|
- go2rtc supports [automatic repackaging](#built-in-transcoding) of `PCMA/PCMU/PCM` codecs into `FLAC` for MSE/MP4/HLS so they'll work almost anywhere
|
||||||
|
- **WebRTC** audio codecs: `PCMU/8000`, `PCMA/8000`, `OPUS/48000/2`
|
||||||
|
- `OPUS` and `MP3` inside **MP4** are part of the standard, but some players do not support them anyway (especially Apple)
|
||||||
|
|
||||||
|
**Apple devices**
|
||||||
|
|
||||||
|
- all Apple devices don't support HTTP progressive streaming
|
||||||
|
- old iPhone firmwares don't support MSE technology because it competes with the HTTP Live Streaming (HLS) technology, invented by Apple
|
||||||
|
- HLS is the worst technology for **live** streaming, it still exists only because of iPhones
|
||||||
|
|
||||||
|
### Built-in transcoding
|
||||||
|
|
||||||
|
There are no plans to embed complex transcoding algorithms inside go2rtc.
|
||||||
|
[FFmpeg source](internal/ffmpeg/README.md) does a great job with this.
|
||||||
|
Including [hardware acceleration](https://github.com/AlexxIT/go2rtc/wiki/Hardware-acceleration) support.
|
||||||
|
|
||||||
|
But go2rtc has some simple algorithms. They are turned on automatically; you do not need to set them up additionally.
|
||||||
|
|
||||||
|
**PCM for MSE/MP4/HLS**
|
||||||
|
|
||||||
|
Go2rtc can pack `PCMA`, `PCMU` and `PCM` codecs into an MP4 container so that they work in all browsers and all built-in players on modern devices. Including Apple QuickTime:
|
||||||
|
|
||||||
|
```text
|
||||||
|
PCMA/PCMU => PCM => FLAC => MSE/MP4/HLS
|
||||||
|
```
|
||||||
|
|
||||||
|
**Resample PCMA/PCMU for WebRTC**
|
||||||
|
|
||||||
|
By default WebRTC supports only `PCMA/8000` and `PCMU/8000`. But go2rtc can automatically resample PCMA and PCMU codecs with a different sample rate. Also, go2rtc can transcode `PCM` codec to `PCMA/8000`, so WebRTC can play it:
|
||||||
|
|
||||||
|
```text
|
||||||
|
PCM/xxx => PCMA/8000 => WebRTC
|
||||||
|
PCMA/xxx => PCMA/8000 => WebRTC
|
||||||
|
PCMU/xxx => PCMU/8000 => WebRTC
|
||||||
|
```
|
||||||
|
|
||||||
|
**Important**
|
||||||
|
|
||||||
|
- FLAC codec not supported in an RTSP stream. If you are using Frigate or Home Assistant for recording MP4 files with PCMA/PCMU/PCM audio, you should set up transcoding to the AAC codec.
|
||||||
|
- PCMA and PCMU are VERY low-quality codecs. They support only 256! different sounds. Use them only when you have no other options.
|
||||||
|
|
||||||
|
### Codecs negotiation
|
||||||
|
|
||||||
|
For example, you want to watch an RTSP stream from a [Dahua IPC-K42](https://www.dahuasecurity.com/fr/products/All-Products/Network-Cameras/Wireless-Series/Wi-Fi-Series/4MP/IPC-K42) camera in your Chrome browser.
|
||||||
|
|
||||||
|
- this camera supports two-way audio standard **ONVIF Profile T**
|
||||||
|
- this camera supports codecs **H264, H265** for sending video, and you select `H264` in camera settings
|
||||||
|
- this camera supports codecs **AAC, PCMU, PCMA** for sending audio (from mic), and you select `AAC/16000` in camera settings
|
||||||
|
- this camera supports codecs **AAC, PCMU, PCMA** for receiving audio (to speaker), you don't need to select them
|
||||||
|
- your browser supports codecs **H264, VP8, VP9, AV1** for receiving video, you don't need to select them
|
||||||
|
- your browser supports codecs **OPUS, PCMU, PCMA** for sending and receiving audio, you don't need to select them
|
||||||
|
- you can't get the camera audio directly because its audio codecs don't match your browser's codecs
|
||||||
|
- so you decide to use transcoding via FFmpeg and add this setting to the config YAML file
|
||||||
|
- you have chosen `OPUS/48000/2` codec, because it is higher quality than the `PCMU/8000` or `PCMA/8000`
|
||||||
|
|
||||||
|
Now you have a stream with two sources - **RTSP and FFmpeg**:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
dahua:
|
||||||
|
- rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif
|
||||||
|
- ffmpeg:rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0#audio=opus
|
||||||
|
```
|
||||||
|
|
||||||
|
**go2rtc** automatically matches codecs for your browser across all of your stream sources. This is called **multi-source two-way codec negotiation**, and it's one of the main features of this app.
|
||||||
|
|
||||||
|
**PS.** You can select `PCMU` or `PCMA` codec in camera settings and not use transcoding at all. Or you can select `AAC` codec for main stream and `PCMU` codec for second stream and add both RTSP to YAML config, this also will work fine.
|
||||||
|
|
||||||
|
## Security
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> If an attacker gains access to the API, you are in danger. Through the API, an attacker can use insecure sources such as echo and exec. And get full access to your server.
|
||||||
|
|
||||||
|
For maximum (paranoid) security, go2rtc has special settings:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
app:
|
||||||
|
# use only allowed modules
|
||||||
|
modules: [api, rtsp, webrtc, exec, ffmpeg, mjpeg]
|
||||||
|
|
||||||
|
api:
|
||||||
|
# use only allowed API paths
|
||||||
|
allow_paths: [/api, /api/streams, /api/webrtc, /api/frame.jpeg]
|
||||||
|
# enable auth for localhost (used together with username and password)
|
||||||
|
local_auth: true
|
||||||
|
|
||||||
|
exec:
|
||||||
|
# use only allowed exec paths
|
||||||
|
allow_paths: [ffmpeg]
|
||||||
|
```
|
||||||
|
|
||||||
|
By default, `go2rtc` starts the Web interface on port `1984` and RTSP on port `8554`, as well as uses port `8555` for WebRTC connections. The three ports are accessible from your local network. So anyone on your local network can watch video from your cameras without authorization. The same rule applies to the Home Assistant add-on.
|
||||||
|
|
||||||
|
This is not a problem if you trust your local network as much as I do. But you can change this behaviour with a `go2rtc.yaml` config:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
api:
|
||||||
|
listen: "127.0.0.1:1984" # localhost
|
||||||
|
|
||||||
|
rtsp:
|
||||||
|
listen: "127.0.0.1:8554" # localhost
|
||||||
|
|
||||||
|
webrtc:
|
||||||
|
listen: ":8555" # external TCP/UDP port
|
||||||
|
```
|
||||||
|
|
||||||
|
- local access to RTSP is not a problem for [FFmpeg](internal/ffmpeg/README.md) integration, because it runs locally on your server
|
||||||
|
- local access to API is not a problem for the [Home Assistant add-on](#go2rtc-home-assistant-add-on), because Home Assistant runs locally on the same server, and the add-on web UI is protected with Home Assistant authorization ([Ingress feature](https://www.home-assistant.io/blog/2019/04/15/hassio-ingress/))
|
||||||
|
- external access to WebRTC TCP port is not a problem, because it is used only for transmitting encrypted media data
|
||||||
|
- anyway you need to open this port to your local network and to the Internet for WebRTC to work
|
||||||
|
|
||||||
|
If you need web interface protection without the Home Assistant add-on, you need to use a reverse proxy, like [Nginx](https://nginx.org/), [Caddy](https://caddyserver.com/), etc.
|
||||||
|
|
||||||
|
PS. Additionally, WebRTC will try to use the 8555 UDP port to transmit encrypted media. It works without problems on the local network, and sometimes also works for external access, even if you haven't opened this port on your router ([read more](https://en.wikipedia.org/wiki/UDP_hole_punching)). But for stable external WebRTC access, you need to open the 8555 port on your router for both TCP and UDP.
|
||||||
|
|
||||||
|
## Projects using go2rtc
|
||||||
|
|
||||||
|
- [Home Assistant](https://www.home-assistant.io/) [2024.11+](https://www.home-assistant.io/integrations/go2rtc/) - top open-source smart home project
|
||||||
|
- [Frigate](https://frigate.video/) [0.12+](https://docs.frigate.video/guides/configuring_go2rtc/) - open-source NVR built around real-time AI object detection
|
||||||
|
- [Advanced Camera Card](https://github.com/dermotduffy/advanced-camera-card) - custom card for Home Assistant
|
||||||
|
- [OpenIPC](https://github.com/OpenIPC/firmware/tree/master/general/package/go2rtc) - alternative IP camera firmware from an open community
|
||||||
|
- [wz_mini_hacks](https://github.com/gtxaspec/wz_mini_hacks) - custom firmware for Wyze cameras
|
||||||
|
- [EufyP2PStream](https://github.com/oischinger/eufyp2pstream) - a small project that provides a video/audio stream from Eufy cameras that don't directly support RTSP
|
||||||
|
- [ioBroker.euSec](https://github.com/bropat/ioBroker.eusec) - [ioBroker](https://www.iobroker.net/) adapter for controlling Eufy security devices
|
||||||
|
- [MMM-go2rtc](https://github.com/Anonym-tsk/MMM-go2rtc) - MagicMirror² module
|
||||||
|
- [ring-mqtt](https://github.com/tsightler/ring-mqtt) - Ring-to-MQTT bridge
|
||||||
|
- [lightNVR](https://github.com/opensensor/lightNVR)
|
||||||
|
|
||||||
|
**Distributions**
|
||||||
|
|
||||||
|
- [Alpine Linux](https://pkgs.alpinelinux.org/packages?name=go2rtc)
|
||||||
|
- [Arch User Repository](https://linux-packages.com/aur/package/go2rtc)
|
||||||
|
- [Gentoo](https://github.com/inode64/inode64-overlay/tree/main/media-video/go2rtc)
|
||||||
|
- [NixOS](https://search.nixos.org/packages?query=go2rtc)
|
||||||
|
- [Proxmox Helper Scripts](https://github.com/community-scripts/ProxmoxVE/)
|
||||||
|
- [QNAP](https://www.myqnap.org/product/go2rtc/)
|
||||||
|
- [Synology NAS](https://synocommunity.com/package/go2rtc)
|
||||||
|
- [Unraid](https://unraid.net/community/apps?q=go2rtc)
|
||||||
|
|
||||||
|
## Camera experience
|
||||||
|
|
||||||
|
- [Dahua](https://www.dahuasecurity.com/) - reference implementation streaming protocols, a lot of settings, high stream quality, multiple streaming clients
|
||||||
|
- [EZVIZ](https://www.ezviz.com/) - awful RTSP protocol implementation, many bugs in SDP
|
||||||
|
- [Hikvision](https://www.hikvision.com/) - a lot of proprietary streaming technologies
|
||||||
|
- [Reolink](https://reolink.com/) - some models have an awful, unusable RTSP implementation and not the best RTMP alternative (I recommend that you contact Reolink support for new firmware), few settings
|
||||||
|
- [Sonoff](https://sonoff.tech/) - very low stream quality, no settings, not the best protocol implementation
|
||||||
|
- [TP-Link](https://www.tp-link.com/) - few streaming clients, packet loss?
|
||||||
|
- Cheap noname cameras, Wyze Cams, Xiaomi cameras with hacks (usually have `/live/ch00_1` in RTSP URL) - awful but usable RTSP protocol implementation, low stream quality, few settings, packet loss?
|
||||||
|
|
||||||
|
## Tips
|
||||||
|
|
||||||
|
**Using apps for low RTSP delay**
|
||||||
|
|
||||||
|
- `ffplay -fflags nobuffer -flags low_delay "rtsp://192.168.1.123:8554/camera1"`
|
||||||
|
- VLC > Preferences > Input / Codecs > Default Caching Level: Lowest Latency
|
||||||
|
|
||||||
|
**Snapshots to Telegram**
|
||||||
|
|
||||||
|
[read more](https://github.com/AlexxIT/go2rtc/wiki/Snapshot-to-Telegram)
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
# syntax=docker/dockerfile:labs
|
||||||
|
|
||||||
|
# 0. Prepare images
|
||||||
|
ARG PYTHON_VERSION="3.13"
|
||||||
|
ARG GO_VERSION="1.25"
|
||||||
|
|
||||||
|
|
||||||
|
# 1. Build go2rtc binary
|
||||||
|
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS build
|
||||||
|
ARG TARGETPLATFORM
|
||||||
|
ARG TARGETOS
|
||||||
|
ARG TARGETARCH
|
||||||
|
|
||||||
|
ENV GOOS=${TARGETOS}
|
||||||
|
ENV GOARCH=${TARGETARCH}
|
||||||
|
|
||||||
|
WORKDIR /build
|
||||||
|
|
||||||
|
RUN apk add git
|
||||||
|
|
||||||
|
# Cache dependencies
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/go-build go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath
|
||||||
|
|
||||||
|
|
||||||
|
# 2. Final image
|
||||||
|
FROM python:${PYTHON_VERSION}-alpine AS base
|
||||||
|
|
||||||
|
# Install ffmpeg, tini (for signal handling),
|
||||||
|
# and other common tools for the echo source.
|
||||||
|
# alsa-plugins-pulse for ALSA support (+0MB)
|
||||||
|
# font-droid for FFmpeg drawtext filter (+2MB)
|
||||||
|
RUN apk add --no-cache tini ffmpeg ffplay bash curl jq alsa-plugins-pulse font-droid
|
||||||
|
|
||||||
|
# Hardware Acceleration for Intel CPU (+50MB)
|
||||||
|
ARG TARGETARCH
|
||||||
|
|
||||||
|
RUN if [ "${TARGETARCH}" = "amd64" ]; then apk add --no-cache libva-intel-driver intel-media-driver; fi
|
||||||
|
|
||||||
|
# Hardware: AMD and NVidia VAAPI (not sure about this)
|
||||||
|
# RUN libva-glx mesa-va-gallium
|
||||||
|
# Hardware: AMD and NVidia VDPAU (not sure about this)
|
||||||
|
# RUN libva-vdpau-driver mesa-vdpau-gallium (+150MB total)
|
||||||
|
|
||||||
|
COPY --from=build /build/go2rtc /usr/local/bin/
|
||||||
|
|
||||||
|
EXPOSE 1984 8554 8555 8555/udp
|
||||||
|
ENTRYPOINT ["/sbin/tini", "--"]
|
||||||
|
VOLUME /config
|
||||||
|
WORKDIR /config
|
||||||
|
|
||||||
|
CMD ["go2rtc", "-config", "/config/go2rtc.yaml"]
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
# Docker
|
||||||
|
|
||||||
|
Images are built automatically via [GitHub actions](https://github.com/AlexxIT/go2rtc/actions) and published on [Docker Hub](https://hub.docker.com/r/alexxit/go2rtc) and [GitHub](https://github.com/AlexxIT/go2rtc/pkgs/container/go2rtc).
|
||||||
|
|
||||||
|
## Versions
|
||||||
|
|
||||||
|
- `alexxit/go2rtc:latest` - latest release based on `alpine` (`amd64`, `386`, `arm/v6`, `arm/v7`, `arm64`) with support for hardware transcoding for Intel iGPU and Raspberry
|
||||||
|
- `alexxit/go2rtc:latest-hardware` - latest release based on `debian 13` (`amd64`) with support for hardware transcoding for Intel iGPU, AMD GPU and NVidia GPU
|
||||||
|
- `alexxit/go2rtc:latest-rockchip` - latest release based on `debian 12` (`arm64`) with support for hardware transcoding for Rockchip RK35xx
|
||||||
|
- `alexxit/go2rtc:master` - latest unstable version based on `alpine`
|
||||||
|
- `alexxit/go2rtc:master-hardware` - latest unstable version based on `debian 13` (`amd64`)
|
||||||
|
- `alexxit/go2rtc:master-rockchip` - latest unstable version based on `debian 12` (`arm64`)
|
||||||
|
|
||||||
|
## Docker compose
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
services:
|
||||||
|
go2rtc:
|
||||||
|
image: alexxit/go2rtc
|
||||||
|
network_mode: host # important for WebRTC, HomeKit, UDP cameras
|
||||||
|
privileged: true # only for FFmpeg hardware transcoding
|
||||||
|
restart: unless-stopped # autorestart on fail or config change from WebUI
|
||||||
|
environment:
|
||||||
|
- TZ=Atlantic/Bermuda # timezone in logs
|
||||||
|
volumes:
|
||||||
|
- "~/go2rtc:/config" # folder for go2rtc.yaml file (edit from WebUI)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Basic Deployment
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -d \
|
||||||
|
--name go2rtc \
|
||||||
|
--network host \
|
||||||
|
--privileged \
|
||||||
|
--restart unless-stopped \
|
||||||
|
-e TZ=Atlantic/Bermuda \
|
||||||
|
-v ~/go2rtc:/config \
|
||||||
|
alexxit/go2rtc
|
||||||
|
```
|
||||||
|
|
||||||
|
## Deployment with GPU Acceleration
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -d \
|
||||||
|
--name go2rtc \
|
||||||
|
--network host \
|
||||||
|
--privileged \
|
||||||
|
--restart unless-stopped \
|
||||||
|
-e TZ=Atlantic/Bermuda \
|
||||||
|
--gpus all \
|
||||||
|
-v ~/go2rtc:/config \
|
||||||
|
alexxit/go2rtc:latest-hardware
|
||||||
|
```
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
# syntax=docker/dockerfile:labs
|
||||||
|
|
||||||
|
# 0. Prepare images
|
||||||
|
# only debian 13 (trixie) has latest ffmpeg
|
||||||
|
# https://packages.debian.org/trixie/ffmpeg
|
||||||
|
ARG DEBIAN_VERSION="trixie-slim"
|
||||||
|
ARG GO_VERSION="1.25-bookworm"
|
||||||
|
|
||||||
|
|
||||||
|
# 1. Build go2rtc binary
|
||||||
|
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build
|
||||||
|
ARG TARGETPLATFORM
|
||||||
|
ARG TARGETOS
|
||||||
|
ARG TARGETARCH
|
||||||
|
|
||||||
|
ENV GOOS=${TARGETOS}
|
||||||
|
ENV GOARCH=${TARGETARCH}
|
||||||
|
|
||||||
|
WORKDIR /build
|
||||||
|
|
||||||
|
# Cache dependencies
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/go-build go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath
|
||||||
|
|
||||||
|
|
||||||
|
# 2. Final image
|
||||||
|
FROM debian:${DEBIAN_VERSION}
|
||||||
|
|
||||||
|
# Prepare apt for buildkit cache
|
||||||
|
RUN rm -f /etc/apt/apt.conf.d/docker-clean \
|
||||||
|
&& echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache
|
||||||
|
|
||||||
|
# Install ffmpeg, tini (for signal handling),
|
||||||
|
# and other common tools for the echo source.
|
||||||
|
# non-free for Intel QSV support (not used by go2rtc, just for tests)
|
||||||
|
# mesa-va-drivers for AMD APU
|
||||||
|
# libasound2-plugins for ALSA support
|
||||||
|
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked --mount=type=cache,target=/var/lib/apt,sharing=locked \
|
||||||
|
echo 'deb http://deb.debian.org/debian trixie non-free' > /etc/apt/sources.list.d/debian-non-free.list && \
|
||||||
|
apt-get -y update && apt-get -y install ffmpeg tini \
|
||||||
|
python3 curl jq \
|
||||||
|
intel-media-va-driver-non-free \
|
||||||
|
mesa-va-drivers \
|
||||||
|
libasound2-plugins && \
|
||||||
|
apt-get clean && rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
COPY --from=build /build/go2rtc /usr/local/bin/
|
||||||
|
|
||||||
|
EXPOSE 1984 8554 8555 8555/udp
|
||||||
|
ENTRYPOINT ["/usr/bin/tini", "--"]
|
||||||
|
VOLUME /config
|
||||||
|
WORKDIR /config
|
||||||
|
# https://github.com/NVIDIA/nvidia-docker/wiki/Installation-(Native-GPU-Support)
|
||||||
|
ENV NVIDIA_VISIBLE_DEVICES all
|
||||||
|
ENV NVIDIA_DRIVER_CAPABILITIES compute,video,utility
|
||||||
|
|
||||||
|
CMD ["go2rtc", "-config", "/config/go2rtc.yaml"]
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
# syntax=docker/dockerfile:labs
|
||||||
|
|
||||||
|
# 0. Prepare images
|
||||||
|
ARG PYTHON_VERSION="3.13-slim-bookworm"
|
||||||
|
ARG GO_VERSION="1.25-bookworm"
|
||||||
|
|
||||||
|
|
||||||
|
# 1. Build go2rtc binary
|
||||||
|
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build
|
||||||
|
ARG TARGETPLATFORM
|
||||||
|
ARG TARGETOS
|
||||||
|
ARG TARGETARCH
|
||||||
|
|
||||||
|
ENV GOOS=${TARGETOS}
|
||||||
|
ENV GOARCH=${TARGETARCH}
|
||||||
|
|
||||||
|
WORKDIR /build
|
||||||
|
|
||||||
|
# Cache dependencies
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/go-build go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath
|
||||||
|
|
||||||
|
|
||||||
|
# 2. Final image
|
||||||
|
FROM python:${PYTHON_VERSION}
|
||||||
|
|
||||||
|
# Prepare apt for buildkit cache
|
||||||
|
RUN rm -f /etc/apt/apt.conf.d/docker-clean \
|
||||||
|
&& echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache
|
||||||
|
|
||||||
|
# Install ffmpeg, tini (for signal handling),
|
||||||
|
# and other common tools for the echo source.
|
||||||
|
# libasound2-plugins for ALSA support
|
||||||
|
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked --mount=type=cache,target=/var/lib/apt,sharing=locked \
|
||||||
|
apt-get -y update && apt-get -y install tini \
|
||||||
|
curl jq \
|
||||||
|
libasound2-plugins && \
|
||||||
|
apt-get clean && rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
COPY --from=build /build/go2rtc /usr/local/bin/
|
||||||
|
ADD --chmod=755 https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/download/6.1-8-no_extra_dump/ffmpeg /usr/local/bin
|
||||||
|
|
||||||
|
EXPOSE 1984 8554 8555 8555/udp
|
||||||
|
ENTRYPOINT ["/usr/bin/tini", "--"]
|
||||||
|
VOLUME /config
|
||||||
|
WORKDIR /config
|
||||||
|
|
||||||
|
CMD ["go2rtc", "-config", "/config/go2rtc.yaml"]
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/hass"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app.Init()
|
||||||
|
streams.Init()
|
||||||
|
|
||||||
|
api.Init()
|
||||||
|
|
||||||
|
hass.Init()
|
||||||
|
|
||||||
|
shell.RunUntilSignal()
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api/ws"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/ffmpeg"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/mjpeg"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/v4l2"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app.Init()
|
||||||
|
streams.Init()
|
||||||
|
|
||||||
|
api.Init()
|
||||||
|
ws.Init()
|
||||||
|
|
||||||
|
ffmpeg.Init()
|
||||||
|
mjpeg.Init()
|
||||||
|
v4l2.Init()
|
||||||
|
|
||||||
|
shell.RunUntilSignal()
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/rtsp"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app.Init()
|
||||||
|
streams.Init()
|
||||||
|
|
||||||
|
rtsp.Init()
|
||||||
|
|
||||||
|
shell.RunUntilSignal()
|
||||||
|
}
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/hap"
|
||||||
|
)
|
||||||
|
|
||||||
|
var servs = map[string]string{
|
||||||
|
"3E": "Accessory Information",
|
||||||
|
"7E": "Security System",
|
||||||
|
"85": "Motion Sensor",
|
||||||
|
"96": "Battery",
|
||||||
|
"A2": "Protocol Information",
|
||||||
|
"110": "Camera RTP Stream Management",
|
||||||
|
"112": "Microphone",
|
||||||
|
"113": "Speaker",
|
||||||
|
"121": "Doorbell",
|
||||||
|
"129": "Data Stream Transport Management",
|
||||||
|
"204": "Camera Recording Management",
|
||||||
|
"21A": "Camera Operating Mode",
|
||||||
|
"22A": "Wi-Fi Transport",
|
||||||
|
"239": "Accessory Runtime Information",
|
||||||
|
}
|
||||||
|
|
||||||
|
var chars = map[string]string{
|
||||||
|
"14": "Identify",
|
||||||
|
"20": "Manufacturer",
|
||||||
|
"21": "Model",
|
||||||
|
"23": "Name",
|
||||||
|
"30": "Serial Number",
|
||||||
|
"52": "Firmware Revision",
|
||||||
|
"53": "Hardware Revision",
|
||||||
|
"220": "Product Data",
|
||||||
|
"A6": "Accessory Flags",
|
||||||
|
|
||||||
|
"22": "Motion Detected",
|
||||||
|
"75": "Status Active",
|
||||||
|
|
||||||
|
"11A": "Mute",
|
||||||
|
"119": "Volume",
|
||||||
|
|
||||||
|
"B0": "Active",
|
||||||
|
"209": "Selected Camera Recording Configuration",
|
||||||
|
"207": "Supported Audio Recording Configuration",
|
||||||
|
"205": "Supported Camera Recording Configuration",
|
||||||
|
"206": "Supported Video Recording Configuration",
|
||||||
|
"226": "Recording Audio Active",
|
||||||
|
|
||||||
|
"223": "Event Snapshots Active",
|
||||||
|
"225": "Periodic Snapshots Active",
|
||||||
|
"21B": "HomeKit Camera Active",
|
||||||
|
"21C": "Third Party Camera Active",
|
||||||
|
"21D": "Camera Operating Mode Indicator",
|
||||||
|
"11B": "Night Vision",
|
||||||
|
//"129": "Supported Data Stream Transport Configuration",
|
||||||
|
"37": "Version",
|
||||||
|
"131": "Setup Data Stream Transport",
|
||||||
|
"130": "Supported Data Stream Transport Configuration",
|
||||||
|
|
||||||
|
"120": "Streaming Status",
|
||||||
|
"115": "Supported Audio Stream Configuration",
|
||||||
|
"116": "Supported RTP Configuration",
|
||||||
|
"114": "Supported Video Stream Configuration",
|
||||||
|
"117": "Selected RTP Stream Configuration",
|
||||||
|
"118": "Setup Endpoints",
|
||||||
|
|
||||||
|
"22B": "Current Transport",
|
||||||
|
"22C": "Wi-Fi Capabilities",
|
||||||
|
"22D": "Wi-Fi Configuration Control",
|
||||||
|
|
||||||
|
"23C": "Ping",
|
||||||
|
|
||||||
|
"68": "Battery Level",
|
||||||
|
"79": "Status Low Battery",
|
||||||
|
"8F": "Charging State",
|
||||||
|
|
||||||
|
"73": "Programmable Switch Event",
|
||||||
|
"232": "Operating State Response",
|
||||||
|
|
||||||
|
"66": "Security System Current State",
|
||||||
|
"67": "Security System Target State",
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
src := os.Args[1]
|
||||||
|
dst := os.Args[2]
|
||||||
|
|
||||||
|
f, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var v hap.JSONAccessories
|
||||||
|
if err = json.NewDecoder(f).Decode(&v); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, acc := range v.Value {
|
||||||
|
for _, srv := range acc.Services {
|
||||||
|
if srv.Desc == "" {
|
||||||
|
srv.Desc = servs[srv.Type]
|
||||||
|
}
|
||||||
|
for _, chr := range srv.Characters {
|
||||||
|
if chr.Desc == "" {
|
||||||
|
chr.Desc = chars[chr.Type]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err = os.Create(dst)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
enc := json.NewEncoder(f)
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
if err = enc.Encode(v); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/mdns"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var service = mdns.ServiceHAP
|
||||||
|
|
||||||
|
if len(os.Args) >= 2 {
|
||||||
|
service = os.Args[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
onentry := func(entry *mdns.ServiceEntry) bool {
|
||||||
|
log.Printf("name=%s, addr=%s, info=%s\n", entry.Name, entry.Addr(), entry.Info)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if len(os.Args) >= 3 {
|
||||||
|
host := os.Args[2]
|
||||||
|
|
||||||
|
log.Printf("run discovery service=%s host=%s\n", service, host)
|
||||||
|
|
||||||
|
err = mdns.QueryOrDiscovery(host, service, onentry)
|
||||||
|
} else {
|
||||||
|
log.Printf("run discovery service=%s\n", service)
|
||||||
|
|
||||||
|
err = mdns.Discovery(service, onentry)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
module pinggy
|
||||||
|
|
||||||
|
go 1.25
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/Pinggy-io/pinggy-go/pinggy v0.6.9 // indirect
|
||||||
|
golang.org/x/crypto v0.8.0 // indirect
|
||||||
|
golang.org/x/sys v0.7.0 // indirect
|
||||||
|
)
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
github.com/Pinggy-io/pinggy-go/pinggy v0.6.9 h1:lzZ00JK6BUGQXnpkJZ+cVj8kIkXsmiVBUci9uEkSwEY=
|
||||||
|
github.com/Pinggy-io/pinggy-go/pinggy v0.6.9/go.mod h1:V1Sxb+4zyr36o9atZiqtT4XhsKtW1RSb2GvsbTbTJYw=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
|
||||||
|
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
|
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
|
||||||
|
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
|
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/Pinggy-io/pinggy-go/pinggy"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tunType := os.Args[1]
|
||||||
|
address := os.Args[2]
|
||||||
|
|
||||||
|
log.SetFlags(log.Llongfile | log.LstdFlags)
|
||||||
|
|
||||||
|
config := pinggy.Config{
|
||||||
|
Type: pinggy.TunnelType(tunType),
|
||||||
|
TcpForwardingAddr: address,
|
||||||
|
|
||||||
|
//SshOverSsl: true,
|
||||||
|
//Stdout: os.Stderr,
|
||||||
|
//Stderr: os.Stderr,
|
||||||
|
}
|
||||||
|
|
||||||
|
if tunType == "http" {
|
||||||
|
hman := pinggy.CreateHeaderManipulationAndAuthConfig()
|
||||||
|
//hman.SetReverseProxy(address)
|
||||||
|
//hman.SetPassPreflight(true)
|
||||||
|
//hman.SetNoReverseProxy()
|
||||||
|
config.HeaderManipulationAndAuth = hman
|
||||||
|
}
|
||||||
|
|
||||||
|
pl, err := pinggy.ConnectWithConfig(config)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
log.Println("Addrs: ", pl.RemoteUrls())
|
||||||
|
//err = pl.InitiateWebDebug("localhost:3424")
|
||||||
|
//log.Println(err)
|
||||||
|
pl.StartForwarding()
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
## ONVIF Client
|
||||||
|
|
||||||
|
```shell
|
||||||
|
go run examples/onvif_client/main.go http://admin:password@192.168.10.90 GetAudioEncoderConfigurations
|
||||||
|
```
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/onvif"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var rawURL = os.Args[1]
|
||||||
|
var operation = os.Args[2]
|
||||||
|
var token string
|
||||||
|
if len(os.Args) > 3 {
|
||||||
|
token = os.Args[3]
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := onvif.NewClient(rawURL)
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var b []byte
|
||||||
|
|
||||||
|
switch operation {
|
||||||
|
case onvif.ServiceGetServiceCapabilities:
|
||||||
|
b, err = client.MediaRequest(operation)
|
||||||
|
case onvif.DeviceGetCapabilities,
|
||||||
|
onvif.DeviceGetDeviceInformation,
|
||||||
|
onvif.DeviceGetDiscoveryMode,
|
||||||
|
onvif.DeviceGetDNS,
|
||||||
|
onvif.DeviceGetHostname,
|
||||||
|
onvif.DeviceGetNetworkDefaultGateway,
|
||||||
|
onvif.DeviceGetNetworkInterfaces,
|
||||||
|
onvif.DeviceGetNetworkProtocols,
|
||||||
|
onvif.DeviceGetNTP,
|
||||||
|
onvif.DeviceGetScopes,
|
||||||
|
onvif.DeviceGetServices,
|
||||||
|
onvif.DeviceGetSystemDateAndTime,
|
||||||
|
onvif.DeviceSystemReboot:
|
||||||
|
b, err = client.DeviceRequest(operation)
|
||||||
|
case onvif.MediaGetProfiles,
|
||||||
|
onvif.MediaGetVideoEncoderConfigurations,
|
||||||
|
onvif.MediaGetVideoSources,
|
||||||
|
onvif.MediaGetVideoSourceConfigurations,
|
||||||
|
onvif.MediaGetAudioEncoderConfigurations,
|
||||||
|
onvif.MediaGetAudioSources,
|
||||||
|
onvif.MediaGetAudioSourceConfigurations:
|
||||||
|
b, err = client.MediaRequest(operation)
|
||||||
|
case onvif.MediaGetProfile:
|
||||||
|
b, err = client.GetProfile(token)
|
||||||
|
case onvif.MediaGetVideoSourceConfiguration:
|
||||||
|
b, err = client.GetVideoSourceConfiguration(token)
|
||||||
|
case onvif.MediaGetStreamUri:
|
||||||
|
b, err = client.GetStreamUri(token)
|
||||||
|
case onvif.MediaGetSnapshotUri:
|
||||||
|
b, err = client.GetSnapshotUri(token)
|
||||||
|
default:
|
||||||
|
log.Printf("unknown action\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("%s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
u, err := url.Parse(rawURL)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = os.WriteFile(u.Hostname()+"_"+operation+".xml", b, 0644); err != nil {
|
||||||
|
log.Printf("%s\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/rtsp"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
client := rtsp.NewClient(os.Args[1])
|
||||||
|
if err := client.Dial(); err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client.Medias = []*core.Media{
|
||||||
|
{
|
||||||
|
Kind: core.KindAudio,
|
||||||
|
Direction: core.DirectionRecvonly,
|
||||||
|
Codecs: []*core.Codec{
|
||||||
|
{Name: core.CodecPCMU, ClockRate: 8000},
|
||||||
|
},
|
||||||
|
ID: "streamid=0",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if err := client.Announce(); err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
if _, err := client.SetupMedia(client.Medias[0]); err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
if err := client.Record(); err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
shell.RunUntilSignal()
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# tutk_decoder
|
||||||
|
|
||||||
|
1. Wireshark > Select any packet > Follow > UDP Stream
|
||||||
|
2. Wireshark > File > Export Packet Dissections > As JSON > Displayed, Values
|
||||||
|
3. `tutk_decoder wireshark.json decoded.txt`
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/tutk"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) != 3 {
|
||||||
|
fmt.Println("Usage: tutk_decoder wireshark.json decoded.txt")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
src, err := os.Open(os.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer src.Close()
|
||||||
|
|
||||||
|
dst, err := os.Create(os.Args[2])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer dst.Close()
|
||||||
|
|
||||||
|
var items []item
|
||||||
|
if err = json.NewDecoder(src).Decode(&items); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var b []byte
|
||||||
|
|
||||||
|
for _, v := range items {
|
||||||
|
if v.Source.Layers.Data.DataData == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
s := strings.ReplaceAll(v.Source.Layers.Data.DataData, ":", "")
|
||||||
|
b, err = hex.DecodeString(s)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tutk.ReverseTransCodePartial(b, b)
|
||||||
|
|
||||||
|
ts := v.Source.Layers.Frame.FrameTimeRelative
|
||||||
|
|
||||||
|
_, _ = fmt.Fprintf(dst, "%8s: %s -> %s [%4d] %x\n",
|
||||||
|
ts[:len(ts)-6],
|
||||||
|
v.Source.Layers.Ip.IpSrc, v.Source.Layers.Ip.IpDst,
|
||||||
|
len(b), b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type item struct {
|
||||||
|
Source struct {
|
||||||
|
Layers struct {
|
||||||
|
Frame struct {
|
||||||
|
FrameTimeRelative string `json:"frame.time_relative"`
|
||||||
|
FrameNumber string `json:"frame.number"`
|
||||||
|
} `json:"frame"`
|
||||||
|
Ip struct {
|
||||||
|
IpSrc string `json:"ip.src"`
|
||||||
|
IpDst string `json:"ip.dst"`
|
||||||
|
} `json:"ip"`
|
||||||
|
Udp struct {
|
||||||
|
UdpSrcport string `json:"udp.srcport"`
|
||||||
|
UdpDstport string `json:"udp.dstport"`
|
||||||
|
} `json:"udp"`
|
||||||
|
Data struct {
|
||||||
|
DataData string `json:"data.data"`
|
||||||
|
DataLen string `json:"data.len"`
|
||||||
|
} `json:"data"`
|
||||||
|
} `json:"layers"`
|
||||||
|
} `json:"_source"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
module github.com/AlexxIT/go2rtc
|
||||||
|
|
||||||
|
go 1.24.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/asticode/go-astits v1.14.0
|
||||||
|
github.com/eclipse/paho.mqtt.golang v1.5.1
|
||||||
|
github.com/expr-lang/expr v1.17.7
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
|
github.com/gorilla/websocket v1.5.3
|
||||||
|
github.com/mattn/go-isatty v0.0.20
|
||||||
|
github.com/miekg/dns v1.1.70
|
||||||
|
github.com/pion/dtls/v3 v3.0.10
|
||||||
|
github.com/pion/ice/v4 v4.2.0
|
||||||
|
github.com/pion/interceptor v0.1.43
|
||||||
|
github.com/pion/rtcp v1.2.16
|
||||||
|
github.com/pion/rtp v1.10.0
|
||||||
|
github.com/pion/sdp/v3 v3.0.17
|
||||||
|
github.com/pion/srtp/v3 v3.0.10
|
||||||
|
github.com/pion/stun/v3 v3.1.1
|
||||||
|
github.com/pion/webrtc/v4 v4.2.3
|
||||||
|
github.com/rs/zerolog v1.34.0
|
||||||
|
github.com/sigurn/crc16 v0.0.0-20240131213347-83fcde1e29d1
|
||||||
|
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f
|
||||||
|
github.com/stretchr/testify v1.11.1
|
||||||
|
github.com/tadglines/go-pkgs v0.0.0-20210623144937-b983b20f54f9
|
||||||
|
golang.org/x/crypto v0.47.0
|
||||||
|
golang.org/x/net v0.49.0
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/asticode/go-astikit v0.57.1 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/kr/pretty v0.3.1 // indirect
|
||||||
|
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||||
|
github.com/pion/datachannel v1.6.0 // indirect
|
||||||
|
github.com/pion/logging v0.2.4 // indirect
|
||||||
|
github.com/pion/mdns/v2 v2.1.0 // indirect
|
||||||
|
github.com/pion/randutil v0.1.0 // indirect
|
||||||
|
github.com/pion/sctp v1.9.2 // indirect
|
||||||
|
github.com/pion/transport/v3 v3.1.1 // indirect
|
||||||
|
github.com/pion/transport/v4 v4.0.1 // indirect
|
||||||
|
github.com/pion/turn/v4 v4.1.4 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
github.com/wlynxg/anet v0.0.5 // indirect
|
||||||
|
golang.org/x/mod v0.32.0 // indirect
|
||||||
|
golang.org/x/sync v0.19.0 // indirect
|
||||||
|
golang.org/x/sys v0.40.0 // indirect
|
||||||
|
golang.org/x/time v0.14.0 // indirect
|
||||||
|
golang.org/x/tools v0.41.0 // indirect
|
||||||
|
)
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
github.com/asticode/go-astikit v0.30.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
|
||||||
|
github.com/asticode/go-astikit v0.57.1 h1:fEykwH98Nny08kcRbk4uer+S8h0rKveCIpG9F6NVLuA=
|
||||||
|
github.com/asticode/go-astikit v0.57.1/go.mod h1:fV43j20UZYfXzP9oBn33udkvCvDvCDhzjVqoLFuuYZE=
|
||||||
|
github.com/asticode/go-astits v1.14.0 h1:zkgnZzipx2XX5mWycqsSBeEyDH58+i4HtyF4j2ROb00=
|
||||||
|
github.com/asticode/go-astits v1.14.0/go.mod h1:QSHmknZ51pf6KJdHKZHJTLlMegIrhega3LPWz3ND/iI=
|
||||||
|
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||||
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2ISgCl2W7nE=
|
||||||
|
github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU=
|
||||||
|
github.com/expr-lang/expr v1.17.6 h1:1h6i8ONk9cexhDmowO/A64VPxHScu7qfSl2k8OlINec=
|
||||||
|
github.com/expr-lang/expr v1.17.6/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
|
||||||
|
github.com/expr-lang/expr v1.17.7 h1:Q0xY/e/2aCIp8g9s/LGvMDCC5PxYlvHgDZRQ4y16JX8=
|
||||||
|
github.com/expr-lang/expr v1.17.7/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
|
||||||
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||||
|
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||||
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/miekg/dns v1.1.69 h1:Kb7Y/1Jo+SG+a2GtfoFUfDkG//csdRPwRLkCsxDG9Sc=
|
||||||
|
github.com/miekg/dns v1.1.69/go.mod h1:7OyjD9nEba5OkqQ/hB4fy3PIoxafSZJtducccIelz3g=
|
||||||
|
github.com/miekg/dns v1.1.70 h1:DZ4u2AV35VJxdD9Fo9fIWm119BsQL5cZU1cQ9s0LkqA=
|
||||||
|
github.com/miekg/dns v1.1.70/go.mod h1:+EuEPhdHOsfk6Wk5TT2CzssZdqkmFhf8r+aVyDEToIs=
|
||||||
|
github.com/pion/datachannel v1.5.10 h1:ly0Q26K1i6ZkGf42W7D4hQYR90pZwzFOjTq5AuCKk4o=
|
||||||
|
github.com/pion/datachannel v1.5.10/go.mod h1:p/jJfC9arb29W7WrxyKbepTU20CFgyx5oLo8Rs4Py/M=
|
||||||
|
github.com/pion/datachannel v1.6.0 h1:XecBlj+cvsxhAMZWFfFcPyUaDZtd7IJvrXqlXD/53i0=
|
||||||
|
github.com/pion/datachannel v1.6.0/go.mod h1:ur+wzYF8mWdC+Mkis5Thosk+u/VOL287apDNEbFpsIk=
|
||||||
|
github.com/pion/dtls/v3 v3.0.9 h1:4AijfFRm8mAjd1gfdlB1wzJF3fjjR/VPIpJgkEtvYmM=
|
||||||
|
github.com/pion/dtls/v3 v3.0.9/go.mod h1:abApPjgadS/ra1wvUzHLc3o2HvoxppAh+NZkyApL4Os=
|
||||||
|
github.com/pion/dtls/v3 v3.0.10 h1:k9ekkq1kaZoxnNEbyLKI8DI37j/Nbk1HWmMuywpQJgg=
|
||||||
|
github.com/pion/dtls/v3 v3.0.10/go.mod h1:YEmmBYIoBsY3jmG56dsziTv/Lca9y4Om83370CXfqJ8=
|
||||||
|
github.com/pion/ice/v4 v4.1.0 h1:YlxIii2bTPWyC08/4hdmtYq4srbrY0T9xcTsTjldGqU=
|
||||||
|
github.com/pion/ice/v4 v4.1.0/go.mod h1:5gPbzYxqenvn05k7zKPIZFuSAufolygiy6P1U9HzvZ4=
|
||||||
|
github.com/pion/ice/v4 v4.2.0 h1:jJC8S+CvXCCvIQUgx+oNZnoUpt6zwc34FhjWwCU4nlw=
|
||||||
|
github.com/pion/ice/v4 v4.2.0/go.mod h1:EgjBGxDgmd8xB0OkYEVFlzQuEI7kWSCFu+mULqaisy4=
|
||||||
|
github.com/pion/interceptor v0.1.42 h1:0/4tvNtruXflBxLfApMVoMubUMik57VZ+94U0J7cmkQ=
|
||||||
|
github.com/pion/interceptor v0.1.42/go.mod h1:g6XYTChs9XyolIQFhRHOOUS+bGVGLRfgTCUzH29EfVU=
|
||||||
|
github.com/pion/interceptor v0.1.43 h1:6hmRfnmjogSs300xfkR0JxYFZ9k5blTEvCD7wxEDuNQ=
|
||||||
|
github.com/pion/interceptor v0.1.43/go.mod h1:BSiC1qKIJt1XVr3l3xQ2GEmCFStk9tx8fwtCZxxgR7M=
|
||||||
|
github.com/pion/logging v0.2.4 h1:tTew+7cmQ+Mc1pTBLKH2puKsOvhm32dROumOZ655zB8=
|
||||||
|
github.com/pion/logging v0.2.4/go.mod h1:DffhXTKYdNZU+KtJ5pyQDjvOAh/GsNSyv1lbkFbe3so=
|
||||||
|
github.com/pion/mdns/v2 v2.1.0 h1:3IJ9+Xio6tWYjhN6WwuY142P/1jA0D5ERaIqawg/fOY=
|
||||||
|
github.com/pion/mdns/v2 v2.1.0/go.mod h1:pcez23GdynwcfRU1977qKU0mDxSeucttSHbCSfFOd9A=
|
||||||
|
github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA=
|
||||||
|
github.com/pion/randutil v0.1.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8=
|
||||||
|
github.com/pion/rtcp v1.2.16 h1:fk1B1dNW4hsI78XUCljZJlC4kZOPk67mNRuQ0fcEkSo=
|
||||||
|
github.com/pion/rtcp v1.2.16/go.mod h1:/as7VKfYbs5NIb4h6muQ35kQF/J0ZVNz2Z3xKoCBYOo=
|
||||||
|
github.com/pion/rtp v1.8.26 h1:VB+ESQFQhBXFytD+Gk8cxB6dXeVf2WQzg4aORvAvAAc=
|
||||||
|
github.com/pion/rtp v1.8.26/go.mod h1:rF5nS1GqbR7H/TCpKwylzeq6yDM+MM6k+On5EgeThEM=
|
||||||
|
github.com/pion/rtp v1.10.0 h1:XN/xca4ho6ZEcijpdF2VGFbwuHUfiIMf3ew8eAAE43w=
|
||||||
|
github.com/pion/rtp v1.10.0/go.mod h1:rF5nS1GqbR7H/TCpKwylzeq6yDM+MM6k+On5EgeThEM=
|
||||||
|
github.com/pion/sctp v1.8.41 h1:20R4OHAno4Vky3/iE4xccInAScAa83X6nWUfyc65MIs=
|
||||||
|
github.com/pion/sctp v1.8.41/go.mod h1:2wO6HBycUH7iCssuGyc2e9+0giXVW0pyCv3ZuL8LiyY=
|
||||||
|
github.com/pion/sctp v1.9.2 h1:HxsOzEV9pWoeggv7T5kewVkstFNcGvhMPx0GvUOUQXo=
|
||||||
|
github.com/pion/sctp v1.9.2/go.mod h1:OTOlsQ5EDQ6mQ0z4MUGXt2CgQmKyafBEXhUVqLRB6G8=
|
||||||
|
github.com/pion/sdp/v3 v3.0.16 h1:0dKzYO6gTAvuLaAKQkC02eCPjMIi4NuAr/ibAwrGDCo=
|
||||||
|
github.com/pion/sdp/v3 v3.0.16/go.mod h1:9tyKzznud3qiweZcD86kS0ff1pGYB3VX+Bcsmkx6IXo=
|
||||||
|
github.com/pion/sdp/v3 v3.0.17 h1:9SfLAW/fF1XC8yRqQ3iWGzxkySxup4k4V7yN8Fs8nuo=
|
||||||
|
github.com/pion/sdp/v3 v3.0.17/go.mod h1:9tyKzznud3qiweZcD86kS0ff1pGYB3VX+Bcsmkx6IXo=
|
||||||
|
github.com/pion/srtp/v3 v3.0.9 h1:lRGF4G61xxj+m/YluB3ZnBpiALSri2lTzba0kGZMrQY=
|
||||||
|
github.com/pion/srtp/v3 v3.0.9/go.mod h1:E+AuWd7Ug2Fp5u38MKnhduvpVkveXJX6J4Lq4rxUYt8=
|
||||||
|
github.com/pion/srtp/v3 v3.0.10 h1:tFirkpBb3XccP5VEXLi50GqXhv5SKPxqrdlhDCJlZrQ=
|
||||||
|
github.com/pion/srtp/v3 v3.0.10/go.mod h1:3mOTIB0cq9qlbn59V4ozvv9ClW/BSEbRp4cY0VtaR7M=
|
||||||
|
github.com/pion/stun/v3 v3.0.2 h1:BJuGEN2oLrJisiNEJtUTJC4BGbzbfp37LizfqswblFU=
|
||||||
|
github.com/pion/stun/v3 v3.0.2/go.mod h1:JFJKfIWvt178MCF5H/YIgZ4VX3LYE77vca4b9HP60SA=
|
||||||
|
github.com/pion/stun/v3 v3.1.1 h1:CkQxveJ4xGQjulGSROXbXq94TAWu8gIX2dT+ePhUkqw=
|
||||||
|
github.com/pion/stun/v3 v3.1.1/go.mod h1:qC1DfmcCTQjl9PBaMa5wSn3x9IPmKxSdcCsxBcDBndM=
|
||||||
|
github.com/pion/transport/v3 v3.1.1 h1:Tr684+fnnKlhPceU+ICdrw6KKkTms+5qHMgw6bIkYOM=
|
||||||
|
github.com/pion/transport/v3 v3.1.1/go.mod h1:+c2eewC5WJQHiAA46fkMMzoYZSuGzA/7E2FPrOYHctQ=
|
||||||
|
github.com/pion/transport/v4 v4.0.1 h1:sdROELU6BZ63Ab7FrOLn13M6YdJLY20wldXW2Cu2k8o=
|
||||||
|
github.com/pion/transport/v4 v4.0.1/go.mod h1:nEuEA4AD5lPdcIegQDpVLgNoDGreqM/YqmEx3ovP4jM=
|
||||||
|
github.com/pion/turn/v4 v4.1.3 h1:jVNW0iR05AS94ysEtvzsrk3gKs9Zqxf6HmnsLfRvlzA=
|
||||||
|
github.com/pion/turn/v4 v4.1.3/go.mod h1:TD/eiBUf5f5LwXbCJa35T7dPtTpCHRJ9oJWmyPLVT3A=
|
||||||
|
github.com/pion/turn/v4 v4.1.4 h1:EU11yMXKIsK43FhcUnjLlrhE4nboHZq+TXBIi3QpcxQ=
|
||||||
|
github.com/pion/turn/v4 v4.1.4/go.mod h1:ES1DXVFKnOhuDkqn9hn5VJlSWmZPaRJLyBXoOeO/BmQ=
|
||||||
|
github.com/pion/webrtc/v4 v4.1.8 h1:ynkjfiURDQ1+8EcJsoa60yumHAmyeYjz08AaOuor+sk=
|
||||||
|
github.com/pion/webrtc/v4 v4.1.8/go.mod h1:KVaARG2RN0lZx0jc7AWTe38JpPv+1/KicOZ9jN52J/s=
|
||||||
|
github.com/pion/webrtc/v4 v4.2.3 h1:RtdWDnkenNQGxUrZqWa5gSkTm5ncsLg5d+zu0M4cXt4=
|
||||||
|
github.com/pion/webrtc/v4 v4.2.3/go.mod h1:7vsyFzRzaKP5IELUnj8zLcglPyIT6wWwqTppBZ1k6Kc=
|
||||||
|
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||||
|
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||||
|
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||||
|
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
|
||||||
|
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
|
||||||
|
github.com/sigurn/crc16 v0.0.0-20240131213347-83fcde1e29d1 h1:NVK+OqnavpyFmUiKfUMHrpvbCi2VFoWTrcpI7aDaJ2I=
|
||||||
|
github.com/sigurn/crc16 v0.0.0-20240131213347-83fcde1e29d1/go.mod h1:9/etS5gpQq9BJsJMWg1wpLbfuSnkm8dPF6FdW2JXVhA=
|
||||||
|
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f h1:1R9KdKjCNSd7F8iGTxIpoID9prlYH8nuNYKt0XvweHA=
|
||||||
|
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f/go.mod h1:vQhwQ4meQEDfahT5kd61wLAF5AAeh5ZPLVI4JJ/tYo8=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
|
github.com/tadglines/go-pkgs v0.0.0-20210623144937-b983b20f54f9 h1:aeN+ghOV0b2VCmKKO3gqnDQ8mLbpABZgRR2FVYx4ouI=
|
||||||
|
github.com/tadglines/go-pkgs v0.0.0-20210623144937-b983b20f54f9/go.mod h1:roo6cZ/uqpwKMuvPG0YmzI5+AmUiMWfjCBZpGXqbTxE=
|
||||||
|
github.com/wlynxg/anet v0.0.5 h1:J3VJGi1gvo0JwZ/P1/Yc/8p63SoW98B5dHkYDmpgvvU=
|
||||||
|
github.com/wlynxg/anet v0.0.5/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA=
|
||||||
|
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
|
||||||
|
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
|
||||||
|
golang.org/x/crypto v0.47.0 h1:V6e3FRj+n4dbpw86FJ8Fv7XVOql7TEwpHapKoMJ/GO8=
|
||||||
|
golang.org/x/crypto v0.47.0/go.mod h1:ff3Y9VzzKbwSSEzWqJsJVBnWmRwRSHt/6Op5n9bQc4A=
|
||||||
|
golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=
|
||||||
|
golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg=
|
||||||
|
golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c=
|
||||||
|
golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU=
|
||||||
|
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
|
||||||
|
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
|
||||||
|
golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o=
|
||||||
|
golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8=
|
||||||
|
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
||||||
|
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||||
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||||
|
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
|
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
|
||||||
|
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
|
golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
|
||||||
|
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
|
||||||
|
golang.org/x/term v0.39.0 h1:RclSuaJf32jOqZz74CkPA9qFuVTX7vhLlpfj/IGWlqY=
|
||||||
|
golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
|
||||||
|
golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
|
||||||
|
golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA=
|
||||||
|
golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc=
|
||||||
|
golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc=
|
||||||
|
golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
# Modules
|
||||||
|
|
||||||
|
go2rtc tries to name formats, protocols and codecs the same way they are named in FFmpeg.
|
||||||
|
Some formats and protocols go2rtc supports exclusively. They have no equivalent in FFmpeg.
|
||||||
|
|
||||||
|
- The [`echo`], [`expr`], [`hass`] and [`onvif`] modules receive a link to a stream. They don't know the protocol in advance.
|
||||||
|
- The [`exec`] and [`ffmpeg`] modules support many formats. They are identical to the [`http`] module.
|
||||||
|
- The [`api`], [`app`], [`debug`], [`ngrok`], [`pinggy`], [`srtp`], [`streams`] are supporting modules.
|
||||||
|
|
||||||
|
**Modules** implement communication APIs: authorization, encryption, command set, structure of media packets.
|
||||||
|
|
||||||
|
**Formats** describe the structure of the data being transmitted.
|
||||||
|
|
||||||
|
**Protocols** implement transport for data transmission.
|
||||||
|
|
||||||
|
| module | formats | protocols | input | output | ingest | two-way |
|
||||||
|
|----------------|-----------------|------------------|-------|--------|--------|---------|
|
||||||
|
| [`alsa`] | `pcm` | `ioctl` | yes | | | |
|
||||||
|
| [`bubble`] | - | `http` | yes | | | |
|
||||||
|
| [`doorbird`] | `mulaw` | `http` | yes | | | yes |
|
||||||
|
| [`dvrip`] | - | `tcp` | yes | | | yes |
|
||||||
|
| [`echo`] | * | * | yes | | | |
|
||||||
|
| [`eseecloud`] | `rtp` | `http` | yes | | | |
|
||||||
|
| [`exec`] | * | `pipe`, `rtsp` | yes | | | yes |
|
||||||
|
| [`expr`] | * | * | yes | | | |
|
||||||
|
| [`ffmpeg`] | * | `pipe`, `rtsp` | yes | | | |
|
||||||
|
| [`flussonic`] | `mp4` | `ws` | yes | | | |
|
||||||
|
| [`gopro`] | `mpegts` | `udp` | yes | | | |
|
||||||
|
| [`hass`] | * | * | yes | | | |
|
||||||
|
| [`hls`] | `mpegts`, `mp4` | `http` | | yes | | |
|
||||||
|
| [`homekit`] | `srtp` | `hap` | yes | yes | | no |
|
||||||
|
| [`http`] | `adts` | `http`, `tcp` | yes | | | |
|
||||||
|
| [`http`] | `flv` | `http`, `tcp` | yes | | | |
|
||||||
|
| [`http`] | `h264` | `http`, `tcp` | yes | | | |
|
||||||
|
| [`http`] | `hevc` | `http`, `tcp` | yes | | | |
|
||||||
|
| [`http`] | `hls` | `http`, `tcp` | yes | | | |
|
||||||
|
| [`http`] | `mjpeg` | `http`, `tcp` | yes | | | |
|
||||||
|
| [`http`] | `mpjpeg` | `http` | yes | | | |
|
||||||
|
| [`http`] | `mpegts` | `http`, `tcp` | yes | | | |
|
||||||
|
| [`http`] | `wav` | `http`, `tcp` | yes | | | |
|
||||||
|
| [`http`] | `yuv4mpegpipe` | `http`, `tcp` | yes | | | |
|
||||||
|
| [`isapi`] | `alaw`, `mulaw` | `http` | | | | yes |
|
||||||
|
| [`ivideon`] | `mp4` | `ws` | yes | | | |
|
||||||
|
| [`kasa`] | `h264`, `mulaw` | `http` | yes | | | |
|
||||||
|
| [`mjpeg`] | `ascii` | `http` | | yes | | |
|
||||||
|
| [`mjpeg`] | `jpeg` | `http` | | yes | | |
|
||||||
|
| [`mjpeg`] | `mpjpeg` | `http` | | yes | yes | |
|
||||||
|
| [`mjpeg`] | `yuv4mpegpipe` | `http` | | yes | | |
|
||||||
|
| [`mp4`] | `mp4` | `http`, `ws` | | yes | | |
|
||||||
|
| [`mpeg`] | `adts` | `http` | | yes | | |
|
||||||
|
| [`mpeg`] | `mpegts` | `http` | | yes | yes | |
|
||||||
|
| [`multitrans`] | `rtp` | `tcp` | | | | yes |
|
||||||
|
| [`nest`] | `srtp` | `rtsp`, `webrtc` | yes | | | no |
|
||||||
|
| [`onvif`] | `rtp` | * | yes | yes | | |
|
||||||
|
| [`ring`] | `srtp` | `webrtc` | yes | | | yes |
|
||||||
|
| [`roborock`] | `srtp` | `webrtc` | yes | | | yes |
|
||||||
|
| [`rtmp`] | `flv` | `rtmp` | yes | yes | yes | |
|
||||||
|
| [`rtmp`] | `flv` | `http` | | yes | yes | |
|
||||||
|
| [`rtsp`] | `rtsp` | `rtsp` | yes | yes | yes | yes |
|
||||||
|
| [`tapo`] | `mpegts` | `http` | yes | | | yes |
|
||||||
|
| [`tuya`] | `srtp` | `webrtc` | yes | | | yes |
|
||||||
|
| [`v4l2`] | `rawvideo` | `ioctl` | yes | | | |
|
||||||
|
| [`webrtc`] | `srtp` | `webrtc` | yes | yes | yes | yes |
|
||||||
|
| [`webtorrent`] | `srtp` | `webrtc` | yes | yes | | |
|
||||||
|
| [`wyoming`] | `pcm` | `tcp` | | yes | | |
|
||||||
|
| [`wyze`] | - | `tutk` | yes | | | yes |
|
||||||
|
| [`xiaomi`] | - | `cs2`, `tutk` | yes | | | yes |
|
||||||
|
| [`yandex`] | `srtp` | `webrtc` | yes | | | |
|
||||||
|
|
||||||
|
[`alsa`]: alsa/README.md
|
||||||
|
[`api`]: api/README.md
|
||||||
|
[`app`]: app/README.md
|
||||||
|
[`bubble`]: bubble/README.md
|
||||||
|
[`debug`]: debug/README.md
|
||||||
|
[`doorbird`]: doorbird/README.md
|
||||||
|
[`dvrip`]: dvrip/README.md
|
||||||
|
[`echo`]: echo/README.md
|
||||||
|
[`eseecloud`]: eseecloud/README.md
|
||||||
|
[`exec`]: exec/README.md
|
||||||
|
[`expr`]: expr/README.md
|
||||||
|
[`ffmpeg`]: ffmpeg/README.md
|
||||||
|
[`flussonic`]: flussonic/README.md
|
||||||
|
[`gopro`]: gopro/README.md
|
||||||
|
[`hass`]: hass/README.md
|
||||||
|
[`hls`]: hls/README.md
|
||||||
|
[`homekit`]: homekit/README.md
|
||||||
|
[`http`]: http/README.md
|
||||||
|
[`isapi`]: isapi/README.md
|
||||||
|
[`ivideon`]: ivideon/README.md
|
||||||
|
[`kasa`]: kasa/README.md
|
||||||
|
[`mjpeg`]: mjpeg/README.md
|
||||||
|
[`mp4`]: mp4/README.md
|
||||||
|
[`mpeg`]: mpeg/README.md
|
||||||
|
[`multitrans`]: multitrans/README.md
|
||||||
|
[`nest`]: nest/README.md
|
||||||
|
[`ngrok`]: ngrok/README.md
|
||||||
|
[`onvif`]: onvif/README.md
|
||||||
|
[`pinggy`]: pinggy/README.md
|
||||||
|
[`ring`]: ring/README.md
|
||||||
|
[`roborock`]: roborock/README.md
|
||||||
|
[`rtmp`]: rtmp/README.md
|
||||||
|
[`rtsp`]: rtsp/README.md
|
||||||
|
[`srtp`]: srtp/README.md
|
||||||
|
[`streams`]: streams/README.md
|
||||||
|
[`tapo`]: tapo/README.md
|
||||||
|
[`tuya`]: tuya/README.md
|
||||||
|
[`v4l2`]: v4l2/README.md
|
||||||
|
[`webrtc`]: webrtc/README.md
|
||||||
|
[`webtorrent`]: webtorrent/README.md
|
||||||
|
[`wyoming`]: wyze/README.md
|
||||||
|
[`wyze`]: wyze/README.md
|
||||||
|
[`xiaomi`]: xiaomi/README.md
|
||||||
|
[`yandex`]: yandex/README.md
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# ALSA
|
||||||
|
|
||||||
|
[`new in v1.9.10`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.10)
|
||||||
|
|
||||||
|
> [!WARNING]
|
||||||
|
> This source is under development and does not always work well.
|
||||||
|
|
||||||
|
[Advanced Linux Sound Architecture](https://en.wikipedia.org/wiki/Advanced_Linux_Sound_Architecture) - a framework for receiving audio from devices on Linux OS.
|
||||||
|
|
||||||
|
Easy to add via **WebUI > add > ALSA**.
|
||||||
|
|
||||||
|
Alternatively, you can use FFmpeg source.
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
//go:build !(linux && (386 || amd64 || arm || arm64 || mipsle))
|
||||||
|
|
||||||
|
package alsa
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
// not supported
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
//go:build linux && (386 || amd64 || arm || arm64 || mipsle)
|
||||||
|
|
||||||
|
package alsa
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/alsa"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/alsa/device"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
streams.HandleFunc("alsa", alsa.Open)
|
||||||
|
|
||||||
|
api.HandleFunc("api/alsa", apiAlsa)
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiAlsa(w http.ResponseWriter, r *http.Request) {
|
||||||
|
files, err := os.ReadDir("/dev/snd/")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var sources []*api.Source
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
if !strings.HasPrefix(file.Name(), "pcm") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
path := "/dev/snd/" + file.Name()
|
||||||
|
|
||||||
|
dev, err := device.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := dev.Info()
|
||||||
|
if err == nil {
|
||||||
|
formats := formatsToString(dev.ListFormats())
|
||||||
|
r1, r2 := dev.RangeRates()
|
||||||
|
c1, c2 := dev.RangeChannels()
|
||||||
|
source := &api.Source{
|
||||||
|
Name: info.ID,
|
||||||
|
Info: fmt.Sprintf("Formats: %s, Rates: %d-%d, Channels: %d-%d", formats, r1, r2, c1, c2),
|
||||||
|
URL: "alsa:device?audio=" + path,
|
||||||
|
}
|
||||||
|
if !strings.Contains(source.Name, info.Name) {
|
||||||
|
source.Name += ", " + info.Name
|
||||||
|
}
|
||||||
|
sources = append(sources, source)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = dev.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
api.ResponseSources(w, sources)
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatsToString(formats []byte) string {
|
||||||
|
var s string
|
||||||
|
for i, format := range formats {
|
||||||
|
if i > 0 {
|
||||||
|
s += " "
|
||||||
|
}
|
||||||
|
switch format {
|
||||||
|
case 2:
|
||||||
|
s += "s16le"
|
||||||
|
case 10:
|
||||||
|
s += "s32le"
|
||||||
|
default:
|
||||||
|
s += strconv.Itoa(int(format))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
# HTTP API
|
||||||
|
|
||||||
|
The HTTP API is the main part for interacting with the application. Default address: `http://localhost:1984/`.
|
||||||
|
|
||||||
|
The HTTP API is described in [OpenAPI](../../website/api/openapi.yaml) format. It can be explored in [interactive viewer](https://go2rtc.org/api/). WebSocket API described [here](ws/README.md).
|
||||||
|
|
||||||
|
The project's static HTML and JS files are located in the [www](../../www/README.md) folder. An external developer can use them as a basis for integrating go2rtc into their project or for developing a custom web interface for go2rtc.
|
||||||
|
|
||||||
|
The contents of `www` folder are built into go2rtc when building, but you can use configuration to specify an external folder as the source of static files.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
**Important!** go2rtc passes requests from localhost and Unix sockets without HTTP authorization, even if you have it configured. It is your responsibility to set up secure external access to the API. If not properly configured, an attacker can gain access to your cameras and even your server.
|
||||||
|
|
||||||
|
- you can disable HTTP API with `listen: ""` and use, for example, only RTSP client/server protocol
|
||||||
|
- you can enable HTTP API only on localhost with `listen: "127.0.0.1:1984"` setting
|
||||||
|
- you can change the API `base_path` and host go2rtc on your main app webserver suburl
|
||||||
|
- all files from `static_dir` hosted on root path: `/`
|
||||||
|
- you can use raw TLS cert/key content or path to files
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
api:
|
||||||
|
listen: ":1984" # default ":1984", HTTP API port ("" - disabled)
|
||||||
|
username: "admin" # default "", Basic auth for WebUI
|
||||||
|
password: "pass" # default "", Basic auth for WebUI
|
||||||
|
local_auth: true # default false, Enable auth check for localhost requests
|
||||||
|
base_path: "/rtc" # default "", API prefix for serving on suburl (/api => /rtc/api)
|
||||||
|
static_dir: "www" # default "", folder for static files (custom web interface)
|
||||||
|
origin: "*" # default "", allow CORS requests (only * supported)
|
||||||
|
tls_listen: ":443" # default "", enable HTTPS server
|
||||||
|
tls_cert: | # default "", PEM-encoded fullchain certificate for HTTPS
|
||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
...
|
||||||
|
-----END CERTIFICATE-----
|
||||||
|
tls_key: | # default "", PEM-encoded private key for HTTPS
|
||||||
|
-----BEGIN PRIVATE KEY-----
|
||||||
|
...
|
||||||
|
-----END PRIVATE KEY-----
|
||||||
|
unix_listen: "/tmp/go2rtc.sock" # default "", unix socket listener for API
|
||||||
|
```
|
||||||
|
|
||||||
|
**PS:**
|
||||||
|
|
||||||
|
- MJPEG over WebSocket plays better than native MJPEG because Chrome [bug](https://bugs.chromium.org/p/chromium/issues/detail?id=527446)
|
||||||
|
- MP4 over WebSocket was created only for Apple iOS because it doesn't support file streaming
|
||||||
@@ -0,0 +1,321 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"slices"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
var cfg struct {
|
||||||
|
Mod struct {
|
||||||
|
Listen string `yaml:"listen"`
|
||||||
|
Username string `yaml:"username"`
|
||||||
|
Password string `yaml:"password"`
|
||||||
|
LocalAuth bool `yaml:"local_auth"`
|
||||||
|
BasePath string `yaml:"base_path"`
|
||||||
|
StaticDir string `yaml:"static_dir"`
|
||||||
|
Origin string `yaml:"origin"`
|
||||||
|
TLSListen string `yaml:"tls_listen"`
|
||||||
|
TLSCert string `yaml:"tls_cert"`
|
||||||
|
TLSKey string `yaml:"tls_key"`
|
||||||
|
UnixListen string `yaml:"unix_listen"`
|
||||||
|
|
||||||
|
AllowPaths []string `yaml:"allow_paths"`
|
||||||
|
} `yaml:"api"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// default config
|
||||||
|
cfg.Mod.Listen = ":1984"
|
||||||
|
|
||||||
|
// load config from YAML
|
||||||
|
app.LoadConfig(&cfg)
|
||||||
|
|
||||||
|
if cfg.Mod.Listen == "" && cfg.Mod.UnixListen == "" && cfg.Mod.TLSListen == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
allowPaths = cfg.Mod.AllowPaths
|
||||||
|
basePath = cfg.Mod.BasePath
|
||||||
|
log = app.GetLogger("api")
|
||||||
|
|
||||||
|
initStatic(cfg.Mod.StaticDir)
|
||||||
|
|
||||||
|
HandleFunc("api", apiHandler)
|
||||||
|
HandleFunc("api/config", configHandler)
|
||||||
|
HandleFunc("api/exit", exitHandler)
|
||||||
|
HandleFunc("api/restart", restartHandler)
|
||||||
|
HandleFunc("api/log", logHandler)
|
||||||
|
|
||||||
|
Handler = http.DefaultServeMux // 4th
|
||||||
|
|
||||||
|
if cfg.Mod.Origin == "*" {
|
||||||
|
Handler = middlewareCORS(Handler) // 3rd
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Mod.Username != "" {
|
||||||
|
Handler = middlewareAuth(cfg.Mod.Username, cfg.Mod.Password, cfg.Mod.LocalAuth, Handler) // 2nd
|
||||||
|
}
|
||||||
|
|
||||||
|
if log.Trace().Enabled() {
|
||||||
|
Handler = middlewareLog(Handler) // 1st
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Mod.Listen != "" {
|
||||||
|
_, port, _ := net.SplitHostPort(cfg.Mod.Listen)
|
||||||
|
Port, _ = strconv.Atoi(port)
|
||||||
|
go listen("tcp", cfg.Mod.Listen)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Mod.UnixListen != "" {
|
||||||
|
_ = syscall.Unlink(cfg.Mod.UnixListen)
|
||||||
|
go listen("unix", cfg.Mod.UnixListen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize the HTTPS server
|
||||||
|
if cfg.Mod.TLSListen != "" && cfg.Mod.TLSCert != "" && cfg.Mod.TLSKey != "" {
|
||||||
|
go tlsListen("tcp", cfg.Mod.TLSListen, cfg.Mod.TLSCert, cfg.Mod.TLSKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func listen(network, address string) {
|
||||||
|
ln, err := net.Listen(network, address)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("[api] listen")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info().Str("addr", address).Msg("[api] listen")
|
||||||
|
|
||||||
|
server := http.Server{
|
||||||
|
Handler: Handler,
|
||||||
|
ReadHeaderTimeout: 5 * time.Second, // Example: Set to 5 seconds
|
||||||
|
}
|
||||||
|
if err = server.Serve(ln); err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("[api] serve")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tlsListen(network, address, certFile, keyFile string) {
|
||||||
|
var cert tls.Certificate
|
||||||
|
var err error
|
||||||
|
if strings.IndexByte(certFile, '\n') < 0 && strings.IndexByte(keyFile, '\n') < 0 {
|
||||||
|
// check if file path
|
||||||
|
cert, err = tls.LoadX509KeyPair(certFile, keyFile)
|
||||||
|
} else {
|
||||||
|
// if text file content
|
||||||
|
cert, err = tls.X509KeyPair([]byte(certFile), []byte(keyFile))
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ln, err := net.Listen(network, address)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("[api] tls listen")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info().Str("addr", address).Msg("[api] tls listen")
|
||||||
|
|
||||||
|
server := &http.Server{
|
||||||
|
Handler: Handler,
|
||||||
|
TLSConfig: &tls.Config{Certificates: []tls.Certificate{cert}},
|
||||||
|
ReadHeaderTimeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
if err = server.ServeTLS(ln, "", ""); err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("[api] tls serve")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var Port int
|
||||||
|
|
||||||
|
const (
|
||||||
|
MimeJSON = "application/json"
|
||||||
|
MimeText = "text/plain"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Handler http.Handler
|
||||||
|
|
||||||
|
// HandleFunc handle pattern with relative path:
|
||||||
|
// - "api/streams" => "{basepath}/api/streams"
|
||||||
|
// - "/streams" => "/streams"
|
||||||
|
func HandleFunc(pattern string, handler http.HandlerFunc) {
|
||||||
|
if len(pattern) == 0 || pattern[0] != '/' {
|
||||||
|
pattern = basePath + "/" + pattern
|
||||||
|
}
|
||||||
|
if allowPaths != nil && !slices.Contains(allowPaths, pattern) {
|
||||||
|
log.Trace().Str("path", pattern).Msg("[api] ignore path not in allow_paths")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Trace().Str("path", pattern).Msg("[api] register path")
|
||||||
|
http.HandleFunc(pattern, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResponseJSON important always add Content-Type
|
||||||
|
// so go won't need to call http.DetectContentType
|
||||||
|
func ResponseJSON(w http.ResponseWriter, v any) {
|
||||||
|
w.Header().Set("Content-Type", MimeJSON)
|
||||||
|
_ = json.NewEncoder(w).Encode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ResponsePrettyJSON(w http.ResponseWriter, v any) {
|
||||||
|
w.Header().Set("Content-Type", MimeJSON)
|
||||||
|
enc := json.NewEncoder(w)
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
_ = enc.Encode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Response(w http.ResponseWriter, body any, contentType string) {
|
||||||
|
w.Header().Set("Content-Type", contentType)
|
||||||
|
|
||||||
|
switch v := body.(type) {
|
||||||
|
case []byte:
|
||||||
|
_, _ = w.Write(v)
|
||||||
|
case string:
|
||||||
|
_, _ = w.Write([]byte(v))
|
||||||
|
default:
|
||||||
|
_, _ = fmt.Fprint(w, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const StreamNotFound = "stream not found"
|
||||||
|
|
||||||
|
var allowPaths []string
|
||||||
|
var basePath string
|
||||||
|
var log zerolog.Logger
|
||||||
|
|
||||||
|
func middlewareLog(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Trace().Msgf("[api] %s %s %s", r.Method, r.URL, r.RemoteAddr)
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func isLoopback(remoteAddr string) bool {
|
||||||
|
return strings.HasPrefix(remoteAddr, "127.") || strings.HasPrefix(remoteAddr, "[::1]") || remoteAddr == "@"
|
||||||
|
}
|
||||||
|
|
||||||
|
func middlewareAuth(username, password string, localAuth bool, next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if localAuth || !isLoopback(r.RemoteAddr) {
|
||||||
|
user, pass, ok := r.BasicAuth()
|
||||||
|
if !ok || user != username || pass != password {
|
||||||
|
w.Header().Set("Www-Authenticate", `Basic realm="go2rtc"`)
|
||||||
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func middlewareCORS(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var mu sync.Mutex
|
||||||
|
|
||||||
|
func apiHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
mu.Lock()
|
||||||
|
app.Info["host"] = r.Host
|
||||||
|
mu.Unlock()
|
||||||
|
|
||||||
|
ResponseJSON(w, app.Info)
|
||||||
|
}
|
||||||
|
|
||||||
|
func exitHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "POST" {
|
||||||
|
http.Error(w, "", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s := r.URL.Query().Get("code")
|
||||||
|
code, err := strconv.Atoi(s)
|
||||||
|
|
||||||
|
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02
|
||||||
|
if err != nil || code < 0 || code > 125 {
|
||||||
|
http.Error(w, "Code must be in the range [0, 125]", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Exit(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func restartHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "POST" {
|
||||||
|
http.Error(w, "", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
path, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Msgf("[api] restart %s", path)
|
||||||
|
|
||||||
|
go syscall.Exec(path, os.Args, os.Environ())
|
||||||
|
}
|
||||||
|
|
||||||
|
func logHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
|
// Send current state of the log file immediately
|
||||||
|
w.Header().Set("Content-Type", "application/jsonlines")
|
||||||
|
_, _ = app.MemoryLog.WriteTo(w)
|
||||||
|
case "DELETE":
|
||||||
|
app.MemoryLog.Reset()
|
||||||
|
Response(w, "OK", "text/plain")
|
||||||
|
default:
|
||||||
|
http.Error(w, "Method not allowed", http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Source struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Info string `json:"info,omitempty"`
|
||||||
|
URL string `json:"url,omitempty"`
|
||||||
|
Location string `json:"location,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ResponseSources(w http.ResponseWriter, sources []*Source) {
|
||||||
|
if len(sources) == 0 {
|
||||||
|
http.Error(w, "no sources", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = struct {
|
||||||
|
Sources []*Source `json:"sources"`
|
||||||
|
}{
|
||||||
|
Sources: sources,
|
||||||
|
}
|
||||||
|
ResponseJSON(w, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Error(w http.ResponseWriter, err error) {
|
||||||
|
log.Error().Err(err).Caller(1).Send()
|
||||||
|
|
||||||
|
http.Error(w, err.Error(), http.StatusInsufficientStorage)
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func configHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if app.ConfigPath == "" {
|
||||||
|
http.Error(w, "", http.StatusGone)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
|
data, err := os.ReadFile(app.ConfigPath)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// https://www.ietf.org/archive/id/draft-ietf-httpapi-yaml-mediatypes-00.html
|
||||||
|
Response(w, data, "application/yaml")
|
||||||
|
|
||||||
|
case "POST", "PATCH":
|
||||||
|
data, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Method == "PATCH" {
|
||||||
|
// no need to validate after merge
|
||||||
|
data, err = mergeYAML(app.ConfigPath, data)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// validate config
|
||||||
|
if err = yaml.Unmarshal(data, map[string]any{}); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = os.WriteFile(app.ConfigPath, data, 0644); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mergeYAML(file1 string, yaml2 []byte) ([]byte, error) {
|
||||||
|
// Read the contents of the first YAML file
|
||||||
|
data1, err := os.ReadFile(file1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal the first YAML file into a map
|
||||||
|
var config1 map[string]any
|
||||||
|
if err = yaml.Unmarshal(data1, &config1); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal the second YAML document into a map
|
||||||
|
var config2 map[string]any
|
||||||
|
if err = yaml.Unmarshal(yaml2, &config2); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge the two maps
|
||||||
|
config1 = merge(config1, config2)
|
||||||
|
|
||||||
|
// Marshal the merged map into YAML
|
||||||
|
return yaml.Marshal(&config1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func merge(dst, src map[string]any) map[string]any {
|
||||||
|
for k, v := range src {
|
||||||
|
if vv, ok := dst[k]; ok {
|
||||||
|
switch vv := vv.(type) {
|
||||||
|
case map[string]any:
|
||||||
|
v := v.(map[string]any)
|
||||||
|
dst[k] = merge(vv, v)
|
||||||
|
case []any:
|
||||||
|
v := v.([]any)
|
||||||
|
dst[k] = v
|
||||||
|
default:
|
||||||
|
dst[k] = v
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dst[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dst
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/www"
|
||||||
|
)
|
||||||
|
|
||||||
|
func initStatic(staticDir string) {
|
||||||
|
var root http.FileSystem
|
||||||
|
if staticDir != "" {
|
||||||
|
log.Info().Str("dir", staticDir).Msg("[api] serve static")
|
||||||
|
root = http.Dir(staticDir)
|
||||||
|
} else {
|
||||||
|
root = http.FS(www.Static)
|
||||||
|
}
|
||||||
|
|
||||||
|
base := len(basePath)
|
||||||
|
fileServer := http.FileServer(root)
|
||||||
|
|
||||||
|
HandleFunc("", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if base > 0 {
|
||||||
|
r.URL.Path = r.URL.Path[base:]
|
||||||
|
}
|
||||||
|
fileServer.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
# WebSocket
|
||||||
|
|
||||||
|
Endpoint: `/api/ws`
|
||||||
|
|
||||||
|
Query parameters:
|
||||||
|
|
||||||
|
- `src` (required) - Stream name
|
||||||
|
|
||||||
|
### WebRTC
|
||||||
|
|
||||||
|
Request SDP:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"type":"webrtc/offer","value":"v=0\r\n..."}
|
||||||
|
```
|
||||||
|
|
||||||
|
Response SDP:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"type":"webrtc/answer","value":"v=0\r\n..."}
|
||||||
|
```
|
||||||
|
|
||||||
|
Request/response candidate:
|
||||||
|
|
||||||
|
- empty value also allowed and optional
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"type":"webrtc/candidate","value":"candidate:3277516026 1 udp 2130706431 192.168.1.123 54321 typ host"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### MSE
|
||||||
|
|
||||||
|
Request:
|
||||||
|
|
||||||
|
- codecs list optional
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"type":"mse","value":"avc1.640029,avc1.64002A,avc1.640033,hvc1.1.6.L153.B0,mp4a.40.2,mp4a.40.5,flac,opus"}
|
||||||
|
```
|
||||||
|
|
||||||
|
Response:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"type":"mse","value":"video/mp4; codecs=\"avc1.64001F,mp4a.40.2\""}
|
||||||
|
```
|
||||||
|
|
||||||
|
### HLS
|
||||||
|
|
||||||
|
Request:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"type":"hls","value":"avc1.640029,avc1.64002A,avc1.640033,hvc1.1.6.L153.B0,mp4a.40.2,mp4a.40.5,flac"}
|
||||||
|
```
|
||||||
|
|
||||||
|
Response:
|
||||||
|
|
||||||
|
- you MUST rewrite full HTTP path to `http://192.168.1.123:1984/api/hls/playlist.m3u8`
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"type":"hls","value":"#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000000,CODECS=\"avc1.64001F,mp4a.40.2\"\nhls/playlist.m3u8?id=DvmHdd9w"}
|
||||||
|
```
|
||||||
|
|
||||||
|
### MJPEG
|
||||||
|
|
||||||
|
Request/response:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{"type":"mjpeg"}
|
||||||
|
```
|
||||||
@@ -0,0 +1,227 @@
|
|||||||
|
package ws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/creds"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
var cfg struct {
|
||||||
|
Mod struct {
|
||||||
|
Origin string `yaml:"origin"`
|
||||||
|
} `yaml:"api"`
|
||||||
|
}
|
||||||
|
|
||||||
|
app.LoadConfig(&cfg)
|
||||||
|
|
||||||
|
log = app.GetLogger("api")
|
||||||
|
|
||||||
|
initWS(cfg.Mod.Origin)
|
||||||
|
|
||||||
|
api.HandleFunc("api/ws", apiWS)
|
||||||
|
}
|
||||||
|
|
||||||
|
var log zerolog.Logger
|
||||||
|
|
||||||
|
// Message - struct for data exchange in Web API
|
||||||
|
type Message struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Value any `json:"value,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Message) String() (value string) {
|
||||||
|
if s, ok := m.Value.(string); ok {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Message) Unmarshal(v any) error {
|
||||||
|
b, err := json.Marshal(m.Value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return json.Unmarshal(b, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
type WSHandler func(tr *Transport, msg *Message) error
|
||||||
|
|
||||||
|
func HandleFunc(msgType string, handler WSHandler) {
|
||||||
|
wsHandlers[msgType] = handler
|
||||||
|
}
|
||||||
|
|
||||||
|
var wsHandlers = make(map[string]WSHandler)
|
||||||
|
|
||||||
|
func initWS(origin string) {
|
||||||
|
wsUp = &websocket.Upgrader{
|
||||||
|
ReadBufferSize: 4096, // for SDP
|
||||||
|
WriteBufferSize: 512 * 1024, // 512K
|
||||||
|
}
|
||||||
|
|
||||||
|
switch origin {
|
||||||
|
case "":
|
||||||
|
// same origin + ignore port
|
||||||
|
wsUp.CheckOrigin = func(r *http.Request) bool {
|
||||||
|
origin := r.Header["Origin"]
|
||||||
|
if len(origin) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
o, err := url.Parse(origin[0])
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if o.Host == r.Host {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
log.Trace().Msgf("[api] ws origin=%s, host=%s", o.Host, r.Host)
|
||||||
|
// https://github.com/AlexxIT/go2rtc/issues/118
|
||||||
|
if i := strings.IndexByte(o.Host, ':'); i > 0 {
|
||||||
|
return o.Host[:i] == r.Host
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
case "*":
|
||||||
|
// any origin
|
||||||
|
wsUp.CheckOrigin = func(r *http.Request) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiWS(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ws, err := wsUp.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
origin := r.Header.Get("Origin")
|
||||||
|
log.Error().Err(err).Caller().Msgf("host=%s origin=%s", r.Host, origin)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tr := &Transport{Request: r}
|
||||||
|
tr.OnWrite(func(msg any) error {
|
||||||
|
_ = ws.SetWriteDeadline(time.Now().Add(time.Second * 5))
|
||||||
|
|
||||||
|
if data, ok := msg.([]byte); ok {
|
||||||
|
return ws.WriteMessage(websocket.BinaryMessage, data)
|
||||||
|
} else {
|
||||||
|
return ws.WriteJSON(msg)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
for {
|
||||||
|
msg := new(Message)
|
||||||
|
if err = ws.ReadJSON(msg); err != nil {
|
||||||
|
if !websocket.IsCloseError(err, websocket.CloseNoStatusReceived) {
|
||||||
|
log.Trace().Err(err).Caller().Send()
|
||||||
|
}
|
||||||
|
_ = ws.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Trace().Str("type", msg.Type).Msg("[api] ws msg")
|
||||||
|
|
||||||
|
if handler := wsHandlers[msg.Type]; handler != nil {
|
||||||
|
go func() {
|
||||||
|
if err = handler(tr, msg); err != nil {
|
||||||
|
errMsg := creds.SecretString(err.Error())
|
||||||
|
tr.Write(&Message{Type: "error", Value: msg.Type + ": " + errMsg})
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tr.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
var wsUp *websocket.Upgrader
|
||||||
|
|
||||||
|
type Transport struct {
|
||||||
|
Request *http.Request
|
||||||
|
|
||||||
|
ctx map[any]any
|
||||||
|
|
||||||
|
closed bool
|
||||||
|
mx sync.Mutex
|
||||||
|
wrmx sync.Mutex
|
||||||
|
|
||||||
|
onChange func()
|
||||||
|
onWrite func(msg any) error
|
||||||
|
onClose []func()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Transport) OnWrite(f func(msg any) error) {
|
||||||
|
t.mx.Lock()
|
||||||
|
if t.onChange != nil {
|
||||||
|
t.onChange()
|
||||||
|
}
|
||||||
|
t.onWrite = f
|
||||||
|
t.mx.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Transport) Write(msg any) {
|
||||||
|
t.wrmx.Lock()
|
||||||
|
_ = t.onWrite(msg)
|
||||||
|
t.wrmx.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Transport) Close() {
|
||||||
|
t.mx.Lock()
|
||||||
|
for _, f := range t.onClose {
|
||||||
|
f()
|
||||||
|
}
|
||||||
|
t.closed = true
|
||||||
|
t.mx.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Transport) OnChange(f func()) {
|
||||||
|
t.mx.Lock()
|
||||||
|
t.onChange = f
|
||||||
|
t.mx.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Transport) OnClose(f func()) {
|
||||||
|
t.mx.Lock()
|
||||||
|
if t.closed {
|
||||||
|
f()
|
||||||
|
} else {
|
||||||
|
t.onClose = append(t.onClose, f)
|
||||||
|
}
|
||||||
|
t.mx.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithContext - run function with Context variable
|
||||||
|
func (t *Transport) WithContext(f func(ctx map[any]any)) {
|
||||||
|
t.mx.Lock()
|
||||||
|
if t.ctx == nil {
|
||||||
|
t.ctx = map[any]any{}
|
||||||
|
}
|
||||||
|
f(t.ctx)
|
||||||
|
t.mx.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Transport) Writer() io.Writer {
|
||||||
|
return &writer{t: t}
|
||||||
|
}
|
||||||
|
|
||||||
|
type writer struct {
|
||||||
|
t *Transport
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *writer) Write(p []byte) (n int, err error) {
|
||||||
|
w.t.wrmx.Lock()
|
||||||
|
if err = w.t.onWrite(p); err == nil {
|
||||||
|
n = len(p)
|
||||||
|
}
|
||||||
|
w.t.wrmx.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
# App
|
||||||
|
|
||||||
|
The application module is responsible for reading configuration files, running other modules and setting up [logs](#log).
|
||||||
|
|
||||||
|
The configuration can be edited through the application's WebUI with code highlighting, syntax and specification checking.
|
||||||
|
|
||||||
|
- By default, go2rtc will search for the `go2rtc.yaml` config file in the current working directory
|
||||||
|
- go2rtc supports multiple config files:
|
||||||
|
- `go2rtc -c config1.yaml -c config2.yaml -c config3.yaml`
|
||||||
|
- go2rtc supports inline config in multiple formats from the command line:
|
||||||
|
- **YAML**: `go2rtc -c '{log: {format: text}}'`
|
||||||
|
- **JSON**: `go2rtc -c '{"log":{"format":"text"}}'`
|
||||||
|
- **key=value**: `go2rtc -c log.format=text`
|
||||||
|
- Each subsequent config will overwrite the previous one (but only for defined params)
|
||||||
|
|
||||||
|
```
|
||||||
|
go2rtc -config "{log: {format: text}}" -config /config/go2rtc.yaml -config "{rtsp: {listen: ''}}" -config /usr/local/go2rtc/go2rtc.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
or a simpler version
|
||||||
|
|
||||||
|
```
|
||||||
|
go2rtc -c log.format=text -c /config/go2rtc.yaml -c rtsp.listen='' -c /usr/local/go2rtc/go2rtc.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
## Environment variables
|
||||||
|
|
||||||
|
There is support for loading external variables into the config. First, they will be loaded from [credential files](https://systemd.io/CREDENTIALS). If `CREDENTIALS_DIRECTORY` is not set, then the key will be loaded from an environment variable. If no environment variable is set, then the string will be left as-is.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
camera1: rtsp://rtsp:${CAMERA_PASSWORD}@192.168.1.123/av_stream/ch0
|
||||||
|
|
||||||
|
rtsp:
|
||||||
|
username: ${RTSP_USER:admin} # "admin" if "RTSP_USER" not set
|
||||||
|
password: ${RTSP_PASS:secret} # "secret" if "RTSP_PASS" not set
|
||||||
|
```
|
||||||
|
|
||||||
|
## JSON Schema
|
||||||
|
|
||||||
|
Editors like [GoLand](https://www.jetbrains.com/go/) and [VS Code](https://code.visualstudio.com/) support autocomplete and syntax validation.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# yaml-language-server: $schema=https://raw.githubusercontent.com/AlexxIT/go2rtc/master/www/schema.json
|
||||||
|
```
|
||||||
|
|
||||||
|
or from a running go2rtc:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# yaml-language-server: $schema=http://localhost:1984/schema.json
|
||||||
|
```
|
||||||
|
|
||||||
|
## Defaults
|
||||||
|
|
||||||
|
- Default values may change in updates
|
||||||
|
- FFmpeg module has many presets, they are not listed here because they may also change in updates
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
api:
|
||||||
|
listen: ":1984" # default public port for WebUI and HTTP API
|
||||||
|
|
||||||
|
ffmpeg:
|
||||||
|
bin: "ffmpeg" # default binary path for FFmpeg
|
||||||
|
|
||||||
|
log:
|
||||||
|
level: "info" # default log level
|
||||||
|
output: "stdout"
|
||||||
|
time: "UNIXMS"
|
||||||
|
|
||||||
|
rtsp:
|
||||||
|
listen: ":8554" # default public port for RTSP server
|
||||||
|
default_query: "video&audio"
|
||||||
|
|
||||||
|
srtp:
|
||||||
|
listen: ":8443" # default public port for SRTP server (used for HomeKit)
|
||||||
|
|
||||||
|
webrtc:
|
||||||
|
listen: ":8555" # default public port for WebRTC server (TCP and UDP)
|
||||||
|
ice_servers:
|
||||||
|
- urls: [ "stun:stun.cloudflare.com:3478", "stun:stun.l.google.com:19302" ]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Log
|
||||||
|
|
||||||
|
You can set different log levels for different modules.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
log:
|
||||||
|
format: "" # empty (default, autodetect color support), color, json, text
|
||||||
|
level: "info" # disabled, trace, debug, info (default), warn, error
|
||||||
|
output: "stdout" # empty (only to memory), stderr, stdout (default)
|
||||||
|
time: "UNIXMS" # empty (disable timestamp), UNIXMS (default), UNIXMICRO, UNIXNANO
|
||||||
|
|
||||||
|
api: trace # module name: log level
|
||||||
|
```
|
||||||
|
|
||||||
|
Modules: `api`, `streams`, `rtsp`, `webrtc`, `mp4`, `hls`, `mjpeg`, `hass`, `homekit`, `onvif`, `rtmp`, `webtorrent`, `wyoming`, `echo`, `exec`, `expr`, `ffmpeg`, `wyze`, `xiaomi`.
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
"runtime/debug"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Version string
|
||||||
|
Modules []string
|
||||||
|
UserAgent string
|
||||||
|
ConfigPath string
|
||||||
|
Info = make(map[string]any)
|
||||||
|
)
|
||||||
|
|
||||||
|
const usage = `Usage of go2rtc:
|
||||||
|
|
||||||
|
-c, --config Path to config file or config string as YAML or JSON, support multiple
|
||||||
|
-d, --daemon Run in background
|
||||||
|
-v, --version Print version and exit
|
||||||
|
`
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
var config flagConfig
|
||||||
|
var daemon bool
|
||||||
|
var version bool
|
||||||
|
|
||||||
|
flag.Var(&config, "config", "")
|
||||||
|
flag.Var(&config, "c", "")
|
||||||
|
flag.BoolVar(&daemon, "daemon", false, "")
|
||||||
|
flag.BoolVar(&daemon, "d", false, "")
|
||||||
|
flag.BoolVar(&version, "version", false, "")
|
||||||
|
flag.BoolVar(&version, "v", false, "")
|
||||||
|
|
||||||
|
flag.Usage = func() { fmt.Print(usage) }
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
revision, vcsTime := readRevisionTime()
|
||||||
|
|
||||||
|
if version {
|
||||||
|
fmt.Printf("go2rtc version %s (%s) %s/%s\n", Version, revision, runtime.GOOS, runtime.GOARCH)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if daemon && os.Getppid() != 1 {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
fmt.Println("Daemon mode is not supported on Windows")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-run the program in background and exit
|
||||||
|
cmd := exec.Command(os.Args[0], os.Args[1:]...)
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
fmt.Println("Failed to start daemon:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Println("Running in daemon mode with PID:", cmd.Process.Pid)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
UserAgent = "go2rtc/" + Version
|
||||||
|
|
||||||
|
Info["version"] = Version
|
||||||
|
Info["revision"] = revision
|
||||||
|
|
||||||
|
initConfig(config)
|
||||||
|
initLogger()
|
||||||
|
|
||||||
|
platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
|
||||||
|
Logger.Info().Str("version", Version).Str("platform", platform).Str("revision", revision).Msg("go2rtc")
|
||||||
|
Logger.Debug().Str("version", runtime.Version()).Str("vcs.time", vcsTime).Msg("build")
|
||||||
|
|
||||||
|
if ConfigPath != "" {
|
||||||
|
Logger.Info().Str("path", ConfigPath).Msg("config")
|
||||||
|
}
|
||||||
|
|
||||||
|
var cfg struct {
|
||||||
|
Mod struct {
|
||||||
|
Modules []string `yaml:"modules"`
|
||||||
|
} `yaml:"app"`
|
||||||
|
}
|
||||||
|
|
||||||
|
LoadConfig(&cfg)
|
||||||
|
|
||||||
|
Modules = cfg.Mod.Modules
|
||||||
|
}
|
||||||
|
|
||||||
|
func readRevisionTime() (revision, vcsTime string) {
|
||||||
|
if info, ok := debug.ReadBuildInfo(); ok {
|
||||||
|
for _, setting := range info.Settings {
|
||||||
|
switch setting.Key {
|
||||||
|
case "vcs.revision":
|
||||||
|
if len(setting.Value) > 7 {
|
||||||
|
revision = setting.Value[:7]
|
||||||
|
} else {
|
||||||
|
revision = setting.Value
|
||||||
|
}
|
||||||
|
case "vcs.time":
|
||||||
|
vcsTime = setting.Value
|
||||||
|
case "vcs.modified":
|
||||||
|
if setting.Value == "true" {
|
||||||
|
revision += ".dirty"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check version from -buildvcs info
|
||||||
|
// Format for tagged version : v1.9.13
|
||||||
|
// Format for modified code: v1.9.14-0.20251215184105-753d6617ab58+dirty
|
||||||
|
if info.Main.Version != "v"+Version {
|
||||||
|
// Format: 1.9.13+dev.753d661[.dirty]
|
||||||
|
// Compatible with "awesomeversion" and "packaging.version" from python.
|
||||||
|
// Version will be larger than the previous release, but smaller than the next release.
|
||||||
|
Version += "+dev." + revision
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/creds"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadConfig(v any) {
|
||||||
|
for _, data := range configs {
|
||||||
|
if err := yaml.Unmarshal(data, v); err != nil {
|
||||||
|
Logger.Warn().Err(err).Send()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var configMu sync.Mutex
|
||||||
|
|
||||||
|
func PatchConfig(path []string, value any) error {
|
||||||
|
if ConfigPath == "" {
|
||||||
|
return errors.New("config file disabled")
|
||||||
|
}
|
||||||
|
|
||||||
|
configMu.Lock()
|
||||||
|
defer configMu.Unlock()
|
||||||
|
|
||||||
|
// empty config is OK
|
||||||
|
b, _ := os.ReadFile(ConfigPath)
|
||||||
|
|
||||||
|
b, err := yaml.Patch(b, path, value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.WriteFile(ConfigPath, b, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
type flagConfig []string
|
||||||
|
|
||||||
|
func (c *flagConfig) String() string {
|
||||||
|
return strings.Join(*c, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *flagConfig) Set(value string) error {
|
||||||
|
*c = append(*c, value)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var configs [][]byte
|
||||||
|
|
||||||
|
func initConfig(confs flagConfig) {
|
||||||
|
if confs == nil {
|
||||||
|
confs = []string{"go2rtc.yaml"}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, conf := range confs {
|
||||||
|
if len(conf) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if conf[0] == '{' {
|
||||||
|
// config as raw YAML or JSON
|
||||||
|
configs = append(configs, []byte(conf))
|
||||||
|
} else if data := parseConfString(conf); data != nil {
|
||||||
|
configs = append(configs, data)
|
||||||
|
} else {
|
||||||
|
// config as file
|
||||||
|
if ConfigPath == "" {
|
||||||
|
ConfigPath = conf
|
||||||
|
initStorage()
|
||||||
|
}
|
||||||
|
|
||||||
|
if data, _ = os.ReadFile(conf); data == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
loadEnv(data)
|
||||||
|
data = creds.ReplaceVars(data)
|
||||||
|
configs = append(configs, data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ConfigPath != "" {
|
||||||
|
if !filepath.IsAbs(ConfigPath) {
|
||||||
|
if cwd, err := os.Getwd(); err == nil {
|
||||||
|
ConfigPath = filepath.Join(cwd, ConfigPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Info["config_path"] = ConfigPath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseConfString(s string) []byte {
|
||||||
|
i := strings.IndexByte(s, '=')
|
||||||
|
if i < 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
items := strings.Split(s[:i], ".")
|
||||||
|
if len(items) < 2 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// `log.level=trace` => `{log: {level: trace}}`
|
||||||
|
var pre string
|
||||||
|
var suf = s[i+1:]
|
||||||
|
for _, item := range items {
|
||||||
|
pre += "{" + item + ": "
|
||||||
|
suf += "}"
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte(pre + suf)
|
||||||
|
}
|
||||||
@@ -0,0 +1,191 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/creds"
|
||||||
|
"github.com/mattn/go-isatty"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
var MemoryLog = newBuffer()
|
||||||
|
|
||||||
|
func GetLogger(module string) zerolog.Logger {
|
||||||
|
Logger.Trace().Str("module", module).Msgf("[log] init")
|
||||||
|
|
||||||
|
if s, ok := modules[module]; ok {
|
||||||
|
lvl, err := zerolog.ParseLevel(s)
|
||||||
|
if err == nil {
|
||||||
|
return Logger.Level(lvl)
|
||||||
|
}
|
||||||
|
Logger.Warn().Err(err).Caller().Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
return Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
// initLogger support:
|
||||||
|
// - output: empty (only to memory), stderr, stdout
|
||||||
|
// - format: empty (autodetect color support), color, json, text
|
||||||
|
// - time: empty (disable timestamp), UNIXMS, UNIXMICRO, UNIXNANO
|
||||||
|
// - level: disabled, trace, debug, info, warn, error...
|
||||||
|
func initLogger() {
|
||||||
|
var cfg struct {
|
||||||
|
Mod map[string]string `yaml:"log"`
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.Mod = modules // defaults
|
||||||
|
|
||||||
|
LoadConfig(&cfg)
|
||||||
|
|
||||||
|
var writer io.Writer
|
||||||
|
|
||||||
|
switch output, path, _ := strings.Cut(modules["output"], ":"); output {
|
||||||
|
case "stderr":
|
||||||
|
writer = os.Stderr
|
||||||
|
case "stdout":
|
||||||
|
writer = os.Stdout
|
||||||
|
case "file":
|
||||||
|
if path == "" {
|
||||||
|
path = "go2rtc.log"
|
||||||
|
}
|
||||||
|
// if fail - only MemoryLog will be available
|
||||||
|
writer, _ = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeFormat := modules["time"]
|
||||||
|
|
||||||
|
if writer != nil {
|
||||||
|
if format := modules["format"]; format != "json" {
|
||||||
|
console := &zerolog.ConsoleWriter{Out: writer}
|
||||||
|
|
||||||
|
switch format {
|
||||||
|
case "text":
|
||||||
|
console.NoColor = true
|
||||||
|
case "color":
|
||||||
|
console.NoColor = false // useless, but anyway
|
||||||
|
default:
|
||||||
|
// autodetection if output support color
|
||||||
|
// go-isatty - dependency for go-colorable - dependency for ConsoleWriter
|
||||||
|
console.NoColor = !isatty.IsTerminal(writer.(*os.File).Fd())
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeFormat != "" {
|
||||||
|
console.TimeFormat = "15:04:05.000"
|
||||||
|
} else {
|
||||||
|
console.PartsOrder = []string{
|
||||||
|
zerolog.LevelFieldName,
|
||||||
|
zerolog.CallerFieldName,
|
||||||
|
zerolog.MessageFieldName,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writer = console
|
||||||
|
}
|
||||||
|
|
||||||
|
writer = zerolog.MultiLevelWriter(writer, MemoryLog)
|
||||||
|
} else {
|
||||||
|
writer = MemoryLog
|
||||||
|
}
|
||||||
|
|
||||||
|
writer = creds.SecretWriter(writer)
|
||||||
|
|
||||||
|
lvl, _ := zerolog.ParseLevel(modules["level"])
|
||||||
|
Logger = zerolog.New(writer).Level(lvl)
|
||||||
|
|
||||||
|
if timeFormat != "" {
|
||||||
|
zerolog.TimeFieldFormat = timeFormat
|
||||||
|
Logger = Logger.With().Timestamp().Logger()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var Logger zerolog.Logger
|
||||||
|
|
||||||
|
// modules log levels
|
||||||
|
var modules = map[string]string{
|
||||||
|
"format": "", // useless, but anyway
|
||||||
|
"level": "info",
|
||||||
|
"output": "stdout", // TODO: change to stderr someday
|
||||||
|
"time": zerolog.TimeFormatUnixMs,
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
chunkCount = 16
|
||||||
|
chunkSize = 1 << 16
|
||||||
|
)
|
||||||
|
|
||||||
|
type circularBuffer struct {
|
||||||
|
chunks [][]byte
|
||||||
|
r, w int
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBuffer() *circularBuffer {
|
||||||
|
b := &circularBuffer{chunks: make([][]byte, 0, chunkCount)}
|
||||||
|
// create first chunk
|
||||||
|
b.chunks = append(b.chunks, make([]byte, 0, chunkSize))
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *circularBuffer) Write(p []byte) (n int, err error) {
|
||||||
|
n = len(p)
|
||||||
|
|
||||||
|
b.mu.Lock()
|
||||||
|
// check if chunk has size
|
||||||
|
if len(b.chunks[b.w])+n > chunkSize {
|
||||||
|
// increase write chunk index
|
||||||
|
if b.w++; b.w == chunkCount {
|
||||||
|
b.w = 0
|
||||||
|
}
|
||||||
|
// check overflow
|
||||||
|
if b.r == b.w {
|
||||||
|
// increase read chunk index
|
||||||
|
if b.r++; b.r == chunkCount {
|
||||||
|
b.r = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// check if current chunk exists
|
||||||
|
if b.w == len(b.chunks) {
|
||||||
|
// allocate new chunk
|
||||||
|
b.chunks = append(b.chunks, make([]byte, 0, chunkSize))
|
||||||
|
} else {
|
||||||
|
// reset len of current chunk
|
||||||
|
b.chunks[b.w] = b.chunks[b.w][:0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
b.chunks[b.w] = append(b.chunks[b.w], p...)
|
||||||
|
b.mu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *circularBuffer) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
|
buf := make([]byte, 0, chunkCount*chunkSize)
|
||||||
|
|
||||||
|
// use temp buffer inside mutex because w.Write can take some time
|
||||||
|
b.mu.Lock()
|
||||||
|
for i := b.r; ; {
|
||||||
|
buf = append(buf, b.chunks[i]...)
|
||||||
|
if i == b.w {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if i++; i == chunkCount {
|
||||||
|
i = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.mu.Unlock()
|
||||||
|
|
||||||
|
nn, err := w.Write(buf)
|
||||||
|
return int64(nn), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *circularBuffer) Reset() {
|
||||||
|
b.mu.Lock()
|
||||||
|
b.chunks[0] = b.chunks[0][:0]
|
||||||
|
b.r = 0
|
||||||
|
b.w = 0
|
||||||
|
b.mu.Unlock()
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/creds"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
func initStorage() {
|
||||||
|
storage = &envStorage{data: make(map[string]string)}
|
||||||
|
creds.SetStorage(storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadEnv(data []byte) {
|
||||||
|
var cfg struct {
|
||||||
|
Env map[string]string `yaml:"env"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
storage.mu.Lock()
|
||||||
|
for name, value := range cfg.Env {
|
||||||
|
storage.data[name] = value
|
||||||
|
creds.AddSecret(value)
|
||||||
|
}
|
||||||
|
storage.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
var storage *envStorage
|
||||||
|
|
||||||
|
type envStorage struct {
|
||||||
|
data map[string]string
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *envStorage) SetValue(name, value string) error {
|
||||||
|
if err := PatchConfig([]string{"env", name}, value); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
s.data[name] = value
|
||||||
|
s.mu.Unlock()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *envStorage) GetValue(name string) (value string, ok bool) {
|
||||||
|
s.mu.Lock()
|
||||||
|
value, ok = s.data[name]
|
||||||
|
s.mu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
# Bubble
|
||||||
|
|
||||||
|
[`new in v1.6.1`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.6.1)
|
||||||
|
|
||||||
|
Private format in some cameras from [dvr163.com](http://help.dvr163.com/) and [eseecloud.com](http://www.eseecloud.com/).
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
- you can skip `username`, `password`, `port`, `ch` and `stream` if they are default
|
||||||
|
- set up separate streams for different channels and streams
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
camera1: bubble://username:password@192.168.1.123:34567/bubble/live?ch=0&stream=0
|
||||||
|
```
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package bubble
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/bubble"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
streams.HandleFunc("bubble", func(source string) (core.Producer, error) {
|
||||||
|
return bubble.Dial(source)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# Debug
|
||||||
|
|
||||||
|
This module provides `GET /api/stack`, with which you can find hanging goroutines
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package debug
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
api.HandleFunc("api/stack", stackHandler)
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package debug
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
var stackSkip = [][]byte{
|
||||||
|
// main.go
|
||||||
|
[]byte("main.main()"),
|
||||||
|
[]byte("created by os/signal.Notify"),
|
||||||
|
|
||||||
|
// api/stack.go
|
||||||
|
[]byte("github.com/AlexxIT/go2rtc/internal/api.stackHandler"),
|
||||||
|
|
||||||
|
// api/api.go
|
||||||
|
[]byte("created by github.com/AlexxIT/go2rtc/internal/api.Init"),
|
||||||
|
[]byte("created by net/http.(*connReader).startBackgroundRead"),
|
||||||
|
[]byte("created by net/http.(*Server).Serve"), // TODO: why two?
|
||||||
|
|
||||||
|
[]byte("created by github.com/AlexxIT/go2rtc/internal/rtsp.Init"),
|
||||||
|
[]byte("created by github.com/AlexxIT/go2rtc/internal/srtp.Init"),
|
||||||
|
|
||||||
|
// homekit
|
||||||
|
[]byte("created by github.com/AlexxIT/go2rtc/internal/homekit.Init"),
|
||||||
|
|
||||||
|
// webrtc/api.go
|
||||||
|
[]byte("created by github.com/pion/ice/v4.NewTCPMuxDefault"),
|
||||||
|
[]byte("created by github.com/pion/ice/v4.NewUDPMuxDefault"),
|
||||||
|
}
|
||||||
|
|
||||||
|
func stackHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
sep := []byte("\n\n")
|
||||||
|
buf := make([]byte, 65535)
|
||||||
|
i := 0
|
||||||
|
n := runtime.Stack(buf, true)
|
||||||
|
skipped := 0
|
||||||
|
for _, item := range bytes.Split(buf[:n], sep) {
|
||||||
|
for _, skip := range stackSkip {
|
||||||
|
if bytes.Contains(item, skip) {
|
||||||
|
item = nil
|
||||||
|
skipped++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if item != nil {
|
||||||
|
i += copy(buf[i:], item)
|
||||||
|
i += copy(buf[i:], sep)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i += copy(buf[i:], fmt.Sprintf(
|
||||||
|
"Total: %d, Skipped: %d", runtime.NumGoroutine(), skipped),
|
||||||
|
)
|
||||||
|
|
||||||
|
api.Response(w, buf[:i], api.MimeText)
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
# Doorbird
|
||||||
|
|
||||||
|
[`new in v1.9.8`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.8)
|
||||||
|
|
||||||
|
This source type supports [Doorbird](https://www.doorbird.com/) devices including MJPEG stream, audio stream as well as two-way audio.
|
||||||
|
|
||||||
|
It is recommended to create a separate user within your doorbird setup for go2rtc. Minimum permissions for the user are:
|
||||||
|
|
||||||
|
- Watch always
|
||||||
|
- API operator
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
doorbird1:
|
||||||
|
- rtsp://admin:password@192.168.1.123:8557/mpeg/720p/media.amp # RTSP stream
|
||||||
|
- doorbird://admin:password@192.168.1.123?media=video # MJPEG stream
|
||||||
|
- doorbird://admin:password@192.168.1.123?media=audio # audio stream
|
||||||
|
- doorbird://admin:password@192.168.1.123 # two-way audio
|
||||||
|
```
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package doorbird
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/doorbird"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
streams.RedirectFunc("doorbird", func(rawURL string) (string, error) {
|
||||||
|
u, err := url.Parse(rawURL)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://www.doorbird.com/downloads/api_lan.pdf
|
||||||
|
switch u.Query().Get("media") {
|
||||||
|
case "video":
|
||||||
|
u.Path = "/bha-api/video.cgi"
|
||||||
|
case "audio":
|
||||||
|
u.Path = "/bha-api/audio-receive.cgi"
|
||||||
|
default:
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
u.Scheme = "http"
|
||||||
|
|
||||||
|
return u.String(), nil
|
||||||
|
})
|
||||||
|
|
||||||
|
streams.HandleFunc("doorbird", func(source string) (core.Producer, error) {
|
||||||
|
return doorbird.Dial(source)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
# DVR-IP
|
||||||
|
|
||||||
|
[`new in v1.2.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.2.0)
|
||||||
|
|
||||||
|
Private format from DVR-IP NVR, NetSurveillance, Sofia protocol (NETsurveillance ActiveX plugin XMeye SDK).
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
- you can skip `username`, `password`, `port`, `channel` and `subtype` if they are default
|
||||||
|
- set up separate streams for different channels
|
||||||
|
- use `subtype=0` for Main stream, and `subtype=1` for Extra1 stream
|
||||||
|
- only the TCP protocol is supported
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
only_stream: dvrip://username:password@192.168.1.123:34567?channel=0&subtype=0
|
||||||
|
only_tts: dvrip://username:password@192.168.1.123:34567?backchannel=1
|
||||||
|
two_way_audio:
|
||||||
|
- dvrip://username:password@192.168.1.123:34567?channel=0&subtype=0
|
||||||
|
- dvrip://username:password@192.168.1.123:34567?backchannel=1
|
||||||
|
```
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
package dvrip
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/dvrip"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
streams.HandleFunc("dvrip", dvrip.Dial)
|
||||||
|
|
||||||
|
// DVRIP client autodiscovery
|
||||||
|
api.HandleFunc("api/dvrip", apiDvrip)
|
||||||
|
}
|
||||||
|
|
||||||
|
const Port = 34569 // UDP port number for dvrip discovery
|
||||||
|
|
||||||
|
func apiDvrip(w http.ResponseWriter, r *http.Request) {
|
||||||
|
items, err := discover()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
api.ResponseSources(w, items)
|
||||||
|
}
|
||||||
|
|
||||||
|
func discover() ([]*api.Source, error) {
|
||||||
|
addr := &net.UDPAddr{
|
||||||
|
Port: Port,
|
||||||
|
IP: net.IP{239, 255, 255, 250},
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := net.ListenUDP("udp4", addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
go sendBroadcasts(conn)
|
||||||
|
|
||||||
|
var items []*api.Source
|
||||||
|
|
||||||
|
for _, info := range getResponses(conn) {
|
||||||
|
if info.HostIP == "" || info.HostName == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
host, err := hexToDecimalBytes(info.HostIP)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
items = append(items, &api.Source{
|
||||||
|
Name: info.HostName,
|
||||||
|
URL: "dvrip://user:pass@" + host + "?channel=0&subtype=0",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendBroadcasts(conn *net.UDPConn) {
|
||||||
|
// broadcasting the same multiple times because the devies some times don't answer
|
||||||
|
data, err := hex.DecodeString("ff00000000000000000000000000fa0500000000")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
addr := &net.UDPAddr{
|
||||||
|
Port: Port,
|
||||||
|
IP: net.IP{255, 255, 255, 255},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
_, _ = conn.WriteToUDP(data, addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
NetCommon NetCommon `json:"NetWork.NetCommon"`
|
||||||
|
Ret int `json:"Ret"`
|
||||||
|
SessionID string `json:"SessionID"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type NetCommon struct {
|
||||||
|
BuildDate string `json:"BuildDate"`
|
||||||
|
ChannelNum int `json:"ChannelNum"`
|
||||||
|
DeviceType int `json:"DeviceType"`
|
||||||
|
GateWay string `json:"GateWay"`
|
||||||
|
HostIP string `json:"HostIP"`
|
||||||
|
HostName string `json:"HostName"`
|
||||||
|
HttpPort int `json:"HttpPort"`
|
||||||
|
MAC string `json:"MAC"`
|
||||||
|
MonMode string `json:"MonMode"`
|
||||||
|
NetConnectState int `json:"NetConnectState"`
|
||||||
|
OtherFunction string `json:"OtherFunction"`
|
||||||
|
SN string `json:"SN"`
|
||||||
|
SSLPort int `json:"SSLPort"`
|
||||||
|
Submask string `json:"Submask"`
|
||||||
|
TCPMaxConn int `json:"TCPMaxConn"`
|
||||||
|
TCPPort int `json:"TCPPort"`
|
||||||
|
UDPPort int `json:"UDPPort"`
|
||||||
|
UseHSDownLoad bool `json:"UseHSDownLoad"`
|
||||||
|
Version string `json:"Version"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getResponses(conn *net.UDPConn) (infos []*NetCommon) {
|
||||||
|
if err := conn.SetReadDeadline(time.Now().Add(time.Second * 2)); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var ips []net.IP // processed IPs
|
||||||
|
|
||||||
|
b := make([]byte, 4096)
|
||||||
|
loop:
|
||||||
|
for {
|
||||||
|
n, addr, err := conn.ReadFromUDP(b)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ip := range ips {
|
||||||
|
if ip.Equal(addr.IP) {
|
||||||
|
continue loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if n <= 20+1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var msg Message
|
||||||
|
|
||||||
|
if err = json.Unmarshal(b[20:n-1], &msg); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
infos = append(infos, &msg.NetCommon)
|
||||||
|
ips = append(ips, addr.IP)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func hexToDecimalBytes(hexIP string) (string, error) {
|
||||||
|
b, err := hex.DecodeString(hexIP[2:]) // remove the '0x' prefix
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%d.%d.%d.%d", b[3], b[2], b[1], b[0]), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
# Echo
|
||||||
|
|
||||||
|
Some sources may have a dynamic link. And you will need to get it using a Bash or Python script. Your script should echo a link to the source. RTSP, FFmpeg or any of the supported sources.
|
||||||
|
|
||||||
|
**Docker** and **Home Assistant add-on** users have preinstalled `python3`, `curl`, `jq`.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
apple_hls: echo:python3 hls.py https://developer.apple.com/streaming/examples/basic-stream-osx-ios5.html
|
||||||
|
```
|
||||||
|
|
||||||
|
## Install python libraries
|
||||||
|
|
||||||
|
**Docker** and **Hass Add-on** users have preinstalled `python3` without any additional libraries, like [requests](https://requests.readthedocs.io/) or others. If you need some additional libraries - you need to install them to folder with your script:
|
||||||
|
|
||||||
|
1. Install [SSH & Web Terminal](https://github.com/hassio-addons/addon-ssh)
|
||||||
|
2. Goto Add-on Web UI
|
||||||
|
3. Install library: `pip install requests -t /config/echo`
|
||||||
|
4. Add your script to `/config/echo/myscript.py`
|
||||||
|
5. Use your script as source `echo:python3 /config/echo/myscript.py`
|
||||||
|
|
||||||
|
## Example: Apple HLS
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
apple_hls: echo:python3 hls.py https://developer.apple.com/streaming/examples/basic-stream-osx-ios5.html
|
||||||
|
```
|
||||||
|
|
||||||
|
**hls.py**
|
||||||
|
|
||||||
|
```python
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from urllib.parse import urljoin
|
||||||
|
from urllib.request import urlopen
|
||||||
|
|
||||||
|
html = urlopen(sys.argv[1]).read().decode("utf-8")
|
||||||
|
url = re.search(r"https.+?m3u8", html)[0]
|
||||||
|
|
||||||
|
html = urlopen(url).read().decode("utf-8")
|
||||||
|
m = re.search(r"^[a-z0-1/_]+\.m3u8$", html, flags=re.MULTILINE)
|
||||||
|
url = urljoin(url, m[0])
|
||||||
|
|
||||||
|
# ffmpeg:https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/gear1/prog_index.m3u8#video=copy
|
||||||
|
print("ffmpeg:" + url + "#video=copy")
|
||||||
|
```
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package echo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"os/exec"
|
||||||
|
"slices"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
var cfg struct {
|
||||||
|
Mod struct {
|
||||||
|
AllowPaths []string `yaml:"allow_paths"`
|
||||||
|
} `yaml:"echo"`
|
||||||
|
}
|
||||||
|
|
||||||
|
app.LoadConfig(&cfg)
|
||||||
|
|
||||||
|
allowPaths := cfg.Mod.AllowPaths
|
||||||
|
|
||||||
|
log := app.GetLogger("echo")
|
||||||
|
|
||||||
|
streams.RedirectFunc("echo", func(url string) (string, error) {
|
||||||
|
args := shell.QuoteSplit(url[5:])
|
||||||
|
|
||||||
|
if allowPaths != nil && !slices.Contains(allowPaths, args[0]) {
|
||||||
|
return "", errors.New("echo: bin not in allow_paths: " + args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := exec.Command(args[0], args[1:]...).Output()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
b = bytes.TrimSpace(b)
|
||||||
|
|
||||||
|
log.Debug().Str("url", url).Msgf("[echo] %s", b)
|
||||||
|
|
||||||
|
return string(b), nil
|
||||||
|
})
|
||||||
|
streams.MarkInsecure("echo")
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# EseeCloud
|
||||||
|
|
||||||
|
[`new in v1.9.10`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.10)
|
||||||
|
|
||||||
|
This source is for cameras with a link like this `http://admin:@192.168.1.123:80/livestream/12`. Related [issue](https://github.com/AlexxIT/go2rtc/issues/1690).
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
camera1: eseecloud://user:pass@192.168.1.123:80/livestream/12
|
||||||
|
```
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package eseecloud
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/eseecloud"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
streams.HandleFunc("eseecloud", eseecloud.Dial)
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
# Exec
|
||||||
|
|
||||||
|
Exec source can run any external application and expect data from it. Two transports are supported - **pipe** ([`new in v1.5.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.5.0)) and **RTSP**.
|
||||||
|
|
||||||
|
If you want to use **RTSP** transport, the command must contain the `{output}` argument in any place. On launch, it will be replaced by the local address of the RTSP server.
|
||||||
|
|
||||||
|
**pipe** reads data from app stdout in different formats: **MJPEG**, **H.264/H.265 bitstream**, **MPEG-TS**. Also pipe can write data to app stdin in two formats: **PCMA** and **PCM/48000**.
|
||||||
|
|
||||||
|
The source can be used with:
|
||||||
|
|
||||||
|
- [FFmpeg](https://ffmpeg.org/) - go2rtc ffmpeg source is just a shortcut to exec source
|
||||||
|
- [FFplay](https://ffmpeg.org/ffplay.html) - play audio on your server
|
||||||
|
- [GStreamer](https://gstreamer.freedesktop.org/)
|
||||||
|
- [Raspberry Pi Cameras](https://www.raspberrypi.com/documentation/computers/camera_software.html)
|
||||||
|
- any of your own software
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
Pipe commands support parameters (format: `exec:{command}#{param1}#{param2}`):
|
||||||
|
|
||||||
|
- `killsignal` - signal which will be sent to stop the process (numeric form)
|
||||||
|
- `killtimeout` - time in seconds for forced termination with sigkill
|
||||||
|
- `backchannel` - enable backchannel for two-way audio
|
||||||
|
- `starttimeout` - time in seconds for waiting first byte from RTSP
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
stream: exec:ffmpeg -re -i /media/BigBuckBunny.mp4 -c copy -rtsp_transport tcp -f rtsp {output}
|
||||||
|
picam_h264: exec:libcamera-vid -t 0 --inline -o -
|
||||||
|
picam_mjpeg: exec:libcamera-vid -t 0 --codec mjpeg -o -
|
||||||
|
pi5cam_h264: exec:libcamera-vid -t 0 --libav-format h264 -o -
|
||||||
|
canon: exec:gphoto2 --capture-movie --stdout#killsignal=2#killtimeout=5
|
||||||
|
play_pcma: exec:ffplay -fflags nobuffer -f alaw -ar 8000 -i -#backchannel=1
|
||||||
|
play_pcm48k: exec:ffplay -fflags nobuffer -f s16be -ar 48000 -i -#backchannel=1
|
||||||
|
```
|
||||||
|
|
||||||
|
## Backchannel
|
||||||
|
|
||||||
|
- You can check audio card names in the **Go2rtc > WebUI > Add**
|
||||||
|
- You can specify multiple backchannel lines with different codecs
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
sources:
|
||||||
|
two_way_audio_win:
|
||||||
|
- exec:ffmpeg -hide_banner -f dshow -i "audio=Microphone (High Definition Audio Device)" -c pcm_s16le -ar 16000 -ac 1 -f wav -
|
||||||
|
- exec:ffplay -nodisp -probesize 32 -f s16le -ar 16000 -#backchannel=1#audio=s16le/16000
|
||||||
|
- exec:ffplay -nodisp -probesize 32 -f alaw -ar 8000 -#backchannel=1#audio=alaw/8000
|
||||||
|
```
|
||||||
@@ -0,0 +1,279 @@
|
|||||||
|
package exec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/rtsp"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/magic"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/pcm"
|
||||||
|
pkg "github.com/AlexxIT/go2rtc/pkg/rtsp"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
var cfg struct {
|
||||||
|
Mod struct {
|
||||||
|
AllowPaths []string `yaml:"allow_paths"`
|
||||||
|
} `yaml:"exec"`
|
||||||
|
}
|
||||||
|
|
||||||
|
app.LoadConfig(&cfg)
|
||||||
|
|
||||||
|
allowPaths = cfg.Mod.AllowPaths
|
||||||
|
|
||||||
|
rtsp.HandleFunc(func(conn *pkg.Conn) bool {
|
||||||
|
waitersMu.Lock()
|
||||||
|
waiter := waiters[conn.URL.Path]
|
||||||
|
waitersMu.Unlock()
|
||||||
|
|
||||||
|
if waiter == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// unblocking write to channel
|
||||||
|
select {
|
||||||
|
case waiter <- conn:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
streams.HandleFunc("exec", execHandle)
|
||||||
|
streams.MarkInsecure("exec")
|
||||||
|
|
||||||
|
log = app.GetLogger("exec")
|
||||||
|
}
|
||||||
|
|
||||||
|
var allowPaths []string
|
||||||
|
|
||||||
|
func execHandle(rawURL string) (prod core.Producer, err error) {
|
||||||
|
rawURL, rawQuery, _ := strings.Cut(rawURL, "#")
|
||||||
|
query := streams.ParseQuery(rawQuery)
|
||||||
|
|
||||||
|
var path string
|
||||||
|
|
||||||
|
// RTSP flow should have `{output}` inside URL
|
||||||
|
// pipe flow may have `#{params}` inside URL
|
||||||
|
if i := strings.Index(rawURL, "{output}"); i > 0 {
|
||||||
|
if rtsp.Port == "" {
|
||||||
|
return nil, errors.New("exec: rtsp module disabled")
|
||||||
|
}
|
||||||
|
|
||||||
|
sum := md5.Sum([]byte(rawURL))
|
||||||
|
path = "/" + hex.EncodeToString(sum[:])
|
||||||
|
rawURL = rawURL[:i] + "rtsp://127.0.0.1:" + rtsp.Port + path + rawURL[i+8:]
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := shell.NewCommand(rawURL[5:]) // remove `exec:`
|
||||||
|
cmd.Stderr = &logWriter{
|
||||||
|
buf: make([]byte, 512),
|
||||||
|
debug: log.Debug().Enabled(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if allowPaths != nil && !slices.Contains(allowPaths, cmd.Args[0]) {
|
||||||
|
_ = cmd.Close()
|
||||||
|
return nil, errors.New("exec: bin not in allow_paths: " + cmd.Args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
if s := query.Get("killsignal"); s != "" {
|
||||||
|
sig := syscall.Signal(core.Atoi(s))
|
||||||
|
cmd.Cancel = func() error {
|
||||||
|
log.Debug().Msgf("[exec] kill with signal=%d", sig)
|
||||||
|
return cmd.Process.Signal(sig)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if s := query.Get("killtimeout"); s != "" {
|
||||||
|
cmd.WaitDelay = time.Duration(core.Atoi(s)) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
if query.Get("backchannel") == "1" {
|
||||||
|
return pcm.NewBackchannel(cmd, query.Get("audio"))
|
||||||
|
}
|
||||||
|
|
||||||
|
var timeout time.Duration
|
||||||
|
if s := query.Get("starttimeout"); s != "" {
|
||||||
|
timeout = time.Duration(core.Atoi(s)) * time.Second
|
||||||
|
} else {
|
||||||
|
timeout = 30 * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
if path == "" {
|
||||||
|
prod, err = handlePipe(rawURL, cmd)
|
||||||
|
} else {
|
||||||
|
prod, err = handleRTSP(rawURL, cmd, path, timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
_ = cmd.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlePipe(source string, cmd *shell.Command) (core.Producer, error) {
|
||||||
|
stdout, err := cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rd := struct {
|
||||||
|
io.Reader
|
||||||
|
io.Closer
|
||||||
|
}{
|
||||||
|
// add buffer for pipe reader to reduce syscall
|
||||||
|
bufio.NewReaderSize(stdout, core.BufferSize),
|
||||||
|
// stop cmd on close pipe call
|
||||||
|
cmd,
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Strs("args", cmd.Args).Msg("[exec] run pipe")
|
||||||
|
|
||||||
|
ts := time.Now()
|
||||||
|
|
||||||
|
if err = cmd.Start(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
prod, err := magic.Open(rd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("exec/pipe: %w\n%s", err, cmd.Stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if info, ok := prod.(core.Info); ok {
|
||||||
|
info.SetProtocol("pipe")
|
||||||
|
setRemoteInfo(info, source, cmd.Args)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Stringer("launch", time.Since(ts)).Msg("[exec] run pipe")
|
||||||
|
|
||||||
|
return prod, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleRTSP(source string, cmd *shell.Command, path string, timeout time.Duration) (core.Producer, error) {
|
||||||
|
if log.Trace().Enabled() {
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
}
|
||||||
|
|
||||||
|
waiter := make(chan *pkg.Conn, 1)
|
||||||
|
|
||||||
|
waitersMu.Lock()
|
||||||
|
waiters[path] = waiter
|
||||||
|
waitersMu.Unlock()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
waitersMu.Lock()
|
||||||
|
delete(waiters, path)
|
||||||
|
waitersMu.Unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
log.Debug().Strs("args", cmd.Args).Msg("[exec] run rtsp")
|
||||||
|
|
||||||
|
ts := time.Now()
|
||||||
|
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
log.Error().Err(err).Str("source", source).Msg("[exec]")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
timer := time.NewTimer(timeout)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-timer.C:
|
||||||
|
// haven't received data from app in timeout
|
||||||
|
log.Error().Str("source", source).Msg("[exec] timeout")
|
||||||
|
return nil, errors.New("exec: timeout")
|
||||||
|
case <-cmd.Done():
|
||||||
|
// app fail before we receive any data
|
||||||
|
return nil, fmt.Errorf("exec/rtsp\n%s", cmd.Stderr)
|
||||||
|
case prod := <-waiter:
|
||||||
|
// app started successfully
|
||||||
|
log.Debug().Stringer("launch", time.Since(ts)).Msg("[exec] run rtsp")
|
||||||
|
setRemoteInfo(prod, source, cmd.Args)
|
||||||
|
prod.OnClose = cmd.Close
|
||||||
|
return prod, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// internal
|
||||||
|
|
||||||
|
var (
|
||||||
|
log zerolog.Logger
|
||||||
|
waiters = make(map[string]chan *pkg.Conn)
|
||||||
|
waitersMu sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
|
type logWriter struct {
|
||||||
|
buf []byte
|
||||||
|
debug bool
|
||||||
|
n int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logWriter) String() string {
|
||||||
|
if l.n == len(l.buf) {
|
||||||
|
return string(l.buf) + "..."
|
||||||
|
}
|
||||||
|
return string(l.buf[:l.n])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logWriter) Write(p []byte) (n int, err error) {
|
||||||
|
if l.n < cap(l.buf) {
|
||||||
|
l.n += copy(l.buf[l.n:], p)
|
||||||
|
}
|
||||||
|
n = len(p)
|
||||||
|
if l.debug {
|
||||||
|
if p = trimSpace(p); p != nil {
|
||||||
|
log.Debug().Msgf("[exec] %s", p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimSpace(b []byte) []byte {
|
||||||
|
start := 0
|
||||||
|
stop := len(b)
|
||||||
|
for ; start < stop; start++ {
|
||||||
|
if b[start] >= ' ' {
|
||||||
|
break // trim all ASCII before 0x20
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for ; ; stop-- {
|
||||||
|
if stop == start {
|
||||||
|
return nil // skip empty output
|
||||||
|
}
|
||||||
|
if b[stop-1] > ' ' {
|
||||||
|
break // trim all ASCII before 0x21
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b[start:stop]
|
||||||
|
}
|
||||||
|
|
||||||
|
func setRemoteInfo(info core.Info, source string, args []string) {
|
||||||
|
info.SetSource(source)
|
||||||
|
|
||||||
|
if i := core.Index(args, "-i"); i > 0 && i < len(args)-1 {
|
||||||
|
rawURL := args[i+1]
|
||||||
|
if u, err := url.Parse(rawURL); err == nil && u.Host != "" {
|
||||||
|
info.SetRemoteAddr(u.Host)
|
||||||
|
info.SetURL(rawURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
# Expr
|
||||||
|
|
||||||
|
[`new in v1.8.2`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.8.2)
|
||||||
|
|
||||||
|
[Expr](https://github.com/antonmedv/expr) - expression language and expression evaluation for Go.
|
||||||
|
|
||||||
|
- [language definition](https://expr.medv.io/docs/Language-Definition) - takes best from JS, Python, Jinja2 syntax
|
||||||
|
- your expression should return a link of any supported source
|
||||||
|
- expression supports multiple operation, but:
|
||||||
|
- all operations must be separated by a semicolon
|
||||||
|
- all operations, except the last one, must declare a new variable (`let s = "abc";`)
|
||||||
|
- the last operation should return a string
|
||||||
|
- go2rtc supports additional functions:
|
||||||
|
- `fetch` - JS-like HTTP requests
|
||||||
|
- `match` - JS-like RegExp queries
|
||||||
|
|
||||||
|
## Fetch examples
|
||||||
|
|
||||||
|
Multiple fetch requests are executed within a single session. They share the same cookie.
|
||||||
|
|
||||||
|
**HTTP GET**
|
||||||
|
|
||||||
|
```js
|
||||||
|
var r = fetch('https://example.org/products.json');
|
||||||
|
```
|
||||||
|
|
||||||
|
**HTTP POST JSON**
|
||||||
|
|
||||||
|
```js
|
||||||
|
var r = fetch('https://example.org/post', {
|
||||||
|
method: 'POST',
|
||||||
|
// Content-Type: application/json will be set automatically
|
||||||
|
json: {username: 'example'}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
**HTTP POST Form**
|
||||||
|
|
||||||
|
```js
|
||||||
|
var r = fetch('https://example.org/post', {
|
||||||
|
method: 'POST',
|
||||||
|
// Content-Type: application/x-www-form-urlencoded will be set automatically
|
||||||
|
data: {username: 'example', password: 'password'}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Script examples
|
||||||
|
|
||||||
|
**Two way audio for Dahua VTO**
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
dahua_vto: |
|
||||||
|
expr:
|
||||||
|
let host = 'admin:password@192.168.1.123';
|
||||||
|
|
||||||
|
var r = fetch('http://' + host + '/cgi-bin/configManager.cgi?action=setConfig&Encode[0].MainFormat[0].Audio.Compression=G.711A&Encode[0].MainFormat[0].Audio.Frequency=8000');
|
||||||
|
|
||||||
|
'rtsp://' + host + '/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif'
|
||||||
|
```
|
||||||
|
|
||||||
|
**dom.ru**
|
||||||
|
|
||||||
|
You can get credentials from https://github.com/ad/domru
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
dom_ru: |
|
||||||
|
expr:
|
||||||
|
let camera = '***';
|
||||||
|
let token = '***';
|
||||||
|
let operator = '***';
|
||||||
|
|
||||||
|
fetch('https://myhome.proptech.ru/rest/v1/forpost/cameras/' + camera + '/video', {
|
||||||
|
headers: {
|
||||||
|
'Authorization': 'Bearer ' + token,
|
||||||
|
'User-Agent': 'Google sdkgphone64x8664 | Android 14 | erth | 8.26.0 (82600010) | 0 | 0 | 0',
|
||||||
|
'Operator': operator
|
||||||
|
}
|
||||||
|
}).json().data.URL
|
||||||
|
```
|
||||||
|
|
||||||
|
**dom.ufanet.ru**
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
ufanet_ru: |
|
||||||
|
expr:
|
||||||
|
let username = '***';
|
||||||
|
let password = '***';
|
||||||
|
let cameraid = '***';
|
||||||
|
|
||||||
|
let r1 = fetch('https://ucams.ufanet.ru/api/internal/login/', {
|
||||||
|
method: 'POST',
|
||||||
|
data: {username: username, password: password}
|
||||||
|
});
|
||||||
|
let r2 = fetch('https://ucams.ufanet.ru/api/v0/cameras/this/?lang=ru', {
|
||||||
|
method: 'POST',
|
||||||
|
json: {'fields': ['token_l', 'server'], 'token_l_ttl': 3600, 'numbers': [cameraid]},
|
||||||
|
}).json().results[0];
|
||||||
|
|
||||||
|
'rtsp://' + r2.server.domain + '/' + r2.number + '?token=' + r2.token_l
|
||||||
|
```
|
||||||
|
|
||||||
|
**Parse HLS files from Apple**
|
||||||
|
|
||||||
|
Same example in two languages - python and expr.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
example_python: |
|
||||||
|
echo:python -c 'from urllib.request import urlopen; import re
|
||||||
|
|
||||||
|
# url1 = "https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/bipbop_16x9_variant.m3u8"
|
||||||
|
html1 = urlopen("https://developer.apple.com/streaming/examples/basic-stream-osx-ios5.html").read().decode("utf-8")
|
||||||
|
url1 = re.search(r"https.+?m3u8", html1)[0]
|
||||||
|
|
||||||
|
# url2 = "gear1/prog_index.m3u8"
|
||||||
|
html2 = urlopen(url1).read().decode("utf-8")
|
||||||
|
url2 = re.search(r"^[a-z0-1/_]+\.m3u8$", html2, flags=re.MULTILINE)[0]
|
||||||
|
|
||||||
|
# url3 = "https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/gear1/prog_index.m3u8"
|
||||||
|
url3 = url1[:url1.rindex("/")+1] + url2
|
||||||
|
|
||||||
|
print("ffmpeg:" + url3 + "#video=copy")'
|
||||||
|
|
||||||
|
example_expr: |
|
||||||
|
expr:
|
||||||
|
|
||||||
|
let html1 = fetch("https://developer.apple.com/streaming/examples/basic-stream-osx-ios5.html").text;
|
||||||
|
let url1 = match(html1, "https.+?m3u8")[0];
|
||||||
|
|
||||||
|
let html2 = fetch(url1).text;
|
||||||
|
let url2 = match(html2, "^[a-z0-1/_]+\\.m3u8$", "m")[0];
|
||||||
|
|
||||||
|
let url3 = url1[:lastIndexOf(url1, "/")+1] + url2;
|
||||||
|
|
||||||
|
"ffmpeg:" + url3 + "#video=copy"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Comparison
|
||||||
|
|
||||||
|
| expr | python | js |
|
||||||
|
|------------------------------|----------------------------|--------------------------------|
|
||||||
|
| let x = 1; | x = 1 | let x = 1 |
|
||||||
|
| {a: 1, b: 2} | {"a": 1, "b": 2} | {a: 1, b: 2} |
|
||||||
|
| let r = fetch(url, {method}) | r = request(method, url) | r = await fetch(url, {method}) |
|
||||||
|
| r.ok | r.ok | r.ok |
|
||||||
|
| r.status | r.status_code | r.status |
|
||||||
|
| r.text | r.text | await r.text() |
|
||||||
|
| r.json() | r.json() | await r.json() |
|
||||||
|
| r.headers | r.headers | r.headers |
|
||||||
|
| let m = match(text, "abc") | m = re.search("abc", text) | let m = text.match(/abc/) |
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package expr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/expr"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
log := app.GetLogger("expr")
|
||||||
|
|
||||||
|
streams.RedirectFunc("expr", func(url string) (string, error) {
|
||||||
|
v, err := expr.Eval(url[5:], nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Msgf("[expr] url=%s", url)
|
||||||
|
|
||||||
|
if url = v.(string); url == "" {
|
||||||
|
return "", errors.New("expr: result is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
return url, nil
|
||||||
|
})
|
||||||
|
streams.MarkInsecure("expr")
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
# FFmpeg
|
||||||
|
|
||||||
|
You can get any stream, file or device via FFmpeg and push it to go2rtc. The app will automatically start FFmpeg with the proper arguments when someone starts watching the stream.
|
||||||
|
|
||||||
|
- FFmpeg preinstalled for **Docker** and **Home Assistant add-on** users
|
||||||
|
- **Home Assistant add-on** users can target files from [/media](https://www.home-assistant.io/more-info/local-media/setup-media/) folder
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
Format: `ffmpeg:{input}#{param1}#{param2}#{param3}`. Examples:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
# [FILE] all tracks will be copied without transcoding codecs
|
||||||
|
file1: ffmpeg:/media/BigBuckBunny.mp4
|
||||||
|
|
||||||
|
# [FILE] video will be transcoded to H264, audio will be skipped
|
||||||
|
file2: ffmpeg:/media/BigBuckBunny.mp4#video=h264
|
||||||
|
|
||||||
|
# [FILE] video will be copied, audio will be transcoded to PCMU
|
||||||
|
file3: ffmpeg:/media/BigBuckBunny.mp4#video=copy#audio=pcmu
|
||||||
|
|
||||||
|
# [HLS] video will be copied, audio will be skipped
|
||||||
|
hls: ffmpeg:https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/gear5/prog_index.m3u8#video=copy
|
||||||
|
|
||||||
|
# [MJPEG] video will be transcoded to H264
|
||||||
|
mjpeg: ffmpeg:http://185.97.122.128/cgi-bin/faststream.jpg#video=h264
|
||||||
|
|
||||||
|
# [RTSP] video with rotation, should be transcoded, so select H264
|
||||||
|
rotate: ffmpeg:rtsp://12345678@192.168.1.123/av_stream/ch0#video=h264#rotate=90
|
||||||
|
```
|
||||||
|
|
||||||
|
All transcoding formats have [built-in templates](ffmpeg.go): `h264`, `h265`, `opus`, `pcmu`, `pcmu/16000`, `pcmu/48000`, `pcma`, `pcma/16000`, `pcma/48000`, `aac`, `aac/16000`.
|
||||||
|
|
||||||
|
But you can override them via YAML config. You can also add your own formats to the config and use them with source params.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
ffmpeg:
|
||||||
|
bin: ffmpeg # path to ffmpeg binary
|
||||||
|
global: "-hide_banner"
|
||||||
|
timeout: 5 # default timeout in seconds for rtsp inputs
|
||||||
|
h264: "-codec:v libx264 -g:v 30 -preset:v superfast -tune:v zerolatency -profile:v main -level:v 4.1"
|
||||||
|
mycodec: "-any args that supported by ffmpeg..."
|
||||||
|
myinput: "-fflags nobuffer -flags low_delay -timeout {timeout} -i {input}"
|
||||||
|
myraw: "-ss 00:00:20"
|
||||||
|
```
|
||||||
|
|
||||||
|
- You can use go2rtc stream name as ffmpeg input (ex. `ffmpeg:camera1#video=h264`)
|
||||||
|
- You can use `video` and `audio` params multiple times (ex. `#video=copy#audio=copy#audio=pcmu`)
|
||||||
|
- You can use `rotate` param with `90`, `180`, `270` or `-90` values, important with transcoding (ex. `#video=h264#rotate=90`)
|
||||||
|
- You can use `width` and/or `height` params, important with transcoding (ex. `#video=h264#width=1280`)
|
||||||
|
- You can use `drawtext` to add a timestamp (ex. `drawtext=x=2:y=2:fontsize=12:fontcolor=white:box=1:boxcolor=black`)
|
||||||
|
- This will greatly increase the CPU of the server, even with hardware acceleration
|
||||||
|
- You can use `timeout` param to set RTSP input timeout in seconds (ex. `#timeout=10`)
|
||||||
|
- You can use `raw` param for any additional FFmpeg arguments (ex. `#raw=-vf transpose=1`)
|
||||||
|
- You can use `input` param to override default input template (ex. `#input=rtsp/udp` will change RTSP transport from TCP to UDP+TCP)
|
||||||
|
- You can use raw input value (ex. `#input=-timeout {timeout} -i {input}`)
|
||||||
|
- You can add your own input templates
|
||||||
|
|
||||||
|
Read more about [hardware acceleration](hardware/README.md).
|
||||||
|
|
||||||
|
**PS.** It is recommended to check the available hardware in the WebUI add page.
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
)
|
||||||
|
|
||||||
|
func apiFFmpeg(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "POST" {
|
||||||
|
http.Error(w, "", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
query := r.URL.Query()
|
||||||
|
dst := query.Get("dst")
|
||||||
|
stream := streams.Get(dst)
|
||||||
|
if stream == nil {
|
||||||
|
http.Error(w, "", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var src string
|
||||||
|
if s := query.Get("file"); s != "" {
|
||||||
|
if streams.Validate(s) == nil {
|
||||||
|
src = "ffmpeg:" + s + "#audio=auto#input=file"
|
||||||
|
}
|
||||||
|
} else if s = query.Get("live"); s != "" {
|
||||||
|
if streams.Validate(s) == nil {
|
||||||
|
src = "ffmpeg:" + s + "#audio=auto"
|
||||||
|
}
|
||||||
|
} else if s = query.Get("text"); s != "" {
|
||||||
|
if strings.IndexAny(s, `'"&%$`) < 0 {
|
||||||
|
src = "ffmpeg:tts?text=" + s
|
||||||
|
if s = query.Get("voice"); s != "" {
|
||||||
|
src += "&voice=" + s
|
||||||
|
}
|
||||||
|
src += "#audio=auto"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if src == "" {
|
||||||
|
http.Error(w, "", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := stream.Play(src); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
# FFmpeg Device
|
||||||
|
|
||||||
|
You can get video from any USB camera or Webcam as RTSP or WebRTC stream. This is part of FFmpeg integration.
|
||||||
|
|
||||||
|
- check available devices in web interface
|
||||||
|
- `video_size` and `framerate` must be supported by your camera!
|
||||||
|
- for Linux supported only video for now
|
||||||
|
- for macOS you can stream FaceTime camera or whole desktop!
|
||||||
|
- for macOS important to set right framerate
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
Format: `ffmpeg:device?{input-params}#{param1}#{param2}#{param3}`
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
linux_usbcam: ffmpeg:device?video=0&video_size=1280x720#video=h264
|
||||||
|
windows_webcam: ffmpeg:device?video=0#video=h264
|
||||||
|
macos_facetime: ffmpeg:device?video=0&audio=1&video_size=1280x720&framerate=30#video=h264#audio=pcma
|
||||||
|
```
|
||||||
|
|
||||||
|
**PS.** It is recommended to check the available devices in the WebUI add page.
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
//go:build freebsd || netbsd || openbsd || dragonfly
|
||||||
|
|
||||||
|
package device
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
func queryToInput(query url.Values) string {
|
||||||
|
if video := query.Get("video"); video != "" {
|
||||||
|
// https://ffmpeg.org/ffmpeg-devices.html#video4linux2_002c-v4l2
|
||||||
|
input := "-f v4l2"
|
||||||
|
|
||||||
|
for key, value := range query {
|
||||||
|
switch key {
|
||||||
|
case "resolution":
|
||||||
|
input += " -video_size " + value[0]
|
||||||
|
case "video_size", "pixel_format", "input_format", "framerate", "use_libv4l2":
|
||||||
|
input += " -" + key + " " + value[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return input + " -i " + indexToItem(videos, video)
|
||||||
|
}
|
||||||
|
|
||||||
|
if audio := query.Get("audio"); audio != "" {
|
||||||
|
input := "-f oss"
|
||||||
|
|
||||||
|
for key, value := range query {
|
||||||
|
switch key {
|
||||||
|
case "channels", "sample_rate":
|
||||||
|
input += " -" + key + " " + value[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return input + " -i " + indexToItem(audios, audio)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDevices() {
|
||||||
|
files, err := os.ReadDir("/dev")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
if !strings.HasPrefix(file.Name(), core.KindVideo) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
name := "/dev/" + file.Name()
|
||||||
|
|
||||||
|
cmd := exec.Command(
|
||||||
|
Bin, "-hide_banner", "-f", "v4l2", "-list_formats", "all", "-i", name,
|
||||||
|
)
|
||||||
|
b, _ := cmd.CombinedOutput()
|
||||||
|
|
||||||
|
// [video4linux2,v4l2 @ 0x860b92280] Raw : yuyv422 : YUYV 4:2:2 : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960
|
||||||
|
// [video4linux2,v4l2 @ 0x860b92280] Compressed: mjpeg : Motion-JPEG : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960
|
||||||
|
re := regexp.MustCompile("(Raw *|Compressed): +(.+?) : +(.+?) : (.+)")
|
||||||
|
m := re.FindAllStringSubmatch(string(b), -1)
|
||||||
|
for _, i := range m {
|
||||||
|
size, _, _ := strings.Cut(i[4], " ")
|
||||||
|
stream := &api.Source{
|
||||||
|
Name: i[3],
|
||||||
|
Info: i[4],
|
||||||
|
URL: "ffmpeg:device?video=" + name + "&input_format=" + i[2] + "&video_size=" + size,
|
||||||
|
}
|
||||||
|
|
||||||
|
if i[1] != "Compressed" {
|
||||||
|
stream.URL += "#video=h264#hardware"
|
||||||
|
}
|
||||||
|
|
||||||
|
videos = append(videos, name)
|
||||||
|
streams = append(streams, stream)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = exec.Command(Bin, "-f", "oss", "-i", "/dev/dsp", "-t", "1", "-f", "null", "-").Run()
|
||||||
|
if err == nil {
|
||||||
|
stream := &api.Source{
|
||||||
|
Name: "OSS default",
|
||||||
|
Info: " ",
|
||||||
|
URL: "ffmpeg:device?audio=default&channels=1&sample_rate=16000&#audio=opus",
|
||||||
|
}
|
||||||
|
|
||||||
|
audios = append(audios, "default")
|
||||||
|
streams = append(streams, stream)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
//go:build darwin || ios
|
||||||
|
|
||||||
|
package device
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
func queryToInput(query url.Values) string {
|
||||||
|
video := query.Get("video")
|
||||||
|
audio := query.Get("audio")
|
||||||
|
|
||||||
|
if video == "" && audio == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://ffmpeg.org/ffmpeg-devices.html#avfoundation
|
||||||
|
input := "-f avfoundation"
|
||||||
|
|
||||||
|
if video != "" {
|
||||||
|
video = indexToItem(videos, video)
|
||||||
|
|
||||||
|
for key, value := range query {
|
||||||
|
switch key {
|
||||||
|
case "resolution":
|
||||||
|
input += " -video_size " + value[0]
|
||||||
|
case "pixel_format", "framerate", "video_size", "capture_cursor", "capture_mouse_clicks", "capture_raw_data":
|
||||||
|
input += " -" + key + " " + value[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if audio != "" {
|
||||||
|
audio = indexToItem(audios, audio)
|
||||||
|
}
|
||||||
|
|
||||||
|
return input + ` -i "` + video + `:` + audio + `"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDevices() {
|
||||||
|
// [AVFoundation indev @ 0x147f04510] AVFoundation video devices:
|
||||||
|
// [AVFoundation indev @ 0x147f04510] [0] FaceTime HD Camera
|
||||||
|
// [AVFoundation indev @ 0x147f04510] [1] Capture screen 0
|
||||||
|
// [AVFoundation indev @ 0x147f04510] AVFoundation audio devices:
|
||||||
|
// [AVFoundation indev @ 0x147f04510] [0] MacBook Pro Microphone
|
||||||
|
cmd := exec.Command(
|
||||||
|
Bin, "-hide_banner", "-list_devices", "true", "-f", "avfoundation", "-i", "",
|
||||||
|
)
|
||||||
|
b, _ := cmd.CombinedOutput()
|
||||||
|
|
||||||
|
re := regexp.MustCompile(`\[\d+] (.+)`)
|
||||||
|
|
||||||
|
var kind string
|
||||||
|
for _, line := range strings.Split(string(b), "\n") {
|
||||||
|
switch {
|
||||||
|
case strings.HasSuffix(line, "video devices:"):
|
||||||
|
kind = core.KindVideo
|
||||||
|
continue
|
||||||
|
case strings.HasSuffix(line, "audio devices:"):
|
||||||
|
kind = core.KindAudio
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
m := re.FindStringSubmatch(line)
|
||||||
|
if m == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
name := m[1]
|
||||||
|
|
||||||
|
switch kind {
|
||||||
|
case core.KindVideo:
|
||||||
|
videos = append(videos, name)
|
||||||
|
case core.KindAudio:
|
||||||
|
audios = append(audios, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
streams = append(streams, &api.Source{
|
||||||
|
Name: name, URL: "ffmpeg:device?" + kind + "=" + name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
//go:build unix && !darwin && !freebsd && !netbsd && !openbsd && !dragonfly
|
||||||
|
|
||||||
|
package device
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
func queryToInput(query url.Values) string {
|
||||||
|
if video := query.Get("video"); video != "" {
|
||||||
|
// https://ffmpeg.org/ffmpeg-devices.html#video4linux2_002c-v4l2
|
||||||
|
input := "-f v4l2"
|
||||||
|
|
||||||
|
for key, value := range query {
|
||||||
|
switch key {
|
||||||
|
case "resolution":
|
||||||
|
input += " -video_size " + value[0]
|
||||||
|
case "video_size", "pixel_format", "input_format", "framerate", "use_libv4l2":
|
||||||
|
input += " -" + key + " " + value[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return input + " -i " + indexToItem(videos, video)
|
||||||
|
}
|
||||||
|
|
||||||
|
if audio := query.Get("audio"); audio != "" {
|
||||||
|
// https://trac.ffmpeg.org/wiki/Capture/ALSA
|
||||||
|
input := "-f alsa"
|
||||||
|
|
||||||
|
for key, value := range query {
|
||||||
|
switch key {
|
||||||
|
case "channels", "sample_rate":
|
||||||
|
input += " -" + key + " " + value[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return input + " -i " + indexToItem(audios, audio)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDevices() {
|
||||||
|
files, err := os.ReadDir("/dev")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
if !strings.HasPrefix(file.Name(), core.KindVideo) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
name := "/dev/" + file.Name()
|
||||||
|
|
||||||
|
cmd := exec.Command(
|
||||||
|
Bin, "-hide_banner", "-f", "v4l2", "-list_formats", "all", "-i", name,
|
||||||
|
)
|
||||||
|
b, _ := cmd.CombinedOutput()
|
||||||
|
|
||||||
|
// [video4linux2,v4l2 @ 0x204e1c0] Compressed: mjpeg : Motion-JPEG : 640x360 1280x720 1920x1080
|
||||||
|
// [video4linux2,v4l2 @ 0x204e1c0] Raw : yuyv422 : YUYV 4:2:2 : 640x360 1280x720 1920x1080
|
||||||
|
// [video4linux2,v4l2 @ 0x204e1c0] Compressed: h264 : H.264 : 640x360 1280x720 1920x1080
|
||||||
|
re := regexp.MustCompile("(Raw *|Compressed): +(.+?) : +(.+?) : (.+)")
|
||||||
|
m := re.FindAllStringSubmatch(string(b), -1)
|
||||||
|
for _, i := range m {
|
||||||
|
size, _, _ := strings.Cut(i[4], " ")
|
||||||
|
stream := &api.Source{
|
||||||
|
Name: i[3],
|
||||||
|
Info: i[4],
|
||||||
|
URL: "ffmpeg:device?video=" + name + "&input_format=" + i[2] + "&video_size=" + size,
|
||||||
|
}
|
||||||
|
|
||||||
|
if i[1] != "Compressed" {
|
||||||
|
stream.URL += "#video=h264#hardware"
|
||||||
|
}
|
||||||
|
|
||||||
|
videos = append(videos, name)
|
||||||
|
streams = append(streams, stream)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = exec.Command(Bin, "-f", "alsa", "-i", "default", "-t", "1", "-f", "null", "-").Run()
|
||||||
|
if err == nil {
|
||||||
|
stream := &api.Source{
|
||||||
|
Name: "ALSA default",
|
||||||
|
Info: " ",
|
||||||
|
URL: "ffmpeg:device?audio=default&channels=1&sample_rate=16000&#audio=opus",
|
||||||
|
}
|
||||||
|
|
||||||
|
audios = append(audios, "default")
|
||||||
|
streams = append(streams, stream)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
//go:build windows
|
||||||
|
|
||||||
|
package device
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
func queryToInput(query url.Values) string {
|
||||||
|
video := query.Get("video")
|
||||||
|
audio := query.Get("audio")
|
||||||
|
|
||||||
|
if video == "" && audio == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://ffmpeg.org/ffmpeg-devices.html#dshow
|
||||||
|
input := "-f dshow"
|
||||||
|
|
||||||
|
if video != "" {
|
||||||
|
video = indexToItem(videos, video)
|
||||||
|
|
||||||
|
for key, value := range query {
|
||||||
|
switch key {
|
||||||
|
case "resolution":
|
||||||
|
input += " -video_size " + value[0]
|
||||||
|
case "video_size", "framerate", "pixel_format":
|
||||||
|
input += " -" + key + " " + value[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if audio != "" {
|
||||||
|
audio = indexToItem(audios, audio)
|
||||||
|
|
||||||
|
for key, value := range query {
|
||||||
|
switch key {
|
||||||
|
case "sample_rate", "sample_size", "channels", "audio_buffer_size":
|
||||||
|
input += " -" + key + " " + value[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if video != "" {
|
||||||
|
input += ` -i "video=` + video
|
||||||
|
|
||||||
|
if audio != "" {
|
||||||
|
input += `:audio=` + audio
|
||||||
|
}
|
||||||
|
|
||||||
|
input += `"`
|
||||||
|
} else {
|
||||||
|
input += ` -i "audio=` + audio + `"`
|
||||||
|
}
|
||||||
|
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDevices() {
|
||||||
|
cmd := exec.Command(
|
||||||
|
Bin, "-hide_banner", "-list_devices", "true", "-f", "dshow", "-i", "",
|
||||||
|
)
|
||||||
|
b, _ := cmd.CombinedOutput()
|
||||||
|
|
||||||
|
re := regexp.MustCompile(`"([^"]+)" \((video|audio)\)`)
|
||||||
|
for _, m := range re.FindAllStringSubmatch(string(b), -1) {
|
||||||
|
name := m[1]
|
||||||
|
kind := m[2]
|
||||||
|
|
||||||
|
stream := &api.Source{
|
||||||
|
Name: name, URL: "ffmpeg:device?" + kind + "=" + name,
|
||||||
|
}
|
||||||
|
|
||||||
|
switch kind {
|
||||||
|
case core.KindVideo:
|
||||||
|
videos = append(videos, name)
|
||||||
|
stream.URL += "#video=h264#hardware"
|
||||||
|
case core.KindAudio:
|
||||||
|
audios = append(audios, name)
|
||||||
|
stream.URL += "&channels=1&sample_rate=16000&audio_buffer_size=10"
|
||||||
|
}
|
||||||
|
|
||||||
|
streams = append(streams, stream)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package device
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init(bin string) {
|
||||||
|
Bin = bin
|
||||||
|
|
||||||
|
api.HandleFunc("api/ffmpeg/devices", apiDevices)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetInput(src string) string {
|
||||||
|
query, err := url.ParseQuery(src)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
runonce.Do(initDevices)
|
||||||
|
|
||||||
|
return queryToInput(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
var Bin string
|
||||||
|
|
||||||
|
var videos, audios []string
|
||||||
|
var streams []*api.Source
|
||||||
|
var runonce sync.Once
|
||||||
|
|
||||||
|
func apiDevices(w http.ResponseWriter, r *http.Request) {
|
||||||
|
runonce.Do(initDevices)
|
||||||
|
|
||||||
|
api.ResponseSources(w, streams)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indexToItem(items []string, index string) string {
|
||||||
|
if i, err := strconv.Atoi(index); err == nil && i < len(items) {
|
||||||
|
return items[i]
|
||||||
|
}
|
||||||
|
return index
|
||||||
|
}
|
||||||
@@ -0,0 +1,388 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/ffmpeg/device"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/ffmpeg/hardware"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/ffmpeg/virtual"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/rtsp"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/ffmpeg"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
var cfg struct {
|
||||||
|
Mod map[string]string `yaml:"ffmpeg"`
|
||||||
|
Log struct {
|
||||||
|
Level string `yaml:"ffmpeg"`
|
||||||
|
} `yaml:"log"`
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.Mod = defaults // will be overriden from yaml
|
||||||
|
cfg.Log.Level = "error"
|
||||||
|
|
||||||
|
app.LoadConfig(&cfg)
|
||||||
|
|
||||||
|
log = app.GetLogger("ffmpeg")
|
||||||
|
|
||||||
|
// zerolog levels: trace debug info warn error fatal panic disabled
|
||||||
|
// FFmpeg levels: trace debug verbose info warning error fatal panic quiet
|
||||||
|
if cfg.Log.Level == "warn" {
|
||||||
|
cfg.Log.Level = "warning"
|
||||||
|
}
|
||||||
|
defaults["global"] += " -v " + cfg.Log.Level
|
||||||
|
|
||||||
|
streams.RedirectFunc("ffmpeg", func(url string) (string, error) {
|
||||||
|
if _, err := Version(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
args := parseArgs(url[7:])
|
||||||
|
if core.Contains(args.Codecs, "auto") {
|
||||||
|
return "", nil // force call streams.HandleFunc("ffmpeg")
|
||||||
|
}
|
||||||
|
return "exec:" + args.String(), nil
|
||||||
|
})
|
||||||
|
|
||||||
|
streams.HandleFunc("ffmpeg", NewProducer)
|
||||||
|
|
||||||
|
api.HandleFunc("api/ffmpeg", apiFFmpeg)
|
||||||
|
|
||||||
|
device.Init(defaults["bin"])
|
||||||
|
hardware.Init(defaults["bin"])
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaults = map[string]string{
|
||||||
|
"bin": "ffmpeg",
|
||||||
|
"global": "-hide_banner",
|
||||||
|
"timeout": "5",
|
||||||
|
|
||||||
|
// inputs
|
||||||
|
"file": "-re -i {input}",
|
||||||
|
"http": "-fflags nobuffer -flags low_delay -i {input}",
|
||||||
|
"rtsp": "-fflags nobuffer -flags low_delay -timeout {timeout} -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i {input}",
|
||||||
|
"rtsp/udp": "-fflags nobuffer -flags low_delay -timeout {timeout} -user_agent go2rtc/ffmpeg -i {input}",
|
||||||
|
|
||||||
|
// output
|
||||||
|
"output": "-user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}",
|
||||||
|
"output/mjpeg": "-f mjpeg -",
|
||||||
|
"output/raw": "-f yuv4mpegpipe -",
|
||||||
|
"output/aac": "-f adts -",
|
||||||
|
"output/wav": "-f wav -",
|
||||||
|
|
||||||
|
// `-preset superfast` - we can't use ultrafast because it doesn't support `-profile main -level 4.1`
|
||||||
|
// `-tune zerolatency` - for minimal latency
|
||||||
|
// `-profile high -level 4.1` - most used streaming profile
|
||||||
|
// `-pix_fmt:v yuv420p` - important for Telegram
|
||||||
|
"h264": "-c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p",
|
||||||
|
"h265": "-c:v libx265 -g 50 -profile:v main -x265-params level=5.1:high-tier=0 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p",
|
||||||
|
"mjpeg": "-c:v mjpeg",
|
||||||
|
//"mjpeg": "-c:v mjpeg -force_duplicated_matrix:v 1 -huffman:v 0 -pix_fmt:v yuvj420p",
|
||||||
|
|
||||||
|
"raw": "-c:v rawvideo",
|
||||||
|
"raw/gray8": "-c:v rawvideo -pix_fmt:v gray8",
|
||||||
|
"raw/yuv420p": "-c:v rawvideo -pix_fmt:v yuv420p",
|
||||||
|
"raw/yuv422p": "-c:v rawvideo -pix_fmt:v yuv422p",
|
||||||
|
"raw/yuv444p": "-c:v rawvideo -pix_fmt:v yuv444p",
|
||||||
|
|
||||||
|
// https://ffmpeg.org/ffmpeg-codecs.html#libopus-1
|
||||||
|
// https://github.com/pion/webrtc/issues/1514
|
||||||
|
// https://ffmpeg.org/ffmpeg-resampler.html
|
||||||
|
// `-async 1` or `-min_comp 0` - force resampling for static timestamp inc, important for WebRTC audio quality
|
||||||
|
"opus": "-c:a libopus -application:a lowdelay -min_comp 0",
|
||||||
|
"opus/16000": "-c:a libopus -application:a lowdelay -min_comp 0 -ar:a 16000 -ac:a 1",
|
||||||
|
"pcmu": "-c:a pcm_mulaw -ar:a 8000 -ac:a 1",
|
||||||
|
"pcmu/8000": "-c:a pcm_mulaw -ar:a 8000 -ac:a 1",
|
||||||
|
"pcmu/16000": "-c:a pcm_mulaw -ar:a 16000 -ac:a 1",
|
||||||
|
"pcmu/48000": "-c:a pcm_mulaw -ar:a 48000 -ac:a 1",
|
||||||
|
"pcma": "-c:a pcm_alaw -ar:a 8000 -ac:a 1",
|
||||||
|
"pcma/8000": "-c:a pcm_alaw -ar:a 8000 -ac:a 1",
|
||||||
|
"pcma/16000": "-c:a pcm_alaw -ar:a 16000 -ac:a 1",
|
||||||
|
"pcma/48000": "-c:a pcm_alaw -ar:a 48000 -ac:a 1",
|
||||||
|
"aac": "-c:a aac", // keep sample rate and channels
|
||||||
|
"aac/16000": "-c:a aac -ar:a 16000 -ac:a 1",
|
||||||
|
"mp3": "-c:a libmp3lame -q:a 8",
|
||||||
|
"pcm": "-c:a pcm_s16be -ar:a 8000 -ac:a 1",
|
||||||
|
"pcm/8000": "-c:a pcm_s16be -ar:a 8000 -ac:a 1",
|
||||||
|
"pcm/16000": "-c:a pcm_s16be -ar:a 16000 -ac:a 1",
|
||||||
|
"pcm/48000": "-c:a pcm_s16be -ar:a 48000 -ac:a 1",
|
||||||
|
"pcml": "-c:a pcm_s16le -ar:a 8000 -ac:a 1",
|
||||||
|
"pcml/8000": "-c:a pcm_s16le -ar:a 8000 -ac:a 1",
|
||||||
|
"pcml/16000": "-c:a pcm_s16le -ar:a 16000 -ac:a 1",
|
||||||
|
"pcml/44100": "-c:a pcm_s16le -ar:a 44100 -ac:a 1",
|
||||||
|
|
||||||
|
// hardware Intel and AMD on Linux
|
||||||
|
// better not to set `-async_depth:v 1` like for QSV, because framedrops
|
||||||
|
// `-bf 0` - disable B-frames is very important
|
||||||
|
"h264/vaapi": "-c:v h264_vaapi -g 50 -bf 0 -profile:v high -level:v 4.1 -sei:v 0",
|
||||||
|
"h265/vaapi": "-c:v hevc_vaapi -g 50 -bf 0 -profile:v main -level:v 5.1 -sei:v 0",
|
||||||
|
"mjpeg/vaapi": "-c:v mjpeg_vaapi",
|
||||||
|
|
||||||
|
// hardware Raspberry
|
||||||
|
"h264/v4l2m2m": "-c:v h264_v4l2m2m -g 50 -bf 0",
|
||||||
|
"h265/v4l2m2m": "-c:v hevc_v4l2m2m -g 50 -bf 0",
|
||||||
|
|
||||||
|
// hardware Rockchip
|
||||||
|
// important to use custom ffmpeg https://github.com/AlexxIT/go2rtc/issues/768
|
||||||
|
// hevc - doesn't have a profile setting
|
||||||
|
"h264/rkmpp": "-c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1",
|
||||||
|
"h265/rkmpp": "-c:v hevc_rkmpp -g 50 -bf 0 -profile:v main -level:v 5.1",
|
||||||
|
"mjpeg/rkmpp": "-c:v mjpeg_rkmpp",
|
||||||
|
|
||||||
|
// hardware NVidia on Linux and Windows
|
||||||
|
// preset=p2 - faster, tune=ll - low latency
|
||||||
|
"h264/cuda": "-c:v h264_nvenc -g 50 -bf 0 -profile:v high -level:v auto -preset:v p2 -tune:v ll",
|
||||||
|
"h265/cuda": "-c:v hevc_nvenc -g 50 -bf 0 -profile:v main -level:v auto",
|
||||||
|
|
||||||
|
// hardware Intel on Windows
|
||||||
|
"h264/dxva2": "-c:v h264_qsv -g 50 -bf 0 -profile:v high -level:v 4.1 -async_depth:v 1",
|
||||||
|
"h265/dxva2": "-c:v hevc_qsv -g 50 -bf 0 -profile:v main -level:v 5.1 -async_depth:v 1",
|
||||||
|
"mjpeg/dxva2": "-c:v mjpeg_qsv",
|
||||||
|
|
||||||
|
// hardware macOS
|
||||||
|
"h264/videotoolbox": "-c:v h264_videotoolbox -g 50 -bf 0 -profile:v high -level:v 4.1",
|
||||||
|
"h265/videotoolbox": "-c:v hevc_videotoolbox -g 50 -bf 0 -profile:v main -level:v 5.1",
|
||||||
|
}
|
||||||
|
|
||||||
|
var log zerolog.Logger
|
||||||
|
|
||||||
|
// configTemplate - return template from config (defaults) if exist or return raw template
|
||||||
|
func configTemplate(template string) string {
|
||||||
|
if s := defaults[template]; s != "" {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return template
|
||||||
|
}
|
||||||
|
|
||||||
|
// inputTemplate - select input template from YAML config by template name
|
||||||
|
// if query has input param - select another template by this name
|
||||||
|
// if there is no another template - use input param as template
|
||||||
|
func inputTemplate(name, s string, query url.Values) string {
|
||||||
|
var template string
|
||||||
|
if input := query.Get("input"); input != "" {
|
||||||
|
template = configTemplate(input)
|
||||||
|
} else {
|
||||||
|
template = defaults[name]
|
||||||
|
}
|
||||||
|
if strings.Contains(template, "{timeout}") {
|
||||||
|
timeout := query.Get("timeout")
|
||||||
|
if timeout == "" {
|
||||||
|
timeout = defaults["timeout"]
|
||||||
|
}
|
||||||
|
template = strings.Replace(template, "{timeout}", timeout+"000000", 1)
|
||||||
|
}
|
||||||
|
return strings.Replace(template, "{input}", s, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseArgs(s string) *ffmpeg.Args {
|
||||||
|
// init FFmpeg arguments
|
||||||
|
args := &ffmpeg.Args{
|
||||||
|
Bin: defaults["bin"],
|
||||||
|
Global: defaults["global"],
|
||||||
|
Output: defaults["output"],
|
||||||
|
Version: verAV,
|
||||||
|
}
|
||||||
|
|
||||||
|
var source = s
|
||||||
|
var query url.Values
|
||||||
|
if i := strings.IndexByte(s, '#'); i >= 0 {
|
||||||
|
query = streams.ParseQuery(s[i+1:])
|
||||||
|
args.Video = len(query["video"])
|
||||||
|
args.Audio = len(query["audio"])
|
||||||
|
s = s[:i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse input:
|
||||||
|
// 1. Input as xxxx:// link (http or rtsp or any other)
|
||||||
|
// 2. Input as stream name
|
||||||
|
// 3. Input as FFmpeg device (local USB camera)
|
||||||
|
if i := strings.Index(s, "://"); i > 0 {
|
||||||
|
switch s[:i] {
|
||||||
|
case "http", "https", "rtmp":
|
||||||
|
args.Input = inputTemplate("http", s, query)
|
||||||
|
case "rtsp", "rtsps":
|
||||||
|
// https://ffmpeg.org/ffmpeg-protocols.html#rtsp
|
||||||
|
// skip unnecessary input tracks
|
||||||
|
switch {
|
||||||
|
case (args.Video > 0 && args.Audio > 0) || (args.Video == 0 && args.Audio == 0):
|
||||||
|
args.Input = "-allowed_media_types video+audio "
|
||||||
|
case args.Video > 0:
|
||||||
|
args.Input = "-allowed_media_types video "
|
||||||
|
case args.Audio > 0:
|
||||||
|
args.Input = "-allowed_media_types audio "
|
||||||
|
}
|
||||||
|
|
||||||
|
args.Input += inputTemplate("rtsp", s, query)
|
||||||
|
default:
|
||||||
|
args.Input = "-i " + s
|
||||||
|
}
|
||||||
|
} else if streams.Get(s) != nil {
|
||||||
|
s = "rtsp://127.0.0.1:" + rtsp.Port + "/" + s
|
||||||
|
switch {
|
||||||
|
case args.Video > 0 && args.Audio == 0:
|
||||||
|
s += "?video"
|
||||||
|
case args.Audio > 0 && args.Video == 0:
|
||||||
|
s += "?audio"
|
||||||
|
default:
|
||||||
|
s += "?video&audio"
|
||||||
|
}
|
||||||
|
s += "&source=ffmpeg:" + url.QueryEscape(source)
|
||||||
|
for _, v := range query["query"] {
|
||||||
|
s += "&" + v
|
||||||
|
}
|
||||||
|
args.Input = inputTemplate("rtsp", s, query)
|
||||||
|
} else if i = strings.Index(s, "?"); i > 0 {
|
||||||
|
switch s[:i] {
|
||||||
|
case "device":
|
||||||
|
args.Input = device.GetInput(s[i+1:])
|
||||||
|
case "virtual":
|
||||||
|
args.Input = virtual.GetInput(s[i+1:])
|
||||||
|
case "tts":
|
||||||
|
args.Input = virtual.GetInputTTS(s[i+1:])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
args.Input = inputTemplate("file", s, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
if query["async"] != nil {
|
||||||
|
args.Input = "-use_wallclock_as_timestamps 1 -async 1 " + args.Input
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse query params:
|
||||||
|
// 1. `width`/`height` params
|
||||||
|
// 2. `rotate` param
|
||||||
|
// 3. `video` params (support multiple)
|
||||||
|
// 4. `audio` params (support multiple)
|
||||||
|
// 5. `hardware` param
|
||||||
|
if query != nil {
|
||||||
|
// 1. Process raw params for FFmpeg
|
||||||
|
for _, raw := range query["raw"] {
|
||||||
|
// support templates https://github.com/AlexxIT/go2rtc/issues/487
|
||||||
|
raw = configTemplate(raw)
|
||||||
|
args.AddCodec(raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Process video filters (resize and rotation)
|
||||||
|
if query["width"] != nil || query["height"] != nil {
|
||||||
|
filter := "scale="
|
||||||
|
if query["width"] != nil {
|
||||||
|
filter += query["width"][0]
|
||||||
|
} else {
|
||||||
|
filter += "-1"
|
||||||
|
}
|
||||||
|
filter += ":"
|
||||||
|
if query["height"] != nil {
|
||||||
|
filter += query["height"][0]
|
||||||
|
} else {
|
||||||
|
filter += "-1"
|
||||||
|
}
|
||||||
|
args.AddFilter(filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
if query["rotate"] != nil {
|
||||||
|
var filter string
|
||||||
|
switch query["rotate"][0] {
|
||||||
|
case "90":
|
||||||
|
filter = "transpose=1" // 90 degrees clockwise
|
||||||
|
case "180":
|
||||||
|
filter = "transpose=1,transpose=1"
|
||||||
|
case "-90", "270":
|
||||||
|
filter = "transpose=2" // 90 degrees counterclockwise
|
||||||
|
}
|
||||||
|
if filter != "" {
|
||||||
|
args.AddFilter(filter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, drawtext := range query["drawtext"] {
|
||||||
|
// support templates https://github.com/AlexxIT/go2rtc/issues/487
|
||||||
|
drawtext = configTemplate(drawtext)
|
||||||
|
|
||||||
|
// support default timestamp format
|
||||||
|
if !strings.Contains(drawtext, "text=") {
|
||||||
|
drawtext += `:text='%{localtime\:%Y-%m-%d %X}'`
|
||||||
|
}
|
||||||
|
|
||||||
|
args.AddFilter("drawtext=" + drawtext)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Process video codecs
|
||||||
|
if args.Video > 0 {
|
||||||
|
for _, video := range query["video"] {
|
||||||
|
if video != "copy" {
|
||||||
|
if codec := defaults[video]; codec != "" {
|
||||||
|
args.AddCodec(codec)
|
||||||
|
} else {
|
||||||
|
args.AddCodec(video)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
args.AddCodec("-c:v copy")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if query["bitrate"] != nil {
|
||||||
|
// https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
|
||||||
|
b := query["bitrate"][0]
|
||||||
|
args.AddCodec("-b:v " + b + " -maxrate " + b + " -bufsize " + b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Process audio codecs
|
||||||
|
if args.Audio > 0 {
|
||||||
|
for _, audio := range query["audio"] {
|
||||||
|
if audio != "copy" {
|
||||||
|
if codec := defaults[audio]; codec != "" {
|
||||||
|
args.AddCodec(codec)
|
||||||
|
} else {
|
||||||
|
args.AddCodec(audio)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
args.AddCodec("-c:a copy")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if query["hardware"] != nil {
|
||||||
|
hardware.MakeHardware(args, query["hardware"][0], defaults)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case args.Video == 0 && args.Audio == 0:
|
||||||
|
args.AddCodec("-c copy")
|
||||||
|
case args.Video == 0:
|
||||||
|
args.AddCodec("-vn")
|
||||||
|
case args.Audio == 0:
|
||||||
|
args.AddCodec("-an")
|
||||||
|
}
|
||||||
|
|
||||||
|
// change otput from RTSP to some other pipe format
|
||||||
|
switch {
|
||||||
|
case args.Video == 0 && args.Audio == 0:
|
||||||
|
// no transcoding from mjpeg input (ffmpeg device with support output as raw MJPEG)
|
||||||
|
if strings.Contains(args.Input, " mjpeg ") {
|
||||||
|
args.Output = defaults["output/mjpeg"]
|
||||||
|
}
|
||||||
|
case args.Video == 1 && args.Audio == 0:
|
||||||
|
switch core.Before(query.Get("video"), "/") {
|
||||||
|
case "mjpeg":
|
||||||
|
args.Output = defaults["output/mjpeg"]
|
||||||
|
case "raw":
|
||||||
|
args.Output = defaults["output/raw"]
|
||||||
|
}
|
||||||
|
case args.Video == 0 && args.Audio == 1:
|
||||||
|
switch core.Before(query.Get("audio"), "/") {
|
||||||
|
case "aac":
|
||||||
|
args.Output = defaults["output/aac"]
|
||||||
|
case "pcma", "pcmu", "pcml":
|
||||||
|
args.Output = defaults["output/wav"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return args
|
||||||
|
}
|
||||||
@@ -0,0 +1,396 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/ffmpeg"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseArgsFile(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
source string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "[FILE] all tracks will be copied without transcoding codecs",
|
||||||
|
source: "/media/bbb.mp4",
|
||||||
|
expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[FILE] video will be transcoded to H264, audio will be skipped",
|
||||||
|
source: "/media/bbb.mp4#video=h264",
|
||||||
|
expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[FILE] video will be copied, audio will be transcoded to pcmu",
|
||||||
|
source: "/media/bbb.mp4#video=copy#audio=pcmu",
|
||||||
|
expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c:v copy -c:a pcm_mulaw -ar:a 8000 -ac:a 1 -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[FILE] video will be transcoded to H265 and rotate 270º, audio will be skipped",
|
||||||
|
source: "/media/bbb.mp4#video=h265#rotate=-90",
|
||||||
|
expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -vf "transpose=2" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[FILE] video will be output for MJPEG to pipe, audio will be skipped",
|
||||||
|
source: "/media/bbb.mp4#video=mjpeg",
|
||||||
|
expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c:v mjpeg -an -f mjpeg -`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "https://github.com/AlexxIT/go2rtc/issues/509",
|
||||||
|
source: "ffmpeg:test.mp4#raw=-ss 00:00:20",
|
||||||
|
expect: `ffmpeg -hide_banner -re -i ffmpeg:test.mp4 -ss 00:00:20 -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
args := parseArgs(test.source)
|
||||||
|
require.Equal(t, test.expect, args.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseArgsDevice(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
source string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "[DEVICE] video will be output for MJPEG to pipe, with size 1920x1080",
|
||||||
|
source: "device?video=0&video_size=1920x1080",
|
||||||
|
expect: `ffmpeg -hide_banner -f dshow -video_size 1920x1080 -i "video=0" -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[DEVICE] video will be transcoded to H265 with framerate 20, audio will be skipped",
|
||||||
|
source: "device?video=0&framerate=20#video=h265",
|
||||||
|
expect: `ffmpeg -hide_banner -f dshow -framerate 20 -i "video=0" -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[DEVICE] video/audio",
|
||||||
|
source: "device?video=FaceTime HD Camera&audio=Microphone (High Definition Audio Device)",
|
||||||
|
expect: `ffmpeg -hide_banner -f dshow -i "video=FaceTime HD Camera:audio=Microphone (High Definition Audio Device)" -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
args := parseArgs(test.source)
|
||||||
|
require.Equal(t, test.expect, args.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseArgsIpCam(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
source string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "[HTTP] video will be copied",
|
||||||
|
source: "http://example.com",
|
||||||
|
expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[HTTP-MJPEG] video will be transcoded to H264",
|
||||||
|
source: "http://example.com#video=h264",
|
||||||
|
expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http://example.com -c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[HLS] video will be copied, audio will be skipped",
|
||||||
|
source: "https://example.com#video=copy",
|
||||||
|
expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i https://example.com -c:v copy -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[RTSP] video will be copied without transcoding codecs",
|
||||||
|
source: "rtsp://example.com",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[RTSP] video with resize to 1280x720, should be transcoded, so select H265",
|
||||||
|
source: "rtsp://example.com#video=h265#width=1280#height=720",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -vf "scale=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[RTSP] video will be copied, changing RTSP transport from TCP to UDP+TCP",
|
||||||
|
source: "rtsp://example.com#input=rtsp/udp",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -i rtsp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[RTMP] video will be copied, changing RTSP transport from TCP to UDP+TCP",
|
||||||
|
source: "rtmp://example.com#input=rtsp/udp",
|
||||||
|
expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -i rtmp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[RTSP] custom timeout",
|
||||||
|
source: "rtsp://example.com#timeout=10",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 10000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
args := parseArgs(test.source)
|
||||||
|
require.Equal(t, test.expect, args.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseArgsAudio(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
source string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "[AUDIO] audio will be transcoded to AAC, video will be skipped",
|
||||||
|
source: "rtsp://example.com#audio=aac",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a aac -vn -f adts -`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[AUDIO] audio will be transcoded to AAC/16000, video will be skipped",
|
||||||
|
source: "rtsp://example.com#audio=aac/16000",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a aac -ar:a 16000 -ac:a 1 -vn -f adts -`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[AUDIO] audio will be transcoded to OPUS, video will be skipped",
|
||||||
|
source: "rtsp://example.com#audio=opus",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a libopus -application:a lowdelay -min_comp 0 -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[AUDIO] audio will be transcoded to PCMU, video will be skipped",
|
||||||
|
source: "rtsp://example.com#audio=pcmu",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_mulaw -ar:a 8000 -ac:a 1 -vn -f wav -`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[AUDIO] audio will be transcoded to PCMU/16000, video will be skipped",
|
||||||
|
source: "rtsp://example.com#audio=pcmu/16000",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_mulaw -ar:a 16000 -ac:a 1 -vn -f wav -`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[AUDIO] audio will be transcoded to PCMU/48000, video will be skipped",
|
||||||
|
source: "rtsp://example.com#audio=pcmu/48000",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_mulaw -ar:a 48000 -ac:a 1 -vn -f wav -`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[AUDIO] audio will be transcoded to PCMA, video will be skipped",
|
||||||
|
source: "rtsp://example.com#audio=pcma",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_alaw -ar:a 8000 -ac:a 1 -vn -f wav -`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[AUDIO] audio will be transcoded to PCMA/16000, video will be skipped",
|
||||||
|
source: "rtsp://example.com#audio=pcma/16000",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_alaw -ar:a 16000 -ac:a 1 -vn -f wav -`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[AUDIO] audio will be transcoded to PCMA/48000, video will be skipped",
|
||||||
|
source: "rtsp://example.com#audio=pcma/48000",
|
||||||
|
expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_alaw -ar:a 48000 -ac:a 1 -vn -f wav -`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
args := parseArgs(test.source)
|
||||||
|
require.Equal(t, test.expect, args.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseArgsHwVaapi(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
source string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "[HTTP-MJPEG] video will be transcoded to H264",
|
||||||
|
source: "http:///example.com#video=h264#hardware=vaapi",
|
||||||
|
expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_vaapi -g 50 -bf 0 -profile:v high -level:v 4.1 -sei:v 0 -an -vf "format=vaapi|nv12,hwupload,scale_vaapi=out_color_matrix=bt709:out_range=tv:format=nv12" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[RTSP] video with rotation, should be transcoded, so select H264",
|
||||||
|
source: "rtsp://example.com#video=h264#rotate=180#hardware=vaapi",
|
||||||
|
expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v h264_vaapi -g 50 -bf 0 -profile:v high -level:v 4.1 -sei:v 0 -an -vf "format=vaapi|nv12,hwupload,transpose_vaapi=4,scale_vaapi=out_color_matrix=bt709:out_range=tv:format=nv12" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[RTSP] video with resize to 1280x720, should be transcoded, so select H265",
|
||||||
|
source: "rtsp://example.com#video=h265#width=1280#height=720#hardware=vaapi",
|
||||||
|
expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v hevc_vaapi -g 50 -bf 0 -profile:v main -level:v 5.1 -sei:v 0 -an -vf "format=vaapi|nv12,hwupload,scale_vaapi=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[FILE] video will be output for MJPEG to pipe, audio will be skipped",
|
||||||
|
source: "/media/bbb.mp4#video=mjpeg#hardware=vaapi",
|
||||||
|
expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -re -i /media/bbb.mp4 -c:v mjpeg_vaapi -an -vf "format=vaapi|nv12,hwupload" -f mjpeg -`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[DEVICE] MJPEG video with size 1920x1080 will be transcoded to H265",
|
||||||
|
source: "device?video=0&video_size=1920x1080#video=h265#hardware=vaapi",
|
||||||
|
expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -f dshow -video_size 1920x1080 -i "video=0" -c:v hevc_vaapi -g 50 -bf 0 -profile:v main -level:v 5.1 -sei:v 0 -an -vf "format=vaapi|nv12,hwupload" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
args := parseArgs(test.source)
|
||||||
|
require.Equal(t, test.expect, args.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func _TestParseArgsHwV4l2m2m(t *testing.T) {
|
||||||
|
// [HTTP-MJPEG] video will be transcoded to H264
|
||||||
|
args := parseArgs("http:///example.com#video=h264#hardware=v4l2m2m")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_v4l2m2m -g 50 -bf 0 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [RTSP] video with rotation, should be transcoded, so select H264
|
||||||
|
args = parseArgs("rtsp://example.com#video=h264#rotate=180#hardware=v4l2m2m")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v h264_v4l2m2m -g 50 -bf 0 -an -vf "transpose=1,transpose=1" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [RTSP] video with resize to 1280x720, should be transcoded, so select H265
|
||||||
|
args = parseArgs("rtsp://example.com#video=h265#width=1280#height=720#hardware=v4l2m2m")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v hevc_v4l2m2m -g 50 -bf 0 -an -vf "scale=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [DEVICE] MJPEG video with size 1920x1080 will be transcoded to H265
|
||||||
|
args = parseArgs("device?video=0&video_size=1920x1080#video=h265#hardware=v4l2m2m")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -f dshow -video_size 1920x1080 -i video="0" -c:v hevc_v4l2m2m -g 50 -bf 0 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseArgsHwRKMPP(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
source string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "[FILE] transcoding to H264",
|
||||||
|
source: "bbb.mp4#video=h264#hardware=rkmpp",
|
||||||
|
expect: `ffmpeg -hide_banner -hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga -re -i bbb.mp4 -c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[FILE] transcoding with rotation",
|
||||||
|
source: "bbb.mp4#video=h264#rotate=180#hardware=rkmpp",
|
||||||
|
expect: `ffmpeg -hide_banner -hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga -re -i bbb.mp4 -c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1 -an -vf "format=drm_prime|nv12,hwupload,vpp_rkrga=transpose=4" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "[FILE] transcoding with scaling",
|
||||||
|
source: "bbb.mp4#video=h264#height=320#hardware=rkmpp",
|
||||||
|
expect: `ffmpeg -hide_banner -hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga -re -i bbb.mp4 -c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1 -an -vf "format=drm_prime|nv12,hwupload,scale_rkrga=-1:320:force_original_aspect_ratio=0" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
args := parseArgs(test.source)
|
||||||
|
require.Equal(t, test.expect, args.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func _TestParseArgsHwCuda(t *testing.T) {
|
||||||
|
// [HTTP-MJPEG] video will be transcoded to H264
|
||||||
|
args := parseArgs("http:///example.com#video=h264#hardware=cuda")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel cuda -hwaccel_output_format cuda -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_nvenc -g 50 -bf 0 -profile:v high -level:v auto -preset:v p2 -tune:v ll -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [RTSP] video with rotation, should be transcoded, so select H264
|
||||||
|
args = parseArgs("rtsp://example.com#video=h264#rotate=180#hardware=cuda")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel cuda -hwaccel_output_format nv12 -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v h264_nvenc -g 50 -bf 0 -profile:v high -level:v auto -preset:v p2 -tune:v ll -an -vf "transpose=1,transpose=1,hwupload" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [RTSP] video with resize to 1280x720, should be transcoded, so select H265
|
||||||
|
args = parseArgs("rtsp://example.com#video=h265#width=1280#height=720#hardware=cuda")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel cuda -hwaccel_output_format cuda -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v hevc_nvenc -g 50 -bf 0 -profile:v high -level:v auto -an -vf "scale_cuda=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [DEVICE] MJPEG video with size 1920x1080 will be transcoded to H265
|
||||||
|
args = parseArgs("device?video=0&video_size=1920x1080#video=h265#hardware=cuda")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel cuda -hwaccel_output_format cuda -f dshow -video_size 1920x1080 -i video="0" -c:v hevc_nvenc -g 50 -bf 0 -profile:v high -level:v auto -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func _TestParseArgsHwDxva2(t *testing.T) {
|
||||||
|
// [HTTP-MJPEG] video will be transcoded to H264
|
||||||
|
args := parseArgs("http:///example.com#video=h264#hardware=dxva2")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel dxva2 -hwaccel_output_format dxva2_vld -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_qsv -g 50 -bf 0 -profile:v high -level:v 4.1 -async_depth:v 1 -an -vf "hwmap=derive_device=qsv,format=qsv" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [RTSP] video with rotation, should be transcoded, so select H264
|
||||||
|
args = parseArgs("rtsp://example.com#video=h264#rotate=180#hardware=dxva2")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel dxva2 -hwaccel_output_format dxva2_vld -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v h264_qsv -g 50 -bf 0 -profile:v high -level:v 4.1 -async_depth:v 1 -an -vf "hwmap=derive_device=qsv,format=qsv,transpose=1,transpose=1" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [RTSP] video with resize to 1280x720, should be transcoded, so select H265
|
||||||
|
args = parseArgs("rtsp://example.com#video=h265#width=1280#height=720#hardware=dxva2")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel dxva2 -hwaccel_output_format dxva2_vld -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v hevc_qsv -g 50 -bf 0 -profile:v high -level:v 5.1 -async_depth:v 1 -an -vf "hwmap=derive_device=qsv,format=qsv,scale_qsv=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [FILE] video will be output for MJPEG to pipe, audio will be skipped
|
||||||
|
args = parseArgs("/media/bbb.mp4#video=mjpeg#hardware=dxva2")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel dxva2 -hwaccel_output_format dxva2_vld -re -i /media/bbb.mp4 -c:v mjpeg_qsv -profile:v high -level:v 5.1 -an -vf "hwmap=derive_device=qsv,format=qsv" -f mjpeg -`, args.String())
|
||||||
|
|
||||||
|
// [DEVICE] MJPEG video with size 1920x1080 will be transcoded to H265
|
||||||
|
args = parseArgs("device?video=0&video_size=1920x1080#video=h265#hardware=dxva2")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel dxva2 -hwaccel_output_format dxva2_vld -f dshow -video_size 1920x1080 -i video="0" -c:v hevc_qsv -g 50 -bf 0 -profile:v high -level:v 5.1 -async_depth:v 1 -an -vf "hwmap=derive_device=qsv,format=qsv" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func _TestParseArgsHwVideotoolbox(t *testing.T) {
|
||||||
|
// [HTTP-MJPEG] video will be transcoded to H264
|
||||||
|
args := parseArgs("http:///example.com#video=h264#hardware=videotoolbox")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel videotoolbox -hwaccel_output_format videotoolbox_vld -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_videotoolbox -g 50 -bf 0 -profile:v high -level:v 4.1 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [RTSP] video with rotation, should be transcoded, so select H264
|
||||||
|
args = parseArgs("rtsp://example.com#video=h264#rotate=180#hardware=videotoolbox")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel videotoolbox -hwaccel_output_format videotoolbox_vld -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v h264_videotoolbox -g 50 -bf 0 -profile:v high -level:v 4.1 -an -vf "transpose=1,transpose=1" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [RTSP] video with resize to 1280x720, should be transcoded, so select H265
|
||||||
|
args = parseArgs("rtsp://example.com#video=h265#width=1280#height=720#hardware=videotoolbox")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel videotoolbox -hwaccel_output_format videotoolbox_vld -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v hevc_videotoolbox -g 50 -bf 0 -profile:v high -level:v 5.1 -an -vf "scale=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
|
||||||
|
// [DEVICE] MJPEG video with size 1920x1080 will be transcoded to H265
|
||||||
|
args = parseArgs("device?video=0&video_size=1920x1080#video=h265#hardware=videotoolbox")
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel videotoolbox -hwaccel_output_format videotoolbox_vld -f dshow -video_size 1920x1080 -i video="0" -c:v hevc_videotoolbox -g 50 -bf 0 -profile:v high -level:v 5.1 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeckLink(t *testing.T) {
|
||||||
|
args := parseArgs(`DeckLink SDI (2)#video=h264#hardware=vaapi#input=-format_code Hp29 -f decklink -i "{input}"`)
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -format_code Hp29 -f decklink -i "DeckLink SDI (2)" -c:v h264_vaapi -g 50 -bf 0 -profile:v high -level:v 4.1 -sei:v 0 -an -vf "format=vaapi|nv12,hwupload,scale_vaapi=out_color_matrix=bt709:out_range=tv:format=nv12" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDrawText(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
source string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
source: "http:///example.com#video=h264#drawtext=fontsize=12",
|
||||||
|
expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http:///example.com -c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -vf "drawtext=fontsize=12:text='%{localtime\:%Y-%m-%d %X}'" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
source: "http:///example.com#video=h264#width=640#drawtext=fontsize=12",
|
||||||
|
expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http:///example.com -c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -vf "scale=640:-1,drawtext=fontsize=12:text='%{localtime\:%Y-%m-%d %X}'" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
source: "http:///example.com#video=h264#width=640#drawtext=fontsize=12#hardware=vaapi",
|
||||||
|
expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format nv12 -hwaccel_flags allow_profile_mismatch -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_vaapi -g 50 -bf 0 -profile:v high -level:v 4.1 -sei:v 0 -an -vf "scale=640:-1,drawtext=fontsize=12:text='%{localtime\:%Y-%m-%d %X}',hwupload" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
args := parseArgs(test.source)
|
||||||
|
require.Equal(t, test.expect, args.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestVersion(t *testing.T) {
|
||||||
|
verAV = ffmpeg.Version61
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
source string
|
||||||
|
expect string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
source: "/media/bbb.mp4",
|
||||||
|
expect: `ffmpeg -hide_banner -readrate_initial_burst 0.001 -re -i /media/bbb.mp4 -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
args := parseArgs(test.source)
|
||||||
|
require.Equal(t, test.expect, args.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
# Hardware
|
||||||
|
|
||||||
|
You **DON'T** need hardware acceleration if:
|
||||||
|
|
||||||
|
- you're not using the [FFmpeg source](../README.md)
|
||||||
|
- you're using only `#video=copy` for the FFmpeg source
|
||||||
|
- you're using only `#audio=...` (any audio) transcoding for the FFmpeg source
|
||||||
|
|
||||||
|
You **NEED** hardware acceleration if you're using `#video=h264`, `#video=h265`, `#video=mjpeg` (video) transcoding.
|
||||||
|
|
||||||
|
## Important
|
||||||
|
|
||||||
|
- Acceleration is disabled by default because it can be unstable (this may change in the future)
|
||||||
|
- go2rtc can automatically detect supported hardware acceleration if enabled
|
||||||
|
- go2rtc will enable hardware decoding only if hardware encoding is supported
|
||||||
|
- go2rtc will use the same GPU for decoder and encoder
|
||||||
|
- Intel and AMD will switch to a software decoder if the input codec isn't supported by the hardware decoder
|
||||||
|
- NVIDIA will fail if the input codec isn't supported by the hardware decoder
|
||||||
|
- Raspberry Pi always uses a software decoder
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
# auto select hardware encoder
|
||||||
|
camera1_hw: ffmpeg:rtsp://rtsp:12345678@192.168.1.123/av_stream/ch0#video=h264#hardware
|
||||||
|
|
||||||
|
# manual select hardware encoder (vaapi, cuda, v4l2m2m, dxva2, videotoolbox)
|
||||||
|
camera1_vaapi: ffmpeg:rtsp://rtsp:12345678@192.168.1.123/av_stream/ch0#video=h264#hardware=vaapi
|
||||||
|
```
|
||||||
|
|
||||||
|
## Docker and Hass Addon
|
||||||
|
|
||||||
|
There are two versions of the Docker container and Hass Add-on:
|
||||||
|
|
||||||
|
- Latest (Alpine) supports hardware acceleration for Intel iGPU (CPU with graphics) and Raspberry Pi.
|
||||||
|
- Hardware (Debian 12) supports Intel iGPU, AMD GPU, NVIDIA GPU.
|
||||||
|
|
||||||
|
## Intel iGPU
|
||||||
|
|
||||||
|
**Supported on:** Windows binary, Linux binary, Docker, Hass Addon.
|
||||||
|
|
||||||
|
If you have an Intel Sandy Bridge (2011) CPU with graphics, you already have hardware decoding/encoding support for `AVC/H.264`.
|
||||||
|
|
||||||
|
If you have an Intel Skylake (2015) CPU with graphics, you already have hardware decoding/encoding support for `AVC/H.264`, `HEVC/H.265` and `MJPEG`.
|
||||||
|
|
||||||
|
Read more [here](https://en.wikipedia.org/wiki/Intel_Quick_Sync_Video#Hardware_decoding_and_encoding) and [here](https://en.wikipedia.org/wiki/Intel_Graphics_Technology#Capabilities_(GPU_video_acceleration)).
|
||||||
|
|
||||||
|
Linux and Docker:
|
||||||
|
|
||||||
|
- It may be important to have a recent OS and Linux kernel. For example, on my **Debian 10 (kernel 4.19)** it did not work, but after updating to **Debian 11 (kernel 5.10)** everything was fine.
|
||||||
|
- If you run into trouble, check that you have the `/dev/dri/` folder on your host.
|
||||||
|
|
||||||
|
Docker users should add the `--privileged` option to the container for access to the hardware.
|
||||||
|
|
||||||
|
**PS.** Supported via [VAAPI](https://trac.ffmpeg.org/wiki/Hardware/VAAPI) engine on Linux and [DXVA2+QSV](https://trac.ffmpeg.org/wiki/Hardware/QuickSync) engine on Windows.
|
||||||
|
|
||||||
|
## AMD GPU
|
||||||
|
|
||||||
|
*I don't have the hardware to test this!!!*
|
||||||
|
|
||||||
|
**Supported on:** Linux binary, Docker, Hass Addon.
|
||||||
|
|
||||||
|
Docker users should install: `alexxit/go2rtc:master-hardware`. Docker users should add the `--privileged` option to the container for access to the hardware.
|
||||||
|
|
||||||
|
Hass Addon users should install **go2rtc master hardware** version.
|
||||||
|
|
||||||
|
**PS.** Supported via [VAAPI](https://trac.ffmpeg.org/wiki/Hardware/VAAPI) engine.
|
||||||
|
|
||||||
|
## NVIDIA GPU
|
||||||
|
|
||||||
|
**Supported on:** Windows binary, Linux binary, Docker.
|
||||||
|
|
||||||
|
Docker users should install: `alexxit/go2rtc:master-hardware`.
|
||||||
|
|
||||||
|
Read more [here](https://docs.frigate.video/configuration/hardware_acceleration) and [here](https://jellyfin.org/docs/general/administration/hardware-acceleration/#nvidia-hardware-acceleration-on-docker-linux).
|
||||||
|
|
||||||
|
**PS.** Supported via [CUDA](https://trac.ffmpeg.org/wiki/HWAccelIntro#CUDANVENCNVDEC) engine.
|
||||||
|
|
||||||
|
## Raspberry Pi 3
|
||||||
|
|
||||||
|
**Supported on:** Linux binary, Docker, Hass Addon.
|
||||||
|
|
||||||
|
I don't recommend using transcoding on the Raspberry Pi 3. It's extremely slow, even with hardware acceleration. Also, it may fail when transcoding a 2K+ stream.
|
||||||
|
|
||||||
|
## Raspberry Pi 4
|
||||||
|
|
||||||
|
*I don't have the hardware to test this!!!*
|
||||||
|
|
||||||
|
**Supported on:** Linux binary, Docker, Hass Addon.
|
||||||
|
|
||||||
|
**PS.** Supported via [v4l2m2m](https://lalitm.com/hw-encoding-raspi/) engine.
|
||||||
|
|
||||||
|
## macOS
|
||||||
|
|
||||||
|
In my tests, transcoding is faster on the M1 CPU than on the M1 GPU. Transcoding time on the M1 CPU is better than any Intel iGPU and comparable to an NVIDIA RTX 2070.
|
||||||
|
|
||||||
|
**PS.** Supported via [videotoolbox](https://trac.ffmpeg.org/wiki/HWAccelIntro#VideoToolbox) engine.
|
||||||
|
|
||||||
|
## Rockchip
|
||||||
|
|
||||||
|
- It's important to use a custom FFmpeg build with Rockchip support from [@nyanmisaka](https://github.com/nyanmisaka/ffmpeg-rockchip)
|
||||||
|
- Static binaries from [@MarcA711](https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/)
|
||||||
|
- It's important to have Linux kernel 5.10 or 6.1
|
||||||
|
|
||||||
|
**Tested**
|
||||||
|
|
||||||
|
- [Orange Pi 3B](https://www.armbian.com/orangepi3b/) with Armbian 6.1, supports transcoding H.264, H.265, MJPEG
|
||||||
@@ -0,0 +1,210 @@
|
|||||||
|
package hardware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/ffmpeg"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
EngineSoftware = "software"
|
||||||
|
EngineVAAPI = "vaapi" // Intel iGPU and AMD GPU
|
||||||
|
EngineV4L2M2M = "v4l2m2m" // Raspberry Pi 3 and 4
|
||||||
|
EngineCUDA = "cuda" // NVidia on Windows and Linux
|
||||||
|
EngineDXVA2 = "dxva2" // Intel on Windows
|
||||||
|
EngineVideoToolbox = "videotoolbox" // macOS
|
||||||
|
EngineRKMPP = "rkmpp" // Rockchip
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init(bin string) {
|
||||||
|
api.HandleFunc("api/ffmpeg/hardware", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
api.ResponseSources(w, ProbeAll(bin))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeHardware converts software FFmpeg args to hardware args
|
||||||
|
// empty engine for autoselect
|
||||||
|
func MakeHardware(args *ffmpeg.Args, engine string, defaults map[string]string) {
|
||||||
|
for i, codec := range args.Codecs {
|
||||||
|
if len(codec) < 10 {
|
||||||
|
continue // skip short line (-c:v mjpeg...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// get current codec name
|
||||||
|
name := cut(codec, ' ', 1)
|
||||||
|
switch name {
|
||||||
|
case "libx264":
|
||||||
|
name = "h264"
|
||||||
|
case "libx265":
|
||||||
|
name = "h265"
|
||||||
|
case "mjpeg":
|
||||||
|
default:
|
||||||
|
continue // skip unsupported codec
|
||||||
|
}
|
||||||
|
|
||||||
|
// temporary disable probe for H265
|
||||||
|
if engine == "" && name != "h265" {
|
||||||
|
if engine = cache[name]; engine == "" {
|
||||||
|
engine = ProbeHardware(args.Bin, name)
|
||||||
|
cache[name] = engine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch engine {
|
||||||
|
case EngineVAAPI:
|
||||||
|
args.Codecs[i] = defaults[name+"/"+engine]
|
||||||
|
|
||||||
|
if !args.HasFilters("drawtext=") {
|
||||||
|
args.Input = "-hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch " + args.Input
|
||||||
|
|
||||||
|
if name == "h264" {
|
||||||
|
fixPixelFormat(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, filter := range args.Filters {
|
||||||
|
if strings.HasPrefix(filter, "scale=") {
|
||||||
|
args.Filters[i] = "scale_vaapi=" + filter[6:]
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(filter, "transpose=") {
|
||||||
|
if filter == "transpose=1,transpose=1" { // 180 degrees half-turn
|
||||||
|
args.Filters[i] = "transpose_vaapi=4" // reversal
|
||||||
|
} else {
|
||||||
|
args.Filters[i] = "transpose_vaapi=" + filter[10:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fix if input doesn't support hwaccel, do nothing when support
|
||||||
|
// insert as first filter before hardware scale and transpose
|
||||||
|
args.InsertFilter("format=vaapi|nv12,hwupload")
|
||||||
|
} else {
|
||||||
|
// enable software pixel for drawtext, scale and transpose
|
||||||
|
args.Input = "-hwaccel vaapi -hwaccel_output_format nv12 -hwaccel_flags allow_profile_mismatch " + args.Input
|
||||||
|
|
||||||
|
args.AddFilter("hwupload")
|
||||||
|
}
|
||||||
|
|
||||||
|
case EngineCUDA:
|
||||||
|
args.Codecs[i] = defaults[name+"/"+engine]
|
||||||
|
|
||||||
|
// CUDA doesn't support hardware transpose
|
||||||
|
// https://github.com/AlexxIT/go2rtc/issues/389
|
||||||
|
if !args.HasFilters("drawtext=", "transpose=") {
|
||||||
|
args.Input = "-hwaccel cuda -hwaccel_output_format cuda " + args.Input
|
||||||
|
|
||||||
|
for i, filter := range args.Filters {
|
||||||
|
if strings.HasPrefix(filter, "scale=") {
|
||||||
|
args.Filters[i] = "scale_cuda=" + filter[6:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
args.Input = "-hwaccel cuda -hwaccel_output_format nv12 " + args.Input
|
||||||
|
|
||||||
|
args.AddFilter("hwupload")
|
||||||
|
}
|
||||||
|
|
||||||
|
case EngineDXVA2:
|
||||||
|
args.Input = "-hwaccel dxva2 -hwaccel_output_format dxva2_vld " + args.Input
|
||||||
|
args.Codecs[i] = defaults[name+"/"+engine]
|
||||||
|
|
||||||
|
for i, filter := range args.Filters {
|
||||||
|
if strings.HasPrefix(filter, "scale=") {
|
||||||
|
args.Filters[i] = "scale_qsv=" + filter[6:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
args.InsertFilter("hwmap=derive_device=qsv,format=qsv")
|
||||||
|
|
||||||
|
case EngineVideoToolbox:
|
||||||
|
args.Input = "-hwaccel videotoolbox -hwaccel_output_format videotoolbox_vld " + args.Input
|
||||||
|
args.Codecs[i] = defaults[name+"/"+engine]
|
||||||
|
|
||||||
|
case EngineV4L2M2M:
|
||||||
|
args.Codecs[i] = defaults[name+"/"+engine]
|
||||||
|
|
||||||
|
case EngineRKMPP:
|
||||||
|
args.Codecs[i] = defaults[name+"/"+engine]
|
||||||
|
|
||||||
|
if !args.HasFilters("drawtext=") {
|
||||||
|
args.Input = "-hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga " + args.Input
|
||||||
|
|
||||||
|
for i, filter := range args.Filters {
|
||||||
|
if strings.HasPrefix(filter, "scale=") {
|
||||||
|
args.Filters[i] = "scale_rkrga=" + filter[6:] + ":force_original_aspect_ratio=0"
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(filter, "transpose=") {
|
||||||
|
if filter == "transpose=1,transpose=1" { // 180 degrees half-turn
|
||||||
|
args.Filters[i] = "vpp_rkrga=transpose=4" // reversal
|
||||||
|
} else {
|
||||||
|
args.Filters[i] = "vpp_rkrga=transpose=" + filter[10:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args.Filters) > 0 {
|
||||||
|
// fix if input doesn't support hwaccel, do nothing when support
|
||||||
|
// insert as first filter before hardware scale and transpose
|
||||||
|
args.InsertFilter("format=drm_prime|nv12,hwupload")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// enable software pixel for drawtext, scale and transpose
|
||||||
|
args.Input = "-hwaccel rkmpp -hwaccel_output_format nv12 -afbc rga " + args.Input
|
||||||
|
|
||||||
|
args.AddFilter("hwupload")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var cache = map[string]string{}
|
||||||
|
|
||||||
|
func run(bin string, args string) bool {
|
||||||
|
err := exec.Command(bin, strings.Split(args, " ")...).Run()
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runToString(bin string, args string) string {
|
||||||
|
if run(bin, args) {
|
||||||
|
return "OK"
|
||||||
|
} else {
|
||||||
|
return "ERROR"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func cut(s string, sep byte, pos int) string {
|
||||||
|
for n := 0; n < pos; n++ {
|
||||||
|
if i := strings.IndexByte(s, sep); i > 0 {
|
||||||
|
s = s[i+1:]
|
||||||
|
} else {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if i := strings.IndexByte(s, sep); i > 0 {
|
||||||
|
return s[:i]
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixPixelFormat:
|
||||||
|
// - good h264 pixel: yuv420p(tv, bt709) == yuv420p (mpeg/limited/tv)
|
||||||
|
// - bad h264 pixel: yuvj420p(pc, bt709) == yuvj420p (jpeg/full/pc)
|
||||||
|
// - bad jpeg pixel: yuvj422p(pc, bt470bg)
|
||||||
|
func fixPixelFormat(args *ffmpeg.Args) {
|
||||||
|
// in my tests this filters has same CPU/GPU load:
|
||||||
|
// - "hwupload"
|
||||||
|
// - "hwupload,scale_vaapi=out_color_matrix=bt709:out_range=tv"
|
||||||
|
// - "hwupload,scale_vaapi=out_color_matrix=bt709:out_range=tv:format=nv12"
|
||||||
|
const fixPixFmt = "out_color_matrix=bt709:out_range=tv:format=nv12"
|
||||||
|
|
||||||
|
for i, filter := range args.Filters {
|
||||||
|
if strings.HasPrefix(filter, "scale=") {
|
||||||
|
args.Filters[i] = filter + ":" + fixPixFmt
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
args.Filters = append(args.Filters, "scale="+fixPixFmt)
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
//go:build freebsd || netbsd || openbsd || dragonfly
|
||||||
|
|
||||||
|
package hardware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ProbeV4L2M2MH264 = "-f lavfi -i testsrc2 -t 1 -c h264_v4l2m2m -f null -"
|
||||||
|
ProbeV4L2M2MH265 = "-f lavfi -i testsrc2 -t 1 -c hevc_v4l2m2m -f null -"
|
||||||
|
ProbeRKMPPH264 = "-f lavfi -i testsrc2 -t 1 -c h264_rkmpp_encoder -f null -"
|
||||||
|
ProbeRKMPPH265 = "-f lavfi -i testsrc2 -t 1 -c hevc_rkmpp_encoder -f null -"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ProbeAll(bin string) []*api.Source {
|
||||||
|
return []*api.Source{
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeV4L2M2MH264),
|
||||||
|
URL: "ffmpeg:...#video=h264#hardware=" + EngineV4L2M2M,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeV4L2M2MH265),
|
||||||
|
URL: "ffmpeg:...#video=h265#hardware=" + EngineV4L2M2M,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeRKMPPH264),
|
||||||
|
URL: "ffmpeg:...#video=h264#hardware=" + EngineRKMPP,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeRKMPPH265),
|
||||||
|
URL: "ffmpeg:...#video=h265#hardware=" + EngineRKMPP,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProbeHardware(bin, name string) string {
|
||||||
|
if runtime.GOARCH == "arm64" || runtime.GOARCH == "arm" {
|
||||||
|
switch name {
|
||||||
|
case "h264":
|
||||||
|
if run(bin, ProbeV4L2M2MH264) {
|
||||||
|
return EngineV4L2M2M
|
||||||
|
}
|
||||||
|
if run(bin, ProbeRKMPPH264) {
|
||||||
|
return EngineRKMPP
|
||||||
|
}
|
||||||
|
case "h265":
|
||||||
|
if run(bin, ProbeV4L2M2MH265) {
|
||||||
|
return EngineV4L2M2M
|
||||||
|
}
|
||||||
|
if run(bin, ProbeRKMPPH265) {
|
||||||
|
return EngineRKMPP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EngineSoftware
|
||||||
|
}
|
||||||
|
|
||||||
|
return EngineSoftware
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
//go:build darwin || ios
|
||||||
|
|
||||||
|
package hardware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
const ProbeVideoToolboxH264 = "-f lavfi -i testsrc2=size=svga -t 1 -c h264_videotoolbox -f null -"
|
||||||
|
const ProbeVideoToolboxH265 = "-f lavfi -i testsrc2=size=svga -t 1 -c hevc_videotoolbox -f null -"
|
||||||
|
|
||||||
|
func ProbeAll(bin string) []*api.Source {
|
||||||
|
return []*api.Source{
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeVideoToolboxH264),
|
||||||
|
URL: "ffmpeg:...#video=h264#hardware=" + EngineVideoToolbox,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeVideoToolboxH265),
|
||||||
|
URL: "ffmpeg:...#video=h265#hardware=" + EngineVideoToolbox,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProbeHardware(bin, name string) string {
|
||||||
|
switch name {
|
||||||
|
case "h264":
|
||||||
|
if run(bin, ProbeVideoToolboxH264) {
|
||||||
|
return EngineVideoToolbox
|
||||||
|
}
|
||||||
|
|
||||||
|
case "h265":
|
||||||
|
if run(bin, ProbeVideoToolboxH265) {
|
||||||
|
return EngineVideoToolbox
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EngineSoftware
|
||||||
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
//go:build unix && !darwin && !freebsd && !netbsd && !openbsd && !dragonfly
|
||||||
|
|
||||||
|
package hardware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ProbeV4L2M2MH264 = "-f lavfi -i testsrc2 -t 1 -c h264_v4l2m2m -f null -"
|
||||||
|
ProbeV4L2M2MH265 = "-f lavfi -i testsrc2 -t 1 -c hevc_v4l2m2m -f null -"
|
||||||
|
ProbeRKMPPH264 = "-f lavfi -i testsrc2 -t 1 -c h264_rkmpp -f null -"
|
||||||
|
ProbeRKMPPH265 = "-f lavfi -i testsrc2 -t 1 -c hevc_rkmpp -f null -"
|
||||||
|
ProbeRKMPPJPEG = "-f lavfi -i testsrc2 -t 1 -c mjpeg_rkmpp -f null -"
|
||||||
|
ProbeVAAPIH264 = "-init_hw_device vaapi -f lavfi -i testsrc2 -t 1 -vf format=nv12,hwupload -c h264_vaapi -f null -"
|
||||||
|
ProbeVAAPIH265 = "-init_hw_device vaapi -f lavfi -i testsrc2 -t 1 -vf format=nv12,hwupload -c hevc_vaapi -f null -"
|
||||||
|
ProbeVAAPIJPEG = "-init_hw_device vaapi -f lavfi -i testsrc2 -t 1 -vf format=nv12,hwupload -c mjpeg_vaapi -f null -"
|
||||||
|
ProbeCUDAH264 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c h264_nvenc -f null -"
|
||||||
|
ProbeCUDAH265 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c hevc_nvenc -f null -"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ProbeAll(bin string) []*api.Source {
|
||||||
|
if runtime.GOARCH == "arm64" || runtime.GOARCH == "arm" {
|
||||||
|
return []*api.Source{
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeV4L2M2MH264),
|
||||||
|
URL: "ffmpeg:...#video=h264#hardware=" + EngineV4L2M2M,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeV4L2M2MH265),
|
||||||
|
URL: "ffmpeg:...#video=h265#hardware=" + EngineV4L2M2M,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeRKMPPH264),
|
||||||
|
URL: "ffmpeg:...#video=h264#hardware=" + EngineRKMPP,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeRKMPPH265),
|
||||||
|
URL: "ffmpeg:...#video=h265#hardware=" + EngineRKMPP,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeRKMPPJPEG),
|
||||||
|
URL: "ffmpeg:...#video=mjpeg#hardware=" + EngineRKMPP,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return []*api.Source{
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeVAAPIH264),
|
||||||
|
URL: "ffmpeg:...#video=h264#hardware=" + EngineVAAPI,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeVAAPIH265),
|
||||||
|
URL: "ffmpeg:...#video=h265#hardware=" + EngineVAAPI,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeVAAPIJPEG),
|
||||||
|
URL: "ffmpeg:...#video=mjpeg#hardware=" + EngineVAAPI,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeCUDAH264),
|
||||||
|
URL: "ffmpeg:...#video=h264#hardware=" + EngineCUDA,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeCUDAH265),
|
||||||
|
URL: "ffmpeg:...#video=h265#hardware=" + EngineCUDA,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProbeHardware(bin, name string) string {
|
||||||
|
if runtime.GOARCH == "arm64" || runtime.GOARCH == "arm" {
|
||||||
|
switch name {
|
||||||
|
case "h264":
|
||||||
|
if run(bin, ProbeV4L2M2MH264) {
|
||||||
|
return EngineV4L2M2M
|
||||||
|
}
|
||||||
|
if run(bin, ProbeRKMPPH264) {
|
||||||
|
return EngineRKMPP
|
||||||
|
}
|
||||||
|
case "h265":
|
||||||
|
if run(bin, ProbeV4L2M2MH265) {
|
||||||
|
return EngineV4L2M2M
|
||||||
|
}
|
||||||
|
if run(bin, ProbeRKMPPH265) {
|
||||||
|
return EngineRKMPP
|
||||||
|
}
|
||||||
|
case "mjpeg":
|
||||||
|
if run(bin, ProbeRKMPPJPEG) {
|
||||||
|
return EngineRKMPP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EngineSoftware
|
||||||
|
}
|
||||||
|
|
||||||
|
switch name {
|
||||||
|
case "h264":
|
||||||
|
if run(bin, ProbeCUDAH264) {
|
||||||
|
return EngineCUDA
|
||||||
|
}
|
||||||
|
if run(bin, ProbeVAAPIH264) {
|
||||||
|
return EngineVAAPI
|
||||||
|
}
|
||||||
|
|
||||||
|
case "h265":
|
||||||
|
if run(bin, ProbeCUDAH265) {
|
||||||
|
return EngineCUDA
|
||||||
|
}
|
||||||
|
if run(bin, ProbeVAAPIH265) {
|
||||||
|
return EngineVAAPI
|
||||||
|
}
|
||||||
|
|
||||||
|
case "mjpeg":
|
||||||
|
if run(bin, ProbeVAAPIJPEG) {
|
||||||
|
return EngineVAAPI
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EngineSoftware
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
//go:build windows
|
||||||
|
|
||||||
|
package hardware
|
||||||
|
|
||||||
|
import "github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
|
||||||
|
const ProbeDXVA2H264 = "-init_hw_device dxva2 -f lavfi -i testsrc2 -t 1 -c h264_qsv -f null -"
|
||||||
|
const ProbeDXVA2H265 = "-init_hw_device dxva2 -f lavfi -i testsrc2 -t 1 -c hevc_qsv -f null -"
|
||||||
|
const ProbeDXVA2JPEG = "-init_hw_device dxva2 -f lavfi -i testsrc2 -t 1 -c mjpeg_qsv -f null -"
|
||||||
|
const ProbeCUDAH264 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c h264_nvenc -f null -"
|
||||||
|
const ProbeCUDAH265 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c hevc_nvenc -f null -"
|
||||||
|
|
||||||
|
func ProbeAll(bin string) []*api.Source {
|
||||||
|
return []*api.Source{
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeDXVA2H264),
|
||||||
|
URL: "ffmpeg:...#video=h264#hardware=" + EngineDXVA2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeDXVA2H265),
|
||||||
|
URL: "ffmpeg:...#video=h265#hardware=" + EngineDXVA2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeDXVA2JPEG),
|
||||||
|
URL: "ffmpeg:...#video=mjpeg#hardware=" + EngineDXVA2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeCUDAH264),
|
||||||
|
URL: "ffmpeg:...#video=h264#hardware=" + EngineCUDA,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: runToString(bin, ProbeCUDAH265),
|
||||||
|
URL: "ffmpeg:...#video=h265#hardware=" + EngineCUDA,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProbeHardware(bin, name string) string {
|
||||||
|
switch name {
|
||||||
|
case "h264":
|
||||||
|
if run(bin, ProbeCUDAH264) {
|
||||||
|
return EngineCUDA
|
||||||
|
}
|
||||||
|
if run(bin, ProbeDXVA2H264) {
|
||||||
|
return EngineDXVA2
|
||||||
|
}
|
||||||
|
|
||||||
|
case "h265":
|
||||||
|
if run(bin, ProbeCUDAH265) {
|
||||||
|
return EngineCUDA
|
||||||
|
}
|
||||||
|
if run(bin, ProbeDXVA2H265) {
|
||||||
|
return EngineDXVA2
|
||||||
|
}
|
||||||
|
|
||||||
|
case "mjpeg":
|
||||||
|
if run(bin, ProbeDXVA2JPEG) {
|
||||||
|
return EngineDXVA2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EngineSoftware
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/ffmpeg/hardware"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/ffmpeg"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
|
)
|
||||||
|
|
||||||
|
func JPEGWithQuery(b []byte, query url.Values) ([]byte, error) {
|
||||||
|
args := parseQuery(query)
|
||||||
|
return transcode(b, args.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func JPEGWithScale(b []byte, width, height int) ([]byte, error) {
|
||||||
|
args := defaultArgs()
|
||||||
|
args.AddFilter(fmt.Sprintf("scale=%d:%d", width, height))
|
||||||
|
return transcode(b, args.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func transcode(b []byte, args string) ([]byte, error) {
|
||||||
|
cmdArgs := shell.QuoteSplit(args)
|
||||||
|
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
||||||
|
cmd.Stdin = bytes.NewBuffer(b)
|
||||||
|
return cmd.Output()
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultArgs() *ffmpeg.Args {
|
||||||
|
return &ffmpeg.Args{
|
||||||
|
Bin: defaults["bin"],
|
||||||
|
Global: defaults["global"],
|
||||||
|
Input: "-i -",
|
||||||
|
Codecs: []string{defaults["mjpeg"]},
|
||||||
|
Output: defaults["output/mjpeg"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseQuery(query url.Values) *ffmpeg.Args {
|
||||||
|
args := defaultArgs()
|
||||||
|
|
||||||
|
var width = -1
|
||||||
|
var height = -1
|
||||||
|
var r, hw string
|
||||||
|
|
||||||
|
for k, v := range query {
|
||||||
|
switch k {
|
||||||
|
case "width", "w":
|
||||||
|
width = core.Atoi(v[0])
|
||||||
|
case "height", "h":
|
||||||
|
height = core.Atoi(v[0])
|
||||||
|
case "rotate":
|
||||||
|
r = v[0]
|
||||||
|
case "hardware", "hw":
|
||||||
|
hw = v[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if width > 0 || height > 0 {
|
||||||
|
args.AddFilter(fmt.Sprintf("scale=%d:%d", width, height))
|
||||||
|
}
|
||||||
|
|
||||||
|
if r != "" {
|
||||||
|
switch r {
|
||||||
|
case "90":
|
||||||
|
args.AddFilter("transpose=1") // 90 degrees clockwise
|
||||||
|
case "180":
|
||||||
|
args.AddFilter("transpose=1,transpose=1")
|
||||||
|
case "-90", "270":
|
||||||
|
args.AddFilter("transpose=2") // 90 degrees counterclockwise
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if hw != "" {
|
||||||
|
hardware.MakeHardware(args, hw, defaults)
|
||||||
|
}
|
||||||
|
|
||||||
|
return args
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseQuery(t *testing.T) {
|
||||||
|
args := parseQuery(nil)
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -i - -c:v mjpeg -f mjpeg -`, args.String())
|
||||||
|
|
||||||
|
query, err := url.ParseQuery("h=480")
|
||||||
|
require.Nil(t, err)
|
||||||
|
args = parseQuery(query)
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -i - -c:v mjpeg -vf "scale=-1:480" -f mjpeg -`, args.String())
|
||||||
|
|
||||||
|
query, err = url.ParseQuery("hw=vaapi")
|
||||||
|
require.Nil(t, err)
|
||||||
|
args = parseQuery(query)
|
||||||
|
require.Equal(t, `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -i - -c:v mjpeg_vaapi -vf "format=vaapi|nv12,hwupload" -f mjpeg -`, args.String())
|
||||||
|
}
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/aac"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Producer struct {
|
||||||
|
core.Connection
|
||||||
|
url string
|
||||||
|
query url.Values
|
||||||
|
ffmpeg core.Producer
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewProducer - FFmpeg producer with auto selection video/audio codec based on client capabilities
|
||||||
|
func NewProducer(url string) (core.Producer, error) {
|
||||||
|
p := &Producer{}
|
||||||
|
|
||||||
|
i := strings.IndexByte(url, '#')
|
||||||
|
p.url, p.query = url[:i], streams.ParseQuery(url[i+1:])
|
||||||
|
|
||||||
|
// ffmpeg.NewProducer support only one audio
|
||||||
|
if len(p.query["video"]) != 0 || len(p.query["audio"]) != 1 {
|
||||||
|
return nil, errors.New("ffmpeg: unsupported params: " + url[i:])
|
||||||
|
}
|
||||||
|
|
||||||
|
p.ID = core.NewID()
|
||||||
|
p.FormatName = "ffmpeg"
|
||||||
|
p.Medias = []*core.Media{
|
||||||
|
{
|
||||||
|
// we can support only audio, because don't know FmtpLine for H264 and PayloadType for MJPEG
|
||||||
|
Kind: core.KindAudio,
|
||||||
|
Direction: core.DirectionRecvonly,
|
||||||
|
// codecs in order from best to worst
|
||||||
|
Codecs: []*core.Codec{
|
||||||
|
// OPUS will always marked as OPUS/48000/2
|
||||||
|
{Name: core.CodecOpus, ClockRate: 48000, Channels: 2},
|
||||||
|
{Name: core.CodecPCML, ClockRate: 16000},
|
||||||
|
{Name: core.CodecPCM, ClockRate: 16000},
|
||||||
|
{Name: core.CodecPCMA, ClockRate: 16000},
|
||||||
|
{Name: core.CodecPCMU, ClockRate: 16000},
|
||||||
|
{Name: core.CodecPCML, ClockRate: 8000},
|
||||||
|
{Name: core.CodecPCM, ClockRate: 8000},
|
||||||
|
{Name: core.CodecPCMA, ClockRate: 8000},
|
||||||
|
{Name: core.CodecPCMU, ClockRate: 8000},
|
||||||
|
// AAC has unknown problems on Dahua two way
|
||||||
|
{Name: core.CodecAAC, ClockRate: 16000, FmtpLine: aac.FMTP + "1408"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Producer) Start() error {
|
||||||
|
var err error
|
||||||
|
if p.ffmpeg, err = streams.GetProducer(p.newURL()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, media := range p.ffmpeg.GetMedias() {
|
||||||
|
track, err := p.ffmpeg.GetTrack(media, media.Codecs[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p.Receivers[i].Replace(track)
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.ffmpeg.Start()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Producer) Stop() error {
|
||||||
|
if p.ffmpeg == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return p.ffmpeg.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Producer) MarshalJSON() ([]byte, error) {
|
||||||
|
if p.ffmpeg == nil {
|
||||||
|
return json.Marshal(p.Connection)
|
||||||
|
}
|
||||||
|
return json.Marshal(p.ffmpeg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Producer) newURL() string {
|
||||||
|
s := p.url
|
||||||
|
// rewrite codecs in url from auto to known presets from defaults
|
||||||
|
for _, receiver := range p.Receivers {
|
||||||
|
codec := receiver.Codec
|
||||||
|
switch codec.Name {
|
||||||
|
case core.CodecOpus:
|
||||||
|
s += "#audio=opus/16000"
|
||||||
|
case core.CodecAAC:
|
||||||
|
s += "#audio=aac/16000"
|
||||||
|
case core.CodecPCML:
|
||||||
|
s += "#audio=pcml/" + strconv.Itoa(int(codec.ClockRate))
|
||||||
|
case core.CodecPCM:
|
||||||
|
s += "#audio=pcm/" + strconv.Itoa(int(codec.ClockRate))
|
||||||
|
case core.CodecPCMA:
|
||||||
|
s += "#audio=pcma/" + strconv.Itoa(int(codec.ClockRate))
|
||||||
|
case core.CodecPCMU:
|
||||||
|
s += "#audio=pcmu/" + strconv.Itoa(int(codec.ClockRate))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// add other params
|
||||||
|
for key, values := range p.query {
|
||||||
|
if key != "audio" {
|
||||||
|
for _, value := range values {
|
||||||
|
s += "#" + key + "=" + value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os/exec"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/ffmpeg"
|
||||||
|
)
|
||||||
|
|
||||||
|
var verMu sync.Mutex
|
||||||
|
var verErr error
|
||||||
|
var verFF string
|
||||||
|
var verAV string
|
||||||
|
|
||||||
|
func Version() (string, error) {
|
||||||
|
verMu.Lock()
|
||||||
|
defer verMu.Unlock()
|
||||||
|
|
||||||
|
if verFF != "" {
|
||||||
|
return verFF, verErr
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(defaults["bin"], "-version")
|
||||||
|
b, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
verFF = "-"
|
||||||
|
verErr = err
|
||||||
|
return verFF, verErr
|
||||||
|
}
|
||||||
|
|
||||||
|
verFF, verAV = ffmpeg.ParseVersion(b)
|
||||||
|
|
||||||
|
if verFF == "" {
|
||||||
|
verFF = "?"
|
||||||
|
}
|
||||||
|
|
||||||
|
// better to compare libavformat, because nightly/master builds
|
||||||
|
if verAV != "" && verAV < ffmpeg.Version50 {
|
||||||
|
verErr = errors.New("ffmpeg: unsupported version: " + verFF)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Str("version", verFF).Str("libavformat", verAV).Msgf("[ffmpeg] bin")
|
||||||
|
|
||||||
|
return verFF, verErr
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package virtual
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetInput(src string) string {
|
||||||
|
query, err := url.ParseQuery(src)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
input := "-re"
|
||||||
|
|
||||||
|
for _, video := range query["video"] {
|
||||||
|
// https://ffmpeg.org/ffmpeg-filters.html
|
||||||
|
sep := "=" // first separator
|
||||||
|
|
||||||
|
if video == "" {
|
||||||
|
video = "testsrc=decimals=2" // default video
|
||||||
|
sep = ":"
|
||||||
|
}
|
||||||
|
|
||||||
|
input += " -f lavfi -i " + video
|
||||||
|
|
||||||
|
// set defaults (using Add instead of Set)
|
||||||
|
query.Add("size", "1920x1080")
|
||||||
|
|
||||||
|
for key, values := range query {
|
||||||
|
value := values[0]
|
||||||
|
|
||||||
|
// https://ffmpeg.org/ffmpeg-utils.html#video-size-syntax
|
||||||
|
switch key {
|
||||||
|
case "color", "rate", "duration", "sar", "decimals":
|
||||||
|
case "size":
|
||||||
|
switch value {
|
||||||
|
case "720":
|
||||||
|
value = "1280x720" // crf=1 -> 12 Mbps
|
||||||
|
case "1080":
|
||||||
|
value = "1920x1080" // crf=1 -> 25 Mbps
|
||||||
|
case "2K":
|
||||||
|
value = "2560x1440" // crf=1 -> 43 Mbps
|
||||||
|
case "4K":
|
||||||
|
value = "3840x2160" // crf=1 -> 103 Mbps
|
||||||
|
case "8K":
|
||||||
|
value = "7680x4230" // https://reolink.com/blog/8k-resolution/
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
input += sep + key + "=" + value
|
||||||
|
sep = ":" // next separator
|
||||||
|
}
|
||||||
|
|
||||||
|
if s := query.Get("format"); s != "" {
|
||||||
|
input += ",format=" + s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetInputTTS(src string) string {
|
||||||
|
query, err := url.ParseQuery(src)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
input := `-re -f lavfi -i "flite=text='` + query.Get("text") + `'`
|
||||||
|
|
||||||
|
// ffmpeg -f lavfi -i flite=list_voices=1
|
||||||
|
// awb, kal, kal16, rms, slt
|
||||||
|
if voice := query.Get("voice"); voice != "" {
|
||||||
|
input += ":voice" + voice
|
||||||
|
}
|
||||||
|
|
||||||
|
return input + `"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package virtual
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetInput(t *testing.T) {
|
||||||
|
s := GetInput("video")
|
||||||
|
require.Equal(t, "-re -f lavfi -i testsrc=decimals=2:size=1920x1080", s)
|
||||||
|
|
||||||
|
s = GetInput("video=testsrc2&size=4K")
|
||||||
|
require.Equal(t, "-re -f lavfi -i testsrc2=size=3840x2160", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetInputTTS(t *testing.T) {
|
||||||
|
s := GetInputTTS("text=hello world&voice=slt")
|
||||||
|
require.Equal(t, `-re -f lavfi -i "flite=text='hello world':voiceslt"`, s)
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# Flussonic
|
||||||
|
|
||||||
|
[`new in v1.9.10`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.10)
|
||||||
|
|
||||||
|
Support streams from [Flussonic](https://flussonic.com/) server. Related [issue](https://github.com/AlexxIT/go2rtc/issues/1678).
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package flussonic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/flussonic"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
streams.HandleFunc("flussonic", flussonic.Dial)
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# GoPro
|
||||||
|
|
||||||
|
[`new in v1.8.3`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.8.3)
|
||||||
|
|
||||||
|
Support streaming from [GoPro](https://gopro.com/) cameras, connected via USB or Wi-Fi to Linux, Mac, Windows.
|
||||||
|
|
||||||
|
Supported models: HERO9, HERO10, HERO11, HERO12.
|
||||||
|
Supported OS: Linux, Mac, Windows, [HassOS](https://www.home-assistant.io/installation/)
|
||||||
|
|
||||||
|
Other camera models have different APIs. I will try to add them in future versions.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
- USB-connected cameras create a new network interface in the system
|
||||||
|
- Linux users do not need to install anything
|
||||||
|
- Windows users should install the [network driver](https://community.gopro.com/s/article/GoPro-Webcam)
|
||||||
|
- if the camera is detected but the stream does not start, you need to disable the firewall
|
||||||
|
|
||||||
|
1. Discover camera address: WebUI > Add > GoPro
|
||||||
|
2. Add camera to config
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
hero12: gopro://172.20.100.51
|
||||||
|
```
|
||||||
|
|
||||||
|
## Useful links
|
||||||
|
|
||||||
|
- https://gopro.github.io/OpenGoPro/
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package gopro
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/gopro"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
streams.HandleFunc("gopro", func(source string) (core.Producer, error) {
|
||||||
|
return gopro.Dial(source)
|
||||||
|
})
|
||||||
|
|
||||||
|
api.HandleFunc("api/gopro", apiGoPro)
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiGoPro(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var items []*api.Source
|
||||||
|
|
||||||
|
for _, host := range gopro.Discovery() {
|
||||||
|
items = append(items, &api.Source{Name: host, URL: "gopro://" + host})
|
||||||
|
}
|
||||||
|
|
||||||
|
api.ResponseSources(w, items)
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
# Hass
|
||||||
|
|
||||||
|
Support import camera links from [Home Assistant](https://www.home-assistant.io/) config files:
|
||||||
|
|
||||||
|
- [Generic Camera](https://www.home-assistant.io/integrations/generic/), setup via GUI
|
||||||
|
- [HomeKit Camera](https://www.home-assistant.io/integrations/homekit_controller/)
|
||||||
|
- [ONVIF](https://www.home-assistant.io/integrations/onvif/)
|
||||||
|
- [Roborock](https://github.com/humbertogontijo/homeassistant-roborock) vacuums with camera
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
hass:
|
||||||
|
config: "/config" # skip this setting if you are a Home Assistant add-on user
|
||||||
|
|
||||||
|
streams:
|
||||||
|
generic_camera: hass:Camera1 # Settings > Integrations > Integration Name
|
||||||
|
aqara_g3: hass:Camera-Hub-G3-AB12
|
||||||
|
```
|
||||||
|
|
||||||
|
### WebRTC Cameras
|
||||||
|
|
||||||
|
[`new in v1.6.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.6.0)
|
||||||
|
|
||||||
|
Any cameras in WebRTC format are supported. But at the moment Home Assistant only supports some [Nest](https://www.home-assistant.io/integrations/nest/) cameras in this format.
|
||||||
|
|
||||||
|
**Important.** The Nest API only allows you to get a link to a stream for 5 minutes.
|
||||||
|
Do not use this with Frigate! If the stream expires, Frigate will consume all available RAM on your machine within seconds.
|
||||||
|
It's recommended to use [Nest source](../nest/README.md) - it supports extending the stream.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
# link to Home Assistant Supervised
|
||||||
|
hass-webrtc1: hass://supervisor?entity_id=camera.nest_doorbell
|
||||||
|
# link to external Home Assistant with Long-Lived Access Tokens
|
||||||
|
hass-webrtc2: hass://192.168.1.123:8123?entity_id=camera.nest_doorbell&token=eyXYZ...
|
||||||
|
```
|
||||||
|
|
||||||
|
### RTSP Cameras
|
||||||
|
|
||||||
|
By default, the Home Assistant API does not allow you to get a dynamic RTSP link to a camera stream. [This method](https://github.com/felipecrs/hass-expose-camera-stream-source#importing-cameras-from-home-assistant-to-go2rtc-or-frigate) can work around it.
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package hass
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/webrtc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func apiOK(w http.ResponseWriter, r *http.Request) {
|
||||||
|
api.Response(w, `{"status":1,"payload":{}}`, api.MimeJSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiStream(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch {
|
||||||
|
// /stream/{id}/add
|
||||||
|
case strings.HasSuffix(r.RequestURI, "/add"):
|
||||||
|
var v addJSON
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// we can get three types of links:
|
||||||
|
// 1. link to go2rtc stream: rtsp://...:8554/{stream_name}
|
||||||
|
// 2. static link to Hass camera
|
||||||
|
// 3. dynamic link to Hass camera
|
||||||
|
if _, err := streams.Patch(v.Name, v.Channels.First.Url); err == nil {
|
||||||
|
apiOK(w, r)
|
||||||
|
} else {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
// /stream/{id}/channel/0/webrtc
|
||||||
|
default:
|
||||||
|
i := strings.IndexByte(r.RequestURI[8:], '/')
|
||||||
|
if i <= 0 {
|
||||||
|
http.Error(w, "", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
name := r.RequestURI[8 : 8+i]
|
||||||
|
stream := streams.Get(name)
|
||||||
|
if stream == nil {
|
||||||
|
http.Error(w, api.StreamNotFound, http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.ParseForm(); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s := r.FormValue("data")
|
||||||
|
offer, err := base64.StdEncoding.DecodeString(s)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err = webrtc.ExchangeSDP(stream, string(offer), "hass/webrtc", r.UserAgent())
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s = base64.StdEncoding.EncodeToString([]byte(s))
|
||||||
|
_, _ = w.Write([]byte(s))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HassioAddr() string {
|
||||||
|
ints, _ := net.Interfaces()
|
||||||
|
|
||||||
|
for _, i := range ints {
|
||||||
|
if i.Name != "hassio" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
addrs, _ := i.Addrs()
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if addr, ok := addr.(*net.IPNet); ok {
|
||||||
|
return addr.IP.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
type addJSON struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Channels struct {
|
||||||
|
First struct {
|
||||||
|
//Name string `json:"name"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
} `json:"0"`
|
||||||
|
} `json:"channels"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,219 @@
|
|||||||
|
package hass
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/roborock"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/hass"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
var conf struct {
|
||||||
|
API struct {
|
||||||
|
Listen string `yaml:"listen"`
|
||||||
|
} `yaml:"api"`
|
||||||
|
Mod struct {
|
||||||
|
Config string `yaml:"config"`
|
||||||
|
} `yaml:"hass"`
|
||||||
|
}
|
||||||
|
|
||||||
|
app.LoadConfig(&conf)
|
||||||
|
|
||||||
|
log = app.GetLogger("hass")
|
||||||
|
|
||||||
|
// support API for https://www.home-assistant.io/integrations/rtsp_to_webrtc/
|
||||||
|
api.HandleFunc("/static", apiOK)
|
||||||
|
api.HandleFunc("/streams", apiOK)
|
||||||
|
api.HandleFunc("/stream/", apiStream)
|
||||||
|
|
||||||
|
streams.RedirectFunc("hass", func(rawURL string) (string, error) {
|
||||||
|
rawURL, rawQuery, _ := strings.Cut(rawURL, "#")
|
||||||
|
|
||||||
|
if location := entities[rawURL[5:]]; location != "" {
|
||||||
|
if rawQuery != "" {
|
||||||
|
return location + "#" + rawQuery, nil
|
||||||
|
}
|
||||||
|
return location, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", nil
|
||||||
|
})
|
||||||
|
|
||||||
|
streams.HandleFunc("hass", func(source string) (core.Producer, error) {
|
||||||
|
// support hass://supervisor?entity_id=camera.driveway_doorbell
|
||||||
|
return hass.NewClient(source)
|
||||||
|
})
|
||||||
|
|
||||||
|
// load static entries from Hass config
|
||||||
|
if err := importConfig(conf.Mod.Config); err != nil {
|
||||||
|
log.Trace().Msgf("[hass] can't import config: %s", err)
|
||||||
|
|
||||||
|
api.HandleFunc("api/hass", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
http.Error(w, "no hass config", http.StatusNotFound)
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
api.HandleFunc("api/hass", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
once.Do(func() {
|
||||||
|
// load WebRTC entities from Hass API, works only for add-on version
|
||||||
|
if token := hass.SupervisorToken(); token != "" {
|
||||||
|
if err := importWebRTC(token); err != nil {
|
||||||
|
log.Warn().Err(err).Caller().Send()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
var items []*api.Source
|
||||||
|
for name, url := range entities {
|
||||||
|
items = append(items, &api.Source{
|
||||||
|
Name: name, URL: "hass:" + name, Location: url,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
api.ResponseSources(w, items)
|
||||||
|
})
|
||||||
|
|
||||||
|
// for Addon listen on hassio interface, so WebUI feature will work
|
||||||
|
if conf.API.Listen == "127.0.0.1:1984" {
|
||||||
|
if addr := HassioAddr(); addr != "" {
|
||||||
|
addr += ":1984"
|
||||||
|
go func() {
|
||||||
|
log.Info().Str("addr", addr).Msg("[hass] listen")
|
||||||
|
if err := http.ListenAndServe(addr, api.Handler); err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func importConfig(config string) error {
|
||||||
|
// support load cameras from Hass config file
|
||||||
|
filename := path.Join(config, ".storage/core.config_entries")
|
||||||
|
b, err := os.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var storage struct {
|
||||||
|
Data struct {
|
||||||
|
Entries []struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Domain string `json:"domain"`
|
||||||
|
Data json.RawMessage `json:"data"`
|
||||||
|
Options json.RawMessage `json:"options"`
|
||||||
|
} `json:"entries"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.Unmarshal(b, &storage); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entrie := range storage.Data.Entries {
|
||||||
|
switch entrie.Domain {
|
||||||
|
case "generic":
|
||||||
|
var options struct {
|
||||||
|
StreamSource string `json:"stream_source"`
|
||||||
|
}
|
||||||
|
if err = json.Unmarshal(entrie.Options, &options); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
entities[entrie.Title] = options.StreamSource
|
||||||
|
|
||||||
|
case "homekit_controller":
|
||||||
|
if !bytes.Contains(entrie.Data, []byte("iOSPairingId")) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var data struct {
|
||||||
|
ClientID string `json:"iOSPairingId"`
|
||||||
|
ClientPrivate string `json:"iOSDeviceLTSK"`
|
||||||
|
ClientPublic string `json:"iOSDeviceLTPK"`
|
||||||
|
DeviceID string `json:"AccessoryPairingID"`
|
||||||
|
DevicePublic string `json:"AccessoryLTPK"`
|
||||||
|
DeviceHost string `json:"AccessoryIP"`
|
||||||
|
DevicePort uint16 `json:"AccessoryPort"`
|
||||||
|
}
|
||||||
|
if err = json.Unmarshal(entrie.Data, &data); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
entities[entrie.Title] = fmt.Sprintf(
|
||||||
|
"homekit://%s:%d?client_id=%s&client_private=%s%s&device_id=%s&device_public=%s",
|
||||||
|
data.DeviceHost, data.DevicePort,
|
||||||
|
data.ClientID, data.ClientPrivate, data.ClientPublic,
|
||||||
|
data.DeviceID, data.DevicePublic,
|
||||||
|
)
|
||||||
|
|
||||||
|
case "roborock":
|
||||||
|
_ = json.Unmarshal(entrie.Data, &roborock.Auth)
|
||||||
|
|
||||||
|
case "onvif":
|
||||||
|
var data struct {
|
||||||
|
Host string `json:"host" json:"host"`
|
||||||
|
Port uint16 `json:"port" json:"port"`
|
||||||
|
Username string `json:"username" json:"username"`
|
||||||
|
Password string `json:"password" json:"password"`
|
||||||
|
}
|
||||||
|
if err = json.Unmarshal(entrie.Data, &data); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if data.Username != "" && data.Password != "" {
|
||||||
|
entities[entrie.Title] = fmt.Sprintf(
|
||||||
|
"onvif://%s:%s@%s:%d", data.Username, data.Password, data.Host, data.Port,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
entities[entrie.Title] = fmt.Sprintf("onvif://%s:%d", data.Host, data.Port)
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Str("url", "hass:"+entrie.Title).Msg("[hass] load config")
|
||||||
|
//streams.Get("hass:" + entrie.Title)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func importWebRTC(token string) error {
|
||||||
|
hassAPI, err := hass.NewAPI("ws://supervisor/core/websocket", token)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
webrtcEntities, err := hassAPI.GetWebRTCEntities()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(webrtcEntities) == 0 {
|
||||||
|
log.Debug().Msg("[hass] webrtc cameras not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, entityID := range webrtcEntities {
|
||||||
|
entities[name] = "hass://supervisor?entity_id=" + entityID
|
||||||
|
|
||||||
|
log.Debug().Msgf("[hass] load webrtc name=%s entity_id=%s", name, entityID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var entities = map[string]string{}
|
||||||
|
var log zerolog.Logger
|
||||||
|
var once sync.Once
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
# HLS
|
||||||
|
|
||||||
|
[`new in v1.1.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.1.0)
|
||||||
|
|
||||||
|
[HLS](https://en.wikipedia.org/wiki/HTTP_Live_Streaming) is the worst technology for real-time streaming.
|
||||||
|
It can only be useful on devices that do not support more modern technology, like [WebRTC](../webrtc/README.md), [MP4](../mp4/README.md).
|
||||||
|
|
||||||
|
The go2rtc implementation differs from the standards and may not work with all players.
|
||||||
|
|
||||||
|
API examples:
|
||||||
|
|
||||||
|
- HLS/TS stream: `http://192.168.1.123:1984/api/stream.m3u8?src=camera1` (H264)
|
||||||
|
- HLS/fMP4 stream: `http://192.168.1.123:1984/api/stream.m3u8?src=camera1&mp4` (H264, H265, AAC)
|
||||||
|
|
||||||
|
Read more about [codecs filters](../../README.md#codecs-filters).
|
||||||
|
|
||||||
|
## Useful links
|
||||||
|
|
||||||
|
- https://walterebert.com/playground/video/hls/
|
||||||
@@ -0,0 +1,217 @@
|
|||||||
|
package hls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api/ws"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/mp4"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/mpegts"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
log = app.GetLogger("hls")
|
||||||
|
|
||||||
|
api.HandleFunc("api/stream.m3u8", handlerStream)
|
||||||
|
api.HandleFunc("api/hls/playlist.m3u8", handlerPlaylist)
|
||||||
|
|
||||||
|
// HLS (TS)
|
||||||
|
api.HandleFunc("api/hls/segment.ts", handlerSegmentTS)
|
||||||
|
|
||||||
|
// HLS (fMP4)
|
||||||
|
api.HandleFunc("api/hls/init.mp4", handlerInit)
|
||||||
|
api.HandleFunc("api/hls/segment.m4s", handlerSegmentMP4)
|
||||||
|
|
||||||
|
ws.HandleFunc("hls", handlerWSHLS)
|
||||||
|
}
|
||||||
|
|
||||||
|
var log zerolog.Logger
|
||||||
|
|
||||||
|
const keepalive = 5 * time.Second
|
||||||
|
|
||||||
|
// once I saw 404 on MP4 segment, so better to use mutex
|
||||||
|
var sessions = map[string]*Session{}
|
||||||
|
var sessionsMu sync.RWMutex
|
||||||
|
|
||||||
|
func handlerStream(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// CORS important for Chromecast
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Content-Type", "application/vnd.apple.mpegurl")
|
||||||
|
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
src := r.URL.Query().Get("src")
|
||||||
|
stream := streams.Get(src)
|
||||||
|
if stream == nil {
|
||||||
|
http.Error(w, api.StreamNotFound, http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var cons core.Consumer
|
||||||
|
|
||||||
|
// use fMP4 with codecs filter and TS without
|
||||||
|
medias := mp4.ParseQuery(r.URL.Query())
|
||||||
|
if medias != nil {
|
||||||
|
c := mp4.NewConsumer(medias)
|
||||||
|
c.FormatName = "hls/fmp4"
|
||||||
|
c.WithRequest(r)
|
||||||
|
cons = c
|
||||||
|
} else {
|
||||||
|
c := mpegts.NewConsumer()
|
||||||
|
c.FormatName = "hls/mpegts"
|
||||||
|
c.WithRequest(r)
|
||||||
|
cons = c
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := stream.AddConsumer(cons); err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session := NewSession(cons)
|
||||||
|
session.alive = time.AfterFunc(keepalive, func() {
|
||||||
|
sessionsMu.Lock()
|
||||||
|
delete(sessions, session.id)
|
||||||
|
sessionsMu.Unlock()
|
||||||
|
|
||||||
|
stream.RemoveConsumer(cons)
|
||||||
|
})
|
||||||
|
|
||||||
|
sessionsMu.Lock()
|
||||||
|
sessions[session.id] = session
|
||||||
|
sessionsMu.Unlock()
|
||||||
|
|
||||||
|
go session.Run()
|
||||||
|
|
||||||
|
if _, err := w.Write(session.Main()); err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlerPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Content-Type", "application/vnd.apple.mpegurl")
|
||||||
|
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sid := r.URL.Query().Get("id")
|
||||||
|
sessionsMu.RLock()
|
||||||
|
session := sessions[sid]
|
||||||
|
sessionsMu.RUnlock()
|
||||||
|
if session == nil {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := w.Write(session.Playlist()); err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlerSegmentTS(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Content-Type", "video/mp2t")
|
||||||
|
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sid := r.URL.Query().Get("id")
|
||||||
|
sessionsMu.RLock()
|
||||||
|
session := sessions[sid]
|
||||||
|
sessionsMu.RUnlock()
|
||||||
|
if session == nil {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session.alive.Reset(keepalive)
|
||||||
|
|
||||||
|
data := session.Segment()
|
||||||
|
if data == nil {
|
||||||
|
log.Warn().Msgf("[hls] can't get segment %s", r.URL.RawQuery)
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := w.Write(data); err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlerInit(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Add("Content-Type", "video/mp4")
|
||||||
|
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sid := r.URL.Query().Get("id")
|
||||||
|
sessionsMu.RLock()
|
||||||
|
session := sessions[sid]
|
||||||
|
sessionsMu.RUnlock()
|
||||||
|
if session == nil {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := session.Init()
|
||||||
|
if data == nil {
|
||||||
|
log.Warn().Msgf("[hls] can't get init %s", r.URL.RawQuery)
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := w.Write(data); err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlerSegmentMP4(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Add("Content-Type", "video/iso.segment")
|
||||||
|
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
query := r.URL.Query()
|
||||||
|
|
||||||
|
sid := query.Get("id")
|
||||||
|
sessionsMu.RLock()
|
||||||
|
session := sessions[sid]
|
||||||
|
sessionsMu.RUnlock()
|
||||||
|
if session == nil {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session.alive.Reset(keepalive)
|
||||||
|
|
||||||
|
data := session.Segment()
|
||||||
|
if data == nil {
|
||||||
|
log.Warn().Msgf("[hls] can't get segment %s", r.URL.RawQuery)
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := w.Write(data); err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,127 @@
|
|||||||
|
package hls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/mp4"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Session struct {
|
||||||
|
cons core.Consumer
|
||||||
|
id string
|
||||||
|
template string
|
||||||
|
init []byte
|
||||||
|
buffer []byte
|
||||||
|
seq int
|
||||||
|
alive *time.Timer
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSession(cons core.Consumer) *Session {
|
||||||
|
s := &Session{
|
||||||
|
id: core.RandString(8, 62),
|
||||||
|
cons: cons,
|
||||||
|
}
|
||||||
|
|
||||||
|
// two segments important for Chromecast
|
||||||
|
if _, ok := cons.(*mp4.Consumer); ok {
|
||||||
|
s.template = `#EXTM3U
|
||||||
|
#EXT-X-VERSION:6
|
||||||
|
#EXT-X-TARGETDURATION:1
|
||||||
|
#EXT-X-MEDIA-SEQUENCE:%d
|
||||||
|
#EXT-X-MAP:URI="init.mp4?id=` + s.id + `"
|
||||||
|
#EXTINF:0.500,
|
||||||
|
segment.m4s?id=` + s.id + `&n=%d
|
||||||
|
#EXTINF:0.500,
|
||||||
|
segment.m4s?id=` + s.id + `&n=%d`
|
||||||
|
} else {
|
||||||
|
s.template = `#EXTM3U
|
||||||
|
#EXT-X-VERSION:3
|
||||||
|
#EXT-X-TARGETDURATION:1
|
||||||
|
#EXT-X-MEDIA-SEQUENCE:%d
|
||||||
|
#EXTINF:0.500,
|
||||||
|
segment.ts?id=` + s.id + `&n=%d
|
||||||
|
#EXTINF:0.500,
|
||||||
|
segment.ts?id=` + s.id + `&n=%d`
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) Write(p []byte) (n int, err error) {
|
||||||
|
s.mu.Lock()
|
||||||
|
if s.init == nil {
|
||||||
|
s.init = p
|
||||||
|
} else {
|
||||||
|
s.buffer = append(s.buffer, p...)
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) Run() {
|
||||||
|
_, _ = s.cons.(io.WriterTo).WriteTo(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) Main() []byte {
|
||||||
|
type withCodecs interface {
|
||||||
|
Codecs() []*core.Codec
|
||||||
|
}
|
||||||
|
|
||||||
|
codecs := mp4.MimeCodecs(s.cons.(withCodecs).Codecs())
|
||||||
|
codecs = strings.Replace(codecs, mp4.MimeFlac, "fLaC", 1)
|
||||||
|
|
||||||
|
// bandwidth important for Safari, codecs useful for smooth playback
|
||||||
|
return []byte(`#EXTM3U
|
||||||
|
#EXT-X-STREAM-INF:BANDWIDTH=192000,CODECS="` + codecs + `"
|
||||||
|
hls/playlist.m3u8?id=` + s.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) Playlist() []byte {
|
||||||
|
return []byte(fmt.Sprintf(s.template, s.seq, s.seq, s.seq+1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) Init() (init []byte) {
|
||||||
|
for i := 0; i < 60 && init == nil; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
// return init only when have some buffer
|
||||||
|
if len(s.buffer) > 0 {
|
||||||
|
init = s.init
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) Segment() (segment []byte) {
|
||||||
|
for i := 0; i < 60 && segment == nil; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
if len(s.buffer) > 0 {
|
||||||
|
segment = s.buffer
|
||||||
|
if _, ok := s.cons.(*mp4.Consumer); ok {
|
||||||
|
s.buffer = nil
|
||||||
|
} else {
|
||||||
|
// for TS important to start new segment with init
|
||||||
|
s.buffer = s.init
|
||||||
|
}
|
||||||
|
s.seq++
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package hls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api/ws"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/mp4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handlerWSHLS(tr *ws.Transport, msg *ws.Message) error {
|
||||||
|
stream, _ := streams.GetOrPatch(tr.Request.URL.Query())
|
||||||
|
if stream == nil {
|
||||||
|
return errors.New(api.StreamNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
codecs := msg.String()
|
||||||
|
medias := mp4.ParseCodecs(codecs, true)
|
||||||
|
cons := mp4.NewConsumer(medias)
|
||||||
|
cons.FormatName = "hls/fmp4"
|
||||||
|
cons.WithRequest(tr.Request)
|
||||||
|
|
||||||
|
log.Trace().Msgf("[hls] new ws consumer codecs=%s", codecs)
|
||||||
|
|
||||||
|
if err := stream.AddConsumer(cons); err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
session := NewSession(cons)
|
||||||
|
|
||||||
|
session.alive = time.AfterFunc(keepalive, func() {
|
||||||
|
sessionsMu.Lock()
|
||||||
|
delete(sessions, session.id)
|
||||||
|
sessionsMu.Unlock()
|
||||||
|
|
||||||
|
stream.RemoveConsumer(cons)
|
||||||
|
})
|
||||||
|
|
||||||
|
sessionsMu.Lock()
|
||||||
|
sessions[session.id] = session
|
||||||
|
sessionsMu.Unlock()
|
||||||
|
|
||||||
|
go session.Run()
|
||||||
|
|
||||||
|
main := session.Main()
|
||||||
|
tr.Write(&ws.Message{Type: "hls", Value: string(main)})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
# Apple HomeKit
|
||||||
|
|
||||||
|
This module supports both client and server for the [Apple HomeKit](https://www.apple.com/home-app/accessories/) protocol.
|
||||||
|
|
||||||
|
## HomeKit Client
|
||||||
|
|
||||||
|
**Important:**
|
||||||
|
|
||||||
|
- You can use HomeKit Cameras **without Apple devices** (iPhone, iPad, etc.), it's just a yet another protocol
|
||||||
|
- HomeKit device can be paired with only one ecosystem. So, if you have paired it to an iPhone (Apple Home), you can't pair it with Home Assistant or go2rtc. Or if you have paired it to go2rtc, you can't pair it with an iPhone
|
||||||
|
- HomeKit device should be on the same network with working [mDNS](https://en.wikipedia.org/wiki/Multicast_DNS) between the device and go2rtc
|
||||||
|
|
||||||
|
go2rtc supports importing paired HomeKit devices from [Home Assistant](../hass/README.md).
|
||||||
|
So you can use HomeKit camera with Home Assistant and go2rtc simultaneously.
|
||||||
|
If you are using Home Assistant, I recommend pairing devices with it; it will give you more options.
|
||||||
|
|
||||||
|
You can pair device with go2rtc on the HomeKit page. If you can't see your devices, reload the page.
|
||||||
|
Also, try rebooting your HomeKit device (power off). If you still can't see it, you have a problem with mDNS.
|
||||||
|
|
||||||
|
If you see a device but it does not have a pairing button, it is paired to some ecosystem (Apple Home, Home Assistant, HomeBridge, etc.). You need to delete the device from that ecosystem, and it will be available for pairing. If you cannot unpair the device, you will have to reset it.
|
||||||
|
|
||||||
|
**Important:**
|
||||||
|
|
||||||
|
- HomeKit audio uses very non-standard **AAC-ELD** codec with very non-standard params and specification violations
|
||||||
|
- Audio can't be played in `VLC` and probably any other player
|
||||||
|
- Audio should be transcoded for use with MSE, WebRTC, etc.
|
||||||
|
|
||||||
|
### Client Configuration
|
||||||
|
|
||||||
|
Recommended settings for using HomeKit Camera with WebRTC, MSE, MP4, RTSP:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
aqara_g3:
|
||||||
|
- hass:Camera-Hub-G3-AB12
|
||||||
|
- ffmpeg:aqara_g3#audio=aac#audio=opus
|
||||||
|
```
|
||||||
|
|
||||||
|
RTSP link with "normal" audio for any player: `rtsp://192.168.1.123:8554/aqara_g3?video&audio=aac`
|
||||||
|
|
||||||
|
**This source is in active development!** Tested only with [Aqara Camera Hub G3](https://www.aqara.com/eu/product/camera-hub-g3) (both EU and CN versions).
|
||||||
|
|
||||||
|
## HomeKit Server
|
||||||
|
|
||||||
|
[`new in v1.7.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.7.0)
|
||||||
|
|
||||||
|
HomeKit module can work in two modes:
|
||||||
|
|
||||||
|
- export any H264 camera to Apple HomeKit
|
||||||
|
- transparent proxy any Apple HomeKit camera (Aqara, Eve, Eufy, etc.) back to Apple HomeKit, so you will have all camera features in Apple Home and also will have RTSP/WebRTC/MP4/etc. from your HomeKit camera
|
||||||
|
|
||||||
|
**Important**
|
||||||
|
|
||||||
|
- HomeKit cameras support only H264 video and OPUS audio
|
||||||
|
|
||||||
|
### Server Configuration
|
||||||
|
|
||||||
|
**Minimal config**
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
dahua1: rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0
|
||||||
|
homekit:
|
||||||
|
dahua1: # same stream ID from streams list, default PIN - 19550224
|
||||||
|
```
|
||||||
|
|
||||||
|
**Full config**
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
dahua1:
|
||||||
|
- rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0
|
||||||
|
- ffmpeg:dahua1#video=h264#hardware # if your camera doesn't support H264, important for HomeKit
|
||||||
|
- ffmpeg:dahua1#audio=opus # only OPUS audio supported by HomeKit
|
||||||
|
|
||||||
|
homekit:
|
||||||
|
dahua1: # same stream ID from streams list
|
||||||
|
pin: 12345678 # custom PIN, default: 19550224
|
||||||
|
name: Dahua camera # custom camera name, default: generated from stream ID
|
||||||
|
device_id: dahua1 # custom ID, default: generated from stream ID
|
||||||
|
device_private: dahua1 # custom key, default: generated from stream ID
|
||||||
|
```
|
||||||
|
|
||||||
|
**Proxy HomeKit camera**
|
||||||
|
|
||||||
|
- Video stream from HomeKit camera to Apple device (iPhone, Apple TV) will be transmitted directly
|
||||||
|
- Video stream from HomeKit camera to RTSP/WebRTC/MP4/etc. will be transmitted via go2rtc
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
aqara1:
|
||||||
|
- homekit://...
|
||||||
|
- ffmpeg:aqara1#audio=aac#audio=opus # optional audio transcoding
|
||||||
|
|
||||||
|
homekit:
|
||||||
|
aqara1: # same stream ID from streams list
|
||||||
|
```
|
||||||
@@ -0,0 +1,181 @@
|
|||||||
|
package homekit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/hap"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/mdns"
|
||||||
|
)
|
||||||
|
|
||||||
|
func apiDiscovery(w http.ResponseWriter, r *http.Request) {
|
||||||
|
sources, err := discovery()
|
||||||
|
if err != nil {
|
||||||
|
api.Error(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
urls := findHomeKitURLs()
|
||||||
|
for id, u := range urls {
|
||||||
|
deviceID := u.Query().Get("device_id")
|
||||||
|
for _, source := range sources {
|
||||||
|
if strings.Contains(source.URL, deviceID) {
|
||||||
|
source.Location = id
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, source := range sources {
|
||||||
|
if source.Location == "" {
|
||||||
|
source.Location = " "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
api.ResponseSources(w, sources)
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiHomekit(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := r.ParseForm(); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case "GET":
|
||||||
|
if id := r.Form.Get("id"); id != "" {
|
||||||
|
if srv := servers[id]; srv != nil {
|
||||||
|
api.ResponsePrettyJSON(w, srv)
|
||||||
|
} else {
|
||||||
|
http.Error(w, "server not found", http.StatusNotFound)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
api.ResponsePrettyJSON(w, servers)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "POST":
|
||||||
|
id := r.Form.Get("id")
|
||||||
|
rawURL := r.Form.Get("src") + "&pin=" + r.Form.Get("pin")
|
||||||
|
if err := apiPair(id, rawURL); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "DELETE":
|
||||||
|
id := r.Form.Get("id")
|
||||||
|
if err := apiUnpair(id); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiHomekitAccessories(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id := r.URL.Query().Get("id")
|
||||||
|
stream := streams.Get(id)
|
||||||
|
if stream == nil {
|
||||||
|
http.Error(w, "", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rawURL := findHomeKitURL(stream.Sources())
|
||||||
|
if rawURL == "" {
|
||||||
|
http.Error(w, "", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := hap.Dial(rawURL)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
res, err := client.Get(hap.PathAccessories)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", api.MimeJSON)
|
||||||
|
_, _ = io.Copy(w, res.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func discovery() ([]*api.Source, error) {
|
||||||
|
var sources []*api.Source
|
||||||
|
|
||||||
|
// 1. Get streams from Discovery
|
||||||
|
err := mdns.Discovery(mdns.ServiceHAP, func(entry *mdns.ServiceEntry) bool {
|
||||||
|
log.Trace().Msgf("[homekit] mdns=%s", entry)
|
||||||
|
|
||||||
|
category := entry.Info[hap.TXTCategory]
|
||||||
|
if entry.Complete() && (category == hap.CategoryCamera || category == hap.CategoryDoorbell) {
|
||||||
|
source := &api.Source{
|
||||||
|
Name: entry.Name,
|
||||||
|
Info: entry.Info[hap.TXTModel],
|
||||||
|
URL: fmt.Sprintf(
|
||||||
|
"homekit://%s:%d?device_id=%s&feature=%s&status=%s",
|
||||||
|
entry.IP, entry.Port, entry.Info[hap.TXTDeviceID],
|
||||||
|
entry.Info[hap.TXTFeatureFlags], entry.Info[hap.TXTStatusFlags],
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
sources = append(sources, source)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return sources, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiPair(id, url string) error {
|
||||||
|
conn, err := hap.Pair(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
streams.New(id, conn.URL())
|
||||||
|
|
||||||
|
return app.PatchConfig([]string{"streams", id}, conn.URL())
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiUnpair(id string) error {
|
||||||
|
stream := streams.Get(id)
|
||||||
|
if stream == nil {
|
||||||
|
return errors.New(api.StreamNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
rawURL := findHomeKitURL(stream.Sources())
|
||||||
|
if rawURL == "" {
|
||||||
|
return errors.New("not homekit source")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := hap.Unpair(rawURL); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
streams.Delete(id)
|
||||||
|
|
||||||
|
return app.PatchConfig([]string{"streams", id}, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func findHomeKitURLs() map[string]*url.URL {
|
||||||
|
urls := map[string]*url.URL{}
|
||||||
|
for name, sources := range streams.GetAllSources() {
|
||||||
|
if rawURL := findHomeKitURL(sources); rawURL != "" {
|
||||||
|
if u, err := url.Parse(rawURL); err == nil {
|
||||||
|
urls[name] = u
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return urls
|
||||||
|
}
|
||||||
@@ -0,0 +1,211 @@
|
|||||||
|
package homekit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/srtp"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/hap"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/hap/camera"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/homekit"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/mdns"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
var cfg struct {
|
||||||
|
Mod map[string]struct {
|
||||||
|
Pin string `yaml:"pin"`
|
||||||
|
Name string `yaml:"name"`
|
||||||
|
DeviceID string `yaml:"device_id"`
|
||||||
|
DevicePrivate string `yaml:"device_private"`
|
||||||
|
CategoryID string `yaml:"category_id"`
|
||||||
|
Pairings []string `yaml:"pairings"`
|
||||||
|
} `yaml:"homekit"`
|
||||||
|
}
|
||||||
|
app.LoadConfig(&cfg)
|
||||||
|
|
||||||
|
log = app.GetLogger("homekit")
|
||||||
|
|
||||||
|
streams.HandleFunc("homekit", streamHandler)
|
||||||
|
|
||||||
|
api.HandleFunc("api/homekit", apiHomekit)
|
||||||
|
api.HandleFunc("api/homekit/accessories", apiHomekitAccessories)
|
||||||
|
api.HandleFunc("api/discovery/homekit", apiDiscovery)
|
||||||
|
|
||||||
|
if cfg.Mod == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hosts = map[string]*server{}
|
||||||
|
servers = map[string]*server{}
|
||||||
|
var entries []*mdns.ServiceEntry
|
||||||
|
|
||||||
|
for id, conf := range cfg.Mod {
|
||||||
|
stream := streams.Get(id)
|
||||||
|
if stream == nil {
|
||||||
|
log.Warn().Msgf("[homekit] missing stream: %s", id)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if conf.Pin == "" {
|
||||||
|
conf.Pin = "19550224" // default PIN
|
||||||
|
}
|
||||||
|
|
||||||
|
pin, err := hap.SanitizePin(conf.Pin)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceID := calcDeviceID(conf.DeviceID, id) // random MAC-address
|
||||||
|
name := calcName(conf.Name, deviceID)
|
||||||
|
setupID := calcSetupID(id)
|
||||||
|
|
||||||
|
srv := &server{
|
||||||
|
stream: id,
|
||||||
|
pairings: conf.Pairings,
|
||||||
|
setupID: setupID,
|
||||||
|
}
|
||||||
|
|
||||||
|
srv.hap = &hap.Server{
|
||||||
|
Pin: pin,
|
||||||
|
DeviceID: deviceID,
|
||||||
|
DevicePrivate: calcDevicePrivate(conf.DevicePrivate, id),
|
||||||
|
GetClientPublic: srv.GetPair,
|
||||||
|
}
|
||||||
|
|
||||||
|
srv.mdns = &mdns.ServiceEntry{
|
||||||
|
Name: name,
|
||||||
|
Port: uint16(api.Port),
|
||||||
|
Info: map[string]string{
|
||||||
|
hap.TXTConfigNumber: "1",
|
||||||
|
hap.TXTFeatureFlags: "0",
|
||||||
|
hap.TXTDeviceID: deviceID,
|
||||||
|
hap.TXTModel: app.UserAgent,
|
||||||
|
hap.TXTProtoVersion: "1.1",
|
||||||
|
hap.TXTStateNumber: "1",
|
||||||
|
hap.TXTStatusFlags: hap.StatusNotPaired,
|
||||||
|
hap.TXTCategory: calcCategoryID(conf.CategoryID),
|
||||||
|
hap.TXTSetupHash: hap.SetupHash(setupID, deviceID),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
entries = append(entries, srv.mdns)
|
||||||
|
|
||||||
|
srv.UpdateStatus()
|
||||||
|
|
||||||
|
if url := findHomeKitURL(stream.Sources()); url != "" {
|
||||||
|
// 1. Act as transparent proxy for HomeKit camera
|
||||||
|
srv.proxyURL = url
|
||||||
|
} else {
|
||||||
|
// 2. Act as basic HomeKit camera
|
||||||
|
srv.accessory = camera.NewAccessory("AlexxIT", "go2rtc", name, "-", app.Version)
|
||||||
|
}
|
||||||
|
|
||||||
|
host := srv.mdns.Host(mdns.ServiceHAP)
|
||||||
|
hosts[host] = srv
|
||||||
|
servers[id] = srv
|
||||||
|
|
||||||
|
log.Trace().Msgf("[homekit] new server: %s", srv.mdns)
|
||||||
|
}
|
||||||
|
|
||||||
|
api.HandleFunc(hap.PathPairSetup, hapHandler)
|
||||||
|
api.HandleFunc(hap.PathPairVerify, hapHandler)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := mdns.Serve(mdns.ServiceHAP, entries); err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
var log zerolog.Logger
|
||||||
|
var hosts map[string]*server
|
||||||
|
var servers map[string]*server
|
||||||
|
|
||||||
|
func streamHandler(rawURL string) (core.Producer, error) {
|
||||||
|
if srtp.Server == nil {
|
||||||
|
return nil, errors.New("homekit: can't work without SRTP server")
|
||||||
|
}
|
||||||
|
|
||||||
|
rawURL, rawQuery, _ := strings.Cut(rawURL, "#")
|
||||||
|
client, err := homekit.Dial(rawURL, srtp.Server)
|
||||||
|
if client != nil && rawQuery != "" {
|
||||||
|
query := streams.ParseQuery(rawQuery)
|
||||||
|
client.MaxWidth = core.Atoi(query.Get("maxwidth"))
|
||||||
|
client.MaxHeight = core.Atoi(query.Get("maxheight"))
|
||||||
|
client.Bitrate = parseBitrate(query.Get("bitrate"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolve(host string) *server {
|
||||||
|
if len(hosts) == 1 {
|
||||||
|
for _, srv := range hosts {
|
||||||
|
return srv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if srv, ok := hosts[host]; ok {
|
||||||
|
return srv
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func hapHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Can support multiple HomeKit cameras on single port ONLY for Apple devices.
|
||||||
|
// Doesn't support Home Assistant and any other open source projects
|
||||||
|
// because they don't send the host header in requests.
|
||||||
|
srv := resolve(r.Host)
|
||||||
|
if srv == nil {
|
||||||
|
log.Error().Msg("[homekit] unknown host: " + r.Host)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
srv.Handle(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func findHomeKitURL(sources []string) string {
|
||||||
|
if len(sources) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
url := sources[0]
|
||||||
|
if strings.HasPrefix(url, "homekit") {
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(url, "hass") {
|
||||||
|
location, _ := streams.Location(url)
|
||||||
|
if strings.HasPrefix(location, "homekit") {
|
||||||
|
return location
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseBitrate(s string) int {
|
||||||
|
n := len(s)
|
||||||
|
if n == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var k int
|
||||||
|
switch n--; s[n] {
|
||||||
|
case 'K':
|
||||||
|
k = 1024
|
||||||
|
s = s[:n]
|
||||||
|
case 'M':
|
||||||
|
k = 1024 * 1024
|
||||||
|
s = s[:n]
|
||||||
|
default:
|
||||||
|
k = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return k * core.Atoi(s)
|
||||||
|
}
|
||||||
@@ -0,0 +1,405 @@
|
|||||||
|
package homekit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/sha512"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/ffmpeg"
|
||||||
|
srtp2 "github.com/AlexxIT/go2rtc/internal/srtp"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/hap"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/hap/camera"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/hap/hds"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/hap/tlv8"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/homekit"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/magic"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/mdns"
|
||||||
|
)
|
||||||
|
|
||||||
|
type server struct {
|
||||||
|
hap *hap.Server // server for HAP connection and encryption
|
||||||
|
mdns *mdns.ServiceEntry
|
||||||
|
|
||||||
|
pairings []string // pairings list
|
||||||
|
conns []any
|
||||||
|
mu sync.Mutex
|
||||||
|
|
||||||
|
accessory *hap.Accessory // HAP accessory
|
||||||
|
consumer *homekit.Consumer
|
||||||
|
proxyURL string
|
||||||
|
setupID string
|
||||||
|
stream string // stream name from YAML
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) MarshalJSON() ([]byte, error) {
|
||||||
|
v := struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
DeviceID string `json:"device_id"`
|
||||||
|
Paired int `json:"paired,omitempty"`
|
||||||
|
CategoryID string `json:"category_id,omitempty"`
|
||||||
|
SetupCode string `json:"setup_code,omitempty"`
|
||||||
|
SetupID string `json:"setup_id,omitempty"`
|
||||||
|
Conns []any `json:"connections,omitempty"`
|
||||||
|
}{
|
||||||
|
Name: s.mdns.Name,
|
||||||
|
DeviceID: s.mdns.Info[hap.TXTDeviceID],
|
||||||
|
CategoryID: s.mdns.Info[hap.TXTCategory],
|
||||||
|
Paired: len(s.pairings),
|
||||||
|
Conns: s.conns,
|
||||||
|
}
|
||||||
|
if v.Paired == 0 {
|
||||||
|
v.SetupCode = s.hap.Pin
|
||||||
|
v.SetupID = s.setupID
|
||||||
|
}
|
||||||
|
return json.Marshal(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) Handle(w http.ResponseWriter, r *http.Request) {
|
||||||
|
conn, rw, err := w.(http.Hijacker).Hijack()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
// Fix reading from Body after Hijack.
|
||||||
|
r.Body = io.NopCloser(rw)
|
||||||
|
|
||||||
|
switch r.RequestURI {
|
||||||
|
case hap.PathPairSetup:
|
||||||
|
id, key, err := s.hap.PairSetup(r, rw)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.AddPair(id, key, hap.PermissionAdmin)
|
||||||
|
|
||||||
|
case hap.PathPairVerify:
|
||||||
|
id, key, err := s.hap.PairVerify(r, rw)
|
||||||
|
if err != nil {
|
||||||
|
log.Debug().Err(err).Caller().Send()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Str("stream", s.stream).Str("client_id", id).Msgf("[homekit] %s: new conn", conn.RemoteAddr())
|
||||||
|
|
||||||
|
controller, err := hap.NewConn(conn, rw, key, false)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.AddConn(controller)
|
||||||
|
defer s.DelConn(controller)
|
||||||
|
|
||||||
|
var handler homekit.HandlerFunc
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case s.accessory != nil:
|
||||||
|
handler = homekit.ServerHandler(s)
|
||||||
|
case s.proxyURL != "":
|
||||||
|
client, err := hap.Dial(s.proxyURL)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
handler = homekit.ProxyHandler(s, client.Conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If your iPhone goes to sleep, it will be an EOF error.
|
||||||
|
if err = handler(controller); err != nil && !errors.Is(err, io.EOF) {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type logger struct {
|
||||||
|
v any
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l logger) String() string {
|
||||||
|
switch v := l.v.(type) {
|
||||||
|
case *hap.Conn:
|
||||||
|
return "hap " + v.RemoteAddr().String()
|
||||||
|
case *hds.Conn:
|
||||||
|
return "hds " + v.RemoteAddr().String()
|
||||||
|
case *homekit.Consumer:
|
||||||
|
return "rtp " + v.RemoteAddr
|
||||||
|
}
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) AddConn(v any) {
|
||||||
|
log.Trace().Str("stream", s.stream).Msgf("[homekit] add conn %s", logger{v})
|
||||||
|
s.mu.Lock()
|
||||||
|
s.conns = append(s.conns, v)
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) DelConn(v any) {
|
||||||
|
log.Trace().Str("stream", s.stream).Msgf("[homekit] del conn %s", logger{v})
|
||||||
|
s.mu.Lock()
|
||||||
|
if i := slices.Index(s.conns, v); i >= 0 {
|
||||||
|
s.conns = slices.Delete(s.conns, i, i+1)
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) UpdateStatus() {
|
||||||
|
// true status is important, or device may be offline in Apple Home
|
||||||
|
if len(s.pairings) == 0 {
|
||||||
|
s.mdns.Info[hap.TXTStatusFlags] = hap.StatusNotPaired
|
||||||
|
} else {
|
||||||
|
s.mdns.Info[hap.TXTStatusFlags] = hap.StatusPaired
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) pairIndex(id string) int {
|
||||||
|
id = "client_id=" + id
|
||||||
|
for i, pairing := range s.pairings {
|
||||||
|
if strings.HasPrefix(pairing, id) {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) GetPair(id string) []byte {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
|
if i := s.pairIndex(id); i >= 0 {
|
||||||
|
query, _ := url.ParseQuery(s.pairings[i])
|
||||||
|
b, _ := hex.DecodeString(query.Get("client_public"))
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) AddPair(id string, public []byte, permissions byte) {
|
||||||
|
log.Debug().Str("stream", s.stream).Msgf("[homekit] add pair id=%s public=%x perm=%d", id, public, permissions)
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
if s.pairIndex(id) < 0 {
|
||||||
|
s.pairings = append(s.pairings, fmt.Sprintf(
|
||||||
|
"client_id=%s&client_public=%x&permissions=%d", id, public, permissions,
|
||||||
|
))
|
||||||
|
s.UpdateStatus()
|
||||||
|
s.PatchConfig()
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) DelPair(id string) {
|
||||||
|
log.Debug().Str("stream", s.stream).Msgf("[homekit] del pair id=%s", id)
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
if i := s.pairIndex(id); i >= 0 {
|
||||||
|
s.pairings = append(s.pairings[:i], s.pairings[i+1:]...)
|
||||||
|
s.UpdateStatus()
|
||||||
|
s.PatchConfig()
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) PatchConfig() {
|
||||||
|
if err := app.PatchConfig([]string{"homekit", s.stream, "pairings"}, s.pairings); err != nil {
|
||||||
|
log.Error().Err(err).Msgf(
|
||||||
|
"[homekit] can't save %s pairings=%v", s.stream, s.pairings,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) GetAccessories(_ net.Conn) []*hap.Accessory {
|
||||||
|
return []*hap.Accessory{s.accessory}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) GetCharacteristic(conn net.Conn, aid uint8, iid uint64) any {
|
||||||
|
log.Trace().Str("stream", s.stream).Msgf("[homekit] get char aid=%d iid=0x%x", aid, iid)
|
||||||
|
|
||||||
|
char := s.accessory.GetCharacterByID(iid)
|
||||||
|
if char == nil {
|
||||||
|
log.Warn().Msgf("[homekit] get unknown characteristic: %d", iid)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch char.Type {
|
||||||
|
case camera.TypeSetupEndpoints:
|
||||||
|
consumer := s.consumer
|
||||||
|
if consumer == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
answer := consumer.GetAnswer()
|
||||||
|
v, err := tlv8.MarshalBase64(answer)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
return char.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) SetCharacteristic(conn net.Conn, aid uint8, iid uint64, value any) {
|
||||||
|
log.Trace().Str("stream", s.stream).Msgf("[homekit] set char aid=%d iid=0x%x value=%v", aid, iid, value)
|
||||||
|
|
||||||
|
char := s.accessory.GetCharacterByID(iid)
|
||||||
|
if char == nil {
|
||||||
|
log.Warn().Msgf("[homekit] set unknown characteristic: %d", iid)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch char.Type {
|
||||||
|
case camera.TypeSetupEndpoints:
|
||||||
|
var offer camera.SetupEndpointsRequest
|
||||||
|
if err := tlv8.UnmarshalBase64(value, &offer); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
consumer := homekit.NewConsumer(conn, srtp2.Server)
|
||||||
|
consumer.SetOffer(&offer)
|
||||||
|
s.consumer = consumer
|
||||||
|
|
||||||
|
case camera.TypeSelectedStreamConfiguration:
|
||||||
|
var conf camera.SelectedStreamConfiguration
|
||||||
|
if err := tlv8.UnmarshalBase64(value, &conf); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Trace().Str("stream", s.stream).Msgf("[homekit] stream id=%x cmd=%d", conf.Control.SessionID, conf.Control.Command)
|
||||||
|
|
||||||
|
switch conf.Control.Command {
|
||||||
|
case camera.SessionCommandEnd:
|
||||||
|
for _, consumer := range s.conns {
|
||||||
|
if consumer, ok := consumer.(*homekit.Consumer); ok {
|
||||||
|
if consumer.SessionID() == conf.Control.SessionID {
|
||||||
|
_ = consumer.Stop()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case camera.SessionCommandStart:
|
||||||
|
consumer := s.consumer
|
||||||
|
if consumer == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !consumer.SetConfig(&conf) {
|
||||||
|
log.Warn().Msgf("[homekit] wrong config")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.AddConn(consumer)
|
||||||
|
|
||||||
|
stream := streams.Get(s.stream)
|
||||||
|
if err := stream.AddConsumer(consumer); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
_, _ = consumer.WriteTo(nil)
|
||||||
|
stream.RemoveConsumer(consumer)
|
||||||
|
|
||||||
|
s.DelConn(consumer)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) GetImage(conn net.Conn, width, height int) []byte {
|
||||||
|
log.Trace().Str("stream", s.stream).Msgf("[homekit] get image width=%d height=%d", width, height)
|
||||||
|
|
||||||
|
stream := streams.Get(s.stream)
|
||||||
|
cons := magic.NewKeyframe()
|
||||||
|
|
||||||
|
if err := stream.AddConsumer(cons); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
once := &core.OnceBuffer{} // init and first frame
|
||||||
|
_, _ = cons.WriteTo(once)
|
||||||
|
b := once.Buffer()
|
||||||
|
|
||||||
|
stream.RemoveConsumer(cons)
|
||||||
|
|
||||||
|
switch cons.CodecName() {
|
||||||
|
case core.CodecH264, core.CodecH265:
|
||||||
|
var err error
|
||||||
|
if b, err = ffmpeg.JPEGWithScale(b, width, height); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func calcName(name, seed string) string {
|
||||||
|
if name != "" {
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
b := sha512.Sum512([]byte(seed))
|
||||||
|
return fmt.Sprintf("go2rtc-%02X%02X", b[0], b[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
func calcDeviceID(deviceID, seed string) string {
|
||||||
|
if deviceID != "" {
|
||||||
|
if len(deviceID) >= 17 {
|
||||||
|
// 1. Returd device_id as is (ex. AA:BB:CC:DD:EE:FF)
|
||||||
|
return deviceID
|
||||||
|
}
|
||||||
|
// 2. Use device_id as seed if not zero
|
||||||
|
seed = deviceID
|
||||||
|
}
|
||||||
|
b := sha512.Sum512([]byte(seed))
|
||||||
|
return fmt.Sprintf("%02X:%02X:%02X:%02X:%02X:%02X", b[32], b[34], b[36], b[38], b[40], b[42])
|
||||||
|
}
|
||||||
|
|
||||||
|
func calcDevicePrivate(private, seed string) []byte {
|
||||||
|
if private != "" {
|
||||||
|
// 1. Decode private from HEX string
|
||||||
|
if b, _ := hex.DecodeString(private); len(b) == ed25519.PrivateKeySize {
|
||||||
|
// 2. Return if OK
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
// 3. Use private as seed if not zero
|
||||||
|
seed = private
|
||||||
|
}
|
||||||
|
b := sha512.Sum512([]byte(seed))
|
||||||
|
return ed25519.NewKeyFromSeed(b[:ed25519.SeedSize])
|
||||||
|
}
|
||||||
|
|
||||||
|
func calcSetupID(seed string) string {
|
||||||
|
b := sha512.Sum512([]byte(seed))
|
||||||
|
return fmt.Sprintf("%02X%02X", b[44], b[46])
|
||||||
|
}
|
||||||
|
|
||||||
|
func calcCategoryID(categoryID string) string {
|
||||||
|
switch categoryID {
|
||||||
|
case "bridge":
|
||||||
|
return hap.CategoryBridge
|
||||||
|
case "doorbell":
|
||||||
|
return hap.CategoryDoorbell
|
||||||
|
}
|
||||||
|
if core.Atoi(categoryID) > 0 {
|
||||||
|
return categoryID
|
||||||
|
}
|
||||||
|
return hap.CategoryCamera
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
# HTTP
|
||||||
|
|
||||||
|
This source supports receiving a stream via an HTTP link.
|
||||||
|
|
||||||
|
It can determine the source format from the`Content-Type` HTTP header:
|
||||||
|
|
||||||
|
- **HTTP-JPEG** (`image/jpeg`) - camera snapshot link, can be converted by go2rtc to MJPEG stream
|
||||||
|
- **HTTP-MJPEG** (`multipart/x-mixed-replace`) - A continuous sequence of JPEG frames (with HTTP headers).
|
||||||
|
- **HLS** (`application/vnd.apple.mpegurl`) - A popular [HTTP Live Streaming](https://en.wikipedia.org/wiki/HTTP_Live_Streaming) (HLS) format, which is not designed for real-time media transmission.
|
||||||
|
|
||||||
|
> [!WARNING]
|
||||||
|
> The HLS format is not designed for real time and is supported quite poorly. It is recommended to use it via ffmpeg source with buffering enabled (disabled by default).
|
||||||
|
|
||||||
|
## TCP
|
||||||
|
|
||||||
|
Source also supports HTTP and TCP streams with autodetection for different formats:
|
||||||
|
|
||||||
|
- `adts` - Audio stream in [AAC](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) codec with Audio Data Transport Stream (ADTS) headers.
|
||||||
|
- `flv` - The legacy but still used [Flash Video](https://en.wikipedia.org/wiki/Flash_Video) format.
|
||||||
|
- `h264` - AVC/H.264 bitstream.
|
||||||
|
- `hevc` - HEVC/H.265 bitstream.
|
||||||
|
- `mjpeg` - A continuous sequence of JPEG frames (without HTTP headers).
|
||||||
|
- `mpegts` - The legacy [MPEG transport stream](https://en.wikipedia.org/wiki/MPEG_transport_stream) format.
|
||||||
|
- `wav` - Audio stream in [WAV](https://en.wikipedia.org/wiki/WAV) format.
|
||||||
|
- `yuv4mpegpipe` - Raw YUV frame stream with YUV4MPEG header.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
# [HTTP-FLV] stream in video/x-flv format
|
||||||
|
http_flv: http://192.168.1.123:20880/api/camera/stream/780900131155/657617
|
||||||
|
|
||||||
|
# [JPEG] snapshots from Dahua camera, will be converted to MJPEG stream
|
||||||
|
dahua_snap: http://admin:password@192.168.1.123/cgi-bin/snapshot.cgi?channel=1
|
||||||
|
|
||||||
|
# [MJPEG] stream will be proxied without modification
|
||||||
|
http_mjpeg: https://mjpeg.sanford.io/count.mjpeg
|
||||||
|
|
||||||
|
# [MJPEG or H.264/H.265 bitstream or MPEG-TS]
|
||||||
|
tcp_magic: tcp://192.168.1.123:12345
|
||||||
|
|
||||||
|
# Add custom header
|
||||||
|
custom_header: "https://mjpeg.sanford.io/count.mjpeg#header=Authorization: Bearer XXX"
|
||||||
|
```
|
||||||
|
|
||||||
|
**PS.** Dahua camera has a bug: if you select MJPEG codec for RTSP second stream, snapshot won't work.
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/hls"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/image"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/magic"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/mpjpeg"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/pcm"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/tcp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
streams.HandleFunc("http", handleHTTP)
|
||||||
|
streams.HandleFunc("https", handleHTTP)
|
||||||
|
streams.HandleFunc("httpx", handleHTTP)
|
||||||
|
|
||||||
|
streams.HandleFunc("tcp", handleTCP)
|
||||||
|
|
||||||
|
api.HandleFunc("api/stream", apiStream)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleHTTP(rawURL string) (core.Producer, error) {
|
||||||
|
rawURL, rawQuery, _ := strings.Cut(rawURL, "#")
|
||||||
|
|
||||||
|
// first we get the Content-Type to define supported producer
|
||||||
|
req, err := http.NewRequest("GET", rawURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if rawQuery != "" {
|
||||||
|
query := streams.ParseQuery(rawQuery)
|
||||||
|
|
||||||
|
for _, header := range query["header"] {
|
||||||
|
key, value, _ := strings.Cut(header, ":")
|
||||||
|
req.Header.Add(key, strings.TrimSpace(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
prod, err := do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if info, ok := prod.(core.Info); ok {
|
||||||
|
info.SetProtocol("http")
|
||||||
|
info.SetRemoteAddr(req.URL.Host) // TODO: rewrite to net.Conn
|
||||||
|
info.SetURL(rawURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
return prod, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func do(req *http.Request) (core.Producer, error) {
|
||||||
|
res, err := tcp.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
return nil, errors.New(res.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Guess format from content type
|
||||||
|
ct := res.Header.Get("Content-Type")
|
||||||
|
if i := strings.IndexByte(ct, ';'); i > 0 {
|
||||||
|
ct = ct[:i]
|
||||||
|
}
|
||||||
|
|
||||||
|
var ext string
|
||||||
|
if i := strings.LastIndexByte(req.URL.Path, '.'); i > 0 {
|
||||||
|
ext = req.URL.Path[i+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case ct == "application/vnd.apple.mpegurl" || ext == "m3u8":
|
||||||
|
return hls.OpenURL(req.URL, res.Body)
|
||||||
|
case ct == "image/jpeg":
|
||||||
|
return image.Open(res)
|
||||||
|
case ct == "multipart/x-mixed-replace":
|
||||||
|
return mpjpeg.Open(res.Body)
|
||||||
|
//https://www.iana.org/assignments/media-types/audio/basic
|
||||||
|
case ct == "audio/basic":
|
||||||
|
return pcm.Open(res.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
return magic.Open(res.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleTCP(rawURL string) (core.Producer, error) {
|
||||||
|
u, err := url.Parse(rawURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := net.DialTimeout("tcp", u.Host, core.ConnDialTimeout)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return magic.Open(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiStream(w http.ResponseWriter, r *http.Request) {
|
||||||
|
dst := r.URL.Query().Get("dst")
|
||||||
|
stream := streams.Get(dst)
|
||||||
|
if stream == nil {
|
||||||
|
http.Error(w, api.StreamNotFound, http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := magic.Open(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.AddProducer(client)
|
||||||
|
defer stream.RemoveProducer(client)
|
||||||
|
|
||||||
|
if err = client.Start(); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
# Hikvision ISAPI
|
||||||
|
|
||||||
|
[`new in v1.3.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.3.0)
|
||||||
|
|
||||||
|
This source type supports only backchannel audio for the [Hikvision ISAPI](https://tpp.hikvision.com/download/ISAPI_OTAP) protocol. So it should be used as a second source in addition to the RTSP protocol.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
hikvision1:
|
||||||
|
- rtsp://admin:password@192.168.1.123:554/Streaming/Channels/101
|
||||||
|
- isapi://admin:password@192.168.1.123:80/
|
||||||
|
```
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package isapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/isapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
streams.HandleFunc("isapi", func(source string) (core.Producer, error) {
|
||||||
|
return isapi.Dial(source)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# Ivideon
|
||||||
|
|
||||||
|
Support public cameras from the service [Ivideon](https://tv.ivideon.com/).
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
quailcam: ivideon:100-tu5dkUPct39cTp9oNEN2B6/0
|
||||||
|
```
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package ivideon
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/ivideon"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
streams.HandleFunc("ivideon", ivideon.Dial)
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
# TP-Link Kasa
|
||||||
|
|
||||||
|
[`new in v1.7.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.7.0)
|
||||||
|
|
||||||
|
[TP-Link Kasa](https://www.kasasmart.com/) non-standard protocol [more info](https://medium.com/@hu3vjeen/reverse-engineering-tp-link-kc100-bac4641bf1cd).
|
||||||
|
|
||||||
|
- `username` - urlsafe email, `alex@gmail.com` -> `alex%40gmail.com`
|
||||||
|
- `password` - base64password, `secret1` -> `c2VjcmV0MQ==`
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
kc401: kasa://username:password@192.168.1.123:19443/https/stream/mixed
|
||||||
|
```
|
||||||
|
|
||||||
|
Tested: KD110, KC200, KC401, KC420WS, EC71.
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package kasa
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/kasa"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
streams.HandleFunc("kasa", func(source string) (core.Producer, error) {
|
||||||
|
return kasa.Dial(source)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
# Motion JPEG
|
||||||
|
|
||||||
|
- This module can provide and receive streams in MJPEG format.
|
||||||
|
- This module is also responsible for receiving snapshots in JPEG format.
|
||||||
|
- This module also supports streaming to the server console (terminal) in the **animated ASCII art** format.
|
||||||
|
|
||||||
|
## MJPEG Client
|
||||||
|
|
||||||
|
**Important.** For a stream in MJPEG format, your source MUST contain the MJPEG codec. If your stream has the MJPEG codec, you can receive an **MJPEG stream** or **JPEG snapshots** via the API.
|
||||||
|
|
||||||
|
You can receive an MJPEG stream in several ways:
|
||||||
|
|
||||||
|
- some cameras support MJPEG codec inside [RTSP stream](../rtsp/README.md) (ex. second stream for Dahua cameras)
|
||||||
|
- some cameras have an HTTP link with [MJPEG stream](../http/README.md)
|
||||||
|
- some cameras have an HTTP link with snapshots - go2rtc can convert them to [MJPEG stream](../http/README.md)
|
||||||
|
- you can convert an H264/H265 stream from your camera via [FFmpeg integration](../ffmpeg/README.md)
|
||||||
|
|
||||||
|
With this example, your stream will have both H264 and MJPEG codecs:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
camera1:
|
||||||
|
- rtsp://rtsp:12345678@192.168.1.123/av_stream/ch0
|
||||||
|
- ffmpeg:camera1#video=mjpeg
|
||||||
|
```
|
||||||
|
|
||||||
|
## MJPEG Server
|
||||||
|
|
||||||
|
### mpjpeg
|
||||||
|
|
||||||
|
Output a stream in [MJPEG](https://en.wikipedia.org/wiki/Motion_JPEG) format. In [FFmpeg](https://ffmpeg.org/), this format is called `mpjpeg` because it contains HTTP headers.
|
||||||
|
|
||||||
|
```
|
||||||
|
ffplay http://192.168.1.123:1984/api/stream.mjpeg?src=camera1
|
||||||
|
```
|
||||||
|
|
||||||
|
### jpeg
|
||||||
|
|
||||||
|
Receiving a JPEG snapshot.
|
||||||
|
|
||||||
|
```
|
||||||
|
curl http://192.168.1.123:1984/api/frame.jpeg?src=camera1
|
||||||
|
```
|
||||||
|
|
||||||
|
- You can use `width`/`w` and/or `height`/`h` parameters.
|
||||||
|
- You can use `rotate` param with `90`, `180`, `270` or `-90` values.
|
||||||
|
- You can use `hardware`/`hw` param [read more](https://github.com/AlexxIT/go2rtc/wiki/Hardware-acceleration).
|
||||||
|
- You can use `cache` param (`1m`, `10s`, etc.) to get a cached snapshot.
|
||||||
|
- The snapshot is cached only when requested with the `cache` parameter.
|
||||||
|
- A cached snapshot will be used if its time is not older than the time specified in the `cache` parameter.
|
||||||
|
- The `cache` parameter does not check the image dimensions from the cache and those specified in the query.
|
||||||
|
|
||||||
|
### ascii
|
||||||
|
|
||||||
|
Stream as ASCII to Terminal. This format is just for fun. You can boast to your friends that you can stream cameras even to the server console without a GUI.
|
||||||
|
|
||||||
|
[](https://www.youtube.com/watch?v=sHj_3h_sX7M)
|
||||||
|
|
||||||
|
> The demo video features a combination of several settings for this format with added audio. Of course, the format doesn't support audio out of the box.
|
||||||
|
|
||||||
|
**Tips**
|
||||||
|
|
||||||
|
- this feature works only with MJPEG codec (use transcoding)
|
||||||
|
- choose a low frame rate (FPS)
|
||||||
|
- choose the width and height to fit in your terminal
|
||||||
|
- different terminals support different numbers of colors (8, 256, rgb)
|
||||||
|
- URL-encode the `text` parameter
|
||||||
|
- you can stream any camera or file from disk
|
||||||
|
|
||||||
|
**go2rtc.yaml** - transcoding to MJPEG, terminal size - 210x59 (16/9), fps - 10
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
streams:
|
||||||
|
gamazda: ffmpeg:gamazda.mp4#video=mjpeg#hardware#width=210#height=59#raw=-r 10
|
||||||
|
```
|
||||||
|
|
||||||
|
**API params**
|
||||||
|
|
||||||
|
- `color` - foreground color, values: empty, `8`, `256`, `rgb`, [SGR](https://en.wikipedia.org/wiki/ANSI_escape_code)
|
||||||
|
- example: `30` (black), `37` (white), `38;5;226` (yellow)
|
||||||
|
- `back` - background color, values: empty, `8`, `256`, `rgb`, [SGR](https://en.wikipedia.org/wiki/ANSI_escape_code)
|
||||||
|
- example: `40` (black), `47` (white), `48;5;226` (yellow)
|
||||||
|
- `text` - character set, values: empty, one character, `block`, list of chars (in order of brightness)
|
||||||
|
- example: `%20` (space), `block` (keyword for block elements), `ox` (two chars)
|
||||||
|
|
||||||
|
**Examples**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
% curl "http://192.168.1.123:1984/api/stream.ascii?src=gamazda"
|
||||||
|
% curl "http://192.168.1.123:1984/api/stream.ascii?src=gamazda&color=256"
|
||||||
|
% curl "http://192.168.1.123:1984/api/stream.ascii?src=gamazda&back=256&text=%20"
|
||||||
|
% curl "http://192.168.1.123:1984/api/stream.ascii?src=gamazda&back=8&text=%20%20"
|
||||||
|
% curl "http://192.168.1.123:1984/api/stream.ascii?src=gamazda&text=helloworld"
|
||||||
|
```
|
||||||
|
|
||||||
|
### yuv4mpegpipe
|
||||||
|
|
||||||
|
Raw [YUV](https://en.wikipedia.org/wiki/Y%E2%80%B2UV) frame stream with [YUV4MPEG](https://manned.org/yuv4mpeg) header.
|
||||||
|
|
||||||
|
```
|
||||||
|
ffplay http://192.168.1.123:1984/api/stream.y4m?src=camera1
|
||||||
|
```
|
||||||
|
|
||||||
|
## Streaming ingest
|
||||||
|
|
||||||
|
```shell
|
||||||
|
ffmpeg -re -i BigBuckBunny.mp4 -c mjpeg -f mpjpeg http://localhost:1984/api/stream.mjpeg?dst=camera1
|
||||||
|
```
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user