-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDirectUploadOptions.swift
338 lines (291 loc) · 11.9 KB
/
DirectUploadOptions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//
// DirectUploadOptions.swift
//
import Foundation
// MARK: - Direct Upload Options
/// Options for the direct upload
public struct DirectUploadOptions {
// MARK: - Transport Options
/// Options for tuning network transport of direct upload
/// chunks to Mux. Using the ``default`` is recommended
/// for most applications.
public struct Transport {
/// The size of each file chunk in bytes sent by the
/// SDK during an upload. At least 8MB is recommended.
public var chunkSizeInBytes: Int
/// Number of retry attempts per chunk if its upload
/// request is unsuccessful
public var retryLimitPerChunk: Int
/// Default options for ``DirectUpload`` chunk transport
/// over the network. The chunk size is 8MB and the
/// per-chunk retry limit is 3.
public static var `default`: Transport {
Transport(
chunkSizeInBytes: 8 * 1024 * 1024,
retryLimitPerChunk: 3
)
}
/// Initializes options for transport of upload chunks
/// over the network
/// - Parameters:
/// - chunkSize: the size of each file chunk sent
/// by the SDK during an upload.
/// Defaults to 8MB.
/// - retryLimitPerChunk: number of times a failed
/// chunk request is retried. Default limit is
/// 3 retries.
public init(
chunkSize: Measurement<UnitInformationStorage> = .defaultDirectUploadChunkSize,
retryLimitPerChunk: Int = 3
) {
self.chunkSizeInBytes = Int(
abs(chunkSize.converted(to: .bytes).value)
.rounded(.down)
)
self.retryLimitPerChunk = retryLimitPerChunk
}
/// Initializes options for transport of upload chunks
/// over the network
/// - Parameters:
/// - chunkSizeInBytes: the size of each file
/// chunk in bytes the SDK uploads in a single
/// request. Default chunk size is 8MB.
/// - retryLimitPerChunk: number of times a failed
/// chunk request is retried. Default limit is
/// 3 retries.
public init(
chunkSizeInBytes: Int = 8 * 1024 * 1024,
retryLimitPerChunk: Int = 3
) {
self.chunkSizeInBytes = chunkSizeInBytes
self.retryLimitPerChunk = retryLimitPerChunk
}
}
/// Network transport options for direct upload chunks
public var transport: Transport
// MARK: - Input Standardization Options
/// Options for adjusments made by ``DirectUpload``
/// to some inputs to minimize processing time during
/// ingestion
public struct InputStandardization {
/// If requested the SDK will attempt to detect
/// non-standard input formats and if so detected
/// will attempt to standardize to a standard input
/// format. ``true`` by default
public var isRequested: Bool = true
/// Preset to control the resolution of the standard
/// input.
///
/// See ``DirectUploadOptions.InputStandardization.maximumResolution``
/// for more details.
public enum MaximumResolution {
/// Preset standardized direct upload input to the SDK
/// default standard resolution of 1920x1080 (1080p).
case `default`
/// Limit standardized direct upload input resolution to
/// 1280x720 (720p).
case preset1280x720 // 720p
/// Limit standardized direct upload input resolution to
/// 1920x1080 (1080p).
case preset1920x1080 // 1080p
}
/// The maximum resolution of the standardized direct
/// upload input. If the input has a video resolution
/// below this value, the resolution will remain
/// unchanged after input standardization.
///
/// Example 1: a direct upload input with 1440 x 1080
/// resolution encoded using Apple ProRes and with
/// no other non-standard input parameters with
/// ``MaximumResolution.default`` selected.
///
/// If input standardization is requested, the SDK
/// will attempt standardize the input into an H.264
/// encoded output that will maintain its original
/// 1440 x 1080 resolution.
///
/// Example 2: a direct upload input with 1440 x 1080
/// resolution encoded using H.264 and with no other
/// non-standard input format parameters with
/// ``MaximumResolution.preset1280x720`` selected.
///
/// If input standardization is requested, the SDK
/// will attempt standardize the input into an H.264
/// encoded output with a reduced 1280 x 720 resolution.
///
public var maximumResolution: MaximumResolution = .default
/// Default options where input standardization is
/// requested and the maximum resolution is set to 1080p.
public static let `default`: InputStandardization = InputStandardization(
isRequested: true,
maximumResolution: .default
)
/// Skip all local input standardization by the SDK.
///
/// Initializing a ``DirectUpload`` with input
/// standardization skipped will result in SDK
/// uploading all inputs as they are with no format
/// changes performed on the client. Mux Video will
/// still convert your input to a standard format
/// on the server when it is ingested.
public static let skipped: InputStandardization = InputStandardization(
isRequested: false,
maximumResolution: .default
)
// Kept private to avoid an invalid combination of
// parameters being used for initialization
private init(
isRequested: Bool,
maximumResolution: MaximumResolution
) {
self.isRequested = isRequested
self.maximumResolution = maximumResolution
}
/// Initializes options that request input
/// standardization with a custom maximum resolution
/// - Parameters:
/// - maximumResolution: the maximum resolution
/// of the standardized input
public init(
maximumResolution: MaximumResolution
) {
self.isRequested = true
self.maximumResolution = maximumResolution
}
}
/// Input standardization options for the direct upload
public var inputStandardization: InputStandardization
// MARK: - Event Tracking Options
/// Event tracking options
public struct EventTracking {
/// Default options that opt into event tracking
static public var `default`: EventTracking {
EventTracking(optedOut: false)
}
/// Flag indicating if opted out of event tracking
public var optedOut: Bool
/// - Parameters:
/// - optedOut: if true opts out of event
/// tracking
public init(
optedOut: Bool
) {
self.optedOut = optedOut
}
}
/// Event tracking options for the direct upload
public var eventTracking: EventTracking
// MARK: Default Direct Upload Options
public static var `default`: DirectUploadOptions {
DirectUploadOptions()
}
// MARK: Direct Upload Options Initializers
/// Initializes options that dictate how the direct upload
/// is carried out by the SDK
/// - Parameters:
/// - inputStandardization: options related to input
/// standardization. Input standardization is requested
/// by default.
/// To skip input standardization pass in
/// ``DirectUploadOptions.InputStandardization.skipped``.
/// - transport: options for transporting the
/// direct upload input to Mux
/// - eventTracking: event tracking options for the
/// direct upload
public init(
inputStandardization: InputStandardization = .default,
transport: Transport = .default,
eventTracking: EventTracking = .default
) {
self.inputStandardization = inputStandardization
self.transport = transport
self.eventTracking = eventTracking
}
/// Initializes options that dictate how the direct upload
/// is carried out by the SDK
/// - Parameters:
/// - eventTracking: event tracking options for the
/// direct upload
/// - inputStandardization: options related to input
/// standardization. Input standardization is requested
/// by default.
/// To skip input standardization pass in
/// ``DirectUploadOptions.InputStandardization.skipped``.
/// - chunkSize: The size of each file chunk sent by
/// the SDK during an upload. Defaults to 8MB.
/// - retryLimitPerChunk: number of retry attempts
/// if the chunk request fails. Defaults to 3.
public init(
eventTracking: EventTracking = .default,
inputStandardization: InputStandardization = .default,
chunkSize: Measurement<UnitInformationStorage> = .defaultDirectUploadChunkSize,
retryLimitPerChunk: Int = 3
) {
self.eventTracking = eventTracking
self.inputStandardization = inputStandardization
self.transport = Transport(
chunkSize: chunkSize,
retryLimitPerChunk: retryLimitPerChunk
)
}
/// Initializes options that dictate how the direct upload
/// is carried out by the SDK
/// - Parameters:
/// - eventTracking: event tracking options for the
/// direct upload
/// - inputStandardization: options related to input
/// standardization. Input standardization is requested
/// by default.
/// To skip input standardization pass in
/// ``DirectUploadOptions.InputStandardization.skipped``.
/// - chunkSizeInBytes: The size of each file chunk
/// in bytes sent by the SDK during an upload.
/// Defaults to 8MB.
/// - retryLimitPerChunk: number of retry attempts
/// if the chunk request fails. Defaults to 3.
public init(
eventTracking: EventTracking = .default,
inputStandardization: InputStandardization = .default,
chunkSizeInBytes: Int = 8 * 1024 * 1024,
retryLimitPerChunk: Int = 3
) {
self.eventTracking = eventTracking
self.inputStandardization = inputStandardization
self.transport = Transport(
chunkSizeInBytes: chunkSizeInBytes,
retryLimitPerChunk: retryLimitPerChunk
)
}
}
// MARK: - Extensions
extension Measurement where UnitType == UnitInformationStorage {
/// Default direct upload chunk size
public static var defaultDirectUploadChunkSize: Self {
Measurement(
value: 8,
unit: .megabytes
)
}
}
extension DirectUploadOptions.InputStandardization.MaximumResolution: CustomStringConvertible {
public var description: String {
switch self {
case .preset1280x720:
return "preset1280x720"
case .preset1920x1080:
return "preset1920x1080"
case .default:
return "default"
}
}
}
extension DirectUploadOptions: Codable { }
extension DirectUploadOptions.EventTracking: Codable { }
extension DirectUploadOptions.InputStandardization: Codable { }
extension DirectUploadOptions.InputStandardization.MaximumResolution: Codable { }
extension DirectUploadOptions.Transport: Codable { }
extension DirectUploadOptions: Equatable { }
extension DirectUploadOptions.EventTracking: Equatable { }
extension DirectUploadOptions.InputStandardization: Equatable { }
extension DirectUploadOptions.InputStandardization.MaximumResolution: Equatable { }
extension DirectUploadOptions.Transport: Equatable { }