From e3ac3089aa548a30b20bbdd36c0951f08f091e70 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 3 Aug 2021 18:28:55 +0700 Subject: [PATCH] Add talk to neolink_core --- crates/core/src/bc/model.rs | 4 ++ crates/core/src/bc/xml.rs | 48 ++++++++++++- crates/core/src/bc_protocol.rs | 1 + crates/core/src/bc_protocol/talk.rs | 106 ++++++++++++++++++++++++++++ crates/core/src/bcmedia/ser.rs | 1 - dissector/messages.md | 72 +++++++++++++++++++ 6 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 crates/core/src/bc_protocol/talk.rs diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 60c91e8..41c1fa2 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -20,6 +20,10 @@ pub const MSG_ID_PING: u32 = 93; pub const MSG_ID_GET_GENERAL: u32 = 104; /// Setting general system info (clock mostly) messages have this ID pub const MSG_ID_SET_GENERAL: u32 = 105; +/// Will send the talk config for talk back data to follow this msg +pub const MSG_ID_TALKCONFIG: u32 = 201; +/// Used to send talk back binary data +pub const MSG_ID_TALK: u32 = 202; /// Getting the LED status is done with this ID pub const MSG_ID_GET_LED_STATUS: u32 = 208; /// Setting the LED status is done with this ID diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index 0ddc393..edfca2b 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -60,6 +60,9 @@ pub struct BcXml { /// Received as part of the LEDState info request #[yaserde(rename = "LedState")] pub led_state: Option, + /// Sent as part of the TalkConfig to prepare the camera for audio talk-back + #[yaserde(rename = "TalkConfig")] + pub talk_config: Option, } impl BcXml { @@ -285,7 +288,7 @@ pub struct LedState { pub version: String, /// Channel ID of camera to get/set its LED state #[yaserde(rename = "channelId")] - pub channel_id: u32, + pub channel_id: u8, /// LED Version, observed value is "2". Should be None when setting the LedState #[yaserde(rename = "ledVersion")] pub led_version: Option, @@ -296,6 +299,49 @@ pub struct LedState { pub light_state: String, } +/// TalkConfig xml +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct TalkConfig { + /// XML Version + #[yaserde(attribute)] + pub version: String, + /// Channel ID of camera to set the TalkConfig + #[yaserde(rename = "channelId")] + pub channel_id: u8, + /// Duplex known values `"FDX"` + pub duplex: String, + /// audioStreamMode known values `"followVideoStream"` + #[yaserde(rename = "audioStreamMode")] + pub audio_stream_mode: String, + /// AudioConfig contans the details of the audio to follow + pub audio_config: AudioConfig, +} + +/// audioConfig xml +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +#[yaserde(rename = "audioConfig")] +pub struct AudioConfig { + /// Audio type known values are `"adpcm"` + /// + /// Do not expect camera to support anything else. + #[yaserde(rename = "audioType")] + pub audio_type: String, + /// Audio sample rate known values are `16000` + #[yaserde(rename = "sampleRate")] + pub sample_rate: u16, + /// Precision of data known vaues are `16` (i.e. 16bit) + #[yaserde(rename = "samplePrecision")] + pub sample_precision: u16, + /// Number of audio samples this should be twice the block size for adpcm + #[yaserde(rename = "lengthPerEncoder")] + pub length_per_encoder: u16, + /// Sound track is the number of tracks known values are `"mono"` + /// + /// Do not expect camera to support anything else + #[yaserde(rename = "soundTrack")] + pub soundTrack: String, +} + /// Convience function to return the xml version used throughout the library pub fn xml_ver() -> String { "1.1".to_string() diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index ee506a7..e0286c5 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -17,6 +17,7 @@ mod logout; mod ping; mod reboot; mod stream; +mod talk; mod time; mod version; diff --git a/crates/core/src/bc_protocol/talk.rs b/crates/core/src/bc_protocol/talk.rs new file mode 100644 index 0000000..425b265 --- /dev/null +++ b/crates/core/src/bc_protocol/talk.rs @@ -0,0 +1,106 @@ +use super::{BcCamera, Error, Result, RX_TIMEOUT}; +use crate::{bc::model::*, bc::xml::*, bcmedia::model::*}; + +impl BcCamera { + /// + /// Send sound to the camera + /// + /// long description + /// + /// # Parameters + /// + /// * `adpcm` - Data must be adpcm in DVI-4 format + /// + /// * `block_size` - is block-align used to encode the adpcm + /// + /// * `sample_rate` - sample rate of the audio + /// + /// + pub fn talk(&self, adpcm: &[u8], block_size: u16, sample_rate: u16) -> Result<()> { + let connection = self.connection.as_ref().expect("Must be connected to ping"); + + let sub = connection.subscribe(MSG_ID_TALKCONFIG)?; + + let talk_config = TalkConfig { + channel_id: self.channel_id, + duplex: "FDX".to_string(), + audio_stream_mode: "followVideoStream".to_string(), + audio_config: AudioConfig { + audio_type: "adpcm".to_string(), + sample_rate, + sample_precision: 16, + length_per_encoder: block_size * 2, + soundTrack: "mono".to_string(), + }, + ..Default::default() + }; + + let msg = Bc { + meta: BcMeta { + msg_id: MSG_ID_TALKCONFIG, + channel_id: self.channel_id, + msg_num: self.new_message_num(), + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: Some(Extension { + channel_id: Some(self.channel_id), + ..Default::default() + }), + payload: Some(BcPayloads::BcXml(BcXml { + talk_config: Some(talk_config), + ..Default::default() + })), + }), + }; + + sub.send(msg)?; + let msg = sub.rx.recv_timeout(RX_TIMEOUT)?; + + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + } else { + return Err(Error::UnintelligibleReply { + reply: msg, + why: "The camera did not except the TalkConfig xml", + }); + } + + let full_block_size = block_size + 4; // Block size + predictor state + let sub = connection.subscribe(MSG_ID_TALK)?; + + for bytes in adpcm.chunks(full_block_size as usize) { + let bcmedia_adpcm = BcMedia::Adpcm(BcMediaAdpcm { + data: bytes.to_vec(), + }); + let bcmedia_adpcm_data = bcmedia_adpcm.serialize(vec![])?; + + let msg = Bc { + meta: BcMeta { + msg_id: MSG_ID_TALK, + channel_id: self.channel_id, + msg_num: self.new_message_num(), + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: Some(Extension { + channel_id: Some(self.channel_id), + binary_data: Some(1), + ..Default::default() + }), + payload: Some(BcPayloads::Binary(bcmedia_adpcm_data)), + }), + }; + + sub.send(msg)?; + } + + Ok(()) + } +} diff --git a/crates/core/src/bcmedia/ser.rs b/crates/core/src/bcmedia/ser.rs index 6da70e6..e5bb801 100644 --- a/crates/core/src/bcmedia/ser.rs +++ b/crates/core/src/bcmedia/ser.rs @@ -19,7 +19,6 @@ pub enum Error { } impl BcMedia { - #[allow(dead_code)] pub(crate) fn serialize(&self, buf: W) -> Result { let (buf, _) = match &self { BcMedia::InfoV1(payload) => gen(bcmedia_info_v1(payload), buf)?, diff --git a/dissector/messages.md b/dissector/messages.md index c200b2e..c5a46d2 100644 --- a/dissector/messages.md +++ b/dissector/messages.md @@ -2087,6 +2087,78 @@ a PR to improve it. ``` +- 201: `` + + - Client + + - Header + + | Magic | Message ID | Message Length | Encryption Offset | Status Code | Message Class | Binary Offset | + |--------------|--------------|----------------|-------------------|-------------------|---------------|---------------| + | 0a bc de f0 | 00 00 00 c9 | 00 00 01 f2 | 12 00 00 00 | 00 00 | 64 14 | 00 00 00 68 | + + - Body + ```xml + + + 0 + + ``` + + - Binary + + ```xml + + + + 0 + FDX + followVideoStream + + adpcm + 16000 + 16 + 1024 + mono + + + + ``` + + - Camera + + - Header + + | Magic | Message ID | Message Length | Encryption Offset | Status Code | Message Class | Binary Offset | + |--------------|--------------|----------------|-------------------|-------------------|---------------|---------------| + | 0a bc de f0 | 00 00 00 c9 | 00 00 00 00 | 12 00 00 00 | c8 00 | 00 00 | 00 00 00 00 | + +- 202: `Talk` + + - Client + + - Header + + | Magic | Message ID | Message Length | Encryption Offset | Status Code | Message Class | Binary Offset | + |--------------|--------------|----------------|-------------------|-------------------|---------------|---------------| + | 0a bc de f0 | 00 00 00 ca | 00 00 08 c3 | 00 00 00 00 | 00 00 | 64 14 | 00 00 00 83 | + + - Body + ```xml + + + 1 + 0 + + ``` + + - Binary + + Binary data contains media-packets of adpcm data + + + **Notes**: No reply from camera. After this the client keeps sending this packet with binary in the BcMedia encoded packets of adpcm data + - 208: `` - Client