2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
15 #include "tts_client.h"
18 static const int MAX_HANDLE_NUM = 999;
19 static const int INVALID_HANDLE = -1;
21 /* allocated handle */
22 static unsigned int g_allocated_handle = 0;
24 static GList *g_client_list = NULL;
27 static pthread_mutex_t g_allocated_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
28 static pthread_mutex_t g_client_list_mutex = PTHREAD_MUTEX_INITIALIZER;
29 static pthread_mutex_t g_repeat_text_mutex = PTHREAD_MUTEX_INITIALIZER;
32 static unsigned int __client_generate_uid(unsigned int pid)
34 pthread_mutex_lock(&g_allocated_handle_mutex);
37 if (g_allocated_handle > MAX_HANDLE_NUM) {
38 g_allocated_handle = 1;
41 /* generate uid, handle number should be smaller than 1000 */
42 unsigned int uid = pid * 1000u + g_allocated_handle;
43 pthread_mutex_unlock(&g_allocated_handle_mutex);
48 static inline void __set_state_changed_cb(tts_client_s* client, tts_state_changed_cb callback, void* user_data)
50 if (callback != NULL) {
51 client->registered_event_mask |= TTS_EVENT_MASK_STATE_CHANGED;
53 client->registered_event_mask &= ~TTS_EVENT_MASK_STATE_CHANGED;
56 client->state_changed_cb = callback;
57 client->state_changed_user_data = user_data;
60 static inline void __set_utterance_started_cb(tts_client_s* client, tts_utterance_started_cb callback, void* user_data)
62 if (callback != NULL) {
63 client->registered_event_mask |= TTS_EVENT_MASK_UTTERANCE_STARTED;
65 client->registered_event_mask &= ~TTS_EVENT_MASK_UTTERANCE_STARTED;
68 client->utt_started_cb = callback;
69 client->utt_started_user_data = user_data;
72 static inline void __set_utterance_completed_cb(tts_client_s* client, tts_utterance_completed_cb callback, void* user_data)
74 if (callback != NULL) {
75 client->registered_event_mask |= TTS_EVENT_MASK_UTTERANCE_COMPLETED;
77 client->registered_event_mask &= ~TTS_EVENT_MASK_UTTERANCE_COMPLETED;
80 client->utt_completed_cb = callback;
81 client->utt_completed_user_data = user_data;
84 static inline void __set_error_cb(tts_client_s* client, tts_error_cb callback, void* user_data)
86 if (callback != NULL) {
87 client->registered_event_mask |= TTS_EVENT_MASK_ERROR;
89 client->registered_event_mask &= ~TTS_EVENT_MASK_ERROR;
92 client->error_cb = callback;
93 client->error_user_data = user_data;
96 static inline void __set_default_voice_changed_cb(tts_client_s* client, tts_default_voice_changed_cb callback, void* user_data)
98 if (callback != NULL) {
99 client->registered_event_mask |= TTS_EVENT_MASK_DEFAULT_VOICE_CHANGED;
101 client->registered_event_mask &= ~TTS_EVENT_MASK_DEFAULT_VOICE_CHANGED;
104 client->default_voice_changed_cb = callback;
105 client->default_voice_changed_user_data = user_data;
108 static inline void __set_engine_changed_cb(tts_client_s* client, tts_engine_changed_cb callback, void* user_data)
110 if (callback != NULL) {
111 client->registered_event_mask |= TTS_EVENT_MASK_ENGINE_CHANGED;
113 client->registered_event_mask &= ~TTS_EVENT_MASK_ENGINE_CHANGED;
116 client->engine_changed_cb = callback;
117 client->engine_changed_user_data = user_data;
120 static inline void __set_screen_reader_changed_cb(tts_client_s* client, tts_screen_reader_changed_cb callback, void* user_data)
122 if (callback != NULL) {
123 client->registered_event_mask |= TTS_EVENT_MASK_SCREEN_READER_CHANGED;
125 client->registered_event_mask &= ~TTS_EVENT_MASK_SCREEN_READER_CHANGED;
128 client->screen_reader_changed_cb = callback;
129 client->screen_reader_changed_user_data = user_data;
132 static inline void __set_supported_voice_cb(tts_client_s* client, tts_supported_voice_cb callback, void* user_data)
134 client->supported_voice_cb = callback;
135 client->supported_voice_user_data = user_data;
138 static inline void __set_service_state_changed_cb(tts_client_s* client, tts_service_state_changed_cb callback, void* user_data)
140 if (callback != NULL) {
141 client->registered_event_mask |= TTS_EVENT_MASK_SERVICE_STATE_CHANGED;
143 client->registered_event_mask &= ~TTS_EVENT_MASK_SERVICE_STATE_CHANGED;
146 client->service_state_changed_cb = callback;
147 client->service_state_changed_user_data = user_data;
150 static inline void __set_synthesized_pcm_cb(tts_client_s* client, tts_synthesized_pcm_cb callback, void* user_data)
152 client->synthesized_pcm_cb = callback;
153 client->synthesized_pcm_user_data = user_data;
156 int tts_client_new(tts_h* tts)
158 tts_client_s* client = (tts_client_s*)calloc(1, sizeof(tts_client_s));
159 if (NULL == client) {
160 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
161 return TTS_ERROR_OUT_OF_MEMORY;
164 tts_h temp = (tts_h)calloc(1, sizeof(struct tts_s));
166 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
168 return TTS_ERROR_OUT_OF_MEMORY;
171 temp->handle = __client_generate_uid((unsigned int)getpid());
173 /* initialize client data */
175 client->pid = getpid();
176 client->uid = temp->handle;
177 client->current_service_state = TTS_SERVICE_STATE_NONE;
179 pthread_mutex_lock(&g_client_list_mutex);
180 g_client_list = g_list_append(g_client_list, client);
181 pthread_mutex_unlock(&g_client_list_mutex);
185 SLOG(LOG_ERROR, TAG_TTSC, "[Success] Create client object : uid(%u)", client->uid);
187 return TTS_ERROR_NONE;
190 int tts_client_destroy(tts_h tts)
193 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
194 return TTS_ERROR_INVALID_PARAMETER;
198 tts_client_s *data = NULL;
200 pthread_mutex_lock(&g_client_list_mutex);
201 /* if list have item */
202 if (g_list_length(g_client_list) > 0) {
203 /* Get a first item */
204 iter = g_list_first(g_client_list);
206 while (NULL != iter) {
208 if (tts->handle == data->tts->handle) {
209 tts->handle = TTS_INVALID_UID;
210 g_client_list = g_list_remove_link(g_client_list, iter);
212 while (0 != data->cb_ref_count) {
213 /* wait for release callback function */
216 unsigned int uid = data->uid;
219 free(data->credential);
221 pthread_mutex_lock(&g_repeat_text_mutex);
222 free(data->text_repeat);
223 data->text_repeat = NULL;
224 pthread_mutex_unlock(&g_repeat_text_mutex);
226 memset(data, 0, sizeof(tts_client_s));
236 pthread_mutex_unlock(&g_client_list_mutex);
237 SLOG(LOG_ERROR, TAG_TTSC, "Client destroy : uid(%u)", uid);
238 return TTS_ERROR_NONE;
242 iter = g_list_next(iter);
245 pthread_mutex_unlock(&g_client_list_mutex);
246 SLOG(LOG_ERROR, TAG_TTSC, "Fail to destroy client : handle is not valid");
248 return TTS_ERROR_INVALID_PARAMETER;
251 tts_client_s* tts_client_get(tts_h tts)
254 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
259 tts_client_s *data = NULL;
261 pthread_mutex_lock(&g_client_list_mutex);
262 if (g_list_length(g_client_list) > 0) {
263 /* Get a first item */
264 iter = g_list_first(g_client_list);
266 while (NULL != iter) {
269 if (tts->handle == data->tts->handle) {
270 pthread_mutex_unlock(&g_client_list_mutex);
275 iter = g_list_next(iter);
279 pthread_mutex_unlock(&g_client_list_mutex);
280 SLOG(LOG_ERROR, TAG_TTSC, "handle is not valid");
285 tts_client_s* tts_client_get_by_uid(unsigned int uid)
287 if (uid == TTS_INVALID_UID) {
288 SLOG(LOG_ERROR, TAG_TTSC, "out of range : handle");
293 tts_client_s *data = NULL;
295 pthread_mutex_lock(&g_client_list_mutex);
296 if (g_list_length(g_client_list) > 0) {
297 /* Get a first item */
298 iter = g_list_first(g_client_list);
300 while (NULL != iter) {
302 if (uid == data->uid) {
303 pthread_mutex_unlock(&g_client_list_mutex);
308 iter = g_list_next(iter);
312 pthread_mutex_unlock(&g_client_list_mutex);
313 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] uid(%u) is not valid", uid);
318 bool tts_client_is_valid_uid(unsigned int uid)
320 if (NULL == tts_client_get_by_uid(uid)) {
321 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Uid is not valid");
328 bool tts_client_is_valid_client(tts_client_s* client)
330 if (NULL == client) {
331 SLOG(LOG_ERROR, TAG_TTSC, "[WARNING] Client is NULL");
335 pthread_mutex_lock(&g_client_list_mutex);
336 if (g_list_length(g_client_list) > 0) {
337 GList *iter = g_list_first(g_client_list);
338 while (NULL != iter) {
339 if (iter->data == client) {
340 pthread_mutex_unlock(&g_client_list_mutex);
344 iter = g_list_next(iter);
347 pthread_mutex_unlock(&g_client_list_mutex);
349 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Client(%p) is not valid", client);
353 tts_h tts_client_get_handle(tts_client_s* client)
355 if (false == tts_client_is_valid_client(client)) {
362 unsigned int tts_client_get_size()
364 pthread_mutex_lock(&g_client_list_mutex);
365 unsigned int size = g_list_length(g_client_list);
366 pthread_mutex_unlock(&g_client_list_mutex);
371 int tts_client_use_callback(tts_client_s* client)
373 client->cb_ref_count++;
377 int tts_client_not_use_callback(tts_client_s* client)
379 client->cb_ref_count--;
383 int tts_client_get_use_callback(tts_client_s* client)
385 return client->cb_ref_count;
388 GList* tts_client_get_client_list()
390 GList* copy_list = NULL;
391 pthread_mutex_lock(&g_client_list_mutex);
392 copy_list = g_list_copy(g_client_list);
393 pthread_mutex_unlock(&g_client_list_mutex);
398 unsigned int tts_client_get_uid(tts_client_s* client)
400 if (NULL == client || false == tts_client_is_valid_client(client)) {
401 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Client(%p) is not valid", client);
402 return TTS_INVALID_UID;
408 void tts_client_set_current_state(tts_client_s* client, tts_state_e state)
410 if (false == tts_client_is_valid_client(client)) {
414 client->before_state = client->current_state;
415 client->current_state = state;
418 tts_state_e tts_client_get_current_state(tts_client_s* client)
420 if (false == tts_client_is_valid_client(client)) {
421 return TTS_STATE_INVALID;
424 return client->current_state;
427 void tts_client_set_current_service_state(tts_client_s* client, tts_service_state_e service_state)
429 if (false == tts_client_is_valid_client(client)) {
433 client->current_service_state = service_state;
436 tts_service_state_e tts_client_get_current_service_state(tts_client_s* client)
438 if (false == tts_client_is_valid_client(client)) {
439 return TTS_SERVICE_STATE_NONE;
442 return client->current_service_state;
445 void tts_client_set_start_listening(unsigned int uid, bool is_listening_started)
447 tts_client_s* client = tts_client_get_by_uid(uid);
448 if (NULL == client) {
452 client->start_listening = is_listening_started;
455 bool tts_client_is_listening_started(unsigned int uid)
457 tts_client_s* client = tts_client_get_by_uid(uid);
458 if (NULL == client) {
462 return client->start_listening;
465 void tts_client_set_reprepared(tts_client_s* client, bool is_reprepared)
467 if (false == tts_client_is_valid_client(client)) {
471 client->reprepared = is_reprepared;
474 bool tts_client_is_reprepared(tts_client_s* client)
476 if (false == tts_client_is_valid_client(client)) {
477 return INVALID_HANDLE;
480 return client->reprepared;
483 void tts_client_set_service_out(tts_client_s* client, bool is_service_out)
485 if (false == tts_client_is_valid_client(client)) {
489 client->service_out = is_service_out;
492 bool tts_client_is_service_out(tts_client_s* client)
494 if (false == tts_client_is_valid_client(client)) {
498 return client->service_out;
501 void tts_client_set_mode(tts_client_s* client, tts_mode_e mode)
503 if (false == tts_client_is_valid_client(client)) {
510 tts_mode_e tts_client_get_mode(tts_client_s* client)
512 if (false == tts_client_is_valid_client(client)) {
513 return INVALID_HANDLE;
519 void tts_client_set_playing_mode(tts_client_s* client, tts_playing_mode_e mode)
521 if (false == tts_client_is_valid_client(client)) {
525 client->playing_mode = mode;
528 tts_playing_mode_e tts_client_get_playing_mode(tts_client_s* client)
530 if (false == tts_client_is_valid_client(client)) {
531 return INVALID_HANDLE;
534 return client->playing_mode;
537 void tts_client_set_repeat_text(tts_client_s* client, const char* text)
539 if (NULL == client || false == tts_client_is_valid_client(client)) {
543 pthread_mutex_lock(&g_repeat_text_mutex);
544 if (NULL != client->text_repeat) {
545 free(client->text_repeat);
549 client->text_repeat = strdup(text);
551 client->text_repeat = NULL;
553 pthread_mutex_unlock(&g_repeat_text_mutex);
556 char* tts_client_get_repeat_text(tts_client_s* client)
558 if (NULL == client || false == tts_client_is_valid_client(client)) {
562 char *repeat_text = NULL;
563 pthread_mutex_lock(&g_repeat_text_mutex);
564 if (NULL != client->text_repeat) {
565 repeat_text = strdup(client->text_repeat);
567 pthread_mutex_unlock(&g_repeat_text_mutex);
572 int tts_client_new_utterance_id(tts_client_s* client)
574 if (NULL == client || false == tts_client_is_valid_client(client)) {
575 return INVALID_HANDLE;
578 client->current_utt_id++;
579 if (client->current_utt_id == 10000) {
580 client->current_utt_id = 1;
583 return client->current_utt_id;
586 void tts_client_set_error_message(tts_client_s* client, const char* error_message)
588 if (false == tts_client_is_valid_client(client)) {
592 if (NULL != client->err_msg) {
593 free(client->err_msg);
594 client->err_msg = NULL;
597 if (NULL != error_message) {
598 client->err_msg = strdup(error_message);
602 const char* tts_client_get_error_message(tts_client_s* client)
604 if (false == tts_client_is_valid_client(client)) {
608 return client->err_msg;
611 void tts_client_set_credential_key(tts_client_s* client, const char* credential_key)
613 if (false == tts_client_is_valid_client(client)) {
617 free(client->credential);
618 client->credential = NULL;
620 if (NULL != credential_key) {
621 client->credential = strdup(credential_key);
625 const char* tts_client_get_credential_key(tts_client_s* client)
627 if (false == tts_client_is_valid_client(client)) {
631 return client->credential;
634 void tts_client_set_state_changed_cb(tts_client_s* client, tts_state_changed_cb callback, void* user_data)
636 if (false == tts_client_is_valid_client(client)) {
640 if (NULL == callback) {
641 if (NULL != client->notify_state_timer) {
642 ecore_timer_del(client->notify_state_timer);
643 client->notify_state_timer = NULL;
646 __set_state_changed_cb(client, callback, user_data);
649 void tts_client_set_utterance_started_cb(tts_client_s* client, tts_utterance_started_cb callback, void* user_data)
651 if (false == tts_client_is_valid_client(client)) {
654 __set_utterance_started_cb(client, callback, user_data);
657 void tts_client_set_utterance_completed_cb(tts_client_s* client, tts_utterance_completed_cb callback, void* user_data)
659 if (false == tts_client_is_valid_client(client)) {
662 __set_utterance_completed_cb(client, callback, user_data);
665 void tts_client_set_error_cb(tts_client_s* client, tts_error_cb callback, void* user_data)
667 if (false == tts_client_is_valid_client(client)) {
671 if (NULL == callback) {
672 if (NULL != client->notify_error_timer) {
673 ecore_timer_del(client->notify_error_timer);
674 client->notify_error_timer = NULL;
677 __set_error_cb(client, callback, user_data);
680 void tts_client_set_default_voice_changed_cb(tts_client_s* client, tts_default_voice_changed_cb callback, void* user_data)
682 if (false == tts_client_is_valid_client(client)) {
685 __set_default_voice_changed_cb(client, callback, user_data);
688 void tts_client_set_engine_changed_cb(tts_client_s* client, tts_engine_changed_cb callback, void* user_data)
690 if (false == tts_client_is_valid_client(client)) {
693 __set_engine_changed_cb(client, callback, user_data);
696 void tts_client_set_screen_reader_changed_cb(tts_client_s* client, tts_screen_reader_changed_cb callback, void* user_data)
698 if (false == tts_client_is_valid_client(client)) {
701 __set_screen_reader_changed_cb(client, callback, user_data);
704 void tts_client_set_supported_voice_cb(tts_client_s* client, tts_supported_voice_cb callback, void* user_data)
706 if (false == tts_client_is_valid_client(client)) {
709 __set_supported_voice_cb(client, callback, user_data);
712 void tts_client_set_service_state_changed_cb(tts_client_s* client, tts_service_state_changed_cb callback, void* user_data)
714 if (false == tts_client_is_valid_client(client)) {
717 __set_service_state_changed_cb(client, callback, user_data);
720 void tts_client_set_synthesized_pcm_cb(tts_client_s* client, tts_synthesized_pcm_cb callback, void* user_data)
722 if (false == tts_client_is_valid_client(client)) {
725 __set_synthesized_pcm_cb(client, callback, user_data);
728 tts_state_changed_cb tts_client_get_state_changed_cb(tts_client_s* client)
730 if (false == tts_client_is_valid_client(client)) {
733 return client->state_changed_cb;
736 void* tts_client_get_state_changed_user_data(tts_client_s* client)
738 if (false == tts_client_is_valid_client(client)) {
741 return client->state_changed_user_data;
744 tts_utterance_started_cb tts_client_get_utterance_started_cb(tts_client_s* client)
746 if (false == tts_client_is_valid_client(client)) {
749 return client->utt_started_cb;
752 void* tts_client_get_utterance_started_user_data(tts_client_s* client)
754 if (false == tts_client_is_valid_client(client)) {
757 return client->utt_started_user_data;
760 tts_utterance_completed_cb tts_client_get_utterance_completed_cb(tts_client_s* client)
762 if (false == tts_client_is_valid_client(client)) {
765 return client->utt_completed_cb;
768 void* tts_client_get_utterance_completed_user_data(tts_client_s* client)
770 if (false == tts_client_is_valid_client(client)) {
773 return client->utt_completed_user_data;
776 tts_error_cb tts_client_get_error_cb(tts_client_s* client)
778 if (false == tts_client_is_valid_client(client)) {
781 return client->error_cb;
784 void* tts_client_get_error_user_data(tts_client_s* client)
786 if (false == tts_client_is_valid_client(client)) {
789 return client->error_user_data;
793 tts_default_voice_changed_cb tts_client_get_default_voice_changed_cb(tts_client_s* client)
795 if (false == tts_client_is_valid_client(client)) {
798 return client->default_voice_changed_cb;
801 void* tts_client_get_default_voice_changed_user_data(tts_client_s* client)
803 if (false == tts_client_is_valid_client(client)) {
806 return client->default_voice_changed_user_data;
809 tts_engine_changed_cb tts_client_get_engine_changed_cb(tts_client_s* client)
811 if (false == tts_client_is_valid_client(client)) {
814 return client->engine_changed_cb;
817 void* tts_client_get_engine_changed_user_data(tts_client_s* client)
819 if (false == tts_client_is_valid_client(client)) {
822 return client->engine_changed_user_data;
826 tts_screen_reader_changed_cb tts_client_get_screen_reader_changed_cb(tts_client_s* client)
828 if (false == tts_client_is_valid_client(client)) {
831 return client->screen_reader_changed_cb;
834 void* tts_client_get_screen_reader_changed_user_data(tts_client_s* client)
836 if (false == tts_client_is_valid_client(client)) {
839 return client->screen_reader_changed_user_data;
842 tts_supported_voice_cb tts_client_get_supported_voice_cb(tts_client_s* client)
844 if (false == tts_client_is_valid_client(client)) {
847 return client->supported_voice_cb;
850 void* tts_client_get_supported_voice_user_data(tts_client_s* client)
852 if (false == tts_client_is_valid_client(client)) {
855 return client->supported_voice_user_data;
858 tts_service_state_changed_cb tts_client_get_service_state_changed_cb(tts_client_s* client)
860 if (false == tts_client_is_valid_client(client)) {
863 return client->service_state_changed_cb;
866 void* tts_client_get_service_state_changed_user_data(tts_client_s* client)
868 if (false == tts_client_is_valid_client(client)) {
871 return client->service_state_changed_user_data;
874 tts_synthesized_pcm_cb tts_client_get_synthesized_pcm_cb(tts_client_s* client)
876 if (false == tts_client_is_valid_client(client)) {
879 return client->synthesized_pcm_cb;
882 void* tts_client_get_synthesized_pcm_user_data(tts_client_s* client)
884 if (false == tts_client_is_valid_client(client)) {
887 return client->synthesized_pcm_user_data;
890 int tts_client_get_registered_event_mask(tts_client_s* client)
892 if (false == tts_client_is_valid_client(client)) {
895 return client->registered_event_mask;
898 void tts_client_unset_all_cb(tts_client_s* client)
900 if (false == tts_client_is_valid_client(client)) {
904 if (NULL != client->notify_state_timer) {
905 ecore_timer_del(client->notify_state_timer);
906 client->notify_state_timer = NULL;
909 if (NULL != client->notify_error_timer) {
910 ecore_timer_del(client->notify_error_timer);
911 client->notify_error_timer = NULL;
914 __set_state_changed_cb(client, NULL, NULL);
915 __set_utterance_started_cb(client, NULL, NULL);
916 __set_utterance_completed_cb(client, NULL, NULL);
917 __set_error_cb(client, NULL, NULL);
918 __set_default_voice_changed_cb(client, NULL, NULL);
919 __set_engine_changed_cb(client, NULL, NULL);
920 __set_supported_voice_cb(client, NULL, NULL);
921 __set_service_state_changed_cb(client, NULL, NULL);
922 __set_synthesized_pcm_cb(client, NULL, NULL);