27 April 2007

simple application for voice transmission and receiving using java rtp

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);
}
}


15 April 2007

Java TV API Overview

The Java TV API is being designed to provide access to functionality unique to digital television receivers, including:

  • Audio/video streaming
  • Conditional access
  • Access to in-band and out-of-band data channels
  • Access to service information
  • Tuner control for channel changing
  • On-screen graphics control
More :http://java.sun.com/products/javatv/

12 April 2007

Sound over IP with jmf RTP

This code will allow you to send and recive sound over IP network using RTP protocol.


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

import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.DataSink;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.NoProcessorException;
import javax.media.NotRealizedError;
import javax.media.Player;
import javax.media.Processor;
import javax.media.control.FormatControl;
import javax.media.control.TrackControl;
import javax.media.format.AudioFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;

public class SimpleVoiceTransmiter {

/**
* @param args
*/
public static void main(String[] args) {
// First find a capture device that will capture linear audio
// data at 8bit 8Khz
AudioFormat format= new AudioFormat(AudioFormat.LINEAR,
8000,
8,
1);

Vector devices= CaptureDeviceManager.getDeviceList( format);

CaptureDeviceInfo di= null;

if (devices.size() > 0) {
di = (CaptureDeviceInfo) devices.elementAt( 0);
}
else {
// exit if we could not find the relevant capturedevice.
System.exit(-1);
}

// Create a processor for this capturedevice & exit if we
// cannot create it
Processor processor = null;
try {
processor = Manager.createProcessor(di.getLocator());
} catch (IOException e) {
System.exit(-1);
} catch (NoProcessorException e) {
System.exit(-1);
}

// configure the processor
processor.configure();

while (processor.getState() != Processor.Configured){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

processor.setContentDescriptor(
new ContentDescriptor( ContentDescriptor.RAW));

TrackControl track[] = processor.getTrackControls();

boolean encodingOk = false;

// Go through the tracks and try to program one of them to
// output gsm data.

for (int i = 0; i < track.length; i++) {
if (!encodingOk && track[i] instanceof FormatControl) {
if (((FormatControl)track[i]).
setFormat( new AudioFormat(AudioFormat.GSM_RTP,
8000,
8,
1)) == null) {

track[i].setEnabled(false);
}
else {
encodingOk = true;
}
} else {
// we could not set this track to gsm, so disable it
track[i].setEnabled(false);
}
}

// At this point, we have determined where we can send out
// gsm data or not.
// realize the processor
if (encodingOk) {
processor.realize();
while (processor.getState() != Processor.Realized){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// get the output datasource of the processor and exit
// if we fail
DataSource ds = null;

try {
ds = processor.getDataOutput();
} catch (NotRealizedError e) {
System.exit(-1);
}

// hand this datasource to manager for creating an RTP
// datasink our RTP datasink will multicast the audio
try {
String url= "rtp://224.0.0.1:22224/audio/16";

MediaLocator m = new MediaLocator(url);

DataSink d = Manager.createDataSink(ds, m);
d.open();
d.start();
processor.start();
} catch (Exception e) {
System.exit(-1);
}
}



}

}




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

import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;

public class SimpleVoiceReciver{

/**
* @param args
*/
public static void main(String[] args) {
String url= "rtp://192.168.1.111:22224/audio/16";

MediaLocator mrl= new MediaLocator(url);

if (mrl == null) {
System.err.println("Can't build MRL for RTP");
System.exit(-1);
}

// Create a player for this rtp session
Player player = null;
try {
player = Manager.createPlayer(mrl);
} catch (NoPlayerException e) {
System.err.println("Error:" + e);
System.exit(-1);
} catch (MalformedURLException e) {
System.err.println("Error:" + e);
System.exit(-1);
} catch (IOException e) {
System.err.println("Error:" + e);
System.exit(-1);
}

if (player != null) {
System.out.println("Player created.");
player.realize();
// wait for realizing
while (player.getState() != Player.Realized){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
player.start();
} else {
System.err.println("Player doesn't created.");
System.exit(-1);
}
}

}

Record and Save Audio Using Java

Step 1:
First Read The Audio From MicroPhone

int cnt = targetDataLine.read( tempBuffer,0, tempBuffer.length);


Step 2:
Save data in output stream

byteArrayOutputStream.write(tempBuffer, 0, cnt);

Step 3:
Get the saved data into a byte array object.

byte audioData[]=byteArrayOutputStream.toByteArray();

Step 4:
Get an input stream on the byte array containing the data

InputStream byteArrayInputStream = new ByteArrayInputStream(audioData);
AudioFormat audioFormat = getAudioFormat();
audioInputStream =new AudioInputStream(byteArrayInputStream,audioFormat,
audioData.length/audioFormat.getFrameSize());



Step 5: Save To File

if (AudioSystem.isFileTypeSupported(AudioFileFormat.Type.AU,
audioInputStream)) {
AudioSystem.write(audioInputStream, AudioFileFormat.Type.AU, file);
}

Note :If U want Full Source Code for This One then Download the Java File
http://javasoft.phpnet.us/src/AudioRecorder.java

RTP Using Java

Download Sample Application For RTP using Java

http://www.live-share.com/files/202962/rtpvoice.rar.html


http://www.live-share.com/files/202964/sam.zip.html

09 April 2007

Java Sound Capture And Save AU File Format and Convertion

http://java.sun.com/docs/books/tutorial/sound/converters.html

05 April 2007

SIP: Creating next-generation telecom applications

SIP: Creating next-generation telecom applications

The Session Initiation Protocol makes developing apps for telecommunications networks easier than ever
Download SipServlet api from
http://www-128.ibm.com/developerworks/library/wi-sip.html


SIP programming for the Java developer
Deliver SIP-based services to Java applications with SIP Servlet
Download sample application src

http://www.javaworld.com/javaworld/jw-06-2006/jw-0619-sip.html?page=1

SSL VS Tomcat Step by Step Configuration

1 . Certificate Generation

Create a certificate keystore by executing the following command


# keytool -genkey -alias tomcat -keyalg RSA


and specify a password value of "changeit".


2 . Tomcat Configuration

open the server.xml file from tomcat_home /conf directory and uncommit or add the following configurations

<-- Define an SSL HTTP/1.1 Connector on port 8443 -->

<connector classname="org.apache.catalina.connector.http.HttpConnector" port="8443" minprocessors="5" maxprocessors="75" enablelookups="true" acceptcount="10" debug="0" scheme="https" secure="true" clientauth="false" protocol="TLS">


next start the server and try https://localhost:8443/ url . you should see the usual Tomcat splash page

mysql Couldn't find the mysql server or manager

Solution
Step 1.)

Rem out the line in /etc/my.conf that was setting basedir to /var/lib. As seen below:

user=mysql
#basedir=/var/lib

Step 2.)

Create the directory "/var/run/mysqld" if it does not exist. Then chown that directory to mysql.mysql as below...

mkdir /var/run/mysqld
chown mysql.mysql /var/run/mysqld

Step 3.)

