Skip to content

Commit 244c451

Browse files
committed
优化双向音视频编码器
tapd: http://tapd.oa.com/20393192/prong/stories/view/1020393192868332011 Change-Id: Iedad5d5d9e4391b4fc0ebe8d190ea35e27701690 (cherry picked from commit b5dfc4b)
1 parent 725b187 commit 244c451

File tree

7 files changed

+49
-24
lines changed

7 files changed

+49
-24
lines changed

Podfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ PODS:
135135
- TIoTLinkKit_IJKPlayer (1.0.2)
136136
- TIoTLinkKit_TPNS (1.0.1)
137137
- TIoTLinkKit_WechatOpenSDK (1.0.1)
138-
- TIoTLinkKit_XP2P (2.4.2-beta.202112231310)
138+
- TIoTLinkKit_XP2P (2.4.3-beta.202201111002)
139139
- TIoTLinkVideo (1.0.0):
140140
- CocoaAsyncSocket (= 7.6.5)
141141
- TIoTLinkKit_FLV (= 2.2.3)
142-
- TIoTLinkKit_XP2P (= 2.4.2-beta.202112231310)
142+
- TIoTLinkKit_XP2P (~> 2.4.3-beta.1)
143143
- TrueTime (5.0.3)
144144
- TXLiteAVSDK_TRTC (8.0.9644)
145145
- TZImagePickerController (3.2.1)
@@ -258,8 +258,8 @@ SPEC CHECKSUMS:
258258
TIoTLinkKit_IJKPlayer: e47c3fed6b204e7544f84a24310cab7a372854d1
259259
TIoTLinkKit_TPNS: 7cea4fc1d20ef6c4c11c4f0b66bbb10ecf4ed968
260260
TIoTLinkKit_WechatOpenSDK: 7822d68cc5c46edf3f6020422202e7b65ea87614
261-
TIoTLinkKit_XP2P: b62f5c1e25c973d309ab45b3f65e5f1ed2d88e0a
262-
TIoTLinkVideo: c6f592694d6b817ce5e0cd84913f3e90e6a2ac27
261+
TIoTLinkKit_XP2P: fcec02f312445e15cdf77e7ff714874a0e1e0753
262+
TIoTLinkVideo: 5934f2e0d052e8612f76bdefce6759bc241df6e1
263263
TrueTime: b49551ffafb28a9dee04e51b226f42a416010842
264264
TXLiteAVSDK_TRTC: e3383a81565e8bb2aaaaab4bd099ad5239cd5b2d
265265
TZImagePickerController: bf4c57b98d8707fce41ea6be872414a71c7a8c9d

