]> sjero.net Git - linphone/blob - coreapi/help/java/org/linphone/core/tutorials/TutorialHelloWorld.java
30510fd84f6fafc61203d1877d79025fc216acdb
[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.LinphoneChatRoom;
24 import org.linphone.core.LinphoneCore;
25 import org.linphone.core.LinphoneCore.EcCalibratorStatus;
26 import org.linphone.core.LinphoneCoreException;
27 import org.linphone.core.LinphoneCoreFactory;
28 import org.linphone.core.LinphoneCoreListener;
29 import org.linphone.core.LinphoneFriend;
30 import org.linphone.core.LinphoneProxyConfig;
31 import org.linphone.core.LinphoneCall.State;
32 import org.linphone.core.LinphoneCore.GlobalState;
33 import org.linphone.core.LinphoneCore.RegistrationState;
34
35
36 /**
37  * This program is a _very_ simple usage example of liblinphone.
38  * It just takes a sip-uri as first argument and attempts to call it.
39  * 
40  * Ported from helloworld.c
41  *
42  * @author Guillaume Beraudo
43  *
44  */
45 public class TutorialHelloWorld implements LinphoneCoreListener {
46         private boolean running;
47         private TutorialNotifier TutorialNotifier;
48
49
50         public TutorialHelloWorld(TutorialNotifier TutorialNotifier) {
51                 this.TutorialNotifier = TutorialNotifier;
52         }
53
54         public TutorialHelloWorld() {
55                 this.TutorialNotifier = new TutorialNotifier();
56         }
57
58         
59         
60         public void show(LinphoneCore lc) {}
61         public void byeReceived(LinphoneCore lc, String from) {}
62         public void authInfoRequested(LinphoneCore lc, String realm, String username) {}
63         public void displayStatus(LinphoneCore lc, String message) {}
64         public void displayMessage(LinphoneCore lc, String message) {}
65         public void displayWarning(LinphoneCore lc, String message) {}
66         public void globalState(LinphoneCore lc, GlobalState state, String message) {}
67         public void registrationState(LinphoneCore lc, LinphoneProxyConfig cfg,RegistrationState cstate, String smessage) {}
68         public void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf,String url) {}
69         public void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf) {}
70         public void textReceived(LinphoneCore lc, LinphoneChatRoom cr,LinphoneAddress from, String message) {}
71         public void ecCalibrationStatus(LinphoneCore lc, EcCalibratorStatus status,int delay_ms, Object data) {}
72         public void callEncryptionChanged(LinphoneCore lc, LinphoneCall call,boolean encrypted, String authenticationToken) {}
73
74         /*
75          * Call state notification listener
76          */
77         public void callState(LinphoneCore lc, LinphoneCall call, State cstate, String msg){
78                 write("State: " + msg);
79
80                 if (State.CallEnd.equals(cstate))
81                         running = false;
82         }
83
84
85         public static void main(String[] args) {
86                 // Check tutorial was called with the right number of arguments
87                 if (args.length != 1) {
88                         throw new IllegalArgumentException("Bad number of arguments");
89                 }
90                 
91                 // Create tutorial object
92                 TutorialHelloWorld helloWorld = new TutorialHelloWorld();
93                 try {
94                         String destinationSipAddress = args[1];
95                         helloWorld.launchTutorial(destinationSipAddress);
96                 } catch (Exception e) {
97                         e.printStackTrace();
98                 }
99         }
100
101         
102         
103         public void launchTutorial(String destinationSipAddress) throws LinphoneCoreException {
104                 
105                 // First instantiate the core Linphone object given only a listener.
106                 // The listener will react to events in Linphone core.
107                 LinphoneCore lc = LinphoneCoreFactory.instance().createLinphoneCore(this);
108
109
110                 
111                 try {
112                         // Send the INVITE message to destination SIP address
113                         LinphoneCall call = lc.invite(destinationSipAddress);
114                         if (call == null) {
115                                 write("Could not place call to " + destinationSipAddress);
116                                 write("Aborting");
117                                 return;
118                         }
119                         write("Call to " + destinationSipAddress + " is in progress...");
120
121
122                         
123                         // main loop for receiving notifications and doing background linphonecore work
124                         running = true;
125                         while (running) {
126                                 lc.iterate();
127                                 try{
128                                         Thread.sleep(50);
129                                 } catch(InterruptedException ie) {
130                                         write("Interrupted!\nAborting");
131                                         return;
132                                 }
133                         }
134
135
136                         
137                         if (!State.CallEnd.equals(call.getState())) {
138                                 write("Terminating the call");
139                                 lc.terminateCall(call);
140                         }
141                 } finally {
142                         write("Shutting down...");
143                         // You need to destroy the LinphoneCore object when no longer used
144                         lc.destroy();
145                         write("Exited");
146                 }
147         }
148
149
150         public void stopMainLoop() {
151                 running=false;
152         }
153
154         
155         private void write(String s) {
156                 TutorialNotifier.notify(s);
157         }
158
159
160 }