sndio-input.c 10.4 KB
Newer Older
V
Vadim Zhukov 已提交
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
/*
Copyright (C) 2020 by Vadim Zhukov <zhuk@openbsd.org>

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, either version 2 of the License, or
(at your option) any later version.

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/>.
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <sndio.h>
#include <string.h>
#include <unistd.h>

#include <util/bmem.h>
#include <util/platform.h>
#include <util/threading.h>
#include <util/util_uint64.h>
#include <obs.h>
#include <obs-module.h>

#include "sndio-input.h"

#define blog(level, msg, ...) \
	blog(level, "sndio-input: %s: " msg, __func__, ##__VA_ARGS__);

#define berr(level, msg, ...)                                   \
	do {                                                    \
K
Kurt Kartaltepe 已提交
41
		const char *errstr = strerror(errno);           \
V
Vadim Zhukov 已提交
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
		blog(level, msg ": %s", ##__VA_ARGS__, errstr); \
	} while (0)

#define NSEC_PER_SEC 1000000000LL

/**
 * Returns the name of the plugin
 */
static const char *sndio_input_getname(void *unused)
{
	UNUSED_PARAMETER(unused);
	return obs_module_text("SndioInput");
}

static enum speaker_layout sndio_channels_to_obs_speakers(int channels)
{
	switch (channels) {
	case 1:
		return SPEAKERS_MONO;
	case 2:
		return SPEAKERS_STEREO;
	case 3:
		return SPEAKERS_2POINT1;
	case 4:
		return SPEAKERS_4POINT0;
	case 5:
		return SPEAKERS_4POINT1;
	case 6:
		return SPEAKERS_5POINT1;
	case 8:
		return SPEAKERS_7POINT1;
	}
	return SPEAKERS_UNKNOWN;
}

static enum audio_format sndio_to_obs_audio_format(const struct sio_par *par)
{
	switch (par->bits) {
	case 8:
		return AUDIO_FORMAT_U8BIT;
	case 16:
		return AUDIO_FORMAT_16BIT;
	case 32:
		return AUDIO_FORMAT_32BIT;
	}
	return AUDIO_FORMAT_UNKNOWN;
}

/**
 * Runs sndio tasks on a pre-opened audio device.
 * Whenever user chooses another device, this thread gets signalled to exit,
 * and another one starts immediately, without waiting for the previous.
 */
