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

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