-
Notifications
You must be signed in to change notification settings - Fork 910
/
transcoder.go
181 lines (150 loc) · 4.29 KB
/
transcoder.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
package soundboard
import (
"context"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/jonas747/dca/v2"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/backgroundworkers"
"github.com/jonas747/yagpdb/soundboard/models"
"goji.io/pat"
)
var (
transcoderStop = make(chan *sync.WaitGroup)
transcoderOptions *dca.EncodeOptions
)
func init() {
// Copy the standard options
cp := *dca.StdEncodeOptions
transcoderOptions = &cp
transcoderOptions.Bitrate = 100
}
var _ commands.CommandProvider = (*Plugin)(nil)
var _ backgroundworkers.BackgroundWorkerPlugin = (*Plugin)(nil)
func (p *Plugin) RunBackgroundWorker() {
backgroundworkers.RESTServerMuxer.HandleFunc(pat.Get("/soundboard/sounddata/:sound"), p.bgworkerHandleGetSound)
transcoderLoop()
}
func (p *Plugin) StopBackgroundWorker(wg *sync.WaitGroup) {
logger.Info("Stopping soundboard transcoder...")
transcoderStop <- wg
}
func transcoderLoop() {
ticker := time.NewTicker(time.Second)
for {
select {
case wg := <-transcoderStop:
wg.Done()
return
case <-ticker.C:
items := getQueue()
for _, v := range items {
started := time.Now()
logger.Println("handling queue item")
err := handleQueueItem(v)
logger.Println("done handling queue item")
if err != nil {
logger.WithError(err).WithField("soundid", v).Error("Failed processing transcode queue item")
}
logger.WithField("sounf", v).Info("Took ", time.Since(started).String(), " to transcode sound ")
}
}
}
}
func getQueue() []string {
files, err := ioutil.ReadDir("soundboard/queue")
if err != nil {
logger.WithError(err).Error("Failed checking queue directory")
return []string{}
}
out := make([]string, len(files))
for k, v := range files {
out[k] = v.Name()
}
return out
}
func handleQueueItem(item string) error {
skipTranscode := false
idStr := item
if strings.HasSuffix(item, ".dca") {
skipTranscode = true
idStr = strings.SplitN(item, ".", 2)[0]
}
parsedId, err := strconv.Atoi(idStr)
if err != nil {
return err
}
// lock it for max 10 minutes, after that something must've gone wrong
locked, err := common.TryLockRedisKey(KeySoundLock(parsedId), 10*60)
if err != nil {
return err
}
if !locked {
logger.WithField("sound", parsedId).Warn("Sound is busy, handling it later")
return nil
}
defer common.UnlockRedisKey(KeySoundLock(parsedId))
sound, err := models.FindSoundboardSoundG(context.Background(), parsedId)
if err != nil {
return err
}
logger.WithField("sound", sound.ID).Info("Handling queued sound ", sound.Name)
if !skipTranscode {
err = transcodeSound(sound)
if err != nil {
logger.WithError(err).WithField("sound", sound.ID).Error("Failed transcoding sound")
common.GORM.Model(&sound).Update("Status", TranscodingStatusFailedOther)
os.Remove(SoundFilePath(sound.ID, TranscodingStatusReady))
} else {
common.GORM.Model(&sound).Update("Status", TranscodingStatusReady)
}
err = os.Remove(SoundFilePath(sound.ID, TranscodingStatusQueued))
} else {
os.Rename(SoundFilePath(sound.ID, TranscodingStatusQueued)+".dca", SoundFilePath(sound.ID, TranscodingStatusReady))
}
return err
}
func transcodeSound(sound *models.SoundboardSound) error {
destFile, err := os.Create(SoundFilePath(sound.ID, TranscodingStatusReady))
if err != nil {
return err
}
defer destFile.Close()
session, err := dca.EncodeFile(SoundFilePath(sound.ID, TranscodingStatus(sound.Status)), transcoderOptions)
if err != nil {
return err
}
_, err = io.Copy(destFile, session)
if err != nil {
session.Truncate()
return err
}
err = session.Error()
return err
}
func (p *Plugin) bgworkerHandleGetSound(w http.ResponseWriter, r *http.Request) {
soundIDStr := pat.Param(r, "sound")
parsed, _ := strconv.ParseInt(soundIDStr, 10, 64)
f, err := os.Open(SoundFilePath(int(parsed), TranscodingStatusReady))
if err != nil {
logger.WithError(err).WithField("sound", parsed).Error("failed opening sound")
return
}
defer f.Close()
io.Copy(w, f)
}
func getSoundFromBGWorker(soundID int) (rc io.ReadCloser, err error) {
path := "http://" + backgroundworkers.HTTPAddr.GetString() + "/soundboard/sounddata/" + strconv.Itoa(soundID)
resp, err := http.Get(path)
if err != nil {
return nil, err
}
return resp.Body, nil
}