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