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 ¶
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 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 ¶
NewOggOpus starts a stream and writes the two header pages. inputRate is informational (OpusHead).
func (*OggOpus) AddPacket ¶
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 ¶
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.
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 ¶
New builds the encoders and the container. audio and video may be empty; at least one must be given.
func (*Recorder) AudioEnded ¶
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) 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) 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 ¶
NewResampler returns a resampler for ch (1 or 2) interleaved channels.
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.
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 ¶
NewWebM writes the headers for the tracks (video, when present, is track 1 and audio the next number).
func (*WebM) AddBlock ¶
AddBlock adds one frame or packet. tsMs is its presentation time; track is the 1-based track number.
func (*WebM) Finish ¶
Finish closes the stream. endMs is the end time of the recording (it becomes the Duration when that still can).