pulse-input.c 10 KB

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