WebRTC ã§ã¯ãã¾ãã¾ãªãããã³ã«ãç¸äºä½ç¨ãã¦ãã¢ã¼éã®æ¥ç¶ã確ç«ãããã¼ã¿ãã¡ãã£ã¢ã®è»¢éãè¡ãã¾ããããã®è¨äºã§ã¯ãã®ä»çµã¿ã解説ãã¾ãã
ã¡ã¢: ãã®ãã¼ã¸ã¯ãæ§é çãªå®å ¨æ§ã¨å 容ã®å®å ¨æ§ã®ããã«ãå¤§å¹ ãªæ¸ãæããå¿ è¦ã§ããå¤ãã®æ å ±ãããã®ã¯è¯ããã¨ã§ãããããã¯ç¾å¨ã´ãæ¨ã¦å ´ã®ãããªãã®ãªã®ã§ãæ§æã¯ãã¡ããã¡ãã§ãã
ã·ã°ããªã³ã°æ®å¿µãªãã¨ã«ãWebRTC ã¯ä¸éã«ä½ããã®ãµã¼ãã¼ããªããã°æ¥ç¶ã使ã§ãã¾ããããã®ãµã¼ãã¼ãã·ã°ãã«ãã£ã³ãã«ãã¾ãã¯ã·ã°ããªã³ã°ãµã¼ãã¹ã¨å¼ã³ã¾ããæ¥ç¶ã確ç«ããåã«æ å ±ã交æããä¼éææ®µã¯ã©ããªãã®ã§ãæ§ãã¾ãããEã¡ã¼ã«ãã¯ããã伿¸é³©ã§ã...決ããã®ã¯ããªãã§ãã
交æããå¿ è¦ã®ããæ å ±ã¯ãªãã¡ã¼ã¨ã¢ã³ãµã¼ã¨å¼ã°ãããã®ä¸èº«ã¯ä¸è¨ã§èª¬æãã SDP ã§ãã
ãã¢ã¼ A ãæ¥ç¶ãåæåããå´ã¨ããã¨ããã¢ã¼ A ããªãã¡ã¼ã使ãã¾ãããããã鏿ãããã·ã°ãã«ãã£ã³ãã«ã使ã£ã¦ãã¢ã¼ B ã«ãªãã¡ã¼ãéãã¾ãããã¢ã¼ B ã¯ã·ã°ãã«ãã£ã³ãã«ãããªãã¡ã¼ãåãåãã¨ãã¢ã³ãµã¼ã使ãã¾ãããããããã¢ã¼ B ã¯ãã¢ã¼ A ã«ã·ã°ãã«ãã£ã³ãã«ã使ã£ã¦ã¢ã³ãµã¼ãéãè¿ãã¾ãã
ã»ãã·ã§ã³ãã£ã¹ã¯ãªãã·ã§ã³WebRTC æ¥ç¶ã®ã¨ã³ããã¤ã³ãè¨å®ã¯ã»ãã·ã§ã³ãã£ã¹ã¯ãªãã·ã§ã³ã¨å¼ã°ãã¾ããããã«å«ã¾ããæ å ±ã¯ãéãããã¡ãã£ã¢ã®ç¨®é¡ãå½¢å¼ã使ç¨ããã転éãããã³ã«ãã¨ã³ããã¤ã³ãã® IP ã¢ãã¬ã¹ã¨ãã¼ããã¾ããã®ä»ã¡ãã£ã¢è»¢éã¨ã³ããã¤ã³ããè¨è¿°ããã®ã«å¿ è¦ãªæ å ±ã§ãããã®æ å ±ã ã»ãã·ã§ã³ãã£ã¹ã¯ãªãã·ã§ã³ãããã³ã« (SDP) ã使ã£ã¦äº¤æããä¿åãã¾ãã SDP ãã¼ã¿å½¢å¼ã®è©³ç´°ã¯ RFC 2327 ã«ããã¾ãã
ã¦ã¼ã¶ã¼ã WebRTC ã³ã¼ã«ãä»ã®ã¦ã¼ã¶ã¼ã«éå§ããã¨ãããªãã¡ã¼ã¨å¼ã°ããç¹å¥ãªè¨è¿°ã使ãã¾ããã³ã¼ã«ããå´ãã³ã¼ã«ã«å¿ è¦ãªè¨å®ãææ¡ãããã®ãã¹ã¦ã®æ å ±ããªãã¡ã¼ã®è¨è¿°ã«çãè¾¼ã¿ã¾ããåãåãå´ã¯ã¢ã³ãµã¼ãè¿ãã¾ããã¢ã³ãµã¼ã¯åãåãå´ãç¨æããè¨è¿°ã§ãããã®ããã«ãã¦ã両ããã¤ã¹ããäºãã«ã¡ãã£ã¢ãã¼ã¿ã®äº¤æã«å¿ è¦ãªæ å ±ãå ±æãã¾ãããã®äº¤æã¯ Interactive Connectivity Establishment (ICE) ã使ã£ã¦è¡ããã¾ããICE ã¨ã¯äºã¤ã®ããã¤ã¹ã Network Address Translation (NAT) ã«ãã£ã¦éã¦ããã¦ãã¦ããªãã¡ã¼ã¨ã¢ã³ãµã¼ã交æããããã«åªä»ãå©ç¨ã§ããããã«ãããããã³ã«ã§ãã
åãã¢ã¼ã¯ 2 ã¤ã®è¨è¿°ãæã«å ¥ãã¾ãã local description ãèªåå´ã®è¨è¿°ã§ã remote description ãç¸æå´ã®è¨è¿°ã§ãã
ãªãã¡ã¼ï¼ã¢ã³ãµã¼ã®äº¤æã¯ã³ã¼ã«ãæåã«ç¢ºç«ããéã«å®è¡ããã¾ãããããã ãã§ãªããã©ã¼ããããä»ã®è¨å®ã«å¤æ´ãå¿ è¦ãªã¨ãã«ãéæå®è¡ããã¾ããã³ã¼ã«ã®æ°è¦ä½ææã§ãæ¢åã®è¨å®å¤æ´æã§ãããããã«ãã¦ããªãã¡ã¼ã¨ã¢ã³ãµã¼ã交æããããã«ä»¥ä¸ã®ãããªåºæ¬çãªã¹ããããå®è¡ããã¾ãããªããããã§ã¯ ICE ã¬ã¤ã¤ã¼ã¯é¤å¤ãã¦ãã¾ãã
navigator.mediaDevices.getUserMedia()
ãéãã¦ãã¼ã«ã«ã¡ãã£ã¢ãåå¾ããRTCPeerConnection
ã使ããRTCPeerConnection.addTrack()
ãå®è¡ããã(addStream
ã鿍奍ã§ãããã)RTCPeerConnection.createOffer()
ãå®è¡ããRTCPeerConnection.setLocalDescription()
ãå®è¡ããRTCPeerConnection.setRemoteDescription()
ãå®è¡ããRTCPeerConnection.addTrack()
ãéãã¦ã¡ãã£ã¢ãã©ãã¯ããã¢ã¼æ¥ç¶ã«ã¢ã¿ããããRTCPeerConnection.createAnswer()
ãå®è¡ãããã¨ã§ã¢ã³ãµã¼ã使ããRTCPeerConnection.setLocalDescription()
ã«ä½æããã¢ã³ãµã¼ã渡ãã¦å®è¡ããã¢ã³ãµã¼ãèªèº«ã® local description ã¨ãã¦ã»ããããããã®æç¹ã§åãåãå´ã¯ä¸¡å´ã®æ¥ç¶è¨å®ãç¥ããã¨ã«ãªããRTCPeerConnection.setRemoteDescription()
ãå®è¡ãããããã§å¼ã³åºãå´ã両è
ã®è¨å®ãç¥ããã¨ã«ãªããè¨å®ããéãã«ã¡ãã£ã¢ãæµãå§ãããTaking one step deeper into the process, we find that localDescription
and remoteDescription
, the properties which return these two descriptions, aren't as simple as they look. Because during renegotiation, an offer might be rejected because it proposes an incompatible format, it's necessary that each endpoint have the ability to propose a new format but not actually switch to it until it's accepted by the other peer. For that reason, WebRTC uses pending and current descriptions.
The current description (which is returned by the RTCPeerConnection.currentLocalDescription
and RTCPeerConnection.currentRemoteDescription
properties) represents the description currently in actual use by the connection. This is the most recent connection that both sides have fully agreed to use.
The pending description (returned by RTCPeerConnection.pendingLocalDescription
and RTCPeerConnection.pendingRemoteDescription
) indicates a description which is currently under consideration following a call to setLocalDescription()
or setRemoteDescription()
, respectively.
When reading the description (returned by RTCPeerConnection.localDescription
and RTCPeerConnection.remoteDescription
), the returned value is the value of pendingLocalDescription
/pendingRemoteDescription
if there's a pending description (that is, the pending description isn't null
); otherwise, the current description (currentLocalDescription
/currentRemoteDescription
) is returned.
When changing the description by calling setLocalDescription()
or setRemoteDescription()
, the specified description is set as the pending description, and the WebRTC layer begins to evaluate whether or not it's acceptable. Once the proposed description has been agreed upon, the value of currentLocalDescription
or currentRemoteDescription
is changed to the pending description, and the pending description is set to null again, indicating that there isn't a pending description.
ã¡ã¢: The pendingLocalDescription
contains not just the offer or answer under consideration, but any local ICE candidates which have already been gathered since the offer or answer was created. Similarly, pendingRemoteDescription
includes any remote ICE candidates which have been provided by calls to RTCPeerConnection.addIceCandidate()
.
See the individual articles on these properties and methods for more specifics, and Codecs used by WebRTC for information about codecs supported by WebRTC and which are compatible with which browsers. The codecs guide also offers guidance to help you choose the best codecs for your needs.
ICE candidatesAs well as exchanging information about the media (discussed above in Offer/Answer and SDP), peers must exchange information about the network connection. This is known as an ICE candidate and details the available methods the peer is able to communicate (directly or through a TURN server). Typically, each peer will propose its best candidates first, making their way down the line toward their worse candidates. Ideally, candidates are UDP (since it's faster, and media streams are able to recover from interruptions relatively easily), but the ICE standard does allow TCP candidates as well.
ã¡ã¢: Generally, ICE candidates using TCP are only going to be used when UDP is not available or is restricted in ways that make it not suitable for media streaming. Not all browsers support ICE over TCP, however.
ICE allows candidates to represent connections over either TCP or UDP, with UDP generally being preferred (and being more widely supported). Each protocol supports a few types of candidate, with the candidate types defining how the data makes its way from peer to peer.
UDP candidate typesUDP candidates (candidates with their protocol
set to udp
) can be one of these types:
host
A host candidate is one for which its ip
address is the actual, direct IP address of the remote peer.
prflx
A peer reflexive candidate is one whose IP address comes from a symmetric NAT between the two peers, usually as an additional candidate during trickle ICE (that is, additional candidate exchanges that occur after primary signaling but before the connection verification phase is finished).
srflx
A server reflexive candidate is generated by a STUN/TURN server; the connection's initiator requests a candidate from the STUN server, which forwards the request through the remote peer's NAT, which creates and returns a candidate whose IP address is local to the remote peer. The STUN server then replies to the initiator's request with a candidate whose IP address is unrelated to the remote peer.
relay
A relay candidate is generated just like a server reflexive candidate ("srflx"
), but using TURN instead of STUN.
TCP candidates (that is, candidates whose protocol
is tcp
) can be of these types:
active
The transport will try to open an outbound connection but won't receive incoming connection requests. This is the most common type, and the only one that most user agents will gather.
passive
The transport will receive incoming connection attempts but won't attempt a connection itself.
so
The transport will try to simultaneously open a connection with its peer.
The ICE layer selects one of the two peers to serve as the controlling agent. This is the ICE agent which will make the final decision as to which candidate pair to use for the connection. The other peer is called the controlled agent. You can identify which one your end of the connection is by examining the value of RTCIceCandidate.transport.role
, although in general it doesn't matter which is which.
The controlling agent not only takes responsibility for making the final decision as to which candidate pair to use, but also for signaling that selection to the controlled agent by using STUN and an updated offer, if necessary. The controlled agent just waits to be told which candidate pair to use.
It's important to keep in mind that a single ICE session may result in the controlling agent choosing more than one candidate pair. Each time it does so and shares that information with the controlled agent, the two peers reconfigure their connection to use the new configuration described by the new candidate pair.
Once the ICE session is complete, the configuration that's currently in effect is the final one, unless an ICE reset occurs.
At the end of each generation of candidates, an end-of-candidates notification is sent in the form of an RTCIceCandidate
whose candidate
property is an empty string. This candidate should still be added to the connection using addIceCandidate()
method, as usual, in order to deliver that notification to the remote peer.
When there are no more candidates at all to be expected during the current negotiation exchange, an end-of-candidates notification is sent by delivering a RTCIceCandidate
whose candidate
property is null
. This message does not need to be sent to the remote peer. It's a legacy notification of a state which can be detected instead by watching for the iceGatheringState
to change to complete
, by watching for the icegatheringstatechange
event.
During negotiation, there will be times when things just don't work out. For example, when renegotiating a connectionâfor example, to adapt to changing hardware or network configurationsâit's possible that negotiation could reach a dead end, or some form of error might occur that prevents negotiation at all. There may be permissions issues or other problems as well, for that matter.
ICE rollbacksWhen renegotiating a connection that's already active and a situation arises in which the negotiation fails, you don't really want to kill the already-running call. After all, you were most likely just trying to upgrade or downgrade the connection, or to otherwise make adaptations to an ongoing session. Aborting the call would be an excessive reaction in that situation.
Instead, you can initiate an ICE rollback. A rollback restores the SDP offer (and the connection configuration by extension) to the configuration it had the last time the connection's signalingState
was stable
.
To programmatically initiate a rollback, send a description whose type
is rollback
. Any other properties in the description object are ignored.
In addition, the ICE agent will automatically initiate a rollback when a peer that had previously created an offer receives an offer from the remote peer. In other words, if the local peer is in the state have-local-offer
, indicating that the local peer had previously sent an offer, calling setRemoteDescription()
with a received offer triggers rollback so that the negotiation switches from the remote peer being the caller to the local peer being the caller.
For now, see ICE restart.
The entire exchange in a complicated diagramRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4