/
play.go
152 lines (136 loc) · 3.33 KB
/
play.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
package player
import (
"errors"
"fmt"
"io"
"time"
"github.com/jonas747/dca"
)
// Play will start to play the current queue
func (p *Player) Play() {
if p.Playing() {
return
}
go func() {
p.audio.Lock()
defer p.audio.Unlock()
for {
tracklen := p.Queue.Len()
if tracklen == 0 {
p.SendNotice("Nothing left to play!", fmt.Sprintf("You can give me more by using the `%s` command!", p.conf.Bot.Prefix), "")
if err := p.Disconnect(); err != nil {
p.log.Err(err).Msg("unable to disconnect from voice channel")
}
return
}
t := p.Queue.Get()
if t == nil {
continue
}
stream, err := t.StreamURL()
if err != nil {
p.log.Err(err).Msg("unable to find a suitable stream")
p.Queue.Pop()
continue
}
if err := p.Read(stream); err != nil {
p.log.Err(err).Msg("unable to read stream")
}
if p.Stopped() {
if err := p.Disconnect(); err != nil {
p.log.Err(err).Msg("unable to disconnect from voice channel")
}
return
}
p.Queue.Pop()
}
}()
}
func (p *Player) onReadStart() error {
if p.voice == nil {
if err := p.Connect(); err != nil {
return fmt.Errorf("unable to connect: %w", err)
}
}
if err := p.voice.Speaking(true); err != nil {
return fmt.Errorf("failed setting voice to speaking: %w", err)
}
p.Stats = &Stats{}
p.state.Lock()
defer p.state.Unlock()
p.state.Playing = true
p.state.Stopped = false
return nil
}
func (p *Player) onReadEnd() {
if p.voice != nil {
if err := p.voice.Speaking(false); err != nil {
p.log.Err(err).Msg("unable to set speaking to false")
}
}
p.Stats = nil
p.state.Lock()
defer p.state.Unlock()
p.state.Playing = false
p.stream = nil
p.encode = nil
}
func (p *Player) Read(url string) error {
var err error
if err = p.onReadStart(); err != nil {
return fmt.Errorf("unable to start playing: %w", err)
}
defer p.onReadEnd()
opts := dca.StdEncodeOptions
opts.RawOutput = true
opts.Bitrate = 120
opts.Volume = p.Volume()
p.encode, err = dca.EncodeFile(url, opts)
if err != nil {
return fmt.Errorf("failed creating the encoding session: %w", err)
}
defer p.encode.Cleanup()
done := make(chan error)
p.stream = dca.NewStream(p.encode, p.voice, done)
tc := time.NewTicker(5 * time.Second)
defer tc.Stop()
for {
select {
case err := <-done:
if err != nil && err == io.EOF {
return nil
}
if errors.Is(err, dca.ErrVoiceConnClosed) {
p.log.Info().Msg("voice connection lost")
if err = p.Disconnect(); err != nil {
p.log.Err(err).Msg("unable to disconnect vocal channel")
return err
}
if err = p.Connect(); err != nil {
p.log.Err(err).Msg("unable to reconnect")
p.voice = nil
return err
}
p.stream = dca.NewStream(p.encode, p.voice, done)
p.log.Info().Msg("voice reconnected")
continue
}
return fmt.Errorf("reading stream: %w", err)
case <-p.stop:
p.log.Debug().Str("event", "stop").Msg("stopping player")
return nil
case <-tc.C:
d := p.stream.PlaybackPosition()
s := p.encode.Stats()
p.Stats.Lock()
p.Stats.PlaybackPosition = d
p.Stats.Bitrate = s.Bitrate
p.Stats.Duration = s.Duration
p.Stats.Speed = s.Speed
p.Stats.Size = s.Size
p.Stats.TimeAxis = append(p.Stats.TimeAxis, float64(s.Duration))
p.Stats.BiteRateAxis = append(p.Stats.BiteRateAxis, float64(s.Bitrate))
p.Stats.Unlock()
}
}
}