-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathWebCameraSource.cs
More file actions
135 lines (114 loc) · 4.57 KB
/
WebCameraSource.cs
File metadata and controls
135 lines (114 loc) · 4.57 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
133
134
using UnityEngine;
using LiveKit.Proto;
using Unity.Collections;
using UnityEngine.Experimental.Rendering;
using System.Runtime.InteropServices;
namespace LiveKit
{
// VideoSource for Unity WebCamTexture
public class WebCameraSource : RtcVideoSource
{
TextureFormat _textureFormat;
private RenderTexture _tempTexture;
public WebCamTexture CamTexture { get; }
public override int GetWidth()
{
return CamTexture.width;
}
public override int GetHeight()
{
return CamTexture.height;
}
protected override VideoRotation GetVideoRotation()
{
switch (CamTexture.videoRotationAngle)
{
case 0: return VideoRotation._0;
case 90: return VideoRotation._90;
case 180: return VideoRotation._180;
case 270: return VideoRotation._270;
}
return VideoRotation._0;
}
public WebCameraSource(WebCamTexture texture, VideoBufferType bufferType = VideoBufferType.Rgba) : base(VideoStreamSource.Texture, bufferType)
{
CamTexture = texture;
base.Init();
}
~WebCameraSource()
{
Dispose(false);
}
protected override void Dispose(bool disposing)
{
if (_tempTexture != null)
{
_tempTexture.Release();
UnityEngine.Object.Destroy(_tempTexture);
_tempTexture = null;
}
base.Dispose(disposing);
}
private Color32[] _readBuffer;
// Read the texture data into a native array asynchronously
protected override bool ReadBuffer()
{
if (_reading && !CamTexture.isPlaying)
return false;
_reading = true;
var textureChanged = false;
int width = GetWidth();
int height = GetHeight();
if (_previewTexture == null ||
_previewTexture.width != width ||
_previewTexture.height != height)
{
// Free previously allocated GPU/native resources before reallocating;
// otherwise the old textures and NativeArray leak on every resolution change.
if (_previewTexture != null)
UnityEngine.Object.Destroy(_previewTexture);
if (_tempTexture != null)
{
_tempTexture.Release();
UnityEngine.Object.Destroy(_tempTexture);
_tempTexture = null;
}
if (_captureBuffer.IsCreated)
_captureBuffer.Dispose();
var compatibleFormat = SystemInfo.GetCompatibleFormat(CamTexture.graphicsFormat, FormatUsage.ReadPixels);
_textureFormat = GraphicsFormatUtility.GetTextureFormat(compatibleFormat);
_bufferType = GetVideoBufferType(_textureFormat);
_readBuffer = new Color32[width * height];
_previewTexture = new Texture2D(width, height, TextureFormat.BGRA32, false);
_captureBuffer = new NativeArray<byte>(width * height * GetStrideForBuffer(_bufferType), Allocator.Persistent);
if (CamTexture.graphicsFormat != _previewTexture.graphicsFormat)
_tempTexture = new RenderTexture(width, height, 0, _previewTexture.graphicsFormat);
textureChanged = true;
}
CamTexture.GetPixels32(_readBuffer);
// GetPixels32 returns rows bottom-up; WebRTC expects top-down. Flip while copying.
var src = MemoryMarshal.Cast<Color32, byte>(_readBuffer);
var dst = _captureBuffer.AsSpan();
int rowBytes = width * GetStrideForBuffer(_bufferType);
for (int y = 0; y < height; y++)
src.Slice(y * rowBytes, rowBytes)
.CopyTo(dst.Slice((height - 1 - y) * rowBytes, rowBytes));
_requestPending = true;
if (CamTexture.graphicsFormat != _previewTexture.graphicsFormat)
{
Graphics.Blit(CamTexture, _tempTexture);
Graphics.CopyTexture(_tempTexture, _previewTexture);
}
else
{
#if UNITY_6000_0_OR_NEWER && UNITY_IOS
_previewTexture.SetPixels(CamTexture.GetPixels());
_previewTexture.Apply();
#else
Graphics.CopyTexture(CamTexture, _previewTexture);
#endif
}
return textureChanged;
}
}
}