[MediaVision] Refactoring
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / 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     public abstract class SurveillanceEngine : IDisposable
31     {
32         private IntPtr _handle = IntPtr.Zero;
33         private bool _disposed = false;
34
35         /// <summary>
36         /// Initializes a new instance of the <see cref="SurveillanceEngine"/> class.
37         /// </summary>
38         /// <param name="eventType">The type of the event trigger</param>
39         internal SurveillanceEngine(string eventType)
40         {
41             EventTriggerCreate(eventType, out _handle).Validate("Failed to create surveillance event trigger.");
42         }
43
44         ~SurveillanceEngine()
45         {
46             Dispose(false);
47         }
48
49         internal IntPtr Handle
50         {
51             get
52             {
53                 if (_disposed)
54                 {
55                     throw new ObjectDisposedException(GetType().Name);
56                 }
57                 return _handle;
58             }
59         }
60
61         /// <summary>
62         /// Sets and gets ROI (Region Of Interest) to the event trigger
63         /// </summary>
64         /// <exception cref="ObjectDisposedException">The <see cref="SurveillanceEngine"/> has already been disposed of.</exception>
65         public Point[] Roi
66         {
67             get
68             {
69                 IntPtr roiPtr = IntPtr.Zero;
70                 try
71                 {
72                     int count = 0;
73                     GetEventTriggerRoi(Handle, out count, out roiPtr).Validate("Failed to get roi");
74
75                     Point[] points = new Point[count];
76                     IntPtr iterPtr = roiPtr;
77
78                     for (int i = 0; i < count; i++)
79                     {
80                         points[i] = Marshal.PtrToStructure<Interop.MediaVision.Point>(roiPtr).ToApiStruct();
81                         iterPtr = IntPtr.Add(iterPtr, Marshal.SizeOf<Interop.MediaVision.Point>());
82                     }
83
84                     return points;
85                 }
86                 finally
87                 {
88                     Interop.Libc.Free(roiPtr);
89                 }
90             }
91             set
92             {
93                 int length = value == null ? 0 : value.Length;
94
95                 var points = value == null ? null : Interop.ToMarshalable(value);
96
97                 SetEventTriggerRoi(Handle, length, points).Validate("Failed to set roi");
98             }
99         }
100
101         internal abstract void OnEventDetected(IntPtr trigger, IntPtr source,
102             int streamId, IntPtr eventResult, IntPtr userData);
103
104         /// <summary>
105         /// Subscribes trigger to process sources pushed from video identified by @a videoStreamId.
106         /// </summary>
107         internal void InvokeAddSource(SurveillanceSource source, SurveillanceEngineConfiguration config)
108         {
109             if (source == null)
110             {
111                 throw new ArgumentNullException(nameof(source));
112             }
113             SubscribeEventTrigger(Handle, source.StreamId, EngineConfiguration.GetHandle(config),
114                 OnEventDetected).Validate("Failed to subscribe trigger");
115         }
116
117         /// <summary>
118         /// Removes the source from <see cref="SurveillanceEngine"/>.
119         /// </summary>
120         /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
121         /// <exception cref="ObjectDisposedException">The <see cref="SurveillanceEngine"/> has already been disposed of.</exception>
122         /// <exception cref="ArgumentException"><paramref name="source"/> has not been added.</exception>
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         public void Dispose()
133         {
134             Dispose(true);
135             GC.SuppressFinalize(this);
136         }
137
138         protected virtual void Dispose(bool disposing)
139         {
140             if (_disposed)
141             {
142                 return;
143             }
144
145             EventTriggerDestroy(_handle);
146             _disposed = true;
147         }
148     }
149 }