BaseExternalPlayer.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using MediaBrowser.Common.Logging;
  2. using MediaBrowser.Model.DTO;
  3. using MediaBrowser.UI.Configuration;
  4. using MediaBrowser.UI.UserInput;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace MediaBrowser.UI.Playback.ExternalPlayer
  12. {
  13. /// <summary>
  14. /// Class BaseExternalPlayer
  15. /// </summary>
  16. public abstract class BaseExternalPlayer : BaseMediaPlayer
  17. {
  18. /// <summary>
  19. /// Gets a value indicating whether this instance can mute.
  20. /// </summary>
  21. /// <value><c>true</c> if this instance can mute; otherwise, <c>false</c>.</value>
  22. public override bool CanMute
  23. {
  24. get { return false; }
  25. }
  26. /// <summary>
  27. /// Gets a value indicating whether this instance can change volume.
  28. /// </summary>
  29. /// <value><c>true</c> if this instance can change volume; otherwise, <c>false</c>.</value>
  30. public override bool CanControlVolume
  31. {
  32. get { return false; }
  33. }
  34. /// <summary>
  35. /// Gets a value indicating whether this instance can close automatically.
  36. /// </summary>
  37. /// <value><c>true</c> if this instance can close automatically; otherwise, <c>false</c>.</value>
  38. protected virtual bool CanCloseAutomatically
  39. {
  40. get
  41. {
  42. return false;
  43. }
  44. }
  45. /// <summary>
  46. /// Gets a value indicating whether [supports multi file playback].
  47. /// </summary>
  48. /// <value><c>true</c> if [supports multi file playback]; otherwise, <c>false</c>.</value>
  49. public override bool SupportsMultiFilePlayback
  50. {
  51. get
  52. {
  53. return false;
  54. }
  55. }
  56. /// <summary>
  57. /// Gets the current process.
  58. /// </summary>
  59. /// <value>The current process.</value>
  60. protected Process CurrentProcess { get; private set; }
  61. /// <summary>
  62. /// Gets the process start info.
  63. /// </summary>
  64. /// <param name="items">The items.</param>
  65. /// <param name="options">The options.</param>
  66. /// <param name="playerConfiguration">The player configuration.</param>
  67. /// <returns>ProcessStartInfo.</returns>
  68. protected virtual ProcessStartInfo GetProcessStartInfo(List<DtoBaseItem> items, PlayOptions options, PlayerConfiguration playerConfiguration)
  69. {
  70. return new ProcessStartInfo
  71. {
  72. FileName = playerConfiguration.Command,
  73. Arguments = GetCommandArguments(items, options, playerConfiguration)
  74. };
  75. }
  76. /// <summary>
  77. /// Gets the command arguments.
  78. /// </summary>
  79. /// <param name="items">The items.</param>
  80. /// <param name="options">The options.</param>
  81. /// <param name="playerConfiguration">The player configuration.</param>
  82. /// <returns>System.String.</returns>
  83. protected virtual string GetCommandArguments(List<DtoBaseItem> items, PlayOptions options, PlayerConfiguration playerConfiguration)
  84. {
  85. var args = playerConfiguration.Args;
  86. if (string.IsNullOrEmpty(args))
  87. {
  88. return string.Empty;
  89. }
  90. return GetCommandArguments(items, args);
  91. }
  92. /// <summary>
  93. /// Gets the command arguments.
  94. /// </summary>
  95. /// <param name="items">The items.</param>
  96. /// <param name="formatString">The format string.</param>
  97. /// <returns>System.String.</returns>
  98. protected string GetCommandArguments(List<DtoBaseItem> items, string formatString)
  99. {
  100. var paths = items.Select(i => "\"" + GetPathForCommandLine(i) + "\"");
  101. return string.Format(formatString, string.Join(" ", paths.ToArray()));
  102. }
  103. /// <summary>
  104. /// Gets the path for command line.
  105. /// </summary>
  106. /// <param name="item">The item.</param>
  107. /// <returns>System.String.</returns>
  108. protected virtual string GetPathForCommandLine(DtoBaseItem item)
  109. {
  110. return item.Path;
  111. }
  112. /// <summary>
  113. /// Gets a value indicating whether this instance can queue.
  114. /// </summary>
  115. /// <value><c>true</c> if this instance can queue; otherwise, <c>false</c>.</value>
  116. public override bool CanQueue
  117. {
  118. get { return false; }
  119. }
  120. /// <summary>
  121. /// Gets a value indicating whether this instance can pause.
  122. /// </summary>
  123. /// <value><c>true</c> if this instance can pause; otherwise, <c>false</c>.</value>
  124. public override bool CanPause
  125. {
  126. get { return false; }
  127. }
  128. /// <summary>
  129. /// Gets a value indicating whether this instance can seek.
  130. /// </summary>
  131. /// <value><c>true</c> if this instance can seek; otherwise, <c>false</c>.</value>
  132. public override bool CanSeek
  133. {
  134. get { return false; }
  135. }
  136. /// <summary>
  137. /// Plays the internal.
  138. /// </summary>
  139. /// <param name="items">The items.</param>
  140. /// <param name="options">The options.</param>
  141. /// <param name="playerConfiguration">The player configuration.</param>
  142. protected override void PlayInternal(List<DtoBaseItem> items, PlayOptions options, PlayerConfiguration playerConfiguration)
  143. {
  144. CurrentProcess = new Process
  145. {
  146. EnableRaisingEvents = true,
  147. StartInfo = GetProcessStartInfo(items, options, playerConfiguration)
  148. };
  149. Logger.Info("{0} {1}", CurrentProcess.StartInfo.FileName, CurrentProcess.StartInfo.Arguments);
  150. CurrentProcess.Start();
  151. OnExternalPlayerLaunched();
  152. if (!CanCloseAutomatically)
  153. {
  154. KeyboardListener.KeyDown += KeyboardListener_KeyDown;
  155. }
  156. CurrentProcess.Exited += CurrentProcess_Exited;
  157. }
  158. /// <summary>
  159. /// Handles the KeyDown event of the KeyboardListener control.
  160. /// </summary>
  161. /// <param name="sender">The source of the event.</param>
  162. /// <param name="e">The <see cref="KeyEventArgs" /> instance containing the event data.</param>
  163. void KeyboardListener_KeyDown(object sender, KeyEventArgs e)
  164. {
  165. if (e.KeyCode == Keys.MediaStop)
  166. {
  167. var playstate = PlayState;
  168. if (playstate == PlayState.Paused || playstate == PlayState.Playing)
  169. {
  170. Stop();
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// Handles the Exited event of the CurrentProcess control.
  176. /// </summary>
  177. /// <param name="sender">The source of the event.</param>
  178. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  179. void CurrentProcess_Exited(object sender, EventArgs e)
  180. {
  181. var process = (Process)sender;
  182. process.Dispose();
  183. OnPlayerStopped(CurrentPlaylistIndex, CurrentPositionTicks);
  184. }
  185. /// <summary>
  186. /// Stops the internal.
  187. /// </summary>
  188. /// <returns>Task.</returns>
  189. protected override Task StopInternal()
  190. {
  191. return Task.Run(() => CurrentProcess.Kill());
  192. }
  193. /// <summary>
  194. /// Called when [player stopped internal].
  195. /// </summary>
  196. protected override void OnPlayerStoppedInternal()
  197. {
  198. KeyboardListener.KeyDown -= KeyboardListener_KeyDown;
  199. base.OnPlayerStoppedInternal();
  200. }
  201. /// <summary>
  202. /// Called when [external player launched].
  203. /// </summary>
  204. protected virtual void OnExternalPlayerLaunched()
  205. {
  206. }
  207. }
  208. }