pulse-input.c 11 KB

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