Issue Description:
Users encounter an issue when attempting to switch the video input device in a game created with the Unreal Engine using the default sample project. The problem occurs after initially releasing the device collection, which prevents further switching as it consistently returns a -1 error.
Platform/SDK:
Unreal Engine with the Agora Video SDK.
Error Message:
Switching the video device returns -1 after calling the release function.
Step by Step Solution:
1. Enumerate the Devices:
Enumerate available video devices using the enumerateVideoDevices() method.
2. Select Desired Device:
Choose the preferred video device and retrieve its deviceId.
3. Switch Device:
Call setDevice() with the chosen deviceId before releasing the device collection.
4. Release After Switch:
Only call the release() method of the IVideoDeviceCollection object after successfully switching the device.
5. Re-enumerate If Needed:
If a device switch is required later, re-enumerate the video devices to obtain an updated list and then repeat steps 2-4.
cpp
// Get device list
auto* devices = videoDeviceManager->enumerateVideoDevices();
char deviceName[512];
char deviceId[512];
// Example: get the first device
devices->getDevice(0, deviceName, deviceId);
// Switch device BEFORE release
int ret = videoDeviceManager->setDevice(deviceId);
if (ret == 0) { // success
devices->release();
}
Root Cause:
The issue arises because the release() method is called prematurely, which destroys the internal list of devices. As a result, subsequent calls to setDevice() fail and return a -1 error code since there is no list to refer to.
Prevention/Best Practice:
- Treat the
IVideoDeviceCollectionobject as temporary and use it only for short-term tasks. - Always execute
setDevice()before invoking therelease()method to ensure the device is correctly set. - Plan for future device switches by re-enumerating devices whenever additional changes are needed.