Skip to content

Commit

Permalink
fix: handle edge case where input is standard but rescaling still nec…
Browse files Browse the repository at this point in the history
…essary
  • Loading branch information
andrewjl-mux committed May 24, 2024
1 parent f421564 commit a9cb4d6
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,42 @@ struct UploadInputFormatInspectionResult {
case unsupportedPixelFormat
}

var nonStandardInputReasons: [NonstandardInputReason]
var nonStandardInputReasons: [NonstandardInputReason] = []

var isStandardInput: Bool {
nonStandardInputReasons.isEmpty
}

var maximumResolution: DirectUploadOptions.InputStandardization.MaximumResolution
struct RescalingDetails {
var maximumDesiredResolutionPreset: DirectUploadOptions.InputStandardization.MaximumResolution = .default

var recordedResolution: CMVideoDimensions = CMVideoDimensions(width: 0, height: 0)

var needsRescaling: Bool {
switch maximumDesiredResolutionPreset {
case .default, .preset1920x1080:
if max(recordedResolution.width, recordedResolution.height) > 1920 {
return true
} else {
return false
}
case .preset1280x720:
if max(recordedResolution.width, recordedResolution.height) > 1280 {
return true
} else {
return false
}
case .preset3840x2160:
if max(recordedResolution.width, recordedResolution.height) > 3840 {
return true
} else {
return false
}
}
}
}

var rescalingDetails: RescalingDetails = RescalingDetails()
}

extension UploadInputFormatInspectionResult.NonstandardInputReason: CustomStringConvertible {
Expand Down
17 changes: 10 additions & 7 deletions Sources/MuxUploadSDK/InputInspection/UploadInputInspector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ class AVFoundationUploadInputInspector: UploadInputInspector {
// Nothing to inspect, therefore nothing to standardize
// declare as already standard
completionHandler(
UploadInputFormatInspectionResult(
nonStandardInputReasons: [],
maximumResolution: maximumResolution
),
UploadInputFormatInspectionResult(),
CMTime.zero,
nil
)
Expand Down Expand Up @@ -157,8 +154,11 @@ class AVFoundationUploadInputInspector: UploadInputInspector {
if nonStandardReasons.isEmpty {
completionHandler(
UploadInputFormatInspectionResult(
nonStandardInputReasons: [],
maximumResolution: maximumResolution
nonStandardInputReasons: nonStandardReasons,
rescalingDetails: UploadInputFormatInspectionResult.RescalingDetails(
maximumDesiredResolutionPreset: maximumResolution,
recordedResolution: videoDimensions
)
),
sourceInputDuration,
nil
Expand All @@ -167,7 +167,10 @@ class AVFoundationUploadInputInspector: UploadInputInspector {
completionHandler(
UploadInputFormatInspectionResult(
nonStandardInputReasons: [],
maximumResolution: maximumResolution
rescalingDetails: UploadInputFormatInspectionResult.RescalingDetails(
maximumDesiredResolutionPreset: maximumResolution,
recordedResolution: videoDimensions
)
),
sourceInputDuration,
nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class UploadInputStandardizationWorker {

func standardize(
sourceAsset: AVAsset,
maximumResolution: DirectUploadOptions.InputStandardization.MaximumResolution,
rescalingDetails: UploadInputFormatInspectionResult.RescalingDetails,
outputURL: URL,
completion: @escaping (AVAsset, AVAsset?, Error?) -> ()
) {
Expand All @@ -52,7 +52,7 @@ class UploadInputStandardizationWorker {

let exportPreset: String

switch maximumResolution {
switch rescalingDetails.maximumDesiredResolutionPreset {
case .default:
exportPreset = AVAssetExportPreset1920x1080
case .preset1280x720:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class UploadInputStandardizer {
func standardize(
id: String,
sourceAsset: AVAsset,
maximumResolution: DirectUploadOptions.InputStandardization.MaximumResolution,
rescalingDetails: UploadInputFormatInspectionResult.RescalingDetails,
outputURL: URL,
completion: @escaping (AVAsset, AVAsset?, Error?) -> ()
) {
let worker = UploadInputStandardizationWorker()

worker.standardize(
sourceAsset: sourceAsset,
maximumResolution: maximumResolution,
rescalingDetails: rescalingDetails,
outputURL: outputURL,
completion: completion
)
Expand Down
58 changes: 54 additions & 4 deletions Sources/MuxUploadSDK/PublicAPI/DirectUpload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,59 @@ public final class DirectUpload {
)
case (.some(let result), .none):
if result.isStandardInput {
self.startNetworkTransport(
videoFile: videoFile
)

if result.rescalingDetails.needsRescaling {
SDKLogger.logger?.debug(
"Detected Input Needs Rescaling"
)

// TODO: inject Date() for testing purposes
let outputFileName = "upload-\(Date().timeIntervalSince1970)"

let outputDirectory = FileManager.default.temporaryDirectory
let outputURL = URL(
fileURLWithPath: outputFileName,
relativeTo: outputDirectory
)

self.inputStandardizer.standardize(
id: self.id,
sourceAsset: AVAsset(url: videoFile),
rescalingDetails: result.rescalingDetails,
outputURL: outputURL
) { sourceAsset, standardizedAsset, error in

if let error {

Check warning on line 512 in Sources/MuxUploadSDK/PublicAPI/DirectUpload.swift

View workflow job for this annotation

GitHub Actions / Run Unit Tests

value 'error' was defined but never used; consider replacing with boolean test
// Request upload confirmation
// before proceeding. If handler unset,
// by default do not cancel upload if
// input standardization fails
let shouldCancelUpload = self.nonStandardInputHandler?() ?? false

if !shouldCancelUpload {
self.startNetworkTransport(
videoFile: videoFile
)
} else {
self.fileWorker?.cancel()
self.uploadManager.acknowledgeUpload(id: self.id)
self.input.processUploadCancellation()
}
} else {
self.startNetworkTransport(
videoFile: outputURL,
duration: inputDuration
)
}

self.inputStandardizer.acknowledgeCompletion(id: self.id)
}

} else {
self.startNetworkTransport(
videoFile: videoFile
)
}
} else {
SDKLogger.logger?.debug(
"""
Expand All @@ -512,7 +562,7 @@ public final class DirectUpload {
self.inputStandardizer.standardize(
id: self.id,
sourceAsset: AVAsset(url: videoFile),
maximumResolution: result.maximumResolution,
rescalingDetails: result.rescalingDetails,
outputURL: outputURL
) { sourceAsset, standardizedAsset, error in

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MockUploadInputInspector: UploadInputInspector {
static let alwaysFailing: MockUploadInputInspector = MockUploadInputInspector(
mockInspectionResult: UploadInputFormatInspectionResult(
nonStandardInputReasons: [],
maximumResolution: .default
rescalingDetails: .init()
),
mockInspectionError: UploadInputInspectionError.inspectionFailure
)
Expand All @@ -26,7 +26,7 @@ class MockUploadInputInspector: UploadInputInspector {
init() {
self.mockInspectionResult = UploadInputFormatInspectionResult(
nonStandardInputReasons: [],
maximumResolution: .default
rescalingDetails: .init()
)
self.duration = .zero
}
Expand Down

0 comments on commit a9cb4d6

Please sign in to comment.