Compare commits

...
39 changed files with 3188 additions and 2684 deletions

No files matched your search

+16 -1
View File
@@ -4,6 +4,12 @@ on:
push:
pull_request:
workflow_dispatch:
inputs:
tag:
description: 'Tag to force the cross build on'
required: false
type: string
default: ''
jobs:
pre_job:
@@ -71,11 +77,15 @@ jobs:
$env:OPENSSL_DIR='D:\\Program Files\OpenSSL\'
}
# Set up pkgconfig for gstreamer
$env:PKG_CONFIG_PATH += ';' + $env:GSTREAMER_1_0_ROOT_MSVC_X86_64 + '\lib\pkgconfig'
# Set github vars
Add-Content -Path $env:GITHUB_ENV -Value "GSTREAMER_1_0_ROOT_MSVC_X86_64=$env:GSTREAMER_1_0_ROOT_MSVC_X86_64"
Add-Content -Path $env:GITHUB_PATH -Value "$env:GSTREAMER_1_0_ROOT_MSVC_X86_64\bin"
Add-Content -Path $env:GITHUB_PATH -Value "%GSTREAMER_1_0_ROOT_MSVC_X86_64%\bin"
Add-Content -Path $env:GITHUB_ENV -Value "OPENSSL_DIR=$env:OPENSSL_DIR"
Add-Content -Path $env:GITHUB_ENV -Value "PKG_CONFIG_PATH=$env:PKG_CONFIG_PATH"
# One last check on directories
dir "$env:GSTREAMER_1_0_ROOT_MSVC_X86_64"
@@ -127,7 +137,7 @@ jobs:
if: needs.pre_job.outputs.should_skip != 'true'
name: cross
runs-on: ubuntu-latest
container: "node:current-bookworm-slim"
container: "node:current-bullseye-slim"
strategy:
fail-fast: false
matrix:
@@ -154,6 +164,8 @@ jobs:
pkgconfig: i386-linux-gnu
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.tag }}
- name: Install basic tools
run: |
apt-get update
@@ -178,6 +190,7 @@ jobs:
echo "${HOME}/.cargo/bin" >> "${GITHUB_PATH}"
- name: Install ${{ matrix.arch }} Rust toolchain
run: |
rustup default 1.79.0
rustup target add ${TARGET}
env:
TARGET: ${{ matrix.target }}
@@ -213,6 +226,8 @@ jobs:
SECRET: "${{ secrets.DOCKER_TOKEN }}"
- name: Check out the repo
uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.tag }}
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
- name: Convert username to lower case for docker
id: string_user
+1 -1
View File
@@ -112,7 +112,7 @@ jobs:
# build a list of images from tags
for j in ${IMAGE_TAGS}
do
if [[ "${j}" =~ ^[0-9][.][0-9][.][0-9]$ ]]; then
if [[ "${j}" =~ ^[v]?[0-9][.][0-9][.][0-9]$ ]]; then
echo "Version tag (${j}) will not be deleted"
continue
fi
+3
View File
@@ -25,3 +25,6 @@
# The token file for the FCM tests
token.toml
# Debug FCM build
./crates/fcm-push-listener/
Generated
+474 -85
View File
@@ -131,27 +131,153 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9"
[[package]]
name = "async-stream"
version = "0.3.5"
name = "async-channel"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd56dd203fef61ac097dd65721a419ddccb106b2d2b70ba60a6b529f03961a51"
checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35"
dependencies = [
"async-stream-impl",
"concurrent-queue",
"event-listener 2.5.3",
"futures-core",
]
[[package]]
name = "async-channel"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89b47800b0be77592da0afd425cc03468052844aff33b84e33cc696f64e77b6a"
dependencies = [
"concurrent-queue",
"event-listener-strategy",
"futures-core",
"pin-project-lite",
]
[[package]]
name = "async-stream-impl"
version = "0.3.5"
name = "async-executor"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193"
checksum = "d7ebdfa2ebdab6b1760375fa7d6f382b9f486eac35fc994625a00e89280bdbb7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.72",
"async-task",
"concurrent-queue",
"fastrand 2.0.2",
"futures-lite 2.3.0",
"slab",
]
[[package]]
name = "async-global-executor"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05b1b633a2115cd122d73b955eadd9916c18c8f510ec9cd1686404c60ad1c29c"
dependencies = [
"async-channel 2.3.1",
"async-executor",
"async-io 2.3.3",
"async-lock 3.4.0",
"blocking",
"futures-lite 2.3.0",
"once_cell",
]
[[package]]
name = "async-io"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fc5b45d93ef0529756f812ca52e44c221b35341892d3dcc34132ac02f3dd2af"
dependencies = [
"async-lock 2.8.0",
"autocfg",
"cfg-if",
"concurrent-queue",
"futures-lite 1.13.0",
"log",
"parking",
"polling 2.8.0",
"rustix 0.37.27",
"slab",
"socket2 0.4.10",
"waker-fn",
]
[[package]]
name = "async-io"
version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d6baa8f0178795da0e71bc42c9e5d13261aac7ee549853162e66a241ba17964"
dependencies = [
"async-lock 3.4.0",
"cfg-if",
"concurrent-queue",
"futures-io",
"futures-lite 2.3.0",
"parking",
"polling 3.7.2",
"rustix 0.38.32",
"slab",
"tracing",
"windows-sys 0.52.0",
]
[[package]]
name = "async-lock"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b"
dependencies = [
"event-listener 2.5.3",
]
[[package]]
name = "async-lock"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff6e472cdea888a4bd64f342f09b3f50e1886d32afe8df3d663c01140b811b18"
dependencies = [
"event-listener 5.3.1",
"event-listener-strategy",
"pin-project-lite",
]
[[package]]
name = "async-std"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d"
dependencies = [
"async-channel 1.9.0",
"async-global-executor",
"async-io 1.13.0",
"async-lock 2.8.0",
"crossbeam-utils",
"futures-channel",
"futures-core",
"futures-io",
"futures-lite 1.13.0",
"gloo-timers",
"kv-log-macro",
"log",
"memchr",
"once_cell",
"pin-project-lite",
"pin-utils",
"slab",
"wasm-bindgen-futures",
]
[[package]]
name = "async-task"
version = "4.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de"
[[package]]
name = "atomic-waker"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
[[package]]
name = "atomic_refcell"
version = "0.1.13"
@@ -197,6 +323,15 @@ version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51"
[[package]]
name = "base64-compat"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a8d4d2746f89841e49230dd26917df1876050f95abafafbe34f47cb534b88d7"
dependencies = [
"byteorder",
]
[[package]]
name = "bitflags"
version = "1.3.2"
@@ -218,6 +353,25 @@ dependencies = [
"generic-array",
]
[[package]]
name = "blocking"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "703f41c54fc768e63e091340b424302bb1c29ef4aa0c7f10fe849dfb114d29ea"
dependencies = [
"async-channel 2.3.1",
"async-task",
"futures-io",
"futures-lite 2.3.0",
"piper",
]
[[package]]
name = "bufstream-fresh"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c431e5d450eceb6f5096c371f502946ae1cc65407935bc2cae8f1d625a2035f"
[[package]]
name = "bumpalo"
version = "3.15.4"
@@ -348,6 +502,15 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "concurrent-queue"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "cookie-factory"
version = "0.3.3"
@@ -614,6 +777,42 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "event-listener"
version = "2.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
[[package]]
name = "event-listener"
version = "5.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6032be9bd27023a771701cc49f9f053c751055f71efb2e0ae5c15809093675ba"
dependencies = [
"concurrent-queue",
"parking",
"pin-project-lite",
]
[[package]]
name = "event-listener-strategy"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f214dc438f977e6d4e3500aaa277f5ad94ca83fbbd9b1a15713ce2344ccc5a1"
dependencies = [
"event-listener 5.3.1",
"pin-project-lite",
]
[[package]]
name = "fastrand"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be"
dependencies = [
"instant",
]
[[package]]
name = "fastrand"
version = "2.0.2"
@@ -640,6 +839,25 @@ dependencies = [
"webpki-roots",
]
[[package]]
name = "fcm-push-listener"
version = "3.0.0"
dependencies = [
"base64 0.21.7",
"ece",
"log",
"prost",
"prost-build",
"rand",
"reqwest",
"serde",
"serde_with",
"tokio",
"tokio-rustls 0.23.4",
"uuid",
"webpki-roots",
]
[[package]]
name = "fixedbitset"
version = "0.4.2"
@@ -735,6 +953,34 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
[[package]]
name = "futures-lite"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49a9d51ce47660b1e808d3c990b4709f2f415d928835a17dfd16991515c46bce"
dependencies = [
"fastrand 1.9.0",
"futures-core",
"futures-io",
"memchr",
"parking",
"pin-project-lite",
"waker-fn",
]
[[package]]
name = "futures-lite"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52527eb5074e35e9339c6b4e8d12600c7128b68fb25dcb9fa9dec18f7c25f3a5"
dependencies = [
"fastrand 2.0.2",
"futures-core",
"futures-io",
"parking",
"pin-project-lite",
]
[[package]]
name = "futures-macro"
version = "0.3.30"
@@ -907,6 +1153,18 @@ dependencies = [
"system-deps",
]
[[package]]
name = "gloo-timers"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c"
dependencies = [
"futures-channel",
"futures-core",
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "gobject-sys"
version = "0.20.0"
@@ -1172,6 +1430,12 @@ version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "hermit-abi"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc"
[[package]]
name = "hex"
version = "0.4.3"
@@ -1268,7 +1532,7 @@ dependencies = [
"httpdate",
"itoa",
"pin-project-lite",
"socket2",
"socket2 0.5.6",
"tokio",
"tower-service",
"tracing",
@@ -1317,16 +1581,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "idna"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c"
dependencies = [
"unicode-bidi",
"unicode-normalization",
]
[[package]]
name = "idna"
version = "0.5.0"
@@ -1337,12 +1591,6 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "if_chain"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed"
[[package]]
name = "indexmap"
version = "1.9.3"
@@ -1379,6 +1627,26 @@ dependencies = [
"generic-array",
]
[[package]]
name = "instant"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
dependencies = [
"cfg-if",
]
[[package]]
name = "io-lifetimes"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2"
dependencies = [
"hermit-abi 0.3.9",
"libc",
"windows-sys 0.48.0",
]
[[package]]
name = "ipnet"
version = "2.9.0"
@@ -1391,7 +1659,7 @@ version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b"
dependencies = [
"hermit-abi",
"hermit-abi 0.3.9",
"libc",
"windows-sys 0.52.0",
]
@@ -1429,6 +1697,15 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "kv-log-macro"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f"
dependencies = [
"log",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@@ -1451,6 +1728,12 @@ dependencies = [
"libc",
]
[[package]]
name = "linux-raw-sys"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519"
[[package]]
name = "linux-raw-sys"
version = "0.4.13"
@@ -1472,6 +1755,58 @@ name = "log"
version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
dependencies = [
"value-bag",
]
[[package]]
name = "mailin"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff08fefa0913a8fb1cbd31bd83a4841db8b6eb78320d517b31fcf60e2541d57a"
dependencies = [
"base64-compat",
"either",
"log",
"nom",
"ternop",
]
[[package]]
name = "mailin-embedded"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "143ac497c8c38d22407344e2252767ffd8253593a3d2464acc8619c8ef15835c"
dependencies = [
"bufstream-fresh",
"cfg-if",
"log",
"mailin",
"rustls 0.22.3",
"rustls-pemfile 2.1.1",
"scoped_threadpool",
]
[[package]]
name = "mailnoti"
version = "0.6.3-rc.3"
dependencies = [
"anyhow",
"async-std",
"clap",
"env_logger 0.10.2",
"get_if_addrs",
"lazy_static",
"log",
"mailin-embedded",
"neolink_core",
"once_cell",
"regex",
"serde",
"tokio",
"toml",
"validator",
]
[[package]]
name = "md5"
@@ -1550,10 +1885,9 @@ dependencies = [
[[package]]
name = "neolink"
version = "0.6.3-rc.2"
version = "0.6.3-rc.3"
dependencies = [
"anyhow",
"async-stream",
"base64 0.22.0",
"byte-slice-cast",
"bytes",
@@ -1561,7 +1895,7 @@ dependencies = [
"crossbeam-channel",
"dirs",
"env_logger 0.11.3",
"fcm-push-listener",
"fcm-push-listener 2.0.3",
"futures",
"gstreamer",
"gstreamer-app",
@@ -1583,12 +1917,12 @@ dependencies = [
"tokio-util",
"toml",
"uuid",
"validator 0.18.1",
"validator",
]
[[package]]
name = "neolink_core"
version = "0.6.3-rc.2"
version = "0.6.3-rc.3"
dependencies = [
"aes",
"assert_matches",
@@ -1619,7 +1953,7 @@ dependencies = [
[[package]]
name = "neolink_decoder"
version = "0.6.3-rc.2"
version = "0.6.3-rc.3"
dependencies = [
"aes",
"anyhow",
@@ -1682,7 +2016,7 @@ version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi",
"hermit-abi 0.3.9",
"libc",
]
@@ -1769,6 +2103,12 @@ dependencies = [
"paste",
]
[[package]]
name = "parking"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae"
[[package]]
name = "parking_lot"
version = "0.12.1"
@@ -1826,12 +2166,54 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "piper"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96c8c490f422ef9a4efd2cb5b42b76c8613d7e7dfc1caf667b8a3350a5acc066"
dependencies = [
"atomic-waker",
"fastrand 2.0.2",
"futures-io",
]
[[package]]
name = "pkg-config"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
[[package]]
name = "polling"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce"
dependencies = [
"autocfg",
"bitflags 1.3.2",
"cfg-if",
"concurrent-queue",
"libc",
"log",
"pin-project-lite",
"windows-sys 0.48.0",
]
[[package]]
name = "polling"
version = "3.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3ed00ed3fbf728b5816498ecd316d1716eecaced9c0c8d2c5a6740ca214985b"
dependencies = [
"cfg-if",
"concurrent-queue",
"hermit-abi 0.4.0",
"pin-project-lite",
"rustix 0.38.32",
"tracing",
"windows-sys 0.52.0",
]
[[package]]
name = "powerfmt"
version = "0.2.0"
@@ -1952,21 +2334,21 @@ dependencies = [
[[package]]
name = "pushnoti"
version = "0.6.3-rc.2"
version = "0.6.3-rc.3"
dependencies = [
"anyhow",
"clap",
"env_logger 0.10.2",
"fcm-push-listener",
"fcm-push-listener 2.0.3",
"lazy_static",
"log",
"neolink_core",
"once_cell",
"regex",
"serde",
"tokio",
"toml",
"validator 0.16.1",
"validator_derive 0.16.0",
"validator",
]
[[package]]
@@ -2187,6 +2569,20 @@ version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustix"
version = "0.37.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea8ca367a3a01fe35e6943c400addf443c0f57670e6ec51196f71a4b8762dd2"
dependencies = [
"bitflags 1.3.2",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys 0.3.8",
"windows-sys 0.48.0",
]
[[package]]
name = "rustix"
version = "0.38.32"
@@ -2196,7 +2592,7 @@ dependencies = [
"bitflags 2.6.0",
"errno",
"libc",
"linux-raw-sys",
"linux-raw-sys 0.4.13",
"windows-sys 0.52.0",
]
@@ -2290,6 +2686,12 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "scoped_threadpool"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8"
[[package]]
name = "scopeguard"
version = "1.2.0"
@@ -2477,6 +2879,16 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c"
[[package]]
name = "socket2"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d"
dependencies = [
"libc",
"winapi 0.3.9",
]
[[package]]
name = "socket2"
version = "0.5.6"
@@ -2595,8 +3007,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1"
dependencies = [
"cfg-if",
"fastrand",
"rustix",
"fastrand 2.0.2",
"rustix 0.38.32",
"windows-sys 0.52.0",
]
@@ -2609,6 +3021,12 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "ternop"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d4ae32d0a4605a89c28534371b056919c12e7a070ee07505af75130ff030111"
[[package]]
name = "textwrap"
version = "0.15.2"
@@ -2722,7 +3140,7 @@ dependencies = [
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"socket2 0.5.6",
"tokio-macros",
"tracing",
"windows-sys 0.48.0",
@@ -2941,7 +3359,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633"
dependencies = [
"form_urlencoded",
"idna 0.5.0",
"idna",
"percent-encoding",
]
@@ -2973,51 +3391,20 @@ dependencies = [
"syn 2.0.72",
]
[[package]]
name = "validator"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b92f40481c04ff1f4f61f304d61793c7b56ff76ac1469f1beb199b1445b253bd"
dependencies = [
"idna 0.4.0",
"lazy_static",
"regex",
"serde",
"serde_derive",
"serde_json",
"url",
]
[[package]]
name = "validator"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db79c75af171630a3148bd3e6d7c4f42b6a9a014c2945bc5ed0020cbb8d9478e"
dependencies = [
"idna 0.5.0",
"idna",
"once_cell",
"regex",
"serde",
"serde_derive",
"serde_json",
"url",
"validator_derive 0.18.1",
]
[[package]]
name = "validator_derive"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc44ca3088bb3ba384d9aecf40c6a23a676ce23e09bdaca2073d99c207f864af"
dependencies = [
"if_chain",
"lazy_static",
"proc-macro-error",
"proc-macro2",
"quote",
"regex",
"syn 1.0.109",
"validator_types",
"validator_derive",
]
[[package]]
@@ -3035,14 +3422,10 @@ dependencies = [
]
[[package]]
name = "validator_types"
version = "0.16.0"
name = "value-bag"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "111abfe30072511849c5910134e8baf8dc05de4c0e5903d681cbd5c9c4d611e3"
dependencies = [
"proc-macro2",
"syn 1.0.109",
]
checksum = "5a84c137d37ab0142f0f2ddfe332651fdbf252e7b7dbb4e67b6c1f1b2e925101"
[[package]]
name = "vcpkg"
@@ -3062,6 +3445,12 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "waker-fn"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "317211a0dc0ceedd78fb2ca9a44aed3d7b9b26f81870d485c07122b4350673b7"
[[package]]
name = "want"
version = "0.3.1"
@@ -3181,7 +3570,7 @@ dependencies = [
"either",
"home",
"once_cell",
"rustix",
"rustix 0.38.32",
]
[[package]]
+3 -5
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink"
description = "A standards-compliant bridge to Reolink IP cameras"
version = "0.6.3-rc.2"
version = "0.6.3-rc.3"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
@@ -13,7 +13,6 @@ members = [
[dependencies]
anyhow = "1.0.70"
async-stream = {version = "0.3.5", optional = true}
base64 = "0.22.0"
byte-slice-cast = {version = "1.2.2", optional = true}
bytes = "1.6.0"
@@ -30,7 +29,7 @@ gstreamer-rtsp-server = { version = "0.23.0", features = ["v1_20"], optional = t
heck = "0.5.0"
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
md5 = {version = "0.7.0", optional = true}
neolink_core = { path = "crates/core", version = "0.6.3-rc.2" }
neolink_core = { path = "crates/core", version = "0.6.3-rc.3" }
once_cell = "1.19.0"
quick-xml = { version = "0.36.1", features = ["serialize"] }
regex = "1.7.3"
@@ -54,7 +53,6 @@ gstreamer = [
"dep:gstreamer-app",
"dep:gstreamer-rtsp",
"dep:gstreamer-rtsp-server",
"dep:async-stream",
"dep:byte-slice-cast",
"dep:crossbeam-channel"
]
@@ -62,4 +60,4 @@ pushnoti = [
"dep:fcm-push-listener",
"dep:dirs",
"dep:md5"
]
]
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink_core"
description = "Core services and structure for Reolink IP cameras"
version = "0.6.3-rc.2"
version = "0.6.3-rc.3"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
+10
View File
@@ -36,6 +36,10 @@ pub const MSG_ID_MOTION: u32 = 33;
pub const MSG_ID_SET_SERVICE_PORTS: u32 = 36;
/// Get service ports
pub const MSG_ID_GET_SERVICE_PORTS: u32 = 37;
/// Get Email setting
pub const MSG_ID_GET_EMAIL: u32 = 42;
/// Set email settings
pub const MSG_ID_SET_EMAIL: u32 = 43;
/// Version messages have this ID
pub const MSG_ID_VERSION: u32 = 80;
/// Ping messages have this ID
@@ -50,6 +54,8 @@ pub const MSG_ID_SNAP: u32 = 109;
pub const MSG_ID_UID: u32 = 114;
/// Used to pass the token and client ID for push notifications
pub const MSG_ID_PUSH_INFO: u32 = 124;
/// Send a test email configuration
pub const MSG_ID_TEST_EMAIL: u32 = 141;
/// StreamInfoList messages have this ID
pub const MSG_ID_STREAM_INFO_LIST: u32 = 146;
/// Used to get the abilities of a user
@@ -70,6 +76,10 @@ pub const MSG_ID_SET_LED_STATUS: u32 = 209;
pub const MSG_ID_GET_PIR_ALARM: u32 = 212;
/// Setting PIR status messages have this ID
pub const MSG_ID_START_PIR_ALARM: u32 = 213;
/// Set Email Task
pub const MSG_ID_SET_EMAIL_TASK: u32 = 216;
/// Get Email Task
pub const MSG_ID_GET_EMAIL_TASK: u32 = 217;
/// UDP Keep alive
pub const MSG_ID_UDP_KEEP_ALIVE: u32 = 234;
/// Battery message initiaed by the camera
+92 -4
View File
@@ -132,6 +132,12 @@ pub struct BcXml {
/// For changing rtmp server port
#[serde(rename = "OnvifPort", skip_serializing_if = "Option::is_none")]
pub onvif_port: Option<OnvifPort>,
/// Email for setting the email notifications
#[serde(rename = "Email", skip_serializing_if = "Option::is_none")]
pub email: Option<Email>,
/// EmailTask for turning the email notifications on/off
#[serde(rename = "EmailTask", skip_serializing_if = "Option::is_none")]
pub email_task: Option<EmailTask>,
}
impl BcXml {
@@ -490,8 +496,9 @@ pub struct TimeBlockList {
pub struct TimeBlock {
/// Whether to enable or disable for this time block
pub enable: u8,
/// The day of the week for this block
pub weekDay: String,
/// The day of the week for this block, Monday, Tuesday, Etc
#[serde(rename = "weekDay")]
pub week_day: String,
/// Time to start this block
#[serde(rename = "beginHour")]
pub begin_hour: u8,
@@ -930,7 +937,7 @@ pub struct FloodlightTask {
#[serde(skip_serializing_if = "Option::is_none")]
pub brightness_min: Option<u32>,
/// Schedule fot auto floodlight
pub schedule: Schedule,
pub schedule: ScheduleFloodLight,
/// Threshold settings for light sensor to consider nightime
#[serde(rename = "lightSensThreshold")]
pub light_sens_threshold: LightSensThreshold,
@@ -947,7 +954,7 @@ pub struct FloodlightTask {
/// Schedule for Floodlight Task
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
pub struct Schedule {
pub struct ScheduleFloodLight {
/// startHour
#[serde(rename = "startHour")]
pub start_hour: u32,
@@ -1478,6 +1485,87 @@ pub struct OnvifPort {
pub enable: Option<u32>,
}
/// Email settings for notificaitons
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize, Clone)]
pub struct Email {
/// XML Version
#[serde(rename = "@version")]
pub version: String,
/// SMTP server address
#[serde(rename = "smtpServer")]
pub smtp_server: String,
/// SMTP username
#[serde(rename = "userName")]
pub user_name: String,
/// SMTP password
pub password: String,
/// Send email address
pub address1: String,
/// Send email address can be empty
pub address2: String,
/// Send email address can be empty
pub address3: String,
/// 465
#[serde(rename = "smtpPort")]
pub smtp_port: u16,
/// Name of recipient to use on the email
#[serde(rename = "sendNickname")]
pub send_nickname: String,
/// Observed value `1`
pub attachment: u8,
/// Observed value `picture`, `video`
#[serde(rename = "attachmentType", skip_serializing_if = "Option::is_none")]
pub attachment_type: Option<String>,
/// Observed value `withText`
#[serde(rename = "textType")]
pub text_type: String,
/// Observed value `1`
pub ssl: u8,
/// Observed value `30`
pub interval: u32,
/// Max length of message. Observed value `127`
/// Read Only
#[serde(rename = "senderMaxLen", skip_serializing_if = "Option::is_none")]
pub sender_max_len: Option<u32>,
}
/// EmailTask settings that controls the times/enables the email
/// notifications
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
pub struct EmailTask {
/// XML Version
#[serde(rename = "@version")]
pub version: String,
/// Channel number
#[serde(rename = "channelId")]
pub channel_id: u8,
/// 1 for enable 0 for disable
#[serde(rename = "enable")]
pub enable: u8,
/// The list of schedule to turn on/off the email notifications
#[serde(rename = "ScheduleList", skip_serializing_if = "Option::is_none")]
pub schedule_list: Option<ScheduleList>,
}
/// List of schedule items for turning on/off the notifications
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
pub struct ScheduleList {
/// List of schedules
#[serde(rename = "Schedule")]
pub schedule: Schedule,
}
/// Schedule item for turning on/off the notifications
#[derive(PartialEq, Eq, Default, Debug, Deserialize, Serialize)]
pub struct Schedule {
/// The alarm type. Observed values: `MD`
#[serde(rename = "alarmType")]
pub alarm_type: String,
/// The list of time blocks
#[serde(rename = "timeBlockList")]
pub time_block_list: TimeBlockList,
}
/// Convience function to return the xml version used throughout the library
pub fn xml_ver() -> String {
"1.1".to_string()
+1
View File
@@ -16,6 +16,7 @@ mod abilityinfo;
mod battery;
mod connection;
mod credentials;
mod email;
mod errors;
mod floodlight;
mod keepalive;
@@ -1,6 +1,7 @@
use super::DiscoveryResult;
use crate::bc::codex::BcCodex;
use crate::bc::model::*;
use crate::bc_protocol::errors::BcUdpDropReciverKind;
use crate::bcudp::codex::BcUdpCodex;
use crate::bcudp::{model::*, xml::*};
use crate::{Credentials, Error, Result};
@@ -354,10 +355,10 @@ impl UdpPayloadInner {
}
packet = inner.next() => {
log::trace!("Cam->App");
let packet = packet.ok_or(Error::BcUdpDropReciver)??;
let packet = packet.ok_or(Error::BcUdpDropReciver(BcUdpDropReciverKind::NoneRecieved))??;
recv_timeout.as_mut().reset(Instant::now() + Duration::from_secs(TIME_OUT));
// let packet = socket_rx.next().await.ok_or(Error::BcUdpDropReciver)??;
socket_out_tx.try_send(packet).map_err(|_| Error::BcUdpDropReciver)?;
socket_out_tx.try_send(packet).map_err(|e| Error::BcUdpDropReciver(BcUdpDropReciverKind::SendFailed(format!("{e:?}"))))?;
continue;
},
packet = socket_in_rx.next() => {
@@ -509,7 +510,7 @@ impl UdpPayloadInner {
log::trace!("Camera->App");
// Incomming from socket
// Outgoing to application
let (item, addr) = v.ok_or(Error::BcUdpDropReciver)?;
let (item, addr) = v.ok_or(Error::BcUdpDropReciver(BcUdpDropReciverKind::NoneRecieved))?;
if addr == camera_addr {
match item {
BcUdp::Discovery(UdpDiscovery{
+264
View File
@@ -0,0 +1,264 @@
//! Email controlling methods
//!
use super::{BcCamera, Error, Result};
use crate::bc::{model::*, xml::*};
impl BcCamera {
/// Get the current Email XML
pub async fn get_email(&self) -> Result<Email> {
let connection = self.get_connection();
let msg_num = self.new_message_num();
let mut sub_get = connection.subscribe(MSG_ID_GET_EMAIL, msg_num).await?;
let get = Bc {
meta: BcMeta {
msg_id: MSG_ID_GET_EMAIL,
channel_id: self.channel_id,
msg_num,
response_code: 0,
stream_type: 0,
class: 0x6414,
},
body: BcBody::ModernMsg(ModernMsg {
extension: None,
payload: None,
}),
};
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavailable {
id: msg.meta.msg_id,
code: msg.meta.response_code,
});
}
if let BcBody::ModernMsg(ModernMsg {
payload:
Some(BcPayloads::BcXml(BcXml {
email: Some(email), ..
})),
..
}) = msg.body
{
Ok(email)
} else {
Err(Error::UnintelligibleReply {
reply: std::sync::Arc::new(Box::new(msg)),
why: "Expected Email xml but it was not recieved",
})
}
}
/// Set the Email XML
pub async fn set_email(&self, email: Email) -> Result<()> {
let connection = self.get_connection();
let msg_num = self.new_message_num();
let mut sub_get = connection.subscribe(MSG_ID_SET_EMAIL, msg_num).await?;
let get = Bc {
meta: BcMeta {
msg_id: MSG_ID_SET_EMAIL,
channel_id: self.channel_id,
msg_num,
response_code: 0,
stream_type: 0,
class: 0x6414,
},
body: BcBody::ModernMsg(ModernMsg {
extension: None,
payload: Some(BcPayloads::BcXml(BcXml {
email: Some(email),
..Default::default()
})),
}),
};
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavailable {
id: msg.meta.msg_id,
code: msg.meta.response_code,
});
}
Ok(())
}
/// Test the Email with this XML
pub async fn test_email(&self, email: Email) -> Result<()> {
let connection = self.get_connection();
let msg_num = self.new_message_num();
let mut sub_get = connection.subscribe(MSG_ID_TEST_EMAIL, msg_num).await?;
let get = Bc {
meta: BcMeta {
msg_id: MSG_ID_TEST_EMAIL,
channel_id: self.channel_id,
msg_num,
response_code: 0,
stream_type: 0,
class: 0x6414,
},
body: BcBody::ModernMsg(ModernMsg {
extension: None,
payload: Some(BcPayloads::BcXml(BcXml {
email: Some(email),
..Default::default()
})),
}),
};
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavailable {
id: msg.meta.msg_id,
code: msg.meta.response_code,
});
}
Ok(())
}
/// Get the current EmailTask XML
pub async fn get_email_task(&self) -> Result<EmailTask> {
let connection = self.get_connection();
let msg_num = self.new_message_num();
let mut sub_get = connection.subscribe(MSG_ID_GET_EMAIL_TASK, msg_num).await?;
let get = Bc {
meta: BcMeta {
msg_id: MSG_ID_GET_EMAIL_TASK,
channel_id: self.channel_id,
msg_num,
response_code: 0,
stream_type: 0,
class: 0x6414,
},
body: BcBody::ModernMsg(ModernMsg {
extension: Some(Extension {
channel_id: Some(self.channel_id),
..Default::default()
}),
payload: None,
}),
};
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavailable {
id: msg.meta.msg_id,
code: msg.meta.response_code,
});
}
if let BcBody::ModernMsg(ModernMsg {
payload:
Some(BcPayloads::BcXml(BcXml {
email_task: Some(email_task),
..
})),
..
}) = msg.body
{
Ok(email_task)
} else {
Err(Error::UnintelligibleReply {
reply: std::sync::Arc::new(Box::new(msg)),
why: "Expected EmailTask xml but it was not recieved",
})
}
}
/// Setup the Email Task
pub async fn set_email_task(&self, email_task: EmailTask) -> Result<()> {
let connection = self.get_connection();
let msg_num = self.new_message_num();
let mut sub_get = connection.subscribe(MSG_ID_SET_EMAIL_TASK, msg_num).await?;
let get = Bc {
meta: BcMeta {
msg_id: MSG_ID_SET_EMAIL_TASK,
channel_id: self.channel_id,
msg_num,
response_code: 0,
stream_type: 0,
class: 0x6414,
},
body: BcBody::ModernMsg(ModernMsg {
extension: Some(Extension {
channel_id: Some(self.channel_id),
..Default::default()
}),
payload: Some(BcPayloads::BcXml(BcXml {
email_task: Some(email_task),
..Default::default()
})),
}),
};
sub_get.send(get).await?;
let msg = sub_get.recv().await?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavailable {
id: msg.meta.msg_id,
code: msg.meta.response_code,
});
}
Ok(())
}
/// Turn on Email notifications
pub async fn email_on(&self) -> Result<()> {
self.set_email_task(EmailTask {
version: xml_ver(),
channel_id: self.channel_id,
enable: 1,
schedule_list: None,
})
.await
}
/// Turn off Email notifications
pub async fn email_off(&self) -> Result<()> {
self.set_email_task(EmailTask {
version: xml_ver(),
channel_id: self.channel_id,
enable: 0,
schedule_list: None,
})
.await
}
/// Turn on Email notifications all the time
pub async fn email_on_always(&self) -> Result<()> {
const DOW: [&str; 7] = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
self.set_email_task(EmailTask {
version: xml_ver(),
channel_id: self.channel_id,
enable: 1,
schedule_list: Some(ScheduleList {
schedule: Schedule {
alarm_type: "MD".to_owned(),
time_block_list: TimeBlockList {
time_block: DOW
.iter()
.map(|d| TimeBlock {
enable: 1,
week_day: d.to_string(),
begin_hour: 0,
end_hour: 23,
})
.collect(),
},
},
}),
})
.await
}
}
+8 -2
View File
@@ -89,8 +89,8 @@ pub enum Error {
BcUdpTimeout,
/// Raised when a BcUdp incomming connection is dropped
#[error("BcUDP reciver dropped")]
BcUdpDropReciver,
#[error("BcUDP reciver dropped: {0:?}")]
BcUdpDropReciver(BcUdpDropReciverKind),
/// Raised when a BcUdp outgoing connection is dropped
#[error("BcUDP sender dropped")]
@@ -236,6 +236,12 @@ pub enum Error {
OtherString(String),
}
#[derive(Debug, Clone)]
pub enum BcUdpDropReciverKind {
NoneRecieved,
SendFailed(String),
}
impl From<std::io::Error> for Error {
fn from(k: std::io::Error) -> Self {
// Check for other error that is already an Error of this type
+2 -1
View File
@@ -410,7 +410,8 @@ impl BcCamera {
// Chunks are still being played, while talk_stop will interrupt them. Wait until we expect
// the stream to end (+ and extra 100ms) before issuing talk_stop.
let remaining_stream_duration = expected_stream_end - std::time::Instant::now();
std::thread::sleep(remaining_stream_duration + std::time::Duration::from_secs_f32(0.1));
tokio::time::sleep(remaining_stream_duration + std::time::Duration::from_secs_f32(0.1))
.await;
self.talk_stop().await?;
+62 -1
View File
@@ -108,7 +108,7 @@ pub(super) const MAGIC_HEADER_BCMEDIA_IFRAME: u32 = 0x63643030;
pub(super) const MAGIC_HEADER_BCMEDIA_IFRAME_LAST: u32 = 0x63643039;
/// Video Types for I/PFrame
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum VideoType {
/// H264 video data
H264,
@@ -205,6 +205,50 @@ pub struct BcMediaAac {
pub data: Vec<u8>,
}
impl BcMediaAac {
/// Read the ADTS header to learn the duration in micro secs
pub fn duration(&self) -> Option<u32> {
if self.data.len() < 8 {
// Too small for the header
return None;
}
if self.data[0] != 0b11111111 {
// Syncword incorrect
return None;
}
if (self.data[1] & 0b11110000) != 0b11110000 {
// Syncword incorrect
return None;
}
let frequency_index = (self.data[2] & 0b00111100) >> 2;
let sample_frequency = match frequency_index {
0 => Some(96000u32),
1 => Some(88200u32),
2 => Some(64000u32),
3 => Some(48000u32),
4 => Some(44100u32),
5 => Some(32000u32),
6 => Some(24000u32),
7 => Some(22050u32),
8 => Some(16000u32),
9 => Some(12000u32),
10 => Some(11025u32),
11 => Some(8000u32),
12 => Some(7350u32),
_ => None,
}?;
log::trace!("sample_frequency: {sample_frequency}");
let frames = (self.data[6] & 0b00000011) + 1;
log::trace!("frames: {frames}");
let samples = frames as u32 * 1024;
log::trace!("samples: {samples}");
const MICROSECONDS: u32 = 1000000;
let duration = samples * MICROSECONDS / sample_frequency;
Some(duration)
}
}
pub(super) const MAGIC_HEADER_BCMEDIA_ADPCM: u32 = 0x62773130;
pub(super) const MAGIC_HEADER_BCMEDIA_ADPCM_DATA: u16 = 0x0100;
@@ -231,3 +275,20 @@ pub struct BcMediaAdpcm {
/// To calculate the block-align size simply remove 4 from the `len()`
pub data: Vec<u8>,
}
impl BcMediaAdpcm {
/// The block size, this is bytes without the block header
pub fn block_size(&self) -> u32 {
self.data.len() as u32 - 4
}
/// Returns duration in micro seconds;
pub fn duration(&self) -> Option<u32> {
let samples = self.block_size() * 2;
// Always 8000Hz for ADPCM
const SAMPLE_FREQUENCY: u32 = 8000;
const MICROSECONDS: u32 = 1000000;
let duration = samples * MICROSECONDS / SAMPLE_FREQUENCY;
Some(duration)
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink_decoder"
description = "A cli decoder for the AES encrypted packets"
version = "0.6.3-rc.2"
version = "0.6.3-rc.3"
authors = ["Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
+22
View File
@@ -0,0 +1,22 @@
[package]
name = "mailnoti"
version = "0.6.3-rc.3"
edition = "2021"
[dependencies]
anyhow = "1.0.71"
async-std = "1.12.0"
clap = { version = "4.2.2", features = ["derive", "cargo"] }
env_logger = "0.10.0"
get_if_addrs = "0.5.3"
lazy_static = "1.4.0"
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
mailin-embedded = "0.8.2"
neolink_core = { path = "../core", version = "0.6.3-rc.3" }
once_cell = "1.19.0"
regex = "1.8.1"
serde = "1.0.163"
tokio = { version = "1.28.1", features = ["full"] }
toml = "0.8.2"
validator = {version="0.18.1", features = ["derive"] }
+6
View File
@@ -0,0 +1,6 @@
# Reolink Mail Notifications
Because google FCM has removed the push notification api we use
we now must resort to email notifications for PIR messages
This is a test crate to investigate this
+67
View File
@@ -0,0 +1,67 @@
use neolink_core::bc_protocol::DiscoveryMethods;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Deserialize;
use std::clone::Clone;
use validator::{Validate, ValidationError};
static RE_MAXENC_SRC: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap()
});
#[derive(Debug, Deserialize, Validate, Clone)]
pub(crate) struct Config {
#[validate(nested)]
pub(crate) cameras: Vec<CameraConfig>,
}
#[derive(Debug, Deserialize, Validate, Clone)]
#[validate(schema(function = "validate_camera_config"))]
pub(crate) struct CameraConfig {
pub(crate) name: String,
#[serde(rename = "address")]
pub(crate) camera_addr: Option<String>,
#[serde(rename = "uid")]
pub(crate) camera_uid: Option<String>,
pub(crate) username: String,
pub(crate) password: Option<String>,
#[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))]
#[serde(default = "default_channel_id", alias = "channel")]
pub(crate) channel_id: u8,
#[serde(default = "default_discovery")]
pub(crate) discovery: DiscoveryMethods,
#[serde(default = "default_maxenc")]
#[validate(regex(
path = *RE_MAXENC_SRC,
message = "Invalid maximum encryption method",
code = "max_encryption"
))]
pub(crate) max_encryption: String,
}
fn default_discovery() -> DiscoveryMethods {
DiscoveryMethods::Relay
}
fn default_maxenc() -> String {
"Aes".to_string()
}
fn default_channel_id() -> u8 {
0
}
fn validate_camera_config(camera_config: &CameraConfig) -> Result<(), ValidationError> {
match (&camera_config.camera_addr, &camera_config.camera_uid) {
(None, None) => Err(ValidationError::new(
"Either camera address or uid must be given",
)),
_ => Ok(()),
}
}
+189
View File
@@ -0,0 +1,189 @@
//! Main test app for mail
use anyhow::{anyhow, Context, Result};
use clap::Parser;
use neolink_core::{
bc::xml::{xml_ver, Email},
bc_protocol::BcCamera,
};
use std::{
fs,
net::{IpAddr, SocketAddr},
};
use validator::Validate;
mod config;
mod opt;
mod utils;
use config::Config;
use opt::Opt;
use utils::find_and_connect;
#[tokio::main]
async fn main() -> Result<()> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let opt = Opt::parse();
let conf_path = opt.config.context("Must supply --config file")?;
let config: Config = toml::from_str(
&fs::read_to_string(&conf_path)
.with_context(|| format!("Failed to read {:?}", conf_path))?,
)
.with_context(|| format!("Failed to parse the {:?} config file", conf_path))?;
config
.validate()
.with_context(|| format!("Failed to validate the {:?} config file", conf_path))?;
let name = opt.camera.clone();
let camera = find_and_connect(&config, &opt.camera).await?;
const MAIL_PORT: u16 = 22022;
// let addr = SocketAddr::from((get_local_ip()?, MAIL_PORT));
let cam_addr = SocketAddr::from((IpAddr::from([192, 168, 1, 201]), MAIL_PORT));
let post_addr = SocketAddr::from((IpAddr::from([127, 0, 0, 1]), MAIL_PORT));
tokio::select! {
v = cam_tasks(&name, camera, cam_addr) => v,
v = mail_server(&name, post_addr) => v,
}?;
Ok(())
}
fn get_local_ip() -> Result<std::net::IpAddr> {
get_if_addrs::get_if_addrs()?
.iter()
.find(|i| !i.is_loopback() && matches!(i.addr, get_if_addrs::IfAddr::V4(_)))
.map(|iface| Ok(iface.ip()))
.unwrap_or_else(|| Err(anyhow!("No Local Ip Address Found")))
}
async fn cam_tasks(name: &str, camera: BcCamera, addr: SocketAddr) -> Result<()> {
let support = camera.get_support().await?;
if support.email.is_some_and(|v| v > 0) {
let ip = addr.ip();
let port = addr.port();
log::info!("Telling camera to send the mail to {}:{}", ip, port);
let mail_settings = Email {
version: xml_ver(),
smtp_server: format!("{}", ip),
user_name: format!("{name}@neolink.neolink"),
password: "TestPass".to_owned(),
address1: "neolink@neolink.neolink".to_owned(),
address2: "".to_owned(),
address3: "".to_owned(),
smtp_port: addr.port(),
send_nickname: name.to_string(),
attachment: 1,
attachment_type: Some("picture".to_owned()),
text_type: "withText".to_owned(),
ssl: 0,
interval: 30,
sender_max_len: None,
};
camera.set_email(mail_settings.clone()).await?;
camera.email_on_always().await?;
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
log::info!("Sending test email");
camera.test_email(mail_settings).await?;
// Wait to give me time to wave at the camera please
tokio::time::sleep(tokio::time::Duration::from_secs(300)).await;
Ok(())
} else {
Err(anyhow::anyhow!(
"Camera does not support email notfications"
))
}
}
use mailin_embedded::{
response::{self, Response},
Handler, Server, SslConfig,
};
use regex::Regex;
#[derive(Clone)]
struct MailHandler {
name: String,
}
impl Handler for MailHandler {
fn helo(&mut self, _ip: IpAddr, _domain: &str) -> Response {
log::debug!("HELO");
response::OK
}
fn rcpt(&mut self, to: &str) -> Response {
log::debug!("rcpr:: {to}");
let re = Regex::new(r"^(.*)@neolink.neolink$").unwrap();
if re.is_match(to) {
response::OK
} else {
response::NO_MAILBOX
}
}
fn mail(&mut self, ip: IpAddr, domain: &str, from: &str) -> Response {
log::debug!("mail:: ip: {ip:?}, domain: {domain}, from: {from}");
response::OK
}
fn data_start(&mut self, domain: &str, from: &str, is8bit: bool, to: &[String]) -> Response {
log::debug!("data_start:: domain: {domain}, from: {from}, is8bit: {is8bit}, to: {to:?}");
response::OK
}
fn data(&mut self, buf: &[u8]) -> std::io::Result<()> {
let text = String::from_utf8_lossy(buf);
log::debug!("data:: text: {text}");
Ok(())
}
fn data_end(&mut self) -> Response {
log::debug!("data_end::");
response::OK
}
fn auth_login(&mut self, username: &str, password: &str) -> Response {
log::debug!("auth_login;: username: {username}, password: {password}");
let correct_username = format!("{}@neolink.neolink", self.name);
let correct_password = "TestPass";
if username == correct_username && password == correct_password {
response::AUTH_OK
} else {
response::INVALID_CREDENTIALS
}
}
fn auth_plain(
&mut self,
authorization_id: &str,
authentication_id: &str,
password: &str,
) -> Response {
log::debug!("auth_plain:: authorization_id: {authorization_id}, authentication_id: {authentication_id}, password: {password}");
response::INVALID_CREDENTIALS
}
}
async fn mail_server(name: &str, addr: SocketAddr) -> Result<()> {
let handler = MailHandler {
name: name.to_string(),
};
let mut server = Server::new(handler);
server
.with_name("neolink.neolink")
.with_ssl(SslConfig::None)
.map_err(|e| anyhow!("{e:?}"))?
.with_addr(addr)
.map_err(|e| anyhow!("{e:?}"))?;
tokio::task::spawn_blocking(move || server.serve().map_err(|e| anyhow!("{e:?}"))).await??;
Ok(())
}
+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,
}
+155
View File
@@ -0,0 +1,155 @@
//! Contains code that is not specific to any of the subcommands
//!
use log::*;
use super::config::{CameraConfig, Config};
use anyhow::{anyhow, Context, Error, Result};
use neolink_core::bc_protocol::{
BcCamera, BcCameraOpt, ConnectionProtocol, Credentials, DiscoveryMethods, MaxEncryption,
};
use std::{
fmt::{Display, Error as FmtError, Formatter},
net::{IpAddr, ToSocketAddrs},
str::FromStr,
};
pub(crate) enum AddressOrUid {
Address(String),
#[allow(dead_code)]
Uid(String, DiscoveryMethods),
#[allow(dead_code)]
AddressWithUid(String, String, DiscoveryMethods),
}
impl Display for AddressOrUid {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match self {
AddressOrUid::AddressWithUid(addr, uid, _) => {
write!(f, "Address: {}, UID: {}", addr, uid)
}
AddressOrUid::Address(host) => write!(f, "Address: {}", host),
AddressOrUid::Uid(host, _) => write!(f, "UID: {}", host),
}
}
}
impl AddressOrUid {
// Created by translating the config fields directly
pub(crate) fn new(
address: &Option<String>,
uid: &Option<String>,
method: &DiscoveryMethods,
) -> Result<Self, Error> {
match (address, uid) {
(None, None) => Err(anyhow!("Neither address or uid given")),
(Some(host), Some(uid)) => Ok(AddressOrUid::AddressWithUid(
host.clone(),
uid.clone(),
*method,
)),
(Some(host), None) => Ok(AddressOrUid::Address(host.clone())),
(None, Some(host)) => Ok(AddressOrUid::Uid(host.clone(), *method)),
}
}
// Convience method to get the BcCamera with the appropiate method
// from a camera_config
pub(crate) async fn connect_camera(
&self,
camera_config: &CameraConfig,
) -> Result<BcCamera, Error> {
let (port, addrs) = {
if let Some(addr_str) = camera_config.camera_addr.as_ref() {
match addr_str.to_socket_addrs() {
Ok(addr_iter) => {
let mut port = None;
let mut ipaddrs = vec![];
for addr in addr_iter {
port = Some(addr.port());
ipaddrs.push(addr.ip());
}
Ok((port, ipaddrs))
}
Err(_) => match IpAddr::from_str(addr_str) {
Ok(ip) => Ok((None, vec![ip])),
Err(_) => Err(anyhow!("Could not parse address in config")),
},
}
} else {
Ok((None, vec![]))
}
}?;
let options = BcCameraOpt {
name: camera_config.name.clone(),
channel_id: camera_config.channel_id,
addrs,
port,
uid: camera_config.camera_uid.clone(),
protocol: ConnectionProtocol::TcpUdp,
discovery: camera_config.discovery,
max_discovery_retries: 10,
credentials: Credentials {
username: camera_config.username.clone(),
password: camera_config.password.clone(),
},
debug: false,
};
trace!("Camera Info: {:?}", options);
Ok(BcCamera::new(&options).await?)
}
}
pub(crate) async fn find_and_connect(config: &Config, name: &str) -> Result<BcCamera> {
let camera_config = find_camera_by_name(config, name)?;
connect_and_login(camera_config).await
}
pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result<BcCamera> {
let camera_addr = AddressOrUid::new(
&camera_config.camera_addr,
&camera_config.camera_uid,
&camera_config.discovery,
)
.unwrap();
info!(
"{}: Connecting to camera at {}",
camera_config.name, camera_addr
);
let camera = camera_addr
.connect_camera(camera_config)
.await
.with_context(|| {
format!(
"Failed to connect to camera {} at {} on channel {}",
camera_config.name, camera_addr, camera_config.channel_id
)
})?;
let max_encryption = match camera_config.max_encryption.to_lowercase().as_str() {
"none" => MaxEncryption::None,
"bcencrypt" => MaxEncryption::BcEncrypt,
"aes" => MaxEncryption::Aes,
_ => MaxEncryption::Aes,
};
info!("{}: Logging in", camera_config.name);
camera
.login_with_maxenc(max_encryption)
.await
.with_context(|| format!("Failed to login to {}", camera_config.name))?;
info!("{}: Connected and logged in", camera_config.name);
Ok(camera)
}
pub(crate) fn find_camera_by_name<'a>(config: &'a Config, name: &str) -> Result<&'a CameraConfig> {
config
.cameras
.iter()
.find(|c| c.name == name)
.ok_or_else(|| anyhow!("Camera {} not found in the config file", name))
}
+6 -5
View File
@@ -1,6 +1,6 @@
[package]
name = "pushnoti"
version = "0.6.3-rc.2"
version = "0.6.3-rc.3"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -9,13 +9,14 @@ edition = "2021"
anyhow = "1.0.71"
clap = { version = "4.2.2", features = ["derive", "cargo"] }
env_logger = "0.10.0"
fcm-push-listener = "2.0.1"
# fcm-push-listener = { path = "../fcm-push-listener" }
fcm-push-listener = { version = "2.0.3" }
lazy_static = "1.4.0"
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
neolink_core = { path = "../core", version = "0.6.3-rc.2" }
neolink_core = { path = "../core", version = "0.6.3-rc.3" }
once_cell = "1.19.0"
regex = "1.8.1"
serde = "1.0.163"
tokio = { version = "1.28.1", features = ["full"] }
toml = "0.8.2"
validator = "0.16.0"
validator_derive = "0.16.0"
validator = {version="0.18.1", features = ["derive"] }
+7 -13
View File
@@ -1,23 +1,17 @@
use lazy_static::lazy_static;
use neolink_core::bc_protocol::DiscoveryMethods;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Deserialize;
use std::clone::Clone;
use validator::{Validate, ValidationError};
use validator_derive::Validate;
lazy_static! {
static ref RE_STREAM_SRC: Regex =
Regex::new(r"^(mainStream|subStream|externStream|both|all)$").unwrap();
static ref RE_TLS_CLIENT_AUTH: Regex = Regex::new(r"^(none|request|require)$").unwrap();
static ref RE_PAUSE_MODE: Regex = Regex::new(r"^(black|still|test|none)$").unwrap();
static ref RE_MAXENC_SRC: Regex =
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap();
}
static RE_MAXENC_SRC: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap()
});
#[derive(Debug, Deserialize, Validate, Clone)]
pub(crate) struct Config {
#[validate]
#[validate(nested)]
pub(crate) cameras: Vec<CameraConfig>,
}
@@ -36,7 +30,7 @@ pub(crate) struct CameraConfig {
pub(crate) password: Option<String>,
#[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))]
#[serde(default = "default_channel_id")]
#[serde(default = "default_channel_id", alias = "channel")]
pub(crate) channel_id: u8,
#[serde(default = "default_discovery")]
@@ -44,7 +38,7 @@ pub(crate) struct CameraConfig {
#[serde(default = "default_maxenc")]
#[validate(regex(
path = "RE_MAXENC_SRC",
path = *RE_MAXENC_SRC,
message = "Invalid maximum encryption method",
code = "max_encryption"
))]
+12
View File
@@ -41,6 +41,11 @@ async fn main() -> Result<()> {
// into this program
// let registration = fcm_push_listener::register("263684512460").await?;
// let firebase_app_id = "1:743639030586:android:86f60a4fb7143876";
// let firebase_project_id = "reolink-login";
// let firebase_api_key = "AIzaSyBEUIuWHnnOEwFahxWgQB4Yt4NsgOmkPyE";
// let vapid_key = "";
let sender_id = "743639030586"; // andriod
// let sender_id = "696841269229"; // ios
@@ -56,6 +61,13 @@ async fn main() -> Result<()> {
} else {
info!("Registering new token");
let registration = fcm_push_listener::register(sender_id).await?;
// let registration = fcm_push_listener::register(
// firebase_app_id,
// firebase_project_id,
// firebase_api_key,
// vapid_key,
// )
// .await?;
let new_token = toml::to_string(&registration)?;
fs::write(token_path, new_token)?;
registration
+973 -658
View File
File diff suppressed because it is too large. Load diff
+204 -46
View File
@@ -5,13 +5,14 @@
//! The camera watch is used as an event to be triggered
//! whenever the camera is lost/updated
use anyhow::{anyhow, Context};
use futures::TryFutureExt;
use futures::{stream::FuturesUnordered, FutureExt, StreamExt, TryFutureExt};
use std::sync::{Arc, Weak};
#[cfg(feature = "pushnoti")]
use tokio::sync::watch::channel as watch;
use tokio::{
sync::{
mpsc::Sender as MpscSender, oneshot::channel as oneshot, watch::Receiver as WatchReceiver,
mpsc::Receiver as MpscReceiver, mpsc::Sender as MpscSender, oneshot::channel as oneshot,
watch::Receiver as WatchReceiver,
},
time::{sleep, Duration},
};
@@ -19,13 +20,13 @@ use tokio_util::sync::CancellationToken;
#[cfg(feature = "pushnoti")]
use super::PushNoti;
#[cfg(feature = "gstreamer")]
use super::StreamInstance;
use super::{MdState, NeoCamCommand, NeoCamThreadState, Permit};
use super::{MdState, NeoCamCommand, NeoCamThreadState, Permit, UseCounter};
use crate::{config::CameraConfig, AnyResult, Result};
use neolink_core::bc_protocol::BcCamera;
use neolink_core::{
bc_protocol::{BcCamera, StreamKind},
bcmedia::model::BcMedia,
};
#[cfg(feature = "gstreamer")]
use neolink_core::bc_protocol::StreamKind;
/// This instance is the primary interface used throughout the app
///
@@ -232,45 +233,6 @@ impl NeoInstance {
}
}
#[cfg(feature = "gstreamer")]
pub(crate) async fn stream(&self, name: StreamKind) -> Result<StreamInstance> {
let (instance_tx, instance_rx) = oneshot();
self.camera_control
.send(NeoCamCommand::Stream(name, instance_tx))
.await?;
Ok(instance_rx.await?)
}
#[cfg(feature = "gstreamer")]
#[allow(dead_code)]
pub(crate) async fn low_stream(&self) -> Result<Option<StreamInstance>> {
let (instance_tx, instance_rx) = oneshot();
self.camera_control
.send(NeoCamCommand::LowStream(instance_tx))
.await?;
Ok(instance_rx.await?)
}
#[cfg(feature = "gstreamer")]
#[allow(dead_code)]
pub(crate) async fn high_stream(&self) -> Result<Option<StreamInstance>> {
let (instance_tx, instance_rx) = oneshot();
self.camera_control
.send(NeoCamCommand::HighStream(instance_tx))
.await?;
Ok(instance_rx.await?)
}
#[cfg(feature = "gstreamer")]
#[allow(dead_code)]
pub(crate) async fn streams(&self) -> Result<Vec<StreamInstance>> {
let (instance_tx, instance_rx) = oneshot();
self.camera_control
.send(NeoCamCommand::Streams(instance_tx))
.await?;
Ok(instance_rx.await?)
}
#[cfg(feature = "pushnoti")]
pub(crate) async fn uid(&self) -> Result<String> {
let (reply_tx, reply_rx) = oneshot();
@@ -384,6 +346,202 @@ impl NeoInstance {
timeout,
}
}
/// Streams a camera source while not paused
pub(crate) async fn stream_while_live(
&self,
stream: StreamKind,
) -> AnyResult<MpscReceiver<BcMedia>> {
let config = self.config().await?.borrow().clone();
let name = config.name.clone();
let media_rx = if config.pause.on_motion {
let (media_tx, media_rx) = tokio::sync::mpsc::channel(100);
let counter = UseCounter::new().await;
let mut md = self.motion().await?;
let mut tasks = FuturesUnordered::new();
// Stream for 5s on a new client always
// This lets us negotiate the camera stream type
let init_permit = counter.create_activated().await?;
tokio::spawn(
async {
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
drop(init_permit);
}
.map(|e| {
log::debug!("Init permit thread stopped {e:?}");
e
}),
);
// Create the permit for controlling the motion
let mut md_permit = {
let md_state = md.borrow_and_update().clone();
match md_state {
MdState::Start(_) => {
log::info!("{name}::{stream:?}: Starting with Motion");
counter.create_activated().await?
}
MdState::Stop(_) | MdState::Unknown => {
log::info!("{name}::{stream:?}: Waiting with Motion");
counter.create_deactivated().await?
}
}
};
// Now listen to the motion
let thread_name = name.clone();
tasks.push(tokio::spawn(
async move {
loop {
match md.changed().await {
Ok(_) => {
let md_state: MdState = md.borrow_and_update().clone();
match md_state {
MdState::Start(_) => {
log::info!("{thread_name}::{stream:?}: Motion Started");
md_permit.activate().await?;
}
MdState::Stop(_) => {
log::info!("{thread_name}::{stream:?}: Motion Stopped");
md_permit.deactivate().await?;
}
MdState::Unknown => {}
}
}
Err(e) => {
// Use break here so we can define the full type on the async closure
break AnyResult::Err(e.into());
}
}
}?;
AnyResult::Ok(())
}
.map(|e| {
log::debug!("Motion thread stopped {e:?}");
e
}),
));
#[cfg(pushnoti)]
{
// Creates a permit for controlling based on the PN
let pn_permit = counter.create_deactivated().await?;
let mut pn = self.push_notifications().await?;
pn.borrow_and_update(); // Ignore any PNs that have already been sent before this
let thread_name = name.clone();
tasks.push(tokio::spawn(
async move {
loop {
let noti: Option<PushNoti> = pn.borrow_and_update().clone();
if let Some(noti) = noti {
if noti.message.contains("Motion Alert from") {
log::info!(
"{thread_name}::{stream:?}: Push Notification Recieved"
);
let mut new_pn_permit = pn_permit.subscribe();
new_pn_permit.activate().await?;
tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_secs(30))
.await;
drop(new_pn_permit);
});
}
}
if let Err(e) = pn.changed().await {
break Err(e);
}
}?;
AnyResult::Ok(())
}
.map(|e| {
log::debug!("PN thread stopped {e:?}");
e
}),
));
}
// Send the camera when the pemit is active
let camera_permit = counter.create_deactivated().await?;
let thread_camera = self.clone();
tokio::spawn(
async move {
loop {
if let Err(e) = camera_permit.aquired_users().await {
break AnyResult::Err(e);
}
log::debug!("Starting stream");
tokio::select! {
v = camera_permit.dropped_users() => {
log::debug!("Dropped users: {v:?}");
v
},
v = async {
log::debug!("Getting stream");
let mut stream = thread_camera.stream(stream).await?;
log::debug!("Got stream");
while let Some(media) = stream.recv().await {
media_tx.send(media).await?;
}
AnyResult::Ok(())
} => {
log::debug!("Stopped stream: {v:?}");
v
},
v = tasks.next() => {
log::debug!("Task failed: {v:?}");
Err(anyhow!("Task ended prematurly: {v:?}"))
}
}?;
log::debug!("Pausing stream");
}?;
drop(counter); // Make sure counter is owned by this thread
AnyResult::Ok(())
}
.map(|e| {
log::debug!("Stream thread stopped {e:?}");
e
}),
);
Ok(media_rx)
} else {
self.stream(stream).await
}?;
Ok(media_rx)
}
/// Streams a camera source
pub(crate) async fn stream(&self, stream: StreamKind) -> AnyResult<MpscReceiver<BcMedia>> {
let (media_tx, media_rx) = tokio::sync::mpsc::channel(100);
let config = self.config().await?.borrow().clone();
let strict = config.strict;
let thread_camera = self.clone();
tokio::task::spawn(
tokio::task::spawn(async move {
thread_camera
.run_task(move |cam| {
let media_tx = media_tx.clone();
Box::pin(async move {
let mut media_stream = cam.start_video(stream, 0, strict).await?;
log::trace!("Camera started");
while let Ok(media) = media_stream.get_data().await? {
media_tx.send(media).await?;
}
AnyResult::Ok(())
})
})
.await
})
.and_then(|res| async move {
log::debug!("Camera finished streaming: {res:?}");
Ok(())
}),
);
Ok(media_rx)
}
}
// A task that is run on a camera when the structure is dropped
+1
View File
@@ -17,6 +17,7 @@ use super::NeoInstance;
use crate::{AnyResult, Result};
use neolink_core::bc_protocol::MotionStatus;
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub(crate) enum MdState {
Start(Instant),
-4
View File
@@ -5,8 +5,6 @@ mod neocam;
#[cfg(feature = "pushnoti")]
mod pushnoti;
mod reactor;
#[cfg(feature = "gstreamer")]
mod streamthread;
mod usecounter;
pub(crate) use camthread::*;
@@ -16,6 +14,4 @@ pub(crate) use neocam::*;
#[cfg(feature = "pushnoti")]
pub(crate) use pushnoti::*;
pub(crate) use reactor::*;
#[cfg(feature = "gstreamer")]
pub(crate) use streamthread::*;
pub(crate) use usecounter::*;
-66
View File
@@ -23,27 +23,15 @@ use super::{
MdRequest, MdState, NeoCamMdThread, NeoCamThread, NeoCamThreadState, NeoInstance, Permit,
UseCounter,
};
#[cfg(feature = "gstreamer")]
use super::{NeoCamStreamThread, StreamInstance, StreamRequest};
#[cfg(feature = "pushnoti")]
use super::{PnRequest, PushNoti};
use crate::{config::CameraConfig, AnyResult, Result};
use neolink_core::bc_protocol::BcCamera;
#[cfg(feature = "gstreamer")]
use neolink_core::bc_protocol::StreamKind;
#[allow(dead_code)]
pub(crate) enum NeoCamCommand {
HangUp,
Instance(OneshotSender<Result<NeoInstance>>),
#[cfg(feature = "gstreamer")]
Stream(StreamKind, OneshotSender<StreamInstance>),
#[cfg(feature = "gstreamer")]
HighStream(OneshotSender<Option<StreamInstance>>),
#[cfg(feature = "gstreamer")]
LowStream(OneshotSender<Option<StreamInstance>>),
#[cfg(feature = "gstreamer")]
Streams(OneshotSender<Vec<StreamInstance>>),
Motion(OneshotSender<WatchReceiver<MdState>>),
Config(OneshotSender<WatchReceiver<CameraConfig>>),
Disconnect(OneshotSender<()>),
@@ -71,8 +59,6 @@ impl NeoCam {
let (commander_tx, commander_rx) = mpsc(100);
let (watch_config_tx, watch_config_rx) = watch(config.clone());
let (camera_watch_tx, camera_watch_rx) = watch(Weak::new());
#[cfg(feature = "gstreamer")]
let (stream_request_tx, stream_request_rx) = mpsc(100);
let (md_request_tx, md_request_rx) = mpsc(100);
let (state_tx, state_rx) = watch(NeoCamThreadState::Connected);
let (uid_tx, uid_rx) = watch(config.camera_uid.clone());
@@ -96,7 +82,6 @@ impl NeoCam {
let sender_cancel = me.cancel.clone();
let mut commander_rx = ReceiverStream::new(commander_rx);
#[cfg(feature = "gstreamer")]
let strict = config.strict;
let thread_commander_tx = commander_tx.clone();
let thread_watch_config_rx = watch_config_rx.clone();
#[cfg(feature = "pushnoti")]
@@ -123,40 +108,6 @@ impl NeoCam {
);
let _ = result.send(instance);
}
#[cfg(feature = "gstreamer")]
NeoCamCommand::Stream(name, sender) => {
stream_request_tx.send(
StreamRequest::GetOrInsert {
name,
sender,
strict,
}
).await?;
},
#[cfg(feature = "gstreamer")]
NeoCamCommand::HighStream(sender) => {
stream_request_tx.send(
StreamRequest::High {
sender,
}
).await?;
},
#[cfg(feature = "gstreamer")]
NeoCamCommand::LowStream(sender) => {
stream_request_tx.send(
StreamRequest::Low {
sender,
}
).await?;
},
#[cfg(feature = "gstreamer")]
NeoCamCommand::Streams(sender) => {
stream_request_tx.send(
StreamRequest::All {
sender,
}
).await?;
},
NeoCamCommand::Motion(sender) => {
md_request_tx.send(
MdRequest::Get {
@@ -231,23 +182,6 @@ impl NeoCam {
.await;
me.set.spawn(async move { cam_thread.run().await });
// This thread maintains the streams
#[cfg(feature = "gstreamer")]
{
let stream_instance = instance.subscribe().await?;
let stream_cancel = me.cancel.clone();
let mut stream_thread =
NeoCamStreamThread::new(stream_request_rx, stream_instance).await?;
me.set.spawn(async move {
tokio::select! {
_ = stream_cancel.cancelled() => AnyResult::Ok(()),
v = stream_thread.run() => {
v
},
}
});
}
// This thread monitors the motion
let md_instance = instance.subscribe().await?;
let md_cancel = me.cancel.clone();
-709
View File
@@ -1,709 +0,0 @@
//! This thread will start and stop the camera
//!
//! If there are no listeners to the broadcast
//! then it will hangup
use futures::stream::{FuturesUnordered, StreamExt};
use std::{
collections::{hash_map::Entry, HashMap, VecDeque},
sync::Arc,
};
use tokio::{
sync::{
broadcast::{
channel as broadcast, Receiver as BroadcastReceiver, Sender as BroadcastSender,
},
mpsc::{channel as mpsc, Receiver as MpscReceiver},
oneshot::{channel as oneshot, Sender as OneshotSender},
watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender},
},
task::JoinHandle,
time::{sleep, timeout, Duration},
};
use tokio_util::sync::CancellationToken;
use super::{NeoInstance, Permit, UseCounter};
use crate::{AnyResult, Result};
use neolink_core::{bc_protocol::StreamKind, bcmedia::model::*};
pub(crate) struct NeoCamStreamThread {
streams: HashMap<StreamKind, StreamData>,
stream_request_rx: MpscReceiver<StreamRequest>,
cancel: CancellationToken,
instance: NeoInstance,
}
impl NeoCamStreamThread {
pub(crate) async fn new(
stream_request_rx: MpscReceiver<StreamRequest>,
instance: NeoInstance,
) -> Result<Self> {
Ok(Self {
streams: Default::default(),
stream_request_rx,
cancel: CancellationToken::new(),
instance,
})
}
pub(crate) async fn run(&mut self) -> Result<()> {
let thread_cancel = self.cancel.clone();
tokio::select! {
_ = thread_cancel.cancelled() => {
for (_, mut data) in self.streams.drain() {
let _ = data.shutdown().await;
}
Ok(())
},
v = async {
while let Some(request) = self.stream_request_rx.recv().await {
match request {
StreamRequest::Get {
name, sender
} => {
if let Entry::Occupied(occ) = self.streams.entry(name) {
let _ = sender.send(Some(
StreamInstance::new(occ.get()).await?));
} else {
let _ = sender.send(None);
}
},
StreamRequest::GetOrInsert {
name, sender, strict
} => {
match self.streams.entry(name) {
Entry::Occupied(occ) => {
let _ = sender
.send(StreamInstance::new(occ.get()).await?);
}
Entry::Vacant(vac) => {
// Make a new streaming instance
let data = StreamData::new(
name,
self.instance.subscribe().await?,
strict,
).await?;
let data = vac.insert(data);
let _ = sender.send(StreamInstance::new(data).await?);
}
}
},
StreamRequest::High {
sender
} => {
let mut result = None;
let mut streams = vec![
StreamKind::Main,
StreamKind::Extern,
StreamKind::Sub,
];
let config = self.instance.config().await?.borrow().clone();
let config_streams = config.stream.as_stream_kinds();
for name in streams.drain(..) {
if config_streams.contains(&name) {
// Fill it in
if let Entry::Vacant(vac) = self.streams.entry(name) {
vac.insert(
StreamData::new(name, self.instance.subscribe().await?, config.strict)
.await?,
);
}
// Grab it
if let Entry::Occupied(occ) = self.streams.entry(name) {
result = Some(
StreamInstance::new(occ.get()).await?);
break;
}
}
}
let _ = sender.send(result);
},
StreamRequest::Low {
sender
} => {
let mut result = None;
let mut streams = vec![
StreamKind::Sub,
StreamKind::Extern,
StreamKind::Main,
];
let config = self.instance.config().await?.borrow().clone();
let config_streams = config.stream.as_stream_kinds();
for name in streams.drain(..) {
if config_streams.contains(&name) {
// Fill it in
if let Entry::Vacant(vac) = self.streams.entry(name) {
vac.insert(
StreamData::new(name, self.instance.subscribe().await?, config.strict)
.await?,
);
}
// Grab it
if let Entry::Occupied(occ) = self.streams.entry(name) {
result = Some(
StreamInstance::new(occ.get()).await?
);
break;
}
}
}
let _ = sender.send(result);
},
StreamRequest::All {
sender
} => {
let config = self.instance.config().await?.borrow_and_update().clone();
let streams = config.stream.as_stream_kinds();
for stream in streams.iter().copied() {
if let Entry::Vacant(vac) = self.streams.entry(stream) {
vac.insert(
StreamData::new(stream, self.instance.subscribe().await?, config.strict)
.await?,
);
}
}
let mut streams = self.streams.iter_mut().filter_map(|(name, stream)| if streams.contains(name) {
Some(async move {
StreamInstance::new(stream).await
})
} else {
None
}
).collect::<FuturesUnordered<_>>().collect::<Vec<_>>().await;
let _ = sender.send(streams.drain(..).flatten().collect());
}
}
}
Ok(())
} => v,
}
}
}
impl Drop for NeoCamStreamThread {
fn drop(&mut self) {
self.cancel.cancel();
for stream in self.streams.values() {
stream.cancel.cancel()
}
}
}
/// The kind of stream we want a async broadcast of
pub(crate) enum StreamRequest {
#[allow(dead_code)]
/// Get a currently loaded stream
Get {
name: StreamKind,
sender: OneshotSender<Option<StreamInstance>>,
},
/// Get or Insert a stream
GetOrInsert {
name: StreamKind,
sender: OneshotSender<StreamInstance>,
strict: bool,
},
/// Get highest available stream. Which this is depends on what is
/// disabled
High {
sender: OneshotSender<Option<StreamInstance>>,
},
/// Get lowest quality available stream. Which this is depends on what is
/// disabled
Low {
sender: OneshotSender<Option<StreamInstance>>,
},
/// Get all streams configured in the config
All {
sender: OneshotSender<Vec<StreamInstance>>,
},
}
/// The data of a running stream
pub(crate) struct StreamData {
vid: BroadcastSender<StampedData>,
aud: BroadcastSender<StampedData>,
vid_history: Arc<WatchSender<VecDeque<StampedData>>>,
aud_history: Arc<WatchSender<VecDeque<StampedData>>>,
config: Arc<WatchSender<StreamConfig>>,
name: StreamKind,
instance: NeoInstance,
cancel: CancellationToken,
handle: Option<JoinHandle<Result<()>>>,
strict: bool,
users: UseCounter,
}
#[derive(Eq, PartialEq, Clone, Debug, Copy)]
pub(crate) enum VidFormat {
None,
H264,
H265,
}
#[derive(Eq, PartialEq, Clone, Debug, Copy)]
pub(crate) enum AudFormat {
None,
Aac,
Adpcm(u32),
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub(crate) struct StreamConfig {
pub(crate) resolution: [u32; 2],
pub(crate) vid_format: VidFormat,
pub(crate) aud_format: AudFormat,
pub(crate) bitrate: u32,
pub(crate) fps: u32,
}
impl StreamConfig {
pub(crate) fn vid_ready(&self) -> bool {
self.resolution[0] > 0
&& self.resolution[1] > 0
&& self.bitrate > 0
&& !matches!(self.vid_format, VidFormat::None)
}
pub(crate) fn aud_ready(&self) -> bool {
self.vid_ready() && !matches!(self.aud_format, AudFormat::None)
}
}
#[derive(Debug, Clone)]
pub(crate) struct StampedData {
pub(crate) keyframe: bool,
pub(crate) data: Arc<Vec<u8>>,
pub(crate) ts: Duration,
}
pub(crate) struct StreamInstance {
#[allow(dead_code)]
pub(crate) name: StreamKind,
pub(crate) vid: BroadcastReceiver<StampedData>,
pub(crate) vid_history: WatchReceiver<VecDeque<StampedData>>,
pub(crate) aud: BroadcastReceiver<StampedData>,
pub(crate) aud_history: WatchReceiver<VecDeque<StampedData>>,
pub(crate) config: WatchReceiver<StreamConfig>,
in_use: Permit,
}
impl StreamInstance {
pub async fn new(data: &StreamData) -> Result<Self> {
Ok(Self {
name: data.name,
vid: data.vid.subscribe(),
vid_history: data.vid_history.subscribe(),
aud: data.aud.subscribe(),
aud_history: data.aud_history.subscribe(),
config: data.config.subscribe(),
in_use: data.users.create_activated().await?,
})
}
pub(crate) async fn activate(&mut self) -> Result<()> {
self.in_use.activate().await
}
pub(crate) async fn deactivate(&mut self) -> Result<()> {
self.in_use.deactivate().await
}
pub(crate) async fn activator_handle(&mut self) -> Permit {
self.in_use.subscribe()
}
}
impl StreamData {
async fn new(name: StreamKind, instance: NeoInstance, strict: bool) -> Result<Self> {
let buffer_duration =
Duration::from_millis(instance.config().await?.borrow().buffer_duration);
log::trace!("New StreamData::{name:?}");
// At 30fps for 15s with audio is is about 900 frames
let buffer_size: usize = 30usize * buffer_duration.as_millis() as usize / 1000usize;
let (vid, _) = broadcast::<StampedData>(buffer_size);
let (aud, _) = broadcast::<StampedData>(buffer_size);
let (vid_history, _) = watch::<VecDeque<StampedData>>(VecDeque::new());
let vid_history = Arc::new(vid_history);
let (aud_history, _) = watch::<VecDeque<StampedData>>(VecDeque::new());
let aud_history = Arc::new(aud_history);
let (resolution, bitrate, fps, fps_table) = instance
.run_passive_task(|cam| {
Box::pin(async move {
let infos = cam
.get_stream_info()
.await?
.stream_infos
.iter()
.flat_map(|info| info.encode_tables.clone())
.collect::<Vec<_>>();
if let Some(encode) =
infos.iter().find(|encode| encode.name == name.to_string())
{
let bitrate_table = encode
.bitrate_table
.split(',')
.filter_map(|c| {
let i: Result<u32, _> = c.parse();
i.ok()
})
.collect::<Vec<u32>>();
let framerate_table = encode
.framerate_table
.split(',')
.filter_map(|c| {
let i: Result<u32, _> = c.parse();
i.ok()
})
.collect::<Vec<u32>>();
Ok((
[encode.resolution.width, encode.resolution.height],
bitrate_table
.get(encode.default_bitrate as usize)
.copied()
.unwrap_or(encode.default_bitrate)
* 1024,
framerate_table
.get(encode.default_framerate as usize)
.copied()
.unwrap_or(encode.default_framerate),
framerate_table.clone(),
))
} else {
Ok(([0, 0], 0, 0, vec![]))
}
})
})
.await?;
let (config_tx, _) = watch(StreamConfig {
resolution,
vid_format: VidFormat::None,
aud_format: AudFormat::None,
bitrate,
fps,
});
let mut me = Self {
name,
cancel: CancellationToken::new(),
config: Arc::new(config_tx),
vid,
vid_history,
aud,
aud_history,
instance,
handle: None,
strict,
users: UseCounter::new().await,
};
let mut self_use = me.users.create_activated().await?; // Take out our own use handle until buffer is ready
let cancel = me.cancel.clone();
let vid = me.vid.clone();
let aud = me.aud.clone();
let instance = me.instance.subscribe().await?;
let name = me.name;
let cam_name = instance.config().await?.borrow().name.clone();
let print_name = format!("{cam_name}::{name}");
let strict = me.strict;
let config = me.config.clone();
let thread_inuse = me.users.create_deactivated().await?;
let vid_history = me.vid_history.clone();
let aud_history = me.aud_history.clone();
let mut permit = instance.permit().await?;
// Rather than extract the time stamp from the frame data we
// create a new one from the fps
// this makes it easier to play the frames at a constant rate
// even across reconnects (since reconnects restart the ts otherwise)
// These are arc mutex to allow tthem to maintain their value across reconnects (else we get an implicit copy)
let master_ts = Arc::new(tokio::sync::RwLock::new(Duration::ZERO));
let fps_delta = Arc::new(tokio::sync::RwLock::new(Duration::from_millis(
1000 / (config.borrow().fps as u64),
)));
me.handle = Some(tokio::task::spawn(async move {
let r = tokio::select! {
_ = cancel.cancelled() => {
Result::<(), anyhow::Error>::Ok(())
},
v = async {
loop {
let (watchdog_tx, mut watchdog_rx) = mpsc(1);
let (watchdog_eat_tx, watchdog_eat_rx) = oneshot();
// Give the watchdog his own thread to play in
//
// This should stop one branch of the select from waking the other
// too often
tokio::task::spawn(async move {
let mut check_timeout = timeout(Duration::from_secs(15), watchdog_rx.recv()).await; // Wait longer for the first feed
loop {
match check_timeout {
Err(_) => {
// Timeout
// Break with Ok to trigger the restart
break;
},
Ok(None) => {
break;
}
Ok(_) => {
check_timeout = timeout(Duration::from_secs(10), watchdog_rx.recv()).await;
}
}
}
// Watch dog is hungry send the kill to the stream thread
let _ = watchdog_eat_tx.send(());
}) ;
tokio::select! {
v = thread_inuse.dropped_users() => {
// Handles the stop and restart when no active users
log::trace!("Stopping StreamThread Permit");
permit.deactivate().await?;
v?;
log::trace!("Waiting for streamthread aquire users");
thread_inuse.aquired_users().await?; // Wait for new users of the stream
log::trace!("Actictating streamthread permit");
permit.activate().await?;
log::trace!("Stream thread should now restart");
AnyResult::Ok(())
},
_ = watchdog_eat_rx => {
sleep(Duration::from_secs(1)).await;
AnyResult::Ok(())
},
result = instance.run_passive_task(|camera| {
let vid_tx = vid.clone();
let aud_tx = aud.clone();
let stream_config = config.clone();
let vid_history = vid_history.clone();
let aud_history = aud_history.clone();
let watchdog_tx = watchdog_tx.clone();
let fps_table = fps_table.clone();
let master_ts = master_ts.clone();
let fps_delta = fps_delta.clone();
Box::pin(async move {
log::trace!("Starting streamthread TASK");
// use std::io::Write;
// let mut file = std::fs::File::create("reference.h264")?;
let mut recieved_iframe = false;
let mut aud_keyframe = false;
let res = async {
let mut stream_data = camera.start_video(name, 0, strict).await?;
loop {
let data = stream_data.get_data().await??;
watchdog_tx.send(()).await?; // Feed the watchdog
// Update the stream config with any information
match &data {
BcMedia::InfoV1(info) => {
stream_config.send_if_modified(|state| {
let new_fps = fps_table.get(info.fps as usize).copied().unwrap_or(info.fps as u32);
if state.resolution[0] != info.video_width || state.resolution[1] != info.video_height || new_fps != state.fps {
state.resolution[0] = info.video_width;
state.resolution[1] = info.video_height;
state.fps = new_fps;
true
} else {
false
}
});
// seperate the borrow so we don't hold it over an await
let new_delta = Duration::from_millis(1000 / (stream_config.borrow().fps as u64));
*fps_delta.write().await = new_delta;
},
BcMedia::InfoV2(info) => {
stream_config.send_if_modified(|state| {
let new_fps = fps_table.get(info.fps as usize).copied().unwrap_or(info.fps as u32);
if state.resolution[0] != info.video_width || state.resolution[1] != info.video_height || new_fps != state.fps {
state.resolution[0] = info.video_width;
state.resolution[1] = info.video_height;
state.fps = new_fps;
true
} else {
false
}
});
// seperate the borrow so we don't hold it over an await
let new_delta = Duration::from_millis(1000 / (stream_config.borrow().fps as u64));
*fps_delta.write().await = new_delta;
},
BcMedia::Iframe(frame) => {
stream_config.send_if_modified(|state| {
let expected = match frame.video_type {
VideoType::H264 => VidFormat::H264,
VideoType::H265 => VidFormat::H265,
};
if state.vid_format != expected {
state.vid_format = expected;
true
} else {
false
}
});
// let _ = file.write(&frame.data);
}
BcMedia::Pframe(frame) => {
stream_config.send_if_modified(|state| {
let expected = match frame.video_type {
VideoType::H264 => VidFormat::H264,
VideoType::H265 => VidFormat::H265,
};
if state.vid_format != expected {
state.vid_format = expected;
true
} else {
false
}
});
// let _ = file.write(&frame.data);
},
BcMedia::Aac(_) => {
stream_config.send_if_modified(|state| {
if state.aud_format != AudFormat::Aac {
state.aud_format = AudFormat::Aac;
true
} else {
false
}
});
}
BcMedia::Adpcm(aud) => {
stream_config.send_if_modified(|state| {
let expected = AudFormat::Adpcm(aud.data.len() as u32 - 4);
if state.aud_format != expected {
state.aud_format = expected;
true
} else {
false
}
});
}
}
match data {
BcMedia::Iframe(BcMediaIframe{data, ..}) => {
let d = StampedData{
keyframe: true,
data: Arc::new(data),
ts: *master_ts.read().await,
};
let _ = vid_tx.send(d.clone());
vid_history.send_modify(|history| {
let drop_time = d.ts.saturating_sub(buffer_duration);
let dts = d.ts;
history.push_back(d);
while history.front().is_some_and(|di| di.ts < drop_time || di.ts > dts) || history.len() > buffer_size {
history.pop_front();
}
log::trace!("history: {}", history.len());
let debug: Vec<Duration> = history.iter().map(|f| f.ts).collect();
log::trace!("history ts: {:?}", debug);
});
recieved_iframe = true;
aud_keyframe = true;
log::trace!("Sent Vid Key Frame: {:?}", master_ts.read().await);
*master_ts.write().await += *fps_delta.read().await;
},
BcMedia::Pframe(BcMediaPframe{data, ..}) if recieved_iframe => {
let d = StampedData{
keyframe: false,
data: Arc::new(data),
ts: *master_ts.read().await,
};
let _ = vid_tx.send(d.clone());
vid_history.send_modify(|history| {
let drop_time = d.ts.saturating_sub(buffer_duration);
let dts = d.ts;
history.push_back(d);
while history.front().is_some_and(|di| di.ts < drop_time || di.ts > dts) || history.len() > buffer_size {
history.pop_front();
}
});
*master_ts.write().await += *fps_delta.read().await;
log::trace!("Sent Vid Frame: {:?}", master_ts.read().await);
}
BcMedia::Aac(BcMediaAac{data, ..}) | BcMedia::Adpcm(BcMediaAdpcm{data,..}) if recieved_iframe => {
let m_ts = *master_ts.read().await;
let d = StampedData{
keyframe: aud_keyframe,
data: Arc::new(data),
ts: m_ts,
};
aud_keyframe = false;
let _ = aud_tx.send(d.clone())?;
let last_vid_ts: Duration = vid_history.borrow().front().map(|vi| vi.ts).unwrap_or(m_ts);
aud_history.send_modify(|history| {
let drop_time = std::cmp::max(d.ts.saturating_sub(buffer_duration), last_vid_ts);
let dts = d.ts;
history.push_back(d);
while history.front().is_some_and(|di| di.ts < drop_time || di.ts > dts) || history.len() > buffer_size {
history.pop_front();
}
});
log::trace!("Sent Aud Frame");
},
_ => {},
}
}
}.await;
log::info!("video outgoing queue: {}", vid_tx.len());
Ok(res)
})
}) => {
match result {
Ok(AnyResult::Ok(())) => {
log::debug!("{print_name}: Video Stream Stopped due to no listeners");
break Ok(());
},
Ok(Err(e)) => {
log::debug!("{print_name}: Video Stream Restarting Due to Error: {:?}", e);
AnyResult::Ok(())
},
Err(e) => {
log::debug!("{print_name}: Video Stream Stopped Due to Instance Error: {:?}", e);
break Err(e);
},
}
},
}?;
// Deactivate our use handle if the vid buffer is ready
if vid_history.borrow().len() >= buffer_size {
self_use.deactivate().await?;
}
}
} => v,
};
r
}));
Ok(me)
}
async fn shutdown(&mut self) -> Result<()> {
self.cancel.cancel();
if let Some(handle) = self.handle.take() {
let _ = handle.await;
}
Ok(())
}
}
impl Drop for StreamData {
fn drop(&mut self) {
log::trace!("Drop StreamData");
self.cancel.cancel();
if let Some(h) = self.handle.take() {
let _gt = tokio::runtime::Handle::current().enter();
tokio::task::spawn(async move {
let _ = h.await;
log::trace!("Dropped StreamData");
});
}
}
}
+3
View File
@@ -64,6 +64,7 @@ impl UseCounter {
}
#[cfg(feature = "gstreamer")]
#[allow(dead_code)]
pub(crate) async fn create_deactivated(&self) -> Result<Permit> {
Ok(Permit::new(self))
}
@@ -89,6 +90,7 @@ pub(crate) struct Permit {
impl Permit {
#[cfg(feature = "gstreamer")]
#[allow(dead_code)]
pub(crate) fn subscribe(&self) -> Self {
Self {
is_active: false,
@@ -144,6 +146,7 @@ impl Permit {
}
#[cfg(feature = "gstreamer")]
#[allow(dead_code)]
pub(crate) fn get_counter(&self) -> WatchReceiver<u32> {
self.value.clone()
}
+6 -6
View File
@@ -59,13 +59,13 @@ pub(crate) struct MqttServerConfig {
pub(crate) port: u16,
#[serde(default)]
#[serde(default, skip_serializing)]
pub(crate) credentials: Option<(String, String)>,
#[serde(default)]
#[serde(default, skip_serializing)]
pub(crate) ca: Option<std::path::PathBuf>,
#[serde(default)]
#[serde(default, skip_serializing)]
pub(crate) client_auth: Option<(std::path::PathBuf, std::path::PathBuf)>,
}
@@ -139,7 +139,7 @@ pub(crate) struct CameraConfig {
pub(crate) username: String,
#[serde(alias = "pass")]
#[serde(alias = "pass", skip_serializing, default)]
pub(crate) password: Option<String>,
#[serde(default = "default_stream")]
@@ -226,8 +226,8 @@ pub(crate) struct UserConfig {
#[serde(alias = "username")]
pub(crate) name: String,
#[serde(alias = "password")]
pub(crate) pass: String,
#[serde(alias = "password", skip_serializing, default)]
pub(crate) pass: Option<String>,
}
#[derive(Debug, Deserialize, Serialize, Clone, Validate, PartialEq, Eq)]
+6 -6
View File
@@ -5,6 +5,7 @@ use gstreamer::{
parse::launch_full, prelude::*, ClockTime, MessageView, ParseFlags, Pipeline, State,
};
use gstreamer_app::AppSrc;
use neolink_core::bcmedia::model::VideoType;
use tokio::{
sync::{
self,
@@ -14,7 +15,7 @@ use tokio::{
};
use tokio_util::sync::CancellationToken;
use crate::{common::VidFormat, AnyResult};
use crate::AnyResult;
#[derive(Debug)]
enum GstControl {
@@ -74,7 +75,7 @@ impl Drop for GstSender {
}
pub(super) async fn from_input<T: AsRef<Path>>(
format: VidFormat,
format: VideoType,
out_file: T,
) -> Result<GstSender> {
let pipeline = create_pipeline(format, out_file.as_ref())?;
@@ -171,13 +172,13 @@ fn get_source(pipeline: &Pipeline) -> Result<AppSrc> {
.map_err(|_| anyhow!("Cannot find appsource in gstreamer, check your gstreamer plugins"))
}
fn create_pipeline(format: VidFormat, file_path: &Path) -> Result<Pipeline> {
fn create_pipeline(format: VideoType, file_path: &Path) -> Result<Pipeline> {
gstreamer::init()
.context("Unable to start gstreamer ensure it and all plugins are installed")?;
let file_path = file_path.with_extension("jpeg");
let launch_str = match format {
VidFormat::H264 => {
VideoType::H264 => {
format!(
"appsrc name=thesource \
! h264parse \
@@ -187,7 +188,7 @@ fn create_pipeline(format: VidFormat, file_path: &Path) -> Result<Pipeline> {
file_path.display()
)
}
VidFormat::H265 => {
VideoType::H265 => {
format!(
"appsrc name=thesource \
! h265parse \
@@ -197,7 +198,6 @@ fn create_pipeline(format: VidFormat, file_path: &Path) -> Result<Pipeline> {
file_path.display()
)
}
VidFormat::None => unreachable!(),
};
log::info!("{}", launch_str);
+59 -40
View File
@@ -17,17 +17,19 @@
/// neolink image --config=config.toml --use_stream --file-path=filepath CameraName
/// ```
///
use anyhow::{Context, Result};
use futures::stream::StreamExt;
use anyhow::{anyhow, Result};
use log::*;
use neolink_core::bc_protocol::*;
use tokio::{fs::File, io::AsyncWriteExt};
use tokio_stream::wrappers::BroadcastStream;
use neolink_core::{
bc_protocol::*,
bcmedia::model::{BcMedia, BcMediaIframe, BcMediaPframe},
};
use std::sync::Arc;
use tokio::{fs::File, io::AsyncWriteExt, sync::RwLock};
mod cmdline;
mod gst;
use crate::common::{NeoReactor, StampedData};
use crate::common::NeoReactor;
pub(crate) use cmdline::Opt;
/// Entry point for the image subcommand
@@ -37,48 +39,65 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
let camera = reactor.get(&opt.camera).await?;
if opt.use_stream {
let stream_data = camera
.stream(StreamKind::Main)
.await
.context("Failed to start video")?;
let (stream_data_tx, mut stream_data_rx) = tokio::sync::mpsc::channel(100);
// Get one iframe at the start while also getting the the video type
let mut stream_config = stream_data.config.clone();
let vid_type = stream_config
.wait_for(|config| config.vid_ready())
.await?
.vid_format;
let mut stream = BroadcastStream::new(stream_data.vid.resubscribe())
.filter(|f| futures::future::ready(f.is_ok())); // Filter to ignore lagged
let buf = loop {
if let Some(Ok(StampedData {
data,
ts: _,
keyframe,
})) = stream.next().await
{
if keyframe {
break data;
}
}
};
// Spawn a video stream
let thread_camera = camera.clone();
let (stream_type_tx, stream_type_rx) = tokio::sync::oneshot::channel();
let stream_type_tx = Arc::new(RwLock::new(Some(stream_type_tx)));
tokio::task::spawn(async move {
thread_camera
.run_task(|cam| {
let stream_type_tx = stream_type_tx.clone();
let stream_data_tx = stream_data_tx.clone();
Box::pin(async move {
let mut stream = cam.start_video(StreamKind::Main, 100, false).await?;
while let Ok(frame) = stream.get_data().await {
let frame = frame?;
match frame {
BcMedia::Iframe(BcMediaIframe {
data, video_type, ..
})
| BcMedia::Pframe(BcMediaPframe {
data, video_type, ..
}) => {
if let Some(stream_type_tx) =
stream_type_tx.write().await.take()
{
let _ = stream_type_tx.send(video_type);
}
stream_data_tx.send(Arc::new(data)).await?;
}
_ => {}
}
}
Result::Ok(())
})
})
.await
});
let vid_type = stream_type_rx.await?;
let buf = stream_data_rx
.recv()
.await
.ok_or(anyhow!("No frames recieved"))?;
let mut sender = gst::from_input(vid_type, &opt.file_path).await?;
sender.send(buf).await?; // Send first iframe
// Keep sending both IFrame or PFrame until finished
while sender.is_finished().await.is_none() {
let buf = match stream.next().await {
Some(Ok(StampedData { data, .. })) => data,
_ => {
continue;
if let Some(buf) = stream_data_rx.recv().await {
debug!("Sending frame data to gstreamer");
if sender.send(buf).await.is_err() {
// Assume that the sender is closed
// because the pipeline is finished
break;
}
};
debug!("Sending frame data to gstreamer");
if sender.send(buf).await.is_err() {
// Assume that the sender is closed
// because the pipeline is finished
} else {
log::error!("Camera stopped sending frames before decoding could complete");
break;
}
}
-35
View File
@@ -1,35 +0,0 @@
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
// Handles app wide things like aborting all connections
pub(crate) struct App {
aborters: Arc<Mutex<HashSet<String>>>,
}
impl App {
pub(crate) fn new() -> Self {
Self {
aborters: Default::default(),
}
}
// Abort at a level
// e.g. about camera_event_loop
pub(crate) fn abort(&self, level: &str) {
let mut aborters = self.aborters.lock().unwrap();
aborters.insert(level.to_string());
}
// Checks if any level has aborted
// e.g. app:camera_a::camera_event_loop
// will check if app, camera_a or camera_event_loop has been aborted
pub(crate) fn running(&self, levels: &str) -> bool {
let aborters = self.aborters.lock().unwrap();
for level in levels.split(':') {
if aborters.contains(&level.to_string()) {
return false;
}
}
true
}
}
+34 -1
View File
@@ -211,7 +211,40 @@ pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> {
.await?;
continue;
}
let config = config?;
let curr_config = thread_config.borrow().clone();
let mut config = config?;
// Fill in skipped passwords
if let (Some(mqtt), Some(curr_mqtt)) = (config.mqtt.as_mut(), curr_config.mqtt.as_ref()) {
if mqtt.credentials.is_none() {
mqtt.credentials = curr_mqtt.credentials.clone();
}
if mqtt.ca.is_none() {
mqtt.ca = curr_mqtt.ca.clone();
}
if mqtt.client_auth.is_none() {
mqtt.client_auth = curr_mqtt.client_auth.clone();
}
}
for cam in config.cameras.iter_mut() {
let name = cam.name.clone();
let cur_cam = curr_config.cameras.iter().find(|c| c.name == name);
if let Some(cur_cam) = cur_cam.as_ref() {
if cam.password.is_none() {
cam.password = cur_cam.password.clone();
}
}
}
for user in config.users.iter_mut() {
let name = user.name.clone();
let cur_user = curr_config.users.iter().find(|c| c.name == name);
if let Some(cur_user) = cur_user.as_ref() {
if user.pass.is_none() {
user.pass = cur_user.pass.clone();
}
}
}
// Passwords should now be restored if they were not set
let validate = config.validate().with_context(|| {
format!("Failed to validate the MQTT {:?} config file", msg.topic)
+463 -117
View File
@@ -1,21 +1,130 @@
use gstreamer::ClockTime;
use std::{collections::HashMap, time::Duration};
use anyhow::{anyhow, Context, Result};
use gstreamer::{prelude::*, Bin, Caps, Element, ElementFactory, GhostPad};
use gstreamer::{prelude::*, Bin, Caps, Element, ElementFactory, FlowError, GhostPad};
use gstreamer_app::{AppSrc, AppSrcCallbacks, AppStreamType};
use tokio::sync::mpsc::{channel as mpsc, Receiver as MpscReceiver};
use crate::{
common::{AudFormat, StreamConfig, VidFormat},
rtsp::gst::NeoMediaFactory,
AnyResult,
use neolink_core::{
bc_protocol::StreamKind,
bcmedia::model::{
BcMedia, BcMediaIframe, BcMediaInfoV1, BcMediaInfoV2, BcMediaPframe, VideoType,
},
};
use tokio::{sync::mpsc::channel as mpsc, task::JoinHandle};
pub(super) struct ClientSourceData {
pub(super) app: AppSrc,
use crate::{common::NeoInstance, rtsp::gst::NeoMediaFactory, AnyResult};
#[derive(Clone, Debug)]
pub enum AudioType {
Aac,
Adpcm(u32),
}
pub(super) struct ClientData {
pub(super) vid: Option<ClientSourceData>,
pub(super) aud: Option<ClientSourceData>,
#[derive(Clone, Debug)]
struct StreamConfig {
#[allow(dead_code)]
resolution: [u32; 2],
bitrate: u32,
fps: u32,
bitrate_table: Vec<u32>,
fps_table: Vec<u32>,
vid_type: Option<VideoType>,
aud_type: Option<AudioType>,
}
impl StreamConfig {
async fn new(instance: &NeoInstance, name: StreamKind) -> AnyResult<Self> {
let (resolution, bitrate, fps, fps_table, bitrate_table) = instance
.run_passive_task(|cam| {
Box::pin(async move {
let infos = cam
.get_stream_info()
.await?
.stream_infos
.iter()
.flat_map(|info| info.encode_tables.clone())
.collect::<Vec<_>>();
if let Some(encode) =
infos.iter().find(|encode| encode.name == name.to_string())
{
let bitrate_table = encode
.bitrate_table
.split(',')
.filter_map(|c| {
let i: Result<u32, _> = c.parse();
i.ok()
})
.collect::<Vec<u32>>();
let framerate_table = encode
.framerate_table
.split(',')
.filter_map(|c| {
let i: Result<u32, _> = c.parse();
i.ok()
})
.collect::<Vec<u32>>();
Ok((
[encode.resolution.width, encode.resolution.height],
bitrate_table
.get(encode.default_bitrate as usize)
.copied()
.unwrap_or(encode.default_bitrate)
* 1024,
framerate_table
.get(encode.default_framerate as usize)
.copied()
.unwrap_or(encode.default_framerate),
framerate_table.clone(),
bitrate_table.clone(),
))
} else {
Ok(([0, 0], 0, 0, vec![], vec![]))
}
})
})
.await?;
Ok(StreamConfig {
resolution,
bitrate,
fps,
fps_table,
bitrate_table,
vid_type: None,
aud_type: None,
})
}
fn update_fps(&mut self, fps: u32) {
let new_fps = self.fps_table.get(fps as usize).copied().unwrap_or(fps);
self.fps = new_fps;
}
#[allow(dead_code)]
fn update_bitrate(&mut self, bitrate: u32) {
let new_bitrate = self
.bitrate_table
.get(bitrate as usize)
.copied()
.unwrap_or(bitrate);
self.bitrate = new_bitrate;
}
fn update_from_media(&mut self, media: &BcMedia) {
match media {
BcMedia::InfoV1(BcMediaInfoV1 { fps, .. })
| BcMedia::InfoV2(BcMediaInfoV2 { fps, .. }) => self.update_fps(*fps as u32),
BcMedia::Aac(_) => {
self.aud_type = Some(AudioType::Aac);
}
BcMedia::Adpcm(adpcm) => {
self.aud_type = Some(AudioType::Adpcm(adpcm.block_size()));
}
BcMedia::Iframe(BcMediaIframe { video_type, .. })
| BcMedia::Pframe(BcMediaPframe { video_type, .. }) => {
self.vid_type = Some(*video_type);
}
}
}
}
pub(super) async fn make_dummy_factory(
@@ -34,78 +143,314 @@ pub(super) async fn make_dummy_factory(
.await
}
enum ClientMsg {
NewClient {
element: Element,
reply: tokio::sync::oneshot::Sender<Element>,
},
}
pub(super) async fn make_factory(
camera: NeoInstance,
stream: StreamKind,
) -> AnyResult<(NeoMediaFactory, JoinHandle<AnyResult<()>>)> {
let (client_tx, mut client_rx) = mpsc(100);
// Create the task that creates the pipelines
let thread = tokio::task::spawn(async move {
let name = camera.config().await?.borrow().name.clone();
while let Some(msg) = client_rx.recv().await {
match msg {
ClientMsg::NewClient { element, reply } => {
log::debug!("New client for {name}::{stream}");
let camera = camera.clone();
let name = name.clone();
tokio::task::spawn(async move {
clear_bin(&element)?;
log::trace!("{name}::{stream}: Starting camera");
// Start the camera
let config = camera.config().await?.borrow().clone();
let mut media_rx = camera.stream_while_live(stream).await?;
log::trace!("{name}::{stream}: Learning camera stream type");
// Learn the camera data type
let mut buffer = vec![];
let mut frame_count = 0usize;
let mut stream_config = StreamConfig::new(&camera, stream).await?;
while let Some(media) = media_rx.recv().await {
stream_config.update_from_media(&media);
buffer.push(media);
if frame_count > 10
|| (stream_config.vid_type.is_some()
&& stream_config.aud_type.is_some())
{
break;
}
frame_count += 1;
}
log::trace!("{name}::{stream}: Building the pipeline");
// Build the right video pipeline
let vid_src = match stream_config.vid_type.as_ref() {
Some(VideoType::H264) => {
let src = build_h264(&element, &stream_config)?;
AnyResult::Ok(Some(src))
}
Some(VideoType::H265) => {
let src = build_h265(&element, &stream_config)?;
AnyResult::Ok(Some(src))
}
None => {
build_unknown(&element, &config.splash_pattern.to_string())?;
AnyResult::Ok(None)
}
}?;
// Build the right audio pipeline
let aud_src = match stream_config.aud_type.as_ref() {
Some(AudioType::Aac) => {
let src = build_aac(&element, &stream_config)?;
AnyResult::Ok(Some(src))
}
Some(AudioType::Adpcm(block_size)) => {
let src = build_adpcm(&element, *block_size, &stream_config)?;
AnyResult::Ok(Some(src))
}
None => AnyResult::Ok(None),
}?;
if let Some(app) = vid_src.as_ref() {
app.set_callbacks(
AppSrcCallbacks::builder()
.seek_data(move |_, _seek_pos| true)
.build(),
);
}
if let Some(app) = aud_src.as_ref() {
app.set_callbacks(
AppSrcCallbacks::builder()
.seek_data(move |_, _seek_pos| true)
.build(),
);
}
log::trace!("{name}::{stream}: Sending pipeline to gstreamer");
// Send the pipeline back to the factory so it can start
let _ = reply.send(element);
// Run blocking code on a seperate thread
// This is not an async thread
std::thread::spawn(move || {
let mut aud_ts = 0u32;
let mut vid_ts = 0u32;
let mut pools = Default::default();
log::trace!("{name}::{stream}: Sending buffered frames");
for buffered in buffer.drain(..) {
send_to_sources(
buffered,
&mut pools,
&vid_src,
&aud_src,
&mut vid_ts,
&mut aud_ts,
&stream_config,
)?;
}
log::trace!("{name}::{stream}: Sending new frames");
while let Some(data) = media_rx.blocking_recv() {
let r = send_to_sources(
data,
&mut pools,
&vid_src,
&aud_src,
&mut vid_ts,
&mut aud_ts,
&stream_config,
);
if let Err(r) = &r {
log::info!("Failed to send to source: {r:?}");
}
r?;
}
log::trace!("All media recieved");
AnyResult::Ok(())
});
AnyResult::Ok(())
});
}
}
}
AnyResult::Ok(())
});
// Now setup the factory
let factory = NeoMediaFactory::new_with_callback(move |element| {
let (reply, new_element) = tokio::sync::oneshot::channel();
client_tx.blocking_send(ClientMsg::NewClient { element, reply })?;
let element = new_element.blocking_recv()?;
Ok(Some(element))
})
.await?;
Ok((factory, thread))
}
fn send_to_sources(
data: BcMedia,
pools: &mut HashMap<usize, gstreamer::BufferPool>,
vid_src: &Option<AppSrc>,
aud_src: &Option<AppSrc>,
vid_ts: &mut u32,
aud_ts: &mut u32,
stream_config: &StreamConfig,
) -> AnyResult<(NeoMediaFactory, MpscReceiver<ClientData>)> {
let (client_tx, client_rx) = mpsc(100);
let factory = {
let stream_config = stream_config.clone();
) -> AnyResult<()> {
// Update TS
match data {
BcMedia::Aac(aac) => {
let duration = aac.duration().expect("Could not calculate AAC duration");
if let Some(aud_src) = aud_src.as_ref() {
log::debug!("Sending AAC: {:?}", Duration::from_micros(*aud_ts as u64));
send_to_appsrc(
aud_src,
aac.data,
Duration::from_micros(*aud_ts as u64),
pools,
)?;
}
*aud_ts += duration;
}
BcMedia::Adpcm(adpcm) => {
let duration = adpcm
.duration()
.expect("Could not calculate ADPCM duration");
if let Some(aud_src) = aud_src.as_ref() {
log::trace!("Sending ADPCM: {:?}", Duration::from_micros(*aud_ts as u64));
send_to_appsrc(
aud_src,
adpcm.data,
Duration::from_micros(*aud_ts as u64),
pools,
)?;
}
*aud_ts += duration;
}
BcMedia::Iframe(BcMediaIframe { data, .. })
| BcMedia::Pframe(BcMediaPframe { data, .. }) => {
if let Some(vid_src) = vid_src.as_ref() {
log::trace!("Sending VID: {:?}", Duration::from_micros(*vid_ts as u64));
send_to_appsrc(vid_src, data, Duration::from_micros(*vid_ts as u64), pools)?;
}
const MICROSECONDS: u32 = 1000000;
*vid_ts += MICROSECONDS / stream_config.fps;
}
_ => {}
}
Ok(())
}
NeoMediaFactory::new_with_callback(move |element| {
clear_bin(&element)?;
let vid = match stream_config.vid_format {
VidFormat::None => {
// This should not be reachable
log::debug!("Building unknown during normal make factory");
build_unknown(&element, "black")?;
AnyResult::Ok(None)
}
VidFormat::H264 => {
let app = build_h264(&element, &stream_config)?;
app.set_callbacks(
AppSrcCallbacks::builder()
.seek_data(move |_, _seek_pos| true)
.build(),
);
AnyResult::Ok(Some(app))
}
VidFormat::H265 => {
let app = build_h265(&element, &stream_config)?;
fn send_to_appsrc(
appsrc: &AppSrc,
data: Vec<u8>,
mut ts: Duration,
pools: &mut HashMap<usize, gstreamer::BufferPool>,
) -> AnyResult<()> {
check_live(appsrc)?; // Stop if appsrc is dropped
app.set_callbacks(
AppSrcCallbacks::builder()
.seek_data(move |_, _seek_pos| true)
.build(),
);
AnyResult::Ok(Some(app))
}
}?;
let aud = if matches!(stream_config.vid_format, VidFormat::None) {
None
// In live mode we follow the advice in
// https://gstreamer.freedesktop.org/documentation/additional/design/element-source.html?gi-language=c#live-sources
// Only push buffers when in play state and have a clock
// we also timestamp at the current time
if appsrc.is_live() {
if let Some(time) = appsrc
.current_clock_time()
.and_then(|t| appsrc.base_time().map(|bt| t - bt))
{
if matches!(appsrc.current_state(), gstreamer::State::Playing) {
ts = Duration::from_micros(time.useconds());
} else {
match stream_config.aud_format {
AudFormat::None => AnyResult::Ok(None),
AudFormat::Aac => {
let app = build_aac(&element, &stream_config)?;
app.set_callbacks(
AppSrcCallbacks::builder()
.seek_data(move |_, _seek_pos| true)
.build(),
);
AnyResult::Ok(Some(app))
}
AudFormat::Adpcm(block_size) => {
let app = build_adpcm(&element, block_size, &stream_config)?;
app.set_callbacks(
AppSrcCallbacks::builder()
.seek_data(move |_, _seek_pos| true)
.build(),
);
AnyResult::Ok(Some(app))
}
}?
};
// Not playing
return Ok(());
}
} else {
// Clock not up yet
return Ok(());
}
}
let buf = {
let msg_size = data.len();
client_tx.blocking_send(ClientData {
vid: vid.map(|app| ClientSourceData { app }),
aud: aud.map(|app| ClientSourceData { app }),
})?;
Ok(Some(element))
})
.await
// Get or create a pool of this len
let pool = pools.entry(msg_size).or_insert_with_key(|size| {
let pool = gstreamer::BufferPool::new();
let mut pool_config = pool.config();
// Set a max buffers to ensure we don't grow in memory endlessly
pool_config.set_params(None, (*size) as u32, 8, 32);
pool.set_config(pool_config).unwrap();
pool.set_active(true).unwrap();
pool
});
// Get a buffer from the pool and then copy in the data
let gst_buf = {
let mut new_buf = pool.acquire_buffer(None).unwrap();
let gst_buf_mut = new_buf.get_mut().unwrap();
let time = ClockTime::from_useconds(ts.as_micros() as u64);
gst_buf_mut.set_dts(time);
gst_buf_mut.set_pts(time);
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
gst_buf_data.copy_from_slice(data.as_slice());
drop(gst_buf_data);
new_buf
};
// Return the new buffer with the data
gst_buf
};
// Push buffer into the appsrc
match appsrc.push_buffer(buf) {
Ok(_) => {
// log::info!(
// "Send {}{} on {}",
// data.data.len(),
// if data.keyframe { " (keyframe)" } else { "" },
// appsrc.name()
// );
Ok(())
}
Err(FlowError::Flushing) => {
// Buffer is full just skip
log::info!(
"Buffer full on {} pausing stream until client consumes frames",
appsrc.name()
);
Ok(())
}
Err(e) => Err(anyhow!("Error in streaming: {e:?}")),
}?;
Ok((factory, client_rx))
// Check if we need to pause
if appsrc.current_level_bytes() >= appsrc.max_bytes() * 2 / 3
&& matches!(appsrc.current_state(), gstreamer::State::Paused)
{
appsrc.set_state(gstreamer::State::Playing).unwrap();
} else if appsrc.current_level_bytes() <= appsrc.max_bytes() / 3
&& matches!(appsrc.current_state(), gstreamer::State::Playing)
{
appsrc.set_state(gstreamer::State::Paused).unwrap();
}
Ok(())
}
fn check_live(app: &AppSrc) -> Result<()> {
app.bus().ok_or(anyhow!("App source is closed"))?;
app.pads()
.iter()
.all(|pad| pad.is_linked())
.then_some(())
.ok_or(anyhow!("App source is not linked"))
}
fn clear_bin(bin: &Element) -> Result<()> {
@@ -181,7 +526,7 @@ fn pipe_h264(bin: &Element, stream_config: &StreamConfig) -> Result<Linked> {
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Seekable);
source.set_stream_type(AppStreamType::Stream);
let source = source
.dynamic_cast::<Element>()
@@ -232,7 +577,7 @@ fn pipe_h265(bin: &Element, stream_config: &StreamConfig) -> Result<Linked> {
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Seekable);
source.set_stream_type(AppStreamType::Stream);
let source = source
.dynamic_cast::<Element>()
@@ -285,7 +630,7 @@ fn pipe_aac(bin: &Element, stream_config: &StreamConfig) -> Result<Linked> {
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Seekable);
source.set_stream_type(AppStreamType::Stream);
let source = source
.dynamic_cast::<Element>()
@@ -371,7 +716,7 @@ fn pipe_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) -> R
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Seekable);
source.set_stream_type(AppStreamType::Stream);
source.set_caps(Some(
&Caps::builder("audio/x-adpcm")
@@ -424,6 +769,7 @@ fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) ->
Ok(linked.appsrc)
}
#[allow(dead_code)]
fn pipe_silence(bin: &Element, stream_config: &StreamConfig) -> Result<Linked> {
// Audio seems to run at about 800kbs
let buffer_size = 512 * 1416;
@@ -442,7 +788,7 @@ fn pipe_silence(bin: &Element, stream_config: &StreamConfig) -> Result<Linked> {
source.set_property("emit-signals", false);
source.set_max_bytes(buffer_size as u64);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Seekable);
source.set_stream_type(AppStreamType::Stream);
let source = source
.dynamic_cast::<Element>()
@@ -477,47 +823,47 @@ struct AppSrcPair {
aud: Option<AppSrc>,
}
#[allow(dead_code)]
/// Experimental build a stream of MPEGTS
fn build_mpegts(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrcPair> {
let buffer_size = buffer_size(stream_config.bitrate);
log::debug!(
"buffer_size: {buffer_size}, bitrate: {}",
stream_config.bitrate
);
// #[allow(dead_code)]
// /// Experimental build a stream of MPEGTS
// fn build_mpegts(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrcPair> {
// let buffer_size = buffer_size(stream_config.bitrate);
// log::debug!(
// "buffer_size: {buffer_size}, bitrate: {}",
// stream_config.bitrate
// );
// VID
let vid_link = match stream_config.vid_format {
VidFormat::H264 => pipe_h264(bin, stream_config)?,
VidFormat::H265 => pipe_h265(bin, stream_config)?,
VidFormat::None => unreachable!(),
};
// // VID
// let vid_link = match stream_config.vid_format {
// VidFormat::H264 => pipe_h264(bin, stream_config)?,
// VidFormat::H265 => pipe_h265(bin, stream_config)?,
// VidFormat::None => unreachable!(),
// };
// AUD
let aud_link = match stream_config.aud_format {
AudFormat::Aac => pipe_aac(bin, stream_config)?,
AudFormat::Adpcm(block) => pipe_adpcm(bin, block, stream_config)?,
AudFormat::None => pipe_silence(bin, stream_config)?,
};
// // AUD
// let aud_link = match stream_config.aud_format {
// AudFormat::Aac => pipe_aac(bin, stream_config)?,
// AudFormat::Adpcm(block) => pipe_adpcm(bin, block, stream_config)?,
// AudFormat::None => pipe_silence(bin, stream_config)?,
// };
let bin = bin
.clone()
.dynamic_cast::<Bin>()
.map_err(|_| anyhow!("Media source's element should be a bin"))?;
// let bin = bin
// .clone()
// .dynamic_cast::<Bin>()
// .map_err(|_| anyhow!("Media source's element should be a bin"))?;
// MUX
let muxer = make_element("mpegtsmux", "mpeg_muxer")?;
let rtp = make_element("rtpmp2tpay", "pay0")?;
// // MUX
// let muxer = make_element("mpegtsmux", "mpeg_muxer")?;
// let rtp = make_element("rtpmp2tpay", "pay0")?;
bin.add_many([&muxer, &rtp])?;
Element::link_many([&vid_link.output, &muxer, &rtp])?;
Element::link_many([&aud_link.output, &muxer])?;
// bin.add_many([&muxer, &rtp])?;
// Element::link_many([&vid_link.output, &muxer, &rtp])?;
// Element::link_many([&aud_link.output, &muxer])?;
Ok(AppSrcPair {
vid: vid_link.appsrc,
aud: Some(aud_link.appsrc),
})
}
// Ok(AppSrcPair {
// vid: vid_link.appsrc,
// aud: Some(aud_link.appsrc),
// })
// }
// Convenice funcion to make an element or provide a message
// about what plugin is missing
+6 -4
View File
@@ -233,7 +233,8 @@ async fn apply_users(rtsp: &NeoRtspServer, curr_users: &HashSet<UserConfig>) ->
// Add those missing
for user in curr_users.iter() {
log::debug!("Adding user {} to rtsp server", user.name);
rtsp.add_user(&user.name, &user.pass).await?;
rtsp.add_user(&user.name, &user.pass.clone().unwrap_or_default())
.await?;
}
// Remove unused
let rtsp_users = rtsp.get_users().await?;
@@ -362,7 +363,7 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
log::debug!("{}: Preparing at {}", name, paths.join(", "));
supported_streams_1.wait_for(|ss| ss.contains(&StreamKind::Main)).await?;
stream_main(camera.stream(StreamKind::Main).await?, camera.clone(), rtsp, &permitted_users, &paths).await
stream_main(camera.clone(), StreamKind::Main, rtsp, &permitted_users, &paths).await
}, if active_streams.contains(&StreamKind::Main) => v,
v = async {
let name = camera.config().await?.borrow().name.clone();
@@ -396,7 +397,8 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
log::debug!("{}: Preparing at {}", name, paths.join(", "));
supported_streams_2.wait_for(|ss| ss.contains(&StreamKind::Sub)).await?;
stream_main(camera.stream(StreamKind::Sub).await?,camera.clone(), rtsp, &permitted_users, &paths).await
stream_main(camera.clone(), StreamKind::Sub, rtsp, &permitted_users, &paths).await
}, if active_streams.contains(&StreamKind::Sub) => v,
v = async {
let name = camera.config().await?.borrow().name.clone();
@@ -429,7 +431,7 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
log::debug!("{}: Preparing at {}", name, paths.join(", "));
supported_streams_3.wait_for(|ss| ss.contains(&StreamKind::Extern)).await?;
stream_main(camera.stream(StreamKind::Extern).await?,camera.clone(), rtsp, &permitted_users, &paths).await
stream_main(camera.clone(), StreamKind::Extern, rtsp, &permitted_users, &paths).await
}, if active_streams.contains(&StreamKind::Extern) => v,
else => {
// all disabled just wait here until config is changed
+11 -869
View File
@@ -1,309 +1,27 @@
use anyhow::{anyhow, Result};
use gstreamer::{prelude::*, ClockTime, FlowError};
use gstreamer_app::AppSrc;
use anyhow::anyhow;
use gstreamer_rtsp_server::prelude::*;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::{
sync::{broadcast::channel as broadcast, watch::channel as watch},
task::JoinSet,
time::{sleep, Duration},
};
use tokio_stream::{wrappers::BroadcastStream, Stream, StreamExt};
use tokio_util::sync::CancellationToken;
use std::collections::HashSet;
use crate::common::{Permit, StampedData, UseCounter, VidFormat};
use crate::{
common::{NeoInstance, StreamConfig, StreamInstance},
AnyResult,
};
use crate::{common::NeoInstance, AnyResult};
use neolink_core::bc_protocol::StreamKind;
use super::{factory::*, gst::NeoRtspServer};
#[derive(Clone)]
struct PauseAffectors {
motion: bool,
push: bool,
client: bool,
}
/// This handles the stream by activating and deacivating it as required
pub(super) async fn stream_main(
mut stream_instance: StreamInstance,
camera: NeoInstance,
rtsp: &NeoRtspServer,
users: &HashSet<String>,
paths: &[String],
) -> Result<()> {
let mut camera_config = camera.config().await?.clone();
let name = camera_config.borrow().name.clone();
let mut curr_pause;
loop {
let this_loop_cancel = CancellationToken::new();
let _drop_guard = this_loop_cancel.clone().drop_guard();
stream_instance.activate().await?;
// Wait for a valid stream format to be detected
stream_instance
.config
.wait_for(|config| config.vid_ready())
.await?;
// After vid give it 1s to look for audio
// Ignore timeout but check err
if let Ok(v) = tokio::time::timeout(
Duration::from_secs(1),
stream_instance.config.wait_for(|config| config.aud_ready()),
)
.await
{
v?;
}
curr_pause = camera_config.borrow().pause.clone();
let last_stream_config = stream_instance.config.borrow().clone();
let mut thread_stream_config = stream_instance.config.clone();
let (pause_affector_tx, pause_affector) = watch(PauseAffectors {
motion: false,
push: false,
client: false,
});
let pause_affector_tx = Arc::new(pause_affector_tx);
let mut set = JoinSet::<AnyResult<()>>::new();
// Handles the on off of the stream with the client pause
let client_counter = UseCounter::new().await;
let client_count = client_counter.create_deactivated().await?;
// Client count affector
if curr_pause.on_disconnect {
let thread_name = name.clone();
let client_count = client_counter.create_deactivated().await?;
let thread_pause_affector_tx = pause_affector_tx.clone();
let cancel = this_loop_cancel.clone();
set.spawn(async move {
tokio::select! {
_ = cancel.cancelled() => AnyResult::Ok(()),
v = async {
loop {
client_count.aquired_users().await?;
log::info!("{}: Enabling Client", thread_name);
thread_pause_affector_tx.send_modify(|current| {
current.client = true;
});
client_count.dropped_users().await?;
log::info!("{}: Pausing Client", thread_name);
thread_pause_affector_tx.send_modify(|current| {
current.client = false;
});
}
} => v,
}
});
}
// Motion affector
if curr_pause.on_motion {
let thread_name = name.clone();
let thread_pause_affector_tx = pause_affector_tx.clone();
let cancel = this_loop_cancel.clone();
let mut motion = camera.motion().await?;
let delta = Duration::from_secs_f64(curr_pause.motion_timeout);
set.spawn(async move {
tokio::select! {
_ = cancel.cancelled() => AnyResult::Ok(()),
v = async {
loop {
motion
.wait_for(|md| matches!(md, crate::common::MdState::Start(_)))
.await?;
log::info!("{}: Enabling Motion", thread_name);
thread_pause_affector_tx.send_modify(|current| {
current.motion = true;
});
motion
.wait_for(
|md| matches!(md, crate::common::MdState::Stop(n) if n.elapsed()>delta),
)
.await?;
log::info!("{}: Pausing Motion", thread_name);
thread_pause_affector_tx.send_modify(|current| {
current.motion = false;
});
}
} => v,
}
});
// Push notfications
#[cfg(feature = "pushnoti")]
{
let mut pn = camera.push_notifications().await?;
let mut curr_pn = None;
let thread_name = name.clone();
let thread_pause_affector_tx = pause_affector_tx.clone();
let cancel = this_loop_cancel.clone();
set.spawn(async move {
tokio::select! {
_ = cancel.cancelled() => AnyResult::Ok(()),
v = async {
loop {
curr_pn = pn
.wait_for(|pn| pn != &curr_pn && pn.is_some())
.await?
.clone();
log::info!("{}: Enabling Push Notification", thread_name);
thread_pause_affector_tx.send_modify(|current| {
current.push = true;
});
tokio::select! {
v = pn.wait_for(|pn| pn != &curr_pn && pn.is_some()) => {
v?;
// If another PN during wait then go back to wait more
continue;
}
_ = sleep(Duration::from_secs(30)) => {}
}
log::info!("{}: Pausing Push Notification", thread_name);
thread_pause_affector_tx.send_modify(|current| {
current.push = false;
});
}
} => v,
}
});
}
}
if curr_pause.on_motion || curr_pause.on_disconnect {
// Take over activation
let cancel = this_loop_cancel.clone();
let mut client_activator = stream_instance.activator_handle().await;
client_activator.deactivate().await?;
stream_instance.deactivate().await?;
let mut pause_affector = tokio_stream::wrappers::WatchStream::new(pause_affector);
let thread_curr_pause = curr_pause.clone();
set.spawn(async move {
tokio::select! {
_ = cancel.cancelled() => AnyResult::Ok(()),
v = async {
while let Some(state) = pause_affector.next().await {
if thread_curr_pause.on_motion && thread_curr_pause.on_disconnect {
if state.client && (state.motion || state.push) {
client_activator.activate().await?;
} else {
client_activator.deactivate().await?;
}
} else if thread_curr_pause.on_motion {
if state.motion || state.push {
client_activator.activate().await?;
} else {
client_activator.deactivate().await?;
}
} else if thread_curr_pause.on_disconnect {
if state.client {
client_activator.activate().await?;
} else {
client_activator.deactivate().await?;
}
} else {
unreachable!()
}
}
AnyResult::Ok(())
} => v,
}
});
}
// This thread jsut keeps it active for 5s after an initial start to build the buffer
let cancel = this_loop_cancel.clone();
let mut init_activator = stream_instance.activator_handle().await;
let init_camera = camera.clone();
set.spawn(async move {
tokio::select! {
_ = cancel.cancelled() => AnyResult::Ok(()),
v = async {
init_activator.activate().await?;
let _ = init_camera
.run_task(|_| {
Box::pin(async move {
sleep(Duration::from_secs(5)).await;
AnyResult::Ok(())
})
})
.await;
init_activator.deactivate().await?;
AnyResult::Ok(())
} => v,
}
});
// Task to just report the number of clients for debug purposes
let cancel = this_loop_cancel.clone();
let counter = client_counter.create_deactivated().await?;
let mut cur_count = 0;
set.spawn(async move {
tokio::select! {
_ = cancel.cancelled() => AnyResult::Ok(()),
v = async {
loop {
cur_count = *counter.get_counter().wait_for(|v| v != &cur_count).await?;
log::trace!("cur_count: {cur_count:?}");
}
} => v,
}
});
// This runs the actual stream.
// The select will restart if the stream's config updates
break tokio::select! {
v = thread_stream_config.wait_for(|new_conf| new_conf != &last_stream_config) => {
let v = v?;
// If stream config changes we reload the stream
log::info!("{}: Stream Configuration Changed. Reloading Streams", &name);
log::trace!(" From {:?} to {:?}", last_stream_config, v.clone());
continue;
},
v = camera_config.wait_for(|new_conf| new_conf.pause != curr_pause ) => {
v?;
// If pause config changes restart
log::info!("{}: Pause Configuration Changed. Reloading Streams", &name);
continue;
},
v = stream_run(&name, &stream_instance, rtsp, &last_stream_config, users, paths, client_count) => v,
};
}
}
/// This handles the stream itself by creating the factory and pushing messages into it
async fn stream_run(
name: &str,
stream_instance: &StreamInstance,
pub(crate) async fn stream_main(
camera: NeoInstance,
stream: StreamKind,
rtsp: &NeoRtspServer,
stream_config: &StreamConfig,
users: &HashSet<String>,
paths: &[String],
client_count: Permit,
) -> AnyResult<()> {
let vidstream = stream_instance.vid.resubscribe();
let audstream = stream_instance.aud.resubscribe();
let vid_history = stream_instance.vid_history.clone();
let aud_history = stream_instance.aud_history.clone();
// Finally ready to create the factory and connect the stream
let name = camera.config().await?.borrow().name.clone();
// Create the factory and connect the stream
let mounts = rtsp
.mount_points()
.ok_or(anyhow!("RTSP server lacks mount point"))?;
// Create the factory
let (factory, mut client_rx) = make_factory(stream_config).await?;
let (factory, thread) = make_factory(camera, stream).await?;
factory.add_permitted_roles(users);
@@ -313,582 +31,6 @@ async fn stream_run(
}
log::info!("{}: Available at {}", name, paths.join(", "));
let stream_cancel = CancellationToken::new();
let drop_guard = stream_cancel.clone().drop_guard();
let mut set = JoinSet::new();
// Wait for new media client data to come in from the factory
while let Some(mut client_data) = client_rx.recv().await {
// New media created
let vid = client_data.vid.take().map(|data| data.app);
let aud = client_data.aud.take().map(|data| data.app);
// This is the data that gets sent to gstreamer thread
// It represents the combination of the camera stream and the appsrc seek messages
// At 30fps for 15s with audio you need about 900 frames
// Therefore the buffer is rather large at 2000
let (aud_data_tx, aud_data_rx) = broadcast(2000);
let (vid_data_tx, vid_data_rx) = broadcast(2000);
// This thread takes the video data from the cam and passed it into the stream
let mut vidstream = BroadcastStream::new(vidstream.resubscribe());
let thread_vid_data_tx = vid_data_tx.clone();
let thread_stream_cancel = stream_cancel.clone();
let thread_vid_history = vid_history.clone();
set.spawn(async move {
let r = tokio::select! {
_ = thread_stream_cancel.cancelled() => AnyResult::Ok(()),
v = async {
// Send Initial
{
let history = thread_vid_history.borrow();
// let last_ts = history.back().map(|s| s.ts);
for data in history.iter() {
thread_vid_data_tx.send(
// StampedData {
// keyframe: data.keyframe,
// data: data.data.clone(),
// ts: last_ts.unwrap()
// }
data.clone()
)?;
}
}
// Send new
while let Some(frame) = vidstream.next().await {
if let Ok(data) = frame {
thread_vid_data_tx.send(
data
)?;
}
};
AnyResult::Ok(())
} => v,
};
log::trace!("Stream Vid Media End {r:?}");
AnyResult::Ok(())
});
// This thread takes the audio data from the cam and passed it into the stream
let mut audstream = BroadcastStream::new(audstream.resubscribe());
let thread_stream_cancel = stream_cancel.clone();
let thread_aud_data_tx = aud_data_tx.clone();
let thread_aud_history = aud_history.clone();
set.spawn(async move {
let r = tokio::select! {
_ = thread_stream_cancel.cancelled() => AnyResult::Ok(()),
v = async {
// Send Initial
{
let history = thread_aud_history.borrow();
// let last_ts = history.back().map(|s| s.ts);
for data in history.iter() {
thread_aud_data_tx.send(
// StampedData {
// keyframe: data.keyframe,
// data: data.data.clone(),
// ts: last_ts.unwrap()
// }
data.clone()
)?;
}
}
// Send new
while let Some(frame) = audstream.next().await {
if let Ok(data) = frame {
thread_aud_data_tx.send(
data
)?;
}
};
AnyResult::Ok(())
} => v,
};
log::trace!("Stream Aud Media End: {r:?}");
AnyResult::Ok(())
});
// Handles sending the video data into gstreamer
let thread_stream_cancel = stream_cancel.clone();
let vid_data_rx = BroadcastStream::new(vid_data_rx).filter(|f| f.is_ok()); // Filter to ignore lagged
let thread_vid = vid.clone();
let mut thread_client_count = client_count.subscribe();
let thread_format = stream_config.vid_format;
let (ts_tx, ts_rx) = tokio::sync::watch::channel(Duration::ZERO);
// let fallback_time = Duration::from_secs(3);
let framerate =
Duration::from_millis(1000u64 / std::cmp::max(stream_config.fps as u64, 5u64));
if let Some(thread_vid) = thread_vid {
set.spawn(async move {
thread_client_count.activate().await?;
let r = tokio::select! {
_ = thread_stream_cancel.cancelled() => {
AnyResult::Ok(())
},
v = send_to_appsrc(
pad_vid(
// insert_filler(
frametime_stream(
sync_stream(
wait_for_keyframe(
vid_data_rx,
),
ts_tx,
),
framerate
),
// thread_format,
// framerate,
// ),
thread_format,
),
&thread_vid
) => {
v
},
};
drop(thread_client_count);
let _ = thread_vid.end_of_stream();
r
});
}
// Handles the audio data into gstreamer
let thread_stream_cancel = stream_cancel.clone();
let aud_data_rx = BroadcastStream::new(aud_data_rx).filter(|f| f.is_ok()); // Filter to ignore lagged
let thread_aud = aud.clone();
let aud_framerate =
Duration::from_millis(1000u64 / std::cmp::max(stream_config.fps as u64, 5u64));
if let Some(thread_aud) = thread_aud {
set.spawn(async move {
let r = tokio::select! {
_ = thread_stream_cancel.cancelled() => {
AnyResult::Ok(())
},
v = send_to_appsrc(
frametime_stream(
hold_stream(
wait_for_keyframe(
aud_data_rx
),
ts_rx,
),
aud_framerate),
&thread_aud) => {
v
},
};
let _ = thread_aud.end_of_stream();
r
});
}
}
// At this point the factory has been destroyed
// Cancel any remaining threads that are trying to send data
// Although it should be finished already when the appsrcs are dropped
stream_cancel.cancel();
drop(drop_guard);
while set.join_next().await.is_some() {}
log::trace!("Stream done");
AnyResult::Ok(())
}
fn check_live(app: &AppSrc) -> Result<()> {
app.bus().ok_or(anyhow!("App source is closed"))?;
app.pads()
.iter()
.all(|pad| pad.is_linked())
.then_some(())
.ok_or(anyhow!("App source is not linked"))
}
#[allow(dead_code)]
fn get_runtime(app: &AppSrc) -> Option<Duration> {
if let Some(clock) = app.clock() {
if let Some(time) = clock.time() {
if let Some(base_time) = app.base_time() {
let runtime = time.saturating_sub(base_time);
return Some(Duration::from_micros(runtime.useconds()));
}
}
}
None
}
// This ensures we start at a keyframe
fn wait_for_keyframe<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
Box::pin(async_stream::stream! {
let mut found_key = false;
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
if frame.keyframe || found_key {
found_key = true;
yield Ok(frame);
}
}
}
})
}
// Take a stream of stamped data and release them
// only when they are after a certain time stamp
// This time stamp is sent via a shared watcher
//
// This is used to ensure that the audio does not run
// ahead of the video too much
fn hold_stream<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
mut vid_ts: tokio::sync::watch::Receiver<Duration>,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
Box::pin(async_stream::stream! {
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
if frame.ts >= *vid_ts.borrow() {
yield Ok(frame);
} else {
// Can't seem to throw an error here as it will cause
// a borrow checker to hold over the await
let _ = vid_ts.wait_for(|o| *o >= frame.ts).await;
yield Ok(frame);
}
}
}
})
}
/// This is the counter part to [`hold_stream`]
///
/// It just updates the ts in the watcher
fn sync_stream<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
vid_ts: tokio::sync::watch::Sender<Duration>,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
Box::pin(async_stream::stream! {
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
vid_ts.send_replace(frame.ts);
yield Ok(frame);
}
}
})
}
// Take a stream of stamped data and reorder it
// in case they are out of order
// this also releases frames in waves of keyframe so it should replace `hold_stream`
#[allow(dead_code)]
fn ensure_order<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
Box::pin(async_stream::stream! {
let mut frame_buffer: Vec<StampedData> = vec![];
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
if ! frame.keyframe {
// Buffer until keyframe and reorder
let mut reorder_buffer = vec![];
while frame_buffer.last().is_some_and(|v| v.ts > frame.ts) {
reorder_buffer.push(frame_buffer.pop().unwrap());
}
frame_buffer.push(frame);
while let Some(frame) = reorder_buffer.pop() {
frame_buffer.push(frame);
}
} else {
// On key frame flush out
for frame in frame_buffer.drain(..) {
yield Ok(frame);
}
yield(Ok(frame));
}
}
}
})
}
// Take a stream of stamped data pause until
// it is time to display it
fn frametime_stream<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
expected_frame_rate: Duration,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
Box::pin(async_stream::stream! {
let mut ts_before = Duration::MAX;
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
if frame.ts < ts_before {
ts_before = frame.ts;
}
let wait = std::cmp::min(frame.ts.saturating_sub(ts_before), expected_frame_rate);
tokio::time::sleep(wait).await;
ts_before = frame.ts;
yield Ok(frame);
}
}
})
}
#[allow(dead_code)]
/// Insert filler into the video stream when no frames are comming
/// this should help the stream not be considered dead while
/// we wait for the reconnect
fn insert_filler<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
format: VidFormat,
delay: Duration,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
let mut last_ts = Duration::ZERO;
Box::pin(async_stream::stream! {
while let Some(frame) = tokio::select!{
v = stream.next() => {
v
},
_ = tokio::time::sleep(delay) => {
log::trace!("Filling");
match format {
VidFormat::H264 => {
Some(Ok(StampedData {
data: Arc::new(h264_filler(4096)),
keyframe: false,
ts: last_ts,
}))
}
VidFormat::H265 => {
Some(Ok(StampedData {
data: Arc::new(h265_filler(4096)),
keyframe: false,
ts: last_ts,
}))
}
VidFormat::None => unreachable!(),
}
}
} {
if let Ok(frame) = frame {
last_ts = frame.ts;
yield Ok(frame);
}
}
})
}
fn h265_filler(size: usize) -> Vec<u8> {
assert!(size >= 6);
let mut buf = vec![0x0, 0x0, 0x1, 0b01001100, 0x0];
buf.resize(size, 0xFF);
buf[size - 1] = 0x80;
buf
}
fn h264_filler(size: usize) -> Vec<u8> {
assert!(size >= 5);
let mut buf = vec![0x0, 0x0, 0x1, 0xC];
buf.resize(size, 0xFF);
buf[size - 1] = 0x80;
buf
}
/// Takes a stream and pads it with filler blocks up to 4kb in h265 or h264 format
fn pad_vid<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
format: VidFormat,
) -> impl Stream<Item = AnyResult<StampedData>> + Unpin {
let min_pad: usize = match format {
VidFormat::H264 => 5,
VidFormat::H265 => 6,
VidFormat::None => unreachable!(),
};
Box::pin(async_stream::stream! {
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
if (frame.data.len() % 4096) != 0 {
let pad_size: usize = (frame.data.len() + min_pad).div_ceil(4096) * 4096 - frame.data.len();
let frame = StampedData {
keyframe: frame.keyframe,
data: Arc::new(
match format {
VidFormat::H264 => {
frame.data.iter().chain(
h264_filler(pad_size).iter()
).copied().collect()
}
VidFormat::H265 => {
frame.data.iter().chain(
h265_filler(pad_size).iter()
).copied().collect()
}
VidFormat::None => unreachable!(),
}
),
ts: frame.ts,
};
yield Ok(frame);
} else {
yield Ok(frame);
}
}
}
})
}
#[allow(dead_code)]
// This will take a stream and if there is a notibable lack of data
// then it will repeat the last keyframe (if there have been no
// pframes in between)
fn repeat_keyframe<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
fallback_time: Duration,
frame_rate: Duration,
) -> impl Stream<Item = Result<StampedData, E>> + Unpin {
Box::pin(async_stream::stream! {
let mut was_repeating = false;
while let Some(frame) = stream.next().await {
if let Ok(frame) = frame {
if frame.keyframe {
let repeater = frame.clone();
yield Ok(frame);
// Wait for either timeout or a new frame
let mut fallback_time = fallback_time;
loop {
tokio::select!{
v = stream.next() => {
if let Some(frame) = v {
if let Ok(frame) = frame {
if was_repeating {
was_repeating = false;
}
yield Ok(frame);
break;
}
} else {
break;
}
},
_ = sleep(fallback_time) => {
if !was_repeating {
// This way we only print once
was_repeating = true;
}
fallback_time = frame_rate;
yield Ok(repeater.clone());
}
}
}
} else {
// P frames go through as-is
yield Ok(frame);
}
}
}
})
}
/// Takes a stream and sends it to an appsrc
async fn send_to_appsrc<E, T: Stream<Item = Result<StampedData, E>> + Unpin>(
mut stream: T,
appsrc: &AppSrc,
) -> AnyResult<()> {
let mut ts_0 = Duration::MAX;
let mut wait_for_iframe = true;
let mut pools: HashMap<usize, gstreamer::BufferPool> = Default::default();
let mut paused = true;
appsrc.set_state(gstreamer::State::Paused).unwrap();
let (tx, mut rx) = tokio::sync::mpsc::channel::<StampedData>(2000);
// Run blocking code on a seperate thread
let appsrc = appsrc.clone();
std::thread::spawn(move || {
let r = (move || {
while let Some(data) = rx.blocking_recv() {
check_live(&appsrc)?; // Stop if appsrc is dropped
if wait_for_iframe && !data.keyframe {
continue;
} else if wait_for_iframe {
wait_for_iframe = false;
}
if ts_0 > data.ts {
ts_0 = data.ts;
}
let rt = data.ts - ts_0;
log::trace!(
"Sending frame with TimeStamp: {:?} on {}",
rt,
appsrc.name()
);
let buf = {
// let mut gst_buf = pool.acquire_buffer(None).unwrap();
let msg_size = data.data.len();
let pool = pools.entry(msg_size).or_insert_with_key(|size| {
let pool = gstreamer::BufferPool::new();
let mut pool_config = pool.config();
pool_config.set_params(None, (*size) as u32, 8, 0);
pool.set_config(pool_config).unwrap();
// let (allocator, alloc_parms) = pool.allocator().unwrap();
pool.set_active(true).unwrap();
pool
});
let mut gst_buf = pool.acquire_buffer(None).unwrap();
// let mut gst_buf = gstreamer::Buffer::with_size(data.data.len()).unwrap();
{
let gst_buf_mut = gst_buf.get_mut().unwrap();
let time = ClockTime::from_useconds(rt.as_micros() as u64);
// gst_buf_mut.set_dts(ClockTime::from_useconds(dts));
gst_buf_mut.set_dts(time);
gst_buf_mut.set_pts(time);
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
gst_buf_data.copy_from_slice(data.data.as_slice());
}
gst_buf
};
match appsrc.push_buffer(buf) {
Ok(_) => {
// log::info!(
// "Send {}{} on {}",
// data.data.len(),
// if data.keyframe { " (keyframe)" } else { "" },
// appsrc.name()
// );
Ok(())
}
Err(FlowError::Flushing) => {
// Buffer is full just skip
//
// But ensure we start with an iframe to reduce gray screens
wait_for_iframe = true;
log::info!("Buffer full on {}", appsrc.name());
Ok(())
}
Err(e) => Err(anyhow!("Error in streaming: {e:?}")),
}?;
if appsrc.current_level_bytes() >= appsrc.max_bytes() * 2 / 3 && paused {
appsrc.set_state(gstreamer::State::Playing).unwrap();
paused = false;
} else if appsrc.current_level_bytes() <= appsrc.max_bytes() / 3 && !paused {
appsrc.set_state(gstreamer::State::Paused).unwrap();
paused = true;
}
}
AnyResult::Ok(())
})();
log::trace!("r: {:?}", r);
r
});
// Send to the blocking thread
while let Some(Ok(data)) = stream.next().await {
// Start on iframes
if tx.send(data).await.is_err() {
break;
}
}
thread.await??;
Ok(())
}