Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Runtime/Scripts/Internal/UpdateCoroutine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections;
using UnityEngine;

namespace Unity.WebRTC
{
public class UpdateCoroutine : MonoBehaviour
{
Coroutine _coroutine;

public Func<IEnumerator> routine
{
set
{
if (_coroutine != null)
StopCoroutine(_coroutine);
_coroutine = StartCoroutine(value());
}
}


void OnDestroy()
{
if (_coroutine != null)
StopCoroutine(_coroutine);
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/Internal/UpdateCoroutine.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Runtime/Scripts/WebRTC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,12 +633,21 @@ public static class WebRTC
private static Context s_context = null;
private static SynchronizationContext s_syncContext;
private static ILogger s_logger;
private static GameObject s_obj;

[RuntimeInitializeOnLoadMethod]
static void RuntimeInitializeOnLoadMethod()
{
// Initialize a custom invokable synchronization context to wrap the main thread UnitySynchronizationContext
s_syncContext = new ExecutableUnitySynchronizationContext(SynchronizationContext.Current);

// Instantiate GameObject for coroutine.
s_obj = new GameObject("webrtc");
s_obj.hideFlags = HideFlags.HideAndDontSave;
if (Application.isPlaying)
UnityEngine.Object.DontDestroyOnLoad(s_obj);
var comp = s_obj.AddComponent<UpdateCoroutine>();
comp.routine = UpdateInternal;
}

internal static void InitializeInternal(bool limitTextureSize = true, bool enableNativeLog = false,
Expand Down Expand Up @@ -666,10 +675,21 @@ internal static void InitializeInternal(bool limitTextureSize = true, bool enabl
///
/// </summary>
/// <returns></returns>
[Obsolete]
public static IEnumerator Update()
{
var instruction = new WaitForEndOfFrame();

while (true)
{
yield return instruction;
}
}

public static IEnumerator UpdateInternal()
{
var instruction = new WaitForEndOfFrame();

while (true)
{
// Wait until all frame rendering is done
Expand Down Expand Up @@ -999,6 +1019,7 @@ static void DestroyImmediate(object state)
{
var obj = state as UnityEngine.Object;
UnityEngine.Object.DestroyImmediate(obj);
obj = null;
}

static void Destroy(object state)
Expand Down
1 change: 0 additions & 1 deletion Samples~/Audio/AudioSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class AudioSample : MonoBehaviour

void Start()
{
StartCoroutine(WebRTC.Update());
StartCoroutine(LoopStatsCoroutine());

toggleEnableMicrophone.isOn = false;
Expand Down
1 change: 0 additions & 1 deletion Samples~/Bandwidth/BandwidthSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ private void AddTracks()

if (!videoUpdateStarted)
{
StartCoroutine(WebRTC.Update());
StartCoroutine(LoopStatsCoroutine());
videoUpdateStarted = true;
}
Expand Down
7 changes: 0 additions & 7 deletions Samples~/ChangeCodecs/ChangeCodecsSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class ChangeCodecsSample : MonoBehaviour
private DelegateOnIceCandidate pc2OnIceCandidate;
private DelegateOnTrack pc2Ontrack;
private DelegateOnNegotiationNeeded pc1OnNegotiationNeeded;
private bool videoUpdateStarted;

private readonly string[] excludeCodecMimeType = { "video/red", "video/ulpfec", "video/rtx" };
private RTCRtpCodecCapability[] availableCodecs;
Expand Down Expand Up @@ -166,12 +165,6 @@ private void AddTracks()
pc1Senders.Add(_pc1.AddTrack(track, videoStream));
}

if (!videoUpdateStarted)
{
StartCoroutine(WebRTC.Update());
videoUpdateStarted = true;
}

RTCRtpCodecCapability[] codecs = null;
if (codecSelector.value == 0)
{
Expand Down
2 changes: 0 additions & 2 deletions Samples~/E2ELatency/E2ELatencySample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ private void Start()
receiveStream.AddTrack(e.Track);
};
pc1OnNegotiationNeeded = () => { StartCoroutine(PeerNegotiationNeeded(_pc1)); };

StartCoroutine(WebRTC.Update());
}

private void OnFramerateChanged(int value)
Expand Down
7 changes: 0 additions & 7 deletions Samples~/Encryption/EncryptionSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class EncryptionSample : MonoBehaviour
private DelegateOnIceCandidate pc2OnIceCandidate;
private DelegateOnTrack pc2Ontrack;
private DelegateOnNegotiationNeeded pc1OnNegotiationNeeded;
private bool videoUpdateStarted;

Dictionary<RTCEncodedVideoFrameType, int> frameTypeToCryptoOffset;

Expand Down Expand Up @@ -408,12 +407,6 @@ private void AddTracks()
{
SetUpSenderTransform(sender);
}

if (!videoUpdateStarted)
{
StartCoroutine(WebRTC.Update());
videoUpdateStarted = true;
}
}

private void RemoveTracks()
Expand Down
8 changes: 0 additions & 8 deletions Samples~/MediaStream/MediaStreamSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class MediaStreamSample : MonoBehaviour
private DelegateOnTrack pc2Ontrack;
private DelegateOnNegotiationNeeded pc1OnNegotiationNeeded;
private StringBuilder trackInfos;
private bool videoUpdateStarted;

private void Awake()
{
Expand Down Expand Up @@ -129,13 +128,6 @@ private void AddTracks()
}
}
}

if (!videoUpdateStarted)
{
StartCoroutine(WebRTC.Update());
videoUpdateStarted = true;
}

addTracksButton.interactable = false;
removeTracksButton.interactable = true;
}
Expand Down
7 changes: 0 additions & 7 deletions Samples~/Metadata/MetadataSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class MetadataSample : MonoBehaviour
private DelegateOnIceCandidate pc2OnIceCandidate;
private DelegateOnTrack pc2Ontrack;
private DelegateOnNegotiationNeeded pc1OnNegotiationNeeded;
private bool videoUpdateStarted;

private readonly object metadataInputLock = new object();
private readonly object metadataOutputLock = new object();
Expand Down Expand Up @@ -308,12 +307,6 @@ private void AddTracks()
{
SetUpSenderTransform(sender);
}

if (!videoUpdateStarted)
{
StartCoroutine(WebRTC.Update());
videoUpdateStarted = true;
}
}

private void RemoveTracks()
Expand Down
1 change: 0 additions & 1 deletion Samples~/MultiAudioReceive/MultiAudioReceiveSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class MultiAudioReceiveSample : MonoBehaviour

private void Awake()
{
StartCoroutine(WebRTC.Update());
callButton.onClick.AddListener(Call);
hangUpButton.onClick.AddListener(HangUp);
addAudioObjectButton.onClick.AddListener(AddVideoObject);
Expand Down
7 changes: 0 additions & 7 deletions Samples~/MultiVideoReceive/MultiVideoReceiveSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class MultiVideoReceiveSample : MonoBehaviour
private DelegateOnTrack pc2Ontrack;
private DelegateOnNegotiationNeeded pc1OnNegotiationNeeded;
private DelegateOnNegotiationNeeded pc2OnNegotiationNeeded;
private bool videoUpdateStarted;
private int objectIndex = 0;
private int videoIndex = 0;

Expand Down Expand Up @@ -194,12 +193,6 @@ private void Call()
_pc2.OnIceConnectionChange = pc2OnIceConnectionChange;
_pc2.OnTrack = pc2Ontrack;
_pc2.OnNegotiationNeeded = pc2OnNegotiationNeeded;

if (!videoUpdateStarted)
{
StartCoroutine(WebRTC.Update());
videoUpdateStarted = true;
}
}

private void AddTracks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class MultiplePeerConnectionsSample : MonoBehaviour

private void Start()
{
StartCoroutine(WebRTC.Update());

startButton.onClick.AddListener(Setup);
callButton.onClick.AddListener(Call);
hangUpButton.onClick.AddListener(HangUp);
Expand Down
2 changes: 0 additions & 2 deletions Samples~/MungeSDP/MungeSDPSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ private void Setup()

sourceVideoStream = cam.CaptureStream(WebRTCSettings.StreamSize.x, WebRTCSettings.StreamSize.y);
sourceImage.texture = cam.targetTexture;
updateCoroutine = StartCoroutine(WebRTC.Update());

receiveVideoStream = new MediaStream();
receiveVideoStream.OnAddTrack = e =>
{
Expand Down
7 changes: 0 additions & 7 deletions Samples~/PeerConnection/PeerConnectionSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ enum ProtocolOption
private DelegateOnIceCandidate pc2OnIceCandidate;
private DelegateOnTrack pc2Ontrack;
private DelegateOnNegotiationNeeded pc1OnNegotiationNeeded;
private bool videoUpdateStarted;

private void Awake()
{
Expand Down Expand Up @@ -211,12 +210,6 @@ private void AddTracks()
}
}
}

if (!videoUpdateStarted)
{
StartCoroutine(WebRTC.Update());
videoUpdateStarted = true;
}
}

