mirror of
https://github.com/pion/webrtc.git
synced 2025-10-05 07:06:51 +08:00

Let's pull off the bandaid! * Reduces studdering: webrtc.RTCTrack -> webrtc.Track * Makes it easier to find types by editor autocomplete * Makes code read more fluently (less repetition) Since we're breaking the API in 2.0, our only chance to do this is now. Relates to #408
81 lines
2.3 KiB
Go
81 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
|
|
"github.com/pions/webrtc"
|
|
"github.com/pions/webrtc/examples/util"
|
|
gst "github.com/pions/webrtc/examples/util/gstreamer-src"
|
|
"github.com/pions/webrtc/pkg/ice"
|
|
)
|
|
|
|
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 ❤️.
|
|
|
|
// Setup the codecs you want to use.
|
|
// We'll use the default ones but you can also define your own
|
|
webrtc.RegisterDefaultCodecs()
|
|
|
|
// 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)
|
|
util.Check(err)
|
|
|
|
// Set the handler for ICE connection state
|
|
// This will notify you when the peer has connected/disconnected
|
|
peerConnection.OnICEConnectionStateChange(func(connectionState ice.ConnectionState) {
|
|
fmt.Printf("Connection State has changed %s \n", connectionState.String())
|
|
})
|
|
|
|
// Create a audio track
|
|
opusTrack, err := peerConnection.NewSampleTrack(webrtc.DefaultPayloadTypeOpus, "audio", "pion1")
|
|
util.Check(err)
|
|
_, err = peerConnection.AddTrack(opusTrack)
|
|
util.Check(err)
|
|
|
|
// Create a video track
|
|
vp8Track, err := peerConnection.NewSampleTrack(webrtc.DefaultPayloadTypeVP8, "video", "pion2")
|
|
util.Check(err)
|
|
_, err = peerConnection.AddTrack(vp8Track)
|
|
util.Check(err)
|
|
|
|
// Wait for the offer to be pasted
|
|
offer := webrtc.SessionDescription{}
|
|
util.Decode(util.MustReadStdin(), &offer)
|
|
|
|
// Set the remote SessionDescription
|
|
err = peerConnection.SetRemoteDescription(offer)
|
|
util.Check(err)
|
|
|
|
// Create an answer
|
|
answer, err := peerConnection.CreateAnswer(nil)
|
|
util.Check(err)
|
|
|
|
// Sets the LocalDescription, and starts our UDP listeners
|
|
err = peerConnection.SetLocalDescription(answer)
|
|
util.Check(err)
|
|
|
|
// Output the answer in base64 so we can paste it in browser
|
|
fmt.Println(util.Encode(answer))
|
|
|
|
// Start pushing buffers on these tracks
|
|
gst.CreatePipeline(webrtc.Opus, opusTrack.Samples, *audioSrc).Start()
|
|
gst.CreatePipeline(webrtc.VP8, vp8Track.Samples, *videoSrc).Start()
|
|
|
|
// Block forever
|
|
select {}
|
|
}
|