Skip to content

Commit e167ef1

Browse files
author
Dogancan Ozturk
committed
removed C++ exceptions and added error handling on C# side with error codes for library loading
1 parent 8787906 commit e167ef1

File tree

9 files changed

+365
-110
lines changed

9 files changed

+365
-110
lines changed
0 Bytes
Binary file not shown.

H264Sharp/H264Decoder.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace H264Sharp
1111
public class H264Decoder : IDisposable
1212
{
1313

14-
private readonly IntPtr decoder;
14+
private IntPtr decoder = IntPtr.Zero;
1515
private int disposed=0;
1616

1717
private bool enableSSEYUVConversion;
@@ -36,15 +36,36 @@ private static void EnableDebug(bool value)
3636
/// </summary>
3737
public H264Decoder()
3838
{
39-
decoder = native.GetDecoder(Defines.CiscoDllName);
39+
LoadDecoder(Defines.CiscoDllName);
4040
}
4141

4242
/// <summary>
4343
/// Initialises new instance.
4444
/// </summary>
4545
public H264Decoder(string ciscoDllPath)
4646
{
47-
decoder = native.GetDecoder(ciscoDllPath);
47+
LoadDecoder(ciscoDllPath);
48+
}
49+
50+
private void LoadDecoder(string ciscoDllPath)
51+
{
52+
decoder = native.GetDecoder(ciscoDllPath, out int result);
53+
switch (result)
54+
{
55+
case 0:
56+
// Success
57+
break;
58+
case 1:
59+
throw new DllNotFoundException($"Failed to load the decoder library: {ciscoDllPath}");
60+
case 2:
61+
throw new EntryPointNotFoundException("Failed to load WelsCreateDecoder function");
62+
case 3:
63+
throw new EntryPointNotFoundException("Failed to load WelsDestroyDecoder function");
64+
case 4:
65+
throw new InvalidOperationException("Failed to create decoder instance");
66+
default:
67+
throw new Exception($"Unknown error occurred while loading decoder: {result}");
68+
}
4869
}
4970

5071
/// <summary>

H264Sharp/H264Encoder.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text;
23
using System.Threading;
34

45
namespace H264Sharp
@@ -8,7 +9,7 @@ namespace H264Sharp
89
/// </summary>
910
public class H264Encoder : IDisposable
1011
{
11-
private readonly IntPtr encoder;
12+
private IntPtr encoder = IntPtr.Zero;
1213
private bool disposedValue;
1314
private int disposed = 0;
1415
private static bool enableDebugPrints = false;
@@ -30,15 +31,36 @@ private static void EnableDebug(bool value)
3031
/// </summary>
3132
public H264Encoder()
3233
{
33-
encoder = native.GetEncoder(Defines.CiscoDllName);
34+
LoadEncoder(Defines.CiscoDllName);
3435
}
3536

3637
/// <summary>
3738
/// Creates new instance.
3839
/// </summary>
3940
public H264Encoder(string ciscoDllPath)
4041
{
41-
encoder = native.GetEncoder(ciscoDllPath);
42+
LoadEncoder(ciscoDllPath);
43+
}
44+
45+
private void LoadEncoder(string ciscoDllPath)
46+
{
47+
encoder = native.GetEncoder(ciscoDllPath, out int result);
48+
switch (result)
49+
{
50+
case 0:
51+
// Success
52+
break;
53+
case 1:
54+
throw new DllNotFoundException($"Failed to load the encoder library: {ciscoDllPath}");
55+
case 2:
56+
throw new EntryPointNotFoundException("Failed to load WelsCreateSVCEncoder function");
57+
case 3:
58+
throw new EntryPointNotFoundException("Failed to load WelsDestroySVCEncoder function");
59+
case 4:
60+
throw new InvalidOperationException("Failed to create encoder instance");
61+
default:
62+
throw new Exception($"Unknown error occurred while loading encoder: {result}");
63+
}
4264
}
4365

4466
/// <summary>

H264Sharp/NativeBindings.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class NativeBindings
1313
//---------------------------------------Definition-----------------------------------------------
1414
// Encoder
1515
private delegate void EnableDebugLogsd(int val);
16-
private delegate IntPtr GetEncoderd(string dllName);
16+
private delegate IntPtr GetEncoderd(string dllName, out int error);
1717
private delegate int InitializeEncoderBased(IntPtr encoder, TagEncParamBase param);
1818
private delegate int InitializeEncoderd(IntPtr encoder, int width, int height, int bps, int fps, int configType);
1919
private delegate int GetDefaultParamsd(IntPtr encoder, ref TagEncParamExt param);
@@ -28,7 +28,7 @@ public class NativeBindings
2828
private delegate int GetOptionEncoderd(IntPtr encoder, ENCODER_OPTION option, IntPtr value);
2929
private delegate int SetOptionEncoderd(IntPtr encoder, ENCODER_OPTION option, IntPtr value);
3030
// Decoder
31-
private delegate IntPtr GetDecoderd(string s);
31+
private delegate IntPtr GetDecoderd(string s, out int error);
3232
private delegate int InitializeDecoderDefaultd(IntPtr dec);
3333
private delegate int InitializeDecoderd(IntPtr dec, TagSVCDecodingParam param);
3434
private delegate int DecodeAsYUVd(IntPtr decoder, ref byte frame, int lenght, bool noDelay, ref int state, ref YUVImagePointer decoded);
@@ -540,8 +540,8 @@ private void LoadAndroidx64Bindings()
540540

541541
#region Interface
542542

543-
internal IntPtr GetEncoder(string dllName)
544-
=> getEncoder(dllName);
543+
internal IntPtr GetEncoder(string dllName, out int error)
544+
=> getEncoder(dllName, out error);
545545
internal int InitializeEncoderBase(IntPtr encoder, TagEncParamBase param)
546546
=> initializeEncoderBase(encoder, param);
547547
internal int InitializeEncoder(IntPtr encoder, int width, int height, int bps, int fps, int configType)
@@ -572,8 +572,8 @@ internal void EncoderEnableDebugLogs(int val)
572572
=> encoderEnableDebugLogs(val);
573573
// Decoder
574574

575-
internal IntPtr GetDecoder(string s)
576-
=> getDecoder(s);
575+
internal IntPtr GetDecoder(string s, out int error)
576+
=> getDecoder(s, out error);
577577
internal int InitializeDecoderDefault(IntPtr dec)
578578
=> initializeDecoderDefault(dec);
579579
internal int InitializeDecoder(IntPtr dec, TagSVCDecodingParam param)
@@ -623,7 +623,7 @@ public unsafe class Winx86
623623
[DllImport(DllName, EntryPoint = "EncoderEnableDebugLogs", CallingConvention = CallingConvention.Cdecl)]
624624
internal static extern void EncoderEnableDebugLogs(int val);
625625
[DllImport(DllName, EntryPoint = "GetEncoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
626-
internal static extern IntPtr GetEncoder(string dllName);
626+
internal static extern IntPtr GetEncoder(string dllName, out int error);
627627

628628
[DllImport(DllName, EntryPoint = "InitializeEncoderBase", CallingConvention = CallingConvention.Cdecl)]
629629
internal static extern int InitializeEncoderBase(IntPtr encoder, TagEncParamBase param);
@@ -670,7 +670,7 @@ public unsafe class Winx86
670670
internal static extern void DecoderEnableDebugLogs(int val);
671671

672672
[DllImport(DllName, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
673-
internal static extern IntPtr GetDecoder(string s);
673+
internal static extern IntPtr GetDecoder(string s, out int error);
674674

675675
[DllImport(DllName, EntryPoint = "InitializeDecoderDefault", CallingConvention = CallingConvention.Cdecl)]
676676
internal static extern int InitializeDecoderDefault(IntPtr dec);
@@ -737,7 +737,7 @@ public unsafe class Winx64
737737
[DllImport(DllName, EntryPoint = "EncoderEnableDebugLogs", CallingConvention = CallingConvention.Cdecl)]
738738
internal static extern void EncoderEnableDebugLogs(int val);
739739
[DllImport(DllName, EntryPoint = "GetEncoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
740-
internal static extern IntPtr GetEncoder(string dllName);
740+
internal static extern IntPtr GetEncoder(string dllName, out int error);
741741

742742
[DllImport(DllName, EntryPoint = "InitializeEncoderBase", CallingConvention = CallingConvention.Cdecl)]
743743
internal static extern int InitializeEncoderBase(IntPtr encoder, TagEncParamBase param);
@@ -784,7 +784,7 @@ public unsafe class Winx64
784784
internal static extern void DecoderEnableDebugLogs(int val);
785785

786786
[DllImport(DllName, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
787-
internal static extern IntPtr GetDecoder(string s);
787+
internal static extern IntPtr GetDecoder(string s, out int error);
788788

789789
[DllImport(DllName, EntryPoint = "InitializeDecoderDefault", CallingConvention = CallingConvention.Cdecl)]
790790
internal static extern int InitializeDecoderDefault(IntPtr dec);
@@ -852,7 +852,7 @@ public unsafe class Linuxx86
852852
[DllImport(DllName, EntryPoint = "EncoderEnableDebugLogs", CallingConvention = CallingConvention.Cdecl)]
853853
internal static extern void EncoderEnableDebugLogs(int val);
854854
[DllImport(DllName, EntryPoint = "GetEncoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
855-
internal static extern IntPtr GetEncoder(string dllName);
855+
internal static extern IntPtr GetEncoder(string dllName, out int error);
856856

857857
[DllImport(DllName, EntryPoint = "InitializeEncoderBase", CallingConvention = CallingConvention.Cdecl)]
858858
internal static extern int InitializeEncoderBase(IntPtr encoder, TagEncParamBase param);
@@ -899,7 +899,7 @@ public unsafe class Linuxx86
899899
internal static extern void DecoderEnableDebugLogs(int val);
900900

901901
[DllImport(DllName, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
902-
internal static extern IntPtr GetDecoder(string s);
902+
internal static extern IntPtr GetDecoder(string s, out int error);
903903

904904
[DllImport(DllName, EntryPoint = "InitializeDecoderDefault", CallingConvention = CallingConvention.Cdecl)]
905905
internal static extern int InitializeDecoderDefault(IntPtr dec);
@@ -966,7 +966,7 @@ public unsafe class Linuxx64
966966
[DllImport(DllName, EntryPoint = "EncoderEnableDebugLogs", CallingConvention = CallingConvention.Cdecl)]
967967
internal static extern void EncoderEnableDebugLogs(int val);
968968
[DllImport(DllName, EntryPoint = "GetEncoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
969-
internal static extern IntPtr GetEncoder(string dllName);
969+
internal static extern IntPtr GetEncoder(string dllName, out int error);
970970

971971
[DllImport(DllName, EntryPoint = "InitializeEncoderBase", CallingConvention = CallingConvention.Cdecl)]
972972
internal static extern int InitializeEncoderBase(IntPtr encoder, TagEncParamBase param);
@@ -1013,7 +1013,7 @@ public unsafe class Linuxx64
10131013
internal static extern void DecoderEnableDebugLogs(int val);
10141014

10151015
[DllImport(DllName, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
1016-
internal static extern IntPtr GetDecoder(string s);
1016+
internal static extern IntPtr GetDecoder(string s, out int error);
10171017

10181018
[DllImport(DllName, EntryPoint = "InitializeDecoderDefault", CallingConvention = CallingConvention.Cdecl)]
10191019
internal static extern int InitializeDecoderDefault(IntPtr dec);
@@ -1080,7 +1080,7 @@ public unsafe class LinuxArm32
10801080
[DllImport(DllName, EntryPoint = "EncoderEnableDebugLogs", CallingConvention = CallingConvention.Cdecl)]
10811081
internal static extern void EncoderEnableDebugLogs(int val);
10821082
[DllImport(DllName, EntryPoint = "GetEncoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
1083-
internal static extern IntPtr GetEncoder(string dllName);
1083+
internal static extern IntPtr GetEncoder(string dllName, out int error);
10841084

10851085
[DllImport(DllName, EntryPoint = "InitializeEncoderBase", CallingConvention = CallingConvention.Cdecl)]
10861086
internal static extern int InitializeEncoderBase(IntPtr encoder, TagEncParamBase param);
@@ -1127,7 +1127,7 @@ public unsafe class LinuxArm32
11271127
internal static extern void DecoderEnableDebugLogs(int val);
11281128

11291129
[DllImport(DllName, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
1130-
internal static extern IntPtr GetDecoder(string s);
1130+
internal static extern IntPtr GetDecoder(string s, out int error);
11311131

11321132
[DllImport(DllName, EntryPoint = "InitializeDecoderDefault", CallingConvention = CallingConvention.Cdecl)]
11331133
internal static extern int InitializeDecoderDefault(IntPtr dec);
@@ -1194,7 +1194,7 @@ public unsafe class LinuxArm64
11941194
[DllImport(DllName, EntryPoint = "EncoderEnableDebugLogs", CallingConvention = CallingConvention.Cdecl)]
11951195
internal static extern void EncoderEnableDebugLogs(int val);
11961196
[DllImport(DllName, EntryPoint = "GetEncoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
1197-
internal static extern IntPtr GetEncoder(string dllName);
1197+
internal static extern IntPtr GetEncoder(string dllName, out int error);
11981198

11991199
[DllImport(DllName, EntryPoint = "InitializeEncoderBase", CallingConvention = CallingConvention.Cdecl)]
12001200
internal static extern int InitializeEncoderBase(IntPtr encoder, TagEncParamBase param);
@@ -1241,7 +1241,7 @@ public unsafe class LinuxArm64
12411241
internal static extern void DecoderEnableDebugLogs(int val);
12421242

12431243
[DllImport(DllName, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
1244-
internal static extern IntPtr GetDecoder(string s);
1244+
internal static extern IntPtr GetDecoder(string s, out int error);
12451245

12461246
[DllImport(DllName, EntryPoint = "InitializeDecoderDefault", CallingConvention = CallingConvention.Cdecl)]
12471247
internal static extern int InitializeDecoderDefault(IntPtr dec);
@@ -1308,7 +1308,7 @@ public unsafe class AndroidArm64
13081308
[DllImport(DllName, EntryPoint = "EncoderEnableDebugLogs", CallingConvention = CallingConvention.Cdecl)]
13091309
internal static extern void EncoderEnableDebugLogs(int val);
13101310
[DllImport(DllName, EntryPoint = "GetEncoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
1311-
internal static extern IntPtr GetEncoder(string dllName);
1311+
internal static extern IntPtr GetEncoder(string dllName, out int error);
13121312

13131313
[DllImport(DllName, EntryPoint = "InitializeEncoderBase", CallingConvention = CallingConvention.Cdecl)]
13141314
internal static extern int InitializeEncoderBase(IntPtr encoder, TagEncParamBase param);
@@ -1355,7 +1355,7 @@ public unsafe class AndroidArm64
13551355
internal static extern void DecoderEnableDebugLogs(int val);
13561356

13571357
[DllImport(DllName, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
1358-
internal static extern IntPtr GetDecoder(string s);
1358+
internal static extern IntPtr GetDecoder(string s, out int error);
13591359

13601360
[DllImport(DllName, EntryPoint = "InitializeDecoderDefault", CallingConvention = CallingConvention.Cdecl)]
13611361
internal static extern int InitializeDecoderDefault(IntPtr dec);
@@ -1422,7 +1422,7 @@ public unsafe class AndroidArm32
14221422
[DllImport(DllName, EntryPoint = "EncoderEnableDebugLogs", CallingConvention = CallingConvention.Cdecl)]
14231423
internal static extern void EncoderEnableDebugLogs(int val);
14241424
[DllImport(DllName, EntryPoint = "GetEncoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
1425-
internal static extern IntPtr GetEncoder(string dllName);
1425+
internal static extern IntPtr GetEncoder(string dllName, out int error);
14261426

14271427
[DllImport(DllName, EntryPoint = "InitializeEncoderBase", CallingConvention = CallingConvention.Cdecl)]
14281428
internal static extern int InitializeEncoderBase(IntPtr encoder, TagEncParamBase param);
@@ -1469,7 +1469,7 @@ public unsafe class AndroidArm32
14691469
internal static extern void DecoderEnableDebugLogs(int val);
14701470

14711471
[DllImport(DllName, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
1472-
internal static extern IntPtr GetDecoder(string s);
1472+
internal static extern IntPtr GetDecoder(string s, out int error);
14731473

14741474
[DllImport(DllName, EntryPoint = "InitializeDecoderDefault", CallingConvention = CallingConvention.Cdecl)]
14751475
internal static extern int InitializeDecoderDefault(IntPtr dec);
@@ -1536,7 +1536,7 @@ public unsafe class Androidx64
15361536
[DllImport(DllName, EntryPoint = "EncoderEnableDebugLogs", CallingConvention = CallingConvention.Cdecl)]
15371537
internal static extern void EncoderEnableDebugLogs(int val);
15381538
[DllImport(DllName, EntryPoint = "GetEncoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
1539-
internal static extern IntPtr GetEncoder(string dllName);
1539+
internal static extern IntPtr GetEncoder(string dllName, out int error);
15401540

15411541
[DllImport(DllName, EntryPoint = "InitializeEncoderBase", CallingConvention = CallingConvention.Cdecl)]
15421542
internal static extern int InitializeEncoderBase(IntPtr encoder, TagEncParamBase param);
@@ -1583,7 +1583,7 @@ public unsafe class Androidx64
15831583
internal static extern void DecoderEnableDebugLogs(int val);
15841584

15851585
[DllImport(DllName, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
1586-
internal static extern IntPtr GetDecoder(string s);
1586+
internal static extern IntPtr GetDecoder(string s, out int error);
15871587

15881588
[DllImport(DllName, EntryPoint = "InitializeDecoderDefault", CallingConvention = CallingConvention.Cdecl)]
15891589
internal static extern int InitializeDecoderDefault(IntPtr dec);

0 commit comments

Comments
 (0)