From 408a25358ca9b1ea63e57bf4391b4fcbf07fe6e2 Mon Sep 17 00:00:00 2001 From: Haesu Gwon Date: Tue, 7 Sep 2021 15:19:21 +0900 Subject: [PATCH] [WebRTC] Add CreateOffer/AnswerAsync method (#3506) --- .../Interop/Interop.WebRTC.cs | 9 +++ src/Tizen.Multimedia.Remoting/WebRTC/WebRTC.cs | 68 +++++++++++++++------- 2 files changed, 56 insertions(+), 21 deletions(-) diff --git a/src/Tizen.Multimedia.Remoting/Interop/Interop.WebRTC.cs b/src/Tizen.Multimedia.Remoting/Interop/Interop.WebRTC.cs index d6bac08..3fdc1ce 100755 --- a/src/Tizen.Multimedia.Remoting/Interop/Interop.WebRTC.cs +++ b/src/Tizen.Multimedia.Remoting/Interop/Interop.WebRTC.cs @@ -64,6 +64,9 @@ internal static partial class Interop [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate bool RetrieveTurnServerCallback(string server, IntPtr userData); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SdpCreatedCallback(IntPtr handle, string sdp, IntPtr userData); + [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_create")] internal static extern WebRTCErrorCode Create(out WebRTCHandle handle); @@ -188,9 +191,15 @@ internal static partial class Interop [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_create_offer")] internal static extern WebRTCErrorCode CreateSDPOffer(IntPtr handle, SafeBundleHandle bundle, out string offer); + [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_create_offer_async")] + internal static extern WebRTCErrorCode CreateSDPOfferAsync(IntPtr handle, SafeBundleHandle bundle, SdpCreatedCallback callback, IntPtr userData); + [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_create_answer")] internal static extern WebRTCErrorCode CreateSDPAnswer(IntPtr handle, SafeBundleHandle bundle, out string offer); + [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_create_answer_async")] + internal static extern WebRTCErrorCode CreateSDPAnswerAsync(IntPtr handle, SafeBundleHandle bundle, SdpCreatedCallback callback, IntPtr userData); + [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_local_description")] internal static extern WebRTCErrorCode SetLocalDescription(IntPtr handle, string description); diff --git a/src/Tizen.Multimedia.Remoting/WebRTC/WebRTC.cs b/src/Tizen.Multimedia.Remoting/WebRTC/WebRTC.cs index a6a5815..b4fd253 100755 --- a/src/Tizen.Multimedia.Remoting/WebRTC/WebRTC.cs +++ b/src/Tizen.Multimedia.Remoting/WebRTC/WebRTC.cs @@ -237,28 +237,43 @@ namespace Tizen.Multimedia.Remoting /// The SDP offer. /// The WebRTC is not in the valid state. /// The WebRTC has already been disposed. - /// + /// /// 9 - public string CreateOffer() => CreateOffer(null); + public string CreateOffer() + { + ValidateWebRTCState(WebRTCState.Negotiating); + + NativeWebRTC.CreateSDPOffer(Handle, new SafeBundleHandle(), out string offer). + ThrowIfFailed("Failed to create offer"); + + return offer; + } /// - /// Creates SDP offer with option to start a new WebRTC connection to a remote peer. + /// Creates SDP offer asynchronously to start a new WebRTC connection to a remote peer. /// /// The WebRTC must be in the - /// Configuration options for the offer. /// The SDP offer. /// The WebRTC is not in the valid state. /// The WebRTC has already been disposed. /// /// 9 - public string CreateOffer(Bundle bundle) + public async Task CreateOfferAsync() { ValidateWebRTCState(WebRTCState.Negotiating); - var bundle_ = bundle?.SafeBundleHandle ?? new SafeBundleHandle(); + var tcsSdpCreated = new TaskCompletionSource(); - NativeWebRTC.CreateSDPOffer(Handle, bundle_, out string offer). - ThrowIfFailed("Failed to create offer"); + NativeWebRTC.SdpCreatedCallback cb = (handle, sdp, _) => + { + tcsSdpCreated.TrySetResult(sdp); + }; + + NativeWebRTC.CreateSDPOfferAsync(Handle, new SafeBundleHandle(), cb, IntPtr.Zero). + ThrowIfFailed("Failed to create offer asynchronously"); + + var offer = await tcsSdpCreated.Task.ConfigureAwait(false); + await Task.Yield(); return offer; } @@ -273,33 +288,44 @@ namespace Tizen.Multimedia.Remoting /// The SDP answer. /// The WebRTC is not in the valid state. /// The WebRTC has already been disposed. - /// + /// /// /// 9 - public string CreateAnswer() => CreateAnswer(null); + public string CreateAnswer() + { + ValidateWebRTCState(WebRTCState.Negotiating); + + NativeWebRTC.CreateSDPAnswer(Handle, new SafeBundleHandle(), out string answer). + ThrowIfFailed("Failed to create answer"); + + return answer; + } /// - /// Creates SDP answer with option to an offer received from a remote peer. + /// Creates SDP answer asynchronously with option to an offer received from a remote peer. /// - /// - /// The WebRTC must be in the .
- /// The SDP offer must be set by before creating answer. - ///
- /// Configuration options for the answer. + /// The WebRTC must be in the /// The SDP answer. /// The WebRTC is not in the valid state. /// The WebRTC has already been disposed. /// - /// /// 9 - public string CreateAnswer(Bundle bundle) + public async Task CreateAnswerAsync() { ValidateWebRTCState(WebRTCState.Negotiating); - var bundle_ = bundle?.SafeBundleHandle ?? new SafeBundleHandle(); + var tcsSdpCreated = new TaskCompletionSource(); - NativeWebRTC.CreateSDPAnswer(Handle, bundle_, out string answer). - ThrowIfFailed("Failed to create answer"); + NativeWebRTC.SdpCreatedCallback cb = (handle, sdp, _) => + { + tcsSdpCreated.TrySetResult(sdp); + }; + + NativeWebRTC.CreateSDPAnswerAsync(Handle, new SafeBundleHandle(), cb, IntPtr.Zero). + ThrowIfFailed("Failed to create answer asynchronously"); + + var answer = await tcsSdpCreated.Task.ConfigureAwait(false); + await Task.Yield(); return answer; } -- 2.7.4