| 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 "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 <unistd.h> |
| 32 | |
| 33 | #include "espeak_ng.h" |
| 34 | #include "speak_lib.h" |
| 35 | |
| 36 | #include "event.h" |
| 37 | |
| 38 | // my_mutex: protects my_thread_is_talking, |
| 39 | static pthread_mutex_t my_mutex; |
| 40 | static pthread_cond_t my_cond_start_is_required; |
| 41 | static bool my_start_is_required = false; |
| 42 | static pthread_cond_t my_cond_stop_is_required; |
| 43 | static bool my_stop_is_required = false; |
| 44 | static pthread_cond_t my_cond_stop_is_acknowledged; |
| 45 | static bool my_stop_is_acknowledged = false; |
| 46 | static bool my_terminate_is_required = 0; |
| 47 | // my_thread: polls the audio duration and compares it to the duration of the first event. |
| 48 | static pthread_t my_thread; |
| 49 | static bool thread_inited = false; |
| 50 | |
| 51 | static t_espeak_callback *my_callback = NULL; |
| 52 | static bool my_event_is_running = false; |
| 53 | |
| 54 | enum { |
| 55 | MIN_TIMEOUT_IN_MS = 10, |
| 56 | ACTIVITY_TIMEOUT = 50, // in ms, check that the stream is active |
| 57 | MAX_ACTIVITY_CHECK = 6 |
| 58 | }; |
| 59 | |
| 60 | typedef struct t_node { |
| 61 | void *data; |
| 62 | struct t_node *next; |
| 63 | } node; |
| 64 | |
| 65 | static node *head = NULL; |
| 66 | static node *tail = NULL; |
| 67 | static int node_counter = 0; |
| 68 | static espeak_ng_STATUS push(void *data); |
| 69 | static void *pop(void); |
| 70 | static void init(void); |
| 71 | static void *polling_thread(void *); |
| 72 | |
| 73 | void event_set_callback(t_espeak_callback *SynthCallback) |
| 74 | { |
| 75 | my_callback = SynthCallback; |
| 76 | } |
| 77 | |
| 78 | void event_init(void) |
| 79 | { |
| 80 | my_event_is_running = false; |
| 81 | |
| 82 | // security |
| 83 | pthread_mutex_init(&my_mutex, (const pthread_mutexattr_t *)NULL); |
| 84 | init(); |
| 85 | |
| 86 | int a_status; |
| 87 | |
| 88 | a_status = pthread_cond_init(&my_cond_start_is_required, NULL); |
| 89 | assert(-1 != a_status); |
| 90 | a_status = pthread_cond_init(&my_cond_stop_is_required, NULL); |
| 91 | assert(-1 != a_status); |
| 92 | a_status = pthread_cond_init(&my_cond_stop_is_acknowledged, NULL); |
| 93 | assert(-1 != a_status); |
| 94 | (void)a_status; |
| 95 | |
| 96 | pthread_attr_t a_attrib; |
| 97 | |
| 98 | if (pthread_attr_init(&a_attrib) == 0 |
| 99 | && pthread_attr_setdetachstate(&a_attrib, PTHREAD_CREATE_JOINABLE) == 0) { |
| 100 | thread_inited = (0 == pthread_create(&my_thread, |
| 101 | &a_attrib, |
| 102 | polling_thread, |
| 103 | (void *)NULL)); |
| 104 | } |
| 105 | assert(thread_inited); |
| 106 | pthread_attr_destroy(&a_attrib); |
| 107 | } |
| 108 | |
| 109 | static espeak_EVENT *event_copy(espeak_EVENT *event) |
| 110 | { |
| 111 | if (event == NULL) |
| 112 | return NULL; |
| 113 | |
| 114 | espeak_EVENT *a_event = (espeak_EVENT *)malloc(sizeof(espeak_EVENT)); |
| 115 | if (a_event) { |
| 116 | memcpy(a_event, event, sizeof(espeak_EVENT)); |
| 117 | |
| 118 | switch (event->type) |
| 119 | { |
| 120 | case espeakEVENT_MARK: |
| 121 | case espeakEVENT_PLAY: |
| 122 | if (event->id.name) |
| 123 | a_event->id.name = strdup(event->id.name); |
| 124 | break; |
| 125 | |
| 126 | default: |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return a_event; |
| 132 | } |
| 133 | |
| 134 | // Call the user supplied callback |
| 135 | // |
| 136 | // Note: the current sequence is: |
| 137 | // |
| 138 | // * First call with: event->type = espeakEVENT_SENTENCE |
| 139 | // * 0, 1 or several calls: event->type = espeakEVENT_WORD |
| 140 | // * Last call: event->type = espeakEVENT_MSG_TERMINATED |
| 141 | // |
| 142 | |
| 143 | static void event_notify(espeak_EVENT *event) |
| 144 | { |
| 145 | static unsigned int a_old_uid = 0; |
| 146 | |
| 147 | espeak_EVENT events[2]; |
| 148 | memcpy(&events[0], event, sizeof(espeak_EVENT)); // the event parameter in the callback function should be an array of eventd |
| 149 | memcpy(&events[1], event, sizeof(espeak_EVENT)); |
| 150 | events[1].type = espeakEVENT_LIST_TERMINATED; // ... terminated by an event type=0 |
| 151 | |
| 152 | if (event && my_callback) { |
| 153 | switch (event->type) |
| 154 | { |
| 155 | case espeakEVENT_SENTENCE: |
| 156 | my_callback(NULL, 0, events); |
| 157 | a_old_uid = event->unique_identifier; |
| 158 | break; |
| 159 | case espeakEVENT_MSG_TERMINATED: |
| 160 | case espeakEVENT_MARK: |
| 161 | case espeakEVENT_WORD: |
| 162 | case espeakEVENT_END: |
| 163 | case espeakEVENT_PHONEME: |
| 164 | { |
| 165 | if (a_old_uid != event->unique_identifier) { |
| 166 | espeak_EVENT_TYPE a_new_type = events[0].type; |
| 167 | events[0].type = espeakEVENT_SENTENCE; |
| 168 | my_callback(NULL, 0, events); |
| 169 | events[0].type = a_new_type; |
| 170 | } |
| 171 | my_callback(NULL, 0, events); |
| 172 | a_old_uid = event->unique_identifier; |
| 173 | } |
| 174 | break; |
| 175 | case espeakEVENT_LIST_TERMINATED: |
| 176 | case espeakEVENT_PLAY: |
| 177 | default: |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static int event_delete(espeak_EVENT *event) |
| 184 | { |
| 185 | if (event == NULL) |
| 186 | return 0; |
| 187 | |
| 188 | switch (event->type) |
| 189 | { |
| 190 | case espeakEVENT_MSG_TERMINATED: |
| 191 | event_notify(event); |
| 192 | break; |
| 193 | case espeakEVENT_MARK: |
| 194 | case espeakEVENT_PLAY: |
| 195 | if (event->id.name) |
| 196 | free((void *)(event->id.name)); |
| 197 | break; |
| 198 | default: |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | free(event); |
| 203 | return 1; |
| 204 | } |
| 205 | |
| 206 | espeak_ng_STATUS event_declare(espeak_EVENT *event) |
| 207 | { |
| 208 | if (!event) |
| 209 | return EINVAL; |
| 210 | |
| 211 | espeak_ng_STATUS status; |
| 212 | if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK) { |
| 213 | my_start_is_required = true; |
| 214 | return status; |
| 215 | } |
| 216 | |
| 217 | espeak_EVENT *a_event = event_copy(event); |
| 218 | if ((status = push(a_event)) != ENS_OK) { |
| 219 | event_delete(a_event); |
| 220 | pthread_mutex_unlock(&my_mutex); |
| 221 | } else { |
| 222 | my_start_is_required = true; |
| 223 | pthread_cond_signal(&my_cond_start_is_required); |
| 224 | status = pthread_mutex_unlock(&my_mutex); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | return status; |
| 229 | } |
| 230 | |
| 231 | espeak_ng_STATUS event_clear_all(void) |
| 232 | { |
| 233 | espeak_ng_STATUS status; |
| 234 | if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK) |
| 235 | return status; |
| 236 | |
| 237 | int a_event_is_running = 0; |
| 238 | if (my_event_is_running) { |
| 239 | my_stop_is_required = true; |
| 240 | pthread_cond_signal(&my_cond_stop_is_required); |
| 241 | a_event_is_running = 1; |
| 242 | } else |
| 243 | init(); // clear pending events |
| 244 | |
| 245 | if (a_event_is_running) { |
| 246 | while (my_stop_is_acknowledged == false) { |
| 247 | while ((pthread_cond_wait(&my_cond_stop_is_acknowledged, &my_mutex) == -1) && errno == EINTR) |
| 248 | continue; // Restart when interrupted by handler |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if ((status = pthread_mutex_unlock(&my_mutex)) != ENS_OK) |
| 253 | return status; |
| 254 | |
| 255 | return ENS_OK; |
| 256 | } |
| 257 | |
| 258 | static void *polling_thread(void *p) |
| 259 | { |
| 260 | (void)p; // unused |
| 261 | |
| 262 | while (!my_terminate_is_required) { |
| 263 | bool a_stop_is_required = false; |
| 264 | |
| 265 | (void)pthread_mutex_lock(&my_mutex); |
| 266 | my_event_is_running = false; |
| 267 | |
| 268 | while (my_start_is_required == false && my_terminate_is_required == false) { |
| 269 | while ((pthread_cond_wait(&my_cond_start_is_required, &my_mutex) == -1) && errno == EINTR) |
| 270 | continue; // Restart when interrupted by handler |
| 271 | } |
| 272 | |
| 273 | my_event_is_running = true; |
| 274 | a_stop_is_required = false; |
| 275 | my_start_is_required = false; |
| 276 | |
| 277 | pthread_mutex_unlock(&my_mutex); |
| 278 | |
| 279 | // In this loop, my_event_is_running = true |
| 280 | while (head && (a_stop_is_required == false) && (my_terminate_is_required == false)) { |
| 281 | espeak_EVENT *event = (espeak_EVENT *)(head->data); |
| 282 | assert(event); |
| 283 | |
| 284 | if (my_callback) { |
| 285 | event_notify(event); |
| 286 | // the user_data (and the type) are cleaned to be sure |
| 287 | // that MSG_TERMINATED is called twice (at delete time too). |
| 288 | event->type = espeakEVENT_LIST_TERMINATED; |
| 289 | event->user_data = NULL; |
| 290 | } |
| 291 | |
| 292 | (void)pthread_mutex_lock(&my_mutex); |
| 293 | event_delete((espeak_EVENT *)pop()); |
| 294 | a_stop_is_required = my_stop_is_required; |
| 295 | if (a_stop_is_required == true) |
| 296 | my_stop_is_required = false; |
| 297 | |
| 298 | (void)pthread_mutex_unlock(&my_mutex); |
| 299 | } |
| 300 | |
| 301 | (void)pthread_mutex_lock(&my_mutex); |
| 302 | |
| 303 | my_event_is_running = false; |
| 304 | |
| 305 | if (a_stop_is_required == false) { |
| 306 | a_stop_is_required = my_stop_is_required; |
| 307 | if (a_stop_is_required == true) |
| 308 | my_stop_is_required = false; |
| 309 | } |
| 310 | |
| 311 | (void)pthread_mutex_unlock(&my_mutex); |
| 312 | |
| 313 | if (a_stop_is_required == true || my_terminate_is_required == true) { |
| 314 | // no mutex required since the stop command is synchronous |
| 315 | // and waiting for my_cond_stop_is_acknowledged |
| 316 | init(); |
| 317 | |
| 318 | // acknowledge the stop request |
| 319 | (void)pthread_mutex_lock(&my_mutex); |
| 320 | my_stop_is_acknowledged = true; |
| 321 | (void)pthread_cond_signal(&my_cond_stop_is_acknowledged); |
| 322 | (void)pthread_mutex_unlock(&my_mutex); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return NULL; |
| 327 | } |
| 328 | |
| 329 | enum { MAX_NODE_COUNTER = 1000 }; |
| 330 | |
| 331 | static espeak_ng_STATUS push(void *the_data) |
| 332 | { |
| 333 | assert((!head && !tail) || (head && tail)); |
| 334 | |
| 335 | if (the_data == NULL) |
| 336 | return EINVAL; |
| 337 | |
| 338 | if (node_counter >= MAX_NODE_COUNTER) |
| 339 | return ENS_EVENT_BUFFER_FULL; |
| 340 | |
| 341 | node *n = (node *)malloc(sizeof(node)); |
| 342 | if (n == NULL) |
| 343 | return static_cast<espeak_ng_STATUS> (ENOMEM); |
| 344 | |
| 345 | if (head == NULL) { |
| 346 | head = n; |
| 347 | tail = n; |
| 348 | } else { |
| 349 | tail->next = n; |
| 350 | tail = n; |
| 351 | } |
| 352 | |
| 353 | tail->next = NULL; |
| 354 | tail->data = the_data; |
| 355 | |
| 356 | node_counter++; |
| 357 | |
| 358 | return ENS_OK; |
| 359 | } |
| 360 | |
| 361 | static void *pop(void) |
| 362 | { |
| 363 | void *the_data = NULL; |
| 364 | |
| 365 | assert((!head && !tail) || (head && tail)); |
| 366 | |
| 367 | if (head != NULL) { |
| 368 | node *n = head; |
| 369 | the_data = n->data; |
| 370 | head = n->next; |
| 371 | free(n); |
| 372 | node_counter--; |
| 373 | } |
| 374 | |
| 375 | if (head == NULL) |
| 376 | tail = NULL; |
| 377 | |
| 378 | return the_data; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | static void init(void) |
| 383 | { |
| 384 | while (event_delete((espeak_EVENT *)pop())) |
| 385 | ; |
| 386 | |
| 387 | node_counter = 0; |
| 388 | } |
| 389 | |
| 390 | void event_terminate(void) |
| 391 | { |
| 392 | if (thread_inited) { |
| 393 | my_terminate_is_required = true; |
| 394 | pthread_cond_signal(&my_cond_start_is_required); |
| 395 | pthread_cond_signal(&my_cond_stop_is_required); |
| 396 | pthread_join(my_thread, NULL); |
| 397 | my_terminate_is_required = false; |
| 398 | |
| 399 | pthread_mutex_destroy(&my_mutex); |
| 400 | pthread_cond_destroy(&my_cond_start_is_required); |
| 401 | pthread_cond_destroy(&my_cond_stop_is_required); |
| 402 | pthread_cond_destroy(&my_cond_stop_is_acknowledged); |
| 403 | init(); // purge event |
| 404 | thread_inited = 0; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | enum { ONE_BILLION = 1000000000 }; |
| 409 | |
| 410 | void clock_gettime2(struct timespec *ts) |
| 411 | { |
| 412 | struct timeval tv; |
| 413 | |
| 414 | if (!ts) |
| 415 | return; |
| 416 | |
| 417 | int a_status = gettimeofday(&tv, NULL); |
| 418 | assert(a_status != -1); |
| 419 | (void)a_status; |
| 420 | ts->tv_sec = tv.tv_sec; |
| 421 | ts->tv_nsec = tv.tv_usec*1000; |
| 422 | } |
| 423 | |
| 424 | void add_time_in_ms(struct timespec *ts, int time_in_ms) |
| 425 | { |
| 426 | if (!ts) |
| 427 | return; |
| 428 | |
| 429 | uint64_t t_ns = (uint64_t)ts->tv_nsec + 1000000 * (uint64_t)time_in_ms; |
| 430 | while (t_ns >= ONE_BILLION) { |
| 431 | ts->tv_sec += 1; |
| 432 | t_ns -= ONE_BILLION; |
| 433 | } |
| 434 | ts->tv_nsec = (long int)t_ns; |
| 435 | } |