Class CameraExtension
Provides extension methods for Camera objects to facilitate video streaming functionalities.
Namespace: Unity.WebRTC
Assembly: Unity.WebRTC.dll
Syntax
public static class CameraExtension
Methods
CaptureStream(Camera, int, int, RenderTextureDepth)
Creates an instance of MediaStream capturing video from a Camera object.
Declaration
public static MediaStream CaptureStream(this Camera cam, int width, int height, RenderTextureDepth depth = RenderTextureDepth.Depth24)
Parameters
Type | Name | Description |
---|---|---|
Camera | cam | The camera from which to capture video frames |
int | width | The desired width of the video stream, in pixels. Must be greater than zero |
int | height | The desired height of the video stream, in pixels. Must be greater than zero |
RenderTextureDepth | depth | The depth buffer format for the render texture. Default is Depth24 |
Returns
Type | Description |
---|---|
MediaStream | A MediaStream containing the video track captured from the camera. |
Remarks
It is recommended to maintain a reference to the MediaStream instance created by this method. Without a reference, the instance may be collected by the garbage collector automatically.
Examples
Creates a MediaStream with a VideoStreamTrack capturing video from the camera.
private static MediaStream CreateMediaStream()
{
MediaStream videoStream = Camera.main.CaptureStream(1280, 720);
return videoStream;
}
CaptureStreamTrack(Camera, int, int, RenderTextureDepth, CopyTexture)
Creates an instance of VideoStreamTrack for streaming video from a Camera object.
Declaration
public static VideoStreamTrack CaptureStreamTrack(this Camera cam, int width, int height, RenderTextureDepth depth = RenderTextureDepth.Depth24, CopyTexture textureCopy = null)
Parameters
Type | Name | Description |
---|---|---|
Camera | cam | The camera from which to capture video frames |
int | width | The desired width of the video stream, in pixels. Must be greater than zero |
int | height | The desired height of the video stream, in pixels. Must be greater than zero |
RenderTextureDepth | depth | The depth buffer format for the render texture. Default is Depth24 |
CopyTexture | textureCopy | An optional CopyTexture to facilitate texture copying. Default is null |
Returns
Type | Description |
---|---|
VideoStreamTrack | A VideoStreamTrack instance that can be used to stream video. |
Remarks
It is recommended to maintain a reference to the VideoStreamTrack instance created by this method. Without a reference, the instance may be collected by the garbage collector automatically.
Examples
Creates a GameObject with a Camera component and a VideoStreamTrack capturing video from the camera.
private void AddVideoObject()
{
Camera newCam = new GameObject("Camera").AddComponent<Camera>();
RawImage newSource = new GameObject("SourceImage").AddComponent<RawImage>();
try
{
videoStreamTrackList.Add(newCam.CaptureStreamTrack(WebRTCSettings.StreamSize.x, WebRTCSettings.StreamSize.y));
newSource.texture = newCam.targetTexture;
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}