This repository has been archived by the owner on May 26, 2022. It is now read-only.
/
recorder.go
224 lines (194 loc) · 5.1 KB
/
recorder.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package recorder
import (
"fmt"
"sync"
"time"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/livekit-recorder/pkg/config"
"github.com/livekit/livekit-recorder/pkg/display"
"github.com/livekit/livekit-recorder/pkg/pipeline"
)
type Recorder struct {
ID string
conf *config.Config
req *livekit.StartRecordingRequest
display *display.Display
pipeline *pipeline.Pipeline
abort chan struct{}
isTemplate bool
url string
filename string
filepath string
// result info
mu sync.Mutex
result *livekit.RecordingInfo
startedAt map[string]time.Time
}
func NewRecorder(conf *config.Config, recordingID string) *Recorder {
return &Recorder{
ID: recordingID,
conf: conf,
abort: make(chan struct{}),
result: &livekit.RecordingInfo{
Id: recordingID,
},
startedAt: make(map[string]time.Time),
}
}
// Run blocks until completion
func (r *Recorder) Run() *livekit.RecordingInfo {
var err error
// check for request
if r.req == nil {
r.result.Error = "recorder not initialized"
return r.result
}
// launch display
r.display, err = display.Launch(r.conf, r.url, r.req.Options, r.isTemplate)
if err != nil {
logger.Errorw("error launching display", err)
r.result.Error = err.Error()
return r.result
}
// create pipeline
r.pipeline, err = r.createPipeline(r.req)
if err != nil {
logger.Errorw("error building pipeline", err)
r.result.Error = err.Error()
return r.result
}
// if using template, listen for START_RECORDING and END_RECORDING messages
if r.isTemplate {
logger.Infow("Waiting for room to start")
select {
case <-r.display.RoomStarted():
logger.Infow("Room started")
case <-r.abort:
r.pipeline.Abort()
logger.Infow("Recording aborted while waiting for room")
r.result.Error = "Recording aborted"
return r.result
}
// stop on END_RECORDING console log
go func(d *display.Display) {
<-d.RoomEnded()
r.Stop()
}(r.display)
}
var startedAt time.Time
go func() {
r.mu.Lock()
defer r.mu.Unlock()
startedAt = r.pipeline.GetStartTime()
switch output := r.req.Output.(type) {
case *livekit.StartRecordingRequest_Rtmp:
for _, url := range output.Rtmp.Urls {
r.startedAt[url] = startedAt
}
}
}()
// run pipeline
err = r.pipeline.Run()
if err != nil {
logger.Errorw("error running pipeline", err)
r.result.Error = err.Error()
return r.result
}
switch r.req.Output.(type) {
case *livekit.StartRecordingRequest_Rtmp:
for url, startTime := range r.startedAt {
r.result.Rtmp = append(r.result.Rtmp, &livekit.RtmpResult{
StreamUrl: url,
Duration: time.Since(startTime).Milliseconds() / 1000,
})
}
case *livekit.StartRecordingRequest_Filepath:
r.result.File = &livekit.FileResult{
Duration: time.Since(startedAt).Milliseconds() / 1000,
}
if r.conf.FileOutput.S3 != nil {
if err = r.uploadS3(); err != nil {
r.result.Error = err.Error()
return r.result
}
r.result.File.DownloadUrl = fmt.Sprintf("s3://%s/%s", r.conf.FileOutput.S3.Bucket, r.filepath)
} else if r.conf.FileOutput.Azblob != nil {
if err = r.uploadAzure(); err != nil {
r.result.Error = err.Error()
return r.result
}
r.result.File.DownloadUrl = fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s",
r.conf.FileOutput.Azblob.AccountName,
r.conf.FileOutput.Azblob.ContainerName,
r.filepath)
} else if r.conf.FileOutput.GCPConfig != nil {
if err = r.uploadGCP(); err != nil {
r.result.Error = err.Error()
return r.result
}
r.result.File.DownloadUrl = fmt.Sprintf("gs://%s/%s", r.conf.FileOutput.GCPConfig.Bucket, r.filepath)
}
}
return r.result
}
func (r *Recorder) createPipeline(req *livekit.StartRecordingRequest) (*pipeline.Pipeline, error) {
switch output := req.Output.(type) {
case *livekit.StartRecordingRequest_Rtmp:
return pipeline.NewRtmpPipeline(output.Rtmp.Urls, req.Options)
case *livekit.StartRecordingRequest_Filepath:
return pipeline.NewFilePipeline(r.filename, req.Options)
}
return nil, ErrNoOutput
}
func (r *Recorder) AddOutput(url string) error {
logger.Debugw("add output", "url", url)
if r.pipeline == nil {
return pipeline.ErrPipelineNotFound
}
if err := r.pipeline.AddOutput(url); err != nil {
return err
}
startedAt := time.Now()
r.mu.Lock()
r.startedAt[url] = startedAt
r.mu.Unlock()
return nil
}
func (r *Recorder) RemoveOutput(url string) error {
logger.Debugw("remove output", "url", url)
if r.pipeline == nil {
return pipeline.ErrPipelineNotFound
}
if err := r.pipeline.RemoveOutput(url); err != nil {
return err
}
endedAt := time.Now()
r.mu.Lock()
if startedAt, ok := r.startedAt[url]; ok {
r.result.Rtmp = append(r.result.Rtmp, &livekit.RtmpResult{
StreamUrl: url,
Duration: endedAt.Sub(startedAt).Milliseconds() / 1000,
})
delete(r.startedAt, url)
}
r.mu.Unlock()
return nil
}
func (r *Recorder) Stop() {
select {
case <-r.abort:
return
default:
close(r.abort)
if p := r.pipeline; p != nil {
p.Close()
}
}
}
// should only be called after pipeline completes
func (r *Recorder) Close() {
if r.display != nil {
r.display.Close()
}
}