BaseMonoApp.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /// <summary>
  17. /// Shutdowns this instance.
  18. /// </summary>
  19. public abstract void Shutdown();
  20. /// <summary>
  21. /// Restarts this instance.
  22. /// </summary>
  23. public virtual void Restart(StartupOptions options)
  24. {
  25. throw new NotImplementedException();
  26. }
  27. /// <summary>
  28. /// Determines whether this instance [can self restart].
  29. /// </summary>
  30. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  31. public virtual bool CanSelfRestart
  32. {
  33. get
  34. {
  35. return false;
  36. }
  37. }
  38. public virtual bool SupportsLibraryMonitor
  39. {
  40. get
  41. {
  42. return true;
  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 List<Assembly> GetAssembliesWithParts()
  64. {
  65. var list = new List<Assembly>();
  66. list.Add(GetType().Assembly);
  67. return list;
  68. }
  69. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  70. {
  71. }
  72. private NativeEnvironment _nativeEnvironment;
  73. public NativeEnvironment Environment
  74. {
  75. get { return _nativeEnvironment ?? (_nativeEnvironment = GetEnvironmentInfo()); }
  76. }
  77. public bool SupportsRunningAsService
  78. {
  79. get
  80. {
  81. return false;
  82. }
  83. }
  84. public bool IsRunningAsService
  85. {
  86. get
  87. {
  88. return false;
  89. }
  90. }
  91. public void ConfigureAutoRun(bool autorun)
  92. {
  93. }
  94. public void LaunchUrl(string url)
  95. {
  96. var process = new Process
  97. {
  98. StartInfo = new ProcessStartInfo
  99. {
  100. FileName = url
  101. },
  102. EnableRaisingEvents = true,
  103. };
  104. process.Exited += ProcessExited;
  105. try
  106. {
  107. process.Start();
  108. }
  109. catch (Exception ex)
  110. {
  111. _logger.ErrorException("Error launching url: {0}", ex, url);
  112. throw;
  113. }
  114. }
  115. /// <summary>
  116. /// Processes the exited.
  117. /// </summary>
  118. /// <param name="sender">The sender.</param>
  119. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  120. private static void ProcessExited(object sender, EventArgs e)
  121. {
  122. ((Process)sender).Dispose();
  123. }
  124. public FFMpegInstallInfo GetFfmpegInstallInfo()
  125. {
  126. return GetInfo(Environment);
  127. }
  128. public static FFMpegInstallInfo GetInfo(NativeEnvironment environment)
  129. {
  130. var info = new FFMpegInstallInfo();
  131. info.ArchiveType = "7z";
  132. switch (environment.SystemArchitecture)
  133. {
  134. case Architecture.X86_X64:
  135. info.Version = "20160124";
  136. break;
  137. case Architecture.X86:
  138. info.Version = "20150110";
  139. break;
  140. }
  141. info.DownloadUrls = GetDownloadUrls(environment);
  142. return info;
  143. }
  144. private static string[] GetDownloadUrls(NativeEnvironment environment)
  145. {
  146. switch (environment.SystemArchitecture)
  147. {
  148. case Architecture.X86_X64:
  149. return new[]
  150. {
  151. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x64-2.8.5.7z"
  152. };
  153. case Architecture.X86:
  154. return new[]
  155. {
  156. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x86-2.5.3.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 IPowerManagement GetPowerManagement()
  167. {
  168. return new NullPowerManagement ();
  169. }
  170. private NativeEnvironment GetEnvironmentInfo()
  171. {
  172. var info = new NativeEnvironment
  173. {
  174. OperatingSystem = Startup.Common.OperatingSystem.Linux
  175. };
  176. var uname = GetUnixName();
  177. var sysName = uname.sysname ?? string.Empty;
  178. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  179. var archX86 = new Regex("(i|I)[3-6]86");
  180. if (archX86.IsMatch(uname.machine))
  181. {
  182. info.SystemArchitecture = Architecture.X86;
  183. }
  184. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  185. {
  186. info.SystemArchitecture = Architecture.X86_X64;
  187. }
  188. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  189. {
  190. info.SystemArchitecture = Architecture.Arm;
  191. }
  192. info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
  193. System.Environment.OSVersion.VersionString :
  194. sysName;
  195. return info;
  196. }
  197. private Uname _unixName;
  198. private Uname GetUnixName()
  199. {
  200. if (_unixName == null)
  201. {
  202. var uname = new Uname();
  203. Utsname utsname;
  204. var callResult = Syscall.uname(out utsname);
  205. if (callResult == 0)
  206. {
  207. uname.sysname = utsname.sysname;
  208. uname.machine = utsname.machine;
  209. }
  210. _unixName = uname;
  211. }
  212. return _unixName;
  213. }
  214. private class Uname
  215. {
  216. public string sysname = string.Empty;
  217. public string machine = string.Empty;
  218. }
  219. private class NullPowerManagement : IPowerManagement
  220. {
  221. public void ScheduleWake(DateTime utcTime)
  222. {
  223. throw new NotImplementedException ();
  224. }
  225. }
  226. }
  227. }