blob: bc6bd0981d2a4c50dae09bfae74fc5cae96bfe91 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000030#include "talk/base/bind.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031#include "talk/base/buffer.h"
32#include "talk/base/byteorder.h"
33#include "talk/base/common.h"
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000034#include "talk/base/dscp.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035#include "talk/base/logging.h"
36#include "talk/media/base/rtputils.h"
37#include "talk/p2p/base/transportchannel.h"
38#include "talk/session/media/channelmanager.h"
39#include "talk/session/media/mediamessages.h"
40#include "talk/session/media/rtcpmuxfilter.h"
41#include "talk/session/media/typingmonitor.h"
42
43
44namespace cricket {
45
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000046using talk_base::Bind;
47
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000049 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 MSG_SCREENCASTWINDOWEVENT,
51 MSG_RTPPACKET,
52 MSG_RTCPPACKET,
53 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 MSG_FIRSTPACKETRECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057};
58
59// Value specified in RFC 5764.
60static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
61
62static const int kAgcMinus10db = -10;
63
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000064static void SetSessionError(BaseSession* session, BaseSession::Error error,
65 const std::string& error_desc) {
66 session->SetError(error, error_desc);
67}
68
69static void SafeSetError(const std::string& message, std::string* error_desc) {
70 if (error_desc) {
71 *error_desc = message;
72 }
73}
74
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075// TODO(hellner): use the device manager for creation of screen capturers when
76// the cl enabling it has landed.
77class NullScreenCapturerFactory : public VideoChannel::ScreenCapturerFactory {
78 public:
79 VideoCapturer* CreateScreenCapturer(const ScreencastId& window) {
80 return NULL;
81 }
82};
83
84
85VideoChannel::ScreenCapturerFactory* CreateScreenCapturerFactory() {
86 return new NullScreenCapturerFactory();
87}
88
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089struct PacketMessageData : public talk_base::MessageData {
90 talk_base::Buffer packet;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000091 talk_base::DiffServCodePoint dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092};
93
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094struct ScreencastEventMessageData : public talk_base::MessageData {
95 ScreencastEventMessageData(uint32 s, talk_base::WindowEvent we)
96 : ssrc(s),
97 event(we) {
98 }
99 uint32 ssrc;
100 talk_base::WindowEvent event;
101};
102
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103struct VoiceChannelErrorMessageData : public talk_base::MessageData {
104 VoiceChannelErrorMessageData(uint32 in_ssrc,
105 VoiceMediaChannel::Error in_error)
106 : ssrc(in_ssrc),
107 error(in_error) {
108 }
109 uint32 ssrc;
110 VoiceMediaChannel::Error error;
111};
112
113struct VideoChannelErrorMessageData : public talk_base::MessageData {
114 VideoChannelErrorMessageData(uint32 in_ssrc,
115 VideoMediaChannel::Error in_error)
116 : ssrc(in_ssrc),
117 error(in_error) {
118 }
119 uint32 ssrc;
120 VideoMediaChannel::Error error;
121};
122
123struct DataChannelErrorMessageData : public talk_base::MessageData {
124 DataChannelErrorMessageData(uint32 in_ssrc,
125 DataMediaChannel::Error in_error)
126 : ssrc(in_ssrc),
127 error(in_error) {}
128 uint32 ssrc;
129 DataMediaChannel::Error error;
130};
131
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000133struct VideoChannel::ScreencastDetailsData {
134 explicit ScreencastDetailsData(uint32 s)
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000135 : ssrc(s), fps(0), screencast_max_pixels(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 }
137 uint32 ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000138 int fps;
139 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140};
141
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142static const char* PacketType(bool rtcp) {
143 return (!rtcp) ? "RTP" : "RTCP";
144}
145
146static bool ValidPacket(bool rtcp, const talk_base::Buffer* packet) {
147 // Check the packet size. We could check the header too if needed.
148 return (packet &&
149 packet->length() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
150 packet->length() <= kMaxRtpPacketLen);
151}
152
153static bool IsReceiveContentDirection(MediaContentDirection direction) {
154 return direction == MD_SENDRECV || direction == MD_RECVONLY;
155}
156
157static bool IsSendContentDirection(MediaContentDirection direction) {
158 return direction == MD_SENDRECV || direction == MD_SENDONLY;
159}
160
161static const MediaContentDescription* GetContentDescription(
162 const ContentInfo* cinfo) {
163 if (cinfo == NULL)
164 return NULL;
165 return static_cast<const MediaContentDescription*>(cinfo->description);
166}
167
168BaseChannel::BaseChannel(talk_base::Thread* thread,
169 MediaEngineInterface* media_engine,
170 MediaChannel* media_channel, BaseSession* session,
171 const std::string& content_name, bool rtcp)
172 : worker_thread_(thread),
173 media_engine_(media_engine),
174 session_(session),
175 media_channel_(media_channel),
176 content_name_(content_name),
177 rtcp_(rtcp),
178 transport_channel_(NULL),
179 rtcp_transport_channel_(NULL),
180 enabled_(false),
181 writable_(false),
182 rtp_ready_to_send_(false),
183 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 was_ever_writable_(false),
185 local_content_direction_(MD_INACTIVE),
186 remote_content_direction_(MD_INACTIVE),
187 has_received_packet_(false),
188 dtls_keyed_(false),
189 secure_required_(false) {
190 ASSERT(worker_thread_ == talk_base::Thread::Current());
191 LOG(LS_INFO) << "Created channel for " << content_name;
192}
193
194BaseChannel::~BaseChannel() {
195 ASSERT(worker_thread_ == talk_base::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000196 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 StopConnectionMonitor();
198 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000199 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 // We must destroy the media channel before the transport channel, otherwise
201 // the media channel may try to send on the dead transport channel. NULLing
202 // is not an effective strategy since the sends will come on another thread.
203 delete media_channel_;
204 set_rtcp_transport_channel(NULL);
205 if (transport_channel_ != NULL)
206 session_->DestroyChannel(content_name_, transport_channel_->component());
207 LOG(LS_INFO) << "Destroyed channel";
208}
209
210bool BaseChannel::Init(TransportChannel* transport_channel,
211 TransportChannel* rtcp_transport_channel) {
212 if (transport_channel == NULL) {
213 return false;
214 }
215 if (rtcp() && rtcp_transport_channel == NULL) {
216 return false;
217 }
218 transport_channel_ = transport_channel;
219
220 if (!SetDtlsSrtpCiphers(transport_channel_, false)) {
221 return false;
222 }
223
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 transport_channel_->SignalWritableState.connect(
225 this, &BaseChannel::OnWritableState);
226 transport_channel_->SignalReadPacket.connect(
227 this, &BaseChannel::OnChannelRead);
228 transport_channel_->SignalReadyToSend.connect(
229 this, &BaseChannel::OnReadyToSend);
230
231 session_->SignalNewLocalDescription.connect(
232 this, &BaseChannel::OnNewLocalDescription);
233 session_->SignalNewRemoteDescription.connect(
234 this, &BaseChannel::OnNewRemoteDescription);
235
236 set_rtcp_transport_channel(rtcp_transport_channel);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000237 // Both RTP and RTCP channels are set, we can call SetInterface on
238 // media channel and it can set network options.
239 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 return true;
241}
242
wu@webrtc.org78187522013-10-07 23:32:02 +0000243void BaseChannel::Deinit() {
244 media_channel_->SetInterface(NULL);
245}
246
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000248 worker_thread_->Invoke<void>(Bind(
249 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
250 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 return true;
252}
253
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254bool BaseChannel::MuteStream(uint32 ssrc, bool mute) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000255 return InvokeOnWorker(Bind(&BaseChannel::MuteStream_w, this, ssrc, mute));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256}
257
258bool BaseChannel::IsStreamMuted(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000259 return InvokeOnWorker(Bind(&BaseChannel::IsStreamMuted_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260}
261
262bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000263 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264}
265
266bool BaseChannel::RemoveRecvStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000267 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268}
269
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000270bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000271 return InvokeOnWorker(
272 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000273}
274
275bool BaseChannel::RemoveSendStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000276 return InvokeOnWorker(
277 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000278}
279
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000281 ContentAction action,
282 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000283 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
284 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285}
286
287bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000288 ContentAction action,
289 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000290 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
291 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292}
293
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294void BaseChannel::StartConnectionMonitor(int cms) {
295 socket_monitor_.reset(new SocketMonitor(transport_channel_,
296 worker_thread(),
297 talk_base::Thread::Current()));
298 socket_monitor_->SignalUpdate.connect(
299 this, &BaseChannel::OnConnectionMonitorUpdate);
300 socket_monitor_->Start(cms);
301}
302
303void BaseChannel::StopConnectionMonitor() {
304 if (socket_monitor_) {
305 socket_monitor_->Stop();
306 socket_monitor_.reset();
307 }
308}
309
310void BaseChannel::set_rtcp_transport_channel(TransportChannel* channel) {
311 if (rtcp_transport_channel_ != channel) {
312 if (rtcp_transport_channel_) {
313 session_->DestroyChannel(
314 content_name_, rtcp_transport_channel_->component());
315 }
316 rtcp_transport_channel_ = channel;
317 if (rtcp_transport_channel_) {
318 // TODO(juberti): Propagate this error code
319 VERIFY(SetDtlsSrtpCiphers(rtcp_transport_channel_, true));
320 rtcp_transport_channel_->SignalWritableState.connect(
321 this, &BaseChannel::OnWritableState);
322 rtcp_transport_channel_->SignalReadPacket.connect(
323 this, &BaseChannel::OnChannelRead);
324 rtcp_transport_channel_->SignalReadyToSend.connect(
325 this, &BaseChannel::OnReadyToSend);
326 }
327 }
328}
329
330bool BaseChannel::IsReadyToReceive() const {
331 // Receive data if we are enabled and have local content,
332 return enabled() && IsReceiveContentDirection(local_content_direction_);
333}
334
335bool BaseChannel::IsReadyToSend() const {
336 // Send outgoing data if we are enabled, have local and remote content,
337 // and we have had some form of connectivity.
338 return enabled() &&
339 IsReceiveContentDirection(remote_content_direction_) &&
340 IsSendContentDirection(local_content_direction_) &&
341 was_ever_writable();
342}
343
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000344bool BaseChannel::SendPacket(talk_base::Buffer* packet,
345 talk_base::DiffServCodePoint dscp) {
346 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347}
348
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000349bool BaseChannel::SendRtcp(talk_base::Buffer* packet,
350 talk_base::DiffServCodePoint dscp) {
351 return SendPacket(true, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352}
353
354int BaseChannel::SetOption(SocketType type, talk_base::Socket::Option opt,
355 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000356 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000358 case ST_RTP:
359 channel = transport_channel_;
360 break;
361 case ST_RTCP:
362 channel = rtcp_transport_channel_;
363 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000365 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366}
367
368void BaseChannel::OnWritableState(TransportChannel* channel) {
369 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
370 if (transport_channel_->writable()
371 && (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
372 ChannelWritable_w();
373 } else {
374 ChannelNotWritable_w();
375 }
376}
377
378void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000379 const char* data, size_t len,
380 const talk_base::PacketTime& packet_time,
381 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
383 ASSERT(worker_thread_ == talk_base::Thread::Current());
384
385 // When using RTCP multiplexing we might get RTCP packets on the RTP
386 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
387 bool rtcp = PacketIsRtcp(channel, data, len);
388 talk_base::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000389 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390}
391
392void BaseChannel::OnReadyToSend(TransportChannel* channel) {
393 SetReadyToSend(channel, true);
394}
395
396void BaseChannel::SetReadyToSend(TransportChannel* channel, bool ready) {
397 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
398 if (channel == transport_channel_) {
399 rtp_ready_to_send_ = ready;
400 }
401 if (channel == rtcp_transport_channel_) {
402 rtcp_ready_to_send_ = ready;
403 }
404
405 if (!ready) {
406 // Notify the MediaChannel when either rtp or rtcp channel can't send.
407 media_channel_->OnReadyToSend(false);
408 } else if (rtp_ready_to_send_ &&
409 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
410 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
411 // Notify the MediaChannel when both rtp and rtcp channel can send.
412 media_channel_->OnReadyToSend(true);
413 }
414}
415
416bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
417 const char* data, size_t len) {
418 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000419 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420}
421
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000422bool BaseChannel::SendPacket(bool rtcp, talk_base::Buffer* packet,
423 talk_base::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 // SendPacket gets called from MediaEngine, typically on an encoder thread.
425 // If the thread is not our worker thread, we will post to our worker
426 // so that the real work happens on our worker. This avoids us having to
427 // synchronize access to all the pieces of the send path, including
428 // SRTP and the inner workings of the transport channels.
429 // The only downside is that we can't return a proper failure code if
430 // needed. Since UDP is unreliable anyway, this should be a non-issue.
431 if (talk_base::Thread::Current() != worker_thread_) {
432 // Avoid a copy by transferring the ownership of the packet data.
433 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
434 PacketMessageData* data = new PacketMessageData;
435 packet->TransferTo(&data->packet);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000436 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 worker_thread_->Post(this, message_id, data);
438 return true;
439 }
440
441 // Now that we are on the correct thread, ensure we have a place to send this
442 // packet before doing anything. (We might get RTCP packets that we don't
443 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
444 // transport.
445 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
446 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000447 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 return false;
449 }
450
451 // Protect ourselves against crazy data.
452 if (!ValidPacket(rtcp, packet)) {
453 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
454 << PacketType(rtcp) << " packet: wrong size="
455 << packet->length();
456 return false;
457 }
458
459 // Signal to the media sink before protecting the packet.
460 {
461 talk_base::CritScope cs(&signal_send_packet_cs_);
462 SignalSendPacketPreCrypto(packet->data(), packet->length(), rtcp);
463 }
464
465 // Protect if needed.
466 if (srtp_filter_.IsActive()) {
467 bool res;
468 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000469 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 if (!rtcp) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000471 res = srtp_filter_.ProtectRtp(data, len,
472 static_cast<int>(packet->capacity()), &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 if (!res) {
474 int seq_num = -1;
475 uint32 ssrc = 0;
476 GetRtpSeqNum(data, len, &seq_num);
477 GetRtpSsrc(data, len, &ssrc);
478 LOG(LS_ERROR) << "Failed to protect " << content_name_
479 << " RTP packet: size=" << len
480 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
481 return false;
482 }
483 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000484 res = srtp_filter_.ProtectRtcp(data, len,
485 static_cast<int>(packet->capacity()),
486 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 if (!res) {
488 int type = -1;
489 GetRtcpType(data, len, &type);
490 LOG(LS_ERROR) << "Failed to protect " << content_name_
491 << " RTCP packet: size=" << len << ", type=" << type;
492 return false;
493 }
494 }
495
496 // Update the length of the packet now that we've added the auth tag.
497 packet->SetLength(len);
498 } else if (secure_required_) {
499 // This is a double check for something that supposedly can't happen.
500 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
501 << " packet when SRTP is inactive and crypto is required";
502
503 ASSERT(false);
504 return false;
505 }
506
507 // Signal to the media sink after protecting the packet.
508 {
509 talk_base::CritScope cs(&signal_send_packet_cs_);
510 SignalSendPacketPostCrypto(packet->data(), packet->length(), rtcp);
511 }
512
513 // Bon voyage.
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000514 int ret = channel->SendPacket(packet->data(), packet->length(), dscp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
516 if (ret != static_cast<int>(packet->length())) {
517 if (channel->GetError() == EWOULDBLOCK) {
518 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
519 SetReadyToSend(channel, false);
520 }
521 return false;
522 }
523 return true;
524}
525
526bool BaseChannel::WantsPacket(bool rtcp, talk_base::Buffer* packet) {
527 // Protect ourselves against crazy data.
528 if (!ValidPacket(rtcp, packet)) {
529 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
530 << PacketType(rtcp) << " packet: wrong size="
531 << packet->length();
532 return false;
533 }
534 // If this channel is suppose to handle RTP data, that is determined by
535 // checking against ssrc filter. This is necessary to do it here to avoid
536 // double decryption.
537 if (ssrc_filter_.IsActive() &&
538 !ssrc_filter_.DemuxPacket(packet->data(), packet->length(), rtcp)) {
539 return false;
540 }
541
542 return true;
543}
544
wu@webrtc.orga9890802013-12-13 00:21:03 +0000545void BaseChannel::HandlePacket(bool rtcp, talk_base::Buffer* packet,
546 const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 if (!WantsPacket(rtcp, packet)) {
548 return;
549 }
550
551 if (!has_received_packet_) {
552 has_received_packet_ = true;
553 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
554 }
555
556 // Signal to the media sink before unprotecting the packet.
557 {
558 talk_base::CritScope cs(&signal_recv_packet_cs_);
559 SignalRecvPacketPostCrypto(packet->data(), packet->length(), rtcp);
560 }
561
562 // Unprotect the packet, if needed.
563 if (srtp_filter_.IsActive()) {
564 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000565 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 bool res;
567 if (!rtcp) {
568 res = srtp_filter_.UnprotectRtp(data, len, &len);
569 if (!res) {
570 int seq_num = -1;
571 uint32 ssrc = 0;
572 GetRtpSeqNum(data, len, &seq_num);
573 GetRtpSsrc(data, len, &ssrc);
574 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
575 << " RTP packet: size=" << len
576 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
577 return;
578 }
579 } else {
580 res = srtp_filter_.UnprotectRtcp(data, len, &len);
581 if (!res) {
582 int type = -1;
583 GetRtcpType(data, len, &type);
584 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
585 << " RTCP packet: size=" << len << ", type=" << type;
586 return;
587 }
588 }
589
590 packet->SetLength(len);
591 } else if (secure_required_) {
592 // Our session description indicates that SRTP is required, but we got a
593 // packet before our SRTP filter is active. This means either that
594 // a) we got SRTP packets before we received the SDES keys, in which case
595 // we can't decrypt it anyway, or
596 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
597 // channels, so we haven't yet extracted keys, even if DTLS did complete
598 // on the channel that the packets are being sent on. It's really good
599 // practice to wait for both RTP and RTCP to be good to go before sending
600 // media, to prevent weird failure modes, so it's fine for us to just eat
601 // packets here. This is all sidestepped if RTCP mux is used anyway.
602 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
603 << " packet when SRTP is inactive and crypto is required";
604 return;
605 }
606
607 // Signal to the media sink after unprotecting the packet.
608 {
609 talk_base::CritScope cs(&signal_recv_packet_cs_);
610 SignalRecvPacketPreCrypto(packet->data(), packet->length(), rtcp);
611 }
612
613 // Push it down to the media channel.
614 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000615 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000617 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 }
619}
620
621void BaseChannel::OnNewLocalDescription(
622 BaseSession* session, ContentAction action) {
623 const ContentInfo* content_info =
624 GetFirstContent(session->local_description());
625 const MediaContentDescription* content_desc =
626 GetContentDescription(content_info);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000627 std::string error_desc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 if (content_desc && content_info && !content_info->rejected &&
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000629 !SetLocalContent(content_desc, action, &error_desc)) {
630 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 }
633}
634
635void BaseChannel::OnNewRemoteDescription(
636 BaseSession* session, ContentAction action) {
637 const ContentInfo* content_info =
638 GetFirstContent(session->remote_description());
639 const MediaContentDescription* content_desc =
640 GetContentDescription(content_info);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000641 std::string error_desc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 if (content_desc && content_info && !content_info->rejected &&
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000643 !SetRemoteContent(content_desc, action, &error_desc)) {
644 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
645 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 }
647}
648
649void BaseChannel::EnableMedia_w() {
650 ASSERT(worker_thread_ == talk_base::Thread::Current());
651 if (enabled_)
652 return;
653
654 LOG(LS_INFO) << "Channel enabled";
655 enabled_ = true;
656 ChangeState();
657}
658
659void BaseChannel::DisableMedia_w() {
660 ASSERT(worker_thread_ == talk_base::Thread::Current());
661 if (!enabled_)
662 return;
663
664 LOG(LS_INFO) << "Channel disabled";
665 enabled_ = false;
666 ChangeState();
667}
668
669bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
670 ASSERT(worker_thread_ == talk_base::Thread::Current());
671 bool ret = media_channel()->MuteStream(ssrc, mute);
672 if (ret) {
673 if (mute)
674 muted_streams_.insert(ssrc);
675 else
676 muted_streams_.erase(ssrc);
677 }
678 return ret;
679}
680
681bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
682 ASSERT(worker_thread_ == talk_base::Thread::Current());
683 return muted_streams_.find(ssrc) != muted_streams_.end();
684}
685
686void BaseChannel::ChannelWritable_w() {
687 ASSERT(worker_thread_ == talk_base::Thread::Current());
688 if (writable_)
689 return;
690
691 LOG(LS_INFO) << "Channel socket writable ("
692 << transport_channel_->content_name() << ", "
693 << transport_channel_->component() << ")"
694 << (was_ever_writable_ ? "" : " for the first time");
695
696 std::vector<ConnectionInfo> infos;
697 transport_channel_->GetStats(&infos);
698 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
699 it != infos.end(); ++it) {
700 if (it->best_connection) {
701 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
702 << "->" << it->remote_candidate.ToSensitiveString();
703 break;
704 }
705 }
706
707 // If we're doing DTLS-SRTP, now is the time.
708 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
709 if (!SetupDtlsSrtp(false)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000710 const std::string error_desc =
711 "Couldn't set up DTLS-SRTP on RTP channel.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 // Sent synchronously.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000713 signaling_thread()->Invoke<void>(Bind(
714 &SetSessionError,
715 session_,
716 BaseSession::ERROR_TRANSPORT,
717 error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 return;
719 }
720
721 if (rtcp_transport_channel_) {
722 if (!SetupDtlsSrtp(true)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000723 const std::string error_desc =
724 "Couldn't set up DTLS-SRTP on RTCP channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 // Sent synchronously.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000726 signaling_thread()->Invoke<void>(Bind(
727 &SetSessionError,
728 session_,
729 BaseSession::ERROR_TRANSPORT,
730 error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 return;
732 }
733 }
734 }
735
736 was_ever_writable_ = true;
737 writable_ = true;
738 ChangeState();
739}
740
741bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
742 std::vector<std::string> ciphers;
743 // We always use the default SRTP ciphers for RTCP, but we may use different
744 // ciphers for RTP depending on the media type.
745 if (!rtcp) {
746 GetSrtpCiphers(&ciphers);
747 } else {
748 GetSupportedDefaultCryptoSuites(&ciphers);
749 }
750 return tc->SetSrtpCiphers(ciphers);
751}
752
753bool BaseChannel::ShouldSetupDtlsSrtp() const {
754 return true;
755}
756
757// This function returns true if either DTLS-SRTP is not in use
758// *or* DTLS-SRTP is successfully set up.
759bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
760 bool ret = false;
761
762 TransportChannel *channel = rtcp_channel ?
763 rtcp_transport_channel_ : transport_channel_;
764
765 // No DTLS
766 if (!channel->IsDtlsActive())
767 return true;
768
769 std::string selected_cipher;
770
771 if (!channel->GetSrtpCipher(&selected_cipher)) {
772 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
773 return false;
774 }
775
776 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
777 << content_name() << " "
778 << PacketType(rtcp_channel);
779
780 // OK, we're now doing DTLS (RFC 5764)
781 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
782 SRTP_MASTER_KEY_SALT_LEN * 2);
783
784 // RFC 5705 exporter using the RFC 5764 parameters
785 if (!channel->ExportKeyingMaterial(
786 kDtlsSrtpExporterLabel,
787 NULL, 0, false,
788 &dtls_buffer[0], dtls_buffer.size())) {
789 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
790 ASSERT(false); // This should never happen
791 return false;
792 }
793
794 // Sync up the keys with the DTLS-SRTP interface
795 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
796 SRTP_MASTER_KEY_SALT_LEN);
797 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
798 SRTP_MASTER_KEY_SALT_LEN);
799 size_t offset = 0;
800 memcpy(&client_write_key[0], &dtls_buffer[offset],
801 SRTP_MASTER_KEY_KEY_LEN);
802 offset += SRTP_MASTER_KEY_KEY_LEN;
803 memcpy(&server_write_key[0], &dtls_buffer[offset],
804 SRTP_MASTER_KEY_KEY_LEN);
805 offset += SRTP_MASTER_KEY_KEY_LEN;
806 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
807 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
808 offset += SRTP_MASTER_KEY_SALT_LEN;
809 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
810 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
811
812 std::vector<unsigned char> *send_key, *recv_key;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000813 talk_base::SSLRole role;
814 if (!channel->GetSslRole(&role)) {
815 LOG(LS_WARNING) << "GetSslRole failed";
816 return false;
817 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000819 if (role == talk_base::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 send_key = &server_write_key;
821 recv_key = &client_write_key;
822 } else {
823 send_key = &client_write_key;
824 recv_key = &server_write_key;
825 }
826
827 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000828 ret = srtp_filter_.SetRtcpParams(
829 selected_cipher,
830 &(*send_key)[0],
831 static_cast<int>(send_key->size()),
832 selected_cipher,
833 &(*recv_key)[0],
834 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000836 ret = srtp_filter_.SetRtpParams(
837 selected_cipher,
838 &(*send_key)[0],
839 static_cast<int>(send_key->size()),
840 selected_cipher,
841 &(*recv_key)[0],
842 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 }
844
845 if (!ret)
846 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
847 else
848 dtls_keyed_ = true;
849
850 return ret;
851}
852
853void BaseChannel::ChannelNotWritable_w() {
854 ASSERT(worker_thread_ == talk_base::Thread::Current());
855 if (!writable_)
856 return;
857
858 LOG(LS_INFO) << "Channel socket not writable ("
859 << transport_channel_->content_name() << ", "
860 << transport_channel_->component() << ")";
861 writable_ = false;
862 ChangeState();
863}
864
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000865// |dtls| will be set to true if DTLS is active for transport channel and
866// crypto is empty.
867bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000868 bool* dtls,
869 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000870 *dtls = transport_channel_->IsDtlsActive();
871 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000872 SafeSetError("Cryptos must be empty when DTLS is active.",
873 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000874 return false;
875 }
876 return true;
877}
878
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000880 ContentAction action,
881 ContentSource src,
882 std::string* error_desc) {
883 if (action == CA_UPDATE) {
884 // no crypto params.
885 return true;
886 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000888 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000889 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
890 if (!ret) {
891 return false;
892 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 switch (action) {
894 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000895 // If DTLS is already active on the channel, we could be renegotiating
896 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000897 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000898 ret = srtp_filter_.SetOffer(cryptos, src);
899 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 break;
901 case CA_PRANSWER:
902 // If we're doing DTLS-SRTP, we don't want to update the filter
903 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000904 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
906 }
907 break;
908 case CA_ANSWER:
909 // If we're doing DTLS-SRTP, we don't want to update the filter
910 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000911 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 ret = srtp_filter_.SetAnswer(cryptos, src);
913 }
914 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 default:
916 break;
917 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000918 if (!ret) {
919 SafeSetError("Failed to setup SRTP filter.", error_desc);
920 return false;
921 }
922 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923}
924
925bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000926 ContentSource src,
927 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 bool ret = false;
929 switch (action) {
930 case CA_OFFER:
931 ret = rtcp_mux_filter_.SetOffer(enable, src);
932 break;
933 case CA_PRANSWER:
934 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
935 break;
936 case CA_ANSWER:
937 ret = rtcp_mux_filter_.SetAnswer(enable, src);
938 if (ret && rtcp_mux_filter_.IsActive()) {
939 // We activated RTCP mux, close down the RTCP transport.
940 set_rtcp_transport_channel(NULL);
941 }
942 break;
943 case CA_UPDATE:
944 // No RTCP mux info.
945 ret = true;
946 default:
947 break;
948 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000949 if (!ret) {
950 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
951 return false;
952 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
954 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
955 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000956 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 // If the RTP transport is already writable, then so are we.
958 if (transport_channel_->writable()) {
959 ChannelWritable_w();
960 }
961 }
962
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000963 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964}
965
966bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
967 ASSERT(worker_thread() == talk_base::Thread::Current());
968 if (!media_channel()->AddRecvStream(sp))
969 return false;
970
971 return ssrc_filter_.AddStream(sp);
972}
973
974bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
975 ASSERT(worker_thread() == talk_base::Thread::Current());
976 ssrc_filter_.RemoveStream(ssrc);
977 return media_channel()->RemoveRecvStream(ssrc);
978}
979
980bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000981 ContentAction action,
982 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
984 action == CA_PRANSWER || action == CA_UPDATE))
985 return false;
986
987 // If this is an update, streams only contain streams that have changed.
988 if (action == CA_UPDATE) {
989 for (StreamParamsVec::const_iterator it = streams.begin();
990 it != streams.end(); ++it) {
991 StreamParams existing_stream;
992 bool stream_exist = GetStreamByIds(local_streams_, it->groupid,
993 it->id, &existing_stream);
994 if (!stream_exist && it->has_ssrcs()) {
995 if (media_channel()->AddSendStream(*it)) {
996 local_streams_.push_back(*it);
997 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
998 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000999 std::ostringstream desc;
1000 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1001 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 return false;
1003 }
1004 } else if (stream_exist && !it->has_ssrcs()) {
1005 if (!media_channel()->RemoveSendStream(existing_stream.first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001006 std::ostringstream desc;
1007 desc << "Failed to remove send stream with ssrc "
1008 << it->first_ssrc() << ".";
1009 SafeSetError(desc.str(), error_desc);
1010 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 }
1012 RemoveStreamBySsrc(&local_streams_, existing_stream.first_ssrc());
1013 } else {
1014 LOG(LS_WARNING) << "Ignore unsupported stream update";
1015 }
1016 }
1017 return true;
1018 }
1019 // Else streams are all the streams we want to send.
1020
1021 // Check for streams that have been removed.
1022 bool ret = true;
1023 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1024 it != local_streams_.end(); ++it) {
1025 if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1026 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001027 std::ostringstream desc;
1028 desc << "Failed to remove send stream with ssrc "
1029 << it->first_ssrc() << ".";
1030 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 ret = false;
1032 }
1033 }
1034 }
1035 // Check for new streams.
1036 for (StreamParamsVec::const_iterator it = streams.begin();
1037 it != streams.end(); ++it) {
1038 if (!GetStreamBySsrc(local_streams_, it->first_ssrc(), NULL)) {
1039 if (media_channel()->AddSendStream(*it)) {
1040 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1041 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001042 std::ostringstream desc;
1043 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1044 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 ret = false;
1046 }
1047 }
1048 }
1049 local_streams_ = streams;
1050 return ret;
1051}
1052
1053bool BaseChannel::UpdateRemoteStreams_w(
1054 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001055 ContentAction action,
1056 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1058 action == CA_PRANSWER || action == CA_UPDATE))
1059 return false;
1060
1061 // If this is an update, streams only contain streams that have changed.
1062 if (action == CA_UPDATE) {
1063 for (StreamParamsVec::const_iterator it = streams.begin();
1064 it != streams.end(); ++it) {
1065 StreamParams existing_stream;
1066 bool stream_exists = GetStreamByIds(remote_streams_, it->groupid,
1067 it->id, &existing_stream);
1068 if (!stream_exists && it->has_ssrcs()) {
1069 if (AddRecvStream_w(*it)) {
1070 remote_streams_.push_back(*it);
1071 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1072 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001073 std::ostringstream desc;
1074 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1075 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 return false;
1077 }
1078 } else if (stream_exists && !it->has_ssrcs()) {
1079 if (!RemoveRecvStream_w(existing_stream.first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001080 std::ostringstream desc;
1081 desc << "Failed to remove remote stream with ssrc "
1082 << it->first_ssrc() << ".";
1083 SafeSetError(desc.str(), error_desc);
1084 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085 }
1086 RemoveStreamBySsrc(&remote_streams_, existing_stream.first_ssrc());
1087 } else {
1088 LOG(LS_WARNING) << "Ignore unsupported stream update."
1089 << " Stream exists? " << stream_exists
1090 << " existing stream = " << existing_stream.ToString()
1091 << " new stream = " << it->ToString();
1092 }
1093 }
1094 return true;
1095 }
1096 // Else streams are all the streams we want to receive.
1097
1098 // Check for streams that have been removed.
1099 bool ret = true;
1100 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1101 it != remote_streams_.end(); ++it) {
1102 if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1103 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001104 std::ostringstream desc;
1105 desc << "Failed to remove remote stream with ssrc "
1106 << it->first_ssrc() << ".";
1107 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 ret = false;
1109 }
1110 }
1111 }
1112 // Check for new streams.
1113 for (StreamParamsVec::const_iterator it = streams.begin();
1114 it != streams.end(); ++it) {
1115 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc(), NULL)) {
1116 if (AddRecvStream_w(*it)) {
1117 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1118 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001119 std::ostringstream desc;
1120 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1121 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 ret = false;
1123 }
1124 }
1125 }
1126 remote_streams_ = streams;
1127 return ret;
1128}
1129
1130bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001131 ContentAction action,
1132 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 // Cache secure_required_ for belt and suspenders check on SendPacket
1134 secure_required_ = content->crypto_required();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001135 bool ret = UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001137 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001139 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 // Set local RTP header extensions.
1141 if (content->rtp_header_extensions_set()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001142 if (!media_channel()->SetRecvRtpHeaderExtensions(
1143 content->rtp_header_extensions())) {
1144 std::ostringstream desc;
1145 desc << "Failed to set receive rtp header extensions for "
1146 << MediaTypeToString(content->type()) << " content.";
1147 SafeSetError(desc.str(), error_desc);
1148 ret = false;
1149 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 }
1151 set_local_content_direction(content->direction());
1152 return ret;
1153}
1154
1155bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001156 ContentAction action,
1157 std::string* error_desc) {
1158 bool ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001160 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001162 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 // Set remote RTP header extensions.
1164 if (content->rtp_header_extensions_set()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001165 if (!media_channel()->SetSendRtpHeaderExtensions(
1166 content->rtp_header_extensions())) {
1167 std::ostringstream desc;
1168 desc << "Failed to set send rtp header extensions for "
1169 << MediaTypeToString(content->type()) << " content.";
1170 SafeSetError(desc.str(), error_desc);
1171 ret = false;
1172 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001174
1175 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1176 std::ostringstream desc;
1177 desc << "Failed to set max send bandwidth for "
1178 << MediaTypeToString(content->type()) << " content.";
1179 SafeSetError(desc.str(), error_desc);
1180 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 }
1182 set_remote_content_direction(content->direction());
1183 return ret;
1184}
1185
1186void BaseChannel::OnMessage(talk_base::Message *pmsg) {
1187 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 case MSG_RTPPACKET:
1189 case MSG_RTCPPACKET: {
1190 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001191 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 delete data; // because it is Posted
1193 break;
1194 }
1195 case MSG_FIRSTPACKETRECEIVED: {
1196 SignalFirstPacketReceived(this);
1197 break;
1198 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 }
1200}
1201
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202void BaseChannel::FlushRtcpMessages() {
1203 // Flush all remaining RTCP messages. This should only be called in
1204 // destructor.
1205 ASSERT(talk_base::Thread::Current() == worker_thread_);
1206 talk_base::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001207 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 for (talk_base::MessageList::iterator it = rtcp_messages.begin();
1209 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001210 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 }
1212}
1213
1214VoiceChannel::VoiceChannel(talk_base::Thread* thread,
1215 MediaEngineInterface* media_engine,
1216 VoiceMediaChannel* media_channel,
1217 BaseSession* session,
1218 const std::string& content_name,
1219 bool rtcp)
1220 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1221 rtcp),
1222 received_media_(false) {
1223}
1224
1225VoiceChannel::~VoiceChannel() {
1226 StopAudioMonitor();
1227 StopMediaMonitor();
1228 // this can't be done in the base class, since it calls a virtual
1229 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001230 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231}
1232
1233bool VoiceChannel::Init() {
1234 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1235 content_name(), "rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1236 if (!BaseChannel::Init(session()->CreateChannel(
1237 content_name(), "rtp", ICE_CANDIDATE_COMPONENT_RTP),
1238 rtcp_channel)) {
1239 return false;
1240 }
1241 media_channel()->SignalMediaError.connect(
1242 this, &VoiceChannel::OnVoiceChannelError);
1243 srtp_filter()->SignalSrtpError.connect(
1244 this, &VoiceChannel::OnSrtpError);
1245 return true;
1246}
1247
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001248bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001249 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1250 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001251}
1252
1253bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001254 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1255 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256}
1257
1258bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001259 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260}
1261
1262// TODO(juberti): Handle early media the right way. We should get an explicit
1263// ringing message telling us to start playing local ringback, which we cancel
1264// if any early media actually arrives. For now, we do the opposite, which is
1265// to wait 1 second for early media, and start playing local ringback if none
1266// arrives.
1267void VoiceChannel::SetEarlyMedia(bool enable) {
1268 if (enable) {
1269 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001270 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1271 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 } else {
1273 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001274 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 }
1276}
1277
1278bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001279 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1280 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281}
1282
1283bool VoiceChannel::PressDTMF(int digit, bool playout) {
1284 int flags = DF_SEND;
1285 if (playout) {
1286 flags |= DF_PLAY;
1287 }
1288 int duration_ms = 160;
1289 return InsertDtmf(0, digit, duration_ms, flags);
1290}
1291
1292bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001293 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1294 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295}
1296
1297bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1298 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001299 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1300 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301}
1302
1303bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001304 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1305 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001307
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001309 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1310 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311}
1312
1313void VoiceChannel::StartMediaMonitor(int cms) {
1314 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
1315 talk_base::Thread::Current()));
1316 media_monitor_->SignalUpdate.connect(
1317 this, &VoiceChannel::OnMediaMonitorUpdate);
1318 media_monitor_->Start(cms);
1319}
1320
1321void VoiceChannel::StopMediaMonitor() {
1322 if (media_monitor_) {
1323 media_monitor_->Stop();
1324 media_monitor_->SignalUpdate.disconnect(this);
1325 media_monitor_.reset();
1326 }
1327}
1328
1329void VoiceChannel::StartAudioMonitor(int cms) {
1330 audio_monitor_.reset(new AudioMonitor(this, talk_base::Thread::Current()));
1331 audio_monitor_
1332 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1333 audio_monitor_->Start(cms);
1334}
1335
1336void VoiceChannel::StopAudioMonitor() {
1337 if (audio_monitor_) {
1338 audio_monitor_->Stop();
1339 audio_monitor_.reset();
1340 }
1341}
1342
1343bool VoiceChannel::IsAudioMonitorRunning() const {
1344 return (audio_monitor_.get() != NULL);
1345}
1346
1347void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1348 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1349 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1350}
1351
1352void VoiceChannel::StopTypingMonitor() {
1353 typing_monitor_.reset();
1354}
1355
1356bool VoiceChannel::IsTypingMonitorRunning() const {
1357 return typing_monitor_;
1358}
1359
1360bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1361 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1362 if (typing_monitor_ && mute)
1363 typing_monitor_->OnChannelMuted();
1364 return ret;
1365}
1366
1367int VoiceChannel::GetInputLevel_w() {
1368 return media_engine()->GetInputLevel();
1369}
1370
1371int VoiceChannel::GetOutputLevel_w() {
1372 return media_channel()->GetOutputLevel();
1373}
1374
1375void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1376 media_channel()->GetActiveStreams(actives);
1377}
1378
1379void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001380 const char* data, size_t len,
1381 const talk_base::PacketTime& packet_time,
1382 int flags) {
1383 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384
1385 // Set a flag when we've received an RTP packet. If we're waiting for early
1386 // media, this will disable the timeout.
1387 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1388 received_media_ = true;
1389 }
1390}
1391
1392void VoiceChannel::ChangeState() {
1393 // Render incoming data if we're the active call, and we have the local
1394 // content. We receive data on the default channel and multiplexed streams.
1395 bool recv = IsReadyToReceive();
1396 if (!media_channel()->SetPlayout(recv)) {
1397 SendLastMediaError();
1398 }
1399
1400 // Send outgoing data if we're the active call, we have the remote content,
1401 // and we have had some form of connectivity.
1402 bool send = IsReadyToSend();
1403 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1404 if (!media_channel()->SetSend(send_flag)) {
1405 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1406 SendLastMediaError();
1407 }
1408
1409 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1410}
1411
1412const ContentInfo* VoiceChannel::GetFirstContent(
1413 const SessionDescription* sdesc) {
1414 return GetFirstAudioContent(sdesc);
1415}
1416
1417bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001418 ContentAction action,
1419 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001420 ASSERT(worker_thread() == talk_base::Thread::Current());
1421 LOG(LS_INFO) << "Setting local voice description";
1422
1423 const AudioContentDescription* audio =
1424 static_cast<const AudioContentDescription*>(content);
1425 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001426 if (!audio) {
1427 SafeSetError("Can't find audio content in local description.", error_desc);
1428 return false;
1429 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001431 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432 // Set local audio codecs (what we want to receive).
1433 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1434 // is set properly.
1435 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001436 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1437 SafeSetError("Failed to set audio receive codecs.", error_desc);
1438 ret = false;
1439 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440 }
1441
1442 // If everything worked, see if we can start receiving.
1443 if (ret) {
1444 ChangeState();
1445 } else {
1446 LOG(LS_WARNING) << "Failed to set local voice description";
1447 }
1448 return ret;
1449}
1450
1451bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001452 ContentAction action,
1453 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454 ASSERT(worker_thread() == talk_base::Thread::Current());
1455 LOG(LS_INFO) << "Setting remote voice description";
1456
1457 const AudioContentDescription* audio =
1458 static_cast<const AudioContentDescription*>(content);
1459 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001460 if (!audio) {
1461 SafeSetError("Can't find audio content in remote description.", error_desc);
1462 return false;
1463 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464
1465 bool ret = true;
1466 // Set remote video codecs (what the other side wants to receive).
1467 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001468 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1469 SafeSetError("Failed to set audio send codecs.", error_desc);
1470 ret = false;
1471 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 }
1473
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001474 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475
1476 if (action != CA_UPDATE) {
1477 // Tweak our audio processing settings, if needed.
1478 AudioOptions audio_options;
1479 if (!media_channel()->GetOptions(&audio_options)) {
1480 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1481 } else {
1482 if (audio->conference_mode()) {
1483 audio_options.conference_mode.Set(true);
1484 }
1485 if (audio->agc_minus_10db()) {
1486 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1487 }
1488 if (!media_channel()->SetOptions(audio_options)) {
1489 // Log an error on failure, but don't abort the call.
1490 LOG(LS_ERROR) << "Failed to set voice channel options";
1491 }
1492 }
1493 }
1494
1495 // If everything worked, see if we can start sending.
1496 if (ret) {
1497 ChangeState();
1498 } else {
1499 LOG(LS_WARNING) << "Failed to set remote voice description";
1500 }
1501 return ret;
1502}
1503
1504bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
1505 ASSERT(worker_thread() == talk_base::Thread::Current());
1506 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1507}
1508
1509bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
1510 ASSERT(worker_thread() == talk_base::Thread::Current());
1511 if (play) {
1512 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1513 } else {
1514 LOG(LS_INFO) << "Stopping ringback tone";
1515 }
1516 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1517}
1518
1519void VoiceChannel::HandleEarlyMediaTimeout() {
1520 // This occurs on the main thread, not the worker thread.
1521 if (!received_media_) {
1522 LOG(LS_INFO) << "No early media received before timeout";
1523 SignalEarlyMediaTimeout(this);
1524 }
1525}
1526
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1528 int flags) {
1529 if (!enabled()) {
1530 return false;
1531 }
1532
1533 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1534}
1535
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001537 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1538 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539}
1540
1541void VoiceChannel::OnMessage(talk_base::Message *pmsg) {
1542 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 case MSG_EARLYMEDIATIMEOUT:
1544 HandleEarlyMediaTimeout();
1545 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546 case MSG_CHANNEL_ERROR: {
1547 VoiceChannelErrorMessageData* data =
1548 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1549 SignalMediaError(this, data->ssrc, data->error);
1550 delete data;
1551 break;
1552 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553 default:
1554 BaseChannel::OnMessage(pmsg);
1555 break;
1556 }
1557}
1558
1559void VoiceChannel::OnConnectionMonitorUpdate(
1560 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
1561 SignalConnectionMonitor(this, infos);
1562}
1563
1564void VoiceChannel::OnMediaMonitorUpdate(
1565 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1566 ASSERT(media_channel == this->media_channel());
1567 SignalMediaMonitor(this, info);
1568}
1569
1570void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1571 const AudioInfo& info) {
1572 SignalAudioMonitor(this, info);
1573}
1574
1575void VoiceChannel::OnVoiceChannelError(
1576 uint32 ssrc, VoiceMediaChannel::Error err) {
1577 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1578 ssrc, err);
1579 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1580}
1581
1582void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1583 SrtpFilter::Error error) {
1584 switch (error) {
1585 case SrtpFilter::ERROR_FAIL:
1586 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1587 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1588 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1589 break;
1590 case SrtpFilter::ERROR_AUTH:
1591 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1592 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1593 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1594 break;
1595 case SrtpFilter::ERROR_REPLAY:
1596 // Only receving channel should have this error.
1597 ASSERT(mode == SrtpFilter::UNPROTECT);
1598 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1599 break;
1600 default:
1601 break;
1602 }
1603}
1604
1605void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1606 GetSupportedAudioCryptoSuites(ciphers);
1607}
1608
1609VideoChannel::VideoChannel(talk_base::Thread* thread,
1610 MediaEngineInterface* media_engine,
1611 VideoMediaChannel* media_channel,
1612 BaseSession* session,
1613 const std::string& content_name,
1614 bool rtcp,
1615 VoiceChannel* voice_channel)
1616 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1617 rtcp),
1618 voice_channel_(voice_channel),
1619 renderer_(NULL),
1620 screencapture_factory_(CreateScreenCapturerFactory()),
1621 previous_we_(talk_base::WE_CLOSE) {
1622}
1623
1624bool VideoChannel::Init() {
1625 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1626 content_name(), "video_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1627 if (!BaseChannel::Init(session()->CreateChannel(
1628 content_name(), "video_rtp", ICE_CANDIDATE_COMPONENT_RTP),
1629 rtcp_channel)) {
1630 return false;
1631 }
1632 media_channel()->SignalMediaError.connect(
1633 this, &VideoChannel::OnVideoChannelError);
1634 srtp_filter()->SignalSrtpError.connect(
1635 this, &VideoChannel::OnSrtpError);
1636 return true;
1637}
1638
1639void VoiceChannel::SendLastMediaError() {
1640 uint32 ssrc;
1641 VoiceMediaChannel::Error error;
1642 media_channel()->GetLastMediaError(&ssrc, &error);
1643 SignalMediaError(this, ssrc, error);
1644}
1645
1646VideoChannel::~VideoChannel() {
1647 std::vector<uint32> screencast_ssrcs;
1648 ScreencastMap::iterator iter;
1649 while (!screencast_capturers_.empty()) {
1650 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1651 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1652 << screencast_capturers_.begin()->first;
1653 ASSERT(false);
1654 break;
1655 }
1656 }
1657
1658 StopMediaMonitor();
1659 // this can't be done in the base class, since it calls a virtual
1660 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001661
1662 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663}
1664
1665bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001666 worker_thread()->Invoke<void>(Bind(
1667 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 return true;
1669}
1670
1671bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001672 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673}
1674
1675VideoCapturer* VideoChannel::AddScreencast(
1676 uint32 ssrc, const ScreencastId& id) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001677 return worker_thread()->Invoke<VideoCapturer*>(Bind(
1678 &VideoChannel::AddScreencast_w, this, ssrc, id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679}
1680
1681bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001682 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1683 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684}
1685
1686bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001687 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688}
1689
1690bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001691 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692}
1693
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001694int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001695 ScreencastDetailsData data(ssrc);
1696 worker_thread()->Invoke<void>(Bind(
1697 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001698 return data.fps;
1699}
1700
1701int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001702 ScreencastDetailsData data(ssrc);
1703 worker_thread()->Invoke<void>(Bind(
1704 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001705 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706}
1707
1708bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001709 worker_thread()->Invoke<void>(Bind(
1710 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 return true;
1712}
1713
1714bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001715 worker_thread()->Invoke<void>(Bind(
1716 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 return true;
1718}
1719
1720void VideoChannel::SetScreenCaptureFactory(
1721 ScreenCapturerFactory* screencapture_factory) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001722 worker_thread()->Invoke<void>(Bind(
1723 &VideoChannel::SetScreenCaptureFactory_w,
1724 this, screencapture_factory));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725}
1726
1727void VideoChannel::ChangeState() {
1728 // Render incoming data if we're the active call, and we have the local
1729 // content. We receive data on the default channel and multiplexed streams.
1730 bool recv = IsReadyToReceive();
1731 if (!media_channel()->SetRender(recv)) {
1732 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1733 // TODO(gangji): Report error back to server.
1734 }
1735
1736 // Send outgoing data if we're the active call, we have the remote content,
1737 // and we have had some form of connectivity.
1738 bool send = IsReadyToSend();
1739 if (!media_channel()->SetSend(send)) {
1740 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1741 // TODO(gangji): Report error back to server.
1742 }
1743
1744 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1745}
1746
1747bool VideoChannel::GetStats(VideoMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001748 return InvokeOnWorker(Bind(&VideoMediaChannel::GetStats,
1749 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750}
1751
1752void VideoChannel::StartMediaMonitor(int cms) {
1753 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
1754 talk_base::Thread::Current()));
1755 media_monitor_->SignalUpdate.connect(
1756 this, &VideoChannel::OnMediaMonitorUpdate);
1757 media_monitor_->Start(cms);
1758}
1759
1760void VideoChannel::StopMediaMonitor() {
1761 if (media_monitor_) {
1762 media_monitor_->Stop();
1763 media_monitor_.reset();
1764 }
1765}
1766
1767const ContentInfo* VideoChannel::GetFirstContent(
1768 const SessionDescription* sdesc) {
1769 return GetFirstVideoContent(sdesc);
1770}
1771
1772bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001773 ContentAction action,
1774 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 ASSERT(worker_thread() == talk_base::Thread::Current());
1776 LOG(LS_INFO) << "Setting local video description";
1777
1778 const VideoContentDescription* video =
1779 static_cast<const VideoContentDescription*>(content);
1780 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001781 if (!video) {
1782 SafeSetError("Can't find video content in local description.", error_desc);
1783 return false;
1784 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001786 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 // Set local video codecs (what we want to receive).
1788 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001789 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1790 SafeSetError("Failed to set video receive codecs.", error_desc);
1791 ret = false;
1792 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 }
1794
1795 if (action != CA_UPDATE) {
1796 VideoOptions video_options;
1797 media_channel()->GetOptions(&video_options);
1798 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1799
1800 if (!media_channel()->SetOptions(video_options)) {
1801 // Log an error on failure, but don't abort the call.
1802 LOG(LS_ERROR) << "Failed to set video channel options";
1803 }
1804 }
1805
1806 // If everything worked, see if we can start receiving.
1807 if (ret) {
1808 ChangeState();
1809 } else {
1810 LOG(LS_WARNING) << "Failed to set local video description";
1811 }
1812 return ret;
1813}
1814
1815bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001816 ContentAction action,
1817 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818 ASSERT(worker_thread() == talk_base::Thread::Current());
1819 LOG(LS_INFO) << "Setting remote video description";
1820
1821 const VideoContentDescription* video =
1822 static_cast<const VideoContentDescription*>(content);
1823 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001824 if (!video) {
1825 SafeSetError("Can't find video content in remote description.", error_desc);
1826 return false;
1827 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828
1829 bool ret = true;
1830 // Set remote video codecs (what the other side wants to receive).
1831 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001832 if (!media_channel()->SetSendCodecs(video->codecs())) {
1833 SafeSetError("Failed to set video send codecs.", error_desc);
1834 ret = false;
1835 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836 }
1837
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001838 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839
1840 if (action != CA_UPDATE) {
1841 // Tweak our video processing settings, if needed.
1842 VideoOptions video_options;
1843 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001844 if (video->conference_mode()) {
1845 video_options.conference_mode.Set(true);
1846 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1848
1849 if (!media_channel()->SetOptions(video_options)) {
1850 // Log an error on failure, but don't abort the call.
1851 LOG(LS_ERROR) << "Failed to set video channel options";
1852 }
1853 }
1854
1855 // If everything worked, see if we can start sending.
1856 if (ret) {
1857 ChangeState();
1858 } else {
1859 LOG(LS_WARNING) << "Failed to set remote video description";
1860 }
1861 return ret;
1862}
1863
1864bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1865 bool ret = true;
1866 // Set the send format for each of the local streams. If the view request
1867 // does not contain a local stream, set its send format to 0x0, which will
1868 // drop all frames.
1869 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1870 it != local_streams().end(); ++it) {
1871 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1872 StaticVideoViews::const_iterator view;
1873 for (view = request.static_video_views.begin();
1874 view != request.static_video_views.end(); ++view) {
1875 if (view->selector.Matches(*it)) {
1876 format.width = view->width;
1877 format.height = view->height;
1878 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1879 break;
1880 }
1881 }
1882
1883 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1884 }
1885
1886 // Check if the view request has invalid streams.
1887 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1888 it != request.static_video_views.end(); ++it) {
1889 if (!GetStream(local_streams(), it->selector, NULL)) {
1890 LOG(LS_WARNING) << "View request for ("
1891 << it->selector.ssrc << ", '"
1892 << it->selector.groupid << "', '"
1893 << it->selector.streamid << "'"
1894 << ") is not in the local streams.";
1895 }
1896 }
1897
1898 return ret;
1899}
1900
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901VideoCapturer* VideoChannel::AddScreencast_w(
1902 uint32 ssrc, const ScreencastId& id) {
1903 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
1904 return NULL;
1905 }
1906 VideoCapturer* screen_capturer =
1907 screencapture_factory_->CreateScreenCapturer(id);
1908 if (!screen_capturer) {
1909 return NULL;
1910 }
1911 screen_capturer->SignalStateChange.connect(this,
1912 &VideoChannel::OnStateChange);
1913 screencast_capturers_[ssrc] = screen_capturer;
1914 return screen_capturer;
1915}
1916
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1918 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1919 if (iter == screencast_capturers_.end()) {
1920 return false;
1921 }
1922 // Clean up VideoCapturer.
1923 delete iter->second;
1924 screencast_capturers_.erase(iter);
1925 return true;
1926}
1927
1928bool VideoChannel::IsScreencasting_w() const {
1929 return !screencast_capturers_.empty();
1930}
1931
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001932void VideoChannel::GetScreencastDetails_w(
1933 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001934 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001936 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 }
1938 VideoCapturer* capturer = iter->second;
1939 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001940 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1941 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942}
1943
1944void VideoChannel::SetScreenCaptureFactory_w(
1945 ScreenCapturerFactory* screencapture_factory) {
1946 if (screencapture_factory == NULL) {
1947 screencapture_factory_.reset(CreateScreenCapturerFactory());
1948 } else {
1949 screencapture_factory_.reset(screencapture_factory);
1950 }
1951}
1952
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
1954 talk_base::WindowEvent we) {
1955 ASSERT(signaling_thread() == talk_base::Thread::Current());
1956 SignalScreencastWindowEvent(ssrc, we);
1957}
1958
1959bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001960 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
1961 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962}
1963
1964void VideoChannel::OnMessage(talk_base::Message *pmsg) {
1965 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 case MSG_SCREENCASTWINDOWEVENT: {
1967 const ScreencastEventMessageData* data =
1968 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1969 OnScreencastWindowEvent_s(data->ssrc, data->event);
1970 delete data;
1971 break;
1972 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973 case MSG_CHANNEL_ERROR: {
1974 const VideoChannelErrorMessageData* data =
1975 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
1976 SignalMediaError(this, data->ssrc, data->error);
1977 delete data;
1978 break;
1979 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 default:
1981 BaseChannel::OnMessage(pmsg);
1982 break;
1983 }
1984}
1985
1986void VideoChannel::OnConnectionMonitorUpdate(
1987 SocketMonitor *monitor, const std::vector<ConnectionInfo> &infos) {
1988 SignalConnectionMonitor(this, infos);
1989}
1990
1991// TODO(pthatcher): Look into removing duplicate code between
1992// audio, video, and data, perhaps by using templates.
1993void VideoChannel::OnMediaMonitorUpdate(
1994 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1995 ASSERT(media_channel == this->media_channel());
1996 SignalMediaMonitor(this, info);
1997}
1998
1999void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
2000 talk_base::WindowEvent event) {
2001 ScreencastEventMessageData* pdata =
2002 new ScreencastEventMessageData(ssrc, event);
2003 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2004}
2005
2006void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2007 // Map capturer events to window events. In the future we may want to simply
2008 // pass these events up directly.
2009 talk_base::WindowEvent we;
2010 if (ev == CS_STOPPED) {
2011 we = talk_base::WE_CLOSE;
2012 } else if (ev == CS_PAUSED) {
2013 we = talk_base::WE_MINIMIZE;
2014 } else if (ev == CS_RUNNING && previous_we_ == talk_base::WE_MINIMIZE) {
2015 we = talk_base::WE_RESTORE;
2016 } else {
2017 return;
2018 }
2019 previous_we_ = we;
2020
2021 uint32 ssrc = 0;
2022 if (!GetLocalSsrc(capturer, &ssrc)) {
2023 return;
2024 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002025
2026 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027}
2028
2029bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2030 *ssrc = 0;
2031 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2032 iter != screencast_capturers_.end(); ++iter) {
2033 if (iter->second == capturer) {
2034 *ssrc = iter->first;
2035 return true;
2036 }
2037 }
2038 return false;
2039}
2040
2041void VideoChannel::OnVideoChannelError(uint32 ssrc,
2042 VideoMediaChannel::Error error) {
2043 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2044 ssrc, error);
2045 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2046}
2047
2048void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2049 SrtpFilter::Error error) {
2050 switch (error) {
2051 case SrtpFilter::ERROR_FAIL:
2052 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2053 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2054 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2055 break;
2056 case SrtpFilter::ERROR_AUTH:
2057 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2058 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2059 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2060 break;
2061 case SrtpFilter::ERROR_REPLAY:
2062 // Only receving channel should have this error.
2063 ASSERT(mode == SrtpFilter::UNPROTECT);
2064 // TODO(gangji): Turn on the signaling of replay error once we have
2065 // switched to the new mechanism for doing video retransmissions.
2066 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2067 break;
2068 default:
2069 break;
2070 }
2071}
2072
2073
2074void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2075 GetSupportedVideoCryptoSuites(ciphers);
2076}
2077
2078DataChannel::DataChannel(talk_base::Thread* thread,
2079 DataMediaChannel* media_channel,
2080 BaseSession* session,
2081 const std::string& content_name,
2082 bool rtcp)
2083 // MediaEngine is NULL
2084 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002085 data_channel_type_(cricket::DCT_NONE),
2086 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087}
2088
2089DataChannel::~DataChannel() {
2090 StopMediaMonitor();
2091 // this can't be done in the base class, since it calls a virtual
2092 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002093
2094 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095}
2096
2097bool DataChannel::Init() {
2098 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
2099 content_name(), "data_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
2100 if (!BaseChannel::Init(session()->CreateChannel(
2101 content_name(), "data_rtp", ICE_CANDIDATE_COMPONENT_RTP),
2102 rtcp_channel)) {
2103 return false;
2104 }
2105 media_channel()->SignalDataReceived.connect(
2106 this, &DataChannel::OnDataReceived);
2107 media_channel()->SignalMediaError.connect(
2108 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002109 media_channel()->SignalReadyToSend.connect(
2110 this, &DataChannel::OnDataChannelReadyToSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 srtp_filter()->SignalSrtpError.connect(
2112 this, &DataChannel::OnSrtpError);
2113 return true;
2114}
2115
2116bool DataChannel::SendData(const SendDataParams& params,
2117 const talk_base::Buffer& payload,
2118 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002119 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2120 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121}
2122
2123const ContentInfo* DataChannel::GetFirstContent(
2124 const SessionDescription* sdesc) {
2125 return GetFirstDataContent(sdesc);
2126}
2127
2128
2129static bool IsRtpPacket(const talk_base::Buffer* packet) {
2130 int version;
2131 if (!GetRtpVersion(packet->data(), packet->length(), &version)) {
2132 return false;
2133 }
2134
2135 return version == 2;
2136}
2137
2138bool DataChannel::WantsPacket(bool rtcp, talk_base::Buffer* packet) {
2139 if (data_channel_type_ == DCT_SCTP) {
2140 // TODO(pthatcher): Do this in a more robust way by checking for
2141 // SCTP or DTLS.
2142 return !IsRtpPacket(packet);
2143 } else if (data_channel_type_ == DCT_RTP) {
2144 return BaseChannel::WantsPacket(rtcp, packet);
2145 }
2146 return false;
2147}
2148
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002149bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2150 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151 // It hasn't been set before, so set it now.
2152 if (data_channel_type_ == DCT_NONE) {
2153 data_channel_type_ = new_data_channel_type;
2154 return true;
2155 }
2156
2157 // It's been set before, but doesn't match. That's bad.
2158 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002159 std::ostringstream desc;
2160 desc << "Data channel type mismatch."
2161 << " Expected " << data_channel_type_
2162 << " Got " << new_data_channel_type;
2163 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 return false;
2165 }
2166
2167 // It's hasn't changed. Nothing to do.
2168 return true;
2169}
2170
2171bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002172 const DataContentDescription* content,
2173 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2175 (content->protocol() == kMediaProtocolDtlsSctp));
2176 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002177 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178}
2179
2180bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002181 ContentAction action,
2182 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183 ASSERT(worker_thread() == talk_base::Thread::Current());
2184 LOG(LS_INFO) << "Setting local data description";
2185
2186 const DataContentDescription* data =
2187 static_cast<const DataContentDescription*>(content);
2188 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002189 if (!data) {
2190 SafeSetError("Can't find data content in local description.", error_desc);
2191 return false;
2192 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193
2194 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002195 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196 return false;
2197 }
2198
2199 if (data_channel_type_ == DCT_SCTP) {
2200 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002201 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 if (ret) {
2203 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002204 // As in SetRemoteContent_w, make sure we set the local SCTP port
2205 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002206 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2207 SafeSetError("Failed to set data receive codecs.", error_desc);
2208 ret = false;
2209 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210 }
2211 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002212 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213
2214 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002215 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2216 SafeSetError("Failed to set data receive codecs.", error_desc);
2217 ret = false;
2218 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002219 }
2220 }
2221
2222 // If everything worked, see if we can start receiving.
2223 if (ret) {
2224 ChangeState();
2225 } else {
2226 LOG(LS_WARNING) << "Failed to set local data description";
2227 }
2228 return ret;
2229}
2230
2231bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002232 ContentAction action,
2233 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 ASSERT(worker_thread() == talk_base::Thread::Current());
2235
2236 const DataContentDescription* data =
2237 static_cast<const DataContentDescription*>(content);
2238 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002239 if (!data) {
2240 SafeSetError("Can't find data content in remote description.", error_desc);
2241 return false;
2242 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002243
2244 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002245 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002246 return false;
2247 }
2248
2249 if (data_channel_type_ == DCT_SCTP) {
2250 LOG(LS_INFO) << "Setting SCTP remote data description";
2251 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002252 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253 if (ret) {
2254 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002255 // We send the SCTP port number (not to be confused with the underlying
2256 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002257 if (!media_channel()->SetSendCodecs(data->codecs())) {
2258 SafeSetError("Failed to set data send codecs.", error_desc);
2259 ret = false;
2260 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002261 }
2262 } else {
2263 // If the remote data doesn't have codecs and isn't an update, it
2264 // must be empty, so ignore it.
2265 if (action != CA_UPDATE && !data->has_codecs()) {
2266 return true;
2267 }
2268 LOG(LS_INFO) << "Setting remote data description";
2269
2270 // Set remote video codecs (what the other side wants to receive).
2271 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002272 if (!media_channel()->SetSendCodecs(data->codecs())) {
2273 SafeSetError("Failed to set data send codecs.", error_desc);
2274 ret = false;
2275 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 }
2277
2278 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002279 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280 }
2281
2282 if (action != CA_UPDATE) {
2283 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002284 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2285 std::ostringstream desc;
2286 desc << "Failed to set max send bandwidth for data content.";
2287 SafeSetError(desc.str(), error_desc);
2288 ret = false;
2289 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 }
2291 }
2292
2293 // If everything worked, see if we can start sending.
2294 if (ret) {
2295 ChangeState();
2296 } else {
2297 LOG(LS_WARNING) << "Failed to set remote data description";
2298 }
2299 return ret;
2300}
2301
2302void DataChannel::ChangeState() {
2303 // Render incoming data if we're the active call, and we have the local
2304 // content. We receive data on the default channel and multiplexed streams.
2305 bool recv = IsReadyToReceive();
2306 if (!media_channel()->SetReceive(recv)) {
2307 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2308 }
2309
2310 // Send outgoing data if we're the active call, we have the remote content,
2311 // and we have had some form of connectivity.
2312 bool send = IsReadyToSend();
2313 if (!media_channel()->SetSend(send)) {
2314 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2315 }
2316
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002317 // Trigger SignalReadyToSendData asynchronously.
2318 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319
2320 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2321}
2322
2323void DataChannel::OnMessage(talk_base::Message *pmsg) {
2324 switch (pmsg->message_id) {
2325 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002326 DataChannelReadyToSendMessageData* data =
2327 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002328 ready_to_send_data_ = data->data();
2329 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330 delete data;
2331 break;
2332 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333 case MSG_DATARECEIVED: {
2334 DataReceivedMessageData* data =
2335 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2336 SignalDataReceived(this, data->params, data->payload);
2337 delete data;
2338 break;
2339 }
2340 case MSG_CHANNEL_ERROR: {
2341 const DataChannelErrorMessageData* data =
2342 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2343 SignalMediaError(this, data->ssrc, data->error);
2344 delete data;
2345 break;
2346 }
2347 default:
2348 BaseChannel::OnMessage(pmsg);
2349 break;
2350 }
2351}
2352
2353void DataChannel::OnConnectionMonitorUpdate(
2354 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
2355 SignalConnectionMonitor(this, infos);
2356}
2357
2358void DataChannel::StartMediaMonitor(int cms) {
2359 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
2360 talk_base::Thread::Current()));
2361 media_monitor_->SignalUpdate.connect(
2362 this, &DataChannel::OnMediaMonitorUpdate);
2363 media_monitor_->Start(cms);
2364}
2365
2366void DataChannel::StopMediaMonitor() {
2367 if (media_monitor_) {
2368 media_monitor_->Stop();
2369 media_monitor_->SignalUpdate.disconnect(this);
2370 media_monitor_.reset();
2371 }
2372}
2373
2374void DataChannel::OnMediaMonitorUpdate(
2375 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2376 ASSERT(media_channel == this->media_channel());
2377 SignalMediaMonitor(this, info);
2378}
2379
2380void DataChannel::OnDataReceived(
2381 const ReceiveDataParams& params, const char* data, size_t len) {
2382 DataReceivedMessageData* msg = new DataReceivedMessageData(
2383 params, data, len);
2384 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2385}
2386
2387void DataChannel::OnDataChannelError(
2388 uint32 ssrc, DataMediaChannel::Error err) {
2389 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2390 ssrc, err);
2391 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2392}
2393
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002394void DataChannel::OnDataChannelReadyToSend(bool writable) {
2395 // This is usded for congestion control to indicate that the stream is ready
2396 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2397 // that the transport channel is ready.
2398 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2399 new DataChannelReadyToSendMessageData(writable));
2400}
2401
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002402void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2403 SrtpFilter::Error error) {
2404 switch (error) {
2405 case SrtpFilter::ERROR_FAIL:
2406 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2407 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2408 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2409 break;
2410 case SrtpFilter::ERROR_AUTH:
2411 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2412 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2413 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2414 break;
2415 case SrtpFilter::ERROR_REPLAY:
2416 // Only receving channel should have this error.
2417 ASSERT(mode == SrtpFilter::UNPROTECT);
2418 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2419 break;
2420 default:
2421 break;
2422 }
2423}
2424
2425void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2426 GetSupportedDataCryptoSuites(ciphers);
2427}
2428
2429bool DataChannel::ShouldSetupDtlsSrtp() const {
2430 return (data_channel_type_ == DCT_RTP);
2431}
2432
2433} // namespace cricket