2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Runtime.InteropServices;
20 using Tizen.Applications;
21 using Tizen.Internals;
22 using Tizen.Multimedia;
23 using Tizen.Multimedia.Remoting;
25 internal static partial class Interop
27 internal static partial class NativeWebRTC
29 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
30 internal delegate void ErrorOccurredCallback(IntPtr handle, WebRTCErrorCode error, WebRTCState state, IntPtr userData);
32 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
33 internal delegate void StateChangedCallback(IntPtr handle, WebRTCState previous, WebRTCState current, IntPtr userData);
35 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
36 internal delegate void IceGatheringStateChangedCallback(IntPtr handle, WebRTCIceGatheringState state, IntPtr userData);
38 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
39 internal delegate void SignalingStateChangedCallback(IntPtr handle, WebRTCSignalingState state, IntPtr userData);
41 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
42 internal delegate void PeerConnectionStateChangedCallback(IntPtr handle, WebRTCPeerConnectionState state, IntPtr userData);
44 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
45 internal delegate void IceConnectionStateChangedCallback(IntPtr handle, WebRTCIceConnectionState state, IntPtr userData);
47 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
48 internal delegate void MediaPacketBufferStatusCallback(uint sourceId, MediaPacketBufferStatus status, IntPtr userData);
50 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
51 internal delegate void NegotiationNeededCallback(IntPtr handle, IntPtr userData);
53 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
54 internal delegate void IceCandidateCallback(IntPtr handle, string candidate, IntPtr userData);
56 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
57 internal delegate void TrackAddedCallback(IntPtr handle, MediaType type, uint id, IntPtr userData);
59 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
60 internal delegate void FrameEncodedCallback(IntPtr handle, MediaType type, uint trackId, IntPtr packetHandle, IntPtr userData);
62 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
63 internal delegate bool SupportedMediaFormatCallback(int format, IntPtr userData);
65 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
66 internal delegate bool RetrieveTurnServerCallback(string server, IntPtr userData);
68 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
69 internal delegate bool RetrieveTransceiverCodecCallback(TransceiverCodec codec, IntPtr userData);
71 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
72 internal delegate void SdpCreatedCallback(IntPtr handle, string sdp, IntPtr userData);
74 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
75 internal delegate bool RetrieveStatsCallback(WebRTCStatisticsCategory category, IntPtr prop, IntPtr userData);
78 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_create")]
79 internal static extern WebRTCErrorCode Create(out WebRTCHandle handle);
81 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_destroy")]
82 internal static extern WebRTCErrorCode Destroy(IntPtr handle);
84 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_start")]
85 internal static extern WebRTCErrorCode Start(IntPtr handle);
87 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_stop")]
88 internal static extern WebRTCErrorCode Stop(IntPtr handle);
90 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_get_state")]
91 internal static extern WebRTCErrorCode GetState(IntPtr handle, out WebRTCState state);
93 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_get_ice_gathering_state")]
94 internal static extern WebRTCErrorCode GetIceGatheringState(IntPtr handle, out WebRTCIceGatheringState state);
96 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_get_signaling_state")]
97 internal static extern WebRTCErrorCode GetSignalingState(IntPtr handle, out WebRTCSignalingState state);
99 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_get_peer_connection_state")]
100 internal static extern WebRTCErrorCode GetPeerConnectionState(IntPtr handle, out WebRTCPeerConnectionState state);
102 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_get_ice_connection_state")]
103 internal static extern WebRTCErrorCode GetIceConnectionState(IntPtr handle, out WebRTCIceConnectionState state);
105 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_add_media_source")]
106 internal static extern WebRTCErrorCode AddMediaSource(IntPtr handle, MediaSourceType type, out uint sourceId);
108 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_add_media_source_internal")]
109 internal static extern WebRTCErrorCode AddCustomMediaSource(IntPtr handle, CustomMediaSourceType type, out uint sourceId);
111 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_remove_media_source")]
112 internal static extern WebRTCErrorCode RemoveMediaSource(IntPtr handle, uint sourceId);
114 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_packet_source_set_format")]
115 internal static extern WebRTCErrorCode SetMediaPacketSourceInfo(IntPtr handle, uint sourceId, IntPtr packet);
117 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_packet_source_push_packet")]
118 internal static extern WebRTCErrorCode PushMediaPacket(IntPtr handle, uint sourceId, IntPtr packet);
120 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_file_source_set_path")]
121 internal static extern WebRTCErrorCode SetFileSourcePath(IntPtr handle, uint sourceId, string path);
123 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_file_source_set_looping")]
124 internal static extern WebRTCErrorCode SetFileSourceLooping(IntPtr handle, uint sourceId, bool looping);
126 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_file_source_get_looping")]
127 internal static extern WebRTCErrorCode GetFileSourceLooping(IntPtr handle, uint sourceId, out bool looping);
129 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_get_transceiver_direction")]
130 internal static extern WebRTCErrorCode GetTransceiverDirection(IntPtr handle, uint sourceId, MediaType type, out TransceiverDirection mode);
132 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_set_transceiver_direction")]
133 internal static extern WebRTCErrorCode SetTransceiverDirection(IntPtr handle, uint sourceId, MediaType type, TransceiverDirection mode);
135 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_foreach_supported_transceiver_codec")]
136 internal static extern WebRTCErrorCode ForeachSupportedTransceiverCodec(IntPtr handle, MediaSourceType sourceType, MediaType mediaType,
137 RetrieveTransceiverCodecCallback callback, IntPtr userData = default);
139 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_set_transceiver_codec")]
140 internal static extern WebRTCErrorCode SetTransceiverCodec(IntPtr handle, uint sourceId, MediaType type, TransceiverCodec codec);
142 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_get_transceiver_codec")]
143 internal static extern WebRTCErrorCode GetTransceiverCodec(IntPtr handle, uint sourceId, MediaType type, out TransceiverCodec codec);
145 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_set_pause")]
146 internal static extern WebRTCErrorCode SetPause(IntPtr handle, uint sourceId, MediaType type, bool pause);
148 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_get_pause")]
149 internal static extern WebRTCErrorCode GetPause(IntPtr handle, uint sourceId, MediaType type, out bool isPaused);
151 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_set_mute")]
152 internal static extern WebRTCErrorCode SetMute(IntPtr handle, uint sourceId, MediaType type, bool mute);
154 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_get_mute")]
155 internal static extern WebRTCErrorCode GetMute(IntPtr handle, uint sourceId, MediaType type, out bool mute);
157 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_set_video_resolution")]
158 internal static extern WebRTCErrorCode SetVideoResolution(IntPtr handle, uint sourceId, int width, int height);
160 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_get_video_resolution")]
161 internal static extern WebRTCErrorCode GetVideoResolution(IntPtr handle, uint sourceId, out int width, out int height);
163 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_set_video_framerate")]
164 internal static extern WebRTCErrorCode SetVideoFrameRate(IntPtr handle, uint sourceId, int frameRate);
166 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_get_video_framerate")]
167 internal static extern WebRTCErrorCode GetVideoFrameRate(IntPtr handle, uint sourceId, out int frameRate);
169 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_set_audio_loopback")]
170 internal static extern WebRTCErrorCode SetAudioLoopback(IntPtr handle, uint sourceId, AudioStreamPolicyHandle streamInfo, out uint trackId);
172 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_set_video_loopback")]
173 internal static extern WebRTCErrorCode SetVideoLoopback(IntPtr handle, uint sourceId, WebRTCDisplayType type, IntPtr display, out uint trackId);
175 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_source_set_video_loopback_to_ecore_wl")]
176 internal static extern WebRTCErrorCode SetEcoreVideoLoopback(IntPtr handle, uint sourceId, IntPtr display, out uint trackId);
178 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_bundle_policy")]
179 internal static extern WebRTCErrorCode SetBundlePolicy(IntPtr handle, WebRTCBundlePolicy bundlePolicy);
181 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_get_bundle_policy")]
182 internal static extern WebRTCErrorCode GetBundlePolicy(IntPtr handle, out WebRTCBundlePolicy bundlePolicy);
184 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_mic_source_set_sound_stream_info")]
185 internal static extern WebRTCErrorCode SetAudioStreamPolicyToMicrophoneSource(IntPtr handle, uint sourceId, AudioStreamPolicyHandle streamInfo);
187 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_sound_stream_info")]
188 internal static extern WebRTCErrorCode SetAudioStreamPolicy(IntPtr handle, uint trackId, AudioStreamPolicyHandle streamInfo);
190 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_display")]
191 internal static extern WebRTCErrorCode SetDisplay(IntPtr handle, uint trackId, WebRTCDisplayType type, IntPtr display);
193 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_ecore_wl_display")]
194 internal static extern WebRTCErrorCode SetEcoreDisplay(IntPtr handle, uint trackId, IntPtr display);
196 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_display_mode")]
197 internal static extern WebRTCErrorCode SetDisplayMode(IntPtr handle, uint trackId, WebRTCDisplayMode mode);
199 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_get_display_mode")]
200 internal static extern WebRTCErrorCode GetDisplayMode(IntPtr handle, uint trackId, out WebRTCDisplayMode mode);
202 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_display_visible")]
203 internal static extern WebRTCErrorCode SetDisplayVisible(IntPtr handle, uint trackId, bool isVisible);
205 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_get_display_visible")]
206 internal static extern WebRTCErrorCode GetDisplayVisible(IntPtr handle, uint trackId, out bool isVisible);
208 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_get_stun_server")]
209 internal static extern WebRTCErrorCode GetStunServer(IntPtr handle, out string server);
211 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_stun_server")]
212 internal static extern WebRTCErrorCode SetStunServer(IntPtr handle, string server);
214 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_add_turn_server")]
215 internal static extern WebRTCErrorCode AddTurnServer(IntPtr handle, string server);
217 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_foreach_turn_server")]
218 internal static extern WebRTCErrorCode ForeachTurnServer(IntPtr handle, RetrieveTurnServerCallback callback, IntPtr userData = default);
220 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_ice_transport_policy")]
221 internal static extern WebRTCErrorCode SetIceTransportPolicy(IntPtr handle, IceTransportPolicy policy);
223 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_get_ice_transport_policy")]
224 internal static extern WebRTCErrorCode GetIceTransportPolicy(IntPtr handle, out IceTransportPolicy policy);
226 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_create_offer")]
227 internal static extern WebRTCErrorCode CreateSDPOffer(IntPtr handle, SafeBundleHandle bundle, out string offer);
229 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_create_offer_async")]
230 internal static extern WebRTCErrorCode CreateSDPOfferAsync(IntPtr handle, SafeBundleHandle bundle, SdpCreatedCallback callback, IntPtr userData);
232 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_create_answer")]
233 internal static extern WebRTCErrorCode CreateSDPAnswer(IntPtr handle, SafeBundleHandle bundle, out string offer);
235 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_create_answer_async")]
236 internal static extern WebRTCErrorCode CreateSDPAnswerAsync(IntPtr handle, SafeBundleHandle bundle, SdpCreatedCallback callback, IntPtr userData);
238 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_local_description")]
239 internal static extern WebRTCErrorCode SetLocalDescription(IntPtr handle, string description);
241 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_remote_description")]
242 internal static extern WebRTCErrorCode SetRemoteDescription(IntPtr handle, string description);
244 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_add_ice_candidate")]
245 internal static extern WebRTCErrorCode AddIceCandidate(IntPtr handle, string candidate);
247 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_foreach_media_source_supported_format")]
248 internal static extern WebRTCErrorCode SupportedMediaSourceFormat(IntPtr handle, SupportedMediaFormatCallback callback, IntPtr userData);
250 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_foreach_stats")]
251 internal static extern WebRTCErrorCode ForeachStats(IntPtr handle, int typeMask, RetrieveStatsCallback callback, IntPtr userData = default);
254 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_error_cb")]
255 internal static extern WebRTCErrorCode SetErrorOccurredCb(IntPtr handle, ErrorOccurredCallback callback, IntPtr userData = default);
257 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_unset_error_cb")]
258 internal static extern WebRTCErrorCode UnsetErrorOccurredCb(IntPtr handle);
260 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_state_changed_cb")]
261 internal static extern WebRTCErrorCode SetStateChangedCb(IntPtr handle, StateChangedCallback callback, IntPtr userData = default);
263 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_unset_state_changed_cb")]
264 internal static extern WebRTCErrorCode UnsetStateChangedCb(IntPtr handle);
266 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_ice_gathering_state_change_cb")]
267 internal static extern WebRTCErrorCode SetIceGatheringStateChangedCb(IntPtr handle, IceGatheringStateChangedCallback callback, IntPtr userData = default);
269 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_unset_ice_gathering_state_change_cb")]
270 internal static extern WebRTCErrorCode UnsetIceGatheringStateChangedCb(IntPtr handle);
272 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_signaling_state_change_cb")]
273 internal static extern WebRTCErrorCode SetSignalingStateChangedCb(IntPtr handle, SignalingStateChangedCallback callback, IntPtr userData = default);
275 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_unset_signaling_state_change_cb")]
276 internal static extern WebRTCErrorCode UnsetSignalingStateChangedCb(IntPtr handle);
278 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_peer_connection_state_change_cb")]
279 internal static extern WebRTCErrorCode SetPeerConnectionStateChangedCb(IntPtr handle, PeerConnectionStateChangedCallback callback, IntPtr userData = default);
281 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_unset_peer_connection_state_change_cb")]
282 internal static extern WebRTCErrorCode UnsetPeerConnectionStateChangedCb(IntPtr handle);
284 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_ice_connection_state_change_cb")]
285 internal static extern WebRTCErrorCode SetIceConnectionStateChangedCb(IntPtr handle, IceConnectionStateChangedCallback callback, IntPtr userData = default);
287 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_unset_ice_connection_state_change_cb")]
288 internal static extern WebRTCErrorCode UnsetIceConnectionStateChangedCb(IntPtr handle);
290 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_packet_source_set_buffer_state_changed_cb")]
291 internal static extern WebRTCErrorCode SetBufferStateChangedCb(IntPtr handle, uint sourceId, MediaPacketBufferStatusCallback callback, IntPtr userData = default);
293 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_media_packet_source_unset_buffer_state_changed_cb")]
294 internal static extern WebRTCErrorCode UnsetBufferStateChangedCb(IntPtr handle, uint sourceId);
296 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_negotiation_needed_cb")]
297 internal static extern WebRTCErrorCode SetNegotiationNeededCb(IntPtr handle, NegotiationNeededCallback callback, IntPtr userData = default);
299 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_unset_negotiation_needed_cb")]
300 internal static extern WebRTCErrorCode UnsetNegotiationNeededCb(IntPtr handle);
302 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_ice_candidate_cb")]
303 internal static extern WebRTCErrorCode SetIceCandidateCb(IntPtr handle, IceCandidateCallback callback, IntPtr userData = default);
305 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_unset_ice_candidate_cb")]
306 internal static extern WebRTCErrorCode UnsetIceCandidateCb(IntPtr handle);
308 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_track_added_cb")]
309 internal static extern WebRTCErrorCode SetTrackAddedCb(IntPtr handle, TrackAddedCallback callback, IntPtr userData = default);
311 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_unset_track_added_cb")]
312 internal static extern WebRTCErrorCode UnsetTrackAddedCb(IntPtr handle);
314 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_encoded_audio_frame_cb")]
315 internal static extern WebRTCErrorCode SetAudioFrameEncodedCb(IntPtr handle, FrameEncodedCallback callback, IntPtr userData = default);
317 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_unset_encoded_audio_frame_cb")]
318 internal static extern WebRTCErrorCode UnsetAudioFrameEncodedCb(IntPtr handle);
320 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_set_encoded_video_frame_cb")]
321 internal static extern WebRTCErrorCode SetVideoFrameEncodedCb(IntPtr handle, FrameEncodedCallback callback, IntPtr userData = default);
323 [DllImport(Libraries.WebRTC, EntryPoint = "webrtc_unset_encoded_video_frame_cb")]
324 internal static extern WebRTCErrorCode UnsetVideoFrameEncodedCb(IntPtr handle);
326 [StructLayout(LayoutKind.Explicit)]
327 internal struct StatsPropertyValueStruct
329 [FieldOffsetAttribute(0)]
331 [FieldOffsetAttribute(0)]
333 [FieldOffsetAttribute(0)]
335 [FieldOffsetAttribute(0)]
337 [FieldOffsetAttribute(0)]
338 internal ulong @ulong;
339 [FieldOffsetAttribute(0)]
340 internal float @float;
341 [FieldOffsetAttribute(0)]
342 internal double @double;
343 [FieldOffsetAttribute(0)]
344 internal IntPtr @string;
347 [NativeStruct("webrtc_stats_prop_info_s", Include="webrtc.h", PkgConfig="capi-media-webrtc")]
348 [StructLayout(LayoutKind.Sequential)]
349 internal struct StatsPropertyStruct
351 internal string name;
352 internal WebRTCStatisticsProperty property;
353 internal WebRTCStatsPropertyType propertyType;
354 internal StatsPropertyValueStruct value;
358 internal class WebRTCHandle : SafeHandle
360 protected WebRTCHandle() : base(IntPtr.Zero, true)
364 public override bool IsInvalid => handle == IntPtr.Zero;
366 protected override bool ReleaseHandle()
368 var ret = NativeWebRTC.Destroy(handle);
369 if (ret != WebRTCErrorCode.None)
371 Log.Debug(GetType().FullName, $"Failed to release native {GetType().Name}");