Skip to main content

Broadcast Multiple Sources

How one broadcast carries several tracks through a composition stream controller.

A broadcast sends one stream, but that stream does not have to come from one device. call.broadcast() takes a composition: a controller that holds a set of sources and publishes their combined output as a single MediaStream. Adding a second camera or swapping the microphone is a change to that set, not a new broadcast.

The parameter type​

CallAPI.broadcast is declared as:

broadcast(ctrl: CompositionStreamControllerAPI, options: BroadcastOptions): Promise<BroadcastAPI>;

CompositionStreamControllerAPI is the compositional contract. It exposes the combined output as source, a MediaStream that is never null, and three verbs for changing what feeds it.

This is also why the guides pass a MediaStreamController here without anything looking unusual. MediaStreamControllerAPI extends CompositionStreamControllerAPI, so the controller you already build for the camera and microphone is a composition with two sources in it. Naming the composition contract does not add a step to the common case; it explains the one you are already using.

Changing what is in the composition​

Three methods mutate the set. Each takes controller instances, not device ids, and each returns a promise you should await before assuming the change has landed.

MethodWhat it does
addSource(ctrl)Adds a source. The kind is taken from the controller's own sourceKind. Adding the same instance twice does nothing.
removeSource(ctrl)Removes a source by instance. The controller is not disposed; its lifecycle stays yours.
replaceSource(oldCtrl, newCtrl)Swaps one source for another of the same kind. Replacing an instance with itself does nothing, and a cross-kind swap is rejected.

Three read-only methods let you inspect the set without tracking it yourself: videoSources(), audioSources(), and has(ctrl).

Because removeSource does not dispose the controller it removes, a source you take out of a composition can be added back later, or added to a different composition. Disposing it is a separate decision, and one you have to make yourself when you are finished with the device.

Building a composition directly​

createCompositionStreamController(ctx, sources, options) builds one from a media context and an optional starting set. The sources are the same controllers the functional device API produces, so a camera, a microphone, a screen capture, or a canvas capture all go in the same way.

import {
createMediaContext,
createCameraController,
createMicrophoneController,
createScreenVideoController,
createCompositionStreamController,
} from "@video/video-client-core";

const ctx = createMediaContext({ logger });

const camera = createCameraController(ctx);
const microphone = createMicrophoneController(ctx);

// Start with the camera and microphone in the composition.
const composition = createCompositionStreamController(ctx, [camera, microphone]);

await camera.start();
await microphone.start();

// One broadcast carries the combined output.
const broadcast = await call.broadcast(composition, { streamName: "default" });

// Add a screen capture to the same broadcast, without restarting it.
const screen = createScreenVideoController(ctx);
await composition.addSource(screen);
await screen.start();

// Take it out again. The controller survives; stop and dispose it yourself.
await composition.removeSource(screen);
await screen.stop();

What the composition tells you​

A composition emits three events, and they are the only ones it emits. Device-level events stay on the individual source controllers.

EventWhen it fires
sourceThe combined stream a consumer should read has changed.
tracksChangedThe set of tracks in that stream has changed.
disposedThe composition was disposed.

A broadcast reads tracksChanged to keep what it publishes in step with the set, and a player reads source. Between them, adding or removing a source propagates without your code republishing anything.

Multiple sources against multiple broadcasts​

A composition is one stream, so everything in it arrives at viewers as one subscribable unit. When you want viewers to be able to take the camera and leave the screenshare, that is two broadcasts with two stream names rather than one composition with two video sources. The screenshare guide takes that route for exactly that reason.

Reach for a composition when the sources belong together in one picture, and for a second broadcast when a viewer should be able to choose between them.

See also​