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


35 comments:

Francesco said...

Hi,
I have the following problem,
at line:

Vector devices = CaptureDeviceManager.getDeviceList(null);

devices is null!!!
How is it possible?
Please help me.... I'm going crazy!

Arun said...

Hey

if u pass the specified format ,this one ill be return this specified format support devices only, if u want all devices means simply pass null

Francesco said...

yes, I know what you say. But how is it possible? Maybe I have some problem whit codec? Or prefetching of hardware?.... Or some other kind of problem!!!

Arun said...

ok

Give the error message

Arun said...

hey

i think ur jmf registry have some problem so please test ur jmf using below document and source file
http://javaorigin.com/resource/JMF_test.zip

ok

Francesco said...

I try your test but I have this output: "Error".

So... nothing is change!!!

Please I think that you have more experience than me, so don't leave me!! Help me!!

Francesco said...

I'm thinking a crazy think... I have Windows XP SP 2, can this be a problem?

Arun said...

ok

did u got the result for the configuration in my document.
if u wont get the result means your jmf is not detecting your hardware device or hardware devices not registered properly .
try in different pc , reinstall jmf

Arun said...

os is not a problem

Francesco said...

Hi,
Finally I solved my problem...
It was a very stupid problem caused by my distraction. After I launch your test I don't link the correct jar files and properties files.

Thanks a lot.

Now I'd like to have your code posted as: " Voice Chat Using Java" but I have some compile error. Please can you link it to me?

If you like I can give you my email.

Francesco said...

Hi,
I'm working on a VoIP client so I have to develop an application that transmit and receive at same time.
Do you know how I can do it?

Best thanks.

Shyam Krishna said...

Hello ,

Whats that ipaddress...191....in the media locator...wts that ??can you explain..

Shyam Krishna said...

Hello ,

Whats that ipaddress...191....in the media locator...wts that ??can you explain..

Unknown said...

hi shyam
each and every machine networked machines have own ipaddress. if u working in LAN u must config the ipaddress If Internet Open Cmd Window and type ipconfig
c:\> ipconfig
Ethernet adapter Network Bridge (Network Bridge):

Media State . . . . . . . . . . . : Media disconnected

Ethernet adapter Bluetooth Network Connection:

Media State . . . . . . . . . . . : Media disconnected

PPP adapter airtelboardband:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 122.164.164.189
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 122.164.164.189

Anonymous said...

hi,

this code seems to only work with one computer sending voice, and another recieving voice..


is there a way of combining the two so that each computer can send and recieve, maybe in half duplex or full duplex transmition?

Unknown said...

Hi all and sorry for my english, i m french!!
i m trying to use your source for a voip application.
The transmitter run but the player never start, i try to listen the stream with jmStudio but no more result, whith vlc no more, so i have try to use a DataSink and a stateHelper to save the flux on my hard disk and no more result. My ask is : the server and the client can they run in the same computer? i have try other ip and port but no result!!! thank u for help

Groooovy Java said...

Dear Arun,

I am in need of a voice chatting solution. I am able to run your both Transmitter and SimpleMediaPlayer programs. But, couldnt see the out put. What is the expected out put?

again, can u pls suggest how to build a voice chat app using it ?

Regards, Arun (mrarunsp@gmail.com

Anonymous said...

can i use on 1 pc? if yes how

Anonymous said...

not possible because system cannot allow more than one resources accessing to micro phone

ranjeet said...

hi
will you give me javax.media api.
thanks in advance.

Anonymous said...

hi
download java sound api from

http://java.sun.com/products/java-media/sound/

apple32 said...

I need code about project "video chat".You can send for me.
Contact: nguyenvansang32@gmail.com
Thanks

sand said...

Any one have java voice chat application which works over the internet?? i tried smack API. no success yet... any bosy could help me?????????????????

Anonymous said...

in rtp:////192.168.1.111:22224/audio/16
why we need to pass it
if i am using throw lan then what to do

Unknown said...

hi,


I am a student .Where I will get sound API download?????

Unknown said...

i arun i dont know how to run this program
so please give step by step procedure

email id:gows88@gmail.com

Unknown said...

For some reason, it doesnt let me use any JMF APIs.

It says for example:

Access restriction: The type MediaLocator is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\ext\jmf.jar

Anybody has an idea how to resolve this?

Anonymous said...

Temporary Fix for Yusuf's Access Restriction Problem:
http://lkamal.blogspot.com/2008/09/eclipse-access-restriction-on-library.html

Anonymous said...

how to run this application pls tell me th step by step procedure

grĂ¼ner tee said...

RTP has important properties of a transport protocol: it runs on end systems, it provides demultiplexing. It differs from transport protocols like TCP in that it does not offer any form of reliability or a protocol-defined flow/congestion control.

kollagen said...

AVReceive2 uses the RTPManager API, introduced in JMF 2.1.1, to receive RTP transmissions. AVReceive2 performs the following tasks:

Open one RTP session per session address given.
Listen for the NewReceiveStreamEvent from the ReceiveStreamListener.
Create a JMF Player for each stream received for playback.

The IP address should be the address of the computer which transmits the data; or the multicast/broadcast address if multicast/broadcast is being used for the transmission. The ports should be the same as what's being used by the transmitter.

Anonymous said...

Hey... Love your code... Works great :) I learnt a lot, thanks :)

I have one question though... Is there any way to make receiver and transmitter work together? To make it a 2 way voice chat? I tried running both receiver and transmitter codes together... But a very loud static noise comes... Is there any way to solve that?

Anonymous said...

Hey thanks for this code... Works great and I learnt a lot..

Is there a way to integrate both transmitter and receiver to make a proper voice chat? I tried running both programs at once, but theres a very loud static noise... Any way to stop that??

Any help would be appreciated... Thanks..

Atharva Tere said...

http://www.box.net/shared/h9oxzmdtg8zgsvpuxsi8


THAT IS A LINK TO DOWNLOAD MY CODE... CAN SOMEONE HELP ME WITH EXECUTING IT....

Anonymous said...

How to run these two java files...on one pc or different pc?

when i compiled two files on one pc then noise comes...how to run on different pc's

im new to java please please please help me this is anusha...