pulse-input.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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-module.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 obs_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_get_name(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(bool input)
  236. {
  237. obs_properties_t props = obs_properties_create();
  238. obs_property_t devices = obs_properties_add_list(props, "device_id",
  239. obs_module_text("Device"), OBS_COMBO_TYPE_LIST,
  240. OBS_COMBO_FORMAT_STRING);
  241. pulse_init();
  242. pa_source_info_cb_t cb = (input) ? pulse_input_info : pulse_output_info;
  243. pulse_get_source_info_list(cb, (void *) devices);
  244. pulse_unref();
  245. return props;
  246. }
  247. static obs_properties_t pulse_input_properties(void)
  248. {
  249. return pulse_properties(true);
  250. }
  251. static obs_properties_t pulse_output_properties(void)
  252. {
  253. return pulse_properties(false);
  254. }
  255. /**
  256. * Server info callback
  257. */
  258. static void pulse_input_device(pa_context *c, const pa_server_info *i,
  259. void *userdata)
  260. {
  261. UNUSED_PARAMETER(c);
  262. obs_data_t settings = (obs_data_t) userdata;
  263. obs_data_set_default_string(settings, "device_id",
  264. i->default_source_name);
  265. blog(LOG_DEBUG, "pulse-input: Default input device: '%s'",
  266. i->default_source_name);
  267. pulse_signal(0);
  268. }
  269. static void pulse_output_device(pa_context *c, const pa_server_info *i,
  270. void *userdata)
  271. {
  272. UNUSED_PARAMETER(c);
  273. obs_data_t settings = (obs_data_t) userdata;
  274. char *monitor = bzalloc(strlen(i->default_sink_name) + 9);
  275. strcat(monitor, i->default_sink_name);
  276. strcat(monitor, ".monitor");
  277. obs_data_set_default_string(settings, "device_id", monitor);
  278. blog(LOG_DEBUG, "pulse-input: Default output device: '%s'", monitor);
  279. bfree(monitor);
  280. pulse_signal(0);
  281. }
  282. /**
  283. * Get plugin defaults
  284. */
  285. static void pulse_defaults(obs_data_t settings, bool input)
  286. {
  287. pulse_init();
  288. pa_server_info_cb_t cb = (input)
  289. ? pulse_input_device : pulse_output_device;
  290. pulse_get_server_info(cb, (void *) settings);
  291. pulse_unref();
  292. }
  293. static void pulse_input_defaults(obs_data_t settings)
  294. {
  295. return pulse_defaults(settings, true);
  296. }
  297. static void pulse_output_defaults(obs_data_t settings)
  298. {
  299. return pulse_defaults(settings, false);
  300. }
  301. /**
  302. * Returns the name of the plugin
  303. */
  304. static const char *pulse_input_getname(void)
  305. {
  306. return obs_module_text("PulseInput");
  307. }
  308. static const char *pulse_output_getname(void)
  309. {
  310. return obs_module_text("PulseOutput");
  311. }
  312. /**
  313. * Destroy the plugin object and free all memory
  314. */
  315. static void pulse_destroy(void *vptr)
  316. {
  317. PULSE_DATA(vptr);
  318. if (!data)
  319. return;
  320. if (data->stream)
  321. pulse_stop_recording(data);
  322. pulse_unref();
  323. if (data->device)
  324. bfree(data->device);
  325. bfree(data);
  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. const char *new_device;
  335. new_device = 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 = bstrdup(new_device);
  340. restart = true;
  341. }
  342. if (!restart)
  343. return;
  344. if (data->stream)
  345. pulse_stop_recording(data);
  346. pulse_start_recording(data);
  347. }
  348. /**
  349. * Create the plugin object
  350. */
  351. static void *pulse_create(obs_data_t settings, obs_source_t source)
  352. {
  353. struct pulse_data *data = bzalloc(sizeof(struct pulse_data));
  354. data->source = source;
  355. data->speakers = SPEAKERS_STEREO;
  356. pulse_init();
  357. pulse_update(data, settings);
  358. if (data->stream)
  359. return data;
  360. pulse_destroy(data);
  361. return NULL;
  362. }
  363. struct obs_source_info pulse_input_capture = {
  364. .id = "pulse_input_capture",
  365. .type = OBS_SOURCE_TYPE_INPUT,
  366. .output_flags = OBS_SOURCE_AUDIO,
  367. .get_name = pulse_input_getname,
  368. .create = pulse_create,
  369. .destroy = pulse_destroy,
  370. .update = pulse_update,
  371. .defaults = pulse_input_defaults,
  372. .properties = pulse_input_properties
  373. };
  374. struct obs_source_info pulse_output_capture = {
  375. .id = "pulse_output_capture",
  376. .type = OBS_SOURCE_TYPE_INPUT,
  377. .output_flags = OBS_SOURCE_AUDIO,
  378. .get_name = pulse_output_getname,
  379. .create = pulse_create,
  380. .destroy = pulse_destroy,
  381. .update = pulse_update,
  382. .defaults = pulse_output_defaults,
  383. .properties = pulse_output_properties
  384. };