MpcHcMediaPlayer.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. using MediaBrowser.Common.Logging;
  2. using MediaBrowser.Model.DTO;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.UI.Configuration;
  5. using MediaBrowser.UI.Controller;
  6. using MediaBrowser.UI.Playback;
  7. using MediaBrowser.UI.Playback.ExternalPlayer;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel.Composition;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Net.Http;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace MediaBrowser.Plugins.MpcHc
  17. {
  18. /// <summary>
  19. /// Class GenericExternalPlayer
  20. /// </summary>
  21. [Export(typeof(BaseMediaPlayer))]
  22. public class MpcHcMediaPlayer : BaseExternalPlayer
  23. {
  24. /// <summary>
  25. /// The state sync lock
  26. /// </summary>
  27. private object stateSyncLock = new object();
  28. /// <summary>
  29. /// The MPC HTTP interface resource pool
  30. /// </summary>
  31. private SemaphoreSlim MpcHttpInterfaceResourcePool = new SemaphoreSlim(1, 1);
  32. /// <summary>
  33. /// Gets or sets the HTTP interface cancellation token.
  34. /// </summary>
  35. /// <value>The HTTP interface cancellation token.</value>
  36. private CancellationTokenSource HttpInterfaceCancellationTokenSource { get; set; }
  37. /// <summary>
  38. /// Gets or sets a value indicating whether this instance has started playing.
  39. /// </summary>
  40. /// <value><c>true</c> if this instance has started playing; otherwise, <c>false</c>.</value>
  41. private bool HasStartedPlaying { get; set; }
  42. /// <summary>
  43. /// Gets or sets the status update timer.
  44. /// </summary>
  45. /// <value>The status update timer.</value>
  46. private Timer StatusUpdateTimer { get; set; }
  47. /// <summary>
  48. /// Gets a value indicating whether this instance can monitor progress.
  49. /// </summary>
  50. /// <value><c>true</c> if this instance can monitor progress; otherwise, <c>false</c>.</value>
  51. protected override bool CanMonitorProgress
  52. {
  53. get
  54. {
  55. return true;
  56. }
  57. }
  58. /// <summary>
  59. /// The _current position ticks
  60. /// </summary>
  61. private long? _currentPositionTicks;
  62. /// <summary>
  63. /// Gets the current position ticks.
  64. /// </summary>
  65. /// <value>The current position ticks.</value>
  66. public override long? CurrentPositionTicks
  67. {
  68. get
  69. {
  70. return _currentPositionTicks;
  71. }
  72. }
  73. /// <summary>
  74. /// The _current playlist index
  75. /// </summary>
  76. private int _currentPlaylistIndex;
  77. /// <summary>
  78. /// Gets the index of the current playlist.
  79. /// </summary>
  80. /// <value>The index of the current playlist.</value>
  81. public override int CurrentPlaylistIndex
  82. {
  83. get
  84. {
  85. return _currentPlaylistIndex;
  86. }
  87. }
  88. /// <summary>
  89. /// Gets the name.
  90. /// </summary>
  91. /// <value>The name.</value>
  92. public override string Name
  93. {
  94. get { return "MpcHc"; }
  95. }
  96. /// <summary>
  97. /// Gets a value indicating whether this instance can close automatically.
  98. /// </summary>
  99. /// <value><c>true</c> if this instance can close automatically; otherwise, <c>false</c>.</value>
  100. protected override bool CanCloseAutomatically
  101. {
  102. get
  103. {
  104. return true;
  105. }
  106. }
  107. /// <summary>
  108. /// Determines whether this instance can play the specified item.
  109. /// </summary>
  110. /// <param name="item">The item.</param>
  111. /// <returns><c>true</c> if this instance can play the specified item; otherwise, <c>false</c>.</returns>
  112. public override bool CanPlay(DtoBaseItem item)
  113. {
  114. return item.IsVideo || item.IsAudio;
  115. }
  116. /// <summary>
  117. /// Gets the command arguments.
  118. /// </summary>
  119. /// <param name="items">The items.</param>
  120. /// <param name="options">The options.</param>
  121. /// <param name="playerConfiguration">The player configuration.</param>
  122. /// <returns>System.String.</returns>
  123. protected override string GetCommandArguments(List<DtoBaseItem> items, PlayOptions options, PlayerConfiguration playerConfiguration)
  124. {
  125. var formatString = "{0} /play /fullscreen /close";
  126. var firstItem = items[0];
  127. var startTicks = Math.Max(options.StartPositionTicks, 0);
  128. if (startTicks > 0 && firstItem.IsVideo && firstItem.VideoType.HasValue && firstItem.VideoType.Value == VideoType.Dvd)
  129. {
  130. formatString += " /dvdpos 1#" + TimeSpan.FromTicks(startTicks).ToString("hh\\:mm\\:ss");
  131. }
  132. else
  133. {
  134. formatString += " /start " + TimeSpan.FromTicks(startTicks).TotalMilliseconds;
  135. }
  136. return GetCommandArguments(items, formatString);
  137. }
  138. /// <summary>
  139. /// Gets the path for command line.
  140. /// </summary>
  141. /// <param name="item">The item.</param>
  142. /// <returns>System.String.</returns>
  143. protected override string GetPathForCommandLine(DtoBaseItem item)
  144. {
  145. var path = base.GetPathForCommandLine(item);
  146. if (item.IsVideo && item.VideoType.HasValue)
  147. {
  148. if (item.VideoType.Value == VideoType.Dvd)
  149. {
  150. // Point directly to the video_ts path
  151. // Otherwise mpc will play any other media files that might exist in the dvd top folder (e.g. video backdrops).
  152. var videoTsPath = Path.Combine(path, "video_ts");
  153. if (Directory.Exists(videoTsPath))
  154. {
  155. path = videoTsPath;
  156. }
  157. }
  158. if (item.VideoType.Value == VideoType.BluRay)
  159. {
  160. // Point directly to the bdmv path
  161. var bdmvPath = Path.Combine(path, "bdmv");
  162. if (Directory.Exists(bdmvPath))
  163. {
  164. path = bdmvPath;
  165. }
  166. }
  167. }
  168. return FormatPath(path);
  169. }
  170. /// <summary>
  171. /// Formats the path.
  172. /// </summary>
  173. /// <param name="path">The path.</param>
  174. /// <returns>System.String.</returns>
  175. private string FormatPath(string path)
  176. {
  177. if (path.EndsWith(":\\", StringComparison.OrdinalIgnoreCase))
  178. {
  179. path = path.TrimEnd('\\');
  180. }
  181. return path;
  182. }
  183. /// <summary>
  184. /// Called when [external player launched].
  185. /// </summary>
  186. protected override void OnExternalPlayerLaunched()
  187. {
  188. base.OnExternalPlayerLaunched();
  189. ReloadStatusUpdateTimer();
  190. }
  191. /// <summary>
  192. /// Reloads the status update timer.
  193. /// </summary>
  194. private void ReloadStatusUpdateTimer()
  195. {
  196. DisposeStatusTimer();
  197. HttpInterfaceCancellationTokenSource = new CancellationTokenSource();
  198. StatusUpdateTimer = new Timer(OnStatusUpdateTimerStopped, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
  199. }
  200. /// <summary>
  201. /// Called when [status update timer stopped].
  202. /// </summary>
  203. /// <param name="state">The state.</param>
  204. private async void OnStatusUpdateTimerStopped(object state)
  205. {
  206. try
  207. {
  208. var token = HttpInterfaceCancellationTokenSource.Token;
  209. using (var stream = await UIKernel.Instance.HttpManager.Get(StatusUrl, MpcHttpInterfaceResourcePool, token).ConfigureAwait(false))
  210. {
  211. token.ThrowIfCancellationRequested();
  212. using (var reader = new StreamReader(stream))
  213. {
  214. token.ThrowIfCancellationRequested();
  215. var result = await reader.ReadToEndAsync().ConfigureAwait(false);
  216. token.ThrowIfCancellationRequested();
  217. ProcessStatusResult(result);
  218. }
  219. }
  220. }
  221. catch (HttpRequestException ex)
  222. {
  223. Logger.ErrorException("Error connecting to MpcHc status interface", ex);
  224. }
  225. catch (OperationCanceledException)
  226. {
  227. // Manually cancelled by us
  228. Logger.Info("Status request cancelled");
  229. }
  230. }
  231. /// <summary>
  232. /// Processes the status result.
  233. /// </summary>
  234. /// <param name="result">The result.</param>
  235. private async void ProcessStatusResult(string result)
  236. {
  237. // Sample result
  238. // OnStatus('test.avi', 'Playing', 5292, '00:00:05', 1203090, '00:20:03', 0, 100, 'C:\test.avi')
  239. // 5292 = position in ms
  240. // 00:00:05 = position
  241. // 1203090 = duration in ms
  242. // 00:20:03 = duration
  243. var quoteChar = result.IndexOf(", \"", StringComparison.OrdinalIgnoreCase) == -1 ? '\'' : '\"';
  244. // Strip off the leading "OnStatus(" and the trailing ")"
  245. result = result.Substring(result.IndexOf(quoteChar));
  246. result = result.Substring(0, result.LastIndexOf(quoteChar));
  247. // Strip off the filename at the beginning
  248. result = result.Substring(result.IndexOf(string.Format("{0}, {0}", quoteChar), StringComparison.OrdinalIgnoreCase) + 3);
  249. // Find the last index of ", '" so that we can extract and then strip off the file path at the end.
  250. var lastIndexOfSeparator = result.LastIndexOf(", " + quoteChar, StringComparison.OrdinalIgnoreCase);
  251. // Get the current playing file path
  252. var currentPlayingFile = result.Substring(lastIndexOfSeparator + 2).Trim(quoteChar);
  253. // Strip off the current playing file path
  254. result = result.Substring(0, lastIndexOfSeparator);
  255. var values = result.Split(',').Select(v => v.Trim().Trim(quoteChar)).ToList();
  256. var currentPositionTicks = TimeSpan.FromMilliseconds(double.Parse(values[1])).Ticks;
  257. //var currentDurationTicks = TimeSpan.FromMilliseconds(double.Parse(values[3])).Ticks;
  258. var playstate = values[0];
  259. var playlistIndex = GetPlaylistIndex(currentPlayingFile);
  260. if (playstate.Equals("stopped", StringComparison.OrdinalIgnoreCase))
  261. {
  262. if (HasStartedPlaying)
  263. {
  264. await ClosePlayer().ConfigureAwait(false);
  265. }
  266. }
  267. else
  268. {
  269. lock (stateSyncLock)
  270. {
  271. if (_currentPlaylistIndex != playlistIndex)
  272. {
  273. OnMediaChanged(_currentPlaylistIndex, _currentPositionTicks, playlistIndex);
  274. }
  275. _currentPositionTicks = currentPositionTicks;
  276. _currentPlaylistIndex = playlistIndex;
  277. }
  278. if (playstate.Equals("playing", StringComparison.OrdinalIgnoreCase))
  279. {
  280. HasStartedPlaying = true;
  281. PlayState = PlayState.Playing;
  282. }
  283. else if (playstate.Equals("paused", StringComparison.OrdinalIgnoreCase))
  284. {
  285. HasStartedPlaying = true;
  286. PlayState = PlayState.Paused;
  287. }
  288. }
  289. }
  290. /// <summary>
  291. /// Gets the index of the playlist.
  292. /// </summary>
  293. /// <param name="nowPlayingPath">The now playing path.</param>
  294. /// <returns>System.Int32.</returns>
  295. private int GetPlaylistIndex(string nowPlayingPath)
  296. {
  297. for (var i = 0; i < Playlist.Count; i++)
  298. {
  299. var item = Playlist[i];
  300. var pathArg = GetPathForCommandLine(item);
  301. if (pathArg.Equals(nowPlayingPath, StringComparison.OrdinalIgnoreCase))
  302. {
  303. return i;
  304. }
  305. if (item.VideoType.HasValue)
  306. {
  307. if (item.VideoType.Value == VideoType.BluRay || item.VideoType.Value == VideoType.Dvd || item.VideoType.Value == VideoType.HdDvd)
  308. {
  309. if (nowPlayingPath.StartsWith(pathArg, StringComparison.OrdinalIgnoreCase))
  310. {
  311. return i;
  312. }
  313. }
  314. }
  315. }
  316. return -1;
  317. }
  318. /// <summary>
  319. /// Called when [player stopped internal].
  320. /// </summary>
  321. protected override void OnPlayerStoppedInternal()
  322. {
  323. HttpInterfaceCancellationTokenSource.Cancel();
  324. DisposeStatusTimer();
  325. _currentPositionTicks = null;
  326. _currentPlaylistIndex = 0;
  327. HasStartedPlaying = false;
  328. HttpInterfaceCancellationTokenSource = null;
  329. base.OnPlayerStoppedInternal();
  330. }
  331. /// <summary>
  332. /// Disposes the status timer.
  333. /// </summary>
  334. private void DisposeStatusTimer()
  335. {
  336. if (StatusUpdateTimer != null)
  337. {
  338. StatusUpdateTimer.Dispose();
  339. }
  340. }
  341. /// <summary>
  342. /// Releases unmanaged and - optionally - managed resources.
  343. /// </summary>
  344. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  345. protected override void Dispose(bool dispose)
  346. {
  347. if (dispose)
  348. {
  349. DisposeStatusTimer();
  350. MpcHttpInterfaceResourcePool.Dispose();
  351. }
  352. base.Dispose(dispose);
  353. }
  354. /// <summary>
  355. /// Seeks the internal.
  356. /// </summary>
  357. /// <param name="positionTicks">The position ticks.</param>
  358. /// <returns>Task.</returns>
  359. protected override Task SeekInternal(long positionTicks)
  360. {
  361. var additionalParams = new Dictionary<string, string>();
  362. var time = TimeSpan.FromTicks(positionTicks);
  363. var timeString = time.Hours + ":" + time.Minutes + ":" + time.Seconds;
  364. additionalParams.Add("position", timeString);
  365. return SendCommandToPlayer("-1", additionalParams);
  366. }
  367. /// <summary>
  368. /// Pauses the internal.
  369. /// </summary>
  370. /// <returns>Task.</returns>
  371. protected override Task PauseInternal()
  372. {
  373. return SendCommandToPlayer("888", new Dictionary<string, string>());
  374. }
  375. /// <summary>
  376. /// Uns the pause internal.
  377. /// </summary>
  378. /// <returns>Task.</returns>
  379. protected override Task UnPauseInternal()
  380. {
  381. return SendCommandToPlayer("887", new Dictionary<string, string>());
  382. }
  383. /// <summary>
  384. /// Stops the internal.
  385. /// </summary>
  386. /// <returns>Task.</returns>
  387. protected override Task StopInternal()
  388. {
  389. return SendCommandToPlayer("890", new Dictionary<string, string>());
  390. }
  391. /// <summary>
  392. /// Closes the player.
  393. /// </summary>
  394. /// <returns>Task.</returns>
  395. protected Task ClosePlayer()
  396. {
  397. return SendCommandToPlayer("816", new Dictionary<string, string>());
  398. }
  399. /// <summary>
  400. /// Sends a command to MPC using the HTTP interface
  401. /// http://www.autohotkey.net/~specter333/MPC/HTTP%20Commands.txt
  402. /// </summary>
  403. /// <param name="commandNumber">The command number.</param>
  404. /// <param name="additionalParams">The additional params.</param>
  405. /// <returns>Task.</returns>
  406. /// <exception cref="System.ArgumentNullException">commandNumber</exception>
  407. private async Task SendCommandToPlayer(string commandNumber, Dictionary<string, string> additionalParams)
  408. {
  409. if (string.IsNullOrEmpty(commandNumber))
  410. {
  411. throw new ArgumentNullException("commandNumber");
  412. }
  413. if (additionalParams == null)
  414. {
  415. throw new ArgumentNullException("additionalParams");
  416. }
  417. var url = CommandUrl + "?wm_command=" + commandNumber;
  418. url = additionalParams.Keys.Aggregate(url, (current, name) => current + ("&" + name + "=" + additionalParams[name]));
  419. Logger.Info("Sending command to MPC: " + url);
  420. try
  421. {
  422. using (var stream = await UIKernel.Instance.HttpManager.Get(url, MpcHttpInterfaceResourcePool, HttpInterfaceCancellationTokenSource.Token).ConfigureAwait(false))
  423. {
  424. }
  425. }
  426. catch (HttpRequestException ex)
  427. {
  428. Logger.ErrorException("Error connecting to MpcHc command interface", ex);
  429. }
  430. catch (OperationCanceledException)
  431. {
  432. // Manually cancelled by us
  433. Logger.Info("Command request cancelled");
  434. }
  435. }
  436. /// <summary>
  437. /// Gets a value indicating whether this instance can pause.
  438. /// </summary>
  439. /// <value><c>true</c> if this instance can pause; otherwise, <c>false</c>.</value>
  440. public override bool CanPause
  441. {
  442. get
  443. {
  444. return true;
  445. }
  446. }
  447. /// <summary>
  448. /// Gets the server name that the http interface will be running on
  449. /// </summary>
  450. /// <value>The HTTP server.</value>
  451. private string HttpServer
  452. {
  453. get
  454. {
  455. return "localhost";
  456. }
  457. }
  458. /// <summary>
  459. /// Gets the port that the web interface will be running on
  460. /// </summary>
  461. /// <value>The HTTP port.</value>
  462. private string HttpPort
  463. {
  464. get
  465. {
  466. return "13579";
  467. }
  468. }
  469. /// <summary>
  470. /// Gets the url of that will be called to for status
  471. /// </summary>
  472. /// <value>The status URL.</value>
  473. private string StatusUrl
  474. {
  475. get
  476. {
  477. return "http://" + HttpServer + ":" + HttpPort + "/status.html";
  478. }
  479. }
  480. /// <summary>
  481. /// Gets the url of that will be called to send commands
  482. /// </summary>
  483. /// <value>The command URL.</value>
  484. private string CommandUrl
  485. {
  486. get
  487. {
  488. return "http://" + HttpServer + ":" + HttpPort + "/command.html";
  489. }
  490. }
  491. }
  492. }