-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder-callbacks.go
87 lines (71 loc) · 1.72 KB
/
decoder-callbacks.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package opus
// #include <opusfile.h>
import "C"
import (
"fmt"
"io"
"sync"
"unsafe"
)
var decoderId int
var decoderInstanceMap = make(map[int]*Decoder)
var decoderLock sync.Mutex
func getDecoderForId(id unsafe.Pointer) *Decoder {
decoderLock.Lock()
defer decoderLock.Unlock()
return decoderInstanceMap[int(uintptr(id))]
}
func getDecoderId(decoder *Decoder) unsafe.Pointer {
decoderLock.Lock()
defer decoderLock.Unlock()
decoderId++
decoderInstanceMap[decoderId] = decoder
return intToUnsafePointer(decoderId)
}
//export goFileRead
func goFileRead(stream unsafe.Pointer, ptr *C.uchar, nbytes C.int) C.int {
d := getDecoderForId(stream)
goPtr := (*byte)(ptr)
data := unsafe.Slice(goPtr, nbytes)
n, err := d.stream.Read(data)
if err != nil {
d.setErr(fmt.Errorf("failed to read from stream, %w", err))
return -1
}
return C.int(n)
}
//export goFileSeek
func goFileSeek(stream unsafe.Pointer, offset C.opus_int64, whence C.int) C.int {
d := getDecoderForId(stream)
var goWhence int
switch whence {
case C.SEEK_SET:
goWhence = io.SeekStart
case C.SEEK_CUR:
goWhence = io.SeekCurrent
case C.SEEK_END:
goWhence = io.SeekEnd
default:
d.setErr(fmt.Errorf("seek failed, unrecognised whence value %d", whence))
return -1
}
_, err := d.stream.Seek(int64(offset), goWhence)
if err != nil {
d.setErr(fmt.Errorf("failed to seek stream, %w", err))
return -1
}
return 0
}
//export goFileTell
func goFileTell(stream unsafe.Pointer) C.opus_int64 {
d := getDecoderForId(stream)
pos, err := d.stream.Seek(0, io.SeekCurrent)
if err != nil {
d.setErr(fmt.Errorf("failed to seek stream for tell, %w", err))
}
return C.opus_int64(pos)
}
//export goFileClose
func goFileClose(_ unsafe.Pointer) C.int {
return 0
}