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.

613 lines
15 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 <pulsecore/core-error.h>
  46. #include <pulsecore/sink.h>
  47. #include <pulsecore/module.h>
  48. #include <pulsecore/core-util.h>
  49. #include <pulsecore/modargs.h>
  50. #include <pulsecore/log.h>
  51. #include <pulsecore/thread.h>
  52. #include <pulsecore/thread-mq.h>
  53. #include <pulsecore/rtpoll.h>
  54. /* defined in pulse/version.h */
  55. #if PA_PROTOCOL_VERSION > 28
  56. /* these used to be defined in pulsecore/macro.h */
  57. typedef bool pa_bool_t;
  58. #define FALSE ((pa_bool_t) 0)
  59. #define TRUE (!FALSE)
  60. #else
  61. #endif
  62. #include "module-xrdp-sink-symdef.h"
  63. #include <xrdp_sockets.h>
  64. PA_MODULE_AUTHOR("Jay Sorg");
  65. PA_MODULE_DESCRIPTION("xrdp sink");
  66. PA_MODULE_VERSION(PACKAGE_VERSION);
  67. PA_MODULE_LOAD_ONCE(FALSE);
  68. PA_MODULE_USAGE(
  69. "sink_name=<name for the sink> "
  70. "sink_properties=<properties for the sink> "
  71. "format=<sample format> "
  72. "rate=<sample rate> "
  73. "channels=<number of channels> "
  74. "channel_map=<channel map>");
  75. #define DEFAULT_SINK_NAME "xrdp-sink"
  76. #define BLOCK_USEC 30000
  77. //#define BLOCK_USEC (PA_USEC_PER_SEC * 2)
  78. struct userdata {
  79. pa_core *core;
  80. pa_module *module;
  81. pa_sink *sink;
  82. pa_thread *thread;
  83. pa_thread_mq thread_mq;
  84. pa_rtpoll *rtpoll;
  85. pa_usec_t block_usec;
  86. pa_usec_t timestamp;
  87. pa_usec_t failed_connect_time;
  88. pa_usec_t last_send_time;
  89. int fd; /* unix domain socket connection to xrdp chansrv */
  90. int display_num;
  91. int skip_bytes;
  92. int got_max_latency;
  93. };
  94. static const char* const valid_modargs[] = {
  95. "sink_name",
  96. "sink_properties",
  97. "format",
  98. "rate",
  99. "channels",
  100. "channel_map",
  101. NULL
  102. };
  103. static int close_send(struct userdata *u);
  104. static int sink_process_msg(pa_msgobject *o, int code, void *data,
  105. int64_t offset, pa_memchunk *chunk) {
  106. struct userdata *u = PA_SINK(o)->userdata;
  107. pa_usec_t now;
  108. long lat;
  109. pa_log_debug("sink_process_msg: code %d", code);
  110. switch (code) {
  111. case PA_SINK_MESSAGE_SET_VOLUME: /* 3 */
  112. break;
  113. case PA_SINK_MESSAGE_SET_MUTE: /* 6 */
  114. break;
  115. case PA_SINK_MESSAGE_GET_LATENCY: /* 7 */
  116. now = pa_rtclock_now();
  117. lat = u->timestamp > now ? u->timestamp - now : 0ULL;
  118. pa_log_debug("sink_process_msg: lat %ld", lat);
  119. *((pa_usec_t*) data) = lat;
  120. return 0;
  121. case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY: /* 8 */
  122. break;
  123. case PA_SINK_MESSAGE_SET_STATE: /* 9 */
  124. if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING) /* 0 */ {
  125. pa_log("sink_process_msg: running");
  126. u->timestamp = pa_rtclock_now();
  127. } else {
  128. pa_log("sink_process_msg: not running");
  129. close_send(u);
  130. }
  131. break;
  132. }
  133. return pa_sink_process_msg(o, code, data, offset, chunk);
  134. }
  135. static void sink_update_requested_latency_cb(pa_sink *s) {
  136. struct userdata *u;
  137. size_t nbytes;
  138. pa_sink_assert_ref(s);
  139. pa_assert_se(u = s->userdata);
  140. u->block_usec = BLOCK_USEC;
  141. //u->block_usec = pa_sink_get_requested_latency_within_thread(s);
  142. pa_log("1 block_usec %llu", (unsigned long long) u->block_usec);
  143. u->got_max_latency = 0;
  144. if (u->block_usec == (pa_usec_t) -1) {
  145. u->block_usec = s->thread_info.max_latency;
  146. pa_log_debug("2 block_usec %llu", (unsigned long long) u->block_usec);
  147. u->got_max_latency = 1;
  148. }
  149. nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec);
  150. pa_sink_set_max_rewind_within_thread(s, nbytes);
  151. pa_sink_set_max_request_within_thread(s, nbytes);
  152. }
  153. static void process_rewind(struct userdata *u, pa_usec_t now) {
  154. size_t rewind_nbytes, in_buffer;
  155. pa_usec_t delay;
  156. pa_assert(u);
  157. /* Figure out how much we shall rewind and reset the counter */
  158. rewind_nbytes = u->sink->thread_info.rewind_nbytes;
  159. u->sink->thread_info.rewind_nbytes = 0;
  160. pa_assert(rewind_nbytes > 0);
  161. pa_log_debug("Requested to rewind %lu bytes.",
  162. (unsigned long) rewind_nbytes);
  163. if (u->timestamp <= now)
  164. goto do_nothing;
  165. delay = u->timestamp - now;
  166. in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
  167. if (in_buffer <= 0)
  168. goto do_nothing;
  169. if (rewind_nbytes > in_buffer)
  170. rewind_nbytes = in_buffer;
  171. pa_sink_process_rewind(u->sink, rewind_nbytes);
  172. u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
  173. u->skip_bytes += rewind_nbytes;
  174. pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
  175. return;
  176. do_nothing:
  177. pa_sink_process_rewind(u->sink, 0);
  178. }
  179. struct header {
  180. int code;
  181. int bytes;
  182. };
  183. static int get_display_num_from_display(char *display_text) {
  184. int index;
  185. int mode;
  186. int host_index;
  187. int disp_index;
  188. int scre_index;
  189. int display_num;
  190. char host[256];
  191. char disp[256];
  192. char scre[256];
  193. if (display_text == NULL) {
  194. return 0;
  195. }
  196. memset(host, 0, 256);
  197. memset(disp, 0, 256);
  198. memset(scre, 0, 256);
  199. index = 0;
  200. host_index = 0;
  201. disp_index = 0;
  202. scre_index = 0;
  203. mode = 0;
  204. while (display_text[index] != 0) {
  205. if (display_text[index] == ':') {
  206. mode = 1;
  207. } else if (display_text[index] == '.') {
  208. mode = 2;
  209. } else if (mode == 0) {
  210. host[host_index] = display_text[index];
  211. host_index++;
  212. } else if (mode == 1) {
  213. disp[disp_index] = display_text[index];
  214. disp_index++;
  215. } else if (mode == 2) {
  216. scre[scre_index] = display_text[index];
  217. scre_index++;
  218. }
  219. index++;
  220. }
  221. host[host_index] = 0;
  222. disp[disp_index] = 0;
  223. scre[scre_index] = 0;
  224. display_num = atoi(disp);
  225. return display_num;
  226. }
  227. static int lsend(int fd, char *data, int bytes) {
  228. int sent = 0;
  229. int error;
  230. while (sent < bytes) {
  231. error = send(fd, data + sent, bytes - sent, 0);
  232. if (error < 1) {
  233. return error;
  234. }
  235. sent += error;
  236. }
  237. return sent;
  238. }
  239. static int data_send(struct userdata *u, pa_memchunk *chunk) {
  240. char *data;
  241. char *socket_dir;
  242. int bytes;
  243. int sent;
  244. int fd;
  245. struct header h;
  246. struct sockaddr_un s;
  247. if (u->fd == 0) {
  248. if (u->failed_connect_time != 0) {
  249. if (pa_rtclock_now() - u->failed_connect_time < 1000000) {
  250. return 0;
  251. }
  252. }
  253. fd = socket(PF_LOCAL, SOCK_STREAM, 0);
  254. memset(&s, 0, sizeof(s));
  255. s.sun_family = AF_UNIX;
  256. bytes = sizeof(s.sun_path) - 1;
  257. socket_dir = getenv("XRDP_SOCKET_PATH");
  258. if (socket_dir == NULL || socket_dir[0] == '\0')
  259. {
  260. socket_dir = "/tmp/.xrdp";
  261. }
  262. snprintf(s.sun_path, bytes, "%s/" CHANSRV_PORT_OUT_BASE_STR,
  263. socket_dir, u->display_num);
  264. pa_log_debug("trying to connect to %s", s.sun_path);
  265. if (connect(fd, (struct sockaddr *)&s,
  266. sizeof(struct sockaddr_un)) != 0) {
  267. u->failed_connect_time = pa_rtclock_now();
  268. pa_log_debug("Connected failed");
  269. close(fd);
  270. return 0;
  271. }
  272. u->failed_connect_time = 0;
  273. pa_log("Connected ok fd %d", fd);
  274. u->fd = fd;
  275. }
  276. bytes = chunk->length;
  277. pa_log_debug("bytes %d", bytes);
  278. /* from rewind */
  279. if (u->skip_bytes > 0) {
  280. if (bytes > u->skip_bytes) {
  281. bytes -= u->skip_bytes;
  282. u->skip_bytes = 0;
  283. } else {
  284. u->skip_bytes -= bytes;
  285. return bytes;
  286. }
  287. }
  288. h.code = 0;
  289. h.bytes = bytes + 8;
  290. if (lsend(u->fd, (char*)(&h), 8) != 8) {
  291. pa_log("data_send: send failed");
  292. close(u->fd);
  293. u->fd = 0;
  294. return 0;
  295. } else {
  296. pa_log_debug("data_send: sent header ok bytes %d", bytes);
  297. }
  298. data = (char*)pa_memblock_acquire(chunk->memblock);
  299. data += chunk->index;
  300. sent = lsend(u->fd, data, bytes);
  301. pa_memblock_release(chunk->memblock);
  302. if (sent != bytes) {
  303. pa_log("data_send: send failed sent %d bytes %d", sent, bytes);
  304. close(u->fd);
  305. u->fd = 0;
  306. return 0;
  307. }
  308. return sent;
  309. }
  310. static int close_send(struct userdata *u) {
  311. struct header h;
  312. pa_log("close_send:");
  313. if (u->fd == 0) {
  314. return 0;
  315. }
  316. h.code = 1;
  317. h.bytes = 8;
  318. if (lsend(u->fd, (char*)(&h), 8) != 8) {
  319. pa_log("close_send: send failed");
  320. close(u->fd);
  321. u->fd = 0;
  322. return 0;
  323. } else {
  324. pa_log_debug("close_send: sent header ok");
  325. }
  326. return 8;
  327. }
  328. static void process_render(struct userdata *u, pa_usec_t now) {
  329. pa_memchunk chunk;
  330. int request_bytes;
  331. pa_assert(u);
  332. if (u->got_max_latency) {
  333. return;
  334. }
  335. pa_log_debug("process_render: u->block_usec %llu", (unsigned long long) u->block_usec);
  336. while (u->timestamp < now + u->block_usec) {
  337. request_bytes = u->sink->thread_info.max_request;
  338. request_bytes = MIN(request_bytes, 16 * 1024);
  339. pa_sink_render(u->sink, request_bytes, &chunk);
  340. data_send(u, &chunk);
  341. pa_memblock_unref(chunk.memblock);
  342. u->timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
  343. }
  344. }
  345. static void thread_func(void *userdata) {
  346. struct userdata *u = userdata;
  347. int ret;
  348. pa_usec_t now;
  349. pa_assert(u);
  350. pa_log_debug("Thread starting up");
  351. pa_thread_mq_install(&u->thread_mq);
  352. u->timestamp = pa_rtclock_now();
  353. for (;;) {
  354. if (u->sink->thread_info.state == PA_SINK_RUNNING) {
  355. now = pa_rtclock_now();
  356. if (u->sink->thread_info.rewind_requested) {
  357. if (u->sink->thread_info.rewind_nbytes > 0) {
  358. process_rewind(u, now);
  359. } else {
  360. pa_sink_process_rewind(u->sink, 0);
  361. }
  362. }
  363. if (u->timestamp <= now) {
  364. pa_log_debug("thread_func: calling process_render");
  365. process_render(u, now);
  366. }
  367. pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
  368. } else {
  369. pa_rtpoll_set_timer_disabled(u->rtpoll);
  370. }
  371. #if defined(PA_CHECK_VERSION) && PA_CHECK_VERSION(6, 0, 0)
  372. if ((ret = pa_rtpoll_run(u->rtpoll)) < 0) {
  373. #else
  374. if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) {
  375. #endif
  376. goto fail;
  377. }
  378. if (ret == 0) {
  379. goto finish;
  380. }
  381. }
  382. fail:
  383. /* If this was no regular exit from the loop we have to continue
  384. * processing messages until we received PA_MESSAGE_SHUTDOWN */
  385. pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core),
  386. PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0,
  387. NULL, NULL);
  388. pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
  389. finish:
  390. pa_log_debug("Thread shutting down");
  391. }
  392. int pa__init(pa_module*m) {
  393. struct userdata *u = NULL;
  394. pa_sample_spec ss;
  395. pa_channel_map map;
  396. pa_modargs *ma = NULL;
  397. pa_sink_new_data data;
  398. size_t nbytes;
  399. pa_assert(m);
  400. if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
  401. pa_log("Failed to parse module arguments.");
  402. goto fail;
  403. }
  404. ss = m->core->default_sample_spec;
  405. map = m->core->default_channel_map;
  406. if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map,
  407. PA_CHANNEL_MAP_DEFAULT) < 0) {
  408. pa_log("Invalid sample format specification or channel map");
  409. goto fail;
  410. }
  411. m->userdata = u = pa_xnew0(struct userdata, 1);
  412. u->core = m->core;
  413. u->module = m;
  414. u->rtpoll = pa_rtpoll_new();
  415. pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
  416. pa_sink_new_data_init(&data);
  417. data.driver = __FILE__;
  418. data.module = m;
  419. pa_sink_new_data_set_name(&data,
  420. pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
  421. pa_sink_new_data_set_sample_spec(&data, &ss);
  422. pa_sink_new_data_set_channel_map(&data, &map);
  423. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "xrdp sink");
  424. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
  425. if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist,
  426. PA_UPDATE_REPLACE) < 0) {
  427. pa_log("Invalid properties");
  428. pa_sink_new_data_done(&data);
  429. goto fail;
  430. }
  431. u->sink = pa_sink_new(m->core, &data,
  432. PA_SINK_LATENCY | PA_SINK_DYNAMIC_LATENCY);
  433. pa_sink_new_data_done(&data);
  434. if (!u->sink) {
  435. pa_log("Failed to create sink object.");
  436. goto fail;
  437. }
  438. u->sink->parent.process_msg = sink_process_msg;
  439. u->sink->update_requested_latency = sink_update_requested_latency_cb;
  440. u->sink->userdata = u;
  441. pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
  442. pa_sink_set_rtpoll(u->sink, u->rtpoll);
  443. u->block_usec = BLOCK_USEC;
  444. pa_log_debug("3 block_usec %llu", (unsigned long long) u->block_usec);
  445. nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
  446. pa_sink_set_max_rewind(u->sink, nbytes);
  447. pa_sink_set_max_request(u->sink, nbytes);
  448. u->display_num = get_display_num_from_display(getenv("DISPLAY"));
  449. #if defined(PA_CHECK_VERSION)
  450. #if PA_CHECK_VERSION(0, 9, 22)
  451. if (!(u->thread = pa_thread_new("xrdp-sink", thread_func, u))) {
  452. #else
  453. if (!(u->thread = pa_thread_new(thread_func, u))) {
  454. #endif
  455. #else
  456. if (!(u->thread = pa_thread_new(thread_func, u))) {
  457. #endif
  458. pa_log("Failed to create thread.");
  459. goto fail;
  460. }
  461. pa_sink_put(u->sink);
  462. pa_modargs_free(ma);
  463. return 0;
  464. fail:
  465. if (ma) {
  466. pa_modargs_free(ma);
  467. }
  468. pa__done(m);
  469. return -1;
  470. }
  471. int pa__get_n_used(pa_module *m) {
  472. struct userdata *u;
  473. pa_assert(m);
  474. pa_assert_se(u = m->userdata);
  475. return pa_sink_linked_by(u->sink);
  476. }
  477. void pa__done(pa_module*m) {
  478. struct userdata *u;
  479. pa_assert(m);
  480. if (!(u = m->userdata)) {
  481. return;
  482. }
  483. if (u->sink) {
  484. pa_sink_unlink(u->sink);
  485. }
  486. if (u->thread) {
  487. pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN,
  488. NULL, 0, NULL);
  489. pa_thread_free(u->thread);
  490. }
  491. pa_thread_mq_done(&u->thread_mq);
  492. if (u->sink) {
  493. pa_sink_unref(u->sink);
  494. }
  495. if (u->rtpoll) {
  496. pa_rtpoll_free(u->rtpoll);
  497. }
  498. pa_xfree(u);
  499. }