blob: 227c5933125c87079437c83640bbdcafde06736e [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"
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -080023#include "api/media_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "api/rtp_receiver_interface.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020025#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020026#include "api/video/video_source_interface.h"
Zhi Huang365381f2018-04-13 16:44:34 -070027#include "call/rtp_packet_sink_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "media/base/media_channel.h"
29#include "media/base/media_engine.h"
30#include "media/base/stream_params.h"
31#include "p2p/base/dtls_transport_internal.h"
32#include "p2p/base/packet_transport_internal.h"
33#include "pc/channel_interface.h"
34#include "pc/dtls_srtp_transport.h"
35#include "pc/media_session.h"
36#include "pc/rtp_transport.h"
37#include "pc/srtp_filter.h"
38#include "pc/srtp_transport.h"
39#include "rtc_base/async_invoker.h"
40#include "rtc_base/async_udp_socket.h"
41#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/network.h"
Artem Titove41c4332018-07-25 15:04:28 +020043#include "rtc_base/third_party/sigslot/sigslot.h"
Tommif888bb52015-12-12 01:37:01 +010044
45namespace webrtc {
46class AudioSinkInterface;
Anton Sukhanov98a462c2018-10-17 13:15:42 -070047class MediaTransportInterface;
Tommif888bb52015-12-12 01:37:01 +010048} // namespace webrtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
50namespace cricket {
51
52struct CryptoParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
deadbeef062ce9f2016-08-26 21:42:15 -070054// BaseChannel contains logic common to voice and video, including enable,
55// marshaling calls to a worker and network threads, and connection and media
56// monitors.
57//
Danil Chapovalov33b01f22016-05-11 19:55:27 +020058// BaseChannel assumes signaling and other threads are allowed to make
59// synchronous calls to the worker thread, the worker thread makes synchronous
60// calls only to the network thread, and the network thread can't be blocked by
61// other threads.
62// All methods with _n suffix must be called on network thread,
deadbeef062ce9f2016-08-26 21:42:15 -070063// methods with _w suffix on worker thread
Danil Chapovalov33b01f22016-05-11 19:55:27 +020064// and methods with _s suffix on signaling thread.
65// Network and worker threads may be the same thread.
wu@webrtc.org78187522013-10-07 23:32:02 +000066//
67// WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
68// This is required to avoid a data race between the destructor modifying the
69// vtable, and the media channel's thread using BaseChannel as the
70// NetworkInterface.
71
Amit Hilbuchdd9390c2018-11-13 16:26:05 -080072class BaseChannel : public ChannelInterface,
73 public rtc::MessageHandler,
Zhi Huang365381f2018-04-13 16:44:34 -070074 public sigslot::has_slots<>,
75 public MediaChannel::NetworkInterface,
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -080076 public webrtc::RtpPacketSinkInterface,
77 public webrtc::MediaTransportNetworkChangeCallback {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 public:
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).
Zhi Huange830e682018-03-30 10:48:35 -070081 // TODO(zhihuang:) Create a BaseChannel::Config struct for the parameter lists
82 // which will make it easier to change the constructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +020083 BaseChannel(rtc::Thread* worker_thread,
84 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -080085 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 15:53:33 -080086 std::unique_ptr<MediaChannel> media_channel,
deadbeefcbecd352015-09-23 11:50:27 -070087 const std::string& content_name,
Zhi Huange830e682018-03-30 10:48:35 -070088 bool srtp_required,
Benjamin Wrighta54daf12018-10-11 15:33:17 -070089 webrtc::CryptoOptions crypto_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 virtual ~BaseChannel();
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -080091 virtual void Init_w(webrtc::RtpTransportInternal* rtp_transport,
92 webrtc::MediaTransportInterface* media_transport);
Zhi Huang2dfc42d2017-12-04 13:38:48 -080093
Danil Chapovalov33b01f22016-05-11 19:55:27 +020094 // Deinit may be called multiple times and is simply ignored if it's already
wu@webrtc.org78187522013-10-07 23:32:02 +000095 // done.
96 void Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000098 rtc::Thread* worker_thread() const { return worker_thread_; }
Danil Chapovalov33b01f22016-05-11 19:55:27 +020099 rtc::Thread* network_thread() const { return network_thread_; }
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800100 const std::string& content_name() const override { return content_name_; }
deadbeeff5346592017-01-24 21:51:21 -0800101 // TODO(deadbeef): This is redundant; remove this.
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800102 const std::string& transport_name() const override { return transport_name_; }
103 bool enabled() const override { return enabled_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
Zhi Huangcf990f52017-09-22 12:12:30 -0700105 // This function returns true if using SRTP (DTLS-based keying or SDES).
Zhi Huange830e682018-03-30 10:48:35 -0700106 bool srtp_active() const {
107 return rtp_transport_ && rtp_transport_->IsSrtpActive();
108 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
110 bool writable() const { return writable_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800112 // Set an RTP level transport which could be an RtpTransport without
113 // encryption, an SrtpTransport for SDES or a DtlsSrtpTransport for DTLS-SRTP.
114 // This can be called from any thread and it hops to the network thread
115 // internally. It would replace the |SetTransports| and its variants.
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800116 bool SetRtpTransport(webrtc::RtpTransportInternal* rtp_transport) override;
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800117
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 // Channel control
119 bool SetLocalContent(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800120 webrtc::SdpType type,
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800121 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 bool SetRemoteContent(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800123 webrtc::SdpType type,
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800124 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800126 bool Enable(bool enable) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 const std::vector<StreamParams>& local_streams() const {
129 return local_streams_;
130 }
131 const std::vector<StreamParams>& remote_streams() const {
132 return remote_streams_;
133 }
134
deadbeef953c2ce2017-01-09 14:53:41 -0800135 sigslot::signal2<BaseChannel*, bool> SignalDtlsSrtpSetupFailure;
136 void SignalDtlsSrtpSetupFailure_n(bool rtcp);
137 void SignalDtlsSrtpSetupFailure_s(bool rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000138
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000139 // Used for latency measurements.
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800140 sigslot::signal1<ChannelInterface*>& SignalFirstPacketReceived() override {
141 return SignalFirstPacketReceived_;
142 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143
zhihuangb2cdd932017-01-19 16:54:25 -0800144 // Forward SignalSentPacket to worker thread.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200145 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket;
146
deadbeefac22f702017-01-12 21:59:29 -0800147 // Emitted whenever rtcp-mux is fully negotiated and the rtcp-transport can
148 // be destroyed.
149 // Fired on the network thread.
150 sigslot::signal1<const std::string&> SignalRtcpMuxFullyActive;
zhihuangf5b251b2017-01-12 19:37:48 -0800151
Zhi Huange830e682018-03-30 10:48:35 -0700152 rtc::PacketTransportInternal* rtp_packet_transport() {
153 if (rtp_transport_) {
154 return rtp_transport_->rtp_packet_transport();
155 }
156 return nullptr;
zhihuangb2cdd932017-01-19 16:54:25 -0800157 }
zhihuangf5b251b2017-01-12 19:37:48 -0800158
Zhi Huange830e682018-03-30 10:48:35 -0700159 rtc::PacketTransportInternal* rtcp_packet_transport() {
160 if (rtp_transport_) {
161 return rtp_transport_->rtcp_packet_transport();
162 }
163 return nullptr;
164 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200165
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700166 // Returns media transport, can be null if media transport is not available.
167 webrtc::MediaTransportInterface* media_transport() {
168 return media_transport_;
169 }
170
zstein56162b92017-04-24 16:54:35 -0700171 // From RtpTransport - public for testing only
172 void OnTransportReadyToSend(bool ready);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000174 // Only public for unit tests. Otherwise, consider protected.
Yves Gerey665174f2018-06-19 15:03:05 +0200175 int SetOption(SocketType type, rtc::Socket::Option o, int val) override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200176 int SetOption_n(SocketType type, rtc::Socket::Option o, int val);
guoweis@webrtc.org4f852882015-03-12 20:09:44 +0000177
Zhi Huang365381f2018-04-13 16:44:34 -0700178 // RtpPacketSinkInterface overrides.
179 void OnRtpPacket(const webrtc::RtpPacketReceived& packet) override;
zstein3dcf0e92017-06-01 13:22:42 -0700180
Steve Anton593e3252017-12-15 11:44:48 -0800181 // Used by the RTCStatsCollector tests to set the transport name without
182 // creating RtpTransports.
183 void set_transport_name_for_testing(const std::string& transport_name) {
184 transport_name_ = transport_name;
185 }
186
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800187 MediaChannel* media_channel() const override { return media_channel_.get(); }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700188
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800189 protected:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 bool was_ever_writable() const { return was_ever_writable_; }
Steve Anton4e70a722017-11-28 14:57:10 -0800191 void set_local_content_direction(webrtc::RtpTransceiverDirection direction) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 local_content_direction_ = direction;
193 }
Steve Anton4e70a722017-11-28 14:57:10 -0800194 void set_remote_content_direction(webrtc::RtpTransceiverDirection direction) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 remote_content_direction_ = direction;
196 }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700197 // These methods verify that:
198 // * The required content description directions have been set.
199 // * The channel is enabled.
200 // * And for sending:
201 // - The SRTP filter is active if it's needed.
202 // - The transport has been writable before, meaning it should be at least
203 // possible to succeed in sending a packet.
204 //
205 // When any of these properties change, UpdateMediaSendRecvState_w should be
206 // called.
207 bool IsReadyToReceiveMedia_w() const;
208 bool IsReadyToSendMedia_w() const;
zhihuangf5b251b2017-01-12 19:37:48 -0800209 rtc::Thread* signaling_thread() { return signaling_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200211 void FlushRtcpMessages_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212
213 // NetworkInterface implementation, called by MediaEngine
jbaucheec21bd2016-03-20 06:15:43 -0700214 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
215 const rtc::PacketOptions& options) override;
216 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
217 const rtc::PacketOptions& options) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800219 // From RtpTransportInternal
220 void OnWritableState(bool writable);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800221
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200222 void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700223
deadbeef5bd5ca32017-02-10 11:31:50 -0800224 bool PacketIsRtcp(const rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700225 const char* data,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 size_t len);
stefanc1aeaf02015-10-15 07:26:07 -0700227 bool SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700228 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700229 const rtc::PacketOptions& options);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200230
Zhi Huang365381f2018-04-13 16:44:34 -0700231 void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer* packet,
Niels Möllere6933812018-11-05 13:01:41 +0100232 int64_t packet_time_us);
Zhi Huang365381f2018-04-13 16:44:34 -0700233
Steve Anton0807d152018-03-05 11:23:09 -0800234 void OnPacketReceived(bool rtcp,
Zhi Huang365381f2018-04-13 16:44:34 -0700235 const rtc::CopyOnWriteBuffer& packet,
Niels Möllere6933812018-11-05 13:01:41 +0100236 int64_t packet_time_us);
zstein3dcf0e92017-06-01 13:22:42 -0700237 void ProcessPacket(bool rtcp,
238 const rtc::CopyOnWriteBuffer& packet,
Niels Möllere6933812018-11-05 13:01:41 +0100239 int64_t packet_time_us);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 void EnableMedia_w();
242 void DisableMedia_w();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700243
244 // Performs actions if the RTP/RTCP writable state changed. This should
245 // be called whenever a channel's writable state changes or when RTCP muxing
246 // becomes active/inactive.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200247 void UpdateWritableState_n();
248 void ChannelWritable_n();
249 void ChannelNotWritable_n();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700250
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 bool AddRecvStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200252 bool RemoveRecvStream_w(uint32_t ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000253 bool AddSendStream_w(const StreamParams& sp);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200254 bool RemoveSendStream_w(uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700256 // Should be called whenever the conditions for
257 // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied).
258 // Updates the send/recv state of the media channel.
259 void UpdateMediaSendRecvState();
260 virtual void UpdateMediaSendRecvState_w() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
Steve Anton3828c062017-12-06 10:34:51 -0800263 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000264 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
Steve Anton3828c062017-12-06 10:34:51 -0800266 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000267 std::string* error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 virtual bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800269 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000270 std::string* error_desc) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800272 webrtc::SdpType type,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000273 std::string* error_desc) = 0;
jbauch5869f502017-06-29 12:31:36 -0700274 // Return a list of RTP header extensions with the non-encrypted extensions
275 // removed depending on the current crypto_options_ and only if both the
276 // non-encrypted and encrypted extension is present for the same URI.
277 RtpHeaderExtensions GetFilteredRtpHeaderExtensions(
278 const RtpHeaderExtensions& extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 // From MessageHandler
rlesterec9d1872015-10-27 14:22:16 -0700281 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282
stefanf79ade12017-06-02 06:44:03 -0700283 // Helper function template for invoking methods on the worker thread.
284 template <class T, class FunctorT>
285 T InvokeOnWorker(const rtc::Location& posted_from, const FunctorT& functor) {
286 return worker_thread_->Invoke<T>(posted_from, functor);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000287 }
288
zstein3dcf0e92017-06-01 13:22:42 -0700289 void AddHandledPayloadType(int payload_type);
290
Zhi Huang365381f2018-04-13 16:44:34 -0700291 void UpdateRtpHeaderExtensionMap(
292 const RtpHeaderExtensions& header_extensions);
293
294 bool RegisterRtpDemuxerSink();
295
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800296 bool has_received_packet_ = false;
297
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 private:
Zhi Huang365381f2018-04-13 16:44:34 -0700299 bool ConnectToRtpTransport();
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800300 void DisconnectFromRtpTransport();
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800301 void SignalSentPacket_n(const rtc::SentPacket& sent_packet);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200302 void SignalSentPacket_w(const rtc::SentPacket& sent_packet);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700303 bool IsReadyToSendMedia_n() const;
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800304
305 // MediaTransportNetworkChangeCallback override.
306 void OnNetworkRouteChanged(const rtc::NetworkRoute& network_route) override;
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800307
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200308 rtc::Thread* const worker_thread_;
309 rtc::Thread* const network_thread_;
zhihuangf5b251b2017-01-12 19:37:48 -0800310 rtc::Thread* const signaling_thread_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200311 rtc::AsyncInvoker invoker_;
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800312 sigslot::signal1<ChannelInterface*> SignalFirstPacketReceived_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +0000314 const std::string content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200315
deadbeeff5346592017-01-24 21:51:21 -0800316 // Won't be set when using raw packet transports. SDP-specific thing.
deadbeefcbecd352015-09-23 11:50:27 -0700317 std::string transport_name_;
zhihuangb2cdd932017-01-19 16:54:25 -0800318
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800319 webrtc::RtpTransportInternal* rtp_transport_ = nullptr;
Zhi Huangcd3fc5d2017-11-29 10:41:57 -0800320
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700321 // Optional media transport (experimental).
322 // If provided, audio and video will be sent through media_transport instead
323 // of RTP/RTCP. Currently media_transport can co-exist with rtp_transport.
324 webrtc::MediaTransportInterface* media_transport_ = nullptr;
325
deadbeeff5346592017-01-24 21:51:21 -0800326 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
deadbeefcbecd352015-09-23 11:50:27 -0700327 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
deadbeef23d947d2016-08-22 16:00:30 -0700328 bool writable_ = false;
329 bool was_ever_writable_ = false;
deadbeef7af91dd2016-12-13 11:29:11 -0800330 const bool srtp_required_ = true;
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700331 webrtc::CryptoOptions crypto_options_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200332
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700333 // MediaChannel related members that should be accessed from the worker
334 // thread.
Steve Anton8699a322017-11-06 15:53:33 -0800335 std::unique_ptr<MediaChannel> media_channel_;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700336 // Currently the |enabled_| flag is accessed from the signaling thread as
337 // well, but it can be changed only when signaling thread does a synchronous
338 // call to the worker thread, so it should be safe.
deadbeef23d947d2016-08-22 16:00:30 -0700339 bool enabled_ = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200340 std::vector<StreamParams> local_streams_;
341 std::vector<StreamParams> remote_streams_;
Steve Anton4e70a722017-11-28 14:57:10 -0800342 webrtc::RtpTransceiverDirection local_content_direction_ =
343 webrtc::RtpTransceiverDirection::kInactive;
344 webrtc::RtpTransceiverDirection remote_content_direction_ =
345 webrtc::RtpTransceiverDirection::kInactive;
Zhi Huangc99b6c72017-11-10 16:44:46 -0800346
Zhi Huang365381f2018-04-13 16:44:34 -0700347 webrtc::RtpDemuxerCriteria demuxer_criteria_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348};
349
350// VoiceChannel is a specialization that adds support for early media, DTMF,
351// and input/output level monitoring.
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800352class VoiceChannel : public BaseChannel,
353 public webrtc::AudioPacketReceivedObserver {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200355 VoiceChannel(rtc::Thread* worker_thread,
356 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800357 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700358 MediaEngineInterface* media_engine,
Steve Anton8699a322017-11-06 15:53:33 -0800359 std::unique_ptr<VoiceMediaChannel> channel,
deadbeefcbecd352015-09-23 11:50:27 -0700360 const std::string& content_name,
Zhi Huange830e682018-03-30 10:48:35 -0700361 bool srtp_required,
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700362 webrtc::CryptoOptions crypto_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 ~VoiceChannel();
solenberg1dd98f32015-09-10 01:57:14 -0700364
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200366 VoiceMediaChannel* media_channel() const override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
368 }
369
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800370 cricket::MediaType media_type() const override {
371 return cricket::MEDIA_TYPE_AUDIO;
372 }
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800373 void Init_w(webrtc::RtpTransportInternal* rtp_transport,
374 webrtc::MediaTransportInterface* media_transport) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 private:
377 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700378 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200379 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800380 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200381 std::string* error_desc) override;
382 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800383 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200384 std::string* error_desc) override;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700385
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800386 void OnFirstAudioPacketReceived(int64_t channel_id) override;
387
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700388 // Last AudioSendParameters sent down to the media_channel() via
389 // SetSendParameters.
390 AudioSendParameters last_send_params_;
391 // Last AudioRecvParameters sent down to the media_channel() via
392 // SetRecvParameters.
393 AudioRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394};
395
396// VideoChannel is a specialization for video.
397class VideoChannel : public BaseChannel {
398 public:
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200399 VideoChannel(rtc::Thread* worker_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800400 rtc::Thread* network_thread,
401 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 15:53:33 -0800402 std::unique_ptr<VideoMediaChannel> media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700403 const std::string& content_name,
Zhi Huange830e682018-03-30 10:48:35 -0700404 bool srtp_required,
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700405 webrtc::CryptoOptions crypto_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 ~VideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200408 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200409 VideoMediaChannel* media_channel() const override {
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200410 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
411 }
412
stefanf79ade12017-06-02 06:44:03 -0700413 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800415 cricket::MediaType media_type() const override {
416 return cricket::MEDIA_TYPE_VIDEO;
417 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700421 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200422 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800423 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200424 std::string* error_desc) override;
425 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800426 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200427 std::string* error_desc) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700429 // Last VideoSendParameters sent down to the media_channel() via
430 // SetSendParameters.
431 VideoSendParameters last_send_params_;
432 // Last VideoRecvParameters sent down to the media_channel() via
433 // SetRecvParameters.
434 VideoRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435};
436
deadbeef953c2ce2017-01-09 14:53:41 -0800437// RtpDataChannel is a specialization for data.
438class RtpDataChannel : public BaseChannel {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 public:
deadbeef953c2ce2017-01-09 14:53:41 -0800440 RtpDataChannel(rtc::Thread* worker_thread,
441 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800442 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 15:53:33 -0800443 std::unique_ptr<DataMediaChannel> channel,
deadbeef953c2ce2017-01-09 14:53:41 -0800444 const std::string& content_name,
Zhi Huange830e682018-03-30 10:48:35 -0700445 bool srtp_required,
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700446 webrtc::CryptoOptions crypto_options);
deadbeef953c2ce2017-01-09 14:53:41 -0800447 ~RtpDataChannel();
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800448 // TODO(zhihuang): Remove this once the RtpTransport can be shared between
449 // BaseChannels.
Steve Anton8699a322017-11-06 15:53:33 -0800450 void Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800451 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800452 rtc::PacketTransportInternal* rtp_packet_transport,
453 rtc::PacketTransportInternal* rtcp_packet_transport);
Piotr (Peter) Slatala309aafe2019-01-15 14:24:34 -0800454 void Init_w(
455 webrtc::RtpTransportInternal* rtp_transport,
456 webrtc::MediaTransportInterface* media_transport = nullptr) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000458 virtual bool SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700459 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000460 SendDataResult* result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000462 // Should be called on the signaling thread only.
Yves Gerey665174f2018-06-19 15:03:05 +0200463 bool ready_to_send_data() const { return ready_to_send_data_; }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000464
deadbeef953c2ce2017-01-09 14:53:41 -0800465 sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
466 SignalDataReceived;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 // Signal for notifying when the channel becomes ready to send data.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000468 // That occurs when the channel is enabled, the transport is writable,
469 // both local and remote descriptions are set, and the channel is unblocked.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 sigslot::signal1<bool> SignalReadyToSendData;
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800471 cricket::MediaType media_type() const override {
472 return cricket::MEDIA_TYPE_DATA;
473 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000475 protected:
476 // downcasts a MediaChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200477 DataMediaChannel* media_channel() const override {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000478 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
479 }
480
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000482 struct SendDataMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 SendDataMessageData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700484 const rtc::CopyOnWriteBuffer* payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 SendDataResult* result)
Yves Gerey665174f2018-06-19 15:03:05 +0200486 : params(params), payload(payload), result(result), succeeded(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487
488 const SendDataParams& params;
jbaucheec21bd2016-03-20 06:15:43 -0700489 const rtc::CopyOnWriteBuffer* payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 SendDataResult* result;
491 bool succeeded;
492 };
493
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000494 struct DataReceivedMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 // We copy the data because the data will become invalid after we
496 // handle DataMediaChannel::SignalDataReceived but before we fire
497 // SignalDataReceived.
Yves Gerey665174f2018-06-19 15:03:05 +0200498 DataReceivedMessageData(const ReceiveDataParams& params,
499 const char* data,
500 size_t len)
501 : params(params), payload(data, len) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 const ReceiveDataParams params;
jbaucheec21bd2016-03-20 06:15:43 -0700503 const rtc::CopyOnWriteBuffer payload;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 };
505
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000506 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000507
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 // overrides from BaseChannel
deadbeef953c2ce2017-01-09 14:53:41 -0800509 // Checks that data channel type is RTP.
510 bool CheckDataChannelTypeFromContent(const DataContentDescription* content,
511 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200512 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800513 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200514 std::string* error_desc) override;
515 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 10:34:51 -0800516 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200517 std::string* error_desc) override;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700518 void UpdateMediaSendRecvState_w() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200520 void OnMessage(rtc::Message* pmsg) override;
Yves Gerey665174f2018-06-19 15:03:05 +0200521 void OnDataReceived(const ReceiveDataParams& params,
522 const char* data,
523 size_t len);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000524 void OnDataChannelReadyToSend(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525
deadbeef953c2ce2017-01-09 14:53:41 -0800526 bool ready_to_send_data_ = false;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700527
528 // Last DataSendParameters sent down to the media_channel() via
529 // SetSendParameters.
530 DataSendParameters last_send_params_;
531 // Last DataRecvParameters sent down to the media_channel() via
532 // SetRecvParameters.
533 DataRecvParameters last_recv_params_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534};
535
536} // namespace cricket
537
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200538#endif // PC_CHANNEL_H_