2 Copyright (C) 2004-2008 Grame
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.
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.
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.
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"
37 extern const char* JACK_METADATA_HARDWARE
;
38 extern const char* JACK_METADATA_PRETTY_NAME
;
43 JackEngine::JackEngine(JackGraphManager
* manager
,
45 JackEngineControl
* control
,
46 char self_connect_mode
)
47 : JackLockAble(control
->fServerName
),
48 fSignal(control
->fServerName
),
51 fGraphManager
= manager
;
52 fSynchroTable
= table
;
53 fEngineControl
= control
;
54 fSelfConnectMode
= self_connect_mode
;
55 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
56 fClientTable
[i
] = NULL
;
59 fSessionPendingReplies
= 0;
60 fSessionTransaction
= NULL
;
61 fSessionResult
= NULL
;
64 JackEngine::~JackEngine()
67 int JackEngine::Open()
69 jack_log("JackEngine::Open");
71 // Open audio thread => request thread communication channel
72 if (fChannel
.Open(fEngineControl
->fServerName
) < 0) {
73 jack_error("Cannot connect to server");
80 int JackEngine::Close()
82 jack_log("JackEngine::Close");
85 // Close remaining clients (RT is stopped)
86 for (int i
= fEngineControl
->fDriverNum
; i
< CLIENT_NUM
; i
++) {
87 if (JackLoadableInternalClient
* loadable_client
= dynamic_cast<JackLoadableInternalClient
*>(fClientTable
[i
])) {
88 jack_log("JackEngine::Close loadable client = %s", loadable_client
->GetClientControl()->fName
);
89 loadable_client
->Close();
90 fClientTable
[i
] = NULL
;
91 delete loadable_client
;
92 } else if (JackExternalClient
* external_client
= dynamic_cast<JackExternalClient
*>(fClientTable
[i
])) {
93 jack_log("JackEngine::Close external client = %s", external_client
->GetClientControl()->fName
);
94 external_client
->Close();
95 fClientTable
[i
] = NULL
;
96 delete external_client
;
103 void JackEngine::NotifyQuit()
105 fChannel
.NotifyQuit();
109 //-----------------------------
110 // Client resource management
111 //-----------------------------
113 int JackEngine::AllocateRefnum()
115 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
116 if (!fClientTable
[i
]) {
117 jack_log("JackEngine::AllocateRefNum ref = %ld", i
);
124 void JackEngine::ReleaseRefnum(int refnum
)
126 fClientTable
[refnum
] = NULL
;
128 if (fEngineControl
->fTemporary
) {
130 for (i
= fEngineControl
->fDriverNum
; i
< CLIENT_NUM
; i
++) {
131 if (fClientTable
[i
]) {
135 if (i
== CLIENT_NUM
) {
136 // Last client and temporary case: quit the server
137 jack_log("JackEngine::ReleaseRefnum server quit");
138 fEngineControl
->fTemporary
= false;
139 throw JackTemporaryException();
148 void JackEngine::ProcessNext(jack_time_t cur_cycle_begin
)
150 fLastSwitchUsecs
= cur_cycle_begin
;
151 if (fGraphManager
->RunNextGraph()) { // True if the graph actually switched to a new state
152 fChannel
.Notify(ALL_CLIENTS
, kGraphOrderCallback
, 0);
154 fSignal
.Signal(); // Signal for threads waiting for next cycle
157 void JackEngine::ProcessCurrent(jack_time_t cur_cycle_begin
)
159 if (cur_cycle_begin
< fLastSwitchUsecs
+ 2 * fEngineControl
->fPeriodUsecs
) { // Signal XRun only for the first failing cycle
160 CheckXRun(cur_cycle_begin
);
162 fGraphManager
->RunCurrentGraph();
165 bool JackEngine::Process(jack_time_t cur_cycle_begin
, jack_time_t prev_cycle_end
)
170 fEngineControl
->CycleBegin(fClientTable
, fGraphManager
, cur_cycle_begin
, prev_cycle_end
);
173 if (fGraphManager
->IsFinishedGraph()) {
174 ProcessNext(cur_cycle_begin
);
177 jack_log("Process: graph not finished!");
178 if (cur_cycle_begin
> fLastSwitchUsecs
+ fEngineControl
->fTimeOutUsecs
) {
179 jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin
- fLastSwitchUsecs
));
180 ProcessNext(cur_cycle_begin
);
183 jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin
- fLastSwitchUsecs
));
184 ProcessCurrent(cur_cycle_begin
);
190 fEngineControl
->CycleEnd(fClientTable
);
195 Client that finish *after* the callback date are considered late even if their output buffers may have been
196 correctly mixed in the time window: callbackUsecs <==> Read <==> Write.
199 static const char* State2String(jack_client_state_t state
)
203 return "NotTriggered";
215 void JackEngine::CheckXRun(jack_time_t callback_usecs
) // REVOIR les conditions de fin
217 for (int i
= fEngineControl
->fDriverNum
; i
< CLIENT_NUM
; i
++) {
218 JackClientInterface
* client
= fClientTable
[i
];
219 if (client
&& client
->GetClientControl()->fActive
) {
220 JackClientTiming
* timing
= fGraphManager
->GetClientTiming(i
);
221 jack_client_state_t status
= timing
->fStatus
;
222 jack_time_t finished_date
= timing
->fFinishedAt
;
224 if (status
!= NotTriggered
&& status
!= Finished
) {
225 jack_error("JackEngine::XRun: client = %s was not finished, state = %s", client
->GetClientControl()->fName
, State2String(status
));
226 fChannel
.Notify(ALL_CLIENTS
, kXRunCallback
, 0); // Notify all clients
229 if (status
== Finished
&& (long)(finished_date
- callback_usecs
) > 0) {
230 jack_error("JackEngine::XRun: client %s finished after current callback", client
->GetClientControl()->fName
);
231 fChannel
.Notify(ALL_CLIENTS
, kXRunCallback
, 0); // Notify all clients
237 int JackEngine::ComputeTotalLatencies()
239 std::vector
<jack_int_t
> sorted
;
240 std::vector
<jack_int_t
>::iterator it
;
241 std::vector
<jack_int_t
>::reverse_iterator rit
;
243 fGraphManager
->TopologicalSort(sorted
);
245 /* iterate over all clients in graph order, and emit
246 * capture latency callback.
249 for (it
= sorted
.begin(); it
!= sorted
.end(); it
++) {
250 NotifyClient(*it
, kLatencyCallback
, true, "", 0, 0);
253 /* now issue playback latency callbacks in reverse graph order.
255 for (rit
= sorted
.rbegin(); rit
!= sorted
.rend(); rit
++) {
256 NotifyClient(*rit
, kLatencyCallback
, true, "", 1, 0);
266 int JackEngine::PropertyChangeNotify(jack_uuid_t subject
, const char* key
, jack_property_change_t change
)
268 jack_log("JackEngine::PropertyChangeNotify: subject = %x key = %s change = %x", subject
, key
, change
);
270 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
271 JackClientInterface
* client
= fClientTable
[i
];
273 char buf
[JACK_UUID_STRING_SIZE
];
274 jack_uuid_unparse(subject
, buf
);
275 client
->ClientNotify(i
, buf
, kPropertyChangeCallback
, false, key
, change
, 0);
286 int JackEngine::ClientNotify(JackClientInterface
* client
, int refnum
, const char* name
, int notify
, int sync
, const char* message
, int value1
, int value2
)
288 // Check if notification is needed
289 if (!client
->GetClientControl()->fCallback
[notify
]) {
290 jack_log("JackEngine::ClientNotify: no callback for notification = %ld", notify
);
297 if (dynamic_cast<JackExternalClient
*>(client
)) {
298 res1
= client
->ClientNotify(refnum
, name
, notify
, sync
, message
, value1
, value2
);
299 // Important for internal client : unlock before calling the notification callbacks
301 bool res2
= Unlock();
302 res1
= client
->ClientNotify(refnum
, name
, notify
, sync
, message
, value1
, value2
);
309 jack_error("ClientNotify fails name = %s notification = %ld val1 = %ld val2 = %ld", name
, notify
, value1
, value2
);
314 void JackEngine::NotifyClient(int refnum
, int event
, int sync
, const char* message
, int value1
, int value2
)
316 JackClientInterface
* client
= fClientTable
[refnum
];
318 ClientNotify(client
, refnum
, client
->GetClientControl()->fName
, event
, sync
, message
, value1
, value2
);
322 void JackEngine::NotifyClients(int event
, int sync
, const char* message
, int value1
, int value2
)
324 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
325 NotifyClient(i
, event
, sync
, message
, value1
, value2
);
329 int JackEngine::NotifyAddClient(JackClientInterface
* new_client
, const char* new_name
, int refnum
)
331 jack_log("JackEngine::NotifyAddClient: name = %s", new_name
);
333 // Notify existing clients of the new client and new client of existing clients.
334 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
335 JackClientInterface
* old_client
= fClientTable
[i
];
336 if (old_client
&& old_client
!= new_client
) {
337 char* old_name
= old_client
->GetClientControl()->fName
;
338 if (ClientNotify(old_client
, refnum
, new_name
, kAddClient
, false, "", 0, 0) < 0) {
339 jack_error("NotifyAddClient old_client fails name = %s", old_name
);
340 // Not considered as a failure...
342 if (ClientNotify(new_client
, i
, old_name
, kAddClient
, true, "", 0, 0) < 0) {
343 jack_error("NotifyAddClient new_client fails name = %s", new_name
);
352 void JackEngine::NotifyRemoveClient(const char* name
, int refnum
)
354 // Notify existing clients (including the one being suppressed) of the removed client
355 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
356 JackClientInterface
* client
= fClientTable
[i
];
358 ClientNotify(client
, refnum
, name
, kRemoveClient
, false, "", 0, 0);
363 // Coming from the driver
364 void JackEngine::NotifyDriverXRun()
366 // Use the audio thread => request thread communication channel
367 fChannel
.Notify(ALL_CLIENTS
, kXRunCallback
, 0);
370 void JackEngine::NotifyClientXRun(int refnum
)
372 if (refnum
== ALL_CLIENTS
) {
373 NotifyClients(kXRunCallback
, false, "", 0, 0);
375 NotifyClient(refnum
, kXRunCallback
, false, "", 0, 0);
379 void JackEngine::NotifyGraphReorder()
381 ComputeTotalLatencies();
382 NotifyClients(kGraphOrderCallback
, false, "", 0, 0);
385 void JackEngine::NotifyBufferSize(jack_nframes_t buffer_size
)
387 NotifyClients(kBufferSizeCallback
, true, "", buffer_size
, 0);
390 void JackEngine::NotifySampleRate(jack_nframes_t sample_rate
)
392 NotifyClients(kSampleRateCallback
, true, "", sample_rate
, 0);
395 void JackEngine::NotifyFailure(int code
, const char* reason
)
397 NotifyClients(kShutDownCallback
, false, reason
, code
, 0);
400 void JackEngine::NotifyFreewheel(bool onoff
)
404 fEngineControl
->fSavedRealTime
= fEngineControl
->fRealTime
;
405 fEngineControl
->fRealTime
= false;
408 fEngineControl
->fRealTime
= fEngineControl
->fSavedRealTime
;
409 fEngineControl
->fSavedRealTime
= false;
411 NotifyClients((onoff
? kStartFreewheelCallback
: kStopFreewheelCallback
), true, "", 0, 0);
414 void JackEngine::NotifyPortRegistation(jack_port_id_t port_index
, bool onoff
)
416 NotifyClients((onoff
? kPortRegistrationOnCallback
: kPortRegistrationOffCallback
), false, "", port_index
, 0);
419 void JackEngine::NotifyPortRename(jack_port_id_t port
, const char* old_name
)
421 NotifyClients(kPortRenameCallback
, false, old_name
, port
, 0);
424 void JackEngine::NotifyPortConnect(jack_port_id_t src
, jack_port_id_t dst
, bool onoff
)
426 NotifyClients((onoff
? kPortConnectCallback
: kPortDisconnectCallback
), false, "", src
, dst
);
429 void JackEngine::NotifyActivate(int refnum
)
431 NotifyClient(refnum
, kActivateClient
, true, "", 0, 0);
434 //----------------------------
435 // Loadable client management
436 //----------------------------
438 int JackEngine::GetInternalClientName(int refnum
, char* name_res
)
440 JackClientInterface
* client
= fClientTable
[refnum
];
442 strncpy(name_res
, client
->GetClientControl()->fName
, JACK_CLIENT_NAME_SIZE
);
446 int JackEngine::InternalClientHandle(const char* client_name
, int* status
, int* int_ref
)
451 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
452 JackClientInterface
* client
= fClientTable
[i
];
453 if (client
&& dynamic_cast<JackLoadableInternalClient
*>(client
) && (strcmp(client
->GetClientControl()->fName
, client_name
) == 0)) {
454 jack_log("InternalClientHandle found client name = %s ref = %ld", client_name
, i
);
460 *status
|= (JackNoSuchClient
| JackFailure
);
464 int JackEngine::InternalClientUnload(int refnum
, int* status
)
466 JackClientInterface
* client
= fClientTable
[refnum
];
468 int res
= client
->Close();
473 *status
= (JackNoSuchClient
| JackFailure
);
478 //-------------------
480 //-------------------
482 int JackEngine::ClientCheck(const char* name
, jack_uuid_t uuid
, char* name_res
, int protocol
, int options
, int* status
)
486 strcpy(name_res
, name
);
488 jack_log("Check protocol client = %ld server = %ld", protocol
, JACK_PROTOCOL_VERSION
);
490 if (protocol
!= JACK_PROTOCOL_VERSION
) {
491 *status
|= (JackFailure
| JackVersionError
);
492 jack_error("JACK protocol mismatch (%d vs %d)", protocol
, JACK_PROTOCOL_VERSION
);
496 std::map
<int,std::string
>::iterator res
= fReservationMap
.find(uuid
);
498 if (res
!= fReservationMap
.end()) {
499 strncpy(name_res
, res
->second
.c_str(), JACK_CLIENT_NAME_SIZE
);
500 } else if (ClientCheckName(name
)) {
502 *status
|= JackNameNotUnique
;
504 if (options
& JackUseExactName
) {
505 jack_error("cannot create new client; %s already exists", name
);
506 *status
|= JackFailure
;
510 if (GenerateUniqueName(name_res
)) {
511 *status
|= JackFailure
;
519 bool JackEngine::GenerateUniqueName(char* name
)
522 int length
= strlen(name
);
524 if (length
> JACK_CLIENT_NAME_SIZE
- 4) {
525 jack_error("%s exists and is too long to make unique", name
);
526 return true; /* failure */
529 /* generate a unique name by appending "-01".."-99" */
530 name
[length
++] = '-';
537 while (ClientCheckName(name
)) {
538 if (name
[ones
] == '9') {
539 if (name
[tens
] == '9') {
540 jack_error("client %s has 99 extra instances already", name
);
541 return true; /* give up */
552 bool JackEngine::ClientCheckName(const char* name
)
554 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
555 JackClientInterface
* client
= fClientTable
[i
];
556 if (client
&& (strcmp(client
->GetClientControl()->fName
, name
) == 0)) {
561 for (std::map
<int,std::string
>::iterator i
= fReservationMap
.begin(); i
!= fReservationMap
.end(); i
++) {
562 if (i
->second
== name
) {
570 void JackEngine::EnsureUUID(jack_uuid_t uuid
)
572 if (jack_uuid_empty(uuid
))
575 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
576 JackClientInterface
* client
= fClientTable
[i
];
577 if (client
&& jack_uuid_compare(client
->GetClientControl()->fSessionID
, uuid
) == 0) {
578 // FIXME? this code does nothing, but jack1 has it like this too..
579 jack_uuid_clear (&uuid
);
580 // client->GetClientControl()->fSessionID = jack_client_uuid_generate();
585 int JackEngine::GetClientPID(const char* name
)
587 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
588 JackClientInterface
* client
= fClientTable
[i
];
589 if (client
&& (strcmp(client
->GetClientControl()->fName
, name
) == 0)) {
590 return client
->GetClientControl()->fPID
;
597 int JackEngine::GetClientRefNum(const char* name
)
599 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
600 JackClientInterface
* client
= fClientTable
[i
];
601 if (client
&& (strcmp(client
->GetClientControl()->fName
, name
) == 0)) {
602 return client
->GetClientControl()->fRefNum
;
609 // Used for external clients
610 int JackEngine::ClientExternalOpen(const char* name
, int pid
, jack_uuid_t uuid
, int* ref
, int* shared_engine
, int* shared_client
, int* shared_graph_manager
)
612 char real_name
[JACK_CLIENT_NAME_SIZE
+ 1];
614 if (jack_uuid_empty(uuid
)) {
615 uuid
= jack_client_uuid_generate();
616 strncpy(real_name
, name
, JACK_CLIENT_NAME_SIZE
);
618 std::map
<int, std::string
>::iterator res
= fReservationMap
.find(uuid
);
619 if (res
!= fReservationMap
.end()) {
620 strncpy(real_name
, res
->second
.c_str(), JACK_CLIENT_NAME_SIZE
);
621 fReservationMap
.erase(uuid
);
623 strncpy(real_name
, name
, JACK_CLIENT_NAME_SIZE
);
628 jack_log("JackEngine::ClientExternalOpen: uuid = %d, name = %s", uuid
, real_name
);
630 int refnum
= AllocateRefnum();
632 jack_error("No more refnum available");
636 JackExternalClient
* client
= new JackExternalClient();
638 if (!fSynchroTable
[refnum
].Allocate(real_name
, fEngineControl
->fServerName
, 0)) {
639 jack_error("Cannot allocate synchro");
643 if (client
->Open(real_name
, pid
, refnum
, uuid
, shared_client
) < 0) {
644 jack_error("Cannot open client");
648 if (!fSignal
.LockedTimedWait(DRIVER_OPEN_TIMEOUT
* 1000000)) {
649 // Failure if RT thread is not running (problem with the driver...)
650 jack_error("Driver is not running");
654 fClientTable
[refnum
] = client
;
656 if (NotifyAddClient(client
, real_name
, refnum
) < 0) {
657 jack_error("Cannot notify add client");
661 fGraphManager
->InitRefNum(refnum
);
662 fEngineControl
->ResetRollingUsecs();
663 *shared_engine
= fEngineControl
->GetShmIndex();
664 *shared_graph_manager
= fGraphManager
->GetShmIndex();
670 fSynchroTable
[refnum
].Destroy();
671 fClientTable
[refnum
] = 0;
677 // Used for server driver clients
678 int JackEngine::ClientInternalOpen(const char* name
, int* ref
, JackEngineControl
** shared_engine
, JackGraphManager
** shared_manager
, JackClientInterface
* client
, bool wait
)
680 jack_log("JackEngine::ClientInternalOpen: name = %s", name
);
682 int refnum
= AllocateRefnum();
684 jack_error("No more refnum available");
688 if (!fSynchroTable
[refnum
].Allocate(name
, fEngineControl
->fServerName
, 0)) {
689 jack_error("Cannot allocate synchro");
693 if (wait
&& !fSignal
.LockedTimedWait(DRIVER_OPEN_TIMEOUT
* 1000000)) {
694 // Failure if RT thread is not running (problem with the driver...)
695 jack_error("Driver is not running");
699 fClientTable
[refnum
] = client
;
701 if (NotifyAddClient(client
, name
, refnum
) < 0) {
702 jack_error("Cannot notify add client");
706 fGraphManager
->InitRefNum(refnum
);
707 fEngineControl
->ResetRollingUsecs();
708 *shared_engine
= fEngineControl
;
709 *shared_manager
= fGraphManager
;
715 fSynchroTable
[refnum
].Destroy();
716 fClientTable
[refnum
] = 0;
720 // Used for external clients
721 int JackEngine::ClientExternalClose(int refnum
)
723 jack_log("JackEngine::ClientExternalClose ref = %ld", refnum
);
724 JackClientInterface
* client
= fClientTable
[refnum
];
726 int res
= ClientCloseAux(refnum
, true);
732 // Used for server internal clients or drivers when the RT thread is stopped
733 int JackEngine::ClientInternalClose(int refnum
, bool wait
)
735 jack_log("JackEngine::ClientInternalClose ref = %ld", refnum
);
736 return ClientCloseAux(refnum
, wait
);
739 int JackEngine::ClientCloseAux(int refnum
, bool wait
)
741 jack_log("JackEngine::ClientCloseAux ref = %ld", refnum
);
743 JackClientInterface
* client
= fClientTable
[refnum
];
744 fEngineControl
->fTransport
.ResetTimebase(refnum
);
746 jack_uuid_t uuid
= JACK_UUID_EMPTY_INITIALIZER
;
747 jack_uuid_copy (&uuid
, client
->GetClientControl()->fSessionID
);
749 // Unregister all ports ==> notifications are sent
750 jack_int_t ports
[PORT_NUM_FOR_CLIENT
];
753 fGraphManager
->GetInputPorts(refnum
, ports
);
754 for (i
= 0; (i
< PORT_NUM_FOR_CLIENT
) && (ports
[i
] != EMPTY
); i
++) {
755 PortUnRegister(refnum
, ports
[i
]);
758 fGraphManager
->GetOutputPorts(refnum
, ports
);
759 for (i
= 0; (i
< PORT_NUM_FOR_CLIENT
) && (ports
[i
] != EMPTY
); i
++) {
760 PortUnRegister(refnum
, ports
[i
]);
763 // Remove the client from the table
764 ReleaseRefnum(refnum
);
767 fGraphManager
->RemoveAllPorts(refnum
);
769 // Wait until next cycle to be sure client is not used anymore
771 if (!fSignal
.LockedTimedWait(fEngineControl
->fTimeOutUsecs
* 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
772 jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum
);
776 if (fMetadata
.RemoveProperties(NULL
, uuid
) > 0) {
777 /* have to do the notification ourselves, since the client argument
778 to fMetadata->RemoveProperties() was NULL
780 PropertyChangeNotify(uuid
, NULL
, PropertyDeleted
);
783 // Notify running clients
784 NotifyRemoveClient(client
->GetClientControl()->fName
, refnum
);
787 fSynchroTable
[refnum
].Destroy();
788 fEngineControl
->ResetRollingUsecs();
792 int JackEngine::ClientActivate(int refnum
, bool is_real_time
)
794 JackClientInterface
* client
= fClientTable
[refnum
];
795 jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum
, client
->GetClientControl()->fName
);
798 fGraphManager
->Activate(refnum
);
801 // Wait for graph state change to be effective
802 if (!fSignal
.LockedTimedWait(fEngineControl
->fTimeOutUsecs
* 10)) {
803 jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum
, client
->GetClientControl()->fName
);
806 jack_int_t input_ports
[PORT_NUM_FOR_CLIENT
];
807 jack_int_t output_ports
[PORT_NUM_FOR_CLIENT
];
808 fGraphManager
->GetInputPorts(refnum
, input_ports
);
809 fGraphManager
->GetOutputPorts(refnum
, output_ports
);
812 NotifyActivate(refnum
);
814 // Then issue port registration notification
815 for (int i
= 0; (i
< PORT_NUM_FOR_CLIENT
) && (input_ports
[i
] != EMPTY
); i
++) {
816 NotifyPortRegistation(input_ports
[i
], true);
818 for (int i
= 0; (i
< PORT_NUM_FOR_CLIENT
) && (output_ports
[i
] != EMPTY
); i
++) {
819 NotifyPortRegistation(output_ports
[i
], true);
826 // May be called without client
827 int JackEngine::ClientDeactivate(int refnum
)
829 JackClientInterface
* client
= fClientTable
[refnum
];
830 jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum
, client
->GetClientControl()->fName
);
832 jack_int_t input_ports
[PORT_NUM_FOR_CLIENT
];
833 jack_int_t output_ports
[PORT_NUM_FOR_CLIENT
];
834 fGraphManager
->GetInputPorts(refnum
, input_ports
);
835 fGraphManager
->GetOutputPorts(refnum
, output_ports
);
837 // First disconnect all ports
838 for (int i
= 0; (i
< PORT_NUM_FOR_CLIENT
) && (input_ports
[i
] != EMPTY
); i
++) {
839 PortDisconnect(-1, input_ports
[i
], ALL_PORTS
);
841 for (int i
= 0; (i
< PORT_NUM_FOR_CLIENT
) && (output_ports
[i
] != EMPTY
); i
++) {
842 PortDisconnect(-1, output_ports
[i
], ALL_PORTS
);
845 // Then issue port registration notification
846 for (int i
= 0; (i
< PORT_NUM_FOR_CLIENT
) && (input_ports
[i
] != EMPTY
); i
++) {
847 NotifyPortRegistation(input_ports
[i
], false);
849 for (int i
= 0; (i
< PORT_NUM_FOR_CLIENT
) && (output_ports
[i
] != EMPTY
); i
++) {
850 NotifyPortRegistation(output_ports
[i
], false);
853 fGraphManager
->Deactivate(refnum
);
854 fLastSwitchUsecs
= 0; // Force switch to occur next cycle, even when called with "dead" clients
856 // Wait for graph state change to be effective
857 if (!fSignal
.LockedTimedWait(fEngineControl
->fTimeOutUsecs
* 10)) {
858 jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum
, client
->GetClientControl()->fName
);
865 void JackEngine::ClientKill(int refnum
)
867 jack_log("JackEngine::ClientKill ref = %ld", refnum
);
868 if (ClientDeactivate(refnum
) < 0) {
869 jack_error("JackEngine::ClientKill ref = %ld cannot be removed from the graph !!", refnum
);
871 if (ClientExternalClose(refnum
) < 0) {
872 jack_error("JackEngine::ClientKill ref = %ld cannot be closed", refnum
);
880 int JackEngine::PortRegister(int refnum
, const char* name
, const char *type
, unsigned int flags
, unsigned int buffer_size
, jack_port_id_t
* port_index
)
882 jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum
, name
, type
, flags
, buffer_size
);
883 JackClientInterface
* client
= fClientTable
[refnum
];
885 // Check if port name already exists
886 if (fGraphManager
->GetPort(name
) != NO_PORT
) {
887 jack_error("port_name \"%s\" already exists", name
);
891 // buffer_size is actually ignored...
892 *port_index
= fGraphManager
->AllocatePort(refnum
, name
, type
, (JackPortFlags
)flags
, fEngineControl
->fBufferSize
);
893 if (*port_index
!= NO_PORT
) {
894 if (client
->GetClientControl()->fActive
) {
895 NotifyPortRegistation(*port_index
, true);
903 int JackEngine::PortUnRegister(int refnum
, jack_port_id_t port_index
)
905 jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum
, port_index
);
906 JackClientInterface
* client
= fClientTable
[refnum
];
909 // Disconnect port ==> notification is sent
910 PortDisconnect(-1, port_index
, ALL_PORTS
);
912 if (fGraphManager
->ReleasePort(refnum
, port_index
) == 0) {
913 const jack_uuid_t uuid
= jack_port_uuid_generate(port_index
);
914 if (!jack_uuid_empty(uuid
))
916 if (fMetadata
.RemoveProperties(NULL
, uuid
) > 0) {
917 /* have to do the notification ourselves, since the client argument
918 to fMetadata->RemoveProperties() was NULL
920 PropertyChangeNotify(uuid
, NULL
, PropertyDeleted
);
924 if (client
->GetClientControl()->fActive
) {
925 NotifyPortRegistation(port_index
, false);
933 // this check is to prevent apps to self connect to other apps
934 // TODO: make this work with multiple clients per app
935 int JackEngine::CheckPortsConnect(int refnum
, jack_port_id_t src
, jack_port_id_t dst
)
937 if (fSelfConnectMode
== ' ') return 1;
939 JackPort
* src_port
= fGraphManager
->GetPort(src
);
940 JackPort
* dst_port
= fGraphManager
->GetPort(dst
);
942 jack_log("JackEngine::CheckPortsConnect(ref = %d, src = %d, dst = %d)", refnum
, src_port
->GetRefNum(), dst_port
->GetRefNum());
944 //jack_log("%s -> %s", src_port->GetName(), dst_port->GetName());
945 //jack_log("mode = '%c'", fSelfConnectMode);
947 int src_self
= src_port
->GetRefNum() == refnum
? 1 : 0;
948 int dst_self
= dst_port
->GetRefNum() == refnum
? 1 : 0;
950 //jack_log("src_self is %s", src_self ? "true" : "false");
951 //jack_log("dst_self is %s", dst_self ? "true" : "false");
953 // 0 means client is connecting other client ports (control app patchbay functionality)
954 // 1 means client is connecting its own port to port of other client (e.g. self connecting into "system" client)
955 // 2 means client is connecting its own ports (for app internal functionality)
956 int sum
= src_self
+ dst_self
;
957 //jack_log("sum = %d", sum);
958 if (sum
== 0) return 1;
959 char lmode
= tolower(fSelfConnectMode
);
960 //jack_log("lmode = '%c'", lmode);
961 if (sum
== 2 && lmode
== 'e') return 1;
962 bool fail
= lmode
!= fSelfConnectMode
; // fail modes are upper case
963 //jack_log("fail = %d", (int)fail);
966 "%s port self connect request%s (%s -> %s)",
967 fail
? "rejecting" : "ignoring",
968 sum
== 1 ? " to external port" : "",
970 dst_port
->GetName());
972 return fail
? -1 : 0;
975 int JackEngine::PortConnect(int refnum
, const char* src
, const char* dst
)
977 jack_log("JackEngine::PortConnect ref = %d src = %s dst = %s", refnum
, src
, dst
);
978 jack_port_id_t port_src
, port_dst
;
980 return (fGraphManager
->GetTwoPorts(src
, dst
, &port_src
, &port_dst
) < 0)
982 : PortConnect(refnum
, port_src
, port_dst
);
985 int JackEngine::PortConnect(int refnum
, jack_port_id_t src
, jack_port_id_t dst
)
987 jack_log("JackEngine::PortConnect ref = %d src = %d dst = %d", refnum
, src
, dst
);
988 JackClientInterface
* client
;
991 if (fGraphManager
->CheckPorts(src
, dst
) < 0) {
995 ref
= fGraphManager
->GetOutputRefNum(src
);
997 client
= fClientTable
[ref
];
999 if (!client
->GetClientControl()->fActive
) {
1000 jack_error("Cannot connect ports owned by inactive clients:"
1001 " \"%s\" is not active", client
->GetClientControl()->fName
);
1005 ref
= fGraphManager
->GetInputRefNum(dst
);
1007 client
= fClientTable
[ref
];
1009 if (!client
->GetClientControl()->fActive
) {
1010 jack_error("Cannot connect ports owned by inactive clients:"
1011 " \"%s\" is not active", client
->GetClientControl()->fName
);
1015 int res
= CheckPortsConnect(refnum
, src
, dst
);
1020 res
= fGraphManager
->Connect(src
, dst
);
1022 NotifyPortConnect(src
, dst
, true);
1027 int JackEngine::PortDisconnect(int refnum
, const char* src
, const char* dst
)
1029 jack_log("JackEngine::PortDisconnect ref = %d src = %s dst = %s", refnum
, src
, dst
);
1030 jack_port_id_t port_src
, port_dst
;
1032 return (fGraphManager
->GetTwoPorts(src
, dst
, &port_src
, &port_dst
) < 0)
1034 : PortDisconnect(refnum
, port_src
, port_dst
);
1037 int JackEngine::PortDisconnect(int refnum
, jack_port_id_t src
, jack_port_id_t dst
)
1039 jack_log("JackEngine::PortDisconnect ref = %d src = %d dst = %d", refnum
, src
, dst
);
1041 if (dst
== ALL_PORTS
) {
1043 jack_int_t connections
[CONNECTION_NUM_FOR_PORT
];
1044 fGraphManager
->GetConnections(src
, connections
);
1046 JackPort
* port
= fGraphManager
->GetPort(src
);
1048 if (port
->GetFlags() & JackPortIsOutput
) {
1049 for (int i
= 0; (i
< CONNECTION_NUM_FOR_PORT
) && (connections
[i
] != EMPTY
); i
++) {
1050 if (PortDisconnect(refnum
, src
, connections
[i
]) != 0) {
1055 for (int i
= 0; (i
< CONNECTION_NUM_FOR_PORT
) && (connections
[i
] != EMPTY
); i
++) {
1056 if (PortDisconnect(refnum
, connections
[i
], src
) != 0) {
1065 if (fGraphManager
->CheckPorts(src
, dst
) < 0) {
1069 int res
= CheckPortsConnect(refnum
, src
, dst
);
1074 res
= fGraphManager
->Disconnect(src
, dst
);
1076 NotifyPortConnect(src
, dst
, false);
1080 int JackEngine::PortRename(int refnum
, jack_port_id_t port
, const char* name
)
1082 char old_name
[REAL_JACK_PORT_NAME_SIZE
+1];
1083 strcpy(old_name
, fGraphManager
->GetPort(port
)->GetName());
1084 fGraphManager
->GetPort(port
)->SetName(name
);
1085 NotifyPortRename(port
, old_name
);
1089 int JackEngine::PortSetDefaultMetadata(jack_port_id_t port
, const char* pretty_name
)
1091 static const char* type
= "text/plain";
1092 jack_uuid_t uuid
= jack_port_uuid_generate(port
);
1094 int res
= fMetadata
.SetProperty(NULL
, uuid
, JACK_METADATA_HARDWARE
, pretty_name
, type
);
1100 res
= fMetadata
.GetProperty(uuid
, JACK_METADATA_PRETTY_NAME
, &v
, &t
);
1102 res
= fMetadata
.SetProperty(NULL
, uuid
, JACK_METADATA_PRETTY_NAME
, pretty_name
, type
);
1108 //--------------------
1109 // Session management
1110 //--------------------
1112 void JackEngine::SessionNotify(int refnum
, const char *target
, jack_session_event_type_t type
, const char *path
, detail::JackChannelTransactionInterface
*socket
, JackSessionNotifyResult
** result
)
1114 if (fSessionPendingReplies
!= 0) {
1115 JackSessionNotifyResult
res(-1);
1117 jack_log("JackEngine::SessionNotify ... busy");
1118 if (result
!= NULL
) *result
= NULL
;
1122 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
1123 JackClientInterface
* client
= fClientTable
[i
];
1124 if (client
&& jack_uuid_empty(client
->GetClientControl()->fSessionID
)) {
1125 client
->GetClientControl()->fSessionID
= jack_client_uuid_generate();
1128 fSessionResult
= new JackSessionNotifyResult();
1130 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
1131 JackClientInterface
* client
= fClientTable
[i
];
1132 if (client
&& client
->GetClientControl()->fCallback
[kSessionCallback
]) {
1134 // check if this is a notification to a specific client.
1135 if (target
!= NULL
&& strlen(target
) != 0) {
1136 if (strcmp(target
, client
->GetClientControl()->fName
)) {
1141 char path_buf
[JACK_PORT_NAME_SIZE
];
1142 if (path
[strlen(path
) - 1] == DIR_SEPARATOR
) {
1143 snprintf(path_buf
, sizeof path_buf
, "%s%s%c", path
, client
->GetClientControl()->fName
, DIR_SEPARATOR
);
1145 snprintf(path_buf
, sizeof path_buf
, "%s%c%s%c", path
, DIR_SEPARATOR
, client
->GetClientControl()->fName
, DIR_SEPARATOR
);
1148 int res
= JackTools::MkDir(path_buf
);
1149 if (res
) jack_error("JackEngine::SessionNotify: can not create session directory '%s'", path_buf
);
1151 int result
= client
->ClientNotify(i
, client
->GetClientControl()->fName
, kSessionCallback
, true, path_buf
, (int)type
, 0);
1153 if (result
== kPendingSessionReply
) {
1154 fSessionPendingReplies
+= 1;
1155 } else if (result
== kImmediateSessionReply
) {
1156 char uuid_buf
[JACK_UUID_STRING_SIZE
];
1157 jack_uuid_unparse(client
->GetClientControl()->fSessionID
, uuid_buf
);
1158 fSessionResult
->fCommandList
.push_back(JackSessionCommand(uuid_buf
,
1159 client
->GetClientControl()->fName
,
1160 client
->GetClientControl()->fSessionCommand
,
1161 client
->GetClientControl()->fSessionFlags
));
1166 if (result
!= NULL
) *result
= fSessionResult
;
1168 if (fSessionPendingReplies
== 0) {
1169 fSessionResult
->Write(socket
);
1170 if (result
== NULL
) delete fSessionResult
;
1171 fSessionResult
= NULL
;
1173 fSessionTransaction
= socket
;
1177 int JackEngine::SessionReply(int refnum
)
1179 JackClientInterface
* client
= fClientTable
[refnum
];
1181 char uuid_buf
[JACK_UUID_STRING_SIZE
];
1182 jack_uuid_unparse(client
->GetClientControl()->fSessionID
, uuid_buf
);
1183 fSessionResult
->fCommandList
.push_back(JackSessionCommand(uuid_buf
,
1184 client
->GetClientControl()->fName
,
1185 client
->GetClientControl()->fSessionCommand
,
1186 client
->GetClientControl()->fSessionFlags
));
1187 fSessionPendingReplies
-= 1;
1189 if (fSessionPendingReplies
== 0) {
1190 fSessionResult
->Write(fSessionTransaction
);
1191 if (fSessionTransaction
!= NULL
) {
1192 delete fSessionResult
;
1194 fSessionResult
= NULL
;
1200 int JackEngine::GetUUIDForClientName(const char *client_name
, char *uuid_res
)
1202 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
1203 JackClientInterface
* client
= fClientTable
[i
];
1205 if (client
&& (strcmp(client_name
, client
->GetClientControl()->fName
) == 0)) {
1206 jack_uuid_unparse(client
->GetClientControl()->fSessionID
, uuid_res
);
1210 // Did not find name.
1214 int JackEngine::GetClientNameForUUID(const char *uuid_buf
, char *name_res
)
1217 if (jack_uuid_parse(uuid_buf
, &uuid
) != 0)
1220 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
1221 JackClientInterface
* client
= fClientTable
[i
];
1227 if (jack_uuid_compare(client
->GetClientControl()->fSessionID
, uuid
) == 0) {
1228 strncpy(name_res
, client
->GetClientControl()->fName
, JACK_CLIENT_NAME_SIZE
);
1232 // Did not find uuid.
1236 int JackEngine::ReserveClientName(const char *name
, const char *uuidstr
)
1238 jack_log("JackEngine::ReserveClientName ( name = %s, uuid = %s )", name
, uuidstr
);
1240 if (ClientCheckName(name
)) {
1241 jack_log("name already taken");
1246 if (jack_uuid_parse(uuidstr
, &uuid
) != 0) {
1247 jack_error("JackEngine::ReserveClientName invalid uuid %s", uuidstr
);
1252 fReservationMap
[uuid
] = name
;
1256 int JackEngine::ClientHasSessionCallback(const char *name
)
1258 JackClientInterface
* client
= NULL
;
1259 for (int i
= 0; i
< CLIENT_NUM
; i
++) {
1260 client
= fClientTable
[i
];
1261 if (client
&& (strcmp(client
->GetClientControl()->fName
, name
) == 0)) {
1267 return client
->GetClientControl()->fCallback
[kSessionCallback
];
1273 } // end of namespace