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.

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