Skip to content

Commit

Permalink
feat: expose moq-pub as library with generic input (#134)
Browse files Browse the repository at this point in the history
Co-authored-by: kixelated <kixelated@gmail.com>
  • Loading branch information
Frando and kixelated authored Mar 7, 2024
1 parent bd91c8a commit ad5cc5d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
1 change: 1 addition & 0 deletions moq-pub/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod media;
7 changes: 3 additions & 4 deletions moq-pub/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use clap::Parser;
mod cli;
use cli::*;

mod media;
use media::*;

use moq_pub::media::Media;
use moq_transport::cache::broadcast;

// TODO: clap complete
Expand All @@ -25,8 +23,9 @@ async fn main() -> anyhow::Result<()> {

let config = Config::parse();

let input = tokio::io::stdin();
let (publisher, subscriber) = broadcast::new("");
let mut media = Media::new(&config, publisher).await?;
let mut media = Media::new(input, publisher).await?;

// Create a list of acceptable root certificates.
let mut roots = rustls::RootCertStore::empty();
Expand Down
19 changes: 9 additions & 10 deletions moq-pub/src/media.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::cli::Config;
use anyhow::{self, Context};
use moq_transport::cache::{broadcast, fragment, segment, track};
use moq_transport::VarInt;
Expand All @@ -8,25 +7,25 @@ use std::cmp::max;
use std::collections::HashMap;
use std::io::Cursor;
use std::time;
use tokio::io::AsyncReadExt;
use tokio::io::{AsyncRead, AsyncReadExt};

pub struct Media {
pub struct Media<I> {
// We hold on to publisher so we don't close then while media is still being published.
_broadcast: broadcast::Publisher,
_catalog: track::Publisher,
_init: track::Publisher,

// Tracks based on their track ID.
tracks: HashMap<u32, Track>,
input: I,
}

impl Media {
pub async fn new(_config: &Config, mut broadcast: broadcast::Publisher) -> anyhow::Result<Self> {
let mut stdin = tokio::io::stdin();
let ftyp = read_atom(&mut stdin).await?;
impl<I: AsyncRead + Send + Unpin + 'static> Media<I> {
pub async fn new(mut input: I, mut broadcast: broadcast::Publisher) -> anyhow::Result<Self> {
let ftyp = read_atom(&mut input).await?;
anyhow::ensure!(&ftyp[4..8] == b"ftyp", "expected ftyp atom");

let moov = read_atom(&mut stdin).await?;
let moov = read_atom(&mut input).await?;
anyhow::ensure!(&moov[4..8] == b"moov", "expected moov atom");

let mut init = ftyp;
Expand Down Expand Up @@ -77,16 +76,16 @@ impl Media {
_catalog: catalog,
_init: init_track,
tracks,
input,
})
}

pub async fn run(&mut self) -> anyhow::Result<()> {
let mut stdin = tokio::io::stdin();
// The current track name
let mut current = None;

loop {
let atom = read_atom(&mut stdin).await?;
let atom = read_atom(&mut self.input).await?;

let mut reader = Cursor::new(&atom);
let header = mp4::BoxHeader::read(&mut reader)?;
Expand Down

0 comments on commit ad5cc5d

Please sign in to comment.