Plugin.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using MediaBrowser.Common.Plugins;
  2. using MediaBrowser.Model.Plugins;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.ComponentModel.Composition;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Api
  12. {
  13. /// <summary>
  14. /// Class Plugin
  15. /// </summary>
  16. [Export(typeof(IPlugin))]
  17. public class Plugin : BasePlugin<BasePluginConfiguration>
  18. {
  19. /// <summary>
  20. /// Gets the name of the plugin
  21. /// </summary>
  22. /// <value>The name.</value>
  23. public override string Name
  24. {
  25. get { return "Web Api"; }
  26. }
  27. /// <summary>
  28. /// Gets a value indicating whether this instance is a core plugin.
  29. /// </summary>
  30. /// <value><c>true</c> if this instance is a core plugin; otherwise, <c>false</c>.</value>
  31. public override bool IsCorePlugin
  32. {
  33. get
  34. {
  35. return true;
  36. }
  37. }
  38. /// <summary>
  39. /// Gets the instance.
  40. /// </summary>
  41. /// <value>The instance.</value>
  42. public static Plugin Instance { get; private set; }
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="Plugin" /> class.
  45. /// </summary>
  46. public Plugin()
  47. {
  48. Instance = this;
  49. }
  50. /// <summary>
  51. /// Releases unmanaged and - optionally - managed resources.
  52. /// </summary>
  53. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  54. protected override void DisposeOnServer(bool dispose)
  55. {
  56. if (dispose)
  57. {
  58. var jobCount = ActiveTranscodingJobs.Count;
  59. Parallel.ForEach(ActiveTranscodingJobs, OnTranscodeKillTimerStopped);
  60. // Try to allow for some time to kill the ffmpeg processes and delete the partial stream files
  61. if (jobCount > 0)
  62. {
  63. Thread.Sleep(1000);
  64. }
  65. }
  66. base.DisposeOnServer(dispose);
  67. }
  68. /// <summary>
  69. /// The active transcoding jobs
  70. /// </summary>
  71. private readonly List<TranscodingJob> ActiveTranscodingJobs = new List<TranscodingJob>();
  72. /// <summary>
  73. /// Called when [transcode beginning].
  74. /// </summary>
  75. /// <param name="path">The path.</param>
  76. /// <param name="type">The type.</param>
  77. /// <param name="process">The process.</param>
  78. public void OnTranscodeBeginning(string path, TranscodingJobType type, Process process)
  79. {
  80. lock (ActiveTranscodingJobs)
  81. {
  82. ActiveTranscodingJobs.Add(new TranscodingJob
  83. {
  84. Type = type,
  85. Path = path,
  86. Process = process,
  87. ActiveRequestCount = 1
  88. });
  89. }
  90. }
  91. /// <summary>
  92. /// Called when [transcode failed to start].
  93. /// </summary>
  94. /// <param name="path">The path.</param>
  95. /// <param name="type">The type.</param>
  96. public void OnTranscodeFailedToStart(string path, TranscodingJobType type)
  97. {
  98. lock (ActiveTranscodingJobs)
  99. {
  100. var job = ActiveTranscodingJobs.First(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
  101. ActiveTranscodingJobs.Remove(job);
  102. }
  103. }
  104. /// <summary>
  105. /// Determines whether [has active transcoding job] [the specified path].
  106. /// </summary>
  107. /// <param name="path">The path.</param>
  108. /// <param name="type">The type.</param>
  109. /// <returns><c>true</c> if [has active transcoding job] [the specified path]; otherwise, <c>false</c>.</returns>
  110. public bool HasActiveTranscodingJob(string path, TranscodingJobType type)
  111. {
  112. lock (ActiveTranscodingJobs)
  113. {
  114. return ActiveTranscodingJobs.Any(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
  115. }
  116. }
  117. /// <summary>
  118. /// Called when [transcode begin request].
  119. /// </summary>
  120. /// <param name="path">The path.</param>
  121. /// <param name="type">The type.</param>
  122. public void OnTranscodeBeginRequest(string path, TranscodingJobType type)
  123. {
  124. lock (ActiveTranscodingJobs)
  125. {
  126. var job = ActiveTranscodingJobs.FirstOrDefault(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
  127. if (job == null)
  128. {
  129. return;
  130. }
  131. job.ActiveRequestCount++;
  132. if (job.KillTimer != null)
  133. {
  134. job.KillTimer.Dispose();
  135. job.KillTimer = null;
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// Called when [transcode end request].
  141. /// </summary>
  142. /// <param name="path">The path.</param>
  143. /// <param name="type">The type.</param>
  144. public void OnTranscodeEndRequest(string path, TranscodingJobType type)
  145. {
  146. lock (ActiveTranscodingJobs)
  147. {
  148. var job = ActiveTranscodingJobs.FirstOrDefault(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
  149. if (job == null)
  150. {
  151. return;
  152. }
  153. job.ActiveRequestCount--;
  154. if (job.ActiveRequestCount == 0)
  155. {
  156. var timerDuration = type == TranscodingJobType.Progressive ? 1000 : 30000;
  157. if (job.KillTimer == null)
  158. {
  159. job.KillTimer = new Timer(OnTranscodeKillTimerStopped, job, timerDuration, Timeout.Infinite);
  160. }
  161. else
  162. {
  163. job.KillTimer.Change(timerDuration, Timeout.Infinite);
  164. }
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// Called when [transcoding finished].
  170. /// </summary>
  171. /// <param name="path">The path.</param>
  172. /// <param name="type">The type.</param>
  173. public void OnTranscodingFinished(string path, TranscodingJobType type)
  174. {
  175. lock (ActiveTranscodingJobs)
  176. {
  177. var job = ActiveTranscodingJobs.FirstOrDefault(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
  178. if (job == null)
  179. {
  180. return;
  181. }
  182. ActiveTranscodingJobs.Remove(job);
  183. if (job.KillTimer != null)
  184. {
  185. job.KillTimer.Dispose();
  186. job.KillTimer = null;
  187. }
  188. }
  189. }
  190. /// <summary>
  191. /// Called when [transcode kill timer stopped].
  192. /// </summary>
  193. /// <param name="state">The state.</param>
  194. private void OnTranscodeKillTimerStopped(object state)
  195. {
  196. var job = (TranscodingJob)state;
  197. lock (ActiveTranscodingJobs)
  198. {
  199. ActiveTranscodingJobs.Remove(job);
  200. if (job.KillTimer != null)
  201. {
  202. job.KillTimer.Dispose();
  203. job.KillTimer = null;
  204. }
  205. }
  206. var process = job.Process;
  207. var hasExited = true;
  208. try
  209. {
  210. hasExited = process.HasExited;
  211. }
  212. catch (Win32Exception ex)
  213. {
  214. Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
  215. }
  216. catch (InvalidOperationException ex)
  217. {
  218. Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
  219. }
  220. catch (NotSupportedException ex)
  221. {
  222. Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
  223. }
  224. if (hasExited)
  225. {
  226. return;
  227. }
  228. try
  229. {
  230. Logger.Info("Killing ffmpeg process for {0}", job.Path);
  231. process.Kill();
  232. }
  233. catch (Win32Exception ex)
  234. {
  235. Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
  236. }
  237. catch (InvalidOperationException ex)
  238. {
  239. Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
  240. }
  241. catch (NotSupportedException ex)
  242. {
  243. Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
  244. }
  245. }
  246. }
  247. /// <summary>
  248. /// Class TranscodingJob
  249. /// </summary>
  250. public class TranscodingJob
  251. {
  252. /// <summary>
  253. /// Gets or sets the path.
  254. /// </summary>
  255. /// <value>The path.</value>
  256. public string Path { get; set; }
  257. /// <summary>
  258. /// Gets or sets the type.
  259. /// </summary>
  260. /// <value>The type.</value>
  261. public TranscodingJobType Type { get; set; }
  262. /// <summary>
  263. /// Gets or sets the process.
  264. /// </summary>
  265. /// <value>The process.</value>
  266. public Process Process { get; set; }
  267. /// <summary>
  268. /// Gets or sets the active request count.
  269. /// </summary>
  270. /// <value>The active request count.</value>
  271. public int ActiveRequestCount { get; set; }
  272. /// <summary>
  273. /// Gets or sets the kill timer.
  274. /// </summary>
  275. /// <value>The kill timer.</value>
  276. public Timer KillTimer { get; set; }
  277. }
  278. /// <summary>
  279. /// Enum TranscodingJobType
  280. /// </summary>
  281. public enum TranscodingJobType
  282. {
  283. /// <summary>
  284. /// The progressive
  285. /// </summary>
  286. Progressive,
  287. /// <summary>
  288. /// The HLS
  289. /// </summary>
  290. Hls
  291. }
  292. }