2020-01-13 21:05:38 +13:00
|
|
|
package gst
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo pkg-config: gstreamer-1.0 gstreamer-app-1.0
|
|
|
|
|
|
|
|
#include "gst.h"
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2021-02-15 05:30:24 +13:00
|
|
|
"time"
|
2020-01-13 21:05:38 +13:00
|
|
|
"unsafe"
|
|
|
|
|
2021-10-06 09:38:24 +13:00
|
|
|
"m1k1o/neko/internal/types"
|
2020-01-13 21:05:38 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
// Pipeline is a wrapper for a GStreamer Pipeline
|
|
|
|
type Pipeline struct {
|
2021-04-05 08:37:33 +12:00
|
|
|
Pipeline *C.GstElement
|
|
|
|
Sample chan types.Sample
|
|
|
|
Src string
|
|
|
|
id int
|
2020-01-13 21:05:38 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
var pipelines = make(map[int]*Pipeline)
|
|
|
|
var pipelinesLock sync.Mutex
|
2020-01-19 12:30:09 +13:00
|
|
|
var registry *C.GstRegistry
|
2020-01-13 21:05:38 +13:00
|
|
|
|
2020-01-19 12:30:09 +13:00
|
|
|
func init() {
|
|
|
|
C.gstreamer_init()
|
|
|
|
registry = C.gst_registry_get()
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:42:42 +12:00
|
|
|
// CreatePipeline creates a GStreamer Pipeline
|
2021-04-04 02:19:01 +13:00
|
|
|
func CreatePipeline(pipelineStr string) (*Pipeline, error) {
|
2020-01-13 21:05:38 +13:00
|
|
|
pipelineStrUnsafe := C.CString(pipelineStr)
|
|
|
|
defer C.free(unsafe.Pointer(pipelineStrUnsafe))
|
|
|
|
|
|
|
|
pipelinesLock.Lock()
|
|
|
|
defer pipelinesLock.Unlock()
|
|
|
|
|
2021-08-16 01:37:27 +12:00
|
|
|
var err *C.GError
|
|
|
|
gstPipeline := C.gstreamer_send_create_pipeline(pipelineStrUnsafe, &err)
|
|
|
|
if err != nil {
|
|
|
|
defer C.g_error_free(err)
|
|
|
|
return nil, fmt.Errorf("%s", C.GoString(err.message))
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:15:59 +13:00
|
|
|
p := &Pipeline{
|
2021-08-16 01:37:27 +12:00
|
|
|
Pipeline: gstPipeline,
|
2021-04-05 08:37:33 +12:00
|
|
|
Sample: make(chan types.Sample),
|
|
|
|
Src: pipelineStr,
|
|
|
|
id: len(pipelines),
|
2020-01-13 21:05:38 +13:00
|
|
|
}
|
|
|
|
|
2020-02-11 18:15:59 +13:00
|
|
|
pipelines[p.id] = p
|
|
|
|
return p, nil
|
2020-01-13 21:05:38 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the GStreamer Pipeline
|
|
|
|
func (p *Pipeline) Start() {
|
|
|
|
C.gstreamer_send_start_pipeline(p.Pipeline, C.int(p.id))
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:09:02 +12:00
|
|
|
// Play starts the GStreamer Pipeline
|
|
|
|
func (p *Pipeline) Play() {
|
|
|
|
C.gstreamer_send_play_pipeline(p.Pipeline)
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:05:38 +13:00
|
|
|
// Stop stops the GStreamer Pipeline
|
|
|
|
func (p *Pipeline) Stop() {
|
|
|
|
C.gstreamer_send_stop_pipeline(p.Pipeline)
|
|
|
|
}
|
|
|
|
|
2020-01-27 14:28:39 +13:00
|
|
|
// gst-inspect-1.0
|
2020-01-19 12:30:09 +13:00
|
|
|
func CheckPlugins(plugins []string) error {
|
|
|
|
var plugin *C.GstPlugin
|
|
|
|
for _, pluginstr := range plugins {
|
|
|
|
plugincstr := C.CString(pluginstr)
|
|
|
|
plugin = C.gst_registry_find_plugin(registry, plugincstr)
|
|
|
|
C.free(unsafe.Pointer(plugincstr))
|
|
|
|
if plugin == nil {
|
2020-01-25 04:47:37 +13:00
|
|
|
return fmt.Errorf("required gstreamer plugin %s not found", pluginstr)
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:05:38 +13:00
|
|
|
//export goHandlePipelineBuffer
|
|
|
|
func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.int, pipelineID C.int) {
|
|
|
|
pipelinesLock.Lock()
|
|
|
|
pipeline, ok := pipelines[int(pipelineID)]
|
|
|
|
pipelinesLock.Unlock()
|
|
|
|
|
|
|
|
if ok {
|
2021-02-15 05:30:24 +13:00
|
|
|
pipeline.Sample <- types.Sample{Data: C.GoBytes(buffer, bufferLen), Timestamp: time.Now(), Duration: time.Duration(duration)}
|
2020-01-13 21:05:38 +13:00
|
|
|
} else {
|
|
|
|
fmt.Printf("discarding buffer, no pipeline with id %d", int(pipelineID))
|
|
|
|
}
|
|
|
|
C.free(buffer)
|
|
|
|
}
|