Start the service..

service mysql start

Download files using Servlet


String fileName = "db.zip";
res.setContentType("application/octet-stream");
res.setHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
OutputStream oStream = res.getOutputStream();
FileInputStream file=new FileInputStream("c:\\db.zip");

String sOutput ="Arunkumar";/*what ever be the information you want to download, should be specified here.*/
int i=0;
while((i=file.read())!=-1){
oStream.write(i);}
file.close();
oStream.close();

Voice Chat Using Java

Two way Communication:

Step 1: first execute sender class
Step 2: Here after execute Tx class

Download Source : revoicechatusingjava.zip

Warning: Dont execute using sampe pc use different pc for each files otherwise it will be rise error ( mic already busy )

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

public class sender {
ServerSocket MyService;
Socket clientSocket = null;
BufferedInputStream input;
TargetDataLine targetDataLine;

BufferedOutputStream out;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;

SourceDataLine sourceDataLine;
byte tempBuffer[] = new byte[10000];

sender() throws LineUnavailableException{
try {
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class,audioFormat);
sourceDataLine = (SourceDataLine)
AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
MyService = new ServerSocket(500);
clientSocket = MyService.accept();
captureAudio();
input = new BufferedInputStream(clientSocket.getInputStream());
out=new BufferedOutputStream(clientSocket.getOutputStream());

while(input.read(tempBuffer)!=-1){
sourceDataLine.write(tempBuffer,0,10000);
}
} catch (IOException e) {

e.printStackTrace();
}

}
private AudioFormat getAudioFormat(){
float sampleRate = 8000.0F;
int sampleSizeInBits = 16;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(
sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
}
public static void main(String s[]) throws LineUnavailableException{
sender s2=new sender();
}


private void captureAudio() {
try {

Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
System.out.println("Available mixers:");
for (int cnt = 0; cnt < audioformat =" getAudioFormat();" datalineinfo =" new" mixer =" AudioSystem.getMixer(mixerInfo[3]);" targetdataline =" (TargetDataLine)" capturethread =" new" cnt =" targetDataLine.read(tempBuffer," stopcapture =" false;" out =" null;" in =" null;" sock =" null;" tx =" new" sock =" new" out =" new" in =" new" mixerinfo =" AudioSystem.getMixerInfo();" cnt =" 0;" audioformat =" getAudioFormat();" datalineinfo =" new" mixer =" AudioSystem.getMixer(mixerInfo[3]);" targetdataline =" (TargetDataLine)" capturethread =" new" datalineinfo1 =" new" sourcedataline =" (SourceDataLine)" playthread =" new" bytearrayoutputstream =" new" stopcapture =" false;" cnt =" targetDataLine.read(tempBuffer,"> 0) {

byteArrayOutputStream.write(tempBuffer, 0, cnt);

}
}
byteArrayOutputStream.close();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}

private AudioFormat getAudioFormat() {
float sampleRate = 8000.0F;

int sampleSizeInBits = 16;

int channels = 1;

boolean signed = true;

boolean bigEndian = false;

return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
bigEndian);
}

