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