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