LFLiveKit:框架支持RTMP,由Adobe公司开发。github地址https://github.com/LaiFengiOS/LFLiveKit

LFLiveKit库里已经集成GPUImage框架用于美颜功能,GPUImage基于OpenGl开发,纯OC语言框架,封装好了各种滤镜同时也可以编写自定义的滤镜,其本身内置了多达125种常见的滤镜效果。

LFLiveKit库通过pod导入项目

1
pod 'LFLiveKit'

配置上传地址

1
2
3
let stream = LFLiveStreamInfo()
stream.url = "rtmp://192.168.***.***:1935/rtmplive/room"
session.startLive(stream)

demo

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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//
// ViewController.swift
// IfeiyvLiveVideo
//
// Created by l on 2019/7/1.
// Copyright © 2019 ifeiyv. All rights reserved.
//

import UIKit

class ViewController: UIViewController, LFLiveSessionDelegate {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

session.delegate = self
session.preView = self.view

self.requestAccessForVideo()
self.requestAccessForAudio()
self.view.backgroundColor = UIColor.clear
self.view.addSubview(containerView)
containerView.addSubview(stateLabel)
containerView.addSubview(closeButton)
containerView.addSubview(beautyButton)
containerView.addSubview(cameraButton)
containerView.addSubview(startLiveButton)

cameraButton.addTarget(self, action: #selector(didTappedCameraButton(_:)), for:.touchUpInside)
beautyButton.addTarget(self, action: #selector(didTappedBeautyButton(_:)), for: .touchUpInside)
startLiveButton.addTarget(self, action: #selector(didTappedStartLiveButton(_:)), for: .touchUpInside)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

//MARK: AccessAuth

func requestAccessForVideo() -> Void {
let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video);
switch status {
// 许可对话没有出现,发起授权许可
case AVAuthorizationStatus.notDetermined:
AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (granted) in
if(granted){
DispatchQueue.main.async {
self.session.running = true
}
}
})
break;
// 已经开启授权,可继续
case AVAuthorizationStatus.authorized:
session.running = true;
break;
// 用户明确地拒绝授权,或者相机设备无法访问
case AVAuthorizationStatus.denied: break
case AVAuthorizationStatus.restricted:break;
default:
break;
}
}

func requestAccessForAudio() -> Void {
let status = AVCaptureDevice.authorizationStatus(for:AVMediaType.audio)
switch status {
// 许可对话没有出现,发起授权许可
case AVAuthorizationStatus.notDetermined:
AVCaptureDevice.requestAccess(for: AVMediaType.audio, completionHandler: { (granted) in

})
break;
// 已经开启授权,可继续
case AVAuthorizationStatus.authorized:
break;
// 用户明确地拒绝授权,或者相机设备无法访问
case AVAuthorizationStatus.denied: break
case AVAuthorizationStatus.restricted:break;
default:
break;
}
}

//MARK: - Callbacks

// 回调
func liveSession(_ session: LFLiveSession?, debugInfo: LFLiveDebug?) {
print("debugInfo: \(debugInfo?.currentBandwidth)")
}

func liveSession(_ session: LFLiveSession?, errorCode: LFLiveSocketErrorCode) {
print("errorCode: \(errorCode.rawValue)")
}

func liveSession(_ session: LFLiveSession?, liveStateDidChange state: LFLiveState) {
print("liveStateDidChange: \(state.rawValue)")
switch state {
case LFLiveState.ready:
stateLabel.text = "未连接"
break;
case LFLiveState.pending:
stateLabel.text = "连接中"
break;
case LFLiveState.start:
stateLabel.text = "已连接"
break;
case LFLiveState.error:
stateLabel.text = "连接错误"
break;
case LFLiveState.stop:
stateLabel.text = "未连接"
break;
default:
break;
}
}

//MARK: - Events

// 开始直播
@objc func didTappedStartLiveButton(_ button: UIButton) -> Void {
startLiveButton.isSelected = !startLiveButton.isSelected;
if (startLiveButton.isSelected) {
startLiveButton.setTitle("结束直播", for: UIControl.State())
let stream = LFLiveStreamInfo()
stream.url = "rtmp://192.168.1.148:1935/rtmplive/room"
session.startLive(stream)
} else {
startLiveButton.setTitle("开始直播", for: UIControl.State())
session.stopLive()
}
}

// 美颜
@objc func didTappedBeautyButton(_ button: UIButton) -> Void {
session.beautyFace = !session.beautyFace;
beautyButton.isSelected = !session.beautyFace
}

// 摄像头
@objc func didTappedCameraButton(_ button: UIButton) -> Void {
let devicePositon = session.captureDevicePosition;
session.captureDevicePosition = (devicePositon == AVCaptureDevice.Position.back) ? AVCaptureDevice.Position.front : AVCaptureDevice.Position.back;
}

// 关闭
func didTappedCloseButton(_ button: UIButton) -> Void {

}

//MARK: - Getters and Setters

//  默认分辨率368 * 640 音频:44.1 iphone6以上48 双声道 方向竖屏
var session: LFLiveSession = {
let audioConfiguration = LFLiveAudioConfiguration.defaultConfiguration(for: LFLiveAudioQuality.high)
let videoConfiguration = LFLiveVideoConfiguration.defaultConfiguration(for: LFLiveVideoQuality.low3)
let session = LFLiveSession(audioConfiguration: audioConfiguration, videoConfiguration: videoConfiguration)
return session!
}()

// 视图
var containerView: UIView = {
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
containerView.backgroundColor = UIColor.clear
containerView.autoresizingMask = [UIView.AutoresizingMask.flexibleHeight, UIView.AutoresizingMask.flexibleHeight]
return containerView
}()

// 状态Label
var stateLabel: UILabel = {
let stateLabel = UILabel(frame: CGRect(x: 20, y: 20, width: 80, height: 40))
stateLabel.text = "未连接"
stateLabel.textColor = UIColor.white
stateLabel.font = UIFont.systemFont(ofSize: 14)
return stateLabel
}()

// 关闭按钮
var closeButton: UIButton = {
let closeButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.width - 10 - 44, y: 20, width: 44, height: 44))
closeButton.setImage(UIImage(named: "close_preview"), for: UIControl.State())
return closeButton
}()

