[MediaCodec] Add exception handling code (#211) (#216)
authorhsgwon <haesu.gwon@samsung.com>
Tue, 10 Apr 2018 01:39:04 +0000 (10:39 +0900)
committerGitHub <noreply@github.com>
Tue, 10 Apr 2018 01:39:04 +0000 (10:39 +0900)
src/Tizen.Multimedia.MediaCodec/Interop/Interop.Libraries.cs
src/Tizen.Multimedia.MediaCodec/Interop/Interop.MediaCodec.cs
src/Tizen.Multimedia.MediaCodec/Interop/Interop.MediaTool.cs
src/Tizen.Multimedia.MediaCodec/MediaCodec/MediaCodec.cs
src/Tizen.Multimedia.MediaCodec/MediaCodec/MediaCodecError.cs
src/Tizen.Multimedia.Recorder/Interop/Interop.Recorder.cs

index ba8ac5b..5f5e599 100644 (file)
  * limitations under the License.
  */
 
-namespace Tizen.Multimedia.MediaCodec
+internal static partial class Interop
 {
-    internal static partial class Interop
+    internal static partial class Libraries
     {
-        internal static partial class Libraries
-        {
-            public const string MediaCodec = "libcapi-media-codec.so.0";
-            public const string MediaTool = "libcapi-media-tool.so.0";
-        }
+        public const string MediaCodec = "libcapi-media-codec.so.0";
+        public const string MediaTool = "libcapi-media-tool.so.0";
     }
 }
index 0aad663..e7f3f2b 100644 (file)
 
 using System;
 using System.Runtime.InteropServices;
+using Tizen.Multimedia.MediaCodec;
 
-namespace Tizen.Multimedia.MediaCodec
+internal static partial class Interop
 {
-    internal static partial class Interop
+    internal static class MediaCodec
     {
-        internal static class MediaCodec
-        {
-            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-            internal delegate void InputBufferUsedCallback(IntPtr mediaPacket, IntPtr arg);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void InputBufferUsedCallback(IntPtr mediaPacket, IntPtr arg);
 
-            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-            internal delegate void OutputBufferAvailableCallback(IntPtr mediaPacket, IntPtr arg);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void OutputBufferAvailableCallback(IntPtr mediaPacket, IntPtr arg);
 
-            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-            internal delegate void ErrorCallback(int errorCode, IntPtr arg);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void ErrorCallback(int errorCode, IntPtr arg);
 
-            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-            internal delegate void EosCallback(IntPtr arg);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void EosCallback(IntPtr arg);
 
-            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-            internal delegate void BufferStatusCallback(int statusCode, IntPtr arg);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void BufferStatusCallback(int statusCode, IntPtr arg);
 
-            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-            internal delegate bool SupportedCodecCallback(int codecType, IntPtr arg);
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate bool SupportedCodecCallback(int codecType, IntPtr arg);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_create")]
-            internal static extern int Create(out IntPtr handle);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_create")]
+        internal static extern MediaCodecErrorCode Create(out IntPtr handle);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_destroy")]
-            internal static extern int Destroy(IntPtr handle);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_destroy")]
+        internal static extern MediaCodecErrorCode Destroy(IntPtr handle);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_codec")]
-            internal static extern int Configure(IntPtr handle, int codecType, int flags);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_codec")]
+        internal static extern MediaCodecErrorCode Configure(IntPtr handle, int codecType, int flags);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_vdec_info")]
-            internal static extern int SetVideoDecoderInfo(IntPtr handle, int width, int height);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_vdec_info")]
+        internal static extern MediaCodecErrorCode SetVideoDecoderInfo(IntPtr handle, int width, int height);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_venc_info")]
-            internal static extern int SetVideoEncoderInfo(IntPtr handle, int width, int height,
-                int fps, int targetBits);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_venc_info")]
+        internal static extern MediaCodecErrorCode SetVideoEncoderInfo(IntPtr handle, int width, int height,
+            int fps, int targetBits);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_adec_info")]
-            internal static extern int SetAudioDecoderInfo(IntPtr handle, int sampleRate, int channel,
-                int bit);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_adec_info")]
+        internal static extern MediaCodecErrorCode SetAudioDecoderInfo(IntPtr handle, int sampleRate, int channel,
+            int bit);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_aenc_info")]
-            internal static extern int SetAudioEncoderInfo(IntPtr handle, int sampleRate, int channel,
-                int bit, int bitRate);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_aenc_info")]
+        internal static extern MediaCodecErrorCode SetAudioEncoderInfo(IntPtr handle, int sampleRate, int channel,
+            int bit, int bitRate);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_prepare")]
-            internal static extern int Prepare(IntPtr handle);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_prepare")]
+        internal static extern MediaCodecErrorCode Prepare(IntPtr handle);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unprepare")]
-            internal static extern int Unprepare(IntPtr handle);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unprepare")]
+        internal static extern MediaCodecErrorCode Unprepare(IntPtr handle);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_process_input")]
-            internal static extern int Process(IntPtr handle, IntPtr mediaPacket, ulong timeoutInUs);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_process_input")]
+        internal static extern MediaCodecErrorCode Process(IntPtr handle, IntPtr mediaPacket, ulong timeoutInUs);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_get_output")]
-            internal static extern int GetOutput(IntPtr handle, out IntPtr packet, ulong timeoutInUs);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_get_output")]
+        internal static extern MediaCodecErrorCode GetOutput(IntPtr handle, out IntPtr packet, ulong timeoutInUs);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_flush_buffers")]
-            internal static extern int FlushBuffers(IntPtr handle);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_flush_buffers")]
+        internal static extern MediaCodecErrorCode FlushBuffers(IntPtr handle);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_input_buffer_used_cb")]
-            internal static extern int SetInputBufferUsedCb(IntPtr handle,
-                InputBufferUsedCallback cb, IntPtr arg = default(IntPtr));
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_input_buffer_used_cb")]
+        internal static extern MediaCodecErrorCode SetInputBufferUsedCb(IntPtr handle,
+            InputBufferUsedCallback cb, IntPtr arg = default(IntPtr));
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unset_input_buffer_used_cb")]
-            internal static extern int UnsetInputBufferUsedCb(IntPtr handle);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unset_input_buffer_used_cb")]
+        internal static extern MediaCodecErrorCode UnsetInputBufferUsedCb(IntPtr handle);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_output_buffer_available_cb")]
-            internal static extern int SetOutputBufferAvailableCb(IntPtr handle,
-                OutputBufferAvailableCallback cb, IntPtr arg = default(IntPtr));
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_output_buffer_available_cb")]
+        internal static extern MediaCodecErrorCode SetOutputBufferAvailableCb(IntPtr handle,
+            OutputBufferAvailableCallback cb, IntPtr arg = default(IntPtr));
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unset_output_buffer_available_cb")]
-            internal static extern int UnsetOutputBufferAvailableCb(IntPtr handle);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unset_output_buffer_available_cb")]
+        internal static extern MediaCodecErrorCode UnsetOutputBufferAvailableCb(IntPtr handle);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_error_cb")]
-            internal static extern int SetErrorCb(IntPtr handle, ErrorCallback cb, IntPtr arg = default(IntPtr));
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_error_cb")]
+        internal static extern MediaCodecErrorCode SetErrorCb(IntPtr handle, ErrorCallback cb, IntPtr arg = default(IntPtr));
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unset_error_cb")]
-            internal static extern int UnsetErrorCb(IntPtr handle);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unset_error_cb")]
+        internal static extern MediaCodecErrorCode UnsetErrorCb(IntPtr handle);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_eos_cb")]
-            internal static extern int SetEosCb(IntPtr handle, EosCallback cb, IntPtr arg = default(IntPtr));
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_eos_cb")]
+        internal static extern MediaCodecErrorCode SetEosCb(IntPtr handle, EosCallback cb, IntPtr arg = default(IntPtr));
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unset_eos_cb")]
-            internal static extern int UnsetEosCb(IntPtr handle);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unset_eos_cb")]
+        internal static extern MediaCodecErrorCode UnsetEosCb(IntPtr handle);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_buffer_status_cb")]
-            internal static extern int SetBufferStatusCb(IntPtr handle, BufferStatusCallback cb,
-                IntPtr arg = default(IntPtr));
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_set_buffer_status_cb")]
+        internal static extern MediaCodecErrorCode SetBufferStatusCb(IntPtr handle, BufferStatusCallback cb,
+            IntPtr arg = default(IntPtr));
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unset_buffer_status_cb")]
-            internal static extern int UnsetBufferStatusCb(IntPtr handle);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_unset_buffer_status_cb")]
+        internal static extern MediaCodecErrorCode UnsetBufferStatusCb(IntPtr handle);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_get_supported_type")]
-            internal static extern int GetSupportedType(IntPtr handle, int codecType, bool isEncoder,
-                out int value);
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_get_supported_type")]
+        internal static extern MediaCodecErrorCode GetSupportedType(IntPtr handle, int codecType, bool isEncoder,
+            out int value);
 
-            [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_foreach_supported_codec_static")]
-            internal static extern int ForeachSupportedCodec(SupportedCodecCallback cb, IntPtr arg);
-        }
+        [DllImport(Libraries.MediaCodec, EntryPoint = "mediacodec_foreach_supported_codec_static")]
+        internal static extern MediaCodecErrorCode ForeachSupportedCodec(SupportedCodecCallback cb, IntPtr arg);
     }
 }
+
index 59d6e7b..11a37e9 100644 (file)
 using System;
 using System.Runtime.InteropServices;
 
-namespace Tizen.Multimedia.MediaCodec
+internal static partial class Interop
 {
-    internal static partial class Interop
+    internal static class MediaPacket
     {
-        internal static class MediaPacket
-        {
-            [DllImport(Libraries.MediaTool, EntryPoint = "media_packet_destroy")]
-            internal static extern int Destroy(IntPtr handle);
-        }
+        [DllImport(Libraries.MediaTool, EntryPoint = "media_packet_destroy")]
+        internal static extern int Destroy(IntPtr handle);
     }
 }
index 2adb5e5..6b2bb83 100644 (file)
@@ -17,6 +17,7 @@
 using System;
 using System.Collections.Generic;
 using System.Diagnostics;
+using Native = Interop.MediaCodec;
 
 namespace Tizen.Multimedia.MediaCodec
 {
@@ -42,14 +43,7 @@ namespace Tizen.Multimedia.MediaCodec
         /// <since_tizen> 3 </since_tizen>
         public MediaCodec()
         {
-            int ret = Interop.MediaCodec.Create(out _handle);
-
-            if (ret == (int)MediaCodecErrorCode.InvalidOperation)
-            {
-                throw new InvalidOperationException("Not able to initialize a new media codec.");
-            }
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.Create(out _handle).ThrowIfFailed("Failed to create media codec.");
 
             RegisterInputProcessed();
             RegisterErrorOccurred();
@@ -71,7 +65,7 @@ namespace Tizen.Multimedia.MediaCodec
             {
                 if (_handle != IntPtr.Zero)
                 {
-                    Interop.MediaCodec.Destroy(_handle);
+                    Native.Destroy(_handle).ThrowIfFailed("Failed to destry media codec.");
                     _handle = IntPtr.Zero;
                 }
 
@@ -174,7 +168,7 @@ namespace Tizen.Multimedia.MediaCodec
             var videoCodecList = new List<MediaFormatVideoMimeType>();
             var audioCodecList = new List<MediaFormatAudioMimeType>();
 
-            Interop.MediaCodec.SupportedCodecCallback cb = (codecType, _) =>
+            Native.SupportedCodecCallback cb = (codecType, _) =>
             {
                 if ((codecType & CodecKindMask) == CodecKindVideo)
                 {
@@ -196,9 +190,7 @@ namespace Tizen.Multimedia.MediaCodec
                 return true;
             };
 
-            int ret = Interop.MediaCodec.ForeachSupportedCodec(cb, IntPtr.Zero);
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.ForeachSupportedCodec(cb, IntPtr.Zero).ThrowIfFailed("Failed to get supported codec.");
 
             _supportedVideoCodecs = videoCodecList.AsReadOnly();
             _supportedAudioCodecs = audioCodecList.AsReadOnly();
@@ -219,18 +211,7 @@ namespace Tizen.Multimedia.MediaCodec
         {
             ValidateNotDisposed();
 
-            int ret = Interop.MediaCodec.Prepare(_handle);
-
-            if (ret == (int)MediaCodecErrorCode.NotInitialized)
-            {
-                throw new InvalidOperationException("The codec is not configured.");
-            }
-            if (ret != (int)MediaCodecErrorCode.None)
-            {
-                throw new InvalidOperationException("Operation failed.");
-            }
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.Prepare(_handle).ThrowIfFailed("Failed to prepare media codec.");
         }
 
         /// <summary>
@@ -243,9 +224,7 @@ namespace Tizen.Multimedia.MediaCodec
         {
             ValidateNotDisposed();
 
-            int ret = Interop.MediaCodec.Unprepare(_handle);
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.Unprepare(_handle).ThrowIfFailed("Failed to unprepare media codec.");
         }
 
         /// <summary>
@@ -309,17 +288,13 @@ namespace Tizen.Multimedia.MediaCodec
 
             if (encoder)
             {
-                int ret = Interop.MediaCodec.SetAudioEncoderInfo(_handle, format.SampleRate,
-                    format.Channel, format.Bit, format.BitRate);
-
-                MultimediaDebug.AssertNoError(ret);
+                Native.SetAudioEncoderInfo(_handle, format.SampleRate, format.Channel, format.Bit, format.BitRate).
+                    ThrowIfFailed("Failed to set audio encoder information.");
             }
             else
             {
-                int ret = Interop.MediaCodec.SetAudioDecoderInfo(_handle, format.SampleRate,
-                    format.Channel, format.Bit);
-
-                MultimediaDebug.AssertNoError(ret);
+                Native.SetAudioDecoderInfo(_handle, format.SampleRate, format.Channel, format.Bit).
+                    ThrowIfFailed("Failed to set audio decoder information.");
             }
         }
 
@@ -338,16 +313,13 @@ namespace Tizen.Multimedia.MediaCodec
 
             if (encoder)
             {
-                int ret = Interop.MediaCodec.SetVideoEncoderInfo(_handle, format.Size.Width,
-                    format.Size.Height, format.FrameRate, format.BitRate / 1000);
-
-                MultimediaDebug.AssertNoError(ret);
+                Native.SetVideoEncoderInfo(_handle, format.Size.Width, format.Size.Height, format.FrameRate, format.BitRate / 1000).
+                    ThrowIfFailed("Failed to set video encoder information.");
             }
             else
             {
-                int ret = Interop.MediaCodec.SetVideoDecoderInfo(_handle, format.Size.Width, format.Size.Height);
-
-                MultimediaDebug.AssertNoError(ret);
+                Native.SetVideoDecoderInfo(_handle, format.Size.Width, format.Size.Height).
+                    ThrowIfFailed("Failed to set video decoder information.");
             }
         }
 
@@ -359,13 +331,7 @@ namespace Tizen.Multimedia.MediaCodec
 
             flags |= (int)supportType;
 
-            int ret = Interop.MediaCodec.Configure(_handle, codecType, flags);
-
-            if (ret == (int)MediaCodecErrorCode.NotSupportedOnDevice)
-            {
-                throw new NotSupportedException("The format is not supported.");
-            }
-            MultimediaDebug.AssertNoError(ret);
+            Native.Configure(_handle, codecType, flags).ThrowIfFailed("Failed to configure media codec.");
         }
 
         /// <summary>
@@ -389,14 +355,7 @@ namespace Tizen.Multimedia.MediaCodec
 
             MediaPacket.Lock packetLock = MediaPacket.Lock.Get(packet);
 
-            int ret = Interop.MediaCodec.Process(_handle, packetLock.GetHandle(), 0);
-
-            if (ret == (int)MediaCodecErrorCode.InvalidState)
-            {
-                throw new InvalidOperationException("The codec is in invalid state.");
-            }
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.Process(_handle, packetLock.GetHandle(), 0).ThrowIfFailed("Failed to process input."); ;
         }
 
         /// <summary>
@@ -409,9 +368,7 @@ namespace Tizen.Multimedia.MediaCodec
         {
             ValidateNotDisposed();
 
-            int ret = Interop.MediaCodec.FlushBuffers(_handle);
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.FlushBuffers(_handle).ThrowIfFailed("Failed to flush buffers.");
         }
 
         /// <summary>
@@ -461,11 +418,9 @@ namespace Tizen.Multimedia.MediaCodec
         private MediaCodecTypes GetCodecType(int mimeType, bool isEncoder)
         {
             int codecType = mimeType & CodecTypeMask;
-            int value = 0;
 
-            int ret = Interop.MediaCodec.GetSupportedType(_handle, codecType, isEncoder, out value);
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.GetSupportedType(_handle, codecType, isEncoder, out int value).
+                ThrowIfFailed("Failed to get supported media codec type.");
 
             return (MediaCodecTypes)value;
         }
@@ -484,7 +439,7 @@ namespace Tizen.Multimedia.MediaCodec
 
         #region OutputAvailable event
         private EventHandler<OutputAvailableEventArgs> _outputAvailable;
-        private Interop.MediaCodec.OutputBufferAvailableCallback _outputBufferAvailableCb;
+        private Native.OutputBufferAvailableCallback _outputBufferAvailableCb;
         private object _outputAvailableLock = new object();
 
         /// <summary>
@@ -529,7 +484,15 @@ namespace Tizen.Multimedia.MediaCodec
             {
                 if (_outputAvailable == null)
                 {
-                    Interop.MediaPacket.Destroy(packetHandle);
+                    try
+                    {
+                        Native.Destroy(packetHandle).ThrowIfFailed("Failed to destroy packet.");
+                    }
+                    catch (Exception)
+                    {
+                        // Do not throw exception in pinvoke callback.
+                    }
+
                     return;
                 }
 
@@ -540,7 +503,14 @@ namespace Tizen.Multimedia.MediaCodec
                 }
                 catch (Exception e)
                 {
-                    Interop.MediaPacket.Destroy(packetHandle);
+                    try
+                    {
+                        Native.Destroy(packetHandle).ThrowIfFailed("Failed to destroy packet.");
+                    }
+                    catch
+                    {
+                        // Do not throw exception in pinvoke callback.
+                    }
 
                     MultimediaLog.Error(typeof(MediaCodec).FullName, "Failed to raise OutputAvailable event", e);
                 }
@@ -551,21 +521,18 @@ namespace Tizen.Multimedia.MediaCodec
                 }
             };
 
