henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 2 | * Copyright 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 11 | #include <algorithm> |
| 12 | #include <iterator> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "pc/channel.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/call/audio_sink.h" |
| 18 | #include "media/base/mediaconstants.h" |
| 19 | #include "media/base/rtputils.h" |
| 20 | #include "rtc_base/bind.h" |
| 21 | #include "rtc_base/byteorder.h" |
| 22 | #include "rtc_base/checks.h" |
| 23 | #include "rtc_base/copyonwritebuffer.h" |
| 24 | #include "rtc_base/dscp.h" |
| 25 | #include "rtc_base/logging.h" |
| 26 | #include "rtc_base/networkroute.h" |
| 27 | #include "rtc_base/ptr_util.h" |
| 28 | #include "rtc_base/trace_event.h" |
Patrik Höglund | 42805f3 | 2018-01-18 19:15:38 +0000 | [diff] [blame] | 29 | // Adding 'nogncheck' to disable the gn include headers check to support modular |
| 30 | // WebRTC build targets. |
| 31 | #include "media/engine/webrtcvoiceengine.h" // nogncheck |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "p2p/base/packettransportinternal.h" |
| 33 | #include "pc/channelmanager.h" |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 34 | #include "pc/rtpmediautils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | |
| 36 | namespace cricket { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 37 | using rtc::Bind; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 38 | using webrtc::SdpType; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 39 | |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 40 | namespace { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 41 | |
| 42 | struct SendPacketMessageData : public rtc::MessageData { |
| 43 | rtc::CopyOnWriteBuffer packet; |
| 44 | rtc::PacketOptions options; |
| 45 | }; |
| 46 | |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 47 | } // namespace |
| 48 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 49 | enum { |
Steve Anton | 0807d15 | 2018-03-05 11:23:09 -0800 | [diff] [blame] | 50 | MSG_SEND_RTP_PACKET = 1, |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 51 | MSG_SEND_RTCP_PACKET, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | MSG_READYTOSENDDATA, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | MSG_DATARECEIVED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | MSG_FIRSTPACKETRECEIVED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 57 | static void SafeSetError(const std::string& message, std::string* error_desc) { |
| 58 | if (error_desc) { |
| 59 | *error_desc = message; |
| 60 | } |
| 61 | } |
| 62 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 63 | static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | // Check the packet size. We could check the header too if needed. |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 65 | return packet && IsValidRtpRtcpPacketSize(rtcp, packet->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 68 | template <class Codec> |
| 69 | void RtpParametersFromMediaDescription( |
| 70 | const MediaContentDescriptionImpl<Codec>* desc, |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 71 | const RtpHeaderExtensions& extensions, |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 72 | RtpParameters<Codec>* params) { |
| 73 | // TODO(pthatcher): Remove this once we're sure no one will give us |
Zhi Huang | 801b868 | 2017-11-15 11:36:43 -0800 | [diff] [blame] | 74 | // a description without codecs. Currently the ORTC implementation is relying |
| 75 | // on this. |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 76 | if (desc->has_codecs()) { |
| 77 | params->codecs = desc->codecs(); |
| 78 | } |
| 79 | // TODO(pthatcher): See if we really need |
| 80 | // rtp_header_extensions_set() and remove it if we don't. |
| 81 | if (desc->rtp_header_extensions_set()) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 82 | params->extensions = extensions; |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 83 | } |
deadbeef | 1387149 | 2015-12-09 12:37:51 -0800 | [diff] [blame] | 84 | params->rtcp.reduced_size = desc->rtcp_reduced_size(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 85 | } |
| 86 | |
nisse | 0510331 | 2016-03-16 02:22:50 -0700 | [diff] [blame] | 87 | template <class Codec> |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 88 | void RtpSendParametersFromMediaDescription( |
| 89 | const MediaContentDescriptionImpl<Codec>* desc, |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 90 | const RtpHeaderExtensions& extensions, |
nisse | 0510331 | 2016-03-16 02:22:50 -0700 | [diff] [blame] | 91 | RtpSendParameters<Codec>* send_params) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 92 | RtpParametersFromMediaDescription(desc, extensions, send_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 93 | send_params->max_bandwidth_bps = desc->bandwidth(); |
| 94 | } |
| 95 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 96 | BaseChannel::BaseChannel(rtc::Thread* worker_thread, |
| 97 | rtc::Thread* network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 98 | rtc::Thread* signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 99 | std::unique_ptr<MediaChannel> media_channel, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 100 | const std::string& content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 101 | bool srtp_required, |
| 102 | rtc::CryptoOptions crypto_options) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 103 | : worker_thread_(worker_thread), |
| 104 | network_thread_(network_thread), |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 105 | signaling_thread_(signaling_thread), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | content_name_(content_name), |
deadbeef | 7af91dd | 2016-12-13 11:29:11 -0800 | [diff] [blame] | 107 | srtp_required_(srtp_required), |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 108 | crypto_options_(crypto_options), |
Zhi Huang | 1d88d74 | 2017-11-15 15:58:49 -0800 | [diff] [blame] | 109 | media_channel_(std::move(media_channel)) { |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 110 | RTC_DCHECK_RUN_ON(worker_thread_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 111 | RTC_LOG(LS_INFO) << "Created channel for " << content_name; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | BaseChannel::~BaseChannel() { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 115 | TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel"); |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 116 | RTC_DCHECK_RUN_ON(worker_thread_); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 117 | Deinit(); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 118 | // Eats any outstanding messages or packets. |
| 119 | worker_thread_->Clear(&invoker_); |
| 120 | worker_thread_->Clear(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | // We must destroy the media channel before the transport channel, otherwise |
| 122 | // the media channel may try to send on the dead transport channel. NULLing |
| 123 | // is not an effective strategy since the sends will come on another thread. |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 124 | media_channel_.reset(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 125 | RTC_LOG(LS_INFO) << "Destroyed channel: " << content_name_; |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 128 | void BaseChannel::ConnectToRtpTransport() { |
| 129 | RTC_DCHECK(rtp_transport_); |
| 130 | rtp_transport_->SignalReadyToSend.connect( |
| 131 | this, &BaseChannel::OnTransportReadyToSend); |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 132 | // TODO(zstein): RtpTransport::SignalPacketReceived will probably be replaced |
| 133 | // with a callback interface later so that the demuxer can select which |
| 134 | // channel to signal. |
| 135 | rtp_transport_->SignalPacketReceived.connect(this, |
| 136 | &BaseChannel::OnPacketReceived); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 137 | rtp_transport_->SignalNetworkRouteChanged.connect( |
| 138 | this, &BaseChannel::OnNetworkRouteChanged); |
| 139 | rtp_transport_->SignalWritableState.connect(this, |
| 140 | &BaseChannel::OnWritableState); |
| 141 | rtp_transport_->SignalSentPacket.connect(this, |
| 142 | &BaseChannel::SignalSentPacket_n); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 143 | // TODO(bugs.webrtc.org/8587): Set the metrics observer through |
| 144 | // JsepTransportController once it takes responsibility for creating |
| 145 | // RtpTransports. |
| 146 | if (metrics_observer_) { |
| 147 | rtp_transport_->SetMetricsObserver(metrics_observer_); |
| 148 | } |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 149 | } |
Danil Chapovalov | dae07ba | 2016-05-14 01:43:50 +0200 | [diff] [blame] | 150 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 151 | void BaseChannel::DisconnectFromRtpTransport() { |
| 152 | RTC_DCHECK(rtp_transport_); |
| 153 | rtp_transport_->SignalReadyToSend.disconnect(this); |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 154 | rtp_transport_->SignalPacketReceived.disconnect(this); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 155 | rtp_transport_->SignalNetworkRouteChanged.disconnect(this); |
| 156 | rtp_transport_->SignalWritableState.disconnect(this); |
| 157 | rtp_transport_->SignalSentPacket.disconnect(this); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 158 | rtp_transport_->SetMetricsObserver(nullptr); |
Danil Chapovalov | dae07ba | 2016-05-14 01:43:50 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 161 | void BaseChannel::Init_w(webrtc::RtpTransportInternal* rtp_transport) { |
| 162 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 163 | network_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 164 | SetRtpTransport(rtp_transport); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 165 | }); |
| 166 | |
| 167 | // Both RTP and RTCP channels should be set, we can call SetInterface on |
| 168 | // the media channel and it can set network options. |
| 169 | media_channel_->SetInterface(this); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 170 | } |
| 171 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 172 | void BaseChannel::Deinit() { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 173 | RTC_DCHECK(worker_thread_->IsCurrent()); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 174 | media_channel_->SetInterface(NULL); |
Danil Chapovalov | dae07ba | 2016-05-14 01:43:50 +0200 | [diff] [blame] | 175 | // Packets arrive on the network thread, processing packets calls virtual |
| 176 | // functions, so need to stop this process in Deinit that is called in |
| 177 | // derived classes destructor. |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 178 | network_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 179 | FlushRtcpMessages_n(); |
Zhi Huang | 27f3bf5 | 2018-03-26 21:37:23 -0700 | [diff] [blame] | 180 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 181 | if (rtp_transport_) { |
| 182 | DisconnectFromRtpTransport(); |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 183 | } |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 184 | // Clear pending read packets/messages. |
| 185 | network_thread_->Clear(&invoker_); |
| 186 | network_thread_->Clear(this); |
| 187 | }); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 190 | void BaseChannel::SetRtpTransport(webrtc::RtpTransportInternal* rtp_transport) { |
| 191 | if (!network_thread_->IsCurrent()) { |
| 192 | network_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 193 | SetRtpTransport(rtp_transport); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 194 | }); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 195 | return; |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 196 | } |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 197 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 198 | if (rtp_transport_) { |
| 199 | DisconnectFromRtpTransport(); |
| 200 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 201 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 202 | rtp_transport_ = rtp_transport; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 203 | if (rtp_transport_) { |
| 204 | RTC_DCHECK(rtp_transport_->rtp_packet_transport()); |
| 205 | transport_name_ = rtp_transport_->rtp_packet_transport()->transport_name(); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 206 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 207 | ConnectToRtpTransport(); |
| 208 | OnTransportReadyToSend(rtp_transport_->IsReadyToSend()); |
| 209 | UpdateWritableState_n(); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 210 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 211 | // Set the cached socket options. |
| 212 | for (const auto& pair : socket_options_) { |
| 213 | rtp_transport_->rtp_packet_transport()->SetOption(pair.first, |
| 214 | pair.second); |
| 215 | } |
| 216 | if (rtp_transport_->rtcp_packet_transport()) { |
| 217 | for (const auto& pair : rtcp_socket_options_) { |
| 218 | rtp_transport_->rtp_packet_transport()->SetOption(pair.first, |
| 219 | pair.second); |
| 220 | } |
| 221 | } |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 222 | } |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 225 | void BaseChannel::SetMetricsObserver( |
| 226 | rtc::scoped_refptr<webrtc::MetricsObserverInterface> metrics_observer) { |
| 227 | metrics_observer_ = metrics_observer; |
| 228 | if (rtp_transport_) { |
| 229 | rtp_transport_->SetMetricsObserver(metrics_observer); |
| 230 | } |
| 231 | } |
| 232 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 233 | bool BaseChannel::Enable(bool enable) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 234 | worker_thread_->Invoke<void>( |
| 235 | RTC_FROM_HERE, |
| 236 | Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w, |
| 237 | this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 241 | bool BaseChannel::AddRecvStream(const StreamParams& sp) { |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 242 | return InvokeOnWorker<bool>(RTC_FROM_HERE, |
| 243 | Bind(&BaseChannel::AddRecvStream_w, this, sp)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 246 | bool BaseChannel::RemoveRecvStream(uint32_t ssrc) { |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 247 | return InvokeOnWorker<bool>( |
| 248 | RTC_FROM_HERE, Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 249 | } |
| 250 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 251 | bool BaseChannel::AddSendStream(const StreamParams& sp) { |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 252 | return InvokeOnWorker<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 253 | RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 256 | bool BaseChannel::RemoveSendStream(uint32_t ssrc) { |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 257 | return InvokeOnWorker<bool>( |
| 258 | RTC_FROM_HERE, |
| 259 | Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 260 | } |
| 261 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 262 | bool BaseChannel::SetLocalContent(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 263 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 264 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 265 | TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent"); |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 266 | return InvokeOnWorker<bool>( |
| 267 | RTC_FROM_HERE, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 268 | Bind(&BaseChannel::SetLocalContent_w, this, content, type, error_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | bool BaseChannel::SetRemoteContent(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 272 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 273 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 274 | TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent"); |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 275 | return InvokeOnWorker<bool>( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 276 | RTC_FROM_HERE, |
| 277 | Bind(&BaseChannel::SetRemoteContent_w, this, content, type, error_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 280 | bool BaseChannel::IsReadyToReceiveMedia_w() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 281 | // Receive data if we are enabled and have local content, |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 282 | return enabled() && |
| 283 | webrtc::RtpTransceiverDirectionHasRecv(local_content_direction_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 286 | bool BaseChannel::IsReadyToSendMedia_w() const { |
| 287 | // Need to access some state updated on the network thread. |
| 288 | return network_thread_->Invoke<bool>( |
| 289 | RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this)); |
| 290 | } |
| 291 | |
| 292 | bool BaseChannel::IsReadyToSendMedia_n() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 293 | // Send outgoing data if we are enabled, have local and remote content, |
| 294 | // and we have had some form of connectivity. |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 295 | return enabled() && |
| 296 | webrtc::RtpTransceiverDirectionHasRecv(remote_content_direction_) && |
| 297 | webrtc::RtpTransceiverDirectionHasSend(local_content_direction_) && |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 298 | was_ever_writable() && (srtp_active() || encryption_disabled_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 299 | } |
| 300 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 301 | bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 302 | const rtc::PacketOptions& options) { |
| 303 | return SendPacket(false, packet, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 304 | } |
| 305 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 306 | bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 307 | const rtc::PacketOptions& options) { |
| 308 | return SendPacket(true, packet, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 309 | } |
| 310 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 311 | int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 312 | int value) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 313 | return network_thread_->Invoke<int>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 314 | RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | int BaseChannel::SetOption_n(SocketType type, |
| 318 | rtc::Socket::Option opt, |
| 319 | int value) { |
| 320 | RTC_DCHECK(network_thread_->IsCurrent()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 321 | RTC_DCHECK(rtp_transport_); |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 322 | rtc::PacketTransportInternal* transport = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 323 | switch (type) { |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 324 | case ST_RTP: |
zstein | e8ab543 | 2017-07-12 11:48:11 -0700 | [diff] [blame] | 325 | transport = rtp_transport_->rtp_packet_transport(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 326 | socket_options_.push_back( |
| 327 | std::pair<rtc::Socket::Option, int>(opt, value)); |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 328 | break; |
| 329 | case ST_RTCP: |
zstein | e8ab543 | 2017-07-12 11:48:11 -0700 | [diff] [blame] | 330 | transport = rtp_transport_->rtcp_packet_transport(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 331 | rtcp_socket_options_.push_back( |
| 332 | std::pair<rtc::Socket::Option, int>(opt, value)); |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 333 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 334 | } |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 335 | return transport ? transport->SetOption(opt, value) : -1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 338 | void BaseChannel::OnWritableState(bool writable) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 339 | RTC_DCHECK(network_thread_->IsCurrent()); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 340 | if (writable) { |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 341 | ChannelWritable_n(); |
| 342 | } else { |
| 343 | ChannelNotWritable_n(); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 347 | void BaseChannel::OnNetworkRouteChanged( |
| 348 | rtc::Optional<rtc::NetworkRoute> network_route) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 349 | RTC_DCHECK(network_thread_->IsCurrent()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 350 | rtc::NetworkRoute new_route; |
| 351 | if (network_route) { |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 352 | new_route = *(network_route); |
Zhi Huang | 8c316c1 | 2017-11-13 21:13:45 +0000 | [diff] [blame] | 353 | } |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 354 | // Note: When the RTCP-muxing is not enabled, RTCP transport and RTP transport |
| 355 | // use the same transport name and MediaChannel::OnNetworkRouteChanged cannot |
| 356 | // work correctly. Intentionally leave it broken to simplify the code and |
| 357 | // encourage the users to stop using non-muxing RTCP. |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 358 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_, [=] { |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 359 | media_channel_->OnNetworkRouteChanged(transport_name_, new_route); |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 360 | }); |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 361 | } |
| 362 | |
zstein | 56162b9 | 2017-04-24 16:54:35 -0700 | [diff] [blame] | 363 | void BaseChannel::OnTransportReadyToSend(bool ready) { |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 364 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_, |
| 365 | [=] { media_channel_->OnReadyToSend(ready); }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 366 | } |
| 367 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 368 | bool BaseChannel::SendPacket(bool rtcp, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 369 | rtc::CopyOnWriteBuffer* packet, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 370 | const rtc::PacketOptions& options) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 371 | // SendPacket gets called from MediaEngine, on a pacer or an encoder thread. |
| 372 | // If the thread is not our network thread, we will post to our network |
| 373 | // so that the real work happens on our network. This avoids us having to |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 374 | // synchronize access to all the pieces of the send path, including |
| 375 | // SRTP and the inner workings of the transport channels. |
| 376 | // The only downside is that we can't return a proper failure code if |
| 377 | // needed. Since UDP is unreliable anyway, this should be a non-issue. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 378 | if (!network_thread_->IsCurrent()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 379 | // Avoid a copy by transferring the ownership of the packet data. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 380 | int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET; |
| 381 | SendPacketMessageData* data = new SendPacketMessageData; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 382 | data->packet = std::move(*packet); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 383 | data->options = options; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 384 | network_thread_->Post(RTC_FROM_HERE, this, message_id, data); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 385 | return true; |
| 386 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 387 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 388 | TRACE_EVENT0("webrtc", "BaseChannel::SendPacket"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 389 | |
| 390 | // Now that we are on the correct thread, ensure we have a place to send this |
| 391 | // packet before doing anything. (We might get RTCP packets that we don't |
| 392 | // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP |
| 393 | // transport. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 394 | if (!rtp_transport_ || !rtp_transport_->IsWritable(rtcp)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 395 | return false; |
| 396 | } |
| 397 | |
| 398 | // Protect ourselves against crazy data. |
| 399 | if (!ValidPacket(rtcp, packet)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 400 | RTC_LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " " |
| 401 | << RtpRtcpStringLiteral(rtcp) |
| 402 | << " packet: wrong size=" << packet->size(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 403 | return false; |
| 404 | } |
| 405 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 406 | if (!srtp_active()) { |
| 407 | if (srtp_required_) { |
| 408 | // The audio/video engines may attempt to send RTCP packets as soon as the |
| 409 | // streams are created, so don't treat this as an error for RTCP. |
| 410 | // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809 |
| 411 | if (rtcp) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 412 | return false; |
| 413 | } |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 414 | // However, there shouldn't be any RTP packets sent before SRTP is set up |
| 415 | // (and SetSend(true) is called). |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 416 | RTC_LOG(LS_ERROR) |
| 417 | << "Can't send outgoing RTP packet when SRTP is inactive" |
| 418 | << " and crypto is required"; |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 419 | RTC_NOTREACHED(); |
deadbeef | 8f425f9 | 2016-12-01 12:26:27 -0800 | [diff] [blame] | 420 | return false; |
| 421 | } |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 422 | |
| 423 | std::string packet_type = rtcp ? "RTCP" : "RTP"; |
| 424 | RTC_LOG(LS_WARNING) << "Sending an " << packet_type |
| 425 | << " packet without encryption."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 426 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 427 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 428 | // Bon voyage. |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 429 | return rtcp ? rtp_transport_->SendRtcpPacket(packet, options, PF_SRTP_BYPASS) |
| 430 | : rtp_transport_->SendRtpPacket(packet, options, PF_SRTP_BYPASS); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 433 | bool BaseChannel::HandlesPayloadType(int packet_type) const { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 434 | return bundle_filter_.FindPayloadType(packet_type); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 435 | } |
| 436 | |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 437 | void BaseChannel::OnPacketReceived(bool rtcp, |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 438 | rtc::CopyOnWriteBuffer* packet, |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 439 | const rtc::PacketTime& packet_time) { |
honghaiz@google.com | a67ca1a | 2015-01-28 19:48:33 +0000 | [diff] [blame] | 440 | if (!has_received_packet_ && !rtcp) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 441 | has_received_packet_ = true; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 442 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 445 | // Filter out the packet this channel cannot handle. |
| 446 | if (!rtcp && !bundle_filter_.DemuxPacket(packet->data(), packet->size())) { |
| 447 | return; |
| 448 | } |
| 449 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 450 | if (!srtp_active() && srtp_required_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 451 | // Our session description indicates that SRTP is required, but we got a |
| 452 | // packet before our SRTP filter is active. This means either that |
| 453 | // a) we got SRTP packets before we received the SDES keys, in which case |
| 454 | // we can't decrypt it anyway, or |
| 455 | // b) we got SRTP packets before DTLS completed on both the RTP and RTCP |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 456 | // transports, so we haven't yet extracted keys, even if DTLS did |
| 457 | // complete on the transport that the packets are being sent on. It's |
| 458 | // really good practice to wait for both RTP and RTCP to be good to go |
| 459 | // before sending media, to prevent weird failure modes, so it's fine |
| 460 | // for us to just eat packets here. This is all sidestepped if RTCP mux |
| 461 | // is used anyway. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 462 | RTC_LOG(LS_WARNING) |
| 463 | << "Can't process incoming " << RtpRtcpStringLiteral(rtcp) |
| 464 | << " packet when SRTP is inactive and crypto is required"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 468 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 469 | RTC_FROM_HERE, worker_thread_, |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 470 | Bind(&BaseChannel::ProcessPacket, this, rtcp, *packet, packet_time)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 471 | } |
| 472 | |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 473 | void BaseChannel::ProcessPacket(bool rtcp, |
| 474 | const rtc::CopyOnWriteBuffer& packet, |
| 475 | const rtc::PacketTime& packet_time) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 476 | RTC_DCHECK(worker_thread_->IsCurrent()); |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 477 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 478 | // Need to copy variable because OnRtcpReceived/OnPacketReceived |
| 479 | // requires non-const pointer to buffer. This doesn't memcpy the actual data. |
| 480 | rtc::CopyOnWriteBuffer data(packet); |
| 481 | if (rtcp) { |
| 482 | media_channel_->OnRtcpReceived(&data, packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 483 | } else { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 484 | media_channel_->OnPacketReceived(&data, packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 488 | void BaseChannel::EnableMedia_w() { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 489 | RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 490 | if (enabled_) |
| 491 | return; |
| 492 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 493 | RTC_LOG(LS_INFO) << "Channel enabled"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 494 | enabled_ = true; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 495 | UpdateMediaSendRecvState_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void BaseChannel::DisableMedia_w() { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 499 | RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 500 | if (!enabled_) |
| 501 | return; |
| 502 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 503 | RTC_LOG(LS_INFO) << "Channel disabled"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 504 | enabled_ = false; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 505 | UpdateMediaSendRecvState_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 508 | void BaseChannel::UpdateWritableState_n() { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 509 | if (rtp_transport_->IsWritable(/*rtcp=*/true) && |
| 510 | rtp_transport_->IsWritable(/*rtcp=*/false)) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 511 | ChannelWritable_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 512 | } else { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 513 | ChannelNotWritable_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 517 | void BaseChannel::ChannelWritable_n() { |
| 518 | RTC_DCHECK(network_thread_->IsCurrent()); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 519 | if (writable_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 520 | return; |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 521 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 522 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 523 | RTC_LOG(LS_INFO) << "Channel writable (" << content_name_ << ")" |
| 524 | << (was_ever_writable_ ? "" : " for the first time"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 525 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 526 | was_ever_writable_ = true; |
| 527 | writable_ = true; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 528 | UpdateMediaSendRecvState(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 531 | void BaseChannel::ChannelNotWritable_n() { |
| 532 | RTC_DCHECK(network_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 533 | if (!writable_) |
| 534 | return; |
| 535 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 536 | RTC_LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 537 | writable_ = false; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 538 | UpdateMediaSendRecvState(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 539 | } |
| 540 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 541 | bool BaseChannel::AddRecvStream_w(const StreamParams& sp) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 542 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
pbos | 482b12e | 2015-11-16 10:19:58 -0800 | [diff] [blame] | 543 | return media_channel()->AddRecvStream(sp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 546 | bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 547 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 548 | return media_channel()->RemoveRecvStream(ssrc); |
| 549 | } |
| 550 | |
| 551 | bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 552 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 553 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 554 | // Check for streams that have been removed. |
| 555 | bool ret = true; |
| 556 | for (StreamParamsVec::const_iterator it = local_streams_.begin(); |
| 557 | it != local_streams_.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 558 | if (!GetStreamBySsrc(streams, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 559 | if (!media_channel()->RemoveSendStream(it->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 560 | std::ostringstream desc; |
| 561 | desc << "Failed to remove send stream with ssrc " |
| 562 | << it->first_ssrc() << "."; |
| 563 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 564 | ret = false; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | // Check for new streams. |
| 569 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 570 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 571 | if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 572 | if (media_channel()->AddSendStream(*it)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 573 | RTC_LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 574 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 575 | std::ostringstream desc; |
| 576 | desc << "Failed to add send stream ssrc: " << it->first_ssrc(); |
| 577 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 578 | ret = false; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | local_streams_ = streams; |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | bool BaseChannel::UpdateRemoteStreams_w( |
| 587 | const std::vector<StreamParams>& streams, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 588 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 589 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 590 | // Check for streams that have been removed. |
| 591 | bool ret = true; |
| 592 | for (StreamParamsVec::const_iterator it = remote_streams_.begin(); |
| 593 | it != remote_streams_.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 594 | if (!GetStreamBySsrc(streams, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 595 | if (!RemoveRecvStream_w(it->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 596 | std::ostringstream desc; |
| 597 | desc << "Failed to remove remote stream with ssrc " |
| 598 | << it->first_ssrc() << "."; |
| 599 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 600 | ret = false; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | // Check for new streams. |
| 605 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 606 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 607 | if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 608 | if (AddRecvStream_w(*it)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 609 | RTC_LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 610 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 611 | std::ostringstream desc; |
| 612 | desc << "Failed to add remote stream ssrc: " << it->first_ssrc(); |
| 613 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 614 | ret = false; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | remote_streams_ = streams; |
| 619 | return ret; |
| 620 | } |
| 621 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 622 | RtpHeaderExtensions BaseChannel::GetFilteredRtpHeaderExtensions( |
| 623 | const RtpHeaderExtensions& extensions) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 624 | RTC_DCHECK(rtp_transport_); |
| 625 | if (crypto_options_.enable_encrypted_rtp_header_extensions) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 626 | RtpHeaderExtensions filtered; |
| 627 | auto pred = [](const webrtc::RtpExtension& extension) { |
| 628 | return !extension.encrypt; |
| 629 | }; |
| 630 | std::copy_if(extensions.begin(), extensions.end(), |
| 631 | std::back_inserter(filtered), pred); |
| 632 | return filtered; |
| 633 | } |
| 634 | |
| 635 | return webrtc::RtpExtension::FilterDuplicateNonEncrypted(extensions); |
| 636 | } |
| 637 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 638 | void BaseChannel::OnMessage(rtc::Message *pmsg) { |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 639 | TRACE_EVENT0("webrtc", "BaseChannel::OnMessage"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 640 | switch (pmsg->message_id) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 641 | case MSG_SEND_RTP_PACKET: |
| 642 | case MSG_SEND_RTCP_PACKET: { |
| 643 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 644 | SendPacketMessageData* data = |
| 645 | static_cast<SendPacketMessageData*>(pmsg->pdata); |
| 646 | bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET; |
| 647 | SendPacket(rtcp, &data->packet, data->options); |
| 648 | delete data; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | case MSG_FIRSTPACKETRECEIVED: { |
| 652 | SignalFirstPacketReceived(this); |
| 653 | break; |
| 654 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 658 | void BaseChannel::AddHandledPayloadType(int payload_type) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 659 | bundle_filter_.AddPayloadType(payload_type); |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 662 | void BaseChannel::FlushRtcpMessages_n() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 663 | // Flush all remaining RTCP messages. This should only be called in |
| 664 | // destructor. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 665 | RTC_DCHECK(network_thread_->IsCurrent()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 666 | rtc::MessageList rtcp_messages; |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 667 | network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages); |
| 668 | for (const auto& message : rtcp_messages) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 669 | network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET, |
| 670 | message.pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 674 | void BaseChannel::SignalSentPacket_n(const rtc::SentPacket& sent_packet) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 675 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 676 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 677 | RTC_FROM_HERE, worker_thread_, |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 678 | rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet)); |
| 679 | } |
| 680 | |
| 681 | void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 682 | RTC_DCHECK(worker_thread_->IsCurrent()); |
| 683 | SignalSentPacket(sent_packet); |
| 684 | } |
| 685 | |
| 686 | VoiceChannel::VoiceChannel(rtc::Thread* worker_thread, |
| 687 | rtc::Thread* network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 688 | rtc::Thread* signaling_thread, |
Niels Möller | f120cba | 2018-01-30 09:33:03 +0100 | [diff] [blame] | 689 | // TODO(nisse): Delete unused argument. |
| 690 | MediaEngineInterface* /* media_engine */, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 691 | std::unique_ptr<VoiceMediaChannel> media_channel, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 692 | const std::string& content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 693 | bool srtp_required, |
| 694 | rtc::CryptoOptions crypto_options) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 695 | : BaseChannel(worker_thread, |
| 696 | network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 697 | signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 698 | std::move(media_channel), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 699 | content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 700 | srtp_required, |
| 701 | crypto_options) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 702 | |
| 703 | VoiceChannel::~VoiceChannel() { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 704 | TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 705 | // this can't be done in the base class, since it calls a virtual |
| 706 | DisableMedia_w(); |
Zhi Huang | 0ffe03d | 2018-03-30 13:17:42 -0700 | [diff] [blame] | 707 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 710 | void BaseChannel::UpdateMediaSendRecvState() { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 711 | RTC_DCHECK(network_thread_->IsCurrent()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 712 | invoker_.AsyncInvoke<void>( |
| 713 | RTC_FROM_HERE, worker_thread_, |
| 714 | Bind(&BaseChannel::UpdateMediaSendRecvState_w, this)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 717 | void VoiceChannel::UpdateMediaSendRecvState_w() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 718 | // Render incoming data if we're the active call, and we have the local |
| 719 | // content. We receive data on the default channel and multiplexed streams. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 720 | bool recv = IsReadyToReceiveMedia_w(); |
solenberg | 5b14b42 | 2015-10-01 04:10:31 -0700 | [diff] [blame] | 721 | media_channel()->SetPlayout(recv); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 722 | |
| 723 | // Send outgoing data if we're the active call, we have the remote content, |
| 724 | // and we have had some form of connectivity. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 725 | bool send = IsReadyToSendMedia_w(); |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 726 | media_channel()->SetSend(send); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 727 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 728 | RTC_LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 729 | } |
| 730 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 731 | bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 732 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 733 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 734 | TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 735 | RTC_DCHECK_RUN_ON(worker_thread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 736 | RTC_LOG(LS_INFO) << "Setting local voice description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 737 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 738 | RTC_DCHECK(content); |
| 739 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 740 | SafeSetError("Can't find audio content in local description.", error_desc); |
| 741 | return false; |
| 742 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 743 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 744 | const AudioContentDescription* audio = content->as_audio(); |
| 745 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 746 | RtpHeaderExtensions rtp_header_extensions = |
| 747 | GetFilteredRtpHeaderExtensions(audio->rtp_header_extensions()); |
| 748 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 749 | AudioRecvParameters recv_params = last_recv_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 750 | RtpParametersFromMediaDescription(audio, rtp_header_extensions, &recv_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 751 | if (!media_channel()->SetRecvParameters(recv_params)) { |
Peter Thatcher | bfab5cb | 2015-08-20 17:40:24 -0700 | [diff] [blame] | 752 | SafeSetError("Failed to set local audio description recv parameters.", |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 753 | error_desc); |
| 754 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 755 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 756 | for (const AudioCodec& codec : audio->codecs()) { |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 757 | AddHandledPayloadType(codec.id); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 758 | } |
| 759 | last_recv_params_ = recv_params; |
| 760 | |
| 761 | // TODO(pthatcher): Move local streams into AudioSendParameters, and |
| 762 | // only give it to the media channel once we have a remote |
| 763 | // description too (without a remote description, we won't be able |
| 764 | // to send them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 765 | if (!UpdateLocalStreams_w(audio->streams(), type, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 766 | SafeSetError("Failed to set local audio description streams.", error_desc); |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | set_local_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 771 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 772 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 776 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 777 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 778 | TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 779 | RTC_DCHECK_RUN_ON(worker_thread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 780 | RTC_LOG(LS_INFO) << "Setting remote voice description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 781 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 782 | RTC_DCHECK(content); |
| 783 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 784 | SafeSetError("Can't find audio content in remote description.", error_desc); |
| 785 | return false; |
| 786 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 787 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 788 | const AudioContentDescription* audio = content->as_audio(); |
| 789 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 790 | RtpHeaderExtensions rtp_header_extensions = |
| 791 | GetFilteredRtpHeaderExtensions(audio->rtp_header_extensions()); |
| 792 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 793 | AudioSendParameters send_params = last_send_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 794 | RtpSendParametersFromMediaDescription(audio, rtp_header_extensions, |
| 795 | &send_params); |
Steve Anton | bb50ce5 | 2018-03-26 10:24:32 -0700 | [diff] [blame] | 796 | send_params.mid = content_name(); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 797 | |
| 798 | bool parameters_applied = media_channel()->SetSendParameters(send_params); |
| 799 | if (!parameters_applied) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 800 | SafeSetError("Failed to set remote audio description send parameters.", |
| 801 | error_desc); |
| 802 | return false; |
| 803 | } |
| 804 | last_send_params_ = send_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 805 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 806 | // TODO(pthatcher): Move remote streams into AudioRecvParameters, |
| 807 | // and only give it to the media channel once we have a local |
| 808 | // description too (without a local description, we won't be able to |
| 809 | // recv them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 810 | if (!UpdateRemoteStreams_w(audio->streams(), type, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 811 | SafeSetError("Failed to set remote audio description streams.", error_desc); |
| 812 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 815 | set_remote_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 816 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 817 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 820 | VideoChannel::VideoChannel(rtc::Thread* worker_thread, |
| 821 | rtc::Thread* network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 822 | rtc::Thread* signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 823 | std::unique_ptr<VideoMediaChannel> media_channel, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 824 | const std::string& content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 825 | bool srtp_required, |
| 826 | rtc::CryptoOptions crypto_options) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 827 | : BaseChannel(worker_thread, |
| 828 | network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 829 | signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 830 | std::move(media_channel), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 831 | content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 832 | srtp_required, |
| 833 | crypto_options) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 834 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 835 | VideoChannel::~VideoChannel() { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 836 | TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 837 | // this can't be done in the base class, since it calls a virtual |
| 838 | DisableMedia_w(); |
Zhi Huang | 0ffe03d | 2018-03-30 13:17:42 -0700 | [diff] [blame] | 839 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 842 | void VideoChannel::UpdateMediaSendRecvState_w() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 843 | // Send outgoing data if we're the active call, we have the remote content, |
| 844 | // and we have had some form of connectivity. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 845 | bool send = IsReadyToSendMedia_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 846 | if (!media_channel()->SetSend(send)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 847 | RTC_LOG(LS_ERROR) << "Failed to SetSend on video channel"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 848 | // TODO(gangji): Report error back to server. |
| 849 | } |
| 850 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 851 | RTC_LOG(LS_INFO) << "Changing video state, send=" << send; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 852 | } |
| 853 | |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 854 | void VideoChannel::FillBitrateInfo(BandwidthEstimationInfo* bwe_info) { |
| 855 | InvokeOnWorker<void>(RTC_FROM_HERE, Bind(&VideoMediaChannel::FillBitrateInfo, |
| 856 | media_channel(), bwe_info)); |
| 857 | } |
| 858 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 859 | bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 860 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 861 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 862 | TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 863 | RTC_DCHECK_RUN_ON(worker_thread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 864 | RTC_LOG(LS_INFO) << "Setting local video description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 865 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 866 | RTC_DCHECK(content); |
| 867 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 868 | SafeSetError("Can't find video content in local description.", error_desc); |
| 869 | return false; |
| 870 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 871 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 872 | const VideoContentDescription* video = content->as_video(); |
| 873 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 874 | RtpHeaderExtensions rtp_header_extensions = |
| 875 | GetFilteredRtpHeaderExtensions(video->rtp_header_extensions()); |
| 876 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 877 | VideoRecvParameters recv_params = last_recv_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 878 | RtpParametersFromMediaDescription(video, rtp_header_extensions, &recv_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 879 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 880 | SafeSetError("Failed to set local video description recv parameters.", |
| 881 | error_desc); |
| 882 | return false; |
| 883 | } |
| 884 | for (const VideoCodec& codec : video->codecs()) { |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 885 | AddHandledPayloadType(codec.id); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 886 | } |
| 887 | last_recv_params_ = recv_params; |
| 888 | |
| 889 | // TODO(pthatcher): Move local streams into VideoSendParameters, and |
| 890 | // only give it to the media channel once we have a remote |
| 891 | // description too (without a remote description, we won't be able |
| 892 | // to send them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 893 | if (!UpdateLocalStreams_w(video->streams(), type, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 894 | SafeSetError("Failed to set local video description streams.", error_desc); |
| 895 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 898 | set_local_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 899 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 900 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 904 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 905 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 906 | TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 907 | RTC_DCHECK_RUN_ON(worker_thread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 908 | RTC_LOG(LS_INFO) << "Setting remote video description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 909 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 910 | RTC_DCHECK(content); |
| 911 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 912 | SafeSetError("Can't find video content in remote description.", error_desc); |
| 913 | return false; |
| 914 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 915 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 916 | const VideoContentDescription* video = content->as_video(); |
| 917 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 918 | RtpHeaderExtensions rtp_header_extensions = |
| 919 | GetFilteredRtpHeaderExtensions(video->rtp_header_extensions()); |
| 920 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 921 | VideoSendParameters send_params = last_send_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 922 | RtpSendParametersFromMediaDescription(video, rtp_header_extensions, |
| 923 | &send_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 924 | if (video->conference_mode()) { |
nisse | 4b4dc86 | 2016-02-17 05:25:36 -0800 | [diff] [blame] | 925 | send_params.conference_mode = true; |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 926 | } |
Steve Anton | bb50ce5 | 2018-03-26 10:24:32 -0700 | [diff] [blame] | 927 | send_params.mid = content_name(); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 928 | |
| 929 | bool parameters_applied = media_channel()->SetSendParameters(send_params); |
| 930 | |
| 931 | if (!parameters_applied) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 932 | SafeSetError("Failed to set remote video description send parameters.", |
| 933 | error_desc); |
| 934 | return false; |
| 935 | } |
| 936 | last_send_params_ = send_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 937 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 938 | // TODO(pthatcher): Move remote streams into VideoRecvParameters, |
| 939 | // and only give it to the media channel once we have a local |
| 940 | // description too (without a local description, we won't be able to |
| 941 | // recv them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 942 | if (!UpdateRemoteStreams_w(video->streams(), type, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 943 | SafeSetError("Failed to set remote video description streams.", error_desc); |
| 944 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 945 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 946 | set_remote_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 947 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 948 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 949 | } |
| 950 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 951 | RtpDataChannel::RtpDataChannel(rtc::Thread* worker_thread, |
| 952 | rtc::Thread* network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 953 | rtc::Thread* signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 954 | std::unique_ptr<DataMediaChannel> media_channel, |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 955 | const std::string& content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 956 | bool srtp_required, |
| 957 | rtc::CryptoOptions crypto_options) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 958 | : BaseChannel(worker_thread, |
| 959 | network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 960 | signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 961 | std::move(media_channel), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 962 | content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 963 | srtp_required, |
| 964 | crypto_options) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 965 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 966 | RtpDataChannel::~RtpDataChannel() { |
| 967 | TRACE_EVENT0("webrtc", "RtpDataChannel::~RtpDataChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 968 | // this can't be done in the base class, since it calls a virtual |
| 969 | DisableMedia_w(); |
Zhi Huang | 0ffe03d | 2018-03-30 13:17:42 -0700 | [diff] [blame] | 970 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 973 | void RtpDataChannel::Init_w(webrtc::RtpTransportInternal* rtp_transport) { |
| 974 | BaseChannel::Init_w(rtp_transport); |
| 975 | media_channel()->SignalDataReceived.connect(this, |
| 976 | &RtpDataChannel::OnDataReceived); |
| 977 | media_channel()->SignalReadyToSend.connect( |
| 978 | this, &RtpDataChannel::OnDataChannelReadyToSend); |
| 979 | } |
| 980 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 981 | bool RtpDataChannel::SendData(const SendDataParams& params, |
| 982 | const rtc::CopyOnWriteBuffer& payload, |
| 983 | SendDataResult* result) { |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 984 | return InvokeOnWorker<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 985 | RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params, |
| 986 | payload, result)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 987 | } |
| 988 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 989 | bool RtpDataChannel::CheckDataChannelTypeFromContent( |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 990 | const DataContentDescription* content, |
| 991 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 992 | bool is_sctp = ((content->protocol() == kMediaProtocolSctp) || |
| 993 | (content->protocol() == kMediaProtocolDtlsSctp)); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 994 | // It's been set before, but doesn't match. That's bad. |
| 995 | if (is_sctp) { |
| 996 | SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.", |
| 997 | error_desc); |
| 998 | return false; |
| 999 | } |
| 1000 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1003 | bool RtpDataChannel::SetLocalContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1004 | SdpType type, |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1005 | std::string* error_desc) { |
| 1006 | TRACE_EVENT0("webrtc", "RtpDataChannel::SetLocalContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1007 | RTC_DCHECK_RUN_ON(worker_thread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1008 | RTC_LOG(LS_INFO) << "Setting local data description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1009 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1010 | RTC_DCHECK(content); |
| 1011 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1012 | SafeSetError("Can't find data content in local description.", error_desc); |
| 1013 | return false; |
| 1014 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1015 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1016 | const DataContentDescription* data = content->as_data(); |
| 1017 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1018 | if (!CheckDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1019 | return false; |
| 1020 | } |
| 1021 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1022 | RtpHeaderExtensions rtp_header_extensions = |
| 1023 | GetFilteredRtpHeaderExtensions(data->rtp_header_extensions()); |
| 1024 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1025 | DataRecvParameters recv_params = last_recv_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1026 | RtpParametersFromMediaDescription(data, rtp_header_extensions, &recv_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1027 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 1028 | SafeSetError("Failed to set remote data description recv parameters.", |
| 1029 | error_desc); |
| 1030 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1031 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1032 | for (const DataCodec& codec : data->codecs()) { |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 1033 | AddHandledPayloadType(codec.id); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1034 | } |
| 1035 | last_recv_params_ = recv_params; |
| 1036 | |
| 1037 | // TODO(pthatcher): Move local streams into DataSendParameters, and |
| 1038 | // only give it to the media channel once we have a remote |
| 1039 | // description too (without a remote description, we won't be able |
| 1040 | // to send them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1041 | if (!UpdateLocalStreams_w(data->streams(), type, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1042 | SafeSetError("Failed to set local data description streams.", error_desc); |
| 1043 | return false; |
| 1044 | } |
| 1045 | |
| 1046 | set_local_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 1047 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1048 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1051 | bool RtpDataChannel::SetRemoteContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1052 | SdpType type, |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1053 | std::string* error_desc) { |
| 1054 | TRACE_EVENT0("webrtc", "RtpDataChannel::SetRemoteContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1055 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 1056 | RTC_LOG(LS_INFO) << "Setting remote data description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1057 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1058 | RTC_DCHECK(content); |
| 1059 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1060 | SafeSetError("Can't find data content in remote description.", error_desc); |
| 1061 | return false; |
| 1062 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1063 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1064 | const DataContentDescription* data = content->as_data(); |
| 1065 | |
Zhi Huang | 801b868 | 2017-11-15 11:36:43 -0800 | [diff] [blame] | 1066 | // If the remote data doesn't have codecs, it must be empty, so ignore it. |
| 1067 | if (!data->has_codecs()) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1068 | return true; |
| 1069 | } |
| 1070 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1071 | if (!CheckDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1072 | return false; |
| 1073 | } |
| 1074 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1075 | RtpHeaderExtensions rtp_header_extensions = |
| 1076 | GetFilteredRtpHeaderExtensions(data->rtp_header_extensions()); |
| 1077 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1078 | RTC_LOG(LS_INFO) << "Setting remote data description"; |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1079 | DataSendParameters send_params = last_send_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1080 | RtpSendParametersFromMediaDescription<DataCodec>(data, rtp_header_extensions, |
| 1081 | &send_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1082 | if (!media_channel()->SetSendParameters(send_params)) { |
| 1083 | SafeSetError("Failed to set remote data description send parameters.", |
| 1084 | error_desc); |
| 1085 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1086 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1087 | last_send_params_ = send_params; |
| 1088 | |
| 1089 | // TODO(pthatcher): Move remote streams into DataRecvParameters, |
| 1090 | // and only give it to the media channel once we have a local |
| 1091 | // description too (without a local description, we won't be able to |
| 1092 | // recv them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1093 | if (!UpdateRemoteStreams_w(data->streams(), type, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1094 | SafeSetError("Failed to set remote data description streams.", |
| 1095 | error_desc); |
| 1096 | return false; |
| 1097 | } |
| 1098 | |
| 1099 | set_remote_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 1100 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1101 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1104 | void RtpDataChannel::UpdateMediaSendRecvState_w() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1105 | // Render incoming data if we're the active call, and we have the local |
| 1106 | // content. We receive data on the default channel and multiplexed streams. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 1107 | bool recv = IsReadyToReceiveMedia_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1108 | if (!media_channel()->SetReceive(recv)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1109 | RTC_LOG(LS_ERROR) << "Failed to SetReceive on data channel"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | // Send outgoing data if we're the active call, we have the remote content, |
| 1113 | // and we have had some form of connectivity. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 1114 | bool send = IsReadyToSendMedia_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1115 | if (!media_channel()->SetSend(send)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1116 | RTC_LOG(LS_ERROR) << "Failed to SetSend on data channel"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1119 | // Trigger SignalReadyToSendData asynchronously. |
| 1120 | OnDataChannelReadyToSend(send); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1121 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1122 | RTC_LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1125 | void RtpDataChannel::OnMessage(rtc::Message* pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1126 | switch (pmsg->message_id) { |
| 1127 | case MSG_READYTOSENDDATA: { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 1128 | DataChannelReadyToSendMessageData* data = |
| 1129 | static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 1130 | ready_to_send_data_ = data->data(); |
| 1131 | SignalReadyToSendData(ready_to_send_data_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1132 | delete data; |
| 1133 | break; |
| 1134 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1135 | case MSG_DATARECEIVED: { |
| 1136 | DataReceivedMessageData* data = |
| 1137 | static_cast<DataReceivedMessageData*>(pmsg->pdata); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1138 | SignalDataReceived(data->params, data->payload); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1139 | delete data; |
| 1140 | break; |
| 1141 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1142 | default: |
| 1143 | BaseChannel::OnMessage(pmsg); |
| 1144 | break; |
| 1145 | } |
| 1146 | } |
| 1147 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1148 | void RtpDataChannel::OnDataReceived(const ReceiveDataParams& params, |
| 1149 | const char* data, |
| 1150 | size_t len) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1151 | DataReceivedMessageData* msg = new DataReceivedMessageData( |
| 1152 | params, data, len); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1153 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1156 | void RtpDataChannel::OnDataChannelReadyToSend(bool writable) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 1157 | // This is usded for congestion control to indicate that the stream is ready |
| 1158 | // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates |
| 1159 | // that the transport channel is ready. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1160 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA, |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 1161 | new DataChannelReadyToSendMessageData(writable)); |
| 1162 | } |
| 1163 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1164 | } // namespace cricket |