生活随笔
收集整理的這篇文章主要介紹了
直播技术初体验,简单实现直播不同阶段
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、前言
- 隨著時代的改變,人們對于內(nèi)容的需求也不斷提高,從文字到圖片到音頻、視頻,可能到以后的 VR
- 直播是一個非常燒錢的項目,需要足夠多的帶寬,足夠好的服務(wù)器,比如負(fù)載均衡,這里還會扯到云等等,保證大數(shù)據(jù)并發(fā),百萬人同時訪問等等
- 涉及到一些專業(yè)的視頻相關(guān)的知識,也需要很長時間的學(xué)習(xí),如解碼(硬解、軟解)、編碼、轉(zhuǎn)碼,還有底層的 ffmpeg(錄制、轉(zhuǎn)換以及流化音視頻的完整解決方案)
- 涉及到 即時通訊 和 美顏處理,其中美顏涉及到 OpenGL ,以及基于 OpenGL 的圖像/視頻處理框架 GPUImage
- 涉及到 CDN:(Content Delivery Network),即內(nèi)容分發(fā)網(wǎng)絡(luò),將網(wǎng)站的內(nèi)容發(fā)布到最接近用戶的網(wǎng)絡(luò)”邊緣”,使用戶可以就近取得所需的內(nèi)容,解決 Internet 網(wǎng)絡(luò)擁擠的狀況,提高用戶訪問網(wǎng)站的響應(yīng)速度.
- 一個簡單的模型:主播-->直播流媒體服務(wù)器--> 多個用戶同時觀看
- 簡單的流程:采集(流)-->編碼-->傳輸-->解碼-->播放
- 移動端主要協(xié)議
- RTMP 協(xié)議 Macromedia(Adobe) 公司協(xié)議
- HTTP Live Streaming(HLS) Apple 公司協(xié)議
- HLS流和RTMP對比:
HLS 主要是延時比較大 ,基于 HTTP
RTMP 主要優(yōu)勢在于延時低,基于 TCP
二、階段
- 1、客戶端拿到服務(wù)器分配好的 URL 直接解碼播放直播視頻。直接客戶端拉流
- 2、自己搭建服務(wù)器,播放的流自己控制。服務(wù)器推流,客戶端拉流
- 3、調(diào)用客戶端攝像頭等進(jìn)行錄制,客戶端推流,服務(wù)器接受流
三、各階段簡要說明
1、拿到 URL 進(jìn)行解碼播放直播的視頻
- 使用 ijkplayer ,可從網(wǎng)上找別人打包好的靜態(tài)庫,直接拖到工程中使用
- 直接用 IJKFFMoviePlayerController 創(chuàng)建 player,設(shè)置 player 中屬性 view 的尺寸,加入到控制器的 view 上
- 界面不播放,最好要記得結(jié)束播放
@interface ViewController ()
@property (nonatomic, strong) id<IJKMediaPlayback> player;
@end@implementation ViewController
- (void)viewDidLoad {[super viewDidLoad];self.player = [[IJKFFMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:@"rtmp://live.hkstv.hk.lxdns.com/live/hks"] withOptions:nil];// 設(shè)置 player 中 view 屬性的frame,且加入到控制器的 view 中self.player.view.frame = self.view.bounds;[self.view addSubview:self.player.view];// 設(shè)置 橫屏?xí)r自動伸縮self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;[self.player prepareToPlay];[self.player play];
}
- (void)viewDidDisappear:(BOOL)animated {[super viewDidDisappear:animated];[self.player stop];self.player = nil;
}
2、自己搭建服務(wù)器,服務(wù)器推流,客戶端拉流
- mac 環(huán)境推薦搭建 Nginx + rtmp ,可網(wǎng)上搜索怎么搭建的
- 配置 nginx.conf
nginx.conf, 找到/usr/local/etc/nginx/nginx.conf 文件,http {……
}
# 在http節(jié)點(diǎn)下面(也就是文件的尾部)加上rtmp配置:
rtmp {server {listen 1935;application xxx {live on;record off;}}
}說明:
rtmp是協(xié)議名稱
server 說明內(nèi)部中是服務(wù)器相關(guān)配置
listen 監(jiān)聽的端口號, rtmp協(xié)議的默認(rèn)端口號是1935
application 訪問的應(yīng)用路徑是 xxx
live on; 開啟實(shí)時
record off; 不記錄數(shù)據(jù)
只是簡單的修改下配置文件 nginx.conf 即可
1.打開 /usr/local/etc/nginx/nginx.conf
2.找到 http 下的 server ,在花括號中增加
server {listen 8080;server_name localhost;location / {root html;index index.html index.htm;}#HLS配置開始,這個配置為了`客戶端`能夠以http協(xié)議獲取HLS的拉流location /hls {# Serve HLS fragmentstypes {application/vnd.apple.mpegurl m3u8;video/mp2t ts;}root html;add_header Cache-Control no-cache;}#HLS配置結(jié)束error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}找到rtmp 下的 server 在花括號中增加rtmp {server {listen 1935;application xxx {live on;record off;}#增加對HLS支持開始#推流必須是基于H264/AAC格式application hls {live on;hls on;hls_path /usr/local/var/www/hls;}#增加對HLS支持結(jié)束}
}
- 配置完 config 后
nginx -s reload - ffmpeg 命令測試 rtmp
ffmpeg -f avfoundation -framerate 30 -i "1:0" -f avfoundation -framerate 30 -video_size 640x480 -i "0" -c:v libx264 -preset ultrafast -filter_complex 'overlay=main_w-overlay_w-10:main_h-overlay_h-10' -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://192.168.33.245:1935/xxx/room
rtmp://192.168.33.245:1935/xxx/room
- 該 rtmp 地址中,ip地址和 xxx/room 自行根據(jù)自己的配置替換,room 暫時可隨便寫,具體怎么分配還不是很清楚
- 開啟后如下
- 測試:
- 1、就可以在 VLC 中打開網(wǎng)絡(luò) URL,輸入 rtmp 地址就可以測試了
- 2、也可在 Xcode 項目中用 ijkplayer框架在模擬器中測試該地址(拉流)
- ffmpeg 命令測試 hls
ffmpeg -loglevel verbose -re -i /Users/HOWIE-CH/Desktop/1.mp4 -vcodec libx264 -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:1935/hls/1
+ 查看 然后你就可以在這個目錄 /usr/local/var/www/hls 看到生成一個個ts的文件,還會生成一個你的 m3u8 的文件名稱.m3u8的文件
+ 測試地址:http://localhost:8080/hls/你的m3u8的文件名稱.m3u8
+ 測試方法1、用 safari 瀏覽測試2、也可在 Xcode 項目中用 ijkplayer框架在模擬器中測試該地址(拉流)
3、客戶端推流
- 相關(guān)第三方框架:
- GDLiveStreaming 是對開源框架 VideoCore 簡單封裝.提供視頻錄制,推送與存儲
- https://github.com/goodow/GDLiveStreaming.git
#import "ViewController.h"
#import <GDLiveStreaming/GDLRawDataOutput.h>
#import <GPUImage/GPUImageVideoCamera.h>
#import <GPUImage/GPUImageView.h>
@interface ViewController ()
@property (nonatomic, strong) GPUImageVideoCamera *camera;
@end
@implementation ViewController
- (void)viewDidLoad {[super viewDidLoad];// 1. 創(chuàng)建視頻攝像頭self.camera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720cameraPosition:AVCaptureDevicePositionBack];// 2. 設(shè)置攝像頭幀率self.camera.frameRate = 25;// 3. 設(shè)置攝像頭輸出視頻的方向self.camera.outputImageOrientation = UIInterfaceOrientationPortraitUpsideDown;// 4. 創(chuàng)建用于展示視頻的GPUImageViewGPUImageView *imageView = [[GPUImageView alloc] init];imageView.frame = self.view.bounds;[self.view addSubview:imageView];// 4.1 添加GPUImageView為攝像頭的的輸出目標(biāo)[self.camera addTarget:imageView];// 5. 創(chuàng)建原始數(shù)據(jù)輸出對象GDLRawDataOutput *output = [[GDLRawDataOutput alloc] initWithVideoCamera:self.camera withImageSize:CGSizeMake(720, 1280)];// 5.1 添加數(shù)據(jù)輸出對象為攝像頭輸出目標(biāo)[self.camera addTarget:output];// 6.開啟前置攝像頭, 不寫這句代碼默認(rèn)開啟的是后置攝像頭[self.camera rotateCamera];// 7.開始捕獲視頻[self.camera startCameraCapture];// 8.開始上傳視頻[output startUploadStreamWithURL:@"rtmp://192.168.33.245:1935/zhanghao" andStreamKey:@"room"];
}
@end
- mac 電腦(搭建 Nginx 服務(wù)器)網(wǎng)段和真機(jī)的網(wǎng)段最好是同一局域網(wǎng)網(wǎng)段下,或者用外網(wǎng)測試
- 真機(jī)做推流到 mac上(搭建 Nginx 服務(wù)器),然后用 Xcode 里模擬器當(dāng)其他客戶端拉流
真機(jī) debug 打印
模擬器中
四、其他
- 直播要考慮的問題很多
- 主要是延遲:轉(zhuǎn)發(fā)環(huán)節(jié)越多,延遲越大;長連接會比短連接會降低延遲
- 網(wǎng)絡(luò)不穩(wěn)定時,斷線自動重連
- ...
- 直播已有一些 SDK ,如暴風(fēng)云直播、七牛云直播、網(wǎng)易云信直播 SDK、騰訊直播 SDK 等,可選性比較多,第三方框架也比較多,當(dāng)然也有通過自己封裝的協(xié)議進(jìn)行直播的
轉(zhuǎn)載于:https://www.cnblogs.com/howie-ch/p/5828129.html
總結(jié)
以上是生活随笔為你收集整理的直播技术初体验,简单实现直播不同阶段的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。