-            int ret = Interop.MediaCodec.SetOutputBufferAvailableCb(_handle, _outputBufferAvailableCb);
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.SetOutputBufferAvailableCb(_handle, _outputBufferAvailableCb).
+                ThrowIfFailed("Failed to set output buffer available callback.");
         }
 
         private void UnregisterOutputAvailableCallback()
         {
-            int ret = Interop.MediaCodec.UnsetOutputBufferAvailableCb(_handle);
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.UnsetOutputBufferAvailableCb(_handle).ThrowIfFailed("Failed to unregister output available callback.");
         }
         #endregion
 
         #region InputProcessed event
-        private Interop.MediaCodec.InputBufferUsedCallback _inputBufferUsedCb;
+        private Native.InputBufferUsedCallback _inputBufferUsedCb;
 
         /// <summary>
         /// Occurs when an input packet is processed.
@@ -593,14 +560,13 @@ namespace Tizen.Multimedia.MediaCodec
                 InputProcessed?.Invoke(this, new InputProcessedEventArgs(packet));
             };
 
-            int ret = Interop.MediaCodec.SetInputBufferUsedCb(_handle, _inputBufferUsedCb);
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.SetInputBufferUsedCb(_handle, _inputBufferUsedCb).
+                ThrowIfFailed("Failed to set input buffer used callback.");
         }
         #endregion
 
         #region ErrorOccurred event
