-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
144 lines (125 loc) · 3.49 KB
/
server.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package server
import (
"errors"
"gitlab.lrz.de/cm/nms-whep-exercise/server/vp8decoder"
"log/slog"
"math/rand"
"strings"
"time"
"github.com/google/uuid"
"github.com/puzpuzpuz/xsync/v3"
)
type Sample struct {
Payload []byte
Timestamp time.Time
Duration time.Duration
}
type SampleWriter interface {
WriteSample(Sample) error
}
type Publisher interface {
Publish(publication, track, kind string) (SampleWriter, error)
Unpublish(publication string)
}
type Publication interface {
Subscribe(string, SampleWriter)
Unsubscribe(string)
Kind() string
}
type Peer interface {
Signal(string) (string, error)
AcceptTracks() error
AddTrack(Publication) error
}
type PeerFactory interface {
Create(name, resource, pipeline string, p Publisher, fc *vp8decoder.FrameContainer) (peer Peer, err error)
}
type OnDemandSource interface {
Play() error
}
type OnDemandFactory interface {
Open(p Publisher, location, resource string) (OnDemandSource, error)
}
type publicationName string
type trackName string
type Server struct {
publishers *xsync.MapOf[publicationName, *xsync.MapOf[trackName, Publication]]
pf PeerFactory
of OnDemandFactory
rn *randomNameGenerator
logger *slog.Logger
fc *vp8decoder.FrameContainer
}
func New(pf PeerFactory, fc *vp8decoder.FrameContainer) *Server {
return &Server{
publishers: xsync.NewMapOf[publicationName, *xsync.MapOf[trackName, Publication]](),
pf: pf,
rn: newRandomNameGenerator(),
logger: slog.Default().WithGroup("SERVER"),
fc: fc,
}
}
func (s *Server) Publish(resource, track, kind string) (SampleWriter, error) {
s.logger.Info("saving track for publisher", "publisher", resource, "track", track)
pub, ok := s.publishers.Load(publicationName(resource))
if !ok {
s.logger.Warn("publisher not found", "publisher", resource)
return nil, errors.New("publisher not found")
}
writer := newTrack(kind)
_, ok = pub.LoadOrStore(trackName(track), writer)
if ok {
s.logger.Warn("track name already in use", "track", track)
return nil, errors.New("track name already in use")
}
return writer, nil
}
func (s *Server) Unpublish(resource string) {
s.publishers.Delete(publicationName(resource))
}
func (s *Server) Handle(sd SessionDescription) (SessionDescription, error) {
uuid, err := uuid.NewRandom()
if err != nil {
return SessionDescription{}, err
}
location := uuid.String()
p, err := s.pf.Create(location, sd.ResourceName, sd.PipelineName, s, s.fc)
if err != nil {
return SessionDescription{}, err
}
if sd.Method == WHIP {
s.logger.Info("storing whip peer", "location", location, "resource", publicationName(sd.ResourceName))
s.publishers.LoadOrStore(publicationName(sd.ResourceName), xsync.NewMapOf[trackName, Publication]())
if err = p.AcceptTracks(); err != nil {
return SessionDescription{}, err
}
}
answer, err := p.Signal(sd.SDP)
if err != nil {
return SessionDescription{}, err
}
return SessionDescription{
ResourceName: location,
SDP: answer,
Method: sd.Method,
}, nil
}
type randomNameGenerator struct {
alphabet []rune
random *rand.Rand
}
func newRandomNameGenerator() *randomNameGenerator {
return &randomNameGenerator{
alphabet: []rune("abcdefghijklmnopqrstuvwxyz"),
random: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
func (g *randomNameGenerator) Generate() string {
size := len(g.alphabet)
var sb strings.Builder
for i := 0; i < 6; i++ {
ch := g.alphabet[g.random.Intn(size)]
sb.WriteRune(ch)
}
return sb.String()
}