change mm player apis
[platform/core/messaging/msg-service.git] / externals / MsgSoundPlayer.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #include <wait.h>
18
19 #include "MsgCallStatusManager.h"
20 #include "MsgDebug.h"
21 #include "MsgCppTypes.h"
22 #include "MsgGconfWrapper.h"
23 #include "MsgSettingTypes.h"
24 #include "MsgDrmWrapper.h"
25 #include "MsgSensorWrapper.h"
26 #include "MsgUtilFile.h"
27 #include "MsgStorageTypes.h"
28 #include "MsgContact.h"
29 #include "MsgAlarm.h"
30 #include "MsgNotificationWrapper.h"
31 #include "MsgSoundPlayer.h"
32
33 #ifndef MSG_WEARABLE_PROFILE
34
35 #include <player.h>
36 #include <sound_manager.h>
37 #include <feedback.h>
38 #include <feedback-internal.h>
39
40 /*==================================================================================================
41                                     DEFINES
42 ==================================================================================================*/
43 #define MAX_SOUND_FILE_LEN 1024
44
45 #define DEFAULT_ALERT_FILE              "/opt/usr/share/settings/Alerts/Whistle.ogg"
46
47 #define HAPTIC_TEST_ITERATION 1
48 #define MSG_VIBRATION_INTERVAL 3000
49 #define MSG_SOUND_TIMEOUT 5500
50
51
52 /*==================================================================================================
53                                      VARIABLES
54 ==================================================================================================*/
55 player_h g_PlayerHandle = NULL;
56 sound_stream_info_h g_stream_info = NULL;
57
58 pthread_mutex_t muMmPlay = PTHREAD_MUTEX_INITIALIZER;
59 pthread_mutex_t muStream = PTHREAD_MUTEX_INITIALIZER;
60
61 #endif /* MSG_WEARABLE_PROFILE */
62
63 /*==================================================================================================
64                                      IMPLEMENTATION OF Functions
65 ==================================================================================================*/
66 void MsgSoundRepeatAlarmCB(int alarmId)
67 {
68         MSG_BEGIN();
69
70 #ifndef MSG_WEARABLE_PROFILE
71
72         MsgSoundPlayer::instance()->MsgSoundInitRepeatAlarm();
73
74 #endif /* MSG_WEARABLE_PROFILE */
75
76         MSG_END();
77         return;
78 }
79
80
81 void MsgSensorCBStop()
82 {
83         MSG_BEGIN();
84
85 #ifndef MSG_WEARABLE_PROFILE
86
87 #if 0
88         MsgSoundPlayer::instance()->MsgSoundPlayStop();
89 #else
90         MsgDeleteNotification(MSG_NOTI_TYPE_ALL, -1);
91
92         MsgRefreshNotification(MSG_NOTI_TYPE_ALL, false, MSG_ACTIVE_NOTI_TYPE_NONE);
93
94 #ifndef MSG_NOTI_INTEGRATION
95         MsgRefreshNotification(MSG_NOTI_TYPE_SIM, false, MSG_ACTIVE_NOTI_TYPE_NONE);
96 #endif
97
98 #endif
99
100 #endif /* MSG_WEARABLE_PROFILE */
101
102         MSG_END();
103 }
104
105 /*
106 static gboolean MsgSoundMelodyTimeout(gpointer data)
107 {
108         MSG_BEGIN();
109
110         MsgSoundPlayer::instance()->MsgSoundPlayStop();
111
112         MSG_END();
113
114         return FALSE;
115 }
116 */
117
118 #ifndef MSG_WEARABLE_PROFILE
119 static void MsgSoundPlayeErrorCallback(int error_code, void *user_data)
120 {
121         MSG_DEBUG("MsgSoundPlayeErrorCallback called [%d]", error_code);
122         MsgSoundPlayer::instance()->MsgStreamStop();
123         MsgSoundPlayer::instance()->MsgSoundPlayStart(NULL, MSG_SOUND_PLAY_DEFAULT);
124 }
125
126 static void MsgSoundPlayeCompletedCallback(void *user_data)
127 {
128         MSG_DEBUG("MsgSoundPlayeCompletedCallback called");
129         MsgSoundPlayer::instance()->MsgSoundPlayStop();
130 }
131
132 static void MsgSoundPlayeInterruptedCallback(player_interrupted_code_e code, void *user_data)
133 {
134         MSG_DEBUG("MsgSoundPlayeInterruptedCallback called [%d]", code);
135         MsgSoundPlayer::instance()->MsgSoundPlayStop();
136 }
137
138 static void MsgStreamFocusCallback(sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason_for_change, const char *additional_info, void *user_data)
139 {
140         MSG_DEBUG("MsgStreamFocusCallback called, reason_for_change [%d], additional_info [%s]", reason_for_change, additional_info);
141
142         sound_stream_focus_state_e playback_focus_state = SOUND_STREAM_FOCUS_STATE_ACQUIRED;
143
144         sound_manager_get_focus_state(stream_info, &playback_focus_state, NULL);
145         if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_RELEASED) {
146                 MSG_DEBUG("sound stream focus released");
147                 MsgSoundPlayer::instance()->MsgSoundPlayStop();
148         }
149 }
150 #endif
151
152 /*==================================================================================================
153                                      IMPLEMENTATION OF MsgSoundPlayer - Member Functions
154 ==================================================================================================*/
155 MsgSoundPlayer* MsgSoundPlayer::pInstance = NULL;
156
157
158 MsgSoundPlayer::MsgSoundPlayer()
159 {
160 #ifndef MSG_WEARABLE_PROFILE
161         bPlaying = false;
162         bVibrating = false;
163         bFeedbackInit = false;
164         g_alarmId = 0;
165
166         defaultRingtonePath = NULL;
167
168         defaultRingtonePath = MsgSettingGetString(VCONFKEY_SETAPPL_NOTI_RINGTONE_DEFAULT_PATH_STR);
169
170         if (defaultRingtonePath == NULL || MsgGetFileSize(defaultRingtonePath) < 1) {
171                 defaultRingtonePath = (char *)DEFAULT_ALERT_FILE;
172         }
173
174         MSG_DEBUG("defaultRingtonePath [%s]", defaultRingtonePath);
175
176         if (MsgSensorConnect() == MSG_SUCCESS) {
177                 if (MsgRegSensorCB(&MsgSensorCBStop) != MSG_SUCCESS) {
178                         MSG_DEBUG("Fail to MsgRegSensorCB.");
179                         MsgSensorDisconnect();
180                 }
181         } else {
182                 MSG_DEBUG("Fail to MsgSensorConnect.");
183         }
184 #endif /* MSG_WEARABLE_PROFILE */
185 }
186
187
188 MsgSoundPlayer::~MsgSoundPlayer()
189 {
190 }
191
192
193 MsgSoundPlayer* MsgSoundPlayer::instance()
194 {
195         if (!pInstance) {
196                 MSG_DEBUG("pInstance is NULL. Now creating instance.");
197                 pInstance = new MsgSoundPlayer();
198         }
199
200         return pInstance;
201 }
202
203
204 void MsgSoundPlayer::MsgGetRingtonePath(char *userRingtonePath, char **msg_tone_file_path_p)
205 {
206 #ifndef MSG_WEARABLE_PROFILE
207         MSG_RINGTONE_TYPE_T ringtoneType = (MSG_RINGTONE_TYPE_T)MsgSettingGetInt(MSG_SETTING_RINGTONE_TYPE);
208
209         MSG_DEBUG("Ringtone type = [%d]", ringtoneType);
210
211         if (ringtoneType == MSG_RINGTONE_TYPE_SILENT) {
212                 *msg_tone_file_path_p = NULL;
213                 return;
214         }
215
216         char *tmpFilePath = NULL;
217         *msg_tone_file_path_p = new char[MSG_FILEPATH_LEN_MAX+1];
218
219         char *msg_tone_file_path = *msg_tone_file_path_p;
220
221         bool bUserRingtone = userRingtonePath && userRingtonePath[0] != '\0';
222         if (bUserRingtone) {
223                 tmpFilePath = userRingtonePath;
224         } else {
225                 if (ringtoneType == MSG_RINGTONE_TYPE_DEFAULT) {
226                         tmpFilePath = MsgSettingGetString(VCONFKEY_SETAPPL_NOTI_MSG_RINGTONE_PATH_STR);
227                 } else {
228                         tmpFilePath = MsgSettingGetString(MSG_SETTING_RINGTONE_PATH);
229                 }
230         }
231
232         memset(msg_tone_file_path, 0x00, sizeof(char)*(MSG_FILEPATH_LEN_MAX+1));
233
234         if ((tmpFilePath == NULL || MsgGetFileSize(tmpFilePath) < 1) || /* wrong ringtone file path */
235                         (MsgDrmIsDrmFile(tmpFilePath) && !MsgDrmCheckRingtone(tmpFilePath))) { /* check DRM */
236                 if (tmpFilePath && (strcmp(tmpFilePath, "silent") == 0)) {
237                         MSG_DEBUG("Set ringtone to NONE(Silent)");
238                         msg_tone_file_path = NULL;
239                 } else {
240                         MSG_DEBUG("Set ringtone to defaultRingtonePath.");
241                         strncpy(msg_tone_file_path, defaultRingtonePath, MSG_FILEPATH_LEN_MAX);
242                 }
243         } else {
244                 MSG_DEBUG("Set ringtone to tmpFilePath.");
245                 snprintf(msg_tone_file_path, MSG_FILEPATH_LEN_MAX, "%s", tmpFilePath);
246         }
247
248         if (tmpFilePath && !bUserRingtone) {
249                 free(tmpFilePath);
250                 tmpFilePath = NULL;
251         }
252 #endif /* MSG_WEARABLE_PROFILE */
253 }
254
255 #ifndef MSG_WEARABLE_PROFILE
256 void MsgSoundPlayer::MsgGetPlayStatus(bool bOnCall, bool bSound, bool bVibration, bool bMsgSound, bool bMsgVibration, bool *bPlaySound, bool *bPlayVibration)
257 {
258         MSG_BEGIN();
259
260         /* samsung basic concept : feedback should follow profile setting.
261          * And if notification setting exist in msg app, then apply it prior to profile when profile either sound or vibration.
262          * But, Play sound when profile is vibration during call, if device does not support haptic */
263         if (bSound || bVibration) {
264                 bool bHantic_device = false;
265 #ifdef FEATURE_HAPTIC_ENABLE
266                 bHantic_device = true;
267 #endif
268                 if (bHantic_device || (bOnCall == false)) {
269                         if (bSound) {
270                                 if (bMsgSound) {
271                                         MSG_DEBUG("Play sound.");
272                                         *bPlaySound = true;
273                                 }
274
275                                 if (bMsgVibration) {
276                                         MSG_DEBUG("Play vibration.");
277                                         *bPlayVibration = true;
278                                 }
279                         } else {
280                                 if (bMsgVibration) {
281                                         MSG_DEBUG("Play vibration.");
282                                         *bPlayVibration = true;
283                                 }
284                         }
285                 } else { /* during call */
286                         if (bMsgSound || bMsgVibration) {
287                                 MSG_DEBUG("Play sound.");
288                                 *bPlaySound = true;
289                         }
290                 }
291         }
292 }
293 #endif /* MSG_WEARABLE_PROFILE */
294
295 void MsgSoundPlayer::MsgGetPlayStatus(bool bVoiceMail, bool *bPlaySound, bool *bPlayVibration, bool *bOnCall)
296 {
297         MSG_BEGIN();
298
299 #ifndef MSG_WEARABLE_PROFILE
300
301         if (!bPlaySound || !bPlayVibration || !bOnCall) {
302                 MSG_DEBUG("IN parameter is NULL.");
303                 return;
304         }
305
306         *bPlaySound = false;
307         *bPlayVibration = false;
308         *bOnCall = false;
309
310         /* Global setting */
311         bool bSoundOn = false; /* sound setting on notification panel */
312         bool bVibrationOn = false; /* vibration setting on notification panel */
313
314         MsgSettingGetBool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &bSoundOn);
315         MsgSettingGetBool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &bVibrationOn);
316
317         /* Alert setting */
318 #if 0   /* not used value */
319         bool bNotiVibrationOn = false; /* alert vibration */
320         MsgSettingGetBool(VCONFKEY_SETAPPL_VIBRATE_WHEN_NOTIFICATION_BOOL, &bNotiVibrationOn);
321 #endif
322
323         bool bMsgSettingNoti = true; /* Alert for message notification */
324         bool bMsgSettingVibration = false; /* vibration for message notification */
325
326         MSG_RINGTONE_TYPE_T ringtoneType = MSG_RINGTONE_TYPE_DEFAULT; /*sound type for message notification */
327         bool bMsgSettingSound = true;
328
329         MsgSettingGetBool(MSG_SETTING_VIBRATION, &bMsgSettingVibration);
330         MsgSettingGetBool(MSG_SETTING_NOTIFICATION, &bMsgSettingNoti);
331         ringtoneType = (MSG_RINGTONE_TYPE_T)MsgSettingGetInt(MSG_SETTING_RINGTONE_TYPE);
332         if (ringtoneType == MSG_RINGTONE_TYPE_SILENT)
333                 bMsgSettingSound = false;
334
335         MSG_SEC_DEBUG("Sound status : Sound mode[%d], Manner mode[%d]", bSoundOn, bVibrationOn);
336         MSG_SEC_DEBUG("Msg Setting : Noti Alert[%d], vibration[%d], sound[%d], ringtoneType[%d]", bMsgSettingNoti, bMsgSettingVibration, bMsgSettingSound, ringtoneType);
337
338         int callStatus = 0;
339 /*      int alertOnCall = 0; */
340
341         callStatus = MsgGetCallStatus();
342         MSG_DEBUG("Call Status [%d]", callStatus);
343
344         /* Check call status */
345         if (callStatus > 0 && callStatus < 3) {
346                 /* 1. On Call */
347                 *bOnCall = true; /* set call status; */
348         } else {
349                 /* 2. Call is not active */
350
351                 MSG_DEBUG("Call is not active.");
352                 int voiceRecording = MsgSettingGetInt(VCONFKEY_RECORDER_STATE);
353
354                 if (bVoiceMail) {       /* 2-1. Voice message */
355                         if (bMsgSettingNoti) {
356                                 MsgGetPlayStatus(false, bSoundOn, bVibrationOn, bMsgSettingSound, bMsgSettingVibration, bPlaySound, bPlayVibration);
357
358                         } else {
359                                 MSG_DEBUG("It doesn't play sound/vibration - voice message.");
360                         }
361                 } else {        /* 2-1. Normal message */
362                         if (bMsgSettingNoti) {
363                                 if (voiceRecording != VCONFKEY_RECORDER_STATE_RECORDING) {
364                                         MsgGetPlayStatus(false, bSoundOn, bVibrationOn, bMsgSettingSound, bMsgSettingVibration, bPlaySound, bPlayVibration);
365                                 } else {
366                                         MSG_DEBUG("It doesn't play sound/vibration.");
367                                 }
368                         } else {
369                                 MSG_DEBUG("It doesn't play sound/vibration.");
370                         }
371                 }
372         }
373
374 #endif /* MSG_WEARABLE_PROFILE */
375
376         MSG_END();
377 }
378
379
380 void MsgSoundPlayer::MsgSoundPlayStart(const MSG_ADDRESS_INFO_S *pAddrInfo, MSG_SOUND_TYPE_T soundType)
381 {
382         MSG_BEGIN();
383 #ifndef MSG_WEARABLE_PROFILE
384
385         MSG_DEBUG("soundType [%d]", soundType);
386
387         /* check camera state */
388         int cameraState = 0;    /* camera recording state */
389         cameraState = MsgSettingGetInt(VCONFKEY_CAMERA_STATE);
390         MSG_SEC_DEBUG("Camera state [%d]", cameraState);
391
392         if (cameraState == VCONFKEY_CAMERA_STATE_RECORDING) {
393                 MSG_END();
394                 return;
395         }
396
397 /* contacts-service is not used for gear */
398 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
399         /* get contact information */
400         MSG_CONTACT_INFO_S contactInfo;
401         memset(&contactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
402
403         if (pAddrInfo) {
404                 /* Get Contact Info */
405                 if (MsgGetContactInfo(pAddrInfo, &contactInfo) != MSG_SUCCESS) {
406                         MSG_DEBUG("MsgGetContactInfo() fail.");
407                 }
408         }
409 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
410         /* get ringtone file path */
411         char *msg_tone_file_path = NULL;
412         unique_ptr<char*, void(*)(char**)> buf(&msg_tone_file_path, unique_ptr_deleter);
413
414         if (soundType == MSG_SOUND_PLAY_EMERGENCY) {
415                 msg_tone_file_path = new char[MAX_SOUND_FILE_LEN+1];
416                 memset(msg_tone_file_path, 0x00, sizeof(char)*(MAX_SOUND_FILE_LEN+1));
417         } else if (soundType == MSG_SOUND_PLAY_DEFAULT) {
418                 msg_tone_file_path = new char[MAX_SOUND_FILE_LEN+1];
419                 memset(msg_tone_file_path, 0x00, sizeof(char)*(MAX_SOUND_FILE_LEN+1));
420                 strncpy(msg_tone_file_path, DEFAULT_ALERT_FILE, MAX_SOUND_FILE_LEN);
421         }
422 /* contacts-service is not used for gear */
423 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
424         else {
425                 MsgGetRingtonePath(contactInfo.alerttonePath, &msg_tone_file_path);
426         }
427 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
428         MSG_SEC_DEBUG("soundType [%d], Sound File [%s]", soundType, msg_tone_file_path);
429
430         /* get sound play status */
431         bool bPlaySound = false;
432         bool bPlayVibration = false;
433         bool bVoiceMsg = (soundType == MSG_SOUND_PLAY_VOICEMAIL)?true:false;
434         bool bOnCall = false;
435
436         MsgGetPlayStatus(bVoiceMsg, &bPlaySound, &bPlayVibration, &bOnCall);
437
438         MSG_SEC_DEBUG("sound=[%d], vibration=[%d], voice_msg?[%d], on_call?[%d]",
439                         bPlaySound, bPlayVibration, bVoiceMsg, bOnCall);
440
441         /* play sound */
442         if (bPlaySound) {
443                 int err = MsgStreamStart(soundType);
444
445                 if (err != SOUND_MANAGER_ERROR_NONE)
446                         MSG_DEBUG("MsgStreamStart() Failed : %d", err);
447                 else
448                         MsgSoundPlayMelody(msg_tone_file_path);
449         }
450
451 /* contacts-service is not used for gear */
452 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
453         /* play vibration */
454         if (bPlayVibration) {
455                 MsgSoundPlayVibration(contactInfo.vibrationPath);
456         }
457 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
458
459         /* For repeatition. */
460 /*      MsgSoundSetRepeatAlarm(); */
461
462 #endif /* MSG_WEARABLE_PROFILE */
463         MSG_END();
464 }
465
466
467 void MsgSoundPlayer::MsgSoundPlayStop()
468 {
469         MSG_BEGIN();
470 #ifndef MSG_WEARABLE_PROFILE
471
472         int err = 0;
473         pthread_mutex_lock(&muMmPlay);
474
475         if (bPlaying == true && g_PlayerHandle != NULL) {
476                 MSG_DEBUG("stopping the player.");
477                 /* Stop playing media contents */
478                 err = player_stop(g_PlayerHandle);
479
480                 if (err != PLAYER_ERROR_NONE)
481                         MSG_DEBUG("stopping the player handle failed");
482         }
483
484         if (g_PlayerHandle != NULL) {
485                 MSG_DEBUG("destroy the player.");
486
487                 player_unset_error_cb(g_PlayerHandle);
488                 player_unset_completed_cb(g_PlayerHandle);
489                 player_unset_interrupted_cb(g_PlayerHandle);
490                 player_unprepare(g_PlayerHandle);
491                 player_destroy(g_PlayerHandle);
492         }
493
494         g_PlayerHandle = NULL;
495         bPlaying = false;
496
497         pthread_mutex_unlock(&muMmPlay);
498
499         MsgStreamStop();
500
501 #endif /* MSG_WEARABLE_PROFILE */
502         MSG_END();
503 }
504
505
506 int MsgSoundPlayer::MsgStreamStart(MSG_SOUND_TYPE_T soundType)
507 {
508         MSG_BEGIN();
509         int err = 0;
510
511 #ifndef MSG_WEARABLE_PROFILE
512         pthread_mutex_lock(&muStream);
513
514         if (g_stream_info != NULL) {
515                 err = sound_manager_destroy_stream_information(g_stream_info);
516                 if (err != SOUND_MANAGER_ERROR_NONE)
517                         MSG_DEBUG("sound_manager_destroy_stream_information() Failed : %d", err);
518
519                 g_stream_info = NULL;
520         }
521
522         if (soundType == MSG_SOUND_PLAY_EMERGENCY)
523                 err = sound_manager_create_stream_information(SOUND_STREAM_TYPE_EMERGENCY, MsgStreamFocusCallback, NULL, &g_stream_info);
524         else
525                 err = sound_manager_create_stream_information(SOUND_STREAM_TYPE_NOTIFICATION, MsgStreamFocusCallback, NULL, &g_stream_info);
526
527         if (err != SOUND_MANAGER_ERROR_NONE) {
528                 MSG_DEBUG("sound_manager_create_stream_information() Failed : %d", err);
529                 pthread_mutex_unlock(&muStream);
530                 return err;
531         }
532
533         err = sound_manager_acquire_focus(g_stream_info, SOUND_STREAM_FOCUS_FOR_PLAYBACK, NULL);
534         pthread_mutex_unlock(&muStream);
535
536 #endif /* MSG_WEARABLE_PROFILE */
537         MSG_END();
538         return err;
539 }
540
541
542 void MsgSoundPlayer::MsgStreamStop()
543 {
544         MSG_BEGIN();
545 #ifndef MSG_WEARABLE_PROFILE
546         pthread_mutex_lock(&muStream);
547
548         if (g_stream_info != NULL) {
549                 int err = sound_manager_release_focus(g_stream_info, SOUND_STREAM_FOCUS_FOR_PLAYBACK, NULL);
550                 if (err != SOUND_MANAGER_ERROR_NONE)
551                         MSG_DEBUG("sound_manager_release_focus() Failed : %d", err);
552
553                 err = sound_manager_destroy_stream_information(g_stream_info);
554                 if (err != SOUND_MANAGER_ERROR_NONE)
555                         MSG_DEBUG("sound_manager_destroy_stream_information() Failed : %d", err);
556
557                 g_stream_info = NULL;
558         }
559         pthread_mutex_unlock(&muStream);
560
561 #endif /* MSG_WEARABLE_PROFILE */
562         MSG_END();
563 }
564
565
566 void MsgSoundPlayer::MsgSoundPlayMelody(char *pMsgToneFilePath)
567 {
568 #ifndef MSG_WEARABLE_PROFILE
569         int err = PLAYER_ERROR_NONE;
570
571         if (!pMsgToneFilePath) {
572                 MSG_DEBUG("Ringtone path is NULL");
573                 return;
574         }
575
576         pthread_mutex_lock(&muMmPlay);
577
578         if (g_stream_info == NULL) {
579                 MSG_DEBUG("g_stream_info is NULL");
580                 return;
581         }
582
583         if (g_PlayerHandle) {
584                 player_unset_error_cb(g_PlayerHandle);
585                 player_unset_completed_cb(g_PlayerHandle);
586                 player_unset_interrupted_cb(g_PlayerHandle);
587                 player_unprepare(g_PlayerHandle);
588                 player_destroy(g_PlayerHandle);
589         }
590
591         err = player_create(&g_PlayerHandle);
592
593         pthread_mutex_unlock(&muMmPlay);
594
595         if (err != PLAYER_ERROR_NONE) {
596                 MSG_DEBUG("creating the player handle failed");
597                 return;
598         }
599
600         /* Setting the call back function msg_callback */
601         player_set_error_cb(g_PlayerHandle, MsgSoundPlayeErrorCallback, NULL);
602         player_set_completed_cb(g_PlayerHandle, MsgSoundPlayeCompletedCallback, NULL);
603         player_set_interrupted_cb(g_PlayerHandle, MsgSoundPlayeInterruptedCallback, NULL);
604
605         player_set_audio_policy_info(g_PlayerHandle, g_stream_info);
606
607         do {
608                 err = player_set_uri(g_PlayerHandle, (const char *)pMsgToneFilePath);
609                 if (err != PLAYER_ERROR_NONE)
610                         MSG_DEBUG("player_set_uri() error : [%d]", err);
611
612                 err = player_prepare(g_PlayerHandle);
613                 if (err != PLAYER_ERROR_NONE) {
614                         MSG_DEBUG("player_prepare() error : [%d]", err);
615                         if (pMsgToneFilePath != defaultRingtonePath) {
616                                 pMsgToneFilePath = defaultRingtonePath;
617                         } else {
618                                 return;
619                         }
620                 }
621         } while (err != PLAYER_ERROR_NONE);
622
623
624         pthread_mutex_lock(&muMmPlay);
625         MSG_DEBUG("player_start with [%s]", pMsgToneFilePath);
626         err = player_start(g_PlayerHandle);
627
628         if (err != PLAYER_ERROR_NONE) {
629                 MSG_DEBUG("player_start, FAIL [%x]", err);
630         } else {
631                 /* Add Timer to stop playing after 5 sec. */
632                 /*
633                 int g_contact_timer = -1;
634                 g_contact_timer = g_timeout_add(MSG_SOUND_TIMEOUT, (GSourceFunc)MsgSoundMelodyTimeout, NULL);
635                 */
636
637                 bPlaying = true;
638         }
639         pthread_mutex_unlock(&muMmPlay);
640
641 #endif
642 }
643
644
645 void MsgSoundPlayer::MsgSoundPlayVibration(char *vibrationPath)
646 {
647         MSG_BEGIN();
648
649 #ifndef MSG_WEARABLE_PROFILE
650
651         int ret = 0;
652
653         if (!bFeedbackInit) {
654                 int ret = feedback_initialize();
655
656                 if (ret != FEEDBACK_ERROR_NONE) {
657                         MSG_DEBUG("Fail to feedback_initialize : [%d]", ret);
658                         bFeedbackInit = false;
659                         return;
660                 } else {
661                         MSG_DEBUG("Success to feedback_initialize.");
662                         bFeedbackInit = true;
663                 }
664         }
665
666         if (vibrationPath && strlen(vibrationPath)) {
667                 MSG_DEBUG("vibrationPath: [%s]", vibrationPath);
668                 ret = feedback_set_resource_path(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_MESSAGE, vibrationPath);
669                 if (ret != FEEDBACK_ERROR_NONE)
670                         MSG_DEBUG("Fail to feedback_set_resource_path");
671                 ret = feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_MESSAGE);
672
673                 if (ret != FEEDBACK_ERROR_NONE)
674                         MSG_DEBUG("Fail to feedback_play_type");
675         } else {
676                 ret = feedback_set_resource_path(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_MESSAGE, NULL);
677                 if (ret != FEEDBACK_ERROR_NONE)
678                         MSG_DEBUG("Fail to feedback_set_resource_path");
679
680                 ret = feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_MESSAGE);
681                 if (ret != FEEDBACK_ERROR_NONE)
682                         MSG_DEBUG("Fail to feedback_play_type");
683         }
684
685 #endif /* MSG_WEARABLE_PROFILE */
686
687         MSG_END();
688 }
689
690
691 void MsgSoundPlayer::MsgSoundSetRepeatAlarm()
692 {
693 #ifndef MSG_WEARABLE_PROFILE
694
695         int nRepeatValue = 0;
696         long    nRepeatTime = 0;
697
698         nRepeatValue = MsgSettingGetInt(MSG_ALERT_REP_TYPE);
699
700         switch (nRepeatValue) {
701         case MSG_ALERT_TONE_ONCE:
702                 nRepeatTime = 0;
703                 break;
704
705         case MSG_ALERT_TONE_2MINS:
706                 nRepeatTime = 2;
707                 break;
708
709         case MSG_ALERT_TONE_5MINS:
710                 nRepeatTime = 5;
711                 break;
712
713         case MSG_ALERT_TONE_10MINS:
714                 nRepeatTime = 10;
715                 break;
716
717         default:
718                 MSG_DEBUG("Invalid Repetition time");
719                 break;
720         }
721
722         MSG_DEBUG("nRepeatTime = %d", nRepeatTime);
723
724         if (nRepeatTime > 0) {
725                 if (g_alarmId > 0) {
726                         if (MsgAlarmRemove(g_alarmId) != MSG_SUCCESS)
727                                 MSG_FATAL("MsgAlarmRemove fail.");
728
729                         g_alarmId = 0;
730                 }
731                 MsgSoundCreateRepeatAlarm(nRepeatTime);
732         }
733
734 #endif /* MSG_WEARABLE_PROFILE */
735 }
736
737
738 void MsgSoundPlayer::MsgSoundCreateRepeatAlarm(int RepeatTime)
739 {
740         MSG_BEGIN();
741
742 #ifndef MSG_WEARABLE_PROFILE
743
744         int alarmId = 0;
745         time_t tmp_time;
746         struct tm repeat_tm;
747
748         time(&tmp_time);
749
750         tmp_time += (RepeatTime*60);
751         tzset();
752         localtime_r(&tmp_time, &repeat_tm);
753
754         if (MsgAlarmRegistration(&repeat_tm, MsgSoundRepeatAlarmCB, &alarmId) != MSG_SUCCESS) {
755                 MSG_DEBUG("MsgAlarmRegistration fail.");
756                 return;
757         }
758
759         g_alarmId = alarmId;
760         MSG_DEBUG("Set g_alarmId to [%d]", alarmId);
761
762         MSG_END();
763
764 #endif /* MSG_WEARABLE_PROFILE */
765 }
766
767
768 int MsgSoundPlayer::MsgSoundGetUnreadMsgCnt()
769 {
770         int unreadCnt = 0;
771
772 #ifndef MSG_WEARABLE_PROFILE
773
774         /* Get SMS Count */
775         unreadCnt = MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_SMS_STATE);
776
777         /* Get MMS Count */
778         unreadCnt += MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_MMS_STATE);
779
780         MSG_DEBUG("unread count : [%d]", unreadCnt);
781
782 #endif /* MSG_WEARABLE_PROFILE */
783
784         return unreadCnt;
785 }
786
787 void MsgSoundPlayer::MsgSoundInitRepeatAlarm()
788 {
789         MSG_BEGIN();
790
791 #ifndef MSG_WEARABLE_PROFILE
792
793         int nRepeatValue = 0;
794         long    nRepeatTime = 0;
795
796         g_alarmId = 0;
797
798         if (MsgSoundGetUnreadMsgCnt() <= 0) {
799                 MSG_DEBUG("no unread msg");
800                 return;
801         }
802
803         nRepeatValue = MsgSettingGetInt(MSG_ALERT_REP_TYPE);
804
805         switch (nRepeatValue) {
806         case MSG_ALERT_TONE_ONCE:
807                 nRepeatTime = 0;
808                 break;
809
810         case MSG_ALERT_TONE_2MINS:
811                 nRepeatTime = 2;
812                 break;
813
814         case MSG_ALERT_TONE_5MINS:
815                 nRepeatTime = 5;
816                 break;
817
818         case MSG_ALERT_TONE_10MINS:
819                 nRepeatTime = 10;
820                 break;
821
822         default:
823                 MSG_DEBUG("Invalid Repetition time");
824                 break;
825         }
826
827         MSG_DEBUG("nRepeatTime = %d", nRepeatTime);
828
829         if (nRepeatTime > 0)
830                 MsgSoundPlayStart(NULL, MSG_SOUND_PLAY_USER);
831
832 #endif /* MSG_WEARABLE_PROFILE */
833
834         MSG_END();
835 }