blob: 3982f2d5f796d7c55ab45f22157f1f04fa9287f1 [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#include "webrtc/ortc/rtptransportadapter.h"
12
13#include <algorithm> // For std::find.
14#include <set>
15#include <sstream>
16#include <utility> // For std::move.
17
18#include "webrtc/api/proxy.h"
19#include "webrtc/base/logging.h"
20
21namespace webrtc {
22
23BEGIN_OWNED_PROXY_MAP(RtpTransport)
24PROXY_SIGNALING_THREAD_DESTRUCTOR()
25PROXY_CONSTMETHOD0(PacketTransportInterface*, GetRtpPacketTransport)
26PROXY_CONSTMETHOD0(PacketTransportInterface*, GetRtcpPacketTransport)
27PROXY_METHOD1(RTCError, SetRtcpParameters, const RtcpParameters&)
28PROXY_CONSTMETHOD0(RtcpParameters, GetRtcpParameters)
29protected:
30RtpTransportAdapter* GetInternal() override {
31 return internal();
32}
33END_PROXY_MAP()
34
zhihuangd3501ad2017-03-03 14:39:06 -080035BEGIN_OWNED_PROXY_MAP(SrtpTransport)
36PROXY_SIGNALING_THREAD_DESTRUCTOR()
37PROXY_CONSTMETHOD0(PacketTransportInterface*, GetRtpPacketTransport)
38PROXY_CONSTMETHOD0(PacketTransportInterface*, GetRtcpPacketTransport)
39PROXY_METHOD1(RTCError, SetRtcpParameters, const RtcpParameters&)
40PROXY_CONSTMETHOD0(RtcpParameters, GetRtcpParameters)
41PROXY_METHOD1(RTCError, SetSrtpSendKey, const cricket::CryptoParams&)
42PROXY_METHOD1(RTCError, SetSrtpReceiveKey, const cricket::CryptoParams&)
43protected:
44RtpTransportAdapter* GetInternal() override {
45 return internal();
46}
47END_PROXY_MAP()
48
deadbeefe814a0d2017-02-25 18:15:09 -080049// static
50RTCErrorOr<std::unique_ptr<RtpTransportInterface>>
51RtpTransportAdapter::CreateProxied(
52 const RtcpParameters& rtcp_parameters,
53 PacketTransportInterface* rtp,
54 PacketTransportInterface* rtcp,
55 RtpTransportControllerAdapter* rtp_transport_controller) {
56 if (!rtp) {
57 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
58 "Must provide an RTP packet transport.");
59 }
60 if (!rtcp_parameters.mux && !rtcp) {
61 LOG_AND_RETURN_ERROR(
62 RTCErrorType::INVALID_PARAMETER,
63 "Must provide an RTCP packet transport when RTCP muxing is not used.");
64 }
65 if (rtcp_parameters.mux && rtcp) {
66 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
67 "Creating an RtpTransport with RTCP muxing enabled, "
68 "with a separate RTCP packet transport?");
69 }
70 if (!rtp_transport_controller) {
71 // Since OrtcFactory::CreateRtpTransport creates an RtpTransportController
72 // automatically when one isn't passed in, this should never be reached.
73 RTC_NOTREACHED();
74 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
75 "Must provide an RTP transport controller.");
76 }
77 return RtpTransportProxyWithInternal<RtpTransportAdapter>::Create(
78 rtp_transport_controller->signaling_thread(),
79 rtp_transport_controller->worker_thread(),
80 std::unique_ptr<RtpTransportAdapter>(new RtpTransportAdapter(
zhihuangd3501ad2017-03-03 14:39:06 -080081 rtcp_parameters, rtp, rtcp, rtp_transport_controller,
82 /*is_srtp_transport*/ false)));
83}
84
85RTCErrorOr<std::unique_ptr<SrtpTransportInterface>>
86RtpTransportAdapter::CreateSrtpProxied(
87 const RtcpParameters& rtcp_parameters,
88 PacketTransportInterface* rtp,
89 PacketTransportInterface* rtcp,
90 RtpTransportControllerAdapter* rtp_transport_controller) {
91 if (!rtp) {
92 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
93 "Must provide an RTP packet transport.");
94 }
95 if (!rtcp_parameters.mux && !rtcp) {
96 LOG_AND_RETURN_ERROR(
97 RTCErrorType::INVALID_PARAMETER,
98 "Must provide an RTCP packet transport when RTCP muxing is not used.");
99 }
100 if (rtcp_parameters.mux && rtcp) {
101 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
102 "Creating an RtpTransport with RTCP muxing enabled, "
103 "with a separate RTCP packet transport?");
104 }
105 if (!rtp_transport_controller) {
106 // Since OrtcFactory::CreateRtpTransport creates an RtpTransportController
107 // automatically when one isn't passed in, this should never be reached.
108 RTC_NOTREACHED();
109 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
110 "Must provide an RTP transport controller.");
111 }
112 return SrtpTransportProxyWithInternal<RtpTransportAdapter>::Create(
113 rtp_transport_controller->signaling_thread(),
114 rtp_transport_controller->worker_thread(),
115 std::unique_ptr<RtpTransportAdapter>(new RtpTransportAdapter(
116 rtcp_parameters, rtp, rtcp, rtp_transport_controller,
117 /*is_srtp_transport*/ true)));
deadbeefe814a0d2017-02-25 18:15:09 -0800118}
119
120void RtpTransportAdapter::TakeOwnershipOfRtpTransportController(
121 std::unique_ptr<RtpTransportControllerInterface> controller) {
122 RTC_DCHECK_EQ(rtp_transport_controller_, controller->GetInternal());
123 RTC_DCHECK(owned_rtp_transport_controller_.get() == nullptr);
124 owned_rtp_transport_controller_ = std::move(controller);
125}
126
127RtpTransportAdapter::RtpTransportAdapter(
128 const RtcpParameters& rtcp_parameters,
129 PacketTransportInterface* rtp,
130 PacketTransportInterface* rtcp,
zhihuangd3501ad2017-03-03 14:39:06 -0800131 RtpTransportControllerAdapter* rtp_transport_controller,
132 bool is_srtp_transport)
deadbeefe814a0d2017-02-25 18:15:09 -0800133 : rtp_packet_transport_(rtp),
134 rtcp_packet_transport_(rtcp),
135 rtp_transport_controller_(rtp_transport_controller),
zhihuangd3501ad2017-03-03 14:39:06 -0800136 rtcp_parameters_(rtcp_parameters),
137 is_srtp_transport_(is_srtp_transport) {
deadbeefe814a0d2017-02-25 18:15:09 -0800138 RTC_DCHECK(rtp_transport_controller);
139 // CNAME should have been filled by OrtcFactory if empty.
140 RTC_DCHECK(!rtcp_parameters_.cname.empty());
141}
142
143RtpTransportAdapter::~RtpTransportAdapter() {
144 SignalDestroyed(this);
145}
146
147PacketTransportInterface* RtpTransportAdapter::GetRtpPacketTransport() const {
148 return rtp_packet_transport_;
149}
150
151PacketTransportInterface* RtpTransportAdapter::GetRtcpPacketTransport() const {
152 return rtcp_packet_transport_;
153}
154
155RTCError RtpTransportAdapter::SetRtcpParameters(
156 const RtcpParameters& parameters) {
157 if (!parameters.mux && rtcp_parameters_.mux) {
158 LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::INVALID_STATE,
159 "Can't disable RTCP muxing after enabling.");
160 }
161 if (!parameters.cname.empty() && parameters.cname != rtcp_parameters_.cname) {
162 LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::UNSUPPORTED_OPERATION,
163 "Changing the RTCP CNAME is currently unsupported.");
164 }
165 // If the CNAME is empty, use the existing one.
166 RtcpParameters copy = parameters;
167 if (copy.cname.empty()) {
168 copy.cname = rtcp_parameters_.cname;
169 }
170 RTCError err = rtp_transport_controller_->SetRtcpParameters(copy, this);
171 if (!err.ok()) {
172 return err;
173 }
174 rtcp_parameters_ = copy;
175 if (rtcp_parameters_.mux) {
176 rtcp_packet_transport_ = nullptr;
177 }
178 return RTCError::OK();
179}
180
zhihuangd3501ad2017-03-03 14:39:06 -0800181RTCError RtpTransportAdapter::SetSrtpSendKey(
182 const cricket::CryptoParams& params) {
183 if (send_key_) {
184 LOG_AND_RETURN_ERROR(
185 webrtc::RTCErrorType::UNSUPPORTED_OPERATION,
186 "Setting the SRTP send key twice is currently unsupported.");
187 }
188 if (receive_key_ && receive_key_->cipher_suite != params.cipher_suite) {
189 LOG_AND_RETURN_ERROR(
190 webrtc::RTCErrorType::UNSUPPORTED_OPERATION,
191 "The send key and receive key must have the same cipher suite.");
192 }
193 send_key_ = rtc::Optional<cricket::CryptoParams>(params);
194 return RTCError::OK();
195}
196
197RTCError RtpTransportAdapter::SetSrtpReceiveKey(
198 const cricket::CryptoParams& params) {
199 if (receive_key_) {
200 LOG_AND_RETURN_ERROR(
201 webrtc::RTCErrorType::UNSUPPORTED_OPERATION,
202 "Setting the SRTP receive key twice is currently unsupported.");
203 }
204 if (send_key_ && send_key_->cipher_suite != params.cipher_suite) {
205 LOG_AND_RETURN_ERROR(
206 webrtc::RTCErrorType::UNSUPPORTED_OPERATION,
207 "The send key and receive key must have the same cipher suite.");
208 }
209 receive_key_ = rtc::Optional<cricket::CryptoParams>(params);
210 return RTCError::OK();
211}
212
deadbeefe814a0d2017-02-25 18:15:09 -0800213} // namespace webrtc