1
0

pulse-input.c 14 KB

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