2019-07-23 07:58:43 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-23 11:27:03 +03:00
|
|
|
"bytes"
|
|
|
|
"reflect"
|
2019-07-23 07:58:43 +03:00
|
|
|
"syscall/js"
|
2019-07-23 11:27:03 +03:00
|
|
|
"unsafe"
|
2019-07-23 07:58:43 +03:00
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/codec/adpcm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-07-23 11:27:03 +03:00
|
|
|
dec := NewDecoder()
|
|
|
|
dec.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decoder is a client side adpcm decoder
|
|
|
|
type Decoder struct {
|
2019-07-24 07:03:17 +03:00
|
|
|
inBuf []uint8
|
|
|
|
onImgLoadCb, initMemCb, getDecLenCb js.Func
|
|
|
|
console js.Value
|
|
|
|
done chan struct{}
|
2019-07-23 11:27:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDecoder returns a new instance of decoder
|
|
|
|
func NewDecoder() *Decoder {
|
|
|
|
return &Decoder{
|
|
|
|
console: js.Global().Get("console"),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Decoder) setupOnImgLoadCb() {
|
|
|
|
s.onImgLoadCb = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
|
|
// reader := bytes.NewReader(s.inBuf)
|
|
|
|
|
|
|
|
//DECODING HAPPENS HERE
|
|
|
|
|
|
|
|
decoded := bytes.NewBuffer(make([]byte, 0, len(s.inBuf)*4))
|
|
|
|
dec := adpcm.NewDecoder(decoded)
|
|
|
|
dec.Write(s.inBuf)
|
2019-07-23 07:58:43 +03:00
|
|
|
|
2019-07-23 11:27:03 +03:00
|
|
|
s.inBuf = decoded.Bytes()
|
2019-07-23 07:58:43 +03:00
|
|
|
|
2019-07-23 11:27:03 +03:00
|
|
|
return nil
|
|
|
|
})
|
2019-07-23 07:58:43 +03:00
|
|
|
}
|
|
|
|
|
2019-07-23 11:27:03 +03:00
|
|
|
func (s *Decoder) setupInitMemCb() {
|
|
|
|
// The length of the data array buffer is passed.
|
|
|
|
// Then the buf slice is initialized to that length.
|
|
|
|
// A pointer to that slice is passed back to the browser.
|
|
|
|
s.initMemCb = js.FuncOf(func(this js.Value, i []js.Value) interface{} {
|
|
|
|
length := i[0].Int()
|
|
|
|
s.console.Call("log", "length:", length)
|
|
|
|
s.inBuf = make([]uint8, length)
|
|
|
|
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&s.inBuf))
|
|
|
|
ptr := uintptr(unsafe.Pointer(hdr.Data))
|
|
|
|
s.console.Call("log", "ptr:", ptr)
|
|
|
|
js.Global().Call("gotMem", ptr)
|
|
|
|
return nil
|
|
|
|
})
|
2019-07-23 07:58:43 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 07:03:17 +03:00
|
|
|
func (s *Decoder) setupGetDecLenCb() {
|
|
|
|
s.getDecLenCb = js.FuncOf(func(this js.Value, i []js.Value) interface{} {
|
|
|
|
return len(s.inBuf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-23 11:27:03 +03:00
|
|
|
// Start sets up all the callbacks and waits for the close signal
|
|
|
|
// to be sent from the browser.
|
|
|
|
func (s *Decoder) Start() {
|
|
|
|
s.setupInitMemCb()
|
|
|
|
js.Global().Set("initMem", s.initMemCb)
|
|
|
|
s.setupOnImgLoadCb()
|
|
|
|
js.Global().Set("loadData", s.onImgLoadCb)
|
2019-07-24 07:03:17 +03:00
|
|
|
s.setupGetDecLenCb()
|
|
|
|
js.Global().Set("getDecLen", s.getDecLenCb)
|
2019-07-23 11:27:03 +03:00
|
|
|
<-s.done
|
2019-07-23 07:58:43 +03:00
|
|
|
}
|