]> sjero.net Git - linphone/blob - coreapi/help/java/org/linphone/core/tutorials/TutorialHelloWorld.java
Add JNI to access call statistics.
[linphone] / coreapi / help / java / org / linphone / core / tutorials / TutorialHelloWorld.java
1 /*
2 TutorialHelloWorld.java
3 Copyright (C) 2010  Belledonne Communications SARL 
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19 package org.linphone.core.tutorials;
20
21 import org.linphone.core.LinphoneAddress;
22 import org.linphone.core.LinphoneCall;
23 import org.linphone.core.LinphoneCallStats;
24 import org.linphone.core.LinphoneChatMessage;
25 import org.linphone.core.LinphoneChatRoom;
26 import org.linphone.core.LinphoneCore;
27 import org.linphone.core.LinphoneCore.EcCalibratorStatus;
28 import org.linphone.core.LinphoneCoreException;
29 import org.linphone.core.LinphoneCoreFactory;
30 import org.linphone.core.LinphoneCoreListener;
31 import org.linphone.core.LinphoneFriend;
32 import org.linphone.core.LinphoneProxyConfig;
33 import org.linphone.core.LinphoneCall.State;
34 import org.linphone.core.LinphoneCore.GlobalState;
35 import org.linphone.core.LinphoneCore.RegistrationState;
36
37
38 /**
39  * This program is a _very_ simple usage example of liblinphone.
40  * It just takes a sip-uri as first argument and attempts to call it.
41  * 
42  * Ported from helloworld.c
43  *
44  * @author Guillaume Beraudo
45  *
46  */
47 public class TutorialHelloWorld implements LinphoneCoreListener {
48         private boolean running;
49         private TutorialNotifier TutorialNotifier;
50
51
52         public TutorialHelloWorld(TutorialNotifier TutorialNotifier) {
53                 this.TutorialNotifier = TutorialNotifier;
54         }
55
56         public TutorialHelloWorld() {
57                 this.TutorialNotifier = new TutorialNotifier();
58         }
59
60         
61         
62         public void show(LinphoneCore lc) {}
63         public void byeReceived(LinphoneCore lc, String from) {}
64         public void authInfoRequested(LinphoneCore lc, String realm, String username) {}
65         public void displayStatus(LinphoneCore lc, String message) {}
66         public void displayMessage(LinphoneCore lc, String message) {}
67         public void displayWarning(LinphoneCore lc, String message) {}
68         public void globalState(LinphoneCore lc, GlobalState state, String message) {}
69         public void registrationState(LinphoneCore lc, LinphoneProxyConfig cfg,RegistrationState cstate, String smessage) {}
70         public void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf,String url) {}
71         public void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf) {}
72         public void textReceived(LinphoneCore lc, LinphoneChatRoom cr,LinphoneAddress from, String message) {}
73         public void callStatsUpdated(LinphoneCore lc, LinphoneCall call, LinphoneCallStats stats) {}
74         public void ecCalibrationStatus(LinphoneCore lc, EcCalibratorStatus status,int delay_ms, Object data) {}
75         public void callEncryptionChanged(LinphoneCore lc, LinphoneCall call,boolean encrypted, String authenticationToken) {}
76         public void notifyReceived(LinphoneCore lc, LinphoneCall call, LinphoneAddress from, byte[] event){}
77         /*
78          * Call state notification listener
79          */
80         public void callState(LinphoneCore lc, LinphoneCall call, State cstate, String msg){
81                 write("State: " + msg);
82
83                 if (State.CallEnd.equals(cstate))
84                         running = false;
85         }
86
87
88         public static void main(String[] args) {
89                 // Check tutorial was called with the right number of arguments
90                 if (args.length != 1) {
91                         throw new IllegalArgumentException("Bad number of arguments");
92                 }
93                 
94                 // Create tutorial object
95                 TutorialHelloWorld helloWorld = new TutorialHelloWorld();
96                 try {
97                         String destinationSipAddress = args[1];
98                         helloWorld.launchTutorial(destinationSipAddress);
99                 } catch (Exception e) {
100                         e.printStackTrace();
101                 }
102         }
103
104         
105         
106         public void launchTutorial(String destinationSipAddress) throws LinphoneCoreException {
107                 
108                 // First instantiate the core Linphone object given only a listener.
109                 // The listener will react to events in Linphone core.
110                 LinphoneCore lc = LinphoneCoreFactory.instance().createLinphoneCore(this);
111
112
113                 
114                 try {
115                         // Send the INVITE message to destination SIP address
116                         LinphoneCall call = lc.invite(destinationSipAddress);
117                         if (call == null) {
118                                 write("Could not place call to " + destinationSipAddress);
119                                 write("Aborting");
120                                 return;
121                         }
122                         write("Call to " + destinationSipAddress + " is in progress...");
123
124
125                         
126                         // main loop for receiving notifications and doing background linphonecore work
127                         running = true;
128                         while (running) {
129                                 lc.iterate();
130                                 try{
131                                         Thread.sleep(50);
132                                 } catch(InterruptedException ie) {
133                                         write("Interrupted!\nAborting");
134                                         return;
135                                 }
136                         }
137
138
139                         
140                         if (!State.CallEnd.equals(call.getState())) {
141                                 write("Terminating the call");
142                                 lc.terminateCall(call);
143                         }
144                 } finally {
145                         write("Shutting down...");
146                         // You need to destroy the LinphoneCore object when no longer used
147                         lc.destroy();
148                         write("Exited");
149                 }
150         }
151
152
153         public void stopMainLoop() {
154                 running=false;
155         }
156
157         
158         private void write(String s) {
159                 TutorialNotifier.notify(s);
160         }
161
162         @Override
163         public void messageReceived(LinphoneCore lc, LinphoneChatRoom cr,
164                         LinphoneChatMessage message) {
165                 // TODO Auto-generated method stub
166                 
167         }
168
169
170 }