[Camera] Add internal constructor (#3489)
authorHaesu Gwon <haesu.gwon@samsung.com>
Thu, 2 Sep 2021 13:41:26 +0000 (22:41 +0900)
committerGitHub <noreply@github.com>
Thu, 2 Sep 2021 13:41:26 +0000 (22:41 +0900)
* [Camera] Add internal constructor and code clean

src/Tizen.Multimedia.Camera/Camera/Camera.cs
src/Tizen.Multimedia.Camera/Camera/CameraDeviceManager.cs
src/Tizen.Multimedia.Camera/Camera/CameraEnums.cs

index 9b1409606b8ebb2407599198ab83976b9dc37df0..2f26aae5da04e277d1ff70805c15a85a93aa298a 100644 (file)
@@ -59,8 +59,26 @@ namespace Tizen.Multimedia
         {
             ValidationUtil.ValidateEnum(typeof(CameraDevice), device, nameof(device));
 
-            Create(device);
+            CreateCameraDevice(device);
 
+            Initialize();
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="Camera"/> class.
+        /// </summary>
+        /// <remarks>CameraDevice and Type will be selected internally by CameraDeviceManager.</remarks>
+        /// <exception cref="InvalidOperationException">In case of any invalid operations.</exception>
+        /// <exception cref="NotSupportedException">The camera feature is not supported.</exception>
+        /// <since_tizen> 9 </since_tizen>
+        /// <feature> http://tizen.org/feature/camera </feature>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Camera() : this(CameraDevice.Default)
+        {
+        }
+
+        private void Initialize()
+        {
             Capabilities = new CameraCapabilities(this);
             Settings = new CameraSettings(this);
             DisplaySettings = new CameraDisplaySettings(this);
@@ -70,39 +88,46 @@ namespace Tizen.Multimedia
             SetState(CameraState.Created);
         }
 
-        private void Create(CameraDevice device)
+        private void CreateCameraDevice(CameraDevice device)
         {
             CameraDeviceType cameraDeviceType = CameraDeviceType.BuiltIn;
+            CameraDevice cameraDevice = device;
 
-            try
+            if (device == CameraDevice.Default || CameraDeviceManager.IsSupported)
             {
-                using (var cameraDeviceManager = new CameraDeviceManager())
+                var deviceInfo = GetDeviceInformation();
+                if (!deviceInfo.Any())
                 {
-                    var deviceInfo = cameraDeviceManager.GetDeviceInformation();
-                    if (!deviceInfo.Any())
-                    {
-                        throw new InvalidOperationException("There's no available camera device.");
-                    }
-
-                    cameraDeviceType = deviceInfo.First().Type;
+                    throw new InvalidOperationException("CDM is supported but, there's no available camera device.");
                 }
+
+                cameraDeviceType = deviceInfo.First().Type;
+                cameraDevice = deviceInfo.First().Device;
+                Log.Debug(CameraLog.Tag, $"Type:[{cameraDeviceType}], Device:[{cameraDevice}]");
             }
-            catch (NotSupportedException e)
+
+            CreateNativeCameraDevice(cameraDeviceType, cameraDevice);
+        }
+
+        private IEnumerable<CameraDeviceInformation> GetDeviceInformation()
+        {
+            using (var cameraDeviceManager = new CameraDeviceManager())
             {
-                Log.Info(CameraLog.Tag,
-                    $"CameraDeviceManager is not supported. {e.Message}. Not error.");
+                return cameraDeviceManager.GetDeviceInformation();
             }
+        }
 
-            if (cameraDeviceType == CameraDeviceType.BuiltIn ||
-                cameraDeviceType == CameraDeviceType.Usb)
+        private void CreateNativeCameraDevice(CameraDeviceType type, CameraDevice device)
+        {
+            if (type == CameraDeviceType.BuiltIn || type == CameraDeviceType.Usb)
             {
                 Native.Create(device, out _handle).
-                    ThrowIfFailed($"Failed to create {cameraDeviceType.ToString()} camera");
+                    ThrowIfFailed($"Failed to create {type} camera");
             }
             else
             {
                 Native.CreateNetworkCamera(device, out _handle).
-                    ThrowIfFailed($"Failed to create {cameraDeviceType.ToString()} camera");
+                    ThrowIfFailed($"Failed to create {type} camera");
             }
         }
 
index 995ef030669414b53003ec42c7f78c342fa25a21..c087f51eea1de9da5664bac52ea4b8c9d75c8c69 100644 (file)
@@ -83,6 +83,26 @@ namespace Tizen.Multimedia
             return GetDeviceInformation(deviceList);
         }
 
+        internal static bool IsSupported
+        {
+            get
+            {
+                try
+                {
+                    using (var cameraDeviceManager = new CameraDeviceManager())
+                    {
+                        return true;
+                    }
+                }
+                catch (NotSupportedException)
+                {
+                    Log.Info(CameraLog.Tag,
+                        $"CameraDeviceManager is not supported. Not error.");
+                }
+                return false;
+            }
+        }
+
         internal static IEnumerable<CameraDeviceInformation> GetDeviceInformation(Native.CameraDeviceListStruct list)
         {
             if (list.count == 0)
index ca5a5b05af465d2679d3c124407df4a6529abd76..9c505bb9710a853ce2f387534a1e5f440fd18706 100644 (file)
@@ -25,10 +25,16 @@ namespace Tizen.Multimedia
     /// <since_tizen> 3 </since_tizen>
     public enum CameraDevice
     {
+        /// <summary>
+        /// The CameraDevice will be decided internally by target policy.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        Default = -1,
         /// <summary>
         /// The rear camera device.
         /// </summary>
-        Rear,
+        Rear = 0,
         /// <summary>
         /// The front camera device.
         /// </summary>