webrtc/pc/sctp_data_channel_transport.cc
Bjorn A Mellem bc3eebc722 Reland "Reland "Refactor SCTP data channels to use DataChannelTransportInterface.""
This is a reland of 487f9a17e4

Original change's description:
> Reland "Refactor SCTP data channels to use DataChannelTransportInterface."
> 
> Also clears SctpTransport before deleting JsepTransport.
> 
> SctpTransport is ref-counted, but the underlying transport is deleted when
> JsepTransport clears the rtp_dtls_transport.  This results in crashes when
> usrsctp attempts to send outgoing packets through a dangling pointer to the
> underlying transport.
> 
> Clearing SctpTransport before DtlsTransport removes the pointer to the
> underlying transport before it becomes invalid.
> 
> This fixes a crash in chromium's web platform tests (see
> https://chromium-review.googlesource.com/c/chromium/src/+/1776711).
> 
> Original change's description:
> > Refactor SCTP data channels to use DataChannelTransportInterface.
> >
> > This change moves SctpTransport to be owned by JsepTransport, which now
> > holds a DataChannelTransport implementation for SCTP when it is used for
> > data channels.
> >
> > This simplifies negotiation and fallback to SCTP.  Negotiation can now
> > use a composite DataChannelTransport, just as negotiation for RTP uses a
> > composite RTP transport.
> >
> > PeerConnection also has one fewer way it needs to manage data channels.
> > It now handles SCTP and datagram- or media-transport-based data channels
> > the same way.
> >
> > There are a few leaky abstractions left.  For example, PeerConnection
> > calls Start() on the SctpTransport at a particular point in negotiation,
> > but does not need to call this for other transports.  Similarly, PC
> > exposes an interface to the SCTP transport directly to the user; there
> > is no equivalent for other transports.
> 
> Bug: webrtc:9719
> Change-Id: I64e94b88afb119fdbf5f22750f88c8a084d53937
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/151981
> Reviewed-by: Benjamin Wright <benwright@webrtc.org>
> Reviewed-by: Steve Anton <steveanton@webrtc.org>
> Commit-Queue: Benjamin Wright <benwright@webrtc.org>
> Commit-Queue: Bjorn Mellem <mellem@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#29120}

Bug: webrtc:9719
Change-Id: I28481a3de64a3506bc57748106383eeba4ef205c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/152740
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Reviewed-by: Benjamin Wright <benwright@webrtc.org>
Reviewed-by: Seth Hampson <shampson@webrtc.org>
Commit-Queue: Bjorn Mellem <mellem@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29290}
2019-09-24 17:10:52 +00:00

112 lines
3.5 KiB
C++

/*
* Copyright 2019 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "pc/sctp_data_channel_transport.h"
#include "pc/sctp_utils.h"
namespace webrtc {
SctpDataChannelTransport::SctpDataChannelTransport(
cricket::SctpTransportInternal* sctp_transport)
: sctp_transport_(sctp_transport) {
sctp_transport_->SignalReadyToSendData.connect(
this, &SctpDataChannelTransport::OnReadyToSendData);
sctp_transport_->SignalDataReceived.connect(
this, &SctpDataChannelTransport::OnDataReceived);
sctp_transport_->SignalClosingProcedureStartedRemotely.connect(
this, &SctpDataChannelTransport::OnClosingProcedureStartedRemotely);
sctp_transport_->SignalClosingProcedureComplete.connect(
this, &SctpDataChannelTransport::OnClosingProcedureComplete);
}
RTCError SctpDataChannelTransport::OpenChannel(int channel_id) {
sctp_transport_->OpenStream(channel_id);
return RTCError::OK();
}
RTCError SctpDataChannelTransport::SendData(
int channel_id,
const SendDataParams& params,
const rtc::CopyOnWriteBuffer& buffer) {
// Map webrtc::SendDataParams to cricket::SendDataParams.
// TODO(mellem): See about unifying these structs.
cricket::SendDataParams sd_params;
sd_params.sid = channel_id;
sd_params.type = ToCricketDataMessageType(params.type);
sd_params.ordered = params.ordered;
sd_params.reliable = !(params.max_rtx_count || params.max_rtx_ms);
sd_params.max_rtx_count = params.max_rtx_count.value_or(-1);
sd_params.max_rtx_ms = params.max_rtx_ms.value_or(-1);
cricket::SendDataResult result;
sctp_transport_->SendData(sd_params, buffer, &result);
// TODO(mellem): See about changing the interfaces to not require mapping
// SendDataResult to RTCError and back again.
switch (result) {
case cricket::SendDataResult::SDR_SUCCESS:
return RTCError::OK();
case cricket::SendDataResult::SDR_BLOCK: {
// Send buffer is full.
ready_to_send_ = false;
return RTCError(RTCErrorType::RESOURCE_EXHAUSTED);
}
case cricket::SendDataResult::SDR_ERROR:
return RTCError(RTCErrorType::NETWORK_ERROR);
}
return RTCError(RTCErrorType::NETWORK_ERROR);
}
RTCError SctpDataChannelTransport::CloseChannel(int channel_id) {
sctp_transport_->ResetStream(channel_id);
return RTCError::OK();
}
void SctpDataChannelTransport::SetDataSink(DataChannelSink* sink) {
sink_ = sink;
if (sink_ && ready_to_send_) {
sink_->OnReadyToSend();
}
}
bool SctpDataChannelTransport::IsReadyToSend() const {
return ready_to_send_;
}
void SctpDataChannelTransport::OnReadyToSendData() {
ready_to_send_ = true;
if (sink_) {
sink_->OnReadyToSend();
}
}
void SctpDataChannelTransport::OnDataReceived(
const cricket::ReceiveDataParams& params,
const rtc::CopyOnWriteBuffer& buffer) {
if (sink_) {
sink_->OnDataReceived(params.sid, ToWebrtcDataMessageType(params.type),
buffer);
}
}
void SctpDataChannelTransport::OnClosingProcedureStartedRemotely(
int channel_id) {
if (sink_) {
sink_->OnChannelClosing(channel_id);
}
}
void SctpDataChannelTransport::OnClosingProcedureComplete(int channel_id) {
if (sink_) {
sink_->OnChannelClosed(channel_id);
}
}
} // namespace webrtc