Skip to content

Commit 55eb746

Browse files
committed
Add support for PCM in GStreamer
You can switch Opus to PCMU or PCMA and everything works! Relates to pion/webrtc#851
1 parent e187587 commit 55eb746

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module github.com/pion/example-webrtc-applications

internal/gstreamer-src/gst.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,52 @@ type Pipeline struct {
2626
tracks []*webrtc.Track
2727
id int
2828
codecName string
29+
clockRate float32
2930
}
3031

3132
var pipelines = make(map[int]*Pipeline)
3233
var pipelinesLock sync.Mutex
3334

35+
const (
36+
videoClockRate = 90000
37+
audioClockRate = 48000
38+
pcmClockRate = 8000
39+
)
40+
3441
// CreatePipeline creates a GStreamer Pipeline
3542
func CreatePipeline(codecName string, tracks []*webrtc.Track, pipelineSrc string) *Pipeline {
3643
pipelineStr := "appsink name=appsink"
44+
var clockRate float32
45+
3746
switch codecName {
3847
case webrtc.VP8:
3948
pipelineStr = pipelineSrc + " ! vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1 ! " + pipelineStr
49+
clockRate = videoClockRate
50+
4051
case webrtc.VP9:
4152
pipelineStr = pipelineSrc + " ! vp9enc ! " + pipelineStr
53+
clockRate = videoClockRate
54+
4255
case webrtc.H264:
4356
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+
4459
case webrtc.Opus:
4560
pipelineStr = pipelineSrc + " ! opusenc ! " + pipelineStr
61+
clockRate = audioClockRate
62+
4663
case webrtc.G722:
4764
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+
4875
default:
4976
panic("Unhandled codec " + codecName)
5077
}
@@ -60,6 +87,7 @@ func CreatePipeline(codecName string, tracks []*webrtc.Track, pipelineSrc string
6087
tracks: tracks,
6188
id: len(pipelines),
6289
codecName: codecName,
90+
clockRate: clockRate,
6391
}
6492

6593
pipelines[pipeline.id] = pipeline
@@ -76,24 +104,14 @@ func (p *Pipeline) Stop() {
76104
C.gstreamer_send_stop_pipeline(p.Pipeline)
77105
}
78106

79-
const (
80-
videoClockRate = 90000
81-
audioClockRate = 48000
82-
)
83-
84107
//export goHandlePipelineBuffer
85108
func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.int, pipelineID C.int) {
86109
pipelinesLock.Lock()
87110
pipeline, ok := pipelines[int(pipelineID)]
88111
pipelinesLock.Unlock()
89112

90113
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))
97115
for _, t := range pipeline.tracks {
98116
if err := t.WriteSample(media.Sample{Data: C.GoBytes(buffer, bufferLen), Samples: samples}); err != nil {
99117
panic(err)

0 commit comments

Comments
 (0)