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
|
#define BACKEND_NAME "visca"
#define DEBUG
#include <string.h>
#include "visca.h"
#include "libmmbackend.h"
/* TODO
* VISCA server
*/
MM_PLUGIN_API int init(){
backend ptz = {
.name = BACKEND_NAME,
.conf = ptz_configure,
.create = ptz_instance,
.conf_instance = ptz_configure_instance,
.channel = ptz_channel,
.handle = ptz_set,
.process = ptz_handle,
.start = ptz_start,
.shutdown = ptz_shutdown
};
//register backend
if(mm_backend_register(ptz)){
LOG("Failed to register backend");
return 1;
}
return 0;
}
static int ptz_configure(char* option, char* value){
LOG("No backend configuration possible");
return 1;
}
static int ptz_configure_instance(instance* inst, char* option, char* value){
char* host = NULL, *port = NULL, *options = NULL;
ptz_instance_data* data = (ptz_instance_data*) inst->impl;
uint8_t mode = 0;
if(!strcmp(option, "id")){
data->cam_address = strtoul(value, NULL, 10);
return 0;
}
else if(!strcmp(option, "connect")){
if(data->fd >= 0){
LOGPF("Instance %s already connected", inst->name);
return 1;
}
mmbackend_parse_hostspec(value, &host, &port, &options);
if(!host || !port){
LOGPF("Invalid destination address specified for instance %s", inst->name);
return 1;
}
if(options && !strcmp(options, "udp")){
mode = 1;
}
data->fd = mmbackend_socket(host, port, mode ? SOCK_DGRAM : SOCK_STREAM, 0, 0, 1);
if(data->fd < 0){
LOGPF("Failed to connect instance %s", inst->name);
return 1;
}
return 0;
}
else if(!strcmp(option, "device")){
if(data->fd >= 0){
LOGPF("Instance %s already connected", inst->name);
return 1;
}
#ifdef _WIN32
LOGPF("Direct device connections are not possible on Windows");
return 1;
#else
data->fd = open(value, O_NONBLOCK);
if(data->fd < 0){
LOGPF("Failed to connect instance %s to device %s", inst->name, value);
return 1;
}
return 0;
#endif
}
LOGPF("Unknown instance configuration parameter %s for instance %s", option, inst->name);
return 1;
}
static int ptz_instance(instance* inst){
ptz_instance_data* data = calloc(1, sizeof(ptz_instance_data));
if(!data){
LOG("Failed to allocate memory");
return 1;
}
data->fd = -1;
data->cam_address = 1;
//start with maximum speeds
data->panspeed = ptz_channels[panspeed].max;
data->tiltspeed = ptz_channels[tiltspeed].max;
inst->impl = data;
return 0;
}
static channel* ptz_channel(instance* inst, char* spec, uint8_t flags){
uint64_t ident = pan;
size_t command = 0;
if(flags & mmchannel_input){
LOG("This backend currently only supports output channels");
return NULL;
}
for(command = 0; command < sentinel; command++){
if(!strncmp(spec, ptz_channels[command].name, strlen(ptz_channels[command].name))){
ident = command;
}
}
if(ident == sentinel){
LOGPF("Unknown channel spec %s", spec);
return NULL;
}
//store the memory to be called above the command type
if(ident == call){
ident |= (strtoul(spec + strlen(ptz_channels[call].name), NULL, 10) << 8);
}
return mm_channel(inst, ident, 1);
}
static size_t ptz_set_pantilt(instance* inst, channel* c, channel_value* v, uint8_t* msg){
ptz_instance_data* data = (ptz_instance_data*) inst->impl;
uint32_t* x = (uint32_t*) (msg + 6);
uint32_t* y = (uint32_t*) (msg + 10);
if(c->ident == pan){
data->x = ((ptz_channels[pan].max - ptz_channels[pan].min) * v->normalised) + ptz_channels[pan].min;
}
else{
data->y = ((ptz_channels[tilt].max - ptz_channels[tilt].min) * v->normalised) + ptz_channels[tilt].min;
}
msg[4] = data->panspeed;
msg[5] = data->tiltspeed;
*x = htobe32(data->x);
*y = htobe32(data->y);
return ptz_channels[pan].bytes;
}
static size_t ptz_set_ptspeed(instance* inst, channel* c, channel_value* v, uint8_t* msg){
ptz_instance_data* data = (ptz_instance_data*) inst->impl;
if(c->ident == panspeed){
data->panspeed = ((ptz_channels[panspeed].max - ptz_channels[panspeed].min) * v->normalised) + ptz_channels[panspeed].min;
}
else{
data->tiltspeed = ((ptz_channels[tiltspeed].max - ptz_channels[tiltspeed].min) * v->normalised) + ptz_channels[tiltspeed].min;
}
return 0;
}
static size_t ptz_set_zoom(instance* inst, channel* c, channel_value* v, uint8_t* msg){
uint32_t* position = (uint32_t*) (msg + 4);
*position = htobe32(((ptz_channels[zoom].max - ptz_channels[zoom].min) * v->normalised) + ptz_channels[zoom].min);
return ptz_channels[zoom].bytes;
}
static size_t ptz_set_focus(instance* inst, channel* c, channel_value* v, uint8_t* msg){
uint32_t* position = (uint32_t*) (msg + 4);
*position = htobe32(((ptz_channels[focus].max - ptz_channels[focus].min) * v->normalised) + ptz_channels[focus].min);
return ptz_channels[focus].bytes;
}
static size_t ptz_set_memory(instance* inst, channel* c, channel_value* v, uint8_t* msg){
if(v->normalised < 0.9){
return 0;
}
msg[5] = (c->ident >> 8);
return ptz_channels[call].bytes;
}
static int ptz_set(instance* inst, size_t num, channel** c, channel_value* v){
ptz_instance_data* data = (ptz_instance_data*) inst->impl;
size_t n = 0, bytes = 0;
uint8_t tx[VISCA_BUFFER_LENGTH] = "";
uint8_t command = 0;
for(n = 0; n < num; n++){
bytes = 0;
command = c[n]->ident & 0xFF;
if(ptz_channels[command].bytes){
memcpy(tx, ptz_channels[command].pattern, ptz_channels[command].bytes);
}
tx[0] = 0x80 | (data->cam_address & 0xF);
if(ptz_channels[command].set){
bytes = ptz_channels[command].set(inst, c[n], v + n, tx);
}
if(bytes && mmbackend_send(data->fd, tx, bytes)){
LOGPF("Failed to push %s command on instance %s", ptz_channels[command].name, inst->name);
}
}
return 0;
}
static int ptz_handle(size_t num, managed_fd* fds){
//no events generated here
return 0;
}
static int ptz_start(size_t n, instance** inst){
//no startup needed yet
return 0;
}
static int ptz_shutdown(size_t n, instance** inst){
size_t u;
ptz_instance_data* data = NULL;
for(u = 0; u < n; u++){
data = (ptz_instance_data*) inst[u]->impl;
if(data->fd >= 0){
close(data->fd);
}
free(data);
inst[u]->impl = NULL;
}
LOG("Backend shut down");
return 0;
}
|