You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WindowsMediaSourceManager.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.mediasource_windows;
  18. import com.dmdirc.addons.nowplaying.MediaSource;
  19. import com.dmdirc.addons.nowplaying.MediaSourceManager;
  20. import com.dmdirc.plugins.PluginDomain;
  21. import com.dmdirc.plugins.PluginInfo;
  22. import com.dmdirc.plugins.PluginManager;
  23. import com.dmdirc.plugins.implementations.PluginFilesHelper;
  24. import com.dmdirc.util.io.StreamUtils;
  25. import com.google.common.io.CharStreams;
  26. import java.io.IOException;
  27. import java.io.InputStreamReader;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import javax.inject.Inject;
  31. /**
  32. * Manages all Windows based media sources.
  33. */
  34. public class WindowsMediaSourceManager implements MediaSourceManager {
  35. /** Media sources. */
  36. private final List<MediaSource> sources;
  37. /** Plugin files helper. */
  38. private final PluginFilesHelper filesHelper;
  39. @Inject
  40. public WindowsMediaSourceManager(
  41. final PluginManager pluginManager,
  42. @PluginDomain(WindowsMediaSourcePlugin.class) final PluginInfo pluginInfo) {
  43. this.filesHelper = new PluginFilesHelper(pluginManager, pluginInfo);
  44. sources = new ArrayList<>();
  45. sources.add(new DllSource(this, "Winamp", true));
  46. sources.add(new DllSource(this, "iTunes", false));
  47. }
  48. public void onLoad() {
  49. // Extract the .dlls and .exe
  50. try {
  51. filesHelper.extractResourcesEndingWith(".dll");
  52. filesHelper.extractResourcesEndingWith(".exe");
  53. } catch (IOException ex) {
  54. throw new IllegalStateException("Unable to extract needed files: " + ex.getMessage(),
  55. ex);
  56. }
  57. }
  58. @Override
  59. public List<MediaSource> getSources() {
  60. return sources;
  61. }
  62. /**
  63. * Get the output from GetMediaInfo.exe for the given player and method
  64. *
  65. * @param player Player to ask about
  66. * @param method Method to call
  67. *
  68. * @return a MediaInfoOutput with the results
  69. */
  70. protected MediaInfoOutput getOutput(final String player, final String method) {
  71. try {
  72. final Process myProcess = Runtime.getRuntime().exec(new String[]{
  73. filesHelper.getFilesDirString() + "GetMediaInfo.exe",
  74. player,
  75. method});
  76. StreamUtils.readStream(myProcess.getErrorStream());
  77. final String data = CharStreams
  78. .toString(new InputStreamReader(myProcess.getInputStream()));
  79. try {
  80. myProcess.waitFor();
  81. } catch (InterruptedException e) {
  82. }
  83. return new MediaInfoOutput(myProcess.exitValue(), data);
  84. } catch (SecurityException | IOException e) {
  85. }
  86. return new MediaInfoOutput(-1, "Error executing GetMediaInfo.exe");
  87. }
  88. }