Unity-Technologies / com.unity.webrtc

WebRTC package for Unity
Other
738 stars 186 forks source link

[REQUEST]: Add a public function to update the frame rendering under application control #1018

Open deninom opened 4 months ago

deninom commented 4 months ago

Is your feature request related to a problem?

Actually the WebRTC.Update() function is a coroutine that is execute at each EndOfFrame. With this implementation the rate isn't under application control and depends by the application rate. It will be useful to have the possibility to control the rate in the application itself.

Describe the solution you'd like

To add this function in WebRTC.cs file

   /// <summary>
   ///
   /// </summary>
   /// <returns></returns>
   public static IEnumerator UpdateNow()
   {
       var instruction = new WaitForEndOfFrame();

           // Wait until all frame rendering is done
           yield return instruction;
           {
               var tempTextureActive = RenderTexture.active;
               RenderTexture.active = null;

               var batch = Context.batch;
               batch.ResizeCapacity(VideoStreamTrack.s_tracks.Count);

               int trackIndex = 0;
               foreach (var reference in VideoStreamTrack.s_tracks.Values)
               {
                   if (!reference.TryGetTarget(out var track))
                       continue;

                   track.UpdateTexture();
                   if (track.DataPtr != IntPtr.Zero)
                   {
                       batch.data.tracks[trackIndex] = track.DataPtr;
                       trackIndex++;
                   }
               }

               batch.data.tracksCount = trackIndex;
               if (trackIndex > 0)
                   batch.Submit();

               RenderTexture.active = tempTextureActive;
           }
   }

Describe alternatives you've considered

As an alternative, it can be added a parameter ,to the current Update function, that indicates the frame per second required and of course change a little bit the code.

Additional context

No response

vodolazov commented 4 months ago

It'd be a great feature! Right now I have to hack into plugin source to be able to control the frame rate of the outgoing stream.

karasusan commented 3 months ago

memo: WRS-411