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.

444 lines
11 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. To stop its respawning habit, open /etc/pulse/client.conf, change
  21. autospawn = yes to autospawn = no, and set daemon-binary to /bin/true.
  22. Make sure these lines are uncommented, like this:
  23. autospawn = no
  24. daemon-binary = /bin/true
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <netinet/in.h>
  32. #include <netinet/tcp.h>
  33. #include <sys/socket.h>
  34. #include <sys/un.h>
  35. #include <sys/types.h>
  36. #include <stdlib.h>
  37. #include <sys/stat.h>
  38. #include <stdio.h>
  39. #include <errno.h>
  40. #include <string.h>
  41. #include <fcntl.h>
  42. #include <unistd.h>
  43. #include <limits.h>
  44. #include <sys/ioctl.h>
  45. #include <poll.h>
  46. #include <pulse/rtclock.h>
  47. #include <pulse/timeval.h>
  48. #include <pulse/xmalloc.h>
  49. #include <pulse/i18n.h>
  50. #include <pulsecore/core-error.h>
  51. #include <pulsecore/sink.h>
  52. #include <pulsecore/module.h>
  53. #include <pulsecore/core-util.h>
  54. #include <pulsecore/modargs.h>
  55. #include <pulsecore/log.h>
  56. #include <pulsecore/thread.h>
  57. #include <pulsecore/thread-mq.h>
  58. #include <pulsecore/rtpoll.h>
  59. #include "module-xrdp-sink-symdef.h"
  60. PA_MODULE_AUTHOR("Jay Sorg");
  61. PA_MODULE_DESCRIPTION("xrdp sink");
  62. PA_MODULE_VERSION(PACKAGE_VERSION);
  63. PA_MODULE_LOAD_ONCE(FALSE);
  64. PA_MODULE_USAGE(
  65. "sink_name=<name for the sink> "
  66. "sink_properties=<properties for the sink> "
  67. "format=<sample format> "
  68. "rate=<sample rate>"
  69. "channels=<number of channels> "
  70. "channel_map=<channel map>");
  71. //#define DEFAULT_FILE_NAME "fifo_output"
  72. #define DEFAULT_SINK_NAME "xrdp"
  73. #define BLOCK_USEC (PA_USEC_PER_SEC * 2)
  74. struct userdata {
  75. pa_core *core;
  76. pa_module *module;
  77. pa_sink *sink;
  78. pa_thread *thread;
  79. pa_thread_mq thread_mq;
  80. pa_rtpoll *rtpoll;
  81. pa_usec_t block_usec;
  82. pa_usec_t timestamp;
  83. int fd; /* unix domain socket connection to chansrv */
  84. pa_memchunk memchunk;
  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, int64_t offset, pa_memchunk *chunk) {
  96. struct userdata *u = PA_SINK(o)->userdata;
  97. switch (code) {
  98. case PA_SINK_MESSAGE_SET_STATE:
  99. if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING)
  100. u->timestamp = pa_rtclock_now();
  101. break;
  102. case PA_SINK_MESSAGE_GET_LATENCY: {
  103. pa_usec_t now;
  104. now = pa_rtclock_now();
  105. *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0ULL;
  106. return 0;
  107. }
  108. }
  109. return pa_sink_process_msg(o, code, data, offset, chunk);
  110. }
  111. static void sink_update_requested_latency_cb(pa_sink *s) {
  112. struct userdata *u;
  113. size_t nbytes;
  114. pa_sink_assert_ref(s);
  115. pa_assert_se(u = s->userdata);
  116. u->block_usec = pa_sink_get_requested_latency_within_thread(s);
  117. if (u->block_usec == (pa_usec_t) -1)
  118. u->block_usec = s->thread_info.max_latency;
  119. nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec);
  120. pa_sink_set_max_rewind_within_thread(s, nbytes);
  121. pa_sink_set_max_request_within_thread(s, nbytes);
  122. }
  123. static void process_rewind(struct userdata *u, pa_usec_t now) {
  124. size_t rewind_nbytes, in_buffer;
  125. pa_usec_t delay;
  126. pa_assert(u);
  127. /* Figure out how much we shall rewind and reset the counter */
  128. rewind_nbytes = u->sink->thread_info.rewind_nbytes;
  129. u->sink->thread_info.rewind_nbytes = 0;
  130. pa_assert(rewind_nbytes > 0);
  131. pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes);
  132. if (u->timestamp <= now)
  133. goto do_nothing;
  134. delay = u->timestamp - now;
  135. in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
  136. if (in_buffer <= 0)
  137. goto do_nothing;
  138. if (rewind_nbytes > in_buffer)
  139. rewind_nbytes = in_buffer;
  140. pa_sink_process_rewind(u->sink, rewind_nbytes);
  141. u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
  142. pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
  143. return;
  144. do_nothing:
  145. pa_sink_process_rewind(u->sink, 0);
  146. }
  147. struct header
  148. {
  149. int code;
  150. int bytes;
  151. };
  152. static int data_send(struct userdata *u) {
  153. char *data;
  154. int bytes;
  155. int sent;
  156. struct header h;
  157. if (u->fd == 0) {
  158. int fd = socket(PF_LOCAL, SOCK_STREAM, 0);
  159. struct sockaddr_un s = { 0 };
  160. s.sun_family = AF_UNIX;
  161. strcpy(s.sun_path, "/tmp/.xrdp/xrdp_chansrv_audio_socket_10");
  162. if (connect(fd, (struct sockaddr *)&s, sizeof(struct sockaddr_un)) != 0) {
  163. pa_log("Connected failed");
  164. close(fd);
  165. return 0;
  166. }
  167. u->fd = fd;
  168. pa_log("Connected ok fd %d", fd);
  169. }
  170. bytes = u->memchunk.length;
  171. pa_log("bytes %d", bytes);
  172. h.code = 0;
  173. h.bytes = bytes + 8;
  174. if (send(u->fd, &h, 8, 0) != 8) {
  175. pa_log("data_send: send failed");
  176. close(u->fd);
  177. u->fd = 0;
  178. return 0;
  179. }
  180. else {
  181. pa_log("data_send: sent header ok bytes %d", bytes);
  182. }
  183. data = (char*)pa_memblock_acquire(u->memchunk.memblock);
  184. data += u->memchunk.index;
  185. sent = send(u->fd, data, bytes, 0);
  186. pa_memblock_release(u->memchunk.memblock);
  187. if (sent != bytes) {
  188. pa_log("data_send: send failed sent %d bytes %d", sent, bytes);
  189. close(u->fd);
  190. u->fd = 0;
  191. return 0;
  192. }
  193. u->memchunk.index += sent;
  194. u->memchunk.length -= sent;
  195. if (u->memchunk.length <= 0) {
  196. pa_memblock_unref(u->memchunk.memblock);
  197. pa_memchunk_reset(&u->memchunk);
  198. }
  199. return sent;
  200. }
  201. static void process_render(struct userdata *u, pa_usec_t now) {
  202. pa_log("%d", u->memchunk.length);
  203. pa_log("a");
  204. if (u->memchunk.length <= 0)
  205. pa_sink_render(u->sink, 8192, &u->memchunk);
  206. pa_log("b");
  207. data_send(u);
  208. pa_log("c");
  209. }
  210. static void thread_func(void *userdata) {
  211. struct userdata *u = userdata;
  212. pa_assert(u);
  213. pa_log_debug("Thread starting up");
  214. pa_thread_mq_install(&u->thread_mq);
  215. u->timestamp = pa_rtclock_now();
  216. for (;;) {
  217. int ret;
  218. /* Render some data and drop it immediately */
  219. if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
  220. pa_usec_t now;
  221. now = pa_rtclock_now();
  222. if (u->sink->thread_info.rewind_requested) {
  223. if (u->sink->thread_info.rewind_nbytes > 0)
  224. process_rewind(u, now);
  225. else
  226. pa_sink_process_rewind(u->sink, 0);
  227. }
  228. if (u->timestamp <= now)
  229. process_render(u, now);
  230. pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
  231. } else
  232. pa_rtpoll_set_timer_disabled(u->rtpoll);
  233. /* Hmm, nothing to do. Let's sleep */
  234. if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
  235. goto fail;
  236. if (ret == 0)
  237. goto finish;
  238. }
  239. fail:
  240. /* If this was no regular exit from the loop we have to continue
  241. * processing messages until we received PA_MESSAGE_SHUTDOWN */
  242. pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
  243. pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
  244. finish:
  245. pa_log_debug("Thread shutting down");
  246. }
  247. int pa__init(pa_module*m) {
  248. struct userdata *u = NULL;
  249. pa_sample_spec ss;
  250. pa_channel_map map;
  251. pa_modargs *ma = NULL;
  252. pa_sink_new_data data;
  253. size_t nbytes;
  254. pa_assert(m);
  255. if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
  256. pa_log("Failed to parse module arguments.");
  257. goto fail;
  258. }
  259. ss = m->core->default_sample_spec;
  260. map = m->core->default_channel_map;
  261. if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
  262. pa_log("Invalid sample format specification or channel map");
  263. goto fail;
  264. }
  265. m->userdata = u = pa_xnew0(struct userdata, 1);
  266. u->core = m->core;
  267. u->module = m;
  268. u->rtpoll = pa_rtpoll_new();
  269. pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
  270. pa_sink_new_data_init(&data);
  271. data.driver = __FILE__;
  272. data.module = m;
  273. pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
  274. pa_sink_new_data_set_sample_spec(&data, &ss);
  275. pa_sink_new_data_set_channel_map(&data, &map);
  276. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "xrdp sink");
  277. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
  278. if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
  279. pa_log("Invalid properties");
  280. pa_sink_new_data_done(&data);
  281. goto fail;
  282. }
  283. u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY);
  284. pa_sink_new_data_done(&data);
  285. if (!u->sink) {
  286. pa_log("Failed to create sink object.");
  287. goto fail;
  288. }
  289. u->sink->parent.process_msg = sink_process_msg;
  290. u->sink->update_requested_latency = sink_update_requested_latency_cb;
  291. u->sink->userdata = u;
  292. pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
  293. pa_sink_set_rtpoll(u->sink, u->rtpoll);
  294. u->block_usec = BLOCK_USEC;
  295. nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
  296. pa_sink_set_max_rewind(u->sink, nbytes);
  297. pa_sink_set_max_request(u->sink, nbytes);
  298. pa_memchunk_reset(&u->memchunk);
  299. if (!(u->thread = pa_thread_new("xrdp-sink", thread_func, u))) {
  300. pa_log("Failed to create thread.");
  301. goto fail;
  302. }
  303. pa_sink_put(u->sink);
  304. pa_modargs_free(ma);
  305. return 0;
  306. fail:
  307. if (ma)
  308. pa_modargs_free(ma);
  309. pa__done(m);
  310. return -1;
  311. }
  312. int pa__get_n_used(pa_module *m) {
  313. struct userdata *u;
  314. pa_assert(m);
  315. pa_assert_se(u = m->userdata);
  316. return pa_sink_linked_by(u->sink);
  317. }
  318. void pa__done(pa_module*m) {
  319. struct userdata *u;
  320. pa_assert(m);
  321. if (!(u = m->userdata))
  322. return;
  323. if (u->sink)
  324. pa_sink_unlink(u->sink);
  325. if (u->memchunk.memblock)
  326. pa_memblock_unref(u->memchunk.memblock);
  327. if (u->thread) {
  328. pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
  329. pa_thread_free(u->thread);
  330. }
  331. pa_thread_mq_done(&u->thread_mq);
  332. if (u->sink)
  333. pa_sink_unref(u->sink);
  334. if (u->rtpoll)
  335. pa_rtpoll_free(u->rtpoll);
  336. pa_xfree(u);
  337. }