@@ -11,26 +11,25 @@ import (
11
11
"flag"
12
12
"fmt"
13
13
14
- gst "github.com/pion/example-webrtc-applications/v3/internal/gstreamer-src"
14
+ "github.com/go-gst/go-gst/gst"
15
+ "github.com/go-gst/go-gst/gst/app"
15
16
"github.com/pion/example-webrtc-applications/v3/internal/signal"
16
17
"github.com/pion/webrtc/v3"
18
+ "github.com/pion/webrtc/v3/pkg/media"
17
19
)
18
20
19
21
func main () {
20
22
audioSrc := flag .String ("audio-src" , "audiotestsrc" , "GStreamer audio src" )
21
23
videoSrc := flag .String ("video-src" , "videotestsrc" , "GStreamer video src" )
22
24
flag .Parse ()
23
25
26
+ // Initialize GStreamer
27
+ gst .Init (nil )
28
+
24
29
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
25
30
26
31
// Prepare the configuration
27
- config := webrtc.Configuration {
28
- ICEServers : []webrtc.ICEServer {
29
- {
30
- URLs : []string {"stun:stun.l.google.com:19302" },
31
- },
32
- },
33
- }
32
+ config := webrtc.Configuration {}
34
33
35
34
// Create a new RTCPeerConnection
36
35
peerConnection , err := webrtc .NewPeerConnection (config )
@@ -105,9 +104,69 @@ func main() {
105
104
fmt .Println (signal .Encode (* peerConnection .LocalDescription ()))
106
105
107
106
// Start pushing buffers on these tracks
108
- gst . CreatePipeline ("opus" , []* webrtc.TrackLocalStaticSample {audioTrack }, * audioSrc ). Start ( )
109
- gst . CreatePipeline ("vp8" , []* webrtc.TrackLocalStaticSample {firstVideoTrack , secondVideoTrack }, * videoSrc ). Start ( )
107
+ pipelineForCodec ("opus" , []* webrtc.TrackLocalStaticSample {audioTrack }, * audioSrc )
108
+ pipelineForCodec ("vp8" , []* webrtc.TrackLocalStaticSample {firstVideoTrack , secondVideoTrack }, * videoSrc )
110
109
111
110
// Block forever
112
111
select {}
113
112
}
113
+
114
+ // Create the appropriate GStreamer pipeline depending on what codec we are working with
115
+ func pipelineForCodec (codecName string , tracks []* webrtc.TrackLocalStaticSample , pipelineSrc string ) {
116
+ pipelineStr := "appsink name=appsink"
117
+ switch codecName {
118
+ case "vp8" :
119
+ pipelineStr = pipelineSrc + " ! vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1 ! " + pipelineStr
120
+ case "vp9" :
121
+ pipelineStr = pipelineSrc + " ! vp9enc ! " + pipelineStr
122
+ case "h264" :
123
+ pipelineStr = pipelineSrc + " ! video/x-raw,format=I420 ! x264enc speed-preset=ultrafast tune=zerolatency key-int-max=20 ! video/x-h264,stream-format=byte-stream ! " + pipelineStr
124
+ case "opus" :
125
+ pipelineStr = pipelineSrc + " ! opusenc ! " + pipelineStr
126
+ case "pcmu" :
127
+ pipelineStr = pipelineSrc + " ! audio/x-raw, rate=8000 ! mulawenc ! " + pipelineStr
128
+ case "pcma" :
129
+ pipelineStr = pipelineSrc + " ! audio/x-raw, rate=8000 ! alawenc ! " + pipelineStr
130
+ default :
131
+ panic ("Unhandled codec " + codecName ) //nolint
132
+ }
133
+
134
+ pipeline , err := gst .NewPipelineFromString (pipelineStr )
135
+ if err != nil {
136
+ panic (err )
137
+ }
138
+
139
+ if err = pipeline .SetState (gst .StatePlaying ); err != nil {
140
+ panic (err )
141
+ }
142
+
143
+ appSink , err := pipeline .GetElementByName ("appsink" )
144
+ if err != nil {
145
+ panic (err )
146
+ }
147
+
148
+ app .SinkFromElement (appSink ).SetCallbacks (& app.SinkCallbacks {
149
+ NewSampleFunc : func (sink * app.Sink ) gst.FlowReturn {
150
+ sample := sink .PullSample ()
151
+ if sample == nil {
152
+ return gst .FlowEOS
153
+ }
154
+
155
+ buffer := sample .GetBuffer ()
156
+ if buffer == nil {
157
+ return gst .FlowError
158
+ }
159
+
160
+ samples := buffer .Map (gst .MapRead ).Bytes ()
161
+ defer buffer .Unmap ()
162
+
163
+ for _ , t := range tracks {
164
+ if err := t .WriteSample (media.Sample {Data : samples , Duration : * buffer .Duration ().AsDuration ()}); err != nil {
165
+ panic (err ) //nolint
166
+ }
167
+ }
168
+
169
+ return gst .FlowOK
170
+ },
171
+ })
172
+ }
0 commit comments