fifo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright (C) 2007, Gilles Casse <gcasse@oralux.org>
  3. * Copyright (C) 2013-2016 Reece H. Dunn
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  17. */
  18. // This source file is only used for asynchronious modes
  19. #include "config.h"
  20. #include <assert.h>
  21. #include <errno.h>
  22. #include <pthread.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/time.h>
  28. #include <time.h>
  29. #include <unistd.h>
  30. #include "espeak_ng.h"
  31. #include "speech.h"
  32. #include "espeak_command.h"
  33. #include "fifo.h"
  34. #ifdef USE_ASYNC
  35. // my_mutex: protects my_thread_is_talking,
  36. // my_stop_is_required, and the command fifo
  37. static pthread_mutex_t my_mutex;
  38. static bool my_command_is_running = false;
  39. static pthread_cond_t my_cond_command_is_running;
  40. static bool my_stop_is_required = false;
  41. // my_thread: reads commands from the fifo, and runs them.
  42. static pthread_t my_thread;
  43. static pthread_cond_t my_cond_start_is_required;
  44. static bool my_start_is_required = false;
  45. static pthread_cond_t my_cond_stop_is_acknowledged;
  46. static bool my_stop_is_acknowledged = false;
  47. static void *say_thread(void *);
  48. static espeak_ng_STATUS push(t_espeak_command *the_command);
  49. static t_espeak_command *pop(void);
  50. static void init(int process_parameters);
  51. static int node_counter = 0;
  52. enum {
  53. MAX_NODE_COUNTER = 400,
  54. INACTIVITY_TIMEOUT = 50, // in ms, check that the stream is inactive
  55. MAX_INACTIVITY_CHECK = 2
  56. };
  57. void fifo_init()
  58. {
  59. // security
  60. pthread_mutex_init(&my_mutex, (const pthread_mutexattr_t *)NULL);
  61. init(0);
  62. assert(-1 != pthread_cond_init(&my_cond_command_is_running, NULL));
  63. assert(-1 != pthread_cond_init(&my_cond_start_is_required, NULL));
  64. assert(-1 != pthread_cond_init(&my_cond_stop_is_acknowledged, NULL));
  65. pthread_attr_t a_attrib;
  66. if (pthread_attr_init(&a_attrib)
  67. || pthread_attr_setdetachstate(&a_attrib, PTHREAD_CREATE_JOINABLE)
  68. || pthread_create(&my_thread,
  69. &a_attrib,
  70. say_thread,
  71. (void *)NULL)) {
  72. assert(0);
  73. }
  74. pthread_attr_destroy(&a_attrib);
  75. // leave once the thread is actually started
  76. assert(-1 != pthread_mutex_lock(&my_mutex));
  77. while (my_stop_is_acknowledged == false) {
  78. while ((pthread_cond_wait(&my_cond_stop_is_acknowledged, &my_mutex) == -1) && errno == EINTR)
  79. ;
  80. }
  81. my_stop_is_acknowledged = false;
  82. pthread_mutex_unlock(&my_mutex);
  83. }
  84. espeak_ng_STATUS fifo_add_command(t_espeak_command *the_command)
  85. {
  86. espeak_ng_STATUS status;
  87. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  88. return status;
  89. if ((status = push(the_command)) != ENS_OK) {
  90. pthread_mutex_unlock(&my_mutex);
  91. return status;
  92. }
  93. my_start_is_required = true;
  94. pthread_cond_signal(&my_cond_start_is_required);
  95. while (my_start_is_required && !my_command_is_running) {
  96. if((status = pthread_cond_wait(&my_cond_command_is_running, &my_mutex)) != ENS_OK && errno != EINTR) {
  97. pthread_mutex_unlock(&my_mutex);
  98. return status;
  99. }
  100. }
  101. if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK)
  102. return status;
  103. return ENS_OK;
  104. }
  105. espeak_ng_STATUS fifo_add_commands(t_espeak_command *command1, t_espeak_command *command2)
  106. {
  107. espeak_ng_STATUS status;
  108. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  109. return status;
  110. if (node_counter+1 >= MAX_NODE_COUNTER) {
  111. pthread_mutex_unlock(&my_mutex);
  112. return ENS_FIFO_BUFFER_FULL;
  113. }
  114. if ((status = push(command1)) != ENS_OK) {
  115. pthread_mutex_unlock(&my_mutex);
  116. return status;
  117. }
  118. if ((status = push(command2)) != ENS_OK) {
  119. pthread_mutex_unlock(&my_mutex);
  120. return status;
  121. }
  122. my_start_is_required = true;
  123. pthread_cond_signal(&my_cond_start_is_required);
  124. while (my_start_is_required && !my_command_is_running) {
  125. if((status = pthread_cond_wait(&my_cond_command_is_running, &my_mutex)) != ENS_OK && errno != EINTR) {
  126. pthread_mutex_unlock(&my_mutex);
  127. return status;
  128. }
  129. }
  130. if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK)
  131. return status;
  132. return ENS_OK;
  133. }
  134. espeak_ng_STATUS fifo_stop()
  135. {
  136. espeak_ng_STATUS status;
  137. if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  138. return status;
  139. bool a_command_is_running = false;
  140. if (my_command_is_running) {
  141. a_command_is_running = true;
  142. my_stop_is_required = true;
  143. my_stop_is_acknowledged = false;
  144. }
  145. if (a_command_is_running) {
  146. while (my_stop_is_acknowledged == false) {
  147. while ((pthread_cond_wait(&my_cond_stop_is_acknowledged, &my_mutex) == -1) && errno == EINTR)
  148. continue; // Restart when interrupted by handler
  149. }
  150. }
  151. my_stop_is_required = false;
  152. if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK)
  153. return status;
  154. return ENS_OK;
  155. }
  156. int fifo_is_busy()
  157. {
  158. return my_command_is_running;
  159. }
  160. static int sleep_until_start_request_or_inactivity()
  161. {
  162. int a_start_is_required = false;
  163. // Wait for the start request (my_cond_start_is_required).
  164. // Besides this, if the audio stream is still busy,
  165. // check from time to time its end.
  166. // The end of the stream is confirmed by several checks
  167. // for filtering underflow.
  168. //
  169. int i = 0;
  170. int err = pthread_mutex_lock(&my_mutex);
  171. assert(err != -1);
  172. while ((i <= MAX_INACTIVITY_CHECK) && !a_start_is_required) {
  173. i++;
  174. struct timespec ts;
  175. struct timeval tv;
  176. clock_gettime2(&ts);
  177. add_time_in_ms(&ts, INACTIVITY_TIMEOUT);
  178. while ((err = pthread_cond_timedwait(&my_cond_start_is_required, &my_mutex, &ts)) == -1
  179. && errno == EINTR)
  180. continue;
  181. assert(gettimeofday(&tv, NULL) != -1);
  182. if (err == 0)
  183. a_start_is_required = true;
  184. }
  185. pthread_mutex_unlock(&my_mutex);
  186. return a_start_is_required;
  187. }
  188. static espeak_ng_STATUS close_stream()
  189. {
  190. espeak_ng_STATUS status = pthread_mutex_lock(&my_mutex);
  191. if (status != ENS_OK)
  192. return status;
  193. bool a_stop_is_required = my_stop_is_required;
  194. if (!a_stop_is_required)
  195. my_command_is_running = true;
  196. status = pthread_mutex_unlock(&my_mutex);
  197. if (!a_stop_is_required) {
  198. int a_status = pthread_mutex_lock(&my_mutex);
  199. if (status == ENS_OK)
  200. status = a_status;
  201. my_command_is_running = false;
  202. a_stop_is_required = my_stop_is_required;
  203. a_status = pthread_mutex_unlock(&my_mutex);
  204. if (status == ENS_OK)
  205. status = a_status;
  206. if (a_stop_is_required) {
  207. // cancel the audio early, to be more responsive when using eSpeak NG
  208. // for audio.
  209. cancel_audio();
  210. // acknowledge the stop request
  211. if((a_status = pthread_mutex_lock(&my_mutex)) != ENS_OK)
  212. return a_status;
  213. my_stop_is_acknowledged = true;
  214. a_status = pthread_cond_signal(&my_cond_stop_is_acknowledged);
  215. if(a_status != ENS_OK)
  216. return a_status;
  217. a_status = pthread_mutex_unlock(&my_mutex);
  218. if (status == ENS_OK)
  219. status = a_status;
  220. }
  221. }
  222. return status;
  223. }
  224. static void *say_thread(void *p)
  225. {
  226. (void)p; // unused
  227. // announce that thread is started
  228. assert(-1 != pthread_mutex_lock(&my_mutex));
  229. my_stop_is_acknowledged = true;
  230. assert(-1 != pthread_cond_signal(&my_cond_stop_is_acknowledged));
  231. assert(-1 != pthread_mutex_unlock(&my_mutex));
  232. bool look_for_inactivity = false;
  233. while (1) {
  234. bool a_start_is_required = false;
  235. if (look_for_inactivity) {
  236. a_start_is_required = sleep_until_start_request_or_inactivity();
  237. if (!a_start_is_required)
  238. close_stream();
  239. }
  240. look_for_inactivity = true;
  241. int a_status = pthread_mutex_lock(&my_mutex);
  242. assert(!a_status);
  243. if (!a_start_is_required) {
  244. while (my_start_is_required == false) {
  245. while ((pthread_cond_wait(&my_cond_start_is_required, &my_mutex) == -1) && errno == EINTR)
  246. continue; // Restart when interrupted by handler
  247. }
  248. }
  249. my_command_is_running = true;
  250. assert(-1 != pthread_cond_broadcast(&my_cond_command_is_running));
  251. assert(-1 != pthread_mutex_unlock(&my_mutex));
  252. while (my_command_is_running) {
  253. int a_status = pthread_mutex_lock(&my_mutex);
  254. assert(!a_status);
  255. t_espeak_command *a_command = (t_espeak_command *)pop();
  256. if (a_command == NULL) {
  257. my_command_is_running = false;
  258. a_status = pthread_mutex_unlock(&my_mutex);
  259. } else {
  260. my_start_is_required = false;
  261. if (my_stop_is_required)
  262. my_command_is_running = false;
  263. a_status = pthread_mutex_unlock(&my_mutex);
  264. if (my_command_is_running)
  265. process_espeak_command(a_command);
  266. delete_espeak_command(a_command);
  267. }
  268. }
  269. if (my_stop_is_required) {
  270. // no mutex required since the stop command is synchronous
  271. // and waiting for my_cond_stop_is_acknowledged
  272. init(1);
  273. assert(-1 != pthread_mutex_lock(&my_mutex));
  274. my_start_is_required = false;
  275. // acknowledge the stop request
  276. my_stop_is_acknowledged = true;
  277. int a_status = pthread_cond_signal(&my_cond_stop_is_acknowledged);
  278. assert(a_status != -1);
  279. pthread_mutex_unlock(&my_mutex);
  280. }
  281. // and wait for the next start
  282. }
  283. return NULL;
  284. }
  285. int fifo_is_command_enabled()
  286. {
  287. return 0 == my_stop_is_required;
  288. }
  289. typedef struct t_node {
  290. t_espeak_command *data;
  291. struct t_node *next;
  292. } node;
  293. static node *head = NULL;
  294. static node *tail = NULL;
  295. static espeak_ng_STATUS push(t_espeak_command *the_command)
  296. {
  297. assert((!head && !tail) || (head && tail));
  298. if (the_command == NULL)
  299. return EINVAL;
  300. if (node_counter >= MAX_NODE_COUNTER)
  301. return ENS_FIFO_BUFFER_FULL;
  302. node *n = (node *)malloc(sizeof(node));
  303. if (n == NULL)
  304. return ENOMEM;
  305. if (head == NULL) {
  306. head = n;
  307. tail = n;
  308. } else {
  309. tail->next = n;
  310. tail = n;
  311. }
  312. tail->next = NULL;
  313. tail->data = the_command;
  314. node_counter++;
  315. the_command->state = CS_PENDING;
  316. return ENS_OK;
  317. }
  318. static t_espeak_command *pop()
  319. {
  320. t_espeak_command *the_command = NULL;
  321. assert((!head && !tail) || (head && tail));
  322. if (head != NULL) {
  323. node *n = head;
  324. the_command = n->data;
  325. head = n->next;
  326. free(n);
  327. node_counter--;
  328. }
  329. if (head == NULL)
  330. tail = NULL;
  331. return the_command;
  332. }
  333. static void init(int process_parameters)
  334. {
  335. t_espeak_command *c = NULL;
  336. c = pop();
  337. while (c != NULL) {
  338. if (process_parameters && (c->type == ET_PARAMETER || c->type == ET_VOICE_NAME || c->type == ET_VOICE_SPEC))
  339. process_espeak_command(c);
  340. delete_espeak_command(c);
  341. c = pop();
  342. }
  343. node_counter = 0;
  344. }
  345. void fifo_terminate()
  346. {
  347. pthread_cancel(my_thread);
  348. pthread_join(my_thread, NULL);
  349. pthread_mutex_destroy(&my_mutex);
  350. pthread_cond_destroy(&my_cond_start_is_required);
  351. pthread_cond_destroy(&my_cond_stop_is_acknowledged);
  352. init(0); // purge fifo
  353. }
  354. #endif