7
7
import us .ihmc .perception .RawImage ;
8
8
9
9
import java .util .ArrayList ;
10
- import java .util .Collection ;
11
10
import java .util .HashMap ;
12
11
import java .util .List ;
13
12
import java .util .Map ;
13
+ import java .util .concurrent .BlockingQueue ;
14
14
15
15
public abstract class ImageSensor implements AutoCloseable
16
16
{
@@ -22,7 +22,7 @@ public abstract class ImageSensor implements AutoCloseable
22
22
23
23
private final RepeatingTaskThread grabThread ;
24
24
private final Object grabNotification = new Object ();
25
- private final Map <Integer , List <Collection <RawImage >>> imageCollectors = new HashMap <>();
25
+ private final Map <Integer , List <BlockingQueue <RawImage >>> imageQueues = new HashMap <>();
26
26
27
27
public ImageSensor (String sensorName )
28
28
{
@@ -105,24 +105,25 @@ public ReferenceFrame getSensorFrame()
105
105
public abstract ReferenceFrame [] getImageFrames ();
106
106
107
107
/**
108
- * Register an image collector for images of a particular key.
108
+ * Register an image queue for images of a particular key.
109
109
* <p>
110
- * Every image grabbed by the sensor will be added to the passed in collection .
110
+ * Every image grabbed by the sensor will be added to the passed in queue .
111
111
* The code accessing the collection must call {@link RawImage#release()} on each image,
112
112
* once it's done using the image.
113
113
* <p>
114
- * Although any class that implements {@link Collection} can be passed in, the implementation of
115
- * the {@link Collection#add(Object)} method should not take a significant amount of time.
114
+ * If the queue becomes full (i.e. when {@code BlockingQueue#remainingCapacity() == 0})
115
+ * the oldest image will be removed so the new image can be added.
116
+ * Ensure a reasonable queue capacity is set to prevent memory leaks.
116
117
*
117
- * @param imageCollector Collection into which the images will be added.
118
+ * @param imageQueue Blocking queue into which the images will be added.
118
119
* @param imageKey The key for images to be collected.
119
120
*/
120
- public void registerImageCollector (Collection <RawImage > imageCollector , int imageKey )
121
+ public void registerImageCollector (BlockingQueue <RawImage > imageQueue , int imageKey )
121
122
{
122
- if (imageCollectors .containsKey (imageKey ))
123
- imageCollectors .get (imageKey ).add (imageCollector );
123
+ if (imageQueues .containsKey (imageKey ))
124
+ imageQueues .get (imageKey ).add (imageQueue );
124
125
else
125
- imageCollectors .put (imageKey , new ArrayList <>(List .of (imageCollector )));
126
+ imageQueues .put (imageKey , new ArrayList <>(List .of (imageQueue )));
126
127
}
127
128
128
129
public String getSensorName ()
@@ -189,8 +190,16 @@ private void grabAndNotify()
189
190
190
191
for (int imageKey : getImageKeys ())
191
192
{
192
- if (imageCollectors .containsKey (imageKey ))
193
- imageCollectors .get (imageKey ).forEach (collector -> collector .add (getImage (imageKey )));
193
+ if (imageQueues .containsKey (imageKey ))
194
+ {
195
+ imageQueues .get (imageKey ).forEach (queue ->
196
+ {
197
+ if (queue .remainingCapacity () == 0 )
198
+ queue .remove ().release ();
199
+
200
+ queue .add (getImage (imageKey ));
201
+ });
202
+ }
194
203
}
195
204
}
196
205
}
0 commit comments