Source/LinkSDKDemo/Video/P2P/Controller/TIoTDemoPreviewDeviceVC.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -642,22 +642,22 @@ - (void)changeVideoDefinitaion {
642642
TIoTDemoCustomSheetView *definitaionSheet = [[TIoTDemoCustomSheetView alloc]init];
643643
NSArray *actionTitleArray = @[@"超清 1080P",@"高清 720P",@"标清 360P",@"取消"];
644644
ChooseFunctionBlock superDefinitaionBlock = ^(TIoTDemoCustomSheetView *view){
645-
self.qualityString = quality_super;
646-
[self resetVideoPlayerWithQuality:self.qualityString];
645+
weakSelf.qualityString = quality_super;
646+
[weakSelf resetVideoPlayerWithQuality:weakSelf.qualityString];
647647
[weakSelf.definitionBtn setTitle:@"超清" forState:UIControlStateNormal];
648648
[definitaionSheet removeFromSuperview];
649649
};
650650

651651
ChooseFunctionBlock highDefinitionBlock = ^(TIoTDemoCustomSheetView *view){
652-
self.qualityString = quality_high;
653-
[self resetVideoPlayerWithQuality:self.qualityString];
652+
weakSelf.qualityString = quality_high;
653+
[weakSelf resetVideoPlayerWithQuality:weakSelf.qualityString];
654654
[weakSelf.definitionBtn setTitle:@"高清" forState:UIControlStateNormal];
655655
[definitaionSheet removeFromSuperview];
656656
};
657657

658658
ChooseFunctionBlock standardDefinitionBlock = ^(TIoTDemoCustomSheetView *view){
659-
self.qualityString = quality_standard;
660-
[self resetVideoPlayerWithQuality:self.qualityString];
659+
weakSelf.qualityString = quality_standard;
660+
[weakSelf resetVideoPlayerWithQuality:weakSelf.qualityString];
661661
[weakSelf.definitionBtn setTitle:@"标清" forState:UIControlStateNormal];
662662
[definitaionSheet removeFromSuperview];
663663
};

Source/SDK/LinkVideo/FLV/TIoTAVCaptionFLV.mm

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ - (void)setupVideoCapture {
112112
self.h264Encoder = [TIoTH264Encoder new];
113113
[self.h264Encoder initWithConfiguration];
114114
[self.h264Encoder initEncode:480 height:640];
115+
// [self.h264Encoder start:480 height:640];
115116
self.h264Encoder.delegate = self;
116117

117118
if ([_session canSetSessionPreset:AVCaptureSessionPreset1280x720]) {
@@ -178,12 +179,12 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CM
178179
[self.aacEncoder encodeSampleBuffer:sampleBuffer completionBlock:^(NSData *encodedData, NSError *error) {
179180

180181
if (encodedData) {
181-
NSLog(@"Audio data (%lu):%@", (unsigned long)encodedData.length,encodedData.description);
182+
// NSLog(@"Audio data (%lu):%@", (unsigned long)encodedData.length,encodedData.description);
182183

183184
[self.data appendData:encodedData];
184185
encodeFlvData(0, encodedData);
185186
}else {
186-
NSLog(@"Error encoding AAC: %@", error);
187+
// NSLog(@"Error encoding AAC: %@", error);
187188
}
188189
}];
189190
}
@@ -210,7 +211,7 @@ - (void)gotSpsPps:(NSData*)sps pps:(NSData*)pps {
210211
}
211212
- (void)gotEncodedData:(NSData*)data isKeyFrame:(BOOL)isKeyFrame {
212213

213-
NSLog(@"Video data (%lu):%@", (unsigned long)data.length,data.description);
214+
// NSLog(@"Video data (%lu):%@", (unsigned long)data.length,data.description);
214215

215216
if (_fileHandle != NULL)
216217
{
@@ -236,7 +237,7 @@ void flv_init_load() {
236237

237238
static int flv_onmuxer(void* flv, int type, const void* data, size_t bytes, uint32_t timestamp)
238239
{
239-
NSLog(@"========= flv_onmuxer type: %d, size: %zu", type,bytes);
240+
// NSLog(@"========= flv_onmuxer type: %d, size: %zu", type,bytes);
240241
return flv_writer_input(flv, type, data, bytes, timestamp);
241242
}
242243

@@ -247,7 +248,7 @@ static int flv_onwrite(void *param, const struct flv_vec_t* vec, int n) {
247248
total_size += vec[i].len;
248249
}
249250

250-
NSLog(@"========= flv_onmuxer total size: %d", total_size);
251+
// NSLog(@"========= flv_onmuxer total size: %d", total_size);
251252
char* bytes = new char[total_size];
252253
for(int i = 0, offset = 0; i < n; i++) {
253254
memcpy(bytes + offset, vec[i].ptr, vec[i].len);
@@ -277,7 +278,7 @@ int encodeFlvData(int type, NSData *packetData) {
277278

278279
const void *c_data = packetData.bytes;
279280
NSUInteger len = packetData.length;
280-
NSLog(@"===========================------------ %ld, pts: %u", len, pts);
281+
// NSLog(@"===========================------------ %ld, pts: %u", len, pts);
281282

282283
int ret = 0;
283284
if (type == 0) { //audio

Source/SDK/LinkVideo/FLV/TIoTH264Encoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@interface TIoTH264Encoder : NSObject
1313

1414
- (void) initWithConfiguration;
15-
- (void) start:(int)width height:(int)height;
15+
//- (void) start:(int)width height:(int)height;
1616
- (void) initEncode:(int)width height:(int)height;
1717
- (void) encode:(CMSampleBufferRef )sampleBuffer;
1818
- (void) End;

Source/SDK/LinkVideo/FLV/TIoTH264Encoder.m

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void didCompressH264(void *outputCallbackRefCon, void *sourceFrameRefCon, OSStat
102102

103103
}
104104

105-
- (void) start:(int)width height:(int)height
105+
/*- (void) start:(int)width height:(int)height
106106
{
107107
int frameSize = (width * height * 1.5);
108108
@@ -241,7 +241,7 @@ - (void) start:(int)width height:(int)height
241241
});
242242
243243
244-
}
244+
}*/
245245
- (void) initEncode:(int)width height:(int)height
246246
{
247247
dispatch_sync(aQueue, ^{
@@ -262,9 +262,33 @@ - (void) initEncode:(int)width height:(int)height
262262
}
263263

264264
// Set the properties
265+
// 设置实时编码输出(避免延迟)
265266
VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue);
266-
VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_Main_AutoLevel);
267+
VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_Baseline_AutoLevel);
268+
269+
//关闭B帧
270+
VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_AllowFrameReordering, kCFBooleanFalse);
271+
272+
// 设置关键帧(GOPsize)间隔
273+
int frameInterval = 10;
274+
CFNumberRef frameIntervalRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &frameInterval);
275+
VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_MaxKeyFrameInterval, frameIntervalRef);
276+
277+
// 设置期望帧率
278+
int fps = 10;
279+
CFNumberRef fpsRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &fps);
280+
VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_ExpectedFrameRate, fpsRef);
281+
282+
283+
//设置码率,均值,单位是byte
284+
int bitRate = width * height * 3 * 4 * 8;
285+
CFNumberRef bitRateRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &bitRate);
286+
VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_AverageBitRate, bitRateRef);
267287