-        private Interop.MediaCodec.ErrorCallback _errorCb;
+        private Native.ErrorCallback _errorCb;
 
         /// <summary>
         /// Occurs whenever an error is produced in the codec.
@@ -617,14 +583,12 @@ namespace Tizen.Multimedia.MediaCodec
 
                 ErrorOccurred?.Invoke(this, new MediaCodecErrorOccurredEventArgs(error));
             };
-            int ret = Interop.MediaCodec.SetErrorCb(_handle, _errorCb);
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.SetErrorCb(_handle, _errorCb).ThrowIfFailed("Failed to set error callback.");
         }
         #endregion
 
         #region EosReached event
-        private Interop.MediaCodec.EosCallback _eosCb;
+        private Native.EosCallback _eosCb;
 
         /// <summary>
         /// Occurs when the codec processes all input data.
@@ -636,15 +600,12 @@ namespace Tizen.Multimedia.MediaCodec
         {
             _eosCb = _ => EosReached?.Invoke(this, EventArgs.Empty);
 
-            int ret = Interop.MediaCodec.SetEosCb(_handle, _eosCb);
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.SetEosCb(_handle, _eosCb).ThrowIfFailed("Failed to set eos callback.");
         }
-
         #endregion
 
         #region BufferStatusChanged event
-        private Interop.MediaCodec.BufferStatusCallback _bufferStatusCb;
+        private Native.BufferStatusCallback _bufferStatusCb;
 
         /// <summary>
         /// Occurs when the codec needs more data or has enough data.
