CAF state bug fix
[platform/framework/native/media.git] / src / FMedia_AudioEqualizerImpl.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
18 #include <unique_ptr.h>
19 #include <FBaseSysLog.h>
20 #include "FMedia_PlayerEvent.h"
21 #include "FMedia_PlayerVideoEvent.h"
22 #include "FMedia_PlayerEventTypes.h"
23 #include "FMedia_PlayerVideoEventTypes.h"
24 #include "FMedia_PlayerImpl.h"
25 #include "FMedia_AudioEqualizerImpl.h"
26
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29
30
31 namespace Tizen { namespace Media
32 {
33
34 _AudioEqualizerImpl::_AudioEqualizerImpl(void):__pPlayerImpl(null)
35 {
36
37 }
38
39 _AudioEqualizerImpl::~_AudioEqualizerImpl(void)
40 {
41
42 }
43
44 result
45 _AudioEqualizerImpl::Construct(Player& player)
46 {
47         result r = E_SUCCESS;
48         __pPlayerImpl = _PlayerImpl::GetInstance(&player);
49         SysAssertf(__pPlayerImpl != null, "Not yet Player::Construct() called! Player::Construct() should be called before use");
50         SysAssertf(__pPlayerImpl->__hPlayer !=  null, "Not yet Player handle assigned.");
51         __hPlayer = __pPlayerImpl->__hPlayer;
52         SysTryCatch(NID_MEDIA, GetBandCount() != 0, r = E_UNSUPPORTED_OPERATION, E_UNSUPPORTED_OPERATION,
53                                                                                 "[E_UNSUPPORTED_OPERATION] Failed to construct _AudioEqualizerImpl");
54         return r;
55
56 CATCH:
57         __hPlayer = null;
58         return r;
59 }
60
61 int
62 _AudioEqualizerImpl::GetBandCount(void) const
63 {
64         SysAssertf(__hPlayer !=  null, "Not yet constructed! Construct() should be called before use");
65         int ret = PLAYER_ERROR_NONE;
66         result r = E_SUCCESS;
67         int count = 0;
68
69         ret = player_audio_effect_get_equalizer_bands_count(__hPlayer, &count);
70         r = MapExceptionToResult(ret);
71         SysTryLog(NID_MEDIA, r == E_SUCCESS, "[%s] Failed to perform GetBandCount operation.", GetErrorMessage(r));
72
73         return count;
74 }
75
76 result
77 _AudioEqualizerImpl::GetBandLevelRange(int index, int& minValue, int& maxValue) const
78 {
79         SysAssertf(__hPlayer !=  null, "Not yet constructed! Construct() should be called before use");
80         int ret = PLAYER_ERROR_NONE;
81         result r = E_SUCCESS;
82
83         int count = 0;
84         count = GetBandCount();
85         SysTryReturn(NID_MEDIA, index >= 0 && index < count , E_INVALID_ARG, E_INVALID_ARG,
86                                 "[E_INVALID_ARG] invalid argument is used. index = %d", index);
87         ret = player_audio_effect_get_equalizer_level_range(__hPlayer, &minValue, &maxValue);
88         r = MapExceptionToResult(ret);
89         return r;
90 }
91
92 result
93 _AudioEqualizerImpl::SetBandLevel(int index, int level)
94 {
95         SysAssertf(__hPlayer !=  null, "Not yet constructed! Construct() should be called before use");
96         int ret = PLAYER_ERROR_NONE;
97         result r = E_SUCCESS;
98         int minValue = 0;
99         int maxValue = 0;
100
101         int count = 0;
102         count = GetBandCount();
103         SysTryReturn(NID_MEDIA, index >= 0 && index < count , E_INVALID_ARG, E_INVALID_ARG,
104                                 "[E_INVALID_ARG] invalid argument is used. index = %d", index);
105         r = GetBandLevelRange(index, minValue, maxValue);
106         
107         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "%s error has been occurred in GetBandLevelRange", GetErrorMessage(r));
108         SysTryReturn(NID_MEDIA, (level <= maxValue) && (level >= minValue), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
109                                 "[E_OUT_OF_RANGE] Equalizer level %d is out of range.", level   );
110         ret = player_audio_effect_set_equalizer_band_level(__hPlayer, index, level);
111         r = MapExceptionToResult(ret);
112         return r;
113 }
114
115 result
116 _AudioEqualizerImpl::SetAllBandsLevel(const Tizen::Base::Collection::IListT<int>* pLevels)
117 {
118         SysAssertf(__hPlayer !=  null, "Not yet constructed! Construct() should be called before use");
119         int ret = PLAYER_ERROR_NONE;
120         result r = E_SUCCESS;
121         std::unique_ptr<int[]> pArray;
122         int level = 0;
123
124         int totalBandCount = 0;
125         totalBandCount = GetBandCount();
126         
127         int listCount = 0;
128         listCount = pLevels->GetCount();
129         
130         SysTryReturn(NID_MEDIA, listCount == totalBandCount, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. pLevels");
131         
132         pArray.reset(new (std::nothrow) int[listCount]);
133         SysTryReturn(NID_MEDIA, pArray.get() != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory Allocation Failed");
134                 
135         for(int i=0; i<listCount; i++)
136         {
137                 pLevels->GetAt(i,level);
138                 (pArray.get())[i] = level;
139         }
140
141         ret = player_audio_effect_set_equalizer_all_bands(__hPlayer, pArray.get(), listCount);
142         r = MapExceptionToResult(ret);
143         return r;
144 }
145
146 result
147 _AudioEqualizerImpl::GetBandLevel(int index, int& level) const
148 {
149         SysAssertf(__hPlayer !=  null, "Not yet constructed! Construct() should be called before use");
150         int ret = PLAYER_ERROR_NONE;
151         result r = E_SUCCESS;
152
153         int count = 0;
154         count = GetBandCount();
155         SysTryReturn(NID_MEDIA, index >= 0 && index < count , E_INVALID_ARG, E_INVALID_ARG,
156                                 "[E_INVALID_ARG] Invalid argument is used. Index = %d", index);
157         ret = player_audio_effect_get_equalizer_band_level(__hPlayer, index, &level);
158         r = MapExceptionToResult(ret);
159         return r;
160
161 }
162
163 result
164 _AudioEqualizerImpl::GetBandCenterFrequency(int index, int& frequency) const
165 {
166         //Not supported by SLP presently
167         result r = E_SUCCESS;
168         SysAssertf(__hPlayer !=  null, "Not yet constructed! Construct() should be called before use");
169         int ret = PLAYER_ERROR_NONE;
170         ret = player_audio_effect_get_equalizer_band_frequency(__hPlayer, index, &frequency);
171         r = MapExceptionToResult(ret);
172         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] player_audio_effect_get_equlizer_band_frequency", GetErrorMessage(r));
173         return r;
174 }
175
176 result
177 _AudioEqualizerImpl::ResetAllToDefault(void)
178 {
179         SysAssertf(__hPlayer !=  null, "Not yet constructed! Construct() should be called before use");
180         int ret = PLAYER_ERROR_NONE;
181         result r = E_SUCCESS;
182         ret = player_audio_effect_equalizer_clear(__hPlayer);
183         r = MapExceptionToResult(ret);
184         return r;
185 }
186
187 result
188 _AudioEqualizerImpl::MapExceptionToResult(int reason)
189 {
190         switch (reason)
191         {
192                 case PLAYER_ERROR_NONE:
193                         return E_SUCCESS;
194                         break;
195                         
196                 case PLAYER_ERROR_OUT_OF_MEMORY:
197                         return E_OUT_OF_MEMORY;
198                         break;
199
200                 case PLAYER_ERROR_INVALID_PARAMETER:
201                         return E_INVALID_ARG;
202                         break;
203
204                 case PLAYER_ERROR_INVALID_STATE:
205                 case PLAYER_ERROR_INVALID_OPERATION:            
206                         return E_INVALID_OPERATION;
207                         break;
208                         
209                 default:
210                         return E_SYSTEM;
211                         break;
212         }
213 }
214
215 };
216 };
217