Skip to content

Commit 0fbcb89

Browse files
committed
ProverSwipe: Add settings for a recorded video's resolution
1 parent 73bd563 commit 0fbcb89

File tree

4 files changed

+73
-32
lines changed

4 files changed

+73
-32
lines changed

client/iOS/swype/ProverSwipe.xcodeproj/project.pbxproj

+12
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
1AACAB2E20D1AC890018AAFA /* ServerModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AACAB2D20D1AC880018AAFA /* ServerModel.swift */; };
6161
1AACAB3020D1AD420018AAFA /* ServerExceptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AACAB2F20D1AD410018AAFA /* ServerExceptions.swift */; };
6262
1AACAB3620D1ADB70018AAFA /* NEMWallet.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 1AACAB3320D1ADB60018AAFA /* NEMWallet.xcdatamodeld */; };
63+
1AC9D53220EF8867006758CA /* Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AC9D53120EF8867006758CA /* Settings.swift */; };
6364
1AF1FFA620C32F2D00176F53 /* Wallet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AF1FFA520C32F2C00176F53 /* Wallet.swift */; };
6465
3C238EC22613F7B47957AE74 /* Pods_ProverSwipe.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 553F0162152AFFAD533356AC /* Pods_ProverSwipe.framework */; };
6566
75A22DE2CD47F9C8C17234F8 /* Pods_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0C9D87733EAB2586D64D03B3 /* Pods_Tests.framework */; };
@@ -289,6 +290,7 @@
289290
1AACAB2D20D1AC880018AAFA /* ServerModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServerModel.swift; sourceTree = "<group>"; };
290291
1AACAB2F20D1AD410018AAFA /* ServerExceptions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServerExceptions.swift; sourceTree = "<group>"; };
291292
1AACAB3420D1ADB60018AAFA /* NEMWallet.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = NEMWallet.xcdatamodel; sourceTree = "<group>"; };
293+
1AC9D53120EF8867006758CA /* Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Settings.swift; sourceTree = "<group>"; };
292294
1AF1FFA520C32F2C00176F53 /* Wallet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Wallet.swift; sourceTree = "<group>"; };
293295
1D8C41A3F68CF1161BBCAAA4 /* Pods-Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Tests/Pods-Tests.debug.xcconfig"; sourceTree = "<group>"; };
294296
553F0162152AFFAD533356AC /* Pods_ProverSwipe.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ProverSwipe.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -681,6 +683,14 @@
681683
path = Additions;
682684
sourceTree = "<group>";
683685
};
686+
1AC9D53020EF8825006758CA /* Settings */ = {
687+
isa = PBXGroup;
688+
children = (
689+
1AC9D53120EF8867006758CA /* Settings.swift */,
690+
);
691+
path = Settings;
692+
sourceTree = "<group>";
693+
};
684694
1AF1FFA720C3362900176F53 /* Dependency */ = {
685695
isa = PBXGroup;
686696
children = (
@@ -1017,6 +1027,7 @@
10171027
9A9DB78420737EA900E29A07 /* Model */,
10181028
9A9DB78E20737FB200E29A07 /* Network */,
10191029
9A9DB77620737B8900E29A07 /* Screeens */,
1030+
1AC9D53020EF8825006758CA /* Settings */,
10201031
9A9DB78520737ED800E29A07 /* Support */,
10211032
9AE467EA20C17C2A00EA6FB0 /* Wallet */,
10221033
);
@@ -1393,6 +1404,7 @@
13931404
9A9DB79F2073925300E29A07 /* HelloOperation.swift in Sources */,
13941405
9A9DB78B20737F7000E29A07 /* Hexadecimal.swift in Sources */,
13951406
1A8A318E20D15EB200BB7E97 /* String+Additions.swift in Sources */,
1407+
1AC9D53220EF8867006758CA /* Settings.swift in Sources */,
13961408
1AACAB2A20D17CC80018AAFA /* Constants.swift in Sources */,
13971409
9A9DB77C20737C0300E29A07 /* ImportWalletViewController.swift in Sources */,
13981410
9A9DB7962073805000E29A07 /* Info.swift in Sources */,

client/iOS/swype/ProverSwipe/Detection/VideoRecorder.swift

+19-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class VideoRecorder: NSObject {
3838

3939
private let videoFileURL =
4040
FileManager.default.temporaryDirectory.appendingPathComponent("recorded_video.mp4")
41-
41+
4242
// MARK: - Initialization
4343
init(withParent parent: VideoPreviewView) {
4444
captureVideoPreviewLayer = parent.videoPreviewLayer
@@ -236,7 +236,7 @@ extension VideoRecorder {
236236
// MARK: - Recording
237237
extension VideoRecorder {
238238

239-
func startRecord() {
239+
private var videoOutputSettings: [String : Any] {
240240
var videoCodec: Any
241241

242242
if #available(iOS 11.0, *) {
@@ -245,10 +245,24 @@ extension VideoRecorder {
245245
videoCodec = AVVideoCodecH264
246246
}
247247

248-
let videoOutputSettings = [AVVideoWidthKey: 720,
249-
AVVideoHeightKey: 1280,
250-
AVVideoCodecKey: videoCodec]
248+
let assistant = AVOutputSettingsAssistant(preset: Settings.currentVideoPreset)
249+
let settings = assistant!.videoSettings!
250+
let settingsWidth = settings[AVVideoWidthKey] as! Int
251+
let settingsHeight = settings[AVVideoHeightKey] as! Int
252+
253+
// We record in the portrait mode so swap width and height
254+
return [AVVideoWidthKey: settingsHeight,
255+
AVVideoHeightKey: settingsWidth,
256+
AVVideoCodecKey: videoCodec]
257+
}
251258

259+
private var audioOutputSettings: [String : Any] {
260+
return [AVFormatIDKey: kAudioFormatMPEG4AAC,
261+
AVSampleRateKey: 12000,
262+
AVNumberOfChannelsKey: 1]
263+
}
264+
265+
func startRecord() {
252266
assetVideoWriterInput = AVAssetWriterInput(mediaType: .video, outputSettings: videoOutputSettings)
253267
assetVideoWriterInput.expectsMediaDataInRealTime = true
254268

@@ -258,10 +272,6 @@ extension VideoRecorder {
258272
assetWriterInput: assetVideoWriterInput,
259273
sourcePixelBufferAttributes: videoOutputAttributes)
260274

261-
let audioOutputSettings = [AVFormatIDKey: kAudioFormatMPEG4AAC,
262-
AVSampleRateKey: 12000,
263-
AVNumberOfChannelsKey: 1]
264-
265275
assetAudioWriterInput = AVAssetWriterInput(mediaType: .audio, outputSettings: audioOutputSettings)
266276
assetAudioWriterInput.expectsMediaDataInRealTime = true
267277

client/iOS/swype/ProverSwipe/Screeens/Swype/SwypeViewController.swift

+14-23
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,16 @@ class SwypeViewController: UIViewController, UpdateBalanceBehaviour {
6767
var saver: VideoSaver?
6868

6969
// MARK: - Private properties
70+
private var canStartVideoProcessing: Bool {
71+
let videoStatus = AVCaptureDevice.authorizationStatus(for: .video)
72+
let audioStatus = AVCaptureDevice.authorizationStatus(for: .audio)
73+
74+
return videoStatus == .authorized && audioStatus != .notDetermined
75+
}
7076
private var swypeBlock = ""
71-
7277
private let queue = OperationQueue()
73-
74-
var state: State = .readyToRecord { didSet { update() }}
7578

76-
var isAccessToCameraDenied: Bool = false
79+
var state: State = .readyToRecord { didSet { update() }}
7780

7881
// MARK: - View controller lifecycle
7982
override func viewDidLoad() {
@@ -96,33 +99,22 @@ class SwypeViewController: UIViewController, UpdateBalanceBehaviour {
9699
if videoProcessor != nil {
97100
videoProcessor.startCapture()
98101
}
99-
else {
100-
101-
let audioStatus = AVCaptureDevice.authorizationStatus(for: .audio)
102-
let videoStatus = AVCaptureDevice.authorizationStatus(for: .video)
103-
104-
if audioStatus != .notDetermined && videoStatus == .authorized {
105-
createVideoProcessingStuff()
106-
}
102+
else if canStartVideoProcessing {
103+
createVideoProcessingStuff()
107104
}
108105
}
109106

110107
override func viewDidAppear(_ animated: Bool) {
111108
print("[SwypeViewController] viewDidAppear")
112109
super.viewDidAppear(animated)
113110

114-
if videoProcessor == nil {
115-
let audioStatus = AVCaptureDevice.authorizationStatus(for: .audio)
116-
let videoStatus = AVCaptureDevice.authorizationStatus(for: .video)
117-
118-
if audioStatus == .notDetermined || videoStatus != .authorized {
119-
requestAuthorizationForAudioCapture()
120-
}
111+
if videoProcessor == nil && !canStartVideoProcessing {
112+
requestAuthorizationForAudioCapture()
121113
}
122114
}
123115

124-
override func viewWillDisappear(_ animated: Bool) {
125-
super.viewWillDisappear(animated)
116+
override func viewDidDisappear(_ animated: Bool) {
117+
super.viewDidDisappear(animated)
126118
print("[SwypeViewController] viewDidDisappear")
127119
videoProcessor?.stopCapture()
128120
}
@@ -148,7 +140,7 @@ class SwypeViewController: UIViewController, UpdateBalanceBehaviour {
148140
else { fatalError("[SwypeViewController] Cast to WalletViewController failed!") }
149141

150142
walletVC.store = store
151-
walletVC.shouldHideLeftButton = isAccessToCameraDenied
143+
walletVC.shouldHideLeftButton = videoProcessor == nil
152144
}
153145
}
154146

@@ -259,7 +251,6 @@ private extension SwypeViewController {
259251
"in your iPhone's Settings"
260252
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
261253
let okAction = UIAlertAction(title: "OK", style: .cancel) { [unowned self] _ in
262-
self.isAccessToCameraDenied = true
263254
self.performSegue(withIdentifier: Segue.showWalletSegue.rawValue, sender: nil)
264255
}
265256

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
3+
class Settings {
4+
5+
private static var defaultVideoPreset: AVOutputSettingsPreset {
6+
let availablePresets = AVOutputSettingsAssistant.availableOutputSettingsPresets()
7+
.reversed()
8+
guard availablePresets.contains(.preset1920x1080) else {
9+
return availablePresets.first!
10+
}
11+
12+
return .preset1920x1080
13+
}
14+
15+
private static let currentVideoPresetKey: String = "currentVideoPreset"
16+
17+
public static var currentVideoPreset: AVOutputSettingsPreset {
18+
get {
19+
guard let presetRawValue = UserDefaults.standard.string(forKey: currentVideoPresetKey) else {
20+
return defaultVideoPreset
21+
}
22+
return AVOutputSettingsPreset(rawValue: presetRawValue)
23+
}
24+
set {
25+
UserDefaults.standard.setValue(newValue.rawValue, forKey: currentVideoPresetKey)
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)