OLD | NEW |
(Empty) | |
| 1 /* |
| 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/helpers.h" |
| 20 #include "webrtc/base/logging.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 static const int kDefaultRtcpCnameLength = 16; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 namespace webrtc { |
| 29 |
| 30 BEGIN_OWNED_PROXY_MAP(RtpTransport) |
| 31 PROXY_SIGNALING_THREAD_DESTRUCTOR() |
| 32 PROXY_CONSTMETHOD0(PacketTransportInterface*, GetRtpPacketTransport) |
| 33 PROXY_CONSTMETHOD0(PacketTransportInterface*, GetRtcpPacketTransport) |
| 34 PROXY_METHOD1(RTCError, SetRtcpParameters, const RtcpParameters&) |
| 35 PROXY_CONSTMETHOD0(RtcpParameters, GetRtcpParameters) |
| 36 protected: |
| 37 RtpTransportAdapter* GetInternal() override { |
| 38 return internal(); |
| 39 } |
| 40 END_PROXY_MAP() |
| 41 |
| 42 // static |
| 43 RTCErrorOr<std::unique_ptr<RtpTransportInterface>> |
| 44 RtpTransportAdapter::CreateProxied( |
| 45 const RtcpParameters& rtcp_parameters, |
| 46 PacketTransportInterface* rtp, |
| 47 PacketTransportInterface* rtcp, |
| 48 RtpTransportControllerInterface* rtp_transport_controller) { |
| 49 if (!rtp) { |
| 50 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 51 "Must provide an RTP packet transport."); |
| 52 } |
| 53 if (!rtcp_parameters.mux && !rtcp) { |
| 54 LOG_AND_RETURN_ERROR( |
| 55 RTCErrorType::INVALID_PARAMETER, |
| 56 "Must provide an RTCP packet transport when RTCP muxing is not used."); |
| 57 } |
| 58 if (rtcp_parameters.mux && rtcp) { |
| 59 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 60 "Creating an RtpTransport with RTCP muxing enabled, " |
| 61 "with a separate RTCP packet transport?"); |
| 62 } |
| 63 if (!rtp_transport_controller) { |
| 64 // Since OrtcFactory::CreateRtpTransport creates an RtpTransportController |
| 65 // automatically when one isn't passed in, this should never be reached. |
| 66 RTC_NOTREACHED(); |
| 67 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 68 "Must provide an RTP transport controller."); |
| 69 } |
| 70 RtpTransportControllerAdapter* internal_controller = |
| 71 rtp_transport_controller->GetInternal(); |
| 72 auto proxy = RtpTransportProxyWithInternal<RtpTransportAdapter>::Create( |
| 73 internal_controller->signaling_thread(), |
| 74 internal_controller->worker_thread(), |
| 75 new RtpTransportAdapter(rtcp_parameters, rtp, rtcp, internal_controller)); |
| 76 internal_controller->AddTransport(proxy.get()); |
| 77 return std::move(proxy); |
| 78 } |
| 79 |
| 80 RTCErrorOr<std::unique_ptr<RtpTransportInterface>> |
| 81 RtpTransportAdapter::CreateProxied( |
| 82 const RtcpParameters& rtcp_parameters, |
| 83 PacketTransportInterface* rtp, |
| 84 PacketTransportInterface* rtcp, |
| 85 std::unique_ptr<RtpTransportControllerInterface> rtp_transport_controller) { |
| 86 auto result = |
| 87 CreateProxied(rtcp_parameters, rtp, rtcp, rtp_transport_controller.get()); |
| 88 // If RtpTransport was successfully created, transfer ownership of |
| 89 // |rtp_transport_controller|. Otherwise it will go out of scope and be |
| 90 // deleted automatically. |
| 91 if (result.ok()) { |
| 92 result.value()->GetInternal()->owned_rtp_transport_controller_.reset( |
| 93 rtp_transport_controller.release()); |
| 94 } |
| 95 return result; |
| 96 } |
| 97 |
| 98 RtpTransportAdapter::~RtpTransportAdapter() { |
| 99 rtp_transport_controller_->RemoveTransport(this); |
| 100 } |
| 101 |
| 102 PacketTransportInterface* RtpTransportAdapter::GetRtpPacketTransport() const { |
| 103 return rtp_packet_transport_; |
| 104 } |
| 105 |
| 106 PacketTransportInterface* RtpTransportAdapter::GetRtcpPacketTransport() const { |
| 107 return rtcp_packet_transport_; |
| 108 } |
| 109 |
| 110 RTCError RtpTransportAdapter::SetRtcpParameters( |
| 111 const RtcpParameters& parameters) { |
| 112 if (!parameters.mux && rtcp_parameters_.mux) { |
| 113 LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::INVALID_STATE, |
| 114 "Can't disable RTCP muxing after enabling."); |
| 115 } |
| 116 if (!parameters.cname.empty() && parameters.cname != rtcp_parameters_.cname) { |
| 117 LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::UNSUPPORTED_OPERATION, |
| 118 "Changing the RTCP CNAME is currently unsupported."); |
| 119 } |
| 120 // If the CNAME is empty, use the existing one. |
| 121 RtcpParameters copy = parameters; |
| 122 if (copy.cname.empty()) { |
| 123 copy.cname = rtcp_parameters_.cname; |
| 124 } |
| 125 RTCError err = rtp_transport_controller_->SetRtcpParameters(copy, this); |
| 126 if (!err.ok()) { |
| 127 return err; |
| 128 } |
| 129 rtcp_parameters_ = copy; |
| 130 if (rtcp_parameters_.mux) { |
| 131 rtcp_packet_transport_ = nullptr; |
| 132 } |
| 133 return RTCError::OK(); |
| 134 } |
| 135 |
| 136 RtpTransportAdapter::RtpTransportAdapter( |
| 137 const RtcpParameters& rtcp_parameters, |
| 138 PacketTransportInterface* rtp, |
| 139 PacketTransportInterface* rtcp, |
| 140 RtpTransportControllerAdapter* rtp_transport_controller) |
| 141 : rtp_packet_transport_(rtp), |
| 142 rtcp_packet_transport_(rtcp), |
| 143 rtp_transport_controller_(rtp_transport_controller), |
| 144 rtcp_parameters_(rtcp_parameters) { |
| 145 RTC_DCHECK(rtp_transport_controller); |
| 146 if (rtcp_parameters_.cname.empty()) { |
| 147 if (!rtc::CreateRandomString(kDefaultRtcpCnameLength, |
| 148 &rtcp_parameters_.cname)) { |
| 149 LOG(LS_ERROR) << "Failed to generate CNAME."; |
| 150 RTC_NOTREACHED(); |
| 151 } |
| 152 } |
| 153 } |
| 154 |
| 155 } // namespace webrtc |
OLD | NEW |