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)) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 725 | const std::string error_desc = |
| 726 | "Couldn't set up DTLS-SRTP on RTP channel."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 727 | // Sent synchronously. |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 728 | signaling_thread()->Invoke<void>(Bind( |
| 729 | &SetSessionError, |
| 730 | session_, |
| 731 | BaseSession::ERROR_TRANSPORT, |
| 732 | error_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 733 | return; |
| 734 | } |
| 735 | |
| 736 | if (rtcp_transport_channel_) { |
| 737 | if (!SetupDtlsSrtp(true)) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 738 | const std::string error_desc = |
| 739 | "Couldn't set up DTLS-SRTP on RTCP channel"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 740 | // Sent synchronously. |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 741 | signaling_thread()->Invoke<void>(Bind( |
| 742 | &SetSessionError, |
| 743 | session_, |
| 744 | BaseSession::ERROR_TRANSPORT, |
| 745 | error_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 746 | return; |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | was_ever_writable_ = true; |
| 752 | writable_ = true; |
| 753 | ChangeState(); |
| 754 | } |
| 755 | |
| 756 | bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) { |
| 757 | std::vector<std::string> ciphers; |
| 758 | // We always use the default SRTP ciphers for RTCP, but we may use different |
| 759 | // ciphers for RTP depending on the media type. |
| 760 | if (!rtcp) { |
| 761 | GetSrtpCiphers(&ciphers); |
| 762 | } else { |
| 763 | GetSupportedDefaultCryptoSuites(&ciphers); |
| 764 | } |
| 765 | return tc->SetSrtpCiphers(ciphers); |
| 766 | } |
| 767 | |
| 768 | bool BaseChannel::ShouldSetupDtlsSrtp() const { |
| 769 | return true; |
| 770 | } |
| 771 | |
| 772 | // This function returns true if either DTLS-SRTP is not in use |
| 773 | // *or* DTLS-SRTP is successfully set up. |
| 774 | bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) { |
| 775 | bool ret = false; |
| 776 | |
| 777 | TransportChannel *channel = rtcp_channel ? |
| 778 | rtcp_transport_channel_ : transport_channel_; |
| 779 | |
| 780 | // No DTLS |
| 781 | if (!channel->IsDtlsActive()) |
| 782 | return true; |
| 783 | |
| 784 | std::string selected_cipher; |
| 785 | |
| 786 | if (!channel->GetSrtpCipher(&selected_cipher)) { |
| 787 | LOG(LS_ERROR) << "No DTLS-SRTP selected cipher"; |
| 788 | return false; |
| 789 | } |
| 790 | |
| 791 | LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " |
| 792 | << content_name() << " " |
| 793 | << PacketType(rtcp_channel); |
| 794 | |
| 795 | // OK, we're now doing DTLS (RFC 5764) |
| 796 | std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 + |
| 797 | SRTP_MASTER_KEY_SALT_LEN * 2); |
| 798 | |
| 799 | // RFC 5705 exporter using the RFC 5764 parameters |
| 800 | if (!channel->ExportKeyingMaterial( |
| 801 | kDtlsSrtpExporterLabel, |
| 802 | NULL, 0, false, |
| 803 | &dtls_buffer[0], dtls_buffer.size())) { |
| 804 | LOG(LS_WARNING) << "DTLS-SRTP key export failed"; |
| 805 | ASSERT(false); // This should never happen |
| 806 | return false; |
| 807 | } |
| 808 | |
| 809 | // Sync up the keys with the DTLS-SRTP interface |
| 810 | std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN + |
| 811 | SRTP_MASTER_KEY_SALT_LEN); |
| 812 | std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN + |
| 813 | SRTP_MASTER_KEY_SALT_LEN); |
| 814 | size_t offset = 0; |
| 815 | memcpy(&client_write_key[0], &dtls_buffer[offset], |
| 816 | SRTP_MASTER_KEY_KEY_LEN); |
| 817 | offset += SRTP_MASTER_KEY_KEY_LEN; |
| 818 | memcpy(&server_write_key[0], &dtls_buffer[offset], |
| 819 | SRTP_MASTER_KEY_KEY_LEN); |
| 820 | offset += SRTP_MASTER_KEY_KEY_LEN; |
| 821 | memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN], |
| 822 | &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN); |
| 823 | offset += SRTP_MASTER_KEY_SALT_LEN; |
| 824 | memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN], |
| 825 | &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN); |
| 826 | |
| 827 | std::vector<unsigned char> *send_key, *recv_key; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 828 | rtc::SSLRole role; |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 829 | if (!channel->GetSslRole(&role)) { |
| 830 | LOG(LS_WARNING) << "GetSslRole failed"; |
| 831 | return false; |
| 832 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 833 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 834 | if (role == rtc::SSL_SERVER) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 835 | send_key = &server_write_key; |
| 836 | recv_key = &client_write_key; |
| 837 | } else { |
| 838 | send_key = &client_write_key; |
| 839 | recv_key = &server_write_key; |
| 840 | } |
| 841 | |
| 842 | if (rtcp_channel) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 843 | ret = srtp_filter_.SetRtcpParams( |
| 844 | selected_cipher, |
| 845 | &(*send_key)[0], |
| 846 | static_cast<int>(send_key->size()), |
| 847 | selected_cipher, |
| 848 | &(*recv_key)[0], |
| 849 | static_cast<int>(recv_key->size())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 850 | } else { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 851 | ret = srtp_filter_.SetRtpParams( |
| 852 | selected_cipher, |
| 853 | &(*send_key)[0], |
| 854 | static_cast<int>(send_key->size()), |
| 855 | selected_cipher, |
| 856 | &(*recv_key)[0], |
| 857 | static_cast<int>(recv_key->size())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | if (!ret) |
| 861 | LOG(LS_WARNING) << "DTLS-SRTP key installation failed"; |
| 862 | else |
| 863 | dtls_keyed_ = true; |
| 864 | |
| 865 | return ret; |
| 866 | } |
| 867 | |
| 868 | void BaseChannel::ChannelNotWritable_w() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 869 | ASSERT(worker_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 870 | if (!writable_) |
| 871 | return; |
| 872 | |
| 873 | LOG(LS_INFO) << "Channel socket not writable (" |
| 874 | << transport_channel_->content_name() << ", " |
| 875 | << transport_channel_->component() << ")"; |
| 876 | writable_ = false; |
| 877 | ChangeState(); |
| 878 | } |
| 879 | |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 880 | // |dtls| will be set to true if DTLS is active for transport channel and |
| 881 | // crypto is empty. |
| 882 | bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 883 | bool* dtls, |
| 884 | std::string* error_desc) { |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 885 | *dtls = transport_channel_->IsDtlsActive(); |
| 886 | if (*dtls && !cryptos.empty()) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 887 | SafeSetError("Cryptos must be empty when DTLS is active.", |
| 888 | error_desc); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 889 | return false; |
| 890 | } |
| 891 | return true; |
| 892 | } |
| 893 | |
buildbot@webrtc.org | 75ce920 | 2014-06-20 12:30:24 +0000 | [diff] [blame] | 894 | bool BaseChannel::SetRecvRtpHeaderExtensions_w( |
| 895 | const MediaContentDescription* content, |
| 896 | MediaChannel* media_channel, |
| 897 | std::string* error_desc) { |
| 898 | if (content->rtp_header_extensions_set()) { |
| 899 | if (!media_channel->SetRecvRtpHeaderExtensions( |
| 900 | content->rtp_header_extensions())) { |
| 901 | std::ostringstream desc; |
| 902 | desc << "Failed to set receive rtp header extensions for " |
| 903 | << MediaTypeToString(content->type()) << " content."; |
| 904 | SafeSetError(desc.str(), error_desc); |
| 905 | return false; |
| 906 | } |
| 907 | } |
| 908 | return true; |
| 909 | } |
| 910 | |
| 911 | bool BaseChannel::SetSendRtpHeaderExtensions_w( |
| 912 | const MediaContentDescription* content, |
| 913 | MediaChannel* media_channel, |
| 914 | std::string* error_desc) { |
| 915 | if (content->rtp_header_extensions_set()) { |
| 916 | if (!media_channel->SetSendRtpHeaderExtensions( |
| 917 | content->rtp_header_extensions())) { |
| 918 | std::ostringstream desc; |
| 919 | desc << "Failed to set send rtp header extensions for " |
| 920 | << MediaTypeToString(content->type()) << " content."; |
| 921 | SafeSetError(desc.str(), error_desc); |
| 922 | return false; |
| 923 | } else { |
| 924 | MaybeCacheRtpAbsSendTimeHeaderExtension(content->rtp_header_extensions()); |
| 925 | } |
| 926 | } |
| 927 | return true; |
| 928 | } |
| 929 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 930 | bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 931 | ContentAction action, |
| 932 | ContentSource src, |
| 933 | std::string* error_desc) { |
| 934 | if (action == CA_UPDATE) { |
| 935 | // no crypto params. |
| 936 | return true; |
| 937 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 938 | bool ret = false; |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 939 | bool dtls = false; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 940 | ret = CheckSrtpConfig(cryptos, &dtls, error_desc); |
| 941 | if (!ret) { |
| 942 | return false; |
| 943 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 944 | switch (action) { |
| 945 | case CA_OFFER: |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 946 | // If DTLS is already active on the channel, we could be renegotiating |
| 947 | // here. We don't update the srtp filter. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 948 | if (!dtls) { |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 949 | ret = srtp_filter_.SetOffer(cryptos, src); |
| 950 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 951 | break; |
| 952 | case CA_PRANSWER: |
| 953 | // If we're doing DTLS-SRTP, we don't want to update the filter |
| 954 | // with an answer, because we already have SRTP parameters. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 955 | if (!dtls) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 956 | ret = srtp_filter_.SetProvisionalAnswer(cryptos, src); |
| 957 | } |
| 958 | break; |
| 959 | case CA_ANSWER: |
| 960 | // If we're doing DTLS-SRTP, we don't want to update the filter |
| 961 | // with an answer, because we already have SRTP parameters. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 962 | if (!dtls) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 963 | ret = srtp_filter_.SetAnswer(cryptos, src); |
| 964 | } |
| 965 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 966 | default: |
| 967 | break; |
| 968 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 969 | if (!ret) { |
| 970 | SafeSetError("Failed to setup SRTP filter.", error_desc); |
| 971 | return false; |
| 972 | } |
| 973 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 977 | ContentSource src, |
| 978 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 979 | bool ret = false; |
| 980 | switch (action) { |
| 981 | case CA_OFFER: |
| 982 | ret = rtcp_mux_filter_.SetOffer(enable, src); |
| 983 | break; |
| 984 | case CA_PRANSWER: |
| 985 | ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src); |
| 986 | break; |
| 987 | case CA_ANSWER: |
| 988 | ret = rtcp_mux_filter_.SetAnswer(enable, src); |
| 989 | if (ret && rtcp_mux_filter_.IsActive()) { |
| 990 | // We activated RTCP mux, close down the RTCP transport. |
| 991 | set_rtcp_transport_channel(NULL); |
| 992 | } |
| 993 | break; |
| 994 | case CA_UPDATE: |
| 995 | // No RTCP mux info. |
| 996 | ret = true; |
| 997 | default: |
| 998 | break; |
| 999 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1000 | if (!ret) { |
| 1001 | SafeSetError("Failed to setup RTCP mux filter.", error_desc); |
| 1002 | return false; |
| 1003 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1004 | // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or |
| 1005 | // CA_ANSWER, but we only want to tear down the RTCP transport channel if we |
| 1006 | // received a final answer. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1007 | if (rtcp_mux_filter_.IsActive()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1008 | // If the RTP transport is already writable, then so are we. |
| 1009 | if (transport_channel_->writable()) { |
| 1010 | ChannelWritable_w(); |
| 1011 | } |
| 1012 | } |
| 1013 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1014 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | bool BaseChannel::AddRecvStream_w(const StreamParams& sp) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1018 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1019 | if (!media_channel()->AddRecvStream(sp)) |
| 1020 | return false; |
| 1021 | |
buildbot@webrtc.org | 5ee0f05 | 2014-05-05 20:18:08 +0000 | [diff] [blame] | 1022 | return bundle_filter_.AddStream(sp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1026 | ASSERT(worker_thread() == rtc::Thread::Current()); |
buildbot@webrtc.org | 5ee0f05 | 2014-05-05 20:18:08 +0000 | [diff] [blame] | 1027 | bundle_filter_.RemoveStream(ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1028 | return media_channel()->RemoveRecvStream(ssrc); |
| 1029 | } |
| 1030 | |
| 1031 | bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1032 | ContentAction action, |
| 1033 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1034 | if (!VERIFY(action == CA_OFFER || action == CA_ANSWER || |
| 1035 | action == CA_PRANSWER || action == CA_UPDATE)) |
| 1036 | return false; |
| 1037 | |
| 1038 | // If this is an update, streams only contain streams that have changed. |
| 1039 | if (action == CA_UPDATE) { |
| 1040 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1041 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1042 | const StreamParams* existing_stream = |
| 1043 | GetStreamByIds(local_streams_, it->groupid, it->id); |
| 1044 | if (!existing_stream && it->has_ssrcs()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1045 | if (media_channel()->AddSendStream(*it)) { |
| 1046 | local_streams_.push_back(*it); |
| 1047 | LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc(); |
| 1048 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1049 | std::ostringstream desc; |
| 1050 | desc << "Failed to add send stream ssrc: " << it->first_ssrc(); |
| 1051 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1052 | return false; |
| 1053 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1054 | } else if (existing_stream && !it->has_ssrcs()) { |
| 1055 | if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1056 | std::ostringstream desc; |
| 1057 | desc << "Failed to remove send stream with ssrc " |
| 1058 | << it->first_ssrc() << "."; |
| 1059 | SafeSetError(desc.str(), error_desc); |
| 1060 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1061 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1062 | RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1063 | } else { |
| 1064 | LOG(LS_WARNING) << "Ignore unsupported stream update"; |
| 1065 | } |
| 1066 | } |
| 1067 | return true; |
| 1068 | } |
| 1069 | // Else streams are all the streams we want to send. |
| 1070 | |
| 1071 | // Check for streams that have been removed. |
| 1072 | bool ret = true; |
| 1073 | for (StreamParamsVec::const_iterator it = local_streams_.begin(); |
| 1074 | it != local_streams_.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1075 | if (!GetStreamBySsrc(streams, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1076 | if (!media_channel()->RemoveSendStream(it->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1077 | std::ostringstream desc; |
| 1078 | desc << "Failed to remove send stream with ssrc " |
| 1079 | << it->first_ssrc() << "."; |
| 1080 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1081 | ret = false; |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | // Check for new streams. |
| 1086 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1087 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1088 | if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1089 | if (media_channel()->AddSendStream(*it)) { |
| 1090 | LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0]; |
| 1091 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1092 | std::ostringstream desc; |
| 1093 | desc << "Failed to add send stream ssrc: " << it->first_ssrc(); |
| 1094 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1095 | ret = false; |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | local_streams_ = streams; |
| 1100 | return ret; |
| 1101 | } |
| 1102 | |
| 1103 | bool BaseChannel::UpdateRemoteStreams_w( |
| 1104 | const std::vector<StreamParams>& streams, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1105 | ContentAction action, |
| 1106 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1107 | if (!VERIFY(action == CA_OFFER || action == CA_ANSWER || |
| 1108 | action == CA_PRANSWER || action == CA_UPDATE)) |
| 1109 | return false; |
| 1110 | |
| 1111 | // If this is an update, streams only contain streams that have changed. |
| 1112 | if (action == CA_UPDATE) { |
| 1113 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1114 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1115 | const StreamParams* existing_stream = |
| 1116 | GetStreamByIds(remote_streams_, it->groupid, it->id); |
| 1117 | if (!existing_stream && it->has_ssrcs()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1118 | if (AddRecvStream_w(*it)) { |
| 1119 | remote_streams_.push_back(*it); |
| 1120 | LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc(); |
| 1121 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1122 | std::ostringstream desc; |
| 1123 | desc << "Failed to add remote stream ssrc: " << it->first_ssrc(); |
| 1124 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1125 | return false; |
| 1126 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1127 | } else if (existing_stream && !it->has_ssrcs()) { |
| 1128 | if (!RemoveRecvStream_w(existing_stream->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1129 | std::ostringstream desc; |
| 1130 | desc << "Failed to remove remote stream with ssrc " |
| 1131 | << it->first_ssrc() << "."; |
| 1132 | SafeSetError(desc.str(), error_desc); |
| 1133 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1134 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1135 | RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1136 | } else { |
| 1137 | LOG(LS_WARNING) << "Ignore unsupported stream update." |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1138 | << " Stream exists? " << (existing_stream != nullptr) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1139 | << " new stream = " << it->ToString(); |
| 1140 | } |
| 1141 | } |
| 1142 | return true; |
| 1143 | } |
| 1144 | // Else streams are all the streams we want to receive. |
| 1145 | |
| 1146 | // Check for streams that have been removed. |
| 1147 | bool ret = true; |
| 1148 | for (StreamParamsVec::const_iterator it = remote_streams_.begin(); |
| 1149 | it != remote_streams_.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1150 | if (!GetStreamBySsrc(streams, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1151 | if (!RemoveRecvStream_w(it->first_ssrc())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1152 | std::ostringstream desc; |
| 1153 | desc << "Failed to remove remote stream with ssrc " |
| 1154 | << it->first_ssrc() << "."; |
| 1155 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1156 | ret = false; |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | // Check for new streams. |
| 1161 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 1162 | it != streams.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1163 | if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1164 | if (AddRecvStream_w(*it)) { |
| 1165 | LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0]; |
| 1166 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1167 | std::ostringstream desc; |
| 1168 | desc << "Failed to add remote stream ssrc: " << it->first_ssrc(); |
| 1169 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1170 | ret = false; |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | remote_streams_ = streams; |
| 1175 | return ret; |
| 1176 | } |
| 1177 | |
| 1178 | bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1179 | ContentAction action, |
| 1180 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1181 | // Cache secure_required_ for belt and suspenders check on SendPacket |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 1182 | secure_required_ = content->crypto_required() != CT_NONE; |
buildbot@webrtc.org | 75ce920 | 2014-06-20 12:30:24 +0000 | [diff] [blame] | 1183 | // Set local RTP header extensions. |
| 1184 | bool ret = SetRecvRtpHeaderExtensions_w(content, media_channel(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1185 | // Set local SRTP parameters (what we will encrypt with). |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1186 | ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1187 | // Set local RTCP mux parameters. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1188 | ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc); |
buildbot@webrtc.org | 75ce920 | 2014-06-20 12:30:24 +0000 | [diff] [blame] | 1189 | |
| 1190 | // Call UpdateLocalStreams_w last to make sure as many settings as possible |
| 1191 | // are already set when creating streams. |
| 1192 | ret &= UpdateLocalStreams_w(content->streams(), action, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1193 | set_local_content_direction(content->direction()); |
| 1194 | return ret; |
| 1195 | } |
| 1196 | |
| 1197 | bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1198 | ContentAction action, |
| 1199 | std::string* error_desc) { |
buildbot@webrtc.org | 75ce920 | 2014-06-20 12:30:24 +0000 | [diff] [blame] | 1200 | // Set remote RTP header extensions. |
| 1201 | bool ret = SetSendRtpHeaderExtensions_w(content, media_channel(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1202 | // Set remote SRTP parameters (what the other side will encrypt with). |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1203 | ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1204 | // Set remote RTCP mux parameters. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1205 | ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1206 | if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) { |
| 1207 | std::ostringstream desc; |
| 1208 | desc << "Failed to set max send bandwidth for " |
| 1209 | << MediaTypeToString(content->type()) << " content."; |
| 1210 | SafeSetError(desc.str(), error_desc); |
| 1211 | ret = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1212 | } |
buildbot@webrtc.org | 75ce920 | 2014-06-20 12:30:24 +0000 | [diff] [blame] | 1213 | |
| 1214 | // Call UpdateRemoteStreams_w last to make sure as many settings as possible |
| 1215 | // are already set when creating streams. |
| 1216 | ret &= UpdateRemoteStreams_w(content->streams(), action, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1217 | set_remote_content_direction(content->direction()); |
| 1218 | return ret; |
| 1219 | } |
| 1220 | |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 1221 | void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension( |
| 1222 | const std::vector<RtpHeaderExtension>& extensions) { |
| 1223 | const RtpHeaderExtension* send_time_extension = |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 1224 | FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension); |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 1225 | rtp_abs_sendtime_extn_id_ = |
| 1226 | send_time_extension ? send_time_extension->id : -1; |
| 1227 | } |
| 1228 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1229 | void BaseChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1230 | switch (pmsg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1231 | case MSG_RTPPACKET: |
| 1232 | case MSG_RTCPPACKET: { |
| 1233 | PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata); |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 1234 | SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1235 | delete data; // because it is Posted |
| 1236 | break; |
| 1237 | } |
| 1238 | case MSG_FIRSTPACKETRECEIVED: { |
| 1239 | SignalFirstPacketReceived(this); |
| 1240 | break; |
| 1241 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1245 | void BaseChannel::FlushRtcpMessages() { |
| 1246 | // Flush all remaining RTCP messages. This should only be called in |
| 1247 | // destructor. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1248 | ASSERT(rtc::Thread::Current() == worker_thread_); |
| 1249 | rtc::MessageList rtcp_messages; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1250 | worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1251 | for (rtc::MessageList::iterator it = rtcp_messages.begin(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1252 | it != rtcp_messages.end(); ++it) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1253 | worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1257 | VoiceChannel::VoiceChannel(rtc::Thread* thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1258 | MediaEngineInterface* media_engine, |
| 1259 | VoiceMediaChannel* media_channel, |
| 1260 | BaseSession* session, |
| 1261 | const std::string& content_name, |
| 1262 | bool rtcp) |
| 1263 | : BaseChannel(thread, media_engine, media_channel, session, content_name, |
| 1264 | rtcp), |
| 1265 | received_media_(false) { |
| 1266 | } |
| 1267 | |
| 1268 | VoiceChannel::~VoiceChannel() { |
| 1269 | StopAudioMonitor(); |
| 1270 | StopMediaMonitor(); |
| 1271 | // this can't be done in the base class, since it calls a virtual |
| 1272 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1273 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | bool VoiceChannel::Init() { |
| 1277 | TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel( |
| 1278 | content_name(), "rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL; |
| 1279 | if (!BaseChannel::Init(session()->CreateChannel( |
| 1280 | content_name(), "rtp", ICE_CANDIDATE_COMPONENT_RTP), |
| 1281 | rtcp_channel)) { |
| 1282 | return false; |
| 1283 | } |
| 1284 | media_channel()->SignalMediaError.connect( |
| 1285 | this, &VoiceChannel::OnVoiceChannelError); |
| 1286 | srtp_filter()->SignalSrtpError.connect( |
| 1287 | this, &VoiceChannel::OnSrtpError); |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1291 | bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1292 | return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer, |
| 1293 | media_channel(), ssrc, renderer)); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1297 | return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer, |
| 1298 | media_channel(), ssrc, renderer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | bool VoiceChannel::SetRingbackTone(const void* buf, int len) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1302 | return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | // TODO(juberti): Handle early media the right way. We should get an explicit |
| 1306 | // ringing message telling us to start playing local ringback, which we cancel |
| 1307 | // if any early media actually arrives. For now, we do the opposite, which is |
| 1308 | // to wait 1 second for early media, and start playing local ringback if none |
| 1309 | // arrives. |
| 1310 | void VoiceChannel::SetEarlyMedia(bool enable) { |
| 1311 | if (enable) { |
| 1312 | // Start the early media timeout |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1313 | worker_thread()->PostDelayed(kEarlyMediaTimeout, this, |
| 1314 | MSG_EARLYMEDIATIMEOUT); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1315 | } else { |
| 1316 | // Stop the timeout if currently going. |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1317 | worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1322 | return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w, |
| 1323 | this, ssrc, play, loop)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | bool VoiceChannel::PressDTMF(int digit, bool playout) { |
| 1327 | int flags = DF_SEND; |
| 1328 | if (playout) { |
| 1329 | flags |= DF_PLAY; |
| 1330 | } |
| 1331 | int duration_ms = 160; |
| 1332 | return InsertDtmf(0, digit, duration_ms, flags); |
| 1333 | } |
| 1334 | |
| 1335 | bool VoiceChannel::CanInsertDtmf() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1336 | return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf, |
| 1337 | media_channel())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration, |
| 1341 | int flags) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1342 | return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this, |
| 1343 | ssrc, event_code, duration, flags)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1347 | return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling, |
| 1348 | media_channel(), ssrc, left, right)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1349 | } |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1350 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1351 | bool VoiceChannel::GetStats(VoiceMediaInfo* stats) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1352 | return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats, |
| 1353 | media_channel(), stats)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | void VoiceChannel::StartMediaMonitor(int cms) { |
| 1357 | media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1358 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1359 | media_monitor_->SignalUpdate.connect( |
| 1360 | this, &VoiceChannel::OnMediaMonitorUpdate); |
| 1361 | media_monitor_->Start(cms); |
| 1362 | } |
| 1363 | |
| 1364 | void VoiceChannel::StopMediaMonitor() { |
| 1365 | if (media_monitor_) { |
| 1366 | media_monitor_->Stop(); |
| 1367 | media_monitor_->SignalUpdate.disconnect(this); |
| 1368 | media_monitor_.reset(); |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | void VoiceChannel::StartAudioMonitor(int cms) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1373 | audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1374 | audio_monitor_ |
| 1375 | ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate); |
| 1376 | audio_monitor_->Start(cms); |
| 1377 | } |
| 1378 | |
| 1379 | void VoiceChannel::StopAudioMonitor() { |
| 1380 | if (audio_monitor_) { |
| 1381 | audio_monitor_->Stop(); |
| 1382 | audio_monitor_.reset(); |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | bool VoiceChannel::IsAudioMonitorRunning() const { |
| 1387 | return (audio_monitor_.get() != NULL); |
| 1388 | } |
| 1389 | |
| 1390 | void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) { |
| 1391 | typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings)); |
| 1392 | SignalAutoMuted.repeat(typing_monitor_->SignalMuted); |
| 1393 | } |
| 1394 | |
| 1395 | void VoiceChannel::StopTypingMonitor() { |
| 1396 | typing_monitor_.reset(); |
| 1397 | } |
| 1398 | |
| 1399 | bool VoiceChannel::IsTypingMonitorRunning() const { |
| 1400 | return typing_monitor_; |
| 1401 | } |
| 1402 | |
| 1403 | bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) { |
| 1404 | bool ret = BaseChannel::MuteStream_w(ssrc, mute); |
| 1405 | if (typing_monitor_ && mute) |
| 1406 | typing_monitor_->OnChannelMuted(); |
| 1407 | return ret; |
| 1408 | } |
| 1409 | |
| 1410 | int VoiceChannel::GetInputLevel_w() { |
| 1411 | return media_engine()->GetInputLevel(); |
| 1412 | } |
| 1413 | |
| 1414 | int VoiceChannel::GetOutputLevel_w() { |
| 1415 | return media_channel()->GetOutputLevel(); |
| 1416 | } |
| 1417 | |
| 1418 | void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) { |
| 1419 | media_channel()->GetActiveStreams(actives); |
| 1420 | } |
| 1421 | |
| 1422 | void VoiceChannel::OnChannelRead(TransportChannel* channel, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 1423 | const char* data, size_t len, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1424 | const rtc::PacketTime& packet_time, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 1425 | int flags) { |
| 1426 | BaseChannel::OnChannelRead(channel, data, len, packet_time, flags); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1427 | |
| 1428 | // Set a flag when we've received an RTP packet. If we're waiting for early |
| 1429 | // media, this will disable the timeout. |
| 1430 | if (!received_media_ && !PacketIsRtcp(channel, data, len)) { |
| 1431 | received_media_ = true; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | void VoiceChannel::ChangeState() { |
| 1436 | // Render incoming data if we're the active call, and we have the local |
| 1437 | // content. We receive data on the default channel and multiplexed streams. |
| 1438 | bool recv = IsReadyToReceive(); |
| 1439 | if (!media_channel()->SetPlayout(recv)) { |
| 1440 | SendLastMediaError(); |
| 1441 | } |
| 1442 | |
| 1443 | // Send outgoing data if we're the active call, we have the remote content, |
| 1444 | // and we have had some form of connectivity. |
| 1445 | bool send = IsReadyToSend(); |
| 1446 | SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING; |
| 1447 | if (!media_channel()->SetSend(send_flag)) { |
| 1448 | LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel"; |
| 1449 | SendLastMediaError(); |
| 1450 | } |
| 1451 | |
| 1452 | LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send; |
| 1453 | } |
| 1454 | |
| 1455 | const ContentInfo* VoiceChannel::GetFirstContent( |
| 1456 | const SessionDescription* sdesc) { |
| 1457 | return GetFirstAudioContent(sdesc); |
| 1458 | } |
| 1459 | |
| 1460 | bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1461 | ContentAction action, |
| 1462 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1463 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1464 | LOG(LS_INFO) << "Setting local voice description"; |
| 1465 | |
| 1466 | const AudioContentDescription* audio = |
| 1467 | static_cast<const AudioContentDescription*>(content); |
| 1468 | ASSERT(audio != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1469 | if (!audio) { |
| 1470 | SafeSetError("Can't find audio content in local description.", error_desc); |
| 1471 | return false; |
| 1472 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1473 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1474 | bool ret = SetBaseLocalContent_w(content, action, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1475 | // Set local audio codecs (what we want to receive). |
| 1476 | // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial |
| 1477 | // is set properly. |
| 1478 | if (action != CA_UPDATE || audio->has_codecs()) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1479 | if (!media_channel()->SetRecvCodecs(audio->codecs())) { |
| 1480 | SafeSetError("Failed to set audio receive codecs.", error_desc); |
| 1481 | ret = false; |
| 1482 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | // If everything worked, see if we can start receiving. |
| 1486 | if (ret) { |
buildbot@webrtc.org | 5ee0f05 | 2014-05-05 20:18:08 +0000 | [diff] [blame] | 1487 | std::vector<AudioCodec>::const_iterator it = audio->codecs().begin(); |
| 1488 | for (; it != audio->codecs().end(); ++it) { |
| 1489 | bundle_filter()->AddPayloadType(it->id); |
| 1490 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1491 | ChangeState(); |
| 1492 | } else { |
| 1493 | LOG(LS_WARNING) << "Failed to set local voice description"; |
| 1494 | } |
| 1495 | return ret; |
| 1496 | } |
| 1497 | |
| 1498 | bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1499 | ContentAction action, |
| 1500 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1501 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1502 | LOG(LS_INFO) << "Setting remote voice description"; |
| 1503 | |
| 1504 | const AudioContentDescription* audio = |
| 1505 | static_cast<const AudioContentDescription*>(content); |
| 1506 | ASSERT(audio != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1507 | if (!audio) { |
| 1508 | SafeSetError("Can't find audio content in remote description.", error_desc); |
| 1509 | return false; |
| 1510 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1511 | |
| 1512 | bool ret = true; |
| 1513 | // Set remote video codecs (what the other side wants to receive). |
| 1514 | if (action != CA_UPDATE || audio->has_codecs()) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1515 | if (!media_channel()->SetSendCodecs(audio->codecs())) { |
| 1516 | SafeSetError("Failed to set audio send codecs.", error_desc); |
| 1517 | ret = false; |
| 1518 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1521 | ret &= SetBaseRemoteContent_w(content, action, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1522 | |
| 1523 | if (action != CA_UPDATE) { |
| 1524 | // Tweak our audio processing settings, if needed. |
| 1525 | AudioOptions audio_options; |
| 1526 | if (!media_channel()->GetOptions(&audio_options)) { |
| 1527 | LOG(LS_WARNING) << "Can not set audio options from on remote content."; |
| 1528 | } else { |
| 1529 | if (audio->conference_mode()) { |
| 1530 | audio_options.conference_mode.Set(true); |
| 1531 | } |
| 1532 | if (audio->agc_minus_10db()) { |
| 1533 | audio_options.adjust_agc_delta.Set(kAgcMinus10db); |
| 1534 | } |
| 1535 | if (!media_channel()->SetOptions(audio_options)) { |
| 1536 | // Log an error on failure, but don't abort the call. |
| 1537 | LOG(LS_ERROR) << "Failed to set voice channel options"; |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | // If everything worked, see if we can start sending. |
| 1543 | if (ret) { |
| 1544 | ChangeState(); |
| 1545 | } else { |
| 1546 | LOG(LS_WARNING) << "Failed to set remote voice description"; |
| 1547 | } |
| 1548 | return ret; |
| 1549 | } |
| 1550 | |
| 1551 | bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1552 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1553 | return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len); |
| 1554 | } |
| 1555 | |
| 1556 | bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1557 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1558 | if (play) { |
| 1559 | LOG(LS_INFO) << "Playing ringback tone, loop=" << loop; |
| 1560 | } else { |
| 1561 | LOG(LS_INFO) << "Stopping ringback tone"; |
| 1562 | } |
| 1563 | return media_channel()->PlayRingbackTone(ssrc, play, loop); |
| 1564 | } |
| 1565 | |
| 1566 | void VoiceChannel::HandleEarlyMediaTimeout() { |
| 1567 | // This occurs on the main thread, not the worker thread. |
| 1568 | if (!received_media_) { |
| 1569 | LOG(LS_INFO) << "No early media received before timeout"; |
| 1570 | SignalEarlyMediaTimeout(this); |
| 1571 | } |
| 1572 | } |
| 1573 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1574 | bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration, |
| 1575 | int flags) { |
| 1576 | if (!enabled()) { |
| 1577 | return false; |
| 1578 | } |
| 1579 | |
| 1580 | return media_channel()->InsertDtmf(ssrc, event, duration, flags); |
| 1581 | } |
| 1582 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1583 | bool VoiceChannel::SetChannelOptions(const AudioOptions& options) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1584 | return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions, |
| 1585 | media_channel(), options)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1588 | void VoiceChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1589 | switch (pmsg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1590 | case MSG_EARLYMEDIATIMEOUT: |
| 1591 | HandleEarlyMediaTimeout(); |
| 1592 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1593 | case MSG_CHANNEL_ERROR: { |
| 1594 | VoiceChannelErrorMessageData* data = |
| 1595 | static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata); |
| 1596 | SignalMediaError(this, data->ssrc, data->error); |
| 1597 | delete data; |
| 1598 | break; |
| 1599 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1600 | default: |
| 1601 | BaseChannel::OnMessage(pmsg); |
| 1602 | break; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | void VoiceChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame^] | 1607 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1608 | SignalConnectionMonitor(this, infos); |
| 1609 | } |
| 1610 | |
| 1611 | void VoiceChannel::OnMediaMonitorUpdate( |
| 1612 | VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) { |
| 1613 | ASSERT(media_channel == this->media_channel()); |
| 1614 | SignalMediaMonitor(this, info); |
| 1615 | } |
| 1616 | |
| 1617 | void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor, |
| 1618 | const AudioInfo& info) { |
| 1619 | SignalAudioMonitor(this, info); |
| 1620 | } |
| 1621 | |
| 1622 | void VoiceChannel::OnVoiceChannelError( |
| 1623 | uint32 ssrc, VoiceMediaChannel::Error err) { |
| 1624 | VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData( |
| 1625 | ssrc, err); |
| 1626 | signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data); |
| 1627 | } |
| 1628 | |
| 1629 | void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, |
| 1630 | SrtpFilter::Error error) { |
| 1631 | switch (error) { |
| 1632 | case SrtpFilter::ERROR_FAIL: |
| 1633 | OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ? |
| 1634 | VoiceMediaChannel::ERROR_REC_SRTP_ERROR : |
| 1635 | VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR); |
| 1636 | break; |
| 1637 | case SrtpFilter::ERROR_AUTH: |
| 1638 | OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ? |
| 1639 | VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED : |
| 1640 | VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED); |
| 1641 | break; |
| 1642 | case SrtpFilter::ERROR_REPLAY: |
| 1643 | // Only receving channel should have this error. |
| 1644 | ASSERT(mode == SrtpFilter::UNPROTECT); |
| 1645 | OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY); |
| 1646 | break; |
| 1647 | default: |
| 1648 | break; |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const { |
| 1653 | GetSupportedAudioCryptoSuites(ciphers); |
| 1654 | } |
| 1655 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1656 | VideoChannel::VideoChannel(rtc::Thread* thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1657 | MediaEngineInterface* media_engine, |
| 1658 | VideoMediaChannel* media_channel, |
| 1659 | BaseSession* session, |
| 1660 | const std::string& content_name, |
| 1661 | bool rtcp, |
| 1662 | VoiceChannel* voice_channel) |
| 1663 | : BaseChannel(thread, media_engine, media_channel, session, content_name, |
| 1664 | rtcp), |
| 1665 | voice_channel_(voice_channel), |
| 1666 | renderer_(NULL), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1667 | previous_we_(rtc::WE_CLOSE) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | bool VideoChannel::Init() { |
| 1671 | TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel( |
| 1672 | content_name(), "video_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL; |
| 1673 | if (!BaseChannel::Init(session()->CreateChannel( |
| 1674 | content_name(), "video_rtp", ICE_CANDIDATE_COMPONENT_RTP), |
| 1675 | rtcp_channel)) { |
| 1676 | return false; |
| 1677 | } |
| 1678 | media_channel()->SignalMediaError.connect( |
| 1679 | this, &VideoChannel::OnVideoChannelError); |
| 1680 | srtp_filter()->SignalSrtpError.connect( |
| 1681 | this, &VideoChannel::OnSrtpError); |
| 1682 | return true; |
| 1683 | } |
| 1684 | |
| 1685 | void VoiceChannel::SendLastMediaError() { |
| 1686 | uint32 ssrc; |
| 1687 | VoiceMediaChannel::Error error; |
| 1688 | media_channel()->GetLastMediaError(&ssrc, &error); |
| 1689 | SignalMediaError(this, ssrc, error); |
| 1690 | } |
| 1691 | |
| 1692 | VideoChannel::~VideoChannel() { |
| 1693 | std::vector<uint32> screencast_ssrcs; |
| 1694 | ScreencastMap::iterator iter; |
| 1695 | while (!screencast_capturers_.empty()) { |
| 1696 | if (!RemoveScreencast(screencast_capturers_.begin()->first)) { |
| 1697 | LOG(LS_ERROR) << "Unable to delete screencast with ssrc " |
| 1698 | << screencast_capturers_.begin()->first; |
| 1699 | ASSERT(false); |
| 1700 | break; |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | StopMediaMonitor(); |
| 1705 | // this can't be done in the base class, since it calls a virtual |
| 1706 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1707 | |
| 1708 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1712 | worker_thread()->Invoke<void>(Bind( |
| 1713 | &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1714 | return true; |
| 1715 | } |
| 1716 | |
| 1717 | bool VideoChannel::ApplyViewRequest(const ViewRequest& request) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1718 | return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 1721 | bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) { |
| 1722 | return worker_thread()->Invoke<bool>(Bind( |
| 1723 | &VideoChannel::AddScreencast_w, this, ssrc, capturer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1727 | return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer, |
| 1728 | media_channel(), ssrc, capturer)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | bool VideoChannel::RemoveScreencast(uint32 ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1732 | return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | bool VideoChannel::IsScreencasting() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1736 | return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1739 | int VideoChannel::GetScreencastFps(uint32 ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1740 | ScreencastDetailsData data(ssrc); |
| 1741 | worker_thread()->Invoke<void>(Bind( |
| 1742 | &VideoChannel::GetScreencastDetails_w, this, &data)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1743 | return data.fps; |
| 1744 | } |
| 1745 | |
| 1746 | int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1747 | ScreencastDetailsData data(ssrc); |
| 1748 | worker_thread()->Invoke<void>(Bind( |
| 1749 | &VideoChannel::GetScreencastDetails_w, this, &data)); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1750 | return data.screencast_max_pixels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | bool VideoChannel::SendIntraFrame() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1754 | worker_thread()->Invoke<void>(Bind( |
| 1755 | &VideoMediaChannel::SendIntraFrame, media_channel())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1756 | return true; |
| 1757 | } |
| 1758 | |
| 1759 | bool VideoChannel::RequestIntraFrame() { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1760 | worker_thread()->Invoke<void>(Bind( |
| 1761 | &VideoMediaChannel::RequestIntraFrame, media_channel())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1762 | return true; |
| 1763 | } |
| 1764 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1765 | void VideoChannel::ChangeState() { |
| 1766 | // Render incoming data if we're the active call, and we have the local |
| 1767 | // content. We receive data on the default channel and multiplexed streams. |
| 1768 | bool recv = IsReadyToReceive(); |
| 1769 | if (!media_channel()->SetRender(recv)) { |
| 1770 | LOG(LS_ERROR) << "Failed to SetRender on video channel"; |
| 1771 | // TODO(gangji): Report error back to server. |
| 1772 | } |
| 1773 | |
| 1774 | // Send outgoing data if we're the active call, we have the remote content, |
| 1775 | // and we have had some form of connectivity. |
| 1776 | bool send = IsReadyToSend(); |
| 1777 | if (!media_channel()->SetSend(send)) { |
| 1778 | LOG(LS_ERROR) << "Failed to SetSend on video channel"; |
| 1779 | // TODO(gangji): Report error back to server. |
| 1780 | } |
| 1781 | |
| 1782 | LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send; |
| 1783 | } |
| 1784 | |
pbos@webrtc.org | 058b1f1 | 2015-03-04 08:54:32 +0000 | [diff] [blame] | 1785 | bool VideoChannel::GetStats(VideoMediaInfo* stats) { |
| 1786 | return InvokeOnWorker( |
| 1787 | Bind(&VideoMediaChannel::GetStats, media_channel(), stats)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | void VideoChannel::StartMediaMonitor(int cms) { |
| 1791 | media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1792 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1793 | media_monitor_->SignalUpdate.connect( |
| 1794 | this, &VideoChannel::OnMediaMonitorUpdate); |
| 1795 | media_monitor_->Start(cms); |
| 1796 | } |
| 1797 | |
| 1798 | void VideoChannel::StopMediaMonitor() { |
| 1799 | if (media_monitor_) { |
| 1800 | media_monitor_->Stop(); |
| 1801 | media_monitor_.reset(); |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | const ContentInfo* VideoChannel::GetFirstContent( |
| 1806 | const SessionDescription* sdesc) { |
| 1807 | return GetFirstVideoContent(sdesc); |
| 1808 | } |
| 1809 | |
| 1810 | bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1811 | ContentAction action, |
| 1812 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1813 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1814 | LOG(LS_INFO) << "Setting local video description"; |
| 1815 | |
| 1816 | const VideoContentDescription* video = |
| 1817 | static_cast<const VideoContentDescription*>(content); |
| 1818 | ASSERT(video != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1819 | if (!video) { |
| 1820 | SafeSetError("Can't find video content in local description.", error_desc); |
| 1821 | return false; |
| 1822 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1823 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1824 | bool ret = SetBaseLocalContent_w(content, action, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1825 | // Set local video codecs (what we want to receive). |
| 1826 | if (action != CA_UPDATE || video->has_codecs()) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1827 | if (!media_channel()->SetRecvCodecs(video->codecs())) { |
| 1828 | SafeSetError("Failed to set video receive codecs.", error_desc); |
| 1829 | ret = false; |
| 1830 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | if (action != CA_UPDATE) { |
| 1834 | VideoOptions video_options; |
| 1835 | media_channel()->GetOptions(&video_options); |
| 1836 | video_options.buffered_mode_latency.Set(video->buffered_mode_latency()); |
| 1837 | |
| 1838 | if (!media_channel()->SetOptions(video_options)) { |
| 1839 | // Log an error on failure, but don't abort the call. |
| 1840 | LOG(LS_ERROR) << "Failed to set video channel options"; |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | // If everything worked, see if we can start receiving. |
| 1845 | if (ret) { |
buildbot@webrtc.org | 5ee0f05 | 2014-05-05 20:18:08 +0000 | [diff] [blame] | 1846 | std::vector<VideoCodec>::const_iterator it = video->codecs().begin(); |
| 1847 | for (; it != video->codecs().end(); ++it) { |
| 1848 | bundle_filter()->AddPayloadType(it->id); |
| 1849 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1850 | ChangeState(); |
| 1851 | } else { |
| 1852 | LOG(LS_WARNING) << "Failed to set local video description"; |
| 1853 | } |
| 1854 | return ret; |
| 1855 | } |
| 1856 | |
| 1857 | bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1858 | ContentAction action, |
| 1859 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1860 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1861 | LOG(LS_INFO) << "Setting remote video description"; |
| 1862 | |
| 1863 | const VideoContentDescription* video = |
| 1864 | static_cast<const VideoContentDescription*>(content); |
| 1865 | ASSERT(video != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1866 | if (!video) { |
| 1867 | SafeSetError("Can't find video content in remote description.", error_desc); |
| 1868 | return false; |
| 1869 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1870 | |
| 1871 | bool ret = true; |
| 1872 | // Set remote video codecs (what the other side wants to receive). |
| 1873 | if (action != CA_UPDATE || video->has_codecs()) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1874 | if (!media_channel()->SetSendCodecs(video->codecs())) { |
| 1875 | SafeSetError("Failed to set video send codecs.", error_desc); |
| 1876 | ret = false; |
| 1877 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1880 | ret &= SetBaseRemoteContent_w(content, action, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1881 | |
| 1882 | if (action != CA_UPDATE) { |
| 1883 | // Tweak our video processing settings, if needed. |
| 1884 | VideoOptions video_options; |
| 1885 | media_channel()->GetOptions(&video_options); |
wu@webrtc.org | 9caf276 | 2013-12-11 18:25:07 +0000 | [diff] [blame] | 1886 | if (video->conference_mode()) { |
| 1887 | video_options.conference_mode.Set(true); |
| 1888 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1889 | video_options.buffered_mode_latency.Set(video->buffered_mode_latency()); |
| 1890 | |
| 1891 | if (!media_channel()->SetOptions(video_options)) { |
| 1892 | // Log an error on failure, but don't abort the call. |
| 1893 | LOG(LS_ERROR) << "Failed to set video channel options"; |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | // If everything worked, see if we can start sending. |
| 1898 | if (ret) { |
| 1899 | ChangeState(); |
| 1900 | } else { |
| 1901 | LOG(LS_WARNING) << "Failed to set remote video description"; |
| 1902 | } |
| 1903 | return ret; |
| 1904 | } |
| 1905 | |
| 1906 | bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) { |
| 1907 | bool ret = true; |
| 1908 | // Set the send format for each of the local streams. If the view request |
| 1909 | // does not contain a local stream, set its send format to 0x0, which will |
| 1910 | // drop all frames. |
| 1911 | for (std::vector<StreamParams>::const_iterator it = local_streams().begin(); |
| 1912 | it != local_streams().end(); ++it) { |
| 1913 | VideoFormat format(0, 0, 0, cricket::FOURCC_I420); |
| 1914 | StaticVideoViews::const_iterator view; |
| 1915 | for (view = request.static_video_views.begin(); |
| 1916 | view != request.static_video_views.end(); ++view) { |
| 1917 | if (view->selector.Matches(*it)) { |
| 1918 | format.width = view->width; |
| 1919 | format.height = view->height; |
| 1920 | format.interval = cricket::VideoFormat::FpsToInterval(view->framerate); |
| 1921 | break; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format); |
| 1926 | } |
| 1927 | |
| 1928 | // Check if the view request has invalid streams. |
| 1929 | for (StaticVideoViews::const_iterator it = request.static_video_views.begin(); |
| 1930 | it != request.static_video_views.end(); ++it) { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 1931 | if (!GetStream(local_streams(), it->selector)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1932 | LOG(LS_WARNING) << "View request for (" |
| 1933 | << it->selector.ssrc << ", '" |
| 1934 | << it->selector.groupid << "', '" |
| 1935 | << it->selector.streamid << "'" |
| 1936 | << ") is not in the local streams."; |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | return ret; |
| 1941 | } |
| 1942 | |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 1943 | bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1944 | if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) { |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 1945 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1946 | } |
buildbot@webrtc.org | 65b98d1 | 2014-08-07 22:09:08 +0000 | [diff] [blame] | 1947 | capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange); |
| 1948 | screencast_capturers_[ssrc] = capturer; |
| 1949 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1952 | bool VideoChannel::RemoveScreencast_w(uint32 ssrc) { |
| 1953 | ScreencastMap::iterator iter = screencast_capturers_.find(ssrc); |
| 1954 | if (iter == screencast_capturers_.end()) { |
| 1955 | return false; |
| 1956 | } |
| 1957 | // Clean up VideoCapturer. |
| 1958 | delete iter->second; |
| 1959 | screencast_capturers_.erase(iter); |
| 1960 | return true; |
| 1961 | } |
| 1962 | |
| 1963 | bool VideoChannel::IsScreencasting_w() const { |
| 1964 | return !screencast_capturers_.empty(); |
| 1965 | } |
| 1966 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1967 | void VideoChannel::GetScreencastDetails_w( |
| 1968 | ScreencastDetailsData* data) const { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1969 | ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1970 | if (iter == screencast_capturers_.end()) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1971 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1972 | } |
| 1973 | VideoCapturer* capturer = iter->second; |
| 1974 | const VideoFormat* video_format = capturer->GetCaptureFormat(); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1975 | data->fps = VideoFormat::IntervalToFps(video_format->interval); |
| 1976 | data->screencast_max_pixels = capturer->screencast_max_pixels(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1979 | void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1980 | rtc::WindowEvent we) { |
| 1981 | ASSERT(signaling_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1982 | SignalScreencastWindowEvent(ssrc, we); |
| 1983 | } |
| 1984 | |
| 1985 | bool VideoChannel::SetChannelOptions(const VideoOptions &options) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1986 | return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions, |
| 1987 | media_channel(), options)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1990 | void VideoChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1991 | switch (pmsg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1992 | case MSG_SCREENCASTWINDOWEVENT: { |
| 1993 | const ScreencastEventMessageData* data = |
| 1994 | static_cast<ScreencastEventMessageData*>(pmsg->pdata); |
| 1995 | OnScreencastWindowEvent_s(data->ssrc, data->event); |
| 1996 | delete data; |
| 1997 | break; |
| 1998 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1999 | case MSG_CHANNEL_ERROR: { |
| 2000 | const VideoChannelErrorMessageData* data = |
| 2001 | static_cast<VideoChannelErrorMessageData*>(pmsg->pdata); |
| 2002 | SignalMediaError(this, data->ssrc, data->error); |
| 2003 | delete data; |
| 2004 | break; |
| 2005 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2006 | default: |
| 2007 | BaseChannel::OnMessage(pmsg); |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | void VideoChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame^] | 2013 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2014 | SignalConnectionMonitor(this, infos); |
| 2015 | } |
| 2016 | |
| 2017 | // TODO(pthatcher): Look into removing duplicate code between |
| 2018 | // audio, video, and data, perhaps by using templates. |
| 2019 | void VideoChannel::OnMediaMonitorUpdate( |
| 2020 | VideoMediaChannel* media_channel, const VideoMediaInfo &info) { |
| 2021 | ASSERT(media_channel == this->media_channel()); |
| 2022 | SignalMediaMonitor(this, info); |
| 2023 | } |
| 2024 | |
| 2025 | void VideoChannel::OnScreencastWindowEvent(uint32 ssrc, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2026 | rtc::WindowEvent event) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2027 | ScreencastEventMessageData* pdata = |
| 2028 | new ScreencastEventMessageData(ssrc, event); |
| 2029 | signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata); |
| 2030 | } |
| 2031 | |
| 2032 | void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) { |
| 2033 | // Map capturer events to window events. In the future we may want to simply |
| 2034 | // pass these events up directly. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2035 | rtc::WindowEvent we; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2036 | if (ev == CS_STOPPED) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2037 | we = rtc::WE_CLOSE; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2038 | } else if (ev == CS_PAUSED) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2039 | we = rtc::WE_MINIMIZE; |
| 2040 | } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) { |
| 2041 | we = rtc::WE_RESTORE; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2042 | } else { |
| 2043 | return; |
| 2044 | } |
| 2045 | previous_we_ = we; |
| 2046 | |
| 2047 | uint32 ssrc = 0; |
| 2048 | if (!GetLocalSsrc(capturer, &ssrc)) { |
| 2049 | return; |
| 2050 | } |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 2051 | |
| 2052 | OnScreencastWindowEvent(ssrc, we); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) { |
| 2056 | *ssrc = 0; |
| 2057 | for (ScreencastMap::iterator iter = screencast_capturers_.begin(); |
| 2058 | iter != screencast_capturers_.end(); ++iter) { |
| 2059 | if (iter->second == capturer) { |
| 2060 | *ssrc = iter->first; |
| 2061 | return true; |
| 2062 | } |
| 2063 | } |
| 2064 | return false; |
| 2065 | } |
| 2066 | |
| 2067 | void VideoChannel::OnVideoChannelError(uint32 ssrc, |
| 2068 | VideoMediaChannel::Error error) { |
| 2069 | VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData( |
| 2070 | ssrc, error); |
| 2071 | signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data); |
| 2072 | } |
| 2073 | |
| 2074 | void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, |
| 2075 | SrtpFilter::Error error) { |
| 2076 | switch (error) { |
| 2077 | case SrtpFilter::ERROR_FAIL: |
| 2078 | OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ? |
| 2079 | VideoMediaChannel::ERROR_REC_SRTP_ERROR : |
| 2080 | VideoMediaChannel::ERROR_PLAY_SRTP_ERROR); |
| 2081 | break; |
| 2082 | case SrtpFilter::ERROR_AUTH: |
| 2083 | OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ? |
| 2084 | VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED : |
| 2085 | VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED); |
| 2086 | break; |
| 2087 | case SrtpFilter::ERROR_REPLAY: |
| 2088 | // Only receving channel should have this error. |
| 2089 | ASSERT(mode == SrtpFilter::UNPROTECT); |
| 2090 | // TODO(gangji): Turn on the signaling of replay error once we have |
| 2091 | // switched to the new mechanism for doing video retransmissions. |
| 2092 | // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY); |
| 2093 | break; |
| 2094 | default: |
| 2095 | break; |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | |
| 2100 | void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const { |
| 2101 | GetSupportedVideoCryptoSuites(ciphers); |
| 2102 | } |
| 2103 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2104 | DataChannel::DataChannel(rtc::Thread* thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2105 | DataMediaChannel* media_channel, |
| 2106 | BaseSession* session, |
| 2107 | const std::string& content_name, |
| 2108 | bool rtcp) |
| 2109 | // MediaEngine is NULL |
| 2110 | : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp), |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 2111 | data_channel_type_(cricket::DCT_NONE), |
| 2112 | ready_to_send_data_(false) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | DataChannel::~DataChannel() { |
| 2116 | StopMediaMonitor(); |
| 2117 | // this can't be done in the base class, since it calls a virtual |
| 2118 | DisableMedia_w(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 2119 | |
| 2120 | Deinit(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | bool DataChannel::Init() { |
| 2124 | TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel( |
| 2125 | content_name(), "data_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL; |
| 2126 | if (!BaseChannel::Init(session()->CreateChannel( |
| 2127 | content_name(), "data_rtp", ICE_CANDIDATE_COMPONENT_RTP), |
| 2128 | rtcp_channel)) { |
| 2129 | return false; |
| 2130 | } |
| 2131 | media_channel()->SignalDataReceived.connect( |
| 2132 | this, &DataChannel::OnDataReceived); |
| 2133 | media_channel()->SignalMediaError.connect( |
| 2134 | this, &DataChannel::OnDataChannelError); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2135 | media_channel()->SignalReadyToSend.connect( |
| 2136 | this, &DataChannel::OnDataChannelReadyToSend); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2137 | media_channel()->SignalStreamClosedRemotely.connect( |
| 2138 | this, &DataChannel::OnStreamClosedRemotely); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2139 | srtp_filter()->SignalSrtpError.connect( |
| 2140 | this, &DataChannel::OnSrtpError); |
| 2141 | return true; |
| 2142 | } |
| 2143 | |
| 2144 | bool DataChannel::SendData(const SendDataParams& params, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2145 | const rtc::Buffer& payload, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2146 | SendDataResult* result) { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 2147 | return InvokeOnWorker(Bind(&DataMediaChannel::SendData, |
| 2148 | media_channel(), params, payload, result)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | const ContentInfo* DataChannel::GetFirstContent( |
| 2152 | const SessionDescription* sdesc) { |
| 2153 | return GetFirstDataContent(sdesc); |
| 2154 | } |
| 2155 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2156 | bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2157 | if (data_channel_type_ == DCT_SCTP) { |
| 2158 | // TODO(pthatcher): Do this in a more robust way by checking for |
| 2159 | // SCTP or DTLS. |
buildbot@webrtc.org | 1ef789d | 2014-06-19 23:54:12 +0000 | [diff] [blame] | 2160 | return !IsRtpPacket(packet->data(), packet->length()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2161 | } else if (data_channel_type_ == DCT_RTP) { |
| 2162 | return BaseChannel::WantsPacket(rtcp, packet); |
| 2163 | } |
| 2164 | return false; |
| 2165 | } |
| 2166 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2167 | bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type, |
| 2168 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2169 | // It hasn't been set before, so set it now. |
| 2170 | if (data_channel_type_ == DCT_NONE) { |
| 2171 | data_channel_type_ = new_data_channel_type; |
| 2172 | return true; |
| 2173 | } |
| 2174 | |
| 2175 | // It's been set before, but doesn't match. That's bad. |
| 2176 | if (data_channel_type_ != new_data_channel_type) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2177 | std::ostringstream desc; |
| 2178 | desc << "Data channel type mismatch." |
| 2179 | << " Expected " << data_channel_type_ |
| 2180 | << " Got " << new_data_channel_type; |
| 2181 | SafeSetError(desc.str(), error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2182 | return false; |
| 2183 | } |
| 2184 | |
| 2185 | // It's hasn't changed. Nothing to do. |
| 2186 | return true; |
| 2187 | } |
| 2188 | |
| 2189 | bool DataChannel::SetDataChannelTypeFromContent( |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2190 | const DataContentDescription* content, |
| 2191 | std::string* error_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2192 | bool is_sctp = ((content->protocol() == kMediaProtocolSctp) || |
| 2193 | (content->protocol() == kMediaProtocolDtlsSctp)); |
| 2194 | DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2195 | return SetDataChannelType(data_channel_type, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | bool DataChannel::SetLocalContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2199 | ContentAction action, |
| 2200 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2201 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2202 | LOG(LS_INFO) << "Setting local data description"; |
| 2203 | |
| 2204 | const DataContentDescription* data = |
| 2205 | static_cast<const DataContentDescription*>(content); |
| 2206 | ASSERT(data != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2207 | if (!data) { |
| 2208 | SafeSetError("Can't find data content in local description.", error_desc); |
| 2209 | return false; |
| 2210 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2211 | |
| 2212 | bool ret = false; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2213 | if (!SetDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2214 | return false; |
| 2215 | } |
| 2216 | |
| 2217 | if (data_channel_type_ == DCT_SCTP) { |
| 2218 | // SCTP data channels don't need the rest of the stuff. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2219 | ret = UpdateLocalStreams_w(data->streams(), action, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2220 | if (ret) { |
| 2221 | set_local_content_direction(content->direction()); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 2222 | // As in SetRemoteContent_w, make sure we set the local SCTP port |
| 2223 | // number as specified in our DataContentDescription. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2224 | if (!media_channel()->SetRecvCodecs(data->codecs())) { |
| 2225 | SafeSetError("Failed to set data receive codecs.", error_desc); |
| 2226 | ret = false; |
| 2227 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2230 | ret = SetBaseLocalContent_w(content, action, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2231 | if (action != CA_UPDATE || data->has_codecs()) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2232 | if (!media_channel()->SetRecvCodecs(data->codecs())) { |
| 2233 | SafeSetError("Failed to set data receive codecs.", error_desc); |
| 2234 | ret = false; |
| 2235 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | // If everything worked, see if we can start receiving. |
| 2240 | if (ret) { |
buildbot@webrtc.org | 5ee0f05 | 2014-05-05 20:18:08 +0000 | [diff] [blame] | 2241 | std::vector<DataCodec>::const_iterator it = data->codecs().begin(); |
| 2242 | for (; it != data->codecs().end(); ++it) { |
| 2243 | bundle_filter()->AddPayloadType(it->id); |
| 2244 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2245 | ChangeState(); |
| 2246 | } else { |
| 2247 | LOG(LS_WARNING) << "Failed to set local data description"; |
| 2248 | } |
| 2249 | return ret; |
| 2250 | } |
| 2251 | |
| 2252 | bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2253 | ContentAction action, |
| 2254 | std::string* error_desc) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2255 | ASSERT(worker_thread() == rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2256 | |
| 2257 | const DataContentDescription* data = |
| 2258 | static_cast<const DataContentDescription*>(content); |
| 2259 | ASSERT(data != NULL); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2260 | if (!data) { |
| 2261 | SafeSetError("Can't find data content in remote description.", error_desc); |
| 2262 | return false; |
| 2263 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2264 | |
| 2265 | bool ret = true; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2266 | if (!SetDataChannelTypeFromContent(data, error_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2267 | return false; |
| 2268 | } |
| 2269 | |
| 2270 | if (data_channel_type_ == DCT_SCTP) { |
| 2271 | LOG(LS_INFO) << "Setting SCTP remote data description"; |
| 2272 | // SCTP data channels don't need the rest of the stuff. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2273 | ret = UpdateRemoteStreams_w(content->streams(), action, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2274 | if (ret) { |
| 2275 | set_remote_content_direction(content->direction()); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 2276 | // We send the SCTP port number (not to be confused with the underlying |
| 2277 | // 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] | 2278 | if (!media_channel()->SetSendCodecs(data->codecs())) { |
| 2279 | SafeSetError("Failed to set data send codecs.", error_desc); |
| 2280 | ret = false; |
| 2281 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2282 | } |
| 2283 | } else { |
| 2284 | // If the remote data doesn't have codecs and isn't an update, it |
| 2285 | // must be empty, so ignore it. |
| 2286 | if (action != CA_UPDATE && !data->has_codecs()) { |
| 2287 | return true; |
| 2288 | } |
| 2289 | LOG(LS_INFO) << "Setting remote data description"; |
| 2290 | |
| 2291 | // Set remote video codecs (what the other side wants to receive). |
| 2292 | if (action != CA_UPDATE || data->has_codecs()) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2293 | if (!media_channel()->SetSendCodecs(data->codecs())) { |
| 2294 | SafeSetError("Failed to set data send codecs.", error_desc); |
| 2295 | ret = false; |
| 2296 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | if (ret) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2300 | ret &= SetBaseRemoteContent_w(content, action, error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | if (action != CA_UPDATE) { |
| 2304 | int bandwidth_bps = data->bandwidth(); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2305 | if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) { |
| 2306 | std::ostringstream desc; |
| 2307 | desc << "Failed to set max send bandwidth for data content."; |
| 2308 | SafeSetError(desc.str(), error_desc); |
| 2309 | ret = false; |
| 2310 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | // If everything worked, see if we can start sending. |
| 2315 | if (ret) { |
| 2316 | ChangeState(); |
| 2317 | } else { |
| 2318 | LOG(LS_WARNING) << "Failed to set remote data description"; |
| 2319 | } |
| 2320 | return ret; |
| 2321 | } |
| 2322 | |
| 2323 | void DataChannel::ChangeState() { |
| 2324 | // Render incoming data if we're the active call, and we have the local |
| 2325 | // content. We receive data on the default channel and multiplexed streams. |
| 2326 | bool recv = IsReadyToReceive(); |
| 2327 | if (!media_channel()->SetReceive(recv)) { |
| 2328 | LOG(LS_ERROR) << "Failed to SetReceive on data channel"; |
| 2329 | } |
| 2330 | |
| 2331 | // Send outgoing data if we're the active call, we have the remote content, |
| 2332 | // and we have had some form of connectivity. |
| 2333 | bool send = IsReadyToSend(); |
| 2334 | if (!media_channel()->SetSend(send)) { |
| 2335 | LOG(LS_ERROR) << "Failed to SetSend on data channel"; |
| 2336 | } |
| 2337 | |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 2338 | // Trigger SignalReadyToSendData asynchronously. |
| 2339 | OnDataChannelReadyToSend(send); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2340 | |
| 2341 | LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send; |
| 2342 | } |
| 2343 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2344 | void DataChannel::OnMessage(rtc::Message *pmsg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2345 | switch (pmsg->message_id) { |
| 2346 | case MSG_READYTOSENDDATA: { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2347 | DataChannelReadyToSendMessageData* data = |
| 2348 | static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 2349 | ready_to_send_data_ = data->data(); |
| 2350 | SignalReadyToSendData(ready_to_send_data_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2351 | delete data; |
| 2352 | break; |
| 2353 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2354 | case MSG_DATARECEIVED: { |
| 2355 | DataReceivedMessageData* data = |
| 2356 | static_cast<DataReceivedMessageData*>(pmsg->pdata); |
| 2357 | SignalDataReceived(this, data->params, data->payload); |
| 2358 | delete data; |
| 2359 | break; |
| 2360 | } |
| 2361 | case MSG_CHANNEL_ERROR: { |
| 2362 | const DataChannelErrorMessageData* data = |
| 2363 | static_cast<DataChannelErrorMessageData*>(pmsg->pdata); |
| 2364 | SignalMediaError(this, data->ssrc, data->error); |
| 2365 | delete data; |
| 2366 | break; |
| 2367 | } |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2368 | case MSG_STREAMCLOSEDREMOTELY: { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2369 | rtc::TypedMessageData<uint32>* data = |
| 2370 | static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2371 | SignalStreamClosedRemotely(data->data()); |
| 2372 | delete data; |
| 2373 | break; |
| 2374 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2375 | default: |
| 2376 | BaseChannel::OnMessage(pmsg); |
| 2377 | break; |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | void DataChannel::OnConnectionMonitorUpdate( |
pthatcher@webrtc.org | b4aac13 | 2015-03-13 18:25:21 +0000 | [diff] [blame^] | 2382 | ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2383 | SignalConnectionMonitor(this, infos); |
| 2384 | } |
| 2385 | |
| 2386 | void DataChannel::StartMediaMonitor(int cms) { |
| 2387 | media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2388 | rtc::Thread::Current())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2389 | media_monitor_->SignalUpdate.connect( |
| 2390 | this, &DataChannel::OnMediaMonitorUpdate); |
| 2391 | media_monitor_->Start(cms); |
| 2392 | } |
| 2393 | |
| 2394 | void DataChannel::StopMediaMonitor() { |
| 2395 | if (media_monitor_) { |
| 2396 | media_monitor_->Stop(); |
| 2397 | media_monitor_->SignalUpdate.disconnect(this); |
| 2398 | media_monitor_.reset(); |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | void DataChannel::OnMediaMonitorUpdate( |
| 2403 | DataMediaChannel* media_channel, const DataMediaInfo& info) { |
| 2404 | ASSERT(media_channel == this->media_channel()); |
| 2405 | SignalMediaMonitor(this, info); |
| 2406 | } |
| 2407 | |
| 2408 | void DataChannel::OnDataReceived( |
| 2409 | const ReceiveDataParams& params, const char* data, size_t len) { |
| 2410 | DataReceivedMessageData* msg = new DataReceivedMessageData( |
| 2411 | params, data, len); |
| 2412 | signaling_thread()->Post(this, MSG_DATARECEIVED, msg); |
| 2413 | } |
| 2414 | |
| 2415 | void DataChannel::OnDataChannelError( |
| 2416 | uint32 ssrc, DataMediaChannel::Error err) { |
| 2417 | DataChannelErrorMessageData* data = new DataChannelErrorMessageData( |
| 2418 | ssrc, err); |
| 2419 | signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data); |
| 2420 | } |
| 2421 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 2422 | void DataChannel::OnDataChannelReadyToSend(bool writable) { |
| 2423 | // This is usded for congestion control to indicate that the stream is ready |
| 2424 | // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates |
| 2425 | // that the transport channel is ready. |
| 2426 | signaling_thread()->Post(this, MSG_READYTOSENDDATA, |
| 2427 | new DataChannelReadyToSendMessageData(writable)); |
| 2428 | } |
| 2429 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2430 | void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode, |
| 2431 | SrtpFilter::Error error) { |
| 2432 | switch (error) { |
| 2433 | case SrtpFilter::ERROR_FAIL: |
| 2434 | OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ? |
| 2435 | DataMediaChannel::ERROR_SEND_SRTP_ERROR : |
| 2436 | DataMediaChannel::ERROR_RECV_SRTP_ERROR); |
| 2437 | break; |
| 2438 | case SrtpFilter::ERROR_AUTH: |
| 2439 | OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ? |
| 2440 | DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED : |
| 2441 | DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED); |
| 2442 | break; |
| 2443 | case SrtpFilter::ERROR_REPLAY: |
| 2444 | // Only receving channel should have this error. |
| 2445 | ASSERT(mode == SrtpFilter::UNPROTECT); |
| 2446 | OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY); |
| 2447 | break; |
| 2448 | default: |
| 2449 | break; |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const { |
| 2454 | GetSupportedDataCryptoSuites(ciphers); |
| 2455 | } |
| 2456 | |
| 2457 | bool DataChannel::ShouldSetupDtlsSrtp() const { |
| 2458 | return (data_channel_type_ == DCT_RTP); |
| 2459 | } |
| 2460 | |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2461 | void DataChannel::OnStreamClosedRemotely(uint32 sid) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2462 | rtc::TypedMessageData<uint32>* message = |
| 2463 | new rtc::TypedMessageData<uint32>(sid); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 2464 | signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message); |
| 2465 | } |
| 2466 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2467 | } // namespace cricket |