BaseMonoApp.cs 6.9 KB

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