blob: 6b35a104f15d2ce885519e707394125afabecc3b [file] [log] [blame]
mikescarlett449c8b22016-04-30 01:30:551/*
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
ossu25e4ac72017-01-23 12:56:2511#ifndef WEBRTC_PC_QUICDATATRANSPORT_H_
12#define WEBRTC_PC_QUICDATATRANSPORT_H_
mikescarlett449c8b22016-04-30 01:30:5513
14#include <string>
15#include <unordered_map>
16
17#include "webrtc/api/datachannelinterface.h"
mikescarlett449c8b22016-04-30 01:30:5518#include "webrtc/base/scoped_ref_ptr.h"
19#include "webrtc/base/sigslot.h"
20#include "webrtc/base/thread.h"
zhihuanga722f712016-08-05 18:14:5021#include "webrtc/p2p/base/transportcontroller.h"
ossu25e4ac72017-01-23 12:56:2522#include "webrtc/pc/quicdatachannel.h"
mikescarlett449c8b22016-04-30 01:30:5523
24namespace cricket {
25class QuicTransportChannel;
26class ReliableQuicStream;
27} // namepsace cricket
28
29namespace webrtc {
30
31// QuicDataTransport creates QuicDataChannels for the PeerConnection. It also
32// handles QUIC stream demuxing by distributing incoming QUIC streams from the
33// QuicTransportChannel among the QuicDataChannels that it has created.
34//
35// QuicDataTransport reads the data channel ID from the incoming QUIC stream,
36// then looks it up in a map of ID => QuicDataChannel. If the data channel
37// exists, it sends the QUIC stream to the QuicDataChannel.
38class QuicDataTransport : public sigslot::has_slots<> {
39 public:
zhihuangda73ac22016-07-13 21:13:4940 QuicDataTransport(rtc::Thread* signaling_thread,
41 rtc::Thread* worker_thread,
zhihuanga722f712016-08-05 18:14:5042 rtc::Thread* network_thread,
43 cricket::TransportController* transport_controller);
mikescarlett449c8b22016-04-30 01:30:5544 ~QuicDataTransport() override;
45
zhihuanga722f712016-08-05 18:14:5046 // The QuicDataTransport acts like a BaseChannel with these functions.
47 bool SetTransport(const std::string& transport_name);
48 const std::string& transport_name() const { return transport_name_; }
49 const std::string& content_name() const { return content_name_; }
50 void set_content_name(const std::string& content_name) {
51 content_name_ = content_name;
52 }
mikescarlett449c8b22016-04-30 01:30:5553
54 // Creates a QuicDataChannel that uses this QuicDataTransport.
55 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
56 const std::string& label,
57 const DataChannelInit* config);
58
59 // Removes a QuicDataChannel with the given ID from the QuicDataTransport's
60 // data channel map.
61 void DestroyDataChannel(int id);
62
63 // True if the QuicDataTransport has a data channel with the given ID.
64 bool HasDataChannel(int id) const;
65
66 // True if the QuicDataTransport has data channels.
67 bool HasDataChannels() const;
68
zhihuanga722f712016-08-05 18:14:5069 cricket::QuicTransportChannel* quic_transport_channel() {
70 return quic_transport_channel_;
71 }
72
mikescarlett449c8b22016-04-30 01:30:5573 private:
zhihuanga722f712016-08-05 18:14:5074 // Sets the QUIC transport channel for the QuicDataChannels and the
75 // QuicDataTransport. Returns false if a different QUIC transport channel is
76 // already set, the QUIC transport channel cannot be set for any of the
77 // QuicDataChannels, or |channel| is NULL.
78 bool SetTransportChannel(cricket::QuicTransportChannel* channel);
79
mikescarlett449c8b22016-04-30 01:30:5580 // Called from the QuicTransportChannel when a ReliableQuicStream is created
81 // to receive incoming data.
82 void OnIncomingStream(cricket::ReliableQuicStream* stream);
83 // Called from the ReliableQuicStream when the first QUIC stream frame is
84 // received for incoming data. The QuicDataTransport reads the data channel ID
85 // and message ID from the incoming data, then dispatches the
86 // ReliableQuicStream to the QuicDataChannel with the same data channel ID.
87 void OnDataReceived(net::QuicStreamId stream_id,
88 const char* data,
89 size_t len);
90
zhihuanga722f712016-08-05 18:14:5091 cricket::QuicTransportChannel* CreateTransportChannel(
92 const std::string& transport_name);
93 void DestroyTransportChannel(cricket::TransportChannel* transport_channel);
94
mikescarlett449c8b22016-04-30 01:30:5595 // Map of data channel ID => QUIC data channel values.
96 std::unordered_map<int, rtc::scoped_refptr<QuicDataChannel>>
97 data_channel_by_id_;
98 // Map of QUIC stream ID => ReliableQuicStream* values.
99 std::unordered_map<net::QuicStreamId, cricket::ReliableQuicStream*>
100 quic_stream_by_id_;
101 // QuicTransportChannel for sending/receiving data.
102 cricket::QuicTransportChannel* quic_transport_channel_ = nullptr;
zhihuangda73ac22016-07-13 21:13:49103 // Threads for the QUIC data channel.
mikescarlett449c8b22016-04-30 01:30:55104 rtc::Thread* const signaling_thread_;
105 rtc::Thread* const worker_thread_;
zhihuangda73ac22016-07-13 21:13:49106 rtc::Thread* const network_thread_;
zhihuanga722f712016-08-05 18:14:50107
108 cricket::TransportController* transport_controller_;
109 std::string content_name_;
110 std::string transport_name_;
mikescarlett449c8b22016-04-30 01:30:55111};
112
113} // namespace webrtc
114
ossu25e4ac72017-01-23 12:56:25115#endif // WEBRTC_PC_QUICDATATRANSPORT_H_