Compare commits

..
Author SHA1 Message Date
QuantumEntangledAndy 67508c1f17 Fix the test_enc3_extension test 2024-04-26 11:16:07 +07:00
QuantumEntangledAndy e85e8f4780 Make check value i32 2024-04-26 11:05:58 +07:00
QuantumEntangledAndy 175c33b131 Merge branch 'test/valgrind' into fix/argus3_disc 2024-04-26 11:05:21 +07:00
QuantumEntangledAndy 3bfd273528 Remove valgrind from docker 2024-04-21 18:01:45 +07:00
QuantumEntangledAndy bfa76009b9 One more print 2024-04-20 21:08:46 +07:00
QuantumEntangledAndy c59b44c83e Deeper discovery prints 2024-04-18 22:08:26 +07:00
QuantumEntangledAndy e146f9593b Reduce history buffer duration 2024-04-18 18:57:39 +07:00
QuantumEntangledAndy 5d05504ec8 Add a discovery udpxml test 2024-04-18 18:57:39 +07:00
QuantumEntangledAndy 88a2ec7a11 Fix the build yml 2024-04-18 18:56:41 +07:00
QuantumEntangledAndy 3b0a510ccf Debug print on discovery 2024-04-18 18:55:36 +07:00
QuantumEntangledAndy 2d49df07db Reduce history buffer duration 2024-04-18 18:50:20 +07:00
QuantumEntangledAndy 4d441d89fd Add a discovery udpxml test 2024-04-18 18:49:32 +07:00
QuantumEntangledAndy 31fb8f31db Add m1 macos 2024-04-18 18:48:42 +07:00
QuantumEntangledAndy 616e2e5223 Option time_r in test 2024-04-16 15:43:31 +07:00
QuantumEntangledAndy 08f917279c Make time_r optional in D2C_CFM 2024-04-16 15:23:17 +07:00
QuantumEntangledAndy df0f494702 In Extension::try_parse try to give use ful error message 2024-04-16 15:09:12 +07:00
QuantumEntangledAndy 4bfc73cce5 Merge branch 'test/valgrind' of github.com:QuantumEntangledAndy/neolink into test/valgrind 2024-04-16 10:15:02 +07:00
QuantumEntangledAndy a19a5f3919 Add a maximum length to history buffer too 2024-04-16 10:14:34 +07:00
QuantumEntangledAndy d0db861c10 Add a maximum length to history buffer too 2024-04-16 10:14:22 +07:00
QuantumEntangledAndy 9480dd8605 Increase timeout on rtsp session 2024-04-16 10:13:24 +07:00
QuantumEntangledAndy 2b827d9542 Use constant fps based time and fix history buffers 2024-04-16 10:12:33 +07:00
QuantumEntangledAndy c85a7641cb Insert fillers when no stream 2024-04-16 10:11:06 +07:00
QuantumEntangledAndy e74e12268a Don't use shared pipeline 2024-04-16 10:10:26 +07:00
QuantumEntangledAndy 6572be1d95 Change latency and queue buffers 2024-04-16 10:09:43 +07:00
QuantumEntangledAndy b46e5db5d8 Add message to udp buffers 2024-04-16 10:08:59 +07:00
QuantumEntangledAndy b8a22f4663 Change time units 2024-04-16 10:08:41 +07:00
QuantumEntangledAndy aa8bb6a5db Update push notification IDs between restarts 2024-04-14 14:59:59 +07:00
QuantumEntangledAndy c92e289fb7 Buffer fill prints 2024-04-13 11:31:51 +07:00
QuantumEntangledAndy 350fc7d28c Add some debug prints 2024-04-13 11:27:57 +07:00
QuantumEntangledAndy e6f8aa1e64 Addressing streaming issues 2024-04-12 19:00:00 +07:00
QuantumEntangledAndy 60cdf914a5 Bits to bytes 2024-04-12 14:58:44 +07:00
QuantumEntangledAndy be8ec85175 Reduce buffer sizes 2024-04-12 14:36:15 +07:00
QuantumEntangledAndy d94b24d86d Remove max pool size 2024-04-12 14:14:09 +07:00
QuantumEntangledAndy d6d47039ba Report byte levels 2024-04-12 11:02:24 +07:00
QuantumEntangledAndy 72c486353b Update github runners to node20 2024-04-12 10:50:55 +07:00
QuantumEntangledAndy 8c186f3f56 Run blocking stream code on a seperate thread 2024-04-12 10:28:03 +07:00
QuantumEntangledAndy 87e5bcb931 Logs on the pause 2024-04-12 09:42:34 +07:00
QuantumEntangledAndy cb27c11ed8 Debug logging cleanup 2024-04-12 09:40:42 +07:00
QuantumEntangledAndy ebf607b1a9 Disable PN if no cameras have it enabled 2024-04-11 14:57:13 +07:00
QuantumEntangledAndy 358e62601e Changes to buffer max sizes 2024-04-11 14:03:27 +07:00
QuantumEntangledAndy 7f8ccc6806 Avoid recreating the AES key 2024-04-11 09:33:50 +07:00
QuantumEntangledAndy 8f8d9e6f78 Avoid a needless copy in de 2024-04-10 17:07:35 +07:00
QuantumEntangledAndy ea1b21b7f8 Use pools and padded data 2024-04-10 16:24:04 +07:00
QuantumEntangledAndy 47a89c0c76 Debug build again 2024-04-10 16:20:09 +07:00
QuantumEntangledAndy f07b8e4a8c Test on release 2024-04-10 09:06:24 +07:00
QuantumEntangledAndy b7eb6cefdc Add a timeout so that it stops on its own (and generated a massif.out) 2024-04-09 13:02:05 +07:00
QuantumEntangledAndy 2fc5c24834 Without the --debug 2024-04-09 11:31:19 +07:00
QuantumEntangledAndy d6d4ea522e Less zealous find and replace 2024-04-09 11:28:13 +07:00
QuantumEntangledAndy 31048ed075 Basic valgrind massif docker 2024-04-09 11:26:15 +07:00
Andrew King d354b90976 Merge pull request #231 from QuantumEntangledAndy/feature/port_control 2024-04-08 10:47:32 +07:00
QuantumEntangledAndy 54f7665414 Add command for camera service port controls (not on old camera) 2024-04-05 19:06:02 +07:00
QuantumEntangledAndy 9187694889 Update to udp discovery xml 2024-04-05 18:47:32 +07:00
Andrew King c0a6123740 Merge pull request #230 from QuantumEntangledAndy/feature/shared_gst
Update deps
2024-04-05 15:15:05 +07:00
QuantumEntangledAndy 04595b6aff Windows build script woes 2024-04-05 14:55:21 +07:00
QuantumEntangledAndy 1ba899840f Merge branch 'master' into feature/shared_gst 2024-04-05 14:39:43 +07:00
QuantumEntangledAndy 919d8fffba Changes to serde 2024-04-05 13:42:28 +07:00
QuantumEntangledAndy c3ed0b41ab Test passing, clippy fixes 2024-04-05 11:29:25 +07:00
QuantumEntangledAndy 4c17fa9f2b Deps and logs
Use quick_xml instead of yaserde

Use thiserror instead of err_derive

Update deps to latest (except fcm), fcm will need to wait for reolink to update their fcm apis first in their app
2024-04-04 12:47:36 +07:00
Andrew King df0642e0f8 Merge pull request #199 from Frans-Willem/feature/fix-talk-stability 2024-03-31 10:52:15 +07:00
Andrew King cfd7ac6778 Merge pull request #184 from vfdev-5/typos 2024-03-31 10:49:43 +07:00
Andrew King 6b9731168d Merge pull request #194 from iam-TJ/doc-dissector 2024-03-31 10:49:00 +07:00
Andrew King 14f867c728 Merge pull request #200 from Cian911/ciang/support-k8s 2024-03-31 10:47:45 +07:00
Andrew King fbd19fd5c6 Merge pull request #205 from developersteve/master 2024-03-31 10:46:10 +07:00
Andrew King fde7a08405 Merge pull request #220 from rexxars/fix/typos 2024-03-31 10:44:46 +07:00
Espen Hovlandsdal 37f560cf66 Fix typos in comments and code 2024-03-14 10:24:16 -07:00
developersteve fcc559885a Update README.md with env var changes
added README information about the added `NEO_LINK_MODE` and `NEO_LINK_PORT` variables
2024-01-18 17:40:57 +11:00
developersteve 8205b652d3 Update Dockerfile with env vars 2024-01-18 17:36:31 +11:00
developersteve 1a020e5788 Update Dockerfile to include a NEO_LINK_MODE
This enables the ability to change the neolink mode type using an env var so that it's not locked to RTSP but will default to RTSP if the env var is not set.
2024-01-17 14:01:06 +11:00
Cian Gallagher b13db1227d feat: add support for running neolink in k8s. add related documentation. 2023-12-03 12:37:22 +00:00
Frans-Willem Hardijzer 261e458947 Stop making assumptions around buffer size, just use expected message duration instead. 2023-12-01 15:02:47 +01:00
Frans-Willem Hardijzer afabd05cef No need to time sending the blocks, the response message will be delayed if the buffer on the device is full. 2023-12-01 14:48:33 +01:00
Frans-Willem Hardijzer ce4e4f2f5d Move payload_bytes into loop, instead of constantly re-setting it. 2023-12-01 14:47:57 +01:00
Frans-Willem Hardijzer b6c7cff6a7 Better timing of sending chunks, works well for files, might not for streams (e.g. microphone) 2023-12-01 13:44:22 +01:00
Frans-Willem Hardijzer 9cd5551156 Properly handle response of TALK command, such that receive buffer doesn't fill up.
If receive buffer fills up, neolink will never exit as the response to TALK_STOP will not get handled correctly.
2023-12-01 13:40:45 +01:00
Tj 311b8b03d8 dissector: add instructions on building and using 2023-11-19 11:04:20 +00:00
vfdev 606c978a86 Fixed typos 2023-11-06 00:06:13 +01:00
QuantumEntangledAndy acae5f95ee Larger buffer for the appsrc 2023-10-31 08:09:07 +07:00
QuantumEntangledAndy 76e804ea6a Set missed tick to skip 2023-10-30 12:08:50 +07:00
QuantumEntangledAndy 18a69b9d1e Add a frame reorder 2023-10-30 09:44:34 +07:00
QuantumEntangledAndy 159cb4b716 Frame time changes 2023-10-30 07:44:37 +07:00
QuantumEntangledAndy 6c64cf663a Use shared factory pipeline 2023-10-29 17:29:40 +07:00
86 changed files with 3773 additions and 2389 deletions

No files matched your search

