BaseMonoApp.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. private NativeEnvironment GetEnvironmentInfo()
  172. {
  173. var info = new NativeEnvironment
  174. {
  175. OperatingSystem = Startup.Common.OperatingSystem.Linux
  176. };
  177. var uname = GetUnixName();
  178. var sysName = uname.sysname ?? string.Empty;
  179. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  180. var archX86 = new Regex("(i|I)[3-6]86");
  181. if (archX86.IsMatch(uname.machine))
  182. {
  183. info.SystemArchitecture = Architecture.X86;
  184. }
  185. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  186. {
  187. info.SystemArchitecture = Architecture.X64;
  188. }
  189. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  190. {
  191. info.SystemArchitecture = Architecture.Arm;
  192. }
  193. info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
  194. System.Environment.OSVersion.VersionString :
  195. sysName;
  196. return info;
  197. }
  198. private Uname _unixName;
  199. private Uname GetUnixName()
  200. {
  201. if (_unixName == null)
  202. {
  203. var uname = new Uname();
  204. Utsname utsname;
  205. var callResult = Syscall.uname(out utsname);
  206. if (callResult == 0)
  207. {
  208. uname.sysname = utsname.sysname;
  209. uname.machine = utsname.machine;
  210. }
  211. _unixName = uname;
  212. }
  213. return _unixName;
  214. }
  215. private class Uname
  216. {
  217. public string sysname = string.Empty;
  218. public string machine = string.Empty;
  219. }
  220. private class NullPowerManagement : IPowerManagement
  221. {
  222. public void ScheduleWake(DateTime utcTime)
  223. {
  224. throw new NotImplementedException ();
  225. }
  226. }
  227. }
  228. }