Skip to main content

Browser overlay on a live source

This example renders a web page as a transparent graphics overlay on top of a live video feed, mixes the page's audio into the programme audio, and publishes the result over RTMP.

Typical uses are scoreboards, lower thirds, tickers and clocks: anything that is easier to build and update as a web page than as a video asset.

note

The browser source runs in a separate Packager image. Contact Vindral support for access.

For a downloadable CPU example using a local video and browser page, see Compose video and audio.

How the pipeline fits together​

A browser overlay always involves five pieces:

  1. A base source: the live feed the overlay is drawn on top of.
  2. A browser source: the web page, rendered with a transparent background.
  3. One decoder transcoder per source: these decode to raw frames rather than to a codec, using the wrapped_avframe rendition.
  4. A compositor: alpha-blends the two video legs, and mixes the two audio legs.
  5. Encoding transcoders and a sink: encode the composited video and mixed audio, and publish them.

Two details are worth understanding before you read the configs, because they are the easiest things to get wrong:

Audio is routed through the compositor, not straight from the source. Both audio legs are declared as passthrough = true tracks on the same decoder transcoders that handle the video. Publishing audio and video under one source_id lets the compositor align the audio to the same presentation-timestamp epoch as the composited video, which is what keeps them in sync. Taking the browser audio directly from the source instead would drift.

audio_capture = true on the browser source is what creates the audio track. Without it the browser leg has no audio track at all, and the amix node fails to start with Number of input tracks (1) is not within the allowed range (2-61) for node audio-mix. A page that happens to be silent does not cause this. The source fills its timeline with silence, so the track exists as soon as the flag is set.

GPU pipeline (NVIDIA)​

This is the preferred configuration where an NVIDIA GPU is available. The base feed is decoded with NVDEC and kept in GPU memory (format = "cuda"), the compositor blends with overlay_cuda, and the result stays on the GPU so NVENC can read it directly without a round-trip through system memory.

Set the RTMP input host and stream key, YOUR_BROWSER_URL to your overlay page, and YOUR_INGEST_HOST and YOUR_STREAM_KEY to the RTMP publishing destination. The overlay page should provide the transparency and audio needed by this recipe. It must be an http:// or https:// URL on a public host: pages on localhost, private or LAN addresses are blocked. See Network access from browser pages.

Download browser-overlay.toml.

toml
# Vindral Bridge Packager example
# A browser overlay on a live RTMP feed, composited on the GPU and published over RTMP.

version = "3"

# Base video: a live RTMP feed.
[[sources]]
id = "main"
source_type = "flv"

[sources.protocol_options.url]
type = "string"
value = "rtmp://YOUR_INPUT_HOST/live/YOUR_INPUT_STREAM_KEY"

# The browser source paces itself internally, so it does NOT set realtime_pacing.
[[sources]]
id = "browser-overlay"
source_type = "browser"

[sources.protocol_options.url]
type = "string"
value = "YOUR_BROWSER_URL"

[sources.demuxer_options.width]
type = "u_int"
value = 1920

[sources.demuxer_options.height]
type = "u_int"
value = 1080

[sources.demuxer_options.transparent_background]
type = "bool"
value = true

[sources.demuxer_options.frame_rate]
type = "u_int"
value = 50

[sources.demuxer_options.startup_delay_ms]
type = "u_int"
value = 500

# Capture the page's audio: the source emits video on stream 0 and PCM f32
# audio on stream 1.
[sources.demuxer_options.audio_capture]
type = "bool"
value = true

# Base leg: NVDEC decode, kept on the GPU.
[[transcoders]]
id = "main-decoder"
source_ids = ["main"]
device = "nvidia"

[[transcoders.tracks]]
type = "video"
id = "main-video"

[transcoders.tracks.track_selector]
type = "track-kind"
kind = "video"

[[transcoders.tracks.renditions]]
id = "main-raw"
codec = "wrapped_avframe"
gop_size_ms = 1000
bit_rate = 400_000
format = "cuda"

# Pass the base audio through untouched so it reaches the compositor. It is
# the timing master of the amix node below.
[[transcoders.tracks]]
type = "audio"
id = "main-audio"
passthrough = true

