# 3
I solved my problem.
Good luck!!
Here is my code:
public class Peer2Peer implements SessionListener, ReceiveStreamListener, ControllerListener {
private MediaLocator locator;
private String ipAddress;
private int localPort;
private int destPort;
private Processor processor;
private RTPManager rtpMgr;
private DataSource dataOutput;
public Peer2Peer(MediaLocator locator, String ipAddress, String destPort, String localPort) {
this.locator = locator;
this.ipAddress = ipAddress;
Integer lp = Integer.valueOf(localPort);
if (lp != null)
this.localPort = lp.intValue();
Integer dp = Integer.valueOf(destPort);
if (dp != null)
this.destPort = dp.intValue();
}//end
public synchronized void start() throws Exception {
createProcessor();
createRTPManager();
createTrasmitter();
createReceiver();
processor.start();
}//end start
public void stop() {
synchronized (this) {
if (processor != null) {
processor.stop();
processor.close();
processor = null;
rtpMgr.removeTargets("Session ended.");
rtpMgr.dispose();
}
}
}//end stop
private void createProcessor() throws Exception {
DataSource ds = javax.media.Manager.createDataSource(locator);
processor = javax.media.Manager.createProcessor(ds);
// Wait for it to configure
boolean result = waitForState(processor, Processor.Configured);
if (result == false)
System.out.println("Couldn't configure processor");
TrackControl[] tracks = processor.getTrackControls();
if (tracks == null || tracks.length < 1) {
System.err.println("Couldn't find tracks in processor");
System.exit(-1);
}
ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);
processor.setContentDescriptor(cd);
boolean encodingOk = false;
for (int i = 0; i < tracks.length; i++) {
if (!encodingOk && tracks[i] instanceof FormatControl) {
if (((FormatControl)tracks[i]).
setFormat( new AudioFormat(AudioFormat.G723_RTP,
8000,
8,
1)) == null) {
tracks[i].setEnabled(false);
}
else {
encodingOk = true;
}
} else {
System.out.println("Track non FormatControl");
tracks[i].setEnabled(false);
}
}
result = waitForState(processor, Controller.Realized);
if (result == false) {
System.err.println("Couldn't realize processor");
System.exit(-1);
}
dataOutput = processor.getDataOutput();
}//end createProcessor
public void createRTPManager() throws Exception{
SessionAddress localAddr, destAddr;
rtpMgr = RTPManager.newInstance();
rtpMgr.addSessionListener(this);
rtpMgr.addReceiveStreamListener(this);
localAddr = new SessionAddress(InetAddress.getLocalHost(), localPort);
destAddr = new SessionAddress(InetAddress.getByName(ipAddress), destPort);
rtpMgr.initialize(localAddr);
rtpMgr.addTarget(destAddr);
System.err.println("Created RTP session: "+ipAddress+" "+localPort);
}//end createRTPManager
private void createTrasmitter() throws Exception {
SendStream sendStream = rtpMgr.createSendStream(dataOutput, 0);
sendStream.start();
}//end instanziate
public void createReceiver() {
BufferControl bc = (BufferControl) rtpMgr
.getControl("javax.media.control.BufferControl");
if (bc != null)
bc.setBufferLength(350);
}//end createReceiver
/**
* SessionListener.
*/
public synchronized void update(SessionEvent evt) {
if (evt instanceof NewParticipantEvent) {
Participant p = ((NewParticipantEvent) evt).getParticipant();
System.err.println(" - A new participant had just joined: "
+ p.getCNAME());
}
}
/**
* ReceiveStreamListener
*/
public synchronized void update(ReceiveStreamEvent evt) {
Participant participant = evt.getParticipant();
ReceiveStream stream = evt.getReceiveStream();
if (evt instanceof RemotePayloadChangeEvent) {
System.err.println(" - Received an RTP PayloadChangeEvent.");
System.err.println("Sorry, cannot handle payload change.");
System.exit(0);
}
else if (evt instanceof NewReceiveStreamEvent) {
try {
stream = ((NewReceiveStreamEvent) evt).getReceiveStream();
DataSource ds = stream.getDataSource();
// Find out the formats.
RTPControl ctl = (RTPControl) ds
.getControl("javax.media.rtp.RTPControl");
if (ctl != null) {
System.err.println(" - Recevied new RTP stream: "
+ ctl.getFormat());
} else
System.err.println(" - Recevied new RTP stream");
if (participant == null)
System.err
.println("The sender of this stream had yet to be identified.");
else {
System.err.println("The stream comes from: "
+ participant.getCNAME());
}
// create a player by passing datasource to the Media Manager
Player p = javax.media.Manager.createPlayer(ds);
if (p == null)
return;
p.addControllerListener(this);
p.realize();
} catch (Exception e) {
System.err.println("NewReceiveStreamEvent exception "
+ e.getMessage());
return;
}
}
else if (evt instanceof StreamMappedEvent) {
if (stream != null && stream.getDataSource() != null) {
DataSource ds = stream.getDataSource();
RTPControl ctl = (RTPControl) ds
.getControl("javax.media.rtp.RTPControl");
System.err.println(" - The previously unidentified stream ");
if (ctl != null)
System.err.println("" + ctl.getFormat());
System.err.println("had now been identified as sent by: "
+ participant.getCNAME());
}
}
else if (evt instanceof ByeEvent) {
System.err.println(" - Got \"bye\" from: "+ participant.getCNAME());
}
}
/**
* ControllerListener for the Players.
*/
public synchronized void controllerUpdate(ControllerEvent ce) {
Player p = (Player) ce.getSourceController();
if (p == null)
return;
// Get this when the internal players are realized.
if (ce instanceof RealizeCompleteEvent) {
p.start();
}
if (ce instanceof ControllerErrorEvent) {
p.removeControllerListener(this);
System.err.println("AVReceive2 internal error: " + ce);
}
}
//close the RTP session.
protected void close() {
if (rtpMgr != null) {
rtpMgr.removeTargets("Closing session from AVReceive2");
rtpMgr.dispose();
rtpMgr = null;
}
}//end close
/***************************************************************************
* Convenience methods to handle processor's state changes.
**************************************************************************/
private Integer stateLock = new Integer(0);
private boolean failed = false;
Integer getStateLock() {
return stateLock;
}
void setFailed() {
failed = true;
}
private synchronized boolean waitForState(Processor p, int state) {
p.addControllerListener(new StateListener());
failed = false;
if (state == Processor.Configured) {
p.configure();
} else if (state == Processor.Realized) {
p.realize();
}
while (p.getState() < state && !failed) {
synchronized (getStateLock()) {
try {
getStateLock().wait();
} catch (InterruptedException ie) {
return false;
}
}
}
if (failed)
return false;
else
return true;
}
/***************************************************************************
* Inner Classes
**************************************************************************/
class StateListener implements ControllerListener {
public void controllerUpdate(ControllerEvent ce) {
if (ce instanceof ControllerClosedEvent)
setFailed();
if (ce instanceof ControllerEvent) {
synchronized (getStateLock()) {
getStateLock().notifyAll();
}
}
}
}
}//end Peer2Peer
rtpa at 2007-7-12 19:22:19 >
