package gst
import "C"
import (
"fmt"
"io"
"sync"
"unsafe"
"github.com/pion/webrtc/v2"
"github.com/pion/webrtc/v2/pkg/media"
)
func init() {
go C.gstreamer_send_start_mainloop()
}
type Pipeline struct {
Pipeline *C.GstElement
audioTrack *webrtc.Track
videoTrack *webrtc.Track
}
var pipeline = &Pipeline{}
var pipelinesLock sync.Mutex
var pipelineFmtStr = `
filesrc location="%s"
! decodebin name=demux
demux.
! queue
! videoconvert
! videoscale add-borders=1
! video/x-raw,width=1024,pixel-aspect-ratio=1/1
! vp8enc deadline=1
! appsink name=video
demux.
! queue
! audioconvert
! audioresample
! audio/x-raw,rate=48000
! opusenc bitrate=128000
! appsink name=audio
`
func CreatePipeline(containerPath string, audioTrack, videoTrack *webrtc.Track) *Pipeline {
pipelineStr := fmt.Sprintf(pipelineFmtStr, containerPath)
pipelineStrUnsafe := C.CString(pipelineStr)
defer C.free(unsafe.Pointer(pipelineStrUnsafe))
pipelinesLock.Lock()
defer pipelinesLock.Unlock()
pipeline = &Pipeline{
Pipeline: C.gstreamer_send_create_pipeline(pipelineStrUnsafe),
audioTrack: audioTrack,
videoTrack: videoTrack,
}
return pipeline
}
func (p *Pipeline) Start() {
C.gstreamer_send_start_pipeline(p.Pipeline)
}
func (p *Pipeline) Play() {
C.gstreamer_send_play_pipeline(p.Pipeline)
}
func (p *Pipeline) Pause() {
C.gstreamer_send_pause_pipeline(p.Pipeline)
}
func (p *Pipeline) SeekToTime(seekPos int64) {
C.gstreamer_send_seek(p.Pipeline, C.int64_t(seekPos))
}
const (
videoClockRate = 90000
audioClockRate = 48000
)
func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.int, isVideo C.int) {
var track *webrtc.Track
var samples uint32
if isVideo == 1 {
samples = uint32(videoClockRate * (float32(duration) / 1000000000))
track = pipeline.videoTrack
} else {
samples = uint32(audioClockRate * (float32(duration) / 1000000000))
track = pipeline.audioTrack
}
if err := track.WriteSample(media.Sample{Data: C.GoBytes(buffer, bufferLen), Samples: samples}); err != nil && err != io.ErrClosedPipe {
panic(err)
}
C.free(buffer)
}