/
recorder.go
196 lines (169 loc) · 3.9 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
package recorder
import (
"image"
"io"
"math/rand"
"os"
"path/filepath"
"regexp"
"strconv"
"sync"
"time"
"github.com/giongto35/cloud-game/v3/pkg/logger"
oss "github.com/giongto35/cloud-game/v3/pkg/os"
)
type Recording struct {
sync.Mutex
enabled bool
audio audioStream
video videoStream
dir string
saveDir string
meta Meta
opts Options
log *logger.Logger
vsync []time.Duration
}
// naming regexp
var (
reDate = regexp.MustCompile(`%date:(.*?)%`)
reUser = regexp.MustCompile(`%user%`)
reGame = regexp.MustCompile(`%game%`)
reRand = regexp.MustCompile(`%rand:(\d+)%`)
)
// stream represent an output stream of the recording.
type stream interface {
io.Closer
}
type audioStream interface {
stream
Write(data Audio)
}
type videoStream interface {
stream
Write(data Video)
}
type (
Audio struct {
Samples *[]int16
Duration time.Duration
}
Video struct {
Image image.Image
Duration time.Duration
}
)
// NewRecording creates new media recorder for the emulator.
func NewRecording(meta Meta, log *logger.Logger, opts Options) *Recording {
savePath, err := filepath.Abs(opts.Dir)
if err != nil {
log.Error().Err(err).Send()
}
if err := oss.CheckCreateDir(savePath); err != nil {
log.Error().Err(err).Send()
}
return &Recording{dir: savePath, meta: meta, opts: opts, log: log, vsync: []time.Duration{}}
}
func (r *Recording) Start() {
r.Lock()
defer r.Unlock()
r.enabled = true
r.saveDir = parseName(r.opts.Name, r.opts.Game, r.meta.UserName)
path := filepath.Join(r.dir, r.saveDir)
r.log.Info().Msgf("[recording] path will be [%v]", path)
if err := oss.CheckCreateDir(path); err != nil {
r.log.Fatal().Err(err)
}
audio, err := newWavStream(path, r.opts)
if err != nil {
r.log.Fatal().Err(err)
}
r.audio = audio
video, err := newPngStream(path, r.opts)
if err != nil {
r.log.Fatal().Err(err)
}
r.video = video
}
func (r *Recording) Stop() (err error) {
r.Lock()
defer r.Unlock()
r.enabled = false
err = r.audio.Close()
err = r.video.Close()
path := filepath.Join(r.dir, r.saveDir)
// FFMPEG
err = createFfmpegMuxFile(path, videoFile, r.vsync, r.opts)
if err == nil && r.opts.Zip && r.saveDir != "" {
src := filepath.Join(r.dir, r.saveDir)
dst := filepath.Join(src, "..", r.saveDir)
go func() {
if err := compress(src, dst); err != nil {
r.log.Error().Err(err).Msg("error during result compress")
return
}
if err := os.RemoveAll(src); err != nil {
r.log.Error().Err(err).Msg("error during result compress")
}
}()
}
r.vsync = []time.Duration{}
return err
}
func (r *Recording) Set(enable bool, user string) {
r.Lock()
if !r.enabled && enable {
r.Unlock()
r.Start()
r.Lock()
} else {
if r.enabled && !enable {
r.Unlock()
if err := r.Stop(); err != nil {
r.log.Error().Err(err).Msg("failed to stop recording")
}
r.log.Debug().Msg("recording has stopped")
r.Lock()
}
}
r.enabled = enable
r.meta.UserName = user
r.Unlock()
}
func (r *Recording) Enabled() bool {
r.Lock()
defer r.Unlock()
return r.enabled
}
func (r *Recording) WriteVideo(frame Video) { r.video.Write(frame) }
func (r *Recording) WriteAudio(audio Audio) {
r.audio.Write(audio)
r.Lock()
r.vsync = append(r.vsync, audio.Duration)
r.Unlock()
}
func parseName(name, game, user string) (out string) {
if d := reDate.FindStringSubmatch(name); d != nil {
out = reDate.ReplaceAllString(name, time.Now().Format(d[1]))
} else {
out = name
}
if rnd := reRand.FindStringSubmatch(out); rnd != nil {
out = reRand.ReplaceAllString(out, random(rnd[1]))
}
out = reUser.ReplaceAllString(out, user)
out = reGame.ReplaceAllString(out, game)
return
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func random(num string) string {
n, err := strconv.Atoi(num)
if err != nil {
return ""
}
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}