d724bd1d51288b9d37c5c0d5baaad6213cdabfeb
[apps/osp/Call.git] / src / CallSoundManager.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
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 /**
18  * @file    CallSettingsMoreOptionsForm.cpp
19  * @brief   Sound manager class
20  */
21 #include "CallSoundManager.h"
22
23 using namespace Tizen::Base;
24 using namespace Tizen::Graphics;
25 using namespace Tizen::Io;
26 using namespace Tizen::Media;
27 using namespace Tizen::Social;
28 using namespace Tizen::System;
29 using namespace Tizen::Base::Collection;
30 using namespace Tizen::Base::Runtime;
31
32 static const wchar_t* RINGTONE_PATH = L"/opt/usr/apps/zktdpemtmw/shared/data/ringtone_sdk.mp3";
33
34 SoundManager::SoundManager(void)
35         : __pSoundCallSession(null)
36         , __pVibrator(null)
37         , __pPlayer(null)
38         , __lastEndedConferenceCall(false)
39 {
40         __timer.Construct(*this);
41         __isSoundStatusOn = false;
42         __isVibrateStatusOn = false;
43 }
44
45 SoundManager::~SoundManager(void)
46 {
47         if(__pSoundCallSession != null)
48         {
49                 sound_manager_call_session_destroy(__pSoundCallSession);
50         }
51         if (__pVibrator != null)
52         {
53                 delete __pVibrator;
54         }
55         if (__pPlayer != null)
56         {
57                 delete __pPlayer;
58         }
59
60 }
61
62 void
63 SoundManager::SetlastEndedConferenceCall()
64 {
65         __lastEndedConferenceCall = true;
66 }
67
68 bool
69 SoundManager::GetLastConferenceCall()
70 {
71         return __lastEndedConferenceCall;
72 }
73
74 Timer*
75 SoundManager::GetTimer()
76 {
77         return &__timer;
78 }
79
80 result
81 SoundManager::StopSession(void)
82 {
83         AppLogDebug("Enter");
84         if(__pSoundCallSession != null)
85         {
86                 AppLogDebug("sound_manager_call_session_destroy");
87                 sound_manager_call_session_destroy(__pSoundCallSession);
88                 __pSoundCallSession = null;
89         }
90
91         return E_SUCCESS;
92 }
93
94 result
95 SoundManager::StartSession(void)
96 {
97         AppLogDebug("Enter");
98         if (__pSoundCallSession != null)
99         {
100                 sound_manager_call_session_set_mode(__pSoundCallSession,(sound_call_session_mode_e)SOUND_CALL_SESSION_MODE_VOICE);
101                 sound_manager_set_active_route((sound_route_e)SOUND_ROUTE_IN_MIC_OUT_RECEIVER);
102                 sound_manager_set_volume((sound_type_e)SOUND_TYPE_CALL,5);
103                 return E_SUCCESS;
104         }
105         int res = sound_manager_call_session_create((sound_call_session_type_e)SOUND_SESSION_TYPE_CALL, &__pSoundCallSession);
106         if (res != SOUND_MANAGER_ERROR_NONE)
107         {
108                 AppLogDebug("Sound manager creation failed");
109                 return E_FAILURE;
110         }
111         res = sound_manager_call_session_set_mode(__pSoundCallSession,(sound_call_session_mode_e)SOUND_CALL_SESSION_MODE_VOICE);
112         sound_manager_set_active_route((sound_route_e)SOUND_ROUTE_IN_MIC_OUT_RECEIVER);
113         //todo start listening for volume key and set the volume
114         sound_manager_set_volume((sound_type_e)SOUND_TYPE_CALL,5);
115         return E_SUCCESS;
116 }
117
118 result
119 SoundManager::SetSpeakerStatus(bool setSpeaker)
120 {
121         result r = E_FAILURE;
122         int res = -1;
123         bool isEarJackPresent = IsEarJackConnected();
124         sound_route_e soundRoute;
125         if (setSpeaker == true)
126         {
127                 soundRoute = SOUND_ROUTE_IN_MIC_OUT_SPEAKER;
128         }
129         else
130         {
131                 if (isEarJackPresent == true)
132                 {
133                         soundRoute = SOUND_ROUTE_IN_MIC_OUT_HEADPHONE;
134                 }
135                 else
136                 {
137                         soundRoute = SOUND_ROUTE_IN_MIC_OUT_RECEIVER;
138                 }
139         }
140         res = sound_manager_set_active_route(soundRoute);
141         if (res == SOUND_MANAGER_ERROR_NONE)
142         {
143                 r = E_SUCCESS;
144         }
145         else
146         {
147                 r = E_FAILURE;
148         }
149         return r;
150 }
151
152 result
153 SoundManager::SetSoundMode(SoundMode soundMode)
154 {
155         if (__pSoundCallSession == null)
156         {
157                 return E_FAILURE;
158         }
159         sound_call_session_mode_e sessionMode;
160         switch (soundMode)
161         {
162         case SOUND_MODE_RINGTONE:
163         {
164                 sessionMode = SOUND_CALL_SESSION_MODE_RINGTONE;
165                 break;
166         }
167         case SOUND_MODE_MEDIA:
168         {
169                 sessionMode = SOUND_CALL_SESSION_MODE_MEDIA;
170                 break;
171         }
172         case SOUND_MODE_VOICE:
173         {
174                 sessionMode = SOUND_CALL_SESSION_MODE_VOICE;
175         }
176         break;
177         default:
178         {
179                 sessionMode = SOUND_CALL_SESSION_MODE_RINGTONE;
180                 break;
181         }
182         }
183         int res = sound_manager_call_session_set_mode(__pSoundCallSession,sessionMode);
184         if (res != SOUND_MANAGER_ERROR_NONE)
185         {
186                 return E_FAILURE;
187         }
188         return E_SUCCESS;
189 }
190
191 void
192 SoundManager::OnTimerExpired(Timer& timer)
193 {
194         String reminderTonePath;
195         int unknownRejectStatus = -1;
196                 vconf_get_bool(VCONFKEY_CISSAPPL_MINUTE_MINDER_BOOL, &unknownRejectStatus);
197                 bool ReminderToneSetStatus = unknownRejectStatus;
198
199         if(ReminderToneSetStatus == true)
200         {
201
202                 if(__pPlayer == null)
203                 {
204                         __pPlayer =  new (std::nothrow) Player();
205                         __pPlayer->Construct(*this);
206                 }
207
208                 reminderTonePath.Append(RINGTONE_PATH);
209                 __pPlayer->OpenFile(reminderTonePath,false);
210                 __pPlayer->SetLooping(false);
211                 __pPlayer->SetVolume(80);
212                 SetSoundMode(SOUND_MODE_MEDIA);
213                 __pPlayer->Play();
214                 SetSoundMode(SOUND_MODE_VOICE);
215         }
216         /*else if(ReminderToneSetStatus == false)
217         {
218                 timer.Cancel();
219         }*/
220
221 }
222
223
224
225 void
226 SoundManager::SetDisconnectTone(void)
227 {
228
229         int unknownRejectStatus = -1;
230         String disconnectTonePath;
231         vconf_get_bool(VCONFKEY_CISSAPPL_CALL_END_TONE_BOOL, &unknownRejectStatus);
232         bool disconnectToneSetStatus = unknownRejectStatus;
233
234         if(disconnectToneSetStatus == true)
235         {
236                 disconnectTonePath.Append(RINGTONE_PATH);
237
238                 if(__pPlayer == null)
239                 {
240                         __pPlayer =  new (std::nothrow) Player();
241                         __pPlayer->Construct(*this);
242                 }
243
244                 if(disconnectTonePath.IsEmpty() == false)
245                 {
246                         __pPlayer->OpenFile(disconnectTonePath,false);
247                         __pPlayer->SetLooping(false);
248                         __pPlayer->SetVolume(80);
249                         SetSoundMode(SOUND_MODE_MEDIA);
250                         __pPlayer->Play();
251                 }
252
253         }
254
255 }
256
257 void
258 SoundManager::SetMinuteReminderTone()
259 {
260
261         int unknownRejectStatus = -1;
262         vconf_get_bool(VCONFKEY_CISSAPPL_MINUTE_MINDER_BOOL, &unknownRejectStatus);
263         bool reminderToneSetStatus = unknownRejectStatus;
264
265         if(reminderToneSetStatus == true)
266         {
267                 __timer.StartAsRepeatable(20000);
268         }
269
270 }
271
272
273 void
274 SoundManager::SetConnectTone()
275 {
276
277         result r = E_FAILURE;
278         String connectTonePath;
279         int unknownRejectStatus = -1;
280         vconf_get_bool(VCONFKEY_CISSAPPL_CALL_CONNECT_TONE_BOOL, &unknownRejectStatus);
281         bool connectToneSetStatus = unknownRejectStatus;
282
283         connectTonePath.Append(RINGTONE_PATH);
284
285         if(connectToneSetStatus == true)
286         {
287                 if(__pPlayer == null)
288                 {
289                         __pPlayer =  new (std::nothrow) Player();
290                         r = __pPlayer->Construct(*this);
291                 }
292
293                 if(connectTonePath.IsEmpty() == false)
294                 {
295                         r = __pPlayer->OpenFile(connectTonePath,false);
296                         r = __pPlayer->SetLooping(false);
297                         r = __pPlayer->SetVolume(80);
298                         r = SetSoundMode(SOUND_MODE_MEDIA);
299                         r = __pPlayer->Play();
300                 }
301         }
302 }
303
304 void
305 SoundManager::StartAlert(String& contactRingTone)
306 {
307         AppLogDebug("Enter");
308         if(__pSoundCallSession != null)
309         {
310                 AppLogDebug("No Alert already in call");
311                 return;
312         }
313         else
314         {
315                 StartSession();
316         }
317         result r = E_FAILURE;
318         if(__pVibrator == null)
319         {
320                 __pVibrator = new (std::nothrow) Vibrator();
321                 r = __pVibrator->Construct();
322         }
323         if(__pPlayer == null)
324         {
325                 __pPlayer =  new (std::nothrow) Player();
326                 r = __pPlayer->Construct(*this);
327         }
328         int retVal = -1;
329         vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &retVal);
330         __isSoundStatusOn = retVal;
331         vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &retVal);
332         __isVibrateStatusOn = retVal;
333         //todo: do only if call alert mode is set
334         if (__isSoundStatusOn == true)
335         {
336                 String ringTonePath;
337                 //Check if contact has any custom ringtone, else play defautl ringtone.
338                 if(contactRingTone.IsEmpty() == true)
339                 {
340                         char* pRingTonePtr = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR);
341                         AppLog("Ringtone Path : %s",pRingTonePtr);
342                         ringTonePath.Append(pRingTonePtr);
343                 }
344                 else
345                 {
346                         AppLog("Ringtone Path : %ls",contactRingTone.GetPointer());
347                         ringTonePath.Append(contactRingTone);
348                 }
349
350                 //Check if file is present
351                 File file;
352                 result res = file.Construct(ringTonePath, "r");
353                 if(res != E_SUCCESS )
354                 {
355                         ringTonePath.Clear();
356                         ringTonePath.Append(RINGTONE_PATH);
357                 }
358                 r = __pPlayer->OpenFile(ringTonePath,false);
359                 r = __pPlayer->SetLooping(true);
360                 r = __pPlayer->SetVolume(80);
361                 r = SetSoundMode(SOUND_MODE_RINGTONE);
362                 r = __pPlayer->Play();
363         }
364         if (__isVibrateStatusOn == true)
365         {
366                 //todo: Get the level from settings
367                 r = __pVibrator->Start(3000,1000,1000);
368         }
369         AppLogDebug("Exit");
370         return;
371 }
372
373 void
374 SoundManager::StopAlert(void)
375 {
376         AppLogDebug("Enter");
377         if (__isSoundStatusOn == true)
378         {
379                 //todo: stop player
380                 AppLogDebug("Stopping ring tone");
381                 __pPlayer->Stop();
382                 __pPlayer->Close();
383                 SetSoundMode(SOUND_MODE_VOICE);
384                 __isSoundStatusOn = false;
385                 StopSession();
386         }
387         if (__isVibrateStatusOn == true)
388         {
389                 __pVibrator->Stop();
390                 __isVibrateStatusOn = false;
391         }
392         AppLogDebug("Exit");
393 }
394
395 void
396 SoundManager::OnPlayerOpened(result r)
397 {
398 }
399
400 void
401 SoundManager::OnPlayerEndOfClip(void)
402 {
403 }
404
405 void
406 SoundManager::OnPlayerSeekCompleted(result r)
407 {
408 }
409
410 void
411 SoundManager::OnPlayerBuffering(int percent)
412 {
413 }
414
415 void
416 SoundManager::OnPlayerErrorOccurred(Tizen::Media::PlayerErrorReason r)
417 {
418 }
419
420 void
421 SoundManager::OnPlayerInterrupted(void)
422 {
423 }
424
425 void
426 SoundManager::OnPlayerReleased(void)
427 {
428 }
429
430 bool
431 SoundManager::IsEarJackConnected(void)
432 {
433         int earJackStatus = -1;
434         if (vconf_get_int(VCONFKEY_SYSMAN_EARJACK, &earJackStatus) == 0)
435         {
436                 if (earJackStatus == VCONFKEY_SYSMAN_EARJACK_REMOVED)
437                 {
438                         return false;
439                 }
440                 else
441                 {
442                         return true;
443                 }
444         }
445         else
446         {
447                 return false;
448         }
449 }