Thursday, 1 September 2016

simple application for voice transmission and receiving using java rtp ~ gniithelp

Transmitter

import javax.media.*;
import javax.media.control.*;
import javax.media.protocol.*;
import javax.media.format.*;

import java.io.IOException;
import java.io.File;
import java.util.Vector;


public class MediaTransmitter {

private MediaLocator mediaLocator = null;
private DataSink dataSink = null;

private Processor mediaProcessor = null;
private static final Format[] FORMATS = new Format[] {
new AudioFormat(AudioFormat.ULAW_RTP)};

private static final ContentDescriptor CONTENT_DESCRIPTOR =
new ContentDescriptor(ContentDescriptor.RAW_RTP);

public MediaTransmitter(MediaLocator locator) {
mediaLocator = locator;
}

public void startTransmitting() throws IOException {

mediaProcessor.start();
dataSink.open();
dataSink.start();
}

public void stopTransmitting() throws IOException {

dataSink.stop();
dataSink.close();
mediaProcessor.stop();
mediaProcessor.close();
}


public void setDataSource(DataSource ds) throws IOException,
NoProcessorException, CannotRealizeException, NoDataSinkException {


mediaProcessor = Manager.createRealizedProcessor(
new ProcessorModel(ds, FORMATS, CONTENT_DESCRIPTOR));


dataSink = Manager.createDataSink(mediaProcessor.getDataOutput(),
mediaLocator);
}



public static void main(String[] args) {

try {

MediaLocator locator = new MediaLocator("rtp://192.168.1.111:333/audio");
MediaTransmitter transmitter = new MediaTransmitter(locator);
System.out.println("-> Created media locator: '" +
locator + "'");

Vector devices=CaptureDeviceManager.getDeviceList ( null );
CaptureDeviceInfo cdi= (CaptureDeviceInfo) devices.elementAt ( 0 );

DataSource source = Manager.createDataSource(
cdi.getLocator());

transmitter.setDataSource(source);
System.out.println("-> Set the data source on the transmitter");

transmitter.startTransmitting();
System.out.println("-> Transmitting...");
System.out.println(" Press the Enter key to exit");

System.in.read();
System.out.println("-> Exiting");
transmitter.stopTransmitting();

} catch (Throwable t) {
t.printStackTrace();
}

System.exit(0);
}
}


Receiver

import javax.media.*;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;

public class SimpleAudioPlayer {


private Player audioPlayer = null;

public SimpleAudioPlayer(MediaLocator url) throws IOException, NoPlayerException,
CannotRealizeException {
audioPlayer = Manager.createRealizedPlayer(url);
}

public void play() {
audioPlayer.start();
}


public void stop() {
audioPlayer.stop();
audioPlayer.close();
}
public static void main(String[] args) {
try {
MediaLocator loc=new MediaLocator("rtp://192.168.1.111:333/audio");
SimpleAudioPlayer player = new SimpleAudioPlayer(loc);
System.out.println(" Press the Enter key to exit");
player.play();
System.in.read();
System.out.println("-> Exiting");
player.stop();

} catch (Exception ex) {
ex.printStackTrace();
}

System.exit(0);
}
}

No comments:

Post a Comment