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