blob: f0c427d1b53dca52e936ca2b537d5b043dd6f623 [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:
39 QuicDataTransport(rtc::Thread* signaling_thread, rtc::Thread* worker_thread);
40 ~QuicDataTransport() override;
41
42 // Sets the QUIC transport channel for the QuicDataChannels and the
43 // QuicDataTransport. Returns false if a different QUIC transport channel is
44 // already set, the QUIC transport channel cannot be set for any of the
45 // QuicDataChannels, or |channel| is NULL.
46 bool SetTransportChannel(cricket::QuicTransportChannel* channel);
47
48 // Creates a QuicDataChannel that uses this QuicDataTransport.
49 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
50 const std::string& label,
51 const DataChannelInit* config);
52
53 // Removes a QuicDataChannel with the given ID from the QuicDataTransport's
54 // data channel map.
55 void DestroyDataChannel(int id);
56
57 // True if the QuicDataTransport has a data channel with the given ID.
58 bool HasDataChannel(int id) const;
59
60 // True if the QuicDataTransport has data channels.
61 bool HasDataChannels() const;
62
63 private:
64 // Called from the QuicTransportChannel when a ReliableQuicStream is created
65 // to receive incoming data.
66 void OnIncomingStream(cricket::ReliableQuicStream* stream);
67 // Called from the ReliableQuicStream when the first QUIC stream frame is
68 // received for incoming data. The QuicDataTransport reads the data channel ID
69 // and message ID from the incoming data, then dispatches the
70 // ReliableQuicStream to the QuicDataChannel with the same data channel ID.
71 void OnDataReceived(net::QuicStreamId stream_id,
72 const char* data,
73 size_t len);
74
75 // Map of data channel ID => QUIC data channel values.
76 std::unordered_map<int, rtc::scoped_refptr<QuicDataChannel>>
77 data_channel_by_id_;
78 // Map of QUIC stream ID => ReliableQuicStream* values.
79 std::unordered_map<net::QuicStreamId, cricket::ReliableQuicStream*>
80 quic_stream_by_id_;
81 // QuicTransportChannel for sending/receiving data.
82 cricket::QuicTransportChannel* quic_transport_channel_ = nullptr;
83 // Signaling and worker threads for the QUIC data channel.
84 rtc::Thread* const signaling_thread_;
85 rtc::Thread* const worker_thread_;
86};
87
88} // namespace webrtc
89
90#endif // WEBRTC_API_QUICDATATRANSPORT_H_