iOS直播技术学习笔记 iOS中实现推流(八)

1,230 阅读2分钟

推流演示

搭建本地服务器

安装Nginx

  • Nginx介绍
    • Nginx是什么?

Nginx.png
* 简言之,Nginx本身是一个非常出色的HTTP服务器,具有占用内存少,高并发的特点。

  • Nginx安装
// 1> 将Nginx Clone到本地
$ brew tap homebrew/nginx
// 2> 链接要执行的命令
$ brew link pcre rtmp-nginx-module
// 3> 安装Nginx
$ brew install nginx-full --with-rtmp-module

  • 记住安装配置文件的路径(/usr/local/etc/nginx/nginx.conf)

  • 启动即可:

  • 配置Nginx,支持http协议拉流

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;
    }

  • 配置Nginx,支持rtmp协议推流
rtmp {
    server {
        listen 1935;
        application rtmplive {
            live on;
            max_connections 1024;
        }
        application hls{
            live on;
            hls on;
            hls_path /usr/local/var/www/hls;
            hls_fragment 1s;
        }
    }
}

  • 重启Nginx: nginx -s reload

推流测试

  • 推流至RTMP到服务器
    • 生成地址: rtmp://localhost:1935/rtmplive/demo
ffmpeg -re -i story.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://localhost:1935/rtmplive/demo

ffmpeg -re -i /Users/apple/Desktop/ffmepg/HLS切片/说出你的励志故事.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://localhost:1935/hls/demo

远程服务器配置

0> 安装git
1> git clone srs
2> cd sis目录
* git checkout 2.0release
* git pull
3> 配置远程服务器环境
* ./configure --disable-all --with-ssl --with-nginx --with-hls --with-http-callback --with-http-server --with-http-api --with-ffmpeg --with-transcode --with-librtmp --with-dvr && make
4> 启动配置
* ./objcs/srs -c conf/srs.conf
* 查看pid: pgrep
5> 关闭进程
* kill nginx/killall nginx
6> 推流可以播放hls/rtmp
* 加载自己配置的conf文件srs/trunk/conf

iOS中实现推流

推流概述

  • 经过前面的讲解&分析, 我们已经可以做到采集-美颜滤镜-视频编码-协议推流
  • 那么下面就需要真正实现iOS中的推流
  • 采用RTMP进行推流, 因为该协议实时性非常的高, 但是将Message封装成Trunk的过程非常麻烦, 所有我们这里直接采用框架实现推流.
  • 常见的推流框架
    • Swift框架: lf.swift
    • OC框架: LFLiveKit

推流框架的使用

  • LFLiveKit是一个集成了视频采集-美颜-编码-推流为一体的框架,并且使用起来非常的简单, 我们可以在iOS中直接使用该框架进行推流
  • 使用方式