2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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
8 * http://www.tizenopensource.org/license
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.
20 #include "MsgCppTypes.h"
21 #include "MsgSettingTypes.h"
22 #include "MsgGconfWrapper.h"
23 #include "MsgUtilFile.h"
24 #include "MsgHelper.h"
26 #include <devman_managed.h>
30 #include <mm_player.h>
31 #include <mm_session_private.h>
34 extern void worker_done();
36 /*==================================================================================================
38 ==================================================================================================*/
39 static MMHandleType hPlayerHandle = 0;
40 static bool bPlaying = false;
41 static bool bVibrating = false;
42 static int dev_handle;
44 pthread_mutex_t muMmPlay = PTHREAD_MUTEX_INITIALIZER;
45 pthread_cond_t cvMmPlay = PTHREAD_COND_INITIALIZER;
47 /*==================================================================================================
48 FUNCTION IMPLEMENTATION
49 ==================================================================================================*/
51 static gboolean MsgStopAndExit(void* data)
54 if(!bPlaying && !bVibrating)
60 static gboolean MsgUninitAndExit(void* data)
63 if(!bPlaying && !bVibrating)
69 static gboolean MsgSoundMelodyTimeout(gpointer data)
74 if(!bPlaying && !bVibrating)
83 static gboolean MsgSoundVibTimeout(gpointer data)
89 if (bVibrating == true) {
90 ret = device_haptic_stop_play(dev_handle);
93 MSG_DEBUG("Fail to stop haptic : [%d]", ret);
96 ret = device_haptic_close(dev_handle);
99 MSG_DEBUG("Fail to close haptic : [%d]", ret);
105 if(!bPlaying && !bVibrating)
114 static int MsgSoundPlayCallback(int message, void *param, void *user_param)
118 case MM_MESSAGE_ERROR:
119 MSG_DEBUG("ERROR is happened.");
120 g_idle_add (MsgUninitAndExit, NULL);
122 case MM_MESSAGE_BEGIN_OF_STREAM:
123 MSG_DEBUG("Play is started.");
125 case MM_MESSAGE_END_OF_STREAM:
126 case MM_MESSAGE_STATE_INTERRUPTED:
127 MSG_DEBUG("EOS or Interrupted.");
128 g_idle_add (MsgStopAndExit, NULL);
131 MSG_DEBUG("message = %d", message);
139 void* MsgPlayThread(void *data)
143 bool bSoundOn = false;
144 bool bVibrationOn = false;
146 /* 0 - off, 1 - sound, 2 - vibration */
149 bool isEmergency = false;
152 isEmergency = (bool)data;
154 char *msg_tone_file_path = NULL;
155 AutoPtr<char> buf(&msg_tone_file_path);
157 msg_tone_file_path = new char[MAX_SOUND_FILE_LEN];
161 char *tmpFileFath = NULL;
163 tmpFileFath = MsgSettingGetString(VCONFKEY_SETAPPL_NOTI_MSG_RINGTONE_PATH_STR);
165 if (tmpFileFath == NULL || MsgGetFileSize(tmpFileFath) < 1) {
166 strncpy(msg_tone_file_path, DEFAULT_FILE, MAX_SOUND_FILE_LEN-1);
168 strncpy(msg_tone_file_path, tmpFileFath, MAX_SOUND_FILE_LEN-1);
174 MSG_DEBUG("Emergency=[%d], Sound File [%s]", isEmergency, msg_tone_file_path);
176 MsgSettingGetBool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &bSoundOn);
177 MsgSettingGetBool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &bVibrationOn);
179 int err = MM_ERROR_NONE;
182 err = mm_session_init(MM_SESSION_TYPE_EMERGENCY);
184 err = mm_session_init(MM_SESSION_TYPE_NOTIFY);
186 if(err != MM_ERROR_NONE)
187 MSG_DEBUG("MM Session Init Failed");
189 MSG_DEBUG("MM Session Init Success : %d", err);
193 err = mm_player_create(&hPlayerHandle);
195 if (err != MM_ERROR_NONE) {
196 MSG_DEBUG("creating the player handle failed");
200 /* Setting the call back function msg_callback */
201 mm_player_set_message_callback(hPlayerHandle, MsgSoundPlayCallback, (void *)hPlayerHandle);
203 callStatus = MsgSettingGetInt(VCONFKEY_CALL_STATE);
204 MSG_DEBUG("Call Status = %d", callStatus);
206 if (callStatus > VCONFKEY_CALL_OFF && callStatus < VCONFKEY_CALL_STATE_MAX) {
207 alertOnCall = MsgSettingGetInt(VCONFKEY_CISSAPPL_ALERT_ON_CALL_INT);
208 MSG_DEBUG("Alert On Call = %d", alertOnCall);
210 if (alertOnCall == 0) {
211 MSG_DEBUG("Call is active & Alert on Call - Off");
212 } else if (alertOnCall == 1) {
213 MSG_DEBUG("Call is active & Alert on Call - Sound");
217 } else if (alertOnCall == 2) {
218 MSG_DEBUG("Call is active & Alert on Call - Vibration");
221 MsgSoundPlayVibration();
224 MSG_DEBUG("Call is not active");
227 MSG_DEBUG("Play vibration.");
228 MsgSoundPlayVibration();
232 MSG_DEBUG("Play sound.");
233 MsgSoundPlayMelody(msg_tone_file_path, false);
237 err = mm_session_finish();
239 if (err != MM_ERROR_NONE)
240 MSG_DEBUG("MM Session Finish Failed");
242 MSG_DEBUG("MM Session Finish Success : %d", err);
244 if(!bPlaying && !bVibrating)
253 msg_error_t MsgSoundPlayUninit()
257 int err = MM_ERROR_NONE;
259 /* Uninitializing the player module */
260 err = mm_player_unrealize(hPlayerHandle);
262 /* Destroying the player handle */
263 err = mm_player_destroy(hPlayerHandle);
265 pthread_mutex_lock(&muMmPlay);
269 pthread_mutex_unlock(&muMmPlay);
271 pthread_cond_signal(&cvMmPlay);
281 void MsgSoundPlayStart(bool isEmergency)
285 pthread_mutex_lock(&muMmPlay);
287 if (bPlaying == true) {
288 MSG_DEBUG("Ringtone is Playing...");
289 pthread_mutex_unlock(&muMmPlay);
293 pthread_mutex_unlock(&muMmPlay);
297 if (pthread_create(&tid, NULL, &MsgPlayThread, (void*)isEmergency) == 0) {
298 MSG_DEBUG("Ring alert thread created = %d", tid);
300 MSG_DEBUG("Creating Thread was failed");
308 void MsgSoundPlayStop()
312 pthread_mutex_lock(&muMmPlay);
314 if (bPlaying == false) {
315 MSG_DEBUG("Ringtone is Not Playing...");
316 pthread_mutex_unlock(&muMmPlay);
320 pthread_mutex_unlock(&muMmPlay);
322 /* Stop playing media contents */
323 MSG_DEBUG("Before mm_player_stop, %p", hPlayerHandle);
325 int err = mm_player_stop(hPlayerHandle);
327 MSG_DEBUG("After mm_player_stop");
329 if (err != MM_ERROR_NONE) {
330 MSG_DEBUG("stopping the player handle failed");
333 MsgSoundPlayUninit();
339 int MsgSoundPlayMelody(char *pMsgToneFilePath, bool bIncreasing)
341 int err = MM_ERROR_NONE;
343 /* Setting fade in,out */
344 err = mm_player_set_attribute(hPlayerHandle, NULL, "sound_priority", 2, NULL);
346 if (err != MM_ERROR_NONE) {
347 MSG_DEBUG("error setting the profile attr");
351 /* Setting the Volume */
352 err = mm_player_set_attribute(hPlayerHandle, NULL, "sound_volume_type", MM_SOUND_VOLUME_TYPE_NOTIFICATION,
353 "profile_uri", pMsgToneFilePath, strlen(pMsgToneFilePath), NULL);
355 if (err != MM_ERROR_NONE) {
356 MSG_DEBUG("error setting the profile attr");
360 err = mm_player_realize(hPlayerHandle);
362 if (err != MM_ERROR_NONE) {
363 MSG_DEBUG("mm_player_realize() error : [%d]", err);
367 /* Add Timer to stop playing after 5 sec. */
368 int g_contact_timer = -1;
369 g_contact_timer = g_timeout_add(5500, (GSourceFunc)MsgSoundMelodyTimeout, NULL);
371 err = mm_player_start(hPlayerHandle);
373 if (err != MM_ERROR_NONE) {
374 MSG_DEBUG("mm_player_start, FAIL [%x]", err);
382 pthread_mutex_lock(&muMmPlay);
386 MSG_DEBUG("Ring Alert Playing");
387 pthread_cond_wait(&cvMmPlay, &muMmPlay);
390 pthread_mutex_unlock(&muMmPlay);
392 MSG_DEBUG("Ring Alert Idle");
398 void MsgSoundPlayVibration()
404 char ivtFilePath[MAX_SOUND_FILE_LEN] = {0,};
406 vibLevel = MsgSettingGetInt(VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT);
411 dev_handle = device_haptic_open(DEV_IDX_0, 0);
413 g_timeout_add(MSG_VIBRATION_INTERVAL , MsgSoundVibTimeout, NULL);
415 /* set timer to stop vibration, then play melody */
416 svi_get_path(SVI_TYPE_VIB, SVI_VIB_NOTIFICATION_MESSAGE, ivtFilePath, sizeof(ivtFilePath));
417 ret = device_haptic_play_file(dev_handle, ivtFilePath, HAPTIC_TEST_ITERATION, vibLevel);
420 MSG_DEBUG("Fail to play haptic : [%d]", ret);
427 void MsgSoundPlayDtmf()
434 ret = mm_sound_play_tone(MM_SOUND_TONE_PROP_BEEP2, VOLUME_TYPE_SYSTEM, 1.0, 300, &hToneHandle);
437 MSG_DEBUG("play tone failed\n");
439 MSG_DEBUG("play tone success\n");
446 int MsgSoundGetUnreadMsgCnt()
451 unreadCnt = MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_SMS_STATE);
454 unreadCnt += MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_MMS_STATE);
456 MSG_DEBUG("unread count : [%d]", unreadCnt);