-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_darwin.go
More file actions
149 lines (125 loc) · 3.15 KB
/
Copy pathcamera_darwin.go
File metadata and controls
149 lines (125 loc) · 3.15 KB
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
package camera
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework AVFoundation -framework CoreMedia -framework Accelerate
#include "camera.h"
// Global device pointer
static webcam_device_t* _device = NULL;
static inline size_t webcam_format_size() {
return (size_t)(_device->width) * (uint32_t)(_device->height) * 4;
}
// Forward declaration of the Go callback function
extern void onFrameAvailableGo(void* data, int width, int height);
// Callback function
static void callback(webcam_device_t* device, void* data) {
onFrameAvailableGo(data, device->width, device->height);
}
static int webcam_open(int deviceId, int width, int height, int framerate) {
if (_device != NULL) {
return -1;
}
_device = (webcam_device_t*)malloc(sizeof(webcam_device_t));
if (!_device) {
return -1;
}
memset(_device, 0, sizeof(*_device));
_device->deviceId = deviceId;
_device->width = width;
_device->height = height;
_device->framerate = framerate;
// already setup
if (_device->stream) {
return -1;
}
WebcamVideoStream* stream = [[WebcamVideoStream alloc] init];
stream->_parent = _device;
BOOL res = [stream setupWithID:_device->deviceId rate:_device->framerate width:_device->width height:_device->height];
if (res == NO) {
_device->stream = NULL;
return -1;
}
_device->stream = stream;
_device->width = stream->_width;
_device->height = stream->_height;
_device->deviceId = stream->_id;
_device->framerate = stream->_framerate;
_device->callback = callback;
return 0;
}
static int webcam_start() {
if (_device == NULL) {
return -1;
}
if (_device->stream && _device->running == 0) {
WebcamVideoStream* stream = (WebcamVideoStream*)(_device->stream);
[stream start];
_device->running = 1;
return 0;
}
return -1;
}
static int webcam_stop() {
if (_device && _device->stream && _device->running == 1) {
WebcamVideoStream* stream = (WebcamVideoStream*)(_device->stream);
[stream stop];
_device->running = 0;
return 0;
}
return -1;
}
static void webcam_delete() {
if (_device == NULL) {
return;
}
if (_device->running == 1) {
webcam_stop();
}
if (_device->stream) {
WebcamVideoStream* stream = (WebcamVideoStream*)(_device->stream);
[stream release];
}
free(_device);
_device = NULL;
}
*/
import "C"
import (
"fmt"
"unsafe"
)
//export onFrameAvailableGo
func onFrameAvailableGo(data unsafe.Pointer, w, h C.int) {
go func() {
size := C.webcam_format_size()
buf := make([]byte, size)
C.copyImage((*C.uint8_t)(unsafe.Pointer(&buf[0])), data, size)
// Convert the buffer to an image.RGBA
rgba := convertBGRAToRGBA(buf, int(w), int(h))
select {
case frameBufferChan <- rgba:
default:
// Drop the frame if the channel is full
}
}()
}
func openCamera(id, width, height int) error {
if C.webcam_open(C.int(id), C.int(width), C.int(height), 30) != 0 {
return fmt.Errorf("failed to initialize camera")
}
return nil
}
func startCamera() error {
if C.webcam_start() != 0 {
return fmt.Errorf("failed to start camera")
}
return nil
}
func stopCamera() error {
if C.webcam_stop() != 0 {
return fmt.Errorf("failed to stop camera")
}
return nil
}
func closeCamera() {
C.webcam_delete()
}