什么網(wǎng)站做國(guó)外批發(fā)國(guó)內(nèi)免費(fèi)建站平臺(tái)
Flutter視頻渲染系列
第一章 Android使用Texture渲染視頻
第二章 Windows使用Texture渲染視頻
第三章 Linux使用Texture渲染視頻
第四章 全平臺(tái)FFI+CustomPainter渲染視頻
第五章 Windows使用Native窗口渲染視頻
第六章 桌面端使用texture_rgba_renderer渲染視頻(本章)
文章目錄
- Flutter視頻渲染系列
- 前言
- 一、如何實(shí)現(xiàn)
- 1、添加插件
- 2、創(chuàng)建texture
- 3、關(guān)聯(lián)texture控件
- 4、寫入bgra
- 二、效果預(yù)覽
- 三、問題分析
- 四、完整代碼
- 總結(jié)
前言
前面幾章介紹了flutter使用texture渲染視頻的方法,但是有個(gè)問題就是在每個(gè)平臺(tái)都需要寫一套原生代碼去創(chuàng)建texture,這樣對(duì)于代碼的維護(hù)是比較不利的。最好的方法應(yīng)該是一套代碼每個(gè)平臺(tái)都能運(yùn)行,筆者最近剛好找到了pub上對(duì)texture封裝的插件,直接提供dart代碼調(diào)用texture進(jìn)行rgba的渲染,當(dāng)然只支持桌面端,即Windows、Linux、Macos,但依然是很方便了。本文只實(shí)現(xiàn)了Windows、Linux的視頻渲染。
一、如何實(shí)現(xiàn)
1、添加插件
插件的地址是https://pub-web.flutter-io.cn/packages/texture_rgba_renderer。我們直接在pubspec.yaml添加依賴即可。
依賴
texture_rgba_renderer: ^0.0.16
引用
import 'package:texture_rgba_renderer/texture_rgba_renderer.dart';
2、創(chuàng)建texture
定義一個(gè)全局插件對(duì)象
final _textureRgbaRendererPlugin = TextureRgbaRenderer();
創(chuàng)建texture,得到textureId
//textureId,使用ValueNotifier方便刷新界面
ValueNotifier<int> _textureId = ValueNotifier<int>(-1);
//參數(shù)為唯一標(biāo)識(shí)符,使用當(dāng)前對(duì)象this的hashCode。_textureId.value = await _textureRgbaRendererPlugin.createTexture(hashCode);
3、關(guān)聯(lián)texture控件
//ValueListenableBuilder與ValueNotifier是配套使用的,方便界面刷新。
ValueListenableBuilder(valueListenable: _textureId,builder: (c, v, w) {//關(guān)聯(lián)textureIdreturn Texture(textureId: _textureId.value);})),
4、寫入bgra
數(shù)據(jù)格式為ffmpeg的AV_PIX_FMT_BGRA
//數(shù)據(jù)地址
int adress = msg[2];
//一行數(shù)據(jù)長(zhǎng)度
int linesize = msg[3];
int width = msg[4];
int height = msg[5];
//將bgra數(shù)據(jù)寫入texture
final ptr = await _textureRgbaRendererPlugin.getTexturePtr(hashCode);
Native.instance.onRgba(Pointer.fromAddress(ptr),Pointer.fromAddress(adress),height* linesize,width,height,linesize);
二、效果預(yù)覽
基本的一個(gè)運(yùn)行效果
三、問題分析
texture_rgba_renderer: 0.0.16,就目前的版本來看,cpu消耗比原生寫texture要高不少。主要原因是在dart寫入bgra數(shù)據(jù)時(shí),插件底層先是拷貝了一次數(shù)據(jù),然后對(duì)又?jǐn)?shù)據(jù)進(jìn)行第二次逐行掃描拷貝到新的緩沖區(qū)對(duì)齊數(shù)據(jù),這些操作都是比較消耗cpu的,尤其是逐行掃描拷貝。
四、完整代碼
https://download.csdn.net/download/u013113678/88124430
注:ttexture_rgba_renderer: 0.0.16的性能不算特別好,請(qǐng)根據(jù)需求下載。
總結(jié)
以上就是今天要講述的內(nèi)容,使用Ftexture_rgba_renderer實(shí)現(xiàn)視頻渲染是筆者無(wú)意中發(fā)現(xiàn)的一個(gè)方法,本質(zhì)也是texture,只是有人將其封裝為了插件,但是由于適應(yīng)場(chǎng)景應(yīng)該不是視頻渲染,雖然能使用但性能并不是特別的好。