-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDelicodeSyphonServer.mm
663 lines (556 loc) · 17 KB
/
DelicodeSyphonServer.mm
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
Vidiot id a VIDeo Input Output Transformer With a Touch of Functionality
Copyright (C) 2016 Delicode Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DelicodeSyphonServer.h"
#include "QImage.h"
#import "Lib/Syphon.framework/Headers/Syphon.h"
#include <qdebug.h>
DelicodeSyphonServer::DelicodeSyphonServer()
{
mSyphon = nil;
has_clients = false;
recreated = false;
hosting_name = "";
memset(server_name, '\0', 2048*sizeof(unsigned char));
has_quit = false;
context_is_valid = false;
has_started = false;
}
DelicodeSyphonServer::~DelicodeSyphonServer()
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
if(mSyphon) {
[(SyphonServer *)mSyphon stop];
[(SyphonServer *)mSyphon release];
}
[pool drain];
}
bool DelicodeSyphonServer::createServer(char *name)
{
sprintf(server_name, "%s", name);
GLenum err;
NSString* title = [NSString stringWithCString:server_name];
if (!mSyphon)
{
recreated = true;
CGLContextObj cgl_ctx = CGLGetCurrentContext();
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "Syphon create server: error getting OpenGL context: " << err;
}
if (cgl_ctx == NULL)
{
context_is_valid = false;
return false;
}
mSyphon = [[SyphonServer alloc] initWithName:title context:CGLGetCurrentContext() options:nil];
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "Syphon create server: OpenGL error initting: " << err;
}
hosting_name = ([title UTF8String]);
if ([(SyphonServer *)mSyphon bindToDrawFrameOfSize:(NSSize){512, 256}] == YES)
[(SyphonServer *)mSyphon unbindAndPublish];
[(SyphonServer *)mSyphon setName:title];
context_is_valid = true;
has_started = true;
}
else
recreated = false;
if (mSyphon == nil)
return false;
return true;
}
bool DelicodeSyphonServer::publishScreen()
{
CGLContextObj cgl_ctx = CGLGetCurrentContext();
GLint dims[4];
if (cgl_ctx == NULL)
{
context_is_valid = false;
return false;
}
context_is_valid = true;
glPushAttrib(GL_TEXTURE_BIT);
glGetIntegerv(GL_VIEWPORT, dims);
if (!mSyphon)
{
CGFloat dims_x = dims[2];
CGFloat dims_y = dims[3];
NSString* title = [NSString stringWithCString:server_name];
mSyphon = [[SyphonServer alloc] initWithName:title context:CGLGetCurrentContext() options:nil];
[(SyphonServer *)mSyphon bindToDrawFrameOfSize:(NSSize){dims_x, dims_y}];
[(SyphonServer *)mSyphon unbindAndPublish];
recreated = true;
}
else
recreated = false;
SyphonImage* img = [(SyphonServer *)mSyphon newFrameImage];
if(img) {
glReadBuffer(GL_FRONT);
glBindTexture(GL_TEXTURE_RECTANGLE_EXT, [img textureName]);
glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, 0, 0, dims[0], dims[1], dims[2], dims[3]);
CGFloat dims_x = dims[2];
CGFloat dims_y = dims[3];
[(SyphonServer *)mSyphon bindToDrawFrameOfSize:(NSSize){dims_x,dims_y}];
[(SyphonServer *)mSyphon unbindAndPublish];
[img release];
glPopAttrib();
return true;
}
else {
glPopAttrib();
[(SyphonServer *)mSyphon stop];
[(SyphonServer *)mSyphon release];
mSyphon = nil;
has_quit = true;
return false;
}
}
bool DelicodeSyphonServer::bindFBO(int res_x, int res_y)
{
if (!mSyphon)
{
return false;
}
SyphonServer* s = (SyphonServer *)mSyphon;
CGFloat dims_x = res_x;
CGFloat dims_y = res_y;
if ([s bindToDrawFrameOfSize:(NSSize){dims_x, dims_y}] == NO)
return false;
return true;
}
bool DelicodeSyphonServer::unbindFBO()
{
if (!mSyphon)
{
return false;
}
SyphonServer* s = (SyphonServer *)mSyphon;
[s unbindAndPublish];
return true;
}
bool DelicodeSyphonServer::publishFBO(unsigned int id, int res_x, int res_y, bool flip)
{
// reset errors before calling the functions
GLenum err = glGetError();
if(id)
{
CGLContextObj cgl_ctx = CGLGetCurrentContext();
if (cgl_ctx == NULL)
{
context_is_valid = false;
return false;
}
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "error getting current context: " << err;
}
if (!mSyphon)
{
const char *name = "Render";
context_is_valid = true;
NSString* title = [NSString stringWithCString:name];
mSyphon = [[SyphonServer alloc] initWithName:title context:CGLGetCurrentContext() options:nil];
if (!mSyphon)
{
return false;
}
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "error initting syphon: " << err;
}
}
NSSize res;
res.width = 1024;
res.height = 1024;
SyphonServer* serv = (SyphonServer *)mSyphon;
int retcode = 0;
[serv delicode_publishFBO:NSMakeSize(res_x,res_y) fbo_id:id];
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "error after publish fbo: " << err;
}
}
else
{
[(SyphonServer *)mSyphon stop];
[(SyphonServer *)mSyphon release];
mSyphon = nil;
return false;
}
return true;
}
bool DelicodeSyphonServer::startServing()
{
GLenum err;
NSString* title = [NSString stringWithCString:server_name];
if (!mSyphon)
{
recreated = true;
CGLContextObj cgl_ctx = CGLGetCurrentContext();
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "Syphon start serving: OpenGL error getting context: " << err;
}
if (cgl_ctx == NULL)
{
context_is_valid = false;
return false;
}
mSyphon = [[SyphonServer alloc] initWithName:title context:CGLGetCurrentContext() options:nil];
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "Syphon start serving: OpenGL error initting: " << err;
}
hosting_name = ([title UTF8String]);
if ([(SyphonServer *)mSyphon bindToDrawFrameOfSize:(NSSize){512, 256}] == YES)
[(SyphonServer *)mSyphon unbindAndPublish];
[(SyphonServer *)mSyphon setName:title];
context_is_valid = true;
}
else
recreated = false;
if (mSyphon == nil)
return false;
return true;
}
bool DelicodeSyphonServer::publishTexture(unsigned int id, int res_x, int res_y, bool flip)
{
GLenum err;
if (!has_started)
startServing();
CGLContextObj cgl_ctx = CGLGetCurrentContext();
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "Syphon publish texture: error in opengl get context: " << err;
}
if (cgl_ctx == NULL)
{
context_is_valid = false;
qDebug() << "Syphon publish texture: opengl context is invalid";
return false;
}
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "syphon publish texture error: " << err;
}
if(id) {
if (!mSyphon)
{
recreated = true;
context_is_valid = true;
NSString* title = [NSString stringWithCString:server_name];
mSyphon = [[SyphonServer alloc] initWithName:title context:CGLGetCurrentContext() options:nil];
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "Syphon publish texture: OpenGL error initting: " << err;
}
}
else
recreated = false;
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "Syphon publish texture: error before " << err;
}
SyphonServer* s = (SyphonServer *)mSyphon;
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "Syphon publish texture: error after getting syphon " << err;
}
//if ([s bindToDrawFrameOfSize:(NSSize){512, 256}] == YES)
// [s unBindAndPublish];
test_name = [s.name UTF8String];
[s publishFrameTexture:id textureTarget:GL_TEXTURE_2D imageRegion:NSMakeRect(0,0,res_x,res_y) textureDimensions:NSMakeSize(res_x,res_y) flipped:flip];
err = glGetError();
if (err != GL_NO_ERROR)
{
qDebug() << "Syphon publish texture: error after publish: " << err;
}
if (s.hasClients == NO)
has_clients = false;
else
has_clients = true;
return true;
}
else {
[(SyphonServer *)mSyphon stop];
[(SyphonServer *)mSyphon release];
mSyphon = nil;
has_quit = true;
return false;
}
}
void DelicodeSyphonServer::getServers(char *name_list)
{
NSArray *server_list = [[SyphonServerDirectory sharedDirectory] servers];
sprintf(name_list, "");
int unknown = 1;
for(NSDictionary *server in server_list) {
NSString* app_name = [server objectForKey:SyphonServerDescriptionAppNameKey];
NSString* server_name = [server objectForKey:SyphonServerDescriptionNameKey];
const char *orig_app_str = [app_name UTF8String];
const char *orig_server_str = [server_name UTF8String];
char app_str[1024]; strcpy(app_str, orig_app_str);
char server_str[1024]; strcpy(server_str, orig_server_str);
for(int i=0; i<strlen(app_str); i++) {
if(app_str[i] == '|')
app_str[i] = '.';
}
for(int i=0; i<strlen(server_str); i++) {
if(server_str[i] == '|')
server_str[i] = '.';
}
qDebug() << "found server: " << server_str << ", app str: " << app_str;
if(strlen(app_str) == 0 && strlen(server_str) == 0)
sprintf(name_list + strlen(name_list), "Unknown application %i|", unknown++);
else if(strlen(server_str) == 0)
sprintf(name_list + strlen(name_list), "%s|", app_str);
else
sprintf(name_list + strlen(name_list), "%s - %s|", app_str, server_str);
}
}
DelicodeSyphonClient::DelicodeSyphonClient(const char* name)
{
mSyphon = nil;
mFrame = nil;
texID = 0;
width = 1;
height = 1;
number_of_servers = 0;
mFbo = 0;
if (name != NULL && strlen(name) > 0)
strcpy(server_name, name);
//qDebug() << "Will search for server named " << QString::fromStdString(std::string(server_name));
number_of_servers = 0;
isConnected = false;
}
void DelicodeSyphonClient::setName(const char* name)
{
strcpy(server_name, name);
}
bool DelicodeSyphonClient::connectToServer()
{
NSArray *server_list = [[SyphonServerDirectory sharedDirectory] servers];
for(NSDictionary *server in server_list) {
NSString* sname = [server objectForKey:SyphonServerDescriptionAppNameKey];
const char *orig_server_str = [sname UTF8String];
char server_str[1024]; strcpy(server_str, orig_server_str);
if (strcmp(server_str, server_name) == 0)
{
mSyphon = [[SyphonClient alloc] initWithServerDescription:server options:nil newFrameHandler:nil];
if ( ((SyphonClient *)mSyphon).isValid == NO)
{
qDebug() << "Syphon connect to server: Could not connect to server: " << server_name;
isConnected = false;
return false;
}
qDebug() << "Syphon connect to server: Connected to server successfully: " << server_name;
SyphonClient* c;
c = ((SyphonClient *)mSyphon);
NSString* temp1 = c.serverDescription[SyphonServerDescriptionUUIDKey];
std::string temp2 = [temp1 UTF8String];
//qDebug() << "Connected server uuid is: " << temp2.c_str();
isConnected = true;
return true;
}
else
{
qDebug() << "server name not matching: " << server_str << " vs " << server_name;
}
}
qDebug() << "no server found with name: " << server_name;
if (mSyphon != nil)
[(SyphonClient *)mSyphon release];
return false;
}
void DelicodeSyphonClient::getServers()
{
number_of_servers = 0;
NSArray* server_list = [[SyphonServerDirectory sharedDirectory] servers];
NSString* search_name_nsstring = [NSString stringWithCString:server_name];
std::string cppString([search_name_nsstring UTF8String]);
for(NSDictionary *server in server_list) {
NSString* app_name = [server objectForKey:SyphonServerDescriptionAppNameKey];
NSString* srv_name = [server objectForKey:SyphonServerDescriptionNameKey];
number_of_servers++;
if ([srv_name rangeOfString:search_name_nsstring].location != NSNotFound)
{
std::string c([srv_name UTF8String]);
//qDebug() << "Found server with proper name: " << QString::fromStdString(c);
//sprintf(assigned_server, "%s", &c[0]);
}
else
{
std::string c([srv_name UTF8String]);
//qDebug() << "Server " << QString::fromStdString(c) << " did not contain substr " << QString::fromStdString(std::string(cppString));
}
}
}
void DelicodeSyphonClient::printServers()
{
NSArray *server_list = [[SyphonServerDirectory sharedDirectory] servers];
NSString* search_name_nsstring = [NSString stringWithCString:server_name];
int count = 0;
for(NSDictionary *server in server_list) {
NSString* app_name = [server objectForKey:SyphonServerDescriptionAppNameKey];
NSString* server_name = [server objectForKey:SyphonServerDescriptionNameKey];
std::string out([server_name UTF8String]);
count++;
//qDebug() << "Server #" << count << ": " << QString::fromStdString(out);
if ([server_name rangeOfString:search_name_nsstring].location == NSNotFound)
{
}
else
{
std::string c([server_name UTF8String]);
//qDebug() << "Found server with proper name: " << QString::fromStdString(c);
}
}
}
void DelicodeSyphonClient::free_frame()
{
if (mFrame)
{
SyphonImage* i = (SyphonImage *)mFrame;
[i release];
mFrame = nil;
}
}
bool DelicodeSyphonClient::update(unsigned int& input_width, unsigned int& input_height)
{
if(!mSyphon) {
texID = 0;
width = 1;
height = 1;
qDebug() << "Syphon update: syphon not initialized";
return false;
}
CGLContextObj cgl_ctx = CGLGetCurrentContext();
if (cgl_ctx == NULL)
{
qDebug() << "Syphon update: Context is not valid";
return false;
}
GLenum err = glGetError();
SyphonClient* c = (SyphonClient *)mSyphon;
if (c.isValid == NO)
{
isConnected = false;
qDebug() << "Syphon update: no connection to server";
return false;
}
isConnected = true;
NSString* temp1 = c.serverDescription[SyphonServerDescriptionUUIDKey];
std::string temp2 = [temp1 UTF8String];
//qDebug() << "Connected server uuid is: " << temp2.c_str();
temp1 = c.serverDescription[SyphonServerDescriptionNameKey];
temp2 = [temp1 UTF8String];
//qDebug() << "Connected server name is: " << QString::fromStdString(temp2);
temp1 = c.serverDescription[SyphonServerDescriptionAppNameKey];
temp2 = [temp1 UTF8String];
//qDebug() << "Connected server app name is: " << QString::fromStdString(temp2);
// Clear error stack
err = glGetError();
if (c.hasNewFrame == YES) {
mFrame = [(SyphonClient *)mSyphon newFrameImageForContext:cgl_ctx];
err = glGetError();
if (err != GL_NO_ERROR)
{
texID = 0;
qDebug() << "Syphon update: error getting frame: " << err;
return false;
}
}
else {
qDebug() << "Syphon update: no new frames";
return false;
}
if(!mFrame) {
texID = 0;
width = 1;
height = 1;
qDebug() << "Syphon update: frame is null";
return false;
}
texID = ((SyphonImage*)mFrame).textureName;
width = ((SyphonImage*)mFrame).textureSize.width;
height = ((SyphonImage*)mFrame).textureSize.height;
input_width = width;
input_height = height;
if(!mFbo)
glGenFramebuffersEXT(1, &mFbo);
err = glGetError();
if (err != GL_NO_ERROR)
qDebug() << "Syphon update: gen framebuffersext " << err;
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFbo);
err = glGetError();
if (err != GL_NO_ERROR)
qDebug() << "Syphon update: bindframebuffersext " << err;
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_EXT, texID, 0);
err = glGetError();
if (err != GL_NO_ERROR)
qDebug() << "Syphon update: framebuffertex2d " << err;
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
err = glGetError();
if (err != GL_NO_ERROR)
qDebug() << "Syphon update: error bindframebufferext " << err;
return true;
}
bool DelicodeSyphonClient::copyToTexture(unsigned int texid)
{
CGLContextObj cgl_ctx = CGLGetCurrentContext();
if (cgl_ctx == nil)
{
qDebug() << "Syphon copy texture: OpenGL context is null";
return false;
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFbo);
glBindTexture(GL_TEXTURE_2D, texid);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, width, height);
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glReadBuffer(GL_BACK);
return true;
}
void DelicodeSyphonClient::release()
{
if(!mSyphon && !mFrame)
return;
[(SyphonImage *)mFrame release];
mFrame = nil;
if (mSyphon)
[(SyphonClient *)mSyphon release];
mSyphon = nil;
}
DelicodeSyphonClient::~DelicodeSyphonClient()
{
if(mFrame)
[(SyphonImage *)mFrame release];
if(mSyphon)
[(SyphonClient *)mSyphon stop];
}