/
participant.go
150 lines (127 loc) · 3.02 KB
/
participant.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
145
146
147
148
149
150
package participant
import (
"context"
"errors"
"fmt"
"time"
"github.com/cloudgroundcontrol/livekit-recorder/pkg/recorder"
"github.com/cloudgroundcontrol/livekit-recorder/pkg/upload"
"github.com/labstack/gommon/log"
"github.com/lithammer/shortuuid/v4"
lksdk "github.com/livekit/server-sdk-go"
"github.com/livekit/server-sdk-go/pkg/samplebuilder"
"github.com/pion/webrtc/v3"
)
const RecordingsDir = "recordings"
type Participant interface {
GetData() ParticipantData
IsVideoRecordable() bool
IsAudioRecordable() bool
RegisterVideo(track *webrtc.TrackRemote) error
RegisterAudio(track *webrtc.TrackRemote) error
Start()
Stop()
}
type participant struct {
ctx context.Context
data ParticipantData
state state
uploader upload.Uploader
pli lksdk.PLIWriter
// Filenames
vf string
af string
// Tracks
vt *webrtc.TrackRemote
at *webrtc.TrackRemote
// Recorders
vr recorder.Recorder
ar recorder.Recorder
}
func NewParticipant(identity string, uploader upload.Uploader, pli lksdk.PLIWriter) Participant {
return &participant{
ctx: context.TODO(),
data: ParticipantData{
Identity: identity,
},
state: stateCreated,
uploader: uploader,
pli: pli,
}
}
func (p *participant) createRecorder(track *webrtc.TrackRemote) (recorder.Recorder, error) {
fileID := shortuuid.New()
if fileID == "" {
return nil, errors.New("empty file ID")
}
fileExt := recorder.GetMediaExtension(track.Codec().MimeType)
if fileExt == "" {
return nil, errors.New("unsupported media")
}
fileName := fmt.Sprintf("%s/%s.%s", RecordingsDir, fileID, fileExt)
return recorder.New(track.Codec(), fileName, samplebuilder.WithPacketDroppedHandler(func() {
p.pli(track.SSRC())
}))
}
func (p *participant) GetData() ParticipantData {
return p.data
}
func (p *participant) IsVideoRecordable() bool {
return p.vr != nil
}
func (p *participant) IsAudioRecordable() bool {
return p.ar != nil
}
func (p *participant) RegisterVideo(track *webrtc.TrackRemote) error {
r, err := p.createRecorder(track)
if err != nil {
return err
}
p.vf = r.Sink().Name()
p.vt = track
p.vr = r
return nil
}
func (p *participant) RegisterAudio(track *webrtc.TrackRemote) error {
r, err := p.createRecorder(track)
if err != nil {
return err
}
p.af = r.Sink().Name()
p.at = track
p.ar = r
return nil
}
func (p *participant) Start() {
if p.state != stateCreated {
return
}
if p.vt != nil && p.vr != nil {
p.vr.Start(p.ctx, p.vt)
}
if p.at != nil && p.ar != nil {
p.ar.Start(p.ctx, p.at)
}
p.state = stateRecording
p.data.Start = time.Now()
}
func (p *participant) Stop() {
if p.state == stateDone {
return
}
if p.vr != nil {
p.vr.Stop()
}
if p.ar != nil {
p.ar.Stop()
}
p.state = stateDone
p.data.End = time.Now()
// Do post processing
// While it is synchronous, the containerisation should be almost instantenous.
// Meanwhile, uploading is done in the background via goroutine
err := p.process()
if err != nil {
log.Errorf("error in post processing | error: %v, participant: %s", err, p.data.Identity)
}
}