1
0

pulse-input.c 14 KB

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