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 | |
| 15 | #ifndef WEBRTC_PC_RTPSENDER_H_ |
| 16 | #define WEBRTC_PC_RTPSENDER_H_ |
| 17 | |
| 18 | #include <memory> |
| 19 | #include <string> |
| 20 | |
| 21 | #include "webrtc/api/mediastreaminterface.h" |
| 22 | #include "webrtc/api/rtpsenderinterface.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 23 | #include "webrtc/rtc_base/basictypes.h" |
| 24 | #include "webrtc/rtc_base/criticalsection.h" |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 25 | // Adding 'nogncheck' to disable the gn include headers check to support modular |
| 26 | // WebRTC build targets. |
| 27 | #include "webrtc/media/base/audiosource.h" // nogncheck |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 28 | #include "webrtc/pc/channel.h" |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 29 | #include "webrtc/pc/dtmfsender.h" |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 30 | #include "webrtc/pc/statscollector.h" |
| 31 | |
| 32 | namespace webrtc { |
| 33 | |
| 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 | |
| 43 | // TODO(deadbeef): Support one sender having multiple stream ids. |
| 44 | virtual void set_stream_id(const std::string& stream_id) = 0; |
| 45 | virtual std::string stream_id() const = 0; |
| 46 | |
| 47 | virtual void Stop() = 0; |
| 48 | }; |
| 49 | |
| 50 | // LocalAudioSinkAdapter receives data callback as a sink to the local |
| 51 | // AudioTrack, and passes the data to the sink of AudioSource. |
| 52 | class LocalAudioSinkAdapter : public AudioTrackSinkInterface, |
| 53 | public cricket::AudioSource { |
| 54 | public: |
| 55 | LocalAudioSinkAdapter(); |
| 56 | virtual ~LocalAudioSinkAdapter(); |
| 57 | |
| 58 | private: |
| 59 | // AudioSinkInterface implementation. |
| 60 | void OnData(const void* audio_data, |
| 61 | int bits_per_sample, |
| 62 | int sample_rate, |
| 63 | size_t number_of_channels, |
| 64 | size_t number_of_frames) override; |
| 65 | |
| 66 | // cricket::AudioSource implementation. |
| 67 | void SetSink(cricket::AudioSource::Sink* sink) override; |
| 68 | |
| 69 | cricket::AudioSource::Sink* sink_; |
| 70 | // Critical section protecting |sink_|. |
| 71 | rtc::CriticalSection lock_; |
| 72 | }; |
| 73 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 74 | class AudioRtpSender : public DtmfProviderInterface, |
| 75 | public ObserverInterface, |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 76 | public rtc::RefCountedObject<RtpSenderInternal> { |
| 77 | public: |
| 78 | // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called |
| 79 | // at the appropriate times. |
| 80 | // |channel| can be null if one does not exist yet. |
| 81 | AudioRtpSender(AudioTrackInterface* track, |
| 82 | const std::string& stream_id, |
| 83 | cricket::VoiceChannel* channel, |
| 84 | StatsCollector* stats); |
| 85 | |
| 86 | // Randomly generates stream_id. |
| 87 | // |channel| can be null if one does not exist yet. |
| 88 | AudioRtpSender(AudioTrackInterface* track, |
| 89 | cricket::VoiceChannel* channel, |
| 90 | StatsCollector* stats); |
| 91 | |
| 92 | // Randomly generates id and stream_id. |
| 93 | // |channel| can be null if one does not exist yet. |
| 94 | AudioRtpSender(cricket::VoiceChannel* channel, StatsCollector* stats); |
| 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 | |
| 120 | std::vector<std::string> stream_ids() const override { |
| 121 | std::vector<std::string> ret = {stream_id_}; |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | RtpParameters GetParameters() const override; |
| 126 | bool SetParameters(const RtpParameters& parameters) override; |
| 127 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 128 | rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override; |
| 129 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 130 | // RtpSenderInternal implementation. |
| 131 | void SetSsrc(uint32_t ssrc) override; |
| 132 | |
| 133 | void set_stream_id(const std::string& stream_id) override { |
| 134 | stream_id_ = stream_id; |
| 135 | } |
| 136 | std::string stream_id() const override { return stream_id_; } |
| 137 | |
| 138 | void Stop() override; |
| 139 | |
| 140 | // Does not take ownership. |
| 141 | // Should call SetChannel(nullptr) before |channel| is destroyed. |
| 142 | void SetChannel(cricket::VoiceChannel* channel) { channel_ = channel; } |
| 143 | |
| 144 | private: |
| 145 | // TODO(nisse): Since SSRC == 0 is technically valid, figure out |
| 146 | // some other way to test if we have a valid SSRC. |
| 147 | bool can_send_track() const { return track_ && ssrc_; } |
| 148 | // Helper function to construct options for |
| 149 | // AudioProviderInterface::SetAudioSend. |
| 150 | void SetAudioSend(); |
| 151 | // Helper function to call SetAudioSend with "stop sending" parameters. |
| 152 | void ClearAudioSend(); |
| 153 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 154 | void CreateDtmfSender(); |
| 155 | |
| 156 | sigslot::signal0<> SignalDestroyed; |
| 157 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 158 | std::string id_; |
| 159 | std::string stream_id_; |
| 160 | cricket::VoiceChannel* channel_ = nullptr; |
| 161 | StatsCollector* stats_; |
| 162 | rtc::scoped_refptr<AudioTrackInterface> track_; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 163 | rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 164 | uint32_t ssrc_ = 0; |
| 165 | bool cached_track_enabled_ = false; |
| 166 | bool stopped_ = false; |
| 167 | |
| 168 | // Used to pass the data callback from the |track_| to the other end of |
| 169 | // cricket::AudioSource. |
| 170 | std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_; |
| 171 | }; |
| 172 | |
| 173 | class VideoRtpSender : public ObserverInterface, |
| 174 | public rtc::RefCountedObject<RtpSenderInternal> { |
| 175 | public: |
| 176 | // |channel| can be null if one does not exist yet. |
| 177 | VideoRtpSender(VideoTrackInterface* track, |
| 178 | const std::string& stream_id, |
| 179 | cricket::VideoChannel* channel); |
| 180 | |
| 181 | // Randomly generates stream_id. |
| 182 | // |channel| can be null if one does not exist yet. |
| 183 | VideoRtpSender(VideoTrackInterface* track, cricket::VideoChannel* channel); |
| 184 | |
| 185 | // Randomly generates id and stream_id. |
| 186 | // |channel| can be null if one does not exist yet. |
| 187 | explicit VideoRtpSender(cricket::VideoChannel* channel); |
| 188 | |
| 189 | virtual ~VideoRtpSender(); |
| 190 | |
| 191 | // ObserverInterface implementation |
| 192 | void OnChanged() override; |
| 193 | |
| 194 | // RtpSenderInterface implementation |
| 195 | bool SetTrack(MediaStreamTrackInterface* track) override; |
| 196 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 197 | return track_; |
| 198 | } |
| 199 | |
| 200 | uint32_t ssrc() const override { return ssrc_; } |
| 201 | |
| 202 | cricket::MediaType media_type() const override { |
| 203 | return cricket::MEDIA_TYPE_VIDEO; |
| 204 | } |
| 205 | |
| 206 | std::string id() const override { return id_; } |
| 207 | |
| 208 | std::vector<std::string> stream_ids() const override { |
| 209 | std::vector<std::string> ret = {stream_id_}; |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | RtpParameters GetParameters() const override; |
| 214 | bool SetParameters(const RtpParameters& parameters) override; |
| 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 { |
| 222 | stream_id_ = stream_id; |
| 223 | } |
| 224 | std::string stream_id() const override { return stream_id_; } |
| 225 | |
| 226 | void Stop() override; |
| 227 | |
| 228 | // Does not take ownership. |
| 229 | // Should call SetChannel(nullptr) before |channel| is destroyed. |
| 230 | void SetChannel(cricket::VideoChannel* channel) { channel_ = channel; } |
| 231 | |
| 232 | private: |
| 233 | bool can_send_track() const { return track_ && ssrc_; } |
| 234 | // Helper function to construct options for |
| 235 | // VideoProviderInterface::SetVideoSend. |
| 236 | void SetVideoSend(); |
| 237 | // Helper function to call SetVideoSend with "stop sending" parameters. |
| 238 | void ClearVideoSend(); |
| 239 | |
| 240 | std::string id_; |
| 241 | std::string stream_id_; |
| 242 | cricket::VideoChannel* channel_ = nullptr; |
| 243 | rtc::scoped_refptr<VideoTrackInterface> track_; |
| 244 | uint32_t ssrc_ = 0; |
| 245 | bool cached_track_enabled_ = false; |
| 246 | VideoTrackInterface::ContentHint cached_track_content_hint_ = |
| 247 | VideoTrackInterface::ContentHint::kNone; |
| 248 | bool stopped_ = false; |
| 249 | }; |
| 250 | |
| 251 | } // namespace webrtc |
| 252 | |
| 253 | #endif // WEBRTC_PC_RTPSENDER_H_ |