BaseMonoApp.cs 6.2 KB

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