BaseMonoApp.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. namespace MediaBrowser.Server.Mac
  11. {
  12. public abstract class BaseMonoApp : INativeApp
  13. {
  14. /// <summary>
  15. /// Shutdowns this instance.
  16. /// </summary>
  17. public abstract void Shutdown();
  18. /// <summary>
  19. /// Restarts this instance.
  20. /// </summary>
  21. public virtual void Restart(StartupOptions options)
  22. {
  23. throw new NotImplementedException();
  24. }
  25. /// <summary>
  26. /// Determines whether this instance [can self restart].
  27. /// </summary>
  28. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  29. public virtual bool CanSelfRestart
  30. {
  31. get
  32. {
  33. return false;
  34. }
  35. }
  36. public virtual bool SupportsLibraryMonitor
  37. {
  38. get
  39. {
  40. return true;
  41. }
  42. }
  43. /// <summary>
  44. /// Gets a value indicating whether this instance can self update.
  45. /// </summary>
  46. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  47. public bool CanSelfUpdate
  48. {
  49. get
  50. {
  51. return false;
  52. }
  53. }
  54. public bool SupportsAutoRunAtStartup
  55. {
  56. get { return false; }
  57. }
  58. public void PreventSystemStandby()
  59. {
  60. }
  61. public List<Assembly> GetAssembliesWithParts()
  62. {
  63. var list = new List<Assembly>();
  64. list.Add(GetType().Assembly);
  65. return list;
  66. }
  67. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  68. {
  69. }
  70. private NativeEnvironment _nativeEnvironment;
  71. public NativeEnvironment Environment
  72. {
  73. get { return _nativeEnvironment ?? (_nativeEnvironment = GetEnvironmentInfo()); }
  74. }
  75. public bool SupportsRunningAsService
  76. {
  77. get
  78. {
  79. return false;
  80. }
  81. }
  82. public bool IsRunningAsService
  83. {
  84. get
  85. {
  86. return false;
  87. }
  88. }
  89. public void ConfigureAutoRun(bool autorun)
  90. {
  91. }
  92. public void LaunchUrl(string url)
  93. {
  94. throw new NotImplementedException();
  95. }
  96. public FFMpegInstallInfo GetFfmpegInstallInfo()
  97. {
  98. return GetInfo(Environment);
  99. }
  100. public static FFMpegDownloadInfo GetInfo(NativeEnvironment environment)
  101. {
  102. var info = new FFMpegDownloadInfo();
  103. info.ArchiveType = "7z";
  104. switch (environment.SystemArchitecture)
  105. {
  106. case Architecture.X86_X64:
  107. info.Version = "20160124";
  108. break;
  109. case Architecture.X86:
  110. info.Version = "20150110";
  111. break;
  112. }
  113. info.DownloadUrls = GetDownloadUrls(environment);
  114. return info;
  115. }
  116. private static string[] GetDownloadUrls(NativeEnvironment environment)
  117. {
  118. switch (environment.SystemArchitecture)
  119. {
  120. case Architecture.X86_X64:
  121. return new[]
  122. {
  123. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x64-2.8.5.7z"
  124. };
  125. case Architecture.X86:
  126. return new[]
  127. {
  128. "https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x86-2.5.3.7z"
  129. };
  130. }
  131. // No version available
  132. return new string[] { };
  133. }
  134. public INetworkManager CreateNetworkManager(ILogger logger)
  135. {
  136. return new NetworkManager(logger);
  137. }
  138. public IPowerManagement GetPowerManagement()
  139. {
  140. return new NullPowerManagement ();
  141. }
  142. private NativeEnvironment GetEnvironmentInfo()
  143. {
  144. var info = new NativeEnvironment
  145. {
  146. OperatingSystem = Startup.Common.OperatingSystem.Linux
  147. };
  148. var uname = GetUnixName();
  149. var sysName = uname.sysname ?? string.Empty;
  150. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  151. var archX86 = new Regex("(i|I)[3-6]86");
  152. if (archX86.IsMatch(uname.machine))
  153. {
  154. info.SystemArchitecture = Architecture.X86;
  155. }
  156. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  157. {
  158. info.SystemArchitecture = Architecture.X86_X64;
  159. }
  160. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  161. {
  162. info.SystemArchitecture = Architecture.Arm;
  163. }
  164. info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
  165. System.Environment.OSVersion.VersionString :
  166. sysName;
  167. return info;
  168. }
  169. private Uname _unixName;
  170. private Uname GetUnixName()
  171. {
  172. if (_unixName == null)
  173. {
  174. var uname = new Uname();
  175. Utsname utsname;
  176. var callResult = Syscall.uname(out utsname);
  177. if (callResult == 0)
  178. {
  179. uname.sysname = utsname.sysname;
  180. uname.machine = utsname.machine;
  181. }
  182. _unixName = uname;
  183. }
  184. return _unixName;
  185. }
  186. private class Uname
  187. {
  188. public string sysname = string.Empty;
  189. public string machine = string.Empty;
  190. }
  191. private class NullPowerManagement : IPowerManagement
  192. {
  193. public void ScheduleWake(DateTime utcTime)
  194. {
  195. throw new NotImplementedException ();
  196. }
  197. }
  198. }
  199. }