check and revise by coding rule
[platform/core/uifw/stt.git] / server / sttd_recorder.c
1 /*
2 *  Copyright (c) 2011-2014 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 #include <audio_io.h>
15 #include <Ecore.h>
16 #include <math.h>
17 #include <pthread.h>
18 #include <sound_manager.h>
19
20 #include "stt_defs.h"
21 #include "sttd_dbus.h"
22 #include "sttd_recorder.h"
23 #include "sttd_main.h"
24 #include "sttp.h"
25
26
27 #define FRAME_LENGTH 160
28 #define BUFFER_LENGTH FRAME_LENGTH * 2
29
30 static pthread_mutex_t sttd_audio_in_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
31
32 typedef enum {
33         STTD_RECORDER_STATE_NONE = -1,
34         STTD_RECORDER_STATE_READY = 0,  /**< Recorder is ready to start */
35         STTD_RECORDER_STATE_RECORDING   /**< In the middle of recording */
36 } sttd_recorder_state;
37
38 typedef struct {
39         int                     engine_id;
40         int                     uid;
41         audio_in_h              audio_h;
42         sttp_audio_type_e       audio_type;
43 } stt_recorder_s;
44
45 static GSList *g_recorder_list;
46
47 static int g_recording_engine_id;
48
49 static stt_recorder_audio_cb    g_audio_cb;
50
51 static sound_stream_info_h      g_stream_info_h;
52
53 static stt_recorder_interrupt_cb        g_interrupt_cb;
54
55 static sttd_recorder_state      g_recorder_state = STTD_RECORDER_STATE_NONE;
56
57 static int g_buffer_count;
58
59 /* Sound buf save for test */
60 /*
61 #define BUF_SAVE_MODE
62 */
63 #ifdef BUF_SAVE_MODE
64 static char g_temp_file_name[128] = {'\0',};
65
66 static FILE* g_pFile;
67
68 static int g_count = 1;
69 #endif
70
71 const char* __stt_get_focus_changed_reason_code(sound_stream_focus_change_reason_e reason)
72 {
73         switch (reason) {
74                 case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA:                       return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA";
75                 case SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM:                      return "SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM";
76                 case SOUND_STREAM_FOCUS_CHANGED_BY_ALARM:                       return "SOUND_STREAM_FOCUS_CHANGED_BY_ALARM";
77                 case SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION:        return "SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION";
78                 case SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY:           return "SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY";
79                 case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION:   return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION";
80                 case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION:   return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION";
81                 case SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE:            return "SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE";
82                 case SOUND_STREAM_FOCUS_CHANGED_BY_VOIP:                        return "SOUND_STREAM_FOCUS_CHANGED_BY_VOIP";
83                 case SOUND_STREAM_FOCUS_CHANGED_BY_CALL:                        return "SOUND_STREAM_FOCUS_CHANGED_BY_CALL";
84                 case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY: return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY";
85                 default:                                                                                        return "Undefined reason code";
86         }
87 }
88
89 void __recorder_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason, const char *extra_info, void *user_data)
90 {
91         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Focus state changed cb");
92
93         if (stream_info != g_stream_info_h) {
94                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid stream info handle");
95                 return;
96         }
97
98         int ret;
99         sound_stream_focus_state_e state_for_recording;
100         ret = sound_manager_get_focus_state(g_stream_info_h, NULL, &state_for_recording);
101         if (SOUND_MANAGER_ERROR_NONE != ret) {
102                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to get focus state");
103                 return;
104         }
105
106         SLOG(LOG_WARN, TAG_STTD, "[Recorder] focus state changed to (%d) with reason(%s)", (int)state_for_recording, __stt_get_focus_changed_reason_code(reason));
107         
108         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_RELEASED == state_for_recording) {
109                 SLOG(LOG_WARN, TAG_STTD, "[Recorder] Focus released as interrupt");
110                 if (NULL != g_interrupt_cb) {
111                         g_interrupt_cb();
112                 }
113         }
114 }
115
116
117 int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interrupt_cb interrupt_cb)
118 {
119         if (NULL == audio_cb || NULL == interrupt_cb) {
120                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input param is NOT valid");
121                 return STTD_ERROR_INVALID_PARAMETER;
122         }
123
124         if (STTD_RECORDER_STATE_NONE != g_recorder_state) {
125                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Current state of recorder is recording");
126                 return STTD_ERROR_INVALID_STATE;
127         }
128
129         if (0 != pthread_mutex_init(&sttd_audio_in_handle_mutex, NULL)) {
130                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to initialize audio in handle mutex.");
131         }
132
133         g_audio_cb = audio_cb;
134         g_interrupt_cb = interrupt_cb;
135         g_recorder_state = STTD_RECORDER_STATE_NONE;
136         g_recording_engine_id = -1;
137
138         if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, __recorder_focus_state_cb, NULL, &g_stream_info_h)) {
139                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create stream info");
140         }
141
142         return 0;
143 }
144
145 int sttd_recorder_deinitialize()
146 {
147         if (0 != pthread_mutex_destroy(&sttd_audio_in_handle_mutex)) {
148                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy audio in handle mutex.");
149         }
150
151         if (0 != sound_manager_destroy_stream_information(g_stream_info_h)) {
152                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy stream info");
153         }
154
155         /* Remove all recorder */
156         GSList *iter = NULL;
157         stt_recorder_s *recorder = NULL;
158
159         iter = g_slist_nth(g_recorder_list, 0);
160
161         while (NULL != iter) {
162                 recorder = iter->data;
163
164                 if (NULL != recorder) {
165                         g_recorder_list = g_slist_remove(g_recorder_list, recorder);
166                         audio_in_destroy(recorder->audio_h);
167
168                         free(recorder);
169                 }
170
171                 iter = g_slist_nth(g_recorder_list, 0);
172         }
173
174         g_recorder_state = STTD_RECORDER_STATE_NONE;
175
176         return 0;
177 }
178
179 static stt_recorder_s* __get_recorder(int engine_id)
180 {
181         GSList *iter = NULL;
182         stt_recorder_s *recorder = NULL;
183
184         iter = g_slist_nth(g_recorder_list, 0);
185
186         while (NULL != iter) {
187                 recorder = iter->data;
188
189                 if (recorder->engine_id == engine_id) {
190                         return recorder;
191                 }
192
193                 iter = g_slist_next(iter);
194         }
195
196         return NULL;
197 }
198
199 int sttd_recorder_set_audio_session()
200 {
201         return 0;
202 }
203
204 int sttd_recorder_unset_audio_session()
205 {
206         return 0;
207 }
208
209 int sttd_recorder_create(int engine_id, int uid, sttp_audio_type_e type, int channel, unsigned int sample_rate)
210 {
211         /* Check engine id is valid */
212         if (NULL != __get_recorder(engine_id)) {
213                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is already registered");
214                 return STTD_ERROR_INVALID_PARAMETER;
215         }
216
217         audio_channel_e audio_ch;
218         audio_sample_type_e audio_type;
219         audio_in_h temp_in_h;
220
221         switch (channel) {
222                 case 1: audio_ch = AUDIO_CHANNEL_MONO;          break;
223                 case 2: audio_ch = AUDIO_CHANNEL_STEREO;        break;
224                 default:
225                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input channel is not supported");
226                         return STTD_ERROR_OPERATION_FAILED;
227                         break;
228         }
229
230         switch (type) {
231                 case STTP_AUDIO_TYPE_PCM_S16_LE:        audio_type = AUDIO_SAMPLE_TYPE_S16_LE;  break;
232                 case STTP_AUDIO_TYPE_PCM_U8:            audio_type = AUDIO_SAMPLE_TYPE_U8;      break;
233                 default:
234                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid Audio Type");
235                         return STTD_ERROR_OPERATION_FAILED;
236                         break;
237         }
238
239         int ret;
240         ret = audio_in_create(sample_rate, audio_ch, audio_type, &temp_in_h);
241         if (AUDIO_IO_ERROR_NONE != ret) {
242                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
243                 return STTD_ERROR_OPERATION_FAILED;
244         }
245
246         stt_recorder_s* recorder;
247         recorder = (stt_recorder_s*)calloc(1, sizeof(stt_recorder_s));
248         if (NULL == recorder) {
249                 audio_in_destroy(temp_in_h);
250                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to allocate memory");
251                 return STTD_ERROR_OUT_OF_MEMORY;
252         }
253
254         recorder->engine_id = engine_id;
255         recorder->uid = uid;
256         recorder->audio_h = temp_in_h;
257         recorder->audio_type = type;
258
259         g_recorder_list = g_slist_append(g_recorder_list, recorder);
260
261         g_recorder_state = STTD_RECORDER_STATE_READY;
262
263         return 0;
264 }
265
266 int sttd_recorder_destroy(int engine_id)
267 {
268         // critical section required because this function can be called from stt engine thread context
269         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
270         pthread_mutex_lock(&sttd_audio_in_handle_mutex);
271
272         /* Check engine id is valid */
273         stt_recorder_s* recorder;
274         recorder = __get_recorder(engine_id);
275         if (NULL == recorder) {
276                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
277                 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
278                 return STTD_ERROR_INVALID_PARAMETER;
279         }
280
281         int ret;
282         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
283                 ret = audio_in_unprepare(recorder->audio_h);
284                 if (AUDIO_IO_ERROR_NONE != ret) {
285                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
286                 }
287
288                 g_recorder_state = STTD_RECORDER_STATE_READY;
289         }
290
291         ret = audio_in_destroy(recorder->audio_h);
292         if (AUDIO_IO_ERROR_NONE != ret) {
293                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy audioin : %d", ret);
294         }
295
296         g_recorder_list = g_slist_remove(g_recorder_list, recorder);
297
298         free(recorder);
299
300         pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
301         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
302
303         return 0;
304 }
305
306 static float get_volume_decibel(char* data, int size, sttp_audio_type_e type)
307 {
308         #define MAX_AMPLITUDE_MEAN_16   32768
309         #define MAX_AMPLITUDE_MEAN_08   128
310
311         int i, depthByte;
312         int count = 0;
313
314         float db = 0.0;
315         float rms = 0.0;
316         unsigned long long square_sum = 0;
317
318         if (type == STTP_AUDIO_TYPE_PCM_S16_LE)
319                 depthByte = 2;
320         else
321                 depthByte = 1;
322
323         for (i = 0; i < size; i += (depthByte<<1)) {
324                 if (depthByte == 2) {
325                         short pcm16 = 0;
326                         memcpy(&pcm16, data + i, sizeof(short));
327                         square_sum += pcm16 * pcm16;
328                 } else {
329                         char pcm8 = 0;
330                         memcpy(&pcm8, data + i, sizeof(char));
331                         square_sum += pcm8 * pcm8;
332                 }
333                 count++;
334         }
335
336         if (0 == count)
337                 rms = 0.0;
338         else
339                 rms = sqrt(square_sum/count);
340
341         if (depthByte == 2)
342                 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
343         else
344                 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_08);
345
346         return db;
347 }
348
349 Eina_Bool __read_audio_func(void *data)
350 {
351         int read_byte = -1;
352         static char g_buffer[BUFFER_LENGTH];
353
354         /* Check engine id is valid */
355         stt_recorder_s* recorder;
356         recorder = __get_recorder(g_recording_engine_id);
357         if (NULL == recorder) {
358                 return EINA_FALSE;
359         }
360
361         if (STTD_RECORDER_STATE_READY == g_recorder_state) {
362                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading func");
363                 return EINA_FALSE;
364         }
365
366         read_byte = audio_in_read(recorder->audio_h, g_buffer, BUFFER_LENGTH);
367         if (0 > read_byte) {
368                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail to read audio : %d", read_byte);
369                 g_recorder_state = STTD_RECORDER_STATE_READY;
370                 return EINA_FALSE;
371         }
372
373         if (0 != g_audio_cb(g_buffer, read_byte)) {
374                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail audio callback");
375                 sttd_recorder_stop(g_recording_engine_id);
376                 return EINA_FALSE;
377         }
378
379         if (0 == g_buffer_count % 30) {
380                 float vol_db = get_volume_decibel(g_buffer, BUFFER_LENGTH, recorder->audio_type);
381                 if (0 != sttdc_send_set_volume(recorder->uid, vol_db)) {
382                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
383                 }
384         }
385
386         /* Audio read log */
387         if (0 == g_buffer_count % 50) {
388                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, read_byte);
389
390                 if (100000 == g_buffer_count) {
391                         g_buffer_count = 0;
392                 }
393         }
394
395         g_buffer_count++;
396
397 #ifdef BUF_SAVE_MODE
398         /* write pcm buffer */
399         fwrite(g_buffer, 1, BUFFER_LENGTH, g_pFile);
400 #endif
401
402         return EINA_TRUE;
403 }
404
405 int sttd_recorder_start(int engine_id)
406 {
407         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
408                 return 0;
409
410         /* Check engine id is valid */
411         stt_recorder_s* recorder;
412         recorder = __get_recorder(engine_id);
413         if (NULL == recorder) {
414                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
415                 return STTD_ERROR_INVALID_PARAMETER;
416         }
417
418         int ret = -1;
419         ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
420         if (SOUND_MANAGER_ERROR_NONE != ret) {
421                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to acquire focus : %d", ret);
422         } else {
423                 ret = audio_in_set_stream_info(recorder->audio_h, g_stream_info_h);
424                 if (AUDIO_IO_ERROR_NONE != ret) {
425                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set stream info");
426                 }
427         }
428         
429         ret = audio_in_prepare(recorder->audio_h);
430         if (AUDIO_IO_ERROR_NONE != ret) {
431                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start audio : %d", ret);
432                 return STTD_ERROR_RECORDER_BUSY;
433         }
434
435         /* Add ecore timer to read audio data */
436         ecore_timer_add(0, __read_audio_func, NULL);
437
438         g_recorder_state = STTD_RECORDER_STATE_RECORDING;
439         g_recording_engine_id = engine_id;
440
441         g_buffer_count = 0;
442
443 #ifdef BUF_SAVE_MODE
444         g_count++;
445
446         snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count);
447         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Temp file name=[%s]", g_temp_file_name);
448
449         /* open test file */
450         g_pFile = fopen(g_temp_file_name, "wb+");
451         if (!g_pFile) {
452                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File not found!");
453                 return -1;
454         }
455 #endif
456
457         return 0;
458 }
459
460 int sttd_recorder_stop(int engine_id)
461 {
462         if (STTD_RECORDER_STATE_READY == g_recorder_state)
463                 return 0;
464
465         /* Check engine id is valid */
466         stt_recorder_s* recorder;
467         recorder = __get_recorder(engine_id);
468         if (NULL == recorder) {
469                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
470                 return STTD_ERROR_INVALID_PARAMETER;
471         }
472
473         int ret;
474         ret = audio_in_unprepare(recorder->audio_h);
475         if (AUDIO_IO_ERROR_NONE != ret) {
476                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
477         }
478
479         g_recorder_state = STTD_RECORDER_STATE_READY;
480         g_recording_engine_id = -1;
481
482         ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
483         if (SOUND_MANAGER_ERROR_NONE != ret) {
484                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to release focus :%d", ret);
485         }
486
487 #ifdef BUF_SAVE_MODE
488         fclose(g_pFile);
489 #endif
490
491         return 0;
492 }