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