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