Files
webrtc/examples/gstreamer-send/main.go
Sean DuBois 6aeb3425b0 Move to new Track API
See v2.0.0 Release Notes[0] for all changes

Resolves #405

[0] https://github.com/pions/webrtc/wiki/v2.0.0-Release-Notes#media-api
2019-02-25 23:44:09 -08:00

94 lines
2.3 KiB
Go

package main
import (
"flag"
"fmt"
"math/rand"
"github.com/pions/webrtc"
gst "github.com/pions/webrtc/examples/internal/gstreamer-src"
"github.com/pions/webrtc/examples/internal/signal"
)
func main() {
audioSrc := flag.String("audio-src", "audiotestsrc", "GStreamer audio src")
videoSrc := flag.String("video-src", "videotestsrc", "GStreamer video src")
flag.Parse()
// Everything below is the pion-WebRTC API! Thanks for using it ❤️.
// Prepare the configuration
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
// Create a new RTCPeerConnection
peerConnection, err := webrtc.NewPeerConnection(config)
if err != nil {
panic(err)
}
// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
})
// Create a audio track
opusTrack, err := peerConnection.NewTrack(webrtc.DefaultPayloadTypeOpus, rand.Uint32(), "audio", "pion1")
if err != nil {
panic(err)
}
_, err = peerConnection.AddTrack(opusTrack)
if err != nil {
panic(err)
}
// Create a video track
vp8Track, err := peerConnection.NewTrack(webrtc.DefaultPayloadTypeVP8, rand.Uint32(), "video", "pion2")
if err != nil {
panic(err)
}
_, err = peerConnection.AddTrack(vp8Track)
if err != nil {
panic(err)
}
// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
signal.Decode(signal.MustReadStdin(), &offer)
// Set the remote SessionDescription
err = peerConnection.SetRemoteDescription(offer)
if err != nil {
panic(err)
}
// Create an answer
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
panic(err)
}
// Sets the LocalDescription, and starts our UDP listeners
err = peerConnection.SetLocalDescription(answer)
if err != nil {
panic(err)
}
// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(answer))
// Start pushing buffers on these tracks
gst.CreatePipeline(webrtc.Opus, opusTrack, *audioSrc).Start()
gst.CreatePipeline(webrtc.VP8, vp8Track, *videoSrc).Start()
// Block forever
select {}
}