ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * 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. |
| 9 | */ |
| 10 | |
| 11 | // This file contains classes that implement RtpSenderInterface. |
| 12 | // An RtpSender associates a MediaStreamTrackInterface with an underlying |
| 13 | // transport (provided by AudioProviderInterface/VideoProviderInterface) |
| 14 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 15 | #ifndef PC_RTP_SENDER_H_ |
| 16 | #define PC_RTP_SENDER_H_ |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 17 | |
| 18 | #include <memory> |
| 19 | #include <string> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 20 | #include <vector> |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 21 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "api/media_stream_interface.h" |
| 23 | #include "api/rtp_sender_interface.h" |
| 24 | #include "media/base/audio_source.h" |
| 25 | #include "media/base/media_channel.h" |
| 26 | #include "pc/dtmf_sender.h" |
| 27 | #include "rtc_base/critical_section.h" |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 31 | class StatsCollector; |
| 32 | |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 33 | bool UnimplementedRtpParameterHasValue(const RtpParameters& parameters); |
| 34 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 35 | // Internal interface used by PeerConnection. |
| 36 | class RtpSenderInternal : public RtpSenderInterface { |
| 37 | public: |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 38 | // Sets the underlying MediaEngine channel associated with this RtpSender. |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 39 | // A VoiceMediaChannel should be used for audio RtpSenders and |
| 40 | // a VideoMediaChannel should be used for video RtpSenders. |
| 41 | // Must call SetMediaChannel(nullptr) before the media channel is destroyed. |
| 42 | virtual void SetMediaChannel(cricket::MediaChannel* media_channel) = 0; |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 43 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 44 | // Used to set the SSRC of the sender, once a local description has been set. |
| 45 | // If |ssrc| is 0, this indiates that the sender should disconnect from the |
| 46 | // underlying transport (this occurs if the sender isn't seen in a local |
| 47 | // description). |
| 48 | virtual void SetSsrc(uint32_t ssrc) = 0; |
| 49 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 50 | virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0; |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 51 | virtual void set_init_send_encodings( |
| 52 | const std::vector<RtpEncodingParameters>& init_send_encodings) = 0; |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 53 | virtual void set_transport( |
| 54 | rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) = 0; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 55 | |
| 56 | virtual void Stop() = 0; |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 57 | |
| 58 | // Returns an ID that changes every time SetTrack() is called, but |
| 59 | // otherwise remains constant. Used to generate IDs for stats. |
| 60 | // The special value zero means that no track is attached. |
| 61 | virtual int AttachmentId() const = 0; |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 62 | |
| 63 | // Disables the layers identified by the specified RIDs. |
| 64 | // If the specified list is empty, this is a no-op. |
| 65 | virtual RTCError DisableEncodingLayers( |
| 66 | const std::vector<std::string>& rid) = 0; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 69 | // Shared implementation for RtpSenderInternal interface. |
| 70 | class RtpSenderBase : public RtpSenderInternal, public ObserverInterface { |
| 71 | public: |
| 72 | // Sets the underlying MediaEngine channel associated with this RtpSender. |
| 73 | // A VoiceMediaChannel should be used for audio RtpSenders and |
| 74 | // a VideoMediaChannel should be used for video RtpSenders. |
| 75 | // Must call SetMediaChannel(nullptr) before the media channel is destroyed. |
| 76 | void SetMediaChannel(cricket::MediaChannel* media_channel) override; |
| 77 | |
| 78 | bool SetTrack(MediaStreamTrackInterface* track) override; |
| 79 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 80 | return track_; |
| 81 | } |
| 82 | |
| 83 | RtpParameters GetParameters() const override; |
| 84 | RTCError SetParameters(const RtpParameters& parameters) override; |
| 85 | |
| 86 | // Used to set the SSRC of the sender, once a local description has been set. |
| 87 | // If |ssrc| is 0, this indiates that the sender should disconnect from the |
| 88 | // underlying transport (this occurs if the sender isn't seen in a local |
| 89 | // description). |
| 90 | void SetSsrc(uint32_t ssrc) override; |
| 91 | uint32_t ssrc() const override { return ssrc_; } |
| 92 | |
| 93 | std::vector<std::string> stream_ids() const override { return stream_ids_; } |
| 94 | void set_stream_ids(const std::vector<std::string>& stream_ids) override { |
| 95 | stream_ids_ = stream_ids; |
| 96 | } |
| 97 | |
| 98 | std::string id() const override { return id_; } |
| 99 | |
| 100 | void set_init_send_encodings( |
| 101 | const std::vector<RtpEncodingParameters>& init_send_encodings) override { |
| 102 | init_parameters_.encodings = init_send_encodings; |
| 103 | } |
| 104 | std::vector<RtpEncodingParameters> init_send_encodings() const override { |
| 105 | return init_parameters_.encodings; |
| 106 | } |
| 107 | |
| 108 | void set_transport( |
| 109 | rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override { |
| 110 | dtls_transport_ = dtls_transport; |
| 111 | } |
| 112 | rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override { |
| 113 | return dtls_transport_; |
| 114 | } |
| 115 | |
| 116 | void SetFrameEncryptor( |
| 117 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override; |
| 118 | |
| 119 | rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor() |
| 120 | const override { |
| 121 | return frame_encryptor_; |
| 122 | } |
| 123 | |
| 124 | void Stop() override; |
| 125 | |
| 126 | // Returns an ID that changes every time SetTrack() is called, but |
| 127 | // otherwise remains constant. Used to generate IDs for stats. |
| 128 | // The special value zero means that no track is attached. |
| 129 | int AttachmentId() const override { return attachment_id_; } |
| 130 | |
| 131 | // Disables the layers identified by the specified RIDs. |
| 132 | // If the specified list is empty, this is a no-op. |
| 133 | RTCError DisableEncodingLayers(const std::vector<std::string>& rid) override; |
| 134 | |
| 135 | protected: |
| 136 | RtpSenderBase(rtc::Thread* worker_thread, const std::string& id); |
| 137 | // TODO(nisse): Since SSRC == 0 is technically valid, figure out |
| 138 | // some other way to test if we have a valid SSRC. |
| 139 | bool can_send_track() const { return track_ && ssrc_; } |
| 140 | |
| 141 | virtual std::string track_kind() const = 0; |
| 142 | |
| 143 | // Enable sending on the media channel. |
| 144 | virtual void SetSend() = 0; |
| 145 | // Disable sending on the media channel. |
| 146 | virtual void ClearSend() = 0; |
| 147 | |
| 148 | // Template method pattern to allow subclasses to add custom behavior for |
| 149 | // when tracks are attached, detached, and for adding tracks to statistics. |
| 150 | virtual void AttachTrack() {} |
| 151 | virtual void DetachTrack() {} |
| 152 | virtual void AddTrackToStats() {} |
| 153 | virtual void RemoveTrackFromStats() {} |
| 154 | |
| 155 | rtc::Thread* worker_thread_; |
| 156 | uint32_t ssrc_ = 0; |
| 157 | bool stopped_ = false; |
| 158 | int attachment_id_ = 0; |
| 159 | const std::string id_; |
| 160 | |
| 161 | std::vector<std::string> stream_ids_; |
| 162 | RtpParameters init_parameters_; |
| 163 | |
| 164 | cricket::MediaChannel* media_channel_ = nullptr; |
| 165 | rtc::scoped_refptr<MediaStreamTrackInterface> track_; |
| 166 | |
| 167 | rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_; |
| 168 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_; |
| 169 | // |last_transaction_id_| is used to verify that |SetParameters| is receiving |
| 170 | // the parameters object that was last returned from |GetParameters|. |
| 171 | // As such, it is used for internal verification and is not observable by the |
| 172 | // the client. It is marked as mutable to enable |GetParameters| to be a |
| 173 | // const method. |
| 174 | mutable absl::optional<std::string> last_transaction_id_; |
| 175 | std::vector<std::string> disabled_rids_; |
| 176 | }; |
| 177 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 178 | // LocalAudioSinkAdapter receives data callback as a sink to the local |
| 179 | // AudioTrack, and passes the data to the sink of AudioSource. |
| 180 | class LocalAudioSinkAdapter : public AudioTrackSinkInterface, |
| 181 | public cricket::AudioSource { |
| 182 | public: |
| 183 | LocalAudioSinkAdapter(); |
| 184 | virtual ~LocalAudioSinkAdapter(); |
| 185 | |
| 186 | private: |
| 187 | // AudioSinkInterface implementation. |
| 188 | void OnData(const void* audio_data, |
| 189 | int bits_per_sample, |
| 190 | int sample_rate, |
| 191 | size_t number_of_channels, |
| 192 | size_t number_of_frames) override; |
| 193 | |
| 194 | // cricket::AudioSource implementation. |
| 195 | void SetSink(cricket::AudioSource::Sink* sink) override; |
| 196 | |
| 197 | cricket::AudioSource::Sink* sink_; |
| 198 | // Critical section protecting |sink_|. |
| 199 | rtc::CriticalSection lock_; |
| 200 | }; |
| 201 | |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 202 | class AudioRtpSender : public DtmfProviderInterface, public RtpSenderBase { |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 203 | public: |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 204 | // Construct an RtpSender for audio with the given sender ID. |
| 205 | // The sender is initialized with no track to send and no associated streams. |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 206 | // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called |
| 207 | // at the appropriate times. |
| 208 | static rtc::scoped_refptr<AudioRtpSender> Create(rtc::Thread* worker_thread, |
| 209 | const std::string& id, |
| 210 | StatsCollector* stats); |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 211 | virtual ~AudioRtpSender(); |
| 212 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 213 | // DtmfSenderProvider implementation. |
| 214 | bool CanInsertDtmf() override; |
| 215 | bool InsertDtmf(int code, int duration) override; |
| 216 | sigslot::signal0<>* GetOnDestroyedSignal() override; |
| 217 | |
| 218 | // ObserverInterface implementation. |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 219 | void OnChanged() override; |
| 220 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 221 | cricket::MediaType media_type() const override { |
| 222 | return cricket::MEDIA_TYPE_AUDIO; |
| 223 | } |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 224 | std::string track_kind() const override { |
| 225 | return MediaStreamTrackInterface::kAudioKind; |
| 226 | } |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 227 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 228 | rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override; |
| 229 | |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 230 | protected: |
| 231 | AudioRtpSender(rtc::Thread* worker_thread, |
| 232 | const std::string& id, |
| 233 | StatsCollector* stats); |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 234 | |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 235 | void SetSend() override; |
| 236 | void ClearSend() override; |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 237 | |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 238 | // Hooks to allow custom logic when tracks are attached and detached. |
| 239 | void AttachTrack() override; |
| 240 | void DetachTrack() override; |
| 241 | void AddTrackToStats() override; |
| 242 | void RemoveTrackFromStats() override; |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 243 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 244 | private: |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 245 | cricket::VoiceMediaChannel* voice_media_channel() { |
| 246 | return static_cast<cricket::VoiceMediaChannel*>(media_channel_); |
| 247 | } |
| 248 | rtc::scoped_refptr<AudioTrackInterface> audio_track() const { |
| 249 | return rtc::scoped_refptr<AudioTrackInterface>( |
| 250 | static_cast<AudioTrackInterface*>(track_.get())); |
| 251 | } |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 252 | sigslot::signal0<> SignalDestroyed; |
| 253 | |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 254 | StatsCollector* stats_ = nullptr; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 255 | rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 256 | bool cached_track_enabled_ = false; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 257 | |
| 258 | // Used to pass the data callback from the |track_| to the other end of |
| 259 | // cricket::AudioSource. |
| 260 | std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_; |
| 261 | }; |
| 262 | |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 263 | class VideoRtpSender : public RtpSenderBase { |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 264 | public: |
Steve Anton | 111fdfd | 2018-06-25 13:03:36 -0700 | [diff] [blame] | 265 | // Construct an RtpSender for video with the given sender ID. |
| 266 | // The sender is initialized with no track to send and no associated streams. |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 267 | static rtc::scoped_refptr<VideoRtpSender> Create(rtc::Thread* worker_thread, |
| 268 | const std::string& id); |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 269 | virtual ~VideoRtpSender(); |
| 270 | |
| 271 | // ObserverInterface implementation |
| 272 | void OnChanged() override; |
| 273 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 274 | cricket::MediaType media_type() const override { |
| 275 | return cricket::MEDIA_TYPE_VIDEO; |
| 276 | } |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 277 | std::string track_kind() const override { |
| 278 | return MediaStreamTrackInterface::kVideoKind; |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 279 | } |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 280 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 281 | rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override; |
| 282 | |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 283 | protected: |
| 284 | VideoRtpSender(rtc::Thread* worker_thread, const std::string& id); |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 285 | |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 286 | void SetSend() override; |
| 287 | void ClearSend() override; |
Benjamin Wright | d81ac95 | 2018-08-29 17:02:10 -0700 | [diff] [blame] | 288 | |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 289 | // Hook to allow custom logic when tracks are attached. |
| 290 | void AttachTrack() override; |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 291 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 292 | private: |
Amit Hilbuch | ea7ef2a | 2019-02-19 15:20:21 -0800 | [diff] [blame] | 293 | cricket::VideoMediaChannel* video_media_channel() { |
| 294 | return static_cast<cricket::VideoMediaChannel*>(media_channel_); |
| 295 | } |
| 296 | rtc::scoped_refptr<VideoTrackInterface> video_track() const { |
| 297 | return rtc::scoped_refptr<VideoTrackInterface>( |
| 298 | static_cast<VideoTrackInterface*>(track_.get())); |
| 299 | } |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 300 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 301 | VideoTrackInterface::ContentHint cached_track_content_hint_ = |
| 302 | VideoTrackInterface::ContentHint::kNone; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | } // namespace webrtc |
| 306 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 307 | #endif // PC_RTP_SENDER_H_ |