forked from xbee/go-toxcore
-
Notifications
You must be signed in to change notification settings - Fork 24
/
toxav.go
350 lines (289 loc) · 13 KB
/
toxav.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package tox
/*
#include <stdlib.h>
#include <string.h>
#include <vpx/vpx_image.h>
#include <tox/tox.h>
#include <tox/toxav.h>
void callbackCallWrapperForC(ToxAV *toxAV, uint32_t friend_number, bool audio_enabled,
bool video_enabled, void *user_data);
void callbackCallStateWrapperForC(ToxAV *toxAV, uint32_t friendNumber, uint32_t state, void* user_data);
void callbackAudioBitRateWrapperForC(ToxAV *toxAV, uint32_t friendNumber, uint32_t audioBitRate, void* user_data);
void callbackVideoBitRateWrapperForC(ToxAV *toxAV, uint32_t friendNumber, uint32_t videoBitRate, void* user_data);
void callbackAudioReceiveFrameWrapperForC(ToxAV *toxAV, uint32_t friendNumber, int16_t *pcm, size_t sample_count, uint8_t channels, uint32_t sampling_rate, void* user_data);
void callbackVideoReceiveFrameWrapperForC(ToxAV *toxAV, uint32_t friendNumber, uint16_t width,
uint16_t height, uint8_t *y, uint8_t *u, uint8_t *v,
int32_t ystride, int32_t ustride, int32_t vstride, void* user_data);
void callbackAudioForC(Tox *tox, uint32_t groupnumber, uint32_t peernumber, int16_t *pcm, unsigned int samples, uint8_t channels, uint32_t sample_rate, void *userdata);
extern void i420_to_rgb(int width, int height, const uint8_t *y, const uint8_t *u, const uint8_t *v,
int ystride, int ustride, int vstride, unsigned char *out);
extern void rgb_to_i420(unsigned char* rgb, vpx_image_t *img);
*/
import "C"
import (
"encoding/hex"
"errors"
"unsafe"
)
type cb_call_ftype func(this *ToxAV, friendNumber uint32, audioEnabled bool, videoEnabled bool, userData interface{})
type cb_call_state_ftype func(this *ToxAV, friendNumber uint32, state uint32, userData interface{})
type cb_audio_bit_rate_ftype func(this *ToxAV, friendNumber uint32, audioBitRate uint32, userData interface{})
type cb_video_bit_rate_ftype func(this *ToxAV, friendNumber uint32, videoBitRate uint32, userData interface{})
type cb_audio_receive_frame_ftype func(this *ToxAV, friendNumber uint32, pcm []byte, sampleCount int, channels int, samplingRate int, userData interface{})
type cb_video_receive_frame_ftype func(this *ToxAV, friendNumber uint32, width uint16, height uint16, data []byte, userData interface{})
type cb_audio_ftype func(this *Tox, groupNumber uint32, peerNumber uint32, pcm []byte, samples uint, channels uint8, sample_rate uint32, userData interface{})
type ToxAV struct {
tox *Tox
toxav *C.ToxAV
// session datas
out_image []byte
out_width C.uint16_t
out_hegith C.uint16_t
in_image *C.vpx_image_t
in_width C.uint16_t
in_height C.uint16_t
// callbacks
cb_call cb_call_ftype
cb_call_user_data interface{}
cb_call_state cb_call_state_ftype
cb_call_state_user_data interface{}
cb_audio_bit_rate cb_audio_bit_rate_ftype
cb_audio_bit_rate_user_data interface{}
cb_video_bit_rate cb_video_bit_rate_ftype
cb_video_bit_rate_user_data interface{}
cb_audio_receive_frame cb_audio_receive_frame_ftype
cb_audio_receive_frame_user_data interface{}
cb_video_receive_frame cb_video_receive_frame_ftype
cb_video_receive_frame_user_data interface{}
}
func NewToxAV(tox *Tox) (*ToxAV, error) {
if tox == nil {
return nil, toxerr("tox can not nil")
}
tav := new(ToxAV)
tav.tox = tox
var cerr C.Toxav_Err_New
tav.toxav = C.toxav_new(tox.toxcore, &cerr)
if cerr != 0 {
return nil, toxerr(cerr)
}
cbAVUserDatas.set(tav.toxav, tav)
return tav, nil
}
func (this *ToxAV) Kill() {
C.toxav_kill(this.toxav)
}
func (this *ToxAV) GetTox() *Tox {
return this.tox
}
func (this *ToxAV) IterationInterval() int {
return int(C.toxav_iteration_interval(this.toxav))
}
func (this *ToxAV) Iterate() {
C.toxav_iterate(this.toxav)
}
func (this *ToxAV) Call(friendNumber uint32, audioBitRate uint32, videoBitRate uint32) (bool, error) {
var cerr C.Toxav_Err_Call
r := C.toxav_call(this.toxav, C.uint32_t(friendNumber), C.uint32_t(audioBitRate), C.uint32_t(videoBitRate), &cerr)
if cerr != 0 {
return bool(r), toxerr(cerr)
}
return bool(r), nil
}
var cbAVUserDatas = newUserDataAV()
//export callbackCallWrapperForC
func callbackCallWrapperForC(m *C.ToxAV, friendNumber C.uint32_t, audioEnabled C.bool, videoEnabled C.bool, a3 unsafe.Pointer) {
var this = cbAVUserDatas.get(m)
if this.cb_call != nil {
this.cb_call(this, uint32(friendNumber), bool(audioEnabled), bool(videoEnabled), this.cb_call_user_data)
}
}
func (this *ToxAV) CallbackCall(cbfn cb_call_ftype, userData interface{}) {
this.cb_call = cbfn
this.cb_call_user_data = userData
var _cbfn = (*C.toxav_call_cb)(C.callbackCallWrapperForC)
C.toxav_callback_call(this.toxav, _cbfn, nil)
}
func (this *ToxAV) Answer(friendNumber uint32, audioBitRate uint32, videoBitRate uint32) (bool, error) {
var cerr C.Toxav_Err_Answer
r := C.toxav_answer(this.toxav, C.uint32_t(friendNumber), C.uint32_t(audioBitRate), C.uint32_t(videoBitRate), &cerr)
if cerr != C.TOXAV_ERR_ANSWER_OK {
return false, toxerr(cerr)
}
return bool(r), nil
}
//export callbackCallStateWrapperForC
func callbackCallStateWrapperForC(m *C.ToxAV, friendNumber C.uint32_t, state C.uint32_t, a3 unsafe.Pointer) {
var this = cbAVUserDatas.get(m)
if this.cb_call_state != nil {
this.cb_call_state(this, uint32(friendNumber), uint32(state), this.cb_call_state_user_data)
}
}
func (this *ToxAV) CallbackCallState(cbfn cb_call_state_ftype, userData interface{}) {
this.cb_call_state = cbfn
this.cb_call_state_user_data = userData
var _cbfn = (*C.toxav_call_state_cb)(C.callbackCallStateWrapperForC)
C.toxav_callback_call_state(this.toxav, _cbfn, nil)
}
func (this *ToxAV) CallControl(friendNumber uint32, control int) (bool, error) {
var cerr C.Toxav_Err_Call_Control
r := C.toxav_call_control(this.toxav, C.uint32_t(friendNumber), C.Toxav_Call_Control(control), &cerr)
if cerr != C.TOXAV_ERR_CALL_CONTROL_OK {
return bool(r), toxerr(cerr)
}
return bool(r), nil
}
func (this *ToxAV) AudioSetBitRate(friendNumber uint32, audioBitRate uint32) (bool, error) {
var cerr C.Toxav_Err_Bit_Rate_Set
r := C.toxav_audio_set_bit_rate(this.toxav, C.uint32_t(friendNumber), C.uint32_t(audioBitRate), &cerr)
if cerr != C.TOXAV_ERR_BIT_RATE_SET_OK {
return bool(r), toxerr(cerr)
}
return bool(r), nil
}
func (this *ToxAV) VideoSetBitRate(friendNumber uint32, videoBitRate uint32) (bool, error) {
var cerr C.Toxav_Err_Bit_Rate_Set
r := C.toxav_video_set_bit_rate(this.toxav, C.uint32_t(friendNumber), C.uint32_t(videoBitRate), &cerr)
if cerr != C.TOXAV_ERR_BIT_RATE_SET_OK {
return bool(r), toxerr(cerr)
}
return bool(r), nil
}
//export callbackAudioBitRateWrapperForC
func callbackAudioBitRateWrapperForC(m *C.ToxAV, friendNumber C.uint32_t, audioBitRate C.uint32_t, a3 unsafe.Pointer) {
var this = cbAVUserDatas.get(m)
if this.cb_audio_bit_rate != nil {
this.cb_audio_bit_rate(this, uint32(friendNumber), uint32(audioBitRate), this.cb_call_state_user_data)
}
}
func (this *ToxAV) CallbackAudioBitRate(cbfn cb_audio_bit_rate_ftype, userData interface{}) {
this.cb_audio_bit_rate = cbfn
this.cb_audio_bit_rate_user_data = userData
var _cbfn = (*C.toxav_audio_bit_rate_cb)(C.callbackAudioBitRateWrapperForC)
C.toxav_callback_audio_bit_rate(this.toxav, _cbfn, nil)
}
//export callbackVideoBitRateWrapperForC
func callbackVideoBitRateWrapperForC(m *C.ToxAV, friendNumber C.uint32_t, videoBitRate C.uint32_t, a3 unsafe.Pointer) {
var this = cbAVUserDatas.get(m)
if this.cb_video_bit_rate != nil {
this.cb_video_bit_rate(this, uint32(friendNumber), uint32(videoBitRate), this.cb_call_state_user_data)
}
}
func (this *ToxAV) CallbackVideoBitRate(cbfn cb_video_bit_rate_ftype, userData interface{}) {
this.cb_video_bit_rate = cbfn
this.cb_video_bit_rate_user_data = userData
var _cbfn = (*C.toxav_video_bit_rate_cb)(C.callbackVideoBitRateWrapperForC)
C.toxav_callback_video_bit_rate(this.toxav, _cbfn, nil)
}
func (this *ToxAV) AudioSendFrame(friendNumber uint32, pcm []byte, sampleCount int, channels int, samplingRate int) (bool, error) {
pcm_ := (*C.int16_t)(unsafe.Pointer(&pcm[0]))
var cerr C.Toxav_Err_Send_Frame
r := C.toxav_audio_send_frame(this.toxav, C.uint32_t(friendNumber), pcm_, C.size_t(sampleCount), C.uint8_t(channels), C.uint32_t(samplingRate), &cerr)
if cerr != C.TOXAV_ERR_SEND_FRAME_OK {
return false, toxerr(cerr)
}
return bool(r), nil
}
func (this *ToxAV) VideoSendFrame(friendNumber uint32, width uint16, height uint16, data []byte) (bool, error) {
if this.in_image != nil && (uint16(this.in_width) != width || uint16(this.in_height) != height) {
C.vpx_img_free(this.in_image)
this.in_image = nil
}
if this.in_image == nil {
this.in_width = C.uint16_t(width)
this.in_height = C.uint16_t(height)
this.in_image = C.vpx_img_alloc(nil, C.VPX_IMG_FMT_I420, C.uint(this.in_width), C.uint(this.in_height), 1)
}
C.rgb_to_i420((*C.uchar)(unsafe.Pointer(&data[0])), this.in_image)
var cerr C.Toxav_Err_Send_Frame
r := C.toxav_video_send_frame(this.toxav, C.uint32_t(friendNumber), C.uint16_t(width), C.uint16_t(height),
(*C.uint8_t)(this.in_image.planes[0]),
(*C.uint8_t)(this.in_image.planes[1]),
(*C.uint8_t)(this.in_image.planes[2]),
&cerr)
if cerr != C.TOXAV_ERR_SEND_FRAME_OK {
return false, toxerr(cerr)
}
return bool(r), nil
}
//export callbackAudioReceiveFrameWrapperForC
func callbackAudioReceiveFrameWrapperForC(m *C.ToxAV, friendNumber C.uint32_t, pcm *C.int16_t, sampleCount C.size_t, channels C.uint8_t, samplingRate C.uint32_t, a3 unsafe.Pointer) {
var this = cbAVUserDatas.get(m)
if this.cb_audio_receive_frame != nil {
length := sampleCount * C.size_t(channels) * 2
pcm_p := unsafe.Pointer(pcm)
pcm_b := C.GoBytes(pcm_p, C.int(length))
this.cb_audio_receive_frame(this, uint32(friendNumber), pcm_b, int(sampleCount), int(channels), int(samplingRate), this.cb_audio_receive_frame_user_data)
}
}
func (this *ToxAV) CallbackAudioReceiveFrame(cbfn cb_audio_receive_frame_ftype, userData interface{}) {
this.cb_audio_receive_frame = cbfn
this.cb_audio_receive_frame_user_data = userData
var _cbfn = (*C.toxav_audio_receive_frame_cb)(C.callbackAudioReceiveFrameWrapperForC)
C.toxav_callback_audio_receive_frame(this.toxav, _cbfn, nil)
}
//export callbackVideoReceiveFrameWrapperForC
func callbackVideoReceiveFrameWrapperForC(m *C.ToxAV, friendNumber C.uint32_t, width C.uint16_t, height C.uint16_t, y *C.uint8_t, u *C.uint8_t, v *C.uint8_t, ystride C.int32_t, ustride C.int32_t, vstride C.int32_t, a3 unsafe.Pointer) {
var this = cbAVUserDatas.get(m)
if this.cb_video_receive_frame != nil {
if this.out_image != nil && (this.out_width != width || this.out_hegith != height) {
this.out_image = nil
}
var buf_size int = int(width) * int(height) * 3
if this.out_image == nil {
this.out_width = width
this.out_hegith = height
this.out_image = make([]byte, buf_size, buf_size)
}
out := unsafe.Pointer(&(this.out_image[0]))
C.i420_to_rgb(C.int(width), C.int(height), y, u, v, C.int(ystride), C.int(ustride), C.int(vstride), (*C.uchar)(out))
this.cb_video_receive_frame(this, uint32(friendNumber), uint16(width), uint16(height), this.out_image, this.cb_video_receive_frame_user_data)
}
}
func (this *ToxAV) CallbackVideoReceiveFrame(cbfn cb_video_receive_frame_ftype, userData interface{}) {
this.cb_video_receive_frame = cbfn
this.cb_video_receive_frame_user_data = userData
var _cbfn = (*C.toxav_video_receive_frame_cb)(C.callbackVideoReceiveFrameWrapperForC)
C.toxav_callback_video_receive_frame(this.toxav, _cbfn, nil)
}
//export callbackAudioForC
func callbackAudioForC(m *C.Tox, groupnumber C.uint32_t, peernumber C.uint32_t, pcm *C.int16_t, samples C.uint, channels C.uint8_t, sample_rate C.uint32_t, userdata unsafe.Pointer) {
var this = cbUserDatas.get(m)
if _cbfnx, ok := this.cb_audios[uint32(groupnumber)]; ok && _cbfnx != nil {
_cbfn := (_cbfnx).(cb_audio_ftype)
this.putcbevts(func() {
blen := C.int(samples) * C.int(channels) * 2
pcm_ := C.GoBytes(unsafe.Pointer(pcm), blen) // TODO copy memory improve
_cbfn(this, uint32(groupnumber), uint32(peernumber), pcm_, uint(samples), uint8(channels), uint32(sample_rate), nil)
})
}
}
func (this *Tox) AddAVGroupChat(cbfn cb_audio_ftype) uint32 {
r := C.toxav_add_av_groupchat(this.toxcore, (*C.toxav_audio_data_cb)(unsafe.Pointer(C.callbackAudioForC)), nil)
if cbfn != nil {
this.cb_audios[uint32(r)] = cbfn
}
return uint32(r)
}
func (this *Tox) JoinAVGroupChat(friendNumber uint32, cookie string, cbfn cb_audio_ftype) (uint32, error) {
data, err := hex.DecodeString(cookie)
if err != nil {
return 0, errors.New("Invalid cookie:" + cookie)
}
var _fn = C.uint32_t(friendNumber)
var _data = (*C.uint8_t)((unsafe.Pointer)(&data[0]))
var length = len(data)
var _length = C.uint16_t(length)
r := C.toxav_join_av_groupchat(this.toxcore, _fn, _data, _length,
(*C.toxav_audio_data_cb)(unsafe.Pointer(C.callbackAudioForC)), nil)
if int(r) == -1 {
return uint32(r), errors.New("Join av group chat failed")
}
if cbfn != nil {
this.cb_audios[uint32(r)] = cbfn
}
if this.hooks.ConferenceJoin != nil {
this.hooks.ConferenceJoin(friendNumber, uint32(r), cookie)
}
return uint32(r), nil
}