相关库
LFLiveKit + RTMP实现直播推流
RTMP协议:RTMP(the Real-time Messaging Protocol)协议作为客户端和服务器端的传输协议,这是一个专门为高效传输视频、音频和数据而设计的 TCP/IP 协议。
HLS协议: HTTP Live Streaming(HLS)是苹果公司(Apple Inc.)实现的基于HTTP的流媒体传输协议。
1、利用cocoapods加载LFLiveKit
source 'https://github.com/CocoaPods/Specs.git'
target ‘Live_LFLiveKit_RTMP’ do
    pod 'LFLiveKit'
end
2、创建直播会话
// 创建直播会话
@property (nonatomic, strong) LFLiveSession *liveSession;
相关属性的设置,LFLiveKit已经包含了GPUImage,可以进行美颜相关操作
- (LFLiveSession*)liveSession {
    if (!_liveSession) {
        _liveSession = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfiguration]];
        // 预览视图
        _liveSession.preView = self.view;
        // 设置为后置摄像头
        _liveSession.captureDevicePosition = AVCaptureDevicePositionBack;
        // 开启美颜
        _liveSession.beautyFace = YES;
        _liveSession.delegate = self;
    }
    return _liveSession;
}
3、Nginx服务器配置
- 安装nginx:
 在终端执行
brew install nginx-full --with-rtmp-module 
如果报错的话,要先执行参考链接:
brew tap denji/nginx
- 配置Nginx,支持http协议拉流:
 在终端执行
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协议推流:
 在终端执行
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支持结束
    }
}

屏幕快照 2019-02-16 下午7.07.16.png
- 启动Nginx
 在终端执行
nginx -s reload
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
4、开始直播
rtmp协议
// 开始直播
- (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、结束直播
// 结束直播
- (void)stopLive {
    [self.liveSession stopLive];
}
采集图像格式、声音格式:
摄像头采集图像并用VideoToolbox编码成H.264码流
麦克风采集声音并用AudioToolbox编码成AAC码流
😊附上demo,如果帮到你了请给个小星星🌟
GitHub