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