2019-03-20 14:36:10 +00:00
|
|
|
// encrypter.rs
|
|
|
|
//
|
|
|
|
// Copyright 2019 Jordan Petridis <jordan@centricular.com>
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-06-03 18:20:54 +00:00
|
|
|
use gst::glib;
|
2019-03-20 14:36:10 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
use gst::subclass::prelude::*;
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
use sodiumoxide::crypto::box_;
|
|
|
|
|
|
|
|
type BufferVec = SmallVec<[gst::Buffer; 16]>;
|
|
|
|
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
2020-11-22 14:24:55 +00:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
|
|
|
gst::DebugCategory::new(
|
|
|
|
"sodiumencrypter",
|
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
Some("Encrypter Element"),
|
|
|
|
)
|
|
|
|
});
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Props {
|
|
|
|
receiver_key: Option<glib::Bytes>,
|
|
|
|
sender_key: Option<glib::Bytes>,
|
|
|
|
block_size: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Props {
|
|
|
|
fn default() -> Self {
|
|
|
|
Props {
|
|
|
|
receiver_key: None,
|
|
|
|
sender_key: None,
|
|
|
|
block_size: 32768,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct State {
|
|
|
|
adapter: gst_base::UniqueAdapter,
|
|
|
|
nonce: box_::Nonce,
|
|
|
|
precomputed_key: box_::PrecomputedKey,
|
|
|
|
block_size: u32,
|
|
|
|
write_headers: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
fn from_props(props: &Props) -> Result<Self, gst::ErrorMessage> {
|
|
|
|
let sender_key = props
|
|
|
|
.sender_key
|
|
|
|
.as_ref()
|
2021-07-30 10:53:35 +00:00
|
|
|
.and_then(|k| box_::SecretKey::from_slice(k))
|
2019-03-20 14:36:10 +00:00
|
|
|
.ok_or_else(|| {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::error_msg!(
|
2019-03-20 14:36:10 +00:00
|
|
|
gst::ResourceError::NotFound,
|
|
|
|
[format!(
|
|
|
|
"Failed to set Sender's Key from property: {:?}",
|
|
|
|
props.sender_key
|
|
|
|
)
|
|
|
|
.as_ref()]
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let receiver_key = props
|
|
|
|
.receiver_key
|
|
|
|
.as_ref()
|
2021-07-30 10:53:35 +00:00
|
|
|
.and_then(|k| box_::PublicKey::from_slice(k))
|
2019-03-20 14:36:10 +00:00
|
|
|
.ok_or_else(|| {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::error_msg!(
|
2019-03-20 14:36:10 +00:00
|
|
|
gst::ResourceError::NotFound,
|
|
|
|
[format!(
|
|
|
|
"Failed to set Receiver's Key from property: {:?}",
|
|
|
|
props.receiver_key
|
|
|
|
)
|
|
|
|
.as_ref()]
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// This env variable is only meant to bypass nonce regeneration during
|
|
|
|
// tests to get determinisic results. It should never be used outside
|
|
|
|
// of testing environments.
|
|
|
|
let nonce = if let Ok(val) = std::env::var("GST_SODIUM_ENCRYPT_NONCE") {
|
|
|
|
let bytes = hex::decode(val).expect("Failed to decode hex variable");
|
|
|
|
assert_eq!(bytes.len(), box_::NONCEBYTES);
|
|
|
|
box_::Nonce::from_slice(&bytes).unwrap()
|
|
|
|
} else {
|
|
|
|
box_::gen_nonce()
|
|
|
|
};
|
|
|
|
|
|
|
|
let precomputed_key = box_::precompute(&receiver_key, &sender_key);
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
adapter: gst_base::UniqueAdapter::new(),
|
|
|
|
precomputed_key,
|
|
|
|
nonce,
|
|
|
|
block_size: props.block_size,
|
|
|
|
write_headers: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn seal(&mut self, message: &[u8]) -> Vec<u8> {
|
|
|
|
let ciphertext = box_::seal_precomputed(message, &self.nonce, &self.precomputed_key);
|
|
|
|
self.nonce.increment_le_inplace();
|
|
|
|
ciphertext
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encrypt_message(&mut self, buffer: &gst::BufferRef) -> gst::Buffer {
|
|
|
|
let map = buffer
|
|
|
|
.map_readable()
|
|
|
|
.expect("Failed to map buffer readable");
|
|
|
|
|
|
|
|
let sealed = self.seal(&map);
|
|
|
|
gst::Buffer::from_mut_slice(sealed)
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:57:34 +00:00
|
|
|
fn encrypt_blocks(&mut self, block_size: usize) -> BufferVec {
|
2019-03-20 14:36:10 +00:00
|
|
|
assert_ne!(block_size, 0);
|
|
|
|
|
|
|
|
let mut buffers = BufferVec::new();
|
|
|
|
|
|
|
|
// As long we have enough bytes to encrypt a block, or more, we do so
|
|
|
|
// else the leftover bytes on the adapter will be pushed when EOS
|
|
|
|
// is sent.
|
|
|
|
while self.adapter.available() >= block_size {
|
|
|
|
let buffer = self.adapter.take_buffer(block_size).unwrap();
|
|
|
|
let out_buf = self.encrypt_message(&buffer);
|
|
|
|
|
|
|
|
buffers.push(out_buf);
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:57:34 +00:00
|
|
|
buffers
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:09:42 +00:00
|
|
|
pub struct Encrypter {
|
2019-03-20 14:36:10 +00:00
|
|
|
srcpad: gst::Pad,
|
|
|
|
sinkpad: gst::Pad,
|
|
|
|
props: Mutex<Props>,
|
|
|
|
state: Mutex<Option<State>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encrypter {
|
|
|
|
fn sink_chain(
|
|
|
|
&self,
|
|
|
|
pad: &gst::Pad,
|
|
|
|
buffer: gst::Buffer,
|
|
|
|
) -> Result<gst::FlowSuccess, gst::FlowError> {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(CAT, obj: pad, "Handling buffer {:?}", buffer);
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
let mut buffers = BufferVec::new();
|
|
|
|
let mut state_guard = self.state.lock().unwrap();
|
|
|
|
let state = state_guard.as_mut().unwrap();
|
|
|
|
|
|
|
|
if state.write_headers {
|
|
|
|
let mut headers = Vec::with_capacity(40);
|
|
|
|
headers.extend_from_slice(crate::TYPEFIND_HEADER);
|
|
|
|
// Write the Nonce used into the stream.
|
|
|
|
headers.extend_from_slice(state.nonce.as_ref());
|
|
|
|
// Write the block_size into the stream
|
|
|
|
headers.extend_from_slice(&state.block_size.to_le_bytes());
|
|
|
|
|
|
|
|
buffers.push(gst::Buffer::from_mut_slice(headers));
|
|
|
|
state.write_headers = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.adapter.push(buffer);
|
|
|
|
|
|
|
|
// Encrypt the whole blocks, if any, and push them.
|
2021-02-09 16:57:34 +00:00
|
|
|
buffers.extend(state.encrypt_blocks(state.block_size as usize));
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
drop(state_guard);
|
|
|
|
|
|
|
|
for buffer in buffers {
|
|
|
|
self.srcpad.push(buffer).map_err(|err| {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::error!(CAT, imp: self, "Failed to push buffer {:?}", err);
|
2019-03-20 14:36:10 +00:00
|
|
|
err
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(gst::FlowSuccess::Ok)
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn sink_event(&self, pad: &gst::Pad, event: gst::Event) -> bool {
|
2019-03-20 14:36:10 +00:00
|
|
|
use gst::EventView;
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(CAT, obj: pad, "Handling event {:?}", event);
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
match event.view() {
|
|
|
|
EventView::Caps(_) => {
|
|
|
|
// We send our own caps downstream
|
|
|
|
let caps = gst::Caps::builder("application/x-sodium-encrypted").build();
|
2020-06-24 22:33:57 +00:00
|
|
|
self.srcpad.push_event(gst::event::Caps::new(&caps))
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
EventView::Eos(_) => {
|
|
|
|
let mut state_mutex = self.state.lock().unwrap();
|
|
|
|
let mut buffers = BufferVec::new();
|
|
|
|
// This will only be run after READY state,
|
|
|
|
// and will be guaranted to be initialized
|
|
|
|
let state = state_mutex.as_mut().unwrap();
|
|
|
|
|
|
|
|
// Now that all the full size blocks are pushed, drain the
|
|
|
|
// rest of the adapter and push whatever is left.
|
|
|
|
let avail = state.adapter.available();
|
|
|
|
// logic error, all the complete blocks that can be pushed
|
|
|
|
// should have been done in the sink_chain call.
|
|
|
|
assert!(avail < state.block_size as usize);
|
|
|
|
|
|
|
|
if avail > 0 {
|
2021-02-09 16:57:34 +00:00
|
|
|
let b = state.encrypt_blocks(avail);
|
|
|
|
buffers.extend(b);
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// drop the lock before pushing into the pad
|
|
|
|
drop(state_mutex);
|
|
|
|
|
|
|
|
for buffer in buffers {
|
|
|
|
if let Err(err) = self.srcpad.push(buffer) {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::error!(CAT, imp: self, "Failed to push buffer at EOS {:?}", err);
|
2019-03-20 14:36:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
pad.event_default(Some(&*self.instance()), event)
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
2022-10-09 13:06:59 +00:00
|
|
|
_ => pad.event_default(Some(&*self.instance()), event),
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn src_event(&self, pad: &gst::Pad, event: gst::Event) -> bool {
|
2019-03-20 14:36:10 +00:00
|
|
|
use gst::EventView;
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(CAT, obj: pad, "Handling event {:?}", event);
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
match event.view() {
|
|
|
|
EventView::Seek(_) => false,
|
2022-10-09 13:06:59 +00:00
|
|
|
_ => pad.event_default(Some(&*self.instance()), event),
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn src_query(&self, pad: &gst::Pad, query: &mut gst::QueryRef) -> bool {
|
2022-01-19 13:07:45 +00:00
|
|
|
use gst::QueryViewMut;
|
2019-03-20 14:36:10 +00:00
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(CAT, obj: pad, "Handling query {:?}", query);
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
match query.view_mut() {
|
2022-01-19 13:07:45 +00:00
|
|
|
QueryViewMut::Seeking(q) => {
|
2021-04-12 12:49:54 +00:00
|
|
|
let format = q.format();
|
2019-03-20 14:36:10 +00:00
|
|
|
q.set(
|
|
|
|
false,
|
2022-10-08 16:29:10 +00:00
|
|
|
gst::GenericFormattedValue::none_for_format(format),
|
|
|
|
gst::GenericFormattedValue::none_for_format(format),
|
2019-03-20 14:36:10 +00:00
|
|
|
);
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(CAT, obj: pad, "Returning {:?}", q.query_mut());
|
2019-03-20 14:36:10 +00:00
|
|
|
true
|
|
|
|
}
|
2022-01-19 13:07:45 +00:00
|
|
|
QueryViewMut::Duration(q) => {
|
2021-04-12 12:49:54 +00:00
|
|
|
if q.format() != gst::Format::Bytes {
|
2022-10-09 13:06:59 +00:00
|
|
|
return pad.query_default(Some(&*self.instance()), query);
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* First let's query the bytes duration upstream */
|
2020-06-24 22:33:57 +00:00
|
|
|
let mut peer_query = gst::query::Duration::new(gst::Format::Bytes);
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
if !self.sinkpad.peer_query(&mut peer_query) {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::error!(CAT, "Failed to query upstream duration");
|
2019-03-20 14:36:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-12 12:49:54 +00:00
|
|
|
let size = match peer_query.result().try_into().unwrap() {
|
2021-05-26 09:54:34 +00:00
|
|
|
Some(gst::format::Bytes(size)) => size,
|
|
|
|
None => {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::error!(CAT, "Failed to query upstream duration");
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let state = self.state.lock().unwrap();
|
|
|
|
let state = match state.as_ref() {
|
|
|
|
// If state isn't set, it means that the
|
|
|
|
// element hasn't been activated yet.
|
|
|
|
None => return false,
|
|
|
|
Some(s) => s,
|
|
|
|
};
|
|
|
|
|
|
|
|
// calculate the number of chunks that exist in the stream
|
|
|
|
let total_chunks = (size + state.block_size as u64 - 1) / state.block_size as u64;
|
|
|
|
// add the MAC of each block
|
|
|
|
let size = size + total_chunks * box_::MACBYTES as u64;
|
|
|
|
|
|
|
|
// add static offsets
|
2020-11-14 17:09:42 +00:00
|
|
|
let size = size + crate::HEADERS_SIZE as u64;
|
2019-03-20 14:36:10 +00:00
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(CAT, obj: pad, "Setting duration bytes: {}", size);
|
2021-05-26 09:54:34 +00:00
|
|
|
q.set(gst::format::Bytes(size));
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
}
|
2022-10-09 13:06:59 +00:00
|
|
|
_ => pad.query_default(Some(&*self.instance()), query),
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:22:24 +00:00
|
|
|
#[glib::object_subclass]
|
2019-03-20 14:36:10 +00:00
|
|
|
impl ObjectSubclass for Encrypter {
|
|
|
|
const NAME: &'static str = "RsSodiumEncrypter";
|
2020-11-14 17:09:42 +00:00
|
|
|
type Type = super::Encrypter;
|
2019-03-20 14:36:10 +00:00
|
|
|
type ParentType = gst::Element;
|
|
|
|
|
2020-11-14 17:09:42 +00:00
|
|
|
fn with_class(klass: &Self::Class) -> Self {
|
2021-04-20 12:58:11 +00:00
|
|
|
let templ = klass.pad_template("sink").unwrap();
|
2020-06-22 08:03:52 +00:00
|
|
|
let sinkpad = gst::Pad::builder_with_template(&templ, Some("sink"))
|
|
|
|
.chain_function(|pad, parent, buffer| {
|
|
|
|
Encrypter::catch_panic_pad_function(
|
|
|
|
parent,
|
|
|
|
|| Err(gst::FlowError::Error),
|
2022-10-09 13:06:59 +00:00
|
|
|
|encrypter| encrypter.sink_chain(pad, buffer),
|
2020-06-22 08:03:52 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.event_function(|pad, parent, event| {
|
|
|
|
Encrypter::catch_panic_pad_function(
|
|
|
|
parent,
|
|
|
|
|| false,
|
2022-10-09 13:06:59 +00:00
|
|
|
|encrypter| encrypter.sink_event(pad, event),
|
2020-06-22 08:03:52 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.build();
|
|
|
|
|
2021-04-20 12:58:11 +00:00
|
|
|
let templ = klass.pad_template("src").unwrap();
|
2020-06-22 08:03:52 +00:00
|
|
|
let srcpad = gst::Pad::builder_with_template(&templ, Some("src"))
|
|
|
|
.query_function(|pad, parent, query| {
|
|
|
|
Encrypter::catch_panic_pad_function(
|
|
|
|
parent,
|
|
|
|
|| false,
|
2022-10-09 13:06:59 +00:00
|
|
|
|encrypter| encrypter.src_query(pad, query),
|
2020-06-22 08:03:52 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.event_function(|pad, parent, event| {
|
|
|
|
Encrypter::catch_panic_pad_function(
|
|
|
|
parent,
|
|
|
|
|| false,
|
2022-10-09 13:06:59 +00:00
|
|
|
|encrypter| encrypter.src_event(pad, event),
|
2020-06-22 08:03:52 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.build();
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
let props = Mutex::new(Props::default());
|
|
|
|
let state = Mutex::new(None);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
srcpad,
|
|
|
|
sinkpad,
|
|
|
|
props,
|
|
|
|
state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectImpl for Encrypter {
|
2021-01-21 18:21:29 +00:00
|
|
|
fn properties() -> &'static [glib::ParamSpec] {
|
|
|
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
|
|
|
vec![
|
2022-09-05 08:45:47 +00:00
|
|
|
glib::ParamSpecBoxed::builder::<glib::Bytes>("receiver-key")
|
2022-08-18 12:04:15 +00:00
|
|
|
.nick("Receiver Key")
|
|
|
|
.blurb("The public key of the Receiver")
|
|
|
|
.build(),
|
2022-09-05 08:45:47 +00:00
|
|
|
glib::ParamSpecBoxed::builder::<glib::Bytes>("sender-key")
|
2022-08-18 12:04:15 +00:00
|
|
|
.nick("Sender Key")
|
|
|
|
.blurb("The private key of the Sender")
|
|
|
|
.write_only()
|
|
|
|
.build(),
|
|
|
|
glib::ParamSpecUInt::builder("block-size")
|
|
|
|
.nick("Block Size")
|
|
|
|
.blurb("The block-size of the chunks")
|
|
|
|
.minimum(1024)
|
|
|
|
.default_value(32768)
|
|
|
|
.build(),
|
2021-01-21 18:21:29 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
PROPERTIES.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn constructed(&self) {
|
|
|
|
self.parent_constructed();
|
2019-03-20 14:36:10 +00:00
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
let obj = self.instance();
|
2020-11-14 17:09:42 +00:00
|
|
|
obj.add_pad(&self.sinkpad).unwrap();
|
|
|
|
obj.add_pad(&self.srcpad).unwrap();
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
|
2021-04-12 12:49:54 +00:00
|
|
|
match pspec.name() {
|
2021-01-21 18:21:29 +00:00
|
|
|
"sender-key" => {
|
2019-03-20 14:36:10 +00:00
|
|
|
let mut props = self.props.lock().unwrap();
|
2019-08-12 22:45:36 +00:00
|
|
|
props.sender_key = value.get().expect("type checked upstream");
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
"receiver-key" => {
|
2019-03-20 14:36:10 +00:00
|
|
|
let mut props = self.props.lock().unwrap();
|
2019-08-12 22:45:36 +00:00
|
|
|
props.receiver_key = value.get().expect("type checked upstream");
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
"block-size" => {
|
2019-03-20 14:36:10 +00:00
|
|
|
let mut props = self.props.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
props.block_size = value.get().expect("type checked upstream");
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
2021-04-12 12:49:54 +00:00
|
|
|
match pspec.name() {
|
2021-01-21 18:21:29 +00:00
|
|
|
"receiver-key" => {
|
2019-03-20 14:36:10 +00:00
|
|
|
let props = self.props.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
props.receiver_key.to_value()
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
"block-size" => {
|
2019-03-20 14:36:10 +00:00
|
|
|
let props = self.props.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
props.block_size.to_value()
|
2019-03-20 14:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 08:57:31 +00:00
|
|
|
impl GstObjectImpl for Encrypter {}
|
|
|
|
|
2019-03-20 14:36:10 +00:00
|
|
|
impl ElementImpl for Encrypter {
|
2021-01-21 18:21:29 +00:00
|
|
|
fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
|
|
|
|
static ELEMENT_METADATA: Lazy<gst::subclass::ElementMetadata> = Lazy::new(|| {
|
|
|
|
gst::subclass::ElementMetadata::new(
|
|
|
|
"Encrypter",
|
|
|
|
"Generic",
|
|
|
|
"libsodium-based file encrypter",
|
|
|
|
"Jordan Petridis <jordan@centricular.com>",
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(&*ELEMENT_METADATA)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pad_templates() -> &'static [gst::PadTemplate] {
|
|
|
|
static PAD_TEMPLATES: Lazy<Vec<gst::PadTemplate>> = Lazy::new(|| {
|
|
|
|
let src_caps = gst::Caps::builder("application/x-sodium-encrypted").build();
|
|
|
|
let src_pad_template = gst::PadTemplate::new(
|
|
|
|
"src",
|
|
|
|
gst::PadDirection::Src,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&src_caps,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let sink_pad_template = gst::PadTemplate::new(
|
|
|
|
"sink",
|
|
|
|
gst::PadDirection::Sink,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&gst::Caps::new_any(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
vec![src_pad_template, sink_pad_template]
|
|
|
|
});
|
|
|
|
|
|
|
|
PAD_TEMPLATES.as_ref()
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:36:10 +00:00
|
|
|
fn change_state(
|
|
|
|
&self,
|
|
|
|
transition: gst::StateChange,
|
|
|
|
) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::debug!(CAT, imp: self, "Changing state {:?}", transition);
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
match transition {
|
|
|
|
gst::StateChange::NullToReady => {
|
|
|
|
let props = self.props.lock().unwrap().clone();
|
|
|
|
|
|
|
|
// Create an internal state struct from the provided properties or
|
|
|
|
// refuse to change state
|
|
|
|
let state_ = State::from_props(&props).map_err(|err| {
|
2022-10-09 13:06:59 +00:00
|
|
|
self.post_error_message(err);
|
2019-03-20 14:36:10 +00:00
|
|
|
gst::StateChangeError
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
*state = Some(state_);
|
|
|
|
}
|
|
|
|
gst::StateChange::ReadyToNull => {
|
|
|
|
let _ = self.state.lock().unwrap().take();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
let success = self.parent_change_state(transition)?;
|
2019-03-20 14:36:10 +00:00
|
|
|
|
|
|
|
if transition == gst::StateChange::ReadyToNull {
|
|
|
|
let _ = self.state.lock().unwrap().take();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(success)
|
|
|
|
}
|
|
|
|
}
|