blob: f8cc286279ef7f60592cbe046592e96d093ee431 [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,
jbauch5869f502017-06-29 12:31:36 -0700322 ContentAction action, ContentSource src,
323 const RtpHeaderExtensions& extensions, std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200324 bool SetRtpTransportParameters_n(const MediaContentDescription* content,
jbauch5869f502017-06-29 12:31:36 -0700325 ContentAction action, ContentSource src,
326 const std::vector<int>& encrypted_extension_ids,
327 std::string* error_desc);
328
329 // Return a list of RTP header extensions with the non-encrypted extensions
330 // removed depending on the current crypto_options_ and only if both the
331 // non-encrypted and encrypted extension is present for the same URI.
332 RtpHeaderExtensions GetFilteredRtpHeaderExtensions(
333 const RtpHeaderExtensions& extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000335 // Helper method to get RTP Absoulute SendTime extension header id if
336 // present in remote supported extensions list.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200337 void MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -0700338 const std::vector<webrtc::RtpExtension>& extensions);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000339
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200340 bool CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
341 bool* dtls,
342 std::string* error_desc);
343 bool SetSrtp_n(const std::vector<CryptoParams>& params,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000344 ContentAction action,
345 ContentSource src,
jbauch5869f502017-06-29 12:31:36 -0700346 const std::vector<int>& encrypted_extension_ids,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000347 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200348 bool SetRtcpMux_n(bool enable,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000349 ContentAction action,
350 ContentSource src,
351 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352
353 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700354 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355
356 // Handled in derived classes
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000357 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 const std::vector<ConnectionInfo>& infos) = 0;
359
stefanf79ade12017-06-02 06:44:03 -0700360 // Helper function template for invoking methods on the worker thread.
361 template <class T, class FunctorT>
362 T InvokeOnWorker(const rtc::Location& posted_from, const FunctorT& functor) {
363 return worker_thread_->Invoke<T>(posted_from, functor);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000364 }
365
zstein3dcf0e92017-06-01 13:22:42 -0700366 void AddHandledPayloadType(int payload_type);
367
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 private:
zhihuangb2cdd932017-01-19 16:54:25 -0800369 bool InitNetwork_n(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800370 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800371 rtc::PacketTransportInternal* rtp_packet_transport,
372 rtc::PacketTransportInternal* rtcp_packet_transport);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200373 void DisconnectTransportChannels_n();
deadbeef5bd5ca32017-02-10 11:31:50 -0800374 void SignalSentPacket_n(rtc::PacketTransportInternal* transport,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200375 const rtc::SentPacket& sent_packet);
376 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);
michaelt79e05882016-11-08 02:50:09 -0800379 int GetTransportOverheadPerPacket() const;
380 void UpdateTransportOverhead();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200381
382 rtc::Thread* const worker_thread_;
383 rtc::Thread* const network_thread_;
zhihuangf5b251b2017-01-12 19:37:48 -0800384 rtc::Thread* const signaling_thread_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200385 rtc::AsyncInvoker invoker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000387 const std::string content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200388 std::unique_ptr<ConnectionMonitor> connection_monitor_;
389
deadbeeff5346592017-01-24 21:51:21 -0800390 // Won't be set when using raw packet transports. SDP-specific thing.
deadbeefcbecd352015-09-23 11:50:27 -0700391 std::string transport_name_;
zhihuangb2cdd932017-01-19 16:54:25 -0800392
zstein56162b92017-04-24 16:54:35 -0700393 const bool rtcp_mux_required_;
394
deadbeeff5346592017-01-24 21:51:21 -0800395 // Separate DTLS/non-DTLS pointers to support using BaseChannel without DTLS.
396 // Temporary measure until more refactoring is done.
397 // If non-null, "X_dtls_transport_" will always equal "X_packet_transport_".
zhihuangb2cdd932017-01-19 16:54:25 -0800398 DtlsTransportInternal* rtp_dtls_transport_ = nullptr;
zhihuangb2cdd932017-01-19 16:54:25 -0800399 DtlsTransportInternal* rtcp_dtls_transport_ = nullptr;
zsteind48dbda2017-04-04 19:45:57 -0700400 webrtc::RtpTransport rtp_transport_;
deadbeeff5346592017-01-24 21:51:21 -0800401 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
deadbeefcbecd352015-09-23 11:50:27 -0700402 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 SrtpFilter srtp_filter_;
404 RtcpMuxFilter rtcp_mux_filter_;
deadbeef23d947d2016-08-22 16:00:30 -0700405 bool writable_ = false;
406 bool was_ever_writable_ = false;
407 bool has_received_packet_ = false;
408 bool dtls_keyed_ = false;
deadbeef7af91dd2016-12-13 11:29:11 -0800409 const bool srtp_required_ = true;
deadbeef23d947d2016-08-22 16:00:30 -0700410 int rtp_abs_sendtime_extn_id_ = -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200411
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700412 // MediaChannel related members that should be accessed from the worker
413 // thread.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200414 MediaChannel* const media_channel_;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700415 // Currently the |enabled_| flag is accessed from the signaling thread as
416 // well, but it can be changed only when signaling thread does a synchronous
417 // call to the worker thread, so it should be safe.
deadbeef23d947d2016-08-22 16:00:30 -0700418 bool enabled_ = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200419 std::vector<StreamParams> local_streams_;
420 std::vector<StreamParams> remote_streams_;
deadbeef23d947d2016-08-22 16:00:30 -0700421 MediaContentDirection local_content_direction_ = MD_INACTIVE;
422 MediaContentDirection remote_content_direction_ = MD_INACTIVE;
michaelt79e05882016-11-08 02:50:09 -0800423 CandidatePairInterface* selected_candidate_pair_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424};
425
426// VoiceChannel is a specialization that adds support for early media, DTMF,
427// and input/output level monitoring.
428class VoiceChannel : public BaseChannel {
429 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200430 VoiceChannel(rtc::Thread* worker_thread,
431 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800432 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700433 MediaEngineInterface* media_engine,
434 VoiceMediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -0700435 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800436 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800437 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 ~VoiceChannel();
solenberg1dd98f32015-09-10 01:57:14 -0700439
440 // Configure sending media on the stream with SSRC |ssrc|
441 // If there is only one sending stream SSRC 0 can be used.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200442 bool SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -0700443 bool enable,
deadbeefcbecd352015-09-23 11:50:27 -0700444 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800445 AudioSource* source);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446
447 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200448 VoiceMediaChannel* media_channel() const override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
450 }
451
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 void SetEarlyMedia(bool enable);
453 // This signal is emitted when we have gone a period of time without
454 // receiving early media. When received, a UI should start playing its
455 // own ringing sound
456 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
457
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 // Returns if the telephone-event has been negotiated.
459 bool CanInsertDtmf();
460 // Send and/or play a DTMF |event| according to the |flags|.
461 // The DTMF out-of-band signal will be used on sending.
462 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000463 // The valid value for the |event| are 0 which corresponding to DTMF
464 // event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800465 bool InsertDtmf(uint32_t ssrc, int event_code, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700466 bool SetOutputVolume(uint32_t ssrc, double volume);
deadbeef2d110be2016-01-13 12:00:26 -0800467 void SetRawAudioSink(uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -0800468 std::unique_ptr<webrtc::AudioSinkInterface> sink);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700469 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const;
470 bool SetRtpSendParameters(uint32_t ssrc,
471 const webrtc::RtpParameters& parameters);
472 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const;
473 bool SetRtpReceiveParameters(uint32_t ssrc,
474 const webrtc::RtpParameters& parameters);
Tommif888bb52015-12-12 01:37:01 +0100475
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 // Get statistics about the current media session.
477 bool GetStats(VoiceMediaInfo* stats);
478
hbos8d609f62017-04-10 07:39:05 -0700479 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const;
zhihuang38ede132017-06-15 12:52:32 -0700480 std::vector<webrtc::RtpSource> GetSources_w(uint32_t ssrc) const;
hbos8d609f62017-04-10 07:39:05 -0700481
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 // Monitoring functions
483 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
484 SignalConnectionMonitor;
485
486 void StartMediaMonitor(int cms);
487 void StopMediaMonitor();
488 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
489
490 void StartAudioMonitor(int cms);
491 void StopAudioMonitor();
492 bool IsAudioMonitorRunning() const;
493 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
494
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);
500 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const;
501 bool SetRtpReceiveParameters_w(uint32_t ssrc,
502 webrtc::RtpParameters parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700503 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_AUDIO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 private:
506 // overrides from BaseChannel
zstein3dcf0e92017-06-01 13:22:42 -0700507 void OnPacketReceived(bool rtcp,
508 rtc::CopyOnWriteBuffer& packet,
509 const rtc::PacketTime& packet_time) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700510 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200511 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
512 bool SetLocalContent_w(const MediaContentDescription* content,
513 ContentAction action,
514 std::string* error_desc) override;
515 bool SetRemoteContent_w(const MediaContentDescription* content,
516 ContentAction action,
517 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 void HandleEarlyMediaTimeout();
solenberg1d63dd02015-12-02 12:35:09 -0800519 bool InsertDtmf_w(uint32_t ssrc, int event, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700520 bool SetOutputVolume_w(uint32_t ssrc, double volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200522 void OnMessage(rtc::Message* pmsg) override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200523 void OnConnectionMonitorUpdate(
524 ConnectionMonitor* monitor,
525 const std::vector<ConnectionInfo>& infos) override;
526 void OnMediaMonitorUpdate(VoiceMediaChannel* media_channel,
527 const VoiceMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529
530 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200531 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 bool received_media_;
kwiberg31022942016-03-11 14:18:21 -0800533 std::unique_ptr<VoiceMediaMonitor> media_monitor_;
534 std::unique_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700535
536 // Last AudioSendParameters sent down to the media_channel() via
537 // SetSendParameters.
538 AudioSendParameters last_send_params_;
539 // Last AudioRecvParameters sent down to the media_channel() via
540 // SetRecvParameters.
541 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542};
543
544// VideoChannel is a specialization for video.
545class VideoChannel : public BaseChannel {
546 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200547 VideoChannel(rtc::Thread* worker_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800548 rtc::Thread* network_thread,
549 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700550 VideoMediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -0700551 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800552 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800553 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 ~VideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200556 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200557 VideoMediaChannel* media_channel() const override {
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200558 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
559 }
560
nisseacd935b2016-11-11 03:55:13 -0800561 bool SetSink(uint32_t ssrc,
562 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
stefanf79ade12017-06-02 06:44:03 -0700563 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000565 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566
567 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
568 SignalConnectionMonitor;
569
570 void StartMediaMonitor(int cms);
571 void StopMediaMonitor();
572 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573
deadbeef5a4a75a2016-06-02 16:23:38 -0700574 // Register a source and set options.
575 // The |ssrc| must correspond to a registered send stream.
576 bool SetVideoSend(uint32_t ssrc,
577 bool enable,
578 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -0800579 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700580 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const;
581 bool SetRtpSendParameters(uint32_t ssrc,
582 const webrtc::RtpParameters& parameters);
583 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const;
584 bool SetRtpReceiveParameters(uint32_t ssrc,
585 const webrtc::RtpParameters& parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700586 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_VIDEO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700590 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200591 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
592 bool SetLocalContent_w(const MediaContentDescription* content,
593 ContentAction action,
594 std::string* error_desc) override;
595 bool SetRemoteContent_w(const MediaContentDescription* content,
596 ContentAction action,
597 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 bool GetStats_w(VideoMediaInfo* stats);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700599 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const;
600 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
601 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const;
602 bool SetRtpReceiveParameters_w(uint32_t ssrc,
603 webrtc::RtpParameters parameters);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200605 void OnMessage(rtc::Message* pmsg) override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200606 void OnConnectionMonitorUpdate(
607 ConnectionMonitor* monitor,
608 const std::vector<ConnectionInfo>& infos) override;
609 void OnMediaMonitorUpdate(VideoMediaChannel* media_channel,
610 const VideoMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611
kwiberg31022942016-03-11 14:18:21 -0800612 std::unique_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700614 // Last VideoSendParameters sent down to the media_channel() via
615 // SetSendParameters.
616 VideoSendParameters last_send_params_;
617 // Last VideoRecvParameters sent down to the media_channel() via
618 // SetRecvParameters.
619 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620};
621
deadbeef953c2ce2017-01-09 14:53:41 -0800622// RtpDataChannel is a specialization for data.
623class RtpDataChannel : public BaseChannel {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 public:
deadbeef953c2ce2017-01-09 14:53:41 -0800625 RtpDataChannel(rtc::Thread* worker_thread,
626 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800627 rtc::Thread* signaling_thread,
628 DataMediaChannel* channel,
deadbeef953c2ce2017-01-09 14:53:41 -0800629 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800630 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -0800631 bool srtp_required);
632 ~RtpDataChannel();
zhihuangb2cdd932017-01-19 16:54:25 -0800633 bool Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800634 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800635 rtc::PacketTransportInternal* rtp_packet_transport,
636 rtc::PacketTransportInternal* rtcp_packet_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000638 virtual bool SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700639 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000640 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641
642 void StartMediaMonitor(int cms);
643 void StopMediaMonitor();
644
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000645 // Should be called on the signaling thread only.
646 bool ready_to_send_data() const {
647 return ready_to_send_data_;
648 }
649
deadbeef953c2ce2017-01-09 14:53:41 -0800650 sigslot::signal2<RtpDataChannel*, const DataMediaInfo&> SignalMediaMonitor;
651 sigslot::signal2<RtpDataChannel*, const std::vector<ConnectionInfo>&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 SignalConnectionMonitor;
deadbeef953c2ce2017-01-09 14:53:41 -0800653
654 sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
655 SignalDataReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000657 // That occurs when the channel is enabled, the transport is writable,
658 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 sigslot::signal1<bool> SignalReadyToSendData;
zhihuang184a3fd2016-06-14 11:47:14 -0700660 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_DATA; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000662 protected:
663 // downcasts a MediaChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200664 DataMediaChannel* media_channel() const override {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000665 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
666 }
667
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000669 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 SendDataMessageData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700671 const rtc::CopyOnWriteBuffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 SendDataResult* result)
673 : params(params),
674 payload(payload),
675 result(result),
676 succeeded(false) {
677 }
678
679 const SendDataParams& params;
jbaucheec21bd2016-03-20 06:15:43 -0700680 const rtc::CopyOnWriteBuffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 SendDataResult* result;
682 bool succeeded;
683 };
684
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000685 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 // We copy the data because the data will become invalid after we
687 // handle DataMediaChannel::SignalDataReceived but before we fire
688 // SignalDataReceived.
689 DataReceivedMessageData(
690 const ReceiveDataParams& params, const char* data, size_t len)
691 : params(params),
692 payload(data, len) {
693 }
694 const ReceiveDataParams params;
jbaucheec21bd2016-03-20 06:15:43 -0700695 const rtc::CopyOnWriteBuffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 };
697
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000698 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000699
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 // overrides from BaseChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200701 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
deadbeef953c2ce2017-01-09 14:53:41 -0800702 // Checks that data channel type is RTP.
703 bool CheckDataChannelTypeFromContent(const DataContentDescription* content,
704 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200705 bool SetLocalContent_w(const MediaContentDescription* content,
706 ContentAction action,
707 std::string* error_desc) override;
708 bool SetRemoteContent_w(const MediaContentDescription* content,
709 ContentAction action,
710 std::string* error_desc) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700711 void UpdateMediaSendRecvState_w() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200713 void OnMessage(rtc::Message* pmsg) override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200714 void OnConnectionMonitorUpdate(
715 ConnectionMonitor* monitor,
716 const std::vector<ConnectionInfo>& infos) override;
717 void OnMediaMonitorUpdate(DataMediaChannel* media_channel,
718 const DataMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 void OnDataReceived(
720 const ReceiveDataParams& params, const char* data, size_t len);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200721 void OnDataChannelError(uint32_t ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000722 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723
kwiberg31022942016-03-11 14:18:21 -0800724 std::unique_ptr<DataMediaMonitor> media_monitor_;
deadbeef953c2ce2017-01-09 14:53:41 -0800725 bool ready_to_send_data_ = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700726
727 // Last DataSendParameters sent down to the media_channel() via
728 // SetSendParameters.
729 DataSendParameters last_send_params_;
730 // Last DataRecvParameters sent down to the media_channel() via
731 // SetRecvParameters.
732 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733};
734
735} // namespace cricket
736
perkjc11b1842016-03-07 17:34:13 -0800737#endif // WEBRTC_PC_CHANNEL_H_