-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscoder.go
57 lines (47 loc) · 1.07 KB
/
transcoder.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
package avmuxer
import (
"errors"
"io"
)
type Transcoder struct {
input Stream
encoder Encoder
io.Reader
}
func NewTranscoder() *Transcoder {
return &Transcoder{}
}
func (tc *Transcoder) AddSource(stream Stream) error {
if tc.input != nil {
return errors.New("source is already present")
}
tc.input = stream
return nil
}
func (tc *Transcoder) AddEncoder(enc Encoder) error {
if tc.encoder != nil {
return errors.New("encoder is already present")
}
tc.encoder = enc
return nil
}
func (tc *Transcoder) Read(dst []byte) (int, error) {
if tc.input == nil {
return 0, errors.New("input stream is not binded")
}
pcm := make([]int16, tc.encoder.SampleSize())
n, err := tc.input.ReadPCM(pcm)
if err != nil {
return 0, err
}
return tc.encoder.Encode(pcm[:n], dst)
}
func (tc *Transcoder) ReadPCM(dst []int16) (int, error) {
if tc.input == nil {
return 0, errors.New("input stream is not binded")
}
return tc.input.ReadPCM(dst)
}
func (tc *Transcoder) WritePCM([]int16) (int, error) {
return 0, errors.New("transcoder stream doesn't support write method")
}