Browse Source

Update DBUS MediaSource to use org.mpris.MediaPlayer2 interfaces.

Change-Id: I487c654af9ca04ae130c607171bc9c535e4c7e89
Reviewed-on: http://gerrit.dmdirc.com/2602
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.7
Shane Mc Cormack 11 years ago
parent
commit
b6e972591b

+ 0
- 175
src/com/dmdirc/addons/mediasource_dbus/BansheeSource.java View File

@@ -1,175 +0,0 @@
1
-/*
2
- * Copyright (c) 2006-2012 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
-
23
-package com.dmdirc.addons.mediasource_dbus;
24
-
25
-import com.dmdirc.addons.nowplaying.MediaSource;
26
-import com.dmdirc.addons.nowplaying.MediaSourceState;
27
-
28
-import java.util.List;
29
-import java.util.Map;
30
-
31
-/**
32
- * A media source for Banshee.
33
- *
34
- * @author chris
35
- */
36
-public class BansheeSource implements MediaSource {
37
-
38
-    /** The dbus service name. */
39
-    private static final String SERVICE = "org.bansheeproject.Banshee";
40
-    /** The dbus interface name. */
41
-    private static final String IFACE = "/org/bansheeproject/Banshee/PlayerEngine";
42
-    /** The method to get the current state. */
43
-    private static final String STATE = "org.bansheeproject.Banshee.PlayerEngine.GetCurrentState";
44
-    /** The method to get the current track. */
45
-    private static final String TRACK = "org.bansheeproject.Banshee.PlayerEngine.GetCurrentTrack";
46
-    /** The method to get the current position. */
47
-    private static final String POSITION = "org.bansheeproject.Banshee.PlayerEngine.GetPosition";
48
-
49
-    /** The media source that owns this source. */
50
-    private final DBusMediaSource source;
51
-    /** A cache of track information. */
52
-    private Map<String, String> trackInfo;
53
-
54
-    public BansheeSource(final DBusMediaSource source) {
55
-        this.source = source;
56
-    }
57
-
58
-    /** {@inheritDoc} */
59
-    @Override
60
-    public MediaSourceState getState() {
61
-        final List<String> res = source.doDBusCall(SERVICE, IFACE, STATE);
62
-
63
-        if (res.isEmpty()) {
64
-            trackInfo = null;
65
-            return MediaSourceState.CLOSED;
66
-        } else if ("playing".equals(res.get(0))) {
67
-            trackInfo = getTrackInfo();
68
-            return MediaSourceState.PLAYING;
69
-        } else if ("paused".equals(res.get(0))) {
70
-            trackInfo = getTrackInfo();
71
-            return MediaSourceState.PAUSED;
72
-        } else if ("idle".equals(res.get(0))) {
73
-            trackInfo = getTrackInfo();
74
-            return MediaSourceState.STOPPED;
75
-        } else {
76
-            trackInfo = null;
77
-            return MediaSourceState.NOTKNOWN;
78
-        }
79
-    }
80
-
81
-    /**
82
-     * Retrieves a map of track information from the dbus service.
83
-     *
84
-     * @return A map of track information
85
-     */
86
-    protected Map<String, String> getTrackInfo() {
87
-        return DBusMediaSource.parseDictionary(source.doDBusCall(SERVICE, IFACE, TRACK));
88
-    }
89
-
90
-    /** {@inheritDoc} */
91
-    @Override
92
-    public String getAppName() {
93
-        return "Banshee";
94
-    }
95
-
96
-    /** {@inheritDoc} */
97
-    @Override
98
-    public String getArtist() {
99
-        return trackInfo == null ? "Unknown" : trackInfo.get("artist");
100
-    }
101
-
102
-    /** {@inheritDoc} */
103
-    @Override
104
-    public String getTitle() {
105
-        return trackInfo == null ? "Unknown" : trackInfo.get("name");
106
-    }
107
-
108
-    /** {@inheritDoc} */
109
-    @Override
110
-    public String getAlbum() {
111
-        return trackInfo == null ? "Unknown" : trackInfo.get("album");
112
-    }
113
-
114
-    /** {@inheritDoc} */
115
-    @Override
116
-    public String getLength() {
117
-        return trackInfo == null ? "Unknown" : duration((long)
118
-                Double.parseDouble(trackInfo.get("length")));
119
-    }
120
-
121
-    /** {@inheritDoc} */
122
-    @Override
123
-    public String getTime() {
124
-        return duration((long) Double.parseDouble(source.doDBusCall(SERVICE,
125
-                IFACE, POSITION).get(0)) / 1000);
126
-    }
127
-
128
-    /** {@inheritDoc} */
129
-    @Override
130
-    public String getFormat() {
131
-        return trackInfo == null ? "Unknown" : trackInfo.get("mime-type");
132
-    }
133
-
134
-    /** {@inheritDoc} */
135
-    @Override
136
-    public String getBitrate() {
137
-        return trackInfo == null ? "Unknown" : trackInfo.get("bit-rate");
138
-    }
139
-
140
-    /**
141
-     * Get the duration in seconds as a string.
142
-     *
143
-     * @param seconds Input to get duration for
144
-     * @return Duration as a string
145
-     */
146
-    private String duration(final long secondsInput) {
147
-        final StringBuilder result = new StringBuilder();
148
-        final long hours = secondsInput / 3600;
149
-        final long minutes = secondsInput / 60 % 60;
150
-        final long seconds = secondsInput % 60;
151
-
152
-        if (hours > 0) {
153
-            if (hours < 10) {
154
-                result.append('0');
155
-            }
156
-
157
-            result.append(hours).append(":");
158
-        }
159
-
160
-        if (minutes < 10) {
161
-            result.append('0');
162
-        }
163
-
164
-        result.append(minutes).append(":");
165
-
166
-        if (seconds < 10) {
167
-            result.append('0');
168
-        }
169
-
170
-        result.append(seconds);
171
-
172
-        return result.toString();
173
-    }
174
-
175
-}

