Newer
Older
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jitsi.impl.neomedia;
import java.awt.*;
import java.lang.ref.*;
import java.util.*;
import java.util.List;
import javax.media.*;
import org.jitsi.service.configuration.*;

Lyubomir Marinov
committed
import org.jitsi.service.libjitsi.*;
import org.jitsi.service.neomedia.*;
import org.jitsi.service.neomedia.event.*;
import org.jitsi.util.*;
/**
* Controls media service volume input or output. If a playback volume level
* is set we change it on all current players, as we synchronize volume
* on all players. Implements interface exposed from media service, also
* implements the interface used in the Renderer part of JMF and merges the two
* functionalities to work as one.
*
* @author Damian Minkov
* @author Lyubomir Marinov
*/
public class AbstractVolumeControl
implements VolumeControl,
GainControl
{
/**
* The <tt>Logger</tt> used by the <tt>VolumeControlImpl</tt> class and
* its instances for logging output.
*/
private static final Logger logger
= Logger.getLogger(AbstractVolumeControl.class);
/**
* The minimum volume level accepted by <tt>AbstractVolumeControl</tt>.
*/
protected static final float MIN_VOLUME_LEVEL = 0.0F;
/**
* The minimum volume level expressed in percent accepted by
* <tt>AbstractVolumeControl</tt>.
*/
public static final int MIN_VOLUME_PERCENT = 0;
/**
* The maximum volume level accepted by <tt>AbstractVolumeControl</tt>.
*/
protected static final float MAX_VOLUME_LEVEL = 1.0F;
/**
* The maximum volume level expressed in percent accepted by
* <tt>AbstractVolumeControl</tt>.
*/
public static final int MAX_VOLUME_PERCENT = 200;
/**
* The <tt>VolumeChangeListener</tt>s interested in volume change events
* through the <tt>VolumeControl</tt> interface.
* <p>
* Because the instances of <tt>AbstractVolumeControl</tt> are global at the
* time of this writing and, consequently, they cause the
* <tt>VolumeChangeListener</tt>s to be leaked, the listeners are referenced
* using <tt>WeakReference</tt>s.
* </p>
*/
private final List<WeakReference<VolumeChangeListener>>
volumeChangeListeners
= new ArrayList<WeakReference<VolumeChangeListener>>();
/**
* Listeners interested in volume change inside FMJ/JMF.
*/
private List<GainChangeListener> gainChangeListeners;
/**
* The current volume level.
*/
protected float volumeLevel;
/**
* The power level reference used to compute equivelents between the volume
* power level and the gain in decibels.
*/
private float gainReferenceLevel;
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Current mute state, by default we start unmuted.
*/
private boolean mute = false;
/**
* Current level in db.
*/
private float db;
/**
* The name of the configuration property which specifies the value of the
* volume level of this <tt>AbstractVolumeControl</tt>.
*/
private final String volumeLevelConfigurationPropertyName;
/**
* Creates volume control instance and initializes initial level value
* if stored in the configuration service.
*
* @param volumeLevelConfigurationPropertyName the name of the configuration
* property which specifies the value of the volume level of the new
* instance
*/
public AbstractVolumeControl(
String volumeLevelConfigurationPropertyName)
{
// Initializes default values.
this.volumeLevel = getDefaultVolumeLevel();
this.gainReferenceLevel = getGainReferenceLevel();
this.volumeLevelConfigurationPropertyName
= volumeLevelConfigurationPropertyName;
// Read the initial volume level from the ConfigurationService.
this.loadVolume();
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
}
/**
* Applies the gain specified by <tt>gainControl</tt> to the signal defined
* by the <tt>length</tt> number of samples given in <tt>buffer</tt>
* starting at <tt>offset</tt>.
*
* @param gainControl the <tt>GainControl</tt> which specifies the gain to
* apply
* @param buffer the samples of the signal to apply the gain to
* @param offset the start of the samples of the signal in <tt>buffer</tt>
* @param length the number of samples of the signal given in
* <tt>buffer</tt>
*/
public static void applyGain(
GainControl gainControl,
byte[] buffer, int offset, int length)
{
if (gainControl.getMute())
Arrays.fill(buffer, offset, offset + length, (byte) 0);
else
{
// Assign a maximum of MAX_VOLUME_PERCENT to the volume scale.
float level = gainControl.getLevel() * (MAX_VOLUME_PERCENT / 100);
if (level != 1)
{
for (int i = offset, toIndex = offset + length;
i < toIndex;
i += 2)
{
int i1 = i + 1;
short s = (short) ((buffer[i] & 0xff) | (buffer[i1] << 8));
/* Clip, don't wrap. */
int si = s;
si = (int) (si * level);
if (si > Short.MAX_VALUE)
s = Short.MAX_VALUE;
else if (si < Short.MIN_VALUE)
s = Short.MIN_VALUE;
else
s = (short) si;
buffer[i] = (byte) s;
buffer[i1] = (byte) (s >> 8);
}
}
}
}
/**
* Current volume value.
*
* @return the current volume level.
*
* @see org.jitsi.service.neomedia.VolumeControl
*/
public float getVolume()
{
return volumeLevel;
}
/**
* Get the current gain set for this
* object as a value between 0.0 and 1.0
*
* @return The gain in the level scale (0.0-1.0).
*
* @see javax.media.GainControl
*/
public float getLevel()
{
return volumeLevel;
}
/**
* Returns the minimum allowed volume value.
*
* @return the minimum allowed volume value.
*
* @see org.jitsi.service.neomedia.VolumeControl
*/
public float getMinValue()
{
return MIN_VOLUME_LEVEL;
}
/**
* Returns the maximum allowed volume value.
*
* @return the maximum allowed volume value.
*
* @see org.jitsi.service.neomedia.VolumeControl
*/
public float getMaxValue()
{
return MAX_VOLUME_LEVEL;
}
/**
* Changes volume level.
*
* @param value the new level to set.
* @return the actual level which was set.
*
* @see org.jitsi.service.neomedia.VolumeControl
*/
public float setVolume(float value)
{
return this.setVolumeLevel(value);
}
/**
* Set the gain using a floating point scale
* with values between 0.0 and 1.0.
* 0.0 is silence; 1.0 is the loudest
* useful level that this <code>GainControl</code> supports.
*
* @param level The new gain value specified in the level scale.
* @return The level that was actually set.
*
* @see javax.media.GainControl
*/
public float setLevel(float level)
{
return this.setVolumeLevel(level);
}
/**
* Internal implementation combining setting level from JMF
* and from outside Media Service.
*
* @param value the new value, changed if different from current
* volume settings.
* @return the value that was changed or just the current one if
* the same.
*/
private float setVolumeLevel(float value)
{
if (value < MIN_VOLUME_LEVEL)
value = MIN_VOLUME_LEVEL;
else if (value > MAX_VOLUME_LEVEL)
value = MAX_VOLUME_LEVEL;
if (volumeLevel == value)
return value;
volumeLevel = value;
updateHardwareVolume();
fireVolumeChange();
/*
* Save the current volume level in the ConfigurationService so that we
* can restore it on the next application run.
*/

Lyubomir Marinov
committed
ConfigurationService cfg = LibJitsi.getConfigurationService();
if (cfg != null)
{
cfg.setProperty(
this.volumeLevelConfigurationPropertyName,
String.valueOf(volumeLevel));
}
db = getDbFromPowerRatio(value, this.gainReferenceLevel);
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
fireGainEvents();
return volumeLevel;
}
/**
* Mutes current sound.
*
* @param mute mutes/unmutes.
*/
public void setMute(boolean mute)
{
if (this.mute != mute)
{
this.mute = mute;
fireVolumeChange();
fireGainEvents();
}
}
/**
* Get mute state of sound.
*
* @return mute state of sound.
*/
public boolean getMute()
{
return mute;
}
/**
* Set the gain in decibels.
* Setting the gain to 0.0 (the default) implies that the audio
* signal is neither amplified nor attenuated.
* Positive values amplify the audio signal and negative values attenuate
* the signal.
*
* @param gain The new gain in dB.
* @return The gain that was actually set.
*
* @see javax.media.GainControl
*/
public float setDB(float gain)
{
if(this.db != gain)
{
this.db = gain;
float volumeLevel = getPowerRatioFromDb(gain, gainReferenceLevel);
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
setVolumeLevel(volumeLevel);
}
return this.db;
}
/**
* Get the current gain set for this object in dB.
* @return The gain in dB.
*/
public float getDB()
{
return this.db;
}
/**
* Register for gain change update events.
* A <code>GainChangeEvent</code> is posted when the state
* of the <code>GainControl</code> changes.
*
* @param listener The object to deliver events to.
*/
public void addGainChangeListener(GainChangeListener listener)
{
if(listener != null)
{
if(gainChangeListeners == null)
gainChangeListeners = new ArrayList<GainChangeListener>();
gainChangeListeners.add(listener);
}
}
/**
* Remove interest in gain change update events.
*
* @param listener The object that has been receiving events.
*/
public void removeGainChangeListener(GainChangeListener listener)
{
if(listener != null && gainChangeListeners != null)
gainChangeListeners.remove(listener);
}
/**
* Adds a <tt>VolumeChangeListener</tt> to be informed for any change
* in the volume levels.
*
* @param listener volume change listener.
*/
public void addVolumeChangeListener(VolumeChangeListener listener)
{
synchronized (volumeChangeListeners)
{
Iterator<WeakReference<VolumeChangeListener>> i
= volumeChangeListeners.iterator();
boolean contains = false;
while (i.hasNext())
{
VolumeChangeListener l = i.next().get();
if (l == null)
i.remove();
else if (l.equals(listener))
contains = true;
}
if(!contains)
volumeChangeListeners.add(
new WeakReference<VolumeChangeListener>(listener));
}
}
/**
* Removes a <tt>VolumeChangeListener</tt>.
*
* @param listener the volume change listener to be removed.
*/
public void removeVolumeChangeListener(VolumeChangeListener listener)
{
synchronized (volumeChangeListeners)
{
Iterator<WeakReference<VolumeChangeListener>> i
= volumeChangeListeners.iterator();
while (i.hasNext())
{
VolumeChangeListener l = i.next().get();
if ((l == null) || l.equals(listener))
i.remove();
}
}
}
/**
* Fire a change in volume to interested listeners.
*/
private void fireVolumeChange()
{
List<VolumeChangeListener> ls;
synchronized (volumeChangeListeners)
{
Iterator<WeakReference<VolumeChangeListener>> i
= volumeChangeListeners.iterator();
ls
= new ArrayList<VolumeChangeListener>(
volumeChangeListeners.size());
while (i.hasNext())
{
VolumeChangeListener l = i.next().get();
if (l == null)
i.remove();
else
ls.add(l);
}
}
VolumeChangeEvent changeEvent
= new VolumeChangeEvent(this, volumeLevel, mute);
for(VolumeChangeListener l : ls)
l.volumeChange(changeEvent);
}
/**
* Fire events informing for our current state.
*/
private void fireGainEvents()
{
if(gainChangeListeners != null)
{
GainChangeEvent gainchangeevent
= new GainChangeEvent(this, mute, db, volumeLevel);
for(GainChangeListener gainchangelistener : gainChangeListeners)
gainchangelistener.gainChange(gainchangeevent);
}
}
/**
* Not used.
* @return null
*/
public Component getControlComponent()
{
return null;
}
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/**
* Returns the decibel value for a ratio between a power level required and
* the reference power level.
*
* @param powerLevelRequired The power level wished for the signal
* (corresponds to the mesured power level).
* @param referencePowerLevel The reference power level.
*
* @return The gain in Db.
*/
private static float getDbFromPowerRatio(
float powerLevelRequired,
float referencePowerLevel)
{
float powerRatio = powerLevelRequired / referencePowerLevel;
// Limits the lowest power ratio to be 0.0001.
float minPowerRatio = 0.0001F;
float flooredPowerRatio = Math.max(powerRatio, minPowerRatio);
return (float) (20.0 * Math.log10(flooredPowerRatio));
}
/**
* Returns the mesured power level corresponding to a gain in decibel and
* compared to the reference power level.
*
* @param gainInDb The gain in Db.
* @param referencePowerLevel The reference power level.
*
* @return The power level the signal, which corresponds to the mesured
* power level.
*/
private static float getPowerRatioFromDb(
float gainInDb,
float referencePowerLevel)
{
return (float) Math.pow(10, (gainInDb / 20)) * referencePowerLevel;
}
/**
* Returns the default volume level.
*
* @return The default volume level.
*/
protected static float getDefaultVolumeLevel()
{
return MIN_VOLUME_LEVEL
+ (MAX_VOLUME_LEVEL - MIN_VOLUME_LEVEL)
/ ((MAX_VOLUME_PERCENT - MIN_VOLUME_PERCENT) / 100);
}
/**
* Returns the reference volume level for computing the gain.
*
* @return The reference volume level for computing the gain.
*/
protected static float getGainReferenceLevel()
{
return getDefaultVolumeLevel();
}
/**
* Modifies the hardware microphone sensibility (hardaware amplification).
* This is a void function for AbstractVolumeControl sincei it does not have
* any connection to hardware volume. But, this function must be redefined
* by any extending class.
*/
protected void updateHardwareVolume()
{
// Nothing to do. This AbstractVolumeControl only modifies the gain.
}
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/**
* Reads the initial volume level from the system.
*/
protected void loadVolume()
{
try
{
ConfigurationService cfg = LibJitsi.getConfigurationService();
if (cfg != null)
{
String volumeLevelString
= cfg.getString(this.volumeLevelConfigurationPropertyName);
if (volumeLevelString != null)
{
this.volumeLevel = Float.parseFloat(volumeLevelString);
if(logger.isDebugEnabled())
{
logger.debug("Restored volume: " + volumeLevelString);
}
}
}
}
catch (Throwable t)
{
logger.warn("Error restoring volume", t);
}
}