Tizen 2.0 Release
[framework/osp/media.git] / src / FMedia_AudioEncoderImpl.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
19 #include <FBaseColArrayListT.h>
20 #include <FBaseColHashMap.h>
21 #include <FBaseColHashMapT.h>
22 #include <FMediaTypes.h>
23 #include <FMediaAudioTypes.h>
24 #include <FBaseSysLog.h>
25 #include "FMedia_IAudioEncoder.h"
26 #include "FMedia_CodecFactory.h"
27 #include "FMedia_AudioEncoderImpl.h"
28
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31
32
33 namespace Tizen { namespace Media
34 {
35
36 _AudioEncoderImpl::_AudioEncoderImpl(void)
37 {
38         __pEnc = null;
39 }
40
41 _AudioEncoderImpl::~_AudioEncoderImpl(void)
42 {
43         if (__pEnc != null)
44         {
45                 delete __pEnc;
46         }
47 }
48
49 result
50 _AudioEncoderImpl::Construct(CodecType type, const Tizen::Base::Collection::HashMap* pOption)
51 {
52         _CodecFactory* pFactory = null;
53         result r = E_SUCCESS;
54
55         SysTryReturnResult(NID_MEDIA, __pEnc == null, E_INVALID_STATE, "already constructed");
56         pFactory = _CodecFactory::GetInstance();
57         SysTryCatch(NID_MEDIA, pFactory != null, r = E_SYSTEM, E_SYSTEM,
58                            "[%s] Failed to get Codec Factory Instance",GetErrorMessage(E_SYSTEM));\r
59         r = pFactory->CreateCodecInstance(type, __pEnc);
60         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] CodecFactory failed to create Codec Instance", GetErrorMessage(r));\r
61         r = __pEnc->Construct(pOption);
62         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to construct __pEnc", GetErrorMessage(r));\r
63         return r;
64
65 CATCH:
66         if (__pEnc != null)
67         {
68                 delete __pEnc;
69                 __pEnc = null;
70         }
71         return r;
72 }
73
74 result
75 _AudioEncoderImpl::Encode(Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& dstBuf)
76 {
77         result r = E_INVALID_STATE;
78         const byte* pSrcByte = null;
79         byte* pDstByte = null;
80         int srcByteSize = 0;
81         int dstByteSize = 0;
82         int offset = 0;
83
84         SysTryReturnResult(NID_MEDIA, __pEnc, E_INVALID_STATE, "not constructed");
85         pSrcByte = srcBuf.GetPointer() + srcBuf.GetPosition();
86         pDstByte = (byte*) dstBuf.GetPointer() + dstBuf.GetPosition();
87         SysTryCatch(NID_MEDIA, pDstByte != null && pSrcByte != null, r = E_INVALID_ARG, E_INVALID_ARG,
88                            "[%s] Invalid argument is used. srcBuf:%x dstBuf:%d", GetErrorMessage(E_INVALID_ARG), pSrcByte, pDstByte);\r
89         srcByteSize = srcBuf.GetRemaining();
90         dstByteSize = dstBuf.GetRemaining();
91         SysTryCatch(NID_MEDIA, srcByteSize > 0 && dstByteSize > 0, r = E_INVALID_ARG, E_INVALID_ARG,
92                            "[%s] Invalid argument is used. srcSize:%d dstSize:%d", GetErrorMessage(E_INVALID_ARG), srcByteSize, dstByteSize);\r
93
94         r = __pEnc->Encode(pSrcByte, srcByteSize, pDstByte, dstByteSize);
95         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Audio Encoder encode failed", GetErrorMessage(r));\r
96
97         offset = srcBuf.GetPosition();
98         if (offset + srcByteSize > srcBuf.GetLimit())
99         {
100                 srcBuf.SetPosition(srcBuf.GetLimit());
101         }
102         else
103         {
104                 srcBuf.SetPosition(offset + srcByteSize);
105         }
106
107         offset = dstBuf.GetPosition();
108         if (offset + dstByteSize > dstBuf.GetLimit())
109         {
110                 dstBuf.SetPosition(dstBuf.GetLimit());
111         }
112         else
113         {
114                 dstBuf.SetPosition(offset + dstByteSize);
115         }
116
117         return r;
118
119 CATCH:
120         return r;
121 }
122
123 result
124 _AudioEncoderImpl::Encode(const byte* srcBuf, int& srcBufLength, byte*& dstBuf, int& dstBufLength)
125 {
126         result r = E_SUCCESS;
127
128         SysTryReturnResult(NID_MEDIA, __pEnc, E_INVALID_STATE, "not constructed");
129         r = __pEnc->Encode(srcBuf, srcBufLength, dstBuf, dstBufLength);
130         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Audio Encoder encode failed", GetErrorMessage(r));\r
131         return r;
132
133 CATCH:
134         return r;
135 }
136
137 result
138 _AudioEncoderImpl::Reset(void)
139 {
140         result r = E_SUCCESS;
141
142         SysTryReturnResult(NID_MEDIA, __pEnc, E_INVALID_STATE, "not constructed");
143         r = __pEnc->Reset();
144         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Audio Encoder reset failed", GetErrorMessage(r));\r
145         return r;
146
147 CATCH:
148         return r;
149 }
150
151 Tizen::Base::Collection::IListT<CodecType>*
152 _AudioEncoderImpl::GetSupportedCodecListN(void)
153 {
154         _CodecFactory* pFactory = null;
155         Tizen::Base::Collection::IListT<CodecType>* pList = null;
156
157         pFactory = _CodecFactory::GetInstance();
158         SysTryReturn(NID_MEDIA, pFactory != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.",\r
159                                 GetErrorMessage(GetLastResult()));
160         pList = pFactory->GetSupportedAudioEncoderListN();
161         SysTryReturn(NID_MEDIA, pList != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.",\r
162                                 GetErrorMessage(GetLastResult()));
163         SetLastResult(E_SUCCESS);
164         return pList;
165 }
166
167 }} // Tizen::Media