blob: 96fe2a0ad73ebf2c56bdea41090ca53604f3df1f [file] [log] [blame]
mikescarlett9bc517f2016-04-29 18:30:55 -07001/*
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
11#ifndef WEBRTC_API_QUICDATATRANSPORT_H_
12#define WEBRTC_API_QUICDATATRANSPORT_H_
13
14#include <string>
15#include <unordered_map>
16
17#include "webrtc/api/datachannelinterface.h"
18#include "webrtc/api/quicdatachannel.h"
19#include "webrtc/base/scoped_ref_ptr.h"
20#include "webrtc/base/sigslot.h"
21#include "webrtc/base/thread.h"
22
23namespace cricket {
24class QuicTransportChannel;
25class ReliableQuicStream;
26} // namepsace cricket
27
28namespace webrtc {
29
30// QuicDataTransport creates QuicDataChannels for the PeerConnection. It also
31// handles QUIC stream demuxing by distributing incoming QUIC streams from the
32// QuicTransportChannel among the QuicDataChannels that it has created.
33//
34// QuicDataTransport reads the data channel ID from the incoming QUIC stream,
35// then looks it up in a map of ID => QuicDataChannel. If the data channel
36// exists, it sends the QUIC stream to the QuicDataChannel.
37class QuicDataTransport : public sigslot::has_slots<> {
38 public:
zhihuangf2c2f8f2016-07-13 14:13:49 -070039 QuicDataTransport(rtc::Thread* signaling_thread,
40 rtc::Thread* worker_thread,
41 rtc::Thread* network_thread);
mikescarlett9bc517f2016-04-29 18:30:55 -070042 ~QuicDataTransport() override;
43
44 // Sets the QUIC transport channel for the QuicDataChannels and the
45 // QuicDataTransport. Returns false if a different QUIC transport channel is
46 // already set, the QUIC transport channel cannot be set for any of the
47 // QuicDataChannels, or |channel| is NULL.
48 bool SetTransportChannel(cricket::QuicTransportChannel* channel);
49
50 // Creates a QuicDataChannel that uses this QuicDataTransport.
51 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
52 const std::string& label,
53 const DataChannelInit* config);
54
55 // Removes a QuicDataChannel with the given ID from the QuicDataTransport's
56 // data channel map.
57 void DestroyDataChannel(int id);
58
59 // True if the QuicDataTransport has a data channel with the given ID.
60 bool HasDataChannel(int id) const;
61
62 // True if the QuicDataTransport has data channels.
63 bool HasDataChannels() const;
64
65 private:
66 // Called from the QuicTransportChannel when a ReliableQuicStream is created
67 // to receive incoming data.
68 void OnIncomingStream(cricket::ReliableQuicStream* stream);
69 // Called from the ReliableQuicStream when the first QUIC stream frame is
70 // received for incoming data. The QuicDataTransport reads the data channel ID
71 // and message ID from the incoming data, then dispatches the
72 // ReliableQuicStream to the QuicDataChannel with the same data channel ID.
73 void OnDataReceived(net::QuicStreamId stream_id,
74 const char* data,
75 size_t len);
76
77 // Map of data channel ID => QUIC data channel values.
78 std::unordered_map<int, rtc::scoped_refptr<QuicDataChannel>>
79 data_channel_by_id_;
80 // Map of QUIC stream ID => ReliableQuicStream* values.
81 std::unordered_map<net::QuicStreamId, cricket::ReliableQuicStream*>
82 quic_stream_by_id_;
83 // QuicTransportChannel for sending/receiving data.
84 cricket::QuicTransportChannel* quic_transport_channel_ = nullptr;
zhihuangf2c2f8f2016-07-13 14:13:49 -070085 // Threads for the QUIC data channel.
mikescarlett9bc517f2016-04-29 18:30:55 -070086 rtc::Thread* const signaling_thread_;
87 rtc::Thread* const worker_thread_;
zhihuangf2c2f8f2016-07-13 14:13:49 -070088 rtc::Thread* const network_thread_;
mikescarlett9bc517f2016-04-29 18:30:55 -070089};
90
91} // namespace webrtc
92
93#endif // WEBRTC_API_QUICDATATRANSPORT_H_