enable dual camera
[platform/framework/native/media.git] / src / FMedia_CamRef.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  * @file                        FMedia_CamRef.cpp
20  * @brief                       This file contains the implementation of the %_CamRef class.
21  *
22  */
23
24 #include <unique_ptr.h>
25 #include <pthread.h>
26 #include <FBaseSysLog.h>
27 #include "FMedia_CamRef.h"
28
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31
32 namespace Tizen { namespace Media
33 {
34
35 IList* _CamRef::__pList = null;
36
37 _CamRef::_CamRef(void)
38 :__pObj(null)
39 {
40 }
41
42 _CamRef::~_CamRef(void)
43 {
44         if (__pObj != null)
45         {
46                 UnRegisterObject(*__pObj);
47         }
48 }
49
50 result
51 _CamRef::Construct(const Tizen::Base::Object& obj)
52 {
53         result r = E_SUCCESS;
54         r = RegisterObject(obj);
55         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
56         __pObj = const_cast<Tizen::Base::Object*>(&obj);
57         return r;
58 }
59
60 void
61 _CamRef::InitSingleton()
62 {
63         result r = E_SUCCESS;
64         std::unique_ptr <ArrayList> pList (new (std::nothrow) ArrayList());
65         SysTryReturnVoidResult(NID_MEDIA, pList.get() != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.  The object is not created.");
66
67         r = pList->Construct();
68         SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
69
70         __pList = pList.release();
71         std::atexit(DestroySingleton);
72 }
73
74 void
75 _CamRef::DestroySingleton(void)
76 {
77         delete __pList;
78 }
79
80 bool
81 _CamRef::IsAlive(const Tizen::Base::Object& obj)
82 {
83         SysTryReturn(NID_MEDIA, __pList != null, false, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Any object was not found.");
84         return __pList->Contains(obj);
85 }
86
87 result
88 _CamRef::RegisterObject(const Tizen::Base::Object& obj)
89 {
90         result r = E_SUCCESS;
91
92         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
93         if (!__pList)
94         {
95                 pthread_once(&once_block, InitSingleton);
96                 r = GetLastResult();
97                 SysTryReturn(NID_MEDIA, __pList != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
98         }
99
100         r = __pList->Add(const_cast<Tizen::Base::Object*>(&obj));
101         SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
102         return r;
103 }
104
105 void
106 _CamRef::UnRegisterObject(const Tizen::Base::Object& obj)
107 {
108         result r = E_SUCCESS;
109         SysTryReturnVoidResult(NID_MEDIA, __pList != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] No object has been registered.");
110
111         r = __pList->Remove(obj);
112         SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
113 }
114
115 }}// Tizen::Media