-
Notifications
You must be signed in to change notification settings - Fork 0
/
SEIViewController.swift
524 lines (414 loc) · 19 KB
/
SEIViewController.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
* Function name: SEI message
* Function brief description: Send a SEI message, and the SEI data will be added to consecutive "repeatCount + 1" video frames starting from the current frame.
* Reminder:
* 1. To demonstrate, all functional tokens are generated by the client side TokenGenerator class, please depend on the specific situation when actually accessing
* 2. In the video call scenario, the SEI is sent with the video frame, while in the voice call scenario,
The SDK automatically publishes black frames with a resolution of 16 x 16 pixels to carry SEI data.
* 3. When making a voice call, this parameter should be set to "ByteRTCStreamIndexMain", otherwise the SEI data will be discarded and cannot be sent to the remote user.
*/
import UIKit
import SnapKit
import BytePlusRTC
@objc(SEIViewController)
class SEIViewController: BaseViewController, ByteRTCVideoDelegate, ByteRTCRoomDelegate, ByteRTCMixedStreamObserver {
var rtcVideo: ByteRTCVideo?
var rtcRoom: ByteRTCRoom?
var users : Array = Array<ByteRTCRemoteStreamKey>()
var mixConfig: ByteRTCMixedStreamConfig?
let taskId = "1999"
override func viewDidLoad() {
super.viewDidLoad()
self.createUI()
self.buildRTCEngine()
}
deinit {
self.rtcRoom?.leaveRoom()
self.rtcRoom?.destroy()
self.rtcRoom = nil
ByteRTCVideo.destroyRTCVideo()
self.rtcVideo = nil
}
// MARK: Private method
@objc func joinRoom() {
let roomId = self.roomSettingItem.text ?? ""
let userId = self.userSettingItem.text ?? ""
var vaild = checkValid(roomId)
if vaild == false {
ToastComponents.shared.show(withMessage: LocalizedString("toast_check_valid_false"))
return
}
vaild = checkValid(userId)
if vaild == false {
ToastComponents.shared.show(withMessage: LocalizedString("toast_check_valid_false"))
return
}
joinButton.isSelected = !joinButton.isSelected
if joinButton.isSelected {
generatorToken(roomId: roomId, userId: userId) { [weak self] token in
self?.joinButton.setTitle(LocalizedString("button_leave_room"), for: .normal)
// Join room
self?.rtcRoom = self?.rtcVideo?.createRTCRoom(roomId)
self?.rtcRoom?.delegate = self
let userInfo = ByteRTCUserInfo.init()
userInfo.userId = userId
let roomCfg = ByteRTCRoomConfig.init()
roomCfg.isAutoPublish = true
roomCfg.isAutoSubscribeAudio = true
roomCfg.isAutoSubscribeVideo = true
self?.rtcRoom?.joinRoom(token, userInfo: userInfo, roomConfig: roomCfg)
}
} else {
self.joinButton.setTitle(LocalizedString("button_join_room"), for: .normal)
self.rtcRoom?.leaveRoom()
}
}
func buildRTCEngine() {
// Create engine
self.rtcVideo = ByteRTCVideo.createRTCVideo(kAppID, delegate: self, parameters: [:])
// Enable local audio and video collection
self.rtcVideo?.startVideoCapture()
self.rtcVideo?.startAudioCapture()
self.bindLocalRenderView()
// Set default merge parameters
self.mixConfig = ByteRTCMixedStreamConfig.default()
}
func bindLocalRenderView() {
// Set local rendering view
let canvas = ByteRTCVideoCanvas.init()
canvas.view = self.localView.videoView
canvas.renderMode = .hidden
self.localView.userId = userSettingItem.text ?? ""
self.rtcVideo?.setLocalVideoCanvas(.main, withCanvas: canvas);
}
func updateRenderView() {
// Get the first user of the room
var remoteUser:ByteRTCRemoteStreamKey?
for streamKey in self.users {
if remoteUser == nil && streamKey.roomId == self.roomSettingItem.text {
remoteUser = streamKey
}
}
if (remoteUser != nil) {
self.bindRemoteRenderView(view: self.firstRemoteView,roomId: (remoteUser?.roomId)!,userId: (remoteUser?.userId)!)
}
}
@objc func startPushCDN() {
if let text = self.urlTextFieldView.text, !text.isEmpty {
self.mixConfig?.pushURL = text
let roomId = roomSettingItem.text
let userId = userSettingItem.text
self.mixConfig?.layoutConfig.regions = self.getMixRegions()
self.mixConfig?.layoutConfig.backgroundColor = "#FFFFFF"
// Additional data transparently transmitted by the confluent user is received through the SEI of the puller.
self.mixConfig?.layoutConfig.userConfigExtraInfo = self.layoutTextFieldView.text ?? ""
self.mixConfig?.roomID = roomId!
self.mixConfig?.userID = userId!
self.rtcVideo?.startPushMixedStream(toCDN: taskId, mixedConfig: self.mixConfig, observer: self)
} else {
ToastComponents.shared.show(withMessage: LocalizedString("toast_mix_url_false"))
}
}
@objc func updatePushCDN() {
// Additional data transparently transmitted by the confluent user is received through the SEI of the puller.
self.mixConfig?.layoutConfig.userConfigExtraInfo = self.layoutTextFieldView.text ?? ""
self.rtcVideo?.updatePushMixedStream(toCDN: taskId, mixedConfig: self.mixConfig!)
}
@objc func stopPushCDN() {
self.rtcVideo?.stopPushStreamToCDN(taskId)
}
func getMixRegions() -> [ByteRTCMixedStreamLayoutRegionConfig] {
let roomId = roomSettingItem.text
let userId = userSettingItem.text
var regions = [ByteRTCMixedStreamLayoutRegionConfig]()
let width = 1.0
let height = 1.0
// Local user
let regionConfig = ByteRTCMixedStreamLayoutRegionConfig.init()
regionConfig.userID = userId!
regionConfig.roomID = roomId!
regionConfig.locationX = 0
regionConfig.locationY = 0
regionConfig.widthProportion = width
regionConfig.heightProportion = height
regionConfig.zOrder = 0
regionConfig.isLocalUser = true
regionConfig.mediaType = .audioAndVideo
regions.append(regionConfig)
return regions
}
// Send SEI information
@objc func sendSEIMessage() {
let message = self.seiTextFieldView.text;
if !message!.isEmpty, let data = message?.data(using: .utf8) {
self.rtcVideo?.sendSEIMessage(.main, andMessage: data, andRepeatCount: 3, andCountPerFrame: .single)
}else {
ToastComponents.shared.show(withMessage: LocalizedString("toast_send_message_empty_false"))
}
}
func bindRemoteRenderView(view: UserVideoView, roomId: String, userId: String) {
// Set the remote user video rendering view
let canvas = ByteRTCVideoCanvas.init()
canvas.view = view.videoView
canvas.renderMode = .hidden
view.userId = userId
let streamKey = ByteRTCRemoteStreamKey.init()
streamKey.userId = userId
streamKey.roomId = roomId;
streamKey.streamIndex = .main
self.rtcVideo?.setRemoteVideoCanvas(streamKey, withCanvas: canvas)
}
func createUI() -> Void {
view.addSubview(containerView)
containerView.snp.makeConstraints { make in
make.left.right.equalTo(self.view)
make.top.equalTo(topView.snp.bottom)
}
self.containerView.addSubview(localView)
self.containerView.addSubview(firstRemoteView)
self.localView.snp.makeConstraints { make in
make.left.top.equalTo(self.containerView)
make.height.equalTo(self.containerView)
make.width.equalTo(self.containerView).multipliedBy(0.5)
}
self.firstRemoteView.snp.makeConstraints { make in
make.right.bottom.equalTo(self.containerView)
make.height.equalTo(self.containerView)
make.width.equalTo(self.containerView).multipliedBy(0.5)
}
view.addSubview(roomSettingItem)
view.addSubview(userSettingItem)
view.addSubview(joinButton)
roomSettingItem.snp.makeConstraints { make in
make.top.equalTo(containerView.snp.bottom).offset(10)
make.left.equalToSuperview().offset(10)
make.height.equalTo(30)
}
userSettingItem.snp.makeConstraints { make in
make.centerY.equalTo(roomSettingItem)
make.left.equalTo(roomSettingItem.snp.right).offset(20)
make.right.equalToSuperview().offset(-10)
make.width.height.equalTo(roomSettingItem)
}
joinButton.snp.makeConstraints { make in
make.top.equalTo(roomSettingItem.snp.bottom).offset(10)
make.left.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.height.equalTo(36)
}
view.addSubview(seiTextFieldView)
view.addSubview(sendButton)
seiTextFieldView.snp.makeConstraints { make in
make.top.equalTo(joinButton.snp.bottom).offset(20)
make.left.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.height.equalTo(30)
}
sendButton.snp.makeConstraints { make in
make.top.equalTo(seiTextFieldView.snp.bottom).offset(10)
make.left.right.width.height.equalTo(joinButton)
}
view.addSubview(urlTextFieldView)
view.addSubview(layoutTextFieldView)
view.addSubview(startPushButton)
view.addSubview(updatePushButton)
view.addSubview(stopPushButton)
urlTextFieldView.snp.makeConstraints { make in
make.top.equalTo(sendButton.snp.bottom).offset(20)
make.left.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.height.equalTo(30)
}
layoutTextFieldView.snp.makeConstraints { make in
make.top.equalTo(urlTextFieldView.snp.bottom).offset(10)
make.left.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.height.equalTo(30)
}
startPushButton.snp.makeConstraints { make in
make.top.equalTo(layoutTextFieldView.snp.bottom).offset(10)
make.left.equalToSuperview().offset(10)
make.height.equalTo(30)
}
updatePushButton.snp.makeConstraints { make in
make.centerY.equalTo(startPushButton)
make.left.equalTo(startPushButton.snp.right).offset(20)
make.width.height.equalTo(startPushButton)
}
stopPushButton.snp.makeConstraints { make in
make.centerY.equalTo(startPushButton)
make.left.equalTo(updatePushButton.snp.right).offset(20)
make.right.equalToSuperview().offset(-10)
make.width.height.equalTo(startPushButton)
}
view.addSubview(receivedSEIItem)
receivedSEIItem.snp.makeConstraints { make in
make.top.equalTo(startPushButton.snp.bottom).offset(20)
make.left.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.height.equalTo(30)
make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-10)
}
}
// MARK: Lazy laod
lazy var roomSettingItem: TextFieldView = {
let settingView = TextFieldView()
settingView.title = LocalizedString("hint_room_id")
return settingView
}()
lazy var userSettingItem: TextFieldView = {
let settingView = TextFieldView()
settingView.title = LocalizedString("hint_user_id")
return settingView
}()
lazy var joinButton: UIButton = {
let button = BaseButton()
button.setTitle(LocalizedString("button_join_room"), for: .normal)
button.addTarget(self, action: #selector(joinRoom), for: .touchUpInside)
return button
}()
lazy var seiTextFieldView: TextFieldView = {
let settingView = TextFieldView()
settingView.title = LocalizedString("example_sei_messaging")
settingView.inputTextField.keyboardType = .default
return settingView
}()
lazy var sendButton: UIButton = {
let button = BaseButton()
button.setTitle(LocalizedString("button_send"), for: .normal)
button.addTarget(self, action: #selector(sendSEIMessage), for: .touchUpInside)
return button
}()
lazy var urlTextFieldView: TextFieldView = {
let settingView = TextFieldView()
settingView.title = LocalizedString("hint_cdn_url")
if let cdnUrl = kCDNUrl as String?, !cdnUrl.isEmpty {
settingView.text = cdnUrl
} else {
LiveAddrProtocol().getRTMPAddr(taskId) { [weak settingView] addr in
settingView?.text = addr
}
}
return settingView
}()
lazy var layoutTextFieldView: TextFieldView = {
let settingView = TextFieldView()
settingView.title = LocalizedString("label_setting_mix")
settingView.inputTextField.keyboardType = .default
return settingView
}()
lazy var startPushButton: UIButton = {
let button = BaseButton()
button.setTitle(LocalizedString("button_start_transcoding"), for: .normal)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.textAlignment = .center
button.addTarget(self, action: #selector(startPushCDN), for: .touchUpInside)
return button
}()
lazy var updatePushButton: UIButton = {
let button = BaseButton()
button.setTitle(LocalizedString("button_update_transcoding"), for: .normal)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.textAlignment = .center
button.addTarget(self, action: #selector(updatePushCDN), for: .touchUpInside)
return button
}()
lazy var stopPushButton: UIButton = {
let button = BaseButton()
button.setTitle(LocalizedString("button_stop_transcoding"), for: .normal)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.textAlignment = .center
button.addTarget(self, action: #selector(stopPushCDN), for: .touchUpInside)
return button
}()
lazy var receivedSEIItem: TextFieldView = {
let settingView = TextFieldView()
settingView.title = LocalizedString("hint_sei_message")
return settingView
}()
lazy var containerView: UIView = {
let view = UIView.init()
view.backgroundColor = .groupTableViewBackground
return view
}()
lazy var localView: UserVideoView = {
let view = UserVideoView.init()
return view
}()
lazy var firstRemoteView: UserVideoView = {
let view = UserVideoView.init()
return view
}()
// MARK: ByteRTCVideoDelegate & ByteRTCRoomDelegate
func rtcRoom(_ rtcRoom: ByteRTCRoom, onRoomStateChanged roomId: String, withUid uid: String, state: Int, extraInfo: String) {
ToastComponents.shared.show(withMessage: "onRoomStateChanged uid: \(uid) state:\(state)")
}
// Remote user publishing stream
func rtcRoom(_ rtcRoom: ByteRTCRoom, onUserPublishStream userId: String, type: ByteRTCMediaStreamType) {
ToastComponents.shared.show(withMessage: "onUserPublishStream uid: \(userId)")
if type == .video || type == .both {
let streamKey = ByteRTCRemoteStreamKey.init()
streamKey.userId = userId
streamKey.roomId = rtcRoom.getId();
streamKey.streamIndex = .main
self.users.append(streamKey)
DispatchQueue.main.async {
self.updateRenderView()
}
}
}
// Remote user cancels publishing flow
func rtcRoom(_ rtcRoom: ByteRTCRoom, onUserUnpublishStream userId: String, type: ByteRTCMediaStreamType, reason: ByteRTCStreamRemoveReason) {
ToastComponents.shared.show(withMessage: "onUserUnpublishStream uid: \(userId)")
if type == .video || type == .both {
// Remove from self.users
var itemsToRemove: [ByteRTCRemoteStreamKey] = []
for streamKey in self.users {
if streamKey.userId == userId {
itemsToRemove.append(streamKey)
}
}
for item in itemsToRemove {
if let index = self.users.firstIndex(of: item) {
self.users.remove(at: index)
}
}
DispatchQueue.main.async {
for videoView in self.containerView.subviews {
if let view = videoView as? UserVideoView {
let userId = view.userId
if userId == userId {
view.userId = ""
}
}
}
}
DispatchQueue.main.async {
self.updateRenderView()
}
}
}
// Remote users join the room
func rtcRoom(_ rtcRoom: ByteRTCRoom, onUserJoined userInfo: ByteRTCUserInfo, elapsed: Int) {
ToastComponents.shared.show(withMessage: "onUserJoined uid: \(userInfo.userId)")
}
// The remote user leaves the room
func rtcRoom(_ rtcRoom: ByteRTCRoom, onUserLeave uid: String, reason: ByteRTCUserOfflineReason) {
ToastComponents.shared.show(withMessage: "onUserLeave uid: \(uid)")
}
// Receive SEI information
func rtcEngine(_ engine: ByteRTCVideo, onSEIMessageReceived remoteStreamKey: ByteRTCRemoteStreamKey, andMessage message: Data) {
if let string = String(data: message, encoding: .utf8) {
ToastComponents.shared.show(withMessage: "onSEIMessageReceived: uid = \(remoteStreamKey.userId ?? ""), data = \(string)")
self.receivedSEIItem.text = string
}
}
// MARK: ByteRTCMixedStreamObserver
func isSupportClientPushStream() -> Bool {
return false
}
// Confluence event callback
func onMixingEvent(_ event: ByteRTCStreamMixingEvent, taskId: String, error errorCode: ByteRTCStreamMixingErrorCode, mix mixType: ByteRTCMixedStreamType) {
ToastComponents.shared.show(withMessage: "onMixingEvent:\(event.rawValue) taskId:\(taskId) errorCode:\(errorCode.rawValue) + mixType:\(mixType.rawValue)")
}
}