Skip to content

Commit

Permalink
feat: use SharedStorageAccessManager as a fallback for FutureAccessLi…
Browse files Browse the repository at this point in the history
…st (#532)

Since SharedStorageAccessManager does not work with network files. Use the file path directly in those cases instead.
  • Loading branch information
huynhsontung authored Jan 8, 2025
1 parent f622247 commit 4fedf88
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
42 changes: 37 additions & 5 deletions Screenbox.Core/Services/LibVlcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Screenbox.Core.Playback;
using System;
using System.Collections.Generic;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.Storage.AccessCache;

Expand All @@ -17,13 +18,26 @@ public sealed class LibVlcService : IDisposable
public LibVLC? LibVlc { get; private set; }

private readonly NotificationService _notificationService;
private readonly bool _useFal;

public LibVlcService(INotificationService notificationService)
{
_notificationService = (NotificationService)notificationService;

// Clear FA periodically because of 1000 items limit
StorageApplicationPermissions.FutureAccessList.Clear();
// FutureAccessList is preferred because it can handle network StorageFiles
// If FutureAccessList is somehow unavailable, SharedStorageAccessManager will be the fallback
_useFal = true;

try
{
// Clear FA periodically because of 1000 items limit
StorageApplicationPermissions.FutureAccessList.Clear();
}
catch (Exception) // FileNotFoundException
{
// FutureAccessList is not available
_useFal = false;
}
}

public VlcMediaPlayer Initialize(string[] swapChainOptions)
Expand Down Expand Up @@ -61,7 +75,18 @@ private Media CreateMedia(IStorageFile file, params string[] options)
{
Guard.IsNotNull(LibVlc, nameof(LibVlc));
LibVLC libVlc = LibVlc;
string mrl = "winrt://" + StorageApplicationPermissions.FutureAccessList.Add(file, "media");
if (file is StorageFile storageFile &&
storageFile.Provider.Id.Equals("network", StringComparison.OrdinalIgnoreCase) &&
!string.IsNullOrEmpty(storageFile.Path))
{
// Optimization for network files. Avoid having to deal with WinRT quirks.
return CreateMedia(new Uri(storageFile.Path, UriKind.Absolute), options);
}

string token = _useFal
? StorageApplicationPermissions.FutureAccessList.Add(file, "media")
: SharedStorageAccessManager.AddFile(file);
string mrl = "winrt://" + token;
return new Media(libVlc, mrl, FromType.FromLocation, options);
}

Expand All @@ -72,15 +97,22 @@ private Media CreateMedia(Uri uri, params string[] options)
return new Media(libVlc, uri, options);
}

public static void DisposeMedia(Media media)
public void DisposeMedia(Media media)
{
string mrl = media.Mrl;
if (mrl.StartsWith("winrt://"))
{
string token = mrl.Substring(8);
try
{
StorageApplicationPermissions.FutureAccessList.Remove(token);
if (_useFal)
{
StorageApplicationPermissions.FutureAccessList.Remove(token);
}
else
{
SharedStorageAccessManager.RemoveFile(token);
}
}
catch (Exception)
{
Expand Down
2 changes: 1 addition & 1 deletion Screenbox.Core/ViewModels/MediaViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void Clean()
PlaybackItem? item = Item.Value;
Item = new Lazy<PlaybackItem?>(CreatePlaybackItem);
if (item == null) return;
LibVlcService.DisposeMedia(item.Media);
_libVlcService.DisposeMedia(item.Media);
}

public void UpdateSource(StorageFile file)
Expand Down

0 comments on commit 4fedf88

Please sign in to comment.