forked from home-assistant/iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraStreamMJPEGViewController.swift
125 lines (106 loc) · 3.96 KB
/
CameraStreamMJPEGViewController.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
import Alamofire
import Foundation
import PromiseKit
import Shared
import UIKit
class CameraStreamMJPEGViewController: UIViewController, CameraStreamHandler {
let api: HomeAssistantAPI
let response: StreamCameraResponse
let imageView: UIImageView
let streamer: MJPEGStreamer
let promise: Promise<Void>
var didUpdateState: (CameraStreamHandlerState) -> Void = { _ in }
private let seal: Resolver<Void>
enum MJPEGError: LocalizedError {
case noPath
case networkError(path: String, error: Error?)
var errorDescription: String? {
switch self {
case .noPath:
return L10n.Extensions.NotificationContent.Error.Request.authFailed
case let .networkError(path, error):
if let error = error as? AFError, let responseCode = error.responseCode {
switch responseCode {
case 401:
return L10n.Extensions.NotificationContent.Error.Request.authFailed
case 404:
return L10n.Extensions.NotificationContent.Error.Request.entityNotFound(path)
default:
return L10n.Extensions.NotificationContent.Error.Request.other(responseCode)
}
}
return error?.localizedDescription ?? L10n.Extensions.NotificationContent.Error.Request.other(-1)
}
}
}
required init(api: HomeAssistantAPI, response: StreamCameraResponse) throws {
guard response.mjpegPath != nil else {
throw MJPEGError.noPath
}
self.api = api
self.response = response
self.streamer = api.VideoStreamer()
self.imageView = UIImageView()
(self.promise, self.seal) = Promise<Void>.pending()
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
imageView.contentMode = .scaleAspectFit
view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: view.topAnchor),
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
setupStreamer()
}
func pause() {
streamer.cancel()
didUpdateState(.paused)
}
func play() {
if !streamer.isActive {
setupStreamer()
}
}
private var aspectRatioConstraint: NSLayoutConstraint? {
willSet {
aspectRatioConstraint?.isActive = false
}
didSet {
aspectRatioConstraint?.isActive = true
}
}
private var lastSize: CGSize? {
didSet {
if oldValue != lastSize, let size = lastSize {
aspectRatioConstraint = NSLayoutConstraint.aspectRatioConstraint(on: imageView, size: size)
}
}
}
private func setupStreamer() {
guard let path = response.mjpegPath else {
fatalError("we checked for a non-nil path on init, this should not be possible")
}
let url = api.server.info.connection.activeURL().appendingPathComponent(path)
// assume 16:9
lastSize = CGSize(width: 16, height: 9)
streamer.streamImages(fromURL: url) { [weak self, imageView] image, error in
guard let image = image else {
self?.seal.reject(MJPEGError.networkError(path: path, error: error))
return
}
imageView.image = image
self?.seal.fulfill(())
self?.lastSize = image.size
self?.didUpdateState(.playing)
}
}
}