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