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.

MPRISSource.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_dbus;
  23. import com.dmdirc.addons.nowplaying.MediaSource;
  24. import com.dmdirc.addons.nowplaying.MediaSourceState;
  25. import com.dmdirc.util.DateUtils;
  26. import java.util.List;
  27. import java.util.Map;
  28. /**
  29. * A media source for anything that's compatible with MRPIS.
  30. */
  31. public class MPRISSource implements MediaSource {
  32. /** The media source manager. */
  33. private final DBusMediaSourceManager source;
  34. /** The service name. */
  35. private final String service;
  36. /** The name of the source. */
  37. private final String name;
  38. /** A temporary cache of track data. */
  39. private Map<String, String> data;
  40. /**
  41. * Creates a new MPRIS source for the specified service name.
  42. *
  43. * @param source The manager which owns this source
  44. * @param service The service name of the MRPIS service
  45. */
  46. public MPRISSource(final DBusMediaSourceManager source, final String service) {
  47. this.source = source;
  48. this.service = service;
  49. final String info = getFirstValue("org.mpris.MediaPlayer2.Identity");
  50. if (info.isEmpty()) {
  51. throw new IllegalArgumentException("No service with that name found");
  52. }
  53. this.name = info.replace(' ', '_');
  54. }
  55. /**
  56. * Get the first line of the output for a dbus call to the given function against this service
  57. * in the /org/mpris/MediaPlayer2 obejct.
  58. *
  59. * @param function Function to get data for.
  60. *
  61. * @return First line of output.
  62. */
  63. protected String getFirstValue(final String function) {
  64. final List<String> info = source.doDBusCall("org.mpris." + service,
  65. "/org/mpris/MediaPlayer2", function);
  66. return info.isEmpty() ? "" : info.get(0);
  67. }
  68. @Override
  69. public MediaSourceState getState() {
  70. final String status = getStatus();
  71. if (status == null) {
  72. data = null;
  73. return MediaSourceState.CLOSED;
  74. }
  75. data = getTrackInfo();
  76. if (status.equalsIgnoreCase("Playing")) {
  77. return MediaSourceState.PLAYING;
  78. } else if (status.equalsIgnoreCase("Paused")) {
  79. return MediaSourceState.PAUSED;
  80. } else if (status.equalsIgnoreCase("Stopped")) {
  81. return MediaSourceState.STOPPED;
  82. } else {
  83. return MediaSourceState.NOTKNOWN;
  84. }
  85. }
  86. @Override
  87. public String getAppName() {
  88. return name;
  89. }
  90. /**
  91. * Utility method to return the value of the specified key if it exists, or "Unknown" if it
  92. * doesn't.
  93. *
  94. * @param key The key to be retrieved
  95. *
  96. * @return The value of the specified key or "Unknown".
  97. */
  98. protected String getData(final String key) {
  99. return data == null || !data.containsKey(key) || data.get(key) == null
  100. ? "Unknown" : data.get(key);
  101. }
  102. @Override
  103. public String getArtist() {
  104. return getData("xesam:artist");
  105. }
  106. @Override
  107. public String getTitle() {
  108. return getData("xesam:title");
  109. }
  110. @Override
  111. public String getAlbum() {
  112. return getData("xesam:album");
  113. }
  114. @Override
  115. public String getLength() {
  116. try {
  117. final Long len = Long.parseLong(getData("mpris:length"));
  118. return DateUtils.formatDurationAsTime((int) (len / 1000));
  119. } catch (final NumberFormatException nfe) {
  120. return "Unknown";
  121. }
  122. }
  123. @Override
  124. public String getTime() {
  125. try {
  126. final String position = getFirstValue("org.mpris.MediaPlayer2.Player.Position");
  127. final Long len = Long.parseLong(position);
  128. if (len == 0) {
  129. return "Unknown";
  130. } else {
  131. return DateUtils.formatDurationAsTime((int) (len / 1000));
  132. }
  133. } catch (final NumberFormatException nfe) {
  134. return "Unknown";
  135. }
  136. }
  137. @Override
  138. public String getFormat() {
  139. return "Unknown";
  140. }
  141. @Override
  142. public String getBitrate() {
  143. return "Unknown";
  144. }
  145. /**
  146. * Retrieves a map of track information from the service.
  147. *
  148. * @return A map of metadata returned by the MPRIS service
  149. */
  150. protected Map<String, String> getTrackInfo() {
  151. final List<String> list = source.doDBusCall("org.mpris." + service,
  152. "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player.Metadata");
  153. return source.parseDictionary(list);
  154. }
  155. /**
  156. * Retrieves the 'status' result from the MPRIS service.
  157. *
  158. * @return The returned status or null if the service isn't running
  159. */
  160. protected String getStatus() {
  161. if (getFirstValue("org.mpris.MediaPlayer2.Identity").isEmpty()) {
  162. return null;
  163. }
  164. return getFirstValue("org.mpris.MediaPlayer2.Player.PlaybackStatus");
  165. }
  166. }