summaryrefslogtreecommitdiff
path: root/modules/aubridge/device.c
blob: 2cd9a6040895302a19e6c6568e410062f1d4b131 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
 * @file device.c Audio bridge -- virtual device table
 *
 * Copyright (C) 2010 Creytiv.com
 */
#include <re.h>
#include <rem.h>
#include <baresip.h>
#include <pthread.h>
#include "aubridge.h"


/* The packet-time is fixed to 20 milliseconds */
enum {PTIME = 20};


struct device {
	struct le le;
	const struct ausrc_st *ausrc;
	const struct auplay_st *auplay;
	char name[64];
	pthread_t thread;
	volatile bool run;
};


static void destructor(void *arg)
{
	struct device *dev = arg;

	device_stop(dev);

	list_unlink(&dev->le);
}


static bool list_apply_handler(struct le *le, void *arg)
{
	struct device *st = le->data;

	return 0 == str_cmp(st->name, arg);
}


static struct device *find_device(const char *device)
{
	return list_ledata(hash_lookup(ht_device, hash_joaat_str(device),
				       list_apply_handler, (void *)device));
}


static void *device_thread(void *arg)
{
	uint64_t now, ts = tmr_jiffies();
	struct device *dev = arg;
	struct auresamp rs;
	int16_t *sampv_in, *sampv_out;
	size_t sampc_in;
	size_t sampc_out;
	int err;

	sampc_in = dev->auplay->prm.srate * dev->auplay->prm.ch * PTIME/1000;
	sampc_out = dev->ausrc->prm.srate * dev->ausrc->prm.ch * PTIME/1000;

	auresamp_init(&rs);

	sampv_in  = mem_alloc(2 * sampc_in, NULL);
	sampv_out = mem_alloc(2 * sampc_out, NULL);
	if (!sampv_in || !sampv_out)
		goto out;

	err = auresamp_setup(&rs,
			     dev->auplay->prm.srate, dev->auplay->prm.ch,
			     dev->ausrc->prm.srate, dev->ausrc->prm.ch);
	if (err)
		goto out;

	while (dev->run) {

		(void)sys_msleep(4);

		if (!dev->run)
			break;

		now = tmr_jiffies();

		if (ts > now)
			continue;

		if (dev->auplay && dev->auplay->wh) {
			dev->auplay->wh(sampv_in, sampc_in, dev->auplay->arg);
		}

		err = auresamp(&rs,
			       sampv_out, &sampc_out,
			       sampv_in, sampc_in);
		if (err) {
			warning("aubridge: auresamp error: %m\n", err);
		}

		if (dev->ausrc && dev->ausrc->rh) {
			dev->ausrc->rh(sampv_out, sampc_out,
				       dev->ausrc->arg);
		}

		ts += PTIME;
	}

 out:
	mem_deref(sampv_in);
	mem_deref(sampv_out);

	return NULL;
}


int device_connect(struct device **devp, const char *device,
		   struct auplay_st *auplay, struct ausrc_st *ausrc)
{
	struct device *dev;
	int err = 0;

	if (!devp)
		return EINVAL;
	if (!str_isset(device))
		return ENODEV;

	dev = find_device(device);
	if (dev) {
		*devp = mem_ref(dev);
	}
	else {
		dev = mem_zalloc(sizeof(*dev), destructor);
		if (!dev)
			return ENOMEM;

		str_ncpy(dev->name, device, sizeof(dev->name));

		hash_append(ht_device, hash_joaat_str(device), &dev->le, dev);

		*devp = dev;

		debug("aubridge: created device '%s'\n", device);
	}

	if (auplay)
		dev->auplay = auplay;
	if (ausrc)
		dev->ausrc = ausrc;

	/* wait until we have both SRC+PLAY */
	if (dev->ausrc && dev->auplay && !dev->run) {

		dev->run = true;
		err = pthread_create(&dev->thread, NULL, device_thread, dev);
		if (err) {
			dev->run = false;
		}
	}

	return err;
}


void device_stop(struct device *dev)
{
	if (!dev)
		return;

	dev->auplay = NULL;
	dev->ausrc = NULL;

	if (dev->run) {
		dev->run = false;
		pthread_join(dev->thread, NULL);
	}
}