-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutliplexer.go
191 lines (162 loc) · 3.73 KB
/
mutliplexer.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
package avmuxer
import (
"errors"
"log"
"sync"
"gopkg.in/hraban/opus.v2"
)
var int16BufferPool sync.Pool
type Multiplexer struct {
sync.RWMutex
encoder Encoder
sources map[string]Stream
}
type Stream interface {
ReadPCM([]int16) (int, error)
WritePCM([]int16) (int, error)
}
type DecodingStream interface {
Decode([]byte, []int16) (int, error)
}
type EncodingStream interface {
Encode([]int16, []byte) (int, error)
}
type Encoder interface {
Encode([]int16, []byte) (int, error)
SampleSize() int
ChannelCount() int
}
type Decoder interface {
Decode([]byte, []int16) (int, error)
}
type OpusDecoder struct {
od *opus.Decoder
buffer *RingBuffer[int16]
}
func (od *OpusDecoder) Decode(in []byte, out []int16) (int, error) {
return od.od.Decode(in, out)
}
func NewOpusDecoder(sampleRate, channel, size int) (Decoder, error) {
decoder, err := opus.NewDecoder(sampleRate, channel)
if err != nil {
return nil, err
}
return &OpusDecoder{
od: decoder,
buffer: NewRingBuffer[int16](size * channel),
}, nil
}
type OpusEncoder struct {
size int
channel int
oe *opus.Encoder
buffer *RingBuffer[byte]
}
func (oe *OpusEncoder) SampleSize() int {
return oe.size
}
func (oe *OpusEncoder) ChannelCount() int {
return oe.channel
}
func (oe *OpusEncoder) Encode(in []int16, out []byte) (int, error) {
return oe.oe.Encode(in, out)
}
func NewOpusEncoder(sampleRate, channel, size int) (Encoder, error) {
enc, err := opus.NewEncoder(sampleRate, channel, opus.AppAudio)
if err != nil {
return nil, err
}
err = enc.SetBitrate(int(opus.Fullband))
if err != nil {
return nil, err
}
return &OpusEncoder{
size: size,
channel: channel,
oe: enc,
buffer: NewRingBuffer[byte](size * channel * 2), // int16 data holds 2 byte, size is sample size
}, nil
}
func NewMultiplexer() *Multiplexer {
return &Multiplexer{
sources: make(map[string]Stream),
}
}
func (mr *Multiplexer) AddEncoder(id string, enc Encoder) error {
if mr.encoder != nil {
return errors.New("encoder already configured")
}
mr.Lock()
mr.encoder = enc
mr.Unlock()
return nil
}
// size should be calculated as clock_rate*sample_duration_in_ms/1000
func (mr *Multiplexer) AddSourceStream(id string, stream Stream) error {
mr.Lock()
defer mr.Unlock()
if _, ok := mr.sources[id]; ok {
return errors.New("stream already exists")
}
mr.sources[id] = stream
return nil
}
func (mr *Multiplexer) interleavedMultiplex(sampleSize int) []int16 {
buffs := [][]int16{}
maxBufSize := 0
mr.RLock()
for _, s := range mr.sources {
bufI := int16BufferPool.Get()
var buf []int16
if bufI == nil {
buf = make([]int16, sampleSize)
} else {
buf = *bufI.(*[]int16)
}
n, err := s.ReadPCM(buf)
if err != nil {
continue
}
buf = buf[:n]
if n > maxBufSize {
maxBufSize = n
}
buffs = append(buffs, buf)
}
mr.RUnlock()
size := len(buffs)
out := make([]int16, maxBufSize)
// for i := maxBufSize - 1; i >= 0; i-- {
for i := 0; i < maxBufSize; i++ {
var sum int16
for _, buf := range buffs {
sum += (buf[i] / int16(size))
}
out[i] = sum
}
for _, buf := range buffs {
if cap(buf) < 8184 {
buf = buf[:0]
int16BufferPool.Put(&buf)
}
}
return out
}
func (mr *Multiplexer) WritePCM([]int16) (int, error) {
return 0, errors.New("multiplexer stream doesn't support write method")
}
func (mr *Multiplexer) ReadPCM(sampleSize int) []int16 {
return mr.interleavedMultiplex(sampleSize)
}
func (mr *Multiplexer) Read(dst []byte) (int, error) {
data := mr.ReadPCM(mr.encoder.SampleSize() * mr.encoder.ChannelCount())
if len(data) == 0 {
return 0, nil
}
n, err := mr.encoder.Encode(data, dst)
if err != nil {
return 0, err
}
log.Printf("encoded data: size(pcm): %v, n: %v, slicecap: %v", len(data), n, len(dst))
return n, nil
}