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.

MplayerMediaSourcePlugin.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.mediasource_mplayer;
  23. import com.dmdirc.addons.nowplaying.MediaSource;
  24. import com.dmdirc.addons.nowplaying.MediaSourceState;
  25. import com.dmdirc.plugins.Plugin;
  26. import java.io.BufferedReader;
  27. import java.io.IOException;
  28. import java.io.InputStreamReader;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. /**
  32. * Provides a media source for mplayer which uses lsof to hackily see what
  33. * mplayer is currently accessing.
  34. *
  35. * @author chris
  36. */
  37. public class MplayerMediaSourcePlugin extends Plugin implements MediaSource {
  38. /**
  39. * Creates a new instance of MplayerMediaSourcePlugin.
  40. */
  41. public MplayerMediaSourcePlugin() {
  42. super();
  43. }
  44. /** {@inheritDoc} */
  45. @Override
  46. public void onLoad() {
  47. // Nothing to do
  48. }
  49. /** {@inheritDoc} */
  50. @Override
  51. public void onUnload() {
  52. // Nothing to do
  53. }
  54. /** {@inheritDoc} */
  55. @Override
  56. public MediaSourceState getState() {
  57. if (getInfo().isEmpty()) {
  58. return MediaSourceState.CLOSED;
  59. } else {
  60. return MediaSourceState.PLAYING;
  61. }
  62. }
  63. /** {@inheritDoc} */
  64. @Override
  65. public String getAppName() {
  66. return "MPlayer";
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  70. public String getArtist() {
  71. return "";
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public String getTitle() {
  76. return getInfo().get(0);
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. public String getAlbum() {
  81. return "";
  82. }
  83. /** {@inheritDoc} */
  84. @Override
  85. public String getLength() {
  86. return "";
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public String getTime() {
  91. return "";
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public String getFormat() {
  96. return "";
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. public String getBitrate() {
  101. return "";
  102. }
  103. /**
  104. * Retrieves information about the currently playing track.
  105. *
  106. * @return Information about the currently playing track
  107. */
  108. public static List<String> getInfo() {
  109. final ArrayList<String> result = new ArrayList<String>();
  110. InputStreamReader reader;
  111. BufferedReader input;
  112. Process process;
  113. try {
  114. final String[] command = new String[]{"/bin/bash", "-c",
  115. "/usr/bin/lsof -c gmplayer |" +
  116. " grep -Ev '/dev|/lib|/var|/usr|/SYS|DIR|/tmp|pipe|socket|" +
  117. "\\.xession|fontconfig' | tail -n 1 | sed -r 's/ +/ /g' |" +
  118. " cut -d ' ' -f 9- | sed -r 's/^.*\\/(.*?)$/\\1/'"};
  119. process = Runtime.getRuntime().exec(command);
  120. reader = new InputStreamReader(process.getInputStream());
  121. input = new BufferedReader(reader);
  122. String line = "";
  123. while ((line = input.readLine()) != null) {
  124. result.add(line);
  125. }
  126. reader.close();
  127. input.close();
  128. process.destroy();
  129. } catch (IOException ex) {
  130. ex.printStackTrace();
  131. }
  132. return result;
  133. }
  134. }