private void RemoveTracks()
Expand Down
2 changes: 0 additions & 2 deletions Samples~/PerfectNegotiation/PerfectNegotiationSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ private void Start()
impolitePeer.SwapTransceivers();
politePeer.SwapTransceivers();
});

StartCoroutine(WebRTC.Update());
}

private void Update()
Expand Down
2 changes: 0 additions & 2 deletions Samples~/ReplaceTrack/ReplaceTrackSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ private void Start()
stopButton.gameObject.SetActive(false);
switchButton.onClick.AddListener(OnSwitchTrack);
switchButton.interactable = false;

StartCoroutine(WebRTC.Update());
}

private void OnStart()
Expand Down
7 changes: 0 additions & 7 deletions Samples~/Simulcast/SimulcastSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class SimulcastSample : MonoBehaviour
private DelegateOnIceCandidate pc2OnIceCandidate;
private DelegateOnTrack pc2Ontrack;
private DelegateOnNegotiationNeeded pc1OnNegotiationNeeded;
private bool videoUpdateStarted;

private ulong[] optionBitrate = new ulong[] { 100, 150, 200, 300, 400, 600, 800, 1100, 1400 };
private int[] optionScaleResolution = new[] { 1, 2, 4, 8 };
Expand Down Expand Up @@ -202,12 +201,6 @@ private void AddTracks()
transceiver.SetCodecPreferences(codecs);
}
_pc1Transceiver = transceiver;

