Skip to content

Commit 59f78cd

Browse files
committed
Input standardization 3
Standardize via export session
1 parent f4796ef commit 59f78cd

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

Sources/MuxUploadSDK/InputStandardization/UploadInputStandardizationWorker.swift

+55
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,64 @@
55
import AVFoundation
66
import Foundation
77

8+
protocol Standardizable { }
9+
10+
extension AVAsset: Standardizable { }
11+
12+
enum StandardizationResult {
13+
case success(standardizedAsset: AVAsset)
14+
case failure(error: Error)
15+
}
16+
17+
enum StandardizationStrategy {
18+
// Prefer using export session whenever possible
19+
case exportSession
20+
}
21+
822
class UploadInputStandardizationWorker {
923

1024
var sourceInput: AVAsset?
1125

1226
var standardizedInput: AVAsset?
27+
28+
func standardize(
29+
sourceAsset: AVAsset,
30+
outputURL: URL,
31+
completion: @escaping (AVAsset, AVAsset?, URL?, Bool) -> ()
32+
) {
33+
34+
let availableExportPresets = AVAssetExportSession.allExportPresets()
35+
36+
guard availableExportPresets.contains(where: {
37+
$0 == AVAssetExportPreset1280x720
38+
}) else {
39+
// TODO: Use VideoToolbox if export preset unavailable
40+
completion(sourceAsset, nil, nil, false)
41+
return
42+
}
43+
44+
guard let exportSession = AVAssetExportSession(
45+
asset: sourceAsset,
46+
presetName: AVAssetExportPreset1280x720
47+
) else {
48+
// TODO: Use VideoToolbox if export session fails to initialize
49+
completion(sourceAsset, nil, nil, false)
50+
return
51+
}
52+
53+
exportSession.outputFileType = .mp4
54+
exportSession.outputURL = outputURL
55+
56+
// TODO: Use Swift Concurrency
57+
exportSession.exportAsynchronously {
58+
if let exportError = exportSession.error {
59+
completion(sourceAsset, nil, nil, false)
60+
} else if let standardizedAssetURL = exportSession.outputURL {
61+
let standardizedAsset = AVAsset(url: standardizedAssetURL)
62+
completion(sourceAsset, standardizedAsset, outputURL, true)
63+
} else {
64+
completion(sourceAsset, nil, nil, false)
65+
}
66+
}
67+
}
1368
}

Sources/MuxUploadSDK/InputStandardization/UploadInputStandardizer.swift

+27-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,31 @@ import AVFoundation
66
import Foundation
77

88
class UploadInputStandardizer {
9-
9+
var workers: [String: UploadInputStandardizationWorker] = [:]
10+
11+
var fileManager: FileManager = .default
12+
13+
func standardize(
14+
id: String,
15+
sourceAsset: AVAsset,
16+
completion: @escaping (AVAsset, AVAsset?, URL?, Bool) -> ()
17+
) {
18+
let worker = UploadInputStandardizationWorker()
19+
20+
// TODO: inject Date() for testing purposes
21+
let outputFileName = "upload-\(Date().timeIntervalSince1970)"
22+
23+
let temporaryDirectory = fileManager.temporaryDirectory
24+
let temporaryOutputURL = URL(
25+
fileURLWithPath: outputFileName,
26+
relativeTo: temporaryDirectory
27+
)
28+
29+
worker.standardize(
30+
sourceAsset: sourceAsset,
31+
outputURL: temporaryOutputURL,
32+
completion: completion
33+
)
34+
workers[id] = worker
35+
}
1036
}

Sources/MuxUploadSDK/PublicAPI/MuxUpload.swift

+17-2
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ public final class MuxUpload : Hashable, Equatable {
474474

475475
switch inspectionResult {
476476
case .inspectionFailure:
477+
// TODO: Request upload confirmation
478+
// before proceeding
477479
self.startNetworkTransport(videoFile: videoFile)
478480
case .standard:
479481
self.startNetworkTransport(videoFile: videoFile)
@@ -488,8 +490,21 @@ public final class MuxUpload : Hashable, Equatable {
488490
"""
489491
)
490492

491-
// Skip format standardization
492-
self.startNetworkTransport(videoFile: videoFile)
493+
self.inputStandardizer.standardize(
494+
id: self.id,
495+
sourceAsset: AVAsset(url: videoFile)
496+
) { sourceAsset, standardizedAsset, outputURL, success in
497+
498+
if let outputURL, success {
499+
self.startNetworkTransport(
500+
videoFile: outputURL
501+
)
502+
} else {
503+
self.startNetworkTransport(
504+
videoFile: videoFile
505+
)
506+
}
507+
}
493508
}
494509
}
495510
}

0 commit comments

Comments
 (0)