| 1 | /* |
| 2 | * Copyright (C) 2007, Gilles Casse <[email protected]> |
| 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 | |
| 19 | // This source file is only used for asynchronous modes |
| 20 | |
| 21 | #include "espeak__config.h" |
| 22 | |
| 23 | #include <assert.h> |
| 24 | #include <errno.h> |
| 25 | #include <pthread.h> |
| 26 | #include <stdbool.h> |
| 27 | #include <stdint.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/time.h> |
| 31 | #include <time.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #include "espeak_ng.h" |
| 35 | |
| 36 | #include "speech.h" |
| 37 | #include "espeak_command.h" |
| 38 | #include "fifo.h" |
| 39 | #include "event.h" |
| 40 | |
| 41 | #if USE_ASYNC |
| 42 | |
| 43 | // my_mutex: protects my_thread_is_talking, |
| 44 | // my_stop_is_required, and the command fifo |
| 45 | static pthread_mutex_t my_mutex; |
| 46 | static bool my_command_is_running = false; |
| 47 | static pthread_cond_t my_cond_command_is_running; |
| 48 | static bool my_stop_is_required = false; |
| 49 | static bool my_terminate_is_required = 0; |
| 50 | |
| 51 | // my_thread: reads commands from the fifo, and runs them. |
| 52 | static pthread_t my_thread; |
| 53 | |
| 54 | static pthread_cond_t my_cond_start_is_required; |
| 55 | static bool my_start_is_required = false; |
| 56 | |
| 57 | static pthread_cond_t my_cond_stop_is_acknowledged; |
| 58 | static bool my_stop_is_acknowledged = false; |
| 59 | |
| 60 | static void *say_thread(void *); |
| 61 | |
| 62 | static espeak_ng_STATUS push(t_espeak_command *the_command); |
| 63 | static t_espeak_command *pop(void); |
| 64 | static void init(int process_parameters); |
| 65 | static int node_counter = 0; |
| 66 | static bool thread_inited = false; |
| 67 | |
| 68 | enum { |
| 69 | MAX_NODE_COUNTER = 400, |
| 70 | INACTIVITY_TIMEOUT = 50, // in ms, check that the stream is inactive |
| 71 | MAX_INACTIVITY_CHECK = 2 |
| 72 | }; |
| 73 | |
| 74 | void fifo_init(void) |
| 75 | { |
| 76 | // security |
| 77 | pthread_mutex_init(&my_mutex, (const pthread_mutexattr_t *)NULL); |
| 78 | init(0); |
| 79 | |
| 80 | int a_status; |
| 81 | a_status = pthread_cond_init(&my_cond_command_is_running, NULL); |
| 82 | assert(-1 != a_status); |
| 83 | a_status = pthread_cond_init(&my_cond_start_is_required, NULL); |
| 84 | assert(-1 != a_status); |
| 85 | a_status = pthread_cond_init(&my_cond_stop_is_acknowledged, NULL); |
| 86 | assert(-1 != a_status); |
| 87 | |
| 88 | pthread_attr_t a_attrib; |
| 89 | if (pthread_attr_init(&a_attrib) |
| 90 | || pthread_attr_setdetachstate(&a_attrib, PTHREAD_CREATE_JOINABLE) |
| 91 | || pthread_create(&my_thread, |
| 92 | &a_attrib, |
| 93 | say_thread, |
| 94 | (void *)NULL)) { |
| 95 | assert(0); |
| 96 | } |
| 97 | thread_inited = true; |
| 98 | |
| 99 | pthread_attr_destroy(&a_attrib); |
| 100 | |
| 101 | // leave once the thread is actually started |
| 102 | a_status = pthread_mutex_lock(&my_mutex); |
| 103 | assert(-1 != a_status); |
| 104 | (void)a_status; |
| 105 | while (my_stop_is_acknowledged == false) { |
| 106 | while ((pthread_cond_wait(&my_cond_stop_is_acknowledged, &my_mutex) == -1) && errno == EINTR) |
| 107 | ; |
| 108 | } |
| 109 | my_stop_is_acknowledged = false; |
| 110 | pthread_mutex_unlock(&my_mutex); |
| 111 | } |
| 112 | |
| 113 | espeak_ng_STATUS fifo_add_command(t_espeak_command *the_command) |
| 114 | { |
| 115 | espeak_ng_STATUS status; |
| 116 | if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK) |
| 117 | return status; |
| 118 | |
| 119 | if ((status = push(the_command)) != ENS_OK) { |
| 120 | pthread_mutex_unlock(&my_mutex); |
| 121 | return status; |
| 122 | } |
| 123 | |
| 124 | my_start_is_required = true; |
| 125 | pthread_cond_signal(&my_cond_start_is_required); |
| 126 | |
| 127 | while (my_start_is_required && !my_command_is_running) { |
| 128 | if((status = pthread_cond_wait(&my_cond_command_is_running, &my_mutex)) != ENS_OK && errno != EINTR) { |
| 129 | pthread_mutex_unlock(&my_mutex); |
| 130 | return status; |
| 131 | } |
| 132 | } |
| 133 | if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK) |
| 134 | return status; |
| 135 | |
| 136 | return ENS_OK; |
| 137 | } |
| 138 | |
| 139 | espeak_ng_STATUS fifo_add_commands(t_espeak_command *command1, t_espeak_command *command2) |
| 140 | { |
| 141 | espeak_ng_STATUS status; |
| 142 | if (!thread_inited) { |
| 143 | return ENS_NOT_INITIALIZED; |
| 144 | } |
| 145 | if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK) |
| 146 | return status; |
| 147 | |
| 148 | if (node_counter+1 >= MAX_NODE_COUNTER) { |
| 149 | pthread_mutex_unlock(&my_mutex); |
| 150 | return ENS_FIFO_BUFFER_FULL; |
| 151 | } |
| 152 | |
| 153 | if ((status = push(command1)) != ENS_OK) { |
| 154 | pthread_mutex_unlock(&my_mutex); |
| 155 | return status; |
| 156 | } |
| 157 | |
| 158 | if ((status = push(command2)) != ENS_OK) { |
| 159 | pthread_mutex_unlock(&my_mutex); |
| 160 | return status; |
| 161 | } |
| 162 | |
| 163 | my_start_is_required = true; |
| 164 | pthread_cond_signal(&my_cond_start_is_required); |
| 165 | |
| 166 | while (my_start_is_required && !my_command_is_running) { |
| 167 | if((status = pthread_cond_wait(&my_cond_command_is_running, &my_mutex)) != ENS_OK && errno != EINTR) { |
| 168 | pthread_mutex_unlock(&my_mutex); |
| 169 | return status; |
| 170 | } |
| 171 | } |
| 172 | if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK) |
| 173 | return status; |
| 174 | |
| 175 | return ENS_OK; |
| 176 | } |
| 177 | |
| 178 | espeak_ng_STATUS fifo_stop(void) |
| 179 | { |
| 180 | if (!thread_inited) return ENS_OK; |
| 181 | espeak_ng_STATUS status; |
| 182 | if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK) |
| 183 | return status; |
| 184 | |
| 185 | bool a_command_is_running = false; |
| 186 | if (my_command_is_running) { |
| 187 | a_command_is_running = true; |
| 188 | my_stop_is_required = true; |
| 189 | my_stop_is_acknowledged = false; |
| 190 | } |
| 191 | |
| 192 | if (a_command_is_running) { |
| 193 | while (my_stop_is_acknowledged == false) { |
| 194 | while ((pthread_cond_wait(&my_cond_stop_is_acknowledged, &my_mutex) == -1) && errno == EINTR) |
| 195 | continue; // Restart when interrupted by handler |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | my_stop_is_required = false; |
| 200 | if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK) |
| 201 | return status; |
| 202 | |
| 203 | return ENS_OK; |
| 204 | } |
| 205 | |
| 206 | int fifo_is_busy(void) |
| 207 | { |
| 208 | if (!thread_inited) return false; |
| 209 | pthread_mutex_lock(&my_mutex); |
| 210 | bool running = my_command_is_running; |
| 211 | pthread_mutex_unlock(&my_mutex); |
| 212 | return running; |
| 213 | } |
| 214 | |
| 215 | static int sleep_until_start_request_or_inactivity(void) |
| 216 | { |
| 217 | int a_start_is_required = false; |
| 218 | |
| 219 | // Wait for the start request (my_cond_start_is_required). |
| 220 | // Besides this, if the audio stream is still busy, |
| 221 | // check from time to time its end. |
| 222 | // The end of the stream is confirmed by several checks |
| 223 | // for filtering underflow. |
| 224 | // |
| 225 | int i = 0; |
| 226 | int err = pthread_mutex_lock(&my_mutex); |
| 227 | assert(err != -1); |
| 228 | while ((i <= MAX_INACTIVITY_CHECK) && !a_start_is_required) { |
| 229 | i++; |
| 230 | |
| 231 | struct timespec ts; |
| 232 | |
| 233 | clock_gettime2(&ts); |
| 234 | |
| 235 | add_time_in_ms(&ts, INACTIVITY_TIMEOUT); |
| 236 | |
| 237 | while ((err = pthread_cond_timedwait(&my_cond_start_is_required, &my_mutex, &ts)) == -1 |
| 238 | && errno == EINTR) |
| 239 | continue; |
| 240 | |
| 241 | if (err == 0) |
| 242 | a_start_is_required = true; |
| 243 | } |
| 244 | pthread_mutex_unlock(&my_mutex); |
| 245 | return a_start_is_required; |
| 246 | } |
| 247 | |
| 248 | static espeak_ng_STATUS close_stream(void) |
| 249 | { |
| 250 | espeak_ng_STATUS status = pthread_mutex_lock(&my_mutex); |
| 251 | if (status != ENS_OK) |
| 252 | return status; |
| 253 | |
| 254 | bool a_stop_is_required = my_stop_is_required; |
| 255 | if (!a_stop_is_required) |
| 256 | my_command_is_running = true; |
| 257 | |
| 258 | status = pthread_mutex_unlock(&my_mutex); |
| 259 | |
| 260 | if (!a_stop_is_required) { |
| 261 | int a_status = pthread_mutex_lock(&my_mutex); |
| 262 | if (status == ENS_OK) |
| 263 | status = a_status; |
| 264 | |
| 265 | my_command_is_running = false; |
| 266 | a_stop_is_required = my_stop_is_required; |
| 267 | |
| 268 | a_status = pthread_mutex_unlock(&my_mutex); |
| 269 | if (status == ENS_OK) |
| 270 | status = a_status; |
| 271 | |
| 272 | if (a_stop_is_required) { |
| 273 | // cancel the audio early, to be more responsive when using eSpeak NG |
| 274 | // for audio. |
| 275 | cancel_audio(); |
| 276 | |
| 277 | // acknowledge the stop request |
| 278 | if((a_status = pthread_mutex_lock(&my_mutex)) != ENS_OK) |
| 279 | return a_status; |
| 280 | |
| 281 | my_stop_is_acknowledged = true; |
| 282 | a_status = pthread_cond_signal(&my_cond_stop_is_acknowledged); |
| 283 | if(a_status != ENS_OK) |
| 284 | return a_status; |
| 285 | a_status = pthread_mutex_unlock(&my_mutex); |
| 286 | if (status == ENS_OK) |
| 287 | status = a_status; |
| 288 | |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return status; |
| 293 | } |
| 294 | |
| 295 | static void *say_thread(void *p) |
| 296 | { |
| 297 | (void)p; // unused |
| 298 | |
| 299 | int a_status; |
| 300 | |
| 301 | // announce that thread is started |
| 302 | a_status = pthread_mutex_lock(&my_mutex); |
| 303 | assert(-1 != a_status); |
| 304 | my_stop_is_acknowledged = true; |
| 305 | a_status = pthread_cond_signal(&my_cond_stop_is_acknowledged); |
| 306 | assert(-1 != a_status); |
| 307 | a_status = pthread_mutex_unlock(&my_mutex); |
| 308 | assert(-1 != a_status); |
| 309 | |
| 310 | bool look_for_inactivity = false; |
| 311 | |
| 312 | while (!my_terminate_is_required) { |
| 313 | bool a_start_is_required = false; |
| 314 | if (look_for_inactivity) { |
| 315 | a_start_is_required = sleep_until_start_request_or_inactivity(); |
| 316 | if (!a_start_is_required) |
| 317 | close_stream(); |
| 318 | } |
| 319 | look_for_inactivity = true; |
| 320 | |
| 321 | a_status = pthread_mutex_lock(&my_mutex); |
| 322 | assert(!a_status); |
| 323 | |
| 324 | if (!a_start_is_required) { |
| 325 | while (my_start_is_required == false && my_terminate_is_required == false) { |
| 326 | while ((pthread_cond_wait(&my_cond_start_is_required, &my_mutex) == -1) && errno == EINTR) |
| 327 | continue; // Restart when interrupted by handler |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | |
| 332 | my_command_is_running = true; |
| 333 | |
| 334 | a_status = pthread_cond_broadcast(&my_cond_command_is_running); |
| 335 | assert(-1 != a_status); |
| 336 | a_status = pthread_mutex_unlock(&my_mutex); |
| 337 | assert(-1 != a_status); |
| 338 | |
| 339 | while (my_command_is_running && !my_terminate_is_required) { |
| 340 | a_status = pthread_mutex_lock(&my_mutex); |
| 341 | assert(!a_status); |
| 342 | t_espeak_command *a_command = (t_espeak_command *)pop(); |
| 343 | |
| 344 | if (a_command == NULL) { |
| 345 | my_command_is_running = false; |
| 346 | a_status = pthread_mutex_unlock(&my_mutex); |
| 347 | } else { |
| 348 | my_start_is_required = false; |
| 349 | |
| 350 | if (my_stop_is_required) |
| 351 | my_command_is_running = false; |
| 352 | a_status = pthread_mutex_unlock(&my_mutex); |
| 353 | |
| 354 | if (my_command_is_running) |
| 355 | process_espeak_command(a_command); |
| 356 | delete_espeak_command(a_command); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if (my_stop_is_required || my_terminate_is_required) { |
| 361 | // no mutex required since the stop command is synchronous |
| 362 | // and waiting for my_cond_stop_is_acknowledged |
| 363 | init(1); |
| 364 | |
| 365 | a_status = pthread_mutex_lock(&my_mutex); |
| 366 | assert(-1 != a_status); |
| 367 | my_start_is_required = false; |
| 368 | |
| 369 | // acknowledge the stop request |
| 370 | my_stop_is_acknowledged = true; |
| 371 | a_status = pthread_cond_signal(&my_cond_stop_is_acknowledged); |
| 372 | assert(a_status != -1); |
| 373 | pthread_mutex_unlock(&my_mutex); |
| 374 | |
| 375 | } |
| 376 | // and wait for the next start |
| 377 | } |
| 378 | (void)a_status; |
| 379 | |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | int fifo_is_command_enabled(void) |
| 384 | { |
| 385 | return 0 == my_stop_is_required; |
| 386 | } |
| 387 | |
| 388 | typedef struct t_node { |
| 389 | t_espeak_command *data; |
| 390 | struct t_node *next; |
| 391 | } node; |
| 392 | |
| 393 | static node *head = NULL; |
| 394 | static node *tail = NULL; |
| 395 | |
| 396 | static espeak_ng_STATUS push(t_espeak_command *the_command) |
| 397 | { |
| 398 | assert((!head && !tail) || (head && tail)); |
| 399 | |
| 400 | if (the_command == NULL) |
| 401 | return EINVAL; |
| 402 | |
| 403 | if (node_counter >= MAX_NODE_COUNTER) |
| 404 | return ENS_FIFO_BUFFER_FULL; |
| 405 | |
| 406 | node *n = (node *)malloc(sizeof(node)); |
| 407 | if (n == NULL) |
| 408 | return static_cast<espeak_ng_STATUS> (ENOMEM); |
| 409 | |
| 410 | if (head == NULL) { |
| 411 | head = n; |
| 412 | tail = n; |
| 413 | } else { |
| 414 | tail->next = n; |
| 415 | tail = n; |
| 416 | } |
| 417 | |
| 418 | tail->next = NULL; |
| 419 | tail->data = the_command; |
| 420 | |
| 421 | node_counter++; |
| 422 | |
| 423 | the_command->state = CS_PENDING; |
| 424 | |
| 425 | return ENS_OK; |
| 426 | } |
| 427 | |
| 428 | static t_espeak_command *pop(void) |
| 429 | { |
| 430 | t_espeak_command *the_command = NULL; |
| 431 | |
| 432 | assert((!head && !tail) || (head && tail)); |
| 433 | |
| 434 | if (head != NULL) { |
| 435 | node *n = head; |
| 436 | the_command = n->data; |
| 437 | head = n->next; |
| 438 | free(n); |
| 439 | node_counter--; |
| 440 | } |
| 441 | |
| 442 | if (head == NULL) |
| 443 | tail = NULL; |
| 444 | |
| 445 | return the_command; |
| 446 | } |
| 447 | |
| 448 | static void init(int process_parameters) |
| 449 | { |
| 450 | t_espeak_command *c = NULL; |
| 451 | c = pop(); |
| 452 | while (c != NULL) { |
| 453 | if (process_parameters && (c->type == ET_PARAMETER || c->type == ET_VOICE_NAME || c->type == ET_VOICE_SPEC)) |
| 454 | process_espeak_command(c); |
| 455 | delete_espeak_command(c); |
| 456 | c = pop(); |
| 457 | } |
| 458 | node_counter = 0; |
| 459 | } |
| 460 | |
| 461 | void fifo_terminate(void) |
| 462 | { |
| 463 | if (!thread_inited) return; |
| 464 | |
| 465 | pthread_mutex_lock(&my_mutex); |
| 466 | my_terminate_is_required = true; |
| 467 | pthread_mutex_unlock(&my_mutex); |
| 468 | pthread_cond_signal(&my_cond_start_is_required); |
| 469 | pthread_join(my_thread, NULL); |
| 470 | my_terminate_is_required = false; |
| 471 | thread_inited = false; |
| 472 | |
| 473 | pthread_mutex_destroy(&my_mutex); |
| 474 | pthread_cond_destroy(&my_cond_start_is_required); |
| 475 | pthread_cond_destroy(&my_cond_stop_is_acknowledged); |
| 476 | |
| 477 | init(0); // purge fifo |
| 478 | } |
| 479 | |
| 480 | #endif |