LFLiveKit的简单用法

LFLiveKIt Github地址

https://github.com/LaiFengiOS/LFLiveKit

该框架主要支持RTMP(Real Time Messaging Protocol)协议,该协议也是LFLiveKit实现视频推流的主要方式:Adobe公司的实时消息传输协议

HlS(HTTP Live Streaming):苹果自家的动态码率自适应技术。主要用于PC和Apple终端的音视频服务。包括一个m3u(8)的索引文件,TS媒体分片文件和key加密串文件。

该框架主要是由OC写的,剩下的是C写的,层次清晰。其扩展性非常强,并支持动态切换码率功能,支持美颜功能。

使用要求

  • iOS 7.0+
  • Xcode 7.3+

可实现的功能

1
2
3
4
5
6
7
8
9
10
11
12
13
Background recording
Support horizontal vertical recording
GPUImage Beauty
H264 Hard coding
AAC Hard coding
Weak network lost frame
Dynamic switching rate
Audio configuration
Video configuration
RTMP Transport
Switch camera
Audio Mute
Support Send Buffer

具体实现

使用时需要添加一个协议

<LFLiveSessionDelegate>

使用的RTMP地址:

1
2
3
@property (nonatomic, copy) NSString *rtmpurl;
@property (nonatomic, strong) LFLiveSession *session;
@property (nonatomic, weak) UIView *livingPreView;

主要方法:

_session = [[LFLiveSession alloc]initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration: [LFLiveVideoConfiguration defaultConfiguration]];

当然也可以自定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//默认分辨率368 * 640  音频:44.1 iphone6以上48  双声道  方向竖屏      
_session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfigurationForQuality:LFLiveVideoQuality_Medium2]];

//或者

//自己定制高质量音频128K 分辨率设置为720*1280 方向竖屏

LFLiveAudioConfiguration *audioConfiguration = [LFLiveAudioConfiguration new];
audioConfiguration.numberOfChannels = 2;
audioConfiguration.audioBitrate = LFLiveAudioBitRate_128Kbps;
audioConfiguration.audioSampleRate = LFLiveAudioSampleRate_44100Hz;

LFLiveVideoConfiguration *videoConfiguration = [LFLiveVideoConfiguration new];
videoConfiguration.videoSize = CGSizeMake(720, 1280);
videoConfiguration.videoBitRate = 800*1024;
videoConfiguration.videoMaxBitRate = 1000*1024;
videoConfiguration.videoMinBitRate = 500*1024;
videoConfiguration.videoFrameRate = 15;
videoConfiguration.videoMaxKeyframeInterval = 30;
videoConfiguration.orientation = UIInterfaceOrientationPortrait;
videoConfiguration.sessionPreset = LFCaptureSessionPreset720x1280;

_session = [[LFLiveSession alloc] initWithAudioConfiguration:audioConfiguration videoConfiguration:videoConfiguration liveType:LFLiveRTMP];

1
2
3
4
// 设置代理
_session.delegate = self;
_session.running = YES;
_session.preView = self.livingPreView;

开始推流:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (IBAction)living:(UIButton *)sender {
sender.selected = !sender.selected;
if (sender.selected) {
//开始直播
LFLiveStreamInfo *stream = [LFLiveStreamInfo new];
//服务器的地址
stream.url = @"rtmp://192.168.1.104/rtmplive/room";
self.rtmpurl = stream.url;
[self.session startLive:stream];
}else {
//结束直播
[self.session stopLive];
self.statusLabel.text = [NSString stringWithFormat:@"状态:播放关闭\nRTMP:%@", self.rtmpurl];
}
}

推流状态的回调:

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
//live status changed will callback
- (void)liveSession:(LFLiveSession *)session liveStateDidChange:(LFLiveState)state {

NSString *tempStatus;
switch (state) {
case LFLiveStop:
tempStatus = @"已断开";
break;
case LFLiveError:
tempStatus = @"链接出错";
break;
case LFLiveReady:
tempStatus = @"准备中";
break;
case LFLiveStart:
tempStatus = @"已连接";
break;
case LFLivePending:
tempStatus = @"连接中";
break;
default:
break;
}

self.statusLabel.text = [NSString stringWithFormat:@"状态:%@\nRTMP:%@", tempStatus,self.rtmpurl];

}

//live debug info callback
- (void)liveSession:(LFLiveSession *)session debugInfo:(LFLiveDebug *)debugInfo {
NSLog(@"debugInfo:%@", debugInfo);
}

//callback socked errorcodes
- (void)liveSession:(LFLiveSession *)session errorCode:(LFLiveSocketErrorCode)errorCode {
NSLog(@"errorCode:%lu", (unsigned long)errorCode);
}

一些其他功能的实现:

1
2
3
4
5
6
7
8
//关闭直播
- (IBAction)clock:(UIButton *)sender {
//连接中或已连接状态就停止播放
if (self.session.state == LFLivePending || self.session.state == LFLiveStart) {
[self.session stopLive];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
1
2
3
4
5
6
7
//开启/关闭美颜按钮
- (IBAction)beautiful:(UIButton *)sender {
sender.selected = !sender.selected;

//默认开启
self.session.beautyFace = !self.session.beautyFace;
}
1
2
3
4
5
6
7
//前后摄像头选择
- (IBAction)SwitchCamera:(UIButton *)sender {
//切换前置/后置摄像头
AVCaptureDevicePosition devicePositon = self.session.captureDevicePosition;
self.session.captureDevicePosition = (devicePositon == AVCaptureDevicePositionBack)? AVCaptureDevicePositionFront :
AVCaptureDevicePositionBack;
}