BaseMonoApp.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 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.X86_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.X86_X64:
  154. return new[]
  155. {
  156. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x64-2.8.5.7z"
  157. };
  158. case Architecture.X86:
  159. return new[]
  160. {
  161. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x86-2.5.3.7z"
  162. };
  163. }
  164. // No version available
  165. return new string[] { };
  166. }
  167. public INetworkManager CreateNetworkManager(ILogger logger)
  168. {
  169. return new NetworkManager(logger);
  170. }
  171. public IPowerManagement GetPowerManagement()
  172. {
  173. return new NullPowerManagement ();
  174. }
  175. private NativeEnvironment GetEnvironmentInfo()
  176. {
  177. var info = new NativeEnvironment
  178. {
  179. OperatingSystem = Startup.Common.OperatingSystem.Linux
  180. };
  181. var uname = GetUnixName();
  182. var sysName = uname.sysname ?? string.Empty;
  183. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  184. var archX86 = new Regex("(i|I)[3-6]86");
  185. if (archX86.IsMatch(uname.machine))
  186. {
  187. info.SystemArchitecture = Architecture.X86;
  188. }
  189. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  190. {
  191. info.SystemArchitecture = Architecture.X86_X64;
  192. }
  193. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  194. {
  195. info.SystemArchitecture = Architecture.Arm;
  196. }
  197. info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
  198. System.Environment.OSVersion.VersionString :
  199. sysName;
  200. return info;
  201. }
  202. private Uname _unixName;
  203. private Uname GetUnixName()
  204. {
  205. if (_unixName == null)
  206. {
  207. var uname = new Uname();
  208. Utsname utsname;
  209. var callResult = Syscall.uname(out utsname);
  210. if (callResult == 0)
  211. {
  212. uname.sysname = utsname.sysname;
  213. uname.machine = utsname.machine;
  214. }
  215. _unixName = uname;
  216. }
  217. return _unixName;
  218. }
  219. private class Uname
  220. {
  221. public string sysname = string.Empty;
  222. public string machine = string.Empty;
  223. }
  224. private class NullPowerManagement : IPowerManagement
  225. {
  226. public void ScheduleWake(DateTime utcTime)
  227. {
  228. throw new NotImplementedException ();
  229. }
  230. }
  231. }
  232. }