美文网首页
Mac搭建nginx+rtmp服务器及iOS推拉流测试

Mac搭建nginx+rtmp服务器及iOS推拉流测试

作者: Kean_Qi | 来源:发表于2016-08-30 12:49 被阅读1241次

经过多方面搜集资料,终于完成移动端直播基础功能,做下记录,以便以后能够使用

Nginx是一款轻量级Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东新浪网易腾讯淘宝等。

RTMP是Real Time Messaging Protocol(实时消息传输协议)的首字母缩写。该协议基于TCP,是一个协议族,包括RTMP基本协议及RTMPT/RTMPS/RTMPE等多种变种。RTMP是一种设计用来进行实时数据通信的网络协议,主要用来在Flash/AIR平台和支持RTMP协议的流媒体/交互服务器之间进行音视频和数据通信。支持该协议的软件包括Adobe Media Server/Ultrant Media Server/red5等。
nginx是非常优秀的开源服务器,用它来做hls或者rtmp流媒体服务器是非常不错的选择
一、安装Homebrow,已经安装了brow的可以直接跳过这一步。

执行安装命令:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
如果已经安装过,而想要卸载,执行卸载命令:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

二、安装nginx
先glone nginx项目到本地

$ brew tap homebrew/nginx

执行安装

$ brew install nginx-full --with-rtmp-module

经过漫长的等待,通过操作以上步骤nginx和rtmp模块就安装好了,下面开始来配置nginx的rtmp模块
首先来看看我们的nginx安装在哪里了

$ brew info nginx-full

执行上面的命令后我们可以看到信息

#nginx安装所在位置
/usr/local/etc/nginx/nginx.conf
#nginx配置文件所在位置
/usr/local/Cellar/nginx-full/1.10.1/bin/nginx
#nginx服务器根目录所在位置
/usr/local/var/www
C072CC08-2755-4F8C-A1A3-92A95A049EB4.png

三、运行nginx测试
执行命令 ,测试下是否能成功启动nginx服务

$ nginx

命令行如下图所示

在浏览器地址栏输入:http://localhost:8080 (直接点击)

2004362-ca10e04901ad2b45.png

代表nginx安装成功了

//如果终端上提示
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
//则表示8080 端口被占用了, 查看端口PID
$ lsof -i tcp:8080
//然后重新执行nginx
//nginx常用方法:重新加载配置文件
$ nginx -s reload
//重新加载日志:
 $ nginx -s reopen
// 停止 nginx
$ nginx -s stop
// 有序退出 nginx
$ nginx -s quit

四、配置ramp

现在我们来修改nginx.conf这个配置文件,配置rtmp
复制nginx配置文件所在位置

/usr/local/etc/nginx/nginx.conf

打开Finder Shift + command + G前往,用记事本工具打开nginx.conf

http {
    ……
}
#在http节点后面加上rtmp配置
rtmp {

  server {
      listen 1935;
      chunk_size 4000;

    #直播流配置
      application rtmplive {
          live on;
      #为 rtmp 引擎设置最大连接数。默认为 off
      max_connections 1024;


       }


      application hls{

          live on;
          hls on;
          hls_path /usr/local/var/www/hls;
          hls_fragment 1s;
      }
   }
}
说明:
rtmp是协议名称
server 说明内部中是服务器相关配置
listen 监听的端口号, rtmp协议的默认端口号是1935
application 访问的应用路径是 gzhm
live on; 开启实时
record off; 不记录数据

配置完成之后重新启动nginx

#关闭nginx
$ nginx -s stop
#再开启nginx
$ nginx
#重新开启
$ nginx -s reload

五、安装ffmepg工具

$ brew install ffmpeg

安装这个需要等一段时间等待吧 然后准备一个视频文件作为来推流,然后我们在安装一个支持rtmp协议的视频播放器,Mac下可以用VLC
ffmepg 安装完成后可以开始推流了

六、MAC下测试直播

ffmpeg -re -i 你的视频文件的绝对路径(如/Users/pactera/Desktop/Player/MPlayer/AvPlayerDemo/3.mp4)  -vcodec copy -f flv rtmp://localhost:1935/rtmplive/home
#这里rtmplive是上面的配置文件中,配置的应用的路径名称;后面的room可以随便写.

七、 验证视频
然后电脑上打开vlc这个播放器软件 点击File---->Open Network 在弹出来的框中选择Network然后输入URL:

rtmp://localhost:1935/rtmplive/home
2CE20A7A-F878-4847-8BEA-72F956499409.png

八、 可能会遇到的问题

#找不到C语言的编译器clang <font>错误信息:
checking for OS
+ Darwin 15.3.0 x86_64
checking for C compiler ... not found
./configure: error: C compiler clang is not found

