master
Cal 2024-11-19 15:57:10 +08:00
parent 6e555569c0
commit d714fb604f
3 changed files with 158 additions and 69 deletions

View File

@ -40,10 +40,12 @@ namespace ZC
private byte[] _bitmapArray;
private ConcurrentQueue<NativeArray<byte>> _textureDataPool;
private Queue<(AsyncGPUReadbackRequest, NativeArray<byte>)> _textureDatas;
private ConcurrentQueue<(AsyncGPUReadbackRequest, NativeArray<byte>)> _doneTextureDatas;
private ConcurrentDictionary<uint, NativeArray<byte>> _doneTextureDatas;
private int _targetTextureWidth;
private int _targetTextureHeight;
private uint _frameIndex;
private uint _doneFrameIndex;
private int _frameCount;
public void SetCamera(Camera camera)
{
@ -61,8 +63,7 @@ namespace ZC
}
_textureDataPool = new ConcurrentQueue<NativeArray<byte>>();
_textureDatas = new Queue<(AsyncGPUReadbackRequest, NativeArray<byte>)>();
_doneTextureDatas = new ConcurrentQueue<(AsyncGPUReadbackRequest, NativeArray<byte>)>();
_doneTextureDatas = new ConcurrentDictionary<uint, NativeArray<byte>>();
_config.Load();
_timer = new Timer(Tick, null, TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(16));
Setup(Camera.main);
@ -76,16 +77,18 @@ namespace ZC
{
try
{
if (this._doneTextureDatas.TryDequeue(out var result))
if (_doneTextureDatas.TryRemove(this._doneFrameIndex, out var result))
{
var textureData = result.Item2;
Debug.Log($"[{_frameCount}]_doneFrameIndex:{_doneFrameIndex}");
var textureData = result;
Profiler.BeginSample("Conversion");
Bitmap.EncodeToBitmap(textureData, _bitmapArray, 0, textureData.Length, _targetTextureWidth, this._targetTextureHeight);
Profiler.EndSample();
Profiler.BeginSample("WriteBuffer");
_process.StandardInput.BaseStream.Write(_bitmapArray, 0, _bitmapArray.Length);
Profiler.EndSample();
_textureDataPool.Enqueue(result.Item2);
_textureDataPool.Enqueue(result);
_ = _doneFrameIndex + 1 >= uint.MaxValue ? _doneFrameIndex = 0 : _doneFrameIndex++;
}
}
catch (Exception e)
@ -108,6 +111,7 @@ namespace ZC
private void Update()
{
this._frameCount = Time.frameCount;
if (_sendData != 0)
{
if (_camera && !_isRequesting)
@ -120,40 +124,30 @@ namespace ZC
nativeArray = new NativeArray<byte>(targetTextureByteCount, Allocator.Persistent);
}
var asyncGPUReadbackRequest = AsyncGPUReadback.RequestIntoNativeArray(ref nativeArray, cameraActiveTexture, 0, GraphicsFormat.B8G8R8_SRGB, null);
_textureDatas.Enqueue((asyncGPUReadbackRequest, nativeArray));
}
if (_textureDatas.TryPeek(out var result))
{
if (result.Item1.done)
var frameIndex = this._frameIndex + 1 >= uint.MaxValue ? this._frameIndex = 0 : this._frameIndex++;
Debug.Log($"[{Time.frameCount}]frameIndex:{frameIndex}");
AsyncGPUReadback.RequestIntoNativeArray(ref nativeArray, cameraActiveTexture, 0, GraphicsFormat.B8G8R8_SRGB, request =>
{
this._textureDatas.Dequeue();
if (result.Item1.hasError)
if (request is { done: true, hasError: false })
{
Debug.LogError("Request GPU DATA has error");
this._doneTextureDatas[frameIndex] = nativeArray;
}
else
{
//放入多线程计算
this._doneTextureDatas.Enqueue(result);
this._textureDataPool.Enqueue(nativeArray);
Debug.LogError($"Done:{request.done} Error:{request.hasError}");
}
}
});
}
}
}
private void DisposeCaptureThread()
{
foreach (var textureData in this._textureDatas)
foreach (var kv in this._doneTextureDatas)
{
textureData.Item2.Dispose();
}
_textureDatas.Clear();
foreach (var doneTextureData in this._doneTextureDatas)
{
doneTextureData.Item2.Dispose();
kv.Value.Dispose();
}
_doneTextureDatas.Clear();
@ -163,6 +157,8 @@ namespace ZC
nativeArray.Dispose();
}
_textureDataPool.Clear();
if (_process != null && !this._process.HasExited)
{
GenerateConsoleCtrlEvent(ConsoleCtrlEvent.CTRL_C, 0);
@ -188,13 +184,13 @@ namespace ZC
processStartInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;
processStartInfo.FileName = $"\"{_config.ffmpegPath}\"";
processStartInfo.Arguments =
$" -probesize 32 -thread_queue_size 5096 -fflags discardcorrupt -flags low_delay -analyzeduration 0 " +
$" -probesize 64 -thread_queue_size 5096 -fflags discardcorrupt -flags low_delay -analyzeduration 0 " +
$" -rtbufsize 100M -f dshow -i audio=\"virtual-audio-capturer\" " +
$" -f image2pipe -use_wallclock_as_timestamps 1 -r 60 -i - " +
$" -loglevel info " +
$" -map 0:a:0 -map 1:v:0 " +
$" -c:a aac " +
$" -c:v:0 libx264 -g 1 -bf 0 -max_delay 0 -vf scale={this._config.resolution} -preset:v ultrafast -tune:v zerolatency -crf 10 -pix_fmt yuv420p -strict -2 " +
$" -c:v:0 libx264 -g 1 -bf 0 -speed 1x -max_delay 0 -vf scale={this._config.resolution} -preset:v ultrafast -tune:v zerolatency -crf 10 -pix_fmt yuv420p -strict -2 " +
$" -f flv {this._config.server}{this._config.appName} -bf 0 ";
Debug.Log(processStartInfo.Arguments);
@ -241,6 +237,7 @@ namespace ZC
_output = new Texture2D(this._targetTextureWidth, this._targetTextureHeight);
_frameIndex = _doneFrameIndex = 0;
CreateCaptureThread();
StartCoroutine(this.Test());

View File

@ -284,7 +284,99 @@ PlayerSettings:
AndroidValidateAppBundleSize: 1
AndroidAppBundleSizeToValidate: 150
m_BuildTargetIcons: []
m_BuildTargetPlatformIcons: []
m_BuildTargetPlatformIcons:
- m_BuildTarget: Android
m_Icons:
- m_Textures: []
m_Width: 432
m_Height: 432
m_Kind: 2
m_SubKind:
- m_Textures: []
m_Width: 324
m_Height: 324
m_Kind: 2
m_SubKind:
- m_Textures: []
m_Width: 216
m_Height: 216
m_Kind: 2
m_SubKind:
- m_Textures: []
m_Width: 162
m_Height: 162
m_Kind: 2
m_SubKind:
- m_Textures: []
m_Width: 108
m_Height: 108
m_Kind: 2
m_SubKind:
- m_Textures: []
m_Width: 81
m_Height: 81
m_Kind: 2
m_SubKind:
- m_Textures: []
m_Width: 192
m_Height: 192
m_Kind: 1
m_SubKind:
- m_Textures: []
m_Width: 144
m_Height: 144
m_Kind: 1
m_SubKind:
- m_Textures: []
m_Width: 96
m_Height: 96
m_Kind: 1
m_SubKind:
- m_Textures: []
m_Width: 72
m_Height: 72
m_Kind: 1
m_SubKind:
- m_Textures: []
m_Width: 48
m_Height: 48
m_Kind: 1
m_SubKind:
- m_Textures: []
m_Width: 36
m_Height: 36
m_Kind: 1
m_SubKind:
- m_Textures: []
m_Width: 192
m_Height: 192
m_Kind: 0
m_SubKind:
- m_Textures: []
m_Width: 144
m_Height: 144
m_Kind: 0
m_SubKind:
- m_Textures: []
m_Width: 96
m_Height: 96
m_Kind: 0
m_SubKind:
- m_Textures: []
m_Width: 72
m_Height: 72
m_Kind: 0
m_SubKind:
- m_Textures: []
m_Width: 48
m_Height: 48
m_Kind: 0
m_SubKind:
- m_Textures: []
m_Width: 36
m_Height: 36
m_Kind: 0
m_SubKind:
m_BuildTargetBatching:
- m_BuildTarget: Standalone
m_StaticBatching: 1

View File

@ -14,12 +14,12 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_PixelRect:
serializedVersion: 2
x: 34
y: 176
x: 36
y: 151
width: 1882
height: 1202
m_ShowMode: 4
m_Title: Console
m_Title: Inspector
m_RootView: {fileID: 2}
m_MinSize: {x: 875, y: 321}
m_MaxSize: {x: 10000, y: 10000}
@ -120,7 +120,7 @@ MonoBehaviour:
m_MinSize: {x: 400, y: 150}
m_MaxSize: {x: 32384, y: 24288}
vertical: 0
controlID: 17340
controlID: 149
draggingID: 0
--- !u!114 &6
MonoBehaviour:
@ -146,7 +146,7 @@ MonoBehaviour:
m_MinSize: {x: 200, y: 150}
m_MaxSize: {x: 16192, y: 24288}
vertical: 0
controlID: 17341
controlID: 26
draggingID: 0
--- !u!114 &7
MonoBehaviour:
@ -173,7 +173,7 @@ MonoBehaviour:
m_MinSize: {x: 100, y: 150}
m_MaxSize: {x: 8096, y: 24288}
vertical: 1
controlID: 17342
controlID: 27
draggingID: 0
--- !u!114 &8
MonoBehaviour:
@ -194,8 +194,8 @@ MonoBehaviour:
y: 0
width: 1183
height: 320
m_MinSize: {x: 201, y: 221}
m_MaxSize: {x: 4001, y: 4021}
m_MinSize: {x: 200, y: 200}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 15}
m_Panes:
- {fileID: 15}
@ -220,8 +220,8 @@ MonoBehaviour:
y: 320
width: 1183
height: 475
m_MinSize: {x: 201, y: 221}
m_MaxSize: {x: 4001, y: 4021}
m_MinSize: {x: 200, y: 200}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 14}
m_Panes:
- {fileID: 14}
@ -246,8 +246,8 @@ MonoBehaviour:
y: 795
width: 1183
height: 357
m_MinSize: {x: 101, y: 121}
m_MaxSize: {x: 4001, y: 4021}
m_MinSize: {x: 100, y: 100}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 16}
m_Panes:
- {fileID: 16}
@ -272,8 +272,8 @@ MonoBehaviour:
y: 0
width: 241
height: 1152
m_MinSize: {x: 102, y: 121}
m_MaxSize: {x: 4002, y: 4021}
m_MinSize: {x: 100, y: 100}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 17}
m_Panes:
- {fileID: 17}
@ -324,8 +324,8 @@ MonoBehaviour:
y: 0
width: 296
height: 1152
m_MinSize: {x: 276, y: 71}
m_MaxSize: {x: 4001, y: 4021}
m_MinSize: {x: 275, y: 50}
m_MaxSize: {x: 4000, y: 4000}
m_ActualView: {fileID: 19}
m_Panes:
- {fileID: 19}
@ -352,8 +352,8 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 34
y: 526
x: 36
y: 501
width: 1182
height: 454
m_SerializedDataModeController:
@ -450,8 +450,8 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 34
y: 206
x: 36
y: 181
width: 1182
height: 299
m_SerializedDataModeController:
@ -775,7 +775,7 @@ MonoBehaviour:
m_Position:
m_Target: {x: 1825.5619, y: 1029.0568, z: -0.9695983}
speed: 2
m_Value: {x: 1825.5618, y: 1029.0566, z: -0.9695303}
m_Value: {x: 1825.5619, y: 1029.0568, z: -0.9695983}
m_RenderMode: 0
m_CameraMode:
drawMode: 0
@ -823,11 +823,11 @@ MonoBehaviour:
m_Rotation:
m_Target: {x: -0.18233694, y: -0.31602854, z: 0.062028006, w: -0.928995}
speed: 2
m_Value: {x: -0.18233694, y: -0.31602854, z: 0.06202801, w: -0.928995}
m_Value: {x: -0.18233694, y: -0.31602854, z: 0.062028006, w: -0.928995}
m_Size:
m_Target: 12.124355
speed: 2
m_Value: 12.124439
m_Value: 12.124355
m_Ortho:
m_Target: 0
speed: 2
@ -872,8 +872,8 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: -32000
y: -31175
x: 36
y: 976
width: 1182
height: 336
m_SerializedDataModeController:
@ -906,8 +906,8 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 1217
y: 206
x: 1219
y: 181
width: 239
height: 1131
m_SerializedDataModeController:
@ -923,9 +923,9 @@ MonoBehaviour:
m_SceneHierarchy:
m_TreeViewState:
scrollPos: {x: 0, y: 0}
m_SelectedIDs: 965a0000
m_LastClickedID: 23190
m_ExpandedIDs: 4672b4ff24fbffffa45a0000b65a0000
m_SelectedIDs:
m_LastClickedID: 0
m_ExpandedIDs: 2cfbffff
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
@ -969,8 +969,8 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 1458
y: 206
x: 1460
y: 181
width: 160
height: 1131
m_SerializedDataModeController:
@ -1009,9 +1009,9 @@ MonoBehaviour:
m_IsLocked: 0
m_FolderTreeState:
scrollPos: {x: 0, y: 0}
m_SelectedIDs: f85a0000
m_LastClickedID: 23288
m_ExpandedIDs: 00000000f85a000000ca9a3b
m_SelectedIDs: 005b0000
m_LastClickedID: 23296
m_ExpandedIDs: 00000000005b000000ca9a3b
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
@ -1039,7 +1039,7 @@ MonoBehaviour:
scrollPos: {x: 0, y: 0}
m_SelectedIDs:
m_LastClickedID: 0
m_ExpandedIDs: 00000000f85a000000ca9a3b
m_ExpandedIDs: 00000000005b0000
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
@ -1115,8 +1115,8 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 1620
y: 206
x: 1622
y: 181
width: 295
height: 1131
m_SerializedDataModeController: