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_RTPTRANSPORTCONTROLLERADAPTER_H_ |
| 12 | #define ORTC_RTPTRANSPORTCONTROLLERADAPTER_H_ |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 13 | |
| 14 | #include <memory> |
| 15 | #include <set> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/ortc/ortcrtpreceiverinterface.h" |
| 20 | #include "api/ortc/ortcrtpsenderinterface.h" |
| 21 | #include "api/ortc/rtptransportcontrollerinterface.h" |
| 22 | #include "api/ortc/srtptransportinterface.h" |
| 23 | #include "call/call.h" |
| 24 | #include "call/rtp_transport_controller_send.h" |
| 25 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 26 | #include "media/base/mediachannel.h" // For MediaConfig. |
| 27 | #include "pc/channelmanager.h" |
| 28 | #include "rtc_base/constructormagic.h" |
| 29 | #include "rtc_base/sigslot.h" |
| 30 | #include "rtc_base/thread.h" |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | |
| 34 | class RtpTransportAdapter; |
| 35 | class OrtcRtpSenderAdapter; |
| 36 | class OrtcRtpReceiverAdapter; |
| 37 | |
nisse | 528b793 | 2017-05-08 03:21:43 -0700 | [diff] [blame] | 38 | // Implementation of RtpTransportControllerInterface. Wraps a Call, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 39 | // a VoiceChannel and VideoChannel, and maintains a list of dependent RTP |
| 40 | // transports. |
| 41 | // |
| 42 | // When used along with an RtpSenderAdapter or RtpReceiverAdapter, the |
| 43 | // sender/receiver passes its parameters along to this class, which turns them |
| 44 | // into cricket:: media descriptions (the interface used by BaseChannel). |
| 45 | // |
| 46 | // Due to the fact that BaseChannel has different subclasses for audio/video, |
| 47 | // the actual BaseChannel object is not created until an RtpSender/RtpReceiver |
| 48 | // needs them. |
| 49 | // |
| 50 | // All methods should be called on the signaling thread. |
| 51 | // |
| 52 | // TODO(deadbeef): When BaseChannel is split apart into separate |
| 53 | // "RtpSender"/"RtpTransceiver"/"RtpSender"/"RtpReceiver" objects, this adapter |
| 54 | // object can be replaced by a "real" one. |
| 55 | class RtpTransportControllerAdapter : public RtpTransportControllerInterface, |
| 56 | public sigslot::has_slots<> { |
| 57 | public: |
| 58 | // Creates a proxy that will call "public interface" methods on the correct |
| 59 | // thread. |
| 60 | // |
| 61 | // Doesn't take ownership of any objects passed in. |
| 62 | // |
| 63 | // |channel_manager| must not be null. |
| 64 | static std::unique_ptr<RtpTransportControllerInterface> CreateProxied( |
| 65 | const cricket::MediaConfig& config, |
| 66 | cricket::ChannelManager* channel_manager, |
| 67 | webrtc::RtcEventLog* event_log, |
| 68 | rtc::Thread* signaling_thread, |
| 69 | rtc::Thread* worker_thread); |
| 70 | |
| 71 | ~RtpTransportControllerAdapter() override; |
| 72 | |
| 73 | // RtpTransportControllerInterface implementation. |
| 74 | std::vector<RtpTransportInterface*> GetTransports() const override; |
| 75 | |
| 76 | // These methods are used by OrtcFactory to create RtpTransports, RtpSenders |
| 77 | // and RtpReceivers using this controller. Called "CreateProxied" because |
| 78 | // these methods return proxies that will safely call methods on the correct |
| 79 | // thread. |
| 80 | RTCErrorOr<std::unique_ptr<RtpTransportInterface>> CreateProxiedRtpTransport( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 81 | const RtpTransportParameters& rtcp_parameters, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 82 | PacketTransportInterface* rtp, |
| 83 | PacketTransportInterface* rtcp); |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 84 | |
| 85 | RTCErrorOr<std::unique_ptr<SrtpTransportInterface>> |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 86 | CreateProxiedSrtpTransport(const RtpTransportParameters& rtcp_parameters, |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 87 | PacketTransportInterface* rtp, |
| 88 | PacketTransportInterface* rtcp); |
| 89 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 90 | // |transport_proxy| needs to be a proxy to a transport because the |
| 91 | // application may call GetTransport() on the returned sender or receiver, |
| 92 | // and expects it to return a thread-safe transport proxy. |
| 93 | RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>> CreateProxiedRtpSender( |
| 94 | cricket::MediaType kind, |
| 95 | RtpTransportInterface* transport_proxy); |
| 96 | RTCErrorOr<std::unique_ptr<OrtcRtpReceiverInterface>> |
| 97 | CreateProxiedRtpReceiver(cricket::MediaType kind, |
| 98 | RtpTransportInterface* transport_proxy); |
| 99 | |
| 100 | // Methods used internally by other "adapter" classes. |
| 101 | rtc::Thread* signaling_thread() const { return signaling_thread_; } |
| 102 | rtc::Thread* worker_thread() const { return worker_thread_; } |
| 103 | |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 104 | // |parameters.keepalive| will be set for ALL RTP transports in the call. |
| 105 | RTCError SetRtpTransportParameters(const RtpTransportParameters& parameters, |
| 106 | RtpTransportInterface* inner_transport); |
| 107 | void SetRtpTransportParameters_w(const RtpTransportParameters& parameters); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 108 | |
| 109 | cricket::VoiceChannel* voice_channel() { return voice_channel_; } |
| 110 | cricket::VideoChannel* video_channel() { return video_channel_; } |
| 111 | |
| 112 | // |primary_ssrc| out parameter is filled with either |
| 113 | // |parameters.encodings[0].ssrc|, or a generated SSRC if that's left unset. |
| 114 | RTCError ValidateAndApplyAudioSenderParameters( |
| 115 | const RtpParameters& parameters, |
| 116 | uint32_t* primary_ssrc); |
| 117 | RTCError ValidateAndApplyVideoSenderParameters( |
| 118 | const RtpParameters& parameters, |
| 119 | uint32_t* primary_ssrc); |
| 120 | RTCError ValidateAndApplyAudioReceiverParameters( |
| 121 | const RtpParameters& parameters); |
| 122 | RTCError ValidateAndApplyVideoReceiverParameters( |
| 123 | const RtpParameters& parameters); |
| 124 | |
| 125 | protected: |
| 126 | RtpTransportControllerAdapter* GetInternal() override { return this; } |
| 127 | |
| 128 | private: |
| 129 | // Only expected to be called by RtpTransportControllerAdapter::CreateProxied. |
| 130 | RtpTransportControllerAdapter(const cricket::MediaConfig& config, |
| 131 | cricket::ChannelManager* channel_manager, |
| 132 | webrtc::RtcEventLog* event_log, |
| 133 | rtc::Thread* signaling_thread, |
| 134 | rtc::Thread* worker_thread); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 135 | void Init_w(); |
| 136 | void Close_w(); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 137 | |
| 138 | // These return an error if another of the same type of object is already |
| 139 | // attached, or if |transport_proxy| can't be used with the sender/receiver |
| 140 | // due to the limitation that the sender/receiver of the same media type must |
| 141 | // use the same transport. |
| 142 | RTCError AttachAudioSender(OrtcRtpSenderAdapter* sender, |
| 143 | RtpTransportInterface* inner_transport); |
| 144 | RTCError AttachVideoSender(OrtcRtpSenderAdapter* sender, |
| 145 | RtpTransportInterface* inner_transport); |
| 146 | RTCError AttachAudioReceiver(OrtcRtpReceiverAdapter* receiver, |
| 147 | RtpTransportInterface* inner_transport); |
| 148 | RTCError AttachVideoReceiver(OrtcRtpReceiverAdapter* receiver, |
| 149 | RtpTransportInterface* inner_transport); |
| 150 | |
| 151 | void OnRtpTransportDestroyed(RtpTransportAdapter* transport); |
| 152 | |
| 153 | void OnAudioSenderDestroyed(); |
| 154 | void OnVideoSenderDestroyed(); |
| 155 | void OnAudioReceiverDestroyed(); |
| 156 | void OnVideoReceiverDestroyed(); |
| 157 | |
| 158 | void CreateVoiceChannel(); |
| 159 | void CreateVideoChannel(); |
| 160 | void DestroyVoiceChannel(); |
| 161 | void DestroyVideoChannel(); |
| 162 | |
| 163 | void CopyRtcpParametersToDescriptions( |
| 164 | const RtcpParameters& params, |
| 165 | cricket::MediaContentDescription* local, |
| 166 | cricket::MediaContentDescription* remote); |
| 167 | |
| 168 | // Helper function to generate an SSRC that doesn't match one in any of the |
| 169 | // "content description" structs, or in |new_ssrcs| (which is needed since |
| 170 | // multiple SSRCs may be generated in one go). |
| 171 | uint32_t GenerateUnusedSsrc(std::set<uint32_t>* new_ssrcs) const; |
| 172 | |
| 173 | // |description| is the matching description where existing SSRCs can be |
| 174 | // found. |
| 175 | // |
| 176 | // This is a member function because it may need to generate SSRCs that don't |
| 177 | // match existing ones, which is more than ToStreamParamsVec does. |
| 178 | RTCErrorOr<cricket::StreamParamsVec> MakeSendStreamParamsVec( |
| 179 | std::vector<RtpEncodingParameters> encodings, |
| 180 | const std::string& cname, |
| 181 | const cricket::MediaContentDescription& description) const; |
| 182 | |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 183 | // If the |rtp_transport| is a SrtpTransport, set the cryptos of the |
| 184 | // audio/video content descriptions. |
| 185 | RTCError MaybeSetCryptos( |
| 186 | RtpTransportInterface* rtp_transport, |
| 187 | cricket::MediaContentDescription* local_description, |
| 188 | cricket::MediaContentDescription* remote_description); |
| 189 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 190 | rtc::Thread* signaling_thread_; |
| 191 | rtc::Thread* worker_thread_; |
| 192 | // |transport_proxies_| and |inner_audio_transport_|/|inner_audio_transport_| |
| 193 | // are somewhat redundant, but the latter are only set when |
| 194 | // RtpSenders/RtpReceivers are attached to the transport. |
| 195 | std::vector<RtpTransportInterface*> transport_proxies_; |
| 196 | RtpTransportInterface* inner_audio_transport_ = nullptr; |
| 197 | RtpTransportInterface* inner_video_transport_ = nullptr; |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 198 | const cricket::MediaConfig media_config_; |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 199 | RtpKeepAliveConfig keepalive_; |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 200 | cricket::ChannelManager* channel_manager_; |
| 201 | webrtc::RtcEventLog* event_log_; |
| 202 | std::unique_ptr<Call> call_; |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 203 | webrtc::RtpTransportControllerSend* call_send_rtp_transport_controller_; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 204 | |
| 205 | // BaseChannel takes content descriptions as input, so we store them here |
| 206 | // such that they can be updated when a new RtpSenderAdapter/ |
| 207 | // RtpReceiverAdapter attaches itself. |
| 208 | cricket::AudioContentDescription local_audio_description_; |
| 209 | cricket::AudioContentDescription remote_audio_description_; |
| 210 | cricket::VideoContentDescription local_video_description_; |
| 211 | cricket::VideoContentDescription remote_video_description_; |
| 212 | cricket::VoiceChannel* voice_channel_ = nullptr; |
| 213 | cricket::VideoChannel* video_channel_ = nullptr; |
| 214 | bool have_audio_sender_ = false; |
| 215 | bool have_video_sender_ = false; |
| 216 | bool have_audio_receiver_ = false; |
| 217 | bool have_video_receiver_ = false; |
| 218 | |
| 219 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtpTransportControllerAdapter); |
| 220 | }; |
| 221 | |
| 222 | } // namespace webrtc |
| 223 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 224 | #endif // ORTC_RTPTRANSPORTCONTROLLERADAPTER_H_ |