You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

547 lines
14 KiB

  1. /***
  2. This file is part of PulseAudio.
  3. Copyright 2004-2008 Lennart Poettering
  4. Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
  5. PulseAudio is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. PulseAudio is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with PulseAudio; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  16. USA.
  17. ***/
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <unistd.h>
  25. #include <sys/socket.h>
  26. #include <sys/un.h>
  27. #include <pulse/rtclock.h>
  28. #include <pulse/timeval.h>
  29. #include <pulse/xmalloc.h>
  30. #include <pulsecore/core-util.h>
  31. #include <pulsecore/log.h>
  32. #include <pulsecore/macro.h>
  33. #include <pulsecore/modargs.h>
  34. #include <pulsecore/module.h>
  35. #include <pulsecore/rtpoll.h>
  36. #include <pulsecore/source.h>
  37. #include <pulsecore/thread-mq.h>
  38. #include <pulsecore/thread.h>
  39. /* defined in pulse/version.h */
  40. #if PA_PROTOCOL_VERSION > 28
  41. /* these used to be defined in pulsecore/macro.h */
  42. typedef bool pa_bool_t;
  43. #define FALSE ((pa_bool_t) 0)
  44. #define TRUE (!FALSE)
  45. #else
  46. #endif
  47. #include "module-xrdp-source-symdef.h"
  48. #include <xrdp_sockets.h>
  49. PA_MODULE_AUTHOR("Laxmikant Rashinkar");
  50. PA_MODULE_DESCRIPTION("xrdp source");
  51. PA_MODULE_VERSION(PACKAGE_VERSION);
  52. PA_MODULE_LOAD_ONCE(FALSE);
  53. PA_MODULE_USAGE(
  54. "format=<sample format> "
  55. "channels=<number of channels> "
  56. "rate=<sample rate> "
  57. "source_name=<name of source> "
  58. "channel_map=<channel map> "
  59. "description=<description for the source> "
  60. "latency_time=<latency time in ms>");
  61. #define DEFAULT_SOURCE_NAME "xrdp-source"
  62. #define DEFAULT_LATENCY_TIME 10
  63. #define MAX_LATENCY_USEC 1000
  64. struct userdata {
  65. pa_core *core;
  66. pa_module *module;
  67. pa_source *source;
  68. pa_thread *thread;
  69. pa_thread_mq thread_mq;
  70. pa_rtpoll *rtpoll;
  71. size_t block_size;
  72. pa_usec_t block_usec;
  73. pa_usec_t timestamp;
  74. pa_usec_t latency_time;
  75. /* xrdp stuff */
  76. int fd; /* UDS connection to xrdp chansrv */
  77. int display_num; /* X display number */
  78. int want_src_data;
  79. };
  80. static const char* const valid_modargs[] = {
  81. "rate",
  82. "format",
  83. "channels",
  84. "source_name",
  85. "channel_map",
  86. "description",
  87. "latency_time",
  88. NULL
  89. };
  90. static int get_display_num_from_display(char *display_text) ;
  91. static int source_process_msg(pa_msgobject *o, int code, void *data,
  92. int64_t offset, pa_memchunk *chunk) {
  93. struct userdata *u = PA_SOURCE(o)->userdata;
  94. switch (code) {
  95. case PA_SOURCE_MESSAGE_SET_STATE:
  96. if (PA_PTR_TO_UINT(data) == PA_SOURCE_RUNNING)
  97. u->timestamp = pa_rtclock_now();
  98. break;
  99. case PA_SOURCE_MESSAGE_GET_LATENCY: {
  100. pa_usec_t now;
  101. now = pa_rtclock_now();
  102. *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0;
  103. return 0;
  104. }
  105. }
  106. return pa_source_process_msg(o, code, data, offset, chunk);
  107. }
  108. static void source_update_requested_latency_cb(pa_source *s) {
  109. struct userdata *u;
  110. pa_source_assert_ref(s);
  111. u = s->userdata;
  112. pa_assert(u);
  113. u->block_usec = pa_source_get_requested_latency_within_thread(s);
  114. }
  115. static int lsend(int fd, char *data, int bytes) {
  116. int sent = 0;
  117. int error;
  118. while (sent < bytes) {
  119. error = send(fd, data + sent, bytes - sent, 0);
  120. if (error < 1) {
  121. return error;
  122. }
  123. sent += error;
  124. }
  125. return sent;
  126. }
  127. static int lrecv(int fd, char *data, int bytes) {
  128. int recved = 0;
  129. int error;
  130. while (recved < bytes) {
  131. error = recv(fd, data + recved, bytes - recved, 0);
  132. if (error < 1) {
  133. return error;
  134. }
  135. recved += error;
  136. }
  137. return recved;
  138. }
  139. static int data_get(struct userdata *u, pa_memchunk *chunk) {
  140. int fd;
  141. int bytes;
  142. int read_bytes;
  143. struct sockaddr_un s;
  144. char *data;
  145. char *socket_dir;
  146. char buf[11];
  147. unsigned char ubuf[10];
  148. if (u->fd == 0) {
  149. /* connect to xrdp unix domain socket */
  150. fd = socket(PF_LOCAL, SOCK_STREAM, 0);
  151. memset(&s, 0, sizeof(s));
  152. s.sun_family = AF_UNIX;
  153. bytes = sizeof(s.sun_path) - 1;
  154. socket_dir = getenv("XRDP_SOCKET_PATH");
  155. if (socket_dir == NULL || socket_dir[0] == '\0')
  156. {
  157. socket_dir = "/tmp/.xrdp";
  158. }
  159. snprintf(s.sun_path, bytes, "%s/" CHANSRV_PORT_IN_BASE_STR,
  160. socket_dir, u->display_num);
  161. pa_log_debug("Trying to connect to %s", s.sun_path);
  162. if (connect(fd, (struct sockaddr *) &s, sizeof(struct sockaddr_un)) != 0) {
  163. pa_log_debug("Connect failed");
  164. close(fd);
  165. return -1;
  166. }
  167. pa_log("Connected ok, fd=%d", fd);
  168. pa_log_debug("###### connected to xrdp audio_in socket");
  169. u->fd = fd;
  170. }
  171. data = (char *) pa_memblock_acquire(chunk->memblock);
  172. if (!u->want_src_data) {
  173. char buf[12];
  174. buf[0] = 0;
  175. buf[1] = 0;
  176. buf[2] = 0;
  177. buf[3] = 0;
  178. buf[4] = 11;
  179. buf[5] = 0;
  180. buf[6] = 0;
  181. buf[7] = 0;
  182. buf[8] = 1;
  183. buf[9] = 0;
  184. buf[10] = 0;
  185. if (lsend(u->fd, buf, 11) != 11) {
  186. close(u->fd);
  187. u->fd = 0;
  188. pa_memblock_release(chunk->memblock);
  189. return -1;
  190. }
  191. u->want_src_data = 1;
  192. pa_log_debug("###### started recording");
  193. }
  194. /* ask for more data */
  195. buf[0] = 0;
  196. buf[1] = 0;
  197. buf[2] = 0;
  198. buf[3] = 0;
  199. buf[4] = 11;
  200. buf[5] = 0;
  201. buf[6] = 0;
  202. buf[7] = 0;
  203. buf[8] = 3;
  204. buf[9] = (unsigned char) chunk->length;
  205. buf[10] = (unsigned char) ((chunk->length >> 8) & 0xff);
  206. if (lsend(u->fd, buf, 11) != 11) {
  207. close(u->fd);
  208. u->fd = 0;
  209. pa_memblock_release(chunk->memblock);
  210. u->want_src_data = 0;
  211. return -1;
  212. }
  213. /* read length of data available */
  214. if (lrecv(u->fd, (char *) ubuf, 2) != 2) {
  215. close(u->fd);
  216. u->fd = 0;
  217. pa_memblock_release(chunk->memblock);
  218. u->want_src_data = 0;
  219. return -1;
  220. }
  221. bytes = ((ubuf[1] << 8) & 0xff00) | (ubuf[0] & 0xff);
  222. if (bytes == 0) {
  223. pa_memblock_release(chunk->memblock);
  224. return 0;
  225. }
  226. /* get data */
  227. read_bytes = lrecv(u->fd, data, bytes);
  228. if (read_bytes != bytes) {
  229. close(u->fd);
  230. u->fd = 0;
  231. pa_memblock_release(chunk->memblock);
  232. u->want_src_data = 0;
  233. return -1;
  234. }
  235. pa_memblock_release(chunk->memblock);
  236. return read_bytes;
  237. }
  238. static void thread_func(void *userdata) {
  239. struct userdata *u = userdata;
  240. int bytes;
  241. pa_assert(u);
  242. pa_thread_mq_install(&u->thread_mq);
  243. u->timestamp = pa_rtclock_now();
  244. for (;;) {
  245. int ret;
  246. /* Generate some null data */
  247. if (u->source->thread_info.state == PA_SOURCE_RUNNING) {
  248. pa_usec_t now;
  249. pa_memchunk chunk;
  250. now = pa_rtclock_now();
  251. if ((chunk.length = pa_usec_to_bytes(now - u->timestamp, &u->source->sample_spec)) > 0) {
  252. chunk.length *= 4;
  253. chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length);
  254. chunk.index = 0;
  255. bytes = data_get(u, &chunk);
  256. if (bytes > 0)
  257. {
  258. chunk.length = bytes;
  259. pa_source_post(u->source, &chunk);
  260. }
  261. pa_memblock_unref(chunk.memblock);
  262. u->timestamp = now;
  263. }
  264. pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp + u->latency_time * PA_USEC_PER_MSEC);
  265. } else {
  266. if (u->want_src_data)
  267. {
  268. /* we don't want source data anymore */
  269. char buf[12];
  270. buf[0] = 0;
  271. buf[1] = 0;
  272. buf[2] = 0;
  273. buf[3] = 0;
  274. buf[4] = 11;
  275. buf[5] = 0;
  276. buf[6] = 0;
  277. buf[7] = 0;
  278. buf[8] = 2;
  279. buf[9] = 0;
  280. buf[10] = 0;
  281. if (lsend(u->fd, buf, 11) != 11) {
  282. close(u->fd);
  283. u->fd = 0;
  284. }
  285. u->want_src_data = 0;
  286. pa_log_debug("###### stopped recording");
  287. }
  288. pa_rtpoll_set_timer_disabled(u->rtpoll);
  289. }
  290. /* Hmm, nothing to do. Let's sleep */
  291. #if defined(PA_CHECK_VERSION) && PA_CHECK_VERSION(6, 0, 0)
  292. if ((ret = pa_rtpoll_run(u->rtpoll)) < 0) {
  293. #else
  294. if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) {
  295. #endif
  296. goto fail;
  297. }
  298. if (ret == 0)
  299. goto finish;
  300. }
  301. fail:
  302. /* If this was no regular exit from the loop we have to continue
  303. * processing messages until we received PA_MESSAGE_SHUTDOWN */
  304. pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
  305. pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
  306. finish:
  307. pa_log_debug("###### thread shutting down");
  308. }
  309. int pa__init(pa_module *m) {
  310. struct userdata *u = NULL;
  311. pa_sample_spec ss;
  312. pa_channel_map map;
  313. pa_modargs *ma = NULL;
  314. pa_source_new_data data;
  315. uint32_t latency_time = DEFAULT_LATENCY_TIME;
  316. pa_assert(m);
  317. if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
  318. pa_log("Failed to parse module arguments.");
  319. goto fail;
  320. }
  321. #if 1
  322. ss = m->core->default_sample_spec;
  323. #else
  324. ss.format = PA_SAMPLE_S16LE;
  325. ss.rate = 22050;
  326. ss.channels = 2;
  327. #endif
  328. map = m->core->default_channel_map;
  329. if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
  330. pa_log("Invalid sample format specification or channel map");
  331. goto fail;
  332. }
  333. m->userdata = u = pa_xnew0(struct userdata, 1);
  334. u->core = m->core;
  335. u->module = m;
  336. u->rtpoll = pa_rtpoll_new();
  337. pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
  338. pa_source_new_data_init(&data);
  339. data.driver = __FILE__;
  340. data.module = m;
  341. pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
  342. pa_source_new_data_set_sample_spec(&data, &ss);
  343. pa_source_new_data_set_channel_map(&data, &map);
  344. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "xrdp source"));
  345. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
  346. u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY | PA_SOURCE_DYNAMIC_LATENCY);
  347. pa_source_new_data_done(&data);
  348. if (!u->source) {
  349. pa_log("Failed to create source object.");
  350. goto fail;
  351. }
  352. u->latency_time = DEFAULT_LATENCY_TIME;
  353. if (pa_modargs_get_value_u32(ma, "latency_time", &latency_time) < 0) {
  354. pa_log("Failed to parse latency_time value.");
  355. goto fail;
  356. }
  357. u->latency_time = latency_time;
  358. u->source->parent.process_msg = source_process_msg;
  359. u->source->update_requested_latency = source_update_requested_latency_cb;
  360. u->source->userdata = u;
  361. pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
  362. pa_source_set_rtpoll(u->source, u->rtpoll);
  363. pa_source_set_latency_range(u->source, 0, MAX_LATENCY_USEC);
  364. u->block_usec = u->source->thread_info.max_latency;
  365. u->source->thread_info.max_rewind =
  366. pa_usec_to_bytes(u->block_usec, &u->source->sample_spec);
  367. #if defined(PA_CHECK_VERSION)
  368. #if PA_CHECK_VERSION(0, 9, 22)
  369. if (!(u->thread = pa_thread_new("xrdp-source", thread_func, u))) {
  370. #else
  371. if (!(u->thread = pa_thread_new(thread_func, u))) {
  372. #endif
  373. #else
  374. if (!(u->thread = pa_thread_new(thread_func, u)))
  375. #endif
  376. pa_log("Failed to create thread.");
  377. goto fail;
  378. }
  379. pa_source_put(u->source);
  380. pa_modargs_free(ma);
  381. u->display_num = get_display_num_from_display(getenv("DISPLAY"));
  382. return 0;
  383. fail:
  384. if (ma)
  385. pa_modargs_free(ma);
  386. pa__done(m);
  387. return -1;
  388. }
  389. void pa__done(pa_module*m) {
  390. struct userdata *u;
  391. pa_assert(m);
  392. if (!(u = m->userdata))
  393. return;
  394. if (u->source)
  395. pa_source_unlink(u->source);
  396. if (u->thread) {
  397. pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
  398. pa_thread_free(u->thread);
  399. }
  400. pa_thread_mq_done(&u->thread_mq);
  401. if (u->source)
  402. pa_source_unref(u->source);
  403. if (u->rtpoll)
  404. pa_rtpoll_free(u->rtpoll);
  405. pa_xfree(u);
  406. }
  407. static int get_display_num_from_display(char *display_text) {
  408. int index;
  409. int mode;
  410. int host_index;
  411. int disp_index;
  412. int scre_index;
  413. int display_num;
  414. char host[256];
  415. char disp[256];
  416. char scre[256];
  417. if (display_text == NULL) {
  418. return 0;
  419. }
  420. memset(host, 0, 256);
  421. memset(disp, 0, 256);
  422. memset(scre, 0, 256);
  423. index = 0;
  424. host_index = 0;
  425. disp_index = 0;
  426. scre_index = 0;
  427. mode = 0;
  428. while (display_text[index] != 0) {
  429. if (display_text[index] == ':') {
  430. mode = 1;
  431. } else if (display_text[index] == '.') {
  432. mode = 2;
  433. } else if (mode == 0) {
  434. host[host_index] = display_text[index];
  435. host_index++;
  436. } else if (mode == 1) {
  437. disp[disp_index] = display_text[index];
  438. disp_index++;
  439. } else if (mode == 2) {
  440. scre[scre_index] = display_text[index];
  441. scre_index++;
  442. }
  443. index++;
  444. }
  445. host[host_index] = 0;
  446. disp[disp_index] = 0;
  447. scre[scre_index] = 0;
  448. display_num = atoi(disp);
  449. return display_num;
  450. }