Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose moq-pub as library with generic input #134

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading