Debian Codegraph @

Xpjproject/pjsip-apps/src/swig/csharp/sample/SimplePjsua2CS/PjSample.cs

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 using System;
20 using System.Diagnostics;
21 using System.Threading;
22 using pjsua2xamarin.pjsua2;
23
24 namespace pjsua2xamarin
25 {
26 public class MyAccount : Account
27 {
28 ~MyAccount()
29 {
30 Debug.WriteLine("*** MyAccount is being deleted");
31 }
32
33 override public void onRegState(OnRegStateParam prm)
34 {
35 AccountInfo ai = getInfo();
36 Debug.WriteLine("***" + (ai.regIsActive ? "" : "Un") +
37 "Register: code=" + prm.code);
38 }
39
40 override public void onIncomingCall(OnIncomingCallParam iprm)
41 {
42 Call call = new Call(this, iprm.callId);
43 CallInfo ci = call.getInfo();
44 CallOpParam prm = new CallOpParam();
45
46 Debug.WriteLine("*** Incoming Call: " + ci.remoteUri + " [" +
47 ci.stateText + "]");
48
49 prm.statusCode = (pjsip_status_code)200;
50 call.answer(prm);
51 }
52 }
53
54 public class MyLogWriter : LogWriter
55 {
56 override public void write(LogEntry entry)
57 {
58 Debug.WriteLine(entry.msg);
59 }
60
61 ~MyLogWriter()
62 {
63 Debug.WriteLine("*** MyLogWriter is being deleted");
64
65 // A bit hack here.
66 // Detach the native instance as the library deletes it automatically upon libDestroy().
67 swigCMemOwn = false;
68 }
69 }
70
71 public class PjSample
72 {
73 public static Endpoint ep = new Endpoint();
74 public static MyLogWriter writer = new MyLogWriter();
75 public static MyAccount acc = new MyAccount();
76
77 /* Preview of default capture video device */
78 private static VideoPreview vp = new VideoPreview(-1);
79
80 public PjSample()
81 {
82 }
83
84 private void checkThread(string name)
85 {
86 if (!ep.libIsThreadRegistered())
87 ep.libRegisterThread(name);
88 }
89
90 public void start()
91 {
92 try
93 {
94 ep.libCreate();
95
96 // Init library
97 EpConfig epConfig = new EpConfig();
98 epConfig.logConfig.writer = writer;
99 //epConfig.logConfig.filename = "PjSample.log";
100 if (epConfig.logConfig.filename.Length == 0)
101 {
102 // Omit newlines for logging to console/debug-output
103 epConfig.logConfig.decor &=
104 ~(uint)pj_log_decoration.PJ_LOG_HAS_NEWLINE;
105 }
106 ep.libInit(epConfig);
107
108 // Create transport
109 TransportConfig tcfg = new TransportConfig();
110 tcfg.port = 5080;
111 ep.transportCreate(pjsip_transport_type_e.PJSIP_TRANSPORT_UDP,
112 tcfg);
113 ep.transportCreate(pjsip_transport_type_e.PJSIP_TRANSPORT_TCP,
114 tcfg);
115
116 // Start library
117 ep.libStart();
118 Debug.WriteLine("*** PJSUA2 STARTED ***");
119
120 // Add account
121 AccountConfig accCfg = new AccountConfig();
122 accCfg.idUri = "sip:[email protected]";
123 accCfg.regConfig.registrarUri = "sip:sip.pjsip.org";
124 accCfg.sipConfig.authCreds.Add(
125 new AuthCredInfo("digest", "*", "test1", 0, "test1"));
126 acc.create(accCfg);
127 Debug.WriteLine("*** ACC CREATED ***");
128
129 }
130 catch (Exception err)
131 {
132 Debug.WriteLine("Exception: " + err.Message);
133 }
134 }
135
136 public void stop()
137 {
138 try
139 {
140 checkThread("pjsua2.stop");
141
142 acc.shutdown();
143 // Explicitly delete account when unused
144 acc.Dispose();
145
146 }
147 catch (Exception err)
148 {
149 Debug.WriteLine("Exception: " + err.Message);
150 }
151
152 new Thread(() =>
153 {
154 try
155 {
156 checkThread("pjsua2.stop.2");
157
158 Debug.WriteLine("*** DESTROYING PJSUA2 ***");
159 ep.libDestroy();
160 ep.Dispose();
161 Debug.WriteLine("*** PJSUA2 DESTROYED ***");
162 }
163 catch (Exception err)
164 {
165 Debug.WriteLine("Exception: " + err.Message);
166 }
167 }).Start();
168 }
169
170 public void startPreview(IntPtr hwnd)
171 {
172 try
173 {
174 VideoPreviewOpParam param = new VideoPreviewOpParam();
175 param.window.handle.window = hwnd;
176 param.format.init(Convert.ToUInt32(pjmedia_format_id.PJMEDIA_FORMAT_I420), 350, 250, 30);
177
178 // Video render operation needs to be invoked from non-main-thread.
179 new Thread(() =>
180 {
181 try
182 {
183 checkThread("pjsua2.startPreview");
184
185 vp.start(param);
186 Debug.WriteLine("Preview started");
187 }
188 catch (Exception err)
189 {
190 Debug.WriteLine("Exception: " + err.Message);
191 }
192 }).Start();
193
194 }
195 catch (Exception err)
196 {
197 Debug.WriteLine("Exception: " + err.Message);
198 }
199 }
200
201 public void updatePreviewWindow(IntPtr hwnd)
202 {
203 try
204 {
205 // Video render operation needs to be invoked from non-main-thread.
206 new Thread(() =>
207 {
208 try
209 {
210 checkThread("pjsua2.updatePreviewWindow");
211
212 VideoWindowHandle handle = new VideoWindowHandle();
213 handle.handle.window = hwnd;
214
215 VideoWindow window = vp.getVideoWindow();
216 window.setWindow(handle);
217
218 Debug.WriteLine("Preview window updated");
219 }
220 catch (Exception err)
221 {
222 Debug.WriteLine("Exception: " + err.Message);
223 }
224 }).Start();
225
226 }
227 catch (Exception err)
228 {
229 Debug.WriteLine("Exception: " + err.Message);
230 }
231 }
232
233 public void stopPreview()
234 {
235 try
236 {
237 checkThread("pjsua2.stopPreview");
238
239 vp.stop();
240 Debug.WriteLine("Preview stopped");
241 }
242 catch (Exception err)
243 {
244 Debug.WriteLine("Exception: " + err.Message);
245 }
246 }
247 }
248
249 }