mikescarlett | 449c8b2 | 2016-04-30 01:30:55 | [diff] [blame] | 1 | /* |
| 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 | |
ossu | 25e4ac7 | 2017-01-23 12:56:25 | [diff] [blame] | 11 | #ifndef WEBRTC_PC_QUICDATATRANSPORT_H_ |
| 12 | #define WEBRTC_PC_QUICDATATRANSPORT_H_ |
mikescarlett | 449c8b2 | 2016-04-30 01:30:55 | [diff] [blame] | 13 | |
| 14 | #include <string> |
| 15 | #include <unordered_map> |
| 16 | |
| 17 | #include "webrtc/api/datachannelinterface.h" |
mikescarlett | 449c8b2 | 2016-04-30 01:30:55 | [diff] [blame] | 18 | #include "webrtc/base/scoped_ref_ptr.h" |
| 19 | #include "webrtc/base/sigslot.h" |
| 20 | #include "webrtc/base/thread.h" |
zhihuang | a722f71 | 2016-08-05 18:14:50 | [diff] [blame] | 21 | #include "webrtc/p2p/base/transportcontroller.h" |
ossu | 25e4ac7 | 2017-01-23 12:56:25 | [diff] [blame] | 22 | #include "webrtc/pc/quicdatachannel.h" |
mikescarlett | 449c8b2 | 2016-04-30 01:30:55 | [diff] [blame] | 23 | |
| 24 | namespace cricket { |
| 25 | class QuicTransportChannel; |
| 26 | class ReliableQuicStream; |
| 27 | } // namepsace cricket |
| 28 | |
| 29 | namespace 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. |
| 38 | class QuicDataTransport : public sigslot::has_slots<> { |
| 39 | public: |
zhihuang | da73ac2 | 2016-07-13 21:13:49 | [diff] [blame] | 40 | QuicDataTransport(rtc::Thread* signaling_thread, |
| 41 | rtc::Thread* worker_thread, |
zhihuang | a722f71 | 2016-08-05 18:14:50 | [diff] [blame] | 42 | rtc::Thread* network_thread, |
| 43 | cricket::TransportController* transport_controller); |
mikescarlett | 449c8b2 | 2016-04-30 01:30:55 | [diff] [blame] | 44 | ~QuicDataTransport() override; |
| 45 | |
zhihuang | a722f71 | 2016-08-05 18:14:50 | [diff] [blame] | 46 | // 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 | } |
mikescarlett | 449c8b2 | 2016-04-30 01:30:55 | [diff] [blame] | 53 | |
| 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 | |
zhihuang | a722f71 | 2016-08-05 18:14:50 | [diff] [blame] | 69 | cricket::QuicTransportChannel* quic_transport_channel() { |
| 70 | return quic_transport_channel_; |
| 71 | } |
| 72 | |
mikescarlett | 449c8b2 | 2016-04-30 01:30:55 | [diff] [blame] | 73 | private: |
zhihuang | a722f71 | 2016-08-05 18:14:50 | [diff] [blame] | 74 | // 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 | |
mikescarlett | 449c8b2 | 2016-04-30 01:30:55 | [diff] [blame] | 80 | // 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 | |
zhihuang | a722f71 | 2016-08-05 18:14:50 | [diff] [blame] | 91 | cricket::QuicTransportChannel* CreateTransportChannel( |
| 92 | const std::string& transport_name); |
| 93 | void DestroyTransportChannel(cricket::TransportChannel* transport_channel); |
| 94 | |
mikescarlett | 449c8b2 | 2016-04-30 01:30:55 | [diff] [blame] | 95 | // 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; |
zhihuang | da73ac2 | 2016-07-13 21:13:49 | [diff] [blame] | 103 | // Threads for the QUIC data channel. |
mikescarlett | 449c8b2 | 2016-04-30 01:30:55 | [diff] [blame] | 104 | rtc::Thread* const signaling_thread_; |
| 105 | rtc::Thread* const worker_thread_; |
zhihuang | da73ac2 | 2016-07-13 21:13:49 | [diff] [blame] | 106 | rtc::Thread* const network_thread_; |
zhihuang | a722f71 | 2016-08-05 18:14:50 | [diff] [blame] | 107 | |
| 108 | cricket::TransportController* transport_controller_; |
| 109 | std::string content_name_; |
| 110 | std::string transport_name_; |
mikescarlett | 449c8b2 | 2016-04-30 01:30:55 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace webrtc |
| 114 | |
ossu | 25e4ac7 | 2017-01-23 12:56:25 | [diff] [blame] | 115 | #endif // WEBRTC_PC_QUICDATATRANSPORT_H_ |