VoIP with JMF,DataSinks and RTP
From Coder's Log
Contents |
Getting raw stream from a processor
Getting access to a raw stream is not that hard, you just need to do some casting to get access to the proper methods. The push streams may need to be changed to pull streams depending on the protocols. This almost works, some distortions come up in the file afterwards, but that may not be due to this code. could be format issues.
Processor processor=Manager.createProcess(source);
processor.configure();
while(processor.getState()<Processor.Configured);
processor.setContentDescriptor(new ContentDescriptor(ContentDescriptor.RAW));
processor.start();
while(processor.getState()<Processor.Started);
PushBufferDataSource ds=(PushBufferDataSource)processor.getDataOutput();
PushBufferStream[] streams=ds.getStreams();
System.out.println("There were "+streams.length+" streams");
Buffer buffer=new Buffer();
FileOutputStream fos=new FileOutputStream("test.raw");
while(!streams[0].endOfStream()){
streams[0].read(buffer);
byte[] data=(byte[])buffer.getData();
System.out.println(buffer.getLength());
fos.write(data,0,buffer.getLength());
}
To use DataSink
Make sure the format is not one of RTP and the descriptor is
new FileTypeDescriptor(FileTypeDescriptor.BASIC_AUDIO);
How to play rtp data directly to player
rtpManager.addReceiveStreamListener(new ReceiveStreamListener() {
public void update(ReceiveStreamEvent event) {
AudioFormat format=new AudioFormat(AudioFormat.ULAW_RTP, 8000d, 8, 1, AudioFormat.LITTLE_ENDIAN, AudioFormat.SIGNED);
ContentDescriptor descriptor=new ContentDescriptor(ContentDescriptor.RAW);
ProcessorModel pm=new ProcessorModel(stream.getDataSource(),new Format[]{format},descriptor);
Processor proc = Manager.createRealizedProcessor(pm);
proc.configure();
while (proc.getState() < Processor.Configured);
proc.start();
stream.getDataSource().start();
Manager.createPlayer(proc.getDataOutput()).start();
}
}
How to store data using DataSink
The format and the descriptor differences are key
rtpManager.addReceiveStreamListener(new ReceiveStreamListener() {
public void update(ReceiveStreamEvent event) {
AudioFormat format=new AudioFormat(AudioFormat.ULAW, 8000d, 8, 1, AudioFormat.LITTLE_ENDIAN, AudioFormat.SIGNED);
ContentDescriptor descriptor=new FileDescriptor(FileDescriptor.BASIC_AUDIO);
ProcessorModel pm=new ProcessorModel(stream.getDataSource(),new Format[]{format},descriptor);
Processor proc = Manager.createRealizedProcessor(pm);
proc.configure();
while (proc.getState() < Processor.Configured);
proc.start();
stream.getDataSource().start();
DataSink sink=Manager.createDataSink(proc.getDataOutput());
sink.open();
sink.start();
Thread.sleep(10000); // This is a bit crude
sink.stop();
sink.close();
}
}
