mirror of
https://github.com/go-gst/go-gst.git
synced 2025-10-17 21:41:13 +08:00
subclassing working! regenerate with gotk4
This commit is contained in:
@@ -6,6 +6,22 @@ import (
|
||||
"github.com/go-gst/go-gst/pkg/gst"
|
||||
)
|
||||
|
||||
func classInit(class *gst.BinClass) {
|
||||
class.ParentClass().SetStaticMetadata(
|
||||
"custom test source",
|
||||
"Src/Test",
|
||||
"Demo source bin with volume",
|
||||
"Wilhelm Bartel <bartel.wilhelm@gmail.com>",
|
||||
)
|
||||
|
||||
class.ParentClass().AddPadTemplate(gst.NewPadTemplate(
|
||||
"src",
|
||||
gst.PadSrc,
|
||||
gst.PadAlways,
|
||||
gst.CapsFromString("audio/x-raw,channels=2,rate=48000"),
|
||||
))
|
||||
}
|
||||
|
||||
type customBin struct {
|
||||
gst.BinInstance // parent object must be first embedded field
|
||||
source1 gst.Element
|
||||
@@ -15,11 +31,11 @@ type customBin struct {
|
||||
|
||||
// constructed is the method we use to override the GOBject.constructed method.
|
||||
func (bin *customBin) constructed() {
|
||||
bin.source1 = gst.ElementFactoryMakeWithProperties("gocustomsrc", map[string]interface{}{
|
||||
bin.source1 = gst.ElementFactoryMakeWithProperties("gocustomsrc", map[string]any{
|
||||
"duration": int64(5 * time.Second),
|
||||
})
|
||||
|
||||
bin.source2 = gst.ElementFactoryMakeWithProperties("gocustomsrc", map[string]interface{}{
|
||||
bin.source2 = gst.ElementFactoryMakeWithProperties("gocustomsrc", map[string]any{
|
||||
"duration": int64(10 * time.Second),
|
||||
})
|
||||
|
||||
|
@@ -10,14 +10,7 @@ import (
|
||||
func Register() bool {
|
||||
registered := gst.RegisterBinSubClass[*customBin](
|
||||
"gocustombin",
|
||||
func(class *gst.BinClass) {
|
||||
class.ParentClass().SetStaticMetadata(
|
||||
"custom test source",
|
||||
"Src/Test",
|
||||
"Demo source bin with volume",
|
||||
"Wilhelm Bartel <bartel.wilhelm@gmail.com>",
|
||||
)
|
||||
},
|
||||
classInit,
|
||||
nil,
|
||||
gst.BinOverrides[*customBin]{
|
||||
ElementOverrides: gst.ElementOverrides[*customBin]{
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package customsrc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
@@ -13,6 +15,33 @@ const samplesperbuffer = 4800
|
||||
|
||||
const samplerate = 48000
|
||||
|
||||
func classInit(class *gst.BinClass) {
|
||||
class.ParentClass().SetStaticMetadata(
|
||||
"custom test source",
|
||||
"Src/Test",
|
||||
"Demo source bin with volume",
|
||||
"Wilhelm Bartel <bartel.wilhelm@gmail.com>",
|
||||
)
|
||||
|
||||
class.ParentClass().AddPadTemplate(gst.NewPadTemplate(
|
||||
"src",
|
||||
gst.PadSrc,
|
||||
gst.PadAlways,
|
||||
gst.CapsFromString(fmt.Sprintf("audio/x-raw,channels=2,rate=%d", samplerate)),
|
||||
))
|
||||
|
||||
class.ParentClass().ParentClass().ParentClass().ParentClass().InstallProperties([]*gobject.ParamSpec{
|
||||
gobject.ParamSpecInt64(
|
||||
"duration",
|
||||
"duration",
|
||||
"Duration of the source in nanoseconds",
|
||||
0,
|
||||
math.MaxInt64,
|
||||
0,
|
||||
gobject.ParamReadwrite|gobject.ParamConstruct),
|
||||
})
|
||||
}
|
||||
|
||||
type customSrc struct {
|
||||
gst.BinInstance // parent must be embedded as the first field
|
||||
|
||||
@@ -24,7 +53,9 @@ type customSrc struct {
|
||||
|
||||
// InstanceInit should initialize the element. Keep in mind that the properties are not yet present. When this is called.
|
||||
func (bin *customSrc) init() {
|
||||
bin.source = gst.ElementFactoryMake("audiotestsrc", "")
|
||||
bin.source = gst.ElementFactoryMakeWithProperties("audiotestsrc", map[string]any{
|
||||
"samplesperbuffer": samplesperbuffer,
|
||||
})
|
||||
bin.volume = gst.ElementFactoryMake("volume", "")
|
||||
|
||||
bin.AddMany(
|
||||
@@ -49,7 +80,8 @@ func (bin *customSrc) init() {
|
||||
func (bin *customSrc) setProperty(_ uint, value any, pspec *gobject.ParamSpec) {
|
||||
switch pspec.Name() {
|
||||
case "duration":
|
||||
bin.Duration = value.(time.Duration)
|
||||
bin.Duration = time.Duration(value.(int64)) // declared as int64 property in classInit
|
||||
log.Printf("set duration to %s", bin.Duration)
|
||||
bin.updateSource()
|
||||
default:
|
||||
panic("unknown property")
|
||||
@@ -59,7 +91,7 @@ func (bin *customSrc) setProperty(_ uint, value any, pspec *gobject.ParamSpec) {
|
||||
func (bin *customSrc) getProperty(_ uint, pspec *gobject.ParamSpec) any {
|
||||
switch pspec.Name() {
|
||||
case "duration":
|
||||
return bin.Duration
|
||||
return int64(bin.Duration) // declared as int64 property in classInit
|
||||
default:
|
||||
panic("unknown property")
|
||||
}
|
||||
@@ -67,9 +99,14 @@ func (bin *customSrc) getProperty(_ uint, pspec *gobject.ParamSpec) any {
|
||||
|
||||
// updateSource will get called to update the audiotestsrc when a property changes
|
||||
func (s *customSrc) updateSource() {
|
||||
if s.source != nil {
|
||||
numBuffers := (float64(s.Duration / time.Second)) / (float64(samplesperbuffer) / float64(samplerate))
|
||||
|
||||
s.source.SetObjectProperty("num-buffers", int(math.Ceil(numBuffers)))
|
||||
if s.source == nil {
|
||||
// the construct param may be set before we initialized the source
|
||||
return
|
||||
}
|
||||
|
||||
numBuffers := (float64(s.Duration / time.Second)) / (float64(samplesperbuffer) / float64(samplerate))
|
||||
|
||||
s.source.SetObjectProperty("num-buffers", int32(math.Ceil(numBuffers)))
|
||||
|
||||
log.Printf("set num-buffers to %d", int(math.Ceil(numBuffers)))
|
||||
}
|
||||
|
@@ -1,8 +1,6 @@
|
||||
package customsrc
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/diamondburned/gotk4/pkg/gobject/v2"
|
||||
"github.com/go-gst/go-gst/pkg/gst"
|
||||
)
|
||||
@@ -12,25 +10,7 @@ import (
|
||||
func Register() bool {
|
||||
registered := gst.RegisterBinSubClass[*customSrc](
|
||||
"gocustomsrc",
|
||||
func(class *gst.BinClass) {
|
||||
class.ParentClass().SetStaticMetadata(
|
||||
"custom test source",
|
||||
"Src/Test",
|
||||
"Demo source bin with volume",
|
||||
"Wilhelm Bartel <bartel.wilhelm@gmail.com>",
|
||||
)
|
||||
|
||||
class.ParentClass().ParentClass().ParentClass().ParentClass().InstallProperties([]*gobject.ParamSpec{
|
||||
gobject.ParamSpecInt(
|
||||
"duration",
|
||||
"Duration",
|
||||
"Duration of the source in nanoseconds",
|
||||
0,
|
||||
math.MaxInt64,
|
||||
0,
|
||||
gobject.ParamWritable|gobject.ParamReadable|gst.ParamMutableReady),
|
||||
})
|
||||
},
|
||||
classInit,
|
||||
nil,
|
||||
gst.BinOverrides[*customSrc]{
|
||||
ElementOverrides: gst.ElementOverrides[*customSrc]{
|
||||
|
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
@@ -30,6 +31,10 @@ func run(ctx context.Context) error {
|
||||
|
||||
systemclock := gst.SystemClockObtain()
|
||||
|
||||
log.Printf("using system clock: %s", systemclock.GetName())
|
||||
|
||||
// After registering the elements, we can use them in the parsed pipeline strings as if they were
|
||||
// provided by a plugin.
|
||||
ret, err := gst.ParseLaunch("gocustombin ! fakesink sync=true")
|
||||
|
||||
if err != nil {
|
||||
@@ -42,8 +47,6 @@ func run(ctx context.Context) error {
|
||||
|
||||
bus := pipeline.GetBus()
|
||||
|
||||
pipeline.SetState(gst.StatePlaying)
|
||||
|
||||
go func() {
|
||||
for msg := range bus.Messages(ctx) {
|
||||
switch msg.Type() {
|
||||
@@ -75,11 +78,11 @@ func run(ctx context.Context) error {
|
||||
default:
|
||||
fmt.Println("got message:", msg.String())
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
pipeline.SetState(gst.StatePlaying)
|
||||
|
||||
<-ctx.Done()
|
||||
|
||||
pipeline.BlockSetState(gst.StateNull, gst.ClockTime(time.Second))
|
||||
|
Reference in New Issue
Block a user