mirror of
https://github.com/pion/webrtc.git
synced 2025-10-26 00:30:35 +08:00
92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/pions/webrtc"
|
|
|
|
gst "github.com/pions/webrtc/examples/internal/gstreamer-src"
|
|
"github.com/pions/webrtc/examples/internal/signal"
|
|
)
|
|
|
|
func main() {
|
|
// 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)
|
|
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.NewSampleTrack(webrtc.DefaultPayloadTypeOpus, "audio", "pion1")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
_, err = peerConnection.AddTrack(opusTrack)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Create a video track
|
|
vp8Track, err := peerConnection.NewSampleTrack(webrtc.DefaultPayloadTypeVP8, "video", "pion2")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
_, err = peerConnection.AddTrack(vp8Track)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Create an offer to send to the browser
|
|
offer, err := peerConnection.CreateOffer(nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Sets the LocalDescription, and starts our UDP listeners
|
|
err = peerConnection.SetLocalDescription(offer)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Output the offer in base64 so we can paste it in browser
|
|
fmt.Println(signal.Encode(offer))
|
|
|
|
// Wait for the answer to be pasted
|
|
answer := webrtc.SessionDescription{}
|
|
signal.Decode(signal.MustReadStdin(), &answer)
|
|
|
|
// Set the remote SessionDescription
|
|
err = peerConnection.SetRemoteDescription(answer)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Start pushing buffers on these tracks
|
|
gst.CreatePipeline(webrtc.Opus, opusTrack.Samples, "audiotestsrc").Start()
|
|
gst.CreatePipeline(webrtc.VP8, vp8Track.Samples, "videotestsrc").Start()
|
|
|
|
// Block forever
|
|
select {}
|
|
}
|