-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture_darwin.go
189 lines (165 loc) · 3.87 KB
/
capture_darwin.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
package gdesk
/*
#cgo LDFLAGS: -framework CoreGraphics -framework CoreFoundation -framework IOSurface
#include "capture_darwin.h"
*/
import "C"
import (
"errors"
"fmt"
"reflect"
"runtime"
"sync/atomic"
"time"
"unsafe"
)
type Display struct {
inner C.CGDirectDisplayID
}
func GetPrimaryDisplay() *Display {
return &Display{inner: C.CGMainDisplayID()}
}
func (d *Display) String() string {
return fmt.Sprintf("%d", d.inner)
}
func (d *Display) Width() uint {
return uint(C.CGDisplayPixelsWide(d.inner))
}
func (d *Display) Height() uint {
return uint(C.CGDisplayPixelsHigh(d.inner))
}
type Frame struct {
inner C.IOSurfaceRef
data []byte
inited int32
}
func NewFrame(surface C.IOSurfaceRef) *Frame {
frame := &Frame{
inner: surface,
}
C.CFRetain(C.CFTypeRef(surface))
C.IOSurfaceIncrementUseCount(surface)
C.IOSurfaceLock(surface, C.kIOSurfaceLockReadOnly, nil)
runtime.SetFinalizer(frame, (*Frame).Release)
frame.inited = 1
return frame
}
func (f *Frame) convert(w, h int) {
surface := f.inner
plane0_addr := C.IOSurfaceGetBaseAddressOfPlane(surface, 0)
plane0_l := int(C.IOSurfaceGetBytesPerRowOfPlane(surface, 0))
plane1_addr := C.IOSurfaceGetBaseAddressOfPlane(surface, 1)
plane1_l := int(C.IOSurfaceGetBytesPerRowOfPlane(surface, 1))
plane0_header := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(plane0_addr)),
Cap: plane0_l,
Len: plane0_l,
}
plane1_header := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(plane1_addr)),
Cap: plane1_l,
Len: plane1_l,
}
plane0 := *(*[]byte)(unsafe.Pointer(&plane0_header))
plane1 := *(*[]byte)(unsafe.Pointer(&plane1_header))
f.data = nv12_to_i420(plane0, plane1, w, h)
}
func (f *Frame) Release() {
if atomic.LoadInt32(&f.inited) == 0 {
return
}
for {
if atomic.CompareAndSwapInt32(&f.inited, f.inited, 0) {
break
}
}
C.IOSurfaceUnlock(f.inner, C.kIOSurfaceLockReadOnly, nil)
C.IOSurfaceDecrementUseCount(f.inner)
C.CFRelease(C.CFTypeRef(f.inner))
}
type Capturer struct {
ref unsafe.Pointer
queue C.dispatch_queue_t
dict C.CFDictionaryRef
stream C.CGDisplayStreamRef
framePin int32
stopped int32
frame unsafe.Pointer
dW, dH int
}
//export CaptureStop
func CaptureStop(c unsafe.Pointer) {
var cap = (*Capturer)(c)
for {
if atomic.CompareAndSwapInt32(&cap.stopped, cap.stopped, 1) {
break
}
}
}
//export CaptureComplete
func CaptureComplete(c unsafe.Pointer, surf C.IOSurfaceRef) {
var cap = (*Capturer)(c)
frame := unsafe.Pointer(NewFrame(surf))
for {
if atomic.CompareAndSwapPointer(&cap.frame, cap.frame, frame) {
break
}
}
}
func NewCapturer() *Capturer {
return &Capturer{
stopped: 1,
framePin: 0,
}
}
func (cap *Capturer) GetFrame() *Frame {
if atomic.LoadInt32(&cap.framePin) == 1 {
return nil
}
for {
if atomic.CompareAndSwapInt32(&cap.framePin, cap.framePin, 1) {
break
}
}
f := (*Frame)(atomic.LoadPointer(&cap.frame))
if f != nil {
f.convert(cap.dW, cap.dH)
}
for {
if atomic.CompareAndSwapInt32(&cap.framePin, cap.framePin, 0) {
break
}
}
return f
}
func (cap *Capturer) Start(display *Display) error {
cap.queue = C.dispatch_queue_create(C.CString("capture queue"), C.dispatch_queue_attr_t(nil))
cap.dict = C.dict_create(0.0, 8, 0, 1)
w := C.uint(display.Width())
h := C.uint(display.Height())
cap.stream = C.DisplayStreamCreateWithDispatchQueue(unsafe.Pointer(cap), display.inner, w, h, cap.dict, cap.queue)
rs := C.CGDisplayStreamStart(cap.stream)
if rs != C.kCGErrorSuccess {
return errors.New(fmt.Sprintf("error start stream, code %d", int(rs)))
}
for {
if atomic.CompareAndSwapInt32(&cap.stopped, cap.stopped, 0) {
break
}
}
cap.dW = int(w)
cap.dH = int(h)
return nil
}
func (cap *Capturer) Stop() {
C.CGDisplayStreamStop(cap.stream)
for {
if atomic.LoadInt32(&cap.stopped) == 1 {
break
}
}
time.Sleep(30 * time.Millisecond)
C.q_release(cap.queue)
C.dict_release(cap.dict)
return
}