BaseMonoApp.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Model.Logging;
  3. using MediaBrowser.Server.Startup.Common;
  4. using Mono.Unix.Native;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. using System.Text.RegularExpressions;
  9. using MediaBrowser.Controller.Power;
  10. using MediaBrowser.Server.Implementations.Persistence;
  11. using MediaBrowser.Server.Startup.Common.FFMpeg;
  12. using System.Diagnostics;
  13. namespace MediaBrowser.Server.Mac
  14. {
  15. public abstract class BaseMonoApp : INativeApp
  16. {
  17. protected ILogger Logger { get; private set; }
  18. protected BaseMonoApp(ILogger logger)
  19. {
  20. Logger = logger;
  21. }
  22. /// <summary>
  23. /// Shutdowns this instance.
  24. /// </summary>
  25. public abstract void Shutdown();
  26. /// <summary>
  27. /// Restarts this instance.
  28. /// </summary>
  29. public virtual void Restart(StartupOptions options)
  30. {
  31. throw new NotImplementedException();
  32. }
  33. /// <summary>
  34. /// Determines whether this instance [can self restart].
  35. /// </summary>
  36. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  37. public virtual bool CanSelfRestart
  38. {
  39. get
  40. {
  41. return false;
  42. }
  43. }
  44. public IDbConnector GetDbConnector()
  45. {
  46. return new DbConnector(Logger);
  47. }
  48. public virtual bool SupportsLibraryMonitor
  49. {
  50. get
  51. {
  52. return true;
  53. }
  54. }
  55. /// <summary>
  56. /// Gets a value indicating whether this instance can self update.
  57. /// </summary>
  58. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  59. public bool CanSelfUpdate
  60. {
  61. get
  62. {
  63. return false;
  64. }
  65. }
  66. public bool SupportsAutoRunAtStartup
  67. {
  68. get { return false; }
  69. }
  70. public void PreventSystemStandby()
  71. {
  72. }
  73. public List<Assembly> GetAssembliesWithParts()
  74. {
  75. var list = new List<Assembly>();
  76. list.Add(GetType().Assembly);
  77. return list;
  78. }
  79. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  80. {
  81. }
  82. private NativeEnvironment _nativeEnvironment;
  83. public NativeEnvironment Environment
  84. {
  85. get { return _nativeEnvironment ?? (_nativeEnvironment = GetEnvironmentInfo()); }
  86. }
  87. public bool SupportsRunningAsService
  88. {
  89. get
  90. {
  91. return false;
  92. }
  93. }
  94. public bool IsRunningAsService
  95. {
  96. get
  97. {
  98. return false;
  99. }
  100. }
  101. public void ConfigureAutoRun(bool autorun)
  102. {
  103. }
  104. public void LaunchUrl(string url)
  105. {
  106. var process = new Process
  107. {
  108. StartInfo = new ProcessStartInfo
  109. {
  110. FileName = url
  111. },
  112. EnableRaisingEvents = true,
  113. };
  114. process.Exited += ProcessExited;
  115. process.Start();
  116. }
  117. /// <summary>
  118. /// Processes the exited.
  119. /// </summary>
  120. /// <param name="sender">The sender.</param>
  121. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  122. private static void ProcessExited(object sender, EventArgs e)
  123. {
  124. ((Process)sender).Dispose();
  125. }
  126. public FFMpegInstallInfo GetFfmpegInstallInfo()
  127. {
  128. return GetInfo(Environment);
  129. }
  130. public static FFMpegInstallInfo GetInfo(NativeEnvironment environment)
  131. {
  132. var info = new FFMpegInstallInfo();
  133. info.ArchiveType = "7z";
  134. switch (environment.SystemArchitecture)
  135. {
  136. case Architecture.X86_X64:
  137. info.Version = "20160124";
  138. break;
  139. case Architecture.X86:
  140. info.Version = "20150110";
  141. break;
  142. }
  143. info.DownloadUrls = GetDownloadUrls(environment);
  144. return info;
  145. }
  146. private static string[] GetDownloadUrls(NativeEnvironment environment)
  147. {
  148. switch (environment.SystemArchitecture)
  149. {
  150. case Architecture.X86_X64:
  151. return new[]
  152. {
  153. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x64-2.8.5.7z"
  154. };
  155. case Architecture.X86:
  156. return new[]
  157. {
  158. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x86-2.5.3.7z"
  159. };
  160. }
  161. // No version available
  162. return new string[] { };
  163. }
  164. public INetworkManager CreateNetworkManager(ILogger logger)
  165. {
  166. return new NetworkManager(logger);
  167. }
  168. public IPowerManagement GetPowerManagement()
  169. {
  170. return new NullPowerManagement ();
  171. }
  172. private NativeEnvironment GetEnvironmentInfo()
  173. {
  174. var info = new NativeEnvironment
  175. {
  176. OperatingSystem = Startup.Common.OperatingSystem.Linux
  177. };
  178. var uname = GetUnixName();
  179. var sysName = uname.sysname ?? string.Empty;
  180. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  181. var archX86 = new Regex("(i|I)[3-6]86");
  182. if (archX86.IsMatch(uname.machine))
  183. {
  184. info.SystemArchitecture = Architecture.X86;
  185. }
  186. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  187. {
  188. info.SystemArchitecture = Architecture.X86_X64;
  189. }
  190. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  191. {
  192. info.SystemArchitecture = Architecture.Arm;
  193. }
  194. info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
  195. System.Environment.OSVersion.VersionString :
  196. sysName;
  197. return info;
  198. }
  199. private Uname _unixName;
  200. private Uname GetUnixName()
  201. {
  202. if (_unixName == null)
  203. {
  204. var uname = new Uname();
  205. Utsname utsname;
  206. var callResult = Syscall.uname(out utsname);
  207. if (callResult == 0)
  208. {
  209. uname.sysname = utsname.sysname;
  210. uname.machine = utsname.machine;
  211. }
  212. _unixName = uname;
  213. }
  214. return _unixName;
  215. }
  216. private class Uname
  217. {
  218. public string sysname = string.Empty;
  219. public string machine = string.Empty;
  220. }
  221. private class NullPowerManagement : IPowerManagement
  222. {
  223. public void ScheduleWake(DateTime utcTime)
  224. {
  225. throw new NotImplementedException ();
  226. }
  227. }
  228. }
  229. }