Skip to content

Commit 94cb611

Browse files
committed
Input Standardization Part 1
Add scaffolding Add initializers Add internal and external state mapping Remove duplicate status enum and add inline docs to external status Add inline API docs to PHAsset-based MuxUpload constructor
1 parent d92513b commit 94cb611

File tree

13 files changed

+461
-35
lines changed

13 files changed

+461
-35
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// UploadInputInspectionWorker.swift
3+
//
4+
5+
import AVFoundation
6+
import Foundation
7+
8+
class UploadInputInspectionWorker {
9+
10+
var sourceInput: AVAsset?
11+
12+
13+
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// UploadInputInspector.swift
3+
//
4+
5+
import AVFoundation
6+
import Foundation
7+
8+
class UploadInputInspector {
9+
10+
}
11+
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// UploadInputStandardizationWorker.swift
3+
//
4+
5+
import AVFoundation
6+
import Foundation
7+
8+
class UploadInputStandardizationWorker {
9+
10+
var sourceInput: AVAsset?
11+
12+
var standardizedInput: AVAsset?
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// UploadInputStandardizer.swift
3+
//
4+
5+
import AVFoundation
6+
import Foundation
7+
8+
class UploadInputStandardizer {
9+
10+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// UploadInput.swift
3+
//
4+
5+
import AVFoundation
6+
import Foundation
7+
8+
// Internal representation of the upload input as it is
9+
// undergoing non-standard
10+
struct UploadInput {
11+
12+
internal enum Status {
13+
case ready(AVAsset)
14+
case started(AVAsset)
15+
case underInspection(AVAsset)
16+
case standardizing(AVAsset)
17+
case standardizationSucceeded(AVAsset)
18+
case standardizationFailed(AVAsset)
19+
case awaitingUploadConfirmation(UploadInfo)
20+
case uploadInProgress(UploadInfo)
21+
case uploadPaused(UploadInfo)
22+
case uploadSucceeded(UploadInfo)
23+
case uploadFailed(UploadInfo)
24+
}
25+
26+
var status: Status
27+
28+
var sourceAsset: AVAsset {
29+
switch status {
30+
case .ready(let sourceAsset):
31+
return sourceAsset
32+
case .started(let sourceAsset):
33+
return sourceAsset
34+
case .underInspection(let sourceAsset):
35+
return sourceAsset
36+
case .standardizing(let sourceAsset):
37+
return sourceAsset
38+
case .standardizationSucceeded(let sourceAsset):
39+
return sourceAsset
40+
case .standardizationFailed(let sourceAsset):
41+
return sourceAsset
42+
case .awaitingUploadConfirmation(let uploadInfo):
43+
return uploadInfo.sourceAsset()
44+
case .uploadInProgress(let uploadInfo):
45+
return uploadInfo.sourceAsset()
46+
case .uploadSucceeded(let uploadInfo):
47+
return uploadInfo.sourceAsset()
48+
case .uploadFailed(let uploadInfo):
49+
return uploadInfo.sourceAsset()
50+
case .uploadPaused(let uploadInfo):
51+
return uploadInfo.sourceAsset()
52+
}
53+
}
54+
55+
var uploadInfo: UploadInfo? {
56+
switch status {
57+
case .ready:
58+
return nil
59+
case .started:
60+
return nil
61+
case .underInspection:
62+
return nil
63+
case .standardizing:
64+
return nil
65+
case .standardizationSucceeded:
66+
return nil
67+
case .standardizationFailed:
68+
return nil
69+
case .awaitingUploadConfirmation(let uploadInfo):
70+
return uploadInfo
71+
case .uploadInProgress(let uploadInfo):
72+
return uploadInfo
73+
case .uploadPaused(let uploadInfo):
74+
return uploadInfo
75+
case .uploadSucceeded(let uploadInfo):
76+
return uploadInfo
77+
case .uploadFailed(let uploadInfo):
78+
return uploadInfo
79+
}
80+
}
81+
82+
83+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// MuxUpload+AVFoundation.swift
3+
//
4+
5+
import AVFoundation
6+
import Foundation
7+
8+
extension MuxUpload {
9+
10+
public convenience init(
11+
uploadURL: URL,
12+
inputAsset: AVAsset,
13+
transportSettings: TransportSettings = TransportSettings(),
14+
optOutOfEventTracking: Bool,
15+
standardizationSettings: StandardizationSettings = .enabled(resolution: .preset1920x1080)
16+
) {
17+
18+
let input = UploadInput(status: .ready(inputAsset))
19+
20+
self.init(
21+
input: input,
22+
transportSettings: transportSettings,
23+
optOutOfEventTracking: optOutOfEventTracking,
24+
standardizationSettings: standardizationSettings,
25+
uploadManager: .shared
26+
)
27+
}
28+
29+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// MuxUpload+PhotosKit.swift
3+
//
4+
5+
import Foundation
6+
import Photos
7+
8+
extension MuxUpload {
9+
/// Asynchronously creates a MuxUpload using a ``PHAsset``
10+
///
11+
/// - Parameters:
12+
/// - imageManager: Photos image manager
13+
/// - inputAsset: the upload input asset, its
14+
/// ``PHAsset.mediaType`` needs to be ``PHAssetMediaType.video``
15+
/// - uploadURL: the direct upload URL
16+
/// - transportSettings: upload transport settings
17+
/// - optOutOfEventTracking: opts out of SDK event reporting
18+
/// - standardizationSettings: if enabled the SDK checks
19+
/// if the input file is in a standard input format,
20+
/// attempts local conversion if it is non-standard
21+
/// - completion: receives the initialized MuxUpload
22+
/// when it is ready, receives nil if initialization
23+
/// failed
24+
public static func makeByExporting(
25+
from imageManager: PHImageManager = .default(),
26+
using inputAsset: PHAsset,
27+
requestOptions: PHVideoRequestOptions,
28+
uploadURL: URL,
29+
transportsettings: TransportSettings = TransportSettings(),
30+
optOutOfEventTracking: Bool,
31+
standardizationSettings: StandardizationSettings = .enabled(resolution: .preset1920x1080),
32+
completion: @escaping (MuxUpload?) -> ()
33+
) {
34+
35+
if inputAsset.mediaType != .video {
36+
completion(nil)
37+
return
38+
}
39+
40+
imageManager
41+
.requestAVAsset(
42+
forVideo: inputAsset,
43+
options: requestOptions
44+
) { asset, audioMix, params in
45+
let upload: MuxUpload? = asset.map { unwrappedAsset in
46+
MuxUpload(
47+
uploadURL: uploadURL,
48+
inputAsset: unwrappedAsset,
49+
transportSettings: transportsettings,
50+
optOutOfEventTracking: optOutOfEventTracking,
51+
standardizationSettings: standardizationSettings
52+
)
53+
}
54+
55+
completion(upload)
56+
}
57+
}
58+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// MuxUpload+Transport.swift
3+
//
4+
5+
import Foundation
6+
7+
extension MuxUpload {
8+
9+
/// Transport-related settings
10+
public struct TransportSettings {
11+
/// // Google recommends *at least* 8M
12+
public var chunkSize: Int
13+
public var retriesPerChunk: Int
14+
15+
public init(
16+
chunkSize: Int = 8 * 1024 * 1024,
17+
retriesPerChunk: Int = 3
18+
) {
19+
self.chunkSize = chunkSize
20+
self.retriesPerChunk = retriesPerChunk
21+
}
22+
}
23+
24+
}

0 commit comments

Comments
 (0)