jitsi / libjitsi

Advanced Java media library for secure real-time audio/video communication.
Apache License 2.0
628 stars 281 forks source link

Change output device due to race condition #512

Open francescoc93 opened 4 years ago

francescoc93 commented 4 years ago

Description

When there are a lot of output device, sometimes when answer the call, the output device change randomly. This problem is due to a race condition in Devices class (package org.jitsi.impl.neomedia.device) method getSelectedDevice(List activeDevices). When some threads reload the list of devices preference (method loadDevicePreferences) , they empty the list of preferences and, when a thread try to get the device where to send the stream, it find an empty list of preferences and then pick up the first active device detected

Current behavior

sometimes when answer the call, the output device change randomly

Expected Behavior

always send the stream to the first first detected device in the list of preferences

Possible Solution

include the block of code from line 174 to line 217 in the synchronized block at the line 221

Steps to reproduce

it's hard reproduce this bug. To facilitate the reproduce of this bug, it's possibile insert a semaphore in loadDevicePreferences' method (acquire the resource at the begginin and release at the end of method) and a semaphore in getSelectedDevice method (acquire resource at line 173 and release at line 175)

Environment details