Skip to content

Commit

Permalink
Basic multiplatform (#218)
Browse files Browse the repository at this point in the history
* Make GetPlatformId() return null on unknown platforms so static ffmpeg ctor won't fail on those.
calling ffmpeg.XXXX functions won't work on unknown platforms, but data structures are available for direct DLLIMPORT calls

* .gitignore for JetBrains Rider

* additional .NET6.0 target framework and better OS detection on those systems
  • Loading branch information
KaFo committed Aug 17, 2022
1 parent 4cba8b6 commit d2fc566
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,9 @@ UpgradeLog*.XML

*.stackdump

# JetBrains Rider
.idea/
*.sln.iml

# Local
/FFmpeg.AutoGen.Example/frame.*.jpg
2 changes: 1 addition & 1 deletion FFmpeg.AutoGen/FFmpeg.AutoGen.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;netstandard2.0;net472;net45</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0;net472;net45</TargetFrameworks>
<GeneratePackageOnBuild Condition=" $(Configuration) == 'Release' ">true</GeneratePackageOnBuild>
<Description>FFmpeg auto generated unsafe bindings for C#/.NET and Mono.</Description>
</PropertyGroup>
Expand Down
13 changes: 13 additions & 0 deletions FFmpeg.AutoGen/FFmpeg.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using FFmpeg.AutoGen.Native;

namespace FFmpeg.AutoGen
Expand Down Expand Up @@ -38,11 +39,23 @@ static ffmpeg()
{
GetOrLoadLibrary = libraryName => LoadLibrary(libraryName, true);

#if NET
//BSD has #define EAGAIN 35 in errno.h, other OS have EAGAIN 11. Apple is based on BSD
bool bsdStyleErrno =
OperatingSystem.IsFreeBSD()
|| OperatingSystem.IsMacCatalyst()
|| OperatingSystem.IsMacOS()
|| OperatingSystem.IsIOS()
|| OperatingSystem.IsTvOS()
|| OperatingSystem.IsWatchOS();
EAGAIN = bsdStyleErrno ? 35 : 11;
#else
EAGAIN = LibraryLoader.GetPlatformId() switch
{
PlatformID.MacOSX => 35,
_ => 11
};
#endif
}

/// <summary>
Expand Down
16 changes: 14 additions & 2 deletions FFmpeg.AutoGen/Native/LibraryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace FFmpeg.AutoGen.Native
{
public delegate PlatformID GetPlatformId();
public delegate PlatformID? GetPlatformId();

public delegate string GetNativeLibraryName(string libraryName, int version);

Expand All @@ -16,11 +16,23 @@ static LibraryLoader()
{
#if NET45 || NET40
return Environment.OSVersion.Platform;
#elif NET
if (OperatingSystem.IsWindows())
return PlatformID.Win32NT;
if (OperatingSystem.IsMacCatalyst()
|| OperatingSystem.IsMacOS()
|| OperatingSystem.IsIOS()
|| OperatingSystem.IsTvOS()
|| OperatingSystem.IsWatchOS())
return PlatformID.MacOSX; // all share similar .dylib calling style. But only static libs on iOS store apps!
if (OperatingSystem.IsAndroid() || OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux())
return PlatformID.Unix;
return null;
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return PlatformID.Win32NT;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return PlatformID.Unix;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) return PlatformID.MacOSX;
throw new PlatformNotSupportedException();
return null;
#endif
};

Expand Down

0 comments on commit d2fc566

Please sign in to comment.