Commit d099a8fb authored by Martin Finkel's avatar Martin Finkel

clear more utf8 allocs

parent 7f2587fe
......@@ -108,9 +108,14 @@ namespace LibVLCSharp.Shared.Helpers
internal static RendererDescription Build(this RendererDescriptionStructure s) =>
new RendererDescription(s.Name.FromUtf8(), s.LongName.FromUtf8());
/// <summary>
/// Helper method that marshals a UTF16 managed string to a UTF8 native string ptr
/// </summary>
/// <param name="str">the managed string to marshal to native</param>
/// <returns>a ptr to the UTF8 string that needs to be freed after use</returns>
internal static IntPtr ToUtf8(this string str)
{
if (string.IsNullOrEmpty(str))
if (str == null)
return IntPtr.Zero;
byte[] utf8bytes = Encoding.UTF8.GetBytes(str);
......@@ -120,6 +125,13 @@ namespace LibVLCSharp.Shared.Helpers
return ptr;
}
/// <summary>
/// Helper method that mashals a UTF8 native string ptr to a UTF16 managed string.
/// Optionally frees the native string ptr
/// </summary>
/// <param name="nativeString">the native string to marshal to managed</param>
/// <param name="libvlcFree">frees the native pointer of the libvlc string (use only for char*)</param>
/// <returns>a managed UTF16 string</returns>
internal static string FromUtf8(this IntPtr nativeString, bool libvlcFree = false)
{
if (nativeString == IntPtr.Zero)
......
......@@ -457,7 +457,8 @@ namespace LibVLCSharp.Shared
MarshalUtils.Retrieve(() =>
{
var audioOutputNameUtf8 = audioOutputName.ToUtf8();
return MarshalUtils.PerformInteropAndFree(() => Native.LibVLCAudioOutputDeviceListGet(NativeReference, audioOutputNameUtf8), audioOutputNameUtf8);
return MarshalUtils.PerformInteropAndFree(() =>
Native.LibVLCAudioOutputDeviceListGet(NativeReference, audioOutputNameUtf8), audioOutputNameUtf8);
},
MarshalUtils.PtrToStructure<AudioOutputDeviceStructure>,
s => s.Build(),
......
......@@ -295,7 +295,8 @@ namespace LibVLCSharp.Shared
{
if(string.IsNullOrEmpty(option)) throw new ArgumentNullException(nameof(option));
Native.LibVLCMediaAddOption(NativeReference, option.ToUtf8());
var optionUtf8 = option.ToUtf8();
MarshalUtils.PerformInteropAndFree(() => Native.LibVLCMediaAddOption(NativeReference, optionUtf8), optionUtf8);
}
/// <summary>
......@@ -326,7 +327,9 @@ namespace LibVLCSharp.Shared
{
if (string.IsNullOrEmpty(option)) throw new ArgumentNullException(nameof(option));
Native.LibVLCMediaAddOptionFlag(NativeReference, option.ToUtf8(), flags);
var optionUtf8 = option.ToUtf8();
MarshalUtils.PerformInteropAndFree(() => Native.LibVLCMediaAddOptionFlag(NativeReference, optionUtf8, flags), optionUtf8);
}
string _mrl;
......@@ -338,8 +341,7 @@ namespace LibVLCSharp.Shared
if (string.IsNullOrEmpty(_mrl))
{
var mrlPtr = Native.LibVLCMediaGetMrl(NativeReference);
_mrl = mrlPtr.FromUtf8();
MarshalUtils.LibVLCFree(ref mrlPtr);
_mrl = mrlPtr.FromUtf8(libvlcFree: true);
}
return _mrl;
}
......@@ -371,11 +373,12 @@ namespace LibVLCSharp.Shared
/// </summary>
/// <param name="metadataType">the <see cref="MetadataType"/> to write</param>
/// <param name="value">the media's meta</param>
public void SetMeta(MetadataType metadataType, string value)
public void SetMeta(MetadataType metadataType, string metaValue)
{
if(string.IsNullOrEmpty(value)) throw new ArgumentNullException(value);
if(string.IsNullOrEmpty(metaValue)) throw new ArgumentNullException(metaValue);
Native.LibVLCMediaSetMeta(NativeReference, metadataType, value.ToUtf8());
var metaUtf8 = metaValue.ToUtf8();
MarshalUtils.PerformInteropAndFree(() => Native.LibVLCMediaSetMeta(NativeReference, metadataType, metaUtf8), metaUtf8);
}
/// <summary>Save the meta previously set</summary>
......@@ -524,9 +527,11 @@ namespace LibVLCSharp.Shared
/// <para>libvlc_media_player_play())</para>
/// <para>LibVLC 3.0.0 and later.</para>
/// </remarks>
public bool AddSlave(MediaSlaveType type, uint priority, string uri) =>
Native.LibVLCMediaAddSlaves(NativeReference, type, priority, uri.ToUtf8()) != 0;
public bool AddSlave(MediaSlaveType type, uint priority, string uri)
{
var uriUtf8 = uri.ToUtf8();
return MarshalUtils.PerformInteropAndFree(() => Native.LibVLCMediaAddSlaves(NativeReference, type, priority, uriUtf8) != 0, uriUtf8);
}
/// <summary>
/// <para>Clear all slaves previously added by libvlc_media_slaves_add() or</para>
......
......@@ -53,7 +53,12 @@ namespace LibVLCSharp.Shared
/// <param name="libVLC">libvlc instance this will be attached to</param>
/// <param name="name">name from one of LibVLC.MediaDiscoverers</param>
public MediaDiscoverer(LibVLC libVLC, string name)
: base(() => Native.LibVLCMediaDiscovererNew(libVLC.NativeReference, name.ToUtf8()), Native.LibVLCMediaDiscovererRelease)
: base(() =>
{
var nameUtf8 = name.ToUtf8();
return MarshalUtils.PerformInteropAndFree(() =>
Native.LibVLCMediaDiscovererNew(libVLC.NativeReference, nameUtf8), nameUtf8);
}, Native.LibVLCMediaDiscovererRelease)
{
}
......
......@@ -417,11 +417,6 @@ namespace LibVLCSharp.Shared
EntryPoint = "libvlc_video_set_spu")]
internal static extern int LibVLCVideoSetSpu(IntPtr mediaPlayer, int spu);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_video_set_subtitle_file")]
internal static extern int LibVLCVideoSetSubtitleFile(IntPtr mediaPlayer, IntPtr subtitle);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_video_get_spu_delay")]
internal static extern long LibVLCVideoGetSpuDelay(IntPtr mediaPlayer);
......@@ -485,17 +480,14 @@ namespace LibVLCSharp.Shared
EntryPoint = "libvlc_video_get_track_count")]
internal static extern int LibVLCVideoGetTrackCount(IntPtr mediaPlayer);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_video_get_track_description")]
internal static extern IntPtr LibVLCVideoGetTrackDescription(IntPtr mediaPlayer);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_video_get_track")]
internal static extern int LibVLCVideoGetTrack(IntPtr mediaPlayer);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_video_set_track")]
internal static extern int LibVLCVideoSetTrack(IntPtr mediaPlayer, int track);
......@@ -509,12 +501,10 @@ namespace LibVLCSharp.Shared
EntryPoint = "libvlc_video_set_deinterlace")]
internal static extern void LibVLCVideoSetDeinterlace(IntPtr mediaPlayer, IntPtr mode);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_video_get_marquee_int")]
internal static extern int LibVLCVideoGetMarqueeInt(IntPtr mediaPlayer, VideoMarqueeOption option);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_video_get_marquee_string")]
internal static extern IntPtr LibVLCVideoGetMarqueeString(IntPtr mediaPlayer, VideoMarqueeOption option);
......@@ -551,12 +541,10 @@ namespace LibVLCSharp.Shared
EntryPoint = "libvlc_video_get_adjust_float")]
internal static extern float LibVLCVideoGetAdjustFloat(IntPtr mediaPlayer, VideoAdjustOption option);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_video_set_adjust_float")]
internal static extern void LibVLCVideoSetAdjustFloat(IntPtr mediaPlayer, VideoAdjustOption option, float value);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_media_player_add_slave")]
internal static extern int LibVLCMediaPlayerAddSlave(IntPtr mediaPlayer, MediaSlaveType mediaSlaveType,
......@@ -1030,8 +1018,12 @@ namespace LibVLCSharp.Shared
/// <param name="format">a four-characters string identifying the sample format (e.g. "S16N" or "FL32")</param>
/// <param name="rate">sample rate (expressed in Hz)</param>
/// <param name="channels">channels count</param>
public void SetAudioFormat(string format, uint rate, uint channels) => Native.LibVLCAudioSetFormat(NativeReference, format.ToUtf8(), rate, channels);
public void SetAudioFormat(string format, uint rate, uint channels)
{
var formatUtf8 = format.ToUtf8();
MarshalUtils.PerformInteropAndFree(() => Native.LibVLCAudioSetFormat(NativeReference, formatUtf8, rate, channels), formatUtf8);
}
/// <summary>
/// Selects an audio output module.
/// Note:
......@@ -1039,7 +1031,11 @@ namespace LibVLCSharp.Shared
/// </summary>
/// <param name="name">name of audio output, use psz_name of</param>
/// <returns>0 if function succeeded, -1 on error</returns>
public int SetAudioOutput(string name) => Native.LibVLCAudioOutputSet(NativeReference, name.ToUtf8());
public int SetAudioOutput(string name)
{
var nameUtf8 = name.ToUtf8();
return MarshalUtils.PerformInteropAndFree(() => Native.LibVLCAudioOutputSet(NativeReference, nameUtf8), nameUtf8);
}
// TODO
/// <summary>
......@@ -1053,7 +1049,14 @@ namespace LibVLCSharp.Shared
}
}
public void OutputDeviceSet(string deviceId, string module = null) => Native.LibVLCAudioOutputDeviceSet(NativeReference, module.ToUtf8(), deviceId.ToUtf8());
public void OutputDeviceSet(string deviceId, string module = null)
{
var deviceIdUtf8 = deviceId.ToUtf8();
var moduleUtf8 = module.ToUtf8();
MarshalUtils.PerformInteropAndFree(() =>
Native.LibVLCAudioOutputDeviceSet(NativeReference, moduleUtf8, deviceIdUtf8),
moduleUtf8, deviceIdUtf8);
}
/// <summary>
/// Toggle mute status.
......@@ -1189,7 +1192,11 @@ namespace LibVLCSharp.Shared
/// <param name="pitch">line pitch (in bytes)</param>
public void SetVideoFormat(string chroma, uint width, uint height, uint pitch)
{
Native.LibVLCVideoSetFormat(NativeReference, chroma.ToUtf8(), width, height, pitch);
var chromaUtf8 = chroma.ToUtf8();
MarshalUtils.PerformInteropAndFree(() =>
Native.LibVLCVideoSetFormat(NativeReference, chromaUtf8, width, height, pitch),
chromaUtf8);
}
/// <summary>
......@@ -1286,7 +1293,11 @@ namespace LibVLCSharp.Shared
public string AspectRatio
{
get => Native.LibVLCVideoGetAspectRatio(NativeReference).FromUtf8();
set => Native.LibVLCVideoSetAspectRatio(NativeReference, value.ToUtf8());
set
{
var aspectRatioUtf8 = value.ToUtf8();
MarshalUtils.PerformInteropAndFree(() => Native.LibVLCVideoSetAspectRatio(NativeReference, aspectRatioUtf8), aspectRatioUtf8);
}
}
public int Spu => Native.LibVLCVideoGetSpu(NativeReference);
......@@ -1312,13 +1323,6 @@ namespace LibVLCSharp.Shared
t => t.Next,
Native.LibVLCTrackDescriptionListRelease);
/// <summary>
/// Set new video subtitle file.
/// </summary>
/// <param name="subtitle">new video subtitle file</param>
/// <returns></returns>
public bool SetSubtitleFile(string subtitle) => Native.LibVLCVideoSetSubtitleFile(NativeReference, subtitle.ToUtf8()) != 0;
/// <summary>
/// Get the current subtitle delay.
/// </summary>
......@@ -1361,7 +1365,11 @@ namespace LibVLCSharp.Shared
public string CropGeometry
{
get => Native.LibVLCVideoGetCropGeometry(NativeReference).FromUtf8();
set => Native.LibVLCVideoSetCropGeometry(NativeReference, value.ToUtf8());
set
{
var cropGeometryUtf8 = value.ToUtf8();
MarshalUtils.PerformInteropAndFree(() => Native.LibVLCVideoSetCropGeometry(NativeReference, cropGeometryUtf8), cropGeometryUtf8);
}
}
/// <summary>
......@@ -1410,14 +1418,26 @@ namespace LibVLCSharp.Shared
/// <param name="width">the snapshot's width</param>
/// <param name="height">the snapshot's height</param>
/// <returns>true on success</returns>
public bool TakeSnapshot(uint num, string filePath, uint width, uint height) =>
Native.LibVLCVideoTakeSnapshot(NativeReference, num, filePath.ToUtf8(), width, height) == 0;
public bool TakeSnapshot(uint num, string filePath, uint width, uint height)
{
var filePathUtf8 = filePath.ToUtf8();
return MarshalUtils.PerformInteropAndFree(() =>
Native.LibVLCVideoTakeSnapshot(NativeReference, num, filePathUtf8, width, height) == 0,
filePathUtf8);
}
/// <summary>
/// Enable or disable deinterlace filter
/// </summary>
/// <param name="mode">type of deinterlace filter, empty string to disable</param>
public void SetDeinterlace(string mode) => Native.LibVLCVideoSetDeinterlace(NativeReference, mode.ToUtf8());
/// <param name="deinterlaceMode">type of deinterlace filter, empty string to disable</param>
public void SetDeinterlace(string deinterlaceMode)
{
var deinterlaceModeUtf8 = deinterlaceMode.ToUtf8();
MarshalUtils.PerformInteropAndFree(() =>
Native.LibVLCVideoSetDeinterlace(NativeReference, deinterlaceModeUtf8),
deinterlaceModeUtf8);
}
/// <summary>
/// Get an integer marquee option value
......@@ -1431,7 +1451,11 @@ namespace LibVLCSharp.Shared
/// </summary>
/// <param name="option">marq option to get</param>
/// <returns></returns>
public string MarqueeString(VideoMarqueeOption option) => Native.LibVLCVideoGetMarqueeString(NativeReference, option).FromUtf8();
public string MarqueeString(VideoMarqueeOption option)
{
var marqueeStrPtr = Native.LibVLCVideoGetMarqueeString(NativeReference, option);
return marqueeStrPtr.FromUtf8(libvlcFree: true);
}
/// <summary>
/// Enable, disable or set an integer marquee option
......@@ -1447,9 +1471,15 @@ namespace LibVLCSharp.Shared
/// Enable, disable or set an string marquee option
/// </summary>
/// <param name="option">marq option to set</param>
/// <param name="value">marq option value</param>
public void SetMarqueeString(VideoMarqueeOption option, string value) =>
Native.LibVLCVideoSetMarqueeString(NativeReference, option, value.ToUtf8());
/// <param name="marqueeValue">marq option value</param>
public void SetMarqueeString(VideoMarqueeOption option, string marqueeValue)
{
var marqueeValueUtf8 = marqueeValue.ToUtf8();
MarshalUtils.PerformInteropAndFree(() =>
Native.LibVLCVideoSetMarqueeString(NativeReference, option, marqueeValueUtf8),
marqueeValueUtf8);
}
/// <summary>
/// Get integer logo option.
......@@ -1471,8 +1501,15 @@ namespace LibVLCSharp.Shared
/// Set logo option as string. Options that take a different type value are ignored.
/// </summary>
/// <param name="option">logo option to set, values of libvlc_video_logo_option_t</param>
/// <param name="value">logo option value</param>
public void SetLogoString(VideoLogoOption option, string value) => Native.LibVLCVideoSetLogoString(NativeReference, option, value.ToUtf8());
/// <param name="logoValue">logo option value</param>
public void SetLogoString(VideoLogoOption option, string logoValue)
{
var logoValueUtf8 = logoValue.ToUtf8();
MarshalUtils.PerformInteropAndFree(() =>
Native.LibVLCVideoSetLogoString(NativeReference, option, logoValueUtf8),
logoValueUtf8);
}
/// <summary>
/// Get integer adjust option.
......@@ -1516,8 +1553,13 @@ namespace LibVLCSharp.Shared
/// <param name="uri">Uri of the slave (should contain a valid scheme).</param>
/// <param name="select">True if this slave should be selected when it's loaded</param>
/// <returns></returns>
public bool AddSlave(MediaSlaveType type, string uri, bool select) =>
Native.LibVLCMediaPlayerAddSlave(NativeReference, type, uri.ToUtf8(), select) == 0;
public bool AddSlave(MediaSlaveType type, string uri, bool select)
{
var uriUtf8 = uri.ToUtf8();
return MarshalUtils.PerformInteropAndFree(() =>
Native.LibVLCMediaPlayerAddSlave(NativeReference, type, uriUtf8, select) == 0,
uriUtf8);
}
/// <summary>
/// Current 360 viewpoint of this mediaplayer.
......
......@@ -41,7 +41,12 @@ namespace LibVLCSharp.Shared
/// <param name="libVLC">libvlc instance this will be connected to</param>
/// <param name="name">The service discovery protocol name depending on platform. Use LibVLC.RendererList to find the one for your platform</param>
public RendererDiscoverer(LibVLC libVLC, string name)
: base(() => Native.LibVLCRendererDiscovererNew(libVLC.NativeReference, name.ToUtf8()), Native.LibVLCRendererDiscovererRelease)
: base(() =>
{
var nameUtf8 = name.ToUtf8();
return MarshalUtils.PerformInteropAndFree(() =>
Native.LibVLCRendererDiscovererNew(libVLC.NativeReference, nameUtf8), nameUtf8);
}, Native.LibVLCRendererDiscovererRelease)
{
}
......
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