BaseMonoApp.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.IsoMounter;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Server.Mono.Networking;
  5. using MediaBrowser.Server.Startup.Common;
  6. using Mono.Unix.Native;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10. using System.Text.RegularExpressions;
  11. using MediaBrowser.Controller.Power;
  12. using MediaBrowser.Server.Startup.Common.FFMpeg;
  13. using OperatingSystem = MediaBrowser.Server.Startup.Common.OperatingSystem;
  14. namespace MediaBrowser.Server.Mono.Native
  15. {
  16. public abstract class BaseMonoApp : INativeApp
  17. {
  18. protected StartupOptions StartupOptions { get; private set; }
  19. protected BaseMonoApp(StartupOptions startupOptions)
  20. {
  21. StartupOptions = startupOptions;
  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 startupOptions)
  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. /// <summary>
  46. /// Gets a value indicating whether this instance can self update.
  47. /// </summary>
  48. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  49. public bool CanSelfUpdate
  50. {
  51. get
  52. {
  53. return false;
  54. }
  55. }
  56. public bool SupportsAutoRunAtStartup
  57. {
  58. get { return false; }
  59. }
  60. public void PreventSystemStandby()
  61. {
  62. }
  63. public void AllowSystemStandby()
  64. {
  65. }
  66. public List<Assembly> GetAssembliesWithParts()
  67. {
  68. var list = new List<Assembly>();
  69. if (Environment.OperatingSystem == Startup.Common.OperatingSystem.Linux)
  70. {
  71. list.AddRange(GetLinuxAssemblies());
  72. }
  73. list.Add(GetType().Assembly);
  74. return list;
  75. }
  76. private IEnumerable<Assembly> GetLinuxAssemblies()
  77. {
  78. var list = new List<Assembly>();
  79. list.Add(typeof(LinuxIsoManager).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 bool SupportsLibraryMonitor
  105. {
  106. get
  107. {
  108. return Environment.OperatingSystem != Startup.Common.OperatingSystem.Osx;
  109. }
  110. }
  111. public void ConfigureAutoRun(bool autorun)
  112. {
  113. }
  114. public INetworkManager CreateNetworkManager(ILogger logger)
  115. {
  116. return new NetworkManager(logger);
  117. }
  118. private NativeEnvironment GetEnvironmentInfo()
  119. {
  120. var info = new NativeEnvironment
  121. {
  122. OperatingSystem = Startup.Common.OperatingSystem.Linux
  123. };
  124. var uname = GetUnixName();
  125. var sysName = uname.sysname ?? string.Empty;
  126. if (string.Equals(sysName, "Darwin", StringComparison.OrdinalIgnoreCase))
  127. {
  128. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  129. }
  130. else if (string.Equals(sysName, "Linux", StringComparison.OrdinalIgnoreCase))
  131. {
  132. info.OperatingSystem = Startup.Common.OperatingSystem.Linux;
  133. }
  134. else if (string.Equals(sysName, "BSD", StringComparison.OrdinalIgnoreCase))
  135. {
  136. info.OperatingSystem = Startup.Common.OperatingSystem.Bsd;
  137. }
  138. var archX86 = new Regex("(i|I)[3-6]86");
  139. if (archX86.IsMatch(uname.machine))
  140. {
  141. info.SystemArchitecture = Architecture.X86;
  142. }
  143. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  144. {
  145. info.SystemArchitecture = Architecture.X86_X64;
  146. }
  147. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  148. {
  149. info.SystemArchitecture = Architecture.Arm;
  150. }
  151. info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
  152. System.Environment.OSVersion.VersionString :
  153. sysName;
  154. return info;
  155. }
  156. private Uname _unixName;
  157. private Uname GetUnixName()
  158. {
  159. if (_unixName == null)
  160. {
  161. var uname = new Uname();
  162. Utsname utsname;
  163. var callResult = Syscall.uname(out utsname);
  164. if (callResult == 0)
  165. {
  166. uname.sysname = utsname.sysname;
  167. uname.machine = utsname.machine;
  168. }
  169. _unixName = uname;
  170. }
  171. return _unixName;
  172. }
  173. public class Uname
  174. {
  175. public string sysname = string.Empty;
  176. public string machine = string.Empty;
  177. }
  178. public IPowerManagement GetPowerManagement()
  179. {
  180. return new NullPowerManagement();
  181. }
  182. public FFMpegInstallInfo GetFfmpegInstallInfo()
  183. {
  184. return GetInfo(Environment);
  185. }
  186. public static FFMpegInstallInfo GetInfo(NativeEnvironment environment)
  187. {
  188. var info = new FFMpegInstallInfo();
  189. // Windows builds: http://ffmpeg.zeranoe.com/builds/
  190. // Linux builds: http://johnvansickle.com/ffmpeg/
  191. // OS X builds: http://ffmpegmac.net/
  192. // OS X x64: http://www.evermeet.cx/ffmpeg/
  193. switch (environment.OperatingSystem)
  194. {
  195. case OperatingSystem.Bsd:
  196. break;
  197. case OperatingSystem.Linux:
  198. info.ArchiveType = "7z";
  199. info.Version = "20160215";
  200. break;
  201. case OperatingSystem.Osx:
  202. info.ArchiveType = "7z";
  203. switch (environment.SystemArchitecture)
  204. {
  205. case Architecture.X86_X64:
  206. info.Version = "20160124";
  207. break;
  208. case Architecture.X86:
  209. info.Version = "20150110";
  210. break;
  211. }
  212. break;
  213. }
  214. info.DownloadUrls = GetDownloadUrls(environment);
  215. return info;
  216. }
  217. private static string[] GetDownloadUrls(NativeEnvironment environment)
  218. {
  219. switch (environment.OperatingSystem)
  220. {
  221. case OperatingSystem.Osx:
  222. switch (environment.SystemArchitecture)
  223. {
  224. case Architecture.X86_X64:
  225. return new[]
  226. {
  227. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x64-2.8.5.7z"
  228. };
  229. case Architecture.X86:
  230. return new[]
  231. {
  232. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x86-2.5.3.7z"
  233. };
  234. }
  235. break;
  236. case OperatingSystem.Linux:
  237. switch (environment.SystemArchitecture)
  238. {
  239. case Architecture.X86_X64:
  240. return new[]
  241. {
  242. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/linux/ffmpeg-git-20160215-64bit-static.7z"
  243. };
  244. case Architecture.X86:
  245. return new[]
  246. {
  247. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/linux/ffmpeg-git-20160215-32bit-static.7z"
  248. };
  249. case Architecture.Arm:
  250. return new[]
  251. {
  252. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/linux/ffmpeg-arm.7z"
  253. };
  254. }
  255. break;
  256. }
  257. // No version available
  258. return new string[] { };
  259. }
  260. }
  261. public class NullPowerManagement : IPowerManagement
  262. {
  263. public void ScheduleWake(DateTime utcTime)
  264. {
  265. throw new NotImplementedException();
  266. }
  267. }
  268. }