Files
mediadevices/examples/simple/main.go
2020-01-05 22:12:44 -08:00

80 lines
1.9 KiB
Go

package main
import (
"fmt"
"github.com/pion/mediadevices"
"github.com/pion/mediadevices/examples/internal/signal"
_ "github.com/pion/mediadevices/pkg/codec/h264" // This is required to register h264 video encoder
"github.com/pion/webrtc/v2"
)
func main() {
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())
})
mediaDevices := mediadevices.NewMediaDevices(peerConnection)
s, err := mediaDevices.GetUserMedia(mediadevices.MediaStreamConstraints{
Video: mediadevices.VideoTrackConstraints{
Enabled: true,
Width: 800, // Optional. This is just an ideal value.
Height: 480, // Optional. This is just an ideal value.
Codec: webrtc.H264,
},
})
if err != nil {
panic(err)
}
for _, tracker := range s.GetTracks() {
_, err = peerConnection.AddTrack(tracker.Track())
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))
select {}
}