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