Compare commits
80
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8623cdcd0 | ||
|
|
9b633e5966 | ||
|
|
7a9fa12c3b | ||
|
|
ae26ad9b51 | ||
|
|
1afb8c4a62 | ||
|
|
f78be4dd18 | ||
|
|
a82c2e28bc | ||
|
|
69a2d5c41f | ||
|
|
efaea4ff91 | ||
|
|
f15fdaefbc | ||
|
|
5fc9bc200a | ||
|
|
7158943fc8 | ||
|
|
8f19d12b8c | ||
|
|
b0e009ba4d | ||
|
|
99146a8dce | ||
|
|
3349486750 | ||
|
|
204c92cb4e | ||
|
|
d16cd4398c | ||
|
|
56d343e69f | ||
|
|
f8bb04fb8b | ||
|
|
813eb25343 | ||
|
|
f86cefc2fd | ||
|
|
a04077e2c1 | ||
|
|
1c797dffb5 | ||
|
|
9ace435f5f | ||
|
|
684e1a9015 | ||
|
|
23bf14119f | ||
|
|
77eeea224c | ||
|
|
b37b3ee4b7 | ||
|
|
3f7a231ff6 | ||
|
|
108adb13a5 | ||
|
|
b561b643c5 | ||
|
|
afd71a7cd2 | ||
|
|
6572577859 | ||
|
|
cc8a6e7e18 | ||
|
|
2e24b1e476 | ||
|
|
82524a12b2 | ||
|
|
ae6e0fd928 | ||
|
|
ab947b895c | ||
|
|
e53b0f09d2 | ||
|
|
0f53185d01 | ||
|
|
1b6011b4eb | ||
|
|
bae36ef40a | ||
|
|
32937c1f61 | ||
|
|
308a454c74 | ||
|
|
daf20e192a | ||
|
|
8f043a3ce2 | ||
|
|
0d5fbd30f1 | ||
|
|
a6ebdfe713 | ||
|
|
186d580462 | ||
|
|
e6321f36d7 | ||
|
|
366c2a4b69 | ||
|
|
e8aca0ee10 | ||
|
|
96e994512e | ||
|
|
a8b93a4ef2 | ||
|
|
9d68338774 | ||
|
|
bd2adf8924 | ||
|
|
7e407c4456 | ||
|
|
d96f637c84 | ||
|
|
67508c1f17 | ||
|
|
e85e8f4780 | ||
|
|
175c33b131 | ||
|
|
556e051edc | ||
|
|
3bfd273528 | ||
|
|
bfa76009b9 | ||
|
|
c59b44c83e | ||
|
|
88a2ec7a11 | ||
|
|
3b0a510ccf | ||
|
|
2d49df07db | ||
|
|
4d441d89fd | ||
|
|
31fb8f31db | ||
|
|
4ef6fb59f0 | ||
|
|
4b801884ef | ||
|
|
26fa7ee161 | ||
|
|
0eddab0b2e | ||
|
|
e7268f2481 | ||
|
|
f31d948671 | ||
|
|
7a920fb09a | ||
|
|
2deac96af4 | ||
|
|
1e620d15df |
No files matched your search
+46
-22
@@ -4,6 +4,12 @@ on:
|
||||
push:
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
tag:
|
||||
description: 'Tag to force the cross build on'
|
||||
required: false
|
||||
type: string
|
||||
default: ''
|
||||
|
||||
jobs:
|
||||
pre_job:
|
||||
@@ -28,7 +34,7 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-22.04, windows-2022, macos-12]
|
||||
os: [ubuntu-22.04, windows-2022, macos-12, macos-14] # macos-14+ is arm64
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
name: Checkout onto ${{ runner.os }}
|
||||
@@ -71,11 +77,15 @@ jobs:
|
||||
$env:OPENSSL_DIR='D:\\Program Files\OpenSSL\'
|
||||
}
|
||||
|
||||
# Set up pkgconfig for gstreamer
|
||||
$env:PKG_CONFIG_PATH += ';' + $env:GSTREAMER_1_0_ROOT_MSVC_X86_64 + '\lib\pkgconfig'
|
||||
|
||||
# Set github vars
|
||||
Add-Content -Path $env:GITHUB_ENV -Value "GSTREAMER_1_0_ROOT_MSVC_X86_64=$env:GSTREAMER_1_0_ROOT_MSVC_X86_64"
|
||||
Add-Content -Path $env:GITHUB_PATH -Value "$env:GSTREAMER_1_0_ROOT_MSVC_X86_64\bin"
|
||||
Add-Content -Path $env:GITHUB_PATH -Value "%GSTREAMER_1_0_ROOT_MSVC_X86_64%\bin"
|
||||
Add-Content -Path $env:GITHUB_ENV -Value "OPENSSL_DIR=$env:OPENSSL_DIR"
|
||||
Add-Content -Path $env:GITHUB_ENV -Value "PKG_CONFIG_PATH=$env:PKG_CONFIG_PATH"
|
||||
|
||||
# One last check on directories
|
||||
dir "$env:GSTREAMER_1_0_ROOT_MSVC_X86_64"
|
||||
@@ -116,17 +126,18 @@ jobs:
|
||||
echo "GSTREAMER_1_0_ROOT_MSVC_X86_64=${GSTREAMER_1_0_ROOT_MSVC_X86_64}"
|
||||
echo "PKG_CONFIG_PATH=${PKG_CONFIG_PATH}"
|
||||
# pkg-config --variable pc_path pkg-config
|
||||
cargo build --all-features
|
||||
export JEMALLOC_SYS_WITH_LG_PAGE=16
|
||||
cargo build --release --all-features
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: debug-${{ matrix.os }}
|
||||
path: "target/debug/neolink*"
|
||||
name: release-${{ matrix.os }}
|
||||
path: "target/release/neolink*"
|
||||
cross:
|
||||
needs: pre_job
|
||||
if: needs.pre_job.outputs.should_skip != 'true'
|
||||
name: cross
|
||||
runs-on: ubuntu-latest
|
||||
container: "node:current-bookworm-slim"
|
||||
container: "node:current-bullseye-slim"
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -153,6 +164,8 @@ jobs:
|
||||
pkgconfig: i386-linux-gnu
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ github.event.inputs.tag }}
|
||||
- name: Install basic tools
|
||||
run: |
|
||||
apt-get update
|
||||
@@ -177,21 +190,23 @@ jobs:
|
||||
echo "${HOME}/.cargo/bin" >> "${GITHUB_PATH}"
|
||||
- name: Install ${{ matrix.arch }} Rust toolchain
|
||||
run: |
|
||||
rustup default 1.79.0
|
||||
rustup target add ${TARGET}
|
||||
env:
|
||||
TARGET: ${{ matrix.target }}
|
||||
- name: Build
|
||||
run: |
|
||||
cargo build --verbose --all-features --target=${TARGET}
|
||||
cargo build --release --verbose --all-features --target=${TARGET}
|
||||
env:
|
||||
# Retarget pkg-config as described in https://www.freedesktop.org/wiki/Software/pkg-config/CrossCompileProposal/
|
||||
PKG_CONFIG_ALLOW_CROSS: 1
|
||||
PKG_CONFIG_LIBDIR: /usr/lib/${{ matrix.pkgconfig }}/pkgconfig
|
||||
TARGET: ${{ matrix.target }}
|
||||
JEMALLOC_SYS_WITH_LG_PAGE: 16
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: debug-${{ matrix.arch }}-bookworm
|
||||
path: "target/${{ matrix.target }}/debug/neolink*"
|
||||
name: release-${{ matrix.arch }}-bookworm
|
||||
path: "target/${{ matrix.target }}/release/neolink*"
|
||||
|
||||
push_to_registry:
|
||||
name: Build Docker image
|
||||
@@ -211,6 +226,8 @@ jobs:
|
||||
SECRET: "${{ secrets.DOCKER_TOKEN }}"
|
||||
- name: Check out the repo
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ github.event.inputs.tag }}
|
||||
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
|
||||
- name: Convert username to lower case for docker
|
||||
id: string_user
|
||||
@@ -299,22 +316,22 @@ jobs:
|
||||
- name: Download Linux x86_64
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: debug-amd64-bookworm
|
||||
name: release-amd64-bookworm
|
||||
path: linux/amd64
|
||||
- name: Download Linux armv7
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: debug-armhf-bookworm
|
||||
name: release-armhf-bookworm
|
||||
path: linux/arm/v7
|
||||
- name: Download Linux arm64
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: debug-arm64-bookworm
|
||||
name: release-arm64-bookworm
|
||||
path: linux/arm64
|
||||
- name: Download Linux i386
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: debug-i386-bookworm
|
||||
name: release-i386-bookworm
|
||||
path: linux/386
|
||||
- name: Push to Docker Hub
|
||||
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
|
||||
@@ -353,43 +370,49 @@ jobs:
|
||||
- name: Download Windows
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: debug-windows-2022
|
||||
name: release-windows-2022
|
||||
path: neolink_windows
|
||||
- name: Download Macos
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: debug-macos-12
|
||||
path: neolink_macos
|
||||
name: release-macos-12
|
||||
path: neolink_macos_intel
|
||||
- name: Download Macos arm64
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: release-macos-14
|
||||
path: neolink_macos_m1
|
||||
- name: Download Linux x86_64
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: debug-ubuntu-22.04
|
||||
name: release-ubuntu-22.04
|
||||
path: neolink_linux_x86_64_ubuntu
|
||||
- name: Download Linux x86_64
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: debug-amd64-bookworm
|
||||
name: release-amd64-bookworm
|
||||
path: neolink_linux_x86_64_bookworm
|
||||
- name: Download Linux armhf
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: debug-armhf-bookworm
|
||||
name: release-armhf-bookworm
|
||||
path: neolink_linux_armhf
|
||||
- name: Download Linux arm64
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: debug-arm64-bookworm
|
||||
name: release-arm64-bookworm
|
||||
path: neolink_linux_arm64
|
||||
- name: Download Linux i386
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: debug-i386-bookworm
|
||||
name: release-i386-bookworm
|
||||
path: neolink_linux_i386
|
||||
- name: Prepare releases
|
||||
run: |
|
||||
dirs=(
|
||||
neolink_windows
|
||||
neolink_macos
|
||||
neolink_macos_intel
|
||||
neolink_macos_m1
|
||||
neolink_linux_x86_64_ubuntu
|
||||
neolink_linux_x86_64_bookworm
|
||||
neolink_linux_armhf
|
||||
@@ -406,7 +429,8 @@ jobs:
|
||||
name: Neolink ${{steps.toml.outputs.version}}
|
||||
files: |
|
||||
neolink_windows.zip
|
||||
neolink_macos.zip
|
||||
neolink_macos_intel.zip
|
||||
neolink_macos_m1.zip
|
||||
neolink_linux_x86_64_ubuntu.zip
|
||||
neolink_linux_x86_64_bookworm.zip
|
||||
neolink_linux_armhf.zip
|
||||
|
||||
@@ -112,7 +112,7 @@ jobs:
|
||||
# build a list of images from tags
|
||||
for j in ${IMAGE_TAGS}
|
||||
do
|
||||
if [[ "${j}" =~ ^[0-9][.][0-9][.][0-9]$ ]]; then
|
||||
if [[ "${j}" =~ ^[v]?[0-9][.][0-9][.][0-9]$ ]]; then
|
||||
echo "Version tag (${j}) will not be deleted"
|
||||
continue
|
||||
fi
|
||||
|
||||
@@ -34,7 +34,11 @@ jobs:
|
||||
rustup toolchain install nightly --component clippy
|
||||
rustup override set nightly
|
||||
- name: Run clippy manually
|
||||
run: cargo +nightly clippy --workspace --all-targets --all-features
|
||||
run: |
|
||||
cargo +nightly clippy --workspace --all-targets --all-features || exit 1
|
||||
cargo +nightly clippy --workspace --all-targets --no-default-features || exit 1
|
||||
cargo +nightly clippy --workspace --all-targets --no-default-features --features=gstreamer || exit 1
|
||||
cargo +nightly clippy --workspace --all-targets --no-default-features --features=pushnoti || exit 1
|
||||
|
||||
check_fmt:
|
||||
needs: pre_job
|
||||
|
||||
@@ -25,3 +25,6 @@
|
||||
|
||||
# The token file for the FCM tests
|
||||
token.toml
|
||||
|
||||
# Debug FCM build
|
||||
./crates/fcm-push-listener/
|
||||
Generated
+560
-170
File diff suppressed because it is too large.
Load diff
+30
-15
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "neolink"
|
||||
description = "A standards-compliant bridge to Reolink IP cameras"
|
||||
version = "0.6.3-rc.2"
|
||||
version = "0.6.3-rc.3"
|
||||
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
|
||||
edition = "2018"
|
||||
license = "AGPL-3.0-or-later"
|
||||
@@ -13,26 +13,25 @@ members = [
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.70"
|
||||
async-stream = "0.3.5"
|
||||
base64 = "0.22.0"
|
||||
byte-slice-cast = "1.2.2"
|
||||
byte-slice-cast = {version = "1.2.2", optional = true}
|
||||
bytes = "1.6.0"
|
||||
clap = { version = "4.2.2", features = ["derive", "cargo"] }
|
||||
crossbeam-channel = "0.5.8"
|
||||
dirs = "5.0.1"
|
||||
crossbeam-channel = {version = "0.5.8", optional = true}
|
||||
dirs = {version = "5.0.1", optional = true}
|
||||
env_logger = "0.11.3"
|
||||
fcm-push-listener = "2.0.3"
|
||||
fcm-push-listener = {version = "2.0.3", optional = true}
|
||||
futures = "0.3.28"
|
||||
gstreamer = "0.22.3"
|
||||
gstreamer-app = { version = "0.22.0", features = ["v1_20"] }
|
||||
gstreamer-rtsp = { version = "0.22.0", features = ["v1_20"] }
|
||||
gstreamer-rtsp-server = { version = "0.22.0", features = ["v1_20"] }
|
||||
gstreamer = {version = "0.23.0", optional = true}
|
||||
gstreamer-app = { version = "0.23.0", features = ["v1_20"], optional = true }
|
||||
gstreamer-rtsp = { version = "0.23.0", features = ["v1_20"], optional = true }
|
||||
gstreamer-rtsp-server = { version = "0.23.0", features = ["v1_20"], optional = true }
|
||||
heck = "0.5.0"
|
||||
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
|
||||
md5 = "0.7.0"
|
||||
neolink_core = { path = "crates/core", version = "0.6.3-rc.2" }
|
||||
md5 = {version = "0.7.0", optional = true}
|
||||
neolink_core = { path = "crates/core", version = "0.6.3-rc.3" }
|
||||
once_cell = "1.19.0"
|
||||
quick-xml = { version = "0.31.0", features = ["serialize"] }
|
||||
quick-xml = { version = "0.36.1", features = ["serialize"] }
|
||||
regex = "1.7.3"
|
||||
rumqttc = "0.24.0"
|
||||
serde = { version = "1.0.160", features = ["derive"] }
|
||||
@@ -41,8 +40,24 @@ tokio = { version = "1.27.0", features = ["rt-multi-thread", "macros", "io-util"
|
||||
tokio-stream = "0.1.12"
|
||||
tokio-util = { version = "0.7.7", features = ["full", "tracing"] }
|
||||
toml = "0.8.2"
|
||||
validator = "0.17.0"
|
||||
validator_derive = "0.17.0"
|
||||
uuid = { version = "1.8.0", features = ["v4"] }
|
||||
validator = {version="0.18.1", features = ["derive"] }
|
||||
|
||||
[target.'cfg(not(target_env = "msvc"))'.dependencies]
|
||||
tikv-jemallocator = "0.5"
|
||||
|
||||
[features]
|
||||
default = ["gstreamer", "pushnoti"]
|
||||
gstreamer = [
|
||||
"dep:gstreamer",
|
||||
"dep:gstreamer-app",
|
||||
"dep:gstreamer-rtsp",
|
||||
"dep:gstreamer-rtsp-server",
|
||||
"dep:byte-slice-cast",
|
||||
"dep:crossbeam-channel"
|
||||
]
|
||||
pushnoti = [
|
||||
"dep:fcm-push-listener",
|
||||
"dep:dirs",
|
||||
"dep:md5"
|
||||
]
|
||||
+6
-6
@@ -30,6 +30,7 @@ RUN echo "TARGETPLATFORM: ${TARGETPLATFORM}"; \
|
||||
else \
|
||||
echo "Building from scratch"; \
|
||||
apt-get update && \
|
||||
apt-get upgrade -y && \
|
||||
apt-get install -y --no-install-recommends \
|
||||
build-essential \
|
||||
openssl \
|
||||
@@ -58,6 +59,7 @@ LABEL maintainer="$OWNER"
|
||||
|
||||
# hadolint ignore=DL3008
|
||||
RUN apt-get update && \
|
||||
apt-get upgrade -y && \
|
||||
apt-get install -y --no-install-recommends \
|
||||
openssl \
|
||||
dnsutils \
|
||||
@@ -70,8 +72,7 @@ RUN apt-get update && \
|
||||
gstreamer1.0-plugins-base \
|
||||
gstreamer1.0-plugins-good \
|
||||
gstreamer1.0-plugins-bad \
|
||||
gstreamer1.0-libav \
|
||||
valgrind && \
|
||||
gstreamer1.0-libav && \
|
||||
apt-get clean -y && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=build \
|
||||
@@ -82,12 +83,11 @@ COPY docker/entrypoint.sh /entrypoint.sh
|
||||
RUN gst-inspect-1.0; \
|
||||
chmod +x "/usr/local/bin/neolink" && \
|
||||
"/usr/local/bin/neolink" --version && \
|
||||
mkdir -m 0700 /root/.config/ && \
|
||||
mkdir -m 0700 /valgrind/
|
||||
|
||||
mkdir -m 0700 /root/.config/
|
||||
|
||||
ENV NEO_LINK_MODE="rtsp" NEO_LINK_PORT=8554
|
||||
|
||||
CMD timeout 1800 valgrind --tool=massif --massif-out-file="/valgrind/massif.out.%p" /usr/local/bin/neolink ${NEO_LINK_MODE} --config /etc/neolink.toml
|
||||
CMD /usr/local/bin/neolink "${NEO_LINK_MODE}" --config /etc/neolink.toml
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
EXPOSE ${NEO_LINK_PORT}
|
||||
|
||||
@@ -36,7 +36,7 @@ features not yet in upstream master.
|
||||
- Protocol more closely follows official reolink format
|
||||
- Possibly can handle more simulatenous connections
|
||||
- More ways to connect to the camera. Including Relaying through reolink
|
||||
servers
|
||||
servers
|
||||
- Camera battery levels can be displayed in the log
|
||||
|
||||
## Installation
|
||||
@@ -67,9 +67,10 @@ sudo apt install \
|
||||
```
|
||||
|
||||
- **Windows**: You may also need to
|
||||
[install openssl](https://wiki.openssl.org/index.php/Binaries)
|
||||
[install openssl](https://wiki.openssl.org/index.php/Binaries)
|
||||
- **Macos**: You may also need to
|
||||
[install openssl](https://wiki.openssl.org/index.php/Binaries) or `brew install openssl@1.1`
|
||||
[install openssl](https://wiki.openssl.org/index.php/Binaries) or
|
||||
`brew install openssl@1.1`
|
||||
- **Ubuntu/Debian**: Install the `libssl` package
|
||||
|
||||
Make a config file see below.
|
||||
@@ -114,7 +115,7 @@ using the terminal in the same folder the neolink binary is in.
|
||||
### Discovery
|
||||
|
||||
To connect to a camera using a UID we need to find the IP address of the camera
|
||||
with that UID
|
||||
with that UID
|
||||
|
||||
The IP is discovered with four methods
|
||||
|
||||
@@ -249,7 +250,7 @@ Status Messages:
|
||||
there will be no `/status/preview` message. Only published when
|
||||
`enable_preview` is true in the config
|
||||
- `/status/floodlight_tasks` The current status of the floodlight tasks
|
||||
used updated every 2s by default
|
||||
used updated every 2s by default
|
||||
|
||||
Query Messages:
|
||||
|
||||
@@ -257,7 +258,7 @@ Query Messages:
|
||||
- `/query/pir` Request that the camera reports its pir status
|
||||
- `/query/ptz/preset` Request that the camera reports its PTZ presets
|
||||
- `/query/preview` Request that the camera post a base64 encoded jpeg
|
||||
of the stream to `/status/preview` now, ignoring the timer
|
||||
of the stream to `/status/preview` now, ignoring the timer
|
||||
|
||||
### Controlling RTSP from MQTT
|
||||
|
||||
@@ -456,8 +457,11 @@ docker pull quantumentangledandy/neolink
|
||||
# network=host, notably macos lacks this option.
|
||||
docker run --network host --volume=$PWD/config.toml:/etc/neolink.toml quantumentangledandy/neolink
|
||||
```
|
||||
#### Environmental Variables
|
||||
There are currently 2 environmental variables available as part of the container:
|
||||
|
||||
#### Environmental Variables
|
||||
|
||||
There are currently 2 environmental variables available as part of the container:
|
||||
|
||||
- `NEO_LINK_MODE`: defaults to `"rtsp"` if not set, other options are "mqtt" or "mqtt-rtsp".
|
||||
- `NEO_LINK_PORT`: defaults to `8554`, set this to your required port value.
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "neolink_core"
|
||||
description = "Core services and structure for Reolink IP cameras"
|
||||
version = "0.6.3-rc.2"
|
||||
version = "0.6.3-rc.3"
|
||||
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
|
||||
edition = "2018"
|
||||
license = "AGPL-3.0-or-later"
|
||||
@@ -20,12 +20,12 @@ lazy_static = "1.4.0"
|
||||
log = "0.4.17"
|
||||
md5 = "0.7.0"
|
||||
nom = { version = "7.1.3", features = ["alloc"] }
|
||||
quick-xml = { version = "0.31.0", features = ["serialize"] }
|
||||
quick-xml = { version = "0.36.0", features = ["serialize"] }
|
||||
rand = "0.8.5"
|
||||
regex = "1.7.3"
|
||||
serde = { version = "1.0.106", features = ["derive"] }
|
||||
thiserror = "1.0.58"
|
||||
time = { version = "0.3.20" , features = [ "macros", "parsing", "local-offset" ]}
|
||||
time = { version = "0.3.36" , features = [ "macros", "parsing", "local-offset" ]}
|
||||
tokio = { version = "1.27.0", features = ["full"] }
|
||||
tokio-stream = { version = "0.1.12", features = ["sync", "time", "net"] }
|
||||
tokio-util = { version = "0.7.7", features = ["full", "tracing"] }
|
||||
|
||||
@@ -36,6 +36,10 @@ pub const MSG_ID_MOTION: u32 = 33;
|
||||
pub const MSG_ID_SET_SERVICE_PORTS: u32 = 36;
|
||||
/// Get service ports
|
||||
pub const MSG_ID_GET_SERVICE_PORTS: u32 = 37;
|
||||
/// Get Email setting
|
||||
pub const MSG_ID_GET_EMAIL: u32 = 42;
|
||||
/// Set email settings
|
||||
pub const MSG_ID_SET_EMAIL: u32 = 43;
|
||||
/// Version messages have this ID
|
||||
pub const MSG_ID_VERSION: u32 = 80;
|
||||
/// Ping messages have this ID
|
||||
@@ -50,6 +54,8 @@ pub const MSG_ID_SNAP: u32 = 109;
|
||||
pub const MSG_ID_UID: u32 = 114;
|
||||
/// Used to pass the token and client ID for push notifications
|
||||
pub const MSG_ID_PUSH_INFO: u32 = 124;
|
||||
/// Send a test email configuration
|
||||
pub const MSG_ID_TEST_EMAIL: u32 = 141;
|
||||
/// StreamInfoList messages have this ID
|
||||
pub const MSG_ID_STREAM_INFO_LIST: u32 = 146;
|
||||
/// Used to get the abilities of a user
|
||||
@@ -70,6 +76,10 @@ pub const MSG_ID_SET_LED_STATUS: u32 = 209;
|
||||
pub const MSG_ID_GET_PIR_ALARM: u32 = 212;
|
||||
/// Setting PIR status messages have this ID
|
||||
pub const MSG_ID_START_PIR_ALARM: u32 = 213;
|
||||
/// Set Email Task
|
||||
pub const MSG_ID_SET_EMAIL_TASK: u32 = 216;
|
||||
/// Get Email Task
|
||||
pub const MSG_ID_GET_EMAIL_TASK: u32 = 217;
|
||||
/// UDP Keep alive
|
||||
pub const MSG_ID_UDP_KEEP_ALIVE: u32 = 234;
|
||||
/// Battery message initiaed by the camera
|
||||
|
||||
+199
-21
@@ -132,6 +132,12 @@ pub struct BcXml {
|
||||
/// For changing rtmp server port
|
||||
#[serde(rename = "OnvifPort", skip_serializing_if = "Option::is_none")]
|
||||
pub onvif_port: Option<OnvifPort>,
|
||||
/// Email for setting the email notifications
|
||||
#[serde(rename = "Email", skip_serializing_if = "Option::is_none")]
|
||||
pub email: Option<Email>,
|
||||
/// EmailTask for turning the email notifications on/off
|
||||
#[serde(rename = "EmailTask", skip_serializing_if = "Option::is_none")]
|
||||
pub email_task: Option<EmailTask>,
|
||||
}
|
||||
|
||||
impl BcXml {
|
||||
@@ -228,7 +234,8 @@ pub struct DeviceInfo {
|
||||
#[serde(rename = "@version")]
|
||||
pub version: Option<String>,
|
||||
/// The resolution xml block
|
||||
pub resolution: Resolution,
|
||||
/// Does not exist for floodlights
|
||||
pub resolution: Option<Resolution>,
|
||||
}
|
||||
|
||||
/// VersionInfo xml
|
||||
@@ -318,7 +325,7 @@ pub struct Extension {
|
||||
pub check_pos: Option<u32>,
|
||||
/// Encrypted binary has this to verify successful decryption
|
||||
#[serde(rename = "checkValue", skip_serializing_if = "Option::is_none")]
|
||||
pub check_value: Option<u32>,
|
||||
pub check_value: Option<i32>,
|
||||
/// Used in newer encrypted payload packets
|
||||
#[serde(rename = "encryptLen", skip_serializing_if = "Option::is_none")]
|
||||
pub encrypt_len: Option<u32>,
|
||||
@@ -430,7 +437,7 @@ pub struct FloodlightStatusList {
|
||||
#[serde(rename = "@version")]
|
||||
pub version: String,
|
||||
/// List of events
|
||||
#[serde(rename = "FloodlightStatus")]
|
||||
#[serde(default, rename = "FloodlightStatus")]
|
||||
pub floodlight_status_list: Vec<FloodlightStatus>,
|
||||
}
|
||||
|
||||
@@ -479,7 +486,7 @@ pub struct RfAlarmCfg {
|
||||
#[serde(rename = "timeBlockList")]
|
||||
pub struct TimeBlockList {
|
||||
/// List of time block entries which disable/enable the PIR at a time
|
||||
#[serde(rename = "timeBlock")]
|
||||
#[serde(default, rename = "timeBlock")]
|
||||
pub time_block: Vec<TimeBlock>,
|
||||
}
|
||||
|
||||
@@ -489,8 +496,9 @@ pub struct TimeBlockList {
|
||||
pub struct TimeBlock {
|
||||
/// Whether to enable or disable for this time block
|
||||
pub enable: u8,
|
||||
/// The day of the week for this block
|
||||
pub weekDay: String,
|
||||
/// The day of the week for this block, Monday, Tuesday, Etc
|
||||
#[serde(rename = "weekDay")]
|
||||
pub week_day: String,
|
||||
/// Time to start this block
|
||||
#[serde(rename = "beginHour")]
|
||||
pub begin_hour: u8,
|
||||
@@ -503,6 +511,7 @@ pub struct TimeBlock {
|
||||
/// AlarmHandle Xml
|
||||
pub struct AlarmHandle {
|
||||
/// Items in the alarm handle
|
||||
#[serde(default)]
|
||||
pub item: Vec<AlarmHandleItem>,
|
||||
}
|
||||
|
||||
@@ -571,13 +580,13 @@ pub struct TalkAbility {
|
||||
#[serde(rename = "@version")]
|
||||
pub version: String,
|
||||
/// Duplexes known values `"FDX"`
|
||||
#[serde(rename = "duplexList")]
|
||||
#[serde(default, rename = "duplexList")]
|
||||
pub duplex_list: Vec<DuplexList>,
|
||||
/// audioStreamModes known values `"followVideoStream"`
|
||||
#[serde(rename = "audioStreamModeList")]
|
||||
#[serde(default, rename = "audioStreamModeList")]
|
||||
pub audio_stream_mode_list: Vec<AudioStreamModeList>,
|
||||
/// AudioConfigs contans the details of the audio to follow
|
||||
#[serde(rename = "audioConfigList")]
|
||||
#[serde(default, rename = "audioConfigList")]
|
||||
pub audio_config_list: Vec<AudioConfigList>,
|
||||
}
|
||||
|
||||
@@ -611,7 +620,7 @@ pub struct AlarmEventList {
|
||||
#[serde(rename = "@version")]
|
||||
pub version: String,
|
||||
/// List of events
|
||||
#[serde(rename = "AlarmEvent")]
|
||||
#[serde(default, rename = "AlarmEvent")]
|
||||
pub alarm_events: Vec<AlarmEvent>,
|
||||
}
|
||||
|
||||
@@ -670,6 +679,7 @@ pub struct PtzPreset {
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
|
||||
pub struct PresetList {
|
||||
/// List of Presets
|
||||
#[serde(default)]
|
||||
pub preset: Vec<Preset>,
|
||||
}
|
||||
|
||||
@@ -693,7 +703,7 @@ pub struct BatteryList {
|
||||
#[serde(rename = "@version")]
|
||||
pub version: String,
|
||||
/// Battery info items
|
||||
#[serde(rename = "BatteryInfo")]
|
||||
#[serde(default, rename = "BatteryInfo")]
|
||||
pub battery_info: Vec<BatteryInfo>,
|
||||
}
|
||||
|
||||
@@ -768,7 +778,7 @@ pub struct AbilityInfo {
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
|
||||
pub struct AbilityInfoToken {
|
||||
/// Submodule for this ability info token
|
||||
#[serde(rename = "subModule")]
|
||||
#[serde(default, rename = "subModule")]
|
||||
pub sub_module: Vec<AbilityInfoSubModule>,
|
||||
}
|
||||
|
||||
@@ -842,7 +852,7 @@ pub struct Snap {
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
|
||||
pub struct StreamInfoList {
|
||||
/// The stream infos. There is usually only one of these
|
||||
#[serde(rename = "StreamInfo")]
|
||||
#[serde(default, rename = "StreamInfo")]
|
||||
pub stream_infos: Vec<StreamInfo>,
|
||||
}
|
||||
|
||||
@@ -853,7 +863,7 @@ pub struct StreamInfo {
|
||||
#[serde(rename = "channelBits")]
|
||||
pub channel_bits: u32,
|
||||
/// List of encode tabeles. These hold the actual stream data
|
||||
#[serde(rename = "encodeTable")]
|
||||
#[serde(default, rename = "encodeTable")]
|
||||
pub encode_tables: Vec<EncodeTable>,
|
||||
}
|
||||
|
||||
@@ -927,7 +937,7 @@ pub struct FloodlightTask {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub brightness_min: Option<u32>,
|
||||
/// Schedule fot auto floodlight
|
||||
pub schedule: Schedule,
|
||||
pub schedule: ScheduleFloodLight,
|
||||
/// Threshold settings for light sensor to consider nightime
|
||||
#[serde(rename = "lightSensThreshold")]
|
||||
pub light_sens_threshold: LightSensThreshold,
|
||||
@@ -944,7 +954,7 @@ pub struct FloodlightTask {
|
||||
|
||||
/// Schedule for Floodlight Task
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
|
||||
pub struct Schedule {
|
||||
pub struct ScheduleFloodLight {
|
||||
/// startHour
|
||||
#[serde(rename = "startHour")]
|
||||
pub start_hour: u32,
|
||||
@@ -1250,7 +1260,7 @@ pub struct Support {
|
||||
#[serde(rename = "smartHome", skip_serializing_if = "Option::is_none")]
|
||||
pub smart_home: Option<SmartHome>,
|
||||
/// Support config for specific channels
|
||||
#[serde(rename = "item")]
|
||||
#[serde(default, rename = "item")]
|
||||
pub items: Vec<SupportItem>,
|
||||
}
|
||||
|
||||
@@ -1260,7 +1270,7 @@ pub struct SmartHome {
|
||||
/// Versionm
|
||||
pub version: u32,
|
||||
/// The smarthome items
|
||||
#[serde(rename = "item")]
|
||||
#[serde(default, rename = "item")]
|
||||
pub items: Vec<SmartHomeItem>,
|
||||
}
|
||||
|
||||
@@ -1475,6 +1485,87 @@ pub struct OnvifPort {
|
||||
pub enable: Option<u32>,
|
||||
}
|
||||
|
||||
/// Email settings for notificaitons
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct Email {
|
||||
/// XML Version
|
||||
#[serde(rename = "@version")]
|
||||
pub version: String,
|
||||
/// SMTP server address
|
||||
#[serde(rename = "smtpServer")]
|
||||
pub smtp_server: String,
|
||||
/// SMTP username
|
||||
#[serde(rename = "userName")]
|
||||
pub user_name: String,
|
||||
/// SMTP password
|
||||
pub password: String,
|
||||
/// Send email address
|
||||
pub address1: String,
|
||||
/// Send email address can be empty
|
||||
pub address2: String,
|
||||
/// Send email address can be empty
|
||||
pub address3: String,
|
||||
/// 465
|
||||
#[serde(rename = "smtpPort")]
|
||||
pub smtp_port: u16,
|
||||
/// Name of recipient to use on the email
|
||||
#[serde(rename = "sendNickname")]
|
||||
pub send_nickname: String,
|
||||
/// Observed value `1`
|
||||
pub attachment: u8,
|
||||
/// Observed value `picture`, `video`
|
||||
#[serde(rename = "attachmentType", skip_serializing_if = "Option::is_none")]
|
||||
pub attachment_type: Option<String>,
|
||||
/// Observed value `withText`
|
||||
#[serde(rename = "textType")]
|
||||
pub text_type: String,
|
||||
/// Observed value `1`
|
||||
pub ssl: u8,
|
||||
/// Observed value `30`
|
||||
pub interval: u32,
|
||||
/// Max length of message. Observed value `127`
|
||||
/// Read Only
|
||||
#[serde(rename = "senderMaxLen", skip_serializing_if = "Option::is_none")]
|
||||
pub sender_max_len: Option<u32>,
|
||||
}
|
||||
|
||||
/// EmailTask settings that controls the times/enables the email
|
||||
/// notifications
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
|
||||
pub struct EmailTask {
|
||||
/// XML Version
|
||||
#[serde(rename = "@version")]
|
||||
pub version: String,
|
||||
/// Channel number
|
||||
#[serde(rename = "channelId")]
|
||||
pub channel_id: u8,
|
||||
/// 1 for enable 0 for disable
|
||||
#[serde(rename = "enable")]
|
||||
pub enable: u8,
|
||||
/// The list of schedule to turn on/off the email notifications
|
||||
#[serde(rename = "ScheduleList", skip_serializing_if = "Option::is_none")]
|
||||
pub schedule_list: Option<ScheduleList>,
|
||||
}
|
||||
|
||||
/// List of schedule items for turning on/off the notifications
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
|
||||
pub struct ScheduleList {
|
||||
/// List of schedules
|
||||
#[serde(rename = "Schedule")]
|
||||
pub schedule: Schedule,
|
||||
}
|
||||
|
||||
/// Schedule item for turning on/off the notifications
|
||||
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
|
||||
pub struct Schedule {
|
||||
/// The alarm type. Observed values: `MD`
|
||||
#[serde(rename = "alarmType")]
|
||||
pub alarm_type: String,
|
||||
/// The list of time blocks
|
||||
#[serde(rename = "timeBlockList")]
|
||||
pub time_block_list: TimeBlockList,
|
||||
}
|
||||
|
||||
/// Convience function to return the xml version used throughout the library
|
||||
pub fn xml_ver() -> String {
|
||||
"1.1".to_string()
|
||||
@@ -1696,18 +1787,17 @@ fn test_deviceinfo_partial_deser() {
|
||||
"#
|
||||
);
|
||||
|
||||
// Needs to ignore all the other crap that we don't care about
|
||||
let b = BcXml::try_parse(sample.as_bytes()).unwrap();
|
||||
match b {
|
||||
BcXml {
|
||||
device_info:
|
||||
Some(DeviceInfo {
|
||||
resolution:
|
||||
Resolution {
|
||||
Some(Resolution {
|
||||
width: 3840,
|
||||
height: 2160,
|
||||
..
|
||||
},
|
||||
}),
|
||||
..
|
||||
}),
|
||||
..
|
||||
@@ -1737,3 +1827,91 @@ fn test_binary_deser() {
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_enc3_extension() {
|
||||
let _ = env_logger::builder().is_test(true).try_init();
|
||||
let sample = indoc!(
|
||||
r#"<?xml version="1.0\" encoding="UTF-8" ?>
|
||||
<Extension version="1.1">
|
||||
<encryptLen>1024</encryptLen>
|
||||
<binaryData>1</binaryData>
|
||||
<checkPos>0</checkPos>
|
||||
<checkValue>1667510320</checkValue>
|
||||
</Extension>
|
||||
"#
|
||||
);
|
||||
let b = Extension::try_parse(sample.as_bytes()).unwrap();
|
||||
match b {
|
||||
Extension {
|
||||
encrypt_len: Some(1024),
|
||||
binary_data: Some(1),
|
||||
check_pos: Some(0),
|
||||
check_value: Some(1667510320),
|
||||
..
|
||||
} => {}
|
||||
_ => panic!(),
|
||||
}
|
||||
|
||||
let sample = indoc!(
|
||||
r#"
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<Extension version="1.1">
|
||||
<checkPos>0</checkPos>
|
||||
<checkValue>-1211658</checkValue>
|
||||
</Extension>
|
||||
"#
|
||||
);
|
||||
let b = Extension::try_parse(sample.as_bytes()).unwrap();
|
||||
match b {
|
||||
Extension {
|
||||
check_pos: Some(0),
|
||||
check_value: Some(-1211658),
|
||||
..
|
||||
} => {}
|
||||
_ => panic!(),
|
||||
}
|
||||
|
||||
let sample = indoc!(
|
||||
r#"
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<Extension version="1.1">
|
||||
<checkPos>0</checkPos>
|
||||
<checkValue>-1821213800</checkValue>
|
||||
</Extension>
|
||||
"#
|
||||
);
|
||||
let b = Extension::try_parse(sample.as_bytes()).unwrap();
|
||||
match b {
|
||||
Extension {
|
||||
check_pos: Some(0),
|
||||
check_value: Some(-1821213800),
|
||||
..
|
||||
} => {}
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_floodlight_status_list() {
|
||||
let _ = env_logger::builder().is_test(true).try_init();
|
||||
let sample = indoc!(
|
||||
r#"<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<FloodlightStatusList version="1.1" />
|
||||
</body>
|
||||
"#
|
||||
);
|
||||
let b = BcXml::try_parse(sample.as_bytes()).unwrap();
|
||||
match b {
|
||||
BcXml {
|
||||
floodlight_status_list:
|
||||
Some(FloodlightStatusList {
|
||||
version,
|
||||
floodlight_status_list,
|
||||
}),
|
||||
..
|
||||
} if version == "1.1" && floodlight_status_list.is_empty() => {}
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ mod abilityinfo;
|
||||
mod battery;
|
||||
mod connection;
|
||||
mod credentials;
|
||||
mod email;
|
||||
mod errors;
|
||||
mod floodlight;
|
||||
mod keepalive;
|
||||
|
||||
@@ -30,7 +30,10 @@ impl BcCamera {
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
|
||||
@@ -396,7 +396,7 @@ impl Poller {
|
||||
};
|
||||
}
|
||||
PollCommand::Disconnect => {
|
||||
return Err(Error::DroppedConnection);
|
||||
return Err(Error::ConnectionShutdown);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,6 +139,7 @@ impl Discoverer {
|
||||
tokio::task::yield_now().await;
|
||||
match reader.next().await {
|
||||
Some(Ok((BcUdp::Discovery(bcudp), addr))) => {
|
||||
log::trace!("Got discovery {:?} for {}", bcudp, addr);
|
||||
let tid = bcudp.tid;
|
||||
let mut needs_removal = false;
|
||||
if let (Some(sender), true) =
|
||||
@@ -158,9 +159,10 @@ impl Discoverer {
|
||||
}
|
||||
Some(Ok(bcudp)) => {
|
||||
// Only discovery packets should be possible atm
|
||||
trace!("Got non Discovery during discovery: {:?}", bcudp);
|
||||
log::debug!("Got non Discovery during discovery: {:?}", bcudp);
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
log::error!("Error on discovery socket: {:?}", e);
|
||||
let mut locked_sub = thread_subscriber.write().await;
|
||||
for (_, sub) in locked_sub.iter() {
|
||||
let _ = sub.send(Err(e.clone())).await;
|
||||
@@ -298,7 +300,7 @@ impl Discoverer {
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(Error::DroppedConnection)
|
||||
Err(Error::DiscoveryIgnored)
|
||||
} => {v},
|
||||
v = async {
|
||||
// Send every inter for ever or until channel is no longer viable
|
||||
@@ -360,16 +362,35 @@ impl Discoverer {
|
||||
}),
|
||||
};
|
||||
|
||||
log::debug!(
|
||||
"Trying a direct connect to: {:?} with tid: {}",
|
||||
addr,
|
||||
msg.tid
|
||||
);
|
||||
|
||||
let (camera_address, camera_id) = self
|
||||
.retry_send(msg, addr, |bc, addr| match bc {
|
||||
UdpDiscovery {
|
||||
tid: _,
|
||||
payload: UdpXml::D2cCr(D2cCr { did, cid, .. }),
|
||||
} if cid == client_id => Some((addr, did)),
|
||||
_ => None,
|
||||
} if cid == client_id => {
|
||||
log::trace!("Got expected reply");
|
||||
Some((addr, did))
|
||||
}
|
||||
n => {
|
||||
log::debug!("Got unexpected reply: {:?}", n);
|
||||
None
|
||||
}
|
||||
})
|
||||
.await?;
|
||||
|
||||
log::debug!(
|
||||
"Direct connect success at {:?} client: {}, device: {}",
|
||||
addr,
|
||||
client_id,
|
||||
camera_id
|
||||
);
|
||||
|
||||
let result = ConnectResult {
|
||||
addr: camera_address,
|
||||
client_id,
|
||||
@@ -378,6 +399,7 @@ impl Discoverer {
|
||||
};
|
||||
self.keep_alive_device(tid, &result).await;
|
||||
|
||||
log::debug!("Returning direct connect: {:?}", result);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use super::DiscoveryResult;
|
||||
use crate::bc::codex::BcCodex;
|
||||
use crate::bc::model::*;
|
||||
use crate::bc_protocol::errors::BcUdpDropReciverKind;
|
||||
use crate::bcudp::codex::BcUdpCodex;
|
||||
use crate::bcudp::{model::*, xml::*};
|
||||
use crate::{Credentials, Error, Result};
|
||||
@@ -350,29 +351,29 @@ impl UdpPayloadInner {
|
||||
loop {
|
||||
break tokio::select!{
|
||||
_ = recv_timeout.as_mut() => {
|
||||
log::trace!("DroppedConnection: Timeout");
|
||||
Err(Error::DroppedConnection)
|
||||
Err(Error::BcUdpTimeout)
|
||||
}
|
||||
packet = inner.next() => {
|
||||
log::trace!("Cam->App");
|
||||
let packet = packet.ok_or(Error::DroppedConnection)??;
|
||||
let packet = packet.ok_or(Error::BcUdpDropReciver(BcUdpDropReciverKind::NoneRecieved))??;
|
||||
recv_timeout.as_mut().reset(Instant::now() + Duration::from_secs(TIME_OUT));
|
||||
// let packet = socket_rx.next().await.ok_or(Error::DroppedConnection)??;
|
||||
socket_out_tx.try_send(packet).map_err(|_| Error::DroppedConnection)?;
|
||||
// let packet = socket_rx.next().await.ok_or(Error::BcUdpDropReciver)??;
|
||||
socket_out_tx.try_send(packet).map_err(|e| Error::BcUdpDropReciver(BcUdpDropReciverKind::SendFailed(format!("{e:?}"))))?;
|
||||
continue;
|
||||
},
|
||||
packet = socket_in_rx.next() => {
|
||||
let packet = packet.ok_or(Error::DroppedConnection)?;
|
||||
let packet = packet.ok_or(Error::BcUdpDropSender)?;
|
||||
match tokio::time::timeout(tokio::time::Duration::from_millis(250), inner.send((packet, thread_camera_addr))).await {
|
||||
Ok(written) => {
|
||||
written?;
|
||||
}
|
||||
Err(_) => {
|
||||
log::trace!("Socket Error, attempting reconnect over a new one");
|
||||
// Socket is (maybe) broken
|
||||
// Seems to happen with network reconnects like over
|
||||
// a lossy cellular network
|
||||
let stream = Arc::new(tokio::time::timeout(tokio::time::Duration::from_millis(250), connect_try_port(inner.inner.get_ref().local_addr()?.port())).await.map_err(|_| Error::DroppedConnection)??);
|
||||
inner = tokio::time::timeout(tokio::time::Duration::from_millis(250), BcUdpSource::new_from_socket(stream, inner.addr)).await.map_err(|_| Error::DroppedConnection)??;
|
||||
let stream = Arc::new(tokio::time::timeout(tokio::time::Duration::from_millis(250), connect_try_port(inner.inner.get_ref().local_addr()?.port())).await.map_err(|_| Error::BcUdpReconnectTimeout)??);
|
||||
inner = tokio::time::timeout(tokio::time::Duration::from_millis(250), BcUdpSource::new_from_socket(stream, inner.addr)).await.map_err(|_| Error::BcUdpReconnectTimeout)??;
|
||||
|
||||
// Inform the camera that we are the same client
|
||||
//
|
||||
@@ -491,10 +492,7 @@ impl UdpPayloadInner {
|
||||
log::trace!("App->Camera");
|
||||
// Incomming from application
|
||||
// Outgoing on socket
|
||||
if v.is_none() {
|
||||
log::trace!("DroppedConnection: self.thread_sink.next(): {:?}", v);
|
||||
}
|
||||
let item = v.ok_or(Error::DroppedConnection)?;
|
||||
let item = v.ok_or(Error::BcUdpDropSender)?;
|
||||
|
||||
for chunk in item.chunks(MTU - UDPDATA_HEADER_SIZE) {
|
||||
let udp_data = UdpData {
|
||||
@@ -512,10 +510,7 @@ impl UdpPayloadInner {
|
||||
log::trace!("Camera->App");
|
||||
// Incomming from socket
|
||||
// Outgoing to application
|
||||
if v.is_none() {
|
||||
log::trace!("DroppedConnection: self.socket_out.next()");
|
||||
}
|
||||
let (item, addr) = v.ok_or(Error::DroppedConnection)?;
|
||||
let (item, addr) = v.ok_or(Error::BcUdpDropReciver(BcUdpDropReciverKind::NoneRecieved))?;
|
||||
if addr == camera_addr {
|
||||
match item {
|
||||
BcUdp::Discovery(UdpDiscovery{
|
||||
@@ -560,7 +555,7 @@ impl UdpPayloadInner {
|
||||
self.packets_want += 1;
|
||||
self.thread_stream.feed(Ok(payload)).await?;
|
||||
}
|
||||
log::info!("recieved: {}", self.recieved.len());
|
||||
log::trace!("recieved: {}", self.recieved.len());
|
||||
log::trace!("Flush");
|
||||
self.socket_in.flush().await?;
|
||||
self.thread_stream.flush().await?;
|
||||
@@ -618,7 +613,7 @@ impl UdpPayloadInner {
|
||||
}
|
||||
}
|
||||
self.ack_latency.feed_ack();
|
||||
log::info!("sent: {}", self.sent.len());
|
||||
log::trace!("sent: {}", self.sent.len());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -657,7 +652,7 @@ impl UdpPayloadSource {
|
||||
if payload_inner.thread_stream.is_closed() {
|
||||
log::trace!("payload_inner.thread_stream.is_closed");
|
||||
payload_inner.thread_sink.close();
|
||||
return Err(Error::DroppedConnection);
|
||||
return Err(Error::BcUdpPayloadDroppedInner);
|
||||
}
|
||||
log::trace!("Calling inner");
|
||||
let res = payload_inner.run().await;
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
//! Email controlling methods
|
||||
//!
|
||||
use super::{BcCamera, Error, Result};
|
||||
use crate::bc::{model::*, xml::*};
|
||||
|
||||
impl BcCamera {
|
||||
/// Get the current Email XML
|
||||
pub async fn get_email(&self) -> Result<Email> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_get = connection.subscribe(MSG_ID_GET_EMAIL, msg_num).await?;
|
||||
let get = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_GET_EMAIL,
|
||||
channel_id: self.channel_id,
|
||||
msg_num,
|
||||
response_code: 0,
|
||||
stream_type: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
body: BcBody::ModernMsg(ModernMsg {
|
||||
extension: None,
|
||||
payload: None,
|
||||
}),
|
||||
};
|
||||
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
payload:
|
||||
Some(BcPayloads::BcXml(BcXml {
|
||||
email: Some(email), ..
|
||||
})),
|
||||
..
|
||||
}) = msg.body
|
||||
{
|
||||
Ok(email)
|
||||
} else {
|
||||
Err(Error::UnintelligibleReply {
|
||||
reply: std::sync::Arc::new(Box::new(msg)),
|
||||
why: "Expected Email xml but it was not recieved",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the Email XML
|
||||
pub async fn set_email(&self, email: Email) -> Result<()> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_get = connection.subscribe(MSG_ID_SET_EMAIL, msg_num).await?;
|
||||
let get = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_SET_EMAIL,
|
||||
channel_id: self.channel_id,
|
||||
msg_num,
|
||||
response_code: 0,
|
||||
stream_type: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
body: BcBody::ModernMsg(ModernMsg {
|
||||
extension: None,
|
||||
payload: Some(BcPayloads::BcXml(BcXml {
|
||||
email: Some(email),
|
||||
..Default::default()
|
||||
})),
|
||||
}),
|
||||
};
|
||||
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Test the Email with this XML
|
||||
pub async fn test_email(&self, email: Email) -> Result<()> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_get = connection.subscribe(MSG_ID_TEST_EMAIL, msg_num).await?;
|
||||
let get = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_TEST_EMAIL,
|
||||
channel_id: self.channel_id,
|
||||
msg_num,
|
||||
response_code: 0,
|
||||
stream_type: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
body: BcBody::ModernMsg(ModernMsg {
|
||||
extension: None,
|
||||
payload: Some(BcPayloads::BcXml(BcXml {
|
||||
email: Some(email),
|
||||
..Default::default()
|
||||
})),
|
||||
}),
|
||||
};
|
||||
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the current EmailTask XML
|
||||
pub async fn get_email_task(&self) -> Result<EmailTask> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_get = connection.subscribe(MSG_ID_GET_EMAIL_TASK, msg_num).await?;
|
||||
let get = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_GET_EMAIL_TASK,
|
||||
channel_id: self.channel_id,
|
||||
msg_num,
|
||||
response_code: 0,
|
||||
stream_type: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
body: BcBody::ModernMsg(ModernMsg {
|
||||
extension: Some(Extension {
|
||||
channel_id: Some(self.channel_id),
|
||||
..Default::default()
|
||||
}),
|
||||
payload: None,
|
||||
}),
|
||||
};
|
||||
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
payload:
|
||||
Some(BcPayloads::BcXml(BcXml {
|
||||
email_task: Some(email_task),
|
||||
..
|
||||
})),
|
||||
..
|
||||
}) = msg.body
|
||||
{
|
||||
Ok(email_task)
|
||||
} else {
|
||||
Err(Error::UnintelligibleReply {
|
||||
reply: std::sync::Arc::new(Box::new(msg)),
|
||||
why: "Expected EmailTask xml but it was not recieved",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Setup the Email Task
|
||||
pub async fn set_email_task(&self, email_task: EmailTask) -> Result<()> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_get = connection.subscribe(MSG_ID_SET_EMAIL_TASK, msg_num).await?;
|
||||
let get = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_SET_EMAIL_TASK,
|
||||
channel_id: self.channel_id,
|
||||
msg_num,
|
||||
response_code: 0,
|
||||
stream_type: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
body: BcBody::ModernMsg(ModernMsg {
|
||||
extension: Some(Extension {
|
||||
channel_id: Some(self.channel_id),
|
||||
..Default::default()
|
||||
}),
|
||||
payload: Some(BcPayloads::BcXml(BcXml {
|
||||
email_task: Some(email_task),
|
||||
..Default::default()
|
||||
})),
|
||||
}),
|
||||
};
|
||||
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Turn on Email notifications
|
||||
pub async fn email_on(&self) -> Result<()> {
|
||||
self.set_email_task(EmailTask {
|
||||
version: xml_ver(),
|
||||
channel_id: self.channel_id,
|
||||
enable: 1,
|
||||
schedule_list: None,
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Turn off Email notifications
|
||||
pub async fn email_off(&self) -> Result<()> {
|
||||
self.set_email_task(EmailTask {
|
||||
version: xml_ver(),
|
||||
channel_id: self.channel_id,
|
||||
enable: 0,
|
||||
schedule_list: None,
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Turn on Email notifications all the time
|
||||
pub async fn email_on_always(&self) -> Result<()> {
|
||||
const DOW: [&str; 7] = [
|
||||
"Sunday",
|
||||
"Monday",
|
||||
"Tuesday",
|
||||
"Wednesday",
|
||||
"Thursday",
|
||||
"Friday",
|
||||
"Saturday",
|
||||
];
|
||||
self.set_email_task(EmailTask {
|
||||
version: xml_ver(),
|
||||
channel_id: self.channel_id,
|
||||
enable: 1,
|
||||
schedule_list: Some(ScheduleList {
|
||||
schedule: Schedule {
|
||||
alarm_type: "MD".to_owned(),
|
||||
time_block_list: TimeBlockList {
|
||||
time_block: DOW
|
||||
.iter()
|
||||
.map(|d| TimeBlock {
|
||||
enable: 1,
|
||||
week_day: d.to_string(),
|
||||
begin_hour: 0,
|
||||
end_hour: 23,
|
||||
})
|
||||
.collect(),
|
||||
},
|
||||
},
|
||||
}),
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -45,8 +45,16 @@ pub enum Error {
|
||||
},
|
||||
|
||||
/// Raised when the camera responds with a status code over than OK
|
||||
#[error("Camera responded with Service Unavaliable: {0}")]
|
||||
CameraServiceUnavailable(u16),
|
||||
#[error("Camera responded with Service Unavaliable: Msg of type {id} returned code {code}")]
|
||||
CameraServiceUnavailable {
|
||||
/// The message ID
|
||||
id: u32,
|
||||
/// The return code this is usually
|
||||
/// 200 for OK
|
||||
/// 400 for not yet ready
|
||||
/// 500 for camera cannot comply or understand
|
||||
code: u16,
|
||||
},
|
||||
|
||||
/// Raised when the camera responds with a status code over than OK during login
|
||||
#[error("Camera responded with Err during login")]
|
||||
@@ -64,6 +72,38 @@ pub enum Error {
|
||||
#[error("Dropped connection (Broadcast TryRecv)")]
|
||||
BroadcastDroppedConnectionTry(#[from] tokio::sync::broadcast::error::TryRecvError),
|
||||
|
||||
/// Raised when a stream thread has finished
|
||||
#[error("End of Stream")]
|
||||
StreamFinished,
|
||||
|
||||
/// Raised when a connection requests shutdown
|
||||
#[error("Connection shuting down")]
|
||||
ConnectionShutdown,
|
||||
|
||||
/// Raised when a discovery attempt fails to get a reply
|
||||
#[error("No reply to discovery packet")]
|
||||
DiscoveryIgnored,
|
||||
|
||||
/// Raised when there is no reply to a UDP packet
|
||||
#[error("BcUDP packet timeout")]
|
||||
BcUdpTimeout,
|
||||
|
||||
/// Raised when a BcUdp incomming connection is dropped
|
||||
#[error("BcUDP reciver dropped: {0:?}")]
|
||||
BcUdpDropReciver(BcUdpDropReciverKind),
|
||||
|
||||
/// Raised when a BcUdp outgoing connection is dropped
|
||||
#[error("BcUDP sender dropped")]
|
||||
BcUdpDropSender,
|
||||
|
||||
/// Raised when a BcUdp outgoing connection is dropped
|
||||
#[error("BcUDPPayload inner protocol was dropped")]
|
||||
BcUdpPayloadDroppedInner,
|
||||
|
||||
/// Raised when BcUdp sender fails to reconnect in time
|
||||
#[error("BcUDP reconnect timeout")]
|
||||
BcUdpReconnectTimeout,
|
||||
|
||||
/// Raised when a connection is dropped during a TryRecv event
|
||||
#[error("Send Error")]
|
||||
TokioBcSendError,
|
||||
@@ -196,6 +236,12 @@ pub enum Error {
|
||||
OtherString(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BcUdpDropReciverKind {
|
||||
NoneRecieved,
|
||||
SendFailed(String),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(k: std::io::Error) -> Self {
|
||||
// Check for other error that is already an Error of this type
|
||||
|
||||
@@ -129,7 +129,10 @@ impl BcCamera {
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
@@ -181,7 +184,10 @@ impl BcCamera {
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -29,7 +29,10 @@ impl BcCamera {
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
|
||||
@@ -27,7 +27,10 @@ impl BcCamera {
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
|
||||
@@ -39,10 +39,16 @@ impl BcCamera {
|
||||
reties += 1;
|
||||
continue;
|
||||
} else {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
} else if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
} else {
|
||||
// Valid message with response_code == 200
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
@@ -101,7 +107,10 @@ impl BcCamera {
|
||||
{
|
||||
let msg = reply?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcMeta {
|
||||
|
||||
@@ -317,7 +317,10 @@ impl BcCamera {
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
|
||||
@@ -64,7 +64,10 @@ impl BcCamera {
|
||||
sub.send(msg).await?;
|
||||
let msg = sub.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -34,7 +34,10 @@ impl BcCamera {
|
||||
{
|
||||
let msg = reply?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcMeta {
|
||||
@@ -90,10 +93,16 @@ impl BcCamera {
|
||||
reties += 1;
|
||||
continue;
|
||||
} else {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
} else if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
} else {
|
||||
// Valid message with response_code == 200
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
|
||||
@@ -39,7 +39,10 @@ impl BcCamera {
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -41,7 +41,10 @@ impl BcCamera {
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
@@ -132,7 +135,10 @@ impl BcCamera {
|
||||
}
|
||||
} else {
|
||||
// anything else is an error
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
// let binary_stream = sub_get.payload_stream();
|
||||
|
||||
@@ -51,17 +51,17 @@ impl StreamData {
|
||||
if handle.is_finished() {
|
||||
self.abort_handle.cancel();
|
||||
handle.await??;
|
||||
return Err(Error::DroppedConnection);
|
||||
return Err(Error::StreamFinished);
|
||||
}
|
||||
} else {
|
||||
self.abort_handle.cancel();
|
||||
return Err(Error::DroppedConnection);
|
||||
return Err(Error::StreamFinished);
|
||||
}
|
||||
match self.rx.recv().await {
|
||||
Some(data) => Ok(data),
|
||||
None => {
|
||||
self.abort_handle.cancel();
|
||||
Err(Error::DroppedConnection)
|
||||
Err(Error::StreamFinished)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -255,7 +255,10 @@ impl BcCamera {
|
||||
msg_id: MSG_ID_VIDEO_STOP,
|
||||
..
|
||||
} = msg.meta {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable{
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
}
|
||||
} => v,
|
||||
@@ -336,7 +339,10 @@ impl BcCamera {
|
||||
|
||||
let reply = sub_video.recv().await?;
|
||||
if reply.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(reply.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: reply.meta.msg_id,
|
||||
code: reply.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -27,7 +27,10 @@ impl BcCamera {
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
|
||||
@@ -26,7 +26,10 @@ impl BcCamera {
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
|
||||
@@ -410,7 +410,8 @@ impl BcCamera {
|
||||
// Chunks are still being played, while talk_stop will interrupt them. Wait until we expect
|
||||
// the stream to end (+ and extra 100ms) before issuing talk_stop.
|
||||
let remaining_stream_duration = expected_stream_end - std::time::Instant::now();
|
||||
std::thread::sleep(remaining_stream_duration + std::time::Duration::from_secs_f32(0.1));
|
||||
tokio::time::sleep(remaining_stream_duration + std::time::Duration::from_secs_f32(0.1))
|
||||
.await;
|
||||
|
||||
self.talk_stop().await?;
|
||||
|
||||
|
||||
@@ -33,7 +33,10 @@ impl BcCamera {
|
||||
sub_get_general.send(get).await?;
|
||||
let msg = sub_get_general.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
|
||||
@@ -25,7 +25,10 @@ impl BcCamera {
|
||||
sub_get.send(get).await?;
|
||||
let msg = sub_get.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: msg.meta.msg_id,
|
||||
code: msg.meta.response_code,
|
||||
});
|
||||
}
|
||||
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
|
||||
@@ -27,9 +27,10 @@ impl BcCamera {
|
||||
|
||||
let modern_reply = sub_version.recv().await?;
|
||||
if modern_reply.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavailable(
|
||||
modern_reply.meta.response_code,
|
||||
));
|
||||
return Err(Error::CameraServiceUnavailable {
|
||||
id: modern_reply.meta.msg_id,
|
||||
code: modern_reply.meta.response_code,
|
||||
});
|
||||
}
|
||||
let version_info;
|
||||
match modern_reply.body {
|
||||
|
||||
@@ -108,7 +108,7 @@ pub(super) const MAGIC_HEADER_BCMEDIA_IFRAME: u32 = 0x63643030;
|
||||
pub(super) const MAGIC_HEADER_BCMEDIA_IFRAME_LAST: u32 = 0x63643039;
|
||||
|
||||
/// Video Types for I/PFrame
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum VideoType {
|
||||
/// H264 video data
|
||||
H264,
|
||||
@@ -205,6 +205,50 @@ pub struct BcMediaAac {
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl BcMediaAac {
|
||||
/// Read the ADTS header to learn the duration in micro secs
|
||||
pub fn duration(&self) -> Option<u32> {
|
||||
if self.data.len() < 8 {
|
||||
// Too small for the header
|
||||
return None;
|
||||
}
|
||||
if self.data[0] != 0b11111111 {
|
||||
// Syncword incorrect
|
||||
return None;
|
||||
}
|
||||
if (self.data[1] & 0b11110000) != 0b11110000 {
|
||||
// Syncword incorrect
|
||||
return None;
|
||||
}
|
||||
let frequency_index = (self.data[2] & 0b00111100) >> 2;
|
||||
let sample_frequency = match frequency_index {
|
||||
0 => Some(96000u32),
|
||||
1 => Some(88200u32),
|
||||
2 => Some(64000u32),
|
||||
3 => Some(48000u32),
|
||||
4 => Some(44100u32),
|
||||
5 => Some(32000u32),
|
||||
6 => Some(24000u32),
|
||||
7 => Some(22050u32),
|
||||
8 => Some(16000u32),
|
||||
9 => Some(12000u32),
|
||||
10 => Some(11025u32),
|
||||
11 => Some(8000u32),
|
||||
12 => Some(7350u32),
|
||||
_ => None,
|
||||
}?;
|
||||
log::trace!("sample_frequency: {sample_frequency}");
|
||||
|
||||
let frames = (self.data[6] & 0b00000011) + 1;
|
||||
log::trace!("frames: {frames}");
|
||||
let samples = frames as u32 * 1024;
|
||||
log::trace!("samples: {samples}");
|
||||
const MICROSECONDS: u32 = 1000000;
|
||||
let duration = samples * MICROSECONDS / sample_frequency;
|
||||
Some(duration)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) const MAGIC_HEADER_BCMEDIA_ADPCM: u32 = 0x62773130;
|
||||
|
||||
pub(super) const MAGIC_HEADER_BCMEDIA_ADPCM_DATA: u16 = 0x0100;
|
||||
@@ -231,3 +275,20 @@ pub struct BcMediaAdpcm {
|
||||
/// To calculate the block-align size simply remove 4 from the `len()`
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl BcMediaAdpcm {
|
||||
/// The block size, this is bytes without the block header
|
||||
pub fn block_size(&self) -> u32 {
|
||||
self.data.len() as u32 - 4
|
||||
}
|
||||
|
||||
/// Returns duration in micro seconds;
|
||||
pub fn duration(&self) -> Option<u32> {
|
||||
let samples = self.block_size() * 2;
|
||||
// Always 8000Hz for ADPCM
|
||||
const SAMPLE_FREQUENCY: u32 = 8000;
|
||||
const MICROSECONDS: u32 = 1000000;
|
||||
let duration = samples * MICROSECONDS / SAMPLE_FREQUENCY;
|
||||
Some(duration)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "neolink_decoder"
|
||||
description = "A cli decoder for the AES encrypted packets"
|
||||
version = "0.6.3-rc.2"
|
||||
version = "0.6.3-rc.3"
|
||||
authors = ["Andrew King <sheepchaan@gmail.com>"]
|
||||
edition = "2018"
|
||||
license = "AGPL-3.0-or-later"
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "mailnoti"
|
||||
version = "0.6.3-rc.3"
|
||||
edition = "2021"
|
||||
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.71"
|
||||
async-std = "1.12.0"
|
||||
clap = { version = "4.2.2", features = ["derive", "cargo"] }
|
||||
env_logger = "0.10.0"
|
||||
get_if_addrs = "0.5.3"
|
||||
lazy_static = "1.4.0"
|
||||
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
|
||||
mailin-embedded = "0.8.2"
|
||||
neolink_core = { path = "../core", version = "0.6.3-rc.3" }
|
||||
once_cell = "1.19.0"
|
||||
regex = "1.8.1"
|
||||
serde = "1.0.163"
|
||||
tokio = { version = "1.28.1", features = ["full"] }
|
||||
toml = "0.8.2"
|
||||
validator = {version="0.18.1", features = ["derive"] }
|
||||
@@ -0,0 +1,6 @@
|
||||
# Reolink Mail Notifications
|
||||
|
||||
Because google FCM has removed the push notification api we use
|
||||
we now must resort to email notifications for PIR messages
|
||||
|
||||
This is a test crate to investigate this
|
||||
@@ -0,0 +1,67 @@
|
||||
use neolink_core::bc_protocol::DiscoveryMethods;
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use serde::Deserialize;
|
||||
use std::clone::Clone;
|
||||
use validator::{Validate, ValidationError};
|
||||
|
||||
static RE_MAXENC_SRC: Lazy<Regex> = Lazy::new(|| {
|
||||
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap()
|
||||
});
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
pub(crate) struct Config {
|
||||
#[validate(nested)]
|
||||
pub(crate) cameras: Vec<CameraConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
#[validate(schema(function = "validate_camera_config"))]
|
||||
pub(crate) struct CameraConfig {
|
||||
pub(crate) name: String,
|
||||
|
||||
#[serde(rename = "address")]
|
||||
pub(crate) camera_addr: Option<String>,
|
||||
|
||||
#[serde(rename = "uid")]
|
||||
pub(crate) camera_uid: Option<String>,
|
||||
|
||||
pub(crate) username: String,
|
||||
pub(crate) password: Option<String>,
|
||||
|
||||
#[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))]
|
||||
#[serde(default = "default_channel_id", alias = "channel")]
|
||||
pub(crate) channel_id: u8,
|
||||
|
||||
#[serde(default = "default_discovery")]
|
||||
pub(crate) discovery: DiscoveryMethods,
|
||||
|
||||
#[serde(default = "default_maxenc")]
|
||||
#[validate(regex(
|
||||
path = *RE_MAXENC_SRC,
|
||||
message = "Invalid maximum encryption method",
|
||||
code = "max_encryption"
|
||||
))]
|
||||
pub(crate) max_encryption: String,
|
||||
}
|
||||
|
||||
fn default_discovery() -> DiscoveryMethods {
|
||||
DiscoveryMethods::Relay
|
||||
}
|
||||
|
||||
fn default_maxenc() -> String {
|
||||
"Aes".to_string()
|
||||
}
|
||||
|
||||
fn default_channel_id() -> u8 {
|
||||
0
|
||||
}
|
||||
|
||||
fn validate_camera_config(camera_config: &CameraConfig) -> Result<(), ValidationError> {
|
||||
match (&camera_config.camera_addr, &camera_config.camera_uid) {
|
||||
(None, None) => Err(ValidationError::new(
|
||||
"Either camera address or uid must be given",
|
||||
)),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
//! Main test app for mail
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use clap::Parser;
|
||||
use neolink_core::{
|
||||
bc::xml::{xml_ver, Email},
|
||||
bc_protocol::BcCamera,
|
||||
};
|
||||
use std::{
|
||||
fs,
|
||||
net::{IpAddr, SocketAddr},
|
||||
};
|
||||
use validator::Validate;
|
||||
|
||||
mod config;
|
||||
mod opt;
|
||||
mod utils;
|
||||
|
||||
use config::Config;
|
||||
use opt::Opt;
|
||||
use utils::find_and_connect;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
||||
|
||||
let opt = Opt::parse();
|
||||
|
||||
let conf_path = opt.config.context("Must supply --config file")?;
|
||||
let config: Config = toml::from_str(
|
||||
&fs::read_to_string(&conf_path)
|
||||
.with_context(|| format!("Failed to read {:?}", conf_path))?,
|
||||
)
|
||||
.with_context(|| format!("Failed to parse the {:?} config file", conf_path))?;
|
||||
|
||||
config
|
||||
.validate()
|
||||
.with_context(|| format!("Failed to validate the {:?} config file", conf_path))?;
|
||||
|
||||
let name = opt.camera.clone();
|
||||
let camera = find_and_connect(&config, &opt.camera).await?;
|
||||
|
||||
const MAIL_PORT: u16 = 22022;
|
||||
// let addr = SocketAddr::from((get_local_ip()?, MAIL_PORT));
|
||||
let cam_addr = SocketAddr::from((IpAddr::from([192, 168, 1, 201]), MAIL_PORT));
|
||||
let post_addr = SocketAddr::from((IpAddr::from([127, 0, 0, 1]), MAIL_PORT));
|
||||
|
||||
tokio::select! {
|
||||
v = cam_tasks(&name, camera, cam_addr) => v,
|
||||
v = mail_server(&name, post_addr) => v,
|
||||
}?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_local_ip() -> Result<std::net::IpAddr> {
|
||||
get_if_addrs::get_if_addrs()?
|
||||
.iter()
|
||||
.find(|i| !i.is_loopback() && matches!(i.addr, get_if_addrs::IfAddr::V4(_)))
|
||||
.map(|iface| Ok(iface.ip()))
|
||||
.unwrap_or_else(|| Err(anyhow!("No Local Ip Address Found")))
|
||||
}
|
||||
|
||||
async fn cam_tasks(name: &str, camera: BcCamera, addr: SocketAddr) -> Result<()> {
|
||||
let support = camera.get_support().await?;
|
||||
if support.email.is_some_and(|v| v > 0) {
|
||||
let ip = addr.ip();
|
||||
let port = addr.port();
|
||||
log::info!("Telling camera to send the mail to {}:{}", ip, port);
|
||||
let mail_settings = Email {
|
||||
version: xml_ver(),
|
||||
smtp_server: format!("{}", ip),
|
||||
user_name: format!("{name}@neolink.neolink"),
|
||||
password: "TestPass".to_owned(),
|
||||
address1: "neolink@neolink.neolink".to_owned(),
|
||||
address2: "".to_owned(),
|
||||
address3: "".to_owned(),
|
||||
smtp_port: addr.port(),
|
||||
send_nickname: name.to_string(),
|
||||
attachment: 1,
|
||||
attachment_type: Some("picture".to_owned()),
|
||||
text_type: "withText".to_owned(),
|
||||
ssl: 0,
|
||||
interval: 30,
|
||||
sender_max_len: None,
|
||||
};
|
||||
camera.set_email(mail_settings.clone()).await?;
|
||||
camera.email_on_always().await?;
|
||||
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
|
||||
log::info!("Sending test email");
|
||||
camera.test_email(mail_settings).await?;
|
||||
|
||||
// Wait to give me time to wave at the camera please
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(300)).await;
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow::anyhow!(
|
||||
"Camera does not support email notfications"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
use mailin_embedded::{
|
||||
response::{self, Response},
|
||||
Handler, Server, SslConfig,
|
||||
};
|
||||
use regex::Regex;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct MailHandler {
|
||||
name: String,
|
||||
}
|
||||
impl Handler for MailHandler {
|
||||
fn helo(&mut self, _ip: IpAddr, _domain: &str) -> Response {
|
||||
log::debug!("HELO");
|
||||
response::OK
|
||||
}
|
||||
|
||||
fn rcpt(&mut self, to: &str) -> Response {
|
||||
log::debug!("rcpr:: {to}");
|
||||
let re = Regex::new(r"^(.*)@neolink.neolink$").unwrap();
|
||||
if re.is_match(to) {
|
||||
response::OK
|
||||
} else {
|
||||
response::NO_MAILBOX
|
||||
}
|
||||
}
|
||||
|
||||
fn mail(&mut self, ip: IpAddr, domain: &str, from: &str) -> Response {
|
||||
log::debug!("mail:: ip: {ip:?}, domain: {domain}, from: {from}");
|
||||
response::OK
|
||||
}
|
||||
|
||||
fn data_start(&mut self, domain: &str, from: &str, is8bit: bool, to: &[String]) -> Response {
|
||||
log::debug!("data_start:: domain: {domain}, from: {from}, is8bit: {is8bit}, to: {to:?}");
|
||||
response::OK
|
||||
}
|
||||
|
||||
fn data(&mut self, buf: &[u8]) -> std::io::Result<()> {
|
||||
let text = String::from_utf8_lossy(buf);
|
||||
log::debug!("data:: text: {text}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn data_end(&mut self) -> Response {
|
||||
log::debug!("data_end::");
|
||||
response::OK
|
||||
}
|
||||
|
||||
fn auth_login(&mut self, username: &str, password: &str) -> Response {
|
||||
log::debug!("auth_login;: username: {username}, password: {password}");
|
||||
let correct_username = format!("{}@neolink.neolink", self.name);
|
||||
let correct_password = "TestPass";
|
||||
if username == correct_username && password == correct_password {
|
||||
response::AUTH_OK
|
||||
} else {
|
||||
response::INVALID_CREDENTIALS
|
||||
}
|
||||
}
|
||||
|
||||
fn auth_plain(
|
||||
&mut self,
|
||||
authorization_id: &str,
|
||||
authentication_id: &str,
|
||||
password: &str,
|
||||
) -> Response {
|
||||
log::debug!("auth_plain:: authorization_id: {authorization_id}, authentication_id: {authentication_id}, password: {password}");
|
||||
response::INVALID_CREDENTIALS
|
||||
}
|
||||
}
|
||||
|
||||
async fn mail_server(name: &str, addr: SocketAddr) -> Result<()> {
|
||||
let handler = MailHandler {
|
||||
name: name.to_string(),
|
||||
};
|
||||
let mut server = Server::new(handler);
|
||||
|
||||
server
|
||||
.with_name("neolink.neolink")
|
||||
.with_ssl(SslConfig::None)
|
||||
.map_err(|e| anyhow!("{e:?}"))?
|
||||
.with_addr(addr)
|
||||
.map_err(|e| anyhow!("{e:?}"))?;
|
||||
|
||||
tokio::task::spawn_blocking(move || server.serve().map_err(|e| anyhow!("{e:?}"))).await??;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
use clap::{crate_authors, crate_version, Parser};
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
/// A standards-compliant bridge to Reolink IP cameras
|
||||
///
|
||||
/// Neolink is free software released under the GNU AGPL v3.
|
||||
/// You can find its source code at https://github.com/thirtythreeforty/neolink
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(name = "pushnoti", arg_required_else_help = true, version = crate_version!(), author = crate_authors!("\n"))]
|
||||
pub struct Opt {
|
||||
#[arg(short, long, value_parser = PathBuf::from_str)]
|
||||
pub config: Option<PathBuf>,
|
||||
/// The name of the camera. Must be a name in the config
|
||||
pub camera: String,
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
//! Contains code that is not specific to any of the subcommands
|
||||
//!
|
||||
use log::*;
|
||||
|
||||
use super::config::{CameraConfig, Config};
|
||||
use anyhow::{anyhow, Context, Error, Result};
|
||||
use neolink_core::bc_protocol::{
|
||||
BcCamera, BcCameraOpt, ConnectionProtocol, Credentials, DiscoveryMethods, MaxEncryption,
|
||||
};
|
||||
use std::{
|
||||
fmt::{Display, Error as FmtError, Formatter},
|
||||
net::{IpAddr, ToSocketAddrs},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
pub(crate) enum AddressOrUid {
|
||||
Address(String),
|
||||
#[allow(dead_code)]
|
||||
Uid(String, DiscoveryMethods),
|
||||
#[allow(dead_code)]
|
||||
AddressWithUid(String, String, DiscoveryMethods),
|
||||
}
|
||||
|
||||
impl Display for AddressOrUid {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
|
||||
match self {
|
||||
AddressOrUid::AddressWithUid(addr, uid, _) => {
|
||||
write!(f, "Address: {}, UID: {}", addr, uid)
|
||||
}
|
||||
AddressOrUid::Address(host) => write!(f, "Address: {}", host),
|
||||
AddressOrUid::Uid(host, _) => write!(f, "UID: {}", host),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AddressOrUid {
|
||||
// Created by translating the config fields directly
|
||||
pub(crate) fn new(
|
||||
address: &Option<String>,
|
||||
uid: &Option<String>,
|
||||
method: &DiscoveryMethods,
|
||||
) -> Result<Self, Error> {
|
||||
match (address, uid) {
|
||||
(None, None) => Err(anyhow!("Neither address or uid given")),
|
||||
(Some(host), Some(uid)) => Ok(AddressOrUid::AddressWithUid(
|
||||
host.clone(),
|
||||
uid.clone(),
|
||||
*method,
|
||||
)),
|
||||
(Some(host), None) => Ok(AddressOrUid::Address(host.clone())),
|
||||
(None, Some(host)) => Ok(AddressOrUid::Uid(host.clone(), *method)),
|
||||
}
|
||||
}
|
||||
|
||||
// Convience method to get the BcCamera with the appropiate method
|
||||
// from a camera_config
|
||||
pub(crate) async fn connect_camera(
|
||||
&self,
|
||||
camera_config: &CameraConfig,
|
||||
) -> Result<BcCamera, Error> {
|
||||
let (port, addrs) = {
|
||||
if let Some(addr_str) = camera_config.camera_addr.as_ref() {
|
||||
match addr_str.to_socket_addrs() {
|
||||
Ok(addr_iter) => {
|
||||
let mut port = None;
|
||||
let mut ipaddrs = vec![];
|
||||
for addr in addr_iter {
|
||||
port = Some(addr.port());
|
||||
ipaddrs.push(addr.ip());
|
||||
}
|
||||
Ok((port, ipaddrs))
|
||||
}
|
||||
Err(_) => match IpAddr::from_str(addr_str) {
|
||||
Ok(ip) => Ok((None, vec![ip])),
|
||||
Err(_) => Err(anyhow!("Could not parse address in config")),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
Ok((None, vec![]))
|
||||
}
|
||||
}?;
|
||||
|
||||
let options = BcCameraOpt {
|
||||
name: camera_config.name.clone(),
|
||||
channel_id: camera_config.channel_id,
|
||||
addrs,
|
||||
port,
|
||||
uid: camera_config.camera_uid.clone(),
|
||||
protocol: ConnectionProtocol::TcpUdp,
|
||||
discovery: camera_config.discovery,
|
||||
max_discovery_retries: 10,
|
||||
credentials: Credentials {
|
||||
username: camera_config.username.clone(),
|
||||
password: camera_config.password.clone(),
|
||||
},
|
||||
debug: false,
|
||||
};
|
||||
|
||||
trace!("Camera Info: {:?}", options);
|
||||
|
||||
Ok(BcCamera::new(&options).await?)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn find_and_connect(config: &Config, name: &str) -> Result<BcCamera> {
|
||||
let camera_config = find_camera_by_name(config, name)?;
|
||||
connect_and_login(camera_config).await
|
||||
}
|
||||
|
||||
pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result<BcCamera> {
|
||||
let camera_addr = AddressOrUid::new(
|
||||
&camera_config.camera_addr,
|
||||
&camera_config.camera_uid,
|
||||
&camera_config.discovery,
|
||||
)
|
||||
.unwrap();
|
||||
info!(
|
||||
"{}: Connecting to camera at {}",
|
||||
camera_config.name, camera_addr
|
||||
);
|
||||
|
||||
let camera = camera_addr
|
||||
.connect_camera(camera_config)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Failed to connect to camera {} at {} on channel {}",
|
||||
camera_config.name, camera_addr, camera_config.channel_id
|
||||
)
|
||||
})?;
|
||||
|
||||
let max_encryption = match camera_config.max_encryption.to_lowercase().as_str() {
|
||||
"none" => MaxEncryption::None,
|
||||
"bcencrypt" => MaxEncryption::BcEncrypt,
|
||||
"aes" => MaxEncryption::Aes,
|
||||
_ => MaxEncryption::Aes,
|
||||
};
|
||||
info!("{}: Logging in", camera_config.name);
|
||||
camera
|
||||
.login_with_maxenc(max_encryption)
|
||||
.await
|
||||
.with_context(|| format!("Failed to login to {}", camera_config.name))?;
|
||||
|
||||
info!("{}: Connected and logged in", camera_config.name);
|
||||
|
||||
Ok(camera)
|
||||
}
|
||||
|
||||
pub(crate) fn find_camera_by_name<'a>(config: &'a Config, name: &str) -> Result<&'a CameraConfig> {
|
||||
config
|
||||
.cameras
|
||||
.iter()
|
||||
.find(|c| c.name == name)
|
||||
.ok_or_else(|| anyhow!("Camera {} not found in the config file", name))
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "pushnoti"
|
||||
version = "0.6.3-rc.2"
|
||||
version = "0.6.3-rc.3"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
@@ -9,13 +9,14 @@ edition = "2021"
|
||||
anyhow = "1.0.71"
|
||||
clap = { version = "4.2.2", features = ["derive", "cargo"] }
|
||||
env_logger = "0.10.0"
|
||||
fcm-push-listener = "2.0.1"
|
||||
# fcm-push-listener = { path = "../fcm-push-listener" }
|
||||
fcm-push-listener = { version = "2.0.3" }
|
||||
lazy_static = "1.4.0"
|
||||
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
|
||||
neolink_core = { path = "../core", version = "0.6.3-rc.2" }
|
||||
neolink_core = { path = "../core", version = "0.6.3-rc.3" }
|
||||
once_cell = "1.19.0"
|
||||
regex = "1.8.1"
|
||||
serde = "1.0.163"
|
||||
tokio = { version = "1.28.1", features = ["full"] }
|
||||
toml = "0.8.2"
|
||||
validator = "0.16.0"
|
||||
validator_derive = "0.16.0"
|
||||
validator = {version="0.18.1", features = ["derive"] }
|
||||
@@ -1,23 +1,17 @@
|
||||
use lazy_static::lazy_static;
|
||||
use neolink_core::bc_protocol::DiscoveryMethods;
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use serde::Deserialize;
|
||||
use std::clone::Clone;
|
||||
use validator::{Validate, ValidationError};
|
||||
use validator_derive::Validate;
|
||||
|
||||
lazy_static! {
|
||||
static ref RE_STREAM_SRC: Regex =
|
||||
Regex::new(r"^(mainStream|subStream|externStream|both|all)$").unwrap();
|
||||
static ref RE_TLS_CLIENT_AUTH: Regex = Regex::new(r"^(none|request|require)$").unwrap();
|
||||
static ref RE_PAUSE_MODE: Regex = Regex::new(r"^(black|still|test|none)$").unwrap();
|
||||
static ref RE_MAXENC_SRC: Regex =
|
||||
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap();
|
||||
}
|
||||
static RE_MAXENC_SRC: Lazy<Regex> = Lazy::new(|| {
|
||||
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap()
|
||||
});
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
pub(crate) struct Config {
|
||||
#[validate]
|
||||
#[validate(nested)]
|
||||
pub(crate) cameras: Vec<CameraConfig>,
|
||||
}
|
||||
|
||||
@@ -36,7 +30,7 @@ pub(crate) struct CameraConfig {
|
||||
pub(crate) password: Option<String>,
|
||||
|
||||
#[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))]
|
||||
#[serde(default = "default_channel_id")]
|
||||
#[serde(default = "default_channel_id", alias = "channel")]
|
||||
pub(crate) channel_id: u8,
|
||||
|
||||
#[serde(default = "default_discovery")]
|
||||
@@ -44,7 +38,7 @@ pub(crate) struct CameraConfig {
|
||||
|
||||
#[serde(default = "default_maxenc")]
|
||||
#[validate(regex(
|
||||
path = "RE_MAXENC_SRC",
|
||||
path = *RE_MAXENC_SRC,
|
||||
message = "Invalid maximum encryption method",
|
||||
code = "max_encryption"
|
||||
))]
|
||||
|
||||
@@ -41,6 +41,11 @@ async fn main() -> Result<()> {
|
||||
// into this program
|
||||
// let registration = fcm_push_listener::register("263684512460").await?;
|
||||
|
||||
// let firebase_app_id = "1:743639030586:android:86f60a4fb7143876";
|
||||
// let firebase_project_id = "reolink-login";
|
||||
// let firebase_api_key = "AIzaSyBEUIuWHnnOEwFahxWgQB4Yt4NsgOmkPyE";
|
||||
// let vapid_key = "";
|
||||
|
||||
let sender_id = "743639030586"; // andriod
|
||||
|
||||
// let sender_id = "696841269229"; // ios
|
||||
@@ -56,6 +61,13 @@ async fn main() -> Result<()> {
|
||||
} else {
|
||||
info!("Registering new token");
|
||||
let registration = fcm_push_listener::register(sender_id).await?;
|
||||
// let registration = fcm_push_listener::register(
|
||||
// firebase_app_id,
|
||||
// firebase_project_id,
|
||||
// firebase_api_key,
|
||||
// vapid_key,
|
||||
// )
|
||||
// .await?;
|
||||
let new_token = toml::to_string(®istration)?;
|
||||
fs::write(token_path, new_token)?;
|
||||
registration
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ make LUA_DIR=/usr
|
||||
The shared object library should be copied to `/usr/local/lib/lua/5.2/`
|
||||
```
|
||||
mkdir --parents /usr/local/lib/lua/5.2/
|
||||
cp luagcypt.so /usr/local/lib/lua/5.2/
|
||||
cp luagcrypt.so /usr/local/lib/lua/5.2/
|
||||
```
|
||||
Additionally, the system where the dissector is used needs these packages installing (if not already present): `libgcrypt20 libgpg-error0`
|
||||
## Install `baichuan.lua`
|
||||
|
||||
+13
-8
@@ -222,13 +222,15 @@ local header_lengths = {
|
||||
-- For other locations, use: LUA_CPATH=.../luagcrypt/?.so
|
||||
bc_protocol.prefs.key = Pref.string("Decryption key", "",
|
||||
"Passphrase used for the camera. Required to decrypt the AES packets")
|
||||
_G.nonce = ""
|
||||
_G.nonce = {}
|
||||
local function hexencode(str)
|
||||
return (str:gsub(".", function(char) return string.format("%02X", char:byte()) end))
|
||||
end
|
||||
local gcrypt = require("luagcrypt")
|
||||
local function aes_decrypt(data)
|
||||
local raw_key = (_G.nonce or "") .. "-" .. bc_protocol.prefs.key
|
||||
local function aes_decrypt(data, pinfo)
|
||||
local nonce_key = tostring(pinfo.src) .. ":" .. pinfo.src_port
|
||||
local nonce = (_G.nonce[nonce_key] or "")
|
||||
local raw_key = nonce .. "-" .. bc_protocol.prefs.key
|
||||
local iv = "0123456789abcdef"
|
||||
local hasher = gcrypt.Hash(gcrypt.MD_MD5)
|
||||
hasher:write(raw_key)
|
||||
@@ -358,9 +360,9 @@ local function process_body(header, body_buffer, bc_subtree, pinfo)
|
||||
local body_tvb = xml_buffer:tvb("Meta Payload")
|
||||
body:add(body_tvb(), "Meta Payload")
|
||||
if xml_len >= 4 then
|
||||
if aes_decrypt(xml_buffer:raw(0,5)):raw() == "<?xml" then -- AES encrypted
|
||||
if aes_decrypt(xml_buffer:raw(0,5), pinfo):raw() == "<?xml" then -- AES encrypted
|
||||
local ba = xml_buffer:bytes()
|
||||
local decrypted = aes_decrypt(ba:raw())
|
||||
local decrypted = aes_decrypt(ba:raw(), pinfo)
|
||||
body_tvb = decrypted:tvb("Decrypted XML (in Meta Payload)")
|
||||
-- Create a tree item that, when clicked, automatically shows the tab we just created
|
||||
body:add(body_tvb(), "Decrypted XML (in Meta Payload)")
|
||||
@@ -369,7 +371,10 @@ local function process_body(header, body_buffer, bc_subtree, pinfo)
|
||||
local ba = xml_buffer:bytes()
|
||||
local decrypted = xml_decrypt(ba, header.enc_offset)
|
||||
local new_noonce = string.match(decrypted:raw(), "[<]nonce[>][ \t\n]*([^ \t\n<]+)[ \t\n]*[<][/]nonce[>]")
|
||||
_G.nonce = new_noonce;
|
||||
local nonce_key = tostring(pinfo.src) .. ":" .. pinfo.src_port
|
||||
_G.nonce[nonce_key] = new_noonce;
|
||||
nonce_key = tostring(pinfo.dst) .. ":" .. pinfo.dst_port
|
||||
_G.nonce[nonce_key] = new_noonce;
|
||||
body_tvb = decrypted:tvb("Decrypted XML (in Meta Payload)")
|
||||
-- Create a tree item that, when clicked, automatically shows the tab we just created
|
||||
body:add(body_tvb(), "Decrypted XML (in Meta Payload)")
|
||||
@@ -390,9 +395,9 @@ local function process_body(header, body_buffer, bc_subtree, pinfo)
|
||||
local body_tvb = binary_buffer:tvb("Main Payload");
|
||||
body:add(body_tvb(), "Main Payload")
|
||||
if bin_len > 4 then
|
||||
if aes_decrypt(binary_buffer:raw(0,5)):raw() == "<?xml" then -- AES encrypted
|
||||
if aes_decrypt(binary_buffer:raw(0,5), pinfo):raw() == "<?xml" then -- AES encrypted
|
||||
local ba = binary_buffer:bytes()
|
||||
local decrypted = aes_decrypt(ba:raw())
|
||||
local decrypted = aes_decrypt(ba:raw(), pinfo)
|
||||
body_tvb = decrypted:tvb("Decrypted XML (in Binary Payload)")
|
||||
-- Create a tree item that, when clicked, automatically shows the tab we just created
|
||||
body:add(body_tvb(), "Decrypted XML (in Binary Payload)")
|
||||
|
||||
+974
-659
File diff suppressed because it is too large.
Load diff
@@ -54,7 +54,7 @@ When receiving packet these should be used to quickly discard invalid packets.
|
||||
Each function in BC has its own message ID. For example login is 1, video data
|
||||
is 3, motion detection is 33.
|
||||
|
||||
For a more complete list please see the [messages doc](dissector/messages.md)
|
||||
For a more complete list please see the [messages doc](messages.md)
|
||||
|
||||
#### Message length
|
||||
|
||||
@@ -146,7 +146,7 @@ extension XML part of the message.
|
||||
|
||||
# Login
|
||||
|
||||
For message details see the [docs](dissector/messages.md)
|
||||
For message details see the [docs](messages.md)
|
||||
|
||||
Clients should login by
|
||||
|
||||
@@ -208,7 +208,7 @@ After which all message bodies of type id 3 are binary.
|
||||
The binary represents a stream of data that can be interrupted by packet
|
||||
boundaries. Clients should create a buffer and pop bytes for processing when
|
||||
complete media packets are received. Media packets descriptions can be found in
|
||||
the [docs](dissector/mediapacket.md)
|
||||
the [docs](mediapacket.md)
|
||||
|
||||
# Other Function
|
||||
|
||||
@@ -256,7 +256,7 @@ Which contains the plain text of the username of interest and the tokens for
|
||||
abilities you want to know about.
|
||||
|
||||
Details of expected formats should be found from the
|
||||
[docs](dissector/messages.md)
|
||||
[docs](messages.md)
|
||||
|
||||
#### AES Encryption
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ exit_func() {
|
||||
}
|
||||
trap exit_func TERM INT
|
||||
|
||||
ulimit -n 1024
|
||||
ulimit -n 65535
|
||||
|
||||
echo "Running: ${*}"
|
||||
"$@"
|
||||
@@ -17,14 +17,18 @@ pub struct Opt {
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
pub enum Command {
|
||||
#[cfg(feature = "gstreamer")]
|
||||
Rtsp(super::rtsp::Opt),
|
||||
StatusLight(super::statusled::Opt),
|
||||
Reboot(super::reboot::Opt),
|
||||
Pir(super::pir::Opt),
|
||||
Ptz(super::ptz::Opt),
|
||||
#[cfg(feature = "gstreamer")]
|
||||
Talk(super::talk::Opt),
|
||||
Mqtt(super::mqtt::Opt),
|
||||
#[cfg(feature = "gstreamer")]
|
||||
MqttRtsp(super::mqtt::Opt),
|
||||
#[cfg(feature = "gstreamer")]
|
||||
Image(super::image::Opt),
|
||||
Battery(super::battery::Opt),
|
||||
Services(super::services::Opt),
|
||||
|
||||
+15
-5
@@ -37,10 +37,14 @@ impl NeoCamThread {
|
||||
}
|
||||
async fn run_camera(&mut self, config: &CameraConfig) -> AnyResult<()> {
|
||||
let name = config.name.clone();
|
||||
log::trace!("Attempting connection with config: {config:?}");
|
||||
let camera = Arc::new(connect_and_login(config).await?);
|
||||
log::trace!(" - Connected");
|
||||
|
||||
sleep(Duration::from_secs(2)).await; // Delay a little since some calls will error if camera is waking up
|
||||
update_camera_time(&camera, &name, config.update_time).await?;
|
||||
if let Err(e) = update_camera_time(&camera, &name, config.update_time).await {
|
||||
log::warn!("Could not set camera time, (perhaps missing on this camera of your login in not an admin): {e:?}");
|
||||
}
|
||||
sleep(Duration::from_secs(2)).await; // Delay a little since some calls will error if camera is waking up
|
||||
|
||||
self.camera_watch.send_replace(Arc::downgrade(&camera));
|
||||
@@ -60,13 +64,16 @@ impl NeoCamThread {
|
||||
let mut missed_pings = 0;
|
||||
loop {
|
||||
interval.tick().await;
|
||||
log::trace!("Sending ping");
|
||||
match timeout(Duration::from_secs(5), camera.get_linktype()).await {
|
||||
Ok(Ok(_)) => {
|
||||
log::trace!("Ping reply");
|
||||
missed_pings = 0;
|
||||
continue
|
||||
},
|
||||
Ok(Err(neolink_core::Error::UnintelligibleReply { .. })) => {
|
||||
Ok(Err(neolink_core::Error::UnintelligibleReply { reply, why })) => {
|
||||
// Camera does not support pings just wait forever
|
||||
log::trace!("Pings not supported: {reply:?}: {why}");
|
||||
futures::future::pending().await
|
||||
},
|
||||
Ok(Err(e)) => {
|
||||
@@ -74,12 +81,12 @@ impl NeoCamThread {
|
||||
},
|
||||
Err(_) => {
|
||||
// Timeout
|
||||
if missed_pings > 5 {
|
||||
if missed_pings < 5 {
|
||||
missed_pings += 1;
|
||||
continue;
|
||||
} else {
|
||||
log::debug!("Timed out waiting for camera ping reply. Assuming unsupported");
|
||||
futures::future::pending().await
|
||||
log::error!("Timed out waiting for camera ping reply");
|
||||
break Err(anyhow::anyhow!("Timed out waiting for camera ping reply"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -121,6 +128,7 @@ impl NeoCamThread {
|
||||
None
|
||||
}
|
||||
Ok(_) = state.wait_for(|state| matches!(state, NeoCamThreadState::Disconnected)) => {
|
||||
log::trace!("State changed to disconnect");
|
||||
None
|
||||
}
|
||||
v = self.run_camera(&config) => {
|
||||
@@ -133,6 +141,7 @@ impl NeoCamThread {
|
||||
// If None go back and reload NOW
|
||||
//
|
||||
// This occurs if there was a config change
|
||||
log::trace!("Config change or Manual disconnect");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -150,6 +159,7 @@ impl NeoCamThread {
|
||||
match result {
|
||||
Ok(()) => {
|
||||
// Normal shutdown
|
||||
log::trace!("Normal camera shutdown");
|
||||
self.cancel.cancel();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
+215
-42
@@ -5,20 +5,28 @@
|
||||
//! The camera watch is used as an event to be triggered
|
||||
//! whenever the camera is lost/updated
|
||||
use anyhow::{anyhow, Context};
|
||||
use futures::TryFutureExt;
|
||||
use futures::{stream::FuturesUnordered, FutureExt, StreamExt, TryFutureExt};
|
||||
use std::sync::{Arc, Weak};
|
||||
#[cfg(feature = "pushnoti")]
|
||||
use tokio::sync::watch::channel as watch;
|
||||
use tokio::{
|
||||
sync::{
|
||||
mpsc::Sender as MpscSender, oneshot::channel as oneshot, watch::channel as watch,
|
||||
mpsc::Receiver as MpscReceiver, mpsc::Sender as MpscSender, oneshot::channel as oneshot,
|
||||
watch::Receiver as WatchReceiver,
|
||||
},
|
||||
time::{sleep, Duration},
|
||||
};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use super::{MdState, NeoCamCommand, NeoCamThreadState, Permit, PushNoti, StreamInstance};
|
||||
#[cfg(feature = "pushnoti")]
|
||||
use super::PushNoti;
|
||||
use super::{MdState, NeoCamCommand, NeoCamThreadState, Permit, UseCounter};
|
||||
use crate::{config::CameraConfig, AnyResult, Result};
|
||||
use neolink_core::bc_protocol::{BcCamera, StreamKind};
|
||||
use neolink_core::{
|
||||
bc_protocol::{BcCamera, StreamKind},
|
||||
bcmedia::model::BcMedia,
|
||||
};
|
||||
#[cfg(feature = "gstreamer")]
|
||||
|
||||
/// This instance is the primary interface used throughout the app
|
||||
///
|
||||
@@ -120,15 +128,17 @@ impl NeoInstance {
|
||||
if let Some(cam) = camera.clone() {
|
||||
let cam_ref = cam.as_ref();
|
||||
let mut r = Err(anyhow!("No run"));
|
||||
for _ in 0..5 {
|
||||
for i in 0..5 {
|
||||
r = task(cam_ref).await;
|
||||
if let Err(e) = &r {
|
||||
log::trace!("- Task Result: {e:?}");
|
||||
log::debug!("- Task Error: {e:?}");
|
||||
}
|
||||
if let Err(Some(neolink_core::Error::CameraServiceUnavailable(400))) = r.as_ref().map_err(|e| e.downcast_ref::<neolink_core::Error>()) {
|
||||
if let Err(Some(e @ neolink_core::Error::CameraServiceUnavailable{code: 400, ..})) = r.as_ref().map_err(|e| e.downcast_ref::<neolink_core::Error>()) {
|
||||
// Retryable without a reconnect
|
||||
// Usually occurs when camera is starting up
|
||||
// or the connection is initialising
|
||||
log::debug!("Got a 400 code for {e:?} retry {i}/5, ");
|
||||
|
||||
sleep(Duration::from_secs(1)).await;
|
||||
continue;
|
||||
} else {
|
||||
@@ -223,41 +233,7 @@ impl NeoInstance {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn stream(&self, name: StreamKind) -> Result<StreamInstance> {
|
||||
let (instance_tx, instance_rx) = oneshot();
|
||||
self.camera_control
|
||||
.send(NeoCamCommand::Stream(name, instance_tx))
|
||||
.await?;
|
||||
Ok(instance_rx.await?)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) async fn low_stream(&self) -> Result<Option<StreamInstance>> {
|
||||
let (instance_tx, instance_rx) = oneshot();
|
||||
self.camera_control
|
||||
.send(NeoCamCommand::LowStream(instance_tx))
|
||||
.await?;
|
||||
Ok(instance_rx.await?)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) async fn high_stream(&self) -> Result<Option<StreamInstance>> {
|
||||
let (instance_tx, instance_rx) = oneshot();
|
||||
self.camera_control
|
||||
.send(NeoCamCommand::HighStream(instance_tx))
|
||||
.await?;
|
||||
Ok(instance_rx.await?)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) async fn streams(&self) -> Result<Vec<StreamInstance>> {
|
||||
let (instance_tx, instance_rx) = oneshot();
|
||||
self.camera_control
|
||||
.send(NeoCamCommand::Streams(instance_tx))
|
||||
.await?;
|
||||
Ok(instance_rx.await?)
|
||||
}
|
||||
|
||||
#[cfg(feature = "pushnoti")]
|
||||
pub(crate) async fn uid(&self) -> Result<String> {
|
||||
let (reply_tx, reply_rx) = oneshot();
|
||||
self.camera_control
|
||||
@@ -266,6 +242,7 @@ impl NeoInstance {
|
||||
Ok(reply_rx.await?)
|
||||
}
|
||||
|
||||
#[cfg(feature = "pushnoti")]
|
||||
pub(crate) async fn push_notifications(&self) -> Result<WatchReceiver<Option<PushNoti>>> {
|
||||
let uid = self.uid().await?;
|
||||
let (instance_tx, instance_rx) = oneshot();
|
||||
@@ -369,6 +346,202 @@ impl NeoInstance {
|
||||
timeout,
|
||||
}
|
||||
}
|
||||
|
||||
/// Streams a camera source while not paused
|
||||
pub(crate) async fn stream_while_live(
|
||||
&self,
|
||||
stream: StreamKind,
|
||||
) -> AnyResult<MpscReceiver<BcMedia>> {
|
||||
let config = self.config().await?.borrow().clone();
|
||||
let name = config.name.clone();
|
||||
|
||||
let media_rx = if config.pause.on_motion {
|
||||
let (media_tx, media_rx) = tokio::sync::mpsc::channel(100);
|
||||
let counter = UseCounter::new().await;
|
||||
|
||||
let mut md = self.motion().await?;
|
||||
let mut tasks = FuturesUnordered::new();
|
||||
// Stream for 5s on a new client always
|
||||
// This lets us negotiate the camera stream type
|
||||
let init_permit = counter.create_activated().await?;
|
||||
tokio::spawn(
|
||||
async {
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
|
||||
drop(init_permit);
|
||||
}
|
||||
.map(|e| {
|
||||
log::debug!("Init permit thread stopped {e:?}");
|
||||
e
|
||||
}),
|
||||
);
|
||||
|
||||
// Create the permit for controlling the motion
|
||||
let mut md_permit = {
|
||||
let md_state = md.borrow_and_update().clone();
|
||||
match md_state {
|
||||
MdState::Start(_) => {
|
||||
log::info!("{name}::{stream:?}: Starting with Motion");
|
||||
counter.create_activated().await?
|
||||
}
|
||||
MdState::Stop(_) | MdState::Unknown => {
|
||||
log::info!("{name}::{stream:?}: Waiting with Motion");
|
||||
counter.create_deactivated().await?
|
||||
}
|
||||
}
|
||||
};
|
||||
// Now listen to the motion
|
||||
let thread_name = name.clone();
|
||||
tasks.push(tokio::spawn(
|
||||
async move {
|
||||
loop {
|
||||
match md.changed().await {
|
||||
Ok(_) => {
|
||||
let md_state: MdState = md.borrow_and_update().clone();
|
||||
match md_state {
|
||||
MdState::Start(_) => {
|
||||
log::info!("{thread_name}::{stream:?}: Motion Started");
|
||||
md_permit.activate().await?;
|
||||
}
|
||||
MdState::Stop(_) => {
|
||||
log::info!("{thread_name}::{stream:?}: Motion Stopped");
|
||||
md_permit.deactivate().await?;
|
||||
}
|
||||
MdState::Unknown => {}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
// Use break here so we can define the full type on the async closure
|
||||
break AnyResult::Err(e.into());
|
||||
}
|
||||
}
|
||||
}?;
|
||||
AnyResult::Ok(())
|
||||
}
|
||||
.map(|e| {
|
||||
log::debug!("Motion thread stopped {e:?}");
|
||||
e
|
||||
}),
|
||||
));
|
||||
|
||||
#[cfg(pushnoti)]
|
||||
{
|
||||
// Creates a permit for controlling based on the PN
|
||||
let pn_permit = counter.create_deactivated().await?;
|
||||
let mut pn = self.push_notifications().await?;
|
||||
pn.borrow_and_update(); // Ignore any PNs that have already been sent before this
|
||||
let thread_name = name.clone();
|
||||
tasks.push(tokio::spawn(
|
||||
async move {
|
||||
loop {
|
||||
let noti: Option<PushNoti> = pn.borrow_and_update().clone();
|
||||
if let Some(noti) = noti {
|
||||
if noti.message.contains("Motion Alert from") {
|
||||
log::info!(
|
||||
"{thread_name}::{stream:?}: Push Notification Recieved"
|
||||
);
|
||||
let mut new_pn_permit = pn_permit.subscribe();
|
||||
new_pn_permit.activate().await?;
|
||||
tokio::spawn(async move {
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(30))
|
||||
.await;
|
||||
drop(new_pn_permit);
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Err(e) = pn.changed().await {
|
||||
break Err(e);
|
||||
}
|
||||
}?;
|
||||
AnyResult::Ok(())
|
||||
}
|
||||
.map(|e| {
|
||||
log::debug!("PN thread stopped {e:?}");
|
||||
e
|
||||
}),
|
||||
));
|
||||
}
|
||||
|
||||
// Send the camera when the pemit is active
|
||||
let camera_permit = counter.create_deactivated().await?;
|
||||
let thread_camera = self.clone();
|
||||
tokio::spawn(
|
||||
async move {
|
||||
loop {
|
||||
if let Err(e) = camera_permit.aquired_users().await {
|
||||
break AnyResult::Err(e);
|
||||
}
|
||||
log::debug!("Starting stream");
|
||||
tokio::select! {
|
||||
v = camera_permit.dropped_users() => {
|
||||
log::debug!("Dropped users: {v:?}");
|
||||
v
|
||||
},
|
||||
v = async {
|
||||
log::debug!("Getting stream");
|
||||
let mut stream = thread_camera.stream(stream).await?;
|
||||
log::debug!("Got stream");
|
||||
while let Some(media) = stream.recv().await {
|
||||
media_tx.send(media).await?;
|
||||
}
|
||||
AnyResult::Ok(())
|
||||
} => {
|
||||
log::debug!("Stopped stream: {v:?}");
|
||||
v
|
||||
},
|
||||
v = tasks.next() => {
|
||||
log::debug!("Task failed: {v:?}");
|
||||
Err(anyhow!("Task ended prematurly: {v:?}"))
|
||||
}
|
||||
}?;
|
||||
log::debug!("Pausing stream");
|
||||
}?;
|
||||
drop(counter); // Make sure counter is owned by this thread
|
||||
AnyResult::Ok(())
|
||||
}
|
||||
.map(|e| {
|
||||
log::debug!("Stream thread stopped {e:?}");
|
||||
e
|
||||
}),
|
||||
);
|
||||
|
||||
Ok(media_rx)
|
||||
} else {
|
||||
self.stream(stream).await
|
||||
}?;
|
||||
|
||||
Ok(media_rx)
|
||||
}
|
||||
|
||||
/// Streams a camera source
|
||||
pub(crate) async fn stream(&self, stream: StreamKind) -> AnyResult<MpscReceiver<BcMedia>> {
|
||||
let (media_tx, media_rx) = tokio::sync::mpsc::channel(100);
|
||||
let config = self.config().await?.borrow().clone();
|
||||
let strict = config.strict;
|
||||
let thread_camera = self.clone();
|
||||
tokio::task::spawn(
|
||||
tokio::task::spawn(async move {
|
||||
thread_camera
|
||||
.run_task(move |cam| {
|
||||
let media_tx = media_tx.clone();
|
||||
Box::pin(async move {
|
||||
let mut media_stream = cam.start_video(stream, 0, strict).await?;
|
||||
log::trace!("Camera started");
|
||||
while let Ok(media) = media_stream.get_data().await? {
|
||||
media_tx.send(media).await?;
|
||||
}
|
||||
AnyResult::Ok(())
|
||||
})
|
||||
})
|
||||
.await
|
||||
})
|
||||
.and_then(|res| async move {
|
||||
log::debug!("Camera finished streaming: {res:?}");
|
||||
Ok(())
|
||||
}),
|
||||
);
|
||||
|
||||
Ok(media_rx)
|
||||
}
|
||||
}
|
||||
|
||||
// A task that is run on a camera when the structure is dropped
|
||||
|
||||
@@ -17,8 +17,9 @@ use super::NeoInstance;
|
||||
use crate::{AnyResult, Result};
|
||||
use neolink_core::bc_protocol::MotionStatus;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) enum MdState {
|
||||
#[allow(dead_code)]
|
||||
Start(Instant),
|
||||
Stop(Instant),
|
||||
Unknown,
|
||||
|
||||
+2
-2
@@ -2,16 +2,16 @@ mod camthread;
|
||||
mod instance;
|
||||
mod mdthread;
|
||||
mod neocam;
|
||||
#[cfg(feature = "pushnoti")]
|
||||
mod pushnoti;
|
||||
mod reactor;
|
||||
mod streamthread;
|
||||
mod usecounter;
|
||||
|
||||
pub(crate) use camthread::*;
|
||||
pub(crate) use instance::*;
|
||||
pub(crate) use mdthread::*;
|
||||
pub(crate) use neocam::*;
|
||||
#[cfg(feature = "pushnoti")]
|
||||
pub(crate) use pushnoti::*;
|
||||
pub(crate) use reactor::*;
|
||||
pub(crate) use streamthread::*;
|
||||
pub(crate) use usecounter::*;
|
||||
+61
-100
@@ -20,26 +20,25 @@ use tokio_stream::wrappers::ReceiverStream;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use super::{
|
||||
MdRequest, MdState, NeoCamMdThread, NeoCamStreamThread, NeoCamThread, NeoCamThreadState,
|
||||
NeoInstance, Permit, PnRequest, PushNoti, StreamInstance, StreamRequest, UseCounter,
|
||||
MdRequest, MdState, NeoCamMdThread, NeoCamThread, NeoCamThreadState, NeoInstance, Permit,
|
||||
UseCounter,
|
||||
};
|
||||
#[cfg(feature = "pushnoti")]
|
||||
use super::{PnRequest, PushNoti};
|
||||
use crate::{config::CameraConfig, AnyResult, Result};
|
||||
use neolink_core::bc_protocol::{BcCamera, StreamKind};
|
||||
|
||||
use neolink_core::bc_protocol::BcCamera;
|
||||
#[cfg(feature = "gstreamer")]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) enum NeoCamCommand {
|
||||
HangUp,
|
||||
Instance(OneshotSender<Result<NeoInstance>>),
|
||||
Stream(StreamKind, OneshotSender<StreamInstance>),
|
||||
HighStream(OneshotSender<Option<StreamInstance>>),
|
||||
LowStream(OneshotSender<Option<StreamInstance>>),
|
||||
Streams(OneshotSender<Vec<StreamInstance>>),
|
||||
Motion(OneshotSender<WatchReceiver<MdState>>),
|
||||
Config(OneshotSender<WatchReceiver<CameraConfig>>),
|
||||
Disconnect(OneshotSender<()>),
|
||||
Connect(OneshotSender<()>),
|
||||
State(OneshotSender<NeoCamThreadState>),
|
||||
GetPermit(OneshotSender<Permit>),
|
||||
#[cfg(feature = "pushnoti")]
|
||||
PushNoti(OneshotSender<WatchReceiver<Option<PushNoti>>>),
|
||||
GetUid(OneshotSender<String>),
|
||||
}
|
||||
@@ -55,12 +54,11 @@ pub(crate) struct NeoCam {
|
||||
impl NeoCam {
|
||||
pub(crate) async fn new(
|
||||
config: CameraConfig,
|
||||
pn_request_tx: MpscSender<PnRequest>,
|
||||
#[cfg(feature = "pushnoti")] pn_request_tx: MpscSender<PnRequest>,
|
||||
) -> Result<NeoCam> {
|
||||
let (commander_tx, commander_rx) = mpsc(100);
|
||||
let (watch_config_tx, watch_config_rx) = watch(config.clone());
|
||||
let (camera_watch_tx, camera_watch_rx) = watch(Weak::new());
|
||||
let (stream_request_tx, stream_request_rx) = mpsc(100);
|
||||
let (md_request_tx, md_request_rx) = mpsc(100);
|
||||
let (state_tx, state_rx) = watch(NeoCamThreadState::Connected);
|
||||
let (uid_tx, uid_rx) = watch(config.camera_uid.clone());
|
||||
@@ -83,10 +81,12 @@ impl NeoCam {
|
||||
// other threads
|
||||
let sender_cancel = me.cancel.clone();
|
||||
let mut commander_rx = ReceiverStream::new(commander_rx);
|
||||
let strict = config.strict;
|
||||
#[cfg(feature = "gstreamer")]
|
||||
let thread_commander_tx = commander_tx.clone();
|
||||
let thread_watch_config_rx = watch_config_rx.clone();
|
||||
#[cfg(feature = "pushnoti")]
|
||||
let thread_pn_request_tx = pn_request_tx.clone();
|
||||
|
||||
me.set.spawn(async move {
|
||||
let thread_cancel = sender_cancel.clone();
|
||||
let res = tokio::select! {
|
||||
@@ -108,36 +108,6 @@ impl NeoCam {
|
||||
);
|
||||
let _ = result.send(instance);
|
||||
}
|
||||
NeoCamCommand::Stream(name, sender) => {
|
||||
stream_request_tx.send(
|
||||
StreamRequest::GetOrInsert {
|
||||
name,
|
||||
sender,
|
||||
strict,
|
||||
}
|
||||
).await?;
|
||||
},
|
||||
NeoCamCommand::HighStream(sender) => {
|
||||
stream_request_tx.send(
|
||||
StreamRequest::High {
|
||||
sender,
|
||||
}
|
||||
).await?;
|
||||
},
|
||||
NeoCamCommand::LowStream(sender) => {
|
||||
stream_request_tx.send(
|
||||
StreamRequest::Low {
|
||||
sender,
|
||||
}
|
||||
).await?;
|
||||
},
|
||||
NeoCamCommand::Streams(sender) => {
|
||||
stream_request_tx.send(
|
||||
StreamRequest::All {
|
||||
sender,
|
||||
}
|
||||
).await?;
|
||||
},
|
||||
NeoCamCommand::Motion(sender) => {
|
||||
md_request_tx.send(
|
||||
MdRequest::Get {
|
||||
@@ -166,6 +136,7 @@ impl NeoCam {
|
||||
NeoCamCommand::GetPermit(sender) => {
|
||||
let _ = sender.send(users.create_activated().await?);
|
||||
}
|
||||
#[cfg(feature = "pushnoti")]
|
||||
NeoCamCommand::PushNoti(sender) => {
|
||||
thread_pn_request_tx.send(
|
||||
PnRequest::Get {
|
||||
@@ -211,19 +182,6 @@ impl NeoCam {
|
||||
.await;
|
||||
me.set.spawn(async move { cam_thread.run().await });
|
||||
|
||||
// This thread maintains the streams
|
||||
let stream_instance = instance.subscribe().await?;
|
||||
let stream_cancel = me.cancel.clone();
|
||||
let mut stream_thread = NeoCamStreamThread::new(stream_request_rx, stream_instance).await?;
|
||||
me.set.spawn(async move {
|
||||
tokio::select! {
|
||||
_ = stream_cancel.cancelled() => AnyResult::Ok(()),
|
||||
v = stream_thread.run() => {
|
||||
v
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
// This thread monitors the motion
|
||||
let md_instance = instance.subscribe().await?;
|
||||
let md_cancel = me.cancel.clone();
|
||||
@@ -292,54 +250,57 @@ impl NeoCam {
|
||||
});
|
||||
|
||||
// Handles push notifications
|
||||
let pn_root_instance = instance.subscribe().await?;
|
||||
let pn_cancel = me.cancel.clone();
|
||||
let thread_pn_request_tx = pn_request_tx.clone();
|
||||
me.set.spawn(async move {
|
||||
tokio::select!{
|
||||
_ = pn_cancel.cancelled() => {
|
||||
AnyResult::Ok(())
|
||||
},
|
||||
v = async {
|
||||
let mut config_rx = pn_root_instance.config().await?;
|
||||
loop {
|
||||
// Wait for the green light
|
||||
config_rx.wait_for(|config| config.push_notifications).await?;
|
||||
#[cfg(feature = "pushnoti")]
|
||||
{
|
||||
let pn_root_instance = instance.subscribe().await?;
|
||||
let pn_cancel = me.cancel.clone();
|
||||
let thread_pn_request_tx = pn_request_tx.clone();
|
||||
me.set.spawn(async move {
|
||||
tokio::select!{
|
||||
_ = pn_cancel.cancelled() => {
|
||||
AnyResult::Ok(())
|
||||
},
|
||||
v = async {
|
||||
let mut config_rx = pn_root_instance.config().await?;
|
||||
loop {
|
||||
// Wait for the green light
|
||||
config_rx.wait_for(|config| config.push_notifications).await?;
|
||||
|
||||
// Activate it
|
||||
let pn_instance = pn_root_instance.subscribe().await?;
|
||||
let (tx, rx) = oneshot();
|
||||
thread_pn_request_tx.send(PnRequest::Activate{sender: tx, instance: pn_instance}).await?;
|
||||
rx.await??;
|
||||
// Activate it
|
||||
let pn_instance = pn_root_instance.subscribe().await?;
|
||||
let (tx, rx) = oneshot();
|
||||
thread_pn_request_tx.send(PnRequest::Activate{sender: tx, instance: pn_instance}).await?;
|
||||
rx.await??;
|
||||
|
||||
let pn_permit_instance = pn_root_instance.subscribe().await?;
|
||||
let r = tokio::select! {
|
||||
// Push notification permits
|
||||
v = async {
|
||||
let mut prev_noti = None;
|
||||
let mut pn = pn_permit_instance.push_notifications().await?;
|
||||
loop{
|
||||
prev_noti = pn.wait_for(|noti| noti != &prev_noti && noti.is_some()).await.map(|noti| noti.clone())?;
|
||||
let _permit = pn_permit_instance.permit().await?;
|
||||
sleep(Duration::from_secs(30)).await; // Push notification will wake us up for 30s
|
||||
}
|
||||
} => v,
|
||||
// Continue loop on Red light
|
||||
v = config_rx.wait_for(|config| !config.push_notifications).map_ok(|_| ()) => {
|
||||
v?;
|
||||
AnyResult::Ok(())
|
||||
},
|
||||
};
|
||||
if r.is_err() {
|
||||
break r;
|
||||
}
|
||||
}?;
|
||||
AnyResult::Ok(())
|
||||
} => {
|
||||
v
|
||||
},
|
||||
}
|
||||
});
|
||||
let pn_permit_instance = pn_root_instance.subscribe().await?;
|
||||
let r = tokio::select! {
|
||||
// Push notification permits
|
||||
v = async {
|
||||
let mut prev_noti = None;
|
||||
let mut pn = pn_permit_instance.push_notifications().await?;
|
||||
loop{
|
||||
prev_noti = pn.wait_for(|noti| noti != &prev_noti && noti.is_some()).await.map(|noti| noti.clone())?;
|
||||
let _permit = pn_permit_instance.permit().await?;
|
||||
sleep(Duration::from_secs(30)).await; // Push notification will wake us up for 30s
|
||||
}
|
||||
} => v,
|
||||
// Continue loop on Red light
|
||||
v = config_rx.wait_for(|config| !config.push_notifications).map_ok(|_| ()) => {
|
||||
v?;
|
||||
AnyResult::Ok(())
|
||||
},
|
||||
};
|
||||
if r.is_err() {
|
||||
break r;
|
||||
}
|
||||
}?;
|
||||
AnyResult::Ok(())
|
||||
} => {
|
||||
v
|
||||
},
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// MD permits
|
||||
let md_permit_instance = instance.subscribe().await?;
|
||||
|
||||
+36
-10
@@ -5,6 +5,7 @@
|
||||
|
||||
use anyhow::Context;
|
||||
use fcm_push_listener::*;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::{fs, sync::Arc};
|
||||
use tokio::{
|
||||
sync::{
|
||||
@@ -21,8 +22,8 @@ use crate::AnyResult;
|
||||
|
||||
pub(crate) struct PushNotiThread {
|
||||
pn_watcher: Arc<WatchSender<Option<PushNoti>>>,
|
||||
registed_cameras: Vec<NeoInstance>,
|
||||
received_ids: Vec<String>,
|
||||
registed_cameras: HashMap<String, NeoInstance>,
|
||||
received_ids: Arc<RwLock<HashSet<String>>>,
|
||||
}
|
||||
|
||||
// The push notification
|
||||
@@ -51,8 +52,8 @@ impl PushNotiThread {
|
||||
|
||||
Ok(PushNotiThread {
|
||||
pn_watcher: Arc::new(pn_watcher),
|
||||
registed_cameras: vec![],
|
||||
received_ids: vec![],
|
||||
registed_cameras: Default::default(),
|
||||
received_ids: Arc::new(RwLock::new(Default::default())),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -74,6 +75,31 @@ impl PushNotiThread {
|
||||
// let vapid_key = "????";
|
||||
|
||||
let token_path = dirs::config_dir().map(|mut d| {
|
||||
fs::create_dir(&d)
|
||||
.map_or_else(
|
||||
|res| {
|
||||
if let std::io::ErrorKind::AlreadyExists = res.kind() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(res)
|
||||
}
|
||||
},
|
||||
Ok,
|
||||
)
|
||||
.expect("Unable to create directory for push notification settings: {d:?}");
|
||||
d.push("neolink");
|
||||
fs::create_dir(&d)
|
||||
.map_or_else(
|
||||
|res| {
|
||||
if let std::io::ErrorKind::AlreadyExists = res.kind() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(res)
|
||||
}
|
||||
},
|
||||
Ok,
|
||||
)
|
||||
.expect("Unable to create directory for push notification settings: {d:?}");
|
||||
d.push("./neolink_token.toml");
|
||||
d
|
||||
});
|
||||
@@ -121,7 +147,7 @@ impl PushNotiThread {
|
||||
log::debug!("Push notification Listening");
|
||||
let thread_pn_watcher = self.pn_watcher.clone();
|
||||
|
||||
for instance in self.registed_cameras.iter() {
|
||||
for (_, instance) in self.registed_cameras.iter() {
|
||||
let uid = uid.clone();
|
||||
let fcm_token = fcm_token.clone();
|
||||
let instance = instance.clone();
|
||||
@@ -145,7 +171,7 @@ impl PushNotiThread {
|
||||
});
|
||||
}
|
||||
|
||||
let received_ids = Arc::new(RwLock::new(self.received_ids.clone()));
|
||||
let received_ids = self.received_ids.clone();
|
||||
tokio::select! {
|
||||
v = async {
|
||||
loop {
|
||||
@@ -162,7 +188,7 @@ impl PushNotiThread {
|
||||
id: message.persistent_id,
|
||||
}));
|
||||
},
|
||||
received_ids.read().await.clone(),
|
||||
received_ids.read().await.iter().cloned().collect(),
|
||||
);
|
||||
let r = timeout(Duration::from_secs(60*5), listener.connect()).await;
|
||||
match &r {
|
||||
@@ -207,7 +233,7 @@ impl PushNotiThread {
|
||||
PnRequest::Activate{instance, sender} => {
|
||||
let uid = uid.clone();
|
||||
let fcm_token = fcm_token.clone();
|
||||
self.registed_cameras.push(instance.clone());
|
||||
self.registed_cameras.insert(uid.clone(), instance.clone());
|
||||
tokio::task::spawn(async move {
|
||||
let r = instance.run_task(|camera| {
|
||||
let fcm_token = fcm_token.clone();
|
||||
@@ -227,8 +253,8 @@ impl PushNotiThread {
|
||||
});
|
||||
}
|
||||
PnRequest::AddPushID{id} => {
|
||||
self.received_ids.push(id.clone());
|
||||
received_ids.write().await.push(id);
|
||||
log::trace!("Recived Push Notifcation of ID: {id}");
|
||||
received_ids.write().await.insert(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+36
-25
@@ -5,6 +5,8 @@ use std::{
|
||||
collections::{hash_map::Entry, HashMap},
|
||||
sync::Arc,
|
||||
};
|
||||
#[cfg(feature = "pushnoti")]
|
||||
use tokio::time::{sleep, Duration};
|
||||
use tokio::{
|
||||
sync::{
|
||||
mpsc::{channel as mpsc, Sender as MpscSender},
|
||||
@@ -12,12 +14,13 @@ use tokio::{
|
||||
watch::{channel as watch, Receiver as WatchReceiver},
|
||||
},
|
||||
task::JoinSet,
|
||||
time::{sleep, Duration},
|
||||
};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use super::{NeoCam, NeoInstance};
|
||||
use crate::{common::PushNotiThread, config::Config, AnyResult, Result};
|
||||
#[cfg(feature = "pushnoti")]
|
||||
use crate::common::PushNotiThread;
|
||||
use crate::{config::Config, AnyResult, Result};
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
enum NeoReactorCommand {
|
||||
@@ -38,7 +41,9 @@ pub(crate) struct NeoReactor {
|
||||
impl NeoReactor {
|
||||
pub(crate) async fn new(config: Config) -> Self {
|
||||
let (commad_tx, mut command_rx) = mpsc(100);
|
||||
#[cfg(feature = "pushnoti")]
|
||||
let (push_noti, mut pn_rx) = mpsc(10);
|
||||
#[cfg(feature = "pushnoti")]
|
||||
let pn_tx = push_noti.clone();
|
||||
let cancel = CancellationToken::new();
|
||||
let (config_tx, _) = watch(config);
|
||||
@@ -73,7 +78,10 @@ impl NeoReactor {
|
||||
Entry::Vacant(vac) => {
|
||||
let current_config: Config = (*thread_config_tx.borrow()).clone();
|
||||
if let Some(config) = current_config.cameras.iter().find(|cam| cam.name == name).cloned() {
|
||||
#[cfg(feature = "pushnoti")]
|
||||
let cam = NeoCam::new(config, push_noti.clone()).await?;
|
||||
#[cfg(not(feature = "pushnoti"))]
|
||||
let cam = NeoCam::new(config).await?;
|
||||
Result::Ok(Some(
|
||||
vac.insert(
|
||||
cam,
|
||||
@@ -113,31 +121,34 @@ impl NeoReactor {
|
||||
});
|
||||
|
||||
// Push notification client
|
||||
let cancel1 = cancel.clone();
|
||||
let mut thread_config_rx = config_tx.subscribe();
|
||||
set.spawn(async move {
|
||||
let r = tokio::select! {
|
||||
_ = cancel1.cancelled() => AnyResult::Ok(()),
|
||||
v = async {
|
||||
#[cfg(feature = "pushnoti")]
|
||||
{
|
||||
let cancel1 = cancel.clone();
|
||||
let mut thread_config_rx = config_tx.subscribe();
|
||||
set.spawn(async move {
|
||||
let r = tokio::select! {
|
||||
_ = cancel1.cancelled() => AnyResult::Ok(()),
|
||||
v = async {
|
||||
|
||||
let mut pn = PushNotiThread::new().await?;
|
||||
loop {
|
||||
thread_config_rx.wait_for(|c| c.cameras.iter().any(|cam| cam.push_notifications)).await?; // Wait until PN are enabled
|
||||
let r = tokio::select!{
|
||||
v = pn.run(&pn_tx, &mut pn_rx) => {v},
|
||||
_ = thread_config_rx.wait_for(|c| c.cameras.iter().all(|cam| !cam.push_notifications)) => AnyResult::Ok(()), // Quit if PN is turned off
|
||||
};
|
||||
if r.is_err() {
|
||||
log::debug!("Issue with push notifier: {r:?}");
|
||||
sleep(Duration::from_secs(5)).await;
|
||||
} else {
|
||||
log::debug!("Push notifier reported normal shutdown. Restarting");
|
||||
let mut pn = PushNotiThread::new().await?;
|
||||
loop {
|
||||
thread_config_rx.wait_for(|c| c.cameras.iter().any(|cam| cam.push_notifications)).await?; // Wait until PN are enabled
|
||||
let r = tokio::select!{
|
||||
v = pn.run(&pn_tx, &mut pn_rx) => {v},
|
||||
_ = thread_config_rx.wait_for(|c| c.cameras.iter().all(|cam| !cam.push_notifications)) => AnyResult::Ok(()), // Quit if PN is turned off
|
||||
};
|
||||
if r.is_err() {
|
||||
log::debug!("Issue with push notifier: {r:?}");
|
||||
sleep(Duration::from_secs(5)).await;
|
||||
} else {
|
||||
log::debug!("Push notifier reported normal shutdown. Restarting");
|
||||
}
|
||||
}
|
||||
}
|
||||
} => v,
|
||||
};
|
||||
r
|
||||
});
|
||||
} => v,
|
||||
};
|
||||
r
|
||||
});
|
||||
}
|
||||
|
||||
Self {
|
||||
cancel,
|
||||
|
||||
@@ -1,684 +0,0 @@
|
||||
//! This thread will start and stop the camera
|
||||
//!
|
||||
//! If there are no listeners to the broadcast
|
||||
//! then it will hangup
|
||||
|
||||
use futures::stream::{FuturesUnordered, StreamExt};
|
||||
use std::{
|
||||
collections::{hash_map::Entry, HashMap, VecDeque},
|
||||
sync::Arc,
|
||||
};
|
||||
use tokio::{
|
||||
sync::{
|
||||
broadcast::{
|
||||
channel as broadcast, Receiver as BroadcastReceiver, Sender as BroadcastSender,
|
||||
},
|
||||
mpsc::{channel as mpsc, Receiver as MpscReceiver},
|
||||
oneshot::{channel as oneshot, Sender as OneshotSender},
|
||||
watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender},
|
||||
},
|
||||
task::JoinHandle,
|
||||
time::{sleep, timeout, Duration},
|
||||
};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use super::{NeoInstance, Permit, UseCounter};
|
||||
use crate::{AnyResult, Result};
|
||||
use neolink_core::{bc_protocol::StreamKind, bcmedia::model::*};
|
||||
|
||||
pub(crate) struct NeoCamStreamThread {
|
||||
streams: HashMap<StreamKind, StreamData>,
|
||||
stream_request_rx: MpscReceiver<StreamRequest>,
|
||||
cancel: CancellationToken,
|
||||
instance: NeoInstance,
|
||||
}
|
||||
|
||||
impl NeoCamStreamThread {
|
||||
pub(crate) async fn new(
|
||||
stream_request_rx: MpscReceiver<StreamRequest>,
|
||||
instance: NeoInstance,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
streams: Default::default(),
|
||||
stream_request_rx,
|
||||
cancel: CancellationToken::new(),
|
||||
instance,
|
||||
})
|
||||
}
|
||||
pub(crate) async fn run(&mut self) -> Result<()> {
|
||||
let thread_cancel = self.cancel.clone();
|
||||
tokio::select! {
|
||||
_ = thread_cancel.cancelled() => {
|
||||
for (_, mut data) in self.streams.drain() {
|
||||
let _ = data.shutdown().await;
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
v = async {
|
||||
while let Some(request) = self.stream_request_rx.recv().await {
|
||||
match request {
|
||||
StreamRequest::Get {
|
||||
name, sender
|
||||
} => {
|
||||
if let Entry::Occupied(occ) = self.streams.entry(name) {
|
||||
let _ = sender.send(Some(
|
||||
StreamInstance::new(occ.get()).await?));
|
||||
} else {
|
||||
let _ = sender.send(None);
|
||||
}
|
||||
},
|
||||
StreamRequest::GetOrInsert {
|
||||
name, sender, strict
|
||||
} => {
|
||||
match self.streams.entry(name) {
|
||||
Entry::Occupied(occ) => {
|
||||
let _ = sender
|
||||
.send(StreamInstance::new(occ.get()).await?);
|
||||
}
|
||||
Entry::Vacant(vac) => {
|
||||
// Make a new streaming instance
|
||||
|
||||
let data = StreamData::new(
|
||||
name,
|
||||
self.instance.subscribe().await?,
|
||||
strict,
|
||||
).await?;
|
||||
let data = vac.insert(data);
|
||||
|
||||
let _ = sender.send(StreamInstance::new(data).await?);
|
||||
}
|
||||
}
|
||||
},
|
||||
StreamRequest::High {
|
||||
sender
|
||||
} => {
|
||||
let mut result = None;
|
||||
let mut streams = vec![
|
||||
StreamKind::Main,
|
||||
StreamKind::Extern,
|
||||
StreamKind::Sub,
|
||||
];
|
||||
let config = self.instance.config().await?.borrow().clone();
|
||||
let config_streams = config.stream.as_stream_kinds();
|
||||
for name in streams.drain(..) {
|
||||
if config_streams.contains(&name) {
|
||||
// Fill it in
|
||||
if let Entry::Vacant(vac) = self.streams.entry(name) {
|
||||
vac.insert(
|
||||
StreamData::new(name, self.instance.subscribe().await?, config.strict)
|
||||
.await?,
|
||||
);
|
||||
}
|
||||
|
||||
// Grab it
|
||||
if let Entry::Occupied(occ) = self.streams.entry(name) {
|
||||
result = Some(
|
||||
StreamInstance::new(occ.get()).await?);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = sender.send(result);
|
||||
},
|
||||
StreamRequest::Low {
|
||||
sender
|
||||
} => {
|
||||
let mut result = None;
|
||||
let mut streams = vec![
|
||||
StreamKind::Sub,
|
||||
StreamKind::Extern,
|
||||
StreamKind::Main,
|
||||
];
|
||||
let config = self.instance.config().await?.borrow().clone();
|
||||
let config_streams = config.stream.as_stream_kinds();
|
||||
for name in streams.drain(..) {
|
||||
if config_streams.contains(&name) {
|
||||
// Fill it in
|
||||
if let Entry::Vacant(vac) = self.streams.entry(name) {
|
||||
vac.insert(
|
||||
StreamData::new(name, self.instance.subscribe().await?, config.strict)
|
||||
.await?,
|
||||
);
|
||||
}
|
||||
|
||||
// Grab it
|
||||
if let Entry::Occupied(occ) = self.streams.entry(name) {
|
||||
result = Some(
|
||||
StreamInstance::new(occ.get()).await?
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = sender.send(result);
|
||||
},
|
||||
StreamRequest::All {
|
||||
sender
|
||||
} => {
|
||||
let config = self.instance.config().await?.borrow_and_update().clone();
|
||||
let streams = config.stream.as_stream_kinds();
|
||||
for stream in streams.iter().copied() {
|
||||
if let Entry::Vacant(vac) = self.streams.entry(stream) {
|
||||
vac.insert(
|
||||
StreamData::new(stream, self.instance.subscribe().await?, config.strict)
|
||||
.await?,
|
||||
);
|
||||
}
|
||||
}
|
||||
let mut streams = self.streams.iter_mut().filter_map(|(name, stream)| if streams.contains(name) {
|
||||
Some(async move {
|
||||
StreamInstance::new(stream).await
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
).collect::<FuturesUnordered<_>>().collect::<Vec<_>>().await;
|
||||
let _ = sender.send(streams.drain(..).flatten().collect());
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
} => v,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for NeoCamStreamThread {
|
||||
fn drop(&mut self) {
|
||||
self.cancel.cancel();
|
||||
for stream in self.streams.values() {
|
||||
stream.cancel.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The kind of stream we want a async broadcast of
|
||||
pub(crate) enum StreamRequest {
|
||||
#[allow(dead_code)]
|
||||
/// Get a currently loaded stream
|
||||
Get {
|
||||
name: StreamKind,
|
||||
sender: OneshotSender<Option<StreamInstance>>,
|
||||
},
|
||||
/// Get or Insert a stream
|
||||
GetOrInsert {
|
||||
name: StreamKind,
|
||||
sender: OneshotSender<StreamInstance>,
|
||||
strict: bool,
|
||||
},
|
||||
/// Get highest available stream. Which this is depends on what is
|
||||
/// disabled
|
||||
High {
|
||||
sender: OneshotSender<Option<StreamInstance>>,
|
||||
},
|
||||
/// Get lowest quality available stream. Which this is depends on what is
|
||||
/// disabled
|
||||
Low {
|
||||
sender: OneshotSender<Option<StreamInstance>>,
|
||||
},
|
||||
/// Get all streams configured in the config
|
||||
All {
|
||||
sender: OneshotSender<Vec<StreamInstance>>,
|
||||
},
|
||||
}
|
||||
|
||||
/// The data of a running stream
|
||||
pub(crate) struct StreamData {
|
||||
vid: BroadcastSender<StampedData>,
|
||||
aud: BroadcastSender<StampedData>,
|
||||
vid_history: Arc<WatchSender<VecDeque<StampedData>>>,
|
||||
aud_history: Arc<WatchSender<VecDeque<StampedData>>>,
|
||||
config: Arc<WatchSender<StreamConfig>>,
|
||||
name: StreamKind,
|
||||
instance: NeoInstance,
|
||||
cancel: CancellationToken,
|
||||
handle: Option<JoinHandle<Result<()>>>,
|
||||
strict: bool,
|
||||
users: UseCounter,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Debug, Copy)]
|
||||
pub(crate) enum VidFormat {
|
||||
None,
|
||||
H264,
|
||||
H265,
|
||||
}
|
||||
#[derive(Eq, PartialEq, Clone, Debug, Copy)]
|
||||
pub(crate) enum AudFormat {
|
||||
None,
|
||||
Aac,
|
||||
Adpcm(u32),
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Debug)]
|
||||
pub(crate) struct StreamConfig {
|
||||
pub(crate) resolution: [u32; 2],
|
||||
pub(crate) vid_format: VidFormat,
|
||||
pub(crate) aud_format: AudFormat,
|
||||
pub(crate) bitrate: u32,
|
||||
pub(crate) fps: u32,
|
||||
}
|
||||
|
||||
impl StreamConfig {
|
||||
pub(crate) fn vid_ready(&self) -> bool {
|
||||
self.resolution[0] > 0
|
||||
&& self.resolution[1] > 0
|
||||
&& self.bitrate > 0
|
||||
&& !matches!(self.vid_format, VidFormat::None)
|
||||
}
|
||||
|
||||
pub(crate) fn aud_ready(&self) -> bool {
|
||||
self.vid_ready() && !matches!(self.aud_format, AudFormat::None)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct StampedData {
|
||||
pub(crate) keyframe: bool,
|
||||
pub(crate) data: Arc<Vec<u8>>,
|
||||
pub(crate) ts: Duration,
|
||||
}
|
||||
|
||||
pub(crate) struct StreamInstance {
|
||||
#[allow(dead_code)]
|
||||
pub(crate) name: StreamKind,
|
||||
pub(crate) vid: BroadcastReceiver<StampedData>,
|
||||
pub(crate) vid_history: WatchReceiver<VecDeque<StampedData>>,
|
||||
pub(crate) aud: BroadcastReceiver<StampedData>,
|
||||
pub(crate) aud_history: WatchReceiver<VecDeque<StampedData>>,
|
||||
pub(crate) config: WatchReceiver<StreamConfig>,
|
||||
in_use: Permit,
|
||||
}
|
||||
|
||||
impl StreamInstance {
|
||||
pub async fn new(data: &StreamData) -> Result<Self> {
|
||||
Ok(Self {
|
||||
name: data.name,
|
||||
vid: data.vid.subscribe(),
|
||||
vid_history: data.vid_history.subscribe(),
|
||||
aud: data.aud.subscribe(),
|
||||
aud_history: data.aud_history.subscribe(),
|
||||
config: data.config.subscribe(),
|
||||
in_use: data.users.create_activated().await?,
|
||||
})
|
||||
}
|
||||
pub(crate) async fn activate(&mut self) -> Result<()> {
|
||||
self.in_use.activate().await
|
||||
}
|
||||
pub(crate) async fn deactivate(&mut self) -> Result<()> {
|
||||
self.in_use.deactivate().await
|
||||
}
|
||||
|
||||
pub(crate) async fn activator_handle(&mut self) -> Permit {
|
||||
self.in_use.subscribe()
|
||||
}
|
||||
}
|
||||
|
||||
impl StreamData {
|
||||
async fn new(name: StreamKind, instance: NeoInstance, strict: bool) -> Result<Self> {
|
||||
const BUFFER_DURATION: Duration = Duration::from_secs(3);
|
||||
// At 30fps for 15s with audio is is about 900 frames
|
||||
const BUFFER_SIZE: usize = 30usize * BUFFER_DURATION.as_millis() as usize / 1000usize;
|
||||
let (vid, _) = broadcast::<StampedData>(BUFFER_SIZE);
|
||||
let (aud, _) = broadcast::<StampedData>(BUFFER_SIZE);
|
||||
let (vid_history, _) = watch::<VecDeque<StampedData>>(VecDeque::new());
|
||||
let vid_history = Arc::new(vid_history);
|
||||
let (aud_history, _) = watch::<VecDeque<StampedData>>(VecDeque::new());
|
||||
let aud_history = Arc::new(aud_history);
|
||||
let (resolution, bitrate, fps, fps_table) = instance
|
||||
.run_passive_task(|cam| {
|
||||
Box::pin(async move {
|
||||
let infos = cam
|
||||
.get_stream_info()
|
||||
.await?
|
||||
.stream_infos
|
||||
.iter()
|
||||
.flat_map(|info| info.encode_tables.clone())
|
||||
.collect::<Vec<_>>();
|
||||
if let Some(encode) =
|
||||
infos.iter().find(|encode| encode.name == name.to_string())
|
||||
{
|
||||
let bitrate_table = encode
|
||||
.bitrate_table
|
||||
.split(',')
|
||||
.filter_map(|c| {
|
||||
let i: Result<u32, _> = c.parse();
|
||||
i.ok()
|
||||
})
|
||||
.collect::<Vec<u32>>();
|
||||
let framerate_table = encode
|
||||
.framerate_table
|
||||
.split(',')
|
||||
.filter_map(|c| {
|
||||
let i: Result<u32, _> = c.parse();
|
||||
i.ok()
|
||||
})
|
||||
.collect::<Vec<u32>>();
|
||||
|
||||
Ok((
|
||||
[encode.resolution.width, encode.resolution.height],
|
||||
bitrate_table
|
||||
.get(encode.default_bitrate as usize)
|
||||
.copied()
|
||||
.unwrap_or(encode.default_bitrate)
|
||||
* 1024,
|
||||
framerate_table
|
||||
.get(encode.default_framerate as usize)
|
||||
.copied()
|
||||
.unwrap_or(encode.default_framerate),
|
||||
framerate_table.clone(),
|
||||
))
|
||||
} else {
|
||||
Ok(([0, 0], 0, 0, vec![]))
|
||||
}
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
let (config_tx, _) = watch(StreamConfig {
|
||||
resolution,
|
||||
vid_format: VidFormat::None,
|
||||
aud_format: AudFormat::None,
|
||||
bitrate,
|
||||
fps,
|
||||
});
|
||||
let mut me = Self {
|
||||
name,
|
||||
cancel: CancellationToken::new(),
|
||||
config: Arc::new(config_tx),
|
||||
vid,
|
||||
vid_history,
|
||||
aud,
|
||||
aud_history,
|
||||
instance,
|
||||
handle: None,
|
||||
strict,
|
||||
users: UseCounter::new().await,
|
||||
};
|
||||
|
||||
let cancel = me.cancel.clone();
|
||||
let vid = me.vid.clone();
|
||||
let aud = me.aud.clone();
|
||||
let instance = me.instance.subscribe().await?;
|
||||
let name = me.name;
|
||||
let cam_name = instance.config().await?.borrow().name.clone();
|
||||
let print_name = format!("{cam_name}::{name}");
|
||||
let strict = me.strict;
|
||||
let config = me.config.clone();
|
||||
let thread_inuse = me.users.create_deactivated().await?;
|
||||
let vid_history = me.vid_history.clone();
|
||||
let aud_history = me.aud_history.clone();
|
||||
let mut permit = instance.permit().await?;
|
||||
|
||||
// Rather than extract the time stamp from the frame data we
|
||||
// create a new one from the fps
|
||||
// this makes it easier to play the frames at a constant rate
|
||||
// even across reconnects (since reconnects restart the ts otherwise)
|
||||
let mut master_ts = Duration::ZERO;
|
||||
let mut fps_delta = Duration::from_millis(1000 / (config.borrow().fps as u64));
|
||||
|
||||
me.handle = Some(tokio::task::spawn(async move {
|
||||
let r = tokio::select! {
|
||||
_ = cancel.cancelled() => {
|
||||
Result::<(), anyhow::Error>::Ok(())
|
||||
},
|
||||
v = async {
|
||||
loop {
|
||||
let (watchdog_tx, mut watchdog_rx) = mpsc(1);
|
||||
let (watchdog_eat_tx, watchdog_eat_rx) = oneshot();
|
||||
// Give the watchdog his own thread to play in
|
||||
//
|
||||
// This should stop one branch of the select from waking the other
|
||||
// too often
|
||||
tokio::task::spawn(async move {
|
||||
let mut check_timeout = timeout(Duration::from_secs(15), watchdog_rx.recv()).await; // Wait longer for the first feed
|
||||
loop {
|
||||
match check_timeout {
|
||||
Err(_) => {
|
||||
// Timeout
|
||||
// Break with Ok to trigger the restart
|
||||
break;
|
||||
},
|
||||
Ok(None) => {
|
||||
break;
|
||||
}
|
||||
Ok(_) => {
|
||||
check_timeout = timeout(Duration::from_secs(10), watchdog_rx.recv()).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Watch dog is hungry send the kill to the stream thread
|
||||
let _ = watchdog_eat_tx.send(());
|
||||
}) ;
|
||||
|
||||
tokio::select! {
|
||||
v = thread_inuse.dropped_users() => {
|
||||
// Handles the stop and restart when no active users
|
||||
permit.deactivate().await?;
|
||||
v?;
|
||||
thread_inuse.aquired_users().await?; // Wait for new users of the stream
|
||||
permit.activate().await?;
|
||||
AnyResult::Ok(())
|
||||
},
|
||||
_ = watchdog_eat_rx => {
|
||||
sleep(Duration::from_secs(1)).await;
|
||||
AnyResult::Ok(())
|
||||
},
|
||||
result = instance.run_passive_task(|camera| {
|
||||
let vid_tx = vid.clone();
|
||||
let aud_tx = aud.clone();
|
||||
let stream_config = config.clone();
|
||||
let vid_history = vid_history.clone();
|
||||
let aud_history = aud_history.clone();
|
||||
let watchdog_tx = watchdog_tx.clone();
|
||||
let fps_table = fps_table.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
// use std::io::Write;
|
||||
// let mut file = std::fs::File::create("reference.h264")?;
|
||||
let mut recieved_iframe = false;
|
||||
let mut aud_keyframe = false;
|
||||
|
||||
let res = async {
|
||||
let mut stream_data = camera.start_video(name, 0, strict).await?;
|
||||
loop {
|
||||
let data = stream_data.get_data().await??;
|
||||
watchdog_tx.send(()).await?; // Feed the watchdog
|
||||
|
||||
// Update the stream config with any information
|
||||
match &data {
|
||||
BcMedia::InfoV1(info) => {
|
||||
stream_config.send_if_modified(|state| {
|
||||
let new_fps = fps_table.get(info.fps as usize).copied().unwrap_or(info.fps as u32);
|
||||
if state.resolution[0] != info.video_width || state.resolution[1] != info.video_height || new_fps != state.fps {
|
||||
state.resolution[0] = info.video_width;
|
||||
state.resolution[1] = info.video_height;
|
||||
state.fps = new_fps;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
fps_delta = Duration::from_millis(1000 / (stream_config.borrow().fps as u64));
|
||||
},
|
||||
BcMedia::InfoV2(info) => {
|
||||
stream_config.send_if_modified(|state| {
|
||||
let new_fps = fps_table.get(info.fps as usize).copied().unwrap_or(info.fps as u32);
|
||||
if state.resolution[0] != info.video_width || state.resolution[1] != info.video_height || new_fps != state.fps {
|
||||
state.resolution[0] = info.video_width;
|
||||
state.resolution[1] = info.video_height;
|
||||
state.fps = new_fps;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
fps_delta = Duration::from_millis(1000 / (stream_config.borrow().fps as u64));
|
||||
},
|
||||
BcMedia::Iframe(frame) => {
|
||||
stream_config.send_if_modified(|state| {
|
||||
let expected = match frame.video_type {
|
||||
VideoType::H264 => VidFormat::H264,
|
||||
VideoType::H265 => VidFormat::H265,
|
||||
};
|
||||
if state.vid_format != expected {
|
||||
state.vid_format = expected;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
// let _ = file.write(&frame.data);
|
||||
}
|
||||
BcMedia::Pframe(frame) => {
|
||||
stream_config.send_if_modified(|state| {
|
||||
let expected = match frame.video_type {
|
||||
VideoType::H264 => VidFormat::H264,
|
||||
VideoType::H265 => VidFormat::H265,
|
||||
};
|
||||
if state.vid_format != expected {
|
||||
state.vid_format = expected;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
// let _ = file.write(&frame.data);
|
||||
},
|
||||
BcMedia::Aac(_) => {
|
||||
stream_config.send_if_modified(|state| {
|
||||
if state.aud_format != AudFormat::Aac {
|
||||
state.aud_format = AudFormat::Aac;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
}
|
||||
BcMedia::Adpcm(aud) => {
|
||||
stream_config.send_if_modified(|state| {
|
||||
let expected = AudFormat::Adpcm(aud.data.len() as u32 - 4);
|
||||
if state.aud_format != expected {
|
||||
state.aud_format = expected;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
match data {
|
||||
BcMedia::Iframe(BcMediaIframe{data, ..}) => {
|
||||
let d = StampedData{
|
||||
keyframe: true,
|
||||
data: Arc::new(data),
|
||||
ts: master_ts,
|
||||
};
|
||||
let _ = vid_tx.send(d.clone());
|
||||
vid_history.send_modify(|history| {
|
||||
let drop_time = d.ts.saturating_sub(BUFFER_DURATION);
|
||||
let dts = d.ts;
|
||||
history.push_back(d);
|
||||
while history.front().is_some_and(|di| di.ts < drop_time || di.ts > dts) || history.len() > BUFFER_SIZE {
|
||||
history.pop_front();
|
||||
}
|
||||
log::info!("history: {}", history.len());
|
||||
let debug: Vec<Duration> = history.iter().map(|f| f.ts).collect();
|
||||
log::info!("history ts: {:?}", debug);
|
||||
});
|
||||
recieved_iframe = true;
|
||||
aud_keyframe = true;
|
||||
log::trace!("Sent Vid Key Frame");
|
||||
master_ts += fps_delta;
|
||||
},
|
||||
BcMedia::Pframe(BcMediaPframe{data, ..}) if recieved_iframe => {
|
||||
let d = StampedData{
|
||||
keyframe: false,
|
||||
data: Arc::new(data),
|
||||
ts: master_ts,
|
||||
};
|
||||
let _ = vid_tx.send(d.clone());
|
||||
vid_history.send_modify(|history| {
|
||||
let drop_time = d.ts.saturating_sub(BUFFER_DURATION);
|
||||
let dts = d.ts;
|
||||
history.push_back(d);
|
||||
while history.front().is_some_and(|di| di.ts < drop_time || di.ts > dts) || history.len() > BUFFER_SIZE {
|
||||
history.pop_front();
|
||||
}
|
||||
});
|
||||
master_ts += fps_delta;
|
||||
log::trace!("Sent Vid Frame");
|
||||
}
|
||||
BcMedia::Aac(BcMediaAac{data, ..}) | BcMedia::Adpcm(BcMediaAdpcm{data,..}) if recieved_iframe => {
|
||||
let d = StampedData{
|
||||
keyframe: aud_keyframe,
|
||||
data: Arc::new(data),
|
||||
ts: master_ts,
|
||||
};
|
||||
aud_keyframe = false;
|
||||
let _ = aud_tx.send(d.clone())?;
|
||||
aud_history.send_modify(|history| {
|
||||
let drop_time = d.ts.saturating_sub(BUFFER_DURATION);
|
||||
let dts = d.ts;
|
||||
history.push_back(d);
|
||||
while history.front().is_some_and(|di| di.ts < drop_time || di.ts > dts) || history.len() > BUFFER_SIZE {
|
||||
history.pop_front();
|
||||
}
|
||||
});
|
||||
log::trace!("Sent Aud Frame");
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
}.await;
|
||||
log::info!("video outgoing queue: {}", vid_tx.len());
|
||||
Ok(res)
|
||||
})
|
||||
}) => {
|
||||
match result {
|
||||
Ok(AnyResult::Ok(())) => {
|
||||
log::debug!("{print_name}: Video Stream Stopped due to no listeners");
|
||||
break Ok(());
|
||||
},
|
||||
Ok(Err(e)) => {
|
||||
log::debug!("{print_name}: Video Stream Restarting Due to Error: {:?}", e);
|
||||
AnyResult::Ok(())
|
||||
},
|
||||
Err(e) => {
|
||||
log::debug!("{print_name}: Video Stream Stopped Due to Instance Error: {:?}", e);
|
||||
break Err(e);
|
||||
},
|
||||
}
|
||||
},
|
||||
}?;
|
||||
}
|
||||
} => v,
|
||||
};
|
||||
r
|
||||
}));
|
||||
|
||||
Ok(me)
|
||||
}
|
||||
|
||||
async fn shutdown(&mut self) -> Result<()> {
|
||||
self.cancel.cancel();
|
||||
if let Some(handle) = self.handle.take() {
|
||||
let _ = handle.await;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for StreamData {
|
||||
fn drop(&mut self) {
|
||||
log::trace!("Drop StreamData");
|
||||
self.cancel.cancel();
|
||||
if let Some(h) = self.handle.take() {
|
||||
let _gt = tokio::runtime::Handle::current().enter();
|
||||
tokio::task::spawn(async move {
|
||||
let _ = h.await;
|
||||
log::trace!("Dropped StreamData");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,8 @@ impl UseCounter {
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
#[cfg(feature = "gstreamer")]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) async fn create_deactivated(&self) -> Result<Permit> {
|
||||
Ok(Permit::new(self))
|
||||
}
|
||||
@@ -87,6 +89,8 @@ pub(crate) struct Permit {
|
||||
}
|
||||
|
||||
impl Permit {
|
||||
#[cfg(feature = "gstreamer")]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn subscribe(&self) -> Self {
|
||||
Self {
|
||||
is_active: false,
|
||||
@@ -141,6 +145,8 @@ impl Permit {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "gstreamer")]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn get_counter(&self) -> WatchReceiver<u32> {
|
||||
self.value.clone()
|
||||
}
|
||||
|
||||
+29
-19
@@ -1,12 +1,14 @@
|
||||
use crate::mqtt::Discoveries;
|
||||
use neolink_core::bc_protocol::{DiscoveryMethods, PrintFormat, StreamKind};
|
||||
#[cfg(feature = "gstreamer")]
|
||||
use neolink_core::bc_protocol::StreamKind;
|
||||
use neolink_core::bc_protocol::{DiscoveryMethods, PrintFormat};
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::clone::Clone;
|
||||
use std::collections::HashSet;
|
||||
use validator::Validate;
|
||||
use validator::ValidationError;
|
||||
use validator_derive::Validate;
|
||||
|
||||
static RE_TLS_CLIENT_AUTH: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(r"^(none|request|require)$").unwrap());
|
||||
@@ -17,7 +19,7 @@ static RE_MAXENC_SRC: Lazy<Regex> = Lazy::new(|| {
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq)]
|
||||
pub(crate) struct Config {
|
||||
#[validate]
|
||||
#[validate(nested)]
|
||||
pub(crate) cameras: Vec<CameraConfig>,
|
||||
|
||||
#[serde(rename = "bind", default = "default_bind_addr")]
|
||||
@@ -44,7 +46,7 @@ pub(crate) struct Config {
|
||||
#[serde(default = "default_tls_client_auth")]
|
||||
pub(crate) tls_client_auth: String,
|
||||
|
||||
#[validate]
|
||||
#[validate(nested)]
|
||||
#[serde(default)]
|
||||
pub(crate) users: Vec<UserConfig>,
|
||||
}
|
||||
@@ -57,13 +59,13 @@ pub(crate) struct MqttServerConfig {
|
||||
|
||||
pub(crate) port: u16,
|
||||
|
||||
#[serde(default)]
|
||||
#[serde(default, skip_serializing)]
|
||||
pub(crate) credentials: Option<(String, String)>,
|
||||
|
||||
#[serde(default)]
|
||||
#[serde(default, skip_serializing)]
|
||||
pub(crate) ca: Option<std::path::PathBuf>,
|
||||
|
||||
#[serde(default)]
|
||||
#[serde(default, skip_serializing)]
|
||||
pub(crate) client_auth: Option<(std::path::PathBuf, std::path::PathBuf)>,
|
||||
}
|
||||
|
||||
@@ -99,6 +101,7 @@ pub(crate) enum StreamConfig {
|
||||
}
|
||||
|
||||
impl StreamConfig {
|
||||
#[cfg(feature = "gstreamer")]
|
||||
pub(crate) fn as_stream_kinds(&self) -> Vec<StreamKind> {
|
||||
match self {
|
||||
StreamConfig::All => {
|
||||
@@ -135,6 +138,8 @@ pub(crate) struct CameraConfig {
|
||||
pub(crate) camera_uid: Option<String>,
|
||||
|
||||
pub(crate) username: String,
|
||||
|
||||
#[serde(alias = "pass", skip_serializing, default)]
|
||||
pub(crate) password: Option<String>,
|
||||
|
||||
#[serde(default = "default_stream")]
|
||||
@@ -146,11 +151,11 @@ pub(crate) struct CameraConfig {
|
||||
#[serde(default = "default_channel_id", alias = "channel")]
|
||||
pub(crate) channel_id: u8,
|
||||
|
||||
#[validate]
|
||||
#[validate(nested)]
|
||||
#[serde(default = "default_mqtt")]
|
||||
pub(crate) mqtt: MqttConfig,
|
||||
|
||||
#[validate]
|
||||
#[validate(nested)]
|
||||
#[serde(default = "default_pause")]
|
||||
pub(crate) pause: PauseConfig,
|
||||
|
||||
@@ -176,13 +181,18 @@ pub(crate) struct CameraConfig {
|
||||
pub(crate) update_time: bool,
|
||||
|
||||
#[validate(range(
|
||||
min = 0,
|
||||
max = 500,
|
||||
message = "Invalid buffer size",
|
||||
code = "buffer_size"
|
||||
min = 1,
|
||||
max = 15000,
|
||||
message = "Invalid buffer duration (it's in ms)",
|
||||
code = "buffer_duration"
|
||||
))]
|
||||
#[serde(default = "default_buffer_size", alias = "size", alias = "buffer")]
|
||||
pub(crate) buffer_size: usize,
|
||||
/// Buffer duration in ms
|
||||
#[serde(
|
||||
default = "default_buffer_duration",
|
||||
alias = "duration",
|
||||
alias = "buffer"
|
||||
)]
|
||||
pub(crate) buffer_duration: u64,
|
||||
|
||||
#[serde(default = "default_true", alias = "enable")]
|
||||
pub(crate) enabled: bool,
|
||||
@@ -216,8 +226,8 @@ pub(crate) struct UserConfig {
|
||||
#[serde(alias = "username")]
|
||||
pub(crate) name: String,
|
||||
|
||||
#[serde(alias = "password")]
|
||||
pub(crate) pass: String,
|
||||
#[serde(alias = "password", skip_serializing, default)]
|
||||
pub(crate) pass: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, Validate, PartialEq, Eq)]
|
||||
@@ -488,8 +498,8 @@ fn default_pause() -> PauseConfig {
|
||||
}
|
||||
}
|
||||
|
||||
fn default_buffer_size() -> usize {
|
||||
25
|
||||
fn default_buffer_duration() -> u64 {
|
||||
3000
|
||||
}
|
||||
|
||||
fn default_max_discovery_retries() -> usize {
|
||||
|
||||
+6
-6
@@ -5,6 +5,7 @@ use gstreamer::{
|
||||
parse::launch_full, prelude::*, ClockTime, MessageView, ParseFlags, Pipeline, State,
|
||||
};
|
||||
use gstreamer_app::AppSrc;
|
||||
use neolink_core::bcmedia::model::VideoType;
|
||||
use tokio::{
|
||||
sync::{
|
||||
self,
|
||||
@@ -14,7 +15,7 @@ use tokio::{
|
||||
};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::{common::VidFormat, AnyResult};
|
||||
use crate::AnyResult;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum GstControl {
|
||||
@@ -74,7 +75,7 @@ impl Drop for GstSender {
|
||||
}
|
||||
|
||||
pub(super) async fn from_input<T: AsRef<Path>>(
|
||||
format: VidFormat,
|
||||
format: VideoType,
|
||||
out_file: T,
|
||||
) -> Result<GstSender> {
|
||||
let pipeline = create_pipeline(format, out_file.as_ref())?;
|
||||
@@ -171,13 +172,13 @@ fn get_source(pipeline: &Pipeline) -> Result<AppSrc> {
|
||||
.map_err(|_| anyhow!("Cannot find appsource in gstreamer, check your gstreamer plugins"))
|
||||
}
|
||||
|
||||
fn create_pipeline(format: VidFormat, file_path: &Path) -> Result<Pipeline> {
|
||||
fn create_pipeline(format: VideoType, file_path: &Path) -> Result<Pipeline> {
|
||||
gstreamer::init()
|
||||
.context("Unable to start gstreamer ensure it and all plugins are installed")?;
|
||||
let file_path = file_path.with_extension("jpeg");
|
||||
|
||||
let launch_str = match format {
|
||||
VidFormat::H264 => {
|
||||
VideoType::H264 => {
|
||||
format!(
|
||||
"appsrc name=thesource \
|
||||
! h264parse \
|
||||
@@ -187,7 +188,7 @@ fn create_pipeline(format: VidFormat, file_path: &Path) -> Result<Pipeline> {
|
||||
file_path.display()
|
||||
)
|
||||
}
|
||||
VidFormat::H265 => {
|
||||
VideoType::H265 => {
|
||||
format!(
|
||||
"appsrc name=thesource \
|
||||
! h265parse \
|
||||
@@ -197,7 +198,6 @@ fn create_pipeline(format: VidFormat, file_path: &Path) -> Result<Pipeline> {
|
||||
file_path.display()
|
||||
)
|
||||
}
|
||||
VidFormat::None => unreachable!(),
|
||||
};
|
||||
|
||||
log::info!("{}", launch_str);
|
||||
|
||||
+59
-40
@@ -17,17 +17,19 @@
|
||||
/// neolink image --config=config.toml --use_stream --file-path=filepath CameraName
|
||||
/// ```
|
||||
///
|
||||
use anyhow::{Context, Result};
|
||||
use futures::stream::StreamExt;
|
||||
use anyhow::{anyhow, Result};
|
||||
use log::*;
|
||||
use neolink_core::bc_protocol::*;
|
||||
use tokio::{fs::File, io::AsyncWriteExt};
|
||||
use tokio_stream::wrappers::BroadcastStream;
|
||||
use neolink_core::{
|
||||
bc_protocol::*,
|
||||
bcmedia::model::{BcMedia, BcMediaIframe, BcMediaPframe},
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use tokio::{fs::File, io::AsyncWriteExt, sync::RwLock};
|
||||
|
||||
mod cmdline;
|
||||
mod gst;
|
||||
|
||||
use crate::common::{NeoReactor, StampedData};
|
||||
use crate::common::NeoReactor;
|
||||
pub(crate) use cmdline::Opt;
|
||||
|
||||
/// Entry point for the image subcommand
|
||||
@@ -37,48 +39,65 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
|
||||
let camera = reactor.get(&opt.camera).await?;
|
||||
|
||||
if opt.use_stream {
|
||||
let stream_data = camera
|
||||
.stream(StreamKind::Main)
|
||||
.await
|
||||
.context("Failed to start video")?;
|
||||
let (stream_data_tx, mut stream_data_rx) = tokio::sync::mpsc::channel(100);
|
||||
|
||||
// Get one iframe at the start while also getting the the video type
|
||||
let mut stream_config = stream_data.config.clone();
|
||||
let vid_type = stream_config
|
||||
.wait_for(|config| config.vid_ready())
|
||||
.await?
|
||||
.vid_format;
|
||||
let mut stream = BroadcastStream::new(stream_data.vid.resubscribe())
|
||||
.filter(|f| futures::future::ready(f.is_ok())); // Filter to ignore lagged
|
||||
let buf = loop {
|
||||
if let Some(Ok(StampedData {
|
||||
data,
|
||||
ts: _,
|
||||
keyframe,
|
||||
})) = stream.next().await
|
||||
{
|
||||
if keyframe {
|
||||
break data;
|
||||
}
|
||||
}
|
||||
};
|
||||
// Spawn a video stream
|
||||
let thread_camera = camera.clone();
|
||||
let (stream_type_tx, stream_type_rx) = tokio::sync::oneshot::channel();
|
||||
let stream_type_tx = Arc::new(RwLock::new(Some(stream_type_tx)));
|
||||
tokio::task::spawn(async move {
|
||||
thread_camera
|
||||
.run_task(|cam| {
|
||||
let stream_type_tx = stream_type_tx.clone();
|
||||
let stream_data_tx = stream_data_tx.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
let mut stream = cam.start_video(StreamKind::Main, 100, false).await?;
|
||||
while let Ok(frame) = stream.get_data().await {
|
||||
let frame = frame?;
|
||||
match frame {
|
||||
BcMedia::Iframe(BcMediaIframe {
|
||||
data, video_type, ..
|
||||
})
|
||||
| BcMedia::Pframe(BcMediaPframe {
|
||||
data, video_type, ..
|
||||
}) => {
|
||||
if let Some(stream_type_tx) =
|
||||
stream_type_tx.write().await.take()
|
||||
{
|
||||
let _ = stream_type_tx.send(video_type);
|
||||
}
|
||||
stream_data_tx.send(Arc::new(data)).await?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Result::Ok(())
|
||||
})
|
||||
})
|
||||
.await
|
||||
});
|
||||
|
||||
let vid_type = stream_type_rx.await?;
|
||||
let buf = stream_data_rx
|
||||
.recv()
|
||||
.await
|
||||
.ok_or(anyhow!("No frames recieved"))?;
|
||||
|
||||
let mut sender = gst::from_input(vid_type, &opt.file_path).await?;
|
||||
sender.send(buf).await?; // Send first iframe
|
||||
|
||||
// Keep sending both IFrame or PFrame until finished
|
||||
while sender.is_finished().await.is_none() {
|
||||
let buf = match stream.next().await {
|
||||
Some(Ok(StampedData { data, .. })) => data,
|
||||
_ => {
|
||||
continue;
|
||||
if let Some(buf) = stream_data_rx.recv().await {
|
||||
debug!("Sending frame data to gstreamer");
|
||||
if sender.send(buf).await.is_err() {
|
||||
// Assume that the sender is closed
|
||||
// because the pipeline is finished
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
debug!("Sending frame data to gstreamer");
|
||||
if sender.send(buf).await.is_err() {
|
||||
// Assume that the sender is closed
|
||||
// because the pipeline is finished
|
||||
} else {
|
||||
log::error!("Camera stopped sending frames before decoding could complete");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+18
-1
@@ -40,14 +40,17 @@ mod battery;
|
||||
mod cmdline;
|
||||
mod common;
|
||||
mod config;
|
||||
#[cfg(feature = "gstreamer")]
|
||||
mod image;
|
||||
mod mqtt;
|
||||
mod pir;
|
||||
mod ptz;
|
||||
mod reboot;
|
||||
#[cfg(feature = "gstreamer")]
|
||||
mod rtsp;
|
||||
mod services;
|
||||
mod statusled;
|
||||
#[cfg(feature = "gstreamer")]
|
||||
mod talk;
|
||||
mod utils;
|
||||
|
||||
@@ -83,13 +86,24 @@ async fn main() -> Result<()> {
|
||||
let neo_reactor = NeoReactor::new(config.clone()).await;
|
||||
|
||||
match opt.cmd {
|
||||
#[cfg(feature = "gstreamer")]
|
||||
None => {
|
||||
warn!(
|
||||
"Deprecated command line option. Please use: `neolink rtsp --config={:?}`",
|
||||
config
|
||||
conf_path
|
||||
);
|
||||
rtsp::main(rtsp::Opt {}, neo_reactor.clone()).await?;
|
||||
}
|
||||
#[cfg(not(feature = "gstreamer"))]
|
||||
None => {
|
||||
// When gstreamer is disabled the default command is MQTT
|
||||
warn!(
|
||||
"Deprecated command line option. Please use: `neolink mqtt --config={:?}`",
|
||||
conf_path
|
||||
);
|
||||
mqtt::main(mqtt::Opt {}, neo_reactor.clone()).await?;
|
||||
}
|
||||
#[cfg(feature = "gstreamer")]
|
||||
Some(Command::Rtsp(opts)) => {
|
||||
rtsp::main(opts, neo_reactor.clone()).await?;
|
||||
}
|
||||
@@ -105,18 +119,21 @@ async fn main() -> Result<()> {
|
||||
Some(Command::Ptz(opts)) => {
|
||||
ptz::main(opts, neo_reactor.clone()).await?;
|
||||
}
|
||||
#[cfg(feature = "gstreamer")]
|
||||
Some(Command::Talk(opts)) => {
|
||||
talk::main(opts, neo_reactor.clone()).await?;
|
||||
}
|
||||
Some(Command::Mqtt(opts)) => {
|
||||
mqtt::main(opts, neo_reactor.clone()).await?;
|
||||
}
|
||||
#[cfg(feature = "gstreamer")]
|
||||
Some(Command::MqttRtsp(opts)) => {
|
||||
tokio::select! {
|
||||
v = mqtt::main(opts, neo_reactor.clone()) => v,
|
||||
v = rtsp::main(rtsp::Opt {}, neo_reactor.clone()) => v,
|
||||
}?;
|
||||
}
|
||||
#[cfg(feature = "gstreamer")]
|
||||
Some(Command::Image(opts)) => {
|
||||
image::main(opts, neo_reactor.clone()).await?;
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
use std::collections::HashSet;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
// Handles app wide things like aborting all connections
|
||||
pub(crate) struct App {
|
||||
aborters: Arc<Mutex<HashSet<String>>>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
aborters: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
// Abort at a level
|
||||
// e.g. about camera_event_loop
|
||||
pub(crate) fn abort(&self, level: &str) {
|
||||
let mut aborters = self.aborters.lock().unwrap();
|
||||
aborters.insert(level.to_string());
|
||||
}
|
||||
|
||||
// Checks if any level has aborted
|
||||
// e.g. app:camera_a::camera_event_loop
|
||||
// will check if app, camera_a or camera_event_loop has been aborted
|
||||
pub(crate) fn running(&self, levels: &str) -> bool {
|
||||
let aborters = self.aborters.lock().unwrap();
|
||||
for level in levels.split(':') {
|
||||
if aborters.contains(&level.to_string()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
+63
-23
@@ -211,7 +211,40 @@ pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> {
|
||||
.await?;
|
||||
continue;
|
||||
}
|
||||
let config = config?;
|
||||
let curr_config = thread_config.borrow().clone();
|
||||
let mut config = config?;
|
||||
|
||||
// Fill in skipped passwords
|
||||
if let (Some(mqtt), Some(curr_mqtt)) = (config.mqtt.as_mut(), curr_config.mqtt.as_ref()) {
|
||||
if mqtt.credentials.is_none() {
|
||||
mqtt.credentials = curr_mqtt.credentials.clone();
|
||||
}
|
||||
if mqtt.ca.is_none() {
|
||||
mqtt.ca = curr_mqtt.ca.clone();
|
||||
}
|
||||
if mqtt.client_auth.is_none() {
|
||||
mqtt.client_auth = curr_mqtt.client_auth.clone();
|
||||
}
|
||||
}
|
||||
for cam in config.cameras.iter_mut() {
|
||||
let name = cam.name.clone();
|
||||
let cur_cam = curr_config.cameras.iter().find(|c| c.name == name);
|
||||
if let Some(cur_cam) = cur_cam.as_ref() {
|
||||
if cam.password.is_none() {
|
||||
cam.password = cur_cam.password.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
for user in config.users.iter_mut() {
|
||||
let name = user.name.clone();
|
||||
let cur_user = curr_config.users.iter().find(|c| c.name == name);
|
||||
if let Some(cur_user) = cur_user.as_ref() {
|
||||
if user.pass.is_none() {
|
||||
user.pass = cur_user.pass.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Passwords should now be restored if they were not set
|
||||
|
||||
let validate = config.validate().with_context(|| {
|
||||
format!("Failed to validate the MQTT {:?} config file", msg.topic)
|
||||
@@ -298,7 +331,9 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
|
||||
let camera_motion = camera.clone();
|
||||
let mqtt_motion = mqtt_instance.resubscribe().await?;
|
||||
|
||||
#[cfg(feature = "pushnoti")]
|
||||
let camera_pn = camera.clone();
|
||||
#[cfg(feature = "pushnoti")]
|
||||
let mqtt_pn = mqtt_instance.resubscribe().await?;
|
||||
|
||||
let camera_snap = camera.clone();
|
||||
@@ -458,7 +493,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
|
||||
}).await;
|
||||
let image = match image {
|
||||
Err(e) => match e.downcast::<neolink_core::Error>() {
|
||||
Ok(neolink_core::Error::CameraServiceUnavailable(_)) => {
|
||||
Ok(neolink_core::Error::CameraServiceUnavailable{..}) => {
|
||||
log::debug!("Image not supported");
|
||||
futures::future::pending().await
|
||||
},
|
||||
@@ -502,7 +537,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
|
||||
}).await;
|
||||
let xml = match xml {
|
||||
Err(e) => match e.downcast::<neolink_core::Error>() {
|
||||
Ok(neolink_core::Error::CameraServiceUnavailable(_)) => {
|
||||
Ok(neolink_core::Error::CameraServiceUnavailable{..}) => {
|
||||
log::debug!("Battery not supported");
|
||||
futures::future::pending().await
|
||||
},
|
||||
@@ -530,27 +565,32 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
|
||||
}, if config.enable_battery => v,
|
||||
// Handle the push notification messages
|
||||
v = async {
|
||||
let mut pn = camera_pn.push_notifications().await?;
|
||||
let mut prev_noti = None;
|
||||
loop {
|
||||
let v = async {
|
||||
let noti = pn.wait_for(|noti| noti != &prev_noti && noti.is_some()).await.with_context(|| {
|
||||
format!("{}: PushNoti Watch Dropped", camera_name)
|
||||
})?.clone();
|
||||
mqtt_pn.send_message("status/notification", ¬i.as_ref().unwrap().message, true).await.with_context(|| {
|
||||
format!("{}: Failed to publish push notification", camera_name)
|
||||
})?;
|
||||
prev_noti = noti;
|
||||
AnyResult::Ok(())
|
||||
}.await;
|
||||
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
|
||||
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
|
||||
Ok(()) => AnyResult::Ok(()),
|
||||
Err(Ok(e)) => Err(e.into()),
|
||||
Err(Err(e)) => Err (e),
|
||||
}?;
|
||||
#[cfg(feature = "pushnoti")]
|
||||
{
|
||||
let mut pn = camera_pn.push_notifications().await?;
|
||||
let mut prev_noti = None;
|
||||
loop {
|
||||
let v = async {
|
||||
let noti = pn.wait_for(|noti| noti != &prev_noti && noti.is_some()).await.with_context(|| {
|
||||
format!("{}: PushNoti Watch Dropped", camera_name)
|
||||
})?.clone();
|
||||
mqtt_pn.send_message("status/notification", ¬i.as_ref().unwrap().message, true).await.with_context(|| {
|
||||
format!("{}: Failed to publish push notification", camera_name)
|
||||
})?;
|
||||
prev_noti = noti;
|
||||
AnyResult::Ok(())
|
||||
}.await;
|
||||
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
|
||||
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
|
||||
Ok(()) => AnyResult::Ok(()),
|
||||
Err(Ok(e)) => Err(e.into()),
|
||||
Err(Err(e)) => Err (e),
|
||||
}?;
|
||||
}
|
||||
}
|
||||
} => v,
|
||||
#[cfg(not(feature = "pushnoti"))]
|
||||
unreachable!()
|
||||
}, if cfg!(feature = "pushnoti") => v,
|
||||
// Handle the floodlight task activation
|
||||
v = async {
|
||||
let flt_status = camera_floodlight_tasks.run_passive_task(|cam| Box::pin(async move {
|
||||
|
||||
+3
-2
@@ -22,6 +22,7 @@ use tokio::{
|
||||
};
|
||||
use tokio_stream::{wrappers::BroadcastStream, StreamExt};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub(crate) struct Mqtt {
|
||||
cancel: CancellationToken,
|
||||
@@ -125,7 +126,7 @@ impl<'a> MqttBackend<'a> {
|
||||
async fn run(&mut self) -> AnyResult<()> {
|
||||
log::trace!("Run MQTT Server");
|
||||
let mut mqttoptions = MqttOptions::new(
|
||||
"Neolink".to_string(),
|
||||
format!("Neolink{}", Uuid::new_v4()),
|
||||
&self.config.broker_addr,
|
||||
self.config.port,
|
||||
);
|
||||
@@ -519,7 +520,7 @@ impl LastWillMqtt {
|
||||
) -> AnyResult<Self> {
|
||||
log::trace!("Run MQTT Last Will");
|
||||
let mut mqttoptions = MqttOptions::new(
|
||||
format!("NeolinkLastWill_{}", topic),
|
||||
format!("NeolinkLastWill_{}_{}", topic, Uuid::new_v4()),
|
||||
&config.broker_addr,
|
||||
config.port,
|
||||
);
|
||||
|
||||
+617
-105
@@ -1,21 +1,130 @@
|
||||
use gstreamer::ClockTime;
|
||||
use std::{collections::HashMap, time::Duration};
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use gstreamer::{prelude::*, Bin, Caps, Element, ElementFactory, GhostPad};
|
||||
use gstreamer::{prelude::*, Bin, Caps, Element, ElementFactory, FlowError, GhostPad};
|
||||
use gstreamer_app::{AppSrc, AppSrcCallbacks, AppStreamType};
|
||||
use tokio::sync::mpsc::{channel as mpsc, Receiver as MpscReceiver};
|
||||
|
||||
use crate::{
|
||||
common::{AudFormat, StreamConfig, VidFormat},
|
||||
rtsp::gst::NeoMediaFactory,
|
||||
AnyResult,
|
||||
use neolink_core::{
|
||||
bc_protocol::StreamKind,
|
||||
bcmedia::model::{
|
||||
BcMedia, BcMediaIframe, BcMediaInfoV1, BcMediaInfoV2, BcMediaPframe, VideoType,
|
||||
},
|
||||
};
|
||||
use tokio::{sync::mpsc::channel as mpsc, task::JoinHandle};
|
||||
|
||||
pub(super) struct ClientSourceData {
|
||||
pub(super) app: AppSrc,
|
||||
use crate::{common::NeoInstance, rtsp::gst::NeoMediaFactory, AnyResult};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum AudioType {
|
||||
Aac,
|
||||
Adpcm(u32),
|
||||
}
|
||||
|
||||
pub(super) struct ClientData {
|
||||
pub(super) vid: Option<ClientSourceData>,
|
||||
pub(super) aud: Option<ClientSourceData>,
|
||||
#[derive(Clone, Debug)]
|
||||
struct StreamConfig {
|
||||
#[allow(dead_code)]
|
||||
resolution: [u32; 2],
|
||||
bitrate: u32,
|
||||
fps: u32,
|
||||
bitrate_table: Vec<u32>,
|
||||
fps_table: Vec<u32>,
|
||||
vid_type: Option<VideoType>,
|
||||
aud_type: Option<AudioType>,
|
||||
}
|
||||
impl StreamConfig {
|
||||
async fn new(instance: &NeoInstance, name: StreamKind) -> AnyResult<Self> {
|
||||
let (resolution, bitrate, fps, fps_table, bitrate_table) = instance
|
||||
.run_passive_task(|cam| {
|
||||
Box::pin(async move {
|
||||
let infos = cam
|
||||
.get_stream_info()
|
||||
.await?
|
||||
.stream_infos
|
||||
.iter()
|
||||
.flat_map(|info| info.encode_tables.clone())
|
||||
.collect::<Vec<_>>();
|
||||
if let Some(encode) =
|
||||
infos.iter().find(|encode| encode.name == name.to_string())
|
||||
{
|
||||
let bitrate_table = encode
|
||||
.bitrate_table
|
||||
.split(',')
|
||||
.filter_map(|c| {
|
||||
let i: Result<u32, _> = c.parse();
|
||||
i.ok()
|
||||
})
|
||||
.collect::<Vec<u32>>();
|
||||
let framerate_table = encode
|
||||
.framerate_table
|
||||
.split(',')
|
||||
.filter_map(|c| {
|
||||
let i: Result<u32, _> = c.parse();
|
||||
i.ok()
|
||||
})
|
||||
.collect::<Vec<u32>>();
|
||||
|
||||
Ok((
|
||||
[encode.resolution.width, encode.resolution.height],
|
||||
bitrate_table
|
||||
.get(encode.default_bitrate as usize)
|
||||
.copied()
|
||||
.unwrap_or(encode.default_bitrate)
|
||||
* 1024,
|
||||
framerate_table
|
||||
.get(encode.default_framerate as usize)
|
||||
.copied()
|
||||
.unwrap_or(encode.default_framerate),
|
||||
framerate_table.clone(),
|
||||
bitrate_table.clone(),
|
||||
))
|
||||
} else {
|
||||
Ok(([0, 0], 0, 0, vec![], vec![]))
|
||||
}
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(StreamConfig {
|
||||
resolution,
|
||||
bitrate,
|
||||
fps,
|
||||
fps_table,
|
||||
bitrate_table,
|
||||
vid_type: None,
|
||||
aud_type: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn update_fps(&mut self, fps: u32) {
|
||||
let new_fps = self.fps_table.get(fps as usize).copied().unwrap_or(fps);
|
||||
self.fps = new_fps;
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
fn update_bitrate(&mut self, bitrate: u32) {
|
||||
let new_bitrate = self
|
||||
.bitrate_table
|
||||
.get(bitrate as usize)
|
||||
.copied()
|
||||
.unwrap_or(bitrate);
|
||||
self.bitrate = new_bitrate;
|
||||
}
|
||||
|
||||
fn update_from_media(&mut self, media: &BcMedia) {
|
||||
match media {
|
||||
BcMedia::InfoV1(BcMediaInfoV1 { fps, .. })
|
||||
| BcMedia::InfoV2(BcMediaInfoV2 { fps, .. }) => self.update_fps(*fps as u32),
|
||||
BcMedia::Aac(_) => {
|
||||
self.aud_type = Some(AudioType::Aac);
|
||||
}
|
||||
BcMedia::Adpcm(adpcm) => {
|
||||
self.aud_type = Some(AudioType::Adpcm(adpcm.block_size()));
|
||||
}
|
||||
BcMedia::Iframe(BcMediaIframe { video_type, .. })
|
||||
| BcMedia::Pframe(BcMediaPframe { video_type, .. }) => {
|
||||
self.vid_type = Some(*video_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn make_dummy_factory(
|
||||
@@ -34,78 +143,314 @@ pub(super) async fn make_dummy_factory(
|
||||
.await
|
||||
}
|
||||
|
||||
enum ClientMsg {
|
||||
NewClient {
|
||||
element: Element,
|
||||
reply: tokio::sync::oneshot::Sender<Element>,
|
||||
},
|
||||
}
|
||||
|
||||
pub(super) async fn make_factory(
|
||||
camera: NeoInstance,
|
||||
stream: StreamKind,
|
||||
) -> AnyResult<(NeoMediaFactory, JoinHandle<AnyResult<()>>)> {
|
||||
let (client_tx, mut client_rx) = mpsc(100);
|
||||
// Create the task that creates the pipelines
|
||||
let thread = tokio::task::spawn(async move {
|
||||
let name = camera.config().await?.borrow().name.clone();
|
||||
|
||||
while let Some(msg) = client_rx.recv().await {
|
||||
match msg {
|
||||
ClientMsg::NewClient { element, reply } => {
|
||||
log::debug!("New client for {name}::{stream}");
|
||||
let camera = camera.clone();
|
||||
let name = name.clone();
|
||||
tokio::task::spawn(async move {
|
||||
clear_bin(&element)?;
|
||||
log::trace!("{name}::{stream}: Starting camera");
|
||||
|
||||
// Start the camera
|
||||
let config = camera.config().await?.borrow().clone();
|
||||
let mut media_rx = camera.stream_while_live(stream).await?;
|
||||
|
||||
log::trace!("{name}::{stream}: Learning camera stream type");
|
||||
// Learn the camera data type
|
||||
let mut buffer = vec![];
|
||||
let mut frame_count = 0usize;
|
||||
|
||||
let mut stream_config = StreamConfig::new(&camera, stream).await?;
|
||||
while let Some(media) = media_rx.recv().await {
|
||||
stream_config.update_from_media(&media);
|
||||
buffer.push(media);
|
||||
if frame_count > 10
|
||||
|| (stream_config.vid_type.is_some()
|
||||
&& stream_config.aud_type.is_some())
|
||||
{
|
||||
break;
|
||||
}
|
||||
frame_count += 1;
|
||||
}
|
||||
|
||||
log::trace!("{name}::{stream}: Building the pipeline");
|
||||
// Build the right video pipeline
|
||||
let vid_src = match stream_config.vid_type.as_ref() {
|
||||
Some(VideoType::H264) => {
|
||||
let src = build_h264(&element, &stream_config)?;
|
||||
AnyResult::Ok(Some(src))
|
||||
}
|
||||
Some(VideoType::H265) => {
|
||||
let src = build_h265(&element, &stream_config)?;
|
||||
AnyResult::Ok(Some(src))
|
||||
}
|
||||
None => {
|
||||
build_unknown(&element, &config.splash_pattern.to_string())?;
|
||||
AnyResult::Ok(None)
|
||||
}
|
||||
}?;
|
||||
|
||||
// Build the right audio pipeline
|
||||
let aud_src = match stream_config.aud_type.as_ref() {
|
||||
Some(AudioType::Aac) => {
|
||||
let src = build_aac(&element, &stream_config)?;
|
||||
AnyResult::Ok(Some(src))
|
||||
}
|
||||
Some(AudioType::Adpcm(block_size)) => {
|
||||
let src = build_adpcm(&element, *block_size, &stream_config)?;
|
||||
AnyResult::Ok(Some(src))
|
||||
}
|
||||
None => AnyResult::Ok(None),
|
||||
}?;
|
||||
|
||||
if let Some(app) = vid_src.as_ref() {
|
||||
app.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.seek_data(move |_, _seek_pos| true)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
if let Some(app) = aud_src.as_ref() {
|
||||
app.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.seek_data(move |_, _seek_pos| true)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
log::trace!("{name}::{stream}: Sending pipeline to gstreamer");
|
||||
// Send the pipeline back to the factory so it can start
|
||||
let _ = reply.send(element);
|
||||
|
||||
// Run blocking code on a seperate thread
|
||||
// This is not an async thread
|
||||
std::thread::spawn(move || {
|
||||
let mut aud_ts = 0u32;
|
||||
let mut vid_ts = 0u32;
|
||||
let mut pools = Default::default();
|
||||
|
||||
log::trace!("{name}::{stream}: Sending buffered frames");
|
||||
for buffered in buffer.drain(..) {
|
||||
send_to_sources(
|
||||
buffered,
|
||||
&mut pools,
|
||||
&vid_src,
|
||||
&aud_src,
|
||||
&mut vid_ts,
|
||||
&mut aud_ts,
|
||||
&stream_config,
|
||||
)?;
|
||||
}
|
||||
|
||||
log::trace!("{name}::{stream}: Sending new frames");
|
||||
while let Some(data) = media_rx.blocking_recv() {
|
||||
let r = send_to_sources(
|
||||
data,
|
||||
&mut pools,
|
||||
&vid_src,
|
||||
&aud_src,
|
||||
&mut vid_ts,
|
||||
&mut aud_ts,
|
||||
&stream_config,
|
||||
);
|
||||
if let Err(r) = &r {
|
||||
log::info!("Failed to send to source: {r:?}");
|
||||
}
|
||||
r?;
|
||||
}
|
||||
log::trace!("All media recieved");
|
||||
AnyResult::Ok(())
|
||||
});
|
||||
AnyResult::Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
AnyResult::Ok(())
|
||||
});
|
||||
|
||||
// Now setup the factory
|
||||
let factory = NeoMediaFactory::new_with_callback(move |element| {
|
||||
let (reply, new_element) = tokio::sync::oneshot::channel();
|
||||
client_tx.blocking_send(ClientMsg::NewClient { element, reply })?;
|
||||
|
||||
let element = new_element.blocking_recv()?;
|
||||
Ok(Some(element))
|
||||
})
|
||||
.await?;
|
||||
Ok((factory, thread))
|
||||
}
|
||||
|
||||
fn send_to_sources(
|
||||
data: BcMedia,
|
||||
pools: &mut HashMap<usize, gstreamer::BufferPool>,
|
||||
vid_src: &Option<AppSrc>,
|
||||
aud_src: &Option<AppSrc>,
|
||||
vid_ts: &mut u32,
|
||||
aud_ts: &mut u32,
|
||||
stream_config: &StreamConfig,
|
||||
) -> AnyResult<(NeoMediaFactory, MpscReceiver<ClientData>)> {
|
||||
let (client_tx, client_rx) = mpsc(100);
|
||||
let factory = {
|
||||
let stream_config = stream_config.clone();
|
||||
) -> AnyResult<()> {
|
||||
// Update TS
|
||||
match data {
|
||||
BcMedia::Aac(aac) => {
|
||||
let duration = aac.duration().expect("Could not calculate AAC duration");
|
||||
if let Some(aud_src) = aud_src.as_ref() {
|
||||
log::debug!("Sending AAC: {:?}", Duration::from_micros(*aud_ts as u64));
|
||||
send_to_appsrc(
|
||||
aud_src,
|
||||
aac.data,
|
||||
Duration::from_micros(*aud_ts as u64),
|
||||
pools,
|
||||
)?;
|
||||
}
|
||||
*aud_ts += duration;
|
||||
}
|
||||
BcMedia::Adpcm(adpcm) => {
|
||||
let duration = adpcm
|
||||
.duration()
|
||||
.expect("Could not calculate ADPCM duration");
|
||||
if let Some(aud_src) = aud_src.as_ref() {
|
||||
log::trace!("Sending ADPCM: {:?}", Duration::from_micros(*aud_ts as u64));
|
||||
send_to_appsrc(
|
||||
aud_src,
|
||||
adpcm.data,
|
||||
Duration::from_micros(*aud_ts as u64),
|
||||
pools,
|
||||
)?;
|
||||
}
|
||||
*aud_ts += duration;
|
||||
}
|
||||
BcMedia::Iframe(BcMediaIframe { data, .. })
|
||||
| BcMedia::Pframe(BcMediaPframe { data, .. }) => {
|
||||
if let Some(vid_src) = vid_src.as_ref() {
|
||||
log::trace!("Sending VID: {:?}", Duration::from_micros(*vid_ts as u64));
|
||||
send_to_appsrc(vid_src, data, Duration::from_micros(*vid_ts as u64), pools)?;
|
||||
}
|
||||
const MICROSECONDS: u32 = 1000000;
|
||||
*vid_ts += MICROSECONDS / stream_config.fps;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
NeoMediaFactory::new_with_callback(move |element| {
|
||||
clear_bin(&element)?;
|
||||
let vid = match stream_config.vid_format {
|
||||
VidFormat::None => {
|
||||
// This should not be reachable
|
||||
log::debug!("Building unknown during normal make factory");
|
||||
build_unknown(&element, "black")?;
|
||||
AnyResult::Ok(None)
|
||||
}
|
||||
VidFormat::H264 => {
|
||||
let app = build_h264(&element, &stream_config)?;
|
||||
app.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.seek_data(move |_, _seek_pos| true)
|
||||
.build(),
|
||||
);
|
||||
AnyResult::Ok(Some(app))
|
||||
}
|
||||
VidFormat::H265 => {
|
||||
let app = build_h265(&element, &stream_config)?;
|
||||
fn send_to_appsrc(
|
||||
appsrc: &AppSrc,
|
||||
data: Vec<u8>,
|
||||
mut ts: Duration,
|
||||
pools: &mut HashMap<usize, gstreamer::BufferPool>,
|
||||
) -> AnyResult<()> {
|
||||
check_live(appsrc)?; // Stop if appsrc is dropped
|
||||
|
||||
app.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.seek_data(move |_, _seek_pos| true)
|
||||
.build(),
|
||||
);
|
||||
AnyResult::Ok(Some(app))
|
||||
}
|
||||
}?;
|
||||
let aud = if matches!(stream_config.vid_format, VidFormat::None) {
|
||||
None
|
||||
// In live mode we follow the advice in
|
||||
// https://gstreamer.freedesktop.org/documentation/additional/design/element-source.html?gi-language=c#live-sources
|
||||
// Only push buffers when in play state and have a clock
|
||||
// we also timestamp at the current time
|
||||
if appsrc.is_live() {
|
||||
if let Some(time) = appsrc
|
||||
.current_clock_time()
|
||||
.and_then(|t| appsrc.base_time().map(|bt| t - bt))
|
||||
{
|
||||
if matches!(appsrc.current_state(), gstreamer::State::Playing) {
|
||||
ts = Duration::from_micros(time.useconds());
|
||||
} else {
|
||||
match stream_config.aud_format {
|
||||
AudFormat::None => AnyResult::Ok(None),
|
||||
AudFormat::Aac => {
|
||||
let app = build_aac(&element, &stream_config)?;
|
||||
app.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.seek_data(move |_, _seek_pos| true)
|
||||
.build(),
|
||||
);
|
||||
AnyResult::Ok(Some(app))
|
||||
}
|
||||
AudFormat::Adpcm(block_size) => {
|
||||
let app = build_adpcm(&element, block_size, &stream_config)?;
|
||||
app.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.seek_data(move |_, _seek_pos| true)
|
||||
.build(),
|
||||
);
|
||||
AnyResult::Ok(Some(app))
|
||||
}
|
||||
}?
|
||||
};
|
||||
// Not playing
|
||||
return Ok(());
|
||||
}
|
||||
} else {
|
||||
// Clock not up yet
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
let buf = {
|
||||
let msg_size = data.len();
|
||||
|
||||
client_tx.blocking_send(ClientData {
|
||||
vid: vid.map(|app| ClientSourceData { app }),
|
||||
aud: aud.map(|app| ClientSourceData { app }),
|
||||
})?;
|
||||
Ok(Some(element))
|
||||
})
|
||||
.await
|
||||
// Get or create a pool of this len
|
||||
let pool = pools.entry(msg_size).or_insert_with_key(|size| {
|
||||
let pool = gstreamer::BufferPool::new();
|
||||
let mut pool_config = pool.config();
|
||||
// Set a max buffers to ensure we don't grow in memory endlessly
|
||||
pool_config.set_params(None, (*size) as u32, 8, 32);
|
||||
pool.set_config(pool_config).unwrap();
|
||||
pool.set_active(true).unwrap();
|
||||
pool
|
||||
});
|
||||
|
||||
// Get a buffer from the pool and then copy in the data
|
||||
let gst_buf = {
|
||||
let mut new_buf = pool.acquire_buffer(None).unwrap();
|
||||
let gst_buf_mut = new_buf.get_mut().unwrap();
|
||||
let time = ClockTime::from_useconds(ts.as_micros() as u64);
|
||||
gst_buf_mut.set_dts(time);
|
||||
gst_buf_mut.set_pts(time);
|
||||
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
|
||||
gst_buf_data.copy_from_slice(data.as_slice());
|
||||
drop(gst_buf_data);
|
||||
new_buf
|
||||
};
|
||||
|
||||
// Return the new buffer with the data
|
||||
gst_buf
|
||||
};
|
||||
|
||||
// Push buffer into the appsrc
|
||||
match appsrc.push_buffer(buf) {
|
||||
Ok(_) => {
|
||||
// log::info!(
|
||||
// "Send {}{} on {}",
|
||||
// data.data.len(),
|
||||
// if data.keyframe { " (keyframe)" } else { "" },
|
||||
// appsrc.name()
|
||||
// );
|
||||
Ok(())
|
||||
}
|
||||
Err(FlowError::Flushing) => {
|
||||
// Buffer is full just skip
|
||||
log::info!(
|
||||
"Buffer full on {} pausing stream until client consumes frames",
|
||||
appsrc.name()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => Err(anyhow!("Error in streaming: {e:?}")),
|
||||
}?;
|
||||
|
||||
Ok((factory, client_rx))
|
||||
// Check if we need to pause
|
||||
if appsrc.current_level_bytes() >= appsrc.max_bytes() * 2 / 3
|
||||
&& matches!(appsrc.current_state(), gstreamer::State::Paused)
|
||||
{
|
||||
appsrc.set_state(gstreamer::State::Playing).unwrap();
|
||||
} else if appsrc.current_level_bytes() <= appsrc.max_bytes() / 3
|
||||
&& matches!(appsrc.current_state(), gstreamer::State::Playing)
|
||||
{
|
||||
appsrc.set_state(gstreamer::State::Paused).unwrap();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn check_live(app: &AppSrc) -> Result<()> {
|
||||
app.bus().ok_or(anyhow!("App source is closed"))?;
|
||||
app.pads()
|
||||
.iter()
|
||||
.all(|pad| pad.is_linked())
|
||||
.then_some(())
|
||||
.ok_or(anyhow!("App source is not linked"))
|
||||
}
|
||||
|
||||
fn clear_bin(bin: &Element) -> Result<()> {
|
||||
@@ -155,9 +500,14 @@ fn build_unknown(bin: &Element, pattern: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
struct Linked {
|
||||
appsrc: AppSrc,
|
||||
output: Element,
|
||||
}
|
||||
|
||||
fn pipe_h264(bin: &Element, stream_config: &StreamConfig) -> Result<Linked> {
|
||||
let buffer_size = buffer_size(stream_config.bitrate);
|
||||
log::info!(
|
||||
log::debug!(
|
||||
"buffer_size: {buffer_size}, bitrate: {}",
|
||||
stream_config.bitrate
|
||||
);
|
||||
@@ -176,7 +526,7 @@ fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(buffer_size as u64);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Seekable);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
@@ -184,17 +534,34 @@ fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
let queue = make_queue("source_queue", buffer_size)?;
|
||||
let parser = make_element("h264parse", "parser")?;
|
||||
// let stamper = make_element("h264timestamper", "stamper")?;
|
||||
let payload = make_element("rtph264pay", "pay0")?;
|
||||
bin.add_many([&source, &queue, &parser, &payload])?;
|
||||
Element::link_many([&source, &queue, &parser, &payload])?;
|
||||
|
||||
bin.add_many([&source, &queue, &parser])?;
|
||||
Element::link_many([&source, &queue, &parser])?;
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
Ok(source)
|
||||
Ok(Linked {
|
||||
appsrc: source,
|
||||
output: parser,
|
||||
})
|
||||
}
|
||||
|
||||
fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
let linked = pipe_h264(bin, stream_config)?;
|
||||
|
||||
let bin = bin
|
||||
.clone()
|
||||
.dynamic_cast::<Bin>()
|
||||
.map_err(|_| anyhow!("Media source's element should be a bin"))?;
|
||||
|
||||
let payload = make_element("rtph264pay", "pay0")?;
|
||||
bin.add_many([&payload])?;
|
||||
Element::link_many([&linked.output, &payload])?;
|
||||
Ok(linked.appsrc)
|
||||
}
|
||||
|
||||
fn pipe_h265(bin: &Element, stream_config: &StreamConfig) -> Result<Linked> {
|
||||
let buffer_size = buffer_size(stream_config.bitrate);
|
||||
let bin = bin
|
||||
.clone()
|
||||
@@ -210,7 +577,7 @@ fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(buffer_size as u64);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Seekable);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
@@ -218,18 +585,36 @@ fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
let queue = make_queue("source_queue", buffer_size)?;
|
||||
let parser = make_element("h265parse", "parser")?;
|
||||
// let stamper = make_element("h265timestamper", "stamper")?;
|
||||
let payload = make_element("rtph265pay", "pay0")?;
|
||||
bin.add_many([&source, &queue, &parser, &payload])?;
|
||||
Element::link_many([&source, &queue, &parser, &payload])?;
|
||||
|
||||
bin.add_many([&source, &queue, &parser])?;
|
||||
Element::link_many([&source, &queue, &parser])?;
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
Ok(source)
|
||||
Ok(Linked {
|
||||
appsrc: source,
|
||||
output: parser,
|
||||
})
|
||||
}
|
||||
|
||||
fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
let buffer_size = 512 * 6;
|
||||
fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
let linked = pipe_h265(bin, stream_config)?;
|
||||
|
||||
let bin = bin
|
||||
.clone()
|
||||
.dynamic_cast::<Bin>()
|
||||
.map_err(|_| anyhow!("Media source's element should be a bin"))?;
|
||||
|
||||
let payload = make_element("rtph265pay", "pay0")?;
|
||||
bin.add_many([&payload])?;
|
||||
Element::link_many([&linked.output, &payload])?;
|
||||
Ok(linked.appsrc)
|
||||
}
|
||||
|
||||
fn pipe_aac(bin: &Element, stream_config: &StreamConfig) -> Result<Linked> {
|
||||
// Audio seems to run at about 800kbs
|
||||
let buffer_size = 512 * 1416;
|
||||
let bin = bin
|
||||
.clone()
|
||||
.dynamic_cast::<Bin>()
|
||||
@@ -245,7 +630,7 @@ fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(buffer_size as u64);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Seekable);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
@@ -268,9 +653,8 @@ fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
}
|
||||
|
||||
let encoder = make_element("audioconvert", "audencoder")?;
|
||||
let payload = make_element("rtpL16pay", "pay1")?;
|
||||
|
||||
bin.add_many([&source, &queue, &parser, &decoder, &encoder, &payload])?;
|
||||
bin.add_many([&source, &queue, &parser, &decoder, &encoder])?;
|
||||
if let Ok(fallback_switch) = fallback_switch.as_ref() {
|
||||
bin.add_many([&silence, fallback_switch])?;
|
||||
Element::link_many([
|
||||
@@ -280,21 +664,37 @@ fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
&decoder,
|
||||
fallback_switch,
|
||||
&encoder,
|
||||
&payload,
|
||||
])?;
|
||||
Element::link_many([&silence, fallback_switch])?;
|
||||
} else {
|
||||
Element::link_many([&source, &queue, &parser, &decoder, &encoder, &payload])?;
|
||||
Element::link_many([&source, &queue, &parser, &decoder, &encoder])?;
|
||||
}
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
Ok(source)
|
||||
Ok(Linked {
|
||||
appsrc: source,
|
||||
output: encoder,
|
||||
})
|
||||
}
|
||||
|
||||
fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
let buffer_size = 512 * 6;
|
||||
fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
let linked = pipe_aac(bin, stream_config)?;
|
||||
|
||||
let bin = bin
|
||||
.clone()
|
||||
.dynamic_cast::<Bin>()
|
||||
.map_err(|_| anyhow!("Media source's element should be a bin"))?;
|
||||
|
||||
let payload = make_element("rtpL16pay", "pay1")?;
|
||||
bin.add_many([&payload])?;
|
||||
Element::link_many([&linked.output, &payload])?;
|
||||
Ok(linked.appsrc)
|
||||
}
|
||||
|
||||
fn pipe_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) -> Result<Linked> {
|
||||
let buffer_size = 512 * 1416;
|
||||
let bin = bin
|
||||
.clone()
|
||||
.dynamic_cast::<Bin>()
|
||||
@@ -316,7 +716,7 @@ fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) ->
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(buffer_size as u64);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Seekable);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
|
||||
source.set_caps(Some(
|
||||
&Caps::builder("audio/x-adpcm")
|
||||
@@ -334,11 +734,10 @@ fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) ->
|
||||
let queue = make_queue("audqueue", buffer_size)?;
|
||||
let decoder = make_element("decodebin", "auddecoder")?;
|
||||
let encoder = make_element("audioconvert", "audencoder")?;
|
||||
let payload = make_element("rtpL16pay", "pay1")?;
|
||||
let encoder_out = encoder.clone();
|
||||
|
||||
bin.add_many([&source, &queue, &decoder, &encoder, &payload])?;
|
||||
bin.add_many([&source, &queue, &decoder, &encoder])?;
|
||||
Element::link_many([&source, &queue, &decoder])?;
|
||||
Element::link_many([&encoder, &payload])?;
|
||||
decoder.connect_pad_added(move |_element, pad| {
|
||||
let sink_pad = encoder
|
||||
.static_pad("sink")
|
||||
@@ -350,9 +749,122 @@ fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) ->
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
Ok(source)
|
||||
Ok(Linked {
|
||||
appsrc: source,
|
||||
output: encoder_out,
|
||||
})
|
||||
}
|
||||
|
||||
fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) -> Result<AppSrc> {
|
||||
let linked = pipe_adpcm(bin, block_size, stream_config)?;
|
||||
|
||||
let bin = bin
|
||||
.clone()
|
||||
.dynamic_cast::<Bin>()
|
||||
.map_err(|_| anyhow!("Media source's element should be a bin"))?;
|
||||
|
||||
let payload = make_element("rtpL16pay", "pay1")?;
|
||||
bin.add_many([&payload])?;
|
||||
Element::link_many([&linked.output, &payload])?;
|
||||
Ok(linked.appsrc)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn pipe_silence(bin: &Element, stream_config: &StreamConfig) -> Result<Linked> {
|
||||
// Audio seems to run at about 800kbs
|
||||
let buffer_size = 512 * 1416;
|
||||
let bin = bin
|
||||
.clone()
|
||||
.dynamic_cast::<Bin>()
|
||||
.map_err(|_| anyhow!("Media source's element should be a bin"))?;
|
||||
log::debug!("Building Silence pipeline");
|
||||
let source = make_element("appsrc", "audsrc")?
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
|
||||
|
||||
source.set_is_live(false);
|
||||
source.set_block(false);
|
||||
source.set_min_latency(1000 / (stream_config.fps as i64));
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(buffer_size as u64);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
|
||||
let sink_queue = make_queue("audsinkqueue", buffer_size)?;
|
||||
let sink = make_element("fakesink", "silence_sink")?;
|
||||
|
||||
let silence = make_element("audiotestsrc", "audsilence")?;
|
||||
silence.set_property_from_str("wave", "silence");
|
||||
let src_queue = make_queue("audsinkqueue", buffer_size)?;
|
||||
let encoder = make_element("audioconvert", "audencoder")?;
|
||||
|
||||
bin.add_many([&source, &sink_queue, &sink, &silence, &src_queue, &encoder])?;
|
||||
|
||||
Element::link_many([&source, &sink_queue, &sink])?;
|
||||
|
||||
Element::link_many([&silence, &src_queue, &encoder])?;
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
Ok(Linked {
|
||||
appsrc: source,
|
||||
output: encoder,
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
struct AppSrcPair {
|
||||
vid: AppSrc,
|
||||
aud: Option<AppSrc>,
|
||||
}
|
||||
|
||||
// #[allow(dead_code)]
|
||||
// /// Experimental build a stream of MPEGTS
|
||||
// fn build_mpegts(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrcPair> {
|
||||
// let buffer_size = buffer_size(stream_config.bitrate);
|
||||
// log::debug!(
|
||||
// "buffer_size: {buffer_size}, bitrate: {}",
|
||||
// stream_config.bitrate
|
||||
// );
|
||||
|
||||
// // VID
|
||||
// let vid_link = match stream_config.vid_format {
|
||||
// VidFormat::H264 => pipe_h264(bin, stream_config)?,
|
||||
// VidFormat::H265 => pipe_h265(bin, stream_config)?,
|
||||
// VidFormat::None => unreachable!(),
|
||||
// };
|
||||
|
||||
// // AUD
|
||||
// let aud_link = match stream_config.aud_format {
|
||||
// AudFormat::Aac => pipe_aac(bin, stream_config)?,
|
||||
// AudFormat::Adpcm(block) => pipe_adpcm(bin, block, stream_config)?,
|
||||
// AudFormat::None => pipe_silence(bin, stream_config)?,
|
||||
// };
|
||||
|
||||
// let bin = bin
|
||||
// .clone()
|
||||
// .dynamic_cast::<Bin>()
|
||||
// .map_err(|_| anyhow!("Media source's element should be a bin"))?;
|
||||
|
||||
// // MUX
|
||||
// let muxer = make_element("mpegtsmux", "mpeg_muxer")?;
|
||||
// let rtp = make_element("rtpmp2tpay", "pay0")?;
|
||||
|
||||
// bin.add_many([&muxer, &rtp])?;
|
||||
// Element::link_many([&vid_link.output, &muxer, &rtp])?;
|
||||
// Element::link_many([&aud_link.output, &muxer])?;
|
||||
|
||||
// Ok(AppSrcPair {
|
||||
// vid: vid_link.appsrc,
|
||||
// aud: Some(aud_link.appsrc),
|
||||
// })
|
||||
// }
|
||||
|
||||
// Convenice funcion to make an element or provide a message
|
||||
// about what plugin is missing
|
||||
fn make_element(kind: &str, name: &str) -> AnyResult<Element> {
|
||||
|
||||
+10
-5
@@ -47,10 +47,13 @@ impl NeoRtspServer {
|
||||
// Setup auth
|
||||
let auth = factory.auth().unwrap_or_default();
|
||||
auth.set_supported_methods(RTSPAuthMethod::Basic);
|
||||
let mut un_authtoken = RTSPToken::new(&[
|
||||
//RTSP_TOKEN_MEDIA_FACTORY_ROLE: Means look inside the media factory settings and use the same permissions this user (`"anonymous"`) has
|
||||
(RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"anonymous"),
|
||||
]);
|
||||
let mut un_authtoken = RTSPToken::builder()
|
||||
.field(
|
||||
//RTSP_TOKEN_MEDIA_FACTORY_ROLE: Means look inside the media factory settings and use the same permissions this user (`"anonymous"`) has
|
||||
RTSP_TOKEN_MEDIA_FACTORY_ROLE,
|
||||
"anonymous",
|
||||
)
|
||||
.build();
|
||||
auth.set_default_token(Some(&mut un_authtoken));
|
||||
factory.set_auth(Some(&auth));
|
||||
|
||||
@@ -217,7 +220,9 @@ impl NeoRtspServerImpl {
|
||||
let mut locked_users = self.users.write().await;
|
||||
let auth = self.obj().auth().unwrap();
|
||||
|
||||
let token = RTSPToken::new(&[(RTSP_TOKEN_MEDIA_FACTORY_ROLE, &username)]);
|
||||
let token = RTSPToken::builder()
|
||||
.field(RTSP_TOKEN_MEDIA_FACTORY_ROLE, username)
|
||||
.build();
|
||||
let basic = RTSPAuth::make_basic(username, password);
|
||||
|
||||
if let Some(old_basic) = locked_users.get(username) {
|
||||
|
||||
+6
-4
@@ -233,7 +233,8 @@ async fn apply_users(rtsp: &NeoRtspServer, curr_users: &HashSet<UserConfig>) ->
|
||||
// Add those missing
|
||||
for user in curr_users.iter() {
|
||||
log::debug!("Adding user {} to rtsp server", user.name);
|
||||
rtsp.add_user(&user.name, &user.pass).await?;
|
||||
rtsp.add_user(&user.name, &user.pass.clone().unwrap_or_default())
|
||||
.await?;
|
||||
}
|
||||
// Remove unused
|
||||
let rtsp_users = rtsp.get_users().await?;
|
||||
@@ -362,7 +363,7 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
|
||||
log::debug!("{}: Preparing at {}", name, paths.join(", "));
|
||||
|
||||
supported_streams_1.wait_for(|ss| ss.contains(&StreamKind::Main)).await?;
|
||||
stream_main(camera.stream(StreamKind::Main).await?, camera.clone(), rtsp, &permitted_users, &paths).await
|
||||
stream_main(camera.clone(), StreamKind::Main, rtsp, &permitted_users, &paths).await
|
||||
}, if active_streams.contains(&StreamKind::Main) => v,
|
||||
v = async {
|
||||
let name = camera.config().await?.borrow().name.clone();
|
||||
@@ -396,7 +397,8 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
|
||||
log::debug!("{}: Preparing at {}", name, paths.join(", "));
|
||||
|
||||
supported_streams_2.wait_for(|ss| ss.contains(&StreamKind::Sub)).await?;
|
||||
stream_main(camera.stream(StreamKind::Sub).await?,camera.clone(), rtsp, &permitted_users, &paths).await
|
||||
|
||||
stream_main(camera.clone(), StreamKind::Sub, rtsp, &permitted_users, &paths).await
|
||||
}, if active_streams.contains(&StreamKind::Sub) => v,
|
||||
v = async {
|
||||
let name = camera.config().await?.borrow().name.clone();
|
||||
@@ -429,7 +431,7 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
|
||||
log::debug!("{}: Preparing at {}", name, paths.join(", "));
|
||||
|
||||
supported_streams_3.wait_for(|ss| ss.contains(&StreamKind::Extern)).await?;
|
||||
stream_main(camera.stream(StreamKind::Extern).await?,camera.clone(), rtsp, &permitted_users, &paths).await
|
||||
stream_main(camera.clone(), StreamKind::Extern, rtsp, &permitted_users, &paths).await
|
||||
}, if active_streams.contains(&StreamKind::Extern) => v,
|
||||
else => {
|
||||
// all disabled just wait here until config is changed
|
||||
|
||||
+11
-857
@@ -1,305 +1,27 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use gstreamer::{prelude::*, ClockTime, FlowError};
|
||||
use gstreamer_app::AppSrc;
|
||||
use anyhow::anyhow;
|
||||
use gstreamer_rtsp_server::prelude::*;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
use tokio::{
|
||||
sync::{broadcast::channel as broadcast, watch::channel as watch},
|
||||
task::JoinSet,
|
||||
time::{interval, sleep, Duration},
|
||||
};
|
||||
use tokio_stream::{wrappers::BroadcastStream, Stream, StreamExt};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::common::{Permit, StampedData, UseCounter, VidFormat};
|
||||
use crate::{
|
||||
common::{NeoInstance, StreamConfig, StreamInstance},
|
||||
AnyResult,
|
||||
};
|
||||
use crate::{common::NeoInstance, AnyResult};
|
||||
use neolink_core::bc_protocol::StreamKind;
|
||||
|
||||
use super::{factory::*, gst::NeoRtspServer};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct PauseAffectors {
|
||||
motion: bool,
|
||||
push: bool,
|
||||
client: bool,
|
||||
}
|
||||
|
||||
/// This handles the stream by activating and deacivating it as required
|
||||
pub(super) async fn stream_main(
|
||||
mut stream_instance: StreamInstance,
|
||||
camera: NeoInstance,
|
||||
rtsp: &NeoRtspServer,
|
||||
users: &HashSet<String>,
|
||||
paths: &[String],
|
||||
) -> Result<()> {
|
||||
let mut camera_config = camera.config().await?.clone();
|
||||
let name = camera_config.borrow().name.clone();
|
||||
|
||||
let mut curr_pause;
|
||||
loop {
|
||||
let this_loop_cancel = CancellationToken::new();
|
||||
let _drop_guard = this_loop_cancel.clone().drop_guard();
|
||||
|
||||
stream_instance.activate().await?;
|
||||
|
||||
// Wait for a valid stream format to be detected
|
||||
stream_instance
|
||||
.config
|
||||
.wait_for(|config| config.vid_ready())
|
||||
.await?;
|
||||
// After vid give it 1s to look for audio
|
||||
// Ignore timeout but check err
|
||||
if let Ok(v) = tokio::time::timeout(
|
||||
Duration::from_secs(1),
|
||||
stream_instance.config.wait_for(|config| config.aud_ready()),
|
||||
)
|
||||
.await
|
||||
{
|
||||
v?;
|
||||
}
|
||||
|
||||
curr_pause = camera_config.borrow().pause.clone();
|
||||
|
||||
let last_stream_config = stream_instance.config.borrow().clone();
|
||||
let mut thread_stream_config = stream_instance.config.clone();
|
||||
|
||||
let (pause_affector_tx, pause_affector) = watch(PauseAffectors {
|
||||
motion: false,
|
||||
push: false,
|
||||
client: false,
|
||||
});
|
||||
let pause_affector_tx = Arc::new(pause_affector_tx);
|
||||
|
||||
let mut set = JoinSet::<AnyResult<()>>::new();
|
||||
// Handles the on off of the stream with the client pause
|
||||
let client_counter = UseCounter::new().await;
|
||||
let client_count = client_counter.create_deactivated().await?;
|
||||
|
||||
// Client count affector
|
||||
if curr_pause.on_motion {
|
||||
let thread_name = name.clone();
|
||||
let client_count = client_counter.create_deactivated().await?;
|
||||
let thread_pause_affector_tx = pause_affector_tx.clone();
|
||||
let cancel = this_loop_cancel.clone();
|
||||
set.spawn(async move {
|
||||
tokio::select! {
|
||||
_ = cancel.cancelled() => AnyResult::Ok(()),
|
||||
v = async {
|
||||
loop {
|
||||
client_count.aquired_users().await?;
|
||||
log::info!("{}: Enabling Client", thread_name);
|
||||
thread_pause_affector_tx.send_modify(|current| {
|
||||
current.client = true;
|
||||
});
|
||||
|
||||
client_count.dropped_users().await?;
|
||||
log::info!("{}: Pausing Client", thread_name);
|
||||
thread_pause_affector_tx.send_modify(|current| {
|
||||
current.client = false;
|
||||
});
|
||||
}
|
||||
} => v,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Motion affector
|
||||
if curr_pause.on_motion {
|
||||
let thread_name = name.clone();
|
||||
let thread_pause_affector_tx = pause_affector_tx.clone();
|
||||
let cancel = this_loop_cancel.clone();
|
||||
|
||||
let mut motion = camera.motion().await?;
|
||||
let delta = Duration::from_secs_f64(curr_pause.motion_timeout);
|
||||
|
||||
set.spawn(async move {
|
||||
tokio::select! {
|
||||
_ = cancel.cancelled() => AnyResult::Ok(()),
|
||||
v = async {
|
||||
loop {
|
||||
motion
|
||||
.wait_for(|md| matches!(md, crate::common::MdState::Start(_)))
|
||||
.await?;
|
||||
log::info!("{}: Enabling Motion", thread_name);
|
||||
thread_pause_affector_tx.send_modify(|current| {
|
||||
current.motion = true;
|
||||
});
|
||||
|
||||
motion
|
||||
.wait_for(
|
||||
|md| matches!(md, crate::common::MdState::Stop(n) if n.elapsed()>delta),
|
||||
)
|
||||
.await?;
|
||||
log::info!("{}: Pausing Motion", thread_name);
|
||||
thread_pause_affector_tx.send_modify(|current| {
|
||||
current.motion = false;
|
||||
});
|
||||
}
|
||||
} => v,
|
||||
}
|
||||
});
|
||||
|
||||
// Push notfications
|
||||
let mut pn = camera.push_notifications().await?;
|
||||
let mut curr_pn = None;
|
||||
let thread_name = name.clone();
|
||||
let thread_pause_affector_tx = pause_affector_tx.clone();
|
||||
let cancel = this_loop_cancel.clone();
|
||||
set.spawn(async move {
|
||||
tokio::select! {
|
||||
_ = cancel.cancelled() => AnyResult::Ok(()),
|
||||
v = async {
|
||||
loop {
|
||||
curr_pn = pn
|
||||
.wait_for(|pn| pn != &curr_pn && pn.is_some())
|
||||
.await?
|
||||
.clone();
|
||||
log::info!("{}: Enabling Push Notification", thread_name);
|
||||
thread_pause_affector_tx.send_modify(|current| {
|
||||
current.push = true;
|
||||
});
|
||||
tokio::select! {
|
||||
v = pn.wait_for(|pn| pn != &curr_pn && pn.is_some()) => {
|
||||
v?;
|
||||
// If another PN during wait then go back to wait more
|
||||
continue;
|
||||
}
|
||||
_ = sleep(Duration::from_secs(30)) => {}
|
||||
}
|
||||
log::info!("{}: Pausing Push Notification", thread_name);
|
||||
thread_pause_affector_tx.send_modify(|current| {
|
||||
current.push = false;
|
||||
});
|
||||
}
|
||||
} => v,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if curr_pause.on_motion || curr_pause.on_disconnect {
|
||||
// Take over activation
|
||||
let cancel = this_loop_cancel.clone();
|
||||
let mut client_activator = stream_instance.activator_handle().await;
|
||||
client_activator.deactivate().await?;
|
||||
stream_instance.deactivate().await?;
|
||||
let mut pause_affector = tokio_stream::wrappers::WatchStream::new(pause_affector);
|
||||
let thread_curr_pause = curr_pause.clone();
|
||||
set.spawn(async move {
|
||||
tokio::select! {
|
||||
_ = cancel.cancelled() => AnyResult::Ok(()),
|
||||
v = async {
|
||||
while let Some(state) = pause_affector.next().await {
|
||||
if thread_curr_pause.on_motion && thread_curr_pause.on_disconnect {
|
||||
if state.client && (state.motion || state.push) {
|
||||
client_activator.activate().await?;
|
||||
} else {
|
||||
client_activator.deactivate().await?;
|
||||
}
|
||||
} else if thread_curr_pause.on_motion {
|
||||
if state.motion || state.push {
|
||||
client_activator.activate().await?;
|
||||
} else {
|
||||
client_activator.deactivate().await?;
|
||||
}
|
||||
} else if thread_curr_pause.on_disconnect {
|
||||
if state.client {
|
||||
client_activator.activate().await?;
|
||||
} else {
|
||||
client_activator.deactivate().await?;
|
||||
}
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
AnyResult::Ok(())
|
||||
} => v,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// This thread jsut keeps it active for 5s after an initial start to build the buffer
|
||||
let cancel = this_loop_cancel.clone();
|
||||
let mut init_activator = stream_instance.activator_handle().await;
|
||||
let init_camera = camera.clone();
|
||||
set.spawn(async move {
|
||||
tokio::select! {
|
||||
_ = cancel.cancelled() => AnyResult::Ok(()),
|
||||
v = async {
|
||||
init_activator.activate().await?;
|
||||
let _ = init_camera
|
||||
.run_task(|_| {
|
||||
Box::pin(async move {
|
||||
sleep(Duration::from_secs(5)).await;
|
||||
AnyResult::Ok(())
|
||||
})
|
||||
})
|
||||
.await;
|
||||
init_activator.deactivate().await?;
|
||||
AnyResult::Ok(())
|
||||
} => v,
|
||||
}
|
||||
});
|
||||
|
||||
// Task to just report the number of clients for debug purposes
|
||||
let cancel = this_loop_cancel.clone();
|
||||
let counter = client_counter.create_deactivated().await?;
|
||||
let mut cur_count = 0;
|
||||
set.spawn(async move {
|
||||
tokio::select! {
|
||||
_ = cancel.cancelled() => AnyResult::Ok(()),
|
||||
v = async {
|
||||
loop {
|
||||
cur_count = *counter.get_counter().wait_for(|v| v != &cur_count).await?;
|
||||
}
|
||||
} => v,
|
||||
}
|
||||
});
|
||||
|
||||
// This runs the actual stream.
|
||||
// The select will restart if the stream's config updates
|
||||
break tokio::select! {
|
||||
v = thread_stream_config.wait_for(|new_conf| new_conf != &last_stream_config) => {
|
||||
let v = v?;
|
||||
// If stream config changes we reload the stream
|
||||
log::info!("{}: Stream Configuration Changed. Reloading Streams", &name);
|
||||
log::trace!(" From {:?} to {:?}", last_stream_config, v.clone());
|
||||
continue;
|
||||
},
|
||||
v = camera_config.wait_for(|new_conf| new_conf.pause != curr_pause ) => {
|
||||
v?;
|
||||
// If pause config changes restart
|
||||
log::info!("{}: Pause Configuration Changed. Reloading Streams", &name);
|
||||
continue;
|
||||
},
|
||||
v = stream_run(&name, &stream_instance, rtsp, &last_stream_config, users, paths, client_count) => v,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// This handles the stream itself by creating the factory and pushing messages into it
|
||||
async fn stream_run(
|
||||
name: &str,
|
||||
stream_instance: &StreamInstance,
|
||||
pub(crate) async fn stream_main(
|
||||
camera: NeoInstance,
|
||||
stream: StreamKind,
|
||||
rtsp: &NeoRtspServer,
|
||||
stream_config: &StreamConfig,
|
||||
users: &HashSet<String>,
|
||||
paths: &[String],
|
||||
client_count: Permit,
|
||||
) -> AnyResult<()> {
|
||||
let vidstream = stream_instance.vid.resubscribe();
|
||||
let audstream = stream_instance.aud.resubscribe();
|
||||
let vid_history = stream_instance.vid_history.clone();
|
||||
let aud_history = stream_instance.aud_history.clone();
|
||||
|
||||
// Finally ready to create the factory and connect the stream
|
||||
let name = camera.config().await?.borrow().name.clone();
|
||||
// Create the factory and connect the stream
|
||||
let mounts = rtsp
|
||||
.mount_points()
|
||||
.ok_or(anyhow!("RTSP server lacks mount point"))?;
|
||||
// Create the factory
|
||||
let (factory, mut client_rx) = make_factory(stream_config).await?;
|
||||
let (factory, thread) = make_factory(camera, stream).await?;
|
||||
|
||||
factory.add_permitted_roles(users);
|
||||
|
||||
@@ -309,574 +31,6 @@ async fn stream_run(
|
||||
}
|
||||
log::info!("{}: Available at {}", name, paths.join(", "));
|
||||
|
||||
let stream_cancel = CancellationToken::new();
|
||||
let drop_guard = stream_cancel.clone().drop_guard();
|
||||
let mut set = JoinSet::new();
|
||||
// Wait for new media client data to come in from the factory
|
||||
while let Some(mut client_data) = client_rx.recv().await {
|
||||
// New media created
|
||||
let vid = client_data.vid.take().map(|data| data.app);
|
||||
let aud = client_data.aud.take().map(|data| data.app);
|
||||
|
||||
// This is the data that gets sent to gstreamer thread
|
||||
// It represents the combination of the camera stream and the appsrc seek messages
|
||||
// At 30fps for 15s with audio you need about 900 frames
|
||||
// Therefore the buffer is rather large at 2000
|
||||
let (aud_data_tx, aud_data_rx) = broadcast(2000);
|
||||
let (vid_data_tx, vid_data_rx) = broadcast(2000);
|
||||
|
||||
// This thread takes the video data from the cam and passed it into the stream
|
||||
let mut vidstream = BroadcastStream::new(vidstream.resubscribe());
|
||||
let thread_vid_data_tx = vid_data_tx.clone();
|
||||
let thread_stream_cancel = stream_cancel.clone();
|
||||
let thread_vid_history = vid_history.clone();
|
||||
set.spawn(async move {
|
||||
let r = tokio::select! {
|
||||
_ = thread_stream_cancel.cancelled() => AnyResult::Ok(()),
|
||||
v = async {
|
||||
// Send Initial
|
||||
{
|
||||
let history = thread_vid_history.borrow();
|
||||
// let last_ts = history.back().map(|s| s.ts);
|
||||
for data in history.iter() {
|
||||
thread_vid_data_tx.send(
|
||||
// StampedData {
|
||||
// keyframe: data.keyframe,
|
||||
// data: data.data.clone(),
|
||||
// ts: last_ts.unwrap()
|
||||
// }
|
||||
data.clone()
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
// Send new
|
||||
while let Some(frame) = vidstream.next().await {
|
||||
if let Ok(data) = frame {
|
||||
thread_vid_data_tx.send(
|
||||
data
|
||||
)?;
|
||||
}
|
||||
};
|
||||
AnyResult::Ok(())
|
||||
} => v,
|
||||
};
|
||||
log::trace!("Stream Vid Media End {r:?}");
|
||||
AnyResult::Ok(())
|
||||
});
|
||||
|
||||
// This thread takes the audio data from the cam and passed it into the stream
|
||||
let mut audstream = BroadcastStream::new(audstream.resubscribe());
|
||||
let thread_stream_cancel = stream_cancel.clone();
|
||||
let thread_aud_data_tx = aud_data_tx.clone();
|
||||
let thread_aud_history = aud_history.clone();
|
||||
set.spawn(async move {
|
||||
let r = tokio::select! {
|
||||
_ = thread_stream_cancel.cancelled() => AnyResult::Ok(()),
|
||||
v = async {
|
||||
// Send Initial
|
||||
{
|
||||
let history = thread_aud_history.borrow();
|
||||
// let last_ts = history.back().map(|s| s.ts);
|
||||
for data in history.iter() {
|
||||
thread_aud_data_tx.send(
|
||||
// StampedData {
|
||||
// keyframe: data.keyframe,
|
||||
// data: data.data.clone(),
|
||||
// ts: last_ts.unwrap()
|
||||
// }
|
||||
data.clone()
|
||||
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
// Send new
|
||||
while let Some(frame) = audstream.next().await {
|
||||
if let Ok(data) = frame {
|
||||
thread_aud_data_tx.send(
|
||||
data
|
||||
)?;
|
||||
}
|
||||
};
|
||||
AnyResult::Ok(())
|
||||
} => v,
|
||||
};
|
||||
log::trace!("Stream Aud Media End: {r:?}");
|
||||
AnyResult::Ok(())
|
||||
});
|
||||
|
||||
// Handles sending the video data into gstreamer
|
||||
let thread_stream_cancel = stream_cancel.clone();
|
||||
let vid_data_rx = BroadcastStream::new(vid_data_rx).filter(|f| f.is_ok()); // Filter to ignore lagged
|
||||
let thread_vid = vid.clone();
|
||||
let mut thread_client_count = client_count.subscribe();
|
||||
let thread_format = stream_config.vid_format;
|
||||
// let fallback_time = Duration::from_secs(3);
|
||||
let framerate =
|
||||
Duration::from_millis(1000u64 / std::cmp::max(stream_config.fps as u64, 5u64));
|
||||
if let Some(thread_vid) = thread_vid {
|
||||
set.spawn(async move {
|
||||
thread_client_count.activate().await?;
|
||||
let r = tokio::select! {
|
||||
_ = thread_stream_cancel.cancelled() => {
|
||||
AnyResult::Ok(())
|
||||
},
|
||||
v = send_to_appsrc(
|
||||
pad_vid(
|
||||
insert_filler(
|
||||
frametime_stream(
|
||||
// ensure_order(
|
||||
wait_for_keyframe(
|
||||
vid_data_rx,
|
||||
),
|
||||
// ),
|
||||
framerate
|
||||
),
|
||||
thread_format,
|
||||
framerate,
|
||||
),
|
||||
thread_format,
|
||||
),
|
||||
&thread_vid
|
||||
) => {
|
||||
v
|
||||
},
|
||||
};
|
||||
drop(thread_client_count);
|
||||
let _ = thread_vid.end_of_stream();
|
||||
r
|
||||
});
|
||||
}
|
||||
|
||||
// Handles the audio data into gstreamer
|
||||
let thread_stream_cancel = stream_cancel.clone();
|
||||
let aud_data_rx = BroadcastStream::new(aud_data_rx).filter(|f| f.is_ok()); // Filter to ignore lagged
|
||||
let thread_aud = aud.clone();
|
||||
let aud_framerate =
|
||||
Duration::from_millis(1000u64 / std::cmp::max(stream_config.fps as u64, 5u64));
|
||||
if let Some(thread_aud) = thread_aud {
|
||||
set.spawn(async move {
|
||||
let r = tokio::select! {
|
||||
_ = thread_stream_cancel.cancelled() => {
|
||||
AnyResult::Ok(())
|
||||
},
|
||||
v = send_to_appsrc(
|
||||
frametime_stream(
|
||||
// ensure_order(
|
||||
wait_for_keyframe(
|
||||
aud_data_rx
|
||||
),
|
||||
// ),
|
||||
aud_framerate),
|
||||
&thread_aud) => {
|
||||
v
|
||||
},
|
||||
};
|
||||
let _ = thread_aud.end_of_stream();
|
||||
r
|
||||
});
|
||||
}
|
||||
}
|
||||
// At this point the factory has been destroyed
|
||||
// Cancel any remaining threads that are trying to send data
|
||||
// Although it should be finished already when the appsrcs are dropped
|
||||
stream_cancel.cancel();
|
||||
drop(drop_guard);
|
||||
while set.join_next().await.is_some() {}
|
||||
log::trace!("Stream done");
|
||||
AnyResult::Ok(())
|
||||
}
|
||||
|
||||
fn check_live(app: &AppSrc) -> Result<()> {
|
||||
app.bus().ok_or(anyhow!("App source is closed"))?;
|
||||
app.pads()
|
||||
.iter()
|
||||
.all(|pad| pad.is_linked())
|
||||
.then_some(())
|
||||
.ok_or(anyhow!("App source is not linked"))
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn get_runtime(app: &AppSrc) -> Option<Duration> {
|
||||
if let Some(clock) = app.clock() {
|
||||
if let Some(time) = clock.time() {
|
||||
if let Some(base_time) = app.base_time() {
|
||||
let runtime = time.saturating_sub(base_time);
|
||||
return Some(Duration::from_micros(runtime.useconds()));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
// This ensures we start at a keyframe
|
||||
fn wait_for_keyframe<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
|
||||
mut stream: T,
|
||||
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
|
||||
Box::pin(async_stream::stream! {
|
||||
let mut found_key = false;
|
||||
while let Some(frame) = stream.next().await {
|
||||
if let Ok(frame) = frame {
|
||||
if frame.keyframe || found_key {
|
||||
found_key = true;
|
||||
yield Ok(frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
// Take a stream of stamped data and release them
|
||||
// in waves when a new key frame is found
|
||||
// this ensure that the last frame sent is always an IFrame
|
||||
fn hold_stream<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
|
||||
mut stream: T,
|
||||
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
|
||||
Box::pin(async_stream::stream! {
|
||||
let mut held_frames = vec![];
|
||||
while let Some(frame) = stream.next().await {
|
||||
if let Ok(frame) = frame {
|
||||
if frame.keyframe {
|
||||
// Release
|
||||
for held_frame in held_frames.drain(..) {
|
||||
yield Ok(held_frame);
|
||||
}
|
||||
yield Ok(frame);
|
||||
} else {
|
||||
// Hold
|
||||
held_frames.push(frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Take a stream of stamped data and reorder it
|
||||
// in case they are out of order
|
||||
// this also releases frames in waves of keyframe so it should replace `hold_stream`
|
||||
#[allow(dead_code)]
|
||||
fn ensure_order<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
|
||||
mut stream: T,
|
||||
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
|
||||
Box::pin(async_stream::stream! {
|
||||
let mut frame_buffer: Vec<StampedData> = vec![];
|
||||
while let Some(frame) = stream.next().await {
|
||||
if let Ok(frame) = frame {
|
||||
if ! frame.keyframe {
|
||||
// Buffer until keyframe and reorder
|
||||
let mut reorder_buffer = vec![];
|
||||
while frame_buffer.last().is_some_and(|v| v.ts > frame.ts) {
|
||||
reorder_buffer.push(frame_buffer.pop().unwrap());
|
||||
}
|
||||
frame_buffer.push(frame);
|
||||
|
||||
while let Some(frame) = reorder_buffer.pop() {
|
||||
frame_buffer.push(frame);
|
||||
}
|
||||
} else {
|
||||
// On key frame flush out
|
||||
for frame in frame_buffer.drain(..) {
|
||||
yield Ok(frame);
|
||||
}
|
||||
yield(Ok(frame));
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Take a stream of stamped data pause until
|
||||
// it is time to display it
|
||||
#[allow(dead_code)]
|
||||
fn frametime_stream<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
|
||||
mut stream: T,
|
||||
expected_frame_rate: Duration,
|
||||
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
|
||||
Box::pin(async_stream::stream! {
|
||||
let mut waiter = interval(expected_frame_rate);
|
||||
waiter.set_missed_tick_behavior( tokio::time::MissedTickBehavior::Skip);
|
||||
while let Some(frame) = stream.next().await {
|
||||
if let Ok(frame) = frame {
|
||||
waiter.tick().await;
|
||||
yield Ok(frame);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Insert filler into the video stream when no frames are comming
|
||||
/// this should help the stream not be considered dead while
|
||||
/// we wait for the reconnect
|
||||
fn insert_filler<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
|
||||
mut stream: T,
|
||||
format: VidFormat,
|
||||
delay: Duration,
|
||||
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
|
||||
let mut last_ts = Duration::ZERO;
|
||||
Box::pin(async_stream::stream! {
|
||||
while let Some(frame) = tokio::select!{
|
||||
v = stream.next() => {
|
||||
v
|
||||
},
|
||||
_ = tokio::time::sleep(delay) => {
|
||||
match format {
|
||||
VidFormat::H264 => {
|
||||
Some(Ok(StampedData {
|
||||
data: Arc::new(h264_filler(4096)),
|
||||
keyframe: false,
|
||||
ts: last_ts,
|
||||
}))
|
||||
}
|
||||
VidFormat::H265 => {
|
||||
Some(Ok(StampedData {
|
||||
data: Arc::new(h265_filler(4096)),
|
||||
keyframe: false,
|
||||
ts: last_ts,
|
||||
}))
|
||||
}
|
||||
VidFormat::None => unreachable!(),
|
||||
}
|
||||
}
|
||||
} {
|
||||
if let Ok(frame) = frame {
|
||||
last_ts = frame.ts;
|
||||
yield Ok(frame);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn h265_filler(size: usize) -> Vec<u8> {
|
||||
assert!(size >= 6);
|
||||
let mut buf = vec![0x0, 0x0, 0x1, 0b01001100, 0x0];
|
||||
buf.resize(size, 0xFF);
|
||||
buf[size - 1] = 0x80;
|
||||
buf
|
||||
}
|
||||
|
||||
fn h264_filler(size: usize) -> Vec<u8> {
|
||||
assert!(size >= 5);
|
||||
let mut buf = vec![0x0, 0x0, 0x1, 0xC];
|
||||
buf.resize(size, 0xFF);
|
||||
buf[size - 1] = 0x80;
|
||||
buf
|
||||
}
|
||||
|
||||
/// Takes a stream and pads it with filler blocks up to 4kb in h265 or h264 format
|
||||
fn pad_vid<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
|
||||
mut stream: T,
|
||||
format: VidFormat,
|
||||
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
|
||||
let min_pad: usize = match format {
|
||||
VidFormat::H264 => 5,
|
||||
VidFormat::H265 => 6,
|
||||
VidFormat::None => unreachable!(),
|
||||
};
|
||||
Box::pin(async_stream::stream! {
|
||||
while let Some(frame) = stream.next().await {
|
||||
if let Ok(frame) = frame {
|
||||
if (frame.data.len() % 4096) != 0 {
|
||||
let pad_size: usize = (frame.data.len() + min_pad).div_ceil(4096) * 4096 - frame.data.len();
|
||||
let frame = StampedData {
|
||||
keyframe: frame.keyframe,
|
||||
data: Arc::new(
|
||||
match format {
|
||||
VidFormat::H264 => {
|
||||
frame.data.iter().chain(
|
||||
h264_filler(pad_size).iter()
|
||||
).copied().collect()
|
||||
}
|
||||
VidFormat::H265 => {
|
||||
frame.data.iter().chain(
|
||||
h265_filler(pad_size).iter()
|
||||
).copied().collect()
|
||||
}
|
||||
VidFormat::None => unreachable!(),
|
||||
}
|
||||
),
|
||||
ts: frame.ts,
|
||||
};
|
||||
yield Ok(frame);
|
||||
} else {
|
||||
yield Ok(frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
// This will take a stream and if there is a notibable lack of data
|
||||
// then it will repeat the last keyframe (if there have been no
|
||||
// pframes in between)
|
||||
fn repeat_keyframe<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
|
||||
mut stream: T,
|
||||
fallback_time: Duration,
|
||||
frame_rate: Duration,
|
||||
) -> impl Stream<Item = Result<StampedData, E>> + Unpin {
|
||||
Box::pin(async_stream::stream! {
|
||||
let mut was_repeating = false;
|
||||
while let Some(frame) = stream.next().await {
|
||||
if let Ok(frame) = frame {
|
||||
if frame.keyframe {
|
||||
let repeater = frame.clone();
|
||||
yield Ok(frame);
|
||||
|
||||
// Wait for either timeout or a new frame
|
||||
let mut fallback_time = fallback_time;
|
||||
loop {
|
||||
tokio::select!{
|
||||
v = stream.next() => {
|
||||
if let Some(frame) = v {
|
||||
if let Ok(frame) = frame {
|
||||
if was_repeating {
|
||||
was_repeating = false;
|
||||
}
|
||||
yield Ok(frame);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
},
|
||||
_ = sleep(fallback_time) => {
|
||||
if !was_repeating {
|
||||
// This way we only print once
|
||||
was_repeating = true;
|
||||
}
|
||||
fallback_time = frame_rate;
|
||||
|
||||
yield Ok(repeater.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// P frames go through as-is
|
||||
yield Ok(frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Takes a stream and sends it to an appsrc
|
||||
async fn send_to_appsrc<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
|
||||
mut stream: T,
|
||||
appsrc: &AppSrc,
|
||||
) -> AnyResult<()> {
|
||||
let mut ts_0 = Duration::MAX;
|
||||
let mut wait_for_iframe = true;
|
||||
let mut pools: HashMap<usize, gstreamer::BufferPool> = Default::default();
|
||||
let mut keyframes_pushed = 0;
|
||||
let mut inited = false;
|
||||
// appsrc.set_state(gstreamer::State::Paused).unwrap();
|
||||
|
||||
let (tx, mut rx) = tokio::sync::mpsc::channel::<StampedData>(2000);
|
||||
|
||||
// Run blocking code on a seperate thread
|
||||
let appsrc = appsrc.clone();
|
||||
std::thread::spawn(move || {
|
||||
let r = (move || {
|
||||
while let Some(data) = rx.blocking_recv() {
|
||||
check_live(&appsrc)?; // Stop if appsrc is dropped
|
||||
if wait_for_iframe && !data.keyframe {
|
||||
continue;
|
||||
} else if wait_for_iframe {
|
||||
wait_for_iframe = false;
|
||||
}
|
||||
if ts_0 > data.ts {
|
||||
ts_0 = data.ts;
|
||||
}
|
||||
let rt = data.ts - ts_0;
|
||||
// log::info!("rt: {:?}", rt);
|
||||
let buf = {
|
||||
// let mut gst_buf = pool.acquire_buffer(None).unwrap();
|
||||
let msg_size = data.data.len();
|
||||
let pool = pools.entry(msg_size).or_insert_with_key(|size| {
|
||||
let pool = gstreamer::BufferPool::new();
|
||||
let mut pool_config = pool.config();
|
||||
pool_config.set_params(None, (*size) as u32, 8, 0);
|
||||
pool.set_config(pool_config).unwrap();
|
||||
// let (allocator, alloc_parms) = pool.allocator().unwrap();
|
||||
pool.set_active(true).unwrap();
|
||||
pool
|
||||
});
|
||||
let mut gst_buf = pool.acquire_buffer(None).unwrap();
|
||||
// let mut gst_buf = gstreamer::Buffer::with_size(data.data.len()).unwrap();
|
||||
{
|
||||
let gst_buf_mut = gst_buf.get_mut().unwrap();
|
||||
let time = ClockTime::from_useconds(rt.as_micros() as u64);
|
||||
// gst_buf_mut.set_dts(ClockTime::from_useconds(dts));
|
||||
gst_buf_mut.set_dts(time);
|
||||
gst_buf_mut.set_pts(time);
|
||||
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
|
||||
gst_buf_data.copy_from_slice(data.data.as_slice());
|
||||
}
|
||||
gst_buf
|
||||
};
|
||||
|
||||
match appsrc.push_buffer(buf) {
|
||||
Ok(_) => {
|
||||
// log::info!(
|
||||
// "Send {}{} on {}",
|
||||
// data.data.len(),
|
||||
// if data.keyframe { " (keyframe)" } else { "" },
|
||||
// appsrc.name()
|
||||
// );
|
||||
Ok(())
|
||||
}
|
||||
Err(FlowError::Flushing) => {
|
||||
// Buffer is full just skip
|
||||
//
|
||||
// But ensure we start with an iframe to reduce gray screens
|
||||
wait_for_iframe = true;
|
||||
log::info!("Buffer full on {}", appsrc.name());
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => Err(anyhow!("Error in streaming: {e:?}")),
|
||||
}?;
|
||||
if !inited {
|
||||
if keyframes_pushed > 1
|
||||
|| appsrc.current_level_bytes() >= appsrc.max_bytes() * 2 / 3
|
||||
{
|
||||
// log::info!("=== Playing state on {}", appsrc.name());
|
||||
// log::info!(
|
||||
// "Buffer filled to {} of {} ({} %) in {} keyframes",
|
||||
// appsrc.current_level_bytes(),
|
||||
// appsrc.max_bytes(),
|
||||
// appsrc.current_level_bytes() * 100 / appsrc.max_bytes(),
|
||||
// keyframes_pushed
|
||||
// );
|
||||
inited = true;
|
||||
// appsrc.set_state(gstreamer::State::Playing).unwrap();
|
||||
} else if data.keyframe {
|
||||
// log::info!(
|
||||
// "Buffer filling to {} of {} ({} %) in {} keyframes on {}",
|
||||
// appsrc.current_level_bytes(),
|
||||
// appsrc.max_bytes(),
|
||||
// appsrc.current_level_bytes() * 100 / appsrc.max_bytes(),
|
||||
// keyframes_pushed,
|
||||
// appsrc.name()
|
||||
// );
|
||||
keyframes_pushed += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
AnyResult::Ok(())
|
||||
})();
|
||||
log::error!("r: {:?}", r);
|
||||
r
|
||||
});
|
||||
|
||||
// Send to the blocking thread
|
||||
while let Some(Ok(data)) = stream.next().await {
|
||||
// Start on iframes
|
||||
if tx.send(data).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
log::error!("Exiting send_to_appsrc");
|
||||
thread.await??;
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user