Jack2: JackEngine.cpp Source File

Jack2 1.9.12

JackEngine.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21 #include <fstream>
22 #include <set>
23 #include <assert.h>
24 #include <ctype.h>
25
26 #include "JackSystemDeps.h"
27 #include "JackLockedEngine.h"
28 #include "JackExternalClient.h"
29 #include "JackInternalClient.h"
30 #include "JackEngineControl.h"
31 #include "JackClientControl.h"
32 #include "JackServerGlobals.h"
33 #include "JackGlobals.h"
34 #include "JackChannel.h"
35 #include "JackError.h"
36
37 namespace Jack
38 {
39
40 JackEngine::JackEngine(JackGraphManager* manager,
41 JackSynchro* table,
42 JackEngineControl* control,
43 char self_connect_mode)
44 : JackLockAble(control->fServerName),
45 fSignal(control->fServerName)
46 {
47 fGraphManager = manager;
48 fSynchroTable = table;
49 fEngineControl = control;
50 fSelfConnectMode = self_connect_mode;
51 for (int i = 0; i < CLIENT_NUM; i++) {
52 fClientTable[i] = NULL;
53 }
54 fLastSwitchUsecs = 0;
55 fMaxUUID = 0;
56 fSessionPendingReplies = 0;
57 fSessionTransaction = NULL;
58 fSessionResult = NULL;
59 }
60
61 JackEngine::~JackEngine()
62 {}
63
64 int JackEngine::Open()
65 {
66 jack_log("JackEngine::Open");
67
68 // Open audio thread => request thread communication channel
69 if (fChannel.Open(fEngineControl->fServerName) < 0) {
70 jack_error("Cannot connect to server");
71 return -1;
72 } else {
73 return 0;
74 }
75 }
76
77 int JackEngine::Close()
78 {
79 jack_log("JackEngine::Close");
80 fChannel.Close();
81
82 // Close remaining clients (RT is stopped)
83 for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
84 if (JackLoadableInternalClient* loadable_client = dynamic_cast<JackLoadableInternalClient*>(fClientTable[i])) {
85 jack_log("JackEngine::Close loadable client = %s", loadable_client->GetClientControl()->fName);
86 loadable_client->Close();
87 fClientTable[i] = NULL;
88 delete loadable_client;
89 } else if (JackExternalClient* external_client = dynamic_cast<JackExternalClient*>(fClientTable[i])) {
90 jack_log("JackEngine::Close external client = %s", external_client->GetClientControl()->fName);
91 external_client->Close();
92 fClientTable[i] = NULL;
93 delete external_client;
94 }
95 }
96
97 return 0;
98 }
99
100 void JackEngine::NotifyQuit()
101 {
102 fChannel.NotifyQuit();
103 }
104
105 //-----------------------------
106 // Client ressource management
107 //-----------------------------
108
109 int JackEngine::AllocateRefnum()
110 {
111 for (int i = 0; i < CLIENT_NUM; i++) {
112 if (!fClientTable[i]) {
113 jack_log("JackEngine::AllocateRefNum ref = %ld", i);
114 return i;
115 }
116 }
117 return -1;
118 }
119
120 void JackEngine::ReleaseRefnum(int refnum)
121 {
122 fClientTable[refnum] = NULL;
123
124 if (fEngineControl->fTemporary) {
125 int i;
126 for (i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
127 if (fClientTable[i]) {
128 break;
129 }
130 }
131 if (i == CLIENT_NUM) {
132 // Last client and temporay case: quit the server
133 jack_log("JackEngine::ReleaseRefnum server quit");
134 fEngineControl->fTemporary = false;
135 throw JackTemporaryException();
136 }
137 }
138 }
139
140 //------------------
141 // Graph management
142 //------------------
143
144 void JackEngine::ProcessNext(jack_time_t cur_cycle_begin)
145 {
146 fLastSwitchUsecs = cur_cycle_begin;
147 if (fGraphManager->RunNextGraph()) { // True if the graph actually switched to a new state
148 fChannel.Notify(ALL_CLIENTS, kGraphOrderCallback, 0);
149 }
150 fSignal.Signal(); // Signal for threads waiting for next cycle
151 }
152
153 void JackEngine::ProcessCurrent(jack_time_t cur_cycle_begin)
154 {
155 if (cur_cycle_begin < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) { // Signal XRun only for the first failing cycle
156 CheckXRun(cur_cycle_begin);
157 }
158 fGraphManager->RunCurrentGraph();
159 }
160
161 bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
162 {
163 bool res = true;
164
165 // Cycle begin
166 fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end);
167
168 // Graph
169 if (fGraphManager->IsFinishedGraph()) {
170 ProcessNext(cur_cycle_begin);
171 res = true;
172 } else {
173 jack_log("Process: graph not finished!");
174 if (cur_cycle_begin > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
175 jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
176 ProcessNext(cur_cycle_begin);
177 res = true;
178 } else {
179 jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
180 ProcessCurrent(cur_cycle_begin);
181 res = false;
182 }
183 }
184
185 // Cycle end
186 fEngineControl->CycleEnd(fClientTable);
187 return res;
188 }
189
190 /*
191 Client that finish *after* the callback date are considered late even if their output buffers may have been
192 correctly mixed in the time window: callbackUsecs <==> Read <==> Write.
193 */
194
195 static const char* State2String(jack_client_state_t state)
196 {
197 switch (state) {
198 case NotTriggered:
199 return "NotTriggered";
200 case Triggered:
201 return "Triggered";
202 case Running:
203 return "Running";
204 case Finished:
205 return "Finished";
206 default:
207 return "";
208 }
209 }
210
211 void JackEngine::CheckXRun(jack_time_t callback_usecs) // REVOIR les conditions de fin
212 {
213 for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
214 JackClientInterface* client = fClientTable[i];
215 if (client && client->GetClientControl()->fActive) {
216 JackClientTiming* timing = fGraphManager->GetClientTiming(i);
217 jack_client_state_t status = timing->fStatus;
218 jack_time_t finished_date = timing->fFinishedAt;
219
220 if (status != NotTriggered && status != Finished) {
221 jack_error("JackEngine::XRun: client = %s was not finished, state = %s", client->GetClientControl()->fName, State2String(status));
222 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
223 }
224
225 if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
226 jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
227 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
228 }
229 }
230 }
231 }
232
233 int JackEngine::ComputeTotalLatencies()
234 {
235 std::vector<jack_int_t> sorted;
236 std::vector<jack_int_t>::iterator it;
237 std::vector<jack_int_t>::reverse_iterator rit;
238
239 fGraphManager->TopologicalSort(sorted);
240
241 /* iterate over all clients in graph order, and emit
242 * capture latency callback.
243 */
244
245 for (it = sorted.begin(); it != sorted.end(); it++) {
246 NotifyClient(*it, kLatencyCallback, true, "", 0, 0);
247 }
248
249 /* now issue playback latency callbacks in reverse graph order.
250 */
251 for (rit = sorted.rbegin(); rit != sorted.rend(); rit++) {
252 NotifyClient(*rit, kLatencyCallback, true, "", 1, 0);
253 }
254
255 return 0;
256 }
257
258 //---------------
259 // Notifications
260 //---------------
261
262 int JackEngine::ClientNotify(JackClientInterface* client, int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
263 {
264 // Check if notification is needed
265 if (!client->GetClientControl()->fCallback[notify]) {
266 jack_log("JackEngine::ClientNotify: no callback for notification = %ld", notify);
267 return 0;
268 }
269
270 int res1;
271
272 // External client
273 if (dynamic_cast<JackExternalClient*>(client)) {
274 res1 = client->ClientNotify(refnum, name, notify, sync, message, value1, value2);
275 // Important for internal client : unlock before calling the notification callbacks
276 } else {
277 bool res2 = Unlock();
278 res1 = client->ClientNotify(refnum, name, notify, sync, message, value1, value2);
279 if (res2) {
280 Lock();
281 }
282 }
283
284 if (res1 < 0) {
285 jack_error("ClientNotify fails name = %s notification = %ld val1 = %ld val2 = %ld", name, notify, value1, value2);
286 }
287 return res1;
288 }
289
290 void JackEngine::NotifyClient(int refnum, int event, int sync, const char* message, int value1, int value2)
291 {
292 JackClientInterface* client = fClientTable[refnum];
293 if (client) {
294 ClientNotify(client, refnum, client->GetClientControl()->fName, event, sync, message, value1, value2);
295 }
296 }
297
298 void JackEngine::NotifyClients(int event, int sync, const char* message, int value1, int value2)
299 {
300 for (int i = 0; i < CLIENT_NUM; i++) {
301 NotifyClient(i, event, sync, message, value1, value2);
302 }
303 }
304
305 int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* new_name, int refnum)
306 {
307 jack_log("JackEngine::NotifyAddClient: name = %s", new_name);
308
309 // Notify existing clients of the new client and new client of existing clients.
310 for (int i = 0; i < CLIENT_NUM; i++) {
311 JackClientInterface* old_client = fClientTable[i];
312 if (old_client && old_client != new_client) {
313 char* old_name = old_client->GetClientControl()->fName;
314 if (ClientNotify(old_client, refnum, new_name, kAddClient, false, "", 0, 0) < 0) {
315 jack_error("NotifyAddClient old_client fails name = %s", old_name);
316 // Not considered as a failure...
317 }
318 if (ClientNotify(new_client, i, old_name, kAddClient, true, "", 0, 0) < 0) {
319 jack_error("NotifyAddClient new_client fails name = %s", new_name);
320 return -1;
321 }
322 }
323 }
324
325 return 0;
326 }
327
328 void JackEngine::NotifyRemoveClient(const char* name, int refnum)
329 {
330 // Notify existing clients (including the one beeing suppressed) of the removed client
331 for (int i = 0; i < CLIENT_NUM; i++) {
332 JackClientInterface* client = fClientTable[i];
333 if (client) {
334 ClientNotify(client, refnum, name, kRemoveClient, false, "", 0, 0);
335 }
336 }
337 }
338
339 // Coming from the driver
340 void JackEngine::NotifyDriverXRun()
341 {
342 // Use the audio thread => request thread communication channel
343 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0);
344 }
345
346 void JackEngine::NotifyClientXRun(int refnum)
347 {
348 if (refnum == ALL_CLIENTS) {
349 NotifyClients(kXRunCallback, false, "", 0, 0);
350 } else {
351 NotifyClient(refnum, kXRunCallback, false, "", 0, 0);
352 }
353 }
354
355 void JackEngine::NotifyGraphReorder()
356 {
357 ComputeTotalLatencies();
358 NotifyClients(kGraphOrderCallback, false, "", 0, 0);
359 }
360
361 void JackEngine::NotifyBufferSize(jack_nframes_t buffer_size)
362 {
363 NotifyClients(kBufferSizeCallback, true, "", buffer_size, 0);
364 }
365
366 void JackEngine::NotifySampleRate(jack_nframes_t sample_rate)
367 {
368 NotifyClients(kSampleRateCallback, true, "", sample_rate, 0);
369 }
370
371 void JackEngine::NotifyFailure(int code, const char* reason)
372 {
373 NotifyClients(kShutDownCallback, false, reason, code, 0);
374 }
375
376 void JackEngine::NotifyFreewheel(bool onoff)
377 {
378 if (onoff) {
379 // Save RT state
380 fEngineControl->fSavedRealTime = fEngineControl->fRealTime;
381 fEngineControl->fRealTime = false;
382 } else {
383 // Restore RT state
384 fEngineControl->fRealTime = fEngineControl->fSavedRealTime;
385 fEngineControl->fSavedRealTime = false;
386 }
387 NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, "", 0, 0);
388 }
389
390 void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
391 {
392 NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, "", port_index, 0);
393 }
394
395 void JackEngine::NotifyPortRename(jack_port_id_t port, const char* old_name)
396 {
397 NotifyClients(kPortRenameCallback, false, old_name, port, 0);
398 }
399
400 void JackEngine::NotifyPortConnect(jack_port_id_t src, jack_port_id_t dst, bool onoff)
401 {
402 NotifyClients((onoff ? kPortConnectCallback : kPortDisconnectCallback), false, "", src, dst);
403 }
404
405 void JackEngine::NotifyActivate(int refnum)
406 {
407 NotifyClient(refnum, kActivateClient, true, "", 0, 0);
408 }
409
410 //----------------------------
411 // Loadable client management
412 //----------------------------
413
414 int JackEngine::GetInternalClientName(int refnum, char* name_res)
415 {
416 JackClientInterface* client = fClientTable[refnum];
417 assert(client);
418 strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
419 return 0;
420 }
421
422 int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
423 {
424 // Clear status
425 *status = 0;
426
427 for (int i = 0; i < CLIENT_NUM; i++) {
428 JackClientInterface* client = fClientTable[i];
429 if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
430 jack_log("InternalClientHandle found client name = %s ref = %ld", client_name, i);
431 *int_ref = i;
432 return 0;
433 }
434 }
435
436 *status |= (JackNoSuchClient | JackFailure);
437 return -1;
438 }
439
440 int JackEngine::InternalClientUnload(int refnum, int* status)
441 {
442 JackClientInterface* client = fClientTable[refnum];
443 if (client) {
444 int res = client->Close();
445 delete client;
446 *status = 0;
447 return res;
448 } else {
449 *status = (JackNoSuchClient | JackFailure);
450 return -1;
451 }
452 }
453
454 //-------------------
455 // Client management
456 //-------------------
457
458 int JackEngine::ClientCheck(const char* name, int uuid, char* name_res, int protocol, int options, int* status)
459 {
460 // Clear status
461 *status = 0;
462 strcpy(name_res, name);
463
464 jack_log("Check protocol client = %ld server = %ld", protocol, JACK_PROTOCOL_VERSION);
465
466 if (protocol != JACK_PROTOCOL_VERSION) {
467 *status |= (JackFailure | JackVersionError);
468 jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
469 return -1;
470 }
471
472 std::map<int,std::string>::iterator res = fReservationMap.find(uuid);
473
474 if (res != fReservationMap.end()) {
475 strncpy(name_res, res->second.c_str(), JACK_CLIENT_NAME_SIZE);
476 } else if (ClientCheckName(name)) {
477
478 *status |= JackNameNotUnique;
479
480 if (options & JackUseExactName) {
481 jack_error("cannot create new client; %s already exists", name);
482 *status |= JackFailure;
483 return -1;
484 }
485
486 if (GenerateUniqueName(name_res)) {
487 *status |= JackFailure;
488 return -1;
489 }
490 }
491
492 return 0;
493 }
494
495 bool JackEngine::GenerateUniqueName(char* name)
496 {
497 int tens, ones;
498 int length = strlen(name);
499
500 if (length > JACK_CLIENT_NAME_SIZE - 4) {
501 jack_error("%s exists and is too long to make unique", name);
502 return true; /* failure */
503 }
504
505 /* generate a unique name by appending "-01".."-99" */
506 name[length++] = '-';
507 tens = length++;
508 ones = length++;
509 name[tens] = '0';
510 name[ones] = '1';
511 name[length] = '\0';
512
513 while (ClientCheckName(name)) {
514 if (name[ones] == '9') {
515 if (name[tens] == '9') {
516 jack_error("client %s has 99 extra instances already", name);
517 return true; /* give up */
518 }
519 name[tens]++;
520 name[ones] = '0';
521 } else {
522 name[ones]++;
523 }
524 }
525 return false;
526 }
527
528 bool JackEngine::ClientCheckName(const char* name)
529 {
530 for (int i = 0; i < CLIENT_NUM; i++) {
531 JackClientInterface* client = fClientTable[i];
532 if (client && (strcmp(client->GetClientControl()->fName, name) == 0)) {
533 return true;
534 }
535 }
536
537 for (std::map<int,std::string>::iterator i = fReservationMap.begin(); i != fReservationMap.end(); i++) {
538 if (i->second == name) {
539 return true;
540 }
541 }
542
543 return false;
544 }
545
546 int JackEngine::GetNewUUID()
547 {
548 return fMaxUUID++;
549 }
550
551 void JackEngine::EnsureUUID(int uuid)
552 {
553 if (uuid > fMaxUUID) {
554 fMaxUUID = uuid + 1;
555 }
556
557 for (int i = 0; i < CLIENT_NUM; i++) {
558 JackClientInterface* client = fClientTable[i];
559 if (client && (client->GetClientControl()->fSessionID == uuid)) {
560 client->GetClientControl()->fSessionID = GetNewUUID();
561 }
562 }
563 }
564
565 int JackEngine::GetClientPID(const char* name)
566 {
567 for (int i = 0; i < CLIENT_NUM; i++) {
568 JackClientInterface* client = fClientTable[i];
569 if (client && (strcmp(client->GetClientControl()->fName, name) == 0)) {
570 return client->GetClientControl()->fPID;
571 }
572 }
573
574 return 0;
575 }
576
577 int JackEngine::GetClientRefNum(const char* name)
578 {
579 for (int i = 0; i < CLIENT_NUM; i++) {
580 JackClientInterface* client = fClientTable[i];
581 if (client && (strcmp(client->GetClientControl()->fName, name) == 0)) {
582 return client->GetClientControl()->fRefNum;
583 }
584 }
585
586 return -1;
587 }
588
589 // Used for external clients
590 int JackEngine::ClientExternalOpen(const char* name, int pid, int uuid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
591 {
592 char real_name[JACK_CLIENT_NAME_SIZE + 1];
593
594 if (uuid < 0) {
595 uuid = GetNewUUID();
596 strncpy(real_name, name, JACK_CLIENT_NAME_SIZE);
597 } else {
598 std::map<int, std::string>::iterator res = fReservationMap.find(uuid);
599 if (res != fReservationMap.end()) {
600 strncpy(real_name, res->second.c_str(), JACK_CLIENT_NAME_SIZE);
601 fReservationMap.erase(uuid);
602 } else {
603 strncpy(real_name, name, JACK_CLIENT_NAME_SIZE);
604 }
605 EnsureUUID(uuid);
606 }
607
608 jack_log("JackEngine::ClientExternalOpen: uuid = %d, name = %s", uuid, real_name);
609
610 int refnum = AllocateRefnum();
611 if (refnum < 0) {
612 jack_error("No more refnum available");
613 return -1;
614 }
615
616 JackExternalClient* client = new JackExternalClient();
617
618 if (!fSynchroTable[refnum].Allocate(real_name, fEngineControl->fServerName, 0)) {
619 jack_error("Cannot allocate synchro");
620 goto error;
621 }
622
623 if (client->Open(real_name, pid, refnum, uuid, shared_client) < 0) {
624 jack_error("Cannot open client");
625 goto error;
626 }
627
628 if (!fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
629 // Failure if RT thread is not running (problem with the driver...)
630 jack_error("Driver is not running");
631 goto error;
632 }
633
634 fClientTable[refnum] = client;
635
636 if (NotifyAddClient(client, real_name, refnum) < 0) {
637 jack_error("Cannot notify add client");
638 goto error;
639 }
640
641 fGraphManager->InitRefNum(refnum);
642 fEngineControl->ResetRollingUsecs();
643 *shared_engine = fEngineControl->GetShmIndex();
644 *shared_graph_manager = fGraphManager->GetShmIndex();
645 *ref = refnum;
646 return 0;
647
648 error:
649 // Cleanup...
650 fSynchroTable[refnum].Destroy();
651 fClientTable[refnum] = 0;
652 client->Close();
653 delete client;
654 return -1;
655 }
656
657 // Used for server driver clients
658 int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
659 {
660 jack_log("JackEngine::ClientInternalOpen: name = %s", name);
661
662 int refnum = AllocateRefnum();
663 if (refnum < 0) {
664 jack_error("No more refnum available");
665 goto error;
666 }
667
668 if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
669 jack_error("Cannot allocate synchro");
670 goto error;
671 }
672
673 if (wait && !fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
674 // Failure if RT thread is not running (problem with the driver...)
675 jack_error("Driver is not running");
676 goto error;
677 }
678
679 fClientTable[refnum] = client;
680
681 if (NotifyAddClient(client, name, refnum) < 0) {
682 jack_error("Cannot notify add client");
683 goto error;
684 }
685
686 fGraphManager->InitRefNum(refnum);
687 fEngineControl->ResetRollingUsecs();
688 *shared_engine = fEngineControl;
689 *shared_manager = fGraphManager;
690 *ref = refnum;
691 return 0;
692
693 error:
694 // Cleanup...
695 fSynchroTable[refnum].Destroy();
696 fClientTable[refnum] = 0;
697 return -1;
698 }
699
700 // Used for external clients
701 int JackEngine::ClientExternalClose(int refnum)
702 {
703 jack_log("JackEngine::ClientExternalClose ref = %ld", refnum);
704 JackClientInterface* client = fClientTable[refnum];
705 assert(client);
706 int res = ClientCloseAux(refnum, true);
707 client->Close();
708 delete client;
709 return res;
710 }
711
712 // Used for server internal clients or drivers when the RT thread is stopped
713 int JackEngine::ClientInternalClose(int refnum, bool wait)
714 {
715 jack_log("JackEngine::ClientInternalClose ref = %ld", refnum);
716 return ClientCloseAux(refnum, wait);
717 }
718
719 int JackEngine::ClientCloseAux(int refnum, bool wait)
720 {
721 jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
722
723 JackClientInterface* client = fClientTable[refnum];
724 fEngineControl->fTransport.ResetTimebase(refnum);
725
726 // Unregister all ports ==> notifications are sent
727 jack_int_t ports[PORT_NUM_FOR_CLIENT];
728 int i;
729
730 fGraphManager->GetInputPorts(refnum, ports);
731 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY); i++) {
732 PortUnRegister(refnum, ports[i]);
733 }
734
735 fGraphManager->GetOutputPorts(refnum, ports);
736 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY); i++) {
737 PortUnRegister(refnum, ports[i]);
738 }
739
740 // Remove the client from the table
741 ReleaseRefnum(refnum);
742
743 // Remove all ports
744 fGraphManager->RemoveAllPorts(refnum);
745
746 // Wait until next cycle to be sure client is not used anymore
747 if (wait) {
748 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
749 jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
750 }
751 }
752
753 // Notify running clients
754 NotifyRemoveClient(client->GetClientControl()->fName, refnum);
755
756 // Cleanup...
757 fSynchroTable[refnum].Destroy();
758 fEngineControl->ResetRollingUsecs();
759 return 0;
760 }
761
762 int JackEngine::ClientActivate(int refnum, bool is_real_time)
763 {
764 JackClientInterface* client = fClientTable[refnum];
765 jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
766
767 if (is_real_time) {
768 fGraphManager->Activate(refnum);
769 }
770
771 // Wait for graph state change to be effective
772 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
773 jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
774 return -1;
775 } else {
776 jack_int_t input_ports[PORT_NUM_FOR_CLIENT];
777 jack_int_t output_ports[PORT_NUM_FOR_CLIENT];
778 fGraphManager->GetInputPorts(refnum, input_ports);
779 fGraphManager->GetOutputPorts(refnum, output_ports);
780
781 // Notify client
782 NotifyActivate(refnum);
783
784 // Then issue port registration notification
785 for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
786 NotifyPortRegistation(input_ports[i], true);
787 }
788 for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
789 NotifyPortRegistation(output_ports[i], true);
790 }
791
792 return 0;
793 }
794 }
795
796 // May be called without client
797 int JackEngine::ClientDeactivate(int refnum)
798 {
799 JackClientInterface* client = fClientTable[refnum];
800 jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
801
802 jack_int_t input_ports[PORT_NUM_FOR_CLIENT];
803 jack_int_t output_ports[PORT_NUM_FOR_CLIENT];
804 fGraphManager->GetInputPorts(refnum, input_ports);
805 fGraphManager->GetOutputPorts(refnum, output_ports);
806
807 // First disconnect all ports
808 for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
809 PortDisconnect(-1, input_ports[i], ALL_PORTS);
810 }
811 for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
812 PortDisconnect(-1, output_ports[i], ALL_PORTS);
813 }
814
815 // Then issue port registration notification
816 for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (input_ports[i] != EMPTY); i++) {
817 NotifyPortRegistation(input_ports[i], false);
818 }
819 for (int i = 0; (i < PORT_NUM_FOR_CLIENT) && (output_ports[i] != EMPTY); i++) {
820 NotifyPortRegistation(output_ports[i], false);
821 }
822
823 fGraphManager->Deactivate(refnum);
824 fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
825
826 // Wait for graph state change to be effective
827 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
828 jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
829 return -1;
830 } else {
831 return 0;
832 }
833 }
834
835 void JackEngine::ClientKill(int refnum)
836 {
837 jack_log("JackEngine::ClientKill ref = %ld", refnum);
838 if (ClientDeactivate(refnum) < 0) {
839 jack_error("JackEngine::ClientKill ref = %ld cannot be removed from the graph !!", refnum);
840 }
841 if (ClientExternalClose(refnum) < 0) {
842 jack_error("JackEngine::ClientKill ref = %ld cannot be closed", refnum);
843 }
844 }
845
846 //-----------------
847 // Port management
848 //-----------------
849
850 int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
851 {
852 jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
853 JackClientInterface* client = fClientTable[refnum];
854
855 // Check if port name already exists
856 if (fGraphManager->GetPort(name) != NO_PORT) {
857 jack_error("port_name \"%s\" already exists", name);
858 return -1;
859 }
860
861 // buffer_size is actually ignored...
862 *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
863 if (*port_index != NO_PORT) {
864 if (client->GetClientControl()->fActive) {
865 NotifyPortRegistation(*port_index, true);
866 }
867 return 0;
868 } else {
869 return -1;
870 }
871 }
872
873 int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
874 {
875 jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
876 JackClientInterface* client = fClientTable[refnum];
877 assert(client);
878
879 // Disconnect port ==> notification is sent
880 PortDisconnect(-1, port_index, ALL_PORTS);
881
882 if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
883 if (client->GetClientControl()->fActive) {
884 NotifyPortRegistation(port_index, false);
885 }
886 return 0;
887 } else {
888 return -1;
889 }
890 }
891
892 // this check is to prevent apps to self connect to other apps
893 // TODO: make this work with multiple clients per app
894 int JackEngine::CheckPortsConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
895 {
896 if (fSelfConnectMode == ' ') return 1;
897
898 JackPort* src_port = fGraphManager->GetPort(src);
899 JackPort* dst_port = fGraphManager->GetPort(dst);
900
901 jack_log("JackEngine::CheckPortsConnect(ref = %d, src = %d, dst = %d)", refnum, src_port->GetRefNum(), dst_port->GetRefNum());
902
903 //jack_log("%s -> %s", src_port->GetName(), dst_port->GetName());
904 //jack_log("mode = '%c'", fSelfConnectMode);
905
906 int src_self = src_port->GetRefNum() == refnum ? 1 : 0;
907 int dst_self = dst_port->GetRefNum() == refnum ? 1 : 0;
908
909 //jack_log("src_self is %s", src_self ? "true" : "false");
910 //jack_log("dst_self is %s", dst_self ? "true" : "false");
911
912 // 0 means client is connecting other client ports (control app patchbay functionality)
913 // 1 means client is connecting its own port to port of other client (e.g. self connecting into "system" client)
914 // 2 means client is connecting its own ports (for app internal functionality)
915 int sum = src_self + dst_self;
916 //jack_log("sum = %d", sum);
917 if (sum == 0) return 1;
918 char lmode = tolower(fSelfConnectMode);
919 //jack_log("lmode = '%c'", lmode);
920 if (sum == 2 && lmode == 'e') return 1;
921 bool fail = lmode != fSelfConnectMode; // fail modes are upper case
922 //jack_log("fail = %d", (int)fail);
923
924 jack_info(
925 "%s port self connect request%s (%s -> %s)",
926 fail ? "rejecting" : "ignoring",
927 sum == 1 ? " to external port" : "",
928 src_port->GetName(),
929 dst_port->GetName());
930
931 return fail ? -1 : 0;
932 }
933
934 int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
935 {
936 jack_log("JackEngine::PortConnect ref = %d src = %s dst = %s", refnum, src, dst);
937 jack_port_id_t port_src, port_dst;
938
939 return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
940 ? -1
941 : PortConnect(refnum, port_src, port_dst);
942 }
943
944 int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
945 {
946 jack_log("JackEngine::PortConnect ref = %d src = %d dst = %d", refnum, src, dst);
947 JackClientInterface* client;
948 int ref;
949
950 if (fGraphManager->CheckPorts(src, dst) < 0) {
951 return -1;
952 }
953
954 ref = fGraphManager->GetOutputRefNum(src);
955 assert(ref >= 0);
956 client = fClientTable[ref];
957 assert(client);
958 if (!client->GetClientControl()->fActive) {
959 jack_error("Cannot connect ports owned by inactive clients:"
960 " \"%s\" is not active", client->GetClientControl()->fName);
961 return -1;
962 }
963
964 ref = fGraphManager->GetInputRefNum(dst);
965 assert(ref >= 0);
966 client = fClientTable[ref];
967 assert(client);
968 if (!client->GetClientControl()->fActive) {
969 jack_error("Cannot connect ports owned by inactive clients:"
970 " \"%s\" is not active", client->GetClientControl()->fName);
971 return -1;
972 }
973
974 int res = CheckPortsConnect(refnum, src, dst);
975 if (res != 1) {
976 return res;
977 }
978
979 res = fGraphManager->Connect(src, dst);
980 if (res == 0) {
981 NotifyPortConnect(src, dst, true);
982 }
983 return res;
984 }
985
986 int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
987 {
988 jack_log("JackEngine::PortDisconnect ref = %d src = %s dst = %s", refnum, src, dst);
989 jack_port_id_t port_src, port_dst;
990
991 return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
992 ? -1
993 : PortDisconnect(refnum, port_src, port_dst);
994 }
995
996 int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
997 {
998 jack_log("JackEngine::PortDisconnect ref = %d src = %d dst = %d", refnum, src, dst);
999
1000 if (dst == ALL_PORTS) {
1001
1002 jack_int_t connections[CONNECTION_NUM_FOR_PORT];
1003 fGraphManager->GetConnections(src, connections);
1004
1005 JackPort* port = fGraphManager->GetPort(src);
1006 int res = 0;
1007 if (port->GetFlags() & JackPortIsOutput) {
1008 for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
1009 if (PortDisconnect(refnum, src, connections[i]) != 0) {
1010 res = -1;
1011 }
1012 }
1013 } else {
1014 for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
1015 if (PortDisconnect(refnum, connections[i], src) != 0) {
1016 res = -1;
1017 }
1018 }
1019 }
1020
1021 return res;
1022 }
1023
1024 if (fGraphManager->CheckPorts(src, dst) < 0) {
1025 return -1;
1026 }
1027
1028 int res = CheckPortsConnect(refnum, src, dst);
1029 if (res != 1) {
1030 return res;
1031 }
1032
1033 res = fGraphManager->Disconnect(src, dst);
1034 if (res == 0)
1035 NotifyPortConnect(src, dst, false);
1036 return res;
1037 }
1038
1039 int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
1040 {
1041 char old_name[REAL_JACK_PORT_NAME_SIZE+1];
1042 strcpy(old_name, fGraphManager->GetPort(port)->GetName());
1043 fGraphManager->GetPort(port)->SetName(name);
1044 NotifyPortRename(port, old_name);
1045 return 0;
1046 }
1047
1048 //--------------------
1049 // Session management
1050 //--------------------
1051
1052 void JackEngine::SessionNotify(int refnum, const char *target, jack_session_event_type_t type, const char *path, detail::JackChannelTransactionInterface *socket, JackSessionNotifyResult** result)
1053 {
1054 if (fSessionPendingReplies != 0) {
1055 JackSessionNotifyResult res(-1);
1056 res.Write(socket);
1057 jack_log("JackEngine::SessionNotify ... busy");
1058 if (result != NULL) *result = NULL;
1059 return;
1060 }
1061
1062 for (int i = 0; i < CLIENT_NUM; i++) {
1063 JackClientInterface* client = fClientTable[i];
1064 if (client && (client->GetClientControl()->fSessionID < 0)) {
1065 client->GetClientControl()->fSessionID = GetNewUUID();
1066 }
1067 }
1068 fSessionResult = new JackSessionNotifyResult();
1069
1070 for (int i = 0; i < CLIENT_NUM; i++) {
1071 JackClientInterface* client = fClientTable[i];
1072 if (client && client->GetClientControl()->fCallback[kSessionCallback]) {
1073
1074 // check if this is a notification to a specific client.
1075 if (target != NULL && strlen(target) != 0) {
1076 if (strcmp(target, client->GetClientControl()->fName)) {
1077 continue;
1078 }
1079 }
1080
1081 char path_buf[JACK_PORT_NAME_SIZE];
1082 if (path[strlen(path) - 1] == DIR_SEPARATOR) {
1083 snprintf(path_buf, sizeof path_buf, "%s%s%c", path, client->GetClientControl()->fName, DIR_SEPARATOR);
1084 } else {
1085 snprintf(path_buf, sizeof path_buf, "%s%c%s%c", path, DIR_SEPARATOR, client->GetClientControl()->fName, DIR_SEPARATOR);
1086 }
1087
1088 int res = JackTools::MkDir(path_buf);
1089 if (res) jack_error("JackEngine::SessionNotify: can not create session directory '%s'", path_buf);
1090
1091 int result = client->ClientNotify(i, client->GetClientControl()->fName, kSessionCallback, true, path_buf, (int)type, 0);
1092
1093 if (result == kPendingSessionReply) {
1094 fSessionPendingReplies += 1;
1095 } else if (result == kImmediateSessionReply) {
1096 char uuid_buf[JACK_UUID_SIZE];
1097 snprintf(uuid_buf, sizeof(uuid_buf), "%d", client->GetClientControl()->fSessionID);
1098 fSessionResult->fCommandList.push_back(JackSessionCommand(uuid_buf,
1099 client->GetClientControl()->fName,
1100 client->GetClientControl()->fSessionCommand,
1101 client->GetClientControl()->fSessionFlags));
1102 }
1103 }
1104 }
1105
1106 if (result != NULL) *result = fSessionResult;
1107
1108 if (fSessionPendingReplies == 0) {
1109 fSessionResult->Write(socket);
1110 if (result == NULL) delete fSessionResult;
1111 fSessionResult = NULL;
1112 } else {
1113 fSessionTransaction = socket;
1114 }
1115 }
1116
1117 int JackEngine::SessionReply(int refnum)
1118 {
1119 JackClientInterface* client = fClientTable[refnum];
1120 assert(client);
1121 char uuid_buf[JACK_UUID_SIZE];
1122 snprintf(uuid_buf, sizeof(uuid_buf), "%d", client->GetClientControl()->fSessionID);
1123 fSessionResult->fCommandList.push_back(JackSessionCommand(uuid_buf,
1124 client->GetClientControl()->fName,
1125 client->GetClientControl()->fSessionCommand,
1126 client->GetClientControl()->fSessionFlags));
1127 fSessionPendingReplies -= 1;
1128
1129 if (fSessionPendingReplies == 0) {
1130 fSessionResult->Write(fSessionTransaction);
1131 if (fSessionTransaction != NULL) {
1132 delete fSessionResult;
1133 }
1134 fSessionResult = NULL;
1135 }
1136
1137 return 0;
1138 }
1139
1140 int JackEngine::GetUUIDForClientName(const char *client_name, char *uuid_res)
1141 {
1142 for (int i = 0; i < CLIENT_NUM; i++) {
1143 JackClientInterface* client = fClientTable[i];
1144
1145 if (client && (strcmp(client_name, client->GetClientControl()->fName) == 0)) {
1146 snprintf(uuid_res, JACK_UUID_SIZE, "%d", client->GetClientControl()->fSessionID);
1147 return 0;
1148 }
1149 }
1150 // Did not find name.
1151 return -1;
1152 }
1153
1154 int JackEngine::GetClientNameForUUID(const char *uuid, char *name_res)
1155 {
1156 for (int i = 0; i < CLIENT_NUM; i++) {
1157 JackClientInterface* client = fClientTable[i];
1158
1159 if (!client) {
1160 continue;
1161 }
1162
1163 char uuid_buf[JACK_UUID_SIZE];
1164 snprintf(uuid_buf, JACK_UUID_SIZE, "%d", client->GetClientControl()->fSessionID);
1165
1166 if (strcmp(uuid,uuid_buf) == 0) {
1167 strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
1168 return 0;
1169 }
1170 }
1171 // Did not find uuid.
1172 return -1;
1173 }
1174
1175 int JackEngine::ReserveClientName(const char *name, const char *uuid)
1176 {
1177 jack_log("JackEngine::ReserveClientName ( name = %s, uuid = %s )", name, uuid);
1178
1179 if (ClientCheckName(name)) {
1180 jack_log("name already taken");
1181 return -1;
1182 }
1183
1184 EnsureUUID(atoi(uuid));
1185 fReservationMap[atoi(uuid)] = name;
1186 return 0;
1187 }
1188
1189 int JackEngine::ClientHasSessionCallback(const char *name)
1190 {
1191 JackClientInterface* client = NULL;
1192 for (int i = 0; i < CLIENT_NUM; i++) {
1193 client = fClientTable[i];
1194 if (client && (strcmp(client->GetClientControl()->fName, name) == 0)) {
1195 break;
1196 }
1197 }
1198
1199 if (client) {
1200 return client->GetClientControl()->fCallback[kSessionCallback];
1201 } else {
1202 return -1;
1203 }
1204 }
1205
1206 } // end of namespace
1207
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_info
SERVER_EXPORT void jack_info(const char *fmt,...)
Definition: JackError.cpp:100
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108