Skip to content

Commit

Permalink
Merge pull request #21 from jwmwalrus/master
Browse files Browse the repository at this point in the history
GstStateChangeReturn, Playbin support, SimpleSeek support
  • Loading branch information
notedit authored Dec 21, 2020
2 parents 6000b39 + 06dccea commit 6b2222d
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 2 deletions.
19 changes: 19 additions & 0 deletions bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "C"

import (
"runtime"
"time"
)

type Bus struct {
Expand Down Expand Up @@ -46,6 +47,24 @@ func (b *Bus) Pull(messageType MessageType) (message *Message) {
return
}

func (b *Bus) TimedPopFiltered(timeout time.Duration, messageType MessageType) (message *Message) {

CGstMessage := C.gst_bus_timed_pop_filtered(b.C, C.GstClockTime(uint64(timeout)), C.GstMessageType(messageType))
if CGstMessage == nil {
return nil
}

message = &Message{
C: CGstMessage,
}

runtime.SetFinalizer(message, func(message *Message) {
C.gst_message_unref(message.C)
})

return
}

func (b *Bus) HavePending() bool {
return C.gst_bus_have_pending(b.C) != 0
}
60 changes: 60 additions & 0 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ const (
StatePlaying StateOptions = C.GST_STATE_PLAYING
)

type StateChangeReturn int

const (
StateChangeFailure StateChangeReturn = C.GST_STATE_CHANGE_FAILURE
StateChangeSuccess StateChangeReturn = C.GST_STATE_CHANGE_SUCCESS
StateChangeAsync StateChangeReturn = C.GST_STATE_CHANGE_ASYNC
StateChangePreroll StateChangeReturn = C.GST_STATE_CHANGE_NO_PREROLL
)

type SeekFlags int

const (
SeekFlagNone SeekFlags = C.GST_SEEK_FLAG_NONE
SeekFlagFlush SeekFlags = C.GST_SEEK_FLAG_FLUSH
SeekFlagAccurate SeekFlags = C.GST_SEEK_FLAG_ACCURATE
SeekFlagKeyUnit SeekFlags = C.GST_SEEK_FLAG_KEY_UNIT
SeekFlagSegment SeekFlags = C.GST_SEEK_FLAG_SEGMENT
SeekFlagTrickmode SeekFlags = C.GST_SEEK_FLAG_TRICKMODE
SeekFlagSkip SeekFlags = C.GST_SEEK_FLAG_SKIP
SeekFlagSnapBefore SeekFlags = C.GST_SEEK_FLAG_SNAP_BEFORE
SeekFlagSnapAfter SeekFlags = C.GST_SEEK_FLAG_SNAP_AFTER
SeekFlagSnapNearest SeekFlags = C.GST_SEEK_FLAG_SNAP_NEAREST
SeekFlagTrickmodeKeyUnits SeekFlags = C.GST_SEEK_FLAG_TRICKMODE_KEY_UNITS
SeekFlagTrickmodeNoAudio SeekFlags = C.GST_SEEK_FLAG_TRICKMODE_NO_AUDIO
SeekFlagTrickmodeForwardPredicted SeekFlags = C.GST_SEEK_FLAG_TRICKMODE_FORWARD_PREDICTED
SeekFlagInstantRateChange SeekFlags = C.GST_SEEK_FLAG_INSTANT_RATE_CHANGE
)

type Element struct {
GstElement *C.GstElement
onPadAdded PadAddedCallback
Expand Down Expand Up @@ -112,6 +140,11 @@ func (e *Element) GetStaticPad(name string) (pad *Pad) {
return
}

func (e *Element) Query(q *Query) bool {
Cboolean := C.gst_element_query(e.GstElement, q.C)
return Cboolean == 1
}

func (e *Element) QueryPosition() (time.Duration, error) {
var position C.gint64

Expand Down Expand Up @@ -282,6 +315,33 @@ func (e *Element) SendEvent(event *Event) bool {
return false
}

func (e *Element) SetState(state StateOptions) StateChangeReturn {
Cint := C.gst_element_set_state(e.GstElement, C.GstState(state))
return StateChangeReturn(Cint)
}

func (e *Element) GetBus() (bus *Bus) {

CBus := C.X_gst_element_get_bus(e.GstElement)

bus = &Bus{
C: CBus,
}

runtime.SetFinalizer(bus, func(bus *Bus) {
C.gst_object_unref(C.gpointer(unsafe.Pointer(bus.C)))
})

return
}

func (e *Element) SeekSimple(format FormatOptions, flags SeekFlags, seekPos time.Duration) bool {

Cbool := C.gst_element_seek_simple(e.GstElement, C.GstFormat(format), C.GstSeekFlags(flags), C.gint64(seekPos))

return Cbool == 1
}

func (e *Element) cleanCallback() {

if e.onPadAdded == nil {
Expand Down
5 changes: 5 additions & 0 deletions gst.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ GstBus* X_gst_pipeline_get_bus(GstElement* element) {
return gst_pipeline_get_bus(GST_PIPELINE(element));
}

GstBus* X_gst_element_get_bus(GstElement* element) {
return gst_element_get_bus(element);
}


GstClock * X_gst_pipeline_get_clock(GstElement* element) {
return gst_pipeline_get_clock(GST_PIPELINE(element));
}
Expand Down
1 change: 1 addition & 0 deletions gst.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ extern void X_gst_structure_set_bool(GstStructure *structure, const gchar *name,
extern GstEventType X_GST_EVENT_TYPE(GstEvent* event);
extern GstMessageType X_GST_MESSAGE_TYPE(GstMessage *message);
extern GstBus* X_gst_pipeline_get_bus(GstElement* element);
extern GstBus* X_gst_element_get_bus(GstElement* element);
extern GstClock * X_gst_pipeline_get_clock(GstElement* element);
extern GstClockTime X_gst_pipeline_get_delay(GstElement* element);
extern GstClockTime X_gst_pipeline_get_latency(GstElement* element);
Expand Down
9 changes: 9 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,12 @@ func (message *Message) GetStructure() (structure *Structure) {

return
}

func (message *Message) ParseStateChanged() (oldState, newState, pending StateOptions) {
var Coldstate, Cnewstate, Cpending C.GstState
C.gst_message_parse_state_changed(message.C, &Coldstate, &Cnewstate, &Cpending)
oldState = StateOptions(Coldstate)
newState = StateOptions(Cnewstate)
pending = StateOptions(Cpending)
return
}
5 changes: 3 additions & 2 deletions pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ func PipelineNew(name string) (e *Pipeline, err error) {
return
}

func (p *Pipeline) SetState(state StateOptions) {
C.gst_element_set_state(p.GstElement, C.GstState(state))
func (p *Pipeline) SetState(state StateOptions) StateChangeReturn {
Cint := C.gst_element_set_state(p.GstElement, C.GstState(state))
return StateChangeReturn(Cint)
}

func (p *Pipeline) GetBus() (bus *Bus) {
Expand Down
66 changes: 66 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package gst

/*
#cgo pkg-config: gstreamer-1.0
#include "gst.h"
*/
import "C"
import (
"errors"
"runtime"
"time"
"unsafe"
)

type FormatOptions int

const (
FormatUndefined FormatOptions = C.GST_FORMAT_UNDEFINED
FormatDefault FormatOptions = C.GST_FORMAT_DEFAULT
FormatBytes FormatOptions = C.GST_FORMAT_BYTES
FormatTime FormatOptions = C.GST_FORMAT_TIME
FormatBuffers FormatOptions = C.GST_FORMAT_BUFFERS
FormatPercent FormatOptions = C.GST_FORMAT_PERCENT
)

type Query struct {
C *C.GstQuery
}

func QueryNewSeeking(format FormatOptions) (q *Query, err error) {

gstQuery := C.gst_query_new_seeking(C.GstFormat(format))
if gstQuery == nil {
err = errors.New("could not create a Gstreamer query")
return
}

q = &Query{}

q.C = gstQuery

runtime.SetFinalizer(q, func(q *Query) {
C.gst_object_unref(C.gpointer(unsafe.Pointer(q.C)))
})

return
}

func (q *Query) ParseSeeking(format *FormatOptions) (seekable bool, segmentStart, segmentEnd time.Duration) {

var Cformat C.GstFormat
var Cseekable C.gboolean
var CsegmentStart, CsegmentEnd C.gint64

if format != nil {
Cformat = C.GstFormat(*format)
}

C.gst_query_parse_seeking(q.C, &Cformat, &Cseekable, &CsegmentStart, &CsegmentEnd)

seekable = Cseekable == 1
segmentStart = time.Duration(CsegmentStart)
segmentEnd = time.Duration(CsegmentEnd)

return
}

0 comments on commit 6b2222d

Please sign in to comment.