相關(guān)庫
LFLiveKit + RTMP實現(xiàn)直播推流
RTMP協(xié)議:RTMP(the Real-time Messaging Protocol)協(xié)議作為客戶端和服務(wù)器端的傳輸協(xié)議,這是一個專門為高效傳輸視頻、音頻和數(shù)據(jù)而設(shè)計的 TCP/IP 協(xié)議。
HLS協(xié)議: HTTP Live Streaming(HLS)是蘋果公司(Apple Inc.)實現(xiàn)的基于HTTP的流媒體傳輸協(xié)議。
1、利用cocoapods加載LFLiveKit
source 'https://github.com/CocoaPods/Specs.git'
target ‘Live_LFLiveKit_RTMP’ do
pod 'LFLiveKit'
end
2、創(chuàng)建直播會話
// 創(chuàng)建直播會話
@property (nonatomic, strong) LFLiveSession *liveSession;
相關(guān)屬性的設(shè)置,LFLiveKit已經(jīng)包含了GPUImage,可以進行美顏相關(guān)操作
- (LFLiveSession*)liveSession {
if (!_liveSession) {
_liveSession = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfiguration]];
// 預(yù)覽視圖
_liveSession.preView = self.view;
// 設(shè)置為后置攝像頭
_liveSession.captureDevicePosition = AVCaptureDevicePositionBack;
// 開啟美顏
_liveSession.beautyFace = YES;
_liveSession.delegate = self;
}
return _liveSession;
}
3、Nginx服務(wù)器配置
- 安裝nginx:
在終端執(zhí)行
brew install nginx-full --with-rtmp-module
如果報錯的話,要先執(zhí)行參考鏈接:
brew tap denji/nginx
- 配置Nginx,支持http協(xié)議拉流:
在終端執(zhí)行
open -t /usr/local/etc/nginx/nginx.conf
添加配置信息
location /hls {
#Serve HLS config
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /usr/local/var/www;
add_header Cache-Control no-cache;
}

屏幕快照 2019-02-16 下午5.29.47.png
- 配置Nginx,支持rtmp協(xié)議推流:
在終端執(zhí)行
open -t /usr/local/etc/nginx/nginx.conf
添加配置信息
rtmp {
server {
listen 1935;
ping 30s;
notify_method get;
application myapp {
live on;
record off;
max_connections 1024;
}
#增加對HLS支持開始
application hls {
live on;
hls on;
hls_path /usr/local/var/www/hls;
hls_fragment 5s;
}
#增加對HLS支持結(jié)束
}
}

屏幕快照 2019-02-16 下午7.07.16.png
- 啟動Nginx
在終端執(zhí)行
nginx -s reload
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
4、開始直播
rtmp協(xié)議
// 開始直播
- (void)startLive {
LFLiveStreamInfo *streamInfo = [LFLiveStreamInfo new];
streamInfo.url = @"rtmp://172.16.237.61:1935/hls/live1";
[self.liveSession startLive:streamInfo];
}

WechatIMG132.jpeg
5、拉流
在瀏覽器輸入:http://localhost:8080/hls/live1.m3u8

屏幕快照 2019-02-16 下午7.04.06.png
6、結(jié)束直播
// 結(jié)束直播
- (void)stopLive {
[self.liveSession stopLive];
}
采集圖像格式、聲音格式:
攝像頭采集圖像并用VideoToolbox編碼成H.264碼流
麥克風(fēng)采集聲音并用AudioToolbox編碼成AAC碼流
??附上demo,如果幫到你了請給個小星星??
GitHub