BaseMonoApp.cs 7.2 KB

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