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

This allows us to demonstrate multi-track easier, without having to worry about encoding multiple times Relates to #54
94 lines
2.3 KiB
Go
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, []*webrtc.Track{opusTrack}, *audioSrc).Start()
|
|
gst.CreatePipeline(webrtc.VP8, []*webrtc.Track{vp8Track}, *videoSrc).Start()
|
|
|
|
// Block forever
|
|
select {}
|
|
}
|