1
- package multiplexer
1
+ package avmuxer
2
2
3
3
import (
4
4
"errors"
5
5
"io"
6
6
"log"
7
7
)
8
8
9
+ // OpusStream interface defines the methods for both encoding and decoding Opus streams
9
10
type OpusStream interface {
10
11
Stream
11
12
@@ -17,8 +18,10 @@ type OpusStream interface {
17
18
ChannelCount () int
18
19
SampleDurationMs () int
19
20
SampleCount () int
21
+ ID () string
20
22
}
21
23
24
+ // opusEncodingStream implements OpusStream for encoding
22
25
type opusEncodingStream struct {
23
26
id string
24
27
sampleRate int
@@ -31,6 +34,7 @@ type opusEncodingStream struct {
31
34
encoder * OpusEncoder
32
35
}
33
36
37
+ // opusDecodingStream implements OpusStream for decoding
34
38
type opusDecodingStream struct {
35
39
id string
36
40
sampleRate int
@@ -43,12 +47,18 @@ type opusDecodingStream struct {
43
47
decoder * OpusDecoder
44
48
}
45
49
50
+ // NewDecodingOpusStream creates a new OpusStream for decoding
46
51
func NewDecodingOpusStream (id string , sampleRate , sampleDuration , channel int ) (OpusStream , error ) {
52
+ // Calculate sample size based on duration and rate
47
53
sampleSize := sampleDuration * sampleRate / 1000
54
+
55
+ // Create a new OpusDecoder
48
56
dec , err := NewOpusDecoder (sampleRate , channel , sampleSize )
49
57
if err != nil {
50
58
return nil , err
51
59
}
60
+
61
+ // Return a new opusDecodingStream
52
62
return & opusDecodingStream {
53
63
id : id ,
54
64
sampleRate : sampleRate ,
@@ -60,7 +70,10 @@ func NewDecodingOpusStream(id string, sampleRate, sampleDuration, channel int) (
60
70
}, err
61
71
}
62
72
73
+ // NewEncodingOpusStream creates a new OpusStream for encoding
63
74
func NewEncodingOpusStream (id string , sampleRate , sampleDuration , channel int ) (OpusStream , error ) {
75
+ // Similar to NewDecodingOpusStream, but for encoding
76
+ // ... existing code ...
64
77
sampleSize := sampleDuration * sampleRate / 1000
65
78
enc , err := NewOpusEncoder (sampleRate , channel , sampleSize )
66
79
if err != nil {
@@ -89,38 +102,46 @@ func (ods *opusDecodingStream) SampleRate() int {
89
102
func (ods * opusDecodingStream ) SampleDurationMs () int {
90
103
return ods .sampleDurationMs
91
104
}
105
+ func (ods * opusDecodingStream ) ID () string {
106
+ return ods .id
107
+ }
92
108
93
109
func (ods * opusDecodingStream ) Decode (src []byte , dst []int16 ) (int , error ) {
94
110
return ods .decoder .Decode (src , dst )
95
111
}
96
112
97
- // It takes encoded opus data and decode then store in the buffer
113
+ // Write decodes Opus data and writes PCM to the sink
98
114
func (ods * opusDecodingStream ) Write (data []byte ) (int , error ) {
115
+ // Decode Opus data to PCM
99
116
pcm := make ([]int16 , ods .size * ods .channel )
100
117
n , err := ods .Decode (data , pcm )
101
118
if err != nil {
102
119
return 0 , err
103
120
}
121
+
104
122
log .Printf ("samples decoded: %v, os.size: %v, data size: %v\n " , n , ods .size , len (data ))
123
+
124
+ // Write decoded PCM to sink if available
105
125
if ods .sink != nil {
106
126
_ , err := ods .sink .Write (int16ToByteSlice (pcm [:n * ods .channel ]))
107
127
if err != nil {
108
128
return 0 , err
109
129
}
110
130
}
111
- // FIXME(itzmanish): do we need to store the pcm data?
131
+
132
+ // Write PCM data to buffer
112
133
return ods .decoder .buffer .Write (pcm [:n * ods .channel ])
113
134
}
114
135
115
- // This reads the raw PCM data from Decoding OPUS stream
136
+ // ReadPCM reads raw PCM data from the decoding buffer
116
137
func (ods * opusDecodingStream ) ReadPCM (dst []int16 ) (int , error ) {
117
138
if ods .decoder == nil {
118
139
return 0 , errors .New ("stream is not decoding supported" )
119
140
}
120
141
return ods .decoder .buffer .Read (dst )
121
142
}
122
143
123
- // This reads the raw PCM data from Decoding OPUS stream and convert them to byte array
144
+ // Read reads raw PCM data and converts it to a byte array
124
145
func (ods * opusDecodingStream ) Read (dst []byte ) (int , error ) {
125
146
if ods .decoder == nil {
126
147
return 0 , errors .New ("stream is not decoding supported" )
@@ -167,6 +188,10 @@ func (oes *opusEncodingStream) SampleDurationMs() int {
167
188
return oes .sampleDurationMs
168
189
}
169
190
191
+ func (oes * opusEncodingStream ) ID () string {
192
+ return oes .id
193
+ }
194
+
170
195
func (* opusEncodingStream ) ReadPCM ([]int16 ) (int , error ) {
171
196
return 0 , errors .New ("encoding stream doesn't support reading pcm" )
172
197
}
@@ -191,25 +216,21 @@ func (oes *opusEncodingStream) WritePCM(data []int16) (int, error) {
191
216
return oes .encoder .buffer .Write (byteData [:n ])
192
217
}
193
218
194
- // This reads encoded data after pulling and encoding from the reader
219
+ // Read reads encoded Opus data from the encoder's buffer
195
220
func (oes * opusEncodingStream ) Read (dst []byte ) (int , error ) {
196
- // if oes.encoder == nil {
197
- // return 0, errors.New("stream is not encoding supported")
198
- // }
199
- // if oes.reader == nil {
200
- // return 0, errors.New("reader is not connected")
201
- // }
202
- // byteArr := make([]byte, oes.size*oes.channel*2)
203
- // n, err := oes.reader.Read(byteArr)
204
- // if err != nil {
205
- // return n, err
206
- // }
207
- // pcm := byteSliceToInt16(byteArr[:n])
208
- // _, err = oes.WritePCM(pcm)
209
- // if err != nil {
210
- // return 0, err
211
- // }
212
- return oes .encoder .buffer .Read (dst )
221
+ if oes .encoder == nil {
222
+ return 0 , errors .New ("encoder is not initialized" )
223
+ }
224
+
225
+ n , err := oes .encoder .buffer .Read (dst )
226
+ if err != nil {
227
+ if err == ErrEmptyBuffer {
228
+ return 0 , io .EOF
229
+ }
230
+ return n , err
231
+ }
232
+
233
+ return n , nil
213
234
}
214
235
215
236
func (oes * opusEncodingStream ) Encode (src []int16 , dst []byte ) (int , error ) {
0 commit comments