diff --git a/src/org/jitsi/impl/neomedia/device/DeviceConfiguration.java b/src/org/jitsi/impl/neomedia/device/DeviceConfiguration.java index 14844f552449ca8fb6d6da276ee6103d4c52fe96..85a67b23c3898c22a25202c7db7097a91f5bef29 100644 --- a/src/org/jitsi/impl/neomedia/device/DeviceConfiguration.java +++ b/src/org/jitsi/impl/neomedia/device/DeviceConfiguration.java @@ -1012,52 +1012,53 @@ public void setVideoSize(Dimension videoSize) * we reset local values so next time we will update from * the configuration. * - * @param event the property change event + * @param ev the property change event */ - public void propertyChange(PropertyChangeEvent event) + public void propertyChange(PropertyChangeEvent ev) { - String propertyName = event.getPropertyName(); + String propertyName = ev.getPropertyName(); if (AUDIO_CAPTURE_DEVICE.equals(propertyName) || AUDIO_NOTIFY_DEVICE.equals(propertyName) || AUDIO_PLAYBACK_DEVICE.equals(propertyName)) { + CaptureDeviceInfo oldValue = (CaptureDeviceInfo) ev.getOldValue(); + CaptureDeviceInfo newValue = (CaptureDeviceInfo) ev.getNewValue(); + // Try to switch to a new active audio system if we are currently // using the "none" system. - switchFromNoneToActiveAudioSystem( - (CaptureDeviceInfo) event.getNewValue()); + switchFromNoneToActiveAudioSystem(newValue); - CaptureDeviceInfo deviceInfo - = (CaptureDeviceInfo) event.getOldValue(); - if(deviceInfo == null) - { - deviceInfo = (CaptureDeviceInfo) event.getNewValue(); - } + CaptureDeviceInfo device = (oldValue == null) ? newValue : oldValue; - // Fire an event on the selected device, only if the event is + // Fire an event on the selected device only if the event is // generated by the selected audio system. - if(deviceInfo == null - || deviceInfo.getLocator().getProtocol().equals( - getAudioSystem().getLocatorProtocol())) + if((device == null) + || device.getLocator().getProtocol().equals( + getAudioSystem().getLocatorProtocol())) { - firePropertyChange( - propertyName, - event.getOldValue(), - event.getNewValue()); + firePropertyChange(propertyName, oldValue, newValue); } } else if (DeviceSystem.PROP_DEVICES.equals(propertyName)) { - if (event.getSource() instanceof AudioSystem) + if (ev.getSource() instanceof AudioSystem) { // Try to switch to a new active audio system if we are // currently using the "none" system. + @SuppressWarnings("unchecked") + List<CaptureDeviceInfo> newValue + = (List<CaptureDeviceInfo>) ev.getNewValue(); + switchFromNoneToActiveAudioSystem( - (CaptureDeviceInfo) event.getNewValue()); + ((newValue == null) || newValue.isEmpty()) + ? null + : newValue.get(0)); + firePropertyChange( PROP_AUDIO_SYSTEM_DEVICES, - event.getOldValue(), - event.getNewValue()); + ev.getOldValue(), + newValue); } } else if (PROP_VIDEO_FRAMERATE.equals(propertyName)) @@ -1221,30 +1222,31 @@ private void switchFromNoneToActiveAudioSystem( { if(newActiveDevice != null) { - String deviceSystemProtocol + String newActiveLocatorProtocol = newActiveDevice.getLocator().getProtocol(); + // If we are currently using the "none" system, and that the new // available device uses a different system: then switch to the new // audio system. - if(!deviceSystemProtocol.equals(NoneAudioSystem.LOCATOR_PROTOCOL) - && getAudioSystem().getLocatorProtocol().equals( - NoneAudioSystem.LOCATOR_PROTOCOL)) + if(!newActiveLocatorProtocol.equals( + NoneAudioSystem.LOCATOR_PROTOCOL) + && getAudioSystem().getLocatorProtocol().equals( + NoneAudioSystem.LOCATOR_PROTOCOL)) { // If the AUDIO media type is disabled via // MediaServiceImpl.DISABLE_AUDIO_SUPPORT_PNAME, then the - // DeviceSystem.initializeDeviceSystems will not instantiante + // DeviceSystem.initializeDeviceSystems will not instantiate // any other audio system (except the "none" one). Thereby, the // Audio.getAudioSystem will return null. - AudioSystem deviceAudioSystem - = AudioSystem.getAudioSystem(deviceSystemProtocol); - if(deviceAudioSystem != null) - { - setAudioSystem(deviceAudioSystem, false); - } + AudioSystem newActiveAudioSystem + = AudioSystem.getAudioSystem(newActiveLocatorProtocol); + + if(newActiveAudioSystem != null) + setAudioSystem(newActiveAudioSystem, false); } } // The device has been unplugged checks. It may be good to check if the - // current active audio system still have actvie devices. If not, then + // current active audio system still has active devices. If not, then // we will choose another audio system, or the "none" one if no audio // system is available. else diff --git a/src/org/jitsi/impl/neomedia/device/DeviceSystem.java b/src/org/jitsi/impl/neomedia/device/DeviceSystem.java index 48f7604811cb19eb2aa6a3673b3cff116de28462..b7a85b3d9018598e702b253c8af02983b0e0bbc4 100644 --- a/src/org/jitsi/impl/neomedia/device/DeviceSystem.java +++ b/src/org/jitsi/impl/neomedia/device/DeviceSystem.java @@ -35,12 +35,6 @@ public abstract class DeviceSystem extends PropertyChangeNotifier { - /** - * The <tt>Logger</tt> used by the <tt>DeviceSystem</tt> class and its - * instances for logging output. - */ - private static final Logger logger = Logger.getLogger(DeviceSystem.class); - /** * The list of <tt>DeviceSystem</tt>s which have been initialized. */ @@ -66,19 +60,20 @@ public abstract class DeviceSystem public static final String LOCATOR_PROTOCOL_VIDEO4LINUX2 = "video4linux2"; - public static final String PROP_DEVICES = "devices"; - /** - * The list of devices connected at the last postInitialize. + * The <tt>Logger</tt> used by the <tt>DeviceSystem</tt> class and its + * instances for logging output. */ - private static Vector<CaptureDeviceInfo> propertyChangeNewDevices - = new Vector<CaptureDeviceInfo>(); + private static final Logger logger = Logger.getLogger(DeviceSystem.class); /** - * The list of devices connected at the last preInitialize. + * The list of <tt>CaptureDeviceInfo</tt>s representing the devices of this + * instance at the time its {@link #preInitialize()} method was last + * invoked. */ - private static Vector<CaptureDeviceInfo> propertyChangeOldDevices - = new Vector<CaptureDeviceInfo>(); + private static List<CaptureDeviceInfo> preInitializeDevices; + + public static final String PROP_DEVICES = "devices"; /** * Returns a <tt>List</tt> of <tt>CaptureDeviceInfo</tt>s which are elements @@ -527,37 +522,59 @@ protected final void initialize() */ protected void postInitialize() { - Format format = getFormat(); - - if (format != null) + try { - // Gets the list of the actual connected devices. - propertyChangeNewDevices.removeAllElements(); - propertyChangeNewDevices.addAll( - CaptureDeviceManager.getDeviceList(format)); - - // Compares the previous connected device list with the current one - // in order to detect new connected or disconnected devices. - for(int i = 0; i < propertyChangeOldDevices.size(); ++i) + Format format = getFormat(); + + if (format != null) { - if(!propertyChangeNewDevices.remove( - propertyChangeOldDevices.get(i))) + /* + * Calculate the lists of old and new devices and report them in + * a PropertyChangeEvent about PROP_DEVICES. + */ + @SuppressWarnings("unchecked") + List<CaptureDeviceInfo> cdis + = CaptureDeviceManager.getDeviceList(format); + List<CaptureDeviceInfo> postInitializeDevices + = new ArrayList<CaptureDeviceInfo>(cdis); + + if (preInitializeDevices != null) + { + for (Iterator<CaptureDeviceInfo> preIter + = preInitializeDevices.iterator(); + preIter.hasNext();) + { + if (postInitializeDevices.remove(preIter.next())) + preIter.remove(); + } + } + + /* + * Fire a PropertyChangeEvent but only if there is an actual + * change in the value of the property. + */ + int preInitializeDeviceCount + = (preInitializeDevices == null) + ? 0 + : preInitializeDevices.size(); + int postInitializeDeviceCount + = (postInitializeDevices == null) + ? 0 + : postInitializeDevices.size(); + + if ((preInitializeDeviceCount != 0) + || (postInitializeDeviceCount != 0)) { - // Old device is removed. firePropertyChange( PROP_DEVICES, - propertyChangeOldDevices.get(i), - null); + preInitializeDevices, + postInitializeDevices); } } - for(int i = 0; i < propertyChangeNewDevices.size(); ++i) - { - // New device is plugged in. - firePropertyChange( - PROP_DEVICES, - null, - propertyChangeNewDevices.get(i)); - } + } + finally + { + preInitializeDevices = null; } } @@ -575,10 +592,10 @@ protected void preInitialize() if (format != null) { @SuppressWarnings("unchecked") - Vector<CaptureDeviceInfo> cdis + List<CaptureDeviceInfo> cdis = CaptureDeviceManager.getDeviceList(format); - propertyChangeOldDevices.removeAllElements(); - propertyChangeOldDevices.addAll(cdis); + + preInitializeDevices = new ArrayList<CaptureDeviceInfo>(cdis); if ((cdis != null) && (cdis.size() > 0)) {