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