Skip to content

Commit c75b3f1

Browse files
authored
Avoid broken jack_on_info_shutdown callback and more checks (#16)
1 parent 1fb4806 commit c75b3f1

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

apf/jack_policy.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,22 @@ struct thread_traits; // definition in mimoprocessor.h
123123
template<>
124124
struct thread_traits<jack_policy, pthread_t>
125125
{
126-
static void set_priority(const jack_policy& obj, pthread_t thread_id)
126+
static void update_priority(const jack_policy& obj, pthread_t thread_id) noexcept
127127
{
128128
if (obj.is_realtime())
129129
{
130+
#ifdef APF_JACK_POLICY_DEBUG
131+
printf("Trying to set priority...");
132+
#endif
130133
struct sched_param param;
131134
param.sched_priority = obj.get_real_time_priority();
132135
if (pthread_setschedparam(thread_id, SCHED_FIFO, &param))
133136
{
134-
throw std::runtime_error("Can't set scheduling priority for thread!");
137+
// We were trying our best to set the priority, but if it doesn't work,
138+
// the show must go on!
139+
#ifdef APF_JACK_POLICY_DEBUG
140+
printf("Can't set scheduling priority %d for thread!\n", param.sched_priority);
141+
#endif
135142
}
136143
}
137144
else
@@ -219,9 +226,11 @@ jack_policy::Xput<X>::_init_port(const parameter_map& p, jack_policy& parent)
219226

220227
name = p.get(X::prefix_name(), X::default_prefix()) + id;
221228
}
222-
223-
return X::is_input
224-
? parent.register_in_port(name) : parent.register_out_port(name);
229+
JackClient::port_t* rport = X::is_input ? parent.register_in_port(name) : parent.register_out_port(name);
230+
if (rport==NULL){
231+
throw std::runtime_error("Could not register JACK port!");
232+
}
233+
return rport;
225234
}
226235

227236
template<typename X>

apf/jackclient.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include <errno.h> // for EEXIST
4242

4343
#ifdef APF_JACKCLIENT_DEBUG
44-
#include <iostream>
4544
#include <jack/statistics.h> // for jack_get_xrun_delayed_usecs()
4645
#define APF_JACKCLIENT_DEBUG_MSG(str) \
4746
do { std::cout << "apf::JackClient: " << str << std::endl; } while (false)
@@ -126,6 +125,7 @@ class JackClient
126125
/// @see jack_activate()
127126
bool activate() const
128127
{
128+
APF_JACKCLIENT_DEBUG_MSG("Activating JACK client.");
129129
if (!_client || jack_activate(_client)) return false;
130130

131131
this->connect_pending_connections();
@@ -233,6 +233,7 @@ class JackClient
233233
<< still_pending_connections.size());
234234

235235
_pending_connections.swap(still_pending_connections);
236+
236237
return _pending_connections.empty();
237238
}
238239

@@ -352,15 +353,10 @@ class JackClient
352353
/// JACK shutdown callback.
353354
/// By default, this is throwing a jack_error exception. If you don't like
354355
/// this, you can overwrite this function in your derived class.
355-
/// @param code status code, see JackInfoShutdownCallback
356-
/// @param reason a string describing the shutdown reason
357-
/// @see JackInfoShutdownCallback and jack_on_info_shutdown()
358-
/// @note There is also JackShutdownCallback and jack_on_shutdown(), but
359-
/// this one is more useful.
360-
virtual void jack_shutdown_callback(jack_status_t code, const char* reason)
356+
/// @see There is also JackShutdownCallback and jack_on_shutdown()
357+
virtual void jack_shutdown_callback()
361358
{
362-
(void)code; // avoid "unused parameter" warning
363-
throw jack_error("JACK shutdown! Reason: " + std::string(reason));
359+
throw jack_error("JACK shutdown!");
364360
}
365361

366362
/// JACK sample rate callback.
@@ -408,10 +404,9 @@ class JackClient
408404
return static_cast<JackClient*>(arg)->jack_sync_callback(state, pos);
409405
}
410406

411-
static void _jack_shutdown_callback(jack_status_t code
412-
, const char* reason, void* arg)
407+
static void _jack_shutdown_callback(void* arg)
413408
{
414-
static_cast<JackClient*>(arg)->jack_shutdown_callback(code, reason);
409+
static_cast<JackClient*>(arg)->jack_shutdown_callback();
415410
}
416411

417412
static int _jack_sample_rate_callback(nframes_t sr, void* arg)
@@ -436,8 +431,11 @@ class JackClient
436431
, const std::string& destination
437432
, _pending_connections_t& pending_connections) const
438433
{
434+
APF_JACKCLIENT_DEBUG_MSG("Connection: " << source << " -> " << destination);
439435
if (_client == nullptr) return false;
440436
int success = jack_connect(_client, source.c_str(), destination.c_str());
437+
APF_JACKCLIENT_DEBUG_MSG("Connection returned: " << success);
438+
441439
switch (success)
442440
{
443441
case 0:
@@ -504,6 +502,14 @@ JackClient::JackClient(const std::string& name
504502
_client = jack_client_open(name.c_str(), options, &status);
505503
if (!_client) throw jack_error(status);
506504

505+
if (status & JackServerStarted) {
506+
APF_JACKCLIENT_DEBUG_MSG("Server started.");
507+
}
508+
if (status & JackNameNotUnique) {
509+
_client_name = jack_get_client_name(_client);
510+
APF_JACKCLIENT_DEBUG_MSG("Unique name assigned: " << _client_name);
511+
}
512+
507513
if (options & JackUseExactName)
508514
{
509515
assert(_client_name == jack_get_client_name(_client));
@@ -525,15 +531,15 @@ JackClient::JackClient(const std::string& name
525531
}
526532

527533
// TODO: separate option to disable sync callback?
528-
529534
if (jack_set_sync_callback(_client, _jack_sync_callback, this))
530535
{
531536
throw jack_error("Could not set sync callback function for '"
532537
+ _client_name + "'!");
533538
}
534539
}
535-
536-
jack_on_info_shutdown(_client, _jack_shutdown_callback, this);
540+
// jack_on_info_shutdown CAUSES ISSUES ON WINDOWS
541+
// Using jack_on_shutdown instead
542+
jack_on_shutdown(_client, _jack_shutdown_callback, this);
537543

538544
if (jack_set_xrun_callback(_client, _jack_xrun_callback, this))
539545
{

apf/mimoprocessor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace apf
7878
template<typename interface_policy, typename native_handle_type>
7979
struct thread_traits
8080
{
81-
static void set_priority(const interface_policy&, native_handle_type) {}
81+
static void update_priority(const interface_policy&, native_handle_type) noexcept {}
8282
};
8383

8484
class enable_queries
@@ -371,7 +371,7 @@ class MimoProcessor : public interface_policy
371371
{
372372
// Set thread priority from interface_policy, if available
373373
thread_traits<interface_policy
374-
, std::thread::native_handle_type>::set_priority(parent
374+
, std::thread::native_handle_type>::update_priority(parent
375375
, _thread.native_handle());
376376
}
377377

0 commit comments

Comments
 (0)