-
Notifications
You must be signed in to change notification settings - Fork 0
/
g711_stream.go
73 lines (62 loc) · 1.35 KB
/
g711_stream.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
package avmuxer
import (
"bytes"
"errors"
"fmt"
"io"
"github.com/zaf/g711"
)
type G711Type int
const (
G711Type_Alaw G711Type = iota + 1
G711Type_Ulaw
)
// commonly known as PCM-A and PCM-U
type G711Stream struct {
id string
decoder *g711.Decoder
inputBuffer *bytes.Buffer
}
func NewG711Stream(id string, stype G711Type) (Stream, error) {
buf := bytes.NewBuffer([]byte{})
var decoder *g711.Decoder
var err error
switch stype {
case G711Type_Alaw:
decoder, err = g711.NewAlawDecoder(buf)
case G711Type_Ulaw:
decoder, err = g711.NewUlawDecoder(buf)
default:
return nil, fmt.Errorf("unknown g711 stream type: %v", stype)
}
if err != nil {
return nil, err
}
return &G711Stream{
id: id,
decoder: decoder,
inputBuffer: buf,
}, nil
}
func (gs *G711Stream) Write(pkt []byte) (int, error) {
return gs.inputBuffer.Write(pkt)
}
func (gs *G711Stream) Read(buf []byte) (int, error) {
return gs.decoder.Read(buf)
}
func (gs *G711Stream) ReadPCM(dst []int16) (int, error) {
bb := make([]byte, len(dst)*2)
n, err := gs.Read(bb)
if err != nil {
return 0, err
}
pcm := ByteSliceToInt16(bb[:n])
if len(dst) < len(pcm) {
return 0, io.ErrShortBuffer
}
n = copy(dst[:len(pcm)], pcm)
return n, nil
}
func (gs *G711Stream) WritePCM(data []int16) (int, error) {
return 0, errors.New("g711 stream doesn't support write pcm")
}