Compare commits

...
Author SHA1 Message Date
QuantumEntangledAndy e7a244a1ab Add protobuf-compiler for clippy checks 2023-05-19 23:29:54 +07:00
QuantumEntangledAndy b978809a59 fmt 2023-05-19 23:20:18 +07:00
QuantumEntangledAndy 6b35db8fbe Cleanup of pushnoti README 2023-05-19 23:16:48 +07:00
QuantumEntangledAndy c3f3ecdb83 Use the right method for fcm 2023-05-19 16:18:30 +07:00
QuantumEntangledAndy f99df6e5e7 Add fcm test project 2023-05-19 09:18:01 +07:00
QuantumEntangledAndy e09d9d30dc More messages and further details on APNS 2023-05-18 14:43:56 +07:00
QuantumEntangledAndy c2f0b5b2d9 Add snap to messages 2023-05-18 14:17:33 +07:00
QuantumEntangledAndy 472eda9f06 RE push noti message 2023-05-18 08:38:30 +07:00
QuantumEntangledAndy c831d07573 v0.5.11 2023-05-18 08:32:23 +07:00
QuantumEntangledAndy 90b51b7e46 v0.5.11 2023-05-18 08:26:26 +07:00
Andrew King cdc8a9cb8e Merge pull request #82 from QuantumEntangledAndy/fix/pause_buffers
Fix pause buffers
2023-05-18 08:06:25 +07:00
QuantumEntangledAndy 790eb655a3 Put a message about max bitrate in the log when buffer is exhausted 2023-05-17 15:01:58 +07:00
QuantumEntangledAndy 99584ce813 Slightly increase the MTU 2023-05-17 11:16:18 +07:00
QuantumEntangledAndy c2a5798905 Change buffer over and undeful size 2023-05-16 18:31:09 +07:00
QuantumEntangledAndy 951a822973 Make buffer size configurable 2023-05-16 18:25:39 +07:00
QuantumEntangledAndy f5ac615b3c Renable time smoothing 2023-05-16 15:52:32 +07:00
QuantumEntangledAndy 3b677f9156 Refilling debug print correction 2023-05-16 15:45:07 +07:00
QuantumEntangledAndy 9473a8ad5d Correct buffer sorting, and reinit 2023-05-16 15:21:00 +07:00
QuantumEntangledAndy 56ef79ff45 Yet another alteration to seek 2023-05-16 12:42:30 +07:00
QuantumEntangledAndy d61e48ce79 Full grouping for frames of the same time 2023-05-16 12:25:49 +07:00
QuantumEntangledAndy 86f15f9eb1 Handle jump to live even for a one frame buffer 2023-05-16 11:37:20 +07:00
QuantumEntangledAndy 84e10530a5 Minimum stamp buffer size 2023-05-16 11:28:58 +07:00
QuantumEntangledAndy 2f74a720f5 Sign correction 2023-05-16 09:00:24 +07:00
QuantumEntangledAndy 511e50fe52 Update the start time on restamp too 2023-05-16 08:58:23 +07:00
QuantumEntangledAndy 78da59fc71 Change the way buffer time stamps are handled 2023-05-16 08:36:47 +07:00
QuantumEntangledAndy 36f70c3222 No smooth and extra debug 2023-05-15 19:44:07 +07:00
QuantumEntangledAndy 279c1c5bf1 Do target live calculation for abstract buffer 2023-05-15 18:32:25 +07:00
QuantumEntangledAndy f5ba6e244f Imporve handoff between preprocess and live 2023-05-15 10:05:31 +07:00
Andrew 6a980b47ae Reduce target buffer size 2023-05-15 09:45:11 +07:00
QuantumEntangledAndy 3a783bf50c Use only unique timestamps to calculate live 2023-05-15 09:32:35 +07:00
Andrew 9dabb43a9c Address pausing of buffers 2023-05-15 08:52:55 +07:00
QuantumEntangledAndy b9fd2f75ff v0.5.10 2023-05-14 12:33:57 +07:00
Andrew 61f65ed42a Merge remote-tracking branch 'origin/master' 2023-05-14 12:31:34 +07:00
Andrew King 634bff08af Merge pull request #72 from AdrianosBates/fix/ptz-stop
Fix/ptz stop
2023-05-14 12:23:39 +07:00
QuantumEntangledAndy 2d1ccf518a Clippy fixes 2023-05-14 10:45:34 +07:00
QuantumEntangledAndy d9600d0607 Filter out OK/FAIL replies 2023-05-14 10:40:46 +07:00
QuantumEntangledAndy 72cf358b0c Make mqtt messgae handler cleaner 2023-05-14 08:46:22 +07:00
QuantumEntangledAndy a819248556 Rework MQTT to allow async message handelling 2023-05-13 20:03:08 +07:00
QuantumEntangledAndy 6e334392da Merge branch 'master' of https://github.com/QuantumEntangledAndy/neolink into fix/ptz-stop 2023-05-13 16:52:40 +07:00
QuantumEntangledAndy eed2aeb1be v0.5.9 2023-05-12 12:13:27 +07:00
QuantumEntangledAndy 84163dcd2a v0.5.9 2023-05-12 09:26:31 +07:00
Andrew King 30573c7ba3 Merge pull request #68 from QuantumEntangledAndy/fix/vlc_seek
Improve VLC support

Should also improve frigate
2023-05-12 09:21:51 +07:00
QuantumEntangledAndy c3c58b3f67 Put back in smoothing and bump up the buffer size 2023-05-11 13:28:21 +07:00
QuantumEntangledAndy bc3f9b65a0 Add messages about buffer size 2023-05-10 20:43:53 +07:00
QuantumEntangledAndy 5513fce18f Disable time smoothing 2023-05-10 20:37:06 +07:00
QuantumEntangledAndy df3462875b Add timeout to p2p address resolution 2023-05-10 13:54:39 +07:00
QuantumEntangledAndy e2d5eb061f Add time out to map discovery 2023-05-10 13:25:51 +07:00
QuantumEntangledAndy 2921ede080 Reinit client buffers when master is restamped 2023-05-10 13:11:56 +07:00
QuantumEntangledAndy 004405e868 Correct delta on buffer schedule 2023-05-10 11:47:18 +07:00
QuantumEntangledAndy d411e9c25b Restamp over clear buffer during reconnect 2023-05-10 11:38:13 +07:00
QuantumEntangledAndy e712e3b47c Dedicated thread for the BC connection layer 2023-05-10 10:26:53 +07:00
QuantumEntangledAndy b09a590fb6 Cleanup logs 2023-05-10 09:39:59 +07:00
QuantumEntangledAndy f9407d8d8d Seek within buffer, not RANDOM_ACCESS 2023-05-10 09:31:13 +07:00
QuantumEntangledAndy eec01faa0a 250ms Latency 2023-05-09 18:25:07 +07:00
QuantumEntangledAndy d2daad0bf2 Just seek to live 2023-05-09 15:47:06 +07:00
QuantumEntangledAndy 55d357ecf8 Async sender code 2023-05-09 15:22:51 +07:00
QuantumEntangledAndy 15f9993c12 Manual seeking 2023-05-09 14:35:01 +07:00
QuantumEntangledAndy 52840348bf Send frames async 2023-05-09 11:23:23 +07:00
QuantumEntangledAndy d5cb12dc3c Improve clearing of 'Stream not ready' streams 2023-05-09 08:56:58 +07:00
QuantumEntangledAndy 9f1cda3c60 Group audio playback
Also use buffer lists
2023-05-08 19:21:00 +07:00
QuantumEntangledAndy 5e81d6a369 Update for pausing 2023-05-08 15:24:00 +07:00
QuantumEntangledAndy 7a50d3e42f Try another method 2023-05-08 13:59:02 +07:00
QuantumEntangledAndy 587c8bf334 Fix client pause 2023-05-07 15:57:13 +07:00
QuantumEntangledAndy dbf21d9c90 Change how frames are popped from buffer. Change how target time is updated 2023-05-07 09:29:47 +07:00
QuantumEntangledAndy 60e4b994ab Some changes to how we poll buffer processing 2023-05-05 20:16:01 +07:00
QuantumEntangledAndy 8c061efc1e Added drifting start_time 2023-05-05 11:01:20 +07:00
QuantumEntangledAndy 262d2b2ed4 Lockless subscription 2023-05-05 08:09:35 +07:00
QuantumEntangledAndy b07aa2ddfe Jump to live if more than 15s difference 2023-05-04 09:10:52 +07:00
QuantumEntangledAndy 386fec4220 Allow decode Bc frame over packet boundary 2023-05-03 20:26:13 +07:00
QuantumEntangledAndy e2602b5c17 Add tokio console to the config 2023-05-03 18:17:05 +07:00
QuantumEntangledAndy 26d894222a Build this way 2023-05-03 16:00:25 +07:00
QuantumEntangledAndy c20b944411 Sh it's sh 2023-05-03 15:31:28 +07:00
QuantumEntangledAndy f60de36bae Source /.cargo/env 2023-05-03 15:27:57 +07:00
QuantumEntangledAndy 929dac374b Nightly 2023-05-03 15:21:49 +07:00
QuantumEntangledAndy fd3282efe2 Add rustflag directly 2023-05-03 15:15:37 +07:00
QuantumEntangledAndy 359597131e Tokio console 2023-05-03 14:43:26 +07:00
QuantumEntangledAndy 0b922aeeb5 Always timed polls 2023-05-03 13:29:47 +07:00
QuantumEntangledAndy 29b6ff8c24 Timed polling 2023-05-03 13:19:59 +07:00
QuantumEntangledAndy ae3f9365b1 Skip printing of pkg_config join tags in array 2023-05-03 11:51:05 +07:00
QuantumEntangledAndy c666a4c3bd Add skip build to style checks too 2023-05-03 11:30:20 +07:00
QuantumEntangledAndy e4bcf9bd23 Fixes to docker build tagging 2023-05-03 11:27:36 +07:00
QuantumEntangledAndy 8a0d4a8f03 Changes to github action to reduce reruns 2023-05-03 11:24:34 +07:00
QuantumEntangledAndy a6e11af084 Use mt tokio 2023-05-03 10:37:18 +07:00
QuantumEntangledAndy 2403ee2b27 Close client on buffers prepared
Don't prepare buffers more than once across restarts

Use time limited queues

Don't cache dummy sender
2023-05-03 10:15:35 +07:00
QuantumEntangledAndy 9d57b2dafd Change how queues work 2023-05-03 08:46:27 +07:00
QuantumEntangledAndy 86f6ea55ec Without jump to lives 2023-05-03 08:15:40 +07:00
QuantumEntangledAndy 2ff1ee474e Handle error in appsrc push during init 2023-05-02 15:49:10 +07:00
QuantumEntangledAndy ccd5778f57 Display stream not ready if connecing before buffers prepared 2023-05-02 15:25:40 +07:00
QuantumEntangledAndy af9f181ff5 Increase minimum buffer amount 2023-05-02 15:19:01 +07:00
QuantumEntangledAndy d50fa87baf Rework the type of the time 2023-05-02 14:40:56 +07:00
QuantumEntangledAndy f2b609f12c Allow negative start times 2023-05-02 13:53:20 +07:00
QuantumEntangledAndy c53b0955f8 Bullseye docker 2023-05-02 13:15:22 +07:00
QuantumEntangledAndy dc6860378d Drop whole session 2023-05-02 10:57:41 +07:00
QuantumEntangledAndy 84bf5ef05c Fix the jump to live process 2023-05-02 10:20:24 +07:00
QuantumEntangledAndy 31ac6e4707 Reconnect streams when media type changes 2023-05-01 18:37:57 +07:00
QuantumEntangledAndy 048af92808 Saturating subs all around 2023-05-01 15:40:14 +07:00
QuantumEntangledAndy 4a08b8148b Use the bin (faster) method for sending the prebuffer 2023-05-01 14:19:32 +07:00
QuantumEntangledAndy cce519de6d Use queue2 2023-05-01 12:34:31 +07:00
QuantumEntangledAndy d24a4d12e2 More adjustments to how the pre-buffer is sent 2023-05-01 12:26:00 +07:00
QuantumEntangledAndy 8cea9e79e5 Use time to trim the buffer 2023-04-30 13:56:03 +07:00
QuantumEntangledAndy a222a2d053 New live buffer method 2023-04-30 09:31:03 +07:00
QuantumEntangledAndy 093a1b1af9 Close brace 2023-04-28 21:15:22 +07:00
QuantumEntangledAndy c30e6293f6 Rework how buffers are sent to always be 2023-04-28 19:59:06 +07:00
Adrian Bates 03a7e1473a Fixed mistake in new error message 2023-04-25 16:14:55 -04:00
Adrian Bates 5ccdc27d37 In mod.rs:
Removed the ability to send "stop" PTZ command with MQTT
	Changed the signature of PTZ MQTT commands to (direction amount seconds), where seconds is currently checked and limited to the range 0.0 - 10.0
In event_cam.rs:
	stop has been removed from event_cam.Direction, and directions now take an additional f32 seconds parameter
	in Message::PTZ if the intial send_ptz is successful, will sleep for designated seconds and then issue a stop command
2023-04-25 15:49:08 -04:00
QuantumEntangledAndy 107902ef3a Complete rework of buffer time 2023-04-25 14:28:35 +07:00
Adrian Bates 29b47aa44e Changed Unrecognized PTZ Direction error to include unrecognized command 2023-04-22 11:41:45 -04:00
Adrian Bates 427046239c Added stop command to PTZ 2023-04-22 11:13:59 -04:00
Andrew c4c35d1bde Implement fake seek for VLC 2023-04-20 15:40:58 +07:00
Andrew King 81a573ba6d Merge pull request #66 from QuantumEntangledAndy/fix/mqtt_asyncio
Update mqtt to use tokio
2023-04-20 15:39:40 +07:00
Andrew 7d771fc94b Log current discovery method 2023-04-20 15:24:31 +07:00
QuantumEntangledAndy 4f50a962e6 Pass fail/OK messages back to mqtt 2023-04-20 07:32:30 +07:00
QuantumEntangledAndy 7468d77178 Pass error up from mqtt, add more detailed error messages 2023-04-20 07:13:54 +07:00
Andrew 19a468ac2e Squash more blocking in async on mqttt 2023-04-19 11:51:50 +07:00
Andrew a46192d47c Do discovery sequentially 2023-04-19 11:47:30 +07:00
QuantumEntangledAndy 45fec7cfc2 Update mqtt to use asyncio 2023-04-18 20:27:53 +07:00
QuantumEntangledAndy 3f4bc510a7 Don't prune version tagged dockerimages 2023-04-18 11:05:16 +07:00
QuantumEntangledAndy 42ff221b07 v0.5.8 2023-04-18 09:54:20 +07:00
QuantumEntangledAndy 9a153cda80 v0.5.8 2023-04-18 09:29:10 +07:00
QuantumEntangledAndy 1f37e699b2 v0.5.8 2023-04-18 08:55:15 +07:00
Andrew King 45789d44d6 Merge pull request #63 from QuantumEntangledAndy/fix/bytes_remaining
Fix/bytes remaining and blocking
2023-04-18 08:44:28 +07:00
QuantumEntangledAndy c1445ea1e1 Remove debug print 2023-04-18 08:17:52 +07:00
QuantumEntangledAndy 74274ae30e Add restarts to the send thread 2023-04-18 08:15:31 +07:00
QuantumEntangledAndy 233a73e1dc Backoff on retries 2023-04-17 19:49:50 +07:00
QuantumEntangledAndy f8b48544f8 Switch too non-locking, fail skipping, signal reading 2023-04-17 19:45:54 +07:00
QuantumEntangledAndy 5e9783fed3 Dockerprune script token 2023-04-16 16:23:42 +07:00
QuantumEntangledAndy 78186a2725 Debug dockerprune 2023-04-16 16:13:00 +07:00
QuantumEntangledAndy 824206faaa Reducing blocking on gstraemer pushes 2023-04-16 16:08:44 +07:00
Andrew f61fef9b32 Allow decode across EOF 2023-04-16 16:08:44 +07:00
Andrew 37049f7ca4 Fix bytes remaining on stream 2023-04-16 16:08:44 +07:00
Andrew King 8db2fd4a91 Merge pull request #60 from QuantumEntangledAndy/refractor/dep_update01
Updates of all packages
2023-04-15 14:26:36 +07:00
Andrew 3a2db7aaa8 Clippy 2023-04-15 09:44:05 +07:00
QuantumEntangledAndy 78375b220d Updates of all packages 2023-04-15 08:05:58 +07:00
QuantumEntangledAndy 1ea8f182fa Add dockerprune script 2023-04-14 10:18:16 +07:00
QuantumEntangledAndy 8380227f8f Added needed packages to docker 2023-04-14 09:20:21 +07:00
71 changed files with 6182 additions and 2286 deletions

No files matched your search

