Add Adpcm audio

This commit is contained in:
QuantumEntangledAndy committed 2023-04-06 10:50:55 +07:00
1 parent 8c6f5be1bd
commit 8a11f6e867
6 files changed
+86 -137

No files matched your search

-29
View File
@@ -1,29 +0,0 @@
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
#[derive(Clone, Default)]
pub(crate) struct AbortHandle {
live: Arc<AtomicBool>,
}
impl AbortHandle {
pub(crate) fn new() -> Self {
Self {
live: Arc::new(AtomicBool::new(true)),
}
}
pub(crate) fn abort(&self) {
self.live.store(false, Ordering::Relaxed);
}
pub(crate) fn reset(&self) {
self.live.store(true, Ordering::Relaxed);
}
pub(crate) fn is_live(&self) -> bool {
self.live.load(Ordering::Relaxed)
}
}
+58 -14
View File
@@ -199,7 +199,7 @@ impl NeoMediaFactoryImpl {
let mut client_data: ClientPipelineData = Default::default();
// Now contruct the actual ones
match VidFormats::from(self.shared.vid_format.load(Ordering::Relaxed)) {
match *self.shared.vid_format.blocking_read() {
VidFormats::H265 => {
debug!("Building H265 Pipeline");
let source = make_element("appsrc", "vidsrc")?
@@ -210,7 +210,7 @@ impl NeoMediaFactoryImpl {
));
source.set_is_live(true);
source.set_block(true);
source.set_property("emit-signals", &false);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Stream);
@@ -241,7 +241,7 @@ impl NeoMediaFactoryImpl {
));
source.set_is_live(true);
source.set_block(true);
source.set_property("emit-signals", &false);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Stream);
@@ -292,7 +292,7 @@ impl NeoMediaFactoryImpl {
}
}
match AudFormats::from(self.shared.aud_format.load(Ordering::Relaxed)) {
match *self.shared.aud_format.blocking_read() {
AudFormats::Unknown => {}
AudFormats::Aac => {
debug!("Building Aac pipeline");
@@ -304,7 +304,7 @@ impl NeoMediaFactoryImpl {
));
source.set_is_live(true);
source.set_block(true);
source.set_property("emit-signals", &false);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Stream);
@@ -330,21 +330,65 @@ impl NeoMediaFactoryImpl {
.expect("Failed to link AAC decoder to encoder");
});
// decoder.link_filtered(
// &encoder,
// & Caps::builder("audio/x-raw")
// .field("format", "S16BE")
// .field("layout", "interleaved")
// // .field("channels", 1)
// .build(),
// )?;
let source = source
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
client_data.audsrc.replace(source);
}
AudFormats::Adpcm(block_size) => {
// Original command line
// caps=audio/x-adpcm,layout=dvi,block_align={},channels=1,rate=8000
// ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024
// ! adpcmdec
// ! audioconvert
// ! rtpL16pay name=pay1
let source = make_element("appsrc", "audsrc")?
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_base_time(ClockTime::from_mseconds(
self.shared.microseconds.load(Ordering::Relaxed),
));
source.set_is_live(true);
source.set_block(true);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
source.set_do_timestamp(false);
source.set_stream_type(AppStreamType::Stream);
source.set_caps(Some(
&Caps::builder("audio/x-adpcm")
.field("layout", "div")
.field("block_align", block_size as i32)
.field("channels", 1i32)
.field("rate", 8000i32)
.build(),
));
let source = source
.dynamic_cast::<Element>()
.map_err(|_| anyhow!("Cannot cast back"))?;
let queue = make_element("queue", "audqueue")?;
let decoder = make_element("decodebin", "auddecoder")?;
let encoder = make_element("audioconvert", "audencoder")?;
let payload = make_element("rtpL16pay", "pay1")?;
bin.add_many(&[&source, &queue, &decoder, &encoder, &payload])?;
Element::link_many(&[&source, &queue, &decoder])?;
Element::link_many(&[&encoder, &payload])?;
decoder.connect_pad_added(move |_element, pad| {
debug!("Linking encoder to decoder: {:?}", pad.caps());
let sink_pad = encoder
.static_pad("sink")
.expect("Encoder is missing its pad");
pad.link(&sink_pad)
.expect("Failed to link ADPCM decoder to encoder");
});
let source = source
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
client_data.audsrc.replace(source);
}
AudFormats::Adpcm => {}
}
self.clientsender.blocking_send(client_data)?;
+16 -36
View File
@@ -47,7 +47,7 @@ impl NeoMediaSender {
if let Some(bc_media) = v {
if ! self.skip_bcmedia(&bc_media)? {
// resend_pause.reset();
self.inspect_bcmedia(&bc_media)?;
self.inspect_bcmedia(&bc_media).await?;
match bc_media {
BcMedia::Iframe(frame) => {
buffer.clear();
@@ -194,21 +194,17 @@ impl NeoMediaSender {
Ok(false)
}
fn inspect_bcmedia(&mut self, bc_media: &BcMedia) -> AnyResult<()> {
let old_vid = self.shared.vid_format.load(Ordering::Relaxed);
let old_aud = self.shared.aud_format.load(Ordering::Relaxed);
async fn inspect_bcmedia(&mut self, bc_media: &BcMedia) -> AnyResult<()> {
let old_vid = *self.shared.vid_format.read().await;
let old_aud = *self.shared.aud_format.read().await;
match bc_media {
BcMedia::Iframe(frame) => {
match frame.video_type {
VideoType::H264 => {
self.shared
.vid_format
.store(VidFormats::H264.into(), Ordering::Relaxed);
(*self.shared.vid_format.write().await) = VidFormats::H264;
}
VideoType::H265 => {
self.shared
.vid_format
.store(VidFormats::H265.into(), Ordering::Relaxed);
(*self.shared.vid_format.write().await) = VidFormats::H265;
}
}
self.shared
@@ -218,14 +214,10 @@ impl NeoMediaSender {
BcMedia::Pframe(frame) => {
match frame.video_type {
VideoType::H264 => {
self.shared
.vid_format
.store(VidFormats::H264.into(), Ordering::Relaxed);
(*self.shared.vid_format.write().await) = VidFormats::H264;
}
VideoType::H265 => {
self.shared
.vid_format
.store(VidFormats::H265.into(), Ordering::Relaxed);
(*self.shared.vid_format.write().await) = VidFormats::H265;
}
}
self.shared
@@ -233,32 +225,20 @@ impl NeoMediaSender {
.store(frame.microseconds as u64, Ordering::Relaxed);
}
BcMedia::Aac(_aac) => {
self.shared
.aud_format
.store(AudFormats::Aac.into(), Ordering::Relaxed);
(*self.shared.aud_format.write().await) = AudFormats::Aac;
}
BcMedia::Adpcm(_) => {
self.shared
.aud_format
.store(AudFormats::Adpcm.into(), Ordering::Relaxed);
BcMedia::Adpcm(adpcm) => {
(*self.shared.aud_format.write().await) = AudFormats::Adpcm(adpcm.data.len() as u16)
}
_ => {}
}
let new_vid = self.shared.vid_format.load(Ordering::Relaxed);
if old_vid != new_vid {
log::debug!(
"Video format set to: {:?} from {:?}",
VidFormats::from(new_vid),
VidFormats::from(old_vid)
);
let new_vid = *self.shared.vid_format.read().await;
if new_vid != old_vid {
log::debug!("Video format set to: {:?} from {:?}", new_vid, old_vid);
}
let new_aud = self.shared.aud_format.load(Ordering::Relaxed);
let new_aud = *self.shared.aud_format.read().await;
if old_aud != new_aud {
log::debug!(
"Audio format set to: {:?} from {:?}",
AudFormats::from(new_aud),
AudFormats::from(old_aud)
);
log::debug!("Audio format set to: {:?} from {:?}", new_aud, old_aud);
}
Ok(())
}
+10 -52
View File
@@ -2,76 +2,34 @@
//! components that manage a media stream
use gstreamer_app::AppSrc;
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
use std::convert::{From, Into};
use std::sync::atomic::AtomicU64;
use tokio::sync::RwLock;
#[derive(PartialEq, Eq, Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(super) enum VidFormats {
Unknown = 0,
Unknown,
H264,
H265,
}
impl From<VidFormats> for u64 {
fn from(value: VidFormats) -> u64 {
match value {
VidFormats::Unknown => 0,
VidFormats::H264 => 1,
VidFormats::H265 => 2,
}
}
}
impl From<u64> for VidFormats {
fn from(value: u64) -> Self {
match value {
0 => VidFormats::Unknown,
1 => VidFormats::H264,
2 => VidFormats::H265,
_ => unreachable!(),
}
}
}
#[derive(PartialEq, Eq, Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(super) enum AudFormats {
Unknown = 0,
Unknown,
Aac,
Adpcm,
}
impl From<AudFormats> for u64 {
fn from(value: AudFormats) -> u64 {
match value {
AudFormats::Unknown => 0,
AudFormats::Aac => 1,
AudFormats::Adpcm => 2,
}
}
}
impl From<u64> for AudFormats {
fn from(value: u64) -> Self {
match value {
0 => AudFormats::Unknown,
1 => AudFormats::Aac,
2 => AudFormats::Adpcm,
_ => unreachable!(),
}
}
Adpcm(u16),
}
pub(super) struct NeoMediaShared {
pub(super) vid_format: AtomicU64,
pub(super) aud_format: AtomicU64,
pub(super) vid_format: RwLock<VidFormats>,
pub(super) aud_format: RwLock<AudFormats>,
pub(super) microseconds: AtomicU64,
}
impl Default for NeoMediaShared {
fn default() -> Self {
Self {
vid_format: AtomicU64::new(VidFormats::Unknown.into()),
aud_format: AtomicU64::new(AudFormats::Unknown.into()),
vid_format: RwLock::new(VidFormats::Unknown),
aud_format: RwLock::new(AudFormats::Unknown),
microseconds: AtomicU64::new(0),
}
}
+1 -5
View File
@@ -58,12 +58,10 @@ use log::*;
use std::sync::Arc;
use std::time::Duration;
mod abort;
mod cmdline;
mod gst;
mod states;
use abort::AbortHandle;
use states::{RtspCamera, StateInfo};
use super::config::{CameraConfig, Config, UserConfig};
@@ -86,19 +84,17 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
)
}
let mut set = tokio::task::JoinSet::new();
let abort = AbortHandle::new();
let arc_user_config = Arc::new(config.users);
for camera_config in config.cameras.drain(..) {
// Spawn each camera controller in it's own thread
let user_config = arc_user_config.clone();
let arc_rtsp = rtsp.clone();
let abort_handle = abort.clone();
set.spawn(async move {
let backoff = Backoff::new();
while abort_handle.is_live() {
loop {
let failure =
camera_main(&camera_config, user_config.as_slice(), arc_rtsp.clone()).await;
match failure {
+1 -1
View File
@@ -46,7 +46,7 @@ impl CameraState for Streaming {
let stream_thead = *stream;
let strict_thread = shared.strict;
let tag_thread = tag.clone();
let handle = self.set.spawn(async move {
self.set.spawn(async move {
let mut stream_data = thread_camera
.start_video(stream_thead, 0, strict_thread)
.await?;