2020-10-23 03:54:50 +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 02:40:17 +13:00
|
|
|
"time"
|
2020-10-23 03:54:50 +13:00
|
|
|
"unsafe"
|
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
type Pipeline struct {
|
2021-02-15 02:40:17 +13:00
|
|
|
Pipeline *C.GstElement
|
|
|
|
Sample chan types.Sample
|
|
|
|
Src string
|
|
|
|
id int
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
var pipelines = make(map[int]*Pipeline)
|
|
|
|
var pipelinesLock sync.Mutex
|
|
|
|
var registry *C.GstRegistry
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
C.gstreamer_init()
|
|
|
|
registry = C.gst_registry_get()
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:50:18 +13:00
|
|
|
func CreatePipeline(pipelineStr string) (*Pipeline, error) {
|
2020-10-23 03:54:50 +13:00
|
|
|
pipelineStrUnsafe := C.CString(pipelineStr)
|
|
|
|
defer C.free(unsafe.Pointer(pipelineStrUnsafe))
|
|
|
|
|
|
|
|
pipelinesLock.Lock()
|
|
|
|
defer pipelinesLock.Unlock()
|
|
|
|
|
2021-01-13 04:12:05 +13:00
|
|
|
var gstPipeline *C.GstElement
|
|
|
|
var gstError *C.GError
|
|
|
|
|
|
|
|
gstPipeline = C.gst_parse_launch(pipelineStrUnsafe, &gstError)
|
|
|
|
|
2021-01-13 04:24:54 +13:00
|
|
|
if gstError != nil {
|
2021-01-15 02:15:17 +13:00
|
|
|
defer C.g_error_free(gstError)
|
2021-02-15 02:40:17 +13:00
|
|
|
return nil, fmt.Errorf("(pipeline error) %s", C.GoString(gstError.message))
|
2021-01-13 04:12:05 +13:00
|
|
|
}
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
p := &Pipeline{
|
2021-02-15 02:40:17 +13:00
|
|
|
Pipeline: gstPipeline,
|
|
|
|
Sample: make(chan types.Sample),
|
|
|
|
Src: pipelineStr,
|
|
|
|
id: len(pipelines),
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
pipelines[p.id] = p
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pipeline) Start() {
|
|
|
|
C.gstreamer_send_start_pipeline(p.Pipeline, C.int(p.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pipeline) Play() {
|
|
|
|
C.gstreamer_send_play_pipeline(p.Pipeline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pipeline) Stop() {
|
|
|
|
C.gstreamer_send_stop_pipeline(p.Pipeline)
|
|
|
|
}
|
|
|
|
|
|
|
|
// gst-inspect-1.0
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("required gstreamer plugin %s not found", pluginstr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//export goHandlePipelineBuffer
|
|
|
|
func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.int, pipelineID C.int) {
|
2021-01-23 06:13:32 +13:00
|
|
|
defer C.free(buffer)
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
pipelinesLock.Lock()
|
|
|
|
pipeline, ok := pipelines[int(pipelineID)]
|
|
|
|
pipelinesLock.Unlock()
|
|
|
|
|
|
|
|
if ok {
|
2021-01-23 06:13:32 +13:00
|
|
|
pipeline.Sample <- types.Sample{
|
2021-02-15 02:40:17 +13:00
|
|
|
Data: C.GoBytes(buffer, bufferLen),
|
2021-02-02 11:50:18 +13:00
|
|
|
Duration: time.Duration(duration),
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
} else {
|
|
|
|
fmt.Printf("discarding buffer, no pipeline with id %d", int(pipelineID))
|
|
|
|
}
|
|
|
|
}
|