// 摄像头
var cameraButton: UIButton = {
let cameraButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.width - 54 * 2, y: 20, width: 44, height: 44))
cameraButton.setImage(UIImage(named: "camra_preview"), for: UIControl.State())
return cameraButton
}()

// 摄像头
var beautyButton: UIButton = {
let beautyButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.width - 54 * 3, y: 20, width: 44, height: 44))
beautyButton.setImage(UIImage(named: "camra_beauty"), for: UIControl.State.selected)
beautyButton.setImage(UIImage(named: "camra_beauty_close"), for: UIControl.State())
return beautyButton
}()

// 开始直播按钮
var startLiveButton: UIButton = {
let startLiveButton = UIButton(frame: CGRect(x: 30, y: UIScreen.main.bounds.height - 50, width: UIScreen.main.bounds.width - 10 - 44, height: 44))
startLiveButton.layer.cornerRadius = 22
startLiveButton.setTitleColor(UIColor.black, for:UIControl.State())
startLiveButton.setTitle("开始直播", for: UIControl.State())
startLiveButton.titleLabel!.font = UIFont.systemFont(ofSize: 14)
startLiveButton.backgroundColor = UIColor(red: 50/255.0, green: 32/255.0, blue: 245/255.0, alpha: 1)//colorLiteralRed: 50, green: 32, blue: 245, alpha: 1
return startLiveButton
}()
}
//
// ViewController.swift
// IfeiyvLiveVideo
//
// Created by l on 2019/7/1.
// Copyright © 2019 ifeiyv. All rights reserved.
//

import UIKit

