deadbeef | e814a0d | 2017-02-25 18:15:09 -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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef ORTC_RTPTRANSPORTADAPTER_H_ |
| 12 | #define ORTC_RTPTRANSPORTADAPTER_H_ |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 13 | |
| 14 | #include <memory> |
| 15 | #include <vector> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/rtcerror.h" |
| 18 | #include "media/base/streamparams.h" |
| 19 | #include "ortc/rtptransportcontrolleradapter.h" |
| 20 | #include "pc/channel.h" |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 21 | #include "pc/rtptransportinternaladapter.h" |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 22 | #include "pc/srtptransport.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/constructormagic.h" |
| 24 | #include "rtc_base/sigslot.h" |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 27 | // This class is a wrapper over an RtpTransport or an SrtpTransport. The base |
| 28 | // class RtpTransportInternalAdapter keeps a raw pointer, |transport_|, of the |
| 29 | // transport object and implements both the public SrtpTransportInterface and |
| 30 | // RtpTransport internal interface by calling the |transport_| underneath. |
| 31 | // |
| 32 | // This adapter can be used as an unencrypted RTP transport or an SrtpTransport |
| 33 | // with RtpSenderAdapter, RtpReceiverAdapter, and RtpTransportControllerAdapter. |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 34 | // |
| 35 | // TODO(deadbeef): When BaseChannel is split apart into separate |
| 36 | // "RtpTransport"/"RtpTransceiver"/"RtpSender"/"RtpReceiver" objects, this |
| 37 | // adapter object can be removed. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 38 | class RtpTransportAdapter : public RtpTransportInternalAdapter { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 39 | public: |
| 40 | // |rtp| can't be null. |rtcp| can if RTCP muxing is used immediately (meaning |
| 41 | // |rtcp_parameters.mux| is also true). |
| 42 | static RTCErrorOr<std::unique_ptr<RtpTransportInterface>> CreateProxied( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 43 | const RtpTransportParameters& rtcp_parameters, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 44 | PacketTransportInterface* rtp, |
| 45 | PacketTransportInterface* rtcp, |
| 46 | RtpTransportControllerAdapter* rtp_transport_controller); |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 47 | |
| 48 | static RTCErrorOr<std::unique_ptr<SrtpTransportInterface>> CreateSrtpProxied( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 49 | const RtpTransportParameters& rtcp_parameters, |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 50 | PacketTransportInterface* rtp, |
| 51 | PacketTransportInterface* rtcp, |
| 52 | RtpTransportControllerAdapter* rtp_transport_controller); |
| 53 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 54 | ~RtpTransportAdapter() override; |
| 55 | |
| 56 | // RtpTransportInterface implementation. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 57 | PacketTransportInterface* GetRtpPacketTransport() const override { |
| 58 | return rtp_packet_transport_; |
| 59 | } |
| 60 | PacketTransportInterface* GetRtcpPacketTransport() const override { |
| 61 | return rtcp_packet_transport_; |
| 62 | } |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 63 | RTCError SetParameters(const RtpTransportParameters& parameters) override; |
| 64 | RtpTransportParameters GetParameters() const override { return parameters_; } |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 65 | |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 66 | // SRTP specific implementation. |
| 67 | RTCError SetSrtpSendKey(const cricket::CryptoParams& params) override; |
| 68 | RTCError SetSrtpReceiveKey(const cricket::CryptoParams& params) override; |
| 69 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 70 | // Methods used internally by OrtcFactory. |
| 71 | RtpTransportControllerAdapter* rtp_transport_controller() { |
| 72 | return rtp_transport_controller_; |
| 73 | } |
| 74 | void TakeOwnershipOfRtpTransportController( |
| 75 | std::unique_ptr<RtpTransportControllerInterface> controller); |
| 76 | |
| 77 | // Used by RtpTransportControllerAdapter to tell when it should stop |
| 78 | // returning this transport from GetTransports(). |
| 79 | sigslot::signal1<RtpTransportAdapter*> SignalDestroyed; |
| 80 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 81 | bool IsSrtpActive() const override { return transport_->IsSrtpActive(); } |
| 82 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 83 | protected: |
| 84 | RtpTransportAdapter* GetInternal() override { return this; } |
| 85 | |
| 86 | private: |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 87 | RtpTransportAdapter(const RtcpParameters& rtcp_params, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 88 | PacketTransportInterface* rtp, |
| 89 | PacketTransportInterface* rtcp, |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 90 | RtpTransportControllerAdapter* rtp_transport_controller, |
| 91 | bool is_srtp_transport); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 92 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 93 | void OnReadyToSend(bool ready) { SignalReadyToSend(ready); } |
| 94 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 95 | void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer* packet, |
| 96 | const rtc::PacketTime& time) { |
| 97 | SignalRtcpPacketReceived(packet, time); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void OnWritableState(bool writable) { SignalWritableState(writable); } |
| 101 | |
| 102 | PacketTransportInterface* rtp_packet_transport_ = nullptr; |
| 103 | PacketTransportInterface* rtcp_packet_transport_ = nullptr; |
| 104 | RtpTransportControllerAdapter* const rtp_transport_controller_ = nullptr; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 105 | // Non-null if this class owns the transport controller. |
| 106 | std::unique_ptr<RtpTransportControllerInterface> |
| 107 | owned_rtp_transport_controller_; |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 108 | RtpTransportParameters parameters_; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 109 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 110 | // Only one of them is non-null; |
| 111 | std::unique_ptr<RtpTransport> unencrypted_rtp_transport_; |
| 112 | std::unique_ptr<SrtpTransport> srtp_transport_; |
| 113 | |
| 114 | rtc::Thread* network_thread_ = nullptr; |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 115 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 116 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtpTransportAdapter); |
| 117 | }; |
| 118 | |
| 119 | } // namespace webrtc |
| 120 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 121 | #endif // ORTC_RTPTRANSPORTADAPTER_H_ |