所謂的視頻推送就是把攝像頭和麥克風捕獲到視頻和音頻推送到直播服務(wù)器上.我們這里使用推送協(xié)議是RTMP協(xié)議. 擴展:騰訊直播平臺,阿里直播平臺,百度直播平臺提供均為RTMP的推流和HLS/RTMP等拉流.
我們使用GDLiveStreaming來實現(xiàn)iOS的端的視頻推送
GDLiveStreaming簡介
GDLiveStreaming是對開源框架VideoCore簡單封裝.提供視頻錄制,推送與存儲. 你可以下載GDLiveStreaming源碼來學習和研究. 我們這里講解如何通過GDLiveStreaming實現(xiàn)RTMP的推流
集成GDLiveStreaming步驟
- 創(chuàng)建項目
- 打開終端
cd到項目目錄 - 執(zhí)行
pod init生成Profile文件 - 在
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'
- 執(zhí)行
pod install命名,等待其安裝完畢,打開白底的你的名稱.xcworkspace文件. 到此GDLiveStreaming安裝完畢.
GDLiveStreaming基本使用
- 導(dǎo)入如下頭文件
#import <GPUImage/GPUImageVideoCamera.h>
#import <GPUImage/GPUImageView.h>
#import <GDLiveStreaming/GDLRawDataOutput.h>
- 實現(xiàn)思路
- 創(chuàng)建視頻攝像頭
- 設(shè)置攝像頭幀率
- 設(shè)置攝像頭輸出圖片的方向
- 創(chuàng)建用于展示視頻的GPUImageView
- 添加GPUImageView為攝像頭的的輸出目標
- 創(chuàng)建
GDLRawDataOutput對象 - 添加數(shù)據(jù)輸出對象為攝像頭輸出目標
- 開始捕獲視頻
- 開始上傳視頻
- 代碼實現(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;