Tizen 2.0 Release
[framework/osp/media.git] / src / FMediaAudioOut.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 <FMediaAudioOut.h>
19 #include <FMediaAudioTypes.h>
20 #include <FBaseSysLog.h>
21 #include "FMedia_AudioOutImpl.h"
22
23
24 namespace Tizen { namespace Media
25 {
26
27 AudioOut::AudioOut(void)
28         : __pAudioOutImpl(null)
29 {
30 }
31
32 AudioOut::~AudioOut(void)
33 {
34         if (__pAudioOutImpl)
35         {
36                 delete __pAudioOutImpl;
37         }
38
39 }
40
41 result
42 AudioOut::Construct(IAudioOutEventListener& listener)
43 {
44         ClearLastResult();
45         result r = E_SUCCESS;
46
47         SysAssertf(__pAudioOutImpl == null, "This instance is already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
48
49         _AudioOutImpl* pAudioOutImpl = new (std::nothrow) _AudioOutImpl;
50         SysTryCatch(NID_MEDIA, pAudioOutImpl != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
51
52         r = pAudioOutImpl->Construct(this, listener);
53         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
54
55         __pAudioOutImpl = pAudioOutImpl;
56
57         return r;
58
59 CATCH:
60         if (pAudioOutImpl)
61         {
62                 delete pAudioOutImpl;
63                 pAudioOutImpl = null;
64         }
65         return r;
66 }
67
68 result
69 AudioOut::Prepare(AudioSampleType audioSampleType, AudioChannelType audioChannelType, int audioSampleRate)
70 {
71         ClearLastResult();
72         result r = E_SUCCESS;
73
74         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
75
76         r = __pAudioOutImpl->Prepare(audioSampleType, audioChannelType, audioSampleRate);
77         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
78
79         return r;
80 }
81
82 result
83 AudioOut::Prepare(AudioStreamType audioStreamType, AudioSampleType audioSampleType, AudioChannelType audioChannelType, int audioSampleRate)
84 {
85         ClearLastResult();
86         result r = E_SUCCESS;
87
88         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
89
90         r = __pAudioOutImpl->Prepare(audioStreamType, audioSampleType, audioChannelType, audioSampleRate);
91         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
92
93         return r;
94 }
95
96 result
97 AudioOut::Unprepare(void)
98 {
99         ClearLastResult();
100         result r = E_SUCCESS;
101
102         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
103
104         r = __pAudioOutImpl->UnPrepare();
105         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
106
107         return r;
108 }
109
110 result
111 AudioOut::WriteBuffer(const Tizen::Base::ByteBuffer& userData)
112 {
113         ClearLastResult();
114         result r = E_SUCCESS;
115
116         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
117
118         r = __pAudioOutImpl->WriteBuffer(userData);
119         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
120
121         return r;
122 }
123
124 result
125 AudioOut::Start(void)
126 {
127         ClearLastResult();
128
129         result r = E_SUCCESS;
130
131         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
132
133         r = __pAudioOutImpl->Start();
134         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
135
136         return r;
137 }
138
139 result
140 AudioOut::Stop(void)
141 {
142         ClearLastResult();
143         result r = E_SUCCESS;
144
145         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
146
147         r = __pAudioOutImpl->Stop();
148         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
149
150         return r;
151 }
152
153 result
154 AudioOut::Reset(void)
155 {
156         ClearLastResult();
157         result r = E_SUCCESS;
158
159         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
160
161         r = __pAudioOutImpl->Reset();
162         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
163
164         return r;
165 }
166
167 AudioOutState
168 AudioOut::GetState(void) const
169 {
170         ClearLastResult();
171         AudioOutState audioState = AUDIOOUT_STATE_ERROR;
172
173         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
174
175         audioState = __pAudioOutImpl->GetState();
176         SysTryReturn(NID_MEDIA, audioState != AUDIOOUT_STATE_ERROR, AUDIOOUT_STATE_ERROR, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
177
178         return audioState;
179 }
180
181 int
182 AudioOut::GetMaxBufferSize(void) const
183 {
184         ClearLastResult();
185         int bufferSize = -1;
186
187         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
188
189         bufferSize = __pAudioOutImpl->GetMaxBufferSize();
190         SysTryReturn(NID_MEDIA, bufferSize != -1, bufferSize, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
191
192         return bufferSize;
193 }
194
195 int
196 AudioOut::GetMinBufferSize(void) const
197 {
198         ClearLastResult();
199         int bufferSize = -1;
200
201         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
202
203         bufferSize = __pAudioOutImpl->GetMinBufferSize();
204         SysTryReturn(NID_MEDIA, bufferSize != -1, bufferSize, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
205         return bufferSize;
206 }
207
208 AudioSampleType
209 AudioOut::GetOptimizedSampleType(void) const
210 {
211         ClearLastResult();
212         AudioSampleType audioSampleType = AUDIO_TYPE_NONE;
213
214         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
215
216         audioSampleType = __pAudioOutImpl->GetOptimizedSampleType();
217         SysTryReturn(NID_MEDIA, audioSampleType != AUDIO_TYPE_NONE, AUDIO_TYPE_NONE, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
218         return audioSampleType;
219 }
220
221 AudioChannelType
222 AudioOut::GetOptimizedChannelType(void) const
223 {
224         ClearLastResult();
225         AudioChannelType audioChannelType = AUDIO_CHANNEL_TYPE_NONE;
226
227         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
228
229         audioChannelType = __pAudioOutImpl->GetOptimizedChannelType();
230         SysTryReturn(NID_MEDIA, audioChannelType != AUDIO_CHANNEL_TYPE_NONE, AUDIO_CHANNEL_TYPE_NONE, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
231         return audioChannelType;
232 }
233
234 int
235 AudioOut::GetOptimizedSampleRate(void) const
236 {
237         ClearLastResult();
238         int sampleRate = 0;
239
240         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
241
242         sampleRate = __pAudioOutImpl->GetOptimizedSampleRate();
243         SysTryReturn(NID_MEDIA, sampleRate != 0, 0, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
244
245         return sampleRate;
246 }
247
248 result
249 AudioOut::SetVolume(int volume)
250 {
251         ClearLastResult();
252         result r = E_SUCCESS;
253
254         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
255         SysTryReturn(NID_MEDIA, (volume <= 100) && (volume >= 0), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
256                         "[E_OUT_OF_RANGE] volume is out of range. The value of volume should be between 0 and 100");
257
258         r = __pAudioOutImpl->SetVolume(volume);
259         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
260
261         return r;
262 }
263
264
265 int
266 AudioOut::GetVolume(void) const
267 {
268         ClearLastResult();
269         int volume = -1;
270
271         SysAssertf(__pAudioOutImpl !=  null, "Not yet constructed! Construct() should be called before use");
272
273         volume = __pAudioOutImpl->GetVolume();
274         SysTryReturn(NID_MEDIA, volume != -1, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
275
276         return volume;
277 }
278
279 }} // Tizen::Media