player

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 14 Imported by: ? 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 player is NSGo Media's public API: a Player opened on a media source, its commands, its state as NSGo signals, and View, the component that draws its picture. The app builds every control itself.

A Player belongs to the scope that made it and to its engine's goroutine: call its methods from components, effects and event handlers. It decodes on its own goroutines and closes when its scope is disposed.

On iOS a player holds the audio session at Playback while it plays, so the Ring/Silent switch does not silence it. It pauses as an interruption, such as a call or Siri, begins, and plays again as it ends if the system allows it; it pauses for good as headphones are unplugged. It plays on in the background, its sound alone, when the app declares the audio background mode, and pauses there otherwise.

Index

Constants

View Source
const (
	PassthroughAC3  = Passthrough(pipeline.PassAC3)
	PassthroughEAC3 = Passthrough(pipeline.PassEAC3)
	// PassthroughDTS passes DTS' core: DTS-HD's too where
	// PassthroughDTSHD is not set or the device takes no high bit rate.
	// Other enhanced DTS, such as DTS-ES, DTS 96/24 or DTS Express, plays
	// as PCM.
	PassthroughDTS = Passthrough(pipeline.PassDTS)
	// PassthroughDTSHD passes DTS-HD High Resolution and Master Audio
	// whole, core and extensions, on HDMI's high bit rate carrier.
	PassthroughDTSHD = Passthrough(pipeline.PassDTSHD)
	// PassthroughTrueHD passes TrueHD as its stream is, Atmos and all,
	// on HDMI's high bit rate carrier.
	PassthroughTrueHD = Passthrough(pipeline.PassTrueHD)
)
View Source
const (
	// FallbackNone is no fallback: the audio passes through, or
	// passthrough was not set for its codec.
	FallbackNone = Fallback(pipeline.FallbackNone)
	// FallbackRate is a rate other than 1, which only PCM plays.
	FallbackRate = Fallback(pipeline.FallbackRate)
	// FallbackEnhancedDTS is DTS with extensions to its core that do not
	// pass: DTS-HD without PassthroughDTSHD, DTS-ES, DTS 96/24 or DTS
	// Express.
	FallbackEnhancedDTS = Fallback(pipeline.FallbackEnhancedDTS)
	// FallbackDevice is the device or the system not taking the stream,
	// refusing it or failing to start it; AudioOutput.Err says why.
	// DTS-HD then passes its core where PassthroughDTS is set.
	FallbackDevice = Fallback(pipeline.FallbackDevice)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AudioOutput

type AudioOutput struct {
	// Passthrough is set while Codec passes through: "ac3", "eac3", "dts"
	// (DTS' core, DTS-HD's too), "dtshd" (DTS-HD whole) or "truehd".
	Passthrough bool
	Codec       string
	// SampleRate and Channels are the device stream's: the PCM's, or
	// passing through, the IEC 61937 carrier's, 2 channels at the codec's
	// rate or four times it for E-AC-3, and for DTS-HD and TrueHD the high
	// bit rate's 8 at 192 kHz (176.4 kHz for 44.1 kHz's family). The
	// track's own are in its Track.
	SampleRate, Channels int
	// Fallback is why the audio plays as PCM or DTS-HD as its core, and
	// Err the device's error for FallbackDevice.
	Fallback Fallback
	Err      error
}

AudioOutput is what the audio device plays.

type Bitmap

type Bitmap struct {
	X, Y  int
	Image *image.RGBA
}

Bitmap is a picture of a cue: Image, premultiplied as image.RGBA is, with its top left corner at X, Y on the cue's canvas. A rotated video's bitmaps and canvas are turned as its picture is.

type Chapter

type Chapter struct {
	Start, End time.Duration
	Title      string
}

Chapter is a named span of the source.

type Cue

type Cue struct {
	// End is 0 while a bitmap cue's end is not read yet: a PGS cue shows
	// until the next cue of its track, which the player reads ahead.
	Start, End time.Duration
	// Generation is the seek the cue was decoded after, rising with each
	// seek and each step backward that decodes again: the same cue read
	// again has new bitmaps. It is 0 for a file AddSubtitles added.
	Generation uint64
	Text       string
	// Canvas is the size, in pixels, of the canvas the bitmaps are placed
	// on, which covers the picture; Place maps it onto a view.
	Canvas  image.Point
	Bitmaps []Bitmap
}

Cue is a subtitle showing from Start to End: the text of a text track, or the bitmaps of a PGS, DVD, DVB or VobSub track. A cue replaces the one before it on a bitmap track; no cue showing clears the screen.

func (Cue) Place

func (c Cue) Place(b Bitmap, picture core.Bounds) core.Bounds

Place is where bitmap b of c shows over a picture drawn at picture, such as PictureBounds gives, in the same units: the canvas is stretched over the picture.

type Fallback

type Fallback int

Fallback is why the audio plays otherwise than passthrough was set for its codec: as PCM, or DTS-HD as its core.

func (Fallback) String

func (f Fallback) String() string

type Fit

type Fit int

Fit is how the picture fills its view.

const (
	// Contain shows the whole picture, leaving bars.
	Contain Fit = iota
	// Cover fills the view, cropping what overflows.
	Cover
	// Fill stretches the picture to the view.
	Fill
)

type Info

type Info struct {
	// Duration is 0 when unknown, as for a live stream's sliding window.
	Duration time.Duration
	// Live is an HLS or DASH stream whose end is not published yet.
	Live bool
	// Seekable is false for a source Seek cannot move in, such as a live
	// stream keeping only a sliding window.
	Seekable bool
	// Format is the container's short names, such as "matroska,webm".
	Format   string
	BitRate  int64
	Metadata map[string]string
	Tracks   []Track
	Chapters []Chapter
}

Info is what opening tells about the source.

func (*Info) Variants

func (i *Info) Variants() []Track

Variants are the video tracks of an HLS or DASH stream's variants, by bandwidth from the lowest, for SelectVariant; none for other sources.

type Kind

type Kind int

Kind is a track's media.

const (
	Video    Kind = Kind(pipeline.Video)
	Audio    Kind = Kind(pipeline.Audio)
	Subtitle Kind = Kind(pipeline.Subtitle)
)

type OpenOption

type OpenOption func(*openConfig)

OpenOption configures how Open reads a source.

func ExternalAudio

func ExternalAudio(source string, h http.Header) OpenOption

ExternalAudio plays the audio of source, a file path or an http(s) URL whose requests send h, with the picture of the source Open opens, as DASH serves them apart. Its audio tracks follow the source's in Info, External, and its first plays. Its sound stays the clock: seeking, rate, pausing and stepping move both sources together, and Buffered is the lesser of the two.

func Headers

func Headers(h http.Header) OpenOption

Headers sends h with every HTTP request for the source, such as the Referer, User-Agent and Cookie a CDN asks for.

type Option

type Option func(*Player)

Option configures a player.

func Software

func Software() Option

Software turns hardware video decoding off.

type Passthrough

type Passthrough int

Passthrough is a set of codecs whose audio passes undecoded, in IEC 61937 bursts, to a receiver that decodes it.

type Player

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

Player plays one source at a time.

func New

func New(cx *core.Scope, opts ...Option) *Player

New makes a player owned by cx, closed when cx is disposed.

func (*Player) AddSubtitles

func (p *Player) AddSubtitles(path string) error

AddSubtitles adds an SRT, WebVTT or ASS file as a subtitle track of the open source, which Info lists and SelectSubtitle selects, or a VobSub .idx, with its .sub beside it, as a track per language. It reads the file at once; before the source opens it waits for it.

func (*Player) AudioOutput

func (p *Player) AudioOutput() AudioOutput

AudioOutput is what the audio output plays now: the playing track passing through or PCM, the device stream's rate and channels, and why it plays PCM, or DTS-HD's core, though passthrough was set for its codec.

func (*Player) AudioTrack

func (p *Player) AudioTrack() int

AudioTrack, VideoTrack and SubtitleTrack are the playing tracks' IDs, -1 for none. For an HLS or DASH stream VideoTrack is the variant's.

func (*Player) AutoVariant

func (p *Player) AutoVariant() bool

AutoVariant reports the estimated bandwidth choosing an HLS or DASH stream's variant, until SelectVariant chooses one.

func (*Player) Buffered

func (p *Player) Buffered() time.Duration

Buffered is how far from the start the source is read: the buffered range runs from Position to it.

func (*Player) Close

func (p *Player) Close()

Close stops playback and every goroutine of the player, returning once they have exited; no signal changes after it. The player may open another source.

func (*Player) Cues

func (p *Player) Cues() []Cue

Cues are the chosen subtitle track's cues showing now, for the app to draw: they hold while paused, change as a bitmap track updates its picture, and are empty when nothing shows. After a seek no cue of the old position stays.

func (*Player) Duration

func (p *Player) Duration() time.Duration

Duration is the source's duration, 0 when unknown or not open.

func (*Player) Err

func (p *Player) Err() error

Err is why the player failed, nil unless State is Failed.

func (*Player) HardwareDevice

func (p *Player) HardwareDevice() string

HardwareDevice is the device video decodes on: "videotoolbox", "d3d11va" or "vaapi", or empty for software decoding.

func (*Player) Info

func (p *Player) Info() *Info

Info is the open source's information, nil until it opened.

func (*Player) Loop

func (p *Player) Loop() bool

func (*Player) Muted

func (p *Player) Muted() bool

func (*Player) Open

func (p *Player) Open(source string, opts ...OpenOption)

Open opens source, a file path or an http(s) URL, closing what was open. The player is Opening until it can show the first frame, then Paused, or Playing after Play.

func (*Player) Passthrough

func (p *Player) Passthrough() Passthrough

Passthrough is the codecs SetPassthrough last set.

func (*Player) Pause

func (p *Player) Pause()

Pause pauses.

func (*Player) PictureBounds

func (p *Player) PictureBounds(box core.Size, fit Fit) core.Bounds

PictureBounds is where a View of size box draws the picture with fit, in its points: what an app draws over the picture, such as a cue's bitmaps placed with Cue.Place, goes in the same box. Cover's bounds overflow the box.

func (*Player) Play

func (p *Player) Play()

Play plays, from the start when it ended.

func (*Player) Position

func (p *Player) Position() time.Duration

Position is the playing position, updated at every display frame.

func (*Player) Rate

func (p *Player) Rate() float64

Rate, Volume, Muted and Loop are what the setters last set.

func (*Player) Seek

func (p *Player) Seek(t time.Duration, mode SeekMode)

Seek moves to t from the start. A source that is not Info.Seekable, such as a live stream keeping only a sliding window, stays where it plays.

func (*Player) SelectAudio

func (p *Player) SelectAudio(id int) error

SelectAudio plays the audio track id, or no sound for -1.

func (*Player) SelectSubtitle

func (p *Player) SelectSubtitle(id int) error

SelectSubtitle delivers the cues of the subtitle track id, or none for -1. A bitmap track of the source decodes only while it is chosen: choosing one decodes again from the keyframe before the position, so the picture holds for a moment, as SelectAudio's does.

func (*Player) SelectVariant

func (p *Player) SelectVariant(id int) error

SelectVariant plays the variant of an HLS or DASH stream whose video track is id, one of Info.Variants, from the position; with -1 the estimated bandwidth chooses it as it plays, as it does when the source opens. Changing the variant decodes again from the keyframe before the position, so the picture holds for a moment.

func (*Player) SetLoop

func (p *Player) SetLoop(l bool)

SetLoop makes playback start over at the end.

func (*Player) SetMuted

func (p *Player) SetMuted(m bool)

SetMuted silences or restores the sound.

func (*Player) SetPassthrough

func (p *Player) SetPassthrough(codecs Passthrough)

SetPassthrough passes the audio of codecs, none for 0, undecoded to the receiver the output device drives, such as an amplifier on HDMI or S/PDIF, which decodes it: through WASAPI in exclusive mode on Windows, a hogged CoreAudio device with an encoded format on macOS, and a PulseAudio sink with passthrough formats on Linux. DTS-HD and TrueHD need HDMI's high bit rate, 8 channels at 192 kHz; where the device takes none, DTS-HD passes its core if PassthroughDTS is set. It is off unless set. A track of another codec, a rate other than 1, enhanced DTS that does not pass, and a device that takes no such stream play as PCM, as AudioOutput tells. Passing through, volume does not change the sound and muting sends silence the receiver understands. Changing it while a track of the codecs plays starts its sound again from the position.

func (*Player) SetRate

func (p *Player) SetRate(r float64)

SetRate sets the speed from 0.25 to 4, keeping the pitch.

func (*Player) SetVolume

func (p *Player) SetVolume(v float64)

SetVolume sets the volume from 0 to 1.

func (*Player) Snapshot

func (p *Player) Snapshot(width, height int) (*image.RGBA, error)

Snapshot is the frame showing: at the source's size, its pixels' shape and rotation applied, however large the views draw it. A width or height above 0 shrinks it to fit, keeping its shape; it never grows. It converts the decoded picture again, which takes milliseconds for a large one.

func (*Player) State

func (p *Player) State() State

State is what the player is doing.

func (*Player) StepBackward

func (p *Player) StepBackward()

StepBackward shows the previous frame while paused, stopping at the first. It keeps the recent frames decoded, so steps back over them are quick; others decode again from the keyframe before them. Playing again goes on from the frame shown, sound included.

func (*Player) StepForward

func (p *Player) StepForward()

StepForward shows the next frame while paused. Steps asked for before the last ones showed add up; stepping stops at the last frame.

func (*Player) Stop

func (p *Player) Stop()

Stop pauses at the start.

func (*Player) SubtitleTrack

func (p *Player) SubtitleTrack() int

func (*Player) VideoSize

func (p *Player) VideoSize() (w, h int)

VideoSize is the size the playing video shows at, in pixels: its pixels' shape and rotation applied. It is zero without video.

func (*Player) VideoTrack

func (p *Player) VideoTrack() int

func (*Player) Volume

func (p *Player) Volume() float64

type SeekMode

type SeekMode int

SeekMode is how precisely a seek lands.

const (
	// Fast lands on the keyframe at or before the time, which shows at
	// once.
	Fast SeekMode = iota
	// Exact lands on the frame showing at the time, decoding from the
	// keyframe before it.
	Exact
)

type State

type State int

State is what a player is doing.

const (
	// Idle is a player with no source.
	Idle State = iota
	// Opening is a player reading its source's information.
	Opening
	// Paused is an opened player not playing, after Pause, Stop or
	// opening.
	Paused
	// Playing is a player playing.
	Playing
	// Buffering is a player that would play but waits for data.
	Buffering
	// Ended is a player that played to the end without looping.
	Ended
	// Failed is a player whose source could not open or play; Err says
	// why.
	Failed
)

func (State) String

func (s State) String() string

type Track

type Track struct {
	// ID names the track to SelectAudio, SelectSubtitle and SelectVariant.
	ID       int
	Kind     Kind
	Codec    string
	Language string // the ISO 639 code the source gives, such as "eng"
	Title    string
	Default  bool
	Forced   bool
	// External is set for a track of ExternalAudio's source and for a
	// subtitle file AddSubtitles added.
	External bool

	// Width and Height are a video's size in pixels as encoded; Rotation
	// is the clockwise quarter turns, in degrees, it is shown at, and
	// AspectRatio the shape of a pixel, width over height.
	Width, Height int
	Rotation      int
	AspectRatio   float64
	FrameRate     float64

	SampleRate, Channels int
	BitRate              int64
	// Bandwidth is the bit rate an HLS or DASH manifest gives the variant
	// the track belongs to; 0 for a track of no single variant.
	Bandwidth int64
}

Track is one stream of the source, or an external subtitle file.

type View

type View struct {
	Player *Player
	Fit    Fit
	// FitFunc, when set, gives the fit instead of Fit, again as what it
	// reads changes.
	FitFunc  func() Fit
	Style    core.Style
	Class    string
	Children []core.Node
	TestID   string
}

View draws a player's picture in its box. Children draw above the picture, as do views placed after it: the picture is an image view, or a GPU view for pictures decoded on the GPU (Windows and Linux) and HDR pictures (macOS), never a native window.

func (View) Render

func (v View) Render(cx *core.Scope) core.Node

Render implements core.Node.

Jump to

Keyboard shortcuts

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