mirror of
https://github.com/v0l/zap-stream-core.git
synced 2025-06-16 01:28:08 +00:00
refactor: convert to workspace
This commit is contained in:
4818
crates/core/Cargo.lock
generated
Executable file
4818
crates/core/Cargo.lock
generated
Executable file
File diff suppressed because it is too large
Load Diff
44
crates/core/Cargo.toml
Normal file
44
crates/core/Cargo.toml
Normal file
@ -0,0 +1,44 @@
|
||||
[package]
|
||||
name = "zap-stream-core"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default = ["test-pattern", "srt", "rtmp"]
|
||||
srt = ["dep:srt-tokio"]
|
||||
rtmp = ["dep:rml_rtmp"]
|
||||
local-overseer = [] # WIP
|
||||
webhook-overseer = [] # WIP
|
||||
test-pattern = [
|
||||
"dep:resvg",
|
||||
"dep:usvg",
|
||||
"dep:tiny-skia",
|
||||
"dep:fontdue",
|
||||
"dep:ringbuf",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
ffmpeg-rs-raw.workspace = true
|
||||
tokio.workspace = true
|
||||
anyhow.workspace = true
|
||||
async-trait.workspace = true
|
||||
log.workspace = true
|
||||
uuid.workspace = true
|
||||
serde.workspace = true
|
||||
hex.workspace = true
|
||||
itertools.workspace = true
|
||||
futures-util = "0.3.30"
|
||||
m3u8-rs = "6.0.0"
|
||||
|
||||
# srt
|
||||
srt-tokio = { version = "0.4.3", optional = true }
|
||||
|
||||
# rtmp
|
||||
rml_rtmp = { version = "0.8.0", optional = true }
|
||||
|
||||
# test-pattern
|
||||
resvg = { version = "0.44.0", optional = true }
|
||||
usvg = { version = "0.44.0", optional = true }
|
||||
tiny-skia = { version = "0.11.4", optional = true }
|
||||
fontdue = { version = "0.9.2", optional = true }
|
||||
ringbuf = { version = "0.4.7", optional = true }
|
BIN
crates/core/SourceCodePro-Regular.ttf
Normal file
BIN
crates/core/SourceCodePro-Regular.ttf
Normal file
Binary file not shown.
2
crates/core/dev-setup/db.sql
Normal file
2
crates/core/dev-setup/db.sql
Normal file
@ -0,0 +1,2 @@
|
||||
create database route96;
|
||||
create database zap_stream;
|
29
crates/core/dev-setup/route96.toml
Normal file
29
crates/core/dev-setup/route96.toml
Normal file
@ -0,0 +1,29 @@
|
||||
# Listen address for webserver
|
||||
listen = "0.0.0.0:8000"
|
||||
|
||||
# Database connection string (MYSQL)
|
||||
database = "mysql://root:root@db:3306/route96"
|
||||
|
||||
# Directory to store uploads
|
||||
storage_dir = "./data"
|
||||
|
||||
# Maximum support filesize for uploading
|
||||
max_upload_bytes = 5e+9
|
||||
|
||||
# Public facing url
|
||||
public_url = "http://localhost:8881"
|
||||
|
||||
# Whitelisted pubkeys, leave out to disable
|
||||
# whitelist = ["63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed"]
|
||||
|
||||
# Path for ViT(224) image model (https://huggingface.co/google/vit-base-patch16-224)
|
||||
# vit_model_path = "model.safetennsors"
|
||||
|
||||
# Webhook api endpoint
|
||||
# webhook_url = "https://api.snort.social/api/v1/media/webhook"
|
||||
|
||||
# Analytics support
|
||||
# plausible_url = "https://plausible.com/"
|
||||
|
||||
# Support legacy void
|
||||
# void_cat_database = "postgres://postgres:postgres@localhost:41911/void"
|
144
crates/core/dev-setup/strfry.conf
Normal file
144
crates/core/dev-setup/strfry.conf
Normal file
@ -0,0 +1,144 @@
|
||||
##
|
||||
## Default strfry config
|
||||
##
|
||||
|
||||
# Directory that contains the strfry LMDB database (restart required)
|
||||
db = "./strfry-db/"
|
||||
|
||||
dbParams {
|
||||
# Maximum number of threads/processes that can simultaneously have LMDB transactions open (restart required)
|
||||
maxreaders = 256
|
||||
|
||||
# Size of mmap() to use when loading LMDB (default is 10TB, does *not* correspond to disk-space used) (restart required)
|
||||
mapsize = 10995116277760
|
||||
|
||||
# Disables read-ahead when accessing the LMDB mapping. Reduces IO activity when DB size is larger than RAM. (restart required)
|
||||
noReadAhead = false
|
||||
}
|
||||
|
||||
events {
|
||||
# Maximum size of normalised JSON, in bytes
|
||||
maxEventSize = 65536
|
||||
|
||||
# Events newer than this will be rejected
|
||||
rejectEventsNewerThanSeconds = 900
|
||||
|
||||
# Events older than this will be rejected
|
||||
rejectEventsOlderThanSeconds = 94608000
|
||||
|
||||
# Ephemeral events older than this will be rejected
|
||||
rejectEphemeralEventsOlderThanSeconds = 60
|
||||
|
||||
# Ephemeral events will be deleted from the DB when older than this
|
||||
ephemeralEventsLifetimeSeconds = 300
|
||||
|
||||
# Maximum number of tags allowed
|
||||
maxNumTags = 2000
|
||||
|
||||
# Maximum size for tag values, in bytes
|
||||
maxTagValSize = 1024
|
||||
}
|
||||
|
||||
relay {
|
||||
# Interface to listen on. Use 0.0.0.0 to listen on all interfaces (restart required)
|
||||
bind = "0.0.0.0"
|
||||
|
||||
# Port to open for the nostr websocket protocol (restart required)
|
||||
port = 7777
|
||||
|
||||
# Set OS-limit on maximum number of open files/sockets (if 0, don't attempt to set) (restart required)
|
||||
nofiles = 0
|
||||
|
||||
# HTTP header that contains the client's real IP, before reverse proxying (ie x-real-ip) (MUST be all lower-case)
|
||||
realIpHeader = ""
|
||||
|
||||
info {
|
||||
# NIP-11: Name of this server. Short/descriptive (< 30 characters)
|
||||
name = "strfry default"
|
||||
|
||||
# NIP-11: Detailed information about relay, free-form
|
||||
description = "This is a strfry instance."
|
||||
|
||||
# NIP-11: Administrative nostr pubkey, for contact purposes
|
||||
pubkey = ""
|
||||
|
||||
# NIP-11: Alternative administrative contact (email, website, etc)
|
||||
contact = ""
|
||||
|
||||
# NIP-11: URL pointing to an image to be used as an icon for the relay
|
||||
icon = ""
|
||||
|
||||
# List of supported lists as JSON array, or empty string to use default. Example: "[1,2]"
|
||||
nips = ""
|
||||
}
|
||||
|
||||
# Maximum accepted incoming websocket frame size (should be larger than max event) (restart required)
|
||||
maxWebsocketPayloadSize = 131072
|
||||
|
||||
# Websocket-level PING message frequency (should be less than any reverse proxy idle timeouts) (restart required)
|
||||
autoPingSeconds = 55
|
||||
|
||||
# If TCP keep-alive should be enabled (detect dropped connections to upstream reverse proxy)
|
||||
enableTcpKeepalive = false
|
||||
|
||||
# How much uninterrupted CPU time a REQ query should get during its DB scan
|
||||
queryTimesliceBudgetMicroseconds = 10000
|
||||
|
||||
# Maximum records that can be returned per filter
|
||||
maxFilterLimit = 500
|
||||
|
||||
# Maximum number of subscriptions (concurrent REQs) a connection can have open at any time
|
||||
maxSubsPerConnection = 20
|
||||
|
||||
writePolicy {
|
||||
# If non-empty, path to an executable script that implements the writePolicy plugin logic
|
||||
plugin = ""
|
||||
}
|
||||
|
||||
compression {
|
||||
# Use permessage-deflate compression if supported by client. Reduces bandwidth, but slight increase in CPU (restart required)
|
||||
enabled = true
|
||||
|
||||
# Maintain a sliding window buffer for each connection. Improves compression, but uses more memory (restart required)
|
||||
slidingWindow = true
|
||||
}
|
||||
|
||||
logging {
|
||||
# Dump all incoming messages
|
||||
dumpInAll = false
|
||||
|
||||
# Dump all incoming EVENT messages
|
||||
dumpInEvents = false
|
||||
|
||||
# Dump all incoming REQ/CLOSE messages
|
||||
dumpInReqs = false
|
||||
|
||||
# Log performance metrics for initial REQ database scans
|
||||
dbScanPerf = false
|
||||
|
||||
# Log reason for invalid event rejection? Can be disabled to silence excessive logging
|
||||
invalidEvents = true
|
||||
}
|
||||
|
||||
numThreads {
|
||||
# Ingester threads: route incoming requests, validate events/sigs (restart required)
|
||||
ingester = 3
|
||||
|
||||
# reqWorker threads: Handle initial DB scan for events (restart required)
|
||||
reqWorker = 3
|
||||
|
||||
# reqMonitor threads: Handle filtering of new events (restart required)
|
||||
reqMonitor = 3
|
||||
|
||||
# negentropy threads: Handle negentropy protocol messages (restart required)
|
||||
negentropy = 2
|
||||
}
|
||||
|
||||
negentropy {
|
||||
# Support negentropy protocol messages
|
||||
enabled = true
|
||||
|
||||
# Maximum records that sync will process before returning an error
|
||||
maxSyncEvents = 1000000
|
||||
}
|
||||
}
|
30
crates/core/src/egress/hls.rs
Normal file
30
crates/core/src/egress/hls.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use anyhow::Result;
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVPacket;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::egress::{Egress, EgressResult};
|
||||
use crate::mux::HlsMuxer;
|
||||
|
||||
/// Alias the muxer directly
|
||||
pub type HlsEgress = HlsMuxer;
|
||||
|
||||
impl Egress for HlsMuxer {
|
||||
unsafe fn process_pkt(
|
||||
&mut self,
|
||||
packet: *mut AVPacket,
|
||||
variant: &Uuid,
|
||||
) -> Result<EgressResult> {
|
||||
if let Some(ns) = self.mux_packet(packet, variant)? {
|
||||
Ok(EgressResult::NewSegment(ns))
|
||||
} else {
|
||||
Ok(EgressResult::None)
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn reset(&mut self) -> Result<()> {
|
||||
for var in &mut self.variants {
|
||||
var.reset()?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
43
crates/core/src/egress/mod.rs
Normal file
43
crates/core/src/egress/mod.rs
Normal file
@ -0,0 +1,43 @@
|
||||
use anyhow::Result;
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVPacket;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub mod hls;
|
||||
pub mod recorder;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct EgressConfig {
|
||||
pub name: String,
|
||||
/// Which variants will be used in this muxer
|
||||
pub variants: HashSet<Uuid>,
|
||||
}
|
||||
|
||||
pub trait Egress {
|
||||
unsafe fn process_pkt(&mut self, packet: *mut AVPacket, variant: &Uuid)
|
||||
-> Result<EgressResult>;
|
||||
unsafe fn reset(&mut self) -> Result<()>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum EgressResult {
|
||||
/// Nothing to report
|
||||
None,
|
||||
/// A new segment was created
|
||||
NewSegment(NewSegment),
|
||||
}
|
||||
|
||||
/// Basic details of new segment created by a muxer
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NewSegment {
|
||||
/// The id of the variant (video or audio)
|
||||
pub variant: Uuid,
|
||||
/// Segment index
|
||||
pub idx: u64,
|
||||
/// Duration in seconds
|
||||
pub duration: f32,
|
||||
/// Path on disk to the segment file
|
||||
pub path: PathBuf,
|
||||
}
|
70
crates/core/src/egress/recorder.rs
Normal file
70
crates/core/src/egress/recorder.rs
Normal file
@ -0,0 +1,70 @@
|
||||
use anyhow::Result;
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVPacket;
|
||||
use ffmpeg_rs_raw::{Encoder, Muxer};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::egress::{Egress, EgressResult};
|
||||
use crate::variant::{StreamMapping, VariantStream};
|
||||
|
||||
pub struct RecorderEgress {
|
||||
/// Pipeline ID
|
||||
id: Uuid,
|
||||
/// Internal muxer writing the output packets
|
||||
muxer: Muxer,
|
||||
/// Mapping from Variant ID to stream index
|
||||
var_map: HashMap<Uuid, i32>,
|
||||
}
|
||||
|
||||
impl RecorderEgress {
|
||||
pub fn new<'a>(
|
||||
id: &Uuid,
|
||||
out_dir: &str,
|
||||
variants: impl Iterator<Item = (&'a VariantStream, &'a Encoder)>,
|
||||
) -> Result<Self> {
|
||||
let base = PathBuf::from(out_dir).join(id.to_string());
|
||||
|
||||
let out_file = base.join("recording.ts");
|
||||
fs::create_dir_all(&base)?;
|
||||
|
||||
let mut var_map = HashMap::new();
|
||||
let muxer = unsafe {
|
||||
let mut m = Muxer::builder()
|
||||
.with_output_path(out_file.to_str().unwrap(), None)?
|
||||
.build()?;
|
||||
for (var, enc) in variants {
|
||||
let stream = m.add_stream_encoder(enc)?;
|
||||
var_map.insert(var.id(), (*stream).index);
|
||||
}
|
||||
m.open(None)?;
|
||||
m
|
||||
};
|
||||
Ok(Self {
|
||||
id: *id,
|
||||
muxer,
|
||||
var_map,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Egress for RecorderEgress {
|
||||
unsafe fn process_pkt(
|
||||
&mut self,
|
||||
packet: *mut AVPacket,
|
||||
variant: &Uuid,
|
||||
) -> Result<EgressResult> {
|
||||
if let Some(stream) = self.var_map.get(variant) {
|
||||
// very important for muxer to know which stream this pkt belongs to
|
||||
(*packet).stream_index = *stream;
|
||||
|
||||
self.muxer.write_packet(packet)?;
|
||||
}
|
||||
Ok(EgressResult::None)
|
||||
}
|
||||
|
||||
unsafe fn reset(&mut self) -> Result<()> {
|
||||
self.muxer.close()
|
||||
}
|
||||
}
|
28
crates/core/src/ingress/file.rs
Normal file
28
crates/core/src/ingress/file.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use crate::ingress::{spawn_pipeline, ConnectionInfo};
|
||||
use crate::overseer::Overseer;
|
||||
use anyhow::Result;
|
||||
use log::info;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
pub async fn listen(out_dir: String, path: PathBuf, overseer: Arc<dyn Overseer>) -> Result<()> {
|
||||
info!("Sending file: {}", path.display());
|
||||
|
||||
let info = ConnectionInfo {
|
||||
ip_addr: "127.0.0.1:6969".to_string(),
|
||||
endpoint: "file-input".to_owned(),
|
||||
app_name: "".to_string(),
|
||||
key: "test".to_string(),
|
||||
};
|
||||
let file = std::fs::File::open(path)?;
|
||||
spawn_pipeline(
|
||||
Handle::current(),
|
||||
info,
|
||||
out_dir.clone(),
|
||||
overseer.clone(),
|
||||
Box::new(file),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
69
crates/core/src/ingress/mod.rs
Normal file
69
crates/core/src/ingress/mod.rs
Normal file
@ -0,0 +1,69 @@
|
||||
use crate::overseer::Overseer;
|
||||
use crate::pipeline::runner::PipelineRunner;
|
||||
use log::{error, info};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::Read;
|
||||
use std::sync::Arc;
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
pub mod file;
|
||||
#[cfg(feature = "rtmp")]
|
||||
pub mod rtmp;
|
||||
#[cfg(feature = "srt")]
|
||||
pub mod srt;
|
||||
pub mod tcp;
|
||||
#[cfg(feature = "test-pattern")]
|
||||
pub mod test;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct ConnectionInfo {
|
||||
/// Endpoint of the ingress
|
||||
pub endpoint: String,
|
||||
|
||||
/// IP address of the connection
|
||||
pub ip_addr: String,
|
||||
|
||||
/// App name, empty unless RTMP ingress
|
||||
pub app_name: String,
|
||||
|
||||
/// Stream key
|
||||
pub key: String,
|
||||
}
|
||||
|
||||
pub fn spawn_pipeline(
|
||||
handle: Handle,
|
||||
info: ConnectionInfo,
|
||||
out_dir: String,
|
||||
seer: Arc<dyn Overseer>,
|
||||
reader: Box<dyn Read + Send>,
|
||||
) {
|
||||
info!("New client connected: {}", &info.ip_addr);
|
||||
let seer = seer.clone();
|
||||
let out_dir = out_dir.to_string();
|
||||
std::thread::spawn(move || unsafe {
|
||||
match PipelineRunner::new(handle, out_dir, seer, info, reader) {
|
||||
Ok(mut pl) => loop {
|
||||
match pl.run() {
|
||||
Ok(c) => {
|
||||
if !c {
|
||||
if let Err(e) = pl.flush() {
|
||||
error!("Pipeline flush failed: {}", e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
if let Err(e) = pl.flush() {
|
||||
error!("Pipeline flush failed: {}", e);
|
||||
}
|
||||
error!("Pipeline run failed: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
error!("Failed to create PipelineRunner: {}", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
238
crates/core/src/ingress/rtmp.rs
Normal file
238
crates/core/src/ingress/rtmp.rs
Normal file
@ -0,0 +1,238 @@
|
||||
use crate::ingress::{spawn_pipeline, ConnectionInfo};
|
||||
use crate::overseer::Overseer;
|
||||
use anyhow::{bail, Result};
|
||||
use log::{error, info};
|
||||
use rml_rtmp::handshake::{Handshake, HandshakeProcessResult, PeerType};
|
||||
use rml_rtmp::sessions::{
|
||||
ServerSession, ServerSessionConfig, ServerSessionEvent, ServerSessionResult,
|
||||
};
|
||||
use std::collections::VecDeque;
|
||||
use std::io::{ErrorKind, Read, Write};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::runtime::Handle;
|
||||
use tokio::time::Instant;
|
||||
#[derive(PartialEq, Eq, Clone, Hash)]
|
||||
struct RtmpPublishedStream(String, String);
|
||||
|
||||
struct RtmpClient {
|
||||
socket: std::net::TcpStream,
|
||||
media_buf: Vec<u8>,
|
||||
session: ServerSession,
|
||||
msg_queue: VecDeque<ServerSessionResult>,
|
||||
reader_buf: [u8; 4096],
|
||||
pub published_stream: Option<RtmpPublishedStream>,
|
||||
}
|
||||
|
||||
impl RtmpClient {
|
||||
async fn start(mut socket: TcpStream) -> Result<Self> {
|
||||
let mut hs = Handshake::new(PeerType::Server);
|
||||
|
||||
let exchange = hs.generate_outbound_p0_and_p1()?;
|
||||
socket.write_all(&exchange).await?;
|
||||
|
||||
let mut buf = [0; 4096];
|
||||
loop {
|
||||
let r = socket.read(&mut buf).await?;
|
||||
if r == 0 {
|
||||
bail!("EOF reached while reading");
|
||||
}
|
||||
|
||||
match hs.process_bytes(&buf[..r])? {
|
||||
HandshakeProcessResult::InProgress { response_bytes } => {
|
||||
socket.write_all(&response_bytes).await?;
|
||||
}
|
||||
HandshakeProcessResult::Completed {
|
||||
response_bytes,
|
||||
remaining_bytes,
|
||||
} => {
|
||||
socket.write_all(&response_bytes).await?;
|
||||
|
||||
let cfg = ServerSessionConfig::new();
|
||||
let (mut ses, mut res) = ServerSession::new(cfg)?;
|
||||
let q = ses.handle_input(&remaining_bytes)?;
|
||||
res.extend(q);
|
||||
|
||||
let ret = Self {
|
||||
socket: socket.into_std()?,
|
||||
media_buf: vec![],
|
||||
session: ses,
|
||||
msg_queue: VecDeque::from(res),
|
||||
reader_buf: [0; 4096],
|
||||
published_stream: None,
|
||||
};
|
||||
|
||||
return Ok(ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Read data until we get the publish request
|
||||
pub fn read_until_publish_request(&mut self, timeout: Duration) -> Result<()> {
|
||||
let start = Instant::now();
|
||||
while self.published_stream.is_none() {
|
||||
if (Instant::now() - start) > timeout {
|
||||
bail!("Timed out waiting for publish request");
|
||||
}
|
||||
self.read_data()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_data(&mut self) -> Result<()> {
|
||||
let r = match self.socket.read(&mut self.reader_buf) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
return match e.kind() {
|
||||
ErrorKind::WouldBlock => Ok(()),
|
||||
ErrorKind::Interrupted => Ok(()),
|
||||
_ => Err(anyhow::Error::new(e)),
|
||||
};
|
||||
}
|
||||
};
|
||||
if r == 0 {
|
||||
bail!("EOF");
|
||||
}
|
||||
|
||||
let mx = self.session.handle_input(&self.reader_buf[..r])?;
|
||||
if !mx.is_empty() {
|
||||
self.msg_queue.extend(mx);
|
||||
self.process_msg_queue()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_msg_queue(&mut self) -> Result<()> {
|
||||
while let Some(msg) = self.msg_queue.pop_front() {
|
||||
match msg {
|
||||
ServerSessionResult::OutboundResponse(data) => {
|
||||
self.socket.write_all(&data.bytes)?
|
||||
}
|
||||
ServerSessionResult::RaisedEvent(ev) => self.handle_event(ev)?,
|
||||
ServerSessionResult::UnhandleableMessageReceived(m) => {
|
||||
// treat any non-flv streams as raw media stream in rtmp
|
||||
self.media_buf.extend(&m.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_event(&mut self, event: ServerSessionEvent) -> Result<()> {
|
||||
match event {
|
||||
ServerSessionEvent::ClientChunkSizeChanged { new_chunk_size } => {
|
||||
info!("New client chunk size: {}", new_chunk_size);
|
||||
}
|
||||
ServerSessionEvent::ConnectionRequested { request_id, .. } => {
|
||||
let mx = self.session.accept_request(request_id)?;
|
||||
self.msg_queue.extend(mx);
|
||||
}
|
||||
ServerSessionEvent::ReleaseStreamRequested { .. } => {}
|
||||
ServerSessionEvent::PublishStreamRequested {
|
||||
request_id,
|
||||
app_name,
|
||||
stream_key,
|
||||
mode,
|
||||
} => {
|
||||
if self.published_stream.is_some() {
|
||||
let mx =
|
||||
self.session
|
||||
.reject_request(request_id, "0", "stream already published")?;
|
||||
self.msg_queue.extend(mx);
|
||||
} else {
|
||||
let mx = self.session.accept_request(request_id)?;
|
||||
self.msg_queue.extend(mx);
|
||||
info!(
|
||||
"Published stream request: {app_name}/{stream_key} [{:?}]",
|
||||
mode
|
||||
);
|
||||
self.published_stream = Some(RtmpPublishedStream(app_name, stream_key));
|
||||
}
|
||||
}
|
||||
ServerSessionEvent::PublishStreamFinished { .. } => {}
|
||||
ServerSessionEvent::StreamMetadataChanged {
|
||||
app_name,
|
||||
stream_key,
|
||||
metadata,
|
||||
} => {
|
||||
info!(
|
||||
"Metadata configured: {}/{} {:?}",
|
||||
app_name, stream_key, metadata
|
||||
);
|
||||
}
|
||||
ServerSessionEvent::AudioDataReceived { data, .. } => {
|
||||
self.media_buf.extend(data);
|
||||
}
|
||||
ServerSessionEvent::VideoDataReceived { data, .. } => {
|
||||
self.media_buf.extend(data);
|
||||
}
|
||||
ServerSessionEvent::UnhandleableAmf0Command { .. } => {}
|
||||
ServerSessionEvent::PlayStreamRequested { request_id, .. } => {
|
||||
let mx = self
|
||||
.session
|
||||
.reject_request(request_id, "0", "playback not supported")?;
|
||||
self.msg_queue.extend(mx);
|
||||
}
|
||||
ServerSessionEvent::PlayStreamFinished { .. } => {}
|
||||
ServerSessionEvent::AcknowledgementReceived { .. } => {}
|
||||
ServerSessionEvent::PingResponseReceived { .. } => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for RtmpClient {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
// block this thread until something comes into [media_buf]
|
||||
while self.media_buf.is_empty() {
|
||||
if let Err(e) = self.read_data() {
|
||||
error!("Error reading data: {}", e);
|
||||
return Ok(0);
|
||||
};
|
||||
}
|
||||
|
||||
let to_read = buf.len().min(self.media_buf.len());
|
||||
let drain = self.media_buf.drain(..to_read);
|
||||
buf[..to_read].copy_from_slice(drain.as_slice());
|
||||
Ok(to_read)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn listen(out_dir: String, addr: String, overseer: Arc<dyn Overseer>) -> Result<()> {
|
||||
let listener = TcpListener::bind(&addr).await?;
|
||||
|
||||
info!("RTMP listening on: {}", &addr);
|
||||
while let Ok((socket, ip)) = listener.accept().await {
|
||||
let mut cc = RtmpClient::start(socket).await?;
|
||||
let addr = addr.clone();
|
||||
let overseer = overseer.clone();
|
||||
let out_dir = out_dir.clone();
|
||||
let handle = Handle::current();
|
||||
std::thread::Builder::new()
|
||||
.name("rtmp-client".to_string())
|
||||
.spawn(move || {
|
||||
if let Err(e) = cc.read_until_publish_request(Duration::from_secs(10)) {
|
||||
error!("{}", e);
|
||||
} else {
|
||||
let pr = cc.published_stream.as_ref().unwrap();
|
||||
let info = ConnectionInfo {
|
||||
ip_addr: ip.to_string(),
|
||||
endpoint: addr.clone(),
|
||||
app_name: pr.0.clone(),
|
||||
key: pr.1.clone(),
|
||||
};
|
||||
spawn_pipeline(
|
||||
handle,
|
||||
info,
|
||||
out_dir.clone(),
|
||||
overseer.clone(),
|
||||
Box::new(cc),
|
||||
);
|
||||
}
|
||||
})?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
66
crates/core/src/ingress/srt.rs
Normal file
66
crates/core/src/ingress/srt.rs
Normal file
@ -0,0 +1,66 @@
|
||||
use crate::ingress::{spawn_pipeline, ConnectionInfo};
|
||||
use crate::overseer::Overseer;
|
||||
use anyhow::Result;
|
||||
use futures_util::stream::FusedStream;
|
||||
use futures_util::StreamExt;
|
||||
use log::info;
|
||||
use srt_tokio::{SrtListener, SrtSocket};
|
||||
use std::io::Read;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
pub async fn listen(out_dir: String, addr: String, overseer: Arc<dyn Overseer>) -> Result<()> {
|
||||
let binder: SocketAddr = addr.parse()?;
|
||||
let (_binding, mut packets) = SrtListener::builder().bind(binder).await?;
|
||||
|
||||
info!("SRT listening on: {}", &addr);
|
||||
while let Some(request) = packets.incoming().next().await {
|
||||
let socket = request.accept(None).await?;
|
||||
let info = ConnectionInfo {
|
||||
endpoint: addr.clone(),
|
||||
ip_addr: socket.settings().remote.to_string(),
|
||||
app_name: "".to_string(),
|
||||
key: socket
|
||||
.settings()
|
||||
.stream_id
|
||||
.as_ref()
|
||||
.map_or(String::new(), |s| s.to_string()),
|
||||
};
|
||||
spawn_pipeline(
|
||||
Handle::current(),
|
||||
info,
|
||||
out_dir.clone(),
|
||||
overseer.clone(),
|
||||
Box::new(SrtReader {
|
||||
handle: Handle::current(),
|
||||
socket,
|
||||
buf: Vec::with_capacity(4096),
|
||||
}),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct SrtReader {
|
||||
pub handle: Handle,
|
||||
pub socket: SrtSocket,
|
||||
pub buf: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Read for SrtReader {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
let (mut rx, _) = self.socket.split_mut();
|
||||
while self.buf.len() < buf.len() {
|
||||
if rx.is_terminated() {
|
||||
return Ok(0);
|
||||
}
|
||||
if let Some((_, data)) = self.handle.block_on(rx.next()) {
|
||||
self.buf.extend(data.iter().as_slice());
|
||||
}
|
||||
}
|
||||
let drain = self.buf.drain(..buf.len());
|
||||
buf.copy_from_slice(drain.as_slice());
|
||||
Ok(buf.len())
|
||||
}
|
||||
}
|
30
crates/core/src/ingress/tcp.rs
Normal file
30
crates/core/src/ingress/tcp.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use crate::ingress::{spawn_pipeline, ConnectionInfo};
|
||||
use crate::overseer::Overseer;
|
||||
use anyhow::Result;
|
||||
use log::info;
|
||||
use std::sync::Arc;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
pub async fn listen(out_dir: String, addr: String, overseer: Arc<dyn Overseer>) -> Result<()> {
|
||||
let listener = TcpListener::bind(&addr).await?;
|
||||
|
||||
info!("TCP listening on: {}", &addr);
|
||||
while let Ok((socket, ip)) = listener.accept().await {
|
||||
let info = ConnectionInfo {
|
||||
ip_addr: ip.to_string(),
|
||||
endpoint: addr.clone(),
|
||||
app_name: "".to_string(),
|
||||
key: "no-key-tcp".to_string(),
|
||||
};
|
||||
let socket = socket.into_std()?;
|
||||
spawn_pipeline(
|
||||
Handle::current(),
|
||||
info,
|
||||
out_dir.clone(),
|
||||
overseer.clone(),
|
||||
Box::new(socket),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
187
crates/core/src/ingress/test.rs
Normal file
187
crates/core/src/ingress/test.rs
Normal file
@ -0,0 +1,187 @@
|
||||
use crate::ingress::{spawn_pipeline, ConnectionInfo};
|
||||
use crate::overseer::Overseer;
|
||||
use anyhow::Result;
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVColorSpace::AVCOL_SPC_RGB;
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVPictureType::AV_PICTURE_TYPE_NONE;
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVPixelFormat::{AV_PIX_FMT_RGBA, AV_PIX_FMT_YUV420P};
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::{
|
||||
av_frame_alloc, av_frame_free, av_frame_get_buffer, av_packet_free, AV_PROFILE_H264_MAIN,
|
||||
};
|
||||
use ffmpeg_rs_raw::{Encoder, Muxer, Scaler};
|
||||
use fontdue::layout::{CoordinateSystem, Layout, TextStyle};
|
||||
use fontdue::Font;
|
||||
use log::info;
|
||||
use ringbuf::traits::{Observer, Split};
|
||||
use ringbuf::{HeapCons, HeapRb};
|
||||
use std::io::Read;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use tiny_skia::Pixmap;
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
pub async fn listen(out_dir: String, overseer: Arc<dyn Overseer>) -> Result<()> {
|
||||
info!("Test pattern enabled");
|
||||
|
||||
let info = ConnectionInfo {
|
||||
endpoint: "test-pattern".to_string(),
|
||||
ip_addr: "test-pattern".to_string(),
|
||||
app_name: "".to_string(),
|
||||
key: "test".to_string(),
|
||||
};
|
||||
let src = TestPatternSrc::new()?;
|
||||
spawn_pipeline(
|
||||
Handle::current(),
|
||||
info,
|
||||
out_dir.clone(),
|
||||
overseer.clone(),
|
||||
Box::new(src),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct TestPatternSrc {
|
||||
encoder: Encoder,
|
||||
scaler: Scaler,
|
||||
muxer: Muxer,
|
||||
background: Pixmap,
|
||||
font: [Font; 1],
|
||||
frame_no: u64,
|
||||
start: Instant,
|
||||
reader: HeapCons<u8>,
|
||||
}
|
||||
|
||||
unsafe impl Send for TestPatternSrc {}
|
||||
|
||||
impl TestPatternSrc {
|
||||
pub fn new() -> Result<Self> {
|
||||
let scaler = Scaler::new();
|
||||
let encoder = unsafe {
|
||||
Encoder::new_with_name("libx264")?
|
||||
.with_stream_index(0)
|
||||
.with_framerate(30.0)?
|
||||
.with_bitrate(1_000_000)
|
||||
.with_pix_fmt(AV_PIX_FMT_YUV420P)
|
||||
.with_width(1280)
|
||||
.with_height(720)
|
||||
.with_level(51)
|
||||
.with_profile(AV_PROFILE_H264_MAIN)
|
||||
.open(None)?
|
||||
};
|
||||
|
||||
let svg_data = include_bytes!("../../test.svg");
|
||||
let tree = usvg::Tree::from_data(svg_data, &Default::default())?;
|
||||
let mut pixmap = Pixmap::new(1280, 720).unwrap();
|
||||
let render_ts = tiny_skia::Transform::from_scale(
|
||||
pixmap.width() as f32 / tree.size().width(),
|
||||
pixmap.height() as f32 / tree.size().height(),
|
||||
);
|
||||
resvg::render(&tree, render_ts, &mut pixmap.as_mut());
|
||||
|
||||
let font = include_bytes!("../../SourceCodePro-Regular.ttf") as &[u8];
|
||||
let font = Font::from_bytes(font, Default::default()).unwrap();
|
||||
|
||||
let buf = HeapRb::new(1024 * 1024);
|
||||
let (writer, reader) = buf.split();
|
||||
|
||||
let muxer = unsafe {
|
||||
let mut m = Muxer::builder()
|
||||
.with_output_write(writer, Some("mpegts"))?
|
||||
.with_stream_encoder(&encoder)?
|
||||
.build()?;
|
||||
m.open(None)?;
|
||||
m
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
encoder,
|
||||
scaler,
|
||||
muxer,
|
||||
background: pixmap,
|
||||
font: [font],
|
||||
frame_no: 0,
|
||||
start: Instant::now(),
|
||||
reader,
|
||||
})
|
||||
}
|
||||
|
||||
pub unsafe fn next_pkt(&mut self) -> Result<()> {
|
||||
let stream_time = Duration::from_secs_f64(self.frame_no as f64 / 30.0);
|
||||
let real_time = Instant::now().duration_since(self.start);
|
||||
let wait_time = if stream_time > real_time {
|
||||
stream_time - real_time
|
||||
} else {
|
||||
Duration::new(0, 0)
|
||||
};
|
||||
if !wait_time.is_zero() {
|
||||
std::thread::sleep(wait_time);
|
||||
}
|
||||
|
||||
self.frame_no += 1;
|
||||
|
||||
let mut src_frame = unsafe {
|
||||
let src_frame = av_frame_alloc();
|
||||
|
||||
(*src_frame).width = 1280;
|
||||
(*src_frame).height = 720;
|
||||
(*src_frame).pict_type = AV_PICTURE_TYPE_NONE;
|
||||
(*src_frame).key_frame = 1;
|
||||
(*src_frame).colorspace = AVCOL_SPC_RGB;
|
||||
(*src_frame).format = AV_PIX_FMT_RGBA as _;
|
||||
(*src_frame).pts = self.frame_no as i64;
|
||||
(*src_frame).duration = 1;
|
||||
av_frame_get_buffer(src_frame, 0);
|
||||
|
||||
self.background
|
||||
.data()
|
||||
.as_ptr()
|
||||
.copy_to((*src_frame).data[0] as *mut _, 1280 * 720 * 4);
|
||||
src_frame
|
||||
};
|
||||
let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
|
||||
layout.clear();
|
||||
layout.append(
|
||||
&self.font,
|
||||
&TextStyle::new(&format!("frame={}", self.frame_no), 40.0, 0),
|
||||
);
|
||||
for g in layout.glyphs() {
|
||||
let (metrics, bitmap) = self.font[0].rasterize_config_subpixel(g.key);
|
||||
for y in 0..metrics.height {
|
||||
for x in 0..metrics.width {
|
||||
let dst_x = x + g.x as usize;
|
||||
let dst_y = y + g.y as usize;
|
||||
let offset_src = (x + y * metrics.width) * 3;
|
||||
unsafe {
|
||||
let offset_dst = 4 * dst_x + dst_y * (*src_frame).linesize[0] as usize;
|
||||
let pixel_dst = (*src_frame).data[0].add(offset_dst);
|
||||
*pixel_dst.offset(0) = bitmap[offset_src];
|
||||
*pixel_dst.offset(1) = bitmap[offset_src + 1];
|
||||
*pixel_dst.offset(2) = bitmap[offset_src + 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// scale/encode
|
||||
let mut frame = self
|
||||
.scaler
|
||||
.process_frame(src_frame, 1280, 720, AV_PIX_FMT_YUV420P)?;
|
||||
for mut pkt in self.encoder.encode_frame(frame)? {
|
||||
self.muxer.write_packet(pkt)?;
|
||||
av_packet_free(&mut pkt);
|
||||
}
|
||||
av_frame_free(&mut frame);
|
||||
av_frame_free(&mut src_frame);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for TestPatternSrc {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
unsafe {
|
||||
while self.reader.occupied_len() < buf.len() {
|
||||
self.next_pkt().map_err(std::io::Error::other)?;
|
||||
}
|
||||
}
|
||||
self.reader.read(buf)
|
||||
}
|
||||
}
|
6
crates/core/src/lib.rs
Normal file
6
crates/core/src/lib.rs
Normal file
@ -0,0 +1,6 @@
|
||||
pub mod egress;
|
||||
pub mod ingress;
|
||||
pub mod mux;
|
||||
pub mod overseer;
|
||||
pub mod pipeline;
|
||||
pub mod variant;
|
443
crates/core/src/mux/hls.rs
Normal file
443
crates/core/src/mux/hls.rs
Normal file
@ -0,0 +1,443 @@
|
||||
use crate::egress::NewSegment;
|
||||
use crate::variant::{StreamMapping, VariantStream};
|
||||
use anyhow::{bail, Result};
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVCodecID::AV_CODEC_ID_H264;
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVMediaType::AVMEDIA_TYPE_VIDEO;
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::{
|
||||
av_free, av_opt_set, av_q2d, av_write_frame, avio_flush, avio_open, AVPacket, AVStream,
|
||||
AVIO_FLAG_WRITE, AV_PKT_FLAG_KEY,
|
||||
};
|
||||
use ffmpeg_rs_raw::{cstr, Encoder, Muxer};
|
||||
use itertools::Itertools;
|
||||
use log::{info, warn};
|
||||
use m3u8_rs::MediaSegment;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Display;
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
use std::ptr;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum SegmentType {
|
||||
MPEGTS,
|
||||
FMP4,
|
||||
}
|
||||
|
||||
pub enum HlsVariantStream {
|
||||
Video {
|
||||
group: usize,
|
||||
index: usize,
|
||||
id: Uuid,
|
||||
},
|
||||
Audio {
|
||||
group: usize,
|
||||
index: usize,
|
||||
id: Uuid,
|
||||
},
|
||||
Subtitle {
|
||||
group: usize,
|
||||
index: usize,
|
||||
id: Uuid,
|
||||
},
|
||||
}
|
||||
|
||||
impl HlsVariantStream {
|
||||
pub fn id(&self) -> &Uuid {
|
||||
match self {
|
||||
HlsVariantStream::Video { id, .. } => id,
|
||||
HlsVariantStream::Audio { id, .. } => id,
|
||||
HlsVariantStream::Subtitle { id, .. } => id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn index(&self) -> &usize {
|
||||
match self {
|
||||
HlsVariantStream::Video { index, .. } => index,
|
||||
HlsVariantStream::Audio { index, .. } => index,
|
||||
HlsVariantStream::Subtitle { index, .. } => index,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for HlsVariantStream {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
HlsVariantStream::Video { index, .. } => write!(f, "v:{}", index),
|
||||
HlsVariantStream::Audio { index, .. } => write!(f, "a:{}", index),
|
||||
HlsVariantStream::Subtitle { index, .. } => write!(f, "s:{}", index),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct HlsVariant {
|
||||
/// Name of this variant (720p)
|
||||
pub name: String,
|
||||
/// MPEG-TS muxer for this variant
|
||||
pub mux: Muxer,
|
||||
/// List of streams ids in this variant
|
||||
pub streams: Vec<HlsVariantStream>,
|
||||
/// Segment length in seconds
|
||||
pub segment_length: f32,
|
||||
/// Current segment index
|
||||
pub idx: u64,
|
||||
/// Current segment start time in seconds (duration)
|
||||
pub pkt_start: f32,
|
||||
/// Output directory (base)
|
||||
pub out_dir: String,
|
||||
/// List of segments to be included in the playlist
|
||||
pub segments: Vec<SegmentInfo>,
|
||||
/// Type of segments to create
|
||||
pub segment_type: SegmentType,
|
||||
}
|
||||
|
||||
struct SegmentInfo(u64, f32, SegmentType);
|
||||
|
||||
impl SegmentInfo {
|
||||
fn to_media_segment(&self) -> MediaSegment {
|
||||
MediaSegment {
|
||||
uri: self.filename(),
|
||||
duration: self.1,
|
||||
title: None,
|
||||
..MediaSegment::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn filename(&self) -> String {
|
||||
HlsVariant::segment_name(self.2, self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl HlsVariant {
|
||||
pub fn new<'a>(
|
||||
out_dir: &'a str,
|
||||
segment_length: f32,
|
||||
group: usize,
|
||||
encoded_vars: impl Iterator<Item = (&'a VariantStream, &'a Encoder)>,
|
||||
segment_type: SegmentType,
|
||||
) -> Result<Self> {
|
||||
let name = format!("stream_{}", group);
|
||||
let first_seg = Self::map_segment_path(out_dir, &name, 1, segment_type);
|
||||
std::fs::create_dir_all(PathBuf::from(&first_seg).parent().unwrap())?;
|
||||
|
||||
let mut opts = HashMap::new();
|
||||
if let SegmentType::FMP4 = segment_type {
|
||||
opts.insert("fflags".to_string(), "-autobsf".to_string());
|
||||
opts.insert(
|
||||
"movflags".to_string(),
|
||||
"+frag_custom+dash+delay_moov".to_string(),
|
||||
);
|
||||
};
|
||||
let mut mux = unsafe {
|
||||
Muxer::builder()
|
||||
.with_output_path(
|
||||
first_seg.as_str(),
|
||||
match segment_type {
|
||||
SegmentType::MPEGTS => Some("mpegts"),
|
||||
SegmentType::FMP4 => Some("mp4"),
|
||||
},
|
||||
)?
|
||||
.build()?
|
||||
};
|
||||
let mut streams = Vec::new();
|
||||
for (var, enc) in encoded_vars {
|
||||
match var {
|
||||
VariantStream::Video(v) => unsafe {
|
||||
let stream = mux.add_stream_encoder(enc)?;
|
||||
streams.push(HlsVariantStream::Video {
|
||||
group,
|
||||
index: (*stream).index as usize,
|
||||
id: v.id(),
|
||||
})
|
||||
},
|
||||
VariantStream::Audio(a) => unsafe {
|
||||
let stream = mux.add_stream_encoder(enc)?;
|
||||
streams.push(HlsVariantStream::Audio {
|
||||
group,
|
||||
index: (*stream).index as usize,
|
||||
id: a.id(),
|
||||
})
|
||||
},
|
||||
VariantStream::Subtitle(s) => unsafe {
|
||||
let stream = mux.add_stream_encoder(enc)?;
|
||||
streams.push(HlsVariantStream::Subtitle {
|
||||
group,
|
||||
index: (*stream).index as usize,
|
||||
id: s.id(),
|
||||
})
|
||||
},
|
||||
_ => panic!("unsupported variant stream"),
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
mux.open(Some(opts))?;
|
||||
}
|
||||
Ok(Self {
|
||||
name: name.clone(),
|
||||
segment_length,
|
||||
mux,
|
||||
streams,
|
||||
idx: 1,
|
||||
pkt_start: 0.0,
|
||||
segments: Vec::from([SegmentInfo(1, segment_length, segment_type)]),
|
||||
out_dir: out_dir.to_string(),
|
||||
segment_type,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn segment_name(t: SegmentType, idx: u64) -> String {
|
||||
match t {
|
||||
SegmentType::MPEGTS => format!("{}.ts", idx),
|
||||
SegmentType::FMP4 => format!("{}.m4s", idx),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn out_dir(&self) -> PathBuf {
|
||||
PathBuf::from(&self.out_dir).join(&self.name)
|
||||
}
|
||||
|
||||
pub fn map_segment_path(out_dir: &str, name: &str, idx: u64, typ: SegmentType) -> String {
|
||||
PathBuf::from(out_dir)
|
||||
.join(name)
|
||||
.join(Self::segment_name(typ, idx))
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// Mux a packet created by the encoder for this variant
|
||||
pub unsafe fn mux_packet(&mut self, pkt: *mut AVPacket) -> Result<Option<NewSegment>> {
|
||||
let pkt_q = av_q2d((*pkt).time_base);
|
||||
// time of this packet in seconds
|
||||
let pkt_time = (*pkt).pts as f32 * pkt_q as f32;
|
||||
// what segment this pkt should be in (index)
|
||||
let pkt_seg = 1 + (pkt_time / self.segment_length).floor() as u64;
|
||||
|
||||
let mut result = None;
|
||||
let pkt_stream = *(*self.mux.context())
|
||||
.streams
|
||||
.add((*pkt).stream_index as usize);
|
||||
let can_split = (*pkt).flags & AV_PKT_FLAG_KEY == AV_PKT_FLAG_KEY
|
||||
&& (*(*pkt_stream).codecpar).codec_type == AVMEDIA_TYPE_VIDEO;
|
||||
if pkt_seg != self.idx && can_split {
|
||||
result = Some(self.split_next_seg(pkt_time)?);
|
||||
}
|
||||
self.mux.write_packet(pkt)?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub unsafe fn reset(&mut self) -> Result<()> {
|
||||
self.mux.close()
|
||||
}
|
||||
|
||||
unsafe fn split_next_seg(&mut self, pkt_time: f32) -> Result<NewSegment> {
|
||||
self.idx += 1;
|
||||
|
||||
// Manually reset muxer avio
|
||||
let ctx = self.mux.context();
|
||||
av_write_frame(ctx, ptr::null_mut());
|
||||
avio_flush((*ctx).pb);
|
||||
av_free((*ctx).url as *mut _);
|
||||
|
||||
let next_seg_url =
|
||||
Self::map_segment_path(&self.out_dir, &self.name, self.idx, self.segment_type);
|
||||
(*ctx).url = cstr!(next_seg_url.as_str());
|
||||
|
||||
let ret = avio_open(&mut (*ctx).pb, (*ctx).url, AVIO_FLAG_WRITE);
|
||||
if ret < 0 {
|
||||
bail!("Failed to re-init avio");
|
||||
}
|
||||
|
||||
// tell muxer it needs to write headers again
|
||||
av_opt_set(
|
||||
(*ctx).priv_data,
|
||||
cstr!("events_flags"),
|
||||
cstr!("resend_headers"),
|
||||
0,
|
||||
);
|
||||
|
||||
let duration = pkt_time - self.pkt_start;
|
||||
info!("Writing segment {} [{}s]", &next_seg_url, duration);
|
||||
if let Err(e) = self.add_segment(self.idx, duration) {
|
||||
warn!("Failed to update playlist: {}", e);
|
||||
}
|
||||
|
||||
/// Get the video variant for this group
|
||||
/// since this could actually be audio which would not be useful for
|
||||
/// [Overseer] impl
|
||||
let video_var = self.video_stream().unwrap_or(self.streams.first().unwrap());
|
||||
|
||||
// emit result of the previously completed segment,
|
||||
let prev_seg = self.idx - 1;
|
||||
let ret = NewSegment {
|
||||
variant: *video_var.id(),
|
||||
idx: prev_seg,
|
||||
duration,
|
||||
path: PathBuf::from(Self::map_segment_path(
|
||||
&self.out_dir,
|
||||
&self.name,
|
||||
prev_seg,
|
||||
self.segment_type,
|
||||
)),
|
||||
};
|
||||
self.pkt_start = pkt_time;
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
fn video_stream(&self) -> Option<&HlsVariantStream> {
|
||||
self.streams
|
||||
.iter()
|
||||
.find(|a| matches!(*a, HlsVariantStream::Video { .. }))
|
||||
}
|
||||
|
||||
fn add_segment(&mut self, idx: u64, duration: f32) -> Result<()> {
|
||||
self.segments
|
||||
.push(SegmentInfo(idx, duration, self.segment_type));
|
||||
|
||||
const MAX_SEGMENTS: usize = 10;
|
||||
|
||||
if self.segments.len() > MAX_SEGMENTS {
|
||||
let n_drain = self.segments.len() - MAX_SEGMENTS;
|
||||
let seg_dir = self.out_dir();
|
||||
for seg in self.segments.drain(..n_drain) {
|
||||
// delete file
|
||||
let seg_path = seg_dir.join(seg.filename());
|
||||
std::fs::remove_file(seg_path)?;
|
||||
}
|
||||
}
|
||||
self.write_playlist()
|
||||
}
|
||||
|
||||
fn write_playlist(&mut self) -> Result<()> {
|
||||
let mut pl = m3u8_rs::MediaPlaylist::default();
|
||||
pl.target_duration = self.segment_length as u64;
|
||||
pl.segments = self.segments.iter().map(|s| s.to_media_segment()).collect();
|
||||
pl.version = Some(3);
|
||||
pl.media_sequence = self.segments.first().map(|s| s.0).unwrap_or(0);
|
||||
|
||||
let mut f_out = File::create(self.out_dir().join("live.m3u8"))?;
|
||||
pl.write_to(&mut f_out)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// https://git.ffmpeg.org/gitweb/ffmpeg.git/blob/HEAD:/libavformat/hlsenc.c#l351
|
||||
unsafe fn to_codec_attr(&self, stream: *mut AVStream) -> Option<String> {
|
||||
let p = (*stream).codecpar;
|
||||
if (*p).codec_id == AV_CODEC_ID_H264 {
|
||||
let data = (*p).extradata;
|
||||
if !data.is_null() {
|
||||
let mut id_ptr = ptr::null_mut();
|
||||
let ds: *mut u16 = data as *mut u16;
|
||||
if (*ds) == 1 && (*data.add(4)) & 0x1F == 7 {
|
||||
id_ptr = data.add(5);
|
||||
} else if (*ds) == 1 && (*data.add(3)) & 0x1F == 7 {
|
||||
id_ptr = data.add(4);
|
||||
} else if *data.add(0) == 1 {
|
||||
id_ptr = data.add(1);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
|
||||
return Some(format!(
|
||||
"avc1.{}",
|
||||
hex::encode([*id_ptr.add(0), *id_ptr.add(1), *id_ptr.add(2)])
|
||||
));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn to_playlist_variant(&self) -> m3u8_rs::VariantStream {
|
||||
unsafe {
|
||||
let pes = self.video_stream().unwrap_or(self.streams.first().unwrap());
|
||||
let av_stream = *(*self.mux.context()).streams.add(*pes.index());
|
||||
let codec_par = (*av_stream).codecpar;
|
||||
m3u8_rs::VariantStream {
|
||||
is_i_frame: false,
|
||||
uri: format!("{}/live.m3u8", self.name),
|
||||
bandwidth: 0,
|
||||
average_bandwidth: Some((*codec_par).bit_rate as u64),
|
||||
codecs: self.to_codec_attr(av_stream),
|
||||
resolution: Some(m3u8_rs::Resolution {
|
||||
width: (*codec_par).width as _,
|
||||
height: (*codec_par).height as _,
|
||||
}),
|
||||
frame_rate: Some(av_q2d((*codec_par).framerate)),
|
||||
hdcp_level: None,
|
||||
audio: None,
|
||||
video: None,
|
||||
subtitles: None,
|
||||
closed_captions: None,
|
||||
other_attributes: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct HlsMuxer {
|
||||
pub out_dir: PathBuf,
|
||||
pub variants: Vec<HlsVariant>,
|
||||
}
|
||||
|
||||
impl HlsMuxer {
|
||||
pub fn new<'a>(
|
||||
id: &Uuid,
|
||||
out_dir: &str,
|
||||
segment_length: f32,
|
||||
encoders: impl Iterator<Item = (&'a VariantStream, &'a Encoder)>,
|
||||
segment_type: SegmentType,
|
||||
) -> Result<Self> {
|
||||
let base = PathBuf::from(out_dir).join(id.to_string());
|
||||
|
||||
let mut vars = Vec::new();
|
||||
for (k, group) in &encoders
|
||||
.sorted_by(|a, b| a.0.group_id().cmp(&b.0.group_id()))
|
||||
.chunk_by(|a| a.0.group_id())
|
||||
{
|
||||
let var = HlsVariant::new(
|
||||
base.to_str().unwrap(),
|
||||
segment_length,
|
||||
k,
|
||||
group,
|
||||
segment_type,
|
||||
)?;
|
||||
vars.push(var);
|
||||
}
|
||||
|
||||
let ret = Self {
|
||||
out_dir: base,
|
||||
variants: vars,
|
||||
};
|
||||
ret.write_master_playlist()?;
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
fn write_master_playlist(&self) -> Result<()> {
|
||||
let mut pl = m3u8_rs::MasterPlaylist::default();
|
||||
pl.version = Some(3);
|
||||
pl.variants = self
|
||||
.variants
|
||||
.iter()
|
||||
.map(|v| v.to_playlist_variant())
|
||||
.collect();
|
||||
|
||||
let mut f_out = File::create(self.out_dir.join("live.m3u8"))?;
|
||||
pl.write_to(&mut f_out)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Mux an encoded packet from [Encoder]
|
||||
pub unsafe fn mux_packet(
|
||||
&mut self,
|
||||
pkt: *mut AVPacket,
|
||||
variant: &Uuid,
|
||||
) -> Result<Option<NewSegment>> {
|
||||
for var in self.variants.iter_mut() {
|
||||
if let Some(vs) = var.streams.iter().find(|s| s.id() == variant) {
|
||||
// very important for muxer to know which stream this pkt belongs to
|
||||
(*pkt).stream_index = *vs.index() as _;
|
||||
return var.mux_packet(pkt);
|
||||
}
|
||||
}
|
||||
bail!("Packet doesnt match any variants");
|
||||
}
|
||||
}
|
2
crates/core/src/mux/mod.rs
Normal file
2
crates/core/src/mux/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
mod hls;
|
||||
pub use hls::*;
|
71
crates/core/src/overseer/local.rs
Normal file
71
crates/core/src/overseer/local.rs
Normal file
@ -0,0 +1,71 @@
|
||||
use crate::egress::EgressConfig;
|
||||
use crate::ingress::ConnectionInfo;
|
||||
use crate::overseer::{get_default_variants, IngressInfo, Overseer};
|
||||
use crate::pipeline::{EgressType, PipelineConfig};
|
||||
use crate::variant::StreamMapping;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use std::path::PathBuf;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Simple static file output without any access controls
|
||||
/// Useful for testing or self-hosting
|
||||
pub struct LocalOverseer;
|
||||
|
||||
impl LocalOverseer {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Overseer for LocalOverseer {
|
||||
async fn check_streams(&self) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn start_stream(
|
||||
&self,
|
||||
_connection: &ConnectionInfo,
|
||||
stream_info: &IngressInfo,
|
||||
) -> Result<PipelineConfig> {
|
||||
let vars = get_default_variants(stream_info)?;
|
||||
let var_ids = vars.iter().map(|v| v.id()).collect();
|
||||
Ok(PipelineConfig {
|
||||
id: Uuid::new_v4(),
|
||||
variants: vars,
|
||||
egress: vec![EgressType::HLS(EgressConfig {
|
||||
name: "HLS".to_owned(),
|
||||
variants: var_ids,
|
||||
})],
|
||||
})
|
||||
}
|
||||
|
||||
async fn on_segment(
|
||||
&self,
|
||||
pipeline_id: &Uuid,
|
||||
variant_id: &Uuid,
|
||||
index: u64,
|
||||
duration: f32,
|
||||
path: &PathBuf,
|
||||
) -> Result<()> {
|
||||
// nothing to do here
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn on_thumbnail(
|
||||
&self,
|
||||
pipeline_id: &Uuid,
|
||||
width: usize,
|
||||
height: usize,
|
||||
path: &PathBuf,
|
||||
) -> Result<()> {
|
||||
// nothing to do here
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn on_end(&self, pipeline_id: &Uuid) -> Result<()> {
|
||||
// nothing to do here
|
||||
Ok(())
|
||||
}
|
||||
}
|
84
crates/core/src/overseer/mod.rs
Normal file
84
crates/core/src/overseer/mod.rs
Normal file
@ -0,0 +1,84 @@
|
||||
use crate::ingress::ConnectionInfo;
|
||||
|
||||
use crate::pipeline::PipelineConfig;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use std::cmp::PartialEq;
|
||||
use std::path::PathBuf;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[cfg(feature = "local-overseer")]
|
||||
mod local;
|
||||
|
||||
#[cfg(feature = "webhook-overseer")]
|
||||
mod webhook;
|
||||
|
||||
#[cfg(feature = "zap-stream")]
|
||||
mod zap_stream;
|
||||
|
||||
/// A copy of [ffmpeg_rs_raw::DemuxerInfo] without internal ptr
|
||||
#[derive(PartialEq, Clone)]
|
||||
pub struct IngressInfo {
|
||||
pub bitrate: usize,
|
||||
pub streams: Vec<IngressStream>,
|
||||
}
|
||||
|
||||
/// A copy of [ffmpeg_rs_raw::StreamInfo] without ptr
|
||||
#[derive(PartialEq, Clone)]
|
||||
pub struct IngressStream {
|
||||
pub index: usize,
|
||||
pub stream_type: IngressStreamType,
|
||||
pub codec: isize,
|
||||
pub format: isize,
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub fps: f32,
|
||||
pub sample_rate: usize,
|
||||
pub language: String,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone)]
|
||||
pub enum IngressStreamType {
|
||||
Video,
|
||||
Audio,
|
||||
Subtitle,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
/// The control process that oversees streaming operations
|
||||
pub trait Overseer: Send + Sync {
|
||||
/// Check all streams
|
||||
async fn check_streams(&self) -> Result<()>;
|
||||
|
||||
/// Set up a new streaming pipeline
|
||||
async fn start_stream(
|
||||
&self,
|
||||
connection: &ConnectionInfo,
|
||||
stream_info: &IngressInfo,
|
||||
) -> Result<PipelineConfig>;
|
||||
|
||||
/// A new segment (HLS etc.) was generated for a stream variant
|
||||
///
|
||||
/// This handler is usually used for distribution / billing
|
||||
async fn on_segment(
|
||||
&self,
|
||||
pipeline_id: &Uuid,
|
||||
variant_id: &Uuid,
|
||||
index: u64,
|
||||
duration: f32,
|
||||
path: &PathBuf,
|
||||
) -> Result<()>;
|
||||
|
||||
/// At a regular interval, pipeline will emit one of the frames for processing as a
|
||||
/// thumbnail
|
||||
async fn on_thumbnail(
|
||||
&self,
|
||||
pipeline_id: &Uuid,
|
||||
width: usize,
|
||||
height: usize,
|
||||
path: &PathBuf,
|
||||
) -> Result<()>;
|
||||
|
||||
/// Stream is finished
|
||||
async fn on_end(&self, pipeline_id: &Uuid) -> Result<()>;
|
||||
}
|
60
crates/core/src/overseer/webhook.rs
Normal file
60
crates/core/src/overseer/webhook.rs
Normal file
@ -0,0 +1,60 @@
|
||||
use crate::ingress::ConnectionInfo;
|
||||
use crate::overseer::{IngressInfo, Overseer};
|
||||
use crate::pipeline::PipelineConfig;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use std::path::PathBuf;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WebhookOverseer {
|
||||
url: String,
|
||||
}
|
||||
|
||||
impl WebhookOverseer {
|
||||
pub fn new(url: &str) -> Self {
|
||||
Self {
|
||||
url: url.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Overseer for WebhookOverseer {
|
||||
async fn check_streams(&self) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn start_stream(
|
||||
&self,
|
||||
connection: &ConnectionInfo,
|
||||
stream_info: &IngressInfo,
|
||||
) -> Result<PipelineConfig> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn on_segment(
|
||||
&self,
|
||||
pipeline_id: &Uuid,
|
||||
variant_id: &Uuid,
|
||||
index: u64,
|
||||
duration: f32,
|
||||
path: &PathBuf,
|
||||
) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn on_thumbnail(
|
||||
&self,
|
||||
pipeline_id: &Uuid,
|
||||
width: usize,
|
||||
height: usize,
|
||||
path: &PathBuf,
|
||||
) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn on_end(&self, pipeline_id: &Uuid) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
}
|
66
crates/core/src/pipeline/mod.rs
Normal file
66
crates/core/src/pipeline/mod.rs
Normal file
@ -0,0 +1,66 @@
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
use crate::egress::EgressConfig;
|
||||
use crate::variant::VariantStream;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub mod runner;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub enum EgressType {
|
||||
/// HLS output egress
|
||||
HLS(EgressConfig),
|
||||
|
||||
/// Record streams to local disk
|
||||
Recorder(EgressConfig),
|
||||
|
||||
/// Forward streams to another RTMP server
|
||||
RTMPForwarder(EgressConfig),
|
||||
}
|
||||
|
||||
impl EgressType {
|
||||
pub fn config(&self) -> &EgressConfig {
|
||||
match self {
|
||||
EgressType::HLS(c) => c,
|
||||
EgressType::Recorder(c) => c,
|
||||
EgressType::RTMPForwarder(c) => c,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for EgressType {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
EgressType::HLS(_) => write!(f, "HLS"),
|
||||
EgressType::Recorder(_) => write!(f, "Recorder"),
|
||||
EgressType::RTMPForwarder(_) => write!(f, "RTMPForwarder"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
|
||||
pub struct PipelineConfig {
|
||||
pub id: Uuid,
|
||||
/// Transcoded/Copied stream config
|
||||
pub variants: Vec<VariantStream>,
|
||||
/// Output muxers
|
||||
pub egress: Vec<EgressType>,
|
||||
}
|
||||
|
||||
impl Display for PipelineConfig {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "\nPipeline Config ID={}", self.id)?;
|
||||
write!(f, "\nVariants:")?;
|
||||
for v in &self.variants {
|
||||
write!(f, "\n\t{}", v)?;
|
||||
}
|
||||
if !self.egress.is_empty() {
|
||||
write!(f, "\nEgress:")?;
|
||||
for e in &self.egress {
|
||||
write!(f, "\n\t{}", e)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
406
crates/core/src/pipeline/runner.rs
Normal file
406
crates/core/src/pipeline/runner.rs
Normal file
@ -0,0 +1,406 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::io::Read;
|
||||
use std::mem::transmute;
|
||||
use std::ops::Sub;
|
||||
use std::path::PathBuf;
|
||||
use std::ptr;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
use crate::egress::hls::HlsEgress;
|
||||
use crate::egress::recorder::RecorderEgress;
|
||||
use crate::egress::{Egress, EgressResult};
|
||||
use crate::ingress::ConnectionInfo;
|
||||
use crate::mux::SegmentType;
|
||||
use crate::overseer::{IngressInfo, IngressStream, IngressStreamType, Overseer};
|
||||
use crate::pipeline::{EgressType, PipelineConfig};
|
||||
use crate::variant::{StreamMapping, VariantStream};
|
||||
use anyhow::{bail, Result};
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVCodecID::AV_CODEC_ID_WEBP;
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVPictureType::AV_PICTURE_TYPE_NONE;
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVPixelFormat::AV_PIX_FMT_YUV420P;
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::{
|
||||
av_frame_free, av_get_sample_fmt, av_packet_free, av_q2d, av_rescale_q, AVMediaType,
|
||||
};
|
||||
use ffmpeg_rs_raw::{
|
||||
cstr, get_frame_from_hw, AudioFifo, Decoder, Demuxer, DemuxerInfo, Encoder, Resample, Scaler,
|
||||
StreamType,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use log::{error, info, warn};
|
||||
use tokio::runtime::Handle;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Pipeline runner is the main entry process for stream transcoding
|
||||
///
|
||||
/// Each client connection spawns a new [PipelineRunner] and it should be run in its own thread
|
||||
/// using [crate::ingress::spawn_pipeline]
|
||||
pub struct PipelineRunner {
|
||||
/// Async runtime handle
|
||||
handle: Handle,
|
||||
|
||||
/// Input stream connection info
|
||||
connection: ConnectionInfo,
|
||||
|
||||
/// Configuration for this pipeline (variants, egress config etc.)
|
||||
config: Option<PipelineConfig>,
|
||||
|
||||
/// Singleton demuxer for this input
|
||||
demuxer: Demuxer,
|
||||
|
||||
/// Singleton decoder for all stream
|
||||
decoder: Decoder,
|
||||
|
||||
/// Scaler for a variant (variant_id, Scaler)
|
||||
scalers: HashMap<Uuid, Scaler>,
|
||||
|
||||
/// Resampler for a variant (variant_id, Resample+FIFO)
|
||||
resampler: HashMap<Uuid, (Resample, AudioFifo)>,
|
||||
|
||||
/// Encoder for a variant (variant_id, Encoder)
|
||||
encoders: HashMap<Uuid, Encoder>,
|
||||
|
||||
/// Simple mapping to copy streams
|
||||
copy_stream: HashMap<Uuid, Uuid>,
|
||||
|
||||
/// All configured egress'
|
||||
egress: Vec<Box<dyn Egress>>,
|
||||
|
||||
/// Info about the input stream
|
||||
info: Option<IngressInfo>,
|
||||
|
||||
/// Overseer managing this pipeline
|
||||
overseer: Arc<dyn Overseer>,
|
||||
|
||||
fps_counter_start: Instant,
|
||||
fps_last_frame_ctr: u64,
|
||||
|
||||
/// Total number of frames produced
|
||||
frame_ctr: u64,
|
||||
out_dir: String,
|
||||
}
|
||||
|
||||
impl PipelineRunner {
|
||||
pub fn new(
|
||||
handle: Handle,
|
||||
out_dir: String,
|
||||
overseer: Arc<dyn Overseer>,
|
||||
connection: ConnectionInfo,
|
||||
recv: Box<dyn Read + Send>,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
handle,
|
||||
out_dir,
|
||||
overseer,
|
||||
connection,
|
||||
config: Default::default(),
|
||||
demuxer: Demuxer::new_custom_io(recv, None)?,
|
||||
decoder: Decoder::new(),
|
||||
scalers: Default::default(),
|
||||
resampler: Default::default(),
|
||||
encoders: Default::default(),
|
||||
copy_stream: Default::default(),
|
||||
fps_counter_start: Instant::now(),
|
||||
egress: Vec::new(),
|
||||
frame_ctr: 0,
|
||||
fps_last_frame_ctr: 0,
|
||||
info: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// EOF, cleanup
|
||||
pub unsafe fn flush(&mut self) -> Result<()> {
|
||||
for (var, enc) in &mut self.encoders {
|
||||
for mut pkt in enc.encode_frame(ptr::null_mut())? {
|
||||
for eg in self.egress.iter_mut() {
|
||||
eg.process_pkt(pkt, var)?;
|
||||
}
|
||||
av_packet_free(&mut pkt);
|
||||
}
|
||||
}
|
||||
for eg in self.egress.iter_mut() {
|
||||
eg.reset()?;
|
||||
}
|
||||
|
||||
if let Some(config) = &self.config {
|
||||
self.handle.block_on(async {
|
||||
if let Err(e) = self.overseer.on_end(&config.id).await {
|
||||
error!("Failed to end stream: {e}");
|
||||
}
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Main processor, should be called in a loop
|
||||
/// Returns false when stream data ended (EOF)
|
||||
pub unsafe fn run(&mut self) -> Result<bool> {
|
||||
self.setup()?;
|
||||
|
||||
let config = if let Some(config) = &self.config {
|
||||
config
|
||||
} else {
|
||||
bail!("Pipeline not configured, cannot run")
|
||||
};
|
||||
|
||||
// run transcoder pipeline
|
||||
let (mut pkt, stream) = self.demuxer.get_packet()?;
|
||||
if pkt.is_null() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
// TODO: For copy streams, skip decoder
|
||||
let frames = match self.decoder.decode_pkt(pkt) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
warn!("Error decoding frames, {e}");
|
||||
return Ok(true);
|
||||
}
|
||||
};
|
||||
|
||||
let mut egress_results = vec![];
|
||||
for frame in frames {
|
||||
// Copy frame from GPU if using hwaccel decoding
|
||||
let mut frame = get_frame_from_hw(frame)?;
|
||||
(*frame).time_base = (*stream).time_base;
|
||||
|
||||
let p = (*stream).codecpar;
|
||||
if (*p).codec_type == AVMediaType::AVMEDIA_TYPE_VIDEO {
|
||||
if (self.frame_ctr % 1800) == 0 {
|
||||
let dst_pic = PathBuf::from(&self.out_dir)
|
||||
.join(config.id.to_string())
|
||||
.join("thumb.webp");
|
||||
let mut sw = Scaler::new();
|
||||
let mut frame = sw.process_frame(
|
||||
frame,
|
||||
(*frame).width as _,
|
||||
(*frame).height as _,
|
||||
AV_PIX_FMT_YUV420P,
|
||||
)?;
|
||||
Encoder::new(AV_CODEC_ID_WEBP)?
|
||||
.with_height((*frame).height)
|
||||
.with_width((*frame).width)
|
||||
.with_pix_fmt(transmute((*frame).format))
|
||||
.open(None)?
|
||||
.save_picture(frame, dst_pic.to_str().unwrap())?;
|
||||
info!("Saved thumb to: {}", dst_pic.display());
|
||||
av_frame_free(&mut frame);
|
||||
}
|
||||
|
||||
// TODO: fix this, multiple video streams in
|
||||
self.frame_ctr += 1;
|
||||
}
|
||||
|
||||
// Get the variants which want this pkt
|
||||
let pkt_vars = config
|
||||
.variants
|
||||
.iter()
|
||||
.filter(|v| v.src_index() == (*stream).index as usize);
|
||||
for var in pkt_vars {
|
||||
let enc = if let Some(enc) = self.encoders.get_mut(&var.id()) {
|
||||
enc
|
||||
} else {
|
||||
//warn!("Frame had nowhere to go in {} :/", var.id());
|
||||
continue;
|
||||
};
|
||||
// before encoding frame, rescale timestamps
|
||||
if !frame.is_null() {
|
||||
let enc_ctx = enc.codec_context();
|
||||
(*frame).pict_type = AV_PICTURE_TYPE_NONE;
|
||||
(*frame).pts =
|
||||
av_rescale_q((*frame).pts, (*frame).time_base, (*enc_ctx).time_base);
|
||||
(*frame).pkt_dts =
|
||||
av_rescale_q((*frame).pkt_dts, (*frame).time_base, (*enc_ctx).time_base);
|
||||
(*frame).duration =
|
||||
av_rescale_q((*frame).duration, (*frame).time_base, (*enc_ctx).time_base);
|
||||
(*frame).time_base = (*enc_ctx).time_base;
|
||||
}
|
||||
|
||||
let mut new_frame = false;
|
||||
let mut frame = match var {
|
||||
VariantStream::Video(v) => {
|
||||
if let Some(s) = self.scalers.get_mut(&v.id()) {
|
||||
new_frame = true;
|
||||
s.process_frame(frame, v.width, v.height, transmute(v.pixel_format))?
|
||||
} else {
|
||||
frame
|
||||
}
|
||||
}
|
||||
VariantStream::Audio(a) => {
|
||||
if let Some((r, f)) = self.resampler.get_mut(&a.id()) {
|
||||
let frame_size = (*enc.codec_context()).frame_size;
|
||||
new_frame = true;
|
||||
let mut resampled_frame = r.process_frame(frame)?;
|
||||
if let Some(ret) =
|
||||
f.buffer_frame(resampled_frame, frame_size as usize)?
|
||||
{
|
||||
av_frame_free(&mut resampled_frame);
|
||||
ret
|
||||
} else {
|
||||
av_frame_free(&mut resampled_frame);
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
frame
|
||||
}
|
||||
}
|
||||
_ => frame,
|
||||
};
|
||||
|
||||
let packets = enc.encode_frame(frame)?;
|
||||
// pass new packets to egress
|
||||
for mut pkt in packets {
|
||||
for eg in self.egress.iter_mut() {
|
||||
let er = eg.process_pkt(pkt, &var.id())?;
|
||||
egress_results.push(er);
|
||||
}
|
||||
av_packet_free(&mut pkt);
|
||||
}
|
||||
|
||||
if new_frame {
|
||||
av_frame_free(&mut frame);
|
||||
}
|
||||
}
|
||||
|
||||
av_frame_free(&mut frame);
|
||||
}
|
||||
|
||||
av_packet_free(&mut pkt);
|
||||
|
||||
// egress results
|
||||
self.handle.block_on(async {
|
||||
for er in egress_results {
|
||||
if let EgressResult::NewSegment(seg) = er {
|
||||
if let Err(e) = self
|
||||
.overseer
|
||||
.on_segment(&config.id, &seg.variant, seg.idx, seg.duration, &seg.path)
|
||||
.await
|
||||
{
|
||||
bail!("Failed to process segment {}", e.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
let elapsed = Instant::now().sub(self.fps_counter_start).as_secs_f32();
|
||||
if elapsed >= 2f32 {
|
||||
let n_frames = self.frame_ctr - self.fps_last_frame_ctr;
|
||||
info!("Average fps: {:.2}", n_frames as f32 / elapsed);
|
||||
self.fps_counter_start = Instant::now();
|
||||
self.fps_last_frame_ctr = self.frame_ctr;
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
unsafe fn setup(&mut self) -> Result<()> {
|
||||
if self.info.is_some() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let info = self.demuxer.probe_input()?;
|
||||
|
||||
// convert to internal type
|
||||
let i_info = IngressInfo {
|
||||
bitrate: info.bitrate,
|
||||
streams: info
|
||||
.streams
|
||||
.iter()
|
||||
.map(|s| IngressStream {
|
||||
index: s.index,
|
||||
stream_type: match s.stream_type {
|
||||
StreamType::Video => IngressStreamType::Video,
|
||||
StreamType::Audio => IngressStreamType::Audio,
|
||||
StreamType::Subtitle => IngressStreamType::Subtitle,
|
||||
},
|
||||
codec: s.codec,
|
||||
format: s.format,
|
||||
width: s.width,
|
||||
height: s.height,
|
||||
fps: s.fps,
|
||||
sample_rate: s.sample_rate,
|
||||
language: s.language.clone(),
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let cfg = self
|
||||
.handle
|
||||
.block_on(async { self.overseer.start_stream(&self.connection, &i_info).await })?;
|
||||
self.config = Some(cfg);
|
||||
self.info = Some(i_info);
|
||||
|
||||
self.setup_pipeline(&info)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
unsafe fn setup_pipeline(&mut self, demux_info: &DemuxerInfo) -> Result<()> {
|
||||
let cfg = if let Some(ref cfg) = self.config {
|
||||
cfg
|
||||
} else {
|
||||
bail!("Cannot setup pipeline without config");
|
||||
};
|
||||
|
||||
// src stream indexes
|
||||
let inputs: HashSet<usize> = cfg.variants.iter().map(|e| e.src_index()).collect();
|
||||
|
||||
// enable hardware decoding
|
||||
self.decoder.enable_hw_decoder_any();
|
||||
|
||||
// setup decoders
|
||||
for input_idx in inputs {
|
||||
let stream = demux_info
|
||||
.streams
|
||||
.iter()
|
||||
.find(|f| f.index == input_idx)
|
||||
.unwrap();
|
||||
self.decoder.setup_decoder(stream, None)?;
|
||||
}
|
||||
|
||||
// setup scaler/encoders
|
||||
for out_stream in &cfg.variants {
|
||||
match out_stream {
|
||||
VariantStream::Video(v) => {
|
||||
self.encoders.insert(out_stream.id(), v.try_into()?);
|
||||
self.scalers.insert(out_stream.id(), Scaler::new());
|
||||
}
|
||||
VariantStream::Audio(a) => {
|
||||
let enc = a.try_into()?;
|
||||
let fmt = av_get_sample_fmt(cstr!(a.sample_fmt.as_str()));
|
||||
let rs = Resample::new(fmt, a.sample_rate as _, a.channels as _);
|
||||
let f = AudioFifo::new(fmt, a.channels as _)?;
|
||||
self.resampler.insert(out_stream.id(), (rs, f));
|
||||
self.encoders.insert(out_stream.id(), enc);
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Setup copy streams
|
||||
|
||||
// Setup egress
|
||||
for e in &cfg.egress {
|
||||
let c = e.config();
|
||||
let encoders = self.encoders.iter().filter_map(|(k, v)| {
|
||||
if c.variants.contains(k) {
|
||||
let var = cfg.variants.iter().find(|x| x.id() == *k)?;
|
||||
Some((var, v))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
match e {
|
||||
EgressType::HLS(_) => {
|
||||
let hls =
|
||||
HlsEgress::new(&cfg.id, &self.out_dir, 2.0, encoders, SegmentType::MPEGTS)?;
|
||||
self.egress.push(Box::new(hls));
|
||||
}
|
||||
EgressType::Recorder(_) => {
|
||||
let rec = RecorderEgress::new(&cfg.id, &self.out_dir, encoders)?;
|
||||
self.egress.push(Box::new(rec));
|
||||
}
|
||||
_ => warn!("{} is not implemented", e),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
79
crates/core/src/variant/audio.rs
Normal file
79
crates/core/src/variant/audio.rs
Normal file
@ -0,0 +1,79 @@
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::av_get_sample_fmt;
|
||||
use ffmpeg_rs_raw::{cstr, Encoder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::{Display, Formatter};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::variant::{StreamMapping, VariantMapping};
|
||||
|
||||
/// Information related to variant streams for a given egress
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct AudioVariant {
|
||||
/// Id, Src, Dst
|
||||
pub mapping: VariantMapping,
|
||||
|
||||
/// Bitrate of this stream
|
||||
pub bitrate: u64,
|
||||
|
||||
/// Codec name
|
||||
pub codec: String,
|
||||
|
||||
/// Number of channels
|
||||
pub channels: u16,
|
||||
|
||||
/// Sample rate
|
||||
pub sample_rate: usize,
|
||||
|
||||
/// Sample format as ffmpeg sample format string
|
||||
pub sample_fmt: String,
|
||||
}
|
||||
|
||||
impl Display for AudioVariant {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"Audio #{}->{}: {}, {}kbps",
|
||||
self.mapping.src_index,
|
||||
self.mapping.dst_index,
|
||||
self.codec,
|
||||
self.bitrate / 1000
|
||||
)
|
||||
}
|
||||
}
|
||||
impl StreamMapping for AudioVariant {
|
||||
fn id(&self) -> Uuid {
|
||||
self.mapping.id
|
||||
}
|
||||
fn src_index(&self) -> usize {
|
||||
self.mapping.src_index
|
||||
}
|
||||
|
||||
fn dst_index(&self) -> usize {
|
||||
self.mapping.dst_index
|
||||
}
|
||||
|
||||
fn set_dst_index(&mut self, dst: usize) {
|
||||
self.mapping.dst_index = dst;
|
||||
}
|
||||
|
||||
fn group_id(&self) -> usize {
|
||||
self.mapping.group_id
|
||||
}
|
||||
}
|
||||
|
||||
impl TryInto<Encoder> for &AudioVariant {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_into(self) -> Result<Encoder, Self::Error> {
|
||||
unsafe {
|
||||
let enc = Encoder::new_with_name(&self.codec)?
|
||||
.with_sample_rate(self.sample_rate as _)?
|
||||
.with_bitrate(self.bitrate as _)
|
||||
.with_default_channel_layout(self.channels as _)
|
||||
.with_sample_format(av_get_sample_fmt(cstr!(self.sample_fmt.as_bytes())))
|
||||
.open(None)?;
|
||||
|
||||
Ok(enc)
|
||||
}
|
||||
}
|
||||
}
|
48
crates/core/src/variant/mapping.rs
Normal file
48
crates/core/src/variant/mapping.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::{Display, Formatter};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::variant::StreamMapping;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
|
||||
pub struct VariantMapping {
|
||||
/// Unique ID of this variant
|
||||
pub id: Uuid,
|
||||
|
||||
/// Source video stream to use for this variant
|
||||
pub src_index: usize,
|
||||
|
||||
/// Index of this variant stream in the output
|
||||
pub dst_index: usize,
|
||||
|
||||
/// Stream group, groups one or more streams into a variant
|
||||
pub group_id: usize,
|
||||
}
|
||||
|
||||
impl Display for VariantMapping {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Copy #{}->{}", self.src_index, self.dst_index)
|
||||
}
|
||||
}
|
||||
|
||||
impl StreamMapping for VariantMapping {
|
||||
fn id(&self) -> Uuid {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn src_index(&self) -> usize {
|
||||
self.src_index
|
||||
}
|
||||
|
||||
fn dst_index(&self) -> usize {
|
||||
self.dst_index
|
||||
}
|
||||
|
||||
fn set_dst_index(&mut self, dst: usize) {
|
||||
self.dst_index = dst;
|
||||
}
|
||||
|
||||
fn group_id(&self) -> usize {
|
||||
self.group_id
|
||||
}
|
||||
}
|
113
crates/core/src/variant/mod.rs
Normal file
113
crates/core/src/variant/mod.rs
Normal file
@ -0,0 +1,113 @@
|
||||
use crate::variant::audio::AudioVariant;
|
||||
use crate::variant::mapping::VariantMapping;
|
||||
use crate::variant::video::VideoVariant;
|
||||
use anyhow::Error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::{Display, Formatter};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub mod audio;
|
||||
pub mod mapping;
|
||||
pub mod video;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub enum VariantStream {
|
||||
/// Video stream mapping
|
||||
Video(VideoVariant),
|
||||
/// Audio stream mapping
|
||||
Audio(AudioVariant),
|
||||
Subtitle(VariantMapping),
|
||||
/// Copy stream src<>dst stream
|
||||
CopyVideo(VariantMapping),
|
||||
/// Copy stream src<>dst stream
|
||||
CopyAudio(VariantMapping),
|
||||
}
|
||||
|
||||
impl StreamMapping for VariantStream {
|
||||
fn id(&self) -> Uuid {
|
||||
match self {
|
||||
VariantStream::Video(v) => v.id(),
|
||||
VariantStream::Audio(v) => v.id(),
|
||||
VariantStream::Subtitle(v) => v.id(),
|
||||
VariantStream::CopyAudio(v) => v.id(),
|
||||
VariantStream::CopyVideo(v) => v.id(),
|
||||
}
|
||||
}
|
||||
|
||||
fn src_index(&self) -> usize {
|
||||
match self {
|
||||
VariantStream::Video(v) => v.src_index(),
|
||||
VariantStream::Audio(v) => v.src_index(),
|
||||
VariantStream::Subtitle(v) => v.src_index(),
|
||||
VariantStream::CopyAudio(v) => v.src_index(),
|
||||
VariantStream::CopyVideo(v) => v.src_index(),
|
||||
}
|
||||
}
|
||||
|
||||
fn dst_index(&self) -> usize {
|
||||
match self {
|
||||
VariantStream::Video(v) => v.dst_index(),
|
||||
VariantStream::Audio(v) => v.dst_index(),
|
||||
VariantStream::Subtitle(v) => v.dst_index(),
|
||||
VariantStream::CopyAudio(v) => v.dst_index(),
|
||||
VariantStream::CopyVideo(v) => v.dst_index(),
|
||||
}
|
||||
}
|
||||
|
||||
fn set_dst_index(&mut self, dst: usize) {
|
||||
match self {
|
||||
VariantStream::Video(v) => v.set_dst_index(dst),
|
||||
VariantStream::Audio(v) => v.set_dst_index(dst),
|
||||
VariantStream::Subtitle(v) => v.set_dst_index(dst),
|
||||
VariantStream::CopyAudio(v) => v.set_dst_index(dst),
|
||||
VariantStream::CopyVideo(v) => v.set_dst_index(dst),
|
||||
}
|
||||
}
|
||||
|
||||
fn group_id(&self) -> usize {
|
||||
match self {
|
||||
VariantStream::Video(v) => v.group_id(),
|
||||
VariantStream::Audio(v) => v.group_id(),
|
||||
VariantStream::Subtitle(v) => v.group_id(),
|
||||
VariantStream::CopyAudio(v) => v.group_id(),
|
||||
VariantStream::CopyVideo(v) => v.group_id(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for VariantStream {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
VariantStream::Video(v) => write!(f, "{}", v),
|
||||
VariantStream::Audio(a) => write!(f, "{}", a),
|
||||
VariantStream::Subtitle(s) => write!(f, "{}", s),
|
||||
VariantStream::CopyVideo(c) => write!(f, "{}", c),
|
||||
VariantStream::CopyAudio(c) => write!(f, "{}", c),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait StreamMapping {
|
||||
fn id(&self) -> Uuid;
|
||||
fn src_index(&self) -> usize;
|
||||
fn dst_index(&self) -> usize;
|
||||
fn set_dst_index(&mut self, dst: usize);
|
||||
fn group_id(&self) -> usize;
|
||||
}
|
||||
|
||||
/// Find a stream by ID in a vec of streams
|
||||
pub fn find_stream<'a>(
|
||||
config: &'a Vec<VariantStream>,
|
||||
id: &Uuid,
|
||||
) -> Result<&'a VariantStream, Error> {
|
||||
config
|
||||
.iter()
|
||||
.find(|x| match x {
|
||||
VariantStream::Video(v) => v.id() == *id,
|
||||
VariantStream::Audio(a) => a.id() == *id,
|
||||
VariantStream::Subtitle(v) => v.id() == *id,
|
||||
VariantStream::CopyVideo(c) => c.id() == *id,
|
||||
VariantStream::CopyAudio(c) => c.id() == *id,
|
||||
})
|
||||
.ok_or(Error::msg("Variant does not exist"))
|
||||
}
|
111
crates/core/src/variant/video.rs
Normal file
111
crates/core/src/variant/video.rs
Normal file
@ -0,0 +1,111 @@
|
||||
use ffmpeg_rs_raw::ffmpeg_sys_the_third::AVColorSpace::AVCOL_SPC_BT709;
|
||||
use ffmpeg_rs_raw::Encoder;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::intrinsics::transmute;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::variant::{StreamMapping, VariantMapping};
|
||||
|
||||
/// Information related to variant streams for a given egress
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct VideoVariant {
|
||||
/// Id, Src, Dst
|
||||
pub mapping: VariantMapping,
|
||||
|
||||
/// Width of this video stream
|
||||
pub width: u16,
|
||||
|
||||
/// Height of this video stream
|
||||
pub height: u16,
|
||||
|
||||
/// FPS for this stream
|
||||
pub fps: f32,
|
||||
|
||||
/// Bitrate of this stream
|
||||
pub bitrate: u64,
|
||||
|
||||
/// Codec name
|
||||
pub codec: String,
|
||||
|
||||
/// Codec profile
|
||||
pub profile: usize,
|
||||
|
||||
/// Codec level
|
||||
pub level: usize,
|
||||
|
||||
/// Keyframe interval in frames
|
||||
pub keyframe_interval: u16,
|
||||
|
||||
/// Pixel Format
|
||||
pub pixel_format: u32,
|
||||
}
|
||||
|
||||
impl Display for VideoVariant {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"Video #{}->{}: {}, {}x{}, {}fps, {}kbps",
|
||||
self.mapping.src_index,
|
||||
self.mapping.dst_index,
|
||||
self.codec,
|
||||
self.width,
|
||||
self.height,
|
||||
self.fps,
|
||||
self.bitrate / 1000
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl StreamMapping for VideoVariant {
|
||||
fn id(&self) -> Uuid {
|
||||
self.mapping.id
|
||||
}
|
||||
fn src_index(&self) -> usize {
|
||||
self.mapping.src_index
|
||||
}
|
||||
|
||||
fn dst_index(&self) -> usize {
|
||||
self.mapping.dst_index
|
||||
}
|
||||
|
||||
fn set_dst_index(&mut self, dst: usize) {
|
||||
self.mapping.dst_index = dst;
|
||||
}
|
||||
|
||||
fn group_id(&self) -> usize {
|
||||
self.mapping.group_id
|
||||
}
|
||||
}
|
||||
|
||||
impl TryInto<Encoder> for &VideoVariant {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_into(self) -> Result<Encoder, Self::Error> {
|
||||
unsafe {
|
||||
let mut opt = HashMap::new();
|
||||
if self.codec == "x264" {
|
||||
opt.insert("preset".to_string(), "fast".to_string());
|
||||
//opt.insert("tune".to_string(), "zerolatency".to_string());
|
||||
}
|
||||
let enc = Encoder::new_with_name(&self.codec)?
|
||||
.with_bitrate(self.bitrate as _)
|
||||
.with_width(self.width as _)
|
||||
.with_height(self.height as _)
|
||||
.with_pix_fmt(transmute(self.pixel_format))
|
||||
.with_profile(transmute(self.profile as i32))
|
||||
.with_level(transmute(self.level as i32))
|
||||
.with_framerate(self.fps)?
|
||||
.with_options(|ctx| {
|
||||
(*ctx).gop_size = self.keyframe_interval as _;
|
||||
(*ctx).keyint_min = self.keyframe_interval as _;
|
||||
(*ctx).max_b_frames = 3;
|
||||
(*ctx).colorspace = AVCOL_SPC_BT709;
|
||||
})
|
||||
.open(Some(opt))?;
|
||||
|
||||
Ok(enc)
|
||||
}
|
||||
}
|
||||
}
|
2018
crates/core/test.svg
Normal file
2018
crates/core/test.svg
Normal file
File diff suppressed because it is too large
Load Diff
After Width: | Height: | Size: 39 KiB |
Reference in New Issue
Block a user