aboutsummaryrefslogtreecommitdiff
path: root/gst/gst.go
blob: 4bac97365567a987a5014d3d6a86797b3bda050b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package gst
 
/*
#cgo pkg-config: gstreamer-1.0 gstreamer-app-1.0
 
#include "gst.h"
 
*/
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()
}
 
// Pipeline is a wrapper for a GStreamer Pipeline
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
`
 
// CreatePipeline creates a GStreamer Pipeline
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
}
 
// Start starts the GStreamer Pipeline
func (p *Pipeline) Start() {
// This will signal to goHandlePipelineBuffer
// and provide a method for cancelling sends.
C.gstreamer_send_start_pipeline(p.Pipeline)
}
 
// Play sets the pipeline to PLAYING
func (p *Pipeline) Play() {
C.gstreamer_send_play_pipeline(p.Pipeline)
}
 
// Pause sets the pipeline to PAUSED
func (p *Pipeline) Pause() {
C.gstreamer_send_pause_pipeline(p.Pipeline)
}
 
// SeekToTime seeks on the pipeline
func (p *Pipeline) SeekToTime(seekPos int64) {
C.gstreamer_send_seek(p.Pipeline, C.int64_t(seekPos))
}
 
const (
videoClockRate = 90000
audioClockRate = 48000
)
 
//export goHandlePipelineBuffer
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)
}