///////////////////////////////////////////////////////
| 187 | |
| 188 | //////////////////////////////////////////////////////////// |
| 189 | std::vector<AudioDevice::DeviceEntry> AudioDevice::getAvailableDevices() |
| 190 | { |
| 191 | const auto getDevices = [](auto& context) |
| 192 | { |
| 193 | ma_device_info* deviceInfos{}; |
| 194 | std::uint32_t deviceCount{}; |
| 195 | |
| 196 | // Get the playback devices |
| 197 | if (const auto result = ma_context_get_devices(&context, &deviceInfos, &deviceCount, nullptr, nullptr); |
| 198 | result != MA_SUCCESS) |
| 199 | { |
| 200 | err() << "Failed to get audio playback devices: " << ma_result_description(result) << std::endl; |
| 201 | return std::vector<DeviceEntry>{}; |
| 202 | } |
| 203 | |
| 204 | std::vector<DeviceEntry> deviceList; |
| 205 | deviceList.reserve(deviceCount); |
| 206 | |
| 207 | // In order to report devices with identical names and still allow |
| 208 | // the user to differentiate between them when selecting, we append |
| 209 | // an index (number) to their name starting from the second entry |
| 210 | std::unordered_map<std::string, int> deviceIndices; |
| 211 | deviceIndices.reserve(deviceCount); |
| 212 | |
| 213 | for (auto i = 0u; i < deviceCount; ++i) |
| 214 | { |
| 215 | auto name = std::string(deviceInfos[i].name); |
| 216 | auto& index = deviceIndices[name]; |
| 217 | |
| 218 | ++index; |
| 219 | |
| 220 | if (index > 1) |
| 221 | name += ' ' + std::to_string(index); |
| 222 | |
| 223 | // Make sure the default device is always placed at the front |
| 224 | deviceList.emplace(deviceInfos[i].isDefault ? deviceList.begin() : deviceList.end(), |
| 225 | DeviceEntry{name, deviceInfos[i].id, deviceInfos[i].isDefault == MA_TRUE}); |
| 226 | } |
| 227 | |
| 228 | return deviceList; |
| 229 | }; |
| 230 | |
| 231 | std::vector<DeviceEntry> deviceList; |
| 232 | auto retry = false; |
| 233 | const ma_backend nullBackend = ma_backend_null; |
| 234 | const ma_backend* retryBackend{}; |
| 235 | |
| 236 | // Use an existing instance's context if one exists |
| 237 | if (auto* instance = getInstance(); instance && instance->m_context) |
| 238 | { |
| 239 | deviceList = getDevices(*instance->m_context); |
| 240 | |
| 241 | if (deviceList.empty() && (instance->m_context->backend != ma_backend_null)) |
| 242 | { |
| 243 | // Retry with null backend |
| 244 | retry = true; |
| 245 | retryBackend = &nullBackend; |
| 246 | } |