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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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.implementations.BasePlugin;
  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 mplayer is currently
  33. * accessing.
  34. */
  35. public class MplayerMediaSourcePlugin extends BasePlugin implements MediaSource {
  36. /** {@inheritDoc} */
  37. @Override
  38. public MediaSourceState getState() {
  39. if (getInfo().isEmpty()) {
  40. return MediaSourceState.CLOSED;
  41. } else {
  42. return MediaSourceState.PLAYING;
  43. }
  44. }
  45. /** {@inheritDoc} */
  46. @Override
  47. public String getAppName() {
  48. return "MPlayer";
  49. }
  50. /** {@inheritDoc} */
  51. @Override
  52. public String getArtist() {
  53. return "";
  54. }
  55. /** {@inheritDoc} */
  56. @Override
  57. public String getTitle() {
  58. return getInfo().get(0);
  59. }
  60. /** {@inheritDoc} */
  61. @Override
  62. public String getAlbum() {
  63. return "";
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public String getLength() {
  68. return "";
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public String getTime() {
  73. return "";
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public String getFormat() {
  78. return "";
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public String getBitrate() {
  83. return "";
  84. }
  85. /**
  86. * Retrieves information about the currently playing track.
  87. *
  88. * @return Information about the currently playing track
  89. */
  90. public static List<String> getInfo() {
  91. final ArrayList<String> result = new ArrayList<>();
  92. InputStreamReader reader;
  93. BufferedReader input;
  94. Process process;
  95. try {
  96. final String[] command = new String[]{"/bin/bash", "-c",
  97. "/usr/bin/lsof -c gmplayer |"
  98. + " grep -Ev '/dev|/lib|/var|/usr|/SYS|DIR|/tmp|pipe|socket|"
  99. + "\\.xession|fontconfig' | tail -n 1 | sed -r 's/ +/ /g' |"
  100. + " cut -d ' ' -f 9- | sed -r 's/^.*\\/(.*?)$/\\1/'"};
  101. process = Runtime.getRuntime().exec(command);
  102. reader = new InputStreamReader(process.getInputStream());
  103. input = new BufferedReader(reader);
  104. String line = "";
  105. while ((line = input.readLine()) != null) {
  106. result.add(line);
  107. }
  108. reader.close();
  109. input.close();
  110. process.destroy();
  111. } catch (IOException ex) {
  112. ex.printStackTrace();
  113. }
  114. return result;
  115. }
  116. }