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