sndio-input.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. Copyright (C) 2020 by Vadim Zhukov <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <errno.h>
  17. #include <poll.h>
  18. #include <pthread.h>
  19. #include <sndio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <util/bmem.h>
  23. #include <util/platform.h>
  24. #include <util/threading.h>
  25. #include <util/util_uint64.h>
  26. #include <obs.h>
  27. #include <obs-module.h>
  28. #include "sndio-input.h"
  29. #define blog(level, msg, ...) blog(level, "sndio-input: %s: " msg, __func__, ##__VA_ARGS__);
  30. #define berr(level, msg, ...) \
  31. do { \
  32. const char *errstr = strerror(errno); \
  33. blog(level, msg ": %s", ##__VA_ARGS__, errstr); \
  34. } while (0)
  35. #define NSEC_PER_SEC 1000000000LL
  36. /**
  37. * Returns the name of the plugin
  38. */
  39. static const char *sndio_input_getname(void *unused)
  40. {
  41. UNUSED_PARAMETER(unused);
  42. return obs_module_text("SndioInput");
  43. }
  44. static enum speaker_layout sndio_channels_to_obs_speakers(int channels)
  45. {
  46. switch (channels) {
  47. case 1:
  48. return SPEAKERS_MONO;
  49. case 2:
  50. return SPEAKERS_STEREO;
  51. case 3:
  52. return SPEAKERS_2POINT1;
  53. case 4:
  54. return SPEAKERS_4POINT0;
  55. case 5:
  56. return SPEAKERS_4POINT1;
  57. case 6:
  58. return SPEAKERS_5POINT1;
  59. case 8:
  60. return SPEAKERS_7POINT1;
  61. }
  62. return SPEAKERS_UNKNOWN;
  63. }
  64. static enum audio_format sndio_to_obs_audio_format(const struct sio_par *par)
  65. {
  66. switch (par->bits) {
  67. case 8:
  68. return AUDIO_FORMAT_U8BIT;
  69. case 16:
  70. return AUDIO_FORMAT_16BIT;
  71. case 32:
  72. return AUDIO_FORMAT_32BIT;
  73. }
  74. return AUDIO_FORMAT_UNKNOWN;
  75. }
  76. /**
  77. * Runs sndio tasks on a pre-opened audio device.
  78. * Whenever user chooses another device, this thread gets signalled to exit,
  79. * and another one starts immediately, without waiting for the previous.
  80. */
  81. static void *sndio_thread(void *attr)
  82. {
  83. struct sndio_thr_data *thrdata = attr;
  84. size_t msgread = 0; // msg bytes read from socket
  85. size_t nsiofds = sio_nfds(thrdata->hdl);
  86. struct pollfd *pfd = bzalloc(sizeof(struct pollfd) * (1 + nsiofds));
  87. struct sio_par par;
  88. uint64_t ts;
  89. ssize_t nread;
  90. int pollres;
  91. uint8_t *buf;
  92. size_t bufsz;
  93. ts = os_gettime_ns();
  94. bufsz = thrdata->par.appbufsz * thrdata->par.bps * 2;
  95. if ((buf = bmalloc(bufsz * 2)) == NULL) {
  96. blog(LOG_ERROR, "could not allocate record buffer of %zu bytes", bufsz);
  97. goto finish;
  98. }
  99. pfd[0].fd = thrdata->sock;
  100. for (;;) {
  101. for (size_t i = 0; i <= nsiofds; i++)
  102. pfd[i].revents = 0;
  103. pfd[0].events = POLLIN;
  104. sio_pollfd(thrdata->hdl, pfd + 1, POLLIN);
  105. if (poll(pfd, 1 + nsiofds, /*INFTIM*/ -1) == -1) {
  106. if (errno == EINTR)
  107. continue;
  108. berr(LOG_ERROR, "exiting due to poll error");
  109. goto finish;
  110. }
  111. if ((pfd[0].revents & POLLHUP) == POLLHUP) {
  112. blog(LOG_INFO, "exiting upon receiving EOF at IPC socket");
  113. goto finish;
  114. }
  115. if ((pfd[0].revents & POLLIN) == POLLIN) {
  116. nread = read(pfd[0].fd, ((uint8_t *)&par) + msgread, sizeof(par) - msgread);
  117. switch (nread) {
  118. case -1:
  119. if (errno == EAGAIN)
  120. goto proceed_sio;
  121. berr(LOG_ERROR, "reading from IPC socket failed");
  122. goto finish;
  123. case 0:
  124. blog(LOG_INFO, "exiting upon receiving EOF at IPC socket");
  125. goto finish;
  126. default:
  127. msgread += (size_t)nread;
  128. if (msgread == sizeof(struct sio_par)) {
  129. size_t tbufsz;
  130. uint8_t *tbuf;
  131. msgread = 0;
  132. sio_stop(thrdata->hdl);
  133. if (!sio_setpar(thrdata->hdl, &par)) {
  134. blog(LOG_WARNING, "sio_setpar failed, keeping old params");
  135. }
  136. blog(LOG_INFO, "after sio_setpar(): appbufsz=%u bps=%u", par.appbufsz, par.bps);
  137. memcpy(&thrdata->par, &par, sizeof(struct sio_par));
  138. tbufsz = thrdata->par.appbufsz * thrdata->par.bps * 2;
  139. if ((tbuf = brealloc(buf, tbufsz)) == NULL) {
  140. blog(LOG_ERROR, "could not reallocate record buffer of %zu bytes",
  141. tbufsz);
  142. goto finish;
  143. }
  144. buf = tbuf;
  145. bufsz = tbufsz;
  146. if (!sio_start(thrdata->hdl)) {
  147. blog(LOG_ERROR, "sio_start failed, exiting");
  148. goto finish;
  149. }
  150. ts = os_gettime_ns();
  151. // Since we restarted recording,
  152. // do not try to handle events we lost.
  153. continue;
  154. }
  155. }
  156. }
  157. proceed_sio:
  158. pollres = sio_revents(thrdata->hdl, pfd + 1);
  159. if ((pollres & POLLHUP) == POLLHUP) {
  160. blog(LOG_ERROR, "sndio device error happened, exiting");
  161. goto finish;
  162. }
  163. if ((pollres & POLLIN) == POLLIN) {
  164. struct obs_source_audio out;
  165. unsigned int nframes;
  166. nread = (ssize_t)sio_read(thrdata->hdl, buf, bufsz);
  167. if (nread == 0) {
  168. if (sio_eof(thrdata->hdl)) {
  169. blog(LOG_ERROR, "sndio device EOF happened, exiting");
  170. goto finish;
  171. }
  172. continue;
  173. }
  174. nframes = (unsigned int)nread / thrdata->par.bps;
  175. //blog(LOG_INFO, "sio_read returned %u, nframes = %u", nread, nframes);
  176. memset(&out, 0, sizeof(struct obs_source_audio));
  177. out.data[0] = buf;
  178. out.frames = nframes;
  179. out.format = sndio_to_obs_audio_format(&thrdata->par);
  180. out.speakers = sndio_channels_to_obs_speakers(thrdata->par.rchan);
  181. out.samples_per_sec = thrdata->par.rate;
  182. out.timestamp = ts;
  183. ts += util_mul_div64(nframes, NSEC_PER_SEC, thrdata->par.rate);
  184. obs_source_output_audio(thrdata->source, &out);
  185. }
  186. }
  187. finish:
  188. sio_close(thrdata->hdl);
  189. close(thrdata->sock);
  190. bfree(buf);
  191. bfree(thrdata);
  192. bfree(pfd);
  193. return NULL;
  194. }
  195. /**
  196. * Destroy the plugin object and free all memory
  197. */
  198. static void sndio_destroy(void *vptr)
  199. {
  200. struct sndio_data *data = vptr;
  201. if (!data)
  202. return;
  203. close(data->sock);
  204. data->sock = -1;
  205. pthread_attr_destroy(&data->attr);
  206. bfree(data);
  207. }
  208. /**
  209. * Tries to apply the input settings.
  210. * Must be called on stopped device.
  211. */
  212. static void sndio_apply(struct sndio_data *data, obs_data_t *settings)
  213. {
  214. struct sndio_thr_data *thrdata;
  215. pthread_t thread;
  216. int ec;
  217. int socks[2] = {-1, -1};
  218. const char *devname = obs_data_get_string(settings, "device");
  219. if ((thrdata = bzalloc(sizeof(struct sndio_thr_data))) == NULL) {
  220. blog(LOG_ERROR, "malloc");
  221. return;
  222. }
  223. if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, socks) == -1) {
  224. berr(LOG_ERROR, "socketpair");
  225. goto error;
  226. }
  227. if (data->sock != -1)
  228. close(data->sock);
  229. data->sock = socks[0];
  230. thrdata->sock = socks[1];
  231. thrdata->source = data->source;
  232. thrdata->hdl = sio_open(devname, SIO_REC, 0);
  233. if (!thrdata->hdl) {
  234. berr(LOG_ERROR, "could not open %s sndio device", devname);
  235. goto error;
  236. }
  237. sio_initpar(&thrdata->par);
  238. thrdata->par.bits = obs_data_get_int(settings, "bits");
  239. thrdata->par.bps = SIO_BPS(thrdata->par.bits);
  240. thrdata->par.sig = thrdata->par.bits > 8;
  241. thrdata->par.le = 1;
  242. thrdata->par.rate = obs_data_get_int(settings, "rate");
  243. thrdata->par.rchan = obs_data_get_int(settings, "channels");
  244. thrdata->par.xrun = SIO_SYNC; // makes timestamping easy
  245. if (!sio_setpar(thrdata->hdl, &thrdata->par)) {
  246. berr(LOG_ERROR, "could not set parameters for %s sndio device", devname);
  247. goto error;
  248. }
  249. blog(LOG_INFO, "after initial sio_setpar(): appbufsz=%u bps=%u", thrdata->par.appbufsz, thrdata->par.bps);
  250. if (!sio_start(thrdata->hdl)) {
  251. berr(LOG_ERROR, "could not start recording on %s sndio device", devname);
  252. goto error;
  253. }
  254. ec = pthread_create(&thread, &data->attr, sndio_thread, thrdata);
  255. if (ec != 0) {
  256. blog(LOG_ERROR, "could not start thread");
  257. goto error;
  258. }
  259. return;
  260. error:
  261. if (thrdata->hdl != NULL)
  262. sio_close(thrdata->hdl);
  263. close(socks[0]);
  264. close(socks[1]);
  265. bfree(thrdata);
  266. }
  267. /**
  268. * Update the input settings.
  269. */
  270. static void sndio_update(void *vptr, obs_data_t *settings)
  271. {
  272. struct sndio_data *data = vptr;
  273. sndio_apply(data, settings);
  274. }
  275. /**
  276. * Create the plugin object
  277. */
  278. static void *sndio_create(obs_data_t *settings, obs_source_t *source)
  279. {
  280. struct sndio_data *data = bzalloc(sizeof(struct sndio_data));
  281. pthread_attr_init(&data->attr);
  282. pthread_attr_setdetachstate(&data->attr, PTHREAD_CREATE_DETACHED);
  283. data->source = source;
  284. data->sock = -1;
  285. sndio_apply(data, settings);
  286. return data;
  287. }
  288. /**
  289. * Get plugin defaults
  290. */
  291. static void sndio_input_defaults(obs_data_t *settings)
  292. {
  293. obs_data_set_default_string(settings, "device", SIO_DEVANY);
  294. obs_data_set_default_int(settings, "rate", 48000);
  295. obs_data_set_default_int(settings, "bits", 16);
  296. obs_data_set_default_int(settings, "channels", 2);
  297. }
  298. /**
  299. * Get plugin properties
  300. */
  301. static obs_properties_t *sndio_input_properties(void *unused)
  302. {
  303. obs_property_t *rate, *bits;
  304. UNUSED_PARAMETER(unused);
  305. obs_properties_t *props = obs_properties_create();
  306. obs_properties_add_text(props, "device", obs_module_text("Device"), OBS_TEXT_DEFAULT);
  307. rate = obs_properties_add_list(props, "rate", obs_module_text("Rate"), OBS_COMBO_TYPE_LIST,
  308. OBS_COMBO_FORMAT_INT);
  309. obs_property_list_add_int(rate, "11025 Hz", 11025);
  310. obs_property_list_add_int(rate, "22050 Hz", 22050);
  311. obs_property_list_add_int(rate, "32000 Hz", 32000);
  312. obs_property_list_add_int(rate, "44100 Hz", 44100);
  313. obs_property_list_add_int(rate, "48000 Hz", 48000);
  314. obs_property_list_add_int(rate, "96000 Hz", 96000);
  315. obs_property_list_add_int(rate, "192000 Hz", 192000);
  316. bits = obs_properties_add_list(props, "bits", obs_module_text("BitsPerSample"), OBS_COMBO_TYPE_LIST,
  317. OBS_COMBO_FORMAT_INT);
  318. obs_property_list_add_int(bits, "8", 8);
  319. obs_property_list_add_int(bits, "16", 16);
  320. obs_property_list_add_int(bits, "32", 32);
  321. obs_properties_add_int(props, "channels", obs_module_text("Channels"), 1, 8, 1);
  322. return props;
  323. }
  324. struct obs_source_info sndio_output_capture = {
  325. .id = "sndio_output_capture",
  326. .type = OBS_SOURCE_TYPE_INPUT,
  327. .output_flags = OBS_SOURCE_AUDIO,
  328. .get_name = sndio_input_getname,
  329. .create = sndio_create,
  330. .destroy = sndio_destroy,
  331. #if SHUTDOWN_ON_DEACTIVATE
  332. .activate = sndio_activate,
  333. .deactivate = sndio_deactivate,
  334. #endif
  335. .update = sndio_update,
  336. .get_defaults = sndio_input_defaults,
  337. .get_properties = sndio_input_properties,
  338. .icon_type = OBS_ICON_TYPE_AUDIO_OUTPUT,
  339. };