Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media: Internally keep a reference to the last set Media #205

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/LibVLCSharp.Tests/LibVLCAPICoverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public async Task CheckLibVLCCoverage()
List<string> notImplementedOnPurpose = new List<string>
{
"libvlc_printerr", "libvlc_vprinterr", "libvlc_clock", "libvlc_dialog_get_context", "libvlc_dialog_set_context",
"libvlc_event_type_name", "libvlc_log_get_object", "libvlc_vlm", "libvlc_media_list_player", "libvlc_media_library"
"libvlc_event_type_name", "libvlc_log_get_object", "libvlc_vlm", "libvlc_media_list_player", "libvlc_media_library",
"libvlc_media_player_get_media"
};

List<string> exclude = new List<string>();
Expand Down Expand Up @@ -141,4 +142,4 @@ public async Task CheckLibVLCCoverage()
Assert.Zero(missingApisCount);
}
}
}
}
13 changes: 13 additions & 0 deletions src/LibVLCSharp.Tests/MediaPlayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,18 @@ public void SetMediaPlayerRole()
Assert.True(mp.SetRole(MediaPlayerRole.None));
Assert.AreEqual(MediaPlayerRole.None, mp.Role);
}

[Test]
public void MediaRefCountTestFail()
{
var media1 = new Media(_libVLC, new Uri(RealStreamMediaPath));
var media2 = new Media(_libVLC, new Uri(RealStreamMediaPath));

var mp = new MediaPlayer(media1);
media1.Dispose();
Debug.WriteLine(mp.Media.Mrl);

mp.Media = media2;
}
}
}
40 changes: 27 additions & 13 deletions src/LibVLCSharp/Shared/MediaPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ readonly struct Native
internal static extern void LibVLCMediaPlayerSetMedia(IntPtr mediaPlayer, IntPtr media);


[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_media_player_get_media")]
internal static extern IntPtr LibVLCMediaPlayerGetMedia(IntPtr mediaPlayer);


[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_media_player_event_manager")]
internal static extern IntPtr LibVLCMediaPlayerEventManager(IntPtr mediaPlayer);
Expand Down Expand Up @@ -623,8 +618,11 @@ public MediaPlayer(Media media)
: base(() => Native.LibVLCMediaPlayerNewFromMedia(media.NativeReference), Native.LibVLCMediaPlayerRelease)
{
_gcHandle = GCHandle.Alloc(this);
_media = media;
}

Media? _media;

/// <summary>
/// Get the media used by the media_player.
/// Set the media that will be used by the media_player.
Expand All @@ -633,12 +631,24 @@ public MediaPlayer(Media media)
/// </summary>
public Media? Media
{
get
get => _media;
set
{
var mediaPtr = Native.LibVLCMediaPlayerGetMedia(NativeReference);
return mediaPtr == IntPtr.Zero ? null : new Media(mediaPtr);
if(_media != null)
{
_media.Dispose();
_media = null;
}

_media = value;

if(_media != null)
{
_media.Retain();
}

Native.LibVLCMediaPlayerSetMedia(NativeReference, _media?.NativeReference ?? IntPtr.Zero);
}
set => Native.LibVLCMediaPlayerSetMedia(NativeReference, value?.NativeReference ?? IntPtr.Zero);
}

/// <summary>
Expand All @@ -653,11 +663,9 @@ public Media? Media
/// <returns>true if successful</returns>
public bool Play()
{
var media = Media;
if(media != null)
if(_media != null)
{
media.AddOption(Configuration);
media.Dispose();
_media.AddOption(Configuration);
}
return Native.LibVLCMediaPlayerPlay(NativeReference) == 0;
}
Expand Down Expand Up @@ -2408,6 +2416,12 @@ protected override void Dispose(bool disposing)
{
if (_gcHandle.IsAllocated)
_gcHandle.Free();

if (_media != null)
{
_media?.Dispose();
_media = null;
}
}

base.Dispose(disposing);
Expand Down