-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vladislav Alekseev
committed
Feb 21, 2023
1 parent
8671d77
commit f5d25ba
Showing
6 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright (c) Avito Tech LLC | ||
*/ | ||
|
||
import Foundation | ||
import PathLib | ||
|
||
public protocol CancellableRecording { | ||
/// Stops recording and returns a path to a file where video is stored. | ||
func stopRecording() -> AbsolutePath | ||
|
||
/// Cancels recording and does not write any data to the file. Thus, does not return any path. | ||
func cancelRecording() | ||
} |
36 changes: 36 additions & 0 deletions
36
Sources/SimulatorVideoRecorder/CancellableRecordingImpl.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) Avito Tech LLC | ||
*/ | ||
|
||
import Foundation | ||
import PathLib | ||
import ProcessController | ||
|
||
class CancellableRecordingImpl: CancellableRecording { | ||
private let outputPath: AbsolutePath | ||
private let recordingProcess: ProcessController | ||
|
||
public init( | ||
outputPath: AbsolutePath, | ||
recordingProcess: ProcessController | ||
) { | ||
self.outputPath = outputPath | ||
self.recordingProcess = recordingProcess | ||
} | ||
|
||
func stopRecording() -> AbsolutePath { | ||
recordingProcess.interruptAndForceKillIfNeeded {} | ||
recordingProcess.waitForProcessToDie() | ||
return outputPath | ||
} | ||
|
||
func cancelRecording() { | ||
recordingProcess.terminateAndForceKillIfNeeded() | ||
recordingProcess.waitForProcessToDie() | ||
|
||
let fileManager = FileManager() | ||
if fileManager.fileExists(atPath: outputPath.pathString) { | ||
try? fileManager.removeItem(atPath: outputPath.pathString) | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
Sources/SimulatorVideoRecorder/SimulatorVideoRecorder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) Avito Tech LLC | ||
*/ | ||
|
||
import Foundation | ||
import PathLib | ||
import ProcessController | ||
|
||
public final class SimulatorVideoRecorder { | ||
public enum CodecType: String { | ||
case h264 | ||
case hevc | ||
} | ||
|
||
private let processControllerProvider: ProcessControllerProvider | ||
private let simulatorUuid: String | ||
private let simulatorSetPath: AbsolutePath | ||
|
||
public init( | ||
processControllerProvider: ProcessControllerProvider, | ||
simulatorUuid: String, | ||
simulatorSetPath: AbsolutePath | ||
) { | ||
self.processControllerProvider = processControllerProvider | ||
self.simulatorUuid = simulatorUuid | ||
self.simulatorSetPath = simulatorSetPath | ||
} | ||
|
||
public func startRecording( | ||
codecType: CodecType, | ||
outputPath: AbsolutePath | ||
) throws -> CancellableRecording { | ||
let processController = try processControllerProvider.createProcessController( | ||
subprocess: Subprocess( | ||
arguments: [ | ||
"/usr/bin/xcrun", | ||
"simctl", | ||
"--set", | ||
simulatorSetPath, | ||
"io", | ||
simulatorUuid, | ||
"recordVideo", | ||
"--codec=\(codecType.rawValue)", | ||
outputPath | ||
] | ||
) | ||
) | ||
try processController.start() | ||
|
||
return CancellableRecordingImpl( | ||
outputPath: outputPath, | ||
recordingProcess: processController | ||
) | ||
} | ||
} |