Tizen 2.0 Release
[framework/osp/media.git] / src / FMedia_VideoEncoderImpl.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 <stdio.h>
19 #include <FBaseColArrayListT.h>
20 #include <FBaseColHashMap.h>
21 #include <FBaseColHashMapT.h>
22 #include <FMediaTypes.h>
23 #include <FBaseSysLog.h>
24 #include "FMedia_IVideoEncoder.h"
25 #include "FMedia_CodecFactory.h"
26 #include "FMedia_VideoEncoderImpl.h"
27
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30
31 namespace Tizen { namespace Media
32 {
33
34 _VideoEncoderImpl::_VideoEncoderImpl(void)
35 {
36         __pEnc = null;
37 }
38
39 _VideoEncoderImpl::~_VideoEncoderImpl(void)
40 {
41         if (__pEnc != null)
42         {
43                 delete __pEnc;
44         }
45 }
46
47 result
48 _VideoEncoderImpl::Construct(CodecType type, const Tizen::Base::Collection::HashMap* pOption)
49 {
50         _IVideoEncoder* pVideoEncoder = null;
51         _CodecFactory* pCodecFactory = null;
52         result r = E_SUCCESS;
53
54         SysTryReturnResult(NID_MEDIA, __pEnc == null, E_INVALID_STATE, "already constructed");
55
56         pCodecFactory = _CodecFactory::GetInstance();
57         SysTryCatch(NID_MEDIA, pCodecFactory != null, r = E_SYSTEM, E_SYSTEM,
58                            "Construct Failed ");
59         r = pCodecFactory->CreateCodecInstance(type, pVideoEncoder);
60         SysTryCatch(NID_MEDIA, pVideoEncoder != null, , r, "Construct Failed:%d", type);
61         __pEnc = pVideoEncoder;
62         r = __pEnc->Construct(pOption);
63         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s]Construct Failed ", GetErrorMessage(r));
64         return r;
65
66 CATCH:
67         if (__pEnc != null)
68         {
69                 delete __pEnc;
70                 __pEnc = null;
71         }
72         return r;
73 }
74
75 result
76 _VideoEncoderImpl::Encode(Tizen::Base::ByteBuffer& srcBuf, Tizen::Base::ByteBuffer& dstBuf)
77 {
78         result r = E_INVALID_STATE;
79         const byte* pSrcByte = null;
80         byte* pDstByte = null;
81         int srcByteSize = 0;
82         int dstByteSize = 0;
83         int offset = 0;
84
85         SysTryReturnResult(NID_MEDIA, __pEnc, E_INVALID_STATE, "not constructed");
86
87         pSrcByte = srcBuf.GetPointer() + srcBuf.GetPosition();
88         pDstByte = (byte*) dstBuf.GetPointer() + dstBuf.GetPosition();
89         SysTryCatch(NID_MEDIA, (null != pDstByte && null != pSrcByte), r = E_INVALID_ARG, E_INVALID_ARG,
90                            "[E_INVALID_ARG] Encode Failed ");
91         srcByteSize = srcBuf.GetRemaining();
92         dstByteSize = dstBuf.GetRemaining();
93         SysTryCatch(NID_MEDIA, (srcByteSize > 0 && dstByteSize > 0), r = E_INVALID_ARG, E_INVALID_ARG,
94                            "[E_INVALID_ARG] Encode Failed ");
95
96         r = __pEnc->Encode(pSrcByte, srcByteSize, pDstByte, dstByteSize);
97         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
98                            "[%s] Encode Failed", GetErrorMessage(r));
99
100         offset = srcBuf.GetPosition();
101         if (offset + srcByteSize > srcBuf.GetLimit())
102         {
103                 srcBuf.SetPosition(srcBuf.GetLimit());
104         }
105         else
106         {
107                 srcBuf.SetPosition(offset + srcByteSize);
108         }
109
110         offset = dstBuf.GetPosition();
111         if (offset + dstByteSize > dstBuf.GetLimit())
112         {
113                 dstBuf.SetPosition(dstBuf.GetLimit());
114         }
115         else
116         {
117                 dstBuf.SetPosition(offset + dstByteSize);
118         }
119
120         return r;
121
122 CATCH:
123         return r;
124 }
125
126 result
127 _VideoEncoderImpl::Encode(const byte* srcBuf, int& srcBufLength, byte* dstBuf, int& dstBufLength)
128 {
129         result r = E_SUCCESS;
130
131         SysTryReturnResult(NID_MEDIA, __pEnc, E_INVALID_STATE, "not constructed");
132
133         r = __pEnc->Encode(srcBuf, srcBufLength, dstBuf, dstBufLength);
134         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
135                            "[%s] Encode Failed", GetErrorMessage(r));
136
137         return r;
138
139 CATCH:
140         return r;
141 }
142
143 result
144 _VideoEncoderImpl::Reset(void)
145 {
146         result r = E_SUCCESS;
147
148         SysTryReturnResult(NID_MEDIA, __pEnc, E_INVALID_STATE, "not constructed");
149
150         r = __pEnc->Reset();
151         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Reset Failed", GetErrorMessage(r));
152
153         return r;
154
155 CATCH:
156         return r;
157 }
158
159 result
160 _VideoEncoderImpl::SetValue(MediaPropertyType key, int value)
161 {
162         result r = E_SUCCESS;
163
164         SysTryReturnResult(NID_MEDIA, __pEnc, E_INVALID_STATE, "not constructed");
165
166         r = __pEnc->SetValue(key, value);
167         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] SetValue Failed", GetErrorMessage(r));
168
169         return r;
170
171 CATCH:
172         return r;
173 }
174
175 result
176 _VideoEncoderImpl::SetValue(MediaPropertyType key, bool value)
177 {
178         result r = E_SUCCESS;
179
180         SysTryReturnResult(NID_MEDIA, __pEnc, E_INVALID_STATE, "not constructed");
181
182         r = __pEnc->SetValue(key, value);
183         SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] SetValue Failed", GetErrorMessage(r));
184
185         return r;
186
187 CATCH:
188         return r;
189 }
190
191 Tizen::Base::Collection::IListT<MediaPropertyType>*
192 _VideoEncoderImpl::GetSupportedPropertyListN(void) const
193 {
194         SysTryReturn(NID_MEDIA, __pEnc, null, E_INVALID_STATE, "[E_INVALID_STATE] not constructed");
195
196         return __pEnc->GetSupportedPropertyListN();
197 }
198
199 bool
200 _VideoEncoderImpl::IsPropertySupported(MediaPropertyType key) const
201 {
202         SysTryReturnResult(NID_MEDIA, __pEnc, E_INVALID_STATE, "not constructed");
203
204         return __pEnc->IsPropertySupported(key);
205 }
206
207 Tizen::Base::Collection::IListT<CodecType>*
208 _VideoEncoderImpl::GetSupportedCodecListN(void)
209 {
210         _CodecFactory* pFactory = null;
211         Tizen::Base::Collection::IListT<CodecType>* pList = null;
212
213         pFactory = _CodecFactory::GetInstance();
214         SysTryReturn(NID_MEDIA, pFactory != null, null, E_OUT_OF_MEMORY, "[%s] Propagated.",
215                                 GetErrorMessage(GetLastResult()));
216         pList = pFactory->GetSupportedVideoEncoderListN();
217         SysTryReturn(NID_MEDIA, pList != null, null, E_OUT_OF_MEMORY, "[%s] Propagated.",
218                                 GetErrorMessage(GetLastResult()));
219         SetLastResult(E_SUCCESS);
220         return pList;
221 }
222
223 }} // Tizen::Media