pulse-input.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <[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 <util/platform.h>
  15. #include <util/bmem.h>
  16. #include <obs.h>
  17. #include "pulse-wrapper.h"
  18. #define PULSE_DATA(voidptr) struct pulse_data *data = voidptr;
  19. struct pulse_data {
  20. obs_source_t source;
  21. pa_stream *stream;
  22. /* user settings */
  23. bool ostime;
  24. char *device;
  25. /* server info */
  26. enum speaker_layout speakers;
  27. pa_sample_format_t format;
  28. uint_fast32_t samples_per_sec;
  29. uint_fast32_t bytes_per_frame;
  30. uint_fast8_t channels;
  31. /* statistics */
  32. uint_fast32_t packets;
  33. uint_fast64_t frames;
  34. };
  35. static void pulse_stop_recording(struct pulse_data *data);
  36. /**
  37. * get obs from pulse audio format
  38. */
  39. static enum audio_format pulse_to_obs_audio_format(
  40. pa_sample_format_t format)
  41. {
  42. switch (format) {
  43. case PA_SAMPLE_U8:
  44. return AUDIO_FORMAT_U8BIT;
  45. case PA_SAMPLE_S16LE:
  46. return AUDIO_FORMAT_16BIT;
  47. case PA_SAMPLE_S24_32LE:
  48. return AUDIO_FORMAT_32BIT;
  49. case PA_SAMPLE_FLOAT32LE:
  50. return AUDIO_FORMAT_FLOAT;
  51. default:
  52. return AUDIO_FORMAT_UNKNOWN;
  53. }
  54. return AUDIO_FORMAT_UNKNOWN;
  55. }
  56. /**
  57. * Get the buffer size needed for length msec with current settings
  58. */
  59. static uint_fast32_t get_buffer_size(struct pulse_data *data,
  60. uint_fast32_t length)
  61. {
  62. return (length * data->samples_per_sec * data->bytes_per_frame) / 1000;
  63. }
  64. /**
  65. * Get latency for a pulse audio stream
  66. */
  67. static int pulse_get_stream_latency(pa_stream *stream, int64_t *latency)
  68. {
  69. int ret;
  70. int sign;
  71. pa_usec_t abs;
  72. ret = pa_stream_get_latency(stream, &abs, &sign);
  73. *latency = (sign) ? -(int64_t) abs : (int64_t) abs;
  74. return ret;
  75. }
  76. /**
  77. * Callback for pulse which gets executed when new audio data is available
  78. *
  79. * @warning The function may be called even after disconnecting the stream
  80. */
  81. static void pulse_stream_read(pa_stream *p, size_t nbytes, void *userdata)
  82. {
  83. UNUSED_PARAMETER(p);
  84. UNUSED_PARAMETER(nbytes);
  85. PULSE_DATA(userdata);
  86. const void *frames;
  87. size_t bytes;
  88. uint64_t pa_time;
  89. int64_t pa_latency;
  90. if (!data->stream)
  91. goto exit;
  92. pa_stream_peek(data->stream, &frames, &bytes);
  93. // check if we got data
  94. if (!bytes)
  95. goto exit;
  96. if (!frames) {
  97. blog(LOG_ERROR, "pulse-input: Got audio hole of %u bytes",
  98. (unsigned int) bytes);
  99. pa_stream_drop(data->stream);
  100. goto exit;
  101. }
  102. if (pa_stream_get_time(data->stream, &pa_time) < 0) {
  103. blog(LOG_ERROR, "pulse-input: Failed to get timing info !");
  104. pa_stream_drop(data->stream);
  105. goto exit;
  106. }
  107. pa_time = (!data->ostime) ? pa_time * 1000 : os_gettime_ns();
  108. pulse_get_stream_latency(data->stream, &pa_latency);
  109. struct source_audio out;
  110. out.speakers = data->speakers;
  111. out.samples_per_sec = data->samples_per_sec;
  112. out.format = pulse_to_obs_audio_format(data->format);
  113. out.data[0] = (uint8_t *) frames;
  114. out.frames = bytes / data->bytes_per_frame;
  115. out.timestamp = pa_time - (pa_latency * 1000);
  116. obs_source_output_audio(data->source, &out);
  117. data->packets++;
  118. data->frames += out.frames;
  119. pa_stream_drop(data->stream);
  120. exit:
  121. pulse_signal(0);
  122. }
  123. /**
  124. * Server info callback
  125. */
  126. static void pulse_server_info(pa_context *c, const pa_server_info *i,
  127. void *userdata)
  128. {
  129. UNUSED_PARAMETER(c);
  130. PULSE_DATA(userdata);
  131. blog(LOG_INFO, "pulse-input: Server name: '%s %s'",
  132. i->server_name, i->server_version);
  133. data->format = i->sample_spec.format;
  134. data->samples_per_sec = i->sample_spec.rate;
  135. data->channels = i->sample_spec.channels;
  136. blog(LOG_INFO, "pulse-input: "
  137. "Audio format: %s, %u Hz, %u channels with %s timestamps",
  138. pa_sample_format_to_string(i->sample_spec.format),
  139. i->sample_spec.rate,
  140. i->sample_spec.channels,
  141. (data->ostime) ? "OS" : "PA");
  142. pulse_signal(0);
  143. }
  144. /**
  145. * start recording
  146. */
  147. static int_fast32_t pulse_start_recording(struct pulse_data *data)
  148. {
  149. if (pulse_get_server_info(pulse_server_info, (void *) data) < 0) {
  150. blog(LOG_ERROR, "pulse-input: Unable to get server info !");
  151. return -1;
  152. }
  153. pa_sample_spec spec;
  154. spec.format = data->format;
  155. spec.rate = data->samples_per_sec;
  156. spec.channels = data->channels;
  157. if (!pa_sample_spec_valid(&spec)) {
  158. blog(LOG_ERROR, "pulse-input: Sample spec is not valid");
  159. return -1;
  160. }
  161. data->bytes_per_frame = pa_frame_size(&spec);
  162. data->stream = pulse_stream_new(obs_source_getname(data->source),
  163. &spec, NULL);
  164. if (!data->stream) {
  165. blog(LOG_ERROR, "pulse-input: Unable to create stream");
  166. return -1;
  167. }
  168. pulse_lock();
  169. pa_stream_set_read_callback(data->stream, pulse_stream_read,
  170. (void *) data);
  171. pulse_unlock();
  172. pa_buffer_attr attr;
  173. attr.fragsize = get_buffer_size(data, 250);
  174. attr.maxlength = (uint32_t) -1;
  175. attr.minreq = (uint32_t) -1;
  176. attr.prebuf = (uint32_t) -1;
  177. attr.tlength = (uint32_t) -1;
  178. pa_stream_flags_t flags =
  179. PA_STREAM_INTERPOLATE_TIMING
  180. | PA_STREAM_AUTO_TIMING_UPDATE
  181. | PA_STREAM_ADJUST_LATENCY;
  182. pulse_lock();
  183. int_fast32_t ret = pa_stream_connect_record(data->stream, data->device,
  184. &attr, flags);
  185. pulse_unlock();
  186. if (ret < 0) {
  187. pulse_stop_recording(data);
  188. blog(LOG_ERROR, "pulse-input: Unable to connect to stream");
  189. return -1;
  190. }
  191. blog(LOG_INFO, "pulse-input: Started recording from '%s'",
  192. data->device);
  193. return 0;
  194. }
  195. /**
  196. * stop recording
  197. */
  198. static void pulse_stop_recording(struct pulse_data *data)
  199. {
  200. if (data->stream) {
  201. pulse_lock();
  202. pa_stream_disconnect(data->stream);
  203. pa_stream_unref(data->stream);
  204. data->stream = NULL;
  205. pulse_unlock();
  206. }
  207. blog(LOG_INFO, "pulse-input: Stopped recording from '%s'",
  208. data->device);
  209. blog(LOG_INFO, "pulse-input: Got %"PRIuFAST32
  210. " packets with %"PRIuFAST64" frames",
  211. data->packets, data->frames);
  212. }
  213. /**
  214. * input info callback
  215. */
  216. static void pulse_input_info(pa_context *c, const pa_source_info *i, int eol,
  217. void *userdata)
  218. {
  219. UNUSED_PARAMETER(c);
  220. if (eol != 0 || i->monitor_of_sink != PA_INVALID_INDEX)
  221. goto skip;
  222. obs_property_list_add_string((obs_property_t) userdata,
  223. i->description, i->name);
  224. skip:
  225. pulse_signal(0);
  226. }
  227. /**
  228. * output info callback
  229. */
  230. static void pulse_output_info(pa_context *c, const pa_source_info *i, int eol,
  231. void *userdata)
  232. {
  233. UNUSED_PARAMETER(c);
  234. if (eol != 0 || i->monitor_of_sink == PA_INVALID_INDEX)
  235. goto skip;
  236. obs_property_list_add_string((obs_property_t) userdata,
  237. i->description, i->name);
  238. skip:
  239. pulse_signal(0);
  240. }
  241. /**
  242. * Get plugin properties
  243. */
  244. static obs_properties_t pulse_properties(const char *locale, bool input)
  245. {
  246. obs_properties_t props = obs_properties_create(locale);
  247. obs_property_t devices = obs_properties_add_list(props, "device_id",
  248. "Device", OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  249. pulse_init();
  250. pa_source_info_cb_t cb = (input) ? pulse_input_info : pulse_output_info;
  251. pulse_get_source_info_list(cb, (void *) devices);
  252. pulse_unref();
  253. obs_properties_add_bool(props, "ostime", "Use OS timestamps");
  254. return props;
  255. }
  256. static obs_properties_t pulse_input_properties(const char *locale)
  257. {
  258. return pulse_properties(locale, true);
  259. }
  260. static obs_properties_t pulse_output_properties(const char *locale)
  261. {
  262. return pulse_properties(locale, false);
  263. }
  264. /**
  265. * Server info callback
  266. */
  267. static void pulse_input_device(pa_context *c, const pa_server_info *i,
  268. void *userdata)
  269. {
  270. UNUSED_PARAMETER(c);
  271. obs_data_t settings = (obs_data_t) userdata;
  272. obs_data_set_default_string(settings, "device_id",
  273. i->default_source_name);
  274. blog(LOG_DEBUG, "pulse-input: Default input device: '%s'",
  275. i->default_source_name);
  276. pulse_signal(0);
  277. }
  278. static void pulse_output_device(pa_context *c, const pa_server_info *i,
  279. void *userdata)
  280. {
  281. UNUSED_PARAMETER(c);
  282. obs_data_t settings = (obs_data_t) userdata;
  283. char *monitor = bzalloc(strlen(i->default_sink_name) + 9);
  284. strcat(monitor, i->default_sink_name);
  285. strcat(monitor, ".monitor");
  286. obs_data_set_default_string(settings, "device_id", monitor);
  287. blog(LOG_DEBUG, "pulse-input: Default output device: '%s'", monitor);
  288. bfree(monitor);
  289. pulse_signal(0);
  290. }
  291. /**
  292. * Get plugin defaults
  293. */
  294. static void pulse_defaults(obs_data_t settings, bool input)
  295. {
  296. pulse_init();
  297. pa_server_info_cb_t cb = (input)
  298. ? pulse_input_device : pulse_output_device;
  299. pulse_get_server_info(cb, (void *) settings);
  300. pulse_unref();
  301. obs_data_set_default_bool(settings, "ostime", false);
  302. }
  303. static void pulse_input_defaults(obs_data_t settings)
  304. {
  305. return pulse_defaults(settings, true);
  306. }
  307. static void pulse_output_defaults(obs_data_t settings)
  308. {
  309. return pulse_defaults(settings, false);
  310. }
  311. /**
  312. * Returns the name of the plugin
  313. */
  314. static const char *pulse_input_getname(const char *locale)
  315. {
  316. UNUSED_PARAMETER(locale);
  317. return "Pulse Audio Input Capture";
  318. }
  319. static const char *pulse_output_getname(const char *locale)
  320. {
  321. UNUSED_PARAMETER(locale);
  322. return "Pulse Audio Output Capture";
  323. }
  324. /**
  325. * Destroy the plugin object and free all memory
  326. */
  327. static void pulse_destroy(void *vptr)
  328. {
  329. PULSE_DATA(vptr);
  330. if (!data)
  331. return;
  332. if (data->stream)
  333. pulse_stop_recording(data);
  334. pulse_unref();
  335. if (data->device)
  336. bfree(data->device);
  337. bfree(data);
  338. }
  339. /**
  340. * Update the input settings
  341. */
  342. static void pulse_update(void *vptr, obs_data_t settings)
  343. {
  344. PULSE_DATA(vptr);
  345. bool restart = false;
  346. const char *new_device;
  347. new_device = obs_data_getstring(settings, "device_id");
  348. if (!data->device || strcmp(data->device, new_device) != 0) {
  349. if (data->device)
  350. bfree(data->device);
  351. data->device = bstrdup(new_device);
  352. restart = true;
  353. }
  354. if (data->ostime != obs_data_getbool(settings, "ostime")) {
  355. data->ostime = obs_data_getbool(settings, "ostime");
  356. restart = true;
  357. }
  358. if (!restart)
  359. return;
  360. if (data->stream)
  361. pulse_stop_recording(data);
  362. pulse_start_recording(data);
  363. }
  364. /**
  365. * Create the plugin object
  366. */
  367. static void *pulse_create(obs_data_t settings, obs_source_t source)
  368. {
  369. struct pulse_data *data = bzalloc(sizeof(struct pulse_data));
  370. data->source = source;
  371. data->speakers = SPEAKERS_STEREO;
  372. pulse_init();
  373. pulse_update(data, settings);
  374. if (data->stream)
  375. return data;
  376. pulse_destroy(data);
  377. return NULL;
  378. }
  379. struct obs_source_info pulse_input_capture = {
  380. .id = "pulse_input_capture",
  381. .type = OBS_SOURCE_TYPE_INPUT,
  382. .output_flags = OBS_SOURCE_AUDIO,
  383. .getname = pulse_input_getname,
  384. .create = pulse_create,
  385. .destroy = pulse_destroy,
  386. .update = pulse_update,
  387. .defaults = pulse_input_defaults,
  388. .properties = pulse_input_properties
  389. };
  390. struct obs_source_info pulse_output_capture = {
  391. .id = "pulse_output_capture",
  392. .type = OBS_SOURCE_TYPE_INPUT,
  393. .output_flags = OBS_SOURCE_AUDIO,
  394. .getname = pulse_output_getname,
  395. .create = pulse_create,
  396. .destroy = pulse_destroy,
  397. .update = pulse_update,
  398. .defaults = pulse_output_defaults,
  399. .properties = pulse_output_properties
  400. };