blob: bc98bc9fc89e1b8360c6e563d2ea9c644bba8490 [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"
Danil Chapovalov33b01f22016-05-11 19:55:27 +020022#include "webrtc/base/asyncinvoker.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000023#include "webrtc/base/asyncudpsocket.h"
24#include "webrtc/base/criticalsection.h"
25#include "webrtc/base/network.h"
26#include "webrtc/base/sigslot.h"
27#include "webrtc/base/window.h"
kjellandera96e2d72016-02-04 23:52:28 -080028#include "webrtc/media/base/mediachannel.h"
29#include "webrtc/media/base/mediaengine.h"
30#include "webrtc/media/base/streamparams.h"
nisse08582ff2016-02-04 01:24:52 -080031#include "webrtc/media/base/videosinkinterface.h"
nisse2ded9b12016-04-08 02:23:55 -070032#include "webrtc/media/base/videosourceinterface.h"
deadbeeff5346592017-01-24 21:51:21 -080033#include "webrtc/p2p/base/dtlstransportinternal.h"
deadbeef5bd5ca32017-02-10 11:31:50 -080034#include "webrtc/p2p/base/packettransportinternal.h"
Tommif888bb52015-12-12 01:37:01 +010035#include "webrtc/p2p/base/transportcontroller.h"
36#include "webrtc/p2p/client/socketmonitor.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010037#include "webrtc/pc/audiomonitor.h"
38#include "webrtc/pc/bundlefilter.h"
39#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
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000151 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152
153 const std::vector<StreamParams>& local_streams() const {
154 return local_streams_;
155 }
156 const std::vector<StreamParams>& remote_streams() const {
157 return remote_streams_;
158 }
159
deadbeef953c2ce2017-01-09 14:53:41 -0800160 sigslot::signal2<BaseChannel*, bool> SignalDtlsSrtpSetupFailure;
161 void SignalDtlsSrtpSetupFailure_n(bool rtcp);
162 void SignalDtlsSrtpSetupFailure_s(bool rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000163
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000164 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
166
zhihuangb2cdd932017-01-19 16:54:25 -0800167 // Forward SignalSentPacket to worker thread.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200168 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket;
169
deadbeefac22f702017-01-12 21:59:29 -0800170 // Emitted whenever rtcp-mux is fully negotiated and the rtcp-transport can
171 // be destroyed.
172 // Fired on the network thread.
173 sigslot::signal1<const std::string&> SignalRtcpMuxFullyActive;
zhihuangf5b251b2017-01-12 19:37:48 -0800174
zhihuangb2cdd932017-01-19 16:54:25 -0800175 // Only public for unit tests. Otherwise, consider private.
176 DtlsTransportInternal* rtp_dtls_transport() const {
177 return rtp_dtls_transport_;
178 }
179 DtlsTransportInternal* rtcp_dtls_transport() const {
180 return rtcp_dtls_transport_;
181 }
zhihuangf5b251b2017-01-12 19:37:48 -0800182
183 bool NeedsRtcpTransport();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200184
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 // Made public for easier testing.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700186 //
187 // Updates "ready to send" for an individual channel, and informs the media
188 // channel that the transport is ready to send if each channel (in use) is
189 // ready to send. This is more specific than just "writable"; it means the
190 // last send didn't return ENOTCONN.
191 //
192 // This should be called whenever a channel's ready-to-send state changes,
193 // or when RTCP muxing becomes active/inactive.
194 void SetTransportChannelReadyToSend(bool rtcp, bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000196 // Only public for unit tests. Otherwise, consider protected.
rlesterec9d1872015-10-27 14:22:16 -0700197 int SetOption(SocketType type, rtc::Socket::Option o, int val)
198 override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200199 int SetOption_n(SocketType type, rtc::Socket::Option o, int val);
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000200
solenberg5b14b422015-10-01 04:10:31 -0700201 SrtpFilter* srtp_filter() { return &srtp_filter_; }
202
zhihuang184a3fd2016-06-14 11:47:14 -0700203 virtual cricket::MediaType media_type() = 0;
204
jbauchcb560652016-08-04 05:20:32 -0700205 bool SetCryptoOptions(const rtc::CryptoOptions& crypto_options);
206
deadbeef7af91dd2016-12-13 11:29:11 -0800207 // This function returns true if we require SRTP for call setup.
208 bool srtp_required_for_testing() const { return srtp_required_; }
209
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 virtual MediaChannel* media_channel() const { return media_channel_; }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700212
zhihuangb2cdd932017-01-19 16:54:25 -0800213 void SetTransports_n(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800214 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800215 rtc::PacketTransportInternal* rtp_packet_transport,
216 rtc::PacketTransportInternal* rtcp_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800217
deadbeef062ce9f2016-08-26 21:42:15 -0700218 // This does not update writability or "ready-to-send" state; it just
219 // disconnects from the old channel and connects to the new one.
deadbeeff5346592017-01-24 21:51:21 -0800220 void SetTransport_n(bool rtcp,
221 DtlsTransportInternal* new_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800222 rtc::PacketTransportInternal* new_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800223
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 bool was_ever_writable() const { return was_ever_writable_; }
225 void set_local_content_direction(MediaContentDirection direction) {
226 local_content_direction_ = direction;
227 }
228 void set_remote_content_direction(MediaContentDirection direction) {
229 remote_content_direction_ = direction;
230 }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700231 // These methods verify that:
232 // * The required content description directions have been set.
233 // * The channel is enabled.
234 // * And for sending:
235 // - The SRTP filter is active if it's needed.
236 // - The transport has been writable before, meaning it should be at least
237 // possible to succeed in sending a packet.
238 //
239 // When any of these properties change, UpdateMediaSendRecvState_w should be
240 // called.
241 bool IsReadyToReceiveMedia_w() const;
242 bool IsReadyToSendMedia_w() const;
zhihuangf5b251b2017-01-12 19:37:48 -0800243 rtc::Thread* signaling_thread() { return signaling_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244
deadbeeff5346592017-01-24 21:51:21 -0800245 void ConnectToDtlsTransport(DtlsTransportInternal* transport);
246 void DisconnectFromDtlsTransport(DtlsTransportInternal* transport);
deadbeef5bd5ca32017-02-10 11:31:50 -0800247 void ConnectToPacketTransport(rtc::PacketTransportInternal* transport);
248 void DisconnectFromPacketTransport(rtc::PacketTransportInternal* transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000249
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200250 void FlushRtcpMessages_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
252 // NetworkInterface implementation, called by MediaEngine
jbaucheec21bd2016-03-20 06:15:43 -0700253 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
254 const rtc::PacketOptions& options) override;
255 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
256 const rtc::PacketOptions& options) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257
258 // From TransportChannel
deadbeef5bd5ca32017-02-10 11:31:50 -0800259 void OnWritableState(rtc::PacketTransportInternal* transport);
260 virtual void OnPacketRead(rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700261 const char* data,
262 size_t len,
263 const rtc::PacketTime& packet_time,
264 int flags);
deadbeef5bd5ca32017-02-10 11:31:50 -0800265 void OnReadyToSend(rtc::PacketTransportInternal* transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266
zhihuangb2cdd932017-01-19 16:54:25 -0800267 void OnDtlsState(DtlsTransportInternal* transport, DtlsTransportState state);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800268
Honghai Zhangcc411c02016-03-29 17:27:21 -0700269 void OnSelectedCandidatePairChanged(
zhihuangb2cdd932017-01-19 16:54:25 -0800270 IceTransportInternal* ice_transport,
Honghai Zhang52dce732016-03-31 12:37:31 -0700271 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700272 int last_sent_packet_id,
273 bool ready_to_send);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700274
deadbeef5bd5ca32017-02-10 11:31:50 -0800275 bool PacketIsRtcp(const rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700276 const char* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700278 bool SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700279 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700280 const rtc::PacketOptions& options);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200281
deadbeef953c2ce2017-01-09 14:53:41 -0800282 bool WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet);
jbaucheec21bd2016-03-20 06:15:43 -0700283 void HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000284 const rtc::PacketTime& packet_time);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200285 void OnPacketReceived(bool rtcp,
286 const rtc::CopyOnWriteBuffer& packet,
287 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 void EnableMedia_w();
290 void DisableMedia_w();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700291
292 // Performs actions if the RTP/RTCP writable state changed. This should
293 // be called whenever a channel's writable state changes or when RTCP muxing
294 // becomes active/inactive.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200295 void UpdateWritableState_n();
296 void ChannelWritable_n();
297 void ChannelNotWritable_n();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700298
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200300 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000301 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200302 bool RemoveSendStream_w(uint32_t ssrc);
deadbeef953c2ce2017-01-09 14:53:41 -0800303 bool ShouldSetupDtlsSrtp_n() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
305 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
zhihuangb2cdd932017-01-19 16:54:25 -0800306 bool SetupDtlsSrtp_n(bool rtcp);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200307 void MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
zhihuangb2cdd932017-01-19 16:54:25 -0800309 bool SetDtlsSrtpCryptoSuites_n(DtlsTransportInternal* transport, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700311 // Should be called whenever the conditions for
312 // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied).
313 // Updates the send/recv state of the media channel.
314 void UpdateMediaSendRecvState();
315 virtual void UpdateMediaSendRecvState_w() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316
317 // Gets the content info appropriate to the channel (audio or video).
318 virtual const ContentInfo* GetFirstContent(
319 const SessionDescription* sdesc) = 0;
320 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000321 ContentAction action,
322 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000324 ContentAction action,
325 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000327 ContentAction action,
328 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000330 ContentAction action,
331 std::string* error_desc) = 0;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200332 bool SetRtpTransportParameters(const MediaContentDescription* content,
333 ContentAction action,
334 ContentSource src,
335 std::string* error_desc);
336 bool SetRtpTransportParameters_n(const MediaContentDescription* content,
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700337 ContentAction action,
338 ContentSource src,
339 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000341 // Helper method to get RTP Absoulute SendTime extension header id if
342 // present in remote supported extensions list.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200343 void MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -0700344 const std::vector<webrtc::RtpExtension>& extensions);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000345
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200346 bool CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
347 bool* dtls,
348 std::string* error_desc);
349 bool SetSrtp_n(const std::vector<CryptoParams>& params,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000350 ContentAction action,
351 ContentSource src,
352 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200353 bool SetRtcpMux_n(bool enable,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000354 ContentAction action,
355 ContentSource src,
356 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357
358 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700359 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360
jbauchcb560652016-08-04 05:20:32 -0700361 const rtc::CryptoOptions& crypto_options() const {
362 return crypto_options_;
363 }
364
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 // Handled in derived classes
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800366 // Get the SRTP crypto suites to use for RTP media
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200367 virtual void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000368 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 const std::vector<ConnectionInfo>& infos) = 0;
370
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000371 // Helper function for invoking bool-returning methods on the worker thread.
372 template <class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700373 bool InvokeOnWorker(const rtc::Location& posted_from,
374 const FunctorT& functor) {
375 return worker_thread_->Invoke<bool>(posted_from, functor);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000376 }
377
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378 private:
zhihuangb2cdd932017-01-19 16:54:25 -0800379 bool InitNetwork_n(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800380 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800381 rtc::PacketTransportInternal* rtp_packet_transport,
382 rtc::PacketTransportInternal* rtcp_packet_transport);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200383 void DisconnectTransportChannels_n();
deadbeef5bd5ca32017-02-10 11:31:50 -0800384 void SignalSentPacket_n(rtc::PacketTransportInternal* transport,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200385 const rtc::SentPacket& sent_packet);
386 void SignalSentPacket_w(const rtc::SentPacket& sent_packet);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700387 bool IsReadyToSendMedia_n() const;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200388 void CacheRtpAbsSendTimeHeaderExtension_n(int rtp_abs_sendtime_extn_id);
michaelt79e05882016-11-08 02:50:09 -0800389 int GetTransportOverheadPerPacket() const;
390 void UpdateTransportOverhead();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200391
392 rtc::Thread* const worker_thread_;
393 rtc::Thread* const network_thread_;
zhihuangf5b251b2017-01-12 19:37:48 -0800394 rtc::Thread* const signaling_thread_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200395 rtc::AsyncInvoker invoker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000397 const std::string content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200398 std::unique_ptr<ConnectionMonitor> connection_monitor_;
399
deadbeeff5346592017-01-24 21:51:21 -0800400 // Won't be set when using raw packet transports. SDP-specific thing.
deadbeefcbecd352015-09-23 11:50:27 -0700401 std::string transport_name_;
zhihuangb2cdd932017-01-19 16:54:25 -0800402
deadbeeff5346592017-01-24 21:51:21 -0800403 // Separate DTLS/non-DTLS pointers to support using BaseChannel without DTLS.
404 // Temporary measure until more refactoring is done.
405 // If non-null, "X_dtls_transport_" will always equal "X_packet_transport_".
zhihuangb2cdd932017-01-19 16:54:25 -0800406 DtlsTransportInternal* rtp_dtls_transport_ = nullptr;
zhihuangb2cdd932017-01-19 16:54:25 -0800407 DtlsTransportInternal* rtcp_dtls_transport_ = nullptr;
zsteind48dbda2017-04-04 19:45:57 -0700408 webrtc::RtpTransport rtp_transport_;
deadbeeff5346592017-01-24 21:51:21 -0800409 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
deadbeefcbecd352015-09-23 11:50:27 -0700410 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 SrtpFilter srtp_filter_;
412 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000413 BundleFilter bundle_filter_;
deadbeef23d947d2016-08-22 16:00:30 -0700414 bool rtp_ready_to_send_ = false;
415 bool rtcp_ready_to_send_ = false;
416 bool writable_ = false;
417 bool was_ever_writable_ = false;
418 bool has_received_packet_ = false;
419 bool dtls_keyed_ = false;
deadbeef7af91dd2016-12-13 11:29:11 -0800420 const bool srtp_required_ = true;
jbauchcb560652016-08-04 05:20:32 -0700421 rtc::CryptoOptions crypto_options_;
deadbeef23d947d2016-08-22 16:00:30 -0700422 int rtp_abs_sendtime_extn_id_ = -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200423
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700424 // MediaChannel related members that should be accessed from the worker
425 // thread.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200426 MediaChannel* const media_channel_;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700427 // Currently the |enabled_| flag is accessed from the signaling thread as
428 // well, but it can be changed only when signaling thread does a synchronous
429 // call to the worker thread, so it should be safe.
deadbeef23d947d2016-08-22 16:00:30 -0700430 bool enabled_ = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200431 std::vector<StreamParams> local_streams_;
432 std::vector<StreamParams> remote_streams_;
deadbeef23d947d2016-08-22 16:00:30 -0700433 MediaContentDirection local_content_direction_ = MD_INACTIVE;
434 MediaContentDirection remote_content_direction_ = MD_INACTIVE;
michaelt79e05882016-11-08 02:50:09 -0800435 CandidatePairInterface* selected_candidate_pair_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436};
437
438// VoiceChannel is a specialization that adds support for early media, DTMF,
439// and input/output level monitoring.
440class VoiceChannel : public BaseChannel {
441 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200442 VoiceChannel(rtc::Thread* worker_thread,
443 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800444 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700445 MediaEngineInterface* media_engine,
446 VoiceMediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -0700447 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800448 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800449 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 ~VoiceChannel();
solenberg1dd98f32015-09-10 01:57:14 -0700451
452 // Configure sending media on the stream with SSRC |ssrc|
453 // If there is only one sending stream SSRC 0 can be used.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200454 bool SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -0700455 bool enable,
deadbeefcbecd352015-09-23 11:50:27 -0700456 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800457 AudioSource* source);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458
459 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200460 VoiceMediaChannel* media_channel() const override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
462 }
463
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 void SetEarlyMedia(bool enable);
465 // This signal is emitted when we have gone a period of time without
466 // receiving early media. When received, a UI should start playing its
467 // own ringing sound
468 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
469
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 // Returns if the telephone-event has been negotiated.
471 bool CanInsertDtmf();
472 // Send and/or play a DTMF |event| according to the |flags|.
473 // The DTMF out-of-band signal will be used on sending.
474 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000475 // The valid value for the |event| are 0 which corresponding to DTMF
476 // event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800477 bool InsertDtmf(uint32_t ssrc, int event_code, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700478 bool SetOutputVolume(uint32_t ssrc, double volume);
deadbeef2d110be2016-01-13 12:00:26 -0800479 void SetRawAudioSink(uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -0800480 std::unique_ptr<webrtc::AudioSinkInterface> sink);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700481 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const;
482 bool SetRtpSendParameters(uint32_t ssrc,
483 const webrtc::RtpParameters& parameters);
484 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const;
485 bool SetRtpReceiveParameters(uint32_t ssrc,
486 const webrtc::RtpParameters& parameters);
Tommif888bb52015-12-12 01:37:01 +0100487
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 // Get statistics about the current media session.
489 bool GetStats(VoiceMediaInfo* stats);
490
491 // Monitoring functions
492 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
493 SignalConnectionMonitor;
494
495 void StartMediaMonitor(int cms);
496 void StopMediaMonitor();
497 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
498
499 void StartAudioMonitor(int cms);
500 void StopAudioMonitor();
501 bool IsAudioMonitorRunning() const;
502 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
503
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 int GetInputLevel_w();
505 int GetOutputLevel_w();
506 void GetActiveStreams_w(AudioInfo::StreamList* actives);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700507 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const;
508 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
509 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const;
510 bool SetRtpReceiveParameters_w(uint32_t ssrc,
511 webrtc::RtpParameters parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700512 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_AUDIO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 private:
515 // overrides from BaseChannel
deadbeef5bd5ca32017-02-10 11:31:50 -0800516 void OnPacketRead(rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700517 const char* data,
518 size_t len,
519 const rtc::PacketTime& packet_time,
520 int flags) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700521 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200522 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
523 bool SetLocalContent_w(const MediaContentDescription* content,
524 ContentAction action,
525 std::string* error_desc) override;
526 bool SetRemoteContent_w(const MediaContentDescription* content,
527 ContentAction action,
528 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 void HandleEarlyMediaTimeout();
solenberg1d63dd02015-12-02 12:35:09 -0800530 bool InsertDtmf_w(uint32_t ssrc, int event, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700531 bool SetOutputVolume_w(uint32_t ssrc, double volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 bool GetStats_w(VoiceMediaInfo* stats);
533
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200534 void OnMessage(rtc::Message* pmsg) override;
535 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override;
536 void OnConnectionMonitorUpdate(
537 ConnectionMonitor* monitor,
538 const std::vector<ConnectionInfo>& infos) override;
539 void OnMediaMonitorUpdate(VoiceMediaChannel* media_channel,
540 const VoiceMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542
543 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200544 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 bool received_media_;
kwiberg31022942016-03-11 14:18:21 -0800546 std::unique_ptr<VoiceMediaMonitor> media_monitor_;
547 std::unique_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700548
549 // Last AudioSendParameters sent down to the media_channel() via
550 // SetSendParameters.
551 AudioSendParameters last_send_params_;
552 // Last AudioRecvParameters sent down to the media_channel() via
553 // SetRecvParameters.
554 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555};
556
557// VideoChannel is a specialization for video.
558class VideoChannel : public BaseChannel {
559 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200560 VideoChannel(rtc::Thread* worker_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800561 rtc::Thread* network_thread,
562 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700563 VideoMediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -0700564 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800565 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800566 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 ~VideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200569 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200570 VideoMediaChannel* media_channel() const override {
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200571 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
572 }
573
nisseacd935b2016-11-11 03:55:13 -0800574 bool SetSink(uint32_t ssrc,
575 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000577 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578
579 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
580 SignalConnectionMonitor;
581
582 void StartMediaMonitor(int cms);
583 void StopMediaMonitor();
584 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585
deadbeef5a4a75a2016-06-02 16:23:38 -0700586 // Register a source and set options.
587 // The |ssrc| must correspond to a registered send stream.
588 bool SetVideoSend(uint32_t ssrc,
589 bool enable,
590 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -0800591 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700592 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const;
593 bool SetRtpSendParameters(uint32_t ssrc,
594 const webrtc::RtpParameters& parameters);
595 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const;
596 bool SetRtpReceiveParameters(uint32_t ssrc,
597 const webrtc::RtpParameters& parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700598 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_VIDEO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700602 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200603 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
604 bool SetLocalContent_w(const MediaContentDescription* content,
605 ContentAction action,
606 std::string* error_desc) override;
607 bool SetRemoteContent_w(const MediaContentDescription* content,
608 ContentAction action,
609 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 bool GetStats_w(VideoMediaInfo* stats);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700611 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const;
612 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
613 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const;
614 bool SetRtpReceiveParameters_w(uint32_t ssrc,
615 webrtc::RtpParameters parameters);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200617 void OnMessage(rtc::Message* pmsg) override;
618 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override;
619 void OnConnectionMonitorUpdate(
620 ConnectionMonitor* monitor,
621 const std::vector<ConnectionInfo>& infos) override;
622 void OnMediaMonitorUpdate(VideoMediaChannel* media_channel,
623 const VideoMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624
kwiberg31022942016-03-11 14:18:21 -0800625 std::unique_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700627 // Last VideoSendParameters sent down to the media_channel() via
628 // SetSendParameters.
629 VideoSendParameters last_send_params_;
630 // Last VideoRecvParameters sent down to the media_channel() via
631 // SetRecvParameters.
632 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633};
634
deadbeef953c2ce2017-01-09 14:53:41 -0800635// RtpDataChannel is a specialization for data.
636class RtpDataChannel : public BaseChannel {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 public:
deadbeef953c2ce2017-01-09 14:53:41 -0800638 RtpDataChannel(rtc::Thread* worker_thread,
639 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800640 rtc::Thread* signaling_thread,
641 DataMediaChannel* channel,
deadbeef953c2ce2017-01-09 14:53:41 -0800642 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800643 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -0800644 bool srtp_required);
645 ~RtpDataChannel();
zhihuangb2cdd932017-01-19 16:54:25 -0800646 bool Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800647 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800648 rtc::PacketTransportInternal* rtp_packet_transport,
649 rtc::PacketTransportInternal* rtcp_packet_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000651 virtual bool SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700652 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000653 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654
655 void StartMediaMonitor(int cms);
656 void StopMediaMonitor();
657
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000658 // Should be called on the signaling thread only.
659 bool ready_to_send_data() const {
660 return ready_to_send_data_;
661 }
662
deadbeef953c2ce2017-01-09 14:53:41 -0800663 sigslot::signal2<RtpDataChannel*, const DataMediaInfo&> SignalMediaMonitor;
664 sigslot::signal2<RtpDataChannel*, const std::vector<ConnectionInfo>&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 SignalConnectionMonitor;
deadbeef953c2ce2017-01-09 14:53:41 -0800666
667 sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
668 SignalDataReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000670 // That occurs when the channel is enabled, the transport is writable,
671 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 sigslot::signal1<bool> SignalReadyToSendData;
zhihuang184a3fd2016-06-14 11:47:14 -0700673 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_DATA; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000675 protected:
676 // downcasts a MediaChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200677 DataMediaChannel* media_channel() const override {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000678 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
679 }
680
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000682 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 SendDataMessageData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700684 const rtc::CopyOnWriteBuffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 SendDataResult* result)
686 : params(params),
687 payload(payload),
688 result(result),
689 succeeded(false) {
690 }
691
692 const SendDataParams& params;
jbaucheec21bd2016-03-20 06:15:43 -0700693 const rtc::CopyOnWriteBuffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 SendDataResult* result;
695 bool succeeded;
696 };
697
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000698 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 // We copy the data because the data will become invalid after we
700 // handle DataMediaChannel::SignalDataReceived but before we fire
701 // SignalDataReceived.
702 DataReceivedMessageData(
703 const ReceiveDataParams& params, const char* data, size_t len)
704 : params(params),
705 payload(data, len) {
706 }
707 const ReceiveDataParams params;
jbaucheec21bd2016-03-20 06:15:43 -0700708 const rtc::CopyOnWriteBuffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 };
710
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000711 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000712
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 // overrides from BaseChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200714 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
deadbeef953c2ce2017-01-09 14:53:41 -0800715 // Checks that data channel type is RTP.
716 bool CheckDataChannelTypeFromContent(const DataContentDescription* content,
717 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200718 bool SetLocalContent_w(const MediaContentDescription* content,
719 ContentAction action,
720 std::string* error_desc) override;
721 bool SetRemoteContent_w(const MediaContentDescription* content,
722 ContentAction action,
723 std::string* error_desc) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700724 void UpdateMediaSendRecvState_w() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200726 void OnMessage(rtc::Message* pmsg) override;
727 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override;
728 void OnConnectionMonitorUpdate(
729 ConnectionMonitor* monitor,
730 const std::vector<ConnectionInfo>& infos) override;
731 void OnMediaMonitorUpdate(DataMediaChannel* media_channel,
732 const DataMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 void OnDataReceived(
734 const ReceiveDataParams& params, const char* data, size_t len);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200735 void OnDataChannelError(uint32_t ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000736 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737
kwiberg31022942016-03-11 14:18:21 -0800738 std::unique_ptr<DataMediaMonitor> media_monitor_;
deadbeef953c2ce2017-01-09 14:53:41 -0800739 bool ready_to_send_data_ = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700740
741 // Last DataSendParameters sent down to the media_channel() via
742 // SetSendParameters.
743 DataSendParameters last_send_params_;
744 // Last DataRecvParameters sent down to the media_channel() via
745 // SetRecvParameters.
746 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747};
748
749} // namespace cricket
750
perkjc11b1842016-03-07 17:34:13 -0800751#endif // WEBRTC_PC_CHANNEL_H_