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