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.

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