e3ca1566b5b520c094a8c768efd0919a29661bd7
[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 static const wchar_t* CALLCONNECTTONE_PATH = L"/opt/usr/apps/zktdpemtmw/shared/data/Call_Connect.ogg";
34 static const wchar_t* CALLDISCONNECTTONE_PATH = L"/opt/usr/apps/zktdpemtmw/shared/data/Call_Disconnect.ogg";
35 static const wchar_t* MINUTEREMINDERTONE_PATH = L"/opt/usr/apps/zktdpemtmw/shared/data/Call_RecordStart.wav";
36 static const wchar_t* CALLWAITINGTONE_PATH = L"/opt/usr/apps/zktdpemtmw/shared/data/Call_WaitingTone.wav";
37
38
39 SoundManager::SoundManager(void)
40         : __pSoundCallSession(null)
41         , __pVibrator(null)
42         , __pPlayer(null)
43         , __lastEndedConferenceCall(false)
44         , __pConnectPlayer(null)
45         , __pDisconnectPlayer(null)
46         , __pReminderPlayer(null)
47         , __pWaitingTonePlayer(null)
48 {
49         __timer.Construct(*this);
50         __isSoundStatusOn = false;
51         __isVibrateStatusOn = false;
52 }
53
54 SoundManager::~SoundManager(void)
55 {
56         if(__pSoundCallSession != null)
57         {
58                 sound_manager_call_session_destroy(__pSoundCallSession);
59         }
60         if (__pVibrator != null)
61         {
62                 delete __pVibrator;
63                 __pVibrator = null;
64         }
65         if (__pPlayer != null)
66         {
67                 delete __pPlayer;
68                 __pPlayer = null;
69         }
70         if(__pConnectPlayer != null)
71         {
72                 delete __pConnectPlayer;
73                 __pConnectPlayer = null;
74         }
75         if(__pDisconnectPlayer != null)
76         {
77                 delete __pDisconnectPlayer;
78                 __pDisconnectPlayer = null;
79         }
80         if(__pReminderPlayer != null)
81         {
82                 delete __pReminderPlayer;
83                 __pReminderPlayer = null;
84         }
85         if(__pWaitingTonePlayer != null)
86         {
87                 delete __pWaitingTonePlayer;
88                 __pWaitingTonePlayer = null;
89         }
90
91 }
92
93 void
94 SoundManager::SetlastEndedConferenceCall()
95 {
96         __lastEndedConferenceCall = true;
97 }
98
99 bool
100 SoundManager::GetLastConferenceCall()
101 {
102         return __lastEndedConferenceCall;
103 }
104
105 Timer*
106 SoundManager::GetTimer()
107 {
108         return &__timer;
109 }
110
111 result
112 SoundManager::StopSession(void)
113 {
114         AppLogDebug("Enter");
115         if(__pSoundCallSession != null)
116         {
117                 AppLogDebug("sound_manager_call_session_destroy");
118                 sound_manager_call_session_destroy(__pSoundCallSession);
119                 __pSoundCallSession = null;
120         }
121
122         return E_SUCCESS;
123 }
124
125 result
126 SoundManager::StartSession(void)
127 {
128         AppLogDebug("Enter");
129         if (__pSoundCallSession != null)
130         {
131                 sound_manager_call_session_set_mode(__pSoundCallSession,(sound_call_session_mode_e)SOUND_CALL_SESSION_MODE_VOICE);
132                 sound_manager_set_active_route((sound_route_e)SOUND_ROUTE_IN_MIC_OUT_RECEIVER);
133                 sound_manager_set_volume((sound_type_e)SOUND_TYPE_CALL,5);
134                 return E_SUCCESS;
135         }
136         int res = sound_manager_call_session_create((sound_call_session_type_e)SOUND_SESSION_TYPE_CALL, &__pSoundCallSession);
137         if (res != SOUND_MANAGER_ERROR_NONE)
138         {
139                 AppLogDebug("Sound manager creation failed");
140                 return E_FAILURE;
141         }
142         res = sound_manager_call_session_set_mode(__pSoundCallSession,(sound_call_session_mode_e)SOUND_CALL_SESSION_MODE_VOICE);
143         sound_manager_set_active_route((sound_route_e)SOUND_ROUTE_IN_MIC_OUT_RECEIVER);
144         //todo start listening for volume key and set the volume
145         sound_manager_set_volume((sound_type_e)SOUND_TYPE_CALL,5);
146         return E_SUCCESS;
147 }
148
149 result
150 SoundManager::SetSpeakerStatus(bool setSpeaker)
151 {
152         result r = E_FAILURE;
153         int res = -1;
154         bool isEarJackPresent = IsEarJackConnected();
155         sound_route_e soundRoute;
156         if (setSpeaker == true)
157         {
158                 soundRoute = SOUND_ROUTE_IN_MIC_OUT_SPEAKER;
159         }
160         else
161         {
162                 if (isEarJackPresent == true)
163                 {
164                         soundRoute = SOUND_ROUTE_IN_MIC_OUT_HEADPHONE;
165                 }
166                 else
167                 {
168                         soundRoute = SOUND_ROUTE_IN_MIC_OUT_RECEIVER;
169                 }
170         }
171         res = sound_manager_set_active_route(soundRoute);
172         if (res == SOUND_MANAGER_ERROR_NONE)
173         {
174                 r = E_SUCCESS;
175         }
176         else
177         {
178                 r = E_FAILURE;
179         }
180         return r;
181 }
182
183 result
184 SoundManager::SetSoundMode(SoundMode soundMode)
185 {
186         if (__pSoundCallSession == null)
187         {
188                 return E_FAILURE;
189         }
190         sound_call_session_mode_e sessionMode;
191         switch (soundMode)
192         {
193         case SOUND_MODE_RINGTONE:
194         {
195                 sessionMode = SOUND_CALL_SESSION_MODE_RINGTONE;
196                 break;
197         }
198         case SOUND_MODE_MEDIA:
199         {
200                 sessionMode = SOUND_CALL_SESSION_MODE_MEDIA;
201                 break;
202         }
203         case SOUND_MODE_VOICE:
204         {
205                 sessionMode = SOUND_CALL_SESSION_MODE_VOICE;
206         }
207         break;
208         default:
209         {
210                 sessionMode = SOUND_CALL_SESSION_MODE_RINGTONE;
211                 break;
212         }
213         }
214         int res = sound_manager_call_session_set_mode(__pSoundCallSession,sessionMode);
215         if (res != SOUND_MANAGER_ERROR_NONE)
216         {
217                 return E_FAILURE;
218         }
219         return E_SUCCESS;
220 }
221
222 void
223 SoundManager::OnTimerExpired(Timer& timer)
224 {
225         String reminderTonePath;
226         int unknownRejectStatus = -1;
227         vconf_get_bool(VCONFKEY_CISSAPPL_MINUTE_MINDER_BOOL, &unknownRejectStatus);
228         bool ReminderToneSetStatus = unknownRejectStatus;
229         result res = E_FAILURE;
230         reminderTonePath.Append(MINUTEREMINDERTONE_PATH);
231
232         if(ReminderToneSetStatus == true)
233         {
234
235                 if(__pReminderPlayer == null)
236                 {
237
238                         if(reminderTonePath.IsEmpty() == false)
239                         {
240                                 __pReminderPlayer =  new (std::nothrow) Player();
241                                 res = __pReminderPlayer->Construct(*this);
242                                 if(res != E_SUCCESS)
243                                 {
244                                         AppLogDebug("__pReminderPlayer Construct failed with %d",res);
245                                         return;
246                                 }
247                                 res = __pReminderPlayer->OpenFile(reminderTonePath,false);
248                                 if(res != E_SUCCESS)
249                                 {
250                                         AppLogDebug("__pReminderPlayer OpenFile failed with %d",res);
251                                         return;
252                                 }
253                                 res = __pReminderPlayer->SetLooping(false);
254                                 if(res != E_SUCCESS)
255                                 {
256                                         AppLogDebug("__pReminderPlayer SetLooping failed with %d",res);
257                                         return;
258                                 }
259                                 res = __pReminderPlayer->SetVolume(80);
260                                 if(res != E_SUCCESS)
261                                 {
262                                         AppLogDebug("__pReminderPlayer SetVolume failed with %d",res);
263                                         return;
264                                 }
265                         }
266                 }
267
268                 if(__pReminderPlayer != null)
269                 {
270                         SetSoundMode(SOUND_MODE_MEDIA);
271                         res = __pReminderPlayer->Play();
272                         if(res != E_SUCCESS)
273                         {
274                                 AppLogDebug("__pReminderPlayer Play failed with %d",res);
275                                 return;
276                         }
277                         SetSoundMode(SOUND_MODE_VOICE);
278                 }
279         }
280         /*else if(ReminderToneSetStatus == false)
281         {
282                 timer.Cancel();
283         }*/
284
285 }
286
287
288
289 void
290 SoundManager::SetDisconnectTone(void)
291 {
292
293         AppLogDebug("Enter");
294         int unknownRejectStatus = -1;
295         String disconnectTonePath=L"";
296         vconf_get_bool(VCONFKEY_CISSAPPL_CALL_END_TONE_BOOL, &unknownRejectStatus);
297         bool disconnectToneSetStatus = unknownRejectStatus;
298         result res = E_FAILURE;
299
300         if(disconnectToneSetStatus == true)
301         {
302                 AppLogDebug("disconnectToneSetStatus == true");
303                 disconnectTonePath.Append(CALLDISCONNECTTONE_PATH);
304                 if(disconnectTonePath.IsEmpty() == false)
305                 {
306                         if(__pDisconnectPlayer == null)
307                         {
308                                 AppLogDebug("__pConnectPlayer == null");
309                                 __pDisconnectPlayer =  new (std::nothrow) Player();
310                                 res = __pDisconnectPlayer->Construct(*this);
311                                 if(res != E_SUCCESS)
312                                 {
313                                         AppLogDebug("__pDisconnectPlayer Construct failed with %d",res);
314                                         return;
315                                 }
316                                 res = __pDisconnectPlayer->OpenFile(disconnectTonePath,false);
317                                 if(res != E_SUCCESS)
318                                 {
319                                         AppLogDebug("__pDisconnectPlayer OpenFile failed with %d",res);
320                                         return;
321                                 }
322                                 res = __pDisconnectPlayer->SetLooping(false);
323                                 if(res != E_SUCCESS)
324                                 {
325                                         AppLogDebug("__pDisconnectPlayer SetLooping failed with %d",res);
326                                         return;
327                                 }
328                                 res = __pDisconnectPlayer->SetVolume(80);
329                                 if(res != E_SUCCESS)
330                                 {
331                                         AppLogDebug("__pDisconnectPlayer SetLooping SetVolume with %d",res);
332                                         return;
333                                 }
334
335                         }
336                 }
337
338                 if(__pDisconnectPlayer != null)
339                 {
340                         AppLogDebug("__pDisconnectPlayer OpenFile %ls",disconnectTonePath.GetPointer());
341                         SetSoundMode(SOUND_MODE_MEDIA);
342                         AppLogDebug("__pDisconnectPlayer SetVolume");
343                         if(__pDisconnectPlayer->GetState() == PLAYER_STATE_PLAYING)
344                         {
345                                 __pDisconnectPlayer->Stop();
346                         }
347                         res = __pDisconnectPlayer->Play();
348                         if(res != E_SUCCESS)
349                         {
350                                 AppLogDebug("__pDisconnectPlayer Play with %d",res);
351                                 return;
352                         }
353                 }
354
355         }
356
357
358
359
360         AppLogDebug("Exit");
361
362 }
363
364 void
365 SoundManager::SetMinuteReminderTone(void)
366 {
367         __timer.StartAsRepeatable(60000);
368 }
369
370
371 void
372 SoundManager::SetConnectTone(void)
373 {
374
375         String connectTonePath;
376         int unknownRejectStatus = -1;
377         vconf_get_bool(VCONFKEY_CISSAPPL_CALL_CONNECT_TONE_BOOL, &unknownRejectStatus);
378         bool connectToneSetStatus = unknownRejectStatus;
379         result res = E_FAILURE;
380
381         connectTonePath.Append(CALLCONNECTTONE_PATH);
382
383         if(connectToneSetStatus == true)
384         {
385
386                 if(__pConnectPlayer == null)
387                 {
388                         if (connectTonePath.IsEmpty() == false)
389                         {
390                                 __pConnectPlayer =  new (std::nothrow) Player();
391                                 res = __pConnectPlayer->Construct(*this);
392                                 if(res != E_SUCCESS)
393                                 {
394                                         AppLogDebug("__pConnectPlayer Construct failed with %d",res);
395                                         return;
396                                 }
397                                 res = __pConnectPlayer->OpenFile(connectTonePath,false);
398                                 if(res != E_SUCCESS)
399                                 {
400                                         AppLogDebug("__pConnectPlayer OpenFile failed with %d",res);
401                                         return;
402                                 }
403                                 res = __pConnectPlayer->SetLooping(false);
404                                 if(res != E_SUCCESS)
405                                 {
406                                         AppLogDebug("__pConnectPlayer SetLooping failed with %d",res);
407                                         return;
408                                 }
409                                 res = __pConnectPlayer->SetVolume(80);
410                                 if(res != E_SUCCESS)
411                                 {
412                                         AppLogDebug("__pConnectPlayer SetVolume failed with %d",res);
413                                         return;
414                                 }
415                         }
416                 }
417
418                 if(__pConnectPlayer != null)
419                 {
420                         res = SetSoundMode(SOUND_MODE_MEDIA);
421                         AppLogDebug("__pConnectPlayer Playing connect tone");
422                         res = __pConnectPlayer->Play();
423                         if(res != E_SUCCESS)
424                         {
425                                 AppLogDebug("__pConnectPlayer Play failed with %d",res);
426                                 return;
427                         }
428                 }
429
430         }
431
432
433 }
434
435 void
436 SoundManager::SetWaitTone(void)
437 {
438         CallAlertStatus status= CALL_ALERT_SOUND;
439         int alertStatus = -1;
440         result res = E_FAILURE;
441         int retVal = vconf_get_int(VCONFKEY_CISSAPPL_ALERT_ON_CALL_INT, &alertStatus);
442         if (retVal == 0)
443         {
444                 status = (CallAlertStatus) alertStatus;
445         }
446         AppLogDebug("No Alert already in call");
447         if(status == CALL_ALERT_SOUND)
448         {
449                 AppLogDebug("Playing alert tone");
450                 String waitingTonePath;
451                 waitingTonePath.Append(CALLWAITINGTONE_PATH);
452                 if(__pWaitingTonePlayer == null)
453                 {
454                         if (waitingTonePath.IsEmpty() == false)
455                         {
456                                 __pWaitingTonePlayer =  new (std::nothrow) Player();
457                                 res = __pWaitingTonePlayer->Construct(*this);
458                                 if(res != E_SUCCESS)
459                                 {
460                                         AppLogDebug("__pWaitingTonePlayer Construct failed with %d",res);
461                                         return;
462                                 }
463                                 res = __pWaitingTonePlayer->OpenFile(waitingTonePath,false);
464                                 if(res != E_SUCCESS)
465                                 {
466                                         AppLogDebug("__pWaitingTonePlayer OpenFile failed with %d",res);
467                                         return;
468                                 }
469                                 res = __pWaitingTonePlayer->SetLooping(true);
470                                 if(res != E_SUCCESS)
471                                 {
472                                         AppLogDebug("__pWaitingTonePlayer SetLooping failed with %d",res);
473                                         return;
474                                 }
475                                 res = __pWaitingTonePlayer->SetVolume(80);
476                                 if(res != E_SUCCESS)
477                                 {
478                                         AppLogDebug("__pWaitingTonePlayer SetVolume failed with %d",res);
479                                         return;
480                                 }
481                         }
482                 }
483
484                 if( __pWaitingTonePlayer != null )
485                 {
486                         SetSoundMode(SOUND_MODE_MEDIA);
487                         res = __pWaitingTonePlayer->Play();
488                         if(res != E_SUCCESS)
489                         {
490                                 AppLogDebug("__pWaitingTonePlayer Play failed with %d",res);
491                                 return;
492                         }
493                 }
494
495         }
496 }
497
498 void
499 SoundManager::StartAlert(String& contactRingTone)
500 {
501         result res = E_FAILURE;
502         AppLogDebug("Enter");
503         if(__pSoundCallSession != null)
504         {
505                 SetWaitTone();
506                 return;
507         }
508         else
509         {
510                 StartSession();
511         }
512         if(__pVibrator == null)
513         {
514                 __pVibrator = new (std::nothrow) Vibrator();
515                 res = __pVibrator->Construct();
516                 if(res != E_SUCCESS)
517                 {
518                         AppLogDebug("__pVibrator Construct failed with %d",res);
519                         return;
520                 }
521         }
522         if(__pPlayer == null)
523         {
524                 __pPlayer =  new (std::nothrow) Player();
525                 res = __pPlayer->Construct(*this);
526                 if(res != E_SUCCESS)
527                 {
528                         AppLogDebug("__pPlayer Construct failed with %d",res);
529                         return;
530                 }
531         }
532         int retVal = -1;
533         vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &retVal);
534         __isSoundStatusOn = retVal;
535         vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &retVal);
536         __isVibrateStatusOn = retVal;
537         //todo: do only if call alert mode is set
538         if (__isSoundStatusOn == true)
539         {
540                 String ringTonePath;
541                 //Check if contact has any custom ringtone, else play defautl ringtone.
542                 if(contactRingTone.IsEmpty() == true)
543                 {
544                         char* pRingTonePtr = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR);
545                         AppLog("Ringtone Path : %s",pRingTonePtr);
546                         ringTonePath.Append(pRingTonePtr);
547                 }
548                 else
549                 {
550                         AppLog("Ringtone Path : %ls",contactRingTone.GetPointer());
551                         ringTonePath.Append(contactRingTone);
552                 }
553
554                 //Check if file is present
555                 File file;
556                 res = file.Construct(ringTonePath, "r");
557                 if(res != E_SUCCESS )
558                 {
559                         ringTonePath.Clear();
560                         ringTonePath.Append(RINGTONE_PATH);
561                 }
562
563                 res = __pPlayer->OpenFile(ringTonePath,false);
564                 if(res != E_SUCCESS)
565                 {
566                         AppLogDebug("__pPlayer Construct failed with %d",res);
567                         return;
568                 }
569                 res = __pPlayer->SetLooping(true);
570                 if(res != E_SUCCESS)
571                 {
572                         AppLogDebug("__pPlayer Construct failed with %d",res);
573                         return;
574                 }
575                 res = __pPlayer->SetVolume(80);
576                 if(res != E_SUCCESS)
577                 {
578                         AppLogDebug("__pPlayer Construct failed with %d",res);
579                         return;
580                 }
581                 res = SetSoundMode(SOUND_MODE_RINGTONE);
582                 res = __pPlayer->Play();
583                 if(res != E_SUCCESS)
584                 {
585                         AppLogDebug("__pPlayer Construct failed with %d",res);
586                         return;
587                 }
588         }
589         if (__isVibrateStatusOn == true)
590         {
591                 //todo: Get the level from settings
592                 res = __pVibrator->Start(3000,1000,1000);
593         }
594         AppLogDebug("Exit");
595         return;
596 }
597
598 void
599 SoundManager::StopAlert(void)
600 {
601         result res = E_FAILURE;
602         AppLogDebug("Enter");
603         if (__isSoundStatusOn == true)
604         {
605                 //todo: stop player
606                 AppLogDebug("Stopping ring tone");
607                 if(__pPlayer->GetState() == PLAYER_STATE_PLAYING)
608                 {
609                         res = __pPlayer->Stop();
610                 }
611                 if(res != E_SUCCESS)
612                 {
613                         AppLogDebug("__pPlayer Stop failed with %d",res);
614                         return;
615                 }
616                 res = __pPlayer->Close();
617                 if(res != E_SUCCESS)
618                 {
619                         AppLogDebug("__pPlayer Close failed with %d",res);
620                         return;
621                 }
622                 SetSoundMode(SOUND_MODE_VOICE);
623                 __isSoundStatusOn = false;
624                 StopSession();
625         }
626         if (__isVibrateStatusOn == true)
627         {
628                 __pVibrator->Stop();
629                 __isVibrateStatusOn = false;
630         }
631         if(__pWaitingTonePlayer != null)
632         {
633                 if(__pWaitingTonePlayer->GetState() == PLAYER_STATE_PLAYING)
634                 {
635                         __pWaitingTonePlayer->Stop();
636                 }
637         }
638
639         AppLogDebug("Exit");
640 }
641
642 void
643 SoundManager::OnPlayerOpened(result r)
644 {
645 }
646
647 void
648 SoundManager::OnPlayerEndOfClip(void)
649 {
650 }
651
652 void
653 SoundManager::OnPlayerSeekCompleted(result r)
654 {
655 }
656
657 void
658 SoundManager::OnPlayerBuffering(int percent)
659 {
660 }
661
662 void
663 SoundManager::OnPlayerErrorOccurred(Tizen::Media::PlayerErrorReason r)
664 {
665 }
666
667 void
668 SoundManager::OnPlayerInterrupted(void)
669 {
670 }
671
672 void
673 SoundManager::OnPlayerReleased(void)
674 {
675 }
676
677 bool
678 SoundManager::IsEarJackConnected(void)
679 {
680         int earJackStatus = -1;
681         if (vconf_get_int(VCONFKEY_SYSMAN_EARJACK, &earJackStatus) == 0)
682         {
683                 if (earJackStatus == VCONFKEY_SYSMAN_EARJACK_REMOVED)
684                 {
685                         return false;
686                 }
687                 else
688                 {
689                         return true;
690                 }
691         }
692         else
693         {
694                 return false;
695         }
696 }