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

Support for catalog in common catalog format #103

Merged
merged 14 commits into from
Jul 24, 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
80 changes: 47 additions & 33 deletions lib/contribute/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Connection, SubscribeRecv } from "../transport"
import { asError } from "../common/error"
import { Segment } from "./segment"
import { Track } from "./track"
import { Catalog, Mp4Track, VideoTrack, Track as CatalogTrack, AudioTrack } from "../media/catalog"
import * as Catalog from "../media/catalog"

import { isAudioTrackSettings, isVideoTrackSettings } from "../common/settings"

Expand All @@ -24,75 +24,89 @@ export class Broadcast {
#tracks = new Map<string, Track>()

readonly config: BroadcastConfig
readonly catalog: Catalog
readonly catalog: Catalog.Root
readonly connection: Connection
readonly namespace: string

#running: Promise<void>

constructor(config: BroadcastConfig) {
this.connection = config.connection
this.config = config
this.catalog = new Catalog(config.namespace)
this.namespace = config.namespace

const tracks: Catalog.Track[] = []

for (const media of this.config.media.getTracks()) {
const track = new Track(media, config)
this.#tracks.set(track.name, track)

const settings = media.getSettings()

let catalog: CatalogTrack

const mp4Catalog: Mp4Track = {
container: "mp4",
kind: media.kind,
init_track: `${track.name}.mp4`,
data_track: `${track.name}.m4s`,
}

if (isVideoTrackSettings(settings)) {
if (!config.video) {
throw new Error("no video configuration provided")
}

const videoCatalog: VideoTrack = {
...mp4Catalog,
kind: "video",
codec: config.video.codec,
width: settings.width,
height: settings.height,
frame_rate: settings.frameRate,
bit_rate: config.video.bitrate,
const video: Catalog.VideoTrack = {
namespace: this.namespace,
name: track.name,
initTrack: `${track.name}.mp4`,
initData: `${track.name}.m4s`,
selectionParams: {
mimeType: "video/mp4",
codec: config.video.codec,
width: settings.width,
height: settings.height,
framerate: settings.frameRate,
bitrate: config.video.bitrate,
},
}

catalog = videoCatalog
tracks.push(video)
} else if (isAudioTrackSettings(settings)) {
if (!config.audio) {
throw new Error("no audio configuration provided")
}

const audioCatalog: AudioTrack = {
...mp4Catalog,
kind: "audio",
codec: config.audio.codec,
sample_rate: settings.sampleRate,
sample_size: settings.sampleSize,
channel_count: settings.channelCount,
bit_rate: config.audio.bitrate,
const audio: Catalog.AudioTrack = {
namespace: this.namespace,
name: track.name,
initTrack: `${track.name}.mp4`,
initData: `${track.name}.m4s`,
selectionParams: {
mimeType: "audio/ogg",
codec: config.audio.codec,
samplerate: settings.sampleRate,
//sampleSize: settings.sampleSize,
channelConfig: `${settings.channelCount}`,
bitrate: config.audio.bitrate,
},
}

catalog = audioCatalog
tracks.push(audio)
} else {
throw new Error(`unknown track type: ${media.kind}`)
}
}

this.catalog.tracks.push(catalog)
this.catalog = {
version: 1,
streamingFormat: 1,
streamingFormatVersion: "0.2",
supportsDeltaUpdates: false,
commonTrackFields: {
packaging: "cmaf",
renderGroup: 1,
},
tracks,
}

this.#running = this.#run()
}

async #run() {
await this.connection.announce(this.catalog.namespace)
await this.connection.announce(this.namespace)

for (;;) {
const subscriber = await this.connection.subscribed()
Expand Down Expand Up @@ -131,7 +145,7 @@ export class Broadcast {
// We only support ".catalog"
if (name !== "") throw new Error(`unknown catalog: ${name}`)

const bytes = this.catalog.encode()
const bytes = Catalog.encode(this.catalog)

// Send a SUBSCRIBE_OK
await subscriber.ack()
Expand Down
Loading
Loading