/
SetVideoQualityViewController.m
332 lines (269 loc) · 12.5 KB
/
SetVideoQualityViewController.m
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
//
// SetVideoQualityViewController.m
// TRTC-API-Example-OC
//
// Created by bluedang on 2021/4/19.
//
/*
设置画面质量功能
TRTC APP 设置画面质量功能
本文件展示如何集成设置画面质量功能
1、设置分辨率 API: [self.trtcCloud setVideoEncoderParam:self.videoEncParam];
2、设置码率 API: [self.trtcCloud setVideoEncoderParam:self.videoEncParam];
3、设置帧率 API: [self.trtcCloud setVideoEncoderParam:self.videoEncParam];
参考文档:https://cloud.tencent.com/document/product/647/32236
*/
/*
Setting Video Quality
TRTC Video Quality Setting
This document shows how to integrate the video quality setting feature.
1. Set resolution: [self.trtcCloud setVideoEncoderParam:self.videoEncParam]
2. Set bitrate: [self.trtcCloud setVideoEncoderParam:self.videoEncParam]
3. Set frame rate: [self.trtcCloud setVideoEncoderParam:self.videoEncParam]
Documentation: https://cloud.tencent.com/document/product/647/32236
*/
#import "SetVideoQualityViewController.h"
static const NSInteger maxRemoteUserNum = 6;
@interface BitrateRange : NSObject
@property (assign, nonatomic) UInt32 minBitrate;
@property (assign, nonatomic) UInt32 maxBitrate;
@property (assign, nonatomic) UInt32 defaultBitrate;
- (instancetype)initWithMinBitrate:(UInt32)minBitrate maxBitRate:(UInt32)maxBitrate defaultBitrate:(UInt32)defaultBitrate;
@end
@interface SetVideoQualityViewController () <TRTCCloudDelegate>
@property (weak, nonatomic) IBOutlet UILabel *roomIdLabel;
@property (weak, nonatomic) IBOutlet UILabel *userIdLabel;
@property (weak, nonatomic) IBOutlet UILabel *chooseBitrateLabel;
@property (weak, nonatomic) IBOutlet UILabel *chooseFpsLabel;
@property (weak, nonatomic) IBOutlet UILabel *chooseResolutionLabel;
@property (weak, nonatomic) IBOutlet UILabel *bitrateLabel;
@property (weak, nonatomic) IBOutlet UILabel *fpsLabel;
@property (weak, nonatomic) IBOutlet UITextField *roomIdTextField;
@property (weak, nonatomic) IBOutlet UITextField *userIdTextField;
@property (weak, nonatomic) IBOutlet UIButton *video360PButton;
@property (weak, nonatomic) IBOutlet UIButton *video540PButton;
@property (weak, nonatomic) IBOutlet UIButton *video720PButton;
@property (weak, nonatomic) IBOutlet UIButton *video1080PButton;
@property (weak, nonatomic) IBOutlet UIButton *startPublisherButton;
@property (weak, nonatomic) IBOutlet UISlider *bitrateSlider;
@property (weak, nonatomic) IBOutlet UISlider *fpsSlider;
@property (weak, nonatomic) IBOutlet UIView *localVideoView;
@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *remoteViewArr;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@property (strong, nonatomic) TRTCCloud *trtcCloud;
@property (strong, nonatomic) TRTCVideoEncParam *videoEncParam;
@property (assign, nonatomic) TRTCVideoResolution videoResolution;
@property (strong, nonatomic) NSMutableOrderedSet *remoteUidSet;
@property (strong, nonatomic) NSMutableDictionary *bitrateDic;
@end
@implementation SetVideoQualityViewController
- (TRTCCloud*)trtcCloud {
if (!_trtcCloud) {
_trtcCloud = [TRTCCloud sharedInstance];
}
return _trtcCloud;
}
- (TRTCVideoEncParam *)videoEncParam {
if (!_videoEncParam) {
_videoEncParam = [[TRTCVideoEncParam alloc] init];
}
return _videoEncParam;
}
- (NSMutableOrderedSet *)remoteUidSet {
if (!_remoteUidSet) {
_remoteUidSet = [[NSMutableOrderedSet alloc]initWithCapacity:maxRemoteUserNum];
}
return _remoteUidSet;
}
- (void)viewDidLoad {
[super viewDidLoad];
_videoResolution = TRTCVideoResolution_960_540;
self.trtcCloud.delegate = self;
[self setupBitrateDic];
[self setupRandomId];
[self setupDefaultUIConfig];
}
- (void)dealloc {
[self destroyTRTCCloud];
}
- (void)setupDefaultUIConfig {
self.title = Localize(@"TRTC-API-Example.SetVideoQuality.Title");
_roomIdLabel.text = Localize(@"TRTC-API-Example.SetVideoQuality.roomId");
_userIdLabel.text = Localize(@"TRTC-API-Example.SetVideoQuality.userId");
_chooseBitrateLabel.text = Localize(@"TRTC-API-Example.SetVideoQuality.chooseBitrate");
_chooseFpsLabel.text = Localize(@"TRTC-API-Example.SetVideoQuality.chooseFps");
_chooseResolutionLabel.text = Localize(@"TRTC-API-Example.SetVideoQuality.chooseResolution");
[_startPublisherButton setTitle:Localize(@"TRTC-API-Example.SetVideoQuality.start") forState:UIControlStateNormal];
[_startPublisherButton setTitle:Localize(@"TRTC-API-Example.SetVideoQuality.stop") forState:UIControlStateSelected];
_fpsSlider.value = 15;
_roomIdLabel.adjustsFontSizeToFitWidth = true;
_userIdLabel.adjustsFontSizeToFitWidth = true;
_chooseFpsLabel.adjustsFontSizeToFitWidth = true;
_chooseBitrateLabel.adjustsFontSizeToFitWidth = true;
_chooseResolutionLabel.adjustsFontSizeToFitWidth = true;
_roomIdTextField.adjustsFontSizeToFitWidth = true;
_userIdTextField.adjustsFontSizeToFitWidth = true;
[self refreshBitrateSlider];
[self addKeyboardObserver];
}
- (void)setupBitrateDic {
if (!_bitrateDic) {
_bitrateDic = [NSMutableDictionary new];
[_bitrateDic setObject:[[BitrateRange alloc] initWithMinBitrate:200 maxBitRate:1000 defaultBitrate:800] forKey:[@(TRTCVideoResolution_640_360) stringValue]];
[_bitrateDic setObject:[[BitrateRange alloc] initWithMinBitrate:400 maxBitRate:1600 defaultBitrate:900] forKey:[@(TRTCVideoResolution_960_540) stringValue]];
[_bitrateDic setObject:[[BitrateRange alloc] initWithMinBitrate:500 maxBitRate:2000 defaultBitrate:1250] forKey:[@(TRTCVideoResolution_1280_720) stringValue]];
[_bitrateDic setObject:[[BitrateRange alloc] initWithMinBitrate:800 maxBitRate:3000 defaultBitrate:1900] forKey:[@(TRTCVideoResolution_1920_1080) stringValue]];
}
}
- (void)setupRandomId {
_roomIdTextField.text = [NSString generateRandomRoomNumber];
_userIdTextField.text = [NSString generateRandomUserId];
_roomIdTextField.textColor = [UIColor themeGrayColor];
_userIdTextField.textColor = [UIColor themeGrayColor];
_roomIdTextField.enabled = false;
_userIdTextField.enabled = false;
}
- (void)setupTRTCCloud {
[self.trtcCloud startLocalPreview:YES view:_localVideoView];
TRTCParams *params = [TRTCParams new];
params.sdkAppId = SDKAppID;
params.roomId = [_roomIdTextField.text intValue];
params.userId = _userIdTextField.text;
params.role = TRTCRoleAnchor;
params.userSig = [GenerateTestUserSig genTestUserSig:params.userId];
[self.trtcCloud enterRoom:params appScene:TRTCAppSceneAudioCall];
self.videoEncParam.videoResolution = _videoResolution;
self.videoEncParam.videoBitrate = (UInt32)_bitrateSlider.value;
self.videoEncParam.videoFps = (UInt32)_fpsSlider.value;
[self.trtcCloud setVideoEncoderParam:self.videoEncParam];
}
- (void)destroyTRTCCloud {
[TRTCCloud destroySharedIntance];
_trtcCloud = nil;
[self removeKeyboardObserver];
}
- (void)refreshBitrateSlider {
BitrateRange *bitrateRange = [_bitrateDic objectForKey:[@(_videoResolution) stringValue]];
_bitrateSlider.maximumValue = bitrateRange.maxBitrate;
_bitrateSlider.minimumValue = bitrateRange.minBitrate;
_bitrateSlider.value = bitrateRange.defaultBitrate;
[self onBitrateChanged:_bitrateSlider];
}
- (void)refreshEncParam {
self.videoEncParam.videoResolution = _videoResolution;
self.videoEncParam.videoBitrate = (UInt32)_bitrateSlider.value;
self.videoEncParam.videoFps = (UInt32)_fpsSlider.value;
[self.trtcCloud setVideoEncoderParam:self.videoEncParam];
}
# pragma mark - IBActions
- (IBAction)onVideo360PClick:(id)sender {
_videoResolution = TRTCVideoResolution_640_360;
_video360PButton.backgroundColor = [UIColor themeGreenColor];
_video540PButton.backgroundColor = [UIColor themeGrayColor];
_video720PButton.backgroundColor = [UIColor themeGrayColor];
_video1080PButton.backgroundColor = [UIColor themeGrayColor];
[self refreshBitrateSlider];
[self refreshEncParam];
}
- (IBAction)onVideo540PClick:(id)sender {
_videoResolution = TRTCVideoResolution_960_540;
_video360PButton.backgroundColor = [UIColor themeGrayColor];
_video540PButton.backgroundColor = [UIColor themeGreenColor];
_video720PButton.backgroundColor = [UIColor themeGrayColor];
_video1080PButton.backgroundColor = [UIColor themeGrayColor];
[self refreshBitrateSlider];
[self refreshEncParam];
}
- (IBAction)onVideo720PClick:(id)sender {
_videoResolution = TRTCVideoResolution_1280_720;
_video360PButton.backgroundColor = [UIColor themeGrayColor];
_video540PButton.backgroundColor = [UIColor themeGrayColor];
_video720PButton.backgroundColor = [UIColor themeGreenColor];
_video1080PButton.backgroundColor = [UIColor themeGrayColor];
[self refreshBitrateSlider];
[self refreshEncParam];
}
- (IBAction)onVideo1080PClick:(id)sender {
_videoResolution = TRTCVideoResolution_1920_1080;
_video360PButton.backgroundColor = [UIColor themeGrayColor];
_video540PButton.backgroundColor = [UIColor themeGrayColor];
_video720PButton.backgroundColor = [UIColor themeGrayColor];
_video1080PButton.backgroundColor = [UIColor themeGreenColor];
[self refreshBitrateSlider];
[self refreshEncParam];
}
- (IBAction)onStartButtonClick:(UIButton*)sender {
if ([sender isSelected]) {
self.title = Localize(@"TRTC-API-Example.SetVideoQuality.Title");
[self.trtcCloud exitRoom];
[self destroyTRTCCloud];
} else {
self.title = [Localize(@"TRTC-API-Example.SetVideoQuality.Title") stringByAppendingString:_roomIdTextField.text];
[self setupTRTCCloud];
}
sender.selected = !sender.selected;
}
- (IBAction)onBitrateChanged:(id)sender {
_bitrateLabel.text = [[@((UInt32)_bitrateSlider.value) stringValue] stringByAppendingString:@" kbps"];
[self refreshEncParam];
}
- (IBAction)onFpsChanged:(id)sender {
_fpsLabel.text = [[@((UInt32)_fpsSlider.value) stringValue] stringByAppendingString:@" fps"];
[self refreshEncParam];
}
#pragma mark - Notification
- (void)addKeyboardObserver {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)removeKeyboardObserver {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (BOOL)keyboardWillShow:(NSNotification *)noti {
CGFloat animationDuration = [[[noti userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect keyboardBounds = [[[noti userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:animationDuration animations:^{
self.bottomConstraint.constant = keyboardBounds.size.height;
}];
return YES;
}
- (BOOL)keyboardWillHide:(NSNotification *)noti {
CGFloat animationDuration = [[[noti userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:animationDuration animations:^{
self.bottomConstraint.constant = 20;
}];
return YES;
}
#pragma mark - TRTCCloud Delegate
- (void)onUserVideoAvailable:(NSString *)userId available:(BOOL)available {
NSInteger index = [self.remoteUidSet indexOfObject:userId];
if (available) {
if (index != NSNotFound) { return; }
[_remoteUidSet addObject:userId];
} else {
[_trtcCloud stopRemoteView:userId streamType:TRTCVideoStreamTypeSmall];
[_remoteUidSet removeObject:userId];
}
[self refreshRemoteVideoViews];
}
- (void)refreshRemoteVideoViews {
NSInteger index = 0;
for (NSString* userId in _remoteUidSet) {
if (index >= maxRemoteUserNum) { return; }
[_remoteViewArr[index] setHidden:false];
[_trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeSmall
view:_remoteViewArr[index++]];
}
}
@end
#pragma mark - BitrateRange
@implementation BitrateRange
- (instancetype)initWithMinBitrate:(UInt32)minBitrate maxBitRate:(UInt32)maxBitrate defaultBitrate:(UInt32)defaultBitrate {
BitrateRange *bitrateRange = [BitrateRange new];
bitrateRange.minBitrate = minBitrate;
bitrateRange.maxBitrate = maxBitrate;
bitrateRange.defaultBitrate = defaultBitrate;
return bitrateRange;
}
@end