This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
/
MediaPlayer.cs
310 lines (276 loc) · 12.8 KB
/
MediaPlayer.cs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using UnityEngine;
using Unity.Profiling;
namespace Microsoft.MixedReality.WebRTC.Unity
{
/// <summary>
/// Play video frames received from a WebRTC video track.
/// </summary>
/// <remarks>
/// This component writes to the attached <a href="https://docs.unity3d.com/ScriptReference/Material.html">Material</a>,
/// via the attached <a href="https://docs.unity3d.com/ScriptReference/Renderer.html">Renderer</a>.
/// </remarks>
[RequireComponent(typeof(Renderer))]
[AddComponentMenu("MixedReality-WebRTC/Media Player")]
public class MediaPlayer : MonoBehaviour
{
[Header("Source")]
[Tooltip("Audio source providing the audio data used by this player")]
public AudioSource AudioSource;
[Tooltip("Video source providing the video frames rendered by this player")]
public VideoSource VideoSource;
[Tooltip("Max video playback framerate, in frames per second")]
[Range(0.001f, 120f)]
public float MaxVideoFramerate = 30f;
[Header("Statistics")]
public bool EnableStatistics = true;
/// <summary>
/// A textmesh onto which frame load stat data will be written
/// </summary>
/// <remarks>
/// This is how fast the frames are given from the underlying implementation
/// </remarks>
[Tooltip("A textmesh onto which frame load stat data will be written")]
public TextMesh FrameLoadStatHolder;
/// <summary>
/// A textmesh onto which frame present stat data will be written
/// </summary>
/// <remarks>
/// This is how fast we render frames to the display
/// </remarks>
[Tooltip("A textmesh onto which frame present stat data will be written")]
public TextMesh FramePresentStatHolder;
/// <summary>
/// A textmesh into which frame skip stat dta will be written
/// </summary>
/// <remarks>
/// This is how often we skip presenting an underlying frame
/// </remarks>
[Tooltip("A textmesh onto which frame skip stat data will be written")]
public TextMesh FrameSkipStatHolder;
/// <summary>
/// The frame queue from which frames will be rendered.
/// </summary>
public IVideoFrameQueue FrameQueue = null;
/// <summary>
/// Internal reference to the attached texture
/// </summary>
private Texture2D _textureY = null; // also used for ARGB32
private Texture2D _textureU = null;
private Texture2D _textureV = null;
/// <summary>
/// Internal timing counter
/// </summary>
private float lastUpdateTime = 0.0f;
private Material videoMaterial;
private float _minUpdateDelay;
private ProfilerMarker displayStatsMarker = new ProfilerMarker("DisplayStats");
private ProfilerMarker loadTextureDataMarker = new ProfilerMarker("LoadTextureData");
private ProfilerMarker uploadTextureToGpuMarker = new ProfilerMarker("UploadTextureToGPU");
private void Start()
{
CreateEmptyVideoTextures();
// Leave 3ms of margin, otherwise it misses 1 frame and drops to ~20 FPS
// when Unity is running at 60 FPS.
_minUpdateDelay = Mathf.Max(0f, 1f / Mathf.Max(0.001f, MaxVideoFramerate) - 0.003f);
AudioSource?.AudioStreamStarted.AddListener(AudioStreamStarted);
AudioSource?.AudioStreamStopped.AddListener(AudioStreamStopped);
VideoSource?.VideoStreamStarted.AddListener(VideoStreamStarted);
VideoSource?.VideoStreamStopped.AddListener(VideoStreamStopped);
}
private void OnDestroy()
{
AudioSource?.AudioStreamStarted.RemoveListener(AudioStreamStarted);
AudioSource?.AudioStreamStopped.RemoveListener(AudioStreamStopped);
VideoSource?.VideoStreamStarted.RemoveListener(VideoStreamStarted);
VideoSource?.VideoStreamStopped.RemoveListener(VideoStreamStopped);
}
private void AudioStreamStarted()
{
}
private void AudioStreamStopped()
{
}
private void VideoStreamStarted()
{
FrameQueue = VideoSource.FrameQueue;
}
private void VideoStreamStopped()
{
FrameQueue = null;
// Clear the video display to not confuse the user who could otherwise
// think that the video is still playing but is lagging.
CreateEmptyVideoTextures();
}
private void CreateEmptyVideoTextures()
{
// Create a default checkboard texture which visually indicates
// that no data is available. This is useful for debugging and
// for the user to know about the state of the video.
_textureY = new Texture2D(2, 2);
_textureY.SetPixel(0, 0, Color.blue);
_textureY.SetPixel(1, 1, Color.blue);
_textureY.Apply();
_textureU = new Texture2D(2, 2);
_textureU.SetPixel(0, 0, Color.blue);
_textureU.SetPixel(1, 1, Color.blue);
_textureU.Apply();
_textureV = new Texture2D(2, 2);
_textureV.SetPixel(0, 0, Color.blue);
_textureV.SetPixel(1, 1, Color.blue);
_textureV.Apply();
// Assign that texture to the video player's Renderer component
videoMaterial = GetComponent<Renderer>().material;
//< TODO - Better abstraction
if (FrameQueue is VideoFrameQueue<I420AVideoFrameStorage>)
{
videoMaterial.SetTexture("_YPlane", _textureY);
videoMaterial.SetTexture("_UPlane", _textureU);
videoMaterial.SetTexture("_VPlane", _textureV);
}
else if (FrameQueue is VideoFrameQueue<Argb32VideoFrameStorage>)
{
videoMaterial.SetTexture("_MainTex", _textureY);
}
}
/// <summary>
/// Unity Engine Start() hook
/// </summary>
/// <remarks>
/// https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html
/// </remarks>
private void Update()
{
if (FrameQueue != null)
{
#if UNITY_EDITOR
// Inside the Editor, constantly update _minUpdateDelay to
// react to user changes to MaxFramerate.
// Leave 3ms of margin, otherwise it misses 1 frame and drops to ~20 FPS
// when Unity is running at 60 FPS.
_minUpdateDelay = Mathf.Max(0f, 1f / Mathf.Max(0.001f, MaxVideoFramerate) - 0.003f);
#endif
var curTime = Time.time;
if (curTime - lastUpdateTime >= _minUpdateDelay)
{
TryProcessFrame();
lastUpdateTime = curTime;
}
if (EnableStatistics)
{
// Share our stats values, if possible.
using (var profileScope = displayStatsMarker.Auto())
{
if (FrameLoadStatHolder != null)
{
FrameLoadStatHolder.text = FrameQueue.QueuedFramesPerSecond.ToString("F2");
}
if (FramePresentStatHolder != null)
{
FramePresentStatHolder.text = FrameQueue.DequeuedFramesPerSecond.ToString("F2");
}
if (FrameSkipStatHolder != null)
{
FrameSkipStatHolder.text = FrameQueue.DroppedFramesPerSecond.ToString("F2");
}
}
}
}
}
/// <summary>
/// Internal helper that attempts to process frame data in the frame queue
/// </summary>
private void TryProcessFrame()
{
//< TODO - Better abstraction
if (FrameQueue is VideoFrameQueue<I420AVideoFrameStorage> i420queue)
{
if (i420queue.TryDequeue(out I420AVideoFrameStorage frame))
{
int lumaWidth = (int)frame.Width;
int lumaHeight = (int)frame.Height;
if (_textureY == null || (_textureY.width != lumaWidth || _textureY.height != lumaHeight))
{
_textureY = new Texture2D(lumaWidth, lumaHeight, TextureFormat.R8, mipChain: false);
videoMaterial.SetTexture("_YPlane", _textureY);
}
int chromaWidth = lumaWidth / 2;
int chromaHeight = lumaHeight / 2;
if (_textureU == null || (_textureU.width != chromaWidth || _textureU.height != chromaHeight))
{
_textureU = new Texture2D(chromaWidth, chromaHeight, TextureFormat.R8, mipChain: false);
videoMaterial.SetTexture("_UPlane", _textureU);
}
if (_textureV == null || (_textureV.width != chromaWidth || _textureV.height != chromaHeight))
{
_textureV = new Texture2D(chromaWidth, chromaHeight, TextureFormat.R8, mipChain: false);
videoMaterial.SetTexture("_VPlane", _textureV);
}
// Copy data from C# buffer into system memory managed by Unity.
// Note: This only "looks right" in Unity because we apply the
// "YUVFeedShader" to the texture (converting YUV planar to RGB).
using (var profileScope = loadTextureDataMarker.Auto())
{
unsafe
{
fixed (void* buffer = frame.Buffer)
{
var src = new System.IntPtr(buffer);
int lumaSize = lumaWidth * lumaHeight;
_textureY.LoadRawTextureData(src, lumaSize);
src += lumaSize;
int chromaSize = chromaWidth * chromaHeight;
_textureU.LoadRawTextureData(src, chromaSize);
src += chromaSize;
_textureV.LoadRawTextureData(src, chromaSize);
}
}
}
// Upload from system memory to GPU
using (var profileScope = uploadTextureToGpuMarker.Auto())
{
_textureY.Apply();
_textureU.Apply();
_textureV.Apply();
}
// Recycle the video frame packet for a later frame
i420queue.RecycleStorage(frame);
}
}
else if (FrameQueue is VideoFrameQueue<Argb32VideoFrameStorage> argbQueue)
{
if (argbQueue.TryDequeue(out Argb32VideoFrameStorage frame))
{
int width = (int)frame.Width;
int height = (int)frame.Height;
if (_textureY == null || (_textureY.width != width || _textureY.height != height))
{
_textureY = new Texture2D(width, height, TextureFormat.BGRA32, mipChain: false);
videoMaterial.SetTexture("_MainTex", _textureY);
}
// Copy data from C# buffer into system memory managed by Unity.
using (var profileScope = loadTextureDataMarker.Auto())
{
unsafe
{
fixed (void* buffer = frame.Buffer)
{
var src = new System.IntPtr(buffer);
int size = width * height * 4;
_textureY.LoadRawTextureData(src, size);
}
}
}
// Upload from system memory to GPU
using (var profileScope = uploadTextureToGpuMarker.Auto())
{
_textureY.Apply();
}
// Recycle the video frame packet for a later frame
argbQueue.RecycleStorage(frame);
}
}
}
}
}