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
- type AudioOutput
- type Bitmap
- type Chapter
- type Cue
- type Fallback
- type Fit
- type Info
- type Kind
- type OpenOption
- type Option
- type Passthrough
- type Player
- func (p *Player) AddSubtitles(path string) error
- func (p *Player) AudioOutput() AudioOutput
- func (p *Player) AudioTrack() int
- func (p *Player) AutoVariant() bool
- func (p *Player) Buffered() time.Duration
- func (p *Player) Close()
- func (p *Player) Cues() []Cue
- func (p *Player) Duration() time.Duration
- func (p *Player) Err() error
- func (p *Player) HardwareDevice() string
- func (p *Player) Info() *Info
- func (p *Player) Loop() bool
- func (p *Player) Muted() bool
- func (p *Player) Open(source string, opts ...OpenOption)
- func (p *Player) Passthrough() Passthrough
- func (p *Player) Pause()
- func (p *Player) PictureBounds(box core.Size, fit Fit) core.Bounds
- func (p *Player) Play()
- func (p *Player) Position() time.Duration
- func (p *Player) Rate() float64
- func (p *Player) Seek(t time.Duration, mode SeekMode)
- func (p *Player) SelectAudio(id int) error
- func (p *Player) SelectSubtitle(id int) error
- func (p *Player) SelectVariant(id int) error
- func (p *Player) SetLoop(l bool)
- func (p *Player) SetMuted(m bool)
- func (p *Player) SetPassthrough(codecs Passthrough)
- func (p *Player) SetRate(r float64)
- func (p *Player) SetVolume(v float64)
- func (p *Player) Snapshot(width, height int) (*image.RGBA, error)
- func (p *Player) State() State
- func (p *Player) StepBackward()
- func (p *Player) StepForward()
- func (p *Player) Stop()
- func (p *Player) SubtitleTrack() int
- func (p *Player) VideoSize() (w, h int)
- func (p *Player) VideoTrack() int
- func (p *Player) Volume() float64
- type SeekMode
- type State
- type Track
- type View
Constants ¶
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) )
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 ¶
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 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.
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.
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.
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 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 (*Player) AddSubtitles ¶
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 ¶
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 ¶
AutoVariant reports the estimated bandwidth choosing an HLS or DASH stream's variant, until SelectVariant chooses one.
func (*Player) Buffered ¶
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 ¶
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) HardwareDevice ¶
HardwareDevice is the device video decodes on: "videotoolbox", "d3d11va" or "vaapi", or empty for software decoding.
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) PictureBounds ¶
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) Seek ¶
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 ¶
SelectAudio plays the audio track id, or no sound for -1.
func (*Player) SelectSubtitle ¶
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 ¶
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) 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) Snapshot ¶
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) 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) SubtitleTrack ¶
func (*Player) VideoSize ¶
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 ¶
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 )
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.