288+
//设置码率,上限,单位是bps
289+
int bitRateLimit = width * height * 3 * 4;
290+
CFNumberRef bitRateLimitRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &bitRateLimit);
291+
VTSessionSetProperty(EncodingSession, kVTCompressionPropertyKey_DataRateLimits, bitRateLimitRef);
268292

269293
// Tell the encoder to start encoding
270294
VTCompressionSessionPrepareToEncodeFrames(EncodingSession);

Source/SDK/LinkVideo/TIoTCoreXP2PBridge.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,10 @@ - (void)stopService:(NSString *)dev_name {
300300
#pragma mark -AWAVCaptureDelegate
301301
- (void)capture:(uint8_t *)data len:(size_t)size {
302302
if (self.isSending) {
303-
NSLog(@"vide stream data:%s size:%zu",data,size);
303+
// NSLog(@"vide stream data:%s size:%zu",data,size);
304304
dataSend(self.dev_name.UTF8String, data, size);
305-
NSData *dataTag = [NSData dataWithBytes:data length:size];
306-
[fileHandle writeData:dataTag];
305+
// NSData *dataTag = [NSData dataWithBytes:data length:size];
306+
// [fileHandle writeData:dataTag];
307307
}
308308
}
309309

TIoTLinkVideo.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Pod::Spec.new do |s|
1717
s.static_framework = true
1818

1919
s.source_files = 'Source/SDK/LinkVideo/**/*.{h,m,c,mm}'
20-
20+
2121
s.dependency 'TIoTLinkKit_XP2P', '~> 2.4.3-beta.1'
2222
s.dependency 'TIoTLinkKit_FLV', '2.2.3'
2323
s.dependency 'CocoaAsyncSocket', '7.6.5'

0 commit comments

Comments
 (0)