mirror of
https://github.com/pion/webrtc.git
synced 2025-11-03 09:40:59 +08:00
Resolves #218 Change Event Callback APIs to setter functions which take care of locking so that users don't need to know about or remember to do this.
75 lines
2.0 KiB
Go
75 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/pions/webrtc"
|
|
"github.com/pions/webrtc/examples/gstreamer-send/gst"
|
|
"github.com/pions/webrtc/examples/util"
|
|
"github.com/pions/webrtc/pkg/ice"
|
|
)
|
|
|
|
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.RTCConfiguration{
|
|
IceServers: []webrtc.RTCIceServer{
|
|
{
|
|
URLs: []string{"stun:stun.l.google.com:19302"},
|
|
},
|
|
},
|
|
}
|
|
|
|
// Create a new RTCPeerConnection
|
|
peerConnection, err := webrtc.New(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.NewRTCSampleTrack(webrtc.DefaultPayloadTypeOpus, "audio", "pion1")
|
|
util.Check(err)
|
|
_, err = peerConnection.AddTrack(opusTrack)
|
|
util.Check(err)
|
|
|
|
// Create a video track
|
|
vp8Track, err := peerConnection.NewRTCSampleTrack(webrtc.DefaultPayloadTypeVP8, "video", "pion2")
|
|
util.Check(err)
|
|
_, err = peerConnection.AddTrack(vp8Track)
|
|
util.Check(err)
|
|
|
|
// Create an offer to send to the browser
|
|
offer, err := peerConnection.CreateOffer(nil)
|
|
util.Check(err)
|
|
|
|
// Output the offer in base64 so we can paste it in browser
|
|
fmt.Println(util.Encode(offer.Sdp))
|
|
|
|
// Wait for the answer to be pasted
|
|
sd := util.Decode(util.MustReadStdin())
|
|
|
|
// Set the remote SessionDescription
|
|
answer := webrtc.RTCSessionDescription{
|
|
Type: webrtc.RTCSdpTypeAnswer,
|
|
Sdp: sd,
|
|
}
|
|
err = peerConnection.SetRemoteDescription(answer)
|
|
util.Check(err)
|
|
|
|
// Start pushing buffers on these tracks
|
|
gst.CreatePipeline(webrtc.Opus, opusTrack.Samples).Start()
|
|
gst.CreatePipeline(webrtc.VP8, vp8Track.Samples).Start()
|
|
|
|
// Block forever
|
|
select {}
|
|
}
|