View Javadoc
1   /*
2    * Copyright 2018-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.windows;
6   
7   import java.util.ArrayList;
8   import java.util.List;
9   
10  import com.sun.jna.platform.win32.Advapi32Util;
11  import com.sun.jna.platform.win32.Win32Exception;
12  import com.sun.jna.platform.win32.WinError;
13  import com.sun.jna.platform.win32.WinReg;
14  
15  import oshi.annotation.concurrent.Immutable;
16  import oshi.hardware.SoundCard;
17  import oshi.hardware.common.AbstractSoundCard;
18  
19  /**
20   * Sound Card data obtained from registry
21   */
22  @Immutable
23  final class WindowsSoundCard extends AbstractSoundCard {
24  
25      private static final String REGISTRY_SOUNDCARDS = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e96c-e325-11ce-bfc1-08002be10318}\\";
26  
27      /**
28       * Constructor for WindowsSoundCard.
29       *
30       * @param kernelVersion The version
31       * @param name          The name
32       * @param codec         The codec
33       */
34      WindowsSoundCard(String kernelVersion, String name, String codec) {
35          super(kernelVersion, name, codec);
36      }
37  
38      /**
39       * Returns Windows audio device driver information, which represents the closest proxy we have to sound cards.
40       * <p>
41       * NOTE : The reason why the codec name is same as the card name is because windows does not provide the name of the
42       * codec chip but sometimes the name of the card returned is infact the name of the codec chip also. Example :
43       * Realtek ALC887 HD Audio Device
44       *
45       * @return List of sound cards
46       */
47      public static List<SoundCard> getSoundCards() {
48          List<SoundCard> soundCards = new ArrayList<>();
49          String[] keys = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE, REGISTRY_SOUNDCARDS);
50          for (String key : keys) {
51              String fullKey = REGISTRY_SOUNDCARDS + key;
52              try {
53                  if (Advapi32Util.registryValueExists(WinReg.HKEY_LOCAL_MACHINE, fullKey, "Driver")) {
54                      soundCards.add(new WindowsSoundCard(
55                              Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, fullKey, "Driver") + " "
56                                      + Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, fullKey,
57                                              "DriverVersion"),
58                              Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, fullKey, "ProviderName")
59                                      + " "
60                                      + Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, fullKey,
61                                              "DriverDesc"),
62                              Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, fullKey, "DriverDesc")));
63                  }
64              } catch (Win32Exception e) {
65                  if (e.getErrorCode() != WinError.ERROR_ACCESS_DENIED) {
66                      // Ignore access denied errors, re-throw others
67                      throw e;
68                  }
69              }
70          }
71          return soundCards;
72      }
73  }