henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "talk/session/media/channel.h" |
| 29 | |
buildbot@webrtc.org | 5b1ebac | 2014-08-07 17:18:00 +0000 | [diff] [blame] | 30 | #include "talk/media/base/constants.h" |
| 31 | #include "talk/media/base/rtputils.h" |
buildbot@webrtc.org | 5b1ebac | 2014-08-07 17:18:00 +0000 | [diff] [blame] | 32 | #include "talk/session/media/channelmanager.h" |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 33 | #include "webrtc/base/bind.h" |
| 34 | #include "webrtc/base/buffer.h" |
| 35 | #include "webrtc/base/byteorder.h" |
| 36 | #include "webrtc/base/common.h" |
| 37 | #include "webrtc/base/dscp.h" |
| 38 | #include "webrtc/base/logging.h" |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame^] | 39 | #include "webrtc/base/trace_event.h" |
| 40 | #include "webrtc/p2p/base/transportchannel.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | |
| 42 | namespace cricket { |
| 43 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 44 | using rtc::Bind; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 45 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | enum { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 47 | MSG_EARLYMEDIATIMEOUT = 1, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | MSG_SCREENCASTWINDOWEVENT, |
| 49 | MSG_RTPPACKET, |
| 50 | MSG_RTCPPACKET, |
| 51 | MSG_CHANNEL_ERROR, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | MSG_READYTOSENDDATA, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | MSG_DATARECEIVED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | MSG_FIRSTPACKETRECEIVED, |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 55 | MSG_STREAMCLOSEDREMOTELY, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | // Value specified in RFC 5764. |
| 59 | static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp"; |
| 60 | |
| 61 | static const int kAgcMinus10db = -10; |
| 62 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 63 | static void SafeSetError(const std::string& message, std::string* error_desc) { |
| 64 | if (error_desc) { |
| 65 | *error_desc = message; |
| 66 | } |
| 67 | } |
| 68 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 69 | struct PacketMessageData : public rtc::MessageData { |
| 70 | rtc::Buffer packet; |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 71 | rtc::PacketOptions options; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 74 | struct ScreencastEventMessageData : public rtc::MessageData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 75 | ScreencastEventMessageData(uint32_t s, rtc::WindowEvent we) |
| 76 | : ssrc(s), event(we) {} |
| 77 | uint32_t ssrc; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 78 | rtc::WindowEvent event; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 81 | struct VoiceChannelErrorMessageData : public rtc::MessageData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 82 | VoiceChannelErrorMessageData(uint32_t in_ssrc, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | VoiceMediaChannel::Error in_error) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 84 | : ssrc(in_ssrc), error(in_error) {} |
| 85 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 86 | VoiceMediaChannel::Error error; |
| 87 | }; |
| 88 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 89 | struct VideoChannelErrorMessageData : public rtc::MessageData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 90 | VideoChannelErrorMessageData(uint32_t in_ssrc, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | VideoMediaChannel::Error in_error) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 92 | : ssrc(in_ssrc), error(in_error) {} |
| 93 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | VideoMediaChannel::Error error; |
| 95 | }; |
| 96 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 97 | struct DataChannelErrorMessageData : public rtc::MessageData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 98 | DataChannelErrorMessageData(uint32_t in_ssrc, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | DataMediaChannel::Error in_error) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 100 | : ssrc(in_ssrc), error(in_error) {} |
| 101 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | DataMediaChannel::Error error; |
| 103 | }; |
| 104 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 106 | struct VideoChannel::ScreencastDetailsData { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 107 | explicit ScreencastDetailsData(uint32_t s) |
| 108 | : ssrc(s), fps(0), screencast_max_pixels(0) {} |
| 109 | uint32_t ssrc; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 110 | int fps; |
| 111 | int screencast_max_pixels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | static const char* PacketType(bool rtcp) { |
| 115 | return (!rtcp) ? "RTP" : "RTCP"; |
| 116 | } |
| 117 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 118 | static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | // Check the packet size. We could check the header too if needed. |
| 120 | return (packet && |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 121 | packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) && |
| 122 | packet->size() <= kMaxRtpPacketLen); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static bool IsReceiveContentDirection(MediaContentDirection direction) { |
| 126 | return direction == MD_SENDRECV || direction == MD_RECVONLY; |
| 127 | } |
| 128 | |
| 129 | static bool IsSendContentDirection(MediaContentDirection direction) { |
| 130 | return direction == MD_SENDRECV || direction == MD_SENDONLY; |
| 131 | } |
| 132 | |
| 133 | static const MediaContentDescription* GetContentDescription( |
| 134 | const ContentInfo* cinfo) { |
| 135 | if (cinfo == NULL) |
| 136 | return NULL; |
| 137 | return static_cast<const MediaContentDescription*>(cinfo->description); |
| 138 | } |
| 139 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 140 | template <class Codec> |
| 141 | void RtpParametersFromMediaDescription( |
| 142 | const MediaContentDescriptionImpl<Codec>* desc, |
| 143 | RtpParameters<Codec>* params) { |
| 144 | // TODO(pthatcher): Remove this once we're sure no one will give us |
| 145 | // a description without codecs (currently a CA_UPDATE with just |
| 146 | // streams can). |
| 147 | if (desc->has_codecs()) { |
| 148 | params->codecs = desc->codecs(); |
| 149 | } |
| 150 | // TODO(pthatcher): See if we really need |
| 151 | // rtp_header_extensions_set() and remove it if we don't. |
| 152 | if (desc->rtp_header_extensions_set()) { |
| 153 | params->extensions = desc->rtp_header_extensions(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | template <class Codec, class Options> |
| 158 | void RtpSendParametersFromMediaDescription( |
| 159 | const MediaContentDescriptionImpl<Codec>* desc, |
| 160 | RtpSendParameters<Codec, Options>* send_params) { |
| 161 | RtpParametersFromMediaDescription(desc, send_params); |
| 162 | send_params->max_bandwidth_bps = desc->bandwidth(); |
| 163 | } |
| 164 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 165 | BaseChannel::BaseChannel(rtc::Thread* thread, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 166 | MediaChannel* media_channel, |
| 167 | TransportController* transport_controller, |
| 168 | const std::string& content_name, |
| 169 | bool rtcp) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | : worker_thread_(thread), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 171 | transport_controller_(transport_controller), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | media_channel_(media_channel), |
| 173 | content_name_(content_name), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 174 | rtcp_transport_enabled_(rtcp), |
| 175 | transport_channel_(nullptr), |
| 176 | rtcp_transport_channel_(nullptr), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 177 | enabled_(false), |
| 178 | writable_(false), |
| 179 | rtp_ready_to_send_(false), |
| 180 | rtcp_ready_to_send_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 181 | was_ever_writable_(false), |
| 182 | local_content_direction_(MD_INACTIVE), |
| 183 | remote_content_direction_(MD_INACTIVE), |
| 184 | has_received_packet_(false), |
| 185 | dtls_keyed_(false), |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 186 | secure_required_(false), |
| 187 | rtp_abs_sendtime_extn_id_(-1) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 188 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 189 | LOG(LS_INFO) << "Created channel for " << content_name; |
| 190 | } |
| 191 | |
| 192 | BaseChannel::~BaseChannel() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 193 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 194 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 195 | StopConnectionMonitor(); |
| 196 | FlushRtcpMessages(); // Send any outstanding RTCP packets. |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 197 | worker_thread_->Clear(this); // eats any outstanding messages or packets |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 198 | // We must destroy the media channel before the transport channel, otherwise |
| 199 | // the media channel may try to send on the dead transport channel. NULLing |
| 200 | // is not an effective strategy since the sends will come on another thread. |
| 201 | delete media_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 202 | // Note that we don't just call set_transport_channel(nullptr) because that |
| 203 | // would call a pure virtual method which we can't do from a destructor. |
| 204 | if (transport_channel_) { |
| 205 | DisconnectFromTransportChannel(transport_channel_); |
| 206 | transport_controller_->DestroyTransportChannel_w( |
| 207 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 208 | } |
| 209 | if (rtcp_transport_channel_) { |
| 210 | DisconnectFromTransportChannel(rtcp_transport_channel_); |
| 211 | transport_controller_->DestroyTransportChannel_w( |
| 212 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 213 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 214 | LOG(LS_INFO) << "Destroyed channel"; |
| 215 | } |
| 216 | |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 217 | bool BaseChannel::Init() { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 218 | if (!SetTransport(content_name())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 222 | if (!SetDtlsSrtpCryptoSuites(transport_channel(), false)) { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 223 | return false; |
| 224 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 225 | if (rtcp_transport_enabled() && |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 226 | !SetDtlsSrtpCryptoSuites(rtcp_transport_channel(), true)) { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 227 | return false; |
| 228 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 229 | |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 230 | // Both RTP and RTCP channels are set, we can call SetInterface on |
| 231 | // media channel and it can set network options. |
| 232 | media_channel_->SetInterface(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 236 | void BaseChannel::Deinit() { |
| 237 | media_channel_->SetInterface(NULL); |
| 238 | } |
| 239 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 240 | bool BaseChannel::SetTransport(const std::string& transport_name) { |
| 241 | return worker_thread_->Invoke<bool>( |
| 242 | Bind(&BaseChannel::SetTransport_w, this, transport_name)); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 243 | } |
| 244 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 245 | bool BaseChannel::SetTransport_w(const std::string& transport_name) { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 246 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 247 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 248 | if (transport_name == transport_name_) { |
| 249 | // Nothing to do if transport name isn't changing |
| 250 | return true; |
| 251 | } |
| 252 | |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 253 | // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport |
| 254 | // changes and wait until the DTLS handshake is complete to set the newly |
| 255 | // negotiated parameters. |
| 256 | if (ShouldSetupDtlsSrtp()) { |
| 257 | srtp_filter_.ResetParams(); |
| 258 | } |
| 259 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 260 | set_transport_channel(transport_controller_->CreateTransportChannel_w( |
| 261 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 262 | if (!transport_channel()) { |
| 263 | return false; |
| 264 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 265 | if (rtcp_transport_enabled()) { |
| 266 | LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name() |
| 267 | << " on " << transport_name << " transport "; |
| 268 | set_rtcp_transport_channel(transport_controller_->CreateTransportChannel_w( |
| 269 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP)); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 270 | if (!rtcp_transport_channel()) { |
| 271 | return false; |
| 272 | } |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 273 | } |
| 274 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 275 | transport_name_ = transport_name; |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 276 | return true; |
| 277 | } |
| 278 | |
| 279 | void BaseChannel::set_transport_channel(TransportChannel* new_tc) { |
| 280 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 281 | |
| 282 | TransportChannel* old_tc = transport_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 283 | if (!old_tc && !new_tc) { |
| 284 | // Nothing to do |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 285 | return; |
| 286 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 287 | ASSERT(old_tc != new_tc); |
| 288 | |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 289 | if (old_tc) { |
| 290 | DisconnectFromTransportChannel(old_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 291 | transport_controller_->DestroyTransportChannel_w( |
| 292 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | transport_channel_ = new_tc; |
| 296 | |
| 297 | if (new_tc) { |
| 298 | ConnectToTransportChannel(new_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 299 | for (const auto& pair : socket_options_) { |
| 300 | new_tc->SetOption(pair.first, pair.second); |
| 301 | } |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 302 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 303 | |
| 304 | // Update aggregate writable/ready-to-send state between RTP and RTCP upon |
| 305 | // setting new channel |
| 306 | UpdateWritableState_w(); |
| 307 | SetReadyToSend(false, new_tc && new_tc->writable()); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) { |
| 311 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 312 | |
| 313 | TransportChannel* old_tc = rtcp_transport_channel_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 314 | if (!old_tc && !new_tc) { |
| 315 | // Nothing to do |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 316 | return; |
| 317 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 318 | ASSERT(old_tc != new_tc); |
| 319 | |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 320 | if (old_tc) { |
| 321 | DisconnectFromTransportChannel(old_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 322 | transport_controller_->DestroyTransportChannel_w( |
| 323 | transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | rtcp_transport_channel_ = new_tc; |
| 327 | |
| 328 | if (new_tc) { |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 329 | RTC_CHECK(!(ShouldSetupDtlsSrtp() && srtp_filter_.IsActive())) |
| 330 | << "Setting RTCP for DTLS/SRTP after SrtpFilter is active " |
| 331 | << "should never happen."; |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 332 | ConnectToTransportChannel(new_tc); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 333 | for (const auto& pair : rtcp_socket_options_) { |
| 334 | new_tc->SetOption(pair.first, pair.second); |
| 335 | } |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 336 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 337 | |
| 338 | // Update aggregate writable/ready-to-send state between RTP and RTCP upon |
| 339 | // setting new channel |
| 340 | UpdateWritableState_w(); |
| 341 | SetReadyToSend(true, new_tc && new_tc->writable()); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) { |
| 345 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 346 | |
| 347 | tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState); |
| 348 | tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead); |
| 349 | tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 350 | tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) { |
| 354 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 355 | |
| 356 | tc->SignalWritableState.disconnect(this); |
| 357 | tc->SignalReadPacket.disconnect(this); |
| 358 | tc->SignalReadyToSend.disconnect(this); |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 359 | tc->SignalDtlsState.disconnect(this); |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 360 | } |
| 361 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 362 | bool BaseChannel::Enable(bool enable) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 363 | worker_thread_->Invoke<void>(Bind( |
| 364 | enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w, |
| 365 | this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 366 | return true; |
| 367 | } |
| 368 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 369 | bool BaseChannel::AddRecvStream(const StreamParams& sp) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 370 | return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 373 | bool BaseChannel::RemoveRecvStream(uint32_t ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 374 | return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 375 | } |
| 376 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 377 | bool BaseChannel::AddSendStream(const StreamParams& sp) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 378 | return InvokeOnWorker( |
| 379 | Bind(&MediaChannel::AddSendStream, media_channel(), sp)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 382 | bool BaseChannel::RemoveSendStream(uint32_t ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 383 | return InvokeOnWorker( |
| 384 | Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 385 | } |
| 386 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 387 | bool BaseChannel::SetLocalContent(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 388 | ContentAction action, |
| 389 | std::string* error_desc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 390 | return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w, |
| 391 | this, content, action, error_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | bool BaseChannel::SetRemoteContent(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 395 | ContentAction action, |
| 396 | std::string* error_desc) { |
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; |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 545 | data->packet = packet->Pass(); |
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) { |
| 1010 | if (action == CA_UPDATE) { |
| 1011 | // no crypto params. |
| 1012 | return true; |
| 1013 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1014 | bool ret = false; |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1015 | bool dtls = false; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1016 | ret = CheckSrtpConfig(cryptos, &dtls, error_desc); |
| 1017 | if (!ret) { |
| 1018 | return false; |
| 1019 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1020 | switch (action) { |
| 1021 | case CA_OFFER: |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1022 | // If DTLS is already active on the channel, we could be renegotiating |
| 1023 | // here. We don't update the srtp filter. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1024 | if (!dtls) { |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1025 | ret = srtp_filter_.SetOffer(cryptos, src); |
| 1026 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1027 | break; |
| 1028 | case CA_PRANSWER: |
| 1029 | // If we're doing DTLS-SRTP, we don't want to update the filter |
| 1030 | // with an answer, because we already have SRTP parameters. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1031 | if (!dtls) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1032 | ret = srtp_filter_.SetProvisionalAnswer(cryptos, src); |
| 1033 | } |
| 1034 | break; |
| 1035 | case CA_ANSWER: |
| 1036 | // If we're doing DTLS-SRTP, we don't want to update the filter |
| 1037 | // with an answer, because we already have SRTP parameters. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1038 | if (!dtls) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1039 | ret = srtp_filter_.SetAnswer(cryptos, src); |
| 1040 | } |
| 1041 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1042 | default: |
| 1043 | break; |
| 1044 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1045 | if (!ret) { |
| 1046 | SafeSetError("Failed to setup SRTP filter.", error_desc); |
| 1047 | return false; |
| 1048 | } |
| 1049 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 1052 | void BaseChannel::ActivateRtcpMux() { |
| 1053 | worker_thread_->Invoke<void>(Bind( |
| 1054 | &BaseChannel::ActivateRtcpMux_w, this)); |
| 1055 | } |
| 1056 | |
| 1057 | void BaseChannel::ActivateRtcpMux_w() { |
| 1058 | if (!rtcp_mux_filter_.IsActive()) { |
| 1059 | rtcp_mux_filter_.SetActive(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1060 | set_rtcp_transport_channel(nullptr); |
| 1061 | rtcp_transport_enabled_ = false; |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1065 | bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1066 | ContentSource src, |
| 1067 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1068 | bool ret = false; |
| 1069 | switch (action) { |
| 1070 | case CA_OFFER: |
| 1071 | ret = rtcp_mux_filter_.SetOffer(enable, src); |
| 1072 | break; |
| 1073 | case CA_PRANSWER: |
| 1074 | ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src); |
| 1075 | break; |
| 1076 | case CA_ANSWER: |
| 1077 | ret = rtcp_mux_filter_.SetAnswer(enable, src); |
| 1078 | if (ret && rtcp_mux_filter_.IsActive()) { |
| 1079 | // We activated RTCP mux, close down the RTCP transport. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1080 | LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name() |
| 1081 | << " by destroying RTCP transport channel for " |
| 1082 | << transport_name(); |
| 1083 | set_rtcp_transport_channel(nullptr); |
| 1084 | rtcp_transport_enabled_ = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1085 | } |
| 1086 | break; |
| 1087 | case CA_UPDATE: |
| 1088 | // No RTCP mux info. |
| 1089 | ret = true; |
Henrik Kjellander | 7c027b6 | 2015-04-22 13:21:30 +0200 | [diff] [blame] | 1090 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1091 | default: |
| 1092 | break; |
| 1093 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1094 | if (!ret) { |
| 1095 | SafeSetError("Failed to setup RTCP mux filter.", error_desc); |
| 1096 | return false; |
| 1097 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1098 | // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or |
| 1099 | // CA_ANSWER, but we only want to tear down the RTCP transport channel if we |
| 1100 | // received a final answer. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1101 | if (rtcp_mux_filter_.IsActive()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1102 | // If the RTP transport is already writable, then so are we. |
| 1103 | if (transport_channel_->writable()) { |
| 1104 | ChannelWritable_w(); |
| 1105 | } |
| 1106 | } |
| 1107 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1108 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | bool BaseChannel::AddRecvStream_w(const StreamParams& sp) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1112 | ASSERT(worker_thread() == rtc::Thread::Current()); |
pbos | 482b12e | 2015-11-16 10:19:58 -0800 | [diff] [blame] | 1113 | return media_channel()->AddRecvStream(sp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1116 | bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1117 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1118 | return media_channel()->RemoveRecvStream(ssrc); |
| 1119 | } |
| 1120 | |
| 1121 | bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1122 | ContentAction action, |
| 1123 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1124 | if (!VERIFY(action == CA_OFFER || action == CA_ANSWER || |
| 1125 | action == CA_PRANSWER || action == CA_UPDATE)) |
| 1126 | return false; |
| 1127 | |
| 1128 | // If this is an update, streams only contain streams that have changed. |
| 1129 | if (action == CA_UPDATE) { |
| 1130 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1131 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1132 | const StreamParams* existing_stream = |
| 1133 | GetStreamByIds(local_streams_, it->groupid, it->id); |
| 1134 | if (!existing_stream && it->has_ssrcs()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1135 | if (media_channel()->AddSendStream(*it)) { |
| 1136 | local_streams_.push_back(*it); |
| 1137 | LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc(); |
| 1138 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1139 | std::ostringstream desc; |
| 1140 | desc << "Failed to add send stream ssrc: " << it->first_ssrc(); |
| 1141 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1142 | return false; |
| 1143 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1144 | } else if (existing_stream && !it->has_ssrcs()) { |
| 1145 | if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1146 | std::ostringstream desc; |
| 1147 | desc << "Failed to remove send stream with ssrc " |
| 1148 | << it->first_ssrc() << "."; |
| 1149 | SafeSetError(desc.str(), error_desc); |
| 1150 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1151 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1152 | RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1153 | } else { |
| 1154 | LOG(LS_WARNING) << "Ignore unsupported stream update"; |
| 1155 | } |
| 1156 | } |
| 1157 | return true; |
| 1158 | } |
| 1159 | // Else streams are all the streams we want to send. |
| 1160 | |
| 1161 | // Check for streams that have been removed. |
| 1162 | bool ret = true; |
| 1163 | for (StreamParamsVec::const_iterator it = local_streams_.begin(); |
| 1164 | it != local_streams_.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1165 | if (!GetStreamBySsrc(streams, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1166 | if (!media_channel()->RemoveSendStream(it->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1167 | std::ostringstream desc; |
| 1168 | desc << "Failed to remove send stream with ssrc " |
| 1169 | << it->first_ssrc() << "."; |
| 1170 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1171 | ret = false; |
| 1172 | } |
| 1173 | } |
| 1174 | } |
| 1175 | // Check for new streams. |
| 1176 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1177 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1178 | if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1179 | if (media_channel()->AddSendStream(*it)) { |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 1180 | LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1181 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1182 | std::ostringstream desc; |
| 1183 | desc << "Failed to add send stream ssrc: " << it->first_ssrc(); |
| 1184 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1185 | ret = false; |
| 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | local_streams_ = streams; |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
| 1193 | bool BaseChannel::UpdateRemoteStreams_w( |
| 1194 | const std::vector<StreamParams>& streams, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1195 | ContentAction action, |
| 1196 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1197 | if (!VERIFY(action == CA_OFFER || action == CA_ANSWER || |
| 1198 | action == CA_PRANSWER || action == CA_UPDATE)) |
| 1199 | return false; |
| 1200 | |
| 1201 | // If this is an update, streams only contain streams that have changed. |
| 1202 | if (action == CA_UPDATE) { |
| 1203 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1204 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1205 | const StreamParams* existing_stream = |
| 1206 | GetStreamByIds(remote_streams_, it->groupid, it->id); |
| 1207 | if (!existing_stream && it->has_ssrcs()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1208 | if (AddRecvStream_w(*it)) { |
| 1209 | remote_streams_.push_back(*it); |
| 1210 | LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc(); |
| 1211 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1212 | std::ostringstream desc; |
| 1213 | desc << "Failed to add remote stream ssrc: " << it->first_ssrc(); |
| 1214 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1215 | return false; |
| 1216 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1217 | } else if (existing_stream && !it->has_ssrcs()) { |
| 1218 | if (!RemoveRecvStream_w(existing_stream->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1219 | std::ostringstream desc; |
| 1220 | desc << "Failed to remove remote stream with ssrc " |
| 1221 | << it->first_ssrc() << "."; |
| 1222 | SafeSetError(desc.str(), error_desc); |
| 1223 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1224 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1225 | RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1226 | } else { |
| 1227 | LOG(LS_WARNING) << "Ignore unsupported stream update." |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1228 | << " Stream exists? " << (existing_stream != nullptr) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1229 | << " new stream = " << it->ToString(); |
| 1230 | } |
| 1231 | } |
| 1232 | return true; |
| 1233 | } |
| 1234 | // Else streams are all the streams we want to receive. |
| 1235 | |
| 1236 | // Check for streams that have been removed. |
| 1237 | bool ret = true; |
| 1238 | for (StreamParamsVec::const_iterator it = remote_streams_.begin(); |
| 1239 | it != remote_streams_.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1240 | if (!GetStreamBySsrc(streams, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1241 | if (!RemoveRecvStream_w(it->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1242 | std::ostringstream desc; |
| 1243 | desc << "Failed to remove remote stream with ssrc " |
| 1244 | << it->first_ssrc() << "."; |
| 1245 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1246 | ret = false; |
| 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | // Check for new streams. |
| 1251 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1252 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1253 | if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1254 | if (AddRecvStream_w(*it)) { |
| 1255 | LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0]; |
| 1256 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1257 | std::ostringstream desc; |
| 1258 | desc << "Failed to add remote stream ssrc: " << it->first_ssrc(); |
| 1259 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1260 | ret = false; |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | remote_streams_ = streams; |
| 1265 | return ret; |
| 1266 | } |
| 1267 | |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 1268 | void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension( |
| 1269 | const std::vector<RtpHeaderExtension>& extensions) { |
| 1270 | const RtpHeaderExtension* send_time_extension = |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 1271 | FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension); |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 1272 | rtp_abs_sendtime_extn_id_ = |
| 1273 | send_time_extension ? send_time_extension->id : -1; |
| 1274 | } |
| 1275 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1276 | void BaseChannel::OnMessage(rtc::Message *pmsg) { |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame^] | 1277 | TRACE_EVENT0("webrtc", "BaseChannel::OnMessage"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1278 | switch (pmsg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1279 | case MSG_RTPPACKET: |
| 1280 | case MSG_RTCPPACKET: { |
| 1281 | PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 1282 | SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, |
| 1283 | data->options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1284 | delete data; // because it is Posted |
| 1285 | break; |
| 1286 | } |
| 1287 | case MSG_FIRSTPACKETRECEIVED: { |
| 1288 | SignalFirstPacketReceived(this); |
| 1289 | break; |
| 1290 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1294 | void BaseChannel::FlushRtcpMessages() { |
| 1295 | // Flush all remaining RTCP messages. This should only be called in |
| 1296 | // destructor. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1297 | ASSERT(rtc::Thread::Current() == worker_thread_); |
| 1298 | rtc::MessageList rtcp_messages; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1299 | worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1300 | for (rtc::MessageList::iterator it = rtcp_messages.begin(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1301 | it != rtcp_messages.end(); ++it) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1302 | worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1306 | VoiceChannel::VoiceChannel(rtc::Thread* thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1307 | MediaEngineInterface* media_engine, |
| 1308 | VoiceMediaChannel* media_channel, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1309 | TransportController* transport_controller, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1310 | const std::string& content_name, |
| 1311 | bool rtcp) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1312 | : BaseChannel(thread, |
| 1313 | media_channel, |
| 1314 | transport_controller, |
| 1315 | content_name, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1316 | rtcp), |
Fredrik Solenberg | 0c02264 | 2015-08-05 12:25:22 +0200 | [diff] [blame] | 1317 | media_engine_(media_engine), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1318 | received_media_(false) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1319 | |
| 1320 | VoiceChannel::~VoiceChannel() { |
| 1321 | StopAudioMonitor(); |
| 1322 | StopMediaMonitor(); |
| 1323 | // this can't be done in the base class, since it calls a virtual |
| 1324 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1325 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | bool VoiceChannel::Init() { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 1329 | if (!BaseChannel::Init()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1330 | return false; |
| 1331 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1332 | return true; |
| 1333 | } |
| 1334 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1335 | bool VoiceChannel::SetAudioSend(uint32_t ssrc, |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 1336 | bool enable, |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1337 | const AudioOptions* options, |
| 1338 | AudioRenderer* renderer) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1339 | return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(), |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 1340 | ssrc, enable, options, renderer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1343 | // TODO(juberti): Handle early media the right way. We should get an explicit |
| 1344 | // ringing message telling us to start playing local ringback, which we cancel |
| 1345 | // if any early media actually arrives. For now, we do the opposite, which is |
| 1346 | // to wait 1 second for early media, and start playing local ringback if none |
| 1347 | // arrives. |
| 1348 | void VoiceChannel::SetEarlyMedia(bool enable) { |
| 1349 | if (enable) { |
| 1350 | // Start the early media timeout |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1351 | worker_thread()->PostDelayed(kEarlyMediaTimeout, this, |
| 1352 | MSG_EARLYMEDIATIMEOUT); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1353 | } else { |
| 1354 | // Stop the timeout if currently going. |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1355 | worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1359 | bool VoiceChannel::CanInsertDtmf() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1360 | return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf, |
| 1361 | media_channel())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1364 | bool VoiceChannel::InsertDtmf(uint32_t ssrc, |
| 1365 | int event_code, |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1366 | int duration) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1367 | return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this, |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1368 | ssrc, event_code, duration)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 1371 | bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) { |
| 1372 | return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume, |
| 1373 | media_channel(), ssrc, volume)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1374 | } |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1375 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1376 | bool VoiceChannel::GetStats(VoiceMediaInfo* stats) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1377 | return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats, |
| 1378 | media_channel(), stats)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | void VoiceChannel::StartMediaMonitor(int cms) { |
| 1382 | media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1383 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1384 | media_monitor_->SignalUpdate.connect( |
| 1385 | this, &VoiceChannel::OnMediaMonitorUpdate); |
| 1386 | media_monitor_->Start(cms); |
| 1387 | } |
| 1388 | |
| 1389 | void VoiceChannel::StopMediaMonitor() { |
| 1390 | if (media_monitor_) { |
| 1391 | media_monitor_->Stop(); |
| 1392 | media_monitor_->SignalUpdate.disconnect(this); |
| 1393 | media_monitor_.reset(); |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | void VoiceChannel::StartAudioMonitor(int cms) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1398 | audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1399 | audio_monitor_ |
| 1400 | ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate); |
| 1401 | audio_monitor_->Start(cms); |
| 1402 | } |
| 1403 | |
| 1404 | void VoiceChannel::StopAudioMonitor() { |
| 1405 | if (audio_monitor_) { |
| 1406 | audio_monitor_->Stop(); |
| 1407 | audio_monitor_.reset(); |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | bool VoiceChannel::IsAudioMonitorRunning() const { |
| 1412 | return (audio_monitor_.get() != NULL); |
| 1413 | } |
| 1414 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1415 | int VoiceChannel::GetInputLevel_w() { |
Fredrik Solenberg | 0c02264 | 2015-08-05 12:25:22 +0200 | [diff] [blame] | 1416 | return media_engine_->GetInputLevel(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | int VoiceChannel::GetOutputLevel_w() { |
| 1420 | return media_channel()->GetOutputLevel(); |
| 1421 | } |
| 1422 | |
| 1423 | void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) { |
| 1424 | media_channel()->GetActiveStreams(actives); |
| 1425 | } |
| 1426 | |
| 1427 | void VoiceChannel::OnChannelRead(TransportChannel* channel, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 1428 | const char* data, size_t len, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1429 | const rtc::PacketTime& packet_time, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 1430 | int flags) { |
| 1431 | BaseChannel::OnChannelRead(channel, data, len, packet_time, flags); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1432 | |
| 1433 | // Set a flag when we've received an RTP packet. If we're waiting for early |
| 1434 | // media, this will disable the timeout. |
| 1435 | if (!received_media_ && !PacketIsRtcp(channel, data, len)) { |
| 1436 | received_media_ = true; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | void VoiceChannel::ChangeState() { |
| 1441 | // Render incoming data if we're the active call, and we have the local |
| 1442 | // content. We receive data on the default channel and multiplexed streams. |
| 1443 | bool recv = IsReadyToReceive(); |
solenberg | 5b14b42 | 2015-10-01 04:10:31 -0700 | [diff] [blame] | 1444 | media_channel()->SetPlayout(recv); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1445 | |
| 1446 | // Send outgoing data if we're the active call, we have the remote content, |
| 1447 | // and we have had some form of connectivity. |
| 1448 | bool send = IsReadyToSend(); |
| 1449 | SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING; |
| 1450 | if (!media_channel()->SetSend(send_flag)) { |
| 1451 | LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send; |
| 1455 | } |
| 1456 | |
| 1457 | const ContentInfo* VoiceChannel::GetFirstContent( |
| 1458 | const SessionDescription* sdesc) { |
| 1459 | return GetFirstAudioContent(sdesc); |
| 1460 | } |
| 1461 | |
| 1462 | bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1463 | ContentAction action, |
| 1464 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1465 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1466 | LOG(LS_INFO) << "Setting local voice description"; |
| 1467 | |
| 1468 | const AudioContentDescription* audio = |
| 1469 | static_cast<const AudioContentDescription*>(content); |
| 1470 | ASSERT(audio != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1471 | if (!audio) { |
| 1472 | SafeSetError("Can't find audio content in local description.", error_desc); |
| 1473 | return false; |
| 1474 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1475 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1476 | if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) { |
| 1477 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1480 | AudioRecvParameters recv_params = last_recv_params_; |
| 1481 | RtpParametersFromMediaDescription(audio, &recv_params); |
| 1482 | if (!media_channel()->SetRecvParameters(recv_params)) { |
Peter Thatcher | bfab5cb | 2015-08-20 17:40:24 -0700 | [diff] [blame] | 1483 | SafeSetError("Failed to set local audio description recv parameters.", |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1484 | error_desc); |
| 1485 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1486 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1487 | for (const AudioCodec& codec : audio->codecs()) { |
| 1488 | bundle_filter()->AddPayloadType(codec.id); |
| 1489 | } |
| 1490 | last_recv_params_ = recv_params; |
| 1491 | |
| 1492 | // TODO(pthatcher): Move local streams into AudioSendParameters, and |
| 1493 | // only give it to the media channel once we have a remote |
| 1494 | // description too (without a remote description, we won't be able |
| 1495 | // to send them anyway). |
| 1496 | if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) { |
| 1497 | SafeSetError("Failed to set local audio description streams.", error_desc); |
| 1498 | return false; |
| 1499 | } |
| 1500 | |
| 1501 | set_local_content_direction(content->direction()); |
| 1502 | ChangeState(); |
| 1503 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1507 | ContentAction action, |
| 1508 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1509 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1510 | LOG(LS_INFO) << "Setting remote voice description"; |
| 1511 | |
| 1512 | const AudioContentDescription* audio = |
| 1513 | static_cast<const AudioContentDescription*>(content); |
| 1514 | ASSERT(audio != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1515 | if (!audio) { |
| 1516 | SafeSetError("Can't find audio content in remote description.", error_desc); |
| 1517 | return false; |
| 1518 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1519 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1520 | if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) { |
| 1521 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1524 | AudioSendParameters send_params = last_send_params_; |
| 1525 | RtpSendParametersFromMediaDescription(audio, &send_params); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1526 | if (audio->agc_minus_10db()) { |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 1527 | send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1528 | } |
| 1529 | if (!media_channel()->SetSendParameters(send_params)) { |
| 1530 | SafeSetError("Failed to set remote audio description send parameters.", |
| 1531 | error_desc); |
| 1532 | return false; |
| 1533 | } |
| 1534 | last_send_params_ = send_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1535 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1536 | // TODO(pthatcher): Move remote streams into AudioRecvParameters, |
| 1537 | // and only give it to the media channel once we have a local |
| 1538 | // description too (without a local description, we won't be able to |
| 1539 | // recv them anyway). |
| 1540 | if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) { |
| 1541 | SafeSetError("Failed to set remote audio description streams.", error_desc); |
| 1542 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Peter Thatcher | bfab5cb | 2015-08-20 17:40:24 -0700 | [diff] [blame] | 1545 | if (audio->rtp_header_extensions_set()) { |
| 1546 | MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions()); |
| 1547 | } |
| 1548 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1549 | set_remote_content_direction(content->direction()); |
| 1550 | ChangeState(); |
| 1551 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1554 | void VoiceChannel::HandleEarlyMediaTimeout() { |
| 1555 | // This occurs on the main thread, not the worker thread. |
| 1556 | if (!received_media_) { |
| 1557 | LOG(LS_INFO) << "No early media received before timeout"; |
| 1558 | SignalEarlyMediaTimeout(this); |
| 1559 | } |
| 1560 | } |
| 1561 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1562 | bool VoiceChannel::InsertDtmf_w(uint32_t ssrc, |
| 1563 | int event, |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1564 | int duration) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1565 | if (!enabled()) { |
| 1566 | return false; |
| 1567 | } |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1568 | return media_channel()->InsertDtmf(ssrc, event, duration); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1571 | void VoiceChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1572 | switch (pmsg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1573 | case MSG_EARLYMEDIATIMEOUT: |
| 1574 | HandleEarlyMediaTimeout(); |
| 1575 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1576 | case MSG_CHANNEL_ERROR: { |
| 1577 | VoiceChannelErrorMessageData* data = |
| 1578 | static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1579 | delete data; |
| 1580 | break; |
| 1581 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1582 | default: |
| 1583 | BaseChannel::OnMessage(pmsg); |
| 1584 | break; |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | void VoiceChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 1589 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1590 | SignalConnectionMonitor(this, infos); |
| 1591 | } |
| 1592 | |
| 1593 | void VoiceChannel::OnMediaMonitorUpdate( |
| 1594 | VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) { |
| 1595 | ASSERT(media_channel == this->media_channel()); |
| 1596 | SignalMediaMonitor(this, info); |
| 1597 | } |
| 1598 | |
| 1599 | void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor, |
| 1600 | const AudioInfo& info) { |
| 1601 | SignalAudioMonitor(this, info); |
| 1602 | } |
| 1603 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1604 | void VoiceChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const { |
| 1605 | GetSupportedAudioCryptoSuites(crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1608 | VideoChannel::VideoChannel(rtc::Thread* thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1609 | VideoMediaChannel* media_channel, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1610 | TransportController* transport_controller, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1611 | const std::string& content_name, |
Fredrik Solenberg | 7fb711f | 2015-04-22 15:30:51 +0200 | [diff] [blame] | 1612 | bool rtcp) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1613 | : BaseChannel(thread, |
| 1614 | media_channel, |
| 1615 | transport_controller, |
| 1616 | content_name, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1617 | rtcp), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1618 | renderer_(NULL), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1619 | previous_we_(rtc::WE_CLOSE) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1620 | |
| 1621 | bool VideoChannel::Init() { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 1622 | if (!BaseChannel::Init()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1623 | return false; |
| 1624 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1625 | return true; |
| 1626 | } |
| 1627 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1628 | VideoChannel::~VideoChannel() { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1629 | std::vector<uint32_t> screencast_ssrcs; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1630 | ScreencastMap::iterator iter; |
| 1631 | while (!screencast_capturers_.empty()) { |
| 1632 | if (!RemoveScreencast(screencast_capturers_.begin()->first)) { |
| 1633 | LOG(LS_ERROR) << "Unable to delete screencast with ssrc " |
| 1634 | << screencast_capturers_.begin()->first; |
| 1635 | ASSERT(false); |
| 1636 | break; |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | StopMediaMonitor(); |
| 1641 | // this can't be done in the base class, since it calls a virtual |
| 1642 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1643 | |
| 1644 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1647 | bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1648 | worker_thread()->Invoke<void>(Bind( |
| 1649 | &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1650 | return true; |
| 1651 | } |
| 1652 | |
| 1653 | bool VideoChannel::ApplyViewRequest(const ViewRequest& request) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1654 | return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request)); |
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::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) { |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 1658 | return worker_thread()->Invoke<bool>(Bind( |
| 1659 | &VideoChannel::AddScreencast_w, this, ssrc, capturer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1662 | bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1663 | return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer, |
| 1664 | media_channel(), ssrc, capturer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1667 | bool VideoChannel::RemoveScreencast(uint32_t ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1668 | return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | bool VideoChannel::IsScreencasting() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1672 | return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1675 | int VideoChannel::GetScreencastFps(uint32_t ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1676 | ScreencastDetailsData data(ssrc); |
| 1677 | worker_thread()->Invoke<void>(Bind( |
| 1678 | &VideoChannel::GetScreencastDetails_w, this, &data)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1679 | return data.fps; |
| 1680 | } |
| 1681 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1682 | int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1683 | ScreencastDetailsData data(ssrc); |
| 1684 | worker_thread()->Invoke<void>(Bind( |
| 1685 | &VideoChannel::GetScreencastDetails_w, this, &data)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1686 | return data.screencast_max_pixels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | bool VideoChannel::SendIntraFrame() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1690 | worker_thread()->Invoke<void>(Bind( |
| 1691 | &VideoMediaChannel::SendIntraFrame, media_channel())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1692 | return true; |
| 1693 | } |
| 1694 | |
| 1695 | bool VideoChannel::RequestIntraFrame() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1696 | worker_thread()->Invoke<void>(Bind( |
| 1697 | &VideoMediaChannel::RequestIntraFrame, media_channel())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1698 | return true; |
| 1699 | } |
| 1700 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1701 | bool VideoChannel::SetVideoSend(uint32_t ssrc, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1702 | bool mute, |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1703 | const VideoOptions* options) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1704 | return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(), |
| 1705 | ssrc, mute, options)); |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1708 | void VideoChannel::ChangeState() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1709 | // Send outgoing data if we're the active call, we have the remote content, |
| 1710 | // and we have had some form of connectivity. |
| 1711 | bool send = IsReadyToSend(); |
| 1712 | if (!media_channel()->SetSend(send)) { |
| 1713 | LOG(LS_ERROR) << "Failed to SetSend on video channel"; |
| 1714 | // TODO(gangji): Report error back to server. |
| 1715 | } |
| 1716 | |
Peter Boström | 34fbfff | 2015-09-24 19:20:30 +0200 | [diff] [blame] | 1717 | LOG(LS_INFO) << "Changing video state, send=" << send; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
pbos@webrtc.org | 058b1f1 | 2015-03-04 08:54:32 +0000 | [diff] [blame] | 1720 | bool VideoChannel::GetStats(VideoMediaInfo* stats) { |
| 1721 | return InvokeOnWorker( |
| 1722 | Bind(&VideoMediaChannel::GetStats, media_channel(), stats)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | void VideoChannel::StartMediaMonitor(int cms) { |
| 1726 | media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1727 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1728 | media_monitor_->SignalUpdate.connect( |
| 1729 | this, &VideoChannel::OnMediaMonitorUpdate); |
| 1730 | media_monitor_->Start(cms); |
| 1731 | } |
| 1732 | |
| 1733 | void VideoChannel::StopMediaMonitor() { |
| 1734 | if (media_monitor_) { |
| 1735 | media_monitor_->Stop(); |
| 1736 | media_monitor_.reset(); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | const ContentInfo* VideoChannel::GetFirstContent( |
| 1741 | const SessionDescription* sdesc) { |
| 1742 | return GetFirstVideoContent(sdesc); |
| 1743 | } |
| 1744 | |
| 1745 | bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1746 | ContentAction action, |
| 1747 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1748 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1749 | LOG(LS_INFO) << "Setting local video description"; |
| 1750 | |
| 1751 | const VideoContentDescription* video = |
| 1752 | static_cast<const VideoContentDescription*>(content); |
| 1753 | ASSERT(video != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1754 | if (!video) { |
| 1755 | SafeSetError("Can't find video content in local description.", error_desc); |
| 1756 | return false; |
| 1757 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1758 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1759 | if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) { |
| 1760 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1763 | VideoRecvParameters recv_params = last_recv_params_; |
| 1764 | RtpParametersFromMediaDescription(video, &recv_params); |
| 1765 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 1766 | SafeSetError("Failed to set local video description recv parameters.", |
| 1767 | error_desc); |
| 1768 | return false; |
| 1769 | } |
| 1770 | for (const VideoCodec& codec : video->codecs()) { |
| 1771 | bundle_filter()->AddPayloadType(codec.id); |
| 1772 | } |
| 1773 | last_recv_params_ = recv_params; |
| 1774 | |
| 1775 | // TODO(pthatcher): Move local streams into VideoSendParameters, and |
| 1776 | // only give it to the media channel once we have a remote |
| 1777 | // description too (without a remote description, we won't be able |
| 1778 | // to send them anyway). |
| 1779 | if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) { |
| 1780 | SafeSetError("Failed to set local video description streams.", error_desc); |
| 1781 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1784 | set_local_content_direction(content->direction()); |
| 1785 | ChangeState(); |
| 1786 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1790 | ContentAction action, |
| 1791 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1792 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1793 | LOG(LS_INFO) << "Setting remote video description"; |
| 1794 | |
| 1795 | const VideoContentDescription* video = |
| 1796 | static_cast<const VideoContentDescription*>(content); |
| 1797 | ASSERT(video != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1798 | if (!video) { |
| 1799 | SafeSetError("Can't find video content in remote description.", error_desc); |
| 1800 | return false; |
| 1801 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1802 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1803 | |
| 1804 | if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) { |
| 1805 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1808 | VideoSendParameters send_params = last_send_params_; |
| 1809 | RtpSendParametersFromMediaDescription(video, &send_params); |
| 1810 | if (video->conference_mode()) { |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 1811 | send_params.options.conference_mode = rtc::Optional<bool>(true); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1812 | } |
| 1813 | if (!media_channel()->SetSendParameters(send_params)) { |
| 1814 | SafeSetError("Failed to set remote video description send parameters.", |
| 1815 | error_desc); |
| 1816 | return false; |
| 1817 | } |
| 1818 | last_send_params_ = send_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1819 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1820 | // TODO(pthatcher): Move remote streams into VideoRecvParameters, |
| 1821 | // and only give it to the media channel once we have a local |
| 1822 | // description too (without a local description, we won't be able to |
| 1823 | // recv them anyway). |
| 1824 | if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) { |
| 1825 | SafeSetError("Failed to set remote video description streams.", error_desc); |
| 1826 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1829 | if (video->rtp_header_extensions_set()) { |
| 1830 | MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1831 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1832 | |
| 1833 | set_remote_content_direction(content->direction()); |
| 1834 | ChangeState(); |
| 1835 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) { |
| 1839 | bool ret = true; |
| 1840 | // Set the send format for each of the local streams. If the view request |
| 1841 | // does not contain a local stream, set its send format to 0x0, which will |
| 1842 | // drop all frames. |
| 1843 | for (std::vector<StreamParams>::const_iterator it = local_streams().begin(); |
| 1844 | it != local_streams().end(); ++it) { |
| 1845 | VideoFormat format(0, 0, 0, cricket::FOURCC_I420); |
| 1846 | StaticVideoViews::const_iterator view; |
| 1847 | for (view = request.static_video_views.begin(); |
| 1848 | view != request.static_video_views.end(); ++view) { |
| 1849 | if (view->selector.Matches(*it)) { |
| 1850 | format.width = view->width; |
| 1851 | format.height = view->height; |
| 1852 | format.interval = cricket::VideoFormat::FpsToInterval(view->framerate); |
| 1853 | break; |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format); |
| 1858 | } |
| 1859 | |
| 1860 | // Check if the view request has invalid streams. |
| 1861 | for (StaticVideoViews::const_iterator it = request.static_video_views.begin(); |
| 1862 | it != request.static_video_views.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1863 | if (!GetStream(local_streams(), it->selector)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1864 | LOG(LS_WARNING) << "View request for (" |
| 1865 | << it->selector.ssrc << ", '" |
| 1866 | << it->selector.groupid << "', '" |
| 1867 | << it->selector.streamid << "'" |
| 1868 | << ") is not in the local streams."; |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | return ret; |
| 1873 | } |
| 1874 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1875 | bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1876 | if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) { |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 1877 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1878 | } |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 1879 | capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange); |
| 1880 | screencast_capturers_[ssrc] = capturer; |
| 1881 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1884 | bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1885 | ScreencastMap::iterator iter = screencast_capturers_.find(ssrc); |
| 1886 | if (iter == screencast_capturers_.end()) { |
| 1887 | return false; |
| 1888 | } |
| 1889 | // Clean up VideoCapturer. |
| 1890 | delete iter->second; |
| 1891 | screencast_capturers_.erase(iter); |
| 1892 | return true; |
| 1893 | } |
| 1894 | |
| 1895 | bool VideoChannel::IsScreencasting_w() const { |
| 1896 | return !screencast_capturers_.empty(); |
| 1897 | } |
| 1898 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1899 | void VideoChannel::GetScreencastDetails_w( |
| 1900 | ScreencastDetailsData* data) const { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1901 | ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1902 | if (iter == screencast_capturers_.end()) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1903 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1904 | } |
| 1905 | VideoCapturer* capturer = iter->second; |
| 1906 | const VideoFormat* video_format = capturer->GetCaptureFormat(); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1907 | data->fps = VideoFormat::IntervalToFps(video_format->interval); |
| 1908 | data->screencast_max_pixels = capturer->screencast_max_pixels(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1911 | void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1912 | rtc::WindowEvent we) { |
| 1913 | ASSERT(signaling_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1914 | SignalScreencastWindowEvent(ssrc, we); |
| 1915 | } |
| 1916 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1917 | void VideoChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1918 | switch (pmsg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1919 | case MSG_SCREENCASTWINDOWEVENT: { |
| 1920 | const ScreencastEventMessageData* data = |
| 1921 | static_cast<ScreencastEventMessageData*>(pmsg->pdata); |
| 1922 | OnScreencastWindowEvent_s(data->ssrc, data->event); |
| 1923 | delete data; |
| 1924 | break; |
| 1925 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1926 | case MSG_CHANNEL_ERROR: { |
| 1927 | const VideoChannelErrorMessageData* data = |
| 1928 | static_cast<VideoChannelErrorMessageData*>(pmsg->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1929 | delete data; |
| 1930 | break; |
| 1931 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1932 | default: |
| 1933 | BaseChannel::OnMessage(pmsg); |
| 1934 | break; |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | void VideoChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 1939 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1940 | SignalConnectionMonitor(this, infos); |
| 1941 | } |
| 1942 | |
| 1943 | // TODO(pthatcher): Look into removing duplicate code between |
| 1944 | // audio, video, and data, perhaps by using templates. |
| 1945 | void VideoChannel::OnMediaMonitorUpdate( |
| 1946 | VideoMediaChannel* media_channel, const VideoMediaInfo &info) { |
| 1947 | ASSERT(media_channel == this->media_channel()); |
| 1948 | SignalMediaMonitor(this, info); |
| 1949 | } |
| 1950 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1951 | void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1952 | rtc::WindowEvent event) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1953 | ScreencastEventMessageData* pdata = |
| 1954 | new ScreencastEventMessageData(ssrc, event); |
| 1955 | signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata); |
| 1956 | } |
| 1957 | |
| 1958 | void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) { |
| 1959 | // Map capturer events to window events. In the future we may want to simply |
| 1960 | // pass these events up directly. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1961 | rtc::WindowEvent we; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1962 | if (ev == CS_STOPPED) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1963 | we = rtc::WE_CLOSE; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1964 | } else if (ev == CS_PAUSED) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1965 | we = rtc::WE_MINIMIZE; |
| 1966 | } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) { |
| 1967 | we = rtc::WE_RESTORE; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1968 | } else { |
| 1969 | return; |
| 1970 | } |
| 1971 | previous_we_ = we; |
| 1972 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1973 | uint32_t ssrc = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1974 | if (!GetLocalSsrc(capturer, &ssrc)) { |
| 1975 | return; |
| 1976 | } |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1977 | |
| 1978 | OnScreencastWindowEvent(ssrc, we); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1981 | bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1982 | *ssrc = 0; |
| 1983 | for (ScreencastMap::iterator iter = screencast_capturers_.begin(); |
| 1984 | iter != screencast_capturers_.end(); ++iter) { |
| 1985 | if (iter->second == capturer) { |
| 1986 | *ssrc = iter->first; |
| 1987 | return true; |
| 1988 | } |
| 1989 | } |
| 1990 | return false; |
| 1991 | } |
| 1992 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1993 | void VideoChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const { |
| 1994 | GetSupportedVideoCryptoSuites(crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1997 | DataChannel::DataChannel(rtc::Thread* thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1998 | DataMediaChannel* media_channel, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1999 | TransportController* transport_controller, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2000 | const std::string& content_name, |
| 2001 | bool rtcp) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2002 | : BaseChannel(thread, |
| 2003 | media_channel, |
| 2004 | transport_controller, |
| 2005 | content_name, |
| 2006 | rtcp), |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 2007 | data_channel_type_(cricket::DCT_NONE), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2008 | ready_to_send_data_(false) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2009 | |
| 2010 | DataChannel::~DataChannel() { |
| 2011 | StopMediaMonitor(); |
| 2012 | // this can't be done in the base class, since it calls a virtual |
| 2013 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 2014 | |
| 2015 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | bool DataChannel::Init() { |
pthatcher@webrtc.org | 6ad507a | 2015-03-16 20:19:12 +0000 | [diff] [blame] | 2019 | if (!BaseChannel::Init()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2020 | return false; |
| 2021 | } |
| 2022 | media_channel()->SignalDataReceived.connect( |
| 2023 | this, &DataChannel::OnDataReceived); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2024 | media_channel()->SignalReadyToSend.connect( |
| 2025 | this, &DataChannel::OnDataChannelReadyToSend); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2026 | media_channel()->SignalStreamClosedRemotely.connect( |
| 2027 | this, &DataChannel::OnStreamClosedRemotely); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2028 | return true; |
| 2029 | } |
| 2030 | |
| 2031 | bool DataChannel::SendData(const SendDataParams& params, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2032 | const rtc::Buffer& payload, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2033 | SendDataResult* result) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 2034 | return InvokeOnWorker(Bind(&DataMediaChannel::SendData, |
| 2035 | media_channel(), params, payload, result)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | const ContentInfo* DataChannel::GetFirstContent( |
| 2039 | const SessionDescription* sdesc) { |
| 2040 | return GetFirstDataContent(sdesc); |
| 2041 | } |
| 2042 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2043 | bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2044 | if (data_channel_type_ == DCT_SCTP) { |
| 2045 | // TODO(pthatcher): Do this in a more robust way by checking for |
| 2046 | // SCTP or DTLS. |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 2047 | return !IsRtpPacket(packet->data(), packet->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2048 | } else if (data_channel_type_ == DCT_RTP) { |
| 2049 | return BaseChannel::WantsPacket(rtcp, packet); |
| 2050 | } |
| 2051 | return false; |
| 2052 | } |
| 2053 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2054 | bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type, |
| 2055 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2056 | // It hasn't been set before, so set it now. |
| 2057 | if (data_channel_type_ == DCT_NONE) { |
| 2058 | data_channel_type_ = new_data_channel_type; |
| 2059 | return true; |
| 2060 | } |
| 2061 | |
| 2062 | // It's been set before, but doesn't match. That's bad. |
| 2063 | if (data_channel_type_ != new_data_channel_type) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2064 | std::ostringstream desc; |
| 2065 | desc << "Data channel type mismatch." |
| 2066 | << " Expected " << data_channel_type_ |
| 2067 | << " Got " << new_data_channel_type; |
| 2068 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2069 | return false; |
| 2070 | } |
| 2071 | |
| 2072 | // It's hasn't changed. Nothing to do. |
| 2073 | return true; |
| 2074 | } |
| 2075 | |
| 2076 | bool DataChannel::SetDataChannelTypeFromContent( |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2077 | const DataContentDescription* content, |
| 2078 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2079 | bool is_sctp = ((content->protocol() == kMediaProtocolSctp) || |
| 2080 | (content->protocol() == kMediaProtocolDtlsSctp)); |
| 2081 | DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2082 | return SetDataChannelType(data_channel_type, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | bool DataChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2086 | ContentAction action, |
| 2087 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2088 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2089 | LOG(LS_INFO) << "Setting local data description"; |
| 2090 | |
| 2091 | const DataContentDescription* data = |
| 2092 | static_cast<const DataContentDescription*>(content); |
| 2093 | ASSERT(data != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2094 | if (!data) { |
| 2095 | SafeSetError("Can't find data content in local description.", error_desc); |
| 2096 | return false; |
| 2097 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2098 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2099 | if (!SetDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2100 | return false; |
| 2101 | } |
| 2102 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2103 | if (data_channel_type_ == DCT_RTP) { |
| 2104 | if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) { |
| 2105 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2109 | // FYI: We send the SCTP port number (not to be confused with the |
| 2110 | // underlying UDP port number) as a codec parameter. So even SCTP |
| 2111 | // data channels need codecs. |
| 2112 | DataRecvParameters recv_params = last_recv_params_; |
| 2113 | RtpParametersFromMediaDescription(data, &recv_params); |
| 2114 | if (!media_channel()->SetRecvParameters(recv_params)) { |
| 2115 | SafeSetError("Failed to set remote data description recv parameters.", |
| 2116 | error_desc); |
| 2117 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2118 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2119 | if (data_channel_type_ == DCT_RTP) { |
| 2120 | for (const DataCodec& codec : data->codecs()) { |
| 2121 | bundle_filter()->AddPayloadType(codec.id); |
| 2122 | } |
| 2123 | } |
| 2124 | last_recv_params_ = recv_params; |
| 2125 | |
| 2126 | // TODO(pthatcher): Move local streams into DataSendParameters, and |
| 2127 | // only give it to the media channel once we have a remote |
| 2128 | // description too (without a remote description, we won't be able |
| 2129 | // to send them anyway). |
| 2130 | if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) { |
| 2131 | SafeSetError("Failed to set local data description streams.", error_desc); |
| 2132 | return false; |
| 2133 | } |
| 2134 | |
| 2135 | set_local_content_direction(content->direction()); |
| 2136 | ChangeState(); |
| 2137 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2141 | ContentAction action, |
| 2142 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2143 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2144 | |
| 2145 | const DataContentDescription* data = |
| 2146 | static_cast<const DataContentDescription*>(content); |
| 2147 | ASSERT(data != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2148 | if (!data) { |
| 2149 | SafeSetError("Can't find data content in remote description.", error_desc); |
| 2150 | return false; |
| 2151 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2152 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2153 | // If the remote data doesn't have codecs and isn't an update, it |
| 2154 | // must be empty, so ignore it. |
| 2155 | if (!data->has_codecs() && action != CA_UPDATE) { |
| 2156 | return true; |
| 2157 | } |
| 2158 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2159 | if (!SetDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2160 | return false; |
| 2161 | } |
| 2162 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2163 | LOG(LS_INFO) << "Setting remote data description"; |
| 2164 | if (data_channel_type_ == DCT_RTP && |
| 2165 | !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) { |
| 2166 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2169 | |
| 2170 | DataSendParameters send_params = last_send_params_; |
| 2171 | RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params); |
| 2172 | if (!media_channel()->SetSendParameters(send_params)) { |
| 2173 | SafeSetError("Failed to set remote data description send parameters.", |
| 2174 | error_desc); |
| 2175 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2176 | } |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 2177 | last_send_params_ = send_params; |
| 2178 | |
| 2179 | // TODO(pthatcher): Move remote streams into DataRecvParameters, |
| 2180 | // and only give it to the media channel once we have a local |
| 2181 | // description too (without a local description, we won't be able to |
| 2182 | // recv them anyway). |
| 2183 | if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) { |
| 2184 | SafeSetError("Failed to set remote data description streams.", |
| 2185 | error_desc); |
| 2186 | return false; |
| 2187 | } |
| 2188 | |
| 2189 | set_remote_content_direction(content->direction()); |
| 2190 | ChangeState(); |
| 2191 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | void DataChannel::ChangeState() { |
| 2195 | // Render incoming data if we're the active call, and we have the local |
| 2196 | // content. We receive data on the default channel and multiplexed streams. |
| 2197 | bool recv = IsReadyToReceive(); |
| 2198 | if (!media_channel()->SetReceive(recv)) { |
| 2199 | LOG(LS_ERROR) << "Failed to SetReceive on data channel"; |
| 2200 | } |
| 2201 | |
| 2202 | // Send outgoing data if we're the active call, we have the remote content, |
| 2203 | // and we have had some form of connectivity. |
| 2204 | bool send = IsReadyToSend(); |
| 2205 | if (!media_channel()->SetSend(send)) { |
| 2206 | LOG(LS_ERROR) << "Failed to SetSend on data channel"; |
| 2207 | } |
| 2208 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 2209 | // Trigger SignalReadyToSendData asynchronously. |
| 2210 | OnDataChannelReadyToSend(send); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2211 | |
| 2212 | LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send; |
| 2213 | } |
| 2214 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2215 | void DataChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2216 | switch (pmsg->message_id) { |
| 2217 | case MSG_READYTOSENDDATA: { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2218 | DataChannelReadyToSendMessageData* data = |
| 2219 | static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 2220 | ready_to_send_data_ = data->data(); |
| 2221 | SignalReadyToSendData(ready_to_send_data_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2222 | delete data; |
| 2223 | break; |
| 2224 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2225 | case MSG_DATARECEIVED: { |
| 2226 | DataReceivedMessageData* data = |
| 2227 | static_cast<DataReceivedMessageData*>(pmsg->pdata); |
| 2228 | SignalDataReceived(this, data->params, data->payload); |
| 2229 | delete data; |
| 2230 | break; |
| 2231 | } |
| 2232 | case MSG_CHANNEL_ERROR: { |
| 2233 | const DataChannelErrorMessageData* data = |
| 2234 | static_cast<DataChannelErrorMessageData*>(pmsg->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2235 | delete data; |
| 2236 | break; |
| 2237 | } |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2238 | case MSG_STREAMCLOSEDREMOTELY: { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2239 | rtc::TypedMessageData<uint32_t>* data = |
| 2240 | static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2241 | SignalStreamClosedRemotely(data->data()); |
| 2242 | delete data; |
| 2243 | break; |
| 2244 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2245 | default: |
| 2246 | BaseChannel::OnMessage(pmsg); |
| 2247 | break; |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | void DataChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame] | 2252 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2253 | SignalConnectionMonitor(this, infos); |
| 2254 | } |
| 2255 | |
| 2256 | void DataChannel::StartMediaMonitor(int cms) { |
| 2257 | media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2258 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2259 | media_monitor_->SignalUpdate.connect( |
| 2260 | this, &DataChannel::OnMediaMonitorUpdate); |
| 2261 | media_monitor_->Start(cms); |
| 2262 | } |
| 2263 | |
| 2264 | void DataChannel::StopMediaMonitor() { |
| 2265 | if (media_monitor_) { |
| 2266 | media_monitor_->Stop(); |
| 2267 | media_monitor_->SignalUpdate.disconnect(this); |
| 2268 | media_monitor_.reset(); |
| 2269 | } |
| 2270 | } |
| 2271 | |
| 2272 | void DataChannel::OnMediaMonitorUpdate( |
| 2273 | DataMediaChannel* media_channel, const DataMediaInfo& info) { |
| 2274 | ASSERT(media_channel == this->media_channel()); |
| 2275 | SignalMediaMonitor(this, info); |
| 2276 | } |
| 2277 | |
| 2278 | void DataChannel::OnDataReceived( |
| 2279 | const ReceiveDataParams& params, const char* data, size_t len) { |
| 2280 | DataReceivedMessageData* msg = new DataReceivedMessageData( |
| 2281 | params, data, len); |
| 2282 | signaling_thread()->Post(this, MSG_DATARECEIVED, msg); |
| 2283 | } |
| 2284 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2285 | void DataChannel::OnDataChannelError(uint32_t ssrc, |
| 2286 | DataMediaChannel::Error err) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2287 | DataChannelErrorMessageData* data = new DataChannelErrorMessageData( |
| 2288 | ssrc, err); |
| 2289 | signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data); |
| 2290 | } |
| 2291 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2292 | void DataChannel::OnDataChannelReadyToSend(bool writable) { |
| 2293 | // This is usded for congestion control to indicate that the stream is ready |
| 2294 | // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates |
| 2295 | // that the transport channel is ready. |
| 2296 | signaling_thread()->Post(this, MSG_READYTOSENDDATA, |
| 2297 | new DataChannelReadyToSendMessageData(writable)); |
| 2298 | } |
| 2299 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 2300 | void DataChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const { |
| 2301 | GetSupportedDataCryptoSuites(crypto_suites); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | bool DataChannel::ShouldSetupDtlsSrtp() const { |
Guo-wei Shieh | 1218d7a | 2015-12-05 09:59:56 -0800 | [diff] [blame] | 2305 | return (data_channel_type_ == DCT_RTP) && BaseChannel::ShouldSetupDtlsSrtp(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2308 | void DataChannel::OnStreamClosedRemotely(uint32_t sid) { |
| 2309 | rtc::TypedMessageData<uint32_t>* message = |
| 2310 | new rtc::TypedMessageData<uint32_t>(sid); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2311 | signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message); |
| 2312 | } |
| 2313 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2314 | } // namespace cricket |