天天看点

iOS直播-基于RTMP的视频推送GDLiveStreaming简介

http://www.jianshu.com/p/83da490c0f95

所谓的视频推送就是把摄像头和麦克风捕获到视频和音频推送到直播服务器上.我们这里使用推送协议是RTMP协议. 扩展:腾讯直播平台,阿里直播平台,百度直播平台提供均为RTMP的推流和HLS/RTMP等拉流.

我们使用GDLiveStreaming来实现iOS的端的视频推送

GDLiveStreaming简介

GDLiveStreaming是对开源框架

VideoCore

简单封装.提供视频录制,推送与存储. 你可以下载GDLiveStreaming源码来学习和研究. 我们这里讲解如何通过

GDLiveStreaming

实现

RTMP的推流

集成GDLiveStreaming步骤

  1. 创建项目
  2. 打开终端

    cd

    到项目目录
  3. 执行

    pod init

    生成

    Profile

    文件
  4. target 你的项目名称 do

    end

    直接添加
    pod 'GDLiveStreaming', :git => 'https://github.com/goodow/GDLiveStreaming.git'
    pod 'VideoCore', :git => 'https://github.com/goodow/VideoCore.git'
    pod 'glm', :podspec => 'https://raw.githubusercontent.com/goodow/GDLiveStreaming/master/glm.podspec'
               
  5. 执行

    pod install

    命名,等待其安装完毕,打开白底的

    你的名称.xcworkspace

    文件. 到此

    GDLiveStreaming

    安装完毕.

GDLiveStreaming基本使用

  1. 导入如下头文件
    #import <GPUImage/GPUImageVideoCamera.h>
    #import <GPUImage/GPUImageView.h>
    #import <GDLiveStreaming/GDLRawDataOutput.h>
               
  2. 实现思路
    • 创建视频摄像头
    • 设置摄像头帧率
    • 设置摄像头输出图片的方向
    • 创建用于展示视频的GPUImageView
    • 添加GPUImageView为摄像头的的输出目标
    • 创建

      GDLRawDataOutput

      对象
    • 添加数据输出对象为摄像头输出目标
    • 开始捕获视频
    • 开始上传视频
  3. 代码实现
//  1. 创建视频摄像头
    self.camera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720
cameraPosition:AVCaptureDevicePositionBack];
//  2. 设置摄像头帧率
    self.camera.frameRate = ;
//  3. 设置摄像头输出视频的方向
    self.camera.outputImageOrientation = UIInterfaceOrientationPortrait;
//  4.0 创建用于展示视频的GPUImageView
    GPUImageView *imageView = [[GPUImageView alloc] init];
    imageView.frame = self.view.bounds;
    [self.view addSubview:imageView];
//  4.1 添加GPUImageView为摄像头的的输出目标
    [self.camera addTarget:imageView];
//  5. 创建原始数据输出对象

    GDLRawDataOutput *output = [[GDLRawDataOutput alloc] initWithVideoCamera:self.camera withImageSize:CGSizeMake(, )];

//  6. 添加数据输出对象为摄像头输出目标
    [self.camera addTarget:output];

//  7.开始捕获视频
    [self.camera startCameraCapture];

//  8.开始上传视频
    [output startUploadStreamWithURL:@"rtmp://192.168.41.35:1935/gzhm" andStre  amKey:@"room"];
           

GDLiveStreaming扩展

  • GPUImageVideoCamera常见用方法
/** 开始捕获
 */
- (void)startCameraCapture;
/** 停止捕获
 */
- (void)stopCameraCapture;
/**暂停捕获
 */
- (void)pauseCameraCapture;
/**恢复捕获
 */
- (void)resumeCameraCapture;
///前置摄像头与后置摄像头切换
- (void)rotateCamera
           
  • GDLRawDataOutput常见用方法
///开始上传视频流, streamKey是拼接在rtmpUrl后面的路径名称,可以理解为视频的直播中的房间编号.
- (void)startUploadStreamWithURL:(NSString *)rtmpUrl andStreamKey:(NSString *)streamKey;
///停止上传视频流
- (void)stopUploadStream;
           

文/满山李子(简书作者)

原文链接:http://www.jianshu.com/p/83da490c0f95

著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。