File renamed without changes.
+79 -58
View File
@@ -17,8 +17,8 @@ jobs:
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'
concurrent_skipping: "same_content_newer"
skip_after_successful_duplicate: "false"
native:
needs: pre_job
@@ -28,9 +28,9 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-22.04, windows-2022, macos-12]
os: [ubuntu-22.04, windows-2022, macos-12, macos-14] # macos-14+ is arm64
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
name: Checkout onto ${{ runner.os }}
- if: runner.os == 'Linux'
name: apt install gstreamer
@@ -45,6 +45,10 @@ jobs:
choco install -y --no-progress gstreamer --version=1.20.0
choco install -y --no-progress gstreamer-devel --version=1.20.0
$env:GSTREAMER_1_0_ROOT_MSVC_X86_64=$env:SYSTEMDRIVE + '\gstreamer\1.0\msvc_x86_64\'
# Github runners work on both C or D drive and figuring out which was used is difficult
if (-not (Test-Path -Path "$env:GSTREAMER_1_0_ROOT_MSVC_X86_64" -PathType Container)) {
$env:GSTREAMER_1_0_ROOT_MSVC_X86_64='D:\\gstreamer\1.0\msvc_x86_64\'
}
echo "GSTREAMER_1_0_ROOT_MSVC_X86_64=$env:GSTREAMER_1_0_ROOT_MSVC_X86_64"
# Proto buffers
@@ -53,9 +57,19 @@ jobs:
# Open SSL
choco install -y --no-progress openssl --version=1.1.1.2100
$env:OPENSSL_DIR=$env:SYSTEMDRIVE + '\Program Files\OpenSSL-Win64\'
# Alternative openssl location (depends on version that gets installed by choco)
if (-not (Test-Path -Path "$env:OPENSSL_DIR" -PathType Container)) {
$env:OPENSSL_DIR=$env:SYSTEMDRIVE + '\Program Files\OpenSSL\'
}
# Github runners work on both C or D drive and figuring out which was used is difficult
if (-not (Test-Path -Path "$env:OPENSSL_DIR" -PathType Container)) {
$env:OPENSSL_DIR='D:\\Program Files\OpenSSL-Win64\'
}
# Or course we could be on alternative location and drive....
if (-not (Test-Path -Path "$env:OPENSSL_DIR" -PathType Container)) {
$env:OPENSSL_DIR='D:\\Program Files\OpenSSL\'
}
# Set github vars
Add-Content -Path $env:GITHUB_ENV -Value "GSTREAMER_1_0_ROOT_MSVC_X86_64=$env:GSTREAMER_1_0_ROOT_MSVC_X86_64"
@@ -80,18 +94,18 @@ jobs:
echo "OPENSSL_INCLUDE_DIR=$(brew --prefix openssl@1.1)/include/" >> "${GITHUB_ENV}"
echo "OPENSSL_LIB_DIR=$(brew --prefix openssl@1.1)/lib/" >> "${GITHUB_ENV}"
- name: Cache cargo registry
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
if: runner.os != 'macOS' # Random missing crates on macOS, unclear why
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
@@ -102,11 +116,11 @@ jobs:
echo "GSTREAMER_1_0_ROOT_MSVC_X86_64=${GSTREAMER_1_0_ROOT_MSVC_X86_64}"
echo "PKG_CONFIG_PATH=${PKG_CONFIG_PATH}"
# pkg-config --variable pc_path pkg-config
cargo build --release --all-features
- uses: actions/upload-artifact@v3
cargo build --all-features
- uses: actions/upload-artifact@v4
with:
name: release-${{ matrix.os }}
path: "target/release/neolink*"
name: debug-${{ matrix.os }}
path: "target/debug/neolink*"
cross:
needs: pre_job
if: needs.pre_job.outputs.should_skip != 'true'
@@ -138,7 +152,7 @@ jobs:
# on i686, the pkgconfig path doesn't match the prefix!
pkgconfig: i386-linux-gnu
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install basic tools
run: |
apt-get update
@@ -168,16 +182,16 @@ jobs:
TARGET: ${{ matrix.target }}
- name: Build
run: |
cargo build --verbose --release --all-features --target=${TARGET}
cargo build --verbose --all-features --target=${TARGET}
env:
# Retarget pkg-config as described in https://www.freedesktop.org/wiki/Software/pkg-config/CrossCompileProposal/
PKG_CONFIG_ALLOW_CROSS: 1
PKG_CONFIG_LIBDIR: /usr/lib/${{ matrix.pkgconfig }}/pkgconfig
TARGET: ${{ matrix.target }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: release-${{ matrix.arch }}-bookworm
path: "target/${{ matrix.target }}/release/neolink*"
name: debug-${{ matrix.arch }}-bookworm
path: "target/${{ matrix.target }}/debug/neolink*"
push_to_registry:
name: Build Docker image
@@ -190,33 +204,33 @@ jobs:
id: vars
shell: bash
run: |
unset HAS_SECRET
if [ -n $SECRET ]; then HAS_SECRET='true' ; fi
echo "HAS_SECRET_TOKEN=${HAS_SECRET}" >> $GITHUB_OUTPUT
unset HAS_SECRET
if [ -n $SECRET ]; then HAS_SECRET='true' ; fi
echo "HAS_SECRET_TOKEN=${HAS_SECRET}" >> $GITHUB_OUTPUT
env:
SECRET: "${{ secrets.DOCKER_TOKEN }}"
- name: Check out the repo
uses: actions/checkout@v3
uses: actions/checkout@v4
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
- name: Convert username to lower case for docker
id: string_user
uses: ASzc/change-string-case-action@v5
uses: ASzc/change-string-case-action@v6
with:
string: ${{ github.repository_owner }}
- name: Convert repo to lower case for docker
id: string_repo
uses: ASzc/change-string-case-action@v5
uses: ASzc/change-string-case-action@v6
with:
string: ${{ github.repository }}
- name: Set up QEMU
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
uses: docker/setup-qemu-action@v2
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
uses: docker/setup-buildx-action@v2
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ vars.DOCKER_USERNAME || steps.string_user.outputs.lowercase }}
@@ -268,7 +282,7 @@ jobs:
REPO_NAME: ${{ steps.docker_repo.outputs.DOCKER_NWO }}
- name: Install latest rust
run: |
rustup toolchain install stable
rustup toolchain install stable
- name: Install toml-cli
run: |
cargo install toml-cli
@@ -283,28 +297,28 @@ jobs:
mkdir linux/arm
mkdir linux/arm64
- name: Download Linux x86_64
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-amd64-bookworm
name: debug-amd64-bookworm
path: linux/amd64
- name: Download Linux armv7
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-armhf-bookworm
name: debug-armhf-bookworm
path: linux/arm/v7
- name: Download Linux arm64
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-arm64-bookworm
name: debug-arm64-bookworm
path: linux/arm64
- name: Download Linux i386
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-i386-bookworm
name: debug-i386-bookworm
path: linux/386
- name: Push to Docker Hub
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
uses: docker/build-push-action@v3
uses: docker/build-push-action@v5
with:
platforms: linux/amd64,linux/arm/v7,linux/arm64,linux/386
context: .
@@ -324,10 +338,10 @@ jobs:
- "native"
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install latest rust
run: |
rustup toolchain install stable
rustup toolchain install stable
- name: Install toml-cli
run: |
cargo install toml-cli
@@ -337,45 +351,51 @@ jobs:
NEOLINK_VERSION="$(toml get Cargo.toml package.version | sed 's|"||g')"
echo "version=${NEOLINK_VERSION}" >> $GITHUB_OUTPUT
- name: Download Windows
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-windows-2022
name: debug-windows-2022
path: neolink_windows
- name: Download Macos
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-macos-12
path: neolink_macos
name: debug-macos-12
path: neolink_macos_intel
- name: Download Macos arm64
uses: actions/download-artifact@v4
with:
name: debug-macos-14
path: neolink_macos_m1
- name: Download Linux x86_64
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-ubuntu-22.04
name: debug-ubuntu-22.04
path: neolink_linux_x86_64_ubuntu
- name: Download Linux x86_64
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-amd64-bookworm
name: debug-amd64-bookworm
path: neolink_linux_x86_64_bookworm
- name: Download Linux armhf
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-armhf-bookworm
name: debug-armhf-bookworm
path: neolink_linux_armhf
- name: Download Linux arm64
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-arm64-bookworm
name: debug-arm64-bookworm
path: neolink_linux_arm64
- name: Download Linux i386
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: release-i386-bookworm
name: debug-i386-bookworm
path: neolink_linux_i386
- name: Prepare Releases
- name: Prepare releases
run: |
dirs=(
neolink_windows
neolink_macos
neolink_macos_intel
neolink_macos_m1
neolink_linux_x86_64_ubuntu
neolink_linux_x86_64_bookworm
neolink_linux_armhf
@@ -386,19 +406,20 @@ jobs:
chmod +x "${d}/neolink"*
zip -r "${d}.zip" "${d}"
done
- name: Release
- name: release
uses: softprops/action-gh-release@v1
with:
name: Neolink ${{steps.toml.outputs.version}}
files: |
neolink_windows.zip
neolink_macos.zip
neolink_macos_intel.zip
neolink_macos_m1.zip
neolink_linux_x86_64_ubuntu.zip
neolink_linux_x86_64_bookworm.zip
neolink_linux_armhf.zip
neolink_linux_arm64.zip
neolink_linux_i386.zip
body: |
Neolink Release ${{steps.toml.outputs.version}}
Neolink release ${{steps.toml.outputs.version}}
draft: true
prerelease: ${{ contains(github.ref_name, 'rc') }}
prerelease: ${{ contains(github.ref_name, 'rc') }}
+8 -8
View File
@@ -14,8 +14,8 @@ jobs:
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'
concurrent_skipping: "same_content_newer"
skip_after_successful_duplicate: "false"
native:
needs: pre_job
@@ -27,21 +27,21 @@ jobs:
id: vars
shell: bash
run: |
unset HAS_SECRET
if [ -n $SECRET ]; then HAS_SECRET='true' ; fi
echo "HAS_SECRET_TOKEN=${HAS_SECRET}" >> $GITHUB_OUTPUT
unset HAS_SECRET
if [ -n $SECRET ]; then HAS_SECRET='true' ; fi
echo "HAS_SECRET_TOKEN=${HAS_SECRET}" >> $GITHUB_OUTPUT
env:
SECRET: "${{ secrets.DOCKER_PRUNE_TOKEN }}"
- name: Convert username to lower case for docker
id: string_user
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
uses: ASzc/change-string-case-action@v5
uses: ASzc/change-string-case-action@v6
with:
string: ${{ github.repository_owner }}
- name: Convert repo to lower case for docker
id: string_repo
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
uses: ASzc/change-string-case-action@v5
uses: ASzc/change-string-case-action@v6
with:
string: ${{ github.event.repository.name }}
- name: Run Clean Script
@@ -144,4 +144,4 @@ jobs:
DAYS: "100"
REPO_NAME: ${{ steps.string_repo.outputs.lowercase }}
OWNER: ${{ vars.DOCKER_USERNAME || steps.string_user.outputs.lowercase }}
TOKEN: ${{ secrets.DOCKER_PRUNE_TOKEN }}
TOKEN: ${{ secrets.DOCKER_PRUNE_TOKEN }}
+24 -24
View File
@@ -14,8 +14,8 @@ jobs:
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'
concurrent_skipping: "same_content_newer"
skip_after_successful_duplicate: "false"
check_clippy:
needs: pre_job
@@ -23,18 +23,18 @@ jobs:
name: Clippy
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- 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 protobuf-compiler
- name: Install nightly rust
run: |
- uses: actions/checkout@v4
- 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 protobuf-compiler
- name: Install nightly rust
run: |
rustup toolchain install nightly --component clippy
rustup override set nightly
- name: Run clippy manually
run: cargo +nightly clippy --workspace --all-targets --all-features
- name: Run clippy manually
run: cargo +nightly clippy --workspace --all-targets --all-features
check_fmt:
needs: pre_job
@@ -42,14 +42,14 @@ jobs:
name: Rust-fmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install nightly rust
run: |
- uses: actions/checkout@v4
- name: Install nightly rust
run: |
rustup toolchain install nightly --component rustfmt
rustup override set nightly
- name: rustfmt
run: |
cargo +nightly fmt --all -- --check
- name: rustfmt
run: |
cargo +nightly fmt --all -- --check
check_lua:
needs: pre_job
@@ -57,9 +57,9 @@ jobs:
name: Luacheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run luacheck
uses: nebularg/actions-luacheck@v1
with:
files: 'dissector/baichuan.lua'
args: --globals Dissector Proto ProtoField base ByteArray DESEGMENT_ONE_MORE_SEGMENT DissectorTable Pref
- uses: actions/checkout@v4
- name: Run luacheck
uses: nebularg/actions-luacheck@v1
with:
files: "dissector/baichuan.lua"
args: --globals Dissector Proto ProtoField base ByteArray DESEGMENT_ONE_MORE_SEGMENT DissectorTable Pref
Generated
+685 -905
View File
File diff suppressed because it is too large. Load diff
+16 -16
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink"
description = "A standards-compliant bridge to Reolink IP cameras"
version = "0.6.3-rc.1"
version = "0.6.3-rc.2"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
@@ -14,35 +14,35 @@ members = [
[dependencies]
anyhow = "1.0.70"
async-stream = "0.3.5"
base64 = "0.21.2"
base64 = "0.22.0"
byte-slice-cast = "1.2.2"
bytes = "1.6.0"
clap = { version = "4.2.2", features = ["derive", "cargo"] }
console-subscriber = "0.2.0"
crossbeam-channel = "0.5.8"
dirs = "5.0.1"
env_logger = "0.10.0"
fcm-push-listener = "2.0.1"
env_logger = "0.11.3"
fcm-push-listener = "2.0.3"
futures = "0.3.28"
gstreamer = "0.21.0"
gstreamer-app = { version = "0.21.0", features = ["v1_18"] }
gstreamer-rtsp = { version = "0.21.0", features = ["v1_18"] }
gstreamer-rtsp-server = { version = "0.21.0", features = ["v1_18"] }
heck = "0.4.1"
lazy_static = "1.4.0"
gstreamer = "0.22.3"
gstreamer-app = { version = "0.22.0", features = ["v1_20"] }
gstreamer-rtsp = { version = "0.22.0", features = ["v1_20"] }
gstreamer-rtsp-server = { version = "0.22.0", features = ["v1_20"] }
heck = "0.5.0"
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
md5 = "0.7.0"
neolink_core = { path = "crates/core", version = "0.6.3-rc.1" }
neolink_core = { path = "crates/core", version = "0.6.3-rc.2" }
once_cell = "1.19.0"
quick-xml = { version = "0.31.0", features = ["serialize"] }
regex = "1.7.3"
rumqttc = "0.22.0"
rumqttc = "0.24.0"
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0.96"
tokio = { version = "1.27.0", features = ["rt-multi-thread", "macros", "io-util", "tracing"] }
tokio-stream = "0.1.12"
tokio-util = { version = "0.7.7", features = ["full", "tracing"] }
toml = "0.8.2"
validator = "0.16.0"
validator_derive = "0.16.0"
yaserde = "0.8.0"
validator = "0.17.0"
validator_derive = "0.17.0"
[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = "0.5"
+8 -7
View File
@@ -8,17 +8,15 @@ FROM docker.io/rust:slim-bookworm AS build
ARG TARGETPLATFORM
ENV DEBIAN_FRONTEND=noninteractive
WORKDIR /usr/local/src/neolink
COPY . /usr/local/src/neolink
# Build the main program or copy from artifact
#
# We prefer building from artifact to reduce
# We prefer copying from artifact to reduce
# build time on the github runners
#
# Because of this through, during normal
# Because of this though, during normal
# github runner ops we are not testing the
# docker to see if it will build from scratch
# so if it is failing please make a PR
@@ -83,8 +81,11 @@ COPY docker/entrypoint.sh /entrypoint.sh
RUN gst-inspect-1.0; \
chmod +x "/usr/local/bin/neolink" && \
"/usr/local/bin/neolink" --version && \
mkdir -m 0700 /root/.config/ # Location that the push notifications are cached to
mkdir -m 0700 /root/.config/ && \
CMD ["/usr/local/bin/neolink", "rtsp", "--config", "/etc/neolink.toml"]
ENV NEO_LINK_MODE="rtsp" NEO_LINK_PORT=8554
CMD /usr/local/bin/neolink ${NEO_LINK_MODE} --config /etc/neolink.toml
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 8554
EXPOSE ${NEO_LINK_PORT}
+10 -6
View File
@@ -33,7 +33,7 @@ features not yet in upstream master.
**Minor Features**:
- Improved error messages when missing gstreamer plugins
- Protocol more closely follows offical reolink format
- Protocol more closely follows official reolink format
- Possibly can handle more simulatenous connections
- More ways to connect to the camera. Including Relaying through reolink
servers
@@ -50,7 +50,7 @@ Install the latest [gstreamer](https://gstreamer.freedesktop.org/download/)
(1.20.5 as of writing this).
- **Windows**: ensure you install `full` when prompted in the MSI options.
- **Mac**: Install the dpkg version on the offical gstreamer website over
- **Mac**: Install the dpkg version on the official gstreamer website over
the brew version
- **Ubuntu/Debian**: These packages should work
@@ -269,7 +269,7 @@ reflected in the rtsp
These include changing the:
- Avaliable users
- Available users
```toml
[[users]]
@@ -284,7 +284,7 @@ These include changing the:
permitted_users = [ "me" ]
```
- Avaliable streams
- Available streams
```toml
[[cameras]]
@@ -324,7 +324,7 @@ enable_motion = false # motion detection
# (limited battery drain since it
# is a passive listening connection)
#
enable_light = false # flood lights only avaliable on some camera
enable_light = false # flood lights only available on some camera
# (limited battery drain since it
# is a passive listening connection)
#
@@ -355,7 +355,7 @@ must be manually specified.
features = ["floodlight"]
```
Avaliable features are:
Available features are:
- `floodlight`: This adds a light control to home assistant
- `camera`: This adds a camera preview to home assistant. It is only updated
@@ -456,6 +456,10 @@ docker pull quantumentangledandy/neolink
# network=host, notably macos lacks this option.
docker run --network host --volume=$PWD/config.toml:/etc/neolink.toml quantumentangledandy/neolink
```
#### Environmental Variables
There are currently 2 environmental variables available as part of the container:
- `NEO_LINK_MODE`: defaults to `"rtsp"` if not set, other options are "mqtt" or "mqtt-rtsp".
- `NEO_LINK_PORT`: defaults to `8554`, set this to your required port value.
### Image
+11 -2
View File
@@ -63,9 +63,18 @@ fn platform_cfg() {
let gstreamer_dir = env::var_os("GSTREAMER_1_0_ROOT_MACOSX")
.and_then(|x| x.into_string().ok())
.unwrap_or_else(|| r"/Library/Frameworks/GStreamer.framework/Versions/1.0".to_string());
let openssl_dir = env::var_os("OPENSSL_1_1_ROOT_MACOSX")
.and_then(|x| x.into_string().ok())
.unwrap_or_else(|| r"/usr/local/opt/openssl@1.1".to_string());
println!(r"cargo:rustc-link-search=native={}/lib", gstreamer_dir);
println!(r"cargo:rustc-link-arg=-Wl,-rpath,{}/lib", gstreamer_dir);
println!(
r"cargo:rustc-link-search=native={}/lib;{}/lib",
gstreamer_dir, openssl_dir
);
println!(
r"cargo:rustc-link-arg=-Wl,-rpath,{}/lib;{}/lib/",
gstreamer_dir, openssl_dir
);
}
#[cfg(all(not(target_os = "macos"), not(target_os = "windows")))]
+4 -5
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink_core"
description = "Core services and structure for Reolink IP cameras"
version = "0.6.3-rc.1"
version = "0.6.3-rc.2"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
@@ -13,23 +13,22 @@ cfb-mode = "0.8.2"
cookie-factory = "0.3.2"
crc32fast = "1.3.2"
crossbeam-channel = "0.5.8"
delegate = "0.10.0"
err-derive = "0.3.1"
delegate = "0.12.0"
futures = "0.3.28"
get_if_addrs = "0.5.3"
lazy_static = "1.4.0"
log = "0.4.17"
md5 = "0.7.0"
nom = { version = "7.1.3", features = ["alloc"] }
quick-xml = { version = "0.31.0", features = ["serialize"] }
rand = "0.8.5"
regex = "1.7.3"
serde = { version = "1.0.106", features = ["derive"] }
thiserror = "1.0.58"
time = { version = "0.3.20" , features = [ "macros", "parsing", "local-offset" ]}
tokio = { version = "1.27.0", features = ["full"] }
tokio-stream = { version = "0.1.12", features = ["sync", "time", "net"] }
tokio-util = { version = "0.7.7", features = ["full", "tracing"] }
yaserde = "0.8.0"
yaserde_derive = "0.8.0"
[dev-dependencies]
assert_matches = "1.5.0"
+8 -7
View File
@@ -33,18 +33,19 @@ impl Encoder<Bc> for BcCodex {
fn encode(&mut self, item: Bc, dst: &mut BytesMut) -> Result<()> {
// let context = self.context.read().unwrap();
const BC_ENCRYPTED: EncryptionProtocol = EncryptionProtocol::BCEncrypt;
let buf: Vec<u8> = Default::default();
let enc_protocol: EncryptionProtocol = match self.context.get_encrypted() {
EncryptionProtocol::Aes(_) | EncryptionProtocol::FullAes(_)
let enc_protocol: &EncryptionProtocol = match self.context.get_encrypted() {
EncryptionProtocol::Aes { .. } | EncryptionProtocol::FullAes { .. }
if item.meta.msg_id == 1 =>
{
// During login the encyption protocol cannot go higher than BCEncrypt
// even if we support AES. (BUt it can go lower i.e. None)
EncryptionProtocol::BCEncrypt
&BC_ENCRYPTED
}
n => *n,
n => n,
};
let buf = item.serialize(buf, &enc_protocol)?;
let buf = item.serialize(buf, enc_protocol)?;
dst.extend_from_slice(buf.as_slice());
Ok(())
}
@@ -117,10 +118,10 @@ impl Decoder for BcCodex {
match encryption_protocol_byte {
0x00 => self.context.set_encrypted(EncryptionProtocol::Unencrypted),
0x01 => self.context.set_encrypted(EncryptionProtocol::BCEncrypt),
0x02 => self.context.set_encrypted(EncryptionProtocol::Aes(
0x02 => self.context.set_encrypted(EncryptionProtocol::aes(
self.context.credentials.make_aeskey(nonce),
)),
0x12 => self.context.set_encrypted(EncryptionProtocol::FullAes(
0x12 => self.context.set_encrypted(EncryptionProtocol::full_aes(
self.context.credentials.make_aeskey(nonce),
)),
_ => {
+122
View File
@@ -0,0 +1,122 @@
use aes::{
cipher::{AsyncStreamCipher, KeyIvInit},
Aes128,
};
use cfb_mode::{Decryptor, Encryptor};
type Aes128CfbEnc = Encryptor<Aes128>;
type Aes128CfbDec = Decryptor<Aes128>;
const XML_KEY: [u8; 8] = [0x1F, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0xFF];
const IV: &[u8] = b"0123456789abcdef";
/// These are the encyption modes supported by the camera
///
/// The mode is negotiated during login
#[derive(Debug, Clone)]
pub enum EncryptionProtocol {
/// Older camera use no encryption
Unencrypted,
/// Camera/Firmwares before 2021 use BCEncrypt which is a simple XOr
BCEncrypt,
/// Latest cameras/firmwares use Aes with the key derived from
/// the camera's password and the negotiated NONCE
Aes {
/// The encryptor
enc: Aes128CfbEnc,
/// The decryptor
dec: Aes128CfbDec,
},
/// Same as Aes but the media stream is also encrypted and not just
/// the control commands
FullAes {
/// The encryptor
enc: Aes128CfbEnc,
/// The decryptor
dec: Aes128CfbDec,
},
}
impl EncryptionProtocol {
/// Helper to make unencrypted
pub fn unencrypted() -> Self {
EncryptionProtocol::Unencrypted
}
/// Helper to make bcencrypted
pub fn bcencrypt() -> Self {
EncryptionProtocol::BCEncrypt
}
/// Helper to make aes
pub fn aes(key: [u8; 16]) -> Self {
EncryptionProtocol::Aes {
enc: Aes128CfbEnc::new(key.as_slice().into(), IV.into()),
dec: Aes128CfbDec::new(key.as_slice().into(), IV.into()),
}
}
/// Helper to make full aes
pub fn full_aes(key: [u8; 16]) -> Self {
EncryptionProtocol::FullAes {
enc: Aes128CfbEnc::new(key.as_slice().into(), IV.into()),
dec: Aes128CfbDec::new(key.as_slice().into(), IV.into()),
}
}
/// Decrypt the data, offset comes from the header of the packet
pub fn decrypt(&self, offset: u32, buf: &[u8]) -> Vec<u8> {
match self {
EncryptionProtocol::Unencrypted => buf.to_vec(),
EncryptionProtocol::BCEncrypt => {
let key_iter = XML_KEY.iter().cycle().skip(offset as usize % 8);
key_iter
.zip(buf)
.map(|(key, i)| *i ^ key ^ (offset as u8))
.collect()
}
EncryptionProtocol::Aes { dec, .. } | EncryptionProtocol::FullAes { dec, .. } => {
// AES decryption
let mut decrypted = buf.to_vec();
dec.clone().decrypt(&mut decrypted);
decrypted
}
}
}
/// Encrypt the data, offset comes from the header of the packet
pub fn encrypt(&self, offset: u32, buf: &[u8]) -> Vec<u8> {
match self {
EncryptionProtocol::Unencrypted => {
// Encrypt is the same as decrypt
self.decrypt(offset, buf)
}
EncryptionProtocol::BCEncrypt => {
// Encrypt is the same as decrypt
self.decrypt(offset, buf)
}
EncryptionProtocol::Aes { enc, .. } | EncryptionProtocol::FullAes { enc, .. } => {
// AES encryption
let mut encrypted = buf.to_vec();
enc.clone().encrypt(&mut encrypted);
encrypted
}
}
}
}
#[test]
fn test_xml_crypto() {
let sample = include_bytes!("samples/xml_crypto_sample1.bin");
let should_be = include_bytes!("samples/xml_crypto_sample1_plaintext.bin");
let decrypted = EncryptionProtocol::BCEncrypt.decrypt(0, &sample[..]);
assert_eq!(decrypted, &should_be[..]);
}
#[test]
fn test_xml_crypto_roundtrip() {
let zeros: [u8; 256] = [0; 256];
let decrypted = EncryptionProtocol::BCEncrypt.encrypt(0, &zeros[..]);
let encrypted = EncryptionProtocol::BCEncrypt.decrypt(0, &decrypted[..]);
assert_eq!(encrypted, &zeros[..]);
}
+64 -47
View File
@@ -1,6 +1,4 @@
use super::model::*;
use super::xml::{BcPayloads, BcXml};
use super::xml_crypto;
use crate::Error;
use bytes::{Buf, BytesMut};
use log::*;
@@ -84,10 +82,10 @@ fn bc_modern_msg<'a>(
Err,
};
fn make_error<I, E: ParseError<I>>(input: I, ctx: &'static str, kind: ErrorKind) -> E
fn make_error<I, E>(input: I, ctx: &'static str, kind: ErrorKind) -> E
where
I: std::marker::Copy,
E: ContextError<I>,
E: ParseError<I> + ContextError<I>,
{
E::add_context(input, ctx, E::from_error_kind(input, kind))
}
@@ -105,7 +103,7 @@ fn bc_modern_msg<'a>(
let processed_ext_buf = match context.get_encrypted() {
EncryptionProtocol::Unencrypted => ext_buf,
encryption_protocol => {
decrypted = xml_crypto::decrypt(header.channel_id as u32, ext_buf, encryption_protocol);
decrypted = encryption_protocol.decrypt(header.channel_id as u32, ext_buf);
&decrypted
}
};
@@ -123,6 +121,7 @@ fn bc_modern_msg<'a>(
// Apply the XML parse function, but throw away the reference to decrypted in the Ok and
// Err case. This error-error-error thing is the same idiom Nom uses internally.
let parsed = Extension::try_parse(processed_ext_buf).map_err(|_| {
log::error!("Extension buffer: {:?}", processed_ext_buf);
Err::Error(make_error(
buf,
"Unable to parse Extension XML",
@@ -151,6 +150,8 @@ fn bc_modern_msg<'a>(
let payload;
if payload_len > 0 {
// Extract remainder of message as binary, if it exists
const UNENCRYPTED: EncryptionProtocol = EncryptionProtocol::Unencrypted;
const BC_ENCRYPTED: EncryptionProtocol = EncryptionProtocol::BCEncrypt;
let encryption_protocol = match header {
BcHeader {
msg_id: 1,
@@ -161,27 +162,27 @@ fn bc_modern_msg<'a>(
// Durig login, the max encryption is BcEncrypt since
// the nonce has not been exchanged yet
match response_code & 0xff {
0x00 => EncryptionProtocol::Unencrypted,
_ => EncryptionProtocol::BCEncrypt,
0x00 => &UNENCRYPTED,
_ => &BC_ENCRYPTED,
}
}
BcHeader { msg_id: 1, .. } => {
match *context.get_encrypted() {
EncryptionProtocol::Aes(_) | EncryptionProtocol::FullAes(_) => {
match &context.get_encrypted() {
EncryptionProtocol::Aes { .. } | EncryptionProtocol::FullAes { .. } => {
// During login max is BcEncrypt
EncryptionProtocol::BCEncrypt
&BC_ENCRYPTED
}
n => n,
n => *n,
}
}
_ => *context.get_encrypted(),
_ => context.get_encrypted(),
};
let processed_payload_buf =
xml_crypto::decrypt(header.channel_id as u32, payload_buf, &encryption_protocol);
encryption_protocol.decrypt(header.channel_id as u32, payload_buf);
if context.in_bin_mode.contains(&(header.msg_num)) || in_binary {
payload = match (context.get_encrypted(), encrypted_len) {
(EncryptionProtocol::FullAes(_), Some(encrypted_len)) => {
(EncryptionProtocol::FullAes { .. }, Some(encrypted_len)) => {
// if if context.debug {
// log::trace!("Binary: {:X?}", &processed_payload_buf[0..30]);
// }
@@ -199,13 +200,14 @@ fn bc_modern_msg<'a>(
.unwrap_or("Not Text".to_string())
);
}
let xml = BcXml::try_parse(processed_payload_buf.as_slice()).map_err(|_| {
let xml = BcXml::try_parse(processed_payload_buf.as_slice()).map_err(|e| {
error!("header.msg_id: {}", header.msg_id);
error!(
"processed_payload_buf: {:X?}::{:?}",
processed_payload_buf,
std::str::from_utf8(&processed_payload_buf)
);
log::error!("e: {:?}", e);
Err::Error(make_error(
buf,
"Unable to parse Payload XML",
@@ -259,9 +261,18 @@ mod tests {
use super::*;
use crate::bc::xml::*;
use assert_matches::assert_matches;
use env_logger::Env;
fn init() {
let _ = env_logger::Builder::from_env(Env::default().default_filter_or("info"))
.is_test(true)
.try_init();
}
#[test]
fn test_bc_modern_login() {
init();
let sample = include_bytes!("samples/model_sample_modern_login.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -290,45 +301,39 @@ mod tests {
#[test]
// This is an 0xdd03 encryption from an Argus 2
//
// It is currently unsupported
fn test_03_enc_login() {
init();
let sample = include_bytes!("samples/battery_enc.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
let (buf, header) = bc_header(&sample[..]).unwrap();
assert!(bc_body(&context, &header, buf).is_err());
// It should error because we don't support it
//
// The following would be its contents if we
// did support it (maybe one day :) left it here
// for then)
//
//
// let (_, body) = bc_body(&mut context, &header, buf).unwrap();
// assert_eq!(header.msg_id, 1);
// assert_eq!(header.body_len, 175);
// assert_eq!(header.channel_id, 0);
// assert_eq!(header.stream_type, 0);
// assert_eq!(header.payload_offset, None);
// assert_eq!(header.response_code, 0xdd03);
// assert_eq!(header.class, 0x6614);
// match body {
// BcBody::ModernMsg(ModernMsg {
// payload:
// Some(BcPayloads::BcXml(BcXml {
// encryption: Some(encryption),
// ..
// })),
// ..
// }) => assert_eq!(encryption.nonce, "0-AhnEZyUg6eKrJFIWgXPF"),
// _ => panic!(),
// }
let (_, body) = bc_body(&context, &header, buf).unwrap();
assert_eq!(header.msg_id, 1);
assert_eq!(header.body_len, 175);
assert_eq!(header.channel_id, 0);
assert_eq!(header.stream_type, 0);
assert_eq!(header.payload_offset, None);
assert_eq!(header.response_code, 0xdd03);
assert_eq!(header.class, 0x6614);
match body {
BcBody::ModernMsg(ModernMsg {
payload:
Some(BcPayloads::BcXml(BcXml {
encryption: Some(encryption),
..
})),
..
}) => assert_eq!(encryption.nonce, "0-AhnEZyUg6eKrJFIWgXPF"),
_ => panic!(),
}
}
#[test]
fn test_bc_legacy_login() {
init();
let sample = include_bytes!("samples/model_sample_legacy_login.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -353,6 +358,8 @@ mod tests {
#[test]
fn test_bc_modern_login_failed() {
init();
let sample = include_bytes!("samples/modern_login_failed.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -377,6 +384,8 @@ mod tests {
#[test]
fn test_bc_modern_login_success() {
init();
let sample = include_bytes!("samples/modern_login_success.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -404,6 +413,8 @@ mod tests {
#[test]
fn test_bc_binary_mode() {
init();
let sample1 = include_bytes!("samples/modern_video_start1.bin");
let sample2 = include_bytes!("samples/modern_video_start2.bin");
@@ -439,10 +450,12 @@ mod tests {
#[test]
// B800 seems to have a different header to the E1 and swann cameras
// the stream_type and message_num do not seem to set in the offical clients
// the stream_type and message_num do not seem to set in the official clients
//
// They also have extra streams
fn test_bc_b800_externstream() {
init();
let sample = include_bytes!("samples/xml_externstream_b800.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -481,10 +494,12 @@ mod tests {
#[test]
// B800 seems to have a different header to the E1 and swann cameras
// the stream_type and message_num do not seem to set in the offical clients
// the stream_type and message_num do not seem to set in the official clients
//
// They also have extra streams
fn test_bc_b800_substream() {
init();
let sample = include_bytes!("samples/xml_substream_b800.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
@@ -523,10 +538,12 @@ mod tests {
#[test]
// B800 seems to have a different header to the E1 and swann cameras
// the stream_type and message_num do not seem to set in the offical clients
// the stream_type and message_num do not seem to set in the official clients
//
// They also have extra streams
fn test_bc_b800_mainstream() {
init();
let sample = include_bytes!("samples/xml_mainstream_b800.bin");
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
+2 -1
View File
@@ -38,6 +38,7 @@ pub mod ser;
/// Contains the structs for the know xmls of payloads and extension
pub mod xml;
mod xml_crypto;
/// Contains the encryption protocols
pub mod crypto;
pub(crate) mod codex;
+5 -17
View File
@@ -1,5 +1,6 @@
use crate::Credentials;
pub use super::crypto::EncryptionProtocol;
pub use super::xml::{BcPayloads, BcXml, Extension};
use std::collections::HashSet;
@@ -31,6 +32,10 @@ pub const MSG_ID_REBOOT: u32 = 23;
pub const MSG_ID_MOTION_REQUEST: u32 = 31;
/// Motion detection messages
pub const MSG_ID_MOTION: u32 = 33;
/// Set service ports
pub const MSG_ID_SET_SERVICE_PORTS: u32 = 36;
/// Get service ports
pub const MSG_ID_GET_SERVICE_PORTS: u32 = 37;
/// Version messages have this ID
pub const MSG_ID_VERSION: u32 = 80;
/// Ping messages have this ID
@@ -206,23 +211,6 @@ pub(super) struct BcSendInfo {
pub payload_offset: Option<u32>,
}
/// These are the encyption modes supported by the camera
///
/// The mode is negotiated during login
#[derive(Debug, Clone, Copy)]
pub enum EncryptionProtocol {
/// Older camera use no encryption
Unencrypted,
/// Camera/Firmwares before 2021 use BCEncrypt which is a simple XOr
BCEncrypt,
/// Latest cameras/firmwares use Aes with the key derived from
/// the camera's password and the negotiated NONCE
Aes([u8; 16]),
/// Same as Aes but the media stream is also encrypted and not just
/// the control commands
FullAes([u8; 16]),
}
#[derive(Debug)]
pub(crate) struct BcContext {
pub(crate) credentials: Credentials,
+6 -4
View File
@@ -1,6 +1,4 @@
use super::model::*;
use super::xml::BcPayloads;
use super::xml_crypto;
use cookie_factory::bytes::*;
use cookie_factory::sequence::tuple;
use cookie_factory::{combinator::*, gen};
@@ -80,7 +78,7 @@ fn bc_ext<W: Write>(
encryption_protocol: &EncryptionProtocol,
) -> impl SerializeFn<W> {
let xml_bytes = xml.serialize(vec![]).unwrap();
let enc_bytes = xml_crypto::encrypt(enc_offset, &xml_bytes, encryption_protocol);
let enc_bytes = encryption_protocol.encrypt(enc_offset, &xml_bytes);
slice(enc_bytes)
}
@@ -92,7 +90,11 @@ fn bc_payload<W: Write>(
let payload_bytes = match payload {
BcPayloads::BcXml(x) => {
let xml_bytes = x.serialize(vec![]).unwrap();
xml_crypto::encrypt(enc_offset, &xml_bytes, encryption_protocol)
// log::debug!(
// "Seialised as: {:?}",
// std::str::from_utf8(xml_bytes.as_ref())
// );
encryption_protocol.encrypt(enc_offset, &xml_bytes)
}
BcPayloads::Binary(x) => x.to_owned(),
};
+627 -336
View File
File diff suppressed because it is too large. Load diff
-69
View File
@@ -1,69 +0,0 @@
use super::model::EncryptionProtocol;
use aes::{
cipher::{AsyncStreamCipher, KeyIvInit},
Aes128,
};
use cfb_mode::{Decryptor, Encryptor};
type Aes128CfbEnc = Encryptor<Aes128>;
type Aes128CfbDec = Decryptor<Aes128>;
const XML_KEY: [u8; 8] = [0x1F, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0xFF];
const IV: &[u8] = b"0123456789abcdef";
pub fn decrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol) -> Vec<u8> {
match encryption_protocol {
EncryptionProtocol::Unencrypted => buf.to_vec(),
EncryptionProtocol::BCEncrypt => {
let key_iter = XML_KEY.iter().cycle().skip(offset as usize % 8);
key_iter
.zip(buf)
.map(|(key, i)| *i ^ key ^ (offset as u8))
.collect()
}
EncryptionProtocol::Aes(aeskey) | EncryptionProtocol::FullAes(aeskey) => {
// AES decryption
let mut decrypted = buf.to_vec();
Aes128CfbDec::new(aeskey.into(), IV.into()).decrypt(&mut decrypted);
decrypted
}
}
}
pub fn encrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol) -> Vec<u8> {
match encryption_protocol {
EncryptionProtocol::Unencrypted => {
// Encrypt is the same as decrypt
decrypt(offset, buf, encryption_protocol)
}
EncryptionProtocol::BCEncrypt => {
// Encrypt is the same as decrypt
decrypt(offset, buf, encryption_protocol)
}
EncryptionProtocol::Aes(aeskey) | EncryptionProtocol::FullAes(aeskey) => {
// AES encryption
let mut encrypted = buf.to_vec();
Aes128CfbEnc::new(aeskey.into(), IV.into()).encrypt(&mut encrypted);
encrypted
}
}
}
#[test]
fn test_xml_crypto() {
let sample = include_bytes!("samples/xml_crypto_sample1.bin");
let should_be = include_bytes!("samples/xml_crypto_sample1_plaintext.bin");
let decrypted = decrypt(0, &sample[..], &EncryptionProtocol::BCEncrypt);
assert_eq!(decrypted, &should_be[..]);
}
#[test]
fn test_xml_crypto_roundtrip() {
let zeros: [u8; 256] = [0; 256];
let decrypted = encrypt(0, &zeros[..], &EncryptionProtocol::BCEncrypt);
let encrypted = decrypt(0, &decrypted[..], &EncryptionProtocol::BCEncrypt);
assert_eq!(encrypted, &zeros[..]);
}
+2 -1
View File
@@ -30,6 +30,7 @@ mod ptz;
mod pushinfo;
mod reboot;
mod resolution;
mod services;
mod siren;
mod snap;
mod stream;
@@ -194,7 +195,7 @@ impl BcCamera {
DiscoveryMethods::Map => (true, true, true, false),
DiscoveryMethods::Relay => (true, true, true, true),
DiscoveryMethods::Cellular => (false, false, true, true),
DiscoveryMethods::Debug => (false, false, false, true),
DiscoveryMethods::Debug => (false, false, true, false),
};
let res = tokio::select! {
+4 -3
View File
@@ -30,7 +30,7 @@ impl BcCamera {
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcBody::ModernMsg(ModernMsg {
@@ -54,8 +54,9 @@ impl BcCamera {
/// Populate ability list of the camera
pub async fn polulate_abilities(&self) -> Result<()> {
let info = self.get_abilityinfo().await?;
let info_res = yaserde::ser::serialize_with_writer(&info, vec![], &Default::default());
if let Ok(Ok(info_str)) = info_res.map(String::from_utf8) {
let mut ser_buf = bytes::BytesMut::new();
let info_res = quick_xml::se::to_writer(&mut ser_buf, &info).map(|_| ser_buf);
if let Ok(Ok(info_str)) = info_res.map(|b| std::str::from_utf8(&b).map(|a| a.to_owned())) {
debug!("Abilities: {}", info_str);
}
+7 -8
View File
@@ -57,14 +57,13 @@ impl BcCamera {
);
}
PrintFormat::Xml => {
let bat_ser = String::from_utf8(
yaserde::ser::serialize_with_writer(
battery,
vec![],
&Default::default(),
)
.expect("Should Ser the struct"),
)
let bat_ser = String::from_utf8({
let mut ser_buf = bytes::BytesMut::new();
let parsed =
quick_xml::se::to_writer(&mut ser_buf, &battery)
.map(|_| ser_buf);
parsed.expect("Could not serialise data").to_vec()
})
.expect("Should be UTF8");
println!("{}", bat_ser);
}
@@ -178,7 +178,6 @@ impl BcConnection {
pub async fn shutdown(&self) -> Result<()> {
let _ = self.poll_commander.send(PollCommand::Disconnect).await;
log::debug!("BcConnection::shutdown Cancel");
self.cancel.cancel();
let mut locked_threads = self.rx_thread.write().await;
while locked_threads.join_next().await.is_some() {}
@@ -196,7 +195,6 @@ impl Drop for BcConnection {
let mut threads = std::mem::take(&mut self.rx_thread);
tokio::task::spawn(async move {
let _ = poll_commander.send(PollCommand::Disconnect).await;
log::debug!("BcConnection::shutdown Cancel");
let locked_threads = threads.get_mut();
while locked_threads.join_next().await.is_some() {}
log::trace!("Dropped BcConnection");
@@ -331,17 +329,19 @@ impl Poller {
}
let _ = sender.send(Ok(response)).await;
} else {
debug!(
trace!(
"Ignoring uninteresting message id {} (number: {})",
msg_id, msg_num
msg_id,
msg_num
);
trace!("Contents: {:?}", response);
}
}
(None, None) => {
debug!(
trace!(
"Ignoring uninteresting message id {} (number: {})",
msg_id, msg_num
msg_id,
msg_num
);
trace!("Contents: {:?}", response);
}
@@ -79,16 +79,13 @@ impl<'a> BcSubscription<'a> {
pub fn payload_stream(&'_ mut self) -> impl Stream<Item = IoResult<Vec<u8>>> + '_ {
(&mut self.rx).filter_map(|x| match x {
Ok(Bc {
meta: BcMeta { msg_id, .. },
meta: BcMeta { .. },
body:
BcBody::ModernMsg(ModernMsg {
payload: Some(BcPayloads::Binary(data)),
..
}),
}) => {
log::debug!("PayloadStream got binary data: {msg_id}");
Some(Ok(data))
}
}) => Some(Ok(data)),
Ok(_) => None,
Err(e) => Some(Err(IoError::new(ErrorKind::Other, e))),
})
@@ -97,15 +94,9 @@ impl<'a> BcSubscription<'a> {
pub fn bcmedia_stream(&'_ mut self, strict: bool) -> impl Stream<Item = Result<BcMedia>> + '_ {
let async_read = self
.payload_stream()
.map(|frame| {
log::debug!("- BcMedia Payloader got packet");
frame
})
.map(|frame| frame)
.into_async_read()
.compat();
FramedRead::new(async_read, BcMediaCodex::new(strict)).map(|frame| {
log::debug!(" - BcMedia Payloader decoded packet");
frame
})
FramedRead::new(async_read, BcMediaCodex::new(strict)).map(|frame| frame)
}
}
@@ -139,6 +139,7 @@ impl Discoverer {
tokio::task::yield_now().await;
match reader.next().await {
Some(Ok((BcUdp::Discovery(bcudp), addr))) => {
log::info!("Got discovery {:?} for {}", bcudp, addr);
let tid = bcudp.tid;
let mut needs_removal = false;
if let (Some(sender), true) =
@@ -158,9 +159,10 @@ impl Discoverer {
}
Some(Ok(bcudp)) => {
// Only discovery packets should be possible atm
trace!("Got non Discovery during discovery: {:?}", bcudp);
log::info!("Got non Discovery during discovery: {:?}", bcudp);
}
Some(Err(e)) => {
log::error!("Error on discovery socket: {:?}", e);
let mut locked_sub = thread_subscriber.write().await;
for (_, sub) in locked_sub.iter() {
let _ = sub.send(Err(e.clone())).await;
@@ -248,7 +250,7 @@ impl Discoverer {
tokio::select! {
v = async {
loop {
let (reply, addr) = reply.next().await.ok_or(Error::ConnectionUnavaliable)??;
let (reply, addr) = reply.next().await.ok_or(Error::ConnectionUnavailable)??;
if let Some(result) = map(reply, addr) {
return Ok(result);
}
@@ -350,33 +352,45 @@ impl Discoverer {
let port = self.local_addr().port();
let msg = UdpDiscovery {
tid,
payload: UdpXml {
c2d_c: Some(C2dC {
uid: uid.to_string(),
cli: ClientList { port: port as u32 },
cid: client_id,
mtu: MTU,
debug: false,
os: "MAC".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2dC(C2dC {
uid: uid.to_string(),
cli: ClientList { port: port as u32 },
cid: client_id,
mtu: MTU,
debug: false,
os: "MAC".to_string(),
}),
};
log::info!(
"Trying a direct connect to: {:?} with tid: {}",
addr,
msg.tid
);
let (camera_address, camera_id) = self
.retry_send(msg, addr, |bc, addr| match bc {
UdpDiscovery {
tid: _,
payload:
UdpXml {
d2c_c_r: Some(D2cCr { did, cid, .. }),
..
},
} if cid == client_id => Some((addr, did)),
_ => None,
payload: UdpXml::D2cCr(D2cCr { did, cid, .. }),
} if cid == client_id => {
log::info!("Got expected reply");
Some((addr, did))
}
n => {
log::info!("Got unexpected reply: {:?}", n);
None
}
})
.await?;
log::info!(
"Direct connect success at {:?} client: {}, device: {}",
addr,
client_id,
camera_id
);
let result = ConnectResult {
addr: camera_address,
client_id,
@@ -385,6 +399,7 @@ impl Discoverer {
};
self.keep_alive_device(tid, &result).await;
log::info!("Returning direct connect: {:?}", result);
Ok(result)
}
@@ -422,32 +437,30 @@ impl Discoverer {
async fn uid_lookup(&self, uid: &str, addr: SocketAddr) -> Result<UidLookupResults> {
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
c2m_q: Some(C2mQ {
uid: uid.to_string(),
os: "MAC".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2mQ(C2mQ {
uid: uid.to_string(),
os: "MAC".to_string(),
}),
};
trace!("Sending look up {:?}", msg);
let (packet, _) = self
let (reg, relay, _) = self
.retry_send(msg, addr, |bc, addr| match bc {
UdpDiscovery {
tid: _,
payload:
UdpXml {
m2c_q_r: Some(m2c_q_r),
UdpXml::M2cQr(M2cQr {
reg: Some(reg),
relay: Some(relay),
..
},
} if valid_addr(&m2c_q_r.reg) => Some((m2c_q_r, addr)),
}),
} if valid_addr(&reg) && valid_addr(&relay) => Some((reg, relay, addr)),
_ => 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),
reg: SocketAddr::new(reg.ip.parse()?, reg.port),
relay: SocketAddr::new(relay.ip.parse()?, relay.port),
})
}
@@ -469,28 +482,27 @@ impl Discoverer {
let msg = UdpDiscovery {
tid,
payload: UdpXml {
c2r_c: Some(C2rC {
uid: uid.to_string(),
cli: IpPort {
ip: local_ip.to_string(),
port: local_port,
},
relay: IpPort {
ip: lookup.relay.ip().to_string(),
port: lookup.relay.port(),
},
cid: client_id,
family: local_family,
debug: false,
os: "MAC".to_string(),
revision: Some(3),
}),
..Default::default()
},
payload: UdpXml::C2rC(C2rC {
uid: uid.to_string(),
cli: IpPort {
ip: local_ip.to_string(),
port: local_port,
},
relay: IpPort {
ip: lookup.relay.ip().to_string(),
port: lookup.relay.port(),
},
cid: client_id,
family: local_family,
debug: false,
os: "MAC".to_string(),
revision: Some(3),
}),
};
trace!("Registering: {:?}", msg.payload.c2r_c);
if let UdpXml::C2rC(ref c2r_c) = msg.payload {
trace!("Registering: {:?}", c2r_c);
}
// Send and await acceptance
let (sid, dev, dmap, relay) = self
@@ -500,22 +512,19 @@ impl Discoverer {
UdpDiscovery {
tid: _,
payload:
UdpXml {
r2c_c_r:
Some(R2cCr {
dmap,
dev,
relay,
sid,
rsp,
..
}),
UdpXml::R2cCr(R2cCr {
dmap,
dev,
relay,
sid: Some(sid),
rsp,
..
},
}),
} if (dev.as_ref().map(valid_addr).unwrap_or(false)
|| dmap.as_ref().map(valid_addr).unwrap_or(false)
|| relay.as_ref().map(valid_addr).unwrap_or(false))
&& rsp != -1 =>
&& rsp != -1
&& rsp != -3 =>
{
Some(Ok((sid, dev, dmap, relay)))
}
@@ -553,21 +562,17 @@ impl Discoverer {
UdpDiscovery {
tid: _,
payload:
UdpXml {
r2c_c_r:
Some(R2cCr {
dev,
dmap,
relay,
rsp,
..
}),
UdpXml::R2cCr(R2cCr {
dev,
dmap,
relay,
rsp,
..
},
}),
} if (dev.as_ref().map(valid_addr).unwrap_or(false)
|| dmap.as_ref().map(valid_addr).unwrap_or(false)
|| relay.as_ref().map(valid_addr).unwrap_or(false))
&& rsp == -1 =>
&& (rsp == -1 || rsp == -3) =>
{
Some(Err(Error::RegisterError))
}
@@ -598,17 +603,13 @@ impl Discoverer {
UdpDiscovery {
tid,
payload:
UdpXml {
d2c_t:
Some(D2cT {
sid,
cid,
did,
conn,
..
}),
UdpXml::D2cT(D2cT {
sid,
cid,
did,
conn,
..
},
}),
},
RegisterResult {
dmap: register_dmap,
@@ -632,16 +633,13 @@ impl Discoverer {
let msg = UdpDiscovery {
tid: local_tid,
payload: UdpXml {
c2d_a: Some(C2dA {
sid: register_result.sid,
conn: "local".to_string(),
cid: register_result.client_id,
did: local_did,
mtu: MTU,
}),
..Default::default()
},
payload: UdpXml::C2dA(C2dA {
sid: register_result.sid,
conn: "local".to_string(),
cid: register_result.client_id,
did: local_did,
mtu: MTU,
}),
};
let permit = self
@@ -657,17 +655,13 @@ impl Discoverer {
UdpDiscovery {
tid: _,
payload:
UdpXml {
d2c_cfm:
Some(D2cCfm {
sid,
cid,
did,
conn,
..
}),
UdpXml::D2cCfm(D2cCfm {
sid,
cid,
did,
conn,
..
},
}),
} if sid == register_result.sid
&& did == local_did
&& cid == register_result.client_id
@@ -690,16 +684,13 @@ impl Discoverer {
// Confirm local to register
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
c2r_cfm: Some(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
rsp: 0,
conn: "local".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2rCfm(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
rsp: 0,
conn: "local".to_string(),
}),
};
self.send_and_forget(msg, register_result.reg).await?;
@@ -723,17 +714,13 @@ impl Discoverer {
UdpDiscovery {
tid,
payload:
UdpXml {
d2c_t:
Some(D2cT {
sid,
cid,
did,
conn,
..
}),
UdpXml::D2cT(D2cT {
sid,
cid,
did,
conn,
..
},
}),
},
RegisterResult {
dmap: register_dmap,
@@ -757,16 +744,13 @@ impl Discoverer {
let msg = UdpDiscovery {
tid: local_tid,
payload: UdpXml {
c2d_a: Some(C2dA {
sid: register_result.sid,
conn: "map".to_string(),
cid: register_result.client_id,
did: local_did,
mtu: MTU,
}),
..Default::default()
},
payload: UdpXml::C2dA(C2dA {
sid: register_result.sid,
conn: "map".to_string(),
cid: register_result.client_id,
did: local_did,
mtu: MTU,
}),
};
let permit = self
@@ -782,17 +766,13 @@ impl Discoverer {
UdpDiscovery {
tid: _,
payload:
UdpXml {
d2c_cfm:
Some(D2cCfm {
sid,
cid,
did,
conn,
..
}),
UdpXml::D2cCfm(D2cCfm {
sid,
cid,
did,
conn,
..
},
}),
} if sid == register_result.sid
&& did == local_did
&& cid == register_result.client_id
@@ -815,16 +795,13 @@ impl Discoverer {
// Confirm map to register
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
c2r_cfm: Some(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
rsp: 0,
conn: "map".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2rCfm(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
rsp: 0,
conn: "map".to_string(),
}),
};
self.send_and_forget(msg, register_result.reg).await?;
@@ -845,26 +822,19 @@ impl Discoverer {
let dev_addr = register_result.dev.ok_or(Error::NoDev)?;
let msg = UdpDiscovery {
tid,
payload: UdpXml {
c2d_t: Some(C2dT {
sid: register_result.sid,
cid: register_result.client_id,
mtu: MTU,
conn: "local".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2dT(C2dT {
sid: register_result.sid,
cid: register_result.client_id,
mtu: MTU,
conn: "local".to_string(),
}),
};
let (final_addr, local_did) = self
.retry_send(msg, dev_addr, |bc, addr| match bc {
UdpDiscovery {
tid: _,
payload:
UdpXml {
d2c_cfm: Some(D2cCfm { cid, did, sid, .. }),
..
},
payload: UdpXml::D2cCfm(D2cCfm { cid, did, sid, .. }),
} if cid == register_result.client_id && sid == register_result.sid => {
Some((addr, did))
}
@@ -888,16 +858,13 @@ impl Discoverer {
// Confirm local to register
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
c2r_cfm: Some(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
conn: "local".to_string(),
rsp: 0,
}),
..Default::default()
},
payload: UdpXml::C2rCfm(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
conn: "local".to_string(),
rsp: 0,
}),
};
self.send_and_forget(msg, register_result.reg).await?;
@@ -918,15 +885,12 @@ impl Discoverer {
let relay_addr = register_result.relay.ok_or(Error::NoDev)?;
let msg = UdpDiscovery {
tid,
payload: UdpXml {
c2d_t: Some(C2dT {
sid: register_result.sid,
cid: register_result.client_id,
mtu: MTU,
conn: "relay".to_string(),
}),
..Default::default()
},
payload: UdpXml::C2dT(C2dT {
sid: register_result.sid,
cid: register_result.client_id,
mtu: MTU,
conn: "relay".to_string(),
}),
};
let permit = self
@@ -940,17 +904,13 @@ impl Discoverer {
UdpDiscovery {
tid: _,
payload:
UdpXml {
d2c_cfm:
Some(D2cCfm {
cid,
did,
sid,
conn,
..
}),
UdpXml::D2cCfm(D2cCfm {
cid,
did,
sid,
conn,
..
},
}),
} if cid == register_result.client_id
&& sid == register_result.sid
&& &conn == "relay" =>
@@ -971,16 +931,13 @@ impl Discoverer {
// Confirm relay to register
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
c2r_cfm: Some(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
conn: "relay".to_string(),
rsp: 0,
}),
..Default::default()
},
payload: UdpXml::C2rCfm(C2rCfm {
sid: result.sid,
cid: result.client_id,
did: result.camera_id,
conn: "relay".to_string(),
rsp: 0,
}),
};
self.send_and_forget(msg, register_result.reg).await?;
@@ -1009,13 +966,10 @@ impl Discoverer {
interval.tick().await;
let msg = BcUdp::Discovery(UdpDiscovery {
tid,
payload: UdpXml {
c2d_hb: Some(C2dHb {
payload: UdpXml::C2dHb(C2dHb {
cid: client_id,
did: camera_id,
}),
..Default::default()
},
});
if sender.send((msg, addr)).await.is_err() {
break Result::Ok(());
@@ -1044,14 +998,11 @@ impl Discoverer {
interval.tick().await;
let msg = BcUdp::Discovery(UdpDiscovery {
tid,
payload: UdpXml {
c2r_hb: Some(C2rHb {
payload: UdpXml::C2rHb(C2rHb {
sid,
cid: client_id,
did: camera_id,
}),
..Default::default()
},
});
if sender.send((msg, addr)).await.is_err() {
break Result::Ok(());
@@ -1167,7 +1118,7 @@ impl Discovery {
) -> Result<DiscoveryResult> {
let mut dests = get_broadcasts(&[2015, 2018])?;
if let Some(mut optional_addrs) = optional_addrs.take() {
trace!("Also sending to {:?}", optional_addrs);
debug!("Also sending to {:?}", optional_addrs);
dests.append(&mut optional_addrs);
}
let discoverer_ref = &self.discoverer;
@@ -371,7 +371,6 @@ impl UdpPayloadInner {
// Socket is (maybe) broken
// Seems to happen with network reconnects like over
// a lossy cellular network
log::debug!("Quick reconnect: Due to socket timeout");
let stream = Arc::new(tokio::time::timeout(tokio::time::Duration::from_millis(250), connect_try_port(inner.inner.get_ref().local_addr()?.port())).await.map_err(|_| Error::DroppedConnection)??);
inner = tokio::time::timeout(tokio::time::Duration::from_millis(250), BcUdpSource::new_from_socket(stream, inner.addr)).await.map_err(|_| Error::DroppedConnection)??;
@@ -384,13 +383,10 @@ impl UdpPayloadInner {
let mut rng = thread_rng();
(rng.gen::<u8>()) as u32
},
payload: UdpXml {
c2d_hb: Some(C2dHb {
payload: UdpXml::C2dHb(C2dHb {
cid: thread_client_id,
did: thread_camera_id,
}),
..Default::default()
},
});
let _ = tokio::time::timeout(tokio::time::Duration::from_millis(250), inner.send((msg, thread_camera_addr))).await;
}
@@ -404,7 +400,6 @@ impl UdpPayloadInner {
Ok(())
} => v,
};
log::debug!("UdpPayloadInner::new SendToSocket Cancel: {:?}", result);
send_cancel.cancel();
result
});
@@ -449,13 +444,10 @@ impl UdpPayloadInner {
let mut rng = thread_rng();
(rng.gen::<u8>()) as u32
},
payload: UdpXml {
c2d_hb: Some(C2dHb {
payload: UdpXml::C2dHb(C2dHb {
cid: thread_client_id,
did: thread_camera_id,
}),
..Default::default()
},
});
if thread_sender.send(msg).await.is_err() {
break Result::Ok(());
@@ -526,6 +518,20 @@ impl UdpPayloadInner {
let (item, addr) = v.ok_or(Error::DroppedConnection)?;
if addr == camera_addr {
match item {
BcUdp::Discovery(UdpDiscovery{
payload: UdpXml::D2cHb(D2cHb {
cid,
did,
}),
..
}) => {
if cid != self.client_id {
log::info!("Camera sent different client ID in HB");
}
if did != self.camera_id {
log::info!("Camera sent different device ID in HB");
}
},
BcUdp::Discovery(_disc) => {},
BcUdp::Ack(ack) => {
if ack.connection_id == self.client_id {
@@ -554,6 +560,7 @@ impl UdpPayloadInner {
self.packets_want += 1;
self.thread_stream.feed(Ok(payload)).await?;
}
log::info!("recieved: {}", self.recieved.len());
log::trace!("Flush");
self.socket_in.flush().await?;
self.thread_stream.flush().await?;
@@ -611,6 +618,7 @@ impl UdpPayloadInner {
}
}
self.ack_latency.feed_ack();
log::info!("sent: {}", self.sent.len());
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ use std::convert::TryInto;
pub struct Credentials {
/// The username to login to the camera with
pub username: String,
/// The password to use for login. Some camera allow this to be ommited
/// The password to use for login. Some camera allow this to be omitted
pub password: Option<String>,
}
+64 -60
View File
@@ -1,33 +1,33 @@
use super::bc::model::Bc;
use super::bc::model::{Bc, BcXml};
use crate::NomErrorType;
use err_derive::Error;
use thiserror::Error;
/// This is the primary error type of the library
#[derive(Debug, Error, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum Error {
/// Underlying IO errors
#[error(display = "IO Error: {:?}", _0)]
Io(#[error(source)] std::sync::Arc<std::io::Error>),
#[error("IO Error: {:?}", _0)]
Io(#[from] std::sync::Arc<std::io::Error>),
/// Raised when fails to parse time from the camera
#[error(display = "Error in time coversion: {:?}", _0)]
TimeRange(#[error(source)] time::error::ComponentRange),
#[error("Error in time coversion: {:?}", _0)]
TimeRange(#[from] time::error::ComponentRange),
/// Raised when fails to parse time from the camera
#[error(display = "Error in time parsing")]
#[error("Error in time parsing")]
TimeParse,
/// Raised when fails to parse time from the camera
#[error(display = "Error in try from NonZeroInt")]
TryFromInt(#[error(source)] std::num::TryFromIntError),
#[error("Error in try from NonZeroInt")]
TryFromInt(#[from] std::num::TryFromIntError),
/// /// Raised when fails to parse time from the camera
#[error(display = "Error in time conversion")]
TimeTryFrom(#[error(source)] time::error::TryFromParsed),
#[error("Error in time conversion")]
TimeTryFrom(#[from] time::error::TryFromParsed),
/// Raised when a Bc reply was not understood
#[error(display = "Communication error")]
#[error("Communication error")]
UnintelligibleReply {
/// The Bc packet that was not understood
reply: std::sync::Arc<Box<Bc>>,
@@ -35,141 +35,145 @@ pub enum Error {
why: &'static str,
},
/// Raised when a BcXml reply was not understood
#[error("Communication error")]
UnintelligibleXml {
/// The Bc packet that was not understood
reply: std::sync::Arc<Box<BcXml>>,
/// The message attached to the error
why: &'static str,
},
/// Raised when the camera responds with a status code over than OK
#[error(display = "Camera responded with Service Unavaliable: {}", _0)]
CameraServiceUnavaliable(u16),
#[error("Camera responded with Service Unavaliable: {0}")]
CameraServiceUnavailable(u16),
/// Raised when the camera responds with a status code over than OK during login
#[error(display = "Camera responded with Err during login")]
#[error("Camera responded with Err during login")]
CameraLoginFail,
/// Raised when a connection is dropped.
#[error(display = "Dropped connection")]
#[error("Dropped connection")]
DroppedConnection,
/// Raised when a connection is dropped during a tokio mpsc TryRecv event
#[error(display = "Dropped connection (TryRecv)")]
DroppedConnectionTry(#[error(source)] tokio::sync::mpsc::error::TryRecvError),
#[error("Dropped connection (TryRecv)")]
DroppedConnectionTry(#[from] tokio::sync::mpsc::error::TryRecvError),
/// Raised when a connection is dropped during a TryRecv event
#[error(display = "Dropped connection (Broadcast TryRecv)")]
BroadcastDroppedConnectionTry(#[error(source)] tokio::sync::broadcast::error::TryRecvError),
#[error("Dropped connection (Broadcast TryRecv)")]
BroadcastDroppedConnectionTry(#[from] tokio::sync::broadcast::error::TryRecvError),
/// Raised when a connection is dropped during a TryRecv event
#[error(display = "Send Error")]
#[error("Send Error")]
TokioBcSendError,
/// Raised when the TIMEOUT is reach
#[error(display = "Timeout")]
Timeout(#[error(source)] std::sync::Arc<tokio::time::error::Elapsed>),
#[error("Timeout")]
Timeout(#[from] std::sync::Arc<tokio::time::error::Elapsed>),
/// Raised when a timeout fails in a non standard way such as timeout during shutdown
#[error(display = "TimeoutError")]
TimeoutError(#[error(source)] tokio::time::error::Error),
#[error("TimeoutError")]
TimeoutError(#[from] tokio::time::error::Error),
/// Raised when connection is dropped because the timeout is reach
#[error(display = "Dropped connection (Timeout)")]
#[error("Dropped connection (Timeout)")]
TimeoutDisconnected,
/// Raised when a camera cannot be connected to ay any of the given addresses
#[error(display = "Cannot contact camera at given address")]
#[error("Cannot contact camera at given address")]
CannotInitCamera,
/// Raised when failed to login to the camera
#[error(display = "Credential error")]
#[error("Credential error")]
AuthFailed,
/// Raised when the given camera url could not be resolved
#[error(display = "Failed to translate camera address")]
#[error("Failed to translate camera address")]
AddrResolutionError,
/// Raised non adpcm data is sent to the talk command
#[error(display = "Talk data is not ADPCM")]
#[error("Talk data is not ADPCM")]
UnknownTalkEncoding,
/// Raised when dicovery times out waiting for a reply
#[error(display = "Timed out while waiting for camera reply")]
#[error("Timed out while waiting for camera reply")]
DiscoveryTimeout,
/// Raised during a (de)seralisation error
#[error(display = "Cookie GenError")]
GenError(#[error(source)] std::sync::Arc<cookie_factory::GenError>),
#[error("Cookie GenError")]
GenError(#[from] std::sync::Arc<cookie_factory::GenError>),
/// Raised when a connection is subscrbed to more than once for msg_num
#[error(display = "Simultaneous subscription, {:?}", _0)]
#[error("Simultaneous subscription, {msg_num:?}")]
SimultaneousSubscription {
/// The message number that was subscribed to
msg_num: Option<u16>,
},
/// Raised when a connection is subscrbed to more than once for msg_id
#[error(display = "Simultaneous subscription, {}", _0)]
#[error("Simultaneous subscription, {msg_id}")]
SimultaneousSubscriptionId {
/// The message number that was subscribed to
msg_id: u32,
},
/// Raised when a new encyrption byte is observed
#[error(display = "Unknown encryption: {:x?}", _0)]
#[error("Unknown encryption: {0:x?}")]
UnknownEncryption(usize),
/// Raised when the camera cannot be found
#[error(display = "Camera Not Findable")]
ConnectionUnavaliable,
#[error("Camera Not Findable")]
ConnectionUnavailable,
/// Raised when the subscription id dropped too soon
#[error(display = "Dropped Subscriber")]
#[error("Dropped Subscriber")]
DroppedSubscriber,
/// Raised when a unknown connection ID attempts to connect with us over UDP
#[error(display = "Connection with unknown connectionID: {:?}", _0)]
#[error("Connection with unknown connectionID: {0:?}")]
UnknownConnectionId(i32),
/// Raised when a unknown SocketAddr attempts to connect with us over UDP
#[error(display = "Connection from unknown source: {:?}", _0)]
#[error("Connection from unknown source: {0:?}")]
UnknownSource(std::net::SocketAddr),
/// Raised when the IP/Hostname cannot be understood
#[error(display = "Could not parse as IP")]
AddrParseError(#[error(source)] std::net::AddrParseError),
#[error("Could not parse as IP")]
AddrParseError(#[from] std::net::AddrParseError),
/// Raised when a relay connection is not possible
/// usually happens if the camera has not contacted reolink yet
#[error(display = "Cannot perform relay connection with this camera")]
#[error("Cannot perform relay connection with this camera")]
NoDmap,
/// Raised when a dev connection is not possible
/// usually happens if the camera has not contacted reolink yet
#[error(display = "Cannot perform lookup with this camera against reolink servers")]
#[error("Cannot perform lookup with this camera against reolink servers")]
NoDev,
/// Raised when a discovery fails to be accepted by the register
#[error(display = "Register refuses to accept us")]
#[error("Register refuses to accept us")]
RegisterError,
/// Raised when a the relay terminates the connection by sending a R2C_DISC
#[error(display = "Relay terminated the connection")]
#[error("Relay terminated the connection")]
RelayTerminate,
/// Raised when a the camera terminates the connection by sending a D2C_DISC
#[error(display = "Camera terminated the connection")]
#[error("Camera terminated the connection")]
CameraTerminate,
/// Raised when the stream is not enough to complete a message
#[error(display = "Nom Parsing incomplete: {}", _0)]
#[error("Nom Parsing incomplete: {0}")]
NomIncomplete(usize),
/// Raised when a stream cannot be decoded
#[error(display = "Nom Parsing error: {}", _0)]
#[error("Nom Parsing error: {0}")]
NomError(String),
/// Raised when a camera/user lacks an ability
#[error(
display = "Missing ability: {} with {} permission has only {}",
name,
requested,
actual
)]
#[error("Missing ability: {name} with {requested} permission has only {actual}")]
MissingAbility {
/// Name of the ability
name: String,
@@ -180,15 +184,15 @@ pub enum Error {
},
/// Raised when a thread panics
#[error(display = "Thread panicked")]
JoinError(#[error(source)] std::sync::Arc<tokio::task::JoinError>),
#[error("Thread panicked")]
JoinError(#[from] std::sync::Arc<tokio::task::JoinError>),
/// A generic catch all error
#[error(display = "Other error: {}", _0)]
#[error("Other error: {0}")]
Other(&'static str),
/// A generic catch all error
#[error(display = "Other error: {}", _0)]
#[error("Other error: {0}")]
OtherString(String),
}
+3 -3
View File
@@ -81,7 +81,7 @@ impl BcCamera {
sub_set.send(get).await?;
if let Ok(reply) =
tokio::time::timeout(tokio::time::Duration::from_micros(500), sub_set.recv()).await
tokio::time::timeout(tokio::time::Duration::from_millis(500), sub_set.recv()).await
{
let msg = reply?;
if let BcMeta {
@@ -129,7 +129,7 @@ impl BcCamera {
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcBody::ModernMsg(ModernMsg {
@@ -181,7 +181,7 @@ impl BcCamera {
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
Ok(())
+1 -1
View File
@@ -2,7 +2,7 @@ use super::{BcCamera, Result};
use crate::bc::model::*;
impl BcCamera {
/// Create a handller to respond to keep alive messages
/// Create a handler to respond to keep alive messages
/// These messages are sent by the camera so we listen to
/// a message ID rather than setting a message number and
/// responding to it
+2 -2
View File
@@ -29,7 +29,7 @@ impl BcCamera {
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcBody::ModernMsg(ModernMsg {
@@ -84,7 +84,7 @@ impl BcCamera {
sub_set.send(get).await?;
if let Ok(reply) =
tokio::time::timeout(tokio::time::Duration::from_micros(500), sub_set.recv()).await
tokio::time::timeout(tokio::time::Duration::from_millis(500), sub_set.recv()).await
{
let msg = reply?;
+1 -1
View File
@@ -27,7 +27,7 @@ impl BcCamera {
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcBody::ModernMsg(ModernMsg {
+4 -4
View File
@@ -39,10 +39,10 @@ impl BcCamera {
reties += 1;
continue;
} else {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
} else if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
} else {
// Valid message with response_code == 200
if let BcBody::ModernMsg(ModernMsg {
@@ -97,11 +97,11 @@ impl BcCamera {
sub_set.send(get).await?;
if let Ok(reply) =
tokio::time::timeout(tokio::time::Duration::from_micros(500), sub_set.recv()).await
tokio::time::timeout(tokio::time::Duration::from_millis(500), sub_set.recv()).await
{
let msg = reply?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcMeta {
+1 -9
View File
@@ -239,16 +239,8 @@ impl BcCamera {
/// The camera will zoom to a given zoom amount.
/// Not sure what the units for this are, seems to be 1000 is 1x and 2000 is 2x
pub async fn zoom_to(&self, zoom_pos: u32) -> Result<()> {
log::debug!("Setting to {}", zoom_pos);
let current = self.get_zoom().await?;
log::debug!(
" curr: {}, min: {}, max: {}",
current.zoom.cur_pos,
current.zoom.min_pos,
current.zoom.max_pos
);
let zoom_pos = zoom_pos.clamp(current.zoom.min_pos, current.zoom.max_pos);
log::debug!("Clamped to {}", zoom_pos);
self.has_ability_rw("control").await?;
let connection = self.get_connection();
@@ -325,7 +317,7 @@ impl BcCamera {
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcBody::ModernMsg(ModernMsg {
+1 -1
View File
@@ -64,7 +64,7 @@ impl BcCamera {
sub.send(msg).await?;
let msg = sub.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
Ok(())
@@ -1,6 +1,5 @@
//! This is a helper module to resolve either to a UID or a SockerAddr
use log::*;
use serde::{Deserialize, Serialize};
use std::{
io::Error,
@@ -89,7 +88,6 @@ impl ToSocketAddrsOrUid for str {
.collect::<Vec<_>>()
.into_iter()),
Err(e) => {
debug!("Trying as uid");
let re = regex::Regex::new(r"^[0-9A-Za-z]+$").unwrap();
if re.is_match(self) {
Ok(vec![SocketAddrOrUid::Uid(
@@ -99,7 +97,6 @@ impl ToSocketAddrsOrUid for str {
)]
.into_iter())
} else {
debug!("Regex fails {:?} => {:?} ", re, self);
Err(e)
}
}
@@ -117,7 +114,6 @@ impl ToSocketAddrsOrUid for String {
.collect::<Vec<_>>()
.into_iter()),
Err(e) => {
debug!("Trying as uid");
let re = regex::Regex::new(r"^[0-9A-Za-z]+$").unwrap();
if re.is_match(self) {
Ok(vec![SocketAddrOrUid::Uid(
@@ -127,7 +123,6 @@ impl ToSocketAddrsOrUid for String {
)]
.into_iter())
} else {
debug!("Regex fails {:?} => {:?} ", re, self);
Err(e)
}
}
+414
View File
@@ -0,0 +1,414 @@
use super::{BcCamera, Error, Result};
use crate::bc::{model::*, xml::*};
use tokio::time::{interval, Duration};
impl BcCamera {
/// Helper to set the service state since they all share the same code
/// No checks are made to ensure the xml is valid service xml
/// hence private method
async fn set_services(&self, bcxml: BcXml) -> Result<()> {
let connection = self.get_connection();
let msg_num = self.new_message_num();
let mut sub_set = connection
.subscribe(MSG_ID_SET_SERVICE_PORTS, msg_num)
.await?;
let get = Bc {
meta: BcMeta {
msg_id: MSG_ID_SET_SERVICE_PORTS,
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)),
}),
};
sub_set.send(get).await?;
if let Ok(reply) =
tokio::time::timeout(tokio::time::Duration::from_millis(500), sub_set.recv()).await
{
let msg = reply?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcMeta {
response_code: 200, ..
} = msg.meta
{
Ok(())
} else {
Err(Error::UnintelligibleReply {
reply: std::sync::Arc::new(Box::new(msg)),
why: "The camera did not except the BcXmp with service data",
})
}
} else {
// Some cameras seem to just not send a reply on success, so after 500ms we return Ok
Ok(())
}
}
/// Helper since they all send the same message
/// No checks are made to ensure the xml is valid service xml
/// hence private method
async fn get_services(&self) -> Result<BcXml> {
let connection = self.get_connection();
let mut reties: usize = 0;
let mut retry_interval = interval(Duration::from_millis(500));
loop {
retry_interval.tick().await;
let msg_num = self.new_message_num();
let mut sub_get = connection
.subscribe(MSG_ID_GET_SERVICE_PORTS, msg_num)
.await?;
let get = Bc {
meta: BcMeta {
msg_id: MSG_ID_GET_SERVICE_PORTS,
channel_id: self.channel_id,
msg_num,
response_code: 0,
stream_type: 0,
class: 0x6414,
},
body: BcBody::ModernMsg(ModernMsg {
extension: None,
payload: None,
}),
};
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code == 400 {
// Retryable
if reties < 5 {
reties += 1;
continue;
} else {
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
} else if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
} else {
// Valid message with response_code == 200
if let BcBody::ModernMsg(ModernMsg {
payload: Some(BcPayloads::BcXml(xml)),
..
}) = msg.body
{
return Ok(xml);
} else {
return Err(Error::UnintelligibleReply {
reply: std::sync::Arc::new(Box::new(msg)),
why: "Expected ModernMsg payload but it was not recieved",
});
}
}
}
}
/// Get the [`ServerPort`] XML
pub async fn get_serverport(&self) -> Result<ServerPort> {
let bcxml = self.get_services().await?;
if let BcXml {
server_port: Some(xml),
..
} = bcxml
{
Ok(xml)
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected ServerPort xml but it was not recieved",
})
}
}
/// Set the server port
pub async fn set_serverport(&self, set_on: Option<bool>, set_port: Option<u32>) -> Result<()> {
let bcxml = self.get_services().await?;
if let BcXml {
server_port: Some(mut xml),
..
} = bcxml
{
if let Some(enabled) = set_on {
xml.enable = Some({
if enabled {
1
} else {
0
}
});
}
if let Some(port) = set_port {
xml.port = port;
}
self.set_services(BcXml {
server_port: Some(xml),
..Default::default()
})
.await
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected ServerPort xml but it was not recieved",
})
}
}
/// Get the [`HttpPort`] XML
pub async fn get_http(&self) -> Result<HttpPort> {
let bcxml = self.get_services().await?;
if let BcXml {
http_port: Some(xml),
..
} = bcxml
{
Ok(xml)
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected HttpPort xml but it was not recieved",
})
}
}
/// Set the http port
pub async fn set_http(&self, set_on: Option<bool>, set_port: Option<u32>) -> Result<()> {
let bcxml = self.get_services().await?;
if let BcXml {
http_port: Some(mut xml),
..
} = bcxml
{
if let Some(enabled) = set_on {
xml.enable = Some({
if enabled {
1
} else {
0
}
});
}
if let Some(port) = set_port {
xml.port = port;
}
self.set_services(BcXml {
http_port: Some(xml),
..Default::default()
})
.await
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected HttpPort xml but it was not recieved",
})
}
}
/// Get the [`HttpPort`] XML
pub async fn get_https(&self) -> Result<HttpsPort> {
let bcxml = self.get_services().await?;
if let BcXml {
https_port: Some(xml),
..
} = bcxml
{
Ok(xml)
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected HttpsPort xml but it was not recieved",
})
}
}
/// Set the https port
pub async fn set_https(&self, set_on: Option<bool>, set_port: Option<u32>) -> Result<()> {
let bcxml = self.get_services().await?;
if let BcXml {
https_port: Some(mut xml),
..
} = bcxml
{
if let Some(enabled) = set_on {
xml.enable = Some({
if enabled {
1
} else {
0
}
});
}
if let Some(port) = set_port {
xml.port = port;
}
self.set_services(BcXml {
https_port: Some(xml),
..Default::default()
})
.await
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected HttpsPort xml but it was not recieved",
})
}
}
/// Get the [`RtspPort`] XML
pub async fn get_rtsp(&self) -> Result<RtspPort> {
let bcxml = self.get_services().await?;
if let BcXml {
rtsp_port: Some(xml),
..
} = bcxml
{
Ok(xml)
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected RtspPort xml but it was not recieved",
})
}
}
/// Set the http port
pub async fn set_rtsp(&self, set_on: Option<bool>, set_port: Option<u32>) -> Result<()> {
let bcxml = self.get_services().await?;
if let BcXml {
rtsp_port: Some(mut xml),
..
} = bcxml
{
if let Some(enabled) = set_on {
xml.enable = Some({
if enabled {
1
} else {
0
}
});
}
if let Some(port) = set_port {
xml.port = port;
}
self.set_services(BcXml {
rtsp_port: Some(xml),
..Default::default()
})
.await
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected RtspPort xml but it was not recieved",
})
}
}
/// Get the [`RtmpPort`] XML
pub async fn get_rtmp(&self) -> Result<RtmpPort> {
let bcxml = self.get_services().await?;
if let BcXml {
rtmp_port: Some(xml),
..
} = bcxml
{
Ok(xml)
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected RtmpPort xml but it was not recieved",
})
}
}
/// Set the rtmp port
pub async fn set_rtmp(&self, set_on: Option<bool>, set_port: Option<u32>) -> Result<()> {
let bcxml = self.get_services().await?;
if let BcXml {
rtmp_port: Some(mut xml),
..
} = bcxml
{
if let Some(enabled) = set_on {
xml.enable = Some({
if enabled {
1
} else {
0
}
});
}
if let Some(port) = set_port {
xml.port = port;
}
self.set_services(BcXml {
rtmp_port: Some(xml),
..Default::default()
})
.await
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected RtmpPort xml but it was not recieved",
})
}
}
/// Get the [`OnvifPort`] XML
pub async fn get_onvif(&self) -> Result<OnvifPort> {
let bcxml = self.get_services().await?;
if let BcXml {
onvif_port: Some(xml),
..
} = bcxml
{
Ok(xml)
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected OnvifPort xml but it was not recieved",
})
}
}
/// Set the onvif port
pub async fn set_onvif(&self, set_on: Option<bool>, set_port: Option<u32>) -> Result<()> {
let bcxml = self.get_services().await?;
if let BcXml {
onvif_port: Some(mut xml),
..
} = bcxml
{
if let Some(enabled) = set_on {
xml.enable = Some({
if enabled {
1
} else {
0
}
});
}
if let Some(port) = set_port {
xml.port = port;
}
self.set_services(BcXml {
onvif_port: Some(xml),
..Default::default()
})
.await
} else {
Err(Error::UnintelligibleXml {
reply: std::sync::Arc::new(Box::new(bcxml)),
why: "Expected OnvifPort xml but it was not recieved",
})
}
}
}
+1 -1
View File
@@ -39,7 +39,7 @@ impl BcCamera {
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
Ok(())
+2 -2
View File
@@ -41,7 +41,7 @@ impl BcCamera {
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcBody::ModernMsg(ModernMsg {
@@ -132,7 +132,7 @@ impl BcCamera {
}
} else {
// anything else is an error
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
// let binary_stream = sub_get.payload_stream();
+6 -22
View File
@@ -17,7 +17,7 @@ pub enum StreamKind {
Sub,
/// This stream represents a balance between SD and HD
///
/// It is only avaliable on some camera. If the camera dosen't
/// It is only available on some camera. If the camera doesn't
/// support it the stream will be the same as the SD stream
Extern,
}
@@ -49,25 +49,17 @@ impl StreamData {
pub async fn get_data(&mut self) -> Result<Result<BcMedia>> {
if let Some(handle) = self.handle.as_mut() {
if handle.is_finished() {
log::debug!("SteamData::get_data Cancel1");
self.abort_handle.cancel();
handle.await??;
return Err(Error::DroppedConnection);
}
} else {
log::debug!("SteamData::get_data Cancel2");
self.abort_handle.cancel();
return Err(Error::DroppedConnection);
}
// debug!("StreamData: Get");
match self.rx.recv().await {
Some(data) => {
// debug!("StreamData: Got");
Ok(data)
}
Some(data) => Ok(data),
None => {
// debug!("StreamData: Drop");
log::debug!("SteamData::get_data Cancel3");
self.abort_handle.cancel();
Err(Error::DroppedConnection)
}
@@ -77,7 +69,6 @@ impl StreamData {
/// Attempts to gracefully shutdown this will cancel the background task and send
/// the Stop command to the camera
pub async fn shutdown(&mut self) -> Result<()> {
log::debug!("SteamData::shutdown Cancel");
self.abort_handle.cancel();
if let Some(handle) = self.handle.take() {
let _ = handle.await?;
@@ -155,7 +146,7 @@ impl BcCamera {
StreamKind::Extern => 0,
};
// Theses are the numbers used with the offical client
// Theses are the numbers used with the official client
// On an E1 and swann cameras:
// - mainStream always has a value of 0
// - subStream always has a value of 1
@@ -218,20 +209,15 @@ impl BcCamera {
_ = abort_handle_thread.cancelled() => {},
_ = async {
while let Some(bc_media) = media_sub.next().await {
// debug!("Stream: Got");
// We now have a complete interesting packet. Send it to on the callback
// debug!("Stream: Send");
if tx.send(bc_media).await.is_err() {
// debug!("Stream: Dropped");
break; // Connection dropped
}
// debug!("Stream: Sent");
}
} => {}
}
}
// debug!("Stream: Stopping");
let stop_video = Bc::new_from_xml(
BcMeta {
msg_id: MSG_ID_VIDEO_STOP,
@@ -251,10 +237,8 @@ impl BcCamera {
..Default::default()
},
);
// debug!("Stream: Send Stop");
let mut sub_stop = connection.subscribe(MSG_ID_VIDEO_STOP, msg_num).await?;
sub_stop.send(stop_video).await?;
// debug!("Stream: Sent Stop");
tokio::select! {
v = async {
@@ -271,7 +255,7 @@ impl BcCamera {
msg_id: MSG_ID_VIDEO_STOP,
..
} = msg.meta {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
}
} => v,
@@ -313,7 +297,7 @@ impl BcCamera {
StreamKind::Extern => 0,
};
// Theses are the numbers used with the offical client
// Theses are the numbers used with the official client
// On an E1 and swann cameras:
// - mainStream always has a value of 0
// - subStream always has a value of 1
@@ -352,7 +336,7 @@ impl BcCamera {
let reply = sub_video.recv().await?;
if reply.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(reply.meta.response_code));
return Err(Error::CameraServiceUnavailable(reply.meta.response_code));
}
Ok(())
+1 -1
View File
@@ -27,7 +27,7 @@ impl BcCamera {
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcBody::ModernMsg(ModernMsg {
+1 -1
View File
@@ -26,7 +26,7 @@ impl BcCamera {
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcBody::ModernMsg(ModernMsg {
+16 -6
View File
@@ -323,7 +323,7 @@ impl BcCamera {
let full_block_size = block_size + 4; // Block size + predictor state
let msg_num = self.new_message_num();
let sub = connection.subscribe(MSG_ID_TALK, msg_num).await?;
let mut sub = connection.subscribe(MSG_ID_TALK, msg_num).await?;
const BLOCK_PER_PAYLOAD: usize = 1;
const BLOCK_HEADER_SIZE: usize = 4;
@@ -333,9 +333,10 @@ impl BcCamera {
let target_chunks = full_block_size as usize * BLOCK_PER_PAYLOAD;
let mut payload_bytes = vec![];
let mut end_of_stream = false;
let mut expected_stream_end = std::time::Instant::now();
while !end_of_stream {
let mut payload_bytes = vec![];
while payload_bytes.len() < target_chunks {
let mut buffer = vec![255; target_chunks - payload_bytes.len()];
if let Ok(read) = buffered_recv.read(&mut buffer) {
@@ -373,8 +374,6 @@ impl BcCamera {
break;
};
payload_bytes = vec![];
// Time to play the sample in seconds
let play_length = samples_sent as f32 / sample_rate as f32;
@@ -397,11 +396,22 @@ impl BcCamera {
}),
};
let time_sent = std::time::Instant::now();
sub.send(msg).await?;
std::thread::sleep(std::time::Duration::from_secs_f32(play_length * 0.95));
let play_length = std::time::Duration::from_secs_f32(play_length);
if time_sent > expected_stream_end {
expected_stream_end = time_sent + play_length;
} else {
expected_stream_end += play_length;
}
let _ = sub.recv().await?;
}
// Chunks are still being played, while talk_stop will interrupt them. Wait until we expect
// the stream to end (+ and extra 100ms) before issuing talk_stop.
let remaining_stream_duration = expected_stream_end - std::time::Instant::now();
std::thread::sleep(remaining_stream_duration + std::time::Duration::from_secs_f32(0.1));
self.talk_stop().await?;
Ok(())
+1 -1
View File
@@ -33,7 +33,7 @@ impl BcCamera {
sub_get_general.send(get).await?;
let msg = sub_get_general.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcBody::ModernMsg(ModernMsg {
+1 -1
View File
@@ -25,7 +25,7 @@ impl BcCamera {
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(msg.meta.response_code));
return Err(Error::CameraServiceUnavailable(msg.meta.response_code));
}
if let BcBody::ModernMsg(ModernMsg {
+1 -1
View File
@@ -27,7 +27,7 @@ impl BcCamera {
let modern_reply = sub_version.recv().await?;
if modern_reply.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable(
return Err(Error::CameraServiceUnavailable(
modern_reply.meta.response_code,
));
}
+4 -4
View File
@@ -39,7 +39,7 @@ impl Decoder for BcMediaCodex {
type Item = BcMedia;
type Error = Error;
/// Since frames can cross EOF boundaries we overload this so it dosen't error if
/// Since frames can cross EOF boundaries we overload this so it doesn't error if
/// there are bytes left on the stream
fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>> {
match self.decode(buf)? {
@@ -50,17 +50,17 @@ impl Decoder for BcMediaCodex {
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
loop {
match { BcMedia::deserialize(src) } {
match BcMedia::deserialize(src) {
Ok(bc) => {
if self.amount_skipped > 0 {
debug!("Amount skipped to restore stream: {}", self.amount_skipped);
trace!("Amount skipped to restore stream: {}", self.amount_skipped);
self.amount_skipped = 0;
}
return Ok(Some(bc));
}
Err(Error::NomIncomplete(_)) => {
if self.amount_skipped > 0 {
debug!("Amount skipped to restore stream: {}", self.amount_skipped);
trace!("Amount skipped to restore stream: {}", self.amount_skipped);
self.amount_skipped = 0;
}
return Ok(None);
+31 -8
View File
@@ -1,4 +1,3 @@
use super::model::BcMediaIframe;
use super::model::*;
use crate::Error;
use bytes::{Buf, BytesMut};
@@ -332,14 +331,20 @@ mod tests {
]
.concat();
let mut buf = BytesMut::from(&sample[..]);
// Should derealise all of this
loop {
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let e = BcMedia::deserialize(&mut buf);
match e {
Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => {
// Reach end of files
break;
}
Err(Error::NomIncomplete(_)) if buf.is_empty() => {
// EOF still (but parser looking for next magic)
break;
}
Err(e) => {
error!("{:?}", e);
panic!();
@@ -365,14 +370,19 @@ mod tests {
]
.concat();
let mut buf = BytesMut::from(&sample[..]);
// Should derealise all of this
loop {
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let e = BcMedia::deserialize(&mut buf);
match e {
Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => {
// Reach end of files
break;
}
Err(Error::NomIncomplete(_)) if buf.is_empty() => {
// EOF still (but parser looking for next magic)
break;
}
Err(e) => {
error!("{:?}", e);
panic!();
@@ -411,14 +421,20 @@ mod tests {
]
.concat();
let mut buf = BytesMut::from(&sample[..]);
// Should derealise all of this
loop {
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let e = BcMedia::deserialize(&mut buf);
match e {
Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => {
// Reach end of files
break;
}
Err(Error::NomIncomplete(_)) if buf.is_empty() => {
// EOF still (but parser looking for next magic)
break;
}
Err(e) => {
error!("{:?}", e);
panic!();
@@ -435,7 +451,9 @@ mod tests {
let sample = include_bytes!("samples/info_v1.raw");
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let mut buf = BytesMut::from(&sample[..]);
let e = BcMedia::deserialize(&mut buf);
assert!(matches!(
e,
Ok(BcMedia::InfoV1(BcMediaInfoV1 {
@@ -471,7 +489,9 @@ mod tests {
]
.concat();
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let mut buf = BytesMut::from(&sample[..]);
let e = BcMedia::deserialize(&mut buf);
if let Ok(BcMedia::Iframe(BcMediaIframe {
video_type: VideoType::H264,
microseconds: 3557705112,
@@ -495,7 +515,9 @@ mod tests {
]
.concat();
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let mut buf = BytesMut::from(&sample[..]);
let e = BcMedia::deserialize(&mut buf);
if let Ok(BcMedia::Pframe(BcMediaPframe {
video_type: VideoType::H264,
microseconds: 3557767112,
@@ -513,8 +535,9 @@ mod tests {
init();
let sample = include_bytes!("samples/adpcm_0.raw");
let mut buf = BytesMut::from(&sample[..]);
let e = BcMedia::deserialize(&mut BytesMut::from(&sample[..]));
let e = BcMedia::deserialize(&mut buf);
if let Ok(BcMedia::Adpcm(BcMediaAdpcm { data: d })) = e {
assert_eq!(d.len(), 244);
} else {
+4 -8
View File
@@ -34,7 +34,7 @@ impl Decoder for BcUdpCodex {
type Item = BcUdp;
type Error = Error;
/// Since frames can cross EOF boundaries we overload this so it dosen't error if
/// Since frames can cross EOF boundaries we overload this so it doesn't error if
/// there are bytes left on the stream
// fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>> {
// match self.decode(buf)? {
@@ -48,20 +48,16 @@ impl Decoder for BcUdpCodex {
if src.is_empty() {
return Ok(None);
}
match { BcUdp::deserialize(src) } {
match BcUdp::deserialize(src) {
Ok(BcUdp::Discovery(UdpDiscovery {
payload: UdpXml {
r2c_disc: Some(_), ..
},
payload: UdpXml::R2cDisc(_),
..
})) => {
log::trace!(" Decoding: Relay terminate");
Err(Error::RelayTerminate)
}
Ok(BcUdp::Discovery(UdpDiscovery {
payload: UdpXml {
d2c_disc: Some(_), ..
},
payload: UdpXml::D2cDisc(_),
..
})) => {
log::trace!(" Decoding:Camera terminate");
+19 -25
View File
@@ -11,10 +11,10 @@ use nom::{
type IResult<I, O, E = nom::error::VerboseError<I>> = Result<(I, O), nom::Err<E>>;
fn make_error<I, E: ParseError<I>>(input: I, ctx: &'static str, kind: ErrorKind) -> E
fn make_error<I, E>(input: I, ctx: &'static str, kind: ErrorKind) -> E
where
I: std::marker::Copy,
E: ContextError<I>,
E: ContextError<I> + ParseError<I>,
{
E::add_context(input, ctx, E::from_error_kind(input, kind))
}
@@ -74,7 +74,12 @@ fn udp_disc(buf: &[u8]) -> IResult<&[u8], UdpDiscovery> {
assert_eq!(checksum, actual_checksum);
let decrypted_payload = decrypt(tid, enc_data_slice);
let payload = UdpXml::try_parse(decrypted_payload.as_slice()).map_err(|_| {
let payload = UdpXml::try_parse(decrypted_payload.as_slice()).map_err(|e| {
log::error!(
"decrypted_payload: {:?}",
std::str::from_utf8(&decrypted_payload)
);
log::error!("e: {:?}", e);
Err::Error(make_error(
buf,
"DISC: Unable to decode UDPXml",
@@ -164,13 +169,10 @@ mod tests {
e,
Ok(BcUdp::Discovery(UdpDiscovery {
tid: 96,
payload: UdpXml {
c2d_disc: Some(C2dDisc {
cid: 82000,
did: 80,
}),
..
},
payload: UdpXml::C2dDisc(C2dDisc {
cid: 82000,
did: 80,
}),
}))
);
}
@@ -187,15 +189,12 @@ mod tests {
e,
Ok(BcUdp::Discovery(UdpDiscovery {
tid: 113,
payload: UdpXml {
d2c_t: Some(D2cT {
payload: UdpXml::D2cT(D2cT {
sid: 62098713,
conn: conn_str,
cid: 82001,
did: 96,
}),
..
},
})) if &conn_str == "local"
);
}
@@ -212,15 +211,12 @@ mod tests {
e,
Ok(BcUdp::Discovery(UdpDiscovery {
tid: 1101,
payload: UdpXml {
c2d_t: Some(C2dT {
payload: UdpXml::C2dT(C2dT {
sid: 62098713,
conn: conn_str,
cid: 82001,
mtu: 1350,
}),
..
},
})) if &conn_str == "local"
);
}
@@ -237,17 +233,14 @@ mod tests {
e,
Ok(BcUdp::Discovery(UdpDiscovery {
tid: 1101,
payload: UdpXml {
d2c_cfm: Some(D2cCfm {
payload: UdpXml::D2cCfm(D2cCfm {
sid: 62098713,
conn: conn_str,
rsp: 0,
cid: 82001,
did: 96,
time_r: 0,
time_r: Some(0),
}),
..
},
})) if &conn_str == "local"
);
}
@@ -307,15 +300,16 @@ mod tests {
]
.concat();
let mut buf = BytesMut::from(&sample[..]);
// Should derealise all of this
loop {
let e = BcUdp::deserialize(&mut BytesMut::from(&sample[..]));
let e = BcUdp::deserialize(&mut buf);
match e {
Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => {
// Reach end of files
break;
}
Err(Error::NomIncomplete(_)) => {
Err(Error::NomIncomplete(_)) if buf.is_empty() => {
// Reach end of files
break;
}
+4 -4
View File
@@ -96,7 +96,7 @@ mod tests {
let msg2 = BcUdp::deserialize(&mut BytesMut::from(ser_buf.as_slice())).unwrap();
assert_eq!(msg, msg2);
// Raw samples don't quite match exactly
// because the yaserde for xml puts spaces and new lines in different places
// because the serde for xml puts spaces and new lines in different places
// then the raw data from the camera so we skip this last assert
//assert_eq!(&sample[..], ser_buf.as_slice());
}
@@ -113,7 +113,7 @@ mod tests {
let msg2 = BcUdp::deserialize(&mut BytesMut::from(ser_buf.as_slice())).unwrap();
assert_eq!(msg, msg2);
// Raw samples don't quite match exactly
// because the yaserde for xml puts spaces and new lines in different places
// because the serde for xml puts spaces and new lines in different places
// then the raw data from the camera so we skip this last assert
//assert_eq!(&sample[..], ser_buf.as_slice());
}
@@ -130,7 +130,7 @@ mod tests {
let msg2 = BcUdp::deserialize(&mut BytesMut::from(ser_buf.as_slice())).unwrap();
assert_eq!(msg, msg2);
// Raw samples don't quite match exactly
// because the yaserde for xml puts spaces and new lines in different places
// because the serde for xml puts spaces and new lines in different places
// then the raw data from the camera so we skip this last assert
//assert_eq!(&sample[..], ser_buf.as_slice());
}
@@ -147,7 +147,7 @@ mod tests {
let msg2 = BcUdp::deserialize(&mut BytesMut::from(ser_buf.as_slice())).unwrap();
assert_eq!(msg, msg2);
// Raw samples don't quite match exactly
// because the yaserde for xml puts spaces and new lines in different places
// because the serde for xml puts spaces and new lines in different places
// then the raw data from the camera so we skip this last assert
//assert_eq!(&sample[..], ser_buf.as_slice());
}
+169 -89
View File
@@ -1,76 +1,96 @@
// YaSerde currently macro-expands names like __type__value from type_
use std::io::{Read, Write};
// YaSerde is currently naming the traits and the derive macros identically
use yaserde::ser::Config;
use yaserde_derive::{YaDeserialize, YaSerialize};
use serde::{Deserialize, Serialize};
use std::io::BufRead;
use std::io::Write;
/// The top level of the UDP xml is P2P
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[yaserde(rename = "P2P")]
pub struct UdpXml {
#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
#[serde(rename = "P2P")]
pub enum UdpXml {
/// C2D_S xml Discovery of any client
#[yaserde(rename = "C2D_S")]
pub c2d_s: Option<C2dS>,
#[serde(rename = "C2D_S")]
C2dS(C2dS),
/// C2D_S xml Discovery of client with a UID
#[yaserde(rename = "C2D_C")]
pub c2d_c: Option<C2dC>,
#[serde(rename = "C2D_C")]
C2dC(C2dC),
/// D2C_C_C xml Reply from discovery
#[yaserde(rename = "D2C_C_R")]
pub d2c_c_r: Option<D2cCr>,
#[serde(rename = "D2C_C_R")]
D2cCr(D2cCr),
/// D2C_T xml
#[yaserde(rename = "D2C_T")]
pub d2c_t: Option<D2cT>,
#[serde(rename = "D2C_T")]
D2cT(D2cT),
/// C2D_T xml
#[yaserde(rename = "C2D_T")]
pub c2d_t: Option<C2dT>,
#[serde(rename = "C2D_T")]
C2dT(C2dT),
/// D2C_CFM xml
#[yaserde(rename = "D2C_CFM")]
pub d2c_cfm: Option<D2cCfm>,
#[serde(rename = "D2C_CFM")]
D2cCfm(D2cCfm),
/// C2D_DISC xml Disconnect
#[yaserde(rename = "C2D_DISC")]
pub c2d_disc: Option<C2dDisc>,
#[serde(rename = "C2D_DISC")]
C2dDisc(C2dDisc),
/// D2C_DISC xml Disconnect
#[yaserde(rename = "D2C_DISC")]
pub d2c_disc: Option<D2cDisc>,
#[serde(rename = "D2C_DISC")]
D2cDisc(D2cDisc),
/// R2C_DISC xml Disconnect
#[yaserde(rename = "R2C_DISC")]
pub r2c_disc: Option<R2cDisc>,
#[serde(rename = "R2C_DISC")]
R2cDisc(R2cDisc),
/// C2M_Q xml client to middle man query
#[yaserde(rename = "C2M_Q")]
pub c2m_q: Option<C2mQ>,
#[serde(rename = "C2M_Q")]
C2mQ(C2mQ),
/// M2C_Q_R xml middle man to client query reply
#[yaserde(rename = "M2C_Q_R")]
pub m2c_q_r: Option<M2cQr>,
#[serde(rename = "M2C_Q_R")]
M2cQr(M2cQr),
/// C2R_C xml client to register connect
#[yaserde(rename = "C2R_C")]
pub c2r_c: Option<C2rC>,
#[serde(rename = "C2R_C")]
C2rC(C2rC),
/// R2C_T xml register to clinet with device ID etc
#[yaserde(rename = "R2C_T")]
pub r2c_t: Option<R2cT>,
#[serde(rename = "R2C_T")]
R2cT(R2cT),
/// R2C_T xml register to clinet with device ID etc handled over dmap ONLY
#[yaserde(rename = "R2C_C_R")]
pub r2c_c_r: Option<R2cCr>,
#[serde(rename = "R2C_C_R")]
R2cCr(R2cCr),
/// C2R_CFM xml client to register CFM
#[yaserde(rename = "C2R_CFM")]
pub c2r_cfm: Option<C2rCfm>,
#[serde(rename = "C2R_CFM")]
C2rCfm(C2rCfm),
/// C2D_A xml client to device accept
#[yaserde(rename = "C2D_A")]
pub c2d_a: Option<C2dA>,
#[serde(rename = "C2D_A")]
C2dA(C2dA),
/// C2D_HB xml client to device heartbeat. This is the keep alive
#[yaserde(rename = "C2D_HB")]
pub c2d_hb: Option<C2dHb>,
#[serde(rename = "C2D_HB")]
C2dHb(C2dHb),
/// C2D_HB xml client to device heartbeat. This is the keep alive
#[yaserde(rename = "C2R_HB")]
pub c2r_hb: Option<C2rHb>,
#[serde(rename = "C2R_HB")]
C2rHb(C2rHb),
/// D2D_HB xml client to device heartbeat. This is the keep alive
#[serde(rename = "D2C_HB")]
D2cHb(D2cHb),
}
/// The top level holder for P2P we auto add/remove this at serde
#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)]
struct P2P {
#[serde(rename = "$value")]
xml: UdpXml,
}
impl UdpXml {
pub(crate) fn try_parse(s: impl Read) -> Result<Self, String> {
yaserde::de::from_reader(s)
pub(crate) fn try_parse(s: impl BufRead) -> Result<Self, quick_xml::de::DeError> {
let p2p: Result<P2P, _> = quick_xml::de::from_reader(s);
p2p.map(|i| i.xml)
}
pub(crate) fn serialize<W: Write>(&self, w: W) -> Result<W, String> {
yaserde::ser::serialize_with_writer(self, w, &Config::default())
pub(crate) fn serialize<W: Write>(&self, mut w: W) -> Result<W, quick_xml::de::DeError> {
let mut writer = quick_xml::writer::Writer::new(&mut w);
// No header on a UdpXml
// writer.write_event(quick_xml::events::Event::Decl(
// quick_xml::events::BytesDecl::new("1.0", Some("UTF-8"), None),
// ))?;
writer
.create_element("P2P")
.write_inner_content::<_, quick_xml::de::DeError>(|writer| {
writer.write_serializable("", &self)?;
Ok(())
})?;
Ok(w)
}
}
@@ -80,14 +100,14 @@ impl UdpXml {
/// to whoever it gets this message from
///
/// It should be broadcasted to port 2015
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dS {
/// The destination to reply to
pub to: PortList,
}
/// Port list xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct PortList {
/// Port to open udp connections with
pub port: u32,
@@ -97,7 +117,7 @@ pub struct PortList {
///
/// This will start a connection with any camera that has this UID
/// It should be broadcasted to port 2018
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dC {
/// UID of the camera the client wants to connect with
pub uid: String,
@@ -110,12 +130,12 @@ pub struct C2dC {
/// Debug mode. Purpose unknown
pub debug: bool,
/// Os of the machine known values are `"MAC"`, `"WIN"`
#[yaserde(rename = "p")]
#[serde(rename = "p")]
pub os: String,
}
/// Client List xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct ClientList {
/// Port to start udp communication with
pub port: u32,
@@ -125,12 +145,12 @@ pub struct ClientList {
///
/// This will start a connection with any camera that has this UID
/// It should be broadcasted to port 2018
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct D2cCr {
/// Called timer but not sure what it is a timer of
pub timer: Timer,
/// Unknown
pub rsp: u32,
/// Unknown seems to be 0 on success and -3 on fail
pub rsp: i32,
/// Client ID
pub cid: i32,
/// Camera ID
@@ -138,7 +158,7 @@ pub struct D2cCr {
}
/// Timer provided by D2C_C_R
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct Timer {
/// Unknown
def: u32,
@@ -149,7 +169,7 @@ pub struct Timer {
}
/// C2D_DISC xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dDisc {
/// The client connection ID
pub cid: i32,
@@ -158,7 +178,7 @@ pub struct C2dDisc {
}
/// D2C_DISC xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct D2cDisc {
/// The client connection ID
pub cid: i32,
@@ -167,14 +187,14 @@ pub struct D2cDisc {
}
/// R2C_DISC xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct R2cDisc {
/// The sid
pub sid: u32,
}
/// D2C_T xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct D2cT {
/// The camera SID
pub sid: u32,
@@ -187,7 +207,7 @@ pub struct D2cT {
}
/// C2D_T xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dT {
/// The camera SID
pub sid: u32,
@@ -204,12 +224,12 @@ pub struct C2dT {
/// This is from client to a reolink middle man server
///
/// It should be sent to a reolink p2p sever on port 9999
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2mQ {
/// UID to look up
pub uid: String,
/// Os of the machine known values are `"MAC"`, `"WIN"`
#[yaserde(rename = "p")]
#[serde(rename = "p")]
pub os: String,
}
@@ -217,22 +237,26 @@ pub struct C2mQ {
///
/// This is from middle man reolink server to client
///
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct M2cQr {
/// The register server location
pub reg: IpPort,
#[serde(skip_serializing_if = "Option::is_none")]
pub reg: Option<IpPort>,
/// The relay server location
pub relay: IpPort,
#[serde(skip_serializing_if = "Option::is_none")]
pub relay: Option<IpPort>,
/// The log server location
pub log: IpPort,
#[serde(skip_serializing_if = "Option::is_none")]
pub log: Option<IpPort>,
/// The camera location
pub t: IpPort,
#[serde(skip_serializing_if = "Option::is_none")]
pub t: Option<IpPort>,
}
/// Used as part of M2C_Q_R to provide the host and port
///
/// of the register, relay and log servers
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct IpPort {
/// Ip of the service
pub ip: String,
@@ -255,7 +279,7 @@ impl std::convert::TryFrom<IpPort> for std::net::SocketAddr {
///
/// This is from client to the register reolink server
///
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2rC {
/// The UID to register connecition request with
pub uid: String,
@@ -270,10 +294,10 @@ pub struct C2rC {
/// Inet family. Observed values `4`
pub family: u8,
/// Os of the machine known values are `"MAC"`, `"WIN"`
#[yaserde(rename = "p")]
#[serde(rename = "p")]
pub os: String,
/// The revision. Known values None and 3
#[yaserde(rename = "r")]
#[serde(rename = "r", skip_serializing_if = "Option::is_none")]
pub revision: Option<i32>,
}
@@ -281,11 +305,13 @@ pub struct C2rC {
///
/// This is from register reolink server to clinet with device ip and did etc
///
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct R2cT {
/// The location of the camera
#[serde(skip_serializing_if = "Option::is_none")]
pub dmap: Option<IpPort>,
/// The location of the camera
#[serde(skip_serializing_if = "Option::is_none")]
pub dev: Option<IpPort>,
/// The client id
pub cid: i32,
@@ -298,19 +324,25 @@ pub struct R2cT {
/// This is from register reolink server to clinet with device ip and did etc
/// during a relay
///
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct R2cCr {
/// Dev camera location (actual local ip)
#[serde(skip_serializing_if = "Option::is_none")]
pub dev: Option<IpPort>,
/// Dmap camera location
#[serde(skip_serializing_if = "Option::is_none")]
pub dmap: Option<IpPort>,
/// The location of the relay
#[serde(skip_serializing_if = "Option::is_none")]
pub relay: Option<IpPort>,
/// The location of the relayt (not sure what the t is for)
#[serde(skip_serializing_if = "Option::is_none")]
pub relayt: Option<IpPort>,
/// The nat type. Known values `"NULL"`
pub nat: String,
/// The camera SID
pub sid: u32,
/// rsp. Known values `0`
/// The camera SID, missing when rsp is `-3`
pub sid: Option<u32>,
/// rsp. Known values `0`, `-3, seems to be 0 on success and -3 on fail`
pub rsp: i32,
/// ac. Known values. `127536491`
pub ac: u32,
@@ -319,33 +351,33 @@ pub struct R2cCr {
/// D2C_CFM xml
///
/// Device to client, with connection started from middle man server
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct D2cCfm {
/// The camera SID
pub sid: u32,
/// Type of connection observed values are `"local"`
pub conn: String,
/// Unknown known values are `0`
pub rsp: u32,
/// Unknown known values are `0`, `-3, seems to be 0 on success and -3 on fail`
pub rsp: i32,
/// The client connection ID
pub cid: i32,
/// The camera connection ID
pub did: i32,
/// The time but only value that has been observed is `0
pub time_r: u32,
pub time_r: Option<u32>,
}
/// C2R_CFM xml
///
/// Client to register
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2rCfm {
/// The camera SID
pub sid: u32,
/// Type of connection observed values are `"local"`
pub conn: String,
/// Unknown known values are `0`
pub rsp: u32,
/// Unknown known values are `0`, `-3, seems to be 0 on success and -3 on fail`
pub rsp: i32,
/// The client connection ID
pub cid: i32,
/// The camera connection ID
@@ -356,7 +388,7 @@ pub struct C2rCfm {
///
/// Client to device accept.
/// Sent it reply to a D2C_T
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dA {
/// The camera SID
pub sid: u32,
@@ -374,7 +406,7 @@ pub struct C2dA {
///
/// Client to device heart beat.
/// Seems to act as a keep alive
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2dHb {
/// The client connection ID
pub cid: i32,
@@ -386,7 +418,7 @@ pub struct C2dHb {
///
/// Client to device heart beat.
/// Seems to act as a keep alive
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct C2rHb {
/// The connection ID
pub sid: u32,
@@ -395,3 +427,51 @@ pub struct C2rHb {
/// The camera connection ID
pub did: i32,
}
/// D2C_HB xml
///
/// Device to client heart beat.
/// Seems to act as a keep alive
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct D2cHb {
/// The client connection ID
pub cid: i32,
/// The camera connection ID
pub did: i32,
}
/// This test was added because of issues with Argus3 during discovery
#[test]
fn test_d2c_c_r_deser() {
let sample = indoc::indoc!(
r#"
<P2P>
<D2C_C_R>
<timer>
<def>3000</def>
<hb>20000</hb>
<hbt>60000</hbt>
</timer>
<rsp>0</rsp>
<cid>-376737975</cid>
<did>49</did>
</D2C_C_R>
</P2P>
"#
);
let b: UdpXml = UdpXml::try_parse(sample.as_bytes()).unwrap();
assert_matches::assert_matches!(
b,
UdpXml::D2cCr(D2cCr {
timer: Timer {
def: 3000,
hb: 20000,
hbt: 60000,
},
rsp: 0,
cid: -376737975,
did: 49,
})
);
}
+39 -7
View File
@@ -9,18 +9,50 @@
//! A camera can be initialised with
//!
//! ```no_run
//! use neolink_core::bc_protocol::BcCamera;
//! let channel_id = 0; // Usually zero but can be non zero if uses a reolink NVR
//! let mut camera = BcCamera::new_with_addr("camera_ip_address", channel_id).unwrap();
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! use neolink_core::bc_protocol::{BcCamera, BcCameraOpt, DiscoveryMethods, ConnectionProtocol, Credentials};
//! let options = BcCameraOpt {
//! name: "CamName".to_string(),
//! channel_id: 0,
//! addrs: ["192.168.1.1".parse().unwrap()].to_vec(),
//! port: Some(9000),
//! uid: Some("CAMUID".to_string()),
//! protocol: ConnectionProtocol::TcpUdp,
//! discovery: DiscoveryMethods::Relay,
//! credentials: Credentials {
//! username: "username".to_string(),
//! password: Some("password".to_string()),
//! },
//! debug: false,
//! max_discovery_retries: 10,
//! };
//! let mut camera = BcCamera::new(&options).await.unwrap();
//! # })
//! ```
//!
//! After that login can be conducted with
//!
//! ```no_run
//! # use neolink_core::bc_protocol::BcCamera;
//! # let channel_id = 0;
//! # let mut camera = BcCamera::new_with_addr("camera_ip_address", channel_id).unwrap();
//! camera.login("username", Some("password"));
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! # use neolink_core::bc_protocol::{BcCamera, BcCameraOpt, DiscoveryMethods, ConnectionProtocol, Credentials};
//! # let options = BcCameraOpt {
//! # name: "CamName".to_string(),
//! # channel_id: 0,
//! # addrs: ["192.168.1.1".parse().unwrap()].to_vec(),
//! # port: Some(9000),
//! # uid: Some("CAMUID".to_string()),
//! # protocol: ConnectionProtocol::TcpUdp,
//! # discovery: DiscoveryMethods::Relay,
//! # credentials: Credentials {
//! # username: "username".to_string(),
//! # password: Some("password".to_string()),
//! # },
//! # debug: false,
//! # max_discovery_retries: 10,
//! # };
//! # let mut camera = BcCamera::new(&options).await.unwrap();
//! camera.login().await;
//! # })
//! ```
//! For further commands see the [`bc_protocol::BcCamera`] struct.
//!
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink_decoder"
description = "A cli decoder for the AES encrypted packets"
version = "0.0.1"
version = "0.6.3-rc.2"
authors = ["Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
+2 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "pushnoti"
version = "0.1.0"
version = "0.6.3-rc.2"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -12,7 +12,7 @@ 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.6.3-rc.1" }
neolink_core = { path = "../core", version = "0.6.3-rc.2" }
regex = "1.8.1"
serde = "1.0.163"
tokio = { version = "1.28.1", features = ["full"] }
+2
View File
@@ -15,7 +15,9 @@ use std::{
pub(crate) enum AddressOrUid {
Address(String),
#[allow(dead_code)]
Uid(String, DiscoveryMethods),
#[allow(dead_code)]
AddressWithUid(String, String, DiscoveryMethods),
}
+25
View File
@@ -0,0 +1,25 @@
# Using the dissector
The dissector can be used with `wireshark` and `tshark`. It requires another wireshark module [`luagcrypt`](https://github.com/Lekensteyn/luagcrypt) which is not packaged in most Linux distributions and needs building from source. Instructions here are suitable for Debian and its derivatives (tested on Debian 12 Bookworm amd64).
## Build `luagcrypt.so`
```
sudo apt install lua5.2 liblua5.2-dev libgcrypt20-dev libgpg-error-dev
git clone https://github.com/Lekensteyn/luagcrypt.git
cd luagcrypt
make LUA_DIR=/usr
```
## Install `luagcrypt.so`
The shared object library should be copied to `/usr/local/lib/lua/5.2/`
```
mkdir --parents /usr/local/lib/lua/5.2/
cp luagcypt.so /usr/local/lib/lua/5.2/
```
Additionally, the system where the dissector is used needs these packages installing (if not already present): `libgcrypt20 libgpg-error0`
## Install `baichuan.lua`
Copy the dissector to the host where `wireshark` (or `tshark`) will be used to analyse the captured packets:
```
mkdir --parents $HOME/.local/lib/wireshark/plugins/
cp baichuan.lua $HOME/.local/lib/wireshark/plugins/
```
+2
View File
@@ -6,4 +6,6 @@ exit_func() {
}
trap exit_func TERM INT
ulimit -n 1024
"$@"
+50
View File
@@ -0,0 +1,50 @@
### Neolink in Kubernetes
#### Introduction
This README provides guidance on deploying the NeoLink application on Kubernetes using the provided manifest files. The deployment is designed to set up a NeoLink application within its own namespace, with a specific configuration for node affinity, resource limits, and service exposure.
#### Prerequisites
- Kubernetes Cluster
- Command-line tool (kubectl)
- Basic understanding of Kubernetes concepts (Pods, Services, Deployments, ConfigMaps)
#### Deployment Overview
The deployment consists of four main parts:
1. Namespace Creation: A new namespace neolink is created to isolate the resources.
2. Deployment Configuration: The neolink-app-deployment is set up with:
- A single replica.
- Affinity to arm64 nodes (Though this is optional).
- Resource limits and requests for CPU and memory.
- A custom command and args to use a specific configuration file.
3. Service Exposure: A LoadBalancer type service named neolink exposes the application on port 8554.
4. ConfigMap: Contains the application configuration neolink.toml.
#### Steps to Deploy
```bash
kubectl apply -f manifest.yaml
```
### Configuration Details
- The neolink.toml file in the ConfigMap provides the application configuration. Customize it as per your requirements.
- The deployment is set to favor arm64 architecture nodes via a `nodeAffinity`. This is a personal preference as in my cluster all my raspberrpis (except one) run 32bit raspian OS. And to try and get more bang for my buck I was to run neolink on my arm64 8Gb node. Again, this is totaly optional for you.
- Resource limits are set to ensure efficient use of cluster resources.
- The LoadBalancer service will make the application accessible externally.
#### Troubleshooting
- If the pod fails to start, check the pod logs and describe the pod for more details.
- Ensure that the arm64 node is available and schedulable in your cluster (if you're using nodeAffinity).
- Verify that the ConfigMap is correctly applied and mounted.
#### Conclusion
This guide provides a step-by-step approach to deploying the NeoLink application on a Kubernetes cluster. Modify the manifests as necessary to fit your specific requirements and environment.
+172
View File
@@ -0,0 +1,172 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: neolink
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: neolink-app-deployment
namespace: neolink
labels:
app: neolink
spec:
replicas: 1
selector:
matchLabels:
app: neolink
template:
metadata:
labels:
app: neolink
spec:
containers:
- name: neolink
image: quantumentangledandy/neolink
ports:
- containerPort: 8554
command: ["/bin/sh", "-c"]
args: ["cp /config/neolink.toml /etc/neolink.toml && /usr/local/bin/neolink rtsp --config /etc/neolink.toml"]
volumeMounts:
- name: config-volume
mountPath: /config
resources:
limits:
memory: "600Mi"
cpu: "300m"
requests:
memory: "200Mi"
cpu: "150m"
volumes:
- name: config-volume
configMap:
name: default-config
---
apiVersion: v1
kind: Service
metadata:
labels:
app: neolink
name: neolink
namespace: neolink
spec:
ports:
- name: tcp-interface-neolink
port: 8554
protocol: TCP
targetPort: 8554
selector:
app: neolink
type: LoadBalancer
---
apiVersion: v1
kind: ConfigMap
metadata:
name: default-config
namespace: neolink
data:
neolink.toml: |
# A bind value of 0.0.0.0 means any network this computer can access
# You can chage this to a specfic network e.g. "192.168.1.101" here
# Or to no networks e.g. this computer only "127.0.0.1"
bind = "0.0.0.0"
# Default port is 8554 but you can change it by uncommenting the following
# bind_port = 8554
# Uncomment the following and supply a path to a valid PEM
# to activate TLS encryption.
# The PEM should contain the certificate and the private key
# If TLS is activated you must connect with "rtsps://" and not "rtsp://"
# certificate = "/path/to/pem/with/cert/and/key"
# Choose if the client is required to provide a certificate signed by the server's CA.
# none|requested|required - default none
# tls_client_auth = "required"
# You can password protect the rtsp server mount points by adding users
# like the following me and someone. If you do not add [[users]]
# then anyone can connect without a password or username
# To access such a stream try using a url such as "rtsp://me:mepass@192.168.1.101/driveway"
# [[users]]
# name = "me"
# pass = "mepass"
#
# [[users]]
# name = "someone"
# pass = "someonepass"
# Uncomment to enable MQTT
#[mqtt]
# mqtt.broker_addr = "192.168.1.122"
# mqtt.port = 1883
# mqtt.credentials = ["mqtt_user", "mqtt_password"]
[[cameras]]
name = "driveway"
username = "admin"
password = "12345678"
address = "192.168.1.187:9000"
# MQTT Discovery: https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery
# mqtt.discovery.topic = "homeassistant" # Uncomment to enable
# If using discovery, _ characters are replaced with spaces in the name and title case is applied
# mqtt.discovery.features = ["floodlight"] # Uncomment if this camera has a spotlight/floodlight
# If you use a battery camera: **Instead** of an `address` supply the uid
# as follows
# uid = "ABCD01234567890EFG"
# By default any of the users can connect (or anyone at all if no users are specfied)
# You can uncomment the following to permit only specfic users
# permitted_users = [ "me" ]
# By default "both" "mainStream" and "subStream" are connected
# If your device has user connection limits try a single stream instead.
# stream = "mainStream"
# By default neolink will use any means to connect to the camera
# from a UID
# This include relaying via reolink servers
# This variable `discovery` controls the method of UID discovery
# - Possible values
# "relay" # Any means including connecting and transmitting through reolink
# "map" # Register our local ip address with reolink and ask the camera to connect to us but don't relay data through reolink
# "remote" # Register our local ip address with reolink but only permit same network connections (useful if broadcast is not possible)
# "local" # Do not contact reolink servers at all. Rely soley on local UDP broadcast based discovery
#
# "cellular" # Cellular camera only support Relay and Map to speed up connecting to them this option will skip the local/remote
#
# discovery = "relay"
# Certain types of camera emit status messages (such as battery levels)
#
# By default we hide these status messages from the user but you can instead requst that
# they be printed to stdout using print_format
#
# Valid values are:
# - None
# - Human
# - Xml
#
# print_format = "None"
[[cameras]]
name = "storage shed"
username = "admin"
password = "987654321"
address = "192.168.1.245:9000"
# If you use a battery camera: **Instead** of an `address` supply the uid
# as follows
# uid = "ABCD01234567890EFG"
# If you use an NVR that relays several camera connections you can choose which
# camera to connect to with by setting the `channel_id`
#
# By default channel_id = 0. Eg the first connected camera on the device
# **Note**: that unlike in the offical client the numbering starts from 0 not 1.
# An 8 channel NVR would have channels 0 through 7
# channel_id = 0
+1 -1
View File
@@ -98,6 +98,6 @@ address = "192.168.1.245:9000"
# camera to connect to with by setting the `channel_id`
#
# By default channel_id = 0. Eg the first connected camera on the device
# **Note**: that unlike in the offical client the numbering starts from 0 not 1.
# **Note**: that unlike in the official client the numbering starts from 0 not 1.
# An 8 channel NVR would have channels 0 through 7
# channel_id = 0
+5 -5
View File
@@ -23,7 +23,6 @@ pub(crate) use cmdline::Opt;
/// Opt is the command line options
pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
let camera = reactor.get(&opt.camera).await?;
log::debug!("Battery: Instance aquired");
let state = camera
.run_task(|cam| {
@@ -35,10 +34,11 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
})
.await?;
let ser = String::from_utf8(
yaserde::ser::serialize_with_writer(&state, vec![], &Default::default())
.expect("Should Ser the struct"),
)
let ser = String::from_utf8({
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).expect("Should Ser the struct");
buf.to_vec()
})
.expect("Should be UTF8");
println!("{}", ser);
+1
View File
@@ -27,4 +27,5 @@ pub enum Command {
MqttRtsp(super::mqtt::Opt),
Image(super::image::Opt),
Battery(super::battery::Opt),
Services(super::services::Opt),
}
-5
View File
@@ -49,11 +49,9 @@ impl NeoCamThread {
// Now we wait for a disconnect
tokio::select! {
_ = cancel_check.cancelled() => {
log::debug!("{name}: Camera Cancelled");
AnyResult::Ok(())
}
v = camera.join() => {
log::debug!("{name}: Camera Join: {:?}", v);
v?;
Ok(())
},
@@ -152,7 +150,6 @@ impl NeoCamThread {
match result {
Ok(()) => {
// Normal shutdown
log::debug!("Cancel:: NeoCamThread::NormalShutdown");
self.cancel.cancel();
return Ok(());
}
@@ -164,7 +161,6 @@ impl NeoCamThread {
Some(neolink_core::Error::CameraLoginFail) => {
// Fatal
log::error!("{name}: Login credentials were not accepted");
log::debug!("NeoCamThread::run Login Cancel");
self.cancel.cancel();
return Err(e);
}
@@ -184,7 +180,6 @@ impl NeoCamThread {
impl Drop for NeoCamThread {
fn drop(&mut self) {
log::debug!("Cancel:: NeoCamThread::drop");
self.cancel.cancel();
}
}
+15 -18
View File
@@ -123,9 +123,9 @@ impl NeoInstance {
for _ in 0..5 {
r = task(cam_ref).await;
if let Err(e) = &r {
log::debug!("- Task Result: {e:?}");
log::trace!("- Task Result: {e:?}");
}
if let Err(Some(neolink_core::Error::CameraServiceUnavaliable(400))) = r.as_ref().map_err(|e| e.downcast_ref::<neolink_core::Error>()) {
if let Err(Some(neolink_core::Error::CameraServiceUnavailable(400))) = r.as_ref().map_err(|e| e.downcast_ref::<neolink_core::Error>()) {
// Retryable without a reconnect
// Usually occurs when camera is starting up
// or the connection is initialising
@@ -147,73 +147,70 @@ impl NeoInstance {
Err(e) => {
match e.downcast::<neolink_core::Error>() {
Ok(neolink_core::Error::DroppedConnection) | Ok(neolink_core::Error::TimeoutDisconnected) => {
log::debug!(" - Neolink error continue");
continue;
},
Ok(neolink_core::Error::TokioBcSendError) => {
log::debug!(" - Neolink Send Error continue");
continue;
},
Ok(neolink_core::Error::Io(e)) => {
log::debug!(" - Neolink Std IO Error");
use std::io::ErrorKind::*;
if let ConnectionReset | ConnectionAborted | BrokenPipe | TimedOut = e.kind() {
// Resetable IO
log::debug!(" - Neolink Std IO Error: Continue");
log::trace!(" - Neolink Std IO Error: Continue");
continue;
} else {
// Check if the inner error is the Other type and then the discomnect
let is_dropped = e.get_ref().is_some_and(|e| {
log::debug!("Std IO Error: Inner: {:?}", e);
log::trace!("Std IO Error: Inner: {:?}", e);
matches!(e.downcast_ref::<neolink_core::Error>(),
Some(neolink_core::Error::DroppedConnection) | Some(neolink_core::Error::TimeoutDisconnected)
)
});
if is_dropped {
// Retry is a None
log::debug!(" - Neolink Std IO Error => Neolink: Continue");
log::trace!(" - Neolink Std IO Error => Neolink: Continue");
continue;
} else {
log::debug!(" - Neolink Std IO Error: Other");
log::trace!(" - Neolink Std IO Error: Other");
Err(e.into())
}
}
}
Ok(e) => {
log::debug!(" - Neolink Error: Other");
log::trace!(" - Neolink Error: Other");
Err(e.into())
},
Err(e) => {
// Check if it is an io error
log::debug!(" - Other Error: {:?}", e);
log::trace!(" - Other Error: {:?}", e);
match e.downcast::<std::io::Error>() {
Ok(e) => {
log::debug!(" - Std IO Error");
log::trace!(" - Std IO Error");
// Check if the inner error is the Other type and then the discomnect
use std::io::ErrorKind::*;
if let ConnectionReset | ConnectionAborted | BrokenPipe | TimedOut = e.kind() {
// Resetable IO
log::debug!(" - Std IO Error: Continue");
log::trace!(" - Std IO Error: Continue");
continue;
} else {
let is_dropped = e.get_ref().is_some_and(|e| {
log::debug!("Std IO Error: Inner: {:?}", e);
log::trace!("Std IO Error: Inner: {:?}", e);
matches!(e.downcast_ref::<neolink_core::Error>(),
Some(neolink_core::Error::DroppedConnection) | Some(neolink_core::Error::TimeoutDisconnected) | Some(neolink_core::Error::TokioBcSendError)
)
});
if is_dropped {
// Retry is a None
log::debug!(" - Std IO Error => Neolink Error: Continue");
log::trace!(" - Std IO Error => Neolink Error: Continue");
continue;
} else {
log::debug!(" - Std IO Error: Other");
log::trace!(" - Std IO Error: Other");
Err(e.into())
}
}
},
Err(e) => {
log::debug!(" - Other Error: {:?}", e);
log::trace!(" - Other Error: {:?}", e);
Err(e)
}
}
@@ -289,7 +286,7 @@ impl NeoInstance {
.await
{
Ok(pn) => {
log::debug!("Forwarding push notification about {}", uid);
log::trace!("Forwarding push notification about {}", uid);
let _ = fwatch_tx.send_replace(pn.clone());
}
Err(e) => {
+1
View File
@@ -18,6 +18,7 @@ use crate::{AnyResult, Result};
use neolink_core::bc_protocol::MotionStatus;
pub(crate) enum MdState {
#[allow(dead_code)]
Start(Instant),
Stop(Instant),
Unknown,
+1 -23
View File
@@ -91,14 +91,12 @@ impl NeoCam {
let thread_cancel = sender_cancel.clone();
let res = tokio::select! {
_ = sender_cancel.cancelled() => {
log::debug!("Control thread Cancelled");
Result::Ok(())
},
v = async {
while let Some(command) = commander_rx.next().await {
match command {
NeoCamCommand::HangUp => {
log::debug!("Cancel:: NeoCamCommand::HangUp");
sender_cancel.cancel();
return Result::<(), anyhow::Error>::Ok(());
}
@@ -153,14 +151,12 @@ impl NeoCam {
NeoCamCommand::Connect(sender) => {
if !matches!(*state_tx.borrow(), NeoCamThreadState::Connected) {
state_tx.send_replace(NeoCamThreadState::Connected);
log::debug!("{}: Connect On Request", thread_watch_config_rx.borrow().name);
}
let _ = sender.send(());
}
NeoCamCommand::Disconnect(sender) => {
if !matches!(*state_tx.borrow(), NeoCamThreadState::Disconnected) {
state_tx.send_replace(NeoCamThreadState::Disconnected);
log::debug!("{}: Disconnect On Request", thread_watch_config_rx.borrow().name);
}
let _ = sender.send(());
}
@@ -187,14 +183,11 @@ impl NeoCam {
},
}
}
log::debug!("Control thread Senders dropped");
Ok(())
} => {
log::debug!("Camera Control Thread Ended; {:?}", v);
v
}
};
log::debug!("Control thread terminated");
res
});
@@ -216,11 +209,7 @@ impl NeoCam {
me.cancel.clone(),
)
.await;
me.set.spawn(async move {
let v = cam_thread.run().await;
log::debug!("Camera MAIN thread ended; {:?}", v);
v
});
me.set.spawn(async move { cam_thread.run().await });
// This thread maintains the streams
let stream_instance = instance.subscribe().await?;
@@ -230,7 +219,6 @@ impl NeoCam {
tokio::select! {
_ = stream_cancel.cancelled() => AnyResult::Ok(()),
v = stream_thread.run() => {
log::debug!("Camera Stream thread ended; {:?}", v);
v
},
}
@@ -244,7 +232,6 @@ impl NeoCam {
tokio::select! {
_ = md_cancel.cancelled() => AnyResult::Ok(()),
v = md_thread.run() => {
log::debug!("MD thread ended; {:?}", v);
v
},
}
@@ -277,7 +264,6 @@ impl NeoCam {
for encode in stream_info.stream_infos.iter().flat_map(|stream_info| stream_info.encode_tables.clone()) {
supported_streams.push(std::format!(" {}: {}x{}", encode.name, encode.resolution.width, encode.resolution.height));
}
log::debug!("{}: Listing Camera Supported Streams\n{}", report_name, supported_streams.join("\n"));
Ok(())
@@ -345,13 +331,11 @@ impl NeoCam {
},
};
if r.is_err() {
log::debug!("Push notifications stopped: {:?}", r);
break r;
}
}?;
AnyResult::Ok(())
} => {
log::debug!("Push notification thread ended; {:?}", v);
v
},
}
@@ -380,7 +364,6 @@ impl NeoCam {
}
}
} => {
log::debug!("MD Wake Up thread ended; {:?}", v);
v
},
}
@@ -393,7 +376,6 @@ impl NeoCam {
// notifications are observed
let connect_instance = instance.subscribe().await?;
let connect_cancel = me.cancel.clone();
let connect_name = config.name.clone();
me.set.spawn(async move {
tokio::select!{
_ = connect_cancel.cancelled() => {
@@ -418,16 +400,13 @@ impl NeoCam {
permit.deactivate().await?; // Watching only from here
loop {
permit.aquired_users().await?;
log::debug!("{connect_name}: InUse");
connect_instance.connect().await?;
permit.dropped_users().await?;
log::debug!("{connect_name}: Idle Wait");
// Wait 30s or if we hit another use then go back and wait again
tokio::select! {
_ = sleep(Duration::from_secs(30)) => {},
_ = permit.aquired_users() => continue,
};
log::debug!("{connect_name}: Idle");
connect_instance.disconnect().await?;
}
} => v,
@@ -438,7 +417,6 @@ impl NeoCam {
}?;
AnyResult::Ok(())
} => {
log::debug!("Idle Disconnect thread ended; {:?}", v);
v
},
}
+24 -16
View File
@@ -11,6 +11,7 @@ use tokio::{
mpsc::{Receiver as MpscReceiver, Sender as MpscSender},
oneshot::Sender as OneshotSender,
watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender},
RwLock,
},
time::{sleep, timeout, Duration},
};
@@ -67,6 +68,11 @@ impl PushNotiThread {
let sender_id = "743639030586"; // andriod
// let sender_id = "696841269229"; // ios
// let firebase_app_id = "1:743639030586:android:86f60a4fb7143876";
// let firebase_project_id = "reolink-login";
// let firebase_api_key = "AIzaSyBEUIuWHnnOEwFahxWgQB4Yt4NsgOmkPyE";
// let vapid_key = "????";
let token_path = dirs::config_dir().map(|mut d| {
d.push("./neolink_token.toml");
d
@@ -114,21 +120,6 @@ impl PushNotiThread {
log::debug!("Push notification Listening");
let thread_pn_watcher = self.pn_watcher.clone();
let mut listener = FcmPushListener::create(
registration,
|message: FcmMessage| {
log::debug!("Got FCM Message: {:?}", message.payload_json);
if let Some(id) = message.persistent_id.clone() {
// Don't worry if queue is full we will just not register as received yet
let _ = sender.try_send(PnRequest::AddPushID { id });
}
thread_pn_watcher.send_replace(Some(PushNoti {
message: message.payload_json,
id: message.persistent_id,
}));
},
self.received_ids.clone(),
);
for instance in self.registed_cameras.iter() {
let uid = uid.clone();
@@ -154,9 +145,25 @@ impl PushNotiThread {
});
}
let received_ids = Arc::new(RwLock::new(self.received_ids.clone()));
tokio::select! {
v = async {
loop {
let mut listener = FcmPushListener::create(
registration.clone(),
|message: FcmMessage| {
log::debug!("Got FCM Message: {:?}", message.payload_json);
if let Some(id) = message.persistent_id.clone() {
// Don't worry if queue is full we will just not register as received yet
let _ = sender.try_send(PnRequest::AddPushID { id });
}
thread_pn_watcher.send_replace(Some(PushNoti {
message: message.payload_json,
id: message.persistent_id,
}));
},
received_ids.read().await.clone(),
);
let r = timeout(Duration::from_secs(60*5), listener.connect()).await;
match &r {
Ok(Ok(_)) => {
@@ -220,7 +227,8 @@ impl PushNotiThread {
});
}
PnRequest::AddPushID{id} => {
self.received_ids.push(id);
self.received_ids.push(id.clone());
received_ids.write().await.push(id);
}
}
}
+12 -11
View File
@@ -43,10 +43,11 @@ impl NeoReactor {
let cancel = CancellationToken::new();
let (config_tx, _) = watch(config);
let mut set = JoinSet::new();
let config_tx = Arc::new(config_tx);
let cancel1 = cancel.clone();
let cancel2 = cancel.clone();
let config_tx = Arc::new(config_tx);
let thread_config_tx = config_tx.clone();
set.spawn(async move {
let mut instances: HashMap<String, NeoCam> = Default::default();
@@ -60,22 +61,19 @@ impl NeoReactor {
match command {
NeoReactorCommand::HangUp => {
instances.clear();
log::debug!("Cancel:: NeoReactorCommand::HangUp");
cancel2.cancel();
return Result::<(), anyhow::Error>::Ok(());
}
NeoReactorCommand::Config(reply) => {
let _ = reply.send(config_tx.subscribe());
let _ = reply.send(thread_config_tx.subscribe());
}
NeoReactorCommand::Get(name, sender) => {
let new = match instances.entry(name.clone()) {
Entry::Occupied(occ) => Result::Ok(Some(occ.get().subscribe().await?)),
Entry::Vacant(vac) => {
log::debug!("Inserting new insance");
let current_config: Config = (*config_tx.borrow()).clone();
let current_config: Config = (*thread_config_tx.borrow()).clone();
if let Some(config) = current_config.cameras.iter().find(|cam| cam.name == name).cloned() {
let cam = NeoCam::new(config, push_noti.clone()).await?;
log::debug!("New instance created");
Result::Ok(Some(
vac.insert(
cam,
@@ -88,7 +86,6 @@ impl NeoReactor {
}
}
};
log::debug!("Got instance from reactor");
let _ = sender.send(new);
},
NeoReactorCommand::UpdateConfig(new_conf, reply) => {
@@ -103,7 +100,7 @@ impl NeoReactor {
}
// Set the new conf
let _ = config_tx.send_replace(new_conf);
let _ = thread_config_tx.send_replace(new_conf);
// Reply that we are done
let _ = reply.send(Ok(()));
}
@@ -117,14 +114,19 @@ impl NeoReactor {
// Push notification client
let cancel1 = cancel.clone();
let mut thread_config_rx = config_tx.subscribe();
set.spawn(async move {
let r = tokio::select! {
_ = cancel1.cancelled() => AnyResult::Ok(()),
v = async {
let mut pn = PushNotiThread::new().await?;
let mut pn = PushNotiThread::new().await?;
loop {
let r = pn.run(&pn_tx, &mut pn_rx).await;
thread_config_rx.wait_for(|c| c.cameras.iter().any(|cam| cam.push_notifications)).await?; // Wait until PN are enabled
let r = tokio::select!{
v = pn.run(&pn_tx, &mut pn_rx) => {v},
_ = thread_config_rx.wait_for(|c| c.cameras.iter().all(|cam| !cam.push_notifications)) => AnyResult::Ok(()), // Quit if PN is turned off
};
if r.is_err() {
log::debug!("Issue with push notifier: {r:?}");
sleep(Duration::from_secs(5)).await;
@@ -134,7 +136,6 @@ impl NeoReactor {
}
} => v,
};
log::debug!("Push notifier ended: {r:?}");
r
});
+53 -40
View File
@@ -185,7 +185,6 @@ impl NeoCamStreamThread {
impl Drop for NeoCamStreamThread {
fn drop(&mut self) {
log::debug!("NeoCamStreamThread::drop Cancel");
self.cancel.cancel();
for stream in self.streams.values() {
stream.cancel.cancel()
@@ -238,13 +237,13 @@ pub(crate) struct StreamData {
users: UseCounter,
}
#[derive(Eq, PartialEq, Clone, Debug)]
#[derive(Eq, PartialEq, Clone, Debug, Copy)]
pub(crate) enum VidFormat {
None,
H264,
H265,
}
#[derive(Eq, PartialEq, Clone, Debug)]
#[derive(Eq, PartialEq, Clone, Debug, Copy)]
pub(crate) enum AudFormat {
None,
Aac,
@@ -317,11 +316,11 @@ impl StreamInstance {
impl StreamData {
async fn new(name: StreamKind, instance: NeoInstance, strict: bool) -> Result<Self> {
const BUFFER_DURATION: Duration = Duration::from_secs(15);
const BUFFER_DURATION: Duration = Duration::from_secs(3);
// At 30fps for 15s with audio is is about 900 frames
// Therefore we set this buffer to a rather large 2000
let (vid, _) = broadcast::<StampedData>(2000);
let (aud, _) = broadcast::<StampedData>(2000);
const BUFFER_SIZE: usize = 30usize * BUFFER_DURATION.as_millis() as usize / 1000usize;
let (vid, _) = broadcast::<StampedData>(BUFFER_SIZE);
let (aud, _) = broadcast::<StampedData>(BUFFER_SIZE);
let (vid_history, _) = watch::<VecDeque<StampedData>>(VecDeque::new());
let vid_history = Arc::new(vid_history);
let (aud_history, _) = watch::<VecDeque<StampedData>>(VecDeque::new());
@@ -339,20 +338,35 @@ impl StreamData {
if let Some(encode) =
infos.iter().find(|encode| encode.name == name.to_string())
{
let bitrate_table = encode
.bitrate_table
.split(',')
.filter_map(|c| {
let i: Result<u32, _> = c.parse();
i.ok()
})
.collect::<Vec<u32>>();
let framerate_table = encode
.framerate_table
.split(',')
.filter_map(|c| {
let i: Result<u32, _> = c.parse();
i.ok()
})
.collect::<Vec<u32>>();
Ok((
[encode.resolution.width, encode.resolution.height],
encode
.bitrate_table
bitrate_table
.get(encode.default_bitrate as usize)
.copied()
.unwrap_or(encode.default_bitrate)
* 1024,
encode
.framerate_table
framerate_table
.get(encode.default_framerate as usize)
.copied()
.unwrap_or(encode.default_framerate),
encode.framerate_table.clone(),
framerate_table.clone(),
))
} else {
Ok(([0, 0], 0, 0, vec![]))
@@ -394,6 +408,14 @@ impl StreamData {
let vid_history = me.vid_history.clone();
let aud_history = me.aud_history.clone();
let mut permit = instance.permit().await?;
// Rather than extract the time stamp from the frame data we
// create a new one from the fps
// this makes it easier to play the frames at a constant rate
// even across reconnects (since reconnects restart the ts otherwise)
let mut master_ts = Duration::ZERO;
let mut fps_delta = Duration::from_millis(1000 / (config.borrow().fps as u64));
me.handle = Some(tokio::task::spawn(async move {
let r = tokio::select! {
_ = cancel.cancelled() => {
@@ -407,7 +429,6 @@ impl StreamData {
//
// This should stop one branch of the select from waking the other
// too often
let watchdog_print_name = print_name.clone();
tokio::task::spawn(async move {
let mut check_timeout = timeout(Duration::from_secs(15), watchdog_rx.recv()).await; // Wait longer for the first feed
loop {
@@ -415,15 +436,12 @@ impl StreamData {
Err(_) => {
// Timeout
// Break with Ok to trigger the restart
log::debug!("{watchdog_print_name}: Watchdog kicking the stream");
break;
},
Ok(None) => {
log::debug!("{watchdog_print_name}: Watchdog dropped the stream");
break;
}
Ok(_) => {
// log::debug!("{print_name}: Good Doggo");
check_timeout = timeout(Duration::from_secs(10), watchdog_rx.recv()).await;
}
}
@@ -435,12 +453,10 @@ impl StreamData {
tokio::select! {
v = thread_inuse.dropped_users() => {
// Handles the stop and restart when no active users
log::debug!("{print_name}: Streaming STOP");
permit.deactivate().await?;
v?;
thread_inuse.aquired_users().await?; // Wait for new users of the stream
permit.activate().await?;
log::debug!("{print_name}: Streaming START");
AnyResult::Ok(())
},
_ = watchdog_eat_rx => {
@@ -455,9 +471,7 @@ impl StreamData {
let aud_history = aud_history.clone();
let watchdog_tx = watchdog_tx.clone();
let fps_table = fps_table.clone();
let print_name = print_name.clone();
log::debug!("{print_name}: Running Stream Instance Task");
Box::pin(async move {
// use std::io::Write;
// let mut file = std::fs::File::create("reference.h264")?;
@@ -465,14 +479,10 @@ impl StreamData {
let mut aud_keyframe = false;
let res = async {
let mut prev_ts = Duration::ZERO;
let mut stream_data = camera.start_video(name, 0, strict).await?;
loop {
log::debug!("{print_name}: Waiting for frame");
let data = stream_data.get_data().await??;
log::debug!("{print_name}: Waiting for Watchdog");
watchdog_tx.send(()).await?; // Feed the watchdog
log::debug!("{print_name}: Got frame");
// Update the stream config with any information
match &data {
@@ -488,6 +498,7 @@ impl StreamData {
false
}
});
fps_delta = Duration::from_millis(1000 / (stream_config.borrow().fps as u64));
},
BcMedia::InfoV2(info) => {
stream_config.send_if_modified(|state| {
@@ -501,6 +512,7 @@ impl StreamData {
false
}
});
fps_delta = Duration::from_millis(1000 / (stream_config.borrow().fps as u64));
},
BcMedia::Iframe(frame) => {
stream_config.send_if_modified(|state| {
@@ -556,58 +568,60 @@ impl StreamData {
}
match data {
BcMedia::Iframe(BcMediaIframe{data, microseconds, ..}) => {
prev_ts = Duration::from_micros(microseconds as u64);
// log::debug!("IFrame: {prev_ts:?}");
BcMedia::Iframe(BcMediaIframe{data, ..}) => {
let d = StampedData{
keyframe: true,
data: Arc::new(data),
ts: prev_ts
ts: master_ts,
};
let _ = vid_tx.send(d.clone());
vid_history.send_modify(|history| {
let drop_time = d.ts.saturating_sub(BUFFER_DURATION);
let dts = d.ts;
history.push_back(d);
while history.front().is_some_and(|di| di.ts < drop_time) {
while history.front().is_some_and(|di| di.ts < drop_time || di.ts > dts) || history.len() > BUFFER_SIZE {
history.pop_front();
}
log::info!("history: {}", history.len());
let debug: Vec<Duration> = history.iter().map(|f| f.ts).collect();
log::info!("history ts: {:?}", debug);
});
recieved_iframe = true;
aud_keyframe = true;
log::trace!("Sent Vid Key Frame");
master_ts += fps_delta;
},
BcMedia::Pframe(BcMediaPframe{data, microseconds,..}) if recieved_iframe => {
prev_ts = Duration::from_micros(microseconds as u64);
// log::debug!("PFrame: {prev_ts:?}");
// log::debug!("data: {data:02X?}");
BcMedia::Pframe(BcMediaPframe{data, ..}) if recieved_iframe => {
let d = StampedData{
keyframe: false,
data: Arc::new(data),
ts: prev_ts
ts: master_ts,
};
let _ = vid_tx.send(d.clone());
vid_history.send_modify(|history| {
let drop_time = d.ts.saturating_sub(BUFFER_DURATION);
let dts = d.ts;
history.push_back(d);
while history.front().is_some_and(|di| di.ts < drop_time) {
while history.front().is_some_and(|di| di.ts < drop_time || di.ts > dts) || history.len() > BUFFER_SIZE {
history.pop_front();
}
});
master_ts += fps_delta;
log::trace!("Sent Vid Frame");
}
BcMedia::Aac(BcMediaAac{data, ..}) | BcMedia::Adpcm(BcMediaAdpcm{data,..}) if recieved_iframe => {
// log::debug!("Audio: {prev_ts:?}");
let d = StampedData{
keyframe: aud_keyframe,
data: Arc::new(data),
ts: prev_ts,
ts: master_ts,
};
aud_keyframe = false;
let _ = aud_tx.send(d.clone())?;
aud_history.send_modify(|history| {
let drop_time = d.ts.saturating_sub(BUFFER_DURATION);
let dts = d.ts;
history.push_back(d);
while history.front().is_some_and(|di| di.ts < drop_time) {
while history.front().is_some_and(|di| di.ts < drop_time || di.ts > dts) || history.len() > BUFFER_SIZE {
history.pop_front();
}
});
@@ -617,6 +631,7 @@ impl StreamData {
}
}
}.await;
log::info!("video outgoing queue: {}", vid_tx.len());
Ok(res)
})
}) => {
@@ -639,7 +654,6 @@ impl StreamData {
}
} => v,
};
log::debug!("{print_name}: Stream Thead Stopped: {:?}", r);
r
}));
@@ -647,7 +661,6 @@ impl StreamData {
}
async fn shutdown(&mut self) -> Result<()> {
log::debug!("StreamData::shutdown Cancel");
self.cancel.cancel();
if let Some(handle) = self.handle.take() {
let _ = handle.await;
+12 -12
View File
@@ -1,19 +1,19 @@
use crate::mqtt::Discoveries;
use lazy_static::lazy_static;
use neolink_core::bc_protocol::{DiscoveryMethods, PrintFormat, StreamKind};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::clone::Clone;
use std::collections::HashSet;
use validator::{Validate, ValidationError};
use validator::ValidationError;
use validator_derive::Validate;
lazy_static! {
static ref RE_TLS_CLIENT_AUTH: Regex = Regex::new(r"^(none|request|require)$").unwrap();
static ref RE_PAUSE_MODE: Regex = Regex::new(r"^(black|still|test|none)$").unwrap();
static ref RE_MAXENC_SRC: Regex =
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap();
}
static RE_TLS_CLIENT_AUTH: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^(none|request|require)$").unwrap());
static RE_PAUSE_MODE: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(black|still|test|none)$").unwrap());
static RE_MAXENC_SRC: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap()
});
#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq)]
pub(crate) struct Config {
@@ -37,7 +37,7 @@ pub(crate) struct Config {
pub(crate) mqtt: Option<MqttServerConfig>,
#[validate(regex(
path = "RE_TLS_CLIENT_AUTH",
path = *RE_TLS_CLIENT_AUTH,
message = "Incorrect tls auth",
code = "tls_client_auth"
))]
@@ -159,7 +159,7 @@ pub(crate) struct CameraConfig {
#[serde(default = "default_maxenc")]
#[validate(regex(
path = "RE_MAXENC_SRC",
path = *RE_MAXENC_SRC,
message = "Invalid maximum encryption method",
code = "max_encryption"
))]
@@ -212,7 +212,7 @@ pub(crate) struct CameraConfig {
#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq, Eq, Hash)]
pub(crate) struct UserConfig {
#[validate(custom = "validate_username")]
#[validate(custom(function = "validate_username"))]
#[serde(alias = "username")]
pub(crate) name: String,
@@ -329,7 +329,7 @@ pub(crate) struct PauseConfig {
#[serde(default = "default_pause_mode")]
#[validate(regex(
path = "RE_PAUSE_MODE",
path = *RE_PAUSE_MODE,
message = "Incorrect pause mode",
code = "mode"
))]
+11 -11
View File
@@ -1,16 +1,16 @@
use err_derive::Error;
use thiserror::Error;
#[derive(Debug, Error)]
#[allow(clippy::large_enum_variant)]
pub enum Error {
#[error(display = "RTSP Error")]
Rtsp(#[error(source)] super::rtsp::Error),
#[error(display = "Status LED Error")]
StatusLight(#[error(source)] super::statusled::Error),
#[error(display = "Reboot Error")]
Reboot(#[error(source)] super::reboot::Error),
#[error(display = "Talk Error")]
Talk(#[error(source)] super::talk::Error),
#[error(display = "Talk Error")]
Mqtt(#[error(source)] super::mqtt::Error),
#[error("RTSP Error")]
Rtsp(#[from] super::rtsp::Error),
#[error("Status LED Error")]
StatusLight(#[from] super::statusled::Error),
#[error("Reboot Error")]
Reboot(#[from] super::reboot::Error),
#[error("Talk Error")]
Talk(#[from] super::talk::Error),
#[error("Talk Error")]
Mqtt(#[from] super::mqtt::Error),
}
+4 -2
View File
@@ -1,7 +1,9 @@
use std::path::Path;
use anyhow::{anyhow, Context, Result};
use gstreamer::{parse_launch, prelude::*, ClockTime, MessageView, Pipeline, State};
use gstreamer::{
parse::launch_full, prelude::*, ClockTime, MessageView, ParseFlags, Pipeline, State,
};
use gstreamer_app::AppSrc;
use tokio::{
sync::{
@@ -203,7 +205,7 @@ fn create_pipeline(format: VidFormat, file_path: &Path) -> Result<Pipeline> {
// Parse the pipeline we want to probe from a static in-line string.
// Here we give our audiotestsrc a name, so we can retrieve that element
// from the resulting pipeline.
let pipeline = parse_launch(&launch_str)
let pipeline = launch_full(&launch_str, None, ParseFlags::empty())
.context("Unable to load gstreamer pipeline ensure all gstramer plugins are installed")?;
let pipeline = pipeline.dynamic_cast::<Pipeline>().map_err(|_| {
anyhow!("Unable to create gstreamer pipeline ensure all gstramer plugins are installed")
+1 -2
View File
@@ -47,8 +47,7 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
let vid_type = stream_config
.wait_for(|config| config.vid_ready())
.await?
.vid_format
.clone();
.vid_format;
let mut stream = BroadcastStream::new(stream_data.vid.resubscribe())
.filter(|f| futures::future::ready(f.is_ok())); // Filter to ignore lagged
let buf = loop {
+5 -17
View File
@@ -20,7 +20,7 @@
//! You should have received a copy of the GNU General Public License along with this program. If
//! not, see <https://www.gnu.org/licenses/>.
//!
//! Neolink source code is available online at <https://github.com/thirtythreeforty/neolink>
//! Neolink source code is available online at <https://github.com/QuantumEntangledAndy/neolink>
//!
#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;
@@ -46,6 +46,7 @@ mod pir;
mod ptz;
mod reboot;
mod rtsp;
mod services;
mod statusled;
mod talk;
mod utils;
@@ -53,21 +54,9 @@ mod utils;
use cmdline::{Command, Opt};
use common::NeoReactor;
use config::Config;
use console_subscriber as _;
pub(crate) type AnyResult<T> = Result<T, anyhow::Error>;
#[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<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
@@ -91,10 +80,6 @@ async fn main() -> Result<()> {
.validate()
.with_context(|| format!("Failed to validate the {:?} config file", conf_path))?;
if config.tokio_console {
tokio_console_enable();
}
let neo_reactor = NeoReactor::new(config.clone()).await;
match opt.cmd {
@@ -138,6 +123,9 @@ async fn main() -> Result<()> {
Some(Command::Battery(opts)) => {
battery::main(opts, neo_reactor.clone()).await?;
}
Some(Command::Services(opts)) => {
services::main(opts, neo_reactor.clone()).await?;
}
}
Ok(())
+18 -27
View File
@@ -123,7 +123,7 @@ pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> {
config_names = thread_config.borrow().clone().cameras.iter().filter(|a| a.enabled).map(|cam_config| cam_config.name.clone()).collect::<HashSet<_>>();
for name in config_names.iter() {
log::info!("{name}: MQTT Staring");
log::info!("{name}: MQTT Starting");
if ! cameras.contains_key(name) {
let local_cancel = CancellationToken::new();
cameras.insert(name.clone(),local_cancel.clone());
@@ -150,7 +150,6 @@ pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> {
if let Ok(()) = &r {
break r
} else {
log::debug!("listen_on_camera stopped: {:?}", r);
continue;
}
}
@@ -160,7 +159,6 @@ pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> {
for (running_name, token) in cameras.iter() {
if ! config_names.contains(running_name) {
log::debug!("Mqtt::main Cancel");
token.cancel();
}
}
@@ -329,7 +327,6 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
tokio::select!{
_ = cancel_msg.cancelled() => AnyResult::Ok(()),
v = async {
// log::debug!("Got message: {msg:?}");
let res = handle_mqtt_message(msg, &mqtt_msg, &camera_msg).await;
if res.is_err() {
tx.send(res).await?;
@@ -339,14 +336,11 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
}
});
}
log::debug!("STOPPED Listening to message on {}", mqtt_msg.get_name());
AnyResult::Ok(())
} => {
log::debug!("MQTT Command Returned: {v:?}");
v
},
v = rx.recv() => {
log::debug!("MQTT Task Returned: {v:?}");
v.ok_or(anyhow!("All error senders were dropped"))?
},
}?;
@@ -370,7 +364,6 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
})?;
}
} => {
log::debug!("CamConnection returned: {v:?}");
v
},
// Handle the floodlight
@@ -413,7 +406,6 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
AnyResult::Ok(())
} => v,
};
log::debug!("Flood light returned: {v:?}");
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
Ok(()) => AnyResult::Ok(()),
@@ -441,7 +433,6 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
})?;
AnyResult::Ok(())
}.await;
log::debug!("Motion returned: {v:?}");
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
Ok(()) => AnyResult::Ok(()),
@@ -467,7 +458,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
}).await;
let image = match image {
Err(e) => match e.downcast::<neolink_core::Error>() {
Ok(neolink_core::Error::CameraServiceUnavaliable(_)) => {
Ok(neolink_core::Error::CameraServiceUnavailable(_)) => {
log::debug!("Image not supported");
futures::future::pending().await
},
@@ -485,7 +476,6 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
}
AnyResult::Ok(())
}.await;
log::debug!("Snap returned: {v:?}");
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
Ok(()) => AnyResult::Ok(()),
@@ -512,7 +502,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
}).await;
let xml = match xml {
Err(e) => match e.downcast::<neolink_core::Error>() {
Ok(neolink_core::Error::CameraServiceUnavaliable(_)) => {
Ok(neolink_core::Error::CameraServiceUnavailable(_)) => {
log::debug!("Battery not supported");
futures::future::pending().await
},
@@ -530,7 +520,6 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
}
AnyResult::Ok(())
}.await;
log::debug!("Battery returned: {v:?}");
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
Ok(()) => AnyResult::Ok(()),
@@ -554,7 +543,6 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
prev_noti = noti;
AnyResult::Ok(())
}.await;
log::debug!("PushNoti returned: {v:?}");
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
Ok(()) => AnyResult::Ok(()),
@@ -608,7 +596,6 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
};
};
log::debug!("Mqtt::listen_on_camera Cancel: {r:?}");
drop(drop_cancel);
r?;
Ok(())
@@ -1057,10 +1044,8 @@ async fn handle_mqtt_message(
.run_task(|_cam| Box::pin(async move { AnyResult::Ok(()) }))
.await;
log::debug!("Wakeup counting down");
sleep(Duration::from_secs(secs * 60)).await;
log::debug!("Wakeup complete");
drop(permit);
});
"OK"
@@ -1156,9 +1141,11 @@ async fn handle_mqtt_message(
"FAIL"
}
Ok(xml) => {
let bytes_res =
yaserde::ser::serialize_with_writer(&xml, vec![], &Default::default());
match bytes_res {
let ser_xml = {
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &xml).map(|_| buf.to_vec())
};
match ser_xml {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => {
mqtt.send_message("status/battery", &str, false)
@@ -1200,9 +1187,11 @@ async fn handle_mqtt_message(
"FAIL"
}
Ok(xml) => {
let bytes_res =
yaserde::ser::serialize_with_writer(&xml, vec![], &Default::default());
match bytes_res {
let ser_xml = {
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &xml).map(|_| buf.to_vec())
};
match ser_xml {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => {
mqtt.send_message("status/pir", &str, false)
@@ -1245,9 +1234,11 @@ async fn handle_mqtt_message(
"FAIL"
}
Ok(xml) => {
let bytes_res =
yaserde::ser::serialize_with_writer(&xml, vec![], &Default::default());
match bytes_res {
let ser_xml = {
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &xml).map(|_| buf.to_vec())
};
match ser_xml {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => {
mqtt.send_message("status/ptz", &str, false)
+4 -4
View File
@@ -6,8 +6,8 @@ use anyhow::{anyhow, Context, Result};
use futures::future::FutureExt;
use log::*;
use rumqttc::{
AsyncClient, ConnectReturnCode, Event, Incoming, Key, LastWill, MqttOptions, QoS,
TlsConfiguration, Transport,
AsyncClient, ConnectReturnCode, Event, Incoming, LastWill, MqttOptions, QoS, TlsConfiguration,
Transport,
};
use std::sync::Arc;
use tokio::task::JoinSet;
@@ -140,7 +140,7 @@ impl<'a> MqttBackend<'a> {
if let (Ok(cert_buf), Ok(key_buf)) =
(std::fs::read(cert_path), std::fs::read(key_path))
{
Some((cert_buf, Key::RSA(key_buf)))
Some((cert_buf, key_buf))
} else {
error!("Failed to set client tls");
None
@@ -534,7 +534,7 @@ impl LastWillMqtt {
if let (Ok(cert_buf), Ok(key_buf)) =
(std::fs::read(cert_path), std::fs::read(key_path))
{
Some((cert_buf, Key::RSA(key_buf)))
Some((cert_buf, key_buf))
} else {
error!("Failed to set client tls");
None
+5 -2
View File
@@ -47,8 +47,11 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
})
.await?;
let pir_ser = String::from_utf8(
yaserde::ser::serialize_with_writer(&pir_state, vec![], &Default::default())
.expect("Should Ser the struct"),
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &pir_state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", pir_ser);
+47 -34
View File
@@ -1,7 +1,6 @@
use anyhow::{anyhow, Context, Result};
use gstreamer::{prelude::*, Bin, Caps, Element, ElementFactory, GhostPad};
use gstreamer_app::{AppSrc, AppSrcCallbacks, AppStreamType};
use log::*;
use tokio::sync::mpsc::{channel as mpsc, Receiver as MpscReceiver};
use crate::{
@@ -115,7 +114,6 @@ fn clear_bin(bin: &Element) -> Result<()> {
.dynamic_cast::<Bin>()
.map_err(|_| anyhow!("Media source's element should be a bin"))?;
// Clear the autogenerated ones
log::debug!("Clearing old elements");
for element in bin.iterate_elements().into_iter().flatten() {
bin.remove(&element)?;
}
@@ -159,7 +157,10 @@ fn build_unknown(bin: &Element, pattern: &str) -> Result<()> {
fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
let buffer_size = buffer_size(stream_config.bitrate);
log::debug!("buffer_size: {buffer_size}");
log::info!(
"buffer_size: {buffer_size}, bitrate: {}",
stream_config.bitrate
);
let bin = bin
.clone()
.dynamic_cast::<Bin>()
@@ -169,12 +170,12 @@ fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_is_live(true);
source.set_is_live(false);
source.set_block(false);
source.set_min_latency(0);
source.set_min_latency(1000 / (stream_config.fps as i64));
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_do_timestamp(true);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Seekable);
let source = source
@@ -182,10 +183,10 @@ fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
.map_err(|_| anyhow!("Cannot cast back"))?;
let queue = make_queue("source_queue", buffer_size)?;
let parser = make_element("h264parse", "parser")?;
let stamper = make_element("h264timestamper", "stamper")?;
// let stamper = make_element("h264timestamper", "stamper")?;
let payload = make_element("rtph264pay", "pay0")?;
bin.add_many([&source, &queue, &parser, &stamper, &payload])?;
Element::link_many([&source, &queue, &parser, &stamper, &payload])?;
bin.add_many([&source, &queue, &parser, &payload])?;
Element::link_many([&source, &queue, &parser, &payload])?;
let source = source
.dynamic_cast::<AppSrc>()
@@ -195,7 +196,6 @@ fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
let buffer_size = buffer_size(stream_config.bitrate);
log::debug!("buffer_size: {buffer_size}");
let bin = bin
.clone()
.dynamic_cast::<Bin>()
@@ -204,12 +204,12 @@ fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
let source = make_element("appsrc", "vidsrc")?
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_is_live(true);
source.set_is_live(false);
source.set_block(false);
source.set_min_latency(0);
source.set_min_latency(1000 / (stream_config.fps as i64));
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_do_timestamp(true);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Seekable);
let source = source
@@ -217,10 +217,10 @@ fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
.map_err(|_| anyhow!("Cannot cast back"))?;
let queue = make_queue("source_queue", buffer_size)?;
let parser = make_element("h265parse", "parser")?;
let stamper = make_element("h265timestamper", "stamper")?;
// let stamper = make_element("h265timestamper", "stamper")?;
let payload = make_element("rtph265pay", "pay0")?;
bin.add_many([&source, &queue, &parser, &stamper, &payload])?;
Element::link_many([&source, &queue, &parser, &stamper, &payload])?;
bin.add_many([&source, &queue, &parser, &payload])?;
Element::link_many([&source, &queue, &parser, &payload])?;
let source = source
.dynamic_cast::<AppSrc>()
@@ -229,8 +229,7 @@ fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
}
fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
let buffer_size = buffer_size(stream_config.bitrate);
log::debug!("buffer_size: {buffer_size}");
let buffer_size = 512 * 6;
let bin = bin
.clone()
.dynamic_cast::<Bin>()
@@ -240,12 +239,12 @@ fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_is_live(true);
source.set_is_live(false);
source.set_block(false);
source.set_min_latency(0);
source.set_min_latency(1000 / (stream_config.fps as i64));
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_do_timestamp(true);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Seekable);
let source = source
@@ -295,8 +294,7 @@ fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
}
fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) -> Result<AppSrc> {
let buffer_size = buffer_size(stream_config.bitrate);
log::debug!("buffer_size: {buffer_size}");
let buffer_size = 512 * 6;
let bin = bin
.clone()
.dynamic_cast::<Bin>()
@@ -312,12 +310,12 @@ fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) ->
let source = make_element("appsrc", "audsrc")?
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_is_live(true);
source.set_is_live(false);
source.set_block(false);
source.set_min_latency(0);
source.set_min_latency(1000 / (stream_config.fps as i64));
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_do_timestamp(true);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Seekable);
source.set_caps(Some(
@@ -342,7 +340,6 @@ fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) ->
Element::link_many([&source, &queue, &decoder])?;
Element::link_many([&encoder, &payload])?;
decoder.connect_pad_added(move |_element, pad| {
debug!("Linking encoder to decoder: {:?}", pad.caps());
let sink_pad = encoder
.static_pad("sink")
.expect("Encoder is missing its pad");
@@ -387,7 +384,9 @@ fn make_element(kind: &str, name: &str) -> AnyResult<Element> {
)
})
}
fn make_queue(name: &str, buffer_size: u32) -> AnyResult<Element> {
#[allow(dead_code)]
fn make_dbl_queue(name: &str, buffer_size: u32) -> AnyResult<Element> {
let queue = make_element("queue", &format!("queue1_{}", name))?;
queue.set_property("max-size-bytes", buffer_size);
queue.set_property("max-size-buffers", 0u32);
@@ -402,11 +401,11 @@ fn make_queue(name: &str, buffer_size: u32) -> AnyResult<Element> {
queue2.set_property("max-size-bytes", buffer_size * 2u32 / 3u32);
queue.set_property("max-size-buffers", 0u32);
queue.set_property("max-size-time", 0u64);
// queue2.set_property(
// "max-size-time",
// std::convert::TryInto::<u64>::try_into(tokio::time::Duration::from_secs(5).as_nanos())
// .unwrap_or(0),
// );
queue2.set_property(
"max-size-time",
std::convert::TryInto::<u64>::try_into(tokio::time::Duration::from_secs(5).as_nanos())
.unwrap_or(0),
);
queue2.set_property("use-buffering", false);
let bin = gstreamer::Bin::builder().name(name).build();
@@ -433,6 +432,20 @@ fn make_queue(name: &str, buffer_size: u32) -> AnyResult<Element> {
Ok(bin)
}
fn make_queue(name: &str, buffer_size: u32) -> AnyResult<Element> {
let queue = make_element("queue", &format!("queue1_{}", name))?;
queue.set_property("max-size-bytes", buffer_size);
queue.set_property("max-size-buffers", 0u32);
queue.set_property("max-size-time", 0u64);
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 buffer_size(bitrate: u32) -> u32 {
std::cmp::max(bitrate * 15u32 / 8u32, 4u32 * 1024u32 * 1024u32)
// 0.1 seconds (according to bitrate) or 4kb what ever is larger
std::cmp::max(bitrate * 2 / 8u32, 4u32 * 1024u32)
}
-1
View File
@@ -5,7 +5,6 @@
use super::AnyResult;
use gstreamer::glib::object_subclass;
use gstreamer::glib::subclass::types::ObjectSubclass;
use gstreamer::Element;
use gstreamer::{
glib::{self, Object},
+7 -2
View File
@@ -7,7 +7,7 @@ use super::AnyResult;
use crate::config::*;
use anyhow::Context;
use gstreamer::glib::{self, object_subclass, subclass::types::ObjectSubclass, MainLoop, Object};
use gstreamer::glib::{self, object_subclass, MainLoop, Object};
use gstreamer_rtsp::RTSPAuthMethod;
use gstreamer_rtsp_server::{
gio::{TlsAuthenticationMode, TlsCertificate},
@@ -57,7 +57,12 @@ impl NeoRtspServer {
factory.connect_client_connected(|_, client| {
client.connect_new_session(|_, session| {
log::debug!("New Session");
session.set_timeout(5);
// Session timeout too small causes us to drop
// some ffmpeg clients too soon
// Too long causes too many open connections with
// clients like frigate (that seem to open multiple
// connections without shutting down old ones)
session.set_timeout(30);
});
});
+3 -11
View File
@@ -104,7 +104,7 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> {
loop {
thread_config.changed().await?;
if let Err(e) = thread_rtsp.set_up_tls(&thread_config.borrow().clone()) {
log::error!("Could not seup TLS: {e}");
log::error!("Could not setup TLS: {e}");
}
}
} => v
@@ -128,7 +128,7 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> {
let config = thread_config.borrow().clone();
if let Err(e) = apply_users(&thread_rtsp, &curr_users).await {
log::error!("Could not seup TLS: {e}");
log::error!("Could not setup TLS: {e}");
}
if config.certificate.is_none() && !curr_users.is_empty() {
@@ -162,7 +162,7 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> {
for name in config_names.iter() {
if ! cameras.contains_key(name) {
log::info!("{name}: Rtsp Staring");
log::info!("{name}: Rtsp Starting");
let local_cancel = CancellationToken::new();
cameras.insert(name.clone(),local_cancel.clone() );
let thread_global_cancel = thread_cancel2.clone();
@@ -186,7 +186,6 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> {
for (running_name, token) in cameras.iter() {
if ! config_names.contains(running_name) {
log::debug!("Rtsp::main Cancel1");
token.cancel();
}
}
@@ -217,7 +216,6 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> {
// Panicked or error in task
// Cancel all and await terminate
log::error!("Error: {e}");
log::debug!("Rtsp::main Cancel2");
global_cancel.cancel();
rtsp.quit().await?;
}
@@ -291,8 +289,6 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
AnyResult::Ok(())
});
log::debug!("{name}: Camera Main::Loop");
let mut camera_config = camera.config().await?.clone();
loop {
let prev_stream_config = camera_config.borrow_and_update().stream;
@@ -316,7 +312,6 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
},
v = async {
// This select handles enabling the right stream
log::debug!("{name}: Camera Main::Select Stream");
// and setting up the users
let all_users = rtsp.get_users().await?.iter().filter(|a| *a != "anyone" && *a != "anonymous").cloned().collect::<HashSet<_>>();
let permitted_users: HashSet<String> = match &prev_stream_users {
@@ -340,7 +335,6 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
let mut supported_streams_3 = supported_streams.clone();
tokio::select! {
v = async {
log::debug!("{name}: Camera Main::Select Main");
let name = camera.config().await?.borrow().name.clone();
let mut paths = vec![
format!("/{name}/main"),
@@ -371,7 +365,6 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
stream_main(camera.stream(StreamKind::Main).await?, camera.clone(), rtsp, &permitted_users, &paths).await
}, if active_streams.contains(&StreamKind::Main) => v,
v = async {
log::debug!("{name}: Camera Main::Select Sub");
let name = camera.config().await?.borrow().name.clone();
let mut paths = vec![
format!("/{name}/sub"),
@@ -406,7 +399,6 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
stream_main(camera.stream(StreamKind::Sub).await?,camera.clone(), rtsp, &permitted_users, &paths).await
}, if active_streams.contains(&StreamKind::Sub) => v,
v = async {
log::debug!("{name}: Camera Main::Select Extern");
let name = camera.config().await?.borrow().name.clone();
let mut paths = vec![
format!("/{name}/extern"),
+284 -99
View File
@@ -2,17 +2,17 @@ use anyhow::{anyhow, Result};
use gstreamer::{prelude::*, ClockTime, FlowError};
use gstreamer_app::AppSrc;
use gstreamer_rtsp_server::prelude::*;
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::{
sync::{broadcast::channel as broadcast, watch::channel as watch},
task::JoinSet,
time::{sleep, sleep_until, Duration, Instant},
time::{interval, sleep, Duration},
};
use tokio_stream::{wrappers::BroadcastStream, Stream, StreamExt};
use tokio_util::sync::CancellationToken;
use crate::common::{Permit, StampedData, UseCounter};
use crate::common::{Permit, StampedData, UseCounter, VidFormat};
use crate::{
common::{NeoInstance, StreamConfig, StreamInstance},
AnyResult,
@@ -43,27 +43,18 @@ pub(super) async fn stream_main(
let this_loop_cancel = CancellationToken::new();
let _drop_guard = this_loop_cancel.clone().drop_guard();
log::debug!("{}: Activating Stream", &name);
stream_instance.activate().await?;
// Wait for a valid stream format to be detected
log::debug!("{}: Waiting for Valid Stream", &name);
stream_instance
.config
.wait_for(|config| {
log::debug!("{:?}", config);
config.vid_ready()
})
.wait_for(|config| config.vid_ready())
.await?;
log::debug!("{}: Waiting for Valid Audio", &name);
// After vid give it 1s to look for audio
// Ignore timeout but check err
if let Ok(v) = tokio::time::timeout(
Duration::from_secs(1),
stream_instance.config.wait_for(|config| {
log::debug!("{:?}", config);
config.aud_ready()
}),
stream_instance.config.wait_for(|config| config.aud_ready()),
)
.await
{
@@ -83,7 +74,6 @@ pub(super) async fn stream_main(
let pause_affector_tx = Arc::new(pause_affector_tx);
let mut set = JoinSet::<AnyResult<()>>::new();
log::debug!("{}: Creating Client Counters", &name);
// Handles the on off of the stream with the client pause
let client_counter = UseCounter::new().await;
let client_count = client_counter.create_deactivated().await?;
@@ -98,7 +88,6 @@ pub(super) async fn stream_main(
tokio::select! {
_ = cancel.cancelled() => AnyResult::Ok(()),
v = async {
log::debug!("{}: Activating Client Pause", thread_name);
loop {
client_count.aquired_users().await?;
log::info!("{}: Enabling Client", thread_name);
@@ -130,7 +119,6 @@ pub(super) async fn stream_main(
tokio::select! {
_ = cancel.cancelled() => AnyResult::Ok(()),
v = async {
log::debug!("{}: Activating Motion Pause", &thread_name);
loop {
motion
.wait_for(|md| matches!(md, crate::common::MdState::Start(_)))
@@ -155,7 +143,6 @@ pub(super) async fn stream_main(
});
// Push notfications
log::debug!("{}: Activating Push Notification Pause", &name);
let mut pn = camera.push_notifications().await?;
let mut curr_pn = None;
let thread_name = name.clone();
@@ -260,14 +247,12 @@ pub(super) async fn stream_main(
let cancel = this_loop_cancel.clone();
let counter = client_counter.create_deactivated().await?;
let mut cur_count = 0;
let thread_name = name.clone();
set.spawn(async move {
tokio::select! {
_ = cancel.cancelled() => AnyResult::Ok(()),
v = async {
loop {
cur_count = *counter.get_counter().wait_for(|v| v != &cur_count).await?;
log::debug!("{thread_name}: Number of rtsp clients: {cur_count}");
}
} => v,
}
@@ -275,7 +260,6 @@ pub(super) async fn stream_main(
// This runs the actual stream.
// The select will restart if the stream's config updates
log::debug!("{}: Stream Activated", &name);
break tokio::select! {
v = thread_stream_config.wait_for(|new_conf| new_conf != &last_stream_config) => {
let v = v?;
@@ -323,14 +307,13 @@ async fn stream_run(
log::debug!("Path: {}", path);
mounts.add_factory(path, factory.clone());
}
log::info!("{}: Avaliable at {}", name, paths.join(", "));
log::info!("{}: Available at {}", name, paths.join(", "));
let stream_cancel = CancellationToken::new();
let drop_guard = stream_cancel.clone().drop_guard();
let mut set = JoinSet::new();
// Wait for new media client data to come in from the factory
while let Some(mut client_data) = client_rx.recv().await {
log::debug!("New media");
// New media created
let vid = client_data.vid.take().map(|data| data.app);
let aud = client_data.aud.take().map(|data| data.app);
@@ -428,10 +411,10 @@ async fn stream_run(
let vid_data_rx = BroadcastStream::new(vid_data_rx).filter(|f| f.is_ok()); // Filter to ignore lagged
let thread_vid = vid.clone();
let mut thread_client_count = client_count.subscribe();
log::debug!("stream_config.fps: {}", stream_config.fps);
let thread_format = stream_config.vid_format;
// let fallback_time = Duration::from_secs(3);
// let fallback_framerate =
// Duration::from_millis(1000u64 / std::cmp::max(stream_config.fps as u64, 5u64));
let framerate =
Duration::from_millis(1000u64 / std::cmp::max(stream_config.fps as u64, 5u64));
if let Some(thread_vid) = thread_vid {
set.spawn(async move {
thread_client_count.activate().await?;
@@ -440,24 +423,28 @@ async fn stream_run(
AnyResult::Ok(())
},
v = send_to_appsrc(
// repeat_keyframe(
frametime_stream(
hold_stream(
wait_for_keyframe(
vid_data_rx,
)
)
pad_vid(
insert_filler(
frametime_stream(
// ensure_order(
wait_for_keyframe(
vid_data_rx,
),
// ),
framerate
),
thread_format,
framerate,
),
// fallback_time,
// fallback_framerate,
// ),
&thread_vid) => {
thread_format,
),
&thread_vid
) => {
v
},
};
drop(thread_client_count);
let _ = thread_vid.end_of_stream();
log::debug!("Vid Thread End: {:?}", r);
r
});
}
@@ -466,6 +453,8 @@ async fn stream_run(
let thread_stream_cancel = stream_cancel.clone();
let aud_data_rx = BroadcastStream::new(aud_data_rx).filter(|f| f.is_ok()); // Filter to ignore lagged
let thread_aud = aud.clone();
let aud_framerate =
Duration::from_millis(1000u64 / std::cmp::max(stream_config.fps as u64, 5u64));
if let Some(thread_aud) = thread_aud {
set.spawn(async move {
let r = tokio::select! {
@@ -474,22 +463,21 @@ async fn stream_run(
},
v = send_to_appsrc(
frametime_stream(
hold_stream(
// ensure_order(
wait_for_keyframe(
aud_data_rx
)
)
), &thread_aud) => {
),
// ),
aud_framerate),
&thread_aud) => {
v
},
};
let _ = thread_aud.end_of_stream();
log::debug!("Aud Thread End: {:?}", r);
r
});
}
}
log::debug!("Cleaning up streams");
// At this point the factory has been destroyed
// Cancel any remaining threads that are trying to send data
// Although it should be finished already when the appsrcs are dropped
@@ -501,23 +489,20 @@ async fn stream_run(
}
fn check_live(app: &AppSrc) -> Result<()> {
// log::debug!("Checking Live: {:?}", app.bus());
app.bus().ok_or(anyhow!("App source is closed"))?;
app.pads()
.iter()
.all(|pad| pad.is_linked())
.then_some(())
.ok_or(anyhow!("App source is closed"))
.ok_or(anyhow!("App source is not linked"))
}
#[allow(dead_code)]
fn get_runtime(app: &AppSrc) -> Option<Duration> {
if let Some(clock) = app.clock() {
if let Some(time) = clock.time() {
// log::debug!("time: {time:?}");
if let Some(base_time) = app.base_time() {
// log::debug!("base_time: {base_time:?}");
let runtime = time.saturating_sub(base_time);
// log::debug!("runtime: {runtime:?}");
return Some(Duration::from_micros(runtime.useconds()));
}
}
@@ -542,6 +527,7 @@ fn wait_for_keyframe<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
})
}
#[allow(dead_code)]
// Take a stream of stamped data and release them
// in waves when a new key frame is found
// this ensure that the last frame sent is always an IFrame
@@ -554,11 +540,9 @@ fn hold_stream<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
if let Ok(frame) = frame {
if frame.keyframe {
// Release
// log::debug!("Yielding: {}", held_frames.len());
for held_frame in held_frames.drain(..) {
yield Ok(held_frame);
}
// log::debug!("Yielded");
yield Ok(frame);
} else {
// Hold
@@ -569,39 +553,160 @@ fn hold_stream<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
})
}
// Take a stream of stamped data pause until
// it is time to display it
fn frametime_stream<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
// Take a stream of stamped data and reorder it
// in case they are out of order
// this also releases frames in waves of keyframe so it should replace `hold_stream`
#[allow(dead_code)]
fn ensure_order<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
Box::pin(async_stream::stream! {
const MIN_FPS_DELTA: Duration = Duration::from_millis(1000/5);
let mut last_release = Instant::now();
let mut cached_prev_ts = None;
let mut frame_buffer: Vec<StampedData> = vec![];
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
let curr_ts = frame.ts;
let mut prev_ts = cached_prev_ts.unwrap_or(curr_ts);
if ! frame.keyframe {
// Buffer until keyframe and reorder
let mut reorder_buffer = vec![];
while frame_buffer.last().is_some_and(|v| v.ts > frame.ts) {
reorder_buffer.push(frame_buffer.pop().unwrap());
}
frame_buffer.push(frame);
// Check if we have gone backwards
if curr_ts < prev_ts {
// If we have reset things
prev_ts = curr_ts;
while let Some(frame) = reorder_buffer.pop() {
frame_buffer.push(frame);
}
} else {
// On key frame flush out
for frame in frame_buffer.drain(..) {
yield Ok(frame);
}
yield(Ok(frame));
}
}
}
})
}
let delta_ts = std::cmp::min(curr_ts - prev_ts, MIN_FPS_DELTA);
// log::debug!("curr_ts: {curr_ts:?}, {prev_ts:?} delta_ts: {delta_ts:?}");
sleep_until(last_release + delta_ts).await;
last_release = Instant::now();
cached_prev_ts = Some(curr_ts);
// Take a stream of stamped data pause until
// it is time to display it
#[allow(dead_code)]
fn frametime_stream<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
expected_frame_rate: Duration,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
Box::pin(async_stream::stream! {
let mut waiter = interval(expected_frame_rate);
waiter.set_missed_tick_behavior( tokio::time::MissedTickBehavior::Skip);
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
waiter.tick().await;
yield Ok(frame);
}
}
})
}
/// Insert filler into the video stream when no frames are comming
/// this should help the stream not be considered dead while
/// we wait for the reconnect
fn insert_filler<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
format: VidFormat,
delay: Duration,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
let mut last_ts = Duration::ZERO;
Box::pin(async_stream::stream! {
while let Some(frame) = tokio::select!{
v = stream.next() => {
v
},
_ = tokio::time::sleep(delay) => {
match format {
VidFormat::H264 => {
Some(Ok(StampedData {
data: Arc::new(h264_filler(4096)),
keyframe: false,
ts: last_ts,
}))
}
VidFormat::H265 => {
Some(Ok(StampedData {
data: Arc::new(h265_filler(4096)),
keyframe: false,
ts: last_ts,
}))
}
VidFormat::None => unreachable!(),
}
}
} {
if let Ok(frame) = frame {
last_ts = frame.ts;
yield Ok(frame);
}
}
})
}
fn h265_filler(size: usize) -> Vec<u8> {
assert!(size >= 6);
let mut buf = vec![0x0, 0x0, 0x1, 0b01001100, 0x0];
buf.resize(size, 0xFF);
buf[size - 1] = 0x80;
buf
}
fn h264_filler(size: usize) -> Vec<u8> {
assert!(size >= 5);
let mut buf = vec![0x0, 0x0, 0x1, 0xC];
buf.resize(size, 0xFF);
buf[size - 1] = 0x80;
buf
}
/// Takes a stream and pads it with filler blocks up to 4kb in h265 or h264 format
fn pad_vid<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
format: VidFormat,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
let min_pad: usize = match format {
VidFormat::H264 => 5,
VidFormat::H265 => 6,
VidFormat::None => unreachable!(),
};
Box::pin(async_stream::stream! {
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
if (frame.data.len() % 4096) != 0 {
let pad_size: usize = (frame.data.len() + min_pad).div_ceil(4096) * 4096 - frame.data.len();
let frame = StampedData {
keyframe: frame.keyframe,
data: Arc::new(
match format {
VidFormat::H264 => {
frame.data.iter().chain(
h264_filler(pad_size).iter()
).copied().collect()
}
VidFormat::H265 => {
frame.data.iter().chain(
h265_filler(pad_size).iter()
).copied().collect()
}
VidFormat::None => unreachable!(),
}
),
ts: frame.ts,
};
yield Ok(frame);
} else {
yield Ok(frame);
}
}
}
})
}
#[allow(dead_code)]
// This will take a stream and if there is a notibable lack of data
// then it will repeat the last keyframe (if there have been no
@@ -616,7 +721,6 @@ fn repeat_keyframe<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
if frame.keyframe {
// log::debug!("Key Frame");
let repeater = frame.clone();
yield Ok(frame);
@@ -628,7 +732,6 @@ fn repeat_keyframe<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
if let Some(frame) = v {
if let Ok(frame) = frame {
if was_repeating {
log::debug!("Key Frame: Resume");
was_repeating = false;
}
yield Ok(frame);
@@ -641,7 +744,6 @@ fn repeat_keyframe<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
_ = sleep(fallback_time) => {
if !was_repeating {
// This way we only print once
log::debug!("Inserting Skip Frames");
was_repeating = true;
}
fallback_time = frame_rate;
@@ -664,34 +766,117 @@ async fn send_to_appsrc<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
appsrc: &AppSrc,
) -> AnyResult<()> {
let mut rt = Duration::ZERO;
while let Some(Ok(data)) = stream.next().await {
check_live(appsrc)?; // Stop if appsrc is dropped
if let Some(rt_i) = get_runtime(appsrc) {
rt = rt_i;
}
let buf = {
let mut gst_buf = gstreamer::Buffer::with_size(data.data.len()).unwrap();
{
let gst_buf_mut = gst_buf.get_mut().unwrap();
// log::debug!("Setting PTS: {ts:?}, Runtime: {ts:?}");
let time = ClockTime::from_useconds(rt.as_micros() as u64);
gst_buf_mut.set_dts(time);
gst_buf_mut.set_pts(time);
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
gst_buf_data.copy_from_slice(data.data.as_slice());
}
gst_buf
};
let mut ts_0 = Duration::MAX;
let mut wait_for_iframe = true;
let mut pools: HashMap<usize, gstreamer::BufferPool> = Default::default();
let mut keyframes_pushed = 0;
let mut inited = false;
// appsrc.set_state(gstreamer::State::Paused).unwrap();
match appsrc.push_buffer(buf) {
Ok(_) => Ok(()),
Err(FlowError::Flushing) => {
// Buffer is full just skip
Ok(())
let (tx, mut rx) = tokio::sync::mpsc::channel::<StampedData>(2000);
// Run blocking code on a seperate thread
let appsrc = appsrc.clone();
std::thread::spawn(move || {
let r = (move || {
while let Some(data) = rx.blocking_recv() {
check_live(&appsrc)?; // Stop if appsrc is dropped
if wait_for_iframe && !data.keyframe {
continue;
} else if wait_for_iframe {
wait_for_iframe = false;
}
if ts_0 > data.ts {
ts_0 = data.ts;
}
let rt = data.ts - ts_0;
// log::info!("rt: {:?}", rt);
let buf = {
// let mut gst_buf = pool.acquire_buffer(None).unwrap();
let msg_size = data.data.len();
let pool = pools.entry(msg_size).or_insert_with_key(|size| {
let pool = gstreamer::BufferPool::new();
let mut pool_config = pool.config();
pool_config.set_params(None, (*size) as u32, 8, 0);
pool.set_config(pool_config).unwrap();
// let (allocator, alloc_parms) = pool.allocator().unwrap();
pool.set_active(true).unwrap();
pool
});
let mut gst_buf = pool.acquire_buffer(None).unwrap();
// let mut gst_buf = gstreamer::Buffer::with_size(data.data.len()).unwrap();
{
let gst_buf_mut = gst_buf.get_mut().unwrap();
let time = ClockTime::from_useconds(rt.as_micros() as u64);
// gst_buf_mut.set_dts(ClockTime::from_useconds(dts));
gst_buf_mut.set_dts(time);
gst_buf_mut.set_pts(time);
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
gst_buf_data.copy_from_slice(data.data.as_slice());
}
gst_buf
};
match appsrc.push_buffer(buf) {
Ok(_) => {
// log::info!(
// "Send {}{} on {}",
// data.data.len(),
// if data.keyframe { " (keyframe)" } else { "" },
// appsrc.name()
// );
Ok(())
}
Err(FlowError::Flushing) => {
// Buffer is full just skip
//
// But ensure we start with an iframe to reduce gray screens
wait_for_iframe = true;
log::info!("Buffer full on {}", appsrc.name());
Ok(())
}
Err(e) => Err(anyhow!("Error in streaming: {e:?}")),
}?;
if !inited {
if keyframes_pushed > 1
|| appsrc.current_level_bytes() >= appsrc.max_bytes() * 2 / 3
{
// log::info!("=== Playing state on {}", appsrc.name());
// log::info!(
// "Buffer filled to {} of {} ({} %) in {} keyframes",
// appsrc.current_level_bytes(),
// appsrc.max_bytes(),
// appsrc.current_level_bytes() * 100 / appsrc.max_bytes(),
// keyframes_pushed
// );
inited = true;
// appsrc.set_state(gstreamer::State::Playing).unwrap();
} else if data.keyframe {
// log::info!(
// "Buffer filling to {} of {} ({} %) in {} keyframes on {}",
// appsrc.current_level_bytes(),
// appsrc.max_bytes(),
// appsrc.current_level_bytes() * 100 / appsrc.max_bytes(),
// keyframes_pushed,
// appsrc.name()
// );
keyframes_pushed += 1;
}
}
}
Err(e) => Err(anyhow!("Error in streaming: {e:?}")),
}?;
AnyResult::Ok(())
})();
log::error!("r: {:?}", r);
r
});
// Send to the blocking thread
while let Some(Ok(data)) = stream.next().await {
// Start on iframes
if tx.send(data).await.is_err() {
break;
}
}
log::error!("Exiting send_to_appsrc");
Ok(())
}
+58
View File
@@ -0,0 +1,58 @@
use anyhow::{anyhow, Result};
use clap::{Parser, ValueEnum};
fn onoff_parse(src: &str) -> Result<bool> {
match src {
"true" | "on" | "yes" => Ok(true),
"false" | "off" | "no" => Ok(false),
_ => Err(anyhow!(
"Could not understand {}, check your input, should be true/false, on/off or yes/no",
src
)),
}
}
/// The services command will control the ports for http/https/rtmp/rtsp/onvif
#[derive(Parser, Debug)]
pub struct Opt {
/// The name of the camera. Must be a name in the config
pub camera: String,
/// service to change
pub service: Services,
/// The action to perform
#[command(subcommand)]
pub cmd: PortAction,
}
#[derive(Parser, Debug, Clone, ValueEnum)]
pub enum Services {
Baichuan,
Http,
Https,
Rtmp,
Rtsp,
Onvif,
}
#[derive(Parser, Debug)]
pub enum PortAction {
/// Get the current details
Get,
/// Turn the service ON
On,
/// Turn the service OFF
Off,
/// Set both port and on/off
Set {
/// The new port
port: u32,
/// On/off
#[arg(value_parser = onoff_parse, action = clap::ArgAction::Set, name = "on|off")]
enabled: bool,
},
/// Set the port
Port {
/// The new port
port: u32,
},
}
+260
View File
@@ -0,0 +1,260 @@
///
/// # Neolink Services
///
/// This module handles the controls of the services like http/https/rtmp/rtsp/onvif
/// It only works on newer cameras that have these built in
///
///
/// # Usage
///
/// ```bash
/// # To turn on http
/// neolink services --config=config.toml CameraName http on
/// # Or off
/// neolink services --config=config.toml CameraName http off
/// # Or set the port
/// neolink services --config=config.toml CameraName http port 80
/// # Or get the current port details
/// neolink services --config=config.toml CameraName http get
/// ```
///
/// Services are
/// - http
/// - https
/// - rtmp
/// - rtsp
/// - onvif (will also turn on rtsp)
///
use anyhow::{Context, Result};
mod cmdline;
use crate::common::NeoReactor;
pub(crate) use cmdline::*;
/// Entry point for the pir subcommand
///
/// Opt is the command line options
pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
let camera = reactor.get(&opt.camera).await?;
match opt.cmd {
PortAction::Get => match opt.service {
Services::Http => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_http()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
Services::Https => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_https()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
Services::Rtsp => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_rtsp()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
Services::Rtmp => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_rtmp()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
Services::Onvif => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_onvif()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
Services::Baichuan => {
let state = camera
.run_task(|cam| {
Box::pin(async move {
cam.get_serverport()
.await
.context("Unable to get camera service state")
})
})
.await?;
let ser = String::from_utf8(
{
let mut buf = bytes::BytesMut::new();
quick_xml::se::to_writer(&mut buf, &state).map(|_| buf.to_vec())
}
.expect("Should Ser the struct"),
)
.expect("Should be UTF8");
println!("{}", ser);
}
},
action => {
let on = match &action {
PortAction::On => Some(true),
PortAction::Off => Some(false),
PortAction::Set { enabled, .. } => Some(*enabled),
_ => None,
};
let port = match &action {
PortAction::Port { port } => Some(*port),
PortAction::Set { port, .. } => Some(*port),
_ => None,
};
match opt.service {
Services::Http => {
camera
.run_task(|cam| {
Box::pin(async move {
cam.set_http(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
Services::Https => {
camera
.run_task(|cam| {
Box::pin(async move {
cam.set_https(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
Services::Rtsp => {
camera
.run_task(|cam| {
Box::pin(async move {
// Onvif will not work without rtsp
if let Some(false) = &on {
cam.set_onvif(Some(false), None)
.await
.context("Unable to set camera service state")?;
}
cam.set_rtsp(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
Services::Rtmp => {
camera
.run_task(|cam| {
Box::pin(async move {
cam.set_rtmp(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
Services::Onvif => {
camera
.run_task(|cam| {
Box::pin(async move {
// Onvif will not work without rtsp
if let Some(true) = &on {
cam.set_rtsp(Some(true), None)
.await
.context("Unable to set camera service state")?;
}
cam.set_onvif(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
Services::Baichuan => {
camera
.run_task(|cam| {
Box::pin(async move {
cam.set_serverport(on, port)
.await
.context("Unable to set camera service state")
})
})
.await?;
}
}
}
}
Ok(())
}
+3 -3
View File
@@ -2,8 +2,8 @@ use crate::AnyResult;
use anyhow::{anyhow, Context, Result};
use crossbeam_channel::{bounded, Receiver, Sender};
use gstreamer::{
element_error, parse_launch, prelude::*, Caps, ClockTime, FlowError, FlowSuccess, MessageView,
Pipeline, ResourceError, State,
element_error, parse::launch_full, prelude::*, Caps, ClockTime, FlowError, FlowSuccess,
MessageView, ParseFlags, Pipeline, ResourceError, State,
};
use gstreamer_app::{AppSink, AppSinkCallbacks};
use tokio::task::JoinSet;
@@ -159,7 +159,7 @@ fn create_pipeline(
// Parse the pipeline we want to probe from a static in-line string.
// Here we give our audiotestsrc a name, so we can retrieve that element
// from the resulting pipeline.
let pipeline = parse_launch(&launch_str)
let pipeline = launch_full(&launch_str, None, ParseFlags::empty())
.context("Unable to load gstreamer pipeline ensure all gstramer plugins are installed")?;
let pipeline = pipeline.dynamic_cast::<Pipeline>().map_err(|_| {
anyhow!("Unable to create gstreamer pipeline ensure all gstramer plugins are installed")
+2
View File
@@ -22,7 +22,9 @@ where
pub(crate) enum AddressOrUid {
Address(String),
#[allow(dead_code)]
Uid(String, DiscoveryMethods),
#[allow(dead_code)]
AddressWithUid(String, String, DiscoveryMethods),
}