blob: d4494d0d6ce5bda54c6241c0703cf1faeb970e1f [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
11#ifndef WEBRTC_ORTC_RTPTRANSPORTCONTROLLERADAPTER_H_
12#define WEBRTC_ORTC_RTPTRANSPORTCONTROLLERADAPTER_H_
13
14#include <memory>
15#include <set>
16#include <string>
17#include <vector>
18
zhihuangd3501ad2017-03-03 14:39:06 -080019#include "webrtc/api/ortc/ortcrtpreceiverinterface.h"
20#include "webrtc/api/ortc/ortcrtpsenderinterface.h"
21#include "webrtc/api/ortc/rtptransportcontrollerinterface.h"
22#include "webrtc/api/ortc/srtptransportinterface.h"
deadbeefe814a0d2017-02-25 18:15:09 -080023#include "webrtc/call/call.h"
sprangdb2a9fc2017-08-09 06:42:32 -070024#include "webrtc/call/rtp_transport_controller_send.h"
deadbeefe814a0d2017-02-25 18:15:09 -080025#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
zhihuangd3501ad2017-03-03 14:39:06 -080026#include "webrtc/media/base/mediachannel.h" // For MediaConfig.
deadbeefe814a0d2017-02-25 18:15:09 -080027#include "webrtc/pc/channelmanager.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020028#include "webrtc/rtc_base/constructormagic.h"
29#include "webrtc/rtc_base/sigslot.h"
30#include "webrtc/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,
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(
sprangdb2a9fc2017-08-09 06:42:32 -070081 const RtpTransportParameters& rtcp_parameters,
deadbeefe814a0d2017-02-25 18:15:09 -080082 PacketTransportInterface* rtp,
83 PacketTransportInterface* rtcp);
zhihuangd3501ad2017-03-03 14:39:06 -080084
85 RTCErrorOr<std::unique_ptr<SrtpTransportInterface>>
sprangdb2a9fc2017-08-09 06:42:32 -070086 CreateProxiedSrtpTransport(const RtpTransportParameters& rtcp_parameters,
zhihuangd3501ad2017-03-03 14:39:06 -080087 PacketTransportInterface* rtp,
88 PacketTransportInterface* rtcp);
89
deadbeefe814a0d2017-02-25 18:15:09 -080090 // |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
sprangdb2a9fc2017-08-09 06:42:32 -0700104 // |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);
deadbeefe814a0d2017-02-25 18:15:09 -0800108
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);
nisseeaabdf62017-05-05 02:23:02 -0700135 void Init_w();
136 void Close_w();
deadbeefe814a0d2017-02-25 18:15:09 -0800137
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
zhihuangd3501ad2017-03-03 14:39:06 -0800183 // 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
deadbeefe814a0d2017-02-25 18:15:09 -0800190 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;
nisseeaabdf62017-05-05 02:23:02 -0700198 const cricket::MediaConfig media_config_;
sprangdb2a9fc2017-08-09 06:42:32 -0700199 RtpKeepAliveConfig keepalive_;
nisseeaabdf62017-05-05 02:23:02 -0700200 cricket::ChannelManager* channel_manager_;
201 webrtc::RtcEventLog* event_log_;
202 std::unique_ptr<Call> call_;
sprangdb2a9fc2017-08-09 06:42:32 -0700203 webrtc::RtpTransportControllerSend* call_send_rtp_transport_controller_;
deadbeefe814a0d2017-02-25 18:15:09 -0800204
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
224#endif // WEBRTC_ORTC_RTPTRANSPORTCONTROLLERADAPTER_H_