18b7e39b9dbf040a025e5342080b1c51eb8fbf62
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / MediaVision / EngineConfiguration.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using static Interop.MediaVision;
20
21 namespace Tizen.Multimedia
22 {
23     /// <summary>
24     /// This class represents an abstract EngineConfiguration class.\n
25     /// It provides dictionary functionality. It means that it is possible to set (key, value) pairs to this class \n
26     /// and use them to transfer these values to the engine part underlying Media Vision API. \n
27     /// Information on which attributes can be set is provided together with concrete engines.
28     /// </summary>
29     public abstract class EngineConfiguration : IDisposable
30     {
31         internal IntPtr _engineHandle = IntPtr.Zero;
32         private readonly IDictionary<string, object> _config = new Dictionary<string, object>();
33         private bool _disposed = false;
34
35         /// <summary>
36         /// Constructor of the EngineConfig class.
37         /// </summary>
38         internal EngineConfiguration()
39         {
40             int ret = Interop.MediaVision.EngineConfig.Create(out _engineHandle);
41             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to create media vision engine.");
42         }
43
44         /// <summary>
45         /// Destructor of the EngineConfig class.
46         /// </summary>
47         ~EngineConfiguration()
48         {
49             Dispose(false);
50         }
51
52         internal void Add<T>(string key, T value)
53         {
54             object val = (object)value;
55
56             if (!_config.ContainsKey(key))
57             {
58                 _config.Add(key, val);
59             }
60             else
61             {
62                 int ret = 0;
63                 if (typeof(T) == typeof(double))
64                 {
65                     ret = Interop.MediaVision.EngineConfig.SetDouble(_engineHandle, key, (double)val);
66                 }
67                 else if (typeof(T) == typeof(int))
68                 {
69                     ret = Interop.MediaVision.EngineConfig.SetInt(_engineHandle, key, (int)val);
70                 }
71                 else if (typeof(T) == typeof(bool))
72                 {
73                     ret = Interop.MediaVision.EngineConfig.SetBool(_engineHandle, key, (bool)val);
74                 }
75                 else if (typeof(T) == typeof(string))
76                 {
77                     ret = Interop.MediaVision.EngineConfig.SetString(_engineHandle, key, (string)val);
78                 }
79
80                 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to add attribute");
81                 _config[key] = val;
82             }
83         }
84
85         internal object Get(string key)
86         {
87             if (_config.ContainsKey(key))
88             {
89                 return _config[key];
90             }
91             else
92             {
93                 Log.Error(MediaVisionLog.Tag, "Attribute was not set");
94                 return null;
95             }
96         }
97
98         public void Dispose()
99         {
100             Dispose(true);
101             GC.SuppressFinalize(this);
102         }
103
104         protected virtual void Dispose(bool disposing)
105         {
106             if (_disposed)
107             {
108                 return;
109             }
110
111             if (disposing)
112             {
113                 // Free managed objects
114             }
115
116             Interop.MediaVision.EngineConfig.Destroy(_engineHandle);
117             _disposed = true;
118         }
119     }
120 }