Change to TTS engine process
[platform/core/uifw/tts.git] / server / ttse.c
1 /*
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.
12 */
13
14
15 #include "ttsd_main.h"
16 #include "ttsd_server.h"
17 #include "ttsd_dbus.h"
18 #include "ttsd_network.h"
19
20 #include <bundle.h>
21 #include <bundle_internal.h>
22 #include <dlog.h>
23 #include <Ecore.h>
24
25 #include "ttse.h"
26
27 #define CLIENT_CLEAN_UP_TIME 500
28
29 static Ecore_Timer* g_check_client_timer = NULL;
30
31 static ttsd_mode_e g_tts_mode = TTSD_MODE_DEFAULT;
32
33 const char* tts_tag()
34 {
35         if (TTSD_MODE_NOTIFICATION == g_tts_mode) {
36                 return "ttsdnoti";
37         } else if (TTSD_MODE_SCREEN_READER == g_tts_mode) {
38                 return "ttsdsr";
39         } else {
40                 return "ttsd";
41         }
42 }
43
44 ttsd_mode_e ttsd_get_mode()
45 {
46         return g_tts_mode;
47 }
48
49 void ttsd_set_mode(ttsd_mode_e mode)
50 {
51         g_tts_mode = mode;
52         return;
53 }
54
55 int ttse_main(int argc, char** argv, ttse_request_callback_s *callback)
56 {
57         bundle *b = NULL;
58         ttsd_mode_e mode = TTSD_MODE_DEFAULT;
59
60         b = bundle_import_from_argv(argc, argv);
61         if (NULL != b) {
62                 char *val = NULL;
63                 if (0 == bundle_get_str(b, "mode", &val)) {
64                         if (NULL != val) {
65                                 if (!strcmp("noti", val)) {
66                                         mode = TTSD_MODE_NOTIFICATION;
67                                 } else if (!strcmp("sr", val)) {
68                                         mode = TTSD_MODE_SCREEN_READER;
69                                 } else {
70                                         SLOG(LOG_WARN, tts_tag(), "[WARNING] mode (%s)", val);
71                                 }
72                         } else {
73                                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] NULL data");
74                         }
75                 } else {
76                         SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get data from bundle");
77                 }
78                 bundle_free(b);
79         } else {
80                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get bundle");
81         }
82
83         ttsd_set_mode(mode);
84
85         SLOG(LOG_DEBUG, tts_tag(), "Start engine as [%d] mode", mode);
86
87         if (!ecore_init()) {
88                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize Ecore");
89                 return -1;
90         }
91
92         if (0 != ttsd_dbus_open_connection()) {
93                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open dbus connection");
94                 return -1;
95         }
96
97         if (0 != ttsd_initialize(callback)) {
98                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize");
99                 return -1;
100         }
101
102         if (0 != ttsd_network_initialize()) {
103                 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to initialize network");
104         }
105
106         g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, ttsd_cleanup_client, NULL);
107         if (NULL == g_check_client_timer) {
108                 SLOG(LOG_WARN, tts_tag(), "[WARNING] Fail to create timer");
109         }
110
111         SLOG(LOG_DEBUG, tts_tag(), "====");
112         SLOG(LOG_DEBUG, tts_tag(), "");
113
114         return 0;
115 }
116
117 int ttse_get_speed_range(int* min, int* normal, int* max)
118 {
119         if (NULL == min || NULL == normal || NULL == max) {
120                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
121                 return TTSE_ERROR_INVALID_PARAMETER;
122         }
123
124         *min = TTS_SPEED_MIN;
125         *normal = TTS_SPEED_NORMAL;
126         *max = TTS_SPEED_MAX;
127
128         return 0;
129 }
130
131 int ttse_get_pitch_range(int* min, int* normal, int* max)
132 {
133         if (NULL == min || NULL == normal || NULL == max) {
134                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
135                 return TTSE_ERROR_INVALID_PARAMETER;
136         }
137
138         *min = TTS_PITCH_MIN;
139         *normal = TTS_PITCH_NORMAL;
140         *max = TTS_PITCH_MAX;
141
142         return 0;
143 }
144
145 int ttse_send_result(ttse_result_event_e event, const void* data, unsigned int data_size, ttse_audio_type_e audio_type, int rate, void* user_data)
146 {
147         int ret;
148
149         if (NULL == data) {
150                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
151                 return TTSE_ERROR_INVALID_PARAMETER;
152         }
153
154         ret = ttsd_send_result(event, data, data_size, audio_type, rate, user_data);
155
156         if (0 != ret) {
157                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send result");
158         }
159
160         return ret;
161 }
162
163 int ttse_send_error(ttse_error_e error, const char* msg)
164 {
165         int ret;
166
167         if (NULL == msg) {
168                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
169                 return TTSE_ERROR_INVALID_PARAMETER;
170         }
171
172         ret = ttsd_send_error(error, msg);
173
174         if (0 != ret) {
175                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send error");
176         }
177
178         return ret;
179 }
180
181 int ttse_set_private_data_set_cb(ttse_private_data_set_cb callback_func)
182 {
183         int ret = ttsd_set_private_data_set_cb(callback_func);
184
185         if (0 != ret) {
186                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data set cb");
187         }
188
189         return ret;
190 }
191
192 int ttse_set_private_data_requested_cb(ttse_private_data_requested_cb callback_func)
193 {
194         int ret = ttsd_set_private_data_requested_cb(callback_func);
195
196         if (0 != ret) {
197                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data requested cb");
198         }
199
200         return ret;
201 }