Compare commits
117
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e7a244a1ab | ||
|
|
b978809a59 | ||
|
|
6b35db8fbe | ||
|
|
c3f3ecdb83 | ||
|
|
f99df6e5e7 | ||
|
|
e09d9d30dc | ||
|
|
c2f0b5b2d9 | ||
|
|
472eda9f06 | ||
|
|
c831d07573 | ||
|
|
90b51b7e46 | ||
|
|
cdc8a9cb8e | ||
|
|
790eb655a3 | ||
|
|
99584ce813 | ||
|
|
c2a5798905 | ||
|
|
951a822973 | ||
|
|
f5ac615b3c | ||
|
|
3b677f9156 | ||
|
|
9473a8ad5d | ||
|
|
56ef79ff45 | ||
|
|
d61e48ce79 | ||
|
|
86f15f9eb1 | ||
|
|
84e10530a5 | ||
|
|
2f74a720f5 | ||
|
|
511e50fe52 | ||
|
|
78da59fc71 | ||
|
|
36f70c3222 | ||
|
|
279c1c5bf1 | ||
|
|
f5ba6e244f | ||
|
|
6a980b47ae | ||
|
|
3a783bf50c | ||
|
|
9dabb43a9c | ||
|
|
b9fd2f75ff | ||
|
|
61f65ed42a | ||
|
|
634bff08af | ||
|
|
2d1ccf518a | ||
|
|
d9600d0607 | ||
|
|
72cf358b0c | ||
|
|
a819248556 | ||
|
|
6e334392da | ||
|
|
eed2aeb1be | ||
|
|
84163dcd2a | ||
|
|
30573c7ba3 | ||
|
|
c3c58b3f67 | ||
|
|
bc3f9b65a0 | ||
|
|
5513fce18f | ||
|
|
df3462875b | ||
|
|
e2d5eb061f | ||
|
|
2921ede080 | ||
|
|
004405e868 | ||
|
|
d411e9c25b | ||
|
|
e712e3b47c | ||
|
|
b09a590fb6 | ||
|
|
f9407d8d8d | ||
|
|
eec01faa0a | ||
|
|
d2daad0bf2 | ||
|
|
55d357ecf8 | ||
|
|
15f9993c12 | ||
|
|
52840348bf | ||
|
|
d5cb12dc3c | ||
|
|
9f1cda3c60 | ||
|
|
5e81d6a369 | ||
|
|
7a50d3e42f | ||
|
|
587c8bf334 | ||
|
|
dbf21d9c90 | ||
|
|
60e4b994ab | ||
|
|
8c061efc1e | ||
|
|
262d2b2ed4 | ||
|
|
b07aa2ddfe | ||
|
|
386fec4220 | ||
|
|
e2602b5c17 | ||
|
|
26d894222a | ||
|
|
c20b944411 | ||
|
|
f60de36bae | ||
|
|
929dac374b | ||
|
|
fd3282efe2 | ||
|
|
359597131e | ||
|
|
0b922aeeb5 | ||
|
|
29b6ff8c24 | ||
|
|
ae3f9365b1 | ||
|
|
c666a4c3bd | ||
|
|
e4bcf9bd23 | ||
|
|
8a0d4a8f03 | ||
|
|
a6e11af084 | ||
|
|
2403ee2b27 | ||
|
|
9d57b2dafd | ||
|
|
86f6ea55ec | ||
|
|
2ff1ee474e | ||
|
|
ccd5778f57 | ||
|
|
af9f181ff5 | ||
|
|
d50fa87baf | ||
|
|
f2b609f12c | ||
|
|
c53b0955f8 | ||
|
|
dc6860378d | ||
|
|
84bf5ef05c | ||
|
|
31ac6e4707 | ||
|
|
048af92808 | ||
|
|
4a08b8148b | ||
|
|
cce519de6d | ||
|
|
d24a4d12e2 | ||
|
|
8cea9e79e5 | ||
|
|
a222a2d053 | ||
|
|
093a1b1af9 | ||
|
|
c30e6293f6 | ||
|
|
03a7e1473a | ||
|
|
5ccdc27d37 | ||
|
|
107902ef3a | ||
|
|
29b47aa44e | ||
|
|
427046239c | ||
|
|
c4c35d1bde | ||
|
|
81a573ba6d | ||
|
|
7d771fc94b | ||
|
|
4f50a962e6 | ||
|
|
7468d77178 | ||
|
|
19a468ac2e | ||
|
|
a46192d47c | ||
|
|
45fec7cfc2 | ||
|
|
3f4bc510a7 |
No files matched your search
+4
-1
@@ -1,5 +1,5 @@
|
||||
[target.x86_64-pc-windows-msvc]
|
||||
rustflags = ["-C", "target-feature=+crt-static"]
|
||||
rustflags = ["-C", "target-feature=+crt-static", "--cfg", "tokio_unstable"]
|
||||
|
||||
# TODO not good as a default
|
||||
[target.armv7-unknown-linux-gnueabihf]
|
||||
@@ -10,3 +10,6 @@ linker = "aarch64-linux-gnu-gcc"
|
||||
|
||||
[target.i686-unknown-linux-gnu]
|
||||
linker = "i686-linux-gnu-gcc"
|
||||
|
||||
[build]
|
||||
rustflags = ["--cfg", "tokio_unstable"]
|
||||
+53
-25
@@ -3,7 +3,23 @@ name: CI
|
||||
on: [push, pull_request, workflow_dispatch]
|
||||
|
||||
jobs:
|
||||
pre_job:
|
||||
# continue-on-error: true # Uncomment once integration is finished
|
||||
runs-on: ubuntu-latest
|
||||
# Map a step output to a job output
|
||||
outputs:
|
||||
should_skip: ${{ steps.skip_check.outputs.should_skip }} && ! (startsWith(github.ref, 'refs/tags/') && github.event_name == 'push')
|
||||
steps:
|
||||
- id: skip_check
|
||||
uses: fkirc/skip-duplicate-actions@v5
|
||||
with:
|
||||
# All of these options are optional, so you can remove them if you are happy with the defaults
|
||||
concurrent_skipping: 'same_content_newer'
|
||||
skip_after_successful_duplicate: 'false'
|
||||
|
||||
native:
|
||||
needs: pre_job
|
||||
if: needs.pre_job.outputs.should_skip != 'true'
|
||||
name: native
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
@@ -62,13 +78,15 @@ jobs:
|
||||
echo "PATH=${PATH}"
|
||||
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
|
||||
# pkg-config --variable pc_path pkg-config
|
||||
cargo build --release --all-features
|
||||
- uses: actions/upload-artifact@v3
|
||||
with:
|
||||
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-bullseye-slim"
|
||||
@@ -126,7 +144,7 @@ jobs:
|
||||
TARGET: ${{ matrix.target }}
|
||||
- name: Build
|
||||
run: |
|
||||
cargo build --release --all-features --target=${TARGET}
|
||||
cargo build --verbose --release --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
|
||||
@@ -134,16 +152,15 @@ jobs:
|
||||
TARGET: ${{ matrix.target }}
|
||||
- uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: release-${{ matrix.arch }}-buster
|
||||
name: release-${{ matrix.arch }}-bullseye
|
||||
path: "target/${{ matrix.target }}/release/neolink*"
|
||||
|
||||
push_to_registry:
|
||||
name: Build Docker image
|
||||
if: github.event_name == 'push'
|
||||
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
|
||||
runs-on: ubuntu-latest
|
||||
needs:
|
||||
- "cross"
|
||||
- "native"
|
||||
steps:
|
||||
- name: Check token is set
|
||||
id: vars
|
||||
@@ -185,18 +202,29 @@ jobs:
|
||||
id: tags
|
||||
shell: bash
|
||||
run: |
|
||||
tags=""
|
||||
if [[ "${GITHUB_REF}" =~ ^'refs/tags/'(.*) ]]; then
|
||||
gittag="$(echo "${BASH_REMATCH[1]}" | sed -E 's|[/]|-|g')"
|
||||
tags="${REPO_NAME}:${gittag}"
|
||||
elif [[ "${GITHUB_REF}" =~ ^'refs/heads/'(.*) ]]; then
|
||||
branch="$(echo "${BASH_REMATCH[1]}" | sed -E 's|[/]|-|g')"
|
||||
tags="${REPO_NAME}:${branch}"
|
||||
tags=( )
|
||||
gittag="$(git tag --points-at HEAD)"
|
||||
branch="$(git rev-parse --abbrev-ref HEAD)"
|
||||
|
||||
if [[ -n "${gittag}" ]]; then
|
||||
tags=( "${tags[@]}" "${REPO_NAME}:${gittag}" )
|
||||
fi
|
||||
if [[ -n "${branch}" ]]; then
|
||||
branch="$(echo "${branch}" | sed -E 's|[/]|-|g')"
|
||||
tags=( "${tags[@]}" "${REPO_NAME}:${branch}" )
|
||||
if [ "${branch}" == "master" ]; then
|
||||
tags="${tags},${REPO_NAME}:latest"
|
||||
tags=( "${tags[@]}" "${tags},${REPO_NAME}:latest" )
|
||||
fi
|
||||
fi
|
||||
echo "TAGS=${tags}" >> "${GITHUB_OUTPUT}"
|
||||
function join_by {
|
||||
local d=${1-} f=${2-}
|
||||
if shift 2; then
|
||||
printf %s "$f" "${@/#/$d}"
|
||||
fi
|
||||
}
|
||||
tagstr="$(join_by "," "${tags[@]}")"
|
||||
echo "TAGS=${tagstr}"
|
||||
echo "TAGS=${tagstr}" >> "${GITHUB_OUTPUT}"
|
||||
env:
|
||||
REPO_NAME: ${{ steps.string_repo.outputs.lowercase }}
|
||||
- name: Install latest rust
|
||||
@@ -218,22 +246,22 @@ jobs:
|
||||
- name: Download Linux x86_64
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: release-amd64-buster
|
||||
name: release-amd64-bullseye
|
||||
path: linux/amd64
|
||||
- name: Download Linux armv7
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: release-armhf-buster
|
||||
name: release-armhf-bullseye
|
||||
path: linux/arm/v7
|
||||
- name: Download Linux arm64
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: release-arm64-buster
|
||||
name: release-arm64-bullseye
|
||||
path: linux/arm64
|
||||
- name: Download Linux i386
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: release-i386-buster
|
||||
name: release-i386-bullseye
|
||||
path: linux/386
|
||||
- name: Push to Docker Hub
|
||||
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
|
||||
@@ -287,22 +315,22 @@ jobs:
|
||||
- name: Download Linux x86_64
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: release-amd64-buster
|
||||
path: neolink_linux_x86_64_buster
|
||||
name: release-amd64-bullseye
|
||||
path: neolink_linux_x86_64_bullseye
|
||||
- name: Download Linux armhf
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: release-armhf-buster
|
||||
name: release-armhf-bullseye
|
||||
path: neolink_linux_armhf
|
||||
- name: Download Linux arm64
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: release-arm64-buster
|
||||
name: release-arm64-bullseye
|
||||
path: neolink_linux_arm64
|
||||
- name: Download Linux i386
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: release-i386-buster
|
||||
name: release-i386-bullseye
|
||||
path: neolink_linux_i386
|
||||
- name: Prepare Releases
|
||||
run: |
|
||||
@@ -310,7 +338,7 @@ jobs:
|
||||
neolink_windows
|
||||
neolink_macos
|
||||
neolink_linux_x86_64_ubuntu
|
||||
neolink_linux_x86_64_buster
|
||||
neolink_linux_x86_64_bullseye
|
||||
neolink_linux_armhf
|
||||
neolink_linux_arm64
|
||||
neolink_linux_i386
|
||||
@@ -327,7 +355,7 @@ jobs:
|
||||
neolink_windows.zip
|
||||
neolink_macos.zip
|
||||
neolink_linux_x86_64_ubuntu.zip
|
||||
neolink_linux_x86_64_buster.zip
|
||||
neolink_linux_x86_64_bullseye.zip
|
||||
neolink_linux_armhf.zip
|
||||
neolink_linux_arm64.zip
|
||||
neolink_linux_i386.zip
|
||||
|
||||
@@ -3,7 +3,23 @@ name: DockerHub
|
||||
on: [push, pull_request, workflow_dispatch]
|
||||
|
||||
jobs:
|
||||
pre_job:
|
||||
# continue-on-error: true # Uncomment once integration is finished
|
||||
runs-on: ubuntu-latest
|
||||
# Map a step output to a job output
|
||||
outputs:
|
||||
should_skip: ${{ steps.skip_check.outputs.should_skip }}
|
||||
steps:
|
||||
- id: skip_check
|
||||
uses: fkirc/skip-duplicate-actions@v5
|
||||
with:
|
||||
# All of these options are optional, so you can remove them if you are happy with the defaults
|
||||
concurrent_skipping: 'same_content_newer'
|
||||
skip_after_successful_duplicate: 'false'
|
||||
|
||||
native:
|
||||
needs: pre_job
|
||||
if: needs.pre_job.outputs.should_skip != 'true'
|
||||
name: native
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
@@ -96,26 +112,30 @@ jobs:
|
||||
# build a list of images from tags
|
||||
for j in ${IMAGE_TAGS}
|
||||
do
|
||||
if [[ "${j}" =~ ^[0-9][.][0-9][.][0-9]$ ]]; then
|
||||
echo "Version tag (${j}) will not be deleted"
|
||||
continue
|
||||
fi
|
||||
echo
|
||||
# add last_updated_time
|
||||
updated_time="$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/?page_size=10000" | jq -r '.last_updated')"
|
||||
echo "$updated_time"
|
||||
datetime=$updated_time
|
||||
timeago="${DAYS} days ago"
|
||||
updated_time="$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/?page_size=10000" | jq -r '.last_updated')"
|
||||
echo "$updated_time"
|
||||
datetime=$updated_time
|
||||
timeago="${DAYS} days ago"
|
||||
|
||||
dtSec=$(date --date "$datetime" +'%s')
|
||||
taSec=$(date --date "$timeago" +'%s')
|
||||
dtSec=$(date --date "$datetime" +'%s')
|
||||
taSec=$(date --date "$timeago" +'%s')
|
||||
|
||||
echo "INFO: dtSec=$dtSec, taSec=$taSec"
|
||||
echo "INFO: dtSec=$dtSec, taSec=$taSec"
|
||||
|
||||
if [ "$dtSec" -lt "$taSec" ]
|
||||
then
|
||||
echo "This image ${UNAME}/${i}:${j} is older than ${DAYS} days, deleting this image"
|
||||
## Please uncomment below line to delete docker hub images of docker hub repositories
|
||||
curl -s -X DELETE -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/"
|
||||
else
|
||||
echo "This image ${UNAME}/${i}:${j} is within ${DAYS} days time range, keep this image"
|
||||
fi
|
||||
if [ "$dtSec" -lt "$taSec" ]
|
||||
then
|
||||
echo "This image ${UNAME}/${i}:${j} is older than ${DAYS} days, deleting this image"
|
||||
## Please uncomment below line to delete docker hub images of docker hub repositories
|
||||
curl -s -X DELETE -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/"
|
||||
else
|
||||
echo "This image ${UNAME}/${i}:${j} is within ${DAYS} days time range, keep this image"
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
@@ -124,4 +144,4 @@ jobs:
|
||||
DAYS: "100"
|
||||
REPO_NAME: ${{ steps.string_repo.outputs.lowercase }}
|
||||
OWNER: ${{ steps.string_user.outputs.lowercase }}
|
||||
TOKEN: ${{ secrets.DOCKER_PRUNE_TOKEN }}
|
||||
TOKEN: ${{ secrets.DOCKER_PRUNE__TOKEN }}
|
||||
@@ -3,16 +3,32 @@ name: Style
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
pre_job:
|
||||
# continue-on-error: true # Uncomment once integration is finished
|
||||
runs-on: ubuntu-latest
|
||||
# Map a step output to a job output
|
||||
outputs:
|
||||
should_skip: ${{ steps.skip_check.outputs.should_skip }}
|
||||
steps:
|
||||
- id: skip_check
|
||||
uses: fkirc/skip-duplicate-actions@v5
|
||||
with:
|
||||
# All of these options are optional, so you can remove them if you are happy with the defaults
|
||||
concurrent_skipping: 'same_content_newer'
|
||||
skip_after_successful_duplicate: 'false'
|
||||
|
||||
check_clippy:
|
||||
needs: pre_job
|
||||
if: needs.pre_job.outputs.should_skip != 'true'
|
||||
name: Clippy
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: apt install gstreamer
|
||||
- name: apt install gstreamer and deps
|
||||
run: |
|
||||
sudo apt update
|
||||
sudo apt install -y aptitude
|
||||
sudo aptitude install -y libgstrtspserver-1.0-dev libgstreamer1.0-dev libgtk2.0-dev
|
||||
sudo aptitude install -y libgstrtspserver-1.0-dev libgstreamer1.0-dev libgtk2.0-dev protobuf-compiler
|
||||
- name: Install nightly rust
|
||||
run: |
|
||||
rustup toolchain install nightly --component clippy
|
||||
@@ -21,6 +37,8 @@ jobs:
|
||||
run: cargo +nightly clippy --workspace --all-targets --all-features
|
||||
|
||||
check_fmt:
|
||||
needs: pre_job
|
||||
if: needs.pre_job.outputs.should_skip != 'true'
|
||||
name: Rust-fmt
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
@@ -34,6 +52,8 @@ jobs:
|
||||
cargo +nightly fmt --all -- --check
|
||||
|
||||
check_lua:
|
||||
needs: pre_job
|
||||
if: needs.pre_job.outputs.should_skip != 'true'
|
||||
name: Luacheck
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
||||
@@ -20,3 +20,6 @@
|
||||
._.DS_Store
|
||||
**/.DS_Store
|
||||
**/._.DS_Store
|
||||
|
||||
# The token file for the FCM tests
|
||||
token.toml
|
||||
@@ -0,0 +1,2 @@
|
||||
MD013:
|
||||
code_blocks: false
|
||||
@@ -16,4 +16,5 @@ repos:
|
||||
hooks:
|
||||
- id: markdownlint-fix
|
||||
files: README.md
|
||||
args: [--disable, MD041, --]
|
||||
|
||||
Generated
+1052
-23
File diff suppressed because it is too large.
Load diff
+8
-5
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "neolink"
|
||||
description = "A standards-compliant bridge to Reolink IP cameras"
|
||||
version = "0.5.8"
|
||||
version = "0.5.11"
|
||||
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
|
||||
edition = "2018"
|
||||
license = "AGPL-3.0-or-later"
|
||||
@@ -15,21 +15,24 @@ members = [
|
||||
anyhow = "1.0.70"
|
||||
byte-slice-cast = "1.2.2"
|
||||
clap = { version = "4.2.2", features = ["derive", "cargo"] }
|
||||
console-subscriber = "0.1.8"
|
||||
crossbeam-channel = "0.5.8"
|
||||
env_logger = "*"
|
||||
env_logger = "0.10.0"
|
||||
futures = "0.3.28"
|
||||
gstreamer = "0.20.3"
|
||||
gstreamer-app = "0.20.0"
|
||||
gstreamer-rtsp = "0.20.0"
|
||||
gstreamer-rtsp-server = { version = "0.20.3", features = ["v1_16"] }
|
||||
is_sorted = "0.1.1"
|
||||
itertools = "0.10.5"
|
||||
lazy_static = "1.4.0"
|
||||
log = { version = "0.4.17" }
|
||||
neolink_core = { path = "crates/core", version = "0.5.8" }
|
||||
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
|
||||
neolink_core = { path = "crates/core", version = "0.5.11" }
|
||||
regex = "1.7.3"
|
||||
rumqttc = "0.20.0"
|
||||
serde = { version = "1.0.160", features = ["derive"] }
|
||||
time = "0.3.20"
|
||||
tokio = { version = "1.27.0", features = ["rt", "macros", "io-util"] }
|
||||
tokio = { version = "1.27.0", features = ["rt-multi-thread", "macros", "io-util", "tracing"] }
|
||||
tokio-stream = "0.1.12"
|
||||
toml = "0.7.3"
|
||||
validator = "0.16.0"
|
||||
|
||||
+2
-2
@@ -4,7 +4,7 @@
|
||||
# Miroslav Šedivý
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
FROM docker.io/rust:slim-buster AS build
|
||||
FROM docker.io/rust:slim-bullseye AS build
|
||||
ARG TARGETPLATFORM
|
||||
|
||||
ENV DEBIAN_FRONTEND=noninteractive
|
||||
@@ -34,7 +34,7 @@ RUN echo "TARGETPLATFORM: ${TARGETPLATFORM}"; \
|
||||
fi
|
||||
|
||||
# Create the release container. Match the base OS used to build
|
||||
FROM debian:buster-slim
|
||||
FROM debian:bullseye-slim
|
||||
ARG TARGETPLATFORM
|
||||
ARG REPO
|
||||
ARG VERSION
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "neolink_core"
|
||||
description = "Core services and structure for Reolink IP cameras"
|
||||
version = "0.5.8"
|
||||
version = "0.5.11"
|
||||
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
|
||||
edition = "2018"
|
||||
license = "AGPL-3.0-or-later"
|
||||
@@ -28,7 +28,7 @@ serde = { version = "1.0.106", features = ["derive"] }
|
||||
time = { version = "0.3.20" , 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"] }
|
||||
tokio-util = { version = "0.7.7", features = ["full", "tracing"] }
|
||||
yaserde = "0.8.0"
|
||||
yaserde_derive = "0.8.0"
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::bc::model::*;
|
||||
use crate::bc::xml::*;
|
||||
use crate::{Credentials, Error, Result};
|
||||
use bytes::BytesMut;
|
||||
use std::io;
|
||||
use nom::AsBytes;
|
||||
use tokio_util::codec::{Decoder, Encoder};
|
||||
|
||||
pub(crate) struct BcCodex {
|
||||
@@ -54,14 +54,25 @@ impl Decoder for BcCodex {
|
||||
if buf.is_empty() {
|
||||
Ok(None)
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("bytes remaining on BC stream: {:X?}", buf),
|
||||
)
|
||||
.into())
|
||||
log::debug!(
|
||||
"bytes remaining on BC stream: {:X?}",
|
||||
buf.as_bytes().chunks(25).next()
|
||||
);
|
||||
// Right after this we seem to get an issue with the camera dropping us
|
||||
// Needs probing
|
||||
// F0, DE, BC, A, 3, 0, 0, 0, 88, 6, 0, 0, 0, 1, 4, 0, C8, 0, 0, 0, 0, 0, 0, 0, 30, 31, 64, 63, 48,
|
||||
// 32, 36, 34, 6A, 6, 0, 0, 0, 0, 0, 0, D8, F5, C7, 86, 56, 0, 0, 0, 0, 0, 0, 1, 21, 9A, FC, 22, 7F, 6, AE, F6, 15, FF, E5, 71, 4, 2F, 24, 61, 15, 96, F0, BF, 83, DE, 10, BE, B4, 2E, 3
|
||||
// 9, 76, 56, 92, 7E, 48, 79, 20, 9A, DC, 1B, BB, AC, 22, 60, 5C, 72, B5, 3D, 8, E0, 34, 43, 3F, 2E, A7, 81, A8, 11, 75, 7F, 58, 3E, 8, 54, 91, 43, 21, EC, 6B, D6, 1A, D5, CB, D5, 6C,
|
||||
// 8C, 2E, 6E, A3, 51, C3, A4, F0, CF, 2B, 61, 81, D0, 1C, A1, 76, EE, BF, 7A, D5, D8, D1, C4, D, B0, 45, EE, 3E, 93, 9A, CE, 5F, AB, 75, 55, AC, 9D, 66, DE, 23, 6D, 5F, 25, 57, DA, F5
|
||||
//, E, 7F, 8D, 30, A7, 66, C4, 60, 76, 41, D0, 6A, 23, E, A9, C5, 51, EE, F6, DD, 19, E7, A8, 96, 9F, 2B, AF, 31, 90, 9D, FC, BE
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
// match self.decode(buf)? {
|
||||
// Some(frame) => Ok(Some(frame)),
|
||||
// None => Ok(None),
|
||||
// }
|
||||
}
|
||||
|
||||
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
|
||||
|
||||
@@ -35,6 +35,8 @@ pub const MSG_ID_GET_GENERAL: u32 = 104;
|
||||
pub const MSG_ID_ABILITY_INFO: u32 = 151;
|
||||
/// Setting general system info (clock mostly) messages have this ID
|
||||
pub const MSG_ID_SET_GENERAL: u32 = 105;
|
||||
/// Used to pass the token and client ID for push notifications
|
||||
pub const MSG_ID_PUSH_INFO: u32 = 124;
|
||||
/// Will send the talk config for talk back data to follow this msg
|
||||
pub const MSG_ID_TALKCONFIG: u32 = 201;
|
||||
/// Used to send talk back binary data
|
||||
|
||||
@@ -85,6 +85,9 @@ pub struct BcXml {
|
||||
/// Recieved on request for a users persmissions/capabilitoes
|
||||
#[yaserde(rename = "AbilityInfo")]
|
||||
pub ability_info: Option<AbilityInfo>,
|
||||
/// Recieved on request for a users persmissions/capabilitoes
|
||||
#[yaserde(rename = "PushInfo")]
|
||||
pub push_info: Option<PushInfo>,
|
||||
}
|
||||
|
||||
impl BcXml {
|
||||
@@ -614,6 +617,19 @@ pub struct AbilityInfoSubModule {
|
||||
pub ability_value: String,
|
||||
}
|
||||
|
||||
/// PushInfo XML
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
pub struct PushInfo {
|
||||
/// The token from FCM registration
|
||||
pub token: String,
|
||||
/// The phone type, known values: `reo_iphone`
|
||||
#[yaserde(rename = "phoneType")]
|
||||
pub phone_type: String,
|
||||
/// A client ID, seems to be an all CAPS MD5 hash of something
|
||||
#[yaserde(rename = "clientID")]
|
||||
pub client_id: String,
|
||||
}
|
||||
|
||||
/// Convience function to return the xml version used throughout the library
|
||||
pub fn xml_ver() -> String {
|
||||
"1.1".to_string()
|
||||
|
||||
+54
-103
@@ -24,6 +24,7 @@ mod motion;
|
||||
mod ping;
|
||||
mod pirstate;
|
||||
mod ptz;
|
||||
mod pushinfo;
|
||||
mod reboot;
|
||||
mod resolution;
|
||||
mod stream;
|
||||
@@ -39,6 +40,7 @@ pub use login::MaxEncryption;
|
||||
pub use motion::{MotionData, MotionStatus};
|
||||
pub use pirstate::PirState;
|
||||
pub use ptz::Direction;
|
||||
pub use pushinfo::PhoneType;
|
||||
pub use resolution::*;
|
||||
use std::sync::Arc;
|
||||
pub use stream::{StreamData, StreamKind};
|
||||
@@ -124,12 +126,6 @@ impl BcCamera {
|
||||
/// Try to connect to the camera via appropaite methods and return
|
||||
/// the location that should be used
|
||||
async fn find_camera(options: &BcCameraOpt) -> Result<CameraLocation> {
|
||||
let mut tcp_set = tokio::task::JoinSet::new();
|
||||
let mut local_set = tokio::task::JoinSet::new();
|
||||
let mut remote_set = tokio::task::JoinSet::new();
|
||||
let mut map_set = tokio::task::JoinSet::new();
|
||||
let mut relay_set = tokio::task::JoinSet::new();
|
||||
|
||||
if let ConnectionProtocol::Tcp | ConnectionProtocol::TcpUdp = options.protocol {
|
||||
let mut sockets = vec![];
|
||||
match options.port {
|
||||
@@ -145,15 +141,15 @@ impl BcCamera {
|
||||
}
|
||||
}
|
||||
}
|
||||
info!("{}: Trying TCP discovery", options.name);
|
||||
for socket in sockets.drain(..) {
|
||||
let channel_id: u8 = options.channel_id;
|
||||
let name: String = options.name.clone();
|
||||
tcp_set.spawn(async move {
|
||||
Discovery::check_tcp(socket, channel_id).await.map(|_| {
|
||||
info!("{}: TCP Discovery success at {:?}", name, &socket);
|
||||
socket
|
||||
})
|
||||
});
|
||||
if let Ok(addr) = Discovery::check_tcp(socket, channel_id).await.map(|_| {
|
||||
info!("{}: TCP Discovery success at {:?}", options.name, &socket);
|
||||
socket
|
||||
}) {
|
||||
return Ok(CameraLocation::Tcp(addr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,108 +183,63 @@ impl BcCamera {
|
||||
|
||||
if allow_local {
|
||||
let uid_local = uid.clone();
|
||||
let name: String = options.name.clone();
|
||||
local_set.spawn(async move {
|
||||
trace!("{}: Starting Local discovery", name);
|
||||
let result = Discovery::local(&uid_local, Some(sockets)).await;
|
||||
if let Ok(disc) = &result {
|
||||
info!(
|
||||
"{}: Local discovery success {} at {}",
|
||||
name,
|
||||
uid_local,
|
||||
disc.get_addr()
|
||||
);
|
||||
}
|
||||
result
|
||||
});
|
||||
info!("{}: Trying local discovery", options.name);
|
||||
let result = Discovery::local(&uid_local, Some(sockets)).await;
|
||||
if let Ok(disc) = result {
|
||||
info!(
|
||||
"{}: Local discovery success {} at {}",
|
||||
options.name,
|
||||
uid_local,
|
||||
disc.get_addr()
|
||||
);
|
||||
return Ok(CameraLocation::Udp(disc));
|
||||
}
|
||||
}
|
||||
if allow_remote {
|
||||
let uid_remote = uid.clone();
|
||||
let name: String = options.name.clone();
|
||||
remote_set.spawn(async move {
|
||||
trace!("Starting Remote discovery");
|
||||
let result = Discovery::remote(&uid_remote).await;
|
||||
if let Ok(disc) = &result {
|
||||
info!(
|
||||
"{}: Remote discovery success {} at {}",
|
||||
name,
|
||||
uid_remote,
|
||||
disc.get_addr()
|
||||
);
|
||||
}
|
||||
result
|
||||
});
|
||||
info!("{}: Trying remote discovery", options.name);
|
||||
let result = Discovery::remote(&uid_remote).await;
|
||||
if let Ok(disc) = result {
|
||||
info!(
|
||||
"{}: Remote discovery success {} at {}",
|
||||
options.name,
|
||||
uid_remote,
|
||||
disc.get_addr()
|
||||
);
|
||||
return Ok(CameraLocation::Udp(disc));
|
||||
}
|
||||
}
|
||||
if allow_map {
|
||||
let uid_map = uid.clone();
|
||||
let name: String = options.name.clone();
|
||||
map_set.spawn(async move {
|
||||
trace!("Starting Map");
|
||||
let result = Discovery::map(&uid_map).await;
|
||||
if let Ok(disc) = &result {
|
||||
info!("{}: Map success {} at {}", name, uid_map, disc.get_addr());
|
||||
}
|
||||
result
|
||||
});
|
||||
info!("{}: Trying map discovery", options.name);
|
||||
let result = Discovery::map(&uid_map).await;
|
||||
if let Ok(disc) = result {
|
||||
info!(
|
||||
"{}: Map success {} at {}",
|
||||
options.name,
|
||||
uid_map,
|
||||
disc.get_addr()
|
||||
);
|
||||
return Ok(CameraLocation::Udp(disc));
|
||||
}
|
||||
}
|
||||
if allow_relay {
|
||||
let uid_relay = uid.clone();
|
||||
let name: String = options.name.clone();
|
||||
relay_set.spawn(async move {
|
||||
trace!("Starting Relay");
|
||||
let result = Discovery::relay(&uid_relay).await;
|
||||
if let Ok(disc) = &result {
|
||||
info!(
|
||||
"{}: Relay success {} at {}",
|
||||
name,
|
||||
uid_relay,
|
||||
disc.get_addr()
|
||||
);
|
||||
}
|
||||
result
|
||||
});
|
||||
info!("{}: Trying relay discovery", options.name);
|
||||
let result = Discovery::relay(&uid_relay).await;
|
||||
if let Ok(disc) = result {
|
||||
info!(
|
||||
"{}: Relay success {} at {}",
|
||||
options.name,
|
||||
uid_relay,
|
||||
disc.get_addr()
|
||||
);
|
||||
return Ok(CameraLocation::Udp(disc));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for all TCP to finish
|
||||
while let Some(result) = tcp_set.join_next().await {
|
||||
if let Ok(Ok(addr)) = result {
|
||||
return Ok(CameraLocation::Tcp(addr));
|
||||
}
|
||||
}
|
||||
debug!("TCP fail");
|
||||
// Tcp failed see if Local faired any better
|
||||
// Wait for all local to finish
|
||||
while let Some(result) = local_set.join_next().await {
|
||||
if let Ok(Ok(discovery)) = result {
|
||||
return Ok(CameraLocation::Udp(discovery));
|
||||
}
|
||||
}
|
||||
debug!("Local fail");
|
||||
// Local failed see if Remote faired any better
|
||||
// Wait for all remote to finish
|
||||
while let Some(result) = remote_set.join_next().await {
|
||||
if let Ok(Ok(discovery)) = result {
|
||||
return Ok(CameraLocation::Udp(discovery));
|
||||
}
|
||||
}
|
||||
debug!("Remote fail");
|
||||
// Remote failed see if Map faired any better
|
||||
// Wait for all Map to finish
|
||||
while let Some(result) = map_set.join_next().await {
|
||||
if let Ok(Ok(discovery)) = result {
|
||||
return Ok(CameraLocation::Udp(discovery));
|
||||
}
|
||||
}
|
||||
debug!("Map fail");
|
||||
// Map failed see if Relay faired any better
|
||||
// Wait for all Relay to finish
|
||||
while let Some(result) = relay_set.join_next().await {
|
||||
if let Ok(Ok(discovery)) = result {
|
||||
return Ok(CameraLocation::Udp(discovery));
|
||||
}
|
||||
}
|
||||
debug!("Relay (All) fail");
|
||||
info!("{}: Discovery failed", options.name);
|
||||
// Nothing works
|
||||
Err(Error::CannotInitCamera)
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ use futures::stream::{Stream, StreamExt};
|
||||
use log::*;
|
||||
use std::collections::btree_map::Entry;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc::{channel, Sender};
|
||||
use tokio::task::yield_now;
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
use tokio_util::sync::PollSender;
|
||||
|
||||
use tokio::{sync::RwLock, task::JoinSet};
|
||||
|
||||
@@ -16,9 +16,9 @@ type MsgHandler = Box<dyn Fn(&Bc) -> Option<Bc> + Send + Sync>;
|
||||
#[derive(Default)]
|
||||
struct Subscriber {
|
||||
/// Subscribers based on their Num
|
||||
num: RwLock<BTreeMap<u16, Sender<Result<Bc>>>>,
|
||||
num: BTreeMap<u16, Sender<Result<Bc>>>,
|
||||
/// Subscribers based on their ID
|
||||
id: RwLock<BTreeMap<u32, MsgHandler>>,
|
||||
id: BTreeMap<u32, MsgHandler>,
|
||||
}
|
||||
|
||||
pub(crate) type BcConnSink = Box<dyn Sink<Bc, Error = Error> + Send + Sync + Unpin>;
|
||||
@@ -31,35 +31,48 @@ pub(crate) type BcConnSource = Box<dyn Stream<Item = Result<Bc>> + Send + Sync +
|
||||
/// There can be only one subscriber per kind of message at a time.
|
||||
pub struct BcConnection {
|
||||
sink: Sender<Result<Bc>>,
|
||||
subscribers: Arc<Subscriber>,
|
||||
#[allow(dead_code)] // Not dead we just need to hold a reference to keep it alive
|
||||
poll_commander: Sender<PollCommand>,
|
||||
rx_thread: RwLock<JoinSet<Result<()>>>,
|
||||
}
|
||||
|
||||
impl BcConnection {
|
||||
pub async fn new(mut sink: BcConnSink, mut source: BcConnSource) -> Result<BcConnection> {
|
||||
let subscribers: Arc<Subscriber> = Default::default();
|
||||
let subs = subscribers.clone();
|
||||
pub async fn new(mut sink: BcConnSink, source: BcConnSource) -> Result<BcConnection> {
|
||||
let (sinker, sinker_rx) = channel::<Result<Bc>>(100);
|
||||
let mut rx_thread = JoinSet::new();
|
||||
let sink_thread = sinker.clone();
|
||||
|
||||
let (poll_commander, poll_commanded) = channel(200);
|
||||
let mut poller = Poller {
|
||||
subscribers: Default::default(),
|
||||
sink: sinker.clone(),
|
||||
reciever: ReceiverStream::new(poll_commanded),
|
||||
};
|
||||
|
||||
let mut rx_thread = JoinSet::<Result<()>>::new();
|
||||
let thread_poll_commander = poll_commander.clone();
|
||||
let handle = tokio::task::spawn_blocking(move || {
|
||||
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||
.build()
|
||||
.unwrap();
|
||||
runtime.block_on(
|
||||
PollSender::new(thread_poll_commander)
|
||||
.send_all(&mut source.map(|bc| Ok(PollCommand::Bc(Box::new(bc))))),
|
||||
)
|
||||
});
|
||||
rx_thread.spawn(async move {
|
||||
loop {
|
||||
yield_now().await; // Give other tasks a chance to run before pulling next packet
|
||||
let packet = source.next().await;
|
||||
let bc = match packet {
|
||||
Some(res) => res,
|
||||
None => continue,
|
||||
};
|
||||
Self::poll(bc, &subs, &sink_thread).await?
|
||||
}
|
||||
handle.await??;
|
||||
Ok(())
|
||||
});
|
||||
|
||||
rx_thread.spawn(async move { sink.send_all(&mut ReceiverStream::new(sinker_rx)).await });
|
||||
|
||||
rx_thread.spawn(async move {
|
||||
loop {
|
||||
poller.run().await?;
|
||||
}
|
||||
});
|
||||
|
||||
Ok(BcConnection {
|
||||
sink: sinker,
|
||||
subscribers,
|
||||
poll_commander,
|
||||
rx_thread: RwLock::new(rx_thread),
|
||||
})
|
||||
}
|
||||
@@ -71,18 +84,9 @@ impl BcConnection {
|
||||
|
||||
pub async fn subscribe(&self, msg_num: u16) -> Result<BcSubscription> {
|
||||
let (tx, rx) = channel(100);
|
||||
match self.subscribers.num.write().await.entry(msg_num) {
|
||||
Entry::Vacant(vac_entry) => {
|
||||
vac_entry.insert(tx);
|
||||
}
|
||||
Entry::Occupied(mut occ_entry) => {
|
||||
if occ_entry.get().is_closed() {
|
||||
occ_entry.insert(tx);
|
||||
} else {
|
||||
return Err(Error::SimultaneousSubscription { msg_num });
|
||||
}
|
||||
}
|
||||
};
|
||||
self.poll_commander
|
||||
.send(PollCommand::AddSubscriber(msg_num, tx))
|
||||
.await?;
|
||||
Ok(BcSubscription::new(rx, msg_num as u32, self))
|
||||
}
|
||||
|
||||
@@ -93,90 +97,18 @@ impl BcConnection {
|
||||
where
|
||||
T: Fn(&Bc) -> Option<Bc> + Send + Sync + 'static,
|
||||
{
|
||||
match self.subscribers.id.write().await.entry(msg_id) {
|
||||
Entry::Vacant(vac_entry) => {
|
||||
vac_entry.insert(Box::new(handler));
|
||||
}
|
||||
Entry::Occupied(_) => {
|
||||
return Err(Error::SimultaneousSubscriptionId { msg_id });
|
||||
}
|
||||
};
|
||||
self.poll_commander
|
||||
.send(PollCommand::AddHandler(msg_id, Box::new(handler)))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Stop a message handler created using [`handle_msg`]
|
||||
#[allow(dead_code)] // Currently unused but added for future use
|
||||
pub async fn unhandle_msg(&self, msg_id: u32) -> Result<()> {
|
||||
self.subscribers.id.write().await.remove(&msg_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn poll(
|
||||
response: Result<Bc>,
|
||||
subscribers: &Subscriber,
|
||||
sink: &Sender<Result<Bc>>,
|
||||
) -> Result<()> {
|
||||
match response {
|
||||
Ok(response) => {
|
||||
let msg_num = response.meta.msg_num;
|
||||
let msg_id = response.meta.msg_id;
|
||||
|
||||
let mut remove_it_num = false;
|
||||
match (
|
||||
subscribers.id.read().await.get(&msg_id),
|
||||
subscribers.num.read().await.get(&msg_num),
|
||||
) {
|
||||
(Some(occ), _) => {
|
||||
if let Some(reply) = occ(&response) {
|
||||
assert!(reply.meta.msg_num == response.meta.msg_num);
|
||||
sink.send(Ok(reply)).await?;
|
||||
}
|
||||
}
|
||||
(None, Some(occ)) => {
|
||||
if occ.capacity() == 0 {
|
||||
warn!("Reaching limit of channel");
|
||||
warn!(
|
||||
"Remaining: {} of {} message space for {} (ID: {})",
|
||||
occ.capacity(),
|
||||
occ.max_capacity(),
|
||||
&msg_num,
|
||||
&msg_id
|
||||
);
|
||||
} else {
|
||||
trace!(
|
||||
"Remaining: {} of {} message space for {} (ID: {})",
|
||||
occ.capacity(),
|
||||
occ.max_capacity(),
|
||||
&msg_num,
|
||||
&msg_id
|
||||
);
|
||||
}
|
||||
if occ.send(Ok(response)).await.is_err() {
|
||||
remove_it_num = true;
|
||||
}
|
||||
}
|
||||
(None, None) => {
|
||||
debug!(
|
||||
"Ignoring uninteresting message id {} (number: {})",
|
||||
msg_id, msg_num
|
||||
);
|
||||
trace!("Contents: {:?}", response);
|
||||
}
|
||||
}
|
||||
if remove_it_num {
|
||||
subscribers.num.write().await.remove(&msg_num);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
for sub in subscribers.num.read().await.values() {
|
||||
let _ = sub.send(Err(e.clone())).await;
|
||||
}
|
||||
subscribers.num.write().await.clear();
|
||||
subscribers.id.write().await.clear();
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
|
||||
self.poll_commander
|
||||
.send(PollCommand::RemoveHandler(msg_id))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -198,3 +130,116 @@ impl BcConnection {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
enum PollCommand {
|
||||
Bc(Box<Result<Bc>>),
|
||||
AddHandler(u32, MsgHandler),
|
||||
RemoveHandler(u32),
|
||||
AddSubscriber(u16, Sender<Result<Bc>>),
|
||||
}
|
||||
|
||||
struct Poller {
|
||||
subscribers: Subscriber,
|
||||
sink: Sender<Result<Bc>>,
|
||||
reciever: ReceiverStream<PollCommand>,
|
||||
}
|
||||
|
||||
impl Poller {
|
||||
async fn run(&mut self) -> Result<()> {
|
||||
while let Some(command) = self.reciever.next().await {
|
||||
yield_now().await;
|
||||
match command {
|
||||
PollCommand::Bc(boxed_response) => match *boxed_response {
|
||||
Ok(response) => {
|
||||
let msg_num = response.meta.msg_num;
|
||||
let msg_id = response.meta.msg_id;
|
||||
|
||||
let mut remove_it_num = false;
|
||||
match (
|
||||
self.subscribers.id.get(&msg_id),
|
||||
self.subscribers.num.get(&msg_num),
|
||||
) {
|
||||
(Some(occ), _) => {
|
||||
if let Some(reply) = occ(&response) {
|
||||
assert!(reply.meta.msg_num == response.meta.msg_num);
|
||||
self.sink.send(Ok(reply)).await?;
|
||||
}
|
||||
}
|
||||
(None, Some(occ)) => {
|
||||
if occ.capacity() == 0 {
|
||||
warn!("Reaching limit of channel");
|
||||
warn!(
|
||||
"Remaining: {} of {} message space for {} (ID: {})",
|
||||
occ.capacity(),
|
||||
occ.max_capacity(),
|
||||
&msg_num,
|
||||
&msg_id
|
||||
);
|
||||
} else {
|
||||
trace!(
|
||||
"Remaining: {} of {} message space for {} (ID: {})",
|
||||
occ.capacity(),
|
||||
occ.max_capacity(),
|
||||
&msg_num,
|
||||
&msg_id
|
||||
);
|
||||
}
|
||||
if occ.send(Ok(response)).await.is_err() {
|
||||
remove_it_num = true;
|
||||
}
|
||||
}
|
||||
(None, None) => {
|
||||
debug!(
|
||||
"Ignoring uninteresting message id {} (number: {})",
|
||||
msg_id, msg_num
|
||||
);
|
||||
trace!("Contents: {:?}", response);
|
||||
}
|
||||
}
|
||||
if remove_it_num {
|
||||
self.subscribers.num.remove(&msg_num);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
for sub in self.subscribers.num.values() {
|
||||
let _ = sub.send(Err(e.clone())).await;
|
||||
}
|
||||
self.subscribers.num.clear();
|
||||
self.subscribers.id.clear();
|
||||
return Err(e);
|
||||
}
|
||||
},
|
||||
PollCommand::AddHandler(msg_id, handler) => {
|
||||
match self.subscribers.id.entry(msg_id) {
|
||||
Entry::Vacant(vac_entry) => {
|
||||
vac_entry.insert(handler);
|
||||
}
|
||||
Entry::Occupied(_) => {
|
||||
return Err(Error::SimultaneousSubscriptionId { msg_id });
|
||||
}
|
||||
};
|
||||
}
|
||||
PollCommand::RemoveHandler(msg_id) => {
|
||||
self.subscribers.id.remove(&msg_id);
|
||||
}
|
||||
PollCommand::AddSubscriber(msg_num, tx) => {
|
||||
match self.subscribers.num.entry(msg_num) {
|
||||
Entry::Vacant(vac_entry) => {
|
||||
vac_entry.insert(tx);
|
||||
}
|
||||
Entry::Occupied(mut occ_entry) => {
|
||||
if occ_entry.get().is_closed() {
|
||||
occ_entry.insert(tx);
|
||||
} else {
|
||||
let _ = tx
|
||||
.send(Err(Error::SimultaneousSubscription { msg_num }))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ struct UidLookupResults {
|
||||
|
||||
pub(crate) struct Discovery {}
|
||||
|
||||
const MTU: u32 = 1030;
|
||||
const MTU: u32 = 1350;
|
||||
lazy_static! {
|
||||
static ref P2P_RELAY_HOSTNAMES: [&'static str; 10] = [
|
||||
"p2p.reolink.com",
|
||||
@@ -101,6 +101,7 @@ impl Discoverer {
|
||||
let thread_subscriber = subsribers.clone();
|
||||
set.spawn(async move {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
match reader.next().await {
|
||||
Some(Ok((BcUdp::Discovery(bcudp), addr))) => {
|
||||
let mut tid = bcudp.tid;
|
||||
@@ -188,11 +189,16 @@ impl Discoverer {
|
||||
F: Fn(UdpDiscovery, SocketAddr) -> Option<T>,
|
||||
{
|
||||
let mut reply = ReceiverStream::new(self.subscribe(0).await?);
|
||||
loop {
|
||||
let (reply, addr) = reply.next().await.ok_or(Error::ConnectionUnavaliable)??;
|
||||
if let Some(result) = map(reply, addr) {
|
||||
return Ok(result);
|
||||
}
|
||||
tokio::select! {
|
||||
v = async {
|
||||
loop {
|
||||
let (reply, addr) = reply.next().await.ok_or(Error::ConnectionUnavaliable)??;
|
||||
if let Some(result) = map(reply, addr) {
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
} => v,
|
||||
_ = tokio::time::sleep(*MAXIMUM_WAIT) => Err::<T, Error>(Error::DiscoveryTimeout),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,6 +222,7 @@ impl Discoverer {
|
||||
where
|
||||
F: Fn(UdpDiscovery, SocketAddr) -> Option<T>,
|
||||
{
|
||||
trace!("Retrying send");
|
||||
disc.tid = 0; // Must be random to avoid simulatenous subscription errors
|
||||
let mut set = FuturesUnordered::new();
|
||||
for dest in dests.iter() {
|
||||
@@ -224,6 +231,7 @@ impl Discoverer {
|
||||
|
||||
// Get what ever completes first
|
||||
while let Some(result) = set.next().await {
|
||||
trace!("Result: {}", result.is_ok());
|
||||
if let Ok(ret) = result {
|
||||
return Ok(ret);
|
||||
}
|
||||
@@ -355,15 +363,20 @@ impl Discoverer {
|
||||
/// On success it returns the M2cQr that the p2p relay
|
||||
/// server has about the UID
|
||||
async fn uid_loopup(&self, uid: &str) -> Result<UidLookupResults> {
|
||||
let mut addrs = vec![];
|
||||
for p2p_relay in P2P_RELAY_HOSTNAMES.iter() {
|
||||
addrs.append(
|
||||
&mut format!("{}:9999", p2p_relay)
|
||||
.to_socket_addrs()
|
||||
.map(|i| i.collect::<Vec<SocketAddr>>())
|
||||
.unwrap_or_else(|_| vec![]),
|
||||
);
|
||||
}
|
||||
let task = tokio::task::spawn_blocking(move || {
|
||||
let mut addrs = vec![];
|
||||
for p2p_relay in P2P_RELAY_HOSTNAMES.iter() {
|
||||
addrs.append(
|
||||
&mut format!("{}:9999", p2p_relay)
|
||||
.to_socket_addrs()
|
||||
.map(|i| i.collect::<Vec<SocketAddr>>())
|
||||
.unwrap_or_else(|_| vec![]),
|
||||
);
|
||||
}
|
||||
addrs
|
||||
});
|
||||
let addrs = timeout(*MAXIMUM_WAIT, task).await??;
|
||||
trace!("Uid lookup to: {:?}", addrs);
|
||||
let msg = UdpDiscovery {
|
||||
tid: 0,
|
||||
payload: UdpXml {
|
||||
@@ -374,6 +387,7 @@ impl Discoverer {
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
trace!("Sending look up {:?}", msg);
|
||||
let (packet, _) = self
|
||||
.retry_send_multi(msg, addrs.as_slice(), |bc, addr| match bc {
|
||||
UdpDiscovery {
|
||||
@@ -387,7 +401,7 @@ impl Discoverer {
|
||||
_ => None,
|
||||
})
|
||||
.await?;
|
||||
|
||||
trace!("Look up complete");
|
||||
Ok(UidLookupResults {
|
||||
reg: SocketAddr::new(packet.reg.ip.parse()?, packet.reg.port),
|
||||
relay: SocketAddr::new(packet.relay.ip.parse()?, packet.relay.port),
|
||||
@@ -431,6 +445,8 @@ impl Discoverer {
|
||||
},
|
||||
};
|
||||
|
||||
trace!("Registering: {:?}", msg.payload.c2r_c);
|
||||
|
||||
// Send and await acceptance
|
||||
let (sid, dev, dmap, relay) = self
|
||||
.retry_send(msg, lookup.reg, |bc, _| match bc {
|
||||
@@ -852,6 +868,7 @@ impl Discoverer {
|
||||
let mut interval = interval(Duration::from_secs(1));
|
||||
self.handle.write().await.spawn(async move {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
interval.tick().await;
|
||||
let msg = BcUdp::Discovery(UdpDiscovery {
|
||||
tid,
|
||||
@@ -880,6 +897,7 @@ impl Discoverer {
|
||||
let mut interval = interval(Duration::from_secs(1));
|
||||
self.handle.write().await.spawn(async move {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
interval.tick().await;
|
||||
let msg = BcUdp::Discovery(UdpDiscovery {
|
||||
tid,
|
||||
@@ -1001,20 +1019,25 @@ impl Discovery {
|
||||
// directly
|
||||
#[allow(unused)]
|
||||
pub(crate) async fn remote(uid: &str) -> Result<DiscoveryResult> {
|
||||
trace!("Start remote");
|
||||
let mut discoverer = Discoverer::new().await?;
|
||||
|
||||
let client_id = generate_cid();
|
||||
trace!("client_id: {}", client_id);
|
||||
|
||||
let lookup = discoverer.uid_loopup(uid).await?;
|
||||
trace!("lookup: {:?}", lookup);
|
||||
let reg_addr = lookup.reg;
|
||||
let relay_addr = lookup.relay;
|
||||
|
||||
let reg_result = discoverer.register_address(uid, client_id, &lookup).await?;
|
||||
trace!("reg_result: {:?}", reg_result);
|
||||
|
||||
let connect_result = tokio::select! {
|
||||
v = discoverer.client_initiated_dev(®_result) => {v},
|
||||
v = discoverer.device_initiated_dev(®_result) => {v},
|
||||
}?;
|
||||
trace!("connect_result: {:?}", connect_result);
|
||||
|
||||
let (socket, keep_alive_tasks) = discoverer.into_socket().await;
|
||||
Ok(DiscoveryResult {
|
||||
|
||||
@@ -28,7 +28,7 @@ use tokio_util::{
|
||||
udp::UdpFramed,
|
||||
};
|
||||
|
||||
const MTU: usize = 1030;
|
||||
const MTU: usize = 1350;
|
||||
const UDPDATA_HEADER_SIZE: usize = 20;
|
||||
|
||||
pub(crate) struct UdpSource {
|
||||
@@ -198,6 +198,7 @@ impl Sink<(BcUdp, SocketAddr)> for BcUdpSource {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
enum State {
|
||||
Normal, // Normal recieve
|
||||
Flushing, // Used to send ack packets and things in the buffer
|
||||
@@ -295,18 +296,18 @@ impl Stream for UdpPayloadSource {
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let camera_addr = self.inner.addr;
|
||||
let mut this = self.get_mut();
|
||||
let this = self.get_mut();
|
||||
match this.state {
|
||||
State::Normal => {
|
||||
this.state = State::YieldNow;
|
||||
// this.state = State::YieldNow;
|
||||
// Handle resend events
|
||||
this.maintanence(cx);
|
||||
// Data ready to go
|
||||
if let Some(payload) = this.recieved.remove(&this.packets_want) {
|
||||
this.packets_want += 1;
|
||||
// error!("packets_want: {}", this.packets_want);
|
||||
return Poll::Ready(Some(Ok(payload)));
|
||||
}
|
||||
// Handle resend events
|
||||
this.maintanence(cx);
|
||||
// Normal behaviors
|
||||
match this.inner.poll_next_unpin(cx) {
|
||||
Poll::Ready(Some(Ok((
|
||||
@@ -416,7 +417,7 @@ impl Sink<Vec<u8>> for UdpPayloadSource {
|
||||
}
|
||||
}
|
||||
fn start_send(self: Pin<&mut Self>, item: Vec<u8>) -> std::result::Result<(), Self::Error> {
|
||||
let mut this = self.get_mut();
|
||||
let this = self.get_mut();
|
||||
for chunk in item.chunks(MTU - UDPDATA_HEADER_SIZE) {
|
||||
let udp_data = UdpData {
|
||||
connection_id: this.camera_id,
|
||||
@@ -486,7 +487,7 @@ impl Sink<Vec<u8>> for UdpPayloadSource {
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<std::result::Result<(), Self::Error>> {
|
||||
let mut this = self.get_mut();
|
||||
let this = self.get_mut();
|
||||
if let State::Closed = this.state {
|
||||
return Poll::Ready(Ok(()));
|
||||
}
|
||||
|
||||
@@ -208,6 +208,12 @@ impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<tokio_util::sync::PollSendError<T>> for Error {
|
||||
fn from(_: tokio_util::sync::PollSendError<T>) -> Self {
|
||||
Error::TokioBcSendError
|
||||
}
|
||||
}
|
||||
|
||||
impl From<cookie_factory::GenError> for Error {
|
||||
fn from(k: cookie_factory::GenError) -> Self {
|
||||
Error::GenError(std::sync::Arc::new(k))
|
||||
|
||||
@@ -216,6 +216,7 @@ impl BcCamera {
|
||||
let mut sub = connection.subscribe(msg_num).await?;
|
||||
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
let msg = sub.recv().await;
|
||||
let status = match msg {
|
||||
Ok(motion_msg) => {
|
||||
|
||||
@@ -15,6 +15,8 @@ pub enum Direction {
|
||||
In,
|
||||
/// To zoom the camera Out (may be done with cropping depending on camera model)
|
||||
Out,
|
||||
/// To stop currently active PTZ command
|
||||
Stop,
|
||||
}
|
||||
|
||||
impl BcCamera {
|
||||
@@ -36,6 +38,7 @@ impl BcCamera {
|
||||
Direction::Out => {
|
||||
todo!()
|
||||
}
|
||||
Direction::Stop => "stop",
|
||||
}
|
||||
.to_string();
|
||||
let send = Bc {
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
use super::{BcCamera, Error, Result};
|
||||
use crate::bc::{model::*, xml::*};
|
||||
|
||||
/// Specifies the phone type for the push notification
|
||||
pub enum PhoneType {
|
||||
/// Specify that this is an ios push notfication
|
||||
///
|
||||
/// In this case the token must be the APNS
|
||||
Ios,
|
||||
/// Specify that this is an andriod push notfication
|
||||
///
|
||||
/// In this case the token must firebase cloud messaging token
|
||||
Android,
|
||||
}
|
||||
|
||||
impl BcCamera {
|
||||
/// Convenience method for andriod of `[send_pushinfo]`
|
||||
pub async fn send_pushinfo_android(&self, token: &str, client_id: &str) -> Result<()> {
|
||||
self.send_pushinfo(token, client_id, PhoneType::Android)
|
||||
.await
|
||||
}
|
||||
/// Convenience method for andriod of `[send_pushinfo]`
|
||||
pub async fn send_pushinfo_ios(&self, token: &str, client_id: &str) -> Result<()> {
|
||||
self.send_pushinfo(token, client_id, PhoneType::Ios).await
|
||||
}
|
||||
/// Send the push info to regsiter for push notfications
|
||||
pub async fn send_pushinfo(
|
||||
&self,
|
||||
token: &str,
|
||||
client_id: &str,
|
||||
phone_type: PhoneType,
|
||||
) -> Result<()> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub = connection.subscribe(msg_num).await?;
|
||||
|
||||
let phone_type_str = match phone_type {
|
||||
PhoneType::Ios => "reo_iphone",
|
||||
PhoneType::Android => "reo_fcm",
|
||||
};
|
||||
|
||||
let msg = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_PUSH_INFO,
|
||||
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 {
|
||||
push_info: Some(PushInfo {
|
||||
token: token.to_owned(),
|
||||
phone_type: phone_type_str.to_owned(),
|
||||
client_id: client_id.to_owned(),
|
||||
}),
|
||||
..Default::default()
|
||||
})),
|
||||
}),
|
||||
};
|
||||
|
||||
sub.send(msg).await?;
|
||||
let msg = sub.recv().await?;
|
||||
if msg.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavaliable);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -105,6 +105,7 @@ impl BcCamera {
|
||||
let channel_id = self.channel_id;
|
||||
|
||||
let handle = task::spawn(async move {
|
||||
tokio::task::yield_now().await;
|
||||
let mut sub_video = connection.subscribe(msg_num).await?;
|
||||
|
||||
// On an E1 and swann cameras:
|
||||
@@ -221,6 +222,28 @@ impl BcCamera {
|
||||
sub_video.send(stop_video).await?;
|
||||
// debug!("Stream: Sent Stop");
|
||||
|
||||
tokio::select! {
|
||||
v = async {
|
||||
loop {
|
||||
let msg = sub_video.recv().await?;
|
||||
if let BcMeta {
|
||||
response_code: 200,
|
||||
msg_id: MSG_ID_VIDEO_STOP,
|
||||
..
|
||||
} = msg.meta {
|
||||
return Ok(());
|
||||
}
|
||||
else if let BcMeta {
|
||||
msg_id: MSG_ID_VIDEO_STOP,
|
||||
..
|
||||
} = msg.meta {
|
||||
return Err(Error::CameraServiceUnavaliable);
|
||||
}
|
||||
}
|
||||
} => v,
|
||||
_ = tokio::time::sleep(tokio::time::Duration::from_secs(2)) => {Ok(())},
|
||||
}?;
|
||||
|
||||
Ok(())
|
||||
});
|
||||
|
||||
|
||||
@@ -12,11 +12,15 @@ pub struct BcMediaCodex {
|
||||
/// If true we will not search for the start of the next packet
|
||||
/// in the event that the stream appears to be corrupted
|
||||
strict: bool,
|
||||
amount_skipped: usize,
|
||||
}
|
||||
|
||||
impl BcMediaCodex {
|
||||
pub(crate) fn new(strict: bool) -> Self {
|
||||
Self { strict }
|
||||
Self {
|
||||
strict,
|
||||
amount_skipped: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,18 +50,19 @@ impl Decoder for BcMediaCodex {
|
||||
}
|
||||
|
||||
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
|
||||
let mut amount_skipped: usize = 0;
|
||||
loop {
|
||||
match { BcMedia::deserialize(src) } {
|
||||
Ok(bc) => {
|
||||
if amount_skipped > 0 {
|
||||
debug!("Amount skipped to restore stream: {}", amount_skipped);
|
||||
if self.amount_skipped > 0 {
|
||||
debug!("Amount skipped to restore stream: {}", self.amount_skipped);
|
||||
self.amount_skipped = 0;
|
||||
}
|
||||
return Ok(Some(bc));
|
||||
}
|
||||
Err(Error::NomIncomplete(_)) => {
|
||||
if amount_skipped > 0 {
|
||||
debug!("Amount skipped to restore stream: {}", amount_skipped);
|
||||
if self.amount_skipped > 0 {
|
||||
debug!("Amount skipped to restore stream: {}", self.amount_skipped);
|
||||
self.amount_skipped = 0;
|
||||
}
|
||||
return Ok(None);
|
||||
}
|
||||
@@ -67,12 +72,12 @@ impl Decoder for BcMediaCodex {
|
||||
} else if src.is_empty() {
|
||||
return Ok(None);
|
||||
} else {
|
||||
if amount_skipped == 0 {
|
||||
if self.amount_skipped == 0 {
|
||||
debug!("Error in stream attempting to restore");
|
||||
trace!(" Stream Error: {:?}", e);
|
||||
}
|
||||
// Drop the whole packet and wait for a packet that starts with magic
|
||||
amount_skipped += src.len();
|
||||
self.amount_skipped += src.len();
|
||||
src.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
[package]
|
||||
name = "pushnoti"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.71"
|
||||
clap = { version = "4.2.2", features = ["derive", "cargo"] }
|
||||
env_logger = "0.10.0"
|
||||
fcm-push-listener = "2.0.1"
|
||||
lazy_static = "1.4.0"
|
||||
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
|
||||
neolink_core = { path = "../core", version = "0.5" }
|
||||
regex = "1.8.1"
|
||||
serde = "1.0.163"
|
||||
tokio = { version = "1.28.1", features = ["full"] }
|
||||
toml = "0.7.3"
|
||||
validator = "0.16.0"
|
||||
validator_derive = "0.16.0"
|
||||
@@ -0,0 +1,273 @@
|
||||
# Reolink Push Notifications
|
||||
|
||||
This is the test crate to experiment with RE-ing the push notifications for a
|
||||
camera
|
||||
|
||||
The goal here is to connect to Google's firebase cloud messaging system (FCM)
|
||||
and recieve push notificaions for motion updates. This way we can get motion
|
||||
notifications without being logged in to the camera and this will let us
|
||||
disconnect from battery cameras and save power.
|
||||
|
||||
The following is the reolink data for google extracted from ios.
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>AD_UNIT_ID_FOR_BANNER_TEST</key>
|
||||
<string>ca-app-pub-3940256099942544/2934735716</string>
|
||||
<key>AD_UNIT_ID_FOR_INTERSTITIAL_TEST</key>
|
||||
<string>ca-app-pub-3940256099942544/4411468910</string>
|
||||
<key>API_KEY</key>
|
||||
<string>AIzaSyAV5l5DbbAkYB5X61wgn81Lb2f7s1h-ANE</string>
|
||||
<key>BUNDLE_ID</key>
|
||||
<string>com.reo.link</string>
|
||||
<key>CLIENT_ID</key>
|
||||
<string>696841269229-mfqfluqdkb0s2raassbsul48h09l2eiv.apps.googleusercontent.com</string>
|
||||
<key>DATABASE_URL</key>
|
||||
<string>https://reolink-for-ios.firebaseio.com</string>
|
||||
<key>GCM_SENDER_ID</key>
|
||||
<string>696841269229</string>
|
||||
<key>GOOGLE_APP_ID</key>
|
||||
<string>1:696841269229:ios:690529b18debb2e3</string>
|
||||
<key>IS_ADS_ENABLED</key>
|
||||
<false/>
|
||||
<key>IS_ANALYTICS_ENABLED</key>
|
||||
<false/>
|
||||
<key>IS_APPINVITE_ENABLED</key>
|
||||
<false/>
|
||||
<key>IS_GCM_ENABLED</key>
|
||||
<false/>
|
||||
<key>IS_SIGNIN_ENABLED</key>
|
||||
<false/>
|
||||
<key>PLIST_VERSION</key>
|
||||
<string>1</string>
|
||||
<key>PROJECT_ID</key>
|
||||
<string>reolink-for-ios</string>
|
||||
<key>REVERSED_CLIENT_ID</key>
|
||||
<string>com.googleusercontent.apps.696841269229-mfqfluqdkb0s2raassbsul48h09l2eiv</string>
|
||||
<key>STORAGE_BUCKET</key>
|
||||
<string>reolink-for-ios.appspot.com</string>
|
||||
</dict>
|
||||
</plist>
|
||||
```
|
||||
|
||||
Key information from this is
|
||||
|
||||
```xml
|
||||
<key>GCM_SENDER_ID</key>
|
||||
<string>696841269229</string>
|
||||
```
|
||||
|
||||
Which we use to register for FCM
|
||||
|
||||
---
|
||||
|
||||
p.s. maybe some one can do something fun with the API
|
||||
key not sure what thats doing there since anyone can
|
||||
extract that.
|
||||
|
||||
```xml
|
||||
<key>API_KEY</key>
|
||||
<string>AIzaSyAV5l5DbbAkYB5X61wgn81Lb2f7s1h-ANE</string>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
On android the details are little harder for me to extract (ios here). By
|
||||
extracting the apk with apktook I found the following:
|
||||
|
||||
```xml
|
||||
<string name="gcm_defaultSenderId">743639030586</string>
|
||||
<string name="google_api_key">AIzaSyBEUIuWHnnOEwFahxWgQB4Yt4NsgOmkPyE</string>
|
||||
<string name="google_app_id">1:743639030586:android:86f60a4fb7143876</string>
|
||||
<string name="google_client_id">743639030586-ove5uqrausoqtjjkev4b1tk4vjdfpt4l.apps.googleusercontent.com</string>
|
||||
```
|
||||
|
||||
Where the key we need is `743639030586`
|
||||
|
||||
To emulate the push recievier we use the `fcm-push-listener` crate. Which
|
||||
is a rust implementation of the good
|
||||
[RE work of Matthieu Lemoine.](https://medium.com/@MatthieuLemoine/my-journey-to-bring-web-push-support-to-node-and-electron-ce70eea1c0b0)
|
||||
hats off to you.
|
||||
|
||||
We also need to send the PushInfo command to the camera with the token we get
|
||||
from FCM and some client ID. On IOS the token is the APNS number.
|
||||
|
||||
The client ID is an all CAPS UID of hexadecimal of 33 chars. Not sure how
|
||||
these are generated but they seem to be unique to the device, perhaps a MD5 of
|
||||
something or some generated UID on app initial login.
|
||||
|
||||
To experiment I did a data wipe on the reolink app and found it generates a new UID.
|
||||
I also found out that if you do this while you have Push Notifications turned on
|
||||
then you cannot turn them off anymore. To disable the push notifications you need
|
||||
the UID which I just wiped.... Fortunatly I saved before I wiped it so I could restore.
|
||||
p.d. If you every wipe your reolink data and need to stop the push notifications
|
||||
you can try doing manually using the API listed at the end.
|
||||
|
||||
This is the data of the `PushInfo` command that is sent to the camera:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<PushInfo version="1.1">
|
||||
<token>TOKEN_FROM_FCM_OR_APNS</token>
|
||||
<phoneType>reo_iphone</phoneType>
|
||||
<clientID>SOMEID</clientID>
|
||||
</PushInfo>
|
||||
</body>
|
||||
```
|
||||
|
||||
For ios the phonetype is `reo_iphone` which I observed live but for android
|
||||
no such luck.
|
||||
|
||||
First I just tried `reo_android` but the camera replied with an error code.
|
||||
Which at least told me that I can test other string quickly and see if it
|
||||
returns the error code.
|
||||
|
||||
Next thing to do was dumping the binaries from the apk and searching for a string
|
||||
like `reo_`
|
||||
|
||||
```bash
|
||||
grep -Ria "reo_" .
|
||||
./lib/armeabi-v7a/libavcodec.so:stereo_modeError splitting the input into NAL units.
|
||||
./lib/arm64-v8a/libavcodec.so:stereo_modeError splitting the input into NAL units.
|
||||
./smali_classes2/com/google/android/exoplayer2/extractor/mkv/MatroskaExtractor.smali:.field private static final ID_STEREO_MODE:I = 0x53b8
|
||||
./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_LEFT_RIGHT:I = 0x2
|
||||
./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_MONO:I = 0x0
|
||||
./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_STEREO_MESH:I = 0x3
|
||||
./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_TOP_BOTTOM:I = 0x1
|
||||
./smali_classes2/com/android/bc/util/DeviceIdUtil$UpdatePushClientIdRequest.smali: const-string v2, "reo_fcm"
|
||||
./smali_classes2/com/android/bc/pushmanager/MyPushAdapterImpl.smali: const-string v0, "reo_fcm"
|
||||
./smali/androidx/core/text/BidiFormatter.smali:.field private static final FLAG_STEREO_RESET:I = 0x2
|
||||
./smali/com/android/bc/component/BaseWebViewFragment.smali: const-string v2, "REO_LANGUAGE="
|
||||
```
|
||||
|
||||
The interesting one was `MyPushAdapterImpl.smali: const-string v0, "reo_fcm"`
|
||||
which suggested that it is `reo_fcm` stuck that in and voilla
|
||||
no error messages from the camera.
|
||||
|
||||
Now to compile the test app with all this info
|
||||
wait patiently for a motion event and...
|
||||
|
||||
viola!
|
||||
|
||||
Reolink was kinda enough to send this to my test app:
|
||||
|
||||
```json
|
||||
{
|
||||
"data": {
|
||||
"SRVTIME": "2023-05-19T08:07:03.566Z",
|
||||
"ALMNAME": "Motion Alert from Cam01",
|
||||
"sound": "push.wav",
|
||||
"CHNAME": "Cam0",
|
||||
"messageType": "alarm",
|
||||
"DEVNAME": "Cam01",
|
||||
"pushVersion": "v1.1",
|
||||
"UID": "REDACTED",
|
||||
"ALMTYPE": "MD",
|
||||
"ALMCHN": "1",
|
||||
"alert": "Motion Alert from Cam01",
|
||||
"title": "Camera Alert"
|
||||
},
|
||||
"from": "743639030586",
|
||||
"priority": "normal",
|
||||
"notification": { "title": "Camera Alert", "body": "Motion Alert from Cam01" },
|
||||
"fcmMessageId": "REDACTED"
|
||||
}
|
||||
```
|
||||
|
||||
There's no push notifiation for motion stop event but I can wait for this message
|
||||
then login and use normal motion detection to tell when it stops.
|
||||
|
||||
Yay.... now to just add all this to actual neolink so we can save battery.....
|
||||
|
||||
---
|
||||
|
||||
While experimenting with the ios app, in addition to observing all the
|
||||
data send to the camera, I also looked at all https calls the app was making
|
||||
(mitmproxy). This showed a set of APIs that could be useful. Including the api
|
||||
for deleting a push notification.
|
||||
|
||||
---
|
||||
|
||||
To disable a PUSH request the following is done:
|
||||
|
||||
```bash
|
||||
curl -v -X DELETE 'https://pushx.reolink.com/devices/<CAMERA_UID>/notifications/listeners/<clientID_FROM_PushInfo>' -H 'Cookie:'
|
||||
```
|
||||
|
||||
Reply is:
|
||||
|
||||
```json
|
||||
{
|
||||
"status" : {
|
||||
"detail" : "OK",
|
||||
"code" : 0
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Nothing seems to be sent to the camera just this to the reolink api
|
||||
|
||||
---
|
||||
|
||||
On ios application start reolink send this:
|
||||
|
||||
```bash
|
||||
curl -v -X GET 'https://pushx.reolink.com/listeners/<clientID_FROM_PushInfo>/devices' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept: */*' -H 'Accept-Language: en-GB,en;q=0.9' -H 'Cookie:'
|
||||
```
|
||||
|
||||
Which resonds with this json which contains a list of all currently
|
||||
enabled push notification camera UIDs for this client
|
||||
|
||||
```json
|
||||
{
|
||||
"status" : {
|
||||
"detail" : "OK",
|
||||
"code" : 0
|
||||
},
|
||||
"data" : [
|
||||
{
|
||||
"uid" : "<CAMERA_UID>",
|
||||
"secret" : ""
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
You can also confirm a device has a listener with
|
||||
|
||||
```bash
|
||||
curl -v -X GET 'https://pushx.reolink.com/devices/<CAMERA_UID>/listeners/<clientID_FROM_PushInfo>/config' -H 'Cookie:'
|
||||
```
|
||||
|
||||
Which returns
|
||||
|
||||
```json
|
||||
{
|
||||
"status": {
|
||||
"code":0,
|
||||
"detail":"OK"
|
||||
},
|
||||
"data": {
|
||||
"sound": {
|
||||
"soundType":0,
|
||||
"soundName":"",
|
||||
"soundVolume":0
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
There's also a profile one that the app does when a new camera is added
|
||||
but I forgot to save the details of it.
|
||||
|
||||
It reports all the capabilities of the camera, such as it's model
|
||||
and its resolution etc. Could be useful to test if a UID is valid
|
||||
quickly
|
||||
@@ -0,0 +1,80 @@
|
||||
use lazy_static::lazy_static;
|
||||
use neolink_core::bc_protocol::{DiscoveryMethods, PrintFormat};
|
||||
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();
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
pub(crate) struct Config {
|
||||
#[validate]
|
||||
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")]
|
||||
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,
|
||||
|
||||
#[serde(default = "default_print", alias = "print")]
|
||||
pub(crate) print_format: PrintFormat,
|
||||
}
|
||||
|
||||
fn default_print() -> PrintFormat {
|
||||
PrintFormat::None
|
||||
}
|
||||
|
||||
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,82 @@
|
||||
use anyhow::{Context, Result};
|
||||
use clap::Parser;
|
||||
use fcm_push_listener::*;
|
||||
use log::*;
|
||||
use std::{fs, path::PathBuf};
|
||||
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 camera = find_and_connect(&config, &opt.camera).await?;
|
||||
|
||||
// 696841269229 is the reo_iphone FCM Sender_ID
|
||||
// let registration = fcm_push_listener::register("696841269229").await?;
|
||||
// 743639030586 is the reo_fcm FCM Sender_ID
|
||||
// 263684512460 is my test Sender_ID
|
||||
// let registration = fcm_push_listener::register("743639030586").await?
|
||||
// I have confirmed that I can recieve test messages with this SenderID
|
||||
// into this program
|
||||
// let registration = fcm_push_listener::register("263684512460").await?;
|
||||
|
||||
let sender_id = "743639030586"; // andriod
|
||||
|
||||
// let sender_id = "696841269229"; // ios
|
||||
|
||||
// let sender_id = "263684512460"; // test
|
||||
|
||||
let token_path = PathBuf::from("./token.toml");
|
||||
let registration = if let Ok(Ok(registration)) =
|
||||
fs::read_to_string(&token_path).map(|v| toml::from_str::<Registration>(&v))
|
||||
{
|
||||
info!("Loaded token");
|
||||
registration
|
||||
} else {
|
||||
info!("Registering new token");
|
||||
let registration = fcm_push_listener::register(sender_id).await?;
|
||||
let new_token = toml::to_string(®istration)?;
|
||||
fs::write(token_path, new_token)?;
|
||||
registration
|
||||
};
|
||||
|
||||
// Send registration.fcm_token to the server to allow it to send push messages to you.
|
||||
info!("registration.fcm_token: {}", registration.fcm_token);
|
||||
let uid = "6A5443E486511B0D828543445DC55A7D"; // MD5 Hash of "WHY_REOLINK"
|
||||
camera
|
||||
.send_pushinfo_android(®istration.fcm_token, uid)
|
||||
.await?;
|
||||
|
||||
info!("Listening");
|
||||
let mut listener = FcmPushListener::create(
|
||||
registration,
|
||||
|message: FcmMessage| {
|
||||
info!("Message JSON: {}", message.payload_json);
|
||||
info!("Persistent ID: {:?}", message.persistent_id);
|
||||
},
|
||||
vec![],
|
||||
);
|
||||
listener.connect().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,152 @@
|
||||
//! 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),
|
||||
Uid(String, DiscoveryMethods),
|
||||
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,
|
||||
aux_printing: camera_config.print_format,
|
||||
credentials: Credentials {
|
||||
username: camera_config.username.clone(),
|
||||
password: camera_config.password.clone(),
|
||||
},
|
||||
};
|
||||
|
||||
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))
|
||||
}
|
||||
@@ -146,11 +146,14 @@ local message_types = {
|
||||
[106]="<Dst>",
|
||||
[107]="<Dst> (write)",
|
||||
[108]="<ConfigFileInfo> (log)",
|
||||
[109]="<Snap>",
|
||||
[114]="<Uid>",
|
||||
[115]="<WifiSignal>",
|
||||
[116]="<Wifi>",
|
||||
[120]="<OnlineUserList>",
|
||||
[122]="<PerformanceInfo>",
|
||||
[123]="<ReplaySeek>",
|
||||
[124]="<PushInfo>",
|
||||
[132]="<VideoInput>", -- <InputAdvanceCfg>
|
||||
[133]="<RfAlarm>",
|
||||
[141]="<Email> (test)",
|
||||
@@ -160,6 +163,7 @@ local message_types = {
|
||||
[151]="<AbilityInfo>",
|
||||
[190]="PTZ Preset",
|
||||
[194]="<Ftp> (test)",
|
||||
[195]="<AutoUpdate>",
|
||||
[199]="<Support>",
|
||||
[208]="<LedState>",
|
||||
[209]="<LedState> (write)",
|
||||
@@ -174,9 +178,13 @@ local message_types = {
|
||||
[228]="<Crop>",
|
||||
[229]="<Crop> (write)",
|
||||
[230]="<cropSnap>",
|
||||
[232]="<AudioTask>",
|
||||
[234]="UDP Keep Alive",
|
||||
[252]="<BatteryInfoList>",
|
||||
[253]="<BatteryInfo>",
|
||||
[268]="<CloudBindInfo>",
|
||||
[282]="<CloudLoginKey>",
|
||||
[287]="<TimeCfg>",
|
||||
[272]="<findAlarmVideo>",
|
||||
[273]="<alarmVideoInfo>",
|
||||
[274]="<findAlarmVideo>",
|
||||
|
||||
@@ -1662,6 +1662,64 @@ Message have zero to two payloads.
|
||||
</body>
|
||||
```
|
||||
|
||||
- 109: `<Snap>`
|
||||
|
||||
- Client:
|
||||
Usual header
|
||||
|
||||
- Meta:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<Extension version="1.1">
|
||||
<channelId>0</channelId>
|
||||
</Extension>
|
||||
```
|
||||
|
||||
- Main:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<Snap version="1.1">
|
||||
<channelId>0</channelId>
|
||||
<logicChannel>0</logicChannel>
|
||||
<time>0</time>
|
||||
<fullFrame>0</fullFrame>
|
||||
<streamType>main</streamType>
|
||||
</Snap>
|
||||
</body>
|
||||
```
|
||||
|
||||
- Camera:
|
||||
|
||||
**Notes:** XML & Binary reply over mutliple packets:
|
||||
- Reply 1:
|
||||
- Main:
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<Snap version="1.1">
|
||||
<channelId>0</channelId>
|
||||
<fileName>01_20230518140240.jpg</fileName>
|
||||
<time>0</time>
|
||||
<pictureSize>23644</pictureSize>
|
||||
</Snap>
|
||||
</body>
|
||||
```
|
||||
|
||||
- Reply 2:
|
||||
- Meta:
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<Extension version="1.1">
|
||||
<binaryData>1</binaryData>
|
||||
</Extension>
|
||||
```
|
||||
- Main:
|
||||
**Binary data containing the file may be broken over multiple packets**
|
||||
|
||||
|
||||
- 115: `<WifiSignal>`
|
||||
|
||||
- Client
|
||||
@@ -1691,6 +1749,35 @@ Message have zero to two payloads.
|
||||
</body>
|
||||
```
|
||||
|
||||
- 116: `<Wifi>`
|
||||
|
||||
- Client:
|
||||
Usual header
|
||||
|
||||
- Camera:
|
||||
Payload:
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<Wifi version="1.1">
|
||||
<mode>station</mode>
|
||||
<authMode>wpa2psk</authMode>
|
||||
<encryptType>aes</encryptType>
|
||||
<udidList>
|
||||
<udid>
|
||||
<name>WIFINAME</name>
|
||||
<signal>NUMBER</signal>
|
||||
<encrypt>ONE_OR_ZERO</encrypt>
|
||||
</udid>
|
||||
# Repeats for ALL wifi in range
|
||||
</udidList>
|
||||
<ssid>YOUR_CURRENT_WIFI</ssid>
|
||||
<key>YOUR_CURRENT_WIFI_PASSWORD_UNENCRYPTED</key>
|
||||
<channel>YOUR_CURRENT_WIFI_CHANNEL</channel>
|
||||
</Wifi>
|
||||
</body>
|
||||
```
|
||||
|
||||
- 132: `<VideoInput>`
|
||||
|
||||
- Client
|
||||
@@ -1803,6 +1890,37 @@ Message have zero to two payloads.
|
||||
</body>
|
||||
```
|
||||
|
||||
- 124: `<PushInfo>`
|
||||
|
||||
- Client
|
||||
- Payload:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<PushInfo version="1.1">
|
||||
<token>A_PUSH_NOTIFICATION_TOKEN</token>
|
||||
<phoneType>reo_iphone</phoneType>
|
||||
<clientID>A_PUSH_NOTIFICATION_CLIENTID</clientID>
|
||||
</PushInfo>
|
||||
</body>
|
||||
```
|
||||
|
||||
- Camera
|
||||
- Payload
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<PushRspInfo version="1.1">
|
||||
<registerHandle>-1</registerHandle>
|
||||
<uid>CAMERA_UID</uid>
|
||||
<uidKey>A_4_CHAR_STRING</uidKey>
|
||||
</PushRspInfo>
|
||||
</body>
|
||||
```
|
||||
|
||||
|
||||
- 133: `<RfAlarm>`
|
||||
|
||||
- Client
|
||||
@@ -2338,6 +2456,154 @@ Message have zero to two payloads.
|
||||
|--------------|--------------|----------------|-------------------|-------------------|---------------|---------------|
|
||||
| 0a bc de f0 | 00 00 00 d1 | 00 00 00 00 | 85 00 00 00 | c8 00 | 00 00 | 00 00 00 00 |
|
||||
|
||||
- 219: `<PushTask>`
|
||||
- Client
|
||||
Usual header
|
||||
|
||||
- Meta:
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<Extension version="1.1">
|
||||
<channelId>0</channelId>
|
||||
</Extension>
|
||||
```
|
||||
- Camera
|
||||
Usual header
|
||||
|
||||
- Payload:
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<PushTask version="1.1">
|
||||
<channelId>0</channelId>
|
||||
<enable>1</enable>
|
||||
<ScheduleList>
|
||||
<Schedule>
|
||||
<alarmType>MD</alarmType>
|
||||
<timeBlockList>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Sunday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Monday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Tuesday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Wednesday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Thursday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Friday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Saturday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
</timeBlockList>
|
||||
</Schedule>
|
||||
</ScheduleList>
|
||||
</PushTask>
|
||||
</body>
|
||||
```
|
||||
|
||||
|
||||
- 232: `<AudioTask>`
|
||||
- Client
|
||||
Usual header
|
||||
|
||||
- Meta:
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<Extension version="1.1">
|
||||
<channelId>0</channelId>
|
||||
</Extension>
|
||||
```
|
||||
- Camera
|
||||
Usual header
|
||||
|
||||
- Payload:
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<AudioTask version="1.1">
|
||||
<channelId>0</channelId>
|
||||
<enable>0</enable>
|
||||
<ScheduleList>
|
||||
<Schedule>
|
||||
<alarmType>MD</alarmType>
|
||||
<timeBlockList>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Sunday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Monday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Tuesday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Wednesday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Thursday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Friday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
<timeBlock>
|
||||
<enable>1</enable>
|
||||
<weekDay>Saturday</weekDay>
|
||||
<beginHour>0</beginHour>
|
||||
<endHour>23</endHour>
|
||||
</timeBlock>
|
||||
</timeBlockList>
|
||||
</Schedule>
|
||||
</ScheduleList>
|
||||
</AudioTask>
|
||||
</body>
|
||||
```
|
||||
|
||||
- 264: `<audioCfg>` (write)
|
||||
|
||||
@@ -2373,6 +2639,55 @@ Message have zero to two payloads.
|
||||
</body>
|
||||
```
|
||||
|
||||
- 268: `CloudBindInfo`
|
||||
- Client:
|
||||
Usual header only packet
|
||||
|
||||
- Camera
|
||||
|
||||
- Payload
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<CloudBindInfo version="1.1">
|
||||
<binded>1</binded>
|
||||
</CloudBindInfo>
|
||||
</body>
|
||||
```
|
||||
|
||||
- 282: `CloudLoginKey`
|
||||
|
||||
- Client:
|
||||
Usual header only packet
|
||||
|
||||
- Camera
|
||||
- Payload
|
||||
|
||||
```xml
|
||||
<body>
|
||||
<CloudLoginKey version="1.1">
|
||||
<enable>0</enable>
|
||||
</CloudLoginKey>
|
||||
</body>
|
||||
```
|
||||
|
||||
- 287: `<TimeCfg>`
|
||||
- Client:
|
||||
- Payload
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<TimeCfg version="1.1">
|
||||
<realTime>1684393362</realTime>
|
||||
</TimeCfg>
|
||||
</body>
|
||||
```
|
||||
|
||||
- Camera
|
||||
Header only 200 Ok reply
|
||||
|
||||
- 288: `<FloodlightManual>` (write)
|
||||
|
||||
- Client
|
||||
|
||||
@@ -75,6 +75,22 @@ address = "192.168.1.187:9000"
|
||||
#
|
||||
# print_format = "None"
|
||||
|
||||
# Internally neolink uses a buffer to improve delivery of frames at their actual times
|
||||
# A large buffer can work even for large interupts in the connection with the camera
|
||||
# A small buffer will reduce the latency introduced
|
||||
# buffer_size: 100
|
||||
|
||||
# Over long periods of time the camera buffer can exhaust with no means of recovery
|
||||
# to combat this a time stretching stratery is employed where the stream is slowed
|
||||
# as the buffer is exhausted and sped up when full
|
||||
# this can keep the camera and rtsp streams in sync and reduce buffer exhaustions
|
||||
#
|
||||
# HOWEVER if there is real latency issues (such as too high a bitrate)
|
||||
# then smoothing can result in a stream appearing in slow motion
|
||||
#
|
||||
# you can disable and enable this feature here
|
||||
# use_smoothing: true
|
||||
|
||||
|
||||
[[cameras]]
|
||||
name = "storage shed"
|
||||
|
||||
@@ -27,6 +27,9 @@ pub(crate) struct Config {
|
||||
#[serde(default = "default_bind_port")]
|
||||
pub(crate) bind_port: u16,
|
||||
|
||||
#[serde(default = "default_tokio_console")]
|
||||
pub(crate) tokio_console: bool,
|
||||
|
||||
#[serde(default = "default_certificate")]
|
||||
pub(crate) certificate: Option<String>,
|
||||
|
||||
@@ -96,6 +99,25 @@ pub(crate) struct CameraConfig {
|
||||
|
||||
#[serde(default = "default_print", alias = "print")]
|
||||
pub(crate) print_format: PrintFormat,
|
||||
|
||||
#[serde(default = "default_update_time", alias = "time")]
|
||||
pub(crate) update_time: bool,
|
||||
|
||||
#[validate(range(
|
||||
min = 10,
|
||||
max = 500,
|
||||
message = "Invalid buffer size",
|
||||
code = "buffer_size"
|
||||
))]
|
||||
#[serde(default = "default_buffer_size", alias = "size", alias = "buffer")]
|
||||
pub(crate) buffer_size: usize,
|
||||
|
||||
#[serde(
|
||||
default = "default_smoothing",
|
||||
alias = "smoothing",
|
||||
alias = "stretching"
|
||||
)]
|
||||
pub(crate) use_smoothing: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate, Clone)]
|
||||
@@ -192,10 +214,18 @@ fn default_tls_client_auth() -> String {
|
||||
"none".to_string()
|
||||
}
|
||||
|
||||
fn default_tokio_console() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn default_channel_id() -> u8 {
|
||||
0
|
||||
}
|
||||
|
||||
fn default_update_time() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn default_motion_timeout() -> f64 {
|
||||
1.
|
||||
}
|
||||
@@ -225,6 +255,14 @@ fn default_pause() -> PauseConfig {
|
||||
}
|
||||
}
|
||||
|
||||
fn default_smoothing() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn default_buffer_size() -> usize {
|
||||
100
|
||||
}
|
||||
|
||||
pub(crate) static RESERVED_NAMES: &[&str] = &["anyone", "anonymous"];
|
||||
fn validate_username(name: &str) -> Result<(), ValidationError> {
|
||||
if name.trim().is_empty() {
|
||||
|
||||
@@ -65,6 +65,7 @@ async fn output(pipeline: Pipeline) -> Result<GstSender> {
|
||||
let mut set = JoinSet::new();
|
||||
set.spawn(async move {
|
||||
while let Some(control) = reciever.recv().await {
|
||||
tokio::task::yield_now().await;
|
||||
match control {
|
||||
GstControl::Data(buf) => {
|
||||
let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap();
|
||||
|
||||
+16
@@ -42,6 +42,18 @@ mod utils;
|
||||
|
||||
use cmdline::{Command, Opt};
|
||||
use config::Config;
|
||||
use console_subscriber as _;
|
||||
|
||||
#[cfg(tokio_unstable)]
|
||||
fn tokio_console_enable() {
|
||||
info!("Tokio Console Enabled");
|
||||
console_subscriber::init();
|
||||
}
|
||||
|
||||
#[cfg(not(tokio_unstable))]
|
||||
fn tokio_console_enable() {
|
||||
debug!("Tokio Console Disabled");
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
@@ -66,6 +78,10 @@ async fn main() -> Result<()> {
|
||||
.validate()
|
||||
.with_context(|| format!("Failed to validate the {:?} config file", conf_path))?;
|
||||
|
||||
if config.tokio_console {
|
||||
tokio_console_enable();
|
||||
}
|
||||
|
||||
match opt.cmd {
|
||||
None => {
|
||||
warn!(
|
||||
|
||||
+328
-288
@@ -1,20 +1,21 @@
|
||||
use super::App;
|
||||
use crate::config::CameraConfig;
|
||||
use crate::utils::AddressOrUid;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::*;
|
||||
use neolink_core::bc_protocol::{
|
||||
BcCamera, Direction as BcDirection, Error as BcError, LightState, MotionStatus,
|
||||
BcCamera, Direction as BcDirection, Error as BcError, LightState, MaxEncryption, MotionStatus,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{
|
||||
mpsc::{channel, error::TryRecvError, Receiver, Sender},
|
||||
Mutex,
|
||||
use tokio::{
|
||||
sync::mpsc::{channel, Receiver, Sender},
|
||||
task::JoinSet,
|
||||
time::sleep,
|
||||
};
|
||||
|
||||
use core::time::Duration;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub(crate) enum Messages {
|
||||
None,
|
||||
Login,
|
||||
MotionStop,
|
||||
MotionStart,
|
||||
@@ -33,12 +34,12 @@ pub(crate) enum Messages {
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub(crate) enum Direction {
|
||||
Up(f32),
|
||||
Down(f32),
|
||||
Left(f32),
|
||||
Right(f32),
|
||||
In(f32),
|
||||
Out(f32),
|
||||
Up(f32, f32),
|
||||
Down(f32, f32),
|
||||
Left(f32, f32),
|
||||
Right(f32, f32),
|
||||
In(f32, f32),
|
||||
Out(f32, f32),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -50,102 +51,114 @@ enum ToCamera {
|
||||
},
|
||||
}
|
||||
|
||||
pub(crate) struct EventCam {
|
||||
config: Arc<CameraConfig>,
|
||||
app: Arc<App>,
|
||||
channel_out: Arc<Mutex<Option<Receiver<Messages>>>>,
|
||||
channel_in: Arc<Mutex<Option<Sender<ToCamera>>>>,
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct EventCamSender {
|
||||
channel_in: Sender<ToCamera>,
|
||||
}
|
||||
|
||||
impl EventCam {
|
||||
pub(crate) fn new(config: Arc<CameraConfig>, app: Arc<App>) -> Self {
|
||||
Self {
|
||||
config,
|
||||
app,
|
||||
channel_out: Arc::new(Mutex::new(None)),
|
||||
channel_in: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn poll(&self) -> Result<Messages> {
|
||||
if let Ok(ref mut channel_out) = self.channel_out.try_lock() {
|
||||
if let Some(channel_out) = channel_out.as_mut() {
|
||||
channel_out.recv().await.context("Camera failed to poll")
|
||||
} else {
|
||||
Ok(Messages::None)
|
||||
}
|
||||
} else {
|
||||
Ok(Messages::None)
|
||||
}
|
||||
}
|
||||
|
||||
impl EventCamSender {
|
||||
#[allow(dead_code)]
|
||||
pub(crate) async fn send_message(&self, msg: Messages) -> Result<()> {
|
||||
let mut channel_in = self.channel_in.lock().await;
|
||||
if let Some(channel_in) = channel_in.as_mut() {
|
||||
channel_in
|
||||
.send(ToCamera::Send(msg))
|
||||
.await
|
||||
.context("Failed to send message from camera")
|
||||
} else {
|
||||
Err(anyhow!("Failed to send camera data over channel"))
|
||||
}
|
||||
self.channel_in
|
||||
.send(ToCamera::Send(msg))
|
||||
.await
|
||||
.context("Failed to send message from camera")
|
||||
}
|
||||
|
||||
pub(crate) async fn send_message_with_reply(&self, msg: Messages) -> Result<String> {
|
||||
let mut channel_in = self.channel_in.lock().await;
|
||||
if let Some(channel_in) = channel_in.as_mut() {
|
||||
let (tx, mut rx) = channel(1);
|
||||
channel_in
|
||||
.send(ToCamera::SendAndReply {
|
||||
message: msg,
|
||||
reply: tx,
|
||||
})
|
||||
.await
|
||||
.context("Failed to send message from camera")?;
|
||||
tokio::time::timeout(std::time::Duration::from_secs(3), rx.recv())
|
||||
.await
|
||||
.context("Failed to recieve reply Timeout")?
|
||||
.ok_or_else(|| anyhow!("Failed to recieve reply"))
|
||||
} else {
|
||||
Err(anyhow!("Failed to send camera data over crossbeam"))
|
||||
}
|
||||
let (tx, mut rx) = channel(1);
|
||||
self.channel_in
|
||||
.send(ToCamera::SendAndReply {
|
||||
message: msg,
|
||||
reply: tx,
|
||||
})
|
||||
.await
|
||||
.context("Failed to send message from camera")?;
|
||||
tokio::time::timeout(std::time::Duration::from_secs(3), rx.recv())
|
||||
.await
|
||||
.context("Failed to recieve reply Timeout")?
|
||||
.ok_or_else(|| anyhow!("Failed to recieve reply"))
|
||||
}
|
||||
}
|
||||
pub(crate) struct EventCam {
|
||||
sender: EventCamSender,
|
||||
channel_out: Receiver<Messages>,
|
||||
set: JoinSet<Result<()>>,
|
||||
}
|
||||
|
||||
pub(crate) fn abort(&self) {
|
||||
self.app.abort(&self.config.name);
|
||||
}
|
||||
|
||||
pub(crate) async fn start_listening(&self) {
|
||||
let loop_config = self.config.clone();
|
||||
|
||||
impl EventCam {
|
||||
pub(crate) async fn new(config: Arc<CameraConfig>) -> Self {
|
||||
// Channels from the camera
|
||||
let (tx, rx) = channel(40);
|
||||
let loop_tx = Arc::new(Mutex::new(tx));
|
||||
*self.channel_out.lock().await = Some(rx);
|
||||
|
||||
// Channels to the camera
|
||||
let (stx, srx) = channel(40);
|
||||
let loop_rx = Arc::new(Mutex::new(srx));
|
||||
*self.channel_in.lock().await = Some(stx);
|
||||
|
||||
while self.app.running(&self.config.name) {
|
||||
// Ignore errors and just loop
|
||||
let _ = Self::cam_run(
|
||||
&loop_config,
|
||||
loop_tx.clone(),
|
||||
loop_rx.clone(),
|
||||
self.app.clone(),
|
||||
)
|
||||
.await;
|
||||
let mut set = JoinSet::<Result<()>>::new();
|
||||
let thread_config = config;
|
||||
let mut eventcam_thread = EventCamThread { rx: srx, tx };
|
||||
set.spawn(async move {
|
||||
let mut wait_for = tokio::time::Duration::from_micros(125);
|
||||
loop {
|
||||
// Ignore errors and just loop
|
||||
tokio::task::yield_now().await;
|
||||
if let Err(e) = eventcam_thread.cam_run(&thread_config).await {
|
||||
warn!("Camera thread error: {:?}. Restarting...", e);
|
||||
tokio::time::sleep(wait_for).await;
|
||||
wait_for *= 2;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Self {
|
||||
sender: EventCamSender { channel_in: stx },
|
||||
channel_out: rx,
|
||||
set,
|
||||
}
|
||||
}
|
||||
|
||||
async fn cam_run(
|
||||
camera_config: &CameraConfig,
|
||||
tx: Arc<Mutex<Sender<Messages>>>,
|
||||
rx: Arc<Mutex<Receiver<ToCamera>>>,
|
||||
app: Arc<App>,
|
||||
) -> Result<()> {
|
||||
/// This will also error is the join set errors
|
||||
pub(crate) async fn poll(&mut self) -> Result<Messages> {
|
||||
let (incoming, set) = (&mut self.channel_out, &mut self.set);
|
||||
tokio::select! {
|
||||
v = incoming.recv() => v.with_context(|| "Camera Polling error"),
|
||||
v = async {
|
||||
while let Some(res) = set.join_next().await {
|
||||
match res {
|
||||
Err(e) => {
|
||||
set.abort_all();
|
||||
return Err(e.into());
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
set.abort_all();
|
||||
return Err(e);
|
||||
}
|
||||
Ok(Ok(())) => {}
|
||||
}
|
||||
}
|
||||
Err(anyhow!("Camera background thread dropped without error"))
|
||||
} => v.with_context(|| "Camera Threads aborted"),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_sender(&self) -> EventCamSender {
|
||||
self.sender.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for EventCam {
|
||||
fn drop(&mut self) {
|
||||
self.set.abort_all();
|
||||
}
|
||||
}
|
||||
|
||||
struct EventCamThread {
|
||||
rx: Receiver<ToCamera>,
|
||||
tx: Sender<Messages>,
|
||||
}
|
||||
|
||||
impl EventCamThread {
|
||||
async fn cam_run(&mut self, camera_config: &CameraConfig) -> Result<()> {
|
||||
let camera_addr = AddressOrUid::new(
|
||||
&camera_config.camera_addr,
|
||||
&camera_config.camera_uid,
|
||||
@@ -168,47 +181,58 @@ impl EventCam {
|
||||
})?;
|
||||
|
||||
info!("{}: Logging in", camera_config.name);
|
||||
let max_encryption = match camera_config.max_encryption.to_lowercase().as_str() {
|
||||
"none" => MaxEncryption::None,
|
||||
"bcencrypt" => MaxEncryption::BcEncrypt,
|
||||
"aes" => MaxEncryption::Aes,
|
||||
_ => MaxEncryption::Aes,
|
||||
};
|
||||
camera
|
||||
.login()
|
||||
.login_with_maxenc(max_encryption)
|
||||
.await
|
||||
.context("Failed to login to the camera")?;
|
||||
info!("{}: Connected and logged in", camera_config.name);
|
||||
|
||||
(tx.lock().await).send(Messages::Login).await?;
|
||||
self.tx.send(Messages::Login).await?;
|
||||
|
||||
// Shararble cameras
|
||||
let arc_cam = Arc::new(camera);
|
||||
|
||||
let mut motion_thread = MotionThread {
|
||||
app: app.clone(),
|
||||
tx,
|
||||
tx: self.tx.clone(),
|
||||
camera: arc_cam.clone(),
|
||||
name: camera_config.name.to_string(),
|
||||
};
|
||||
|
||||
let mut message_handler = MessageHandler {
|
||||
app: app.clone(),
|
||||
rx,
|
||||
rx: &mut self.rx,
|
||||
camera: arc_cam,
|
||||
name: camera_config.name.to_string(),
|
||||
};
|
||||
|
||||
let camera_name = camera_config.name.clone();
|
||||
info!("{}: Listening to Camera Motion", camera_config.name);
|
||||
let handle_motion = tokio::task::spawn(async move {
|
||||
if let Err(e) = motion_thread.run().await {
|
||||
error!("Motion thread aborted: {:?}", e);
|
||||
}
|
||||
app.abort(&camera_name);
|
||||
});
|
||||
info!("{}: Setting up camera actions", camera_config.name);
|
||||
let handle_camera = tokio::task::spawn(async move {
|
||||
message_handler.listen().await;
|
||||
});
|
||||
|
||||
tokio::select! {
|
||||
val = async {handle_motion.await} => {val},
|
||||
val = async {handle_camera.await} => {val}
|
||||
val = async {
|
||||
info!("{}: Listening to Camera Motion", camera_config.name);
|
||||
motion_thread.run().await
|
||||
} => {
|
||||
if let Err(e) = val {
|
||||
error!("Motion thread aborted: {:?}", e);
|
||||
Err(e)
|
||||
} else {
|
||||
debug!("Normal finish on motion thread");
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
val = async {
|
||||
info!("{}: Setting up camera actions", camera_config.name);
|
||||
message_handler.listen().await
|
||||
} => {
|
||||
if let Err(e) = val {
|
||||
error!("Camera message handler aborted: {:?}", e);
|
||||
Err(e)
|
||||
} else {
|
||||
debug!("Normal finish on camera message thread");
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
}?;
|
||||
|
||||
Ok(())
|
||||
@@ -216,26 +240,26 @@ impl EventCam {
|
||||
}
|
||||
|
||||
struct MotionThread {
|
||||
app: Arc<App>,
|
||||
tx: Arc<Mutex<Sender<Messages>>>,
|
||||
tx: Sender<Messages>,
|
||||
camera: Arc<BcCamera>,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl MotionThread {
|
||||
async fn run(&mut self) -> Result<()> {
|
||||
let mut motion_data = self.camera.listen_on_motion().await?;
|
||||
while self.app.running(&format!("app:{}", self.name)) {
|
||||
for motion_status in motion_data.consume_motion_events()?.drain(..) {
|
||||
let mut queued_motion = motion_data.consume_motion_events()?;
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
for motion_status in queued_motion.drain(..) {
|
||||
match motion_status {
|
||||
MotionStatus::Start(_) => {
|
||||
(self.tx.lock().await)
|
||||
self.tx
|
||||
.send(Messages::MotionStart)
|
||||
.await
|
||||
.map_err(|_| BcError::Other("Failed to send Message"))?;
|
||||
}
|
||||
MotionStatus::Stop(_) => {
|
||||
(self.tx.lock().await)
|
||||
self.tx
|
||||
.send(Messages::MotionStop)
|
||||
.await
|
||||
.map_err(|_| BcError::Other("Failed to send Message"))?;
|
||||
@@ -243,185 +267,201 @@ impl MotionThread {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
queued_motion.push(motion_data.next_motion().await?);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct MessageHandler {
|
||||
app: Arc<App>,
|
||||
rx: Arc<Mutex<Receiver<ToCamera>>>,
|
||||
struct MessageHandler<'a> {
|
||||
rx: &'a mut Receiver<ToCamera>,
|
||||
camera: Arc<BcCamera>,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl MessageHandler {
|
||||
async fn listen(&mut self) {
|
||||
while self.app.running(&format!("app:{}", self.name)) {
|
||||
// Try and lock don't worry if not
|
||||
if let Ok(ref mut channel_out) = self.rx.try_lock() {
|
||||
match channel_out.try_recv() {
|
||||
Ok(to_camera) => {
|
||||
let (replier, message) = match to_camera {
|
||||
ToCamera::Send(message) => (None, message),
|
||||
ToCamera::SendAndReply { message, reply } => (Some(reply), message),
|
||||
};
|
||||
let reply = match message {
|
||||
Messages::Reboot => {
|
||||
if self.camera.reboot().await.is_err() {
|
||||
error!("Failed to reboot the camera");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
impl<'a> MessageHandler<'a> {
|
||||
async fn listen(&mut self) -> Result<()> {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
match self.rx.recv().await {
|
||||
Some(to_camera) => {
|
||||
let (replier, message) = match to_camera {
|
||||
ToCamera::Send(message) => (None, message),
|
||||
ToCamera::SendAndReply { message, reply } => (Some(reply), message),
|
||||
};
|
||||
let mut error = None;
|
||||
let reply = match message {
|
||||
Messages::Reboot => {
|
||||
if let Err(e) = self.camera.reboot().await {
|
||||
error = Some(format!("Failed to reboot the camera: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
Messages::StatusLedOn => {
|
||||
if self.camera.led_light_set(true).await.is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::StatusLedOff => {
|
||||
if self.camera.led_light_set(false).await.is_err() {
|
||||
error!("Failed to turn off the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::IRLedOn => {
|
||||
if self.camera.irled_light_set(LightState::On).await.is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::IRLedOff => {
|
||||
if self.camera.irled_light_set(LightState::Off).await.is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::IRLedAuto => {
|
||||
if self.camera.irled_light_set(LightState::Auto).await.is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::Battery => match self.camera.battery_info().await {
|
||||
Err(_) => {
|
||||
error!("Failed to get battery status");
|
||||
"FAIL".to_string()
|
||||
}
|
||||
Ok(battery_info) => {
|
||||
let bytes_res = yaserde::ser::serialize_with_writer(
|
||||
&battery_info,
|
||||
vec![],
|
||||
&Default::default(),
|
||||
);
|
||||
match bytes_res {
|
||||
Ok(bytes) => match String::from_utf8(bytes) {
|
||||
Ok(str) => str,
|
||||
Err(_) => {
|
||||
error!("Failed to encode battery status");
|
||||
"FAIL".to_string()
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
error!("Failed to serialise battery status");
|
||||
"FAIL".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Messages::PIRQuery => match self.camera.get_pirstate().await {
|
||||
Err(_) => {
|
||||
error!("Failed to get pir status");
|
||||
"FAIL".to_string()
|
||||
}
|
||||
Ok(pir_info) => {
|
||||
let bytes_res = yaserde::ser::serialize_with_writer(
|
||||
&pir_info,
|
||||
vec![],
|
||||
&Default::default(),
|
||||
);
|
||||
match bytes_res {
|
||||
Ok(bytes) => match String::from_utf8(bytes) {
|
||||
Ok(str) => str,
|
||||
Err(_) => {
|
||||
error!("Failed to encode pir status");
|
||||
"FAIL".to_string()
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
error!("Failed to serialise pir status");
|
||||
"FAIL".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Messages::PIROn => {
|
||||
if self.camera.pir_set(true).await.is_err() {
|
||||
error!("Failed to turn on the pir");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::PIROff => {
|
||||
if self.camera.pir_set(false).await.is_err() {
|
||||
error!("Failed to turn on the pir");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::Ptz(direction) => {
|
||||
let (bc_direction, amount) = match direction {
|
||||
Direction::Up(amount) => (BcDirection::Up, amount),
|
||||
Direction::Down(amount) => (BcDirection::Down, amount),
|
||||
Direction::Left(amount) => (BcDirection::Left, amount),
|
||||
Direction::Right(amount) => (BcDirection::Right, amount),
|
||||
Direction::In(amount) => (BcDirection::In, amount),
|
||||
Direction::Out(amount) => (BcDirection::Out, amount),
|
||||
};
|
||||
if self.camera.send_ptz(bc_direction, amount).await.is_err() {
|
||||
error!("Failed to turn on the status light");
|
||||
self.abort();
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
_ => "UNKNOWN COMMAND".to_string(),
|
||||
};
|
||||
if let Some(replier) = replier {
|
||||
let _ = replier.send(reply).await;
|
||||
}
|
||||
Messages::StatusLedOn => {
|
||||
if let Err(e) = self.camera.led_light_set(true).await {
|
||||
error =
|
||||
Some(format!("Failed to turn on the status light: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::StatusLedOff => {
|
||||
if let Err(e) = self.camera.led_light_set(false).await {
|
||||
error =
|
||||
Some(format!("Failed to turn off the status light: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::IRLedOn => {
|
||||
if let Err(e) = self.camera.irled_light_set(LightState::On).await {
|
||||
error =
|
||||
Some(format!("Failed to turn on the status light: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::IRLedOff => {
|
||||
if let Err(e) = self.camera.irled_light_set(LightState::Off).await {
|
||||
error =
|
||||
Some(format!("Failed to turn on the status light: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::IRLedAuto => {
|
||||
if let Err(e) = self.camera.irled_light_set(LightState::Auto).await {
|
||||
error =
|
||||
Some(format!("Failed to turn on the status light: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::Battery => match self.camera.battery_info().await {
|
||||
Err(e) => {
|
||||
error!("Failed to get battery status: {:?}", e);
|
||||
"FAIL".to_string()
|
||||
}
|
||||
Ok(battery_info) => {
|
||||
let bytes_res = yaserde::ser::serialize_with_writer(
|
||||
&battery_info,
|
||||
vec![],
|
||||
&Default::default(),
|
||||
);
|
||||
match bytes_res {
|
||||
Ok(bytes) => match String::from_utf8(bytes) {
|
||||
Ok(str) => str,
|
||||
Err(_) => {
|
||||
error!("Failed to encode battery status");
|
||||
"FAIL".to_string()
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
error!("Failed to serialise battery status");
|
||||
"FAIL".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Messages::PIRQuery => match self.camera.get_pirstate().await {
|
||||
Err(e) => {
|
||||
error!("Failed to get pir status: {:?}", e);
|
||||
"FAIL".to_string()
|
||||
}
|
||||
Ok(pir_info) => {
|
||||
let bytes_res = yaserde::ser::serialize_with_writer(
|
||||
&pir_info,
|
||||
vec![],
|
||||
&Default::default(),
|
||||
);
|
||||
match bytes_res {
|
||||
Ok(bytes) => match String::from_utf8(bytes) {
|
||||
Ok(str) => str,
|
||||
Err(_) => {
|
||||
error!("Failed to encode pir status");
|
||||
"FAIL".to_string()
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
error!("Failed to serialise pir status");
|
||||
"FAIL".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Messages::PIROn => {
|
||||
if let Err(e) = self.camera.pir_set(true).await {
|
||||
error = Some(format!("Failed to turn on the pir: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::PIROff => {
|
||||
if let Err(e) = self.camera.pir_set(false).await {
|
||||
error = Some(format!("Failed to turn on the pir: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
Messages::Ptz(direction) => {
|
||||
let (bc_direction, amount, seconds) = match direction {
|
||||
Direction::Up(amount, seconds) => {
|
||||
(BcDirection::Up, amount, seconds)
|
||||
}
|
||||
Direction::Down(amount, seconds) => {
|
||||
(BcDirection::Down, amount, seconds)
|
||||
}
|
||||
Direction::Left(amount, seconds) => {
|
||||
(BcDirection::Left, amount, seconds)
|
||||
}
|
||||
Direction::Right(amount, seconds) => {
|
||||
(BcDirection::Right, amount, seconds)
|
||||
}
|
||||
Direction::In(amount, seconds) => {
|
||||
(BcDirection::In, amount, seconds)
|
||||
}
|
||||
Direction::Out(amount, seconds) => {
|
||||
(BcDirection::Out, amount, seconds)
|
||||
}
|
||||
};
|
||||
if let Err(e) = self.camera.send_ptz(bc_direction, amount).await {
|
||||
error = Some(format!("Failed to send PTZ: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
// sleep for the designated seconds
|
||||
sleep(Duration::from_secs_f32(seconds)).await;
|
||||
|
||||
// note that amount is not used in the stop command
|
||||
if let Err(e) =
|
||||
self.camera.send_ptz(BcDirection::Stop, amount).await
|
||||
{
|
||||
error = Some(format!("Failed to send PTZ: {:?}", e));
|
||||
"FAIL".to_string()
|
||||
} else {
|
||||
"OK".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => "UNKNOWN COMMAND".to_string(),
|
||||
};
|
||||
if let Some(replier) = replier {
|
||||
let _ = replier.send(reply).await;
|
||||
}
|
||||
Err(TryRecvError::Empty) => (),
|
||||
Err(TryRecvError::Disconnected) => self.abort(),
|
||||
if let Some(error) = error {
|
||||
return Err(anyhow!("{}", error));
|
||||
}
|
||||
}
|
||||
None => {
|
||||
return Err(anyhow!("Message handller channel dropped"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn abort(&self) {
|
||||
self.app.abort(&self.name);
|
||||
}
|
||||
}
|
||||
+279
-197
@@ -1,4 +1,3 @@
|
||||
use log::*;
|
||||
///
|
||||
/// # Neolink MQTT
|
||||
///
|
||||
@@ -54,27 +53,29 @@ use log::*;
|
||||
// `credentials` are the username and password required to identify with the mqtt server
|
||||
//
|
||||
use std::sync::Arc;
|
||||
use tokio::time::{sleep, Duration};
|
||||
|
||||
mod app;
|
||||
mod cmdline;
|
||||
mod event_cam;
|
||||
mod mqttc;
|
||||
|
||||
use crate::config::{CameraConfig, Config, MqttConfig};
|
||||
use anyhow::{anyhow, Result};
|
||||
pub(crate) use app::App;
|
||||
use anyhow::{anyhow, Context, Error, Result};
|
||||
pub(crate) use cmdline::Opt;
|
||||
use event_cam::EventCam;
|
||||
pub(crate) use event_cam::{Direction, Messages};
|
||||
use log::*;
|
||||
use mqttc::{Mqtt, MqttReplyRef};
|
||||
|
||||
use self::{
|
||||
event_cam::EventCamSender,
|
||||
mqttc::{MqttReply, MqttSender},
|
||||
};
|
||||
|
||||
/// Entry point for the mqtt subcommand
|
||||
///
|
||||
/// Opt is the command line options
|
||||
pub(crate) async fn main(_: Opt, config: Config) -> Result<()> {
|
||||
let app = App::new();
|
||||
let arc_app = Arc::new(app);
|
||||
|
||||
if config.cameras.iter().all(|config| config.mqtt.is_none()) {
|
||||
return Err(anyhow!(
|
||||
"MQTT command run, but no cameras configured with MQTT settings. Exiting."
|
||||
@@ -89,219 +90,300 @@ pub(crate) async fn main(_: Opt, config: Config) -> Result<()> {
|
||||
.collect::<Vec<_>>()
|
||||
{
|
||||
if let Some(mqtt_config) = camera_config.mqtt.as_ref().map(|a| Arc::new(a.clone())) {
|
||||
let loop_arc_app = arc_app.clone();
|
||||
info!("{}: Setting up mqtt", camera_config.name);
|
||||
set.spawn(async move {
|
||||
while loop_arc_app.running("app") {
|
||||
let _ =
|
||||
listen_on_camera(camera_config.clone(), &mqtt_config, loop_arc_app.clone())
|
||||
.await;
|
||||
let mut wait_for = Duration::from_micros(125);
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
if let Err(e) = listen_on_camera(camera_config.clone(), &mqtt_config).await {
|
||||
warn!("Error: {:?}. Retrying", e);
|
||||
}
|
||||
sleep(wait_for).await;
|
||||
wait_for *= 2;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(result) = set.join_next().await {
|
||||
while let Some(result) = set.join_next().await {
|
||||
result?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn listen_on_camera(
|
||||
cam_config: Arc<CameraConfig>,
|
||||
mqtt_config: &MqttConfig,
|
||||
arc_app: Arc<App>,
|
||||
) -> Result<()> {
|
||||
async fn listen_on_camera(cam_config: Arc<CameraConfig>, mqtt_config: &MqttConfig) -> Result<()> {
|
||||
// Camera thread
|
||||
let arc_event_cam = Arc::new(EventCam::new(cam_config.clone(), arc_app.clone()));
|
||||
let arc_mqtt = Mqtt::new(mqtt_config, &cam_config.name, arc_app.clone());
|
||||
let mut set = tokio::task::JoinSet::new();
|
||||
// Start listening to camera events
|
||||
let event_cam = arc_event_cam.clone();
|
||||
set.spawn(async move {
|
||||
event_cam.start_listening().await; // Loop forever
|
||||
event_cam.abort(); // Just to ensure everything aborts
|
||||
});
|
||||
let mut event_cam = EventCam::new(cam_config.clone()).await;
|
||||
let mut mqtt = Mqtt::new(mqtt_config, &cam_config.name).await;
|
||||
|
||||
// Start listening to mqtt events
|
||||
let event_cam = arc_event_cam.clone();
|
||||
let mqtt = arc_mqtt.clone();
|
||||
set.spawn(async move {
|
||||
let _ = mqtt.start().is_err();
|
||||
event_cam.abort();
|
||||
});
|
||||
|
||||
// Listen on camera messages and post on mqtt
|
||||
let camera_name = cam_config.name.clone();
|
||||
let event_cam = arc_event_cam.clone();
|
||||
let mqtt = arc_mqtt.clone();
|
||||
let app = arc_app.clone();
|
||||
set.spawn(async move {
|
||||
while app.running(&format!("app: {}", camera_name)) {
|
||||
if let Ok(msg) = event_cam.poll().await {
|
||||
match msg {
|
||||
Messages::Login => {
|
||||
if mqtt.send_message("status", "connected", true).is_err() {
|
||||
error!("Failed to post connect over MQTT for {}", camera_name);
|
||||
}
|
||||
}
|
||||
Messages::MotionStop => {
|
||||
if mqtt.send_message("status/motion", "off", true).is_err() {
|
||||
error!("Failed to publish motion stop for {}", camera_name);
|
||||
}
|
||||
}
|
||||
Messages::MotionStart => {
|
||||
if mqtt.send_message("status/motion", "on", true).is_err() {
|
||||
error!("Failed to publish motion start for {}", camera_name);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
let mqtt_sender_cam = mqtt.get_sender();
|
||||
let mqtt_sender_mqtt = mqtt.get_sender();
|
||||
let event_cam_sender = event_cam.get_sender();
|
||||
|
||||
// Listen on mqtt messages and post on camera
|
||||
let event_cam = arc_event_cam.clone();
|
||||
let mqtt = arc_mqtt.clone();
|
||||
let app = arc_app.clone();
|
||||
let camera_name = cam_config.name.clone();
|
||||
set.spawn(async move {
|
||||
while app.running(&format!("app: {}", camera_name)) {
|
||||
if let Ok(msg) = mqtt.poll() {
|
||||
match msg.as_ref() {
|
||||
MqttReplyRef {
|
||||
topic: "control/led",
|
||||
message: "on",
|
||||
} => {
|
||||
if event_cam.send_message(Messages::StatusLedOn).await.is_err() {
|
||||
error!("Failed to set camera status light on");
|
||||
// When one error is recieved we pass it up async
|
||||
let (error_send, mut error_recv) = tokio::sync::mpsc::channel::<Error>(1);
|
||||
|
||||
let mqtt_to_cam = async {
|
||||
// Select to wait on an error (via the channel) or normal poll ops
|
||||
tokio::select! {
|
||||
v = async {
|
||||
// Normal poll operations
|
||||
while let Ok(msg) = mqtt.poll().await {
|
||||
tokio::task::yield_now().await;
|
||||
// Put the reply on it's own async thread so we can safely sleep
|
||||
// and wait for it to reply in it's own time
|
||||
let event_cam_sender = event_cam_sender.clone();
|
||||
let mqtt_sender_mqtt = mqtt_sender_mqtt.clone();
|
||||
let error_send = error_send.clone();
|
||||
tokio::task::spawn(async move {
|
||||
// Handle the message and wait for ok/error on this thread
|
||||
let result: Result<()> = handle_mqtt_message(&msg, event_cam_sender, mqtt_sender_mqtt).await;
|
||||
// If there is an error we pass it to the channel
|
||||
// this allows for async error handelling
|
||||
if let Err(e) = result {
|
||||
let _ = error_send.try_send(e);
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/led",
|
||||
message: "off",
|
||||
} => {
|
||||
if event_cam
|
||||
.send_message(Messages::StatusLedOff)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "on",
|
||||
} => {
|
||||
if event_cam.send_message(Messages::IRLedOn).await.is_err() {
|
||||
error!("Failed to set camera status light on");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "off",
|
||||
} => {
|
||||
if event_cam.send_message(Messages::IRLedOff).await.is_err() {
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "auto",
|
||||
} => {
|
||||
if event_cam.send_message(Messages::IRLedAuto).await.is_err() {
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/reboot",
|
||||
..
|
||||
} => {
|
||||
if event_cam.send_message(Messages::Reboot).await.is_err() {
|
||||
error!("Failed to set camera status light off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ptz",
|
||||
message,
|
||||
} => {
|
||||
let lowercase_message = message.to_lowercase();
|
||||
let mut words = lowercase_message.split_whitespace();
|
||||
if let Some(direction_txt) = words.next() {
|
||||
let amount = words.next().unwrap_or("32.0");
|
||||
if let Ok(amount) = amount.parse::<f32>() {
|
||||
let direction = match direction_txt {
|
||||
"up" => Direction::Up(amount),
|
||||
"down" => Direction::Down(amount),
|
||||
"left" => Direction::Left(amount),
|
||||
"right" => Direction::Right(amount),
|
||||
"in" => Direction::In(amount),
|
||||
"out" => Direction::Out(amount),
|
||||
_ => {
|
||||
error!("Unrecongnized PTZ direction");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if event_cam
|
||||
.send_message(Messages::Ptz(direction))
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to send PTZ");
|
||||
}
|
||||
} else {
|
||||
error!("No PTZ direction speed was not a valid number");
|
||||
}
|
||||
} else {
|
||||
error!("No PTZ Direction given. Please add up/down/left/right/in/out");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/pir",
|
||||
message: "on",
|
||||
} => {
|
||||
if event_cam.send_message(Messages::PIROn).await.is_err() {
|
||||
error!("Failed to set pir on");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/pir",
|
||||
message: "off",
|
||||
} => {
|
||||
if event_cam.send_message(Messages::PIROff).await.is_err() {
|
||||
error!("Failed to set pir off");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "query/battery",
|
||||
..
|
||||
} => match event_cam.send_message_with_reply(Messages::Battery).await {
|
||||
Ok(reply) => {
|
||||
if mqtt.send_message("status/battery", &reply, false).is_err() {
|
||||
error!("Failed to send battery status reply");
|
||||
}
|
||||
}
|
||||
Err(_) => error!("Failed to get battery status"),
|
||||
},
|
||||
MqttReplyRef {
|
||||
topic: "query/pir", ..
|
||||
} => match event_cam.send_message_with_reply(Messages::PIRQuery).await {
|
||||
Ok(reply) => {
|
||||
if mqtt.send_message("status/pir", &reply, false).is_err() {
|
||||
error!("Failed to send pir status reply");
|
||||
}
|
||||
}
|
||||
Err(_) => error!("Failed to get pir status"),
|
||||
},
|
||||
_ => {}
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
} => v,
|
||||
// Wait on any error from any of the error channels and if we get it we abort
|
||||
v = error_recv.recv() => v.map(Err).unwrap_or_else(|| Err(anyhow!("Listen on camera error channel closed"))),
|
||||
}
|
||||
};
|
||||
|
||||
let cam_to_mqtt = async {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
match event_cam.poll().await? {
|
||||
Messages::Login => {
|
||||
mqtt_sender_cam
|
||||
.send_message("status", "connected", true)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!("Failed to post connect over MQTT for {}", camera_name)
|
||||
})?;
|
||||
}
|
||||
Messages::MotionStop => {
|
||||
mqtt_sender_cam
|
||||
.send_message("status/motion", "off", true)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!("Failed to publish motion stop for {}", camera_name)
|
||||
})?;
|
||||
}
|
||||
Messages::MotionStart => {
|
||||
mqtt_sender_cam
|
||||
.send_message("status/motion", "on", true)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!("Failed to publish motion start for {}", camera_name)
|
||||
})?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
while set.join_next().await.is_some() {}
|
||||
tokio::select! {
|
||||
v = mqtt_to_cam => {v},
|
||||
v = cam_to_mqtt => {v},
|
||||
}?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_mqtt_message(
|
||||
msg: &MqttReply,
|
||||
event_cam_sender: EventCamSender,
|
||||
mqtt_sender_mqtt: MqttSender,
|
||||
) -> Result<()> {
|
||||
let mut reply = None;
|
||||
let mut reply_topic = None;
|
||||
match msg.as_ref() {
|
||||
MqttReplyRef {
|
||||
topic: _,
|
||||
message: "OK",
|
||||
}
|
||||
| MqttReplyRef {
|
||||
topic: _,
|
||||
message: "FAIL",
|
||||
} => {
|
||||
// Do nothing for the success/fail replies
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/led",
|
||||
message: "on",
|
||||
} => {
|
||||
reply = Some(
|
||||
event_cam_sender
|
||||
.send_message_with_reply(Messages::StatusLedOn)
|
||||
.await
|
||||
.with_context(|| "Failed to set camera status light on")?,
|
||||
);
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/led",
|
||||
message: "off",
|
||||
} => {
|
||||
reply = Some(
|
||||
event_cam_sender
|
||||
.send_message_with_reply(Messages::StatusLedOff)
|
||||
.await
|
||||
.with_context(|| "Failed to set camera status light off")?,
|
||||
);
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "on",
|
||||
} => {
|
||||
reply = Some(
|
||||
event_cam_sender
|
||||
.send_message_with_reply(Messages::IRLedOn)
|
||||
.await
|
||||
.with_context(|| "Failed to set camera status light on")?,
|
||||
);
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "off",
|
||||
} => {
|
||||
reply = Some(
|
||||
event_cam_sender
|
||||
.send_message_with_reply(Messages::IRLedOff)
|
||||
.await
|
||||
.with_context(|| "Failed to set camera status light off")?,
|
||||
);
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ir",
|
||||
message: "auto",
|
||||
} => {
|
||||
reply = Some(
|
||||
event_cam_sender
|
||||
.send_message_with_reply(Messages::IRLedAuto)
|
||||
.await
|
||||
.with_context(|| "Failed to set camera status light off")?,
|
||||
);
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/reboot",
|
||||
..
|
||||
} => {
|
||||
reply = Some(
|
||||
event_cam_sender
|
||||
.send_message_with_reply(Messages::Reboot)
|
||||
.await
|
||||
.with_context(|| "Failed to set camera status light off")?,
|
||||
);
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/ptz",
|
||||
message,
|
||||
} => {
|
||||
let lowercase_message = message.to_lowercase();
|
||||
let mut words = lowercase_message.split_whitespace();
|
||||
if let Some(direction_txt) = words.next() {
|
||||
// Target amount to move
|
||||
let amount = words.next().unwrap_or("32.0");
|
||||
if let Ok(amount) = amount.parse::<f32>() {
|
||||
let seconds = amount / 32f32;
|
||||
// range checking on seconds so that you can't sleep for 3.4E+38 seconds
|
||||
match seconds {
|
||||
x if (0.0..10.0).contains(&x) => seconds,
|
||||
_ => {
|
||||
error!("seconds was not a valid number (out of range)");
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let direction = match direction_txt {
|
||||
"up" => Direction::Up(amount, seconds),
|
||||
"down" => Direction::Down(amount, seconds),
|
||||
"left" => Direction::Left(amount, seconds),
|
||||
"right" => Direction::Right(amount, seconds),
|
||||
"in" => Direction::In(amount, seconds),
|
||||
"out" => Direction::Out(amount, seconds),
|
||||
_ => {
|
||||
error!("Unrecognized PTZ direction \"{}\"", direction_txt);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
reply = Some(
|
||||
event_cam_sender
|
||||
.send_message_with_reply(Messages::Ptz(direction))
|
||||
.await
|
||||
.with_context(|| "Failed to send PTZ")?,
|
||||
);
|
||||
} else {
|
||||
error!("No PTZ direction speed was not a valid number");
|
||||
}
|
||||
} else {
|
||||
error!("No PTZ Direction given. Please add up/down/left/right/in/out");
|
||||
}
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/pir",
|
||||
message: "on",
|
||||
} => {
|
||||
reply = Some(
|
||||
event_cam_sender
|
||||
.send_message_with_reply(Messages::PIROn)
|
||||
.await
|
||||
.with_context(|| "Failed to set pir on")?,
|
||||
);
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "control/pir",
|
||||
message: "off",
|
||||
} => {
|
||||
reply = Some(
|
||||
event_cam_sender
|
||||
.send_message_with_reply(Messages::PIROff)
|
||||
.await
|
||||
.with_context(|| "Failed to set pir off")?,
|
||||
);
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "query/battery",
|
||||
..
|
||||
} => {
|
||||
reply = Some(
|
||||
event_cam_sender
|
||||
.send_message_with_reply(Messages::Battery)
|
||||
.await
|
||||
.with_context(|| "Failed to get battery status")?,
|
||||
);
|
||||
reply_topic = Some("status/battery");
|
||||
}
|
||||
MqttReplyRef {
|
||||
topic: "query/pir", ..
|
||||
} => {
|
||||
reply = Some(
|
||||
event_cam_sender
|
||||
.send_message_with_reply(Messages::PIRQuery)
|
||||
.await
|
||||
.with_context(|| "Failed to get pir status")?,
|
||||
);
|
||||
reply_topic = Some("status/pir");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if let Some(reply) = reply {
|
||||
if let Some(topic) = reply_topic {
|
||||
mqtt_sender_mqtt
|
||||
.send_message(topic, &reply, false)
|
||||
.await
|
||||
.with_context(|| "Failed to send Camera reply to Mqtt")?;
|
||||
} else {
|
||||
mqtt_sender_mqtt
|
||||
.send_message(&msg.topic, &reply, false)
|
||||
.await
|
||||
.with_context(|| "Failed to send Camera reply to Mqtt")?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
+170
-145
@@ -1,23 +1,21 @@
|
||||
use super::App;
|
||||
use crate::config::MqttConfig;
|
||||
use anyhow::{Context, Result};
|
||||
use crossbeam_channel::{unbounded, Receiver, RecvError, Sender};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::*;
|
||||
use rumqttc::{
|
||||
Client, ClientError, ConnectReturnCode, Connection, Event, Incoming, Key, LastWill,
|
||||
AsyncClient, ClientError, ConnectReturnCode, Event, EventLoop, Incoming, Key, LastWill,
|
||||
MqttOptions, Publish, QoS, TlsConfiguration, Transport,
|
||||
};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
||||
use tokio::task::JoinSet;
|
||||
|
||||
pub(crate) struct Mqtt {
|
||||
app: Arc<App>,
|
||||
client: Mutex<Client>,
|
||||
connection: Mutex<Connection>,
|
||||
client: Arc<AsyncClient>,
|
||||
name: String,
|
||||
incoming: (Sender<Publish>, Receiver<Publish>),
|
||||
msg_channel: (Sender<MqttReply>, Receiver<MqttReply>),
|
||||
drop_message: Mutex<Option<MqttReply>>,
|
||||
incoming: Receiver<MqttReply>,
|
||||
set: JoinSet<Result<()>>,
|
||||
drop_message: Option<MqttReply>,
|
||||
}
|
||||
|
||||
pub(crate) struct MqttReply {
|
||||
@@ -39,10 +37,120 @@ pub(crate) struct MqttReplyRef<'a> {
|
||||
pub(crate) message: &'a str,
|
||||
}
|
||||
|
||||
struct MqttReciever {
|
||||
client: MqttSender,
|
||||
incoming_tx: Sender<MqttReply>,
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct MqttSender {
|
||||
client: Arc<AsyncClient>,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl MqttSender {
|
||||
pub async fn send_message(
|
||||
&self,
|
||||
sub_topic: &str,
|
||||
message: &str,
|
||||
retain: bool,
|
||||
) -> Result<(), ClientError> {
|
||||
self.client
|
||||
.publish(
|
||||
format!("neolink/{}/{}", self.name, sub_topic),
|
||||
QoS::AtLeastOnce,
|
||||
retain,
|
||||
message,
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn subscribe(&self) -> Result<(), ClientError> {
|
||||
self.client
|
||||
.subscribe(format!("neolink/{}/#", self.name), QoS::AtMostOnce)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_status(&self) -> Result<()> {
|
||||
self.send_message("status", "disconnected", true).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn try_send_message(&self, sub_topic: &str, message: &str, retain: bool) -> Result<()> {
|
||||
self.client
|
||||
.try_publish(
|
||||
format!("neolink/{}/{}", self.name, sub_topic),
|
||||
QoS::AtLeastOnce,
|
||||
retain,
|
||||
message,
|
||||
)
|
||||
.map_err(|e| e.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl MqttReciever {
|
||||
async fn run(&mut self, connection: &mut EventLoop) -> Result<()> {
|
||||
// This acts as an event loop
|
||||
let name = self.name.clone();
|
||||
info!("Starting MQTT Client for {}", name);
|
||||
loop {
|
||||
let notification = connection
|
||||
.poll()
|
||||
.await
|
||||
.with_context(|| "MQTT connection dropped")?;
|
||||
match notification {
|
||||
Event::Incoming(Incoming::ConnAck(connected)) => {
|
||||
if ConnectReturnCode::Success == connected.code {
|
||||
self.client
|
||||
.update_status()
|
||||
.await
|
||||
.context("Failed to update status")?;
|
||||
// We succesfully logged in. Now ask for the cameras subscription.
|
||||
self.client
|
||||
.subscribe()
|
||||
.await
|
||||
.context("Failed to subscribe")?;
|
||||
}
|
||||
}
|
||||
Event::Incoming(Incoming::Publish(published_message)) => {
|
||||
if self.handle_message(published_message).await.is_err() {
|
||||
error!("Failed to forward messages in mqtt");
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_message(&mut self, published_message: Publish) -> Result<()> {
|
||||
if let Some(sub_topic) = published_message
|
||||
.topic
|
||||
.strip_prefix(&format!("neolink/{}/", &self.name))
|
||||
{
|
||||
if self
|
||||
.incoming_tx
|
||||
.send(MqttReply {
|
||||
topic: sub_topic.to_string(),
|
||||
message: String::from_utf8_lossy(published_message.payload.as_ref())
|
||||
.into_owned(),
|
||||
})
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to send messages up the mqtt msg channel");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Mqtt {
|
||||
pub(crate) fn new(config: &MqttConfig, name: &str, app: Arc<App>) -> Arc<Self> {
|
||||
let incoming = unbounded::<Publish>();
|
||||
let msg_channel = unbounded::<MqttReply>();
|
||||
pub(crate) async fn new(config: &MqttConfig, name: &str) -> Self {
|
||||
let (incoming_tx, incoming) = channel::<MqttReply>(100);
|
||||
let mut mqttoptions = MqttOptions::new(
|
||||
format!("Neolink-{}", name),
|
||||
&config.broker_addr,
|
||||
@@ -91,162 +199,79 @@ impl Mqtt {
|
||||
true,
|
||||
));
|
||||
|
||||
let (client, connection) = Client::new(mqttoptions, 10);
|
||||
let (client, mut connection) = AsyncClient::new(mqttoptions, 10);
|
||||
|
||||
let me = Self {
|
||||
app: app.clone(),
|
||||
client: Mutex::new(client),
|
||||
connection: Mutex::new(connection),
|
||||
name: name.to_string(),
|
||||
incoming,
|
||||
msg_channel,
|
||||
// Send the drop message on clean disconnect
|
||||
drop_message: Mutex::new(Some(MqttReply {
|
||||
topic: "status".to_string(),
|
||||
message: "offline".to_string(),
|
||||
})),
|
||||
};
|
||||
|
||||
let arc_me = Arc::new(me);
|
||||
let client = Arc::new(client);
|
||||
|
||||
// Start the mqtt server
|
||||
let mqtt_running = arc_me.clone();
|
||||
let mqtt_app = app.clone();
|
||||
let mqtt_name = name.to_string();
|
||||
std::thread::spawn(move || {
|
||||
let _ = (*mqtt_running).start();
|
||||
mqtt_app.abort(&mqtt_name)
|
||||
});
|
||||
let mut set = JoinSet::<Result<()>>::new();
|
||||
|
||||
// Start polling messages
|
||||
info!("{}: Starting listening to mqtt", name);
|
||||
let mqtt_read_app = app;
|
||||
let mqtt_reading = arc_me.clone();
|
||||
let mqtt_read_name = name.to_string();
|
||||
std::thread::spawn(move || {
|
||||
while mqtt_read_app.running(&format!("app:{}", mqtt_read_name)) {
|
||||
if (*mqtt_reading).handle_message().is_err() {
|
||||
error!(
|
||||
"Failed to get messages from mqtt client {}",
|
||||
(mqtt_read_name)
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
let mut reciever = MqttReciever {
|
||||
incoming_tx,
|
||||
name: name.to_string(),
|
||||
client: MqttSender {
|
||||
client: client.clone(),
|
||||
name: name.to_string(),
|
||||
},
|
||||
};
|
||||
set.spawn(async move { reciever.run(&mut connection).await });
|
||||
|
||||
arc_me
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn set_drop_message(&mut self, topic: &str, message: &str) {
|
||||
self.drop_message.lock().unwrap().replace(MqttReply {
|
||||
topic: topic.to_string(),
|
||||
message: message.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
fn subscribe(&self) -> Result<(), ClientError> {
|
||||
let mut client = self.client.lock().unwrap();
|
||||
client.subscribe(format!("neolink/{}/#", self.name), QoS::AtMostOnce)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn update_status(&self) -> Result<(), ClientError> {
|
||||
self.send_message("status", "disconnected", true)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn send_message(
|
||||
&self,
|
||||
sub_topic: &str,
|
||||
message: &str,
|
||||
retain: bool,
|
||||
) -> Result<(), ClientError> {
|
||||
let mut client = self.client.lock().unwrap();
|
||||
client.publish(
|
||||
format!("neolink/{}/{}", self.name, sub_topic),
|
||||
QoS::AtLeastOnce,
|
||||
retain,
|
||||
message,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_message(&self) -> Result<(), RecvError> {
|
||||
let (_, receiver) = &self.incoming;
|
||||
let published_message = receiver.recv()?;
|
||||
|
||||
if let Some(sub_topic) = published_message
|
||||
.topic
|
||||
.strip_prefix(&format!("neolink/{}/", &self.name))
|
||||
{
|
||||
if self
|
||||
.msg_channel
|
||||
.0
|
||||
.send(MqttReply {
|
||||
topic: sub_topic.to_string(),
|
||||
message: String::from_utf8_lossy(published_message.payload.as_ref())
|
||||
.into_owned(),
|
||||
})
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to send messages up the mqtt msg channel");
|
||||
}
|
||||
Self {
|
||||
client,
|
||||
name: name.to_string(),
|
||||
incoming,
|
||||
set,
|
||||
// Send the drop message on clean disconnect
|
||||
drop_message: Some(MqttReply {
|
||||
topic: "status".to_string(),
|
||||
message: "offline".to_string(),
|
||||
}),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_message_listener(&self) -> Receiver<MqttReply> {
|
||||
self.msg_channel.1.clone()
|
||||
pub fn get_sender(&self) -> MqttSender {
|
||||
MqttSender {
|
||||
client: self.client.clone(),
|
||||
name: self.name.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn poll(&self) -> Result<MqttReply> {
|
||||
self.get_message_listener()
|
||||
.recv()
|
||||
.context("Mqtt Polling error")
|
||||
}
|
||||
|
||||
pub(crate) fn start(&self) -> Result<()> {
|
||||
// This acts as an event loop
|
||||
let mut connection = self.connection.lock().unwrap();
|
||||
let (sender, _) = &self.incoming;
|
||||
info!("Starting MQTT Client for {}", self.name);
|
||||
while self.app.running(&format!("app:{}", self.name)) {
|
||||
for (_i, notification) in connection.iter().enumerate() {
|
||||
if let Ok(notification) = notification {
|
||||
match notification {
|
||||
Event::Incoming(Incoming::ConnAck(connected)) => {
|
||||
if ConnectReturnCode::Success == connected.code {
|
||||
self.update_status().context("Failed to update status")?;
|
||||
// We succesfully logged in. Now ask for the cameras subscription.
|
||||
self.subscribe().context("Failed to subscribe")?;
|
||||
}
|
||||
/// This will also error is the join set errors
|
||||
pub(crate) async fn poll(&mut self) -> Result<MqttReply> {
|
||||
let (incoming, set) = (&mut self.incoming, &mut self.set);
|
||||
tokio::select! {
|
||||
v = incoming.recv() => v.with_context(|| "Mqtt Polling error"),
|
||||
v = async {
|
||||
while let Some(res) = set.join_next().await {
|
||||
match res {
|
||||
Err(e) => {
|
||||
set.abort_all();
|
||||
return Err(e.into());
|
||||
}
|
||||
Event::Incoming(Incoming::Publish(published_message)) => {
|
||||
if sender.send(published_message).is_err() {
|
||||
error!("Failed to publish motion message on mqtt");
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
set.abort_all();
|
||||
return Err(e);
|
||||
}
|
||||
_ => {}
|
||||
Ok(Ok(())) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(anyhow!("MQTT background thread dropped without error"))
|
||||
} => v.with_context(|| "MQTT Threads aborted"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Mqtt {
|
||||
fn drop(&mut self) {
|
||||
let drop_message = self.drop_message.lock().unwrap();
|
||||
if let Some(drop_message) = drop_message.as_ref() {
|
||||
if let Some(drop_message) = self.drop_message.as_ref() {
|
||||
if self
|
||||
.send_message(&drop_message.topic, &drop_message.message, true)
|
||||
.get_sender()
|
||||
.try_send_message(&drop_message.topic, &drop_message.message, true)
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to send offline message to mqtt");
|
||||
}
|
||||
}
|
||||
self.set.abort_all();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -6,7 +6,7 @@ mod sender;
|
||||
mod server;
|
||||
mod shared;
|
||||
|
||||
use factory::*;
|
||||
pub(crate) use factory::*;
|
||||
|
||||
pub(crate) use self::server::NeoRtspServer;
|
||||
|
||||
|
||||
+284
-168
@@ -11,7 +11,7 @@ use gstreamer::{
|
||||
glib::{self, Object},
|
||||
Structure,
|
||||
};
|
||||
use gstreamer::{Bin, Caps, ClockTime, Element, ElementFactory};
|
||||
use gstreamer::{Bin, Caps, Element, ElementFactory};
|
||||
use gstreamer_app::{AppSrc, AppSrcCallbacks, AppStreamType};
|
||||
use gstreamer_rtsp::RTSPUrl;
|
||||
use gstreamer_rtsp_server::prelude::*;
|
||||
@@ -26,7 +26,10 @@ use std::{
|
||||
sync::{atomic::Ordering, Arc},
|
||||
};
|
||||
use tokio::{
|
||||
sync::mpsc::{channel, Sender},
|
||||
sync::{
|
||||
mpsc::{channel, Sender},
|
||||
RwLock,
|
||||
},
|
||||
task::JoinSet,
|
||||
};
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
@@ -38,14 +41,16 @@ glib::wrapper! {
|
||||
|
||||
impl Default for NeoMediaFactory {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
Self::new(100, true)
|
||||
}
|
||||
}
|
||||
|
||||
impl NeoMediaFactory {
|
||||
pub(crate) fn new() -> Self {
|
||||
pub(crate) fn new(buffer_size: usize, use_smoothing: bool) -> Self {
|
||||
let factory = Object::new::<NeoMediaFactory>();
|
||||
factory.set_shared(false);
|
||||
factory.imp().shared.set_buffer_size(buffer_size);
|
||||
factory.imp().shared.set_use_smoothing(use_smoothing);
|
||||
// factory.set_do_retransmission(false); // Can't use as the method is missing on the 32bit docker gst dll
|
||||
factory.set_launch("videotestsrc pattern=\"snow\" ! video/x-raw,width=896,height=512,framerate=25/1 ! textoverlay name=\"inittextoverlay\" text=\"Stream not Ready\" valignment=top halignment=left font-desc=\"Sans, 32\" ! jpegenc ! rtpjpegpay name=pay0");
|
||||
factory.set_suspend_mode(RTSPSuspendMode::None);
|
||||
@@ -53,7 +58,7 @@ impl NeoMediaFactory {
|
||||
factory
|
||||
}
|
||||
|
||||
pub(crate) fn get_sender(&self) -> Sender<BcMedia> {
|
||||
pub(crate) fn get_sender(&self) -> Sender<FactoryCommand> {
|
||||
self.imp().sender.clone()
|
||||
}
|
||||
|
||||
@@ -103,22 +108,35 @@ impl NeoMediaFactory {
|
||||
pub(crate) fn buffer_ready(&self) -> bool {
|
||||
self.imp().buffer_ready()
|
||||
}
|
||||
|
||||
pub(crate) async fn join(&self) -> AnyResult<()> {
|
||||
self.imp().join().await
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for NeoMediaFactory {}
|
||||
unsafe impl Sync for NeoMediaFactory {}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum FactoryCommand {
|
||||
BcMedia(BcMedia),
|
||||
ClearBuffer,
|
||||
JumpToLive,
|
||||
Pause,
|
||||
Resume,
|
||||
}
|
||||
|
||||
pub(crate) struct NeoMediaFactoryImpl {
|
||||
sender: Sender<BcMedia>,
|
||||
clientsender: Sender<ClientPipelineData>,
|
||||
sender: Sender<FactoryCommand>,
|
||||
clientsender: Sender<NeoMediaSender>,
|
||||
shared: Arc<NeoMediaShared>,
|
||||
#[allow(dead_code)] // Not dead just need a handle to keep it alive and drop with this obj
|
||||
threads: JoinSet<AnyResult<()>>,
|
||||
threads: RwLock<JoinSet<AnyResult<()>>>,
|
||||
}
|
||||
|
||||
impl Drop for NeoMediaFactoryImpl {
|
||||
fn drop(&mut self) {
|
||||
log::info!("Dopping NeoMediaFactoryImpl");
|
||||
log::debug!("Dopping NeoMediaFactoryImpl");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,16 +149,15 @@ impl Default for NeoMediaFactoryImpl {
|
||||
|
||||
// Prepare thread that sends data into the appsrcs
|
||||
let mut threads: JoinSet<AnyResult<()>> = Default::default();
|
||||
let mut sender = NeoMediaSender {
|
||||
data_source: ReceiverStream::new(datarx),
|
||||
clientsource: ReceiverStream::new(rx_clientsender),
|
||||
shared: shared.clone(),
|
||||
uid: Default::default(),
|
||||
clientdata: Default::default(),
|
||||
waiting_for_iframe: true,
|
||||
};
|
||||
let mut sender = NeoMediaSenders::new(
|
||||
shared.clone(),
|
||||
ReceiverStream::new(datarx),
|
||||
ReceiverStream::new(rx_clientsender),
|
||||
100,
|
||||
);
|
||||
threads.spawn(async move {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
match sender.run().await {
|
||||
Err(e) => {
|
||||
warn!("Media send thead failed... restarting. Cause: {:?}", e);
|
||||
@@ -149,16 +166,16 @@ impl Default for NeoMediaFactoryImpl {
|
||||
break;
|
||||
}
|
||||
}
|
||||
sender.waiting_for_iframe = true;
|
||||
}
|
||||
Ok(())
|
||||
unreachable!();
|
||||
// Ok(())
|
||||
});
|
||||
|
||||
Self {
|
||||
sender: datasender,
|
||||
clientsender,
|
||||
shared,
|
||||
threads,
|
||||
threads: RwLock::new(threads),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -209,6 +226,32 @@ fn make_element(kind: &str, name: &str) -> AnyResult<Element> {
|
||||
})
|
||||
}
|
||||
|
||||
fn make_queue(name: &str) -> AnyResult<Element> {
|
||||
let queue = make_element("queue", name)?;
|
||||
queue.set_property_from_str("leaky", "downstream");
|
||||
queue.set_property("max-size-bytes", 0u32);
|
||||
queue.set_property("max-size-buffers", 0u32);
|
||||
queue.set_property(
|
||||
"max-size-time",
|
||||
std::convert::TryInto::<u64>::try_into(tokio::time::Duration::from_secs(5).as_nanos())
|
||||
.unwrap_or(0),
|
||||
);
|
||||
Ok(queue)
|
||||
}
|
||||
|
||||
fn make_queue2(name: &str) -> AnyResult<Element> {
|
||||
let queue = make_element("queue2", name)?;
|
||||
queue.set_property("use-buffering", true);
|
||||
queue.set_property("max-size-bytes", 0u32);
|
||||
queue.set_property("max-size-buffers", 0u32);
|
||||
queue.set_property(
|
||||
"max-size-time",
|
||||
std::convert::TryInto::<u64>::try_into(tokio::time::Duration::from_secs(5).as_nanos())
|
||||
.unwrap_or(0),
|
||||
);
|
||||
Ok(queue)
|
||||
}
|
||||
|
||||
impl NeoMediaFactoryImpl {
|
||||
pub(crate) fn buffer_ready(&self) -> bool {
|
||||
self.shared.buffer_ready.load(Ordering::Relaxed)
|
||||
@@ -222,131 +265,28 @@ impl NeoMediaFactoryImpl {
|
||||
let bin = media
|
||||
.dynamic_cast::<Bin>()
|
||||
.map_err(|_| anyhow!("Media source's element should be a bin"))?;
|
||||
let clock = bin.clock();
|
||||
// Clear the autogenerated ones
|
||||
for element in bin.iterate_elements().into_iter().flatten() {
|
||||
bin.remove(&element)?;
|
||||
}
|
||||
|
||||
let mut client_data = ClientPipelineData {
|
||||
start_time: self.shared.microseconds.load(Ordering::Relaxed),
|
||||
clock,
|
||||
..Default::default()
|
||||
};
|
||||
let mut client_data = NeoMediaSender::new(
|
||||
self.shared.get_buffer_size(),
|
||||
self.shared.get_use_smoothing(),
|
||||
);
|
||||
|
||||
// Now contruct the actual ones
|
||||
match *self.shared.vid_format.blocking_read() {
|
||||
VidFormats::H265 => {
|
||||
debug!("Building H265 Pipeline");
|
||||
let source = make_element("appsrc", "vidsrc")?
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
|
||||
source.set_base_time(ClockTime::from_mseconds(
|
||||
self.shared.microseconds.load(Ordering::Relaxed),
|
||||
));
|
||||
source.set_is_live(true);
|
||||
source.set_block(false);
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(52428800);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
|
||||
let enough_data_start = client_data.enough_data.clone();
|
||||
let enough_data_stop = client_data.enough_data.clone();
|
||||
source.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.need_data(move |_appsrc, _amt| {
|
||||
if enough_data_stop.load(Ordering::Relaxed) {
|
||||
log::debug!("Client is in need of data");
|
||||
}
|
||||
enough_data_stop.store(false, Ordering::Relaxed);
|
||||
})
|
||||
.enough_data(move |_appsrc| {
|
||||
if !enough_data_start.load(Ordering::Relaxed) {
|
||||
log::debug!("Client is full of of data");
|
||||
}
|
||||
enough_data_start.store(true, Ordering::Relaxed);
|
||||
})
|
||||
.build(),
|
||||
);
|
||||
// source.set_caps(Some(
|
||||
// &Caps::builder("video/x-h265").field("parsed", false).build(),
|
||||
// ));
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
let queue = make_element("queue", "source_queue")?;
|
||||
queue.set_property_from_str("leaky", "downstream");
|
||||
queue.set_property("max-size-bytes", 104857600u32);
|
||||
let parser = make_element("h265parse", "parser")?;
|
||||
let payload = make_element("rtph265pay", "pay0")?;
|
||||
bin.add_many(&[&source, &queue, &parser, &payload])?;
|
||||
Element::link_many(&[&source, &queue, &parser, &payload])?;
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
client_data.vidsrc.replace(source);
|
||||
}
|
||||
VidFormats::H264 => {
|
||||
debug!("Building H264 Pipeline");
|
||||
let source = make_element("appsrc", "vidsrc")?
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
|
||||
source.set_base_time(ClockTime::from_mseconds(
|
||||
self.shared.microseconds.load(Ordering::Relaxed),
|
||||
));
|
||||
source.set_is_live(true);
|
||||
source.set_block(false);
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(52428800);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
|
||||
let enough_data_start = client_data.enough_data.clone();
|
||||
let enough_data_stop = client_data.enough_data.clone();
|
||||
source.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.need_data(move |_appsrc, _amt| {
|
||||
if enough_data_stop.load(Ordering::Relaxed) {
|
||||
log::debug!("Client is in need of data");
|
||||
}
|
||||
enough_data_stop.store(false, Ordering::Relaxed);
|
||||
})
|
||||
.enough_data(move |_appsrc| {
|
||||
if !enough_data_start.load(Ordering::Relaxed) {
|
||||
log::debug!("Client is full of of data");
|
||||
}
|
||||
enough_data_start.store(true, Ordering::Relaxed);
|
||||
})
|
||||
.build(),
|
||||
);
|
||||
// source.set_caps(Some(
|
||||
// &Caps::builder("video/x-h264").field("parsed", false).build(),
|
||||
// ));
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
let queue = make_element("queue", "source_queue")?;
|
||||
queue.set_property_from_str("leaky", "downstream");
|
||||
|
||||
let parser = make_element("h264parse", "parser")?;
|
||||
let payload = make_element("rtph264pay", "pay0")?;
|
||||
bin.add_many(&[&source, &queue, &parser, &payload])?;
|
||||
Element::link_many(&[&source, &queue, &parser, &payload])?;
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
client_data.vidsrc.replace(source);
|
||||
}
|
||||
VidFormats::Unknown => {
|
||||
match (
|
||||
*self.shared.vid_format.blocking_read(),
|
||||
self.shared.buffer_ready.load(Ordering::Relaxed),
|
||||
) {
|
||||
(VidFormats::Unknown, true) | (_, false) => {
|
||||
debug!("Building Unknown Pipeline");
|
||||
let source = make_element("videotestsrc", "vidsrc")?;
|
||||
let source = make_element("videotestsrc", "testvidsrc")?;
|
||||
source.set_property_from_str("pattern", "snow");
|
||||
let queue = make_element("queue", "queue0")?;
|
||||
queue.set_property_from_str("leaky", "downstream");
|
||||
queue.set_property("max-size-bytes", 104857600u32);
|
||||
source.set_property("num-buffers", 500i32); // Send buffers then EOS
|
||||
let queue = make_queue("queue0")?;
|
||||
let queue2 = make_queue2("queue2")?;
|
||||
let overlay = make_element("textoverlay", "overlay")?;
|
||||
overlay.set_property("text", "Stream not Ready");
|
||||
overlay.set_property_from_str("valignment", "top");
|
||||
@@ -355,7 +295,7 @@ impl NeoMediaFactoryImpl {
|
||||
let encoder = make_element("jpegenc", "encoder")?;
|
||||
let payload = make_element("rtpjpegpay", "pay0")?;
|
||||
|
||||
bin.add_many(&[&source, &queue, &overlay, &encoder, &payload])?;
|
||||
bin.add_many(&[&source, &queue, &queue2, &overlay, &encoder, &payload])?;
|
||||
source.link_filtered(
|
||||
&queue,
|
||||
&Caps::builder("video/x-raw")
|
||||
@@ -366,42 +306,198 @@ impl NeoMediaFactoryImpl {
|
||||
.build(),
|
||||
)?;
|
||||
// source.link(&queue)?;
|
||||
queue.link(&overlay)?;
|
||||
overlay.link(&encoder)?;
|
||||
encoder.link(&payload)?;
|
||||
Element::link_many(&[&queue, &queue2, &overlay, &encoder, &payload])?;
|
||||
}
|
||||
}
|
||||
|
||||
match *self.shared.aud_format.blocking_read() {
|
||||
AudFormats::Unknown => {}
|
||||
AudFormats::Aac => {
|
||||
debug!("Building Aac pipeline");
|
||||
let source = make_element("appsrc", "audsrc")?
|
||||
(VidFormats::H265, true) => {
|
||||
debug!("Building H265 Pipeline");
|
||||
let source = make_element("appsrc", "vidsrc")?
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
|
||||
source.set_base_time(ClockTime::from_mseconds(
|
||||
self.shared.microseconds.load(Ordering::Relaxed),
|
||||
));
|
||||
source.set_is_live(true);
|
||||
source.set_block(false);
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(52428800);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
source.set_stream_type(AppStreamType::Seekable);
|
||||
|
||||
let need_command = client_data.get_commader();
|
||||
let enough_command = client_data.get_commader();
|
||||
let seek_command = client_data.get_commader();
|
||||
source.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.need_data(move |_appsrc, _amt| {
|
||||
let _ = need_command.blocking_send(NeoMediaSenderCommand::Resume);
|
||||
})
|
||||
.enough_data(move |_appsrc| {
|
||||
let _ = enough_command.blocking_send(NeoMediaSenderCommand::Pause);
|
||||
})
|
||||
.seek_data(move |appsrc, seek_pos| {
|
||||
debug!("Send seek H265");
|
||||
let runtime = appsrc.clock().and_then(|clock| {
|
||||
clock.time().and_then(|time| {
|
||||
appsrc.base_time().map(|base_time| {
|
||||
time.saturating_sub(base_time).useconds() as i64
|
||||
})
|
||||
})
|
||||
});
|
||||
let res = seek_command
|
||||
.blocking_send(NeoMediaSenderCommand::Seek(runtime, seek_pos))
|
||||
.is_ok();
|
||||
debug!("Sent seek H265: {}", res);
|
||||
res
|
||||
})
|
||||
.build(),
|
||||
);
|
||||
// source.set_caps(Some(
|
||||
// &Caps::builder("video/x-h265").field("parsed", false).build(),
|
||||
// ));
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
let queue = make_queue("source_queue")?;
|
||||
let queue2 = make_queue2("queue2")?;
|
||||
let parser = make_element("h265parse", "parser")?;
|
||||
// parser.set_property("config-interval", 5i32);
|
||||
let payload = make_element("rtph265pay", "pay0")?;
|
||||
bin.add_many(&[&source, &queue, &queue2, &parser, &payload])?;
|
||||
Element::link_many(&[&source, &queue, &queue2, &parser, &payload])?;
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
client_data.update_vid(source);
|
||||
}
|
||||
(VidFormats::H264, true) => {
|
||||
debug!("Building H264 Pipeline");
|
||||
let source = make_element("appsrc", "vidsrc")?
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
|
||||
|
||||
source.set_is_live(true);
|
||||
source.set_block(false);
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(50000000u64); // 50MB
|
||||
// source.set_property("max-buffers", 0u32); // v1.20
|
||||
// source.set_property("max-time", Duration::from_secs_f32(2.25).as_nanos() as u64);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Seekable);
|
||||
|
||||
let need_command = client_data.get_commader();
|
||||
let enough_command = client_data.get_commader();
|
||||
let seek_command = client_data.get_commader();
|
||||
source.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.need_data(move |_appsrc, _amt| {
|
||||
let _ = need_command.blocking_send(NeoMediaSenderCommand::Resume);
|
||||
})
|
||||
.enough_data(move |_appsrc| {
|
||||
let _ = enough_command.blocking_send(NeoMediaSenderCommand::Pause);
|
||||
})
|
||||
.seek_data(move |appsrc, seek_pos| {
|
||||
debug!("Send seek H264: {}", seek_pos);
|
||||
let runtime = appsrc.clock().and_then(|clock| {
|
||||
clock.time().and_then(|time| {
|
||||
appsrc.base_time().map(|base_time| {
|
||||
time.saturating_sub(base_time).useconds() as i64
|
||||
})
|
||||
})
|
||||
});
|
||||
let result = seek_command
|
||||
.blocking_send(NeoMediaSenderCommand::Seek(runtime, seek_pos))
|
||||
.is_ok();
|
||||
debug!(" - Sent seek H264: {}", result);
|
||||
result
|
||||
})
|
||||
.build(),
|
||||
);
|
||||
// source.set_caps(Some(
|
||||
// &Caps::builder("video/x-h264").field("parsed", false).build(),
|
||||
// ));
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
let queue = make_queue("source_queue")?;
|
||||
let queue2 = make_queue2("queue2")?;
|
||||
let parser = make_element("h264parse", "parser")?;
|
||||
// parser.set_property("update-timecode", true);
|
||||
let payload = make_element("rtph264pay", "pay0")?;
|
||||
// payload.set_property("config-interval", 1i32);
|
||||
// let storage = make_element("rtpstorage", "vidstorage")?;
|
||||
// storage.set_property("size-time", Duration::from_secs_f32(2.25).as_nanos() as u64);
|
||||
// let jitter = make_element("rtpjitterbuffer", "pay0")?;
|
||||
// jitter.set_property("latency", Duration::from_secs_f32(2.0).as_nanos() as u32);
|
||||
|
||||
// payload.set_property("config-interval", 5i32);
|
||||
bin.add_many(&[
|
||||
&source, &queue, &queue2, &parser,
|
||||
&payload,
|
||||
// &storage,
|
||||
// &jitter,
|
||||
])?;
|
||||
Element::link_many(&[
|
||||
&source, &queue, &queue2, &parser,
|
||||
&payload,
|
||||
// &storage,
|
||||
// &jitter,
|
||||
])?;
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
client_data.update_vid(source);
|
||||
}
|
||||
}
|
||||
|
||||
// let do_aud = false;
|
||||
// if do_aud {
|
||||
match (
|
||||
*self.shared.aud_format.blocking_read(),
|
||||
self.shared.buffer_ready.load(Ordering::Relaxed),
|
||||
) {
|
||||
(AudFormats::Unknown, true) | (_, false) => {}
|
||||
(AudFormats::Aac, true) => {
|
||||
debug!("Building Aac pipeline");
|
||||
let source = make_element("appsrc", "audsrc")?
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
|
||||
|
||||
source.set_is_live(true);
|
||||
source.set_block(false);
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(52428800);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Seekable);
|
||||
|
||||
let need_command = client_data.get_commader();
|
||||
let enough_command = client_data.get_commader();
|
||||
// let seek_command = client_data.get_commader();
|
||||
source.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.need_data(move |_appsrc, _amt| {
|
||||
let _ = need_command.blocking_send(NeoMediaSenderCommand::Resume);
|
||||
})
|
||||
.enough_data(move |_appsrc| {
|
||||
let _ = enough_command.blocking_send(NeoMediaSenderCommand::Pause);
|
||||
})
|
||||
.seek_data(move |_appsrc, _seek_pos| true)
|
||||
.build(),
|
||||
);
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
|
||||
let queue = make_element("queue", "audqueue")?;
|
||||
queue.set_property_from_str("leaky", "downstream");
|
||||
queue.set_property("max-size-bytes", 104857600u32);
|
||||
let queue = make_queue("audqueue")?;
|
||||
let queue2 = make_queue2("audqueue2")?;
|
||||
let parser = make_element("aacparse", "audparser")?;
|
||||
let decoder = make_element("decodebin", "auddecoder")?;
|
||||
let encoder = make_element("audioconvert", "audencoder")?;
|
||||
let payload = make_element("rtpL16pay", "pay1")?;
|
||||
|
||||
bin.add_many(&[&source, &queue, &parser, &decoder, &encoder, &payload])?;
|
||||
Element::link_many(&[&source, &queue, &parser, &decoder])?;
|
||||
bin.add_many(&[
|
||||
&source, &queue, &queue2, &parser, &decoder, &encoder, &payload,
|
||||
])?;
|
||||
Element::link_many(&[&source, &queue, &queue2, &parser, &decoder])?;
|
||||
Element::link_many(&[&encoder, &payload])?;
|
||||
decoder.connect_pad_added(move |_element, pad| {
|
||||
debug!("Linking encoder to decoder: {:?}", pad.caps());
|
||||
@@ -415,9 +511,9 @@ impl NeoMediaFactoryImpl {
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
client_data.audsrc.replace(source);
|
||||
client_data.update_aud(source);
|
||||
}
|
||||
AudFormats::Adpcm(block_size) => {
|
||||
(AudFormats::Adpcm(block_size), true) => {
|
||||
debug!("Building Adpcm pipeline");
|
||||
// Original command line
|
||||
// caps=audio/x-adpcm,layout=dvi,block_align={},channels=1,rate=8000
|
||||
@@ -429,15 +525,12 @@ impl NeoMediaFactoryImpl {
|
||||
let source = make_element("appsrc", "audsrc")?
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
|
||||
source.set_base_time(ClockTime::from_mseconds(
|
||||
self.shared.microseconds.load(Ordering::Relaxed),
|
||||
));
|
||||
source.set_is_live(true);
|
||||
source.set_block(false);
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(52428800);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
source.set_stream_type(AppStreamType::Seekable);
|
||||
source.set_caps(Some(
|
||||
&Caps::builder("audio/x-adpcm")
|
||||
.field("layout", "div")
|
||||
@@ -446,19 +539,34 @@ impl NeoMediaFactoryImpl {
|
||||
.field("rate", 8000i32)
|
||||
.build(),
|
||||
));
|
||||
|
||||
let need_command = client_data.get_commader();
|
||||
let enough_command = client_data.get_commader();
|
||||
// let seek_command = client_data.get_commader();
|
||||
source.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.need_data(move |_appsrc, _amt| {
|
||||
let _ = need_command.blocking_send(NeoMediaSenderCommand::Resume);
|
||||
})
|
||||
.enough_data(move |_appsrc| {
|
||||
let _ = enough_command.blocking_send(NeoMediaSenderCommand::Pause);
|
||||
})
|
||||
.seek_data(move |_appsrc, _seek_pos| true)
|
||||
.build(),
|
||||
);
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
|
||||
let queue = make_element("queue", "audqueue")?;
|
||||
queue.set_property_from_str("leaky", "downstream");
|
||||
queue.set_property("max-size-bytes", 104857600u32);
|
||||
let queue = make_queue("audqueue")?;
|
||||
let queue2 = make_queue2("audqueue2")?;
|
||||
let decoder = make_element("decodebin", "auddecoder")?;
|
||||
let encoder = make_element("audioconvert", "audencoder")?;
|
||||
let payload = make_element("rtpL16pay", "pay1")?;
|
||||
|
||||
bin.add_many(&[&source, &queue, &decoder, &encoder, &payload])?;
|
||||
Element::link_many(&[&source, &queue, &decoder])?;
|
||||
bin.add_many(&[&source, &queue, &queue2, &decoder, &encoder, &payload])?;
|
||||
Element::link_many(&[&source, &queue, &queue2, &decoder])?;
|
||||
Element::link_many(&[&encoder, &payload])?;
|
||||
decoder.connect_pad_added(move |_element, pad| {
|
||||
debug!("Linking encoder to decoder: {:?}", pad.caps());
|
||||
@@ -472,7 +580,7 @@ impl NeoMediaFactoryImpl {
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
client_data.audsrc.replace(source);
|
||||
client_data.update_aud(source);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,4 +589,12 @@ impl NeoMediaFactoryImpl {
|
||||
bin.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))
|
||||
}
|
||||
|
||||
async fn join(&self) -> AnyResult<()> {
|
||||
let mut threads = self.threads.write().await;
|
||||
while let Some(thread) = threads.join_next().await {
|
||||
thread??;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
+929
-334
File diff suppressed because it is too large.
Load diff
+207
-17
@@ -13,10 +13,9 @@ use gstreamer_rtsp_server::{
|
||||
gio::{TlsAuthenticationMode, TlsCertificate},
|
||||
prelude::*,
|
||||
subclass::prelude::*,
|
||||
RTSPAuth, RTSPServer, RTSPToken, RTSP_TOKEN_MEDIA_FACTORY_ROLE,
|
||||
RTSPAuth, RTSPFilterResult, RTSPServer, RTSPToken, RTSP_TOKEN_MEDIA_FACTORY_ROLE,
|
||||
};
|
||||
use log::*;
|
||||
use neolink_core::bcmedia::model::*;
|
||||
use std::{
|
||||
collections::{hash_map::Entry, HashMap, HashSet},
|
||||
fs,
|
||||
@@ -24,7 +23,8 @@ use std::{
|
||||
};
|
||||
use tokio::{
|
||||
sync::{mpsc::Sender, RwLock},
|
||||
task::JoinHandle,
|
||||
task::JoinSet,
|
||||
time::{timeout, Duration},
|
||||
};
|
||||
|
||||
glib::wrapper! {
|
||||
@@ -45,12 +45,19 @@ impl NeoRtspServer {
|
||||
Ok(factory)
|
||||
}
|
||||
|
||||
pub(crate) async fn get_sender<T: Into<String>>(&self, tag: T) -> Option<Sender<BcMedia>> {
|
||||
pub(crate) async fn get_sender<T: Into<String>>(
|
||||
&self,
|
||||
tag: T,
|
||||
) -> Option<Sender<FactoryCommand>> {
|
||||
self.imp().get_sender(tag).await
|
||||
}
|
||||
|
||||
pub(crate) async fn create_stream<U: Into<String>>(&self, tag: U) -> AnyResult<()> {
|
||||
self.imp().create_stream(tag).await
|
||||
pub(crate) async fn create_stream<U: Into<String>>(
|
||||
&self,
|
||||
tag: U,
|
||||
config: &CameraConfig,
|
||||
) -> AnyResult<()> {
|
||||
self.imp().create_stream(tag, config).await
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
@@ -83,11 +90,7 @@ impl NeoRtspServer {
|
||||
self.imp().add_permitted_roles(tag, permitted_users).await
|
||||
}
|
||||
|
||||
pub(crate) async fn run(
|
||||
&self,
|
||||
bind_addr: &str,
|
||||
bind_port: u16,
|
||||
) -> AnyResult<(JoinHandle<AnyResult<()>>, Arc<MainLoop>)> {
|
||||
pub(crate) async fn run(&self, bind_addr: &str, bind_port: u16) -> AnyResult<()> {
|
||||
let server = self;
|
||||
server.set_address(bind_addr);
|
||||
server.set_service(&format!("{}", bind_port));
|
||||
@@ -96,10 +99,34 @@ impl NeoRtspServer {
|
||||
let main_loop = Arc::new(MainLoop::new(None, false));
|
||||
// Run the Glib main loop.
|
||||
let main_loop_thread = main_loop.clone();
|
||||
#[allow(clippy::unit_arg)]
|
||||
let handle = tokio::task::spawn_blocking(move || Ok(main_loop_thread.run()));
|
||||
let handle = tokio::task::spawn_blocking(move || {
|
||||
main_loop_thread.run();
|
||||
AnyResult::Ok(())
|
||||
});
|
||||
timeout(Duration::from_secs(5), self.imp().threads.write())
|
||||
.await
|
||||
.with_context(|| "Timeout waiting to lock Server threads")?
|
||||
.spawn(async move { handle.await? });
|
||||
timeout(Duration::from_secs(5), self.imp().main_loop.write())
|
||||
.await
|
||||
.with_context(|| "Timeout waiting to lock Server main_loop")?
|
||||
.replace(main_loop);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Ok((handle, main_loop))
|
||||
pub(crate) async fn quit(&self) -> AnyResult<()> {
|
||||
if let Some(main_loop) = self.imp().main_loop.read().await.as_ref() {
|
||||
main_loop.quit();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn join(&self) -> AnyResult<()> {
|
||||
let mut threads = self.imp().threads.write().await;
|
||||
while let Some(thread) = threads.join_next().await {
|
||||
thread??;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_up_tls(&self, config: &Config) {
|
||||
@@ -150,6 +177,155 @@ impl NeoRtspServer {
|
||||
pub(crate) async fn buffer_ready<T: Into<String>>(&self, tag: T) -> Option<bool> {
|
||||
self.imp().buffer_ready(tag).await
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
/// Clear all sessions on a given path
|
||||
pub(crate) async fn clear_session<T: Into<String>>(&self, tag: T) {
|
||||
let tag: String = tag.into();
|
||||
if let Some(media) = self.imp().medias.read().await.get(&tag) {
|
||||
self.clear_session_paths(media.paths.iter());
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_session_paths<T: AsRef<str>>(&self, paths: impl Iterator<Item = T>) {
|
||||
let paths: Vec<_> = paths.collect();
|
||||
self.client_filter(Some(&mut |_server, client| {
|
||||
let mut close_client = false;
|
||||
client.session_filter(Some(&mut |_client, session| {
|
||||
let mut clean_up = false;
|
||||
session.filter(Some(&mut |_session, session_media| {
|
||||
if paths.iter().any(|p| {
|
||||
let s = p.as_ref();
|
||||
session_media
|
||||
.matches(s)
|
||||
.map(|amt| amt == s.len() as i32)
|
||||
.unwrap_or(false)
|
||||
}) {
|
||||
info!("Removing Session Media");
|
||||
clean_up = true;
|
||||
RTSPFilterResult::Remove
|
||||
} else {
|
||||
RTSPFilterResult::Keep
|
||||
}
|
||||
}));
|
||||
|
||||
if clean_up {
|
||||
close_client = true;
|
||||
RTSPFilterResult::Remove
|
||||
} else {
|
||||
RTSPFilterResult::Keep
|
||||
}
|
||||
}));
|
||||
if close_client {
|
||||
client.close();
|
||||
RTSPFilterResult::Remove
|
||||
} else {
|
||||
RTSPFilterResult::Keep
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
/// Clear all sessions on the rtsp server that are using the not ready stream
|
||||
pub(crate) async fn clear_session_notready<T: Into<String>>(&self, tag: T) {
|
||||
let tag: String = tag.into();
|
||||
if let Some(media) = self.imp().medias.read().await.get(&tag) {
|
||||
self.clear_session_paths_notready(media.paths.iter());
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_session_paths_notready<T: AsRef<str>>(&self, paths: impl Iterator<Item = T>) {
|
||||
let paths: Vec<_> = paths.collect();
|
||||
self.client_filter(Some(&mut |_server, client| {
|
||||
let mut close_client = false;
|
||||
client.session_filter(Some(&mut |_client, session| {
|
||||
let mut clean_up = false;
|
||||
session.filter(Some(&mut |_session, session_media| {
|
||||
if paths.iter().any(|p| {
|
||||
let s = p.as_ref();
|
||||
session_media
|
||||
.matches(s)
|
||||
.map(|amt| amt == s.len() as i32)
|
||||
.unwrap_or(false)
|
||||
}) {
|
||||
// Path is correct now check for no vidsrc
|
||||
let element = session_media
|
||||
.media()
|
||||
.expect("Media should exist")
|
||||
.element()
|
||||
.dynamic_cast::<gstreamer::Bin>()
|
||||
.expect("Media element should be a bin");
|
||||
// debug!("Searching for testvidsrc on {:?}", element.name());
|
||||
// for child in element.children().iter() {
|
||||
// debug!(" - Child: {:?}", child.name());
|
||||
// }
|
||||
if element.child_by_name("testvidsrc").is_some() {
|
||||
info!("Removing Session Media");
|
||||
clean_up = true;
|
||||
RTSPFilterResult::Remove
|
||||
} else {
|
||||
RTSPFilterResult::Keep
|
||||
}
|
||||
} else {
|
||||
RTSPFilterResult::Keep
|
||||
}
|
||||
}));
|
||||
|
||||
if clean_up {
|
||||
close_client = true;
|
||||
RTSPFilterResult::Remove
|
||||
} else {
|
||||
RTSPFilterResult::Keep
|
||||
}
|
||||
}));
|
||||
if close_client {
|
||||
client.close();
|
||||
RTSPFilterResult::Remove
|
||||
} else {
|
||||
RTSPFilterResult::Keep
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
// Clear buffers on all senders of a tag
|
||||
#[allow(dead_code)] // Old function, was used to clear buffer on reconnect
|
||||
pub(crate) async fn clear_buffer<T: Into<String>>(&self, tag: T) -> AnyResult<()> {
|
||||
if let Some(sender) = self.imp().get_sender(tag).await {
|
||||
sender.send(FactoryCommand::ClearBuffer).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("No such tag"))
|
||||
}
|
||||
}
|
||||
|
||||
// Jump to live on all senders of a tag
|
||||
pub(crate) async fn jump_to_live<T: Into<String>>(&self, tag: T) -> AnyResult<()> {
|
||||
if let Some(sender) = self.imp().get_sender(tag).await {
|
||||
sender.send(FactoryCommand::JumpToLive).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("No such tag"))
|
||||
}
|
||||
}
|
||||
|
||||
// Pause on all senders of a tag
|
||||
pub(crate) async fn pause<T: Into<String>>(&self, tag: T) -> AnyResult<()> {
|
||||
if let Some(sender) = self.imp().get_sender(tag).await {
|
||||
sender.send(FactoryCommand::Pause).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("No such tag"))
|
||||
}
|
||||
}
|
||||
|
||||
// Resume on all senders of a tag
|
||||
pub(crate) async fn resume<T: Into<String>>(&self, tag: T) -> AnyResult<()> {
|
||||
if let Some(sender) = self.imp().get_sender(tag).await {
|
||||
sender.send(FactoryCommand::Resume).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("No such tag"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for NeoRtspServer {}
|
||||
@@ -163,6 +339,8 @@ struct FactoryData {
|
||||
#[derive(Default)]
|
||||
pub(crate) struct NeoRtspServerImpl {
|
||||
medias: RwLock<HashMap<String, FactoryData>>,
|
||||
threads: RwLock<JoinSet<AnyResult<()>>>,
|
||||
main_loop: RwLock<Option<Arc<MainLoop>>>,
|
||||
}
|
||||
|
||||
impl ObjectImpl for NeoRtspServerImpl {}
|
||||
@@ -176,12 +354,21 @@ impl ObjectSubclass for NeoRtspServerImpl {
|
||||
}
|
||||
|
||||
impl NeoRtspServerImpl {
|
||||
pub(crate) async fn create_stream<U: Into<String>>(&self, tag: U) -> AnyResult<()> {
|
||||
pub(crate) async fn create_stream<U: Into<String>>(
|
||||
&self,
|
||||
tag: U,
|
||||
config: &CameraConfig,
|
||||
) -> AnyResult<()> {
|
||||
let key = tag.into();
|
||||
match self.medias.write().await.entry(key.clone()) {
|
||||
Entry::Occupied(_occ) => {}
|
||||
Entry::Vacant(vac) => {
|
||||
let media = NeoMediaFactory::new();
|
||||
let media = NeoMediaFactory::new(config.buffer_size, config.use_smoothing);
|
||||
let thread_media = media.clone();
|
||||
self.threads
|
||||
.write()
|
||||
.await
|
||||
.spawn(async move { thread_media.join().await });
|
||||
vac.insert(FactoryData {
|
||||
factory: media,
|
||||
paths: Default::default(),
|
||||
@@ -191,7 +378,10 @@ impl NeoRtspServerImpl {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn get_sender<T: Into<String>>(&self, tag: T) -> Option<Sender<BcMedia>> {
|
||||
pub(crate) async fn get_sender<T: Into<String>>(
|
||||
&self,
|
||||
tag: T,
|
||||
) -> Option<Sender<FactoryCommand>> {
|
||||
let key = tag.into();
|
||||
self.medias
|
||||
.read()
|
||||
|
||||
+21
-14
@@ -1,10 +1,7 @@
|
||||
//! Data shared between the various
|
||||
//! components that manage a media stream
|
||||
use gstreamer::Clock;
|
||||
use gstreamer_app::AppSrc;
|
||||
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
@@ -24,9 +21,10 @@ pub(super) enum AudFormats {
|
||||
pub(super) struct NeoMediaShared {
|
||||
pub(super) vid_format: RwLock<VidFormats>,
|
||||
pub(super) aud_format: RwLock<AudFormats>,
|
||||
pub(super) microseconds: AtomicU64,
|
||||
pub(super) number_of_clients: AtomicUsize,
|
||||
pub(super) buffer_ready: AtomicBool,
|
||||
pub(super) buffer_size: AtomicUsize,
|
||||
pub(super) use_smoothing: AtomicBool,
|
||||
}
|
||||
|
||||
impl Default for NeoMediaShared {
|
||||
@@ -34,19 +32,28 @@ impl Default for NeoMediaShared {
|
||||
Self {
|
||||
vid_format: RwLock::new(VidFormats::Unknown),
|
||||
aud_format: RwLock::new(AudFormats::Unknown),
|
||||
microseconds: AtomicU64::new(0),
|
||||
number_of_clients: AtomicUsize::new(0),
|
||||
buffer_ready: AtomicBool::new(false),
|
||||
buffer_size: AtomicUsize::new(100),
|
||||
use_smoothing: AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub(super) struct ClientPipelineData {
|
||||
pub(super) vidsrc: Option<AppSrc>,
|
||||
pub(super) audsrc: Option<AppSrc>,
|
||||
pub(super) start_time: u64,
|
||||
pub(super) inited: bool,
|
||||
pub(super) clock: Option<Clock>,
|
||||
pub(super) enough_data: Arc<AtomicBool>,
|
||||
impl NeoMediaShared {
|
||||
pub(super) fn get_buffer_size(&self) -> usize {
|
||||
self.buffer_size.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub(super) fn set_buffer_size(&self, new_size: usize) {
|
||||
self.buffer_size.store(new_size, Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub(super) fn get_use_smoothing(&self) -> bool {
|
||||
self.use_smoothing.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub(super) fn set_use_smoothing(&self, new_value: bool) {
|
||||
self.use_smoothing.store(new_value, Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
+96
-36
@@ -61,11 +61,13 @@ use tokio_stream::StreamExt;
|
||||
|
||||
mod cmdline;
|
||||
mod gst;
|
||||
mod spring;
|
||||
mod states;
|
||||
|
||||
use super::config::Config;
|
||||
pub(crate) use cmdline::Opt;
|
||||
use gst::NeoRtspServer;
|
||||
pub(crate) use spring::*;
|
||||
use states::*;
|
||||
|
||||
/// Entry point for the rtsp subcommand
|
||||
@@ -97,6 +99,7 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
|
||||
let name = camera.get_name();
|
||||
let mut backoff = Duration::from_micros(125);
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
let failure = camera_main(camera).await;
|
||||
match failure {
|
||||
Err(CameraFailureKind::Fatal(e)) => {
|
||||
@@ -130,14 +133,15 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
|
||||
|
||||
let bind_addr = config.bind_addr.clone();
|
||||
let bind_port = config.bind_port;
|
||||
let (handle, main_loop) = rtsp.run(&bind_addr, bind_port).await?;
|
||||
set.spawn(async move { handle.await? });
|
||||
rtsp.run(&bind_addr, bind_port).await?;
|
||||
let thread_rtsp = rtsp.clone();
|
||||
set.spawn(async move { thread_rtsp.join().await });
|
||||
|
||||
while let Some(joined) = set.join_next().await {
|
||||
match &joined {
|
||||
Err(_) | Ok(Err(_)) => {
|
||||
// Panicked or error in task
|
||||
main_loop.quit();
|
||||
rtsp.quit().await?;
|
||||
}
|
||||
Ok(Ok(_)) => {
|
||||
// All good
|
||||
@@ -177,43 +181,66 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
|
||||
|
||||
let _ = loggedin.manage().await;
|
||||
|
||||
let tags = loggedin.shared.get_tags();
|
||||
let rtsp_thread = loggedin.get_rtsp();
|
||||
|
||||
// Clear all buffers present
|
||||
// Uncomment to clear buffers. This is now handlled in the buffer itself,
|
||||
// instead of clearing it restamps it whenever there is a jump in the
|
||||
// timestamps of >1s
|
||||
//
|
||||
// tags.iter()
|
||||
// .map(|tag| rtsp_thread.clear_buffer(tag))
|
||||
// .collect::<FuturesUnordered<_>>()
|
||||
// .collect::<Vec<_>>()
|
||||
// .await;
|
||||
|
||||
// Start pulling data from the camera
|
||||
let mut streaming = loggedin
|
||||
.stream()
|
||||
.await
|
||||
.with_context(|| format!("{}: Could not start stream", name))
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
|
||||
let tags = streaming.shared.get_tags();
|
||||
let rtsp_thread = streaming.get_rtsp();
|
||||
|
||||
let mut waiter = tokio::time::interval(Duration::from_micros(500));
|
||||
loop {
|
||||
waiter.tick().await;
|
||||
if tags
|
||||
.iter()
|
||||
.map(|tag| rtsp_thread.buffer_ready(tag))
|
||||
.collect::<FuturesUnordered<_>>()
|
||||
.all(|f| f.unwrap_or(false))
|
||||
.await
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if streaming.get_config().pause.on_motion || streaming.get_config().pause.on_disconnect {
|
||||
log::info!("Pause buffer prepared");
|
||||
// Wait for buffers to be prepared
|
||||
tokio::select! {
|
||||
v = async {
|
||||
let mut waiter = tokio::time::interval(Duration::from_micros(500));
|
||||
loop {
|
||||
waiter.tick().await;
|
||||
if tags
|
||||
.iter()
|
||||
.map(|tag| rtsp_thread.buffer_ready(tag))
|
||||
.collect::<FuturesUnordered<_>>()
|
||||
.all(|f| f.unwrap_or(false))
|
||||
.await
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
} => v,
|
||||
// Or for stream to error
|
||||
v = streaming.join() => {v},
|
||||
}
|
||||
.with_context(|| format!("{}: Error while waiting for buffers", name))
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
|
||||
// tokio::task::spawn(async move {
|
||||
// let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
|
||||
// loop {
|
||||
// inter.tick().await;
|
||||
// log::info!(
|
||||
// "Users: {:?}, {}",
|
||||
// rtsp_thread.get_number_of_clients("Cammy01::Main").await,
|
||||
// rtsp_thread.num_path_users(&["/Cammy01/mainStream"])
|
||||
// );
|
||||
// }
|
||||
// });
|
||||
tags.iter()
|
||||
.map(|tag| rtsp_thread.jump_to_live(tag))
|
||||
.collect::<FuturesUnordered<_>>()
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
|
||||
// Clear "stream not ready" media to try and force a reconnect
|
||||
// This shoud stop them from watching the "Stream Not Ready" thing
|
||||
debug!("Clearing not ready clients");
|
||||
tags.iter()
|
||||
.map(|tag| rtsp_thread.clear_session_notready(tag))
|
||||
.collect::<FuturesUnordered<_>>()
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
log::info!("{}: Buffers prepared", name);
|
||||
|
||||
loop {
|
||||
// Wait for error or reason to pause
|
||||
@@ -237,7 +264,6 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
|
||||
// Wait for client to disconnect
|
||||
let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
|
||||
|
||||
info!("Await Client Pause");
|
||||
loop {
|
||||
inter.tick().await;
|
||||
let total_clients = tags.iter().map(|tag| rtsp_thread.get_number_of_clients(tag)).collect::<FuturesUnordered<_>>().fold(0usize, |acc, f| acc + f.unwrap_or(0usize)).await;
|
||||
@@ -253,6 +279,12 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
|
||||
}.with_context(|| format!("{}: Error while streaming", name))
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
|
||||
tags.iter()
|
||||
.map(|tag| rtsp_thread.pause(tag))
|
||||
.collect::<FuturesUnordered<_>>()
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
|
||||
let paused = streaming
|
||||
.stop()
|
||||
.await
|
||||
@@ -260,13 +292,29 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
// Wait for reason to restart
|
||||
tokio::select! {
|
||||
v = async {
|
||||
// Wait for motion start and client for start
|
||||
let mut motion = paused.get_camera().listen_on_motion().await?;
|
||||
let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
|
||||
loop {
|
||||
motion.await_start(Duration::ZERO).await?;
|
||||
let total_clients = tags.iter().map(|tag| rtsp_thread.get_number_of_clients(tag)).collect::<FuturesUnordered<_>>().fold(0usize, |acc, f| acc + f.unwrap_or(0usize)).await;
|
||||
if total_clients > 0 {
|
||||
return Result::<()>::Ok(());
|
||||
}
|
||||
inter.tick().await;
|
||||
}
|
||||
}, if paused.get_config().pause.on_motion && paused.get_config().pause.on_disconnect => {
|
||||
info!("Motion and Client Resume");
|
||||
v.with_context(|| "Error while processing motion/client messages")
|
||||
},
|
||||
v = async {
|
||||
// Wait for motion start
|
||||
let mut motion = paused.get_camera().listen_on_motion().await?;
|
||||
motion.await_start(Duration::ZERO).await
|
||||
}, if paused.get_config().pause.on_motion => {
|
||||
}, if paused.get_config().pause.on_motion && !paused.get_config().pause.on_disconnect => {
|
||||
info!("Motion Resume");
|
||||
v.map_err(|e| anyhow!("Error while processing motion messages: {:?}", e))
|
||||
v.with_context(|| "Error while processing motion messages")
|
||||
},
|
||||
v = async {
|
||||
// Wait for client to connect
|
||||
@@ -279,7 +327,7 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
|
||||
return Ok(())
|
||||
}
|
||||
}
|
||||
}, if paused.get_config().pause.on_disconnect => {
|
||||
}, if paused.get_config().pause.on_disconnect && !paused.get_config().pause.on_motion => {
|
||||
info!("Client Resume");
|
||||
v
|
||||
},
|
||||
@@ -293,10 +341,22 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
|
||||
.with_context(|| format!("{}: Error while paused", name))
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
|
||||
tags.iter()
|
||||
.map(|tag| rtsp_thread.jump_to_live(tag))
|
||||
.collect::<FuturesUnordered<_>>()
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
|
||||
streaming = paused
|
||||
.stream()
|
||||
.await
|
||||
.with_context(|| format!("{}: Could not start stream", name))
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
|
||||
tags.iter()
|
||||
.map(|tag| rtsp_thread.resume(tag))
|
||||
.collect::<FuturesUnordered<_>>()
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
//! A critically damped spring
|
||||
use tokio::time::Instant;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Spring {
|
||||
last_update: Instant,
|
||||
target: f64,
|
||||
value: f64,
|
||||
velocity: f64,
|
||||
halflife: f64,
|
||||
}
|
||||
|
||||
impl Spring {
|
||||
pub(crate) fn new(value: f64, target: f64, halflife: f64) -> Self {
|
||||
Self {
|
||||
value,
|
||||
target,
|
||||
halflife,
|
||||
last_update: Instant::now(),
|
||||
velocity: 0.0f64,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn update(&mut self) {
|
||||
let now = Instant::now();
|
||||
let dt = now - self.last_update;
|
||||
self.last_update = now;
|
||||
spring_update(
|
||||
&mut self.value,
|
||||
self.target,
|
||||
&mut self.velocity,
|
||||
self.halflife,
|
||||
dt.as_secs_f64(),
|
||||
);
|
||||
}
|
||||
|
||||
// pub(crate) fn value(&self) -> f64 {
|
||||
// self.value
|
||||
// }
|
||||
|
||||
// pub(crate) fn value_u32(&self) -> u32 {
|
||||
// self.value.max(std::u32::MIN as f64).floor().min(std::u32::MAX as f64) as u32
|
||||
// }
|
||||
pub(crate) fn value_i64(&self) -> i64 {
|
||||
self.value
|
||||
.max(std::i64::MIN as f64)
|
||||
.floor()
|
||||
.min(std::i64::MAX as f64) as i64
|
||||
}
|
||||
pub(crate) fn value_u64(&self) -> u64 {
|
||||
self.value
|
||||
.max(std::u64::MIN as f64)
|
||||
.floor()
|
||||
.min(std::u64::MAX as f64) as u64
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn set_target(&mut self, target: f64) {
|
||||
self.target = target;
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // Used in debug print
|
||||
pub(crate) fn get_target(&self) -> f64 {
|
||||
self.target
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn mod_value(&mut self, delta: f64) {
|
||||
self.target += delta;
|
||||
self.value += delta;
|
||||
}
|
||||
|
||||
pub(crate) fn reset_to(&mut self, target: f64) {
|
||||
self.target = target;
|
||||
self.value = target;
|
||||
self.velocity = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
const EPS: f64 = 1e-5;
|
||||
fn halflife_to_damping(halflife: f64) -> f64 {
|
||||
(4.0 * std::f64::consts::LN_2) / (halflife + EPS)
|
||||
}
|
||||
|
||||
// fn damping_to_halflife(damping: f32) -> f32 {
|
||||
// (4.0f32 * std::f32::consts::LN_2) / (damping + EPS)
|
||||
// }
|
||||
fn fast_negexp(x: f64) -> f64 {
|
||||
1.0 / (1.0 + x + 0.48 * x * x + 0.235 * x * x * x)
|
||||
}
|
||||
|
||||
fn spring_update(value: &mut f64, target: f64, velocity: &mut f64, halflife: f64, dt: f64) {
|
||||
if halflife.abs() < 1e-3 {
|
||||
*value = target;
|
||||
*velocity = 0.0;
|
||||
} else {
|
||||
let y = halflife_to_damping(halflife) / 2.0;
|
||||
let j0 = *value - target;
|
||||
let j1 = *velocity + j0 * y;
|
||||
let eydt = fast_negexp(y * dt);
|
||||
|
||||
*value = eydt * (j0 + j1 * dt) + target;
|
||||
*velocity = eydt * (*velocity - j1 * y * dt);
|
||||
}
|
||||
}
|
||||
@@ -36,4 +36,13 @@ impl Camera<Connected> {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) async fn join(&self) -> Result<()> {
|
||||
self.state
|
||||
.camera
|
||||
.join()
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("Camera join error: {:?}", e))
|
||||
}
|
||||
}
|
||||
@@ -32,22 +32,28 @@ impl Camera<LoggedIn> {
|
||||
|
||||
pub(crate) async fn manage(&self) -> Result<()> {
|
||||
let cam_time = self.state.camera.get_time().await?;
|
||||
let mut update = false;
|
||||
if let Some(time) = cam_time {
|
||||
info!(
|
||||
"{}: Camera time is already set: {}",
|
||||
self.shared.config.name, time
|
||||
);
|
||||
if self.shared.config.update_time {
|
||||
update = true;
|
||||
}
|
||||
} else {
|
||||
update = true;
|
||||
warn!(
|
||||
"{}: Camera has no time set, Updating",
|
||||
self.shared.config.name
|
||||
);
|
||||
}
|
||||
if update {
|
||||
use time::OffsetDateTime;
|
||||
// We'd like now_local() but it's deprecated - try to get the local time, but if no
|
||||
// time zone, fall back to UTC.
|
||||
let new_time =
|
||||
OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
|
||||
|
||||
warn!(
|
||||
"{}: Camera has no time set, setting to {}",
|
||||
self.shared.config.name, new_time
|
||||
);
|
||||
info!("{}: Setting time to {}", self.shared.config.name, new_time);
|
||||
match self.state.camera.set_time(new_time).await {
|
||||
Ok(_) => {
|
||||
let cam_time = self.state.camera.get_time().await?;
|
||||
@@ -90,4 +96,13 @@ impl Camera<LoggedIn> {
|
||||
pub(crate) fn get_camera(&self) -> &BcCamera {
|
||||
&self.state.camera
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) async fn join(&self) -> Result<()> {
|
||||
self.state
|
||||
.camera
|
||||
.join()
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("Camera join error: {:?}", e))
|
||||
}
|
||||
}
|
||||
@@ -85,7 +85,7 @@ impl Shared {
|
||||
pub(crate) async fn setup_streams(&self) -> Result<()> {
|
||||
for stream in self.streams.iter() {
|
||||
let tag = self.get_tag_for_stream(stream);
|
||||
self.rtsp.create_stream(&tag).await?;
|
||||
self.rtsp.create_stream(&tag, &self.config).await?;
|
||||
self.rtsp
|
||||
.add_permitted_roles(&tag, &self.permitted_users)
|
||||
.await?;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// Data is streamed into a gstreamer source
|
||||
|
||||
use anyhow::{anyhow, Error, Result};
|
||||
use anyhow::{anyhow, Context, Error, Result};
|
||||
use log::*;
|
||||
use neolink_core::bcmedia::model::BcMedia;
|
||||
use tokio::time::{timeout, Duration};
|
||||
@@ -11,6 +11,7 @@ use tokio::{sync::RwLock, task::JoinSet};
|
||||
use neolink_core::bc_protocol::{BcCamera, StreamKind as Stream};
|
||||
|
||||
use super::{camera::Camera, LoggedIn};
|
||||
use crate::rtsp::gst::FactoryCommand;
|
||||
|
||||
pub(crate) struct Streaming {
|
||||
pub(crate) camera: BcCamera,
|
||||
@@ -52,8 +53,11 @@ impl Camera<Streaming> {
|
||||
.await?;
|
||||
set.spawn(async move {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
// debug!("Straming: Get");
|
||||
let data = timeout(Duration::from_secs(15), stream_data.get_data()).await??;
|
||||
let data = timeout(Duration::from_secs(15), stream_data.get_data())
|
||||
.await
|
||||
.with_context(|| "Timed out waiting for new Media Frame")??;
|
||||
// debug!("Straming: Got");
|
||||
match &data {
|
||||
Ok(BcMedia::InfoV1(_)) => trace!("{}: - InfoV1", &tag_thread),
|
||||
@@ -65,7 +69,12 @@ impl Camera<Streaming> {
|
||||
Err(_) => trace!(" - Error"),
|
||||
}
|
||||
// debug!("Straming: Send");
|
||||
sender.send(data?).await?;
|
||||
timeout(
|
||||
Duration::from_secs(15),
|
||||
sender.send(FactoryCommand::BcMedia(data?)),
|
||||
)
|
||||
.await
|
||||
.with_context(|| "Timed out waiting to send Media Frame")??;
|
||||
// debug!("Straming: Sent");
|
||||
}
|
||||
});
|
||||
@@ -91,20 +100,26 @@ impl Camera<Streaming> {
|
||||
}
|
||||
|
||||
pub(crate) async fn join(&self) -> Result<()> {
|
||||
let mut locked_threads = self.state.set.write().await;
|
||||
while let Some(res) = locked_threads.join_next().await {
|
||||
match res {
|
||||
Err(e) => {
|
||||
locked_threads.abort_all();
|
||||
return Err(e.into());
|
||||
tokio::select! {
|
||||
v = async {
|
||||
let mut locked_threads = self.state.set.write().await;
|
||||
while let Some(res) = locked_threads.join_next().await {
|
||||
match res {
|
||||
Err(e) => {
|
||||
locked_threads.abort_all();
|
||||
return Err(e.into());
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
locked_threads.abort_all();
|
||||
return Err(e);
|
||||
}
|
||||
Ok(Ok(())) => {}
|
||||
}
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
locked_threads.abort_all();
|
||||
return Err(e);
|
||||
}
|
||||
Ok(Ok(())) => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
} => v,
|
||||
v = self.state.camera.join() => v.map_err(|e| anyhow!("Camera join error: {:?}", e)),
|
||||
}?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user