2019-12-03 14:04:21 +01:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef PC_DATA_CHANNEL_CONTROLLER_H_
|
|
|
|
|
#define PC_DATA_CHANNEL_CONTROLLER_H_
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2021-01-29 14:45:08 +00:00
|
|
|
#include "api/data_channel_interface.h"
|
2022-02-23 13:44:59 +00:00
|
|
|
#include "api/rtc_error.h"
|
2021-01-29 14:45:08 +00:00
|
|
|
#include "api/scoped_refptr.h"
|
2021-02-10 14:31:24 +01:00
|
|
|
#include "api/sequence_checker.h"
|
2023-03-06 12:51:39 +01:00
|
|
|
#include "api/task_queue/pending_task_safety_flag.h"
|
2021-01-29 14:45:08 +00:00
|
|
|
#include "api/transport/data_channel_transport_interface.h"
|
|
|
|
|
#include "pc/data_channel_utils.h"
|
2020-07-09 15:32:34 -07:00
|
|
|
#include "pc/sctp_data_channel.h"
|
2021-01-29 14:45:08 +00:00
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/copy_on_write_buffer.h"
|
|
|
|
|
#include "rtc_base/ssl_stream_adapter.h"
|
|
|
|
|
#include "rtc_base/thread.h"
|
|
|
|
|
#include "rtc_base/thread_annotations.h"
|
2019-12-03 22:31:42 +01:00
|
|
|
#include "rtc_base/weak_ptr.h"
|
2019-12-03 14:04:21 +01:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2022-02-08 10:49:09 +00:00
|
|
|
class PeerConnectionInternal;
|
2022-02-08 07:41:25 +00:00
|
|
|
|
2022-05-11 09:35:36 +00:00
|
|
|
class DataChannelController : public SctpDataChannelControllerInterface,
|
2019-12-03 14:04:21 +01:00
|
|
|
public DataChannelSink {
|
|
|
|
|
public:
|
2022-02-08 10:49:09 +00:00
|
|
|
explicit DataChannelController(PeerConnectionInternal* pc) : pc_(pc) {}
|
2022-05-11 09:35:36 +00:00
|
|
|
~DataChannelController();
|
2019-12-03 14:04:21 +01:00
|
|
|
|
2020-01-09 13:29:56 +01:00
|
|
|
// Not copyable or movable.
|
|
|
|
|
DataChannelController(DataChannelController&) = delete;
|
|
|
|
|
DataChannelController& operator=(const DataChannelController& other) = delete;
|
|
|
|
|
DataChannelController(DataChannelController&&) = delete;
|
|
|
|
|
DataChannelController& operator=(DataChannelController&& other) = delete;
|
|
|
|
|
|
2021-04-16 11:12:14 +00:00
|
|
|
// Implements
|
2020-07-09 15:32:34 -07:00
|
|
|
// SctpDataChannelProviderInterface.
|
2023-03-21 14:48:51 +01:00
|
|
|
RTCError SendData(StreamId sid,
|
|
|
|
|
const SendDataParams& params,
|
|
|
|
|
const rtc::CopyOnWriteBuffer& payload) override;
|
2023-03-21 11:35:24 +01:00
|
|
|
void AddSctpDataStream(StreamId sid) override;
|
|
|
|
|
void RemoveSctpDataStream(StreamId sid) override;
|
2019-12-03 14:04:21 +01:00
|
|
|
bool ReadyToSendData() const override;
|
2023-03-02 10:51:16 +01:00
|
|
|
void OnChannelStateChanged(SctpDataChannel* channel,
|
|
|
|
|
DataChannelInterface::DataState state) override;
|
2019-12-03 14:04:21 +01:00
|
|
|
|
|
|
|
|
// Implements DataChannelSink.
|
|
|
|
|
void OnDataReceived(int channel_id,
|
|
|
|
|
DataMessageType type,
|
|
|
|
|
const rtc::CopyOnWriteBuffer& buffer) override;
|
|
|
|
|
void OnChannelClosing(int channel_id) override;
|
|
|
|
|
void OnChannelClosed(int channel_id) override;
|
|
|
|
|
void OnReadyToSend() override;
|
2021-06-29 14:58:23 +02:00
|
|
|
void OnTransportClosed(RTCError error) override;
|
2019-12-03 14:04:21 +01:00
|
|
|
|
|
|
|
|
// Called from PeerConnection::SetupDataChannelTransport_n
|
|
|
|
|
void SetupDataChannelTransport_n();
|
|
|
|
|
// Called from PeerConnection::TeardownDataChannelTransport_n
|
|
|
|
|
void TeardownDataChannelTransport_n();
|
|
|
|
|
|
|
|
|
|
// Called from PeerConnection::OnTransportChanged
|
|
|
|
|
// to make required changes to datachannels' transports.
|
|
|
|
|
void OnTransportChanged(
|
|
|
|
|
DataChannelTransportInterface* data_channel_transport);
|
|
|
|
|
|
2020-06-16 16:54:10 +02:00
|
|
|
// Called from PeerConnection::GetDataChannelStats on the signaling thread.
|
2020-07-09 15:32:34 -07:00
|
|
|
std::vector<DataChannelStats> GetDataChannelStats() const;
|
2020-06-16 16:54:10 +02:00
|
|
|
|
2019-12-03 14:04:21 +01:00
|
|
|
// Creates channel and adds it to the collection of DataChannels that will
|
2020-07-09 15:32:34 -07:00
|
|
|
// be offered in a SessionDescription, and wraps it in a proxy object.
|
|
|
|
|
rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy(
|
2019-12-03 14:04:21 +01:00
|
|
|
const std::string& label,
|
2023-03-25 10:56:18 +01:00
|
|
|
const InternalDataChannelInit& config);
|
2019-12-03 14:04:21 +01:00
|
|
|
void AllocateSctpSids(rtc::SSLRole role);
|
|
|
|
|
|
|
|
|
|
// Checks if any data channel has been added.
|
2023-03-15 20:39:42 +00:00
|
|
|
// A data channel currently exist.
|
2019-12-03 14:04:21 +01:00
|
|
|
bool HasDataChannels() const;
|
2023-03-15 20:39:42 +00:00
|
|
|
// At some point in time, a data channel has existed.
|
|
|
|
|
bool HasUsedDataChannels() const;
|
2019-12-03 14:04:21 +01:00
|
|
|
|
|
|
|
|
// Accessors
|
2020-06-15 13:47:42 +02:00
|
|
|
DataChannelTransportInterface* data_channel_transport() const;
|
|
|
|
|
void set_data_channel_transport(DataChannelTransportInterface* transport);
|
2019-12-03 14:04:21 +01:00
|
|
|
|
|
|
|
|
// Called when the transport for the data channels is closed or destroyed.
|
2021-06-29 14:58:23 +02:00
|
|
|
void OnTransportChannelClosed(RTCError error);
|
2019-12-03 14:04:21 +01:00
|
|
|
|
2020-07-09 15:32:34 -07:00
|
|
|
void OnSctpDataChannelClosed(SctpDataChannel* channel);
|
2019-12-03 14:04:21 +01:00
|
|
|
|
|
|
|
|
private:
|
2020-07-09 15:32:34 -07:00
|
|
|
rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel(
|
|
|
|
|
const std::string& label,
|
2023-03-25 10:56:18 +01:00
|
|
|
const InternalDataChannelInit& config);
|
2020-07-09 15:32:34 -07:00
|
|
|
|
2019-12-03 14:04:21 +01:00
|
|
|
// Parses and handles open messages. Returns true if the message is an open
|
2023-03-04 16:47:53 +01:00
|
|
|
// message and should be considered to be handled, false otherwise.
|
2023-03-15 12:36:20 +01:00
|
|
|
bool HandleOpenMessage_n(int channel_id,
|
|
|
|
|
DataMessageType type,
|
2019-12-03 14:04:21 +01:00
|
|
|
const rtc::CopyOnWriteBuffer& buffer)
|
2023-03-04 16:47:53 +01:00
|
|
|
RTC_RUN_ON(network_thread());
|
2019-12-03 14:04:21 +01:00
|
|
|
// Called when a valid data channel OPEN message is received.
|
|
|
|
|
void OnDataChannelOpenMessage(const std::string& label,
|
|
|
|
|
const InternalDataChannelInit& config)
|
|
|
|
|
RTC_RUN_ON(signaling_thread());
|
|
|
|
|
|
2020-06-15 13:47:42 +02:00
|
|
|
// Called from SendData when data_channel_transport() is true.
|
2023-03-21 14:48:51 +01:00
|
|
|
RTCError DataChannelSendData(StreamId sid,
|
|
|
|
|
const SendDataParams& params,
|
|
|
|
|
const rtc::CopyOnWriteBuffer& payload);
|
2020-06-15 13:47:42 +02:00
|
|
|
|
2020-06-16 16:54:10 +02:00
|
|
|
// Called when all data channels need to be notified of a transport channel
|
|
|
|
|
// (calls OnTransportChannelCreated on the signaling thread).
|
|
|
|
|
void NotifyDataChannelsOfTransportCreated();
|
|
|
|
|
|
2023-03-20 10:26:19 +01:00
|
|
|
std::vector<rtc::scoped_refptr<SctpDataChannel>>::iterator FindChannel(
|
|
|
|
|
StreamId stream_id);
|
|
|
|
|
|
2019-12-03 14:04:21 +01:00
|
|
|
rtc::Thread* network_thread() const;
|
|
|
|
|
rtc::Thread* signaling_thread() const;
|
|
|
|
|
|
|
|
|
|
// Plugin transport used for data channels. Pointer may be accessed and
|
|
|
|
|
// checked from any thread, but the object may only be touched on the
|
|
|
|
|
// network thread.
|
|
|
|
|
// TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
|
|
|
|
|
// thread.
|
|
|
|
|
DataChannelTransportInterface* data_channel_transport_ = nullptr;
|
|
|
|
|
|
|
|
|
|
// Cached value of whether the data channel transport is ready to send.
|
|
|
|
|
bool data_channel_transport_ready_to_send_
|
|
|
|
|
RTC_GUARDED_BY(signaling_thread()) = false;
|
|
|
|
|
|
2023-03-21 18:45:24 +01:00
|
|
|
SctpSidAllocator sid_allocator_;
|
2020-07-09 15:32:34 -07:00
|
|
|
std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_
|
2019-12-03 14:04:21 +01:00
|
|
|
RTC_GUARDED_BY(signaling_thread());
|
2023-03-15 20:39:42 +00:00
|
|
|
bool has_used_data_channels_ RTC_GUARDED_BY(signaling_thread()) = false;
|
2019-12-03 14:04:21 +01:00
|
|
|
|
|
|
|
|
// Owning PeerConnection.
|
2022-02-08 10:49:09 +00:00
|
|
|
PeerConnectionInternal* const pc_;
|
2021-03-23 09:23:10 +01:00
|
|
|
// The weak pointers must be dereferenced and invalidated on the signalling
|
|
|
|
|
// thread only.
|
2019-12-03 22:31:42 +01:00
|
|
|
rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
|
2023-03-06 12:51:39 +01:00
|
|
|
ScopedTaskSafety signaling_safety_;
|
2019-12-03 14:04:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
|
|
|
|
|
#endif // PC_DATA_CHANNEL_CONTROLLER_H_
|