From 4bfb9738cba8bc29749b60093debfd6a00a72a84 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 21 Jun 2022 11:04:27 +0700 Subject: [PATCH] Add stream stop message --- crates/core/src/bc/model.rs | 2 + .../core/src/bc_protocol/connection/bcconn.rs | 4 +- crates/core/src/bc_protocol/stream.rs | 75 +++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 6f1b8be..ee6ade4 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -10,6 +10,8 @@ pub const MSG_ID_LOGIN: u32 = 1; pub const MSG_ID_LOGOUT: u32 = 2; /// Video and Audio Streams messages have this ID pub const MSG_ID_VIDEO: u32 = 3; +/// Video and Audio Streams messages have this ID when a stop is requested +pub const MSG_ID_VIDEO_STOP: u32 = 4; /// TalkAbility messages have this ID pub const MSG_ID_TALKABILITY: u32 = 10; /// TalkReset messages have this ID diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index 48c8524..9d4e938 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -14,8 +14,8 @@ use std::thread::JoinHandle; use std::time::{Duration, Instant}; /// A shareable connection to a camera. Handles serialization of messages. To send/receive, call -/// .[subscribe()] with a message ID. You can use the BcSubscription to send or receive only -/// messages with that ID; each incoming message is routed to its appropriate subscriber. +/// .[subscribe()] with a message number. You can use the BcSubscription to send or receive only +/// messages with that number; each incoming message is routed to its appropriate subscriber. /// /// There can be only one subscriber per kind of message at a time. pub struct BcConnection { diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index 560f364..4c53488 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -123,4 +123,79 @@ impl BcCamera { data_outs.write(bc_media)?; } } + + /// Stop a camera from sending more stream data. + pub fn stop_video(&self, stream: Stream) -> Result<()> { + let connection = self + .connection + .as_ref() + .expect("Must be connected to stop video"); + let msg_num = self.new_message_num(); + let sub_video = connection.subscribe(msg_num)?; + + // On an E1 and swann cameras: + // - mainStream always has a value of 0 + // - subStream always has a value of 1 + // - There is no externStram + // On a B800: + // - mainStream is 0 + // - subStream is 0 + // - externStream is 0 + let stream_code = match stream { + Stream::Main => 0, + Stream::Sub => 1, + Stream::Extern => 0, + }; + + // Theses are the numbers used with the offical client + // On an E1 and swann cameras: + // - mainStream always has a value of 0 + // - subStream always has a value of 1 + // - There is no externStram + // On a B800: + // - mainStream is 0 + // - subStream is 256 + // - externStram is 1024 + let handle = match stream { + Stream::Main => 0, + Stream::Sub => 256, + Stream::Extern => 1024, + }; + + let stream_name = match stream { + Stream::Main => "mainStream", + Stream::Sub => "subStream", + Stream::Extern => "externStream", + } + .to_string(); + + let stop_video = Bc::new_from_xml( + BcMeta { + msg_id: MSG_ID_VIDEO_STOP, + channel_id: self.channel_id, + msg_num, + stream_type: stream_code, + response_code: 0, + class: 0x6414, // IDK why + }, + BcXml { + preview: Some(Preview { + version: xml_ver(), + channel_id: self.channel_id, + handle, + stream_type: stream_name, + }), + ..Default::default() + }, + ); + + sub_video.send(stop_video)?; + + let reply = sub_video.rx.recv_timeout(crate::RX_TIMEOUT)?; + if reply.meta.response_code != 200 { + return Err(super::Error::CameraServiceUnavaliable); + } + + Ok(()) + } }