-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushManager.swift
More file actions
159 lines (133 loc) · 4.54 KB
/
PushManager.swift
File metadata and controls
159 lines (133 loc) · 4.54 KB
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
//
// PushManager.swift
// MistTray
//
import Foundation
class PushManager {
static let shared = PushManager()
private init() {}
// MARK: - Push Operations
func startPush(
streamName: String, targetURL: String, completion: @escaping (Result<Void, APIError>) -> Void
) {
print("PushManager: Starting push for stream '\(streamName)' to '\(targetURL)'")
APIClient.shared.startPush(streamName: streamName, targetURL: targetURL) { result in
DispatchQueue.main.async {
switch result {
case .success:
print("Push started successfully")
completion(.success(()))
case .failure(let error):
print("Failed to start push: \(error)")
completion(.failure(error))
}
}
}
}
func stopPush(pushId: Int, completion: @escaping (Result<Void, APIError>) -> Void) {
print("PushManager: Stopping push '\(pushId)'")
APIClient.shared.stopPush(pushId: pushId) { result in
DispatchQueue.main.async {
switch result {
case .success:
print("Push stopped successfully")
completion(.success(()))
case .failure(let error):
print("Failed to stop push: \(error)")
completion(.failure(error))
}
}
}
}
// MARK: - Auto-Push Rules
func createAutoPushRule(
streamPattern: String, targetURL: String,
completion: @escaping (Result<String, APIError>) -> Void
) {
print("PushManager: Creating auto push rule for pattern '\(streamPattern)' to '\(targetURL)'")
APIClient.shared.createAutoPushRule(streamPattern: streamPattern, targetURL: targetURL) {
result in
DispatchQueue.main.async {
switch result {
case .success(let response):
print("Auto push rule created successfully")
if let ruleId = response["id"] as? String {
completion(.success(ruleId))
} else {
completion(.success("rule_created"))
}
case .failure(let error):
print("Failed to create auto push rule: \(error)")
completion(.failure(error))
}
}
}
}
func deleteAutoPushRule(ruleId: String, completion: @escaping (Result<Void, APIError>) -> Void) {
APIClient.shared.deleteAutoPushRule(ruleId: ruleId) { result in
switch result {
case .success:
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
}
func listAutoPushRules(completion: @escaping (Result<[String: Any], APIError>) -> Void) {
APIClient.shared.fetchAutoPushRules(completion: completion)
}
// MARK: - Push Validation
func validatePushConfiguration(streamName: String, targetURL: String) -> PushValidationResult {
var errors: [String] = []
if streamName.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
errors.append("Stream name cannot be empty")
}
if targetURL.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
errors.append("Target URL cannot be empty")
} else if !isValidPushURL(targetURL) {
errors.append("Invalid target URL format")
}
return PushValidationResult(isValid: errors.isEmpty, errors: errors)
}
private func isValidPushURL(_ url: String) -> Bool {
let validPrefixes = [
"rtmp://", "rtmps://", "srt://", "udp://", "file://", "http://", "https://",
]
return validPrefixes.contains { url.lowercased().hasPrefix($0) }
}
// MARK: - Push Settings
func performPushStart(
streamName: String, targetURL: String, completion: @escaping (Result<Void, APIError>) -> Void
) {
print("Performing push start for \(streamName) -> \(targetURL)")
startPush(streamName: streamName, targetURL: targetURL, completion: completion)
}
func applyPushSettings(
_ settings: [String: Any], completion: @escaping (Result<Void, APIError>) -> Void
) {
guard let streamName = settings["stream"] as? String,
let targetURL = settings["target"] as? String
else {
completion(.failure(.invalidRequest))
return
}
let autoStart = settings["autoStart"] as? Bool ?? false
if autoStart {
createAutoPushRule(streamPattern: streamName, targetURL: targetURL) { result in
switch result {
case .success:
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
} else {
startPush(streamName: streamName, targetURL: targetURL, completion: completion)
}
}
}
// MARK: - Supporting Types
struct PushValidationResult {
let isValid: Bool
let errors: [String]
}