static void *sndio_thread(void *attr)
{
	struct sndio_thr_data *thrdata = attr;
	size_t msgread = 0; // msg bytes read from socket
	size_t nsiofds = sio_nfds(thrdata->hdl);
	struct pollfd pfd[1 + nsiofds];
	struct sio_par par;
	uint64_t ts;
	ssize_t nread;
	int pollres;
	uint8_t *buf;
	size_t bufsz;

	ts = os_gettime_ns();

	bufsz = thrdata->par.appbufsz * thrdata->par.bps * 2;
	if ((buf = bmalloc(bufsz * 2)) == NULL) {
		blog(LOG_ERROR, "could not allocate record buffer of %zu bytes",
		     bufsz);
		goto finish;
	}

	memset(pfd, 0, sizeof(pfd));
	pfd[0].fd = thrdata->sock;

	for (;;) {
		for (size_t i = 0; i <= nsiofds; i++)
			pfd[i].revents = 0;
		pfd[0].events = POLLIN;
		sio_pollfd(thrdata->hdl, pfd + 1, POLLIN);

		if (poll(pfd, 1 + nsiofds, /*INFTIM*/ -1) == -1) {
			if (errno == EINTR)
				continue;
			berr(LOG_ERROR, "exiting due to poll error");
			goto finish;
		}

		if ((pfd[0].revents & POLLHUP) == POLLHUP) {
			blog(LOG_INFO,
			     "exiting upon receiving EOF at IPC socket");
			goto finish;
		}
		if ((pfd[0].revents & POLLIN) == POLLIN) {
			nread = read(pfd[0].fd, ((uint8_t *)&par) + msgread,
				     sizeof(par) - msgread);
			switch (nread) {
			case -1:
				if (errno == EAGAIN)
					goto proceed_sio;
				berr(LOG_ERROR,
				     "reading from IPC socket failed");
				goto finish;

			case 0:
				blog(LOG_INFO,
				     "exiting upon receiving EOF at IPC socket");
				goto finish;

			default:
				msgread += (size_t)nread;
				if (msgread == sizeof(struct sio_par)) {
					size_t tbufsz;
					uint8_t *tbuf;

					msgread = 0;
					sio_stop(thrdata->hdl);
					if (!sio_setpar(thrdata->hdl, &par)) {
						blog(LOG_WARNING,
						     "sio_setpar failed, keeping old params");
					}
					blog(LOG_INFO,
					     "after sio_setpar(): appbufsz=%u bps=%u",
					     par.appbufsz, par.bps);
					memcpy(&thrdata->par, &par,
					       sizeof(struct sio_par));

					tbufsz = thrdata->par.appbufsz *
						 thrdata->par.bps * 2;
					if ((tbuf = brealloc(buf, tbufsz)) ==
					    NULL) {
						blog(LOG_ERROR,
						     "could not reallocate record buffer of %zu bytes",
						     tbufsz);
						goto finish;
					}
					buf = tbuf;
					bufsz = tbufsz;

					if (!sio_start(thrdata->hdl)) {
						blog(LOG_ERROR,
						     "sio_start failed, exiting");
						goto finish;
					}
					ts = os_gettime_ns();
					// Since we restarted recording,
					// do not try to handle events we lost.
					continue;
				}
			}
		}

	proceed_sio:
		pollres = sio_revents(thrdata->hdl, pfd + 1);
		if ((pollres & POLLHUP) == POLLHUP) {
			blog(LOG_ERROR, "sndio device error happened, exiting");
			goto finish;
		}
		if ((pollres & POLLIN) == POLLIN) {
			struct obs_source_audio out;
			unsigned int nframes;

			nread = (ssize_t)sio_read(thrdata->hdl, buf, bufsz);
			if (nread == 0) {
				if (sio_eof(thrdata->hdl)) {
					blog(LOG_ERROR,
					     "sndio device EOF happened, exiting");
					goto finish;
				}
				continue;
			}
			nframes = (unsigned int)nread / thrdata->par.bps;
			//blog(LOG_INFO, "sio_read returned %u, nframes = %u", nread, nframes);

			memset(&out, 0, sizeof(struct obs_source_audio));
			out.data[0] = buf;
			out.frames = nframes;
			out.format = sndio_to_obs_audio_format(&thrdata->par);
			out.speakers = sndio_channels_to_obs_speakers(
				thrdata->par.rchan);
			out.samples_per_sec = thrdata->par.rate;
			out.timestamp = ts;

			ts += util_mul_div64(nframes, NSEC_PER_SEC,
					     thrdata->par.rate);

			obs_source_output_audio(thrdata->source, &out);
		}
	}

finish:
	sio_close(thrdata->hdl);
	close(thrdata->sock);
	bfree(buf);
	bfree(thrdata);
	return NULL;
}

/**
 * Destroy the plugin object and free all memory
 */
static void sndio_destroy(void *vptr)
{
	struct sndio_data *data = vptr;

	if (!data)
		return;
	close(data->sock);
	data->sock = -1;
	pthread_attr_destroy(&data->attr);
	bfree(data);
}

/**
 * Tries to apply the input settings.
 * Must be called on stopped device.
 */
static void sndio_apply(struct sndio_data *data, obs_data_t *settings)
{
	struct sndio_thr_data *thrdata;
	pthread_t thread;
	int ec;
	int socks[2] = {-1, -1};
	const char *devname = obs_data_get_string(settings, "device");

	if ((thrdata = bzalloc(sizeof(struct sndio_thr_data))) == NULL) {
		blog(LOG_ERROR, "malloc");
		return;
	}
	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0,
		       socks) == -1) {
		berr(LOG_ERROR, "socketpair");
		goto error;
	}

	if (data->sock != -1)
		close(data->sock);
	data->sock = socks[0];
	thrdata->sock = socks[1];
	thrdata->source = data->source;

	thrdata->hdl = sio_open(devname, SIO_REC, 0);
	if (!thrdata->hdl) {
		berr(LOG_ERROR, "could not open %s sndio device", devname);
		goto error;
	}

	sio_initpar(&thrdata->par);
	thrdata->par.bits = obs_data_get_int(settings, "bits");
	thrdata->par.bps = SIO_BPS(thrdata->par.bits);
	thrdata->par.sig = thrdata->par.bits > 8;
	thrdata->par.le = 1;
	thrdata->par.rate = obs_data_get_int(settings, "rate");
	thrdata->par.rchan = obs_data_get_int(settings, "channels");
	thrdata->par.xrun = SIO_SYNC; // makes timestamping easy
	if (!sio_setpar(thrdata->hdl, &thrdata->par)) {
		berr(LOG_ERROR, "could not set parameters for %s sndio device",
		     devname);
		goto error;
	}
	blog(LOG_INFO, "after initial sio_setpar(): appbufsz=%u bps=%u",
	     thrdata->par.appbufsz, thrdata->par.bps);

	if (!sio_start(thrdata->hdl)) {
		berr(LOG_ERROR, "could not start recording on %s sndio device",
		     devname);
		goto error;
	}

	ec = pthread_create(&thread, &data->attr, sndio_thread, thrdata);
	if (ec != 0) {
		blog(LOG_ERROR, "could not start thread");
		goto error;
	}

	return;

