2 * Copyright (c) 2016 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.Diagnostics;
19 using System.Runtime.InteropServices;
20 using System.Threading.Tasks;
22 using NativeTransform = Interop.ImageUtil.Transform;
24 namespace Tizen.Multimedia.Util
27 /// A base class for image transformations.
29 /// <since_tizen> 4 </since_tizen>
30 public abstract class ImageTransform
32 internal ImageTransform()
36 internal async Task<MediaPacket> RunAsync(TransformHandle handle, MediaPacket source)
38 Debug.Assert(source.Format is VideoMediaFormat);
39 ValidateFormat(source.Format as VideoMediaFormat);
41 var tcs = new TaskCompletionSource<MediaPacket>();
43 using (var cbKeeper = ObjectKeeper.Get(GetCallback(tcs, source)))
45 var result = NativeTransform.Run(handle, source.GetHandle(), cbKeeper.Target);
47 if (result == ImageUtilError.NotSupportedFormat)
49 throw new NotSupportedException(
50 GenerateNotSupportedErrorMessage(source.Format as VideoMediaFormat));
52 result.ThrowIfFailed("Failed to transform given packet with " + GetType());
54 return await tcs.Task;
58 private NativeTransform.TransformCompletedCallback GetCallback(TaskCompletionSource<MediaPacket> tcs,
61 return (nativehandle, errorCode, _) =>
63 if (errorCode == ImageUtilError.None)
67 tcs.TrySetResult(MediaPacket.From(Marshal.PtrToStructure<IntPtr>(nativehandle)));
71 tcs.TrySetException(e);
74 else if (errorCode == ImageUtilError.NotSupportedFormat)
76 tcs.TrySetException(new NotSupportedException(
77 GenerateNotSupportedErrorMessage(source.Format as VideoMediaFormat)));
81 tcs.TrySetException(errorCode.ToException("Image transformation failed"));
86 internal static TransformHandle CreateHandle()
88 NativeTransform.Create(out var handle).ThrowIfFailed("Failed to run ImageTransformer");
89 Debug.Assert(handle != null);
93 internal abstract string GenerateNotSupportedErrorMessage(VideoMediaFormat format);
95 internal abstract void Configure(TransformHandle handle);
97 internal virtual void ValidateFormat(VideoMediaFormat format)
101 internal virtual async Task<MediaPacket> ApplyAsync(MediaPacket source)
103 using (TransformHandle handle = CreateHandle())
107 return await RunAsync(handle, source);