@@ -26,25 +26,52 @@ type Pipeline struct {
26
26
tracks []* webrtc.Track
27
27
id int
28
28
codecName string
29
+ clockRate float32
29
30
}
30
31
31
32
var pipelines = make (map [int ]* Pipeline )
32
33
var pipelinesLock sync.Mutex
33
34
35
+ const (
36
+ videoClockRate = 90000
37
+ audioClockRate = 48000
38
+ pcmClockRate = 8000
39
+ )
40
+
34
41
// CreatePipeline creates a GStreamer Pipeline
35
42
func CreatePipeline (codecName string , tracks []* webrtc.Track , pipelineSrc string ) * Pipeline {
36
43
pipelineStr := "appsink name=appsink"
44
+ var clockRate float32
45
+
37
46
switch codecName {
38
47
case webrtc .VP8 :
39
48
pipelineStr = pipelineSrc + " ! vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1 ! " + pipelineStr
49
+ clockRate = videoClockRate
50
+
40
51
case webrtc .VP9 :
41
52
pipelineStr = pipelineSrc + " ! vp9enc ! " + pipelineStr
53
+ clockRate = videoClockRate
54
+
42
55
case webrtc .H264 :
43
56
pipelineStr = pipelineSrc + " ! video/x-raw,format=I420 ! x264enc bframes=0 speed-preset=veryfast key-int-max=60 ! video/x-h264,stream-format=byte-stream ! " + pipelineStr
57
+ clockRate = videoClockRate
58
+
44
59
case webrtc .Opus :
45
60
pipelineStr = pipelineSrc + " ! opusenc ! " + pipelineStr
61
+ clockRate = audioClockRate
62
+
46
63
case webrtc .G722 :
47
64
pipelineStr = pipelineSrc + " ! avenc_g722 ! " + pipelineStr
65
+ clockRate = audioClockRate
66
+
67
+ case webrtc .PCMU :
68
+ pipelineStr = pipelineSrc + " ! audio/x-raw, rate=8000 ! mulawenc ! " + pipelineStr
69
+ clockRate = pcmClockRate
70
+
71
+ case webrtc .PCMA :
72
+ pipelineStr = pipelineSrc + " ! audio/x-raw, rate=8000 ! alawenc ! " + pipelineStr
73
+ clockRate = pcmClockRate
74
+
48
75
default :
49
76
panic ("Unhandled codec " + codecName )
50
77
}
@@ -60,6 +87,7 @@ func CreatePipeline(codecName string, tracks []*webrtc.Track, pipelineSrc string
60
87
tracks : tracks ,
61
88
id : len (pipelines ),
62
89
codecName : codecName ,
90
+ clockRate : clockRate ,
63
91
}
64
92
65
93
pipelines [pipeline .id ] = pipeline
@@ -76,24 +104,14 @@ func (p *Pipeline) Stop() {
76
104
C .gstreamer_send_stop_pipeline (p .Pipeline )
77
105
}
78
106
79
- const (
80
- videoClockRate = 90000
81
- audioClockRate = 48000
82
- )
83
-
84
107
//export goHandlePipelineBuffer
85
108
func goHandlePipelineBuffer (buffer unsafe.Pointer , bufferLen C.int , duration C.int , pipelineID C.int ) {
86
109
pipelinesLock .Lock ()
87
110
pipeline , ok := pipelines [int (pipelineID )]
88
111
pipelinesLock .Unlock ()
89
112
90
113
if ok {
91
- var samples uint32
92
- if pipeline .codecName == webrtc .Opus {
93
- samples = uint32 (audioClockRate * (float32 (duration ) / 1000000000 ))
94
- } else {
95
- samples = uint32 (videoClockRate * (float32 (duration ) / 1000000000 ))
96
- }
114
+ samples := uint32 (pipeline .clockRate * (float32 (duration ) / 1000000000 ))
97
115
for _ , t := range pipeline .tracks {
98
116
if err := t .WriteSample (media.Sample {Data : C .GoBytes (buffer , bufferLen ), Samples : samples }); err != nil {
99
117
panic (err )
0 commit comments