Skip to content

Commit

Permalink
Uplift of #26726 (squashed) to release
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-builds committed Nov 25, 2024
1 parent 05f1783 commit c66b3b0
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ extension PlaylistLegacyCarplayController {
let cacheState = PlaylistManager.shared.state(for: item.tagId)
if cacheState != .invalid {
if let index = PlaylistManager.shared.index(of: item.tagId),
let asset = PlaylistManager.shared.assetAtIndex(index)
let asset = PlaylistManager.shared.assetAtIndexSynchronous(index)
{

do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ extension PlaylistViewController: VideoViewDelegate {
let cacheState = PlaylistManager.shared.state(for: item.tagId)
if cacheState != .invalid {
if let index = PlaylistManager.shared.index(of: item.tagId),
let asset = PlaylistManager.shared.assetAtIndex(index)
let asset = PlaylistManager.shared.assetAtIndexSynchronous(index)
{

do {
Expand Down
42 changes: 31 additions & 11 deletions ios/brave-ios/Sources/Playlist/PlaylistDownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ protocol PlaylistDownloadManagerDelegate: AnyObject {

private protocol PlaylistStreamDownloadManagerDelegate: AnyObject {
// TODO: Should be async, fix when removing legacy playlist UI
func localAsset(for itemId: String) -> AVURLAsset?
func localAssetSynchronous(for itemId: String) -> AVURLAsset?
func localAsset(for itemId: String) async -> AVURLAsset?
func onDownloadProgressUpdate(streamDownloader: Any, id: String, percentComplete: Double)
func onDownloadStateChanged(
streamDownloader: Any,
Expand Down Expand Up @@ -229,7 +230,8 @@ public class PlaylistDownloadManager: PlaylistStreamDownloadManagerDelegate {

// MARK: - PlaylistStreamDownloadManagerDelegate

func localAsset(for itemId: String) -> AVURLAsset? {
@available(*, deprecated, renamed: "localAsset(for:)", message: "Use async version")
func localAssetSynchronous(for itemId: String) -> AVURLAsset? {
guard let item = PlaylistItem.getItem(uuid: itemId),
let cachedData = item.cachedData,
!cachedData.isEmpty
Expand All @@ -253,6 +255,30 @@ public class PlaylistDownloadManager: PlaylistStreamDownloadManagerDelegate {
}
}

func localAsset(for itemId: String) async -> AVURLAsset? {
let cachedData = await MainActor.run {
return PlaylistItem.getItem(uuid: itemId)?.cachedData
}
guard let cachedData = cachedData, !cachedData.isEmpty else { return nil }

var bookmarkDataIsStale = false
do {
let url = try URL(
resolvingBookmarkData: cachedData,
bookmarkDataIsStale: &bookmarkDataIsStale
)

if bookmarkDataIsStale {
return nil
}

return AVURLAsset(url: url, options: AVAsset.defaultOptions)
} catch {
Logger.module.error("\(error.localizedDescription)")
return nil
}
}

fileprivate func onDownloadProgressUpdate(
streamDownloader: Any,
id: String,
Expand Down Expand Up @@ -536,9 +562,7 @@ private class PlaylistHLSDownloadManager: NSObject, AVAssetDownloadDelegate {
// HLS streams can be in two spots, we need to delete from both
// just in case the download process was in the middle of transferring the asset
// to its proper location
if let cacheLocation = await MainActor.run(body: {
delegate?.localAsset(for: asset.id)?.url
}) {
if let cacheLocation = await delegate?.localAsset(for: asset.id)?.url {
do {
try await AsyncFileManager.default.removeItem(at: cacheLocation)
} catch {
Expand Down Expand Up @@ -741,9 +765,7 @@ private class PlaylistFileDownloadManager: NSObject, URLSessionDownloadDelegate
if let error = error as NSError? {
switch (error.domain, error.code) {
case (NSURLErrorDomain, NSURLErrorCancelled):
if let cacheLocation = await MainActor.run(body: {
delegate?.localAsset(for: asset.id)?.url
}) {
if let cacheLocation = await delegate?.localAsset(for: asset.id)?.url {
do {
try await AsyncFileManager.default.removeItem(at: cacheLocation)
PlaylistItem.updateCache(uuid: asset.id, pageSrc: asset.pageSrc, cachedData: nil)
Expand Down Expand Up @@ -1064,9 +1086,7 @@ private class PlaylistDataDownloadManager: NSObject, URLSessionDataDelegate {
if let error = error as NSError? {
switch (error.domain, error.code) {
case (NSURLErrorDomain, NSURLErrorCancelled):
if let cacheLocation = await MainActor.run(body: {
delegate?.localAsset(for: asset.id)?.url
}) {
if let cacheLocation = await delegate?.localAsset(for: asset.id)?.url {
Task {
do {
try await AsyncFileManager.default.removeItem(at: cacheLocation)
Expand Down
Loading

0 comments on commit c66b3b0

Please sign in to comment.