pulse-input.c 12 KB

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