blob: 07fae6b9547c494a23ff3d758e2e3a62aeffc8d0 [file] [log] [blame]
ossu7bb87ee2017-01-23 04:56:25 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020015#ifndef PC_RTPSENDER_H_
16#define PC_RTPSENDER_H_
ossu7bb87ee2017-01-23 04:56:25 -080017
18#include <memory>
19#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070020#include <vector>
ossu7bb87ee2017-01-23 04:56:25 -080021
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/mediastreaminterface.h"
23#include "api/rtpsenderinterface.h"
Niels Möller6daa2782018-01-23 10:37:42 +010024#include "media/base/audiosource.h"
25#include "media/base/mediachannel.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "pc/dtmfsender.h"
Yves Gerey665174f2018-06-19 15:03:05 +020027#include "rtc_base/criticalsection.h"
ossu7bb87ee2017-01-23 04:56:25 -080028
29namespace webrtc {
30
Steve Anton2d8609c2018-01-23 16:38:46 -080031class StatsCollector;
32
Florent Castelli892acf02018-10-01 22:47:20 +020033bool UnimplementedRtpParameterHasValue(const RtpParameters& parameters);
34
ossu7bb87ee2017-01-23 04:56:25 -080035// Internal interface used by PeerConnection.
36class RtpSenderInternal : public RtpSenderInterface {
37 public:
Steve Anton57858b32018-02-15 15:19:50 -080038 // Sets the underlying MediaEngine channel associated with this RtpSender.
Amit Hilbuchdd9390c2018-11-13 16:26:05 -080039 // A VoiceMediaChannel should be used for audio RtpSenders and
40 // a VideoMediaChannel should be used for video RtpSenders.
41 // Must call SetMediaChannel(nullptr) before the media channel is destroyed.
42 virtual void SetMediaChannel(cricket::MediaChannel* media_channel) = 0;
Steve Anton57858b32018-02-15 15:19:50 -080043
ossu7bb87ee2017-01-23 04:56:25 -080044 // Used to set the SSRC of the sender, once a local description has been set.
45 // If |ssrc| is 0, this indiates that the sender should disconnect from the
46 // underlying transport (this occurs if the sender isn't seen in a local
47 // description).
48 virtual void SetSsrc(uint32_t ssrc) = 0;
49
Steve Anton8ffb9c32017-08-31 15:45:38 -070050 virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
Florent Castelli892acf02018-10-01 22:47:20 +020051 virtual void set_init_send_encodings(
52 const std::vector<RtpEncodingParameters>& init_send_encodings) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080053
54 virtual void Stop() = 0;
Steve Anton57858b32018-02-15 15:19:50 -080055
56 // Returns an ID that changes every time SetTrack() is called, but
57 // otherwise remains constant. Used to generate IDs for stats.
58 // The special value zero means that no track is attached.
59 virtual int AttachmentId() const = 0;
ossu7bb87ee2017-01-23 04:56:25 -080060};
61
62// LocalAudioSinkAdapter receives data callback as a sink to the local
63// AudioTrack, and passes the data to the sink of AudioSource.
64class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
65 public cricket::AudioSource {
66 public:
67 LocalAudioSinkAdapter();
68 virtual ~LocalAudioSinkAdapter();
69
70 private:
71 // AudioSinkInterface implementation.
72 void OnData(const void* audio_data,
73 int bits_per_sample,
74 int sample_rate,
75 size_t number_of_channels,
76 size_t number_of_frames) override;
77
78 // cricket::AudioSource implementation.
79 void SetSink(cricket::AudioSource::Sink* sink) override;
80
81 cricket::AudioSource::Sink* sink_;
82 // Critical section protecting |sink_|.
83 rtc::CriticalSection lock_;
84};
85
deadbeef20cb0c12017-02-01 20:27:00 -080086class AudioRtpSender : public DtmfProviderInterface,
87 public ObserverInterface,
ossu7bb87ee2017-01-23 04:56:25 -080088 public rtc::RefCountedObject<RtpSenderInternal> {
89 public:
90 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
91 // at the appropriate times.
ossu7bb87ee2017-01-23 04:56:25 -080092
Steve Anton111fdfd2018-06-25 13:03:36 -070093 // Construct an RtpSender for audio with the given sender ID.
94 // The sender is initialized with no track to send and no associated streams.
Steve Anton47136dd2018-01-12 10:49:35 -080095 AudioRtpSender(rtc::Thread* worker_thread,
Steve Anton111fdfd2018-06-25 13:03:36 -070096 const std::string& id,
Steve Anton02ee47c2018-01-10 16:26:06 -080097 StatsCollector* stats);
ossu7bb87ee2017-01-23 04:56:25 -080098
99 virtual ~AudioRtpSender();
100
deadbeef20cb0c12017-02-01 20:27:00 -0800101 // DtmfSenderProvider implementation.
102 bool CanInsertDtmf() override;
103 bool InsertDtmf(int code, int duration) override;
104 sigslot::signal0<>* GetOnDestroyedSignal() override;
105
106 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800107 void OnChanged() override;
108
deadbeef20cb0c12017-02-01 20:27:00 -0800109 // RtpSenderInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800110 bool SetTrack(MediaStreamTrackInterface* track) override;
111 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
112 return track_;
113 }
114
115 uint32_t ssrc() const override { return ssrc_; }
116
117 cricket::MediaType media_type() const override {
118 return cricket::MEDIA_TYPE_AUDIO;
119 }
120
121 std::string id() const override { return id_; }
122
Steve Anton8ffb9c32017-08-31 15:45:38 -0700123 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800124
Florent Castellicebf50f2018-05-03 15:31:53 +0200125 RtpParameters GetParameters() override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800126 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 04:56:25 -0800127
deadbeef20cb0c12017-02-01 20:27:00 -0800128 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
129
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700130 void SetFrameEncryptor(
131 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
132
133 rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor()
134 const override;
135
ossu7bb87ee2017-01-23 04:56:25 -0800136 // RtpSenderInternal implementation.
137 void SetSsrc(uint32_t ssrc) override;
138
Steve Anton8ffb9c32017-08-31 15:45:38 -0700139 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
140 stream_ids_ = stream_ids;
141 }
ossu7bb87ee2017-01-23 04:56:25 -0800142
Florent Castelli892acf02018-10-01 22:47:20 +0200143 void set_init_send_encodings(
144 const std::vector<RtpEncodingParameters>& init_send_encodings) override {
145 init_parameters_.encodings = init_send_encodings;
146 }
147 std::vector<RtpEncodingParameters> init_send_encodings() const override {
148 return init_parameters_.encodings;
149 }
150
ossu7bb87ee2017-01-23 04:56:25 -0800151 void Stop() override;
152
Harald Alvestrandc72af932018-01-11 17:18:19 +0100153 int AttachmentId() const override { return attachment_id_; }
154
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800155 void SetMediaChannel(cricket::MediaChannel* media_channel) override;
ossu7bb87ee2017-01-23 04:56:25 -0800156
157 private:
158 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
159 // some other way to test if we have a valid SSRC.
160 bool can_send_track() const { return track_ && ssrc_; }
161 // Helper function to construct options for
162 // AudioProviderInterface::SetAudioSend.
163 void SetAudioSend();
164 // Helper function to call SetAudioSend with "stop sending" parameters.
165 void ClearAudioSend();
166
deadbeef20cb0c12017-02-01 20:27:00 -0800167 sigslot::signal0<> SignalDestroyed;
168
Steve Anton47136dd2018-01-12 10:49:35 -0800169 rtc::Thread* const worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800170 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700171 std::vector<std::string> stream_ids_;
Florent Castelli892acf02018-10-01 22:47:20 +0200172 RtpParameters init_parameters_;
Steve Anton47136dd2018-01-12 10:49:35 -0800173 cricket::VoiceMediaChannel* media_channel_ = nullptr;
Steve Anton111fdfd2018-06-25 13:03:36 -0700174 StatsCollector* stats_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800175 rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef20cb0c12017-02-01 20:27:00 -0800176 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200177 absl::optional<std::string> last_transaction_id_;
ossu7bb87ee2017-01-23 04:56:25 -0800178 uint32_t ssrc_ = 0;
179 bool cached_track_enabled_ = false;
180 bool stopped_ = false;
181
182 // Used to pass the data callback from the |track_| to the other end of
183 // cricket::AudioSource.
184 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100185 int attachment_id_ = 0;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700186 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
ossu7bb87ee2017-01-23 04:56:25 -0800187};
188
189class VideoRtpSender : public ObserverInterface,
190 public rtc::RefCountedObject<RtpSenderInternal> {
191 public:
Steve Anton111fdfd2018-06-25 13:03:36 -0700192 // Construct an RtpSender for video with the given sender ID.
193 // The sender is initialized with no track to send and no associated streams.
194 VideoRtpSender(rtc::Thread* worker_thread, const std::string& id);
ossu7bb87ee2017-01-23 04:56:25 -0800195
196 virtual ~VideoRtpSender();
197
198 // ObserverInterface implementation
199 void OnChanged() override;
200
201 // RtpSenderInterface implementation
202 bool SetTrack(MediaStreamTrackInterface* track) override;
203 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
204 return track_;
205 }
206
207 uint32_t ssrc() const override { return ssrc_; }
208
209 cricket::MediaType media_type() const override {
210 return cricket::MEDIA_TYPE_VIDEO;
211 }
212
213 std::string id() const override { return id_; }
214
Steve Anton8ffb9c32017-08-31 15:45:38 -0700215 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800216
Florent Castelli892acf02018-10-01 22:47:20 +0200217 void set_init_send_encodings(
218 const std::vector<RtpEncodingParameters>& init_send_encodings) override {
219 init_parameters_.encodings = init_send_encodings;
220 }
221 std::vector<RtpEncodingParameters> init_send_encodings() const override {
222 return init_parameters_.encodings;
223 }
224
Florent Castellicebf50f2018-05-03 15:31:53 +0200225 RtpParameters GetParameters() override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800226 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 04:56:25 -0800227
deadbeef20cb0c12017-02-01 20:27:00 -0800228 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
229
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700230 void SetFrameEncryptor(
231 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
232
233 rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor()
234 const override;
235
ossu7bb87ee2017-01-23 04:56:25 -0800236 // RtpSenderInternal implementation.
237 void SetSsrc(uint32_t ssrc) override;
238
Steve Anton8ffb9c32017-08-31 15:45:38 -0700239 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
240 stream_ids_ = stream_ids;
241 }
ossu7bb87ee2017-01-23 04:56:25 -0800242
243 void Stop() override;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100244 int AttachmentId() const override { return attachment_id_; }
ossu7bb87ee2017-01-23 04:56:25 -0800245
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800246 void SetMediaChannel(cricket::MediaChannel* media_channel) override;
ossu7bb87ee2017-01-23 04:56:25 -0800247
248 private:
249 bool can_send_track() const { return track_ && ssrc_; }
250 // Helper function to construct options for
251 // VideoProviderInterface::SetVideoSend.
252 void SetVideoSend();
253 // Helper function to call SetVideoSend with "stop sending" parameters.
254 void ClearVideoSend();
255
Steve Anton47136dd2018-01-12 10:49:35 -0800256 rtc::Thread* worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800257 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700258 std::vector<std::string> stream_ids_;
Florent Castelli892acf02018-10-01 22:47:20 +0200259 RtpParameters init_parameters_;
Steve Anton47136dd2018-01-12 10:49:35 -0800260 cricket::VideoMediaChannel* media_channel_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800261 rtc::scoped_refptr<VideoTrackInterface> track_;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200262 absl::optional<std::string> last_transaction_id_;
ossu7bb87ee2017-01-23 04:56:25 -0800263 uint32_t ssrc_ = 0;
ossu7bb87ee2017-01-23 04:56:25 -0800264 VideoTrackInterface::ContentHint cached_track_content_hint_ =
265 VideoTrackInterface::ContentHint::kNone;
266 bool stopped_ = false;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100267 int attachment_id_ = 0;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700268 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
ossu7bb87ee2017-01-23 04:56:25 -0800269};
270
271} // namespace webrtc
272
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200273#endif // PC_RTPSENDER_H_