webrtc を使い始める: 14. pion webrtc のデータ転送プロトコルである SCTP

近年、データ伝送の過程において、伝送セキュリティメカニズムの要素が引き続き強調されています。webrtcデータ伝送を確実にする場合は、それが採用されておりDTLS 协议、具体的なプロトコル方法は関連文書で見ることができます。

DTLS (Datagram Transport Layer Security) は、UDP シナリオでデータ パケットが失われたり並べ替えられたりする可能性があるという現実に基づいて、UDP 用にカスタマイズおよび改良された TLS プロトコルです。WebRTC で DTLS が使用される場所には、SRTP キーのネゴシエーションと管理、および DataChannel の暗号化されたチャネルの提供という 2 つの部分があります。

DataChannel のデータはDTLSチャネルを使用し、暗号化してから udp 経由で反対側に送信します。In webrtc, a channel can send data or video. 異なるデータ フォームは異なるプロトコルを使用します. たとえば、オーディオとビデオのデータ伝送は RTP プロトコルを使用しますが、dataChannel は SCTP (ストリーム制御伝送プロトコル) プロトコルを使用します.

tcp や udp の欠点はよく知られていますが、SCTP はフロー制御や輻輳制御などを再設計し、安定したデータ転送を行うことができます。

ここに画像の説明を挿入

pionで実装する方法

DTLSTransportプロトコルはに実装されておりDTLS、呼び出しに便利です.t.conn取得後は、データ チャネルとして使用されます.

// Start DTLS transport negotiation with the parameters of the remote DTLS transport
func (t *DTLSTransport) Start(remoteParameters DTLSParameters) error {
    
    
	
	...
		t.srtpEndpoint = t.iceTransport.newEndpoint(mux.MatchSRTP)
		t.srtcpEndpoint = t.iceTransport.newEndpoint(mux.MatchSRTCP)
		t.remoteParameters = remoteParameters

		cert := t.certificates[0]
		t.onStateChange(DTLSTransportStateConnecting)

	...

	// Connect as DTLS Client/Server, function is blocking and we
	// must not hold the DTLSTransport lock
	if role == DTLSRoleClient {
    
    
		dtlsConn, err = dtls.Client(dtlsEndpoint, dtlsConfig)
	} else {
    
    
		dtlsConn, err = dtls.Server(dtlsEndpoint, dtlsConfig)
	}

	...

	t.conn = dtlsConn
	t.onStateChange(DTLSTransportStateConnected)

	return t.startSRTP()
}

sctp.Clientのチャネルは です。つまりdtlsTransport.conn、後でデータを送信するために使用されます。

func (r *SCTPTransport) Start(remoteCaps SCTPCapabilities) error {
    
    
	...
	sctpAssociation, err := sctp.Client(sctp.Config{
    
    
		NetConn:              dtlsTransport.conn,
		MaxReceiveBufferSize: r.api.settingEngine.sctp.maxReceiveBufferSize,
		LoggerFactory:        r.api.settingEngine.LoggerFactory,
	})
	if err != nil {
    
    
		return err
	}

...

	go r.acceptDataChannels(sctpAssociation)

	return nil
}

おすすめ

転載: blog.csdn.net/weixin_40425640/article/details/127086822