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