class ViewController: UIViewController, LFLiveSessionDelegate {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

session.delegate = self
session.preView = self.view

self.requestAccessForVideo()
self.requestAccessForAudio()
self.view.backgroundColor = UIColor.clear
self.view.addSubview(containerView)
containerView.addSubview(stateLabel)
containerView.addSubview(closeButton)
containerView.addSubview(beautyButton)
containerView.addSubview(cameraButton)
containerView.addSubview(startLiveButton)

cameraButton.addTarget(self, action: #selector(didTappedCameraButton(_:)), for:.touchUpInside)
beautyButton.addTarget(self, action: #selector(didTappedBeautyButton(_:)), for: .touchUpInside)
startLiveButton.addTarget(self, action: #selector(didTappedStartLiveButton(_:)), for: .touchUpInside)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

//MARK: AccessAuth

func requestAccessForVideo() -> Void {
let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video);
switch status {
// 许可对话没有出现,发起授权许可
case AVAuthorizationStatus.notDetermined:
AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (granted) in
if(granted){
DispatchQueue.main.async {
self.session.running = true
}
}
})
break;
// 已经开启授权,可继续
case AVAuthorizationStatus.authorized:
session.running = true;
break;
// 用户明确地拒绝授权,或者相机设备无法访问
case AVAuthorizationStatus.denied: break
case AVAuthorizationStatus.restricted:break;
default:
break;
}
}

func requestAccessForAudio() -> Void {
let status = AVCaptureDevice.authorizationStatus(for:AVMediaType.audio)
switch status {
// 许可对话没有出现,发起授权许可
case AVAuthorizationStatus.notDetermined:
AVCaptureDevice.requestAccess(for: AVMediaType.audio, completionHandler: { (granted) in

})
break;
// 已经开启授权,可继续
case AVAuthorizationStatus.authorized:
break;
// 用户明确地拒绝授权,或者相机设备无法访问
case AVAuthorizationStatus.denied: break
case AVAuthorizationStatus.restricted:break;
default:
break;
}
}

//MARK: - Callbacks

// 回调
func liveSession(_ session: LFLiveSession?, debugInfo: LFLiveDebug?) {
print("debugInfo: \(debugInfo?.currentBandwidth)")
}

func liveSession(_ session: LFLiveSession?, errorCode: LFLiveSocketErrorCode) {
print("errorCode: \(errorCode.rawValue)")
}

func liveSession(_ session: LFLiveSession?, liveStateDidChange state: LFLiveState) {
print("liveStateDidChange: \(state.rawValue)")
switch state {
case LFLiveState.ready:
stateLabel.text = "未连接"
break;
case LFLiveState.pending:
stateLabel.text = "连接中"
break;
case LFLiveState.start:
stateLabel.text = "已连接"
break;
case LFLiveState.error:
stateLabel.text = "连接错误"
break;
case LFLiveState.stop:
stateLabel.text = "未连接"
break;
default:
break;
}
}

//MARK: - Events

// 开始直播
@objc func didTappedStartLiveButton(_ button: UIButton) -> Void {
startLiveButton.isSelected = !startLiveButton.isSelected;
if (startLiveButton.isSelected) {
startLiveButton.setTitle("结束直播", for: UIControl.State())
let stream = LFLiveStreamInfo()
stream.url = "rtmp://192.168.1.148:1935/rtmplive/room"
session.startLive(stream)
} else {
startLiveButton.setTitle("开始直播", for: UIControl.State())
session.stopLive()
}
}

// 美颜
@objc func didTappedBeautyButton(_ button: UIButton) -> Void {
session.beautyFace = !session.beautyFace;
beautyButton.isSelected = !session.beautyFace
}

// 摄像头
@objc func didTappedCameraButton(_ button: UIButton) -> Void {
let devicePositon = session.captureDevicePosition;
session.captureDevicePosition = (devicePositon == AVCaptureDevice.Position.back) ? AVCaptureDevice.Position.front : AVCaptureDevice.Position.back;
}

// 关闭
func didTappedCloseButton(_ button: UIButton) -> Void {

}

//MARK: - Getters and Setters

//  默认分辨率368 * 640 音频:44.1 iphone6以上48 双声道 方向竖屏
var session: LFLiveSession = {
let audioConfiguration = LFLiveAudioConfiguration.defaultConfiguration(for: LFLiveAudioQuality.high)
let videoConfiguration = LFLiveVideoConfiguration.defaultConfiguration(for: LFLiveVideoQuality.low3)
let session = LFLiveSession(audioConfiguration: audioConfiguration, videoConfiguration: videoConfiguration)
return session!
}()

// 视图
var containerView: UIView = {
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
containerView.backgroundColor = UIColor.clear
containerView.autoresizingMask = [UIView.AutoresizingMask.flexibleHeight, UIView.AutoresizingMask.flexibleHeight]
return containerView
}()

// 状态Label
var stateLabel: UILabel = {
let stateLabel = UILabel(frame: CGRect(x: 20, y: 20, width: 80, height: 40))
stateLabel.text = "未连接"
stateLabel.textColor = UIColor.white
stateLabel.font = UIFont.systemFont(ofSize: 14)
return stateLabel
}()

// 关闭按钮
var closeButton: UIButton = {
let closeButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.width - 10 - 44, y: 20, width: 44, height: 44))
closeButton.setImage(UIImage(named: "close_preview"), for: UIControl.State())
return closeButton
}()

// 摄像头
var cameraButton: UIButton = {
let cameraButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.width - 54 * 2, y: 20, width: 44, height: 44))
cameraButton.setImage(UIImage(named: "camra_preview"), for: UIControl.State())
return cameraButton
}()

// 摄像头
var beautyButton: UIButton = {
let beautyButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.width - 54 * 3, y: 20, width: 44, height: 44))
beautyButton.setImage(UIImage(named: "camra_beauty"), for: UIControl.State.selected)
beautyButton.setImage(UIImage(named: "camra_beauty_close"), for: UIControl.State())
return beautyButton
}()

// 开始直播按钮
var startLiveButton: UIButton = {
let startLiveButton = UIButton(frame: CGRect(x: 30, y: UIScreen.main.bounds.height - 50, width: UIScreen.main.bounds.width - 10 - 44, height: 44))
startLiveButton.layer.cornerRadius = 22
startLiveButton.setTitleColor(UIColor.black, for:UIControl.State())
startLiveButton.setTitle("开始直播", for: UIControl.State())
startLiveButton.titleLabel!.font = UIFont.systemFont(ofSize: 14)
startLiveButton.backgroundColor = UIColor(red: 50/255.0, green: 32/255.0, blue: 245/255.0, alpha: 1)//colorLiteralRed: 50, green: 32, blue: 245, alpha: 1
return startLiveButton
}()
}