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.

584 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 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("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("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 = pa_sink_get_requested_latency_within_thread(s);
  133. u->got_max_latency = 0;
  134. if (u->block_usec == (pa_usec_t) -1) {
  135. u->block_usec = s->thread_info.max_latency;
  136. u->got_max_latency = 1;
  137. }
  138. nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec);
  139. pa_sink_set_max_rewind_within_thread(s, nbytes);
  140. pa_sink_set_max_request_within_thread(s, nbytes);
  141. }
  142. static void process_rewind(struct userdata *u, pa_usec_t now) {
  143. size_t rewind_nbytes, in_buffer;
  144. pa_usec_t delay;
  145. pa_assert(u);
  146. /* Figure out how much we shall rewind and reset the counter */
  147. rewind_nbytes = u->sink->thread_info.rewind_nbytes;
  148. u->sink->thread_info.rewind_nbytes = 0;
  149. pa_assert(rewind_nbytes > 0);
  150. pa_log_debug("Requested to rewind %lu bytes.",
  151. (unsigned long) rewind_nbytes);
  152. if (u->timestamp <= now)
  153. goto do_nothing;
  154. delay = u->timestamp - now;
  155. in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
  156. if (in_buffer <= 0)
  157. goto do_nothing;
  158. if (rewind_nbytes > in_buffer)
  159. rewind_nbytes = in_buffer;
  160. pa_sink_process_rewind(u->sink, rewind_nbytes);
  161. u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
  162. u->skip_bytes += rewind_nbytes;
  163. pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
  164. return;
  165. do_nothing:
  166. pa_sink_process_rewind(u->sink, 0);
  167. }
  168. struct header {
  169. int code;
  170. int bytes;
  171. };
  172. static int get_display_num_from_display(char *display_text) {
  173. int index;
  174. int mode;
  175. int host_index;
  176. int disp_index;
  177. int scre_index;
  178. int display_num;
  179. char host[256];
  180. char disp[256];
  181. char scre[256];
  182. memset(host, 0, 256);
  183. memset(disp, 0, 256);
  184. memset(scre, 0, 256);
  185. index = 0;
  186. host_index = 0;
  187. disp_index = 0;
  188. scre_index = 0;
  189. mode = 0;
  190. while (display_text[index] != 0) {
  191. if (display_text[index] == ':') {
  192. mode = 1;
  193. } else if (display_text[index] == '.') {
  194. mode = 2;
  195. } else if (mode == 0) {
  196. host[host_index] = display_text[index];
  197. host_index++;
  198. } else if (mode == 1) {
  199. disp[disp_index] = display_text[index];
  200. disp_index++;
  201. } else if (mode == 2) {
  202. scre[scre_index] = display_text[index];
  203. scre_index++;
  204. }
  205. index++;
  206. }
  207. host[host_index] = 0;
  208. disp[disp_index] = 0;
  209. scre[scre_index] = 0;
  210. display_num = atoi(disp);
  211. return display_num;
  212. }
  213. static int data_send(struct userdata *u, pa_memchunk *chunk) {
  214. char *data;
  215. int bytes;
  216. int sent;
  217. int fd;
  218. struct header h;
  219. struct sockaddr_un s;
  220. if (u->fd == 0) {
  221. if (u->failed_connect_time != 0) {
  222. if (pa_rtclock_now() - u->failed_connect_time < 1000000) {
  223. return 0;
  224. }
  225. }
  226. fd = socket(PF_LOCAL, SOCK_STREAM, 0);
  227. memset(&s, 0, sizeof(s));
  228. s.sun_family = AF_UNIX;
  229. bytes = sizeof(s.sun_path) - 1;
  230. snprintf(s.sun_path, bytes, CHANSRV_PORT_STR, u->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. if (bytes > u->skip_bytes) {
  248. bytes -= u->skip_bytes;
  249. u->skip_bytes = 0;
  250. } else {
  251. u->skip_bytes -= bytes;
  252. return bytes;
  253. }
  254. }
  255. h.code = 0;
  256. h.bytes = bytes + 8;
  257. if (send(u->fd, &h, 8, 0) != 8) {
  258. pa_log("data_send: send failed");
  259. close(u->fd);
  260. u->fd = 0;
  261. return 0;
  262. } else {
  263. //pa_log("data_send: sent header ok bytes %d", bytes);
  264. }
  265. data = (char*)pa_memblock_acquire(chunk->memblock);
  266. data += chunk->index;
  267. sent = send(u->fd, data, bytes, 0);
  268. pa_memblock_release(chunk->memblock);
  269. if (sent != bytes) {
  270. pa_log("data_send: send failed sent %d bytes %d", sent, bytes);
  271. close(u->fd);
  272. u->fd = 0;
  273. return 0;
  274. }
  275. return sent;
  276. }
  277. static int close_send(struct userdata *u) {
  278. struct header h;
  279. pa_log("close_send:");
  280. if (u->fd == 0) {
  281. return 0;
  282. }
  283. h.code = 1;
  284. h.bytes = 8;
  285. if (send(u->fd, &h, 8, 0) != 8) {
  286. pa_log("close_send: send failed");
  287. close(u->fd);
  288. u->fd = 0;
  289. return 0;
  290. } else {
  291. //pa_log("close_send: sent header ok");
  292. }
  293. return 8;
  294. }
  295. static void process_render(struct userdata *u, pa_usec_t now) {
  296. pa_memchunk chunk;
  297. int request_bytes;
  298. //int index;
  299. pa_assert(u);
  300. if (u->got_max_latency) {
  301. return;
  302. }
  303. //index = 0;
  304. while (u->timestamp < now + u->block_usec) {
  305. //index++;
  306. //if (index > 3) {
  307. /* used when u->block_usec and
  308. u->sink->thread_info.max_request get big
  309. using got_max_latency now */
  310. // return;
  311. //}
  312. request_bytes = u->sink->thread_info.max_request;
  313. request_bytes = MIN(request_bytes, 16 * 1024);
  314. pa_sink_render(u->sink, request_bytes, &chunk);
  315. //pa_log("bytes %d index %d", chunk.length, index);
  316. data_send(u, &chunk);
  317. pa_memblock_unref(chunk.memblock);
  318. u->timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
  319. }
  320. }
  321. static void thread_func(void *userdata) {
  322. struct userdata *u = userdata;
  323. int ret;
  324. pa_usec_t now;
  325. pa_assert(u);
  326. pa_log_debug("Thread starting up");
  327. pa_thread_mq_install(&u->thread_mq);
  328. u->timestamp = pa_rtclock_now();
  329. for (;;) {
  330. if (u->sink->thread_info.state == PA_SINK_RUNNING) {
  331. now = pa_rtclock_now();
  332. if (u->sink->thread_info.rewind_requested) {
  333. if (u->sink->thread_info.rewind_nbytes > 0) {
  334. process_rewind(u, now);
  335. } else {
  336. pa_sink_process_rewind(u->sink, 0);
  337. }
  338. }
  339. if (u->timestamp <= now) {
  340. process_render(u, now);
  341. }
  342. pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
  343. } else {
  344. pa_rtpoll_set_timer_disabled(u->rtpoll);
  345. }
  346. if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) {
  347. goto fail;
  348. }
  349. if (ret == 0) {
  350. goto finish;
  351. }
  352. }
  353. fail:
  354. /* If this was no regular exit from the loop we have to continue
  355. * processing messages until we received PA_MESSAGE_SHUTDOWN */
  356. pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core),
  357. PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0,
  358. NULL, NULL);
  359. pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
  360. finish:
  361. pa_log_debug("Thread shutting down");
  362. }
  363. int pa__init(pa_module*m) {
  364. struct userdata *u = NULL;
  365. pa_sample_spec ss;
  366. pa_channel_map map;
  367. pa_modargs *ma = NULL;
  368. pa_sink_new_data data;
  369. size_t nbytes;
  370. pa_assert(m);
  371. if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
  372. pa_log("Failed to parse module arguments.");
  373. goto fail;
  374. }
  375. ss = m->core->default_sample_spec;
  376. map = m->core->default_channel_map;
  377. if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map,
  378. PA_CHANNEL_MAP_DEFAULT) < 0) {
  379. pa_log("Invalid sample format specification or channel map");
  380. goto fail;
  381. }
  382. m->userdata = u = pa_xnew0(struct userdata, 1);
  383. u->core = m->core;
  384. u->module = m;
  385. u->rtpoll = pa_rtpoll_new();
  386. pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
  387. pa_sink_new_data_init(&data);
  388. data.driver = __FILE__;
  389. data.module = m;
  390. pa_sink_new_data_set_name(&data,
  391. pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
  392. pa_sink_new_data_set_sample_spec(&data, &ss);
  393. pa_sink_new_data_set_channel_map(&data, &map);
  394. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "xrdp sink");
  395. pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
  396. if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist,
  397. PA_UPDATE_REPLACE) < 0) {
  398. pa_log("Invalid properties");
  399. pa_sink_new_data_done(&data);
  400. goto fail;
  401. }
  402. u->sink = pa_sink_new(m->core, &data,
  403. PA_SINK_LATENCY | PA_SINK_DYNAMIC_LATENCY);
  404. pa_sink_new_data_done(&data);
  405. if (!u->sink) {
  406. pa_log("Failed to create sink object.");
  407. goto fail;
  408. }
  409. u->sink->parent.process_msg = sink_process_msg;
  410. u->sink->update_requested_latency = sink_update_requested_latency_cb;
  411. u->sink->userdata = u;
  412. pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
  413. pa_sink_set_rtpoll(u->sink, u->rtpoll);
  414. u->block_usec = 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. }