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.

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