RTP Server Using JMF
From Coder's Log
Intro
The following code is for a basic RTP server supporting ULAW protocol at 8Khz bitrate. Used to interface to asterisk or any other softphone.
The stream player just plays to the system's sound card probably not the most useful but great for debugging.
RTPServer.java
import java.io.IOException;
import java.net.InetAddress;
import javax.media.Manager;
import javax.media.MediaException;
import javax.media.Player;
import javax.media.Processor;
import javax.media.format.AudioFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;
import javax.media.rtp.RTPManager;
import javax.media.rtp.SendStream;
import javax.media.rtp.SessionAddress;
import javax.media.rtp.SessionManagerException;
public class RTPServer {
public static AudioFormat formatUlawRtp = new AudioFormat(AudioFormat.ULAW_RTP, 8000d, 8, 1, AudioFormat.LITTLE_ENDIAN, AudioFormat.SIGNED);
public static final int ANY_PORT = SessionAddress.ANY_PORT;
private RTPManager rtpManager;
private SendStream sendStream;
private SessionAddress localAddress;
public RTPServer(String localHost) throws IOException, SessionManagerException, MediaException {
this(localHost, ANY_PORT);
}
public RTPServer(String localHost, int localPort) throws IOException, SessionManagerException, MediaException {
rtpManager = RTPManager.newInstance();
InetAddress localIp = InetAddress.getByName(localHost);
localAddress = new SessionAddress(localIp, localPort);
rtpManager.initialize(localAddress);
rtpManager.addReceiveStreamListener(new RTPStreamPlayer());
}
public void addTarget(String remoteHost, int remotePort) throws IOException, SessionManagerException {
InetAddress ipAddress = InetAddress.getByName(remoteHost);
SessionAddress remoteAddress = new SessionAddress(ipAddress, remotePort);
rtpManager.addTarget(remoteAddress);
}
public void initSendStream(DataSource sendStreamSource) throws IOException, MediaException {
Processor proc = Manager.createProcessor(sendStreamSource);
proc.configure();
Tools.waitForState(proc, Processor.Configured);
proc.setContentDescriptor(new ContentDescriptor(ContentDescriptor.RAW_RTP));
proc.start();
proc.getTrackControls()[0].setFormat(formatUlawRtp);
Tools.waitForState(proc, Player.Started);
sendStream = rtpManager.createSendStream(proc.getDataOutput(), 0);
}
public void startSending() throws IOException {
sendStream.start();
}
public void stopSending() throws IOException {
sendStream.stop();
}
public void dispose() {
rtpManager.removeTargets("Disconnected!");
rtpManager.dispose();
}
public SessionAddress getLocalAddress() {
return localAddress;
}
}
RTPStreamPlayer.java
import javax.media.Format;
import javax.media.Manager;
import javax.media.Player;
import javax.media.Processor;
import javax.media.ProcessorModel;
import javax.media.protocol.ContentDescriptor;
import javax.media.rtp.ReceiveStream;
import javax.media.rtp.ReceiveStreamListener;
import javax.media.rtp.event.NewReceiveStreamEvent;
import javax.media.rtp.event.ReceiveStreamEvent;
public class RTPStreamPlayer implements ReceiveStreamListener {
private Processor proc;
private Player player;
public void update(ReceiveStreamEvent event) {
System.out.println(event);
if (event instanceof NewReceiveStreamEvent) {
System.out.println("GOT NEW STREAM");
ReceiveStream stream = event.getReceiveStream();
try {
ContentDescriptor descriptor = new ContentDescriptor(ContentDescriptor.RAW);
ProcessorModel pm = new ProcessorModel(stream.getDataSource(), new Format[] { RTPServer.formatUlawRtp }, descriptor);
proc = Manager.createRealizedProcessor(pm);
proc.configure();
Tools.waitForState(proc, Processor.Configured);
player = Manager.createPlayer(proc.getDataOutput());
player.start();
proc.start();
stream.getDataSource().start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void stop() {
player.stop();
proc.stop();
}
}
