This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_method.go
80 lines (68 loc) · 2.06 KB
/
signal_method.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
79
80
package transportc
import (
"errors"
)
type SignalMethod interface {
// MakeOffer sends the SDP offer generated by offerer to the answerer.
//
// Called by OFFERER (#1)
MakeOffer(offer []byte) error
// GetOffer SHOULD return an error if Offer is not yet received/available.
// However, blocking until an offer is received is also acceptable.
//
// Called by ANSWERER (#2)
GetOffer() ([]byte, error)
// Answer sends the SDP answer generated by answerer to the offerer.
//
// Called by ANSWERER (#3)
Answer(answer []byte) error
// GetAnswer blocks until the next SDP answer is available.
//
// Called by OFFERER (#4)
GetAnswer() ([]byte, error)
}
var (
// SignalMethodManual won't automatically establish a connection.
// The connection will be established once both party have called SetRemoteDescription.
SignalMethodManual SignalMethod = nil
)
// IOSignal implements a minimalistic signaling method.
// Offerer's writer should write to the answerer's reader and vice versa.
type DebugSignal struct {
offers chan []byte
answers chan []byte
}
// NewDebugSignal creates a new DebugSignal.
func NewDebugSignal(bufferSize int) *DebugSignal {
return &DebugSignal{
offers: make(chan []byte, bufferSize),
answers: make(chan []byte, bufferSize),
}
}
// MakeOffer implements SignalMethod.MakeOffer.
// It writes the SDP offer to offers channel.
func (ds *DebugSignal) MakeOffer(offer []byte) error {
ds.offers <- offer
return nil
}
// GetOffer implements SignalMethod.GetOffer
// It reads the SDP offer from offers channel.
func (ds *DebugSignal) GetOffer() ([]byte, error) {
if len(ds.offers) == 0 {
return nil, errors.New("no offer available")
}
offer := <-ds.offers
return offer, nil
}
// Answer implements SignalMethod.Answer.
// It writes the SDP answer to answers channel.
func (ds *DebugSignal) Answer(answer []byte) error {
ds.answers <- answer
return nil
}
// GetAnswer implements SignalMethod.GetAnswer
// It reads the SDP answer from answers channel.
func (ds *DebugSignal) GetAnswer() ([]byte, error) {
answer := <-ds.answers
return answer, nil
}