blob: bdc245dc6e674c25d9f40129a55202e74266d296 [file] [log] [blame]
deadbeefe814a0d2017-02-25 18:15:09 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef ORTC_RTPTRANSPORTCONTROLLERADAPTER_H_
12#define ORTC_RTPTRANSPORTCONTROLLERADAPTER_H_
deadbeefe814a0d2017-02-25 18:15:09 -080013
14#include <memory>
15#include <set>
16#include <string>
17#include <vector>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#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"
deadbeefe814a0d2017-02-25 18:15:09 -080031
32namespace webrtc {
33
34class RtpTransportAdapter;
35class OrtcRtpSenderAdapter;
36class OrtcRtpReceiverAdapter;
37
nisse528b7932017-05-08 03:21:43 -070038// Implementation of RtpTransportControllerInterface. Wraps a Call,
deadbeefe814a0d2017-02-25 18:15:09 -080039// 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.
55class 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 Huange830e682018-03-30 10:48:35 -070069 rtc::Thread* worker_thread,
70 rtc::Thread* network_thread);
deadbeefe814a0d2017-02-25 18:15:09 -080071
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(
sprangdb2a9fc2017-08-09 06:42:32 -070082 const RtpTransportParameters& rtcp_parameters,
deadbeefe814a0d2017-02-25 18:15:09 -080083 PacketTransportInterface* rtp,
84 PacketTransportInterface* rtcp);
zhihuangd3501ad2017-03-03 14:39:06 -080085
86 RTCErrorOr<std::unique_ptr<SrtpTransportInterface>>
sprangdb2a9fc2017-08-09 06:42:32 -070087 CreateProxiedSrtpTransport(const RtpTransportParameters& rtcp_parameters,
zhihuangd3501ad2017-03-03 14:39:06 -080088 PacketTransportInterface* rtp,
89 PacketTransportInterface* rtcp);
90
deadbeefe814a0d2017-02-25 18:15:09 -080091 // |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 Huange830e682018-03-30 10:48:35 -0700104 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefe814a0d2017-02-25 18:15:09 -0800105
sprangdb2a9fc2017-08-09 06:42:32 -0700106 // |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);
deadbeefe814a0d2017-02-25 18:15:09 -0800110
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 Huange830e682018-03-30 10:48:35 -0700136 rtc::Thread* worker_thread,
137 rtc::Thread* network_thread);
nisseeaabdf62017-05-05 02:23:02 -0700138 void Init_w();
139 void Close_w();
deadbeefe814a0d2017-02-25 18:15:09 -0800140
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
deadbeefe814a0d2017-02-25 18:15:09 -0800186 rtc::Thread* signaling_thread_;
187 rtc::Thread* worker_thread_;
Zhi Huange830e682018-03-30 10:48:35 -0700188 rtc::Thread* network_thread_;
deadbeefe814a0d2017-02-25 18:15:09 -0800189 // |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;
nisseeaabdf62017-05-05 02:23:02 -0700195 const cricket::MediaConfig media_config_;
sprangdb2a9fc2017-08-09 06:42:32 -0700196 RtpKeepAliveConfig keepalive_;
nisseeaabdf62017-05-05 02:23:02 -0700197 cricket::ChannelManager* channel_manager_;
198 webrtc::RtcEventLog* event_log_;
199 std::unique_ptr<Call> call_;
sprangdb2a9fc2017-08-09 06:42:32 -0700200 webrtc::RtpTransportControllerSend* call_send_rtp_transport_controller_;
deadbeefe814a0d2017-02-25 18:15:09 -0800201
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 Bonadei92ea95e2017-09-15 06:47:31 +0200221#endif // ORTC_RTPTRANSPORTCONTROLLERADAPTER_H_