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 | |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 11 | #include <utility> |
| 12 | |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 06:47:59 +0100 | [diff] [blame] | 13 | #include "webrtc/pc/channel.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | |
kjellander@webrtc.org | 7ffeab5 | 2016-02-26 22:46:09 +0100 | [diff] [blame] | 15 | #include "webrtc/audio_sink.h" |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 16 | #include "webrtc/base/bind.h" |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 17 | #include "webrtc/base/byteorder.h" |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 18 | #include "webrtc/base/checks.h" |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 19 | #include "webrtc/base/common.h" |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 20 | #include "webrtc/base/copyonwritebuffer.h" |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 21 | #include "webrtc/base/dscp.h" |
| 22 | #include "webrtc/base/logging.h" |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 23 | #include "webrtc/base/networkroute.h" |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 24 | #include "webrtc/base/trace_event.h" |
kjellander | f475277 | 2016-03-02 05:42:30 -0800 | [diff] [blame] | 25 | #include "webrtc/media/base/mediaconstants.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 26 | #include "webrtc/media/base/rtputils.h" |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 27 | #include "webrtc/p2p/base/transportchannel.h" |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 06:47:59 +0100 | [diff] [blame] | 28 | #include "webrtc/pc/channelmanager.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
| 30 | namespace cricket { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 31 | using rtc::Bind; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 32 | |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 33 | namespace { |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 34 | // See comment below for why we need to use a pointer to a unique_ptr. |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 35 | bool SetRawAudioSink_w(VoiceMediaChannel* channel, |
| 36 | uint32_t ssrc, |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 37 | std::unique_ptr<webrtc::AudioSinkInterface>* sink) { |
| 38 | channel->SetRawAudioSink(ssrc, std::move(*sink)); |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 39 | return true; |
| 40 | } |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 41 | |
| 42 | struct SendPacketMessageData : public rtc::MessageData { |
| 43 | rtc::CopyOnWriteBuffer packet; |
| 44 | rtc::PacketOptions options; |
| 45 | }; |
| 46 | |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 47 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 48 | // Returns the named header extension if found among all extensions, |
| 49 | // nullptr otherwise. |
| 50 | const webrtc::RtpExtension* FindHeaderExtension( |
| 51 | const std::vector<webrtc::RtpExtension>& extensions, |
| 52 | const std::string& uri) { |
| 53 | for (const auto& extension : extensions) { |
| 54 | if (extension.uri == uri) |
| 55 | return &extension; |
| 56 | } |
| 57 | return nullptr; |
| 58 | } |
| 59 | #endif |
| 60 | |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 61 | } // namespace |
| 62 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | enum { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 64 | MSG_EARLYMEDIATIMEOUT = 1, |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 65 | MSG_SEND_RTP_PACKET, |
| 66 | MSG_SEND_RTCP_PACKET, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 67 | MSG_CHANNEL_ERROR, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | MSG_READYTOSENDDATA, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | MSG_DATARECEIVED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 70 | MSG_FIRSTPACKETRECEIVED, |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 71 | MSG_STREAMCLOSEDREMOTELY, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // Value specified in RFC 5764. |
| 75 | static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp"; |
| 76 | |
| 77 | static const int kAgcMinus10db = -10; |
| 78 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 79 | static void SafeSetError(const std::string& message, std::string* error_desc) { |
| 80 | if (error_desc) { |
| 81 | *error_desc = message; |
| 82 | } |
| 83 | } |
| 84 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 85 | struct VoiceChannelErrorMessageData : public rtc::MessageData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 86 | VoiceChannelErrorMessageData(uint32_t in_ssrc, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | VoiceMediaChannel::Error in_error) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 88 | : ssrc(in_ssrc), error(in_error) {} |
| 89 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | VoiceMediaChannel::Error error; |
| 91 | }; |
| 92 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 93 | struct VideoChannelErrorMessageData : public rtc::MessageData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 94 | VideoChannelErrorMessageData(uint32_t in_ssrc, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | VideoMediaChannel::Error in_error) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 96 | : ssrc(in_ssrc), error(in_error) {} |
| 97 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | VideoMediaChannel::Error error; |
| 99 | }; |
| 100 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 101 | struct DataChannelErrorMessageData : public rtc::MessageData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 102 | DataChannelErrorMessageData(uint32_t in_ssrc, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | DataMediaChannel::Error in_error) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 104 | : ssrc(in_ssrc), error(in_error) {} |
| 105 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | DataMediaChannel::Error error; |
| 107 | }; |
| 108 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | static const char* PacketType(bool rtcp) { |
| 110 | return (!rtcp) ? "RTP" : "RTCP"; |
| 111 | } |
| 112 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 113 | static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | // Check the packet size. We could check the header too if needed. |
| 115 | return (packet && |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 116 | packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) && |
| 117 | packet->size() <= kMaxRtpPacketLen); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static bool IsReceiveContentDirection(MediaContentDirection direction) { |
| 121 | return direction == MD_SENDRECV || direction == MD_RECVONLY; |
| 122 | } |
| 123 | |
| 124 | static bool IsSendContentDirection(MediaContentDirection direction) { |
| 125 | return direction == MD_SENDRECV || direction == MD_SENDONLY; |
| 126 | } |
| 127 | |
| 128 | static const MediaContentDescription* GetContentDescription( |
| 129 | const ContentInfo* cinfo) { |
| 130 | if (cinfo == NULL) |
| 131 | return NULL; |
| 132 | return static_cast<const MediaContentDescription*>(cinfo->description); |
| 133 | } |
| 134 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 135 | template <class Codec> |
| 136 | void RtpParametersFromMediaDescription( |
| 137 | const MediaContentDescriptionImpl<Codec>* desc, |
| 138 | RtpParameters<Codec>* params) { |
| 139 | // TODO(pthatcher): Remove this once we're sure no one will give us |
| 140 | // a description without codecs (currently a CA_UPDATE with just |
| 141 | // streams can). |
| 142 | if (desc->has_codecs()) { |
| 143 | params->codecs = desc->codecs(); |
| 144 | } |
| 145 | // TODO(pthatcher): See if we really need |
| 146 | // rtp_header_extensions_set() and remove it if we don't. |
| 147 | if (desc->rtp_header_extensions_set()) { |
| 148 | params->extensions = desc->rtp_header_extensions(); |
| 149 | } |
deadbeef | 1387149 | 2015-12-09 12:37:51 -0800 | [diff] [blame] | 150 | params->rtcp.reduced_size = desc->rtcp_reduced_size(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 151 | } |
| 152 | |
nisse | 0510331 | 2016-03-16 02:22:50 -0700 | [diff] [blame] | 153 | template <class Codec> |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 154 | void RtpSendParametersFromMediaDescription( |
| 155 | const MediaContentDescriptionImpl<Codec>* desc, |
nisse | 0510331 | 2016-03-16 02:22:50 -0700 | [diff] [blame] | 156 | RtpSendParameters<Codec>* send_params) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 157 | RtpParametersFromMediaDescription(desc, send_params); |
| 158 | send_params->max_bandwidth_bps = desc->bandwidth(); |
| 159 | } |
| 160 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 161 | BaseChannel::BaseChannel(rtc::Thread* worker_thread, |
| 162 | rtc::Thread* network_thread, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 163 | MediaChannel* media_channel, |
| 164 | TransportController* transport_controller, |
| 165 | const std::string& content_name, |
| 166 | bool rtcp) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 167 | : worker_thread_(worker_thread), |
| 168 | network_thread_(network_thread), |
| 169 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | content_name_(content_name), |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 171 | |
| 172 | transport_controller_(transport_controller), |
deadbeef | 23d947d | 2016-08-22 16:00:30 -0700 | [diff] [blame] | 173 | rtcp_enabled_(rtcp), |
| 174 | media_channel_(media_channel) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 175 | RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 176 | if (transport_controller) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 177 | RTC_DCHECK_EQ(network_thread, transport_controller->network_thread()); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 178 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 179 | LOG(LS_INFO) << "Created channel for " << content_name; |
| 180 | } |
| 181 | |
| 182 | BaseChannel::~BaseChannel() { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 183 | TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel"); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 184 | RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 185 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 186 | StopConnectionMonitor(); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 187 | // Eats any outstanding messages or packets. |
| 188 | worker_thread_->Clear(&invoker_); |
| 189 | worker_thread_->Clear(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 190 | // We must destroy the media channel before the transport channel, otherwise |
| 191 | // the media channel may try to send on the dead transport channel. NULLing |
| 192 | // is not an effective strategy since the sends will come on another thread. |
| 193 | delete media_channel_; |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 194 | // Note that we don't just call SetTransportChannel_n(nullptr) because that |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 195 | // would call a pure virtual method which we can't do from a destructor. |
Danil Chapovalov | dae07ba | 2016-05-14 01:43:50 +0200 | [diff] [blame] | 196 | network_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 197 | RTC_FROM_HERE, Bind(&BaseChannel::DestroyTransportChannels_n, this)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 198 | LOG(LS_INFO) << "Destroyed channel"; |
| 199 | } |
| 200 | |
Danil Chapovalov | dae07ba | 2016-05-14 01:43:50 +0200 | [diff] [blame] | 201 | void BaseChannel::DisconnectTransportChannels_n() { |
| 202 | // Send any outstanding RTCP packets. |
| 203 | FlushRtcpMessages_n(); |
| 204 | |
| 205 | // Stop signals from transport channels, but keep them alive because |
| 206 | // media_channel may use them from a different thread. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 207 | if (transport_channel_) { |
| 208 | DisconnectFromTransportChannel(transport_channel_); |
Danil Chapovalov | dae07ba | 2016-05-14 01:43:50 +0200 | [diff] [blame] | 209 | } |
| 210 | if (rtcp_transport_channel_) { |
| 211 | DisconnectFromTransportChannel(rtcp_transport_channel_); |
| 212 | } |
| 213 | |
| 214 | // Clear pending read packets/messages. |
| 215 | network_thread_->Clear(&invoker_); |
| 216 | network_thread_->Clear(this); |
| 217 | } |
| 218 | |
| 219 | void BaseChannel::DestroyTransportChannels_n() { |
| 220 | if (transport_channel_) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 221 | transport_controller_->DestroyTransportChannel_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 222 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 223 | } |
| 224 | if (rtcp_transport_channel_) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 225 | transport_controller_->DestroyTransportChannel_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 226 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 227 | } |
Danil Chapovalov | dae07ba | 2016-05-14 01:43:50 +0200 | [diff] [blame] | 228 | // Clear pending send packets/messages. |
| 229 | network_thread_->Clear(&invoker_); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 230 | network_thread_->Clear(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 231 | } |
| 232 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 233 | bool BaseChannel::Init_w(const std::string* bundle_transport_name) { |
| 234 | if (!network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 235 | RTC_FROM_HERE, |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 236 | Bind(&BaseChannel::InitNetwork_n, this, bundle_transport_name))) { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 237 | return false; |
| 238 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 239 | |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 240 | // Both RTP and RTCP channels are set, we can call SetInterface on |
| 241 | // media channel and it can set network options. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 242 | RTC_DCHECK(worker_thread_->IsCurrent()); |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 243 | media_channel_->SetInterface(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 244 | return true; |
| 245 | } |
| 246 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 247 | bool BaseChannel::InitNetwork_n(const std::string* bundle_transport_name) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 248 | RTC_DCHECK(network_thread_->IsCurrent()); |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 249 | const std::string& transport_name = |
| 250 | (bundle_transport_name ? *bundle_transport_name : content_name()); |
| 251 | if (!SetTransport_n(transport_name)) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 252 | return false; |
| 253 | } |
| 254 | |
| 255 | if (!SetDtlsSrtpCryptoSuites_n(transport_channel_, false)) { |
| 256 | return false; |
| 257 | } |
deadbeef | 23d947d | 2016-08-22 16:00:30 -0700 | [diff] [blame] | 258 | if (rtcp_transport_channel_ && |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 259 | !SetDtlsSrtpCryptoSuites_n(rtcp_transport_channel_, true)) { |
| 260 | return false; |
| 261 | } |
| 262 | return true; |
| 263 | } |
| 264 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 265 | void BaseChannel::Deinit() { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 266 | RTC_DCHECK(worker_thread_->IsCurrent()); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 267 | media_channel_->SetInterface(NULL); |
Danil Chapovalov | dae07ba | 2016-05-14 01:43:50 +0200 | [diff] [blame] | 268 | // Packets arrive on the network thread, processing packets calls virtual |
| 269 | // functions, so need to stop this process in Deinit that is called in |
| 270 | // derived classes destructor. |
| 271 | network_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 272 | RTC_FROM_HERE, Bind(&BaseChannel::DisconnectTransportChannels_n, this)); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 273 | } |
| 274 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 275 | bool BaseChannel::SetTransport(const std::string& transport_name) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 276 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 277 | RTC_FROM_HERE, Bind(&BaseChannel::SetTransport_n, this, transport_name)); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 280 | bool BaseChannel::SetTransport_n(const std::string& transport_name) { |
| 281 | RTC_DCHECK(network_thread_->IsCurrent()); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 282 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 283 | if (transport_name == transport_name_) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 284 | // Nothing to do if transport name isn't changing. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 285 | return true; |
| 286 | } |
| 287 | |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 288 | // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport |
| 289 | // changes and wait until the DTLS handshake is complete to set the newly |
| 290 | // negotiated parameters. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 291 | if (ShouldSetupDtlsSrtp_n()) { |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 292 | // Set |writable_| to false such that UpdateWritableState_w can set up |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 293 | // DTLS-SRTP when |writable_| becomes true again. |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 294 | writable_ = false; |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 295 | srtp_filter_.ResetParams(); |
| 296 | } |
| 297 | |
deadbeef | 23d947d | 2016-08-22 16:00:30 -0700 | [diff] [blame] | 298 | // If this BaseChannel uses RTCP and we haven't fully negotiated RTCP mux, |
| 299 | // we need an RTCP channel. |
| 300 | if (rtcp_enabled_ && !rtcp_mux_filter_.IsFullyActive()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 301 | LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name() |
| 302 | << " on " << transport_name << " transport "; |
deadbeef | 23d947d | 2016-08-22 16:00:30 -0700 | [diff] [blame] | 303 | // TODO(deadbeef): Remove this grossness when we remove non-muxed RTCP. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 304 | SetRtcpTransportChannel_n( |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 305 | transport_controller_->CreateTransportChannel_n( |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 306 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP), |
| 307 | false /* update_writablity */); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 308 | if (!rtcp_transport_channel_) { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 309 | return false; |
| 310 | } |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 311 | } |
| 312 | |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 313 | // We're not updating the writablity during the transition state. |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 314 | SetTransportChannel_n(transport_controller_->CreateTransportChannel_n( |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 315 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 316 | if (!transport_channel_) { |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | |
deadbeef | 23d947d | 2016-08-22 16:00:30 -0700 | [diff] [blame] | 320 | // TODO(deadbeef): Remove this grossness when we remove non-muxed RTCP. |
| 321 | if (rtcp_transport_channel_) { |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 322 | // We can only update the RTCP ready to send after set_transport_channel has |
| 323 | // handled channel writability. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 324 | SetTransportChannelReadyToSend(true, rtcp_transport_channel_->writable()); |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 325 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 326 | transport_name_ = transport_name; |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 330 | void BaseChannel::SetTransportChannel_n(TransportChannel* new_tc) { |
| 331 | RTC_DCHECK(network_thread_->IsCurrent()); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 332 | |
| 333 | TransportChannel* old_tc = transport_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 334 | if (!old_tc && !new_tc) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 335 | // Nothing to do. |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 336 | return; |
| 337 | } |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 338 | RTC_DCHECK(old_tc != new_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 339 | |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 340 | if (old_tc) { |
| 341 | DisconnectFromTransportChannel(old_tc); |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 342 | transport_controller_->DestroyTransportChannel_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 343 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | transport_channel_ = new_tc; |
| 347 | |
| 348 | if (new_tc) { |
| 349 | ConnectToTransportChannel(new_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 350 | for (const auto& pair : socket_options_) { |
| 351 | new_tc->SetOption(pair.first, pair.second); |
| 352 | } |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 353 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 354 | |
| 355 | // Update aggregate writable/ready-to-send state between RTP and RTCP upon |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 356 | // setting new transport channels. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 357 | UpdateWritableState_n(); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 358 | // On setting a new channel, assume it's ready to send if it's writable, |
| 359 | // because we have no way of knowing otherwise (the channel doesn't give us |
| 360 | // "was last send successful?"). |
| 361 | // |
| 362 | // This won't always be accurate (the last SendPacket call from another |
| 363 | // BaseChannel could have resulted in an error), but even so, we'll just |
| 364 | // encounter the error again and update "ready to send" accordingly. |
| 365 | SetTransportChannelReadyToSend(false, new_tc && new_tc->writable()); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 368 | void BaseChannel::SetRtcpTransportChannel_n(TransportChannel* new_tc, |
| 369 | bool update_writablity) { |
| 370 | RTC_DCHECK(network_thread_->IsCurrent()); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 371 | |
| 372 | TransportChannel* old_tc = rtcp_transport_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 373 | if (!old_tc && !new_tc) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 374 | // Nothing to do. |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 375 | return; |
| 376 | } |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 377 | RTC_DCHECK(old_tc != new_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 378 | |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 379 | if (old_tc) { |
| 380 | DisconnectFromTransportChannel(old_tc); |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 381 | transport_controller_->DestroyTransportChannel_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 382 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | rtcp_transport_channel_ = new_tc; |
| 386 | |
| 387 | if (new_tc) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 388 | RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive())) |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 389 | << "Setting RTCP for DTLS/SRTP after SrtpFilter is active " |
| 390 | << "should never happen."; |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 391 | ConnectToTransportChannel(new_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 392 | for (const auto& pair : rtcp_socket_options_) { |
| 393 | new_tc->SetOption(pair.first, pair.second); |
| 394 | } |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 395 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 396 | |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 397 | if (update_writablity) { |
| 398 | // Update aggregate writable/ready-to-send state between RTP and RTCP upon |
| 399 | // setting new channel |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 400 | UpdateWritableState_n(); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 401 | // On setting a new channel, assume it's ready to send if it's writable, |
| 402 | // because we have no way of knowing otherwise (the channel doesn't give us |
| 403 | // "was last send successful?"). |
| 404 | // |
| 405 | // This won't always be accurate (the last SendPacket call from another |
| 406 | // BaseChannel could have resulted in an error), but even so, we'll just |
| 407 | // encounter the error again and update "ready to send" accordingly. |
| 408 | SetTransportChannelReadyToSend(true, new_tc && new_tc->writable()); |
guoweis | 4638331 | 2015-12-17 16:45:59 -0800 | [diff] [blame] | 409 | } |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 413 | RTC_DCHECK(network_thread_->IsCurrent()); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 414 | |
| 415 | tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState); |
| 416 | tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead); |
| 417 | tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 418 | tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState); |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 419 | tc->SignalSelectedCandidatePairChanged.connect( |
| 420 | this, &BaseChannel::OnSelectedCandidatePairChanged); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 421 | tc->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 425 | RTC_DCHECK(network_thread_->IsCurrent()); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 426 | |
| 427 | tc->SignalWritableState.disconnect(this); |
| 428 | tc->SignalReadPacket.disconnect(this); |
| 429 | tc->SignalReadyToSend.disconnect(this); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 430 | tc->SignalDtlsState.disconnect(this); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 431 | tc->SignalSelectedCandidatePairChanged.disconnect(this); |
| 432 | tc->SignalSentPacket.disconnect(this); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 433 | } |
| 434 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 435 | bool BaseChannel::Enable(bool enable) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 436 | worker_thread_->Invoke<void>( |
| 437 | RTC_FROM_HERE, |
| 438 | Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w, |
| 439 | this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 440 | return true; |
| 441 | } |
| 442 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 443 | bool BaseChannel::AddRecvStream(const StreamParams& sp) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 444 | return InvokeOnWorker(RTC_FROM_HERE, |
| 445 | Bind(&BaseChannel::AddRecvStream_w, this, sp)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 448 | bool BaseChannel::RemoveRecvStream(uint32_t ssrc) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 449 | return InvokeOnWorker(RTC_FROM_HERE, |
| 450 | Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 451 | } |
| 452 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 453 | bool BaseChannel::AddSendStream(const StreamParams& sp) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 454 | return InvokeOnWorker( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 455 | RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 458 | bool BaseChannel::RemoveSendStream(uint32_t ssrc) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 459 | return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream, |
| 460 | media_channel(), ssrc)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 461 | } |
| 462 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 463 | bool BaseChannel::SetLocalContent(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 464 | ContentAction action, |
| 465 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 466 | TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent"); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 467 | return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w, |
| 468 | this, content, action, error_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | bool BaseChannel::SetRemoteContent(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 472 | ContentAction action, |
| 473 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 474 | TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent"); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 475 | return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w, |
| 476 | this, content, action, error_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 477 | } |
| 478 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 479 | void BaseChannel::StartConnectionMonitor(int cms) { |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 480 | // We pass in the BaseChannel instead of the transport_channel_ |
| 481 | // because if the transport_channel_ changes, the ConnectionMonitor |
| 482 | // would be pointing to the wrong TransportChannel. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 483 | // We pass in the network thread because on that thread connection monitor |
| 484 | // will call BaseChannel::GetConnectionStats which must be called on the |
| 485 | // network thread. |
| 486 | connection_monitor_.reset( |
| 487 | new ConnectionMonitor(this, network_thread(), rtc::Thread::Current())); |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 488 | connection_monitor_->SignalUpdate.connect( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 489 | this, &BaseChannel::OnConnectionMonitorUpdate); |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 490 | connection_monitor_->Start(cms); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | void BaseChannel::StopConnectionMonitor() { |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 494 | if (connection_monitor_) { |
| 495 | connection_monitor_->Stop(); |
| 496 | connection_monitor_.reset(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 500 | bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 501 | RTC_DCHECK(network_thread_->IsCurrent()); |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 502 | return transport_channel_->GetStats(infos); |
| 503 | } |
| 504 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 505 | bool BaseChannel::IsReadyToReceiveMedia_w() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 506 | // Receive data if we are enabled and have local content, |
| 507 | return enabled() && IsReceiveContentDirection(local_content_direction_); |
| 508 | } |
| 509 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 510 | bool BaseChannel::IsReadyToSendMedia_w() const { |
| 511 | // Need to access some state updated on the network thread. |
| 512 | return network_thread_->Invoke<bool>( |
| 513 | RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this)); |
| 514 | } |
| 515 | |
| 516 | bool BaseChannel::IsReadyToSendMedia_n() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 517 | // Send outgoing data if we are enabled, have local and remote content, |
| 518 | // and we have had some form of connectivity. |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 519 | return enabled() && IsReceiveContentDirection(remote_content_direction_) && |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 520 | IsSendContentDirection(local_content_direction_) && |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 521 | was_ever_writable() && |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 522 | (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 523 | } |
| 524 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 525 | bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 526 | const rtc::PacketOptions& options) { |
| 527 | return SendPacket(false, packet, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 528 | } |
| 529 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 530 | bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 531 | const rtc::PacketOptions& options) { |
| 532 | return SendPacket(true, packet, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 533 | } |
| 534 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 535 | int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 536 | int value) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 537 | return network_thread_->Invoke<int>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 538 | RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | int BaseChannel::SetOption_n(SocketType type, |
| 542 | rtc::Socket::Option opt, |
| 543 | int value) { |
| 544 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 545 | TransportChannel* channel = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 546 | switch (type) { |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 547 | case ST_RTP: |
| 548 | channel = transport_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 549 | socket_options_.push_back( |
| 550 | std::pair<rtc::Socket::Option, int>(opt, value)); |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 551 | break; |
| 552 | case ST_RTCP: |
| 553 | channel = rtcp_transport_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 554 | rtcp_socket_options_.push_back( |
| 555 | std::pair<rtc::Socket::Option, int>(opt, value)); |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 556 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 557 | } |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 558 | return channel ? channel->SetOption(opt, value) : -1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 559 | } |
| 560 | |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 561 | bool BaseChannel::SetCryptoOptions(const rtc::CryptoOptions& crypto_options) { |
| 562 | crypto_options_ = crypto_options; |
| 563 | return true; |
| 564 | } |
| 565 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 566 | void BaseChannel::OnWritableState(TransportChannel* channel) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 567 | RTC_DCHECK(channel == transport_channel_ || |
| 568 | channel == rtcp_transport_channel_); |
| 569 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 570 | UpdateWritableState_n(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | void BaseChannel::OnChannelRead(TransportChannel* channel, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 574 | const char* data, size_t len, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 575 | const rtc::PacketTime& packet_time, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 576 | int flags) { |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 577 | TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 578 | // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 579 | RTC_DCHECK(network_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 580 | |
| 581 | // When using RTCP multiplexing we might get RTCP packets on the RTP |
| 582 | // transport. We feed RTP traffic into the demuxer to determine if it is RTCP. |
| 583 | bool rtcp = PacketIsRtcp(channel, data, len); |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 584 | rtc::CopyOnWriteBuffer packet(data, len); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 585 | HandlePacket(rtcp, &packet, packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | void BaseChannel::OnReadyToSend(TransportChannel* channel) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 589 | RTC_DCHECK(channel == transport_channel_ || |
| 590 | channel == rtcp_transport_channel_); |
| 591 | SetTransportChannelReadyToSend(channel == rtcp_transport_channel_, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 594 | void BaseChannel::OnDtlsState(TransportChannel* channel, |
| 595 | DtlsTransportState state) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 596 | if (!ShouldSetupDtlsSrtp_n()) { |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 597 | return; |
| 598 | } |
| 599 | |
| 600 | // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED |
| 601 | // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to |
| 602 | // cover other scenarios like the whole channel is writable (not just this |
| 603 | // TransportChannel) or when TransportChannel is attached after DTLS is |
| 604 | // negotiated. |
| 605 | if (state != DTLS_TRANSPORT_CONNECTED) { |
| 606 | srtp_filter_.ResetParams(); |
| 607 | } |
| 608 | } |
| 609 | |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 610 | void BaseChannel::OnSelectedCandidatePairChanged( |
| 611 | TransportChannel* channel, |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 612 | CandidatePairInterface* selected_candidate_pair, |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 613 | int last_sent_packet_id, |
| 614 | bool ready_to_send) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 615 | RTC_DCHECK(channel == transport_channel_ || |
| 616 | channel == rtcp_transport_channel_); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 617 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 618 | std::string transport_name = channel->transport_name(); |
Honghai Zhang | 0e533ef | 2016-04-19 15:41:36 -0700 | [diff] [blame] | 619 | rtc::NetworkRoute network_route; |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 620 | if (selected_candidate_pair) { |
Honghai Zhang | 0e533ef | 2016-04-19 15:41:36 -0700 | [diff] [blame] | 621 | network_route = rtc::NetworkRoute( |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 622 | ready_to_send, selected_candidate_pair->local_candidate().network_id(), |
Honghai Zhang | 0e533ef | 2016-04-19 15:41:36 -0700 | [diff] [blame] | 623 | selected_candidate_pair->remote_candidate().network_id(), |
| 624 | last_sent_packet_id); |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 625 | } |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 626 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 627 | RTC_FROM_HERE, worker_thread_, |
| 628 | Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name, |
| 629 | network_route)); |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 632 | void BaseChannel::SetTransportChannelReadyToSend(bool rtcp, bool ready) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 633 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 634 | if (rtcp) { |
| 635 | rtcp_ready_to_send_ = ready; |
| 636 | } else { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 637 | rtp_ready_to_send_ = ready; |
| 638 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 639 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 640 | bool ready_to_send = |
| 641 | (rtp_ready_to_send_ && |
| 642 | // In the case of rtcp mux |rtcp_transport_channel_| will be null. |
| 643 | (rtcp_ready_to_send_ || !rtcp_transport_channel_)); |
| 644 | |
| 645 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 646 | RTC_FROM_HERE, worker_thread_, |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 647 | Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | bool BaseChannel::PacketIsRtcp(const TransportChannel* channel, |
| 651 | const char* data, size_t len) { |
| 652 | return (channel == rtcp_transport_channel_ || |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 653 | rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 654 | } |
| 655 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 656 | bool BaseChannel::SendPacket(bool rtcp, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 657 | rtc::CopyOnWriteBuffer* packet, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 658 | const rtc::PacketOptions& options) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 659 | // SendPacket gets called from MediaEngine, on a pacer or an encoder thread. |
| 660 | // If the thread is not our network thread, we will post to our network |
| 661 | // 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] | 662 | // synchronize access to all the pieces of the send path, including |
| 663 | // SRTP and the inner workings of the transport channels. |
| 664 | // The only downside is that we can't return a proper failure code if |
| 665 | // needed. Since UDP is unreliable anyway, this should be a non-issue. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 666 | if (!network_thread_->IsCurrent()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 667 | // Avoid a copy by transferring the ownership of the packet data. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 668 | int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET; |
| 669 | SendPacketMessageData* data = new SendPacketMessageData; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 670 | data->packet = std::move(*packet); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 671 | data->options = options; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 672 | network_thread_->Post(RTC_FROM_HERE, this, message_id, data); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 673 | return true; |
| 674 | } |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 675 | TRACE_EVENT0("webrtc", "BaseChannel::SendPacket"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 676 | |
| 677 | // Now that we are on the correct thread, ensure we have a place to send this |
| 678 | // packet before doing anything. (We might get RTCP packets that we don't |
| 679 | // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP |
| 680 | // transport. |
| 681 | TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ? |
| 682 | transport_channel_ : rtcp_transport_channel_; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 683 | if (!channel || !channel->writable()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 684 | return false; |
| 685 | } |
| 686 | |
| 687 | // Protect ourselves against crazy data. |
| 688 | if (!ValidPacket(rtcp, packet)) { |
| 689 | LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " " |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 690 | << PacketType(rtcp) |
| 691 | << " packet: wrong size=" << packet->size(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 692 | return false; |
| 693 | } |
| 694 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 695 | rtc::PacketOptions updated_options; |
| 696 | updated_options = options; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 697 | // Protect if needed. |
| 698 | if (srtp_filter_.IsActive()) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 699 | TRACE_EVENT0("webrtc", "SRTP Encode"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 700 | bool res; |
Karl Wiberg | c56ac1e | 2015-05-04 14:54:55 +0200 | [diff] [blame] | 701 | uint8_t* data = packet->data(); |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 702 | int len = static_cast<int>(packet->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 703 | if (!rtcp) { |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 704 | // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done |
| 705 | // inside libsrtp for a RTP packet. A external HMAC module will be writing |
| 706 | // a fake HMAC value. This is ONLY done for a RTP packet. |
| 707 | // Socket layer will update rtp sendtime extension header if present in |
| 708 | // packet with current time before updating the HMAC. |
| 709 | #if !defined(ENABLE_EXTERNAL_AUTH) |
| 710 | res = srtp_filter_.ProtectRtp( |
| 711 | data, len, static_cast<int>(packet->capacity()), &len); |
| 712 | #else |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 713 | updated_options.packet_time_params.rtp_sendtime_extension_id = |
henrike@webrtc.org | 0537634 | 2014-03-10 15:53:12 +0000 | [diff] [blame] | 714 | rtp_abs_sendtime_extn_id_; |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 715 | res = srtp_filter_.ProtectRtp( |
| 716 | data, len, static_cast<int>(packet->capacity()), &len, |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 717 | &updated_options.packet_time_params.srtp_packet_index); |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 718 | // If protection succeeds, let's get auth params from srtp. |
| 719 | if (res) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 720 | uint8_t* auth_key = NULL; |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 721 | int key_len; |
| 722 | res = srtp_filter_.GetRtpAuthParams( |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 723 | &auth_key, &key_len, |
| 724 | &updated_options.packet_time_params.srtp_auth_tag_len); |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 725 | if (res) { |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 726 | updated_options.packet_time_params.srtp_auth_key.resize(key_len); |
| 727 | updated_options.packet_time_params.srtp_auth_key.assign( |
| 728 | auth_key, auth_key + key_len); |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | #endif |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 732 | if (!res) { |
| 733 | int seq_num = -1; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 734 | uint32_t ssrc = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 735 | GetRtpSeqNum(data, len, &seq_num); |
| 736 | GetRtpSsrc(data, len, &ssrc); |
| 737 | LOG(LS_ERROR) << "Failed to protect " << content_name_ |
| 738 | << " RTP packet: size=" << len |
| 739 | << ", seqnum=" << seq_num << ", SSRC=" << ssrc; |
| 740 | return false; |
| 741 | } |
| 742 | } else { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 743 | res = srtp_filter_.ProtectRtcp(data, len, |
| 744 | static_cast<int>(packet->capacity()), |
| 745 | &len); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 746 | if (!res) { |
| 747 | int type = -1; |
| 748 | GetRtcpType(data, len, &type); |
| 749 | LOG(LS_ERROR) << "Failed to protect " << content_name_ |
| 750 | << " RTCP packet: size=" << len << ", type=" << type; |
| 751 | return false; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | // Update the length of the packet now that we've added the auth tag. |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 756 | packet->SetSize(len); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 757 | } else if (secure_required_) { |
| 758 | // This is a double check for something that supposedly can't happen. |
| 759 | LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp) |
| 760 | << " packet when SRTP is inactive and crypto is required"; |
| 761 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 762 | RTC_DCHECK(false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 763 | return false; |
| 764 | } |
| 765 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 766 | // Bon voyage. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 767 | int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL; |
| 768 | int ret = channel->SendPacket(packet->data<char>(), packet->size(), |
| 769 | updated_options, flags); |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 770 | if (ret != static_cast<int>(packet->size())) { |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame] | 771 | if (channel->GetError() == ENOTCONN) { |
| 772 | LOG(LS_WARNING) << "Got ENOTCONN from transport."; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 773 | SetTransportChannelReadyToSend(rtcp, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 774 | } |
| 775 | return false; |
| 776 | } |
| 777 | return true; |
| 778 | } |
| 779 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 780 | bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 781 | // Protect ourselves against crazy data. |
| 782 | if (!ValidPacket(rtcp, packet)) { |
| 783 | LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " " |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 784 | << PacketType(rtcp) |
| 785 | << " packet: wrong size=" << packet->size(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 786 | return false; |
| 787 | } |
pbos | 482b12e | 2015-11-16 10:19:58 -0800 | [diff] [blame] | 788 | if (rtcp) { |
| 789 | // Permit all (seemingly valid) RTCP packets. |
| 790 | return true; |
| 791 | } |
| 792 | // Check whether we handle this payload. |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 793 | return bundle_filter_.DemuxPacket(packet->data(), packet->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 794 | } |
| 795 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 796 | void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 797 | const rtc::PacketTime& packet_time) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 798 | RTC_DCHECK(network_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 799 | if (!WantsPacket(rtcp, packet)) { |
| 800 | return; |
| 801 | } |
| 802 | |
honghaiz@google.com | a67ca1a | 2015-01-28 19:48:33 +0000 | [diff] [blame] | 803 | // We are only interested in the first rtp packet because that |
| 804 | // indicates the media has started flowing. |
| 805 | if (!has_received_packet_ && !rtcp) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 806 | has_received_packet_ = true; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 807 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 808 | } |
| 809 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 810 | // Unprotect the packet, if needed. |
| 811 | if (srtp_filter_.IsActive()) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 812 | TRACE_EVENT0("webrtc", "SRTP Decode"); |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 813 | char* data = packet->data<char>(); |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 814 | int len = static_cast<int>(packet->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 815 | bool res; |
| 816 | if (!rtcp) { |
| 817 | res = srtp_filter_.UnprotectRtp(data, len, &len); |
| 818 | if (!res) { |
| 819 | int seq_num = -1; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 820 | uint32_t ssrc = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 821 | GetRtpSeqNum(data, len, &seq_num); |
| 822 | GetRtpSsrc(data, len, &ssrc); |
| 823 | LOG(LS_ERROR) << "Failed to unprotect " << content_name_ |
| 824 | << " RTP packet: size=" << len |
| 825 | << ", seqnum=" << seq_num << ", SSRC=" << ssrc; |
| 826 | return; |
| 827 | } |
| 828 | } else { |
| 829 | res = srtp_filter_.UnprotectRtcp(data, len, &len); |
| 830 | if (!res) { |
| 831 | int type = -1; |
| 832 | GetRtcpType(data, len, &type); |
| 833 | LOG(LS_ERROR) << "Failed to unprotect " << content_name_ |
| 834 | << " RTCP packet: size=" << len << ", type=" << type; |
| 835 | return; |
| 836 | } |
| 837 | } |
| 838 | |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 839 | packet->SetSize(len); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 840 | } else if (secure_required_) { |
| 841 | // Our session description indicates that SRTP is required, but we got a |
| 842 | // packet before our SRTP filter is active. This means either that |
| 843 | // a) we got SRTP packets before we received the SDES keys, in which case |
| 844 | // we can't decrypt it anyway, or |
| 845 | // b) we got SRTP packets before DTLS completed on both the RTP and RTCP |
| 846 | // channels, so we haven't yet extracted keys, even if DTLS did complete |
| 847 | // on the channel that the packets are being sent on. It's really good |
| 848 | // practice to wait for both RTP and RTCP to be good to go before sending |
| 849 | // media, to prevent weird failure modes, so it's fine for us to just eat |
| 850 | // packets here. This is all sidestepped if RTCP mux is used anyway. |
| 851 | LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp) |
| 852 | << " packet when SRTP is inactive and crypto is required"; |
| 853 | return; |
| 854 | } |
| 855 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 856 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 857 | RTC_FROM_HERE, worker_thread_, |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 858 | Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time)); |
| 859 | } |
| 860 | |
| 861 | void BaseChannel::OnPacketReceived(bool rtcp, |
| 862 | const rtc::CopyOnWriteBuffer& packet, |
| 863 | const rtc::PacketTime& packet_time) { |
| 864 | RTC_DCHECK(worker_thread_->IsCurrent()); |
| 865 | // Need to copy variable because OnRtcpReceived/OnPacketReceived |
| 866 | // requires non-const pointer to buffer. This doesn't memcpy the actual data. |
| 867 | rtc::CopyOnWriteBuffer data(packet); |
| 868 | if (rtcp) { |
| 869 | media_channel_->OnRtcpReceived(&data, packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 870 | } else { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 871 | media_channel_->OnPacketReceived(&data, packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 875 | bool BaseChannel::PushdownLocalDescription( |
| 876 | const SessionDescription* local_desc, ContentAction action, |
| 877 | std::string* error_desc) { |
| 878 | const ContentInfo* content_info = GetFirstContent(local_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 879 | const MediaContentDescription* content_desc = |
| 880 | GetContentDescription(content_info); |
| 881 | if (content_desc && content_info && !content_info->rejected && |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 882 | !SetLocalContent(content_desc, action, error_desc)) { |
| 883 | LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action; |
| 884 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 885 | } |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 886 | return true; |
| 887 | } |
| 888 | |
| 889 | bool BaseChannel::PushdownRemoteDescription( |
| 890 | const SessionDescription* remote_desc, ContentAction action, |
| 891 | std::string* error_desc) { |
| 892 | const ContentInfo* content_info = GetFirstContent(remote_desc); |
| 893 | const MediaContentDescription* content_desc = |
| 894 | GetContentDescription(content_info); |
| 895 | if (content_desc && content_info && !content_info->rejected && |
| 896 | !SetRemoteContent(content_desc, action, error_desc)) { |
| 897 | LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action; |
| 898 | return false; |
| 899 | } |
| 900 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | void BaseChannel::EnableMedia_w() { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 904 | RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 905 | if (enabled_) |
| 906 | return; |
| 907 | |
| 908 | LOG(LS_INFO) << "Channel enabled"; |
| 909 | enabled_ = true; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 910 | UpdateMediaSendRecvState_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | void BaseChannel::DisableMedia_w() { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 914 | RTC_DCHECK(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 915 | if (!enabled_) |
| 916 | return; |
| 917 | |
| 918 | LOG(LS_INFO) << "Channel disabled"; |
| 919 | enabled_ = false; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 920 | UpdateMediaSendRecvState_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 923 | void BaseChannel::UpdateWritableState_n() { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 924 | if (transport_channel_ && transport_channel_->writable() && |
| 925 | (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 926 | ChannelWritable_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 927 | } else { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 928 | ChannelNotWritable_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 932 | void BaseChannel::ChannelWritable_n() { |
| 933 | RTC_DCHECK(network_thread_->IsCurrent()); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 934 | if (writable_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 935 | return; |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 936 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 937 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 938 | LOG(LS_INFO) << "Channel writable (" << content_name_ << ")" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 939 | << (was_ever_writable_ ? "" : " for the first time"); |
| 940 | |
| 941 | std::vector<ConnectionInfo> infos; |
| 942 | transport_channel_->GetStats(&infos); |
| 943 | for (std::vector<ConnectionInfo>::const_iterator it = infos.begin(); |
| 944 | it != infos.end(); ++it) { |
| 945 | if (it->best_connection) { |
| 946 | LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString() |
| 947 | << "->" << it->remote_candidate.ToSensitiveString(); |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 952 | was_ever_writable_ = true; |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 953 | MaybeSetupDtlsSrtp_n(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 954 | writable_ = true; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 955 | UpdateMediaSendRecvState(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 958 | void BaseChannel::SignalDtlsSetupFailure_n(bool rtcp) { |
| 959 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 960 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 961 | RTC_FROM_HERE, signaling_thread(), |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 962 | Bind(&BaseChannel::SignalDtlsSetupFailure_s, this, rtcp)); |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 966 | RTC_DCHECK(signaling_thread() == rtc::Thread::Current()); |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 967 | SignalDtlsSetupFailure(this, rtcp); |
| 968 | } |
| 969 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 970 | bool BaseChannel::SetDtlsSrtpCryptoSuites_n(TransportChannel* tc, bool rtcp) { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 971 | std::vector<int> crypto_suites; |
| 972 | // We always use the default SRTP crypto suites for RTCP, but we may use |
| 973 | // different crypto suites for RTP depending on the media type. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 974 | if (!rtcp) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 975 | GetSrtpCryptoSuites_n(&crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 976 | } else { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 977 | GetDefaultSrtpCryptoSuites(crypto_options(), &crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 978 | } |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 979 | return tc->SetSrtpCryptoSuites(crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 982 | bool BaseChannel::ShouldSetupDtlsSrtp_n() const { |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 983 | // Since DTLS is applied to all channels, checking RTP should be enough. |
| 984 | return transport_channel_ && transport_channel_->IsDtlsActive(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | // This function returns true if either DTLS-SRTP is not in use |
| 988 | // *or* DTLS-SRTP is successfully set up. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 989 | bool BaseChannel::SetupDtlsSrtp_n(bool rtcp_channel) { |
| 990 | RTC_DCHECK(network_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 991 | bool ret = false; |
| 992 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 993 | TransportChannel* channel = |
| 994 | rtcp_channel ? rtcp_transport_channel_ : transport_channel_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 995 | |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 996 | RTC_DCHECK(channel->IsDtlsActive()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 997 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 998 | int selected_crypto_suite; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 999 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1000 | if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) { |
| 1001 | LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1002 | return false; |
| 1003 | } |
| 1004 | |
| 1005 | LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " |
| 1006 | << content_name() << " " |
| 1007 | << PacketType(rtcp_channel); |
| 1008 | |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1009 | int key_len; |
| 1010 | int salt_len; |
| 1011 | if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len, |
| 1012 | &salt_len)) { |
| 1013 | LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite; |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1017 | // OK, we're now doing DTLS (RFC 5764) |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1018 | std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1019 | |
| 1020 | // RFC 5705 exporter using the RFC 5764 parameters |
| 1021 | if (!channel->ExportKeyingMaterial( |
| 1022 | kDtlsSrtpExporterLabel, |
| 1023 | NULL, 0, false, |
| 1024 | &dtls_buffer[0], dtls_buffer.size())) { |
| 1025 | LOG(LS_WARNING) << "DTLS-SRTP key export failed"; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1026 | RTC_DCHECK(false); // This should never happen |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1027 | return false; |
| 1028 | } |
| 1029 | |
| 1030 | // Sync up the keys with the DTLS-SRTP interface |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1031 | std::vector<unsigned char> client_write_key(key_len + salt_len); |
| 1032 | std::vector<unsigned char> server_write_key(key_len + salt_len); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1033 | size_t offset = 0; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1034 | memcpy(&client_write_key[0], &dtls_buffer[offset], key_len); |
| 1035 | offset += key_len; |
| 1036 | memcpy(&server_write_key[0], &dtls_buffer[offset], key_len); |
| 1037 | offset += key_len; |
| 1038 | memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len); |
| 1039 | offset += salt_len; |
| 1040 | memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1041 | |
| 1042 | std::vector<unsigned char> *send_key, *recv_key; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1043 | rtc::SSLRole role; |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1044 | if (!channel->GetSslRole(&role)) { |
| 1045 | LOG(LS_WARNING) << "GetSslRole failed"; |
| 1046 | return false; |
| 1047 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1048 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1049 | if (role == rtc::SSL_SERVER) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1050 | send_key = &server_write_key; |
| 1051 | recv_key = &client_write_key; |
| 1052 | } else { |
| 1053 | send_key = &client_write_key; |
| 1054 | recv_key = &server_write_key; |
| 1055 | } |
| 1056 | |
| 1057 | if (rtcp_channel) { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1058 | ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0], |
| 1059 | static_cast<int>(send_key->size()), |
| 1060 | selected_crypto_suite, &(*recv_key)[0], |
| 1061 | static_cast<int>(recv_key->size())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1062 | } else { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1063 | ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0], |
| 1064 | static_cast<int>(send_key->size()), |
| 1065 | selected_crypto_suite, &(*recv_key)[0], |
| 1066 | static_cast<int>(recv_key->size())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | if (!ret) |
| 1070 | LOG(LS_WARNING) << "DTLS-SRTP key installation failed"; |
| 1071 | else |
| 1072 | dtls_keyed_ = true; |
| 1073 | |
| 1074 | return ret; |
| 1075 | } |
| 1076 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1077 | void BaseChannel::MaybeSetupDtlsSrtp_n() { |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 1078 | if (srtp_filter_.IsActive()) { |
| 1079 | return; |
| 1080 | } |
| 1081 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1082 | if (!ShouldSetupDtlsSrtp_n()) { |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 1083 | return; |
| 1084 | } |
| 1085 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1086 | if (!SetupDtlsSrtp_n(false)) { |
| 1087 | SignalDtlsSetupFailure_n(false); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | if (rtcp_transport_channel_) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1092 | if (!SetupDtlsSrtp_n(true)) { |
| 1093 | SignalDtlsSetupFailure_n(true); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 1094 | return; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1099 | void BaseChannel::ChannelNotWritable_n() { |
| 1100 | RTC_DCHECK(network_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1101 | if (!writable_) |
| 1102 | return; |
| 1103 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1104 | LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1105 | writable_ = false; |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1106 | UpdateMediaSendRecvState(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1109 | bool BaseChannel::SetRtpTransportParameters( |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1110 | const MediaContentDescription* content, |
| 1111 | ContentAction action, |
| 1112 | ContentSource src, |
| 1113 | std::string* error_desc) { |
| 1114 | if (action == CA_UPDATE) { |
| 1115 | // These parameters never get changed by a CA_UDPATE. |
| 1116 | return true; |
| 1117 | } |
| 1118 | |
| 1119 | // Cache secure_required_ for belt and suspenders check on SendPacket |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1120 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1121 | RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this, |
| 1122 | content, action, src, error_desc)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | bool BaseChannel::SetRtpTransportParameters_n( |
| 1126 | const MediaContentDescription* content, |
| 1127 | ContentAction action, |
| 1128 | ContentSource src, |
| 1129 | std::string* error_desc) { |
| 1130 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1131 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1132 | if (src == CS_LOCAL) { |
| 1133 | set_secure_required(content->crypto_required() != CT_NONE); |
| 1134 | } |
| 1135 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1136 | if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1137 | return false; |
| 1138 | } |
| 1139 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1140 | if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1141 | return false; |
| 1142 | } |
| 1143 | |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1147 | // |dtls| will be set to true if DTLS is active for transport channel and |
| 1148 | // crypto is empty. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1149 | bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos, |
| 1150 | bool* dtls, |
| 1151 | std::string* error_desc) { |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1152 | *dtls = transport_channel_->IsDtlsActive(); |
| 1153 | if (*dtls && !cryptos.empty()) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1154 | SafeSetError("Cryptos must be empty when DTLS is active.", error_desc); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1155 | return false; |
| 1156 | } |
| 1157 | return true; |
| 1158 | } |
| 1159 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1160 | bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1161 | ContentAction action, |
| 1162 | ContentSource src, |
| 1163 | std::string* error_desc) { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 1164 | TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w"); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1165 | if (action == CA_UPDATE) { |
| 1166 | // no crypto params. |
| 1167 | return true; |
| 1168 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1169 | bool ret = false; |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1170 | bool dtls = false; |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1171 | ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1172 | if (!ret) { |
| 1173 | return false; |
| 1174 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1175 | switch (action) { |
| 1176 | case CA_OFFER: |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1177 | // If DTLS is already active on the channel, we could be renegotiating |
| 1178 | // here. We don't update the srtp filter. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1179 | if (!dtls) { |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1180 | ret = srtp_filter_.SetOffer(cryptos, src); |
| 1181 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1182 | break; |
| 1183 | case CA_PRANSWER: |
| 1184 | // If we're doing DTLS-SRTP, we don't want to update the filter |
| 1185 | // with an answer, because we already have SRTP parameters. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1186 | if (!dtls) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1187 | ret = srtp_filter_.SetProvisionalAnswer(cryptos, src); |
| 1188 | } |
| 1189 | break; |
| 1190 | case CA_ANSWER: |
| 1191 | // If we're doing DTLS-SRTP, we don't want to update the filter |
| 1192 | // with an answer, because we already have SRTP parameters. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1193 | if (!dtls) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1194 | ret = srtp_filter_.SetAnswer(cryptos, src); |
| 1195 | } |
| 1196 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1197 | default: |
| 1198 | break; |
| 1199 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1200 | if (!ret) { |
| 1201 | SafeSetError("Failed to setup SRTP filter.", error_desc); |
| 1202 | return false; |
| 1203 | } |
| 1204 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 1207 | void BaseChannel::ActivateRtcpMux() { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1208 | network_thread_->Invoke<void>(RTC_FROM_HERE, |
| 1209 | Bind(&BaseChannel::ActivateRtcpMux_n, this)); |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1212 | void BaseChannel::ActivateRtcpMux_n() { |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 1213 | if (!rtcp_mux_filter_.IsActive()) { |
| 1214 | rtcp_mux_filter_.SetActive(); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1215 | SetRtcpTransportChannel_n(nullptr, true); |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1219 | bool BaseChannel::SetRtcpMux_n(bool enable, |
| 1220 | ContentAction action, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1221 | ContentSource src, |
| 1222 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1223 | bool ret = false; |
| 1224 | switch (action) { |
| 1225 | case CA_OFFER: |
| 1226 | ret = rtcp_mux_filter_.SetOffer(enable, src); |
| 1227 | break; |
| 1228 | case CA_PRANSWER: |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1229 | // This may activate RTCP muxing, but we don't yet destroy the channel |
| 1230 | // because the final answer may deactivate it. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1231 | ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src); |
| 1232 | break; |
| 1233 | case CA_ANSWER: |
| 1234 | ret = rtcp_mux_filter_.SetAnswer(enable, src); |
| 1235 | if (ret && rtcp_mux_filter_.IsActive()) { |
| 1236 | // We activated RTCP mux, close down the RTCP transport. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1237 | LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name() |
| 1238 | << " by destroying RTCP transport channel for " |
| 1239 | << transport_name(); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1240 | SetRtcpTransportChannel_n(nullptr, true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1241 | } |
| 1242 | break; |
| 1243 | case CA_UPDATE: |
| 1244 | // No RTCP mux info. |
| 1245 | ret = true; |
Henrik Kjellander | 7c027b6 | 2015-04-22 13:21:30 +0200 | [diff] [blame] | 1246 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1247 | default: |
| 1248 | break; |
| 1249 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1250 | if (!ret) { |
| 1251 | SafeSetError("Failed to setup RTCP mux filter.", error_desc); |
| 1252 | return false; |
| 1253 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1254 | // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or |
| 1255 | // CA_ANSWER, but we only want to tear down the RTCP transport channel if we |
| 1256 | // received a final answer. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1257 | if (rtcp_mux_filter_.IsActive()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1258 | // If the RTP transport is already writable, then so are we. |
| 1259 | if (transport_channel_->writable()) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1260 | ChannelWritable_n(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1264 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | bool BaseChannel::AddRecvStream_w(const StreamParams& sp) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1268 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
pbos | 482b12e | 2015-11-16 10:19:58 -0800 | [diff] [blame] | 1269 | return media_channel()->AddRecvStream(sp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1272 | bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1273 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1274 | return media_channel()->RemoveRecvStream(ssrc); |
| 1275 | } |
| 1276 | |
| 1277 | bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1278 | ContentAction action, |
| 1279 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1280 | if (!VERIFY(action == CA_OFFER || action == CA_ANSWER || |
| 1281 | action == CA_PRANSWER || action == CA_UPDATE)) |
| 1282 | return false; |
| 1283 | |
| 1284 | // If this is an update, streams only contain streams that have changed. |
| 1285 | if (action == CA_UPDATE) { |
| 1286 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1287 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1288 | const StreamParams* existing_stream = |
| 1289 | GetStreamByIds(local_streams_, it->groupid, it->id); |
| 1290 | if (!existing_stream && it->has_ssrcs()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1291 | if (media_channel()->AddSendStream(*it)) { |
| 1292 | local_streams_.push_back(*it); |
| 1293 | LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc(); |
| 1294 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1295 | std::ostringstream desc; |
| 1296 | desc << "Failed to add send stream ssrc: " << it->first_ssrc(); |
| 1297 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1298 | return false; |
| 1299 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1300 | } else if (existing_stream && !it->has_ssrcs()) { |
| 1301 | if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1302 | std::ostringstream desc; |
| 1303 | desc << "Failed to remove send stream with ssrc " |
| 1304 | << it->first_ssrc() << "."; |
| 1305 | SafeSetError(desc.str(), error_desc); |
| 1306 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1307 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1308 | RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1309 | } else { |
| 1310 | LOG(LS_WARNING) << "Ignore unsupported stream update"; |
| 1311 | } |
| 1312 | } |
| 1313 | return true; |
| 1314 | } |
| 1315 | // Else streams are all the streams we want to send. |
| 1316 | |
| 1317 | // Check for streams that have been removed. |
| 1318 | bool ret = true; |
| 1319 | for (StreamParamsVec::const_iterator it = local_streams_.begin(); |
| 1320 | it != local_streams_.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1321 | if (!GetStreamBySsrc(streams, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1322 | if (!media_channel()->RemoveSendStream(it->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1323 | std::ostringstream desc; |
| 1324 | desc << "Failed to remove send stream with ssrc " |
| 1325 | << it->first_ssrc() << "."; |
| 1326 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1327 | ret = false; |
| 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | // Check for new streams. |
| 1332 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1333 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1334 | if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1335 | if (media_channel()->AddSendStream(*it)) { |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 1336 | LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1337 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1338 | std::ostringstream desc; |
| 1339 | desc << "Failed to add send stream ssrc: " << it->first_ssrc(); |
| 1340 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1341 | ret = false; |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | local_streams_ = streams; |
| 1346 | return ret; |
| 1347 | } |
| 1348 | |
| 1349 | bool BaseChannel::UpdateRemoteStreams_w( |
| 1350 | const std::vector<StreamParams>& streams, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1351 | ContentAction action, |
| 1352 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1353 | if (!VERIFY(action == CA_OFFER || action == CA_ANSWER || |
| 1354 | action == CA_PRANSWER || action == CA_UPDATE)) |
| 1355 | return false; |
| 1356 | |
| 1357 | // If this is an update, streams only contain streams that have changed. |
| 1358 | if (action == CA_UPDATE) { |
| 1359 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1360 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1361 | const StreamParams* existing_stream = |
| 1362 | GetStreamByIds(remote_streams_, it->groupid, it->id); |
| 1363 | if (!existing_stream && it->has_ssrcs()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1364 | if (AddRecvStream_w(*it)) { |
| 1365 | remote_streams_.push_back(*it); |
| 1366 | LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc(); |
| 1367 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1368 | std::ostringstream desc; |
| 1369 | desc << "Failed to add remote stream ssrc: " << it->first_ssrc(); |
| 1370 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1371 | return false; |
| 1372 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1373 | } else if (existing_stream && !it->has_ssrcs()) { |
| 1374 | if (!RemoveRecvStream_w(existing_stream->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1375 | std::ostringstream desc; |
| 1376 | desc << "Failed to remove remote stream with ssrc " |
| 1377 | << it->first_ssrc() << "."; |
| 1378 | SafeSetError(desc.str(), error_desc); |
| 1379 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1380 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1381 | RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1382 | } else { |
| 1383 | LOG(LS_WARNING) << "Ignore unsupported stream update." |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1384 | << " Stream exists? " << (existing_stream != nullptr) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1385 | << " new stream = " << it->ToString(); |
| 1386 | } |
| 1387 | } |
| 1388 | return true; |
| 1389 | } |
| 1390 | // Else streams are all the streams we want to receive. |
| 1391 | |
| 1392 | // Check for streams that have been removed. |
| 1393 | bool ret = true; |
| 1394 | for (StreamParamsVec::const_iterator it = remote_streams_.begin(); |
| 1395 | it != remote_streams_.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1396 | if (!GetStreamBySsrc(streams, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1397 | if (!RemoveRecvStream_w(it->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1398 | std::ostringstream desc; |
| 1399 | desc << "Failed to remove remote stream with ssrc " |
| 1400 | << it->first_ssrc() << "."; |
| 1401 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1402 | ret = false; |
| 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | // Check for new streams. |
| 1407 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1408 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1409 | if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1410 | if (AddRecvStream_w(*it)) { |
| 1411 | LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0]; |
| 1412 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1413 | std::ostringstream desc; |
| 1414 | desc << "Failed to add remote stream ssrc: " << it->first_ssrc(); |
| 1415 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1416 | ret = false; |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | remote_streams_ = streams; |
| 1421 | return ret; |
| 1422 | } |
| 1423 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1424 | void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w( |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 1425 | const std::vector<webrtc::RtpExtension>& extensions) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1426 | // Absolute Send Time extension id is used only with external auth, |
| 1427 | // so do not bother searching for it and making asyncronious call to set |
| 1428 | // something that is not used. |
| 1429 | #if defined(ENABLE_EXTERNAL_AUTH) |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 1430 | const webrtc::RtpExtension* send_time_extension = |
| 1431 | FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1432 | int rtp_abs_sendtime_extn_id = |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 1433 | send_time_extension ? send_time_extension->id : -1; |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1434 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1435 | RTC_FROM_HERE, network_thread_, |
| 1436 | Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this, |
| 1437 | rtp_abs_sendtime_extn_id)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1438 | #endif |
| 1439 | } |
| 1440 | |
| 1441 | void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n( |
| 1442 | int rtp_abs_sendtime_extn_id) { |
| 1443 | rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id; |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1446 | void BaseChannel::OnMessage(rtc::Message *pmsg) { |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 1447 | TRACE_EVENT0("webrtc", "BaseChannel::OnMessage"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1448 | switch (pmsg->message_id) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1449 | case MSG_SEND_RTP_PACKET: |
| 1450 | case MSG_SEND_RTCP_PACKET: { |
| 1451 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1452 | SendPacketMessageData* data = |
| 1453 | static_cast<SendPacketMessageData*>(pmsg->pdata); |
| 1454 | bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET; |
| 1455 | SendPacket(rtcp, &data->packet, data->options); |
| 1456 | delete data; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1457 | break; |
| 1458 | } |
| 1459 | case MSG_FIRSTPACKETRECEIVED: { |
| 1460 | SignalFirstPacketReceived(this); |
| 1461 | break; |
| 1462 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1463 | } |
| 1464 | } |
| 1465 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1466 | void BaseChannel::FlushRtcpMessages_n() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1467 | // Flush all remaining RTCP messages. This should only be called in |
| 1468 | // destructor. |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1469 | RTC_DCHECK(network_thread_->IsCurrent()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1470 | rtc::MessageList rtcp_messages; |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1471 | network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages); |
| 1472 | for (const auto& message : rtcp_messages) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1473 | network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET, |
| 1474 | message.pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1478 | void BaseChannel::SignalSentPacket_n(TransportChannel* /* channel */, |
| 1479 | const rtc::SentPacket& sent_packet) { |
| 1480 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1481 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1482 | RTC_FROM_HERE, worker_thread_, |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1483 | rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet)); |
| 1484 | } |
| 1485 | |
| 1486 | void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 1487 | RTC_DCHECK(worker_thread_->IsCurrent()); |
| 1488 | SignalSentPacket(sent_packet); |
| 1489 | } |
| 1490 | |
| 1491 | VoiceChannel::VoiceChannel(rtc::Thread* worker_thread, |
| 1492 | rtc::Thread* network_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1493 | MediaEngineInterface* media_engine, |
| 1494 | VoiceMediaChannel* media_channel, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1495 | TransportController* transport_controller, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1496 | const std::string& content_name, |
| 1497 | bool rtcp) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1498 | : BaseChannel(worker_thread, |
| 1499 | network_thread, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1500 | media_channel, |
| 1501 | transport_controller, |
| 1502 | content_name, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1503 | rtcp), |
Fredrik Solenberg | 0c02264 | 2015-08-05 12:25:22 +0200 | [diff] [blame] | 1504 | media_engine_(media_engine), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1505 | received_media_(false) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1506 | |
| 1507 | VoiceChannel::~VoiceChannel() { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 1508 | TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1509 | StopAudioMonitor(); |
| 1510 | StopMediaMonitor(); |
| 1511 | // this can't be done in the base class, since it calls a virtual |
| 1512 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1513 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1516 | bool VoiceChannel::Init_w(const std::string* bundle_transport_name) { |
| 1517 | if (!BaseChannel::Init_w(bundle_transport_name)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1518 | return false; |
| 1519 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1520 | return true; |
| 1521 | } |
| 1522 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1523 | bool VoiceChannel::SetAudioSend(uint32_t ssrc, |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 1524 | bool enable, |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1525 | const AudioOptions* options, |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1526 | AudioSource* source) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1527 | return InvokeOnWorker(RTC_FROM_HERE, |
| 1528 | Bind(&VoiceMediaChannel::SetAudioSend, media_channel(), |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1529 | ssrc, enable, options, source)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1532 | // TODO(juberti): Handle early media the right way. We should get an explicit |
| 1533 | // ringing message telling us to start playing local ringback, which we cancel |
| 1534 | // if any early media actually arrives. For now, we do the opposite, which is |
| 1535 | // to wait 1 second for early media, and start playing local ringback if none |
| 1536 | // arrives. |
| 1537 | void VoiceChannel::SetEarlyMedia(bool enable) { |
| 1538 | if (enable) { |
| 1539 | // Start the early media timeout |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1540 | worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this, |
| 1541 | MSG_EARLYMEDIATIMEOUT); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1542 | } else { |
| 1543 | // Stop the timeout if currently going. |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1544 | worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1548 | bool VoiceChannel::CanInsertDtmf() { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1549 | return InvokeOnWorker( |
| 1550 | RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1553 | bool VoiceChannel::InsertDtmf(uint32_t ssrc, |
| 1554 | int event_code, |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1555 | int duration) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1556 | return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this, |
| 1557 | ssrc, event_code, duration)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 1560 | bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1561 | return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume, |
| 1562 | media_channel(), ssrc, volume)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1563 | } |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1564 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 1565 | void VoiceChannel::SetRawAudioSink( |
| 1566 | uint32_t ssrc, |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 1567 | std::unique_ptr<webrtc::AudioSinkInterface> sink) { |
| 1568 | // We need to work around Bind's lack of support for unique_ptr and ownership |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 1569 | // passing. So we invoke to our own little routine that gets a pointer to |
| 1570 | // our local variable. This is OK since we're synchronously invoking. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1571 | InvokeOnWorker(RTC_FROM_HERE, |
| 1572 | Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink)); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1575 | webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const { |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1576 | return worker_thread()->Invoke<webrtc::RtpParameters>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1577 | RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc)); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1580 | webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w( |
| 1581 | uint32_t ssrc) const { |
| 1582 | return media_channel()->GetRtpSendParameters(ssrc); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1585 | bool VoiceChannel::SetRtpSendParameters( |
| 1586 | uint32_t ssrc, |
| 1587 | const webrtc::RtpParameters& parameters) { |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1588 | return InvokeOnWorker( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1589 | RTC_FROM_HERE, |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1590 | Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters)); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1593 | bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc, |
| 1594 | webrtc::RtpParameters parameters) { |
| 1595 | return media_channel()->SetRtpSendParameters(ssrc, parameters); |
| 1596 | } |
| 1597 | |
| 1598 | webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters( |
| 1599 | uint32_t ssrc) const { |
| 1600 | return worker_thread()->Invoke<webrtc::RtpParameters>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1601 | RTC_FROM_HERE, |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1602 | Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc)); |
| 1603 | } |
| 1604 | |
| 1605 | webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w( |
| 1606 | uint32_t ssrc) const { |
| 1607 | return media_channel()->GetRtpReceiveParameters(ssrc); |
| 1608 | } |
| 1609 | |
| 1610 | bool VoiceChannel::SetRtpReceiveParameters( |
| 1611 | uint32_t ssrc, |
| 1612 | const webrtc::RtpParameters& parameters) { |
| 1613 | return InvokeOnWorker( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1614 | RTC_FROM_HERE, |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1615 | Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters)); |
| 1616 | } |
| 1617 | |
| 1618 | bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc, |
| 1619 | webrtc::RtpParameters parameters) { |
| 1620 | return media_channel()->SetRtpReceiveParameters(ssrc, parameters); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1623 | bool VoiceChannel::GetStats(VoiceMediaInfo* stats) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1624 | return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats, |
| 1625 | media_channel(), stats)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | void VoiceChannel::StartMediaMonitor(int cms) { |
| 1629 | media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1630 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1631 | media_monitor_->SignalUpdate.connect( |
| 1632 | this, &VoiceChannel::OnMediaMonitorUpdate); |
| 1633 | media_monitor_->Start(cms); |
| 1634 | } |
| 1635 | |
| 1636 | void VoiceChannel::StopMediaMonitor() { |
| 1637 | if (media_monitor_) { |
| 1638 | media_monitor_->Stop(); |
| 1639 | media_monitor_->SignalUpdate.disconnect(this); |
| 1640 | media_monitor_.reset(); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | void VoiceChannel::StartAudioMonitor(int cms) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1645 | audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1646 | audio_monitor_ |
| 1647 | ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate); |
| 1648 | audio_monitor_->Start(cms); |
| 1649 | } |
| 1650 | |
| 1651 | void VoiceChannel::StopAudioMonitor() { |
| 1652 | if (audio_monitor_) { |
| 1653 | audio_monitor_->Stop(); |
| 1654 | audio_monitor_.reset(); |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | bool VoiceChannel::IsAudioMonitorRunning() const { |
| 1659 | return (audio_monitor_.get() != NULL); |
| 1660 | } |
| 1661 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1662 | int VoiceChannel::GetInputLevel_w() { |
Fredrik Solenberg | 0c02264 | 2015-08-05 12:25:22 +0200 | [diff] [blame] | 1663 | return media_engine_->GetInputLevel(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | int VoiceChannel::GetOutputLevel_w() { |
| 1667 | return media_channel()->GetOutputLevel(); |
| 1668 | } |
| 1669 | |
| 1670 | void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) { |
| 1671 | media_channel()->GetActiveStreams(actives); |
| 1672 | } |
| 1673 | |
| 1674 | void VoiceChannel::OnChannelRead(TransportChannel* channel, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 1675 | const char* data, size_t len, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1676 | const rtc::PacketTime& packet_time, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 1677 | int flags) { |
| 1678 | BaseChannel::OnChannelRead(channel, data, len, packet_time, flags); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1679 | |
| 1680 | // Set a flag when we've received an RTP packet. If we're waiting for early |
| 1681 | // media, this will disable the timeout. |
| 1682 | if (!received_media_ && !PacketIsRtcp(channel, data, len)) { |
| 1683 | received_media_ = true; |
| 1684 | } |
| 1685 | } |
| 1686 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1687 | void BaseChannel::UpdateMediaSendRecvState() { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1688 | RTC_DCHECK(network_thread_->IsCurrent()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1689 | invoker_.AsyncInvoke<void>( |
| 1690 | RTC_FROM_HERE, worker_thread_, |
| 1691 | Bind(&BaseChannel::UpdateMediaSendRecvState_w, this)); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1692 | } |
| 1693 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1694 | void VoiceChannel::UpdateMediaSendRecvState_w() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1695 | // Render incoming data if we're the active call, and we have the local |
| 1696 | // content. We receive data on the default channel and multiplexed streams. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1697 | bool recv = IsReadyToReceiveMedia_w(); |
solenberg | 5b14b42 | 2015-10-01 04:10:31 -0700 | [diff] [blame] | 1698 | media_channel()->SetPlayout(recv); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1699 | |
| 1700 | // Send outgoing data if we're the active call, we have the remote content, |
| 1701 | // and we have had some form of connectivity. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1702 | bool send = IsReadyToSendMedia_w(); |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1703 | media_channel()->SetSend(send); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1704 | |
| 1705 | LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send; |
| 1706 | } |
| 1707 | |
| 1708 | const ContentInfo* VoiceChannel::GetFirstContent( |
| 1709 | const SessionDescription* sdesc) { |
| 1710 | return GetFirstAudioContent(sdesc); |
| 1711 | } |
| 1712 | |
| 1713 | bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1714 | ContentAction action, |
| 1715 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 1716 | TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w"); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1717 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1718 | LOG(LS_INFO) << "Setting local voice description"; |
| 1719 | |
| 1720 | const AudioContentDescription* audio = |
| 1721 | static_cast<const AudioContentDescription*>(content); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1722 | RTC_DCHECK(audio != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1723 | if (!audio) { |
| 1724 | SafeSetError("Can't find audio content in local description.", error_desc); |
| 1725 | return false; |
| 1726 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1727 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1728 | if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1729 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1732 | AudioRecvParameters recv_params = last_recv_params_; |
| 1733 | RtpParametersFromMediaDescription(audio, &recv_params); |
| 1734 | if (!media_channel()->SetRecvParameters(recv_params)) { |
Peter Thatcher | bfab5cb | 2015-08-20 17:40:24 -0700 | [diff] [blame] | 1735 | SafeSetError("Failed to set local audio description recv parameters.", |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1736 | error_desc); |
| 1737 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1738 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1739 | for (const AudioCodec& codec : audio->codecs()) { |
| 1740 | bundle_filter()->AddPayloadType(codec.id); |
| 1741 | } |
| 1742 | last_recv_params_ = recv_params; |
| 1743 | |
| 1744 | // TODO(pthatcher): Move local streams into AudioSendParameters, and |
| 1745 | // only give it to the media channel once we have a remote |
| 1746 | // description too (without a remote description, we won't be able |
| 1747 | // to send them anyway). |
| 1748 | if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) { |
| 1749 | SafeSetError("Failed to set local audio description streams.", error_desc); |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | set_local_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1754 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1755 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1759 | ContentAction action, |
| 1760 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 1761 | TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w"); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1762 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1763 | LOG(LS_INFO) << "Setting remote voice description"; |
| 1764 | |
| 1765 | const AudioContentDescription* audio = |
| 1766 | static_cast<const AudioContentDescription*>(content); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1767 | RTC_DCHECK(audio != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1768 | if (!audio) { |
| 1769 | SafeSetError("Can't find audio content in remote description.", error_desc); |
| 1770 | return false; |
| 1771 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1772 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1773 | if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1774 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1777 | AudioSendParameters send_params = last_send_params_; |
| 1778 | RtpSendParametersFromMediaDescription(audio, &send_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1779 | if (audio->agc_minus_10db()) { |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 1780 | send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1781 | } |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1782 | |
| 1783 | bool parameters_applied = media_channel()->SetSendParameters(send_params); |
| 1784 | if (!parameters_applied) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1785 | SafeSetError("Failed to set remote audio description send parameters.", |
| 1786 | error_desc); |
| 1787 | return false; |
| 1788 | } |
| 1789 | last_send_params_ = send_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1790 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1791 | // TODO(pthatcher): Move remote streams into AudioRecvParameters, |
| 1792 | // and only give it to the media channel once we have a local |
| 1793 | // description too (without a local description, we won't be able to |
| 1794 | // recv them anyway). |
| 1795 | if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) { |
| 1796 | SafeSetError("Failed to set remote audio description streams.", error_desc); |
| 1797 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Peter Thatcher | bfab5cb | 2015-08-20 17:40:24 -0700 | [diff] [blame] | 1800 | if (audio->rtp_header_extensions_set()) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1801 | MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions()); |
Peter Thatcher | bfab5cb | 2015-08-20 17:40:24 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1804 | set_remote_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1805 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1806 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1809 | void VoiceChannel::HandleEarlyMediaTimeout() { |
| 1810 | // This occurs on the main thread, not the worker thread. |
| 1811 | if (!received_media_) { |
| 1812 | LOG(LS_INFO) << "No early media received before timeout"; |
| 1813 | SignalEarlyMediaTimeout(this); |
| 1814 | } |
| 1815 | } |
| 1816 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1817 | bool VoiceChannel::InsertDtmf_w(uint32_t ssrc, |
| 1818 | int event, |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1819 | int duration) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1820 | if (!enabled()) { |
| 1821 | return false; |
| 1822 | } |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1823 | return media_channel()->InsertDtmf(ssrc, event, duration); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1826 | void VoiceChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1827 | switch (pmsg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1828 | case MSG_EARLYMEDIATIMEOUT: |
| 1829 | HandleEarlyMediaTimeout(); |
| 1830 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1831 | case MSG_CHANNEL_ERROR: { |
| 1832 | VoiceChannelErrorMessageData* data = |
| 1833 | static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1834 | delete data; |
| 1835 | break; |
| 1836 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1837 | default: |
| 1838 | BaseChannel::OnMessage(pmsg); |
| 1839 | break; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | void VoiceChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 1844 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1845 | SignalConnectionMonitor(this, infos); |
| 1846 | } |
| 1847 | |
| 1848 | void VoiceChannel::OnMediaMonitorUpdate( |
| 1849 | VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1850 | RTC_DCHECK(media_channel == this->media_channel()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1851 | SignalMediaMonitor(this, info); |
| 1852 | } |
| 1853 | |
| 1854 | void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor, |
| 1855 | const AudioInfo& info) { |
| 1856 | SignalAudioMonitor(this, info); |
| 1857 | } |
| 1858 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1859 | void VoiceChannel::GetSrtpCryptoSuites_n( |
| 1860 | std::vector<int>* crypto_suites) const { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 1861 | GetSupportedAudioCryptoSuites(crypto_options(), crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1864 | VideoChannel::VideoChannel(rtc::Thread* worker_thread, |
| 1865 | rtc::Thread* network_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1866 | VideoMediaChannel* media_channel, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1867 | TransportController* transport_controller, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1868 | const std::string& content_name, |
Fredrik Solenberg | 7fb711f | 2015-04-22 15:30:51 +0200 | [diff] [blame] | 1869 | bool rtcp) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1870 | : BaseChannel(worker_thread, |
| 1871 | network_thread, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1872 | media_channel, |
| 1873 | transport_controller, |
| 1874 | content_name, |
perkj | c11b184 | 2016-03-07 17:34:13 -0800 | [diff] [blame] | 1875 | rtcp) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1876 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1877 | bool VideoChannel::Init_w(const std::string* bundle_transport_name) { |
| 1878 | if (!BaseChannel::Init_w(bundle_transport_name)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1879 | return false; |
| 1880 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1881 | return true; |
| 1882 | } |
| 1883 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1884 | VideoChannel::~VideoChannel() { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 1885 | TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1886 | StopMediaMonitor(); |
| 1887 | // this can't be done in the base class, since it calls a virtual |
| 1888 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1889 | |
| 1890 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
nisse | 08582ff | 2016-02-04 01:24:52 -0800 | [diff] [blame] | 1893 | bool VideoChannel::SetSink(uint32_t ssrc, |
| 1894 | rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 1895 | worker_thread()->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1896 | RTC_FROM_HERE, |
nisse | 08582ff | 2016-02-04 01:24:52 -0800 | [diff] [blame] | 1897 | Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1898 | return true; |
| 1899 | } |
| 1900 | |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 1901 | bool VideoChannel::SetVideoSend( |
nisse | 2ded9b1 | 2016-04-08 02:23:55 -0700 | [diff] [blame] | 1902 | uint32_t ssrc, |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 1903 | bool mute, |
| 1904 | const VideoOptions* options, |
nisse | 2ded9b1 | 2016-04-08 02:23:55 -0700 | [diff] [blame] | 1905 | rtc::VideoSourceInterface<cricket::VideoFrame>* source) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1906 | return InvokeOnWorker(RTC_FROM_HERE, |
| 1907 | Bind(&VideoMediaChannel::SetVideoSend, media_channel(), |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 1908 | ssrc, mute, options, source)); |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1911 | webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const { |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1912 | return worker_thread()->Invoke<webrtc::RtpParameters>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1913 | RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc)); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1916 | webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w( |
| 1917 | uint32_t ssrc) const { |
| 1918 | return media_channel()->GetRtpSendParameters(ssrc); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1921 | bool VideoChannel::SetRtpSendParameters( |
| 1922 | uint32_t ssrc, |
| 1923 | const webrtc::RtpParameters& parameters) { |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1924 | return InvokeOnWorker( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1925 | RTC_FROM_HERE, |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1926 | Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters)); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1929 | bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc, |
| 1930 | webrtc::RtpParameters parameters) { |
| 1931 | return media_channel()->SetRtpSendParameters(ssrc, parameters); |
| 1932 | } |
| 1933 | |
| 1934 | webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters( |
| 1935 | uint32_t ssrc) const { |
| 1936 | return worker_thread()->Invoke<webrtc::RtpParameters>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1937 | RTC_FROM_HERE, |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1938 | Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc)); |
| 1939 | } |
| 1940 | |
| 1941 | webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w( |
| 1942 | uint32_t ssrc) const { |
| 1943 | return media_channel()->GetRtpReceiveParameters(ssrc); |
| 1944 | } |
| 1945 | |
| 1946 | bool VideoChannel::SetRtpReceiveParameters( |
| 1947 | uint32_t ssrc, |
| 1948 | const webrtc::RtpParameters& parameters) { |
| 1949 | return InvokeOnWorker( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1950 | RTC_FROM_HERE, |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1951 | Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters)); |
| 1952 | } |
| 1953 | |
| 1954 | bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc, |
| 1955 | webrtc::RtpParameters parameters) { |
| 1956 | return media_channel()->SetRtpReceiveParameters(ssrc, parameters); |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 1957 | } |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1958 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1959 | void VideoChannel::UpdateMediaSendRecvState_w() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1960 | // Send outgoing data if we're the active call, we have the remote content, |
| 1961 | // and we have had some form of connectivity. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 1962 | bool send = IsReadyToSendMedia_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1963 | if (!media_channel()->SetSend(send)) { |
| 1964 | LOG(LS_ERROR) << "Failed to SetSend on video channel"; |
| 1965 | // TODO(gangji): Report error back to server. |
| 1966 | } |
| 1967 | |
Peter Boström | 34fbfff | 2015-09-24 19:20:30 +0200 | [diff] [blame] | 1968 | LOG(LS_INFO) << "Changing video state, send=" << send; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
pbos@webrtc.org | 058b1f1 | 2015-03-04 08:54:32 +0000 | [diff] [blame] | 1971 | bool VideoChannel::GetStats(VideoMediaInfo* stats) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1972 | return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats, |
| 1973 | media_channel(), stats)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | void VideoChannel::StartMediaMonitor(int cms) { |
| 1977 | media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1978 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1979 | media_monitor_->SignalUpdate.connect( |
| 1980 | this, &VideoChannel::OnMediaMonitorUpdate); |
| 1981 | media_monitor_->Start(cms); |
| 1982 | } |
| 1983 | |
| 1984 | void VideoChannel::StopMediaMonitor() { |
| 1985 | if (media_monitor_) { |
| 1986 | media_monitor_->Stop(); |
| 1987 | media_monitor_.reset(); |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | const ContentInfo* VideoChannel::GetFirstContent( |
| 1992 | const SessionDescription* sdesc) { |
| 1993 | return GetFirstVideoContent(sdesc); |
| 1994 | } |
| 1995 | |
| 1996 | bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1997 | ContentAction action, |
| 1998 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 1999 | TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w"); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2000 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2001 | LOG(LS_INFO) << "Setting local video description"; |
| 2002 | |
| 2003 | const VideoContentDescription* video = |
| 2004 | static_cast<const VideoContentDescription*>(content); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2005 | RTC_DCHECK(video != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2006 | if (!video) { |
| 2007 | SafeSetError("Can't find video content in local description.", error_desc); |
| 2008 | return false; |
| 2009 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2010 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 2011 | if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2012 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2015 | VideoRecvParameters recv_params = last_recv_params_; |
| 2016 | RtpParametersFromMediaDescription(video, &recv_params); |
| 2017 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 2018 | SafeSetError("Failed to set local video description recv parameters.", |
| 2019 | error_desc); |
| 2020 | return false; |
| 2021 | } |
| 2022 | for (const VideoCodec& codec : video->codecs()) { |
| 2023 | bundle_filter()->AddPayloadType(codec.id); |
| 2024 | } |
| 2025 | last_recv_params_ = recv_params; |
| 2026 | |
| 2027 | // TODO(pthatcher): Move local streams into VideoSendParameters, and |
| 2028 | // only give it to the media channel once we have a remote |
| 2029 | // description too (without a remote description, we won't be able |
| 2030 | // to send them anyway). |
| 2031 | if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) { |
| 2032 | SafeSetError("Failed to set local video description streams.", error_desc); |
| 2033 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2036 | set_local_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2037 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2038 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2042 | ContentAction action, |
| 2043 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 2044 | TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w"); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2045 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2046 | LOG(LS_INFO) << "Setting remote video description"; |
| 2047 | |
| 2048 | const VideoContentDescription* video = |
| 2049 | static_cast<const VideoContentDescription*>(content); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2050 | RTC_DCHECK(video != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2051 | if (!video) { |
| 2052 | SafeSetError("Can't find video content in remote description.", error_desc); |
| 2053 | return false; |
| 2054 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2055 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 2056 | if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2057 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2060 | VideoSendParameters send_params = last_send_params_; |
| 2061 | RtpSendParametersFromMediaDescription(video, &send_params); |
| 2062 | if (video->conference_mode()) { |
nisse | 4b4dc86 | 2016-02-17 05:25:36 -0800 | [diff] [blame] | 2063 | send_params.conference_mode = true; |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2064 | } |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 2065 | |
| 2066 | bool parameters_applied = media_channel()->SetSendParameters(send_params); |
| 2067 | |
| 2068 | if (!parameters_applied) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2069 | SafeSetError("Failed to set remote video description send parameters.", |
| 2070 | error_desc); |
| 2071 | return false; |
| 2072 | } |
| 2073 | last_send_params_ = send_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2074 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2075 | // TODO(pthatcher): Move remote streams into VideoRecvParameters, |
| 2076 | // and only give it to the media channel once we have a local |
| 2077 | // description too (without a local description, we won't be able to |
| 2078 | // recv them anyway). |
| 2079 | if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) { |
| 2080 | SafeSetError("Failed to set remote video description streams.", error_desc); |
| 2081 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2084 | if (video->rtp_header_extensions_set()) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 2085 | MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2086 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2087 | |
| 2088 | set_remote_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2089 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2090 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2093 | void VideoChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2094 | switch (pmsg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2095 | case MSG_CHANNEL_ERROR: { |
| 2096 | const VideoChannelErrorMessageData* data = |
| 2097 | static_cast<VideoChannelErrorMessageData*>(pmsg->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2098 | delete data; |
| 2099 | break; |
| 2100 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2101 | default: |
| 2102 | BaseChannel::OnMessage(pmsg); |
| 2103 | break; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | void VideoChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 2108 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2109 | SignalConnectionMonitor(this, infos); |
| 2110 | } |
| 2111 | |
| 2112 | // TODO(pthatcher): Look into removing duplicate code between |
| 2113 | // audio, video, and data, perhaps by using templates. |
| 2114 | void VideoChannel::OnMediaMonitorUpdate( |
| 2115 | VideoMediaChannel* media_channel, const VideoMediaInfo &info) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2116 | RTC_DCHECK(media_channel == this->media_channel()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2117 | SignalMediaMonitor(this, info); |
| 2118 | } |
| 2119 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 2120 | void VideoChannel::GetSrtpCryptoSuites_n( |
| 2121 | std::vector<int>* crypto_suites) const { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 2122 | GetSupportedVideoCryptoSuites(crypto_options(), crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 2125 | DataChannel::DataChannel(rtc::Thread* worker_thread, |
| 2126 | rtc::Thread* network_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2127 | DataMediaChannel* media_channel, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2128 | TransportController* transport_controller, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2129 | const std::string& content_name, |
| 2130 | bool rtcp) |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 2131 | : BaseChannel(worker_thread, |
| 2132 | network_thread, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2133 | media_channel, |
| 2134 | transport_controller, |
| 2135 | content_name, |
| 2136 | rtcp), |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 2137 | data_channel_type_(cricket::DCT_NONE), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2138 | ready_to_send_data_(false) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2139 | |
| 2140 | DataChannel::~DataChannel() { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 2141 | TRACE_EVENT0("webrtc", "DataChannel::~DataChannel"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2142 | StopMediaMonitor(); |
| 2143 | // this can't be done in the base class, since it calls a virtual |
| 2144 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 2145 | |
| 2146 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 2149 | bool DataChannel::Init_w(const std::string* bundle_transport_name) { |
| 2150 | if (!BaseChannel::Init_w(bundle_transport_name)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2151 | return false; |
| 2152 | } |
| 2153 | media_channel()->SignalDataReceived.connect( |
| 2154 | this, &DataChannel::OnDataReceived); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2155 | media_channel()->SignalReadyToSend.connect( |
| 2156 | this, &DataChannel::OnDataChannelReadyToSend); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2157 | media_channel()->SignalStreamClosedRemotely.connect( |
| 2158 | this, &DataChannel::OnStreamClosedRemotely); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2159 | return true; |
| 2160 | } |
| 2161 | |
| 2162 | bool DataChannel::SendData(const SendDataParams& params, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 2163 | const rtc::CopyOnWriteBuffer& payload, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2164 | SendDataResult* result) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 2165 | return InvokeOnWorker( |
| 2166 | RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params, |
| 2167 | payload, result)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | const ContentInfo* DataChannel::GetFirstContent( |
| 2171 | const SessionDescription* sdesc) { |
| 2172 | return GetFirstDataContent(sdesc); |
| 2173 | } |
| 2174 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 2175 | bool DataChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2176 | if (data_channel_type_ == DCT_SCTP) { |
| 2177 | // TODO(pthatcher): Do this in a more robust way by checking for |
| 2178 | // SCTP or DTLS. |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 2179 | return !IsRtpPacket(packet->data(), packet->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2180 | } else if (data_channel_type_ == DCT_RTP) { |
| 2181 | return BaseChannel::WantsPacket(rtcp, packet); |
| 2182 | } |
| 2183 | return false; |
| 2184 | } |
| 2185 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2186 | bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type, |
| 2187 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2188 | // It hasn't been set before, so set it now. |
| 2189 | if (data_channel_type_ == DCT_NONE) { |
| 2190 | data_channel_type_ = new_data_channel_type; |
| 2191 | return true; |
| 2192 | } |
| 2193 | |
| 2194 | // It's been set before, but doesn't match. That's bad. |
| 2195 | if (data_channel_type_ != new_data_channel_type) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2196 | std::ostringstream desc; |
| 2197 | desc << "Data channel type mismatch." |
| 2198 | << " Expected " << data_channel_type_ |
| 2199 | << " Got " << new_data_channel_type; |
| 2200 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2201 | return false; |
| 2202 | } |
| 2203 | |
| 2204 | // It's hasn't changed. Nothing to do. |
| 2205 | return true; |
| 2206 | } |
| 2207 | |
| 2208 | bool DataChannel::SetDataChannelTypeFromContent( |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2209 | const DataContentDescription* content, |
| 2210 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2211 | bool is_sctp = ((content->protocol() == kMediaProtocolSctp) || |
| 2212 | (content->protocol() == kMediaProtocolDtlsSctp)); |
| 2213 | DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2214 | return SetDataChannelType(data_channel_type, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | bool DataChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2218 | ContentAction action, |
| 2219 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 2220 | TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w"); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2221 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2222 | LOG(LS_INFO) << "Setting local data description"; |
| 2223 | |
| 2224 | const DataContentDescription* data = |
| 2225 | static_cast<const DataContentDescription*>(content); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2226 | RTC_DCHECK(data != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2227 | if (!data) { |
| 2228 | SafeSetError("Can't find data content in local description.", error_desc); |
| 2229 | return false; |
| 2230 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2231 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2232 | if (!SetDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2233 | return false; |
| 2234 | } |
| 2235 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2236 | if (data_channel_type_ == DCT_RTP) { |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 2237 | if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2238 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2239 | } |
| 2240 | } |
| 2241 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2242 | // FYI: We send the SCTP port number (not to be confused with the |
| 2243 | // underlying UDP port number) as a codec parameter. So even SCTP |
| 2244 | // data channels need codecs. |
| 2245 | DataRecvParameters recv_params = last_recv_params_; |
| 2246 | RtpParametersFromMediaDescription(data, &recv_params); |
| 2247 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 2248 | SafeSetError("Failed to set remote data description recv parameters.", |
| 2249 | error_desc); |
| 2250 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2251 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2252 | if (data_channel_type_ == DCT_RTP) { |
| 2253 | for (const DataCodec& codec : data->codecs()) { |
| 2254 | bundle_filter()->AddPayloadType(codec.id); |
| 2255 | } |
| 2256 | } |
| 2257 | last_recv_params_ = recv_params; |
| 2258 | |
| 2259 | // TODO(pthatcher): Move local streams into DataSendParameters, and |
| 2260 | // only give it to the media channel once we have a remote |
| 2261 | // description too (without a remote description, we won't be able |
| 2262 | // to send them anyway). |
| 2263 | if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) { |
| 2264 | SafeSetError("Failed to set local data description streams.", error_desc); |
| 2265 | return false; |
| 2266 | } |
| 2267 | |
| 2268 | set_local_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2269 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2270 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2274 | ContentAction action, |
| 2275 | std::string* error_desc) { |
Peter Boström | 9f45a45 | 2015-12-08 13:25:57 +0100 | [diff] [blame] | 2276 | TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w"); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2277 | RTC_DCHECK(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2278 | |
| 2279 | const DataContentDescription* data = |
| 2280 | static_cast<const DataContentDescription*>(content); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2281 | RTC_DCHECK(data != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2282 | if (!data) { |
| 2283 | SafeSetError("Can't find data content in remote description.", error_desc); |
| 2284 | return false; |
| 2285 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2286 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2287 | // If the remote data doesn't have codecs and isn't an update, it |
| 2288 | // must be empty, so ignore it. |
| 2289 | if (!data->has_codecs() && action != CA_UPDATE) { |
| 2290 | return true; |
| 2291 | } |
| 2292 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2293 | if (!SetDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2294 | return false; |
| 2295 | } |
| 2296 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2297 | LOG(LS_INFO) << "Setting remote data description"; |
| 2298 | if (data_channel_type_ == DCT_RTP && |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 2299 | !SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) { |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2300 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2303 | |
| 2304 | DataSendParameters send_params = last_send_params_; |
| 2305 | RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params); |
| 2306 | if (!media_channel()->SetSendParameters(send_params)) { |
| 2307 | SafeSetError("Failed to set remote data description send parameters.", |
| 2308 | error_desc); |
| 2309 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2310 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2311 | last_send_params_ = send_params; |
| 2312 | |
| 2313 | // TODO(pthatcher): Move remote streams into DataRecvParameters, |
| 2314 | // and only give it to the media channel once we have a local |
| 2315 | // description too (without a local description, we won't be able to |
| 2316 | // recv them anyway). |
| 2317 | if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) { |
| 2318 | SafeSetError("Failed to set remote data description streams.", |
| 2319 | error_desc); |
| 2320 | return false; |
| 2321 | } |
| 2322 | |
| 2323 | set_remote_content_direction(content->direction()); |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2324 | UpdateMediaSendRecvState_w(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2325 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2328 | void DataChannel::UpdateMediaSendRecvState_w() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2329 | // Render incoming data if we're the active call, and we have the local |
| 2330 | // content. We receive data on the default channel and multiplexed streams. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2331 | bool recv = IsReadyToReceiveMedia_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2332 | if (!media_channel()->SetReceive(recv)) { |
| 2333 | LOG(LS_ERROR) << "Failed to SetReceive on data channel"; |
| 2334 | } |
| 2335 | |
| 2336 | // Send outgoing data if we're the active call, we have the remote content, |
| 2337 | // and we have had some form of connectivity. |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2338 | bool send = IsReadyToSendMedia_w(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2339 | if (!media_channel()->SetSend(send)) { |
| 2340 | LOG(LS_ERROR) << "Failed to SetSend on data channel"; |
| 2341 | } |
| 2342 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 2343 | // Trigger SignalReadyToSendData asynchronously. |
| 2344 | OnDataChannelReadyToSend(send); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2345 | |
| 2346 | LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send; |
| 2347 | } |
| 2348 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2349 | void DataChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2350 | switch (pmsg->message_id) { |
| 2351 | case MSG_READYTOSENDDATA: { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2352 | DataChannelReadyToSendMessageData* data = |
| 2353 | static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 2354 | ready_to_send_data_ = data->data(); |
| 2355 | SignalReadyToSendData(ready_to_send_data_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2356 | delete data; |
| 2357 | break; |
| 2358 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2359 | case MSG_DATARECEIVED: { |
| 2360 | DataReceivedMessageData* data = |
| 2361 | static_cast<DataReceivedMessageData*>(pmsg->pdata); |
| 2362 | SignalDataReceived(this, data->params, data->payload); |
| 2363 | delete data; |
| 2364 | break; |
| 2365 | } |
| 2366 | case MSG_CHANNEL_ERROR: { |
| 2367 | const DataChannelErrorMessageData* data = |
| 2368 | static_cast<DataChannelErrorMessageData*>(pmsg->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2369 | delete data; |
| 2370 | break; |
| 2371 | } |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2372 | case MSG_STREAMCLOSEDREMOTELY: { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2373 | rtc::TypedMessageData<uint32_t>* data = |
| 2374 | static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2375 | SignalStreamClosedRemotely(data->data()); |
| 2376 | delete data; |
| 2377 | break; |
| 2378 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2379 | default: |
| 2380 | BaseChannel::OnMessage(pmsg); |
| 2381 | break; |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | void DataChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 2386 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2387 | SignalConnectionMonitor(this, infos); |
| 2388 | } |
| 2389 | |
| 2390 | void DataChannel::StartMediaMonitor(int cms) { |
| 2391 | media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2392 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2393 | media_monitor_->SignalUpdate.connect( |
| 2394 | this, &DataChannel::OnMediaMonitorUpdate); |
| 2395 | media_monitor_->Start(cms); |
| 2396 | } |
| 2397 | |
| 2398 | void DataChannel::StopMediaMonitor() { |
| 2399 | if (media_monitor_) { |
| 2400 | media_monitor_->Stop(); |
| 2401 | media_monitor_->SignalUpdate.disconnect(this); |
| 2402 | media_monitor_.reset(); |
| 2403 | } |
| 2404 | } |
| 2405 | |
| 2406 | void DataChannel::OnMediaMonitorUpdate( |
| 2407 | DataMediaChannel* media_channel, const DataMediaInfo& info) { |
Taylor Brandstetter | bad33bf | 2016-08-25 13:31:14 -0700 | [diff] [blame^] | 2408 | RTC_DCHECK(media_channel == this->media_channel()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2409 | SignalMediaMonitor(this, info); |
| 2410 | } |
| 2411 | |
| 2412 | void DataChannel::OnDataReceived( |
| 2413 | const ReceiveDataParams& params, const char* data, size_t len) { |
| 2414 | DataReceivedMessageData* msg = new DataReceivedMessageData( |
| 2415 | params, data, len); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 2416 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2419 | void DataChannel::OnDataChannelError(uint32_t ssrc, |
| 2420 | DataMediaChannel::Error err) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2421 | DataChannelErrorMessageData* data = new DataChannelErrorMessageData( |
| 2422 | ssrc, err); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 2423 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2426 | void DataChannel::OnDataChannelReadyToSend(bool writable) { |
| 2427 | // This is usded for congestion control to indicate that the stream is ready |
| 2428 | // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates |
| 2429 | // that the transport channel is ready. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 2430 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA, |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2431 | new DataChannelReadyToSendMessageData(writable)); |
| 2432 | } |
| 2433 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 2434 | void DataChannel::GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 2435 | GetSupportedDataCryptoSuites(crypto_options(), crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 2438 | bool DataChannel::ShouldSetupDtlsSrtp_n() const { |
| 2439 | return data_channel_type_ == DCT_RTP && BaseChannel::ShouldSetupDtlsSrtp_n(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2442 | void DataChannel::OnStreamClosedRemotely(uint32_t sid) { |
| 2443 | rtc::TypedMessageData<uint32_t>* message = |
| 2444 | new rtc::TypedMessageData<uint32_t>(sid); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 2445 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_STREAMCLOSEDREMOTELY, |
| 2446 | message); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2449 | } // namespace cricket |