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.

VlcMediaSourcePlugin.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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_vlc;
  23. import com.dmdirc.addons.nowplaying.MediaSource;
  24. import com.dmdirc.addons.nowplaying.MediaSourceState;
  25. import com.dmdirc.config.IdentityManager;
  26. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  27. import com.dmdirc.config.prefs.PreferencesCategory;
  28. import com.dmdirc.config.prefs.PreferencesDialogModel;
  29. import com.dmdirc.config.prefs.PreferencesSetting;
  30. import com.dmdirc.config.prefs.PreferencesType;
  31. import com.dmdirc.plugins.Plugin;
  32. import com.dmdirc.util.Downloader;
  33. import java.io.File;
  34. import java.io.IOException;
  35. import java.net.MalformedURLException;
  36. import java.util.HashMap;
  37. import java.util.List;
  38. import java.util.Map;
  39. /**
  40. * Retrieves information from VLC using its HTTP interface.
  41. *
  42. * @author chris
  43. */
  44. public class VlcMediaSourcePlugin extends Plugin implements MediaSource {
  45. /** The information obtained from VLC. */
  46. private final Map<String, String> information
  47. = new HashMap<String, String>();
  48. /** {@inheritDoc} */
  49. @Override
  50. public MediaSourceState getState() {
  51. if (fetchInformation()) {
  52. final String output = information.get("state");
  53. if (output.equalsIgnoreCase("stop")) {
  54. return MediaSourceState.STOPPED;
  55. } else if (output.equalsIgnoreCase("playing")) {
  56. return MediaSourceState.PLAYING;
  57. } else if (output.equalsIgnoreCase("paused")) {
  58. return MediaSourceState.PAUSED;
  59. } else {
  60. return MediaSourceState.NOTKNOWN;
  61. }
  62. } else {
  63. return MediaSourceState.CLOSED;
  64. }
  65. }
  66. /** {@inheritDoc} */
  67. @Override
  68. public String getAppName() {
  69. return "VLC";
  70. }
  71. /** {@inheritDoc} */
  72. @Override
  73. public String getArtist() {
  74. return information.containsKey("artist") ? information.get("artist") :
  75. getFallbackArtist();
  76. }
  77. /**
  78. * Retrieves the fallback artist (parsed from the file name).
  79. *
  80. * @return The fallback artist
  81. */
  82. private String getFallbackArtist() {
  83. String result = "unknown";
  84. if (information.containsKey("playlist_current")) {
  85. try {
  86. final int item = Integer.parseInt(information.get(
  87. "playlist_current"));
  88. String[] bits = information.get("playlist_item_" + item).split(
  89. (File.separatorChar == '\\' ? "\\\\" : File.separator));
  90. result = bits[bits.length - 1];
  91. bits = result.split("-");
  92. if (bits.length > 1) {
  93. result = bits[0];
  94. } else {
  95. // Whole filename is the title, so no artist is known.
  96. result = "unknown";
  97. }
  98. } catch (NumberFormatException nfe) {
  99. // DO nothing
  100. }
  101. }
  102. return result;
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public String getTitle() {
  107. return information.containsKey("title") ? information.get("title")
  108. : getFallbackTitle();
  109. }
  110. /**
  111. * Retrieves the fallback title (parsed from the file name).
  112. *
  113. * @return The fallback title
  114. */
  115. private String getFallbackTitle() {
  116. String result = "unknown";
  117. // Title is unknown, lets guess using the filename
  118. if (information.containsKey("playlist_current")) {
  119. try {
  120. final int item = Integer.parseInt(information.get(
  121. "playlist_current"));
  122. result = information.get("playlist_item_" + item);
  123. final int sepIndex = result.lastIndexOf(File.separatorChar);
  124. final int extIndex = result.lastIndexOf('.');
  125. result = result.substring(sepIndex,
  126. extIndex > sepIndex ? extIndex : result.length());
  127. final int offset = result.indexOf('-');
  128. if (offset > -1) {
  129. result = result.substring(offset + 1).trim();
  130. }
  131. } catch (NumberFormatException nfe) {
  132. // Do nothing
  133. }
  134. }
  135. return result;
  136. }
  137. /** {@inheritDoc} */
  138. @Override
  139. public String getAlbum() {
  140. return information.containsKey("album/movie/show title")
  141. ? information.get("album/movie/show title") : "unknown";
  142. }
  143. /** {@inheritDoc} */
  144. @Override
  145. public String getLength() {
  146. // This is just seconds, could do with formatting.
  147. return information.containsKey("length") ? information.get("length")
  148. : "unknown";
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public String getTime() {
  153. // This is just seconds, could do with formatting.
  154. return information.containsKey("time") ? information.get("time")
  155. : "unknown";
  156. }
  157. /** {@inheritDoc} */
  158. @Override
  159. public String getFormat() {
  160. return information.containsKey("codec") ? information.get("codec")
  161. : "unknown";
  162. }
  163. /** {@inheritDoc} */
  164. @Override
  165. public String getBitrate() {
  166. return information.containsKey("bitrate") ? information.get("bitrate")
  167. : "unknown";
  168. }
  169. /** {@inheritDoc} */
  170. @Override
  171. public void onLoad() {
  172. // Do nothing
  173. }
  174. /** {@inheritDoc} */
  175. @Override
  176. public void onUnload() {
  177. // Do nothing
  178. }
  179. /** {@inheritDoc} */
  180. @Override
  181. public void showConfig(final PreferencesDialogModel manager) {
  182. final PreferencesCategory general = new PluginPreferencesCategory(
  183. getPluginInfo(), "VLC Media Source",
  184. "", "category-vlc");
  185. final PreferencesSetting setting = new PreferencesSetting(
  186. PreferencesType.LABEL, getDomain(), "", "Instructions",
  187. "Instructions");
  188. setting.setValue("<html><p>"
  189. + "The VLC media source requires that VLC's web interface is"
  190. + " enabled. To do this, follow the steps below:</p>"
  191. + "<ol style='margin-left: 20px; padding-left: 0px;'>"
  192. + "<li>Open VLC's preferences dialog (found in the 'Tools' "
  193. + "menu)"
  194. + "<li>Set the 'Show settings' option to 'All'"
  195. + "<li>Expand the 'Interface' category by clicking on the plus "
  196. + "sign next to it"
  197. + "<li>Select the 'Main interfaces' category"
  198. + "<li>Check the box next to 'HTTP remote control interface'"
  199. + "<li>Expand the 'Main interfaces' category"
  200. + "<li>Select the 'HTTP' category"
  201. + "<li>In the 'Host address' field, enter 'localhost:8082'"
  202. + "<li>In the 'Source directory' field enter the path to VLC's"
  203. + " http directory<ul style='margin-left: 5px; padding-left: "
  204. + "0px; list-style-type: none;'>"
  205. + "<li style='padding-bottom: 5px'>For Linux users this may be "
  206. + "/usr/share/vlc/http/"
  207. + "<li>For Windows users this will be under the main VLC "
  208. + "directory, e.g. C:\\Program Files\\VLC\\http</ul><li>Click "
  209. + "'Save'<li>Restart VLC</ol></html>");
  210. general.addSetting(setting);
  211. general.addSetting(new PreferencesSetting(PreferencesType.TEXT,
  212. getDomain(), "host", "Hostname and port",
  213. "The host and port that VLC listens on for web connections"));
  214. manager.getCategory("Plugins").addSubCategory(general);
  215. }
  216. /**
  217. * Attempts to fetch information from VLC's web interface.
  218. *
  219. * @return True on success, false otherwise
  220. */
  221. private boolean fetchInformation() {
  222. information.clear();
  223. List<String> res;
  224. List<String> res2;
  225. try {
  226. res = Downloader.getPage("http://"
  227. + IdentityManager.getGlobalConfig().getOption(getDomain(),
  228. "host") + "/old/info.html");
  229. res2 = Downloader.getPage("http://"
  230. + IdentityManager.getGlobalConfig().getOption(getDomain(),
  231. "host") + "/old/");
  232. parseInformation(res, res2);
  233. return true;
  234. } catch (MalformedURLException ex) {
  235. return false;
  236. } catch (IOException ex) {
  237. return false;
  238. }
  239. }
  240. /**
  241. * Parses the information from the two pages obtained from VLC's web
  242. * interface.
  243. *
  244. * @param res The first page of VLC info (/old/info.html)
  245. * @param res2 The second page of VLC info (/old/)
  246. */
  247. protected void parseInformation(final List<String> res,
  248. final List<String> res2) {
  249. for (String line : res) {
  250. final String tline = line.trim();
  251. if (tline.startsWith("<li>")) {
  252. final int colon = tline.indexOf(':');
  253. final String key = tline.substring(5, colon).trim()
  254. .toLowerCase();
  255. final String value = tline.substring(colon + 1, tline.length()
  256. - 5).trim();
  257. information.put(key, value);
  258. }
  259. }
  260. boolean isPlaylist = false;
  261. boolean isCurrent = false;
  262. boolean isItem = false;
  263. int playlistItem = 0;
  264. for (String line : res2) {
  265. final String tline = line.trim();
  266. if (isPlaylist) {
  267. if (tline.startsWith("</ul>")) {
  268. isPlaylist = false;
  269. information.put("playlist_items", Integer.toString(
  270. playlistItem));
  271. } else if (tline.equalsIgnoreCase("<strong>")) {
  272. isCurrent = true;
  273. } else if (tline.equalsIgnoreCase("</strong>")) {
  274. isCurrent = false;
  275. } else if (tline.startsWith("<a href=\"?control=play&amp")) {
  276. isItem = true;
  277. } else if (isItem) {
  278. String itemname = tline;
  279. if (itemname.endsWith("</a>")) {
  280. itemname = itemname.substring(0, itemname.length() - 4);
  281. }
  282. if (!itemname.isEmpty()) {
  283. if (isCurrent) {
  284. information.put("playlist_current", Integer
  285. .toString(playlistItem));
  286. }
  287. information.put("playlist_item_" + Integer.toString(
  288. playlistItem++), itemname);
  289. }
  290. isItem = false;
  291. }
  292. } else if (tline.equalsIgnoreCase("<!-- Playlist -->")) {
  293. isPlaylist = true;
  294. } else if (tline.startsWith("State:")) {
  295. information.put("state", tline.substring(6, tline.indexOf('<'))
  296. .trim());
  297. } else if (tline.startsWith("got_")) {
  298. final int equals = tline.indexOf('=');
  299. information.put(tline.substring(4, equals).trim(),
  300. tline.substring(equals + 1, tline.length() - 1).trim());
  301. }
  302. }
  303. }
  304. }