/* | |||||
* To change this license header, choose License Headers in Project Properties. | |||||
* To change this template file, choose Tools | Templates | |||||
* and open the template in the editor. | |||||
*/ | |||||
package model; | |||||
import java.io.File; | |||||
import java.util.logging.Level; | |||||
import java.util.logging.Logger; | |||||
import javax.sound.sampled.AudioFormat; | |||||
import javax.sound.sampled.AudioInputStream; | |||||
import javax.sound.sampled.AudioSystem; | |||||
import javax.sound.sampled.Clip; | |||||
import javax.sound.sampled.DataLine; | |||||
import logger.OhmLogger; | |||||
/** | |||||
* https://docs.oracle.com/javase/tutorial/sound/playing.html | |||||
* | |||||
* @author Jan | |||||
*/ | |||||
public class AudioRinging | |||||
{ | |||||
private AudioInputStream stream; | |||||
private AudioFormat format; | |||||
private DataLine.Info info; | |||||
private Clip clip; | |||||
private File audioFile; | |||||
private static final Logger lgAudioRing = OhmLogger.getLogger(); | |||||
public AudioRinging() | |||||
{ | |||||
//Normalerweise so da Betriebssystemunabhängig aber ka wo der Fehler ist | |||||
//String file = (File.separator+"phone"+File.separator+"SoundDateien"+ File.separator+"RingingPhone.wav"); | |||||
audioFile = new File(getClass().getResource("/SoundDateien/RingingPhone.wav").getFile()); | |||||
try | |||||
{ | |||||
format = AudioSystem.getAudioInputStream(audioFile).getFormat(); | |||||
info = new DataLine.Info(Clip.class, format); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
lgAudioRing.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex); | |||||
} | |||||
} | |||||
public void startRinging() | |||||
{ | |||||
try | |||||
{ | |||||
stream = AudioSystem.getAudioInputStream(audioFile);; | |||||
clip = (Clip) AudioSystem.getLine(info); | |||||
clip.open(stream); | |||||
clip.start(); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
lgAudioRing.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex); | |||||
} | |||||
} | |||||
public void endRinging() | |||||
{ | |||||
clip.stop(); | |||||
clip.close(); | |||||
} | |||||
} |
/* | |||||
* To change this license header, choose License Headers in Project Properties. | |||||
* To change this template file, choose Tools | Templates | |||||
* and open the template in the editor. | |||||
*/ | |||||
package model; | |||||
import java.io.ByteArrayInputStream; | |||||
import java.io.ByteArrayOutputStream; | |||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.util.concurrent.ExecutorService; | |||||
import java.util.concurrent.Executors; | |||||
import java.util.logging.Level; | |||||
import java.util.logging.Logger; | |||||
import javax.sound.sampled.AudioFormat; | |||||
import javax.sound.sampled.AudioInputStream; | |||||
import javax.sound.sampled.AudioSystem; | |||||
import javax.sound.sampled.DataLine; | |||||
import javax.sound.sampled.SourceDataLine; | |||||
import javax.sound.sampled.TargetDataLine; | |||||
/** | |||||
* | |||||
* @author Jan | |||||
*/ | |||||
public class Audioausgabe implements Runnable | |||||
{ | |||||
AudioFormat format; | |||||
AudioInputStream audioInput; | |||||
SourceDataLine sourceData; | |||||
private ExecutorService audioausgabe; | |||||
ByteArrayOutputStream output; | |||||
public Audioausgabe() | |||||
{ | |||||
audioausgabe = Executors.newSingleThreadExecutor(); | |||||
float sampleRate = 48000; | |||||
int sampleSizeInBits = 16; | |||||
int channels = 1; | |||||
boolean signed = true; | |||||
boolean bigEndian = true; | |||||
try | |||||
{ | |||||
format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); | |||||
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
System.out.println("Fehler bei dem Audioformat des Mikrophones"); | |||||
Logger.getLogger(Audioausgabe.class.getName()).log(Level.SEVERE, null, e); | |||||
} | |||||
} | |||||
public void playAudio() | |||||
{ | |||||
try | |||||
{ | |||||
byte audioData[] = output.toByteArray(); | |||||
InputStream byteArrayInputStream = new ByteArrayInputStream( | |||||
audioData); | |||||
audioInput = new AudioInputStream(byteArrayInputStream, format, audioData.length / format.getFrameSize()); | |||||
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format); | |||||
sourceData = (SourceDataLine) AudioSystem.getLine(dataLineInfo); | |||||
sourceData.open(format); | |||||
sourceData.start(); | |||||
int cnt = 0; | |||||
byte tempBuffer[] = new byte[1000000]; | |||||
try | |||||
{ | |||||
while ((cnt = audioInput.read(tempBuffer, 0, tempBuffer.length)) != -1) | |||||
{ | |||||
if (cnt > 0) | |||||
{ | |||||
sourceData.write(tempBuffer, 0, cnt); | |||||
} | |||||
} | |||||
} | |||||
catch (IOException e) | |||||
{ | |||||
e.printStackTrace(); | |||||
} | |||||
sourceData.drain(); | |||||
sourceData.close(); | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
} | |||||
} | |||||
@Override | |||||
public void run() | |||||
{ | |||||
} | |||||
} |
/* | |||||
* To change this license header, choose License Headers in Project Properties. | |||||
* To change this template file, choose Tools | Templates | |||||
* and open the template in the editor. | |||||
*/ | |||||
package model; | |||||
import java.util.ArrayList; | |||||
import java.util.logging.Level; | |||||
import java.util.logging.Logger; | |||||
import javax.sip.address.Address; | |||||
import javax.sip.address.AddressFactory; | |||||
import javax.sip.address.URI; | |||||
import javax.sip.header.AllowHeader; | |||||
import javax.sip.header.CSeqHeader; | |||||
import javax.sip.header.CallIdHeader; | |||||
import javax.sip.header.ContactHeader; | |||||
import javax.sip.header.ExpiresHeader; | |||||
import javax.sip.header.FromHeader; | |||||
import javax.sip.header.HeaderFactory; | |||||
import javax.sip.header.MaxForwardsHeader; | |||||
import javax.sip.header.ToHeader; | |||||
import javax.sip.header.ViaHeader; | |||||
import javax.sip.message.MessageFactory; | |||||
import javax.sip.message.Request; | |||||
import logger.OhmLogger; | |||||
/** | |||||
* | |||||
* @author Jan | |||||
*/ | |||||
public class SIPBye | |||||
{ | |||||
private Request byeRequest; | |||||
private static final Logger lgBye= OhmLogger.getLogger(); | |||||
//Wichtig Unterscheidung Wer das Gespräch begonnen hat -> Unterschiedliche Header!!! | |||||
public SIPBye(String calluser, String proxyAddress, String myName, String myIPAddress, | |||||
String myTag,String proxyTag, Integer myPort, String branch, CallIdHeader callID , | |||||
Long cSequenz, MessageFactory messageFactory, ContactHeader contactHeader, | |||||
AddressFactory addressFactory, HeaderFactory headerFactory ) | |||||
{ | |||||
try | |||||
{ | |||||
Address addressLine = addressFactory.createAddress("sip:" + calluser + "@" + proxyAddress); | |||||
URI requestURI = addressLine.getURI(); | |||||
Address addressfrom = addressFactory.createAddress("sip:" + myName + "@" + myIPAddress); | |||||
FromHeader from = headerFactory.createFromHeader(addressfrom, myTag); | |||||
Address addressTo = addressFactory.createAddress("sip:" + myName + "@" + proxyAddress); | |||||
ToHeader to = headerFactory.createToHeader(addressTo, proxyTag); // Ergänzung TAG!! | |||||
ArrayList via = new ArrayList(); //Via needs a List as input | |||||
ViaHeader viaheader = headerFactory.createViaHeader(myIPAddress, myPort, "UDP", branch); | |||||
via.add(viaheader); | |||||
CSeqHeader cSeq = headerFactory.createCSeqHeader(cSequenz, "BYE"); | |||||
MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70); | |||||
AllowHeader allow = headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE"); | |||||
ExpiresHeader expire = headerFactory.createExpiresHeader(3600); //int value | |||||
byeRequest = messageFactory.createRequest(requestURI, "BYE", callID, cSeq, from, to, via, maxForwards); | |||||
byeRequest.addHeader(contactHeader); | |||||
byeRequest.addHeader(allow); | |||||
byeRequest.addHeader(expire); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
lgBye.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex); | |||||
} | |||||
} | |||||
/** | |||||
* @return the byeRequest | |||||
*/ | |||||
public Request getByeRequest() | |||||
{ | |||||
return byeRequest; | |||||
} | |||||
} |
public ListeningPoint listeningPoint; // SIP listening IP address/port. | public ListeningPoint listeningPoint; // SIP listening IP address/port. | ||||
public Properties properties; // Other properties. | public Properties properties; // Other properties. | ||||
private AudioRinging audioRinging; | |||||
private int myPort = 5060; | private int myPort = 5060; | ||||
private String myName = "129"; | private String myName = "129"; | ||||
public String protocolTyp = "UDP"; | public String protocolTyp = "UDP"; | ||||
line = ""; | line = ""; | ||||
cSequenz = 1; | cSequenz = 1; | ||||
werdeAngerufen = false; | werdeAngerufen = false; | ||||
audioRinging = new AudioRinging(); | |||||
myTag = createString(8); //erzeugt Random Tag mit Länge 8 | myTag = createString(8); //erzeugt Random Tag mit Länge 8 | ||||
branch = createString(18);//erzeugt Random branch mit Länge 18 | branch = createString(18);//erzeugt Random branch mit Länge 18 | ||||
{ | { | ||||
proxyAddress = serverIP + ":" + serverPort; | proxyAddress = serverIP + ":" + serverPort; | ||||
SIPRegister register = new SIPRegister(proxyAddress, myTag, myIPAddress, myPort, branch, addressFactory, headerFactory, messageFactory, contactHeader, sipProvider.getNewCallId()); | SIPRegister register = new SIPRegister(proxyAddress, myTag, myIPAddress, myPort, branch, addressFactory, headerFactory, messageFactory, contactHeader, sipProvider.getNewCallId()); | ||||
this.sipProvider.sendRequest(register.getRequest()); | |||||
sipProvider.sendRequest(register.getRequest()); | |||||
lg.info("Erfolgreiches Senden der Registrierung"); | lg.info("Erfolgreiches Senden der Registrierung"); | ||||
} | } | ||||
{ //ServerPort wird nicht verwendet? | { //ServerPort wird nicht verwendet? | ||||
resetCallParameter(); | resetCallParameter(); | ||||
anrufer = true; | anrufer = true; | ||||
callID = this.sipProvider.getNewCallId(); | |||||
callID = sipProvider.getNewCallId(); | |||||
SIPSessionDescription sdp = new SIPSessionDescription(myName, myIPAddress, null, anrufer); | SIPSessionDescription sdp = new SIPSessionDescription(myName, myIPAddress, null, anrufer); | ||||
SIPInvite sipinvite = new SIPInvite(sipaddresse, proxyaddresse, myIPAddress, myPort, myTag, branch, callID, cSequenz, addressFactory, sdp.getSessionDescription(), headerFactory, messageFactory, contactHeader); | SIPInvite sipinvite = new SIPInvite(sipaddresse, proxyaddresse, myIPAddress, myPort, myTag, branch, callID, cSequenz, addressFactory, sdp.getSessionDescription(), headerFactory, messageFactory, contactHeader); | ||||
this.sipProvider.sendRequest(sipinvite.getInviterequest()); | |||||
sipProvider.sendRequest(sipinvite.getInviterequest()); | |||||
lg.info("Erfolgreiches Senden der Invitation"); | lg.info("Erfolgreiches Senden der Invitation"); | ||||
} | } | ||||
{ | { | ||||
try | try | ||||
{ | { | ||||
if (anrufAngenommen != true) | |||||
if (anrufAngenommen != true) //Falls man nicht abnimmt, klingeln + Ringing schicken | |||||
{ | { | ||||
sipProvider.sendResponse(tempRinging.getResponseRing()); | sipProvider.sendResponse(tempRinging.getResponseRing()); | ||||
audioRinging.startRinging(); | |||||
} | } | ||||
else if(anrufAngenommen==true) | |||||
else if(anrufAngenommen==true) //bei annehmen, klingeln aufhören, OK mit SDP schicken | |||||
{ | { | ||||
audioRinging.endRinging(); | |||||
SIP200Ok respcall = new SIP200Ok(messageFactory, requestEvent, contactHeader, headerFactory); | SIP200Ok respcall = new SIP200Ok(messageFactory, requestEvent, contactHeader, headerFactory); | ||||
SIPSessionDescription sdp = new SIPSessionDescription(myName, myIPAddress, requestEvent, anrufer); | SIPSessionDescription sdp = new SIPSessionDescription(myName, myIPAddress, requestEvent, anrufer); | ||||
respcall.addSDP(sdp.getSessionDescription(), myTag); | respcall.addSDP(sdp.getSessionDescription(), myTag); | ||||
public void byecall(String calluser) throws ParseException, InvalidArgumentException, SipException | public void byecall(String calluser) throws ParseException, InvalidArgumentException, SipException | ||||
{ | { | ||||
Address addressLine = this.addressFactory.createAddress("sip:" + calluser + "@" + proxyAddress); | |||||
URI requestURI = addressLine.getURI(); | |||||
Address addressfrom = this.addressFactory.createAddress("sip:" + myName + "@" + myIPAddress); | |||||
FromHeader from = this.headerFactory.createFromHeader(addressfrom, String.valueOf(this.myTag)); | |||||
Address addressTo = this.addressFactory.createAddress("sip:" + myName + "@" + proxyAddress); | |||||
ToHeader to = this.headerFactory.createToHeader(addressTo, proxyTag); // Ergänzung TAG!! | |||||
ArrayList via = new ArrayList(); //Via needs a List as input | |||||
ViaHeader viaheader = this.headerFactory.createViaHeader(this.myIPAddress, this.myPort, "UDP", String.valueOf(branch)); | |||||
via.add(viaheader); | |||||
cSequenz++; | cSequenz++; | ||||
CSeqHeader cSeq = this.headerFactory.createCSeqHeader(cSequenz, "BYE"); | |||||
MaxForwardsHeader maxForwards = this.headerFactory.createMaxForwardsHeader(70); | |||||
AllowHeader allow = this.headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE"); | |||||
ExpiresHeader expire = this.headerFactory.createExpiresHeader(3600); //int value | |||||
Request request = this.messageFactory.createRequest(requestURI, "BYE", callID, cSeq, from, to, via, maxForwards); | |||||
request.addHeader(contactHeader); | |||||
request.addHeader(allow); | |||||
request.addHeader(expire); | |||||
this.sipProvider.sendRequest(request); | |||||
SIPBye tempBye = new SIPBye(calluser, proxyAddress, myName, myIPAddress, myTag, proxyTag, myPort, branch, callID, cSequenz, messageFactory, contactHeader, addressFactory, headerFactory); | |||||
sipProvider.sendRequest(tempBye.getByeRequest()); | |||||
lg.info("Erfolgreiches Senden des BYE Headers"); | lg.info("Erfolgreiches Senden des BYE Headers"); | ||||
} | } | ||||