summaryrefslogtreecommitdiffstats
path: root/spa/plugins/jack/jack-client.c
blob: b3cdcc7f2916ce2bca4e187b73f24c65c15d4721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Spa JACK Client
 *
 * Copyright © 2019 Wim Taymans
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <errno.h>

#include "jack-client.h"

static int jack_process(jack_nframes_t nframes, void *arg)
{
	struct spa_jack_client *client = arg;

	jack_get_cycle_times(client->client,
			&client->current_frames, &client->current_usecs,
			&client->next_usecs, &client->period_usecs);

	jack_transport_query (client->client, &client->pos);

	client->buffer_size = nframes;

	spa_jack_client_emit_process(client);

	return 0;
}

static void jack_shutdown(void* arg)
{
	struct spa_jack_client *client = arg;

	spa_jack_client_emit_shutdown(client);

	spa_hook_list_init(&client->listener_list);
	client->client = NULL;
}

static int status_to_result(jack_status_t status)
{
	int res;

	if (status & JackInvalidOption)
		res = -EINVAL;
	else if (status & JackServerFailed)
		res = -ECONNREFUSED;
	else if (status & JackVersionError)
		res = -EPROTO;
	else if (status & JackInitFailure)
		res = -EIO;
	else
		res = -EFAULT;

	return res;
}

int spa_jack_client_open(struct spa_jack_client *client,
		const char *client_name, const char *server_name)
{
	jack_status_t status;

	if (client->client)
		return 0;

	client->client = jack_client_open(client_name,
			JackNoStartServer, &status, NULL);

	if (client->client == NULL)
		return status_to_result(status);

	spa_hook_list_init(&client->listener_list);

	jack_set_process_callback(client->client, jack_process, client);
	jack_on_shutdown(client->client, jack_shutdown, client);
	client->frame_rate = jack_get_sample_rate(client->client);
	client->buffer_size = jack_get_buffer_size(client->client);

	return 0;
}


int spa_jack_client_close(struct spa_jack_client *client)
{
	if (client->client == NULL)
		return 0;

	spa_jack_client_emit_destroy(client);

	if (jack_client_close(client->client) != 0)
		return -EIO;

	spa_hook_list_init(&client->listener_list);
	client->client = NULL;

	return 0;
}