error:
	if (thrdata->hdl != NULL)
		sio_close(thrdata->hdl);
	close(socks[0]);
	close(socks[1]);
	bfree(thrdata);
}

/**
 * Update the input settings.
 */
static void sndio_update(void *vptr, obs_data_t *settings)
{
	struct sndio_data *data = vptr;
	sndio_apply(data, settings);
}

/**
 * Create the plugin object
 */
static void *sndio_create(obs_data_t *settings, obs_source_t *source)
{
	struct sndio_data *data = bzalloc(sizeof(struct sndio_data));
	pthread_attr_init(&data->attr);
	pthread_attr_setdetachstate(&data->attr, PTHREAD_CREATE_DETACHED);
	data->source = source;
	data->sock = -1;
	sndio_apply(data, settings);
	return data;
}

/**
 * Get plugin defaults
 */
static void sndio_input_defaults(obs_data_t *settings)
{
	obs_data_set_default_string(settings, "device", SIO_DEVANY);
	obs_data_set_default_int(settings, "rate", 48000);
	obs_data_set_default_int(settings, "bits", 16);
	obs_data_set_default_int(settings, "channels", 2);
}

/**
 * Get plugin properties
 */
static obs_properties_t *sndio_input_properties(void *unused)
{
	obs_property_t *rate, *bits;

	UNUSED_PARAMETER(unused);

	obs_properties_t *props = obs_properties_create();

	obs_properties_add_text(props, "device", obs_module_text("Device"),
				OBS_TEXT_DEFAULT);

	rate = obs_properties_add_list(props, "rate", obs_module_text("Rate"),
				       OBS_COMBO_TYPE_LIST,
				       OBS_COMBO_FORMAT_INT);
	obs_property_list_add_int(rate, "11025 Hz", 11025);
	obs_property_list_add_int(rate, "22050 Hz", 22050);
	obs_property_list_add_int(rate, "32000 Hz", 32000);
	obs_property_list_add_int(rate, "44100 Hz", 44100);
	obs_property_list_add_int(rate, "48000 Hz", 48000);
	obs_property_list_add_int(rate, "96000 Hz", 96000);
	obs_property_list_add_int(rate, "192000 Hz", 192000);

	bits = obs_properties_add_list(props, "bits",
				       obs_module_text("BitsPerSample"),
				       OBS_COMBO_TYPE_LIST,
				       OBS_COMBO_FORMAT_INT);
	obs_property_list_add_int(bits, "8", 8);
	obs_property_list_add_int(bits, "16", 16);
	obs_property_list_add_int(bits, "32", 32);

	obs_properties_add_int(props, "channels", obs_module_text("Channels"),
			       1, 8, 1);

	return props;
}

struct obs_source_info sndio_output_capture = {
	.id = "sndio_output_capture",
	.type = OBS_SOURCE_TYPE_INPUT,
	.output_flags = OBS_SOURCE_AUDIO,
	.get_name = sndio_input_getname,
	.create = sndio_create,
	.destroy = sndio_destroy,
#if SHUTDOWN_ON_DEACTIVATE
	.activate = sndio_activate,
	.deactivate = sndio_deactivate,
#endif
	.update = sndio_update,
	.get_defaults = sndio_input_defaults,
	.get_properties = sndio_input_properties,
	.icon_type = OBS_ICON_TYPE_AUDIO_OUTPUT,
};