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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #ifndef PC_RTPSENDER_H_ |
| 16 | #define PC_RTPSENDER_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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "api/mediastreaminterface.h" |
| 23 | #include "api/rtpsenderinterface.h" |
| 24 | #include "rtc_base/basictypes.h" |
| 25 | #include "rtc_base/criticalsection.h" |
Niels Möller | 6daa278 | 2018-01-23 10:37:42 +0100 | [diff] [blame] | 26 | #include "media/base/audiosource.h" |
| 27 | #include "media/base/mediachannel.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "pc/dtmfsender.h" |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
| 31 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 32 | class StatsCollector; |
| 33 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 34 | // Internal interface used by PeerConnection. |
| 35 | class RtpSenderInternal : public RtpSenderInterface { |
| 36 | public: |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 37 | // Sets the underlying MediaEngine channel associated with this RtpSender. |
| 38 | // SetVoiceMediaChannel should be used for audio RtpSenders and |
| 39 | // SetVideoMediaChannel should be used for video RtpSenders. Must call the |
| 40 | // appropriate SetXxxMediaChannel(nullptr) before the media channel is |
| 41 | // destroyed. |
| 42 | virtual void SetVoiceMediaChannel( |
| 43 | cricket::VoiceMediaChannel* voice_media_channel) = 0; |
| 44 | virtual void SetVideoMediaChannel( |
| 45 | cricket::VideoMediaChannel* video_media_channel) = 0; |
| 46 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 47 | // Used to set the SSRC of the sender, once a local description has been set. |
| 48 | // If |ssrc| is 0, this indiates that the sender should disconnect from the |
| 49 | // underlying transport (this occurs if the sender isn't seen in a local |
| 50 | // description). |
| 51 | virtual void SetSsrc(uint32_t ssrc) = 0; |
| 52 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 53 | virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 54 | |
| 55 | virtual void Stop() = 0; |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 56 | |
| 57 | // Returns an ID that changes every time SetTrack() is called, but |
| 58 | // otherwise remains constant. Used to generate IDs for stats. |
| 59 | // The special value zero means that no track is attached. |
| 60 | virtual int AttachmentId() const = 0; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | // LocalAudioSinkAdapter receives data callback as a sink to the local |
| 64 | // AudioTrack, and passes the data to the sink of AudioSource. |
| 65 | class LocalAudioSinkAdapter : public AudioTrackSinkInterface, |
| 66 | public cricket::AudioSource { |
| 67 | public: |
| 68 | LocalAudioSinkAdapter(); |
| 69 | virtual ~LocalAudioSinkAdapter(); |
| 70 | |
| 71 | private: |
| 72 | // AudioSinkInterface implementation. |
| 73 | void OnData(const void* audio_data, |
| 74 | int bits_per_sample, |
| 75 | int sample_rate, |
| 76 | size_t number_of_channels, |
| 77 | size_t number_of_frames) override; |
| 78 | |
| 79 | // cricket::AudioSource implementation. |
| 80 | void SetSink(cricket::AudioSource::Sink* sink) override; |
| 81 | |
| 82 | cricket::AudioSource::Sink* sink_; |
| 83 | // Critical section protecting |sink_|. |
| 84 | rtc::CriticalSection lock_; |
| 85 | }; |
| 86 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 87 | class AudioRtpSender : public DtmfProviderInterface, |
| 88 | public ObserverInterface, |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 89 | public rtc::RefCountedObject<RtpSenderInternal> { |
| 90 | public: |
| 91 | // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called |
| 92 | // at the appropriate times. |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 93 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 94 | // Construct an AudioRtpSender with a null track, a single, randomly generated |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 95 | // stream id, and a randomly generated ID. |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 96 | AudioRtpSender(rtc::Thread* worker_thread, StatsCollector* stats); |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 97 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 98 | // Construct an AudioRtpSender with the given track and stream ids. The |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 99 | // sender ID will be set to the track's ID. |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 100 | AudioRtpSender(rtc::Thread* worker_thread, |
| 101 | rtc::scoped_refptr<AudioTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 102 | const std::vector<std::string>& stream_ids, |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 103 | StatsCollector* stats); |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 104 | |
| 105 | virtual ~AudioRtpSender(); |
| 106 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 107 | // DtmfSenderProvider implementation. |
| 108 | bool CanInsertDtmf() override; |
| 109 | bool InsertDtmf(int code, int duration) override; |
| 110 | sigslot::signal0<>* GetOnDestroyedSignal() override; |
| 111 | |
| 112 | // ObserverInterface implementation. |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 113 | void OnChanged() override; |
| 114 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 115 | // RtpSenderInterface implementation. |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 116 | bool SetTrack(MediaStreamTrackInterface* track) override; |
| 117 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 118 | return track_; |
| 119 | } |
| 120 | |
| 121 | uint32_t ssrc() const override { return ssrc_; } |
| 122 | |
| 123 | cricket::MediaType media_type() const override { |
| 124 | return cricket::MEDIA_TYPE_AUDIO; |
| 125 | } |
| 126 | |
| 127 | std::string id() const override { return id_; } |
| 128 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 129 | std::vector<std::string> stream_ids() const override { return stream_ids_; } |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 130 | |
| 131 | RtpParameters GetParameters() const override; |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 132 | RTCError SetParameters(const RtpParameters& parameters) override; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 133 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 134 | rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override; |
| 135 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 136 | // RtpSenderInternal implementation. |
| 137 | void SetSsrc(uint32_t ssrc) override; |
| 138 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 139 | void set_stream_ids(const std::vector<std::string>& stream_ids) override { |
| 140 | stream_ids_ = stream_ids; |
| 141 | } |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 142 | |
| 143 | void Stop() override; |
| 144 | |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 145 | int AttachmentId() const override { return attachment_id_; } |
| 146 | |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 147 | void SetVoiceMediaChannel( |
| 148 | cricket::VoiceMediaChannel* voice_media_channel) override { |
| 149 | media_channel_ = voice_media_channel; |
| 150 | } |
| 151 | void SetVideoMediaChannel( |
| 152 | cricket::VideoMediaChannel* video_media_channel) override { |
| 153 | RTC_NOTREACHED(); |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 154 | } |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 155 | |
| 156 | private: |
| 157 | // TODO(nisse): Since SSRC == 0 is technically valid, figure out |
| 158 | // some other way to test if we have a valid SSRC. |
| 159 | bool can_send_track() const { return track_ && ssrc_; } |
| 160 | // Helper function to construct options for |
| 161 | // AudioProviderInterface::SetAudioSend. |
| 162 | void SetAudioSend(); |
| 163 | // Helper function to call SetAudioSend with "stop sending" parameters. |
| 164 | void ClearAudioSend(); |
| 165 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 166 | sigslot::signal0<> SignalDestroyed; |
| 167 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 168 | rtc::Thread* const worker_thread_; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 169 | const std::string id_; |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 170 | std::vector<std::string> stream_ids_; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 171 | cricket::VoiceMediaChannel* media_channel_ = nullptr; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 172 | StatsCollector* stats_; |
| 173 | rtc::scoped_refptr<AudioTrackInterface> track_; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 174 | rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 175 | uint32_t ssrc_ = 0; |
| 176 | bool cached_track_enabled_ = false; |
| 177 | bool stopped_ = false; |
| 178 | |
| 179 | // Used to pass the data callback from the |track_| to the other end of |
| 180 | // cricket::AudioSource. |
| 181 | std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_; |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 182 | int attachment_id_ = 0; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | class VideoRtpSender : public ObserverInterface, |
| 186 | public rtc::RefCountedObject<RtpSenderInternal> { |
| 187 | public: |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 188 | // Construct a VideoRtpSender with a null track, a single, randomly generated |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 189 | // stream id, and a randomly generated ID. |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 190 | explicit VideoRtpSender(rtc::Thread* worker_thread); |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 191 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 192 | // Construct a VideoRtpSender with the given track and stream ids. The |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 193 | // sender ID will be set to the track's ID. |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 194 | VideoRtpSender(rtc::Thread* worker_thread, |
| 195 | rtc::scoped_refptr<VideoTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 196 | const std::vector<std::string>& stream_ids); |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 197 | |
| 198 | virtual ~VideoRtpSender(); |
| 199 | |
| 200 | // ObserverInterface implementation |
| 201 | void OnChanged() override; |
| 202 | |
| 203 | // RtpSenderInterface implementation |
| 204 | bool SetTrack(MediaStreamTrackInterface* track) override; |
| 205 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 206 | return track_; |
| 207 | } |
| 208 | |
| 209 | uint32_t ssrc() const override { return ssrc_; } |
| 210 | |
| 211 | cricket::MediaType media_type() const override { |
| 212 | return cricket::MEDIA_TYPE_VIDEO; |
| 213 | } |
| 214 | |
| 215 | std::string id() const override { return id_; } |
| 216 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 217 | std::vector<std::string> stream_ids() const override { return stream_ids_; } |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 218 | |
| 219 | RtpParameters GetParameters() const override; |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 220 | RTCError SetParameters(const RtpParameters& parameters) override; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 221 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 222 | rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override; |
| 223 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 224 | // RtpSenderInternal implementation. |
| 225 | void SetSsrc(uint32_t ssrc) override; |
| 226 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 227 | void set_stream_ids(const std::vector<std::string>& stream_ids) override { |
| 228 | stream_ids_ = stream_ids; |
| 229 | } |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 230 | |
| 231 | void Stop() override; |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 232 | int AttachmentId() const override { return attachment_id_; } |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 233 | |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 234 | void SetVoiceMediaChannel( |
| 235 | cricket::VoiceMediaChannel* voice_media_channel) override { |
| 236 | RTC_NOTREACHED(); |
| 237 | } |
| 238 | void SetVideoMediaChannel( |
| 239 | cricket::VideoMediaChannel* video_media_channel) override { |
| 240 | media_channel_ = video_media_channel; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 241 | } |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 242 | |
| 243 | private: |
| 244 | bool can_send_track() const { return track_ && ssrc_; } |
| 245 | // Helper function to construct options for |
| 246 | // VideoProviderInterface::SetVideoSend. |
| 247 | void SetVideoSend(); |
| 248 | // Helper function to call SetVideoSend with "stop sending" parameters. |
| 249 | void ClearVideoSend(); |
| 250 | |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 251 | rtc::Thread* worker_thread_; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 252 | const std::string id_; |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 253 | std::vector<std::string> stream_ids_; |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 254 | cricket::VideoMediaChannel* media_channel_ = nullptr; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 255 | rtc::scoped_refptr<VideoTrackInterface> track_; |
| 256 | uint32_t ssrc_ = 0; |
| 257 | bool cached_track_enabled_ = false; |
| 258 | VideoTrackInterface::ContentHint cached_track_content_hint_ = |
| 259 | VideoTrackInterface::ContentHint::kNone; |
| 260 | bool stopped_ = false; |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 261 | int attachment_id_ = 0; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | } // namespace webrtc |
| 265 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 266 | #endif // PC_RTPSENDER_H_ |