+4 -1
View File
@@ -1,5 +1,5 @@
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]
rustflags = ["-C", "target-feature=+crt-static", "--cfg", "tokio_unstable"]
# TODO not good as a default
[target.armv7-unknown-linux-gnueabihf]
@@ -10,3 +10,6 @@ linker = "aarch64-linux-gnu-gcc"
[target.i686-unknown-linux-gnu]
linker = "i686-linux-gnu-gcc"
[build]
rustflags = ["--cfg", "tokio_unstable"]
+56 -21
View File
@@ -3,7 +3,23 @@ name: CI
on: [push, pull_request, workflow_dispatch]
jobs:
pre_job:
# continue-on-error: true # Uncomment once integration is finished
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }} && ! (startsWith(github.ref, 'refs/tags/') && github.event_name == 'push')
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@v5
with:
# All of these options are optional, so you can remove them if you are happy with the defaults
concurrent_skipping: 'same_content_newer'
skip_after_successful_duplicate: 'false'
native:
needs: pre_job
if: needs.pre_job.outputs.should_skip != 'true'
name: native
runs-on: ${{ matrix.os }}
strategy:
@@ -62,13 +78,15 @@ jobs:
echo "PATH=${PATH}"
echo "GSTREAMER_1_0_ROOT_MSVC_X86_64=${GSTREAMER_1_0_ROOT_MSVC_X86_64}"
echo "PKG_CONFIG_PATH=${PKG_CONFIG_PATH}"
pkg-config --variable pc_path pkg-config
# pkg-config --variable pc_path pkg-config
cargo build --release --all-features
- uses: actions/upload-artifact@v3
with:
name: release-${{ matrix.os }}
path: "target/release/neolink*"
cross:
needs: pre_job
if: needs.pre_job.outputs.should_skip != 'true'
name: cross
runs-on: ubuntu-latest
container: "node:current-bullseye-slim"
@@ -126,7 +144,7 @@ jobs:
TARGET: ${{ matrix.target }}
- name: Build
run: |
cargo build --release --all-features --target=${TARGET}
cargo build --verbose --release --all-features --target=${TARGET}
env:
# Retarget pkg-config as described in https://www.freedesktop.org/wiki/Software/pkg-config/CrossCompileProposal/
PKG_CONFIG_ALLOW_CROSS: 1
@@ -134,16 +152,15 @@ jobs:
TARGET: ${{ matrix.target }}
- uses: actions/upload-artifact@v3
with:
name: release-${{ matrix.arch }}-buster
name: release-${{ matrix.arch }}-bullseye
path: "target/${{ matrix.target }}/release/neolink*"
push_to_registry:
name: Build Docker image
if: startsWith(github.ref, 'refs/tags/') == false && github.event_name == 'push'
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
runs-on: ubuntu-latest
needs:
- "cross"
- "native"
steps:
- name: Check token is set
id: vars
@@ -183,13 +200,31 @@ jobs:
- name: Get tag name
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
id: tags
shell: bash
run: |
branch="$(echo "${GITHUB_REF#refs/heads/}" | sed -E 's|[/]|-|g')"
tags="${REPO_NAME}:${branch}"
if [ "${branch}" == "master" ]; then
tags="${tags},${REPO_NAME}:latest"
tags=( )
gittag="$(git tag --points-at HEAD)"
branch="$(git rev-parse --abbrev-ref HEAD)"
if [[ -n "${gittag}" ]]; then
tags=( "${tags[@]}" "${REPO_NAME}:${gittag}" )
fi
echo "TAGS=${tags}" >> $GITHUB_OUTPUT
if [[ -n "${branch}" ]]; then
branch="$(echo "${branch}" | sed -E 's|[/]|-|g')"
tags=( "${tags[@]}" "${REPO_NAME}:${branch}" )
if [ "${branch}" == "master" ]; then
tags=( "${tags[@]}" "${tags},${REPO_NAME}:latest" )
fi
fi
function join_by {
local d=${1-} f=${2-}
if shift 2; then
printf %s "$f" "${@/#/$d}"
fi
}
tagstr="$(join_by "," "${tags[@]}")"
echo "TAGS=${tagstr}"
echo "TAGS=${tagstr}" >> "${GITHUB_OUTPUT}"
env:
REPO_NAME: ${{ steps.string_repo.outputs.lowercase }}
- name: Install latest rust
@@ -211,22 +246,22 @@ jobs:
- name: Download Linux x86_64
uses: actions/download-artifact@v3
with:
name: release-amd64-buster
name: release-amd64-bullseye
path: linux/amd64
- name: Download Linux armv7
uses: actions/download-artifact@v3
with:
name: release-armhf-buster
name: release-armhf-bullseye
path: linux/arm/v7
- name: Download Linux arm64
uses: actions/download-artifact@v3
with:
name: release-arm64-buster
name: release-arm64-bullseye
path: linux/arm64
- name: Download Linux i386
uses: actions/download-artifact@v3
with:
name: release-i386-buster
name: release-i386-bullseye
path: linux/386
- name: Push to Docker Hub
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
@@ -280,22 +315,22 @@ jobs:
- name: Download Linux x86_64
uses: actions/download-artifact@v3
with:
name: release-amd64-buster
path: neolink_linux_x86_64_buster
name: release-amd64-bullseye
path: neolink_linux_x86_64_bullseye
- name: Download Linux armhf
uses: actions/download-artifact@v3
with:
name: release-armhf-buster
name: release-armhf-bullseye
path: neolink_linux_armhf
- name: Download Linux arm64
uses: actions/download-artifact@v3
with:
name: release-arm64-buster
name: release-arm64-bullseye
path: neolink_linux_arm64
- name: Download Linux i386
uses: actions/download-artifact@v3
with:
name: release-i386-buster
name: release-i386-bullseye
path: neolink_linux_i386
- name: Prepare Releases
run: |
@@ -303,7 +338,7 @@ jobs:
neolink_windows
neolink_macos
neolink_linux_x86_64_ubuntu
neolink_linux_x86_64_buster
neolink_linux_x86_64_bullseye
neolink_linux_armhf
neolink_linux_arm64
neolink_linux_i386
@@ -320,7 +355,7 @@ jobs:
neolink_windows.zip
neolink_macos.zip
neolink_linux_x86_64_ubuntu.zip
neolink_linux_x86_64_buster.zip
neolink_linux_x86_64_bullseye.zip
neolink_linux_armhf.zip
neolink_linux_arm64.zip
neolink_linux_i386.zip
+147
View File
@@ -0,0 +1,147 @@
name: DockerHub
on: [push, pull_request, workflow_dispatch]
jobs:
pre_job:
# continue-on-error: true # Uncomment once integration is finished
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }}
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@v5
with:
# All of these options are optional, so you can remove them if you are happy with the defaults
concurrent_skipping: 'same_content_newer'
skip_after_successful_duplicate: 'false'
native:
needs: pre_job
if: needs.pre_job.outputs.should_skip != 'true'
name: native
runs-on: ubuntu-latest
steps:
- name: Check token is set
id: vars
shell: bash
run: |
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
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
with:
string: ${{ github.event.repository.name }}
- name: Run Clean Script
shell: bash
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
run: |
#!/bin/bash
#Script will delete all images in all repositories of your docker hub account which are older than x days
set -e
# set username and password
UNAME="${OWNER}"
TOKEN="${TOKEN}"
DAYS="${DAYS}"
REPO_NAME="${REPO_NAME}"
# get list of namespaces accessible by user (not in use right now)
#NAMESPACES=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/namespaces/ | jq -r '.namespaces|.[]')
#echo $TOKEN
echo
# get list of repos for that user account
echo "List of Repositories in ${UNAME} Docker Hub account"
REPO_LIST="$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000" | jq -r '.results|.[]|.name')"
echo "$REPO_LIST"
echo
# build a list of all images & tags
for i in ${REPO_LIST}
do
# get tags for repo
IMAGE_TAGS="$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000" | jq -r '.results|.[]|.name')"
# build a list of images from tags
for j in ${IMAGE_TAGS}
do
# add each tag to list
FULL_IMAGE_LIST="${FULL_IMAGE_LIST} ${UNAME}/${i}:${j}"
done
done
# output list of all docker images
echo
echo "List of all docker images in ${UNAME} Docker Hub account"
for i in ${FULL_IMAGE_LIST}
do
echo "${i}"
done
echo
echo "Identifying and deleting images which are older than ${DAYS} days in ${UNAME} docker hub account"
# Note!!! Please un-comment below line if you wanna perform operation on all repositories of your Docker Hub account
#for i in ${REPO_LIST}
repos=(
"${REPO_NAME}"
)
for i in "${repos[@]}"; do
# get tags for repo
echo
echo "Looping Through $i repository in ${UNAME} account"
IMAGE_TAGS="$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000" | jq -r '.results|.[]|.name')"
# build a list of images from tags
for j in ${IMAGE_TAGS}
do
if [[ "${j}" =~ ^[0-9][.][0-9][.][0-9]$ ]]; then
echo "Version tag (${j}) will not be deleted"
continue
fi
echo
# add last_updated_time
updated_time="$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/?page_size=10000" | jq -r '.last_updated')"
echo "$updated_time"
datetime=$updated_time
timeago="${DAYS} days ago"
dtSec=$(date --date "$datetime" +'%s')
taSec=$(date --date "$timeago" +'%s')
echo "INFO: dtSec=$dtSec, taSec=$taSec"
if [ "$dtSec" -lt "$taSec" ]
then
echo "This image ${UNAME}/${i}:${j} is older than ${DAYS} days, deleting this image"
## Please uncomment below line to delete docker hub images of docker hub repositories
curl -s -X DELETE -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/"
else
echo "This image ${UNAME}/${i}:${j} is within ${DAYS} days time range, keep this image"
fi
done
done
echo "Script execution ends"
env:
DAYS: "100"
REPO_NAME: ${{ steps.string_repo.outputs.lowercase }}
OWNER: ${{ steps.string_user.outputs.lowercase }}
TOKEN: ${{ secrets.DOCKER_PRUNE__TOKEN }}
+22 -2
View File
@@ -3,16 +3,32 @@ name: Style
on: [push, pull_request]
jobs:
pre_job:
# continue-on-error: true # Uncomment once integration is finished
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }}
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@v5
with:
# All of these options are optional, so you can remove them if you are happy with the defaults
concurrent_skipping: 'same_content_newer'
skip_after_successful_duplicate: 'false'
check_clippy:
needs: pre_job
if: needs.pre_job.outputs.should_skip != 'true'
name: Clippy
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- name: apt install gstreamer
- name: apt install gstreamer and deps
run: |
sudo apt update
sudo apt install -y aptitude
sudo aptitude install -y libgstrtspserver-1.0-dev libgstreamer1.0-dev libgtk2.0-dev
sudo aptitude install -y libgstrtspserver-1.0-dev libgstreamer1.0-dev libgtk2.0-dev protobuf-compiler
- name: Install nightly rust
run: |
rustup toolchain install nightly --component clippy
@@ -21,6 +37,8 @@ jobs:
run: cargo +nightly clippy --workspace --all-targets --all-features
check_fmt:
needs: pre_job
if: needs.pre_job.outputs.should_skip != 'true'
name: Rust-fmt
runs-on: ubuntu-latest
steps:
@@ -34,6 +52,8 @@ jobs:
cargo +nightly fmt --all -- --check
check_lua:
needs: pre_job
if: needs.pre_job.outputs.should_skip != 'true'
name: Luacheck
runs-on: ubuntu-latest
steps:
+3
View File
@@ -20,3 +20,6 @@
._.DS_Store
**/.DS_Store
**/._.DS_Store
# The token file for the FCM tests
token.toml
+2
View File
@@ -0,0 +1,2 @@
MD013:
code_blocks: false
+1
View File
@@ -16,4 +16,5 @@ repos:
hooks:
- id: markdownlint-fix
files: README.md
args: [--disable, MD041, --]
Generated
+1640 -689
View File
File diff suppressed because it is too large. Load diff
+25 -22
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink"
description = "A standards-compliant bridge to Reolink IP cameras"
version = "0.5.7"
version = "0.5.11"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
@@ -12,26 +12,29 @@ members = [
]
[dependencies]
anyhow = "1.0.42"
anyhow = "1.0.70"
byte-slice-cast = "1.2.2"
crossbeam-channel = "0.5.1"
env_logger = "*"
futures = "0.3.26"
gstreamer = "0.19.0"
gstreamer-app = "0.19.0"
gstreamer-rtsp = "0.19.0"
gstreamer-rtsp-server = { version = "0.19.0", features = ["v1_16"] }
lazy_static = "1.4"
log = { version = "0.4" }
neolink_core = { path = "crates/core", version = "0.5.6" }
regex = "1"
rumqttc = "0.8.0"
serde = { version = "1.0", features = ["derive"] }
structopt = "0.3"
time = "0.2"
tokio = { version = "1.0", features = ["rt", "macros", "io-util"] }
clap = { version = "4.2.2", features = ["derive", "cargo"] }
console-subscriber = "0.1.8"
crossbeam-channel = "0.5.8"
env_logger = "0.10.0"
futures = "0.3.28"
gstreamer = "0.20.3"
gstreamer-app = "0.20.0"
gstreamer-rtsp = "0.20.0"
gstreamer-rtsp-server = { version = "0.20.3", features = ["v1_16"] }
is_sorted = "0.1.1"
itertools = "0.10.5"
lazy_static = "1.4.0"
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
neolink_core = { path = "crates/core", version = "0.5.11" }
regex = "1.7.3"
rumqttc = "0.20.0"
serde = { version = "1.0.160", features = ["derive"] }
time = "0.3.20"
tokio = { version = "1.27.0", features = ["rt-multi-thread", "macros", "io-util", "tracing"] }
tokio-stream = "0.1.12"
toml = "0.5"
validator = "0.10"
validator_derive = "0.10"
yaserde = "0.3.16"
toml = "0.7.3"
validator = "0.16.0"
validator_derive = "0.16.0"
yaserde = "0.8.0"
+4 -2
View File
@@ -4,7 +4,7 @@
# Miroslav Šedivý
# SPDX-License-Identifier: AGPL-3.0-only
FROM docker.io/rust:slim-buster AS build
FROM docker.io/rust:slim-bullseye AS build
ARG TARGETPLATFORM
ENV DEBIAN_FRONTEND=noninteractive
@@ -34,7 +34,7 @@ RUN echo "TARGETPLATFORM: ${TARGETPLATFORM}"; \
fi
# Create the release container. Match the base OS used to build
FROM debian:buster-slim
FROM debian:bullseye-slim
ARG TARGETPLATFORM
ARG REPO
ARG VERSION
@@ -50,6 +50,8 @@ RUN apt-get update && \
apt-get install -y --no-install-recommends \
libgstrtspserver-1.0-0 \
libgstreamer1.0-0 \
gstreamer1.0-tools \
gstreamer1.0-x \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad && \
+7 -4
View File
@@ -58,6 +58,7 @@ sudo apt install \
libgstrtspserver-1.0-0 \
libgstreamer1.0-0 \
libgstreamer-plugins-bad1.0-0 \
gstreamer1.0-x \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad
@@ -223,8 +224,8 @@ Then start the rtsp server as usual:
### Battery Levels
If you have a battery camera and would like to see the battery messages in the
log add the following to your config
If you have a battery camera and would like to see the battery messages in th
log add the following to your config
```toml
[[cameras]]
@@ -232,13 +233,14 @@ log add the following to your config
print_format = "Human"
```
You can also print into xml format with `print_format = "Xml"` which can then be
You can also print into xml format with `print_format = "Xml"` which can then b
passed by a script for processing.
### Docker
[Docker](https://hub.docker.com/r/quantumentangledandy/neolink) builds are also
provided in multiple architectures. The latest tag tracks master while each
branch gets it's own tag.
```bash
@@ -253,10 +255,11 @@ You can write an image from the stream to disk using:
neolink image --config=config.toml --file-path=filepath CameraName
```
Where filepath is the path to save the image to and CameraName is the name of
Where filepath is the path to save the image to and CameraName is the name o
the camera from the config to save the image from.
File is always jpeg and the extension given in filepath will be added or changed
to reflect this.
## License
+24 -25
View File
@@ -1,39 +1,38 @@
[package]
name = "neolink_core"
description = "Core services and structure for Reolink IP cameras"
version = "0.5.7"
version = "0.5.11"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
[dependencies]
aes = "0.6"
bytes = "1.0.0"
cfb-mode = "0.6"
cookie-factory = "0.3"
crc32fast = "1.2.1"
crossbeam-channel = "0.5.6"
aes = "0.8.2"
bytes = "1.4.0"
cfb-mode = "0.8.2"
cookie-factory = "0.3.2"
crc32fast = "1.3.2"
crossbeam-channel = "0.5.8"
delegate = "0.9.0"
err-derive = "0.2"
futures = "0.3.25"
err-derive = "0.3.1"
futures = "0.3.28"
get_if_addrs = "0.5.3"
lazy_static = "1.4"
local-ip-address = "0.4.4"
log = { version = "0.4" }
md5 = "0.7"
nom = { version = "6.1.2", features = ["alloc"] }
rand = "0.8.4"
regex = "1.5.4"
serde = { version = "1.0", features = ["derive"] }
time = "0.2"
tokio = { version = "1.24.2", features = ["full"] }
tokio-stream = { version = "0.1.11", features = ["sync", "time", "net"] }
tokio-util = { version = "0.7.4", features = ["full"] }
xml-rs = "0.8"
yaserde = "0.3.16"
yaserde_derive = "0.3.16"
lazy_static = "1.4.0"
local-ip-address = "0.5.1"
log = "0.4.17"
md5 = "0.7.0"
nom = { version = "7.1.3", features = ["alloc"] }
rand = "0.8.5"
regex = "1.7.3"
serde = { version = "1.0.106", features = ["derive"] }
time = { version = "0.3.20" , features = [ "macros", "parsing", "local-offset" ]}
tokio = { version = "1.27.0", features = ["full"] }
tokio-stream = { version = "0.1.12", features = ["sync", "time", "net"] }
tokio-util = { version = "0.7.7", features = ["full", "tracing"] }
yaserde = "0.8.0"
yaserde_derive = "0.8.0"
[dev-dependencies]
assert_matches = "1.5.0"
env_logger = "*"
indoc = "0.3"
indoc = "2.0.1"
+30
View File
@@ -7,6 +7,7 @@ use crate::bc::model::*;
use crate::bc::xml::*;
use crate::{Credentials, Error, Result};
use bytes::BytesMut;
use nom::AsBytes;
use tokio_util::codec::{Decoder, Encoder};
pub(crate) struct BcCodex {
@@ -45,6 +46,35 @@ impl Encoder<Bc> for BcCodex {
impl Decoder for BcCodex {
type Item = Bc;
type Error = Error;
fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>> {
match self.decode(buf)? {
Some(frame) => Ok(Some(frame)),
None => {
if buf.is_empty() {
Ok(None)
} else {
log::debug!(
"bytes remaining on BC stream: {:X?}",
buf.as_bytes().chunks(25).next()
);
// Right after this we seem to get an issue with the camera dropping us
// Needs probing
// F0, DE, BC, A, 3, 0, 0, 0, 88, 6, 0, 0, 0, 1, 4, 0, C8, 0, 0, 0, 0, 0, 0, 0, 30, 31, 64, 63, 48,
// 32, 36, 34, 6A, 6, 0, 0, 0, 0, 0, 0, D8, F5, C7, 86, 56, 0, 0, 0, 0, 0, 0, 1, 21, 9A, FC, 22, 7F, 6, AE, F6, 15, FF, E5, 71, 4, 2F, 24, 61, 15, 96, F0, BF, 83, DE, 10, BE, B4, 2E, 3
// 9, 76, 56, 92, 7E, 48, 79, 20, 9A, DC, 1B, BB, AC, 22, 60, 5C, 72, B5, 3D, 8, E0, 34, 43, 3F, 2E, A7, 81, A8, 11, 75, 7F, 58, 3E, 8, 54, 91, 43, 21, EC, 6B, D6, 1A, D5, CB, D5, 6C,
// 8C, 2E, 6E, A3, 51, C3, A4, F0, CF, 2B, 61, 81, D0, 1C, A1, 76, EE, BF, 7A, D5, D8, D1, C4, D, B0, 45, EE, 3E, 93, 9A, CE, 5F, AB, 75, 55, AC, 9D, 66, DE, 23, 6D, 5F, 25, 57, DA, F5
//, E, 7F, 8D, 30, A7, 66, C4, 60, 76, 41, D0, 6A, 23, E, A9, C5, 51, EE, F6, DD, 19, E7, A8, 96, 9F, 2B, AF, 31, 90, 9D, FC, BE
Ok(None)
}
}
}
// match self.decode(buf)? {
// Some(frame) => Ok(Some(frame)),
// None => Ok(None),
// }
}
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
// trace!("Decoding: {:X?}", src);
let bc = Bc::deserialize(&self.context, src);
+2
View File
@@ -35,6 +35,8 @@ pub const MSG_ID_GET_GENERAL: u32 = 104;
pub const MSG_ID_ABILITY_INFO: u32 = 151;
/// Setting general system info (clock mostly) messages have this ID
pub const MSG_ID_SET_GENERAL: u32 = 105;
/// Used to pass the token and client ID for push notifications
pub const MSG_ID_PUSH_INFO: u32 = 124;
/// Will send the talk config for talk back data to follow this msg
pub const MSG_ID_TALKCONFIG: u32 = 201;
/// Used to send talk back binary data
+17 -1
View File
@@ -3,7 +3,7 @@
use std::io::{Read, Write};
// YaSerde is currently naming the traits and the derive macros identically
use yaserde::{ser::Config, YaDeserialize, YaSerialize};
use yaserde::ser::Config;
use yaserde_derive::{YaDeserialize, YaSerialize};
#[cfg(test)]
@@ -85,6 +85,9 @@ pub struct BcXml {
/// Recieved on request for a users persmissions/capabilitoes
#[yaserde(rename = "AbilityInfo")]
pub ability_info: Option<AbilityInfo>,
/// Recieved on request for a users persmissions/capabilitoes
#[yaserde(rename = "PushInfo")]
pub push_info: Option<PushInfo>,
}
impl BcXml {
@@ -614,6 +617,19 @@ pub struct AbilityInfoSubModule {
pub ability_value: String,
}
/// PushInfo XML
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
pub struct PushInfo {
/// The token from FCM registration
pub token: String,
/// The phone type, known values: `reo_iphone`
#[yaserde(rename = "phoneType")]
pub phone_type: String,
/// A client ID, seems to be an all CAPS MD5 hash of something
#[yaserde(rename = "clientID")]
pub client_id: String,
}
/// Convience function to return the xml version used throughout the library
pub fn xml_ver() -> String {
"1.1".to_string()
+10 -5
View File
@@ -1,7 +1,12 @@
use super::model::EncryptionProtocol;
use aes::Aes128;
use cfb_mode::cipher::{NewStreamCipher, StreamCipher};
use cfb_mode::Cfb;
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";
@@ -20,7 +25,7 @@ pub fn decrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol
// AES decryption
let mut decrypted = buf.to_vec();
Cfb::<Aes128>::new(aeskey.into(), IV.into()).decrypt(&mut decrypted);
Aes128CfbDec::new(aeskey.into(), IV.into()).decrypt(&mut decrypted);
decrypted
}
}
@@ -39,7 +44,7 @@ pub fn encrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol
EncryptionProtocol::Aes(aeskey) => {
// AES encryption
let mut encrypted = buf.to_vec();
Cfb::<Aes128>::new(aeskey.into(), IV.into()).encrypt(&mut encrypted);
Aes128CfbEnc::new(aeskey.into(), IV.into()).encrypt(&mut encrypted);
encrypted
}
}
+54 -98
View File
@@ -24,6 +24,7 @@ mod motion;
mod ping;
mod pirstate;
mod ptz;
mod pushinfo;
mod reboot;
mod resolution;
mod stream;
@@ -39,6 +40,7 @@ pub use login::MaxEncryption;
pub use motion::{MotionData, MotionStatus};
pub use pirstate::PirState;
pub use ptz::Direction;
pub use pushinfo::PhoneType;
pub use resolution::*;
use std::sync::Arc;
pub use stream::{StreamData, StreamKind};
@@ -124,12 +126,6 @@ impl BcCamera {
/// Try to connect to the camera via appropaite methods and return
/// the location that should be used
async fn find_camera(options: &BcCameraOpt) -> Result<CameraLocation> {
let mut tcp_set = tokio::task::JoinSet::new();
let mut local_set = tokio::task::JoinSet::new();
let mut remote_set = tokio::task::JoinSet::new();
let mut map_set = tokio::task::JoinSet::new();
let mut relay_set = tokio::task::JoinSet::new();
if let ConnectionProtocol::Tcp | ConnectionProtocol::TcpUdp = options.protocol {
let mut sockets = vec![];
match options.port {
@@ -145,15 +141,15 @@ impl BcCamera {
}
}
}
info!("{}: Trying TCP discovery", options.name);
for socket in sockets.drain(..) {
let channel_id: u8 = options.channel_id;
let name: String = options.name.clone();
tcp_set.spawn(async move {
Discovery::check_tcp(socket, channel_id).await.map(|_| {
info!("{}: TCP Discovery success at {:?}", name, &socket);
socket
})
});
if let Ok(addr) = Discovery::check_tcp(socket, channel_id).await.map(|_| {
info!("{}: TCP Discovery success at {:?}", options.name, &socket);
socket
}) {
return Ok(CameraLocation::Tcp(addr));
}
}
}
@@ -187,103 +183,63 @@ impl BcCamera {
if allow_local {
let uid_local = uid.clone();
let name: String = options.name.clone();
local_set.spawn(async move {
trace!("{}: Starting Local discovery", name);
let result = Discovery::local(&uid_local, Some(sockets)).await;
if let Ok(disc) = &result {
info!(
"{}: Local discovery success {} at {}",
name,
uid_local,
disc.get_addr()
);
}
result
});
info!("{}: Trying local discovery", options.name);
let result = Discovery::local(&uid_local, Some(sockets)).await;
if let Ok(disc) = result {
info!(
"{}: Local discovery success {} at {}",
options.name,
uid_local,
disc.get_addr()
);
return Ok(CameraLocation::Udp(disc));
}
}
if allow_remote {
let uid_remote = uid.clone();
let name: String = options.name.clone();
remote_set.spawn(async move {
trace!("Starting Remote discovery");
let result = Discovery::remote(&uid_remote).await;
if let Ok(disc) = &result {
info!(
"{}: Remote discovery success {} at {}",
name,
uid_remote,
disc.get_addr()
);
}
result
});
info!("{}: Trying remote discovery", options.name);
let result = Discovery::remote(&uid_remote).await;
if let Ok(disc) = result {
info!(
"{}: Remote discovery success {} at {}",
options.name,
uid_remote,
disc.get_addr()
);
return Ok(CameraLocation::Udp(disc));
}
}
if allow_map {
let uid_map = uid.clone();
let name: String = options.name.clone();
map_set.spawn(async move {
trace!("Starting Map");
let result = Discovery::map(&uid_map).await;
if let Ok(disc) = &result {
info!("{}: Map success {} at {}", name, uid_map, disc.get_addr());
}
result
});
info!("{}: Trying map discovery", options.name);
let result = Discovery::map(&uid_map).await;
if let Ok(disc) = result {
info!(
"{}: Map success {} at {}",
options.name,
uid_map,
disc.get_addr()
);
return Ok(CameraLocation::Udp(disc));
}
}
if allow_relay {
let uid_relay = uid.clone();
let name: String = options.name.clone();
relay_set.spawn(async move {
trace!("Starting Relay");
let result = Discovery::relay(&uid_relay).await;
if let Ok(disc) = &result {
info!(
"{}: Relay success {} at {}",
name,
uid_relay,
disc.get_addr()
);
}
result
});
info!("{}: Trying relay discovery", options.name);
let result = Discovery::relay(&uid_relay).await;
if let Ok(disc) = result {
info!(
"{}: Relay success {} at {}",
options.name,
uid_relay,
disc.get_addr()
);
return Ok(CameraLocation::Udp(disc));
}
}
}
// Wait for all TCP to finish
while let Some(result) = tcp_set.join_next().await {
if let Ok(Ok(addr)) = result {
return Ok(CameraLocation::Tcp(addr));
}
}
// Tcp failed see if Local faired any better
// Wait for all local to finish
while let Some(result) = local_set.join_next().await {
if let Ok(Ok(discovery)) = result {
return Ok(CameraLocation::Udp(discovery));
}
}
// Local failed see if Remote faired any better
// Wait for all remote to finish
while let Some(result) = remote_set.join_next().await {
if let Ok(Ok(discovery)) = result {
return Ok(CameraLocation::Udp(discovery));
}
}
// Remote failed see if Map faired any better
// Wait for all Map to finish
while let Some(result) = map_set.join_next().await {
if let Ok(Ok(discovery)) = result {
return Ok(CameraLocation::Udp(discovery));
}
}
// Map failed see if Relay faired any better
// Wait for all Relay to finish
while let Some(result) = relay_set.join_next().await {
if let Ok(Ok(discovery)) = result {
return Ok(CameraLocation::Udp(discovery));
}
}
info!("{}: Discovery failed", options.name);
// Nothing works
Err(Error::CannotInitCamera)
}
+159 -113
View File
@@ -5,9 +5,10 @@ use futures::stream::{Stream, StreamExt};
use log::*;
use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::sync::Arc;
use tokio::sync::mpsc::{channel, Sender};
use tokio::sync::Mutex;
use tokio::task::yield_now;
use tokio_stream::wrappers::ReceiverStream;
use tokio_util::sync::PollSender;
use tokio::{sync::RwLock, task::JoinSet};
@@ -15,9 +16,9 @@ type MsgHandler = Box<dyn Fn(&Bc) -> Option<Bc> + Send + Sync>;
#[derive(Default)]
struct Subscriber {
/// Subscribers based on their Num
num: RwLock<BTreeMap<u16, Sender<Result<Bc>>>>,
num: BTreeMap<u16, Sender<Result<Bc>>>,
/// Subscribers based on their ID
id: RwLock<BTreeMap<u32, MsgHandler>>,
id: BTreeMap<u32, MsgHandler>,
}
pub(crate) type BcConnSink = Box<dyn Sink<Bc, Error = Error> + Send + Sync + Unpin>;
@@ -29,59 +30,63 @@ pub(crate) type BcConnSource = Box<dyn Stream<Item = Result<Bc>> + Send + Sync +
///
/// There can be only one subscriber per kind of message at a time.
pub struct BcConnection {
sink: Arc<Mutex<BcConnSink>>,
subscribers: Arc<Subscriber>,
#[allow(dead_code)] // Not dead we just need to hold a reference to keep it alive
sink: Sender<Result<Bc>>,
poll_commander: Sender<PollCommand>,
rx_thread: RwLock<JoinSet<Result<()>>>,
}
impl BcConnection {
pub async fn new(sink: BcConnSink, mut source: BcConnSource) -> Result<BcConnection> {
let subscribers: Arc<Subscriber> = Default::default();
let sink = Arc::new(Mutex::new(sink));
pub async fn new(mut sink: BcConnSink, source: BcConnSource) -> Result<BcConnection> {
let (sinker, sinker_rx) = channel::<Result<Bc>>(100);
let subs = subscribers.clone();
let (poll_commander, poll_commanded) = channel(200);
let mut poller = Poller {
subscribers: Default::default(),
sink: sinker.clone(),
reciever: ReceiverStream::new(poll_commanded),
};
let mut rx_thread = JoinSet::<Result<()>>::new();
let thread_poll_commander = poll_commander.clone();
let handle = tokio::task::spawn_blocking(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
runtime.block_on(
PollSender::new(thread_poll_commander)
.send_all(&mut source.map(|bc| Ok(PollCommand::Bc(Box::new(bc))))),
)
});
rx_thread.spawn(async move {
handle.await??;
Ok(())
});
rx_thread.spawn(async move { sink.send_all(&mut ReceiverStream::new(sinker_rx)).await });
let mut rx_thread = JoinSet::new();
let sink_thread = sink.clone();
rx_thread.spawn(async move {
loop {
let packet = source.next().await;
let bc = match packet {
Some(res) => res,
None => continue,
};
Self::poll(bc, &subs, &sink_thread).await?
poller.run().await?;
}
});
Ok(BcConnection {
sink,
subscribers,
sink: sinker,
poll_commander,
rx_thread: RwLock::new(rx_thread),
})
}
pub(super) async fn send(&self, bc: Bc) -> crate::Result<()> {
self.sink.lock().await.send(bc).await?;
self.sink.send(Ok(bc)).await?;
Ok(())
}
pub async fn subscribe(&self, msg_num: u16) -> Result<BcSubscription> {
let (tx, rx) = channel(100);
match self.subscribers.num.write().await.entry(msg_num) {
Entry::Vacant(vac_entry) => {
vac_entry.insert(tx);
}
Entry::Occupied(mut occ_entry) => {
if occ_entry.get().is_closed() {
occ_entry.insert(tx);
} else {
return Err(Error::SimultaneousSubscription { msg_num });
}
}
};
self.poll_commander
.send(PollCommand::AddSubscriber(msg_num, tx))
.await?;
Ok(BcSubscription::new(rx, msg_num as u32, self))
}
@@ -92,90 +97,18 @@ impl BcConnection {
where
T: Fn(&Bc) -> Option<Bc> + Send + Sync + 'static,
{
match self.subscribers.id.write().await.entry(msg_id) {
Entry::Vacant(vac_entry) => {
vac_entry.insert(Box::new(handler));
}
Entry::Occupied(_) => {
return Err(Error::SimultaneousSubscriptionId { msg_id });
}
};
self.poll_commander
.send(PollCommand::AddHandler(msg_id, Box::new(handler)))
.await?;
Ok(())
}
/// Stop a message handler created using [`handle_msg`]
#[allow(dead_code)] // Currently unused but added for future use
pub async fn unhandle_msg(&self, msg_id: u32) -> Result<()> {
self.subscribers.id.write().await.remove(&msg_id);
Ok(())
}
async fn poll(
response: Result<Bc>,
subscribers: &Subscriber,
sink: &Mutex<BcConnSink>,
) -> Result<()> {
match response {
Ok(response) => {
let msg_num = response.meta.msg_num;
let msg_id = response.meta.msg_id;
let mut remove_it_num = false;
match (
subscribers.id.read().await.get(&msg_id),
subscribers.num.read().await.get(&msg_num),
) {
(Some(occ), _) => {
if let Some(reply) = occ(&response) {
assert!(reply.meta.msg_num == response.meta.msg_num);
sink.lock().await.send(reply).await?;
}
}
(None, Some(occ)) => {
if occ.capacity() == 0 {
warn!("Reaching limit of channel");
warn!(
"Remaining: {} of {} message space for {} (ID: {})",
occ.capacity(),
occ.max_capacity(),
&msg_num,
&msg_id
);
} else {
trace!(
"Remaining: {} of {} message space for {} (ID: {})",
occ.capacity(),
occ.max_capacity(),
&msg_num,
&msg_id
);
}
if occ.send(Ok(response)).await.is_err() {
remove_it_num = true;
}
}
(None, None) => {
debug!(
"Ignoring uninteresting message id {} (number: {})",
msg_id, msg_num
);
trace!("Contents: {:?}", response);
}
}
if remove_it_num {
subscribers.num.write().await.remove(&msg_num);
}
}
Err(e) => {
for sub in subscribers.num.read().await.values() {
let _ = sub.send(Err(e.clone())).await;
}
subscribers.num.write().await.clear();
subscribers.id.write().await.clear();
return Err(e);
}
}
self.poll_commander
.send(PollCommand::RemoveHandler(msg_id))
.await?;
Ok(())
}
@@ -197,3 +130,116 @@ impl BcConnection {
Ok(())
}
}
enum PollCommand {
Bc(Box<Result<Bc>>),
AddHandler(u32, MsgHandler),
RemoveHandler(u32),
AddSubscriber(u16, Sender<Result<Bc>>),
}
struct Poller {
subscribers: Subscriber,
sink: Sender<Result<Bc>>,
reciever: ReceiverStream<PollCommand>,
}
impl Poller {
async fn run(&mut self) -> Result<()> {
while let Some(command) = self.reciever.next().await {
yield_now().await;
match command {
PollCommand::Bc(boxed_response) => match *boxed_response {
Ok(response) => {
let msg_num = response.meta.msg_num;
let msg_id = response.meta.msg_id;
let mut remove_it_num = false;
match (
self.subscribers.id.get(&msg_id),
self.subscribers.num.get(&msg_num),
) {
(Some(occ), _) => {
if let Some(reply) = occ(&response) {
assert!(reply.meta.msg_num == response.meta.msg_num);
self.sink.send(Ok(reply)).await?;
}
}
(None, Some(occ)) => {
if occ.capacity() == 0 {
warn!("Reaching limit of channel");
warn!(
"Remaining: {} of {} message space for {} (ID: {})",
occ.capacity(),
occ.max_capacity(),
&msg_num,
&msg_id
);
} else {
trace!(
"Remaining: {} of {} message space for {} (ID: {})",
occ.capacity(),
occ.max_capacity(),
&msg_num,
&msg_id
);
}
if occ.send(Ok(response)).await.is_err() {
remove_it_num = true;
}
}
(None, None) => {
debug!(
"Ignoring uninteresting message id {} (number: {})",
msg_id, msg_num
);
trace!("Contents: {:?}", response);
}
}
if remove_it_num {
self.subscribers.num.remove(&msg_num);
}
}
Err(e) => {
for sub in self.subscribers.num.values() {
let _ = sub.send(Err(e.clone())).await;
}
self.subscribers.num.clear();
self.subscribers.id.clear();
return Err(e);
}
},
PollCommand::AddHandler(msg_id, handler) => {
match self.subscribers.id.entry(msg_id) {
Entry::Vacant(vac_entry) => {
vac_entry.insert(handler);
}
Entry::Occupied(_) => {
return Err(Error::SimultaneousSubscriptionId { msg_id });
}
};
}
PollCommand::RemoveHandler(msg_id) => {
self.subscribers.id.remove(&msg_id);
}
PollCommand::AddSubscriber(msg_num, tx) => {
match self.subscribers.num.entry(msg_num) {
Entry::Vacant(vac_entry) => {
vac_entry.insert(tx);
}
Entry::Occupied(mut occ_entry) => {
if occ_entry.get().is_closed() {
occ_entry.insert(tx);
} else {
let _ = tx
.send(Err(Error::SimultaneousSubscription { msg_num }))
.await;
}
}
};
}
}
}
Ok(())
}
}
@@ -48,6 +48,7 @@ impl<'a> Stream for BcPayloadStream<'a> {
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<IoResult<Vec<u8>>>> {
// log::debug!("PayloadStream: Poll");
match Pin::new(&mut self.rx).poll_next(cx) {
Poll::Ready(Some(Ok(Bc {
body:
@@ -57,17 +58,27 @@ impl<'a> Stream for BcPayloadStream<'a> {
}),
..
}))) => {
// log::debug!("PayloadStream: Data");
return Poll::Ready(Some(Ok(data)));
}
Poll::Ready(Some(Ok(_bc))) => {
// trace!("Got other BC in payload stream");
// log::debug!("PayloadStream: Other Data!");
}
Poll::Ready(Some(Err(e))) => {
return Poll::Ready(Some(Err(IoError::new(ErrorKind::Other, e))))
// log::debug!("PayloadStream: Err");
return Poll::Ready(Some(Err(IoError::new(ErrorKind::Other, e))));
}
Poll::Ready(None) => {
// log::debug!("PayloadStream: None");
return Poll::Ready(None);
}
Poll::Pending => {
// log::debug!("PayloadStream: Pend");
return Poll::Pending;
}
Poll::Ready(None) => return Poll::Ready(None),
Poll::Pending => return Poll::Pending,
}
// log::debug!("PayloadStream: Default Pend and Wake");
cx.waker().wake_by_ref();
Poll::Pending
}
@@ -11,7 +11,7 @@ use crate::bcudp::xml::*;
use crate::{Error, Result};
use futures::{
sink::SinkExt,
stream::{FuturesUnordered, SplitSink, StreamExt},
stream::{FuturesUnordered, StreamExt},
};
use lazy_static::lazy_static;
use local_ip_address::local_ip;
@@ -24,7 +24,7 @@ use tokio::{
net::UdpSocket,
sync::{
mpsc::{channel, Receiver, Sender},
Mutex, RwLock,
RwLock,
},
task::JoinSet,
time::{interval, timeout, Duration},
@@ -58,7 +58,7 @@ struct UidLookupResults {
pub(crate) struct Discovery {}
const MTU: u32 = 1030;
const MTU: u32 = 1350;
lazy_static! {
static ref P2P_RELAY_HOSTNAMES: [&'static str; 10] = [
"p2p.reolink.com",
@@ -83,7 +83,7 @@ type ArcFramedSocket = UdpFramed<BcUdpCodex, Arc<UdpSocket>>;
pub(crate) struct Discoverer {
socket: Arc<UdpSocket>,
handle: RwLock<JoinSet<()>>,
writer: Mutex<SplitSink<ArcFramedSocket, (BcUdp, SocketAddr)>>,
writer: Sender<Result<(BcUdp, SocketAddr)>>,
subsribers: Subscriber,
local_addr: SocketAddr,
}
@@ -94,13 +94,14 @@ impl Discoverer {
let local_addr = socket.local_addr()?;
let inner: ArcFramedSocket = UdpFramed::new(socket.clone(), BcUdpCodex::new());
let (writer, mut reader) = inner.split();
let (mut writer, mut reader) = inner.split();
let mut set = JoinSet::new();
let subsribers: Subscriber = Default::default();
let thread_subscriber = subsribers.clone();
set.spawn(async move {
loop {
tokio::task::yield_now().await;
match reader.next().await {
Some(Ok((BcUdp::Discovery(bcudp), addr))) => {
let mut tid = bcudp.tid;
@@ -138,10 +139,15 @@ impl Discoverer {
}
});
let (sinker, sinker_rx) = channel(100);
set.spawn(async move {
let _ = writer.send_all(&mut ReceiverStream::new(sinker_rx)).await;
});
Ok(Discoverer {
socket,
handle: RwLock::new(set),
writer: Mutex::new(writer),
writer: sinker,
subsribers,
local_addr,
})
@@ -183,16 +189,24 @@ impl Discoverer {
F: Fn(UdpDiscovery, SocketAddr) -> Option<T>,
{
let mut reply = ReceiverStream::new(self.subscribe(0).await?);
loop {
let (reply, addr) = reply.next().await.ok_or(Error::ConnectionUnavaliable)??;
if let Some(result) = map(reply, addr) {
return Ok(result);
}
tokio::select! {
v = async {
loop {
let (reply, addr) = reply.next().await.ok_or(Error::ConnectionUnavaliable)??;
if let Some(result) = map(reply, addr) {
return Ok(result);
}
}
} => v,
_ = tokio::time::sleep(*MAXIMUM_WAIT) => Err::<T, Error>(Error::DiscoveryTimeout),
}
}
async fn send(&self, disc: BcUdp, addr: SocketAddr) -> Result<()> {
self.writer.lock().await.send((disc, addr)).await
self.writer
.send(Ok((disc, addr)))
.await
.map_err(Error::from)
}
fn local_addr(&self) -> &SocketAddr {
@@ -208,6 +222,7 @@ impl Discoverer {
where
F: Fn(UdpDiscovery, SocketAddr) -> Option<T>,
{
trace!("Retrying send");
disc.tid = 0; // Must be random to avoid simulatenous subscription errors
let mut set = FuturesUnordered::new();
for dest in dests.iter() {
@@ -216,6 +231,7 @@ impl Discoverer {
// Get what ever completes first
while let Some(result) = set.next().await {
trace!("Result: {}", result.is_ok());
if let Ok(ret) = result {
return Ok(ret);
}
@@ -347,15 +363,20 @@ impl Discoverer {
/// On success it returns the M2cQr that the p2p relay
/// server has about the UID
async fn uid_loopup(&self, uid: &str) -> Result<UidLookupResults> {
let mut addrs = vec![];
for p2p_relay in P2P_RELAY_HOSTNAMES.iter() {
addrs.append(
&mut format!("{}:9999", p2p_relay)
.to_socket_addrs()
.map(|i| i.collect::<Vec<SocketAddr>>())
.unwrap_or_else(|_| vec![]),
);
}
let task = tokio::task::spawn_blocking(move || {
let mut addrs = vec![];
for p2p_relay in P2P_RELAY_HOSTNAMES.iter() {
addrs.append(
&mut format!("{}:9999", p2p_relay)
.to_socket_addrs()
.map(|i| i.collect::<Vec<SocketAddr>>())
.unwrap_or_else(|_| vec![]),
);
}
addrs
});
let addrs = timeout(*MAXIMUM_WAIT, task).await??;
trace!("Uid lookup to: {:?}", addrs);
let msg = UdpDiscovery {
tid: 0,
payload: UdpXml {
@@ -366,6 +387,7 @@ impl Discoverer {
..Default::default()
},
};
trace!("Sending look up {:?}", msg);
let (packet, _) = self
.retry_send_multi(msg, addrs.as_slice(), |bc, addr| match bc {
UdpDiscovery {
@@ -379,7 +401,7 @@ impl Discoverer {
_ => None,
})
.await?;
trace!("Look up complete");
Ok(UidLookupResults {
reg: SocketAddr::new(packet.reg.ip.parse()?, packet.reg.port),
relay: SocketAddr::new(packet.relay.ip.parse()?, packet.relay.port),
@@ -423,6 +445,8 @@ impl Discoverer {
},
};
trace!("Registering: {:?}", msg.payload.c2r_c);
// Send and await acceptance
let (sid, dev, dmap, relay) = self
.retry_send(msg, lookup.reg, |bc, _| match bc {
@@ -844,6 +868,7 @@ impl Discoverer {
let mut interval = interval(Duration::from_secs(1));
self.handle.write().await.spawn(async move {
loop {
tokio::task::yield_now().await;
interval.tick().await;
let msg = BcUdp::Discovery(UdpDiscovery {
tid,
@@ -872,6 +897,7 @@ impl Discoverer {
let mut interval = interval(Duration::from_secs(1));
self.handle.write().await.spawn(async move {
loop {
tokio::task::yield_now().await;
interval.tick().await;
let msg = BcUdp::Discovery(UdpDiscovery {
tid,
@@ -993,20 +1019,25 @@ impl Discovery {
// directly
#[allow(unused)]
pub(crate) async fn remote(uid: &str) -> Result<DiscoveryResult> {
trace!("Start remote");
let mut discoverer = Discoverer::new().await?;
let client_id = generate_cid();
trace!("client_id: {}", client_id);
let lookup = discoverer.uid_loopup(uid).await?;
trace!("lookup: {:?}", lookup);
let reg_addr = lookup.reg;
let relay_addr = lookup.relay;
let reg_result = discoverer.register_address(uid, client_id, &lookup).await?;
trace!("reg_result: {:?}", reg_result);
let connect_result = tokio::select! {
v = discoverer.client_initiated_dev(&reg_result) => {v},
v = discoverer.device_initiated_dev(&reg_result) => {v},
}?;
trace!("connect_result: {:?}", connect_result);
let (socket, keep_alive_tasks) = discoverer.into_socket().await;
Ok(DiscoveryResult {
@@ -28,7 +28,7 @@ use tokio_util::{
udp::UdpFramed,
};
const MTU: usize = 1030;
const MTU: usize = 1350;
const UDPDATA_HEADER_SIZE: usize = 20;
pub(crate) struct UdpSource {
@@ -198,10 +198,12 @@ impl Sink<(BcUdp, SocketAddr)> for BcUdpSource {
}
}
#[allow(dead_code)]
enum State {
Normal,
Flushing,
Closed,
Normal, // Normal recieve
Flushing, // Used to send ack packets and things in the buffer
Closed, // Used to shutdown
YieldNow, // Used to ensure we rest between polling packets so as to not starve the runtime
}
pub(crate) struct UdpPayloadSource {
@@ -277,14 +279,12 @@ impl UdpPayloadSource {
fn maintanence(&mut self, cx: &mut Context<'_>) {
// Check for periodic resends
if self.resend_interval.poll_tick(cx).is_ready() {
trace!("UDPSource.RecievedPacket: Resend");
for (_, resend) in self.sent.iter() {
self.send_buffer.push_back(BcUdp::Data(resend.clone()));
}
}
if self.ack_interval.poll_tick(cx).is_ready() {
let ack = BcUdp::Ack(self.build_send_ack());
trace!("UDPSource.RecievedPacket: QueueingAck");
self.send_buffer.push_back(ack);
self.state = State::Flushing;
}
@@ -295,21 +295,19 @@ impl Stream for UdpPayloadSource {
type Item = IoResult<Vec<u8>>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
trace!("UdpPayloadSource.poll_next");
let camera_addr = self.inner.addr;
let mut this = self.get_mut();
let this = self.get_mut();
match this.state {
State::Normal => {
trace!("UdpPayloadSource.State: Normal");
// this.state = State::YieldNow;
// Handle resend events
this.maintanence(cx);
// Data ready to go
if let Some(payload) = this.recieved.remove(&this.packets_want) {
trace!("UdpPayloadSource.Data: PreReady: {}", this.packets_want);
this.packets_want += 1;
// error!("packets_want: {}", this.packets_want);
return Poll::Ready(Some(Ok(payload)));
}
// Handle resend events
this.maintanence(cx);
// Normal behaviors
match this.inner.poll_next_unpin(cx) {
Poll::Ready(Some(Ok((
@@ -324,20 +322,12 @@ impl Stream for UdpPayloadSource {
&& packet_id >= this.packets_want =>
{
if packet_id == this.packets_want {
trace!(
"UdpPayloadSource.RecievedPacker: NewData: {}",
this.packets_want
);
this.packets_want += 1;
// error!("packets_want: {}", this.packets_want);
let ack = BcUdp::Ack(this.build_send_ack());
this.send_buffer.push_back(ack);
return Poll::Ready(Some(Ok(payload)));
} else {
trace!(
"UdpPayloadSource.RecievedPacker: Future Data: {}",
packet_id
);
this.recieved.insert(packet_id, payload);
}
}
@@ -358,17 +348,14 @@ impl Stream for UdpPayloadSource {
BcUdp::Ack(ack @ UdpAck { connection_id, .. }),
addr,
)))) if connection_id == this.client_id && addr == camera_addr => {
trace!("UdpPayloadSource.RecievedPacket: Ack: {:?}", ack);
this.handle_ack(ack);
// Rather then immediatly flush wait for the next call
// this.state = State::Flushing;
}
Poll::Ready(Some(Err(e))) => {
trace!("UdpPayloadSource.RecievedPacker: Error");
return Poll::Ready(Some(Err(IoError::new(ErrorKind::Other, e))));
}
Poll::Ready(None) => {
trace!("UdpPayloadSource.RecievedPacker: Empty");
return Poll::Ready(None);
}
Poll::Pending => {
@@ -386,24 +373,23 @@ impl Stream for UdpPayloadSource {
// }
}
}
State::Flushing => {
trace!("UdpPayloadSource.State: Flushing");
match this.poll_flush_unpin(cx) {
Poll::Ready(Ok(())) => {
this.state = State::Normal;
}
Poll::Ready(Err(e)) => {
return Poll::Ready(Some(Err(e)));
}
Poll::Pending => {
return Poll::Pending;
}
State::Flushing => match this.poll_flush_unpin(cx) {
Poll::Ready(Ok(())) => {
this.state = State::Normal;
}
}
Poll::Ready(Err(e)) => {
return Poll::Ready(Some(Err(e)));
}
Poll::Pending => {
return Poll::Pending;
}
},
State::Closed => {
trace!("UdpPayloadSource.State: Closed");
return Poll::Ready(Some(Err(IoError::from(ErrorKind::ConnectionAborted))));
}
State::YieldNow => {
this.state = State::Normal;
}
}
cx.waker().wake_by_ref();
Poll::Pending
@@ -419,7 +405,7 @@ impl Sink<Vec<u8>> for UdpPayloadSource {
) -> Poll<std::result::Result<(), Self::Error>> {
let this = self.get_mut();
match this.state {
State::Normal => this
State::Normal | State::YieldNow => this
.inner
.poll_ready_unpin(cx)
.map_err(|e| IoError::new(ErrorKind::Other, e)),
@@ -431,7 +417,7 @@ impl Sink<Vec<u8>> for UdpPayloadSource {
}
}
fn start_send(self: Pin<&mut Self>, item: Vec<u8>) -> std::result::Result<(), Self::Error> {
let mut this = self.get_mut();
let this = self.get_mut();
for chunk in item.chunks(MTU - UDPDATA_HEADER_SIZE) {
let udp_data = UdpData {
connection_id: this.camera_id,
@@ -496,11 +482,12 @@ impl Sink<Vec<u8>> for UdpPayloadSource {
cx.waker().wake_by_ref();
Poll::Pending
}
fn poll_close(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), Self::Error>> {
let mut this = self.get_mut();
let this = self.get_mut();
if let State::Closed = this.state {
return Poll::Ready(Ok(()));
}
+36
View File
@@ -10,6 +10,22 @@ pub enum Error {
#[error(display = "IO Error: {:?}", _0)]
Io(#[error(source)] 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),
/// Raised when fails to parse time from the camera
#[error(display = "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),
/// /// Raised when fails to parse time from the camera
#[error(display = "Error in time conversion")]
TimeTryFrom(#[error(source)] time::error::TryFromParsed),
/// Raised when a Bc reply was not understood
#[error(display = "Communication error")]
UnintelligibleReply {
@@ -23,6 +39,10 @@ pub enum Error {
#[error(display = "Camera responded with Service Unavaliable")]
CameraServiceUnavaliable,
/// Raised when the camera responds with a status code over than OK during login
#[error(display = "Camera responded with Err during login")]
CameraLoginFail,
/// Raised when a connection is dropped.
#[error(display = "Dropped connection")]
DroppedConnection,
@@ -35,6 +55,10 @@ pub enum Error {
#[error(display = "Dropped connection")]
BroadcastDroppedConnectionTry(#[error(source)] tokio::sync::broadcast::error::TryRecvError),
/// Raised when a connection is dropped during a TryRecv event
#[error(display = "Send Error")]
TokioBcSendError,
/// Raised when the TIMEOUT is reach
#[error(display = "Timeout")]
Timeout(#[error(source)] std::sync::Arc<tokio::time::error::Elapsed>),
@@ -178,6 +202,18 @@ impl From<std::io::Error> for Error {
}
}
impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self {
Error::TokioBcSendError
}
}
impl<T> From<tokio_util::sync::PollSendError<T>> for Error {
fn from(_: tokio_util::sync::PollSendError<T>) -> Self {
Error::TokioBcSendError
}
}
impl From<cookie_factory::GenError> for Error {
fn from(k: cookie_factory::GenError) -> Self {
Error::GenError(std::sync::Arc::new(k))
+1 -1
View File
@@ -136,7 +136,7 @@ impl BcCamera {
sub_login.send(modern_login).await?;
let modern_reply = sub_login.recv().await?;
if modern_reply.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable);
return Err(Error::CameraLoginFail);
}
match modern_reply.body {
+1
View File
@@ -216,6 +216,7 @@ impl BcCamera {
let mut sub = connection.subscribe(msg_num).await?;
loop {
tokio::task::yield_now().await;
let msg = sub.recv().await;
let status = match msg {
Ok(motion_msg) => {
+3
View File
@@ -15,6 +15,8 @@ pub enum Direction {
In,
/// To zoom the camera Out (may be done with cropping depending on camera model)
Out,
/// To stop currently active PTZ command
Stop,
}
impl BcCamera {
@@ -36,6 +38,7 @@ impl BcCamera {
Direction::Out => {
todo!()
}
Direction::Stop => "stop",
}
.to_string();
let send = Bc {
+72
View File
@@ -0,0 +1,72 @@
use super::{BcCamera, Error, Result};
use crate::bc::{model::*, xml::*};
/// Specifies the phone type for the push notification
pub enum PhoneType {
/// Specify that this is an ios push notfication
///
/// In this case the token must be the APNS
Ios,
/// Specify that this is an andriod push notfication
///
/// In this case the token must firebase cloud messaging token
Android,
}
impl BcCamera {
/// Convenience method for andriod of `[send_pushinfo]`
pub async fn send_pushinfo_android(&self, token: &str, client_id: &str) -> Result<()> {
self.send_pushinfo(token, client_id, PhoneType::Android)
.await
}
/// Convenience method for andriod of `[send_pushinfo]`
pub async fn send_pushinfo_ios(&self, token: &str, client_id: &str) -> Result<()> {
self.send_pushinfo(token, client_id, PhoneType::Ios).await
}
/// Send the push info to regsiter for push notfications
pub async fn send_pushinfo(
&self,
token: &str,
client_id: &str,
phone_type: PhoneType,
) -> Result<()> {
let connection = self.get_connection();
let msg_num = self.new_message_num();
let mut sub = connection.subscribe(msg_num).await?;
let phone_type_str = match phone_type {
PhoneType::Ios => "reo_iphone",
PhoneType::Android => "reo_fcm",
};
let msg = Bc {
meta: BcMeta {
msg_id: MSG_ID_PUSH_INFO,
channel_id: self.channel_id,
msg_num,
response_code: 0,
stream_type: 0,
class: 0x6414,
},
body: BcBody::ModernMsg(ModernMsg {
extension: None,
payload: Some(BcPayloads::BcXml(BcXml {
push_info: Some(PushInfo {
token: token.to_owned(),
phone_type: phone_type_str.to_owned(),
client_id: client_id.to_owned(),
}),
..Default::default()
})),
}),
};
sub.send(msg).await?;
let msg = sub.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable);
}
Ok(())
}
}
+51 -4
View File
@@ -31,7 +31,6 @@ pub enum StreamKind {
///
/// When this object is dropped the streaming is stopped
pub struct StreamData {
#[allow(dead_code)]
handle: Option<JoinHandle<Result<()>>>,
rx: Receiver<Result<BcMedia>>,
abort_handle: Arc<AtomicBool>,
@@ -41,9 +40,27 @@ impl StreamData {
/// Pull data from the camera's buffer
/// This returns raw BcMedia packets
pub async fn get_data(&mut self) -> Result<Result<BcMedia>> {
if let Some(handle) = self.handle.as_mut() {
if handle.is_finished() {
self.abort_handle.store(true, Ordering::Relaxed);
handle.await??;
return Err(Error::DroppedConnection);
}
} else {
self.abort_handle.store(true, Ordering::Relaxed);
return Err(Error::DroppedConnection);
}
// debug!("StreamData: Get");
match self.rx.recv().await {
Some(data) => Ok(data),
None => Err(Error::DroppedConnection),
Some(data) => {
// debug!("StreamData: Got");
Ok(data)
}
None => {
// debug!("StreamData: Drop");
self.abort_handle.store(true, Ordering::Relaxed);
Err(Error::DroppedConnection)
}
}
}
}
@@ -88,6 +105,7 @@ impl BcCamera {
let channel_id = self.channel_id;
let handle = task::spawn(async move {
tokio::task::yield_now().await;
let mut sub_video = connection.subscribe(msg_num).await?;
// On an E1 and swann cameras:
@@ -164,17 +182,23 @@ impl BcCamera {
let mut media_sub = sub_video.bcmedia_stream(strict);
while !abort_handle_thread.load(Ordering::Relaxed) {
// debug!("Stream: Get");
if 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");
} else {
break;
}
}
}
// debug!("Stream: Stopping");
let stop_video = Bc::new_from_xml(
BcMeta {
msg_id: MSG_ID_VIDEO_STOP,
@@ -194,8 +218,31 @@ impl BcCamera {
..Default::default()
},
);
// debug!("Stream: Send Stop");
sub_video.send(stop_video).await?;
// debug!("Stream: Sent Stop");
tokio::select! {
v = async {
loop {
let msg = sub_video.recv().await?;
if let BcMeta {
response_code: 200,
msg_id: MSG_ID_VIDEO_STOP,
..
} = msg.meta {
return Ok(());
}
else if let BcMeta {
msg_id: MSG_ID_VIDEO_STOP,
..
} = msg.meta {
return Err(Error::CameraServiceUnavaliable);
}
}
} => v,
_ = tokio::time::sleep(tokio::time::Duration::from_secs(2)) => {Ok(())},
}?;
Ok(())
});
+26 -11
View File
@@ -1,6 +1,9 @@
use super::{BcCamera, Error, Result};
use crate::bc::{model::*, xml::*};
use time::{date, Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};
use std::convert::{TryFrom, TryInto};
use time::{
macros::date, parsing::Parsed, Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset,
};
impl BcCamera {
///
@@ -115,9 +118,9 @@ impl BcCamera {
//osd_format: Some("MDY".to_string()),
time_format: Some(0),
// Reolink uses positive seconds to indicate a negative UTC offset:
time_zone: Some(-timestamp.offset().as_seconds()),
time_zone: Some(-timestamp.offset().whole_seconds()),
year: Some(timestamp.year()),
month: Some(timestamp.month()),
month: Some(timestamp.month().into()),
day: Some(timestamp.day()),
hour: Some(timestamp.hour()),
minute: Some(timestamp.minute()),
@@ -153,14 +156,26 @@ fn try_build_timestamp(
hour: u8,
minute: u8,
second: u8,
) -> std::result::Result<OffsetDateTime, time::ComponentRangeError> {
let date = Date::try_from_ymd(year, month, day)?;
let time = Time::try_from_hms(hour, minute, second)?;
let offset = if timezone > 0 {
UtcOffset::west_seconds(timezone as u32)
} else {
UtcOffset::east_seconds(-timezone as u32)
};
) -> std::result::Result<OffsetDateTime, crate::Error> {
let date = Date::try_from(
Parsed::new()
.with_year(year)
.ok_or(Error::TimeParse)?
.with_month(month.try_into()?)
.ok_or(Error::TimeParse)?
.with_day(day.try_into()?)
.ok_or(Error::TimeParse)?,
)?;
let time = Time::try_from(
Parsed::new()
.with_hour_24(hour)
.ok_or(Error::TimeParse)?
.with_minute(minute)
.ok_or(Error::TimeParse)?
.with_second(second)
.ok_or(Error::TimeParse)?,
)?;
let offset = UtcOffset::from_whole_seconds(timezone)?;
Ok(PrimitiveDateTime::new(date, time).assume_offset(offset))
}
+25 -10
View File
@@ -4,7 +4,7 @@
//!
use crate::bcmedia::model::*;
use crate::{Error, Result};
use bytes::{Buf, BytesMut};
use bytes::BytesMut;
use log::*;
use tokio_util::codec::{Decoder, Encoder};
@@ -12,11 +12,15 @@ pub struct BcMediaCodex {
/// If true we will not search for the start of the next packet
/// in the event that the stream appears to be corrupted
strict: bool,
amount_skipped: usize,
}
impl BcMediaCodex {
pub(crate) fn new(strict: bool) -> Self {
Self { strict }
Self {
strict,
amount_skipped: 0,
}
}
}
@@ -36,19 +40,29 @@ 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
/// there are bytes left on the stream
fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>> {
match self.decode(buf)? {
Some(frame) => Ok(Some(frame)),
None => Ok(None),
}
}
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
let mut amount_skipped: usize = 0;
loop {
match { BcMedia::deserialize(src) } {
Ok(bc) => {
if amount_skipped > 0 {
debug!("Amount skipped to restore stream: {}", amount_skipped);
if self.amount_skipped > 0 {
debug!("Amount skipped to restore stream: {}", self.amount_skipped);
self.amount_skipped = 0;
}
return Ok(Some(bc));
}
Err(Error::NomIncomplete(_)) => {
if amount_skipped > 0 {
debug!("Amount skipped to restore stream: {}", amount_skipped);
if self.amount_skipped > 0 {
debug!("Amount skipped to restore stream: {}", self.amount_skipped);
self.amount_skipped = 0;
}
return Ok(None);
}
@@ -58,12 +72,13 @@ impl Decoder for BcMediaCodex {
} else if src.is_empty() {
return Ok(None);
} else {
if amount_skipped == 0 {
if self.amount_skipped == 0 {
debug!("Error in stream attempting to restore");
trace!(" Stream Error: {:?}", e);
}
src.advance(1);
amount_skipped += 1;
// Drop the whole packet and wait for a packet that starts with magic
self.amount_skipped += src.len();
src.clear();
continue;
}
}
+11 -11
View File
@@ -2,7 +2,7 @@ use super::model::BcMediaIframe;
use super::model::*;
use crate::Error;
use bytes::{Buf, BytesMut};
use nom::{combinator::*, error::context, number::streaming::*, take};
use nom::{bytes::streaming::take, combinator::*, error::context, number::streaming::*};
type IResult<I, O, E = nom::error::VerboseError<I>> = Result<(I, O), nom::Err<E>>;
@@ -182,18 +182,18 @@ fn bcmedia_iframe(buf: &[u8]) -> IResult<&[u8], BcMediaIframe> {
};
let (buf, _unknown_remained) = if additional_header_size > 4 {
let remainder = additional_header_size - 4;
let (buf, unknown_remained) = take!(buf, remainder)?;
let (buf, unknown_remained) = take(remainder)(buf)?;
(buf, Some(unknown_remained))
} else {
(buf, None)
};
let (buf, data_slice) = take!(buf, payload_size)?;
let (buf, data_slice) = take(payload_size)(buf)?;
let pad_size = match payload_size % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
let (buf, _padding) = take(pad_size)(buf)?;
assert_eq!(payload_size as usize, data_slice.len());
let video_type = match video_type_str {
@@ -223,13 +223,13 @@ fn bcmedia_pframe(buf: &[u8]) -> IResult<&[u8], BcMediaPframe> {
let (buf, additional_header_size) = le_u32(buf)?;
let (buf, microseconds) = le_u32(buf)?;
let (buf, _unknown_b) = le_u32(buf)?;
let (buf, _additional_header) = take!(buf, additional_header_size)?;
let (buf, data_slice) = take!(buf, payload_size)?;
let (buf, _additional_header) = take(additional_header_size)(buf)?;
let (buf, data_slice) = take(payload_size)(buf)?;
let pad_size = match payload_size % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
let (buf, _padding) = take(pad_size)(buf)?;
assert_eq!(payload_size as usize, data_slice.len());
let video_type = match video_type_str {
@@ -252,12 +252,12 @@ fn bcmedia_pframe(buf: &[u8]) -> IResult<&[u8], BcMediaPframe> {
fn bcmedia_aac(buf: &[u8]) -> IResult<&[u8], BcMediaAac> {
let (buf, payload_size) = le_u16(buf)?;
let (buf, _payload_size_b) = le_u16(buf)?;
let (buf, data_slice) = take!(buf, payload_size)?;
let (buf, data_slice) = take(payload_size)(buf)?;
let pad_size = match payload_size as u32 % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
let (buf, _padding) = take(pad_size)(buf)?;
Ok((
buf,
@@ -281,12 +281,12 @@ fn bcmedia_adpcm(buf: &[u8]) -> IResult<&[u8], BcMediaAdpcm> {
// On other cameras is half the block size without the header
let (buf, _half_block_size) = le_u16(buf)?;
let block_size = payload_size - SUB_HEADER_SIZE;
let (buf, data_slice) = take!(buf, block_size)?;
let (buf, data_slice) = take(block_size)(buf)?;
let pad_size = match payload_size as u32 % PAD_SIZE {
0 => 0,
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
let (buf, _padding) = take(pad_size)(buf)?;
Ok((
buf,
+9
View File
@@ -33,6 +33,15 @@ 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
/// there are bytes left on the stream
fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>> {
match self.decode(buf)? {
Some(frame) => Ok(Some(frame)),
None => Ok(None),
}
}
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
match { BcUdp::deserialize(src) } {
Ok(BcUdp::Discovery(UdpDiscovery {
+9 -8
View File
@@ -2,10 +2,11 @@ use super::{crc::calc_crc, model::*, xml::*, xml_crypto::decrypt};
use crate::Error;
use bytes::{Buf, BytesMut};
use nom::{
bytes::streaming::take,
combinator::*,
error::{context as error_context, ContextError, ErrorKind, ParseError},
number::streaming::*,
take, Err,
Err,
};
type IResult<I, O, E = nom::error::VerboseError<I>> = Result<(I, O), nom::Err<E>>;
@@ -67,7 +68,7 @@ fn udp_disc(buf: &[u8]) -> IResult<&[u8], UdpDiscovery> {
)(buf)?;
let (buf, tid) = error_context("DISC: Missing TID", le_u32)(buf)?;
let (buf, checksum) = error_context("DISC: Missing checksum", le_u32)(buf)?;
let (buf, enc_data_slice) = take!(buf, payload_size)?;
let (buf, enc_data_slice) = take(payload_size)(buf)?;
let actual_checksum = calc_crc(enc_data_slice);
assert_eq!(checksum, actual_checksum);
@@ -98,11 +99,11 @@ fn udp_ack(buf: &[u8]) -> IResult<&[u8], UdpAck> {
let (buf, _unknown) = error_context("ACK: Missing unknown", le_u32)(buf)?;
let (buf, payload_size) = error_context("ACK: Missing payload_size", le_u32)(buf)?;
let (buf, payload) = if payload_size > 0 {
let (buf, t_payload) = take!(buf, payload_size)?; // It is a binary payload of
// `00 01 01 01 01 00 01`
// This is a truth map of missing packets
// since last contigious packet_id up
// to the last packet we sent and it recieved
let (buf, t_payload) = take(payload_size)(buf)?; // It is a binary payload of
// `00 01 01 01 01 00 01`
// This is a truth map of missing packets
// since last contigious packet_id up
// to the last packet we sent and it recieved
(buf, t_payload.to_vec())
} else {
(buf, vec![])
@@ -123,7 +124,7 @@ fn udp_data(buf: &[u8]) -> IResult<&[u8], UdpData> {
error_context("DATA: Unable to verify UnownA", verify(le_u32, |&x| x == 0))(buf)?;
let (buf, packet_id) = error_context("DATA: Missing packet_id", le_u32)(buf)?;
let (buf, payload_size) = error_context("DATA: Missing payload_size", le_u32)(buf)?;
let (buf, payload) = take!(buf, payload_size)?;
let (buf, payload) = take(payload_size)(buf)?;
let data = UdpData {
connection_id,
+1 -1
View File
@@ -2,7 +2,7 @@
use std::io::{Read, Write};
// YaSerde is currently naming the traits and the derive macros identically
use yaserde::{ser::Config, YaDeserialize, YaSerialize};
use yaserde::ser::Config;
use yaserde_derive::{YaDeserialize, YaSerialize};
/// The top level of the UDP xml is P2P
+4 -4
View File
@@ -7,11 +7,11 @@ edition = "2018"
license = "AGPL-3.0-or-later"
[dependencies]
aes = "0.6"
aes = "0.8.2"
anyhow = "1.0.42"
cfb-mode = "0.6"
cfb-mode = "0.8.2"
env_logger = "*"
hex-string = "0.1.0"
md5 = "0.7"
md5 = "0.7.0"
requestty = "0.5.0"
log = { version = "0.4" }
log = "0.4.17"
+8 -4
View File
@@ -6,15 +6,19 @@ use hex_string::HexString;
use requestty::Question;
use std::convert::TryInto;
use aes::Aes128;
use cfb_mode::cipher::{NewStreamCipher, StreamCipher};
use cfb_mode::Cfb;
use aes::{
cipher::{AsyncStreamCipher, KeyIvInit},
Aes128,
};
use cfb_mode::Decryptor;
type Aes128CfbDec = Decryptor<Aes128>;
const IV: &[u8] = b"0123456789abcdef";
fn decrypt(buf: &[u8], aeskey: &[u8; 16]) -> Vec<u8> {
let mut decrypted = buf.to_vec();
Cfb::<Aes128>::new(aeskey.into(), IV.into()).decrypt(&mut decrypted);
Aes128CfbDec::new(aeskey.into(), IV.into()).decrypt(&mut decrypted);
decrypted
}
+21
View File
@@ -0,0 +1,21 @@
[package]
name = "pushnoti"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.71"
clap = { version = "4.2.2", features = ["derive", "cargo"] }
env_logger = "0.10.0"
fcm-push-listener = "2.0.1"
lazy_static = "1.4.0"
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
neolink_core = { path = "../core", version = "0.5" }
regex = "1.8.1"
serde = "1.0.163"
tokio = { version = "1.28.1", features = ["full"] }
toml = "0.7.3"
validator = "0.16.0"
validator_derive = "0.16.0"
+273
View File
@@ -0,0 +1,273 @@
# Reolink Push Notifications
This is the test crate to experiment with RE-ing the push notifications for a
camera
The goal here is to connect to Google's firebase cloud messaging system (FCM)
and recieve push notificaions for motion updates. This way we can get motion
notifications without being logged in to the camera and this will let us
disconnect from battery cameras and save power.
The following is the reolink data for google extracted from ios.
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AD_UNIT_ID_FOR_BANNER_TEST</key>
<string>ca-app-pub-3940256099942544/2934735716</string>
<key>AD_UNIT_ID_FOR_INTERSTITIAL_TEST</key>
<string>ca-app-pub-3940256099942544/4411468910</string>
<key>API_KEY</key>
<string>AIzaSyAV5l5DbbAkYB5X61wgn81Lb2f7s1h-ANE</string>
<key>BUNDLE_ID</key>
<string>com.reo.link</string>
<key>CLIENT_ID</key>
<string>696841269229-mfqfluqdkb0s2raassbsul48h09l2eiv.apps.googleusercontent.com</string>
<key>DATABASE_URL</key>
<string>https://reolink-for-ios.firebaseio.com</string>
<key>GCM_SENDER_ID</key>
<string>696841269229</string>
<key>GOOGLE_APP_ID</key>
<string>1:696841269229:ios:690529b18debb2e3</string>
<key>IS_ADS_ENABLED</key>
<false/>
<key>IS_ANALYTICS_ENABLED</key>
<false/>
<key>IS_APPINVITE_ENABLED</key>
<false/>
<key>IS_GCM_ENABLED</key>
<false/>
<key>IS_SIGNIN_ENABLED</key>
<false/>
<key>PLIST_VERSION</key>
<string>1</string>
<key>PROJECT_ID</key>
<string>reolink-for-ios</string>
<key>REVERSED_CLIENT_ID</key>
<string>com.googleusercontent.apps.696841269229-mfqfluqdkb0s2raassbsul48h09l2eiv</string>
<key>STORAGE_BUCKET</key>
<string>reolink-for-ios.appspot.com</string>
</dict>
</plist>
```
Key information from this is
```xml
<key>GCM_SENDER_ID</key>
<string>696841269229</string>
```
Which we use to register for FCM
---
p.s. maybe some one can do something fun with the API
key not sure what thats doing there since anyone can
extract that.
```xml
<key>API_KEY</key>
<string>AIzaSyAV5l5DbbAkYB5X61wgn81Lb2f7s1h-ANE</string>
```
---
On android the details are little harder for me to extract (ios here). By
extracting the apk with apktook I found the following:
```xml
<string name="gcm_defaultSenderId">743639030586</string>
<string name="google_api_key">AIzaSyBEUIuWHnnOEwFahxWgQB4Yt4NsgOmkPyE</string>
<string name="google_app_id">1:743639030586:android:86f60a4fb7143876</string>
<string name="google_client_id">743639030586-ove5uqrausoqtjjkev4b1tk4vjdfpt4l.apps.googleusercontent.com</string>
```
Where the key we need is `743639030586`
To emulate the push recievier we use the `fcm-push-listener` crate. Which
is a rust implementation of the good
[RE work of Matthieu Lemoine.](https://medium.com/@MatthieuLemoine/my-journey-to-bring-web-push-support-to-node-and-electron-ce70eea1c0b0)
hats off to you.
We also need to send the PushInfo command to the camera with the token we get
from FCM and some client ID. On IOS the token is the APNS number.
The client ID is an all CAPS UID of hexadecimal of 33 chars. Not sure how
these are generated but they seem to be unique to the device, perhaps a MD5 of
something or some generated UID on app initial login.
To experiment I did a data wipe on the reolink app and found it generates a new UID.
I also found out that if you do this while you have Push Notifications turned on
then you cannot turn them off anymore. To disable the push notifications you need
the UID which I just wiped.... Fortunatly I saved before I wiped it so I could restore.
p.d. If you every wipe your reolink data and need to stop the push notifications
you can try doing manually using the API listed at the end.
This is the data of the `PushInfo` command that is sent to the camera:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<PushInfo version="1.1">
<token>TOKEN_FROM_FCM_OR_APNS</token>
<phoneType>reo_iphone</phoneType>
<clientID>SOMEID</clientID>
</PushInfo>
</body>
```
For ios the phonetype is `reo_iphone` which I observed live but for android
no such luck.
First I just tried `reo_android` but the camera replied with an error code.
Which at least told me that I can test other string quickly and see if it
returns the error code.
Next thing to do was dumping the binaries from the apk and searching for a string
like `reo_`
```bash
grep -Ria "reo_" .
./lib/armeabi-v7a/libavcodec.so:stereo_modeError splitting the input into NAL units.
./lib/arm64-v8a/libavcodec.so:stereo_modeError splitting the input into NAL units.
./smali_classes2/com/google/android/exoplayer2/extractor/mkv/MatroskaExtractor.smali:.field private static final ID_STEREO_MODE:I = 0x53b8
./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_LEFT_RIGHT:I = 0x2
./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_MONO:I = 0x0
./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_STEREO_MESH:I = 0x3
./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_TOP_BOTTOM:I = 0x1
./smali_classes2/com/android/bc/util/DeviceIdUtil$UpdatePushClientIdRequest.smali: const-string v2, "reo_fcm"
./smali_classes2/com/android/bc/pushmanager/MyPushAdapterImpl.smali: const-string v0, "reo_fcm"
./smali/androidx/core/text/BidiFormatter.smali:.field private static final FLAG_STEREO_RESET:I = 0x2
./smali/com/android/bc/component/BaseWebViewFragment.smali: const-string v2, "REO_LANGUAGE="
```
The interesting one was `MyPushAdapterImpl.smali: const-string v0, "reo_fcm"`
which suggested that it is `reo_fcm` stuck that in and voilla
no error messages from the camera.
Now to compile the test app with all this info
wait patiently for a motion event and...
viola!
Reolink was kinda enough to send this to my test app:
```json
{
"data": {
"SRVTIME": "2023-05-19T08:07:03.566Z",
"ALMNAME": "Motion Alert from Cam01",
"sound": "push.wav",
"CHNAME": "Cam0",
"messageType": "alarm",
"DEVNAME": "Cam01",
"pushVersion": "v1.1",
"UID": "REDACTED",
"ALMTYPE": "MD",
"ALMCHN": "1",
"alert": "Motion Alert from Cam01",
"title": "Camera Alert"
},
"from": "743639030586",
"priority": "normal",
"notification": { "title": "Camera Alert", "body": "Motion Alert from Cam01" },
"fcmMessageId": "REDACTED"
}
```
There's no push notifiation for motion stop event but I can wait for this message
then login and use normal motion detection to tell when it stops.
Yay.... now to just add all this to actual neolink so we can save battery.....
---
While experimenting with the ios app, in addition to observing all the
data send to the camera, I also looked at all https calls the app was making
(mitmproxy). This showed a set of APIs that could be useful. Including the api
for deleting a push notification.
---
To disable a PUSH request the following is done:
```bash
curl -v -X DELETE 'https://pushx.reolink.com/devices/<CAMERA_UID>/notifications/listeners/<clientID_FROM_PushInfo>' -H 'Cookie:'
```
Reply is:
```json
{
"status" : {
"detail" : "OK",
"code" : 0
}
}
```
Nothing seems to be sent to the camera just this to the reolink api
---
On ios application start reolink send this:
```bash
curl -v -X GET 'https://pushx.reolink.com/listeners/<clientID_FROM_PushInfo>/devices' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept: */*' -H 'Accept-Language: en-GB,en;q=0.9' -H 'Cookie:'
```
Which resonds with this json which contains a list of all currently
enabled push notification camera UIDs for this client
```json
{
"status" : {
"detail" : "OK",
"code" : 0
},
"data" : [
{
"uid" : "<CAMERA_UID>",
"secret" : ""
}
]
}
```
---
You can also confirm a device has a listener with
```bash
curl -v -X GET 'https://pushx.reolink.com/devices/<CAMERA_UID>/listeners/<clientID_FROM_PushInfo>/config' -H 'Cookie:'
```
Which returns
```json
{
"status": {
"code":0,
"detail":"OK"
},
"data": {
"sound": {
"soundType":0,
"soundName":"",
"soundVolume":0
}
}
}
```
---
There's also a profile one that the app does when a new camera is added
but I forgot to save the details of it.
It reports all the capabilities of the camera, such as it's model
and its resolution etc. Could be useful to test if a UID is valid
quickly
+80
View File
@@ -0,0 +1,80 @@
use lazy_static::lazy_static;
use neolink_core::bc_protocol::{DiscoveryMethods, PrintFormat};
use regex::Regex;
use serde::Deserialize;
use std::clone::Clone;
use validator::{Validate, ValidationError};
use validator_derive::Validate;
lazy_static! {
static ref RE_STREAM_SRC: Regex =
Regex::new(r"^(mainStream|subStream|externStream|both|all)$").unwrap();
static ref RE_TLS_CLIENT_AUTH: Regex = Regex::new(r"^(none|request|require)$").unwrap();
static ref RE_PAUSE_MODE: Regex = Regex::new(r"^(black|still|test|none)$").unwrap();
static ref RE_MAXENC_SRC: Regex =
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap();
}
#[derive(Debug, Deserialize, Validate, Clone)]
pub(crate) struct Config {
#[validate]
pub(crate) cameras: Vec<CameraConfig>,
}
#[derive(Debug, Deserialize, Validate, Clone)]
#[validate(schema(function = "validate_camera_config"))]
pub(crate) struct CameraConfig {
pub(crate) name: String,
#[serde(rename = "address")]
pub(crate) camera_addr: Option<String>,
#[serde(rename = "uid")]
pub(crate) camera_uid: Option<String>,
pub(crate) username: String,
pub(crate) password: Option<String>,
#[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))]
#[serde(default = "default_channel_id")]
pub(crate) channel_id: u8,
#[serde(default = "default_discovery")]
pub(crate) discovery: DiscoveryMethods,
#[serde(default = "default_maxenc")]
#[validate(regex(
path = "RE_MAXENC_SRC",
message = "Invalid maximum encryption method",
code = "max_encryption"
))]
pub(crate) max_encryption: String,
#[serde(default = "default_print", alias = "print")]
pub(crate) print_format: PrintFormat,
}
fn default_print() -> PrintFormat {
PrintFormat::None
}
fn default_discovery() -> DiscoveryMethods {
DiscoveryMethods::Relay
}
fn default_maxenc() -> String {
"Aes".to_string()
}
fn default_channel_id() -> u8 {
0
}
fn validate_camera_config(camera_config: &CameraConfig) -> Result<(), ValidationError> {
match (&camera_config.camera_addr, &camera_config.camera_uid) {
(None, None) => Err(ValidationError::new(
"Either camera address or uid must be given",
)),
_ => Ok(()),
}
}
+82
View File
@@ -0,0 +1,82 @@
use anyhow::{Context, Result};
use clap::Parser;
use fcm_push_listener::*;
use log::*;
use std::{fs, path::PathBuf};
use validator::Validate;
mod config;
mod opt;
mod utils;
use config::Config;
use opt::Opt;
use utils::find_and_connect;
#[tokio::main]
async fn main() -> Result<()> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let opt = Opt::parse();
let conf_path = opt.config.context("Must supply --config file")?;
let config: Config = toml::from_str(
&fs::read_to_string(&conf_path)
.with_context(|| format!("Failed to read {:?}", conf_path))?,
)
.with_context(|| format!("Failed to parse the {:?} config file", conf_path))?;
config
.validate()
.with_context(|| format!("Failed to validate the {:?} config file", conf_path))?;
let camera = find_and_connect(&config, &opt.camera).await?;
// 696841269229 is the reo_iphone FCM Sender_ID
// let registration = fcm_push_listener::register("696841269229").await?;
// 743639030586 is the reo_fcm FCM Sender_ID
// 263684512460 is my test Sender_ID
// let registration = fcm_push_listener::register("743639030586").await?
// I have confirmed that I can recieve test messages with this SenderID
// into this program
// let registration = fcm_push_listener::register("263684512460").await?;
let sender_id = "743639030586"; // andriod
// let sender_id = "696841269229"; // ios
// let sender_id = "263684512460"; // test
let token_path = PathBuf::from("./token.toml");
let registration = if let Ok(Ok(registration)) =
fs::read_to_string(&token_path).map(|v| toml::from_str::<Registration>(&v))
{
info!("Loaded token");
registration
} else {
info!("Registering new token");
let registration = fcm_push_listener::register(sender_id).await?;
let new_token = toml::to_string(&registration)?;
fs::write(token_path, new_token)?;
registration
};
// Send registration.fcm_token to the server to allow it to send push messages to you.
info!("registration.fcm_token: {}", registration.fcm_token);
let uid = "6A5443E486511B0D828543445DC55A7D"; // MD5 Hash of "WHY_REOLINK"
camera
.send_pushinfo_android(&registration.fcm_token, uid)
.await?;
info!("Listening");
let mut listener = FcmPushListener::create(
registration,
|message: FcmMessage| {
info!("Message JSON: {}", message.payload_json);
info!("Persistent ID: {:?}", message.persistent_id);
},
vec![],
);
listener.connect().await?;
Ok(())
}
+16
View File
@@ -0,0 +1,16 @@
use clap::{crate_authors, crate_version, Parser};
use std::path::PathBuf;
use std::str::FromStr;
/// A standards-compliant bridge to Reolink IP cameras
///
/// Neolink is free software released under the GNU AGPL v3.
/// You can find its source code at https://github.com/thirtythreeforty/neolink
#[derive(Parser, Debug)]
#[command(name = "pushnoti", arg_required_else_help = true, version = crate_version!(), author = crate_authors!("\n"))]
pub struct Opt {
#[arg(short, long, value_parser = PathBuf::from_str)]
pub config: Option<PathBuf>,
/// The name of the camera. Must be a name in the config
pub camera: String,
}
+152
View File
@@ -0,0 +1,152 @@
//! Contains code that is not specific to any of the subcommands
//!
use log::*;
use super::config::{CameraConfig, Config};
use anyhow::{anyhow, Context, Error, Result};
use neolink_core::bc_protocol::{
BcCamera, BcCameraOpt, ConnectionProtocol, Credentials, DiscoveryMethods, MaxEncryption,
};
use std::{
fmt::{Display, Error as FmtError, Formatter},
net::{IpAddr, ToSocketAddrs},
str::FromStr,
};
pub(crate) enum AddressOrUid {
Address(String),
Uid(String, DiscoveryMethods),
AddressWithUid(String, String, DiscoveryMethods),
}
impl Display for AddressOrUid {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match self {
AddressOrUid::AddressWithUid(addr, uid, _) => {
write!(f, "Address: {}, UID: {}", addr, uid)
}
AddressOrUid::Address(host) => write!(f, "Address: {}", host),
AddressOrUid::Uid(host, _) => write!(f, "UID: {}", host),
}
}
}
impl AddressOrUid {
// Created by translating the config fields directly
pub(crate) fn new(
address: &Option<String>,
uid: &Option<String>,
method: &DiscoveryMethods,
) -> Result<Self, Error> {
match (address, uid) {
(None, None) => Err(anyhow!("Neither address or uid given")),
(Some(host), Some(uid)) => Ok(AddressOrUid::AddressWithUid(
host.clone(),
uid.clone(),
*method,
)),
(Some(host), None) => Ok(AddressOrUid::Address(host.clone())),
(None, Some(host)) => Ok(AddressOrUid::Uid(host.clone(), *method)),
}
}
// Convience method to get the BcCamera with the appropiate method
// from a camera_config
pub(crate) async fn connect_camera(
&self,
camera_config: &CameraConfig,
) -> Result<BcCamera, Error> {
let (port, addrs) = {
if let Some(addr_str) = camera_config.camera_addr.as_ref() {
match addr_str.to_socket_addrs() {
Ok(addr_iter) => {
let mut port = None;
let mut ipaddrs = vec![];
for addr in addr_iter {
port = Some(addr.port());
ipaddrs.push(addr.ip());
}
Ok((port, ipaddrs))
}
Err(_) => match IpAddr::from_str(addr_str) {
Ok(ip) => Ok((None, vec![ip])),
Err(_) => Err(anyhow!("Could not parse address in config")),
},
}
} else {
Ok((None, vec![]))
}
}?;
let options = BcCameraOpt {
name: camera_config.name.clone(),
channel_id: camera_config.channel_id,
addrs,
port,
uid: camera_config.camera_uid.clone(),
protocol: ConnectionProtocol::TcpUdp,
discovery: camera_config.discovery,
aux_printing: camera_config.print_format,
credentials: Credentials {
username: camera_config.username.clone(),
password: camera_config.password.clone(),
},
};
trace!("Camera Info: {:?}", options);
Ok(BcCamera::new(&options).await?)
}
}
pub(crate) async fn find_and_connect(config: &Config, name: &str) -> Result<BcCamera> {
let camera_config = find_camera_by_name(config, name)?;
connect_and_login(camera_config).await
}
pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result<BcCamera> {
let camera_addr = AddressOrUid::new(
&camera_config.camera_addr,
&camera_config.camera_uid,
&camera_config.discovery,
)
.unwrap();
info!(
"{}: Connecting to camera at {}",
camera_config.name, camera_addr
);
let camera = camera_addr
.connect_camera(camera_config)
.await
.with_context(|| {
format!(
"Failed to connect to camera {} at {} on channel {}",
camera_config.name, camera_addr, camera_config.channel_id
)
})?;
let max_encryption = match camera_config.max_encryption.to_lowercase().as_str() {
"none" => MaxEncryption::None,
"bcencrypt" => MaxEncryption::BcEncrypt,
"aes" => MaxEncryption::Aes,
_ => MaxEncryption::Aes,
};
info!("{}: Logging in", camera_config.name);
camera
.login_with_maxenc(max_encryption)
.await
.with_context(|| format!("Failed to login to {}", camera_config.name))?;
info!("{}: Connected and logged in", camera_config.name);
Ok(camera)
}
pub(crate) fn find_camera_by_name<'a>(config: &'a Config, name: &str) -> Result<&'a CameraConfig> {
config
.cameras
.iter()
.find(|c| c.name == name)
.ok_or_else(|| anyhow!("Camera {} not found in the config file", name))
}
+19 -15
View File
@@ -75,6 +75,9 @@ notice = "warn"
# output a note when they are encountered.
ignore = [
#"RUSTSEC-0000-0000",
# This is rust-xml which is unmaintaned. Not much we can do since
# all the xml serde like crates seem to use it
"RUSTSEC-2022-0048",
]
# Threshold for security vulnerabilities, any vulnerability with a CVSS score
# lower than the range specified will be ignored. Note that ignored advisories
@@ -106,10 +109,11 @@ allow = [
"Apache-2.0",
"Apache-2.0 WITH LLVM-exception",
"BSD-3-Clause",
"AGPL-3.0-only",
"AGPL-3.0-or-later",
"AGPL-3.0",
"Unicode-DFS-2016",
"ISC",
"OpenSSL",
"Zlib",
]
# List of explicitly disallowed licenses
# See https://spdx.org/licenses/ for list of possible licenses
@@ -118,7 +122,7 @@ deny = [
#"Nokia",
]
# Lint level for licenses considered copyleft
copyleft = "warn"
copyleft = "allow"
# Blanket approval or denial for OSI-approved or FSF Free/Libre licenses
# * both - The license will be approved if it is both OSI-approved *AND* FSF
# * either - The license will be approved if it is either OSI-approved *OR* FSF
@@ -142,28 +146,28 @@ exceptions = [
# Each entry is the crate and version constraint, and its specific allow
# list
#{ allow = ["Zlib"], name = "adler32", version = "*" },
{ allow = ["untrusted"], name = "untrusted", version = "*" },
]
# Some crates don't have (easily) machine readable licensing information,
# adding a clarification entry for it allows you to manually specify the
# licensing information
#[[licenses.clarify]]
[[licenses.clarify]]
# The name of the crate the clarification applies to
#name = "ring"
name = "ring"
# The optional version constraint for the crate
#version = "*"
version = "*"
# The SPDX expression for the license requirements of the crate
#expression = "MIT AND ISC AND OpenSSL"
expression = "MIT AND ISC AND OpenSSL"
# One or more files in the crate's source used as the "source of truth" for
# the license expression. If the contents match, the clarification will be used
# when running the license check, otherwise the clarification will be ignored
# and the crate will be checked normally, which may produce warnings or errors
# depending on the rest of your configuration
#license-files = [
license-files = [
# Each entry is a crate relative path, and the (opaque) hash of its contents
#{ path = "LICENSE", hash = 0xbd0eed23 }
#]
{ path = "LICENSE", hash = 0xbd0eed23 }
]
[licenses.private]
# If true, ignores workspace crates that aren't published, or are only
@@ -183,7 +187,7 @@ registries = [
# https://embarkstudios.github.io/cargo-deny/checks/bans/cfg.html
[bans]
# Lint level for when multiple versions of the same crate are detected
multiple-versions = "warn"
multiple-versions = "allow"
# Lint level for when a crate version requirement is `*`
wildcards = "allow"
# The graph highlighting used when creating dotgraphs for crates
@@ -268,8 +272,8 @@ allow-git = []
[sources.allow-org]
# 1 or more github.com organizations to allow git sources for
github = [""]
#github = [""]
# 1 or more gitlab.com organizations to allow git sources for
gitlab = [""]
#gitlab = [""]
# 1 or more bitbucket.org organizations to allow git sources for
bitbucket = [""]
#bitbucket = [""]
+8
View File
@@ -146,11 +146,14 @@ local message_types = {
[106]="<Dst>",
[107]="<Dst> (write)",
[108]="<ConfigFileInfo> (log)",
[109]="<Snap>",
[114]="<Uid>",
[115]="<WifiSignal>",
[116]="<Wifi>",
[120]="<OnlineUserList>",
[122]="<PerformanceInfo>",
[123]="<ReplaySeek>",
[124]="<PushInfo>",
[132]="<VideoInput>", -- <InputAdvanceCfg>
[133]="<RfAlarm>",
[141]="<Email> (test)",
@@ -160,6 +163,7 @@ local message_types = {
[151]="<AbilityInfo>",
[190]="PTZ Preset",
[194]="<Ftp> (test)",
[195]="<AutoUpdate>",
[199]="<Support>",
[208]="<LedState>",
[209]="<LedState> (write)",
@@ -174,9 +178,13 @@ local message_types = {
[228]="<Crop>",
[229]="<Crop> (write)",
[230]="<cropSnap>",
[232]="<AudioTask>",
[234]="UDP Keep Alive",
[252]="<BatteryInfoList>",
[253]="<BatteryInfo>",
[268]="<CloudBindInfo>",
[282]="<CloudLoginKey>",
[287]="<TimeCfg>",
[272]="<findAlarmVideo>",
[273]="<alarmVideoInfo>",
[274]="<findAlarmVideo>",
+315
View File
@@ -1662,6 +1662,64 @@ Message have zero to two payloads.
</body>
```
- 109: `<Snap>`
- Client:
Usual header
- Meta:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<Extension version="1.1">
<channelId>0</channelId>
</Extension>
```
- Main:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<Snap version="1.1">
<channelId>0</channelId>
<logicChannel>0</logicChannel>
<time>0</time>
<fullFrame>0</fullFrame>
<streamType>main</streamType>
</Snap>
</body>
```
- Camera:
**Notes:** XML & Binary reply over mutliple packets:
- Reply 1:
- Main:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<Snap version="1.1">
<channelId>0</channelId>
<fileName>01_20230518140240.jpg</fileName>
<time>0</time>
<pictureSize>23644</pictureSize>
</Snap>
</body>
```
- Reply 2:
- Meta:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<Extension version="1.1">
<binaryData>1</binaryData>
</Extension>
```
- Main:
**Binary data containing the file may be broken over multiple packets**
- 115: `<WifiSignal>`
- Client
@@ -1691,6 +1749,35 @@ Message have zero to two payloads.
</body>
```
- 116: `<Wifi>`
- Client:
Usual header
- Camera:
Payload:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<Wifi version="1.1">
<mode>station</mode>
<authMode>wpa2psk</authMode>
<encryptType>aes</encryptType>
<udidList>
<udid>
<name>WIFINAME</name>
<signal>NUMBER</signal>
<encrypt>ONE_OR_ZERO</encrypt>
</udid>
# Repeats for ALL wifi in range
</udidList>
<ssid>YOUR_CURRENT_WIFI</ssid>
<key>YOUR_CURRENT_WIFI_PASSWORD_UNENCRYPTED</key>
<channel>YOUR_CURRENT_WIFI_CHANNEL</channel>
</Wifi>
</body>
```
- 132: `<VideoInput>`
- Client
@@ -1803,6 +1890,37 @@ Message have zero to two payloads.
</body>
```
- 124: `<PushInfo>`
- Client
- Payload:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<PushInfo version="1.1">
<token>A_PUSH_NOTIFICATION_TOKEN</token>
<phoneType>reo_iphone</phoneType>
<clientID>A_PUSH_NOTIFICATION_CLIENTID</clientID>
</PushInfo>
</body>
```
- Camera
- Payload
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<PushRspInfo version="1.1">
<registerHandle>-1</registerHandle>
<uid>CAMERA_UID</uid>
<uidKey>A_4_CHAR_STRING</uidKey>
</PushRspInfo>
</body>
```
- 133: `<RfAlarm>`
- Client
@@ -2338,6 +2456,154 @@ Message have zero to two payloads.
|--------------|--------------|----------------|-------------------|-------------------|---------------|---------------|
| 0a bc de f0 | 00 00 00 d1 | 00 00 00 00 | 85 00 00 00 | c8 00 | 00 00 | 00 00 00 00 |
- 219: `<PushTask>`
- Client
Usual header
- Meta:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<Extension version="1.1">
<channelId>0</channelId>
</Extension>
```
- Camera
Usual header
- Payload:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<PushTask version="1.1">
<channelId>0</channelId>
<enable>1</enable>
<ScheduleList>
<Schedule>
<alarmType>MD</alarmType>
<timeBlockList>
<timeBlock>
<enable>1</enable>
<weekDay>Sunday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Monday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Tuesday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Wednesday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Thursday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Friday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Saturday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
</timeBlockList>
</Schedule>
</ScheduleList>
</PushTask>
</body>
```
- 232: `<AudioTask>`
- Client
Usual header
- Meta:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<Extension version="1.1">
<channelId>0</channelId>
</Extension>
```
- Camera
Usual header
- Payload:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<AudioTask version="1.1">
<channelId>0</channelId>
<enable>0</enable>
<ScheduleList>
<Schedule>
<alarmType>MD</alarmType>
<timeBlockList>
<timeBlock>
<enable>1</enable>
<weekDay>Sunday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Monday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Tuesday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Wednesday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Thursday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Friday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
<timeBlock>
<enable>1</enable>
<weekDay>Saturday</weekDay>
<beginHour>0</beginHour>
<endHour>23</endHour>
</timeBlock>
</timeBlockList>
</Schedule>
</ScheduleList>
</AudioTask>
</body>
```
- 264: `<audioCfg>` (write)
@@ -2373,6 +2639,55 @@ Message have zero to two payloads.
</body>
```
- 268: `CloudBindInfo`
- Client:
Usual header only packet
- Camera
- Payload
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<CloudBindInfo version="1.1">
<binded>1</binded>
</CloudBindInfo>
</body>
```
- 282: `CloudLoginKey`
- Client:
Usual header only packet
- Camera
- Payload
```xml
<body>
<CloudLoginKey version="1.1">
<enable>0</enable>
</CloudLoginKey>
</body>
```
- 287: `<TimeCfg>`
- Client:
- Payload
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<TimeCfg version="1.1">
<realTime>1684393362</realTime>
</TimeCfg>
</body>
```
- Camera
Header only 200 Ok reply
- 288: `<FloodlightManual>` (write)
- Client
+16
View File
@@ -75,6 +75,22 @@ address = "192.168.1.187:9000"
#
# print_format = "None"
# Internally neolink uses a buffer to improve delivery of frames at their actual times
# A large buffer can work even for large interupts in the connection with the camera
# A small buffer will reduce the latency introduced
# buffer_size: 100
# Over long periods of time the camera buffer can exhaust with no means of recovery
# to combat this a time stretching stratery is employed where the stream is slowed
# as the buffer is exhausted and sped up when full
# this can keep the camera and rtsp streams in sync and reduce buffer exhaustions
#
# HOWEVER if there is real latency issues (such as too high a bitrate)
# then smoothing can result in a stream appearing in slow motion
#
# you can disable and enable this feature here
# use_smoothing: true
[[cameras]]
name = "storage shed"
+6 -9
View File
@@ -1,24 +1,21 @@
use clap::{crate_authors, crate_version, Parser};
use std::path::PathBuf;
use structopt::{clap::AppSettings, StructOpt};
use std::str::FromStr;
/// A standards-compliant bridge to Reolink IP cameras
///
/// Neolink is free software released under the GNU AGPL v3.
/// You can find its source code at https://github.com/thirtythreeforty/neolink
#[derive(StructOpt, Debug)]
#[structopt(
name = "neolink",
setting(AppSettings::ArgRequiredElseHelp),
setting(AppSettings::UnifiedHelpMessage)
)]
#[derive(Parser, Debug)]
#[command(name = "neolink", arg_required_else_help = true, version = crate_version!(), author = crate_authors!("\n"))]
pub struct Opt {
#[structopt(short, long, global(true), parse(from_os_str))]
#[arg(short, long, global = true, value_parser = PathBuf::from_str)]
pub config: Option<PathBuf>,
#[structopt(subcommand)]
pub cmd: Option<Command>,
}
#[derive(StructOpt, Debug)]
#[derive(Parser, Debug)]
pub enum Command {
Rtsp(super::rtsp::Opt),
StatusLight(super::statusled::Opt),
+38
View File
@@ -27,6 +27,9 @@ pub(crate) struct Config {
#[serde(default = "default_bind_port")]
pub(crate) bind_port: u16,
#[serde(default = "default_tokio_console")]
pub(crate) tokio_console: bool,
#[serde(default = "default_certificate")]
pub(crate) certificate: Option<String>,
@@ -96,6 +99,25 @@ pub(crate) struct CameraConfig {
#[serde(default = "default_print", alias = "print")]
pub(crate) print_format: PrintFormat,
#[serde(default = "default_update_time", alias = "time")]
pub(crate) update_time: bool,
#[validate(range(
min = 10,
max = 500,
message = "Invalid buffer size",
code = "buffer_size"
))]
#[serde(default = "default_buffer_size", alias = "size", alias = "buffer")]
pub(crate) buffer_size: usize,
#[serde(
default = "default_smoothing",
alias = "smoothing",
alias = "stretching"
)]
pub(crate) use_smoothing: bool,
}
#[derive(Debug, Deserialize, Validate, Clone)]
@@ -192,10 +214,18 @@ fn default_tls_client_auth() -> String {
"none".to_string()
}
fn default_tokio_console() -> bool {
false
}
fn default_channel_id() -> u8 {
0
}
fn default_update_time() -> bool {
false
}
fn default_motion_timeout() -> f64 {
1.
}
@@ -225,6 +255,14 @@ fn default_pause() -> PauseConfig {
}
}
fn default_smoothing() -> bool {
true
}
fn default_buffer_size() -> usize {
100
}
pub(crate) static RESERVED_NAMES: &[&str] = &["anyone", "anonymous"];
fn validate_username(name: &str) -> Result<(), ValidationError> {
if name.trim().is_empty() {
+4 -3
View File
@@ -1,12 +1,13 @@
use clap::Parser;
use std::path::PathBuf;
use structopt::StructOpt;
use std::str::FromStr;
/// The image command will dump a still image from the camera
#[derive(StructOpt, Debug)]
#[derive(Parser, Debug)]
pub struct Opt {
/// The name of the camera to get the image from. Must be a name in the config
pub camera: String,
/// The path of the output.
#[structopt(short, long, parse(from_os_str))]
#[structopt(short, long, value_parser = PathBuf::from_str)]
pub file_path: PathBuf,
}
+1
View File
@@ -65,6 +65,7 @@ async fn output(pipeline: Pipeline) -> Result<GstSender> {
let mut set = JoinSet::new();
set.spawn(async move {
while let Some(control) = reciever.recv().await {
tokio::task::yield_now().await;
match control {
GstControl::Data(buf) => {
let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap();
+18 -2
View File
@@ -23,10 +23,10 @@
//! Neolink source code is available online at <https://github.com/thirtythreeforty/neolink>
//!
use anyhow::{Context, Result};
use clap::Parser;
use env_logger::Env;
use log::*;
use std::fs;
use structopt::StructOpt;
use validator::Validate;
mod cmdline;
@@ -42,6 +42,18 @@ mod utils;
use cmdline::{Command, Opt};
use config::Config;
use console_subscriber as _;
#[cfg(tokio_unstable)]
fn tokio_console_enable() {
info!("Tokio Console Enabled");
console_subscriber::init();
}
#[cfg(not(tokio_unstable))]
fn tokio_console_enable() {
debug!("Tokio Console Disabled");
}
#[tokio::main]
async fn main() -> Result<()> {
@@ -53,7 +65,7 @@ async fn main() -> Result<()> {
env!("NEOLINK_PROFILE")
);
let opt = Opt::from_args();
let opt = Opt::parse();
let conf_path = opt.config.context("Must supply --config file")?;
let config: Config = toml::from_str(
@@ -66,6 +78,10 @@ async fn main() -> Result<()> {
.validate()
.with_context(|| format!("Failed to validate the {:?} config file", conf_path))?;
if config.tokio_console {
tokio_console_enable();
}
match opt.cmd {
None => {
warn!(
+2 -2
View File
@@ -1,4 +1,4 @@
use structopt::StructOpt;
use clap::Parser;
#[derive(StructOpt, Debug)]
#[derive(Parser, Debug)]
pub struct Opt {}
+328 -288
View File
@@ -1,20 +1,21 @@
use super::App;
use crate::config::CameraConfig;
use crate::utils::AddressOrUid;
use anyhow::{anyhow, Context, Result};
use log::*;
use neolink_core::bc_protocol::{
BcCamera, Direction as BcDirection, Error as BcError, LightState, MotionStatus,
BcCamera, Direction as BcDirection, Error as BcError, LightState, MaxEncryption, MotionStatus,
};
use std::sync::Arc;
use tokio::sync::{
mpsc::{channel, error::TryRecvError, Receiver, Sender},
Mutex,
use tokio::{
sync::mpsc::{channel, Receiver, Sender},
task::JoinSet,
time::sleep,
};
use core::time::Duration;
#[derive(Debug, Copy, Clone)]
pub(crate) enum Messages {
None,
Login,
MotionStop,
MotionStart,
@@ -33,12 +34,12 @@ pub(crate) enum Messages {
#[derive(Debug, Copy, Clone)]
pub(crate) enum Direction {
Up(f32),
Down(f32),
Left(f32),
Right(f32),
In(f32),
Out(f32),
Up(f32, f32),
Down(f32, f32),
Left(f32, f32),
Right(f32, f32),
In(f32, f32),
Out(f32, f32),
}
#[derive(Debug)]
@@ -50,102 +51,114 @@ enum ToCamera {
},
}
pub(crate) struct EventCam {
config: Arc<CameraConfig>,
app: Arc<App>,
channel_out: Arc<Mutex<Option<Receiver<Messages>>>>,
channel_in: Arc<Mutex<Option<Sender<ToCamera>>>>,
#[derive(Clone)]
pub(crate) struct EventCamSender {
channel_in: Sender<ToCamera>,
}
impl EventCam {
pub(crate) fn new(config: Arc<CameraConfig>, app: Arc<App>) -> Self {
Self {
config,
app,
channel_out: Arc::new(Mutex::new(None)),
channel_in: Arc::new(Mutex::new(None)),
}
}
pub(crate) async fn poll(&self) -> Result<Messages> {
if let Ok(ref mut channel_out) = self.channel_out.try_lock() {
if let Some(channel_out) = channel_out.as_mut() {
channel_out.recv().await.context("Camera failed to poll")
} else {
Ok(Messages::None)
}
} else {
Ok(Messages::None)
}
}
impl EventCamSender {
#[allow(dead_code)]
pub(crate) async fn send_message(&self, msg: Messages) -> Result<()> {
let mut channel_in = self.channel_in.lock().await;
if let Some(channel_in) = channel_in.as_mut() {
channel_in
.send(ToCamera::Send(msg))
.await
.context("Failed to send message from camera")
} else {
Err(anyhow!("Failed to send camera data over channel"))
}
self.channel_in
.send(ToCamera::Send(msg))
.await
.context("Failed to send message from camera")
}
pub(crate) async fn send_message_with_reply(&self, msg: Messages) -> Result<String> {
let mut channel_in = self.channel_in.lock().await;
if let Some(channel_in) = channel_in.as_mut() {
let (tx, mut rx) = channel(1);
channel_in
.send(ToCamera::SendAndReply {
message: msg,
reply: tx,
})
.await
.context("Failed to send message from camera")?;
tokio::time::timeout(std::time::Duration::from_secs(3), rx.recv())
.await
.context("Failed to recieve reply Timeout")?
.ok_or_else(|| anyhow!("Failed to recieve reply"))
} else {
Err(anyhow!("Failed to send camera data over crossbeam"))
}
let (tx, mut rx) = channel(1);
self.channel_in
.send(ToCamera::SendAndReply {
message: msg,
reply: tx,
})
.await
.context("Failed to send message from camera")?;
tokio::time::timeout(std::time::Duration::from_secs(3), rx.recv())
.await
.context("Failed to recieve reply Timeout")?
.ok_or_else(|| anyhow!("Failed to recieve reply"))
}
}
pub(crate) struct EventCam {
sender: EventCamSender,
channel_out: Receiver<Messages>,
set: JoinSet<Result<()>>,
}
pub(crate) fn abort(&self) {
self.app.abort(&self.config.name);
}
pub(crate) async fn start_listening(&self) {
let loop_config = self.config.clone();
impl EventCam {
pub(crate) async fn new(config: Arc<CameraConfig>) -> Self {
// Channels from the camera
let (tx, rx) = channel(40);
let loop_tx = Arc::new(Mutex::new(tx));
*self.channel_out.lock().await = Some(rx);
// Channels to the camera
let (stx, srx) = channel(40);
let loop_rx = Arc::new(Mutex::new(srx));
*self.channel_in.lock().await = Some(stx);
while self.app.running(&self.config.name) {
// Ignore errors and just loop
let _ = Self::cam_run(
&loop_config,
loop_tx.clone(),
loop_rx.clone(),
self.app.clone(),
)
.await;
let mut set = JoinSet::<Result<()>>::new();
let thread_config = config;
let mut eventcam_thread = EventCamThread { rx: srx, tx };
set.spawn(async move {
let mut wait_for = tokio::time::Duration::from_micros(125);
loop {
// Ignore errors and just loop
tokio::task::yield_now().await;
if let Err(e) = eventcam_thread.cam_run(&thread_config).await {
warn!("Camera thread error: {:?}. Restarting...", e);
tokio::time::sleep(wait_for).await;
wait_for *= 2;
}
}
});
Self {
sender: EventCamSender { channel_in: stx },
channel_out: rx,
set,
}
}
async fn cam_run(
camera_config: &CameraConfig,
tx: Arc<Mutex<Sender<Messages>>>,
rx: Arc<Mutex<Receiver<ToCamera>>>,
app: Arc<App>,
) -> Result<()> {
/// This will also error is the join set errors
pub(crate) async fn poll(&mut self) -> Result<Messages> {
let (incoming, set) = (&mut self.channel_out, &mut self.set);
tokio::select! {
v = incoming.recv() => v.with_context(|| "Camera Polling error"),
v = async {
while let Some(res) = set.join_next().await {
match res {
Err(e) => {
set.abort_all();
return Err(e.into());
}
Ok(Err(e)) => {
set.abort_all();
return Err(e);
}
Ok(Ok(())) => {}
}
}
Err(anyhow!("Camera background thread dropped without error"))
} => v.with_context(|| "Camera Threads aborted"),
}
}
pub(crate) fn get_sender(&self) -> EventCamSender {
self.sender.clone()
}
}
impl Drop for EventCam {
fn drop(&mut self) {
self.set.abort_all();
}
}
struct EventCamThread {
rx: Receiver<ToCamera>,
tx: Sender<Messages>,
}
impl EventCamThread {
async fn cam_run(&mut self, camera_config: &CameraConfig) -> Result<()> {
let camera_addr = AddressOrUid::new(
&camera_config.camera_addr,
&camera_config.camera_uid,
@@ -168,47 +181,58 @@ impl EventCam {
})?;
info!("{}: Logging in", camera_config.name);
let max_encryption = match camera_config.max_encryption.to_lowercase().as_str() {
"none" => MaxEncryption::None,
"bcencrypt" => MaxEncryption::BcEncrypt,
"aes" => MaxEncryption::Aes,
_ => MaxEncryption::Aes,
};
camera
.login()
.login_with_maxenc(max_encryption)
.await
.context("Failed to login to the camera")?;
info!("{}: Connected and logged in", camera_config.name);
(tx.lock().await).send(Messages::Login).await?;
self.tx.send(Messages::Login).await?;
// Shararble cameras
let arc_cam = Arc::new(camera);
let mut motion_thread = MotionThread {
app: app.clone(),
tx,
tx: self.tx.clone(),
camera: arc_cam.clone(),
name: camera_config.name.to_string(),
};
let mut message_handler = MessageHandler {
app: app.clone(),
rx,
rx: &mut self.rx,
camera: arc_cam,
name: camera_config.name.to_string(),
};
let camera_name = camera_config.name.clone();
info!("{}: Listening to Camera Motion", camera_config.name);
let handle_motion = tokio::task::spawn(async move {
if let Err(e) = motion_thread.run().await {
error!("Motion thread aborted: {:?}", e);
}
app.abort(&camera_name);
});
info!("{}: Setting up camera actions", camera_config.name);
let handle_camera = tokio::task::spawn(async move {
message_handler.listen().await;
});
tokio::select! {
val = async {handle_motion.await} => {val},
val = async {handle_camera.await} => {val}
val = async {
info!("{}: Listening to Camera Motion", camera_config.name);
motion_thread.run().await
} => {
if let Err(e) = val {
error!("Motion thread aborted: {:?}", e);
Err(e)
} else {
debug!("Normal finish on motion thread");
Ok(())
}
},
val = async {
info!("{}: Setting up camera actions", camera_config.name);
message_handler.listen().await
} => {
if let Err(e) = val {
error!("Camera message handler aborted: {:?}", e);
Err(e)
} else {
debug!("Normal finish on camera message thread");
Ok(())
}
},
}?;
Ok(())
@@ -216,26 +240,26 @@ impl EventCam {
}
struct MotionThread {
app: Arc<App>,
tx: Arc<Mutex<Sender<Messages>>>,
tx: Sender<Messages>,
camera: Arc<BcCamera>,
name: String,
}
impl MotionThread {
async fn run(&mut self) -> Result<()> {
let mut motion_data = self.camera.listen_on_motion().await?;
while self.app.running(&format!("app:{}", self.name)) {
for motion_status in motion_data.consume_motion_events()?.drain(..) {
let mut queued_motion = motion_data.consume_motion_events()?;
loop {
tokio::task::yield_now().await;
for motion_status in queued_motion.drain(..) {
match motion_status {
MotionStatus::Start(_) => {
(self.tx.lock().await)
self.tx
.send(Messages::MotionStart)
.await
.map_err(|_| BcError::Other("Failed to send Message"))?;
}
MotionStatus::Stop(_) => {
(self.tx.lock().await)
self.tx
.send(Messages::MotionStop)
.await
.map_err(|_| BcError::Other("Failed to send Message"))?;
@@ -243,185 +267,201 @@ impl MotionThread {
_ => {}
}
}
queued_motion.push(motion_data.next_motion().await?);
}
Ok(())
}
}
struct MessageHandler {
app: Arc<App>,
rx: Arc<Mutex<Receiver<ToCamera>>>,
struct MessageHandler<'a> {
rx: &'a mut Receiver<ToCamera>,
camera: Arc<BcCamera>,
name: String,
}
impl MessageHandler {
async fn listen(&mut self) {
while self.app.running(&format!("app:{}", self.name)) {
// Try and lock don't worry if not
if let Ok(ref mut channel_out) = self.rx.try_lock() {
match channel_out.try_recv() {
Ok(to_camera) => {
let (replier, message) = match to_camera {
ToCamera::Send(message) => (None, message),
ToCamera::SendAndReply { message, reply } => (Some(reply), message),
};
let reply = match message {
Messages::Reboot => {
if self.camera.reboot().await.is_err() {
error!("Failed to reboot the camera");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
impl<'a> MessageHandler<'a> {
async fn listen(&mut self) -> Result<()> {
loop {
tokio::task::yield_now().await;
match self.rx.recv().await {
Some(to_camera) => {
let (replier, message) = match to_camera {
ToCamera::Send(message) => (None, message),
ToCamera::SendAndReply { message, reply } => (Some(reply), message),
};
let mut error = None;
let reply = match message {
Messages::Reboot => {
if let Err(e) = self.camera.reboot().await {
error = Some(format!("Failed to reboot the camera: {:?}", e));
"FAIL".to_string()
} else {
"OK".to_string()
}
Messages::StatusLedOn => {
if self.camera.led_light_set(true).await.is_err() {
error!("Failed to turn on the status light");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::StatusLedOff => {
if self.camera.led_light_set(false).await.is_err() {
error!("Failed to turn off the status light");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::IRLedOn => {
if self.camera.irled_light_set(LightState::On).await.is_err() {
error!("Failed to turn on the status light");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::IRLedOff => {
if self.camera.irled_light_set(LightState::Off).await.is_err() {
error!("Failed to turn on the status light");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::IRLedAuto => {
if self.camera.irled_light_set(LightState::Auto).await.is_err() {
error!("Failed to turn on the status light");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::Battery => match self.camera.battery_info().await {
Err(_) => {
error!("Failed to get battery status");
"FAIL".to_string()
}
Ok(battery_info) => {
let bytes_res = yaserde::ser::serialize_with_writer(
&battery_info,
vec![],
&Default::default(),
);
match bytes_res {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => str,
Err(_) => {
error!("Failed to encode battery status");
"FAIL".to_string()
}
},
Err(_) => {
error!("Failed to serialise battery status");
"FAIL".to_string()
}
}
}
},
Messages::PIRQuery => match self.camera.get_pirstate().await {
Err(_) => {
error!("Failed to get pir status");
"FAIL".to_string()
}
Ok(pir_info) => {
let bytes_res = yaserde::ser::serialize_with_writer(
&pir_info,
vec![],
&Default::default(),
);
match bytes_res {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => str,
Err(_) => {
error!("Failed to encode pir status");
"FAIL".to_string()
}
},
Err(_) => {
error!("Failed to serialise pir status");
"FAIL".to_string()
}
}
}
},
Messages::PIROn => {
if self.camera.pir_set(true).await.is_err() {
error!("Failed to turn on the pir");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::PIROff => {
if self.camera.pir_set(false).await.is_err() {
error!("Failed to turn on the pir");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::Ptz(direction) => {
let (bc_direction, amount) = match direction {
Direction::Up(amount) => (BcDirection::Up, amount),
Direction::Down(amount) => (BcDirection::Down, amount),
Direction::Left(amount) => (BcDirection::Left, amount),
Direction::Right(amount) => (BcDirection::Right, amount),
Direction::In(amount) => (BcDirection::In, amount),
Direction::Out(amount) => (BcDirection::Out, amount),
};
if self.camera.send_ptz(bc_direction, amount).await.is_err() {
error!("Failed to turn on the status light");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
_ => "UNKNOWN COMMAND".to_string(),
};
if let Some(replier) = replier {
let _ = replier.send(reply).await;
}
Messages::StatusLedOn => {
if let Err(e) = self.camera.led_light_set(true).await {
error =
Some(format!("Failed to turn on the status light: {:?}", e));
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::StatusLedOff => {
if let Err(e) = self.camera.led_light_set(false).await {
error =
Some(format!("Failed to turn off the status light: {:?}", e));
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::IRLedOn => {
if let Err(e) = self.camera.irled_light_set(LightState::On).await {
error =
Some(format!("Failed to turn on the status light: {:?}", e));
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::IRLedOff => {
if let Err(e) = self.camera.irled_light_set(LightState::Off).await {
error =
Some(format!("Failed to turn on the status light: {:?}", e));
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::IRLedAuto => {
if let Err(e) = self.camera.irled_light_set(LightState::Auto).await {
error =
Some(format!("Failed to turn on the status light: {:?}", e));
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::Battery => match self.camera.battery_info().await {
Err(e) => {
error!("Failed to get battery status: {:?}", e);
"FAIL".to_string()
}
Ok(battery_info) => {
let bytes_res = yaserde::ser::serialize_with_writer(
&battery_info,
vec![],
&Default::default(),
);
match bytes_res {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => str,
Err(_) => {
error!("Failed to encode battery status");
"FAIL".to_string()
}
},
Err(_) => {
error!("Failed to serialise battery status");
"FAIL".to_string()
}
}
}
},
Messages::PIRQuery => match self.camera.get_pirstate().await {
Err(e) => {
error!("Failed to get pir status: {:?}", e);
"FAIL".to_string()
}
Ok(pir_info) => {
let bytes_res = yaserde::ser::serialize_with_writer(
&pir_info,
vec![],
&Default::default(),
);
match bytes_res {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(str) => str,
Err(_) => {
error!("Failed to encode pir status");
"FAIL".to_string()
}
},
Err(_) => {
error!("Failed to serialise pir status");
"FAIL".to_string()
}
}
}
},
Messages::PIROn => {
if let Err(e) = self.camera.pir_set(true).await {
error = Some(format!("Failed to turn on the pir: {:?}", e));
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::PIROff => {
if let Err(e) = self.camera.pir_set(false).await {
error = Some(format!("Failed to turn on the pir: {:?}", e));
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::Ptz(direction) => {
let (bc_direction, amount, seconds) = match direction {
Direction::Up(amount, seconds) => {
(BcDirection::Up, amount, seconds)
}
Direction::Down(amount, seconds) => {
(BcDirection::Down, amount, seconds)
}
Direction::Left(amount, seconds) => {
(BcDirection::Left, amount, seconds)
}
Direction::Right(amount, seconds) => {
(BcDirection::Right, amount, seconds)
}
Direction::In(amount, seconds) => {
(BcDirection::In, amount, seconds)
}
Direction::Out(amount, seconds) => {
(BcDirection::Out, amount, seconds)
}
};
if let Err(e) = self.camera.send_ptz(bc_direction, amount).await {
error = Some(format!("Failed to send PTZ: {:?}", e));
"FAIL".to_string()
} else {
// sleep for the designated seconds
sleep(Duration::from_secs_f32(seconds)).await;
// note that amount is not used in the stop command
if let Err(e) =
self.camera.send_ptz(BcDirection::Stop, amount).await
{
error = Some(format!("Failed to send PTZ: {:?}", e));
"FAIL".to_string()
} else {
"OK".to_string()
}
}
}
_ => "UNKNOWN COMMAND".to_string(),
};
if let Some(replier) = replier {
let _ = replier.send(reply).await;
}
Err(TryRecvError::Empty) => (),
Err(TryRecvError::Disconnected) => self.abort(),
if let Some(error) = error {
return Err(anyhow!("{}", error));
}
}
None => {
return Err(anyhow!("Message handller channel dropped"));
}
}
}
}
fn abort(&self) {
self.app.abort(&self.name);
}
}
+279 -197
View File
@@ -1,4 +1,3 @@
use log::*;
///
/// # Neolink MQTT
///
@@ -54,27 +53,29 @@ use log::*;
// `credentials` are the username and password required to identify with the mqtt server
//
use std::sync::Arc;
use tokio::time::{sleep, Duration};
mod app;
mod cmdline;
mod event_cam;
mod mqttc;
use crate::config::{CameraConfig, Config, MqttConfig};
use anyhow::{anyhow, Result};
pub(crate) use app::App;
use anyhow::{anyhow, Context, Error, Result};
pub(crate) use cmdline::Opt;
use event_cam::EventCam;
pub(crate) use event_cam::{Direction, Messages};
use log::*;
use mqttc::{Mqtt, MqttReplyRef};
use self::{
event_cam::EventCamSender,
mqttc::{MqttReply, MqttSender},
};
/// Entry point for the mqtt subcommand
///
/// Opt is the command line options
pub(crate) async fn main(_: Opt, config: Config) -> Result<()> {
let app = App::new();
let arc_app = Arc::new(app);
if config.cameras.iter().all(|config| config.mqtt.is_none()) {
return Err(anyhow!(
"MQTT command run, but no cameras configured with MQTT settings. Exiting."
@@ -89,219 +90,300 @@ pub(crate) async fn main(_: Opt, config: Config) -> Result<()> {
.collect::<Vec<_>>()
{
if let Some(mqtt_config) = camera_config.mqtt.as_ref().map(|a| Arc::new(a.clone())) {
let loop_arc_app = arc_app.clone();
info!("{}: Setting up mqtt", camera_config.name);
set.spawn(async move {
while loop_arc_app.running("app") {
let _ =
listen_on_camera(camera_config.clone(), &mqtt_config, loop_arc_app.clone())
.await;
let mut wait_for = Duration::from_micros(125);
loop {
tokio::task::yield_now().await;
if let Err(e) = listen_on_camera(camera_config.clone(), &mqtt_config).await {
warn!("Error: {:?}. Retrying", e);
}
sleep(wait_for).await;
wait_for *= 2;
}
});
}
}
if let Some(result) = set.join_next().await {
while let Some(result) = set.join_next().await {
result?;
}
Ok(())
}
async fn listen_on_camera(
cam_config: Arc<CameraConfig>,
mqtt_config: &MqttConfig,
arc_app: Arc<App>,
) -> Result<()> {
async fn listen_on_camera(cam_config: Arc<CameraConfig>, mqtt_config: &MqttConfig) -> Result<()> {
// Camera thread
let arc_event_cam = Arc::new(EventCam::new(cam_config.clone(), arc_app.clone()));
let arc_mqtt = Mqtt::new(mqtt_config, &cam_config.name, arc_app.clone());
let mut set = tokio::task::JoinSet::new();
// Start listening to camera events
let event_cam = arc_event_cam.clone();
set.spawn(async move {
event_cam.start_listening().await; // Loop forever
event_cam.abort(); // Just to ensure everything aborts
});
let mut event_cam = EventCam::new(cam_config.clone()).await;
let mut mqtt = Mqtt::new(mqtt_config, &cam_config.name).await;
// Start listening to mqtt events
let event_cam = arc_event_cam.clone();
let mqtt = arc_mqtt.clone();
set.spawn(async move {
let _ = mqtt.start().is_err();
event_cam.abort();
});
// Listen on camera messages and post on mqtt
let camera_name = cam_config.name.clone();
let event_cam = arc_event_cam.clone();
let mqtt = arc_mqtt.clone();
let app = arc_app.clone();
set.spawn(async move {
while app.running(&format!("app: {}", camera_name)) {
if let Ok(msg) = event_cam.poll().await {
match msg {
Messages::Login => {
if mqtt.send_message("status", "connected", true).is_err() {
error!("Failed to post connect over MQTT for {}", camera_name);
}
}
Messages::MotionStop => {
if mqtt.send_message("status/motion", "off", true).is_err() {
error!("Failed to publish motion stop for {}", camera_name);
}
}
Messages::MotionStart => {
if mqtt.send_message("status/motion", "on", true).is_err() {
error!("Failed to publish motion start for {}", camera_name);
}
}
_ => {}
}
}
}
});
let mqtt_sender_cam = mqtt.get_sender();
let mqtt_sender_mqtt = mqtt.get_sender();
let event_cam_sender = event_cam.get_sender();
// Listen on mqtt messages and post on camera
let event_cam = arc_event_cam.clone();
let mqtt = arc_mqtt.clone();
let app = arc_app.clone();
let camera_name = cam_config.name.clone();
set.spawn(async move {
while app.running(&format!("app: {}", camera_name)) {
if let Ok(msg) = mqtt.poll() {
match msg.as_ref() {
MqttReplyRef {
topic: "control/led",
message: "on",
} => {
if event_cam.send_message(Messages::StatusLedOn).await.is_err() {
error!("Failed to set camera status light on");
// When one error is recieved we pass it up async
let (error_send, mut error_recv) = tokio::sync::mpsc::channel::<Error>(1);
let mqtt_to_cam = async {
// Select to wait on an error (via the channel) or normal poll ops
tokio::select! {
v = async {
// Normal poll operations
while let Ok(msg) = mqtt.poll().await {
tokio::task::yield_now().await;
// Put the reply on it's own async thread so we can safely sleep
// and wait for it to reply in it's own time
let event_cam_sender = event_cam_sender.clone();
let mqtt_sender_mqtt = mqtt_sender_mqtt.clone();
let error_send = error_send.clone();
tokio::task::spawn(async move {
// Handle the message and wait for ok/error on this thread
let result: Result<()> = handle_mqtt_message(&msg, event_cam_sender, mqtt_sender_mqtt).await;
// If there is an error we pass it to the channel
// this allows for async error handelling
if let Err(e) = result {
let _ = error_send.try_send(e);
}
}
MqttReplyRef {
topic: "control/led",
message: "off",
} => {
if event_cam
.send_message(Messages::StatusLedOff)
.await
.is_err()
{
error!("Failed to set camera status light off");
}
}
MqttReplyRef {
topic: "control/ir",
message: "on",
} => {
if event_cam.send_message(Messages::IRLedOn).await.is_err() {
error!("Failed to set camera status light on");
}
}
MqttReplyRef {
topic: "control/ir",
message: "off",
} => {
if event_cam.send_message(Messages::IRLedOff).await.is_err() {
error!("Failed to set camera status light off");
}
}
MqttReplyRef {
topic: "control/ir",
message: "auto",
} => {
if event_cam.send_message(Messages::IRLedAuto).await.is_err() {
error!("Failed to set camera status light off");
}
}
MqttReplyRef {
topic: "control/reboot",
..
} => {
if event_cam.send_message(Messages::Reboot).await.is_err() {
error!("Failed to set camera status light off");
}
}
MqttReplyRef {
topic: "control/ptz",
message,
} => {
let lowercase_message = message.to_lowercase();
let mut words = lowercase_message.split_whitespace();
if let Some(direction_txt) = words.next() {
let amount = words.next().unwrap_or("32.0");
if let Ok(amount) = amount.parse::<f32>() {
let direction = match direction_txt {
"up" => Direction::Up(amount),
"down" => Direction::Down(amount),
"left" => Direction::Left(amount),
"right" => Direction::Right(amount),
"in" => Direction::In(amount),
"out" => Direction::Out(amount),
_ => {
error!("Unrecongnized PTZ direction");
continue;
}
};
if event_cam
.send_message(Messages::Ptz(direction))
.await
.is_err()
{
error!("Failed to send PTZ");
}
} else {
error!("No PTZ direction speed was not a valid number");
}
} else {
error!("No PTZ Direction given. Please add up/down/left/right/in/out");
}
}
MqttReplyRef {
topic: "control/pir",
message: "on",
} => {
if event_cam.send_message(Messages::PIROn).await.is_err() {
error!("Failed to set pir on");
}
}
MqttReplyRef {
topic: "control/pir",
message: "off",
} => {
if event_cam.send_message(Messages::PIROff).await.is_err() {
error!("Failed to set pir off");
}
}
MqttReplyRef {
topic: "query/battery",
..
} => match event_cam.send_message_with_reply(Messages::Battery).await {
Ok(reply) => {
if mqtt.send_message("status/battery", &reply, false).is_err() {
error!("Failed to send battery status reply");
}
}
Err(_) => error!("Failed to get battery status"),
},
MqttReplyRef {
topic: "query/pir", ..
} => match event_cam.send_message_with_reply(Messages::PIRQuery).await {
Ok(reply) => {
if mqtt.send_message("status/pir", &reply, false).is_err() {
error!("Failed to send pir status reply");
}
}
Err(_) => error!("Failed to get pir status"),
},
_ => {}
});
}
Ok(())
} => v,
// Wait on any error from any of the error channels and if we get it we abort
v = error_recv.recv() => v.map(Err).unwrap_or_else(|| Err(anyhow!("Listen on camera error channel closed"))),
}
};
let cam_to_mqtt = async {
loop {
tokio::task::yield_now().await;
match event_cam.poll().await? {
Messages::Login => {
mqtt_sender_cam
.send_message("status", "connected", true)
.await
.with_context(|| {
format!("Failed to post connect over MQTT for {}", camera_name)
})?;
}
Messages::MotionStop => {
mqtt_sender_cam
.send_message("status/motion", "off", true)
.await
.with_context(|| {
format!("Failed to publish motion stop for {}", camera_name)
})?;
}
Messages::MotionStart => {
mqtt_sender_cam
.send_message("status/motion", "on", true)
.await
.with_context(|| {
format!("Failed to publish motion start for {}", camera_name)
})?;
}
_ => {}
}
}
});
};
while set.join_next().await.is_some() {}
tokio::select! {
v = mqtt_to_cam => {v},
v = cam_to_mqtt => {v},
}?;
Ok(())
}
async fn handle_mqtt_message(
msg: &MqttReply,
event_cam_sender: EventCamSender,
mqtt_sender_mqtt: MqttSender,
) -> Result<()> {
let mut reply = None;
let mut reply_topic = None;
match msg.as_ref() {
MqttReplyRef {
topic: _,
message: "OK",
}
| MqttReplyRef {
topic: _,
message: "FAIL",
} => {
// Do nothing for the success/fail replies
}
MqttReplyRef {
topic: "control/led",
message: "on",
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::StatusLedOn)
.await
.with_context(|| "Failed to set camera status light on")?,
);
}
MqttReplyRef {
topic: "control/led",
message: "off",
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::StatusLedOff)
.await
.with_context(|| "Failed to set camera status light off")?,
);
}
MqttReplyRef {
topic: "control/ir",
message: "on",
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::IRLedOn)
.await
.with_context(|| "Failed to set camera status light on")?,
);
}
MqttReplyRef {
topic: "control/ir",
message: "off",
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::IRLedOff)
.await
.with_context(|| "Failed to set camera status light off")?,
);
}
MqttReplyRef {
topic: "control/ir",
message: "auto",
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::IRLedAuto)
.await
.with_context(|| "Failed to set camera status light off")?,
);
}
MqttReplyRef {
topic: "control/reboot",
..
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::Reboot)
.await
.with_context(|| "Failed to set camera status light off")?,
);
}
MqttReplyRef {
topic: "control/ptz",
message,
} => {
let lowercase_message = message.to_lowercase();
let mut words = lowercase_message.split_whitespace();
if let Some(direction_txt) = words.next() {
// Target amount to move
let amount = words.next().unwrap_or("32.0");
if let Ok(amount) = amount.parse::<f32>() {
let seconds = amount / 32f32;
// range checking on seconds so that you can't sleep for 3.4E+38 seconds
match seconds {
x if (0.0..10.0).contains(&x) => seconds,
_ => {
error!("seconds was not a valid number (out of range)");
return Ok(());
}
};
let direction = match direction_txt {
"up" => Direction::Up(amount, seconds),
"down" => Direction::Down(amount, seconds),
"left" => Direction::Left(amount, seconds),
"right" => Direction::Right(amount, seconds),
"in" => Direction::In(amount, seconds),
"out" => Direction::Out(amount, seconds),
_ => {
error!("Unrecognized PTZ direction \"{}\"", direction_txt);
return Ok(());
}
};
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::Ptz(direction))
.await
.with_context(|| "Failed to send PTZ")?,
);
} else {
error!("No PTZ direction speed was not a valid number");
}
} else {
error!("No PTZ Direction given. Please add up/down/left/right/in/out");
}
}
MqttReplyRef {
topic: "control/pir",
message: "on",
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::PIROn)
.await
.with_context(|| "Failed to set pir on")?,
);
}
MqttReplyRef {
topic: "control/pir",
message: "off",
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::PIROff)
.await
.with_context(|| "Failed to set pir off")?,
);
}
MqttReplyRef {
topic: "query/battery",
..
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::Battery)
.await
.with_context(|| "Failed to get battery status")?,
);
reply_topic = Some("status/battery");
}
MqttReplyRef {
topic: "query/pir", ..
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::PIRQuery)
.await
.with_context(|| "Failed to get pir status")?,
);
reply_topic = Some("status/pir");
}
_ => {}
}
if let Some(reply) = reply {
if let Some(topic) = reply_topic {
mqtt_sender_mqtt
.send_message(topic, &reply, false)
.await
.with_context(|| "Failed to send Camera reply to Mqtt")?;
} else {
mqtt_sender_mqtt
.send_message(&msg.topic, &reply, false)
.await
.with_context(|| "Failed to send Camera reply to Mqtt")?;
}
}
Ok(())
}
+172 -146
View File
@@ -1,22 +1,21 @@
use super::App;
use crate::config::MqttConfig;
use anyhow::{Context, Result};
use crossbeam_channel::{unbounded, Receiver, RecvError, Sender};
use anyhow::{anyhow, Context, Result};
use log::*;
use rumqttc::{
Client, ClientError, ConnectReturnCode, Connection, Event, Incoming, Key, LastWill,
AsyncClient, ClientError, ConnectReturnCode, Event, EventLoop, Incoming, Key, LastWill,
MqttOptions, Publish, QoS, TlsConfiguration, Transport,
};
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc::{channel, Receiver, Sender};
use tokio::task::JoinSet;
pub(crate) struct Mqtt {
app: Arc<App>,
client: Mutex<Client>,
connection: Mutex<Connection>,
client: Arc<AsyncClient>,
name: String,
incoming: (Sender<Publish>, Receiver<Publish>),
msg_channel: (Sender<MqttReply>, Receiver<MqttReply>),
drop_message: Mutex<Option<MqttReply>>,
incoming: Receiver<MqttReply>,
set: JoinSet<Result<()>>,
drop_message: Option<MqttReply>,
}
pub(crate) struct MqttReply {
@@ -38,10 +37,120 @@ pub(crate) struct MqttReplyRef<'a> {
pub(crate) message: &'a str,
}
struct MqttReciever {
client: MqttSender,
incoming_tx: Sender<MqttReply>,
name: String,
}
#[derive(Clone)]
pub(crate) struct MqttSender {
client: Arc<AsyncClient>,
name: String,
}
impl MqttSender {
pub async fn send_message(
&self,
sub_topic: &str,
message: &str,
retain: bool,
) -> Result<(), ClientError> {
self.client
.publish(
format!("neolink/{}/{}", self.name, sub_topic),
QoS::AtLeastOnce,
retain,
message,
)
.await?;
Ok(())
}
async fn subscribe(&self) -> Result<(), ClientError> {
self.client
.subscribe(format!("neolink/{}/#", self.name), QoS::AtMostOnce)
.await?;
Ok(())
}
async fn update_status(&self) -> Result<()> {
self.send_message("status", "disconnected", true).await?;
Ok(())
}
pub fn try_send_message(&self, sub_topic: &str, message: &str, retain: bool) -> Result<()> {
self.client
.try_publish(
format!("neolink/{}/{}", self.name, sub_topic),
QoS::AtLeastOnce,
retain,
message,
)
.map_err(|e| e.into())
}
}
impl MqttReciever {
async fn run(&mut self, connection: &mut EventLoop) -> Result<()> {
// This acts as an event loop
let name = self.name.clone();
info!("Starting MQTT Client for {}", name);
loop {
let notification = connection
.poll()
.await
.with_context(|| "MQTT connection dropped")?;
match notification {
Event::Incoming(Incoming::ConnAck(connected)) => {
if ConnectReturnCode::Success == connected.code {
self.client
.update_status()
.await
.context("Failed to update status")?;
// We succesfully logged in. Now ask for the cameras subscription.
self.client
.subscribe()
.await
.context("Failed to subscribe")?;
}
}
Event::Incoming(Incoming::Publish(published_message)) => {
if self.handle_message(published_message).await.is_err() {
error!("Failed to forward messages in mqtt");
}
}
_ => {}
}
}
}
async fn handle_message(&mut self, published_message: Publish) -> Result<()> {
if let Some(sub_topic) = published_message
.topic
.strip_prefix(&format!("neolink/{}/", &self.name))
{
if self
.incoming_tx
.send(MqttReply {
topic: sub_topic.to_string(),
message: String::from_utf8_lossy(published_message.payload.as_ref())
.into_owned(),
})
.await
.is_err()
{
error!("Failed to send messages up the mqtt msg channel");
}
}
Ok(())
}
}
impl Mqtt {
pub(crate) fn new(config: &MqttConfig, name: &str, app: Arc<App>) -> Arc<Self> {
let incoming = unbounded::<Publish>();
let msg_channel = unbounded::<MqttReply>();
pub(crate) async fn new(config: &MqttConfig, name: &str) -> Self {
let (incoming_tx, incoming) = channel::<MqttReply>(100);
let mut mqttoptions = MqttOptions::new(
format!("Neolink-{}", name),
&config.broker_addr,
@@ -80,7 +189,7 @@ impl Mqtt {
mqttoptions.set_credentials(username, password);
}
mqttoptions.set_keep_alive(5);
mqttoptions.set_keep_alive(Duration::from_secs(5));
// On unclean disconnect send this
mqttoptions.set_last_will(LastWill::new(
@@ -90,162 +199,79 @@ impl Mqtt {
true,
));
let (client, connection) = Client::new(mqttoptions, 10);
let (client, mut connection) = AsyncClient::new(mqttoptions, 10);
let me = Self {
app: app.clone(),
client: Mutex::new(client),
connection: Mutex::new(connection),
name: name.to_string(),
incoming,
msg_channel,
// Send the drop message on clean disconnect
drop_message: Mutex::new(Some(MqttReply {
topic: "status".to_string(),
message: "offline".to_string(),
})),
};
let arc_me = Arc::new(me);
let client = Arc::new(client);
// Start the mqtt server
let mqtt_running = arc_me.clone();
let mqtt_app = app.clone();
let mqtt_name = name.to_string();
std::thread::spawn(move || {
let _ = (*mqtt_running).start();
mqtt_app.abort(&mqtt_name)
});
let mut set = JoinSet::<Result<()>>::new();
// Start polling messages
info!("{}: Starting listening to mqtt", name);
let mqtt_read_app = app;
let mqtt_reading = arc_me.clone();
let mqtt_read_name = name.to_string();
std::thread::spawn(move || {
while mqtt_read_app.running(&format!("app:{}", mqtt_read_name)) {
if (*mqtt_reading).handle_message().is_err() {
error!(
"Failed to get messages from mqtt client {}",
(mqtt_read_name)
);
}
}
});
let mut reciever = MqttReciever {
incoming_tx,
name: name.to_string(),
client: MqttSender {
client: client.clone(),
name: name.to_string(),
},
};
set.spawn(async move { reciever.run(&mut connection).await });
arc_me
}
#[allow(unused)]
pub(crate) fn set_drop_message(&mut self, topic: &str, message: &str) {
self.drop_message.lock().unwrap().replace(MqttReply {
topic: topic.to_string(),
message: message.to_string(),
});
}
fn subscribe(&self) -> Result<(), ClientError> {
let mut client = self.client.lock().unwrap();
client.subscribe(format!("neolink/{}/#", self.name), QoS::AtMostOnce)?;
Ok(())
}
fn update_status(&self) -> Result<(), ClientError> {
self.send_message("status", "disconnected", true)?;
Ok(())
}
pub fn send_message(
&self,
sub_topic: &str,
message: &str,
retain: bool,
) -> Result<(), ClientError> {
let mut client = self.client.lock().unwrap();
client.publish(
format!("neolink/{}/{}", self.name, sub_topic),
QoS::AtLeastOnce,
retain,
message,
)?;
Ok(())
}
fn handle_message(&self) -> Result<(), RecvError> {
let (_, receiver) = &self.incoming;
let published_message = receiver.recv()?;
if let Some(sub_topic) = published_message
.topic
.strip_prefix(&format!("neolink/{}/", &self.name))
{
if self
.msg_channel
.0
.send(MqttReply {
topic: sub_topic.to_string(),
message: String::from_utf8_lossy(published_message.payload.as_ref())
.into_owned(),
})
.is_err()
{
error!("Failed to send messages up the mqtt msg channel");
}
Self {
client,
name: name.to_string(),
incoming,
set,
// Send the drop message on clean disconnect
drop_message: Some(MqttReply {
topic: "status".to_string(),
message: "offline".to_string(),
}),
}
Ok(())
}
pub fn get_message_listener(&self) -> Receiver<MqttReply> {
self.msg_channel.1.clone()
pub fn get_sender(&self) -> MqttSender {
MqttSender {
client: self.client.clone(),
name: self.name.to_string(),
}
}
pub(crate) fn poll(&self) -> Result<MqttReply> {
self.get_message_listener()
.recv()
.context("Mqtt Polling error")
}
pub(crate) fn start(&self) -> Result<()> {
// This acts as an event loop
let mut connection = self.connection.lock().unwrap();
let (sender, _) = &self.incoming;
info!("Starting MQTT Client for {}", self.name);
while self.app.running(&format!("app:{}", self.name)) {
for (_i, notification) in connection.iter().enumerate() {
if let Ok(notification) = notification {
match notification {
Event::Incoming(Incoming::ConnAck(connected)) => {
if ConnectReturnCode::Success == connected.code {
self.update_status().context("Failed to update status")?;
// We succesfully logged in. Now ask for the cameras subscription.
self.subscribe().context("Failed to subscribe")?;
}
/// This will also error is the join set errors
pub(crate) async fn poll(&mut self) -> Result<MqttReply> {
let (incoming, set) = (&mut self.incoming, &mut self.set);
tokio::select! {
v = incoming.recv() => v.with_context(|| "Mqtt Polling error"),
v = async {
while let Some(res) = set.join_next().await {
match res {
Err(e) => {
set.abort_all();
return Err(e.into());
}
Event::Incoming(Incoming::Publish(published_message)) => {
if sender.send(published_message).is_err() {
error!("Failed to publish motion message on mqtt");
}
Ok(Err(e)) => {
set.abort_all();
return Err(e);
}
_ => {}
Ok(Ok(())) => {}
}
}
}
Err(anyhow!("MQTT background thread dropped without error"))
} => v.with_context(|| "MQTT Threads aborted"),
}
Ok(())
}
}
impl Drop for Mqtt {
fn drop(&mut self) {
let drop_message = self.drop_message.lock().unwrap();
if let Some(drop_message) = drop_message.as_ref() {
if let Some(drop_message) = self.drop_message.as_ref() {
if self
.send_message(&drop_message.topic, &drop_message.message, true)
.get_sender()
.try_send_message(&drop_message.topic, &drop_message.message, true)
.is_err()
{
error!("Failed to send offline message to mqtt");
}
}
self.set.abort_all();
}
}
+3 -3
View File
@@ -1,5 +1,5 @@
use anyhow::{anyhow, Result};
use structopt::StructOpt;
use clap::Parser;
fn onoff_parse(src: &str) -> Result<bool> {
match src {
@@ -13,11 +13,11 @@ fn onoff_parse(src: &str) -> Result<bool> {
}
/// The pir command will control the PIR status of the camera
#[derive(StructOpt, Debug)]
#[derive(Parser, Debug)]
pub struct Opt {
/// The name of the camera. Must be a name in the config
pub camera: String,
/// Whether to turn the PIR ON or OFF
#[structopt(parse(try_from_str = onoff_parse), name = "on|off")]
#[arg(value_parser = onoff_parse, name = "on|off")]
pub on: Option<bool>,
}
+2 -2
View File
@@ -1,7 +1,7 @@
use structopt::StructOpt;
use clap::Parser;
/// The reboot command will reboot the camera
#[derive(StructOpt, Debug)]
#[derive(Parser, Debug)]
pub struct Opt {
/// The name of the camera to change the lights of. Must be a name in the config
pub camera: String,
+2 -2
View File
@@ -1,5 +1,5 @@
use structopt::StructOpt;
use clap::Parser;
/// The rtsp command will serve all cameras in the config over the rtsp protocol
#[derive(StructOpt, Debug)]
#[derive(Parser, Debug)]
pub struct Opt {}
+1 -1
View File
@@ -6,7 +6,7 @@ mod sender;
mod server;
mod shared;
use factory::*;
pub(crate) use factory::*;
pub(crate) use self::server::NeoRtspServer;
+308 -129
View File
@@ -11,8 +11,8 @@ use gstreamer::{
glib::{self, Object},
Structure,
};
use gstreamer::{Bin, Caps, ClockTime, Element, ElementFactory};
use gstreamer_app::{AppSrc, AppStreamType};
use gstreamer::{Bin, Caps, Element, ElementFactory};
use gstreamer_app::{AppSrc, AppSrcCallbacks, AppStreamType};
use gstreamer_rtsp::RTSPUrl;
use gstreamer_rtsp_server::prelude::*;
use gstreamer_rtsp_server::subclass::prelude::*;
@@ -26,7 +26,10 @@ use std::{
sync::{atomic::Ordering, Arc},
};
use tokio::{
sync::mpsc::{channel, Sender},
sync::{
mpsc::{channel, Sender},
RwLock,
},
task::JoinSet,
};
use tokio_stream::wrappers::ReceiverStream;
@@ -38,14 +41,16 @@ glib::wrapper! {
impl Default for NeoMediaFactory {
fn default() -> Self {
Self::new()
Self::new(100, true)
}
}
impl NeoMediaFactory {
pub(crate) fn new() -> Self {
let factory = Object::new::<NeoMediaFactory>(&[]);
pub(crate) fn new(buffer_size: usize, use_smoothing: bool) -> Self {
let factory = Object::new::<NeoMediaFactory>();
factory.set_shared(false);
factory.imp().shared.set_buffer_size(buffer_size);
factory.imp().shared.set_use_smoothing(use_smoothing);
// factory.set_do_retransmission(false); // Can't use as the method is missing on the 32bit docker gst dll
factory.set_launch("videotestsrc pattern=\"snow\" ! video/x-raw,width=896,height=512,framerate=25/1 ! textoverlay name=\"inittextoverlay\" text=\"Stream not Ready\" valignment=top halignment=left font-desc=\"Sans, 32\" ! jpegenc ! rtpjpegpay name=pay0");
factory.set_suspend_mode(RTSPSuspendMode::None);
@@ -53,19 +58,18 @@ impl NeoMediaFactory {
factory
}
pub(crate) fn get_sender(&self) -> Sender<BcMedia> {
pub(crate) fn get_sender(&self) -> Sender<FactoryCommand> {
self.imp().sender.clone()
}
pub(crate) fn add_permitted_roles<T: AsRef<str>>(&self, permitted_roles: &HashSet<T>) {
for permitted_role in permitted_roles {
self.add_role_from_structure(&Structure::new(
permitted_role.as_ref(),
&[
(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true),
(*RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, &true),
],
));
self.add_role_from_structure(
&Structure::builder(permitted_role.as_ref())
.field(RTSP_PERM_MEDIA_FACTORY_ACCESS, true)
.field(RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, true)
.build(),
);
}
// During auth, first it binds anonymously. At this point it checks
// RTSP_PERM_MEDIA_FACTORY_ACCESS to see if anyone can connect
@@ -87,10 +91,11 @@ impl NeoMediaFactory {
.collect::<HashSet<&str>>()
.contains(&"anonymous")
{
self.add_role_from_structure(&Structure::new(
"anonymous",
&[(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true)],
));
self.add_role_from_structure(
&Structure::builder("anonymous")
.field(RTSP_PERM_MEDIA_FACTORY_ACCESS, true)
.build(),
);
}
}
@@ -103,49 +108,74 @@ impl NeoMediaFactory {
pub(crate) fn buffer_ready(&self) -> bool {
self.imp().buffer_ready()
}
pub(crate) async fn join(&self) -> AnyResult<()> {
self.imp().join().await
}
}
unsafe impl Send for NeoMediaFactory {}
unsafe impl Sync for NeoMediaFactory {}
#[derive(Debug)]
pub(crate) enum FactoryCommand {
BcMedia(BcMedia),
ClearBuffer,
JumpToLive,
Pause,
Resume,
}
pub(crate) struct NeoMediaFactoryImpl {
sender: Sender<BcMedia>,
clientsender: Sender<ClientPipelineData>,
sender: Sender<FactoryCommand>,
clientsender: Sender<NeoMediaSender>,
shared: Arc<NeoMediaShared>,
#[allow(dead_code)] // Not dead just need a handle to keep it alive and drop with this obj
threads: JoinSet<AnyResult<()>>,
threads: RwLock<JoinSet<AnyResult<()>>>,
}
impl Drop for NeoMediaFactoryImpl {
fn drop(&mut self) {
log::info!("Dopping NeoMediaFactoryImpl");
log::debug!("Dopping NeoMediaFactoryImpl");
}
}
impl Default for NeoMediaFactoryImpl {
fn default() -> Self {
warn!("Constructing Factor Impl");
debug!("Constructing Factor Impl");
let (datasender, datarx) = channel(3);
let (clientsender, rx_clientsender) = channel(3);
let shared: Arc<NeoMediaShared> = Default::default();
// Prepare thread that sends data into the appsrcs
let mut threads: JoinSet<AnyResult<()>> = Default::default();
let mut sender = NeoMediaSender {
data_source: ReceiverStream::new(datarx),
clientsource: ReceiverStream::new(rx_clientsender),
shared: shared.clone(),
uid: Default::default(),
clientdata: Default::default(),
waiting_for_iframe: true,
};
threads.spawn(async move { sender.run().await });
let mut sender = NeoMediaSenders::new(
shared.clone(),
ReceiverStream::new(datarx),
ReceiverStream::new(rx_clientsender),
100,
);
threads.spawn(async move {
loop {
tokio::task::yield_now().await;
match sender.run().await {
Err(e) => {
warn!("Media send thead failed... restarting. Cause: {:?}", e);
}
Ok(()) => {
break;
}
}
}
unreachable!();
// Ok(())
});
Self {
sender: datasender,
clientsender,
shared,
threads,
threads: RwLock::new(threads),
}
}
}
@@ -196,6 +226,32 @@ fn make_element(kind: &str, name: &str) -> AnyResult<Element> {
})
}
fn make_queue(name: &str) -> AnyResult<Element> {
let queue = make_element("queue", name)?;
queue.set_property_from_str("leaky", "downstream");
queue.set_property("max-size-bytes", 0u32);
queue.set_property("max-size-buffers", 0u32);
queue.set_property(
"max-size-time",
std::convert::TryInto::<u64>::try_into(tokio::time::Duration::from_secs(5).as_nanos())
.unwrap_or(0),
);
Ok(queue)
}
fn make_queue2(name: &str) -> AnyResult<Element> {
let queue = make_element("queue2", name)?;
queue.set_property("use-buffering", true);
queue.set_property("max-size-bytes", 0u32);
queue.set_property("max-size-buffers", 0u32);
queue.set_property(
"max-size-time",
std::convert::TryInto::<u64>::try_into(tokio::time::Duration::from_secs(5).as_nanos())
.unwrap_or(0),
);
Ok(queue)
}
impl NeoMediaFactoryImpl {
pub(crate) fn buffer_ready(&self) -> bool {
self.shared.buffer_ready.load(Ordering::Relaxed)
@@ -214,80 +270,23 @@ impl NeoMediaFactoryImpl {
bin.remove(&element)?;
}
let mut client_data = ClientPipelineData {
start_time: self.shared.microseconds.load(Ordering::Relaxed),
..Default::default()
};
let mut client_data = NeoMediaSender::new(
self.shared.get_buffer_size(),
self.shared.get_use_smoothing(),
);
// Now contruct the actual ones
match *self.shared.vid_format.blocking_read() {
VidFormats::H265 => {
debug!("Building H265 Pipeline");
let source = make_element("appsrc", "vidsrc")?
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_base_time(ClockTime::from_mseconds(
self.shared.microseconds.load(Ordering::Relaxed),
));
source.set_is_live(true);
source.set_block(true);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Stream);
// source.set_caps(Some(
// &Caps::builder("video/x-h265").field("parsed", false).build(),
// ));
let source = source
.dynamic_cast::<Element>()
.map_err(|_| anyhow!("Cannot cast back"))?;
let queue = make_element("queue", "source_queue")?;
let parser = make_element("h265parse", "parser")?;
let payload = make_element("rtph265pay", "pay0")?;
bin.add_many(&[&source, &queue, &parser, &payload])?;
Element::link_many(&[&source, &queue, &parser, &payload])?;
let source = source
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
client_data.vidsrc.replace(source);
}
VidFormats::H264 => {
debug!("Building H264 Pipeline");
let source = make_element("appsrc", "vidsrc")?
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_base_time(ClockTime::from_mseconds(
self.shared.microseconds.load(Ordering::Relaxed),
));
source.set_is_live(true);
source.set_block(true);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Stream);
// source.set_caps(Some(
// &Caps::builder("video/x-h264").field("parsed", false).build(),
// ));
let source = source
.dynamic_cast::<Element>()
.map_err(|_| anyhow!("Cannot cast back"))?;
let queue = make_element("queue", "source_queue")?;
let parser = make_element("h264parse", "parser")?;
let payload = make_element("rtph264pay", "pay0")?;
bin.add_many(&[&source, &queue, &parser, &payload])?;
Element::link_many(&[&source, &queue, &parser, &payload])?;
let source = source
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
client_data.vidsrc.replace(source);
}
VidFormats::Unknown => {
match (
*self.shared.vid_format.blocking_read(),
self.shared.buffer_ready.load(Ordering::Relaxed),
) {
(VidFormats::Unknown, true) | (_, false) => {
debug!("Building Unknown Pipeline");
let source = make_element("videotestsrc", "vidsrc")?;
let source = make_element("videotestsrc", "testvidsrc")?;
source.set_property_from_str("pattern", "snow");
let queue = make_element("queue", "queue0")?;
source.set_property("num-buffers", 500i32); // Send buffers then EOS
let queue = make_queue("queue0")?;
let queue2 = make_queue2("queue2")?;
let overlay = make_element("textoverlay", "overlay")?;
overlay.set_property("text", "Stream not Ready");
overlay.set_property_from_str("valignment", "top");
@@ -296,7 +295,7 @@ impl NeoMediaFactoryImpl {
let encoder = make_element("jpegenc", "encoder")?;
let payload = make_element("rtpjpegpay", "pay0")?;
bin.add_many(&[&source, &queue, &overlay, &encoder, &payload])?;
bin.add_many(&[&source, &queue, &queue2, &overlay, &encoder, &payload])?;
source.link_filtered(
&queue,
&Caps::builder("video/x-raw")
@@ -307,40 +306,198 @@ impl NeoMediaFactoryImpl {
.build(),
)?;
// source.link(&queue)?;
queue.link(&overlay)?;
overlay.link(&encoder)?;
encoder.link(&payload)?;
Element::link_many(&[&queue, &queue2, &overlay, &encoder, &payload])?;
}
(VidFormats::H265, true) => {
debug!("Building H265 Pipeline");
let source = make_element("appsrc", "vidsrc")?
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_is_live(true);
source.set_block(false);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Seekable);
let need_command = client_data.get_commader();
let enough_command = client_data.get_commader();
let seek_command = client_data.get_commader();
source.set_callbacks(
AppSrcCallbacks::builder()
.need_data(move |_appsrc, _amt| {
let _ = need_command.blocking_send(NeoMediaSenderCommand::Resume);
})
.enough_data(move |_appsrc| {
let _ = enough_command.blocking_send(NeoMediaSenderCommand::Pause);
})
.seek_data(move |appsrc, seek_pos| {
debug!("Send seek H265");
let runtime = appsrc.clock().and_then(|clock| {
clock.time().and_then(|time| {
appsrc.base_time().map(|base_time| {
time.saturating_sub(base_time).useconds() as i64
})
})
});
let res = seek_command
.blocking_send(NeoMediaSenderCommand::Seek(runtime, seek_pos))
.is_ok();
debug!("Sent seek H265: {}", res);
res
})
.build(),
);
// source.set_caps(Some(
// &Caps::builder("video/x-h265").field("parsed", false).build(),
// ));
let source = source
.dynamic_cast::<Element>()
.map_err(|_| anyhow!("Cannot cast back"))?;
let queue = make_queue("source_queue")?;
let queue2 = make_queue2("queue2")?;
let parser = make_element("h265parse", "parser")?;
// parser.set_property("config-interval", 5i32);
let payload = make_element("rtph265pay", "pay0")?;
bin.add_many(&[&source, &queue, &queue2, &parser, &payload])?;
Element::link_many(&[&source, &queue, &queue2, &parser, &payload])?;
let source = source
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
client_data.update_vid(source);
}
(VidFormats::H264, true) => {
debug!("Building H264 Pipeline");
let source = make_element("appsrc", "vidsrc")?
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_is_live(true);
source.set_block(false);
source.set_property("emit-signals", false);
source.set_max_bytes(50000000u64); // 50MB
// source.set_property("max-buffers", 0u32); // v1.20
// source.set_property("max-time", Duration::from_secs_f32(2.25).as_nanos() as u64);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Seekable);
let need_command = client_data.get_commader();
let enough_command = client_data.get_commader();
let seek_command = client_data.get_commader();
source.set_callbacks(
AppSrcCallbacks::builder()
.need_data(move |_appsrc, _amt| {
let _ = need_command.blocking_send(NeoMediaSenderCommand::Resume);
})
.enough_data(move |_appsrc| {
let _ = enough_command.blocking_send(NeoMediaSenderCommand::Pause);
})
.seek_data(move |appsrc, seek_pos| {
debug!("Send seek H264: {}", seek_pos);
let runtime = appsrc.clock().and_then(|clock| {
clock.time().and_then(|time| {
appsrc.base_time().map(|base_time| {
time.saturating_sub(base_time).useconds() as i64
})
})
});
let result = seek_command
.blocking_send(NeoMediaSenderCommand::Seek(runtime, seek_pos))
.is_ok();
debug!(" - Sent seek H264: {}", result);
result
})
.build(),
);
// source.set_caps(Some(
// &Caps::builder("video/x-h264").field("parsed", false).build(),
// ));
let source = source
.dynamic_cast::<Element>()
.map_err(|_| anyhow!("Cannot cast back"))?;
let queue = make_queue("source_queue")?;
let queue2 = make_queue2("queue2")?;
let parser = make_element("h264parse", "parser")?;
// parser.set_property("update-timecode", true);
let payload = make_element("rtph264pay", "pay0")?;
// payload.set_property("config-interval", 1i32);
// let storage = make_element("rtpstorage", "vidstorage")?;
// storage.set_property("size-time", Duration::from_secs_f32(2.25).as_nanos() as u64);
// let jitter = make_element("rtpjitterbuffer", "pay0")?;
// jitter.set_property("latency", Duration::from_secs_f32(2.0).as_nanos() as u32);
// payload.set_property("config-interval", 5i32);
bin.add_many(&[
&source, &queue, &queue2, &parser,
&payload,
// &storage,
// &jitter,
])?;
Element::link_many(&[
&source, &queue, &queue2, &parser,
&payload,
// &storage,
// &jitter,
])?;
let source = source
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
client_data.update_vid(source);
}
}
match *self.shared.aud_format.blocking_read() {
AudFormats::Unknown => {}
AudFormats::Aac => {
// let do_aud = false;
// if do_aud {
match (
*self.shared.aud_format.blocking_read(),
self.shared.buffer_ready.load(Ordering::Relaxed),
) {
(AudFormats::Unknown, true) | (_, false) => {}
(AudFormats::Aac, true) => {
debug!("Building Aac pipeline");
let source = make_element("appsrc", "audsrc")?
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_base_time(ClockTime::from_mseconds(
self.shared.microseconds.load(Ordering::Relaxed),
));
source.set_is_live(true);
source.set_block(true);
source.set_block(false);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Stream);
source.set_stream_type(AppStreamType::Seekable);
let need_command = client_data.get_commader();
let enough_command = client_data.get_commader();
// let seek_command = client_data.get_commader();
source.set_callbacks(
AppSrcCallbacks::builder()
.need_data(move |_appsrc, _amt| {
let _ = need_command.blocking_send(NeoMediaSenderCommand::Resume);
})
.enough_data(move |_appsrc| {
let _ = enough_command.blocking_send(NeoMediaSenderCommand::Pause);
})
.seek_data(move |_appsrc, _seek_pos| true)
.build(),
);
let source = source
.dynamic_cast::<Element>()
.map_err(|_| anyhow!("Cannot cast back"))?;
let queue = make_element("queue", "audqueue")?;
let queue = make_queue("audqueue")?;
let queue2 = make_queue2("audqueue2")?;
let parser = make_element("aacparse", "audparser")?;
let decoder = make_element("decodebin", "auddecoder")?;
let encoder = make_element("audioconvert", "audencoder")?;
let payload = make_element("rtpL16pay", "pay1")?;
bin.add_many(&[&source, &queue, &parser, &decoder, &encoder, &payload])?;
Element::link_many(&[&source, &queue, &parser, &decoder])?;
bin.add_many(&[
&source, &queue, &queue2, &parser, &decoder, &encoder, &payload,
])?;
Element::link_many(&[&source, &queue, &queue2, &parser, &decoder])?;
Element::link_many(&[&encoder, &payload])?;
decoder.connect_pad_added(move |_element, pad| {
debug!("Linking encoder to decoder: {:?}", pad.caps());
@@ -354,9 +511,9 @@ impl NeoMediaFactoryImpl {
let source = source
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
client_data.audsrc.replace(source);
client_data.update_aud(source);
}
AudFormats::Adpcm(block_size) => {
(AudFormats::Adpcm(block_size), true) => {
debug!("Building Adpcm pipeline");
// Original command line
// caps=audio/x-adpcm,layout=dvi,block_align={},channels=1,rate=8000
@@ -368,15 +525,12 @@ impl NeoMediaFactoryImpl {
let source = make_element("appsrc", "audsrc")?
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_base_time(ClockTime::from_mseconds(
self.shared.microseconds.load(Ordering::Relaxed),
));
source.set_is_live(true);
source.set_block(true);
source.set_block(false);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Stream);
source.set_stream_type(AppStreamType::Seekable);
source.set_caps(Some(
&Caps::builder("audio/x-adpcm")
.field("layout", "div")
@@ -385,17 +539,34 @@ impl NeoMediaFactoryImpl {
.field("rate", 8000i32)
.build(),
));
let need_command = client_data.get_commader();
let enough_command = client_data.get_commader();
// let seek_command = client_data.get_commader();
source.set_callbacks(
AppSrcCallbacks::builder()
.need_data(move |_appsrc, _amt| {
let _ = need_command.blocking_send(NeoMediaSenderCommand::Resume);
})
.enough_data(move |_appsrc| {
let _ = enough_command.blocking_send(NeoMediaSenderCommand::Pause);
})
.seek_data(move |_appsrc, _seek_pos| true)
.build(),
);
let source = source
.dynamic_cast::<Element>()
.map_err(|_| anyhow!("Cannot cast back"))?;
let queue = make_element("queue", "audqueue")?;
let queue = make_queue("audqueue")?;
let queue2 = make_queue2("audqueue2")?;
let decoder = make_element("decodebin", "auddecoder")?;
let encoder = make_element("audioconvert", "audencoder")?;
let payload = make_element("rtpL16pay", "pay1")?;
bin.add_many(&[&source, &queue, &decoder, &encoder, &payload])?;
Element::link_many(&[&source, &queue, &decoder])?;
bin.add_many(&[&source, &queue, &queue2, &decoder, &encoder, &payload])?;
Element::link_many(&[&source, &queue, &queue2, &decoder])?;
Element::link_many(&[&encoder, &payload])?;
decoder.connect_pad_added(move |_element, pad| {
debug!("Linking encoder to decoder: {:?}", pad.caps());
@@ -409,7 +580,7 @@ impl NeoMediaFactoryImpl {
let source = source
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
client_data.audsrc.replace(source);
client_data.update_aud(source);
}
}
@@ -418,4 +589,12 @@ impl NeoMediaFactoryImpl {
bin.dynamic_cast::<Element>()
.map_err(|_| anyhow!("Cannot cast back"))
}
async fn join(&self) -> AnyResult<()> {
let mut threads = self.threads.write().await;
while let Some(thread) = threads.join_next().await {
thread??;
}
Ok(())
}
}
+935 -263
View File
File diff suppressed because it is too large. Load diff
+212 -21
View File
@@ -13,10 +13,9 @@ use gstreamer_rtsp_server::{
gio::{TlsAuthenticationMode, TlsCertificate},
prelude::*,
subclass::prelude::*,
RTSPAuth, RTSPServer, RTSPToken, RTSP_TOKEN_MEDIA_FACTORY_ROLE,
RTSPAuth, RTSPFilterResult, RTSPServer, RTSPToken, RTSP_TOKEN_MEDIA_FACTORY_ROLE,
};
use log::*;
use neolink_core::bcmedia::model::*;
use std::{
collections::{hash_map::Entry, HashMap, HashSet},
fs,
@@ -24,7 +23,8 @@ use std::{
};
use tokio::{
sync::{mpsc::Sender, RwLock},
task::JoinHandle,
task::JoinSet,
time::{timeout, Duration},
};
glib::wrapper! {
@@ -41,16 +41,23 @@ impl Default for NeoRtspServer {
impl NeoRtspServer {
pub(crate) fn new() -> AnyResult<Self> {
gstreamer::init().context("Gstreamer failed to initialise")?;
let factory = Object::new::<NeoRtspServer>(&[]);
let factory = Object::new::<NeoRtspServer>();
Ok(factory)
}
pub(crate) async fn get_sender<T: Into<String>>(&self, tag: T) -> Option<Sender<BcMedia>> {
pub(crate) async fn get_sender<T: Into<String>>(
&self,
tag: T,
) -> Option<Sender<FactoryCommand>> {
self.imp().get_sender(tag).await
}
pub(crate) async fn create_stream<U: Into<String>>(&self, tag: U) -> AnyResult<()> {
self.imp().create_stream(tag).await
pub(crate) async fn create_stream<U: Into<String>>(
&self,
tag: U,
config: &CameraConfig,
) -> AnyResult<()> {
self.imp().create_stream(tag, config).await
}
#[allow(dead_code)]
@@ -83,11 +90,7 @@ impl NeoRtspServer {
self.imp().add_permitted_roles(tag, permitted_users).await
}
pub(crate) async fn run(
&self,
bind_addr: &str,
bind_port: u16,
) -> AnyResult<(JoinHandle<AnyResult<()>>, Arc<MainLoop>)> {
pub(crate) async fn run(&self, bind_addr: &str, bind_port: u16) -> AnyResult<()> {
let server = self;
server.set_address(bind_addr);
server.set_service(&format!("{}", bind_port));
@@ -96,10 +99,34 @@ impl NeoRtspServer {
let main_loop = Arc::new(MainLoop::new(None, false));
// Run the Glib main loop.
let main_loop_thread = main_loop.clone();
#[allow(clippy::unit_arg)]
let handle = tokio::task::spawn_blocking(move || Ok(main_loop_thread.run()));
let handle = tokio::task::spawn_blocking(move || {
main_loop_thread.run();
AnyResult::Ok(())
});
timeout(Duration::from_secs(5), self.imp().threads.write())
.await
.with_context(|| "Timeout waiting to lock Server threads")?
.spawn(async move { handle.await? });
timeout(Duration::from_secs(5), self.imp().main_loop.write())
.await
.with_context(|| "Timeout waiting to lock Server main_loop")?
.replace(main_loop);
Ok(())
}
Ok((handle, main_loop))
pub(crate) async fn quit(&self) -> AnyResult<()> {
if let Some(main_loop) = self.imp().main_loop.read().await.as_ref() {
main_loop.quit();
}
Ok(())
}
pub(crate) async fn join(&self) -> AnyResult<()> {
let mut threads = self.imp().threads.write().await;
while let Some(thread) = threads.join_next().await {
thread??;
}
Ok(())
}
pub(crate) fn set_up_tls(&self, config: &Config) {
@@ -150,6 +177,155 @@ impl NeoRtspServer {
pub(crate) async fn buffer_ready<T: Into<String>>(&self, tag: T) -> Option<bool> {
self.imp().buffer_ready(tag).await
}
#[allow(dead_code)]
/// Clear all sessions on a given path
pub(crate) async fn clear_session<T: Into<String>>(&self, tag: T) {
let tag: String = tag.into();
if let Some(media) = self.imp().medias.read().await.get(&tag) {
self.clear_session_paths(media.paths.iter());
}
}
fn clear_session_paths<T: AsRef<str>>(&self, paths: impl Iterator<Item = T>) {
let paths: Vec<_> = paths.collect();
self.client_filter(Some(&mut |_server, client| {
let mut close_client = false;
client.session_filter(Some(&mut |_client, session| {
let mut clean_up = false;
session.filter(Some(&mut |_session, session_media| {
if paths.iter().any(|p| {
let s = p.as_ref();
session_media
.matches(s)
.map(|amt| amt == s.len() as i32)
.unwrap_or(false)
}) {
info!("Removing Session Media");
clean_up = true;
RTSPFilterResult::Remove
} else {
RTSPFilterResult::Keep
}
}));
if clean_up {
close_client = true;
RTSPFilterResult::Remove
} else {
RTSPFilterResult::Keep
}
}));
if close_client {
client.close();
RTSPFilterResult::Remove
} else {
RTSPFilterResult::Keep
}
}));
}
/// Clear all sessions on the rtsp server that are using the not ready stream
pub(crate) async fn clear_session_notready<T: Into<String>>(&self, tag: T) {
let tag: String = tag.into();
if let Some(media) = self.imp().medias.read().await.get(&tag) {
self.clear_session_paths_notready(media.paths.iter());
}
}
fn clear_session_paths_notready<T: AsRef<str>>(&self, paths: impl Iterator<Item = T>) {
let paths: Vec<_> = paths.collect();
self.client_filter(Some(&mut |_server, client| {
let mut close_client = false;
client.session_filter(Some(&mut |_client, session| {
let mut clean_up = false;
session.filter(Some(&mut |_session, session_media| {
if paths.iter().any(|p| {
let s = p.as_ref();
session_media
.matches(s)
.map(|amt| amt == s.len() as i32)
.unwrap_or(false)
}) {
// Path is correct now check for no vidsrc
let element = session_media
.media()
.expect("Media should exist")
.element()
.dynamic_cast::<gstreamer::Bin>()
.expect("Media element should be a bin");
// debug!("Searching for testvidsrc on {:?}", element.name());
// for child in element.children().iter() {
// debug!(" - Child: {:?}", child.name());
// }
if element.child_by_name("testvidsrc").is_some() {
info!("Removing Session Media");
clean_up = true;
RTSPFilterResult::Remove
} else {
RTSPFilterResult::Keep
}
} else {
RTSPFilterResult::Keep
}
}));
if clean_up {
close_client = true;
RTSPFilterResult::Remove
} else {
RTSPFilterResult::Keep
}
}));
if close_client {
client.close();
RTSPFilterResult::Remove
} else {
RTSPFilterResult::Keep
}
}));
}
// Clear buffers on all senders of a tag
#[allow(dead_code)] // Old function, was used to clear buffer on reconnect
pub(crate) async fn clear_buffer<T: Into<String>>(&self, tag: T) -> AnyResult<()> {
if let Some(sender) = self.imp().get_sender(tag).await {
sender.send(FactoryCommand::ClearBuffer).await?;
Ok(())
} else {
Err(anyhow!("No such tag"))
}
}
// Jump to live on all senders of a tag
pub(crate) async fn jump_to_live<T: Into<String>>(&self, tag: T) -> AnyResult<()> {
if let Some(sender) = self.imp().get_sender(tag).await {
sender.send(FactoryCommand::JumpToLive).await?;
Ok(())
} else {
Err(anyhow!("No such tag"))
}
}
// Pause on all senders of a tag
pub(crate) async fn pause<T: Into<String>>(&self, tag: T) -> AnyResult<()> {
if let Some(sender) = self.imp().get_sender(tag).await {
sender.send(FactoryCommand::Pause).await?;
Ok(())
} else {
Err(anyhow!("No such tag"))
}
}
// Resume on all senders of a tag
pub(crate) async fn resume<T: Into<String>>(&self, tag: T) -> AnyResult<()> {
if let Some(sender) = self.imp().get_sender(tag).await {
sender.send(FactoryCommand::Resume).await?;
Ok(())
} else {
Err(anyhow!("No such tag"))
}
}
}
unsafe impl Send for NeoRtspServer {}
@@ -163,6 +339,8 @@ struct FactoryData {
#[derive(Default)]
pub(crate) struct NeoRtspServerImpl {
medias: RwLock<HashMap<String, FactoryData>>,
threads: RwLock<JoinSet<AnyResult<()>>>,
main_loop: RwLock<Option<Arc<MainLoop>>>,
}
impl ObjectImpl for NeoRtspServerImpl {}
@@ -176,12 +354,21 @@ impl ObjectSubclass for NeoRtspServerImpl {
}
impl NeoRtspServerImpl {
pub(crate) async fn create_stream<U: Into<String>>(&self, tag: U) -> AnyResult<()> {
pub(crate) async fn create_stream<U: Into<String>>(
&self,
tag: U,
config: &CameraConfig,
) -> AnyResult<()> {
let key = tag.into();
match self.medias.write().await.entry(key.clone()) {
Entry::Occupied(_occ) => {}
Entry::Vacant(vac) => {
let media = NeoMediaFactory::new();
let media = NeoMediaFactory::new(config.buffer_size, config.use_smoothing);
let thread_media = media.clone();
self.threads
.write()
.await
.spawn(async move { thread_media.join().await });
vac.insert(FactoryData {
factory: media,
paths: Default::default(),
@@ -191,7 +378,10 @@ impl NeoRtspServerImpl {
Ok(())
}
pub(crate) async fn get_sender<T: Into<String>>(&self, tag: T) -> Option<Sender<BcMedia>> {
pub(crate) async fn get_sender<T: Into<String>>(
&self,
tag: T,
) -> Option<Sender<FactoryCommand>> {
let key = tag.into();
self.medias
.read()
@@ -259,7 +449,8 @@ impl NeoRtspServerImpl {
.expect("The server should have mountpoints");
for path in paths {
media.paths.insert(path.clone());
mounts.add_factory(path, &media.factory);
// We can clone here because GObjects are referernce counted
mounts.add_factory(path, media.factory.clone());
// debug!("Adding path: {}", path);
}
Ok(())
@@ -302,13 +493,13 @@ impl NeoRtspServerImpl {
let auth = self.obj().auth().unwrap_or_else(RTSPAuth::new);
auth.set_supported_methods(RTSPAuthMethod::Basic);
let mut un_authtoken = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"anonymous")]);
let mut un_authtoken = RTSPToken::new(&[(RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"anonymous")]);
auth.set_default_token(Some(&mut un_authtoken));
for credential in credentials {
let (user, pass) = credential;
trace!("Setting credentials for user {}", user);
let token = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, user)]);
let token = RTSPToken::new(&[(RTSP_TOKEN_MEDIA_FACTORY_ROLE, user)]);
let basic = RTSPAuth::make_basic(user, pass);
auth.add_basic(basic.as_str(), &token);
}
+21 -10
View File
@@ -1,8 +1,7 @@
//! Data shared between the various
//! components that manage a media stream
use gstreamer_app::AppSrc;
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use tokio::sync::RwLock;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -22,9 +21,10 @@ pub(super) enum AudFormats {
pub(super) struct NeoMediaShared {
pub(super) vid_format: RwLock<VidFormats>,
pub(super) aud_format: RwLock<AudFormats>,
pub(super) microseconds: AtomicU64,
pub(super) number_of_clients: AtomicUsize,
pub(super) buffer_ready: AtomicBool,
pub(super) buffer_size: AtomicUsize,
pub(super) use_smoothing: AtomicBool,
}
impl Default for NeoMediaShared {
@@ -32,17 +32,28 @@ impl Default for NeoMediaShared {
Self {
vid_format: RwLock::new(VidFormats::Unknown),
aud_format: RwLock::new(AudFormats::Unknown),
microseconds: AtomicU64::new(0),
number_of_clients: AtomicUsize::new(0),
buffer_ready: AtomicBool::new(false),
buffer_size: AtomicUsize::new(100),
use_smoothing: AtomicBool::new(false),
}
}
}
#[derive(Default, Debug)]
pub(super) struct ClientPipelineData {
pub(super) vidsrc: Option<AppSrc>,
pub(super) audsrc: Option<AppSrc>,
pub(super) start_time: u64,
pub(super) inited: bool,
impl NeoMediaShared {
pub(super) fn get_buffer_size(&self) -> usize {
self.buffer_size.load(Ordering::Relaxed)
}
pub(super) fn set_buffer_size(&self, new_size: usize) {
self.buffer_size.store(new_size, Ordering::Relaxed)
}
pub(super) fn get_use_smoothing(&self) -> bool {
self.use_smoothing.load(Ordering::Relaxed)
}
pub(super) fn set_use_smoothing(&self, new_value: bool) {
self.use_smoothing.store(new_value, Ordering::Relaxed)
}
}
+108 -37
View File
@@ -61,11 +61,13 @@ use tokio_stream::StreamExt;
mod cmdline;
mod gst;
mod spring;
mod states;
use super::config::Config;
pub(crate) use cmdline::Opt;
use gst::NeoRtspServer;
pub(crate) use spring::*;
use states::*;
/// Entry point for the rtsp subcommand
@@ -95,7 +97,9 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
set.spawn(async move {
let shared = camera.shared.clone();
let name = camera.get_name();
let mut backoff = Duration::from_micros(125);
loop {
tokio::task::yield_now().await;
let failure = camera_main(camera).await;
match failure {
Err(CameraFailureKind::Fatal(e)) => {
@@ -104,6 +108,10 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
}
Err(CameraFailureKind::Retry(e)) => {
warn!("{}: Retryable error: {:X?}", name, e);
tokio::time::sleep(backoff).await;
if backoff < Duration::from_secs(5) {
backoff *= 2;
}
camera = Camera {
shared: shared.clone(),
state: Disconnected {},
@@ -125,14 +133,15 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
let bind_addr = config.bind_addr.clone();
let bind_port = config.bind_port;
let (handle, main_loop) = rtsp.run(&bind_addr, bind_port).await?;
set.spawn(async move { handle.await? });
rtsp.run(&bind_addr, bind_port).await?;
let thread_rtsp = rtsp.clone();
set.spawn(async move { thread_rtsp.join().await });
while let Some(joined) = set.join_next().await {
match &joined {
Err(_) | Ok(Err(_)) => {
// Panicked or error in task
main_loop.quit();
rtsp.quit().await?;
}
Ok(Ok(_)) => {
// All good
@@ -162,47 +171,76 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
.login()
.await
.with_context(|| format!("{}: Could not login to camera", name))
.map_err(CameraFailureKind::Fatal)?;
.map_err(|e| {
let e_inner = e.downcast_ref::<neolink_core::Error>().unwrap();
match e_inner {
neolink_core::Error::CameraLoginFail => CameraFailureKind::Fatal(e),
_ => CameraFailureKind::Retry(e),
}
})?;
let _ = loggedin.manage().await;
let tags = loggedin.shared.get_tags();
let rtsp_thread = loggedin.get_rtsp();
// Clear all buffers present
// Uncomment to clear buffers. This is now handlled in the buffer itself,
// instead of clearing it restamps it whenever there is a jump in the
// timestamps of >1s
//
// tags.iter()
// .map(|tag| rtsp_thread.clear_buffer(tag))
// .collect::<FuturesUnordered<_>>()
// .collect::<Vec<_>>()
// .await;
// Start pulling data from the camera
let mut streaming = loggedin
.stream()
.await
.with_context(|| format!("{}: Could not start stream", name))
.map_err(CameraFailureKind::Retry)?;
let tags = streaming.shared.get_tags();
let rtsp_thread = streaming.get_rtsp();
let mut waiter = tokio::time::interval(Duration::from_micros(500));
loop {
waiter.tick().await;
if tags
.iter()
.map(|tag| rtsp_thread.buffer_ready(tag))
.collect::<FuturesUnordered<_>>()
.all(|f| f.unwrap_or(false))
.await
{
break;
}
}
if streaming.get_config().pause.on_motion || streaming.get_config().pause.on_disconnect {
log::info!("Pause buffer prepared");
// Wait for buffers to be prepared
tokio::select! {
v = async {
let mut waiter = tokio::time::interval(Duration::from_micros(500));
loop {
waiter.tick().await;
if tags
.iter()
.map(|tag| rtsp_thread.buffer_ready(tag))
.collect::<FuturesUnordered<_>>()
.all(|f| f.unwrap_or(false))
.await
{
break;
}
}
Ok(())
} => v,
// Or for stream to error
v = streaming.join() => {v},
}
.with_context(|| format!("{}: Error while waiting for buffers", name))
.map_err(CameraFailureKind::Retry)?;
// tokio::task::spawn(async move {
// let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
// loop {
// inter.tick().await;
// log::info!(
// "Users: {:?}, {}",
// rtsp_thread.get_number_of_clients("Cammy01::Main").await,
// rtsp_thread.num_path_users(&["/Cammy01/mainStream"])
// );
// }
// });
tags.iter()
.map(|tag| rtsp_thread.jump_to_live(tag))
.collect::<FuturesUnordered<_>>()
.collect::<Vec<_>>()
.await;
// Clear "stream not ready" media to try and force a reconnect
// This shoud stop them from watching the "Stream Not Ready" thing
debug!("Clearing not ready clients");
tags.iter()
.map(|tag| rtsp_thread.clear_session_notready(tag))
.collect::<FuturesUnordered<_>>()
.collect::<Vec<_>>()
.await;
log::info!("{}: Buffers prepared", name);
loop {
// Wait for error or reason to pause
@@ -226,7 +264,6 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
// Wait for client to disconnect
let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
info!("Await Client Pause");
loop {
inter.tick().await;
let total_clients = tags.iter().map(|tag| rtsp_thread.get_number_of_clients(tag)).collect::<FuturesUnordered<_>>().fold(0usize, |acc, f| acc + f.unwrap_or(0usize)).await;
@@ -242,6 +279,12 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
}.with_context(|| format!("{}: Error while streaming", name))
.map_err(CameraFailureKind::Retry)?;
tags.iter()
.map(|tag| rtsp_thread.pause(tag))
.collect::<FuturesUnordered<_>>()
.collect::<Vec<_>>()
.await;
let paused = streaming
.stop()
.await
@@ -249,13 +292,29 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
.map_err(CameraFailureKind::Retry)?;
// Wait for reason to restart
tokio::select! {
v = async {
// Wait for motion start and client for start
let mut motion = paused.get_camera().listen_on_motion().await?;
let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
loop {
motion.await_start(Duration::ZERO).await?;
let total_clients = tags.iter().map(|tag| rtsp_thread.get_number_of_clients(tag)).collect::<FuturesUnordered<_>>().fold(0usize, |acc, f| acc + f.unwrap_or(0usize)).await;
if total_clients > 0 {
return Result::<()>::Ok(());
}
inter.tick().await;
}
}, if paused.get_config().pause.on_motion && paused.get_config().pause.on_disconnect => {
info!("Motion and Client Resume");
v.with_context(|| "Error while processing motion/client messages")
},
v = async {
// Wait for motion start
let mut motion = paused.get_camera().listen_on_motion().await?;
motion.await_start(Duration::ZERO).await
}, if paused.get_config().pause.on_motion => {
}, if paused.get_config().pause.on_motion && !paused.get_config().pause.on_disconnect => {
info!("Motion Resume");
v.map_err(|e| anyhow!("Error while processing motion messages: {:?}", e))
v.with_context(|| "Error while processing motion messages")
},
v = async {
// Wait for client to connect
@@ -268,7 +327,7 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
return Ok(())
}
}
}, if paused.get_config().pause.on_disconnect => {
}, if paused.get_config().pause.on_disconnect && !paused.get_config().pause.on_motion => {
info!("Client Resume");
v
},
@@ -282,10 +341,22 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
.with_context(|| format!("{}: Error while paused", name))
.map_err(CameraFailureKind::Retry)?;
tags.iter()
.map(|tag| rtsp_thread.jump_to_live(tag))
.collect::<FuturesUnordered<_>>()
.collect::<Vec<_>>()
.await;
streaming = paused
.stream()
.await
.with_context(|| format!("{}: Could not start stream", name))
.map_err(CameraFailureKind::Retry)?;
tags.iter()
.map(|tag| rtsp_thread.resume(tag))
.collect::<FuturesUnordered<_>>()
.collect::<Vec<_>>()
.await;
}
}
+105
View File
@@ -0,0 +1,105 @@
//! A critically damped spring
use tokio::time::Instant;
#[derive(Debug)]
pub(crate) struct Spring {
last_update: Instant,
target: f64,
value: f64,
velocity: f64,
halflife: f64,
}
impl Spring {
pub(crate) fn new(value: f64, target: f64, halflife: f64) -> Self {
Self {
value,
target,
halflife,
last_update: Instant::now(),
velocity: 0.0f64,
}
}
pub(crate) async fn update(&mut self) {
let now = Instant::now();
let dt = now - self.last_update;
self.last_update = now;
spring_update(
&mut self.value,
self.target,
&mut self.velocity,
self.halflife,
dt.as_secs_f64(),
);
}
// pub(crate) fn value(&self) -> f64 {
// self.value
// }
// pub(crate) fn value_u32(&self) -> u32 {
// self.value.max(std::u32::MIN as f64).floor().min(std::u32::MAX as f64) as u32
// }
pub(crate) fn value_i64(&self) -> i64 {
self.value
.max(std::i64::MIN as f64)
.floor()
.min(std::i64::MAX as f64) as i64
}
pub(crate) fn value_u64(&self) -> u64 {
self.value
.max(std::u64::MIN as f64)
.floor()
.min(std::u64::MAX as f64) as u64
}
#[allow(dead_code)]
pub(crate) fn set_target(&mut self, target: f64) {
self.target = target;
}
#[allow(dead_code)] // Used in debug print
pub(crate) fn get_target(&self) -> f64 {
self.target
}
#[allow(dead_code)]
pub(crate) fn mod_value(&mut self, delta: f64) {
self.target += delta;
self.value += delta;
}
pub(crate) fn reset_to(&mut self, target: f64) {
self.target = target;
self.value = target;
self.velocity = 0.0;
}
}
const EPS: f64 = 1e-5;
fn halflife_to_damping(halflife: f64) -> f64 {
(4.0 * std::f64::consts::LN_2) / (halflife + EPS)
}
// fn damping_to_halflife(damping: f32) -> f32 {
// (4.0f32 * std::f32::consts::LN_2) / (damping + EPS)
// }
fn fast_negexp(x: f64) -> f64 {
1.0 / (1.0 + x + 0.48 * x * x + 0.235 * x * x * x)
}
fn spring_update(value: &mut f64, target: f64, velocity: &mut f64, halflife: f64, dt: f64) {
if halflife.abs() < 1e-3 {
*value = target;
*velocity = 0.0;
} else {
let y = halflife_to_damping(halflife) / 2.0;
let j0 = *value - target;
let j1 = *velocity + j0 * y;
let eydt = fast_negexp(y * dt);
*value = eydt * (j0 + j1 * dt) + target;
*velocity = eydt * (*velocity - j1 * y * dt);
}
}
+9
View File
@@ -36,4 +36,13 @@ impl Camera<Connected> {
},
})
}
#[allow(unused)]
pub(crate) async fn join(&self) -> Result<()> {
self.state
.camera
.join()
.await
.map_err(|e| anyhow::anyhow!("Camera join error: {:?}", e))
}
}
+23 -8
View File
@@ -32,22 +32,28 @@ impl Camera<LoggedIn> {
pub(crate) async fn manage(&self) -> Result<()> {
let cam_time = self.state.camera.get_time().await?;
let mut update = false;
if let Some(time) = cam_time {
info!(
"{}: Camera time is already set: {}",
self.shared.config.name, time
);
if self.shared.config.update_time {
update = true;
}
} else {
use time::OffsetDateTime;
// We'd like now_local() but it's deprecated - try to get the local time, but if no
// time zone, fall back to UTC.
let new_time =
OffsetDateTime::try_now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
update = true;
warn!(
"{}: Camera has no time set, setting to {}",
self.shared.config.name, new_time
"{}: Camera has no time set, Updating",
self.shared.config.name
);
}
if update {
use time::OffsetDateTime;
let new_time =
OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
info!("{}: Setting time to {}", self.shared.config.name, new_time);
match self.state.camera.set_time(new_time).await {
Ok(_) => {
let cam_time = self.state.camera.get_time().await?;
@@ -90,4 +96,13 @@ impl Camera<LoggedIn> {
pub(crate) fn get_camera(&self) -> &BcCamera {
&self.state.camera
}
#[allow(unused)]
pub(crate) async fn join(&self) -> Result<()> {
self.state
.camera
.join()
.await
.map_err(|e| anyhow::anyhow!("Camera join error: {:?}", e))
}
}
+1 -1
View File
@@ -85,7 +85,7 @@ impl Shared {
pub(crate) async fn setup_streams(&self) -> Result<()> {
for stream in self.streams.iter() {
let tag = self.get_tag_for_stream(stream);
self.rtsp.create_stream(&tag).await?;
self.rtsp.create_stream(&tag, &self.config).await?;
self.rtsp
.add_permitted_roles(&tag, &self.permitted_users)
.await?;
+46 -27
View File
@@ -2,14 +2,16 @@
//
// Data is streamed into a gstreamer source
use anyhow::{anyhow, Error, Result};
use anyhow::{anyhow, Context, Error, Result};
use log::*;
use neolink_core::bcmedia::model::BcMedia;
use tokio::time::{timeout, Duration};
use tokio::{sync::RwLock, task::JoinSet};
use neolink_core::bc_protocol::{BcCamera, StreamKind as Stream};
use super::{camera::Camera, LoggedIn};
use crate::rtsp::gst::FactoryCommand;
pub(crate) struct Streaming {
pub(crate) camera: BcCamera,
@@ -43,7 +45,7 @@ impl Camera<Streaming> {
let stream_thead = *stream;
let strict_thread = loggedin.shared.config.strict;
// let tag_thread = tag.clone();
let tag_thread = tag.clone();
let mut stream_data = loggedin
.state
.camera
@@ -51,18 +53,29 @@ impl Camera<Streaming> {
.await?;
set.spawn(async move {
loop {
// debug!("{}: BcMediaStreamRecv", &tag_thread);
let data = timeout(Duration::from_secs(15), stream_data.get_data()).await??;
// match &data {
// Ok(BcMedia::InfoV1(_)) => debug!("{}: - InfoV1", &tag_thread),
// Ok(BcMedia::InfoV2(_)) => debug!("{}: - InfoV2", &tag_thread),
// Ok(BcMedia::Iframe(_)) => debug!("{}: - Iframe", &tag_thread),
// Ok(BcMedia::Pframe(_)) => debug!("{}: - Pframe", &tag_thread),
// Ok(BcMedia::Aac(_)) => debug!("{}: - Aac", &tag_thread),
// Ok(BcMedia::Adpcm(_)) => debug!("{}: - Adpcm", &tag_thread),
// Err(_) => debug!(" - Error"),
// }
sender.send(data?).await?;
tokio::task::yield_now().await;
// debug!("Straming: Get");
let data = timeout(Duration::from_secs(15), stream_data.get_data())
.await
.with_context(|| "Timed out waiting for new Media Frame")??;
// debug!("Straming: Got");
match &data {
Ok(BcMedia::InfoV1(_)) => trace!("{}: - InfoV1", &tag_thread),
Ok(BcMedia::InfoV2(_)) => trace!("{}: - InfoV2", &tag_thread),
Ok(BcMedia::Iframe(_)) => trace!("{}: - Iframe", &tag_thread),
Ok(BcMedia::Pframe(_)) => trace!("{}: - Pframe", &tag_thread),
Ok(BcMedia::Aac(_)) => trace!("{}: - Aac", &tag_thread),
Ok(BcMedia::Adpcm(_)) => trace!("{}: - Adpcm", &tag_thread),
Err(_) => trace!(" - Error"),
}
// debug!("Straming: Send");
timeout(
Duration::from_secs(15),
sender.send(FactoryCommand::BcMedia(data?)),
)
.await
.with_context(|| "Timed out waiting to send Media Frame")??;
// debug!("Straming: Sent");
}
});
}
@@ -87,20 +100,26 @@ impl Camera<Streaming> {
}
pub(crate) async fn join(&self) -> Result<()> {
let mut locked_threads = self.state.set.write().await;
while let Some(res) = locked_threads.join_next().await {
match res {
Err(e) => {
locked_threads.abort_all();
return Err(e.into());
tokio::select! {
v = async {
let mut locked_threads = self.state.set.write().await;
while let Some(res) = locked_threads.join_next().await {
match res {
Err(e) => {
locked_threads.abort_all();
return Err(e.into());
}
Ok(Err(e)) => {
locked_threads.abort_all();
return Err(e);
}
Ok(Ok(())) => {}
}
}
Ok(Err(e)) => {
locked_threads.abort_all();
return Err(e);
}
Ok(Ok(())) => {}
}
}
Ok(())
} => v,
v = self.state.camera.join() => v.map_err(|e| anyhow!("Camera join error: {:?}", e)),
}?;
Ok(())
}
+3 -3
View File
@@ -1,5 +1,5 @@
use anyhow::{anyhow, Result};
use structopt::StructOpt;
use clap::Parser;
fn onoff_parse(src: &str) -> Result<bool> {
match src {
@@ -13,11 +13,11 @@ fn onoff_parse(src: &str) -> Result<bool> {
}
/// The status-light command will control the blue status light on the camera
#[derive(StructOpt, Debug)]
#[derive(Parser, Debug)]
pub struct Opt {
/// The name of the camera to change the lights of. Must be a name in the config
pub camera: String,
/// Whether to turn the light on or off
#[structopt(parse(try_from_str = onoff_parse), name = "on|off")]
#[arg(value_parser = onoff_parse, name = "on|off")]
pub on: bool,
}
+7 -6
View File
@@ -1,5 +1,6 @@
use clap::Parser;
use std::path::PathBuf;
use structopt::StructOpt;
use std::str::FromStr;
/// The talk command will send audio for the camera to say
///
@@ -7,19 +8,19 @@ use structopt::StructOpt;
/// This should be ok with most common formats.
///
/// `gst-launch` can be used to prepare this data
#[derive(StructOpt, Debug)]
#[derive(Parser, Debug)]
pub struct Opt {
/// The name of the camera to talk through. Must be a name in the config
pub camera: String,
/// The path to the audio file.
#[structopt(short, long, parse(from_os_str), conflicts_with = "microphone")]
#[arg(short, long, value_parser = PathBuf::from_str, conflicts_with = "microphone")]
pub file_path: Option<PathBuf>,
/// Use the microphone as the source. Defaults to autoaudiosrc - Which microphone depends
/// on [gstreamer](https://gstreamer.freedesktop.org/documentation/autodetect/autoaudiosrc.html?gi-language=c#autoaudiosrc-page)
#[structopt(short, long, conflicts_with = "file_path")]
#[arg(short, long, conflicts_with = "file_path")]
pub microphone: bool,
/// Use a specific microphone like "alsasrc device=hw:1"
#[structopt(
#[arg(
short,
long,
default_value = "autoaudiosrc",
@@ -27,6 +28,6 @@ pub struct Opt {
)]
pub input_src: String,
/// Use to change the volume of the input
#[structopt(short, long, default_value = "1.0")]
#[arg(short, long, default_value = "1.0")]
pub volume: f32,
}
+8 -9
View File
@@ -166,15 +166,14 @@ fn create_pipeline(
// provide the format we request.
// This can be set after linking the two objects, because format negotiation between
// both elements will happen during pre-rolling of the pipeline.
appsink.set_caps(Some(&Caps::new_simple(
"audio/x-adpcm",
&[
("layout", &"dvi"),
("block_align", &(block_align as i32)),
("channels", &(1i32)),
("rate", &(sample_rate as i32)),
],
)));
appsink.set_caps(Some(
&Caps::builder("audio/x-adpcm")
.field("layout", "dvi")
.field("block_align", block_align as i32)
.field("channels", 1i32)
.field("rate", sample_rate as i32)
.build(),
));
Ok(pipeline)
}