Skip to content

Instantly share code, notes, and snippets.

@ritwickdey
Created February 20, 2022 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritwickdey/d7d751ff492ab8acfae2431f65867e5f to your computer and use it in GitHub Desktop.
Save ritwickdey/d7d751ff492ab8acfae2431f65867e5f to your computer and use it in GitHub Desktop.
go webrtc sample example
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/pion/webrtc/v3"
)
func main() {
offerSDChan := make(chan webrtc.SessionDescription, 1)
answerSDChan := make(chan webrtc.SessionDescription, 1)
go func() {
conn, err := NewConnection()
panicIf(err)
conn.OnICECandidate(func(i *webrtc.ICECandidate) {
log.Println("Offer:OnICECandidate", i)
})
conn.OnICEConnectionStateChange(func(is webrtc.ICEConnectionState) {
log.Println("Offer:OnICEConnectionStateChange", is)
})
dataChan, err := conn.CreateDataChannel("mydata", &webrtc.DataChannelInit{})
panicIf(err)
dataChan.OnOpen(func() {
log.Println("Offer:Data: Conection opened")
err = dataChan.SendText("hello Guys")
panicIf(err)
})
dataChan.OnError(func(err error) {
log.Println("Offer:data:error", err)
})
dataChan.OnClose(func() {
log.Println("Offer:data:close. Goodbye")
})
dataChan.OnMessage(func(msg webrtc.DataChannelMessage) {
log.Println("Offer:data: Got some message: ")
})
sd, err := conn.CreateOffer(nil)
panicIf(err)
err = conn.SetLocalDescription(sd)
panicIf(err)
log.Println("Offer: Local Set")
log.Println("Offer:GatheringCompletePromise: started.")
<-webrtc.GatheringCompletePromise(conn)
log.Println("Offer:GatheringCompletePromise: done.")
offerSDChan <- *conn.LocalDescription()
log.Println("Offer: Waiting for answer SD")
answerSD := <-answerSDChan
err = conn.SetRemoteDescription(answerSD)
panicIf(err)
log.Println("Offer: remote Set")
}()
go func() {
conn, err := NewConnection()
panicIf(err)
conn.OnICECandidate(func(i *webrtc.ICECandidate) {
log.Println("\tanswer:OnICECandidate", i)
})
conn.OnICEConnectionStateChange(func(is webrtc.ICEConnectionState) {
log.Println("\tanswer:OnICEConnectionStateChange", is)
})
conn.OnDataChannel(func(dc *webrtc.DataChannel) {
log.Println("\tAnswer:data")
dc.OnMessage(func(msg webrtc.DataChannelMessage) {
log.Println("\tAnswer:msg:", string(msg.Data))
})
dc.OnOpen(func() {
log.Println("\tAnswer:Data: Conection opened")
})
dc.OnError(func(err error) {
log.Println("\tAnswer:data:error", err)
})
dc.OnClose(func() {
log.Println("\tAnswer:data:close. Goodbye")
})
})
log.Println("\tAnswer: Waiting for offer SD")
offerSD := <-offerSDChan
err = conn.SetRemoteDescription(offerSD)
log.Println("\tAnswer: remote Set")
ansSD, err := conn.CreateAnswer(nil)
panicIf(err)
err = conn.SetLocalDescription(ansSD)
panicIf(err)
log.Println("\tAnswer: Local Set")
log.Println("\tAnswer:GatheringCompletePromise: started.")
<-webrtc.GatheringCompletePromise(conn)
log.Println("\tAnswer:GatheringCompletePromise: done.")
answerSDChan <- *conn.LocalDescription()
}()
// ctrl+c to exit
setupCloseHandler()
}
func NewConnection() (*webrtc.PeerConnection, error) {
peerConnectionConfig := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{
"stun:stun.l.google.com:19302",
// "stun:stun1.l.google.com:19302",
// "stun:stun2.l.google.com:19302",
// "stun:stun3.l.google.com:19302",
// "stun:stun4.l.google.com:19302",
// "stun:stun01.sipphone.com",
// "stun:stun.ekiga.net",
// "stun:stun.fwdnet.net",
// "stun:stun.ideasip.com",
// "stun:stun.iptel.org",
// "stun:stun.rixtelecom.se",
// "stun:stun.schlund.de",
// "stun:stunserver.org",
// "stun:stun.softjoys.com",
// "stun:stun.voiparound.com",
// "stun:stun.voipbuster.com",
// "stun:stun.voipstunt.com",
// "stun:stun.voxgratia.org",
// "stun:stun.xten.com",
},
},
},
}
return webrtc.NewPeerConnection(peerConnectionConfig)
}
func panicIf(err error) {
if err != nil {
log.Panicln(err)
}
}
func setupCloseHandler() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
os.Exit(0)
}
@ritwickdey
Copy link
Author

ritwickdey commented Feb 20, 2022

Thanks to @hnasr (https://www.youtube.com/watch?v=FExZvpVvYxA - amazing video. Must watch).

I've just converted his javascript code to Golang.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment