Tmt5MediaPlayer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Logging;
  3. using MediaBrowser.Model.DTO;
  4. using MediaBrowser.UI.Configuration;
  5. using MediaBrowser.UI.Playback;
  6. using MediaBrowser.UI.Playback.ExternalPlayer;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.Specialized;
  10. using System.ComponentModel.Composition;
  11. using System.Diagnostics;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Plugins.Tmt5
  16. {
  17. /// <summary>
  18. /// Class GenericExternalPlayer
  19. /// </summary>
  20. [Export(typeof(BaseMediaPlayer))]
  21. public class Tmt5MediaPlayer : BaseExternalPlayer
  22. {
  23. /// <summary>
  24. /// Gets the name.
  25. /// </summary>
  26. /// <value>The name.</value>
  27. public override string Name
  28. {
  29. get { return "TMT5"; }
  30. }
  31. /// <summary>
  32. /// Gets a value indicating whether this instance can pause.
  33. /// </summary>
  34. /// <value><c>true</c> if this instance can pause; otherwise, <c>false</c>.</value>
  35. public override bool CanPause
  36. {
  37. get
  38. {
  39. return true;
  40. }
  41. }
  42. /// <summary>
  43. /// Gets a value indicating whether this instance can close automatically.
  44. /// </summary>
  45. /// <value><c>true</c> if this instance can close automatically; otherwise, <c>false</c>.</value>
  46. protected override bool CanCloseAutomatically
  47. {
  48. get
  49. {
  50. return true;
  51. }
  52. }
  53. /// <summary>
  54. /// Gets the play state directory.
  55. /// </summary>
  56. /// <value>The play state directory.</value>
  57. private string PlayStateDirectory
  58. {
  59. get
  60. {
  61. return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ArcSoft");
  62. }
  63. }
  64. /// <summary>
  65. /// The _current position ticks
  66. /// </summary>
  67. private long? _currentPositionTicks;
  68. /// <summary>
  69. /// Gets the current position ticks.
  70. /// </summary>
  71. /// <value>The current position ticks.</value>
  72. public override long? CurrentPositionTicks
  73. {
  74. get
  75. {
  76. return _currentPositionTicks;
  77. }
  78. }
  79. /// <summary>
  80. /// The _current playlist index
  81. /// </summary>
  82. private int _currentPlaylistIndex;
  83. /// <summary>
  84. /// Gets the index of the current playlist.
  85. /// </summary>
  86. /// <value>The index of the current playlist.</value>
  87. public override int CurrentPlaylistIndex
  88. {
  89. get
  90. {
  91. return _currentPlaylistIndex;
  92. }
  93. }
  94. /// <summary>
  95. /// Gets or sets the status file watcher.
  96. /// </summary>
  97. /// <value>The status file watcher.</value>
  98. private FileSystemWatcher StatusFileWatcher { get; set; }
  99. /// <summary>
  100. /// Gets or sets a value indicating whether this instance has started playing.
  101. /// </summary>
  102. /// <value><c>true</c> if this instance has started playing; otherwise, <c>false</c>.</value>
  103. private bool HasStartedPlaying { get; set; }
  104. /// <summary>
  105. /// Gets or sets a value indicating whether this instance has stopped playing.
  106. /// </summary>
  107. /// <value><c>true</c> if this instance has stopped playing; otherwise, <c>false</c>.</value>
  108. private bool HasStoppedPlaying { get; set; }
  109. /// <summary>
  110. /// Determines whether this instance can play the specified item.
  111. /// </summary>
  112. /// <param name="item">The item.</param>
  113. /// <returns><c>true</c> if this instance can play the specified item; otherwise, <c>false</c>.</returns>
  114. public override bool CanPlay(DtoBaseItem item)
  115. {
  116. return item.IsVideo || item.IsAudio;
  117. }
  118. /// <summary>
  119. /// Called when [player stopped internal].
  120. /// </summary>
  121. protected override void OnPlayerStoppedInternal()
  122. {
  123. DisposeFileSystemWatcher();
  124. HasStartedPlaying = false;
  125. HasStoppedPlaying = false;
  126. _currentPlaylistIndex = 0;
  127. _currentPositionTicks = 0;
  128. base.OnPlayerStoppedInternal();
  129. }
  130. /// <summary>
  131. /// Gets the command arguments.
  132. /// </summary>
  133. /// <param name="items">The items.</param>
  134. /// <param name="options">The options.</param>
  135. /// <param name="playerConfiguration">The player configuration.</param>
  136. /// <returns>System.String.</returns>
  137. protected override string GetCommandArguments(List<DtoBaseItem> items, PlayOptions options, PlayerConfiguration playerConfiguration)
  138. {
  139. return "\"" + items[0].Path + "\"";
  140. }
  141. /// <summary>
  142. /// Called when [external player launched].
  143. /// </summary>
  144. protected override void OnExternalPlayerLaunched()
  145. {
  146. base.OnExternalPlayerLaunched();
  147. // If the playstate directory exists, start watching it
  148. if (Directory.Exists(PlayStateDirectory))
  149. {
  150. ReloadFileSystemWatcher();
  151. }
  152. }
  153. /// <summary>
  154. /// Pauses the internal.
  155. /// </summary>
  156. /// <returns>Task.</returns>
  157. protected override Task PauseInternal()
  158. {
  159. return SendCommandToMMC("-pause");
  160. }
  161. /// <summary>
  162. /// Uns the pause internal.
  163. /// </summary>
  164. /// <returns>Task.</returns>
  165. protected override Task UnPauseInternal()
  166. {
  167. return SendCommandToMMC("-play");
  168. }
  169. /// <summary>
  170. /// Stops the internal.
  171. /// </summary>
  172. /// <returns>Task.</returns>
  173. protected override Task StopInternal()
  174. {
  175. return SendCommandToMMC("-stop");
  176. }
  177. /// <summary>
  178. /// Closes the player.
  179. /// </summary>
  180. /// <returns>Task.</returns>
  181. protected Task ClosePlayer()
  182. {
  183. return SendCommandToMMC("-close");
  184. }
  185. /// <summary>
  186. /// Seeks the internal.
  187. /// </summary>
  188. /// <param name="positionTicks">The position ticks.</param>
  189. /// <returns>Task.</returns>
  190. /// <exception cref="System.InvalidOperationException">No media to seek to</exception>
  191. protected override Task SeekInternal(long positionTicks)
  192. {
  193. if (CurrentMedia == null)
  194. {
  195. throw new InvalidOperationException("No media to seek to");
  196. }
  197. if (CurrentMedia.Chapters == null)
  198. {
  199. throw new InvalidOperationException("TMT5 cannot seek without chapter information");
  200. }
  201. var chapterIndex = 0;
  202. for (var i = 0; i < CurrentMedia.Chapters.Count; i++)
  203. {
  204. if (CurrentMedia.Chapters[i].StartPositionTicks < positionTicks)
  205. {
  206. chapterIndex = i;
  207. }
  208. }
  209. return JumpToChapter(chapterIndex);
  210. }
  211. /// <summary>
  212. /// Jumps to chapter.
  213. /// </summary>
  214. /// <param name="chapter">The chapter.</param>
  215. /// <returns>Task.</returns>
  216. protected Task JumpToChapter(int chapter)
  217. {
  218. return SendCommandToMMC(" -chapter " + chapter);
  219. }
  220. /// <summary>
  221. /// Sends an arbitrary command to the TMT MMC console
  222. /// </summary>
  223. /// <param name="command">The command.</param>
  224. /// <returns>Task.</returns>
  225. protected Task SendCommandToMMC(string command)
  226. {
  227. return Task.Run(() =>
  228. {
  229. var directory = Path.GetDirectoryName(CurrentPlayerConfiguration.Command);
  230. var processInfo = new ProcessStartInfo
  231. {
  232. FileName = Path.Combine(directory, "MMCEDT5.exe"),
  233. Arguments = command,
  234. CreateNoWindow = true
  235. };
  236. Logger.Debug("{0} {1}", processInfo.FileName, processInfo.Arguments);
  237. using (var process = Process.Start(processInfo))
  238. {
  239. process.WaitForExit(2000);
  240. }
  241. });
  242. }
  243. /// <summary>
  244. /// Reloads the file system watcher.
  245. /// </summary>
  246. private void ReloadFileSystemWatcher()
  247. {
  248. DisposeFileSystemWatcher();
  249. Logger.Info("Watching TMT folder: " + PlayStateDirectory);
  250. StatusFileWatcher = new FileSystemWatcher(PlayStateDirectory, "*.set")
  251. {
  252. IncludeSubdirectories = true
  253. };
  254. // Need to include subdirectories since there are subfolders undearneath this with the TMT version #.
  255. StatusFileWatcher.Changed += StatusFileWatcher_Changed;
  256. StatusFileWatcher.EnableRaisingEvents = true;
  257. }
  258. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  259. /// <summary>
  260. /// Handles the Changed event of the StatusFileWatcher control.
  261. /// </summary>
  262. /// <param name="sender">The source of the event.</param>
  263. /// <param name="e">The <see cref="FileSystemEventArgs" /> instance containing the event data.</param>
  264. async void StatusFileWatcher_Changed(object sender, FileSystemEventArgs e)
  265. {
  266. Logger.Debug("TMT File Watcher reports change type {1} at {0}", e.FullPath, e.ChangeType);
  267. NameValueCollection values;
  268. try
  269. {
  270. values = FileSystem.ParseIniFile(e.FullPath);
  271. }
  272. catch (IOException)
  273. {
  274. // This can happen if the file is being written to at the exact moment we're trying to access it
  275. // Unfortunately we kind of have to just eat it
  276. return;
  277. }
  278. var tmtPlayState = values["State"];
  279. if (tmtPlayState.Equals("play", StringComparison.OrdinalIgnoreCase))
  280. {
  281. PlayState = PlayState.Playing;
  282. // Playback just started
  283. HasStartedPlaying = true;
  284. if (CurrentPlayOptions.StartPositionTicks > 0)
  285. {
  286. SeekInternal(CurrentPlayOptions.StartPositionTicks);
  287. }
  288. }
  289. else if (tmtPlayState.Equals("pause", StringComparison.OrdinalIgnoreCase))
  290. {
  291. PlayState = PlayState.Paused;
  292. }
  293. // If playback has previously started...
  294. // First notify the Progress event handler
  295. // Then check if playback has stopped
  296. if (HasStartedPlaying)
  297. {
  298. TimeSpan currentPosition;
  299. //TimeSpan.TryParse(values["TotalTime"], out currentDuration);
  300. if (TimeSpan.TryParse(values["CurTime"], UsCulture, out currentPosition))
  301. {
  302. _currentPositionTicks = currentPosition.Ticks;
  303. }
  304. _currentPlaylistIndex = 0;
  305. // Playback has stopped
  306. if (tmtPlayState.Equals("stop", StringComparison.OrdinalIgnoreCase))
  307. {
  308. Logger.Info("Playstate changed to stopped");
  309. if (!HasStoppedPlaying)
  310. {
  311. HasStoppedPlaying = true;
  312. DisposeFileSystemWatcher();
  313. await ClosePlayer().ConfigureAwait(false);
  314. }
  315. }
  316. }
  317. }
  318. /// <summary>
  319. /// Disposes the file system watcher.
  320. /// </summary>
  321. private void DisposeFileSystemWatcher()
  322. {
  323. if (StatusFileWatcher != null)
  324. {
  325. StatusFileWatcher.EnableRaisingEvents = false;
  326. StatusFileWatcher.Changed -= StatusFileWatcher_Changed;
  327. StatusFileWatcher.Dispose();
  328. StatusFileWatcher = null;
  329. }
  330. }
  331. /// <summary>
  332. /// Releases unmanaged and - optionally - managed resources.
  333. /// </summary>
  334. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  335. protected override void Dispose(bool dispose)
  336. {
  337. if (dispose)
  338. {
  339. DisposeFileSystemWatcher();
  340. }
  341. base.Dispose(dispose);
  342. }
  343. }
  344. }