Release 4.0.0-preview1-00295
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Vision / MediaVision / SurveillanceEngine.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.Runtime.InteropServices;
19 using static Interop.MediaVision.Surveillance;
20
21 namespace Tizen.Multimedia.Vision
22 {
23     /// <summary>
24     /// SurveillanceEngine is a base class for surveillance event triggers.
25     /// Media Vision Surveillance provides the functionality which can be utilized for creation of video surveillance systems.
26     /// </summary>
27     /// <seealso cref="MovementDetector"/>
28     /// <seealso cref="PersonAppearanceDetector"/>
29     /// <seealso cref="PersonRecognizer"/>
30     /// <since_tizen> 3 </since_tizen>
31     public abstract class SurveillanceEngine : IDisposable
32     {
33         private IntPtr _handle = IntPtr.Zero;
34         private bool _disposed = false;
35
36         /// <summary>
37         /// Initializes a new instance of the <see cref="SurveillanceEngine"/> class.
38         /// </summary>
39         /// <param name="eventType">The type of the event trigger.</param>
40         internal SurveillanceEngine(string eventType)
41         {
42             EventTriggerCreate(eventType, out _handle).Validate("Failed to create surveillance event trigger.");
43         }
44
45         /// <summary>
46         /// Finalizes an instance of the SurveillanceEngine class.
47         /// </summary>
48         ~SurveillanceEngine()
49         {
50             Dispose(false);
51         }
52
53         internal IntPtr Handle
54         {
55             get
56             {
57                 if (_disposed)
58                 {
59                     throw new ObjectDisposedException(GetType().Name);
60                 }
61                 return _handle;
62             }
63         }
64
65         /// <summary>
66         /// Sets and gets the ROI (Region Of Interest).
67         /// </summary>
68         /// <exception cref="ObjectDisposedException">The <see cref="SurveillanceEngine"/> has already been disposed of.</exception>
69         /// <since_tizen> 3 </since_tizen>
70         public Point[] Roi
71         {
72             get
73             {
74                 IntPtr roiPtr = IntPtr.Zero;
75                 try
76                 {
77                     int count = 0;
78                     GetEventTriggerRoi(Handle, out count, out roiPtr).Validate("Failed to get roi");
79
80                     Point[] points = new Point[count];
81                     IntPtr iterPtr = roiPtr;
82
83                     for (int i = 0; i < count; i++)
84                     {
85                         points[i] = Marshal.PtrToStructure<global::Interop.MediaVision.Point>(iterPtr).ToApiStruct();
86                         iterPtr = IntPtr.Add(iterPtr, Marshal.SizeOf<global::Interop.MediaVision.Point>());
87                     }
88
89                     return points;
90                 }
91                 finally
92                 {
93                     LibcSupport.Free(roiPtr);
94                 }
95             }
96             set
97             {
98                 int length = value == null ? 0 : value.Length;
99
100                 var points = value == null ? null : global::Interop.ToMarshalable(value);
101
102                 SetEventTriggerRoi(Handle, length, points).Validate("Failed to set roi");
103             }
104         }
105
106         internal abstract void OnEventDetected(IntPtr trigger, IntPtr source,
107             int streamId, IntPtr eventResult, IntPtr userData);
108
109         internal void InvokeAddSource(SurveillanceSource source, SurveillanceEngineConfiguration config)
110         {
111             if (source == null)
112             {
113                 throw new ArgumentNullException(nameof(source));
114             }
115             SubscribeEventTrigger(Handle, source.StreamId, EngineConfiguration.GetHandle(config),
116                 OnEventDetected).Validate("Failed to subscribe trigger");
117         }
118
119         /// <summary>
120         /// Removes the source from <see cref="SurveillanceEngine"/>.
121         /// </summary>
122         /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
123         /// <exception cref="ObjectDisposedException">The <see cref="SurveillanceEngine"/> has already been disposed of.</exception>
124         /// <exception cref="ArgumentException"><paramref name="source"/> has not been added.</exception>
125         /// <since_tizen> 3 </since_tizen>
126         public void RemoveSource(SurveillanceSource source)
127         {
128             if (source == null)
129             {
130                 throw new ArgumentNullException(nameof(source));
131             }
132             UnsubscribeEventTrigger(Handle, source.StreamId).Validate("Failed to unsubscribe event trigger");
133         }
134
135
136         /// <summary>
137         /// Releases all the resources used by the <see cref="SurveillanceEngine"/> object.
138         /// </summary>
139         public void Dispose()
140         {
141             Dispose(true);
142             GC.SuppressFinalize(this);
143         }
144
145         /// <summary>
146         /// Releases the resources used by the <see cref="SurveillanceEngine"/> object.
147         /// </summary>
148         /// <param name="disposing">
149         /// true to release both managed and unmanaged resources; otherwise false to release only unmanaged resources.
150         /// </param>
151         protected virtual void Dispose(bool disposing)
152         {
153             if (_disposed)
154             {
155                 return;
156             }
157
158             EventTriggerDestroy(_handle);
159             _disposed = true;
160         }
161     }
162 }