0.2.0-dev Download ZIP
210 lines ·12.1 KB Raw
| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="WebRTCPeerConnection" inherits="RefCounted" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd"> |
| 3 | <brief_description> |
| 4 | Interface to a WebRTC peer connection. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | A WebRTC connection between the local computer and a remote peer. Provides an interface to connect, maintain, and monitor the connection. |
| 8 | Setting up a WebRTC connection between two peers may not seem a trivial task, but it can be broken down into 3 main steps: |
| 9 | - The peer that wants to initiate the connection ([code]A[/code] from now on) creates an offer and sends it to the other peer ([code]B[/code] from now on). |
| 10 | - [code]B[/code] receives the offer, generates an answer, and sends it to [code]A[/code]. |
| 11 | - [code]A[/code] and [code]B[/code] then generate and exchange ICE candidates with each other. |
| 12 | After these steps, the connection should be established. Refer to the linked tutorials for details. |
| 13 | </description> |
| 14 | <tutorials> |
| 15 | <link title="WebRTC documentation">$DOCS_URL/tutorials/networking/webrtc.html</link> |
| 16 | <link title="High-level multiplayer">$DOCS_URL/tutorials/networking/high_level_multiplayer.html</link> |
| 17 | </tutorials> |
| 18 | <methods> |
| 19 | <method name="add_ice_candidate"> |
| 20 | <return type="int" enum="Error" /> |
| 21 | <param index="0" name="media" type="String" /> |
| 22 | <param index="1" name="index" type="int" /> |
| 23 | <param index="2" name="name" type="String" /> |
| 24 | <description> |
| 25 | Add an ice candidate generated by a remote peer (and received over the signaling server). See [signal ice_candidate_created]. |
| 26 | </description> |
| 27 | </method> |
| 28 | <method name="close"> |
| 29 | <return type="void" /> |
| 30 | <description> |
| 31 | Close the peer connection and all data channels associated with it. |
| 32 | [b]Note:[/b] You cannot reuse this object for a new connection unless you call [method initialize]. |
| 33 | </description> |
| 34 | </method> |
| 35 | <method name="create_data_channel"> |
| 36 | <return type="WebRTCDataChannel" /> |
| 37 | <param index="0" name="label" type="String" /> |
| 38 | <param index="1" name="options" type="Dictionary" default="{}" /> |
| 39 | <description> |
| 40 | Returns a new [WebRTCDataChannel] (or [code]null[/code] on failure) with given [param label] and optionally configured via the [param options] dictionary. This method can only be called when the connection is in state [constant STATE_NEW]. |
| 41 | There are two ways to create a working data channel: either call [method create_data_channel] on only one of the peer and listen to [signal data_channel_received] on the other, or call [method create_data_channel] on both peers, with the same values, and the [code]"negotiated"[/code] option set to [code]true[/code]. |
| 42 | Valid [param options] are: |
| 43 | [codeblock] |
| 44 | { |
| 45 | "negotiated": true, # When set to true (default off), means the channel is negotiated out of band. "id" must be set too. "data_channel_received" will not be called. |
| 46 | "id": 1, # When "negotiated" is true this value must also be set to the same value on both peer. |
| 47 | |
| 48 | # Only one of maxRetransmits and maxPacketLifeTime can be specified, not both. They make the channel unreliable (but also better at real time). |
| 49 | "maxRetransmits": 1, # Specify the maximum number of attempt the peer will make to retransmits packets if they are not acknowledged. |
| 50 | "maxPacketLifeTime": 100, # Specify the maximum amount of time before giving up retransmitions of unacknowledged packets (in milliseconds). |
| 51 | "ordered": true, # When in unreliable mode (i.e. either "maxRetransmits" or "maxPacketLifeTime" is set), "ordered" (true by default) specify if packet ordering is to be enforced. |
| 52 | |
| 53 | "protocol": "my-custom-protocol", # A custom sub-protocol string for this channel. |
| 54 | } |
| 55 | [/codeblock] |
| 56 | [b]Note:[/b] You must keep a reference to channels created this way, or it will be closed. |
| 57 | </description> |
| 58 | </method> |
| 59 | <method name="create_offer"> |
| 60 | <return type="int" enum="Error" /> |
| 61 | <description> |
| 62 | Creates a new SDP offer to start a WebRTC connection with a remote peer. At least one [WebRTCDataChannel] must have been created before calling this method. |
| 63 | If this functions returns [constant OK], [signal session_description_created] will be called when the session is ready to be sent. |
| 64 | </description> |
| 65 | </method> |
| 66 | <method name="get_connection_state" qualifiers="const"> |
| 67 | <return type="int" enum="WebRTCPeerConnection.ConnectionState" /> |
| 68 | <description> |
| 69 | Returns the connection state. |
| 70 | </description> |
| 71 | </method> |
| 72 | <method name="get_gathering_state" qualifiers="const"> |
| 73 | <return type="int" enum="WebRTCPeerConnection.GatheringState" /> |
| 74 | <description> |
| 75 | Returns the ICE [enum GatheringState] of the connection. This lets you detect, for example, when collection of ICE candidates has finished. |
| 76 | </description> |
| 77 | </method> |
| 78 | <method name="get_signaling_state" qualifiers="const"> |
| 79 | <return type="int" enum="WebRTCPeerConnection.SignalingState" /> |
| 80 | <description> |
| 81 | Returns the signaling state on the local end of the connection while connecting or reconnecting to another peer. |
| 82 | </description> |
| 83 | </method> |
| 84 | <method name="initialize"> |
| 85 | <return type="int" enum="Error" /> |
| 86 | <param index="0" name="configuration" type="Dictionary" default="{}" /> |
| 87 | <description> |
| 88 | Re-initialize this peer connection, closing any previously active connection, and going back to state [constant STATE_NEW]. A dictionary of [param configuration] options can be passed to configure the peer connection. |
| 89 | Valid [param configuration] options are: |
| 90 | [codeblock] |
| 91 | { |
| 92 | "iceServers": [ |
| 93 | { |
| 94 | "urls": [ "stun:stun.example.com:3478" ], # One or more STUN servers. |
| 95 | }, |
| 96 | { |
| 97 | "urls": [ "turn:turn.example.com:3478" ], # One or more TURN servers. |
| 98 | "username": "a_username", # Optional username for the TURN server. |
| 99 | "credential": "a_password", # Optional password for the TURN server. |
| 100 | } |
| 101 | ] |
| 102 | } |
| 103 | [/codeblock] |
| 104 | </description> |
| 105 | </method> |
| 106 | <method name="poll"> |
| 107 | <return type="int" enum="Error" /> |
| 108 | <description> |
| 109 | Call this method frequently (e.g. in [method Node._process] or [method Node._physics_process]) to properly receive signals. |
| 110 | </description> |
| 111 | </method> |
| 112 | <method name="set_default_extension" qualifiers="static"> |
| 113 | <return type="void" /> |
| 114 | <param index="0" name="extension_class" type="StringName" /> |
| 115 | <description> |
| 116 | Sets the [param extension_class] as the default [WebRTCPeerConnectionExtension] returned when creating a new [WebRTCPeerConnection]. |
| 117 | </description> |
| 118 | </method> |
| 119 | <method name="set_local_description"> |
| 120 | <return type="int" enum="Error" /> |
| 121 | <param index="0" name="type" type="String" /> |
| 122 | <param index="1" name="sdp" type="String" /> |
| 123 | <description> |
| 124 | Sets the SDP description of the local peer. This should be called in response to [signal session_description_created]. |
| 125 | After calling this function the peer will start emitting [signal ice_candidate_created] (unless an [enum Error] different from [constant OK] is returned). |
| 126 | </description> |
| 127 | </method> |
| 128 | <method name="set_remote_description"> |
| 129 | <return type="int" enum="Error" /> |
| 130 | <param index="0" name="type" type="String" /> |
| 131 | <param index="1" name="sdp" type="String" /> |
| 132 | <description> |
| 133 | Sets the SDP description of the remote peer. This should be called with the values generated by a remote peer and received over the signaling server. |
| 134 | If [param type] is [code]"offer"[/code] the peer will emit [signal session_description_created] with the appropriate answer. |
| 135 | If [param type] is [code]"answer"[/code] the peer will start emitting [signal ice_candidate_created]. |
| 136 | </description> |
| 137 | </method> |
| 138 | </methods> |
| 139 | <signals> |
| 140 | <signal name="data_channel_received"> |
| 141 | <param index="0" name="channel" type="WebRTCDataChannel" /> |
| 142 | <description> |
| 143 | Emitted when a new in-band channel is received, i.e. when the channel was created with [code]negotiated: false[/code] (default). |
| 144 | The object will be an instance of [WebRTCDataChannel]. You must keep a reference of it or it will be closed automatically. See [method create_data_channel]. |
| 145 | </description> |
| 146 | </signal> |
| 147 | <signal name="ice_candidate_created"> |
| 148 | <param index="0" name="media" type="String" /> |
| 149 | <param index="1" name="index" type="int" /> |
| 150 | <param index="2" name="name" type="String" /> |
| 151 | <description> |
| 152 | Emitted when a new ICE candidate has been created. The three parameters are meant to be passed to the remote peer over the signaling server. |
| 153 | </description> |
| 154 | </signal> |
| 155 | <signal name="session_description_created"> |
| 156 | <param index="0" name="type" type="String" /> |
| 157 | <param index="1" name="sdp" type="String" /> |
| 158 | <description> |
| 159 | Emitted after a successful call to [method create_offer] or [method set_remote_description] (when it generates an answer). The parameters are meant to be passed to [method set_local_description] on this object, and sent to the remote peer over the signaling server. |
| 160 | </description> |
| 161 | </signal> |
| 162 | </signals> |
| 163 | <constants> |
| 164 | <constant name="STATE_NEW" value="0" enum="ConnectionState"> |
| 165 | The connection is new, data channels and an offer can be created in this state. |
| 166 | </constant> |
| 167 | <constant name="STATE_CONNECTING" value="1" enum="ConnectionState"> |
| 168 | The peer is connecting, ICE is in progress, none of the transports has failed. |
| 169 | </constant> |
| 170 | <constant name="STATE_CONNECTED" value="2" enum="ConnectionState"> |
| 171 | The peer is connected, all ICE transports are connected. |
| 172 | </constant> |
| 173 | <constant name="STATE_DISCONNECTED" value="3" enum="ConnectionState"> |
| 174 | At least one ICE transport is disconnected. |
| 175 | </constant> |
| 176 | <constant name="STATE_FAILED" value="4" enum="ConnectionState"> |
| 177 | One or more of the ICE transports failed. |
| 178 | </constant> |
| 179 | <constant name="STATE_CLOSED" value="5" enum="ConnectionState"> |
| 180 | The peer connection is closed (after calling [method close] for example). |
| 181 | </constant> |
| 182 | <constant name="GATHERING_STATE_NEW" value="0" enum="GatheringState"> |
| 183 | The peer connection was just created and hasn't done any networking yet. |
| 184 | </constant> |
| 185 | <constant name="GATHERING_STATE_GATHERING" value="1" enum="GatheringState"> |
| 186 | The ICE agent is in the process of gathering candidates for the connection. |
| 187 | </constant> |
| 188 | <constant name="GATHERING_STATE_COMPLETE" value="2" enum="GatheringState"> |
| 189 | The ICE agent has finished gathering candidates. If something happens that requires collecting new candidates, such as a new interface being added or the addition of a new ICE server, the state will revert to gathering to gather those candidates. |
| 190 | </constant> |
| 191 | <constant name="SIGNALING_STATE_STABLE" value="0" enum="SignalingState"> |
| 192 | There is no ongoing exchange of offer and answer underway. This may mean that the [WebRTCPeerConnection] is new ([constant STATE_NEW]) or that negotiation is complete and a connection has been established ([constant STATE_CONNECTED]). |
| 193 | </constant> |
| 194 | <constant name="SIGNALING_STATE_HAVE_LOCAL_OFFER" value="1" enum="SignalingState"> |
| 195 | The local peer has called [method set_local_description], passing in SDP representing an offer (usually created by calling [method create_offer]), and the offer has been applied successfully. |
| 196 | </constant> |
| 197 | <constant name="SIGNALING_STATE_HAVE_REMOTE_OFFER" value="2" enum="SignalingState"> |
| 198 | The remote peer has created an offer and used the signaling server to deliver it to the local peer, which has set the offer as the remote description by calling [method set_remote_description]. |
| 199 | </constant> |
| 200 | <constant name="SIGNALING_STATE_HAVE_LOCAL_PRANSWER" value="3" enum="SignalingState"> |
| 201 | The offer sent by the remote peer has been applied and an answer has been created and applied by calling [method set_local_description]. This provisional answer describes the supported media formats and so forth, but may not have a complete set of ICE candidates included. Further candidates will be delivered separately later. |
| 202 | </constant> |
| 203 | <constant name="SIGNALING_STATE_HAVE_REMOTE_PRANSWER" value="4" enum="SignalingState"> |
| 204 | A provisional answer has been received and successfully applied in response to an offer previously sent and established by calling [method set_local_description]. |
| 205 | </constant> |
| 206 | <constant name="SIGNALING_STATE_CLOSED" value="5" enum="SignalingState"> |
| 207 | The [WebRTCPeerConnection] has been closed. |
| 208 | </constant> |
| 209 | </constants> |
| 210 | </class> |