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.

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