Skip to content

Commit 7ff85ca

Browse files
committed
[WIP] VideoController で SwiftUIVideoView (VideoView) を制御する
1 parent 1b2811c commit 7ff85ca

File tree

1 file changed

+24
-52
lines changed

1 file changed

+24
-52
lines changed

Sora/SwiftUIVideoView.swift

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,15 @@ import UIKit
77
*/
88
@available(iOS 14, *)
99
public struct SwiftUIVideoView<Background>: View where Background: View {
10-
private var stream: MediaStream?
11-
private var background: Background
12-
13-
// TODO(zztkm): わかりやすいコメントを書く
14-
// 親 View で定義された stopVideo 変数と接続するための変数
15-
@Binding private var isStop: Bool
16-
@Binding private var isClear: Bool
17-
1810
@ObservedObject private var controller: VideoController
19-
11+
private var background: Background
2012
/**
2113
ビューを初期化します。
2214

23-
- parameter stream: 描画される映像ストリーム。 nil の場合は何も描画されません
24-
- parameter isStop: 映像の描画を制御するフラグ (default: false)
25-
- parameter isClear: 映像をクリアして背景 View を表示するためのフラグ (default: false)
15+
- parameter controller: VideoController
2616
*/
27-
public init(_ stream: MediaStream?, isStop: Binding<Bool>? = nil, isClear: Binding<Bool>? = nil) where Background == EmptyView {
28-
self.init(stream, background: EmptyView(), isStop: isStop, isClear: isClear)
17+
public init(_ controller: VideoController) where Background == EmptyView {
18+
self.init(controller, background: EmptyView())
2919
}
3020

3121
/**
@@ -36,22 +26,23 @@ public struct SwiftUIVideoView<Background>: View where Background: View {
3626
- parameter isStop: 映像の描画を制御するフラグ (default: false)
3727
- parameter isClear: 映像をクリアして背景 View を表示するためのフラグ (default: false)
3828
*/
39-
public init(_ stream: MediaStream?, background: Background, isStop: Binding<Bool>? = nil, isClear: Binding<Bool>? = nil) {
40-
self.stream = stream
29+
public init(_ controller: VideoController, background: Background) {
30+
self.controller = controller
4131
self.background = background
42-
// 指定がない場合は固定値 false を与える
43-
_isStop = isStop ?? .constant(false)
44-
_isClear = isClear ?? .constant(false)
45-
controller = VideoController(stream: stream)
4632
}
4733

4834
/// :nodoc:
35+
/// TODO(zztkm): SwiftUI での Background と UIKit での Background が同居しているが使い分けについて考える
36+
/// 今の実装だと isClear で UIKit の方も clear してるけど、これは意味がなくて isClear すると SwiftUI の Backgroud View が表示されるようになる
37+
/// なので、そもそも VideView の clear() method を呼ぶ意味がなくなってしまってる
38+
/// この辺の制御はできるだけ SwiftUI 側に寄せたいので、clear() metthod を呼ばないようにするなど検討したい
4939
public var body: some View {
5040
ZStack {
41+
// isClear が true のときは背景を表示し、false のときは Video を表示する
5142
background
52-
.opacity(controller.isCleared ? 1 : 0)
43+
.opacity(isClear ? 1 : 0)
5344
RepresentedVideoView(controller, isStop: $isStop, isClear: $isClear)
54-
.opacity(controller.isCleared ? 0 : 1)
45+
.opacity(isClear ? 0 : 1)
5546
}
5647
}
5748

@@ -85,33 +76,7 @@ public struct SwiftUIVideoView<Background>: View where Background: View {
8576

8677
/// 映像のクリア時に表示する背景ビューを指定します。
8778
public func videoBackground<Background>(_ background: Background) -> SwiftUIVideoView<Background> where Background: View {
88-
var new = SwiftUIVideoView<Background>(stream, background: background)
89-
new.controller = controller
90-
return new
91-
}
92-
93-
/**
94-
映像の描画を停止します。
95-
*/
96-
private func videoStop(_ flag: Bool) -> SwiftUIVideoView<Background> {
97-
if flag {
98-
controller.videoView.stop()
99-
} else if !controller.videoView.isRendering {
100-
controller.videoView.start()
101-
}
102-
return self
103-
}
104-
105-
/**
106-
画面を背景ビューに切り替えます。
107-
このメソッドは描画停止時のみ有効です。
108-
*/
109-
public func videoClear(_ flag: Bool) -> SwiftUIVideoView<Background> {
110-
if flag {
111-
controller.videoView.clear()
112-
controller.isCleared = true
113-
}
114-
return self
79+
SwiftUIVideoView<Background>(controller, background: background)
11580
}
11681

11782
/// 映像のサイズの変更時に実行されるブロックを指定します。
@@ -134,7 +99,11 @@ private struct RepresentedVideoView: UIViewRepresentable {
13499
typealias UIViewType = VideoView
135100

136101
@ObservedObject private var controller: VideoController
102+
103+
// 以下は SDK ユーザー側で制御される変数
104+
/// 映像を停止するかを制御するフラグ
137105
@Binding private var isStop: Bool
106+
/// backgroundView を表示するか制御するフラグ
138107
@Binding private var isClear: Bool
139108

140109
public init(_ controller: VideoController, isStop: Binding<Bool>, isClear: Binding<Bool>) {
@@ -158,6 +127,11 @@ private struct RepresentedVideoView: UIViewRepresentable {
158127
controller.stream?.videoRenderer = uiView
159128
}
160129

130+
// 現在 VideoView が表示している映像の元々のフレームサイズを返します。
131+
public var currentVideoFrameSize: CGSize? {
132+
controller.videoView.currentVideoFrameSize
133+
}
134+
161135
/**
162136
映像の描画を停止します。TODO(zztkm): method 名の検討 (stop start の toggle なので、stop はおかしいかも?
163137
*/
@@ -182,15 +156,13 @@ private struct RepresentedVideoView: UIViewRepresentable {
182156
}
183157
}
184158

185-
class VideoController: ObservableObject {
159+
public class VideoController: ObservableObject {
186160
var stream: MediaStream?
187161

188162
// init() で VideoView を生成すると次のエラーが出るので、生成のタイミングを遅らせておく
189163
// Failed to bind EAGLDrawable: <CAEAGLLayer: 0x********> to GL_RENDERBUFFER 1
190164
lazy var videoView = VideoView()
191165

192-
@Published var isCleared: Bool = false
193-
194166
init(stream: MediaStream?) {
195167
self.stream = stream
196168
}

0 commit comments

Comments
 (0)