From 7b57dbba5a81e074d1e703b9d991332933d76172 Mon Sep 17 00:00:00 2001 From: Nanang Izzuddin Date: Thu, 24 Jul 2025 14:01:51 +0700 Subject: [PATCH] Various fixes/updates in pjsua2 C# sample app SimplePjsua2CS - Fix compile error due to removal of VideoPreviewOpParam.window.handle.setWindow() in #4273. - Fix truncated captured video by matching video renderer's format size to the video renderer size. - Minor updates such as: use default video capture device (was colorbar/2), optional logging to file, add incoming call handling, indentations. - Update gitignore for intermediate files of this project. --- .gitignore | 3 + .../csharp/sample/SimplePjsua2CS/PjSample.cs | 74 ++++++++++++------- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 84cdbc1ff7..6e50ad219f 100644 --- a/.gitignore +++ b/.gitignore @@ -55,6 +55,9 @@ pjsip-apps/src/swig/java/android/app/src/main/jniLibs/ # SWIG CSharp/Xamarin stuff pjsip-apps/src/swig/csharp/pjsua2xamarin/ +pjsip-apps/src/swig/csharp/sample/SimplePjsua2CS/pjsua2 +pjsip-apps/src/swig/csharp/sample/SimplePjsua2CS/obj +pjsip-apps/src/swig/csharp/sample/SimplePjsua2CS/*.user # SWIG Python stuff pjsip-apps/src/swig/python/build/ diff --git a/pjsip-apps/src/swig/csharp/sample/SimplePjsua2CS/PjSample.cs b/pjsip-apps/src/swig/csharp/sample/SimplePjsua2CS/PjSample.cs index 00a21ea41d..45bba96601 100644 --- a/pjsip-apps/src/swig/csharp/sample/SimplePjsua2CS/PjSample.cs +++ b/pjsip-apps/src/swig/csharp/sample/SimplePjsua2CS/PjSample.cs @@ -25,16 +25,29 @@ namespace pjsua2xamarin { public class MyAccount : Account { - ~MyAccount() - { + ~MyAccount() + { Debug.WriteLine("*** MyAccount is being deleted"); - } + } override public void onRegState(OnRegStateParam prm) { - AccountInfo ai = getInfo(); - Debug.WriteLine("***" + (ai.regIsActive? "": "Un") + - "Register: code=" + prm.code); + AccountInfo ai = getInfo(); + Debug.WriteLine("***" + (ai.regIsActive ? "" : "Un") + + "Register: code=" + prm.code); + } + + override public void onIncomingCall(OnIncomingCallParam iprm) + { + Call call = new Call(this, iprm.callId); + CallInfo ci = call.getInfo(); + CallOpParam prm = new CallOpParam(); + + Debug.WriteLine("*** Incoming Call: " + ci.remoteUri + " [" + + ci.stateText + "]"); + + prm.statusCode = (pjsip_status_code)200; + call.answer(prm); } } @@ -61,8 +74,8 @@ public class PjSample public static MyLogWriter writer = new MyLogWriter(); public static MyAccount acc = new MyAccount(); - /* Preview of Colorbar */ - private static VideoPreview vp = new VideoPreview(2); + /* Preview of default capture video device */ + private static VideoPreview vp = new VideoPreview(-1); public PjSample() { @@ -83,7 +96,13 @@ public void start() // Init library EpConfig epConfig = new EpConfig(); epConfig.logConfig.writer = writer; - epConfig.logConfig.decor &= ~(uint)pj_log_decoration.PJ_LOG_HAS_NEWLINE; + //epConfig.logConfig.filename = "PjSample.log"; + if (epConfig.logConfig.filename.Length == 0) + { + // Omit newlines for logging to console/debug-output + epConfig.logConfig.decor &= + ~(uint)pj_log_decoration.PJ_LOG_HAS_NEWLINE; + } ep.libInit(epConfig); // Create transport @@ -131,21 +150,21 @@ public void stop() } new Thread(() => + { + try { - try - { - checkThread("pjsua2.stop.2"); - - Debug.WriteLine("*** DESTROYING PJSUA2 ***"); - ep.libDestroy(); - ep.Dispose(); - Debug.WriteLine("*** PJSUA2 DESTROYED ***"); - } - catch (Exception err) - { - Debug.WriteLine("Exception: " + err.Message); - } - }).Start(); + checkThread("pjsua2.stop.2"); + + Debug.WriteLine("*** DESTROYING PJSUA2 ***"); + ep.libDestroy(); + ep.Dispose(); + Debug.WriteLine("*** PJSUA2 DESTROYED ***"); + } + catch (Exception err) + { + Debug.WriteLine("Exception: " + err.Message); + } + }).Start(); } public void startPreview(IntPtr hwnd) @@ -153,9 +172,10 @@ public void startPreview(IntPtr hwnd) try { VideoPreviewOpParam param = new VideoPreviewOpParam(); - param.window.handle.setWindow(hwnd.ToInt64()); + param.window.handle.window = hwnd; + param.format.init(Convert.ToUInt32(pjmedia_format_id.PJMEDIA_FORMAT_I420), 350, 250, 30); - // Video render operation needs to be invoked from non-main-thread. + // Video render operation needs to be invoked from non-main-thread. new Thread(() => { try @@ -169,7 +189,7 @@ public void startPreview(IntPtr hwnd) { Debug.WriteLine("Exception: " + err.Message); } - }).Start(); + }).Start(); } catch (Exception err) @@ -190,7 +210,7 @@ public void updatePreviewWindow(IntPtr hwnd) checkThread("pjsua2.updatePreviewWindow"); VideoWindowHandle handle = new VideoWindowHandle(); - handle.handle.setWindow(hwnd.ToInt64()); + handle.handle.window = hwnd; VideoWindow window = vp.getVideoWindow(); window.setWindow(handle);