-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathCameraVideoSource.cs
More file actions
133 lines (116 loc) · 4.63 KB
/
CameraVideoSource.cs
File metadata and controls
133 lines (116 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using UnityEngine;
using LiveKit.Proto;
using LiveKit.Internal;
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering;
using System;
using Unity.Collections;
namespace LiveKit
{
// VideoSource for Unity Camera
public class CameraVideoSource : RtcVideoSource
{
private TextureFormat _textureFormat;
private RenderTexture _renderTexture;
public Camera Camera { get; }
public override int GetWidth()
{
return Camera.pixelWidth;
}
public override int GetHeight()
{
return Camera.pixelHeight;
}
protected override VideoRotation GetVideoRotation()
{
return VideoRotation._0;
}
public CameraVideoSource(Camera camera, VideoBufferType bufferType = VideoBufferType.Rgba) : base(VideoStreamSource.Screen, bufferType)
{
Camera = camera;
base.Init();
}
~CameraVideoSource()
{
Dispose(false);
ClearRenderTexture();
}
public override void Stop()
{
base.Stop();
ClearRenderTexture();
}
protected override void Dispose(bool disposing)
{
if (disposing && _renderTexture != null)
{
_renderTexture.Release();
UnityEngine.Object.Destroy(_renderTexture);
_renderTexture = null;
}
base.Dispose(disposing);
}
private void ClearRenderTexture()
{
if (_renderTexture)
{
var renderText = _renderTexture as RenderTexture;
renderText.Release(); // can only be done on main thread
}
}
// Read the texture data into a native array asynchronously
protected override bool ReadBuffer()
{
if (_reading)
return false;
_reading = true;
var textureChanged = false;
try
{
if (_renderTexture == null || _renderTexture.width != GetWidth() || _renderTexture.height != GetHeight())
{
// Free previously allocated GPU/native resources before reallocating;
// otherwise the old textures and NativeArray leak on every resolution change.
if (_renderTexture != null)
{
_renderTexture.Release();
UnityEngine.Object.Destroy(_renderTexture);
}
if (_previewTexture != null)
UnityEngine.Object.Destroy(_previewTexture);
if (_captureBuffer.IsCreated)
_captureBuffer.Dispose();
var targetFormat = Utils.GetSupportedGraphicsFormat(SystemInfo.graphicsDeviceType);
var compatibleFormat = SystemInfo.GetCompatibleFormat(targetFormat, FormatUsage.ReadPixels);
_textureFormat = GraphicsFormatUtility.GetTextureFormat(compatibleFormat);
_bufferType = GetVideoBufferType(_textureFormat);
_renderTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat);
Camera.targetTexture = _renderTexture as RenderTexture;
_captureBuffer = new NativeArray<byte>(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent);
_previewTexture = new Texture2D(GetWidth(), GetHeight(), _textureFormat, false);
textureChanged = true;
}
ScreenCapture.CaptureScreenshotIntoRenderTexture(_renderTexture);
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
// Flip the texture for OSX
Graphics.CopyTexture(_renderTexture, _previewTexture);
var pixels = _previewTexture.GetPixels();
var flippedPixels = new Color[pixels.Length];
for (int i = 0; i < _previewTexture.height; i++)
{
Array.Copy(pixels, i * _previewTexture.width, flippedPixels, (_previewTexture.height - i - 1) * _previewTexture.width, _previewTexture.width);
}
_previewTexture.SetPixels(flippedPixels);
#else
Graphics.CopyTexture(_renderTexture, _previewTexture);
#endif
AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _renderTexture, 0, _textureFormat, OnReadback);
}
catch (Exception e)
{
Utils.Error(e);
}
return textureChanged;
}
}
}