2020-04-06 06:45:47 +00:00
|
|
|
package webtorrent
|
2020-04-05 03:55:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pion/datachannel"
|
|
|
|
|
|
|
|
"github.com/pion/webrtc/v2"
|
|
|
|
)
|
|
|
|
|
2020-04-07 02:17:21 +00:00
|
|
|
var (
|
|
|
|
api = func() *webrtc.API {
|
2020-04-13 04:31:39 +00:00
|
|
|
// Enable the detach API (since it's non-standard but more idiomatic).
|
2020-04-07 02:17:21 +00:00
|
|
|
s := webrtc.SettingEngine{}
|
|
|
|
s.DetachDataChannels()
|
|
|
|
return webrtc.NewAPI(webrtc.WithSettingEngine(s))
|
|
|
|
}()
|
|
|
|
config = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}}
|
|
|
|
newPeerConnectionMu sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
func newPeerConnection() (*webrtc.PeerConnection, error) {
|
|
|
|
newPeerConnectionMu.Lock()
|
|
|
|
defer newPeerConnectionMu.Unlock()
|
|
|
|
return api.NewPeerConnection(config)
|
|
|
|
}
|
|
|
|
|
2020-04-13 04:31:39 +00:00
|
|
|
type transport struct {
|
2020-04-05 03:55:14 +00:00
|
|
|
pc *webrtc.PeerConnection
|
|
|
|
dc *webrtc.DataChannel
|
|
|
|
|
2020-04-07 02:17:21 +00:00
|
|
|
lock sync.Mutex
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 04:31:39 +00:00
|
|
|
// newTransport creates a transport and returns a WebRTC offer to be announced
|
|
|
|
func newTransport() (*transport, webrtc.SessionDescription, error) {
|
2020-04-07 02:17:21 +00:00
|
|
|
peerConnection, err := newPeerConnection()
|
2020-04-05 03:55:14 +00:00
|
|
|
if err != nil {
|
2020-04-14 00:47:26 +00:00
|
|
|
return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to peer connection: %w", err)
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
dataChannel, err := peerConnection.CreateDataChannel("webrtc-datachannel", nil)
|
|
|
|
if err != nil {
|
2020-04-14 00:47:26 +00:00
|
|
|
return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to data channel: %w", err)
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
|
2020-04-14 00:47:26 +00:00
|
|
|
//fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
|
2020-04-05 03:55:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
dataChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
|
2020-04-14 00:47:26 +00:00
|
|
|
//fmt.Printf("Message from DataChannel '%s': '%s'\n", dataChannel.Label(), string(msg.Data))
|
2020-04-05 03:55:14 +00:00
|
|
|
})
|
|
|
|
offer, err := peerConnection.CreateOffer(nil)
|
|
|
|
if err != nil {
|
2020-04-14 00:47:26 +00:00
|
|
|
return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to create offer: %w", err)
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
err = peerConnection.SetLocalDescription(offer)
|
|
|
|
if err != nil {
|
2020-04-14 00:47:26 +00:00
|
|
|
return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to set local description: %w", err)
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 04:31:39 +00:00
|
|
|
t := &transport{pc: peerConnection, dc: dataChannel}
|
2020-04-05 03:55:14 +00:00
|
|
|
return t, offer, nil
|
|
|
|
}
|
|
|
|
|
2020-04-13 04:31:39 +00:00
|
|
|
// newTransportFromOffer creates a transport from a WebRTC offer and and returns a WebRTC answer to
|
2020-04-07 02:17:21 +00:00
|
|
|
// be announced.
|
2020-04-13 04:31:39 +00:00
|
|
|
func newTransportFromOffer(offer webrtc.SessionDescription, onOpen onDataChannelOpen, offerId string) (*transport, webrtc.SessionDescription, error) {
|
2020-04-07 02:17:21 +00:00
|
|
|
peerConnection, err := newPeerConnection()
|
2020-04-05 03:55:14 +00:00
|
|
|
if err != nil {
|
2020-04-14 00:47:26 +00:00
|
|
|
return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to peer connection: %w", err)
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
|
2020-04-14 00:47:26 +00:00
|
|
|
//fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
|
2020-04-05 03:55:14 +00:00
|
|
|
})
|
|
|
|
|
2020-04-13 04:31:39 +00:00
|
|
|
t := &transport{pc: peerConnection}
|
2020-04-05 03:55:14 +00:00
|
|
|
|
|
|
|
err = peerConnection.SetRemoteDescription(offer)
|
|
|
|
if err != nil {
|
2020-04-14 00:47:26 +00:00
|
|
|
return nil, webrtc.SessionDescription{}, err
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
answer, err := peerConnection.CreateAnswer(nil)
|
|
|
|
if err != nil {
|
2020-04-14 00:47:26 +00:00
|
|
|
return nil, webrtc.SessionDescription{}, err
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
2020-04-07 04:30:27 +00:00
|
|
|
peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
|
2020-04-14 00:47:26 +00:00
|
|
|
//fmt.Printf("New DataChannel %s %d\n", d.Label(), d.ID())
|
2020-04-07 04:30:27 +00:00
|
|
|
t.lock.Lock()
|
|
|
|
t.dc = d
|
|
|
|
t.lock.Unlock()
|
|
|
|
t.handleOpen(func(dc datachannel.ReadWriteCloser) {
|
2020-04-13 04:04:34 +00:00
|
|
|
onOpen(dc, DataChannelContext{answer, offer, offerId, false})
|
2020-04-07 04:30:27 +00:00
|
|
|
})
|
|
|
|
})
|
2020-04-05 03:55:14 +00:00
|
|
|
err = peerConnection.SetLocalDescription(answer)
|
|
|
|
if err != nil {
|
2020-04-14 00:47:26 +00:00
|
|
|
return nil, webrtc.SessionDescription{}, err
|
2020-04-05 03:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return t, answer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAnswer sets the WebRTC answer
|
2020-04-13 04:31:39 +00:00
|
|
|
func (t *transport) SetAnswer(answer webrtc.SessionDescription, onOpen func(datachannel.ReadWriteCloser)) error {
|
2020-04-05 03:55:14 +00:00
|
|
|
t.handleOpen(onOpen)
|
|
|
|
|
|
|
|
err := t.pc.SetRemoteDescription(answer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-13 04:31:39 +00:00
|
|
|
func (t *transport) handleOpen(onOpen func(datachannel.ReadWriteCloser)) {
|
2020-04-05 03:55:14 +00:00
|
|
|
t.lock.Lock()
|
|
|
|
dc := t.dc
|
|
|
|
t.lock.Unlock()
|
|
|
|
dc.OnOpen(func() {
|
2020-04-14 00:47:26 +00:00
|
|
|
//fmt.Printf("Data channel '%s'-'%d' open.\n", dc.Label(), dc.ID())
|
2020-04-05 03:55:14 +00:00
|
|
|
|
|
|
|
// Detach the data channel
|
|
|
|
raw, err := dc.Detach()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to detach: %v", err) // TODO: Error handling
|
|
|
|
}
|
|
|
|
|
|
|
|
onOpen(raw)
|
|
|
|
})
|
|
|
|
}
|