1package gumble
2
3import (
4	"sync"
5)
6
7const (
8	audioCodecIDOpus = 4
9)
10
11var (
12	audioCodecsLock sync.Mutex
13	audioCodecs     [8]AudioCodec
14)
15
16// RegisterAudioCodec registers an audio codec that can be used for encoding
17// and decoding outgoing and incoming audio data. The function panics if the
18// ID is invalid.
19func RegisterAudioCodec(id int, codec AudioCodec) {
20	audioCodecsLock.Lock()
21	defer audioCodecsLock.Unlock()
22
23	if id < 0 || id >= len(audioCodecs) {
24		panic("id out of range")
25	}
26	audioCodecs[id] = codec
27}
28
29func getAudioCodec(id int) AudioCodec {
30	audioCodecsLock.Lock()
31	defer audioCodecsLock.Unlock()
32	return audioCodecs[id]
33}
34
35// AudioCodec can create a encoder and a decoder for outgoing and incoming
36// data.
37type AudioCodec interface {
38	ID() int
39	NewEncoder() AudioEncoder
40	NewDecoder() AudioDecoder
41}
42
43// AudioEncoder encodes a chunk of PCM audio samples to a certain type.
44type AudioEncoder interface {
45	ID() int
46	Encode(pcm []int16, mframeSize, maxDataBytes int) ([]byte, error)
47	Reset()
48}
49
50// AudioDecoder decodes an encoded byte slice to a chunk of PCM audio samples.
51type AudioDecoder interface {
52	ID() int
53	Decode(data []byte, frameSize int) ([]int16, error)
54	Reset()
55}
56