解决方案:
需要去apple 官网下载命令行工具,安装即可. 注意命令行工具的版本需要和你的Xcode对应.

在手机端实现推拉流

一、LFLiveKit 推流
推流端可以选择很多GitHub上的开源项目替代LFLiveKit,比如上面所提到的VideoCore,和LiveVideoCoreSDK。商用的话可以选择各大厂商的SDK,网易直播云、七牛、腾讯、百度、新浪、其中金山直播云本人用过。使用直播云的好处就是能快速上线App,功能十分齐全,可以播放器和推流端,服务器一套下来,有专业客服人员帮助集成到工程中,缺点就是流量费太贵了,具体可以了解下各大厂商的收费标准。

//调用LF的API开始录制
- (UIButton*)startLiveButton{
    if(!_startLiveButton){
        
        _startLiveButton = [UIButton new];
        
        //位置
        _startLiveButton.frame = CGRectMake((XJScreenW - 200) * 0.5, XJScreenH - 100, 200, 40);
        
        _startLiveButton.layer.cornerRadius = _startLiveButton.frame.size.height * 0.5;
        [_startLiveButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_startLiveButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_startLiveButton setTitle:@"开始直播" forState:UIControlStateNormal];
        [_startLiveButton setBackgroundColor:[UIColor grayColor]];
        _startLiveButton.exclusiveTouch = YES;
        __weak typeof(self) _self = self;
        [_startLiveButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
            _self.startLiveButton.selected = !_self.startLiveButton.selected;
            if(_self.startLiveButton.selected){
                [_self.startLiveButton setTitle:@"结束直播" forState:UIControlStateNormal];
                LFLiveStreamInfo *stream = [LFLiveStreamInfo new];
                stream.url = @"rtmp://192.168.24.78:1935/rtmplive/home";
                [_self.session startLive:stream];
            }else{
                [_self.startLiveButton setTitle:@"开始直播" forState:UIControlStateNormal];
                [_self.session stopLive];
            }
        }];
    }
    return _startLiveButton;
}

@"rtmp://192.168.24.78:1935/rtmplive/home"即推流url
是把
@"rtmp://localhost:1935/rtmplive/home"
中的localhost改为自己电脑的IP地址,用自己电脑的网络测试

拉流断播放

播放端用的针对RTMP优化过的ijkplayer(),ijkplayer是基于FFmpeg的跨平台播放器,这个开源项目已经被多个 App 使用,其中映客、美拍和斗鱼使用了 ijkplayer
当然用kxmovie也是一个很不错的播放器
关于ffmpge编译方法请看链接FFmpeg编译及使用
拉留地址和推流地址一样

#IJKMediaFramework
//获取url
    self.url = [NSURL URLWithString:@"rtmp://192.168.24.78:1935/rtmplive/home"];
    _player = [[IJKFFMoviePlayerController alloc] initWithContentURL:self.url withOptions:nil];
    
    UIView *playerview = [self.player view];
    UIView *displayView = [[UIView alloc] initWithFrame:self.view.bounds];
    
    self.PlayerView = displayView;
    [self.view addSubview:self.PlayerView];
    
    // 自动调整自己的宽度和高度
    playerview.frame = self.PlayerView.bounds;
    playerview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    [self.PlayerView insertSubview:playerview atIndex:1];
    [_player setScalingMode:IJKMPMovieScalingModeAspectFill];
#KxMovie

    NSString *path = @"rtmp://192.168.24.78:1935/rtmplive/home";
    
//    NSString *path=[[NSBundle mainBundle] pathForResource:@"2" ofType:@"mp4"];

    NSMutableDictionary *parametes = [NSMutableDictionary dictionary];
    if ([path.pathExtension isEqualToString:@"wmv"])
        //电影参数最小缓冲时间
        parametes[KxMovieParameterMinBufferedDuration]=@(5.0);
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        //电影参数禁用反交错
        parametes[KxMovieParameterDisableDeinterlacing] = @(YES);
    
    KxMovieViewController *vc = [KxMovieViewController movieViewControllerWithContentPath:path parameters:parametes];
//    [self presentViewController:vc animated:YES completion:nil];
    [self.navigationController pushViewController:vc animated:YES];

至此直播整个流程就测试完毕

友情链接:
LFLiveKit:框架支持RTMP,由Adobe公司开发。github地址
FFmpeg编译及使用
Mac上搭建直播服务器Nginx
转 映客 LFLiveKit 推流
打包好的IJKPlayer
Mac搭建nginx+rtmp服务器
FFmpeg常用推流命令

相关文章

网友评论

      本文标题:Mac搭建nginx+rtmp服务器及iOS推拉流测试

      本文链接:https://www.haomeiwen.com/subject/ztpfettx.html