-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstream.go
More file actions
532 lines (473 loc) · 14.1 KB
/
stream.go
File metadata and controls
532 lines (473 loc) · 14.1 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// stream.go implements streaming io.Reader and io.WriteCloser wrappers for Opus encoding/decoding.
package gopus
import (
"encoding/binary"
"io"
"math"
)
// Streaming API
//
// The Reader and Writer types provide io.Reader and io.WriteCloser interfaces
// for streaming Opus encode/decode operations. They handle frame boundaries
// internally, allowing integration with Go's standard io patterns.
//
// # Streaming Decode
//
// To decode a stream of Opus packets:
//
// source := &MyPacketReader{} // implements PacketReader
// reader, err := gopus.NewReader(gopus.DefaultDecoderConfig(48000, 2), source, gopus.FormatFloat32LE)
// if err != nil {
// log.Fatal(err)
// }
//
// // Read decoded PCM bytes
// buf := make([]byte, 4096)
// for {
// n, err := reader.Read(buf)
// if err == io.EOF {
// break
// }
// if err != nil {
// log.Fatal(err)
// }
// processAudio(buf[:n])
// }
//
// # Streaming Encode
//
// To encode PCM audio to a stream of Opus packets:
//
// sink := &MyPacketSink{} // implements PacketSink
// writer, err := gopus.NewWriter(48000, 2, sink, gopus.FormatFloat32LE, gopus.ApplicationAudio)
// if err != nil {
// log.Fatal(err)
// }
//
// // Write PCM bytes
// pcmBytes := getPCMData() // float32 little-endian bytes
// _, err = writer.Write(pcmBytes)
// if err != nil {
// log.Fatal(err)
// }
//
// // Flush remaining buffered samples and close the sink when supported.
// if err := writer.Close(); err != nil {
// log.Fatal(err)
// }
//
// # Sample Format
//
// Both Reader and Writer support two sample formats:
// - FormatFloat32LE: 32-bit float, little-endian (4 bytes per sample)
// - FormatInt16LE: 16-bit signed integer, little-endian (2 bytes per sample)
//
// Samples are interleaved for stereo: [L0, R0, L1, R1, ...]
// SampleFormat specifies the PCM sample format for streaming.
type SampleFormat int
const (
// FormatFloat32LE is 32-bit float, little-endian (4 bytes per sample).
FormatFloat32LE SampleFormat = iota
// FormatInt16LE is 16-bit signed integer, little-endian (2 bytes per sample).
FormatInt16LE
)
func validSampleFormat(format SampleFormat) bool {
switch format {
case FormatFloat32LE, FormatInt16LE:
return true
default:
return false
}
}
// BytesPerSample returns the number of bytes per sample for the format.
func (f SampleFormat) BytesPerSample() int {
switch f {
case FormatFloat32LE:
return 4
case FormatInt16LE:
return 2
default:
return 0
}
}
// PacketReader provides Opus packets for streaming decode.
// Implementations should return io.EOF when no more packets are available.
type PacketReader interface {
// ReadPacketInto fills dst with the next Opus packet.
//
// granulePos is the source packet position in decoded-sample units when the
// container provides one (for example Ogg Opus granule positions). Sources
// that do not track positions should return 0.
//
// Returns io.EOF when stream ends. Return n=0, err=nil to trigger PLC.
ReadPacketInto(dst []byte) (n int, granulePos uint64, err error)
}
// PacketSink receives encoded Opus packets from streaming encode.
type PacketSink interface {
// WritePacket writes an encoded Opus packet.
// Returns number of bytes written and any error.
//
// If the sink also implements io.Closer, Writer.Close forwards to it after
// flushing any buffered audio.
WritePacket(packet []byte) (int, error)
}
// Reader decodes an Opus stream, implementing io.Reader.
// Output is PCM samples in the configured format.
//
// The Reader handles frame boundaries internally, buffering decoded
// PCM samples and serving byte-oriented reads.
//
// Example:
//
// reader, err := gopus.NewReader(gopus.DefaultDecoderConfig(48000, 2), source, gopus.FormatFloat32LE)
// io.Copy(audioOutput, reader)
type Reader struct {
dec *Decoder
source PacketReader
format SampleFormat // Output sample format
packetBuf []byte
pcmFloat []float32 // Decoded PCM samples
pcmInt16 []int16
byteBuf []byte // PCM as bytes
offset int // Current read position in byteBuf
lastGranulePos uint64 // Most recent packet position reported by the source
eof bool // Source exhausted
}
// NewReader creates a streaming decoder.
//
// Parameters:
// - cfg: decoder configuration
// - source: provides Opus packets for decoding
// - format: output sample format (FormatFloat32LE or FormatInt16LE)
func NewReader(cfg DecoderConfig, source PacketReader, format SampleFormat) (*Reader, error) {
if source == nil {
return nil, ErrNilPacketReader
}
if !validSampleFormat(format) {
return nil, ErrInvalidSampleFormat
}
dec, err := NewDecoder(cfg)
if err != nil {
return nil, err
}
return &Reader{
dec: dec,
source: source,
format: format,
packetBuf: make([]byte, dec.maxPacketBytes),
pcmFloat: make([]float32, dec.maxPacketSamples*dec.channels),
pcmInt16: make([]int16, dec.maxPacketSamples*dec.channels),
offset: 0,
eof: false,
}, nil
}
// Read implements io.Reader, reading decoded PCM bytes.
//
// The Reader handles frame boundaries internally, fetching and decoding
// packets as needed to fill the buffer.
func (r *Reader) Read(p []byte) (int, error) {
// If buffer is exhausted, try to get more data
if r.offset >= len(r.byteBuf) {
if r.eof {
return 0, io.EOF
}
// Fetch next packet from source
nPacket, granulePos, err := r.source.ReadPacketInto(r.packetBuf)
if err == io.EOF {
r.eof = true
return 0, io.EOF
}
if err != nil {
return 0, err
}
r.lastGranulePos = granulePos
var packet []byte
if nPacket > 0 {
packet = r.packetBuf[:nPacket]
}
switch r.format {
case FormatFloat32LE:
nSamples, decErr := r.dec.Decode(packet, r.pcmFloat)
if decErr != nil {
return 0, decErr
}
byteLen := nSamples * r.dec.channels * 4
if cap(r.byteBuf) < byteLen {
r.byteBuf = make([]byte, byteLen)
}
r.byteBuf = r.byteBuf[:byteLen]
for i := 0; i < nSamples*r.dec.channels; i++ {
bits := math.Float32bits(r.pcmFloat[i])
binary.LittleEndian.PutUint32(r.byteBuf[i*4:], bits)
}
case FormatInt16LE:
nSamples, decErr := r.dec.DecodeInt16(packet, r.pcmInt16)
if decErr != nil {
return 0, decErr
}
byteLen := nSamples * r.dec.channels * 2
if cap(r.byteBuf) < byteLen {
r.byteBuf = make([]byte, byteLen)
}
r.byteBuf = r.byteBuf[:byteLen]
for i := 0; i < nSamples*r.dec.channels; i++ {
binary.LittleEndian.PutUint16(r.byteBuf[i*2:], uint16(r.pcmInt16[i]))
}
default:
return 0, ErrInvalidSampleFormat
}
r.offset = 0
}
// Copy available bytes to p
n := copy(p, r.byteBuf[r.offset:])
r.offset += n
return n, nil
}
// SampleRate returns the sample rate in Hz.
func (r *Reader) SampleRate() int {
return r.dec.SampleRate()
}
// Channels returns the number of audio channels (1 or 2).
func (r *Reader) Channels() int {
return r.dec.Channels()
}
// LastGranulePos returns the most recent packet position reported by the source.
//
// For Ogg Opus this is the granule position from the underlying page header.
// Sources that do not track positions may leave this at 0.
func (r *Reader) LastGranulePos() uint64 {
return r.lastGranulePos
}
// Reset clears buffers and decoder state for a new stream.
func (r *Reader) Reset() {
r.dec.Reset()
if r.byteBuf != nil {
r.byteBuf = r.byteBuf[:0]
}
r.offset = 0
r.lastGranulePos = 0
r.eof = false
}
// Writer encodes PCM samples to an Opus stream, implementing io.WriteCloser.
// Input is PCM samples in the configured format.
//
// The Writer buffers input samples until a complete frame is accumulated,
// then encodes and sends the packet to the sink.
//
// Example:
//
// writer, err := gopus.NewWriter(48000, 2, sink, gopus.FormatFloat32LE, gopus.ApplicationAudio)
// io.Copy(writer, audioInput)
// writer.Close() // flush remaining buffered samples
type Writer struct {
enc *Encoder
sink PacketSink
format SampleFormat // Input sample format
sampleBuf []byte // Buffered input bytes
frameBytes int // Bytes needed for one frame
frameSamples int // Samples per frame across all channels
packetBuf []byte // Buffer for encoded packet (4000 bytes)
pcmScratch []float32 // Reused PCM scratch for byte-to-sample conversion
paddedBuf []byte // Reused zero-padded frame buffer for Flush
closed bool
}
// NewWriter creates a streaming encoder.
//
// Parameters:
// - sampleRate: input sample rate (8000, 12000, 16000, 24000, or 48000)
// - channels: number of audio channels (1 or 2)
// - sink: receives encoded Opus packets
// - format: input sample format (FormatFloat32LE or FormatInt16LE)
// - application: encoder application hint
func NewWriter(sampleRate, channels int, sink PacketSink, format SampleFormat, application Application) (*Writer, error) {
if sink == nil {
return nil, ErrNilPacketSink
}
if !validSampleFormat(format) {
return nil, ErrInvalidSampleFormat
}
enc, err := NewEncoder(EncoderConfig{SampleRate: sampleRate, Channels: channels, Application: application})
if err != nil {
return nil, err
}
// Default frame size is 960 samples (20ms at 48kHz)
frameSize := enc.FrameSize()
bytesPerSample := format.BytesPerSample()
frameBytes := frameSize * channels * bytesPerSample
frameSamples := frameSize * channels
return &Writer{
enc: enc,
sink: sink,
format: format,
sampleBuf: make([]byte, 0, frameBytes*2), // Pre-allocate for 2 frames
frameBytes: frameBytes,
frameSamples: frameSamples,
packetBuf: make([]byte, 4000),
pcmScratch: make([]float32, frameSamples),
paddedBuf: make([]byte, frameBytes),
}, nil
}
// Write implements io.Writer, encoding PCM bytes to Opus packets.
//
// The Writer buffers input samples until a complete frame is accumulated,
// then encodes and sends the packet to the sink.
func (w *Writer) Write(p []byte) (int, error) {
if w.closed {
return 0, io.ErrClosedPipe
}
initialBuffered := len(w.sampleBuf)
processedBytes := 0
// Append input to buffer
w.sampleBuf = append(w.sampleBuf, p...)
// Process complete frames
for len(w.sampleBuf)-processedBytes >= w.frameBytes {
// Extract one frame of bytes
frameData := w.sampleBuf[processedBytes : processedBytes+w.frameBytes]
// Convert bytes to float32 PCM using reusable scratch.
pcm := w.pcmScratch[:w.frameSamples]
w.decodePCMInto(pcm, frameData)
// Encode the frame
n, err := w.enc.Encode(pcm, w.packetBuf)
if err != nil {
w.discardConsumedPrefix(processedBytes)
return consumedInputBytes(initialBuffered, processedBytes, len(p)), err
}
// If n > 0, send packet to sink (n == 0 means DTX suppressed)
if n > 0 {
if err := w.writePacketToSink(w.packetBuf[:n]); err != nil {
w.closed = true
w.discardConsumedPrefix(processedBytes)
return consumedInputBytes(initialBuffered, processedBytes, len(p)), err
}
}
processedBytes += w.frameBytes
}
w.discardConsumedPrefix(processedBytes)
return len(p), nil
}
func consumedInputBytes(initialBuffered, processedBytes, incoming int) int {
if processedBytes <= initialBuffered {
return 0
}
consumed := processedBytes - initialBuffered
if consumed > incoming {
return incoming
}
return consumed
}
func (w *Writer) writePacketToSink(packet []byte) error {
n, err := w.sink.WritePacket(packet)
if err != nil {
if n > 0 {
return io.ErrShortWrite
}
return err
}
if n != len(packet) {
return io.ErrShortWrite
}
return nil
}
func (w *Writer) discardConsumedPrefix(consumed int) {
if consumed == 0 {
return
}
remaining := len(w.sampleBuf) - consumed
copy(w.sampleBuf, w.sampleBuf[consumed:])
w.sampleBuf = w.sampleBuf[:remaining]
}
// decodePCMInto converts bytes to float32 PCM samples using caller-provided scratch.
func (w *Writer) decodePCMInto(dst []float32, data []byte) {
switch w.format {
case FormatFloat32LE:
for i := range dst {
bits := binary.LittleEndian.Uint32(data[i*4:])
dst[i] = math.Float32frombits(bits)
}
case FormatInt16LE:
for i := range dst {
sample := int16(binary.LittleEndian.Uint16(data[i*2:]))
dst[i] = float32(sample) / 32768.0
}
}
}
// Flush encodes any buffered samples.
// If samples don't fill a complete frame, they are zero-padded.
// Call Flush before closing the stream to ensure all audio is encoded.
func (w *Writer) Flush() error {
if w.closed {
return io.ErrClosedPipe
}
if len(w.sampleBuf) == 0 {
return nil
}
// Zero-pad to complete frame using reusable scratch.
clear(w.paddedBuf)
copy(w.paddedBuf, w.sampleBuf)
pcm := w.pcmScratch[:w.frameSamples]
w.decodePCMInto(pcm, w.paddedBuf)
// Encode the frame
n, err := w.enc.Encode(pcm, w.packetBuf)
if err != nil {
return err
}
// If n > 0, send packet to sink
if n > 0 {
if err := w.writePacketToSink(w.packetBuf[:n]); err != nil {
w.closed = true
return err
}
}
// Clear buffer
w.sampleBuf = w.sampleBuf[:0]
return nil
}
// Close flushes buffered samples and closes the underlying sink when supported.
//
// If the sink implements io.Closer, Close forwards to it after a successful
// flush. Close is idempotent.
func (w *Writer) Close() error {
if w.closed {
return nil
}
if err := w.Flush(); err != nil {
return err
}
w.closed = true
if closer, ok := w.sink.(io.Closer); ok {
return closer.Close()
}
return nil
}
// SetBitrate sets the target bitrate in bits per second.
// Valid range is 6000 to 510000 (6 kbps to 510 kbps).
func (w *Writer) SetBitrate(bitrate int) error {
return w.enc.SetBitrate(bitrate)
}
// SetComplexity sets the encoder's computational complexity (0-10).
func (w *Writer) SetComplexity(complexity int) error {
return w.enc.SetComplexity(complexity)
}
// SetFEC enables or disables in-band Forward Error Correction.
func (w *Writer) SetFEC(enabled bool) {
w.enc.SetFEC(enabled)
}
// SetDTX enables or disables Discontinuous Transmission.
func (w *Writer) SetDTX(enabled bool) {
w.enc.SetDTX(enabled)
}
// Reset clears buffers and encoder state for a new stream.
// It also clears the closed flag so the writer can be reused with a reusable sink.
func (w *Writer) Reset() {
w.enc.Reset()
w.sampleBuf = w.sampleBuf[:0]
w.closed = false
}
// SampleRate returns the sample rate in Hz.
func (w *Writer) SampleRate() int {
return w.enc.SampleRate()
}
// Channels returns the number of audio channels (1 or 2).
func (w *Writer) Channels() int {
return w.enc.Channels()
}