Thanks to visit codestin.com
Credit goes to webrtc.googlesource.com

blob: 3d2d67f87633f49368684bf91d0751dd77a2c990 [file] [log] [blame]
[email protected]28e20752013-07-10 00:45:361/*
kjellander65c7f672016-02-12 08:05:012 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
[email protected]28e20752013-07-10 00:45:363 *
kjellander65c7f672016-02-12 08:05:014 * 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.
[email protected]28e20752013-07-10 00:45:369 */
10
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef PC_CHANNEL_H_
12#define PC_CHANNEL_H_
[email protected]28e20752013-07-10 00:45:3613
deadbeefcbecd352015-09-23 18:50:2714#include <map>
kwiberg31022942016-03-11 22:18:2115#include <memory>
deadbeefcbecd352015-09-23 18:50:2716#include <set>
kjellandera96e2d72016-02-05 07:52:2817#include <string>
deadbeefcbecd352015-09-23 18:50:2718#include <utility>
kjellandera96e2d72016-02-05 07:52:2819#include <vector>
[email protected]28e20752013-07-10 00:45:3620
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "api/call/audio_sink.h"
Danil Chapovalov89313452019-11-29 11:56:4322#include "api/function_view.h"
Steve Anton3828c062017-12-06 18:34:5123#include "api/jsep.h"
Steve Anton10542f22019-01-11 17:11:0024#include "api/rtp_receiver_interface.h"
Niels Möllerc6ce9c52018-05-11 09:15:3025#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 12:09:3126#include "api/video/video_source_interface.h"
Zhi Huang365381f2018-04-13 23:44:3427#include "call/rtp_packet_sink_interface.h"
Steve Anton10542f22019-01-11 17:11:0028#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"
Steve Anton10542f22019-01-11 17:11:0039#include "rtc_base/async_udp_socket.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3140#include "rtc_base/network.h"
Taylor Brandstetterc03a1872020-09-02 20:25:3141#include "rtc_base/synchronization/sequence_checker.h"
Danil Chapovalovfd9500e2021-01-15 16:29:5342#include "rtc_base/task_utils/pending_task_safety_flag.h"
Artem Titove41c4332018-07-25 13:04:2843#include "rtc_base/third_party/sigslot/sigslot.h"
Taylor Brandstetterc03a1872020-09-02 20:25:3144#include "rtc_base/thread_annotations.h"
Amit Hilbuchbcd39d42019-01-26 01:13:5645#include "rtc_base/unique_id_generator.h"
Tommif888bb52015-12-12 00:37:0146
47namespace webrtc {
48class AudioSinkInterface;
49} // namespace webrtc
[email protected]28e20752013-07-10 00:45:3650
51namespace cricket {
52
53struct CryptoParams;
[email protected]28e20752013-07-10 00:45:3654
deadbeef062ce9f2016-08-27 04:42:1555// 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 17:55:2759// 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-27 04:42:1564// methods with _w suffix on worker thread
Danil Chapovalov33b01f22016-05-11 17:55:2765// and methods with _s suffix on signaling thread.
66// Network and worker threads may be the same thread.
[email protected]78187522013-10-07 23:32:0267//
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
Amit Hilbuchdd9390c2018-11-14 00:26:0573class BaseChannel : public ChannelInterface,
Tomas Gunnarssonabdb4702020-09-05 16:43:3674 public rtc::MessageHandlerAutoCleanup,
Zhi Huang365381f2018-04-13 23:44:3475 public sigslot::has_slots<>,
76 public MediaChannel::NetworkInterface,
Bjorn A Mellem7a9a0922019-11-26 17:19:4077 public webrtc::RtpPacketSinkInterface {
[email protected]28e20752013-07-10 00:45:3678 public:
deadbeef7af91dd2016-12-13 19:29:1179 // 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).
Amit Hilbuchbcd39d42019-01-26 01:13:5681 // The BaseChannel does not own the UniqueRandomIdGenerator so it is the
82 // responsibility of the user to ensure it outlives this object.
Zhi Huange830e682018-03-30 17:48:3583 // TODO(zhihuang:) Create a BaseChannel::Config struct for the parameter lists
84 // which will make it easier to change the constructor.
Danil Chapovalov33b01f22016-05-11 17:55:2785 BaseChannel(rtc::Thread* worker_thread,
86 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-13 03:37:4887 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 23:53:3388 std::unique_ptr<MediaChannel> media_channel,
deadbeefcbecd352015-09-23 18:50:2789 const std::string& content_name,
Zhi Huange830e682018-03-30 17:48:3590 bool srtp_required,
Amit Hilbuchbcd39d42019-01-26 01:13:5691 webrtc::CryptoOptions crypto_options,
92 rtc::UniqueRandomIdGenerator* ssrc_generator);
[email protected]28e20752013-07-10 00:45:3693 virtual ~BaseChannel();
Niels Möller2a707032020-06-16 14:39:1394 virtual void Init_w(webrtc::RtpTransportInternal* rtp_transport);
Zhi Huang2dfc42d2017-12-04 21:38:4895
Danil Chapovalov33b01f22016-05-11 17:55:2796 // Deinit may be called multiple times and is simply ignored if it's already
[email protected]78187522013-10-07 23:32:0297 // done.
98 void Deinit();
[email protected]28e20752013-07-10 00:45:3699
[email protected]d4e598d2014-07-29 17:36:52100 rtc::Thread* worker_thread() const { return worker_thread_; }
Danil Chapovalov33b01f22016-05-11 17:55:27101 rtc::Thread* network_thread() const { return network_thread_; }
Amit Hilbuchdd9390c2018-11-14 00:26:05102 const std::string& content_name() const override { return content_name_; }
deadbeeff5346592017-01-25 05:51:21103 // TODO(deadbeef): This is redundant; remove this.
Amit Hilbuchdd9390c2018-11-14 00:26:05104 const std::string& transport_name() const override { return transport_name_; }
105 bool enabled() const override { return enabled_; }
[email protected]28e20752013-07-10 00:45:36106
Zhi Huangcf990f52017-09-22 19:12:30107 // This function returns true if using SRTP (DTLS-based keying or SDES).
Zhi Huange830e682018-03-30 17:48:35108 bool srtp_active() const {
Harald Alvestrand476859d2020-12-07 11:16:45109 RTC_DCHECK_RUN_ON(network_thread());
110 return rtp_transport_ && rtp_transport_->IsSrtpActive();
111 }
112
113 // Version of the above that can be called from any thread.
114 bool SrtpActiveForTesting() const {
Harald Alvestrand27883a22020-11-26 07:24:32115 if (!network_thread_->IsCurrent()) {
116 return network_thread_->Invoke<bool>(RTC_FROM_HERE,
117 [this] { return srtp_active(); });
118 }
119 RTC_DCHECK_RUN_ON(network_thread());
Harald Alvestrand476859d2020-12-07 11:16:45120 return srtp_active();
Zhi Huange830e682018-03-30 17:48:35121 }
Zhi Huang2dfc42d2017-12-04 21:38:48122 // Set an RTP level transport which could be an RtpTransport without
123 // encryption, an SrtpTransport for SDES or a DtlsSrtpTransport for DTLS-SRTP.
124 // This can be called from any thread and it hops to the network thread
125 // internally. It would replace the |SetTransports| and its variants.
Amit Hilbuchdd9390c2018-11-14 00:26:05126 bool SetRtpTransport(webrtc::RtpTransportInternal* rtp_transport) override;
Zhi Huang2dfc42d2017-12-04 21:38:48127
Harald Alvestrand27883a22020-11-26 07:24:32128 webrtc::RtpTransportInternal* rtp_transport() const {
Harald Alvestrand476859d2020-12-07 11:16:45129 RTC_DCHECK_RUN_ON(network_thread());
130 return rtp_transport_;
131 }
132
133 // Version of the above that can be called from any thread.
134 webrtc::RtpTransportInternal* RtpTransportForTesting() const {
Harald Alvestrand27883a22020-11-26 07:24:32135 if (!network_thread_->IsCurrent()) {
136 return network_thread_->Invoke<webrtc::RtpTransportInternal*>(
137 RTC_FROM_HERE, [this] { return rtp_transport(); });
138 }
139 RTC_DCHECK_RUN_ON(network_thread());
Harald Alvestrand476859d2020-12-07 11:16:45140 return rtp_transport();
Harald Alvestrand27883a22020-11-26 07:24:32141 }
Bjorn A Mellem3a1b9272019-05-24 23:13:08142
Taylor Brandstetterc1ad1ff2020-12-10 23:31:14143 // Channel control. Must call UpdateRtpTransport afterwards to apply any
144 // changes to the RtpTransport on the network thread.
[email protected]28e20752013-07-10 00:45:36145 bool SetLocalContent(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 18:34:51146 webrtc::SdpType type,
Amit Hilbuchdd9390c2018-11-14 00:26:05147 std::string* error_desc) override;
[email protected]28e20752013-07-10 00:45:36148 bool SetRemoteContent(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 18:34:51149 webrtc::SdpType type,
Amit Hilbuchdd9390c2018-11-14 00:26:05150 std::string* error_desc) override;
Taylor Brandstetterc03a1872020-09-02 20:25:31151 // Controls whether this channel will receive packets on the basis of
152 // matching payload type alone. This is needed for legacy endpoints that
153 // don't signal SSRCs or use MID/RID, but doesn't make sense if there is
154 // more than channel of specific media type, As that creates an ambiguity.
155 //
156 // This method will also remove any existing streams that were bound to this
157 // channel on the basis of payload type, since one of these streams might
158 // actually belong to a new channel. See: crbug.com/webrtc/11477
Taylor Brandstetterc1ad1ff2020-12-10 23:31:14159 //
160 // As with SetLocalContent/SetRemoteContent, must call UpdateRtpTransport
161 // afterwards to apply changes to the RtpTransport on the network thread.
162 void SetPayloadTypeDemuxingEnabled(bool enabled) override;
163 bool UpdateRtpTransport(std::string* error_desc) override;
[email protected]28e20752013-07-10 00:45:36164
Amit Hilbuchdd9390c2018-11-14 00:26:05165 bool Enable(bool enable) override;
[email protected]28e20752013-07-10 00:45:36166
Amit Hilbuchbcd39d42019-01-26 01:13:56167 const std::vector<StreamParams>& local_streams() const override {
[email protected]28e20752013-07-10 00:45:36168 return local_streams_;
169 }
Amit Hilbuchbcd39d42019-01-26 01:13:56170 const std::vector<StreamParams>& remote_streams() const override {
[email protected]28e20752013-07-10 00:45:36171 return remote_streams_;
172 }
173
[email protected]6bfd6192014-05-15 16:15:59174 // Used for latency measurements.
Tomas Gunnarssonb2995a12020-09-28 12:05:35175 sigslot::signal1<ChannelInterface*>& SignalFirstPacketReceived() override;
[email protected]28e20752013-07-10 00:45:36176
zhihuangb2cdd932017-01-20 00:54:25177 // Forward SignalSentPacket to worker thread.
Tomas Gunnarssonb2995a12020-09-28 12:05:35178 sigslot::signal1<const rtc::SentPacket&>& SignalSentPacket();
zhihuangf5b251b2017-01-13 03:37:48179
zstein56162b92017-04-24 23:54:35180 // From RtpTransport - public for testing only
181 void OnTransportReadyToSend(bool ready);
[email protected]28e20752013-07-10 00:45:36182
[email protected]4f852882015-03-12 20:09:44183 // Only public for unit tests. Otherwise, consider protected.
Yves Gerey665174f2018-06-19 13:03:05184 int SetOption(SocketType type, rtc::Socket::Option o, int val) override;
Harald Alvestrand27883a22020-11-26 07:24:32185 int SetOption_n(SocketType type, rtc::Socket::Option o, int val)
186 RTC_RUN_ON(network_thread());
[email protected]4f852882015-03-12 20:09:44187
Zhi Huang365381f2018-04-13 23:44:34188 // RtpPacketSinkInterface overrides.
189 void OnRtpPacket(const webrtc::RtpPacketReceived& packet) override;
zstein3dcf0e92017-06-01 20:22:42190
Steve Anton593e3252017-12-15 19:44:48191 // Used by the RTCStatsCollector tests to set the transport name without
192 // creating RtpTransports.
193 void set_transport_name_for_testing(const std::string& transport_name) {
194 transport_name_ = transport_name;
195 }
196
Harald Alvestrand27883a22020-11-26 07:24:32197 MediaChannel* media_channel() const override {
Harald Alvestrand27883a22020-11-26 07:24:32198 return media_channel_.get();
199 }
Taylor Brandstetterbad33bf2016-08-25 20:31:14200
Amit Hilbuchdd9390c2018-11-14 00:26:05201 protected:
Harald Alvestrand27883a22020-11-26 07:24:32202 bool was_ever_writable() const {
Taylor Brandstetterc1ad1ff2020-12-10 23:31:14203 RTC_DCHECK_RUN_ON(worker_thread());
Harald Alvestrand27883a22020-11-26 07:24:32204 return was_ever_writable_;
205 }
Steve Anton4e70a722017-11-28 22:57:10206 void set_local_content_direction(webrtc::RtpTransceiverDirection direction) {
Harald Alvestrand27883a22020-11-26 07:24:32207 RTC_DCHECK_RUN_ON(worker_thread());
[email protected]28e20752013-07-10 00:45:36208 local_content_direction_ = direction;
209 }
Steve Anton4e70a722017-11-28 22:57:10210 void set_remote_content_direction(webrtc::RtpTransceiverDirection direction) {
Harald Alvestrand27883a22020-11-26 07:24:32211 RTC_DCHECK_RUN_ON(worker_thread());
[email protected]28e20752013-07-10 00:45:36212 remote_content_direction_ = direction;
213 }
Taylor Brandstetterbad33bf2016-08-25 20:31:14214 // These methods verify that:
215 // * The required content description directions have been set.
216 // * The channel is enabled.
217 // * And for sending:
218 // - The SRTP filter is active if it's needed.
219 // - The transport has been writable before, meaning it should be at least
220 // possible to succeed in sending a packet.
221 //
222 // When any of these properties change, UpdateMediaSendRecvState_w should be
223 // called.
Harald Alvestrand27883a22020-11-26 07:24:32224 bool IsReadyToReceiveMedia_w() const RTC_RUN_ON(worker_thread());
225 bool IsReadyToSendMedia_w() const RTC_RUN_ON(worker_thread());
Markus Handell5932fe12020-12-17 21:19:40226 rtc::Thread* signaling_thread() const { return signaling_thread_; }
[email protected]28e20752013-07-10 00:45:36227
Harald Alvestrand27883a22020-11-26 07:24:32228 void FlushRtcpMessages_n() RTC_RUN_ON(network_thread());
[email protected]28e20752013-07-10 00:45:36229
230 // NetworkInterface implementation, called by MediaEngine
jbaucheec21bd2016-03-20 13:15:43231 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
232 const rtc::PacketOptions& options) override;
233 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
234 const rtc::PacketOptions& options) override;
[email protected]28e20752013-07-10 00:45:36235
Zhi Huangcd3fc5d2017-11-29 18:41:57236 // From RtpTransportInternal
237 void OnWritableState(bool writable);
Guo-wei Shieh1218d7a2015-12-05 17:59:56238
Danil Chapovalov66cadcc2018-06-19 14:47:43239 void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route);
Honghai Zhangcc411c02016-03-30 00:27:21240
deadbeef5bd5ca32017-02-10 19:31:50241 bool PacketIsRtcp(const rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 17:50:32242 const char* data,
[email protected]28e20752013-07-10 00:45:36243 size_t len);
stefanc1aeaf02015-10-15 14:26:07244 bool SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 13:15:43245 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 14:26:07246 const rtc::PacketOptions& options);
Danil Chapovalov33b01f22016-05-11 17:55:27247
Harald Alvestrand27883a22020-11-26 07:24:32248 void EnableMedia_w() RTC_RUN_ON(worker_thread());
249 void DisableMedia_w() RTC_RUN_ON(worker_thread());
Taylor Brandstetterbad33bf2016-08-25 20:31:14250
251 // Performs actions if the RTP/RTCP writable state changed. This should
252 // be called whenever a channel's writable state changes or when RTCP muxing
253 // becomes active/inactive.
Harald Alvestrand27883a22020-11-26 07:24:32254 void UpdateWritableState_n() RTC_RUN_ON(network_thread());
255 void ChannelWritable_n() RTC_RUN_ON(network_thread());
256 void ChannelNotWritable_n() RTC_RUN_ON(network_thread());
Taylor Brandstetterbad33bf2016-08-25 20:31:14257
Harald Alvestrand27883a22020-11-26 07:24:32258 bool AddRecvStream_w(const StreamParams& sp) RTC_RUN_ON(worker_thread());
259 bool RemoveRecvStream_w(uint32_t ssrc) RTC_RUN_ON(worker_thread());
260 void ResetUnsignaledRecvStream_w() RTC_RUN_ON(worker_thread());
Taylor Brandstetterc1ad1ff2020-12-10 23:31:14261 void SetPayloadTypeDemuxingEnabled_w(bool enabled)
Harald Alvestrand27883a22020-11-26 07:24:32262 RTC_RUN_ON(worker_thread());
263 bool AddSendStream_w(const StreamParams& sp) RTC_RUN_ON(worker_thread());
264 bool RemoveSendStream_w(uint32_t ssrc) RTC_RUN_ON(worker_thread());
[email protected]28e20752013-07-10 00:45:36265
Taylor Brandstetterbad33bf2016-08-25 20:31:14266 // Should be called whenever the conditions for
267 // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied).
268 // Updates the send/recv state of the media channel.
Taylor Brandstetterbad33bf2016-08-25 20:31:14269 virtual void UpdateMediaSendRecvState_w() = 0;
[email protected]28e20752013-07-10 00:45:36270
[email protected]28e20752013-07-10 00:45:36271 bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
Steve Anton3828c062017-12-06 18:34:51272 webrtc::SdpType type,
Harald Alvestrand27883a22020-11-26 07:24:32273 std::string* error_desc)
274 RTC_RUN_ON(worker_thread());
[email protected]28e20752013-07-10 00:45:36275 bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
Steve Anton3828c062017-12-06 18:34:51276 webrtc::SdpType type,
Harald Alvestrand27883a22020-11-26 07:24:32277 std::string* error_desc)
278 RTC_RUN_ON(worker_thread());
[email protected]28e20752013-07-10 00:45:36279 virtual bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 18:34:51280 webrtc::SdpType type,
[email protected]4b26e2e2014-01-15 23:15:54281 std::string* error_desc) = 0;
[email protected]28e20752013-07-10 00:45:36282 virtual bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 18:34:51283 webrtc::SdpType type,
[email protected]4b26e2e2014-01-15 23:15:54284 std::string* error_desc) = 0;
jbauch5869f502017-06-29 19:31:36285 // Return a list of RTP header extensions with the non-encrypted extensions
286 // removed depending on the current crypto_options_ and only if both the
287 // non-encrypted and encrypted extension is present for the same URI.
288 RtpHeaderExtensions GetFilteredRtpHeaderExtensions(
289 const RtpHeaderExtensions& extensions);
Taylor Brandstetterc1ad1ff2020-12-10 23:31:14290 // Set a list of RTP extensions we should prepare to receive on the next
291 // UpdateRtpTransport call.
292 void SetReceiveExtensions(const RtpHeaderExtensions& extensions);
[email protected]28e20752013-07-10 00:45:36293
[email protected]28e20752013-07-10 00:45:36294 // From MessageHandler
rlesterec9d1872015-10-27 21:22:16295 void OnMessage(rtc::Message* pmsg) override;
[email protected]28e20752013-07-10 00:45:36296
stefanf79ade12017-06-02 13:44:03297 // Helper function template for invoking methods on the worker thread.
Danil Chapovalov89313452019-11-29 11:56:43298 template <class T>
299 T InvokeOnWorker(const rtc::Location& posted_from,
300 rtc::FunctionView<T()> functor) {
stefanf79ade12017-06-02 13:44:03301 return worker_thread_->Invoke<T>(posted_from, functor);
[email protected]9cf037b2014-02-07 19:03:26302 }
303
Taylor Brandstetterc03a1872020-09-02 20:25:31304 // Add |payload_type| to |demuxer_criteria_| if payload type demuxing is
305 // enabled.
306 void MaybeAddHandledPayloadType(int payload_type) RTC_RUN_ON(worker_thread());
zstein3dcf0e92017-06-01 20:22:42307
Taylor Brandstetterc03a1872020-09-02 20:25:31308 void ClearHandledPayloadTypes() RTC_RUN_ON(worker_thread());
Yura Yaroshevichc3252462020-05-18 10:26:14309 // Return description of media channel to facilitate logging
310 std::string ToString() const;
311
Markus Handell5932fe12020-12-17 21:19:40312 void SetNegotiatedHeaderExtensions_w(const RtpHeaderExtensions& extensions);
313
314 // ChannelInterface overrides
315 RtpHeaderExtensions GetNegotiatedRtpHeaderExtensions() const override;
316
[email protected]28e20752013-07-10 00:45:36317 private:
Zhi Huang365381f2018-04-13 23:44:34318 bool ConnectToRtpTransport();
Zhi Huangcd3fc5d2017-11-29 18:41:57319 void DisconnectFromRtpTransport();
Harald Alvestrand27883a22020-11-26 07:24:32320 void SignalSentPacket_n(const rtc::SentPacket& sent_packet)
321 RTC_RUN_ON(network_thread());
Piotr (Peter) Slatala179a3922018-11-16 17:57:58322
Danil Chapovalov33b01f22016-05-11 17:55:27323 rtc::Thread* const worker_thread_;
324 rtc::Thread* const network_thread_;
zhihuangf5b251b2017-01-13 03:37:48325 rtc::Thread* const signaling_thread_;
Danil Chapovalovfd9500e2021-01-15 16:29:53326 rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> alive_;
Tomas Gunnarssonb2995a12020-09-28 12:05:35327 sigslot::signal1<ChannelInterface*> SignalFirstPacketReceived_
328 RTC_GUARDED_BY(signaling_thread_);
329 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket_
330 RTC_GUARDED_BY(worker_thread_);
[email protected]28e20752013-07-10 00:45:36331
[email protected]990a00c2015-03-13 18:20:33332 const std::string content_name_;
Danil Chapovalov33b01f22016-05-11 17:55:27333
Tomas Gunnarsson33c0ab42021-01-18 09:49:05334 bool has_received_packet_ = false;
335
deadbeeff5346592017-01-25 05:51:21336 // Won't be set when using raw packet transports. SDP-specific thing.
Harald Alvestrand27883a22020-11-26 07:24:32337 // TODO(bugs.webrtc.org/12230): Written on network thread, read on
338 // worker thread (at least).
deadbeefcbecd352015-09-23 18:50:27339 std::string transport_name_;
zhihuangb2cdd932017-01-20 00:54:25340
Harald Alvestrand27883a22020-11-26 07:24:32341 webrtc::RtpTransportInternal* rtp_transport_
342 RTC_GUARDED_BY(network_thread()) = nullptr;
Zhi Huangcd3fc5d2017-11-29 18:41:57343
Harald Alvestrand27883a22020-11-26 07:24:32344 std::vector<std::pair<rtc::Socket::Option, int> > socket_options_
345 RTC_GUARDED_BY(network_thread());
346 std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_
347 RTC_GUARDED_BY(network_thread());
Taylor Brandstetterc1ad1ff2020-12-10 23:31:14348 bool writable_ RTC_GUARDED_BY(network_thread()) = false;
349 bool was_ever_writable_n_ RTC_GUARDED_BY(network_thread()) = false;
350 bool was_ever_writable_ RTC_GUARDED_BY(worker_thread()) = false;
deadbeef7af91dd2016-12-13 19:29:11351 const bool srtp_required_ = true;
Harald Alvestrand27883a22020-11-26 07:24:32352 const webrtc::CryptoOptions crypto_options_;
Danil Chapovalov33b01f22016-05-11 17:55:27353
Taylor Brandstetterbad33bf2016-08-25 20:31:14354 // MediaChannel related members that should be accessed from the worker
355 // thread.
Harald Alvestrand0f0bcb32020-12-07 11:45:55356 const std::unique_ptr<MediaChannel> media_channel_;
Taylor Brandstetterbad33bf2016-08-25 20:31:14357 // Currently the |enabled_| flag is accessed from the signaling thread as
358 // well, but it can be changed only when signaling thread does a synchronous
359 // call to the worker thread, so it should be safe.
deadbeef23d947d2016-08-22 23:00:30360 bool enabled_ = false;
Taylor Brandstetterc03a1872020-09-02 20:25:31361 bool payload_type_demuxing_enabled_ RTC_GUARDED_BY(worker_thread()) = true;
Harald Alvestrand27883a22020-11-26 07:24:32362 std::vector<StreamParams> local_streams_ RTC_GUARDED_BY(worker_thread());
363 std::vector<StreamParams> remote_streams_ RTC_GUARDED_BY(worker_thread());
364 // TODO(bugs.webrtc.org/12230): local_content_direction and
365 // remote_content_direction are set on the worker thread, but accessed on the
366 // network thread.
Steve Anton4e70a722017-11-28 22:57:10367 webrtc::RtpTransceiverDirection local_content_direction_ =
368 webrtc::RtpTransceiverDirection::kInactive;
369 webrtc::RtpTransceiverDirection remote_content_direction_ =
370 webrtc::RtpTransceiverDirection::kInactive;
Zhi Huangc99b6c72017-11-11 00:44:46371
Taylor Brandstetterd3ef4992020-10-16 01:22:57372 // Cached list of payload types, used if payload type demuxing is re-enabled.
373 std::set<uint8_t> payload_types_ RTC_GUARDED_BY(worker_thread());
Taylor Brandstetterc1ad1ff2020-12-10 23:31:14374 // TODO(bugs.webrtc.org/12239): These two variables are modified on the worker
375 // thread, accessed on the network thread in UpdateRtpTransport.
Zhi Huang365381f2018-04-13 23:44:34376 webrtc::RtpDemuxerCriteria demuxer_criteria_;
Taylor Brandstetterc1ad1ff2020-12-10 23:31:14377 RtpHeaderExtensions receive_rtp_header_extensions_;
Amit Hilbuchbcd39d42019-01-26 01:13:56378 // This generator is used to generate SSRCs for local streams.
379 // This is needed in cases where SSRCs are not negotiated or set explicitly
380 // like in Simulcast.
381 // This object is not owned by the channel so it must outlive it.
382 rtc::UniqueRandomIdGenerator* const ssrc_generator_;
Markus Handell5932fe12020-12-17 21:19:40383
Markus Handell19217082021-01-14 13:00:40384 // |negotiated_header_extensions_| is read on the signaling thread, but
385 // written on the worker thread while being sync-invoked from the signal
386 // thread in SdpOfferAnswerHandler::PushdownMediaDescription(). Hence the lock
387 // isn't strictly needed, but it's anyway placed here for future safeness.
388 mutable webrtc::Mutex negotiated_header_extensions_lock_;
Markus Handell5932fe12020-12-17 21:19:40389 RtpHeaderExtensions negotiated_header_extensions_
Markus Handell19217082021-01-14 13:00:40390 RTC_GUARDED_BY(negotiated_header_extensions_lock_);
[email protected]28e20752013-07-10 00:45:36391};
392
393// VoiceChannel is a specialization that adds support for early media, DTMF,
394// and input/output level monitoring.
Bjorn A Mellem7a9a0922019-11-26 17:19:40395class VoiceChannel : public BaseChannel {
[email protected]28e20752013-07-10 00:45:36396 public:
Danil Chapovalov33b01f22016-05-11 17:55:27397 VoiceChannel(rtc::Thread* worker_thread,
398 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-13 03:37:48399 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 23:53:33400 std::unique_ptr<VoiceMediaChannel> channel,
deadbeefcbecd352015-09-23 18:50:27401 const std::string& content_name,
Zhi Huange830e682018-03-30 17:48:35402 bool srtp_required,
Amit Hilbuchbcd39d42019-01-26 01:13:56403 webrtc::CryptoOptions crypto_options,
404 rtc::UniqueRandomIdGenerator* ssrc_generator);
[email protected]28e20752013-07-10 00:45:36405 ~VoiceChannel();
solenberg1dd98f32015-09-10 08:57:14406
[email protected]28e20752013-07-10 00:45:36407 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 17:55:27408 VoiceMediaChannel* media_channel() const override {
[email protected]28e20752013-07-10 00:45:36409 return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
410 }
411
Amit Hilbuchdd9390c2018-11-14 00:26:05412 cricket::MediaType media_type() const override {
413 return cricket::MEDIA_TYPE_AUDIO;
414 }
Niels Möller2a707032020-06-16 14:39:13415 void Init_w(webrtc::RtpTransportInternal* rtp_transport) override;
[email protected]28e20752013-07-10 00:45:36416
[email protected]28e20752013-07-10 00:45:36417 private:
418 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 20:31:14419 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 17:55:27420 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 18:34:51421 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 17:55:27422 std::string* error_desc) override;
423 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 18:34:51424 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 17:55:27425 std::string* error_desc) override;
Peter Thatcherc2ee2c82015-08-07 23:05:34426
427 // Last AudioSendParameters sent down to the media_channel() via
428 // SetSendParameters.
429 AudioSendParameters last_send_params_;
430 // Last AudioRecvParameters sent down to the media_channel() via
431 // SetRecvParameters.
432 AudioRecvParameters last_recv_params_;
[email protected]28e20752013-07-10 00:45:36433};
434
435// VideoChannel is a specialization for video.
436class VideoChannel : public BaseChannel {
437 public:
Danil Chapovalov33b01f22016-05-11 17:55:27438 VideoChannel(rtc::Thread* worker_thread,
zhihuangf5b251b2017-01-13 03:37:48439 rtc::Thread* network_thread,
440 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 23:53:33441 std::unique_ptr<VideoMediaChannel> media_channel,
deadbeefcbecd352015-09-23 18:50:27442 const std::string& content_name,
Zhi Huange830e682018-03-30 17:48:35443 bool srtp_required,
Amit Hilbuchbcd39d42019-01-26 01:13:56444 webrtc::CryptoOptions crypto_options,
445 rtc::UniqueRandomIdGenerator* ssrc_generator);
[email protected]28e20752013-07-10 00:45:36446 ~VideoChannel();
[email protected]28e20752013-07-10 00:45:36447
Fredrik Solenberg4b60c732015-05-07 12:07:48448 // downcasts a MediaChannel
Danil Chapovalov33b01f22016-05-11 17:55:27449 VideoMediaChannel* media_channel() const override {
Fredrik Solenberg4b60c732015-05-07 12:07:48450 return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
451 }
452
stefanf79ade12017-06-02 13:44:03453 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
[email protected]28e20752013-07-10 00:45:36454
Amit Hilbuchdd9390c2018-11-14 00:26:05455 cricket::MediaType media_type() const override {
456 return cricket::MEDIA_TYPE_VIDEO;
457 }
[email protected]28e20752013-07-10 00:45:36458
[email protected]28e20752013-07-10 00:45:36459 private:
[email protected]28e20752013-07-10 00:45:36460 // overrides from BaseChannel
Taylor Brandstetterbad33bf2016-08-25 20:31:14461 void UpdateMediaSendRecvState_w() override;
Danil Chapovalov33b01f22016-05-11 17:55:27462 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 18:34:51463 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 17:55:27464 std::string* error_desc) override;
465 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 18:34:51466 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 17:55:27467 std::string* error_desc) override;
[email protected]28e20752013-07-10 00:45:36468
Peter Thatcherc2ee2c82015-08-07 23:05:34469 // Last VideoSendParameters sent down to the media_channel() via
470 // SetSendParameters.
471 VideoSendParameters last_send_params_;
472 // Last VideoRecvParameters sent down to the media_channel() via
473 // SetRecvParameters.
474 VideoRecvParameters last_recv_params_;
[email protected]28e20752013-07-10 00:45:36475};
476
deadbeef953c2ce2017-01-09 22:53:41477// RtpDataChannel is a specialization for data.
478class RtpDataChannel : public BaseChannel {
[email protected]28e20752013-07-10 00:45:36479 public:
deadbeef953c2ce2017-01-09 22:53:41480 RtpDataChannel(rtc::Thread* worker_thread,
481 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-13 03:37:48482 rtc::Thread* signaling_thread,
Steve Anton8699a322017-11-06 23:53:33483 std::unique_ptr<DataMediaChannel> channel,
deadbeef953c2ce2017-01-09 22:53:41484 const std::string& content_name,
Zhi Huange830e682018-03-30 17:48:35485 bool srtp_required,
Amit Hilbuchbcd39d42019-01-26 01:13:56486 webrtc::CryptoOptions crypto_options,
487 rtc::UniqueRandomIdGenerator* ssrc_generator);
deadbeef953c2ce2017-01-09 22:53:41488 ~RtpDataChannel();
Zhi Huang2dfc42d2017-12-04 21:38:48489 // TODO(zhihuang): Remove this once the RtpTransport can be shared between
490 // BaseChannels.
Steve Anton8699a322017-11-06 23:53:33491 void Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-25 05:51:21492 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 19:31:50493 rtc::PacketTransportInternal* rtp_packet_transport,
494 rtc::PacketTransportInternal* rtcp_packet_transport);
Niels Möller2a707032020-06-16 14:39:13495 void Init_w(webrtc::RtpTransportInternal* rtp_transport) override;
[email protected]28e20752013-07-10 00:45:36496
[email protected]d64719d2013-08-01 00:00:07497 virtual bool SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 13:15:43498 const rtc::CopyOnWriteBuffer& payload,
[email protected]d64719d2013-08-01 00:00:07499 SendDataResult* result);
[email protected]28e20752013-07-10 00:45:36500
[email protected]07a6fbe2013-11-04 18:41:34501 // Should be called on the signaling thread only.
Yves Gerey665174f2018-06-19 13:03:05502 bool ready_to_send_data() const { return ready_to_send_data_; }
[email protected]07a6fbe2013-11-04 18:41:34503
deadbeef953c2ce2017-01-09 22:53:41504 sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
505 SignalDataReceived;
[email protected]28e20752013-07-10 00:45:36506 // Signal for notifying when the channel becomes ready to send data.
[email protected]d64719d2013-08-01 00:00:07507 // That occurs when the channel is enabled, the transport is writable,
508 // both local and remote descriptions are set, and the channel is unblocked.
[email protected]28e20752013-07-10 00:45:36509 sigslot::signal1<bool> SignalReadyToSendData;
Amit Hilbuchdd9390c2018-11-14 00:26:05510 cricket::MediaType media_type() const override {
511 return cricket::MEDIA_TYPE_DATA;
512 }
[email protected]28e20752013-07-10 00:45:36513
[email protected]cadf9042013-08-30 21:24:16514 protected:
515 // downcasts a MediaChannel.
Danil Chapovalov33b01f22016-05-11 17:55:27516 DataMediaChannel* media_channel() const override {
[email protected]cadf9042013-08-30 21:24:16517 return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
518 }
519
[email protected]28e20752013-07-10 00:45:36520 private:
[email protected]d4e598d2014-07-29 17:36:52521 struct SendDataMessageData : public rtc::MessageData {
[email protected]28e20752013-07-10 00:45:36522 SendDataMessageData(const SendDataParams& params,
jbaucheec21bd2016-03-20 13:15:43523 const rtc::CopyOnWriteBuffer* payload,
[email protected]28e20752013-07-10 00:45:36524 SendDataResult* result)
Yves Gerey665174f2018-06-19 13:03:05525 : params(params), payload(payload), result(result), succeeded(false) {}
[email protected]28e20752013-07-10 00:45:36526
527 const SendDataParams& params;
jbaucheec21bd2016-03-20 13:15:43528 const rtc::CopyOnWriteBuffer* payload;
[email protected]28e20752013-07-10 00:45:36529 SendDataResult* result;
530 bool succeeded;
531 };
532
[email protected]d4e598d2014-07-29 17:36:52533 struct DataReceivedMessageData : public rtc::MessageData {
[email protected]28e20752013-07-10 00:45:36534 // We copy the data because the data will become invalid after we
535 // handle DataMediaChannel::SignalDataReceived but before we fire
536 // SignalDataReceived.
Yves Gerey665174f2018-06-19 13:03:05537 DataReceivedMessageData(const ReceiveDataParams& params,
538 const char* data,
539 size_t len)
540 : params(params), payload(data, len) {}
[email protected]28e20752013-07-10 00:45:36541 const ReceiveDataParams params;
jbaucheec21bd2016-03-20 13:15:43542 const rtc::CopyOnWriteBuffer payload;
[email protected]28e20752013-07-10 00:45:36543 };
544
[email protected]d4e598d2014-07-29 17:36:52545 typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
[email protected]d64719d2013-08-01 00:00:07546
[email protected]28e20752013-07-10 00:45:36547 // overrides from BaseChannel
deadbeef953c2ce2017-01-09 22:53:41548 // Checks that data channel type is RTP.
Harald Alvestrand755187f2019-12-05 12:43:34549 bool CheckDataChannelTypeFromContent(const MediaContentDescription* content,
deadbeef953c2ce2017-01-09 22:53:41550 std::string* error_desc);
Danil Chapovalov33b01f22016-05-11 17:55:27551 bool SetLocalContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 18:34:51552 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 17:55:27553 std::string* error_desc) override;
554 bool SetRemoteContent_w(const MediaContentDescription* content,
Steve Anton3828c062017-12-06 18:34:51555 webrtc::SdpType type,
Danil Chapovalov33b01f22016-05-11 17:55:27556 std::string* error_desc) override;
Taylor Brandstetterbad33bf2016-08-25 20:31:14557 void UpdateMediaSendRecvState_w() override;
[email protected]28e20752013-07-10 00:45:36558
Danil Chapovalov33b01f22016-05-11 17:55:27559 void OnMessage(rtc::Message* pmsg) override;
Yves Gerey665174f2018-06-19 13:03:05560 void OnDataReceived(const ReceiveDataParams& params,
561 const char* data,
562 size_t len);
[email protected]d64719d2013-08-01 00:00:07563 void OnDataChannelReadyToSend(bool writable);
[email protected]28e20752013-07-10 00:45:36564
deadbeef953c2ce2017-01-09 22:53:41565 bool ready_to_send_data_ = false;
Peter Thatcherc2ee2c82015-08-07 23:05:34566
567 // Last DataSendParameters sent down to the media_channel() via
568 // SetSendParameters.
569 DataSendParameters last_send_params_;
570 // Last DataRecvParameters sent down to the media_channel() via
571 // SetRecvParameters.
572 DataRecvParameters last_recv_params_;
[email protected]28e20752013-07-10 00:45:36573};
574
575} // namespace cricket
576
Mirko Bonadei92ea95e2017-09-15 04:47:31577#endif // PC_CHANNEL_H_