-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCustomVideoCaptureViewController.m
More file actions
148 lines (119 loc) · 5.16 KB
/
CustomVideoCaptureViewController.m
File metadata and controls
148 lines (119 loc) · 5.16 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
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
//
// CustomVideoCaptureViewController.m
// MLVB-API-Example-OC
//
// Created by bluedang on 2021/6/29.
// Copyright © 2021 Tencent. All rights reserved.
//
/*
Custom Video Capturing
Custom Video Capturing in MLVB App
This document shows how to integrate the custom video capturing feature.
1. Turn speaker on: [self.livePusher startMicrophone]
2. Enable custom capturing: [self.livePusher enableCustomVideoCapture:true]
3. Start publishing: [self.livePusher startPush:[LiveUrl generateTRTCPushUrl:streamId]]
4. Send data: [self.livePusher sendCustomVideoFrame:videoFrame]
Documentation: https://cloud.tencent.com/document/product/454/56601
*/
#import "CustomVideoCaptureViewController.h"
#import "CustomCameraHelper.h"
@interface CustomVideoCaptureViewController () <CustomCameraHelperSampleBufferDelegate>
@property (weak, nonatomic) IBOutlet UILabel *streamIdLabel;
@property (weak, nonatomic) IBOutlet UITextField *streamIdTextField;
@property (weak, nonatomic) IBOutlet UIButton *startPushButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@property (strong, nonatomic) V2TXLivePusher *livePusher;
@property (strong, nonatomic) CustomCameraHelper *customVideoCaputre;
@end
@implementation CustomVideoCaptureViewController
- (V2TXLivePusher *)livePusher {
if (!_livePusher) {
_livePusher = [[V2TXLivePusher alloc] initWithLiveMode:V2TXLiveMode_RTMP];
}
return _livePusher;
}
- (CustomCameraHelper *)customVideoCaputre {
if (!_customVideoCaputre) {
_customVideoCaputre = [[CustomCameraHelper alloc] init];
[_customVideoCaputre setDelegate:self];
}
return _customVideoCaputre;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupDefaultUIConfig];
[self addKeyboardObserver];
[self.customVideoCaputre checkPermission];
[self.customVideoCaputre createSession];
}
- (void)dealloc {
[self removeKeyboardObserver];
}
- (void)setupDefaultUIConfig {
self.streamIdTextField.text = [NSString generateRandomStreamId];
self.streamIdLabel.text = localize(@"MLVB-API-Example.CustomVideoCapture.streamIdInput");
self.streamIdLabel.adjustsFontSizeToFitWidth = true;
self.startPushButton.backgroundColor = [UIColor themeBlueColor];
[self.startPushButton setTitle:localize(@"MLVB-API-Example.CustomVideoCapture.startPush") forState:UIControlStateNormal];
[self.startPushButton setTitle:localize(@"MLVB-API-Example.CustomVideoCapture.stopPush") forState:UIControlStateSelected];
self.startPushButton.titleLabel.adjustsFontSizeToFitWidth = true;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:true];
}
- (void)startPush:(NSString*)streamId {
[self.livePusher setRenderView:self.view];
[self.livePusher enableCustomVideoCapture:true];
[self.livePusher startMicrophone];
[self.customVideoCaputre startCameraCapture];
[self.livePusher startPush:[URLUtils generateRtmpPushUrl:streamId]];
}
- (void)stopPush {
[self.customVideoCaputre stopCameraCapture];
[self.livePusher stopMicrophone];
[self.livePusher stopPush];
}
#pragma mark - Actions
- (IBAction)onStartPushButtonClick:(UIButton*)sender {
sender.selected = !sender.isSelected;
if (sender.selected) {
NSString* streamId = self.streamIdTextField.text;
self.title = streamId;
[self startPush:streamId];
} else {
[self stopPush];
}
}
#pragma mark - CustomCameraHelperSampleBufferDelegate
- (void)onVideoSampleBuffer:(CMSampleBufferRef)videoBuffer {
V2TXLiveVideoFrame *videoFrame = [[V2TXLiveVideoFrame alloc] init];
videoFrame.bufferType = V2TXLiveBufferTypePixelBuffer;
videoFrame.pixelFormat = V2TXLivePixelFormatNV12;
videoFrame.pixelBuffer = CMSampleBufferGetImageBuffer(videoBuffer);
[self.livePusher sendCustomVideoFrame:videoFrame];
}
#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 = 25;
}];
return YES;
}
@end