BaseMonoApp.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.Startup.Common.FFMpeg;
  11. using System.Diagnostics;
  12. namespace MediaBrowser.Server.Mac
  13. {
  14. public abstract class BaseMonoApp : INativeApp
  15. {
  16. protected ILogger Logger { get; private set; }
  17. protected BaseMonoApp(ILogger logger)
  18. {
  19. Logger = logger;
  20. }
  21. /// <summary>
  22. /// Shutdowns this instance.
  23. /// </summary>
  24. public abstract void Shutdown();
  25. /// <summary>
  26. /// Restarts this instance.
  27. /// </summary>
  28. public virtual void Restart(StartupOptions options)
  29. {
  30. throw new NotImplementedException();
  31. }
  32. /// <summary>
  33. /// Determines whether this instance [can self restart].
  34. /// </summary>
  35. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  36. public virtual bool CanSelfRestart
  37. {
  38. get
  39. {
  40. return false;
  41. }
  42. }
  43. public IDbConnector GetDbConnector()
  44. {
  45. return new DbConnector(Logger);
  46. }
  47. public virtual bool SupportsLibraryMonitor
  48. {
  49. get
  50. {
  51. return true;
  52. }
  53. }
  54. /// <summary>
  55. /// Gets a value indicating whether this instance can self update.
  56. /// </summary>
  57. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  58. public bool CanSelfUpdate
  59. {
  60. get
  61. {
  62. return false;
  63. }
  64. }
  65. public bool SupportsAutoRunAtStartup
  66. {
  67. get { return false; }
  68. }
  69. public void PreventSystemStandby()
  70. {
  71. }
  72. public List<Assembly> GetAssembliesWithParts()
  73. {
  74. var list = new List<Assembly>();
  75. list.Add(GetType().Assembly);
  76. return list;
  77. }
  78. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  79. {
  80. }
  81. private NativeEnvironment _nativeEnvironment;
  82. public NativeEnvironment Environment
  83. {
  84. get { return _nativeEnvironment ?? (_nativeEnvironment = GetEnvironmentInfo()); }
  85. }
  86. public bool SupportsRunningAsService
  87. {
  88. get
  89. {
  90. return false;
  91. }
  92. }
  93. public bool IsRunningAsService
  94. {
  95. get
  96. {
  97. return false;
  98. }
  99. }
  100. public void ConfigureAutoRun(bool autorun)
  101. {
  102. }
  103. public void LaunchUrl(string url)
  104. {
  105. var process = new Process
  106. {
  107. StartInfo = new ProcessStartInfo
  108. {
  109. FileName = url
  110. },
  111. EnableRaisingEvents = true,
  112. };
  113. process.Exited += ProcessExited;
  114. process.Start();
  115. }
  116. /// <summary>
  117. /// Processes the exited.
  118. /// </summary>
  119. /// <param name="sender">The sender.</param>
  120. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  121. private static void ProcessExited(object sender, EventArgs e)
  122. {
  123. ((Process)sender).Dispose();
  124. }
  125. public FFMpegInstallInfo GetFfmpegInstallInfo()
  126. {
  127. return GetInfo(Environment);
  128. }
  129. public static FFMpegInstallInfo GetInfo(NativeEnvironment environment)
  130. {
  131. var info = new FFMpegInstallInfo();
  132. info.ArchiveType = "7z";
  133. switch (environment.SystemArchitecture)
  134. {
  135. case Architecture.X86_X64:
  136. info.Version = "20160124";
  137. break;
  138. case Architecture.X86:
  139. info.Version = "20150110";
  140. break;
  141. }
  142. info.DownloadUrls = GetDownloadUrls(environment);
  143. return info;
  144. }
  145. private static string[] GetDownloadUrls(NativeEnvironment environment)
  146. {
  147. switch (environment.SystemArchitecture)
  148. {
  149. case Architecture.X86_X64:
  150. return new[]
  151. {
  152. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x64-2.8.5.7z"
  153. };
  154. case Architecture.X86:
  155. return new[]
  156. {
  157. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x86-2.5.3.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.X86_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. }