Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Nextcloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -6671,7 +6671,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = NKUJUXUJ3B;
Expand Down Expand Up @@ -6738,7 +6738,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = NKUJUXUJ3B;
Expand Down
2 changes: 2 additions & 0 deletions Tests/NextcloudUnitTests/NCMediaViewerTestLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ actor NCMediaViewerTestLoader: NCMediaViewerLoading, NCMediaViewerServices {
pendingMetadata.removeValue(forKey: ocId)?.resume(returning: items[ocId])
}
func metadataForDetails(for metadata: NCMediaViewerItem) async -> NCMediaViewerItem { metadata }
/// Streams nothing, so remote audio pages keep exercising the download path.
func streamingURL(for metadata: NCMediaViewerItem) async -> URL? { nil }
func localMediaURL(for metadata: NCMediaViewerItem) async -> URL? {
originals.contains(metadata.ocId) ? URL(fileURLWithPath: "/original/\(metadata.ocId).jpg") : nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import NextcloudKit

struct NCAudioViewerContentView: View {
let metadata: NCMediaViewerItem
let localURL: URL
/// The downloaded file, or a streaming URL when the original is not on the device.
let url: URL
let previewURL: URL?
let playbackProgressTint: Color
let backgroundStyle: NCViewerBackgroundStyle
Expand All @@ -32,7 +33,7 @@ struct NCAudioViewerContentView: View {

init(
metadata: NCMediaViewerItem,
localURL: URL,
url: URL,
previewURL: URL? = nil,
backgroundStyle: NCViewerBackgroundStyle = .system,
playbackProgressTint: Color = .accentColor,
Expand All @@ -51,7 +52,7 @@ struct NCAudioViewerContentView: View {
onToggleChrome: @escaping () -> Void = {}
) {
self.metadata = metadata
self.localURL = localURL
self.url = url
self.previewURL = previewURL
self.backgroundStyle = backgroundStyle
self.playbackProgressTint = playbackProgressTint
Expand All @@ -78,13 +79,13 @@ struct NCAudioViewerContentView: View {
normalContent
}
}
.task(id: localURL) {
.task(id: url) {
guard isSelected else {
return
}

model.configurePlaybackCompletion(options: playbackOptions, onPlayNextMedia: onPlayNextMedia)
await model.load(url: localURL)
await model.load(url: url)
await consumeAutoPlayIfNeeded()
}
.onChange(of: isSelected) { _, selected in
Expand All @@ -95,7 +96,7 @@ struct NCAudioViewerContentView: View {

Task { @MainActor in
model.configurePlaybackCompletion(options: playbackOptions, onPlayNextMedia: onPlayNextMedia)
await model.load(url: localURL)
await model.load(url: url)
await consumeAutoPlayIfNeeded()
}
}
Expand Down Expand Up @@ -359,7 +360,7 @@ struct NCAudioViewerContentView: View {
// The viewer-wide stop notification also releases players belonging to
// prefetched audio pages. Recreate this page's player before autoplaying
// instead of relying on the previous preload still being alive.
await model.load(url: localURL)
await model.load(url: url)

guard shouldAutoPlay else {
return
Expand Down
14 changes: 14 additions & 0 deletions iOSClient/Viewer/NCViewerMedia/Core/NCMediaViewerLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ protocol NCMediaViewerLoading: Sendable {

func downloadMedia(for metadata: NCMediaViewerItem, onDownloadStarted: (@Sendable () async -> Void)?) async throws -> URL

/// A URL that plays the original from the server without downloading it first, or nil to download instead.
func streamingURL(for metadata: NCMediaViewerItem) async -> URL?

func localLivePhotoURL(for metadata: NCMediaViewerItem) async -> URL?

func downloadLivePhotoMedia(for metadata: NCMediaViewerItem) async -> URL?
Expand Down Expand Up @@ -151,6 +154,17 @@ final class NCMediaViewerLoader: NCMediaViewerLoading, Sendable {
try await downloadOriginal(for: metadata, isLivePhoto: false, onDownloadStarted: onDownloadStarted)
}

/// Uses the same direct download link as video playback. AVPlayer cannot use a certificate
/// the user trusted manually, so those servers keep downloading the file first.
func streamingURL(for metadata: NCMediaViewerItem) async -> URL? {
let result = await NCVideoURLResolver().getVideoURL(metadata: metadata, isLocal: false)
guard result.error == .success, let url = result.url else { return nil }

let hosts = [url.host, URL(string: metadata.urlBase)?.host].compactMap { $0 }
guard !hosts.contains(where: { NCNetworking.shared.requiresUserTrustedCertificate(for: $0) }) else { return nil }
return url
}

private func downloadOriginal(for metadata: NCMediaViewerItem, isLivePhoto: Bool, onDownloadStarted: (@Sendable () async -> Void)? = nil) async throws -> URL {
if let localURL = await localMediaURL(for: metadata) { return localURL }
try Task.checkCancellation()
Expand Down
23 changes: 17 additions & 6 deletions iOSClient/Viewer/NCViewerMedia/Core/NCMediaViewerModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ enum NCMediaViewerPageState {
case idle
case metadataMissing
case image(previewURL: URL?, localURL: URL?, livePhotoURL: URL?, progress: Double?)
case audio(localURL: URL, previewURL: URL?)
/// `url` is the downloaded file, or a streaming URL when the original is not on the device.
case audio(url: URL, previewURL: URL?)
case video(localURL: URL?, previewURL: URL?)
case downloading(previewURL: URL?, progress: Double?)
case ready(localURL: URL, previewURL: URL?)
Expand Down Expand Up @@ -742,6 +743,16 @@ final class NCMediaViewerModel: ObservableObject {
}

guard thumbnailScrubID == nil else { return }

// Audio plays from the server at once, like video; it is downloaded first only when it cannot stream.
if metadata.classFile == NKTypeClassFile.audio.rawValue,
let streamingURL = await loader.streamingURL(for: metadata) {
guard !Task.isCancelled else { return }
setState(.audio(url: streamingURL, previewURL: previewURL), for: ocId)
await loadAudioPreviewIfNeeded(metadata: metadata, localURL: streamingURL, currentPreviewURL: previewURL, for: ocId, index: index)
return
}

do {
let downloadedURL = try await loader.downloadMedia(for: metadata, onDownloadStarted: nil)
viewerDownloadedOcIds.insert(ocId)
Expand Down Expand Up @@ -866,7 +877,7 @@ final class NCMediaViewerModel: ObservableObject {
setState(.downloading(previewURL: previewURL, progress: nil), for: ocId)
return
}
setState(.audio(localURL: localURL, previewURL: previewURL), for: ocId)
setState(.audio(url: localURL, previewURL: previewURL), for: ocId)
await loadAudioPreviewIfNeeded(metadata: metadata, localURL: localURL, currentPreviewURL: previewURL, for: ocId, index: index)
default:
break
Expand Down Expand Up @@ -1057,7 +1068,7 @@ final class NCMediaViewerModel: ObservableObject {
} else if metadata.classFile == NKTypeClassFile.video.rawValue {
setState(.video(localURL: localURL, previewURL: previewURL), for: ocId)
} else if metadata.classFile == NKTypeClassFile.audio.rawValue {
setState(.audio(localURL: localURL, previewURL: previewURL), for: ocId)
setState(.audio(url: localURL, previewURL: previewURL), for: ocId)
} else {
setState(.ready(localURL: localURL, previewURL: previewURL), for: ocId)
}
Expand All @@ -1075,12 +1086,12 @@ final class NCMediaViewerModel: ObservableObject {
return
}

guard case .audio(let readyLocalURL, _) = pageState(for: ocId),
readyLocalURL == localURL else {
guard case .audio(let readyURL, _) = pageState(for: ocId),
readyURL == localURL else {
return
}

setState(.audio(localURL: localURL, previewURL: previewURL), for: ocId)
setState(.audio(url: localURL, previewURL: previewURL), for: ocId)
}

private func updatePage(ocId: String, publishRevision: Bool = true, mutation: (NCMediaViewerPageModel) -> Void) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct NCMediaViewerPreviewLoader: NCMediaViewerLoading {
func localMediaURL(for metadata: NCMediaViewerItem) async -> URL? { nil }
func previewURL(for metadata: NCMediaViewerItem, ext: String) async -> URL? { imageURL }
func downloadMedia(for metadata: NCMediaViewerItem, onDownloadStarted: (@Sendable () async -> Void)?) async throws -> URL { throw URLError(.unsupportedURL) }
func streamingURL(for metadata: NCMediaViewerItem) async -> URL? { nil }
func localLivePhotoURL(for metadata: NCMediaViewerItem) async -> URL? { nil }
func downloadLivePhotoMedia(for metadata: NCMediaViewerItem) async -> URL? { nil }
func cancelDownload(for ocId: String) async { }
Expand Down Expand Up @@ -61,7 +62,7 @@ struct NCMediaViewerPreviewLoader: NCMediaViewerLoading {
let page = model.pageModel(at: index)
page?.metadata = metadata
if metadata.classFile == NKTypeClassFile.audio.rawValue {
page?.state = .audio(localURL: URL(fileURLWithPath: "/canvas-audio.m4a"), previewURL: imageURL)
page?.state = .audio(url: URL(fileURLWithPath: "/canvas-audio.m4a"), previewURL: imageURL)
}
}
return model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ struct NCMediaViewerPageView: View {
case .video(let localURL, let previewURL):
videoStateView(localURL: localURL, previewURL: previewURL)

case .audio(let localURL, let previewURL):
audioStateView(localURL: localURL, previewURL: previewURL)
case .audio(let url, let previewURL):
audioStateView(url: url, previewURL: previewURL)

case .downloading(let previewURL, let progress):
downloadingStateView(previewURL: previewURL, progress)
Expand Down Expand Up @@ -206,11 +206,11 @@ struct NCMediaViewerPageView: View {
}

@ViewBuilder
private func audioStateView(localURL: URL, previewURL: URL?) -> some View {
private func audioStateView(url: URL, previewURL: URL?) -> some View {
if let metadata = page.metadata {
NCAudioViewerContentView(
metadata: metadata,
localURL: localURL,
url: url,
previewURL: previewURL,
backgroundStyle: backgroundStyle,
playbackProgressTint: model.playbackProgressTint(metadata.account),
Expand Down Expand Up @@ -269,7 +269,7 @@ struct NCMediaViewerPageView: View {
videoStateView(localURL: localURL, previewURL: previewURL)

case NKTypeClassFile.audio.rawValue:
audioStateView(localURL: localURL, previewURL: previewURL)
audioStateView(url: localURL, previewURL: previewURL)

default:
imageContentView(previewURL: previewURL, localURL: localURL, livePhotoURL: nil, backgroundStyle: backgroundStyle)
Expand Down
Loading