[transcoders.tracks.track_selector]
type = "track-kind"
kind = "audio"

# Browser leg: the page's BGRA frames are uploaded to CUDA, and its PCM audio is
# passed through on the same task so both travel under one source_id.
[[transcoders]]
id = "overlay-decoder"
source_ids = ["browser-overlay"]
device = "nvidia"

[[transcoders.tracks]]
type = "video"
id = "overlay-video"

[transcoders.tracks.track_selector]
type = "track-kind"
kind = "video"

[[transcoders.tracks.renditions]]
id = "overlay-raw"
codec = "wrapped_avframe"
gop_size_ms = 1000
bit_rate = 400_000
format = "cuda"

[[transcoders.tracks]]
type = "audio"
id = "overlay-audio"
passthrough = true

[transcoders.tracks.track_selector]
type = "track-kind"
kind = "audio"

[[compositors]]
id = "compositor"

[[compositors.video.sources]]
id = "base"
source_id = "main-decoder"

[compositors.video.sources.track_selector]
type = "track-kind"
kind = "video"

[[compositors.video.sources]]
id = "overlay"
source_id = "overlay-decoder"

[compositors.video.sources.track_selector]
type = "track-kind"
kind = "video"

# GPU alpha blend. output_format = "cuda" keeps the result in GPU memory so
# NVENC reads it directly.
[[compositors.video.nodes]]
type = "overlay_cuda"
id = "browser-overlay"
source_ids = ["base", "overlay"]
x = "0"
y = "0"
output_format = "cuda"

[[compositors.audio.sources]]
id = "base-audio"
source_id = "main-decoder"

[compositors.audio.sources.track_selector]
type = "track-kind"
kind = "audio"

[[compositors.audio.sources]]
id = "browser-audio"
source_id = "overlay-decoder"

[compositors.audio.sources.track_selector]
type = "track-kind"
kind = "audio"

# Mix the base audio with the page audio. The first source is the timing
# master: it passes through untouched and defines the output rate and
# timeline. weights keeps the base at full level with the page quieter on
# top; normalize = false leaves the base level untouched.
[[compositors.audio.nodes]]
type = "amix"
id = "audio-mix"
source_ids = ["base-audio", "browser-audio"]
weights = [1.0, 0.3]
normalize = false

# Encode the composited video.
[[transcoders]]
id = "transcoder"
source_ids = ["compositor"]
device = "nvidia"

[[transcoders.tracks]]
type = "video"
id = "output-video"

[transcoders.tracks.track_selector]
type = "track-kind"
kind = "video"

[[transcoders.tracks.renditions]]
id = "h264-output"
codec = "h264"
gop_size_ms = 2000
bit_rate = 6_000_000

[transcoders.tracks.renditions.size]
scale_factor = 1.0

# Encode the mixed audio. Scoped to the compositor; kind = "audio" selects the
# amix output.
[[transcoders]]
id = "audio-transcoder"
source_ids = ["compositor"]
device = "cpu"

[[transcoders.tracks]]
type = "audio"
id = "mixed-audio"

[transcoders.tracks.track_selector]
type = "track-kind"
kind = "audio"

[[transcoders.tracks.renditions]]
id = "aac-output"
codec = "aac"
bit_rate = 128_000

# Mux the composited video and the mixed audio into one RTMP publish.
[[sinks]]
id = "rtmp-sink"
sink_type = "flv"
source_ids = ["transcoder", "audio-transcoder"]
wait_for_all_sources = true

[sinks.protocol_options.url]
type = "string"
value = "rtmps://YOUR_INGEST_HOST/publish/YOUR_STREAM_KEY"

CPU pipeline​

The same result without a GPU. The structure is identical; only the device, the filtering and the pixel formats change.

GPUCPU
device on the decoders and video encodernvidiacpu
hw_filtering_enabledtruefalse (or omitted)
Base decoder rendition formatcudayuv420p
Browser decoder rendition formatcudayuva420p (keeps the alpha channel)
Compositor node typeoverlay_cudaoverlay
Compositor node output_formatcudayuv420p
Compositor node formatnot supportedauto (enables alpha blending)

For Intel GPUs, the overlay_qsv node keeps the whole path on QSV; see Browser overlay on QSV.

