blob: 2a8289f417eac94e2483a98be7f78b6bbd7f827c [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.
39 // SetVoiceMediaChannel should be used for audio RtpSenders and
40 // SetVideoMediaChannel should be used for video RtpSenders. Must call the
41 // appropriate SetXxxMediaChannel(nullptr) before the media channel is
42 // destroyed.
43 virtual void SetVoiceMediaChannel(
44 cricket::VoiceMediaChannel* voice_media_channel) = 0;
45 virtual void SetVideoMediaChannel(
46 cricket::VideoMediaChannel* video_media_channel) = 0;
47
ossu7bb87ee2017-01-23 04:56:25 -080048 // Used to set the SSRC of the sender, once a local description has been set.
49 // If |ssrc| is 0, this indiates that the sender should disconnect from the
50 // underlying transport (this occurs if the sender isn't seen in a local
51 // description).
52 virtual void SetSsrc(uint32_t ssrc) = 0;
53
Steve Anton8ffb9c32017-08-31 15:45:38 -070054 virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
Florent Castelli892acf02018-10-01 22:47:20 +020055 virtual void set_init_send_encodings(
56 const std::vector<RtpEncodingParameters>& init_send_encodings) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080057
58 virtual void Stop() = 0;
Steve Anton57858b32018-02-15 15:19:50 -080059
60 // Returns an ID that changes every time SetTrack() is called, but
61 // otherwise remains constant. Used to generate IDs for stats.
62 // The special value zero means that no track is attached.
63 virtual int AttachmentId() const = 0;
ossu7bb87ee2017-01-23 04:56:25 -080064};
65
66// LocalAudioSinkAdapter receives data callback as a sink to the local
67// AudioTrack, and passes the data to the sink of AudioSource.
68class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
69 public cricket::AudioSource {
70 public:
71 LocalAudioSinkAdapter();
72 virtual ~LocalAudioSinkAdapter();
73
74 private:
75 // AudioSinkInterface implementation.
76 void OnData(const void* audio_data,
77 int bits_per_sample,
78 int sample_rate,
79 size_t number_of_channels,
80 size_t number_of_frames) override;
81
82 // cricket::AudioSource implementation.
83 void SetSink(cricket::AudioSource::Sink* sink) override;
84
85 cricket::AudioSource::Sink* sink_;
86 // Critical section protecting |sink_|.
87 rtc::CriticalSection lock_;
88};
89
deadbeef20cb0c12017-02-01 20:27:00 -080090class AudioRtpSender : public DtmfProviderInterface,
91 public ObserverInterface,
ossu7bb87ee2017-01-23 04:56:25 -080092 public rtc::RefCountedObject<RtpSenderInternal> {
93 public:
94 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
95 // at the appropriate times.
ossu7bb87ee2017-01-23 04:56:25 -080096
Steve Anton111fdfd2018-06-25 13:03:36 -070097 // Construct an RtpSender for audio with the given sender ID.
98 // The sender is initialized with no track to send and no associated streams.
Steve Anton47136dd2018-01-12 10:49:35 -080099 AudioRtpSender(rtc::Thread* worker_thread,
Steve Anton111fdfd2018-06-25 13:03:36 -0700100 const std::string& id,
Steve Anton02ee47c2018-01-10 16:26:06 -0800101 StatsCollector* stats);
ossu7bb87ee2017-01-23 04:56:25 -0800102
103 virtual ~AudioRtpSender();
104
deadbeef20cb0c12017-02-01 20:27:00 -0800105 // DtmfSenderProvider implementation.
106 bool CanInsertDtmf() override;
107 bool InsertDtmf(int code, int duration) override;
108 sigslot::signal0<>* GetOnDestroyedSignal() override;
109
110 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800111 void OnChanged() override;
112
deadbeef20cb0c12017-02-01 20:27:00 -0800113 // RtpSenderInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800114 bool SetTrack(MediaStreamTrackInterface* track) override;
115 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
116 return track_;
117 }
118
119 uint32_t ssrc() const override { return ssrc_; }
120
121 cricket::MediaType media_type() const override {
122 return cricket::MEDIA_TYPE_AUDIO;
123 }
124
125 std::string id() const override { return id_; }
126
Steve Anton8ffb9c32017-08-31 15:45:38 -0700127 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800128
Florent Castellicebf50f2018-05-03 15:31:53 +0200129 RtpParameters GetParameters() override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800130 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 04:56:25 -0800131
deadbeef20cb0c12017-02-01 20:27:00 -0800132 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
133
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700134 void SetFrameEncryptor(
135 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
136
137 rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor()
138 const override;
139
ossu7bb87ee2017-01-23 04:56:25 -0800140 // RtpSenderInternal implementation.
141 void SetSsrc(uint32_t ssrc) override;
142
Steve Anton8ffb9c32017-08-31 15:45:38 -0700143 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
144 stream_ids_ = stream_ids;
145 }
ossu7bb87ee2017-01-23 04:56:25 -0800146
Florent Castelli892acf02018-10-01 22:47:20 +0200147 void set_init_send_encodings(
148 const std::vector<RtpEncodingParameters>& init_send_encodings) override {
149 init_parameters_.encodings = init_send_encodings;
150 }
151 std::vector<RtpEncodingParameters> init_send_encodings() const override {
152 return init_parameters_.encodings;
153 }
154
ossu7bb87ee2017-01-23 04:56:25 -0800155 void Stop() override;
156
Harald Alvestrandc72af932018-01-11 17:18:19 +0100157 int AttachmentId() const override { return attachment_id_; }
158
Steve Anton57858b32018-02-15 15:19:50 -0800159 void SetVoiceMediaChannel(
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700160 cricket::VoiceMediaChannel* voice_media_channel) override;
161
Steve Anton57858b32018-02-15 15:19:50 -0800162 void SetVideoMediaChannel(
163 cricket::VideoMediaChannel* video_media_channel) override {
164 RTC_NOTREACHED();
Steve Anton47136dd2018-01-12 10:49:35 -0800165 }
ossu7bb87ee2017-01-23 04:56:25 -0800166
167 private:
168 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
169 // some other way to test if we have a valid SSRC.
170 bool can_send_track() const { return track_ && ssrc_; }
171 // Helper function to construct options for
172 // AudioProviderInterface::SetAudioSend.
173 void SetAudioSend();
174 // Helper function to call SetAudioSend with "stop sending" parameters.
175 void ClearAudioSend();
176
deadbeef20cb0c12017-02-01 20:27:00 -0800177 sigslot::signal0<> SignalDestroyed;
178
Steve Anton47136dd2018-01-12 10:49:35 -0800179 rtc::Thread* const worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800180 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700181 std::vector<std::string> stream_ids_;
Florent Castelli892acf02018-10-01 22:47:20 +0200182 RtpParameters init_parameters_;
Steve Anton47136dd2018-01-12 10:49:35 -0800183 cricket::VoiceMediaChannel* media_channel_ = nullptr;
Steve Anton111fdfd2018-06-25 13:03:36 -0700184 StatsCollector* stats_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800185 rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef20cb0c12017-02-01 20:27:00 -0800186 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200187 absl::optional<std::string> last_transaction_id_;
ossu7bb87ee2017-01-23 04:56:25 -0800188 uint32_t ssrc_ = 0;
189 bool cached_track_enabled_ = false;
190 bool stopped_ = false;
191
192 // Used to pass the data callback from the |track_| to the other end of
193 // cricket::AudioSource.
194 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100195 int attachment_id_ = 0;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700196 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
ossu7bb87ee2017-01-23 04:56:25 -0800197};
198
199class VideoRtpSender : public ObserverInterface,
200 public rtc::RefCountedObject<RtpSenderInternal> {
201 public:
Steve Anton111fdfd2018-06-25 13:03:36 -0700202 // Construct an RtpSender for video with the given sender ID.
203 // The sender is initialized with no track to send and no associated streams.
204 VideoRtpSender(rtc::Thread* worker_thread, const std::string& id);
ossu7bb87ee2017-01-23 04:56:25 -0800205
206 virtual ~VideoRtpSender();
207
208 // ObserverInterface implementation
209 void OnChanged() override;
210
211 // RtpSenderInterface implementation
212 bool SetTrack(MediaStreamTrackInterface* track) override;
213 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
214 return track_;
215 }
216
217 uint32_t ssrc() const override { return ssrc_; }
218
219 cricket::MediaType media_type() const override {
220 return cricket::MEDIA_TYPE_VIDEO;
221 }
222
223 std::string id() const override { return id_; }
224
Steve Anton8ffb9c32017-08-31 15:45:38 -0700225 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800226
Florent Castelli892acf02018-10-01 22:47:20 +0200227 void set_init_send_encodings(
228 const std::vector<RtpEncodingParameters>& init_send_encodings) override {
229 init_parameters_.encodings = init_send_encodings;
230 }
231 std::vector<RtpEncodingParameters> init_send_encodings() const override {
232 return init_parameters_.encodings;
233 }
234
Florent Castellicebf50f2018-05-03 15:31:53 +0200235 RtpParameters GetParameters() override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800236 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 04:56:25 -0800237
deadbeef20cb0c12017-02-01 20:27:00 -0800238 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
239
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700240 void SetFrameEncryptor(
241 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
242
243 rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor()
244 const override;
245
ossu7bb87ee2017-01-23 04:56:25 -0800246 // RtpSenderInternal implementation.
247 void SetSsrc(uint32_t ssrc) override;
248
Steve Anton8ffb9c32017-08-31 15:45:38 -0700249 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
250 stream_ids_ = stream_ids;
251 }
ossu7bb87ee2017-01-23 04:56:25 -0800252
253 void Stop() override;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100254 int AttachmentId() const override { return attachment_id_; }
ossu7bb87ee2017-01-23 04:56:25 -0800255
Steve Anton57858b32018-02-15 15:19:50 -0800256 void SetVoiceMediaChannel(
257 cricket::VoiceMediaChannel* voice_media_channel) override {
258 RTC_NOTREACHED();
259 }
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700260
Steve Anton57858b32018-02-15 15:19:50 -0800261 void SetVideoMediaChannel(
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700262 cricket::VideoMediaChannel* video_media_channel) override;
ossu7bb87ee2017-01-23 04:56:25 -0800263
264 private:
265 bool can_send_track() const { return track_ && ssrc_; }
266 // Helper function to construct options for
267 // VideoProviderInterface::SetVideoSend.
268 void SetVideoSend();
269 // Helper function to call SetVideoSend with "stop sending" parameters.
270 void ClearVideoSend();
271
Steve Anton47136dd2018-01-12 10:49:35 -0800272 rtc::Thread* worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800273 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700274 std::vector<std::string> stream_ids_;
Florent Castelli892acf02018-10-01 22:47:20 +0200275 RtpParameters init_parameters_;
Steve Anton47136dd2018-01-12 10:49:35 -0800276 cricket::VideoMediaChannel* media_channel_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800277 rtc::scoped_refptr<VideoTrackInterface> track_;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200278 absl::optional<std::string> last_transaction_id_;
ossu7bb87ee2017-01-23 04:56:25 -0800279 uint32_t ssrc_ = 0;
ossu7bb87ee2017-01-23 04:56:25 -0800280 VideoTrackInterface::ContentHint cached_track_content_hint_ =
281 VideoTrackInterface::ContentHint::kNone;
282 bool stopped_ = false;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100283 int attachment_id_ = 0;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700284 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
ossu7bb87ee2017-01-23 04:56:25 -0800285};
286
287} // namespace webrtc
288
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200289#endif // PC_RTPSENDER_H_