相關(guān)庫(kù)
LFLiveKit + RTMP實(shí)現(xiàn)直播推流
RTMP協(xié)議:RTMP(the Real-time Messaging Protocol)協(xié)議作為客戶(hù)端和服務(wù)器端的傳輸協(xié)議,這是一個(gè)專(zhuān)門(mén)為高效傳輸視頻围段、音頻和數(shù)據(jù)而設(shè)計(jì)的 TCP/IP 協(xié)議溉浙。
HLS協(xié)議: HTTP Live Streaming(HLS)是蘋(píng)果公司(Apple Inc.)實(shí)現(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)建直播會(huì)話
// 創(chuàng)建直播會(huì)話
@property (nonatomic, strong) LFLiveSession *liveSession;
相關(guān)屬性的設(shè)置,LFLiveKit已經(jīng)包含了GPUImage痹仙,可以進(jìn)行美顏相關(guān)操作
- (LFLiveSession*)liveSession {
if (!_liveSession) {
_liveSession = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfiguration]];
// 預(yù)覽視圖
_liveSession.preView = self.view;
// 設(shè)置為后置攝像頭
_liveSession.captureDevicePosition = AVCaptureDevicePositionBack;
// 開(kāi)啟美顏
_liveSession.beautyFace = YES;
_liveSession.delegate = self;
}
return _liveSession;
}
3是尔、Nginx服務(wù)器配置
- 安裝nginx:
在終端執(zhí)行
brew install nginx-full --with-rtmp-module
如果報(bào)錯(cuò)的話,要先執(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;
}
#增加對(duì)HLS支持開(kāi)始
application hls {
live on;
hls on;
hls_path /usr/local/var/www/hls;
hls_fragment 5s;
}
#增加對(duì)HLS支持結(jié)束
}
}
屏幕快照 2019-02-16 下午7.07.16.png
- 啟動(dòng)Nginx
在終端執(zhí)行
nginx -s reload
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
4、開(kāi)始直播
rtmp協(xié)議
// 開(kāi)始直播
- (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田轧,如果幫到你了請(qǐng)給個(gè)小星星??
GitHub