8 "github.com/pion/datachannel"
10 "github.com/pion/webrtc/v2"
14 api = func() *webrtc.API {
15 // Enable the detach API (since it's non-standard but more idiomatic).
16 s := webrtc.SettingEngine{}
17 s.DetachDataChannels()
18 return webrtc.NewAPI(webrtc.WithSettingEngine(s))
20 config = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}}
21 newPeerConnectionMu sync.Mutex
24 func newPeerConnection() (*webrtc.PeerConnection, error) {
25 newPeerConnectionMu.Lock()
26 defer newPeerConnectionMu.Unlock()
27 return api.NewPeerConnection(config)
30 type transport struct {
31 pc *webrtc.PeerConnection
32 dc *webrtc.DataChannel
37 // newTransport creates a transport and returns a WebRTC offer to be announced
38 func newTransport() (*transport, webrtc.SessionDescription, error) {
39 peerConnection, err := newPeerConnection()
41 return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to peer connection: %v\n", err)
43 dataChannel, err := peerConnection.CreateDataChannel("webrtc-datachannel", nil)
45 return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to data channel: %v\n", err)
47 peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
48 fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
51 dataChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
52 fmt.Printf("Message from DataChannel '%s': '%s'\n", dataChannel.Label(), string(msg.Data))
54 offer, err := peerConnection.CreateOffer(nil)
56 return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to create offer: %v\n", err)
58 err = peerConnection.SetLocalDescription(offer)
60 return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to set local description: %v\n", err)
63 t := &transport{pc: peerConnection, dc: dataChannel}
67 // newTransportFromOffer creates a transport from a WebRTC offer and and returns a WebRTC answer to
69 func newTransportFromOffer(offer webrtc.SessionDescription, onOpen onDataChannelOpen, offerId string) (*transport, webrtc.SessionDescription, error) {
70 peerConnection, err := newPeerConnection()
72 return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to peer connection: %v", err)
74 peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
75 fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
78 t := &transport{pc: peerConnection}
80 err = peerConnection.SetRemoteDescription(offer)
82 return nil, webrtc.SessionDescription{}, fmt.Errorf("%v", err)
84 answer, err := peerConnection.CreateAnswer(nil)
86 return nil, webrtc.SessionDescription{}, fmt.Errorf("%v", err)
88 peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
89 fmt.Printf("New DataChannel %s %d\n", d.Label(), d.ID())
93 t.handleOpen(func(dc datachannel.ReadWriteCloser) {
94 onOpen(dc, DataChannelContext{answer, offer, offerId, false})
97 err = peerConnection.SetLocalDescription(answer)
99 return nil, webrtc.SessionDescription{}, fmt.Errorf("%v", err)
102 return t, answer, nil
105 // SetAnswer sets the WebRTC answer
106 func (t *transport) SetAnswer(answer webrtc.SessionDescription, onOpen func(datachannel.ReadWriteCloser)) error {
109 err := t.pc.SetRemoteDescription(answer)
116 func (t *transport) handleOpen(onOpen func(datachannel.ReadWriteCloser)) {
121 fmt.Printf("Data channel '%s'-'%d' open.\n", dc.Label(), dc.ID())
123 // Detach the data channel
124 raw, err := dc.Detach()
126 log.Fatalf("failed to detach: %v", err) // TODO: Error handling