class PlayThread extends Thread {
byte tempBuffer[] = new byte[10000];

public void run() {
try {
while (in.read(tempBuffer) != -1) {
sourceDataLine.write(tempBuffer, 0, 10000);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

----------------------------------------------

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;

import javax.sound.sampled.TargetDataLine;

public class Tx {
boolean stopCapture = false;

ByteArrayOutputStream byteArrayOutputStream;

AudioFormat audioFormat;

TargetDataLine targetDataLine;

AudioInputStream audioInputStream;

BufferedOutputStream out = null;

BufferedInputStream in = null;

Socket sock = null;

SourceDataLine sourceDataLine;

public static void main(String[] args) {
Tx tx = new Tx();
tx.captureAudio();

}

private void captureAudio() {
try {

sock = new Socket("192.168.1.51", 500);

out = new BufferedOutputStream(sock.getOutputStream());
in = new BufferedInputStream(sock.getInputStream());

Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
System.out.println("Available mixers:");
for (int cnt = 0; cnt < audioformat =" getAudioFormat();" datalineinfo =" new" mixer =" AudioSystem.getMixer(mixerInfo[3]);" targetdataline =" (TargetDataLine)" capturethread =" new" datalineinfo1 =" new" sourcedataline =" (SourceDataLine)" playthread =" new" bytearrayoutputstream =" new" stopcapture =" false;" cnt =" targetDataLine.read(tempBuffer,"> 0) {

byteArrayOutputStream.write(tempBuffer, 0, cnt);

}
}
byteArrayOutputStream.close();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}

private AudioFormat getAudioFormat() {
float sampleRate = 8000.0F;

int sampleSizeInBits = 16;

int channels = 1;

boolean signed = true;

boolean bigEndian = false;

return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
bigEndian);
}

class PlayThread extends Thread {
byte tempBuffer[] = new byte[10000];

public void run() {
try {
while (in.read(tempBuffer) != -1) {
sourceDataLine.write(tempBuffer, 0, 10000);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

}