more subclassing code, start to migrate example

This commit is contained in:
RSWilli
2025-09-16 22:36:07 +02:00
parent 74ba1ec9b0
commit e7d3de9e6e
16 changed files with 538 additions and 210 deletions

View File

@@ -7,14 +7,14 @@ import (
)
type customBin struct {
gst.Bin // parent object must be first embedded field
source1 gst.Element
source2 gst.Element
mixer gst.Element
gst.BinInstance // parent object must be first embedded field
source1 gst.Element
source2 gst.Element
mixer gst.Element
}
// init should initialize the element. Keep in mind that the properties are not yet present. When this is called.
func (bin *customBin) init() {
// constructed is the method we use to override the GOBject.constructed method.
func (bin *customBin) constructed() {
bin.source1 = gst.ElementFactoryMakeWithProperties("gocustomsrc", map[string]interface{}{
"duration": int64(5 * time.Second),
})

View File

@@ -1,41 +1,45 @@
package custombin
import (
"github.com/diamondburned/gotk4/pkg/gobject/v2"
"github.com/go-gst/go-gst/pkg/gst"
)
// Register needs to be called after gst.Init() to make the gocustombin available in the standard
// gst element registry. After this call the element can be used like any other gstreamer element
func Register() bool {
panic("Register is not implemented yet")
// registered := glib.RegisterSubclassWithConstructor[*customBin](
// func() *customBin {
// return &customBin{}
// },
// glib.WithOverrides[*customBin, gst.BinOverrides](func(b *customBin) gst.BinOverrides {
// return gst.BinOverrides{}
// }),
// glib.WithClassInit[*gst.BinClass](func(bc *gst.BinClass) {
// bc.ParentClass().SetStaticMetadata(
// "custom test source",
// "Src/Test",
// "Demo source bin with volume",
// "Wilhelm Bartel <bartel.wilhelm@gmail.com>",
// )
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>",
)
},
nil,
gst.BinOverrides[*customBin]{
ElementOverrides: gst.ElementOverrides[*customBin]{
ObjectOverrides: gst.ObjectOverrides[*customBin]{
InitiallyUnownedOverrides: gobject.InitiallyUnownedOverrides[*customBin]{
ObjectOverrides: gobject.ObjectOverrides[*customBin]{
Constructed: (*customBin).constructed,
},
},
},
},
},
map[string]gobject.SignalDefinition{},
)
// bc.ParentClass().AddPadTemplate(gst.NewPadTemplate(
// "src",
// gst.PadSrc,
// gst.PadAlways,
// gst.CapsFromString("audio/x-raw,channels=2,rate=48000"),
// ))
// }),
// )
// return gst.ElementRegister(
// // no plugin:
// nil,
// // The name of the element
// "gocustombin",
// // The rank of the element
// uint(gst.RankNone),
// // The GoElement implementation for the element
// registered.Type(),
// )
return gst.ElementRegister(
// no plugin:
nil,
// The name of the element
"gocustombin",
// The rank of the element
uint(gst.RankNone),
registered,
)
}

View File

@@ -4,6 +4,7 @@ import (
"math"
"time"
"github.com/diamondburned/gotk4/pkg/gobject/v2"
"github.com/go-gst/go-gst/pkg/gst"
)
@@ -13,12 +14,12 @@ const samplesperbuffer = 4800
const samplerate = 48000
type customSrc struct {
gst.Bin // parent must be embedded as the first field
gst.BinInstance // parent must be embedded as the first field
source gst.Element
volume gst.Element
Duration time.Duration `glib:"duration"`
Duration time.Duration
}
// InstanceInit should initialize the element. Keep in mind that the properties are not yet present. When this is called.
@@ -45,6 +46,25 @@ func (bin *customSrc) init() {
bin.updateSource()
}
func (bin *customSrc) setProperty(_ uint, value any, pspec *gobject.ParamSpec) {
switch pspec.Name() {
case "duration":
bin.Duration = value.(time.Duration)
bin.updateSource()
default:
panic("unknown property")
}
}
func (bin *customSrc) getProperty(_ uint, pspec *gobject.ParamSpec) any {
switch pspec.Name() {
case "duration":
return bin.Duration
default:
panic("unknown property")
}
}
// updateSource will get called to update the audiotestsrc when a property changes
func (s *customSrc) updateSource() {
if s.source != nil {

View File

@@ -1,40 +1,60 @@
package customsrc
import (
"math"
"github.com/diamondburned/gotk4/pkg/gobject/v2"
"github.com/go-gst/go-gst/pkg/gst"
)
// Register needs to be called after gst.Init() to make the gocustomsrc available in the standard
// gst element registry. After this call the element can be used like any other gstreamer element
func Register() bool {
panic("Register is not implemented yet")
// registered := glib.RegisterSubclassWithConstructor[*customSrc](
// func() *customSrc {
// return &customSrc{}
// },
// glib.WithOverrides[*customSrc, gst.BinOverrides](func(b *customSrc) gst.BinOverrides {
// return gst.BinOverrides{}
// }),
// glib.WithClassInit[*gst.BinClass](func(bc *gst.BinClass) {
// bc.ParentClass().SetStaticMetadata(
// "custom test source",
// "Src/Test",
// "Demo source bin with volume",
// "Wilhelm Bartel <bartel.wilhelm@gmail.com>",
// )
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>",
)
// bc.ParentClass().AddPadTemplate(gst.NewPadTemplate(
// "src",
// gst.PadSrc,
// gst.PadAlways,
// gst.CapsFromString("audio/x-raw,channels=2,rate=48000"),
// ))
// }),
// )
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),
})
},
nil,
gst.BinOverrides[*customSrc]{
ElementOverrides: gst.ElementOverrides[*customSrc]{
ObjectOverrides: gst.ObjectOverrides[*customSrc]{
InitiallyUnownedOverrides: gobject.InitiallyUnownedOverrides[*customSrc]{
ObjectOverrides: gobject.ObjectOverrides[*customSrc]{
Constructed: (*customSrc).init,
SetProperty: (*customSrc).setProperty,
GetProperty: (*customSrc).getProperty,
},
},
},
},
},
map[string]gobject.SignalDefinition{},
)
// return gst.ElementRegister(
// // no plugin:
// nil,
// // The name of the element
// "gocustomsrc",
// // The rank of the element
// uint(gst.RankNone),
// registered.Type(),
// )
return gst.ElementRegister(
// no plugin:
nil,
// The name of the element
"gocustomsrc",
// The rank of the element
uint(gst.RankNone),
registered,
)
}

View File

@@ -72,6 +72,8 @@ func run(ctx context.Context) error {
fmt.Println("reached EOS")
cancel()
return
default:
fmt.Println("got message:", msg.String())
}
return