Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Vision / MediaVision / ImageTracker.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 System.Threading.Tasks;
20 using InteropImage = Interop.MediaVision.Image;
21
22 namespace Tizen.Multimedia
23 {
24     /// <summary>
25     /// Provides the ability to track images on image sources.
26     /// </summary>
27     /// <since_tizen> 3</since_tizen>
28     public static class ImageTracker
29     {
30         /// <summary>
31         /// Tracks the given image tracking model on the current frame.
32         /// </summary>
33         /// <param name="source">The current image of sequence where image tracking model will be tracked.</param>
34         /// <param name="trackingModel">The image tracking model which processed as target of tracking.</param>
35         /// <returns>A task that represents the asynchronous tracking operation.</returns>
36         /// <exception cref="ArgumentNullException">
37         ///     <paramref name="source"/> is null.\n
38         ///     -or-\n
39         ///     <paramref name="trackingModel"/> is null.
40         /// </exception>
41         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
42         /// <exception cref="ObjectDisposedException">
43         ///     <paramref name="source"/> has already been disposed of.\n
44         ///     -or-\n
45         ///     <paramref name="trackingModel"/> has already been disposed of.
46         /// </exception>
47         /// <exception cref="ArgumentException"><paramref name="trackingModel"/> has no target.</exception>
48         /// <seealso cref="ImageTrackingModel.SetTarget(ImageObject)"/>
49         /// <since_tizen> 3</since_tizen>
50         public static async Task<Quadrangle> TrackAsync(MediaVisionSource source,
51             ImageTrackingModel trackingModel)
52         {
53             return await TrackAsync(source, trackingModel, null);
54         }
55
56         /// <summary>
57         /// Tracks the given image tracking model on the current frame and <see cref="ImageTrackingConfiguration"/>.
58         /// </summary>
59         /// <param name="source">The current image of sequence where image tracking model will be tracked.</param>
60         /// <param name="trackingModel">The image tracking model which processed as target of tracking.</param>
61         /// <param name="config">The configuration used for tracking. This value can be null.</param>
62         /// <returns>A task that represents the asynchronous tracking operation.</returns>
63         /// <exception cref="ArgumentNullException">
64         ///     <paramref name="source"/> is null.\n
65         ///     -or-\n
66         ///     <paramref name="trackingModel"/> is null.
67         /// </exception>
68         /// <exception cref="NotSupportedException">The feature is not supported.</exception>
69         /// <exception cref="ObjectDisposedException">
70         ///     <paramref name="source"/> has already been disposed of.\n
71         ///     -or-\n
72         ///     <paramref name="trackingModel"/> has already been disposed of.\n
73         ///     -or-\n
74         ///     <paramref name="config"/> has already been disposed of.
75         /// </exception>
76         /// <exception cref="ArgumentException"><paramref name="trackingModel"/> has no target.</exception>
77         /// <seealso cref="ImageTrackingModel.SetTarget(ImageObject)"/>
78         /// <since_tizen> 3</since_tizen>
79         public static async Task<Quadrangle> TrackAsync(MediaVisionSource source,
80             ImageTrackingModel trackingModel, ImageTrackingConfiguration config)
81         {
82             if (source == null)
83             {
84                 throw new ArgumentNullException(nameof(source));
85             }
86             if (trackingModel == null)
87             {
88                 throw new ArgumentNullException(nameof(trackingModel));
89             }
90
91             TaskCompletionSource<Quadrangle> tcs = new TaskCompletionSource<Quadrangle>();
92
93             using (var cb = ObjectKeeper.Get(GetCallback(tcs)))
94             {
95                 InteropImage.Track(source.Handle, trackingModel.Handle, EngineConfiguration.GetHandle(config),
96                     cb.Target).Validate("Failed to perform image tracking.");
97
98                 return await tcs.Task;
99             }
100         }
101
102         private static InteropImage.TrackedCallback GetCallback(TaskCompletionSource<Quadrangle> tcs)
103         {
104             return (IntPtr sourceHandle, IntPtr imageTrackingModelHandle, IntPtr engineCfgHandle, IntPtr locationPtr, IntPtr _) =>
105             {
106                 try
107                 {
108                     Quadrangle region = null;
109                     if (locationPtr != IntPtr.Zero)
110                     {
111                         region = Marshal.PtrToStructure<global::Interop.MediaVision.Quadrangle>(locationPtr).ToApiStruct();
112                     }
113
114                     Log.Info(MediaVisionLog.Tag, $"Image tracked, region : {region}");
115
116                     if (!tcs.TrySetResult(region))
117                     {
118                         Log.Info(MediaVisionLog.Tag, "Failed to set track result");
119                     }
120                 }
121                 catch (Exception e)
122                 {
123                     MultimediaLog.Error(MediaVisionLog.Tag, "Failed to handle track result", e);
124                     tcs.TrySetException(e);
125                 }
126             };
127
128         }
129     }
130 }