Video streaming | WebRTC | 2.4.0-exp.11 (2024)

WebRTC enables streaming video between peers. It can stream video rendered by Unity to multiple browsers at the same time.

Note

The package samples contains the PeerConnection scene which demonstrates video streaming features of the package.

Sending video

To implement video streaming, create a VideoStreamTrack instance.

// Create a track from the Cameravar camera = GetComponnent<Camera>();var track = camera.CaptureStreamTrack(1280, 720);

There is also a way to directly assign a RenderTexture.

// Get a valid RendertextureFormatvar gfxType = SystemInfo.graphicsDeviceType;var format = WebRTC.GetSupportedRenderTextureFormat(gfxType);// Create a track from the RenderTexturevar rt = new RenderTexture(width, height, 0, format);var track = new VideoStreamTrack("video", renderTexture);

Add track

Add the created video track to the RTCPeerConnection instance. The track can be added by calling the AddTrack method. Next, call the CreateOffer or CreateAnswer to create an SDP.

// Add the track.peerConnection.AddTrack(track);// Create the SDP.RTCAnswerOptions options = default;var op = pc.CreateAnswer(ref options);yield return op;

Multi track

It's possible to use multiple video tracks simultaneously. Simply call the AddTrack method multiple times and add the tracks.

foreach(var track in listTrack){ peerConnection.AddTrack(track);}
Note

When using hardware encoding, the number of tracks that can be used simultaneously may be limited depending on the graphic device's limitations. Generally, on desktop GPUs, up to two tracks can be used simultaneously on an NVIDIA Geforce card (On server-grade GPUs this is typically 4). For details, see the NVIDIA Codec SDK documentation.

Receiving video

You can use VideoStreamTrack to receive the video.The class for receiving video is got on OnTrack event of the RTCPeerConnection instance.If the type of MediaStreamTrack argument of the event is TrackKind.Video, The [Track] instance is able to be casted to the VideoStreamTrack class.

