2 * Copyright 2012-2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.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"
27 #include <mm_player.h>
28 #include <mm_session_private.h>
32 extern void worker_done();
34 /*==================================================================================================
36 ==================================================================================================*/
37 static MMHandleType hPlayerHandle = 0;
38 static bool bPlaying = false;
39 static bool bVibrating = false;
41 pthread_mutex_t muMmPlay = PTHREAD_MUTEX_INITIALIZER;
42 pthread_cond_t cvMmPlay = PTHREAD_COND_INITIALIZER;
44 /*==================================================================================================
45 FUNCTION IMPLEMENTATION
46 ==================================================================================================*/
48 static gboolean MsgStopAndExit(void* data)
51 if(!bPlaying && !bVibrating)
57 static gboolean MsgUninitAndExit(void* data)
60 if(!bPlaying && !bVibrating)
66 static gboolean MsgSoundMelodyTimeout(gpointer data)
71 if(!bPlaying && !bVibrating)
79 static int MsgSoundPlayCallback(int message, void *param, void *user_param)
83 case MM_MESSAGE_ERROR:
84 MSG_DEBUG("ERROR is happened.");
85 g_idle_add (MsgUninitAndExit, NULL);
87 case MM_MESSAGE_BEGIN_OF_STREAM:
88 MSG_DEBUG("Play is started.");
90 case MM_MESSAGE_END_OF_STREAM:
91 case MM_MESSAGE_STATE_INTERRUPTED:
92 MSG_DEBUG("EOS or Interrupted.");
93 g_idle_add (MsgStopAndExit, NULL);
96 MSG_DEBUG("message = %d", message);
104 void* MsgPlayThread(void *data)
108 bool bSoundOn = false;
109 bool bVibrationOn = false;
111 /* 0 - off, 1 - sound, 2 - vibration */
114 bool isEmergency = false;
117 isEmergency = (bool)data;
119 char *msg_tone_file_path = NULL;
120 AutoPtr<char> buf(&msg_tone_file_path);
122 msg_tone_file_path = new char[MAX_SOUND_FILE_LEN];
126 char *tmpFileFath = NULL;
128 tmpFileFath = MsgSettingGetString(VCONFKEY_SETAPPL_NOTI_MSG_RINGTONE_PATH_STR);
130 if (tmpFileFath == NULL || MsgGetFileSize(tmpFileFath) < 1) {
131 strncpy(msg_tone_file_path, DEFAULT_FILE, MAX_SOUND_FILE_LEN-1);
133 strncpy(msg_tone_file_path, tmpFileFath, MAX_SOUND_FILE_LEN-1);
139 MSG_DEBUG("Emergency=[%d], Sound File [%s]", isEmergency, msg_tone_file_path);
141 MsgSettingGetBool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &bSoundOn);
142 MsgSettingGetBool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &bVibrationOn);
144 int err = MM_ERROR_NONE;
147 err = mm_session_init(MM_SESSION_TYPE_EMERGENCY);
149 err = mm_session_init(MM_SESSION_TYPE_NOTIFY);
151 if(err != MM_ERROR_NONE)
152 MSG_DEBUG("MM Session Init Failed");
154 MSG_DEBUG("MM Session Init Success : %d", err);
158 err = mm_player_create(&hPlayerHandle);
160 if (err != MM_ERROR_NONE) {
161 MSG_DEBUG("creating the player handle failed");
165 /* Setting the call back function msg_callback */
166 mm_player_set_message_callback(hPlayerHandle, MsgSoundPlayCallback, (void *)hPlayerHandle);
168 callStatus = MsgSettingGetInt(VCONFKEY_CALL_STATE);
169 MSG_DEBUG("Call Status = %d", callStatus);
171 if (callStatus > VCONFKEY_CALL_OFF && callStatus < VCONFKEY_CALL_STATE_MAX) {
172 alertOnCall = MsgSettingGetInt(VCONFKEY_CISSAPPL_ALERT_ON_CALL_INT);
173 MSG_DEBUG("Alert On Call = %d", alertOnCall);
175 if (alertOnCall == 0) {
176 MSG_DEBUG("Call is active & Alert on Call - Off");
177 } else if (alertOnCall == 1) {
178 MSG_DEBUG("Call is active & Alert on Call - Sound");
182 } else if (alertOnCall == 2) {
183 MSG_DEBUG("Call is active & Alert on Call - Vibration");
186 MsgSoundPlayVibration(true);
189 MSG_DEBUG("Call is not active");
192 MSG_DEBUG("Play vibration.");
193 MsgSoundPlayVibration(false);
197 MSG_DEBUG("Play sound.");
198 MsgSoundPlayMelody(msg_tone_file_path, false);
202 err = mm_session_finish();
204 if (err != MM_ERROR_NONE)
205 MSG_DEBUG("MM Session Finish Failed");
207 MSG_DEBUG("MM Session Finish Success : %d", err);
209 if(!bPlaying && !bVibrating)
218 msg_error_t MsgSoundPlayUninit()
222 int err = MM_ERROR_NONE;
224 /* Uninitializing the player module */
225 err = mm_player_unrealize(hPlayerHandle);
227 /* Destroying the player handle */
228 err = mm_player_destroy(hPlayerHandle);
230 pthread_mutex_lock(&muMmPlay);
234 pthread_mutex_unlock(&muMmPlay);
236 pthread_cond_signal(&cvMmPlay);
246 void MsgSoundPlayStart(bool isEmergency)
250 pthread_mutex_lock(&muMmPlay);
252 if (bPlaying == true) {
253 MSG_DEBUG("Ringtone is Playing...");
254 pthread_mutex_unlock(&muMmPlay);
258 pthread_mutex_unlock(&muMmPlay);
262 if (pthread_create(&tid, NULL, &MsgPlayThread, (void*)isEmergency) == 0) {
263 MSG_DEBUG("Ring alert thread created = %d", tid);
265 MSG_DEBUG("Creating Thread was failed");
273 void MsgSoundPlayStop()
277 pthread_mutex_lock(&muMmPlay);
279 if (bPlaying == false) {
280 MSG_DEBUG("Ringtone is Not Playing...");
281 pthread_mutex_unlock(&muMmPlay);
285 pthread_mutex_unlock(&muMmPlay);
287 /* Stop playing media contents */
288 MSG_DEBUG("Before mm_player_stop, %p", hPlayerHandle);
290 int err = mm_player_stop(hPlayerHandle);
292 MSG_DEBUG("After mm_player_stop");
294 if (err != MM_ERROR_NONE) {
295 MSG_DEBUG("stopping the player handle failed");
298 MsgSoundPlayUninit();
304 int MsgSoundPlayMelody(char *pMsgToneFilePath, bool bIncreasing)
306 int err = MM_ERROR_NONE;
308 /* Setting fade in/out, Volume */
309 err = mm_player_set_attribute(hPlayerHandle, NULL, "sound_volume_type", MM_SOUND_VOLUME_TYPE_NOTIFICATION,
310 "profile_uri", pMsgToneFilePath, strlen(pMsgToneFilePath),
311 "sound_priority", 2, NULL);
313 if (err != MM_ERROR_NONE) {
314 MSG_DEBUG("error setting the profile attr");
318 err = mm_player_realize(hPlayerHandle);
320 if (err != MM_ERROR_NONE) {
321 MSG_DEBUG("mm_player_realize() error : [%d]", err);
325 /* Add Timer to stop playing after 5 sec. */
326 int g_contact_timer = -1;
327 g_contact_timer = g_timeout_add(5500, (GSourceFunc)MsgSoundMelodyTimeout, NULL);
329 err = mm_player_start(hPlayerHandle);
331 if (err != MM_ERROR_NONE) {
332 MSG_DEBUG("mm_player_start, FAIL [%x]", err);
340 pthread_mutex_lock(&muMmPlay);
344 MSG_DEBUG("Ring Alert Playing");
345 pthread_cond_wait(&cvMmPlay, &muMmPlay);
348 pthread_mutex_unlock(&muMmPlay);
350 MSG_DEBUG("Ring Alert Idle");
356 void MsgSoundPlayVibration(bool isOnCall)
362 ret = feedback_initialize();
364 if (ret != FEEDBACK_ERROR_NONE) {
365 MSG_DEBUG("Fail to feedback_initialize : [%d]", ret);
370 ret = feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_MESSAGE_ON_CALL);
372 ret = feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_MESSAGE);
374 if (ret != FEEDBACK_ERROR_NONE)
375 MSG_DEBUG("Fail to feedback_play_type");
377 ret = feedback_deinitialize();
379 if (ret != FEEDBACK_ERROR_NONE) {
380 MSG_DEBUG("Fail to feedback_deinitialize : [%d]", ret);
387 void MsgSoundPlayDtmf()
394 ret = mm_sound_play_tone(MM_SOUND_TONE_PROP_BEEP2, VOLUME_TYPE_SYSTEM, 1.0, 300, &hToneHandle);
397 MSG_DEBUG("play tone failed\n");
399 MSG_DEBUG("play tone success\n");
406 int MsgSoundGetUnreadMsgCnt()
411 unreadCnt = MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_SMS_STATE);
414 unreadCnt += MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_MMS_STATE);
416 MSG_DEBUG("unread count : [%d]", unreadCnt);