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, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 69 | rtc::Thread* worker_thread, |
| 70 | rtc::Thread* network_thread); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 71 | |
| 72 | ~RtpTransportControllerAdapter() override; |
| 73 | |
| 74 | // RtpTransportControllerInterface implementation. |
| 75 | std::vector<RtpTransportInterface*> GetTransports() const override; |
| 76 | |
| 77 | // These methods are used by OrtcFactory to create RtpTransports, RtpSenders |
| 78 | // and RtpReceivers using this controller. Called "CreateProxied" because |
| 79 | // these methods return proxies that will safely call methods on the correct |
| 80 | // thread. |
| 81 | RTCErrorOr<std::unique_ptr<RtpTransportInterface>> CreateProxiedRtpTransport( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 82 | const RtpTransportParameters& rtcp_parameters, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 83 | PacketTransportInterface* rtp, |
| 84 | PacketTransportInterface* rtcp); |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 85 | |
| 86 | RTCErrorOr<std::unique_ptr<SrtpTransportInterface>> |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 87 | CreateProxiedSrtpTransport(const RtpTransportParameters& rtcp_parameters, |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 88 | PacketTransportInterface* rtp, |
| 89 | PacketTransportInterface* rtcp); |
| 90 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 91 | // |transport_proxy| needs to be a proxy to a transport because the |
| 92 | // application may call GetTransport() on the returned sender or receiver, |
| 93 | // and expects it to return a thread-safe transport proxy. |
| 94 | RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>> CreateProxiedRtpSender( |
| 95 | cricket::MediaType kind, |
| 96 | RtpTransportInterface* transport_proxy); |
| 97 | RTCErrorOr<std::unique_ptr<OrtcRtpReceiverInterface>> |
| 98 | CreateProxiedRtpReceiver(cricket::MediaType kind, |
| 99 | RtpTransportInterface* transport_proxy); |
| 100 | |
| 101 | // Methods used internally by other "adapter" classes. |
| 102 | rtc::Thread* signaling_thread() const { return signaling_thread_; } |
| 103 | rtc::Thread* worker_thread() const { return worker_thread_; } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 104 | rtc::Thread* network_thread() const { return network_thread_; } |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 105 | |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 106 | // |parameters.keepalive| will be set for ALL RTP transports in the call. |
| 107 | RTCError SetRtpTransportParameters(const RtpTransportParameters& parameters, |
| 108 | RtpTransportInterface* inner_transport); |
| 109 | void SetRtpTransportParameters_w(const RtpTransportParameters& parameters); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 110 | |
| 111 | cricket::VoiceChannel* voice_channel() { return voice_channel_; } |
| 112 | cricket::VideoChannel* video_channel() { return video_channel_; } |
| 113 | |
| 114 | // |primary_ssrc| out parameter is filled with either |
| 115 | // |parameters.encodings[0].ssrc|, or a generated SSRC if that's left unset. |
| 116 | RTCError ValidateAndApplyAudioSenderParameters( |
| 117 | const RtpParameters& parameters, |
| 118 | uint32_t* primary_ssrc); |
| 119 | RTCError ValidateAndApplyVideoSenderParameters( |
| 120 | const RtpParameters& parameters, |
| 121 | uint32_t* primary_ssrc); |
| 122 | RTCError ValidateAndApplyAudioReceiverParameters( |
| 123 | const RtpParameters& parameters); |
| 124 | RTCError ValidateAndApplyVideoReceiverParameters( |
| 125 | const RtpParameters& parameters); |
| 126 | |
| 127 | protected: |
| 128 | RtpTransportControllerAdapter* GetInternal() override { return this; } |
| 129 | |
| 130 | private: |
| 131 | // Only expected to be called by RtpTransportControllerAdapter::CreateProxied. |
| 132 | RtpTransportControllerAdapter(const cricket::MediaConfig& config, |
| 133 | cricket::ChannelManager* channel_manager, |
| 134 | webrtc::RtcEventLog* event_log, |
| 135 | rtc::Thread* signaling_thread, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 136 | rtc::Thread* worker_thread, |
| 137 | rtc::Thread* network_thread); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 138 | void Init_w(); |
| 139 | void Close_w(); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 140 | |
| 141 | // These return an error if another of the same type of object is already |
| 142 | // attached, or if |transport_proxy| can't be used with the sender/receiver |
| 143 | // due to the limitation that the sender/receiver of the same media type must |
| 144 | // use the same transport. |
| 145 | RTCError AttachAudioSender(OrtcRtpSenderAdapter* sender, |
| 146 | RtpTransportInterface* inner_transport); |
| 147 | RTCError AttachVideoSender(OrtcRtpSenderAdapter* sender, |
| 148 | RtpTransportInterface* inner_transport); |
| 149 | RTCError AttachAudioReceiver(OrtcRtpReceiverAdapter* receiver, |
| 150 | RtpTransportInterface* inner_transport); |
| 151 | RTCError AttachVideoReceiver(OrtcRtpReceiverAdapter* receiver, |
| 152 | RtpTransportInterface* inner_transport); |
| 153 | |
| 154 | void OnRtpTransportDestroyed(RtpTransportAdapter* transport); |
| 155 | |
| 156 | void OnAudioSenderDestroyed(); |
| 157 | void OnVideoSenderDestroyed(); |
| 158 | void OnAudioReceiverDestroyed(); |
| 159 | void OnVideoReceiverDestroyed(); |
| 160 | |
| 161 | void CreateVoiceChannel(); |
| 162 | void CreateVideoChannel(); |
| 163 | void DestroyVoiceChannel(); |
| 164 | void DestroyVideoChannel(); |
| 165 | |
| 166 | void CopyRtcpParametersToDescriptions( |
| 167 | const RtcpParameters& params, |
| 168 | cricket::MediaContentDescription* local, |
| 169 | cricket::MediaContentDescription* remote); |
| 170 | |
| 171 | // Helper function to generate an SSRC that doesn't match one in any of the |
| 172 | // "content description" structs, or in |new_ssrcs| (which is needed since |
| 173 | // multiple SSRCs may be generated in one go). |
| 174 | uint32_t GenerateUnusedSsrc(std::set<uint32_t>* new_ssrcs) const; |
| 175 | |
| 176 | // |description| is the matching description where existing SSRCs can be |
| 177 | // found. |
| 178 | // |
| 179 | // This is a member function because it may need to generate SSRCs that don't |
| 180 | // match existing ones, which is more than ToStreamParamsVec does. |
| 181 | RTCErrorOr<cricket::StreamParamsVec> MakeSendStreamParamsVec( |
| 182 | std::vector<RtpEncodingParameters> encodings, |
| 183 | const std::string& cname, |
| 184 | const cricket::MediaContentDescription& description) const; |
| 185 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 186 | rtc::Thread* signaling_thread_; |
| 187 | rtc::Thread* worker_thread_; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 188 | rtc::Thread* network_thread_; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 189 | // |transport_proxies_| and |inner_audio_transport_|/|inner_audio_transport_| |
| 190 | // are somewhat redundant, but the latter are only set when |
| 191 | // RtpSenders/RtpReceivers are attached to the transport. |
| 192 | std::vector<RtpTransportInterface*> transport_proxies_; |
| 193 | RtpTransportInterface* inner_audio_transport_ = nullptr; |
| 194 | RtpTransportInterface* inner_video_transport_ = nullptr; |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 195 | const cricket::MediaConfig media_config_; |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 196 | RtpKeepAliveConfig keepalive_; |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 197 | cricket::ChannelManager* channel_manager_; |
| 198 | webrtc::RtcEventLog* event_log_; |
| 199 | std::unique_ptr<Call> call_; |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 200 | webrtc::RtpTransportControllerSend* call_send_rtp_transport_controller_; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 201 | |
| 202 | // BaseChannel takes content descriptions as input, so we store them here |
| 203 | // such that they can be updated when a new RtpSenderAdapter/ |
| 204 | // RtpReceiverAdapter attaches itself. |
| 205 | cricket::AudioContentDescription local_audio_description_; |
| 206 | cricket::AudioContentDescription remote_audio_description_; |
| 207 | cricket::VideoContentDescription local_video_description_; |
| 208 | cricket::VideoContentDescription remote_video_description_; |
| 209 | cricket::VoiceChannel* voice_channel_ = nullptr; |
| 210 | cricket::VideoChannel* video_channel_ = nullptr; |
| 211 | bool have_audio_sender_ = false; |
| 212 | bool have_video_sender_ = false; |
| 213 | bool have_audio_receiver_ = false; |
| 214 | bool have_video_receiver_ = false; |
| 215 | |
| 216 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtpTransportControllerAdapter); |
| 217 | }; |
| 218 | |
| 219 | } // namespace webrtc |
| 220 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 221 | #endif // ORTC_RTPTRANSPORTCONTROLLERADAPTER_H_ |