pulse-input.c 10 KB

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