Debian Codegraph @

Xpjproject/pjsip-apps/src/swig/java/sample2.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 import javafx.application.Application;
25 import javafx.event.ActionEvent;
26 import javafx.event.EventHandler;
27 import javafx.scene.Scene;
28 import javafx.scene.layout.StackPane;
29 import javafx.stage.Stage;
30 import com.sun.javafx.tk.TKStage;
31 import java.lang.reflect.Method;
32
33 class MyObserver implements MyAppObserver {
34 private static MyCall currentCall = null;
35 private static boolean del_call_scheduled = false;
36
37 public void check_call_deletion()
38 {
39 if (del_call_scheduled && currentCall != null) {
40 currentCall.delete();
41 currentCall = null;
42 del_call_scheduled = false;
43 }
44 }
45
46 @Override
47 public void notifyRegState(int code, String reason, long expiration) {}
48
49 @Override
50 public void notifyIncomingCall(MyCall call) {
51 /* Auto answer. */
52 CallOpParam call_param = new CallOpParam();
53 call_param.setStatusCode(pjsip_status_code.PJSIP_SC_OK);
54 try {
55 currentCall = call;
56 currentCall.answer(call_param);
57 } catch (Exception e) {
58 System.out.println(e);
59 return;
60 }
61 }
62
63 @Override
64 public void notifyCallMediaState(MyCall call) {
65 }
66
67 public void notifyCallState(MyCall call) {
68 if (currentCall == null || call.getId() != currentCall.getId())
69 return;
70
71 CallInfo ci;
72 try {
73 ci = call.getInfo();
74 } catch (Exception e) {
75 ci = null;
76 }
77 if (ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) {
78 // Should not delete call instance here, so let's schedule it.
79 // The call will be deleted by our main worker thread.
80 del_call_scheduled = true;
81 } else if (ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED) {
82 if (ci.getSetting().getVideoCount() != 0) {
83 System.out.println("Changing video window using " + sample2.hwnd);
84 // Change window
85 VideoWindowHandle vidWH = new VideoWindowHandle();
86 vidWH.getHandle().setWindow(sample2.hwnd);
87 try {
88 currentCall.vidWin.setWindow(vidWH);
89 } catch (Exception e) {
90 System.out.println(e);
91 }
92 }
93 }
94 }
95
96 @Override
97 public void notifyBuddyState(MyBuddy buddy) {}
98
99 @Override
100 public void notifyChangeNetwork() {}
101
102 @Override
103 public void notifyTimer(OnTimerParam prm) {
104 System.out.println("OnTimer invoked, user data=" + prm.getUserData());
105 }
106 }
107
108 class MyThread extends Thread {
109 private static MyApp app = new MyApp();
110 private static MyObserver observer = new MyObserver();
111 private static MyAccount account = null;
112 private static AccountConfig accCfg = null;
113
114 public void run() {
115 try {
116 app.init(observer, ".", true);
117 } catch (Exception e) {
118 System.out.println(e);
119 app.deinit();
120 System.exit(-1);
121 }
122
123 if (app.accList.size() == 0) {
124 accCfg = new AccountConfig();
125 accCfg.setIdUri("sip:localhost");
126 account = app.addAcc(accCfg);
127
128 accCfg.setIdUri("sip:[email protected]");
129 AccountSipConfig sipCfg = accCfg.getSipConfig();
130 AuthCredInfoVector ciVec = sipCfg.getAuthCreds();
131 ciVec.add(new AuthCredInfo("Digest",
132 "*",
133 "test1",
134 0,
135 "test1"));
136
137 StringVector proxy = sipCfg.getProxies();
138 proxy.add("sip:sip.pjsip.org;transport=tcp");
139
140 AccountRegConfig regCfg = accCfg.getRegConfig();
141 regCfg.setRegistrarUri("sip:pjsip.org");
142
143 accCfg.getVideoConfig().setAutoTransmitOutgoing(true);
144 accCfg.getVideoConfig().setAutoShowIncoming(true);
145 account = app.addAcc(accCfg);
146 } else {
147 account = app.accList.get(0);
148 accCfg = account.cfg;
149 }
150
151 try {
152 account.modify(accCfg);
153 } catch (Exception e) {}
154
155 while (!Thread.currentThread().isInterrupted()) {
156 // Handle events
157 MyApp.ep.libHandleEvents(10);
158
159 // Check if any call instance need to be deleted
160 observer.check_call_deletion();
161 try {
162 Thread.sleep(50);
163 } catch (InterruptedException ie) {
164 break;
165 }
166 }
167 app.deinit();
168 }
169 }
170
171 public class sample2 extends Application {
172 public static long hwnd;
173 private static Thread myThread = new MyThread();
174
175 private static long getWindowPointer(Stage stage) {
176 try {
177 TKStage tkStage = stage.impl_getPeer();
178 Method getPlatformWindow = tkStage.getClass().getDeclaredMethod("getPlatformWindow" );
179 getPlatformWindow.setAccessible(true);
180 Object platformWindow = getPlatformWindow.invoke(tkStage);
181 Method getNativeHandle = platformWindow.getClass().getMethod( "getNativeHandle" );
182 getNativeHandle.setAccessible(true);
183 return (long)getNativeHandle.invoke(platformWindow);
184 } catch (Throwable e) {
185 System.err.println("Error getting Window Pointer");
186 return 0;
187 }
188 }
189
190 @Override
191 public void start(Stage primaryStage) {
192 primaryStage.setTitle("Pjsua2 javafx sample");
193 StackPane root = new StackPane();
194 primaryStage.setScene(new Scene(root, 300, 250));
195 primaryStage.show();
196 hwnd = getWindowPointer(primaryStage);
197 myThread.start();
198 }
199 @Override
200 public void stop() throws Exception {
201 myThread.interrupt();
202 myThread.join();
203 }
204 public static void main(String argv[]) {
205 launch(argv);
206 }
207 }