Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Question Unity WebRTC with async/await

Discussion in 'Unity Render Streaming' started by patrickk2, Oct 30, 2020.

  1. patrickk2

    patrickk2

    Joined:
    Dec 8, 2019
    Posts:
    92
    Hello everyone!

    I hope this is the right subforum for this question (the com.unity.webrtc Github repository links here).

    In my project I would like to use Unity.WebRTC but with async/await instead of starting Coroutines. There is an asset in the Assetstore offering GetAwaiter extensions for a variety of IEnumerator instances.

    Unfortunatelly when I try `var answer = await RTCPeerConnection.CreateAnswer()` this results in a `null` value. Do I need a special Awaiter for the resulting `RTCSessionDescriptionAsyncOperation`? (I see for example that the `AsyncOperationBase` has a `keepWaiting` property, but I am not sure how a GetAwaiter extension should use this.)

    Any tipps or hints in the right direction would be greatly appreciated!

    Thanks in advance for any insights on this issue and best regards,
    Patrick
     
  2. kazuki_unity729

    kazuki_unity729

    Unity Technologies

    Joined:
    Aug 2, 2018
    Posts:
    803
    What kind of assets in the AssetStore are you using?
     
  3. patrickk2

    patrickk2

    Joined:
    Dec 8, 2019
    Posts:
    92
  4. EirikWahl

    EirikWahl

    Joined:
    Apr 7, 2018
    Posts:
    21
    The error is not related to WebRTC. Instead of `var answer = await RTCPeerConnection.CreateAnswer()` you need to save the original returned RTCSessionDescriptionAsyncOperation, to be able to get the return value after awaiting. The helper library you are using does not support return values the way you tried.

    Example:
    Code (CSharp):
    1. var op = pc.CreateAnswer(ref options);
    2. await op;
    3. if (op.IsError)
    4. {
    5.     throw new Exception($"Error when creating answer: {op.Error}");
    6. }
    7. Debug.Log($"Return value is: {op.Desc}");
    8.  
     
    kazuki_unity729 likes this.
  5. patrickk2

    patrickk2

    Joined:
    Dec 8, 2019
    Posts:
    92
    Hello EirikWahl!

    Thank you so much for your answer! I did not realize that this made a difference here. I will definitely try that.

    Thanks again and best regards,
    Patrick