iOS直播-基于RTMP的視頻推送

所謂的視頻推送就是把攝像頭和麥克風捕獲到視頻和音頻推送到直播服務(wù)器上.我們這里使用推送協(xié)議是RTMP協(xié)議. 擴展:騰訊直播平臺,阿里直播平臺,百度直播平臺提供均為RTMP的推流和HLS/RTMP等拉流.
我們使用GDLiveStreaming來實現(xiàn)iOS的端的視頻推送

GDLiveStreaming簡介

GDLiveStreaming是對開源框架VideoCore簡單封裝.提供視頻錄制,推送與存儲. 你可以下載GDLiveStreaming源碼來學習和研究. 我們這里講解如何通過GDLiveStreaming實現(xiàn)RTMP的推流

集成GDLiveStreaming步驟

  1. 創(chuàng)建項目
  2. 打開終端cd到項目目錄
  3. 執(zhí)行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'
  1. 執(zhí)行 pod install 命名,等待其安裝完畢,打開白底的你的名稱.xcworkspace文件. 到此GDLiveStreaming安裝完畢.

GDLiveStreaming基本使用

  1. 導(dǎo)入如下頭文件
#import <GPUImage/GPUImageVideoCamera.h>
#import <GPUImage/GPUImageView.h>
#import <GDLiveStreaming/GDLRawDataOutput.h>
  1. 實現(xiàn)思路
    • 創(chuàng)建視頻攝像頭
    • 設(shè)置攝像頭幀率
    • 設(shè)置攝像頭輸出圖片的方向
    • 創(chuàng)建用于展示視頻的GPUImageView
    • 添加GPUImageView為攝像頭的的輸出目標
    • 創(chuàng)建GDLRawDataOutput對象
    • 添加數(shù)據(jù)輸出對象為攝像頭輸出目標
    • 開始捕獲視頻
    • 開始上傳視頻
  2. 代碼實現(xiàn)
//  1. 創(chuàng)建視頻攝像頭
    self.camera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720
cameraPosition:AVCaptureDevicePositionBack];
//  2. 設(shè)置攝像頭幀率
    self.camera.frameRate = 25;
//  3. 設(shè)置攝像頭輸出視頻的方向
    self.camera.outputImageOrientation = UIInterfaceOrientationPortrait;
//  4.0 創(chuàng)建用于展示視頻的GPUImageView
    GPUImageView *imageView = [[GPUImageView alloc] init];
    imageView.frame = self.view.bounds;
    [self.view addSubview:imageView];
//  4.1 添加GPUImageView為攝像頭的的輸出目標
    [self.camera addTarget:imageView];
//  5. 創(chuàng)建原始數(shù)據(jù)輸出對象
    
    GDLRawDataOutput *output = [[GDLRawDataOutput alloc] initWithVideoCamera:self.camera withImageSize:CGSizeMake(720, 1280)];
    
//  6. 添加數(shù)據(jù)輸出對象為攝像頭輸出目標
    [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;
/**恢復(fù)捕獲
 */
- (void)resumeCameraCapture;
///前置攝像頭與后置攝像頭切換
- (void)rotateCamera
  • GDLRawDataOutput常見用方法
///開始上傳視頻流, streamKey是拼接在rtmpUrl后面的路徑名稱,可以理解為視頻的直播中的房間編號.
- (void)startUploadStreamWithURL:(NSString *)rtmpUrl andStreamKey:(NSString *)streamKey;
///停止上傳視頻流
- (void)stopUploadStream;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容