sample2.java 8.1 KB

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