blob: ba584690ba6ad07557bc196a160330e1d844f507 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_CHANNEL_H_
12#define 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/call/audio_sink.h"
Steve Anton3828c062017-12-06 10:34:51 -080022#include "api/jsep.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/rtpreceiverinterface.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020024#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020025#include "api/video/video_source_interface.h"
Zhi Huang365381f2018-04-13 16:44:34 -070026#include "call/rtp_packet_sink_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "media/base/mediachannel.h"
28#include "media/base/mediaengine.h"
29#include "media/base/streamparams.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "p2p/base/dtlstransportinternal.h"
31#include "p2p/base/packettransportinternal.h"
Zhi Huangcd3fc5d2017-11-29 10:41:57 -080032#include "pc/dtlssrtptransport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "pc/mediasession.h"
Zhi Huangcd3fc5d2017-11-29 10:41:57 -080034#include "pc/rtptransport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "pc/srtpfilter.h"
Zhi Huangcd3fc5d2017-11-29 10:41:57 -080036#include "pc/srtptransport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/asyncinvoker.h"
38#include "rtc_base/asyncudpsocket.h"
39#include "rtc_base/criticalsection.h"
40#include "rtc_base/network.h"
Artem Titove41c4332018-07-25 15:04:28 +020041#include "rtc_base/third_party/sigslot/sigslot.h"
Tommif888bb52015-12-12 01:37:01 +010042
43namespace webrtc {
44class AudioSinkInterface;
45} // namespace webrtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
47namespace cricket {
48
49struct CryptoParams;
50class MediaContentDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
deadbeef062ce9f2016-08-26 21:42:15 -070052// BaseChannel contains logic common to voice and video, including enable,
53// marshaling calls to a worker and network threads, and connection and media
54// monitors.
55//
Danil Chapovalov33b01f22016-05-11 19:55:27 +020056// BaseChannel assumes signaling and other threads are allowed to make
57// synchronous calls to the worker thread, the worker thread makes synchronous
58// calls only to the network thread, and the network thread can't be blocked by
59// other threads.
60// All methods with _n suffix must be called on network thread,
deadbeef062ce9f2016-08-26 21:42:15 -070061// methods with _w suffix on worker thread
Danil Chapovalov33b01f22016-05-11 19:55:27 +020062// and methods with _s suffix on signaling thread.
63// Network and worker threads may be the same thread.
wu@webrtc.org78187522013-10-07 23:32:02 +000064//
65// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
66// This is required to avoid a data race between the destructor modifying the
67// vtable, and the media channel's thread using BaseChannel as the
68// NetworkInterface.
69
Zhi Huang365381f2018-04-13 16:44:34 -070070class BaseChannel : public rtc::MessageHandler,
71 public sigslot::has_slots<>,
72 public MediaChannel::NetworkInterface,
73 public webrtc::RtpPacketSinkInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 public:
deadbeef7af91dd2016-12-13 11:29:11 -080075 // If |srtp_required| is true, the channel will not send or receive any
76 // RTP/RTCP packets without using SRTP (either using SDES or DTLS-SRTP).
Zhi Huange830e682018-03-30 10:48:35 -070077 // TODO(zhihuang:) Create a BaseChannel::Config struct for the parameter lists
78 // which will make it easier to change the constructor.
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,
Steve Anton8699a322017-11-06 15:53:33 -080082 std::unique_ptr<MediaChannel> media_channel,
deadbeefcbecd352015-09-23 11:50:27 -070083 const std::string& content_name,
Zhi Huange830e682018-03-30 10:48:35 -070084 bool srtp_required,
85 rtc::CryptoOptions crypto_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 virtual ~BaseChannel();
Zhi Huang2dfc42d2017-12-04 13:38:48 -080087 void Init_w(webrtc::RtpTransportInternal* rtp_transport);
88
Danil Chapovalov33b01f22016-05-11 19:55:27 +020089 // Deinit may be called multiple times and is simply ignored if it's already
wu@webrtc.org78187522013-10-07 23:32:02 +000090 // done.
91 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093 rtc::Thread* worker_thread() const { return worker_thread_; }
Danil Chapovalov33b01f22016-05-11 19:55:27 +020094 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070095 const std::string& content_name() const { return content_name_; }
deadbeeff5346592017-01-24 21:51:21 -080096 // TODO(deadbeef): This is redundant; remove this.
deadbeefcbecd352015-09-23 11:50:27 -070097 const std::string& transport_name() const { return transport_name_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 bool enabled() const { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
Zhi Huangcf990f52017-09-22 12:12:30 -0700100 // This function returns true if using SRTP (DTLS-based keying or SDES).
Zhi Huange830e682018-03-30 10:48:35 -0700101 bool srtp_active() const {
102 return rtp_transport_ && rtp_transport_->IsSrtpActive();
103 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
105 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800107 // Set an RTP level transport which could be an RtpTransport without
108 // encryption, an SrtpTransport for SDES or a DtlsSrtpTransport for DTLS-SRTP.
109 // This can be called from any thread and it hops to the network thread
110 // internally. It would replace the |SetTransports| and its variants.
Zhi Huang365381f2018-04-13 16:44:34 -0700111 bool SetRtpTransport(webrtc::RtpTransportInternal* rtp_transport);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800112
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 // Channel control
114 bool SetLocalContent(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800115 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000116 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 bool SetRemoteContent(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800118 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000119 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120
121 bool Enable(bool enable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
Zhi Huang365381f2018-04-13 16:44:34 -0700123 // TODO(zhihuang): These methods are used for testing and can be removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 bool AddRecvStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200125 bool RemoveRecvStream(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000126 bool AddSendStream(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200127 bool RemoveSendStream(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 const std::vector<StreamParams>& local_streams() const {
130 return local_streams_;
131 }
132 const std::vector<StreamParams>& remote_streams() const {
133 return remote_streams_;
134 }
135
deadbeef953c2ce2017-01-09 14:53:41 -0800136 sigslot::signal2<BaseChannel*, bool> SignalDtlsSrtpSetupFailure;
137 void SignalDtlsSrtpSetupFailure_n(bool rtcp);
138 void SignalDtlsSrtpSetupFailure_s(bool rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000139
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000140 // Used for latency measurements.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 sigslot::signal1<BaseChannel*> SignalFirstPacketReceived;
142
zhihuangb2cdd932017-01-19 16:54:25 -0800143 // Forward SignalSentPacket to worker thread.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200144 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket;
145
deadbeefac22f702017-01-12 21:59:29 -0800146 // Emitted whenever rtcp-mux is fully negotiated and the rtcp-transport can
147 // be destroyed.
148 // Fired on the network thread.
149 sigslot::signal1<const std::string&> SignalRtcpMuxFullyActive;
zhihuangf5b251b2017-01-12 19:37:48 -0800150
Zhi Huange830e682018-03-30 10:48:35 -0700151 rtc::PacketTransportInternal* rtp_packet_transport() {
152 if (rtp_transport_) {
153 return rtp_transport_->rtp_packet_transport();
154 }
155 return nullptr;
zhihuangb2cdd932017-01-19 16:54:25 -0800156 }
zhihuangf5b251b2017-01-12 19:37:48 -0800157
Zhi Huange830e682018-03-30 10:48:35 -0700158 rtc::PacketTransportInternal* rtcp_packet_transport() {
159 if (rtp_transport_) {
160 return rtp_transport_->rtcp_packet_transport();
161 }
162 return nullptr;
163 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200164
zstein56162b92017-04-24 16:54:35 -0700165 // From RtpTransport - public for testing only
166 void OnTransportReadyToSend(bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000168 // Only public for unit tests. Otherwise, consider protected.
Yves Gerey665174f2018-06-19 15:03:05 +0200169 int SetOption(SocketType type, rtc::Socket::Option o, int val) override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200170 int SetOption_n(SocketType type, rtc::Socket::Option o, int val);
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000171
zhihuang184a3fd2016-06-14 11:47:14 -0700172 virtual cricket::MediaType media_type() = 0;
173
Zhi Huang365381f2018-04-13 16:44:34 -0700174 // RtpPacketSinkInterface overrides.
175 void OnRtpPacket(const webrtc::RtpPacketReceived& packet) override;
zstein3dcf0e92017-06-01 13:22:42 -0700176
Steve Anton593e3252017-12-15 11:44:48 -0800177 // Used by the RTCStatsCollector tests to set the transport name without
178 // creating RtpTransports.
179 void set_transport_name_for_testing(const std::string& transport_name) {
180 transport_name_ = transport_name;
181 }
182
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 protected:
Steve Anton8699a322017-11-06 15:53:33 -0800184 virtual MediaChannel* media_channel() const { return media_channel_.get(); }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700185
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 bool was_ever_writable() const { return was_ever_writable_; }
Steve Anton4e70a722017-11-28 14:57:10 -0800187 void set_local_content_direction(webrtc::RtpTransceiverDirection direction) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 local_content_direction_ = direction;
189 }
Steve Anton4e70a722017-11-28 14:57:10 -0800190 void set_remote_content_direction(webrtc::RtpTransceiverDirection direction) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 remote_content_direction_ = direction;
192 }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700193 // These methods verify that:
194 // * The required content description directions have been set.
195 // * The channel is enabled.
196 // * And for sending:
197 // - The SRTP filter is active if it's needed.
198 // - The transport has been writable before, meaning it should be at least
199 // possible to succeed in sending a packet.
200 //
201 // When any of these properties change, UpdateMediaSendRecvState_w should be
202 // called.
203 bool IsReadyToReceiveMedia_w() const;
204 bool IsReadyToSendMedia_w() const;
zhihuangf5b251b2017-01-12 19:37:48 -0800205 rtc::Thread* signaling_thread() { return signaling_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200207 void FlushRtcpMessages_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
209 // NetworkInterface implementation, called by MediaEngine
jbaucheec21bd2016-03-20 06:15:43 -0700210 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
211 const rtc::PacketOptions& options) override;
212 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
213 const rtc::PacketOptions& options) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800215 // From RtpTransportInternal
216 void OnWritableState(bool writable);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800217
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200218 void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700219
deadbeef5bd5ca32017-02-10 11:31:50 -0800220 bool PacketIsRtcp(const rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700221 const char* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700223 bool SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700224 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700225 const rtc::PacketOptions& options);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200226
Zhi Huang365381f2018-04-13 16:44:34 -0700227 void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer* packet,
228 const rtc::PacketTime& packet_time);
229
Steve Anton0807d152018-03-05 11:23:09 -0800230 void OnPacketReceived(bool rtcp,
Zhi Huang365381f2018-04-13 16:44:34 -0700231 const rtc::CopyOnWriteBuffer& packet,
Steve Anton0807d152018-03-05 11:23:09 -0800232 const rtc::PacketTime& packet_time);
zstein3dcf0e92017-06-01 13:22:42 -0700233 void ProcessPacket(bool rtcp,
234 const rtc::CopyOnWriteBuffer& packet,
235 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 void EnableMedia_w();
238 void DisableMedia_w();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700239
240 // Performs actions if the RTP/RTCP writable state changed. This should
241 // be called whenever a channel's writable state changes or when RTCP muxing
242 // becomes active/inactive.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200243 void UpdateWritableState_n();
244 void ChannelWritable_n();
245 void ChannelNotWritable_n();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700246
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200248 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000249 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200250 bool RemoveSendStream_w(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700252 // Should be called whenever the conditions for
253 // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied).
254 // Updates the send/recv state of the media channel.
255 void UpdateMediaSendRecvState();
256 virtual void UpdateMediaSendRecvState_w() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
Steve Anton3828c062017-12-06 10:34:51 -0800259 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000260 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
Steve Anton3828c062017-12-06 10:34:51 -0800262 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000263 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 virtual bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800265 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000266 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800268 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000269 std::string* error_desc) = 0;
jbauch5869f502017-06-29 12:31:36 -0700270 // Return a list of RTP header extensions with the non-encrypted extensions
271 // removed depending on the current crypto_options_ and only if both the
272 // non-encrypted and encrypted extension is present for the same URI.
273 RtpHeaderExtensions GetFilteredRtpHeaderExtensions(
274 const RtpHeaderExtensions& extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700277 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278
stefanf79ade12017-06-02 06:44:03 -0700279 // Helper function template for invoking methods on the worker thread.
280 template <class T, class FunctorT>
281 T InvokeOnWorker(const rtc::Location& posted_from, const FunctorT& functor) {
282 return worker_thread_->Invoke<T>(posted_from, functor);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000283 }
284
zstein3dcf0e92017-06-01 13:22:42 -0700285 void AddHandledPayloadType(int payload_type);
286
Zhi Huang365381f2018-04-13 16:44:34 -0700287 void UpdateRtpHeaderExtensionMap(
288 const RtpHeaderExtensions& header_extensions);
289
290 bool RegisterRtpDemuxerSink();
291
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 private:
Zhi Huang365381f2018-04-13 16:44:34 -0700293 bool ConnectToRtpTransport();
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800294 void DisconnectFromRtpTransport();
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800295 void SignalSentPacket_n(const rtc::SentPacket& sent_packet);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200296 void SignalSentPacket_w(const rtc::SentPacket& sent_packet);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700297 bool IsReadyToSendMedia_n() const;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200298 rtc::Thread* const worker_thread_;
299 rtc::Thread* const network_thread_;
zhihuangf5b251b2017-01-12 19:37:48 -0800300 rtc::Thread* const signaling_thread_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200301 rtc::AsyncInvoker invoker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000303 const std::string content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200304
deadbeeff5346592017-01-24 21:51:21 -0800305 // Won't be set when using raw packet transports. SDP-specific thing.
deadbeefcbecd352015-09-23 11:50:27 -0700306 std::string transport_name_;
zhihuangb2cdd932017-01-19 16:54:25 -0800307
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800308 webrtc::RtpTransportInternal* rtp_transport_ = nullptr;
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800309
deadbeeff5346592017-01-24 21:51:21 -0800310 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
deadbeefcbecd352015-09-23 11:50:27 -0700311 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
deadbeef23d947d2016-08-22 16:00:30 -0700312 bool writable_ = false;
313 bool was_ever_writable_ = false;
314 bool has_received_packet_ = false;
deadbeef7af91dd2016-12-13 11:29:11 -0800315 const bool srtp_required_ = true;
Zhi Huange830e682018-03-30 10:48:35 -0700316 rtc::CryptoOptions crypto_options_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200317
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700318 // MediaChannel related members that should be accessed from the worker
319 // thread.
Steve Anton8699a322017-11-06 15:53:33 -0800320 std::unique_ptr<MediaChannel> media_channel_;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700321 // Currently the |enabled_| flag is accessed from the signaling thread as
322 // well, but it can be changed only when signaling thread does a synchronous
323 // call to the worker thread, so it should be safe.
deadbeef23d947d2016-08-22 16:00:30 -0700324 bool enabled_ = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200325 std::vector<StreamParams> local_streams_;
326 std::vector<StreamParams> remote_streams_;
Steve Anton4e70a722017-11-28 14:57:10 -0800327 webrtc::RtpTransceiverDirection local_content_direction_ =
328 webrtc::RtpTransceiverDirection::kInactive;
329 webrtc::RtpTransceiverDirection remote_content_direction_ =
330 webrtc::RtpTransceiverDirection::kInactive;
Zhi Huangc99b6c72017-11-10 16:44:46 -0800331
Zhi Huang365381f2018-04-13 16:44:34 -0700332 webrtc::RtpDemuxerCriteria demuxer_criteria_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333};
334
335// VoiceChannel is a specialization that adds support for early media, DTMF,
336// and input/output level monitoring.
337class VoiceChannel : public BaseChannel {
338 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200339 VoiceChannel(rtc::Thread* worker_thread,
340 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800341 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700342 MediaEngineInterface* media_engine,
Steve Anton8699a322017-11-06 15:53:33 -0800343 std::unique_ptr<VoiceMediaChannel> channel,
deadbeefcbecd352015-09-23 11:50:27 -0700344 const std::string& content_name,
Zhi Huange830e682018-03-30 10:48:35 -0700345 bool srtp_required,
346 rtc::CryptoOptions crypto_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 ~VoiceChannel();
solenberg1dd98f32015-09-10 01:57:14 -0700348
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200350 VoiceMediaChannel* media_channel() const override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
352 }
353
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700354 webrtc::RtpParameters GetRtpSendParameters_w(uint32_t ssrc) const;
Zach Steinba37b4b2018-01-23 15:02:36 -0800355 webrtc::RTCError SetRtpSendParameters_w(uint32_t ssrc,
356 webrtc::RtpParameters parameters);
zhihuang184a3fd2016-06-14 11:47:14 -0700357 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_AUDIO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 private:
360 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700361 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200362 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800363 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200364 std::string* error_desc) override;
365 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800366 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200367 std::string* error_desc) override;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700368
369 // Last AudioSendParameters sent down to the media_channel() via
370 // SetSendParameters.
371 AudioSendParameters last_send_params_;
372 // Last AudioRecvParameters sent down to the media_channel() via
373 // SetRecvParameters.
374 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375};
376
377// VideoChannel is a specialization for video.
378class VideoChannel : public BaseChannel {
379 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200380 VideoChannel(rtc::Thread* worker_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800381 rtc::Thread* network_thread,
382 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 15:53:33 -0800383 std::unique_ptr<VideoMediaChannel> media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700384 const std::string& content_name,
Zhi Huange830e682018-03-30 10:48:35 -0700385 bool srtp_required,
386 rtc::CryptoOptions crypto_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 ~VideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200389 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200390 VideoMediaChannel* media_channel() const override {
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200391 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
392 }
393
stefanf79ade12017-06-02 06:44:03 -0700394 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395
zhihuang184a3fd2016-06-14 11:47:14 -0700396 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_VIDEO; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700400 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200401 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800402 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200403 std::string* error_desc) override;
404 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800405 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200406 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700408 // Last VideoSendParameters sent down to the media_channel() via
409 // SetSendParameters.
410 VideoSendParameters last_send_params_;
411 // Last VideoRecvParameters sent down to the media_channel() via
412 // SetRecvParameters.
413 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414};
415
deadbeef953c2ce2017-01-09 14:53:41 -0800416// RtpDataChannel is a specialization for data.
417class RtpDataChannel : public BaseChannel {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 public:
deadbeef953c2ce2017-01-09 14:53:41 -0800419 RtpDataChannel(rtc::Thread* worker_thread,
420 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800421 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 15:53:33 -0800422 std::unique_ptr<DataMediaChannel> channel,
deadbeef953c2ce2017-01-09 14:53:41 -0800423 const std::string& content_name,
Zhi Huange830e682018-03-30 10:48:35 -0700424 bool srtp_required,
425 rtc::CryptoOptions crypto_options);
deadbeef953c2ce2017-01-09 14:53:41 -0800426 ~RtpDataChannel();
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800427 // TODO(zhihuang): Remove this once the RtpTransport can be shared between
428 // BaseChannels.
Steve Anton8699a322017-11-06 15:53:33 -0800429 void Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800430 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800431 rtc::PacketTransportInternal* rtp_packet_transport,
432 rtc::PacketTransportInternal* rtcp_packet_transport);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800433 void Init_w(webrtc::RtpTransportInternal* rtp_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000435 virtual bool SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700436 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000437 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000439 // Should be called on the signaling thread only.
Yves Gerey665174f2018-06-19 15:03:05 +0200440 bool ready_to_send_data() const { return ready_to_send_data_; }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000441
deadbeef953c2ce2017-01-09 14:53:41 -0800442 sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
443 SignalDataReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000445 // That occurs when the channel is enabled, the transport is writable,
446 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 sigslot::signal1<bool> SignalReadyToSendData;
zhihuang184a3fd2016-06-14 11:47:14 -0700448 cricket::MediaType media_type() override { return cricket::MEDIA_TYPE_DATA; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000450 protected:
451 // downcasts a MediaChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200452 DataMediaChannel* media_channel() const override {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000453 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
454 }
455
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000457 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 SendDataMessageData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700459 const rtc::CopyOnWriteBuffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 SendDataResult* result)
Yves Gerey665174f2018-06-19 15:03:05 +0200461 : params(params), payload(payload), result(result), succeeded(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462
463 const SendDataParams& params;
jbaucheec21bd2016-03-20 06:15:43 -0700464 const rtc::CopyOnWriteBuffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 SendDataResult* result;
466 bool succeeded;
467 };
468
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000469 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 // We copy the data because the data will become invalid after we
471 // handle DataMediaChannel::SignalDataReceived but before we fire
472 // SignalDataReceived.
Yves Gerey665174f2018-06-19 15:03:05 +0200473 DataReceivedMessageData(const ReceiveDataParams& params,
474 const char* data,
475 size_t len)
476 : params(params), payload(data, len) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 const ReceiveDataParams params;
jbaucheec21bd2016-03-20 06:15:43 -0700478 const rtc::CopyOnWriteBuffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 };
480
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000481 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000482
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 // overrides from BaseChannel
deadbeef953c2ce2017-01-09 14:53:41 -0800484 // Checks that data channel type is RTP.
485 bool CheckDataChannelTypeFromContent(const DataContentDescription* content,
486 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200487 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800488 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200489 std::string* error_desc) override;
490 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800491 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200492 std::string* error_desc) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700493 void UpdateMediaSendRecvState_w() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200495 void OnMessage(rtc::Message* pmsg) override;
Yves Gerey665174f2018-06-19 15:03:05 +0200496 void OnDataReceived(const ReceiveDataParams& params,
497 const char* data,
498 size_t len);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000499 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500
deadbeef953c2ce2017-01-09 14:53:41 -0800501 bool ready_to_send_data_ = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700502
503 // Last DataSendParameters sent down to the media_channel() via
504 // SetSendParameters.
505 DataSendParameters last_send_params_;
506 // Last DataRecvParameters sent down to the media_channel() via
507 // SetRecvParameters.
508 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509};
510
511} // namespace cricket
512
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200513#endif // PC_CHANNEL_H_