@@ -663,9 +624,8 @@ namespace Tizen.Multimedia.MediaCodec
                     new BufferStatusChangedEventArgs((MediaCodecStatus)statusCode));
             };
 
-            int ret = Interop.MediaCodec.SetBufferStatusCb(_handle, _bufferStatusCb);
-
-            MultimediaDebug.AssertNoError(ret);
+            Native.SetBufferStatusCb(_handle, _bufferStatusCb).
+                ThrowIfFailed("Failed to set buffer status callback.");
         }
         #endregion
     }
index 0f11ff0..b55e548 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+using System;
+using System.IO;
 using Tizen.Internals.Errors;
 
 namespace Tizen.Multimedia.MediaCodec
@@ -71,4 +73,55 @@ namespace Tizen.Multimedia.MediaCodec
         /// </summary>
         InvalidStream = MediaCodecErrorCode.InvalidStream,
     }
+
+    internal static class MediaCodecErrorExtensions
+    {
+        internal static void ThrowIfFailed(this MediaCodecErrorCode errorCode, string message)
+        {
+            if (errorCode == MediaCodecErrorCode.None)
+            {
+                return;
+            }
+
+            string msg = $"{ (message ?? "Operation failed") } : { errorCode.ToString() }.";
+
+            switch (errorCode)
+            {
+                case MediaCodecErrorCode.OutOfMemory:
+                    throw new OutOfMemoryException(msg);
+
+                case MediaCodecErrorCode.InvalidParameter:
+                    throw new ArgumentException(msg);
+
+                case MediaCodecErrorCode.CodecNotFound:
+                case MediaCodecErrorCode.DecodingError:
+                case MediaCodecErrorCode.InvalidOperation:
+                case MediaCodecErrorCode.InvalidState:
+                case MediaCodecErrorCode.InvalidInBuffer:
+                case MediaCodecErrorCode.InvalidOutBuffer:
+                case MediaCodecErrorCode.Internal:
+                case MediaCodecErrorCode.NotInitialized:
+                case MediaCodecErrorCode.InvalidStream:
+                case MediaCodecErrorCode.StreamNotFound:
+                    throw new InvalidOperationException(msg);
+
+                case MediaCodecErrorCode.NotSupportedOnDevice:
+                case MediaCodecErrorCode.NotSupportedFormat:
+                    throw new NotSupportedException(msg);
+
+                case MediaCodecErrorCode.PermissionDenied:
+                    throw new UnauthorizedAccessException(msg);
+
+                case MediaCodecErrorCode.OutOfStorage:
+                    throw new IOException($"{msg}; Not enough disk space or specified path is not available to .");
+
+                case MediaCodecErrorCode.NoAvailableBuffer:
+                case MediaCodecErrorCode.OverflowInBuffer:
+                    throw new InternalBufferOverflowException(msg);
+
+                default:
+                    throw new Exception($"Unknown error : {errorCode.ToString()}.");
+            }
+        }
+    }
 }
index 5fa70fb..06fb0da 100644 (file)
@@ -20,7 +20,6 @@ using Tizen.Multimedia;
 
 internal static partial class Interop
 {
-
     internal static partial class Recorder
     {
         [DllImport(Libraries.Recorder, EntryPoint = "recorder_create_audiorecorder")]