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.

535 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. PA_MODULE_AUTHOR("Laxmikant Rashinkar");
  49. PA_MODULE_DESCRIPTION("xrdp source");
  50. PA_MODULE_VERSION(PACKAGE_VERSION);
  51. PA_MODULE_LOAD_ONCE(FALSE);
  52. PA_MODULE_USAGE(
  53. "format=<sample format> "
  54. "channels=<number of channels> "
  55. "rate=<sample rate> "
  56. "source_name=<name of source> "
  57. "channel_map=<channel map> "
  58. "description=<description for the source> "
  59. "latency_time=<latency time in ms>");
  60. #define DEFAULT_SOURCE_NAME "xrdp-source"
  61. #define DEFAULT_LATENCY_TIME 10
  62. #define MAX_LATENCY_USEC 1000
  63. #define CHANSRV_PORT_STR "/tmp/.xrdp/xrdp_chansrv_audio_in_socket_%d"
  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 buf[11];
  146. unsigned char ubuf[10];
  147. if (u->fd == 0) {
  148. /* connect to xrdp unix domain socket */
  149. fd = socket(PF_LOCAL, SOCK_STREAM, 0);
  150. memset(&s, 0, sizeof(s));
  151. s.sun_family = AF_UNIX;
  152. bytes = sizeof(s.sun_path) - 1;
  153. snprintf(s.sun_path, bytes, CHANSRV_PORT_STR, u->display_num);
  154. pa_log_debug("Trying to connect to %s", s.sun_path);
  155. if (connect(fd, (struct sockaddr *) &s, sizeof(struct sockaddr_un)) != 0) {
  156. pa_log_debug("Connect failed");
  157. close(fd);
  158. return -1;
  159. }
  160. pa_log("Connected ok, fd=%d", fd);
  161. pa_log_debug("###### connected to xrdp audio_in socket");
  162. u->fd = fd;
  163. }
  164. data = (char *) pa_memblock_acquire(chunk->memblock);
  165. if (!u->want_src_data) {
  166. char buf[12];
  167. buf[0] = 0;
  168. buf[1] = 0;
  169. buf[2] = 0;
  170. buf[3] = 0;
  171. buf[4] = 11;
  172. buf[5] = 0;
  173. buf[6] = 0;
  174. buf[7] = 0;
  175. buf[8] = 1;
  176. buf[9] = 0;
  177. buf[10] = 0;
  178. if (lsend(u->fd, buf, 11) != 11) {
  179. close(u->fd);
  180. u->fd = 0;
  181. pa_memblock_release(chunk->memblock);
  182. return -1;
  183. }
  184. u->want_src_data = 1;
  185. pa_log_debug("###### started recording");
  186. }
  187. /* ask for more data */
  188. buf[0] = 0;
  189. buf[1] = 0;
  190. buf[2] = 0;
  191. buf[3] = 0;
  192. buf[4] = 11;
  193. buf[5] = 0;
  194. buf[6] = 0;
  195. buf[7] = 0;
  196. buf[8] = 3;
  197. buf[9] = (unsigned char) chunk->length;
  198. buf[10] = (unsigned char) ((chunk->length >> 8) & 0xff);
  199. if (lsend(u->fd, buf, 11) != 11) {
  200. close(u->fd);
  201. u->fd = 0;
  202. pa_memblock_release(chunk->memblock);
  203. u->want_src_data = 0;
  204. return -1;
  205. }
  206. /* read length of data available */
  207. if (lrecv(u->fd, (char *) ubuf, 2) != 2) {
  208. close(u->fd);
  209. u->fd = 0;
  210. pa_memblock_release(chunk->memblock);
  211. u->want_src_data = 0;
  212. return -1;
  213. }
  214. bytes = ((ubuf[1] << 8) & 0xff00) | (ubuf[0] & 0xff);
  215. if (bytes == 0) {
  216. pa_memblock_release(chunk->memblock);
  217. return 0;
  218. }
  219. /* get data */
  220. read_bytes = lrecv(u->fd, data, bytes);
  221. if (read_bytes != bytes) {
  222. close(u->fd);
  223. u->fd = 0;
  224. pa_memblock_release(chunk->memblock);
  225. u->want_src_data = 0;
  226. return -1;
  227. }
  228. pa_memblock_release(chunk->memblock);
  229. return read_bytes;
  230. }
  231. static void thread_func(void *userdata) {
  232. struct userdata *u = userdata;
  233. int bytes;
  234. pa_assert(u);
  235. pa_thread_mq_install(&u->thread_mq);
  236. u->timestamp = pa_rtclock_now();
  237. for (;;) {
  238. int ret;
  239. /* Generate some null data */
  240. if (u->source->thread_info.state == PA_SOURCE_RUNNING) {
  241. pa_usec_t now;
  242. pa_memchunk chunk;
  243. now = pa_rtclock_now();
  244. if ((chunk.length = pa_usec_to_bytes(now - u->timestamp, &u->source->sample_spec)) > 0) {
  245. chunk.length *= 4;
  246. chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length);
  247. chunk.index = 0;
  248. bytes = data_get(u, &chunk);
  249. if (bytes > 0)
  250. {
  251. chunk.length = bytes;
  252. pa_source_post(u->source, &chunk);
  253. }
  254. pa_memblock_unref(chunk.memblock);
  255. u->timestamp = now;
  256. }
  257. pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp + u->latency_time * PA_USEC_PER_MSEC);
  258. } else {
  259. if (u->want_src_data)
  260. {
  261. /* we don't want source data anymore */
  262. char buf[12];
  263. buf[0] = 0;
  264. buf[1] = 0;
  265. buf[2] = 0;
  266. buf[3] = 0;
  267. buf[4] = 11;
  268. buf[5] = 0;
  269. buf[6] = 0;
  270. buf[7] = 0;
  271. buf[8] = 2;
  272. buf[9] = 0;
  273. buf[10] = 0;
  274. if (lsend(u->fd, buf, 11) != 11) {
  275. close(u->fd);
  276. u->fd = 0;
  277. }
  278. u->want_src_data = 0;
  279. pa_log_debug("###### stopped recording");
  280. }
  281. pa_rtpoll_set_timer_disabled(u->rtpoll);
  282. }
  283. /* Hmm, nothing to do. Let's sleep */
  284. if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
  285. goto fail;
  286. if (ret == 0)
  287. goto finish;
  288. }
  289. fail:
  290. /* If this was no regular exit from the loop we have to continue
  291. * processing messages until we received PA_MESSAGE_SHUTDOWN */
  292. pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
  293. pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
  294. finish:
  295. pa_log_debug("###### thread shutting down");
  296. }
  297. int pa__init(pa_module *m) {
  298. struct userdata *u = NULL;
  299. pa_sample_spec ss;
  300. pa_channel_map map;
  301. pa_modargs *ma = NULL;
  302. pa_source_new_data data;
  303. uint32_t latency_time = DEFAULT_LATENCY_TIME;
  304. pa_assert(m);
  305. if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
  306. pa_log("Failed to parse module arguments.");
  307. goto fail;
  308. }
  309. #if 1
  310. ss = m->core->default_sample_spec;
  311. #else
  312. ss.format = PA_SAMPLE_S16LE;
  313. ss.rate = 22050;
  314. ss.channels = 2;
  315. #endif
  316. map = m->core->default_channel_map;
  317. if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
  318. pa_log("Invalid sample format specification or channel map");
  319. goto fail;
  320. }
  321. m->userdata = u = pa_xnew0(struct userdata, 1);
  322. u->core = m->core;
  323. u->module = m;
  324. u->rtpoll = pa_rtpoll_new();
  325. pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
  326. pa_source_new_data_init(&data);
  327. data.driver = __FILE__;
  328. data.module = m;
  329. pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
  330. pa_source_new_data_set_sample_spec(&data, &ss);
  331. pa_source_new_data_set_channel_map(&data, &map);
  332. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "xrdp source"));
  333. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
  334. u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY | PA_SOURCE_DYNAMIC_LATENCY);
  335. pa_source_new_data_done(&data);
  336. if (!u->source) {
  337. pa_log("Failed to create source object.");
  338. goto fail;
  339. }
  340. u->latency_time = DEFAULT_LATENCY_TIME;
  341. if (pa_modargs_get_value_u32(ma, "latency_time", &latency_time) < 0) {
  342. pa_log("Failed to parse latency_time value.");
  343. goto fail;
  344. }
  345. u->latency_time = latency_time;
  346. u->source->parent.process_msg = source_process_msg;
  347. u->source->update_requested_latency = source_update_requested_latency_cb;
  348. u->source->userdata = u;
  349. pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
  350. pa_source_set_rtpoll(u->source, u->rtpoll);
  351. pa_source_set_latency_range(u->source, 0, MAX_LATENCY_USEC);
  352. u->block_usec = u->source->thread_info.max_latency;
  353. u->source->thread_info.max_rewind =
  354. pa_usec_to_bytes(u->block_usec, &u->source->sample_spec);
  355. #if defined(PA_CHECK_VERSION)
  356. #if PA_CHECK_VERSION(0, 9, 22)
  357. if (!(u->thread = pa_thread_new("xrdp-source", thread_func, u))) {
  358. #else
  359. if (!(u->thread = pa_thread_new(thread_func, u))) {
  360. #endif
  361. #else
  362. if (!(u->thread = pa_thread_new(thread_func, u)))
  363. #endif
  364. pa_log("Failed to create thread.");
  365. goto fail;
  366. }
  367. pa_source_put(u->source);
  368. pa_modargs_free(ma);
  369. u->display_num = get_display_num_from_display(getenv("DISPLAY"));
  370. return 0;
  371. fail:
  372. if (ma)
  373. pa_modargs_free(ma);
  374. pa__done(m);
  375. return -1;
  376. }
  377. void pa__done(pa_module*m) {
  378. struct userdata *u;
  379. pa_assert(m);
  380. if (!(u = m->userdata))
  381. return;
  382. if (u->source)
  383. pa_source_unlink(u->source);
  384. if (u->thread) {
  385. pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
  386. pa_thread_free(u->thread);
  387. }
  388. pa_thread_mq_done(&u->thread_mq);
  389. if (u->source)
  390. pa_source_unref(u->source);
  391. if (u->rtpoll)
  392. pa_rtpoll_free(u->rtpoll);
  393. pa_xfree(u);
  394. }
  395. static int get_display_num_from_display(char *display_text) {
  396. int index;
  397. int mode;
  398. int host_index;
  399. int disp_index;
  400. int scre_index;
  401. int display_num;
  402. char host[256];
  403. char disp[256];
  404. char scre[256];
  405. if (display_text == NULL) {
  406. return 0;
  407. }
  408. memset(host, 0, 256);
  409. memset(disp, 0, 256);
  410. memset(scre, 0, 256);
  411. index = 0;
  412. host_index = 0;
  413. disp_index = 0;
  414. scre_index = 0;
  415. mode = 0;
  416. while (display_text[index] != 0) {
  417. if (display_text[index] == ':') {
  418. mode = 1;
  419. } else if (display_text[index] == '.') {
  420. mode = 2;
  421. } else if (mode == 0) {
  422. host[host_index] = display_text[index];
  423. host_index++;
  424. } else if (mode == 1) {
  425. disp[disp_index] = display_text[index];
  426. disp_index++;
  427. } else if (mode == 2) {
  428. scre[scre_index] = display_text[index];
  429. scre_index++;
  430. }
  431. index++;
  432. }
  433. host[host_index] = 0;
  434. disp[disp_index] = 0;
  435. scre[scre_index] = 0;
  436. display_num = atoi(disp);
  437. return display_num;
  438. }