jellyfin/MediaBrowser.Server.Mac/Native/BaseMonoApp.cs

276 lines
7.2 KiB
C#
Raw Normal View History

2014-11-26 20:23:52 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Logging;
using MediaBrowser.Server.Startup.Common;
using Mono.Unix.Native;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
2016-02-02 20:03:16 +00:00
using MediaBrowser.Controller.Power;
2016-05-23 18:04:06 +00:00
using MediaBrowser.Server.Implementations.Persistence;
2016-04-30 04:27:55 +00:00
using MediaBrowser.Server.Startup.Common.FFMpeg;
2016-04-30 05:35:53 +00:00
using System.Diagnostics;
2014-11-26 20:23:52 +00:00
namespace MediaBrowser.Server.Mac
{
public abstract class BaseMonoApp : INativeApp
{
2016-05-23 18:03:32 +00:00
protected ILogger Logger { get; private set; }
protected BaseMonoApp(ILogger logger)
{
Logger = logger;
}
2014-11-26 20:23:52 +00:00
/// <summary>
/// Shutdowns this instance.
/// </summary>
public abstract void Shutdown();
/// <summary>
/// Restarts this instance.
/// </summary>
2015-06-07 19:13:12 +00:00
public virtual void Restart(StartupOptions options)
2014-11-26 20:23:52 +00:00
{
2015-05-23 22:01:13 +00:00
throw new NotImplementedException();
2014-11-26 20:23:52 +00:00
}
/// <summary>
/// Determines whether this instance [can self restart].
/// </summary>
/// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
2015-05-23 22:01:13 +00:00
public virtual bool CanSelfRestart
2014-11-26 20:23:52 +00:00
{
get
{
return false;
}
}
2016-05-23 18:03:32 +00:00
public IDbConnector GetDbConnector()
{
return new DbConnector(Logger);
}
2014-11-26 20:23:52 +00:00
2015-10-27 04:13:12 +00:00
public virtual bool SupportsLibraryMonitor
{
get
{
2016-03-03 07:07:26 +00:00
return true;
2015-10-27 04:13:12 +00:00
}
}
2014-11-26 20:23:52 +00:00
/// <summary>
/// Gets a value indicating whether this instance can self update.
/// </summary>
/// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
public bool CanSelfUpdate
{
get
{
return false;
}
}
public bool SupportsAutoRunAtStartup
{
get { return false; }
}
public void PreventSystemStandby()
{
}
public List<Assembly> GetAssembliesWithParts()
{
var list = new List<Assembly>();
list.Add(GetType().Assembly);
return list;
}
2016-03-27 23:11:27 +00:00
public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
2014-11-26 20:23:52 +00:00
{
}
private NativeEnvironment _nativeEnvironment;
public NativeEnvironment Environment
{
get { return _nativeEnvironment ?? (_nativeEnvironment = GetEnvironmentInfo()); }
}
public bool SupportsRunningAsService
{
get
{
return false;
}
}
public bool IsRunningAsService
{
get
{
return false;
}
}
public void ConfigureAutoRun(bool autorun)
{
}
2016-04-30 04:21:35 +00:00
public void LaunchUrl(string url)
{
2016-04-30 05:35:53 +00:00
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = url
},
EnableRaisingEvents = true,
};
process.Exited += ProcessExited;
2016-04-30 20:54:11 +00:00
process.Start();
2016-04-30 05:35:53 +00:00
}
/// <summary>
/// Processes the exited.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
private static void ProcessExited(object sender, EventArgs e)
{
((Process)sender).Dispose();
2016-04-30 04:21:35 +00:00
}
2016-04-30 04:24:45 +00:00
public FFMpegInstallInfo GetFfmpegInstallInfo()
{
return GetInfo(Environment);
}
2016-04-30 04:27:55 +00:00
public static FFMpegInstallInfo GetInfo(NativeEnvironment environment)
2016-04-30 04:24:45 +00:00
{
2016-04-30 04:27:55 +00:00
var info = new FFMpegInstallInfo();
2016-04-30 04:24:45 +00:00
info.ArchiveType = "7z";
switch (environment.SystemArchitecture)
{
case Architecture.X86_X64:
info.Version = "20160124";
break;
case Architecture.X86:
info.Version = "20150110";
break;
}
info.DownloadUrls = GetDownloadUrls(environment);
return info;
}
private static string[] GetDownloadUrls(NativeEnvironment environment)
{
switch (environment.SystemArchitecture)
{
case Architecture.X86_X64:
return new[]
{
"https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x64-2.8.5.7z"
};
case Architecture.X86:
return new[]
{
"https://github.com/MediaBrowser/Emby.Resources/raw/master/ffmpeg/osx/ffmpeg-x86-2.5.3.7z"
};
}
// No version available
return new string[] { };
}
2014-11-26 20:23:52 +00:00
public INetworkManager CreateNetworkManager(ILogger logger)
{
return new NetworkManager(logger);
}
2016-02-02 20:03:16 +00:00
public IPowerManagement GetPowerManagement()
{
return new NullPowerManagement ();
}
2014-11-26 20:23:52 +00:00
private NativeEnvironment GetEnvironmentInfo()
{
var info = new NativeEnvironment
{
OperatingSystem = Startup.Common.OperatingSystem.Linux
};
var uname = GetUnixName();
var sysName = uname.sysname ?? string.Empty;
info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
var archX86 = new Regex("(i|I)[3-6]86");
if (archX86.IsMatch(uname.machine))
{
info.SystemArchitecture = Architecture.X86;
}
else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
{
info.SystemArchitecture = Architecture.X86_X64;
}
else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
{
info.SystemArchitecture = Architecture.Arm;
}
info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
System.Environment.OSVersion.VersionString :
sysName;
return info;
}
private Uname _unixName;
private Uname GetUnixName()
{
if (_unixName == null)
{
var uname = new Uname();
Utsname utsname;
var callResult = Syscall.uname(out utsname);
if (callResult == 0)
{
uname.sysname = utsname.sysname;
uname.machine = utsname.machine;
}
_unixName = uname;
}
return _unixName;
}
private class Uname
{
public string sysname = string.Empty;
public string machine = string.Empty;
}
2016-02-02 20:03:16 +00:00
private class NullPowerManagement : IPowerManagement
{
public void ScheduleWake(DateTime utcTime)
{
throw new NotImplementedException ();
}
}
2014-11-26 20:23:52 +00:00
}
}