BaseMonoApp.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 void PreventSystemStandby()
  77. {
  78. }
  79. public List<Assembly> GetAssembliesWithParts()
  80. {
  81. var list = new List<Assembly>();
  82. list.Add(GetType().Assembly);
  83. return list;
  84. }
  85. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  86. {
  87. }
  88. private NativeEnvironment _nativeEnvironment;
  89. public NativeEnvironment Environment
  90. {
  91. get { return _nativeEnvironment ?? (_nativeEnvironment = GetEnvironmentInfo()); }
  92. }
  93. public bool SupportsRunningAsService
  94. {
  95. get
  96. {
  97. return false;
  98. }
  99. }
  100. public bool IsRunningAsService
  101. {
  102. get
  103. {
  104. return false;
  105. }
  106. }
  107. public void ConfigureAutoRun(bool autorun)
  108. {
  109. }
  110. public void LaunchUrl(string url)
  111. {
  112. var process = new Process
  113. {
  114. StartInfo = new ProcessStartInfo
  115. {
  116. FileName = url
  117. },
  118. EnableRaisingEvents = true,
  119. };
  120. process.Exited += ProcessExited;
  121. process.Start();
  122. }
  123. /// <summary>
  124. /// Processes the exited.
  125. /// </summary>
  126. /// <param name="sender">The sender.</param>
  127. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  128. private static void ProcessExited(object sender, EventArgs e)
  129. {
  130. ((Process)sender).Dispose();
  131. }
  132. public FFMpegInstallInfo GetFfmpegInstallInfo()
  133. {
  134. return GetInfo(Environment);
  135. }
  136. public static FFMpegInstallInfo GetInfo(NativeEnvironment environment)
  137. {
  138. var info = new FFMpegInstallInfo();
  139. info.ArchiveType = "7z";
  140. switch (environment.SystemArchitecture)
  141. {
  142. case Architecture.X86_X64:
  143. info.Version = "20160124";
  144. break;
  145. case Architecture.X86:
  146. info.Version = "20150110";
  147. break;
  148. }
  149. info.DownloadUrls = GetDownloadUrls(environment);
  150. return info;
  151. }
  152. private static string[] GetDownloadUrls(NativeEnvironment environment)
  153. {
  154. switch (environment.SystemArchitecture)
  155. {
  156. case Architecture.X86_X64:
  157. return new[]
  158. {
  159. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x64-2.8.5.7z"
  160. };
  161. case Architecture.X86:
  162. return new[]
  163. {
  164. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x86-2.5.3.7z"
  165. };
  166. }
  167. // No version available
  168. return new string[] { };
  169. }
  170. public INetworkManager CreateNetworkManager(ILogger logger)
  171. {
  172. return new NetworkManager(logger);
  173. }
  174. public IPowerManagement GetPowerManagement()
  175. {
  176. return new NullPowerManagement ();
  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.X86_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. }