From 4d8658bdfd03b69ff67c68f84a0f700e69acd61a Mon Sep 17 00:00:00 2001 From: Minje Ahn Date: Fri, 10 Feb 2017 09:00:25 +0900 Subject: [PATCH] [ThumbnailExtractor] code cleanup Simplify error checking Add constructor using Size class Correction for readability Change-Id: Icc4ff93e5f09d005272b292890394ae0d981316e Signed-off-by: Minje Ahn --- packaging/csapi-multimedia.spec | 2 +- .../ThumbnailExtractor/ThumbnailExtractor.cs | 163 +++++++++++---------- .../ThumbnailExtractorErrorFactory.cs | 8 +- 3 files changed, 95 insertions(+), 78 deletions(-) diff --git a/packaging/csapi-multimedia.spec b/packaging/csapi-multimedia.spec index aa97af9..fcc011f 100644 --- a/packaging/csapi-multimedia.spec +++ b/packaging/csapi-multimedia.spec @@ -1,6 +1,6 @@ Name: csapi-multimedia Summary: Tizen Multimedia API for C# -Version: 1.0.30 +Version: 1.0.31 Release: 0 Group: Development/Libraries License: Apache-2.0 diff --git a/src/Tizen.Multimedia/ThumbnailExtractor/ThumbnailExtractor.cs b/src/Tizen.Multimedia/ThumbnailExtractor/ThumbnailExtractor.cs index 49f6241..bdac381 100755 --- a/src/Tizen.Multimedia/ThumbnailExtractor/ThumbnailExtractor.cs +++ b/src/Tizen.Multimedia/ThumbnailExtractor/ThumbnailExtractor.cs @@ -25,6 +25,7 @@ namespace Tizen.Multimedia { internal const string LogTag = "Tizen.Multimedia.ThumbnailExtractor"; } + /// /// The Thumbnail extractor class provides a set of functions to extract the thumbnail data of the input media file /// @@ -32,6 +33,7 @@ namespace Tizen.Multimedia { private bool _disposed = false; internal IntPtr _handle = IntPtr.Zero; + /// /// Thumbnail extractor constructor /// @@ -41,96 +43,105 @@ namespace Tizen.Multimedia /// The path of the media file to extract the thumbnail data public ThumbnailExtractor(string path) { - ThumbnailExtractorError ret = ThumbnailExtractorError.None; - if (path == null) { - Log.Error(ThumbnailExtractorLog.LogTag, "Path is NULL"); throw new ArgumentNullException(nameof(path)); } - else + + ThumbnailExtractorError ret = Interop.ThumbnailExtractor.Create(out _handle); + ThumbnailExtractorErrorFactory.ThrowIfError(ret, "Failed to create constructor"); + + try { - ret = Interop.ThumbnailExtractor.Create(out _handle); - if (ret != ThumbnailExtractorError.None) - { - Log.Error(ThumbnailExtractorLog.LogTag, "Failed to create constructor" + ret); - ThumbnailExtractorErrorFactory.ThrowException(ret, "Failed to create constructor"); - } - ret = Interop.ThumbnailExtractor.SetPath(_handle, path); - if (ret != ThumbnailExtractorError.None) - { - Log.Error(ThumbnailExtractorLog.LogTag, "Failed to set path" + ret); - Interop.ThumbnailExtractor.Destroy(_handle); - _handle = IntPtr.Zero; - ThumbnailExtractorErrorFactory.ThrowException(ret, "Failed to set path"); - } + ThumbnailExtractorErrorFactory.ThrowIfError( + Interop.ThumbnailExtractor.SetPath(_handle, path), "Failed to set the path"); + } + catch (Exception) + { + Interop.ThumbnailExtractor.Destroy(_handle); + _handle = IntPtr.Zero; + throw; } } - /// - /// Thumbnail extractor constructor - /// - /// - /// If you need default size thumbnail, use ThumbnailExtractor(path). Default size is 320x240. - /// If the set width is not a multiple of 8, it can be changed by inner process. - /// The width will be a multiple of 8 greater than the set value. - /// - /// The path of the media file to extract the thumbnail data - /// The width of the thumbnail - /// The height of the thumbnail - public ThumbnailExtractor(string path, int width, int height) - { - ThumbnailExtractorError ret = ThumbnailExtractorError.None; + private void Create(String path, int width, int height) + { if (path == null) { - throw new ArgumentNullException(nameof(path), "Path is NULL"); - } - else if (width <= 0) - { - throw new ArgumentOutOfRangeException(nameof(width), "Wrong width [" + width + "]"); + throw new ArgumentNullException(nameof(path)); } - else if (height <= 0) + + if (width <= 0) { - throw new ArgumentOutOfRangeException(nameof(height), "Wrong width [" + height + "]"); + throw new ArgumentOutOfRangeException(nameof(width), "The width must be greater than zero:[" + width + "]"); } - ret = Interop.ThumbnailExtractor.Create(out _handle); - if (ret != ThumbnailExtractorError.None) + if (height <= 0) { - Log.Error(ThumbnailExtractorLog.LogTag, "Failed to create constructor" + ret); - ThumbnailExtractorErrorFactory.ThrowException(ret, "Failed to create constructor"); + throw new ArgumentOutOfRangeException(nameof(height), "The height must be greater than zero:[" + height + "]"); } + ThumbnailExtractorError ret = Interop.ThumbnailExtractor.Create(out _handle); + ThumbnailExtractorErrorFactory.ThrowIfError(ret, "Failed to create constructor"); + try { - ret = Interop.ThumbnailExtractor.SetPath(_handle, path); - if (ret != ThumbnailExtractorError.None) - { - Log.Error(ThumbnailExtractorLog.LogTag, "Failed to set path" + ret); - ThumbnailExtractorErrorFactory.ThrowException(ret, "Failed to set path"); - } - ret = Interop.ThumbnailExtractor.SetSize(_handle, width, height); - if (ret != ThumbnailExtractorError.None) - { - Log.Error(ThumbnailExtractorLog.LogTag, "Failed to set size" + ret); - ThumbnailExtractorErrorFactory.ThrowException(ret, "Failed to set size"); - } + ThumbnailExtractorErrorFactory.ThrowIfError( + Interop.ThumbnailExtractor.SetPath(_handle, path), "Failed to set the path"); + + ThumbnailExtractorErrorFactory.ThrowIfError( + Interop.ThumbnailExtractor.SetSize(_handle, width, height), "Failed to set the size"); } catch (Exception) { Interop.ThumbnailExtractor.Destroy(_handle); _handle = IntPtr.Zero; + throw; } } - private Task ExtractRequest() + /// + /// Thumbnail extractor constructor + /// + /// + /// If you need default size thumbnail, use ThumbnailExtractor(path). Default size is 320x240. + /// If the set width is not a multiple of 8, it can be changed by inner process. + /// The width will be a multiple of 8 greater than the set value. + /// + /// The path of the media file to extract the thumbnail data + /// The width of the thumbnail + /// The height of the thumbnail + public ThumbnailExtractor(string path, int width, int height) + { + Create(path, width, height); + } + + /// + /// Thumbnail extractor constructor + /// + /// + /// If you need default size thumbnail, use ThumbnailExtractor(path). Default size is 320x240. + /// If the set width is not a multiple of 8, it can be changed by inner process. + /// The width will be a multiple of 8 greater than the set value. + /// + /// The path of the media file to extract the thumbnail data + /// The size of the media file to extract the thumbnail data + public ThumbnailExtractor(string path, Size size) + { + Create(path, size.Width, size.Height); + } + + /// + /// Extract thumbnail + /// + /// ThumbData object + public Task Extract() { if (_handle == IntPtr.Zero) { throw new ObjectDisposedException(nameof(ThumbnailExtractor), "Failed to extract thumbnail"); } - IntPtr id = IntPtr.Zero; var task = new TaskCompletionSource(); Interop.ThumbnailExtractor.ThumbnailExtractCallback extractCallback = (ThumbnailExtractorError error, @@ -143,39 +154,40 @@ namespace Tizen.Multimedia { if (error == ThumbnailExtractorError.None) { - byte[] tmpBuf = new byte[thumbSize]; - Marshal.Copy(thumbData, tmpBuf, 0, thumbSize); - Interop.Libc.Free(thumbData); - task.TrySetResult(new ThumbnailData(tmpBuf, thumbWidth, thumbHeight)); + try + { + byte[] tmpBuf = new byte[thumbSize]; + Marshal.Copy(thumbData, tmpBuf, 0, thumbSize); + + task.SetResult(new ThumbnailData(tmpBuf, thumbWidth, thumbHeight)); + } + catch (Exception) + { + task.SetException(new InvalidOperationException("[" + error + "] Fail to copy data")); + } + finally + { + Interop.Libc.Free(thumbData); + } } else { - Log.Error(ThumbnailExtractorLog.LogTag, "Failed to extract thumbnail" + error); task.SetException(new InvalidOperationException("["+ error +"] Fail to create thumbnail")); } }; + + IntPtr id = IntPtr.Zero; ThumbnailExtractorError res = Interop.ThumbnailExtractor.Extract(_handle, extractCallback, IntPtr.Zero, out id); if (id != IntPtr.Zero) { Interop.Libc.Free(id); id = IntPtr.Zero; } - if (res != ThumbnailExtractorError.None) - { - Log.Error(ThumbnailExtractorLog.LogTag, "Failed to extract thumbnail" + res); - ThumbnailExtractorErrorFactory.ThrowException(res, "Failed to extract thumbnail"); - } + + ThumbnailExtractorErrorFactory.ThrowIfError(res, "Failed to extract thumbnail"); return task.Task; } - /// - /// Extract thumbnail - /// - /// ThumbData object - public async Task Extract() - { - return await ExtractRequest(); - } /// /// Thumbnail Utility destructor @@ -193,6 +205,7 @@ namespace Tizen.Multimedia { // To be used if there are any other disposable objects } + if (_handle != IntPtr.Zero) { Interop.ThumbnailExtractor.Destroy(_handle); diff --git a/src/Tizen.Multimedia/ThumbnailExtractor/ThumbnailExtractorErrorFactory.cs b/src/Tizen.Multimedia/ThumbnailExtractor/ThumbnailExtractorErrorFactory.cs index a7974ce..9546c32 100755 --- a/src/Tizen.Multimedia/ThumbnailExtractor/ThumbnailExtractorErrorFactory.cs +++ b/src/Tizen.Multimedia/ThumbnailExtractor/ThumbnailExtractorErrorFactory.cs @@ -36,18 +36,22 @@ namespace Tizen.Multimedia internal static class ThumbnailExtractorErrorFactory { - internal static void ThrowException(ThumbnailExtractorError errorCode, string errorMessage = null, string paramName = null) + internal static void ThrowIfError(ThumbnailExtractorError errorCode, string errorMessage) { switch (errorCode) { case ThumbnailExtractorError.InvalidParameter: - throw new ArgumentException("[" + errorCode.ToString() + "]" + errorMessage, paramName); + throw new ArgumentException("[" + errorCode.ToString() + "]" + errorMessage); + case ThumbnailExtractorError.OutOfMemory: throw new OutOfMemoryException("[" + errorCode.ToString() + "]" + errorMessage); + case ThumbnailExtractorError.InvalidOperation: throw new InvalidOperationException("[" + errorCode.ToString() + "]" + errorMessage); + case ThumbnailExtractorError.FileNoSpaceOnDevice: throw new IOException("[" + errorCode.ToString() + "] No space to write on the device"); + case ThumbnailExtractorError.PermissionDenied: throw new UnauthorizedAccessException("[" + errorCode.ToString() + "]" + errorMessage); } -- 2.7.4