JMF RTP send DTMF using au file
You should modify some code according your program.
MediaLocator medialocator ;
String ipAddress ;
int port ;
Integer stateLock = new Integer(0);
boolean failed;
RTPManager rtpManager[] ;
DataSource dataSource = null ;
// Constructor for RTPServer
public void DTMFServer( MediaLocator locator, String ipAddress,
int portNumber )
{
// Assign the values taken in from the constructor to
// those defined in the main class
this.medialocator = locator ;
this.ipAddress = ipAddress ;
//Integer portValue = Integer.valueOf( portNumber ) ;
// If port can be converted from string to integer
// assign it to be the port number
//if ( portValue != null )
//this.port = portValue.intValue() ;
this.port = portNumber;
// Create a processor for this output
instantiate() ;
}
public void instantiate()
{
boolean processorOK = false ;
boolean configureOK = false ;
processorOK = createProcessor() ;
System.out.println( "processorOK = " +processorOK ) ;
if( processorOK )
configureOK = createSend() ;
System.out.println( "configureOK = " +configureOK ) ;
if( configureOK ){
processor.start() ;
System.out.println( "Processor starting..." ) ;
}
}
// Method to create a Processor and do error checking
public boolean createProcessor()
{
//DataSource source ;
// create the datasource
try {
dataSource = javax.media.Manager.createDataSource( medialocator ) ;
System.out.println( "DataSource created from file" ) ;
} catch ( Exception e ) {
System.out.println( "Error: Couldn't create Datasource" ) ;
}
// Create the processor to process the DataSource
try {
processor = javax.media.Manager.createProcessor( dataSource ) ;
System.out.println( "Processor created" ) ;
} catch ( NoProcessorException p ) {
System.out.println( "Error: Couldn't create processor" ) ;
} catch ( IOException i ) {
System.out.println( "Error: Error reading file" ) ;
//System.out.println( i.printStack() ) ;
}
// Run Processor methods to configure Processor
processor.addControllerListener(new StateListener());
waitForState(processor,Processor.Configured);
TrackControl [] track = processor.getTrackControls() ;
boolean encodingOK = false ;
// Check to see if the datasource had tracks
if ( ( track == null ) || ( track.length < 1 ) )
{
System.out.println( "Error: No tracks in Source" ) ;
}
// Set the content type of the output data to RTP data
ContentDescriptor content = new ContentDescriptor( ContentDescriptor.RAW_RTP ) ;
processor.setContentDescriptor( content ) ;
Format supportedFormats[] ;
Format chosen=null;
for ( int i = 0 ; i < track.length ; i++ )
{
// Set format to the format of TrackControl
// i.e. ContentDescriptor
Format format = track.getFormat() ;
if ( track.isEnabled() )
{
// Find formats that support RAW_RTP
supportedFormats = track.getSupportedFormats() ;
if ( supportedFormats.length > 0 )
{
for(int j=0;j<supportedFormats.length;j++){
if(Receiver.sessionDescription.getAudioFormat()!=null)
if(supportedFormats[j].toString().toLowerCase().indexOf(
Receiver.sessionDescription.getAudioFormat().toLowerCase())!=-1)
chosen = supportedFormats[j];
}
// Encode the track with MPEG_RTP format
if( track.setFormat( chosen ) == null )
{
track.setEnabled( false ) ;
encodingOK = false ;
} else {
encodingOK = true ;
}
}
} // if
} // for
waitForState(processor,Processor.Realized);
dataSource = processor.getDataOutput() ;
return true ;
}
// Use the RTPManager to pass the application data to the lower network layer
public boolean createSend()
{
// Make a buffered Push Source from our data source
PushBufferDataSource bufferDatasource = ( PushBufferDataSource ) dataSource ;
// Make a buffered stream for each track in the processor
PushBufferStream bufferStream[] = bufferDatasource.getStreams() ;
// Make an RTPManager for each stream
rtpManager = new RTPManager[ bufferStream.length ] ;
// Create SessionAddresses to locate the client and server; port as well
SessionAddress serverAddress, clientAddress ;
InetAddress ipAddr ;
int portNumber ;
// Create a SendStream that will carry the output
SendStream outputStream ;
// Create the needed SDES list to identify sources
// For every track, make an instance in the RTPManager
for ( int i = 0 ; i >< bufferStream.length ; i++ )
{
try{
rtpManager = RTPManager.newInstance() ;
// make the port, since each instance will have two ports (RTP and RTCP)
// increment the portnumber by 2 for each instance
portNumber = port + 2*i ;
// Get IP of source and destination to associate with send stream
ipAddr = InetAddress.getByName( ipAddress ) ;
serverAddress = new SessionAddress( InetAddress.getLocalHost(), portNumber ) ;
clientAddress = new SessionAddress( ipAddr, portNumber ) ;
// Now Initialze the Manager and then add the target to the RTP Manager
rtpManager.initialize( serverAddress ) ;
rtpManager.addTarget( clientAddress ) ;
System.out.println( "RTP Session #" +i +" created: " +ipAddress +" " +portNumber ) ;
// Create the sendStreams and link them to the targets just added to the RTPManager
outputStream = rtpManager.createSendStream( dataSource, i ) ;
outputStream.start() ;
} catch ( Exception e ) {
System.out.println( "Error: SendStream could not be created" ) ;
System.out.println( e.getMessage() ) ;
}
}
return true ;
}
class StateListener implements ControllerListener
{
public void controllerUpdate( ControllerEvent c )
{
if( c instanceof ControllerClosedEvent )
{if( processor != null ){
processor.stop() ;
processor.close() ;
processor = null ;
for( int i = 0 ; i < rtpManager.length ; i++ )
{
rtpManager.removeTargets( "Sessions are done" ) ;
rtpManager.dispose() ;
}}
}
if( c instanceof EndOfMediaEvent )
{
processor.stop() ;
processor.close() ;
processor = null ;
for( int i = 0 ; i < rtpManager.length ; i++ )
{
rtpManager.removeTargets( "Sessions are done" ) ;
rtpManager.dispose() ;
}
System.out.println( "End of Media Stream" ) ;
}
if (c instanceof ControllerEvent) {
synchronized (getStateLock()) {
getStateLock().notifyAll();
}
}
}
}
public Integer getStateLock() {
return stateLock;
}
public synchronized void waitForState(Processor p, int state) {
p.addControllerListener(new StateListener());
failed = false;
// Call the required method on the processor
if (state == Processor.Configured) {
p.configure();
} else if (state == Processor.Realized) {
p.realize();
}
// Wait until we get an event that confirmes success or failure.
while (p.getState() < state && !failed) {
synchronized(getStateLock()) {
try {
getStateLock().wait();
} catch (InterruptedException ie) {
System.err.println("Error while waiting for state: " + ie);
}
}
}
}

