Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 PC_DTLSSRTPTRANSPORT_H_ |
| 12 | #define PC_DTLSSRTPTRANSPORT_H_ |
| 13 | |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "p2p/base/dtlstransportinternal.h" |
| 19 | #include "pc/rtptransportinternaladapter.h" |
| 20 | #include "pc/srtptransport.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | // This class is intended to be used as an RtpTransport and it wraps both an |
| 25 | // SrtpTransport and DtlsTransports(RTP/RTCP). When the DTLS handshake is |
| 26 | // finished, it extracts the keying materials from DtlsTransport and sets them |
| 27 | // to SrtpTransport. |
| 28 | class DtlsSrtpTransport : public RtpTransportInternalAdapter { |
| 29 | public: |
| 30 | explicit DtlsSrtpTransport( |
| 31 | std::unique_ptr<webrtc::SrtpTransport> srtp_transport); |
| 32 | |
| 33 | // Set P2P layer RTP/RTCP DtlsTransports. When using RTCP-muxing, |
| 34 | // |rtcp_dtls_transport| is null. |
| 35 | void SetDtlsTransports(cricket::DtlsTransportInternal* rtp_dtls_transport, |
| 36 | cricket::DtlsTransportInternal* rtcp_dtls_transport); |
| 37 | |
| 38 | void SetRtcpMuxEnabled(bool enable) override; |
| 39 | |
| 40 | // Set the header extension ids that should be encrypted. |
| 41 | void SetSendEncryptedHeaderExtensionIds( |
| 42 | const std::vector<int>& send_extension_ids); |
| 43 | |
| 44 | void SetRecvEncryptedHeaderExtensionIds( |
| 45 | const std::vector<int>& recv_extension_ids); |
| 46 | |
| 47 | bool IsActive() { return srtp_transport_->IsActive(); } |
| 48 | |
| 49 | // TODO(zhihuang): Remove this when we remove RtpTransportAdapter. |
| 50 | RtpTransportAdapter* GetInternal() override { return nullptr; } |
| 51 | |
| 52 | sigslot::signal2<DtlsSrtpTransport*, bool> SignalDtlsSrtpSetupFailure; |
| 53 | |
| 54 | private: |
| 55 | bool IsDtlsActive(); |
| 56 | bool IsDtlsConnected(); |
| 57 | bool IsDtlsWritable(); |
| 58 | bool DtlsHandshakeCompleted(); |
| 59 | void MaybeSetupDtlsSrtp(); |
| 60 | void SetupRtpDtlsSrtp(); |
| 61 | void SetupRtcpDtlsSrtp(); |
| 62 | bool ExtractParams(cricket::DtlsTransportInternal* dtls_transport, |
| 63 | int* selected_crypto_suite, |
| 64 | std::vector<unsigned char>* send_key, |
| 65 | std::vector<unsigned char>* recv_key); |
| 66 | void SetDtlsTransport(cricket::DtlsTransportInternal* new_dtls_transport, |
| 67 | cricket::DtlsTransportInternal** old_dtls_transport); |
| 68 | void SetRtpDtlsTransport(cricket::DtlsTransportInternal* rtp_dtls_transport); |
| 69 | void SetRtcpDtlsTransport( |
| 70 | cricket::DtlsTransportInternal* rtcp_dtls_transport); |
| 71 | void UpdateWritableStateAndMaybeSetupDtlsSrtp(); |
| 72 | // Set the writability and fire the SignalWritableState if the writability |
| 73 | // changes. |
| 74 | void SetWritable(bool writable); |
| 75 | |
| 76 | void OnDtlsState(cricket::DtlsTransportInternal* dtls_transport, |
| 77 | cricket::DtlsTransportState state); |
| 78 | void OnWritableState(rtc::PacketTransportInternal* transport); |
| 79 | void OnPacketReceived(bool rtcp, |
| 80 | rtc::CopyOnWriteBuffer* packet, |
| 81 | const rtc::PacketTime& packet_time); |
| 82 | void OnReadyToSend(bool ready); |
| 83 | |
| 84 | bool writable_ = false; |
| 85 | std::unique_ptr<SrtpTransport> srtp_transport_; |
| 86 | // Owned by the TransportController. |
| 87 | cricket::DtlsTransportInternal* rtp_dtls_transport_ = nullptr; |
| 88 | cricket::DtlsTransportInternal* rtcp_dtls_transport_ = nullptr; |
| 89 | |
| 90 | // The encrypted header extension IDs. |
| 91 | rtc::Optional<std::vector<int>> send_extension_ids_; |
| 92 | rtc::Optional<std::vector<int>> recv_extension_ids_; |
| 93 | }; |
| 94 | |
| 95 | } // namespace webrtc |
| 96 | |
| 97 | #endif // PC_DTLSSRTPTRANSPORT_H_ |