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.
16 #include <sound_manager.h>
17 #include <sound_manager_internal.h>
19 #include "ttsd_main.h"
20 #include "ttsd_player.h"
21 #include "ttsd_data.h"
22 #include "ttsd_dbus.h"
24 #include "tts_internal.h"
25 #include "ttsd_server.h"
28 * Internal data structure
38 int uid; /** client id */
39 app_tts_state_e state; /** client state */
41 /* Current utterance information */
42 ttse_result_event_e event; /** event of last utterance */
46 sound_data_s* paused_data;
49 #define SOUND_BUFFER_LENGTH 2048
50 #define FOCUS_SERVER_READY "/tmp/.sound_server_ready"
52 /** player init info */
53 static bool g_player_init = false;
56 static GList *g_player_list;
58 /** current player information */
59 static player_s* g_playing_info;
62 static audio_state_e g_audio_state;
64 static ttse_audio_type_e g_audio_type;
66 static int g_sampling_rate;
68 static audio_out_h g_audio_h;
70 static sound_stream_info_h g_stream_info_h;
72 static int g_focus_watch_id;
77 player_s* __player_get_item(int uid)
80 player_s *data = NULL;
82 if (0 < g_list_length(g_player_list)) {
83 /* Get a first item */
84 iter = g_list_first(g_player_list);
86 while (NULL != iter) {
87 /* Get handle data from list */
88 data = (player_s*)iter->data;
95 iter = g_list_next(iter);
102 void __player_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state,
103 sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, const char *extra_info, void *user_data)
105 SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state changed cb");
107 if (stream_info != g_stream_info_h) {
108 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid stream info handle");
111 SLOG(LOG_WARN, tts_tag(), "[Player] focus state changed to (%d) with reason(%d) and extra info(%s)", (int)focus_state, (int)reason_for_change, extra_info);
113 if (AUDIO_STATE_PLAY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_RELEASED == focus_state) {
114 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
115 g_audio_state = AUDIO_STATE_READY;
117 if (NULL == g_playing_info) {
118 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
122 if (APP_STATE_PLAYING == g_playing_info->state) {
123 int uid = g_playing_info->uid;
125 if (0 != ttsd_player_pause(uid)) {
126 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to pause the player");
130 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
131 int pid = ttsd_data_get_pid(uid);
132 /* send message to client about changing state */
133 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
136 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
140 /* if (AUDIO_STATE_READY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_ACQUIRED == focus_state) {
141 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
142 g_audio_state = AUDIO_STATE_PLAY;
144 if (NULL == g_playing_info) {
145 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
149 if (APP_STATE_PAUSED == g_playing_info->state) {
150 int uid = g_playing_info->uid;
152 g_audio_state = AUDIO_STATE_PLAY;
153 if (0 != ttsd_player_resume(uid)) {
154 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to resume the player");
155 g_audio_state = AUDIO_STATE_READY;
159 ttsd_data_set_client_state(uid, APP_STATE_PLAYING);
160 int pid = ttsd_data_get_pid(uid);
161 ttsdc_send_set_state_message(pid, uid, APP_STATE_PLAYING);
165 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
169 SLOG(LOG_DEBUG, tts_tag(), "@@@");
174 void __player_focus_state_watch_cb(int id, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state, sound_stream_focus_change_reason_e reason,
175 const char *extra_info, void *user_data)
177 SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state watch cb");
179 ttsd_mode_e mode = ttsd_get_mode();
181 if (TTSD_MODE_SCREEN_READER != mode && TTSD_MODE_NOTIFICATION != mode) {
182 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] This is not screen-reader mode and notification mode.");
186 if (AUDIO_STATE_PLAY == g_audio_state && SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason &&
187 NULL != extra_info && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
188 /* If the focus is changed by "Interrupt" mode and current players of "SR" and "Noti" modes are on going, please stop the current players. */
189 g_audio_state = AUDIO_STATE_READY;
191 if (NULL == g_playing_info) {
192 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
196 if (APP_STATE_PLAYING == g_playing_info->state) {
197 int uid = g_playing_info->uid;
199 if (0 != ttsd_server_stop(uid)) {
200 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to stop TTS server");
203 if (0 != ttsd_player_stop(uid)) {
204 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to stop the player");
208 ttsd_data_set_client_state(uid, APP_STATE_READY);
209 int pid = ttsd_data_get_pid(uid);
210 /* send message to client about changing state */
211 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
213 SLOG(LOG_DEBUG, tts_tag(), "[Player] Not playing state");
216 SLOG(LOG_DEBUG, tts_tag(), "[Player] This is not Interrupt mode or not playing state.");
222 static int __create_audio_out(ttse_audio_type_e type, int rate)
225 audio_sample_type_e sample_type;
227 if (TTSE_AUDIO_TYPE_RAW_S16 == type) {
228 sample_type = AUDIO_SAMPLE_TYPE_S16_LE;
230 sample_type = AUDIO_SAMPLE_TYPE_U8;
233 ret = audio_out_create_new(rate, AUDIO_CHANNEL_MONO, sample_type, &g_audio_h);
234 if (AUDIO_IO_ERROR_NONE != ret) {
235 g_audio_state = AUDIO_STATE_NONE;
237 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio");
240 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create audio");
244 g_sampling_rate = rate;
246 g_audio_state = AUDIO_STATE_READY;
251 static int __destroy_audio_out()
253 if (NULL == g_audio_h) {
254 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current handle is not valid");
259 ret = audio_out_destroy(g_audio_h);
260 if (AUDIO_IO_ERROR_NONE != ret) {
261 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to destroy audio");
264 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy audio");
270 g_audio_state = AUDIO_STATE_NONE;
276 static void __end_play_thread(void *data, Ecore_Thread *thread)
278 SLOG(LOG_ERROR, tts_tag(), "@@@ End thread");
281 static void __set_policy_for_playing(int volume)
283 /* Set stream info */
285 ttsd_mode_e mode = ttsd_get_mode();
286 if (TTSD_MODE_DEFAULT == mode) {
287 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
288 if (SOUND_MANAGER_ERROR_NONE != ret) {
289 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
291 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (default mode)");
293 } else if (TTSD_MODE_INTERRUPT == mode) {
294 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, "TTSD_MODE_INTERRUPT");
295 if (SOUND_MANAGER_ERROR_NONE != ret) {
296 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
298 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (interrupt mode)");
302 ret = audio_out_set_sound_stream_info(g_audio_h, g_stream_info_h);
303 if (AUDIO_IO_ERROR_NONE != ret) {
304 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to set stream info");
307 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] set policy for playing");
312 static void __unset_policy_for_playing()
315 ttsd_mode_e mode = ttsd_get_mode();
316 /* Unset stream info */
317 if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
318 sound_stream_focus_state_e state_for_playing = SOUND_STREAM_FOCUS_STATE_ACQUIRED;
319 ret = sound_manager_get_focus_state(g_stream_info_h, &state_for_playing, NULL);
320 if (SOUND_MANAGER_ERROR_NONE != ret) {
321 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to get focus state: %d", ret);
324 if (SOUND_STREAM_FOCUS_STATE_ACQUIRED == state_for_playing) {
325 if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
326 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
327 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release focus (mode: %d)", mode);
330 if (SOUND_MANAGER_ERROR_NONE != ret) {
331 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to release focus");
336 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] unset policy for playing");
341 int ttsd_player_check_current_playback_focus(bool *is_current_interrupt)
344 ttsd_mode_e mode = ttsd_get_mode();
346 if (TTSD_MODE_INTERRUPT != mode) {
347 /* check the current playback focus */
348 sound_stream_focus_change_reason_e reason;
349 int sound_behavior = 0;
350 char *extra_info = NULL;
352 ret = sound_manager_get_current_playback_focus(&reason, &sound_behavior, &extra_info);
354 SLOG(LOG_DEBUG, tts_tag(), "[Player] current playback focus: extra_info(%s), reason(%d), sound_behavior(%d)", extra_info, reason, sound_behavior);
356 if (SOUND_MANAGER_ERROR_NONE == ret && NULL != extra_info && 0 < strlen(extra_info)) {
357 if (SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
358 SLOG(LOG_DEBUG, tts_tag(), "[Player] The current focus in Interrupt. Cannot play the requested sound data");
359 *is_current_interrupt = true;
364 return TTSD_ERROR_NONE;
368 if (NULL != extra_info) {
374 *is_current_interrupt = false;
376 return TTSD_ERROR_NONE;
379 static void __play_thread(void *data, Ecore_Thread *thread)
381 SLOG(LOG_DEBUG, tts_tag(), "@@@ Start thread");
383 if (NULL == g_playing_info) {
384 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
388 player_s* player = g_playing_info;
389 sound_data_s* sound_data = NULL;
392 int len = SOUND_BUFFER_LENGTH;
395 /* set volume policy as 40% */
396 __set_policy_for_playing(40);
398 if (true == player->is_paused_data) {
400 sound_data = player->paused_data;
401 player->paused_data = NULL;
405 player->is_paused_data = false;
408 if (NULL == sound_data) {
409 /* Request unprepare */
410 ret = audio_out_unprepare(g_audio_h);
411 if (AUDIO_IO_ERROR_NONE != ret) {
412 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
414 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
417 g_audio_state = AUDIO_STATE_READY;
419 /* unset volume policy, volume will be 100% */
420 __unset_policy_for_playing();
423 SLOG(LOG_INFO, tts_tag(), "[Player] Sound info : id(%d) data(%p) size(%d) audiotype(%d) rate(%d) event(%d)",
424 sound_data->utt_id, sound_data->data, sound_data->data_size, sound_data->audio_type, sound_data->rate, sound_data->event);
427 ret = ttsd_data_get_sound_data(player->uid, &sound_data);
428 if (0 != ret || NULL == sound_data) {
430 SLOG(LOG_DEBUG, tts_tag(), "[Player] No sound data. Waiting mode");
432 /* wait for new audio data come */
435 if (NULL == g_playing_info) {
436 /* current playing uid is replaced */
437 SLOG(LOG_INFO, tts_tag(), "[Player] Finish thread");
438 if (AUDIO_STATE_PLAY == g_audio_state) {
439 /* release audio & recover session */
440 ret = audio_out_unprepare(g_audio_h);
441 if (AUDIO_IO_ERROR_NONE != ret) {
442 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
444 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
446 g_audio_state = AUDIO_STATE_READY;
448 /* unset volume policy, volume will be 100% */
449 __unset_policy_for_playing();
451 } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
452 /* new audio data come */
453 SLOG(LOG_INFO, tts_tag(), "[Player] Resume thread");
457 /* If engine is not on processing */
458 if (TTSD_SYNTHESIS_CONTROL_DOING != ttsd_get_synth_control()) {
459 if (AUDIO_STATE_PLAY == g_audio_state) {
460 /* release audio & recover session */
461 ret = audio_out_unprepare(g_audio_h);
462 if (AUDIO_IO_ERROR_NONE != ret) {
463 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
465 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
467 g_audio_state = AUDIO_STATE_READY;
469 /* unset volume policy, volume will be 100% */
470 __unset_policy_for_playing();
475 SLOG(LOG_INFO, tts_tag(), "[Player] Finish to wait for new audio data come");
477 if (AUDIO_STATE_READY == g_audio_state) {
478 /* set volume policy as 40%, when resume play thread*/
479 __set_policy_for_playing(40);
482 /* resume play thread */
483 player->state = APP_STATE_PLAYING;
487 /* If wdata's event is 'start', current wdata is first data of engine for synthesis.
488 * If wdata's event is 'finish', player should check previous event to know whether this wdata is first or not.
489 * When previous wdata's event is 'finish' and current wdata's event is 'finish',
490 * the player should send utt started event.
492 if (TTSE_RESULT_EVENT_START == sound_data->event ||
493 (TTSE_RESULT_EVENT_FINISH == player->event && TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
494 int pid = ttsd_data_get_pid(player->uid);
497 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
498 /* unset volume policy, volume will be 100% */
499 __unset_policy_for_playing();
504 if (0 != ttsdc_send_utt_start_message(pid, player->uid, sound_data->utt_id)) {
505 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Start Signal : pid(%d), uid(%d), uttid(%d)",
506 pid, player->uid, sound_data->utt_id);
508 SLOG(LOG_INFO, tts_tag(), "[Player] Start utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
511 /* Save last event to check utterance start */
512 player->event = sound_data->event;
515 if (NULL == sound_data->data || 0 >= sound_data->data_size) {
516 if (TTSE_RESULT_EVENT_FINISH == sound_data->event) {
517 SLOG(LOG_DEBUG, tts_tag(), "No sound data");
518 /* send utterence finish signal */
519 int pid = ttsd_data_get_pid(player->uid);
522 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
523 /* unset volume policy, volume will be 100% */
524 __unset_policy_for_playing();
527 if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
528 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
529 pid, player->uid, sound_data->utt_id);
532 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
533 ttsd_data_clear_sound_data(player->uid, &sound_data);
538 if (g_sampling_rate != sound_data->rate || g_audio_type != sound_data->audio_type) {
539 SLOG(LOG_INFO, tts_tag(), "[Player] Change audio handle : org type(%d) org rate(%d)", g_audio_type, g_sampling_rate);
540 if (NULL != g_audio_h) {
541 __destroy_audio_out();
544 if (0 > __create_audio_out(sound_data->audio_type, sound_data->rate)) {
545 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio out");
546 /* unset volume policy, volume will be 100% */
547 __unset_policy_for_playing();
549 ttsd_data_clear_sound_data(player->uid, &sound_data);
554 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Success to destroy and recreate audio out");
555 __set_policy_for_playing(40);
558 while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
559 if ((unsigned int)idx >= sound_data->data_size)
562 if ((unsigned int)idx + SOUND_BUFFER_LENGTH > sound_data->data_size) {
563 len = sound_data->data_size - idx;
565 len = SOUND_BUFFER_LENGTH;
568 if (AUDIO_STATE_READY == g_audio_state) {
569 /* Request prepare */
570 ret = audio_out_prepare(g_audio_h);
571 if (AUDIO_IO_ERROR_NONE != ret) {
572 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to prepare audio : %d", ret);
573 g_playing_info = NULL;
574 /* unset volume policy, volume will be 100% */
575 __unset_policy_for_playing();
577 ttsd_data_clear_sound_data(player->uid, &sound_data);
581 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
582 g_audio_state = AUDIO_STATE_PLAY;
585 char* temp_data = sound_data->data;
586 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Before audio_out_write. data(%p), data[%d](%p), uid(%d), utt_id(%d), len(%d)",
587 temp_data, idx, &temp_data[idx], player->uid, sound_data->utt_id, len);
588 ret = audio_out_write(g_audio_h, &temp_data[idx], len);
590 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
593 SLOG(LOG_INFO, tts_tag(), "[Player INFO] After audio_out_write");
596 if (NULL == g_playing_info && APP_STATE_PAUSED != player->state) {
597 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
598 g_audio_state = AUDIO_STATE_READY;
599 ret = audio_out_unprepare(g_audio_h);
600 if (AUDIO_IO_ERROR_NONE != ret) {
601 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
603 /* unset volume policy, volume will be 100% */
604 __unset_policy_for_playing();
606 ttsd_data_clear_sound_data(player->uid, &sound_data);
611 if (APP_STATE_PAUSED == player->state) {
613 player->paused_data = sound_data;
615 player->is_paused_data = true;
618 g_audio_state = AUDIO_STATE_READY;
619 SLOG(LOG_INFO, tts_tag(), "[Player] Stop player thread by pause");
621 /* Request prepare */
622 ret = audio_out_unprepare(g_audio_h);
623 if (AUDIO_IO_ERROR_NONE != ret) {
624 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
626 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
628 /* unset volume policy, volume will be 100% */
629 __unset_policy_for_playing();
634 if (NULL == g_playing_info && APP_STATE_READY == player->state) {
636 g_audio_state = AUDIO_STATE_READY;
637 SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread");
639 /* Request prepare */
640 ret = audio_out_unprepare(g_audio_h);
641 if (AUDIO_IO_ERROR_NONE != ret) {
642 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
644 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
647 ttsd_data_clear_sound_data(player->uid, &sound_data);
648 /* unset volume policy, volume will be 100% */
649 __unset_policy_for_playing();
653 if ((APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) &&
654 (TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
655 /* send utterence finish signal */
656 int pid = ttsd_data_get_pid(player->uid);
659 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
660 /* unset volume policy, volume will be 100% */
661 __unset_policy_for_playing();
666 if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
667 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
668 pid, player->uid, sound_data->utt_id);
669 /* unset volume policy, volume will be 100% */
670 __unset_policy_for_playing();
672 ttsd_data_clear_sound_data(player->uid, &sound_data);
676 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
679 ttsd_data_clear_sound_data(player->uid, &sound_data);
681 if (NULL == g_playing_info) {
682 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
683 g_audio_state = AUDIO_STATE_READY;
684 ret = audio_out_unprepare(g_audio_h);
685 if (AUDIO_IO_ERROR_NONE != ret) {
686 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
688 /* unset volume policy, volume will be 100% */
689 __unset_policy_for_playing();
699 int ttsd_player_init()
701 g_playing_info = NULL;
702 g_audio_state = AUDIO_STATE_NONE;
707 ecore_thread_max_set(1);
711 if (0 == access(FOCUS_SERVER_READY, F_OK)) {
712 SLOG(LOG_ERROR, tts_tag(), "[Player SUCCESS] focus server is available");
716 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] focus server is not available");
721 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_INFORMATION, __player_focus_state_cb, NULL, &g_stream_info_h);
722 if (SOUND_MANAGER_ERROR_NONE != ret) {
723 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create stream info");
726 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream info");
729 /* add sound focus state watch callback */
730 ret = sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_PLAYBACK, __player_focus_state_watch_cb, NULL, &g_focus_watch_id);
731 if (SOUND_MANAGER_ERROR_NONE != ret) {
732 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to add sound focus watch callback");
733 sound_manager_destroy_stream_information(g_stream_info_h);
736 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Add sound focus watch callback");
739 ecore_thread_max_set(1);
741 ret = __create_audio_out(TTSE_AUDIO_TYPE_RAW_S16, 16000);
743 sound_manager_destroy_stream_information(g_stream_info_h);
744 sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
748 g_player_init = true;
753 int ttsd_player_release(void)
755 if (false == g_player_init) {
756 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
757 return TTSD_ERROR_OPERATION_FAILED;
762 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
763 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
764 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
766 /* The thread should be released */
767 int thread_count = ecore_thread_active_get();
769 while (0 < thread_count) {
774 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
778 thread_count = ecore_thread_active_get();
781 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
783 ret = __destroy_audio_out();
787 ret = sound_manager_destroy_stream_information(g_stream_info_h);
788 if (SOUND_MANAGER_ERROR_NONE != ret) {
789 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream info");
791 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream info");
794 /* remove focus state watch callback */
795 ret = sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
796 if (SOUND_MANAGER_ERROR_NONE != ret) {
797 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to remove the focus state watch cb");
799 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Remove the focus state watch cb");
802 /* clear g_player_list */
803 g_playing_info = NULL;
804 g_player_init = false;
809 int ttsd_player_create_instance(int uid)
811 if (false == g_player_init) {
812 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
816 /* Check uid is duplicated */
817 if (NULL != __player_get_item(uid)) {
818 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is already registered", uid);
822 player_s* new_client = (player_s*)calloc(1, sizeof(player_s));
823 if (NULL == new_client) {
824 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to allocate memory");
825 return TTSE_ERROR_OUT_OF_MEMORY;
828 new_client->uid = uid;
829 new_client->event = TTSE_RESULT_EVENT_FINISH;
830 new_client->state = APP_STATE_READY;
831 new_client->is_paused_data = false;
833 new_client->paused_data = NULL;
835 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%d)", uid);
837 g_player_list = g_list_append(g_player_list, new_client);
842 int ttsd_player_destroy_instance(int uid)
844 if (false == g_player_init) {
845 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
850 current = __player_get_item(uid);
851 if (NULL == current) {
852 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
856 if (NULL != g_playing_info) {
857 if (uid == g_playing_info->uid) {
858 g_playing_info = NULL;
863 player_s *data = NULL;
865 if (0 < g_list_length(g_player_list)) {
866 /* Get a first item */
867 iter = g_list_first(g_player_list);
869 while (NULL != iter) {
870 /* Get handle data from list */
871 data = (player_s*)iter->data;
875 if (uid == data->uid) {
876 g_player_list = g_list_remove_link(g_player_list, iter);
884 iter = g_list_next(iter);
888 SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
893 int ttsd_player_play(int uid)
895 if (false == g_player_init) {
896 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
900 if (NULL != g_playing_info) {
901 if (uid == g_playing_info->uid) {
902 SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
905 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
906 ttsd_player_stop(g_playing_info->uid);
910 SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
912 /* Check sound queue size */
913 if (0 == ttsd_data_get_sound_data_size(uid)) {
914 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
920 current = __player_get_item(uid);
921 if (NULL == current) {
922 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
926 current->state = APP_STATE_PLAYING;
928 g_playing_info = current;
930 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
932 if (0 < ttsd_data_get_sound_data_size(current->uid)) {
933 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
934 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
940 int ttsd_player_stop(int uid)
942 if (false == g_player_init) {
943 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
949 current = __player_get_item(uid);
950 if (NULL == current) {
951 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
955 /* check whether uid is current playing or not */
956 if (NULL != g_playing_info) {
957 if (uid == g_playing_info->uid) {
958 /* release current playing info */
959 g_playing_info = NULL;
962 SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
965 if (true == current->is_paused_data) {
966 if (NULL != current->paused_data) {
967 if (NULL != current->paused_data->data) {
968 free(current->paused_data->data);
969 current->paused_data->data = NULL;
972 free(current->paused_data);
973 current->paused_data = NULL;
977 current->event = TTSE_RESULT_EVENT_FINISH;
978 current->state = APP_STATE_READY;
979 current->is_paused_data = false;
982 if (NULL == g_playing_info) {
983 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
984 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
985 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
987 /* The thread should be released */
988 int thread_count = ecore_thread_active_get();
990 while (0 < thread_count) {
995 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
999 thread_count = ecore_thread_active_get();
1002 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1003 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1004 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1007 SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
1012 int ttsd_player_clear(int uid)
1014 if (false == g_player_init) {
1015 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1021 current = __player_get_item(uid);
1022 if (NULL == current) {
1023 SECURE_SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1027 if (true == current->is_paused_data) {
1028 if (NULL != current->paused_data) {
1029 if (NULL != current->paused_data->data) {
1030 free(current->paused_data->data);
1031 current->paused_data->data = NULL;
1034 free(current->paused_data);
1035 current->paused_data = NULL;
1039 current->event = TTSE_RESULT_EVENT_FINISH;
1040 current->state = APP_STATE_READY;
1041 current->is_paused_data = false;
1044 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%d)", uid);
1049 int ttsd_player_pause(int uid)
1051 SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%d)", uid);
1053 if (false == g_player_init) {
1054 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1060 current = __player_get_item(uid);
1061 if (NULL == current) {
1062 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%d) is not valid", uid);
1066 /* check whether uid is current playing or not */
1067 if (NULL != g_playing_info) {
1068 if (uid == g_playing_info->uid) {
1069 /* release current playing info */
1070 g_playing_info = NULL;
1076 current->state = APP_STATE_PAUSED;
1078 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1079 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1080 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1082 /* The thread should be released */
1083 int thread_count = ecore_thread_active_get();
1085 while (0 < thread_count) {
1090 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
1094 thread_count = ecore_thread_active_get();
1097 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1098 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1099 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1101 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
1106 int ttsd_player_resume(int uid)
1108 SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%d)", uid);
1110 if (false == g_player_init) {
1111 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1117 current = __player_get_item(uid);
1118 if (NULL == current) {
1119 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1123 /* check current player */
1124 if (NULL != g_playing_info)
1125 g_playing_info = NULL;
1127 current->state = APP_STATE_PLAYING;
1128 g_playing_info = current;
1130 SLOG(LOG_INFO, tts_tag(), "[Player] Resume to run thread");
1131 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1136 int ttsd_player_all_stop()
1138 if (false == g_player_init) {
1139 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1143 g_playing_info = NULL;
1146 player_s *data = NULL;
1148 if (0 < g_list_length(g_player_list)) {
1149 /* Get a first item */
1150 iter = g_list_first(g_player_list);
1152 while (NULL != iter) {
1153 /* Get handle data from list */
1154 data = (player_s*)iter->data;
1156 app_tts_state_e state;
1157 if (0 > ttsd_data_get_client_state(data->uid, &state)) {
1158 SLOG(LOG_ERROR, tts_tag(), "[player ERROR] uid(%d) is not valid", data->uid);
1159 ttsd_player_destroy_instance(data->uid);
1160 iter = g_list_next(iter);
1164 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1165 data->event = TTSE_RESULT_EVENT_FINISH;
1166 data->state = APP_STATE_READY;
1168 if (true == data->is_paused_data) {
1169 if (NULL != data->paused_data) {
1170 if (NULL != data->paused_data->data) {
1171 free(data->paused_data->data);
1172 data->paused_data->data = NULL;
1175 free(data->paused_data);
1176 data->paused_data = NULL;
1180 data->is_paused_data = false;
1185 iter = g_list_next(iter);
1189 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
1194 int ttsd_player_play_pcm(int uid)
1196 if (false == g_player_init) {
1197 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1201 if (NULL != g_playing_info) {
1202 if (uid == g_playing_info->uid) {
1203 SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
1206 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
1207 ttsd_player_stop(g_playing_info->uid);
1211 SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
1213 /* Check sound queue size */
1214 if (0 == ttsd_data_get_sound_data_size(uid)) {
1215 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
1220 current = __player_get_item(uid);
1221 if (NULL == current) {
1222 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1226 current->state = APP_STATE_PLAYING;
1228 g_playing_info = current;
1230 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
1232 if (0 <= ttsd_data_get_sound_data_size(current->uid)) {
1233 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
1234 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);