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 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 17 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/call/audio_sink.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "media/base/media_constants.h" |
| 20 | #include "media/base/rtp_utils.h" |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 21 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/bind.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/byte_order.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/copy_on_write_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/dscp.h" |
| 27 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "rtc_base/network_route.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 29 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "rtc_base/trace_event.h" |
Patrik Höglund | 42805f3 | 2018-01-18 19:15:38 +0000 | [diff] [blame] | 31 | // Adding 'nogncheck' to disable the gn include headers check to support modular |
| 32 | // WebRTC build targets. |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 33 | #include "media/engine/webrtc_voice_engine.h" // nogncheck |
| 34 | #include "p2p/base/packet_transport_internal.h" |
| 35 | #include "pc/channel_manager.h" |
| 36 | #include "pc/rtp_media_utils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | |
| 38 | namespace cricket { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 39 | using rtc::Bind; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 40 | using webrtc::SdpType; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 41 | |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 42 | namespace { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 43 | |
| 44 | struct SendPacketMessageData : public rtc::MessageData { |
| 45 | rtc::CopyOnWriteBuffer packet; |
| 46 | rtc::PacketOptions options; |
| 47 | }; |
| 48 | |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 49 | } // namespace |
| 50 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | enum { |
Steve Anton | 0807d15 | 2018-03-05 11:23:09 -0800 | [diff] [blame] | 52 | MSG_SEND_RTP_PACKET = 1, |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 53 | MSG_SEND_RTCP_PACKET, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | MSG_READYTOSENDDATA, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | MSG_DATARECEIVED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | MSG_FIRSTPACKETRECEIVED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 59 | static void SafeSetError(const std::string& message, std::string* error_desc) { |
| 60 | if (error_desc) { |
| 61 | *error_desc = message; |
| 62 | } |
| 63 | } |
| 64 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 65 | static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | // Check the packet size. We could check the header too if needed. |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 67 | return packet && IsValidRtpRtcpPacketSize(rtcp, packet->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 70 | template <class Codec> |
| 71 | void RtpParametersFromMediaDescription( |
| 72 | const MediaContentDescriptionImpl<Codec>* desc, |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 73 | const RtpHeaderExtensions& extensions, |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 74 | RtpParameters<Codec>* params) { |
| 75 | // 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] | 76 | // a description without codecs. Currently the ORTC implementation is relying |
| 77 | // on this. |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 78 | if (desc->has_codecs()) { |
| 79 | params->codecs = desc->codecs(); |
| 80 | } |
| 81 | // TODO(pthatcher): See if we really need |
| 82 | // rtp_header_extensions_set() and remove it if we don't. |
| 83 | if (desc->rtp_header_extensions_set()) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 84 | params->extensions = extensions; |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 85 | } |
deadbeef | 1387149 | 2015-12-09 12:37:51 -0800 | [diff] [blame] | 86 | params->rtcp.reduced_size = desc->rtcp_reduced_size(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 87 | } |
| 88 | |
nisse | 0510331 | 2016-03-16 02:22:50 -0700 | [diff] [blame] | 89 | template <class Codec> |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 90 | void RtpSendParametersFromMediaDescription( |
| 91 | const MediaContentDescriptionImpl<Codec>* desc, |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 92 | const RtpHeaderExtensions& extensions, |
nisse | 0510331 | 2016-03-16 02:22:50 -0700 | [diff] [blame] | 93 | RtpSendParameters<Codec>* send_params) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 94 | RtpParametersFromMediaDescription(desc, extensions, send_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 95 | send_params->max_bandwidth_bps = desc->bandwidth(); |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 96 | send_params->extmap_allow_mixed = desc->extmap_allow_mixed(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 99 | BaseChannel::BaseChannel(rtc::Thread* worker_thread, |
| 100 | rtc::Thread* network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 101 | rtc::Thread* signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 102 | std::unique_ptr<MediaChannel> media_channel, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 103 | const std::string& content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 104 | bool srtp_required, |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 105 | webrtc::CryptoOptions crypto_options) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 106 | : worker_thread_(worker_thread), |
| 107 | network_thread_(network_thread), |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 108 | signaling_thread_(signaling_thread), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | content_name_(content_name), |
deadbeef | 7af91dd | 2016-12-13 11:29:11 -0800 | [diff] [blame] | 110 | srtp_required_(srtp_required), |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 111 | crypto_options_(crypto_options), |
Zhi Huang | 1d88d74 | 2017-11-15 15:58:49 -0800 | [diff] [blame] | 112 | media_channel_(std::move(media_channel)) { |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 113 | RTC_DCHECK_RUN_ON(worker_thread_); |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 114 | demuxer_criteria_.mid = content_name; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 115 | RTC_LOG(LS_INFO) << "Created channel for " << content_name; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | BaseChannel::~BaseChannel() { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 119 | TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel"); |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 120 | RTC_DCHECK_RUN_ON(worker_thread_); |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 121 | |
| 122 | if (media_transport_) { |
| 123 | media_transport_->SetNetworkChangeCallback(nullptr); |
| 124 | } |
| 125 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 126 | // Eats any outstanding messages or packets. |
| 127 | worker_thread_->Clear(&invoker_); |
| 128 | worker_thread_->Clear(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 129 | // We must destroy the media channel before the transport channel, otherwise |
| 130 | // the media channel may try to send on the dead transport channel. NULLing |
| 131 | // 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] | 132 | media_channel_.reset(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 133 | RTC_LOG(LS_INFO) << "Destroyed channel: " << content_name_; |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 136 | bool BaseChannel::ConnectToRtpTransport() { |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 137 | RTC_DCHECK(rtp_transport_); |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 138 | if (!RegisterRtpDemuxerSink()) { |
| 139 | return false; |
| 140 | } |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 141 | rtp_transport_->SignalReadyToSend.connect( |
| 142 | this, &BaseChannel::OnTransportReadyToSend); |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 143 | rtp_transport_->SignalRtcpPacketReceived.connect( |
| 144 | this, &BaseChannel::OnRtcpPacketReceived); |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 145 | |
| 146 | // If media transport is used, it's responsible for providing network |
| 147 | // route changed callbacks. |
| 148 | if (!media_transport_) { |
| 149 | rtp_transport_->SignalNetworkRouteChanged.connect( |
| 150 | this, &BaseChannel::OnNetworkRouteChanged); |
| 151 | } |
| 152 | // TODO(bugs.webrtc.org/9719): Media transport should also be used to provide |
| 153 | // 'writable' state here. |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 154 | rtp_transport_->SignalWritableState.connect(this, |
| 155 | &BaseChannel::OnWritableState); |
| 156 | rtp_transport_->SignalSentPacket.connect(this, |
| 157 | &BaseChannel::SignalSentPacket_n); |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 158 | return true; |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 159 | } |
Danil Chapovalov | dae07ba | 2016-05-14 01:43:50 +0200 | [diff] [blame] | 160 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 161 | void BaseChannel::DisconnectFromRtpTransport() { |
| 162 | RTC_DCHECK(rtp_transport_); |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 163 | rtp_transport_->UnregisterRtpDemuxerSink(this); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 164 | rtp_transport_->SignalReadyToSend.disconnect(this); |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 165 | rtp_transport_->SignalRtcpPacketReceived.disconnect(this); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 166 | rtp_transport_->SignalNetworkRouteChanged.disconnect(this); |
| 167 | rtp_transport_->SignalWritableState.disconnect(this); |
| 168 | rtp_transport_->SignalSentPacket.disconnect(this); |
Danil Chapovalov | dae07ba | 2016-05-14 01:43:50 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 171 | void BaseChannel::Init_w(webrtc::RtpTransportInternal* rtp_transport, |
| 172 | webrtc::MediaTransportInterface* media_transport) { |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 173 | RTC_DCHECK_RUN_ON(worker_thread_); |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 174 | media_transport_ = media_transport; |
| 175 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 176 | network_thread_->Invoke<void>( |
| 177 | RTC_FROM_HERE, [this, rtp_transport] { SetRtpTransport(rtp_transport); }); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 178 | |
| 179 | // Both RTP and RTCP channels should be set, we can call SetInterface on |
| 180 | // the media channel and it can set network options. |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 181 | media_channel_->SetInterface(this, media_transport); |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 182 | |
| 183 | RTC_LOG(LS_INFO) << "BaseChannel::Init_w, media_transport=" |
| 184 | << (media_transport_ != nullptr); |
| 185 | if (media_transport_) { |
| 186 | media_transport_->SetNetworkChangeCallback(this); |
| 187 | } |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 188 | } |
| 189 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 190 | void BaseChannel::Deinit() { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 191 | RTC_DCHECK(worker_thread_->IsCurrent()); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 192 | media_channel_->SetInterface(/*iface=*/nullptr, |
| 193 | /*media_transport=*/nullptr); |
Danil Chapovalov | dae07ba | 2016-05-14 01:43:50 +0200 | [diff] [blame] | 194 | // Packets arrive on the network thread, processing packets calls virtual |
| 195 | // functions, so need to stop this process in Deinit that is called in |
| 196 | // derived classes destructor. |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 197 | network_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 198 | FlushRtcpMessages_n(); |
Zhi Huang | 27f3bf5 | 2018-03-26 21:37:23 -0700 | [diff] [blame] | 199 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 200 | if (rtp_transport_) { |
| 201 | DisconnectFromRtpTransport(); |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 202 | } |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 203 | // Clear pending read packets/messages. |
| 204 | network_thread_->Clear(&invoker_); |
| 205 | network_thread_->Clear(this); |
| 206 | }); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 209 | bool BaseChannel::SetRtpTransport(webrtc::RtpTransportInternal* rtp_transport) { |
| 210 | if (rtp_transport == rtp_transport_) { |
| 211 | return true; |
| 212 | } |
| 213 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 214 | if (!network_thread_->IsCurrent()) { |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 215 | return network_thread_->Invoke<bool>(RTC_FROM_HERE, [this, rtp_transport] { |
| 216 | return SetRtpTransport(rtp_transport); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 217 | }); |
| 218 | } |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 219 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 220 | if (rtp_transport_) { |
| 221 | DisconnectFromRtpTransport(); |
| 222 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 223 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 224 | rtp_transport_ = rtp_transport; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 225 | if (rtp_transport_) { |
| 226 | RTC_DCHECK(rtp_transport_->rtp_packet_transport()); |
| 227 | transport_name_ = rtp_transport_->rtp_packet_transport()->transport_name(); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 228 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 229 | if (!ConnectToRtpTransport()) { |
| 230 | RTC_LOG(LS_ERROR) << "Failed to connect to the new RtpTransport."; |
| 231 | return false; |
| 232 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 233 | OnTransportReadyToSend(rtp_transport_->IsReadyToSend()); |
| 234 | UpdateWritableState_n(); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 235 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 236 | // Set the cached socket options. |
| 237 | for (const auto& pair : socket_options_) { |
| 238 | rtp_transport_->rtp_packet_transport()->SetOption(pair.first, |
| 239 | pair.second); |
| 240 | } |
| 241 | if (rtp_transport_->rtcp_packet_transport()) { |
| 242 | for (const auto& pair : rtcp_socket_options_) { |
| 243 | rtp_transport_->rtp_packet_transport()->SetOption(pair.first, |
| 244 | pair.second); |
| 245 | } |
| 246 | } |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 247 | } |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 248 | return true; |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 249 | } |
| 250 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 251 | bool BaseChannel::Enable(bool enable) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 252 | worker_thread_->Invoke<void>( |
| 253 | RTC_FROM_HERE, |
| 254 | Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w, |
| 255 | this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 256 | return true; |
| 257 | } |
| 258 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 259 | bool BaseChannel::SetLocalContent(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 260 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 261 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 262 | TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent"); |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 263 | return InvokeOnWorker<bool>( |
| 264 | RTC_FROM_HERE, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 265 | Bind(&BaseChannel::SetLocalContent_w, this, content, type, error_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | bool BaseChannel::SetRemoteContent(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 269 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 270 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 271 | TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent"); |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 272 | return InvokeOnWorker<bool>( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 273 | RTC_FROM_HERE, |
| 274 | Bind(&BaseChannel::SetRemoteContent_w, this, content, type, error_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 277 | bool BaseChannel::IsReadyToReceiveMedia_w() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 278 | // Receive data if we are enabled and have local content, |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 279 | return enabled() && |
| 280 | webrtc::RtpTransceiverDirectionHasRecv(local_content_direction_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 283 | bool BaseChannel::IsReadyToSendMedia_w() const { |
| 284 | // Need to access some state updated on the network thread. |
| 285 | return network_thread_->Invoke<bool>( |
| 286 | RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this)); |
| 287 | } |
| 288 | |
| 289 | bool BaseChannel::IsReadyToSendMedia_n() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 290 | // Send outgoing data if we are enabled, have local and remote content, |
| 291 | // and we have had some form of connectivity. |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 292 | return enabled() && |
| 293 | webrtc::RtpTransceiverDirectionHasRecv(remote_content_direction_) && |
| 294 | webrtc::RtpTransceiverDirectionHasSend(local_content_direction_) && |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 295 | was_ever_writable(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 296 | } |
| 297 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 298 | bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 299 | const rtc::PacketOptions& options) { |
| 300 | return SendPacket(false, packet, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 301 | } |
| 302 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 303 | bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 304 | const rtc::PacketOptions& options) { |
| 305 | return SendPacket(true, packet, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 308 | int BaseChannel::SetOption(SocketType type, |
| 309 | rtc::Socket::Option opt, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 310 | int value) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 311 | return network_thread_->Invoke<int>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 312 | RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | int BaseChannel::SetOption_n(SocketType type, |
| 316 | rtc::Socket::Option opt, |
| 317 | int value) { |
| 318 | RTC_DCHECK(network_thread_->IsCurrent()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 319 | RTC_DCHECK(rtp_transport_); |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 320 | rtc::PacketTransportInternal* transport = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 321 | switch (type) { |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 322 | case ST_RTP: |
zstein | e8ab543 | 2017-07-12 11:48:11 -0700 | [diff] [blame] | 323 | transport = rtp_transport_->rtp_packet_transport(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 324 | socket_options_.push_back( |
| 325 | std::pair<rtc::Socket::Option, int>(opt, value)); |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 326 | break; |
| 327 | case ST_RTCP: |
zstein | e8ab543 | 2017-07-12 11:48:11 -0700 | [diff] [blame] | 328 | transport = rtp_transport_->rtcp_packet_transport(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 329 | rtcp_socket_options_.push_back( |
| 330 | std::pair<rtc::Socket::Option, int>(opt, value)); |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 331 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 332 | } |
deadbeef | f534659 | 2017-01-24 21:51:21 -0800 | [diff] [blame] | 333 | return transport ? transport->SetOption(opt, value) : -1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 336 | void BaseChannel::OnWritableState(bool writable) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 337 | RTC_DCHECK(network_thread_->IsCurrent()); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 338 | if (writable) { |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 339 | ChannelWritable_n(); |
| 340 | } else { |
| 341 | ChannelNotWritable_n(); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 345 | void BaseChannel::OnNetworkRouteChanged( |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 346 | absl::optional<rtc::NetworkRoute> network_route) { |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 347 | RTC_LOG(LS_INFO) << "Network route was changed."; |
| 348 | |
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 | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 433 | void BaseChannel::OnRtpPacket(const webrtc::RtpPacketReceived& parsed_packet) { |
Niels Möller | 29e13fd | 2018-12-17 12:35:30 +0100 | [diff] [blame] | 434 | // Take packet time from the |parsed_packet|. |
| 435 | // RtpPacketReceived.arrival_time_ms = (timestamp_us + 500) / 1000; |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 436 | int64_t timestamp_us = -1; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 437 | if (parsed_packet.arrival_time_ms() > 0) { |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 438 | timestamp_us = parsed_packet.arrival_time_ms() * 1000; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 439 | } |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 440 | |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 441 | OnPacketReceived(/*rtcp=*/false, parsed_packet.Buffer(), timestamp_us); |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void BaseChannel::UpdateRtpHeaderExtensionMap( |
| 445 | const RtpHeaderExtensions& header_extensions) { |
| 446 | RTC_DCHECK(rtp_transport_); |
| 447 | // Update the header extension map on network thread in case there is data |
| 448 | // race. |
| 449 | // TODO(zhihuang): Add an rtc::ThreadChecker make sure to RtpTransport won't |
| 450 | // be accessed from different threads. |
| 451 | // |
| 452 | // NOTE: This doesn't take the BUNDLE case in account meaning the RTP header |
| 453 | // extension maps are not merged when BUNDLE is enabled. This is fine because |
| 454 | // the ID for MID should be consistent among all the RTP transports. |
| 455 | network_thread_->Invoke<void>(RTC_FROM_HERE, [this, &header_extensions] { |
| 456 | rtp_transport_->UpdateRtpHeaderExtensionMap(header_extensions); |
| 457 | }); |
| 458 | } |
| 459 | |
| 460 | bool BaseChannel::RegisterRtpDemuxerSink() { |
| 461 | RTC_DCHECK(rtp_transport_); |
| 462 | return network_thread_->Invoke<bool>(RTC_FROM_HERE, [this] { |
| 463 | return rtp_transport_->RegisterRtpDemuxerSink(demuxer_criteria_, this); |
| 464 | }); |
| 465 | } |
| 466 | |
| 467 | void BaseChannel::OnRtcpPacketReceived(rtc::CopyOnWriteBuffer* packet, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 468 | int64_t packet_time_us) { |
| 469 | OnPacketReceived(/*rtcp=*/true, *packet, packet_time_us); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 470 | } |
| 471 | |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 472 | void BaseChannel::OnPacketReceived(bool rtcp, |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 473 | const rtc::CopyOnWriteBuffer& packet, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 474 | int64_t packet_time_us) { |
honghaiz@google.com | a67ca1a | 2015-01-28 19:48:33 +0000 | [diff] [blame] | 475 | if (!has_received_packet_ && !rtcp) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 476 | has_received_packet_ = true; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 477 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 480 | if (!srtp_active() && srtp_required_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 481 | // Our session description indicates that SRTP is required, but we got a |
| 482 | // packet before our SRTP filter is active. This means either that |
| 483 | // a) we got SRTP packets before we received the SDES keys, in which case |
| 484 | // we can't decrypt it anyway, or |
| 485 | // 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] | 486 | // transports, so we haven't yet extracted keys, even if DTLS did |
| 487 | // complete on the transport that the packets are being sent on. It's |
| 488 | // really good practice to wait for both RTP and RTCP to be good to go |
| 489 | // before sending media, to prevent weird failure modes, so it's fine |
| 490 | // for us to just eat packets here. This is all sidestepped if RTCP mux |
| 491 | // is used anyway. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 492 | RTC_LOG(LS_WARNING) |
| 493 | << "Can't process incoming " << RtpRtcpStringLiteral(rtcp) |
| 494 | << " packet when SRTP is inactive and crypto is required"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 498 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 499 | RTC_FROM_HERE, worker_thread_, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 500 | Bind(&BaseChannel::ProcessPacket, this, rtcp, packet, packet_time_us)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 501 | } |
| 502 | |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 503 | void BaseChannel::ProcessPacket(bool rtcp, |
| 504 | const rtc::CopyOnWriteBuffer& packet, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 505 | int64_t packet_time_us) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 506 | RTC_DCHECK(worker_thread_->IsCurrent()); |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 507 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 508 | // Need to copy variable because OnRtcpReceived/OnPacketReceived |
| 509 | // requires non-const pointer to buffer. This doesn't memcpy the actual data. |
| 510 | rtc::CopyOnWriteBuffer data(packet); |
| 511 | if (rtcp) { |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 512 | media_channel_->OnRtcpReceived(&data, packet_time_us); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 513 | } else { |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 514 | media_channel_->OnPacketReceived(&data, packet_time_us); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 518 | void BaseChannel::EnableMedia_w() { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 519 | RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 520 | if (enabled_) |
| 521 | return; |
| 522 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 523 | RTC_LOG(LS_INFO) << "Channel enabled"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 524 | enabled_ = true; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 525 | UpdateMediaSendRecvState_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | void BaseChannel::DisableMedia_w() { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 529 | RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 530 | if (!enabled_) |
| 531 | return; |
| 532 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 533 | RTC_LOG(LS_INFO) << "Channel disabled"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 534 | enabled_ = false; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 535 | UpdateMediaSendRecvState_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 538 | void BaseChannel::UpdateWritableState_n() { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 539 | if (rtp_transport_->IsWritable(/*rtcp=*/true) && |
| 540 | rtp_transport_->IsWritable(/*rtcp=*/false)) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 541 | ChannelWritable_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 542 | } else { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 543 | ChannelNotWritable_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 547 | void BaseChannel::ChannelWritable_n() { |
| 548 | RTC_DCHECK(network_thread_->IsCurrent()); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 549 | if (writable_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 550 | return; |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 551 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 552 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 553 | RTC_LOG(LS_INFO) << "Channel writable (" << content_name_ << ")" |
| 554 | << (was_ever_writable_ ? "" : " for the first time"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 555 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 556 | was_ever_writable_ = true; |
| 557 | writable_ = true; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 558 | UpdateMediaSendRecvState(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 561 | void BaseChannel::ChannelNotWritable_n() { |
| 562 | RTC_DCHECK(network_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 563 | if (!writable_) |
| 564 | return; |
| 565 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 566 | RTC_LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 567 | writable_ = false; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 568 | UpdateMediaSendRecvState(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 569 | } |
| 570 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 571 | bool BaseChannel::AddRecvStream_w(const StreamParams& sp) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 572 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
pbos | 482b12e | 2015-11-16 10:19:58 -0800 | [diff] [blame] | 573 | return media_channel()->AddRecvStream(sp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 576 | bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 577 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 578 | return media_channel()->RemoveRecvStream(ssrc); |
| 579 | } |
| 580 | |
| 581 | bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 582 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 583 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 584 | // Check for streams that have been removed. |
| 585 | bool ret = true; |
Steve Anton | 5f8b5fd | 2018-12-27 16:58:10 -0800 | [diff] [blame] | 586 | for (const StreamParams& old_stream : local_streams_) { |
| 587 | if (old_stream.has_ssrcs() && |
| 588 | !GetStreamBySsrc(streams, old_stream.first_ssrc())) { |
| 589 | if (!media_channel()->RemoveSendStream(old_stream.first_ssrc())) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 590 | rtc::StringBuilder desc; |
Steve Anton | 5f8b5fd | 2018-12-27 16:58:10 -0800 | [diff] [blame] | 591 | desc << "Failed to remove send stream with ssrc " |
| 592 | << old_stream.first_ssrc() << "."; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 593 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 594 | ret = false; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | // Check for new streams. |
Steve Anton | 5f8b5fd | 2018-12-27 16:58:10 -0800 | [diff] [blame] | 599 | for (const StreamParams& new_stream : streams) { |
| 600 | if (new_stream.has_ssrcs() && |
| 601 | !GetStreamBySsrc(local_streams_, new_stream.first_ssrc())) { |
| 602 | if (media_channel()->AddSendStream(new_stream)) { |
| 603 | RTC_LOG(LS_INFO) << "Add send stream ssrc: " << new_stream.ssrcs[0]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 604 | } else { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 605 | rtc::StringBuilder desc; |
Steve Anton | 5f8b5fd | 2018-12-27 16:58:10 -0800 | [diff] [blame] | 606 | desc << "Failed to add send stream ssrc: " << new_stream.first_ssrc(); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 607 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 608 | ret = false; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | local_streams_ = streams; |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | bool BaseChannel::UpdateRemoteStreams_w( |
| 617 | const std::vector<StreamParams>& streams, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 618 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 619 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 620 | // Check for streams that have been removed. |
| 621 | bool ret = true; |
Steve Anton | 5f8b5fd | 2018-12-27 16:58:10 -0800 | [diff] [blame] | 622 | for (const StreamParams& old_stream : remote_streams_) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 623 | // If we no longer have an unsignaled stream, we would like to remove |
| 624 | // the unsignaled stream params that are cached. |
Steve Anton | 5f8b5fd | 2018-12-27 16:58:10 -0800 | [diff] [blame] | 625 | if ((!old_stream.has_ssrcs() && !HasStreamWithNoSsrcs(streams)) || |
| 626 | !GetStreamBySsrc(streams, old_stream.first_ssrc())) { |
| 627 | if (RemoveRecvStream_w(old_stream.first_ssrc())) { |
| 628 | RTC_LOG(LS_INFO) << "Remove remote ssrc: " << old_stream.first_ssrc(); |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 629 | } else { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 630 | rtc::StringBuilder desc; |
Steve Anton | 5f8b5fd | 2018-12-27 16:58:10 -0800 | [diff] [blame] | 631 | desc << "Failed to remove remote stream with ssrc " |
| 632 | << old_stream.first_ssrc() << "."; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 633 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 634 | ret = false; |
| 635 | } |
| 636 | } |
| 637 | } |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 638 | demuxer_criteria_.ssrcs.clear(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 639 | // Check for new streams. |
Steve Anton | 5f8b5fd | 2018-12-27 16:58:10 -0800 | [diff] [blame] | 640 | for (const StreamParams& new_stream : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 641 | // We allow a StreamParams with an empty list of SSRCs, in which case the |
| 642 | // MediaChannel will cache the parameters and use them for any unsignaled |
| 643 | // stream received later. |
Steve Anton | 5f8b5fd | 2018-12-27 16:58:10 -0800 | [diff] [blame] | 644 | if ((!new_stream.has_ssrcs() && !HasStreamWithNoSsrcs(remote_streams_)) || |
| 645 | !GetStreamBySsrc(remote_streams_, new_stream.first_ssrc())) { |
| 646 | if (AddRecvStream_w(new_stream)) { |
| 647 | RTC_LOG(LS_INFO) << "Add remote ssrc: " << new_stream.first_ssrc(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 648 | } else { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 649 | rtc::StringBuilder desc; |
Steve Anton | 5f8b5fd | 2018-12-27 16:58:10 -0800 | [diff] [blame] | 650 | desc << "Failed to add remote stream ssrc: " << new_stream.first_ssrc(); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 651 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 652 | ret = false; |
| 653 | } |
| 654 | } |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 655 | // Update the receiving SSRCs. |
Steve Anton | 5f8b5fd | 2018-12-27 16:58:10 -0800 | [diff] [blame] | 656 | demuxer_criteria_.ssrcs.insert(new_stream.ssrcs.begin(), |
| 657 | new_stream.ssrcs.end()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 658 | } |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 659 | // Re-register the sink to update the receiving ssrcs. |
| 660 | RegisterRtpDemuxerSink(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 661 | remote_streams_ = streams; |
| 662 | return ret; |
| 663 | } |
| 664 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 665 | RtpHeaderExtensions BaseChannel::GetFilteredRtpHeaderExtensions( |
| 666 | const RtpHeaderExtensions& extensions) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 667 | RTC_DCHECK(rtp_transport_); |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 668 | if (crypto_options_.srtp.enable_encrypted_rtp_header_extensions) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 669 | RtpHeaderExtensions filtered; |
| 670 | auto pred = [](const webrtc::RtpExtension& extension) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 671 | return !extension.encrypt; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 672 | }; |
| 673 | std::copy_if(extensions.begin(), extensions.end(), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 674 | std::back_inserter(filtered), pred); |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 675 | return filtered; |
| 676 | } |
| 677 | |
| 678 | return webrtc::RtpExtension::FilterDuplicateNonEncrypted(extensions); |
| 679 | } |
| 680 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 681 | void BaseChannel::OnMessage(rtc::Message* pmsg) { |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 682 | TRACE_EVENT0("webrtc", "BaseChannel::OnMessage"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 683 | switch (pmsg->message_id) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 684 | case MSG_SEND_RTP_PACKET: |
| 685 | case MSG_SEND_RTCP_PACKET: { |
| 686 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 687 | SendPacketMessageData* data = |
| 688 | static_cast<SendPacketMessageData*>(pmsg->pdata); |
| 689 | bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET; |
| 690 | SendPacket(rtcp, &data->packet, data->options); |
| 691 | delete data; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 692 | break; |
| 693 | } |
| 694 | case MSG_FIRSTPACKETRECEIVED: { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 695 | SignalFirstPacketReceived_(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 696 | break; |
| 697 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 701 | void BaseChannel::AddHandledPayloadType(int payload_type) { |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 702 | demuxer_criteria_.payload_types.insert(static_cast<uint8_t>(payload_type)); |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 705 | void BaseChannel::FlushRtcpMessages_n() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 706 | // Flush all remaining RTCP messages. This should only be called in |
| 707 | // destructor. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 708 | RTC_DCHECK(network_thread_->IsCurrent()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 709 | rtc::MessageList rtcp_messages; |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 710 | network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages); |
| 711 | for (const auto& message : rtcp_messages) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 712 | network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET, |
| 713 | message.pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 717 | void BaseChannel::SignalSentPacket_n(const rtc::SentPacket& sent_packet) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 718 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 719 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 720 | RTC_FROM_HERE, worker_thread_, |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 721 | rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet)); |
| 722 | } |
| 723 | |
| 724 | void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 725 | RTC_DCHECK(worker_thread_->IsCurrent()); |
| 726 | SignalSentPacket(sent_packet); |
| 727 | } |
| 728 | |
| 729 | VoiceChannel::VoiceChannel(rtc::Thread* worker_thread, |
| 730 | rtc::Thread* network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 731 | rtc::Thread* signaling_thread, |
Niels Möller | f120cba | 2018-01-30 09:33:03 +0100 | [diff] [blame] | 732 | // TODO(nisse): Delete unused argument. |
| 733 | MediaEngineInterface* /* media_engine */, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 734 | std::unique_ptr<VoiceMediaChannel> media_channel, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 735 | const std::string& content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 736 | bool srtp_required, |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 737 | webrtc::CryptoOptions crypto_options) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 738 | : BaseChannel(worker_thread, |
| 739 | network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 740 | signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 741 | std::move(media_channel), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 742 | content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 743 | srtp_required, |
| 744 | crypto_options) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 745 | |
| 746 | VoiceChannel::~VoiceChannel() { |
Piotr (Peter) Slatala | 309aafe | 2019-01-15 14:24:34 -0800 | [diff] [blame^] | 747 | if (media_transport()) { |
| 748 | media_transport()->SetFirstAudioPacketReceivedObserver(nullptr); |
| 749 | } |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 750 | TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 751 | // this can't be done in the base class, since it calls a virtual |
| 752 | DisableMedia_w(); |
Zhi Huang | 0ffe03d | 2018-03-30 13:17:42 -0700 | [diff] [blame] | 753 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 756 | void BaseChannel::UpdateMediaSendRecvState() { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 757 | RTC_DCHECK(network_thread_->IsCurrent()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 758 | invoker_.AsyncInvoke<void>( |
| 759 | RTC_FROM_HERE, worker_thread_, |
| 760 | Bind(&BaseChannel::UpdateMediaSendRecvState_w, this)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 761 | } |
| 762 | |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 763 | void BaseChannel::OnNetworkRouteChanged( |
| 764 | const rtc::NetworkRoute& network_route) { |
| 765 | OnNetworkRouteChanged(absl::make_optional(network_route)); |
| 766 | } |
| 767 | |
Piotr (Peter) Slatala | 309aafe | 2019-01-15 14:24:34 -0800 | [diff] [blame^] | 768 | void VoiceChannel::Init_w(webrtc::RtpTransportInternal* rtp_transport, |
| 769 | webrtc::MediaTransportInterface* media_transport) { |
| 770 | BaseChannel::Init_w(rtp_transport, media_transport); |
| 771 | if (BaseChannel::media_transport()) { |
| 772 | this->media_transport()->SetFirstAudioPacketReceivedObserver(this); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | void VoiceChannel::OnFirstAudioPacketReceived(int64_t channel_id) { |
| 777 | has_received_packet_ = true; |
| 778 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED); |
| 779 | } |
| 780 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 781 | void VoiceChannel::UpdateMediaSendRecvState_w() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 782 | // Render incoming data if we're the active call, and we have the local |
| 783 | // content. We receive data on the default channel and multiplexed streams. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 784 | bool recv = IsReadyToReceiveMedia_w(); |
solenberg | 5b14b42 | 2015-10-01 04:10:31 -0700 | [diff] [blame] | 785 | media_channel()->SetPlayout(recv); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 786 | |
| 787 | // Send outgoing data if we're the active call, we have the remote content, |
| 788 | // and we have had some form of connectivity. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 789 | bool send = IsReadyToSendMedia_w(); |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 790 | media_channel()->SetSend(send); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 791 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 792 | RTC_LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 793 | } |
| 794 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 795 | bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 796 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 797 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 798 | TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 799 | RTC_DCHECK_RUN_ON(worker_thread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 800 | RTC_LOG(LS_INFO) << "Setting local voice description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 801 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 802 | RTC_DCHECK(content); |
| 803 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 804 | SafeSetError("Can't find audio content in local description.", error_desc); |
| 805 | return false; |
| 806 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 807 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 808 | const AudioContentDescription* audio = content->as_audio(); |
| 809 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 810 | RtpHeaderExtensions rtp_header_extensions = |
| 811 | GetFilteredRtpHeaderExtensions(audio->rtp_header_extensions()); |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 812 | UpdateRtpHeaderExtensionMap(rtp_header_extensions); |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 813 | media_channel()->SetExtmapAllowMixed(audio->extmap_allow_mixed()); |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 814 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 815 | AudioRecvParameters recv_params = last_recv_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 816 | RtpParametersFromMediaDescription(audio, rtp_header_extensions, &recv_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 817 | if (!media_channel()->SetRecvParameters(recv_params)) { |
Peter Thatcher | bfab5cb | 2015-08-20 17:40:24 -0700 | [diff] [blame] | 818 | SafeSetError("Failed to set local audio description recv parameters.", |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 819 | error_desc); |
| 820 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 821 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 822 | for (const AudioCodec& codec : audio->codecs()) { |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 823 | AddHandledPayloadType(codec.id); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 824 | } |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 825 | // Need to re-register the sink to update the handled payload. |
| 826 | if (!RegisterRtpDemuxerSink()) { |
| 827 | RTC_LOG(LS_ERROR) << "Failed to set up audio demuxing."; |
| 828 | return false; |
| 829 | } |
| 830 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 831 | last_recv_params_ = recv_params; |
| 832 | |
| 833 | // TODO(pthatcher): Move local streams into AudioSendParameters, and |
| 834 | // only give it to the media channel once we have a remote |
| 835 | // description too (without a remote description, we won't be able |
| 836 | // to send them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 837 | if (!UpdateLocalStreams_w(audio->streams(), type, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 838 | SafeSetError("Failed to set local audio description streams.", error_desc); |
| 839 | return false; |
| 840 | } |
| 841 | |
| 842 | set_local_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 843 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 844 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 848 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 849 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 850 | TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 851 | RTC_DCHECK_RUN_ON(worker_thread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 852 | RTC_LOG(LS_INFO) << "Setting remote voice description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 853 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 854 | RTC_DCHECK(content); |
| 855 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 856 | SafeSetError("Can't find audio content in remote description.", error_desc); |
| 857 | return false; |
| 858 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 859 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 860 | const AudioContentDescription* audio = content->as_audio(); |
| 861 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 862 | RtpHeaderExtensions rtp_header_extensions = |
| 863 | GetFilteredRtpHeaderExtensions(audio->rtp_header_extensions()); |
| 864 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 865 | AudioSendParameters send_params = last_send_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 866 | RtpSendParametersFromMediaDescription(audio, rtp_header_extensions, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 867 | &send_params); |
Steve Anton | bb50ce5 | 2018-03-26 10:24:32 -0700 | [diff] [blame] | 868 | send_params.mid = content_name(); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 869 | |
| 870 | bool parameters_applied = media_channel()->SetSendParameters(send_params); |
| 871 | if (!parameters_applied) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 872 | SafeSetError("Failed to set remote audio description send parameters.", |
| 873 | error_desc); |
| 874 | return false; |
| 875 | } |
| 876 | last_send_params_ = send_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 877 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 878 | // TODO(pthatcher): Move remote streams into AudioRecvParameters, |
| 879 | // and only give it to the media channel once we have a local |
| 880 | // description too (without a local description, we won't be able to |
| 881 | // recv them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 882 | if (!UpdateRemoteStreams_w(audio->streams(), type, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 883 | SafeSetError("Failed to set remote audio description streams.", error_desc); |
| 884 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 887 | set_remote_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 888 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 889 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 892 | VideoChannel::VideoChannel(rtc::Thread* worker_thread, |
| 893 | rtc::Thread* network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 894 | rtc::Thread* signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 895 | std::unique_ptr<VideoMediaChannel> media_channel, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 896 | const std::string& content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 897 | bool srtp_required, |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 898 | webrtc::CryptoOptions crypto_options) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 899 | : BaseChannel(worker_thread, |
| 900 | network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 901 | signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 902 | std::move(media_channel), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 903 | content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 904 | srtp_required, |
| 905 | crypto_options) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 906 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 907 | VideoChannel::~VideoChannel() { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 908 | TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 909 | // this can't be done in the base class, since it calls a virtual |
| 910 | DisableMedia_w(); |
Zhi Huang | 0ffe03d | 2018-03-30 13:17:42 -0700 | [diff] [blame] | 911 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 914 | void VideoChannel::UpdateMediaSendRecvState_w() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 915 | // Send outgoing data if we're the active call, we have the remote content, |
| 916 | // and we have had some form of connectivity. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 917 | bool send = IsReadyToSendMedia_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 918 | if (!media_channel()->SetSend(send)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 919 | RTC_LOG(LS_ERROR) << "Failed to SetSend on video channel"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 920 | // TODO(gangji): Report error back to server. |
| 921 | } |
| 922 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 923 | RTC_LOG(LS_INFO) << "Changing video state, send=" << send; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 924 | } |
| 925 | |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 926 | void VideoChannel::FillBitrateInfo(BandwidthEstimationInfo* bwe_info) { |
| 927 | InvokeOnWorker<void>(RTC_FROM_HERE, Bind(&VideoMediaChannel::FillBitrateInfo, |
| 928 | media_channel(), bwe_info)); |
| 929 | } |
| 930 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 931 | bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 932 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 933 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 934 | TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 935 | RTC_DCHECK_RUN_ON(worker_thread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 936 | RTC_LOG(LS_INFO) << "Setting local video description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 937 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 938 | RTC_DCHECK(content); |
| 939 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 940 | SafeSetError("Can't find video content in local description.", error_desc); |
| 941 | return false; |
| 942 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 943 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 944 | const VideoContentDescription* video = content->as_video(); |
| 945 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 946 | RtpHeaderExtensions rtp_header_extensions = |
| 947 | GetFilteredRtpHeaderExtensions(video->rtp_header_extensions()); |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 948 | UpdateRtpHeaderExtensionMap(rtp_header_extensions); |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 949 | media_channel()->SetExtmapAllowMixed(video->extmap_allow_mixed()); |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 950 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 951 | VideoRecvParameters recv_params = last_recv_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 952 | RtpParametersFromMediaDescription(video, rtp_header_extensions, &recv_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 953 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 954 | SafeSetError("Failed to set local video description recv parameters.", |
| 955 | error_desc); |
| 956 | return false; |
| 957 | } |
| 958 | for (const VideoCodec& codec : video->codecs()) { |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 959 | AddHandledPayloadType(codec.id); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 960 | } |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 961 | // Need to re-register the sink to update the handled payload. |
| 962 | if (!RegisterRtpDemuxerSink()) { |
| 963 | RTC_LOG(LS_ERROR) << "Failed to set up video demuxing."; |
| 964 | return false; |
| 965 | } |
| 966 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 967 | last_recv_params_ = recv_params; |
| 968 | |
| 969 | // TODO(pthatcher): Move local streams into VideoSendParameters, and |
| 970 | // only give it to the media channel once we have a remote |
| 971 | // description too (without a remote description, we won't be able |
| 972 | // to send them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 973 | if (!UpdateLocalStreams_w(video->streams(), type, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 974 | SafeSetError("Failed to set local video description streams.", error_desc); |
| 975 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 978 | set_local_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 979 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 980 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 984 | SdpType type, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 985 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 986 | TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 987 | RTC_DCHECK_RUN_ON(worker_thread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 988 | RTC_LOG(LS_INFO) << "Setting remote video description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 989 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 990 | RTC_DCHECK(content); |
| 991 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 992 | SafeSetError("Can't find video content in remote description.", error_desc); |
| 993 | return false; |
| 994 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 995 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 996 | const VideoContentDescription* video = content->as_video(); |
| 997 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 998 | RtpHeaderExtensions rtp_header_extensions = |
| 999 | GetFilteredRtpHeaderExtensions(video->rtp_header_extensions()); |
| 1000 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1001 | VideoSendParameters send_params = last_send_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1002 | RtpSendParametersFromMediaDescription(video, rtp_header_extensions, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1003 | &send_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1004 | if (video->conference_mode()) { |
nisse | 4b4dc86 | 2016-02-17 05:25:36 -0800 | [diff] [blame] | 1005 | send_params.conference_mode = true; |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1006 | } |
Steve Anton | bb50ce5 | 2018-03-26 10:24:32 -0700 | [diff] [blame] | 1007 | send_params.mid = content_name(); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1008 | |
| 1009 | bool parameters_applied = media_channel()->SetSendParameters(send_params); |
| 1010 | |
| 1011 | if (!parameters_applied) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1012 | SafeSetError("Failed to set remote video description send parameters.", |
| 1013 | error_desc); |
| 1014 | return false; |
| 1015 | } |
| 1016 | last_send_params_ = send_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1017 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1018 | // TODO(pthatcher): Move remote streams into VideoRecvParameters, |
| 1019 | // and only give it to the media channel once we have a local |
| 1020 | // description too (without a local description, we won't be able to |
| 1021 | // recv them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1022 | if (!UpdateRemoteStreams_w(video->streams(), type, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1023 | SafeSetError("Failed to set remote video description streams.", error_desc); |
| 1024 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1025 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1026 | set_remote_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 1027 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1028 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1031 | RtpDataChannel::RtpDataChannel(rtc::Thread* worker_thread, |
| 1032 | rtc::Thread* network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1033 | rtc::Thread* signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 1034 | std::unique_ptr<DataMediaChannel> media_channel, |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1035 | const std::string& content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1036 | bool srtp_required, |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 1037 | webrtc::CryptoOptions crypto_options) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1038 | : BaseChannel(worker_thread, |
| 1039 | network_thread, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1040 | signaling_thread, |
Steve Anton | 8699a32 | 2017-11-06 15:53:33 -0800 | [diff] [blame] | 1041 | std::move(media_channel), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1042 | content_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1043 | srtp_required, |
| 1044 | crypto_options) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1045 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1046 | RtpDataChannel::~RtpDataChannel() { |
| 1047 | TRACE_EVENT0("webrtc", "RtpDataChannel::~RtpDataChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1048 | // this can't be done in the base class, since it calls a virtual |
| 1049 | DisableMedia_w(); |
Zhi Huang | 0ffe03d | 2018-03-30 13:17:42 -0700 | [diff] [blame] | 1050 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Piotr (Peter) Slatala | 309aafe | 2019-01-15 14:24:34 -0800 | [diff] [blame^] | 1053 | void RtpDataChannel::Init_w(webrtc::RtpTransportInternal* rtp_transport, |
| 1054 | webrtc::MediaTransportInterface* media_transport) { |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 1055 | BaseChannel::Init_w(rtp_transport, /*media_transport=*/nullptr); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 1056 | media_channel()->SignalDataReceived.connect(this, |
| 1057 | &RtpDataChannel::OnDataReceived); |
| 1058 | media_channel()->SignalReadyToSend.connect( |
| 1059 | this, &RtpDataChannel::OnDataChannelReadyToSend); |
| 1060 | } |
| 1061 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1062 | bool RtpDataChannel::SendData(const SendDataParams& params, |
| 1063 | const rtc::CopyOnWriteBuffer& payload, |
| 1064 | SendDataResult* result) { |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1065 | return InvokeOnWorker<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1066 | RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params, |
| 1067 | payload, result)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1070 | bool RtpDataChannel::CheckDataChannelTypeFromContent( |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1071 | const DataContentDescription* content, |
| 1072 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1073 | bool is_sctp = ((content->protocol() == kMediaProtocolSctp) || |
| 1074 | (content->protocol() == kMediaProtocolDtlsSctp)); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1075 | // It's been set before, but doesn't match. That's bad. |
| 1076 | if (is_sctp) { |
| 1077 | SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.", |
| 1078 | error_desc); |
| 1079 | return false; |
| 1080 | } |
| 1081 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1084 | bool RtpDataChannel::SetLocalContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1085 | SdpType type, |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1086 | std::string* error_desc) { |
| 1087 | TRACE_EVENT0("webrtc", "RtpDataChannel::SetLocalContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1088 | RTC_DCHECK_RUN_ON(worker_thread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1089 | RTC_LOG(LS_INFO) << "Setting local data description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1090 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1091 | RTC_DCHECK(content); |
| 1092 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1093 | SafeSetError("Can't find data content in local description.", error_desc); |
| 1094 | return false; |
| 1095 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1096 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1097 | const DataContentDescription* data = content->as_data(); |
| 1098 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1099 | if (!CheckDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1100 | return false; |
| 1101 | } |
| 1102 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1103 | RtpHeaderExtensions rtp_header_extensions = |
| 1104 | GetFilteredRtpHeaderExtensions(data->rtp_header_extensions()); |
| 1105 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1106 | DataRecvParameters recv_params = last_recv_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1107 | RtpParametersFromMediaDescription(data, rtp_header_extensions, &recv_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1108 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 1109 | SafeSetError("Failed to set remote data description recv parameters.", |
| 1110 | error_desc); |
| 1111 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1112 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1113 | for (const DataCodec& codec : data->codecs()) { |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 1114 | AddHandledPayloadType(codec.id); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1115 | } |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 1116 | // Need to re-register the sink to update the handled payload. |
| 1117 | if (!RegisterRtpDemuxerSink()) { |
| 1118 | RTC_LOG(LS_ERROR) << "Failed to set up data demuxing."; |
| 1119 | return false; |
| 1120 | } |
| 1121 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1122 | last_recv_params_ = recv_params; |
| 1123 | |
| 1124 | // TODO(pthatcher): Move local streams into DataSendParameters, and |
| 1125 | // only give it to the media channel once we have a remote |
| 1126 | // description too (without a remote description, we won't be able |
| 1127 | // to send them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1128 | if (!UpdateLocalStreams_w(data->streams(), type, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1129 | SafeSetError("Failed to set local data description streams.", error_desc); |
| 1130 | return false; |
| 1131 | } |
| 1132 | |
| 1133 | set_local_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 1134 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1135 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1138 | bool RtpDataChannel::SetRemoteContent_w(const MediaContentDescription* content, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1139 | SdpType type, |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1140 | std::string* error_desc) { |
| 1141 | TRACE_EVENT0("webrtc", "RtpDataChannel::SetRemoteContent_w"); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1142 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 1143 | RTC_LOG(LS_INFO) << "Setting remote data description"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1144 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1145 | RTC_DCHECK(content); |
| 1146 | if (!content) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1147 | SafeSetError("Can't find data content in remote description.", error_desc); |
| 1148 | return false; |
| 1149 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1150 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1151 | const DataContentDescription* data = content->as_data(); |
| 1152 | |
Zhi Huang | 801b868 | 2017-11-15 11:36:43 -0800 | [diff] [blame] | 1153 | // If the remote data doesn't have codecs, it must be empty, so ignore it. |
| 1154 | if (!data->has_codecs()) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1155 | return true; |
| 1156 | } |
| 1157 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1158 | if (!CheckDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1159 | return false; |
| 1160 | } |
| 1161 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1162 | RtpHeaderExtensions rtp_header_extensions = |
| 1163 | GetFilteredRtpHeaderExtensions(data->rtp_header_extensions()); |
| 1164 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1165 | RTC_LOG(LS_INFO) << "Setting remote data description"; |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1166 | DataSendParameters send_params = last_send_params_; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1167 | RtpSendParametersFromMediaDescription<DataCodec>(data, rtp_header_extensions, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1168 | &send_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1169 | if (!media_channel()->SetSendParameters(send_params)) { |
| 1170 | SafeSetError("Failed to set remote data description send parameters.", |
| 1171 | error_desc); |
| 1172 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1173 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1174 | last_send_params_ = send_params; |
| 1175 | |
| 1176 | // TODO(pthatcher): Move remote streams into DataRecvParameters, |
| 1177 | // and only give it to the media channel once we have a local |
| 1178 | // description too (without a local description, we won't be able to |
| 1179 | // recv them anyway). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1180 | if (!UpdateRemoteStreams_w(data->streams(), type, error_desc)) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1181 | SafeSetError("Failed to set remote data description streams.", error_desc); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1182 | return false; |
| 1183 | } |
| 1184 | |
| 1185 | set_remote_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 1186 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1187 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1190 | void RtpDataChannel::UpdateMediaSendRecvState_w() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1191 | // Render incoming data if we're the active call, and we have the local |
| 1192 | // content. We receive data on the default channel and multiplexed streams. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 1193 | bool recv = IsReadyToReceiveMedia_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1194 | if (!media_channel()->SetReceive(recv)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1195 | RTC_LOG(LS_ERROR) << "Failed to SetReceive on data channel"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | // Send outgoing data if we're the active call, we have the remote content, |
| 1199 | // and we have had some form of connectivity. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame] | 1200 | bool send = IsReadyToSendMedia_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1201 | if (!media_channel()->SetSend(send)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1202 | RTC_LOG(LS_ERROR) << "Failed to SetSend on data channel"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1205 | // Trigger SignalReadyToSendData asynchronously. |
| 1206 | OnDataChannelReadyToSend(send); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1207 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1208 | RTC_LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1211 | void RtpDataChannel::OnMessage(rtc::Message* pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1212 | switch (pmsg->message_id) { |
| 1213 | case MSG_READYTOSENDDATA: { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 1214 | DataChannelReadyToSendMessageData* data = |
| 1215 | static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 1216 | ready_to_send_data_ = data->data(); |
| 1217 | SignalReadyToSendData(ready_to_send_data_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1218 | delete data; |
| 1219 | break; |
| 1220 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1221 | case MSG_DATARECEIVED: { |
| 1222 | DataReceivedMessageData* data = |
| 1223 | static_cast<DataReceivedMessageData*>(pmsg->pdata); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1224 | SignalDataReceived(data->params, data->payload); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1225 | delete data; |
| 1226 | break; |
| 1227 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1228 | default: |
| 1229 | BaseChannel::OnMessage(pmsg); |
| 1230 | break; |
| 1231 | } |
| 1232 | } |
| 1233 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1234 | void RtpDataChannel::OnDataReceived(const ReceiveDataParams& params, |
| 1235 | const char* data, |
| 1236 | size_t len) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1237 | DataReceivedMessageData* msg = new DataReceivedMessageData(params, data, len); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1238 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1241 | void RtpDataChannel::OnDataChannelReadyToSend(bool writable) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 1242 | // This is usded for congestion control to indicate that the stream is ready |
| 1243 | // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates |
| 1244 | // that the transport channel is ready. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1245 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA, |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 1246 | new DataChannelReadyToSendMessageData(writable)); |
| 1247 | } |
| 1248 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1249 | } // namespace cricket |