17
17
#import " LFGPUImageBeautyFilter.h"
18
18
#import " LFH264VideoEncoder.h"
19
19
20
- #define LFLiveReportKey @" com.youku.liveSessionReport"
21
20
22
21
@interface LFLiveSession ()<LFAudioCaptureDelegate, LFVideoCaptureDelegate, LFAudioEncodingDelegate, LFVideoEncodingDelegate, LFStreamSocketDelegate>
23
- {
24
- dispatch_semaphore_t _lock;
25
- }
26
- // /音频配置
22
+
23
+ // / 音频配置
27
24
@property (nonatomic , strong ) LFLiveAudioConfiguration *audioConfiguration;
28
- // /视频配置
25
+ // / 视频配置
29
26
@property (nonatomic , strong ) LFLiveVideoConfiguration *videoConfiguration;
30
27
// / 声音采集
31
28
@property (nonatomic , strong ) LFAudioCapture *audioCaptureSource;
@@ -38,17 +35,21 @@ @interface LFLiveSession ()<LFAudioCaptureDelegate, LFVideoCaptureDelegate, LFAu
38
35
// / 上传
39
36
@property (nonatomic , strong ) id <LFStreamSocket> socket;
40
37
38
+
41
39
#pragma mark -- 内部标识
42
- // / 上报
43
- @property (nonatomic , copy ) dispatch_block_t reportBlock;
44
- // / debugInfo
40
+ // / 调试信息
45
41
@property (nonatomic , strong ) LFLiveDebug *debugInfo;
46
- // / streamInfo
42
+ // / 流信息
47
43
@property (nonatomic , strong ) LFLiveStreamInfo *streamInfo;
48
- // / uploading
44
+ // / 是否开始上传
49
45
@property (nonatomic , assign ) BOOL uploading;
50
- // / state
46
+ // / 当前状态
51
47
@property (nonatomic , assign , readwrite ) LFLiveState state;
48
+ // / 当前直播type
49
+ @property (nonatomic , assign , readwrite ) LFLiveCaptureTypeMask captureType;
50
+
51
+ // / 时间戳锁
52
+ @property (nonatomic , strong ) dispatch_semaphore_t lock;
52
53
53
54
@end
54
55
@@ -68,20 +69,25 @@ @implementation LFLiveSession
68
69
69
70
#pragma mark -- LifeCycle
70
71
- (instancetype )initWithAudioConfiguration : (LFLiveAudioConfiguration *)audioConfiguration videoConfiguration : (LFLiveVideoConfiguration *)videoConfiguration {
71
- if (!audioConfiguration || !videoConfiguration) @throw [NSException exceptionWithName: @" LFLiveSession init error" reason: @" audioConfiguration or videoConfiguration is nil " userInfo: nil ];
72
+ return [self initWithAudioConfiguration: audioConfiguration videoConfiguration: videoConfiguration captureType: LFLiveCaptureDefaultMask];
73
+ }
74
+
75
+ - (nullable instancetype )initWithAudioConfiguration : (nullable LFLiveAudioConfiguration *)audioConfiguration videoConfiguration : (nullable LFLiveVideoConfiguration *)videoConfiguration captureType : (LFLiveCaptureTypeMask)captureType {
76
+ if ((captureType & LFLiveCaptureMaskAudio || captureType & LFLiveInputMaskAudio) && !audioConfiguration) @throw [NSException exceptionWithName: @" LFLiveSession init error" reason: @" audioConfiguration is nil " userInfo: nil ];
77
+ if ((captureType & LFLiveCaptureMaskVideo || captureType & LFLiveInputMaskVideo) && !videoConfiguration) @throw [NSException exceptionWithName: @" LFLiveSession init error" reason: @" videoConfiguration is nil " userInfo: nil ];
72
78
if (self = [super init ]) {
73
79
_audioConfiguration = audioConfiguration;
74
80
_videoConfiguration = videoConfiguration;
75
- _lock = dispatch_semaphore_create (1 );
76
81
_adaptiveBitrate = NO ;
77
82
_isFirstFrame = YES ;
83
+ _captureType = captureType;
78
84
}
79
85
return self;
80
86
}
81
87
82
88
- (void )dealloc {
83
- self.audioCaptureSource .running = NO ;
84
89
self.videoCaptureSource .running = NO ;
90
+ self.audioCaptureSource .running = NO ;
85
91
}
86
92
87
93
#pragma mark -- CustomMethod
@@ -90,6 +96,7 @@ - (void)startLive:(LFLiveStreamInfo *)streamInfo {
90
96
_streamInfo = streamInfo;
91
97
_streamInfo.videoConfiguration = _videoConfiguration;
92
98
_streamInfo.audioConfiguration = _audioConfiguration;
99
+ _streamInfo.needDropFrame = (self.captureType & LFLiveCaptureMaskVideo || self.captureType & LFLiveInputMaskVideo) ? YES : NO ;// < 有视频执行丢帧算法
93
100
[self .socket start ];
94
101
}
95
102
@@ -99,12 +106,24 @@ - (void)stopLive {
99
106
self.socket = nil ;
100
107
}
101
108
109
+ - (void )pushVideo : (CVPixelBufferRef)pixelBuffer {
110
+ if (self.captureType & LFLiveInputMaskVideo){
111
+ if (self.uploading ) [self .videoEncoder encodeVideoData: pixelBuffer timeStamp: self .currentTimestamp];
112
+ }
113
+ }
114
+
115
+ - (void )pushAudio : (AudioBufferList)audioBufferList {
116
+ if (self.captureType & LFLiveInputMaskAudio){
117
+ if (self.uploading ) [self .audioEncoder encodeAudioData: audioBufferList timeStamp: self .currentTimestamp];
118
+ }
119
+ }
120
+
102
121
#pragma mark -- CaptureDelegate
103
122
- (void )captureOutput : (nullable LFAudioCapture *)capture audioBuffer : (AudioBufferList)inBufferList {
104
123
if (self.uploading ) [self .audioEncoder encodeAudioData: inBufferList timeStamp: self .currentTimestamp];
105
124
}
106
125
107
- - (void )captureOutput : (nullable LFVideoCapture *)capture pixelBuffer : (nullable CVImageBufferRef )pixelBuffer {
126
+ - (void )captureOutput : (nullable LFVideoCapture *)capture pixelBuffer : (nullable CVPixelBufferRef )pixelBuffer {
108
127
if (self.uploading ) [self .videoEncoder encodeVideoData: pixelBuffer timeStamp: self .currentTimestamp];
109
128
}
110
129
@@ -156,18 +175,18 @@ - (void)socketDebug:(nullable id<LFStreamSocket>)socket debugInfo:(nullable LFLi
156
175
}
157
176
158
177
- (void )socketBufferStatus : (nullable id <LFStreamSocket>)socket status : (LFLiveBuffferState)status {
159
- if ( self.adaptiveBitrate ) {
160
- NSUInteger videoBitRate = [_videoEncoder videoBitRate ];
178
+ if (( self.captureType & LFLiveCaptureMaskVideo || self. captureType & LFLiveInputMaskVideo) && self. adaptiveBitrate ) {
179
+ NSUInteger videoBitRate = [self .videoEncoder videoBitRate ];
161
180
if (status == LFLiveBuffferDecline) {
162
181
if (videoBitRate < _videoConfiguration.videoMaxBitRate ) {
163
182
videoBitRate = videoBitRate + 50 * 1000 ;
164
- [_videoEncoder setVideoBitRate: videoBitRate];
183
+ [self .videoEncoder setVideoBitRate: videoBitRate];
165
184
NSLog (@" Increase bitrate %@ " , @(videoBitRate));
166
185
}
167
186
} else {
168
- if (videoBitRate > _videoConfiguration .videoMinBitRate ) {
187
+ if (videoBitRate > self. videoConfiguration .videoMinBitRate ) {
169
188
videoBitRate = videoBitRate - 100 * 1000 ;
170
- [_videoEncoder setVideoBitRate: videoBitRate];
189
+ [self .videoEncoder setVideoBitRate: videoBitRate];
171
190
NSLog (@" Decline bitrate %@ " , @(videoBitRate));
172
191
}
173
192
}
@@ -284,16 +303,20 @@ - (UIView*)warterMarkView{
284
303
285
304
- (LFAudioCapture *)audioCaptureSource {
286
305
if (!_audioCaptureSource) {
287
- _audioCaptureSource = [[LFAudioCapture alloc ] initWithAudioConfiguration: _audioConfiguration];
288
- _audioCaptureSource.delegate = self;
306
+ if (self.captureType & LFLiveCaptureMaskAudio){
307
+ _audioCaptureSource = [[LFAudioCapture alloc ] initWithAudioConfiguration: _audioConfiguration];
308
+ _audioCaptureSource.delegate = self;
309
+ }
289
310
}
290
311
return _audioCaptureSource;
291
312
}
292
313
293
314
- (LFVideoCapture *)videoCaptureSource {
294
315
if (!_videoCaptureSource) {
295
- _videoCaptureSource = [[LFVideoCapture alloc ] initWithVideoConfiguration: _videoConfiguration];
296
- _videoCaptureSource.delegate = self;
316
+ if (self.captureType & LFLiveCaptureMaskVideo){
317
+ _videoCaptureSource = [[LFVideoCapture alloc ] initWithVideoConfiguration: _videoConfiguration];
318
+ _videoCaptureSource.delegate = self;
319
+ }
297
320
}
298
321
return _videoCaptureSource;
299
322
}
@@ -320,7 +343,7 @@ - (LFVideoCapture *)videoCaptureSource {
320
343
321
344
- (id <LFStreamSocket>)socket {
322
345
if (!_socket) {
323
- _socket = [[LFStreamRTMPSocket alloc ] initWithStream: self .streamInfo videoSize: self .videoConfiguration.videoSize reconnectInterval: self .reconnectInterval reconnectCount: self .reconnectCount];
346
+ _socket = [[LFStreamRTMPSocket alloc ] initWithStream: self .streamInfo reconnectInterval: self .reconnectInterval reconnectCount: self .reconnectCount];
324
347
[_socket setDelegate: self ];
325
348
}
326
349
return _socket;
@@ -333,8 +356,15 @@ - (LFLiveStreamInfo *)streamInfo {
333
356
return _streamInfo;
334
357
}
335
358
359
+ - (dispatch_semaphore_t )lock {
360
+ if (!_lock){
361
+ _lock = dispatch_semaphore_create (1 );
362
+ }
363
+ return _lock;
364
+ }
365
+
336
366
- (uint64_t )currentTimestamp {
337
- dispatch_semaphore_wait (_lock , DISPATCH_TIME_FOREVER);
367
+ dispatch_semaphore_wait (self. lock , DISPATCH_TIME_FOREVER);
338
368
uint64_t currentts = 0 ;
339
369
if (_isFirstFrame) {
340
370
_timestamp = NOW;
@@ -343,7 +373,7 @@ - (uint64_t)currentTimestamp {
343
373
} else {
344
374
currentts = NOW - _timestamp;
345
375
}
346
- dispatch_semaphore_signal (_lock );
376
+ dispatch_semaphore_signal (self. lock );
347
377
return currentts;
348
378
}
349
379
0 commit comments