+ 2
- 5
src/com/dmdirc/addons/mediasource_dbus/DBusMediaSource.java View File

@@ -33,7 +33,6 @@ import java.io.File;
33 33
 import java.io.IOException;
34 34
 import java.io.InputStreamReader;
35 35
 import java.util.ArrayList;
36
-import java.util.Arrays;
37 36
 import java.util.HashMap;
38 37
 import java.util.List;
39 38
 import java.util.Map;
@@ -54,9 +53,7 @@ public class DBusMediaSource extends BasePlugin implements MediaSourceManager {
54 53
     /** {@inheritDoc} */
55 54
     @Override
56 55
     public void onLoad() {
57
-        sources = new ArrayList<MediaSource>(Arrays.asList(new MediaSource[]{
58
-            new BansheeSource(this),
59
-        }));
56
+        sources = new ArrayList<MediaSource>();
60 57
 
61 58
         if (new File("/usr/bin/qdbus").exists()) {
62 59
             qdbus = "/usr/bin/qdbus";
@@ -160,7 +157,7 @@ public class DBusMediaSource extends BasePlugin implements MediaSourceManager {
160 157
         final Map<String, String> res = new HashMap<String, String>();
161 158
 
162 159
         for (String line : lines) {
163
-            final int index = line.indexOf(':');
160
+            final int index = line.indexOf(':', line.indexOf(':') + 1);
164 161
 
165 162
             if (index == -1 || index >= line.length() - 2) {
166 163
                 continue;

+ 73
- 37
src/com/dmdirc/addons/mediasource_dbus/MPRISSource.java View File

@@ -54,20 +54,33 @@ public class MPRISSource implements MediaSource {
54 54
         this.source = source;
55 55
         this.service = service;
56 56
 
57
-        final List<String> info = source.doDBusCall("org.mpris." + service, "/",
58
-                "org.freedesktop.MediaPlayer.Identity");
57
+        final String info = getFirstValue("org.mpris.MediaPlayer2.Identity");
59 58
 
60 59
         if (info.isEmpty()) {
61 60
             throw new IllegalArgumentException("No service with that name found");
62 61
         }
63 62
 
64
-        this.name = info.get(0).replace(' ', '_');
63
+        this.name = info.replace(' ', '_');
64
+    }
65
+
66
+    /**
67
+     * Get the first line of the output for a dbus call to the given function
68
+     * against this service in the /org/mpris/MediaPlayer2 obejct.
69
+     *
70
+     * @param function Function to get data for.
71
+     * @return First line of output.
72
+     */
73
+    protected String getFirstValue(final String function) {
74
+        final List<String> info = source.doDBusCall("org.mpris." + service,
75
+                "/org/mpris/MediaPlayer2", function);
76
+
77
+        return info.isEmpty() ? "" : info.get(0);
65 78
     }
66 79
 
67 80
     /** {@inheritDoc} */
68 81
     @Override
69 82
     public MediaSourceState getState() {
70
-        final char[] status = getStatus();
83
+        final String status = getStatus();
71 84
 
72 85
         if (status == null) {
73 86
             data = null;
@@ -76,11 +89,11 @@ public class MPRISSource implements MediaSource {
76 89
 
77 90
         data = getTrackInfo();
78 91
 
79
-        if (status[0] == '0') {
92
+        if (status.equalsIgnoreCase("Playing")) {
80 93
             return MediaSourceState.PLAYING;
81
-        } else if (status[0] == '1') {
94
+        } else if (status.equalsIgnoreCase("Paused")) {
82 95
             return MediaSourceState.PAUSED;
83
-        } else if (status[0] == '2') {
96
+        } else if (status.equalsIgnoreCase("Stopped")) {
84 97
             return MediaSourceState.STOPPED;
85 98
         } else {
86 99
             return MediaSourceState.NOTKNOWN;
@@ -108,31 +121,46 @@ public class MPRISSource implements MediaSource {
108 121
     /** {@inheritDoc} */
109 122
     @Override
110 123
     public String getArtist() {
111
-        return getData("artist");
124
+        return getData("xesam:artist");
112 125
     }
113 126
 
114 127
     /** {@inheritDoc} */
115 128
     @Override
116 129
     public String getTitle() {
117
-        return getData("title");
130
+        return getData("xesam:title");
118 131
     }
119 132
 
120 133
     /** {@inheritDoc} */
121 134
     @Override
122 135
     public String getAlbum() {
123
-        return getData("album");
136
+        return getData("xesam:album");
124 137
     }
125 138
 
126 139
     /** {@inheritDoc} */
127 140
     @Override
128 141
     public String getLength() {
129
-        return getData("time");
142
+        try {
143
+            final Long len = Long.parseLong(getData("mpris:length"));
144
+            return duration(len / 1000);
145
+        } catch (final NumberFormatException nfe) {
146
+            return "Unknown";
147
+        }
130 148
     }
131 149
 
132 150
     /** {@inheritDoc} */
133 151
     @Override
134 152
     public String getTime() {
135
-        return "Unknown";
153
+        try {
154
+            final String position = getFirstValue("org.mpris.MediaPlayer2.Player.Position");
155
+            final Long len = Long.parseLong(position);
156
+            if (len == 0) {
157
+                return "Unknown";
158
+            } else {
159
+                return duration(len / 1000);
160
+            }
161
+        } catch (final NumberFormatException nfe) {
162
+            return "Unknown";
163
+        }
136 164
     }
137 165
 
138 166
     /** {@inheritDoc} */
@@ -144,7 +172,7 @@ public class MPRISSource implements MediaSource {
144 172
     /** {@inheritDoc} */
145 173
     @Override
146 174
     public String getBitrate() {
147
-        return getData("audio-bitrate");
175
+        return "Unknown";
148 176
     }
149 177
 
150 178
     /**
@@ -153,11 +181,8 @@ public class MPRISSource implements MediaSource {
153 181
      * @return A map of metadata returned by the MPRIS service
154 182
      */
155 183
     protected Map<String, String> getTrackInfo() {
156
-        // If only there were a standard...
157 184
         final List<String> list = source.doDBusCall("org.mpris." + service,
158
-                "/Player", "org.freedesktop.MediaPlayer.GetMetadata");
159
-        list.addAll(source.doDBusCall("org.mpris." + service,
160
-                "/Player", "org.freedesktop.MediaPlayer.GetMetaData"));
185
+                "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player.Metadata");
161 186
         return DBusMediaSource.parseDictionary(list);
162 187
     }
163 188
 
@@ -166,35 +191,46 @@ public class MPRISSource implements MediaSource {
166 191
      *
167 192
      * @return The returned status or null if the service isn't running
168 193
      */
169
-    protected char[] getStatus() {
170
-        if (source.doDBusCall("org.mpris." + service, "/",
171
-                "org.freedesktop.MediaPlayer.Identity").isEmpty()) {
172
-            // Calling dbus-send can seemingly start applications that have
173
-            // quit (not entirely sure how), so check that it's running first.
194
+    protected String getStatus() {
195
+        if (getFirstValue("org.mpris.MediaPlayer2.Identity").isEmpty()) {
174 196
             return null;
175 197
         }
176 198
 
177
-        final List<String> res = DBusMediaSource.getInfo(new String[]{
178
-            "/usr/bin/dbus-send", "--print-reply", "--dest=org.mpris." + service,
179
-            "/Player", "org.freedesktop.MediaPlayer.GetStatus"
180
-        });
199
+        return getFirstValue("org.mpris.MediaPlayer2.Player.PlaybackStatus");
200
+    }
181 201
 
182
-        if (res.isEmpty()) {
183
-            return null;
202
+    /**
203
+     * Get the duration in seconds as a string.
204
+     *
205
+     * @param seconds Input to get duration for
206
+     * @return Duration as a string
207
+     */
208
+    private String duration(final long secondsInput) {
209
+        final StringBuilder result = new StringBuilder();
210
+        final long hours = secondsInput / 3600;
211
+        final long minutes = secondsInput / 60 % 60;
212
+        final long seconds = secondsInput % 60;
213
+
214
+        if (hours > 0) {
215
+            if (hours < 10) {
216
+                result.append('0');
217
+            }
218
+
219
+            result.append(hours).append(":");
184 220
         }
185 221
 
186
-        final char[] result = new char[4];
187
-        int i = 0;
222
+        if (minutes < 10) {
223
+            result.append('0');
224
+        }
188 225
 
189
-        for (String line : res) {
190
-            final String tline = line.trim();
226
+        result.append(minutes).append(":");
191 227
 
192
-            if (tline.startsWith("int32")) {
193
-                result[i++] = tline.charAt(tline.length() - 1);
194
-            }
228
+        if (seconds < 10) {
229
+            result.append('0');
195 230
         }
196 231
 
197
-        return result;
198
-    }
232
+        result.append(seconds);
199 233
 
234
+        return result.toString();
235
+    }
200 236
 }

+ 1
- 2
src/com/dmdirc/addons/mediasource_dbus/plugin.config View File

@@ -20,10 +20,9 @@ updates:
20 20
   id=39
21 21
 
22 22
 version:
23
-  friendly=1.0
23
+  friendly=2.0
24 24
 
25 25
 provides:
26
-  banshee mediasource
27 26
   mpris mediasource
28 27
 
29 28
 requires:

Loading…
Cancel
Save