mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-09 23:27:24 -05:00
Compare commits
23 Commits
v0.2.0-bet
...
v0.2.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
03e641525c | ||
|
|
f2b189ee2b | ||
|
|
3f0581dad1 | ||
|
|
daf2b58cf2 | ||
|
|
cf2c0ca07d | ||
|
|
eead22ffad | ||
|
|
1550c62782 | ||
|
|
5f2d8c8fe6 | ||
|
|
22d3871d0b | ||
|
|
455e45269c | ||
|
|
ca8406bdc0 | ||
|
|
399d164bd0 | ||
|
|
6f2a7e89a9 | ||
|
|
b821588e54 | ||
|
|
62aca27e66 | ||
|
|
cdcc487095 | ||
|
|
d5b6589e36 | ||
|
|
be7ab871d1 | ||
|
|
294a830aaf | ||
|
|
a7da60cd27 | ||
|
|
894ebeb040 | ||
|
|
682fe0c779 | ||
|
|
88d3088ebc |
@@ -7,8 +7,8 @@
|
||||
<property name="major" value="0"/>
|
||||
<property name="minor" value="2"/>
|
||||
<property name="patch" value="0"/>
|
||||
<property name="release" value="beta.6"/>
|
||||
<property name="filename" value="sdrtrunk_${major}.${minor}.${patch}-${release}"/>
|
||||
<property name="release" value=""/>
|
||||
<property name="filename" value="sdrtrunk_${major}.${minor}.${patch}${release}"/>
|
||||
|
||||
<!-- Build properties -->
|
||||
<property name="repo" location=".."/>
|
||||
|
||||
2
scripts/run_sdrtrunk_linux.sh
Normal file → Executable file
2
scripts/run_sdrtrunk_linux.sh
Normal file → Executable file
@@ -1,2 +1,2 @@
|
||||
#!/bin/bash
|
||||
java -XX:+UseG1GC -cp "*:libs/*" gui.SDRTrunk&
|
||||
java -XX:+UseG1GC -cp "*:libs/*" gui.SDRTrunk
|
||||
@@ -1,5 +1,3 @@
|
||||
package audio;
|
||||
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014-2016 Dennis Sheirer
|
||||
@@ -17,6 +15,8 @@ package audio;
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
******************************************************************************/
|
||||
package audio;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
@@ -429,19 +429,17 @@ public class AudioManager implements Listener<AudioPacket>, IAudioController
|
||||
* output and maintains current state information about the audio activity
|
||||
* received form the source.
|
||||
*/
|
||||
public class AudioOutputConnection implements Listener<AudioEvent>
|
||||
public class AudioOutputConnection
|
||||
{
|
||||
private static final int DISCONNECTED = -1;
|
||||
|
||||
private AudioOutput mAudioOutput;
|
||||
private int mPriority = 0;
|
||||
private long mLastActivity = System.currentTimeMillis();
|
||||
private int mSource = DISCONNECTED;
|
||||
|
||||
public AudioOutputConnection( AudioOutput audioOutput )
|
||||
{
|
||||
mAudioOutput = audioOutput;
|
||||
mAudioOutput.addAudioEventListener( this );
|
||||
}
|
||||
|
||||
public void receive( AudioPacket packet )
|
||||
@@ -457,8 +455,6 @@ public class AudioManager implements Listener<AudioPacket>, IAudioController
|
||||
updateLowestPriorityAssignment();
|
||||
}
|
||||
|
||||
updateTimestamp();
|
||||
|
||||
if( mAudioOutput != null )
|
||||
{
|
||||
mAudioOutput.receive( packet );
|
||||
@@ -481,11 +477,6 @@ public class AudioManager implements Listener<AudioPacket>, IAudioController
|
||||
}
|
||||
}
|
||||
|
||||
private void updateTimestamp()
|
||||
{
|
||||
mLastActivity = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminates the audio output and prepares this connection for disposal
|
||||
*/
|
||||
@@ -518,7 +509,8 @@ public class AudioManager implements Listener<AudioPacket>, IAudioController
|
||||
public void connect( int source )
|
||||
{
|
||||
mSource = source;
|
||||
updateTimestamp();
|
||||
|
||||
mAudioOutput.updateTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -536,6 +528,7 @@ public class AudioManager implements Listener<AudioPacket>, IAudioController
|
||||
public void disconnect()
|
||||
{
|
||||
mSource = DISCONNECTED;
|
||||
mPriority = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -544,28 +537,12 @@ public class AudioManager implements Listener<AudioPacket>, IAudioController
|
||||
*/
|
||||
public boolean isInactive()
|
||||
{
|
||||
return mLastActivity != Long.MAX_VALUE &&
|
||||
mLastActivity + AUDIO_TIMEOUT < System.currentTimeMillis();
|
||||
return ( mAudioOutput.getLastActivityTimestamp() + AUDIO_TIMEOUT ) < System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public int getPriority()
|
||||
{
|
||||
return mPriority;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receive( AudioEvent event )
|
||||
{
|
||||
switch( event.getType() )
|
||||
{
|
||||
case AUDIO_STARTED:
|
||||
case AUDIO_STOPPED:
|
||||
case AUDIO_CONTINUATION:
|
||||
updateTimestamp();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,11 @@ public class AudioPacket
|
||||
return mAudioData;
|
||||
}
|
||||
|
||||
public boolean hasAudioBuffer()
|
||||
{
|
||||
return mAudioData != null;
|
||||
}
|
||||
|
||||
public enum Type
|
||||
{
|
||||
AUDIO,
|
||||
|
||||
@@ -1,19 +1,6 @@
|
||||
package audio.metadata;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sample.Listener;
|
||||
import alias.Alias;
|
||||
import alias.priority.Priority;
|
||||
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014,2015 Dennis Sheirer
|
||||
* Copyright (C) 2014-2016 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -28,15 +15,30 @@ import alias.priority.Priority;
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
******************************************************************************/
|
||||
package audio.metadata;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sample.Listener;
|
||||
import alias.Alias;
|
||||
import alias.priority.Priority;
|
||||
|
||||
public class AudioMetadata implements Listener<Metadata>
|
||||
{
|
||||
protected static final Logger mLog = LoggerFactory.getLogger( AudioMetadata.class );
|
||||
|
||||
private int mSource;
|
||||
private List<Metadata> mMetadata = new ArrayList<>();
|
||||
private Map<MetadataType,Metadata> mMetadata = new HashMap<>();
|
||||
private boolean mUpdated = false;
|
||||
|
||||
/* Channel Selectect == highest priority */
|
||||
/* Channel Selected == highest priority */
|
||||
private boolean mSelected = false;
|
||||
|
||||
/* Call Priority */
|
||||
@@ -92,7 +94,8 @@ public class AudioMetadata implements Listener<Metadata>
|
||||
copy.mPriority = mPriority;
|
||||
copy.mSelected = mSelected;
|
||||
copy.mRecordable = mRecordable;
|
||||
copy.mMetadata.addAll( mMetadata );
|
||||
|
||||
copy.mMetadata.putAll( mMetadata );
|
||||
copy.mUpdated = mUpdated;
|
||||
copy.mIdentifier = new String( mIdentifier );
|
||||
|
||||
@@ -106,27 +109,36 @@ public class AudioMetadata implements Listener<Metadata>
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
Iterator<Metadata> it = mMetadata.iterator();
|
||||
|
||||
while( it.hasNext() )
|
||||
{
|
||||
if( it.next().isTemporal() )
|
||||
{
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/* Reset recordable state, identifier and audio call priority levels
|
||||
* and reprocess the metadata */
|
||||
mRecordable = mSourceRecordable;
|
||||
setIdentifier( mSource );
|
||||
mPriority = Priority.DEFAULT_PRIORITY;
|
||||
|
||||
for( Metadata metadata: mMetadata )
|
||||
List<MetadataType> toRemove = new ArrayList<>();
|
||||
|
||||
for( MetadataType type: mMetadata.keySet() )
|
||||
{
|
||||
processMetadata( metadata );
|
||||
Metadata metadata = mMetadata.get( type );
|
||||
|
||||
if( metadata != null )
|
||||
{
|
||||
if( metadata.isTemporal() )
|
||||
{
|
||||
toRemove.add( type );
|
||||
}
|
||||
else
|
||||
{
|
||||
processMetadata( metadata );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for( MetadataType type: toRemove )
|
||||
{
|
||||
mMetadata.remove( type );
|
||||
}
|
||||
|
||||
mUpdated = true;
|
||||
}
|
||||
|
||||
@@ -185,8 +197,14 @@ public class AudioMetadata implements Listener<Metadata>
|
||||
}
|
||||
else
|
||||
{
|
||||
mMetadata.add( metadata );
|
||||
processMetadata( metadata );
|
||||
Metadata existing = mMetadata.get( metadata.getMetadataType() );
|
||||
|
||||
if( existing == null || !existing.equals( metadata ) )
|
||||
{
|
||||
mMetadata.put( metadata.getMetadataType(), metadata );
|
||||
processMetadata( metadata );
|
||||
setUpdated( true );
|
||||
}
|
||||
}
|
||||
|
||||
mUpdated = true;
|
||||
@@ -195,26 +213,17 @@ public class AudioMetadata implements Listener<Metadata>
|
||||
/**
|
||||
* Returns the complete set of metadata
|
||||
*/
|
||||
public List<Metadata> getMetadata()
|
||||
public Map<MetadataType,Metadata> getMetadata()
|
||||
{
|
||||
return mMetadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first metadata object matching the type, or null if the
|
||||
* metadata type is not found.
|
||||
* Returns the metadata object matching the type or null
|
||||
*/
|
||||
public Metadata getMetadata( MetadataType type )
|
||||
{
|
||||
for( Metadata metadata: mMetadata )
|
||||
{
|
||||
if( metadata.getMetadataType() == type )
|
||||
{
|
||||
return metadata;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return mMetadata.get( type );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -296,11 +305,11 @@ public class AudioMetadata implements Listener<Metadata>
|
||||
sb.append( getPriority() );
|
||||
sb.append( " updated:" + isUpdated() );
|
||||
sb.append( " " );
|
||||
for( Metadata m: getMetadata() )
|
||||
for( Entry<MetadataType,Metadata> entry: getMetadata().entrySet() )
|
||||
{
|
||||
sb.append( m.getKey() );
|
||||
sb.append( entry.getValue().getKey() );
|
||||
sb.append( ":" );
|
||||
sb.append( m.getValue() );
|
||||
sb.append( entry.getValue().getValue() );
|
||||
sb.append( " " );
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public class Metadata
|
||||
mValueAlias = alias;
|
||||
mTemporal = temporal;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constucts a metadata object with the temporal settings. Temporal
|
||||
* indicates that this metadata will be removed at the end of a reset
|
||||
@@ -103,4 +103,44 @@ public class Metadata
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public boolean equals( Metadata other )
|
||||
{
|
||||
if( mMetadataType != other.getMetadataType() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if( mTemporal != other.isTemporal() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if( mValue != null && other.getValue() != null &&
|
||||
!mValue.contentEquals( other.getValue() ))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if( ( mValue == null && other.getValue() != null ) ||
|
||||
( mValue != null && other.getValue() == null ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if( mValueAlias != null && other.getAlias() != null &&
|
||||
!mValueAlias.getName().contentEquals( other.getAlias().getName() ))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if( ( mValueAlias == null && other.getAlias() != null ) ||
|
||||
( mValueAlias != null && other.getAlias() == null ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,34 +1,148 @@
|
||||
package audio.output;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.LinkedTransferQueue;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
import javax.sound.sampled.BooleanControl;
|
||||
import javax.sound.sampled.Control;
|
||||
import javax.sound.sampled.FloatControl;
|
||||
import javax.sound.sampled.Line;
|
||||
import javax.sound.sampled.LineEvent;
|
||||
import javax.sound.sampled.LineListener;
|
||||
import javax.sound.sampled.LineUnavailableException;
|
||||
import javax.sound.sampled.Mixer;
|
||||
import javax.sound.sampled.SourceDataLine;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sample.Broadcaster;
|
||||
import sample.Listener;
|
||||
import source.mixer.MixerChannel;
|
||||
import audio.AudioEvent;
|
||||
import audio.AudioPacket;
|
||||
import audio.AudioPacket.Type;
|
||||
import audio.metadata.AudioMetadata;
|
||||
import controller.ThreadPoolManager;
|
||||
import controller.ThreadPoolManager.ThreadType;
|
||||
|
||||
public abstract class AudioOutput implements Listener<AudioPacket>, LineListener
|
||||
{
|
||||
private final static Logger mLog = LoggerFactory.getLogger( AudioOutput.class );
|
||||
|
||||
private Listener<AudioMetadata> mAudioMetadataListener;
|
||||
protected Broadcaster<AudioEvent> mAudioEventBroadcaster = new Broadcaster<>();
|
||||
protected LinkedTransferQueue<AudioPacket> mBuffer = new LinkedTransferQueue<>();
|
||||
protected ThreadPoolManager mThreadPoolManager;
|
||||
protected boolean mCanProcessAudio;
|
||||
protected long mLastActivity;
|
||||
private LinkedTransferQueue<AudioPacket> mBuffer = new LinkedTransferQueue<>();
|
||||
private int mBufferStartThreshold;
|
||||
private int mBufferStopThreshold;
|
||||
|
||||
public AudioOutput( ThreadPoolManager threadPoolManager )
|
||||
private Listener<AudioMetadata> mAudioMetadataListener;
|
||||
private Broadcaster<AudioEvent> mAudioEventBroadcaster = new Broadcaster<>();
|
||||
|
||||
private ThreadPoolManager mThreadPoolManager;
|
||||
private ScheduledFuture<?> mProcessorTask;
|
||||
|
||||
private SourceDataLine mOutput;
|
||||
private Mixer mMixer;
|
||||
private MixerChannel mMixerChannel;
|
||||
private FloatControl mGainControl;
|
||||
private BooleanControl mMuteControl;
|
||||
|
||||
private AudioEvent mAudioStartEvent;
|
||||
private AudioEvent mAudioStopEvent;
|
||||
|
||||
private boolean mCanProcessAudio = false;
|
||||
private long mLastActivity = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* Single audio channel playback with automatic starting and stopping of the
|
||||
* underlying sourcedataline specified by the mixer and mixer channel
|
||||
* arguments.
|
||||
*
|
||||
* Maintains an internal non-blocking audio packet queue and processes this
|
||||
* queue 25 times a second (every 40 ms).
|
||||
*
|
||||
* @param threadPoolManager for assigning buffer processing schedule task
|
||||
* @param mixer to obtain source data line
|
||||
* @param mixerChannel either mono or left/right stereo
|
||||
* @param audioFormat to use during playback
|
||||
* @param lineInfo to use when obtaining the source data line
|
||||
* @param requestedBufferSize of approximately 1 second of audio
|
||||
*/
|
||||
public AudioOutput( ThreadPoolManager threadPoolManager,
|
||||
Mixer mixer,
|
||||
MixerChannel mixerChannel,
|
||||
AudioFormat audioFormat,
|
||||
Line.Info lineInfo,
|
||||
int requestedBufferSize )
|
||||
{
|
||||
mThreadPoolManager = threadPoolManager;
|
||||
mMixer = mixer;
|
||||
mMixerChannel = mixerChannel;
|
||||
|
||||
try
|
||||
{
|
||||
mOutput = (SourceDataLine)mMixer.getLine( lineInfo );
|
||||
|
||||
if( mOutput != null )
|
||||
{
|
||||
mOutput.open( audioFormat, requestedBufferSize );
|
||||
|
||||
//Start threshold: buffer is full with 10% or less of capacity remaining
|
||||
mBufferStartThreshold = (int)( mOutput.getBufferSize() * 0.10 );
|
||||
|
||||
//Stop threshold: buffer is empty with 90% or more capacity available
|
||||
mBufferStopThreshold = (int)( mOutput.getBufferSize() * 0.90 );
|
||||
|
||||
mOutput.addLineListener( this );
|
||||
|
||||
if( mOutput != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
Control gain = mOutput.getControl( FloatControl.Type.MASTER_GAIN );
|
||||
mGainControl = (FloatControl)gain;
|
||||
}
|
||||
catch( IllegalArgumentException iae )
|
||||
{
|
||||
mLog.warn( "Couldn't obtain MASTER GAIN control for stereo line [" +
|
||||
mixer.getMixerInfo().getName() + " | " + getChannelName() + "]" );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Control mute = mOutput.getControl( BooleanControl.Type.MUTE );
|
||||
mMuteControl = (BooleanControl)mute;
|
||||
}
|
||||
catch( IllegalArgumentException iae )
|
||||
{
|
||||
mLog.warn( "Couldn't obtain MUTE control for stereo line [" +
|
||||
mixer.getMixerInfo().getName() + " | " + getChannelName() + "]" );
|
||||
}
|
||||
|
||||
/* Run the queue processor task every 40 milliseconds or 25 times a second */
|
||||
mProcessorTask = mThreadPoolManager.scheduleFixedRate(
|
||||
ThreadType.AUDIO_PROCESSING, new BufferProcessor(),
|
||||
40, TimeUnit.MILLISECONDS );
|
||||
}
|
||||
|
||||
mAudioStartEvent = new AudioEvent( AudioEvent.Type.AUDIO_STARTED,
|
||||
getChannelName() );
|
||||
mAudioStopEvent = new AudioEvent( AudioEvent.Type.AUDIO_STOPPED,
|
||||
getChannelName() );
|
||||
|
||||
mCanProcessAudio = true;
|
||||
}
|
||||
}
|
||||
catch ( LineUnavailableException e )
|
||||
{
|
||||
mLog.error( "Couldn't obtain audio source data line for "
|
||||
+ "audio output - mixer [" + mMixer.getMixerInfo().getName() + "]" );
|
||||
}
|
||||
}
|
||||
|
||||
public void reset()
|
||||
@@ -41,36 +155,52 @@ public abstract class AudioOutput implements Listener<AudioPacket>, LineListener
|
||||
{
|
||||
mCanProcessAudio = false;
|
||||
|
||||
if( mProcessorTask != null && mThreadPoolManager != null )
|
||||
{
|
||||
mThreadPoolManager.cancel( mProcessorTask );
|
||||
}
|
||||
|
||||
mThreadPoolManager = null;
|
||||
mProcessorTask = null;
|
||||
|
||||
mBuffer.clear();
|
||||
|
||||
mAudioEventBroadcaster.dispose();
|
||||
mAudioEventBroadcaster = null;
|
||||
mAudioMetadataListener = null;
|
||||
|
||||
mThreadPoolManager = null;
|
||||
}
|
||||
|
||||
public abstract String getChannelName();
|
||||
|
||||
public abstract void setMuted( boolean muted );
|
||||
public abstract boolean isMuted();
|
||||
|
||||
public abstract FloatControl getGainControl();
|
||||
public abstract boolean hasGainControl();
|
||||
|
||||
protected void broadcast( AudioMetadata metadata )
|
||||
{
|
||||
if( mAudioMetadataListener != null )
|
||||
if( mOutput != null )
|
||||
{
|
||||
mAudioMetadataListener.receive( metadata );
|
||||
mOutput.close();
|
||||
}
|
||||
|
||||
mOutput = null;
|
||||
mGainControl = null;
|
||||
mMuteControl = null;
|
||||
}
|
||||
|
||||
protected void broadcast( AudioEvent audioEvent )
|
||||
|
||||
/**
|
||||
* Converts the audio packet data into a byte buffer format appropriate for
|
||||
* the underlying source data line.
|
||||
*/
|
||||
protected abstract ByteBuffer convert( AudioPacket packet );
|
||||
|
||||
/**
|
||||
* Audio output channel name
|
||||
*/
|
||||
public String getChannelName()
|
||||
{
|
||||
mAudioEventBroadcaster.broadcast( audioEvent );
|
||||
return mMixerChannel.getLabel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixer Channel for this audio output
|
||||
*/
|
||||
protected MixerChannel getMixerChannel()
|
||||
{
|
||||
return mMixerChannel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a single listener to receive audio start and audio stop events
|
||||
*/
|
||||
@@ -84,6 +214,14 @@ public abstract class AudioOutput implements Listener<AudioPacket>, LineListener
|
||||
mAudioEventBroadcaster.removeListener( listener );
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcasts an audio event to the registered listener
|
||||
*/
|
||||
private void broadcast( AudioEvent audioEvent )
|
||||
{
|
||||
mAudioEventBroadcaster.broadcast( audioEvent );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a single listener to receive the audio metadata from each
|
||||
* audio packet
|
||||
@@ -99,21 +237,200 @@ public abstract class AudioOutput implements Listener<AudioPacket>, LineListener
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp when most recent buffer was received
|
||||
* Broadcasts audio metadata to the registered listener
|
||||
*/
|
||||
public long getLastBufferReceived()
|
||||
private void broadcast( AudioMetadata metadata )
|
||||
{
|
||||
if( mAudioMetadataListener != null )
|
||||
{
|
||||
mAudioMetadataListener.receive( metadata );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamp of either last buffer received or last buffer processed
|
||||
*/
|
||||
public long getLastActivityTimestamp()
|
||||
{
|
||||
return mLastActivity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the last activity timestamp to current system time
|
||||
*/
|
||||
public void updateTimestamp()
|
||||
{
|
||||
mLastActivity = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receive( AudioPacket packet )
|
||||
{
|
||||
if( mCanProcessAudio )
|
||||
{
|
||||
mLastActivity = System.currentTimeMillis();
|
||||
//Update the activity timestamp so that this audio output doesn't
|
||||
//get disconnected before it starts processing the audio stream
|
||||
updateTimestamp();
|
||||
|
||||
mBuffer.add( packet );
|
||||
}
|
||||
}
|
||||
|
||||
public class BufferProcessor implements Runnable
|
||||
{
|
||||
private AtomicBoolean mProcessing = new AtomicBoolean();
|
||||
|
||||
public BufferProcessor()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
/* The processing flag ensures that only one instance of the
|
||||
* processor can run at any given time */
|
||||
if( mProcessing.compareAndSet( false, true ) )
|
||||
{
|
||||
List<AudioPacket> packets = new ArrayList<AudioPacket>();
|
||||
|
||||
mBuffer.drainTo( packets );
|
||||
|
||||
for( AudioPacket packet: packets )
|
||||
{
|
||||
if( packet.getType() == Type.AUDIO )
|
||||
{
|
||||
broadcast( packet.getAudioMetadata() );
|
||||
|
||||
ByteBuffer buffer = convert( packet );
|
||||
|
||||
int wrote = 0;
|
||||
|
||||
if( !mOutput.isRunning() )
|
||||
{
|
||||
int toWrite = mOutput.available();
|
||||
|
||||
if( toWrite > buffer.array().length )
|
||||
{
|
||||
toWrite = buffer.array().length;
|
||||
}
|
||||
|
||||
//Top off the buffer and check if we can start it
|
||||
wrote += mOutput.write( buffer.array(), 0, toWrite );
|
||||
|
||||
checkStart();
|
||||
}
|
||||
|
||||
if( mOutput.isRunning() && wrote < buffer.array().length )
|
||||
{
|
||||
//Blocking write
|
||||
wrote += mOutput.write( buffer.array(), wrote,
|
||||
buffer.array().length - wrote );
|
||||
}
|
||||
|
||||
updateTimestamp();
|
||||
}
|
||||
}
|
||||
|
||||
checkStop();
|
||||
|
||||
mProcessing.set( false );
|
||||
}
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
mLog.error( "Error while processing audio buffers", e );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts audio playback once audio buffer is almost full and remaining
|
||||
* capacity falls below the start threshold.
|
||||
*/
|
||||
private void checkStart()
|
||||
{
|
||||
if( mCanProcessAudio &&
|
||||
!mOutput.isRunning() &&
|
||||
mOutput.available() <= mBufferStartThreshold )
|
||||
{
|
||||
mOutput.start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops audio playback and drains the audio buffer to empty when the
|
||||
* audio buffer is mostly empty and the available buffer capacity
|
||||
* exceeds the stop threshold
|
||||
*/
|
||||
private void checkStop()
|
||||
{
|
||||
if( mCanProcessAudio &&
|
||||
mOutput.isRunning() &&
|
||||
mOutput.available() >= mBufferStopThreshold )
|
||||
{
|
||||
mOutput.drain();
|
||||
mOutput.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the mute state for this audio output channel
|
||||
*/
|
||||
public void setMuted( boolean muted )
|
||||
{
|
||||
if( mMuteControl != null )
|
||||
{
|
||||
mMuteControl.setValue( muted );
|
||||
|
||||
broadcast( new AudioEvent( muted ? AudioEvent.Type.AUDIO_MUTED :
|
||||
AudioEvent.Type.AUDIO_UNMUTED, getChannelName() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Current mute state for this audio output channel
|
||||
*/
|
||||
public boolean isMuted()
|
||||
{
|
||||
if( mMuteControl != null )
|
||||
{
|
||||
return mMuteControl.getValue();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gain/volume control for this audio output channel, if one is available.
|
||||
*/
|
||||
public FloatControl getGainControl()
|
||||
{
|
||||
return mGainControl;
|
||||
}
|
||||
|
||||
public boolean hasGainControl()
|
||||
{
|
||||
return mGainControl != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Monitors the source data line playback state and broadcasts audio events
|
||||
* to the registered listener as the state changes
|
||||
*/
|
||||
@Override
|
||||
public void update( LineEvent event )
|
||||
{
|
||||
LineEvent.Type type = event.getType();
|
||||
|
||||
if( type == LineEvent.Type.START )
|
||||
{
|
||||
mAudioEventBroadcaster.broadcast( mAudioStartEvent );
|
||||
}
|
||||
else if( type == LineEvent.Type.STOP )
|
||||
{
|
||||
mAudioEventBroadcaster.broadcast( mAudioStopEvent );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,280 +3,55 @@ package audio.output;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.ShortBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import javax.sound.sampled.BooleanControl;
|
||||
import javax.sound.sampled.Control;
|
||||
import javax.sound.sampled.FloatControl;
|
||||
import javax.sound.sampled.LineEvent;
|
||||
import javax.sound.sampled.LineUnavailableException;
|
||||
import javax.sound.sampled.Mixer;
|
||||
import javax.sound.sampled.SourceDataLine;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import source.mixer.MixerChannel;
|
||||
import audio.AudioEvent;
|
||||
import audio.AudioFormats;
|
||||
import audio.AudioPacket;
|
||||
import audio.AudioPacket.Type;
|
||||
import controller.ThreadPoolManager;
|
||||
import controller.ThreadPoolManager.ThreadType;
|
||||
|
||||
/**
|
||||
* Mono Audio output with automatic flow control based on the availability of
|
||||
* audio data packets.
|
||||
* Mono Audio output implementation
|
||||
*/
|
||||
public class MonoAudioOutput extends AudioOutput
|
||||
{
|
||||
private final static Logger mLog = LoggerFactory.getLogger( MonoAudioOutput.class );
|
||||
|
||||
/* Output line buffer size is set at one half second of audio at 48 kHz and
|
||||
* two bytes per sample */
|
||||
private final static int BUFFER_SIZE = 48000;
|
||||
|
||||
/* The queue processor will run every 20 milliseconds checking for inbound
|
||||
* audio and automatically starting or stopping audio playback. We set the
|
||||
* data line available space threshold to start when we're 20 ms away from
|
||||
* being full, and the stop threshold when 20 ms away from being empty */
|
||||
private final static int SAMPLE_SIZE_40_MS = BUFFER_SIZE / 25;
|
||||
private final static int START_THRESHOLD = SAMPLE_SIZE_40_MS;
|
||||
private final static int STOP_THRESHOLD = BUFFER_SIZE - SAMPLE_SIZE_40_MS;
|
||||
|
||||
private SourceDataLine mOutput;
|
||||
private FloatControl mGainControl;
|
||||
private BooleanControl mMuteControl;
|
||||
private AudioEvent mAudioStartEvent;
|
||||
private AudioEvent mAudioStopEvent;
|
||||
private AudioEvent mAudioContinuationEvent;
|
||||
private ScheduledFuture<?> mProcessorTask;
|
||||
|
||||
public MonoAudioOutput( ThreadPoolManager threadPoolManager, Mixer mixer )
|
||||
{
|
||||
super( threadPoolManager );
|
||||
super( threadPoolManager,
|
||||
mixer,
|
||||
MixerChannel.MONO,
|
||||
AudioFormats.PCM_SIGNED_48KHZ_16BITS_MONO,
|
||||
AudioFormats.MONO_SOURCE_DATALINE_INFO,
|
||||
BUFFER_SIZE );
|
||||
}
|
||||
|
||||
mAudioStartEvent = new AudioEvent( AudioEvent.Type.AUDIO_STARTED,
|
||||
getChannelName() );
|
||||
mAudioStopEvent = new AudioEvent( AudioEvent.Type.AUDIO_STOPPED,
|
||||
getChannelName() );
|
||||
mAudioContinuationEvent = new AudioEvent( AudioEvent.Type.AUDIO_CONTINUATION,
|
||||
getChannelName() );
|
||||
|
||||
try
|
||||
/**
|
||||
* Converts the audio packet data into mono audio frames.
|
||||
*/
|
||||
protected ByteBuffer convert( AudioPacket packet )
|
||||
{
|
||||
if( packet.hasAudioBuffer() )
|
||||
{
|
||||
mOutput = (SourceDataLine)mixer.getLine(
|
||||
AudioFormats.MONO_SOURCE_DATALINE_INFO );
|
||||
float[] samples = packet.getAudioBuffer().getSamples();
|
||||
|
||||
if( mOutput != null )
|
||||
/* Little-endian byte buffer */
|
||||
ByteBuffer buffer =
|
||||
ByteBuffer.allocate( samples.length * 2 )
|
||||
.order( ByteOrder.LITTLE_ENDIAN );
|
||||
|
||||
ShortBuffer shortBuffer = buffer.asShortBuffer();
|
||||
|
||||
for( float sample: samples )
|
||||
{
|
||||
mOutput.open( AudioFormats.PCM_SIGNED_48KHZ_16BITS_MONO, BUFFER_SIZE );
|
||||
mOutput.addLineListener( this );
|
||||
mCanProcessAudio = true;
|
||||
}
|
||||
}
|
||||
catch ( LineUnavailableException e )
|
||||
{
|
||||
mLog.error( "Couldn't obtain source data line for 48kHz PCM mono "
|
||||
+ "audio output" );
|
||||
}
|
||||
|
||||
if( mOutput != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
Control gain = mOutput.getControl( FloatControl.Type.MASTER_GAIN );
|
||||
mGainControl = (FloatControl)gain;
|
||||
}
|
||||
catch( IllegalArgumentException iae )
|
||||
{
|
||||
mLog.warn( "Couldn't obtain MASTER GAIN control for mono line [" +
|
||||
mixer.getMixerInfo().getName() + "]" );
|
||||
shortBuffer.put( (short)( sample * Short.MAX_VALUE ) );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Control mute = mOutput.getControl( BooleanControl.Type.MUTE );
|
||||
mMuteControl = (BooleanControl)mute;
|
||||
}
|
||||
catch( IllegalArgumentException iae )
|
||||
{
|
||||
mLog.warn( "Couldn't obtain MUTE control for mono line [" +
|
||||
mixer.getMixerInfo().getName() + "]" );
|
||||
}
|
||||
|
||||
/* Run the queue processor task every 40 milliseconds */
|
||||
mProcessorTask = mThreadPoolManager.scheduleFixedRate(
|
||||
ThreadType.AUDIO_PROCESSING, new QueueProcessor(),
|
||||
40, TimeUnit.MILLISECONDS );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getChannelName()
|
||||
{
|
||||
return MixerChannel.MONO.getLabel();
|
||||
}
|
||||
|
||||
public void dispose()
|
||||
{
|
||||
if( mProcessorTask != null && mThreadPoolManager != null )
|
||||
{
|
||||
mThreadPoolManager.cancel( mProcessorTask );
|
||||
return buffer;
|
||||
}
|
||||
|
||||
mProcessorTask = null;
|
||||
|
||||
super.dispose();
|
||||
|
||||
if( mOutput != null )
|
||||
{
|
||||
mOutput.close();
|
||||
}
|
||||
|
||||
mOutput = null;
|
||||
mGainControl = null;
|
||||
mMuteControl = null;
|
||||
}
|
||||
|
||||
public class QueueProcessor implements Runnable
|
||||
{
|
||||
private AtomicBoolean mProcessing = new AtomicBoolean();
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
/* The mProcessing flag ensures that only one instance of the
|
||||
* processor can run at any given time */
|
||||
if( mProcessing.compareAndSet( false, true ) )
|
||||
{
|
||||
List<AudioPacket> packets = new ArrayList<AudioPacket>();
|
||||
|
||||
mBuffer.drainTo( packets );
|
||||
|
||||
if( packets.size() > 0 )
|
||||
{
|
||||
for( AudioPacket packet: packets )
|
||||
{
|
||||
broadcast( mAudioContinuationEvent );
|
||||
|
||||
if( mCanProcessAudio )
|
||||
{
|
||||
if( packet.getType() == Type.AUDIO )
|
||||
{
|
||||
float[] samples = packet.getAudioBuffer().getSamples();
|
||||
|
||||
/* Little-endian byte buffer */
|
||||
ByteBuffer buffer =
|
||||
ByteBuffer.allocate( samples.length * 2 )
|
||||
.order( ByteOrder.LITTLE_ENDIAN );
|
||||
|
||||
ShortBuffer shortBuffer = buffer.asShortBuffer();
|
||||
|
||||
for( float sample: samples )
|
||||
{
|
||||
shortBuffer.put( (short)( sample * Short.MAX_VALUE ) );
|
||||
}
|
||||
|
||||
//This is a blocking write
|
||||
mOutput.write( buffer.array(), 0, buffer.array().length );
|
||||
|
||||
checkStart();
|
||||
|
||||
broadcast( packet.getAudioMetadata() );
|
||||
|
||||
mLastActivity = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
checkStop();
|
||||
}
|
||||
|
||||
mProcessing.set( false );
|
||||
}
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
mLog.error( "Error while processing audio buffers", e );
|
||||
}
|
||||
}
|
||||
|
||||
private void checkStart()
|
||||
{
|
||||
if( mCanProcessAudio &&
|
||||
!mOutput.isRunning() && mOutput.available() <= START_THRESHOLD )
|
||||
{
|
||||
mOutput.start();
|
||||
}
|
||||
}
|
||||
|
||||
private void checkStop()
|
||||
{
|
||||
if( mCanProcessAudio && mOutput.isRunning() &&
|
||||
mOutput.available() >= STOP_THRESHOLD )
|
||||
{
|
||||
mOutput.drain();
|
||||
mOutput.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMuted( boolean muted )
|
||||
{
|
||||
if( mMuteControl != null )
|
||||
{
|
||||
mMuteControl.setValue( muted );
|
||||
|
||||
broadcast( new AudioEvent( muted ? AudioEvent.Type.AUDIO_MUTED :
|
||||
AudioEvent.Type.AUDIO_UNMUTED, getChannelName() ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMuted()
|
||||
{
|
||||
if( mMuteControl != null )
|
||||
{
|
||||
return mMuteControl.getValue();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FloatControl getGainControl()
|
||||
{
|
||||
return mGainControl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasGainControl()
|
||||
{
|
||||
return mGainControl != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update( LineEvent event )
|
||||
{
|
||||
LineEvent.Type type = event.getType();
|
||||
|
||||
if( type == LineEvent.Type.START )
|
||||
{
|
||||
mAudioEventBroadcaster.broadcast( mAudioStartEvent );
|
||||
}
|
||||
else if( type == LineEvent.Type.STOP )
|
||||
{
|
||||
mAudioEventBroadcaster.broadcast( mAudioStopEvent );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,310 +3,69 @@ package audio.output;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.ShortBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import javax.sound.sampled.BooleanControl;
|
||||
import javax.sound.sampled.Control;
|
||||
import javax.sound.sampled.FloatControl;
|
||||
import javax.sound.sampled.LineEvent;
|
||||
import javax.sound.sampled.LineUnavailableException;
|
||||
import javax.sound.sampled.Mixer;
|
||||
import javax.sound.sampled.SourceDataLine;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import source.mixer.MixerChannel;
|
||||
import audio.AudioEvent;
|
||||
import audio.AudioFormats;
|
||||
import audio.AudioPacket;
|
||||
import audio.AudioPacket.Type;
|
||||
import controller.ThreadPoolManager;
|
||||
import controller.ThreadPoolManager.ThreadType;
|
||||
|
||||
/**
|
||||
* Mono Audio output in stereo format with automatic flow control based on the
|
||||
* availability of audio data packets.
|
||||
* Stereo audio output implementation.
|
||||
*/
|
||||
public class StereoAudioOutput extends AudioOutput
|
||||
{
|
||||
private final static Logger mLog = LoggerFactory.getLogger( StereoAudioOutput.class );
|
||||
|
||||
/* Output line buffer size is set at one half second of audio at 48 kHz and
|
||||
* two bytes per sample for two channels */
|
||||
private final static int BUFFER_SIZE = 96000;
|
||||
|
||||
/* The queue processor will run every 20 milliseconds checking for inbound
|
||||
* audio and automatically start or stop audio playback. We set the
|
||||
* data line available space threshold to start when we're 20 ms away from
|
||||
* being full and the stop threshold when 20 ms away from being empty */
|
||||
private final static int SAMPLE_SIZE_40_MS = BUFFER_SIZE / 25;
|
||||
private final static int START_THRESHOLD = SAMPLE_SIZE_40_MS;
|
||||
private final static int STOP_THRESHOLD = BUFFER_SIZE - SAMPLE_SIZE_40_MS;
|
||||
|
||||
private SourceDataLine mOutput;
|
||||
private FloatControl mGainControl;
|
||||
private BooleanControl mMuteControl;
|
||||
private AudioEvent mAudioStartEvent;
|
||||
private AudioEvent mAudioStopEvent;
|
||||
private AudioEvent mAudioContinuationEvent;
|
||||
private MixerChannel mMixerChannel;
|
||||
private ScheduledFuture<?> mProcessorTask;
|
||||
|
||||
public StereoAudioOutput( ThreadPoolManager threadPoolManager,
|
||||
Mixer mixer, MixerChannel channel )
|
||||
{
|
||||
super( threadPoolManager );
|
||||
|
||||
assert( channel == MixerChannel.LEFT || channel == MixerChannel.RIGHT );
|
||||
|
||||
mMixerChannel = channel;
|
||||
super( threadPoolManager,
|
||||
mixer,
|
||||
channel,
|
||||
AudioFormats.PCM_SIGNED_48KHZ_16BITS_STEREO,
|
||||
AudioFormats.STEREO_SOURCE_DATALINE_INFO,
|
||||
BUFFER_SIZE );
|
||||
}
|
||||
|
||||
mAudioStartEvent = new AudioEvent( AudioEvent.Type.AUDIO_STARTED,
|
||||
getChannelName() );
|
||||
mAudioStopEvent = new AudioEvent( AudioEvent.Type.AUDIO_STOPPED,
|
||||
getChannelName() );
|
||||
mAudioContinuationEvent = new AudioEvent( AudioEvent.Type.AUDIO_CONTINUATION,
|
||||
getChannelName() );
|
||||
|
||||
try
|
||||
/**
|
||||
* Converts the audio packet data into stereo audio frames with the mixer
|
||||
* channel containing the audio and the other channel containing zero
|
||||
* valued (silent) samples.
|
||||
*/
|
||||
protected ByteBuffer convert( AudioPacket packet )
|
||||
{
|
||||
if( packet.hasAudioBuffer() )
|
||||
{
|
||||
mOutput = (SourceDataLine)mixer.getLine(
|
||||
AudioFormats.STEREO_SOURCE_DATALINE_INFO );
|
||||
float[] samples = packet.getAudioBuffer().getSamples();
|
||||
|
||||
if( mOutput != null )
|
||||
{
|
||||
mOutput.open( AudioFormats.PCM_SIGNED_48KHZ_16BITS_STEREO, BUFFER_SIZE );
|
||||
mOutput.addLineListener( this );
|
||||
mCanProcessAudio = true;
|
||||
}
|
||||
}
|
||||
catch ( LineUnavailableException e )
|
||||
{
|
||||
mLog.error( "Couldn't obtain source data line for 48kHz PCM stereo "
|
||||
+ "audio output - mixer [" + mixer.getMixerInfo().getName() +
|
||||
"] channel [" + mMixerChannel.name() + "]" );
|
||||
}
|
||||
|
||||
if( mOutput != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
Control gain = mOutput.getControl( FloatControl.Type.MASTER_GAIN );
|
||||
mGainControl = (FloatControl)gain;
|
||||
}
|
||||
catch( IllegalArgumentException iae )
|
||||
{
|
||||
mLog.warn( "Couldn't obtain MASTER GAIN control for stereo line [" +
|
||||
mixer.getMixerInfo().getName() + " | " + channel.name() + "]" );
|
||||
}
|
||||
/* Little-endian byte buffer */
|
||||
ByteBuffer buffer = ByteBuffer.allocate( samples.length * 4 )
|
||||
.order( ByteOrder.LITTLE_ENDIAN );
|
||||
|
||||
try
|
||||
{
|
||||
Control mute = mOutput.getControl( BooleanControl.Type.MUTE );
|
||||
mMuteControl = (BooleanControl)mute;
|
||||
}
|
||||
catch( IllegalArgumentException iae )
|
||||
{
|
||||
mLog.warn( "Couldn't obtain MUTE control for stereo line [" +
|
||||
mixer.getMixerInfo().getName() + " | " + channel.name() + "]" );
|
||||
}
|
||||
|
||||
/* Run the queue processor task every 40 milliseconds */
|
||||
mProcessorTask = mThreadPoolManager.scheduleFixedRate(
|
||||
ThreadType.AUDIO_PROCESSING, new QueueProcessor(),
|
||||
40, TimeUnit.MILLISECONDS );
|
||||
}
|
||||
}
|
||||
ShortBuffer shortBuffer = buffer.asShortBuffer();
|
||||
|
||||
@Override
|
||||
public String getChannelName()
|
||||
{
|
||||
return mMixerChannel.getLabel();
|
||||
}
|
||||
|
||||
public void dispose()
|
||||
{
|
||||
if( mProcessorTask != null && mThreadPoolManager != null )
|
||||
{
|
||||
mThreadPoolManager.cancel( mProcessorTask );
|
||||
}
|
||||
|
||||
mProcessorTask = null;
|
||||
|
||||
super.dispose();
|
||||
|
||||
if( mOutput != null )
|
||||
{
|
||||
mOutput.close();
|
||||
}
|
||||
|
||||
mOutput = null;
|
||||
mGainControl = null;
|
||||
mMuteControl = null;
|
||||
}
|
||||
|
||||
private ByteBuffer convert( float[] samples )
|
||||
{
|
||||
/* Little-endian byte buffer */
|
||||
ByteBuffer buffer = ByteBuffer.allocate( samples.length * 4 )
|
||||
.order( ByteOrder.LITTLE_ENDIAN );
|
||||
|
||||
ShortBuffer shortBuffer = buffer.asShortBuffer();
|
||||
|
||||
if( mMixerChannel == MixerChannel.LEFT )
|
||||
{
|
||||
for( float sample: samples )
|
||||
if( getMixerChannel() == MixerChannel.LEFT )
|
||||
{
|
||||
shortBuffer.put( (short)( sample * Short.MAX_VALUE ) );
|
||||
shortBuffer.put( (short)0 );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for( float sample: samples )
|
||||
{
|
||||
shortBuffer.put( (short)0 );
|
||||
shortBuffer.put( (short)( sample * Short.MAX_VALUE ) );
|
||||
}
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public class QueueProcessor implements Runnable
|
||||
{
|
||||
private AtomicBoolean mProcessing = new AtomicBoolean();
|
||||
|
||||
public QueueProcessor()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
/* The mProcessing flag ensures that only one instance of the
|
||||
* processor can run at any given time */
|
||||
if( mProcessing.compareAndSet( false, true ) )
|
||||
for( float sample: samples )
|
||||
{
|
||||
List<AudioPacket> packets = new ArrayList<AudioPacket>();
|
||||
|
||||
mBuffer.drainTo( packets );
|
||||
|
||||
if( packets.size() > 0 )
|
||||
{
|
||||
|
||||
for( AudioPacket packet: packets )
|
||||
{
|
||||
broadcast( mAudioContinuationEvent );
|
||||
|
||||
if( mCanProcessAudio )
|
||||
{
|
||||
if( packet.getType() == Type.AUDIO )
|
||||
{
|
||||
ByteBuffer buffer = convert(
|
||||
packet.getAudioBuffer().getSamples() );
|
||||
|
||||
mOutput.write( buffer.array(), 0,
|
||||
buffer.array().length );
|
||||
|
||||
checkStart();
|
||||
|
||||
broadcast( packet.getAudioMetadata() );
|
||||
|
||||
mLastActivity = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
checkStop();
|
||||
}
|
||||
|
||||
mProcessing.set( false );
|
||||
shortBuffer.put( (short)( sample * Short.MAX_VALUE ) );
|
||||
shortBuffer.put( (short)0 );
|
||||
}
|
||||
}
|
||||
catch( Exception e )
|
||||
else
|
||||
{
|
||||
mLog.error( "Error while processing audio buffers", e );
|
||||
for( float sample: samples )
|
||||
{
|
||||
shortBuffer.put( (short)0 );
|
||||
shortBuffer.put( (short)( sample * Short.MAX_VALUE ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkStart()
|
||||
{
|
||||
if( mCanProcessAudio &&
|
||||
!mOutput.isRunning() && mOutput.available() <= START_THRESHOLD )
|
||||
{
|
||||
mOutput.start();
|
||||
}
|
||||
}
|
||||
|
||||
private void checkStop()
|
||||
{
|
||||
if( mCanProcessAudio && mOutput.isRunning() &&
|
||||
mOutput.available() >= STOP_THRESHOLD )
|
||||
{
|
||||
mOutput.drain();
|
||||
mOutput.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMuted( boolean muted )
|
||||
{
|
||||
if( mMuteControl != null )
|
||||
{
|
||||
mMuteControl.setValue( muted );
|
||||
|
||||
broadcast( new AudioEvent( muted ? AudioEvent.Type.AUDIO_MUTED :
|
||||
AudioEvent.Type.AUDIO_UNMUTED, getChannelName() ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMuted()
|
||||
{
|
||||
if( mMuteControl != null )
|
||||
{
|
||||
return mMuteControl.getValue();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FloatControl getGainControl()
|
||||
{
|
||||
return mGainControl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasGainControl()
|
||||
{
|
||||
return mGainControl != null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update( LineEvent event )
|
||||
{
|
||||
LineEvent.Type type = event.getType();
|
||||
|
||||
if( type == LineEvent.Type.START )
|
||||
{
|
||||
mAudioEventBroadcaster.broadcast( mAudioStartEvent );
|
||||
}
|
||||
else if( type == LineEvent.Type.STOP )
|
||||
{
|
||||
mAudioEventBroadcaster.broadcast( mAudioStopEvent );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,6 @@ public class P25_C4FMDecoder extends P25Decoder implements IRealBufferListener
|
||||
/* Filter demodulated sample buffers */
|
||||
float[] filter = FilterFactory.getLowPass( 48000, 2500, 4000, 80, WindowType.HANNING, true );
|
||||
|
||||
mLog.debug( "Demod Filter tap count:" + filter.length + " coefficients:" + Arrays.toString( filter ) );
|
||||
mC4FMPreFilter = new RealFIRFilter_RB_RB( filter, 1.0f );
|
||||
|
||||
/* Issue tuned frequency correction commands, remotely controlled by the
|
||||
|
||||
@@ -18,4 +18,19 @@ public class TDUMessage extends P25Message
|
||||
mCRC = new CRC[ 1 ];
|
||||
mCRC[ 0 ] = CRC.PASSED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append( "NAC:" );
|
||||
sb.append( getNAC() );
|
||||
sb.append( " " );
|
||||
sb.append( getDUID().getLabel() );
|
||||
|
||||
sb.append( " TERMINATOR DATA UNIT" );
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ import java.awt.Component;
|
||||
import java.awt.Point;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JPanel;
|
||||
@@ -53,8 +54,8 @@ public class ChannelList extends JPanel implements ChannelEventListener
|
||||
|
||||
private final static Logger mLog = LoggerFactory.getLogger( ChannelList.class );
|
||||
|
||||
private HashMap<Channel,ChannelCollectionPanel> mDisplayedPanels =
|
||||
new HashMap<Channel,ChannelCollectionPanel>();
|
||||
private Map<Channel,ChannelCollectionPanel> mDisplayedPanels =
|
||||
new ConcurrentHashMap<Channel,ChannelCollectionPanel>();
|
||||
|
||||
private PlaylistManager mPlaylistManager;
|
||||
private SettingsManager mSettingsManager;
|
||||
|
||||
@@ -383,6 +383,8 @@ public class ChannelState extends Module implements ICallEventProvider,
|
||||
{
|
||||
broadcast( SquelchState.SQUELCH );
|
||||
|
||||
broadcast( new MetadataReset() );
|
||||
|
||||
mState = State.TEARDOWN;
|
||||
|
||||
broadcast( ChangedAttribute.CHANNEL_STATE );
|
||||
|
||||
@@ -202,7 +202,7 @@ public class ChannelStatePanel extends JPanel
|
||||
switch( changedAttribute )
|
||||
{
|
||||
case CHANNEL_STATE:
|
||||
if( state.getState() != null )
|
||||
if( state != null && state.getState() != null )
|
||||
{
|
||||
mStateLabel.setText( state.getState().getDisplayValue() );
|
||||
}
|
||||
|
||||
@@ -91,7 +91,6 @@ public class TrafficChannelManager extends Module
|
||||
@Override
|
||||
public void dispose()
|
||||
{
|
||||
mLog.debug( "dispose() on traffic channel manager" );
|
||||
for( Channel trafficChannel: mTrafficChannelPool )
|
||||
{
|
||||
trafficChannel.setEnabled( false );
|
||||
|
||||
@@ -557,11 +557,6 @@ public class SettingsManager
|
||||
ArrayList<TunerConfiguration> configs = getSettings()
|
||||
.getTunerConfigurations( type );
|
||||
|
||||
if( configs.isEmpty() )
|
||||
{
|
||||
configs.add( addNewTunerConfiguration( type, "Default" ) );
|
||||
}
|
||||
|
||||
return configs;
|
||||
}
|
||||
|
||||
|
||||
@@ -130,8 +130,11 @@ public class TunerChannelSource extends ComplexSource
|
||||
mTaskHandle = null;
|
||||
}
|
||||
|
||||
mBuffer.clear();
|
||||
mBuffer = null;
|
||||
synchronized( mBuffer )
|
||||
{
|
||||
mBuffer.clear();
|
||||
mBuffer = null;
|
||||
}
|
||||
|
||||
mFrequencyChangeListener = null;
|
||||
mListener = null;
|
||||
@@ -155,7 +158,13 @@ public class TunerChannelSource extends ComplexSource
|
||||
@Override
|
||||
public void receive( ComplexBuffer buffer )
|
||||
{
|
||||
mBuffer.add( buffer );
|
||||
synchronized( mBuffer )
|
||||
{
|
||||
if( mBuffer != null )
|
||||
{
|
||||
mBuffer.add( buffer );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setFrequencyChangeListener( FrequencyChangeListener listener )
|
||||
|
||||
@@ -36,6 +36,7 @@ public enum TunerClass
|
||||
GIGABYTE_GTU7300( TunerType.FITIPOWER_FC0012, "1B80", "D393", "Gigabyte", "GT-U7300" ),
|
||||
GTEK_T803( TunerType.FITIPOWER_FC0012, "1F4D", "B803", "GTek", "T803" ),
|
||||
HACKRF_ONE( TunerType.HACKRF, "1D50", "6089", "Great Scott Gadgets", "HackRF One" ),
|
||||
RAD1O( TunerType.HACKRF, "1D50", "CC15", "Munich hackerspace", "Rad1o" ),
|
||||
LIFEVIEW_LV5T_DELUXE( TunerType.FITIPOWER_FC0012, "1F4D", "C803", "Liveview", "LV5T Deluxe" ),
|
||||
MYGICA_TD312( TunerType.FITIPOWER_FC0012, "1F4D", "D286", "MyGica", "TD312" ),
|
||||
PEAK_102569AGPK( TunerType.FITIPOWER_FC0012, "1B80", "D395", "Peak", "102569AGPK" ),
|
||||
@@ -185,6 +186,10 @@ public enum TunerClass
|
||||
{
|
||||
retVal = AIRSPY;
|
||||
}
|
||||
else if( productID == 52245 ) //CC15
|
||||
{
|
||||
retVal = HACKRF_ONE;
|
||||
}
|
||||
break;
|
||||
case 8013: //1F4D
|
||||
if( productID == 47107 ) //B803
|
||||
|
||||
@@ -218,6 +218,7 @@ public class TunerManager
|
||||
case FUNCUBE_DONGLE_PRO_PLUS:
|
||||
return initFuncubeProPlusTuner( device, descriptor );
|
||||
case HACKRF_ONE:
|
||||
case RAD1O:
|
||||
return initHackRFTuner( device, descriptor );
|
||||
case COMPRO_VIDEOMATE_U620F:
|
||||
case COMPRO_VIDEOMATE_U650F:
|
||||
@@ -544,16 +545,14 @@ public class TunerManager
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
config = configs.get( 0 );
|
||||
}
|
||||
|
||||
//If we're still null at this point, create a default config
|
||||
if( config == null )
|
||||
{
|
||||
String name = "Default-" + tuner.getUniqueID();
|
||||
|
||||
config = mResourceManager.getSettingsManager()
|
||||
.addNewTunerConfiguration( tuner.getTunerType(), "Default" );
|
||||
.addNewTunerConfiguration( tuner.getTunerType(), name );
|
||||
}
|
||||
|
||||
//Persist the config as the selected tuner configuration. The method
|
||||
|
||||
@@ -262,7 +262,30 @@ public class AirspyTunerController extends TunerController
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public static String getTransferStatus( int status )
|
||||
{
|
||||
switch( status )
|
||||
{
|
||||
case 0:
|
||||
return "TRANSFER COMPLETED (0)";
|
||||
case 1:
|
||||
return "TRANSFER ERROR (1)";
|
||||
case 2:
|
||||
return "TRANSFER TIMED OUT (2)";
|
||||
case 3:
|
||||
return "TRANSFER CANCELLED (3)";
|
||||
case 4:
|
||||
return "TRANSFER STALL (4)";
|
||||
case 5:
|
||||
return "TRANSFER NO DEVICE (5)";
|
||||
case 6:
|
||||
return "TRANSFER OVERFLOW (6)";
|
||||
default:
|
||||
return "UNKNOWN TRANSFER STATUS (" + status + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply( TunerConfiguration config ) throws SourceException
|
||||
{
|
||||
if( config instanceof AirspyTunerConfiguration )
|
||||
@@ -1192,18 +1215,21 @@ public class AirspyTunerController extends TunerController
|
||||
*/
|
||||
public void addListener( Listener<ComplexBuffer> listener )
|
||||
{
|
||||
mComplexBufferBroadcaster.addListener( listener );
|
||||
synchronized( mComplexBufferBroadcaster )
|
||||
{
|
||||
mComplexBufferBroadcaster.addListener( listener );
|
||||
|
||||
if( mBufferProcessor == null || !mBufferProcessor.isRunning() )
|
||||
{
|
||||
mBufferProcessor = new BufferProcessor();
|
||||
if( mBufferProcessor == null || !mBufferProcessor.isRunning() )
|
||||
{
|
||||
mBufferProcessor = new BufferProcessor();
|
||||
|
||||
Thread thread = new Thread( mBufferProcessor );
|
||||
thread.setDaemon( true );
|
||||
thread.setName( "Airspy Buffer Processor" );
|
||||
Thread thread = new Thread( mBufferProcessor );
|
||||
thread.setDaemon( true );
|
||||
thread.setName( "Airspy Buffer Processor" );
|
||||
|
||||
thread.start();
|
||||
}
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1212,12 +1238,15 @@ public class AirspyTunerController extends TunerController
|
||||
*/
|
||||
public void removeListener( Listener<ComplexBuffer> listener )
|
||||
{
|
||||
mComplexBufferBroadcaster.removeListener( listener );
|
||||
|
||||
if( !mComplexBufferBroadcaster.hasListeners() )
|
||||
{
|
||||
mBufferProcessor.stop();
|
||||
}
|
||||
synchronized( mComplexBufferBroadcaster )
|
||||
{
|
||||
mComplexBufferBroadcaster.removeListener( listener );
|
||||
|
||||
if( !mComplexBufferBroadcaster.hasListeners() )
|
||||
{
|
||||
mBufferProcessor.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1227,8 +1256,11 @@ public class AirspyTunerController extends TunerController
|
||||
public class BufferProcessor implements Runnable, TransferCallback
|
||||
{
|
||||
private ScheduledFuture<?> mSampleDispatcherTask;
|
||||
private CopyOnWriteArrayList<Transfer> mTransfers;
|
||||
private LinkedTransferQueue<Transfer> mAvailableTransfers;
|
||||
private LinkedTransferQueue<Transfer> mTransfersInProgress = new LinkedTransferQueue<>();
|
||||
private AtomicBoolean mRunning = new AtomicBoolean();
|
||||
private ByteBuffer mLibUsbHandlerStatus;
|
||||
private boolean mCancel = false;
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
@@ -1237,33 +1269,64 @@ public class AirspyTunerController extends TunerController
|
||||
{
|
||||
prepareTransfers();
|
||||
|
||||
for( Transfer transfer: mTransfers )
|
||||
{
|
||||
int result = LibUsb.submitTransfer( transfer );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error submitting transfer [" +
|
||||
LibUsb.errorName( result ) + "]" );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mSampleDispatcherTask = mThreadPoolManager.scheduleFixedRate(
|
||||
ThreadType.SOURCE_SAMPLE_PROCESSING, new BufferDispatcher(),
|
||||
20, TimeUnit.MILLISECONDS );
|
||||
|
||||
mLibUsbHandlerStatus = ByteBuffer.allocateDirect( 4 );
|
||||
|
||||
List<Transfer> transfers = new ArrayList<>();
|
||||
|
||||
mCancel = false;
|
||||
|
||||
while( mRunning.get() )
|
||||
{
|
||||
ByteBuffer completed = ByteBuffer.allocateDirect( 4 );
|
||||
|
||||
mAvailableTransfers.drainTo( transfers );
|
||||
|
||||
for( Transfer transfer: transfers )
|
||||
{
|
||||
int result = LibUsb.submitTransfer( transfer );
|
||||
|
||||
if( result == LibUsb.SUCCESS )
|
||||
{
|
||||
mTransfersInProgress.add( transfer );
|
||||
}
|
||||
else
|
||||
{
|
||||
mLog.error( "error submitting transfer [" +
|
||||
LibUsb.errorName( result ) + "]" );
|
||||
}
|
||||
}
|
||||
|
||||
int result = LibUsb.handleEventsTimeoutCompleted(
|
||||
null, USB_TIMEOUT_MS, completed.asIntBuffer() );
|
||||
null, USB_TIMEOUT_MS, mLibUsbHandlerStatus.asIntBuffer() );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error handling events for libusb" );
|
||||
}
|
||||
|
||||
transfers.clear();
|
||||
|
||||
mLibUsbHandlerStatus.rewind();
|
||||
}
|
||||
|
||||
if( mCancel )
|
||||
{
|
||||
for( Transfer transfer: mTransfersInProgress )
|
||||
{
|
||||
LibUsb.cancelTransfer( transfer );
|
||||
}
|
||||
|
||||
int result = LibUsb.handleEventsTimeoutCompleted( null,
|
||||
USB_TIMEOUT_MS, mLibUsbHandlerStatus.asIntBuffer() );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error handling events for libusb" );
|
||||
}
|
||||
|
||||
mLibUsbHandlerStatus.rewind();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1273,10 +1336,10 @@ public class AirspyTunerController extends TunerController
|
||||
*/
|
||||
public void stop()
|
||||
{
|
||||
mCancel = true;
|
||||
|
||||
if( mRunning.compareAndSet( true, false ) )
|
||||
{
|
||||
cancel();
|
||||
|
||||
if( mSampleDispatcherTask != null )
|
||||
{
|
||||
mSampleDispatcherTask.cancel( true );
|
||||
@@ -1296,66 +1359,39 @@ public class AirspyTunerController extends TunerController
|
||||
@Override
|
||||
public void processTransfer( Transfer transfer )
|
||||
{
|
||||
if( transfer.status() == LibUsb.TRANSFER_COMPLETED )
|
||||
{
|
||||
ByteBuffer buffer = transfer.buffer();
|
||||
|
||||
byte[] data = new byte[ transfer.actualLength() ];
|
||||
|
||||
buffer.get( data );
|
||||
|
||||
buffer.rewind();
|
||||
|
||||
mFilledBuffers.add( data );
|
||||
|
||||
if( !isRunning() )
|
||||
{
|
||||
LibUsb.cancelTransfer( transfer );
|
||||
}
|
||||
}
|
||||
mTransfersInProgress.remove( transfer );
|
||||
|
||||
switch( transfer.status() )
|
||||
{
|
||||
case LibUsb.TRANSFER_COMPLETED:
|
||||
/* resubmit the transfer */
|
||||
int result = LibUsb.submitTransfer( transfer );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
case LibUsb.TRANSFER_STALL:
|
||||
if( transfer.actualLength() > 0 )
|
||||
{
|
||||
mLog.error( "couldn't resubmit buffer transfer to tuner" );
|
||||
LibUsb.freeTransfer( transfer );
|
||||
mTransfers.remove( transfer );
|
||||
ByteBuffer buffer = transfer.buffer();
|
||||
|
||||
byte[] data = new byte[ transfer.actualLength() ];
|
||||
|
||||
buffer.get( data );
|
||||
|
||||
buffer.rewind();
|
||||
|
||||
if( isRunning() )
|
||||
{
|
||||
mFilledBuffers.add( data );
|
||||
}
|
||||
}
|
||||
break;
|
||||
case LibUsb.TRANSFER_CANCELLED:
|
||||
/* free the transfer and remove it */
|
||||
LibUsb.freeTransfer( transfer );
|
||||
mTransfers.remove( transfer );
|
||||
break;
|
||||
default:
|
||||
/* unexpected error */
|
||||
mLog.error( "transfer error" );
|
||||
mLog.error( "transfer error [" +
|
||||
getTransferStatus( transfer.status() ) +
|
||||
"] transferred actual: " + transfer.actualLength() );
|
||||
}
|
||||
}
|
||||
|
||||
private void cancel()
|
||||
{
|
||||
for( Transfer transfer: mTransfers )
|
||||
{
|
||||
LibUsb.cancelTransfer( transfer );
|
||||
}
|
||||
|
||||
ByteBuffer completed = ByteBuffer.allocateDirect( 4 );
|
||||
|
||||
int result = LibUsb.handleEventsTimeoutCompleted(
|
||||
null, USB_TIMEOUT_MS, completed.asIntBuffer() );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error handling usb events during cancel [" +
|
||||
LibUsb.errorName( result ) + "]" );
|
||||
}
|
||||
}
|
||||
mAvailableTransfers.add( transfer );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares (allocates) a set of transfer buffers for use in
|
||||
@@ -1363,30 +1399,28 @@ public class AirspyTunerController extends TunerController
|
||||
*/
|
||||
private void prepareTransfers() throws LibUsbException
|
||||
{
|
||||
mTransfers = new CopyOnWriteArrayList<Transfer>();
|
||||
|
||||
for( int x = 0; x < TRANSFER_BUFFER_POOL_SIZE; x++ )
|
||||
if( mAvailableTransfers == null )
|
||||
{
|
||||
Transfer transfer = LibUsb.allocTransfer();
|
||||
mAvailableTransfers = new LinkedTransferQueue<>();
|
||||
|
||||
for( int x = 0; x < TRANSFER_BUFFER_POOL_SIZE; x++ )
|
||||
{
|
||||
Transfer transfer = LibUsb.allocTransfer();
|
||||
|
||||
if( transfer == null )
|
||||
{
|
||||
throw new LibUsbException( "couldn't allocate transfer",
|
||||
LibUsb.ERROR_NO_MEM );
|
||||
}
|
||||
|
||||
final ByteBuffer buffer =
|
||||
ByteBuffer.allocateDirect( mBufferSize );
|
||||
|
||||
LibUsb.fillBulkTransfer( transfer,
|
||||
mDeviceHandle,
|
||||
USB_ENDPOINT,
|
||||
buffer,
|
||||
BufferProcessor.this,
|
||||
"Buffer #" + x,
|
||||
USB_TIMEOUT_MS );
|
||||
|
||||
mTransfers.add( transfer );
|
||||
if( transfer == null )
|
||||
{
|
||||
throw new LibUsbException( "couldn't allocate transfer",
|
||||
LibUsb.ERROR_NO_MEM );
|
||||
}
|
||||
|
||||
final ByteBuffer buffer =
|
||||
ByteBuffer.allocateDirect( mBufferSize );
|
||||
|
||||
LibUsb.fillBulkTransfer( transfer, mDeviceHandle, USB_ENDPOINT,
|
||||
buffer, BufferProcessor.this, "Buffer", USB_TIMEOUT_MS );
|
||||
|
||||
mAvailableTransfers.add( transfer );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.LinkedTransferQueue;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
@@ -91,7 +92,7 @@ public class HackRFTunerController extends TunerController
|
||||
private final static Logger mLog =
|
||||
LoggerFactory.getLogger( HackRFTunerController.class );
|
||||
|
||||
public final static long TIMEOUT_US = 1000000l; //uSeconds
|
||||
public final static long USB_TIMEOUT_MS = 1000000l; //uSeconds
|
||||
public static final byte USB_ENDPOINT = (byte)0x81;
|
||||
public static final byte USB_INTERFACE = (byte)0x0;
|
||||
|
||||
@@ -394,7 +395,7 @@ public class HackRFTunerController extends TunerController
|
||||
(short)value,
|
||||
(short)index,
|
||||
buffer,
|
||||
TIMEOUT_US );
|
||||
USB_TIMEOUT_MS );
|
||||
|
||||
if( transferred < 0 )
|
||||
{
|
||||
@@ -467,7 +468,7 @@ public class HackRFTunerController extends TunerController
|
||||
(short)value,
|
||||
(short)index,
|
||||
buffer,
|
||||
TIMEOUT_US );
|
||||
USB_TIMEOUT_MS );
|
||||
|
||||
if( transferred < 0 )
|
||||
{
|
||||
@@ -886,18 +887,21 @@ public class HackRFTunerController extends TunerController
|
||||
*/
|
||||
public void addListener( Listener<ComplexBuffer> listener )
|
||||
{
|
||||
mComplexBufferBroadcaster.addListener( listener );
|
||||
synchronized( mComplexBufferBroadcaster )
|
||||
{
|
||||
mComplexBufferBroadcaster.addListener( listener );
|
||||
|
||||
if( mBufferProcessor == null || !mBufferProcessor.isRunning() )
|
||||
{
|
||||
mBufferProcessor = new BufferProcessor();
|
||||
if( mBufferProcessor == null || !mBufferProcessor.isRunning() )
|
||||
{
|
||||
mBufferProcessor = new BufferProcessor();
|
||||
|
||||
Thread thread = new Thread( mBufferProcessor );
|
||||
thread.setDaemon( true );
|
||||
thread.setName( "HackRF Sample Processor" );
|
||||
Thread thread = new Thread( mBufferProcessor );
|
||||
thread.setDaemon( true );
|
||||
thread.setName( "HackRF Sample Processor" );
|
||||
|
||||
thread.start();
|
||||
}
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -906,12 +910,15 @@ public class HackRFTunerController extends TunerController
|
||||
*/
|
||||
public void removeListener( Listener<ComplexBuffer> listener )
|
||||
{
|
||||
mComplexBufferBroadcaster.removeListener( listener );
|
||||
|
||||
if( !mComplexBufferBroadcaster.hasListeners() )
|
||||
{
|
||||
mBufferProcessor.stop();
|
||||
}
|
||||
synchronized( mComplexBufferBroadcaster )
|
||||
{
|
||||
mComplexBufferBroadcaster.removeListener( listener );
|
||||
|
||||
if( !mComplexBufferBroadcaster.hasListeners() )
|
||||
{
|
||||
mBufferProcessor.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -921,45 +928,77 @@ public class HackRFTunerController extends TunerController
|
||||
public class BufferProcessor implements Runnable, TransferCallback
|
||||
{
|
||||
private ScheduledFuture<?> mSampleDispatcherTask;
|
||||
private CopyOnWriteArrayList<Transfer> mTransfers;
|
||||
private LinkedTransferQueue<Transfer> mAvailableTransfers;
|
||||
private LinkedTransferQueue<Transfer> mTransfersInProgress = new LinkedTransferQueue<>();
|
||||
private AtomicBoolean mRunning = new AtomicBoolean();
|
||||
private ByteBuffer mLibUsbHandlerStatus;
|
||||
private boolean mCancel = false;
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
if( mRunning.compareAndSet( false, true ) )
|
||||
{
|
||||
mLog.info( "starting HackRF sample fetch thread" );
|
||||
|
||||
prepareTransfers();
|
||||
|
||||
for( Transfer transfer: mTransfers )
|
||||
{
|
||||
int result = LibUsb.submitTransfer( transfer );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error submitting transfer [" +
|
||||
LibUsb.errorName( result ) + "]" );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mSampleDispatcherTask = mThreadPoolManager.scheduleFixedRate(
|
||||
ThreadType.SOURCE_SAMPLE_PROCESSING, new BufferDispatcher(),
|
||||
20, TimeUnit.MILLISECONDS );
|
||||
|
||||
mLibUsbHandlerStatus = ByteBuffer.allocateDirect( 4 );
|
||||
|
||||
List<Transfer> transfers = new ArrayList<>();
|
||||
|
||||
mCancel = false;
|
||||
|
||||
while( mRunning.get() )
|
||||
{
|
||||
ByteBuffer completed = ByteBuffer.allocateDirect( 4 );
|
||||
mAvailableTransfers.drainTo( transfers );
|
||||
|
||||
for( Transfer transfer: transfers )
|
||||
{
|
||||
int result = LibUsb.submitTransfer( transfer );
|
||||
|
||||
if( result == LibUsb.SUCCESS )
|
||||
{
|
||||
mTransfersInProgress.add( transfer );
|
||||
}
|
||||
else
|
||||
{
|
||||
mLog.error( "error submitting transfer [" +
|
||||
LibUsb.errorName( result ) + "]" );
|
||||
}
|
||||
}
|
||||
|
||||
int result = LibUsb.handleEventsTimeoutCompleted(
|
||||
null, TIMEOUT_US, completed.asIntBuffer() );
|
||||
null, USB_TIMEOUT_MS, mLibUsbHandlerStatus.asIntBuffer() );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error handling events for libusb" );
|
||||
}
|
||||
|
||||
transfers.clear();
|
||||
|
||||
mLibUsbHandlerStatus.rewind();
|
||||
}
|
||||
|
||||
if( mCancel )
|
||||
{
|
||||
for( Transfer transfer: mTransfersInProgress )
|
||||
{
|
||||
LibUsb.cancelTransfer( transfer );
|
||||
}
|
||||
|
||||
int result = LibUsb.handleEventsTimeoutCompleted(
|
||||
null, USB_TIMEOUT_MS, mLibUsbHandlerStatus.asIntBuffer() );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error cancelling transfers in progress" );
|
||||
}
|
||||
|
||||
mLibUsbHandlerStatus.rewind();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -969,12 +1008,10 @@ public class HackRFTunerController extends TunerController
|
||||
*/
|
||||
public void stop()
|
||||
{
|
||||
mCancel = true;
|
||||
|
||||
if( mRunning.compareAndSet( true, false ) )
|
||||
{
|
||||
mLog.info( "stopping HackRF sample fetch thread" );
|
||||
|
||||
cancel();
|
||||
|
||||
if( mSampleDispatcherTask != null )
|
||||
{
|
||||
mSampleDispatcherTask.cancel( true );
|
||||
@@ -994,41 +1031,29 @@ public class HackRFTunerController extends TunerController
|
||||
@Override
|
||||
public void processTransfer( Transfer transfer )
|
||||
{
|
||||
if( transfer.status() == LibUsb.TRANSFER_COMPLETED )
|
||||
{
|
||||
ByteBuffer buffer = transfer.buffer();
|
||||
|
||||
byte[] data = new byte[ transfer.actualLength() ];
|
||||
|
||||
buffer.get( data );
|
||||
|
||||
buffer.rewind();
|
||||
|
||||
mFilledBuffers.add( data );
|
||||
|
||||
if( !isRunning() )
|
||||
{
|
||||
LibUsb.cancelTransfer( transfer );
|
||||
}
|
||||
}
|
||||
mTransfersInProgress.remove( transfer );
|
||||
|
||||
switch( transfer.status() )
|
||||
{
|
||||
case LibUsb.TRANSFER_COMPLETED:
|
||||
/* resubmit the transfer */
|
||||
int result = LibUsb.submitTransfer( transfer );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
case LibUsb.TRANSFER_STALL:
|
||||
if( transfer.actualLength() > 0 )
|
||||
{
|
||||
mLog.error( "couldn't resubmit buffer transfer to tuner" );
|
||||
LibUsb.freeTransfer( transfer );
|
||||
mTransfers.remove( transfer );
|
||||
ByteBuffer buffer = transfer.buffer();
|
||||
|
||||
byte[] data = new byte[ transfer.actualLength() ];
|
||||
|
||||
buffer.get( data );
|
||||
|
||||
buffer.rewind();
|
||||
|
||||
if( isRunning() )
|
||||
{
|
||||
mFilledBuffers.add( data );
|
||||
}
|
||||
}
|
||||
break;
|
||||
case LibUsb.TRANSFER_CANCELLED:
|
||||
/* free the transfer and remove it */
|
||||
LibUsb.freeTransfer( transfer );
|
||||
mTransfers.remove( transfer );
|
||||
break;
|
||||
default:
|
||||
/* unexpected error */
|
||||
@@ -1036,26 +1061,9 @@ public class HackRFTunerController extends TunerController
|
||||
getTransferStatus( transfer.status() ) +
|
||||
"] transferred actual: " + transfer.actualLength() );
|
||||
}
|
||||
}
|
||||
|
||||
private void cancel()
|
||||
{
|
||||
for( Transfer transfer: mTransfers )
|
||||
{
|
||||
LibUsb.cancelTransfer( transfer );
|
||||
}
|
||||
|
||||
ByteBuffer completed = ByteBuffer.allocateDirect( 4 );
|
||||
|
||||
int result = LibUsb.handleEventsTimeoutCompleted(
|
||||
null, TIMEOUT_US, completed.asIntBuffer() );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error handling usb events during cancel [" +
|
||||
LibUsb.errorName( result ) + "]" );
|
||||
}
|
||||
}
|
||||
mAvailableTransfers.add( transfer );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares (allocates) a set of transfer buffers for use in
|
||||
@@ -1063,30 +1071,28 @@ public class HackRFTunerController extends TunerController
|
||||
*/
|
||||
private void prepareTransfers() throws LibUsbException
|
||||
{
|
||||
mTransfers = new CopyOnWriteArrayList<Transfer>();
|
||||
|
||||
for( int x = 0; x < TRANSFER_BUFFER_POOL_SIZE; x++ )
|
||||
if( mAvailableTransfers == null )
|
||||
{
|
||||
Transfer transfer = LibUsb.allocTransfer();
|
||||
mAvailableTransfers = new LinkedTransferQueue<>();
|
||||
|
||||
for( int x = 0; x < TRANSFER_BUFFER_POOL_SIZE; x++ )
|
||||
{
|
||||
Transfer transfer = LibUsb.allocTransfer();
|
||||
|
||||
if( transfer == null )
|
||||
{
|
||||
throw new LibUsbException( "couldn't allocate transfer",
|
||||
LibUsb.ERROR_NO_MEM );
|
||||
}
|
||||
|
||||
final ByteBuffer buffer =
|
||||
ByteBuffer.allocateDirect( mBufferSize );
|
||||
|
||||
LibUsb.fillBulkTransfer( transfer,
|
||||
mDeviceHandle,
|
||||
USB_ENDPOINT,
|
||||
buffer,
|
||||
BufferProcessor.this,
|
||||
"Buffer #" + x,
|
||||
TIMEOUT_US );
|
||||
|
||||
mTransfers.add( transfer );
|
||||
if( transfer == null )
|
||||
{
|
||||
throw new LibUsbException( "couldn't allocate transfer",
|
||||
LibUsb.ERROR_NO_MEM );
|
||||
}
|
||||
|
||||
final ByteBuffer buffer =
|
||||
ByteBuffer.allocateDirect( mBufferSize );
|
||||
|
||||
LibUsb.fillBulkTransfer( transfer, mDeviceHandle,USB_ENDPOINT,
|
||||
buffer, BufferProcessor.this, "Buffer", USB_TIMEOUT_MS );
|
||||
|
||||
mAvailableTransfers.add( transfer );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ import java.nio.charset.Charset;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.LinkedTransferQueue;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -55,8 +55,6 @@ import sample.complex.ComplexBuffer;
|
||||
import source.SourceException;
|
||||
import source.tuner.TunerController;
|
||||
import source.tuner.TunerType;
|
||||
import source.tuner.frequency.FrequencyChangeEvent;
|
||||
import source.tuner.frequency.FrequencyChangeEvent.Event;
|
||||
import buffer.FloatAveragingBuffer;
|
||||
import controller.ResourceManager;
|
||||
import controller.ThreadPoolManager;
|
||||
@@ -73,7 +71,7 @@ public abstract class RTL2832TunerController extends TunerController
|
||||
public final static int TWO_TO_22_POWER = 4194304;
|
||||
|
||||
public final static byte USB_INTERFACE = (byte)0x0;
|
||||
public final static byte BULK_ENDPOINT_IN = (byte)0x81;
|
||||
public final static byte USB_BULK_ENDPOINT = (byte)0x81;
|
||||
public final static byte CONTROL_ENDPOINT_IN =
|
||||
(byte)( LibUsb.ENDPOINT_IN | LibUsb.REQUEST_TYPE_VENDOR );
|
||||
public final static byte CONTROL_ENDPOINT_OUT =
|
||||
@@ -1342,21 +1340,24 @@ public abstract class RTL2832TunerController extends TunerController
|
||||
*/
|
||||
public void addListener( Listener<ComplexBuffer> listener )
|
||||
{
|
||||
mComplexBufferBroadcaster.addListener( listener );
|
||||
synchronized( mComplexBufferBroadcaster )
|
||||
{
|
||||
mComplexBufferBroadcaster.addListener( listener );
|
||||
|
||||
if( mBufferProcessor == null )
|
||||
{
|
||||
mBufferProcessor = new BufferProcessor( mThreadPoolManager );
|
||||
}
|
||||
|
||||
if( !mBufferProcessor.isRunning() )
|
||||
{
|
||||
Thread thread = new Thread( mBufferProcessor );
|
||||
thread.setDaemon( true );
|
||||
thread.setName( "RTL2832 Sample Processor" );
|
||||
if( mBufferProcessor == null )
|
||||
{
|
||||
mBufferProcessor = new BufferProcessor( mThreadPoolManager );
|
||||
}
|
||||
|
||||
if( !mBufferProcessor.isRunning() )
|
||||
{
|
||||
Thread thread = new Thread( mBufferProcessor );
|
||||
thread.setDaemon( true );
|
||||
thread.setName( "RTL2832 Sample Processor" );
|
||||
|
||||
thread.start();
|
||||
}
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1365,12 +1366,15 @@ public abstract class RTL2832TunerController extends TunerController
|
||||
*/
|
||||
public void removeListener( Listener<ComplexBuffer> listener )
|
||||
{
|
||||
mComplexBufferBroadcaster.removeListener( listener );
|
||||
synchronized( mComplexBufferBroadcaster )
|
||||
{
|
||||
mComplexBufferBroadcaster.removeListener( listener );
|
||||
|
||||
if( !mComplexBufferBroadcaster.hasListeners() )
|
||||
{
|
||||
mBufferProcessor.stop();
|
||||
}
|
||||
if( !mComplexBufferBroadcaster.hasListeners() )
|
||||
{
|
||||
mBufferProcessor.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1380,9 +1384,12 @@ public abstract class RTL2832TunerController extends TunerController
|
||||
public class BufferProcessor implements Runnable, TransferCallback
|
||||
{
|
||||
private ScheduledFuture<?> mSampleDispatcherTask;
|
||||
private CopyOnWriteArrayList<Transfer> mTransfers;
|
||||
private LinkedTransferQueue<Transfer> mAvailableTransfers;
|
||||
private LinkedTransferQueue<Transfer> mTransfersInProgress = new LinkedTransferQueue<>();
|
||||
private AtomicBoolean mRunning = new AtomicBoolean();
|
||||
private ThreadPoolManager mThreadPoolManager;
|
||||
private ByteBuffer mLibUsbHandlerStatus;
|
||||
private boolean mCancel = false;
|
||||
|
||||
public BufferProcessor( ThreadPoolManager manager )
|
||||
{
|
||||
@@ -1409,18 +1416,6 @@ public abstract class RTL2832TunerController extends TunerController
|
||||
|
||||
prepareTransfers();
|
||||
|
||||
for( Transfer transfer: mTransfers )
|
||||
{
|
||||
int result = LibUsb.submitTransfer( transfer );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error submitting transfer [" +
|
||||
LibUsb.errorName( result ) + "]" );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
mSampleDispatcherTask = mThreadPoolManager.scheduleFixedRate(
|
||||
@@ -1431,20 +1426,66 @@ public abstract class RTL2832TunerController extends TunerController
|
||||
{
|
||||
mLog.error( "NPE! = tpm null: " + ( mThreadPoolManager == null ), npe );
|
||||
}
|
||||
|
||||
|
||||
while( mRunning.get() )
|
||||
mLibUsbHandlerStatus = ByteBuffer.allocateDirect( 4 );
|
||||
|
||||
List<Transfer> transfers = new ArrayList<>();
|
||||
|
||||
mCancel = false;
|
||||
|
||||
while( mRunning.get() )
|
||||
{
|
||||
ByteBuffer completed = ByteBuffer.allocateDirect( 4 );
|
||||
|
||||
mAvailableTransfers.drainTo( transfers );
|
||||
|
||||
for( Transfer transfer: transfers )
|
||||
{
|
||||
int result = LibUsb.submitTransfer( transfer );
|
||||
|
||||
if( result == LibUsb.SUCCESS )
|
||||
{
|
||||
mTransfersInProgress.add( transfer );
|
||||
}
|
||||
else
|
||||
{
|
||||
mLog.error( "Error submitting transfer [" +
|
||||
LibUsb.errorName( result ) + "]" );
|
||||
}
|
||||
}
|
||||
|
||||
int result = LibUsb.handleEventsTimeoutCompleted(
|
||||
null, TIMEOUT_US, completed.asIntBuffer() );
|
||||
null, TIMEOUT_US, mLibUsbHandlerStatus.asIntBuffer() );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error handling events for libusb" );
|
||||
}
|
||||
|
||||
transfers.clear();
|
||||
|
||||
mLibUsbHandlerStatus.rewind();
|
||||
}
|
||||
|
||||
if( mCancel )
|
||||
{
|
||||
for( Transfer transfer: mTransfersInProgress )
|
||||
{
|
||||
LibUsb.cancelTransfer( transfer );
|
||||
}
|
||||
|
||||
int result = LibUsb.handleEventsTimeoutCompleted(
|
||||
null, TIMEOUT_US, mLibUsbHandlerStatus.asIntBuffer() );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error handling events for libusb" );
|
||||
}
|
||||
|
||||
mLibUsbHandlerStatus.rewind();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mLog.error( "Couldn't start buffer processor - already running!" );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1453,10 +1494,10 @@ public abstract class RTL2832TunerController extends TunerController
|
||||
*/
|
||||
public void stop()
|
||||
{
|
||||
mCancel = true;
|
||||
|
||||
if( mRunning.compareAndSet( true, false ) )
|
||||
{
|
||||
cancel();
|
||||
|
||||
if( mSampleDispatcherTask != null )
|
||||
{
|
||||
mThreadPoolManager.cancel( mSampleDispatcherTask );
|
||||
@@ -1477,43 +1518,29 @@ public abstract class RTL2832TunerController extends TunerController
|
||||
@Override
|
||||
public void processTransfer( Transfer transfer )
|
||||
{
|
||||
if( transfer.status() == LibUsb.TRANSFER_COMPLETED )
|
||||
{
|
||||
ByteBuffer buffer = transfer.buffer();
|
||||
|
||||
byte[] data = new byte[ transfer.actualLength() ];
|
||||
|
||||
buffer.get( data );
|
||||
|
||||
buffer.rewind();
|
||||
|
||||
mFilledBuffers.add( data );
|
||||
|
||||
mSampleCounter.addAndGet( transfer.actualLength() );
|
||||
|
||||
if( !isRunning() )
|
||||
{
|
||||
LibUsb.cancelTransfer( transfer );
|
||||
}
|
||||
}
|
||||
mTransfersInProgress.remove( transfer );
|
||||
|
||||
switch( transfer.status() )
|
||||
{
|
||||
case LibUsb.TRANSFER_COMPLETED:
|
||||
/* resubmit the transfer */
|
||||
int result = LibUsb.submitTransfer( transfer );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
case LibUsb.TRANSFER_STALL:
|
||||
if( transfer.actualLength() > 0 )
|
||||
{
|
||||
mLog.error( "couldn't resubmit buffer transfer to tuner" );
|
||||
LibUsb.freeTransfer( transfer );
|
||||
mTransfers.remove( transfer );
|
||||
ByteBuffer buffer = transfer.buffer();
|
||||
|
||||
byte[] data = new byte[ transfer.actualLength() ];
|
||||
|
||||
buffer.get( data );
|
||||
|
||||
buffer.rewind();
|
||||
|
||||
if( isRunning() )
|
||||
{
|
||||
mFilledBuffers.add( data );
|
||||
}
|
||||
}
|
||||
break;
|
||||
case LibUsb.TRANSFER_CANCELLED:
|
||||
/* free the transfer and remove it */
|
||||
LibUsb.freeTransfer( transfer );
|
||||
mTransfers.remove( transfer );
|
||||
break;
|
||||
default:
|
||||
/* unexpected error */
|
||||
@@ -1521,26 +1548,9 @@ public abstract class RTL2832TunerController extends TunerController
|
||||
getTransferStatus( transfer.status() ) +
|
||||
"] transferred actual: " + transfer.actualLength() );
|
||||
}
|
||||
}
|
||||
|
||||
private void cancel()
|
||||
{
|
||||
for( Transfer transfer: mTransfers )
|
||||
{
|
||||
LibUsb.cancelTransfer( transfer );
|
||||
}
|
||||
|
||||
ByteBuffer completed = ByteBuffer.allocateDirect( 4 );
|
||||
|
||||
int result = LibUsb.handleEventsTimeoutCompleted(
|
||||
null, TIMEOUT_US, completed.asIntBuffer() );
|
||||
|
||||
if( result != LibUsb.SUCCESS )
|
||||
{
|
||||
mLog.error( "error handling usb events during cancel [" +
|
||||
LibUsb.errorName( result ) + "]" );
|
||||
}
|
||||
}
|
||||
mAvailableTransfers.add( transfer );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares (allocates) a set of transfer buffers for use in
|
||||
@@ -1548,30 +1558,28 @@ public abstract class RTL2832TunerController extends TunerController
|
||||
*/
|
||||
private void prepareTransfers() throws LibUsbException
|
||||
{
|
||||
mTransfers = new CopyOnWriteArrayList<Transfer>();
|
||||
|
||||
for( int x = 0; x < TRANSFER_BUFFER_POOL_SIZE; x++ )
|
||||
if( mAvailableTransfers == null )
|
||||
{
|
||||
Transfer transfer = LibUsb.allocTransfer();
|
||||
mAvailableTransfers = new LinkedTransferQueue<>();
|
||||
|
||||
for( int x = 0; x < TRANSFER_BUFFER_POOL_SIZE; x++ )
|
||||
{
|
||||
Transfer transfer = LibUsb.allocTransfer();
|
||||
|
||||
if( transfer == null )
|
||||
{
|
||||
throw new LibUsbException( "couldn't allocate transfer",
|
||||
LibUsb.ERROR_NO_MEM );
|
||||
}
|
||||
|
||||
final ByteBuffer buffer =
|
||||
ByteBuffer.allocateDirect( mBufferSize );
|
||||
|
||||
LibUsb.fillBulkTransfer( transfer,
|
||||
mDeviceHandle,
|
||||
BULK_ENDPOINT_IN,
|
||||
buffer,
|
||||
BufferProcessor.this,
|
||||
"Buffer #" + x,
|
||||
TIMEOUT_US );
|
||||
|
||||
mTransfers.add( transfer );
|
||||
if( transfer == null )
|
||||
{
|
||||
throw new LibUsbException( "couldn't allocate transfer",
|
||||
LibUsb.ERROR_NO_MEM );
|
||||
}
|
||||
|
||||
final ByteBuffer buffer =
|
||||
ByteBuffer.allocateDirect( mBufferSize );
|
||||
|
||||
LibUsb.fillBulkTransfer( transfer, mDeviceHandle, USB_BULK_ENDPOINT,
|
||||
buffer, BufferProcessor.this, "Buffer", TIMEOUT_US );
|
||||
|
||||
mAvailableTransfers.add( transfer );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ public class R820TTunerConfiguration extends TunerConfiguration
|
||||
private R820TLNAGain mLNAGain = R820TLNAGain.GAIN_222;
|
||||
private R820TVGAGain mVGAGain = R820TVGAGain.GAIN_210;
|
||||
private double mFrequencyCorrection = 0.0d;
|
||||
private SampleRate mSampleRate = SampleRate.RATE_1_920MHZ;
|
||||
private SampleRate mSampleRate = SampleRate.RATE_2_400MHZ;
|
||||
|
||||
/**
|
||||
* Default constructor for JAXB
|
||||
|
||||
@@ -20,6 +20,7 @@ package spectrum;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
@@ -56,8 +57,12 @@ public class DFTProcessor implements Listener<ComplexBuffer>,
|
||||
private CopyOnWriteArrayList<DFTResultsConverter> mListeners =
|
||||
new CopyOnWriteArrayList<DFTResultsConverter>();
|
||||
|
||||
private ArrayBlockingQueue<ComplexBuffer> mQueue =
|
||||
new ArrayBlockingQueue<ComplexBuffer>( 200 );
|
||||
//Fixed size sample queue. We set the size at 12 based on the highest
|
||||
//sample rate tuner (10 MHz) producing 153 frames per second and the lowest
|
||||
//DFT processing frame rate of 12 frames per second. This means that the
|
||||
//maximum size needed is 153 / 12 = 11 frames per DFT processing iteration
|
||||
//so we set it at 12.
|
||||
private BlockingQueue<ComplexBuffer> mQueue = new ArrayBlockingQueue<>( 12 );
|
||||
|
||||
private ScheduledExecutorService mScheduler = Executors
|
||||
.newScheduledThreadPool( 1, new NamingThreadFactory( "spectrum dft" ) );
|
||||
@@ -246,15 +251,7 @@ public class DFTProcessor implements Listener<ComplexBuffer>,
|
||||
@Override
|
||||
public void receive( ComplexBuffer sampleBuffer )
|
||||
{
|
||||
if( !mQueue.offer( sampleBuffer ) )
|
||||
{
|
||||
mLog.error( "DFTProcessor - [" + mSampleType.toString()
|
||||
+ "] queue is full, purging queue, "
|
||||
+ "samples[" + sampleBuffer + "]" );
|
||||
|
||||
mQueue.clear();
|
||||
mQueue.offer( sampleBuffer );
|
||||
}
|
||||
mQueue.offer( sampleBuffer );
|
||||
}
|
||||
|
||||
private void getNextBuffer()
|
||||
|
||||
Reference in New Issue
Block a user