blob: 75299d5dbf0fb9fbfcba061aeb773b333cbb3cbd [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_CHANNEL_H_
12#define PC_CHANNEL_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
deadbeefcbecd352015-09-23 11:50:27 -070014#include <map>
kwiberg31022942016-03-11 14:18:21 -080015#include <memory>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <set>
kjellandera96e2d72016-02-04 23:52:28 -080017#include <string>
deadbeefcbecd352015-09-23 11:50:27 -070018#include <utility>
kjellandera96e2d72016-02-04 23:52:28 -080019#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/call/audio_sink.h"
Steve Anton3828c062017-12-06 10:34:51 -080022#include "api/jsep.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/rtpreceiverinterface.h"
Patrik Höglundbe214a22018-01-04 12:14:35 +010024#include "api/videosinkinterface.h"
Patrik Höglund9e194032018-01-04 15:58:20 +010025#include "api/videosourceinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "media/base/mediachannel.h"
27#include "media/base/mediaengine.h"
28#include "media/base/streamparams.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "p2p/base/dtlstransportinternal.h"
30#include "p2p/base/packettransportinternal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "p2p/client/socketmonitor.h"
32#include "pc/audiomonitor.h"
Zhi Huangcd3fc5d2017-11-29 10:41:57 -080033#include "pc/dtlssrtptransport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "pc/mediasession.h"
35#include "pc/rtcpmuxfilter.h"
Zhi Huangcd3fc5d2017-11-29 10:41:57 -080036#include "pc/rtptransport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "pc/srtpfilter.h"
Zhi Huangcd3fc5d2017-11-29 10:41:57 -080038#include "pc/srtptransport.h"
Zhi Huangb5261582017-09-29 10:51:43 -070039#include "pc/transportcontroller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "rtc_base/asyncinvoker.h"
41#include "rtc_base/asyncudpsocket.h"
42#include "rtc_base/criticalsection.h"
43#include "rtc_base/network.h"
44#include "rtc_base/sigslot.h"
Tommif888bb52015-12-12 01:37:01 +010045
46namespace webrtc {
47class AudioSinkInterface;
48} // namespace webrtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
50namespace cricket {
51
52struct CryptoParams;
53class MediaContentDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
deadbeef062ce9f2016-08-26 21:42:15 -070055// BaseChannel contains logic common to voice and video, including enable,
56// marshaling calls to a worker and network threads, and connection and media
57// monitors.
58//
Danil Chapovalov33b01f22016-05-11 19:55:27 +020059// BaseChannel assumes signaling and other threads are allowed to make
60// synchronous calls to the worker thread, the worker thread makes synchronous
61// calls only to the network thread, and the network thread can't be blocked by
62// other threads.
63// All methods with _n suffix must be called on network thread,
deadbeef062ce9f2016-08-26 21:42:15 -070064// methods with _w suffix on worker thread
Danil Chapovalov33b01f22016-05-11 19:55:27 +020065// and methods with _s suffix on signaling thread.
66// Network and worker threads may be the same thread.
wu@webrtc.org78187522013-10-07 23:32:02 +000067//
68// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
69// This is required to avoid a data race between the destructor modifying the
70// vtable, and the media channel's thread using BaseChannel as the
71// NetworkInterface.
72
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 : public rtc::MessageHandler, public sigslot::has_slots<>,
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000075 public MediaChannel::NetworkInterface,
76 public ConnectionStatsGetter {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 public:
deadbeef7af91dd2016-12-13 11:29:11 -080078 // If |srtp_required| is true, the channel will not send or receive any
79 // RTP/RTCP packets without using SRTP (either using SDES or DTLS-SRTP).
Danil Chapovalov33b01f22016-05-11 19:55:27 +020080 BaseChannel(rtc::Thread* worker_thread,
81 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -080082 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 15:53:33 -080083 std::unique_ptr<MediaChannel> media_channel,
deadbeefcbecd352015-09-23 11:50:27 -070084 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -080085 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -080086 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 virtual ~BaseChannel();
Zhi Huang2dfc42d2017-12-04 13:38:48 -080088 // TODO(zhihuang): Remove this once the RtpTransport can be shared between
89 // BaseChannels.
Steve Anton8699a322017-11-06 15:53:33 -080090 void Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -080091 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -080092 rtc::PacketTransportInternal* rtp_packet_transport,
93 rtc::PacketTransportInternal* rtcp_packet_transport);
Zhi Huang2dfc42d2017-12-04 13:38:48 -080094 void Init_w(webrtc::RtpTransportInternal* rtp_transport);
95
Danil Chapovalov33b01f22016-05-11 19:55:27 +020096 // Deinit may be called multiple times and is simply ignored if it's already
wu@webrtc.org78187522013-10-07 23:32:02 +000097 // done.
98 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000100 rtc::Thread* worker_thread() const { return worker_thread_; }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200101 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -0700102 const std::string& content_name() const { return content_name_; }
deadbeeff5346592017-01-24 21:51:21 -0800103 // TODO(deadbeef): This is redundant; remove this.
deadbeefcbecd352015-09-23 11:50:27 -0700104 const std::string& transport_name() const { return transport_name_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
Zhi Huangcf990f52017-09-22 12:12:30 -0700107 // This function returns true if we are using SDES.
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800108 bool sdes_active() const {
109 return sdes_transport_ && sdes_negotiator_.IsActive();
110 }
Zhi Huangcf990f52017-09-22 12:12:30 -0700111 // The following function returns true if we are using DTLS-based keying.
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800112 bool dtls_active() const {
113 return dtls_srtp_transport_ && dtls_srtp_transport_->IsActive();
114 }
Zhi Huangcf990f52017-09-22 12:12:30 -0700115 // This function returns true if using SRTP (DTLS-based keying or SDES).
116 bool srtp_active() const { return sdes_active() || dtls_active(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
118 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800120 // Set an RTP level transport which could be an RtpTransport without
121 // encryption, an SrtpTransport for SDES or a DtlsSrtpTransport for DTLS-SRTP.
122 // This can be called from any thread and it hops to the network thread
123 // internally. It would replace the |SetTransports| and its variants.
124 void SetRtpTransport(webrtc::RtpTransportInternal* rtp_transport);
125
deadbeefbad5dad2017-01-17 18:32:35 -0800126 // Set the transport(s), and update writability and "ready-to-send" state.
127 // |rtp_transport| must be non-null.
128 // |rtcp_transport| must be supplied if NeedsRtcpTransport() is true (meaning
129 // RTCP muxing is not fully active yet).
130 // |rtp_transport| and |rtcp_transport| must share the same transport name as
131 // well.
deadbeef5bd5ca32017-02-10 11:31:50 -0800132 // Can not start with "rtc::PacketTransportInternal" and switch to
deadbeeff5346592017-01-24 21:51:21 -0800133 // "DtlsTransportInternal", or vice-versa.
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800134 // TODO(zhihuang): Remove these two once the RtpTransport can be shared
135 // between BaseChannels.
zhihuangb2cdd932017-01-19 16:54:25 -0800136 void SetTransports(DtlsTransportInternal* rtp_dtls_transport,
137 DtlsTransportInternal* rtcp_dtls_transport);
deadbeef5bd5ca32017-02-10 11:31:50 -0800138 void SetTransports(rtc::PacketTransportInternal* rtp_packet_transport,
139 rtc::PacketTransportInternal* rtcp_packet_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 // Channel control
141 bool SetLocalContent(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800142 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000143 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 bool SetRemoteContent(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800145 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000146 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147
148 bool Enable(bool enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149
150 // Multiplexing
151 bool AddRecvStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200152 bool RemoveRecvStream(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000153 bool AddSendStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200154 bool RemoveSendStream(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
156 // Monitoring
157 void StartConnectionMonitor(int cms);
158 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000159 // For ConnectionStatsGetter, used by ConnectionMonitor
deadbeefcbecd352015-09-23 11:50:27 -0700160 bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 const std::vector<StreamParams>& local_streams() const {
163 return local_streams_;
164 }
165 const std::vector<StreamParams>& remote_streams() const {
166 return remote_streams_;
167 }
168
deadbeef953c2ce2017-01-09 14:53:41 -0800169 sigslot::signal2<BaseChannel*, bool> SignalDtlsSrtpSetupFailure;
170 void SignalDtlsSrtpSetupFailure_n(bool rtcp);
171 void SignalDtlsSrtpSetupFailure_s(bool rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000172
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000173 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
175
zhihuangb2cdd932017-01-19 16:54:25 -0800176 // Forward SignalSentPacket to worker thread.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200177 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket;
178
deadbeefac22f702017-01-12 21:59:29 -0800179 // Emitted whenever rtcp-mux is fully negotiated and the rtcp-transport can
180 // be destroyed.
181 // Fired on the network thread.
182 sigslot::signal1<const std::string&> SignalRtcpMuxFullyActive;
zhihuangf5b251b2017-01-12 19:37:48 -0800183
zhihuangb2cdd932017-01-19 16:54:25 -0800184 // Only public for unit tests. Otherwise, consider private.
185 DtlsTransportInternal* rtp_dtls_transport() const {
186 return rtp_dtls_transport_;
187 }
188 DtlsTransportInternal* rtcp_dtls_transport() const {
189 return rtcp_dtls_transport_;
190 }
zhihuangf5b251b2017-01-12 19:37:48 -0800191
192 bool NeedsRtcpTransport();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200193
zstein56162b92017-04-24 16:54:35 -0700194 // From RtpTransport - public for testing only
195 void OnTransportReadyToSend(bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000197 // Only public for unit tests. Otherwise, consider protected.
rlesterec9d1872015-10-27 14:22:16 -0700198 int SetOption(SocketType type, rtc::Socket::Option o, int val)
199 override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200200 int SetOption_n(SocketType type, rtc::Socket::Option o, int val);
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000201
zhihuang184a3fd2016-06-14 11:47:14 -0700202 virtual cricket::MediaType media_type() = 0;
203
zstein3dcf0e92017-06-01 13:22:42 -0700204 // Public for testing.
205 // TODO(zstein): Remove this once channels register themselves with
206 // an RtpTransport in a more explicit way.
207 bool HandlesPayloadType(int payload_type) const;
208
Steve Anton593e3252017-12-15 11:44:48 -0800209 // Used by the RTCStatsCollector tests to set the transport name without
210 // creating RtpTransports.
211 void set_transport_name_for_testing(const std::string& transport_name) {
212 transport_name_ = transport_name;
213 }
214
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 protected:
Steve Anton8699a322017-11-06 15:53:33 -0800216 virtual MediaChannel* media_channel() const { return media_channel_.get(); }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700217
zhihuangb2cdd932017-01-19 16:54:25 -0800218 void SetTransports_n(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800219 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800220 rtc::PacketTransportInternal* rtp_packet_transport,
221 rtc::PacketTransportInternal* rtcp_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800222
deadbeef062ce9f2016-08-26 21:42:15 -0700223 // This does not update writability or "ready-to-send" state; it just
224 // disconnects from the old channel and connects to the new one.
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800225 // TODO(zhihuang): Remove this once the RtpTransport can be shared between
226 // BaseChannels.
deadbeeff5346592017-01-24 21:51:21 -0800227 void SetTransport_n(bool rtcp,
228 DtlsTransportInternal* new_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800229 rtc::PacketTransportInternal* new_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 bool was_ever_writable() const { return was_ever_writable_; }
Steve Anton4e70a722017-11-28 14:57:10 -0800232 void set_local_content_direction(webrtc::RtpTransceiverDirection direction) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 local_content_direction_ = direction;
234 }
Steve Anton4e70a722017-11-28 14:57:10 -0800235 void set_remote_content_direction(webrtc::RtpTransceiverDirection direction) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 remote_content_direction_ = direction;
237 }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700238 // These methods verify that:
239 // * The required content description directions have been set.
240 // * The channel is enabled.
241 // * And for sending:
242 // - The SRTP filter is active if it's needed.
243 // - The transport has been writable before, meaning it should be at least
244 // possible to succeed in sending a packet.
245 //
246 // When any of these properties change, UpdateMediaSendRecvState_w should be
247 // called.
248 bool IsReadyToReceiveMedia_w() const;
249 bool IsReadyToSendMedia_w() const;
zhihuangf5b251b2017-01-12 19:37:48 -0800250 rtc::Thread* signaling_thread() { return signaling_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200252 void FlushRtcpMessages_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253
254 // NetworkInterface implementation, called by MediaEngine
jbaucheec21bd2016-03-20 06:15:43 -0700255 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
256 const rtc::PacketOptions& options) override;
257 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
258 const rtc::PacketOptions& options) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800260 // From RtpTransportInternal
261 void OnWritableState(bool writable);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800262
Zhi Huang942bc2e2017-11-13 13:26:07 -0800263 void OnNetworkRouteChanged(rtc::Optional<rtc::NetworkRoute> network_route);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700264
deadbeef5bd5ca32017-02-10 11:31:50 -0800265 bool PacketIsRtcp(const rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700266 const char* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700268 bool SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700269 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700270 const rtc::PacketOptions& options);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200271
deadbeef953c2ce2017-01-09 14:53:41 -0800272 bool WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet);
jbaucheec21bd2016-03-20 06:15:43 -0700273 void HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000274 const rtc::PacketTime& packet_time);
zstein3dcf0e92017-06-01 13:22:42 -0700275 // TODO(zstein): packet can be const once the RtpTransport handles protection.
276 virtual void OnPacketReceived(bool rtcp,
zstein634977b2017-07-14 12:30:04 -0700277 rtc::CopyOnWriteBuffer* packet,
zstein3dcf0e92017-06-01 13:22:42 -0700278 const rtc::PacketTime& packet_time);
279 void ProcessPacket(bool rtcp,
280 const rtc::CopyOnWriteBuffer& packet,
281 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 void EnableMedia_w();
284 void DisableMedia_w();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700285
286 // Performs actions if the RTP/RTCP writable state changed. This should
287 // be called whenever a channel's writable state changes or when RTCP muxing
288 // becomes active/inactive.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200289 void UpdateWritableState_n();
290 void ChannelWritable_n();
291 void ChannelNotWritable_n();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700292
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200294 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000295 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200296 bool RemoveSendStream_w(uint32_t ssrc);
deadbeef953c2ce2017-01-09 14:53:41 -0800297 bool ShouldSetupDtlsSrtp_n() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
299 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
zhihuangb2cdd932017-01-19 16:54:25 -0800300 bool SetupDtlsSrtp_n(bool rtcp);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200301 void MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700303 // Should be called whenever the conditions for
304 // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied).
305 // Updates the send/recv state of the media channel.
306 void UpdateMediaSendRecvState();
307 virtual void UpdateMediaSendRecvState_w() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
Steve Anton3828c062017-12-06 10:34:51 -0800310 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000311 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
Steve Anton3828c062017-12-06 10:34:51 -0800313 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000314 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 virtual bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800316 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000317 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800319 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000320 std::string* error_desc) = 0;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200321 bool SetRtpTransportParameters(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800322 webrtc::SdpType type,
323 ContentSource src,
324 const RtpHeaderExtensions& extensions,
325 std::string* error_desc);
326 bool SetRtpTransportParameters_n(
327 const MediaContentDescription* content,
328 webrtc::SdpType type,
329 ContentSource src,
jbauch5869f502017-06-29 12:31:36 -0700330 const std::vector<int>& encrypted_extension_ids,
331 std::string* error_desc);
332
333 // Return a list of RTP header extensions with the non-encrypted extensions
334 // removed depending on the current crypto_options_ and only if both the
335 // non-encrypted and encrypted extension is present for the same URI.
336 RtpHeaderExtensions GetFilteredRtpHeaderExtensions(
337 const RtpHeaderExtensions& extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000339 // Helper method to get RTP Absoulute SendTime extension header id if
340 // present in remote supported extensions list.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200341 void MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -0700342 const std::vector<webrtc::RtpExtension>& extensions);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000343
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200344 bool CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
345 bool* dtls,
346 std::string* error_desc);
347 bool SetSrtp_n(const std::vector<CryptoParams>& params,
Steve Anton3828c062017-12-06 10:34:51 -0800348 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000349 ContentSource src,
jbauch5869f502017-06-29 12:31:36 -0700350 const std::vector<int>& encrypted_extension_ids,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000351 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200352 bool SetRtcpMux_n(bool enable,
Steve Anton3828c062017-12-06 10:34:51 -0800353 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000354 ContentSource src,
355 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356
357 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700358 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359
360 // Handled in derived classes
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000361 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 const std::vector<ConnectionInfo>& infos) = 0;
363
stefanf79ade12017-06-02 06:44:03 -0700364 // Helper function template for invoking methods on the worker thread.
365 template <class T, class FunctorT>
366 T InvokeOnWorker(const rtc::Location& posted_from, const FunctorT& functor) {
367 return worker_thread_->Invoke<T>(posted_from, functor);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000368 }
369
zstein3dcf0e92017-06-01 13:22:42 -0700370 void AddHandledPayloadType(int payload_type);
371
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372 private:
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800373 void ConnectToRtpTransport();
374 void DisconnectFromRtpTransport();
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800375 void SignalSentPacket_n(const rtc::SentPacket& sent_packet);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200376 void SignalSentPacket_w(const rtc::SentPacket& sent_packet);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700377 bool IsReadyToSendMedia_n() const;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200378 void CacheRtpAbsSendTimeHeaderExtension_n(int rtp_abs_sendtime_extn_id);
Zhi Huangcf990f52017-09-22 12:12:30 -0700379 // Wraps the existing RtpTransport in an SrtpTransport.
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800380 void EnableSdes_n();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200381
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800382 // Wraps the existing RtpTransport in a new SrtpTransport and wraps that in a
383 // new DtlsSrtpTransport.
384 void EnableDtlsSrtp_n();
385
386 // Update the encrypted header extension IDs when setting the local/remote
Zhi Huangc99b6c72017-11-10 16:44:46 -0800387 // description and use them later together with other crypto parameters from
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800388 // DtlsTransport. If DTLS-SRTP is enabled, it also update the encrypted header
389 // extension IDs for DtlsSrtpTransport.
390 void UpdateEncryptedHeaderExtensionIds(cricket::ContentSource source,
391 const std::vector<int>& extension_ids);
Zhi Huangc99b6c72017-11-10 16:44:46 -0800392
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800393 // Permanently enable RTCP muxing. Set null RTCP PacketTransport for
394 // BaseChannel and RtpTransport. If using DTLS-SRTP, set null DtlsTransport
395 // for DtlsSrtpTransport.
396 void ActivateRtcpMux();
Zhi Huangc99b6c72017-11-10 16:44:46 -0800397
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200398 rtc::Thread* const worker_thread_;
399 rtc::Thread* const network_thread_;
zhihuangf5b251b2017-01-12 19:37:48 -0800400 rtc::Thread* const signaling_thread_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200401 rtc::AsyncInvoker invoker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000403 const std::string content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200404 std::unique_ptr<ConnectionMonitor> connection_monitor_;
405
deadbeeff5346592017-01-24 21:51:21 -0800406 // Won't be set when using raw packet transports. SDP-specific thing.
deadbeefcbecd352015-09-23 11:50:27 -0700407 std::string transport_name_;
zhihuangb2cdd932017-01-19 16:54:25 -0800408
zstein56162b92017-04-24 16:54:35 -0700409 const bool rtcp_mux_required_;
410
deadbeeff5346592017-01-24 21:51:21 -0800411 // Separate DTLS/non-DTLS pointers to support using BaseChannel without DTLS.
412 // Temporary measure until more refactoring is done.
413 // If non-null, "X_dtls_transport_" will always equal "X_packet_transport_".
zhihuangb2cdd932017-01-19 16:54:25 -0800414 DtlsTransportInternal* rtp_dtls_transport_ = nullptr;
zhihuangb2cdd932017-01-19 16:54:25 -0800415 DtlsTransportInternal* rtcp_dtls_transport_ = nullptr;
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800416
417 webrtc::RtpTransportInternal* rtp_transport_ = nullptr;
418 // Only one of these transports is non-null at a time. One for DTLS-SRTP, one
419 // for SDES and one for unencrypted RTP.
420 std::unique_ptr<webrtc::SrtpTransport> sdes_transport_;
421 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport_;
422 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport_;
423
deadbeeff5346592017-01-24 21:51:21 -0800424 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
deadbeefcbecd352015-09-23 11:50:27 -0700425 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
Zhi Huangcf990f52017-09-22 12:12:30 -0700426 SrtpFilter sdes_negotiator_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 RtcpMuxFilter rtcp_mux_filter_;
deadbeef23d947d2016-08-22 16:00:30 -0700428 bool writable_ = false;
429 bool was_ever_writable_ = false;
430 bool has_received_packet_ = false;
deadbeef7af91dd2016-12-13 11:29:11 -0800431 const bool srtp_required_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200432
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700433 // MediaChannel related members that should be accessed from the worker
434 // thread.
Steve Anton8699a322017-11-06 15:53:33 -0800435 std::unique_ptr<MediaChannel> media_channel_;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700436 // Currently the |enabled_| flag is accessed from the signaling thread as
437 // well, but it can be changed only when signaling thread does a synchronous
438 // call to the worker thread, so it should be safe.
deadbeef23d947d2016-08-22 16:00:30 -0700439 bool enabled_ = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200440 std::vector<StreamParams> local_streams_;
441 std::vector<StreamParams> remote_streams_;
Steve Anton4e70a722017-11-28 14:57:10 -0800442 webrtc::RtpTransceiverDirection local_content_direction_ =
443 webrtc::RtpTransceiverDirection::kInactive;
444 webrtc::RtpTransceiverDirection remote_content_direction_ =
445 webrtc::RtpTransceiverDirection::kInactive;
Zhi Huangc99b6c72017-11-10 16:44:46 -0800446
447 // The cached encrypted header extension IDs.
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800448 rtc::Optional<std::vector<int>> cached_send_extension_ids_;
449 rtc::Optional<std::vector<int>> cached_recv_extension_ids_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450};
451
452// VoiceChannel is a specialization that adds support for early media, DTMF,
453// and input/output level monitoring.
454class VoiceChannel : public BaseChannel {
455 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200456 VoiceChannel(rtc::Thread* worker_thread,
457 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800458 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700459 MediaEngineInterface* media_engine,
Steve Anton8699a322017-11-06 15:53:33 -0800460 std::unique_ptr<VoiceMediaChannel> channel,
deadbeefcbecd352015-09-23 11:50:27 -0700461 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800462 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800463 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 ~VoiceChannel();
solenberg1dd98f32015-09-10 01:57:14 -0700465
466 // Configure sending media on the stream with SSRC |ssrc|
467 // If there is only one sending stream SSRC 0 can be used.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200468 bool SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -0700469 bool enable,
deadbeefcbecd352015-09-23 11:50:27 -0700470 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800471 AudioSource* source);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472
473 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200474 VoiceMediaChannel* media_channel() const override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
476 }
477
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 void SetEarlyMedia(bool enable);
479 // This signal is emitted when we have gone a period of time without
480 // receiving early media. When received, a UI should start playing its
481 // own ringing sound
482 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
483
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 // Get statistics about the current media session.
485 bool GetStats(VoiceMediaInfo* stats);
486
487 // Monitoring functions
488 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
489 SignalConnectionMonitor;
490
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 void StartAudioMonitor(int cms);
492 void StopAudioMonitor();
493 bool IsAudioMonitorRunning() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 int GetInputLevel_w();
496 int GetOutputLevel_w();
497 void GetActiveStreams_w(AudioInfo::StreamList* actives);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700498 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const;
499 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700500 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_AUDIO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 private:
503 // overrides from BaseChannel
zstein3dcf0e92017-06-01 13:22:42 -0700504 void OnPacketReceived(bool rtcp,
zstein634977b2017-07-14 12:30:04 -0700505 rtc::CopyOnWriteBuffer* packet,
zstein3dcf0e92017-06-01 13:22:42 -0700506 const rtc::PacketTime& packet_time) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700507 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200508 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800509 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200510 std::string* error_desc) override;
511 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800512 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200513 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 void HandleEarlyMediaTimeout();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200516 void OnMessage(rtc::Message* pmsg) override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200517 void OnConnectionMonitorUpdate(
518 ConnectionMonitor* monitor,
519 const std::vector<ConnectionInfo>& infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520
521 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200522 MediaEngineInterface* media_engine_;
Steve Anton8699a322017-11-06 15:53:33 -0800523 bool received_media_ = false;
kwiberg31022942016-03-11 14:18:21 -0800524 std::unique_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700525
526 // Last AudioSendParameters sent down to the media_channel() via
527 // SetSendParameters.
528 AudioSendParameters last_send_params_;
529 // Last AudioRecvParameters sent down to the media_channel() via
530 // SetRecvParameters.
531 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532};
533
534// VideoChannel is a specialization for video.
535class VideoChannel : public BaseChannel {
536 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200537 VideoChannel(rtc::Thread* worker_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800538 rtc::Thread* network_thread,
539 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 15:53:33 -0800540 std::unique_ptr<VideoMediaChannel> media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700541 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800542 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800543 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 ~VideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200546 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200547 VideoMediaChannel* media_channel() const override {
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200548 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
549 }
550
stefanf79ade12017-06-02 06:44:03 -0700551 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000553 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554
555 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
556 SignalConnectionMonitor;
557
zhihuang184a3fd2016-06-14 11:47:14 -0700558 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_VIDEO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700562 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200563 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800564 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200565 std::string* error_desc) override;
566 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800567 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200568 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 bool GetStats_w(VideoMediaInfo* stats);
570
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200571 void OnConnectionMonitorUpdate(
572 ConnectionMonitor* monitor,
573 const std::vector<ConnectionInfo>& infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700575 // Last VideoSendParameters sent down to the media_channel() via
576 // SetSendParameters.
577 VideoSendParameters last_send_params_;
578 // Last VideoRecvParameters sent down to the media_channel() via
579 // SetRecvParameters.
580 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581};
582
deadbeef953c2ce2017-01-09 14:53:41 -0800583// RtpDataChannel is a specialization for data.
584class RtpDataChannel : public BaseChannel {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 public:
deadbeef953c2ce2017-01-09 14:53:41 -0800586 RtpDataChannel(rtc::Thread* worker_thread,
587 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800588 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 15:53:33 -0800589 std::unique_ptr<DataMediaChannel> channel,
deadbeef953c2ce2017-01-09 14:53:41 -0800590 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800591 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -0800592 bool srtp_required);
593 ~RtpDataChannel();
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800594 // TODO(zhihuang): Remove this once the RtpTransport can be shared between
595 // BaseChannels.
Steve Anton8699a322017-11-06 15:53:33 -0800596 void Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800597 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800598 rtc::PacketTransportInternal* rtp_packet_transport,
599 rtc::PacketTransportInternal* rtcp_packet_transport);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800600 void Init_w(webrtc::RtpTransportInternal* rtp_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000602 virtual bool SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700603 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000604 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000606 // Should be called on the signaling thread only.
607 bool ready_to_send_data() const {
608 return ready_to_send_data_;
609 }
610
deadbeef953c2ce2017-01-09 14:53:41 -0800611 sigslot::signal2<RtpDataChannel*, const std::vector<ConnectionInfo>&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 SignalConnectionMonitor;
deadbeef953c2ce2017-01-09 14:53:41 -0800613
614 sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
615 SignalDataReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000617 // That occurs when the channel is enabled, the transport is writable,
618 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 sigslot::signal1<bool> SignalReadyToSendData;
zhihuang184a3fd2016-06-14 11:47:14 -0700620 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_DATA; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000622 protected:
623 // downcasts a MediaChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200624 DataMediaChannel* media_channel() const override {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000625 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
626 }
627
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000629 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 SendDataMessageData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700631 const rtc::CopyOnWriteBuffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 SendDataResult* result)
633 : params(params),
634 payload(payload),
635 result(result),
636 succeeded(false) {
637 }
638
639 const SendDataParams& params;
jbaucheec21bd2016-03-20 06:15:43 -0700640 const rtc::CopyOnWriteBuffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 SendDataResult* result;
642 bool succeeded;
643 };
644
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000645 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 // We copy the data because the data will become invalid after we
647 // handle DataMediaChannel::SignalDataReceived but before we fire
648 // SignalDataReceived.
649 DataReceivedMessageData(
650 const ReceiveDataParams& params, const char* data, size_t len)
651 : params(params),
652 payload(data, len) {
653 }
654 const ReceiveDataParams params;
jbaucheec21bd2016-03-20 06:15:43 -0700655 const rtc::CopyOnWriteBuffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 };
657
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000658 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000659
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 // overrides from BaseChannel
deadbeef953c2ce2017-01-09 14:53:41 -0800661 // Checks that data channel type is RTP.
662 bool CheckDataChannelTypeFromContent(const DataContentDescription* content,
663 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200664 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800665 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200666 std::string* error_desc) override;
667 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800668 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200669 std::string* error_desc) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700670 void UpdateMediaSendRecvState_w() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200672 void OnMessage(rtc::Message* pmsg) override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200673 void OnConnectionMonitorUpdate(
674 ConnectionMonitor* monitor,
675 const std::vector<ConnectionInfo>& infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 void OnDataReceived(
677 const ReceiveDataParams& params, const char* data, size_t len);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000678 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679
deadbeef953c2ce2017-01-09 14:53:41 -0800680 bool ready_to_send_data_ = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700681
682 // Last DataSendParameters sent down to the media_channel() via
683 // SetSendParameters.
684 DataSendParameters last_send_params_;
685 // Last DataRecvParameters sent down to the media_channel() via
686 // SetRecvParameters.
687 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688};
689
690} // namespace cricket
691
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200692#endif // PC_CHANNEL_H_