The sources, the audio legs, the amix node and the sink are unchanged. Applying those substitutions to the configuration above gives the CPU variant; the compositor and decoder sections become:

toml
[[transcoders]]
id = "main-decoder"
source_ids = ["main"]
device = "cpu"

[[transcoders.tracks]]
type = "video"
id = "main-video"

[transcoders.tracks.track_selector]
type = "track-kind"
kind = "video"

[[transcoders.tracks.renditions]]
id = "main-raw"
codec = "wrapped_avframe"
gop_size_ms = 1000
bit_rate = 400_000
format = "yuv420p"

[[transcoders.tracks]]
type = "audio"
id = "main-audio"
passthrough = true

[transcoders.tracks.track_selector]
type = "track-kind"
kind = "audio"

[[transcoders]]
id = "overlay-decoder"
source_ids = ["browser-overlay"]
device = "cpu"

[[transcoders.tracks]]
type = "video"
id = "overlay-video"

[transcoders.tracks.processing]
hw_filtering_enabled = false

[transcoders.tracks.track_selector]
type = "track-kind"
kind = "video"

[[transcoders.tracks.renditions]]
id = "overlay-raw"
codec = "wrapped_avframe"
gop_size_ms = 1000
bit_rate = 400_000
# yuva420p preserves the page's alpha channel for blending.
format = "yuva420p"

[[transcoders.tracks]]
type = "audio"
id = "overlay-audio"
passthrough = true

[transcoders.tracks.track_selector]
type = "track-kind"
kind = "audio"

# ... sources, compositor audio sources and amix node unchanged ...

[[compositors.video.nodes]]
type = "overlay"
id = "browser-overlay"
source_ids = ["base", "overlay"]
x = "0"
y = "0"
format = "auto"
output_format = "yuv420p"

Sizing the overlay for performance​

The example above renders the page at the full 1920x1080 because its graphic moves around the whole frame. That is the most expensive way to run a browser source, and usually not what you want.

width, height and frame_rate dominate the cost: every frame is rendered by a full browser engine and then carried through the pipeline as raw BGRA, so both the render work and the bandwidth scale with resolution and rate. Configure only what the graphic actually needs.

For a scoreboard, a lower third or a clock, or anything else occupying one region of the frame, render the page at the size of the graphic itself and position it with the overlay node, instead of rendering a mostly-empty full-frame transparent page:

toml
# A 480x160 lower third, rendered at its own size.
[sources.demuxer_options.width]
type = "u_int"
value = 480

[sources.demuxer_options.height]
type = "u_int"
value = 160
toml
[[compositors.video.nodes]]
type = "overlay_cuda"
id = "browser-overlay"
source_ids = ["base", "overlay"]
x = "64"
y = "H-h-64"
output_format = "cuda"

Frame rate is worth the same scrutiny. The overlay does not have to match the base feed: a clock ticking once a second or a score that changes every few minutes looks no different at 25 fps than at 50, and costs half as much to render. Set frame_rate to what the graphic needs, not to what the programme feed runs at.

Positioning the overlay​

x and y are FFmpeg expressions, not just numbers, so the overlay can be placed relative to the frame size. W and H are the base dimensions, w and h the overlay's:

toml
x = "W-w-32"        # 32 px from the right edge
y = "H-h-32" # 32 px from the bottom edge

Both values can be changed while the stream is running when using the CPU overlay, via the compositor command API. overlay_cuda accepts the same expressions in the configuration, but changing them requires a restart.

Adjusting the mix​

weights is one gain per entry in source_ids, in the same order, so [1.0, 0.3] means full-level base audio with the page mixed in at roughly a third. Weights must be finite and non-negative, and cannot all be zero; a single zero is fine and mutes that source.

With normalize = false the weights are applied as written. Setting normalize = true makes amix divide by the weight sum, which keeps the output from clipping but also pulls the base audio down below its original level.

To change the mix while the stream runs, send new weights to the audio-mix node, for example to mute the page:

bash
curl --fail-with-body -X POST \
http://127.0.0.1:4040/api/compositors/compositor/audio-mix/command \
-H 'Content-Type: application/json' -d '{"weights":[1.0,0.0]}'

See also​