-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediastream.go
116 lines (99 loc) · 2.52 KB
/
mediastream.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
package rawrtc
/*
#cgo CFLAGS : -g -I../../include
#cgo linux LDFLAGS: -L../../lib -ldl
#cgo windows LDFLAGS: -L../../lib
#include "api.h"
*/
import "C"
import (
"unsafe"
)
type MediaStream struct {
fd unsafe.Pointer
OnAddTrack func(track *MediaStreamTrack)
OnRemoveTrack func(track *MediaStreamTrack)
}
func (me *MediaStream) Init() *MediaStream {
return me
}
func (me *MediaStream) ID() string {
id := C.MediaStreamGetID(me.fd)
defer func() {
C.Free(unsafe.Pointer(id))
}()
return C.GoString(id)
}
func (me *MediaStream) AddTrack(track *MediaStreamTrack) bool {
eno := (int)(C.MediaStreamAddTrack(me.fd, track.fd))
if eno != 0 {
LogErrorf("Failed to MediaStreamAddTrack.")
return false
}
return true
}
func (me *MediaStream) RemoveTrack(track *MediaStreamTrack) bool {
eno := (int)(C.MediaStreamRemoveTrack(me.fd, track.fd))
if eno != 0 {
LogErrorf("Failed to MediaStreamRemoveTrack.")
return false
}
return true
}
func (me *MediaStream) GetAudioTracks() []*MediaStreamTrack {
var (
size C.size_t = 8
array = make([]unsafe.Pointer, 8)
tracks = make([]*MediaStreamTrack, 0)
)
C.MediaStreamGetAudioTracks(me.fd, &size, (*unsafe.Pointer)(&array[0]))
for i := 0; i < (int)(size); i++ {
track := new(MediaStreamTrack).Init()
track.fd = array[i]
tracks = append(tracks, track)
}
return tracks
}
func (me *MediaStream) GetVideoTracks() []*MediaStreamTrack {
var (
size C.size_t = 8
array = make([]unsafe.Pointer, 8)
tracks = make([]*MediaStreamTrack, 0)
)
C.MediaStreamGetVideoTracks(me.fd, &size, (*unsafe.Pointer)(&array[0]))
for i := 0; i < (int)(size); i++ {
track := new(MediaStreamTrack).Init()
track.fd = array[i]
tracks = append(tracks, track)
}
return tracks
}
func (me *MediaStream) FindAudioTrack(id string) *MediaStreamTrack {
track_id := C.CString(id)
defer func() {
C.free(unsafe.Pointer(track_id))
}()
track := new(MediaStreamTrack).Init()
track.fd = C.MediaStreamFindAudioTrack(me.fd, track_id)
if track.fd == nil {
LogErrorf("Failed to MediaStreamFindAudioTrack: id=%s", id)
return nil
}
return track
}
func (me *MediaStream) FindVideoTrack(id string) *MediaStreamTrack {
track_id := C.CString(id)
defer func() {
C.free(unsafe.Pointer(track_id))
}()
track := new(MediaStreamTrack).Init()
track.fd = C.MediaStreamFindVideoTrack(me.fd, track_id)
if track.fd == nil {
LogErrorf("Failed to MediaStreamFindVideoTrack: id=%s", id)
return nil
}
return track
}
func (me *MediaStream) Release() {
C.MediaStreamRelease(me.fd)
}