var receiveStream = new MediaStream();receiveStream.OnAddTrack = e => { if (e.Track is VideoStreamTrack track) { // You can access received texture using `track.Texture` property. } else if(e.Track is AudioStreamTrack track) { // This track is for audio. }}var peerConnection = new RTCPeerConnection();peerConnection.OnTrack = (RTCTrackEvent e) => { if (e.Track.Kind == TrackKind.Video) { // Add track to MediaStream for receiver. // This process triggers `OnAddTrack` event of `MediaStream`. receiveStream.AddTrack(e.Track); }};

Receiving multi video

Multiple VideoTracks can be received in a single RTCPeerConnection.It is a good idea to call the AddTransceiver method on the RTCPeerConnection instance as needed track count, and then do signaling.

// Call AddTransceiver as needed track count.peerConnection.AddTransceiver(TrackKind.Video);// Do process signaling
Note
  • It's impossible to send and receive video in a single VideoStreamTrack instance.
  • The VideoStreamTrack used to receive the video should be the track received in the event of the RTCPeerConnection.OnTrack.

Video configuration

Developers can control properties about video streaming quality in real-time.

  • Bitrate
  • Frame rate
  • Video resolution
Note

If you want to check these features, the Bandwidth scene in the package sample is good to learn.

Bitrate control

To control the bitrate of video streaming, use SetParameters method of RTCRtpSender instance. The instance of RTCRtpSender is obtained from RTCPeerConnection. Or, obtained from AddTrack method as its return value.

// Get from `GetSenders` method.var senders = peerConnection.GetSenders();// Get from `AddTrack` method.var sender = peerConnection.AddTrack(track);

After obtained the instance of RTCRtpSender, to get the settings about the sending stream, call the GetParameters method is able. The returning value is defined as RTCRtpSendParameters class. And call the SetParameters method with customized settings. as a result, the settings are reflected.

// Get `RTCRtpSendParameters`var parameters = sender.GetParameters();// Changing bitrate of all encoders.foreach (var encoding in parameters.Encodings){ encoding.maxBitrate = bitrate;}// Set updated parameters.sender.SetParameters(parameters);

Frame rate control

Developers can also change the encoding frame rate using SetParameters method. The example code below shows how to change the frame rate of the video encoder. You should set this parameter lower than Application.targetFramerate.

// Get `RTCRtpSendParameters`var parameters = sender.GetParameters();// Changing framerate of all encoders.foreach (var encoding in parameters.Encodings){ // Change encoding frequency 30 frame per second. encoding.maxFramerate = 30;}// Set updated parameters.sender.SetParameters(parameters);

Video resolution control

You can also change the video resolution to reduce network traffic. The scaleResolutionDownBy property in RTCRtpEncodingParameters class can resize the video resolution. The type of property is float which represents the factor of the size. If you set 2.0 for this value, it reduces the size of the video, the result's resolution 25% of the original one.

// Get `RTCRtpSendParameters`var parameters = sender.GetParameters();// Changing framerate of all encoders.foreach (var encoding in parameters.Encodings){ // Change video size to half. encoding.scaleResolutionDownBy = 2.0f;}// Set updated parameters.sender.SetParameters(parameters);

Checking bitrate

It's possible to check the current bitrate on browsers. If using Google Chrome, shows statistics of WebRTC by accessing the address chrome://webrtc-internals. Check the graph showing the received bytes per unit time in the category RTCInboundRTPVideoStream of statistics.

Video streaming | WebRTC | 2.4.0-exp.11 (1)

Video codec

You can choose from several codecs to use in this package. Noted that the available codecs vary by platform.

Selecting video codec

To select video codec, first call GetCapabilities method to get a list of available codecs on the device. By default, all available codecs are used for negotiation with other peers. When negotiating between peers, each peers search for codecs that are commonly available to both peers in order of priority. Therefore, developers need to filter and sort the video codec list to explicitly select codecs to use. The following example extracts the H.264 codecs from the list of available codecs.

// Get all available video codecs.var codecs = RTCRtpSender.GetCapabilities(TrackKind.Video).codecs;// Filter codecs.var h264Codecs = codecs.Where(codec => codec.mimeType == "video/H264");

Create a list of codecs to use and pass it to the SetCodecPreferences method. If an unavailable codec is passed, InvalidParameter is returned.

var error = transceiver.SetCodecPreferences(h264Codecs.ToArray());if (error != RTCErrorType.None) Debug.LogError("SetCodecPreferences failed");

Video Encoder

There are two types of encoder for video streaming, one is using hardware for encoding and one is using software. Regarding different kinds of codecs, the hardware encoder uses H.264, and the software encoder uses VP8, VP9, AV1.

Video Decoder

As with video encoders, we offer hardware-intensive H.264 decoders and non-hardware-intensive VP8, VP9, and AV1 decoders.

Hardware acceleration codecs

For codecs that support hardware acceleration, the following codecs are supported.

The encoders that support hardware acceleration are shown below.

PlatformGraphics APINVCODECVideoToolboxMediaCodec
Windows x64DirectX11Y--
Windows x64DirectX12Y--
Windows x64OpenGL CoreN--
Windows x64VulkanY--
Linux x64OpenGL CoreY--
Linux x64VulkanY--
MacOSMetal-Y-
iOSMetal-Y-
AndroidVulkan--Y
AndroidOpenGL ES--Y

The decoders that support hardware acceleration are shown below.

PlatformGraphics APINVCODECVideoToolboxMediaCodec
Windows x64DirectX11Y--
Windows x64DirectX12Y--
Windows x64OpenGL CoreN--
Windows x64VulkanY--
Linux x64OpenGL CoreY--
Linux x64VulkanY--
MacOSMetal-Y-
iOSMetal-Y-
AndroidVulkan--Y
AndroidOpenGL ES--Y

NVCODEC is available on the PC installed NVIDIA driver.

  • Windows: NVIDIA Driver version 456.71 or higher
  • Linux: NVIDIA Driver version 455.27 or higher

To check the compatible NVIDIA graphics card, please visit on the NVIDIA VIDEO CODEC SDK web site.

Video streaming | WebRTC | 2.4.0-exp.11 (2024)

FAQs

How does video streaming work? ›

How does streaming work? Just like other data that's sent over the Internet, audio and video data is broken down into data packets. Each packet contains a small piece of the file, and an audio or video player in the browser on the client device takes the flow of data packets and interprets them as video or audio.

What is WebRTC streaming? ›

Web Real-Time Communications (WebRTC) is an open-source communications protocol that enables real-time voice, text, and video streaming between web browsers and devices. With the help of signaling servers, WebRTC is able to manage multiple device connections and ensure their integrity.

Do I need a streaming device if I have a smart TV? ›

Smart TVs have their own streaming infrastructure and software, including preloaded apps and channels (usually the most popular apps like Netflix, Hulu, Peaco*ck, and similar channels).

What is required for streaming TV? ›

To stream content, you'll need a device that's compatible with your chosen streaming service, such as a smart TV, streaming media player, game console, or smartphone. Once you've selected your service and device, simply download the app, connect to your Flume service, sign in, and start watching!

What are the disadvantages of streaming TV? ›

Some services have the feature to download and store content on the device, but even so, the content lasts for a limited time. You need an internet connection to access streaming services. In addition, having poor internet causes buffering and other streaming problems, which means an overall poor viewing experience.

Is streaming TV free? ›

Some services offer a selection of programming for free. These services may include advertisem*nts or a more limited collection of movies and shows, but anyone can access these without having to pay anything extra. Examples of free streaming services include Tubi, Crackle or Angel Studios.

Does streaming cost money? ›

Subscribe to the top five streaming services in the U. S.—Amazon Prime, Netflix, Paramount+, Hulu and Max—with the most inclusive plans with no ads and you'll pay about $150 a month. Even a combination of the most basic plans from those five can run you $40 a month.

What is streaming for dummies? ›

Streaming is a way for you to watch your favorite TV shows or movies over an internet connection instead of through a cable or satellite connection. Streaming also allows you to watch your video content on different devices on the go, like your phone, tablet or laptop.

What is media soup? ›

Mediasoup, a server-side WebRTC library, revolutionizes the development of scalable real-time applications. Known for its superior codec support, Mediasoup offers a creative platform for building advanced real-time media streaming services.

Is WebRTC free or paid? ›

WebRTC is free, but sometimes paid. WebRTC is an open protocol and has a free open source implementation. This free implementation is embedded in all modern browsers, making it free to use as a developer and a user.

Does WebRTC cost money? ›

WebRTC is a free, open project that provides browsers and mobile applications with Real-Time Communications (RTC) capabilities via simple APIs. The WebRTC components have been optimized to best serve this purpose.

Do you have to pay to watch that is streaming? ›

TV streaming allows people to watch television, sports, news and movies online without a subscription. Viewers can access a variety of programming on-demand through internet-based streaming services.

What is streaming on TV and how do you get it? ›

Streaming TV delivers film, TV, live coverage and digital content over the internet, and services can be purchased "a la carte," which is different from the traditional approach of paying a large monthly fee for a massive cable or satellite package, regardless of how many channels you truly watch.

References

Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 6115

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.