diff options
author | Lyubomir Marinov <lyubomir.marinov@jitsi.org> | 2010-06-15 23:14:02 +0000 |
---|---|---|
committer | Lyubomir Marinov <lyubomir.marinov@jitsi.org> | 2010-06-15 23:14:02 +0000 |
commit | 5f3646989c06873c0abfa31bad13b0f3c593e163 (patch) | |
tree | a4947cfe3cc17b0cdc870aaee8446b0b6deecab8 /src/net/java/sip/communicator | |
parent | 71ceae7c10e35c91b6132f17f153199962f74aea (diff) | |
download | jitsi-5f3646989c06873c0abfa31bad13b0f3c593e163.zip jitsi-5f3646989c06873c0abfa31bad13b0f3c593e163.tar.gz jitsi-5f3646989c06873c0abfa31bad13b0f3c593e163.tar.bz2 |
Tries to port the functionality of GatherEntropy which was disabled in r7261 to the wideband PortAudio interface.
Diffstat (limited to 'src/net/java/sip/communicator')
4 files changed, 180 insertions, 84 deletions
diff --git a/src/net/java/sip/communicator/impl/neomedia/GatherEntropy.java b/src/net/java/sip/communicator/impl/neomedia/GatherEntropy.java index ea25627..8ffa22b 100644 --- a/src/net/java/sip/communicator/impl/neomedia/GatherEntropy.java +++ b/src/net/java/sip/communicator/impl/neomedia/GatherEntropy.java @@ -1,6 +1,9 @@ package net.java.sip.communicator.impl.neomedia; +import java.io.*; + import javax.media.*; +import javax.media.format.*; import gnu.java.zrtp.utils.*; @@ -33,7 +36,7 @@ public class GatherEntropy /** * Device config to look for catpture devices. */ - private DeviceConfiguration deviceConfiguration; + private final DeviceConfiguration deviceConfiguration; /** * Other methods shall/may check this to see if Fortuna was seeded with @@ -93,86 +96,115 @@ public class GatherEntropy else if (deviceConfiguration.getAudioSystem().equals( DeviceConfiguration.AUDIO_SYSTEM_PORTAUDIO)) { -// GatherPortAudio gatherer = new GatherPortAudio(); -// retValue = gatherer.preparePortAudioEntropy(); -// if (retValue) -// gatherer.start(); + GatherPortAudio gatherer = new GatherPortAudio(); + retValue = gatherer.preparePortAudioEntropy(); + if (retValue) + gatherer.start(); } return retValue; } -// private class GatherPortAudio extends Thread -// { -// private InputPortAudioStream portAudioStream = null; -// -// /** -// * Prepares to read entropy data from portaudio capture device. -// * -// * The method gets an PortAudio instance with a set of capture -// * parameters. -// * -// * @return True if the PortAudio input stream is available. -// */ -// private boolean preparePortAudioEntropy() -// { -// int deviceIndex -// = DataSource.getDeviceIndex( -// deviceConfiguration -// .getAudioCaptureDevice().getLocator()); -// -// try { -// portAudioStream = PortAudioManager.getInstance() -// .getInputStream(deviceIndex, 8000.0, 1); -// } catch (PortAudioException e) { -// return false; -// } -// return true; -// } -// -// /** -// * Gather entropy from portaudio capture device and seed Fortuna PRNG. -// * -// * The method gathers a number of samples and seeds the Fortuna PRNG. -// */ -// public void run() { -// -// ZrtpFortuna fortuna = ZrtpFortuna.getInstance(); -// -// Buffer firstBuf = new Buffer(); -// -// if (portAudioStream == null) { -// return; -// } -// -// try { -// portAudioStream.start(); -// -// for (int i = 0; i < NUM_OF_FRAMES; i++) -// { -// portAudioStream.read(firstBuf); -// byte[] entropy = (byte[])firstBuf.getData(); -// gatheredEntropy += entropy.length; -// // distribute first buffers evenly over the pools, put -// // others on the first pools. This method is adapted to -// // SC requirements to get random data -// if (i < 32) -// { -// fortuna.addSeedMaterial(entropy); -// } -// else -// { -// fortuna.addSeedMaterial((i%3), entropy, 0, entropy.length); -// } -// } -// entropyOk = true; -// portAudioStream.stop(); -// } catch (PortAudioException e) { -// // ignore exception -// } -// // this forces a Fortuna to use the new seed (entropy) data. -// byte[] random = new byte[300]; -// fortuna.nextBytes(random); -// return; -// } -// } + private class GatherPortAudio extends Thread + { + private DataSource dataSource = null; + + private PortAudioStream portAudioStream = null; + + /** + * Prepares to read entropy data from portaudio capture device. + * + * The method gets an PortAudio instance with a set of capture + * parameters. + * + * @return True if the PortAudio input stream is available. + */ + private boolean preparePortAudioEntropy() + { + MediaLocator audioCaptureDeviceLocator + = deviceConfiguration.getAudioCaptureDevice().getLocator(); + + if (audioCaptureDeviceLocator == null) + return false; + + dataSource + = new DataSource( + audioCaptureDeviceLocator, + new Format[] + { + new AudioFormat( + AudioFormat.LINEAR, + 8000, + 16 /* sampleSizeInBits */, + 1 /* channels */, + AudioFormat.LITTLE_ENDIAN, + AudioFormat.SIGNED, + Format.NOT_SPECIFIED /* frameSizeInBits */, + Format.NOT_SPECIFIED /* frameRate */, + Format.byteArray) + }, + false); + + try + { + dataSource.connect(); + } + catch (IOException ioex) + { + return false; + } + portAudioStream = (PortAudioStream) (dataSource.getStreams()[0]); + return (portAudioStream != null); + } + + /** + * Gather entropy from portaudio capture device and seed Fortuna PRNG. + * + * The method gathers a number of samples and seeds the Fortuna PRNG. + */ + public void run() { + + ZrtpFortuna fortuna = ZrtpFortuna.getInstance(); + + Buffer firstBuf = new Buffer(); + + if ((dataSource == null) || (portAudioStream == null)) + return; + + try { + dataSource.start(); + + for (int i = 0; i < NUM_OF_FRAMES; i++) + { + portAudioStream.read(firstBuf); + byte[] entropy = (byte[])firstBuf.getData(); + gatheredEntropy += entropy.length; + // distribute first buffers evenly over the pools, put + // others on the first pools. This method is adapted to + // SC requirements to get random data + if (i < 32) + { + fortuna.addSeedMaterial(entropy); + } + else + { + fortuna.addSeedMaterial((i%3), entropy, 0, entropy.length); + } + } + entropyOk = true; + } + catch (IOException ioex) + { + // ignore exception + } + finally + { + portAudioStream = null; + dataSource.disconnect(); + } + // this forces a Fortuna to use the new seed (entropy) data. + byte[] random = new byte[300]; + fortuna.nextBytes(random); + return; + } + } } diff --git a/src/net/java/sip/communicator/impl/neomedia/NeomediaActivator.java b/src/net/java/sip/communicator/impl/neomedia/NeomediaActivator.java index db40e4f..57f14cd 100644 --- a/src/net/java/sip/communicator/impl/neomedia/NeomediaActivator.java +++ b/src/net/java/sip/communicator/impl/neomedia/NeomediaActivator.java @@ -136,7 +136,9 @@ public class NeomediaActivator 1100), null); - GatherEntropy entropy = new GatherEntropy(mediaServiceImpl.getDeviceConfiguration()); + GatherEntropy entropy + = new GatherEntropy(mediaServiceImpl.getDeviceConfiguration()); + entropy.setEntropy(); //we use the nist-sdp stack to make parse sdp and we need to set the diff --git a/src/net/java/sip/communicator/impl/neomedia/jmfext/media/protocol/portaudio/DataSource.java b/src/net/java/sip/communicator/impl/neomedia/jmfext/media/protocol/portaudio/DataSource.java index 74572c9..f22a25f 100644 --- a/src/net/java/sip/communicator/impl/neomedia/jmfext/media/protocol/portaudio/DataSource.java +++ b/src/net/java/sip/communicator/impl/neomedia/jmfext/media/protocol/portaudio/DataSource.java @@ -24,6 +24,34 @@ import net.java.sip.communicator.impl.neomedia.portaudio.*; public class DataSource extends AbstractPullBufferCaptureDevice { + private final boolean audioQualityImprovement; + + private final Format[] supportedFormats; + + public DataSource() + { + this.supportedFormats = null; + this.audioQualityImprovement = true; + } + + public DataSource(MediaLocator locator) + { + this(locator, null, true); + } + + public DataSource( + MediaLocator locator, + Format[] supportedFormats, + boolean audioQualityImprovement) + { + super(locator); + + this.supportedFormats + = (supportedFormats == null) + ? null + : supportedFormats.clone(); + this.audioQualityImprovement = audioQualityImprovement; + } /** * Creates a new <tt>PullBufferStream</tt> which is to be at a specific @@ -46,7 +74,7 @@ public class DataSource int streamIndex, FormatControl formatControl) { - return new PortAudioStream(formatControl); + return new PortAudioStream(formatControl, audioQualityImprovement); } /** @@ -144,4 +172,28 @@ public class DataSource throw new IllegalArgumentException("locator.protocol"); } } + + /** + * Gets the <tt>Format</tt>s which are to be reported by a + * <tt>FormatControl</tt> as supported formats for a + * <tt>PullBufferStream</tt> at a specific zero-based index in the list of + * streams of this <tt>PullBufferDataSource</tt>. + * + * @param streamIndex the zero-based index of the <tt>PullBufferStream</tt> + * for which the specified <tt>FormatControl</tt> is to report the list of + * supported <tt>Format</tt>s + * @return an array of <tt>Format</tt>s to be reported by a + * <tt>FormatControl</tt> as the supported formats for the + * <tt>PullBufferStream</tt> at the specified <tt>streamIndex</tt> in the + * list of streams of this <tt>PullBufferDataSource</tt> + * @see AbstractPullBufferCaptureDevice#getSupportedFormats(int) + */ + @Override + protected Format[] getSupportedFormats(int streamIndex) + { + return + (supportedFormats == null) + ? super.getSupportedFormats(streamIndex) + : supportedFormats; + } } diff --git a/src/net/java/sip/communicator/impl/neomedia/jmfext/media/protocol/portaudio/PortAudioStream.java b/src/net/java/sip/communicator/impl/neomedia/jmfext/media/protocol/portaudio/PortAudioStream.java index 42bf536..641c079 100644 --- a/src/net/java/sip/communicator/impl/neomedia/jmfext/media/protocol/portaudio/PortAudioStream.java +++ b/src/net/java/sip/communicator/impl/neomedia/jmfext/media/protocol/portaudio/PortAudioStream.java @@ -24,6 +24,7 @@ import net.java.sip.communicator.impl.neomedia.portaudio.*; public class PortAudioStream extends AbstractPullBufferStream { + private final boolean audioQualityImprovement; /** * The number of bytes to read from a native PortAudio stream in a single @@ -69,10 +70,15 @@ public class PortAudioStream * * @param formatControl the <tt>FormatControl</tt> which is to abstract the * <tt>Format</tt>-related information of the new instance + * @param audioQualityImprovement */ - public PortAudioStream(FormatControl formatControl) + public PortAudioStream( + FormatControl formatControl, + boolean audioQualityImprovement) { super(formatControl); + + this.audioQualityImprovement = audioQualityImprovement; } /** @@ -274,10 +280,14 @@ public class PortAudioStream Format.NOT_SPECIFIED /* frameRate */, Format.byteArray); - PortAudio.setDenoise(stream, PortAudioManager.isEnabledDeNoise()); + PortAudio.setDenoise( + stream, + audioQualityImprovement + && PortAudioManager.isEnabledDeNoise()); PortAudio.setEchoFilterLengthInMillis( stream, - PortAudioManager.isEnabledEchoCancel() + (audioQualityImprovement + && PortAudioManager.isEnabledEchoCancel()) ? PortAudioManager.getFilterLengthInMillis() : 0); } |