pulse-input.c 12 KB

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