if (!videoUpdateStarted)
{
StartCoroutine(WebRTC.Update());
videoUpdateStarted = true;
}
}

private void RemoveTracks()
Expand Down
1 change: 0 additions & 1 deletion Samples~/VideoReceive/VideoReceiveSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ private void Start()
}
};
pc1OnNegotiationNeeded = () => { StartCoroutine(PeerNegotiationNeeded(_pc1)); };
StartCoroutine(WebRTC.Update());
}

private void Update()
Expand Down
6 changes: 0 additions & 6 deletions Tests/Runtime/MediaStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ public IEnumerator SenderGetStats()
var test = new MonoBehaviourTest<SignalingPeers>();
test.component.AddStream(0, videoStream);
yield return test;
test.component.CoroutineUpdate();
yield return new WaitForSeconds(0.1f);
var op = test.component.GetSenderStats(0, 0);
yield return op;
Expand Down Expand Up @@ -225,7 +224,6 @@ public IEnumerator ReceiverGetStats()
var test = new MonoBehaviourTest<SignalingPeers>();
test.component.AddStream(0, videoStream);
yield return test;
test.component.CoroutineUpdate();
yield return new WaitForSeconds(0.1f);
var op = test.component.GetReceiverStats(1, 0);
yield return op;
Expand Down Expand Up @@ -268,7 +266,6 @@ public IEnumerator SetParametersReturnNoError()
var test = new MonoBehaviourTest<SignalingPeers>();
test.component.AddStream(0, videoStream);
yield return test;
test.component.CoroutineUpdate();
yield return new WaitForSeconds(0.1f);

var senders = test.component.GetPeerSenders(0);
Expand Down Expand Up @@ -311,7 +308,6 @@ public IEnumerator SetParametersReturnErrorIfInvalidTextureResolution()
var test = new MonoBehaviourTest<SignalingPeers>();
test.component.AddStream(0, videoStream);
yield return test;
test.component.CoroutineUpdate();
yield return new WaitForSeconds(0.1f);

var senders = test.component.GetPeerSenders(0);
Expand Down Expand Up @@ -360,7 +356,6 @@ public IEnumerator AddAndRemoveTrack()
var test = new MonoBehaviourTest<SignalingPeers>();
test.component.AddStream(0, stream1);
yield return test;
test.component.CoroutineUpdate();
yield return new WaitUntil(() => test.component.NegotiationCompleted());

bool calledOnAddTrack = false;
Expand Down Expand Up @@ -398,7 +393,6 @@ public IEnumerator OnAddTrackDelegatesWithEvent()
var test = new MonoBehaviourTest<SignalingPeers>();
test.component.AddStream(0, videoStream);
yield return test;
test.component.CoroutineUpdate();
yield return new WaitForSeconds(0.1f);

bool isCalledOnAddTrack = false;
Expand Down
1 change: 0 additions & 1 deletion Tests/Runtime/PeerConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,6 @@ public IEnumerator GetStatsReturnsReport()
test.component.AddTransceiver(0, track1);
test.component.AddTransceiver(1, track2);
yield return test;
test.component.CoroutineUpdate();

var op1 = test.component.GetSenderStats(0, 0);
var op2 = test.component.GetReceiverStats(0, 0);
Expand Down
6 changes: 0 additions & 6 deletions Tests/Runtime/SignalingPeers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ public IEnumerable<RTCRtpTransceiver> GetPeerTransceivers(int indexPeer)
return peers[indexPeer].GetTransceivers();
}


public Coroutine CoroutineUpdate()
{
return StartCoroutine(WebRTC.Update());
}

void Awake()
{
RTCConfiguration config = default;
Expand Down
Loading