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.

606 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 "../common/file_loc.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. int bytes;
  242. int sent;
  243. int fd;
  244. struct header h;
  245. struct sockaddr_un s;
  246. if (u->fd == 0) {
  247. if (u->failed_connect_time != 0) {
  248. if (pa_rtclock_now() - u->failed_connect_time < 1000000) {
  249. return 0;
  250. }
  251. }
  252. fd = socket(PF_LOCAL, SOCK_STREAM, 0);
  253. memset(&s, 0, sizeof(s));
  254. s.sun_family = AF_UNIX;
  255. bytes = sizeof(s.sun_path) - 1;
  256. snprintf(s.sun_path, bytes, CHANSRV_PORT_OUT_STR, u->display_num);
  257. pa_log_debug("trying to connect to %s", s.sun_path);
  258. if (connect(fd, (struct sockaddr *)&s,
  259. sizeof(struct sockaddr_un)) != 0) {
  260. u->failed_connect_time = pa_rtclock_now();
  261. pa_log_debug("Connected failed");
  262. close(fd);
  263. return 0;
  264. }
  265. u->failed_connect_time = 0;
  266. pa_log("Connected ok fd %d", fd);
  267. u->fd = fd;
  268. }
  269. bytes = chunk->length;
  270. pa_log_debug("bytes %d", bytes);
  271. /* from rewind */
  272. if (u->skip_bytes > 0) {
  273. if (bytes > u->skip_bytes) {
  274. bytes -= u->skip_bytes;
  275. u->skip_bytes = 0;
  276. } else {
  277. u->skip_bytes -= bytes;
  278. return bytes;
  279. }
  280. }
  281. h.code = 0;
  282. h.bytes = bytes + 8;
  283. if (lsend(u->fd, (char*)(&h), 8) != 8) {
  284. pa_log("data_send: send failed");
  285. close(u->fd);
  286. u->fd = 0;
  287. return 0;
  288. } else {
  289. pa_log_debug("data_send: sent header ok bytes %d", bytes);
  290. }
  291. data = (char*)pa_memblock_acquire(chunk->memblock);
  292. data += chunk->index;
  293. sent = lsend(u->fd, data, bytes);
  294. pa_memblock_release(chunk->memblock);
  295. if (sent != bytes) {
  296. pa_log("data_send: send failed sent %d bytes %d", sent, bytes);
  297. close(u->fd);
  298. u->fd = 0;
  299. return 0;
  300. }
  301. return sent;
  302. }
  303. static int close_send(struct userdata *u) {
  304. struct header h;
  305. pa_log("close_send:");
  306. if (u->fd == 0) {
  307. return 0;
  308. }
  309. h.code = 1;
  310. h.bytes = 8;
  311. if (lsend(u->fd, (char*)(&h), 8) != 8) {
  312. pa_log("close_send: send failed");
  313. close(u->fd);
  314. u->fd = 0;
  315. return 0;
  316. } else {
  317. pa_log_debug("close_send: sent header ok");
  318. }
  319. return 8;
  320. }
  321. static void process_render(struct userdata *u, pa_usec_t now) {
  322. pa_memchunk chunk;
  323. int request_bytes;
  324. pa_assert(u);
  325. if (u->got_max_latency) {
  326. return;
  327. }
  328. pa_log_debug("process_render: u->block_usec %llu", (unsigned long long) u->block_usec);
  329. while (u->timestamp < now + u->block_usec) {
  330. request_bytes = u->sink->thread_info.max_request;
  331. request_bytes = MIN(request_bytes, 16 * 1024);
  332. pa_sink_render(u->sink, request_bytes, &chunk);
  333. data_send(u, &chunk);
  334. pa_memblock_unref(chunk.memblock);
  335. u->timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
  336. }
  337. }
  338. static void thread_func(void *userdata) {
  339. struct userdata *u = userdata;
  340. int ret;
  341. pa_usec_t now;
  342. pa_assert(u);
  343. pa_log_debug("Thread starting up");
  344. pa_thread_mq_install(&u->thread_mq);
  345. u->timestamp = pa_rtclock_now();
  346. for (;;) {
  347. if (u->sink->thread_info.state == PA_SINK_RUNNING) {
  348. now = pa_rtclock_now();
  349. if (u->sink->thread_info.rewind_requested) {
  350. if (u->sink->thread_info.rewind_nbytes > 0) {
  351. process_rewind(u, now);
  352. } else {
  353. pa_sink_process_rewind(u->sink, 0);
  354. }
  355. }
  356. if (u->timestamp <= now) {
  357. pa_log_debug("thread_func: calling process_render");
  358. process_render(u, now);
  359. }
  360. pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
  361. } else {
  362. pa_rtpoll_set_timer_disabled(u->rtpoll);
  363. }
  364. #if defined(PA_CHECK_VERSION) && PA_CHECK_VERSION(6, 0, 0)
  365. if ((ret = pa_rtpoll_run(u->rtpoll)) < 0) {
  366. #else
  367. if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) {
  368. #endif
  369. goto fail;
  370. }
  371. if (ret == 0) {
  372. goto finish;
  373. }
  374. }
  375. fail:
  376. /* If this was no regular exit from the loop we have to continue
  377. * processing messages until we received PA_MESSAGE_SHUTDOWN */
  378. pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core),
  379. PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0,
  380. NULL, NULL);
  381. pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
  382. finish:
  383. pa_log_debug("Thread shutting down");
  384. }
  385. int pa__init(pa_module*m) {
  386. struct userdata *u = NULL;
  387. pa_sample_spec ss;
  388. pa_channel_map map;
  389. pa_modargs *ma = NULL;
  390. pa_sink_new_data data;
  391. size_t nbytes;
  392. pa_assert(m);
  393. if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
  394. pa_log("Failed to parse module arguments.");
  395. goto fail;
  396. }
  397. ss = m->core->default_sample_spec;
  398. map = m->core->default_channel_map;
  399. if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map,
  400. PA_CHANNEL_MAP_DEFAULT) < 0) {
  401. pa_log("Invalid sample format specification or channel map");
  402. goto fail;
  403. }
  404. m->userdata = u = pa_xnew0(struct userdata, 1);
  405. u->core = m->core;
  406. u->module = m;
  407. u->rtpoll = pa_rtpoll_new();
  408. pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
  409. pa_sink_new_data_init(&data);
  410. data.driver = __FILE__;
  411. data.module = m;
  412. pa_sink_new_data_set_name(&data,
  413. pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
  414. pa_sink_new_data_set_sample_spec(&data, &ss);
  415. pa_sink_new_data_set_channel_map(&data, &map);
  416. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "xrdp sink");
  417. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
  418. if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist,
  419. PA_UPDATE_REPLACE) < 0) {
  420. pa_log("Invalid properties");
  421. pa_sink_new_data_done(&data);
  422. goto fail;
  423. }
  424. u->sink = pa_sink_new(m->core, &data,
  425. PA_SINK_LATENCY | PA_SINK_DYNAMIC_LATENCY);
  426. pa_sink_new_data_done(&data);
  427. if (!u->sink) {
  428. pa_log("Failed to create sink object.");
  429. goto fail;
  430. }
  431. u->sink->parent.process_msg = sink_process_msg;
  432. u->sink->update_requested_latency = sink_update_requested_latency_cb;
  433. u->sink->userdata = u;
  434. pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
  435. pa_sink_set_rtpoll(u->sink, u->rtpoll);
  436. u->block_usec = BLOCK_USEC;
  437. pa_log_debug("3 block_usec %llu", (unsigned long long) u->block_usec);
  438. nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
  439. pa_sink_set_max_rewind(u->sink, nbytes);
  440. pa_sink_set_max_request(u->sink, nbytes);
  441. u->display_num = get_display_num_from_display(getenv("DISPLAY"));
  442. #if defined(PA_CHECK_VERSION)
  443. #if PA_CHECK_VERSION(0, 9, 22)
  444. if (!(u->thread = pa_thread_new("xrdp-sink", thread_func, u))) {
  445. #else
  446. if (!(u->thread = pa_thread_new(thread_func, u))) {
  447. #endif
  448. #else
  449. if (!(u->thread = pa_thread_new(thread_func, u))) {
  450. #endif
  451. pa_log("Failed to create thread.");
  452. goto fail;
  453. }
  454. pa_sink_put(u->sink);
  455. pa_modargs_free(ma);
  456. return 0;
  457. fail:
  458. if (ma) {
  459. pa_modargs_free(ma);
  460. }
  461. pa__done(m);
  462. return -1;
  463. }
  464. int pa__get_n_used(pa_module *m) {
  465. struct userdata *u;
  466. pa_assert(m);
  467. pa_assert_se(u = m->userdata);
  468. return pa_sink_linked_by(u->sink);
  469. }
  470. void pa__done(pa_module*m) {
  471. struct userdata *u;
  472. pa_assert(m);
  473. if (!(u = m->userdata)) {
  474. return;
  475. }
  476. if (u->sink) {
  477. pa_sink_unlink(u->sink);
  478. }
  479. if (u->thread) {
  480. pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN,
  481. NULL, 0, NULL);
  482. pa_thread_free(u->thread);
  483. }
  484. pa_thread_mq_done(&u->thread_mq);
  485. if (u->sink) {
  486. pa_sink_unref(u->sink);
  487. }
  488. if (u->rtpoll) {
  489. pa_rtpoll_free(u->rtpoll);
  490. }
  491. pa_xfree(u);
  492. }