forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
49 lines (39 loc) · 775 Bytes
/
writer.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
package gortsplib
import (
"github.com/aler9/gortsplib/v2/pkg/ringbuffer"
)
// this struct contains a queue that allows to detach the routine that is reading a stream
// from the routine that is writing a stream.
type writer struct {
running bool
buffer *ringbuffer.RingBuffer
done chan struct{}
}
func (w *writer) allocateBuffer(size int) {
w.buffer, _ = ringbuffer.New(uint64(size))
}
func (w *writer) start() {
w.running = true
w.done = make(chan struct{})
go w.run()
}
func (w *writer) stop() {
if w.running {
w.buffer.Close()
<-w.done
w.running = false
}
}
func (w *writer) run() {
defer close(w.done)
for {
tmp, ok := w.buffer.Pull()
if !ok {
return
}
tmp.(func())()
}
}
func (w *writer) queue(cb func()) {
w.buffer.Push(cb)
}