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