Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

MplayerMediaSourcePlugin.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_mplayer;
  18. import com.dmdirc.addons.nowplaying.MediaSource;
  19. import com.dmdirc.addons.nowplaying.MediaSourceState;
  20. import com.dmdirc.plugins.implementations.BasePlugin;
  21. import java.io.BufferedReader;
  22. import java.io.IOException;
  23. import java.io.InputStreamReader;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. /**
  27. * Provides a media source for mplayer which uses lsof to hackily see what mplayer is currently
  28. * accessing.
  29. */
  30. public class MplayerMediaSourcePlugin extends BasePlugin implements MediaSource {
  31. @Override
  32. public MediaSourceState getState() {
  33. if (getInfo().isEmpty()) {
  34. return MediaSourceState.CLOSED;
  35. } else {
  36. return MediaSourceState.PLAYING;
  37. }
  38. }
  39. @Override
  40. public String getAppName() {
  41. return "MPlayer";
  42. }
  43. @Override
  44. public String getArtist() {
  45. return "";
  46. }
  47. @Override
  48. public String getTitle() {
  49. return getInfo().get(0);
  50. }
  51. @Override
  52. public String getAlbum() {
  53. return "";
  54. }
  55. @Override
  56. public String getLength() {
  57. return "";
  58. }
  59. @Override
  60. public String getTime() {
  61. return "";
  62. }
  63. @Override
  64. public String getFormat() {
  65. return "";
  66. }
  67. @Override
  68. public String getBitrate() {
  69. return "";
  70. }
  71. /**
  72. * Retrieves information about the currently playing track.
  73. *
  74. * @return Information about the currently playing track
  75. */
  76. public static List<String> getInfo() {
  77. final List<String> result = new ArrayList<>();
  78. try {
  79. final String[] command = {"/bin/bash", "-c",
  80. "/usr/bin/lsof -c gmplayer |"
  81. + " grep -Ev '/dev|/lib|/var|/usr|/SYS|DIR|/tmp|pipe|socket|"
  82. + "\\.xession|fontconfig' | tail -n 1 | sed -r 's/ +/ /g' |"
  83. + " cut -d ' ' -f 9- | sed -r 's/^.*\\/(.*?)$/\\1/'"};
  84. final Process process = Runtime.getRuntime().exec(command);
  85. try (InputStreamReader reader = new InputStreamReader(process.getInputStream());
  86. BufferedReader input = new BufferedReader(reader)) {
  87. String line;
  88. while ((line = input.readLine()) != null) {
  89. result.add(line);
  90. }
  91. }
  92. process.destroy();
  93. } catch (IOException ex) {
  94. ex.printStackTrace();
  95. }
  96. return result;
  97. }
  98. }