forked from ionorg/ion-avp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
78 lines (62 loc) · 1.74 KB
/
publisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package avp
import (
log "github.com/pion/ion-log"
"github.com/pion/webrtc/v3"
)
type Publisher struct {
pc *webrtc.PeerConnection
candidates []webrtc.ICECandidateInit
}
// NewPublisher creates a new Publisher
func NewPublisher(cfg WebRTCTransportConfig) (*Publisher, error) {
api := webrtc.NewAPI(webrtc.WithSettingEngine(cfg.setting))
pc, err := api.NewPeerConnection(cfg.configuration)
if err != nil {
log.Errorf("NewPeer error: %v", err)
return nil, errPeerConnectionInitFailed
}
_, err = pc.CreateDataChannel("ion-sfu", &webrtc.DataChannelInit{})
if err != nil {
log.Errorf("error creating data channel: %v", err)
return nil, errPeerConnectionInitFailed
}
return &Publisher{
pc: pc,
}, nil
}
func (p *Publisher) CreateOffer() (webrtc.SessionDescription, error) {
offer, err := p.pc.CreateOffer(nil)
if err != nil {
return webrtc.SessionDescription{}, err
}
err = p.pc.SetLocalDescription(offer)
if err != nil {
return webrtc.SessionDescription{}, err
}
return offer, nil
}
// OnICECandidate handler
func (p *Publisher) OnICECandidate(f func(c *webrtc.ICECandidate)) {
p.pc.OnICECandidate(f)
}
// AddICECandidate to peer connection
func (p *Publisher) AddICECandidate(candidate webrtc.ICECandidateInit) error {
if p.pc.RemoteDescription() != nil {
return p.pc.AddICECandidate(candidate)
}
p.candidates = append(p.candidates, candidate)
return nil
}
// SetRemoteDescription sets the SessionDescription of the remote peer
func (p *Publisher) SetRemoteDescription(desc webrtc.SessionDescription) error {
err := p.pc.SetRemoteDescription(desc)
if err != nil {
log.Errorf("SetRemoteDescription error: %v", err)
return err
}
return nil
}
// Close peer
func (p *Publisher) Close() error {
return p.pc.Close()
}