src/JackHandler.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 30 Sep 2020 21:37:01 +0200
branchv_0
changeset 5 6be3464ccb2b
parent 4 65dfbf0494a3
child 6 4174fc0c2e7c
permissions -rw-r--r--
wait for a port connection

/**
 * Relational pipes
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * 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, version 3 of the License.
 *
 * 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/>.
 */
#pragma once

#include <memory>
#include <atomic>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <sys/mman.h>
#include <signal.h>
#include <unistd.h>

#include <jack/jack.h>
#include <jack/midiport.h>
#include <jack/ringbuffer.h>

#include <relpipe/common/type/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>

#include "Configuration.h"
#include "JackException.h"

namespace relpipe {
namespace out {
namespace jack {

int dequeueMessages(jack_nframes_t frames, void* arg);

class JackHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
private:
	Configuration& configuration;
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding

	jack_client_t* jackClient = nullptr;
	jack_port_t* jackPort = nullptr;
	jack_ringbuffer_t* ringBuffer = nullptr;
	pthread_mutex_t messageThreadLock = PTHREAD_MUTEX_INITIALIZER;
	pthread_cond_t dataReady = PTHREAD_COND_INITIALIZER;

	std::atomic<bool> continueProcessing{true};

	const int RING_BUFFER_SIZE = 100;

	struct MidiMessage {
		uint8_t buffer[4096];
		uint32_t size;
		uint32_t time;
	};

	/**
	 * The message being prepared before enqueing in the ringBuffer.
	 * Not the message dequeued from the ringBuffer (different thread).
	 */
	MidiMessage currentMidiMessage;
	size_t currentAttributeCount = 0;
	size_t currentAttributeIndex = 0;


public:

	JackHandler(Configuration& configuration) : configuration(configuration) {
		// Initialize JACK connection:
		std::string clientName = convertor.to_bytes(configuration.jackClientName);
		jackClient = jack_client_open(clientName.c_str(), JackNullOption, nullptr);
		if (jackClient == nullptr) throw JackException(L"Could not create JACK client.");

		ringBuffer = jack_ringbuffer_create(RING_BUFFER_SIZE * sizeof (MidiMessage));

		jack_set_process_callback(jackClient, relpipe::out::jack::dequeueMessages, this);
		// TODO: report also other events (connections etc.)

		jackPort = jack_port_register(jackClient, "output", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
		if (jackPort == nullptr) throw JackException(L"Could not register port.");

		if (mlockall(MCL_CURRENT | MCL_FUTURE)) fwprintf(stderr, L"Warning: Can not lock memory.\n");

		int jackError = jack_activate(jackClient);
		if (jackError) throw JackException(L"Could not activate client.");

		// Wait for a port connection, because it does not make much sense to send MIDI events nowhere:
		// TODO: configurable waiting?
		while (jack_port_connected(jackPort) == 0) usleep(10000);
	}

	void startRelation(const relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
		// TODO: validate metadata and prepare attribute mappings (names and types are important, order does not matter)

		currentAttributeCount = attributes.size();
	}

	void attribute(const relpipe::common::type::StringX& value) override {
		// TODO: append to current message + if this is last attribute, put whole message to the ring buffer
		// TODO: if (continueProcessing) {} ?

		if (currentAttributeIndex < currentAttributeCount) {
			// memcpy(currentMidiMessage.buffer, ….buffer, ….size);
			// currentMidiMessage.size = …;
			// currentMidiMessage.time = …;
			currentAttributeIndex++;
		} else {
			if (jack_ringbuffer_write_space(ringBuffer) >= sizeof (MidiMessage)) {
				jack_ringbuffer_write(ringBuffer, (const char *) &currentMidiMessage, sizeof (MidiMessage));
			} else {
				fwprintf(stderr, L"Error: ring buffer is full → skipping event.\n");
			}

			if (pthread_mutex_trylock(&messageThreadLock) == 0) {
				pthread_cond_signal(&dataReady);
				pthread_mutex_unlock(&messageThreadLock);
			}

			currentMidiMessage = MidiMessage();
			currentAttributeIndex = 0;
		}

	}

	void endOfPipe() {
		// TODO: send optional (configurable) MIDI events

		// Wait until the ring buffer is empty
		while (continueProcessing && jack_ringbuffer_read_space(ringBuffer)) usleep(1000);
	}

	int dequeueMessages(jack_nframes_t frames) {

		std::cout << "dequeueMessages(" << frames << ")" << std::endl; // TODO: remove debug message

		// Process messages from the ring buffer queue:
		pthread_mutex_lock(&messageThreadLock);
		while (continueProcessing) { // TODO: is continueProcessing needed?
			const size_t queuedMessages = jack_ringbuffer_read_space(ringBuffer) / sizeof (MidiMessage);
			for (size_t i = 0; i < queuedMessages; ++i) {
				MidiMessage m;
				jack_ringbuffer_read(ringBuffer, (char*) &m, sizeof (MidiMessage));
				// TODO: send events from the ring buffer to JACK + correct timing
				// FIXME: do not block here in the while loop; check frames
				std::cout << "will process MidiMessage: " << &m << "" << std::endl; // TODO: remove debug message
			}
			pthread_cond_wait(&dataReady, &messageThreadLock);
		}
		pthread_mutex_unlock(&messageThreadLock);


		return 0;
	}

	void finish(int sig) {
		continueProcessing = false;
	}

	virtual ~JackHandler() {
		// Close JACK connection:
		jack_deactivate(jackClient);
		jack_client_close(jackClient);
		jack_ringbuffer_free(ringBuffer);
	}

};

int dequeueMessages(jack_nframes_t frames, void* arg) {
	JackHandler* instance = (JackHandler*) arg;
	return instance->dequeueMessages(frames);
}

}
}
}