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.

460 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. #include "module-xrdp-source-symdef.h"
  40. PA_MODULE_AUTHOR("Laxmikant Rashinkar");
  41. PA_MODULE_DESCRIPTION("xrdp source");
  42. PA_MODULE_VERSION(PACKAGE_VERSION);
  43. PA_MODULE_LOAD_ONCE(FALSE);
  44. PA_MODULE_USAGE(
  45. "format=<sample format> "
  46. "channels=<number of channels> "
  47. "rate=<sample rate> "
  48. "source_name=<name of source> "
  49. "channel_map=<channel map> "
  50. "description=<description for the source> "
  51. "latency_time=<latency time in ms>");
  52. #define DEFAULT_SOURCE_NAME "xrdp"
  53. #define DEFAULT_LATENCY_TIME 10
  54. #define MAX_LATENCY_USEC (PA_USEC_PER_SEC * 2)
  55. #define CHANSRV_PORT_STR "/tmp/.xrdp/xrdp_chansrv_audio_in_socket_%d"
  56. struct userdata {
  57. pa_core *core;
  58. pa_module *module;
  59. pa_source *source;
  60. pa_thread *thread;
  61. pa_thread_mq thread_mq;
  62. pa_rtpoll *rtpoll;
  63. size_t block_size;
  64. pa_usec_t block_usec;
  65. pa_usec_t timestamp;
  66. pa_usec_t latency_time;
  67. /* xrdp stuff */
  68. int fd; /* UDS connection to xrdp chansrv */
  69. int display_num; /* X display number */
  70. int want_src_data;
  71. };
  72. static const char* const valid_modargs[] = {
  73. "rate",
  74. "format",
  75. "channels",
  76. "source_name",
  77. "channel_map",
  78. "description",
  79. "latency_time",
  80. NULL
  81. };
  82. static int get_display_num_from_display(char *display_text) ;
  83. static int source_process_msg(pa_msgobject *o, int code, void *data,
  84. int64_t offset, pa_memchunk *chunk) {
  85. struct userdata *u = PA_SOURCE(o)->userdata;
  86. switch (code) {
  87. case PA_SOURCE_MESSAGE_SET_STATE:
  88. if (PA_PTR_TO_UINT(data) == PA_SOURCE_RUNNING)
  89. u->timestamp = pa_rtclock_now();
  90. break;
  91. case PA_SOURCE_MESSAGE_GET_LATENCY: {
  92. pa_usec_t now;
  93. now = pa_rtclock_now();
  94. *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0;
  95. return 0;
  96. }
  97. }
  98. return pa_source_process_msg(o, code, data, offset, chunk);
  99. }
  100. static void source_update_requested_latency_cb(pa_source *s) {
  101. struct userdata *u;
  102. pa_source_assert_ref(s);
  103. u = s->userdata;
  104. pa_assert(u);
  105. u->block_usec = pa_source_get_requested_latency_within_thread(s);
  106. }
  107. static int data_get(struct userdata *u, pa_memchunk *chunk) {
  108. int fd;
  109. int bytes;
  110. struct sockaddr_un s;
  111. char *data;
  112. char buf[11];
  113. unsigned char ubuf[10];
  114. if (u->fd == 0) {
  115. /* connect to xrdp unix domain socket */
  116. fd = socket(PF_LOCAL, SOCK_STREAM, 0);
  117. memset(&s, 0, sizeof(s));
  118. s.sun_family = AF_UNIX;
  119. bytes = sizeof(s.sun_path) - 1;
  120. snprintf(s.sun_path, bytes, CHANSRV_PORT_STR, u->display_num);
  121. pa_log_debug("Trying to connect to %s", s.sun_path);
  122. if (connect(fd, (struct sockaddr *) &s, sizeof(struct sockaddr_un)) != 0) {
  123. pa_log_debug("Connect failed");
  124. close(fd);
  125. return -1;
  126. }
  127. pa_log("Connected ok, fd=%d", fd);
  128. pa_log_debug("###### connected to xrdp audio_in socket");
  129. u->fd = fd;
  130. }
  131. data = (char *) pa_memblock_acquire(chunk->memblock);
  132. if (!u->want_src_data) {
  133. char buf[12];
  134. buf[0] = 0;
  135. buf[1] = 0;
  136. buf[2] = 0;
  137. buf[3] = 0;
  138. buf[4] = 11;
  139. buf[5] = 0;
  140. buf[6] = 0;
  141. buf[7] = 0;
  142. buf[8] = 1;
  143. buf[9] = 0;
  144. buf[10] = 0;
  145. send(u->fd, buf, 11, 0);
  146. u->want_src_data = 1;
  147. pa_log_debug("###### started recording");
  148. }
  149. /* ask for more data */
  150. buf[0] = 0;
  151. buf[1] = 0;
  152. buf[2] = 0;
  153. buf[3] = 0;
  154. buf[4] = 11;
  155. buf[5] = 0;
  156. buf[6] = 0;
  157. buf[7] = 0;
  158. buf[8] = 3;
  159. buf[9] = (unsigned char) chunk->length;
  160. buf[10] = (unsigned char) ((chunk->length >> 8) & 0xff);
  161. send(u->fd, buf, 11, 0);
  162. /* read length of data available */
  163. recv(u->fd, ubuf, 2, 0);
  164. bytes = ((ubuf[1] << 8) & 0xff00) | (ubuf[0] & 0xff);
  165. if (bytes == 0) {
  166. pa_memblock_release(chunk->memblock);
  167. return 0;
  168. }
  169. /* get data */
  170. bytes = recv(u->fd, data, bytes, 0);
  171. pa_memblock_release(chunk->memblock);
  172. return bytes;
  173. }
  174. static void thread_func(void *userdata) {
  175. struct userdata *u = userdata;
  176. pa_assert(u);
  177. pa_thread_mq_install(&u->thread_mq);
  178. u->timestamp = pa_rtclock_now();
  179. for (;;) {
  180. int ret;
  181. /* Generate some null data */
  182. if (u->source->thread_info.state == PA_SOURCE_RUNNING) {
  183. pa_usec_t now;
  184. pa_memchunk chunk;
  185. now = pa_rtclock_now();
  186. if ((chunk.length = pa_usec_to_bytes(now - u->timestamp, &u->source->sample_spec)) > 0) {
  187. chunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1); /* or chunk.length? */
  188. chunk.index = 0;
  189. data_get(u, &chunk);
  190. pa_source_post(u->source, &chunk);
  191. pa_memblock_unref(chunk.memblock);
  192. u->timestamp = now;
  193. }
  194. pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp + u->latency_time * PA_USEC_PER_MSEC);
  195. } else {
  196. if (u->want_src_data)
  197. {
  198. /* we dont want source data anymore */
  199. char buf[12];
  200. buf[0] = 0;
  201. buf[1] = 0;
  202. buf[2] = 0;
  203. buf[3] = 0;
  204. buf[4] = 11;
  205. buf[5] = 0;
  206. buf[6] = 0;
  207. buf[7] = 0;
  208. buf[8] = 2;
  209. buf[9] = 0;
  210. buf[10] = 0;
  211. send(u->fd, buf, 11, 0);
  212. u->want_src_data = 0;
  213. pa_log_debug("###### stopped recording");
  214. }
  215. pa_rtpoll_set_timer_disabled(u->rtpoll);
  216. }
  217. /* Hmm, nothing to do. Let's sleep */
  218. if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
  219. goto fail;
  220. if (ret == 0)
  221. goto finish;
  222. }
  223. fail:
  224. /* If this was no regular exit from the loop we have to continue
  225. * processing messages until we received PA_MESSAGE_SHUTDOWN */
  226. pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
  227. pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
  228. finish:
  229. pa_log_debug("###### thread shutting down");
  230. }
  231. int pa__init(pa_module *m) {
  232. struct userdata *u = NULL;
  233. pa_sample_spec ss;
  234. pa_channel_map map;
  235. pa_modargs *ma = NULL;
  236. pa_source_new_data data;
  237. uint32_t latency_time = DEFAULT_LATENCY_TIME;
  238. pa_assert(m);
  239. if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
  240. pa_log("Failed to parse module arguments.");
  241. goto fail;
  242. }
  243. #if 0
  244. ss = m->core->default_sample_spec;
  245. #else
  246. ss.format = PA_SAMPLE_S16LE;
  247. ss.rate = 22050;
  248. ss.channels = 2;
  249. #endif
  250. map = m->core->default_channel_map;
  251. if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
  252. pa_log("Invalid sample format specification or channel map");
  253. goto fail;
  254. }
  255. m->userdata = u = pa_xnew0(struct userdata, 1);
  256. u->core = m->core;
  257. u->module = m;
  258. u->rtpoll = pa_rtpoll_new();
  259. pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
  260. pa_source_new_data_init(&data);
  261. data.driver = __FILE__;
  262. data.module = m;
  263. pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
  264. pa_source_new_data_set_sample_spec(&data, &ss);
  265. pa_source_new_data_set_channel_map(&data, &map);
  266. //pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "Null Input"));
  267. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "xrdp Input"));
  268. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
  269. u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY | PA_SOURCE_DYNAMIC_LATENCY);
  270. pa_source_new_data_done(&data);
  271. if (!u->source) {
  272. pa_log("Failed to create source object.");
  273. goto fail;
  274. }
  275. u->latency_time = DEFAULT_LATENCY_TIME;
  276. if (pa_modargs_get_value_u32(ma, "latency_time", &latency_time) < 0) {
  277. pa_log("Failed to parse latency_time value.");
  278. goto fail;
  279. }
  280. u->latency_time = latency_time;
  281. u->source->parent.process_msg = source_process_msg;
  282. u->source->update_requested_latency = source_update_requested_latency_cb;
  283. u->source->userdata = u;
  284. pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
  285. pa_source_set_rtpoll(u->source, u->rtpoll);
  286. pa_source_set_latency_range(u->source, 0, MAX_LATENCY_USEC);
  287. u->block_usec = u->source->thread_info.max_latency;
  288. u->source->thread_info.max_rewind =
  289. pa_usec_to_bytes(u->block_usec, &u->source->sample_spec);
  290. if (!(u->thread = pa_thread_new("null-source", thread_func, u))) {
  291. pa_log("Failed to create thread.");
  292. goto fail;
  293. }
  294. pa_source_put(u->source);
  295. pa_modargs_free(ma);
  296. u->display_num = get_display_num_from_display(getenv("DISPLAY"));
  297. return 0;
  298. fail:
  299. if (ma)
  300. pa_modargs_free(ma);
  301. pa__done(m);
  302. return -1;
  303. }
  304. void pa__done(pa_module*m) {
  305. struct userdata *u;
  306. pa_assert(m);
  307. if (!(u = m->userdata))
  308. return;
  309. if (u->source)
  310. pa_source_unlink(u->source);
  311. if (u->thread) {
  312. pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
  313. pa_thread_free(u->thread);
  314. }
  315. pa_thread_mq_done(&u->thread_mq);
  316. if (u->source)
  317. pa_source_unref(u->source);
  318. if (u->rtpoll)
  319. pa_rtpoll_free(u->rtpoll);
  320. pa_xfree(u);
  321. }
  322. static int get_display_num_from_display(char *display_text) {
  323. int index;
  324. int mode;
  325. int host_index;
  326. int disp_index;
  327. int scre_index;
  328. int display_num;
  329. char host[256];
  330. char disp[256];
  331. char scre[256];
  332. if (display_text == NULL) {
  333. return 0;
  334. }
  335. memset(host, 0, 256);
  336. memset(disp, 0, 256);
  337. memset(scre, 0, 256);
  338. index = 0;
  339. host_index = 0;
  340. disp_index = 0;
  341. scre_index = 0;
  342. mode = 0;
  343. while (display_text[index] != 0) {
  344. if (display_text[index] == ':') {
  345. mode = 1;
  346. } else if (display_text[index] == '.') {
  347. mode = 2;
  348. } else if (mode == 0) {
  349. host[host_index] = display_text[index];
  350. host_index++;
  351. } else if (mode == 1) {
  352. disp[disp_index] = display_text[index];
  353. disp_index++;
  354. } else if (mode == 2) {
  355. scre[scre_index] = display_text[index];
  356. scre_index++;
  357. }
  358. index++;
  359. }
  360. host[host_index] = 0;
  361. disp[disp_index] = 0;
  362. scre[scre_index] = 0;
  363. display_num = atoi(disp);
  364. return display_num;
  365. }