Skip to content

Commit c003c7d

Browse files
authored
Merge pull request #30 from piotrpdev/OKO-106-Automatic-Camera-Detection
2 parents ba69fcd + b80854e commit c003c7d

File tree

10 files changed

+364
-27
lines changed

10 files changed

+364
-27
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Made using the following resources:
6262
| [gatekeeper source code][gatekeeper] | [Unlicense][gatekeeper-license] |
6363
| [`esp-camera-rs` package fork][cam-rs] | [MIT][cam-rs-license] |
6464
| [Geist font][geist] | [OFL-1.1][geist-license] |
65+
|[`mdns` examples][mdns] | [MIT][mdns-license] |
6566

6667
[^1]: [*"...this solution can be customized to suit your particular requirements.
6768
Don’t hesitate to make adjustments and employ this code according to your
@@ -116,5 +117,7 @@ video-to-image conversion needs."*][video2image-medium]
116117
[cam-rs-license]: https://github.com/hnz1102/esp-camera-rs/blob/main/LICENSE
117118
[geist]: https://github.com/vercel/geist-font
118119
[geist-license]: https://github.com/vercel/geist-font/blob/main/OFL.txt
120+
[mdns]: https://github.com/dylanmckay/mdns/tree/master/examples
121+
[mdns-license]: https://github.com/dylanmckay/mdns/blob/master/LICENSE
119122

120123
<!-- https://eslint.org/docs/latest/use/configure/language-options -->

backend/Cargo.lock

Lines changed: 119 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ tokio-util = { version = "0.7.12", features = ["rt"] }
4141
axum-embed = "0.1.0"
4242
rust-embed = "8.5.0"
4343
serde_bytes = "0.11.15"
44+
mdns = { git = "https://github.com/PhysicalGraph/mdns.git", rev = "6de7b76434f3fc4f5702322b995ac66963b2c60f" } # This fork adds back tokio support
45+
tokio-stream = { version = "0.1.17", features = ["sync"] }
46+
async-stream = "0.3.6"
4447

4548
[dev-dependencies]
4649
playwright = { version = "0.0.20", default-features = false, features = ["rt-tokio"] }

backend/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use futures_util::{Stream, StreamExt};
2+
use tokio_util::sync::CancellationToken;
13
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
24

35
pub use crate::web::{ApiChannelMessage, App, ImageContainer};
@@ -12,6 +14,33 @@ pub use {
1214
db::VideoCameraView,
1315
};
1416

17+
// Taken from https://github.com/hyperium/hyper/issues/2787#issuecomment-1073229886
18+
/// Run a stream until it completes or we receive the shutdown signal.
19+
///
20+
/// Uses the `async-stream` to make things easier to write.
21+
pub fn or_until_shutdown<S>(
22+
stream: S,
23+
shutdown_token: CancellationToken,
24+
) -> impl Stream<Item = S::Item>
25+
where
26+
S: Stream,
27+
{
28+
async_stream::stream! {
29+
futures_util::pin_mut!(stream);
30+
31+
loop {
32+
tokio::select! {
33+
Some(item) = stream.next() => {
34+
yield item
35+
}
36+
() = shutdown_token.cancelled() => {
37+
break;
38+
}
39+
}
40+
}
41+
}
42+
}
43+
1544
pub async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
1645
// TODO: Improve this, log to file, etc.
1746
// TODO: Use tracing wherever '?' is used.

backend/src/web.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub use app::App;
2+
pub use app::AppState;
23
pub use app::ImageContainer;
34
use serde::Deserialize;
45
use serde::Serialize;
@@ -21,6 +22,12 @@ pub enum ApiChannelMessage {
2122
Initial,
2223
}
2324

25+
#[derive(Serialize, Deserialize, Debug, Clone)]
26+
pub enum MdnsChannelMessage {
27+
ServiceDiscovered { mdns_response: mdns::Response },
28+
Initial,
29+
}
30+
2431
mod app;
2532
mod auth;
2633
mod protected;

0 commit comments

Comments
 (0)