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" |
| 23 | #include "webrtc/base/basictypes.h" |
| 24 | #include "webrtc/base/criticalsection.h" |
| 25 | #include "webrtc/media/base/audiosource.h" |
| 26 | #include "webrtc/pc/channel.h" |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame^] | 27 | #include "webrtc/pc/dtmfsender.h" |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 28 | #include "webrtc/pc/statscollector.h" |
| 29 | |
| 30 | namespace webrtc { |
| 31 | |
| 32 | // Internal interface used by PeerConnection. |
| 33 | class RtpSenderInternal : public RtpSenderInterface { |
| 34 | public: |
| 35 | // Used to set the SSRC of the sender, once a local description has been set. |
| 36 | // If |ssrc| is 0, this indiates that the sender should disconnect from the |
| 37 | // underlying transport (this occurs if the sender isn't seen in a local |
| 38 | // description). |
| 39 | virtual void SetSsrc(uint32_t ssrc) = 0; |
| 40 | |
| 41 | // TODO(deadbeef): Support one sender having multiple stream ids. |
| 42 | virtual void set_stream_id(const std::string& stream_id) = 0; |
| 43 | virtual std::string stream_id() const = 0; |
| 44 | |
| 45 | virtual void Stop() = 0; |
| 46 | }; |
| 47 | |
| 48 | // LocalAudioSinkAdapter receives data callback as a sink to the local |
| 49 | // AudioTrack, and passes the data to the sink of AudioSource. |
| 50 | class LocalAudioSinkAdapter : public AudioTrackSinkInterface, |
| 51 | public cricket::AudioSource { |
| 52 | public: |
| 53 | LocalAudioSinkAdapter(); |
| 54 | virtual ~LocalAudioSinkAdapter(); |
| 55 | |
| 56 | private: |
| 57 | // AudioSinkInterface implementation. |
| 58 | void OnData(const void* audio_data, |
| 59 | int bits_per_sample, |
| 60 | int sample_rate, |
| 61 | size_t number_of_channels, |
| 62 | size_t number_of_frames) override; |
| 63 | |
| 64 | // cricket::AudioSource implementation. |
| 65 | void SetSink(cricket::AudioSource::Sink* sink) override; |
| 66 | |
| 67 | cricket::AudioSource::Sink* sink_; |
| 68 | // Critical section protecting |sink_|. |
| 69 | rtc::CriticalSection lock_; |
| 70 | }; |
| 71 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame^] | 72 | class AudioRtpSender : public DtmfProviderInterface, |
| 73 | public ObserverInterface, |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 74 | public rtc::RefCountedObject<RtpSenderInternal> { |
| 75 | public: |
| 76 | // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called |
| 77 | // at the appropriate times. |
| 78 | // |channel| can be null if one does not exist yet. |
| 79 | AudioRtpSender(AudioTrackInterface* track, |
| 80 | const std::string& stream_id, |
| 81 | cricket::VoiceChannel* channel, |
| 82 | StatsCollector* stats); |
| 83 | |
| 84 | // Randomly generates stream_id. |
| 85 | // |channel| can be null if one does not exist yet. |
| 86 | AudioRtpSender(AudioTrackInterface* track, |
| 87 | cricket::VoiceChannel* channel, |
| 88 | StatsCollector* stats); |
| 89 | |
| 90 | // Randomly generates id and stream_id. |
| 91 | // |channel| can be null if one does not exist yet. |
| 92 | AudioRtpSender(cricket::VoiceChannel* channel, StatsCollector* stats); |
| 93 | |
| 94 | virtual ~AudioRtpSender(); |
| 95 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame^] | 96 | // DtmfSenderProvider implementation. |
| 97 | bool CanInsertDtmf() override; |
| 98 | bool InsertDtmf(int code, int duration) override; |
| 99 | sigslot::signal0<>* GetOnDestroyedSignal() override; |
| 100 | |
| 101 | // ObserverInterface implementation. |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 102 | void OnChanged() override; |
| 103 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame^] | 104 | // RtpSenderInterface implementation. |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 105 | bool SetTrack(MediaStreamTrackInterface* track) override; |
| 106 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 107 | return track_; |
| 108 | } |
| 109 | |
| 110 | uint32_t ssrc() const override { return ssrc_; } |
| 111 | |
| 112 | cricket::MediaType media_type() const override { |
| 113 | return cricket::MEDIA_TYPE_AUDIO; |
| 114 | } |
| 115 | |
| 116 | std::string id() const override { return id_; } |
| 117 | |
| 118 | std::vector<std::string> stream_ids() const override { |
| 119 | std::vector<std::string> ret = {stream_id_}; |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | RtpParameters GetParameters() const override; |
| 124 | bool SetParameters(const RtpParameters& parameters) override; |
| 125 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame^] | 126 | rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override; |
| 127 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 128 | // RtpSenderInternal implementation. |
| 129 | void SetSsrc(uint32_t ssrc) override; |
| 130 | |
| 131 | void set_stream_id(const std::string& stream_id) override { |
| 132 | stream_id_ = stream_id; |
| 133 | } |
| 134 | std::string stream_id() const override { return stream_id_; } |
| 135 | |
| 136 | void Stop() override; |
| 137 | |
| 138 | // Does not take ownership. |
| 139 | // Should call SetChannel(nullptr) before |channel| is destroyed. |
| 140 | void SetChannel(cricket::VoiceChannel* channel) { channel_ = channel; } |
| 141 | |
| 142 | private: |
| 143 | // TODO(nisse): Since SSRC == 0 is technically valid, figure out |
| 144 | // some other way to test if we have a valid SSRC. |
| 145 | bool can_send_track() const { return track_ && ssrc_; } |
| 146 | // Helper function to construct options for |
| 147 | // AudioProviderInterface::SetAudioSend. |
| 148 | void SetAudioSend(); |
| 149 | // Helper function to call SetAudioSend with "stop sending" parameters. |
| 150 | void ClearAudioSend(); |
| 151 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame^] | 152 | void CreateDtmfSender(); |
| 153 | |
| 154 | sigslot::signal0<> SignalDestroyed; |
| 155 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 156 | std::string id_; |
| 157 | std::string stream_id_; |
| 158 | cricket::VoiceChannel* channel_ = nullptr; |
| 159 | StatsCollector* stats_; |
| 160 | rtc::scoped_refptr<AudioTrackInterface> track_; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame^] | 161 | rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_; |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 162 | uint32_t ssrc_ = 0; |
| 163 | bool cached_track_enabled_ = false; |
| 164 | bool stopped_ = false; |
| 165 | |
| 166 | // Used to pass the data callback from the |track_| to the other end of |
| 167 | // cricket::AudioSource. |
| 168 | std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_; |
| 169 | }; |
| 170 | |
| 171 | class VideoRtpSender : public ObserverInterface, |
| 172 | public rtc::RefCountedObject<RtpSenderInternal> { |
| 173 | public: |
| 174 | // |channel| can be null if one does not exist yet. |
| 175 | VideoRtpSender(VideoTrackInterface* track, |
| 176 | const std::string& stream_id, |
| 177 | cricket::VideoChannel* channel); |
| 178 | |
| 179 | // Randomly generates stream_id. |
| 180 | // |channel| can be null if one does not exist yet. |
| 181 | VideoRtpSender(VideoTrackInterface* track, cricket::VideoChannel* channel); |
| 182 | |
| 183 | // Randomly generates id and stream_id. |
| 184 | // |channel| can be null if one does not exist yet. |
| 185 | explicit VideoRtpSender(cricket::VideoChannel* channel); |
| 186 | |
| 187 | virtual ~VideoRtpSender(); |
| 188 | |
| 189 | // ObserverInterface implementation |
| 190 | void OnChanged() override; |
| 191 | |
| 192 | // RtpSenderInterface implementation |
| 193 | bool SetTrack(MediaStreamTrackInterface* track) override; |
| 194 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 195 | return track_; |
| 196 | } |
| 197 | |
| 198 | uint32_t ssrc() const override { return ssrc_; } |
| 199 | |
| 200 | cricket::MediaType media_type() const override { |
| 201 | return cricket::MEDIA_TYPE_VIDEO; |
| 202 | } |
| 203 | |
| 204 | std::string id() const override { return id_; } |
| 205 | |
| 206 | std::vector<std::string> stream_ids() const override { |
| 207 | std::vector<std::string> ret = {stream_id_}; |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | RtpParameters GetParameters() const override; |
| 212 | bool SetParameters(const RtpParameters& parameters) override; |
| 213 | |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame^] | 214 | rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override; |
| 215 | |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 216 | // RtpSenderInternal implementation. |
| 217 | void SetSsrc(uint32_t ssrc) override; |
| 218 | |
| 219 | void set_stream_id(const std::string& stream_id) override { |
| 220 | stream_id_ = stream_id; |
| 221 | } |
| 222 | std::string stream_id() const override { return stream_id_; } |
| 223 | |
| 224 | void Stop() override; |
| 225 | |
| 226 | // Does not take ownership. |
| 227 | // Should call SetChannel(nullptr) before |channel| is destroyed. |
| 228 | void SetChannel(cricket::VideoChannel* channel) { channel_ = channel; } |
| 229 | |
| 230 | private: |
| 231 | bool can_send_track() const { return track_ && ssrc_; } |
| 232 | // Helper function to construct options for |
| 233 | // VideoProviderInterface::SetVideoSend. |
| 234 | void SetVideoSend(); |
| 235 | // Helper function to call SetVideoSend with "stop sending" parameters. |
| 236 | void ClearVideoSend(); |
| 237 | |
| 238 | std::string id_; |
| 239 | std::string stream_id_; |
| 240 | cricket::VideoChannel* channel_ = nullptr; |
| 241 | rtc::scoped_refptr<VideoTrackInterface> track_; |
| 242 | uint32_t ssrc_ = 0; |
| 243 | bool cached_track_enabled_ = false; |
| 244 | VideoTrackInterface::ContentHint cached_track_content_hint_ = |
| 245 | VideoTrackInterface::ContentHint::kNone; |
| 246 | bool stopped_ = false; |
| 247 | }; |
| 248 | |
| 249 | } // namespace webrtc |
| 250 | |
| 251 | #endif // WEBRTC_PC_RTPSENDER_H_ |