1
0

pulse-input.c 11 KB

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