blob: 2bec643b47a09bc9440e61deaa67307477d6ba1f [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"
39#include "webrtc/pc/bundlefilter.h"
40#include "webrtc/pc/mediamonitor.h"
41#include "webrtc/pc/mediasession.h"
42#include "webrtc/pc/rtcpmuxfilter.h"
zsteind48dbda2017-04-04 19:45:57 -070043#include "webrtc/pc/rtptransport.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010044#include "webrtc/pc/srtpfilter.h"
Tommif888bb52015-12-12 01:37:01 +010045
46namespace webrtc {
47class AudioSinkInterface;
48} // namespace webrtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
50namespace cricket {
51
52struct CryptoParams;
53class MediaContentDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
deadbeef062ce9f2016-08-26 21:42:15 -070055// BaseChannel contains logic common to voice and video, including enable,
56// marshaling calls to a worker and network threads, and connection and media
57// monitors.
58//
Danil Chapovalov33b01f22016-05-11 19:55:27 +020059// BaseChannel assumes signaling and other threads are allowed to make
60// synchronous calls to the worker thread, the worker thread makes synchronous
61// calls only to the network thread, and the network thread can't be blocked by
62// other threads.
63// All methods with _n suffix must be called on network thread,
deadbeef062ce9f2016-08-26 21:42:15 -070064// methods with _w suffix on worker thread
Danil Chapovalov33b01f22016-05-11 19:55:27 +020065// and methods with _s suffix on signaling thread.
66// Network and worker threads may be the same thread.
wu@webrtc.org78187522013-10-07 23:32:02 +000067//
68// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
69// This is required to avoid a data race between the destructor modifying the
70// vtable, and the media channel's thread using BaseChannel as the
71// NetworkInterface.
72
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073class BaseChannel
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 : public rtc::MessageHandler, public sigslot::has_slots<>,
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +000075 public MediaChannel::NetworkInterface,
76 public ConnectionStatsGetter {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 public:
deadbeef7af91dd2016-12-13 11:29:11 -080078 // If |srtp_required| is true, the channel will not send or receive any
79 // RTP/RTCP packets without using SRTP (either using SDES or DTLS-SRTP).
Danil Chapovalov33b01f22016-05-11 19:55:27 +020080 BaseChannel(rtc::Thread* worker_thread,
81 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -080082 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -070083 MediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -070084 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -080085 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -080086 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 virtual ~BaseChannel();
zhihuangb2cdd932017-01-19 16:54:25 -080088 bool Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -080089 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -080090 rtc::PacketTransportInternal* rtp_packet_transport,
91 rtc::PacketTransportInternal* rtcp_packet_transport);
Danil Chapovalov33b01f22016-05-11 19:55:27 +020092 // Deinit may be called multiple times and is simply ignored if it's already
wu@webrtc.org78187522013-10-07 23:32:02 +000093 // done.
94 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000096 rtc::Thread* worker_thread() const { return worker_thread_; }
Danil Chapovalov33b01f22016-05-11 19:55:27 +020097 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070098 const std::string& content_name() const { return content_name_; }
deadbeeff5346592017-01-24 21:51:21 -080099 // TODO(deadbeef): This is redundant; remove this.
deadbeefcbecd352015-09-23 11:50:27 -0700100 const std::string& transport_name() const { return transport_name_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102
103 // This function returns true if we are using SRTP.
104 bool secure() const { return srtp_filter_.IsActive(); }
105 // The following function returns true if we are using
106 // DTLS-based keying. If you turned off SRTP later, however
107 // you could have secure() == false and dtls_secure() == true.
108 bool secure_dtls() const { return dtls_keyed_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
110 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111
deadbeefbad5dad2017-01-17 18:32:35 -0800112 // Set the transport(s), and update writability and "ready-to-send" state.
113 // |rtp_transport| must be non-null.
114 // |rtcp_transport| must be supplied if NeedsRtcpTransport() is true (meaning
115 // RTCP muxing is not fully active yet).
116 // |rtp_transport| and |rtcp_transport| must share the same transport name as
117 // well.
deadbeef5bd5ca32017-02-10 11:31:50 -0800118 // Can not start with "rtc::PacketTransportInternal" and switch to
deadbeeff5346592017-01-24 21:51:21 -0800119 // "DtlsTransportInternal", or vice-versa.
zhihuangb2cdd932017-01-19 16:54:25 -0800120 void SetTransports(DtlsTransportInternal* rtp_dtls_transport,
121 DtlsTransportInternal* rtcp_dtls_transport);
deadbeef5bd5ca32017-02-10 11:31:50 -0800122 void SetTransports(rtc::PacketTransportInternal* rtp_packet_transport,
123 rtc::PacketTransportInternal* rtcp_packet_transport);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000124 bool PushdownLocalDescription(const SessionDescription* local_desc,
125 ContentAction action,
126 std::string* error_desc);
127 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
128 ContentAction action,
129 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 // Channel control
131 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000132 ContentAction action,
133 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000135 ContentAction action,
136 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137
138 bool Enable(bool enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139
140 // Multiplexing
141 bool AddRecvStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200142 bool RemoveRecvStream(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000143 bool AddSendStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200144 bool RemoveSendStream(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145
146 // Monitoring
147 void StartConnectionMonitor(int cms);
148 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000149 // For ConnectionStatsGetter, used by ConnectionMonitor
deadbeefcbecd352015-09-23 11:50:27 -0700150 bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000152 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153
154 const std::vector<StreamParams>& local_streams() const {
155 return local_streams_;
156 }
157 const std::vector<StreamParams>& remote_streams() const {
158 return remote_streams_;
159 }
160
deadbeef953c2ce2017-01-09 14:53:41 -0800161 sigslot::signal2<BaseChannel*, bool> SignalDtlsSrtpSetupFailure;
162 void SignalDtlsSrtpSetupFailure_n(bool rtcp);
163 void SignalDtlsSrtpSetupFailure_s(bool rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000164
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000165 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
167
zhihuangb2cdd932017-01-19 16:54:25 -0800168 // Forward SignalSentPacket to worker thread.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200169 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket;
170
deadbeefac22f702017-01-12 21:59:29 -0800171 // Emitted whenever rtcp-mux is fully negotiated and the rtcp-transport can
172 // be destroyed.
173 // Fired on the network thread.
174 sigslot::signal1<const std::string&> SignalRtcpMuxFullyActive;
zhihuangf5b251b2017-01-12 19:37:48 -0800175
zhihuangb2cdd932017-01-19 16:54:25 -0800176 // Only public for unit tests. Otherwise, consider private.
177 DtlsTransportInternal* rtp_dtls_transport() const {
178 return rtp_dtls_transport_;
179 }
180 DtlsTransportInternal* rtcp_dtls_transport() const {
181 return rtcp_dtls_transport_;
182 }
zhihuangf5b251b2017-01-12 19:37:48 -0800183
184 bool NeedsRtcpTransport();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200185
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 // Made public for easier testing.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700187 //
188 // Updates "ready to send" for an individual channel, and informs the media
189 // channel that the transport is ready to send if each channel (in use) is
190 // ready to send. This is more specific than just "writable"; it means the
191 // last send didn't return ENOTCONN.
192 //
193 // This should be called whenever a channel's ready-to-send state changes,
194 // or when RTCP muxing becomes active/inactive.
195 void SetTransportChannelReadyToSend(bool rtcp, bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000197 // Only public for unit tests. Otherwise, consider protected.
rlesterec9d1872015-10-27 14:22:16 -0700198 int SetOption(SocketType type, rtc::Socket::Option o, int val)
199 override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200200 int SetOption_n(SocketType type, rtc::Socket::Option o, int val);
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000201
solenberg5b14b422015-10-01 04:10:31 -0700202 SrtpFilter* srtp_filter() { return &srtp_filter_; }
203
zhihuang184a3fd2016-06-14 11:47:14 -0700204 virtual cricket::MediaType media_type() = 0;
205
jbauchcb560652016-08-04 05:20:32 -0700206 bool SetCryptoOptions(const rtc::CryptoOptions& crypto_options);
207
deadbeef7af91dd2016-12-13 11:29:11 -0800208 // This function returns true if we require SRTP for call setup.
209 bool srtp_required_for_testing() const { return srtp_required_; }
210
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 virtual MediaChannel* media_channel() const { return media_channel_; }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700213
zhihuangb2cdd932017-01-19 16:54:25 -0800214 void SetTransports_n(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800215 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800216 rtc::PacketTransportInternal* rtp_packet_transport,
217 rtc::PacketTransportInternal* rtcp_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800218
deadbeef062ce9f2016-08-26 21:42:15 -0700219 // This does not update writability or "ready-to-send" state; it just
220 // disconnects from the old channel and connects to the new one.
deadbeeff5346592017-01-24 21:51:21 -0800221 void SetTransport_n(bool rtcp,
222 DtlsTransportInternal* new_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800223 rtc::PacketTransportInternal* new_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800224
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 bool was_ever_writable() const { return was_ever_writable_; }
226 void set_local_content_direction(MediaContentDirection direction) {
227 local_content_direction_ = direction;
228 }
229 void set_remote_content_direction(MediaContentDirection direction) {
230 remote_content_direction_ = direction;
231 }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700232 // These methods verify that:
233 // * The required content description directions have been set.
234 // * The channel is enabled.
235 // * And for sending:
236 // - The SRTP filter is active if it's needed.
237 // - The transport has been writable before, meaning it should be at least
238 // possible to succeed in sending a packet.
239 //
240 // When any of these properties change, UpdateMediaSendRecvState_w should be
241 // called.
242 bool IsReadyToReceiveMedia_w() const;
243 bool IsReadyToSendMedia_w() const;
zhihuangf5b251b2017-01-12 19:37:48 -0800244 rtc::Thread* signaling_thread() { return signaling_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245
deadbeeff5346592017-01-24 21:51:21 -0800246 void ConnectToDtlsTransport(DtlsTransportInternal* transport);
247 void DisconnectFromDtlsTransport(DtlsTransportInternal* transport);
deadbeef5bd5ca32017-02-10 11:31:50 -0800248 void ConnectToPacketTransport(rtc::PacketTransportInternal* transport);
249 void DisconnectFromPacketTransport(rtc::PacketTransportInternal* transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000250
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200251 void FlushRtcpMessages_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252
253 // NetworkInterface implementation, called by MediaEngine
jbaucheec21bd2016-03-20 06:15:43 -0700254 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
255 const rtc::PacketOptions& options) override;
256 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
257 const rtc::PacketOptions& options) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258
259 // From TransportChannel
deadbeef5bd5ca32017-02-10 11:31:50 -0800260 void OnWritableState(rtc::PacketTransportInternal* transport);
261 virtual void OnPacketRead(rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700262 const char* data,
263 size_t len,
264 const rtc::PacketTime& packet_time,
265 int flags);
deadbeef5bd5ca32017-02-10 11:31:50 -0800266 void OnReadyToSend(rtc::PacketTransportInternal* transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267
zhihuangb2cdd932017-01-19 16:54:25 -0800268 void OnDtlsState(DtlsTransportInternal* transport, DtlsTransportState state);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800269
Honghai Zhangcc411c02016-03-29 17:27:21 -0700270 void OnSelectedCandidatePairChanged(
zhihuangb2cdd932017-01-19 16:54:25 -0800271 IceTransportInternal* ice_transport,
Honghai Zhang52dce732016-03-31 12:37:31 -0700272 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700273 int last_sent_packet_id,
274 bool ready_to_send);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700275
deadbeef5bd5ca32017-02-10 11:31:50 -0800276 bool PacketIsRtcp(const rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700277 const char* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700279 bool SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700280 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700281 const rtc::PacketOptions& options);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200282
deadbeef953c2ce2017-01-09 14:53:41 -0800283 bool WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet);
jbaucheec21bd2016-03-20 06:15:43 -0700284 void HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000285 const rtc::PacketTime& packet_time);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200286 void OnPacketReceived(bool rtcp,
287 const rtc::CopyOnWriteBuffer& packet,
288 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 void EnableMedia_w();
291 void DisableMedia_w();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700292
293 // Performs actions if the RTP/RTCP writable state changed. This should
294 // be called whenever a channel's writable state changes or when RTCP muxing
295 // becomes active/inactive.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200296 void UpdateWritableState_n();
297 void ChannelWritable_n();
298 void ChannelNotWritable_n();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700299
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200301 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000302 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200303 bool RemoveSendStream_w(uint32_t ssrc);
deadbeef953c2ce2017-01-09 14:53:41 -0800304 bool ShouldSetupDtlsSrtp_n() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
306 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
zhihuangb2cdd932017-01-19 16:54:25 -0800307 bool SetupDtlsSrtp_n(bool rtcp);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200308 void MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
zhihuangb2cdd932017-01-19 16:54:25 -0800310 bool SetDtlsSrtpCryptoSuites_n(DtlsTransportInternal* transport, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700312 // Should be called whenever the conditions for
313 // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied).
314 // Updates the send/recv state of the media channel.
315 void UpdateMediaSendRecvState();
316 virtual void UpdateMediaSendRecvState_w() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317
318 // Gets the content info appropriate to the channel (audio or video).
319 virtual const ContentInfo* GetFirstContent(
320 const SessionDescription* sdesc) = 0;
321 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000322 ContentAction action,
323 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000325 ContentAction action,
326 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000328 ContentAction action,
329 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000331 ContentAction action,
332 std::string* error_desc) = 0;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200333 bool SetRtpTransportParameters(const MediaContentDescription* content,
334 ContentAction action,
335 ContentSource src,
336 std::string* error_desc);
337 bool SetRtpTransportParameters_n(const MediaContentDescription* content,
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700338 ContentAction action,
339 ContentSource src,
340 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000342 // Helper method to get RTP Absoulute SendTime extension header id if
343 // present in remote supported extensions list.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200344 void MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -0700345 const std::vector<webrtc::RtpExtension>& extensions);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000346
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200347 bool CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
348 bool* dtls,
349 std::string* error_desc);
350 bool SetSrtp_n(const std::vector<CryptoParams>& params,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000351 ContentAction action,
352 ContentSource src,
353 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200354 bool SetRtcpMux_n(bool enable,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000355 ContentAction action,
356 ContentSource src,
357 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358
359 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700360 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361
jbauchcb560652016-08-04 05:20:32 -0700362 const rtc::CryptoOptions& crypto_options() const {
363 return crypto_options_;
364 }
365
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366 // Handled in derived classes
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800367 // Get the SRTP crypto suites to use for RTP media
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200368 virtual void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000369 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 const std::vector<ConnectionInfo>& infos) = 0;
371
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000372 // Helper function for invoking bool-returning methods on the worker thread.
373 template <class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700374 bool InvokeOnWorker(const rtc::Location& posted_from,
375 const FunctorT& functor) {
376 return worker_thread_->Invoke<bool>(posted_from, functor);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000377 }
378
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 private:
zhihuangb2cdd932017-01-19 16:54:25 -0800380 bool InitNetwork_n(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800381 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800382 rtc::PacketTransportInternal* rtp_packet_transport,
383 rtc::PacketTransportInternal* rtcp_packet_transport);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200384 void DisconnectTransportChannels_n();
deadbeef5bd5ca32017-02-10 11:31:50 -0800385 void SignalSentPacket_n(rtc::PacketTransportInternal* transport,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200386 const rtc::SentPacket& sent_packet);
387 void SignalSentPacket_w(const rtc::SentPacket& sent_packet);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700388 bool IsReadyToSendMedia_n() const;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200389 void CacheRtpAbsSendTimeHeaderExtension_n(int rtp_abs_sendtime_extn_id);
michaelt79e05882016-11-08 02:50:09 -0800390 int GetTransportOverheadPerPacket() const;
391 void UpdateTransportOverhead();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200392
393 rtc::Thread* const worker_thread_;
394 rtc::Thread* const network_thread_;
zhihuangf5b251b2017-01-12 19:37:48 -0800395 rtc::Thread* const signaling_thread_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200396 rtc::AsyncInvoker invoker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000398 const std::string content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200399 std::unique_ptr<ConnectionMonitor> connection_monitor_;
400
deadbeeff5346592017-01-24 21:51:21 -0800401 // Won't be set when using raw packet transports. SDP-specific thing.
deadbeefcbecd352015-09-23 11:50:27 -0700402 std::string transport_name_;
zhihuangb2cdd932017-01-19 16:54:25 -0800403
deadbeeff5346592017-01-24 21:51:21 -0800404 // Separate DTLS/non-DTLS pointers to support using BaseChannel without DTLS.
405 // Temporary measure until more refactoring is done.
406 // If non-null, "X_dtls_transport_" will always equal "X_packet_transport_".
zhihuangb2cdd932017-01-19 16:54:25 -0800407 DtlsTransportInternal* rtp_dtls_transport_ = nullptr;
zhihuangb2cdd932017-01-19 16:54:25 -0800408 DtlsTransportInternal* rtcp_dtls_transport_ = nullptr;
zsteind48dbda2017-04-04 19:45:57 -0700409 webrtc::RtpTransport rtp_transport_;
deadbeeff5346592017-01-24 21:51:21 -0800410 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
deadbeefcbecd352015-09-23 11:50:27 -0700411 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412 SrtpFilter srtp_filter_;
413 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000414 BundleFilter bundle_filter_;
deadbeef23d947d2016-08-22 16:00:30 -0700415 bool rtp_ready_to_send_ = false;
416 bool rtcp_ready_to_send_ = false;
417 bool writable_ = false;
418 bool was_ever_writable_ = false;
419 bool has_received_packet_ = false;
420 bool dtls_keyed_ = false;
deadbeef7af91dd2016-12-13 11:29:11 -0800421 const bool srtp_required_ = true;
jbauchcb560652016-08-04 05:20:32 -0700422 rtc::CryptoOptions crypto_options_;
deadbeef23d947d2016-08-22 16:00:30 -0700423 int rtp_abs_sendtime_extn_id_ = -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200424
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700425 // MediaChannel related members that should be accessed from the worker
426 // thread.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200427 MediaChannel* const media_channel_;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700428 // Currently the |enabled_| flag is accessed from the signaling thread as
429 // well, but it can be changed only when signaling thread does a synchronous
430 // call to the worker thread, so it should be safe.
deadbeef23d947d2016-08-22 16:00:30 -0700431 bool enabled_ = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200432 std::vector<StreamParams> local_streams_;
433 std::vector<StreamParams> remote_streams_;
deadbeef23d947d2016-08-22 16:00:30 -0700434 MediaContentDirection local_content_direction_ = MD_INACTIVE;
435 MediaContentDirection remote_content_direction_ = MD_INACTIVE;
michaelt79e05882016-11-08 02:50:09 -0800436 CandidatePairInterface* selected_candidate_pair_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437};
438
439// VoiceChannel is a specialization that adds support for early media, DTMF,
440// and input/output level monitoring.
441class VoiceChannel : public BaseChannel {
442 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200443 VoiceChannel(rtc::Thread* worker_thread,
444 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800445 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700446 MediaEngineInterface* media_engine,
447 VoiceMediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -0700448 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800449 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800450 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 ~VoiceChannel();
solenberg1dd98f32015-09-10 01:57:14 -0700452
453 // Configure sending media on the stream with SSRC |ssrc|
454 // If there is only one sending stream SSRC 0 can be used.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200455 bool SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -0700456 bool enable,
deadbeefcbecd352015-09-23 11:50:27 -0700457 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800458 AudioSource* source);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459
460 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200461 VoiceMediaChannel* media_channel() const override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
463 }
464
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 void SetEarlyMedia(bool enable);
466 // This signal is emitted when we have gone a period of time without
467 // receiving early media. When received, a UI should start playing its
468 // own ringing sound
469 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
470
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 // Returns if the telephone-event has been negotiated.
472 bool CanInsertDtmf();
473 // Send and/or play a DTMF |event| according to the |flags|.
474 // The DTMF out-of-band signal will be used on sending.
475 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000476 // The valid value for the |event| are 0 which corresponding to DTMF
477 // event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800478 bool InsertDtmf(uint32_t ssrc, int event_code, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700479 bool SetOutputVolume(uint32_t ssrc, double volume);
deadbeef2d110be2016-01-13 12:00:26 -0800480 void SetRawAudioSink(uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -0800481 std::unique_ptr<webrtc::AudioSinkInterface> sink);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700482 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const;
483 bool SetRtpSendParameters(uint32_t ssrc,
484 const webrtc::RtpParameters& parameters);
485 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const;
486 bool SetRtpReceiveParameters(uint32_t ssrc,
487 const webrtc::RtpParameters& parameters);
Tommif888bb52015-12-12 01:37:01 +0100488
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 // Get statistics about the current media session.
490 bool GetStats(VoiceMediaInfo* stats);
491
hbos8d609f62017-04-10 07:39:05 -0700492 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const;
493
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 // Monitoring functions
495 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
496 SignalConnectionMonitor;
497
498 void StartMediaMonitor(int cms);
499 void StopMediaMonitor();
500 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
501
502 void StartAudioMonitor(int cms);
503 void StopAudioMonitor();
504 bool IsAudioMonitorRunning() const;
505 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
506
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 int GetInputLevel_w();
508 int GetOutputLevel_w();
509 void GetActiveStreams_w(AudioInfo::StreamList* actives);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700510 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const;
511 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
512 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const;
513 bool SetRtpReceiveParameters_w(uint32_t ssrc,
514 webrtc::RtpParameters parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700515 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_AUDIO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 private:
518 // overrides from BaseChannel
deadbeef5bd5ca32017-02-10 11:31:50 -0800519 void OnPacketRead(rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700520 const char* data,
521 size_t len,
522 const rtc::PacketTime& packet_time,
523 int flags) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700524 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200525 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
526 bool SetLocalContent_w(const MediaContentDescription* content,
527 ContentAction action,
528 std::string* error_desc) override;
529 bool SetRemoteContent_w(const MediaContentDescription* content,
530 ContentAction action,
531 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 void HandleEarlyMediaTimeout();
solenberg1d63dd02015-12-02 12:35:09 -0800533 bool InsertDtmf_w(uint32_t ssrc, int event, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700534 bool SetOutputVolume_w(uint32_t ssrc, double volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200536 void OnMessage(rtc::Message* pmsg) override;
537 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override;
538 void OnConnectionMonitorUpdate(
539 ConnectionMonitor* monitor,
540 const std::vector<ConnectionInfo>& infos) override;
541 void OnMediaMonitorUpdate(VoiceMediaChannel* media_channel,
542 const VoiceMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544
545 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200546 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 bool received_media_;
kwiberg31022942016-03-11 14:18:21 -0800548 std::unique_ptr<VoiceMediaMonitor> media_monitor_;
549 std::unique_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700550
551 // Last AudioSendParameters sent down to the media_channel() via
552 // SetSendParameters.
553 AudioSendParameters last_send_params_;
554 // Last AudioRecvParameters sent down to the media_channel() via
555 // SetRecvParameters.
556 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557};
558
559// VideoChannel is a specialization for video.
560class VideoChannel : public BaseChannel {
561 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200562 VideoChannel(rtc::Thread* worker_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800563 rtc::Thread* network_thread,
564 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700565 VideoMediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -0700566 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800567 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800568 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 ~VideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200571 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200572 VideoMediaChannel* media_channel() const override {
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200573 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
574 }
575
nisseacd935b2016-11-11 03:55:13 -0800576 bool SetSink(uint32_t ssrc,
577 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000579 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580
581 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
582 SignalConnectionMonitor;
583
584 void StartMediaMonitor(int cms);
585 void StopMediaMonitor();
586 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587
deadbeef5a4a75a2016-06-02 16:23:38 -0700588 // Register a source and set options.
589 // The |ssrc| must correspond to a registered send stream.
590 bool SetVideoSend(uint32_t ssrc,
591 bool enable,
592 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -0800593 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700594 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const;
595 bool SetRtpSendParameters(uint32_t ssrc,
596 const webrtc::RtpParameters& parameters);
597 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const;
598 bool SetRtpReceiveParameters(uint32_t ssrc,
599 const webrtc::RtpParameters& parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700600 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_VIDEO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700604 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200605 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
606 bool SetLocalContent_w(const MediaContentDescription* content,
607 ContentAction action,
608 std::string* error_desc) override;
609 bool SetRemoteContent_w(const MediaContentDescription* content,
610 ContentAction action,
611 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 bool GetStats_w(VideoMediaInfo* stats);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700613 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const;
614 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
615 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const;
616 bool SetRtpReceiveParameters_w(uint32_t ssrc,
617 webrtc::RtpParameters parameters);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200619 void OnMessage(rtc::Message* pmsg) override;
620 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override;
621 void OnConnectionMonitorUpdate(
622 ConnectionMonitor* monitor,
623 const std::vector<ConnectionInfo>& infos) override;
624 void OnMediaMonitorUpdate(VideoMediaChannel* media_channel,
625 const VideoMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626
kwiberg31022942016-03-11 14:18:21 -0800627 std::unique_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700629 // Last VideoSendParameters sent down to the media_channel() via
630 // SetSendParameters.
631 VideoSendParameters last_send_params_;
632 // Last VideoRecvParameters sent down to the media_channel() via
633 // SetRecvParameters.
634 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635};
636
deadbeef953c2ce2017-01-09 14:53:41 -0800637// RtpDataChannel is a specialization for data.
638class RtpDataChannel : public BaseChannel {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 public:
deadbeef953c2ce2017-01-09 14:53:41 -0800640 RtpDataChannel(rtc::Thread* worker_thread,
641 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800642 rtc::Thread* signaling_thread,
643 DataMediaChannel* channel,
deadbeef953c2ce2017-01-09 14:53:41 -0800644 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800645 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -0800646 bool srtp_required);
647 ~RtpDataChannel();
zhihuangb2cdd932017-01-19 16:54:25 -0800648 bool Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800649 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800650 rtc::PacketTransportInternal* rtp_packet_transport,
651 rtc::PacketTransportInternal* rtcp_packet_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000653 virtual bool SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700654 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000655 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656
657 void StartMediaMonitor(int cms);
658 void StopMediaMonitor();
659
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000660 // Should be called on the signaling thread only.
661 bool ready_to_send_data() const {
662 return ready_to_send_data_;
663 }
664
deadbeef953c2ce2017-01-09 14:53:41 -0800665 sigslot::signal2<RtpDataChannel*, const DataMediaInfo&> SignalMediaMonitor;
666 sigslot::signal2<RtpDataChannel*, const std::vector<ConnectionInfo>&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 SignalConnectionMonitor;
deadbeef953c2ce2017-01-09 14:53:41 -0800668
669 sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
670 SignalDataReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000672 // That occurs when the channel is enabled, the transport is writable,
673 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 sigslot::signal1<bool> SignalReadyToSendData;
zhihuang184a3fd2016-06-14 11:47:14 -0700675 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_DATA; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000677 protected:
678 // downcasts a MediaChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200679 DataMediaChannel* media_channel() const override {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000680 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
681 }
682
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000684 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 SendDataMessageData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700686 const rtc::CopyOnWriteBuffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 SendDataResult* result)
688 : params(params),
689 payload(payload),
690 result(result),
691 succeeded(false) {
692 }
693
694 const SendDataParams& params;
jbaucheec21bd2016-03-20 06:15:43 -0700695 const rtc::CopyOnWriteBuffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 SendDataResult* result;
697 bool succeeded;
698 };
699
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000700 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 // We copy the data because the data will become invalid after we
702 // handle DataMediaChannel::SignalDataReceived but before we fire
703 // SignalDataReceived.
704 DataReceivedMessageData(
705 const ReceiveDataParams& params, const char* data, size_t len)
706 : params(params),
707 payload(data, len) {
708 }
709 const ReceiveDataParams params;
jbaucheec21bd2016-03-20 06:15:43 -0700710 const rtc::CopyOnWriteBuffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 };
712
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000713 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000714
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 // overrides from BaseChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200716 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
deadbeef953c2ce2017-01-09 14:53:41 -0800717 // Checks that data channel type is RTP.
718 bool CheckDataChannelTypeFromContent(const DataContentDescription* content,
719 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200720 bool SetLocalContent_w(const MediaContentDescription* content,
721 ContentAction action,
722 std::string* error_desc) override;
723 bool SetRemoteContent_w(const MediaContentDescription* content,
724 ContentAction action,
725 std::string* error_desc) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700726 void UpdateMediaSendRecvState_w() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200728 void OnMessage(rtc::Message* pmsg) override;
729 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override;
730 void OnConnectionMonitorUpdate(
731 ConnectionMonitor* monitor,
732 const std::vector<ConnectionInfo>& infos) override;
733 void OnMediaMonitorUpdate(DataMediaChannel* media_channel,
734 const DataMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 void OnDataReceived(
736 const ReceiveDataParams& params, const char* data, size_t len);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200737 void OnDataChannelError(uint32_t ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000738 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739
kwiberg31022942016-03-11 14:18:21 -0800740 std::unique_ptr<DataMediaMonitor> media_monitor_;
deadbeef953c2ce2017-01-09 14:53:41 -0800741 bool ready_to_send_data_ = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700742
743 // Last DataSendParameters sent down to the media_channel() via
744 // SetSendParameters.
745 DataSendParameters last_send_params_;
746 // Last DataRecvParameters sent down to the media_channel() via
747 // SetRecvParameters.
748 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749};
750
751} // namespace cricket
752
perkjc11b1842016-03-07 17:34:13 -0800753#endif // WEBRTC_PC_CHANNEL_H_