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.

494 lines
12 KiB

  1. /**
  2. * xrdp: A Remote Desktop Protocol server.
  3. * pulse sink
  4. *
  5. * Copyright (C) Jay Sorg 2013
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * see pulse-notes.txt
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include <netinet/in.h>
  28. #include <netinet/tcp.h>
  29. #include <sys/socket.h>
  30. #include <sys/un.h>
  31. #include <sys/types.h>
  32. #include <stdlib.h>
  33. #include <sys/stat.h>
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #include <string.h>
  37. #include <fcntl.h>
  38. #include <unistd.h>
  39. #include <limits.h>
  40. #include <sys/ioctl.h>
  41. #include <poll.h>
  42. #include <pulse/rtclock.h>
  43. #include <pulse/timeval.h>
  44. #include <pulse/xmalloc.h>
  45. #include <pulse/i18n.h>
  46. #include <pulsecore/core-error.h>
  47. #include <pulsecore/sink.h>
  48. #include <pulsecore/module.h>
  49. #include <pulsecore/core-util.h>
  50. #include <pulsecore/modargs.h>
  51. #include <pulsecore/log.h>
  52. #include <pulsecore/thread.h>
  53. #include <pulsecore/thread-mq.h>
  54. #include <pulsecore/rtpoll.h>
  55. #include "module-xrdp-sink-symdef.h"
  56. PA_MODULE_AUTHOR("Jay Sorg");
  57. PA_MODULE_DESCRIPTION("xrdp sink");
  58. PA_MODULE_VERSION(PACKAGE_VERSION);
  59. PA_MODULE_LOAD_ONCE(FALSE);
  60. PA_MODULE_USAGE(
  61. "sink_name=<name for the sink> "
  62. "sink_properties=<properties for the sink> "
  63. "format=<sample format> "
  64. "rate=<sample rate>"
  65. "channels=<number of channels> "
  66. "channel_map=<channel map>");
  67. #define DEFAULT_SINK_NAME "xrdp"
  68. #define BLOCK_USEC (PA_USEC_PER_SEC * 2)
  69. #define CHANSRV_PORT_STR "/tmp/.xrdp/xrdp_chansrv_audio_socket_%d"
  70. struct userdata {
  71. pa_core *core;
  72. pa_module *module;
  73. pa_sink *sink;
  74. pa_thread *thread;
  75. pa_thread_mq thread_mq;
  76. pa_rtpoll *rtpoll;
  77. pa_usec_t block_usec;
  78. pa_usec_t timestamp;
  79. int fd; /* unix domain socket connection to chansrv */
  80. pa_memchunk memchunk;
  81. };
  82. static const char* const valid_modargs[] = {
  83. "sink_name",
  84. "sink_properties",
  85. "format",
  86. "rate",
  87. "channels",
  88. "channel_map",
  89. NULL
  90. };
  91. static int sink_process_msg(pa_msgobject *o, int code, void *data,
  92. int64_t offset, pa_memchunk *chunk) {
  93. struct userdata *u = PA_SINK(o)->userdata;
  94. pa_usec_t now;
  95. //pa_log("sink_process_msg: code %d", code);
  96. switch (code) {
  97. case PA_SINK_MESSAGE_SET_STATE:
  98. if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING) {
  99. u->timestamp = pa_rtclock_now();
  100. }
  101. break;
  102. case PA_SINK_MESSAGE_GET_LATENCY:
  103. now = pa_rtclock_now();
  104. *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0ULL;
  105. return 0;
  106. }
  107. return pa_sink_process_msg(o, code, data, offset, chunk);
  108. }
  109. static void sink_update_requested_latency_cb(pa_sink *s) {
  110. struct userdata *u;
  111. size_t nbytes;
  112. pa_sink_assert_ref(s);
  113. pa_assert_se(u = s->userdata);
  114. u->block_usec = pa_sink_get_requested_latency_within_thread(s);
  115. if (u->block_usec == (pa_usec_t) -1)
  116. u->block_usec = s->thread_info.max_latency;
  117. nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec);
  118. pa_sink_set_max_rewind_within_thread(s, nbytes);
  119. pa_sink_set_max_request_within_thread(s, nbytes);
  120. }
  121. static void process_rewind(struct userdata *u, pa_usec_t now) {
  122. size_t rewind_nbytes, in_buffer;
  123. pa_usec_t delay;
  124. pa_assert(u);
  125. /* Figure out how much we shall rewind and reset the counter */
  126. rewind_nbytes = u->sink->thread_info.rewind_nbytes;
  127. u->sink->thread_info.rewind_nbytes = 0;
  128. pa_assert(rewind_nbytes > 0);
  129. pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes);
  130. if (u->timestamp <= now)
  131. goto do_nothing;
  132. delay = u->timestamp - now;
  133. in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
  134. if (in_buffer <= 0)
  135. goto do_nothing;
  136. if (rewind_nbytes > in_buffer)
  137. rewind_nbytes = in_buffer;
  138. pa_sink_process_rewind(u->sink, rewind_nbytes);
  139. u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
  140. pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
  141. return;
  142. do_nothing:
  143. pa_sink_process_rewind(u->sink, 0);
  144. }
  145. struct header
  146. {
  147. int code;
  148. int bytes;
  149. };
  150. static int get_display_num_from_display(char *display_text) {
  151. int index;
  152. int mode;
  153. int host_index;
  154. int disp_index;
  155. int scre_index;
  156. int display_num;
  157. char host[256];
  158. char disp[256];
  159. char scre[256];
  160. memset(host, 0, 256);
  161. memset(disp, 0, 256);
  162. memset(scre, 0, 256);
  163. index = 0;
  164. host_index = 0;
  165. disp_index = 0;
  166. scre_index = 0;
  167. mode = 0;
  168. while (display_text[index] != 0) {
  169. if (display_text[index] == ':') {
  170. mode = 1;
  171. } else if (display_text[index] == '.') {
  172. mode = 2;
  173. } else if (mode == 0) {
  174. host[host_index] = display_text[index];
  175. host_index++;
  176. } else if (mode == 1) {
  177. disp[disp_index] = display_text[index];
  178. disp_index++;
  179. } else if (mode == 2) {
  180. scre[scre_index] = display_text[index];
  181. scre_index++;
  182. }
  183. index++;
  184. }
  185. host[host_index] = 0;
  186. disp[disp_index] = 0;
  187. scre[scre_index] = 0;
  188. display_num = atoi(disp);
  189. return display_num;
  190. }
  191. static int data_send(struct userdata *u) {
  192. char *data;
  193. int bytes;
  194. int sent;
  195. int display_num;
  196. struct header h;
  197. if (u->fd == 0) {
  198. int fd = socket(PF_LOCAL, SOCK_STREAM, 0);
  199. struct sockaddr_un s = { 0 };
  200. s.sun_family = AF_UNIX;
  201. display_num = get_display_num_from_display(getenv("DISPLAY"));
  202. bytes = sizeof(s.sun_path) - 1;
  203. snprintf(s.sun_path, bytes, CHANSRV_PORT_STR, display_num);
  204. if (connect(fd, (struct sockaddr *)&s,
  205. sizeof(struct sockaddr_un)) != 0) {
  206. //pa_log("Connected failed");
  207. close(fd);
  208. return 0;
  209. }
  210. u->fd = fd;
  211. pa_log("Connected ok fd %d", fd);
  212. }
  213. bytes = u->memchunk.length;
  214. pa_log("bytes %d", bytes);
  215. h.code = 0;
  216. h.bytes = bytes + 8;
  217. if (send(u->fd, &h, 8, 0) != 8) {
  218. pa_log("data_send: send failed");
  219. close(u->fd);
  220. u->fd = 0;
  221. return 0;
  222. } else {
  223. pa_log("data_send: sent header ok bytes %d", bytes);
  224. }
  225. data = (char*)pa_memblock_acquire(u->memchunk.memblock);
  226. data += u->memchunk.index;
  227. sent = send(u->fd, data, bytes, 0);
  228. pa_memblock_release(u->memchunk.memblock);
  229. if (sent != bytes) {
  230. pa_log("data_send: send failed sent %d bytes %d", sent, bytes);
  231. close(u->fd);
  232. u->fd = 0;
  233. return 0;
  234. }
  235. u->memchunk.index += sent;
  236. u->memchunk.length -= sent;
  237. if (u->memchunk.length <= 0) {
  238. pa_memblock_unref(u->memchunk.memblock);
  239. pa_memchunk_reset(&u->memchunk);
  240. }
  241. return sent;
  242. }
  243. static void process_render(struct userdata *u, pa_usec_t now) {
  244. //pa_log("%d", u->memchunk.length);
  245. //pa_log("a");
  246. //if (u->memchunk.length <= 0)
  247. pa_sink_render(u->sink, 8192, &u->memchunk);
  248. //pa_log("b");
  249. //data_send(u);
  250. //pa_log("c");
  251. }
  252. static void thread_func(void *userdata) {
  253. struct userdata *u = userdata;
  254. pa_assert(u);
  255. pa_log_debug("Thread starting up");
  256. pa_thread_mq_install(&u->thread_mq);
  257. u->timestamp = pa_rtclock_now();
  258. for (;;) {
  259. int ret;
  260. /* Render some data and drop it immediately */
  261. if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
  262. pa_usec_t now;
  263. now = pa_rtclock_now();
  264. if (u->sink->thread_info.rewind_requested) {
  265. if (u->sink->thread_info.rewind_nbytes > 0)
  266. process_rewind(u, now);
  267. else
  268. pa_sink_process_rewind(u->sink, 0);
  269. }
  270. if (u->timestamp <= now)
  271. process_render(u, now);
  272. pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
  273. } else
  274. pa_rtpoll_set_timer_disabled(u->rtpoll);
  275. /* Hmm, nothing to do. Let's sleep */
  276. if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
  277. goto fail;
  278. if (ret == 0)
  279. goto finish;
  280. }
  281. fail:
  282. /* If this was no regular exit from the loop we have to continue
  283. * processing messages until we received PA_MESSAGE_SHUTDOWN */
  284. pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
  285. pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
  286. finish:
  287. pa_log_debug("Thread shutting down");
  288. }
  289. int pa__init(pa_module*m) {
  290. struct userdata *u = NULL;
  291. pa_sample_spec ss;
  292. pa_channel_map map;
  293. pa_modargs *ma = NULL;
  294. pa_sink_new_data data;
  295. size_t nbytes;
  296. pa_assert(m);
  297. if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
  298. pa_log("Failed to parse module arguments.");
  299. goto fail;
  300. }
  301. ss = m->core->default_sample_spec;
  302. map = m->core->default_channel_map;
  303. if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
  304. pa_log("Invalid sample format specification or channel map");
  305. goto fail;
  306. }
  307. m->userdata = u = pa_xnew0(struct userdata, 1);
  308. u->core = m->core;
  309. u->module = m;
  310. u->rtpoll = pa_rtpoll_new();
  311. pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
  312. pa_sink_new_data_init(&data);
  313. data.driver = __FILE__;
  314. data.module = m;
  315. pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
  316. pa_sink_new_data_set_sample_spec(&data, &ss);
  317. pa_sink_new_data_set_channel_map(&data, &map);
  318. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "xrdp sink");
  319. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
  320. if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
  321. pa_log("Invalid properties");
  322. pa_sink_new_data_done(&data);
  323. goto fail;
  324. }
  325. u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY);
  326. pa_sink_new_data_done(&data);
  327. if (!u->sink) {
  328. pa_log("Failed to create sink object.");
  329. goto fail;
  330. }
  331. u->sink->parent.process_msg = sink_process_msg;
  332. u->sink->update_requested_latency = sink_update_requested_latency_cb;
  333. u->sink->userdata = u;
  334. pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
  335. pa_sink_set_rtpoll(u->sink, u->rtpoll);
  336. u->block_usec = BLOCK_USEC;
  337. nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
  338. pa_sink_set_max_rewind(u->sink, nbytes);
  339. pa_sink_set_max_request(u->sink, nbytes);
  340. pa_memchunk_reset(&u->memchunk);
  341. #if defined(PA_CHECK_VERSION) && (PA_CHECK_VERSION(0, 9, 22))
  342. if (!(u->thread = pa_thread_new("xrdp-sink", thread_func, u))) {
  343. #else
  344. if (!(u->thread = pa_thread_new(thread_func, u))) {
  345. #endif
  346. pa_log("Failed to create thread.");
  347. goto fail;
  348. }
  349. pa_sink_put(u->sink);
  350. pa_modargs_free(ma);
  351. return 0;
  352. fail:
  353. if (ma)
  354. pa_modargs_free(ma);
  355. pa__done(m);
  356. return -1;
  357. }
  358. int pa__get_n_used(pa_module *m) {
  359. struct userdata *u;
  360. pa_assert(m);
  361. pa_assert_se(u = m->userdata);
  362. return pa_sink_linked_by(u->sink);
  363. }
  364. void pa__done(pa_module*m) {
  365. struct userdata *u;
  366. pa_assert(m);
  367. if (!(u = m->userdata))
  368. return;
  369. if (u->sink)
  370. pa_sink_unlink(u->sink);
  371. if (u->memchunk.memblock)
  372. pa_memblock_unref(u->memchunk.memblock);
  373. if (u->thread) {
  374. pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
  375. pa_thread_free(u->thread);
  376. }
  377. pa_thread_mq_done(&u->thread_mq);
  378. if (u->sink)
  379. pa_sink_unref(u->sink);
  380. if (u->rtpoll)
  381. pa_rtpoll_free(u->rtpoll);
  382. pa_xfree(u);
  383. }