-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRtcSurfaceView.swift
More file actions
105 lines (91 loc) · 3.3 KB
/
RtcSurfaceView.swift
File metadata and controls
105 lines (91 loc) · 3.3 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
//
// RtcSurfaceView.swift
// RCTAgora
//
// Created by LXH on 2020/4/15.
// Copyright © 2020 Syan. All rights reserved.
//
import Foundation
import UIKit
import AgoraRtcKit
class RtcSurfaceView: UIView {
private var surface: UIView
private var canvas: AgoraRtcVideoCanvas
private weak var channel: AgoraRtcChannel?
override init(frame: CGRect) {
surface = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: frame.size))
canvas = AgoraRtcVideoCanvas()
canvas.view = surface
super.init(frame: frame)
addSubview(surface)
addObserver(self, forKeyPath: observerForKeyPath(), options: .new, context: nil)
}
func observerForKeyPath() -> String {
return "frame"
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
canvas.view = nil
removeObserver(self, forKeyPath: observerForKeyPath(), context: nil)
}
func setData(_ engine: AgoraRtcEngineKit, _ channel: AgoraRtcChannel?, _ uid: UInt) {
self.channel = channel
canvas.channelId = channel?.getId()
canvas.uid = uid
setupVideoCanvas(engine)
}
func resetVideoCanvas(_ engine: AgoraRtcEngineKit) {
let canvas = AgoraRtcVideoCanvas()
canvas.view = nil
canvas.renderMode = self.canvas.renderMode
canvas.channelId = self.canvas.channelId
canvas.uid = self.canvas.uid
canvas.mirrorMode = self.canvas.mirrorMode
if canvas.uid == 0 {
engine.setupLocalVideo(canvas)
} else {
engine.setupRemoteVideo(canvas)
}
}
private func setupVideoCanvas(_ engine: AgoraRtcEngineKit) {
subviews.forEach {
$0.removeFromSuperview()
}
surface = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: bounds.size))
addSubview(surface)
canvas.view = surface
if canvas.uid == 0 {
engine.setupLocalVideo(canvas)
} else {
engine.setupRemoteVideo(canvas)
}
}
func setRenderMode(_ engine: AgoraRtcEngineKit, _ renderMode: UInt) {
canvas.renderMode = AgoraVideoRenderMode(rawValue: renderMode)!
setupRenderMode(engine)
}
func setMirrorMode(_ engine: AgoraRtcEngineKit, _ mirrorMode: UInt) {
canvas.mirrorMode = AgoraVideoMirrorMode(rawValue: mirrorMode)!
setupRenderMode(engine)
}
private func setupRenderMode(_ engine: AgoraRtcEngineKit) {
if canvas.uid == 0 {
engine.setLocalRenderMode(canvas.renderMode, mirrorMode: canvas.mirrorMode)
} else {
if let `channel` = channel {
channel.setRemoteRenderMode(canvas.uid, renderMode: canvas.renderMode, mirrorMode: canvas.mirrorMode)
} else {
engine.setRemoteRenderMode(canvas.uid, renderMode: canvas.renderMode, mirrorMode: canvas.mirrorMode)
}
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == observerForKeyPath() {
if let rect = change?[.newKey] as? CGRect {
surface.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: rect.size)
}
}
}
}