xref: /aosp_15_r20/external/webrtc/pc/rtp_sender.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2015 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker // This file contains classes that implement RtpSenderInterface.
12*d9f75844SAndroid Build Coastguard Worker // An RtpSender associates a MediaStreamTrackInterface with an underlying
13*d9f75844SAndroid Build Coastguard Worker // transport (provided by AudioProviderInterface/VideoProviderInterface)
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #ifndef PC_RTP_SENDER_H_
16*d9f75844SAndroid Build Coastguard Worker #define PC_RTP_SENDER_H_
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
19*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
20*d9f75844SAndroid Build Coastguard Worker 
21*d9f75844SAndroid Build Coastguard Worker #include <memory>
22*d9f75844SAndroid Build Coastguard Worker #include <string>
23*d9f75844SAndroid Build Coastguard Worker #include <vector>
24*d9f75844SAndroid Build Coastguard Worker 
25*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
26*d9f75844SAndroid Build Coastguard Worker #include "api/crypto/frame_encryptor_interface.h"
27*d9f75844SAndroid Build Coastguard Worker #include "api/dtls_transport_interface.h"
28*d9f75844SAndroid Build Coastguard Worker #include "api/dtmf_sender_interface.h"
29*d9f75844SAndroid Build Coastguard Worker #include "api/frame_transformer_interface.h"
30*d9f75844SAndroid Build Coastguard Worker #include "api/media_stream_interface.h"
31*d9f75844SAndroid Build Coastguard Worker #include "api/media_types.h"
32*d9f75844SAndroid Build Coastguard Worker #include "api/rtc_error.h"
33*d9f75844SAndroid Build Coastguard Worker #include "api/rtp_parameters.h"
34*d9f75844SAndroid Build Coastguard Worker #include "api/rtp_sender_interface.h"
35*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
36*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
37*d9f75844SAndroid Build Coastguard Worker #include "media/base/audio_source.h"
38*d9f75844SAndroid Build Coastguard Worker #include "media/base/media_channel.h"
39*d9f75844SAndroid Build Coastguard Worker #include "pc/dtmf_sender.h"
40*d9f75844SAndroid Build Coastguard Worker #include "pc/legacy_stats_collector_interface.h"
41*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
42*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/synchronization/mutex.h"
43*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread.h"
44*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread_annotations.h"
45*d9f75844SAndroid Build Coastguard Worker 
46*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
47*d9f75844SAndroid Build Coastguard Worker 
48*d9f75844SAndroid Build Coastguard Worker bool UnimplementedRtpParameterHasValue(const RtpParameters& parameters);
49*d9f75844SAndroid Build Coastguard Worker 
50*d9f75844SAndroid Build Coastguard Worker // Internal interface used by PeerConnection.
51*d9f75844SAndroid Build Coastguard Worker class RtpSenderInternal : public RtpSenderInterface {
52*d9f75844SAndroid Build Coastguard Worker  public:
53*d9f75844SAndroid Build Coastguard Worker   // Sets the underlying MediaEngine channel associated with this RtpSender.
54*d9f75844SAndroid Build Coastguard Worker   // A VoiceMediaChannel should be used for audio RtpSenders and
55*d9f75844SAndroid Build Coastguard Worker   // a VideoMediaChannel should be used for video RtpSenders.
56*d9f75844SAndroid Build Coastguard Worker   // Must call SetMediaChannel(nullptr) before the media channel is destroyed.
57*d9f75844SAndroid Build Coastguard Worker   virtual void SetMediaChannel(cricket::MediaChannel* media_channel) = 0;
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   // Used to set the SSRC of the sender, once a local description has been set.
60*d9f75844SAndroid Build Coastguard Worker   // If `ssrc` is 0, this indiates that the sender should disconnect from the
61*d9f75844SAndroid Build Coastguard Worker   // underlying transport (this occurs if the sender isn't seen in a local
62*d9f75844SAndroid Build Coastguard Worker   // description).
63*d9f75844SAndroid Build Coastguard Worker   virtual void SetSsrc(uint32_t ssrc) = 0;
64*d9f75844SAndroid Build Coastguard Worker 
65*d9f75844SAndroid Build Coastguard Worker   virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
66*d9f75844SAndroid Build Coastguard Worker   virtual void set_init_send_encodings(
67*d9f75844SAndroid Build Coastguard Worker       const std::vector<RtpEncodingParameters>& init_send_encodings) = 0;
68*d9f75844SAndroid Build Coastguard Worker   virtual void set_transport(
69*d9f75844SAndroid Build Coastguard Worker       rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) = 0;
70*d9f75844SAndroid Build Coastguard Worker 
71*d9f75844SAndroid Build Coastguard Worker   virtual void Stop() = 0;
72*d9f75844SAndroid Build Coastguard Worker 
73*d9f75844SAndroid Build Coastguard Worker   // `GetParameters` and `SetParameters` operate with a transactional model.
74*d9f75844SAndroid Build Coastguard Worker   // Allow access to get/set parameters without invalidating transaction id.
75*d9f75844SAndroid Build Coastguard Worker   virtual RtpParameters GetParametersInternal() const = 0;
76*d9f75844SAndroid Build Coastguard Worker   virtual void SetParametersInternal(const RtpParameters& parameters,
77*d9f75844SAndroid Build Coastguard Worker                                      SetParametersCallback,
78*d9f75844SAndroid Build Coastguard Worker                                      bool blocking) = 0;
79*d9f75844SAndroid Build Coastguard Worker 
80*d9f75844SAndroid Build Coastguard Worker   // GetParameters and SetParameters will remove deactivated simulcast layers
81*d9f75844SAndroid Build Coastguard Worker   // and restore them on SetParameters. This is probably a Bad Idea, but we
82*d9f75844SAndroid Build Coastguard Worker   // do not know who depends on this behavior
83*d9f75844SAndroid Build Coastguard Worker   virtual RtpParameters GetParametersInternalWithAllLayers() const = 0;
84*d9f75844SAndroid Build Coastguard Worker   virtual RTCError SetParametersInternalWithAllLayers(
85*d9f75844SAndroid Build Coastguard Worker       const RtpParameters& parameters) = 0;
86*d9f75844SAndroid Build Coastguard Worker 
87*d9f75844SAndroid Build Coastguard Worker   // Additional checks that are specific to the Sender type
CheckSVCParameters(const RtpParameters & parameters)88*d9f75844SAndroid Build Coastguard Worker   virtual RTCError CheckSVCParameters(const RtpParameters& parameters) {
89*d9f75844SAndroid Build Coastguard Worker     return webrtc::RTCError::OK();
90*d9f75844SAndroid Build Coastguard Worker   }
91*d9f75844SAndroid Build Coastguard Worker 
92*d9f75844SAndroid Build Coastguard Worker   // Returns an ID that changes every time SetTrack() is called, but
93*d9f75844SAndroid Build Coastguard Worker   // otherwise remains constant. Used to generate IDs for stats.
94*d9f75844SAndroid Build Coastguard Worker   // The special value zero means that no track is attached.
95*d9f75844SAndroid Build Coastguard Worker   virtual int AttachmentId() const = 0;
96*d9f75844SAndroid Build Coastguard Worker 
97*d9f75844SAndroid Build Coastguard Worker   // Disables the layers identified by the specified RIDs.
98*d9f75844SAndroid Build Coastguard Worker   // If the specified list is empty, this is a no-op.
99*d9f75844SAndroid Build Coastguard Worker   virtual RTCError DisableEncodingLayers(
100*d9f75844SAndroid Build Coastguard Worker       const std::vector<std::string>& rid) = 0;
101*d9f75844SAndroid Build Coastguard Worker 
102*d9f75844SAndroid Build Coastguard Worker   virtual void SetTransceiverAsStopped() = 0;
103*d9f75844SAndroid Build Coastguard Worker 
104*d9f75844SAndroid Build Coastguard Worker   // Used by the owning transceiver to inform the sender on the currently
105*d9f75844SAndroid Build Coastguard Worker   // selected codecs.
106*d9f75844SAndroid Build Coastguard Worker   virtual void SetVideoCodecPreferences(
107*d9f75844SAndroid Build Coastguard Worker       std::vector<cricket::VideoCodec> codec_preferences) = 0;
108*d9f75844SAndroid Build Coastguard Worker };
109*d9f75844SAndroid Build Coastguard Worker 
110*d9f75844SAndroid Build Coastguard Worker // Shared implementation for RtpSenderInternal interface.
111*d9f75844SAndroid Build Coastguard Worker class RtpSenderBase : public RtpSenderInternal, public ObserverInterface {
112*d9f75844SAndroid Build Coastguard Worker  public:
113*d9f75844SAndroid Build Coastguard Worker   class SetStreamsObserver {
114*d9f75844SAndroid Build Coastguard Worker    public:
115*d9f75844SAndroid Build Coastguard Worker     virtual ~SetStreamsObserver() = default;
116*d9f75844SAndroid Build Coastguard Worker     virtual void OnSetStreams() = 0;
117*d9f75844SAndroid Build Coastguard Worker   };
118*d9f75844SAndroid Build Coastguard Worker 
119*d9f75844SAndroid Build Coastguard Worker   // Sets the underlying MediaEngine channel associated with this RtpSender.
120*d9f75844SAndroid Build Coastguard Worker   // A VoiceMediaChannel should be used for audio RtpSenders and
121*d9f75844SAndroid Build Coastguard Worker   // a VideoMediaChannel should be used for video RtpSenders.
122*d9f75844SAndroid Build Coastguard Worker   // Must call SetMediaChannel(nullptr) before the media channel is destroyed.
123*d9f75844SAndroid Build Coastguard Worker   void SetMediaChannel(cricket::MediaChannel* media_channel) override;
124*d9f75844SAndroid Build Coastguard Worker 
125*d9f75844SAndroid Build Coastguard Worker   bool SetTrack(MediaStreamTrackInterface* track) override;
track()126*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
127*d9f75844SAndroid Build Coastguard Worker     // This method is currently called from the worker thread by
128*d9f75844SAndroid Build Coastguard Worker     // RTCStatsCollector::PrepareTransceiverStatsInfosAndCallStats_s_w_n.
129*d9f75844SAndroid Build Coastguard Worker     // RTC_DCHECK_RUN_ON(signaling_thread_);
130*d9f75844SAndroid Build Coastguard Worker     return track_;
131*d9f75844SAndroid Build Coastguard Worker   }
132*d9f75844SAndroid Build Coastguard Worker 
133*d9f75844SAndroid Build Coastguard Worker   RtpParameters GetParameters() const override;
134*d9f75844SAndroid Build Coastguard Worker   RTCError SetParameters(const RtpParameters& parameters) override;
135*d9f75844SAndroid Build Coastguard Worker   void SetParametersAsync(const RtpParameters& parameters,
136*d9f75844SAndroid Build Coastguard Worker                           SetParametersCallback callback) override;
137*d9f75844SAndroid Build Coastguard Worker 
138*d9f75844SAndroid Build Coastguard Worker   // `GetParameters` and `SetParameters` operate with a transactional model.
139*d9f75844SAndroid Build Coastguard Worker   // Allow access to get/set parameters without invalidating transaction id.
140*d9f75844SAndroid Build Coastguard Worker   RtpParameters GetParametersInternal() const override;
141*d9f75844SAndroid Build Coastguard Worker   void SetParametersInternal(const RtpParameters& parameters,
142*d9f75844SAndroid Build Coastguard Worker                              SetParametersCallback callback = nullptr,
143*d9f75844SAndroid Build Coastguard Worker                              bool blocking = true) override;
144*d9f75844SAndroid Build Coastguard Worker   RTCError CheckSetParameters(const RtpParameters& parameters);
145*d9f75844SAndroid Build Coastguard Worker   RtpParameters GetParametersInternalWithAllLayers() const override;
146*d9f75844SAndroid Build Coastguard Worker   RTCError SetParametersInternalWithAllLayers(
147*d9f75844SAndroid Build Coastguard Worker       const RtpParameters& parameters) override;
148*d9f75844SAndroid Build Coastguard Worker 
149*d9f75844SAndroid Build Coastguard Worker   // Used to set the SSRC of the sender, once a local description has been set.
150*d9f75844SAndroid Build Coastguard Worker   // If `ssrc` is 0, this indiates that the sender should disconnect from the
151*d9f75844SAndroid Build Coastguard Worker   // underlying transport (this occurs if the sender isn't seen in a local
152*d9f75844SAndroid Build Coastguard Worker   // description).
153*d9f75844SAndroid Build Coastguard Worker   void SetSsrc(uint32_t ssrc) override;
ssrc()154*d9f75844SAndroid Build Coastguard Worker   uint32_t ssrc() const override {
155*d9f75844SAndroid Build Coastguard Worker     // This method is currently called from the worker thread by
156*d9f75844SAndroid Build Coastguard Worker     // RTCStatsCollector::PrepareTransceiverStatsInfosAndCallStats_s_w_n.
157*d9f75844SAndroid Build Coastguard Worker     // RTC_DCHECK_RUN_ON(signaling_thread_);
158*d9f75844SAndroid Build Coastguard Worker     return ssrc_;
159*d9f75844SAndroid Build Coastguard Worker   }
160*d9f75844SAndroid Build Coastguard Worker 
stream_ids()161*d9f75844SAndroid Build Coastguard Worker   std::vector<std::string> stream_ids() const override {
162*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(signaling_thread_);
163*d9f75844SAndroid Build Coastguard Worker     return stream_ids_;
164*d9f75844SAndroid Build Coastguard Worker   }
set_stream_ids(const std::vector<std::string> & stream_ids)165*d9f75844SAndroid Build Coastguard Worker   void set_stream_ids(const std::vector<std::string>& stream_ids) override {
166*d9f75844SAndroid Build Coastguard Worker     stream_ids_ = stream_ids;
167*d9f75844SAndroid Build Coastguard Worker   }
168*d9f75844SAndroid Build Coastguard Worker   void SetStreams(const std::vector<std::string>& stream_ids) override;
169*d9f75844SAndroid Build Coastguard Worker 
id()170*d9f75844SAndroid Build Coastguard Worker   std::string id() const override { return id_; }
171*d9f75844SAndroid Build Coastguard Worker 
set_init_send_encodings(const std::vector<RtpEncodingParameters> & init_send_encodings)172*d9f75844SAndroid Build Coastguard Worker   void set_init_send_encodings(
173*d9f75844SAndroid Build Coastguard Worker       const std::vector<RtpEncodingParameters>& init_send_encodings) override {
174*d9f75844SAndroid Build Coastguard Worker     init_parameters_.encodings = init_send_encodings;
175*d9f75844SAndroid Build Coastguard Worker   }
init_send_encodings()176*d9f75844SAndroid Build Coastguard Worker   std::vector<RtpEncodingParameters> init_send_encodings() const override {
177*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(signaling_thread_);
178*d9f75844SAndroid Build Coastguard Worker     return init_parameters_.encodings;
179*d9f75844SAndroid Build Coastguard Worker   }
180*d9f75844SAndroid Build Coastguard Worker 
set_transport(rtc::scoped_refptr<DtlsTransportInterface> dtls_transport)181*d9f75844SAndroid Build Coastguard Worker   void set_transport(
182*d9f75844SAndroid Build Coastguard Worker       rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override {
183*d9f75844SAndroid Build Coastguard Worker     dtls_transport_ = dtls_transport;
184*d9f75844SAndroid Build Coastguard Worker   }
dtls_transport()185*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override {
186*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(signaling_thread_);
187*d9f75844SAndroid Build Coastguard Worker     return dtls_transport_;
188*d9f75844SAndroid Build Coastguard Worker   }
189*d9f75844SAndroid Build Coastguard Worker 
190*d9f75844SAndroid Build Coastguard Worker   void SetFrameEncryptor(
191*d9f75844SAndroid Build Coastguard Worker       rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
192*d9f75844SAndroid Build Coastguard Worker 
GetFrameEncryptor()193*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor()
194*d9f75844SAndroid Build Coastguard Worker       const override {
195*d9f75844SAndroid Build Coastguard Worker     return frame_encryptor_;
196*d9f75844SAndroid Build Coastguard Worker   }
197*d9f75844SAndroid Build Coastguard Worker 
198*d9f75844SAndroid Build Coastguard Worker   void Stop() override;
199*d9f75844SAndroid Build Coastguard Worker 
200*d9f75844SAndroid Build Coastguard Worker   // Returns an ID that changes every time SetTrack() is called, but
201*d9f75844SAndroid Build Coastguard Worker   // otherwise remains constant. Used to generate IDs for stats.
202*d9f75844SAndroid Build Coastguard Worker   // The special value zero means that no track is attached.
AttachmentId()203*d9f75844SAndroid Build Coastguard Worker   int AttachmentId() const override { return attachment_id_; }
204*d9f75844SAndroid Build Coastguard Worker 
205*d9f75844SAndroid Build Coastguard Worker   // Disables the layers identified by the specified RIDs.
206*d9f75844SAndroid Build Coastguard Worker   // If the specified list is empty, this is a no-op.
207*d9f75844SAndroid Build Coastguard Worker   RTCError DisableEncodingLayers(const std::vector<std::string>& rid) override;
208*d9f75844SAndroid Build Coastguard Worker 
209*d9f75844SAndroid Build Coastguard Worker   void SetEncoderToPacketizerFrameTransformer(
210*d9f75844SAndroid Build Coastguard Worker       rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) override;
211*d9f75844SAndroid Build Coastguard Worker 
212*d9f75844SAndroid Build Coastguard Worker   void SetEncoderSelector(
213*d9f75844SAndroid Build Coastguard Worker       std::unique_ptr<VideoEncoderFactory::EncoderSelectorInterface>
214*d9f75844SAndroid Build Coastguard Worker           encoder_selector) override;
215*d9f75844SAndroid Build Coastguard Worker 
216*d9f75844SAndroid Build Coastguard Worker   void SetEncoderSelectorOnChannel();
217*d9f75844SAndroid Build Coastguard Worker 
SetTransceiverAsStopped()218*d9f75844SAndroid Build Coastguard Worker   void SetTransceiverAsStopped() override {
219*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(signaling_thread_);
220*d9f75844SAndroid Build Coastguard Worker     is_transceiver_stopped_ = true;
221*d9f75844SAndroid Build Coastguard Worker   }
222*d9f75844SAndroid Build Coastguard Worker 
SetVideoCodecPreferences(std::vector<cricket::VideoCodec> codec_preferences)223*d9f75844SAndroid Build Coastguard Worker   void SetVideoCodecPreferences(
224*d9f75844SAndroid Build Coastguard Worker       std::vector<cricket::VideoCodec> codec_preferences) override {
225*d9f75844SAndroid Build Coastguard Worker     video_codec_preferences_ = codec_preferences;
226*d9f75844SAndroid Build Coastguard Worker   }
227*d9f75844SAndroid Build Coastguard Worker 
228*d9f75844SAndroid Build Coastguard Worker  protected:
229*d9f75844SAndroid Build Coastguard Worker   // If `set_streams_observer` is not null, it is invoked when SetStreams()
230*d9f75844SAndroid Build Coastguard Worker   // is called. `set_streams_observer` is not owned by this object. If not
231*d9f75844SAndroid Build Coastguard Worker   // null, it must be valid at least until this sender becomes stopped.
232*d9f75844SAndroid Build Coastguard Worker   RtpSenderBase(rtc::Thread* worker_thread,
233*d9f75844SAndroid Build Coastguard Worker                 const std::string& id,
234*d9f75844SAndroid Build Coastguard Worker                 SetStreamsObserver* set_streams_observer);
235*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org/8694): Since SSRC == 0 is technically valid, figure
236*d9f75844SAndroid Build Coastguard Worker   // out some other way to test if we have a valid SSRC.
can_send_track()237*d9f75844SAndroid Build Coastguard Worker   bool can_send_track() const { return track_ && ssrc_; }
238*d9f75844SAndroid Build Coastguard Worker 
239*d9f75844SAndroid Build Coastguard Worker   virtual std::string track_kind() const = 0;
240*d9f75844SAndroid Build Coastguard Worker 
241*d9f75844SAndroid Build Coastguard Worker   // Enable sending on the media channel.
242*d9f75844SAndroid Build Coastguard Worker   virtual void SetSend() = 0;
243*d9f75844SAndroid Build Coastguard Worker   // Disable sending on the media channel.
244*d9f75844SAndroid Build Coastguard Worker   virtual void ClearSend() = 0;
245*d9f75844SAndroid Build Coastguard Worker 
246*d9f75844SAndroid Build Coastguard Worker   // Template method pattern to allow subclasses to add custom behavior for
247*d9f75844SAndroid Build Coastguard Worker   // when tracks are attached, detached, and for adding tracks to statistics.
AttachTrack()248*d9f75844SAndroid Build Coastguard Worker   virtual void AttachTrack() {}
DetachTrack()249*d9f75844SAndroid Build Coastguard Worker   virtual void DetachTrack() {}
AddTrackToStats()250*d9f75844SAndroid Build Coastguard Worker   virtual void AddTrackToStats() {}
RemoveTrackFromStats()251*d9f75844SAndroid Build Coastguard Worker   virtual void RemoveTrackFromStats() {}
252*d9f75844SAndroid Build Coastguard Worker 
253*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* const signaling_thread_;
254*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* const worker_thread_;
255*d9f75844SAndroid Build Coastguard Worker   uint32_t ssrc_ = 0;
256*d9f75844SAndroid Build Coastguard Worker   bool stopped_ RTC_GUARDED_BY(signaling_thread_) = false;
257*d9f75844SAndroid Build Coastguard Worker   bool is_transceiver_stopped_ RTC_GUARDED_BY(signaling_thread_) = false;
258*d9f75844SAndroid Build Coastguard Worker   int attachment_id_ = 0;
259*d9f75844SAndroid Build Coastguard Worker   const std::string id_;
260*d9f75844SAndroid Build Coastguard Worker 
261*d9f75844SAndroid Build Coastguard Worker   std::vector<std::string> stream_ids_;
262*d9f75844SAndroid Build Coastguard Worker   RtpParameters init_parameters_;
263*d9f75844SAndroid Build Coastguard Worker   std::vector<cricket::VideoCodec> video_codec_preferences_;
264*d9f75844SAndroid Build Coastguard Worker 
265*d9f75844SAndroid Build Coastguard Worker   // TODO(tommi): `media_channel_` and several other member variables in this
266*d9f75844SAndroid Build Coastguard Worker   // class (ssrc_, stopped_, etc) are accessed from more than one thread without
267*d9f75844SAndroid Build Coastguard Worker   // a guard or lock. Internally there are also several Invoke()s that we could
268*d9f75844SAndroid Build Coastguard Worker   // remove since the upstream code may already be performing several operations
269*d9f75844SAndroid Build Coastguard Worker   // on the worker thread.
270*d9f75844SAndroid Build Coastguard Worker   cricket::MediaChannel* media_channel_ = nullptr;
271*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<MediaStreamTrackInterface> track_;
272*d9f75844SAndroid Build Coastguard Worker 
273*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_;
274*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
275*d9f75844SAndroid Build Coastguard Worker   // `last_transaction_id_` is used to verify that `SetParameters` is receiving
276*d9f75844SAndroid Build Coastguard Worker   // the parameters object that was last returned from `GetParameters`.
277*d9f75844SAndroid Build Coastguard Worker   // As such, it is used for internal verification and is not observable by the
278*d9f75844SAndroid Build Coastguard Worker   // the client. It is marked as mutable to enable `GetParameters` to be a
279*d9f75844SAndroid Build Coastguard Worker   // const method.
280*d9f75844SAndroid Build Coastguard Worker   mutable absl::optional<std::string> last_transaction_id_;
281*d9f75844SAndroid Build Coastguard Worker   std::vector<std::string> disabled_rids_;
282*d9f75844SAndroid Build Coastguard Worker 
283*d9f75844SAndroid Build Coastguard Worker   SetStreamsObserver* set_streams_observer_ = nullptr;
284*d9f75844SAndroid Build Coastguard Worker 
285*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_;
286*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<VideoEncoderFactory::EncoderSelectorInterface>
287*d9f75844SAndroid Build Coastguard Worker       encoder_selector_;
288*d9f75844SAndroid Build Coastguard Worker };
289*d9f75844SAndroid Build Coastguard Worker 
290*d9f75844SAndroid Build Coastguard Worker // LocalAudioSinkAdapter receives data callback as a sink to the local
291*d9f75844SAndroid Build Coastguard Worker // AudioTrack, and passes the data to the sink of AudioSource.
292*d9f75844SAndroid Build Coastguard Worker class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
293*d9f75844SAndroid Build Coastguard Worker                               public cricket::AudioSource {
294*d9f75844SAndroid Build Coastguard Worker  public:
295*d9f75844SAndroid Build Coastguard Worker   LocalAudioSinkAdapter();
296*d9f75844SAndroid Build Coastguard Worker   virtual ~LocalAudioSinkAdapter();
297*d9f75844SAndroid Build Coastguard Worker 
298*d9f75844SAndroid Build Coastguard Worker  private:
299*d9f75844SAndroid Build Coastguard Worker   // AudioSinkInterface implementation.
300*d9f75844SAndroid Build Coastguard Worker   void OnData(const void* audio_data,
301*d9f75844SAndroid Build Coastguard Worker               int bits_per_sample,
302*d9f75844SAndroid Build Coastguard Worker               int sample_rate,
303*d9f75844SAndroid Build Coastguard Worker               size_t number_of_channels,
304*d9f75844SAndroid Build Coastguard Worker               size_t number_of_frames,
305*d9f75844SAndroid Build Coastguard Worker               absl::optional<int64_t> absolute_capture_timestamp_ms) override;
306*d9f75844SAndroid Build Coastguard Worker 
307*d9f75844SAndroid Build Coastguard Worker   // AudioSinkInterface implementation.
OnData(const void * audio_data,int bits_per_sample,int sample_rate,size_t number_of_channels,size_t number_of_frames)308*d9f75844SAndroid Build Coastguard Worker   void OnData(const void* audio_data,
309*d9f75844SAndroid Build Coastguard Worker               int bits_per_sample,
310*d9f75844SAndroid Build Coastguard Worker               int sample_rate,
311*d9f75844SAndroid Build Coastguard Worker               size_t number_of_channels,
312*d9f75844SAndroid Build Coastguard Worker               size_t number_of_frames) override {
313*d9f75844SAndroid Build Coastguard Worker     OnData(audio_data, bits_per_sample, sample_rate, number_of_channels,
314*d9f75844SAndroid Build Coastguard Worker            number_of_frames,
315*d9f75844SAndroid Build Coastguard Worker            /*absolute_capture_timestamp_ms=*/absl::nullopt);
316*d9f75844SAndroid Build Coastguard Worker   }
317*d9f75844SAndroid Build Coastguard Worker 
318*d9f75844SAndroid Build Coastguard Worker   // AudioSinkInterface implementation.
NumPreferredChannels()319*d9f75844SAndroid Build Coastguard Worker   int NumPreferredChannels() const override { return num_preferred_channels_; }
320*d9f75844SAndroid Build Coastguard Worker 
321*d9f75844SAndroid Build Coastguard Worker   // cricket::AudioSource implementation.
322*d9f75844SAndroid Build Coastguard Worker   void SetSink(cricket::AudioSource::Sink* sink) override;
323*d9f75844SAndroid Build Coastguard Worker 
324*d9f75844SAndroid Build Coastguard Worker   cricket::AudioSource::Sink* sink_;
325*d9f75844SAndroid Build Coastguard Worker   // Critical section protecting `sink_`.
326*d9f75844SAndroid Build Coastguard Worker   Mutex lock_;
327*d9f75844SAndroid Build Coastguard Worker   int num_preferred_channels_ = -1;
328*d9f75844SAndroid Build Coastguard Worker };
329*d9f75844SAndroid Build Coastguard Worker 
330*d9f75844SAndroid Build Coastguard Worker class AudioRtpSender : public DtmfProviderInterface, public RtpSenderBase {
331*d9f75844SAndroid Build Coastguard Worker  public:
332*d9f75844SAndroid Build Coastguard Worker   // Construct an RtpSender for audio with the given sender ID.
333*d9f75844SAndroid Build Coastguard Worker   // The sender is initialized with no track to send and no associated streams.
334*d9f75844SAndroid Build Coastguard Worker   // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
335*d9f75844SAndroid Build Coastguard Worker   // at the appropriate times.
336*d9f75844SAndroid Build Coastguard Worker   // If `set_streams_observer` is not null, it is invoked when SetStreams()
337*d9f75844SAndroid Build Coastguard Worker   // is called. `set_streams_observer` is not owned by this object. If not
338*d9f75844SAndroid Build Coastguard Worker   // null, it must be valid at least until this sender becomes stopped.
339*d9f75844SAndroid Build Coastguard Worker   static rtc::scoped_refptr<AudioRtpSender> Create(
340*d9f75844SAndroid Build Coastguard Worker       rtc::Thread* worker_thread,
341*d9f75844SAndroid Build Coastguard Worker       const std::string& id,
342*d9f75844SAndroid Build Coastguard Worker       LegacyStatsCollectorInterface* stats,
343*d9f75844SAndroid Build Coastguard Worker       SetStreamsObserver* set_streams_observer);
344*d9f75844SAndroid Build Coastguard Worker   virtual ~AudioRtpSender();
345*d9f75844SAndroid Build Coastguard Worker 
346*d9f75844SAndroid Build Coastguard Worker   // DtmfSenderProvider implementation.
347*d9f75844SAndroid Build Coastguard Worker   bool CanInsertDtmf() override;
348*d9f75844SAndroid Build Coastguard Worker   bool InsertDtmf(int code, int duration) override;
349*d9f75844SAndroid Build Coastguard Worker 
350*d9f75844SAndroid Build Coastguard Worker   // ObserverInterface implementation.
351*d9f75844SAndroid Build Coastguard Worker   void OnChanged() override;
352*d9f75844SAndroid Build Coastguard Worker 
media_type()353*d9f75844SAndroid Build Coastguard Worker   cricket::MediaType media_type() const override {
354*d9f75844SAndroid Build Coastguard Worker     return cricket::MEDIA_TYPE_AUDIO;
355*d9f75844SAndroid Build Coastguard Worker   }
track_kind()356*d9f75844SAndroid Build Coastguard Worker   std::string track_kind() const override {
357*d9f75844SAndroid Build Coastguard Worker     return MediaStreamTrackInterface::kAudioKind;
358*d9f75844SAndroid Build Coastguard Worker   }
359*d9f75844SAndroid Build Coastguard Worker 
360*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
361*d9f75844SAndroid Build Coastguard Worker   RTCError GenerateKeyFrame(const std::vector<std::string>& rids) override;
362*d9f75844SAndroid Build Coastguard Worker 
363*d9f75844SAndroid Build Coastguard Worker  protected:
364*d9f75844SAndroid Build Coastguard Worker   AudioRtpSender(rtc::Thread* worker_thread,
365*d9f75844SAndroid Build Coastguard Worker                  const std::string& id,
366*d9f75844SAndroid Build Coastguard Worker                  LegacyStatsCollectorInterface* legacy_stats,
367*d9f75844SAndroid Build Coastguard Worker                  SetStreamsObserver* set_streams_observer);
368*d9f75844SAndroid Build Coastguard Worker 
369*d9f75844SAndroid Build Coastguard Worker   void SetSend() override;
370*d9f75844SAndroid Build Coastguard Worker   void ClearSend() override;
371*d9f75844SAndroid Build Coastguard Worker 
372*d9f75844SAndroid Build Coastguard Worker   // Hooks to allow custom logic when tracks are attached and detached.
373*d9f75844SAndroid Build Coastguard Worker   void AttachTrack() override;
374*d9f75844SAndroid Build Coastguard Worker   void DetachTrack() override;
375*d9f75844SAndroid Build Coastguard Worker   void AddTrackToStats() override;
376*d9f75844SAndroid Build Coastguard Worker   void RemoveTrackFromStats() override;
377*d9f75844SAndroid Build Coastguard Worker 
378*d9f75844SAndroid Build Coastguard Worker  private:
voice_media_channel()379*d9f75844SAndroid Build Coastguard Worker   cricket::VoiceMediaChannel* voice_media_channel() {
380*d9f75844SAndroid Build Coastguard Worker     return static_cast<cricket::VoiceMediaChannel*>(media_channel_);
381*d9f75844SAndroid Build Coastguard Worker   }
audio_track()382*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
383*d9f75844SAndroid Build Coastguard Worker     return rtc::scoped_refptr<AudioTrackInterface>(
384*d9f75844SAndroid Build Coastguard Worker         static_cast<AudioTrackInterface*>(track_.get()));
385*d9f75844SAndroid Build Coastguard Worker   }
386*d9f75844SAndroid Build Coastguard Worker 
387*d9f75844SAndroid Build Coastguard Worker   LegacyStatsCollectorInterface* legacy_stats_ = nullptr;
388*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<DtmfSender> dtmf_sender_;
389*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
390*d9f75844SAndroid Build Coastguard Worker   bool cached_track_enabled_ = false;
391*d9f75844SAndroid Build Coastguard Worker 
392*d9f75844SAndroid Build Coastguard Worker   // Used to pass the data callback from the `track_` to the other end of
393*d9f75844SAndroid Build Coastguard Worker   // cricket::AudioSource.
394*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
395*d9f75844SAndroid Build Coastguard Worker };
396*d9f75844SAndroid Build Coastguard Worker 
397*d9f75844SAndroid Build Coastguard Worker class VideoRtpSender : public RtpSenderBase {
398*d9f75844SAndroid Build Coastguard Worker  public:
399*d9f75844SAndroid Build Coastguard Worker   // Construct an RtpSender for video with the given sender ID.
400*d9f75844SAndroid Build Coastguard Worker   // The sender is initialized with no track to send and no associated streams.
401*d9f75844SAndroid Build Coastguard Worker   // If `set_streams_observer` is not null, it is invoked when SetStreams()
402*d9f75844SAndroid Build Coastguard Worker   // is called. `set_streams_observer` is not owned by this object. If not
403*d9f75844SAndroid Build Coastguard Worker   // null, it must be valid at least until this sender becomes stopped.
404*d9f75844SAndroid Build Coastguard Worker   static rtc::scoped_refptr<VideoRtpSender> Create(
405*d9f75844SAndroid Build Coastguard Worker       rtc::Thread* worker_thread,
406*d9f75844SAndroid Build Coastguard Worker       const std::string& id,
407*d9f75844SAndroid Build Coastguard Worker       SetStreamsObserver* set_streams_observer);
408*d9f75844SAndroid Build Coastguard Worker   virtual ~VideoRtpSender();
409*d9f75844SAndroid Build Coastguard Worker 
410*d9f75844SAndroid Build Coastguard Worker   // ObserverInterface implementation
411*d9f75844SAndroid Build Coastguard Worker   void OnChanged() override;
412*d9f75844SAndroid Build Coastguard Worker 
media_type()413*d9f75844SAndroid Build Coastguard Worker   cricket::MediaType media_type() const override {
414*d9f75844SAndroid Build Coastguard Worker     return cricket::MEDIA_TYPE_VIDEO;
415*d9f75844SAndroid Build Coastguard Worker   }
track_kind()416*d9f75844SAndroid Build Coastguard Worker   std::string track_kind() const override {
417*d9f75844SAndroid Build Coastguard Worker     return MediaStreamTrackInterface::kVideoKind;
418*d9f75844SAndroid Build Coastguard Worker   }
419*d9f75844SAndroid Build Coastguard Worker 
420*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
421*d9f75844SAndroid Build Coastguard Worker   RTCError GenerateKeyFrame(const std::vector<std::string>& rids) override;
422*d9f75844SAndroid Build Coastguard Worker 
423*d9f75844SAndroid Build Coastguard Worker   RTCError CheckSVCParameters(const RtpParameters& parameters) override;
424*d9f75844SAndroid Build Coastguard Worker 
425*d9f75844SAndroid Build Coastguard Worker  protected:
426*d9f75844SAndroid Build Coastguard Worker   VideoRtpSender(rtc::Thread* worker_thread,
427*d9f75844SAndroid Build Coastguard Worker                  const std::string& id,
428*d9f75844SAndroid Build Coastguard Worker                  SetStreamsObserver* set_streams_observer);
429*d9f75844SAndroid Build Coastguard Worker 
430*d9f75844SAndroid Build Coastguard Worker   void SetSend() override;
431*d9f75844SAndroid Build Coastguard Worker   void ClearSend() override;
432*d9f75844SAndroid Build Coastguard Worker 
433*d9f75844SAndroid Build Coastguard Worker   // Hook to allow custom logic when tracks are attached.
434*d9f75844SAndroid Build Coastguard Worker   void AttachTrack() override;
435*d9f75844SAndroid Build Coastguard Worker 
436*d9f75844SAndroid Build Coastguard Worker  private:
video_media_channel()437*d9f75844SAndroid Build Coastguard Worker   cricket::VideoMediaChannel* video_media_channel() {
438*d9f75844SAndroid Build Coastguard Worker     return static_cast<cricket::VideoMediaChannel*>(media_channel_);
439*d9f75844SAndroid Build Coastguard Worker   }
video_track()440*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<VideoTrackInterface> video_track() const {
441*d9f75844SAndroid Build Coastguard Worker     return rtc::scoped_refptr<VideoTrackInterface>(
442*d9f75844SAndroid Build Coastguard Worker         static_cast<VideoTrackInterface*>(track_.get()));
443*d9f75844SAndroid Build Coastguard Worker   }
444*d9f75844SAndroid Build Coastguard Worker 
445*d9f75844SAndroid Build Coastguard Worker   VideoTrackInterface::ContentHint cached_track_content_hint_ =
446*d9f75844SAndroid Build Coastguard Worker       VideoTrackInterface::ContentHint::kNone;
447*d9f75844SAndroid Build Coastguard Worker };
448*d9f75844SAndroid Build Coastguard Worker 
449*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
450*d9f75844SAndroid Build Coastguard Worker 
451*d9f75844SAndroid Build Coastguard Worker #endif  // PC_RTP_SENDER_H_
452