BaseMonoApp.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. process.Start();
  106. }
  107. /// <summary>
  108. /// Processes the exited.
  109. /// </summary>
  110. /// <param name="sender">The sender.</param>
  111. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  112. private static void ProcessExited(object sender, EventArgs e)
  113. {
  114. ((Process)sender).Dispose();
  115. }
  116. public FFMpegInstallInfo GetFfmpegInstallInfo()
  117. {
  118. return GetInfo(Environment);
  119. }
  120. public static FFMpegInstallInfo GetInfo(NativeEnvironment environment)
  121. {
  122. var info = new FFMpegInstallInfo();
  123. info.ArchiveType = "7z";
  124. switch (environment.SystemArchitecture)
  125. {
  126. case Architecture.X86_X64:
  127. info.Version = "20160124";
  128. break;
  129. case Architecture.X86:
  130. info.Version = "20150110";
  131. break;
  132. }
  133. info.DownloadUrls = GetDownloadUrls(environment);
  134. return info;
  135. }
  136. private static string[] GetDownloadUrls(NativeEnvironment environment)
  137. {
  138. switch (environment.SystemArchitecture)
  139. {
  140. case Architecture.X86_X64:
  141. return new[]
  142. {
  143. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x64-2.8.5.7z"
  144. };
  145. case Architecture.X86:
  146. return new[]
  147. {
  148. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x86-2.5.3.7z"
  149. };
  150. }
  151. // No version available
  152. return new string[] { };
  153. }
  154. public INetworkManager CreateNetworkManager(ILogger logger)
  155. {
  156. return new NetworkManager(logger);
  157. }
  158. public IPowerManagement GetPowerManagement()
  159. {
  160. return new NullPowerManagement ();
  161. }
  162. private NativeEnvironment GetEnvironmentInfo()
  163. {
  164. var info = new NativeEnvironment
  165. {
  166. OperatingSystem = Startup.Common.OperatingSystem.Linux
  167. };
  168. var uname = GetUnixName();
  169. var sysName = uname.sysname ?? string.Empty;
  170. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  171. var archX86 = new Regex("(i|I)[3-6]86");
  172. if (archX86.IsMatch(uname.machine))
  173. {
  174. info.SystemArchitecture = Architecture.X86;
  175. }
  176. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  177. {
  178. info.SystemArchitecture = Architecture.X86_X64;
  179. }
  180. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  181. {
  182. info.SystemArchitecture = Architecture.Arm;
  183. }
  184. info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
  185. System.Environment.OSVersion.VersionString :
  186. sysName;
  187. return info;
  188. }
  189. private Uname _unixName;
  190. private Uname GetUnixName()
  191. {
  192. if (_unixName == null)
  193. {
  194. var uname = new Uname();
  195. Utsname utsname;
  196. var callResult = Syscall.uname(out utsname);
  197. if (callResult == 0)
  198. {
  199. uname.sysname = utsname.sysname;
  200. uname.machine = utsname.machine;
  201. }
  202. _unixName = uname;
  203. }
  204. return _unixName;
  205. }
  206. private class Uname
  207. {
  208. public string sysname = string.Empty;
  209. public string machine = string.Empty;
  210. }
  211. private class NullPowerManagement : IPowerManagement
  212. {
  213. public void ScheduleWake(DateTime utcTime)
  214. {
  215. throw new NotImplementedException ();
  216. }
  217. }
  218. }
  219. }