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

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

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

GDLiveStreaming简介

GDLiveStreaming是对开源框架VideoCore简单封装.提供视频录制,推送与存储. 你可以下载GDLiveStreaming源码来学习和研究. 我们这里讲解如何通过GDLiveStreaming实现RTMP的推流

集成GDLiveStreaming步骤

  1. 创建项目
  2. 打开终端cd到项目目录
  3. 执行pod init 生成 Profile文件
  4. target 你的项目名称 doend直接添加
    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 = 25;
//  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(720, 1280)];

//  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;
原文地址:https://www.cnblogs.com/LiLihongqiang/p/6704658.html