Documentation ¶
Overview ¶
Package gst provides wrappers for building gstreamer pipelines and then reading and/or writing from either end of the pipeline.
It uses cgo to interface with the gstreamer-1.0 C API.
A simple opus/webm encoder created from a launch string could look like this:
import ( "os" "github.com/tinyzimmer/go-gst-launch/gst" ) func main() { gst.Init() encoder, err := gst.NewPipelineFromLaunchString("opusenc ! webmmux", gst.PipelineReadWrite) if err != nil { panic(err) } // You should close even if you don't start the pipeline, since this // will free resources created by gstreamer. defer encoder.Close() if err := encoder.Start() ; err != nil { panic(err) } go func() { encoder.Write(...) // Write raw audio data to the pipeline }() // don't actually do this - copy encoded audio to stdout if _, err := io.Copy(os.Stdout, encoder) ; err != nil { panic(err) } }
You can accomplish the same thing using the "configuration" functionality provided by NewPipelineFromConfig(). Here is an example that will record from a pulse server and make opus/webm data available on the Reader.
import ( "io" "os" "github.com/tinyzimmer/go-gst-launch/gst" ) func main() { gst.Init() encoder, err := gst.NewPipelineFromConfig(&gst.PipelineConfig{ Plugins: []*gst.Plugin{ { Name: "pulsesrc", Data: map[string]interface{}{ "server": "/run/user/1000/pulse/native", "device": "playback-device.monitor", }, SinkCaps: gst.NewRawCaps("S16LE", 24000, 2), }, { Name: "opusenc", }, { Name: "webmmux", }, }, }, gst.PipelineRead, nil) if err != nil { panic(err) } defer encoder.Close() if err := encoder.Start() ; err != nil { panic(err) } // Create an output file f, err := os.Create("out.opus") if err != nil { panic(err) } // Copy the data from the pipeline to the file if err := io.Copy(f, encoder) ; err != nil { panic(err) } }
There are two channels exported for listening for messages from the pipeline. An example of listening to messages on a fake pipeline for 10 seconds:
package main import ( "fmt" "time" "github.com/tinyzimmer/go-gst-launch/gst" ) func main() { gst.Init() pipeline, err := gst.NewPipelineFromLaunchString("audiotestsrc ! fakesink", gst.PipelineInternalOnly) if err != nil { panic(err) } defer pipeline.Close() go func() { for msg := range pipeline.MessageChan() { fmt.Println("Got message:", msg.TypeName()) } }() go func() { for msg := range pipeline.ErrorChan() { fmt.Println("Got error:", err) } }() if err := pipeline.Start(); err != nil { fmt.Println("Pipeline failed to start") return } time.Sleep(time.Second * 10) }
The package also exposes some low level functionality for building pipelines and doing dynamic linking yourself. See the NewPipeline() function for creating an empty pipeline that you can then build out using the other structs and methods provided by this package.
Index ¶
- Constants
- Variables
- func ElementLinkMany(elems ...*Element) error
- func Init()
- func NewElementMany(elemNames ...string) (map[int]*Element, error)
- func Wait(p *Pipeline)
- type AppSink
- type AppSrc
- type Bin
- func (b *Bin) Add(elem *Element) error
- func (b *Bin) AddMany(elems ...*Element) error
- func (b *Bin) GetElementByName(name string) (*Element, error)
- func (b *Bin) GetElements() ([]*Element, error)
- func (b *Bin) GetSinkElements() ([]*Element, error)
- func (b *Bin) GetSourceElements() ([]*Element, error)
- func (b *Bin) Instance() *C.GstBin
- type Bus
- type Caps
- type Element
- func (e *Element) BlockSetState(state State) error
- func (e *Element) GetBus() (*Bus, error)
- func (e *Element) GetFactory() *ElementFactory
- func (e *Element) GetState() State
- func (e *Element) Instance() *C.GstElement
- func (e *Element) Link(elem *Element) error
- func (e *Element) LinkFiltered(elem *Element, caps *Caps) error
- func (e *Element) SetState(state State) error
- type ElementFactory
- func (e *ElementFactory) CanSinkAllCaps(caps *C.GstCaps) bool
- func (e *ElementFactory) CanSinkAnyCaps(caps *C.GstCaps) bool
- func (e *ElementFactory) CanSourceAllCaps(caps *C.GstCaps) bool
- func (e *ElementFactory) CanSourceAnyCaps(caps *C.GstCaps) bool
- func (e *ElementFactory) GetMetadata(key string) string
- func (e *ElementFactory) GetMetadataKeys() []string
- func (e *ElementFactory) Instance() *C.GstElementFactory
- type FlowReturn
- type GoGError
- type Message
- func (m *Message) Copy() *Message
- func (m *Message) Native() *C.GstMessage
- func (m *Message) ParseError() *GoGError
- func (m *Message) ParseInfo() *GoGError
- func (m *Message) ParseStateChanged() (oldState, newState State)
- func (m *Message) ParseWarning() *GoGError
- func (m *Message) Ref() *Message
- func (m *Message) Type() MessageType
- func (m *Message) TypeName() string
- func (m *Message) Unref()
- type MessageType
- type Object
- type Pipeline
- func (p *Pipeline) AutoFlush() bool
- func (p *Pipeline) BlockFlush() error
- func (p *Pipeline) Close() error
- func (p *Pipeline) Flush() error
- func (p *Pipeline) GetAppSink() *AppSink
- func (p *Pipeline) GetAppSrc() *AppSrc
- func (p *Pipeline) GetBus() *Bus
- func (p *Pipeline) Instance() *C.GstPipeline
- func (p *Pipeline) IsUsingGstApp() bool
- func (p *Pipeline) LinkReaderTo(elem *Element)
- func (p *Pipeline) LinkWriterTo(elem *Element)
- func (p *Pipeline) Read(b []byte) (int, error)
- func (p *Pipeline) ReadBufferSize() int
- func (p *Pipeline) SetAutoFlush(b bool)
- func (p *Pipeline) SetWriterCaps(caps *Caps)
- func (p *Pipeline) Start() error
- func (p *Pipeline) TotalBufferSize() int
- func (p *Pipeline) Write(b []byte) (int, error)
- func (p *Pipeline) WriteBufferSize() int
- type PipelineConfig
- type PipelineElement
- type PipelineFlags
- type Plugin
- type PluginFeature
- type Registry
- type Sample
- type State
Constants ¶
const ( MessageAny MessageType = C.GST_MESSAGE_ANY MessageStreamStart = C.GST_MESSAGE_STREAM_START MessageEOS = C.GST_MESSAGE_EOS MessageInfo = C.GST_MESSAGE_INFO MessageWarning = C.GST_MESSAGE_WARNING MessageError = C.GST_MESSAGE_ERROR MessageStateChanged = C.GST_MESSAGE_STATE_CHANGED MessageElement = C.GST_MESSAGE_ELEMENT MessageStreamStatus = C.GST_MESSAGE_STREAM_STATUS MessageBuffering = C.GST_MESSAGE_BUFFERING MessageLatency = C.GST_MESSAGE_LATENCY MessageNewClock = C.GST_MESSAGE_NEW_CLOCK MessageAsyncDone = C.GST_MESSAGE_ASYNC_DONE MessageTag = C.GST_MESSAGE_TAG )
Type casting of GstMessageTypes
const ( FlowOK FlowReturn = C.GST_FLOW_OK // Data passing was ok FlowNotLinked = C.GST_FLOW_NOT_LINKED // Pad is not linked FlowFlushing = C.GST_FLOW_FLUSHING // Pad is flushing FlowEOS = C.GST_FLOW_EOS // Pad is EOS FlowNotNegotiated = C.GST_FLOW_NOT_NEGOTIATED // Pad is not negotiated FlowError = C.GST_FLOW_ERROR // Some (fatal) error occurred FlowNotSupported = C.GST_FLOW_NOT_SUPPORTED // The operation is not supported. )
Type casting of the GstFlowReturn types. Custom ones are omitted for now.
const ( VoidPending State = C.GST_STATE_VOID_PENDING // (0) – no pending state. StateNull = C.GST_STATE_NULL // (1) – the NULL state or initial state of an element. StateReady = C.GST_STATE_READY // (2) – the element is ready to go to PAUSED. StatePaused = C.GST_STATE_PAUSED // (3) – the element is PAUSED, it is ready to accept and process data. Sink elements however only accept one buffer and then block. StatePlaying = C.GST_STATE_PLAYING // (4) – the element is PLAYING, the GstClock is running and the data is flowing. )
Type casting for GstStates
Variables ¶
var ErrEOS = errors.New("Pipeline has reached end-of-stream")
ErrEOS represents that the stream has ended.
Functions ¶
func ElementLinkMany ¶ added in v0.0.10
ElementLinkMany is a go implementation of `gst_element_link_many` to compensate for no variadic functions in cgo.
func Init ¶
func Init()
Init runs `gst_init`. It currently does not support arguments. This should be called before building any pipelines.
func NewElementMany ¶
NewElementMany is a convenience wrapper around building many GstElements in a single function call. It returns an error if the creation of any element fails. A map containing the ordinal of the argument to the Element created is returned.
Types ¶
type AppSink ¶ added in v0.0.10
type AppSink struct{ *Element }
AppSink wraps an Element object with additional methods for pulling samples.
func NewAppSink ¶ added in v0.0.10
NewAppSink returns a new appsink element. Unref after usage.
func (*AppSink) BlockPullSample ¶ added in v0.0.10
BlockPullSample will block until a sample becomes available or the stream is ended.
func (*AppSink) Instance ¶ added in v0.0.10
func (a *AppSink) Instance() *C.GstAppSink
Instance returns the native GstAppSink instance.
func (*AppSink) IsEOS ¶ added in v0.0.10
IsEOS returns true if this AppSink has reached the end-of-stream.
func (*AppSink) PullSample ¶ added in v0.0.10
PullSample will try to pull a sample or return nil if none is available.
type AppSrc ¶ added in v0.0.10
type AppSrc struct{ *Element }
AppSrc wraps an Element object with additional methods for pushing samples.
func (*AppSrc) EndStream ¶ added in v0.0.10
func (a *AppSrc) EndStream() FlowReturn
EndStream signals to the app source that the stream has ended after the last queued buffer.
func (*AppSrc) PushBuffer ¶ added in v0.0.10
func (a *AppSrc) PushBuffer(data io.Reader) FlowReturn
PushBuffer pushes a buffer to the appsrc. Currently by default this will block until the source is ready to accept the buffer.
func (*AppSrc) SetDuration ¶ added in v0.0.10
SetDuration sets the duration of the source stream. You should call this if the value is known.
type Bin ¶ added in v0.0.10
type Bin struct{ *Element }
Bin is a go wrapper arounds a GstBin.
func (*Bin) AddMany ¶ added in v0.0.10
AddMany is a go implementation of `gst_bin_add_many` to compensate for the inability to use variadic functions in cgo.
func (*Bin) GetElementByName ¶ added in v0.0.10
GetElementByName returns the element with the given name. Unref after usage.
func (*Bin) GetElements ¶ added in v0.0.10
GetElements returns a list of the elements added to this pipeline. Unref elements after usage.
func (*Bin) GetSinkElements ¶ added in v0.0.10
GetSinkElements returns a list of all the sink elements in this pipeline. Unref elements after usage.
func (*Bin) GetSourceElements ¶ added in v0.0.10
GetSourceElements returns a list of all the source elements in this pipeline. Unref elements after usafe.
type Bus ¶
type Bus struct { *Object // contains filtered or unexported fields }
Bus is a Go wrapper around a GstBus. It provides convenience methods for popping messages from the queue.
func (*Bus) BlockPopMessage ¶
BlockPopMessage blocks until a message is available on the bus and then returns it. This function can return nil if the bus is closed. The message should be unreffed after usage.
func (*Bus) MessageChan ¶ added in v0.0.10
MessageChan returns a new channel to listen for messages asynchronously. Messages should be unreffed after each usage. Messages are delivered to channels in the order in which this function was called.
While a message is being delivered to created channels, there is a lock on creating new ones.
type Caps ¶
Caps is a wrapper around GstCaps. It provides a function for easy type conversion.
func NewRawCaps ¶
NewRawCaps returns new GstCaps with the given format, sample-rate, and channels.
type Element ¶
type Element struct{ *Object }
Element is a Go wrapper around a GstElement.
func NewElement ¶
NewElement is a generic wrapper around `gst_element_factory_make`.
func (*Element) BlockSetState ¶ added in v0.0.10
BlockSetState is like SetState except it will block until the transition is complete.
func (*Element) GetBus ¶ added in v0.0.10
GetBus returns the GstBus for retrieving messages from this element.
func (*Element) GetFactory ¶ added in v0.0.10
func (e *Element) GetFactory() *ElementFactory
GetFactory returns the factory that created this element.
func (*Element) Instance ¶ added in v0.0.10
func (e *Element) Instance() *C.GstElement
Instance returns the underlying GstElement instance.
func (*Element) Link ¶ added in v0.0.10
Link wraps gst_element_link and links this element to the given one.
func (*Element) LinkFiltered ¶ added in v0.0.10
LinkFiltered wraps gst_element_link_filtered and link this element to the given one using the provided sink caps.
type ElementFactory ¶ added in v0.0.10
type ElementFactory struct{ *Object }
ElementFactory wraps the GstElementFactory
func (*ElementFactory) CanSinkAllCaps ¶ added in v0.0.10
func (e *ElementFactory) CanSinkAllCaps(caps *C.GstCaps) bool
CanSinkAllCaps checks if the factory can sink all possible capabilities.
func (*ElementFactory) CanSinkAnyCaps ¶ added in v0.0.10
func (e *ElementFactory) CanSinkAnyCaps(caps *C.GstCaps) bool
CanSinkAnyCaps checks if the factory can sink any possible capability.
func (*ElementFactory) CanSourceAllCaps ¶ added in v0.0.10
func (e *ElementFactory) CanSourceAllCaps(caps *C.GstCaps) bool
CanSourceAllCaps checks if the factory can src all possible capabilities.
func (*ElementFactory) CanSourceAnyCaps ¶ added in v0.0.10
func (e *ElementFactory) CanSourceAnyCaps(caps *C.GstCaps) bool
CanSourceAnyCaps checks if the factory can src any possible capability.
func (*ElementFactory) GetMetadata ¶ added in v0.0.10
func (e *ElementFactory) GetMetadata(key string) string
GetMetadata gets the metadata on this factory with key.
func (*ElementFactory) GetMetadataKeys ¶ added in v0.0.10
func (e *ElementFactory) GetMetadataKeys() []string
GetMetadataKeys gets the available keys for the metadata on this factory.
func (*ElementFactory) Instance ¶ added in v0.0.10
func (e *ElementFactory) Instance() *C.GstElementFactory
Instance returns the C GstFactory instance
type FlowReturn ¶ added in v0.0.10
type FlowReturn C.GstFlowReturn
FlowReturn is go type casting for GstFlowReturn.
type GoGError ¶
type GoGError struct {
// contains filtered or unexported fields
}
GoGError is a Go wrapper for a C GError. It implements the error interface and provides additional functions for retrieving debug strings and details.
func (*GoGError) DebugString ¶
DebugString returns any debug info alongside the error.
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
Message is a Go wrapper around a GstMessage. It provides convenience methods for unref-ing and parsing the underlying messages.
func (*Message) Copy ¶ added in v0.0.9
Copy will copy this object into a new Message that can be Unrefed separately.
func (*Message) Native ¶
func (m *Message) Native() *C.GstMessage
Native returns the underlying GstMessage object.
func (*Message) ParseError ¶
ParseError will return a GoGError from the contents of this message. This will only work if the GstMessageType is `GST_MESSAGE_ERROR`.
func (*Message) ParseInfo ¶
ParseInfo is identical to ParseError. The returned types are the same. However, this is intended for use with GstMessageType `GST_MESSAGE_INFO`.
func (*Message) ParseStateChanged ¶
ParseStateChanged will return the old and new states as Go strings. This will only work if the GstMessageType is `GST_MESSAGE_STATE_CHANGED`.
func (*Message) ParseWarning ¶
ParseWarning is identical to ParseError. The returned types are the same. However, this is intended for use with GstMessageType `GST_MESSAGE_WARNING`.
func (*Message) Ref ¶ added in v0.0.9
Ref will increase the ref count on this message. This increases the total amount of times Unref needs to be called before the object is freed from memory. It returns the underlying message object for convenience.
func (*Message) Type ¶
func (m *Message) Type() MessageType
Type returns the MessageType of the message.
type MessageType ¶ added in v0.0.6
type MessageType C.GstMessageType
MessageType is an alias to the C equivalent of GstMessageType.
type Object ¶ added in v0.0.10
type Object struct{ *glib.InitiallyUnowned }
Object is a go representation of a GstObject. Type casting stops here and we do not descend into the glib library.
type Pipeline ¶
type Pipeline struct { *Bin // contains filtered or unexported fields }
Pipeline is the base implementation of a GstPipeline using CGO to wrap gstreamer API calls. It provides methods to be inherited by the extending PlaybackPipeline and RecordingPipeline objects. The struct itself implements a ReadWriteCloser.
func NewPipeline ¶
func NewPipeline(flags PipelineFlags) (*Pipeline, error)
NewPipeline builds and returns a new empty Pipeline instance.
func NewPipelineFromConfig ¶
func NewPipelineFromConfig(cfg *PipelineConfig, flags PipelineFlags, caps *Caps) (pipeline *Pipeline, err error)
NewPipelineFromConfig builds a new pipeline from the given PipelineConfig. The plugins provided in the configuration will be linked in the order they are given. If using PipelineWrite, you can optionally pass a Caps object to filter between the write-buffer and the start of the pipeline.
func NewPipelineFromLaunchString ¶
func NewPipelineFromLaunchString(launchStr string, flags PipelineFlags) (*Pipeline, error)
NewPipelineFromLaunchString returns a new GstPipeline from the given launch string. If flags contain PipelineRead or PipelineWrite, the launch string is further formatted accordingly.
If using PipelineWrite, you should generally start your pipeline with the caps of the source.
func (*Pipeline) AutoFlush ¶ added in v0.0.10
AutoFlush returns true if the pipeline is using a GstAppSink and is configured to autoflush to the read-buffer.
func (*Pipeline) BlockFlush ¶ added in v0.0.10
BlockFlush is like Flush but it blocks until a sample is available. This is intended for use with PipelineUseGstApp.
func (*Pipeline) Flush ¶ added in v0.0.10
Flush flushes the app sink to the read buffer. It is usually more desirable to interface with the PullSample and BlockPullSample methods on the AppSink interface directly. Or to set autoflush to true.
func (*Pipeline) GetAppSink ¶ added in v0.0.10
GetAppSink returns the AppSink for this pipeline if created with PipelineUseGstApp. Unref after usage.
func (*Pipeline) GetAppSrc ¶ added in v0.0.10
GetAppSrc returns the AppSrc for this pipeline if created with PipelineUseGstApp. Unref after usage.
func (*Pipeline) Instance ¶ added in v0.0.10
func (p *Pipeline) Instance() *C.GstPipeline
Instance returns the native GstPipeline instance.
func (*Pipeline) IsUsingGstApp ¶ added in v0.0.10
IsUsingGstApp returns true if the current pipeline is using GstApp instead of file descriptors.
func (*Pipeline) LinkReaderTo ¶
LinkReaderTo links the read buffer on this Pipeline to the given element. This must be called when the pipeline is constructed with PipelineRead or PipelineReadWrite.
func (*Pipeline) LinkWriterTo ¶
LinkWriterTo links the write buffer on this Pipeline to the given element. This must be called when the pipeline is constructed with PipelineWrite or PipelineReadWrite.
func (*Pipeline) ReadBufferSize ¶ added in v0.0.10
ReadBufferSize returns the current size of the unread portion of the read-buffer.
func (*Pipeline) SetAutoFlush ¶ added in v0.0.10
SetAutoFlush sets whether or not samples should be automatically flushed to the read-buffer (default for pipelines not built with PipelineUseGstApp) and if messages should be flushed on the bus when the pipeline is stopped.
func (*Pipeline) SetWriterCaps ¶ added in v0.0.8
SetWriterCaps sets the caps on the write-buffer. You will usually want to call this on a custom pipeline, unless you are using downstream elements that do dynamic pad linking.
func (*Pipeline) Start ¶
Start will start the GstPipeline. It is asynchronous so it does not need to be called within a goroutine, however, it is still safe to do so.
func (*Pipeline) TotalBufferSize ¶ added in v0.0.10
TotalBufferSize returns the sum of the Read and Write buffer unread portions.
func (*Pipeline) WriteBufferSize ¶ added in v0.0.10
WriteBufferSize returns the current size of the unread portion of the write-buffer.
type PipelineConfig ¶
type PipelineConfig struct {
Elements []*PipelineElement
}
PipelineConfig represents a list of elements and their configurations to be used with NewPipelineFromConfig.
func (*PipelineConfig) ElementNames ¶ added in v0.0.10
func (p *PipelineConfig) ElementNames() []string
ElementNames returns a string slice of the names of all the plugins.
func (*PipelineConfig) GetElementByName ¶ added in v0.0.10
func (p *PipelineConfig) GetElementByName(name string) *PipelineElement
GetElementByName returns the Element configuration for the given name.
type PipelineElement ¶ added in v0.0.10
PipelineElement represents an `GstElement` in a `GstPipeline` when building a Pipeline with `NewPipelineFromConfig`. The Name should coorespond to a valid gstreamer plugin name. The data are additional fields to set on the element. If SinkCaps is non-nil, they are applied to the sink of this element.
func (*PipelineElement) GetName ¶ added in v0.0.10
func (p *PipelineElement) GetName() string
GetName returns the name to use when creating Elements from this configuration.
type PipelineFlags ¶ added in v0.0.6
type PipelineFlags int
PipelineFlags represents arguments passed to a new Pipeline.
const ( // PipelineInternalOnly signals that this pipeline only handles data internally. PipelineInternalOnly PipelineFlags = 1 << iota // PipelineRead signals that the Read() method can be used on the end of this pipeline. PipelineRead // PipelineWrite signals that the Write() method can be used on the start of this pipeline. PipelineWrite // PipelineUseGstApp signals the desire to use an AppSink or AppSrc instead of the default // os pipes, fdsrc, and fdsink. // When using this flag, you should interact with the pipeline using the GetAppSink and // GetAppSrc methods. PipelineUseGstApp // PipelineReadWrite signals that this pipeline can be both read and written to. PipelineReadWrite = PipelineRead | PipelineWrite )
type Plugin ¶
type Plugin struct{ *Object }
Plugin is a go representation of a GstPlugin.
func (*Plugin) Description ¶ added in v0.0.10
Description returns the description for this plugin.
type PluginFeature ¶ added in v0.0.10
type PluginFeature struct{ *Object }
PluginFeature is a go representation of a GstPluginFeature
func (*PluginFeature) GetPlugin ¶ added in v0.0.10
func (p *PluginFeature) GetPlugin() *Plugin
GetPlugin returns the plugin that provides this feature or nil. Unref after usage.
func (*PluginFeature) GetPluginName ¶ added in v0.0.10
func (p *PluginFeature) GetPluginName() string
GetPluginName returns the name of the plugin that provides this feature.
func (*PluginFeature) Instance ¶ added in v0.0.10
func (p *PluginFeature) Instance() *C.GstPluginFeature
Instance returns the underlying GstPluginFeature instance
type Registry ¶ added in v0.0.10
type Registry struct{ *Object }
Registry is a go representation of a GstRegistry.
func GetRegistry ¶ added in v0.0.10
func GetRegistry() *Registry
GetRegistry returns the default global GstRegistry.
func (*Registry) FindPlugin ¶ added in v0.0.10
FindPlugin retrieves the plugin by the given name. Unref after usage.
func (*Registry) Instance ¶ added in v0.0.10
func (r *Registry) Instance() *C.GstRegistry
Instance returns the underlying GstRegistry instance.
func (*Registry) LookupFeature ¶ added in v0.0.10
func (r *Registry) LookupFeature(name string) (*PluginFeature, error)
LookupFeature looks up the given plugin feature by name. Unref after usage.
type Sample ¶ added in v0.0.10
type Sample struct {
// contains filtered or unexported fields
}
Sample is a go wrapper around a GstSample object.
func (*Sample) GetBuffer ¶ added in v0.0.10
GetBuffer returns a Reader for the buffer inside this sample.