/* * IncomingAllConference.java * * Copyright (c) 2004-2007 Brekeke Software, Inc. All rights reserved. * * BREKEKE JTAPI SDK Sample Program */ /******************************************************************/ /* This sample program will receive a call at a number "1234" */ /* and add all callers to one conference. First caller will */ /* hear a music on hold. */ /* When new user joins, all members will hear a beep sound. */ /******************************************************************/ import java.util.*; import javax.telephony.*; import javax.telephony.callcontrol.*; import com.brekeke.jtapi.*; public class IncomingAllConference { static CallControlCall maincall; // Call for conference room static CallControlTerminalConnection maintc; static ArrayList confmember; static boolean mohflag = false; // Whether this is a first call to the conference static SoundPlayer mohp = null; // Music-on-hold player static String confroom = "1234";// Conference room number public static void main(String args[]) { Provider myprovider = null; confmember = new ArrayList(3); try { JtapiPeer peer = JtapiPeerFactory.getJtapiPeer("com.brekeke.jtapi.BrPeer"); // MUST HAVE /* Set your work directory of JTAPI in the second argument */ myprovider = peer.getProvider("BrProvider; pdir=C:/JTAPI1.0/work"); /* Add this if you need to know Register is successful or not */ myprovider.addProviderListener(new IncomingAllConference.MyProviderListener()); /* * Start provider and register this Provider's number (1234) to Brekeke * SIP Server */ myprovider.start(); maincall = null; } catch (Exception excp) { System.out.println("Can't get Provider: " + excp.toString()); System.exit(0); } try { /* Choose one address from the addresses this application has */ Terminal terminal = myprovider.getTerminal(confroom); terminal.addCallListener(new MyCallListener()); } catch (Exception excp) { System.out.println("Can't get Terminal: " + excp.toString()); System.exit(0); } } /* Listener for checking Register request result */ static class MyProviderListener implements com.brekeke.jtapi.BrProviderListener { public void eventRegistFail(BrProviderEvent event) { System.out.println("Register failed:" + event.getSourceUrl()+" ("+event.getStatus()+")"); } public void eventRegistSuccess(BrProviderEvent event) { System.out.println("Register successful:" + event.getSourceUrl()+" ("+event.getStatus()+")"); } public void providerInService(ProviderEvent event) { System.out.println("Provider bacame In Service"); } public void providerEventTransmissionEnded(ProviderEvent event) { } /* Not supported */ public void providerOutOfService(ProviderEvent event) { } /* Not supported */ public void providerShutdown(ProviderEvent event) { } } static class MyCallListener implements javax.telephony.TerminalConnectionListener { public void callActive(CallEvent event) { } public void callEventTransmissionEnded(CallEvent event) { } public void callInvalid(CallEvent event) { } public void multiCallMetaMergeEnded(MetaEvent event) { } /* Not supported */ public void multiCallMetaMergeStarted(MetaEvent event) { } /* Not supported */ public void multiCallMetaTransferEnded(MetaEvent event) { } /* Not supported */ public void multiCallMetaTransferStarted(MetaEvent event) { } /* Not supported */ public void singleCallMetaProgressEnded(MetaEvent event) { } /* Not supported */ public void singleCallMetaProgressStarted(MetaEvent event) { } /* Not supported */ public void singleCallMetaSnapshotEnded(MetaEvent event) { } /* Not supported */ public void singleCallMetaSnapshotStarted(MetaEvent event) { } /* Not supported */ public void connectionAlerting(ConnectionEvent event) { } public void connectionConnected(ConnectionEvent event) { Connection con = event.getConnection(); Address addr = con.getAddress(); String addr_name = addr.getName(); Address addr2 = null; System.out.println("Connection Connected:" + addr_name); CallControlCall call = (CallControlCall) con.getCall(); CallControlTerminalConnection tc = (CallControlTerminalConnection) con.getTerminalConnections()[0]; if (addr_name.compareTo(confroom) == 0) { try { for(int i = 0; i< call.getConnections().length; ++i){ if(call.getConnections()[i] != con ){ System.out.println("Add this connection:"+ call.getConnections()[i].getAddress().getName()); confmember.add(call.getConnections()[i]); addr2 = (call.getConnections()[i]).getAddress(); } } if(maincall != null && maincall != call){ call.setConferenceController(tc); if(maintc.getState()== CallControlTerminalConnection.TALKING || maintc.getState() == TerminalConnection.ACTIVE){ maincall.setConferenceController(maintc); } /// Let new caller to join the conference /// maincall.conference(call); int count = countmembers(maincall); System.out.println("number of members ="+count); } // If this caller is the first person in the conference // if(countmembers(maincall) == 1 && mohflag == true && mohp == null){ mohflag = false; Terminal tm = null; if(addr.getTerminals() != null){ if(addr2!= null){ // Get a terminal for playing MOH // tm = addr2.getTerminals()[0]; mohp = new SoundPlayer(tm); } } if(mohp != null){ System.out.println("MOH will start now 1:" + mohp); mohp.start("/sound/hold.ul"); } } // If this caller is not the first person else{ if(mohp != null){ System.out.println("MOH will stop 1: " + mohp); mohp.stop(); mohp = null; mohflag = false; } System.out.println("Sound beep"); sendbeep(maincall); } } catch (Exception e) { System.out.println("Exception when adding a conference member: " + e); e.printStackTrace(); } } } public void connectionCreated(ConnectionEvent event) { Connection con = event.getConnection(); Address addr = con.getAddress(); String addr_name = addr.getName(); System.out.println("Connection Created:" + addr_name); } public void connectionDisconnected(ConnectionEvent event) { Connection con = event.getConnection(); Address addr = con.getAddress(); String addr_name = addr.getName(); Terminal tm; Call call = con.getCall(); System.out.println("Connection Disconnected:" + addr_name); if(addr_name.compareTo(confroom)!=0) { if(confmember.contains(con)==true){ System.out.println("Connection is deleted:" + addr_name); confmember.remove(con); } } /// If only one member is remaining in the call, start MOH again /// if(countmembers(maincall)==1){ tm = getActiveTerminal(maincall); if(mohp == null && tm != null){ mohp = new SoundPlayer(tm); System.out.println("MOH will start now 2:" + mohp); mohp.start("/sound/hold.ul"); } } /// If no one is in the conference room, stop MOH /// else if(countmembers(maincall)==0){ if(mohp != null){ mohp.stop(); mohp = null; } } System.out.println("count conf members:"+countmembers(maincall)); if(maincall != null){ if(maincall.getState()!= Call.ACTIVE ){ maincall = null; maintc = null; } } } public void connectionFailed(ConnectionEvent event) { Connection con = event.getConnection(); Address addr = con.getAddress(); String addr_name = addr.getName(); Terminal tm; System.out.println("Connection Failed:" + addr_name); if(addr_name.compareTo(confroom)!= 0) { System.out.println("Connection is deleted"+addr_name); confmember.remove(con); } /// If only one member is remaining in the call, start MOH again /// if(countmembers(maincall)==1){ tm = getActiveTerminal(maincall); if(mohp == null && tm != null){ mohp = new SoundPlayer(tm); System.out.println("MOH will start now 2:" + mohp); mohp.start("/sound/hold.ul"); } } /// If no one is in the conference room, stop MOH /// else if(countmembers(maincall)==0){ if(mohp != null){ mohp.stop(); mohp = null; } } System.out.println("count conf members:"+countmembers(maincall)); if(maincall != null){ if(maincall.getState()!= Call.ACTIVE ){ maincall = null; maintc = null; } } } public void connectionInProgress(ConnectionEvent event) { } public void connectionUnknown(ConnectionEvent event) { } public void terminalConnectionActive(TerminalConnectionEvent event) { final TerminalConnection _tc = event.getTerminalConnection(); Terminal term = _tc.getTerminal(); System.out.println("Terminal Connection is active!! : Name=" + term.getName() + "Object=" + _tc); } public void terminalConnectionCreated(TerminalConnectionEvent event) { final TerminalConnection _tc = event.getTerminalConnection(); Terminal term = _tc.getTerminal(); System.out.println("Terminal Connection is created!! :" + term.getName()); } public void terminalConnectionDropped(TerminalConnectionEvent event) { TerminalConnection _tc = event.getTerminalConnection(); Terminal term = _tc.getTerminal(); System.out.println("Terminal Connection is dropped:" + term.getName()); } public void terminalConnectionPassive(TerminalConnectionEvent event) { } //When 1234 receive INVITE request public void terminalConnectionRinging(TerminalConnectionEvent event) { try { final CallControlTerminalConnection _tc = (CallControlTerminalConnection) event.getTerminalConnection(); Terminal term = _tc.getTerminal(); CallControlCall call = (CallControlCall) event.getCall(); Connection[] cons; String callerAddr = null; System.out.println("Terminal Connection is Ringing!! :" + term.getName()); //When 1234 receives INVITE request if (term.getName().compareTo(confroom) == 0) { if (call != null) { if(maincall == null){ maincall = call; maintc = _tc; mohflag = true; } else mohflag = false; cons = call.getConnections(); if (cons.length > 0) { for (int i = 0; i < cons.length; ++i) { callerAddr = cons[i].getAddress().getName(); if (callerAddr.compareTo(confroom) != 0) break; } } } Runnable r = new Runnable() { public void run() { try { _tc.answer(); // Answer the call } catch (Exception excp) { System.out.println("Failed to answer:" + excp); excp.printStackTrace(); } }; }; Thread T = new Thread(r); T.start(); } } catch (Exception excp) { System.out.println("Failed in terminalConnectionRinging:"+excp); excp.printStackTrace(); } } public void terminalConnectionUnknown(TerminalConnectionEvent event) { } } /** Count active conference members **/ static int countmembers(Call call){ int num = 0; if(call != null){ Connection[] confcons = call.getConnections(); if(confcons != null && confcons.length > 0){ for(int i=0; i< confcons.length; ++i){ if(confcons[i].getAddress().getName().compareTo(confroom) != 0 && confcons[i].getState() == Connection.CONNECTED){ ++num; } } } } return num; } /** Get active conference member's terminal **/ static Terminal getActiveTerminal(Call call){ Connection[] confcons = call.getConnections(); Terminal t = null; if(confcons.length > 0){ for(int i=0; i< confcons.length; ++i){ if(confcons[i].getAddress().getName().compareTo(confroom) != 0 && confcons[i].getState() == Connection.CONNECTED){ t= confcons[i].getAddress().getTerminals()[0]; } } } return t; } /** send beep sound for all conference members when a new member join **/ static void sendbeep(Call call){ Connection[] confcons = call.getConnections(); Terminal t = null; SoundPlayer beepplayer; if(confcons.length > 0){ for(int i=0; i< confcons.length; ++i){ if(confcons[i].getAddress().getName().compareTo(confroom) != 0 && confcons[i].getState() == Connection.CONNECTED){ t= confcons[i].getAddress().getTerminals()[0]; System.out.println("The terminal to beep = "+t); beepplayer = new SoundPlayer(t); beepplayer.singleplay("/sound/beep.ul"); beepplayer = null; } } } } } /* * SoundPlayer.java * * Copyright (c) 2004-2007 Brekeke Software, Inc. All rights reserved. * * BREKEKE JTAPI SDK Sample Program */ import javax.telephony.*; import javax.telephony.callcontrol.CallControlCall; import javax.telephony.media.BasicMediaService; import javax.telephony.media.ConfigSpec; import javax.telephony.media.MediaProvider; import javax.telephony.media.PlayerEvent; /*** Play a specified sound file (.ul) only one time or repeatedly ***/ public class SoundPlayer extends BasicMediaService implements Runnable { boolean single = false; CallControlCall call = null; volatile Thread thread; MediaProvider prv; String filename; Terminal tm = null; /// Constructors /// SoundPlayer(Call call) { super((MediaProvider)call.getProvider()); this.tm = null; this.call = (CallControlCall) call; } SoundPlayer(Terminal tm){ super((MediaProvider) tm.getProvider()); this.call = null; this.tm = tm; } /// Play repeatedly untill stop()/// public void start(String filename){ single = false; this.filename = filename; thread = new Thread(this); thread.start(); } /// Play just one time /// public void singleplay(String filename){ single = true; this.filename = filename; System.out.println("filename="+filename); thread = new Thread(this); thread.start(); } public void run() { Thread thisThread = Thread.currentThread(); try { if(isBound() == false){ if(call != null){ bindToCall(ConfigSpec.basicConfig, call); } else if(tm != null){ bindToTerminal(ConfigSpec.basicConfig, tm); } } PlayerEvent playev = null; while(thread == thisThread) { if(call != null || tm != null ){ if(filename == null) throw new Exception("Filename is null"); System.out.println("play sound :"+filename); playev = play(filename, 0, null, null); if(single == true) break; if(playev.getQualifier().equals(PlayerEvent.q_Standard)) { continue; } else{ break; } } else{ break; } } } catch (Exception ex) { System.err.println(ex); } } /// Stop Playing sound /// public void stop(){ if(thread!=null){ try{ System.out.println("isBound? "+isBound() + " this:"+this); if(isBound() == false){ if(call != null){ bindToCall(ConfigSpec.basicConfig, call); } else if(tm != null){ bindToTerminal(ConfigSpec.basicConfig, tm); } } stopPlayer(); if (isBound()) try { release(); } catch (Exception ex) {} }catch(Exception ex){ ex.printStackTrace(); } thread = null; } } }