deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 11 | // This file contains classes that implement RtpSenderInterface. |
| 12 | // An RtpSender associates a MediaStreamTrackInterface with an underlying |
| 13 | // transport (provided by AudioProviderInterface/VideoProviderInterface) |
| 14 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 15 | #ifndef WEBRTC_API_RTPSENDER_H_ |
| 16 | #define WEBRTC_API_RTPSENDER_H_ |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 17 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 18 | #include <memory> |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 21 | #include "webrtc/api/mediastreaminterface.h" |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 22 | #include "webrtc/api/rtpsenderinterface.h" |
| 23 | #include "webrtc/api/statscollector.h" |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 24 | #include "webrtc/base/basictypes.h" |
| 25 | #include "webrtc/base/criticalsection.h" |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 26 | #include "webrtc/media/base/audiosource.h" |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 27 | #include "webrtc/pc/channel.h" |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 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 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 47 | // LocalAudioSinkAdapter receives data callback as a sink to the local |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 48 | // AudioTrack, and passes the data to the sink of AudioSource. |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 49 | class LocalAudioSinkAdapter : public AudioTrackSinkInterface, |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 50 | public cricket::AudioSource { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 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, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 60 | size_t number_of_channels, |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 61 | size_t number_of_frames) override; |
| 62 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 63 | // cricket::AudioSource implementation. |
| 64 | void SetSink(cricket::AudioSource::Sink* sink) override; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 65 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 66 | cricket::AudioSource::Sink* sink_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 67 | // Critical section protecting |sink_|. |
| 68 | rtc::CriticalSection lock_; |
| 69 | }; |
| 70 | |
| 71 | class AudioRtpSender : public ObserverInterface, |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 72 | public rtc::RefCountedObject<RtpSenderInternal> { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 73 | public: |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 74 | // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called |
| 75 | // at the appropriate times. |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 76 | // |channel| can be null if one does not exist yet. |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 77 | AudioRtpSender(AudioTrackInterface* track, |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 78 | const std::string& stream_id, |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 79 | cricket::VoiceChannel* channel, |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 80 | StatsCollector* stats); |
| 81 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 82 | // Randomly generates stream_id. |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 83 | // |channel| can be null if one does not exist yet. |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 84 | AudioRtpSender(AudioTrackInterface* track, |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 85 | cricket::VoiceChannel* channel, |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 86 | StatsCollector* stats); |
| 87 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 88 | // Randomly generates id and stream_id. |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 89 | // |channel| can be null if one does not exist yet. |
| 90 | AudioRtpSender(cricket::VoiceChannel* channel, StatsCollector* stats); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 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 { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 100 | return track_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 101 | } |
| 102 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 103 | uint32_t ssrc() const override { return ssrc_; } |
| 104 | |
| 105 | cricket::MediaType media_type() const override { |
| 106 | return cricket::MEDIA_TYPE_AUDIO; |
| 107 | } |
| 108 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 109 | std::string id() const override { return id_; } |
| 110 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 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 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 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 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 127 | void Stop() override; |
| 128 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 129 | // Does not take ownership. |
| 130 | // Should call SetChannel(nullptr) before |channel| is destroyed. |
| 131 | void SetChannel(cricket::VoiceChannel* channel) { channel_ = channel; } |
| 132 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 133 | private: |
nisse | fcc640f | 2016-04-01 01:10:42 -0700 | [diff] [blame] | 134 | // TODO(nisse): Since SSRC == 0 is technically valid, figure out |
| 135 | // some other way to test if we have a valid SSRC. |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 136 | bool can_send_track() const { return track_ && ssrc_; } |
| 137 | // Helper function to construct options for |
| 138 | // AudioProviderInterface::SetAudioSend. |
| 139 | void SetAudioSend(); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 140 | // Helper function to call SetAudioSend with "stop sending" parameters. |
| 141 | void ClearAudioSend(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 142 | |
| 143 | std::string id_; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 144 | std::string stream_id_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 145 | cricket::VoiceChannel* channel_ = nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 146 | StatsCollector* stats_; |
| 147 | rtc::scoped_refptr<AudioTrackInterface> track_; |
| 148 | uint32_t ssrc_ = 0; |
| 149 | bool cached_track_enabled_ = false; |
| 150 | bool stopped_ = false; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 151 | |
| 152 | // Used to pass the data callback from the |track_| to the other end of |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 153 | // cricket::AudioSource. |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 154 | std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | class VideoRtpSender : public ObserverInterface, |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 158 | public rtc::RefCountedObject<RtpSenderInternal> { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 159 | public: |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 160 | // |channel| can be null if one does not exist yet. |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 161 | VideoRtpSender(VideoTrackInterface* track, |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 162 | const std::string& stream_id, |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 163 | cricket::VideoChannel* channel); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 164 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 165 | // Randomly generates stream_id. |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 166 | // |channel| can be null if one does not exist yet. |
| 167 | VideoRtpSender(VideoTrackInterface* track, cricket::VideoChannel* channel); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 168 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 169 | // Randomly generates id and stream_id. |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 170 | // |channel| can be null if one does not exist yet. |
| 171 | explicit VideoRtpSender(cricket::VideoChannel* channel); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 172 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 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 { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 181 | return track_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 182 | } |
| 183 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 184 | uint32_t ssrc() const override { return ssrc_; } |
| 185 | |
| 186 | cricket::MediaType media_type() const override { |
| 187 | return cricket::MEDIA_TYPE_VIDEO; |
| 188 | } |
| 189 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 190 | std::string id() const override { return id_; } |
| 191 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 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 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 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 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 208 | void Stop() override; |
| 209 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 210 | // Does not take ownership. |
| 211 | // Should call SetChannel(nullptr) before |channel| is destroyed. |
| 212 | void SetChannel(cricket::VideoChannel* channel) { channel_ = channel; } |
| 213 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 214 | private: |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 215 | bool can_send_track() const { return track_ && ssrc_; } |
| 216 | // Helper function to construct options for |
| 217 | // VideoProviderInterface::SetVideoSend. |
| 218 | void SetVideoSend(); |
deadbeef | 5a4a75a | 2016-06-02 16:23:38 -0700 | [diff] [blame] | 219 | // Helper function to call SetVideoSend with "stop sending" parameters. |
| 220 | void ClearVideoSend(); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 221 | |
| 222 | std::string id_; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 223 | std::string stream_id_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 224 | cricket::VideoChannel* channel_ = nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 225 | rtc::scoped_refptr<VideoTrackInterface> track_; |
| 226 | uint32_t ssrc_ = 0; |
| 227 | bool cached_track_enabled_ = false; |
pbos | 5214a0a | 2016-12-16 15:39:11 -0800 | [diff] [blame] | 228 | VideoTrackInterface::ContentHint cached_track_content_hint_ = |
| 229 | VideoTrackInterface::ContentHint::kNone; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 230 | bool stopped_ = false; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | } // namespace webrtc |
| 234 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 235 | #endif // WEBRTC_API_RTPSENDER_H_ |