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.

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