17
17
18
18
#define LFLiveReportKey @" com.youku.liveSessionReport"
19
19
20
- @interface LFLiveSession ()<LFAudioCaptureDelegate,LFVideoCaptureDelegate,LFAudioEncodingDelegate,LFVideoEncodingDelegate,LFStreamSocketDelegate>
20
+ @interface LFLiveSession ()<LFAudioCaptureDelegate, LFVideoCaptureDelegate, LFAudioEncodingDelegate, LFVideoEncodingDelegate, LFStreamSocketDelegate>
21
21
{
22
22
dispatch_semaphore_t _lock;
23
23
}
@@ -46,7 +46,7 @@ @interface LFLiveSession ()<LFAudioCaptureDelegate,LFVideoCaptureDelegate,LFAudi
46
46
// / uploading
47
47
@property (nonatomic , assign ) BOOL uploading;
48
48
// / state
49
- @property (nonatomic ,assign ,readwrite ) LFLiveState state;
49
+ @property (nonatomic , assign , readwrite ) LFLiveState state;
50
50
51
51
@end
52
52
@@ -63,135 +63,135 @@ @interface LFLiveSession ()
63
63
@implementation LFLiveSession
64
64
65
65
#pragma mark -- LifeCycle
66
- - (instancetype )initWithAudioConfiguration : (LFLiveAudioConfiguration *)audioConfiguration videoConfiguration : (LFLiveVideoConfiguration *)videoConfiguration {
67
- if (!audioConfiguration || !videoConfiguration) @throw [NSException exceptionWithName: @" LFLiveSession init error" reason: @" audioConfiguration or videoConfiguration is nil " userInfo: nil ];
68
- if (self = [super init ]){
66
+ - (instancetype )initWithAudioConfiguration : (LFLiveAudioConfiguration *)audioConfiguration videoConfiguration : (LFLiveVideoConfiguration *)videoConfiguration {
67
+ if (!audioConfiguration || !videoConfiguration) @throw [NSException exceptionWithName: @" LFLiveSession init error" reason: @" audioConfiguration or videoConfiguration is nil " userInfo: nil ];
68
+ if (self = [super init ]) {
69
69
_audioConfiguration = audioConfiguration;
70
70
_videoConfiguration = videoConfiguration;
71
71
_lock = dispatch_semaphore_create (1 );
72
72
}
73
73
return self;
74
74
}
75
75
76
- - (void )dealloc {
76
+ - (void )dealloc {
77
77
self.audioCaptureSource .running = NO ;
78
78
self.videoCaptureSource .running = NO ;
79
79
}
80
80
81
81
#pragma mark -- CustomMethod
82
- - (void )startLive : (LFLiveStreamInfo*)streamInfo {
83
- if (!streamInfo) return ;
82
+ - (void )startLive : (LFLiveStreamInfo *)streamInfo {
83
+ if (!streamInfo) return ;
84
84
_streamInfo = streamInfo;
85
85
_streamInfo.videoConfiguration = _videoConfiguration;
86
86
_streamInfo.audioConfiguration = _audioConfiguration;
87
87
[self .socket start ];
88
88
}
89
89
90
- - (void )stopLive {
90
+ - (void )stopLive {
91
91
self.uploading = NO ;
92
92
[self .socket stop ];
93
93
}
94
94
95
95
#pragma mark -- CaptureDelegate
96
- - (void )captureOutput : (nullable LFAudioCapture*)capture audioBuffer : (AudioBufferList)inBufferList {
96
+ - (void )captureOutput : (nullable LFAudioCapture *)capture audioBuffer : (AudioBufferList)inBufferList {
97
97
[self .audioEncoder encodeAudioData: inBufferList timeStamp: self .currentTimestamp];
98
98
}
99
99
100
- - (void )captureOutput : (nullable LFVideoCapture*)capture pixelBuffer : (nullable CVImageBufferRef)pixelBuffer {
100
+ - (void )captureOutput : (nullable LFVideoCapture *)capture pixelBuffer : (nullable CVImageBufferRef)pixelBuffer {
101
101
[self .videoEncoder encodeVideoData: pixelBuffer timeStamp: self .currentTimestamp];
102
102
}
103
103
104
104
#pragma mark -- EncoderDelegate
105
- - (void )audioEncoder : (nullable id <LFAudioEncoding>)encoder audioFrame : (nullable LFAudioFrame*)frame {
106
- if (self.uploading ) [self .socket sendFrame: frame];// <上传
105
+ - (void )audioEncoder : (nullable id <LFAudioEncoding>)encoder audioFrame : (nullable LFAudioFrame *)frame {
106
+ if (self.uploading ) [self .socket sendFrame: frame]; // <上传
107
107
}
108
108
109
- - (void )videoEncoder : (nullable id <LFVideoEncoding>)encoder videoFrame : (nullable LFVideoFrame*)frame {
110
- if (self.uploading ) [self .socket sendFrame: frame];// <上传
109
+ - (void )videoEncoder : (nullable id <LFVideoEncoding>)encoder videoFrame : (nullable LFVideoFrame *)frame {
110
+ if (self.uploading ) [self .socket sendFrame: frame]; // <上传
111
111
}
112
112
113
113
#pragma mark -- LFStreamTcpSocketDelegate
114
- - (void )socketStatus : (nullable id <LFStreamSocket>)socket status : (LFLiveState)status {
115
- if (status == LFLiveStart){
116
- if (!self.uploading ){
114
+ - (void )socketStatus : (nullable id <LFStreamSocket>)socket status : (LFLiveState)status {
115
+ if (status == LFLiveStart) {
116
+ if (!self.uploading ) {
117
117
self.timestamp = 0 ;
118
118
self.isFirstFrame = YES ;
119
119
self.uploading = YES ;
120
120
}
121
121
}
122
122
dispatch_async (dispatch_get_main_queue (), ^{
123
123
self.state = status;
124
- if (self.delegate && [self .delegate respondsToSelector: @selector (liveSession:liveStateDidChange: )]){
124
+ if (self.delegate && [self .delegate respondsToSelector: @selector (liveSession:liveStateDidChange: )]) {
125
125
[self .delegate liveSession: self liveStateDidChange: status];
126
126
}
127
127
});
128
128
}
129
129
130
- - (void )socketDidError : (nullable id <LFStreamSocket>)socket errorCode : (LFLiveSocketErrorCode)errorCode {
130
+ - (void )socketDidError : (nullable id <LFStreamSocket>)socket errorCode : (LFLiveSocketErrorCode)errorCode {
131
131
dispatch_async (dispatch_get_main_queue (), ^{
132
- if (self.delegate && [self .delegate respondsToSelector: @selector (liveSession:errorCode: )]){
132
+ if (self.delegate && [self .delegate respondsToSelector: @selector (liveSession:errorCode: )]) {
133
133
[self .delegate liveSession: self errorCode: errorCode];
134
134
}
135
135
});
136
136
}
137
137
138
- - (void )socketDebug : (nullable id <LFStreamSocket>)socket debugInfo : (nullable LFLiveDebug*)debugInfo {
138
+ - (void )socketDebug : (nullable id <LFStreamSocket>)socket debugInfo : (nullable LFLiveDebug *)debugInfo {
139
139
self.debugInfo = debugInfo;
140
- if (self.showDebugInfo ){
140
+ if (self.showDebugInfo ) {
141
141
dispatch_async (dispatch_get_main_queue (), ^{
142
- if (self.delegate && [self .delegate respondsToSelector: @selector (liveSession:debugInfo: )]){
142
+ if (self.delegate && [self .delegate respondsToSelector: @selector (liveSession:debugInfo: )]) {
143
143
[self .delegate liveSession: self debugInfo: debugInfo];
144
144
}
145
145
});
146
146
}
147
147
}
148
148
149
- - (void )socketBufferStatus : (nullable id <LFStreamSocket>)socket status : (LFLiveBuffferState)status {
149
+ - (void )socketBufferStatus : (nullable id <LFStreamSocket>)socket status : (LFLiveBuffferState)status {
150
150
NSUInteger videoBitRate = [_videoEncoder videoBitRate ];
151
- if (status == LFLiveBuffferIncrease){
152
- if (videoBitRate < _videoConfiguration.videoMaxBitRate ){
151
+ if (status == LFLiveBuffferIncrease) {
152
+ if (videoBitRate < _videoConfiguration.videoMaxBitRate ) {
153
153
videoBitRate = videoBitRate + 50 * 1000 ;
154
154
[_videoEncoder setVideoBitRate: videoBitRate];
155
155
}
156
- }else {
157
- if (videoBitRate > _videoConfiguration.videoMinBitRate ){
156
+ } else {
157
+ if (videoBitRate > _videoConfiguration.videoMinBitRate ) {
158
158
videoBitRate = videoBitRate - 100 * 1000 ;
159
159
[_videoEncoder setVideoBitRate: videoBitRate];
160
160
}
161
161
}
162
162
}
163
163
164
164
#pragma mark -- Getter Setter
165
- - (void )setRunning : (BOOL )running {
166
- if (_running == running) return ;
165
+ - (void )setRunning : (BOOL )running {
166
+ if (_running == running) return ;
167
167
[self willChangeValueForKey: @" running" ];
168
168
_running = running;
169
169
[self didChangeValueForKey: @" running" ];
170
170
self.videoCaptureSource .running = _running;
171
171
self.audioCaptureSource .running = _running;
172
172
}
173
173
174
- - (void )setPreView : (UIView *)preView {
174
+ - (void )setPreView : (UIView *)preView {
175
175
[self .videoCaptureSource setPreView: preView];
176
176
}
177
177
178
- - (UIView*)preView {
178
+ - (UIView *)preView {
179
179
return self.videoCaptureSource .preView ;
180
180
}
181
181
182
- - (void )setCaptureDevicePosition : (AVCaptureDevicePosition)captureDevicePosition {
182
+ - (void )setCaptureDevicePosition : (AVCaptureDevicePosition)captureDevicePosition {
183
183
[self .videoCaptureSource setCaptureDevicePosition: captureDevicePosition];
184
184
}
185
185
186
- - (AVCaptureDevicePosition)captureDevicePosition {
186
+ - (AVCaptureDevicePosition)captureDevicePosition {
187
187
return self.videoCaptureSource .captureDevicePosition ;
188
188
}
189
189
190
- - (void )setBeautyFace : (BOOL )beautyFace {
190
+ - (void )setBeautyFace : (BOOL )beautyFace {
191
191
[self .videoCaptureSource setBeautyFace: beautyFace];
192
192
}
193
193
194
- - (BOOL )beautyFace {
194
+ - (BOOL )beautyFace {
195
195
return self.videoCaptureSource .beautyFace ;
196
196
}
197
197
@@ -235,65 +235,65 @@ - (BOOL)mirror {
235
235
return self.videoCaptureSource .mirror ;
236
236
}
237
237
238
- - (void )setMuted : (BOOL )muted {
238
+ - (void )setMuted : (BOOL )muted {
239
239
[self .audioCaptureSource setMuted: muted];
240
240
}
241
241
242
- - (BOOL )muted {
242
+ - (BOOL )muted {
243
243
return self.audioCaptureSource .muted ;
244
244
}
245
245
246
- - (LFAudioCapture*)audioCaptureSource {
247
- if (!_audioCaptureSource){
246
+ - (LFAudioCapture *)audioCaptureSource {
247
+ if (!_audioCaptureSource) {
248
248
_audioCaptureSource = [[LFAudioCapture alloc ] initWithAudioConfiguration: _audioConfiguration];
249
249
_audioCaptureSource.delegate = self;
250
250
}
251
251
return _audioCaptureSource;
252
252
}
253
253
254
- - (LFVideoCapture*)videoCaptureSource {
255
- if (!_videoCaptureSource){
254
+ - (LFVideoCapture *)videoCaptureSource {
255
+ if (!_videoCaptureSource) {
256
256
_videoCaptureSource = [[LFVideoCapture alloc ] initWithVideoConfiguration: _videoConfiguration];
257
257
_videoCaptureSource.delegate = self;
258
258
}
259
259
return _videoCaptureSource;
260
260
}
261
261
262
- - (id <LFAudioEncoding>)audioEncoder {
263
- if (!_audioEncoder){
262
+ - (id <LFAudioEncoding>)audioEncoder {
263
+ if (!_audioEncoder) {
264
264
_audioEncoder = [[LFHardwareAudioEncoder alloc ] initWithAudioStreamConfiguration: _audioConfiguration];
265
265
[_audioEncoder setDelegate: self ];
266
266
}
267
267
return _audioEncoder;
268
268
}
269
269
270
- - (id <LFVideoEncoding>)videoEncoder {
271
- if (!_videoEncoder){
270
+ - (id <LFVideoEncoding>)videoEncoder {
271
+ if (!_videoEncoder) {
272
272
_videoEncoder = [[LFHardwareVideoEncoder alloc ] initWithVideoStreamConfiguration: _videoConfiguration];
273
273
[_videoEncoder setDelegate: self ];
274
274
}
275
275
return _videoEncoder;
276
276
}
277
277
278
- - (id <LFStreamSocket>)socket {
279
- if (!_socket){
278
+ - (id <LFStreamSocket>)socket {
279
+ if (!_socket) {
280
280
_socket = [[LFStreamRtmpSocket alloc ] initWithStream: self .streamInfo videoSize: self .videoConfiguration.videoSize reconnectInterval: self .reconnectInterval reconnectCount: self .reconnectCount];
281
281
[_socket setDelegate: self ];
282
282
}
283
283
return _socket;
284
284
}
285
285
286
- - (LFLiveStreamInfo*)streamInfo {
287
- if (!_streamInfo){
286
+ - (LFLiveStreamInfo *)streamInfo {
287
+ if (!_streamInfo) {
288
288
_streamInfo = [[LFLiveStreamInfo alloc ] init ];
289
289
}
290
290
return _streamInfo;
291
291
}
292
292
293
- - (uint64_t )currentTimestamp {
293
+ - (uint64_t )currentTimestamp {
294
294
dispatch_semaphore_wait (_lock, DISPATCH_TIME_FOREVER);
295
295
uint64_t currentts = 0 ;
296
- if (_isFirstFrame == true ) {
296
+ if (_isFirstFrame == true ) {
297
297
_timestamp = NOW;
298
298
_isFirstFrame = false ;
299
299
currentts = 0 ;
0 commit comments