mediarec

package
v0.1.0-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mediarec is the engine behind MediaRecorder: it takes live audio chunks and video frames, encodes them (Opus, VP8), packs them into a streamable container (WebM or Ogg) and hands out byte chunks that, concatenated, form a valid file. It knows nothing about JavaScript; internal/engine/mediarecorder.go connects it to pages and docs/mediarecorder.md describes the behaviour a page sees.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultMime

func DefaultMime(hasAudio, hasVideo bool) string

DefaultMime picks the type for a stream with the given kinds of tracks, the way Chromium does.

Types

type AudioIn

type AudioIn struct {
	C        <-chan *capture.AudioChunk
	Cancel   func()
	Rate     int
	Channels int
	OwnClock bool
}

AudioIn is one audio source. Rate and Channels are what the source declares; chunks that differ are converted. OwnClock says the samples are produced on a clock of their own (a Web Audio graph) and must not be padded or trimmed against the wall clock.

type Chunk

type Chunk struct {
	Data   []byte
	TimeMs float64
}

Chunk is a piece of the recording; TimeMs is the recording time of its first block.

type Config

type Config struct {
	Container          string // "webm" or "ogg"
	AudioBPS, VideoBPS int
	CBR                bool
	Timeslice          time.Duration
}

Config selects the container and the encoder settings.

type OggOpus

type OggOpus struct {
	// contains filtered or unexported fields
}

OggOpus writes an Ogg Opus stream (RFC 7845): an OpusHead page, an OpusTags page, then audio pages whose granule position counts 48 kHz samples including the pre-skip. Bytes accumulate in an internal buffer that Take hands out; whatever was taken, concatenated, is the stream so far.

func NewOggOpus

func NewOggOpus(channels, inputRate, preSkip int) *OggOpus

NewOggOpus starts a stream and writes the two header pages. inputRate is informational (OpusHead).

func (*OggOpus) AddPacket

func (o *OggOpus) AddPacket(p []byte, samples int)

AddPacket appends one Opus packet that covers samples 48 kHz samples. A page is closed when it would exceed 255 segments or hold about a second of audio.

func (*OggOpus) Finish

func (o *OggOpus) Finish(realSamples int64)

Finish closes the stream with an end-of-stream page. realSamples is the number of 48 kHz samples of real audio in the packets; a larger granule position than preSkip+realSamples would count encoder padding as music.

func (*OggOpus) Flush

func (o *OggOpus) Flush()

Flush closes the page being built (if any packets wait) so Take returns everything written so far.

func (*OggOpus) Take

func (o *OggOpus) Take() []byte

Take returns the bytes written since the last call.

type Recorder

type Recorder struct {
	// contains filtered or unexported fields
}

Recorder encodes and muxes. Create it with New, start it with Start, and finish with Stop; Done is closed after the last chunk went to the emit function. emit is called from one goroutine at a time, in order.

func New

func New(cfg Config, audio []*AudioIn, video *VideoIn, emit func(Chunk)) (*Recorder, error)

New builds the encoders and the container. audio and video may be empty; at least one must be given.

func (*Recorder) AudioEnded

func (r *Recorder) AudioEnded(i int)

AudioEnded says audio source i delivers nothing more (its track ended).

func (*Recorder) Done

func (r *Recorder) Done() <-chan struct{}

Done is closed after the final chunk was emitted.

func (*Recorder) Elapsed

func (r *Recorder) Elapsed() time.Duration

Elapsed is the recording time so far.

func (*Recorder) Pause

func (r *Recorder) Pause()

Pause stops gathering; the recording clock stops with it.

func (*Recorder) RequestData

func (r *Recorder) RequestData()

RequestData emits what has been recorded since the last chunk.

func (*Recorder) Resume

func (r *Recorder) Resume()

Resume continues after Pause.

func (*Recorder) Start

func (r *Recorder) Start()

Start begins recording.

func (*Recorder) Stop

func (r *Recorder) Stop()

Stop ends the recording: sources are released, the last chunk is emitted, then Done closes.

func (*Recorder) VideoEnded

func (r *Recorder) VideoEnded()

VideoEnded says the video source delivers nothing more.

type Resampler

type Resampler struct {
	// contains filtered or unexported fields
}

Resampler converts interleaved float32 PCM from one sample rate to another, in a stream: windowed-sinc interpolation (a Hann-windowed lowpass at the lower of the two Nyquist rates, 8 zero crossings wide), with the weights normalised per output sample so a constant input stays constant. It keeps only the few samples of history it needs.

func NewResampler

func NewResampler(inRate, outRate, ch int) *Resampler

NewResampler returns a resampler for ch (1 or 2) interleaved channels.

func (*Resampler) Process

func (r *Resampler) Process(in []float32) []float32

Process appends in (interleaved) and returns the output samples that are now complete.

type Spec

type Spec struct {
	Container string // "webm" or "ogg"
	Type      string // "audio/webm", "video/webm", "audio/ogg"
	Audio     bool   // the type names (or defaults to) an Opus track
	Video     bool   // the type names (or defaults to) a VP8 track
}

Spec is a parsed recording MIME type.

func ParseMime

func ParseMime(s string) (sp Spec, ok bool)

ParseMime answers MediaRecorder.isTypeSupported(): ok is true only for what this build can really record (WebM with VP8 and/or Opus, Ogg with Opus). An empty string is accepted and means "choose for me".

type VideoIn

type VideoIn struct {
	C      <-chan *capture.Frame
	Cancel func()
	W, H   int
	FPS    float64
}

VideoIn is the video source; frames of another size are scaled to W x H.

type WebM

type WebM struct {
	// contains filtered or unexported fields
}

WebM writes a Matroska/WebM stream for live use: an unknown-size Segment, an Info and a Tracks element, then Clusters of SimpleBlocks. A cluster is written (with its real size) when it is closed, so every byte handed out by Take belongs to a complete element and the concatenation of the chunks is a parseable file. When nothing has been handed out before Finish, the duration is patched into the Info element and the file is complete; later chunks cannot be patched, like in browsers, and such a stream has no Duration (players find it by scanning).

func NewWebM

func NewWebM(tracks []WebMTrack) *WebM

NewWebM writes the headers for the tracks (video, when present, is track 1 and audio the next number).

func (*WebM) AddBlock

func (w *WebM) AddBlock(track int, tsMs int64, key bool, data []byte)

AddBlock adds one frame or packet. tsMs is its presentation time; track is the 1-based track number.

func (*WebM) Finish

func (w *WebM) Finish(endMs int64)

Finish closes the stream. endMs is the end time of the recording (it becomes the Duration when that still can).

func (*WebM) Flush

func (w *WebM) Flush()

Flush closes the open cluster so that Take returns every block added so far.

func (*WebM) Take

func (w *WebM) Take() []byte

Take returns the bytes written since the last call.

type WebMTrack

type WebMTrack struct {
	Video         bool
	Width, Height int // video
	Channels      int // audio
	Rate          int // audio: sampling frequency (48000 for Opus)
	OpusHead      []byte
	PreSkip       int // samples at 48 kHz
}

WebMTrack describes one track of a recording.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL