Debian Codegraph @

Xpjproject/pjsip-apps/src/swig/java/sample.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2013 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 package org.pjsip.pjsua2.app;
20
21 import java.io.IOException;
22 import org.pjsip.pjsua2.*;
23 import org.pjsip.pjsua2.app.*;
24
25 class MyObserver implements MyAppObserver {
26 private static MyCall currentCall = null;
27 private boolean del_call_scheduled = false;
28 private MyApp app = null;
29
30 public MyObserver(MyApp app_) {
31 app = app_;
32 }
33
34 public void check_call_deletion()
35 {
36 if (del_call_scheduled && currentCall != null) {
37 currentCall.delete();
38 currentCall = null;
39 del_call_scheduled = false;
40 }
41 }
42
43 @Override
44 public void notifyRegState(int code, String reason, long expiration) {}
45
46 @Override
47 public void notifyIncomingCall(MyCall call) {
48 /* Auto answer. */
49 CallOpParam call_param = new CallOpParam();
50 call_param.setStatusCode(pjsip_status_code.PJSIP_SC_OK);
51 try {
52 currentCall = call;
53 currentCall.answer(call_param);
54 } catch (Exception e) {
55 System.out.println(e);
56 return;
57 }
58 }
59
60 @Override
61 public void notifyCallMediaState(MyCall call) {
62 }
63
64 @Override
65 public void notifyCallState(MyCall call) {
66 if (currentCall == null || call.getId() != currentCall.getId())
67 return;
68
69 CallInfo ci;
70 try {
71 ci = call.getInfo();
72 } catch (Exception e) {
73 ci = null;
74 }
75 if (ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) {
76 // Should not delete call instance in this context,
77 // so let's just schedule it, the call will be deleted
78 // in our main worker thread context.
79 del_call_scheduled = true;
80 }
81
82 }
83
84 @Override
85 public void notifyBuddyState(MyBuddy buddy) {}
86
87 @Override
88 public void notifyChangeNetwork() {}
89
90 @Override
91 public void notifyCallMediaEvent(MyCall call, OnCallMediaEventParam prm) {}
92
93 @Override
94 public void notifyTimer(OnTimerParam prm) {
95 app.ep.utilLogWrite(3, "-TIMER-", "OnTimer invoked: user data=" + prm.getUserData());
96 }
97 }
98
99 class MyShutdownHook extends Thread {
100 Thread thread;
101 MyShutdownHook(Thread thr) {
102 thread = thr;
103 }
104 public void run() {
105 thread.interrupt();
106 try {
107 thread.join();
108 } catch (Exception e) {
109 ;
110 }
111 }
112 }
113
114 public class sample {
115 private static MyApp app = new MyApp();
116 private static MyObserver observer = new MyObserver(app);
117 private static MyAccount account = null;
118 private static AccountConfig accCfg = null;
119 private static MyCall call = null;
120
121 // Snippet code to set native window to output video
122 /*
123 private void setOutputVidWin() {}
124 VideoWindowHandle vidWH = new VideoWindowHandle();
125 vidWH.getHandle().setWindow(getNativeWindow());
126 try {
127 currentCall.vidWin.setWindow(vidWH);
128 } catch (Exception e) {
129 System.out.println(e);
130 }
131 }
132 */
133
134 private static void runWorker() {
135 try {
136 app.init(observer, ".", true);
137
138 // Schedule a timer
139 long token = app.ep.utilTimerSchedule(1000, -100);
140 // Immediately cancel it, callback should not be invoked
141 app.ep.utilTimerCancel(token);
142
143 // Schedule another timer
144 app.ep.utilLogWrite(3, "-TIMER-", "Scheduling timer: timeout=1000ms, user data=-99");
145 app.ep.utilTimerSchedule(1000, -99);
146
147 } catch (Exception e) {
148 System.out.println(e);
149 app.deinit();
150 System.exit(-1);
151 }
152
153 if (app.accList.size() == 0) {
154 accCfg = new AccountConfig();
155 accCfg.setIdUri("sip:localhost");
156 account = app.addAcc(accCfg);
157
158 accCfg.setIdUri("sip:[email protected]");
159 AccountSipConfig sipCfg = accCfg.getSipConfig();
160 AuthCredInfoVector ciVec = sipCfg.getAuthCreds();
161 ciVec.add(new AuthCredInfo("Digest",
162 "*",
163 "test",
164 0,
165 "passwd"));
166
167 StringVector proxy = sipCfg.getProxies();
168 proxy.add("sip:sip.pjsip.org;transport=tcp");
169
170 AccountRegConfig regCfg = accCfg.getRegConfig();
171 regCfg.setRegistrarUri("sip:pjsip.org");
172 account = app.addAcc(accCfg);
173 } else {
174 account = app.accList.get(0);
175 accCfg = account.cfg;
176 }
177
178 try {
179 account.modify(accCfg);
180
181 /* Make call to self */
182 /*
183 call = new MyCall(app.accList.get(0), -1);
184 CallOpParam prm = new CallOpParam(true);
185 prm.getOpt().setAudioCount(1);
186 prm.getOpt().setVideoCount(0);
187 call.makeCall("sip:localhost:6000", prm);
188 */
189 } catch (Exception e) {}
190
191 while (!Thread.currentThread().isInterrupted()) {
192 // Handle events
193 MyApp.ep.libHandleEvents(10);
194
195 // Check if any call instance need to be deleted
196 observer.check_call_deletion();
197 }
198 app.deinit();
199 }
200
201 public static void main(String argv[]) {
202 Runtime.getRuntime().addShutdownHook(new MyShutdownHook(Thread.currentThread()));
203
204 runWorker();
205 }
206 }