2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Threading.Tasks;
19 using InteropRoi = Interop.MediaVision.RoiTracker;
21 namespace Tizen.Multimedia.Vision
24 /// Provides the ability to track ROI(Region Of Interest) area on image source.
26 /// <since_tizen> 10 </since_tizen>
27 public static class RoiTracker
29 /// <summary>Tracks ROI(Region Of Interest) on the source image.</summary>
30 /// <remarks><see cref="RoiTrackingConfiguration.Roi"/> should be set first correctly.</remarks>
31 /// <feature>http://tizen.org/feature/vision.roi_tracking</feature>
32 /// <param name="source">The source of the media user wants to track.</param>
33 /// <param name="config">The engine's configuration that will be used for tracking.</param>
34 /// <returns>A task that represents the asynchronous tracking operation.</returns>
35 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="config"/> is null.</exception>
36 /// <exception cref="ArgumentException"><paramref name="config.Roi"/> is not set correctly.</exception>
37 /// <exception cref="InvalidOperationException">Internal error.</exception>
38 /// <seealso cref="RoiTrackingConfiguration"/>
39 /// <since_tizen> 10 </since_tizen>
40 public static async Task<Rectangle> TrackAsync(MediaVisionSource source, RoiTrackingConfiguration config)
44 throw new ArgumentNullException(nameof(source));
48 throw new ArgumentNullException(nameof(config));
50 if (config._roi == null)
52 throw new ArgumentException(nameof(config.Roi));
55 if (!config.IsApplied)
57 // Configure() and Prepare() can be called every time but it's useless, because Native Vision FW can accept it once.
58 InteropRoi.Configure(config.GetRoiConfigHandle(), config.Handle).
59 Validate("Failed to configure roi tracking");
61 var initialRoi = config.Roi;
62 InteropRoi.Prepare(config.GetRoiConfigHandle(), initialRoi.X, initialRoi.Y, initialRoi.Width, initialRoi.Height).
63 Validate("Failed to prepare roi tracking");
65 config.IsApplied = true;
68 var tcs = new TaskCompletionSource<Rectangle>();
70 InteropRoi.RoiTrackedCallback callback = (handle, roi_, _) => tcs.TrySetResult(roi_.ToApiStruct());
72 using (var cb = ObjectKeeper.Get(callback))
74 InteropRoi.TrackRoi(config.GetRoiConfigHandle(), source.Handle, cb.Target).
75 Validate("Failed to track roi");
77 return await tcs.Task;