Skip to content

Instantly share code, notes, and snippets.

@Sean-Der
Created August 9, 2018 09:27
Show Gist options
  • Save Sean-Der/85533792c1b96c237f1775191640b1a8 to your computer and use it in GitHub Desktop.
Save Sean-Der/85533792c1b96c237f1775191640b1a8 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"encoding/base64"
"fmt"
"io/ioutil"
"math/rand"
"os"
"time"
"github.com/pions/webrtc"
"github.com/pions/webrtc/pkg/ice"
)
func randSeq(n int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[r.Intn(len(letters))]
}
return string(b)
}
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()
// Create a new RTCPeerConnection
peerConnection, err := webrtc.New(webrtc.RTCConfiguration{
ICEServers: []webrtc.RTCICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
})
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 ice.ConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
}
offer, err := peerConnection.CreateOffer(nil)
if err != nil {
panic(err)
}
fmt.Println(base64.StdEncoding.EncodeToString([]byte(offer.Sdp)))
fmt.Println("Press enter to read sdp.txt as the answer")
if _, err := bufio.NewReader(os.Stdin).ReadString('\n'); err != nil {
panic(err)
}
rawSdp, err := ioutil.ReadFile("sdp.txt")
if err != nil {
panic(err)
}
sd, err := base64.StdEncoding.DecodeString(string(rawSdp))
if err != nil {
panic(err)
}
// Set the remote SessionDescription
answer := webrtc.RTCSessionDescription{
Type: webrtc.RTCSdpTypeAnswer,
Sdp: string(sd),
}
if err := peerConnection.SetRemoteDescription(answer); err != nil {
panic(err)
}
select {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment