Tizen 2.0 Release
[framework/osp/media.git] / src / FMedia_AudioDecoderImpl.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 andW
15 // limitations under the License.
16 //
17
18 #include <FBaseColArrayListT.h>
19 #include <FMediaTypes.h>
20 #include <FMediaAudioTypes.h>
21 #include <FBaseColHashMap.h>
22 #include <FBaseColHashMapT.h>
23
24 #include <FBaseSysLog.h>
25
26 #include "FMedia_IAudioDecoder.h"
27 #include "FMedia_CodecFactory.h"
28 #include "FMedia_AudioDecoderImpl.h"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32
33
34 namespace Tizen { namespace Media
35 {
36
37 _AudioDecoderImpl::_AudioDecoderImpl(void)
38 {
39         __pDec = null;
40 }
41
42 _AudioDecoderImpl::~_AudioDecoderImpl(void)
43 {
44         if (__pDec != null)
45         {
46                 delete __pDec;
47         }
48 }
49
50 result
51 _AudioDecoderImpl::Construct(CodecType type, const Tizen::Base::Collection::HashMap* pOption)
52 {
53         _CodecFactory* pFactory = null;
54         result r = E_SUCCESS;
55
56         SysTryReturn(NID_MEDIA, __pDec == null, E_INVALID_STATE, E_INVALID_STATE,
57                                 "[E_INVALID_STATE] Already Constructed");
58         SysAssertf(__pDec == null, " Already Constructed .");\r
59         \r
60
61         pFactory = _CodecFactory::GetInstance();
62         SysTryCatch(NID_MEDIA, pFactory != null, r = E_SYSTEM, E_SYSTEM,
63                            "[%s] GetFactoryInstance Failed",GetErrorMessage(E_SYSTEM));\r
64         r = pFactory->CreateCodecInstance(type, __pDec);
65         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
66                            "[%s] CreateCodecInstance failed:%d", GetErrorMessage(r), type);
67         r = __pDec->Construct(pOption);
68         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] dec.Construct Failed", GetErrorMessage(r));
69         return r;
70
71 CATCH:
72         if (__pDec != null)
73         {
74                 delete __pDec;
75                 __pDec = null;
76         }
77         return r;
78 }
79
80 result
81 _AudioDecoderImpl::Probe(const Tizen::Base::ByteBuffer& srcBuf, AudioSampleType& sampleType,
82                                                  AudioChannelType& channelType, int& sampleRate)
83 {
84         result r = E_SUCCESS;
85         const byte* pSrcBuf = null;
86         int srcBufSize = 0;
87
88         SysTryReturn(NID_MEDIA, __pDec != null, E_INVALID_STATE, E_INVALID_STATE,
89                                 "[%s] The instance is in invalid state",GetErrorMessage(E_INVALID_STATE));\r
90
91         SysTryCatch(NID_MEDIA, srcBuf.GetPointer() != null, r = E_INVALID_ARG, E_INVALID_ARG,
92                            "[%s] The input argument is invalid",GetErrorMessage(E_INVALID_ARG));\r
93
94         pSrcBuf = srcBuf.GetPointer() + srcBuf.GetPosition();
95         srcBufSize = srcBuf.GetRemaining();
96         SysTryCatch(NID_MEDIA, srcBufSize > 0, r = E_INVALID_ARG, E_INVALID_ARG,
97                            "[%s] The input argument is invalid",GetErrorMessage(E_INVALID_ARG));\r
98         r = __pDec->Probe(pSrcBuf, srcBufSize, sampleType, channelType, sampleRate);
99         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Probe Failed", GetErrorMessage(r));
100
101         SetLastResult(r);
102         return r;
103
104 CATCH:
105         return r;
106 }
107
108 result
109 _AudioDecoderImpl::Decode(Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& dstBuf)
110 {
111         result r = E_SUCCESS;
112         const byte* pSrcBuf = null;
113         byte* pDstBuf = null;
114         int srcBufSize = 0;
115         int dstBufSize = 0;
116         int srcBufUsed = 0;
117         int dstBufUsed = 0;
118
119         SysTryReturn(NID_MEDIA, __pDec != null, E_INVALID_STATE, E_INVALID_STATE,
120                                 "[%s] The instance is in invalid state",GetErrorMessage(E_INVALID_STATE));\r
121
122         SysTryCatch(NID_MEDIA, srcBuf.GetPointer() != null && dstBuf.GetPointer() != null,
123                            r = E_INVALID_ARG, E_INVALID_ARG,
124                            "[%s] srcBuf:0x%x dstBuf:0x%x", GetErrorMessage(E_INVALID_ARG), srcBuf.GetPointer(), dstBuf.GetPointer());\r
125         pSrcBuf = srcBuf.GetPointer() + srcBuf.GetPosition();
126         pDstBuf = (byte*)dstBuf.GetPointer() + dstBuf.GetPosition();
127         srcBufSize = srcBuf.GetRemaining();
128         dstBufSize = dstBuf.GetRemaining();
129         SysTryCatch(NID_MEDIA, srcBufSize > 0 && dstBufSize > 0, r = E_INVALID_ARG, E_INVALID_ARG,
130                            "[%s] srcSize:%d dstSize:%d", GetErrorMessage(E_INVALID_ARG), srcBufSize, dstBufSize);\r
131
132         r = __pDec->Decode(pSrcBuf, srcBufSize, srcBufUsed, pDstBuf, dstBufSize, dstBufUsed);
133
134         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] dec.Decode", GetErrorMessage(r));
135
136         r = srcBuf.SetPosition(srcBuf.GetPosition() + srcBufUsed);
137         SysTryCatch(NID_MEDIA, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM,
138                            "[%s] srcBuf overflow", GetErrorMessage(r));
139
140         r = dstBuf.SetPosition(dstBuf.GetPosition() + dstBufUsed);
141         SysTryCatch(NID_MEDIA, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM,
142                            "[%s] dstBuf overflow", GetErrorMessage(r));
143
144         SetLastResult(r);
145         return r;
146
147 CATCH:
148         return r;
149 }
150
151 result
152 _AudioDecoderImpl::Reset(void)
153 {
154
155         SysTryReturn(NID_MEDIA, __pDec != null, E_INVALID_STATE, E_INVALID_STATE,
156                                 "[E_INVALID_STATE] Not Constructed");
157         return __pDec->Reset();
158 }
159
160 result
161 _AudioDecoderImpl::GetValue(MediaPropertyType key, int& value) const
162 {
163         SysTryReturn(NID_MEDIA, __pDec != null, E_INVALID_STATE, E_INVALID_STATE,
164                                 "[E_INVALID_STATE] Not Constructed");
165         return __pDec->GetValue(key, value);
166 }
167
168 Tizen::Base::Collection::IListT<MediaPropertyType>*
169 _AudioDecoderImpl::GetSupportedPropertyListN(void) const
170 {
171
172         SysTryReturn(NID_MEDIA, __pDec != null, null, E_INVALID_STATE,
173                                 "[E_INVALID_STATE] Not Constructed");
174         return __pDec->GetSupportedPropertyListN();
175 }
176
177 bool
178 _AudioDecoderImpl::IsPropertySupported(MediaPropertyType key) const
179 {
180         SysTryReturn(NID_MEDIA, __pDec != null, false, E_INVALID_STATE,
181                                 "[E_INVALID_STATE] Not Constructed");
182         return  __pDec->IsPropertySupported(key);
183 }
184
185 Tizen::Base::Collection::IListT<CodecType>*
186 _AudioDecoderImpl::GetSupportedCodecListN(void)
187 {
188         _CodecFactory* pFactory = null;
189         Tizen::Base::Collection::IListT<CodecType>* pList = null;
190
191         pFactory = _CodecFactory::GetInstance();
192         SysTryReturn(NID_MEDIA, pFactory != null, null, E_OUT_OF_MEMORY, "[%s] Propagated.",
193                                 GetErrorMessage(GetLastResult()));
194         pList = pFactory->GetSupportedAudioDecoderListN();
195         SysTryReturn(NID_MEDIA, pList != null, null, E_OUT_OF_MEMORY, "[%s] Propagated.",
196                                 GetErrorMessage(GetLastResult()));
197         SetLastResult(E_SUCCESS);
198         return pList;
199 }
200
201 }} // Tizen::Media