Tizen 2.0 Release
[framework/osp/media.git] / src / FMedia_VoipAudioSessionManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License)
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 #include <iostream>
18 #include <pthread.h>
19 #include <sound_manager.h>
20 #include <FBaseRt.h>
21 #include <FBaseSysLog.h>
22 #include <FMediaAudioManagerTypes.h>
23 #include "FMedia_AudioManagerConvert.h"
24 #include "FMedia_VoipAudioSessionManagerImpl.h"
25
26 using namespace Tizen::Base;
27 using namespace Tizen::Base::Runtime;
28
29 namespace Tizen { namespace Media {
30
31 _VoipAudioSessionManagerImpl* _VoipAudioSessionManagerImpl::__pVoipAudioSessionManagerImpl = null;
32
33 _VoipAudioSessionManagerImpl::_VoipAudioSessionManagerImpl()
34         :__soundCallSessionHandle(null)
35 {
36
37 }
38
39 _VoipAudioSessionManagerImpl::~_VoipAudioSessionManagerImpl(void)
40 {
41         int ret = SOUND_MANAGER_ERROR_NONE;
42         result r = E_SUCCESS;
43         if (__soundCallSessionHandle != null)
44         {
45                 ret = sound_manager_call_session_destroy(__soundCallSessionHandle);
46                 r = _AudioManagerConvert::CovertSoundManagerError2Result(ret);
47                 SysTryLog(NID_MEDIA, r == E_SUCCESS,
48                         "[%s] Failed to perform sound_manager_call_session_destroy operation.", GetErrorMessage(r));
49                 __soundCallSessionHandle = null;
50         }
51 }
52
53 void
54 _VoipAudioSessionManagerImpl::InitVoipAudioSessionManagerImpl(void)
55 {
56         static _VoipAudioSessionManagerImpl instance;
57         __pVoipAudioSessionManagerImpl = &instance;
58 }
59
60 _VoipAudioSessionManagerImpl*
61 _VoipAudioSessionManagerImpl::GetInstance(void)
62 {
63         ClearLastResult();
64         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
65         if (__pVoipAudioSessionManagerImpl == null)
66         {
67                 pthread_once(&onceBlock, InitVoipAudioSessionManagerImpl);
68         }
69         return __pVoipAudioSessionManagerImpl;
70 }
71
72 result
73 _VoipAudioSessionManagerImpl::EnterCallSession(void)
74 {
75         int ret = SOUND_MANAGER_ERROR_NONE;
76         result r = E_SUCCESS;
77         SysTryReturn(NID_MEDIA, __soundCallSessionHandle == null , E_INVALID_OPERATION, E_INVALID_OPERATION,
78                 "[E_INVALID_OPERATION] A error has been occurred. The value of __soundCallSessionHandle is not null.");
79
80         ret = sound_manager_call_session_create(SOUND_SESSION_TYPE_VOIP, &__soundCallSessionHandle);
81         r = _AudioManagerConvert::CovertSoundManagerError2Result(ret);
82         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
83                 "[%s] Failed to perform sound_manager_get_a2dp_status operation with error code : 0x%x", GetErrorMessage(r), ret);
84         SysTryCatch(NID_MEDIA, __soundCallSessionHandle != null, r = E_SYSTEM, E_SYSTEM,
85                 "[E_SYSTEM] A system error has been occurred. The value of __soundCallSessionHandle is null.");
86
87         return E_SUCCESS;
88 CATCH:
89         if (__soundCallSessionHandle)
90         {
91                 sound_manager_call_session_destroy(__soundCallSessionHandle);
92                 __soundCallSessionHandle = null;
93         }
94         return r;
95 }
96
97 result
98 _VoipAudioSessionManagerImpl::ExitCallSession(void)
99 {
100         SysTryReturn(NID_MEDIA, __soundCallSessionHandle != null , E_INVALID_OPERATION, E_INVALID_OPERATION,
101                 "[E_INVALID_OPERATION] A error has been occurred. The value of __soundCallSessionHandle is null.");
102         int ret = SOUND_MANAGER_ERROR_NONE;
103         result r = E_SUCCESS;
104         ret = sound_manager_call_session_destroy(__soundCallSessionHandle);
105         r = _AudioManagerConvert::CovertSoundManagerError2Result(ret);
106         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r,"[%s] Failed to perform sound_manager_call_session_destroy operation.", GetErrorMessage(r));
107         __soundCallSessionHandle = null;
108         return E_SUCCESS;
109 }
110
111 result
112 _VoipAudioSessionManagerImpl::SetCallSessionMode(VoipAudioSessionMode mode)
113 {
114         result r = E_SUCCESS;
115         int ret = SOUND_MANAGER_ERROR_NONE;
116         SysAssertf(__soundCallSessionHandle != null, "Not yet constructed! Construct() should be called before use");
117         SysTryReturn(NID_MEDIA, mode != VOIP_AUDIO_SESSION_MODE_NONE , E_INVALID_ARG, E_INVALID_ARG,
118                 " [E_INVALID_ARG] Invalid argument is used. The mode is not correct : %d", mode);
119         ret = sound_manager_call_session_set_mode(__soundCallSessionHandle, _AudioManagerConvert::ConvertVoipMode2CallMode(mode));
120         r = _AudioManagerConvert::CovertSoundManagerError2Result(ret);
121         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r,
122                 "[%s] Failed to perform sound_manager_call_session_set_mode operation.", GetErrorMessage(r));
123         return r;
124 }
125
126 VoipAudioSessionMode
127 _VoipAudioSessionManagerImpl::GetCallSessionMode(void)
128 {
129         int ret = SOUND_MANAGER_ERROR_NONE;
130         result r = E_SUCCESS;
131         SysAssertf(__soundCallSessionHandle != null, "Not yet constructed! Construct() should be called before use");
132         sound_call_session_mode_e soundCallSessionMode = SOUND_CALL_SESSION_MODE_VOICE;
133         ret = sound_manager_call_session_get_mode(__soundCallSessionHandle, &soundCallSessionMode);
134         r = _AudioManagerConvert::CovertSoundManagerError2Result(ret);
135         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
136                 "[%s] Failed to perform sound_manager_call_session_get_mode operation with error code : 0x%x", GetErrorMessage(r), ret);
137         return _AudioManagerConvert::ConvertCallSessionMode2VoipMode(soundCallSessionMode);
138 CATCH:
139         return VOIP_AUDIO_SESSION_MODE_NONE;
140 }
141
142 };
143 };              //Tizen::Media