Skip to content

Commit

Permalink
add client, fix weak ptr expire
Browse files Browse the repository at this point in the history
  • Loading branch information
jpietek committed Jun 6, 2024
1 parent dde943f commit 45276d2
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/JackClient.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "graph_interfaces.hpp"
#include "instance_shared.hpp"
#include <jack/jack.h>
#include <jack/types.h>
#include <string>
#include <vector>

extern "C" {
#include <libavutil/samplefmt.h>
}

class JackClient : public InstanceShared<JackClient> {
protected:
jack_client_t *jack_client_;
std::string client_name_;
std::vector<std::weak_ptr<IJackSink>> sinks_;

static int jack_process_callback(jack_nframes_t nframes, void *p) {
auto *jc = (JackClient *)(p);
auto start = std::chrono::high_resolution_clock::now();
for (const auto &sptr : jc->sinks_) {
auto s = sptr.lock();
s->jack_process(nframes);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
//logstream << "jack process duration" << " " << duration.count();
return 0;
}

public:

~JackClient() {
jack_deactivate(jack_client_);
if (jack_client_) {
jack_client_close(jack_client_);
}
}

void setClientName(std::string name) { client_name_ = name; }

void addSink(std::weak_ptr<IJackSink> sink) {
if (!sinks_.empty()) {
sinks_.erase(std::remove_if(
sinks_.begin(), sinks_.end(),
[](std::weak_ptr<IJackSink> &s) { return s.expired(); }),
sinks_.end());
}
sinks_.push_back(sink);
}

jack_client_t *instance() {
if (!jack_client_) {
jack_status_t status;
jack_client_ =
jack_client_open(client_name_.c_str(), JackNoStartServer, &status);
if (jack_client_ == nullptr) {
throw Error("unable to create jack client, status: " +
std::to_string(status));
}

jack_set_process_callback(jack_client_, jack_process_callback, this);
if (jack_activate(jack_client_) != 0) {
throw Error("cannot activate client");
}
}
return jack_client_;
}
};

0 comments on commit 45276d2

Please sign in to comment.