Commit c768601f authored by Martin Finkel's avatar Martin Finkel

MediaConfiguration: Improve libvlc configuration API for HW, file/network caching

- works on all platforms
- can be set from Media and MediaPlayer objects
- nit: Defines: Replace COCOA by APPLE

Closes #132
parent 64bc64c4
......@@ -12,13 +12,13 @@
<DefineConstants>$(DefineConstants);NET471;NET</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('Xamarin.iOS'))">
<DefineConstants>$(DefineConstants);MONO;IOS;COCOA</DefineConstants>
<DefineConstants>$(DefineConstants);MONO;IOS;APPLE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('Xamarin.Mac'))">
<DefineConstants>$(DefineConstants);MONO;MAC;COCOA</DefineConstants>
<DefineConstants>$(DefineConstants);MONO;MAC;APPLE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('Xamarin.TVOS'))">
<DefineConstants>$(DefineConstants);MONO;TVOS;COCOA</DefineConstants>
<DefineConstants>$(DefineConstants);MONO;TVOS;APPLE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('MonoAndroid'))">
<DefineConstants>$(DefineConstants);MONO;ANDROID</DefineConstants>
......
......@@ -307,7 +307,10 @@ namespace LibVLCSharp.Shared
{
if (mediaConfiguration == null) throw new ArgumentNullException(nameof(mediaConfiguration));
AddOption(mediaConfiguration.Build());
foreach(var option in mediaConfiguration.Build())
{
AddOption(option);
}
}
/// <summary>Add an option to the media with configurable flags.</summary>
......@@ -1082,23 +1085,5 @@ namespace LibVLCSharp.Shared
Playlist = 5
}
#endregion
/// <summary>
/// Small configuration helper
/// </summary>
public class MediaConfiguration
{
HashSet<string> _options = new HashSet<string>();
public MediaConfiguration EnableHardwareDecoding()
{
#if ANDROID
_options.Add(":codec=mediacodec_ndk");
#endif
return this;
}
public string Build() => string.Join(",", _options);
}
}
#endregion
}
\ No newline at end of file
using System.Collections.Generic;
using System.Linq;
namespace LibVLCSharp.Shared
{
/// <summary>
/// Configuration helper designed to be used for advanced libvlc configuration
/// <para/> More info at https://wiki.videolan.org/VLC_command-line_help/
/// </summary>
public class MediaConfiguration
{
readonly Dictionary<string, string> _options = new Dictionary<string, string>
{
{ nameof(EnableHardwareDecoding), string.Empty },
{ nameof(FileCaching), string.Empty },
{ nameof(NetworkCaching), string.Empty },
};
bool _enableHardwareDecoding;
/// <summary>
/// Enable/disable hardware decoding (crossplatform).
/// </summary>
public bool EnableHardwareDecoding
{
get => _enableHardwareDecoding;
set
{
_enableHardwareDecoding = value;
_options[nameof(EnableHardwareDecoding)] = HardwareDecodingOptionString(_enableHardwareDecoding);
}
}
int _fileCaching;
/// <summary>
/// Caching value for local files, in milliseconds [0 .. 60000ms]
/// </summary>
public int FileCaching
{
get => _fileCaching;
set
{
_fileCaching = value;
_options[nameof(FileCaching)] = _fileCaching.ToString();
}
}
int _networkCaching;
/// <summary>
/// Caching value for network resources, in milliseconds [0 .. 60000ms]
/// </summary>
public int NetworkCaching
{
get => _networkCaching;
set
{
_networkCaching = value;
_options[nameof(NetworkCaching)] = _networkCaching.ToString();
}
}
#if ANDROID
const string ENABLE_HW_ANDROID = ":codec=mediacodec_ndk";
const string DISABLE_HW_ANDROID = "";
#endif
const string ENABLE_HW_APPLE = ":videotoolbox";
const string ENABLE_HW_WINDOWS = ":avcodec-hw=d3d11va";
const string DISABLE_HW_APPLE = ":no-videotoolbox";
const string DISABLE_HW_WINDOWS = ":avcodec-hw=none";
private string HardwareDecodingOptionString(bool enable)
{
if(enable)
{
#if ANDROID
return ENABLE_HW_ANDROID;
#elif APPLE
return ENABLE_HW_APPLE;
#else
if (PlatformHelper.IsWindows)
return ENABLE_HW_WINDOWS;
if (PlatformHelper.IsMac)
return ENABLE_HW_APPLE;
return string.Empty;
#endif
}
else
{
#if ANDROID
return DISABLE_HW_ANDROID;
#elif APPLE
return DISABLE_HW_APPLE;
#else
if (PlatformHelper.IsWindows)
return DISABLE_HW_WINDOWS;
if (PlatformHelper.IsMac)
return DISABLE_HW_APPLE;
return string.Empty;
#endif
}
}
/// <summary>
/// Builds the current MediaConfiguration for consumption by libvlc (or storage)
/// </summary>
/// <returns>Configured libvlc options as strings</returns>
public string[] Build() => _options.Values.Where(option => !string.IsNullOrEmpty(option)).ToArray();
}
}
\ No newline at end of file
......@@ -64,7 +64,7 @@ namespace LibVLCSharp.Shared
EntryPoint = "libvlc_media_player_stop")]
internal static extern void LibVLCMediaPlayerStop(IntPtr mediaPlayer);
#if COCOA || NET || NETSTANDARD
#if APPLE || NET || NETSTANDARD
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_media_player_set_nsobject")]
internal static extern void LibVLCMediaPlayerSetNsobject(IntPtr mediaPlayer, IntPtr drawable);
......@@ -664,7 +664,11 @@ namespace LibVLCSharp.Shared
/// If playback was already started, this method has no effect
/// </summary>
/// <returns>true if successful</returns>
public bool Play() => Native.LibVLCMediaPlayerPlay(NativeReference) == 0;
public bool Play()
{
Media?.AddOption(Configuration);
return Native.LibVLCMediaPlayerPlay(NativeReference) == 0;
}
/// <summary>
/// Set media and start playback
......@@ -697,7 +701,7 @@ namespace LibVLCSharp.Shared
/// </summary>
public void Stop() => Native.LibVLCMediaPlayerStop(NativeReference);
#if COCOA || NET || NETSTANDARD
#if APPLE || NET || NETSTANDARD
/// <summary>
/// Get the NSView handler previously set
/// return the NSView handler or 0 if none where set
......@@ -1662,6 +1666,35 @@ namespace LibVLCSharp.Shared
/// <summary>Increments the native reference counter for this mediaplayer instance</summary>
internal void Retain() => Native.LibVLCMediaPlayerRetain(NativeReference);
/// <summary>
/// Enable/disable hardware decoding in a crossplatform way.
/// </summary>
public bool EnableHardwareDecoding
{
get => Configuration.EnableHardwareDecoding;
set => Configuration.EnableHardwareDecoding = value;
}
/// <summary>
/// Caching value for local files, in milliseconds [0 .. 60000ms]
/// </summary>
public int FileCaching
{
get => Configuration.FileCaching;
set => Configuration.FileCaching = value;
}
/// <summary>
/// Caching value for network resources, in milliseconds [0 .. 60000ms]
/// </summary>
public int NetworkCaching
{
get => Configuration.NetworkCaching;
set => Configuration.NetworkCaching = value;
}
MediaConfiguration Configuration = new MediaConfiguration();
#if UNITY_ANDROID
/// <summary>
/// Retrieve a video frame from the Unity plugin.
......@@ -1676,7 +1709,7 @@ namespace LibVLCSharp.Shared
}
#endif
#region Callbacks
#region Callbacks
/// <summary>
/// <para>A LibVLC media player plays one media (usually in a custom drawable).</para>
......@@ -1879,7 +1912,8 @@ namespace LibVLCSharp.Shared
}
}
#region events
#region events
public event EventHandler<MediaPlayerMediaChangedEventArgs> MediaChanged
{
......
......@@ -29,14 +29,14 @@ namespace LibVLCSharp.Android.Sample
Core.Initialize();
_libVLC = new LibVLC();
_mediaPlayer = new MediaPlayer(_libVLC);
_mediaPlayer = new MediaPlayer(_libVLC)
{
EnableHardwareDecoding = true
};
_videoView = new VideoView(this) { MediaPlayer = _mediaPlayer };
AddContentView(_videoView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent));
var media = new Media(_libVLC, "https://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4", FromType.FromLocation);
var configuration = new MediaConfiguration();
configuration.EnableHardwareDecoding();
media.AddOption(configuration);
_videoView.MediaPlayer.Play(media);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment