-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_queue.go
150 lines (110 loc) · 2.52 KB
/
stream_queue.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 main
import (
"errors"
"io"
"os"
"os/exec"
"sync"
"time"
)
type StreamQueue struct {
elements []*StreamQueueElement
mu sync.Mutex
}
func (queue *StreamQueue) Add(el *StreamQueueElement) {
queue.mu.Lock()
queue.elements = append(queue.elements, el)
defer queue.mu.Unlock()
}
func (queue *StreamQueue) shift() {
if len(queue.elements) > 0 {
queue.elements[0] = nil
queue.elements = queue.elements[1:]
}
}
func (queue *StreamQueue) GetAudioFrame() ([]byte, bool) {
queue.mu.Lock()
defer queue.mu.Unlock()
frame := make([]byte, BytesPerTick)
if len(queue.elements) == 0 {
return frame, false
}
if !queue.elements[0].started {
go queue.elements[0].Start()
}
read, err := queue.elements[0].Read(frame)
if len(queue.elements) >= 2 {
if !queue.elements[1].started && (queue.elements[0].IsNearEnd() || err != nil) {
go queue.elements[1].Start()
}
if read < BytesPerTick && errors.Is(err, io.EOF) {
_, _ = queue.elements[1].Read(frame[read:])
}
}
if errors.Is(err, io.EOF) {
queue.shift()
}
return frame, true
}
type StreamQueueElement struct {
source string
data []byte
started bool
done bool
mu sync.Mutex
}
func (e *StreamQueueElement) Start() {
e.started = true
master := "media/" + e.source + "/master.m3u8"
for {
// Make sure the media source exists
if _, err := os.Stat("media/" + e.source); os.IsNotExist(err) {
// We're just going to set e.done to true to tell the queue to move on, as something is wrong
e.done = true
return
}
if _, err := os.Stat(master); !os.IsNotExist(err) {
// Master playlist found! Let's process it!
break
}
// If we're here, it probably means ffmpeg isn't finished downloading the source, so we'll try again in a sec
time.Sleep(time.Second)
}
command := exec.Command("ffmpeg", "-i", master, "-f", "s16le", "-ar", "44100", "-ac", "2", "pipe:1")
pipe, err := command.StdoutPipe()
if err != nil {
e.done = true
return
}
err = command.Start()
if err != nil {
e.done = true
return
}
for {
buf := make([]byte, 44100*4*2)
read, err := pipe.Read(buf)
e.mu.Lock()
e.data = append(e.data, buf[:read]...)
e.mu.Unlock()
if err != nil {
e.done = true
return
}
}
}
func (e *StreamQueueElement) Read(b []byte) (n int, err error) {
e.mu.Lock()
n = copy(b, e.data)
e.data = e.data[n:]
if len(e.data) == 0 && e.done {
err = io.EOF
}
e.mu.Unlock()
return
}
func (e *StreamQueueElement) IsNearEnd() bool {
e.mu.Lock()
defer e.mu.Unlock()
return len(e.data) < BytesPerTick
}