3 * Copyright 2004 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "talk/session/media/channel.h"
30 #include "talk/base/bind.h"
31 #include "talk/base/buffer.h"
32 #include "talk/base/byteorder.h"
33 #include "talk/base/common.h"
34 #include "talk/base/dscp.h"
35 #include "talk/base/logging.h"
36 #include "talk/media/base/constants.h"
37 #include "talk/media/base/rtputils.h"
38 #include "talk/p2p/base/transportchannel.h"
39 #include "talk/session/media/channelmanager.h"
40 #include "talk/session/media/mediamessages.h"
41 #include "talk/session/media/typingmonitor.h"
46 using talk_base::Bind;
49 MSG_EARLYMEDIATIMEOUT = 1,
50 MSG_SCREENCASTWINDOWEVENT,
56 MSG_FIRSTPACKETRECEIVED,
59 // Value specified in RFC 5764.
60 static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
62 static const int kAgcMinus10db = -10;
64 static void SetSessionError(BaseSession* session, BaseSession::Error error,
65 const std::string& error_desc) {
66 session->SetError(error, error_desc);
69 static void SafeSetError(const std::string& message, std::string* error_desc) {
71 *error_desc = message;
75 // TODO(hellner): use the device manager for creation of screen capturers when
76 // the cl enabling it has landed.
77 class NullScreenCapturerFactory : public VideoChannel::ScreenCapturerFactory {
79 VideoCapturer* CreateScreenCapturer(const ScreencastId& window) {
85 VideoChannel::ScreenCapturerFactory* CreateScreenCapturerFactory() {
86 return new NullScreenCapturerFactory();
89 struct PacketMessageData : public talk_base::MessageData {
90 talk_base::Buffer packet;
91 talk_base::DiffServCodePoint dscp;
94 struct ScreencastEventMessageData : public talk_base::MessageData {
95 ScreencastEventMessageData(uint32 s, talk_base::WindowEvent we)
100 talk_base::WindowEvent event;
103 struct VoiceChannelErrorMessageData : public talk_base::MessageData {
104 VoiceChannelErrorMessageData(uint32 in_ssrc,
105 VoiceMediaChannel::Error in_error)
110 VoiceMediaChannel::Error error;
113 struct VideoChannelErrorMessageData : public talk_base::MessageData {
114 VideoChannelErrorMessageData(uint32 in_ssrc,
115 VideoMediaChannel::Error in_error)
120 VideoMediaChannel::Error error;
123 struct DataChannelErrorMessageData : public talk_base::MessageData {
124 DataChannelErrorMessageData(uint32 in_ssrc,
125 DataMediaChannel::Error in_error)
129 DataMediaChannel::Error error;
133 struct VideoChannel::ScreencastDetailsData {
134 explicit ScreencastDetailsData(uint32 s)
135 : ssrc(s), fps(0), screencast_max_pixels(0) {
139 int screencast_max_pixels;
142 static const char* PacketType(bool rtcp) {
143 return (!rtcp) ? "RTP" : "RTCP";
146 static bool ValidPacket(bool rtcp, const talk_base::Buffer* packet) {
147 // Check the packet size. We could check the header too if needed.
149 packet->length() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
150 packet->length() <= kMaxRtpPacketLen);
153 static bool IsReceiveContentDirection(MediaContentDirection direction) {
154 return direction == MD_SENDRECV || direction == MD_RECVONLY;
157 static bool IsSendContentDirection(MediaContentDirection direction) {
158 return direction == MD_SENDRECV || direction == MD_SENDONLY;
161 static const MediaContentDescription* GetContentDescription(
162 const ContentInfo* cinfo) {
165 return static_cast<const MediaContentDescription*>(cinfo->description);
168 BaseChannel::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),
175 media_channel_(media_channel),
176 content_name_(content_name),
178 transport_channel_(NULL),
179 rtcp_transport_channel_(NULL),
182 rtp_ready_to_send_(false),
183 rtcp_ready_to_send_(false),
184 was_ever_writable_(false),
185 local_content_direction_(MD_INACTIVE),
186 remote_content_direction_(MD_INACTIVE),
187 has_received_packet_(false),
189 secure_required_(false),
190 rtp_abs_sendtime_extn_id_(-1) {
191 ASSERT(worker_thread_ == talk_base::Thread::Current());
192 LOG(LS_INFO) << "Created channel for " << content_name;
195 BaseChannel::~BaseChannel() {
196 ASSERT(worker_thread_ == talk_base::Thread::Current());
198 StopConnectionMonitor();
199 FlushRtcpMessages(); // Send any outstanding RTCP packets.
200 worker_thread_->Clear(this); // eats any outstanding messages or packets
201 // We must destroy the media channel before the transport channel, otherwise
202 // the media channel may try to send on the dead transport channel. NULLing
203 // is not an effective strategy since the sends will come on another thread.
204 delete media_channel_;
205 set_rtcp_transport_channel(NULL);
206 if (transport_channel_ != NULL)
207 session_->DestroyChannel(content_name_, transport_channel_->component());
208 LOG(LS_INFO) << "Destroyed channel";
211 bool BaseChannel::Init(TransportChannel* transport_channel,
212 TransportChannel* rtcp_transport_channel) {
213 if (transport_channel == NULL) {
216 if (rtcp() && rtcp_transport_channel == NULL) {
219 transport_channel_ = transport_channel;
221 if (!SetDtlsSrtpCiphers(transport_channel_, false)) {
225 transport_channel_->SignalWritableState.connect(
226 this, &BaseChannel::OnWritableState);
227 transport_channel_->SignalReadPacket.connect(
228 this, &BaseChannel::OnChannelRead);
229 transport_channel_->SignalReadyToSend.connect(
230 this, &BaseChannel::OnReadyToSend);
232 session_->SignalNewLocalDescription.connect(
233 this, &BaseChannel::OnNewLocalDescription);
234 session_->SignalNewRemoteDescription.connect(
235 this, &BaseChannel::OnNewRemoteDescription);
237 set_rtcp_transport_channel(rtcp_transport_channel);
238 // Both RTP and RTCP channels are set, we can call SetInterface on
239 // media channel and it can set network options.
240 media_channel_->SetInterface(this);
244 void BaseChannel::Deinit() {
245 media_channel_->SetInterface(NULL);
248 bool BaseChannel::Enable(bool enable) {
249 worker_thread_->Invoke<void>(Bind(
250 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
255 bool BaseChannel::MuteStream(uint32 ssrc, bool mute) {
256 return InvokeOnWorker(Bind(&BaseChannel::MuteStream_w, this, ssrc, mute));
259 bool BaseChannel::IsStreamMuted(uint32 ssrc) {
260 return InvokeOnWorker(Bind(&BaseChannel::IsStreamMuted_w, this, ssrc));
263 bool BaseChannel::AddRecvStream(const StreamParams& sp) {
264 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
267 bool BaseChannel::RemoveRecvStream(uint32 ssrc) {
268 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
271 bool BaseChannel::AddSendStream(const StreamParams& sp) {
272 return InvokeOnWorker(
273 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
276 bool BaseChannel::RemoveSendStream(uint32 ssrc) {
277 return InvokeOnWorker(
278 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
281 bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
282 ContentAction action,
283 std::string* error_desc) {
284 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
285 this, content, action, error_desc));
288 bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
289 ContentAction action,
290 std::string* error_desc) {
291 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
292 this, content, action, error_desc));
295 void BaseChannel::StartConnectionMonitor(int cms) {
296 socket_monitor_.reset(new SocketMonitor(transport_channel_,
298 talk_base::Thread::Current()));
299 socket_monitor_->SignalUpdate.connect(
300 this, &BaseChannel::OnConnectionMonitorUpdate);
301 socket_monitor_->Start(cms);
304 void BaseChannel::StopConnectionMonitor() {
305 if (socket_monitor_) {
306 socket_monitor_->Stop();
307 socket_monitor_.reset();
311 void BaseChannel::set_rtcp_transport_channel(TransportChannel* channel) {
312 if (rtcp_transport_channel_ != channel) {
313 if (rtcp_transport_channel_) {
314 session_->DestroyChannel(
315 content_name_, rtcp_transport_channel_->component());
317 rtcp_transport_channel_ = channel;
318 if (rtcp_transport_channel_) {
319 // TODO(juberti): Propagate this error code
320 VERIFY(SetDtlsSrtpCiphers(rtcp_transport_channel_, true));
321 rtcp_transport_channel_->SignalWritableState.connect(
322 this, &BaseChannel::OnWritableState);
323 rtcp_transport_channel_->SignalReadPacket.connect(
324 this, &BaseChannel::OnChannelRead);
325 rtcp_transport_channel_->SignalReadyToSend.connect(
326 this, &BaseChannel::OnReadyToSend);
331 bool BaseChannel::IsReadyToReceive() const {
332 // Receive data if we are enabled and have local content,
333 return enabled() && IsReceiveContentDirection(local_content_direction_);
336 bool BaseChannel::IsReadyToSend() const {
337 // Send outgoing data if we are enabled, have local and remote content,
338 // and we have had some form of connectivity.
340 IsReceiveContentDirection(remote_content_direction_) &&
341 IsSendContentDirection(local_content_direction_) &&
345 bool BaseChannel::SendPacket(talk_base::Buffer* packet,
346 talk_base::DiffServCodePoint dscp) {
347 return SendPacket(false, packet, dscp);
350 bool BaseChannel::SendRtcp(talk_base::Buffer* packet,
351 talk_base::DiffServCodePoint dscp) {
352 return SendPacket(true, packet, dscp);
355 int BaseChannel::SetOption(SocketType type, talk_base::Socket::Option opt,
357 TransportChannel* channel = NULL;
360 channel = transport_channel_;
363 channel = rtcp_transport_channel_;
366 return channel ? channel->SetOption(opt, value) : -1;
369 void BaseChannel::OnWritableState(TransportChannel* channel) {
370 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
371 if (transport_channel_->writable()
372 && (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
375 ChannelNotWritable_w();
379 void BaseChannel::OnChannelRead(TransportChannel* channel,
380 const char* data, size_t len,
381 const talk_base::PacketTime& packet_time,
383 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
384 ASSERT(worker_thread_ == talk_base::Thread::Current());
386 // When using RTCP multiplexing we might get RTCP packets on the RTP
387 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
388 bool rtcp = PacketIsRtcp(channel, data, len);
389 talk_base::Buffer packet(data, len);
390 HandlePacket(rtcp, &packet, packet_time);
393 void BaseChannel::OnReadyToSend(TransportChannel* channel) {
394 SetReadyToSend(channel, true);
397 void BaseChannel::SetReadyToSend(TransportChannel* channel, bool ready) {
398 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
399 if (channel == transport_channel_) {
400 rtp_ready_to_send_ = ready;
402 if (channel == rtcp_transport_channel_) {
403 rtcp_ready_to_send_ = ready;
407 // Notify the MediaChannel when either rtp or rtcp channel can't send.
408 media_channel_->OnReadyToSend(false);
409 } else if (rtp_ready_to_send_ &&
410 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
411 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
412 // Notify the MediaChannel when both rtp and rtcp channel can send.
413 media_channel_->OnReadyToSend(true);
417 bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
418 const char* data, size_t len) {
419 return (channel == rtcp_transport_channel_ ||
420 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
423 bool BaseChannel::SendPacket(bool rtcp, talk_base::Buffer* packet,
424 talk_base::DiffServCodePoint dscp) {
425 // SendPacket gets called from MediaEngine, typically on an encoder thread.
426 // If the thread is not our worker thread, we will post to our worker
427 // so that the real work happens on our worker. This avoids us having to
428 // synchronize access to all the pieces of the send path, including
429 // SRTP and the inner workings of the transport channels.
430 // The only downside is that we can't return a proper failure code if
431 // needed. Since UDP is unreliable anyway, this should be a non-issue.
432 if (talk_base::Thread::Current() != worker_thread_) {
433 // Avoid a copy by transferring the ownership of the packet data.
434 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
435 PacketMessageData* data = new PacketMessageData;
436 packet->TransferTo(&data->packet);
438 worker_thread_->Post(this, message_id, data);
442 // Now that we are on the correct thread, ensure we have a place to send this
443 // packet before doing anything. (We might get RTCP packets that we don't
444 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
446 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
447 transport_channel_ : rtcp_transport_channel_;
448 if (!channel || !channel->writable()) {
452 // Protect ourselves against crazy data.
453 if (!ValidPacket(rtcp, packet)) {
454 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
455 << PacketType(rtcp) << " packet: wrong size="
460 // Signal to the media sink before protecting the packet.
462 talk_base::CritScope cs(&signal_send_packet_cs_);
463 SignalSendPacketPreCrypto(packet->data(), packet->length(), rtcp);
466 talk_base::PacketOptions options(dscp);
467 // Protect if needed.
468 if (srtp_filter_.IsActive()) {
470 char* data = packet->data();
471 int len = static_cast<int>(packet->length());
473 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
474 // inside libsrtp for a RTP packet. A external HMAC module will be writing
475 // a fake HMAC value. This is ONLY done for a RTP packet.
476 // Socket layer will update rtp sendtime extension header if present in
477 // packet with current time before updating the HMAC.
478 #if !defined(ENABLE_EXTERNAL_AUTH)
479 res = srtp_filter_.ProtectRtp(
480 data, len, static_cast<int>(packet->capacity()), &len);
482 options.packet_time_params.rtp_sendtime_extension_id =
483 rtp_abs_sendtime_extn_id_;
484 res = srtp_filter_.ProtectRtp(
485 data, len, static_cast<int>(packet->capacity()), &len,
486 &options.packet_time_params.srtp_packet_index);
487 // If protection succeeds, let's get auth params from srtp.
489 uint8* auth_key = NULL;
491 res = srtp_filter_.GetRtpAuthParams(
492 &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
494 options.packet_time_params.srtp_auth_key.resize(key_len);
495 options.packet_time_params.srtp_auth_key.assign(auth_key,
503 GetRtpSeqNum(data, len, &seq_num);
504 GetRtpSsrc(data, len, &ssrc);
505 LOG(LS_ERROR) << "Failed to protect " << content_name_
506 << " RTP packet: size=" << len
507 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
511 res = srtp_filter_.ProtectRtcp(data, len,
512 static_cast<int>(packet->capacity()),
516 GetRtcpType(data, len, &type);
517 LOG(LS_ERROR) << "Failed to protect " << content_name_
518 << " RTCP packet: size=" << len << ", type=" << type;
523 // Update the length of the packet now that we've added the auth tag.
524 packet->SetLength(len);
525 } else if (secure_required_) {
526 // This is a double check for something that supposedly can't happen.
527 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
528 << " packet when SRTP is inactive and crypto is required";
534 // Signal to the media sink after protecting the packet.
536 talk_base::CritScope cs(&signal_send_packet_cs_);
537 SignalSendPacketPostCrypto(packet->data(), packet->length(), rtcp);
541 int ret = channel->SendPacket(packet->data(), packet->length(), options,
542 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
543 if (ret != static_cast<int>(packet->length())) {
544 if (channel->GetError() == EWOULDBLOCK) {
545 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
546 SetReadyToSend(channel, false);
553 bool BaseChannel::WantsPacket(bool rtcp, talk_base::Buffer* packet) {
554 // Protect ourselves against crazy data.
555 if (!ValidPacket(rtcp, packet)) {
556 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
557 << PacketType(rtcp) << " packet: wrong size="
562 // Bundle filter handles both rtp and rtcp packets.
563 return bundle_filter_.DemuxPacket(packet->data(), packet->length(), rtcp);
566 void BaseChannel::HandlePacket(bool rtcp, talk_base::Buffer* packet,
567 const talk_base::PacketTime& packet_time) {
568 if (!WantsPacket(rtcp, packet)) {
572 if (!has_received_packet_) {
573 has_received_packet_ = true;
574 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
577 // Signal to the media sink before unprotecting the packet.
579 talk_base::CritScope cs(&signal_recv_packet_cs_);
580 SignalRecvPacketPostCrypto(packet->data(), packet->length(), rtcp);
583 // Unprotect the packet, if needed.
584 if (srtp_filter_.IsActive()) {
585 char* data = packet->data();
586 int len = static_cast<int>(packet->length());
589 res = srtp_filter_.UnprotectRtp(data, len, &len);
593 GetRtpSeqNum(data, len, &seq_num);
594 GetRtpSsrc(data, len, &ssrc);
595 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
596 << " RTP packet: size=" << len
597 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
601 res = srtp_filter_.UnprotectRtcp(data, len, &len);
604 GetRtcpType(data, len, &type);
605 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
606 << " RTCP packet: size=" << len << ", type=" << type;
611 packet->SetLength(len);
612 } else if (secure_required_) {
613 // Our session description indicates that SRTP is required, but we got a
614 // packet before our SRTP filter is active. This means either that
615 // a) we got SRTP packets before we received the SDES keys, in which case
616 // we can't decrypt it anyway, or
617 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
618 // channels, so we haven't yet extracted keys, even if DTLS did complete
619 // on the channel that the packets are being sent on. It's really good
620 // practice to wait for both RTP and RTCP to be good to go before sending
621 // media, to prevent weird failure modes, so it's fine for us to just eat
622 // packets here. This is all sidestepped if RTCP mux is used anyway.
623 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
624 << " packet when SRTP is inactive and crypto is required";
628 // Signal to the media sink after unprotecting the packet.
630 talk_base::CritScope cs(&signal_recv_packet_cs_);
631 SignalRecvPacketPreCrypto(packet->data(), packet->length(), rtcp);
634 // Push it down to the media channel.
636 media_channel_->OnPacketReceived(packet, packet_time);
638 media_channel_->OnRtcpReceived(packet, packet_time);
642 void BaseChannel::OnNewLocalDescription(
643 BaseSession* session, ContentAction action) {
644 const ContentInfo* content_info =
645 GetFirstContent(session->local_description());
646 const MediaContentDescription* content_desc =
647 GetContentDescription(content_info);
648 std::string error_desc;
649 if (content_desc && content_info && !content_info->rejected &&
650 !SetLocalContent(content_desc, action, &error_desc)) {
651 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
652 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
656 void BaseChannel::OnNewRemoteDescription(
657 BaseSession* session, ContentAction action) {
658 const ContentInfo* content_info =
659 GetFirstContent(session->remote_description());
660 const MediaContentDescription* content_desc =
661 GetContentDescription(content_info);
662 std::string error_desc;
663 if (content_desc && content_info && !content_info->rejected &&
664 !SetRemoteContent(content_desc, action, &error_desc)) {
665 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
666 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
670 void BaseChannel::EnableMedia_w() {
671 ASSERT(worker_thread_ == talk_base::Thread::Current());
675 LOG(LS_INFO) << "Channel enabled";
680 void BaseChannel::DisableMedia_w() {
681 ASSERT(worker_thread_ == talk_base::Thread::Current());
685 LOG(LS_INFO) << "Channel disabled";
690 bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
691 ASSERT(worker_thread_ == talk_base::Thread::Current());
692 bool ret = media_channel()->MuteStream(ssrc, mute);
695 muted_streams_.insert(ssrc);
697 muted_streams_.erase(ssrc);
702 bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
703 ASSERT(worker_thread_ == talk_base::Thread::Current());
704 return muted_streams_.find(ssrc) != muted_streams_.end();
707 void BaseChannel::ChannelWritable_w() {
708 ASSERT(worker_thread_ == talk_base::Thread::Current());
712 LOG(LS_INFO) << "Channel socket writable ("
713 << transport_channel_->content_name() << ", "
714 << transport_channel_->component() << ")"
715 << (was_ever_writable_ ? "" : " for the first time");
717 std::vector<ConnectionInfo> infos;
718 transport_channel_->GetStats(&infos);
719 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
720 it != infos.end(); ++it) {
721 if (it->best_connection) {
722 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
723 << "->" << it->remote_candidate.ToSensitiveString();
728 // If we're doing DTLS-SRTP, now is the time.
729 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
730 if (!SetupDtlsSrtp(false)) {
731 const std::string error_desc =
732 "Couldn't set up DTLS-SRTP on RTP channel.";
733 // Sent synchronously.
734 signaling_thread()->Invoke<void>(Bind(
737 BaseSession::ERROR_TRANSPORT,
742 if (rtcp_transport_channel_) {
743 if (!SetupDtlsSrtp(true)) {
744 const std::string error_desc =
745 "Couldn't set up DTLS-SRTP on RTCP channel";
746 // Sent synchronously.
747 signaling_thread()->Invoke<void>(Bind(
750 BaseSession::ERROR_TRANSPORT,
757 was_ever_writable_ = true;
762 bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
763 std::vector<std::string> ciphers;
764 // We always use the default SRTP ciphers for RTCP, but we may use different
765 // ciphers for RTP depending on the media type.
767 GetSrtpCiphers(&ciphers);
769 GetSupportedDefaultCryptoSuites(&ciphers);
771 return tc->SetSrtpCiphers(ciphers);
774 bool BaseChannel::ShouldSetupDtlsSrtp() const {
778 // This function returns true if either DTLS-SRTP is not in use
779 // *or* DTLS-SRTP is successfully set up.
780 bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
783 TransportChannel *channel = rtcp_channel ?
784 rtcp_transport_channel_ : transport_channel_;
787 if (!channel->IsDtlsActive())
790 std::string selected_cipher;
792 if (!channel->GetSrtpCipher(&selected_cipher)) {
793 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
797 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
798 << content_name() << " "
799 << PacketType(rtcp_channel);
801 // OK, we're now doing DTLS (RFC 5764)
802 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
803 SRTP_MASTER_KEY_SALT_LEN * 2);
805 // RFC 5705 exporter using the RFC 5764 parameters
806 if (!channel->ExportKeyingMaterial(
807 kDtlsSrtpExporterLabel,
809 &dtls_buffer[0], dtls_buffer.size())) {
810 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
811 ASSERT(false); // This should never happen
815 // Sync up the keys with the DTLS-SRTP interface
816 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
817 SRTP_MASTER_KEY_SALT_LEN);
818 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
819 SRTP_MASTER_KEY_SALT_LEN);
821 memcpy(&client_write_key[0], &dtls_buffer[offset],
822 SRTP_MASTER_KEY_KEY_LEN);
823 offset += SRTP_MASTER_KEY_KEY_LEN;
824 memcpy(&server_write_key[0], &dtls_buffer[offset],
825 SRTP_MASTER_KEY_KEY_LEN);
826 offset += SRTP_MASTER_KEY_KEY_LEN;
827 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
828 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
829 offset += SRTP_MASTER_KEY_SALT_LEN;
830 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
831 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
833 std::vector<unsigned char> *send_key, *recv_key;
834 talk_base::SSLRole role;
835 if (!channel->GetSslRole(&role)) {
836 LOG(LS_WARNING) << "GetSslRole failed";
840 if (role == talk_base::SSL_SERVER) {
841 send_key = &server_write_key;
842 recv_key = &client_write_key;
844 send_key = &client_write_key;
845 recv_key = &server_write_key;
849 ret = srtp_filter_.SetRtcpParams(
852 static_cast<int>(send_key->size()),
855 static_cast<int>(recv_key->size()));
857 ret = srtp_filter_.SetRtpParams(
860 static_cast<int>(send_key->size()),
863 static_cast<int>(recv_key->size()));
867 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
874 void BaseChannel::ChannelNotWritable_w() {
875 ASSERT(worker_thread_ == talk_base::Thread::Current());
879 LOG(LS_INFO) << "Channel socket not writable ("
880 << transport_channel_->content_name() << ", "
881 << transport_channel_->component() << ")";
886 // |dtls| will be set to true if DTLS is active for transport channel and
888 bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
890 std::string* error_desc) {
891 *dtls = transport_channel_->IsDtlsActive();
892 if (*dtls && !cryptos.empty()) {
893 SafeSetError("Cryptos must be empty when DTLS is active.",
900 bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
901 ContentAction action,
903 std::string* error_desc) {
904 if (action == CA_UPDATE) {
910 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
916 // If DTLS is already active on the channel, we could be renegotiating
917 // here. We don't update the srtp filter.
919 ret = srtp_filter_.SetOffer(cryptos, src);
923 // If we're doing DTLS-SRTP, we don't want to update the filter
924 // with an answer, because we already have SRTP parameters.
926 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
930 // If we're doing DTLS-SRTP, we don't want to update the filter
931 // with an answer, because we already have SRTP parameters.
933 ret = srtp_filter_.SetAnswer(cryptos, src);
940 SafeSetError("Failed to setup SRTP filter.", error_desc);
946 bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
948 std::string* error_desc) {
952 ret = rtcp_mux_filter_.SetOffer(enable, src);
955 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
958 ret = rtcp_mux_filter_.SetAnswer(enable, src);
959 if (ret && rtcp_mux_filter_.IsActive()) {
960 // We activated RTCP mux, close down the RTCP transport.
961 set_rtcp_transport_channel(NULL);
971 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
974 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
975 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
976 // received a final answer.
977 if (rtcp_mux_filter_.IsActive()) {
978 // If the RTP transport is already writable, then so are we.
979 if (transport_channel_->writable()) {
987 bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
988 ASSERT(worker_thread() == talk_base::Thread::Current());
989 if (!media_channel()->AddRecvStream(sp))
992 return bundle_filter_.AddStream(sp);
995 bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
996 ASSERT(worker_thread() == talk_base::Thread::Current());
997 bundle_filter_.RemoveStream(ssrc);
998 return media_channel()->RemoveRecvStream(ssrc);
1001 bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
1002 ContentAction action,
1003 std::string* error_desc) {
1004 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1005 action == CA_PRANSWER || action == CA_UPDATE))
1008 // If this is an update, streams only contain streams that have changed.
1009 if (action == CA_UPDATE) {
1010 for (StreamParamsVec::const_iterator it = streams.begin();
1011 it != streams.end(); ++it) {
1012 StreamParams existing_stream;
1013 bool stream_exist = GetStreamByIds(local_streams_, it->groupid,
1014 it->id, &existing_stream);
1015 if (!stream_exist && it->has_ssrcs()) {
1016 if (media_channel()->AddSendStream(*it)) {
1017 local_streams_.push_back(*it);
1018 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1020 std::ostringstream desc;
1021 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1022 SafeSetError(desc.str(), error_desc);
1025 } else if (stream_exist && !it->has_ssrcs()) {
1026 if (!media_channel()->RemoveSendStream(existing_stream.first_ssrc())) {
1027 std::ostringstream desc;
1028 desc << "Failed to remove send stream with ssrc "
1029 << it->first_ssrc() << ".";
1030 SafeSetError(desc.str(), error_desc);
1033 RemoveStreamBySsrc(&local_streams_, existing_stream.first_ssrc());
1035 LOG(LS_WARNING) << "Ignore unsupported stream update";
1040 // Else streams are all the streams we want to send.
1042 // Check for streams that have been removed.
1044 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1045 it != local_streams_.end(); ++it) {
1046 if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1047 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
1048 std::ostringstream desc;
1049 desc << "Failed to remove send stream with ssrc "
1050 << it->first_ssrc() << ".";
1051 SafeSetError(desc.str(), error_desc);
1056 // Check for new streams.
1057 for (StreamParamsVec::const_iterator it = streams.begin();
1058 it != streams.end(); ++it) {
1059 if (!GetStreamBySsrc(local_streams_, it->first_ssrc(), NULL)) {
1060 if (media_channel()->AddSendStream(*it)) {
1061 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1063 std::ostringstream desc;
1064 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1065 SafeSetError(desc.str(), error_desc);
1070 local_streams_ = streams;
1074 bool BaseChannel::UpdateRemoteStreams_w(
1075 const std::vector<StreamParams>& streams,
1076 ContentAction action,
1077 std::string* error_desc) {
1078 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1079 action == CA_PRANSWER || action == CA_UPDATE))
1082 // If this is an update, streams only contain streams that have changed.
1083 if (action == CA_UPDATE) {
1084 for (StreamParamsVec::const_iterator it = streams.begin();
1085 it != streams.end(); ++it) {
1086 StreamParams existing_stream;
1087 bool stream_exists = GetStreamByIds(remote_streams_, it->groupid,
1088 it->id, &existing_stream);
1089 if (!stream_exists && it->has_ssrcs()) {
1090 if (AddRecvStream_w(*it)) {
1091 remote_streams_.push_back(*it);
1092 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1094 std::ostringstream desc;
1095 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1096 SafeSetError(desc.str(), error_desc);
1099 } else if (stream_exists && !it->has_ssrcs()) {
1100 if (!RemoveRecvStream_w(existing_stream.first_ssrc())) {
1101 std::ostringstream desc;
1102 desc << "Failed to remove remote stream with ssrc "
1103 << it->first_ssrc() << ".";
1104 SafeSetError(desc.str(), error_desc);
1107 RemoveStreamBySsrc(&remote_streams_, existing_stream.first_ssrc());
1109 LOG(LS_WARNING) << "Ignore unsupported stream update."
1110 << " Stream exists? " << stream_exists
1111 << " existing stream = " << existing_stream.ToString()
1112 << " new stream = " << it->ToString();
1117 // Else streams are all the streams we want to receive.
1119 // Check for streams that have been removed.
1121 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1122 it != remote_streams_.end(); ++it) {
1123 if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1124 if (!RemoveRecvStream_w(it->first_ssrc())) {
1125 std::ostringstream desc;
1126 desc << "Failed to remove remote stream with ssrc "
1127 << it->first_ssrc() << ".";
1128 SafeSetError(desc.str(), error_desc);
1133 // Check for new streams.
1134 for (StreamParamsVec::const_iterator it = streams.begin();
1135 it != streams.end(); ++it) {
1136 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc(), NULL)) {
1137 if (AddRecvStream_w(*it)) {
1138 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1140 std::ostringstream desc;
1141 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1142 SafeSetError(desc.str(), error_desc);
1147 remote_streams_ = streams;
1151 bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
1152 ContentAction action,
1153 std::string* error_desc) {
1154 // Cache secure_required_ for belt and suspenders check on SendPacket
1155 secure_required_ = content->crypto_required() != CT_NONE;
1156 bool ret = UpdateLocalStreams_w(content->streams(), action, error_desc);
1157 // Set local SRTP parameters (what we will encrypt with).
1158 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
1159 // Set local RTCP mux parameters.
1160 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
1161 // Set local RTP header extensions.
1162 if (content->rtp_header_extensions_set()) {
1163 if (!media_channel()->SetRecvRtpHeaderExtensions(
1164 content->rtp_header_extensions())) {
1165 std::ostringstream desc;
1166 desc << "Failed to set receive rtp header extensions for "
1167 << MediaTypeToString(content->type()) << " content.";
1168 SafeSetError(desc.str(), error_desc);
1172 set_local_content_direction(content->direction());
1176 bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
1177 ContentAction action,
1178 std::string* error_desc) {
1179 bool ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
1180 // Set remote SRTP parameters (what the other side will encrypt with).
1181 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
1182 // Set remote RTCP mux parameters.
1183 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
1184 // Set remote RTP header extensions.
1185 if (content->rtp_header_extensions_set()) {
1186 if (!media_channel()->SetSendRtpHeaderExtensions(
1187 content->rtp_header_extensions())) {
1188 std::ostringstream desc;
1189 desc << "Failed to set send rtp header extensions for "
1190 << MediaTypeToString(content->type()) << " content.";
1191 SafeSetError(desc.str(), error_desc);
1194 MaybeCacheRtpAbsSendTimeHeaderExtension(content->rtp_header_extensions());
1198 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1199 std::ostringstream desc;
1200 desc << "Failed to set max send bandwidth for "
1201 << MediaTypeToString(content->type()) << " content.";
1202 SafeSetError(desc.str(), error_desc);
1205 set_remote_content_direction(content->direction());
1209 void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1210 const std::vector<RtpHeaderExtension>& extensions) {
1211 const RtpHeaderExtension* send_time_extension =
1212 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
1213 rtp_abs_sendtime_extn_id_ =
1214 send_time_extension ? send_time_extension->id : -1;
1217 void BaseChannel::OnMessage(talk_base::Message *pmsg) {
1218 switch (pmsg->message_id) {
1220 case MSG_RTCPPACKET: {
1221 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
1222 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
1223 delete data; // because it is Posted
1226 case MSG_FIRSTPACKETRECEIVED: {
1227 SignalFirstPacketReceived(this);
1233 void BaseChannel::FlushRtcpMessages() {
1234 // Flush all remaining RTCP messages. This should only be called in
1236 ASSERT(talk_base::Thread::Current() == worker_thread_);
1237 talk_base::MessageList rtcp_messages;
1238 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
1239 for (talk_base::MessageList::iterator it = rtcp_messages.begin();
1240 it != rtcp_messages.end(); ++it) {
1241 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
1245 VoiceChannel::VoiceChannel(talk_base::Thread* thread,
1246 MediaEngineInterface* media_engine,
1247 VoiceMediaChannel* media_channel,
1248 BaseSession* session,
1249 const std::string& content_name,
1251 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1253 received_media_(false) {
1256 VoiceChannel::~VoiceChannel() {
1259 // this can't be done in the base class, since it calls a virtual
1264 bool VoiceChannel::Init() {
1265 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1266 content_name(), "rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1267 if (!BaseChannel::Init(session()->CreateChannel(
1268 content_name(), "rtp", ICE_CANDIDATE_COMPONENT_RTP),
1272 media_channel()->SignalMediaError.connect(
1273 this, &VoiceChannel::OnVoiceChannelError);
1274 srtp_filter()->SignalSrtpError.connect(
1275 this, &VoiceChannel::OnSrtpError);
1279 bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
1280 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1281 media_channel(), ssrc, renderer));
1284 bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
1285 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1286 media_channel(), ssrc, renderer));
1289 bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
1290 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
1293 // TODO(juberti): Handle early media the right way. We should get an explicit
1294 // ringing message telling us to start playing local ringback, which we cancel
1295 // if any early media actually arrives. For now, we do the opposite, which is
1296 // to wait 1 second for early media, and start playing local ringback if none
1298 void VoiceChannel::SetEarlyMedia(bool enable) {
1300 // Start the early media timeout
1301 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1302 MSG_EARLYMEDIATIMEOUT);
1304 // Stop the timeout if currently going.
1305 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
1309 bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
1310 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1311 this, ssrc, play, loop));
1314 bool VoiceChannel::PressDTMF(int digit, bool playout) {
1315 int flags = DF_SEND;
1319 int duration_ms = 160;
1320 return InsertDtmf(0, digit, duration_ms, flags);
1323 bool VoiceChannel::CanInsertDtmf() {
1324 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1328 bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1330 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1331 ssrc, event_code, duration, flags));
1334 bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
1335 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1336 media_channel(), ssrc, left, right));
1339 bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
1340 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1341 media_channel(), stats));
1344 void VoiceChannel::StartMediaMonitor(int cms) {
1345 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
1346 talk_base::Thread::Current()));
1347 media_monitor_->SignalUpdate.connect(
1348 this, &VoiceChannel::OnMediaMonitorUpdate);
1349 media_monitor_->Start(cms);
1352 void VoiceChannel::StopMediaMonitor() {
1353 if (media_monitor_) {
1354 media_monitor_->Stop();
1355 media_monitor_->SignalUpdate.disconnect(this);
1356 media_monitor_.reset();
1360 void VoiceChannel::StartAudioMonitor(int cms) {
1361 audio_monitor_.reset(new AudioMonitor(this, talk_base::Thread::Current()));
1363 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1364 audio_monitor_->Start(cms);
1367 void VoiceChannel::StopAudioMonitor() {
1368 if (audio_monitor_) {
1369 audio_monitor_->Stop();
1370 audio_monitor_.reset();
1374 bool VoiceChannel::IsAudioMonitorRunning() const {
1375 return (audio_monitor_.get() != NULL);
1378 void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1379 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1380 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1383 void VoiceChannel::StopTypingMonitor() {
1384 typing_monitor_.reset();
1387 bool VoiceChannel::IsTypingMonitorRunning() const {
1388 return typing_monitor_;
1391 bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1392 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1393 if (typing_monitor_ && mute)
1394 typing_monitor_->OnChannelMuted();
1398 int VoiceChannel::GetInputLevel_w() {
1399 return media_engine()->GetInputLevel();
1402 int VoiceChannel::GetOutputLevel_w() {
1403 return media_channel()->GetOutputLevel();
1406 void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1407 media_channel()->GetActiveStreams(actives);
1410 void VoiceChannel::OnChannelRead(TransportChannel* channel,
1411 const char* data, size_t len,
1412 const talk_base::PacketTime& packet_time,
1414 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
1416 // Set a flag when we've received an RTP packet. If we're waiting for early
1417 // media, this will disable the timeout.
1418 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1419 received_media_ = true;
1423 void VoiceChannel::ChangeState() {
1424 // Render incoming data if we're the active call, and we have the local
1425 // content. We receive data on the default channel and multiplexed streams.
1426 bool recv = IsReadyToReceive();
1427 if (!media_channel()->SetPlayout(recv)) {
1428 SendLastMediaError();
1431 // Send outgoing data if we're the active call, we have the remote content,
1432 // and we have had some form of connectivity.
1433 bool send = IsReadyToSend();
1434 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1435 if (!media_channel()->SetSend(send_flag)) {
1436 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1437 SendLastMediaError();
1440 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1443 const ContentInfo* VoiceChannel::GetFirstContent(
1444 const SessionDescription* sdesc) {
1445 return GetFirstAudioContent(sdesc);
1448 bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
1449 ContentAction action,
1450 std::string* error_desc) {
1451 ASSERT(worker_thread() == talk_base::Thread::Current());
1452 LOG(LS_INFO) << "Setting local voice description";
1454 const AudioContentDescription* audio =
1455 static_cast<const AudioContentDescription*>(content);
1456 ASSERT(audio != NULL);
1458 SafeSetError("Can't find audio content in local description.", error_desc);
1462 bool ret = SetBaseLocalContent_w(content, action, error_desc);
1463 // Set local audio codecs (what we want to receive).
1464 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1466 if (action != CA_UPDATE || audio->has_codecs()) {
1467 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1468 SafeSetError("Failed to set audio receive codecs.", error_desc);
1473 // If everything worked, see if we can start receiving.
1475 std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1476 for (; it != audio->codecs().end(); ++it) {
1477 bundle_filter()->AddPayloadType(it->id);
1481 LOG(LS_WARNING) << "Failed to set local voice description";
1486 bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
1487 ContentAction action,
1488 std::string* error_desc) {
1489 ASSERT(worker_thread() == talk_base::Thread::Current());
1490 LOG(LS_INFO) << "Setting remote voice description";
1492 const AudioContentDescription* audio =
1493 static_cast<const AudioContentDescription*>(content);
1494 ASSERT(audio != NULL);
1496 SafeSetError("Can't find audio content in remote description.", error_desc);
1501 // Set remote video codecs (what the other side wants to receive).
1502 if (action != CA_UPDATE || audio->has_codecs()) {
1503 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1504 SafeSetError("Failed to set audio send codecs.", error_desc);
1509 ret &= SetBaseRemoteContent_w(content, action, error_desc);
1511 if (action != CA_UPDATE) {
1512 // Tweak our audio processing settings, if needed.
1513 AudioOptions audio_options;
1514 if (!media_channel()->GetOptions(&audio_options)) {
1515 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1517 if (audio->conference_mode()) {
1518 audio_options.conference_mode.Set(true);
1520 if (audio->agc_minus_10db()) {
1521 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1523 if (!media_channel()->SetOptions(audio_options)) {
1524 // Log an error on failure, but don't abort the call.
1525 LOG(LS_ERROR) << "Failed to set voice channel options";
1530 // If everything worked, see if we can start sending.
1534 LOG(LS_WARNING) << "Failed to set remote voice description";
1539 bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
1540 ASSERT(worker_thread() == talk_base::Thread::Current());
1541 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1544 bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
1545 ASSERT(worker_thread() == talk_base::Thread::Current());
1547 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1549 LOG(LS_INFO) << "Stopping ringback tone";
1551 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1554 void VoiceChannel::HandleEarlyMediaTimeout() {
1555 // This occurs on the main thread, not the worker thread.
1556 if (!received_media_) {
1557 LOG(LS_INFO) << "No early media received before timeout";
1558 SignalEarlyMediaTimeout(this);
1562 bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1568 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1571 bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
1572 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1573 media_channel(), options));
1576 void VoiceChannel::OnMessage(talk_base::Message *pmsg) {
1577 switch (pmsg->message_id) {
1578 case MSG_EARLYMEDIATIMEOUT:
1579 HandleEarlyMediaTimeout();
1581 case MSG_CHANNEL_ERROR: {
1582 VoiceChannelErrorMessageData* data =
1583 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1584 SignalMediaError(this, data->ssrc, data->error);
1589 BaseChannel::OnMessage(pmsg);
1594 void VoiceChannel::OnConnectionMonitorUpdate(
1595 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
1596 SignalConnectionMonitor(this, infos);
1599 void VoiceChannel::OnMediaMonitorUpdate(
1600 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1601 ASSERT(media_channel == this->media_channel());
1602 SignalMediaMonitor(this, info);
1605 void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1606 const AudioInfo& info) {
1607 SignalAudioMonitor(this, info);
1610 void VoiceChannel::OnVoiceChannelError(
1611 uint32 ssrc, VoiceMediaChannel::Error err) {
1612 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1614 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1617 void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1618 SrtpFilter::Error error) {
1620 case SrtpFilter::ERROR_FAIL:
1621 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1622 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1623 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1625 case SrtpFilter::ERROR_AUTH:
1626 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1627 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1628 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1630 case SrtpFilter::ERROR_REPLAY:
1631 // Only receving channel should have this error.
1632 ASSERT(mode == SrtpFilter::UNPROTECT);
1633 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1640 void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1641 GetSupportedAudioCryptoSuites(ciphers);
1644 VideoChannel::VideoChannel(talk_base::Thread* thread,
1645 MediaEngineInterface* media_engine,
1646 VideoMediaChannel* media_channel,
1647 BaseSession* session,
1648 const std::string& content_name,
1650 VoiceChannel* voice_channel)
1651 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1653 voice_channel_(voice_channel),
1655 screencapture_factory_(CreateScreenCapturerFactory()),
1656 previous_we_(talk_base::WE_CLOSE) {
1659 bool VideoChannel::Init() {
1660 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1661 content_name(), "video_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1662 if (!BaseChannel::Init(session()->CreateChannel(
1663 content_name(), "video_rtp", ICE_CANDIDATE_COMPONENT_RTP),
1667 media_channel()->SignalMediaError.connect(
1668 this, &VideoChannel::OnVideoChannelError);
1669 srtp_filter()->SignalSrtpError.connect(
1670 this, &VideoChannel::OnSrtpError);
1674 void VoiceChannel::SendLastMediaError() {
1676 VoiceMediaChannel::Error error;
1677 media_channel()->GetLastMediaError(&ssrc, &error);
1678 SignalMediaError(this, ssrc, error);
1681 VideoChannel::~VideoChannel() {
1682 std::vector<uint32> screencast_ssrcs;
1683 ScreencastMap::iterator iter;
1684 while (!screencast_capturers_.empty()) {
1685 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1686 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1687 << screencast_capturers_.begin()->first;
1694 // this can't be done in the base class, since it calls a virtual
1700 bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
1701 worker_thread()->Invoke<void>(Bind(
1702 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
1706 bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
1707 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
1710 VideoCapturer* VideoChannel::AddScreencast(
1711 uint32 ssrc, const ScreencastId& id) {
1712 return worker_thread()->Invoke<VideoCapturer*>(Bind(
1713 &VideoChannel::AddScreencast_w, this, ssrc, id));
1716 bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
1717 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1718 media_channel(), ssrc, capturer));
1721 bool VideoChannel::RemoveScreencast(uint32 ssrc) {
1722 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
1725 bool VideoChannel::IsScreencasting() {
1726 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
1729 int VideoChannel::GetScreencastFps(uint32 ssrc) {
1730 ScreencastDetailsData data(ssrc);
1731 worker_thread()->Invoke<void>(Bind(
1732 &VideoChannel::GetScreencastDetails_w, this, &data));
1736 int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
1737 ScreencastDetailsData data(ssrc);
1738 worker_thread()->Invoke<void>(Bind(
1739 &VideoChannel::GetScreencastDetails_w, this, &data));
1740 return data.screencast_max_pixels;
1743 bool VideoChannel::SendIntraFrame() {
1744 worker_thread()->Invoke<void>(Bind(
1745 &VideoMediaChannel::SendIntraFrame, media_channel()));
1749 bool VideoChannel::RequestIntraFrame() {
1750 worker_thread()->Invoke<void>(Bind(
1751 &VideoMediaChannel::RequestIntraFrame, media_channel()));
1755 void VideoChannel::SetScreenCaptureFactory(
1756 ScreenCapturerFactory* screencapture_factory) {
1757 worker_thread()->Invoke<void>(Bind(
1758 &VideoChannel::SetScreenCaptureFactory_w,
1759 this, screencapture_factory));
1762 void VideoChannel::ChangeState() {
1763 // Render incoming data if we're the active call, and we have the local
1764 // content. We receive data on the default channel and multiplexed streams.
1765 bool recv = IsReadyToReceive();
1766 if (!media_channel()->SetRender(recv)) {
1767 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1768 // TODO(gangji): Report error back to server.
1771 // Send outgoing data if we're the active call, we have the remote content,
1772 // and we have had some form of connectivity.
1773 bool send = IsReadyToSend();
1774 if (!media_channel()->SetSend(send)) {
1775 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1776 // TODO(gangji): Report error back to server.
1779 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1782 bool VideoChannel::GetStats(
1783 const StatsOptions& options, VideoMediaInfo* stats) {
1784 return InvokeOnWorker(Bind(&VideoMediaChannel::GetStats,
1785 media_channel(), options, stats));
1788 void VideoChannel::StartMediaMonitor(int cms) {
1789 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
1790 talk_base::Thread::Current()));
1791 media_monitor_->SignalUpdate.connect(
1792 this, &VideoChannel::OnMediaMonitorUpdate);
1793 media_monitor_->Start(cms);
1796 void VideoChannel::StopMediaMonitor() {
1797 if (media_monitor_) {
1798 media_monitor_->Stop();
1799 media_monitor_.reset();
1803 const ContentInfo* VideoChannel::GetFirstContent(
1804 const SessionDescription* sdesc) {
1805 return GetFirstVideoContent(sdesc);
1808 bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
1809 ContentAction action,
1810 std::string* error_desc) {
1811 ASSERT(worker_thread() == talk_base::Thread::Current());
1812 LOG(LS_INFO) << "Setting local video description";
1814 const VideoContentDescription* video =
1815 static_cast<const VideoContentDescription*>(content);
1816 ASSERT(video != NULL);
1818 SafeSetError("Can't find video content in local description.", error_desc);
1822 bool ret = SetBaseLocalContent_w(content, action, error_desc);
1823 // Set local video codecs (what we want to receive).
1824 if (action != CA_UPDATE || video->has_codecs()) {
1825 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1826 SafeSetError("Failed to set video receive codecs.", error_desc);
1831 if (action != CA_UPDATE) {
1832 VideoOptions video_options;
1833 media_channel()->GetOptions(&video_options);
1834 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1836 if (!media_channel()->SetOptions(video_options)) {
1837 // Log an error on failure, but don't abort the call.
1838 LOG(LS_ERROR) << "Failed to set video channel options";
1842 // If everything worked, see if we can start receiving.
1844 std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1845 for (; it != video->codecs().end(); ++it) {
1846 bundle_filter()->AddPayloadType(it->id);
1850 LOG(LS_WARNING) << "Failed to set local video description";
1855 bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
1856 ContentAction action,
1857 std::string* error_desc) {
1858 ASSERT(worker_thread() == talk_base::Thread::Current());
1859 LOG(LS_INFO) << "Setting remote video description";
1861 const VideoContentDescription* video =
1862 static_cast<const VideoContentDescription*>(content);
1863 ASSERT(video != NULL);
1865 SafeSetError("Can't find video content in remote description.", error_desc);
1870 // Set remote video codecs (what the other side wants to receive).
1871 if (action != CA_UPDATE || video->has_codecs()) {
1872 if (!media_channel()->SetSendCodecs(video->codecs())) {
1873 SafeSetError("Failed to set video send codecs.", error_desc);
1878 ret &= SetBaseRemoteContent_w(content, action, error_desc);
1880 if (action != CA_UPDATE) {
1881 // Tweak our video processing settings, if needed.
1882 VideoOptions video_options;
1883 media_channel()->GetOptions(&video_options);
1884 if (video->conference_mode()) {
1885 video_options.conference_mode.Set(true);
1887 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1889 if (!media_channel()->SetOptions(video_options)) {
1890 // Log an error on failure, but don't abort the call.
1891 LOG(LS_ERROR) << "Failed to set video channel options";
1895 // If everything worked, see if we can start sending.
1899 LOG(LS_WARNING) << "Failed to set remote video description";
1904 bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1906 // Set the send format for each of the local streams. If the view request
1907 // does not contain a local stream, set its send format to 0x0, which will
1909 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1910 it != local_streams().end(); ++it) {
1911 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1912 StaticVideoViews::const_iterator view;
1913 for (view = request.static_video_views.begin();
1914 view != request.static_video_views.end(); ++view) {
1915 if (view->selector.Matches(*it)) {
1916 format.width = view->width;
1917 format.height = view->height;
1918 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1923 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1926 // Check if the view request has invalid streams.
1927 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1928 it != request.static_video_views.end(); ++it) {
1929 if (!GetStream(local_streams(), it->selector, NULL)) {
1930 LOG(LS_WARNING) << "View request for ("
1931 << it->selector.ssrc << ", '"
1932 << it->selector.groupid << "', '"
1933 << it->selector.streamid << "'"
1934 << ") is not in the local streams.";
1941 VideoCapturer* VideoChannel::AddScreencast_w(
1942 uint32 ssrc, const ScreencastId& id) {
1943 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
1946 VideoCapturer* screen_capturer =
1947 screencapture_factory_->CreateScreenCapturer(id);
1948 if (!screen_capturer) {
1951 screen_capturer->SignalStateChange.connect(this,
1952 &VideoChannel::OnStateChange);
1953 screencast_capturers_[ssrc] = screen_capturer;
1954 return screen_capturer;
1957 bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1958 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1959 if (iter == screencast_capturers_.end()) {
1962 // Clean up VideoCapturer.
1963 delete iter->second;
1964 screencast_capturers_.erase(iter);
1968 bool VideoChannel::IsScreencasting_w() const {
1969 return !screencast_capturers_.empty();
1972 void VideoChannel::GetScreencastDetails_w(
1973 ScreencastDetailsData* data) const {
1974 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
1975 if (iter == screencast_capturers_.end()) {
1978 VideoCapturer* capturer = iter->second;
1979 const VideoFormat* video_format = capturer->GetCaptureFormat();
1980 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1981 data->screencast_max_pixels = capturer->screencast_max_pixels();
1984 void VideoChannel::SetScreenCaptureFactory_w(
1985 ScreenCapturerFactory* screencapture_factory) {
1986 if (screencapture_factory == NULL) {
1987 screencapture_factory_.reset(CreateScreenCapturerFactory());
1989 screencapture_factory_.reset(screencapture_factory);
1993 void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
1994 talk_base::WindowEvent we) {
1995 ASSERT(signaling_thread() == talk_base::Thread::Current());
1996 SignalScreencastWindowEvent(ssrc, we);
1999 bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
2000 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
2001 media_channel(), options));
2004 void VideoChannel::OnMessage(talk_base::Message *pmsg) {
2005 switch (pmsg->message_id) {
2006 case MSG_SCREENCASTWINDOWEVENT: {
2007 const ScreencastEventMessageData* data =
2008 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
2009 OnScreencastWindowEvent_s(data->ssrc, data->event);
2013 case MSG_CHANNEL_ERROR: {
2014 const VideoChannelErrorMessageData* data =
2015 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
2016 SignalMediaError(this, data->ssrc, data->error);
2021 BaseChannel::OnMessage(pmsg);
2026 void VideoChannel::OnConnectionMonitorUpdate(
2027 SocketMonitor *monitor, const std::vector<ConnectionInfo> &infos) {
2028 SignalConnectionMonitor(this, infos);
2031 // TODO(pthatcher): Look into removing duplicate code between
2032 // audio, video, and data, perhaps by using templates.
2033 void VideoChannel::OnMediaMonitorUpdate(
2034 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2035 ASSERT(media_channel == this->media_channel());
2036 SignalMediaMonitor(this, info);
2039 void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
2040 talk_base::WindowEvent event) {
2041 ScreencastEventMessageData* pdata =
2042 new ScreencastEventMessageData(ssrc, event);
2043 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2046 void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2047 // Map capturer events to window events. In the future we may want to simply
2048 // pass these events up directly.
2049 talk_base::WindowEvent we;
2050 if (ev == CS_STOPPED) {
2051 we = talk_base::WE_CLOSE;
2052 } else if (ev == CS_PAUSED) {
2053 we = talk_base::WE_MINIMIZE;
2054 } else if (ev == CS_RUNNING && previous_we_ == talk_base::WE_MINIMIZE) {
2055 we = talk_base::WE_RESTORE;
2062 if (!GetLocalSsrc(capturer, &ssrc)) {
2066 OnScreencastWindowEvent(ssrc, we);
2069 bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2071 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2072 iter != screencast_capturers_.end(); ++iter) {
2073 if (iter->second == capturer) {
2074 *ssrc = iter->first;
2081 void VideoChannel::OnVideoChannelError(uint32 ssrc,
2082 VideoMediaChannel::Error error) {
2083 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2085 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2088 void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2089 SrtpFilter::Error error) {
2091 case SrtpFilter::ERROR_FAIL:
2092 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2093 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2094 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2096 case SrtpFilter::ERROR_AUTH:
2097 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2098 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2099 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2101 case SrtpFilter::ERROR_REPLAY:
2102 // Only receving channel should have this error.
2103 ASSERT(mode == SrtpFilter::UNPROTECT);
2104 // TODO(gangji): Turn on the signaling of replay error once we have
2105 // switched to the new mechanism for doing video retransmissions.
2106 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2114 void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2115 GetSupportedVideoCryptoSuites(ciphers);
2118 DataChannel::DataChannel(talk_base::Thread* thread,
2119 DataMediaChannel* media_channel,
2120 BaseSession* session,
2121 const std::string& content_name,
2123 // MediaEngine is NULL
2124 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
2125 data_channel_type_(cricket::DCT_NONE),
2126 ready_to_send_data_(false) {
2129 DataChannel::~DataChannel() {
2131 // this can't be done in the base class, since it calls a virtual
2137 bool DataChannel::Init() {
2138 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
2139 content_name(), "data_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
2140 if (!BaseChannel::Init(session()->CreateChannel(
2141 content_name(), "data_rtp", ICE_CANDIDATE_COMPONENT_RTP),
2145 media_channel()->SignalDataReceived.connect(
2146 this, &DataChannel::OnDataReceived);
2147 media_channel()->SignalMediaError.connect(
2148 this, &DataChannel::OnDataChannelError);
2149 media_channel()->SignalReadyToSend.connect(
2150 this, &DataChannel::OnDataChannelReadyToSend);
2151 srtp_filter()->SignalSrtpError.connect(
2152 this, &DataChannel::OnSrtpError);
2156 bool DataChannel::SendData(const SendDataParams& params,
2157 const talk_base::Buffer& payload,
2158 SendDataResult* result) {
2159 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2160 media_channel(), params, payload, result));
2163 const ContentInfo* DataChannel::GetFirstContent(
2164 const SessionDescription* sdesc) {
2165 return GetFirstDataContent(sdesc);
2169 static bool IsRtpPacket(const talk_base::Buffer* packet) {
2171 if (!GetRtpVersion(packet->data(), packet->length(), &version)) {
2175 return version == 2;
2178 bool DataChannel::WantsPacket(bool rtcp, talk_base::Buffer* packet) {
2179 if (data_channel_type_ == DCT_SCTP) {
2180 // TODO(pthatcher): Do this in a more robust way by checking for
2182 return !IsRtpPacket(packet);
2183 } else if (data_channel_type_ == DCT_RTP) {
2184 return BaseChannel::WantsPacket(rtcp, packet);
2189 bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2190 std::string* error_desc) {
2191 // It hasn't been set before, so set it now.
2192 if (data_channel_type_ == DCT_NONE) {
2193 data_channel_type_ = new_data_channel_type;
2197 // It's been set before, but doesn't match. That's bad.
2198 if (data_channel_type_ != new_data_channel_type) {
2199 std::ostringstream desc;
2200 desc << "Data channel type mismatch."
2201 << " Expected " << data_channel_type_
2202 << " Got " << new_data_channel_type;
2203 SafeSetError(desc.str(), error_desc);
2207 // It's hasn't changed. Nothing to do.
2211 bool DataChannel::SetDataChannelTypeFromContent(
2212 const DataContentDescription* content,
2213 std::string* error_desc) {
2214 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2215 (content->protocol() == kMediaProtocolDtlsSctp));
2216 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
2217 return SetDataChannelType(data_channel_type, error_desc);
2220 bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
2221 ContentAction action,
2222 std::string* error_desc) {
2223 ASSERT(worker_thread() == talk_base::Thread::Current());
2224 LOG(LS_INFO) << "Setting local data description";
2226 const DataContentDescription* data =
2227 static_cast<const DataContentDescription*>(content);
2228 ASSERT(data != NULL);
2230 SafeSetError("Can't find data content in local description.", error_desc);
2235 if (!SetDataChannelTypeFromContent(data, error_desc)) {
2239 if (data_channel_type_ == DCT_SCTP) {
2240 // SCTP data channels don't need the rest of the stuff.
2241 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
2243 set_local_content_direction(content->direction());
2244 // As in SetRemoteContent_w, make sure we set the local SCTP port
2245 // number as specified in our DataContentDescription.
2246 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2247 SafeSetError("Failed to set data receive codecs.", error_desc);
2252 ret = SetBaseLocalContent_w(content, action, error_desc);
2253 if (action != CA_UPDATE || data->has_codecs()) {
2254 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2255 SafeSetError("Failed to set data receive codecs.", error_desc);
2261 // If everything worked, see if we can start receiving.
2263 std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2264 for (; it != data->codecs().end(); ++it) {
2265 bundle_filter()->AddPayloadType(it->id);
2269 LOG(LS_WARNING) << "Failed to set local data description";
2274 bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
2275 ContentAction action,
2276 std::string* error_desc) {
2277 ASSERT(worker_thread() == talk_base::Thread::Current());
2279 const DataContentDescription* data =
2280 static_cast<const DataContentDescription*>(content);
2281 ASSERT(data != NULL);
2283 SafeSetError("Can't find data content in remote description.", error_desc);
2288 if (!SetDataChannelTypeFromContent(data, error_desc)) {
2292 if (data_channel_type_ == DCT_SCTP) {
2293 LOG(LS_INFO) << "Setting SCTP remote data description";
2294 // SCTP data channels don't need the rest of the stuff.
2295 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
2297 set_remote_content_direction(content->direction());
2298 // We send the SCTP port number (not to be confused with the underlying
2299 // UDP port number) as a codec parameter. Make sure it gets there.
2300 if (!media_channel()->SetSendCodecs(data->codecs())) {
2301 SafeSetError("Failed to set data send codecs.", error_desc);
2306 // If the remote data doesn't have codecs and isn't an update, it
2307 // must be empty, so ignore it.
2308 if (action != CA_UPDATE && !data->has_codecs()) {
2311 LOG(LS_INFO) << "Setting remote data description";
2313 // Set remote video codecs (what the other side wants to receive).
2314 if (action != CA_UPDATE || data->has_codecs()) {
2315 if (!media_channel()->SetSendCodecs(data->codecs())) {
2316 SafeSetError("Failed to set data send codecs.", error_desc);
2322 ret &= SetBaseRemoteContent_w(content, action, error_desc);
2325 if (action != CA_UPDATE) {
2326 int bandwidth_bps = data->bandwidth();
2327 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2328 std::ostringstream desc;
2329 desc << "Failed to set max send bandwidth for data content.";
2330 SafeSetError(desc.str(), error_desc);
2336 // If everything worked, see if we can start sending.
2340 LOG(LS_WARNING) << "Failed to set remote data description";
2345 void DataChannel::ChangeState() {
2346 // Render incoming data if we're the active call, and we have the local
2347 // content. We receive data on the default channel and multiplexed streams.
2348 bool recv = IsReadyToReceive();
2349 if (!media_channel()->SetReceive(recv)) {
2350 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2353 // Send outgoing data if we're the active call, we have the remote content,
2354 // and we have had some form of connectivity.
2355 bool send = IsReadyToSend();
2356 if (!media_channel()->SetSend(send)) {
2357 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2360 // Trigger SignalReadyToSendData asynchronously.
2361 OnDataChannelReadyToSend(send);
2363 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2366 void DataChannel::OnMessage(talk_base::Message *pmsg) {
2367 switch (pmsg->message_id) {
2368 case MSG_READYTOSENDDATA: {
2369 DataChannelReadyToSendMessageData* data =
2370 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
2371 ready_to_send_data_ = data->data();
2372 SignalReadyToSendData(ready_to_send_data_);
2376 case MSG_DATARECEIVED: {
2377 DataReceivedMessageData* data =
2378 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2379 SignalDataReceived(this, data->params, data->payload);
2383 case MSG_CHANNEL_ERROR: {
2384 const DataChannelErrorMessageData* data =
2385 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2386 SignalMediaError(this, data->ssrc, data->error);
2391 BaseChannel::OnMessage(pmsg);
2396 void DataChannel::OnConnectionMonitorUpdate(
2397 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
2398 SignalConnectionMonitor(this, infos);
2401 void DataChannel::StartMediaMonitor(int cms) {
2402 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
2403 talk_base::Thread::Current()));
2404 media_monitor_->SignalUpdate.connect(
2405 this, &DataChannel::OnMediaMonitorUpdate);
2406 media_monitor_->Start(cms);
2409 void DataChannel::StopMediaMonitor() {
2410 if (media_monitor_) {
2411 media_monitor_->Stop();
2412 media_monitor_->SignalUpdate.disconnect(this);
2413 media_monitor_.reset();
2417 void DataChannel::OnMediaMonitorUpdate(
2418 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2419 ASSERT(media_channel == this->media_channel());
2420 SignalMediaMonitor(this, info);
2423 void DataChannel::OnDataReceived(
2424 const ReceiveDataParams& params, const char* data, size_t len) {
2425 DataReceivedMessageData* msg = new DataReceivedMessageData(
2427 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2430 void DataChannel::OnDataChannelError(
2431 uint32 ssrc, DataMediaChannel::Error err) {
2432 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2434 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2437 void DataChannel::OnDataChannelReadyToSend(bool writable) {
2438 // This is usded for congestion control to indicate that the stream is ready
2439 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2440 // that the transport channel is ready.
2441 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2442 new DataChannelReadyToSendMessageData(writable));
2445 void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2446 SrtpFilter::Error error) {
2448 case SrtpFilter::ERROR_FAIL:
2449 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2450 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2451 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2453 case SrtpFilter::ERROR_AUTH:
2454 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2455 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2456 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2458 case SrtpFilter::ERROR_REPLAY:
2459 // Only receving channel should have this error.
2460 ASSERT(mode == SrtpFilter::UNPROTECT);
2461 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2468 void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2469 GetSupportedDataCryptoSuites(ciphers);
2472 bool DataChannel::ShouldSetupDtlsSrtp() const {
2473 return (data_channel_type_ == DCT_RTP);
2476 } // namespace cricket