blob: 902ec01c5137e74909ec6d84851b406a7e4d2e78 [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/base/constructormagic.h"
24#include "webrtc/base/sigslot.h"
25#include "webrtc/base/thread.h"
26#include "webrtc/call/call.h"
27#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
zhihuangd3501ad2017-03-03 14:39:06 -080028#include "webrtc/media/base/mediachannel.h" // For MediaConfig.
deadbeefe814a0d2017-02-25 18:15:09 -080029#include "webrtc/pc/channelmanager.h"
30#include "webrtc/pc/mediacontroller.h"
deadbeefe814a0d2017-02-25 18:15:09 -080031
32namespace webrtc {
33
34class RtpTransportAdapter;
35class OrtcRtpSenderAdapter;
36class OrtcRtpReceiverAdapter;
37
38// Implementation of RtpTransportControllerInterface. Wraps a MediaController,
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.
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(
81 const RtcpParameters& rtcp_parameters,
82 PacketTransportInterface* rtp,
83 PacketTransportInterface* rtcp);
zhihuangd3501ad2017-03-03 14:39:06 -080084
85 RTCErrorOr<std::unique_ptr<SrtpTransportInterface>>
86 CreateProxiedSrtpTransport(const RtcpParameters& rtcp_parameters,
87 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
104 RTCError SetRtcpParameters(const RtcpParameters& parameters,
105 RtpTransportInterface* inner_transport);
106
107 cricket::VoiceChannel* voice_channel() { return voice_channel_; }
108 cricket::VideoChannel* video_channel() { return video_channel_; }
109
110 // |primary_ssrc| out parameter is filled with either
111 // |parameters.encodings[0].ssrc|, or a generated SSRC if that's left unset.
112 RTCError ValidateAndApplyAudioSenderParameters(
113 const RtpParameters& parameters,
114 uint32_t* primary_ssrc);
115 RTCError ValidateAndApplyVideoSenderParameters(
116 const RtpParameters& parameters,
117 uint32_t* primary_ssrc);
118 RTCError ValidateAndApplyAudioReceiverParameters(
119 const RtpParameters& parameters);
120 RTCError ValidateAndApplyVideoReceiverParameters(
121 const RtpParameters& parameters);
122
123 protected:
124 RtpTransportControllerAdapter* GetInternal() override { return this; }
125
126 private:
127 // Only expected to be called by RtpTransportControllerAdapter::CreateProxied.
128 RtpTransportControllerAdapter(const cricket::MediaConfig& config,
129 cricket::ChannelManager* channel_manager,
130 webrtc::RtcEventLog* event_log,
131 rtc::Thread* signaling_thread,
132 rtc::Thread* worker_thread);
133
134 // These return an error if another of the same type of object is already
135 // attached, or if |transport_proxy| can't be used with the sender/receiver
136 // due to the limitation that the sender/receiver of the same media type must
137 // use the same transport.
138 RTCError AttachAudioSender(OrtcRtpSenderAdapter* sender,
139 RtpTransportInterface* inner_transport);
140 RTCError AttachVideoSender(OrtcRtpSenderAdapter* sender,
141 RtpTransportInterface* inner_transport);
142 RTCError AttachAudioReceiver(OrtcRtpReceiverAdapter* receiver,
143 RtpTransportInterface* inner_transport);
144 RTCError AttachVideoReceiver(OrtcRtpReceiverAdapter* receiver,
145 RtpTransportInterface* inner_transport);
146
147 void OnRtpTransportDestroyed(RtpTransportAdapter* transport);
148
149 void OnAudioSenderDestroyed();
150 void OnVideoSenderDestroyed();
151 void OnAudioReceiverDestroyed();
152 void OnVideoReceiverDestroyed();
153
154 void CreateVoiceChannel();
155 void CreateVideoChannel();
156 void DestroyVoiceChannel();
157 void DestroyVideoChannel();
158
159 void CopyRtcpParametersToDescriptions(
160 const RtcpParameters& params,
161 cricket::MediaContentDescription* local,
162 cricket::MediaContentDescription* remote);
163
164 // Helper function to generate an SSRC that doesn't match one in any of the
165 // "content description" structs, or in |new_ssrcs| (which is needed since
166 // multiple SSRCs may be generated in one go).
167 uint32_t GenerateUnusedSsrc(std::set<uint32_t>* new_ssrcs) const;
168
169 // |description| is the matching description where existing SSRCs can be
170 // found.
171 //
172 // This is a member function because it may need to generate SSRCs that don't
173 // match existing ones, which is more than ToStreamParamsVec does.
174 RTCErrorOr<cricket::StreamParamsVec> MakeSendStreamParamsVec(
175 std::vector<RtpEncodingParameters> encodings,
176 const std::string& cname,
177 const cricket::MediaContentDescription& description) const;
178
zhihuangd3501ad2017-03-03 14:39:06 -0800179 // If the |rtp_transport| is a SrtpTransport, set the cryptos of the
180 // audio/video content descriptions.
181 RTCError MaybeSetCryptos(
182 RtpTransportInterface* rtp_transport,
183 cricket::MediaContentDescription* local_description,
184 cricket::MediaContentDescription* remote_description);
185
deadbeefe814a0d2017-02-25 18:15:09 -0800186 rtc::Thread* signaling_thread_;
187 rtc::Thread* worker_thread_;
188 // |transport_proxies_| and |inner_audio_transport_|/|inner_audio_transport_|
189 // are somewhat redundant, but the latter are only set when
190 // RtpSenders/RtpReceivers are attached to the transport.
191 std::vector<RtpTransportInterface*> transport_proxies_;
192 RtpTransportInterface* inner_audio_transport_ = nullptr;
193 RtpTransportInterface* inner_video_transport_ = nullptr;
194 std::unique_ptr<MediaControllerInterface> media_controller_;
195
196 // BaseChannel takes content descriptions as input, so we store them here
197 // such that they can be updated when a new RtpSenderAdapter/
198 // RtpReceiverAdapter attaches itself.
199 cricket::AudioContentDescription local_audio_description_;
200 cricket::AudioContentDescription remote_audio_description_;
201 cricket::VideoContentDescription local_video_description_;
202 cricket::VideoContentDescription remote_video_description_;
203 cricket::VoiceChannel* voice_channel_ = nullptr;
204 cricket::VideoChannel* video_channel_ = nullptr;
205 bool have_audio_sender_ = false;
206 bool have_video_sender_ = false;
207 bool have_audio_receiver_ = false;
208 bool have_video_receiver_ = false;
209
210 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtpTransportControllerAdapter);
211};
212
213} // namespace webrtc
214
215#endif // WEBRTC_ORTC_RTPTRANSPORTCONTROLLERADAPTER_H_