blob: 35369d6f60e071cd81a06522ef30dc4b28fd2064 [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
perkjc11b1842016-03-07 17:34:13 -080011#ifndef WEBRTC_PC_CHANNEL_H_
12#define WEBRTC_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
kjellandera69d9732016-08-31 07:33:05 -070021#include "webrtc/api/call/audio_sink.h"
hbos8d609f62017-04-10 07:39:05 -070022#include "webrtc/api/rtpreceiverinterface.h"
Danil Chapovalov33b01f22016-05-11 19:55:27 +020023#include "webrtc/base/asyncinvoker.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000024#include "webrtc/base/asyncudpsocket.h"
25#include "webrtc/base/criticalsection.h"
26#include "webrtc/base/network.h"
27#include "webrtc/base/sigslot.h"
28#include "webrtc/base/window.h"
kjellandera96e2d72016-02-04 23:52:28 -080029#include "webrtc/media/base/mediachannel.h"
30#include "webrtc/media/base/mediaengine.h"
31#include "webrtc/media/base/streamparams.h"
nisse08582ff2016-02-04 01:24:52 -080032#include "webrtc/media/base/videosinkinterface.h"
nisse2ded9b12016-04-08 02:23:55 -070033#include "webrtc/media/base/videosourceinterface.h"
deadbeeff5346592017-01-24 21:51:21 -080034#include "webrtc/p2p/base/dtlstransportinternal.h"
deadbeef5bd5ca32017-02-10 11:31:50 -080035#include "webrtc/p2p/base/packettransportinternal.h"
Tommif888bb52015-12-12 01:37:01 +010036#include "webrtc/p2p/base/transportcontroller.h"
37#include "webrtc/p2p/client/socketmonitor.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010038#include "webrtc/pc/audiomonitor.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010039#include "webrtc/pc/mediamonitor.h"
40#include "webrtc/pc/mediasession.h"
41#include "webrtc/pc/rtcpmuxfilter.h"
zsteind48dbda2017-04-04 19:45:57 -070042#include "webrtc/pc/rtptransport.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010043#include "webrtc/pc/srtpfilter.h"
Tommif888bb52015-12-12 01:37:01 +010044
45namespace webrtc {
46class AudioSinkInterface;
47} // namespace webrtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49namespace cricket {
50
51struct CryptoParams;
52class MediaContentDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
deadbeef062ce9f2016-08-26 21:42:15 -070054// BaseChannel contains logic common to voice and video, including enable,
55// marshaling calls to a worker and network threads, and connection and media
56// monitors.
57//
Danil Chapovalov33b01f22016-05-11 19:55:27 +020058// BaseChannel assumes signaling and other threads are allowed to make
59// synchronous calls to the worker thread, the worker thread makes synchronous
60// calls only to the network thread, and the network thread can't be blocked by
61// other threads.
62// All methods with _n suffix must be called on network thread,
deadbeef062ce9f2016-08-26 21:42:15 -070063// methods with _w suffix on worker thread
Danil Chapovalov33b01f22016-05-11 19:55:27 +020064// and methods with _s suffix on signaling thread.
65// Network and worker threads may be the same thread.
wu@webrtc.org78187522013-10-07 23:32:02 +000066//
67// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
68// This is required to avoid a data race between the destructor modifying the
69// vtable, and the media channel's thread using BaseChannel as the
70// NetworkInterface.
71
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073 : public rtc::MessageHandler, public sigslot::has_slots<>,
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000074 public MediaChannel::NetworkInterface,
75 public ConnectionStatsGetter {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 public:
deadbeef7af91dd2016-12-13 11:29:11 -080077 // If |srtp_required| is true, the channel will not send or receive any
78 // RTP/RTCP packets without using SRTP (either using SDES or DTLS-SRTP).
Danil Chapovalov33b01f22016-05-11 19:55:27 +020079 BaseChannel(rtc::Thread* worker_thread,
80 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -080081 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -070082 MediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -070083 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -080084 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -080085 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 virtual ~BaseChannel();
zhihuangb2cdd932017-01-19 16:54:25 -080087 bool Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -080088 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -080089 rtc::PacketTransportInternal* rtp_packet_transport,
90 rtc::PacketTransportInternal* rtcp_packet_transport);
Danil Chapovalov33b01f22016-05-11 19:55:27 +020091 // Deinit may be called multiple times and is simply ignored if it's already
wu@webrtc.org78187522013-10-07 23:32:02 +000092 // done.
93 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000095 rtc::Thread* worker_thread() const { return worker_thread_; }
Danil Chapovalov33b01f22016-05-11 19:55:27 +020096 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070097 const std::string& content_name() const { return content_name_; }
deadbeeff5346592017-01-24 21:51:21 -080098 // TODO(deadbeef): This is redundant; remove this.
deadbeefcbecd352015-09-23 11:50:27 -070099 const std::string& transport_name() const { return transport_name_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
102 // This function returns true if we are using SRTP.
103 bool secure() const { return srtp_filter_.IsActive(); }
104 // The following function returns true if we are using
105 // DTLS-based keying. If you turned off SRTP later, however
106 // you could have secure() == false and dtls_secure() == true.
107 bool secure_dtls() const { return dtls_keyed_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108
109 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110
deadbeefbad5dad2017-01-17 18:32:35 -0800111 // Set the transport(s), and update writability and "ready-to-send" state.
112 // |rtp_transport| must be non-null.
113 // |rtcp_transport| must be supplied if NeedsRtcpTransport() is true (meaning
114 // RTCP muxing is not fully active yet).
115 // |rtp_transport| and |rtcp_transport| must share the same transport name as
116 // well.
deadbeef5bd5ca32017-02-10 11:31:50 -0800117 // Can not start with "rtc::PacketTransportInternal" and switch to
deadbeeff5346592017-01-24 21:51:21 -0800118 // "DtlsTransportInternal", or vice-versa.
zhihuangb2cdd932017-01-19 16:54:25 -0800119 void SetTransports(DtlsTransportInternal* rtp_dtls_transport,
120 DtlsTransportInternal* rtcp_dtls_transport);
deadbeef5bd5ca32017-02-10 11:31:50 -0800121 void SetTransports(rtc::PacketTransportInternal* rtp_packet_transport,
122 rtc::PacketTransportInternal* rtcp_packet_transport);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000123 bool PushdownLocalDescription(const SessionDescription* local_desc,
124 ContentAction action,
125 std::string* error_desc);
126 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
127 ContentAction action,
128 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 // Channel control
130 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000131 ContentAction action,
132 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000134 ContentAction action,
135 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
137 bool Enable(bool enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138
139 // Multiplexing
140 bool AddRecvStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200141 bool RemoveRecvStream(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000142 bool AddSendStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200143 bool RemoveSendStream(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
145 // Monitoring
146 void StartConnectionMonitor(int cms);
147 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000148 // For ConnectionStatsGetter, used by ConnectionMonitor
deadbeefcbecd352015-09-23 11:50:27 -0700149 bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 const std::vector<StreamParams>& local_streams() const {
152 return local_streams_;
153 }
154 const std::vector<StreamParams>& remote_streams() const {
155 return remote_streams_;
156 }
157
deadbeef953c2ce2017-01-09 14:53:41 -0800158 sigslot::signal2<BaseChannel*, bool> SignalDtlsSrtpSetupFailure;
159 void SignalDtlsSrtpSetupFailure_n(bool rtcp);
160 void SignalDtlsSrtpSetupFailure_s(bool rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000161
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000162 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
164
zhihuangb2cdd932017-01-19 16:54:25 -0800165 // Forward SignalSentPacket to worker thread.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200166 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket;
167
deadbeefac22f702017-01-12 21:59:29 -0800168 // Emitted whenever rtcp-mux is fully negotiated and the rtcp-transport can
169 // be destroyed.
170 // Fired on the network thread.
171 sigslot::signal1<const std::string&> SignalRtcpMuxFullyActive;
zhihuangf5b251b2017-01-12 19:37:48 -0800172
zhihuangb2cdd932017-01-19 16:54:25 -0800173 // Only public for unit tests. Otherwise, consider private.
174 DtlsTransportInternal* rtp_dtls_transport() const {
175 return rtp_dtls_transport_;
176 }
177 DtlsTransportInternal* rtcp_dtls_transport() const {
178 return rtcp_dtls_transport_;
179 }
zhihuangf5b251b2017-01-12 19:37:48 -0800180
181 bool NeedsRtcpTransport();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200182
zstein56162b92017-04-24 16:54:35 -0700183 // From RtpTransport - public for testing only
184 void OnTransportReadyToSend(bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000186 // Only public for unit tests. Otherwise, consider protected.
rlesterec9d1872015-10-27 14:22:16 -0700187 int SetOption(SocketType type, rtc::Socket::Option o, int val)
188 override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200189 int SetOption_n(SocketType type, rtc::Socket::Option o, int val);
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000190
solenberg5b14b422015-10-01 04:10:31 -0700191 SrtpFilter* srtp_filter() { return &srtp_filter_; }
192
zhihuang184a3fd2016-06-14 11:47:14 -0700193 virtual cricket::MediaType media_type() = 0;
194
deadbeef7af91dd2016-12-13 11:29:11 -0800195 // This function returns true if we require SRTP for call setup.
196 bool srtp_required_for_testing() const { return srtp_required_; }
197
zstein3dcf0e92017-06-01 13:22:42 -0700198 // Public for testing.
199 // TODO(zstein): Remove this once channels register themselves with
200 // an RtpTransport in a more explicit way.
201 bool HandlesPayloadType(int payload_type) const;
202
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 virtual MediaChannel* media_channel() const { return media_channel_; }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700205
zhihuangb2cdd932017-01-19 16:54:25 -0800206 void SetTransports_n(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800207 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800208 rtc::PacketTransportInternal* rtp_packet_transport,
209 rtc::PacketTransportInternal* rtcp_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800210
deadbeef062ce9f2016-08-26 21:42:15 -0700211 // This does not update writability or "ready-to-send" state; it just
212 // disconnects from the old channel and connects to the new one.
deadbeeff5346592017-01-24 21:51:21 -0800213 void SetTransport_n(bool rtcp,
214 DtlsTransportInternal* new_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800215 rtc::PacketTransportInternal* new_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800216
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 bool was_ever_writable() const { return was_ever_writable_; }
218 void set_local_content_direction(MediaContentDirection direction) {
219 local_content_direction_ = direction;
220 }
221 void set_remote_content_direction(MediaContentDirection direction) {
222 remote_content_direction_ = direction;
223 }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700224 // These methods verify that:
225 // * The required content description directions have been set.
226 // * The channel is enabled.
227 // * And for sending:
228 // - The SRTP filter is active if it's needed.
229 // - The transport has been writable before, meaning it should be at least
230 // possible to succeed in sending a packet.
231 //
232 // When any of these properties change, UpdateMediaSendRecvState_w should be
233 // called.
234 bool IsReadyToReceiveMedia_w() const;
235 bool IsReadyToSendMedia_w() const;
zhihuangf5b251b2017-01-12 19:37:48 -0800236 rtc::Thread* signaling_thread() { return signaling_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237
deadbeeff5346592017-01-24 21:51:21 -0800238 void ConnectToDtlsTransport(DtlsTransportInternal* transport);
239 void DisconnectFromDtlsTransport(DtlsTransportInternal* transport);
deadbeef5bd5ca32017-02-10 11:31:50 -0800240 void ConnectToPacketTransport(rtc::PacketTransportInternal* transport);
241 void DisconnectFromPacketTransport(rtc::PacketTransportInternal* transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000242
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200243 void FlushRtcpMessages_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244
245 // NetworkInterface implementation, called by MediaEngine
jbaucheec21bd2016-03-20 06:15:43 -0700246 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
247 const rtc::PacketOptions& options) override;
248 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
249 const rtc::PacketOptions& options) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250
251 // From TransportChannel
deadbeef5bd5ca32017-02-10 11:31:50 -0800252 void OnWritableState(rtc::PacketTransportInternal* transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253
zhihuangb2cdd932017-01-19 16:54:25 -0800254 void OnDtlsState(DtlsTransportInternal* transport, DtlsTransportState state);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800255
Honghai Zhangcc411c02016-03-29 17:27:21 -0700256 void OnSelectedCandidatePairChanged(
zhihuangb2cdd932017-01-19 16:54:25 -0800257 IceTransportInternal* ice_transport,
Honghai Zhang52dce732016-03-31 12:37:31 -0700258 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700259 int last_sent_packet_id,
260 bool ready_to_send);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700261
deadbeef5bd5ca32017-02-10 11:31:50 -0800262 bool PacketIsRtcp(const rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700263 const char* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700265 bool SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700266 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700267 const rtc::PacketOptions& options);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200268
deadbeef953c2ce2017-01-09 14:53:41 -0800269 bool WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet);
jbaucheec21bd2016-03-20 06:15:43 -0700270 void HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000271 const rtc::PacketTime& packet_time);
zstein3dcf0e92017-06-01 13:22:42 -0700272 // TODO(zstein): packet can be const once the RtpTransport handles protection.
273 virtual void OnPacketReceived(bool rtcp,
274 rtc::CopyOnWriteBuffer& packet,
275 const rtc::PacketTime& packet_time);
276 void ProcessPacket(bool rtcp,
277 const rtc::CopyOnWriteBuffer& packet,
278 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 void EnableMedia_w();
281 void DisableMedia_w();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700282
283 // Performs actions if the RTP/RTCP writable state changed. This should
284 // be called whenever a channel's writable state changes or when RTCP muxing
285 // becomes active/inactive.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200286 void UpdateWritableState_n();
287 void ChannelWritable_n();
288 void ChannelNotWritable_n();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700289
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200291 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000292 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200293 bool RemoveSendStream_w(uint32_t ssrc);
deadbeef953c2ce2017-01-09 14:53:41 -0800294 bool ShouldSetupDtlsSrtp_n() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
296 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
zhihuangb2cdd932017-01-19 16:54:25 -0800297 bool SetupDtlsSrtp_n(bool rtcp);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200298 void MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700300 // Should be called whenever the conditions for
301 // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied).
302 // Updates the send/recv state of the media channel.
303 void UpdateMediaSendRecvState();
304 virtual void UpdateMediaSendRecvState_w() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305
306 // Gets the content info appropriate to the channel (audio or video).
307 virtual const ContentInfo* GetFirstContent(
308 const SessionDescription* sdesc) = 0;
309 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000310 ContentAction action,
311 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000313 ContentAction action,
314 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000316 ContentAction action,
317 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000319 ContentAction action,
320 std::string* error_desc) = 0;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200321 bool SetRtpTransportParameters(const MediaContentDescription* content,
322 ContentAction action,
323 ContentSource src,
324 std::string* error_desc);
325 bool SetRtpTransportParameters_n(const MediaContentDescription* content,
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700326 ContentAction action,
327 ContentSource src,
328 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000330 // Helper method to get RTP Absoulute SendTime extension header id if
331 // present in remote supported extensions list.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200332 void MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -0700333 const std::vector<webrtc::RtpExtension>& extensions);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000334
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200335 bool CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
336 bool* dtls,
337 std::string* error_desc);
338 bool SetSrtp_n(const std::vector<CryptoParams>& params,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339 ContentAction action,
340 ContentSource src,
341 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200342 bool SetRtcpMux_n(bool enable,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 ContentAction action,
344 ContentSource src,
345 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346
347 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700348 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349
350 // Handled in derived classes
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000351 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 const std::vector<ConnectionInfo>& infos) = 0;
353
stefanf79ade12017-06-02 06:44:03 -0700354 // Helper function template for invoking methods on the worker thread.
355 template <class T, class FunctorT>
356 T InvokeOnWorker(const rtc::Location& posted_from, const FunctorT& functor) {
357 return worker_thread_->Invoke<T>(posted_from, functor);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000358 }
359
zstein3dcf0e92017-06-01 13:22:42 -0700360 void AddHandledPayloadType(int payload_type);
361
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 private:
zhihuangb2cdd932017-01-19 16:54:25 -0800363 bool InitNetwork_n(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800364 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800365 rtc::PacketTransportInternal* rtp_packet_transport,
366 rtc::PacketTransportInternal* rtcp_packet_transport);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200367 void DisconnectTransportChannels_n();
deadbeef5bd5ca32017-02-10 11:31:50 -0800368 void SignalSentPacket_n(rtc::PacketTransportInternal* transport,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200369 const rtc::SentPacket& sent_packet);
370 void SignalSentPacket_w(const rtc::SentPacket& sent_packet);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700371 bool IsReadyToSendMedia_n() const;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200372 void CacheRtpAbsSendTimeHeaderExtension_n(int rtp_abs_sendtime_extn_id);
michaelt79e05882016-11-08 02:50:09 -0800373 int GetTransportOverheadPerPacket() const;
374 void UpdateTransportOverhead();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200375
376 rtc::Thread* const worker_thread_;
377 rtc::Thread* const network_thread_;
zhihuangf5b251b2017-01-12 19:37:48 -0800378 rtc::Thread* const signaling_thread_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200379 rtc::AsyncInvoker invoker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000381 const std::string content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200382 std::unique_ptr<ConnectionMonitor> connection_monitor_;
383
deadbeeff5346592017-01-24 21:51:21 -0800384 // Won't be set when using raw packet transports. SDP-specific thing.
deadbeefcbecd352015-09-23 11:50:27 -0700385 std::string transport_name_;
zhihuangb2cdd932017-01-19 16:54:25 -0800386
zstein56162b92017-04-24 16:54:35 -0700387 const bool rtcp_mux_required_;
388
deadbeeff5346592017-01-24 21:51:21 -0800389 // Separate DTLS/non-DTLS pointers to support using BaseChannel without DTLS.
390 // Temporary measure until more refactoring is done.
391 // If non-null, "X_dtls_transport_" will always equal "X_packet_transport_".
zhihuangb2cdd932017-01-19 16:54:25 -0800392 DtlsTransportInternal* rtp_dtls_transport_ = nullptr;
zhihuangb2cdd932017-01-19 16:54:25 -0800393 DtlsTransportInternal* rtcp_dtls_transport_ = nullptr;
zsteind48dbda2017-04-04 19:45:57 -0700394 webrtc::RtpTransport rtp_transport_;
deadbeeff5346592017-01-24 21:51:21 -0800395 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
deadbeefcbecd352015-09-23 11:50:27 -0700396 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 SrtpFilter srtp_filter_;
398 RtcpMuxFilter rtcp_mux_filter_;
deadbeef23d947d2016-08-22 16:00:30 -0700399 bool writable_ = false;
400 bool was_ever_writable_ = false;
401 bool has_received_packet_ = false;
402 bool dtls_keyed_ = false;
deadbeef7af91dd2016-12-13 11:29:11 -0800403 const bool srtp_required_ = true;
deadbeef23d947d2016-08-22 16:00:30 -0700404 int rtp_abs_sendtime_extn_id_ = -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200405
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700406 // MediaChannel related members that should be accessed from the worker
407 // thread.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200408 MediaChannel* const media_channel_;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700409 // Currently the |enabled_| flag is accessed from the signaling thread as
410 // well, but it can be changed only when signaling thread does a synchronous
411 // call to the worker thread, so it should be safe.
deadbeef23d947d2016-08-22 16:00:30 -0700412 bool enabled_ = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200413 std::vector<StreamParams> local_streams_;
414 std::vector<StreamParams> remote_streams_;
deadbeef23d947d2016-08-22 16:00:30 -0700415 MediaContentDirection local_content_direction_ = MD_INACTIVE;
416 MediaContentDirection remote_content_direction_ = MD_INACTIVE;
michaelt79e05882016-11-08 02:50:09 -0800417 CandidatePairInterface* selected_candidate_pair_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418};
419
420// VoiceChannel is a specialization that adds support for early media, DTMF,
421// and input/output level monitoring.
422class VoiceChannel : public BaseChannel {
423 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200424 VoiceChannel(rtc::Thread* worker_thread,
425 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800426 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700427 MediaEngineInterface* media_engine,
428 VoiceMediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -0700429 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800430 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800431 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 ~VoiceChannel();
solenberg1dd98f32015-09-10 01:57:14 -0700433
434 // Configure sending media on the stream with SSRC |ssrc|
435 // If there is only one sending stream SSRC 0 can be used.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200436 bool SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -0700437 bool enable,
deadbeefcbecd352015-09-23 11:50:27 -0700438 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800439 AudioSource* source);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440
441 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200442 VoiceMediaChannel* media_channel() const override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
444 }
445
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 void SetEarlyMedia(bool enable);
447 // This signal is emitted when we have gone a period of time without
448 // receiving early media. When received, a UI should start playing its
449 // own ringing sound
450 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
451
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 // Returns if the telephone-event has been negotiated.
453 bool CanInsertDtmf();
454 // Send and/or play a DTMF |event| according to the |flags|.
455 // The DTMF out-of-band signal will be used on sending.
456 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000457 // The valid value for the |event| are 0 which corresponding to DTMF
458 // event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800459 bool InsertDtmf(uint32_t ssrc, int event_code, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700460 bool SetOutputVolume(uint32_t ssrc, double volume);
deadbeef2d110be2016-01-13 12:00:26 -0800461 void SetRawAudioSink(uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -0800462 std::unique_ptr<webrtc::AudioSinkInterface> sink);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700463 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const;
464 bool SetRtpSendParameters(uint32_t ssrc,
465 const webrtc::RtpParameters& parameters);
466 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const;
467 bool SetRtpReceiveParameters(uint32_t ssrc,
468 const webrtc::RtpParameters& parameters);
Tommif888bb52015-12-12 01:37:01 +0100469
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 // Get statistics about the current media session.
471 bool GetStats(VoiceMediaInfo* stats);
472
hbos8d609f62017-04-10 07:39:05 -0700473 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const;
474
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 // Monitoring functions
476 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
477 SignalConnectionMonitor;
478
479 void StartMediaMonitor(int cms);
480 void StopMediaMonitor();
481 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
482
483 void StartAudioMonitor(int cms);
484 void StopAudioMonitor();
485 bool IsAudioMonitorRunning() const;
486 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
487
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 int GetInputLevel_w();
489 int GetOutputLevel_w();
490 void GetActiveStreams_w(AudioInfo::StreamList* actives);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700491 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const;
492 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
493 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const;
494 bool SetRtpReceiveParameters_w(uint32_t ssrc,
495 webrtc::RtpParameters parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700496 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_AUDIO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 private:
499 // overrides from BaseChannel
zstein3dcf0e92017-06-01 13:22:42 -0700500 void OnPacketReceived(bool rtcp,
501 rtc::CopyOnWriteBuffer& packet,
502 const rtc::PacketTime& packet_time) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700503 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200504 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
505 bool SetLocalContent_w(const MediaContentDescription* content,
506 ContentAction action,
507 std::string* error_desc) override;
508 bool SetRemoteContent_w(const MediaContentDescription* content,
509 ContentAction action,
510 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 void HandleEarlyMediaTimeout();
solenberg1d63dd02015-12-02 12:35:09 -0800512 bool InsertDtmf_w(uint32_t ssrc, int event, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700513 bool SetOutputVolume_w(uint32_t ssrc, double volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200515 void OnMessage(rtc::Message* pmsg) override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200516 void OnConnectionMonitorUpdate(
517 ConnectionMonitor* monitor,
518 const std::vector<ConnectionInfo>& infos) override;
519 void OnMediaMonitorUpdate(VoiceMediaChannel* media_channel,
520 const VoiceMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522
523 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200524 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 bool received_media_;
kwiberg31022942016-03-11 14:18:21 -0800526 std::unique_ptr<VoiceMediaMonitor> media_monitor_;
527 std::unique_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700528
529 // Last AudioSendParameters sent down to the media_channel() via
530 // SetSendParameters.
531 AudioSendParameters last_send_params_;
532 // Last AudioRecvParameters sent down to the media_channel() via
533 // SetRecvParameters.
534 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535};
536
537// VideoChannel is a specialization for video.
538class VideoChannel : public BaseChannel {
539 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200540 VideoChannel(rtc::Thread* worker_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800541 rtc::Thread* network_thread,
542 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700543 VideoMediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -0700544 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800545 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800546 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 ~VideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200549 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200550 VideoMediaChannel* media_channel() const override {
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200551 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
552 }
553
nisseacd935b2016-11-11 03:55:13 -0800554 bool SetSink(uint32_t ssrc,
555 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
stefanf79ade12017-06-02 06:44:03 -0700556 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000558 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559
560 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
561 SignalConnectionMonitor;
562
563 void StartMediaMonitor(int cms);
564 void StopMediaMonitor();
565 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566
deadbeef5a4a75a2016-06-02 16:23:38 -0700567 // Register a source and set options.
568 // The |ssrc| must correspond to a registered send stream.
569 bool SetVideoSend(uint32_t ssrc,
570 bool enable,
571 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -0800572 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700573 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const;
574 bool SetRtpSendParameters(uint32_t ssrc,
575 const webrtc::RtpParameters& parameters);
576 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const;
577 bool SetRtpReceiveParameters(uint32_t ssrc,
578 const webrtc::RtpParameters& parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700579 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_VIDEO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700583 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200584 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
585 bool SetLocalContent_w(const MediaContentDescription* content,
586 ContentAction action,
587 std::string* error_desc) override;
588 bool SetRemoteContent_w(const MediaContentDescription* content,
589 ContentAction action,
590 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 bool GetStats_w(VideoMediaInfo* stats);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700592 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const;
593 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
594 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const;
595 bool SetRtpReceiveParameters_w(uint32_t ssrc,
596 webrtc::RtpParameters parameters);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200598 void OnMessage(rtc::Message* pmsg) override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200599 void OnConnectionMonitorUpdate(
600 ConnectionMonitor* monitor,
601 const std::vector<ConnectionInfo>& infos) override;
602 void OnMediaMonitorUpdate(VideoMediaChannel* media_channel,
603 const VideoMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604
kwiberg31022942016-03-11 14:18:21 -0800605 std::unique_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700607 // Last VideoSendParameters sent down to the media_channel() via
608 // SetSendParameters.
609 VideoSendParameters last_send_params_;
610 // Last VideoRecvParameters sent down to the media_channel() via
611 // SetRecvParameters.
612 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613};
614
deadbeef953c2ce2017-01-09 14:53:41 -0800615// RtpDataChannel is a specialization for data.
616class RtpDataChannel : public BaseChannel {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 public:
deadbeef953c2ce2017-01-09 14:53:41 -0800618 RtpDataChannel(rtc::Thread* worker_thread,
619 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800620 rtc::Thread* signaling_thread,
621 DataMediaChannel* channel,
deadbeef953c2ce2017-01-09 14:53:41 -0800622 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800623 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -0800624 bool srtp_required);
625 ~RtpDataChannel();
zhihuangb2cdd932017-01-19 16:54:25 -0800626 bool Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800627 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800628 rtc::PacketTransportInternal* rtp_packet_transport,
629 rtc::PacketTransportInternal* rtcp_packet_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000631 virtual bool SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700632 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000633 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634
635 void StartMediaMonitor(int cms);
636 void StopMediaMonitor();
637
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000638 // Should be called on the signaling thread only.
639 bool ready_to_send_data() const {
640 return ready_to_send_data_;
641 }
642
deadbeef953c2ce2017-01-09 14:53:41 -0800643 sigslot::signal2<RtpDataChannel*, const DataMediaInfo&> SignalMediaMonitor;
644 sigslot::signal2<RtpDataChannel*, const std::vector<ConnectionInfo>&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 SignalConnectionMonitor;
deadbeef953c2ce2017-01-09 14:53:41 -0800646
647 sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
648 SignalDataReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000650 // That occurs when the channel is enabled, the transport is writable,
651 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 sigslot::signal1<bool> SignalReadyToSendData;
zhihuang184a3fd2016-06-14 11:47:14 -0700653 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_DATA; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000655 protected:
656 // downcasts a MediaChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200657 DataMediaChannel* media_channel() const override {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000658 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
659 }
660
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000662 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 SendDataMessageData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700664 const rtc::CopyOnWriteBuffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 SendDataResult* result)
666 : params(params),
667 payload(payload),
668 result(result),
669 succeeded(false) {
670 }
671
672 const SendDataParams& params;
jbaucheec21bd2016-03-20 06:15:43 -0700673 const rtc::CopyOnWriteBuffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 SendDataResult* result;
675 bool succeeded;
676 };
677
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000678 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 // We copy the data because the data will become invalid after we
680 // handle DataMediaChannel::SignalDataReceived but before we fire
681 // SignalDataReceived.
682 DataReceivedMessageData(
683 const ReceiveDataParams& params, const char* data, size_t len)
684 : params(params),
685 payload(data, len) {
686 }
687 const ReceiveDataParams params;
jbaucheec21bd2016-03-20 06:15:43 -0700688 const rtc::CopyOnWriteBuffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 };
690
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000691 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000692
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 // overrides from BaseChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200694 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
deadbeef953c2ce2017-01-09 14:53:41 -0800695 // Checks that data channel type is RTP.
696 bool CheckDataChannelTypeFromContent(const DataContentDescription* content,
697 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200698 bool SetLocalContent_w(const MediaContentDescription* content,
699 ContentAction action,
700 std::string* error_desc) override;
701 bool SetRemoteContent_w(const MediaContentDescription* content,
702 ContentAction action,
703 std::string* error_desc) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700704 void UpdateMediaSendRecvState_w() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200706 void OnMessage(rtc::Message* pmsg) override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200707 void OnConnectionMonitorUpdate(
708 ConnectionMonitor* monitor,
709 const std::vector<ConnectionInfo>& infos) override;
710 void OnMediaMonitorUpdate(DataMediaChannel* media_channel,
711 const DataMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 void OnDataReceived(
713 const ReceiveDataParams& params, const char* data, size_t len);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200714 void OnDataChannelError(uint32_t ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000715 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716
kwiberg31022942016-03-11 14:18:21 -0800717 std::unique_ptr<DataMediaMonitor> media_monitor_;
deadbeef953c2ce2017-01-09 14:53:41 -0800718 bool ready_to_send_data_ = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700719
720 // Last DataSendParameters sent down to the media_channel() via
721 // SetSendParameters.
722 DataSendParameters last_send_params_;
723 // Last DataRecvParameters sent down to the media_channel() via
724 // SetRecvParameters.
725 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726};
727
728} // namespace cricket
729
perkjc11b1842016-03-07 17:34:13 -0800730#endif // WEBRTC_PC_CHANNEL_H_