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.

468 lines
12 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 (PA_USEC_PER_SEC * 2)
  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 data_get(struct userdata *u, pa_memchunk *chunk) {
  116. int fd;
  117. int bytes;
  118. struct sockaddr_un s;
  119. char *data;
  120. char buf[11];
  121. unsigned char ubuf[10];
  122. if (u->fd == 0) {
  123. /* connect to xrdp unix domain socket */
  124. fd = socket(PF_LOCAL, SOCK_STREAM, 0);
  125. memset(&s, 0, sizeof(s));
  126. s.sun_family = AF_UNIX;
  127. bytes = sizeof(s.sun_path) - 1;
  128. snprintf(s.sun_path, bytes, CHANSRV_PORT_STR, u->display_num);
  129. pa_log_debug("Trying to connect to %s", s.sun_path);
  130. if (connect(fd, (struct sockaddr *) &s, sizeof(struct sockaddr_un)) != 0) {
  131. pa_log_debug("Connect failed");
  132. close(fd);
  133. return -1;
  134. }
  135. pa_log("Connected ok, fd=%d", fd);
  136. pa_log_debug("###### connected to xrdp audio_in socket");
  137. u->fd = fd;
  138. }
  139. data = (char *) pa_memblock_acquire(chunk->memblock);
  140. if (!u->want_src_data) {
  141. char buf[12];
  142. buf[0] = 0;
  143. buf[1] = 0;
  144. buf[2] = 0;
  145. buf[3] = 0;
  146. buf[4] = 11;
  147. buf[5] = 0;
  148. buf[6] = 0;
  149. buf[7] = 0;
  150. buf[8] = 1;
  151. buf[9] = 0;
  152. buf[10] = 0;
  153. send(u->fd, buf, 11, 0);
  154. u->want_src_data = 1;
  155. pa_log_debug("###### started recording");
  156. }
  157. /* ask for more data */
  158. buf[0] = 0;
  159. buf[1] = 0;
  160. buf[2] = 0;
  161. buf[3] = 0;
  162. buf[4] = 11;
  163. buf[5] = 0;
  164. buf[6] = 0;
  165. buf[7] = 0;
  166. buf[8] = 3;
  167. buf[9] = (unsigned char) chunk->length;
  168. buf[10] = (unsigned char) ((chunk->length >> 8) & 0xff);
  169. send(u->fd, buf, 11, 0);
  170. /* read length of data available */
  171. recv(u->fd, ubuf, 2, 0);
  172. bytes = ((ubuf[1] << 8) & 0xff00) | (ubuf[0] & 0xff);
  173. if (bytes == 0) {
  174. pa_memblock_release(chunk->memblock);
  175. return 0;
  176. }
  177. /* get data */
  178. bytes = recv(u->fd, data, bytes, 0);
  179. pa_memblock_release(chunk->memblock);
  180. return bytes;
  181. }
  182. static void thread_func(void *userdata) {
  183. struct userdata *u = userdata;
  184. pa_assert(u);
  185. pa_thread_mq_install(&u->thread_mq);
  186. u->timestamp = pa_rtclock_now();
  187. for (;;) {
  188. int ret;
  189. /* Generate some null data */
  190. if (u->source->thread_info.state == PA_SOURCE_RUNNING) {
  191. pa_usec_t now;
  192. pa_memchunk chunk;
  193. now = pa_rtclock_now();
  194. if ((chunk.length = pa_usec_to_bytes(now - u->timestamp, &u->source->sample_spec)) > 0) {
  195. chunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1); /* or chunk.length? */
  196. chunk.index = 0;
  197. data_get(u, &chunk);
  198. pa_source_post(u->source, &chunk);
  199. pa_memblock_unref(chunk.memblock);
  200. u->timestamp = now;
  201. }
  202. pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp + u->latency_time * PA_USEC_PER_MSEC);
  203. } else {
  204. if (u->want_src_data)
  205. {
  206. /* we dont want source data anymore */
  207. char buf[12];
  208. buf[0] = 0;
  209. buf[1] = 0;
  210. buf[2] = 0;
  211. buf[3] = 0;
  212. buf[4] = 11;
  213. buf[5] = 0;
  214. buf[6] = 0;
  215. buf[7] = 0;
  216. buf[8] = 2;
  217. buf[9] = 0;
  218. buf[10] = 0;
  219. send(u->fd, buf, 11, 0);
  220. u->want_src_data = 0;
  221. pa_log_debug("###### stopped recording");
  222. }
  223. pa_rtpoll_set_timer_disabled(u->rtpoll);
  224. }
  225. /* Hmm, nothing to do. Let's sleep */
  226. if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
  227. goto fail;
  228. if (ret == 0)
  229. goto finish;
  230. }
  231. fail:
  232. /* If this was no regular exit from the loop we have to continue
  233. * processing messages until we received PA_MESSAGE_SHUTDOWN */
  234. pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
  235. pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
  236. finish:
  237. pa_log_debug("###### thread shutting down");
  238. }
  239. int pa__init(pa_module *m) {
  240. struct userdata *u = NULL;
  241. pa_sample_spec ss;
  242. pa_channel_map map;
  243. pa_modargs *ma = NULL;
  244. pa_source_new_data data;
  245. uint32_t latency_time = DEFAULT_LATENCY_TIME;
  246. pa_assert(m);
  247. if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
  248. pa_log("Failed to parse module arguments.");
  249. goto fail;
  250. }
  251. #if 0
  252. ss = m->core->default_sample_spec;
  253. #else
  254. ss.format = PA_SAMPLE_S16LE;
  255. ss.rate = 22050;
  256. ss.channels = 2;
  257. #endif
  258. map = m->core->default_channel_map;
  259. if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
  260. pa_log("Invalid sample format specification or channel map");
  261. goto fail;
  262. }
  263. m->userdata = u = pa_xnew0(struct userdata, 1);
  264. u->core = m->core;
  265. u->module = m;
  266. u->rtpoll = pa_rtpoll_new();
  267. pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
  268. pa_source_new_data_init(&data);
  269. data.driver = __FILE__;
  270. data.module = m;
  271. pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
  272. pa_source_new_data_set_sample_spec(&data, &ss);
  273. pa_source_new_data_set_channel_map(&data, &map);
  274. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "xrdp source"));
  275. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
  276. u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY | PA_SOURCE_DYNAMIC_LATENCY);
  277. pa_source_new_data_done(&data);
  278. if (!u->source) {
  279. pa_log("Failed to create source object.");
  280. goto fail;
  281. }
  282. u->latency_time = DEFAULT_LATENCY_TIME;
  283. if (pa_modargs_get_value_u32(ma, "latency_time", &latency_time) < 0) {
  284. pa_log("Failed to parse latency_time value.");
  285. goto fail;
  286. }
  287. u->latency_time = latency_time;
  288. u->source->parent.process_msg = source_process_msg;
  289. u->source->update_requested_latency = source_update_requested_latency_cb;
  290. u->source->userdata = u;
  291. pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
  292. pa_source_set_rtpoll(u->source, u->rtpoll);
  293. pa_source_set_latency_range(u->source, 0, MAX_LATENCY_USEC);
  294. u->block_usec = u->source->thread_info.max_latency;
  295. u->source->thread_info.max_rewind =
  296. pa_usec_to_bytes(u->block_usec, &u->source->sample_spec);
  297. if (!(u->thread = pa_thread_new("null-source", thread_func, u))) {
  298. pa_log("Failed to create thread.");
  299. goto fail;
  300. }
  301. pa_source_put(u->source);
  302. pa_modargs_free(ma);
  303. u->display_num = get_display_num_from_display(getenv("DISPLAY"));
  304. return 0;
  305. fail:
  306. if (ma)
  307. pa_modargs_free(ma);
  308. pa__done(m);
  309. return -1;
  310. }
  311. void pa__done(pa_module*m) {
  312. struct userdata *u;
  313. pa_assert(m);
  314. if (!(u = m->userdata))
  315. return;
  316. if (u->source)
  317. pa_source_unlink(u->source);
  318. if (u->thread) {
  319. pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
  320. pa_thread_free(u->thread);
  321. }
  322. pa_thread_mq_done(&u->thread_mq);
  323. if (u->source)
  324. pa_source_unref(u->source);
  325. if (u->rtpoll)
  326. pa_rtpoll_free(u->rtpoll);
  327. pa_xfree(u);
  328. }
  329. static int get_display_num_from_display(char *display_text) {
  330. int index;
  331. int mode;
  332. int host_index;
  333. int disp_index;
  334. int scre_index;
  335. int display_num;
  336. char host[256];
  337. char disp[256];
  338. char scre[256];
  339. if (display_text == NULL) {
  340. return 0;
  341. }
  342. memset(host, 0, 256);
  343. memset(disp, 0, 256);
  344. memset(scre, 0, 256);
  345. index = 0;
  346. host_index = 0;
  347. disp_index = 0;
  348. scre_index = 0;
  349. mode = 0;
  350. while (display_text[index] != 0) {
  351. if (display_text[index] == ':') {
  352. mode = 1;
  353. } else if (display_text[index] == '.') {
  354. mode = 2;
  355. } else if (mode == 0) {
  356. host[host_index] = display_text[index];
  357. host_index++;
  358. } else if (mode == 1) {
  359. disp[disp_index] = display_text[index];
  360. disp_index++;
  361. } else if (mode == 2) {
  362. scre[scre_index] = display_text[index];
  363. scre_index++;
  364. }
  365. index++;
  366. }
  367. host[host_index] = 0;
  368. disp[disp_index] = 0;
  369. scre[scre_index] = 0;
  370. display_num = atoi(disp);
  371. return display_num;
  372. }