From 30fd27bfcfccc225e2d2a733c128db2898dd6a7a Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 26 Sep 2023 10:06:42 +0700 Subject: [PATCH] A few more memory optimisations --- .github/workflows/build.yml | 26 ++-- Cargo.lock | 111 +++++++++--------- Cargo.toml | 8 +- Dockerfile | 4 +- crates/core/Cargo.toml | 2 +- .../core/src/bc_protocol/connection/bcconn.rs | 30 +++-- src/rtsp/mod.rs | 101 +++++++++------- 7 files changed, 150 insertions(+), 132 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9f6d3f8..ae72c9f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -92,7 +92,7 @@ jobs: if: needs.pre_job.outputs.should_skip != 'true' name: cross runs-on: ubuntu-latest - container: "node:current-bullseye-slim" + container: "node:current-bookworm-slim" strategy: fail-fast: false matrix: @@ -155,7 +155,7 @@ jobs: TARGET: ${{ matrix.target }} - uses: actions/upload-artifact@v3 with: - name: release-${{ matrix.arch }}-bullseye + name: release-${{ matrix.arch }}-bookworm path: "target/${{ matrix.target }}/release/neolink*" push_to_registry: @@ -264,22 +264,22 @@ jobs: - name: Download Linux x86_64 uses: actions/download-artifact@v3 with: - name: release-amd64-bullseye + name: release-amd64-bookworm path: linux/amd64 - name: Download Linux armv7 uses: actions/download-artifact@v3 with: - name: release-armhf-bullseye + name: release-armhf-bookworm path: linux/arm/v7 - name: Download Linux arm64 uses: actions/download-artifact@v3 with: - name: release-arm64-bullseye + name: release-arm64-bookworm path: linux/arm64 - name: Download Linux i386 uses: actions/download-artifact@v3 with: - name: release-i386-bullseye + name: release-i386-bookworm path: linux/386 - name: Push to Docker Hub if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }} @@ -333,22 +333,22 @@ jobs: - name: Download Linux x86_64 uses: actions/download-artifact@v3 with: - name: release-amd64-bullseye - path: neolink_linux_x86_64_bullseye + name: release-amd64-bookworm + path: neolink_linux_x86_64_bookworm - name: Download Linux armhf uses: actions/download-artifact@v3 with: - name: release-armhf-bullseye + name: release-armhf-bookworm path: neolink_linux_armhf - name: Download Linux arm64 uses: actions/download-artifact@v3 with: - name: release-arm64-bullseye + name: release-arm64-bookworm path: neolink_linux_arm64 - name: Download Linux i386 uses: actions/download-artifact@v3 with: - name: release-i386-bullseye + name: release-i386-bookworm path: neolink_linux_i386 - name: Prepare Releases run: | @@ -356,7 +356,7 @@ jobs: neolink_windows neolink_macos neolink_linux_x86_64_ubuntu - neolink_linux_x86_64_bullseye + neolink_linux_x86_64_bookworm neolink_linux_armhf neolink_linux_arm64 neolink_linux_i386 @@ -373,7 +373,7 @@ jobs: neolink_windows.zip neolink_macos.zip neolink_linux_x86_64_ubuntu.zip - neolink_linux_x86_64_bullseye.zip + neolink_linux_x86_64_bookworm.zip neolink_linux_armhf.zip neolink_linux_arm64.zip neolink_linux_i386.zip diff --git a/Cargo.lock b/Cargo.lock index 52b404a..06be568 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -737,11 +737,10 @@ checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" [[package]] name = "gio" -version = "0.17.10" +version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6973e92937cf98689b6a054a9e56c657ed4ff76de925e36fc331a15f0c5d30a" +checksum = "57052f84e8e5999b258e8adf8f5f2af0ac69033864936b8b6838321db2f759b1" dependencies = [ - "bitflags 1.3.2", "futures-channel", "futures-core", "futures-io", @@ -757,9 +756,9 @@ dependencies = [ [[package]] name = "gio-sys" -version = "0.17.10" +version = "0.18.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ccf87c30a12c469b6d958950f6a9c09f2be20b7773f7e70d20b867fdf2628c3" +checksum = "37566df850baf5e4cb0dfb78af2e4b9898d817ed9263d1090a2df958c64737d2" dependencies = [ "glib-sys", "gobject-sys", @@ -770,11 +769,11 @@ dependencies = [ [[package]] name = "glib" -version = "0.17.10" +version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3fad45ba8d4d2cea612b432717e834f48031cd8853c8aaf43b2c79fec8d144b" +checksum = "1c316afb01ce8067c5eaab1fc4f2cd47dc21ce7b6296358605e2ffab23ccbd19" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.3.3", "futures-channel", "futures-core", "futures-executor", @@ -793,24 +792,23 @@ dependencies = [ [[package]] name = "glib-macros" -version = "0.17.10" +version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eca5c79337338391f1ab8058d6698125034ce8ef31b72a442437fa6c8580de26" +checksum = "f8da903822b136d42360518653fcf154455defc437d3e7a81475bf9a95ff1e47" dependencies = [ - "anyhow", "heck", "proc-macro-crate", "proc-macro-error", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.28", ] [[package]] name = "glib-sys" -version = "0.17.10" +version = "0.18.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d80aa6ea7bba0baac79222204aa786a6293078c210abe69ef1336911d4bdc4f0" +checksum = "063ce2eb6a8d0ea93d2bf8ba1957e78dbab6be1c2220dd3daca57d5a9d869898" dependencies = [ "libc", "system-deps", @@ -818,9 +816,9 @@ dependencies = [ [[package]] name = "gobject-sys" -version = "0.17.10" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd34c3317740a6358ec04572c1bcfd3ac0b5b6529275fae255b237b314bb8062" +checksum = "0850127b514d1c4a4654ead6dedadb18198999985908e6ffe4436f53c785ce44" dependencies = [ "glib-sys", "libc", @@ -829,22 +827,21 @@ dependencies = [ [[package]] name = "gstreamer" -version = "0.20.7" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0a4150420d4aa1caf6fa15f0dba7a5007d4116380633bd1253acce206098fc9" +checksum = "f8cdb86791dc39a5443f7d08cf3e7ae9c88a94991aba620d177cb5804838201f" dependencies = [ - "bitflags 1.3.2", "cfg-if", "futures-channel", "futures-core", "futures-util", "glib", "gstreamer-sys", + "itertools 0.11.0", "libc", "muldiv", "num-integer", "num-rational", - "once_cell", "option-operations", "paste", "pretty-hex", @@ -854,11 +851,10 @@ dependencies = [ [[package]] name = "gstreamer-app" -version = "0.20.7" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe9a6d39222f708ba3a55f259261452daf4fc0d36693fe14126a630beb6461a8" +checksum = "f6da0a8e5671d836ac70741dff0a3e7688ec9ac7ab8dc62c3135dece85df63d4" dependencies = [ - "bitflags 1.3.2", "futures-core", "futures-sink", "glib", @@ -866,14 +862,13 @@ dependencies = [ "gstreamer-app-sys", "gstreamer-base", "libc", - "once_cell", ] [[package]] name = "gstreamer-app-sys" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7cb5375aa8c23012ec5fadde1a37b972e87680776772669d2628772867056e2" +checksum = "2bb093f39bd9b3d0c4ef8f1c4d46028d35bce033f9bda1a0449e21ef17349e03" dependencies = [ "glib-sys", "gstreamer-base-sys", @@ -884,25 +879,23 @@ dependencies = [ [[package]] name = "gstreamer-base" -version = "0.20.7" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0896c4acff303dd21d6a96a7ea4cc9339f7096230fe1433720c9f0bed203985" +checksum = "0fe38a6d5c1e516ce3fd6069e972a540d315448ed69fdadad739e6c6c6eb2a01" dependencies = [ "atomic_refcell", - "bitflags 1.3.2", "cfg-if", "glib", "gstreamer", "gstreamer-base-sys", "libc", - "once_cell", ] [[package]] name = "gstreamer-base-sys" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26114ed96f6668380f5a1554128159e98e06c3a7a8460f216d7cd6dce28f928c" +checksum = "88b9c029583ed61fa5258076a42df91732dc7f5582044ea7ee66a721641e6af4" dependencies = [ "glib-sys", "gobject-sys", @@ -913,9 +906,9 @@ dependencies = [ [[package]] name = "gstreamer-net" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17ee66adedfd176b680fb455f0b07e8cb8ab6a09120b21241399df995ae62ac7" +checksum = "09decec00a21c881eec5f5a9c903bb0ebe39d4ee1c7dce6e245b3103bc5af84d" dependencies = [ "gio", "glib", @@ -925,9 +918,9 @@ dependencies = [ [[package]] name = "gstreamer-net-sys" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56a94a90959c94b0726aa8d9416519b5fe6f91c47782d69dcaef651ddd2ce4c2" +checksum = "237f2dceda3ba258a943465706b91881b1031a0655f08777cad38dbf5a887570" dependencies = [ "gio-sys", "glib-sys", @@ -938,11 +931,10 @@ dependencies = [ [[package]] name = "gstreamer-rtsp" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61efeded67731e013622858f91e37d52b95c26c3b09cb5a82dec741aa6a5c584" +checksum = "a1fa437b3e7409968de28b2fc8210c7322b7088b44f465b3b471afe326165048" dependencies = [ - "bitflags 1.3.2", "glib", "gstreamer", "gstreamer-rtsp-sys", @@ -952,11 +944,10 @@ dependencies = [ [[package]] name = "gstreamer-rtsp-server" -version = "0.20.6" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62f889ba4bb84d6e7616382f102cc418acc305cbe6831b96fb76b42fed7780f1" +checksum = "3a857101b15e1cce0c42b399e985ada818d59ea246605b16357c82c162cfdd50" dependencies = [ - "bitflags 1.3.2", "gio", "glib", "gstreamer", @@ -965,14 +956,13 @@ dependencies = [ "gstreamer-rtsp-server-sys", "gstreamer-sdp", "libc", - "once_cell", ] [[package]] name = "gstreamer-rtsp-server-sys" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9073d4b06c963a7604e32d089437f90be7eb148320c0085780c613aeb4cf83bd" +checksum = "3ddad8f97bba42e4fba019732e2660152374da5992e916ada3ac8a786ae4ae2c" dependencies = [ "gio-sys", "glib-sys", @@ -987,9 +977,9 @@ dependencies = [ [[package]] name = "gstreamer-rtsp-sys" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7f4432b9046942875c7626d323c451184bffd836d75f56c505f8d04042ab32b" +checksum = "58434d020872f1297f29dfed7ff0d3078652828d4ab40ac6a6f28a77125bc3b0" dependencies = [ "gio-sys", "glib-sys", @@ -1002,9 +992,9 @@ dependencies = [ [[package]] name = "gstreamer-sdp" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bb57c03bfdbb0489bcab6e9b58a2c059e6089dcf660b424949dbe672af726bf" +checksum = "44947e1d02e706baadfc0a8d25fed276948df86eb98fce434c8bc2172e83c3fa" dependencies = [ "glib", "gstreamer", @@ -1013,9 +1003,9 @@ dependencies = [ [[package]] name = "gstreamer-sdp-sys" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be9ae71a2d53659f6da93a83ebe06de12c9e075604e012c45644499b24474e1a" +checksum = "006e4bdf4618c70a2cb440ca4adf1a4140fc118b1b314684903a48f0ef5e1da1" dependencies = [ "glib-sys", "gstreamer-sys", @@ -1025,9 +1015,9 @@ dependencies = [ [[package]] name = "gstreamer-sys" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e56fe047adef7d47dbafa8bc1340fddb53c325e16574763063702fc94b5786d2" +checksum = "a70e3a99118bcd1221f8a62d7a905bae5e5cc2cda678bb46bf3cd36e0f899d33" dependencies = [ "glib-sys", "gobject-sys", @@ -1247,6 +1237,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.9" @@ -1282,9 +1281,9 @@ checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503" [[package]] name = "local-ip-address" -version = "0.5.4" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "885efb07efcd6ae1c6af70be7565544121424fa9e5b1c3e4b58bbbf141a58cef" +checksum = "3fefe707432eb6bd4704b3dacfc87aab269d56667ad05dcd6869534e8890e767" dependencies = [ "libc", "neli", @@ -1723,7 +1722,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4" dependencies = [ "anyhow", - "itertools", + "itertools 0.10.5", "proc-macro2", "quote", "syn 1.0.109", diff --git a/Cargo.toml b/Cargo.toml index 83ee236..ad18204 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,10 +20,10 @@ console-subscriber = "0.1.8" crossbeam-channel = "0.5.8" env_logger = "0.10.0" futures = "0.3.28" -gstreamer = "0.20.3" -gstreamer-app = "0.20.0" -gstreamer-rtsp = "0.20.0" -gstreamer-rtsp-server = { version = "0.20.3", features = ["v1_16"] } +gstreamer = "0.21.0" +gstreamer-app = { version = "0.21.0", features = ["v1_16"] } +gstreamer-rtsp = { version = "0.21.0", features = ["v1_16"] } +gstreamer-rtsp-server = { version = "0.21.0", features = ["v1_16"] } heck = "0.4.1" lazy_static = "1.4.0" log = { version = "0.4.17", features = [ "release_max_level_debug" ] } diff --git a/Dockerfile b/Dockerfile index 96e797e..70c0953 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ # Miroslav Šedivý # SPDX-License-Identifier: AGPL-3.0-only -FROM docker.io/rust:slim-bullseye AS build +FROM docker.io/rust:slim-bookworm AS build ARG TARGETPLATFORM ENV DEBIAN_FRONTEND=noninteractive @@ -34,7 +34,7 @@ RUN echo "TARGETPLATFORM: ${TARGETPLATFORM}"; \ fi # Create the release container. Match the base OS used to build -FROM debian:bullseye-slim +FROM debian:bookworm-slim ARG TARGETPLATFORM ARG REPO ARG VERSION diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index feb9d43..ef023cd 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -18,7 +18,7 @@ err-derive = "0.3.1" futures = "0.3.28" get_if_addrs = "0.5.3" lazy_static = "1.4.0" -local-ip-address = "0.5.1" +local-ip-address = "0.5.5" log = "0.4.17" md5 = "0.7.0" nom = { version = "7.1.3", features = ["alloc"] } diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index e8b5738..b6e0082 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -8,7 +8,6 @@ use std::collections::btree_map::Entry; use std::collections::BTreeMap; use std::sync::Arc; use tokio::sync::mpsc::{channel, Sender}; -use tokio::task::yield_now; use tokio_stream::wrappers::ReceiverStream; use tokio_util::sync::CancellationToken; @@ -234,7 +233,6 @@ struct Poller { impl Poller { async fn run(&mut self) -> Result<()> { while let Some(command) = self.reciever.next().await { - yield_now().await; // Clean Up subscribers self.subscribers .num @@ -267,10 +265,15 @@ impl Poller { ) { (Some(occ), _) => { log::trace!("Calling ID callback"); - if let Some(reply) = occ(&response).await { - assert!(reply.meta.msg_num == response.meta.msg_num); - self.sink.send(Ok(reply)).await?; - } + let occ = occ.clone(); + let sink = self.sink.clone(); + tokio::task::spawn(async move { + if let Some(reply) = occ(&response).await { + assert!(reply.meta.msg_num == response.meta.msg_num); + sink.send(Ok(reply)).await?; + } + Result::Ok(()) + }); log::trace!("Called ID callback"); } (None, Some(occ)) => { @@ -315,9 +318,10 @@ impl Poller { &msg_id ); } - if sender.send(Ok(response)).await.is_err() { - occ.remove(&Some(msg_num)); - } + let sender = sender.clone(); + tokio::task::spawn(async move { + sender.send(Ok(response)).await + }); } else { debug!( "Ignoring uninteresting message id {} (number: {})", @@ -376,9 +380,11 @@ impl Poller { occ_entry.insert(tx); } else { // log::error!("Failed to subscribe in bcconn to {:?} for {:?}", msg_num, msg_id); - let _ = tx - .send(Err(Error::SimultaneousSubscription { msg_num })) - .await; + tokio::task::spawn(async move { + let _ = tx + .send(Err(Error::SimultaneousSubscription { msg_num })) + .await; + }); } } }; diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index bce692f..ef57e5b 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -53,7 +53,7 @@ // - `"none"`: Resends the last iframe the camera. This does not reencode at all. **Most use cases should use this one as it has the least effort on the cpu and gives what you would expect** // use anyhow::{anyhow, Context, Result}; -use gstreamer::{Bin, Caps, ClockTime, Element, ElementFactory}; +use gstreamer::{prelude::*, Bin, Caps, ClockTime, Element, ElementFactory, GhostPad}; use gstreamer_app::{AppSrc, AppSrcCallbacks, AppStreamType}; use gstreamer_rtsp_server::prelude::*; use log::*; @@ -88,6 +88,8 @@ use gst::NeoRtspServer; type AnyResult = anyhow::Result; +const GST_BUFFER_SIZE: u64 = 1024u64 * 1024u64 * 10u64; // 10MB + /// Entry point for the rtsp subcommand /// /// Opt is the command line options @@ -984,20 +986,8 @@ async fn handle_data> + Unpin, E>( let mut ft = Duration::ZERO; let mut last_rt: Option = None; let mut rt = Duration::ZERO; - let (gst_data_tx, mut gst_data_rx) = mpsc(100); let appsrc = app.clone(); - // push_buffer can block so we need to do this on a dedicated thread - // however it is expensive to call spawn_blocking too often - // so we call it once and push via a channel - tokio::task::spawn_blocking(move || { - while let Some(buf) = gst_data_rx.blocking_recv() { - appsrc - .push_buffer(buf) - .map(|_| ()) - .map_err(|_| anyhow!("Could not push buffer to appsrc"))?; - } - AnyResult::Ok(()) - }); + while let Some(Ok(data)) = data_rx.next().await { check_live(app)?; // Stop if appsrc is dropped // Cache the last runtime @@ -1045,7 +1035,10 @@ async fn handle_data> + Unpin, E>( gst_buf }; - gst_data_tx.send(buf).await?; + appsrc + .push_buffer(buf) + .map(|_| ()) + .map_err(|_| anyhow!("Could not push buffer to appsrc"))?; } } } @@ -1106,7 +1099,7 @@ fn build_unknown(bin: &Element) -> Result<()> { let encoder = make_element("jpegenc", "encoder")?; let payload = make_element("rtpjpegpay", "pay0")?; - bin.add_many(&[&source, &queue, &overlay, &encoder, &payload])?; + bin.add_many([&source, &queue, &overlay, &encoder, &payload])?; source.link_filtered( &queue, &Caps::builder("video/x-raw") @@ -1116,7 +1109,7 @@ fn build_unknown(bin: &Element) -> Result<()> { .field("framerate", gstreamer::Fraction::new(25, 1)) .build(), )?; - Element::link_many(&[&queue, &overlay, &encoder, &payload])?; + Element::link_many([&queue, &overlay, &encoder, &payload])?; Ok(()) } @@ -1134,7 +1127,7 @@ fn build_h264(bin: &Element) -> Result { source.set_is_live(false); source.set_block(false); source.set_property("emit-signals", false); - source.set_max_bytes(50000000u64); // 50MB + source.set_max_bytes(GST_BUFFER_SIZE); // 4MB source.set_do_timestamp(false); source.set_stream_type(AppStreamType::Seekable); @@ -1144,8 +1137,8 @@ fn build_h264(bin: &Element) -> Result { let queue = make_queue("source_queue")?; let parser = make_element("h264parse", "parser")?; let payload = make_element("rtph264pay", "pay0")?; - bin.add_many(&[&source, &queue, &parser, &payload])?; - Element::link_many(&[&source, &queue, &parser, &payload])?; + bin.add_many([&source, &queue, &parser, &payload])?; + Element::link_many([&source, &queue, &parser, &payload])?; let source = source .dynamic_cast::() @@ -1165,7 +1158,7 @@ fn build_h265(bin: &Element) -> Result { source.set_is_live(false); source.set_block(false); source.set_property("emit-signals", false); - source.set_max_bytes(52428800); + source.set_max_bytes(GST_BUFFER_SIZE); source.set_do_timestamp(false); source.set_stream_type(AppStreamType::Seekable); @@ -1175,8 +1168,8 @@ fn build_h265(bin: &Element) -> Result { let queue = make_queue("source_queue")?; let parser = make_element("h265parse", "parser")?; let payload = make_element("rtph265pay", "pay0")?; - bin.add_many(&[&source, &queue, &parser, &payload])?; - Element::link_many(&[&source, &queue, &parser, &payload])?; + bin.add_many([&source, &queue, &parser, &payload])?; + Element::link_many([&source, &queue, &parser, &payload])?; let source = source .dynamic_cast::() @@ -1197,7 +1190,7 @@ fn build_aac(bin: &Element) -> Result { source.set_is_live(false); source.set_block(false); source.set_property("emit-signals", false); - source.set_max_bytes(52428800); + source.set_max_bytes(GST_BUFFER_SIZE); source.set_do_timestamp(false); source.set_stream_type(AppStreamType::Seekable); @@ -1214,8 +1207,8 @@ fn build_aac(bin: &Element) -> Result { let encoder = make_element("audioconvert", "audencoder")?; let payload = make_element("rtpL16pay", "pay1")?; - bin.add_many(&[&source, &queue, &parser, &decoder, &encoder, &payload])?; - Element::link_many(&[&source, &queue, &parser, &decoder, &encoder, &payload])?; + bin.add_many([&source, &queue, &parser, &decoder, &encoder, &payload])?; + Element::link_many([&source, &queue, &parser, &decoder, &encoder, &payload])?; let source = source .dynamic_cast::() @@ -1242,7 +1235,7 @@ fn build_adpcm(bin: &Element, block_size: u32) -> Result { source.set_is_live(false); source.set_block(false); source.set_property("emit-signals", false); - source.set_max_bytes(52428800); + source.set_max_bytes(GST_BUFFER_SIZE); source.set_do_timestamp(false); source.set_stream_type(AppStreamType::Seekable); @@ -1264,9 +1257,9 @@ fn build_adpcm(bin: &Element, block_size: u32) -> Result { 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])?; + 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 @@ -1314,25 +1307,45 @@ fn make_element(kind: &str, name: &str) -> AnyResult { }) } fn make_queue(name: &str) -> AnyResult { - // let queue = make_element("queue", name)?; - // queue.set_property_from_str("leaky", "downstream"); - // queue.set_property("max-size-bytes", 0u32); + let queue = make_element("queue", &format!("queue1_{}", name))?; + // queue.set_property("max-size-bytes", GST_BUFFER_SIZE as u32); // queue.set_property("max-size-buffers", 0u32); // queue.set_property( // "max-size-time", // std::convert::TryInto::::try_into(tokio::time::Duration::from_secs(5).as_nanos()) // .unwrap_or(0), // ); - // Ok(queue) - let queue = make_element("queue2", name)?; - // queue.set_property("max-size-bytes", 0u32); - // queue.set_property("max-size-buffers", 0u32); - queue.set_property( - "max-size-time", - std::convert::TryInto::::try_into(tokio::time::Duration::from_secs(5).as_nanos()) - .unwrap_or(0), - ); - queue.set_property("use-buffering", true); - Ok(queue) + let queue2 = make_element("queue2", &format!("queue2_{}", name))?; + // queue2.set_property("max-size-bytes", (GST_BUFFER_SIZE * 2u64 / 3u64) as u32 ); + // // queue.set_property("max-size-buffers", 0u32); + // queue2.set_property( + // "max-size-time", + // std::convert::TryInto::::try_into(tokio::time::Duration::from_secs(5).as_nanos()) + // .unwrap_or(0), + // ); + queue2.set_property("use-buffering", true); + + let bin = gstreamer::Bin::builder().name(name).build(); + bin.add_many([&queue, &queue2])?; + Element::link_many([&queue, &queue2])?; + + let pad = queue + .static_pad("sink") + .expect("Failed to get a static pad from queue."); + let ghost_pad = GhostPad::builder_with_target(&pad).unwrap().build(); + ghost_pad.set_active(true)?; + bin.add_pad(&ghost_pad)?; + + let pad = queue2 + .static_pad("src") + .expect("Failed to get a static pad from queue2."); + let ghost_pad = GhostPad::builder_with_target(&pad).unwrap().build(); + ghost_pad.set_active(true)?; + bin.add_pad(&ghost_pad)?; + + let bin = bin + .dynamic_cast::() + .map_err(|_| anyhow!("Cannot convert bin"))?; + Ok(bin) }