blob: e5180ca26f6cb3cad13406be27a7b22f1fa3eb61 [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"
Tommif888bb52015-12-12 01:37:01 +010033#include "webrtc/p2p/base/transportcontroller.h"
34#include "webrtc/p2p/client/socketmonitor.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010035#include "webrtc/pc/audiomonitor.h"
36#include "webrtc/pc/bundlefilter.h"
37#include "webrtc/pc/mediamonitor.h"
38#include "webrtc/pc/mediasession.h"
39#include "webrtc/pc/rtcpmuxfilter.h"
40#include "webrtc/pc/srtpfilter.h"
Tommif888bb52015-12-12 01:37:01 +010041
johand89ab142016-10-25 10:50:32 -070042namespace rtc {
43class PacketTransportInterface;
44}
45
Tommif888bb52015-12-12 01:37:01 +010046namespace 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:
deadbeef23d947d2016-08-22 16:00:30 -070078 // |rtcp| represents whether or not this channel uses RTCP.
deadbeef7af91dd2016-12-13 11:29:11 -080079 // If |srtp_required| is true, the channel will not send or receive any
80 // RTP/RTCP packets without using SRTP (either using SDES or DTLS-SRTP).
Danil Chapovalov33b01f22016-05-11 19:55:27 +020081 BaseChannel(rtc::Thread* worker_thread,
82 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -080083 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -070084 MediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -070085 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -080086 bool rtcp,
87 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 virtual ~BaseChannel();
zhihuangf5b251b2017-01-12 19:37:48 -080089 bool Init_w(TransportChannel* rtp_transport,
90 TransportChannel* rtcp_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_; }
98 const std::string& transport_name() const { return transport_name_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
101 // This function returns true if we are using SRTP.
102 bool secure() const { return srtp_filter_.IsActive(); }
103 // The following function returns true if we are using
104 // DTLS-based keying. If you turned off SRTP later, however
105 // you could have secure() == false and dtls_secure() == true.
106 bool secure_dtls() const { return dtls_keyed_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
108 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700110 // Activate RTCP mux, regardless of the state so far. Once
111 // activated, it can not be deactivated, and if the remote
112 // description doesn't support RTCP mux, setting the remote
113 // description will fail.
114 void ActivateRtcpMux();
zhihuangf5b251b2017-01-12 19:37:48 -0800115 bool SetTransport(TransportChannel* rtp_transport,
116 TransportChannel* rtcp_transport);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000117 bool PushdownLocalDescription(const SessionDescription* local_desc,
118 ContentAction action,
119 std::string* error_desc);
120 bool PushdownRemoteDescription(const SessionDescription* remote_desc,
121 ContentAction action,
122 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 // Channel control
124 bool SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000125 ContentAction action,
126 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 bool SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000128 ContentAction action,
129 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
131 bool Enable(bool enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
133 // Multiplexing
134 bool AddRecvStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200135 bool RemoveRecvStream(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000136 bool AddSendStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200137 bool RemoveSendStream(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138
139 // Monitoring
140 void StartConnectionMonitor(int cms);
141 void StopConnectionMonitor();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000142 // For ConnectionStatsGetter, used by ConnectionMonitor
deadbeefcbecd352015-09-23 11:50:27 -0700143 bool GetConnectionStats(ConnectionInfos* infos) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000145 BundleFilter* bundle_filter() { return &bundle_filter_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
147 const std::vector<StreamParams>& local_streams() const {
148 return local_streams_;
149 }
150 const std::vector<StreamParams>& remote_streams() const {
151 return remote_streams_;
152 }
153
deadbeef953c2ce2017-01-09 14:53:41 -0800154 sigslot::signal2<BaseChannel*, bool> SignalDtlsSrtpSetupFailure;
155 void SignalDtlsSrtpSetupFailure_n(bool rtcp);
156 void SignalDtlsSrtpSetupFailure_s(bool rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000157
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000158 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
160
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200161 // Forward TransportChannel SignalSentPacket to worker thread.
162 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket;
163
zhihuangf5b251b2017-01-12 19:37:48 -0800164 // Emitted whenever the rtcp-mux is active and the rtcp-transport can be
165 // destroyed.
166 sigslot::signal1<const std::string&> SignalDestroyRtcpTransport;
167
168 TransportChannel* rtp_transport() const { return rtp_transport_; }
169 TransportChannel* rtcp_transport() const { return rtcp_transport_; }
170
171 bool NeedsRtcpTransport();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200172
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 // Made public for easier testing.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700174 //
175 // Updates "ready to send" for an individual channel, and informs the media
176 // channel that the transport is ready to send if each channel (in use) is
177 // ready to send. This is more specific than just "writable"; it means the
178 // last send didn't return ENOTCONN.
179 //
180 // This should be called whenever a channel's ready-to-send state changes,
181 // or when RTCP muxing becomes active/inactive.
182 void SetTransportChannelReadyToSend(bool rtcp, bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000184 // Only public for unit tests. Otherwise, consider protected.
rlesterec9d1872015-10-27 14:22:16 -0700185 int SetOption(SocketType type, rtc::Socket::Option o, int val)
186 override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200187 int SetOption_n(SocketType type, rtc::Socket::Option o, int val);
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000188
solenberg5b14b422015-10-01 04:10:31 -0700189 SrtpFilter* srtp_filter() { return &srtp_filter_; }
190
zhihuang184a3fd2016-06-14 11:47:14 -0700191 virtual cricket::MediaType media_type() = 0;
192
jbauchcb560652016-08-04 05:20:32 -0700193 bool SetCryptoOptions(const rtc::CryptoOptions& crypto_options);
194
deadbeef7af91dd2016-12-13 11:29:11 -0800195 // This function returns true if we require SRTP for call setup.
196 bool srtp_required_for_testing() const { return srtp_required_; }
197
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 virtual MediaChannel* media_channel() const { return media_channel_; }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700200
zhihuangf5b251b2017-01-12 19:37:48 -0800201 // Sets the |rtp_transport_| (and |rtcp_transport_|, if
202 // |rtcp_enabled_| is true).
deadbeef062ce9f2016-08-26 21:42:15 -0700203 // This method also updates writability and "ready-to-send" state.
zhihuangf5b251b2017-01-12 19:37:48 -0800204 bool SetTransport_n(TransportChannel* rtp_transport,
205 TransportChannel* rtcp_transport);
guoweis46383312015-12-17 16:45:59 -0800206
deadbeef062ce9f2016-08-26 21:42:15 -0700207 // This does not update writability or "ready-to-send" state; it just
208 // disconnects from the old channel and connects to the new one.
zhihuangf5b251b2017-01-12 19:37:48 -0800209 void SetTransportChannel_n(bool rtcp, TransportChannel* new_transport);
guoweis46383312015-12-17 16:45:59 -0800210
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 bool was_ever_writable() const { return was_ever_writable_; }
212 void set_local_content_direction(MediaContentDirection direction) {
213 local_content_direction_ = direction;
214 }
215 void set_remote_content_direction(MediaContentDirection direction) {
216 remote_content_direction_ = direction;
217 }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700218 // These methods verify that:
219 // * The required content description directions have been set.
220 // * The channel is enabled.
221 // * And for sending:
222 // - The SRTP filter is active if it's needed.
223 // - The transport has been writable before, meaning it should be at least
224 // possible to succeed in sending a packet.
225 //
226 // When any of these properties change, UpdateMediaSendRecvState_w should be
227 // called.
228 bool IsReadyToReceiveMedia_w() const;
229 bool IsReadyToSendMedia_w() const;
zhihuangf5b251b2017-01-12 19:37:48 -0800230 rtc::Thread* signaling_thread() { return signaling_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000232 void ConnectToTransportChannel(TransportChannel* tc);
233 void DisconnectFromTransportChannel(TransportChannel* tc);
234
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200235 void FlushRtcpMessages_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236
237 // NetworkInterface implementation, called by MediaEngine
jbaucheec21bd2016-03-20 06:15:43 -0700238 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
239 const rtc::PacketOptions& options) override;
240 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
241 const rtc::PacketOptions& options) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242
243 // From TransportChannel
johand89ab142016-10-25 10:50:32 -0700244 void OnWritableState(rtc::PacketTransportInterface* transport);
245 virtual void OnPacketRead(rtc::PacketTransportInterface* transport,
246 const char* data,
247 size_t len,
248 const rtc::PacketTime& packet_time,
249 int flags);
250 void OnReadyToSend(rtc::PacketTransportInterface* transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800252 void OnDtlsState(TransportChannel* channel, DtlsTransportState state);
253
Honghai Zhangcc411c02016-03-29 17:27:21 -0700254 void OnSelectedCandidatePairChanged(
255 TransportChannel* channel,
Honghai Zhang52dce732016-03-31 12:37:31 -0700256 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700257 int last_sent_packet_id,
258 bool ready_to_send);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700259
johand89ab142016-10-25 10:50:32 -0700260 bool PacketIsRtcp(const rtc::PacketTransportInterface* transport,
261 const char* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700263 bool SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700264 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700265 const rtc::PacketOptions& options);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200266
deadbeef953c2ce2017-01-09 14:53:41 -0800267 bool WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet);
jbaucheec21bd2016-03-20 06:15:43 -0700268 void HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000269 const rtc::PacketTime& packet_time);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200270 void OnPacketReceived(bool rtcp,
271 const rtc::CopyOnWriteBuffer& packet,
272 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 void EnableMedia_w();
275 void DisableMedia_w();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700276
277 // Performs actions if the RTP/RTCP writable state changed. This should
278 // be called whenever a channel's writable state changes or when RTCP muxing
279 // becomes active/inactive.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200280 void UpdateWritableState_n();
281 void ChannelWritable_n();
282 void ChannelNotWritable_n();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700283
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200285 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000286 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200287 bool RemoveSendStream_w(uint32_t ssrc);
deadbeef953c2ce2017-01-09 14:53:41 -0800288 bool ShouldSetupDtlsSrtp_n() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 // Do the DTLS key expansion and impose it on the SRTP/SRTCP filters.
290 // |rtcp_channel| indicates whether to set up the RTP or RTCP filter.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200291 bool SetupDtlsSrtp_n(bool rtcp_channel);
292 void MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 // Set the DTLS-SRTP cipher policy on this channel as appropriate.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200294 bool SetDtlsSrtpCryptoSuites_n(TransportChannel* tc, bool rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700296 // Should be called whenever the conditions for
297 // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied).
298 // Updates the send/recv state of the media channel.
299 void UpdateMediaSendRecvState();
300 virtual void UpdateMediaSendRecvState_w() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301
302 // Gets the content info appropriate to the channel (audio or video).
303 virtual const ContentInfo* GetFirstContent(
304 const SessionDescription* sdesc) = 0;
305 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000306 ContentAction action,
307 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000309 ContentAction action,
310 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 virtual bool SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000312 ContentAction action,
313 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000315 ContentAction action,
316 std::string* error_desc) = 0;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200317 bool SetRtpTransportParameters(const MediaContentDescription* content,
318 ContentAction action,
319 ContentSource src,
320 std::string* error_desc);
321 bool SetRtpTransportParameters_n(const MediaContentDescription* content,
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700322 ContentAction action,
323 ContentSource src,
324 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000326 // Helper method to get RTP Absoulute SendTime extension header id if
327 // present in remote supported extensions list.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200328 void MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -0700329 const std::vector<webrtc::RtpExtension>& extensions);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000330
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200331 bool CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
332 bool* dtls,
333 std::string* error_desc);
334 bool SetSrtp_n(const std::vector<CryptoParams>& params,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000335 ContentAction action,
336 ContentSource src,
337 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200338 void ActivateRtcpMux_n();
339 bool SetRtcpMux_n(bool enable,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000340 ContentAction action,
341 ContentSource src,
342 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343
344 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700345 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346
jbauchcb560652016-08-04 05:20:32 -0700347 const rtc::CryptoOptions& crypto_options() const {
348 return crypto_options_;
349 }
350
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 // Handled in derived classes
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800352 // Get the SRTP crypto suites to use for RTP media
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200353 virtual void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const = 0;
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000354 virtual void OnConnectionMonitorUpdate(ConnectionMonitor* monitor,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 const std::vector<ConnectionInfo>& infos) = 0;
356
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000357 // Helper function for invoking bool-returning methods on the worker thread.
358 template <class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700359 bool InvokeOnWorker(const rtc::Location& posted_from,
360 const FunctorT& functor) {
361 return worker_thread_->Invoke<bool>(posted_from, functor);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000362 }
363
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 private:
zhihuangf5b251b2017-01-12 19:37:48 -0800365 bool InitNetwork_n(TransportChannel* rtp_transport,
366 TransportChannel* rtcp_transport);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200367 void DisconnectTransportChannels_n();
johand89ab142016-10-25 10:50:32 -0700368 void SignalSentPacket_n(rtc::PacketTransportInterface* transport,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200369 const rtc::SentPacket& sent_packet);
370 void SignalSentPacket_w(const rtc::SentPacket& sent_packet);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700371 bool IsReadyToSendMedia_n() const;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200372 void CacheRtpAbsSendTimeHeaderExtension_n(int rtp_abs_sendtime_extn_id);
michaelt79e05882016-11-08 02:50:09 -0800373 int GetTransportOverheadPerPacket() const;
374 void UpdateTransportOverhead();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200375
376 rtc::Thread* const worker_thread_;
377 rtc::Thread* const network_thread_;
zhihuangf5b251b2017-01-12 19:37:48 -0800378 rtc::Thread* const signaling_thread_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200379 rtc::AsyncInvoker invoker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000381 const std::string content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200382 std::unique_ptr<ConnectionMonitor> connection_monitor_;
383
deadbeefcbecd352015-09-23 11:50:27 -0700384 std::string transport_name_;
deadbeef23d947d2016-08-22 16:00:30 -0700385 // Is RTCP used at all by this type of channel?
386 // Expected to be true (as of typing this) for everything except data
387 // channels.
388 const bool rtcp_enabled_;
johand89ab142016-10-25 10:50:32 -0700389 // TODO(johan): Replace TransportChannel* with rtc::PacketTransportInterface*.
zhihuangf5b251b2017-01-12 19:37:48 -0800390 TransportChannel* rtp_transport_ = nullptr;
deadbeefcbecd352015-09-23 11:50:27 -0700391 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
zhihuangf5b251b2017-01-12 19:37:48 -0800392 TransportChannel* rtcp_transport_ = nullptr;
deadbeefcbecd352015-09-23 11:50:27 -0700393 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 SrtpFilter srtp_filter_;
395 RtcpMuxFilter rtcp_mux_filter_;
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000396 BundleFilter bundle_filter_;
deadbeef23d947d2016-08-22 16:00:30 -0700397 bool rtp_ready_to_send_ = false;
398 bool rtcp_ready_to_send_ = false;
399 bool writable_ = false;
400 bool was_ever_writable_ = false;
401 bool has_received_packet_ = false;
402 bool dtls_keyed_ = false;
deadbeef7af91dd2016-12-13 11:29:11 -0800403 const bool srtp_required_ = true;
jbauchcb560652016-08-04 05:20:32 -0700404 rtc::CryptoOptions crypto_options_;
deadbeef23d947d2016-08-22 16:00:30 -0700405 int rtp_abs_sendtime_extn_id_ = -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200406
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700407 // MediaChannel related members that should be accessed from the worker
408 // thread.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200409 MediaChannel* const media_channel_;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700410 // Currently the |enabled_| flag is accessed from the signaling thread as
411 // well, but it can be changed only when signaling thread does a synchronous
412 // call to the worker thread, so it should be safe.
deadbeef23d947d2016-08-22 16:00:30 -0700413 bool enabled_ = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200414 std::vector<StreamParams> local_streams_;
415 std::vector<StreamParams> remote_streams_;
deadbeef23d947d2016-08-22 16:00:30 -0700416 MediaContentDirection local_content_direction_ = MD_INACTIVE;
417 MediaContentDirection remote_content_direction_ = MD_INACTIVE;
michaelt79e05882016-11-08 02:50:09 -0800418 CandidatePairInterface* selected_candidate_pair_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419};
420
421// VoiceChannel is a specialization that adds support for early media, DTMF,
422// and input/output level monitoring.
423class VoiceChannel : public BaseChannel {
424 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200425 VoiceChannel(rtc::Thread* worker_thread,
426 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800427 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700428 MediaEngineInterface* media_engine,
429 VoiceMediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -0700430 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -0800431 bool rtcp,
432 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 ~VoiceChannel();
zhihuangf5b251b2017-01-12 19:37:48 -0800434 bool Init_w(TransportChannel* rtp_transport,
435 TransportChannel* rtcp_transport);
solenberg1dd98f32015-09-10 01:57:14 -0700436
437 // Configure sending media on the stream with SSRC |ssrc|
438 // If there is only one sending stream SSRC 0 can be used.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200439 bool SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -0700440 bool enable,
deadbeefcbecd352015-09-23 11:50:27 -0700441 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800442 AudioSource* source);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443
444 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200445 VoiceMediaChannel* media_channel() const override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
447 }
448
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 void SetEarlyMedia(bool enable);
450 // This signal is emitted when we have gone a period of time without
451 // receiving early media. When received, a UI should start playing its
452 // own ringing sound
453 sigslot::signal1<VoiceChannel*> SignalEarlyMediaTimeout;
454
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455 // Returns if the telephone-event has been negotiated.
456 bool CanInsertDtmf();
457 // Send and/or play a DTMF |event| according to the |flags|.
458 // The DTMF out-of-band signal will be used on sending.
459 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000460 // The valid value for the |event| are 0 which corresponding to DTMF
461 // event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800462 bool InsertDtmf(uint32_t ssrc, int event_code, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700463 bool SetOutputVolume(uint32_t ssrc, double volume);
deadbeef2d110be2016-01-13 12:00:26 -0800464 void SetRawAudioSink(uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -0800465 std::unique_ptr<webrtc::AudioSinkInterface> sink);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700466 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const;
467 bool SetRtpSendParameters(uint32_t ssrc,
468 const webrtc::RtpParameters& parameters);
469 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const;
470 bool SetRtpReceiveParameters(uint32_t ssrc,
471 const webrtc::RtpParameters& parameters);
Tommif888bb52015-12-12 01:37:01 +0100472
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 // Get statistics about the current media session.
474 bool GetStats(VoiceMediaInfo* stats);
475
476 // Monitoring functions
477 sigslot::signal2<VoiceChannel*, const std::vector<ConnectionInfo>&>
478 SignalConnectionMonitor;
479
480 void StartMediaMonitor(int cms);
481 void StopMediaMonitor();
482 sigslot::signal2<VoiceChannel*, const VoiceMediaInfo&> SignalMediaMonitor;
483
484 void StartAudioMonitor(int cms);
485 void StopAudioMonitor();
486 bool IsAudioMonitorRunning() const;
487 sigslot::signal2<VoiceChannel*, const AudioInfo&> SignalAudioMonitor;
488
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 int GetInputLevel_w();
490 int GetOutputLevel_w();
491 void GetActiveStreams_w(AudioInfo::StreamList* actives);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700492 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const;
493 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
494 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const;
495 bool SetRtpReceiveParameters_w(uint32_t ssrc,
496 webrtc::RtpParameters parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700497 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_AUDIO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 private:
500 // overrides from BaseChannel
johand89ab142016-10-25 10:50:32 -0700501 void OnPacketRead(rtc::PacketTransportInterface* transport,
502 const char* data,
503 size_t len,
504 const rtc::PacketTime& packet_time,
505 int flags) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700506 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200507 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
508 bool SetLocalContent_w(const MediaContentDescription* content,
509 ContentAction action,
510 std::string* error_desc) override;
511 bool SetRemoteContent_w(const MediaContentDescription* content,
512 ContentAction action,
513 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 void HandleEarlyMediaTimeout();
solenberg1d63dd02015-12-02 12:35:09 -0800515 bool InsertDtmf_w(uint32_t ssrc, int event, int duration);
solenberg4bac9c52015-10-09 02:32:53 -0700516 bool SetOutputVolume_w(uint32_t ssrc, double volume);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 bool GetStats_w(VoiceMediaInfo* stats);
518
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200519 void OnMessage(rtc::Message* pmsg) override;
520 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override;
521 void OnConnectionMonitorUpdate(
522 ConnectionMonitor* monitor,
523 const std::vector<ConnectionInfo>& infos) override;
524 void OnMediaMonitorUpdate(VoiceMediaChannel* media_channel,
525 const VoiceMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 void OnAudioMonitorUpdate(AudioMonitor* monitor, const AudioInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527
528 static const int kEarlyMediaTimeout = 1000;
Fredrik Solenberg0c022642015-08-05 12:25:22 +0200529 MediaEngineInterface* media_engine_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 bool received_media_;
kwiberg31022942016-03-11 14:18:21 -0800531 std::unique_ptr<VoiceMediaMonitor> media_monitor_;
532 std::unique_ptr<AudioMonitor> audio_monitor_;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700533
534 // Last AudioSendParameters sent down to the media_channel() via
535 // SetSendParameters.
536 AudioSendParameters last_send_params_;
537 // Last AudioRecvParameters sent down to the media_channel() via
538 // SetRecvParameters.
539 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540};
541
542// VideoChannel is a specialization for video.
543class VideoChannel : public BaseChannel {
544 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200545 VideoChannel(rtc::Thread* worker_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800546 rtc::Thread* network_thread,
547 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700548 VideoMediaChannel* channel,
deadbeefcbecd352015-09-23 11:50:27 -0700549 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -0800550 bool rtcp,
551 bool srtp_required);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 ~VideoChannel();
zhihuangf5b251b2017-01-12 19:37:48 -0800553 bool Init_w(TransportChannel* rtp_transport,
554 TransportChannel* rtcp_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200556 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200557 VideoMediaChannel* media_channel() const override {
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200558 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
559 }
560
nisseacd935b2016-11-11 03:55:13 -0800561 bool SetSink(uint32_t ssrc,
562 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 // Get statistics about the current media session.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000564 bool GetStats(VideoMediaInfo* stats);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565
566 sigslot::signal2<VideoChannel*, const std::vector<ConnectionInfo>&>
567 SignalConnectionMonitor;
568
569 void StartMediaMonitor(int cms);
570 void StopMediaMonitor();
571 sigslot::signal2<VideoChannel*, const VideoMediaInfo&> SignalMediaMonitor;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572
deadbeef5a4a75a2016-06-02 16:23:38 -0700573 // Register a source and set options.
574 // The |ssrc| must correspond to a registered send stream.
575 bool SetVideoSend(uint32_t ssrc,
576 bool enable,
577 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -0800578 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700579 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const;
580 bool SetRtpSendParameters(uint32_t ssrc,
581 const webrtc::RtpParameters& parameters);
582 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const;
583 bool SetRtpReceiveParameters(uint32_t ssrc,
584 const webrtc::RtpParameters& parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700585 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_VIDEO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700589 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200590 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
591 bool SetLocalContent_w(const MediaContentDescription* content,
592 ContentAction action,
593 std::string* error_desc) override;
594 bool SetRemoteContent_w(const MediaContentDescription* content,
595 ContentAction action,
596 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 bool GetStats_w(VideoMediaInfo* stats);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700598 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const;
599 bool SetRtpSendParameters_w(uint32_t ssrc, webrtc::RtpParameters parameters);
600 webrtc::RtpParameters GetRtpReceiveParameters_w(uint32_t ssrc) const;
601 bool SetRtpReceiveParameters_w(uint32_t ssrc,
602 webrtc::RtpParameters parameters);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200604 void OnMessage(rtc::Message* pmsg) override;
605 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override;
606 void OnConnectionMonitorUpdate(
607 ConnectionMonitor* monitor,
608 const std::vector<ConnectionInfo>& infos) override;
609 void OnMediaMonitorUpdate(VideoMediaChannel* media_channel,
610 const VideoMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611
kwiberg31022942016-03-11 14:18:21 -0800612 std::unique_ptr<VideoMediaMonitor> media_monitor_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700614 // Last VideoSendParameters sent down to the media_channel() via
615 // SetSendParameters.
616 VideoSendParameters last_send_params_;
617 // Last VideoRecvParameters sent down to the media_channel() via
618 // SetRecvParameters.
619 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620};
621
deadbeef953c2ce2017-01-09 14:53:41 -0800622// RtpDataChannel is a specialization for data.
623class RtpDataChannel : public BaseChannel {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 public:
deadbeef953c2ce2017-01-09 14:53:41 -0800625 RtpDataChannel(rtc::Thread* worker_thread,
626 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800627 rtc::Thread* signaling_thread,
628 DataMediaChannel* channel,
deadbeef953c2ce2017-01-09 14:53:41 -0800629 const std::string& content_name,
630 bool rtcp,
631 bool srtp_required);
632 ~RtpDataChannel();
zhihuangf5b251b2017-01-12 19:37:48 -0800633 bool Init_w(TransportChannel* rtp_transport,
634 TransportChannel* rtcp_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000636 virtual bool SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700637 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000638 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639
640 void StartMediaMonitor(int cms);
641 void StopMediaMonitor();
642
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000643 // Should be called on the signaling thread only.
644 bool ready_to_send_data() const {
645 return ready_to_send_data_;
646 }
647
deadbeef953c2ce2017-01-09 14:53:41 -0800648 sigslot::signal2<RtpDataChannel*, const DataMediaInfo&> SignalMediaMonitor;
649 sigslot::signal2<RtpDataChannel*, const std::vector<ConnectionInfo>&>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 SignalConnectionMonitor;
deadbeef953c2ce2017-01-09 14:53:41 -0800651
652 sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
653 SignalDataReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000655 // That occurs when the channel is enabled, the transport is writable,
656 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 sigslot::signal1<bool> SignalReadyToSendData;
zhihuang184a3fd2016-06-14 11:47:14 -0700658 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_DATA; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000660 protected:
661 // downcasts a MediaChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200662 DataMediaChannel* media_channel() const override {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000663 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
664 }
665
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000667 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 SendDataMessageData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700669 const rtc::CopyOnWriteBuffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 SendDataResult* result)
671 : params(params),
672 payload(payload),
673 result(result),
674 succeeded(false) {
675 }
676
677 const SendDataParams& params;
jbaucheec21bd2016-03-20 06:15:43 -0700678 const rtc::CopyOnWriteBuffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 SendDataResult* result;
680 bool succeeded;
681 };
682
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000683 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 // We copy the data because the data will become invalid after we
685 // handle DataMediaChannel::SignalDataReceived but before we fire
686 // SignalDataReceived.
687 DataReceivedMessageData(
688 const ReceiveDataParams& params, const char* data, size_t len)
689 : params(params),
690 payload(data, len) {
691 }
692 const ReceiveDataParams params;
jbaucheec21bd2016-03-20 06:15:43 -0700693 const rtc::CopyOnWriteBuffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 };
695
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000696 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000697
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 // overrides from BaseChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200699 const ContentInfo* GetFirstContent(const SessionDescription* sdesc) override;
deadbeef953c2ce2017-01-09 14:53:41 -0800700 // Checks that data channel type is RTP.
701 bool CheckDataChannelTypeFromContent(const DataContentDescription* content,
702 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200703 bool SetLocalContent_w(const MediaContentDescription* content,
704 ContentAction action,
705 std::string* error_desc) override;
706 bool SetRemoteContent_w(const MediaContentDescription* content,
707 ContentAction action,
708 std::string* error_desc) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700709 void UpdateMediaSendRecvState_w() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200711 void OnMessage(rtc::Message* pmsg) override;
712 void GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const override;
713 void OnConnectionMonitorUpdate(
714 ConnectionMonitor* monitor,
715 const std::vector<ConnectionInfo>& infos) override;
716 void OnMediaMonitorUpdate(DataMediaChannel* media_channel,
717 const DataMediaInfo& info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 void OnDataReceived(
719 const ReceiveDataParams& params, const char* data, size_t len);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200720 void OnDataChannelError(uint32_t ssrc, DataMediaChannel::Error error);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000721 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722
kwiberg31022942016-03-11 14:18:21 -0800723 std::unique_ptr<DataMediaMonitor> media_monitor_;
deadbeef953c2ce2017-01-09 14:53:41 -0800724 bool ready_to_send_data_ = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700725
726 // Last DataSendParameters sent down to the media_channel() via
727 // SetSendParameters.
728 DataSendParameters last_send_params_;
729 // Last DataRecvParameters sent down to the media_channel() via
730 // SetRecvParameters.
731 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732};
733
734} // namespace cricket
735
perkjc11b1842016-03-07 17:34:13 -0800736#endif // WEBRTC_PC_CHANNEL_H_